Index: include/llvm-c/lto.h =================================================================== --- include/llvm-c/lto.h +++ include/llvm-c/lto.h @@ -201,6 +201,34 @@ /** + * Returns the number of dependent libraries in the object module. + */ +extern unsigned int +lto_module_get_num_deplibs(lto_module_t mod); + + +/** + * Returns the ith dependent library in the module. + */ +extern const char* +lto_module_get_deplib(lto_module_t mod, unsigned int index); + + +/** + * Returns the number of linker options in the object module. + */ +extern unsigned int +lto_module_get_num_linkeropts(lto_module_t mod); + + +/** + * Returns the ith linker option in the module. + */ +extern const char* +lto_module_get_linkeropt(lto_module_t mod, unsigned int index); + + +/** * Instantiates a code generator. * Returns NULL on error (check lto_get_error_message() for details). */ Index: include/llvm/CodeGen/TargetLoweringObjectFileImpl.h =================================================================== --- include/llvm/CodeGen/TargetLoweringObjectFileImpl.h +++ include/llvm/CodeGen/TargetLoweringObjectFileImpl.h @@ -80,6 +80,10 @@ public: virtual ~TargetLoweringObjectFileMachO() {} + /// getDepLibFromLinkerOpt - Extract the dependent library name from a linker + /// option string. Returns NULL if the option does not specify a library. + virtual const char *getDepLibFromLinkerOpt(StringRef LinkerOption) const; + /// emitModuleFlags - Emit the module flags that specify the garbage /// collection information. virtual void emitModuleFlags(MCStreamer &Streamer, @@ -129,6 +133,10 @@ SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, Mangler *Mang, const TargetMachine &TM) const; + /// getDepLibFromLinkerOpt - Extract the dependent library name from a linker + /// option string. Returns NULL if the option does not specify a library. + virtual const char *getDepLibFromLinkerOpt(StringRef LinkerOption) const; + /// emitModuleFlags - Emit Obj-C garbage collection and linker options. Only /// linker option emission is implemented for COFF. virtual void emitModuleFlags(MCStreamer &Streamer, Index: include/llvm/LTO/LTOModule.h =================================================================== --- include/llvm/LTO/LTOModule.h +++ include/llvm/LTO/LTOModule.h @@ -51,6 +51,8 @@ llvm::OwningPtr _module; llvm::OwningPtr _target; llvm::MCObjectFileInfo ObjFileInfo; + std::vector _deplibs; + std::vector _linkeropts; std::vector _symbols; // _defines and _undefines only needed to disambiguate tentative definitions @@ -129,6 +131,30 @@ return NULL; } + /// getDependentLibraryCount - Get the number of dependent libraries + uint32_t getDependentLibraryCount() { + return _deplibs.size(); + } + + /// getDependentLibrary - Get the dependent library at the specified index. + const char *getDependentLibrary(uint32_t index) { + if (index < _deplibs.size()) + return _deplibs[index]; + return NULL; + } + + /// getLinkerOptCount - Get the number of linker options + uint32_t getLinkerOptCount() { + return _linkeropts.size(); + } + + /// getLinkerOpt - Get the linker option at the specified index. + const char *getLinkerOpt(uint32_t index) { + if (index < _linkeropts.size()) + return _linkeropts[index]; + return NULL; + } + /// getLLVVMModule - Return the Module. llvm::Module *getLLVVMModule() { return _module.get(); } @@ -138,6 +164,10 @@ } private: + /// parseMetadata - Parse metadata from the module + // FIXME: it only parses "Linker Options" metadata at the moment + void parseMetadata(); + /// parseSymbols - Parse the symbols from the module and model-level ASM and /// add them to either the defined or undefined lists. bool parseSymbols(std::string &errMsg); Index: include/llvm/Target/TargetLoweringObjectFile.h =================================================================== --- include/llvm/Target/TargetLoweringObjectFile.h +++ include/llvm/Target/TargetLoweringObjectFile.h @@ -55,6 +55,12 @@ const TargetMachine &TM, const MCSymbol *Sym) const; + /// getDepLibFromLinkerOpt - Extract the dependent library name from a linker + /// option string. Returns NULL if the option does not specify a library. + virtual const char *getDepLibFromLinkerOpt(StringRef LinkerOption) const { + return NULL; + } + /// emitModuleFlags - Emit the module flags that the platform cares about. virtual void emitModuleFlags(MCStreamer &, ArrayRef, Index: lib/CodeGen/TargetLoweringObjectFileImpl.cpp =================================================================== --- lib/CodeGen/TargetLoweringObjectFileImpl.cpp +++ lib/CodeGen/TargetLoweringObjectFileImpl.cpp @@ -402,6 +402,16 @@ // MachO //===----------------------------------------------------------------------===// +/// getDepLibFromLinkerOpt - Extract the dependent library name from a linker +/// option string. Returns NULL if the option does not specify a library. +const char *TargetLoweringObjectFileMachO:: +getDepLibFromLinkerOpt(StringRef LinkerOption) const { + const char *LibCmd = "-1"; + if (LinkerOption.startswith(LibCmd)) + return LinkerOption.data() + strlen(LibCmd); + return NULL; +} + /// emitModuleFlags - Perform code emission for module flags. void TargetLoweringObjectFileMachO:: emitModuleFlags(MCStreamer &Streamer, @@ -774,6 +784,14 @@ return DataSection; } +const char *TargetLoweringObjectFileCOFF:: +getDepLibFromLinkerOpt(StringRef LinkerOption) const { + const char *LibCmd = "/DEFAULTLIB:"; + if (LinkerOption.startswith(LibCmd)) + return LinkerOption.data() + strlen(LibCmd); + return NULL; +} + void TargetLoweringObjectFileCOFF:: emitModuleFlags(MCStreamer &Streamer, ArrayRef ModuleFlags, Index: lib/LTO/LTOModule.cpp =================================================================== --- lib/LTO/LTOModule.cpp +++ lib/LTO/LTOModule.cpp @@ -18,6 +18,7 @@ #include "llvm/Bitcode/ReaderWriter.h" #include "llvm/IR/Constants.h" #include "llvm/IR/LLVMContext.h" +#include "llvm/IR/Metadata.h" #include "llvm/IR/Module.h" #include "llvm/MC/MCExpr.h" #include "llvm/MC/MCInst.h" @@ -37,6 +38,8 @@ #include "llvm/Support/TargetRegistry.h" #include "llvm/Support/TargetSelect.h" #include "llvm/Support/system_error.h" +#include "llvm/Target/TargetLowering.h" +#include "llvm/Target/TargetLoweringObjectFile.h" #include "llvm/Target/TargetRegisterInfo.h" #include "llvm/Transforms/Utils/GlobalStatus.h" using namespace llvm; @@ -175,6 +178,8 @@ return NULL; } + Ret->parseMetadata(); + return Ret; } @@ -796,3 +801,25 @@ return false; } + +/// parseMetadata - Parse metadata from the module +void LTOModule::parseMetadata() { + // Linker Options + if (Value *Val = _module->getModuleFlag("Linker Options")) { + MDNode *LinkerOptions = cast(Val); + for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) { + MDNode *MDOptions = cast(LinkerOptions->getOperand(i)); + for (unsigned ii = 0, ie = MDOptions->getNumOperands(); ii != ie; ++ii) { + MDString *MDOption = cast(MDOptions->getOperand(ii)); + StringRef Op = MDOption->getString(); + if (const char *DepLibName = _target->getTargetLowering()-> + getObjFileLowering().getDepLibFromLinkerOpt(Op)) + _deplibs.push_back(DepLibName); + else if (!Op.empty()) + _linkeropts.push_back(Op.data()); + } + } + } + + // Add other interesting metadata here. +} Index: tools/lto/lto.cpp =================================================================== --- tools/lto/lto.cpp +++ tools/lto/lto.cpp @@ -193,6 +193,28 @@ return mod->getSymbolAttributes(index); } +/// lto_module_get_num_deplibs - Returns the number of dependent libraries in +/// the object module. +unsigned int lto_module_get_num_deplibs(lto_module_t mod) { + return mod->getDependentLibraryCount(); +} + +/// lto_module_get_deplib - Returns the ith dependent library in the module. +const char* lto_module_get_deplib(lto_module_t mod, unsigned int index) { + return mod->getDependentLibrary(index); +} + +/// lto_module_get_num_linkeropts - Returns the number of linker options in the +/// object module. +unsigned int lto_module_get_num_linkeropts(lto_module_t mod) { + return mod->getLinkerOptCount(); +} + +/// lto_module_get_linkeropt - Returns the ith linker option in the module. +const char* lto_module_get_linkeropt(lto_module_t mod, unsigned int index) { + return mod->getLinkerOpt(index); +} + /// lto_codegen_create - Instantiates a code generator. Returns NULL if there /// is an error. lto_code_gen_t lto_codegen_create(void) { Index: tools/lto/lto.exports =================================================================== --- tools/lto/lto.exports +++ tools/lto/lto.exports @@ -5,6 +5,10 @@ lto_module_create_from_fd lto_module_create_from_fd_at_offset lto_module_create_from_memory +lto_module_get_deplib +lto_module_get_linkeropt +lto_module_get_num_deplibs +lto_module_get_num_linkeropts lto_module_get_num_symbols lto_module_get_symbol_attribute lto_module_get_symbol_name