1

I'm currently playing around with LTO for an embedded system (to see if it could reduce the size) and was having some issues getting everything to link properly using ld directly and was wondering what I was doing wrong. This is mostly playing with a toy program before I use this in a larger project.

The setup is basically I have 3 files:

  • test.c - data transform function
  • test_main.c - calls a function defined in start.S
  • start.S - calls a function in test.c and also contains _start

I compile the files using:

arm-none-eabi-as -mthumb -Wall start.S -o start.o
arm-none-eabi-gcc -Wall -Werror -march=armv7 -mtune=cortex-r7 -mthumb -fPIC -nostdlib -flto -static test.c -c test.o
arm-none-eabi-gcc -Wall -Werror -march=armv7 -mtune=cortex-r7 -mthumb -fPIC -nostdlib -flto -static test_main.c -c test_main.o

If I then try to link the program with ld I get:

arm-none-eabi-ld --static -fPIC --shared --fatal-warning test.o start.o test_main.o -test
arm-none-eabi-ld: test.o: plugin needed to handle lto object
arm-none-eabi-ld: test.o: plugin needed to handle lto object
arm-none-eabi-ld: test_main.o: plugin needed to handle lto object
arm-none-eabi-ld: test_main.o: plugin needed to handle lto object   

If I use gcc though, it works:

arm-none-eabi-gcc -Wall -Werror -march=armv7 -mtune=cortex-r7 -mthumb -fPIC -nostdlib -flto -static test.o start.o test_main.o -o test.gcc

I have tried specifying the linker plugin directly but then I get a different error:

arm-none-eabi-ld --static -fPIC --shared --fatal-warning --plugin <path_to_correct>/liblto_plugin.so test.o start.o test_main.o -test
arm-none-eabi-ld: <path_to_correct>.../lto-plugin.c:741: all_symbols_read_handler: Assertion 'lto_wrapper_argv' failed.
Aborted (core dumped)

What flags, parameters, etc. am I missing in my ld call?

Adam A.
  • 502
  • 1
  • 4
  • 12
  • 2
    Use `gcc -v` to see what options it passes for you. – Jester May 19 '21 at 16:50
  • 1
    Why do you need to call `ld` directly? If `gcc` works fine for you, just use it. – SergeyA May 19 '21 at 17:01
  • @SergeyA In the real project, I have linker scripts I use to control the layout – Adam A. May 19 '21 at 17:04
  • Can you use `gcc -Wl,--script=file`? Not sure if that works with LTO or not. – Peter Cordes May 19 '21 at 17:46
  • @PeterCordes That works! But! Is there a way to do this with ld directly? I'm fine if it literally too difficult to do this manually with LTO. when I use the -v flag as Jester mentioned, I see that collect2 is used to get a ridiculously large number of options. While using gcc and passing linker flags works, I would still like to know is there a way of calling ld with the correct parameters manually? – Adam A. May 19 '21 at 19:09
  • 1
    You can pass those ridiculously large number of options yourself. You can also start removing stuff and find out which ones are actually required. – Jester May 19 '21 at 19:11
  • `collect2` on almost all systems, probably including this, just passes all its args to `ld`, I think. So there you go, those are the options you want, if for some reason you really want to invoke `ld` yourself instead of just modifying the way `gcc` invokes `ld`. – Peter Cordes May 19 '21 at 19:23

0 Answers0