This is an archive of the discontinued LLVM Phabricator instance.

[Driver] Correctly handle static C++ standard library
AcceptedPublic

Authored by phosek on Sep 20 2021, 11:31 PM.

Details

Summary

When statically linking C++ standard library, we shouldn't add -Bdynamic
after including the library on the link line because that might override
user settings like -static and -static-pie. Rather, we should surround
the library with --push-state/--pop-state to make sure that -Bstatic
only applies to C++ standard library and nothing else. This has been
supported since GNU ld 2.25 (2014) so backwards compatibility should
no longer be a concern.

Diff Detail

Event Timeline

phosek requested review of this revision.Sep 20 2021, 11:31 PM
phosek created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptSep 20 2021, 11:31 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
MaskRay added a comment.EditedSep 21 2021, 11:36 AM

LGTM.

I looked at this in 2019 but did not change because GNU ld 2.25 (2014) introduced --push-state. (gold added it in 2016 but I think we can have higher version requirement for gold.)
In 2021, binutils 2.24 (2013) compatibility should be irrelevant...

clang/lib/Driver/ToolChains/Fuchsia.cpp
142–144

--as-needed only applies to shared objects so you can move it into the else code path.

clang/lib/Driver/ToolChains/Gnu.cpp
581

The --as-needed change should be dropped from this patch.

MaskRay added inline comments.Sep 21 2021, 11:39 AM
clang/test/Driver/linux-ld.c
499

-no-canonical-prefixes is only useful when the CHECK lines inspect the spelling of clang.
Otherwise -no-canonical-prefixes can be omitted.

Omit -o %t.o since it is not used.

504

To improve the robustness of the test, use the -SAME: {{^}} style in linux-cross.cpp, or just place these consecutive options on the same line.

509

Prefer --target= to -target

phosek updated this revision to Diff 374154.Sep 22 2021, 1:37 AM
phosek marked 5 inline comments as done.
MaskRay accepted this revision.Sep 22 2021, 2:26 PM

Thanks!

This revision is now accepted and ready to land.Sep 22 2021, 2:26 PM

Please mention GNU ld 2.25 (2014) in the description.

phosek edited the summary of this revision. (Show Details)Sep 23 2021, 12:58 AM
This revision was landed with ongoing or failed builds.Sep 23 2021, 1:00 AM
This revision was automatically updated to reflect the committed changes.

Looks like the linker used on clang-ppc64le-rhel bot doesn't support --push-state/--pop-state.

@MaskRay Do you think we should gate the use of this feature on -fbinutils-version= or -fuse-ld=lld? It'd be nice if the owner of clang-ppc64le-rhel builder could update the binutils version but I'm not sure how feasible is it.

MaskRay added inline comments.Sep 23 2021, 12:14 PM
clang/lib/Driver/ToolChains/Gnu.cpp
581

--push-state should only be added when -Bstatic is added.

Then the old clang-ppc64le-rhel issue may be moot because they probably don't use -static-libstdc++ ...

phosek updated this revision to Diff 374690.Sep 23 2021, 4:20 PM
phosek marked an inline comment as done.
phosek reopened this revision.Sep 23 2021, 4:32 PM
This revision is now accepted and ready to land.Sep 23 2021, 4:32 PM
MaskRay added inline comments.Sep 23 2021, 4:41 PM
clang/lib/Driver/ToolChains/Fuchsia.cpp
144

as-needed needs push-state as well.

phosek updated this revision to Diff 374699.Sep 23 2021, 5:27 PM
phosek marked an inline comment as done.
This revision was landed with ongoing or failed builds.Sep 24 2021, 12:40 AM
This revision was automatically updated to reflect the committed changes.
lkail added a subscriber: lkail.

@MaskRay Do you think we should gate the use of this feature on -fbinutils-version= or -fuse-ld=lld? It'd be nice if the owner of clang-ppc64le-rhel builder could update the binutils version but I'm not sure how feasible is it.

We certainly don't mind updating binutils to a supported version. However, if a specific version of binutils (or really any other software package) is required by Clang/LLVM, that should be documented at https://llvm.org/docs/GettingStarted.html#software

Of course, if our bot has a version of binutils that is older than the one listed there currently, we will be sure to update it ASAP.

But of course, as @MaskRay pointed out, this may not be an issue.

@MaskRay Do you think we should gate the use of this feature on -fbinutils-version= or -fuse-ld=lld? It'd be nice if the owner of clang-ppc64le-rhel builder could update the binutils version but I'm not sure how feasible is it.

We certainly don't mind updating binutils to a supported version. However, if a specific version of binutils (or really any other software package) is required by Clang/LLVM, that should be documented at https://llvm.org/docs/GettingStarted.html#software

Of course, if our bot has a version of binutils that is older than the one listed there currently, we will be sure to update it ASAP.

But of course, as @MaskRay pointed out, this may not be an issue.

Thanks for reaching out @nemanjai, I assume you're responsible for this builder? Do you know what version of Binutils is currently installed on that machine? Is newer version available for RHEL 7?

We don't specify Binutils version on https://llvm.org/docs/GettingStarted.html but I think we should. We need to determine that baseline first though. It looks like 2.25 roughly corresponds to GCC 5.1 which is the minimum requirement for the compiler.

Note that stage 2 fails, where we're supposed to use lld. So I think this is probably some compiler-rt test config problem you need to sort out, and not a fundamental problem with the patch.

Note that stage 2 fails, where we're supposed to use lld. So I think this is probably some compiler-rt test config problem you need to sort out, and not a fundamental problem with the patch.

It looks like the LLVM_ENABLE_LLD option doesn't affect compiler-rt tests.

phosek reopened this revision.Sep 24 2021, 6:29 PM
This revision is now accepted and ready to land.Sep 24 2021, 6:29 PM