[cfe-dev] PIE/PIC Clang Defaults on Linux x86-64

Fangrui Song via cfe-dev cfe-dev at lists.llvm.org
Tue Jun 2 15:38:46 PDT 2020


On 2020-06-02, Eric Christopher wrote:
>I'll elaborate on this a bit more since it may not be coming across clearly:
>
>a) gcc and clang differ here:
>
>clang++/g++  -o foo.x foo.c -fPIE
>
>gcc passes -pie to the linker and clang doesn't. This mostly seems like an
>oversight that can be fixed.
>
>b) gcc and clang differ here
>
>clang++/g++ -o foo.x foo.c
>
>where gcc defaults to -pie and pie code generation and clang doesn't.
>
>This is something where we should at least have the discussion. For what
>it's worth my thought is that we should move to PIE by default for linux
>if, for not other reason, as the system compiler (gcc) already does this.
>We also already do this on android and in some sanitizer cases.
>
>Anyone else have any thoughts?
>
>Thanks!
>
>-eric

GCC 6 introduced a configure option --enable-default-pie (default -fPIE and -pie)
(https://gcc.gnu.org/git/?p=gcc.git&a=commit;h=428b381275bc95032274440f02d7719225de2c17 )

Distributions can decide whether they want to enable it.

There are two questions:

(1) Do we want to make default-pie configurable (which requires a CMake option)
(2) Whether or not this is configurable, do we want Generic_GCC::isPIEDefault() to return true

At this point, it is probably that more distributions enable it by default.
So switching Generic_GCC::isPIEDefault() probably makes sense.
I want to avoid a CMake variable to reduce issues in testing.
(These CLANG_DEFAULT_* are a source of test instability.)

(I suspect some distributions already ship downstream patches to enable
-fPIE and -pie by default.)

>On Tue, Jun 2, 2020 at 2:44 PM Mitch Phillips via cfe-dev <
>cfe-dev at lists.llvm.org> wrote:
>
>> Hi folks,
>>
>> I recently noticed that clang and GCC differ on the default PIE/PIC flags.
>> In particular, clang doesn't default to PIE/PIC on Linux x86-64, and GCC
>> does.
>>
>> $ clang++ a.cpp && readelf -l a.out | egrep '(LOAD|VirtAddr)'
>>>   Type           Offset             VirtAddr           PhysAddr
>>>   LOAD           0x0000000000000000 0x0000000000400000 0x0000000000400000
>>>
>>
>> $ g++ a.cpp && readelf -l a.out | egrep '(LOAD|VirtAddr)'
>>>   Type           Offset             VirtAddr           PhysAddr
>>
>>   LOAD           0x0000000000000000 0x0000000000000000 0x0000000000000000
>>
>>
>> Are there any concerns about changing the clang default to emit PIC and
>> pass PIE to the linker?
>>
>> Also, using `clang++ -fPIC -fPIE <...>` doesn't emit a PIE. Any opponents
>> to making `-fPIE` imply passing `-pie` to the linker?
>>
>> - Mitch.
>> _______________________________________________
>> cfe-dev mailing list
>> cfe-dev at lists.llvm.org
>> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>>


More information about the cfe-dev mailing list