llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.24k stars 12.07k forks source link

LTO ignores -fPIC/-fPIE when building a non PIE executable #38024

Open glandium opened 6 years ago

glandium commented 6 years ago
Bugzilla Link 38676
Version unspecified
OS Linux
CC @froydnj

Extended Description

$ cat hello.c

include

int main() { printf("Hello, world\n"); return 0; }

$ clang-7 -o hello hello.c -O3 -fPIC -flto=thin # same result with -flto $ objdump -d hello (...) 0000000000401130

: 401130: 50 push %rax 401131: bf 04 20 40 00 mov $0x402004,%edi 401136: e8 f5 fe ff ff callq 401030 puts@plt 40113b: 31 c0 xor %eax,%eax 40113d: 59 pop %rcx 40113e: c3 retq
40113f: 90 nop (...)

Note how the string is loaded with an absolute address, compared to:

$ clang-7 -o hello hello.c -O3 -fPIC $ objdump -d hello (...) 0000000000401130

: 401130: 50 push %rax 401131: 48 8d 3d cc 0e 00 00 lea 0xecc(%rip),%rdi # 402004 <_IO_stdin_used+0x4> 401138: e8 f3 fe ff ff callq 401030 puts@plt 40113d: 31 c0 xor %eax,%eax 40113f: 59 pop %rcx 401140: c3 retq
401141: 66 2e 0f 1f 84 00 00 nopw %cs:0x0(%rax,%rax,1) 401148: 00 00 00 40114b: 0f 1f 44 00 00 nopl 0x0(%rax,%rax,1) (...)

FWIW, GCC does respect -fPIC/-fPIE on LTOed non-PIE executables.

glandium commented 6 years ago

Note that one consequence of forcing everything to be non-PIC is the use of copy relocations, which are better avoided.