llvm.org GIT mirror llvm / 4f20ed1
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 Rafael Espindola 5 years ago
3 changed file(s) with 35 addition(s) and 28 deletion(s). Raw diff Collapse all Expand all
3535 std::error_code printSymbolName(raw_ostream &OS,
3636 DataRefImpl Symb) const override;
3737 uint32_t getSymbolFlags(DataRefImpl Symb) const override;
38 const GlobalValue *getSymbolGV(DataRefImpl Symb) const;
38 GlobalValue *getSymbolGV(DataRefImpl Symb);
39 const GlobalValue *getSymbolGV(DataRefImpl Symb) const {
40 return const_cast(this)->getSymbolGV(Symb);
41 }
3942 basic_symbol_iterator symbol_begin_impl() const override;
4043 basic_symbol_iterator symbol_end_impl() const override;
4144
4548 Module &getModule() {
4649 return *M;
4750 }
51 std::unique_ptr takeModule() { return std::move(M); }
4852
4953 static inline bool classof(const Binary *v) {
5054 return v->isIR();
115115 IRObjectFile::~IRObjectFile() {
116116 }
117117
118 static const GlobalValue *getGV(DataRefImpl &Symb) {
118 static GlobalValue *getGV(DataRefImpl &Symb) {
119119 if ((Symb.p & 3) == 3)
120120 return nullptr;
121121
234234 return Res;
235235 }
236236
237 const GlobalValue *IRObjectFile::getSymbolGV(DataRefImpl Symb) const {
238 const GlobalValue *GV = getGV(Symb);
239 return GV;
240 }
237 GlobalValue *IRObjectFile::getSymbolGV(DataRefImpl Symb) { return getGV(Symb); }
241238
242239 basic_symbol_iterator IRObjectFile::symbol_begin_impl() const {
243240 Module::const_iterator I = M->begin();
260260 return cast(&GV);
261261 }
262262
263 static bool shouldSkip(uint32_t Symflags) {
264 if (!(Symflags & object::BasicSymbolRef::SF_Global))
265 return true;
266 if (Symflags & object::BasicSymbolRef::SF_FormatSpecific)
267 return true;
268 return false;
269 }
270
263271 /// Called by gold to see whether this file is one that our plugin can handle.
264272 /// We'll try to open it and register all the symbols with add_symbol if
265273 /// possible.
317325
318326 for (auto &Sym : Obj->symbols()) {
319327 uint32_t Symflags = Sym.getFlags();
320 if (!(Symflags & object::BasicSymbolRef::SF_Global))
321 continue;
322
323 if (Symflags & object::BasicSymbolRef::SF_FormatSpecific)
328 if (shouldSkip(Symflags))
324329 continue;
325330
326331 cf.syms.push_back(ld_plugin_symbol());
555560 if (get_view(F.handle, &View) != LDPS_OK)
556561 message(LDPL_FATAL, "Failed to get a view of file");
557562
558 llvm::ErrorOr MBOrErr =
559 object::IRObjectFile::findBitcodeInMemBuffer(
560 MemoryBufferRef(StringRef((const char *)View, File.filesize), ""));
561 if (std::error_code EC = MBOrErr.getError())
563 MemoryBufferRef BufferRef(StringRef((const char *)View, File.filesize), "");
564 ErrorOr> ObjOrErr =
565 object::IRObjectFile::createIRObjectFile(BufferRef, Context);
566
567 if (std::error_code EC = ObjOrErr.getError())
562568 message(LDPL_FATAL, "Could not read bitcode from file : %s",
563569 EC.message().c_str());
564570
565 std::unique_ptr Buffer =
566 MemoryBuffer::getMemBuffer(MBOrErr->getBuffer(), "", false);
567
568571 if (release_input_file(F.handle) != LDPS_OK)
569572 message(LDPL_FATAL, "Failed to release file information");
570573
571 ErrorOr MOrErr = getLazyBitcodeModule(std::move(Buffer), Context);
572
573 if (std::error_code EC = MOrErr.getError())
574 message(LDPL_FATAL, "Could not read bitcode from file : %s",
575 EC.message().c_str());
576
577 std::unique_ptr M(MOrErr.get());
574 object::IRObjectFile &Obj = **ObjOrErr;
575
576 Module &M = Obj.getModule();
578577
579578 SmallPtrSet Used;
580 collectUsedGlobalVariables(*M, Used, /*CompilerUsed*/ false);
579 collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false);
581580
582581 DenseSet Drop;
583582 std::vector KeptAliases;
584 for (ld_plugin_symbol &Sym : F.syms) {
583
584 unsigned SymNum = 0;
585 for (auto &ObjSym : Obj.symbols()) {
586 if (shouldSkip(ObjSym.getFlags()))
587 continue;
588 ld_plugin_symbol &Sym = F.syms[SymNum];
589 ++SymNum;
590
585591 ld_plugin_symbol_resolution Resolution =
586592 (ld_plugin_symbol_resolution)Sym.resolution;
587593
588594 if (options::generate_api_file)
589595 *ApiFile << Sym.name << ' ' << getResolutionName(Resolution) << '\n';
590596
591 GlobalValue *GV = M->getNamedValue(Sym.name);
597 GlobalValue *GV = Obj.getSymbolGV(ObjSym.getRawDataRefImpl());
592598 if (!GV)
593599 continue; // Asm symbol.
594600
670676 for (auto *GV : Drop)
671677 drop(*GV);
672678
673 return M;
679 return Obj.takeModule();
674680 }
675681
676682 static void runLTOPasses(Module &M, TargetMachine &TM) {