Re: [PATCH 2/2] x86/boot/compressed: Remove unnecessary sections from bzImage

From: Nathan Chancellor
Date: Sat Feb 22 2020 - 02:42:58 EST


On Fri, Feb 21, 2020 at 11:21:44PM -0800, Fangrui Song wrote:
> On 2020-02-22, Nathan Chancellor wrote:
> > On Sat, Feb 22, 2020 at 07:55:21AM +0100, Borislav Petkov wrote:
> > > On Fri, Feb 21, 2020 at 10:08:45PM -0700, Nathan Chancellor wrote:
> > > > On Thu, Jan 09, 2020 at 10:02:18AM -0500, Arvind Sankar wrote:
> > > > > Discarding the sections that are unused in the compressed kernel saves
> > > > > about 10 KiB on 32-bit and 6 KiB on 64-bit, mostly from .eh_frame.
> > > > >
> > > > > Signed-off-by: Arvind Sankar <nivedita@xxxxxxxxxxxx>
> > > > > ---
> > > > > arch/x86/boot/compressed/vmlinux.lds.S | 5 +++++
> > > > > 1 file changed, 5 insertions(+)
> > > > >
> > > > > diff --git a/arch/x86/boot/compressed/vmlinux.lds.S b/arch/x86/boot/compressed/vmlinux.lds.S
> > > > > index 508cfa6828c5..12a20603d92e 100644
> > > > > --- a/arch/x86/boot/compressed/vmlinux.lds.S
> > > > > +++ b/arch/x86/boot/compressed/vmlinux.lds.S
> > > > > @@ -73,4 +73,9 @@ SECTIONS
> > > > > #endif
> > > > > . = ALIGN(PAGE_SIZE); /* keep ZO size page aligned */
> > > > > _end = .;
> > > > > +
> > > > > + /* Discard all remaining sections */
> > > > > + /DISCARD/ : {
> > > > > + *(*)
> > > > > + }
> > > > > }
> > > > > --
> > > > > 2.24.1
> > > > >
> > > >
> > > > This patch breaks linking with ld.lld:
> > > >
> > > > $ make -j$(nproc) -s CC=clang LD=ld.lld O=out.x86_64 distclean defconfig bzImage
> > > > ld.lld: error: discarding .shstrtab section is not allowed
> > >
> > > Well, why is it not allowed? And why isn't the GNU linker complaining?
> >
> > No idea, unfortunately I am not a linker expert and the patch that
> > changes this in lld does not really explain why it adds this
> > restriction:
> >
> > https://github.com/llvm/llvm-project/commit/1e799942b37d04f30b73f6a9e792d551dadafeea
> >
> > CC'ing Fangrui as I don't know George's email and he is usually
> > responsive to ld.lld issues/questions.
> >
> > Cheers,
> > Nathan
>
> In GNU ld, it seems that .shstrtab .symtab and .strtab are special
> cased. Neither the input section description *(.shstrtab) nor *(*)
> discards .shstrtab . I feel that this is a weird case (probably even a bug)
> that lld should not implement.
>
> In GNU ld, the following is not useful, while lld will discard the
> synthesized .strtab as requested:
>
> /DISCARD/ : { *(.strtab) }
>
> I think it is better making the intention (retaining .shstrtab)
> explicit, by adding a .shstrtab beside /DISCARD/ :
>
> SECTIONS {
> ...
> .shstrtab : { *(.shstrtab) }
> /DISCARD/ : { *(*) }
> }

Thanks for the clarity. With your suggestion (diff below), I see the
following error:

arch/x86/boot/compressed/vmlinux: no symbols
ld.lld: error: undefined symbol: ZO_input_data
>>> referenced by arch/x86/boot/header.o:(.header+0x59)

ld.lld: error: undefined symbol: ZO_z_input_len
>>> referenced by arch/x86/boot/header.o:(.header+0x5D)
make[3]: *** [../arch/x86/boot/Makefile:108: arch/x86/boot/setup.elf]

It seems like the section still isn't being added?

Cheers,
Nathan

diff --git a/arch/x86/boot/compressed/vmlinux.lds.S b/arch/x86/boot/compressed/vmlinux.lds.S
index 12a20603d92e..a6c3c7440c27 100644
--- a/arch/x86/boot/compressed/vmlinux.lds.S
+++ b/arch/x86/boot/compressed/vmlinux.lds.S
@@ -74,6 +74,10 @@ SECTIONS
. = ALIGN(PAGE_SIZE); /* keep ZO size page aligned */
_end = .;

+ .shstrtab : {
+ *(.shstrtab)
+ }
+
/* Discard all remaining sections */
/DISCARD/ : {
*(*)