llvm.org GIT mirror llvm / fcadda6
llvm-readobj: use the associated string table to print symbols. NFI. This just removes some cases that require ELFFile to eagerly parse the ELF file. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@242794 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 4 years ago
2 changed file(s) with 58 addition(s) and 20 deletion(s). Raw diff Collapse all Expand all
238238 const Elf_Hash *getHashTable() const { return HashTable; }
239239
240240 ErrorOr getStringTable(const Elf_Shdr *Section) const;
241 ErrorOr getStringTableForSymtab(const Elf_Shdr &Section) const;
242
241243 const char *getDynamicString(uintX_t Offset) const;
242244 ErrorOr getSymbolVersion(const Elf_Shdr *section,
243245 const Elf_Sym *Symb,
645647 return;
646648 }
647649 dot_symtab_sec = &Sec;
648 ErrorOr SectionOrErr = getSection(Sec.sh_link);
649 if ((EC = SectionOrErr.getError()))
650 return;
651 ErrorOr SymtabOrErr = getStringTable(*SectionOrErr);
650 ErrorOr SymtabOrErr = getStringTableForSymtab(Sec);
652651 if ((EC = SymtabOrErr.getError()))
653652 return;
654653 DotStrtab = *SymtabOrErr;
875874 if (Data[Size - 1] != '\0')
876875 return object_error::string_table_non_null_end;
877876 return Data;
877 }
878
879 template
880 ErrorOr
881 ELFFile::getStringTableForSymtab(const Elf_Shdr &Sec) const {
882 if (Sec.sh_type != ELF::SHT_SYMTAB && Sec.sh_type != ELF::SHT_DYNSYM)
883 return object_error::parse_failed;
884 ErrorOr SectionOrErr = getSection(Sec.sh_link);
885 if (std::error_code EC = SectionOrErr.getError())
886 return EC;
887 return getStringTable(*SectionOrErr);
878888 }
879889
880890 template
7070 typedef typename ELFO::Elf_Shdr Elf_Shdr;
7171 typedef typename ELFO::Elf_Sym Elf_Sym;
7272
73 void printSymbol(const Elf_Sym *Symbol, bool IsDynamic);
73 void printSymbol(const Elf_Sym *Symbol, StringRef StrTable, bool IsDynamic);
7474
7575 void printRelocations(const Elf_Shdr *Sec);
7676 void printRelocation(const Elf_Shdr *Sec, typename ELFO::Elf_Rela Rel);
125125 template
126126 static std::string getFullSymbolName(const ELFO &Obj,
127127 const typename ELFO::Elf_Sym *Symbol,
128 bool IsDynamic) {
129 StringRef SymbolName = errorOrDefault(Obj.getSymbolName(Symbol, IsDynamic));
128 StringRef StrTable, bool IsDynamic) {
129 StringRef SymbolName = errorOrDefault(Symbol->getName(StrTable));
130130 if (!IsDynamic)
131131 return SymbolName;
132132
645645
646646 if (opts::SectionSymbols) {
647647 ListScope D(W, "Symbols");
648 const Elf_Shdr *Symtab = Obj->getDotSymtabSec();
649 ErrorOr StrTableOrErr = Obj->getStringTableForSymtab(*Symtab);
650 error(StrTableOrErr.getError());
651 StringRef StrTable = *StrTableOrErr;
652
648653 for (const typename ELFO::Elf_Sym &Sym : Obj->symbols()) {
649654 ErrorOr SymSec = Obj->getSection(&Sym);
650655 if (!SymSec)
651656 continue;
652657 if (*SymSec == &Sec)
653 printSymbol(&Sym, false);
658 printSymbol(&Sym, StrTable, false);
654659 }
655660 }
656661
774779 template
775780 void ELFDumper::printSymbols() {
776781 ListScope Group(W, "Symbols");
782
783 const Elf_Shdr *Symtab = Obj->getDotSymtabSec();
784 ErrorOr StrTableOrErr = Obj->getStringTableForSymtab(*Symtab);
785 error(StrTableOrErr.getError());
786 StringRef StrTable = *StrTableOrErr;
777787 for (const typename ELFO::Elf_Sym &Sym : Obj->symbols())
778 printSymbol(&Sym, false);
788 printSymbol(&Sym, StrTable, false);
779789 }
780790
781791 template
782792 void ELFDumper::printDynamicSymbols() {
783793 ListScope Group(W, "DynamicSymbols");
784794
795 const Elf_Shdr *Symtab = Obj->getDotDynSymSec();
796 ErrorOr StrTableOrErr = Obj->getStringTableForSymtab(*Symtab);
797 error(StrTableOrErr.getError());
798 StringRef StrTable = *StrTableOrErr;
785799 for (const typename ELFO::Elf_Sym &Sym : Obj->dynamic_symbols())
786 printSymbol(&Sym, true);
800 printSymbol(&Sym, StrTable, true);
787801 }
788802
789803 template
790804 void ELFDumper::printSymbol(const typename ELFO::Elf_Sym *Symbol,
791 bool IsDynamic) {
805 StringRef StrTable, bool IsDynamic) {
792806 unsigned SectionIndex = 0;
793807 StringRef SectionName;
794808 getSectionNameIndex(*Obj, Symbol, SectionName, SectionIndex);
795 std::string FullSymbolName = getFullSymbolName(*Obj, Symbol, IsDynamic);
809 std::string FullSymbolName =
810 getFullSymbolName(*Obj, Symbol, StrTable, IsDynamic);
796811
797812 DictScope D(W, "Symbol");
798813 W.printNumber("Name", FullSymbolName, Symbol->st_name);
12101225
12111226 void printGotEntry(uint64_t GotAddr, GOTIter BeginIt, GOTIter It);
12121227 void printGlobalGotEntry(uint64_t GotAddr, GOTIter BeginIt, GOTIter It,
1213 const Elf_Sym *Sym, bool IsDynamic);
1228 const Elf_Sym *Sym, StringRef StrTable,
1229 bool IsDynamic);
12141230 void printPLTEntry(uint64_t PLTAddr, GOTIter BeginIt, GOTIter It,
12151231 StringRef Purpose);
12161232 void printPLTEntry(uint64_t PLTAddr, GOTIter BeginIt, GOTIter It,
1217 const Elf_Sym *Sym);
1233 StringRef StrTable, const Elf_Sym *Sym);
12181234 };
12191235 }
12201236
12761292 return;
12771293 }
12781294
1295 const Elf_Shdr *DynSymSec = Obj->getDotDynSymSec();
1296 ErrorOr StrTable = Obj->getStringTableForSymtab(*DynSymSec);
1297 error(StrTable.getError());
12791298 const Elf_Sym *DynSymBegin = Obj->dynamic_symbol_begin();
12801299 const Elf_Sym *DynSymEnd = Obj->dynamic_symbol_end();
12811300 std::size_t DynSymTotal = std::size_t(std::distance(DynSymBegin, DynSymEnd));
13281347 const Elf_Sym *GotDynSym = DynSymBegin + *DtGotSym;
13291348 for (; It != GotGlobalEnd; ++It) {
13301349 DictScope D(W, "Entry");
1331 printGlobalGotEntry(GOTShdr->sh_addr, GotBegin, It, GotDynSym++, true);
1350 printGlobalGotEntry(GOTShdr->sh_addr, GotBegin, It, GotDynSym++,
1351 *StrTable, true);
13321352 }
13331353 }
13341354
13621382 W.startLine() << "There is no .rel.plt section in the file.\n";
13631383 return;
13641384 }
1385 ErrorOr SymTableOrErr =
1386 Obj->getSection(PLTRelShdr->sh_link);
1387 error(SymTableOrErr.getError());
1388 ErrorOr StrTable = Obj->getStringTableForSymtab(**SymTableOrErr);
1389 error(StrTable.getError());
13651390
13661391 GOTIter PLTBegin = makeGOTIter(*PLT, 0);
13671392 GOTIter PLTEnd = makeGOTIter(*PLT, getGOTTotal(*PLT));
13841409 RI != RE && It != PLTEnd; ++RI, ++It) {
13851410 const Elf_Sym *Sym =
13861411 Obj->getRelocationSymbol(&*PLTRelShdr, &*RI).second;
1387 printPLTEntry(PLTShdr->sh_addr, PLTBegin, It, Sym);
1412 printPLTEntry(PLTShdr->sh_addr, PLTBegin, It, *StrTable, Sym);
13881413 }
13891414 break;
13901415 case ELF::SHT_RELA:
13941419 RI != RE && It != PLTEnd; ++RI, ++It) {
13951420 const Elf_Sym *Sym =
13961421 Obj->getRelocationSymbol(&*PLTRelShdr, &*RI).second;
1397 printPLTEntry(PLTShdr->sh_addr, PLTBegin, It, Sym);
1422 printPLTEntry(PLTShdr->sh_addr, PLTBegin, It, *StrTable, Sym);
13981423 }
13991424 break;
14001425 }
14251450 template
14261451 void MipsGOTParser::printGlobalGotEntry(uint64_t GotAddr, GOTIter BeginIt,
14271452 GOTIter It, const Elf_Sym *Sym,
1453 StringRef StrTable,
14281454 bool IsDynamic) {
14291455 printGotEntry(GotAddr, BeginIt, It);
14301456
14361462 getSectionNameIndex(*Obj, Sym, SectionName, SectionIndex);
14371463 W.printHex("Section", SectionName, SectionIndex);
14381464
1439 std::string FullSymbolName = getFullSymbolName(*Obj, Sym, IsDynamic);
1465 std::string FullSymbolName =
1466 getFullSymbolName(*Obj, Sym, StrTable, IsDynamic);
14401467 W.printNumber("Name", FullSymbolName, Sym->st_name);
14411468 }
14421469
14521479
14531480 template
14541481 void MipsGOTParser::printPLTEntry(uint64_t PLTAddr, GOTIter BeginIt,
1455 GOTIter It, const Elf_Sym *Sym) {
1482 GOTIter It, StringRef StrTable,
1483 const Elf_Sym *Sym) {
14561484 DictScope D(W, "Entry");
14571485 int64_t Offset = std::distance(BeginIt, It) * sizeof(GOTEntry);
14581486 W.printHex("Address", PLTAddr + Offset);
14651493 getSectionNameIndex(*Obj, Sym, SectionName, SectionIndex);
14661494 W.printHex("Section", SectionName, SectionIndex);
14671495
1468 std::string FullSymbolName = getFullSymbolName(*Obj, Sym, true);
1496 std::string FullSymbolName = getFullSymbolName(*Obj, Sym, StrTable, true);
14691497 W.printNumber("Name", FullSymbolName, Sym->st_name);
14701498 }
14711499