Index: include/llvm/LTO/LTO.h =================================================================== --- include/llvm/LTO/LTO.h +++ include/llvm/LTO/LTO.h @@ -25,6 +25,7 @@ #include "llvm/LTO/Config.h" #include "llvm/Linker/IRMover.h" #include "llvm/Object/IRObjectFile.h" +#include "llvm/Support/Error.h" #include "llvm/Support/thread.h" #include "llvm/Target/TargetOptions.h" #include "llvm/Transforms/IPO/FunctionImport.h" @@ -82,6 +83,7 @@ // FIXME: Remove the LLVMContext once we have bitcode symbol tables. LLVMContext Ctx; struct InputModule; + std::string LinkerOpts; std::vector Mods; ModuleSymbolTable SymTab; @@ -220,6 +222,9 @@ symbol_iterator(SymTab.symbols().end(), SymTab, this)); } + /// Returns linker options specified in the input file. + Expected getLinkerOpts(); + /// Returns the path to the InputFile. StringRef getName() const; Index: lib/LTO/LTO.cpp =================================================================== --- lib/LTO/LTO.cpp +++ lib/LTO/LTO.cpp @@ -20,9 +20,11 @@ #include "llvm/IR/AutoUpgrade.h" #include "llvm/IR/DiagnosticPrinter.h" #include "llvm/IR/LegacyPassManager.h" +#include "llvm/IR/Metadata.h" #include "llvm/LTO/LTOBackend.h" #include "llvm/Linker/IRMover.h" #include "llvm/Object/ModuleSummaryIndexObjectFile.h" +#include "llvm/Support/Error.h" #include "llvm/Support/ManagedStatic.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Path.h" @@ -288,6 +290,25 @@ return -1; } +Expected InputFile::getLinkerOpts() { + raw_string_ostream LOS(LinkerOpts); + // Extract linker options. + for (InputModule &Mod : Mods) { + std::unique_ptr &M = Mod.Mod; + if (auto E = M->materializeMetadata()) + return std::move(E); + if (Metadata *Val = M->getModuleFlag("Linker Options")) { + MDNode *LinkerOptions = cast(Val); + for (const MDOperand &MDOptions : LinkerOptions->operands()) { + for (const MDOperand &MDOption : cast(MDOptions)->operands()) { + LOS << " " << cast(MDOption)->getString(); + } + } + } + } + return LOS.str(); +} + StringRef InputFile::getName() const { return Mods[0].BM.getModuleIdentifier(); }