From 4f20ed1c3683c02e9507e8f49d2c95adc2fb47c6 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Tue, 9 Dec 2014 16:13:59 +0000 Subject: [PATCH] Don't lookup an object symbol name in the module. Instead, walk the obj symbol list in parallel to find the GV. This shouldn't change anything on ELF where global symbols are not mangled, but it is a step toward supporting other object formats. Gold itself is ELF only, but bfd ld supports COFF and the logic in the gold plugin could be reused on lld. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@223780 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Object/IRObjectFile.h | 6 +++- lib/Object/IRObjectFile.cpp | 7 ++--- tools/gold/gold-plugin.cpp | 48 +++++++++++++++++------------- 3 files changed, 34 insertions(+), 27 deletions(-) diff --git a/include/llvm/Object/IRObjectFile.h b/include/llvm/Object/IRObjectFile.h index b650d5d3293..10ea05ddf1f 100644 --- a/include/llvm/Object/IRObjectFile.h +++ b/include/llvm/Object/IRObjectFile.h @@ -36,7 +36,10 @@ public: std::error_code printSymbolName(raw_ostream &OS, DataRefImpl Symb) const override; uint32_t getSymbolFlags(DataRefImpl Symb) const override; - const GlobalValue *getSymbolGV(DataRefImpl Symb) const; + GlobalValue *getSymbolGV(DataRefImpl Symb); + const GlobalValue *getSymbolGV(DataRefImpl Symb) const { + return const_cast(this)->getSymbolGV(Symb); + } basic_symbol_iterator symbol_begin_impl() const override; basic_symbol_iterator symbol_end_impl() const override; @@ -46,6 +49,7 @@ public: Module &getModule() { return *M; } + std::unique_ptr takeModule() { return std::move(M); } static inline bool classof(const Binary *v) { return v->isIR(); diff --git a/lib/Object/IRObjectFile.cpp b/lib/Object/IRObjectFile.cpp index 7256a2fc007..84b6c95f2c7 100644 --- a/lib/Object/IRObjectFile.cpp +++ b/lib/Object/IRObjectFile.cpp @@ -116,7 +116,7 @@ IRObjectFile::IRObjectFile(MemoryBufferRef Object, std::unique_ptr Mod) IRObjectFile::~IRObjectFile() { } -static const GlobalValue *getGV(DataRefImpl &Symb) { +static GlobalValue *getGV(DataRefImpl &Symb) { if ((Symb.p & 3) == 3) return nullptr; @@ -235,10 +235,7 @@ uint32_t IRObjectFile::getSymbolFlags(DataRefImpl Symb) const { return Res; } -const GlobalValue *IRObjectFile::getSymbolGV(DataRefImpl Symb) const { - const GlobalValue *GV = getGV(Symb); - return GV; -} +GlobalValue *IRObjectFile::getSymbolGV(DataRefImpl Symb) { return getGV(Symb); } basic_symbol_iterator IRObjectFile::symbol_begin_impl() const { Module::const_iterator I = M->begin(); diff --git a/tools/gold/gold-plugin.cpp b/tools/gold/gold-plugin.cpp index b7d8c5e14c6..972dbd32365 100644 --- a/tools/gold/gold-plugin.cpp +++ b/tools/gold/gold-plugin.cpp @@ -261,6 +261,14 @@ static const GlobalObject *getBaseObject(const GlobalValue &GV) { return cast(&GV); } +static bool shouldSkip(uint32_t Symflags) { + if (!(Symflags & object::BasicSymbolRef::SF_Global)) + return true; + if (Symflags & object::BasicSymbolRef::SF_FormatSpecific) + return true; + return false; +} + /// Called by gold to see whether this file is one that our plugin can handle. /// We'll try to open it and register all the symbols with add_symbol if /// possible. @@ -318,10 +326,7 @@ static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file, for (auto &Sym : Obj->symbols()) { uint32_t Symflags = Sym.getFlags(); - if (!(Symflags & object::BasicSymbolRef::SF_Global)) - continue; - - if (Symflags & object::BasicSymbolRef::SF_FormatSpecific) + if (shouldSkip(Symflags)) continue; cf.syms.push_back(ld_plugin_symbol()); @@ -556,40 +561,41 @@ getModuleForFile(LLVMContext &Context, claimed_file &F, raw_fd_ostream *ApiFile, if (get_view(F.handle, &View) != LDPS_OK) message(LDPL_FATAL, "Failed to get a view of file"); - llvm::ErrorOr MBOrErr = - object::IRObjectFile::findBitcodeInMemBuffer( - MemoryBufferRef(StringRef((const char *)View, File.filesize), "")); - if (std::error_code EC = MBOrErr.getError()) + MemoryBufferRef BufferRef(StringRef((const char *)View, File.filesize), ""); + ErrorOr> ObjOrErr = + object::IRObjectFile::createIRObjectFile(BufferRef, Context); + + if (std::error_code EC = ObjOrErr.getError()) message(LDPL_FATAL, "Could not read bitcode from file : %s", EC.message().c_str()); - std::unique_ptr Buffer = - MemoryBuffer::getMemBuffer(MBOrErr->getBuffer(), "", false); - if (release_input_file(F.handle) != LDPS_OK) message(LDPL_FATAL, "Failed to release file information"); - ErrorOr MOrErr = getLazyBitcodeModule(std::move(Buffer), Context); + object::IRObjectFile &Obj = **ObjOrErr; - if (std::error_code EC = MOrErr.getError()) - message(LDPL_FATAL, "Could not read bitcode from file : %s", - EC.message().c_str()); - - std::unique_ptr M(MOrErr.get()); + Module &M = Obj.getModule(); SmallPtrSet Used; - collectUsedGlobalVariables(*M, Used, /*CompilerUsed*/ false); + collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false); DenseSet Drop; std::vector KeptAliases; - for (ld_plugin_symbol &Sym : F.syms) { + + unsigned SymNum = 0; + for (auto &ObjSym : Obj.symbols()) { + if (shouldSkip(ObjSym.getFlags())) + continue; + ld_plugin_symbol &Sym = F.syms[SymNum]; + ++SymNum; + ld_plugin_symbol_resolution Resolution = (ld_plugin_symbol_resolution)Sym.resolution; if (options::generate_api_file) *ApiFile << Sym.name << ' ' << getResolutionName(Resolution) << '\n'; - GlobalValue *GV = M->getNamedValue(Sym.name); + GlobalValue *GV = Obj.getSymbolGV(ObjSym.getRawDataRefImpl()); if (!GV) continue; // Asm symbol. @@ -671,7 +677,7 @@ getModuleForFile(LLVMContext &Context, claimed_file &F, raw_fd_ostream *ApiFile, for (auto *GV : Drop) drop(*GV); - return M; + return Obj.takeModule(); } static void runLTOPasses(Module &M, TargetMachine &TM) {