[cfe-dev] Why -fno-pic and -fPIC gives the same results?

Peng Yu via cfe-dev cfe-dev at lists.llvm.org
Thu Jan 24 08:40:24 PST 2019


On Thu, Jan 24, 2019 at 10:21 AM Peter Smith <peter.smith at linaro.org> wrote:
>
> A caveat, this is on X86_64. Your clang may have a target where all
> code is PIC. Let me know if I've misunderstood your question?
>
> int global;
>
> int f() { return global; }
>
> I've edited the assembler output to remove directives.
> clang f.c -fno-pic -S -o -
> f:
> pushq %rbp
> movq %rsp, %rbp
> movl global, %eax
> popq %rbp
> retq
>
> clang f.c -fPIC -S -o -
> f:
> pushq %rbp
> movq %rsp, %rbp
> movq global at GOTPCREL(%rip), %rax
> movl (%rax), %eax
> popq %rbp
> retq

Here is what I have. So Mac OS X does not support -fno-pic? I think
that clang should show some messages when -fno-pic is not supported.

$ uname
Darwin
$ cat main.sh
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:

set -v
function cmd {
diff <($CC -S -fno-pic f.c -c -o -) <($CC -S -fPIC f.c -c -o -)
}

CC=clang cmd
CC=gcc cmd
$ ./main.sh
function cmd {
diff <($CC -S -fno-pic f.c -c -o -) <($CC -S -fPIC f.c -c -o -)
}

CC=clang cmd
CC=gcc cmd
$ cat f.c
/* vim: set noexpandtab tabstop=2: */
#include <stdio.h>

int global;
int f() { return global; }

$ uname
Linux
$ ./main.sh
function cmd {
diff <("$CC" -S -fno-pic f.c -c -o -) <("$CC" -S -fPIC f.c -c -o -)
}

CC=clang cmd
"$CC" -S -fPIC f.c -c -o -
"$CC" -S -fno-pic f.c -c -o -
14c14,15
<     movl    global, %eax
---
>     movq    global at GOTPCREL(%rip), %rax
>     movl    (%rax), %eax
CC=gcc cmd
"$CC" -S -fPIC f.c -c -o -
"$CC" -S -fno-pic f.c -c -o -
14c14,15
<     movl    global(%rip), %eax
---
>     movq    global at GOTPCREL(%rip), %rax
>     movl    (%rax), %eax


-- 
Regards,
Peng



More information about the cfe-dev mailing list