From 3d6edc325c52082ab63ffd003c55a4ed875a52c5 Mon Sep 17 00:00:00 2001 From: Andrey Skvortsov Date: Tue, 5 Sep 2023 22:09:25 +0300 Subject: [PATCH 1/3] feat(build): check that .text section starts at page boundary Linker may decide to put new unspecified sections before .text section. That will cause non-working image, because entry point isn't at __BLXX_START__. Device just not booted with such image. This happened for example with .note.gnu.build-id section generated for LTO build in some cases. Now linker will report this situation as an error. ``` /usr/lib/gcc-cross/aarch64-linux-gnu/13/../../../../aarch64-linux-gnu/bin/ld: .text is not aligned on a page boundary. collect2: error: ld returned 1 exit status ``` Signed-off-by: Andrey Skvortsov Change-Id: I5ae46ddd1e6e431e1df1715d1d301f6dd7181cc7 --- bl1/bl1.ld.S | 6 ++++++ bl2/bl2.ld.S | 6 ++++++ bl2/bl2_el3.ld.S | 6 ++++++ bl2u/bl2u.ld.S | 6 ++++++ bl31/bl31.ld.S | 6 ++++++ bl32/sp_min/sp_min.ld.S | 6 ++++++ bl32/tsp/tsp.ld.S | 6 ++++++ 7 files changed, 42 insertions(+) diff --git a/bl1/bl1.ld.S b/bl1/bl1.ld.S index 49dda855e..d25ec63fb 100644 --- a/bl1/bl1.ld.S +++ b/bl1/bl1.ld.S @@ -36,6 +36,9 @@ SECTIONS { #if SEPARATE_CODE_AND_RODATA .text . : { + ASSERT(. == ALIGN(PAGE_SIZE), + ".text address is not aligned on a page boundary."); + __TEXT_START__ = .; *bl1_entrypoint.o(.text*) @@ -80,6 +83,9 @@ SECTIONS { } >ROM #else /* SEPARATE_CODE_AND_RODATA */ .ro . : { + ASSERT(. == ALIGN(PAGE_SIZE), + ".ro address is not aligned on a page boundary."); + __RO_START__ = .; *bl1_entrypoint.o(.text*) diff --git a/bl2/bl2.ld.S b/bl2/bl2.ld.S index db83a0c50..310e6fe78 100644 --- a/bl2/bl2.ld.S +++ b/bl2/bl2.ld.S @@ -25,6 +25,9 @@ SECTIONS { #if SEPARATE_CODE_AND_RODATA .text . : { + ASSERT(. == ALIGN(PAGE_SIZE), + ".text address is not aligned on a page boundary."); + __TEXT_START__ = .; #if ENABLE_RME @@ -65,6 +68,9 @@ SECTIONS { } >RAM #else /* SEPARATE_CODE_AND_RODATA */ .ro . : { + ASSERT(. == ALIGN(PAGE_SIZE), + ".ro address is not aligned on a page boundary."); + __RO_START__ = .; *bl2_entrypoint.o(.text*) diff --git a/bl2/bl2_el3.ld.S b/bl2/bl2_el3.ld.S index 4aa5cb045..811f41e14 100644 --- a/bl2/bl2_el3.ld.S +++ b/bl2/bl2_el3.ld.S @@ -55,6 +55,9 @@ SECTIONS { #if SEPARATE_CODE_AND_RODATA .text . : { + ASSERT(. == ALIGN(PAGE_SIZE), + ".text address is not aligned on a page boundary."); + __TEXT_START__ = .; __TEXT_RESIDENT_START__ = .; @@ -89,6 +92,9 @@ SECTIONS { "Resident part of BL2 has exceeded its limit.") #else /* SEPARATE_CODE_AND_RODATA */ .ro . : { + ASSERT(. == ALIGN(PAGE_SIZE), + ".ro address is not aligned on a page boundary."); + __RO_START__ = .; __TEXT_RESIDENT_START__ = .; diff --git a/bl2u/bl2u.ld.S b/bl2u/bl2u.ld.S index 7b1a10103..ee6a02065 100644 --- a/bl2u/bl2u.ld.S +++ b/bl2u/bl2u.ld.S @@ -27,6 +27,9 @@ SECTIONS { #if SEPARATE_CODE_AND_RODATA .text . : { + ASSERT(. == ALIGN(PAGE_SIZE), + ".text address is not aligned on a page boundary."); + __TEXT_START__ = .; *bl2u_entrypoint.o(.text*) @@ -60,6 +63,9 @@ SECTIONS { } >RAM #else /* SEPARATE_CODE_AND_RODATA */ .ro . : { + ASSERT(. == ALIGN(PAGE_SIZE), + ".ro address is not aligned on a page boundary."); + __RO_START__ = .; *bl2u_entrypoint.o(.text*) diff --git a/bl31/bl31.ld.S b/bl31/bl31.ld.S index 773b41d3e..8698dffd4 100644 --- a/bl31/bl31.ld.S +++ b/bl31/bl31.ld.S @@ -37,6 +37,9 @@ SECTIONS { #if SEPARATE_CODE_AND_RODATA .text . : { + ASSERT(. == ALIGN(PAGE_SIZE), + ".text is not aligned on a page boundary."); + __TEXT_START__ = .; *bl31_entrypoint.o(.text*) @@ -71,6 +74,9 @@ SECTIONS { } >RAM #else /* SEPARATE_CODE_AND_RODATA */ .ro . : { + ASSERT(. == ALIGN(PAGE_SIZE), + ".ro is not aligned on a page boundary."); + __RO_START__ = .; *bl31_entrypoint.o(.text*) diff --git a/bl32/sp_min/sp_min.ld.S b/bl32/sp_min/sp_min.ld.S index dd8197332..a2d9b7bf6 100644 --- a/bl32/sp_min/sp_min.ld.S +++ b/bl32/sp_min/sp_min.ld.S @@ -29,6 +29,9 @@ SECTIONS { #if SEPARATE_CODE_AND_RODATA .text . : { + ASSERT(. == ALIGN(PAGE_SIZE), + ".text address is not aligned on a page boundary."); + __TEXT_START__ = .; *entrypoint.o(.text*) @@ -67,6 +70,9 @@ SECTIONS { } >RAM #else /* SEPARATE_CODE_AND_RODATA */ .ro . : { + ASSERT(. == ALIGN(PAGE_SIZE), + ".ro address is not aligned on a page boundary."); + __RO_START__ = .; *entrypoint.o(.text*) diff --git a/bl32/tsp/tsp.ld.S b/bl32/tsp/tsp.ld.S index 22bf11dad..5116b20ac 100644 --- a/bl32/tsp/tsp.ld.S +++ b/bl32/tsp/tsp.ld.S @@ -25,6 +25,9 @@ SECTIONS { #if SEPARATE_CODE_AND_RODATA .text . : { + ASSERT(. == ALIGN(PAGE_SIZE), + ".text address is not aligned on a page boundary."); + __TEXT_START__ = .; *tsp_entrypoint.o(.text*) @@ -51,6 +54,9 @@ SECTIONS { } >RAM #else /* SEPARATE_CODE_AND_RODATA */ .ro . : { + ASSERT(. == ALIGN(PAGE_SIZE), + ".ro address is not aligned on a page boundary."); + __RO_START__ = .; *tsp_entrypoint.o(.text*) From 49ba1df52204e721f06a6da76ef0f8692ce1b2f8 Mon Sep 17 00:00:00 2001 From: Andrey Skvortsov Date: Tue, 5 Sep 2023 22:39:21 +0300 Subject: [PATCH 2/3] fix(build): add forgotten BL_LDFLAGS to lto command line as a result of missing BL_LDFLAGS '-Wl,--sort-section=alignment' was missing in link arguments for bl31. Signed-off-by: Andrey Skvortsov Change-Id: I78878e49da21fdc565abb3072e4abaf9face49f4 --- make_helpers/build_macros.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/make_helpers/build_macros.mk b/make_helpers/build_macros.mk index 71cf18b05..08a604625 100644 --- a/make_helpers/build_macros.mk +++ b/make_helpers/build_macros.mk @@ -573,7 +573,7 @@ ifneq ($(findstring armlink,$(notdir $(LD))),) $(LDPATHS) $(LIBWRAPPER) $(LDLIBS) $(BL_LIBS) \ $(BUILD_DIR)/build_message.o $(OBJS) else ifneq ($(findstring gcc,$(notdir $(LD))),) - $$(Q)$$(LD) -o $$@ $$(TF_LDFLAGS) $$(LDFLAGS) -Wl,-Map=$(MAPFILE) \ + $$(Q)$$(LD) -o $$@ $$(TF_LDFLAGS) $$(LDFLAGS) $(BL_LDFLAGS) -Wl,-Map=$(MAPFILE) \ $(addprefix -Wl$(comma)--script$(comma),$(LINKER_SCRIPTS)) -Wl,--script,$(DEFAULT_LINKER_SCRIPT) \ $(BUILD_DIR)/build_message.o \ $(OBJS) $(LDPATHS) $(LIBWRAPPER) $(LDLIBS) $(BL_LIBS) From 304ad94b34c2117823169a199558e7484139caa1 Mon Sep 17 00:00:00 2001 From: Andrey Skvortsov Date: Tue, 5 Sep 2023 23:06:29 +0300 Subject: [PATCH 3/3] fix(build): don't generate build-id 1. build-id takes space in memory 2. in some cases for lto build linker decided to put new .note.gnu.build-id section before .text section. The result was non-working image, because entry point wasn't at __BLXX_START__ anymore. Signed-off-by: Andrey Skvortsov Change-Id: Id78ccbc51e5ef82296069444ab438a1964a74e78 --- Makefile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Makefile b/Makefile index f55dfdd11..4713e87b3 100644 --- a/Makefile +++ b/Makefile @@ -361,6 +361,7 @@ else ifneq ($(findstring gcc,$(notdir $(LD))),) TF_LDFLAGS += -Wl,-z,common-page-size=4096 #Configure page size constants TF_LDFLAGS += -Wl,-z,max-page-size=4096 + TF_LDFLAGS += -Wl,--build-id=none ifeq ($(ENABLE_LTO),1) ifeq (${ARCH},aarch64) @@ -388,6 +389,7 @@ else TF_LDFLAGS += -z common-page-size=4096 # Configure page size constants TF_LDFLAGS += -z max-page-size=4096 + TF_LDFLAGS += --build-id=none # ld.lld doesn't recognize the errata flags, # therefore don't add those in that case.