Serialize and honor whether a particular library should be force-loaded.

When a module built with -autolink-force-load is imported, add a reference
to a special symbol in the corresponding library so that ld is forced to
link it.

This means the library will be linked into the final binary even if no other
symbols are used (which happens for some of our overlays that just add
category methods to Objective-C classes).

Second part of <rdar://problem/16829587>

Swift SVN r17751
This commit is contained in:
Jordan Rose
2014-05-09 01:20:32 +00:00
parent 41700b03da
commit dde2a8b06b
9 changed files with 63 additions and 38 deletions

View File

@@ -388,6 +388,19 @@ llvm::Constant *IRGenModule::getSize(Size size) {
return llvm::ConstantInt::get(SizeTy, size.getValue());
}
static StringRef encodeForceLoadSymbolName(llvm::SmallVectorImpl<char> &buf,
StringRef name) {
llvm::raw_svector_ostream os{buf};
os << "_swift_FORCE_LOAD_$";
if (clang::isValidIdentifier(name)) {
os << "_" << name;
} else {
for (auto c : name)
os.write_hex(static_cast<uint8_t>(c));
}
return os.str();
}
void IRGenModule::addLinkLibrary(const LinkLibrary &linkLib) {
llvm::LLVMContext &ctx = Module.getContext();
@@ -410,6 +423,22 @@ void IRGenModule::addLinkLibrary(const LinkLibrary &linkLib) {
AutolinkEntries.push_back(llvm::MDNode::get(ctx, args));
break;
}
if (linkLib.shouldForceLoad()) {
llvm::SmallString<64> buf;
encodeForceLoadSymbolName(buf, linkLib.getName());
auto symbolAddr = Module.getOrInsertGlobal(buf.str(), Int1Ty);
buf += "_$_";
buf += Opts.ModuleName;
if (!Module.getGlobalVariable(buf.str())) {
(void)new llvm::GlobalVariable(Module, symbolAddr->getType(),
/*constant=*/true,
llvm::GlobalAlias::LinkOnceAnyLinkage,
symbolAddr, buf.str());
}
}
}
// FIXME: This should just be the implementation of
@@ -425,19 +454,6 @@ static int pointerPODSortComparator(T * const *lhs, T * const *rhs) {
return 0;
}
static StringRef encodeForceLoadSymbolName(llvm::SmallVectorImpl<char> &buf,
StringRef name) {
llvm::raw_svector_ostream os{buf};
os << "_swift_FORCE_LOAD_$";
if (clang::isValidIdentifier(name)) {
os << "_" << name;
} else {
for (auto c : name)
os.write_hex(static_cast<uint8_t>(c));
}
return os.str();
}
void IRGenModule::emitAutolinkInfo() {
// FIXME: This constant should be vended by LLVM somewhere.
static const char * const LinkerOptionsFlagName = "Linker Options";