This is an automated email from the ASF dual-hosted git repository.

GUIDINGLI pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new c9de25e1b43 tools: add symbolshare.py
c9de25e1b43 is described below

commit c9de25e1b437be064b5804b327c93fcfe8a84ae8
Author: anjiahao <[email protected]>
AuthorDate: Wed Apr 16 17:31:46 2025 +0800

    tools: add symbolshare.py
    
    Add a tool for protected builds that shares a section of the kernel
    ELF with the user ELF.  It extracts the symbols from the requested
    sections of the kernel ELF, checks whether they are already defined
    in the user link library, and generates a linker script that assigns
    the shared symbol addresses.
    
    Usage:
      symbolshare.py -e kernel.elf -s section1 [section2] -a user.linklib
    
    Signed-off-by: anjiahao <[email protected]>
---
 tools/symbolshare.py | 118 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 118 insertions(+)

diff --git a/tools/symbolshare.py b/tools/symbolshare.py
new file mode 100755
index 00000000000..3f5b2539d53
--- /dev/null
+++ b/tools/symbolshare.py
@@ -0,0 +1,118 @@
+#!/usr/bin/env python3
+# tools/symbolshare.py
+#
+# SPDX-License-Identifier: Apache-2.0
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.  The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations
+# under the License.
+#
+
+import argparse
+import os
+import subprocess
+import sys
+import tempfile
+
+import lief
+
+
+def find_symbol_in_section(elf_path, section_names):
+    symbols = []
+    sections = []
+    binary = lief.parse(elf_path)
+    for section_name in section_names:
+        section = binary.get_section(section_name)
+        if section is None:
+            print(f"Section '{section_name}' not found in {elf_path}")
+            continue
+
+        sections.append(
+            (
+                section.virtual_address,
+                section.virtual_address + section.size,
+                section_name,
+            )
+        )
+
+    for symbol in binary.symbols:
+        for start, end, section_name in sections:
+            if (
+                symbol.value < end
+                and symbol.value >= start
+                and symbol.type == lief.ELF.Symbol.TYPE.OBJECT
+            ):
+                symbols.append((symbol.name, symbol.value, symbol.size, 
section_name))
+
+    return symbols
+
+
+def objfile_iter(path):
+    for p in path:
+        temp_dir = tempfile.mkdtemp()
+        subprocess.run(["ar", "x", os.path.abspath(p)], cwd=temp_dir)
+        for filename in os.listdir(temp_dir):
+            yield os.path.join(temp_dir, filename)
+
+
+def find_symbols_in_library(libpath):
+    undefined_names = []
+    defined_names = []
+
+    for obj in objfile_iter(libpath):
+        binary = lief.parse(obj)
+        for symbol in binary.symbols:
+            if symbol.type == lief.ELF.Symbol.TYPE.NOTYPE:
+                undefined_names.append(symbol.name)
+            else:
+                defined_names.append(symbol.name)
+
+    defined_names = set(defined_names)
+    undefined_names = list(set(undefined_names) - defined_names)
+    return (defined_names, undefined_names)
+
+
+def args_parser():
+    parser = argparse.ArgumentParser(description="Show symbols in ELF 
sections.")
+    parser.add_argument("-e", "--elf", required=True, help="Path to the ELF 
file")
+    parser.add_argument("-l", "--ld", default="tmp.ld", help="Output link 
script")
+    parser.add_argument(
+        "-s", "--section", required=True, nargs="+", help="Section names to 
inspect"
+    )
+    parser.add_argument(
+        "-a",
+        "--libs",
+        required=True,
+        nargs="+",
+        help="Library paths to inspect for symbols",
+    )
+    return parser.parse_args()
+
+
+if __name__ == "__main__":
+    args = args_parser()
+    section_symbols = find_symbol_in_section(args.elf, args.section)
+    defined_names, undefined_names = find_symbols_in_library(args.libs)
+
+    section_names = {symbol[0] for symbol in section_symbols}
+    common_names = section_names.intersection(defined_names)
+    if len(common_names) != 0:
+        print("Common symbols between section and library:")
+        for symbol in common_names:
+            print(symbol)
+        sys.exit(-1)
+
+    with open(args.ld, "w") as fd:
+        for symbol in section_symbols:
+            fd.write(f"{symbol[0]} = 0x{symbol[1]:x};\n")

Reply via email to