Skip to content

Driver: prevent C++ runtime linkage #26110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 25, 2019

Conversation

compnerd
Copy link
Member

Replace this paragraph with a description of your changes and rationale. Provide links to external references/discussions if appropriate.

Resolves SR-NNNN.

@compnerd
Copy link
Member Author

CC: @jrose-apple

@compnerd
Copy link
Member Author

@swift-ci please test

// `--as-needed` however that breaks other Unix semantics of linking where
// the dependency on the library is not visible to the linker should still be
// linked upon request).
Arguments.push_back("-nostdlib++");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where does the C++ runtime get linked, then?

Copy link
Member Author

@compnerd compnerd Jul 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would you need the C++ runtime? Swift doesn't yet allow importing C++, so there is no dependency from any Swift code on the C++ runtime. If you are worried about the standard library, that explicitly links in C++ mode using clang++ rather than the swift driver (separate issue). If you have dependencies which rely on C++, they will be linked against the C++ runtime.

On android you should be linking against stl_port (or if you are targeting something in the future, libc++). On Linux, you should be linking against libstdc++ unless you are running a custom closed environment that is libc++ based. On Windows, you should be linking against msvcprt unless you are using libc++ across your application. On Darwin, you should link against libc++. Because you have multiple options based on the target, we need to either make this explicit from the user at some point. But, given that we currently do not support importing C++ code, I think that we can deal with that issue when the time comes.

Copy link

@ephemer ephemer Jul 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just chiming in here- I don't know enough about this to have a strong opinion either way, but I did notice I have to add -Xclang-linker -nostdlib++ to swiftc invocations to prevent libc++ being linked for Swift(-only) binaries on Android with recent compiler versions.

Adding the flag doesn't seem to have any negative effect, but again, I'm far from an expert at this. There's a reason for everything :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about static linking, but I think that's already covered with an explicit -lc++ and I just forgot. Sorry for the distraction!

@compnerd compnerd changed the title Minus minus is double plus good Driver: prevent C++ runtime linkage Jul 12, 2019
@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 38afc15ee79cfabeb6d08d2c21caf65d9278f9a7

@compnerd
Copy link
Member Author

Hmm, I think that it is falling back to the system clang which may be older than 7.0 which introduced the flag. I'm not sure what the best way to handle this is. I guess I can restrict this to just android for the time being, though this should apply equally to all Unices.

@compnerd compnerd force-pushed the minus-minus-is-double-plus-good branch from 38afc15 to a4336f8 Compare July 13, 2019 03:52
@compnerd
Copy link
Member Author

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - a4336f8e6847682cd10b25f579e81fc4a5ea26f5

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - a4336f8e6847682cd10b25f579e81fc4a5ea26f5

@compnerd compnerd force-pushed the minus-minus-is-double-plus-good branch from a4336f8 to 13f41f2 Compare July 13, 2019 04:23
@compnerd
Copy link
Member Author

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - a4336f8e6847682cd10b25f579e81fc4a5ea26f5

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - a4336f8e6847682cd10b25f579e81fc4a5ea26f5

@gwynne
Copy link
Contributor

gwynne commented Jul 14, 2019

My take is to merge this as is for now, with the caveat that it absolutely shouldn't be left this way any longer than it needs to be. The stopgap is, I think, worth it.

@jrose-apple
Copy link
Contributor

Another option: go down to clang instead of clang++ to link. https://reviews.llvm.org/D35780

@compnerd
Copy link
Member Author

compnerd commented Jul 15, 2019

@jrose-apple - yeah, I did consider that, but that means that we also loose out on -lm. Its unclear whether that is worth the effort of trying to make the clang++ driver work right now. Going to update the patch to do that instead.

@compnerd compnerd force-pushed the minus-minus-is-double-plus-good branch from 13f41f2 to dc597fd Compare July 15, 2019 16:29
@compnerd
Copy link
Member Author

@swift-ci please test

@@ -153,14 +159,13 @@ toolchains::GenericUnix::constructInvocation(const DynamicLinkJobAction &job,

// Configure the toolchain.
// By default, use the system clang++ to link.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment needs correction (and would probably be a good place to explain why someone shouldn't just change it back to clang++).

Copy link
Member Author

@compnerd compnerd Jul 15, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sigh that is what happens when you try to be quick. Thanks for pointing that out. (I'll wait until the tests complete to update the diff).

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - dc597fd2bf3be1fa263ed0c7c662581f9ff11045

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - dc597fd2bf3be1fa263ed0c7c662581f9ff11045

@compnerd compnerd force-pushed the minus-minus-is-double-plus-good branch from dc597fd to e22137b Compare July 16, 2019 16:35
@compnerd
Copy link
Member Author

@swift-ci please test

@compnerd
Copy link
Member Author

@swift-ci please test Windows platform

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - e22137b50b78bf783105cbadd46c490d60f8cdcc

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - e22137b50b78bf783105cbadd46c490d60f8cdcc

@compnerd
Copy link
Member Author

@swift-ci please test macOS platform

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - e22137b50b78bf783105cbadd46c490d60f8cdcc

@compnerd compnerd force-pushed the minus-minus-is-double-plus-good branch from e22137b to 4fa9fe4 Compare July 23, 2019 23:08
@compnerd
Copy link
Member Author

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - e22137b50b78bf783105cbadd46c490d60f8cdcc

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - e22137b50b78bf783105cbadd46c490d60f8cdcc

@compnerd compnerd force-pushed the minus-minus-is-double-plus-good branch from 4fa9fe4 to e8b4ef5 Compare July 24, 2019 04:03
@compnerd
Copy link
Member Author

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 4fa9fe44263b5f9bcd12d97ad926f76fadabe0a3

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 4fa9fe44263b5f9bcd12d97ad926f76fadabe0a3

Copy link
Contributor

@drodriguez drodriguez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "fake" clang++ that was renamed to clang in test/Driver/Inputs/fake-toolchain/ has lost the executable bits, and so it is not found by the UnixToolChains.cpp and making the tools_directory.swift test fail.

@compnerd compnerd force-pushed the minus-minus-is-double-plus-good branch from e8b4ef5 to 32e1342 Compare July 24, 2019 22:47
@compnerd
Copy link
Member Author

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 32e1342b3a87ab376567be65bf7823bdcbf4f56d

@compnerd compnerd force-pushed the minus-minus-is-double-plus-good branch from 32e1342 to 3a32c2f Compare July 25, 2019 00:45
@compnerd
Copy link
Member Author

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 32e1342b3a87ab376567be65bf7823bdcbf4f56d

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 32e1342b3a87ab376567be65bf7823bdcbf4f56d

Use `clang` rather than `clang++` as the linker driver.  This ensures
that we do not force a C++ runtime on the general code.  This is fine
for now as C++ interop is not yet available for Swift.  This prevents
the accidental mix-and-match of various C++ runtimes.  This can cause
problems on platforms like android where `libstdc++` is an unsupported
runtime but is generally the default for Linux platforms.
@compnerd compnerd force-pushed the minus-minus-is-double-plus-good branch from 3a32c2f to 67475dc Compare July 25, 2019 02:48
@compnerd
Copy link
Member Author

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 3a32c2f37c15afc8b602aaf6bb25e5ce1845223d

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 3a32c2f37c15afc8b602aaf6bb25e5ce1845223d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants