This is an archive of the discontinued LLVM Phabricator instance.

[Driver] Add CLANG_DEFAULT_PIE_ON_LINUX to emulate GCC --enable-default-pie
ClosedPublic

Authored by MaskRay on Nov 7 2021, 2:38 PM.

Details

Summary

In 2015-05, GCC added the configure option --enable-default-pie. When enabled,

  • in the absence of -fno-pic/-fpie/-fpic (and their upper-case variants), -fPIE is the default.
  • in the absence of -no-pie/-pie/-shared/-static/-static-pie, -pie is the default.

This has been adopted by all(?) major distros.

I think default PIE is the majority in the Linux world, but
--disable-default-pie users is not that uncommon because GCC upstream hasn't
switched the default yet (https://gcc.gnu.org/PR103398).

This patch add CLANG_DEFAULT_PIE_ON_LINUX which allows distros to use default PIE.
The option is justified as its adoption can be very high among Linux distros
to make Clang default match GCC, and is likely a future-new-default, at which
point we will remove CLANG_DEFAULT_PIE_ON_LINUX.
The lit feature default-pie-on-linux can be handy to exclude default PIE sensitive tests.

Diff Detail

Event Timeline

MaskRay created this revision.Nov 7 2021, 2:38 PM
MaskRay requested review of this revision.Nov 7 2021, 2:38 PM
Herald added a project: Restricted Project. · View Herald TranscriptNov 7 2021, 2:38 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
MaskRay updated this revision to Diff 385375.Nov 7 2021, 2:43 PM

add a test

ajakk added a subscriber: ajakk.Nov 7 2021, 2:52 PM

Maybe add it to the release notes too?

MaskRay updated this revision to Diff 389305.Nov 23 2021, 1:24 PM

Add a release note

Herald added a project: Restricted Project. · View Herald TranscriptNov 23 2021, 1:24 PM
thesamesam accepted this revision.Dec 1 2021, 11:28 AM

This is working well here on Gentoo.

This brings some long-desired feature parity to Clang which we've been wanting downstream: GCC has had this option for quite a few years (after pushing from various downstreams, including us) and this ends up biting folks who want to try migrate to Clang as their system compiler.

The change is rather conservative given we're defaulting to PIE being off for now (and this is the same as GCC anyway).

This revision is now accepted and ready to land.Dec 1 2021, 11:28 AM

I will wait until Dec 14 before committing.

Hope Arch Linux folks can test this, too: @felixonmars @foutrelis

sylvestre.ledru accepted this revision.Dec 11 2021, 10:21 AM
foutrelis accepted this revision.Dec 11 2021, 12:03 PM

No issues with default PIE and SSP on Arch for over 4 years now. It's nice to see this being upstreamed; thanks for doing it. This patch worked fine.

Based on my own hack of a patch, the tests should be somewhat straight-foward to fix, though I'm not sure how elegantly they can be adjusted.

joerg added a subscriber: joerg.Dec 11 2021, 7:22 PM
joerg added inline comments.
clang/CMakeLists.txt
230

This option should really be called something like CLANG_DEFAULT_PIE_ON_LINUX or so. It should be explicit from the name that it only effects that toolchain and not all the various toolchains that support PIE.

MaskRay added inline comments.Dec 12 2021, 3:40 PM
clang/CMakeLists.txt
230

This name is fine with me. This ecosystem variety probably only happens for Linux and other operations systems probably can just switch unconditionally (with possibly gated on an os version).

MaskRay updated this revision to Diff 393782.Dec 12 2021, 4:33 PM
MaskRay retitled this revision from [Driver] Add CLANG_DEFAULT_PIE to emulate GCC --enable-default-pie to [Driver] Add CLANG_DEFAULT_PIE_ON_LINUX to emulate GCC --enable-default-pie.
MaskRay edited the summary of this revision. (Show Details)

CLANG_DEFAULT_PIE => CLANG_DEFAULT_PIE_ON_LINUX

I have fixed all test/Driver tests in a separate commit.

joerg added a comment.Dec 12 2021, 8:22 PM

Last update introduced a lot of unrelated changes? But the actual intended change seems fine now.

Last update introduced a lot of unrelated changes? But the actual intended change seems fine now.

The last update just did a renaming. I have checked that changes are all intended.

MaskRay updated this revision to Diff 394015.Dec 13 2021, 12:49 PM

Rename test/Driver/default-pie.c to linux-default-pie.c

clang-format a region.

MaskRay edited the summary of this revision. (Show Details)Dec 14 2021, 10:07 AM
This revision was landed with ongoing or failed builds.Dec 14 2021, 10:09 AM
This revision was automatically updated to reflect the committed changes.
arichardson added inline comments.
clang/CMakeLists.txt
232

Can these 3 lines be removed after D115751?

MaskRay marked an inline comment as done.Dec 28 2021, 10:50 AM
MaskRay added inline comments.
clang/CMakeLists.txt
232
MaskRay marked an inline comment as done.Dec 28 2021, 10:50 AM

When enabled, this seems to break a fair number of tests:

Clang :: CodeGen/mips-vector-return.c
Clang :: Driver/hexagon-toolchain-elf.c
Clang :: Driver/hip-fpie-option.hip
Clang :: Driver/mips-cs.cpp
Clang :: Driver/mips-fsf.cpp
Clang :: Driver/mips-img-v2.cpp
Clang :: Driver/mips-img.cpp
Clang :: Driver/mips-mti-linux.c

I've been able to get the hexagon test to work by passing -fno-pie explicitly but others don't seem this easy.

When enabled, this seems to break a fair number of tests:

Clang :: CodeGen/mips-vector-return.c
Clang :: Driver/hexagon-toolchain-elf.c
Clang :: Driver/hip-fpie-option.hip
Clang :: Driver/mips-cs.cpp
Clang :: Driver/mips-fsf.cpp
Clang :: Driver/mips-img-v2.cpp
Clang :: Driver/mips-img.cpp
Clang :: Driver/mips-mti-linux.c

I've been able to get the hexagon test to work by passing -fno-pie explicitly but others don't seem this easy.

In release/14.x, these have all been fixed.