Add a new (hidden) option -autolink-force-load.

This option puts a special symbol into the generated object files that other
object files can reference to force the library to be loaded.

The next commit will modify the way we serialize autolinking information so
that importers of this module will always emit a reference to this symbol.
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).

Part of <rdar://problem/16829587>

Swift SVN r17750
This commit is contained in:
Jordan Rose
2014-05-09 01:07:07 +00:00
parent 4f0776936a
commit 41700b03da
5 changed files with 37 additions and 1 deletions

View File

@@ -20,6 +20,7 @@
#include "swift/AST/IRGenOptions.h"
#include "swift/ClangImporter/ClangImporter.h"
#include "clang/AST/ASTContext.h"
#include "clang/Basic/CharInfo.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/CodeGen/CodeGenABITypes.h"
#include "clang/CodeGen/ModuleBuilder.h"
@@ -424,6 +425,19 @@ 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";
@@ -437,6 +451,15 @@ void IRGenModule::emitAutolinkInfo() {
llvm::LLVMContext &ctx = Module.getContext();
Module.addModuleFlag(llvm::Module::AppendUnique, LinkerOptionsFlagName,
llvm::MDNode::get(ctx, AutolinkEntries));
if (!Opts.ForceLoadSymbolName.empty()) {
llvm::SmallString<64> buf;
encodeForceLoadSymbolName(buf, Opts.ForceLoadSymbolName);
(void)new llvm::GlobalVariable(Module, Int1Ty, /*constant=*/true,
llvm::GlobalVariable::LinkOnceAnyLinkage,
llvm::Constant::getNullValue(Int1Ty),
buf.str());
}
}
void IRGenModule::finalize() {