Skip to contentSkip to conversation

Various toolchain compatibility fixes/improvements

PATCH In review Sam Edwards 5 participants 24 messages Mar 15, 2025 – Apr 3, 2025

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v2 cover Various toolchain compatibility fixes/improvements

Hello again U-Boot list,

This is v2 of my "misc. fixes" series, sent to prepare the codebase for more direct LLVM support in the near future. This series contains several fixes that I found in the process of preparing that support and which address issues independent of any future feature or enhancement. I am sending these now, both so that their inclusion is not delayed by discussion on my upcoming series and to make the latter more manageable.

PLEASE APPLY PARTIALLY! I have tried to sort these so that the most straightforward changes come first. If any patch proves contentious, you can either skip it or stop applying the series at that point. Any patches that need revisions will be included in a future series, but I'm trying to avoid there being a v3 of *this* series. :)

Happy Saturday,
Sam

Changes v1->v2:
- Carried forward acked/reviewed tags
- Dropped two patches (one was incorrect, the other can be fixed differently)
- Minor phrasing changes to commit messages
- Instead of removing ENTRY(_start), I instead added .globl _start where appropriate, in the EFI apps

Sam Edwards (15):
arm: Remove stray .mmutable reference in linker script
arm: Exclude eabi_compat from LTO
arm: Add __aeabi_memclr in eabi_compat
arm: Add aligned-memory aliases to eabi_compat
arm: Discard unwanted sections in linker script
arm: Replace 'adrl' in EFI crt0
x86: Fix call64's section flags
makefile: Avoid objcopy --gap-fill for .hex/.srec
makefile: Add norelro linker option
makefile: Add READELF command variable
arm: riscv: efi: Export _start symbol from crt0_*_efi stubs
efi_loader: Move .dynamic out of .text in EFI scripts/Makefile.lib: efi: Preserve the .dynstr section as well
spl: riscv: opensbi: Error on misaligned FDT
spl: Align FDT load address

 Makefile                        | 14 ++++++++------
 arch/arm/cpu/u-boot.lds         | 29 ++++++++++-------------------
 arch/arm/lib/Makefile           |  1 +
 arch/arm/lib/crt0_aarch64_efi.S |  1 +
 arch/arm/lib/crt0_arm_efi.S     |  4 +++-
 arch/arm/lib/eabi_compat.c      | 17 +++++++++++++++++
 arch/riscv/lib/crt0_riscv_efi.S |  1 +
 arch/x86/cpu/i386/call64.S      |  2 +-
 common/spl/spl_fit.c            |  2 +-
 common/spl/spl_opensbi.c        |  5 +++++
 lib/efi_loader/elf_efi.ldsi     |  6 +++---
 scripts/Makefile.lib            |  4 ++--
 12 files changed, 53 insertions(+), 33 deletions(-)
Signature · 1 line
2.48.1
#1 lore raw

PATCH v2 1/15 arm: Remove stray .mmutable reference in linker script

The .mmutable section was deprecated in 2012 [1] and finally removed entirely from U-Boot in 2022 [2], so this special handling is no longer necessary. Remove it to tidy up the linker script.

[1]: dde3b70dcf3d ("arm: add a common .lds link script") [2]: 3135ba642f9a ("arm: pxa: Remove CONFIG_CPU_PXA25X")

---
 arch/arm/cpu/u-boot.lds | 9 ---------
 1 file changed, 9 deletions(-)
arch/arm/cpu/u-boot.lds +0 -9
index 2f50087f57a..63e82a09fad 100644
@@ -169,15 +169,6 @@ SECTIONS
 	_end = .;
 	_image_binary_end = .;
 
-	/*
-	 * Deprecated: this MMU section is used by pxa at present but
-	 * should not be used by new boards/CPUs.
-	 */
-	. = ALIGN(4096);
-	.mmutable : {
-		*(.mmutable)
-	}
-
 /*
  * These sections occupy the same memory, but their lifetimes do
  * not overlap: U-Boot initializes .bss only after applying dynamic
Signature · 1 line
2.48.1
#2 lore raw

PATCH v2 2/15 arm: Exclude eabi_compat from LTO

In reply to Sam Edwards

These symbols need to survive the IR-level dead function elimination pass, since nothing at the IR level is referencing them (calls to these are inserted later, at codegen time).

---
 arch/arm/lib/Makefile | 1 +
 1 file changed, 1 insertion(+)
arch/arm/lib/Makefile +1 -0
index 1c95dd6fed2..74cd5051552 100644
@@ -92,6 +92,7 @@ obj-$(CONFIG_GENERATE_ACPI_TABLE) += acpi_table.o
 # For EABI conformant tool chains, provide eabi_compat()
 ifneq (,$(findstring -mabi=aapcs-linux,$(PLATFORM_CPPFLAGS)))
 extra-y	+= eabi_compat.o
+CFLAGS_REMOVE_eabi_compat.o := $(LTO_CFLAGS)
 endif
 
 # some files can only build in ARM or THUMB2, not THUMB1
Signature · 1 line
2.48.1
#3 lore raw

PATCH v2 3/15 arm: Add __aeabi_memclr in eabi_compat

In reply to Sam Edwards

LLVM's code generator will sometimes emit calls to __aeabi_memclr. Add an implementation of this for LLVM compatibility.

---
 arch/arm/lib/eabi_compat.c | 5 +++++
 1 file changed, 5 insertions(+)
arch/arm/lib/eabi_compat.c +5 -0
index 602efe04c04..e4190c049a3 100644
@@ -37,3 +37,8 @@ void __aeabi_memset(void *dest, size_t n, int c)
 {
 	(void) memset(dest, c, n);
 }
+
+void __aeabi_memclr(void *dest, size_t n)
+{
+	(void) memset(dest, 0, n);
+}
Signature · 1 line
2.48.1
#4 lore raw

PATCH v2 4/15 arm: Add aligned-memory aliases to eabi_compat

In reply to Sam Edwards

These are sometimes used by LLVM's code-generator, when it can guarantee that the memory buffer being passed is aligned on a (4- or 8-byte) boundary. They can safely be aliased to the unaligned versions.

---
 arch/arm/lib/eabi_compat.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
arch/arm/lib/eabi_compat.c +12 -0
index e4190c049a3..e6cafcc5f2b 100644
@@ -33,12 +33,24 @@ void __aeabi_memcpy(void *dest, const void *src, size_t n)
 	(void) memcpy(dest, src, n);
 }
 
+void __aeabi_memcpy4(void *dest, const void *src, size_t n) __alias(__aeabi_memcpy);
+
+void __aeabi_memcpy8(void *dest, const void *src, size_t n) __alias(__aeabi_memcpy);
+
 void __aeabi_memset(void *dest, size_t n, int c)
 {
 	(void) memset(dest, c, n);
 }
 
+void __aeabi_memset4(void *dest, size_t n, int c) __alias(__aeabi_memset);
+
+void __aeabi_memset8(void *dest, size_t n, int c) __alias(__aeabi_memset);
+
 void __aeabi_memclr(void *dest, size_t n)
 {
 	(void) memset(dest, 0, n);
 }
+
+void __aeabi_memclr4(void *dest, size_t n) __alias(__aeabi_memclr);
+
+void __aeabi_memclr8(void *dest, size_t n) __alias(__aeabi_memclr);
Signature · 1 line
2.48.1
#5 lore raw

PATCH v2 5/15 arm: Discard unwanted sections in linker script

In reply to Sam Edwards

There are a handful of sections that are not useful in the U-Boot output binary. At present, the linker script moves these to the end of the binary, after the _image_binary_end marker symbol, so that they don't get loaded.

The linker script syntax supports discarding sections that shouldn't be included in the output. Switch to this instead, to make the intention clearer and reduce the ELF sections that have to be handled later in the build. This is also consistent with the other architectures' linker scripts.

---
 arch/arm/cpu/u-boot.lds | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
arch/arm/cpu/u-boot.lds +10 -10
index 63e82a09fad..817e7a983ae 100644
@@ -181,14 +181,14 @@ SECTIONS
 		__bss_end = .;
 	}
 
-	.dynsym _image_binary_end : { *(.dynsym) }
-	.dynbss : { *(.dynbss) }
-	.dynstr : { *(.dynstr*) }
-	.dynamic : { *(.dynamic*) }
-	.plt : { *(.plt*) }
-	.interp : { *(.interp*) }
-	.gnu.hash : { *(.gnu.hash) }
-	.gnu : { *(.gnu*) }
-	.ARM.exidx : { *(.ARM.exidx*) }
-	.gnu.linkonce.armexidx : { *(.gnu.linkonce.armexidx.*) }
+	/DISCARD/ : { *(.dynsym) }
+	/DISCARD/ : { *(.dynbss) }
+	/DISCARD/ : { *(.dynstr*) }
+	/DISCARD/ : { *(.dynamic*) }
+	/DISCARD/ : { *(.plt*) }
+	/DISCARD/ : { *(.interp*) }
+	/DISCARD/ : { *(.gnu.hash) }
+	/DISCARD/ : { *(.gnu*) }
+	/DISCARD/ : { *(.ARM.exidx*) }
+	/DISCARD/ : { *(.gnu.linkonce.armexidx.*) }
 }
Signature · 1 line
2.48.1
#7 lore raw

PATCH v2 6/15 arm: Replace 'adrl' in EFI crt0

In reply to Sam Edwards

LLVM's IAS does not (and cannot easily) support the 'adrl' pseudoinstruction, and ARM developers generally do not consider it portable across assembler implementations either.

Instead, expand it into the two subtract instructions it would emit anyway. An explanation of the math follows:

The .+8 and .+4 refer to the same memory location; this is because the .+4 expression occurs in a subsequent instruction, 4 bytes after the first. This memory location is the value of the PC register when it is read by the first sub instruction. Thus, both inner parenthesized expressions evaluate to the same result: PC's offset relative to image_base. The subtract instructions then remove one byte each (low, then high) of the total offset, thereby getting the absolute address of image_base loaded in r0.

---
 arch/arm/lib/crt0_arm_efi.S | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm/lib/crt0_arm_efi.S +2 -1
index 91b0fe12c51..235b3a0c48f 100644
@@ -149,7 +149,8 @@ _start:
 	adr		r1, .L_DYNAMIC
 	ldr		r0, [r1]
 	add		r1, r0, r1
-	adrl		r0, image_base
+	sub		r0, pc, #((.+8-image_base) & 0xff)
+	sub		r0, r0, #((.+4-image_base) & 0xff00)
 	bl		_relocate
 	teq		r0, #0
 	bne		0f
Signature · 1 line
2.48.1
#9 lore raw

PATCH v2 7/15 x86: Fix call64's section flags

In reply to Sam Edwards

When a section is not flagged with SHF_ALLOC, LLD's --gc-sections algorithm fails to visit the sections that it references. As a result of this, LLD was dropping the call64.o(.data) section, which is itself only referenced by .text_call64.

This appears to be a bug in LLD, but the .section directive for .text_call64 should really have the correct flags either way.

Add "ax" to mark the section as ALLOC ("supposed to be loaded") and CODE ("supposed to be executed").

Fixes 7dc82591d68e2a ("x86: Move call64 into its own section")
---
 arch/x86/cpu/i386/call64.S | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/cpu/i386/call64.S +1 -1
index d81bcc6f8f4..424732fa3fa 100644
@@ -10,7 +10,7 @@
 #include <asm/processor-flags.h>
 
 .code32
-.section .text_call64
+.section .text_call64, "ax"
 .globl cpu_call64
 cpu_call64:
 	/*
Signature · 1 line
2.48.1
#10 lore raw

PATCH v2 8/15 makefile: Avoid objcopy --gap-fill for .hex/.srec

In reply to Sam Edwards

This flag only makes sense for binary output, because .hex/.srec are sparse formats and represent gaps without filler. While the GNU binutils version of objcopy does not seem to mind the extra flag being passed, llvm-objcopy considers this a fatal error.

There is already a version of the objcopy command template in the Makefile that doesn't use --gap-fill, which is provided for EFI. So use this other version for all .hex/.srec outputs as well.

---
 Makefile | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
Makefile +5 -5
index 0694c425438..9c353af17cd 100644
@@ -1067,7 +1067,7 @@ quiet_cmd_objcopy = OBJCOPY $@
 cmd_objcopy = $(OBJCOPY) --gap-fill=0xff $(OBJCOPYFLAGS) \
 	$(OBJCOPYFLAGS_$(@F)) $< $@
 
-# Provide a version which does not do this, for use by EFI
+# Provide a version which does not do this, for use by EFI and hex/srec
 quiet_cmd_zobjcopy = OBJCOPY $@
 cmd_zobjcopy = $(OBJCOPY) $(OBJCOPYFLAGS) $(OBJCOPYFLAGS_$(@F)) $< $@
 
@@ -1282,7 +1282,7 @@ OBJCOPYFLAGS_u-boot.hex := -O ihex
 OBJCOPYFLAGS_u-boot.srec := -O srec
 
 u-boot.hex u-boot.srec: u-boot FORCE
-	$(call if_changed,objcopy)
+	$(call if_changed,zobjcopy)
 
 OBJCOPYFLAGS_u-boot-elf.srec := $(OBJCOPYFLAGS_u-boot.srec)
 
@@ -1296,12 +1296,12 @@ OBJCOPYFLAGS_u-boot-elf.srec += --change-addresses=0x50000000
 endif
 
 u-boot-elf.srec: u-boot.elf FORCE
-	$(call if_changed,objcopy)
+	$(call if_changed,zobjcopy)
 
 OBJCOPYFLAGS_u-boot-spl.srec = $(OBJCOPYFLAGS_u-boot.srec)
 
 spl/u-boot-spl.srec: spl/u-boot-spl FORCE
-	$(call if_changed,objcopy)
+	$(call if_changed,zobjcopy)
 
 %.scif: %.srec
 	$(Q)$(MAKE) $(build)=arch/arm/mach-renesas $@
@@ -1436,7 +1436,7 @@ OBJCOPYFLAGS_u-boot.ldr.hex := -I binary -O ihex
 OBJCOPYFLAGS_u-boot.ldr.srec := -I binary -O srec
 
 u-boot.ldr.hex u-boot.ldr.srec: u-boot.ldr FORCE
-	$(call if_changed,objcopy)
+	$(call if_changed,zobjcopy)
 
 ifdef CONFIG_SPL_LOAD_FIT
 MKIMAGEFLAGS_u-boot.img = -f auto -A $(ARCH) -T firmware -C none -O u-boot \
Signature · 1 line
2.48.1
#12 lore raw

PATCH v2 9/15 makefile: Add `norelro` linker option

In reply to Sam Edwards

RELRO is an instruction to a dynamic loader to make a memory range read-only after relocations are applied, for added security. Some linkers (e.g. LLD) require that all sections covered by the RELRO are contiguous, so that only a single RELRO is needed. U-Boot at present neither satisfies this requirement (e.g. x86_64 linker script currently puts .dynamic too far from .got) nor preserves the RELRO when converting away from ELF, therefore add -z norelro to global linker options.

This can be brought back in the future when the linker scripts are cleaned up and U-Boot understands RELROs.

---
 Makefile | 1 +
 1 file changed, 1 insertion(+)
Makefile +1 -0
index 9c353af17cd..05a93813afa 100644
@@ -820,6 +820,7 @@ KBUILD_AFLAGS += $(KAFLAGS)
 KBUILD_CFLAGS += $(KCFLAGS)
 
 KBUILD_LDFLAGS  += -z noexecstack
+KBUILD_LDFLAGS  += -z norelro
 KBUILD_LDFLAGS  += $(call ld-option,--no-warn-rwx-segments)
 
 KBUILD_HOSTCFLAGS += $(if $(CONFIG_TOOLS_DEBUG),-g)
Signature · 1 line
2.48.1
#13 lore raw

PATCH v2 10/15 makefile: Add READELF command variable

In reply to Sam Edwards

This allows setting READELF=llvm-readelf in order to use the LLVM version of the readelf utility. It also aligns with the practice of not using $(CROSS_COMPILE) in any build recipes directly, reducing the number of places where $(CROSS_COMPILE) is used.

---
 Makefile | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Makefile +2 -1
index 05a93813afa..fb5cd67745f 100644
@@ -406,6 +406,7 @@ LDR		= $(CROSS_COMPILE)ldr
 STRIP		= $(CROSS_COMPILE)strip
 OBJCOPY		= $(CROSS_COMPILE)objcopy
 OBJDUMP		= $(CROSS_COMPILE)objdump
+READELF		= $(CROSS_COMPILE)readelf
 LEX		= flex
 YACC		= bison
 AWK		= awk
@@ -2177,7 +2178,7 @@ System.map:	u-boot
 # ARM relocations should all be R_ARM_RELATIVE (32-bit) or
 # R_AARCH64_RELATIVE (64-bit).
 checkarmreloc: u-boot
-	@RELOC="`$(CROSS_COMPILE)readelf -r -W $< | cut -d ' ' -f 4 | \
+	@RELOC="`$(READELF) -r -W $< | cut -d ' ' -f 4 | \
 		grep R_A | sort -u`"; \
 	if test "$$RELOC" != "R_ARM_RELATIVE" -a \
 		 "$$RELOC" != "R_AARCH64_RELATIVE"; then \
Signature · 1 line
2.48.1
#14 lore raw

PATCH v2 11/15 arm: riscv: efi: Export _start symbol from crt0_*_efi stubs

In reply to Sam Edwards

While the _start label is only intended for use locally to populate the (hand-written) PE header, the linker script includes ENTRY(_start) which designates it as the entry point in the output ELF, resulting in linker warnings under some linkers (e.g. LLVM's lld) due to _start not being a globally-visible symbol. Since ELF is only an intermediary build format, and the aforementioned PE header correctly points to _start, the ENTRY(_start) directive could easily be removed to silence this warning.

However, since some developers who are debugging EFI by analyzing the intermediary ELF may appreciate having correct entry-point information, this patch instead promotes the _start labels to global symbols, silencing the linker warning and making the intermediary ELF reflect the true entry point.

This patch doesn't affect the final output binaries in any way.

---
 arch/arm/lib/crt0_aarch64_efi.S | 1 +
 arch/arm/lib/crt0_arm_efi.S     | 1 +
 arch/riscv/lib/crt0_riscv_efi.S | 1 +
 3 files changed, 3 insertions(+)
arch/arm/lib/crt0_aarch64_efi.S +1 -0
index e21b54fdbcb..003d5f83041 100644
@@ -144,6 +144,7 @@ section_table:
 		 IMAGE_SCN_CNT_INITIALIZED_DATA)
 
 	.align		12
+	.globl		_start
 _start:
 	stp		x29, x30, [sp, #-32]!
 	mov		x29, sp
arch/arm/lib/crt0_arm_efi.S +1 -0
index 235b3a0c48f..593ee1e194a 100644
@@ -143,6 +143,7 @@ section_table:
 		 IMAGE_SCN_CNT_INITIALIZED_DATA)
 
 	.align		12
+	.globl		_start
 _start:
 	stmfd		sp!, {r0-r2, lr}
 
arch/riscv/lib/crt0_riscv_efi.S +1 -0
index 9eacbe4a859..f170e4b26d6 100644
@@ -179,6 +179,7 @@ section_table:
 		 IMAGE_SCN_CNT_INITIALIZED_DATA)
 
 	.align		12
+	.globl		_start
 _start:
 	addi		sp, sp, -(SIZE_LONG * 3)
 	SAVE_LONG(a0, 0)
Signature · 1 line
2.48.1
#16 lore raw

PATCH v2 12/15 efi_loader: Move .dynamic out of .text in EFI

In reply to Sam Edwards

EFI applications need to be relocatable. Ordinarily, this is achieved through a PE-format .reloc section, but since that requires toolchain tricks to achieve, U-Boot's EFI applications instead embed ELF-flavored relocation information and use it for self-relocation; thus, the .dynamic section needs to be preserved.

Before this patch, it was tacked on to the end of .text, but this was not proper: A .text section is SHT_PROGBITS, while the .dynamic section is SHT_DYNAMIC. Attempting to combine them like this creates a section type mismatch. While GNU ld doesn't seem to complain, LLVM's lld considers this a fatal linking error.

This patch moves .dynamic out to its own section, so that the output ELF has the correct types. (They're all mashed together when converting to binary anyway, so this patch causes no change in the final .efi output.)

Cc Heinrich Schuchardt <[email protected]>
---
 lib/efi_loader/elf_efi.ldsi | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
lib/efi_loader/elf_efi.ldsi +3 -3
index 190a88fb69e..4fa5ca43872 100644
@@ -21,10 +21,10 @@ SECTIONS
 		*(.gnu.linkonce.t.*)
 		*(.srodata)
 		*(.rodata*)
-		. = ALIGN(16);
-		*(.dynamic);
-		. = ALIGN(512);
 	}
+	. = ALIGN(16);
+	.dynamic : { *(.dynamic) }
+	. = ALIGN(512);
 	.rela.dyn : { *(.rela.dyn) }
 	.rela.plt : { *(.rela.plt) }
 	.rela.got : { *(.rela.got) }
Signature · 1 line
2.48.1
#18 lore raw

PATCH v2 13/15 scripts/Makefile.lib: efi: Preserve the .dynstr section as well

In reply to Sam Edwards

This section is required by .dynamic and llvm-objcopy will exit with a fatal error if it is not also preserved in the output.

---
 scripts/Makefile.lib | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
scripts/Makefile.lib +2 -2
index 18993435eae..275c308154b 100644
@@ -513,8 +513,8 @@ $(obj)/%_efi.S: $(obj)/%.efi
 	$(call cmd,S_efi)
 
 quiet_cmd_efi_objcopy = OBJCOPY $@
-cmd_efi_objcopy = $(OBJCOPY) -j .header -j .text -j .sdata -j .data -j \
-		.dynamic -j .dynsym  -j .rel* -j .rela* -j .reloc \
+cmd_efi_objcopy = $(OBJCOPY) -j .header -j .text -j .sdata -j .data \
+		-j .dynamic -j .dynstr  -j .dynsym -j .rel* -j .reloc \
 		$(if $(EFI_TARGET),$(EFI_TARGET),-O binary) $^ $@
 
 $(obj)/%.efi: $(obj)/%_efi.so
Signature · 1 line
2.48.1
#19 lore raw

PATCH v2 14/15 spl: riscv: opensbi: Error on misaligned FDT

In reply to Sam Edwards

libfdt 1.6.1+ requires the FDT to be 8-byte aligned and returns an error if not. OpenSBI 1.0+ includes this version of libfdt and will also reject misaligned FDTs.

However, OpenSBI cannot indicate the error to the user: since it cannot access the serial console, it can only silently hang. This proved very difficult to diagnose without proper debugging facilities. Therefore, give the U-Boot SPL, which *can* print error messages, an additional check for proper FDT alignment. Hopefully this saves a lot of development cycles if another developer encounters alignment problems.

---
 common/spl/spl_opensbi.c | 5 +++++
 1 file changed, 5 insertions(+)
common/spl/spl_opensbi.c +5 -0
index 5a26d7c31a4..0ed6afeacc6 100644
@@ -57,6 +57,11 @@ void __noreturn spl_invoke_opensbi(struct spl_image_info *spl_image)
 		hang();
 	}
 
+	if (!IS_ALIGNED((uintptr_t)spl_image->fdt_addr, 8)) {
+		pr_err("SPL image loaded an improperly-aligned device tree\n");
+		hang();
+	}
+
 	/*
 	 * Originally, u-boot-spl will place DTB directly after the kernel,
 	 * but the size of the kernel did not include the BSS section, which
Signature · 1 line
2.48.1
#20 lore raw

PATCH v2 15/15 spl: Align FDT load address

In reply to Sam Edwards

While the image size is generally a multiple of 8 bytes, this is not actually guaranteed; some linkers (like LLD) will shave a few bytes off of the end of output sections if there are no content bytes there. Since libfdt imposes a hard rule of 8-byte alignment, make the SPL also be explicit about the alignment when loading the FDT.

---
 common/spl/spl_fit.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
common/spl/spl_fit.c +1 -1
index 49b4df60560..86506d6905c 100644
@@ -397,7 +397,7 @@ static int spl_fit_append_fdt(struct spl_image_info *spl_image,
 	 * Use the address following the image as target address for the
 	 * device tree.
 	 */
-	image_info.load_addr = spl_image->load_addr + spl_image->size;
+	image_info.load_addr = ALIGN(spl_image->load_addr + spl_image->size, 8);
 
 	/* Figure out which device tree the board wants to use */
 	node = spl_fit_get_image_node(ctx, FIT_FDT_PROP, index++);
Signature · 1 line
2.48.1
#21 lore raw
In reply to Sam Edwards

Am 15. März 2025 23:17:58 MEZ schrieb Sam Edwards <[email protected]>:

Quoted 9 lines

Hello again U-Boot list,

This is v2 of my "misc. fixes" series, sent to prepare the codebase for more direct LLVM support in the near future. This series contains several fixes that I found in the process of preparing that support and which address issues independent of any future feature or enhancement. I am sending these now, both so that their inclusion is not delayed by discussion on my upcoming series and to make the latter more manageable.

Quoted 8 lines

PLEASE APPLY PARTIALLY! I have tried to sort these so that the most straightforward changes come first. If any patch proves contentious, you can either skip it or stop applying the series at that point. Any patches that need revisions will be included in a future series, but I'm trying to avoid there being a v3 of *this* series. :)

Happy Saturday,
Sam

Hello Sam,

I wonder why there are only ARM and X86 specific patches in this series . Wouldn't some of the changes also be needed for RISC-V and other architectures?

For EFI we have carved out a linker script include file to harmonize the linker scripts used by ARM and RISC-V. Shouldn't we do the same for main U-Boot?

Best regards

Heinrich

Quoted 39 lines

Changes v1->v2:
- Carried forward acked/reviewed tags
- Dropped two patches (one was incorrect, the other can be fixed differently)
- Minor phrasing changes to commit messages
- Instead of removing ENTRY(_start), I instead added .globl _start where appropriate, in the EFI apps

Sam Edwards (15):
arm: Remove stray .mmutable reference in linker script
arm: Exclude eabi_compat from LTO
arm: Add __aeabi_memclr in eabi_compat
arm: Add aligned-memory aliases to eabi_compat
arm: Discard unwanted sections in linker script
arm: Replace 'adrl' in EFI crt0
x86: Fix call64's section flags
makefile: Avoid objcopy --gap-fill for .hex/.srec
makefile: Add norelro linker option
makefile: Add READELF command variable
arm: riscv: efi: Export _start symbol from crt0_*_efi stubs
efi_loader: Move .dynamic out of .text in EFI scripts/Makefile.lib: efi: Preserve the .dynstr section as well
spl: riscv: opensbi: Error on misaligned FDT
spl: Align FDT load address

Makefile                        | 14 ++++++++------
arch/arm/cpu/u-boot.lds         | 29 ++++++++++-------------------
arch/arm/lib/Makefile           |  1 +
arch/arm/lib/crt0_aarch64_efi.S |  1 +
arch/arm/lib/crt0_arm_efi.S     |  4 +++-
arch/arm/lib/eabi_compat.c      | 17 +++++++++++++++++
arch/riscv/lib/crt0_riscv_efi.S |  1 +
arch/x86/cpu/i386/call64.S      |  2 +-
common/spl/spl_fit.c            |  2 +-
common/spl/spl_opensbi.c        |  5 +++++
lib/efi_loader/elf_efi.ldsi     |  6 +++---
scripts/Makefile.lib            |  4 ++--
12 files changed, 53 insertions(+), 33 deletions(-)
#22 lore raw
Quoted 17 lines · Heinrich Schuchardt

Am 15. März 2025 23:17:58 MEZ schrieb Sam Edwards <[email protected]>:

Quoted 15 lines

Hello again U-Boot list,

This is v2 of my "misc. fixes" series, sent to prepare the codebase for more direct LLVM support in the near future. This series contains several fixes that I found in the process of preparing that support and which address issues independent of any future feature or enhancement. I am sending these now, both so that their inclusion is not delayed by discussion on my upcoming series and to make the latter more manageable.

PLEASE APPLY PARTIALLY! I have tried to sort these so that the most straightforward changes come first. If any patch proves contentious, you can either skip it or stop applying the series at that point. Any patches that need revisions will be included in a future series, but I'm trying to avoid there being a v3 of *this* series. :)

Quoted 6 lines
Quoted 2 lines

Happy Saturday,
Sam

Hello Sam,

I wonder why there are only ARM and X86 specific patches in this series . Wouldn't some of the changes also be needed for RISC-V and other architectures?

Hi Heinrich,

The breakdown of arch-specific patches is: 6 for ARM, 1 for RISC-V, 1 for X86. This distribution was only because ARM needed more work, not because it received more attention. (Some architectures, like "sandbox," required no fixes, and MIPS appears to need only compiler-side improvements at this time, for example.)

Quoted 2 lines

For EFI we have carved out a linker script include file to harmonize the linker scripts used by ARM and RISC-V. Shouldn't we do the same for main U-Boot?

I like the idea in principle, but I'm skeptical because EFI applications have a much more uniform and constrained memory layout compared to raw binaries on [vendor-specific board]. So while it's possible to mostly unify the memory layout, doing so would take significantly more effort than with EFI. Plus, most of our supported architectures don't have EFI at all, so unifying EFI across a few architectures isn't a strong indicator that this approach would scale well. Also, Linux doesn't do this either, and I'd expect they would have if it were a good idea.

That said, I'd prefer to keep this patchset focused on fixing these immediate issues rather than reworking the linker scripts more broadly. If we want to explore that as a separate effort, I think it would need a deeper discussion.

Happy Sunday,
Sam

Quoted 45 lines

Best regards

Heinrich

Quoted 39 lines

Changes v1->v2:
- Carried forward acked/reviewed tags
- Dropped two patches (one was incorrect, the other can be fixed differently)
- Minor phrasing changes to commit messages
- Instead of removing ENTRY(_start), I instead added .globl _start where appropriate, in the EFI apps

Sam Edwards (15):
arm: Remove stray .mmutable reference in linker script
arm: Exclude eabi_compat from LTO
arm: Add __aeabi_memclr in eabi_compat
arm: Add aligned-memory aliases to eabi_compat
arm: Discard unwanted sections in linker script
arm: Replace 'adrl' in EFI crt0
x86: Fix call64's section flags
makefile: Avoid objcopy --gap-fill for .hex/.srec
makefile: Add norelro linker option
makefile: Add READELF command variable
arm: riscv: efi: Export _start symbol from crt0_*_efi stubs
efi_loader: Move .dynamic out of .text in EFI scripts/Makefile.lib: efi: Preserve the .dynstr section as well
spl: riscv: opensbi: Error on misaligned FDT
spl: Align FDT load address

Makefile                        | 14 ++++++++------
arch/arm/cpu/u-boot.lds         | 29 ++++++++++-------------------
arch/arm/lib/Makefile           |  1 +
arch/arm/lib/crt0_aarch64_efi.S |  1 +
arch/arm/lib/crt0_arm_efi.S     |  4 +++-
arch/arm/lib/eabi_compat.c      | 17 +++++++++++++++++
arch/riscv/lib/crt0_riscv_efi.S |  1 +
arch/x86/cpu/i386/call64.S      |  2 +-
common/spl/spl_fit.c            |  2 +-
common/spl/spl_opensbi.c        |  5 +++++
lib/efi_loader/elf_efi.ldsi     |  6 +++---
scripts/Makefile.lib            |  4 ++--
12 files changed, 53 insertions(+), 33 deletions(-)
Reviewed-by on v2 12/15
#17 lore raw

Re: [PATCH v2 12/15] efi_loader: Move .dynamic out of .text in EFI

In reply to Sam Edwards
Quoted 12 lines · Sam Edwards

EFI applications need to be relocatable. Ordinarily, this is achieved through a PE-format .reloc section, but since that requires toolchain tricks to achieve, U-Boot's EFI applications instead embed ELF-flavored relocation information and use it for self-relocation; thus, the .dynamic section needs to be preserved.

Before this patch, it was tacked on to the end of .text, but this was not proper: A .text section is SHT_PROGBITS, while the .dynamic section is SHT_DYNAMIC. Attempting to combine them like this creates a section type mismatch. While GNU ld doesn't seem to complain, LLVM's lld considers this a fatal linking error.

Quoted 6 lines

This patch moves .dynamic out to its own section, so that the output ELF has the correct types. (They're all mashed together when converting to binary anyway, so this patch causes no change in the final .efi output.)

Cc Heinrich Schuchardt <[email protected]>
Quoted 22 lines
---
  lib/efi_loader/elf_efi.ldsi | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
lib/efi_loader/elf_efi.ldsi +3 -3
index 190a88fb69e..4fa5ca43872 100644
@@ -21,10 +21,10 @@ SECTIONS
  		*(.gnu.linkonce.t.*)
  		*(.srodata)
  		*(.rodata*)
-		. = ALIGN(16);
-		*(.dynamic);
-		. = ALIGN(512);
  	}
+	. = ALIGN(16);
+	.dynamic : { *(.dynamic) }
+	. = ALIGN(512);
  	.rela.dyn : { *(.rela.dyn) }
  	.rela.plt : { *(.rela.plt) }
  	.rela.got : { *(.rela.got) }
Reviewed-by on v2 11/15
#15 lore raw

Re: [PATCH v2 11/15] arm: riscv: efi: Export _start symbol from crt0_*_efi stubs

In reply to Sam Edwards
Quoted 14 lines · Sam Edwards

While the _start label is only intended for use locally to populate the (hand-written) PE header, the linker script includes ENTRY(_start) which designates it as the entry point in the output ELF, resulting in linker warnings under some linkers (e.g. LLVM's lld) due to _start not being a globally-visible symbol. Since ELF is only an intermediary build format, and the aforementioned PE header correctly points to _start, the ENTRY(_start) directive could easily be removed to silence this warning.

However, since some developers who are debugging EFI by analyzing the intermediary ELF may appreciate having correct entry-point information, this patch instead promotes the _start labels to global symbols, silencing the linker warning and making the intermediary ELF reflect the true entry point.

Quoted 3 lines

This patch doesn't affect the final output binaries in any way.

Quoted 42 lines
---
  arch/arm/lib/crt0_aarch64_efi.S | 1 +
  arch/arm/lib/crt0_arm_efi.S     | 1 +
  arch/riscv/lib/crt0_riscv_efi.S | 1 +
  3 files changed, 3 insertions(+)
arch/arm/lib/crt0_aarch64_efi.S +1 -0
index e21b54fdbcb..003d5f83041 100644
@@ -144,6 +144,7 @@ section_table:
  		 IMAGE_SCN_CNT_INITIALIZED_DATA)
 
  	.align		12
+	.globl		_start
  _start:
  	stp		x29, x30, [sp, #-32]!
  	mov		x29, sp
arch/arm/lib/crt0_arm_efi.S +1 -0
index 235b3a0c48f..593ee1e194a 100644
@@ -143,6 +143,7 @@ section_table:
  		 IMAGE_SCN_CNT_INITIALIZED_DATA)
 
  	.align		12
+	.globl		_start
  _start:
  	stmfd		sp!, {r0-r2, lr}
 
arch/riscv/lib/crt0_riscv_efi.S +1 -0
index 9eacbe4a859..f170e4b26d6 100644
@@ -179,6 +179,7 @@ section_table:
  		 IMAGE_SCN_CNT_INITIALIZED_DATA)
 
  	.align		12
+	.globl		_start
  _start:
  	addi		sp, sp, -(SIZE_LONG * 3)
  	SAVE_LONG(a0, 0)
Reviewed-by on v2 6/15
#8 lore raw

Re: [PATCH v2 06/15] arm: Replace 'adrl' in EFI crt0

In reply to Sam Edwards
Quoted 17 lines · Sam Edwards

LLVM's IAS does not (and cannot easily) support the 'adrl' pseudoinstruction, and ARM developers generally do not consider it portable across assembler implementations either.

Instead, expand it into the two subtract instructions it would emit anyway. An explanation of the math follows:

The .+8 and .+4 refer to the same memory location; this is because the .+4 expression occurs in a subsequent instruction, 4 bytes after the first. This memory location is the value of the PC register when it is read by the first sub instruction. Thus, both inner parenthesized expressions evaluate to the same result: PC's offset relative to image_base. The subtract instructions then remove one byte each (low, then high) of the total offset, thereby getting the absolute address of image_base loaded in r0.

Quoted 1 line
Quoted 21 lines
---
 arch/arm/lib/crt0_arm_efi.S | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm/lib/crt0_arm_efi.S +2 -2
index 91b0fe12c51..235b3a0c48f 100644
@@ -149,7 +149,8 @@ _start:
        adr             r1, .L_DYNAMIC
        ldr             r0, [r1]
        add             r1, r0, r1
-       adrl            r0, image_base
+       sub             r0, pc, #((.+8-image_base) & 0xff)
+       sub             r0, r0, #((.+4-image_base) & 0xff00)
        bl              _relocate
        teq             r0, #0
        bne             0f
--

2.48.1

Acked-by on v2 8/15
#11 lore raw

Re: [PATCH v2 08/15] makefile: Avoid objcopy --gap-fill for .hex/.srec

In reply to Sam Edwards
Quoted 11 lines · Sam Edwards

This flag only makes sense for binary output, because .hex/.srec are sparse formats and represent gaps without filler. While the GNU binutils version of objcopy does not seem to mind the extra flag being passed, llvm-objcopy considers this a fatal error.

There is already a version of the objcopy command template in the Makefile that doesn't use --gap-fill, which is provided for EFI. So use this other version for all .hex/.srec outputs as well.

Quoted 53 lines
---
 Makefile | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
Makefile +5 -6
index 0694c425438..9c353af17cd 100644
@@ -1067,7 +1067,7 @@ quiet_cmd_objcopy = OBJCOPY $@
 cmd_objcopy = $(OBJCOPY) --gap-fill=0xff $(OBJCOPYFLAGS) \
        $(OBJCOPYFLAGS_$(@F)) $< $@
 
-# Provide a version which does not do this, for use by EFI
+# Provide a version which does not do this, for use by EFI and hex/srec
 quiet_cmd_zobjcopy = OBJCOPY $@
 cmd_zobjcopy = $(OBJCOPY) $(OBJCOPYFLAGS) $(OBJCOPYFLAGS_$(@F)) $< $@
 
@@ -1282,7 +1282,7 @@ OBJCOPYFLAGS_u-boot.hex := -O ihex
 OBJCOPYFLAGS_u-boot.srec := -O srec
 
 u-boot.hex u-boot.srec: u-boot FORCE
-       $(call if_changed,objcopy)
+       $(call if_changed,zobjcopy)
 
 OBJCOPYFLAGS_u-boot-elf.srec := $(OBJCOPYFLAGS_u-boot.srec)
 
@@ -1296,12 +1296,12 @@ OBJCOPYFLAGS_u-boot-elf.srec += --change-addresses=0x50000000
 endif
 
 u-boot-elf.srec: u-boot.elf FORCE
-       $(call if_changed,objcopy)
+       $(call if_changed,zobjcopy)
 
 OBJCOPYFLAGS_u-boot-spl.srec = $(OBJCOPYFLAGS_u-boot.srec)
 
 spl/u-boot-spl.srec: spl/u-boot-spl FORCE
-       $(call if_changed,objcopy)
+       $(call if_changed,zobjcopy)
 
 %.scif: %.srec
        $(Q)$(MAKE) $(build)=arch/arm/mach-renesas $@
@@ -1436,7 +1436,7 @@ OBJCOPYFLAGS_u-boot.ldr.hex := -I binary -O ihex
 OBJCOPYFLAGS_u-boot.ldr.srec := -I binary -O srec
 
 u-boot.ldr.hex u-boot.ldr.srec: u-boot.ldr FORCE
-       $(call if_changed,objcopy)
+       $(call if_changed,zobjcopy)
 
 ifdef CONFIG_SPL_LOAD_FIT
 MKIMAGEFLAGS_u-boot.img = -f auto -A $(ARCH) -T firmware -C none -O u-boot \
--

2.48.1

Reviewed-by on v2 5/15
#6 lore raw

Re: [PATCH v2 05/15] arm: Discard unwanted sections in linker script

In reply to Sam Edwards
Quoted 6 lines · Sam Edwards

There are a handful of sections that are not useful in the U-Boot output binary. At present, the linker script moves these to the end of the binary, after the _image_binary_end marker symbol, so that they don't get loaded.

Quoted 7 lines

The linker script syntax supports discarding sections that shouldn't be included in the output. Switch to this instead, to make the intention clearer and reduce the ELF sections that have to be handled later in the build. This is also consistent with the other architectures' linker scripts.

Quoted 36 lines
---
 arch/arm/cpu/u-boot.lds | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
arch/arm/cpu/u-boot.lds +10 -11
index 63e82a09fad..817e7a983ae 100644
@@ -181,14 +181,14 @@ SECTIONS
                __bss_end = .;
        }
 
-       .dynsym _image_binary_end : { *(.dynsym) }
-       .dynbss : { *(.dynbss) }
-       .dynstr : { *(.dynstr*) }
-       .dynamic : { *(.dynamic*) }
-       .plt : { *(.plt*) }
-       .interp : { *(.interp*) }
-       .gnu.hash : { *(.gnu.hash) }
-       .gnu : { *(.gnu*) }
-       .ARM.exidx : { *(.ARM.exidx*) }
-       .gnu.linkonce.armexidx : { *(.gnu.linkonce.armexidx.*) }
+       /DISCARD/ : { *(.dynsym) }
+       /DISCARD/ : { *(.dynbss) }
+       /DISCARD/ : { *(.dynstr*) }
+       /DISCARD/ : { *(.dynamic*) }
+       /DISCARD/ : { *(.plt*) }
+       /DISCARD/ : { *(.interp*) }
+       /DISCARD/ : { *(.gnu.hash) }
+       /DISCARD/ : { *(.gnu*) }
+       /DISCARD/ : { *(.ARM.exidx*) }
+       /DISCARD/ : { *(.gnu.linkonce.armexidx.*) }
 }
--

2.48.1

#23 lore raw
In reply to Sam Edwards
Quoted 8 lines · Sam Edwards

This is v2 of my "misc. fixes" series, sent to prepare the codebase for more direct LLVM support in the near future. This series contains several fixes that I found in the process of preparing that support and which address issues independent of any future feature or enhancement. I am sending these now, both so that their inclusion is not delayed by discussion on my upcoming series and to make the latter more manageable.

[...]

Applied to u-boot/next, thanks!

[01/15] arm: Remove stray .mmutable reference in linker script

        commit: 805377b1f5ae1834e2b1a07dbb6fed94672e0954
[02/15] arm: Exclude eabi_compat from LTO
        commit: 6ba839a0f5473aac8dc038f8601071234c810119
[03/15] arm: Add __aeabi_memclr in eabi_compat
        commit: 828484a7740d4ebfed8db85e4b4569cfbfe66cde
[04/15] arm: Add aligned-memory aliases to eabi_compat
        commit: deba40dd0ba452a3f15a359894e6b50494870d0e
[05/15] arm: Discard unwanted sections in linker script
        commit: 16448c443c8cacba7dacb3e919c0b414f70b8a7c
[06/15] arm: Replace 'adrl' in EFI crt0
        commit: d5734b183c3d578fff1c1e81e46a1d04342edffe
[07/15] x86: Fix call64's section flags
        commit: 8c39dc549b0de155c02a2b39f01dae19775f41a5
[08/15] makefile: Avoid objcopy --gap-fill for .hex/.srec
        commit: 86838a1ddc8a0e5b5f548a5051e5e68f90fb6660
[09/15] makefile: Add `norelro` linker option
        commit: 7a8121fe6d314b314531eee7487272601f469c1d
[10/15] makefile: Add READELF command variable
        commit: 586bb720e776396208df399874665ae8c6eb81e8
[11/15] arm: riscv: efi: Export _start symbol from crt0_*_efi stubs
        commit: f692540b24a7775e43f1d315d3e13a5d3ed21dd4
[12/15] efi_loader: Move .dynamic out of .text in EFI
        commit: 1755071db7d95fa0b95e9f9bedd3785e2abc10cf
[13/15] scripts/Makefile.lib: efi: Preserve the .dynstr section as well
        commit: 9ca475a6b5da06908a70d1eceb439d480137d69b
[14/15] spl: riscv: opensbi: Error on misaligned FDT
        commit: 17d830cb4b6cdbac56d41938d455820fd7a96a89
[15/15] spl: Align FDT load address
        commit: 358d1cc232c30091767ce192e74169e7861ae58a
Signature · 1 line
Tom