Skip to content

build: PIC-ify the dynamic SDK overlay #15801

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
Apr 9, 2018
Merged

Conversation

compnerd
Copy link
Member

@compnerd compnerd commented Apr 6, 2018

The dynamic SDK overlay needs to be built with -fPIC. Ensure that the
clang importer is marking the external globals appropriately. This was
caught by building the swiftGlibc SDK overlay component in a dynamic
configuration. Failure to do so will create RIP-relative references on
x86_64 which may overflow at runtime.

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

compnerd commented Apr 6, 2018

@jrose-apple perhaps we should have a CI build that builds the shared SDK overlay for Linux?

@compnerd
Copy link
Member Author

compnerd commented Apr 6, 2018

@swift-ci please test

@jrose-apple
Copy link
Contributor

I'm pretty sure that's how all our builds work on Linux. Why would we do this here and not in the Clang importer in general? Do we need to support non-PIC at all?

@jckarter
Copy link
Contributor

jckarter commented Apr 6, 2018

No, we can't and shouldn't need to support non-PIC.

@compnerd
Copy link
Member Author

compnerd commented Apr 6, 2018

@jrose-apple hmm, doing that in the clang importer seems reasonable. Not supporting PIC can technically be beneficial. The non-PIC CodeGen would avoid the GOT/PLT which can be a significant cost if you are calling functions across modules often. But, always building PIC means that it would work everywhere.

@jckarter
Copy link
Contributor

jckarter commented Apr 6, 2018

The GOT and PLT are only strictly necessary for cross-dynamic library calls, in which case they're unavoidable. Within a binary, they're unnecessary and can be avoided if the symbols are known to be in the same binary. The overhead of PIC for code within a binary was more significant for platforms like 32-bit i386 that didn't support PC-relative data addressing, but for x86_64 the cost of PIC is much smaller. Disabling PIC is not supported for dynamic libraries on Linux, and disabling it for executables means they can't support ASLR.

@jrose-apple
Copy link
Contributor

Cool, then let's just throw this in the importer and be done with it. :-)

@compnerd compnerd force-pushed the pics branch 2 times, most recently from 6b52460 to 2a86b36 Compare April 6, 2018 23:20
@compnerd
Copy link
Member Author

compnerd commented Apr 9, 2018

@swift-ci please test

@jrose-apple
Copy link
Contributor

Is there a way to write a test case for this?

The dynamic SDK overlay needs to be built with -fPIC.  Ensure that the
clang importer is marking the external globals appropriately.  This was
caught by building the swiftGlibc SDK overlay component in a dynamic
configuration.  Failure to do so will create RIP-relative references on
x86_64 which may overflow at runtime.
@compnerd
Copy link
Member Author

compnerd commented Apr 9, 2018

@jrose-apple to ensure that the flag is there, or something else?

@jrose-apple
Copy link
Contributor

I guess that we don't generate code that references Clang-imported declarations incorrectly.

@compnerd
Copy link
Member Author

compnerd commented Apr 9, 2018

Yeah, I didn't see much that a test would've caught really. But, if you have an idea of something that would be useful, I'd be happy to address that.

@jrose-apple
Copy link
Contributor

Ah, right, because none of this shows up until linking, and then the actual thing you'd check would have to be disassembly, which is pretty platform-specific. All right, never mind.

@compnerd
Copy link
Member Author

compnerd commented Apr 9, 2018

@swift-ci please test and merge

@swift-ci
Copy link
Contributor

swift-ci commented Apr 9, 2018

Build failed
Swift Test Linux Platform
Git Sha - 2a86b366a671bcef1c4d2e46be34c007de37e0af

@swift-ci
Copy link
Contributor

swift-ci commented Apr 9, 2018

Build failed
Swift Test OS X Platform
Git Sha - 2a86b366a671bcef1c4d2e46be34c007de37e0af

@swift-ci swift-ci merged commit e74f2a6 into swiftlang:master Apr 9, 2018
@compnerd compnerd deleted the pics branch April 9, 2018 21:56
@jckarter
Copy link
Contributor

jckarter commented Apr 9, 2018

While it'd be nice to have a test, I think that failure to build things as PIC will manifest itself incidentally fairly reliably when we try to link a dynamic library with non-PIC code in it.

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.

4 participants