Skip to contentSkip to conversation

x86/boot: Fix build with LLVM toolchain

PATCH In review Frediano Ziglio 2 participants 5 messages Nov 6, 2024

lore
Share & tools
lore
#0 lore raw

v3 cover x86/boot: Fix build with LLVM toolchain

Fix some compatibility generating 32 bit code.
LLVM has different object files and map files.
Do not use map file as they are not standard.

Changes since v1:
- do not use map files;
- add a fix for file names and preprocessor definition name.

Changes since v2:
- address comments.

Frediano Ziglio (3):
x86/boot: Fix intermediate file names to generate 32 bit code x86/boot: Uses nm command instead of map file to get symbols x86/boot: Explicitly list .{sym,shstr,str}tab in build32.lds.S

 xen/arch/x86/boot/Makefile        |  9 +++++----
 xen/arch/x86/boot/build32.lds.S   | 11 ++++++++++-
 xen/tools/combine_two_binaries.py | 19 ++++++++++++-------
 3 files changed, 27 insertions(+), 12 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v3 1/3 x86/boot: Fix intermediate file names to generate 32 bit code

The "base" and "offset" definition were inverted, "base" file should be the files without offsets applied while "offset" should have the offsets applied.
Also update an old usage of "final" to "apply offset" to make more clear and consistent (in former commit messages the "final" term was used instead of "offset").

Fixes aa9045e77130 ('x86/boot: Rework how 32bit C is linked/included for early boot')

---
Anthony was right, it was the opposite

---
 xen/arch/x86/boot/Makefile      | 6 +++---
 xen/arch/x86/boot/build32.lds.S | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)
xen/arch/x86/boot/Makefile +3 -3
index e102bd8c70..5f832c5896 100644
@@ -44,7 +44,7 @@ text_gap := 0x010200
 text_diff := 0x408020
 
 $(obj)/build32.base.lds: AFLAGS-y += -DGAP=$(text_gap) -DTEXT_DIFF=$(text_diff)
-$(obj)/build32.offset.lds: AFLAGS-y += -DGAP=$(text_gap) -DTEXT_DIFF=$(text_diff) -DFINAL
+$(obj)/build32.offset.lds: AFLAGS-y += -DGAP=$(text_gap) -DTEXT_DIFF=$(text_diff) -DAPPLY_OFFSET
 $(obj)/build32.base.lds $(obj)/build32.offset.lds: $(src)/build32.lds.S FORCE
 	$(call if_changed_dep,cpp_lds_S)
 
@@ -75,10 +75,10 @@ cmd_combine = \
     $(PYTHON) $(srctree)/tools/combine_two_binaries.py \
               --gap       $(text_gap) \
               --text-diff $(text_diff) \
-              --script    $(obj)/build32.offset.lds \
+              --script    $(obj)/build32.base.lds \
               --bin1      $(obj)/built-in-32.base.bin \
               --bin2      $(obj)/built-in-32.offset.bin \
-              --map       $(obj)/built-in-32.offset.map \
+              --map       $(obj)/built-in-32.base.map \
               --exports   cmdline_parse_early,reloc,reloc_trampoline32 \
               --output    $@
 
xen/arch/x86/boot/build32.lds.S +1 -1
index f20fc18977..9b29f0184f 100644
@@ -15,7 +15,7 @@
  * with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#ifdef FINAL
+#ifndef APPLY_OFFSET
 # undef GAP
 # define GAP 0
 # define MULT 0
Signature · 1 line
2.34.1
#2 lore raw

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

In reply to Frediano Ziglio

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')
---
 xen/arch/x86/boot/Makefile        |  3 ++-
 xen/tools/combine_two_binaries.py | 19 ++++++++++++-------
 2 files changed, 14 insertions(+), 8 deletions(-)
xen/arch/x86/boot/Makefile +2 -1
index 5f832c5896..d457876659 100644
@@ -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)
 
xen/tools/combine_two_binaries.py +12 -7
index 447c0d3bdb..581e57cbc0 100755
@@ -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)
Signature · 1 line
2.34.1
Reviewed-by on v3 2/3
#3 lore raw

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

Quoted 8 lines · Frediano Ziglio

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')

Much nicer.

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

~Andrew

#4 lore raw

PATCH v3 3/3 x86/boot: Explicitly list .{sym,shstr,str}tab in build32.lds.S

In reply to Frediano Ziglio

Currently, building with LLVM's LLD fails:

ld -melf_i386_fbsd --orphan-handling=error -N -T ...
ld: error: <internal>:(.symtab) is being placed in '.symtab'
ld: error: <internal>:(.shstrtab) is being placed in '.shstrtab'
ld: error: <internal>:(.strtab) is being placed in '.strtab' gmake[11]: *** [arch/x86/boot/Makefile:69:
arch/x86/boot/built-in-32.base.bin] Error 1

This is a consequence of --orphan-handling, and it appears that Binutils doesn't diagnose some orphaned sections even explicitly asked to do so.

List the sections explicitly.

Fixes aa9045e77130 ('x86/boot: Rework how 32bit C is linked/included for early boot')
---
 xen/arch/x86/boot/build32.lds.S | 9 +++++++++
 1 file changed, 9 insertions(+)
xen/arch/x86/boot/build32.lds.S +9 -0
index 9b29f0184f..1e59732edd 100644
@@ -66,6 +66,15 @@ SECTIONS
        *(.comment.*)
        *(.note.*)
   }
+  .shstrtab : {
+       *(.shstrtab)
+  }
+  .strtab : {
+       *(.strtab)
+  }
+  .symtab : {
+       *(.symtab)
+  }
   /* Dynamic linkage sections.  Collected simply so we can check they're empty. */
   .got : {
         *(.got)
Signature · 1 line
2.34.1