swiftlang/swift

[SR-8960] Emit a symbol reference to ensure swiftrt.o is linked on Linux/Windows

Open

#51,465 opened on Oct 10, 2018

View on GitHub
 (1 comment) (0 reactions) (0 assignees)Swift (69,989 stars) (10,719 forks)batch import
Linuxbugcompilergood first issueruntime

Description

Previous ID SR-8960
Radar None
Original Reporter @jckarter
Type Bug
Votes 0
Component/s Compiler
Labels Bug, Linux, Runtime, StarterBug
Assignee None
Priority Medium

md5: 7f33519b3b36a320eb441265efc7bc17

Issue Description:

On Linux and Windows, Swift binaries rely on a small stub object file to inject a static constructor necessary to register with the Swift runtime on load. When Swift object files are incorporated with other language object files and build systems, it's easy to forget to explicitly link this object file, which can appear to work at first but leads to surprising behavior when the metadata for the binary cannot be found at runtime. We can have the linker help us out with this problem, by having swiftrt.o define a dummy absolute symbol with a conspicuous name, and having the Swift compiler emit a dummy reference to this symbol if the generated IR includes any data structures that require runtime registration, so that linking without including swiftrt.o fails.

It should be possible to do this in such a way that the resulting symbols get dead-stripped because they wouldn't in reality be used anywhere by the final linked image.

Contributor guide