COFF: Read linker directives from bitcode files.
Differential Revision: http://reviews.llvm.org/D10285
llvm-svn: 239212
diff --git a/lld/COFF/InputFiles.cpp b/lld/COFF/InputFiles.cpp
index ec8e9f5..62bb144 100644
--- a/lld/COFF/InputFiles.cpp
+++ b/lld/COFF/InputFiles.cpp
@@ -263,6 +263,23 @@
SymbolBodies.push_back(new (Alloc) DefinedBitcode(SymName));
}
}
+
+ // Extract any linker directives from the bitcode file, which are represented
+ // as module flags with the key "Linker Options".
+ llvm::SmallVector<llvm::Module::ModuleFlagEntry, 8> Flags;
+ M->getModule().getModuleFlagsMetadata(Flags);
+ for (auto &&Flag : Flags) {
+ if (Flag.Key->getString() != "Linker Options")
+ continue;
+
+ for (llvm::Metadata *Op : cast<llvm::MDNode>(Flag.Val)->operands()) {
+ for (llvm::Metadata *InnerOp : cast<llvm::MDNode>(Op)->operands()) {
+ Directives += " ";
+ Directives += cast<llvm::MDString>(InnerOp)->getString();
+ }
+ }
+ }
+
return std::error_code();
}
diff --git a/lld/COFF/InputFiles.h b/lld/COFF/InputFiles.h
index 9d7b0ef..e4c75d4 100644
--- a/lld/COFF/InputFiles.h
+++ b/lld/COFF/InputFiles.h
@@ -170,6 +170,9 @@
LTOModule *getModule() const { return M.get(); }
LTOModule *releaseModule() { return M.release(); }
+ // Returns linker directives from module flags metadata if present.
+ StringRef getDirectives() { return Directives; }
+
private:
std::error_code parse() override;
@@ -177,6 +180,7 @@
std::vector<SymbolBody *> SymbolBodies;
llvm::BumpPtrAllocator Alloc;
std::unique_ptr<LTOModule> M;
+ std::string Directives;
};
} // namespace coff
diff --git a/lld/COFF/SymbolTable.cpp b/lld/COFF/SymbolTable.cpp
index 35042e5..a90e208 100644
--- a/lld/COFF/SymbolTable.cpp
+++ b/lld/COFF/SymbolTable.cpp
@@ -40,6 +40,17 @@
return addImport(cast<ImportFile>(FileP));
}
+std::error_code SymbolTable::addDirectives(StringRef Dir) {
+ if (Dir.empty())
+ return std::error_code();
+ std::vector<std::unique_ptr<InputFile>> Libs;
+ if (auto EC = Driver->parseDirectives(Dir, &Libs))
+ return EC;
+ for (std::unique_ptr<InputFile> &Lib : Libs)
+ addFile(std::move(Lib));
+ return std::error_code();
+}
+
std::error_code SymbolTable::addObject(ObjectFile *File) {
ObjectFiles.emplace_back(File);
for (SymbolBody *Body : File->getSymbols())
@@ -49,15 +60,7 @@
// If an object file contains .drectve section, read it and add
// files listed in the section.
- StringRef Dir = File->getDirectives();
- if (!Dir.empty()) {
- std::vector<std::unique_ptr<InputFile>> Libs;
- if (auto EC = Driver->parseDirectives(Dir, &Libs))
- return EC;
- for (std::unique_ptr<InputFile> &Lib : Libs)
- addFile(std::move(Lib));
- }
- return std::error_code();
+ return addDirectives(File->getDirectives());
}
std::error_code SymbolTable::addArchive(ArchiveFile *File) {
@@ -75,8 +78,8 @@
if (auto EC = resolve(Body))
return EC;
- // TODO: Handle linker directives.
- return std::error_code();
+ // Add any linker directives from the module flags metadata.
+ return addDirectives(File->getDirectives());
}
std::error_code SymbolTable::addImport(ImportFile *File) {
diff --git a/lld/COFF/SymbolTable.h b/lld/COFF/SymbolTable.h
index 0c5e795..99a794d 100644
--- a/lld/COFF/SymbolTable.h
+++ b/lld/COFF/SymbolTable.h
@@ -77,6 +77,8 @@
std::error_code rename(StringRef From, StringRef To);
private:
+ std::error_code addDirectives(StringRef Dir);
+
std::error_code addObject(ObjectFile *File);
std::error_code addArchive(ArchiveFile *File);
std::error_code addImport(ImportFile *File);