11

I have just been painfully discovering that gcc apparently generates -fpic code by default and links with -fPIE by default (On ubuntu 17.04). This completely screws up thousands of tests I run with scripts and makefiles used by lots of different linux distros. Is there any global or per-user way to turn off these defaults and make the compiler backward compatible with the behavior it has had for decades? I'm not interested in tracking down every compilation in thousands of scripts that knows the default is not -fpic, etc. An environment variable or two perhaps?

1
  • 1
    Well, if it's a gcc-6 issue specifically, gcc-4.7 through gcc-5 all appear to be available in the repository - no-one's forcing you to migrate at this point. May 3, 2017 at 20:12

1 Answer 1

11

I had the same problem and just solved it thanks to this post on Stack Overflow.

You should add -no-pie option to compilation command line

without:

$ gcc main.c -o main
$ file main 
main:ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32,
BuildID[sha1]=46ada4e5e25fc120ca052c9beb8bfa5491fc6239, not stripped

with:

$ gcc main.c -o main -no-pie
$ file main
main: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32,
BuildID[sha1]=17f860c6c84fc1a5771c8744b7aaaf164c219559, not stripped
2
  • 1
    Can you include the steps in your answer in case the link become broken in the future. Jul 20, 2017 at 11:22
  • 2
    Changing the existing 47,000 lines of test scripts and makefiles which know they don't need an option to not link a PIE executable and don't need an option to not compile with PIC is not a viable solution. Couldn't we get an alternative gcc driver package with the old behavior? And use the alternatives mechanism to decide which should be the default? Aug 25, 2017 at 14:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.