Open glandium opened 6 years ago
$ cat hello.c
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.
Note that one consequence of forcing everything to be non-PIC is the use of copy relocations, which are better avoided.
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.