How to configure gcc to use -no-pie by default?

Aadit M Shah picture Aadit M Shah · Mar 4, 2018 · Viewed 13.7k times · Source

I want to compile the following program on Linux:

    .global _start
    .text
_start:
    mov $1,   %rax
    mov $1,   %rdi
    mov $msg, %rsi
    mov $13,  %rdx
    syscall
    mov $60,  %rax
    xor %rdi, %rdi
    syscall
msg:
    .ascii "Hello World!\n"

However, it gives me the following linker error:

$ gcc -nostdlib hello.s
/usr/bin/ld: /tmp/ccMNQrOF.o: relocation R_X86_64_32S against `.text' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status

I figured that the reason it doesn't work is because gcc is using -pie to generate a shared object by default. Thus, using -no-pie fixes it:

$ gcc -no-pie -nostdlib hello.s
$ ./a.out
Hello World!

How do I configure gcc to use -no-pie by default? I'm using Arch Linux.