llvm.org GIT mirror llvm / 8109763
[Object][ELF] Support dumping hash-tables from files with no section table. This time without breaking the bots. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@241869 91177308-0d34-0410-b5e6-96231b3b80d8 Michael J. Spencer 4 years ago
8 changed file(s) with 105 addition(s) and 30 deletion(s). Raw diff Collapse all Expand all
1515
1616 #include "llvm/ADT/ArrayRef.h"
1717 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/IntervalMap.h"
1819 #include "llvm/ADT/PointerIntPair.h"
1920 #include "llvm/ADT/SmallVector.h"
2021 #include "llvm/ADT/StringSwitch.h"
138139 typedef Elf_Verneed_Impl Elf_Verneed;
139140 typedef Elf_Vernaux_Impl Elf_Vernaux;
140141 typedef Elf_Versym_Impl Elf_Versym;
142 typedef Elf_Hash Elf_Hash;
141143 typedef ELFEntityIterator Elf_Dyn_Iter;
142144 typedef iterator_range Elf_Dyn_Range;
143145 typedef ELFEntityIterator Elf_Rela_Iter;
175177 const Elf_Shdr *dot_symtab_sec = nullptr; // Symbol table section.
176178 StringRef DynSymStrTab; // Dynnamic symbol string table.
177179 const Elf_Shdr *DotDynSymSec = nullptr; // Dynamic symbol table section.
180 const Elf_Hash *HashTable = nullptr;
178181
179182 const Elf_Shdr *SymbolTableSectionHeaderIndex = nullptr;
180183 DenseMap ExtendedSymbolTable;
228231 void LoadVersionNeeds(const Elf_Shdr *ec) const;
229232 void LoadVersionMap() const;
230233
234 void scanDynamicTable();
235
231236 public:
232237 template
233238 const T *getEntry(uint32_t Section, uint32_t Entry) const;
236241
237242 const Elf_Shdr *getDotSymtabSec() const { return dot_symtab_sec; }
238243 const Elf_Shdr *getDotDynSymSec() const { return DotDynSymSec; }
244 const Elf_Hash *getHashTable() const { return HashTable; }
239245
240246 ErrorOr getStringTable(const Elf_Shdr *Section) const;
241247 const char *getDynamicString(uintX_t Offset) const;
577583
578584 Header = reinterpret_cast(base());
579585
580 if (Header->e_shoff == 0)
586 if (Header->e_shoff == 0) {
587 scanDynamicTable();
581588 return;
589 }
582590
583591 const uint64_t SectionTableOffset = Header->e_shoff;
584592
603611
604612 for (const Elf_Shdr &Sec : sections()) {
605613 switch (Sec.sh_type) {
614 case ELF::SHT_HASH:
615 if (HashTable) {
616 EC = object_error::parse_failed;
617 return;
618 }
619 HashTable = reinterpret_cast(base() + Sec.sh_offset);
620 break;
606621 case ELF::SHT_SYMTAB_SHNDX:
607622 if (SymbolTableSectionHeaderIndex) {
608623 // More than one .symtab_shndx!
700715 }
701716 }
702717
703 // Scan program headers.
718 scanDynamicTable();
719
720 EC = std::error_code();
721 }
722
723 template
724 void ELFFile::scanDynamicTable() {
725 // Build load-address to file-offset map.
726 typedef IntervalMap<
727 uintX_t, uintptr_t,
728 IntervalMapImpl::NodeSizer::LeafSize,
729 IntervalMapHalfOpenInfo> LoadMapT;
730 typename LoadMapT::Allocator Alloc;
731 LoadMapT LoadMap(Alloc);
732
704733 for (Elf_Phdr_Iter PhdrI = program_header_begin(),
705734 PhdrE = program_header_end();
706735 PhdrI != PhdrE; ++PhdrI) {
708737 DynamicRegion.Addr = base() + PhdrI->p_offset;
709738 DynamicRegion.Size = PhdrI->p_filesz;
710739 DynamicRegion.EntSize = sizeof(Elf_Dyn);
711 break;
712 }
713 }
714
715 // Scan dynamic table.
740 continue;
741 }
742 if (PhdrI->p_type != ELF::PT_LOAD)
743 continue;
744 if (PhdrI->p_filesz == 0)
745 continue;
746 LoadMap.insert(PhdrI->p_vaddr, PhdrI->p_vaddr + PhdrI->p_filesz,
747 PhdrI->p_offset);
748 }
749
750 auto toMappedAddr = [&](uint64_t VAddr) -> const uint8_t * {
751 auto I = LoadMap.find(VAddr);
752 if (I == LoadMap.end())
753 return nullptr;
754 return base() + I.value() + (VAddr - I.start());
755 };
756
716757 for (Elf_Dyn_Iter DynI = dynamic_table_begin(), DynE = dynamic_table_end();
717758 DynI != DynE; ++DynI) {
718759 switch (DynI->d_tag) {
719 case ELF::DT_RELA: {
720 uint64_t VBase = 0;
721 const uint8_t *FBase = nullptr;
722 for (Elf_Phdr_Iter PhdrI = program_header_begin(),
723 PhdrE = program_header_end();
724 PhdrI != PhdrE; ++PhdrI) {
725 if (PhdrI->p_type != ELF::PT_LOAD)
726 continue;
727 if (DynI->getPtr() >= PhdrI->p_vaddr &&
728 DynI->getPtr() < PhdrI->p_vaddr + PhdrI->p_memsz) {
729 VBase = PhdrI->p_vaddr;
730 FBase = base() + PhdrI->p_offset;
731 break;
732 }
733 }
734 if (!VBase)
735 return;
736 DynRelaRegion.Addr = FBase + DynI->getPtr() - VBase;
760 case ELF::DT_HASH:
761 if (HashTable)
762 continue;
763 HashTable =
764 reinterpret_cast(toMappedAddr(DynI->getPtr()));
737765 break;
738 }
766 case ELF::DT_RELA:
767 if (!DynRelaRegion.Addr)
768 DynRelaRegion.Addr = toMappedAddr(DynI->getPtr());
769 break;
739770 case ELF::DT_RELASZ:
740771 DynRelaRegion.Size = DynI->getVal();
741772 break;
743774 DynRelaRegion.EntSize = DynI->getVal();
744775 }
745776 }
746
747 EC = std::error_code();
748777 }
749778
750779 template
99 #ifndef LLVM_OBJECT_ELFTYPES_H
1010 #define LLVM_OBJECT_ELFTYPES_H
1111
12 #include "llvm/ADT/ArrayRef.h"
1213 #include "llvm/Object/Error.h"
1314 #include "llvm/Support/DataTypes.h"
1415 #include "llvm/Support/ELF.h"
462463 Elf_Xword p_align; // Segment alignment constraint
463464 };
464465
466 // ELFT needed for endianess.
467 template
468 struct Elf_Hash {
469 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
470 Elf_Word nbucket;
471 Elf_Word nchain;
472
473 ArrayRef buckets() const {
474 return ArrayRef(&nbucket + 2, &nbucket + 2 + nbucket);
475 }
476
477 ArrayRef chains() const {
478 return ArrayRef(&nbucket + 2 + nbucket,
479 &nbucket + 2 + nbucket + nchain);
480 }
481 };
482
465483 // MIPS .reginfo section
466484 template
467485 struct Elf_Mips_RegInfo;
0 RUN: llvm-readobj %p/Inputs/no-section-table.so -hash-table | FileCheck %s
1
2 CHECK: HashTable {
3 CHECK: Num Buckets: 3
4 CHECK: Num Chains: 13
5 CHECK: Buckets: [12, 10, 11]
6 CHECK: Chains: [0, 0, 0, 0, 2, 3, 4, 0, 7, 5, 6, 8, 9]
7 CHECK: }
5555 void printDynamicTable() override;
5656 void printNeededLibraries() override;
5757 void printProgramHeaders() override;
58 void printHashTable() override;
5859
5960 void printAttributes() override;
6061 void printMipsPLTGOT() override;
11181119 }
11191120 }
11201121
1122 template
1123 void ELFDumper::printHashTable() {
1124 DictScope D(W, "HashTable");
1125 auto HT = Obj->getHashTable();
1126 if (!HT)
1127 return;
1128 W.printNumber("Num Buckets", HT->nbucket);
1129 W.printNumber("Num Chains", HT->nchain);
1130 W.printList("Buckets", HT->buckets());
1131 W.printList("Chains", HT->chains());
1132 }
1133
11211134 template
11221135 void ELFDumper::printAttributes() {
11231136 W.startLine() << "Attributes not implemented.\n";
3636 virtual void printDynamicTable() { }
3737 virtual void printNeededLibraries() { }
3838 virtual void printProgramHeaders() { }
39 virtual void printHashTable() { }
3940
4041 // Only implemented for ARM ELF at this time.
4142 virtual void printAttributes() { }
180180 startLine() << Label << ": " << (Value ? "Yes" : "No") << '\n';
181181 }
182182
183 template
184 void printList(StringRef Label, const SmallVectorImpl &List) {
183 template
184 void printList(StringRef Label, const T &List) {
185185 startLine() << Label << ": [";
186186 bool Comma = false;
187187 for (const auto &Item : List) {
126126 cl::opt ProgramHeaders("program-headers",
127127 cl::desc("Display ELF program headers"));
128128
129 // -hash-table
130 cl::opt HashTable("hash-table",
131 cl::desc("Display ELF hash table"));
132
129133 // -expand-relocs
130134 cl::opt ExpandRelocs("expand-relocs",
131135 cl::desc("Expand each shown relocation to multiple lines"));
299303 Dumper->printNeededLibraries();
300304 if (opts::ProgramHeaders)
301305 Dumper->printProgramHeaders();
306 if (opts::HashTable)
307 Dumper->printHashTable();
302308 if (Obj->getArch() == llvm::Triple::arm && Obj->isELF())
303309 if (opts::ARMAttributes)
304310 Dumper->printAttributes();