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

Nico Weber via cfe-dev cfe-dev at lists.llvm.org
Fri Jan 25 13:23:31 PST 2019


64-bit mac binaries (both x86 and arm) always force PIC on and it can't be
disabled there. (cf
http://llvm-cs.pcc.me.uk/tools/clang/lib/Driver/ToolChains/Darwin.cpp#2222)

On Thu, Jan 24, 2019 at 11:40 AM Peng Yu via cfe-dev <cfe-dev at lists.llvm.org>
wrote:

> 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
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20190125/2ac74d3f/attachment.html>


More information about the cfe-dev mailing list