Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

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

Open Quuxplusone opened 6 years ago

Quuxplusone commented 6 years ago
Bugzilla Link PR38676
Status NEW
Importance P enhancement
Reported by Mike Hommey (mh+llvm@glandium.org)
Reported on 2018-08-23 01:43:19 -0700
Last modified on 2018-08-23 07:27:26 -0700
Version unspecified
Hardware PC Linux
CC froydnj@gmail.com, llvm-bugs@lists.llvm.org
Fixed by commit(s)
Attachments
Blocks
Blocked by
See also
$ cat hello.c
#include <stdio.h>

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 <main>:
  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 <main>:
  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.
Quuxplusone 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.