diff mbox series

[u-boot,16/39] build: use thin archives instead of incremental linking

Message ID 20210307042538.21229-17-marek.behun@nic.cz
State Superseded
Delegated to: Tom Rini
Headers show
Series U-Boot LTO (Sandbox + Some ARM boards) | expand

Commit Message

Marek Behún March 7, 2021, 4:25 a.m. UTC
Currently we use incremental linking (ld -r) to link several object
files from one directory into one built-in.o object file containing the
linked code from that directory (and its subdirectories).

Linux has, some time ago, moved to thin archives instead.

Thin archives are archives (.a) that do not really contain the object
files, only references to them.

Using thin archives instead of incremental linking
- saves disk space
- apparently works better with dead code elimination
- makes things easier for LTO

The third point is the important one for us. With incremental linking
there are several options how to do LTO, and that would unnecessarily
complicate things.

On the other hand, by using thin archives we can make (via the
--whole-archive use flag) the final linking behave as if we passed all
the object files from the archives to the linking program as arguments.

We also need to use the P flag for ar, otherwise final linking may fail.

Signed-off-by: Marek Behún <marek.behun@nic.cz>
---
 Makefile               |  4 ++--
 arch/sandbox/config.mk | 10 +++++++---
 scripts/Makefile.build | 16 ++++++++--------
 scripts/Makefile.spl   |  4 ++--
 4 files changed, 19 insertions(+), 15 deletions(-)

Comments

Bin Meng March 8, 2021, 9:16 a.m. UTC | #1
Hi Marek,

On Sun, Mar 7, 2021 at 12:26 PM Marek Behún <marek.behun@nic.cz> wrote:
>
> Currently we use incremental linking (ld -r) to link several object
> files from one directory into one built-in.o object file containing the
> linked code from that directory (and its subdirectories).
>
> Linux has, some time ago, moved to thin archives instead.
>
> Thin archives are archives (.a) that do not really contain the object
> files, only references to them.
>
> Using thin archives instead of incremental linking
> - saves disk space
> - apparently works better with dead code elimination
> - makes things easier for LTO
>
> The third point is the important one for us. With incremental linking
> there are several options how to do LTO, and that would unnecessarily
> complicate things.
>
> On the other hand, by using thin archives we can make (via the
> --whole-archive use flag) the final linking behave as if we passed all
> the object files from the archives to the linking program as arguments.

I don't think --whole-archive is required for LTO to work. Switching
to --whole-archive should be made conditionally when LTO is on,
otherwise for targets that don't have
-ffunction-sections/data-sections/--gc-sections specified, it will
create unnecessary bloat.

>
> We also need to use the P flag for ar, otherwise final linking may fail.
>
> Signed-off-by: Marek Behún <marek.behun@nic.cz>
> ---
>  Makefile               |  4 ++--
>  arch/sandbox/config.mk | 10 +++++++---
>  scripts/Makefile.build | 16 ++++++++--------
>  scripts/Makefile.spl   |  4 ++--
>  4 files changed, 19 insertions(+), 15 deletions(-)
>

Regards,
Bin
Marek Behún March 8, 2021, 10:11 a.m. UTC | #2
On Mon, 8 Mar 2021 17:16:03 +0800
Bin Meng <bmeng.cn@gmail.com> wrote:

> Hi Marek,
> 
> On Sun, Mar 7, 2021 at 12:26 PM Marek Behún <marek.behun@nic.cz> wrote:
> >
> > Currently we use incremental linking (ld -r) to link several object
> > files from one directory into one built-in.o object file containing the
> > linked code from that directory (and its subdirectories).
> >
> > Linux has, some time ago, moved to thin archives instead.
> >
> > Thin archives are archives (.a) that do not really contain the object
> > files, only references to them.
> >
> > Using thin archives instead of incremental linking
> > - saves disk space
> > - apparently works better with dead code elimination
> > - makes things easier for LTO
> >
> > The third point is the important one for us. With incremental linking
> > there are several options how to do LTO, and that would unnecessarily
> > complicate things.
> >
> > On the other hand, by using thin archives we can make (via the
> > --whole-archive use flag) the final linking behave as if we passed all
> > the object files from the archives to the linking program as arguments.  
> 
> I don't think --whole-archive is required for LTO to work.

It is. Linking fails if it is not used, for example for
nokia_rx51_defconfig.

> Switching to --whole-archive should be made conditionally when LTO is on,
> otherwise for targets that don't have
> -ffunction-sections/data-sections/--gc-sections specified, it will
> create unnecessary bloat.

OK I will push into CI without this flag for non-LTO and if it passes
all tests I shall remove this flag for non-LTO.

Marek
Bin Meng March 8, 2021, 10:44 a.m. UTC | #3
On Mon, Mar 8, 2021 at 6:11 PM Marek Behun <marek.behun@nic.cz> wrote:
>
> On Mon, 8 Mar 2021 17:16:03 +0800
> Bin Meng <bmeng.cn@gmail.com> wrote:
>
> > Hi Marek,
> >
> > On Sun, Mar 7, 2021 at 12:26 PM Marek Behún <marek.behun@nic.cz> wrote:
> > >
> > > Currently we use incremental linking (ld -r) to link several object
> > > files from one directory into one built-in.o object file containing the
> > > linked code from that directory (and its subdirectories).
> > >
> > > Linux has, some time ago, moved to thin archives instead.
> > >
> > > Thin archives are archives (.a) that do not really contain the object
> > > files, only references to them.
> > >
> > > Using thin archives instead of incremental linking
> > > - saves disk space
> > > - apparently works better with dead code elimination
> > > - makes things easier for LTO
> > >
> > > The third point is the important one for us. With incremental linking
> > > there are several options how to do LTO, and that would unnecessarily
> > > complicate things.
> > >
> > > On the other hand, by using thin archives we can make (via the
> > > --whole-archive use flag) the final linking behave as if we passed all
> > > the object files from the archives to the linking program as arguments.
> >
> > I don't think --whole-archive is required for LTO to work.
>
> It is. Linking fails if it is not used, for example for
> nokia_rx51_defconfig.

Could you investigate why? Is this due to missing marking some
variables/functions as __used?

>
> > Switching to --whole-archive should be made conditionally when LTO is on,
> > otherwise for targets that don't have
> > -ffunction-sections/data-sections/--gc-sections specified, it will
> > create unnecessary bloat.
>
> OK I will push into CI without this flag for non-LTO and if it passes
> all tests I shall remove this flag for non-LTO.
>

Regards,
Bin
Pali Rohár March 8, 2021, 11 a.m. UTC | #4
On Monday 08 March 2021 18:44:58 Bin Meng wrote:
> On Mon, Mar 8, 2021 at 6:11 PM Marek Behun <marek.behun@nic.cz> wrote:
> >
> > On Mon, 8 Mar 2021 17:16:03 +0800
> > Bin Meng <bmeng.cn@gmail.com> wrote:
> >
> > > Hi Marek,
> > >
> > > On Sun, Mar 7, 2021 at 12:26 PM Marek Behún <marek.behun@nic.cz> wrote:
> > > >
> > > > Currently we use incremental linking (ld -r) to link several object
> > > > files from one directory into one built-in.o object file containing the
> > > > linked code from that directory (and its subdirectories).
> > > >
> > > > Linux has, some time ago, moved to thin archives instead.
> > > >
> > > > Thin archives are archives (.a) that do not really contain the object
> > > > files, only references to them.
> > > >
> > > > Using thin archives instead of incremental linking
> > > > - saves disk space
> > > > - apparently works better with dead code elimination
> > > > - makes things easier for LTO
> > > >
> > > > The third point is the important one for us. With incremental linking
> > > > there are several options how to do LTO, and that would unnecessarily
> > > > complicate things.
> > > >
> > > > On the other hand, by using thin archives we can make (via the
> > > > --whole-archive use flag) the final linking behave as if we passed all
> > > > the object files from the archives to the linking program as arguments.
> > >
> > > I don't think --whole-archive is required for LTO to work.
> >
> > It is. Linking fails if it is not used, for example for
> > nokia_rx51_defconfig.
> 
> Could you investigate why? Is this due to missing marking some
> variables/functions as __used?

See email Message-ID: <20210306210045.pn3vzuarh23poxuv@pali> for details.

> >
> > > Switching to --whole-archive should be made conditionally when LTO is on,
> > > otherwise for targets that don't have
> > > -ffunction-sections/data-sections/--gc-sections specified, it will
> > > create unnecessary bloat.
> >
> > OK I will push into CI without this flag for non-LTO and if it passes
> > all tests I shall remove this flag for non-LTO.
> >
> 
> Regards,
> Bin
Marek Behún March 8, 2021, 11:18 a.m. UTC | #5
On Mon, 8 Mar 2021 18:44:58 +0800
Bin Meng <bmeng.cn@gmail.com> wrote:

> Could you investigate why?

I could, but I don't understand why exactly I should
- Linux is also using --whole-archive
- it is working
- I have other things I would like to work on

Maybe you could look into this? :)

Marek
Bin Meng March 8, 2021, 11:32 a.m. UTC | #6
On Mon, Mar 8, 2021 at 7:18 PM Marek Behun <marek.behun@nic.cz> wrote:
>
> On Mon, 8 Mar 2021 18:44:58 +0800
> Bin Meng <bmeng.cn@gmail.com> wrote:
>
> > Could you investigate why?
>
> I could, but I don't understand why exactly I should
> - Linux is also using --whole-archive
> - it is working
> - I have other things I would like to work on
>
> Maybe you could look into this? :)

Yes, I can look into this. I wonder if you already knew this which
could save some time as this is a normal review process, asking for
clarifications if something isn't clear.

Regards,
Bin
Bin Meng March 8, 2021, 11:41 a.m. UTC | #7
On Mon, Mar 8, 2021 at 7:00 PM Pali Rohár <pali@kernel.org> wrote:
>
> On Monday 08 March 2021 18:44:58 Bin Meng wrote:
> > On Mon, Mar 8, 2021 at 6:11 PM Marek Behun <marek.behun@nic.cz> wrote:
> > >
> > > On Mon, 8 Mar 2021 17:16:03 +0800
> > > Bin Meng <bmeng.cn@gmail.com> wrote:
> > >
> > > > Hi Marek,
> > > >
> > > > On Sun, Mar 7, 2021 at 12:26 PM Marek Behún <marek.behun@nic.cz> wrote:
> > > > >
> > > > > Currently we use incremental linking (ld -r) to link several object
> > > > > files from one directory into one built-in.o object file containing the
> > > > > linked code from that directory (and its subdirectories).
> > > > >
> > > > > Linux has, some time ago, moved to thin archives instead.
> > > > >
> > > > > Thin archives are archives (.a) that do not really contain the object
> > > > > files, only references to them.
> > > > >
> > > > > Using thin archives instead of incremental linking
> > > > > - saves disk space
> > > > > - apparently works better with dead code elimination
> > > > > - makes things easier for LTO
> > > > >
> > > > > The third point is the important one for us. With incremental linking
> > > > > there are several options how to do LTO, and that would unnecessarily
> > > > > complicate things.
> > > > >
> > > > > On the other hand, by using thin archives we can make (via the
> > > > > --whole-archive use flag) the final linking behave as if we passed all
> > > > > the object files from the archives to the linking program as arguments.
> > > >
> > > > I don't think --whole-archive is required for LTO to work.
> > >
> > > It is. Linking fails if it is not used, for example for
> > > nokia_rx51_defconfig.
> >
> > Could you investigate why? Is this due to missing marking some
> > variables/functions as __used?
>
> See email Message-ID: <20210306210045.pn3vzuarh23poxuv@pali> for details.
>

Looking at <20210306210045.pn3vzuarh23poxuv@pali>, the error you
reported seemed to be caused by not passing P to ar, as Marek replied
to you.

Regards,
Bin
Marek Behún March 8, 2021, 11:52 a.m. UTC | #8
On Mon, 8 Mar 2021 19:32:10 +0800
Bin Meng <bmeng.cn@gmail.com> wrote:

> On Mon, Mar 8, 2021 at 7:18 PM Marek Behun <marek.behun@nic.cz> wrote:
> >
> > On Mon, 8 Mar 2021 18:44:58 +0800
> > Bin Meng <bmeng.cn@gmail.com> wrote:
> >  
> > > Could you investigate why?  
> >
> > I could, but I don't understand why exactly I should
> > - Linux is also using --whole-archive
> > - it is working
> > - I have other things I would like to work on
> >
> > Maybe you could look into this? :)  
> 
> Yes, I can look into this. I wonder if you already knew this which
> could save some time as this is a normal review process, asking for
> clarifications if something isn't clear.

If I knew what? Whether it is used in Linux also?

To be honest I wasn't sure, I looked into Linux' sources before
replying.

The thing is, when I first worked on these patches, I just started with
some old LTO patches for Linux (which were not even merged then), and
played with the options until LTO worked for sandbox. Then the things
started to get complicated and I rebased the series many times, so I
did not remember which came from where.

Marek
Marek Behún March 8, 2021, 1:24 p.m. UTC | #9
On Mon, 8 Mar 2021 19:32:10 +0800
Bin Meng <bmeng.cn@gmail.com> wrote:

> On Mon, Mar 8, 2021 at 7:18 PM Marek Behun <marek.behun@nic.cz> wrote:
> >
> > On Mon, 8 Mar 2021 18:44:58 +0800
> > Bin Meng <bmeng.cn@gmail.com> wrote:
> >  
> > > Could you investigate why?  
> >
> > I could, but I don't understand why exactly I should
> > - Linux is also using --whole-archive
> > - it is working
> > - I have other things I would like to work on
> >
> > Maybe you could look into this? :)  
> 
> Yes, I can look into this. I wonder if you already knew this which
> could save some time as this is a normal review process, asking for
> clarifications if something isn't clear.

Bin, CI is failing without the --whole-archive option.

What is interesting is that the binaries build successfully, but
testing them fails :)

You can try this (with and without the --whole-archive options) (note
that this is without LTO)
  make qemu_arm_defconfig
  CROSS_COMPILE=arm-compiler- make -j8
  qemu-system-arm -M virt -nographic \
    -netdev user,id=net0,tftp=$(pwd) \
    -device e1000,netdev=net0 -device virtio-rng-pci \
    -bios u-boot.bin -serial mon:stdio

With --whole-archive option this boots successfully into U-Boot,
without --whole-archive it just hangs.

Marek
Bin Meng March 8, 2021, 2:30 p.m. UTC | #10
Hi Marek,

On Mon, Mar 8, 2021 at 9:24 PM Marek Behún <marek.behun@nic.cz> wrote:
>
> On Mon, 8 Mar 2021 19:32:10 +0800
> Bin Meng <bmeng.cn@gmail.com> wrote:
>
> > On Mon, Mar 8, 2021 at 7:18 PM Marek Behun <marek.behun@nic.cz> wrote:
> > >
> > > On Mon, 8 Mar 2021 18:44:58 +0800
> > > Bin Meng <bmeng.cn@gmail.com> wrote:
> > >
> > > > Could you investigate why?
> > >
> > > I could, but I don't understand why exactly I should
> > > - Linux is also using --whole-archive
> > > - it is working
> > > - I have other things I would like to work on
> > >
> > > Maybe you could look into this? :)
> >
> > Yes, I can look into this. I wonder if you already knew this which
> > could save some time as this is a normal review process, asking for
> > clarifications if something isn't clear.
>
> Bin, CI is failing without the --whole-archive option.
>
> What is interesting is that the binaries build successfully, but
> testing them fails :)
>
> You can try this (with and without the --whole-archive options) (note
> that this is without LTO)
>   make qemu_arm_defconfig
>   CROSS_COMPILE=arm-compiler- make -j8
>   qemu-system-arm -M virt -nographic \
>     -netdev user,id=net0,tftp=$(pwd) \
>     -device e1000,netdev=net0 -device virtio-rng-pci \
>     -bios u-boot.bin -serial mon:stdio
>
> With --whole-archive option this boots successfully into U-Boot,
> without --whole-archive it just hangs.

Thanks for reporting. My initnial build testing on qemu_arm_defconfig
with this series succeeded but there are some warnings:

/opt/armv7-linux/bin/arm-linux-ld.bfd:
lib/efi_selftest/efi_selftest_miniapp_exception.o: plugin needed to
handle lto object
/opt/armv7-linux/bin/arm-linux-ld.bfd:
examples/standalone/hello_world.o: plugin needed to handle lto object
/opt/armv7-linux/bin/arm-linux-ld.bfd: examples/standalone/libstubs.o:
plugin needed to handle lto object
/opt/armv7-linux/bin/arm-linux-ld.bfd: warning: cannot find entry
symbol hello_world; defaulting to 000000000c100000

It looks we should update the make rules to remove "-flto" for these targets.

Applying the following diff to remove --whole-archive, I got a build error:

diff --git a/Makefile b/Makefile
index 0f538270d7..127630e060 100644
--- a/Makefile
+++ b/Makefile
@@ -1780,7 +1780,7 @@ quiet_cmd_u-boot__ ?= LTO     $@
                $(LTO_FINAL_CFLAGS) $(c_flags)
         \
                $(KBUILD_LDFLAGS:%=-Wl,%) $(LDFLAGS_u-boot:%=-Wl,%) -o
$@       \
                -T u-boot.lds $(u-boot-init)
         \
-               -Wl,--start-group -Wl,--whole-archive
         \
+               -Wl,--start-group
         \
                        $(u-boot-main)
         \
                        $(PLATFORM_LIBS)
         \
                -Wl,--no-whole-archive -Wl,--end-group
         \
@@ -1790,9 +1790,9 @@ else
 quiet_cmd_u-boot__ ?= LD      $@
       cmd_u-boot__ ?= $(LD) $(KBUILD_LDFLAGS) $(LDFLAGS_u-boot) -o $@
         \
                -T u-boot.lds $(u-boot-init)
         \
-               --start-group --whole-archive
         \
+               --start-group
         \
                        $(u-boot-main)
         \
-               --no-whole-archive --end-group
         \
+               --end-group
         \
                $(PLATFORM_LIBS) -Map u-boot.map;
         \
                $(if $(ARCH_POSTLINK), $(MAKE) -f $(ARCH_POSTLINK) $@, true)
 endif

  LTO     u-boot
/opt/armv7-linux/bin/../lib/gcc/arm-buildroot-linux-gnueabihf/9.3.0/../../../../arm-buildroot-linux-gnueabihf/bin/ld:
arch/arm/lib/lib.a(lib1funcs.o): in function `Ldiv0':
arch/arm/lib/lib1funcs.S:360: undefined reference to `__div0'

I am using a pre-built armv7 toolchain from https://toolchains.bootlin.com/

Regards,
Bin
Marek Behún March 8, 2021, 3:22 p.m. UTC | #11
On Mon, 8 Mar 2021 22:30:17 +0800
Bin Meng <bmeng.cn@gmail.com> wrote:

> Hi Marek,
> 
> On Mon, Mar 8, 2021 at 9:24 PM Marek Behún <marek.behun@nic.cz> wrote:
> >
> > On Mon, 8 Mar 2021 19:32:10 +0800
> > Bin Meng <bmeng.cn@gmail.com> wrote:
> >  
> > > On Mon, Mar 8, 2021 at 7:18 PM Marek Behun <marek.behun@nic.cz>
> > > wrote:  
> > > >
> > > > On Mon, 8 Mar 2021 18:44:58 +0800
> > > > Bin Meng <bmeng.cn@gmail.com> wrote:
> > > >  
> > > > > Could you investigate why?  
> > > >
> > > > I could, but I don't understand why exactly I should
> > > > - Linux is also using --whole-archive
> > > > - it is working
> > > > - I have other things I would like to work on
> > > >
> > > > Maybe you could look into this? :)  
> > >
> > > Yes, I can look into this. I wonder if you already knew this which
> > > could save some time as this is a normal review process, asking
> > > for clarifications if something isn't clear.  
> >
> > Bin, CI is failing without the --whole-archive option.
> >
> > What is interesting is that the binaries build successfully, but
> > testing them fails :)
> >
> > You can try this (with and without the --whole-archive options)
> > (note that this is without LTO)
> >   make qemu_arm_defconfig
> >   CROSS_COMPILE=arm-compiler- make -j8
> >   qemu-system-arm -M virt -nographic \
> >     -netdev user,id=net0,tftp=$(pwd) \
> >     -device e1000,netdev=net0 -device virtio-rng-pci \
> >     -bios u-boot.bin -serial mon:stdio
> >
> > With --whole-archive option this boots successfully into U-Boot,
> > without --whole-archive it just hangs.  
> 
> Thanks for reporting. My initnial build testing on qemu_arm_defconfig
> with this series succeeded but there are some warnings:
> 
> /opt/armv7-linux/bin/arm-linux-ld.bfd:
> lib/efi_selftest/efi_selftest_miniapp_exception.o: plugin needed to
> handle lto object
> /opt/armv7-linux/bin/arm-linux-ld.bfd:
> examples/standalone/hello_world.o: plugin needed to handle lto object
> /opt/armv7-linux/bin/arm-linux-ld.bfd: examples/standalone/libstubs.o:
> plugin needed to handle lto object
> /opt/armv7-linux/bin/arm-linux-ld.bfd: warning: cannot find entry
> symbol hello_world; defaulting to 000000000c100000
> 
> It looks we should update the make rules to remove "-flto" for these
> targets.
> 
> Applying the following diff to remove --whole-archive, I got a build
> error:
> 
> diff --git a/Makefile b/Makefile
> index 0f538270d7..127630e060 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -1780,7 +1780,7 @@ quiet_cmd_u-boot__ ?= LTO     $@
>                 $(LTO_FINAL_CFLAGS) $(c_flags)
>          \
>                 $(KBUILD_LDFLAGS:%=-Wl,%) $(LDFLAGS_u-boot:%=-Wl,%) -o
> $@       \
>                 -T u-boot.lds $(u-boot-init)
>          \
> -               -Wl,--start-group -Wl,--whole-archive
>          \
> +               -Wl,--start-group
>          \
>                         $(u-boot-main)
>          \
>                         $(PLATFORM_LIBS)
>          \
>                 -Wl,--no-whole-archive -Wl,--end-group
>          \
> @@ -1790,9 +1790,9 @@ else
>  quiet_cmd_u-boot__ ?= LD      $@
>        cmd_u-boot__ ?= $(LD) $(KBUILD_LDFLAGS) $(LDFLAGS_u-boot) -o $@
>          \
>                 -T u-boot.lds $(u-boot-init)
>          \
> -               --start-group --whole-archive
>          \
> +               --start-group
>          \
>                         $(u-boot-main)
>          \
> -               --no-whole-archive --end-group
>          \
> +               --end-group
>          \
>                 $(PLATFORM_LIBS) -Map u-boot.map;
>          \
>                 $(if $(ARCH_POSTLINK), $(MAKE) -f $(ARCH_POSTLINK)
> $@, true) endif
> 
>   LTO     u-boot
> /opt/armv7-linux/bin/../lib/gcc/arm-buildroot-linux-gnueabihf/9.3.0/../../../../arm-buildroot-linux-gnueabihf/bin/ld:
> arch/arm/lib/lib.a(lib1funcs.o): in function `Ldiv0':
> arch/arm/lib/lib1funcs.S:360: undefined reference to `__div0'
> 
> I am using a pre-built armv7 toolchain from
> https://toolchains.bootlin.com/

The
  plugin needed to handle lto object
need another change in Makefiles, I will look into this.

As for the __div0 availability, that happens when compiling with LTO
and not using the --whole-archive flag.
Bin Meng March 9, 2021, 1:24 a.m. UTC | #12
Hi Marek,

On Mon, Mar 8, 2021 at 11:22 PM Marek Behún <marek.behun@nic.cz> wrote:
>
> On Mon, 8 Mar 2021 22:30:17 +0800
> Bin Meng <bmeng.cn@gmail.com> wrote:
>
> > Hi Marek,
> >
> > On Mon, Mar 8, 2021 at 9:24 PM Marek Behún <marek.behun@nic.cz> wrote:
> > >
> > > On Mon, 8 Mar 2021 19:32:10 +0800
> > > Bin Meng <bmeng.cn@gmail.com> wrote:
> > >
> > > > On Mon, Mar 8, 2021 at 7:18 PM Marek Behun <marek.behun@nic.cz>
> > > > wrote:
> > > > >
> > > > > On Mon, 8 Mar 2021 18:44:58 +0800
> > > > > Bin Meng <bmeng.cn@gmail.com> wrote:
> > > > >
> > > > > > Could you investigate why?
> > > > >
> > > > > I could, but I don't understand why exactly I should
> > > > > - Linux is also using --whole-archive
> > > > > - it is working
> > > > > - I have other things I would like to work on
> > > > >
> > > > > Maybe you could look into this? :)
> > > >
> > > > Yes, I can look into this. I wonder if you already knew this which
> > > > could save some time as this is a normal review process, asking
> > > > for clarifications if something isn't clear.
> > >
> > > Bin, CI is failing without the --whole-archive option.
> > >
> > > What is interesting is that the binaries build successfully, but
> > > testing them fails :)
> > >
> > > You can try this (with and without the --whole-archive options)
> > > (note that this is without LTO)
> > >   make qemu_arm_defconfig
> > >   CROSS_COMPILE=arm-compiler- make -j8
> > >   qemu-system-arm -M virt -nographic \
> > >     -netdev user,id=net0,tftp=$(pwd) \
> > >     -device e1000,netdev=net0 -device virtio-rng-pci \
> > >     -bios u-boot.bin -serial mon:stdio
> > >
> > > With --whole-archive option this boots successfully into U-Boot,
> > > without --whole-archive it just hangs.
> >
> > Thanks for reporting. My initnial build testing on qemu_arm_defconfig
> > with this series succeeded but there are some warnings:
> >
> > /opt/armv7-linux/bin/arm-linux-ld.bfd:
> > lib/efi_selftest/efi_selftest_miniapp_exception.o: plugin needed to
> > handle lto object
> > /opt/armv7-linux/bin/arm-linux-ld.bfd:
> > examples/standalone/hello_world.o: plugin needed to handle lto object
> > /opt/armv7-linux/bin/arm-linux-ld.bfd: examples/standalone/libstubs.o:
> > plugin needed to handle lto object
> > /opt/armv7-linux/bin/arm-linux-ld.bfd: warning: cannot find entry
> > symbol hello_world; defaulting to 000000000c100000
> >
> > It looks we should update the make rules to remove "-flto" for these
> > targets.
> >
> > Applying the following diff to remove --whole-archive, I got a build
> > error:
> >
> > diff --git a/Makefile b/Makefile
> > index 0f538270d7..127630e060 100644
> > --- a/Makefile
> > +++ b/Makefile
> > @@ -1780,7 +1780,7 @@ quiet_cmd_u-boot__ ?= LTO     $@
> >                 $(LTO_FINAL_CFLAGS) $(c_flags)
> >          \
> >                 $(KBUILD_LDFLAGS:%=-Wl,%) $(LDFLAGS_u-boot:%=-Wl,%) -o
> > $@       \
> >                 -T u-boot.lds $(u-boot-init)
> >          \
> > -               -Wl,--start-group -Wl,--whole-archive
> >          \
> > +               -Wl,--start-group
> >          \
> >                         $(u-boot-main)
> >          \
> >                         $(PLATFORM_LIBS)
> >          \
> >                 -Wl,--no-whole-archive -Wl,--end-group
> >          \
> > @@ -1790,9 +1790,9 @@ else
> >  quiet_cmd_u-boot__ ?= LD      $@
> >        cmd_u-boot__ ?= $(LD) $(KBUILD_LDFLAGS) $(LDFLAGS_u-boot) -o $@
> >          \
> >                 -T u-boot.lds $(u-boot-init)
> >          \
> > -               --start-group --whole-archive
> >          \
> > +               --start-group
> >          \
> >                         $(u-boot-main)
> >          \
> > -               --no-whole-archive --end-group
> >          \
> > +               --end-group
> >          \
> >                 $(PLATFORM_LIBS) -Map u-boot.map;
> >          \
> >                 $(if $(ARCH_POSTLINK), $(MAKE) -f $(ARCH_POSTLINK)
> > $@, true) endif
> >
> >   LTO     u-boot
> > /opt/armv7-linux/bin/../lib/gcc/arm-buildroot-linux-gnueabihf/9.3.0/../../../../arm-buildroot-linux-gnueabihf/bin/ld:
> > arch/arm/lib/lib.a(lib1funcs.o): in function `Ldiv0':
> > arch/arm/lib/lib1funcs.S:360: undefined reference to `__div0'
> >
> > I am using a pre-built armv7 toolchain from
> > https://toolchains.bootlin.com/
>
> The
>   plugin needed to handle lto object
> need another change in Makefiles, I will look into this.
>
> As for the __div0 availability, that happens when compiling with LTO
> and not using the --whole-archive flag.

Yes, that's something we need to investigate. Using "--whole-archive"
just masks the issue.

Regards,
Bin
Bin Meng March 9, 2021, 3:42 a.m. UTC | #13
Hi Marek,

On Tue, Mar 9, 2021 at 9:24 AM Bin Meng <bmeng.cn@gmail.com> wrote:
>
> Hi Marek,
>
> On Mon, Mar 8, 2021 at 11:22 PM Marek Behún <marek.behun@nic.cz> wrote:
> >
> > On Mon, 8 Mar 2021 22:30:17 +0800
> > Bin Meng <bmeng.cn@gmail.com> wrote:
> >
> > > Hi Marek,
> > >
> > > On Mon, Mar 8, 2021 at 9:24 PM Marek Behún <marek.behun@nic.cz> wrote:
> > > >
> > > > On Mon, 8 Mar 2021 19:32:10 +0800
> > > > Bin Meng <bmeng.cn@gmail.com> wrote:
> > > >
> > > > > On Mon, Mar 8, 2021 at 7:18 PM Marek Behun <marek.behun@nic.cz>
> > > > > wrote:
> > > > > >
> > > > > > On Mon, 8 Mar 2021 18:44:58 +0800
> > > > > > Bin Meng <bmeng.cn@gmail.com> wrote:
> > > > > >
> > > > > > > Could you investigate why?
> > > > > >
> > > > > > I could, but I don't understand why exactly I should
> > > > > > - Linux is also using --whole-archive
> > > > > > - it is working
> > > > > > - I have other things I would like to work on
> > > > > >
> > > > > > Maybe you could look into this? :)
> > > > >
> > > > > Yes, I can look into this. I wonder if you already knew this which
> > > > > could save some time as this is a normal review process, asking
> > > > > for clarifications if something isn't clear.
> > > >
> > > > Bin, CI is failing without the --whole-archive option.
> > > >
> > > > What is interesting is that the binaries build successfully, but
> > > > testing them fails :)
> > > >
> > > > You can try this (with and without the --whole-archive options)
> > > > (note that this is without LTO)
> > > >   make qemu_arm_defconfig
> > > >   CROSS_COMPILE=arm-compiler- make -j8
> > > >   qemu-system-arm -M virt -nographic \
> > > >     -netdev user,id=net0,tftp=$(pwd) \
> > > >     -device e1000,netdev=net0 -device virtio-rng-pci \
> > > >     -bios u-boot.bin -serial mon:stdio
> > > >
> > > > With --whole-archive option this boots successfully into U-Boot,
> > > > without --whole-archive it just hangs.
> > >
> > > Thanks for reporting. My initnial build testing on qemu_arm_defconfig
> > > with this series succeeded but there are some warnings:
> > >
> > > /opt/armv7-linux/bin/arm-linux-ld.bfd:
> > > lib/efi_selftest/efi_selftest_miniapp_exception.o: plugin needed to
> > > handle lto object
> > > /opt/armv7-linux/bin/arm-linux-ld.bfd:
> > > examples/standalone/hello_world.o: plugin needed to handle lto object
> > > /opt/armv7-linux/bin/arm-linux-ld.bfd: examples/standalone/libstubs.o:
> > > plugin needed to handle lto object
> > > /opt/armv7-linux/bin/arm-linux-ld.bfd: warning: cannot find entry
> > > symbol hello_world; defaulting to 000000000c100000
> > >
> > > It looks we should update the make rules to remove "-flto" for these
> > > targets.
> > >
> > > Applying the following diff to remove --whole-archive, I got a build
> > > error:
> > >
> > > diff --git a/Makefile b/Makefile
> > > index 0f538270d7..127630e060 100644
> > > --- a/Makefile
> > > +++ b/Makefile
> > > @@ -1780,7 +1780,7 @@ quiet_cmd_u-boot__ ?= LTO     $@
> > >                 $(LTO_FINAL_CFLAGS) $(c_flags)
> > >          \
> > >                 $(KBUILD_LDFLAGS:%=-Wl,%) $(LDFLAGS_u-boot:%=-Wl,%) -o
> > > $@       \
> > >                 -T u-boot.lds $(u-boot-init)
> > >          \
> > > -               -Wl,--start-group -Wl,--whole-archive
> > >          \
> > > +               -Wl,--start-group
> > >          \
> > >                         $(u-boot-main)
> > >          \
> > >                         $(PLATFORM_LIBS)
> > >          \
> > >                 -Wl,--no-whole-archive -Wl,--end-group
> > >          \
> > > @@ -1790,9 +1790,9 @@ else
> > >  quiet_cmd_u-boot__ ?= LD      $@
> > >        cmd_u-boot__ ?= $(LD) $(KBUILD_LDFLAGS) $(LDFLAGS_u-boot) -o $@
> > >          \
> > >                 -T u-boot.lds $(u-boot-init)
> > >          \
> > > -               --start-group --whole-archive
> > >          \
> > > +               --start-group
> > >          \
> > >                         $(u-boot-main)
> > >          \
> > > -               --no-whole-archive --end-group
> > >          \
> > > +               --end-group
> > >          \
> > >                 $(PLATFORM_LIBS) -Map u-boot.map;
> > >          \
> > >                 $(if $(ARCH_POSTLINK), $(MAKE) -f $(ARCH_POSTLINK)
> > > $@, true) endif
> > >
> > >   LTO     u-boot
> > > /opt/armv7-linux/bin/../lib/gcc/arm-buildroot-linux-gnueabihf/9.3.0/../../../../arm-buildroot-linux-gnueabihf/bin/ld:
> > > arch/arm/lib/lib.a(lib1funcs.o): in function `Ldiv0':
> > > arch/arm/lib/lib1funcs.S:360: undefined reference to `__div0'
> > >
> > > I am using a pre-built armv7 toolchain from
> > > https://toolchains.bootlin.com/
> >
> > The
> >   plugin needed to handle lto object
> > need another change in Makefiles, I will look into this.
> >
> > As for the __div0 availability, that happens when compiling with LTO
> > and not using the --whole-archive flag.
>
> Yes, that's something we need to investigate. Using "--whole-archive"
> just masks the issue.

Please ignore this. I think "--whole-archive" is correct after
experimenting with the compiler.

Regards,
Bin
Marek Behún March 9, 2021, 10:36 a.m. UTC | #14
On Tue, 9 Mar 2021 11:42:08 +0800
Bin Meng <bmeng.cn@gmail.com> wrote:

> Hi Marek,
> 
> On Tue, Mar 9, 2021 at 9:24 AM Bin Meng <bmeng.cn@gmail.com> wrote:
> >
> > Hi Marek,
> >
> > On Mon, Mar 8, 2021 at 11:22 PM Marek Behún <marek.behun@nic.cz> wrote:  
> > >
> > > On Mon, 8 Mar 2021 22:30:17 +0800
> > > Bin Meng <bmeng.cn@gmail.com> wrote:
> > >  
> > > > Hi Marek,
> > > >
> > > > On Mon, Mar 8, 2021 at 9:24 PM Marek Behún <marek.behun@nic.cz> wrote:  
> > > > >
> > > > > On Mon, 8 Mar 2021 19:32:10 +0800
> > > > > Bin Meng <bmeng.cn@gmail.com> wrote:
> > > > >  
> > > > > > On Mon, Mar 8, 2021 at 7:18 PM Marek Behun <marek.behun@nic.cz>
> > > > > > wrote:  
> > > > > > >
> > > > > > > On Mon, 8 Mar 2021 18:44:58 +0800
> > > > > > > Bin Meng <bmeng.cn@gmail.com> wrote:
> > > > > > >  
> > > > > > > > Could you investigate why?  
> > > > > > >
> > > > > > > I could, but I don't understand why exactly I should
> > > > > > > - Linux is also using --whole-archive
> > > > > > > - it is working
> > > > > > > - I have other things I would like to work on
> > > > > > >
> > > > > > > Maybe you could look into this? :)  
> > > > > >
> > > > > > Yes, I can look into this. I wonder if you already knew this which
> > > > > > could save some time as this is a normal review process, asking
> > > > > > for clarifications if something isn't clear.  
> > > > >
> > > > > Bin, CI is failing without the --whole-archive option.
> > > > >
> > > > > What is interesting is that the binaries build successfully, but
> > > > > testing them fails :)
> > > > >
> > > > > You can try this (with and without the --whole-archive options)
> > > > > (note that this is without LTO)
> > > > >   make qemu_arm_defconfig
> > > > >   CROSS_COMPILE=arm-compiler- make -j8
> > > > >   qemu-system-arm -M virt -nographic \
> > > > >     -netdev user,id=net0,tftp=$(pwd) \
> > > > >     -device e1000,netdev=net0 -device virtio-rng-pci \
> > > > >     -bios u-boot.bin -serial mon:stdio
> > > > >
> > > > > With --whole-archive option this boots successfully into U-Boot,
> > > > > without --whole-archive it just hangs.  
> > > >
> > > > Thanks for reporting. My initnial build testing on qemu_arm_defconfig
> > > > with this series succeeded but there are some warnings:
> > > >
> > > > /opt/armv7-linux/bin/arm-linux-ld.bfd:
> > > > lib/efi_selftest/efi_selftest_miniapp_exception.o: plugin needed to
> > > > handle lto object
> > > > /opt/armv7-linux/bin/arm-linux-ld.bfd:
> > > > examples/standalone/hello_world.o: plugin needed to handle lto object
> > > > /opt/armv7-linux/bin/arm-linux-ld.bfd: examples/standalone/libstubs.o:
> > > > plugin needed to handle lto object
> > > > /opt/armv7-linux/bin/arm-linux-ld.bfd: warning: cannot find entry
> > > > symbol hello_world; defaulting to 000000000c100000
> > > >
> > > > It looks we should update the make rules to remove "-flto" for these
> > > > targets.
> > > >
> > > > Applying the following diff to remove --whole-archive, I got a build
> > > > error:
> > > >
> > > > diff --git a/Makefile b/Makefile
> > > > index 0f538270d7..127630e060 100644
> > > > --- a/Makefile
> > > > +++ b/Makefile
> > > > @@ -1780,7 +1780,7 @@ quiet_cmd_u-boot__ ?= LTO     $@
> > > >                 $(LTO_FINAL_CFLAGS) $(c_flags)
> > > >          \
> > > >                 $(KBUILD_LDFLAGS:%=-Wl,%) $(LDFLAGS_u-boot:%=-Wl,%) -o
> > > > $@       \
> > > >                 -T u-boot.lds $(u-boot-init)
> > > >          \
> > > > -               -Wl,--start-group -Wl,--whole-archive
> > > >          \
> > > > +               -Wl,--start-group
> > > >          \
> > > >                         $(u-boot-main)
> > > >          \
> > > >                         $(PLATFORM_LIBS)
> > > >          \
> > > >                 -Wl,--no-whole-archive -Wl,--end-group
> > > >          \
> > > > @@ -1790,9 +1790,9 @@ else
> > > >  quiet_cmd_u-boot__ ?= LD      $@
> > > >        cmd_u-boot__ ?= $(LD) $(KBUILD_LDFLAGS) $(LDFLAGS_u-boot) -o $@
> > > >          \
> > > >                 -T u-boot.lds $(u-boot-init)
> > > >          \
> > > > -               --start-group --whole-archive
> > > >          \
> > > > +               --start-group
> > > >          \
> > > >                         $(u-boot-main)
> > > >          \
> > > > -               --no-whole-archive --end-group
> > > >          \
> > > > +               --end-group
> > > >          \
> > > >                 $(PLATFORM_LIBS) -Map u-boot.map;
> > > >          \
> > > >                 $(if $(ARCH_POSTLINK), $(MAKE) -f $(ARCH_POSTLINK)
> > > > $@, true) endif
> > > >
> > > >   LTO     u-boot
> > > > /opt/armv7-linux/bin/../lib/gcc/arm-buildroot-linux-gnueabihf/9.3.0/../../../../arm-buildroot-linux-gnueabihf/bin/ld:
> > > > arch/arm/lib/lib.a(lib1funcs.o): in function `Ldiv0':
> > > > arch/arm/lib/lib1funcs.S:360: undefined reference to `__div0'
> > > >
> > > > I am using a pre-built armv7 toolchain from
> > > > https://toolchains.bootlin.com/  
> > >
> > > The
> > >   plugin needed to handle lto object
> > > need another change in Makefiles, I will look into this.
> > >
> > > As for the __div0 availability, that happens when compiling with LTO
> > > and not using the --whole-archive flag.  
> >
> > Yes, that's something we need to investigate. Using "--whole-archive"
> > just masks the issue.  
> 
> Please ignore this. I think "--whole-archive" is correct after
> experimenting with the compiler.

So do you agree with this patch? Can I consider having your
Reviewed-by tag? :)
Bin Meng March 9, 2021, 1 p.m. UTC | #15
On Sun, Mar 7, 2021 at 12:26 PM Marek Behún <marek.behun@nic.cz> wrote:
>
> Currently we use incremental linking (ld -r) to link several object
> files from one directory into one built-in.o object file containing the
> linked code from that directory (and its subdirectories).
>
> Linux has, some time ago, moved to thin archives instead.
>
> Thin archives are archives (.a) that do not really contain the object
> files, only references to them.
>
> Using thin archives instead of incremental linking
> - saves disk space
> - apparently works better with dead code elimination
> - makes things easier for LTO
>
> The third point is the important one for us. With incremental linking
> there are several options how to do LTO, and that would unnecessarily
> complicate things.
>
> On the other hand, by using thin archives we can make (via the
> --whole-archive use flag) the final linking behave as if we passed all
> the object files from the archives to the linking program as arguments.
>
> We also need to use the P flag for ar, otherwise final linking may fail.
>
> Signed-off-by: Marek Behún <marek.behun@nic.cz>
> ---
>  Makefile               |  4 ++--
>  arch/sandbox/config.mk | 10 +++++++---
>  scripts/Makefile.build | 16 ++++++++--------
>  scripts/Makefile.spl   |  4 ++--
>  4 files changed, 19 insertions(+), 15 deletions(-)
>
> diff --git a/Makefile b/Makefile
> index 7b0ba9df9a..6dc47810ea 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -1754,9 +1754,9 @@ ARCH_POSTLINK := $(wildcard $(srctree)/arch/$(ARCH)/Makefile.postlink)
>  quiet_cmd_u-boot__ ?= LD      $@
>        cmd_u-boot__ ?= $(LD) $(KBUILD_LDFLAGS) $(LDFLAGS_u-boot) -o $@          \
>                 -T u-boot.lds $(u-boot-init)                                    \
> -               --start-group                                                   \
> +               --start-group --whole-archive                                   \

--start-group is useless now.

>                         $(u-boot-main)                                          \
> -               --end-group                                                     \
> +               --no-whole-archive --end-group                                  \

and --end-group

>                 $(PLATFORM_LIBS) -Map u-boot.map;                               \
>                 $(if $(ARCH_POSTLINK), $(MAKE) -f $(ARCH_POSTLINK) $@, true)
>
> diff --git a/arch/sandbox/config.mk b/arch/sandbox/config.mk
> index 189e9c2b0c..d11b9c63c9 100644
> --- a/arch/sandbox/config.mk
> +++ b/arch/sandbox/config.mk
> @@ -17,13 +17,17 @@ PLATFORM_CPPFLAGS += $(shell $(SDL_CONFIG) --cflags)
>  endif
>
>  cmd_u-boot__ = $(CC) -o $@ -Wl,-T u-boot.lds $(u-boot-init) \
> -       -Wl,--start-group $(u-boot-main) -Wl,--end-group \
> +       -Wl,--start-group -Wl,--whole-archive \
> +               $(u-boot-main) \
> +       -Wl,--no-whole-archive -Wl,--end-group \
>         $(PLATFORM_LIBS) -Wl,-Map -Wl,u-boot.map
>
>  cmd_u-boot-spl = (cd $(obj) && $(CC) -o $(SPL_BIN) -Wl,-T u-boot-spl.lds \
>         $(patsubst $(obj)/%,%,$(u-boot-spl-init)) \
> -       -Wl,--start-group $(patsubst $(obj)/%,%,$(u-boot-spl-main)) \
> -       $(patsubst $(obj)/%,%,$(u-boot-spl-platdata)) -Wl,--end-group \
> +       -Wl,--start-group -Wl,--whole-archive \
> +               $(patsubst $(obj)/%,%,$(u-boot-spl-main)) \
> +               $(patsubst $(obj)/%,%,$(u-boot-spl-platdata)) \
> +       -Wl,--no-whole-archive -Wl,--end-group \
>         $(PLATFORM_LIBS) -Wl,-Map -Wl,u-boot-spl.map -Wl,--gc-sections)
>
>  CONFIG_ARCH_DEVICE_TREE := sandbox
> diff --git a/scripts/Makefile.build b/scripts/Makefile.build
> index 705a886cb9..a9a02d7d33 100644
> --- a/scripts/Makefile.build
> +++ b/scripts/Makefile.build
> @@ -331,12 +331,11 @@ $(sort $(subdir-obj-y)): $(subdir-ym) ;
>  # Rule to compile a set of .o files into one .o file
>  #
>  ifdef builtin-target
> -quiet_cmd_link_o_target = LD      $@
> +quiet_cmd_link_o_target = AR      $@
>  # If the list of objects to link is empty, just create an empty built-in.o
>  cmd_link_o_target = $(if $(strip $(obj-y)),\
> -                     $(LD) $(ld_flags) -r -o $@ $(filter $(obj-y), $^) \
> -                     $(cmd_secanalysis),\
> -                     rm -f $@; $(AR) rcs$(KBUILD_ARFLAGS) $@)
> +                     rm -f $@; $(AR) cDPrsT $@ $(filter $(obj-y), $^), \
> +                     rm -f $@; $(AR) cPrsT$(KBUILD_ARFLAGS) $@)

nits: should we use D for the empty one for consistency?

>
>  $(builtin-target): $(obj-y) FORCE
>         $(call if_changed,link_o_target)
> @@ -362,7 +361,7 @@ $(modorder-target): $(subdir-ym) FORCE
>  #
>  ifdef lib-target
>  quiet_cmd_link_l_target = AR      $@
> -cmd_link_l_target = rm -f $@; $(AR) rcs$(KBUILD_ARFLAGS) $@ $(lib-y)
> +cmd_link_l_target = rm -f $@; $(AR) cPrs$(KBUILD_ARFLAGS) $@ $(lib-y)

It looks this line change is not needed

>
>  $(lib-target): $(lib-y) FORCE
>         $(call if_changed,link_l_target)
> @@ -382,10 +381,11 @@ $(filter $(addprefix $(obj)/,         \
>  $($(subst $(obj)/,,$(@:.o=-objs)))    \
>  $($(subst $(obj)/,,$(@:.o=-y)))), $^)
>
> -quiet_cmd_link_multi-y = LD      $@
> -cmd_link_multi-y = $(LD) $(ld_flags) -r -o $@ $(link_multi_deps) $(cmd_secanalysis)
>
> -quiet_cmd_link_multi-m = LD [M]  $@
> +quiet_cmd_link_multi-y = AR      $@
> +cmd_link_multi-y = rm -f $@; $(AR) cDPrsT $@ $(link_multi_deps)
> +
> +quiet_cmd_link_multi-m = AR [M]  $@
>  cmd_link_multi-m = $(cmd_link_multi-y)
>
>  $(multi-used-y): FORCE
> diff --git a/scripts/Makefile.spl b/scripts/Makefile.spl
> index 3b5f5046c9..889debb93c 100644
> --- a/scripts/Makefile.spl
> +++ b/scripts/Makefile.spl
> @@ -423,10 +423,10 @@ quiet_cmd_u-boot-spl ?= LD      $@
>        cmd_u-boot-spl ?= (cd $(obj) && \
>                 $(LD) $(KBUILD_LDFLAGS) $(LDFLAGS_$(@F))                \
>                 $(patsubst $(obj)/%,%,$(u-boot-spl-init))               \
> -               --start-group                                           \
> +               --start-group --whole-archive                           \
>                         $(patsubst $(obj)/%,%,$(u-boot-spl-main))       \
>                         $(patsubst $(obj)/%,%,$(u-boot-spl-platdata))   \
> -               --end-group                                             \
> +               --no-whole-archive --end-group                          \
>                 $(PLATFORM_LIBS) -Map $(SPL_BIN).map -o $(SPL_BIN)      \
>                         )
>

Otherwise LGTM:
Reviewed-by: Bin Meng <bmeng.cn@gmail.com>

Regards,
Bin
Marek Behún March 11, 2021, 12:42 p.m. UTC | #16
On Tue, 9 Mar 2021 21:00:00 +0800
Bin Meng <bmeng.cn@gmail.com> wrote:

> 
> --start-group is useless now.
> 
> >                         $(u-boot-main)                                          \
> > -               --end-group                                                     \
> > +               --no-whole-archive --end-group                                  \  
> 
> and --end-group
> 

I will test this

> > -                     rm -f $@; $(AR) rcs$(KBUILD_ARFLAGS) $@)
> > +                     rm -f $@; $(AR) cDPrsT $@ $(filter $(obj-y), $^), \
> > +                     rm -f $@; $(AR) cPrsT$(KBUILD_ARFLAGS) $@)  
> 
> nits: should we use D for the empty one for consistency?

OK

> >
> >  $(builtin-target): $(obj-y) FORCE
> >         $(call if_changed,link_o_target)
> > @@ -362,7 +361,7 @@ $(modorder-target): $(subdir-ym) FORCE
> >  #
> >  ifdef lib-target
> >  quiet_cmd_link_l_target = AR      $@
> > -cmd_link_l_target = rm -f $@; $(AR) rcs$(KBUILD_ARFLAGS) $@ $(lib-y)
> > +cmd_link_l_target = rm -f $@; $(AR) cPrs$(KBUILD_ARFLAGS) $@ $(lib-y)  
> 
> It looks this line change is not needed

Hmm. I will look into this, maybe I added it just for consistency.

> Otherwise LGTM:
> Reviewed-by: Bin Meng <bmeng.cn@gmail.com>

THX
diff mbox series

Patch

diff --git a/Makefile b/Makefile
index 7b0ba9df9a..6dc47810ea 100644
--- a/Makefile
+++ b/Makefile
@@ -1754,9 +1754,9 @@  ARCH_POSTLINK := $(wildcard $(srctree)/arch/$(ARCH)/Makefile.postlink)
 quiet_cmd_u-boot__ ?= LD      $@
       cmd_u-boot__ ?= $(LD) $(KBUILD_LDFLAGS) $(LDFLAGS_u-boot) -o $@		\
 		-T u-boot.lds $(u-boot-init)					\
-		--start-group							\
+		--start-group --whole-archive					\
 			$(u-boot-main)						\
-		--end-group							\
+		--no-whole-archive --end-group					\
 		$(PLATFORM_LIBS) -Map u-boot.map;				\
 		$(if $(ARCH_POSTLINK), $(MAKE) -f $(ARCH_POSTLINK) $@, true)
 
diff --git a/arch/sandbox/config.mk b/arch/sandbox/config.mk
index 189e9c2b0c..d11b9c63c9 100644
--- a/arch/sandbox/config.mk
+++ b/arch/sandbox/config.mk
@@ -17,13 +17,17 @@  PLATFORM_CPPFLAGS += $(shell $(SDL_CONFIG) --cflags)
 endif
 
 cmd_u-boot__ = $(CC) -o $@ -Wl,-T u-boot.lds $(u-boot-init) \
-	-Wl,--start-group $(u-boot-main) -Wl,--end-group \
+	-Wl,--start-group -Wl,--whole-archive \
+		$(u-boot-main) \
+	-Wl,--no-whole-archive -Wl,--end-group \
 	$(PLATFORM_LIBS) -Wl,-Map -Wl,u-boot.map
 
 cmd_u-boot-spl = (cd $(obj) && $(CC) -o $(SPL_BIN) -Wl,-T u-boot-spl.lds \
 	$(patsubst $(obj)/%,%,$(u-boot-spl-init)) \
-	-Wl,--start-group $(patsubst $(obj)/%,%,$(u-boot-spl-main)) \
-	$(patsubst $(obj)/%,%,$(u-boot-spl-platdata)) -Wl,--end-group \
+	-Wl,--start-group -Wl,--whole-archive \
+		$(patsubst $(obj)/%,%,$(u-boot-spl-main)) \
+		$(patsubst $(obj)/%,%,$(u-boot-spl-platdata)) \
+	-Wl,--no-whole-archive -Wl,--end-group \
 	$(PLATFORM_LIBS) -Wl,-Map -Wl,u-boot-spl.map -Wl,--gc-sections)
 
 CONFIG_ARCH_DEVICE_TREE := sandbox
diff --git a/scripts/Makefile.build b/scripts/Makefile.build
index 705a886cb9..a9a02d7d33 100644
--- a/scripts/Makefile.build
+++ b/scripts/Makefile.build
@@ -331,12 +331,11 @@  $(sort $(subdir-obj-y)): $(subdir-ym) ;
 # Rule to compile a set of .o files into one .o file
 #
 ifdef builtin-target
-quiet_cmd_link_o_target = LD      $@
+quiet_cmd_link_o_target = AR      $@
 # If the list of objects to link is empty, just create an empty built-in.o
 cmd_link_o_target = $(if $(strip $(obj-y)),\
-		      $(LD) $(ld_flags) -r -o $@ $(filter $(obj-y), $^) \
-		      $(cmd_secanalysis),\
-		      rm -f $@; $(AR) rcs$(KBUILD_ARFLAGS) $@)
+		      rm -f $@; $(AR) cDPrsT $@ $(filter $(obj-y), $^), \
+		      rm -f $@; $(AR) cPrsT$(KBUILD_ARFLAGS) $@)
 
 $(builtin-target): $(obj-y) FORCE
 	$(call if_changed,link_o_target)
@@ -362,7 +361,7 @@  $(modorder-target): $(subdir-ym) FORCE
 #
 ifdef lib-target
 quiet_cmd_link_l_target = AR      $@
-cmd_link_l_target = rm -f $@; $(AR) rcs$(KBUILD_ARFLAGS) $@ $(lib-y)
+cmd_link_l_target = rm -f $@; $(AR) cPrs$(KBUILD_ARFLAGS) $@ $(lib-y)
 
 $(lib-target): $(lib-y) FORCE
 	$(call if_changed,link_l_target)
@@ -382,10 +381,11 @@  $(filter $(addprefix $(obj)/,         \
 $($(subst $(obj)/,,$(@:.o=-objs)))    \
 $($(subst $(obj)/,,$(@:.o=-y)))), $^)
 
-quiet_cmd_link_multi-y = LD      $@
-cmd_link_multi-y = $(LD) $(ld_flags) -r -o $@ $(link_multi_deps) $(cmd_secanalysis)
 
-quiet_cmd_link_multi-m = LD [M]  $@
+quiet_cmd_link_multi-y = AR      $@
+cmd_link_multi-y = rm -f $@; $(AR) cDPrsT $@ $(link_multi_deps)
+
+quiet_cmd_link_multi-m = AR [M]  $@
 cmd_link_multi-m = $(cmd_link_multi-y)
 
 $(multi-used-y): FORCE
diff --git a/scripts/Makefile.spl b/scripts/Makefile.spl
index 3b5f5046c9..889debb93c 100644
--- a/scripts/Makefile.spl
+++ b/scripts/Makefile.spl
@@ -423,10 +423,10 @@  quiet_cmd_u-boot-spl ?= LD      $@
       cmd_u-boot-spl ?= (cd $(obj) && \
 		$(LD) $(KBUILD_LDFLAGS) $(LDFLAGS_$(@F))		\
 		$(patsubst $(obj)/%,%,$(u-boot-spl-init))		\
-		--start-group						\
+		--start-group --whole-archive				\
 			$(patsubst $(obj)/%,%,$(u-boot-spl-main))	\
 			$(patsubst $(obj)/%,%,$(u-boot-spl-platdata))	\
-		--end-group						\
+		--no-whole-archive --end-group				\
 		$(PLATFORM_LIBS) -Map $(SPL_BIN).map -o $(SPL_BIN)	\
 			)