[PATCH v3 2/3] x86/boot: Uses nm command instead of map file to get symbols

Frediano Ziglio posted 3 patches 2 weeks, 2 days ago
[PATCH v3 2/3] x86/boot: Uses nm command instead of map file to get symbols
Posted by Frediano Ziglio 2 weeks, 2 days ago
combine_two_binaries.py only understands GNU LD's format, and does
not work with LLVM's LLD.

Use nm command instead to get list of symbols; specifically
BSD format as it does not truncate symbols names like sysv one.

Fixes: aa9045e77130 ('x86/boot: Rework how 32bit C is linked/included for early boot')
Signed-off-by: Frediano Ziglio <frediano.ziglio@cloud.com>
---
 xen/arch/x86/boot/Makefile        |  3 ++-
 xen/tools/combine_two_binaries.py | 19 ++++++++++++-------
 2 files changed, 14 insertions(+), 8 deletions(-)

diff --git a/xen/arch/x86/boot/Makefile b/xen/arch/x86/boot/Makefile
index 5f832c5896..d457876659 100644
--- a/xen/arch/x86/boot/Makefile
+++ b/xen/arch/x86/boot/Makefile
@@ -66,7 +66,8 @@ $(obj)/built-in-32.tmp.o: $(obj32)
 # If possible we use --orphan-handling=error option to make sure we account
 # for all possible sections from C code.
 $(obj)/built-in-32.%.bin: $(obj)/build32.%.lds $(obj)/built-in-32.tmp.o
-	$(LD32) $(orphan-handling-y) -N -T $< -Map $(@:bin=map) -o $(@:bin=o) $(filter %.o,$^)
+	$(LD32) $(orphan-handling-y) -N -T $< -o $(@:bin=o) $(filter %.o,$^)
+	$(NM) -p --format=bsd $(@:bin=o) > $(@:bin=map)
 	$(OBJCOPY) -j .text -O binary $(@:bin=o) $@
 	rm -f $(@:bin=o)
 
diff --git a/xen/tools/combine_two_binaries.py b/xen/tools/combine_two_binaries.py
index 447c0d3bdb..581e57cbc0 100755
--- a/xen/tools/combine_two_binaries.py
+++ b/xen/tools/combine_two_binaries.py
@@ -29,7 +29,7 @@ parser.add_argument('--text-diff', dest='text_diff',
 parser.add_argument('--output', dest='output',
                     help='Output file')
 parser.add_argument('--map', dest='mapfile',
-                    help='Map file to read for symbols to export')
+                    help='Map file (NM) to read for symbols to export')
 parser.add_argument('--exports', dest='exports',
                     help='Symbols to export')
 parser.add_argument('--section-header', dest='section_header',
@@ -65,15 +65,20 @@ exports = []
 if args.exports is not None:
     exports = dict([(name, None) for name in args.exports.split(',')])
 
-# Parse mapfile, look for ther symbols we want to export.
+# Parse mapfile, look for symbols we want to export.
 if args.mapfile is not None:
-    symbol_re = re.compile(r'\s{15,}0x([0-9a-f]+)\s+(\S+)\n')
+    exports["dummy_start"] = None
     for line in open(args.mapfile):
-        m = symbol_re.match(line)
-        if not m or m.group(2) not in exports:
+        parts = line.split()
+        if len(parts) != 3:
             continue
-        addr = int(m.group(1), 16)
-        exports[m.group(2)] = addr
+        addr, sym_type, sym = parts
+        if sym_type.upper() == 'T' and sym in exports:
+            exports[sym] = int(addr, 16)
+    if exports["dummy_start"] != 0:
+        raise Exception("dummy_start symbol expected to be present and 0")
+    del exports["dummy_start"]
+
 for (name, addr) in exports.items():
     if addr is None:
         raise Exception("Required export symbols %s not found" % name)
-- 
2.34.1
Re: [PATCH v3 2/3] x86/boot: Uses nm command instead of map file to get symbols
Posted by Andrew Cooper 2 weeks, 2 days ago
On 06/11/2024 1:06 pm, Frediano Ziglio wrote:
> combine_two_binaries.py only understands GNU LD's format, and does
> not work with LLVM's LLD.
>
> Use nm command instead to get list of symbols; specifically
> BSD format as it does not truncate symbols names like sysv one.
>
> Fixes: aa9045e77130 ('x86/boot: Rework how 32bit C is linked/included for early boot')
> Signed-off-by: Frediano Ziglio <frediano.ziglio@cloud.com>

Much nicer.

Reviewed-by: Andrew Cooper <andrew.cooper3@citrix.com>


Looks like the series is ready to go in now, so I'll take it right away
to unblock FreeBSD testing.

~Andrew