Does one still need to use -fPIC when compiling with GCC?

前端 未结 5 1173
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-20 12:26

On gcc target machines, when one wanted to compile a shared library, one would need to specify -fpic or -fPIC to get things to work correcly. This is because by default absolute

5条回答
  •  囚心锁ツ
    2021-02-20 13:02

    You still need to compile with -fPIC. The problem isn't solvable with pc-relative addressing. The problem is how you resolve external symbols. In a dynamically linked program the resolution follows different rules and especially with adress space randomization it can't be resolved during link time.

    And clang does have the -fPIC flag just like gcc.

    $ cat > foo.c
    void foo(void);
    void bar(void) { foo(); }
    $ gcc -S foo.c && grep call.*foo foo.s
        call    foo
    $ gcc -fPIC -S foo.c && grep call.*foo foo.s
        call    foo@PLT
    $ clang -S foo.c && grep call.*foo foo.s
        callq   foo
    $ clang -fPIC -S foo.c && grep call.*foo foo.s
        callq   foo@PLT
    $
    

提交回复
热议问题