Skip to content

[IRGen] Be smarter about adding the FORCE_LINK symbols for overlays #15647

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
Mar 31, 2018

Conversation

jrose-apple
Copy link
Contributor

We use dummy symbols to force overlays not to get dropped when autolinking, even if the user doesn't use anything from them explicitly. This behavior is triggered by the semi-hidden flag -autolink-force-load.

(It's semi-hidden because it has few legitimate uses in real life. If you searched for "how to force autolinking to pick up a library" and found this pull request, don't just do this and move on. Come talk to me on https://forums.swift.org.)

Previously we added these dummy symbols to every object file using "common" linkage, a little-known feature added for C that ensures that only one definition will actually get used in the final object file. However, the way we were doing that wouldn't work so well for COFF, and so in #14493 @compnerd changed this to use "weak ODR" linkage. This has nearly the same effect, and avoids some other weirdness, but has the downside of making the symbol in the final dylib "weak" itself, meaning that some other library could come along and override it. That impacts loading time, and an Apple-internal tool caught that as rdar://39019606.

To avoid this whole mess, "just" emit the symbol into the object file that corresponds to the first file in the module, which allows us to mark it as a normal public symbol.

P.S. None of this is actually important at the moment because all of the overlays are built with single-threaded WMO, which always produces one object file anyway. But I wanted to get it right once and for all.

We use dummy symbols to force overlays not to get dropped when
autolinking, even if the user doesn't use anything from them
explicitly. This behavior is triggered by the semi-hidden flag
-autolink-force-load.

(It's semi-hidden because it has few legitimate uses in real life. If
you searched for "how to force autolinking to pick up a library" and
found this commit, don't just do this and move on. Come talk to me on
forums.swift.org.)

Previously we added these dummy symbols to every object file using
"common" linkage, a little-known feature added for C that ensures that
only one definition will actually get used in the final object file.
However, the way we were doing that wouldn't work so well for COFF,
and so in 1025eed Saleem changed this to use "weak ODR" linkage.
This has *nearly* the same effect, and avoids some other weirdness,
but has the downside of making the symbol in the final dylib "weak"
itself, meaning that some /other/ library could come along and
override it. That impacts loading time, and an Apple-internal tool
caught that as rdar://39019606.

To avoid this whole mess, "just" emit the symbol into the object file
that corresponds to the first file in the module, which allows us to
mark it as a normal public symbol.

P.S. None of this is actually important at the moment because all of
the overlays are built with single-threaded WMO, which always produces
one object file anyway. But I wanted to get it right once and for all.
@jrose-apple jrose-apple requested a review from compnerd March 31, 2018 01:28
@jrose-apple
Copy link
Contributor Author

@swift-ci Please test

@jrose-apple
Copy link
Contributor Author

cc @Rostepher

Copy link
Member

@compnerd compnerd left a comment

Choose a reason for hiding this comment

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

This mostly looks good. The one thing that I wonder about is the test suite. IIRC, there are windows tests for the auto linking too, but they seem untouched? That seems a bit odd. In general I've tried hard to try to make the tests run irrespective of the build host, so, perhaps it was loose enough as it was checking that the function was emitted, not the linkage type.

@jrose-apple
Copy link
Contributor Author

Yep, I did check that the COFF IRGen tests were still passing. If there are tests that are only run under OS=windows then we're stuck, but those tests are likely to go stale until we have second-tier CI anyway.

Thanks, Saleem!

@jrose-apple jrose-apple merged commit 6af333f into swiftlang:master Mar 31, 2018
@jrose-apple jrose-apple deleted the weak-force-strong-force branch March 31, 2018 22:20
jrose-apple added a commit to jrose-apple/swift that referenced this pull request Apr 2, 2018
…wiftlang#15647)

We use dummy symbols to force overlays not to get dropped when
autolinking, even if the user doesn't use anything from them
explicitly. This behavior is triggered by the semi-hidden flag
-autolink-force-load.

(It's semi-hidden because it has few legitimate uses in real life. If
you searched for "how to force autolinking to pick up a library" and
found this commit, don't just do this and move on. Come talk to me on
forums.swift.org.)

Previously we added these dummy symbols to every object file using
"common" linkage, a little-known feature added for C that ensures that
only one definition will actually get used in the final object file.
However, the way we were doing that wouldn't work so well for COFF,
and so in 1025eed Saleem changed this to use "weak ODR" linkage.
This has *nearly* the same effect, and avoids some other weirdness,
but has the downside of making the symbol in the final dylib "weak"
itself, meaning that some /other/ library could come along and
override it. That impacts loading time, and an Apple-internal tool
caught that as rdar://39019606.

To avoid this whole mess, "just" emit the symbol into the object file
that corresponds to the first file in the module, which allows us to
mark it as a normal public symbol.

P.S. None of this is actually important at the moment because all of
the overlays are built with single-threaded WMO, which always produces
one object file anyway. But I wanted to get it right once and for all.

(cherry picked from commit 6af333f)
@jrose-apple
Copy link
Contributor Author

Oh, yuck, this doesn't work with incremental builds. I don't think we need to fix that right now, but I should at least put in an error.

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.

2 participants