Cheng
Cheng

Reputation: 11

How to get gcc LTO work with library archives?

gcc experts,

I'm trying to use gcc lto with library archives, as the gcc comes with my system (RedHat Enterprise Linux 5.7) doesn't work with -flto (neither for my Ubuntu 14.10), so I build binutils && gcc from scratch.

Here is what I did:
1. Build binutils-2.22 with --enable-plugins
2. Build gcc-4.7.2 with --with-plugin-ld=/path/to/ld/built/in/step1 --enable-lto
3. Then for the following simple test:

// 1.c:  
int foo(void)  
{ return 0; }  

// 2.c:  
extern int foo(void)  
int main(void)  
{ return foo(); }

The following can get foo() inlined:

my_gcc -O3 -flto -c -o 1.o 1.c  
my_gcc -O3 -flto -c -o 2.o 2.c  
my_gcc -O3 -flto -o a.out 1.o 2.o

While the following can't:

my_gcc -O3 -flto -c -o 1.o 1.c  
my_gcc -O3 -flto -c -o 2.o 2.c  
my_ar cr --plugin <my_gcc>/libexec/gcc/x86_64-redhat-linux/4.7.2/liblto_plugin.so 1.a 1.o  
my_ar cr --plugin <my_gcc>/libexec/gcc/x86_64-redhat-linux/4.7.2/liblto_plugin.so 2.a 2.o  
gcc -O3 -flto -fuse-linker-plugin -o a.out 1.a 2.a

As the building system for the product I'm working on has to use archives, then what I can do to let lto work with library archive?

Your help will be much much appreciated.

Thanks a lot.

Upvotes: 0

Views: 2692

Answers (1)

bcmpinc
bcmpinc

Reputation: 3380

When linking, the order in which the libraries are listed on the command line, matters. So when compiling from the archives, you should swap 1.a and 2.a:

gcc -O3 -flto -fuse-linker-plugin -o a.out 2.a 1.a

I tested with gcc 4.9.2 and the disassembly, obtained with objdump -d a.out, shows that foo() is being inlined.

Upvotes: 2

Related Questions