Skip to contentSkip to conversation

efi/libstub: Fix library dependencies

PATCH In review Ard Biesheuvel 3 participants 6 messages Nov 26

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

cover efi/libstub: Fix library dependencies

From: Ard Biesheuvel <[email protected]>

Dmitry reports that 32-bit LoongArch builds with EFI_ZBOOT disabled fail to link, due to missing helpers for 64-bit integer shifts.

Fix this, and while at it, tweak the build so some of these dependencies can be dropped for the zboot build.

Cc Dmitry Antipov <[email protected]>
Cc Huacai Chen <[email protected]>
Cc WANG Xuerui <[email protected]>

Ard Biesheuvel (3):
efi/libstub: loongarch: Provide 64-bit integer shift helpers for

    32-bit

efi/zboot: Eliminate unused code from the zboot executable efi/libstub: Provide library helpers only where needed

 drivers/firmware/efi/libstub/Makefile       | 18 +++++++++++-------
 drivers/firmware/efi/libstub/Makefile.zboot |  3 ++-
 drivers/firmware/efi/libstub/zboot.lds      |  4 ++--
 3 files changed, 15 insertions(+), 10 deletions(-)
Signature · 1 line
2.56.1.839.g22a8290db9-goog
#1 lore raw

PATCH 1/3 efi/libstub: loongarch: Provide 64-bit integer shift helpers for 32-bit

From: Ard Biesheuvel <[email protected]>

The EFI stub's copy of lib/cmdline.c performs 64-bit shifts in memparse() [which is never actually called from the stub], and so it needs library routines when running on 32-bit CPUs that lack native instructions for this.

The LoongArch 32-bit EFI stub build already pulls in the library helpers that are needed for this, but only when CONFIG_EFI_ZBOOT=y, which is not sufficient, given that the same helpers will be needed in the vmlinux build for the builtin copy of the EFI stub.

On other architectures, the builtin copy will call vmlinux's versions of those routines, which are exposed to the stub via aliases in the linker script. However, on LoongArch, those are C implementations that may be instrumented and therefore unsuitable for use in the EFI stub.

---
 drivers/firmware/efi/libstub/Makefile | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/firmware/efi/libstub/Makefile +4 -3
index bbab12b8eb4b..ba6c9e763862 100644
@@ -88,7 +88,9 @@ lib-$(CONFIG_ARM64)		+= kaslr.o arm64.o arm64-stub.o smbios.o
 lib-$(CONFIG_X86)		+= x86-stub.o smbios.o
 lib-$(CONFIG_X86_64)		+= x86-5lvl.o
 lib-$(CONFIG_RISCV)		+= kaslr.o riscv.o riscv-stub.o
-lib-$(CONFIG_LOONGARCH)		+= loongarch.o loongarch-stub.o
+lib-loongarch-$(CONFIG_32BIT)	+= lib-ashldi3.o lib-lshrdi3.o
+lib-$(CONFIG_LOONGARCH)		+= loongarch.o loongarch-stub.o \
+				   $(lib-loongarch-y)
 
 CFLAGS_arm32-stub.o		:= -DTEXT_OFFSET=$(TEXT_OFFSET)
 
@@ -99,8 +101,7 @@ CFLAGS_zboot-decompress-zstd.o	+= -I$(srctree)/lib/zstd
 
 zboot-riscv-obj-$(CONFIG_32BIT)	:= lib-ashldi3.o lib-lshrdi3.o
 zboot-obj-$(CONFIG_RISCV)	+= lib-clz_ctz.o $(zboot-riscv-obj-y)
-zboot-loongarch-obj-$(CONFIG_32BIT) := lib-ashldi3.o lib-lshrdi3.o
-zboot-obj-$(CONFIG_LOONGARCH)	+= lib-clz_ctz.o $(zboot-loongarch-obj-y)
+zboot-obj-$(CONFIG_LOONGARCH)	+= lib-clz_ctz.o
 lib-$(CONFIG_EFI_ZBOOT)		+= zboot.o $(zboot-obj-y)
 
 lib-$(CONFIG_UNACCEPTED_MEMORY) += unaccepted_memory.o bitmap.o find.o
Signature · 1 line
2.56.1.839.g22a8290db9-goog
#2 lore raw

PATCH 2/3 efi/zboot: Eliminate unused code from the zboot executable

In reply to Ard Biesheuvel

From: Ard Biesheuvel <[email protected]>

Wire up -ffunction-sections and --gc-sections so that unused functions are dropped from the build. This saves some space, but more importantly, will allow us to get rid of library routines that are never called (64-bit shift on 32-bit, clz/ctz on RISC-V)

---
 drivers/firmware/efi/libstub/Makefile       | 2 ++
 drivers/firmware/efi/libstub/Makefile.zboot | 3 ++-
 drivers/firmware/efi/libstub/zboot.lds      | 4 ++--
 3 files changed, 6 insertions(+), 3 deletions(-)
drivers/firmware/efi/libstub/Makefile +2 -0
index ba6c9e763862..20b5b77956d1 100644
@@ -34,6 +34,8 @@ cflags-$(CONFIG_LOONGARCH)	+= -fpie
 
 cflags-$(CONFIG_EFI_PARAMS_FROM_FDT)	+= -I$(srctree)/scripts/dtc/libfdt
 
+cflags-$(CONFIG_EFI_ZBOOT)	+= -ffunction-sections
+
 KBUILD_CFLAGS			:= $(subst $(CC_FLAGS_FTRACE),,$(cflags-y)) \
 				   -Os -DDISABLE_BRANCH_PROFILING \
 				   -include $(srctree)/include/linux/hidden.h \
drivers/firmware/efi/libstub/Makefile.zboot +2 -1
index 52baacf3e9d9..3b827e4b571d 100644
@@ -50,7 +50,8 @@ endif
 
 ZBOOT_DEPS := $(obj)/zboot-header.o $(objtree)/drivers/firmware/efi/libstub/lib.a
 
-LDFLAGS_vmlinuz.efi.elf := -T $(srctree)/drivers/firmware/efi/libstub/zboot.lds
+LDFLAGS_vmlinuz.efi.elf := -T $(srctree)/drivers/firmware/efi/libstub/zboot.lds \
+			   --gc-sections
 $(obj)/vmlinuz.efi.elf: $(obj)/vmlinuz.o $(ZBOOT_DEPS) FORCE
 	$(call if_changed,ld)
 
drivers/firmware/efi/libstub/zboot.lds +2 -2
index a785c0123749..f3d4342c8a08 100644
@@ -16,7 +16,7 @@ SECTIONS
 
 	.rodata : ALIGN(8) {
 		__efistub__gzdata_start = .;
-		*(.gzdata)
+		KEEP(*(.gzdata))
 		__efistub_payload_size = . - 4;
 		__efistub__gzdata_end = .;
 		*(.rodata* .init.rodata* .srodata*)
@@ -31,7 +31,7 @@ SECTIONS
 
         .sbat : ALIGN(4096) {
 		_sbat = .;
-		*(.sbat)
+		KEEP(*(.sbat))
 		_esbat = ALIGN(4096);
 		. = _esbat;
 	}
Signature · 1 line
2.56.1.839.g22a8290db9-goog
#3 lore raw

PATCH 3/3 efi/libstub: Provide library helpers only where needed

In reply to Ard Biesheuvel

From: Ard Biesheuvel <[email protected]>

Now that dead stub code is being eliminated by the linker, the only remaining user of the 64-bit left shift and clz/ctz helper routines is ZSTD compression, and only in some cases:

- the 64-bit left shift is only needed on 32-bit architectures
- the clz/ctz helper is needed on all RISC-V and 32-bit reduced LoongArch

---
 drivers/firmware/efi/libstub/Makefile | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
drivers/firmware/efi/libstub/Makefile +6 -5
index 20b5b77956d1..ba6d67e5cd78 100644
@@ -98,14 +98,15 @@ CFLAGS_arm32-stub.o		:= -DTEXT_OFFSET=$(TEXT_OFFSET)
 
 zboot-obj-y			:= zboot-decompress-gzip.o
 CFLAGS_zboot-decompress-gzip.o	+= -I$(srctree)/lib/zlib_inflate
-zboot-obj-$(CONFIG_KERNEL_ZSTD)	:= zboot-decompress-zstd.o lib-xxhash.o
+
+zstd-obj-$(CONFIG_32BIT)	:= lib-ashldi3.o
+zstd-obj-$(CONFIG_RISCV)	+= lib-clz_ctz.o
+zstd-obj-$(CONFIG_32BIT_REDUCED) += lib-clz_ctz.o # loongarch
+zboot-obj-$(CONFIG_KERNEL_ZSTD)	:= zboot-decompress-zstd.o lib-xxhash.o \
+				   $(zstd-obj-y)
 CFLAGS_zboot-decompress-zstd.o	+= -I$(srctree)/lib/zstd
 
-zboot-riscv-obj-$(CONFIG_32BIT)	:= lib-ashldi3.o lib-lshrdi3.o
-zboot-obj-$(CONFIG_RISCV)	+= lib-clz_ctz.o $(zboot-riscv-obj-y)
-zboot-obj-$(CONFIG_LOONGARCH)	+= lib-clz_ctz.o
 lib-$(CONFIG_EFI_ZBOOT)		+= zboot.o $(zboot-obj-y)
-
 lib-$(CONFIG_UNACCEPTED_MEMORY) += unaccepted_memory.o bitmap.o find.o
 
 targets				:= $(lib-y)
Signature · 1 line
2.56.1.839.g22a8290db9-goog
#4 lore raw

Re: [PATCH 3/3] efi/libstub: Provide library helpers only where needed

Quoted 1 line · Ard Biesheuvel

- the clz/ctz helper is needed on all RISC-V

IIUC Zbb-capable cores has native clz/ctz, isn't it?

Dmitry

maintainer
#5 lore raw

Re: [PATCH 3/3] efi/libstub: Provide library helpers only where needed

Quoted 6 lines · Dmitry Antipov
Quoted 1 line · Ard Biesheuvel

- the clz/ctz helper is needed on all RISC-V

IIUC Zbb-capable cores has native clz/ctz, isn't it?

Yes but the UEFI spec does not mandate that extension.

The kernel proper uses alternatives patching for Zbb, and so the native clz/ctz will be used at runtime, but we cannot rely on it at boot.

In any case, this just builds the object in the libstub static library, and so if the compiler never emits the library calls, the code will not be linked in.