All of lore.kernel.org
 help / color / mirror / Atom feed
* [RFC PATCH 0/1] zboot: Garbage collect unused functions
@ 2023-06-21 11:44 Alexandre Ghiti
  2023-06-21 11:44 ` [RFC PATCH 1/1] libstub: zboot: Use -ffunction-sections + --gc-sections Alexandre Ghiti
  0 siblings, 1 reply; 5+ messages in thread
From: Alexandre Ghiti @ 2023-06-21 11:44 UTC (permalink / raw)
  To: Ard Biesheuvel, linux-kernel, linux-efi; +Cc: Alexandre Ghiti

This patch is meant to solve the issue reported by lkp in [1] with the KASLR
series support for RISC-V.

The problem in [1] is caused by the addition of new functions in
efi-stub-helper.c which reference symbols that are not defined in the context
of zboot. Those functions are not used in zboot but it causes those link
errors.

I can move those new functions around so that the problem disappears, but I
believe that fixing that using the linker garbage collection is more sane.

That's an RFC, I'll include it in my KASLR series if that's the right
direction.

[1] https://lore.kernel.org/oe-kbuild-all/202306080741.ArdxyO6n-lkp@intel.com/

Alexandre Ghiti (1):
  libstub: zboot: Use -ffunction-sections + --gc-sections

 drivers/firmware/efi/libstub/Makefile       | 1 +
 drivers/firmware/efi/libstub/Makefile.zboot | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)

-- 
2.39.2


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [RFC PATCH 1/1] libstub: zboot: Use -ffunction-sections + --gc-sections
  2023-06-21 11:44 [RFC PATCH 0/1] zboot: Garbage collect unused functions Alexandre Ghiti
@ 2023-06-21 11:44 ` Alexandre Ghiti
  2023-07-07  8:44   ` Alexandre Ghiti
  0 siblings, 1 reply; 5+ messages in thread
From: Alexandre Ghiti @ 2023-06-21 11:44 UTC (permalink / raw)
  To: Ard Biesheuvel, linux-kernel, linux-efi; +Cc: Alexandre Ghiti

Static linking is done at an object file (actually section) granularity,
meaning that if a function in an object has an undefined reference, the
link will fail, even though this function is not used in the resulting
ELF.

To avoid such failures, allow the linker to garbage collect unused sections
(which actually are functions).

Signed-off-by: Alexandre Ghiti <alexghiti@rivosinc.com>
---
 drivers/firmware/efi/libstub/Makefile       | 1 +
 drivers/firmware/efi/libstub/Makefile.zboot | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/firmware/efi/libstub/Makefile b/drivers/firmware/efi/libstub/Makefile
index 3abb2b357482..6182366d00c0 100644
--- a/drivers/firmware/efi/libstub/Makefile
+++ b/drivers/firmware/efi/libstub/Makefile
@@ -40,6 +40,7 @@ KBUILD_CFLAGS			:= $(subst $(CC_FLAGS_FTRACE),,$(cflags-y)) \
 				   -ffreestanding \
 				   -fno-stack-protector \
 				   $(call cc-option,-fno-addrsig) \
+				   -ffunction-sections \
 				   -D__DISABLE_EXPORTS
 
 #
diff --git a/drivers/firmware/efi/libstub/Makefile.zboot b/drivers/firmware/efi/libstub/Makefile.zboot
index 89ef820f3b34..5bb52d262133 100644
--- a/drivers/firmware/efi/libstub/Makefile.zboot
+++ b/drivers/firmware/efi/libstub/Makefile.zboot
@@ -51,7 +51,7 @@ $(obj)/zboot-header.o: $(srctree)/drivers/firmware/efi/libstub/zboot-header.S FO
 
 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)
 
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [RFC PATCH 1/1] libstub: zboot: Use -ffunction-sections + --gc-sections
  2023-06-21 11:44 ` [RFC PATCH 1/1] libstub: zboot: Use -ffunction-sections + --gc-sections Alexandre Ghiti
@ 2023-07-07  8:44   ` Alexandre Ghiti
  2023-07-07  8:47     ` Ard Biesheuvel
  0 siblings, 1 reply; 5+ messages in thread
From: Alexandre Ghiti @ 2023-07-07  8:44 UTC (permalink / raw)
  To: Ard Biesheuvel, linux-kernel, linux-efi

Hi Ard,

On Wed, Jun 21, 2023 at 1:45 PM Alexandre Ghiti <alexghiti@rivosinc.com> wrote:
>
> Static linking is done at an object file (actually section) granularity,
> meaning that if a function in an object has an undefined reference, the
> link will fail, even though this function is not used in the resulting
> ELF.
>
> To avoid such failures, allow the linker to garbage collect unused sections
> (which actually are functions).
>
> Signed-off-by: Alexandre Ghiti <alexghiti@rivosinc.com>
> ---
>  drivers/firmware/efi/libstub/Makefile       | 1 +
>  drivers/firmware/efi/libstub/Makefile.zboot | 2 +-
>  2 files changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/firmware/efi/libstub/Makefile b/drivers/firmware/efi/libstub/Makefile
> index 3abb2b357482..6182366d00c0 100644
> --- a/drivers/firmware/efi/libstub/Makefile
> +++ b/drivers/firmware/efi/libstub/Makefile
> @@ -40,6 +40,7 @@ KBUILD_CFLAGS                 := $(subst $(CC_FLAGS_FTRACE),,$(cflags-y)) \
>                                    -ffreestanding \
>                                    -fno-stack-protector \
>                                    $(call cc-option,-fno-addrsig) \
> +                                  -ffunction-sections \
>                                    -D__DISABLE_EXPORTS
>
>  #
> diff --git a/drivers/firmware/efi/libstub/Makefile.zboot b/drivers/firmware/efi/libstub/Makefile.zboot
> index 89ef820f3b34..5bb52d262133 100644
> --- a/drivers/firmware/efi/libstub/Makefile.zboot
> +++ b/drivers/firmware/efi/libstub/Makefile.zboot
> @@ -51,7 +51,7 @@ $(obj)/zboot-header.o: $(srctree)/drivers/firmware/efi/libstub/zboot-header.S FO
>
>  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)
>
> --
> 2.39.2
>

Sorry to bother you, do you have any opinion about this patch? Does
that make sense? If not, I should find another solution for my riscv
KASLR series!

Thanks,

Alex

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [RFC PATCH 1/1] libstub: zboot: Use -ffunction-sections + --gc-sections
  2023-07-07  8:44   ` Alexandre Ghiti
@ 2023-07-07  8:47     ` Ard Biesheuvel
  2023-07-07  9:06       ` Alexandre Ghiti
  0 siblings, 1 reply; 5+ messages in thread
From: Ard Biesheuvel @ 2023-07-07  8:47 UTC (permalink / raw)
  To: Alexandre Ghiti; +Cc: linux-kernel, linux-efi

On Fri, 7 Jul 2023 at 10:44, Alexandre Ghiti <alexghiti@rivosinc.com> wrote:
>
> Hi Ard,
>
> On Wed, Jun 21, 2023 at 1:45 PM Alexandre Ghiti <alexghiti@rivosinc.com> wrote:
> >
> > Static linking is done at an object file (actually section) granularity,
> > meaning that if a function in an object has an undefined reference, the
> > link will fail, even though this function is not used in the resulting
> > ELF.
> >
> > To avoid such failures, allow the linker to garbage collect unused sections
> > (which actually are functions).
> >
> > Signed-off-by: Alexandre Ghiti <alexghiti@rivosinc.com>
> > ---
> >  drivers/firmware/efi/libstub/Makefile       | 1 +
> >  drivers/firmware/efi/libstub/Makefile.zboot | 2 +-
> >  2 files changed, 2 insertions(+), 1 deletion(-)
> >
> > diff --git a/drivers/firmware/efi/libstub/Makefile b/drivers/firmware/efi/libstub/Makefile
> > index 3abb2b357482..6182366d00c0 100644
> > --- a/drivers/firmware/efi/libstub/Makefile
> > +++ b/drivers/firmware/efi/libstub/Makefile
> > @@ -40,6 +40,7 @@ KBUILD_CFLAGS                 := $(subst $(CC_FLAGS_FTRACE),,$(cflags-y)) \
> >                                    -ffreestanding \
> >                                    -fno-stack-protector \
> >                                    $(call cc-option,-fno-addrsig) \
> > +                                  -ffunction-sections \
> >                                    -D__DISABLE_EXPORTS
> >
> >  #
> > diff --git a/drivers/firmware/efi/libstub/Makefile.zboot b/drivers/firmware/efi/libstub/Makefile.zboot
> > index 89ef820f3b34..5bb52d262133 100644
> > --- a/drivers/firmware/efi/libstub/Makefile.zboot
> > +++ b/drivers/firmware/efi/libstub/Makefile.zboot
> > @@ -51,7 +51,7 @@ $(obj)/zboot-header.o: $(srctree)/drivers/firmware/efi/libstub/zboot-header.S FO
> >
> >  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)
> >
> > --
> > 2.39.2
> >
>
> Sorry to bother you, do you have any opinion about this patch? Does
> that make sense? If not, I should find another solution for my riscv
> KASLR series!
>

Hi,

Apologies for the delay in responding. I have been away and email
tends to pile up a bit.

The patch by itself looks fine to me, but I do wonder if your series
could be improved slightly by moving those newly shared pieces into a
separate source file, rather than to an existing one.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [RFC PATCH 1/1] libstub: zboot: Use -ffunction-sections + --gc-sections
  2023-07-07  8:47     ` Ard Biesheuvel
@ 2023-07-07  9:06       ` Alexandre Ghiti
  0 siblings, 0 replies; 5+ messages in thread
From: Alexandre Ghiti @ 2023-07-07  9:06 UTC (permalink / raw)
  To: Ard Biesheuvel; +Cc: linux-kernel, linux-efi

On Fri, Jul 7, 2023 at 10:47 AM Ard Biesheuvel <ardb@kernel.org> wrote:
>
> On Fri, 7 Jul 2023 at 10:44, Alexandre Ghiti <alexghiti@rivosinc.com> wrote:
> >
> > Hi Ard,
> >
> > On Wed, Jun 21, 2023 at 1:45 PM Alexandre Ghiti <alexghiti@rivosinc.com> wrote:
> > >
> > > Static linking is done at an object file (actually section) granularity,
> > > meaning that if a function in an object has an undefined reference, the
> > > link will fail, even though this function is not used in the resulting
> > > ELF.
> > >
> > > To avoid such failures, allow the linker to garbage collect unused sections
> > > (which actually are functions).
> > >
> > > Signed-off-by: Alexandre Ghiti <alexghiti@rivosinc.com>
> > > ---
> > >  drivers/firmware/efi/libstub/Makefile       | 1 +
> > >  drivers/firmware/efi/libstub/Makefile.zboot | 2 +-
> > >  2 files changed, 2 insertions(+), 1 deletion(-)
> > >
> > > diff --git a/drivers/firmware/efi/libstub/Makefile b/drivers/firmware/efi/libstub/Makefile
> > > index 3abb2b357482..6182366d00c0 100644
> > > --- a/drivers/firmware/efi/libstub/Makefile
> > > +++ b/drivers/firmware/efi/libstub/Makefile
> > > @@ -40,6 +40,7 @@ KBUILD_CFLAGS                 := $(subst $(CC_FLAGS_FTRACE),,$(cflags-y)) \
> > >                                    -ffreestanding \
> > >                                    -fno-stack-protector \
> > >                                    $(call cc-option,-fno-addrsig) \
> > > +                                  -ffunction-sections \
> > >                                    -D__DISABLE_EXPORTS
> > >
> > >  #
> > > diff --git a/drivers/firmware/efi/libstub/Makefile.zboot b/drivers/firmware/efi/libstub/Makefile.zboot
> > > index 89ef820f3b34..5bb52d262133 100644
> > > --- a/drivers/firmware/efi/libstub/Makefile.zboot
> > > +++ b/drivers/firmware/efi/libstub/Makefile.zboot
> > > @@ -51,7 +51,7 @@ $(obj)/zboot-header.o: $(srctree)/drivers/firmware/efi/libstub/zboot-header.S FO
> > >
> > >  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)
> > >
> > > --
> > > 2.39.2
> > >
> >
> > Sorry to bother you, do you have any opinion about this patch? Does
> > that make sense? If not, I should find another solution for my riscv
> > KASLR series!
> >
>
> Hi,
>
> Apologies for the delay in responding. I have been away and email
> tends to pile up a bit.

No worries!

>
> The patch by itself looks fine to me, but I do wonder if your series
> could be improved slightly by moving those newly shared pieces into a
> separate source file, rather than to an existing one.

If you prefer this solution, I'll do that then.

But I'm happy the patch makes sense to you, I'll probably use this to
fix some issues/limitations with the riscv pre-mmu code :)

Thanks for your quick answer!

Alex

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-07-07  9:07 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-21 11:44 [RFC PATCH 0/1] zboot: Garbage collect unused functions Alexandre Ghiti
2023-06-21 11:44 ` [RFC PATCH 1/1] libstub: zboot: Use -ffunction-sections + --gc-sections Alexandre Ghiti
2023-07-07  8:44   ` Alexandre Ghiti
2023-07-07  8:47     ` Ard Biesheuvel
2023-07-07  9:06       ` Alexandre Ghiti

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.