From: Xing GUO Date: Mon, 25 Mar 2019 11:02:49 +0000 (+0000) Subject: [llvm-readobj] Separate `Symbol Version` dumpers into `LLVM style` and `GNU style` X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=ea16be1ca7e4bf97a3faa00b5baf14b60894f283;p=platform%2Fupstream%2Fllvm.git [llvm-readobj] Separate `Symbol Version` dumpers into `LLVM style` and `GNU style` Summary: Currently, llvm-readobj can dump symbol version sections only in LLVM style. In this patch, I would like to separate these dumpers into GNU style and LLVM style for future implementation. Reviewers: grimar, jhenderson, mattd, rupprecht Reviewed By: jhenderson, rupprecht Subscribers: ormris, dyung, RKSimon, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D59186 llvm-svn: 356881 --- diff --git a/llvm/test/tools/llvm-readobj/elf-versioninfo.test b/llvm/test/tools/llvm-readobj/elf-versioninfo.test index 82029cf..7ef599e 100644 --- a/llvm/test/tools/llvm-readobj/elf-versioninfo.test +++ b/llvm/test/tools/llvm-readobj/elf-versioninfo.test @@ -1,106 +1,119 @@ // Test that llvm-readobj dumps version info tags correctly. -RUN: llvm-readobj -dynamic-table -V %p/Inputs/verdef.elf-x86-64 | FileCheck %s +RUN: llvm-readobj -dynamic-table -V %p/Inputs/verdef.elf-x86-64 | FileCheck %s --check-prefix=LLVM-VERDEF +RUN: llvm-readelf -dynamic-table -V %p/Inputs/verdef.elf-x86-64 | FileCheck %s --check-prefix=GNU-VERDEF -CHECK: 0x000000006FFFFFF0 VERSYM 0x24C -CHECK: 0x000000006FFFFFFC VERDEF 0x25C -CHECK: 0x000000006FFFFFFD VERDEFNUM 3 +LLVM-VERDEF: 0x000000006FFFFFF0 VERSYM 0x24C +LLVM-VERDEF: 0x000000006FFFFFFC VERDEF 0x25C +LLVM-VERDEF: 0x000000006FFFFFFD VERDEFNUM 3 -CHECK: Version symbols { -CHECK-NEXT: Section Name: .gnu.version (20) -CHECK-NEXT: Address: 0x24C -CHECK-NEXT: Offset: 0x24C -CHECK-NEXT: Link: 1 -CHECK-NEXT: Symbols [ -CHECK-NEXT: Symbol { -CHECK-NEXT: Version: 0 -CHECK-NEXT: Name: {{$}} -CHECK-NEXT: } -CHECK-NEXT: Symbol { -CHECK-NEXT: Version: 1 -CHECK-NEXT: Name: _end{{$}} -CHECK-NEXT: } -CHECK-NEXT: Symbol { -CHECK-NEXT: Version: 1 -CHECK-NEXT: Name: _edata{{$}} -CHECK-NEXT: } -CHECK-NEXT: Symbol { -CHECK-NEXT: Version: 3 -CHECK-NEXT: Name: goo@@VERSION2 -CHECK-NEXT: } -CHECK-NEXT: Symbol { -CHECK-NEXT: Version: 1 -CHECK-NEXT: Name: __bss_start{{$}} -CHECK-NEXT: } -CHECK-NEXT: Symbol { -CHECK-NEXT: Version: 2 -CHECK-NEXT: Name: foo@@VERSION1 -CHECK-NEXT: } -CHECK-NEXT: Symbol { -CHECK-NEXT: Version: 2 -CHECK-NEXT: Name: VERSION1@@VERSION1 -CHECK-NEXT: } -CHECK-NEXT: Symbol { -CHECK-NEXT: Version: 3 -CHECK-NEXT: Name: VERSION2@@VERSION2 -CHECK-NEXT: } -CHECK-NEXT: ] -CHECK-NEXT: } +LLVM-VERDEF: Version symbols { +LLVM-VERDEF-NEXT: Section Name: .gnu.version (20) +LLVM-VERDEF-NEXT: Address: 0x24C +LLVM-VERDEF-NEXT: Offset: 0x24C +LLVM-VERDEF-NEXT: Link: 1 +LLVM-VERDEF-NEXT: Symbols [ +LLVM-VERDEF-NEXT: Symbol { +LLVM-VERDEF-NEXT: Version: 0 +LLVM-VERDEF-NEXT: Name: {{$}} +LLVM-VERDEF-NEXT: } +LLVM-VERDEF-NEXT: Symbol { +LLVM-VERDEF-NEXT: Version: 1 +LLVM-VERDEF-NEXT: Name: _end{{$}} +LLVM-VERDEF-NEXT: } +LLVM-VERDEF-NEXT: Symbol { +LLVM-VERDEF-NEXT: Version: 1 +LLVM-VERDEF-NEXT: Name: _edata{{$}} +LLVM-VERDEF-NEXT: } +LLVM-VERDEF-NEXT: Symbol { +LLVM-VERDEF-NEXT: Version: 3 +LLVM-VERDEF-NEXT: Name: goo@@VERSION2 +LLVM-VERDEF-NEXT: } +LLVM-VERDEF-NEXT: Symbol { +LLVM-VERDEF-NEXT: Version: 1 +LLVM-VERDEF-NEXT: Name: __bss_start{{$}} +LLVM-VERDEF-NEXT: } +LLVM-VERDEF-NEXT: Symbol { +LLVM-VERDEF-NEXT: Version: 2 +LLVM-VERDEF-NEXT: Name: foo@@VERSION1 +LLVM-VERDEF-NEXT: } +LLVM-VERDEF-NEXT: Symbol { +LLVM-VERDEF-NEXT: Version: 2 +LLVM-VERDEF-NEXT: Name: VERSION1@@VERSION1 +LLVM-VERDEF-NEXT: } +LLVM-VERDEF-NEXT: Symbol { +LLVM-VERDEF-NEXT: Version: 3 +LLVM-VERDEF-NEXT: Name: VERSION2@@VERSION2 +LLVM-VERDEF-NEXT: } +LLVM-VERDEF-NEXT: ] +LLVM-VERDEF-NEXT: } -CHECK: SHT_GNU_verdef { -CHECK-NEXT: Definition { -CHECK-NEXT: Version: 1 -CHECK-NEXT: Flags: Base (0x1) -CHECK-NEXT: Index: 1 -CHECK-NEXT: Hash: 430712 -CHECK-NEXT: Name: blah -CHECK-NEXT: } -CHECK-NEXT: Definition { -CHECK-NEXT: Version: 1 -CHECK-NEXT: Flags: 0x0 -CHECK-NEXT: Index: 2 -CHECK-NEXT: Hash: 175630257 -CHECK-NEXT: Name: VERSION1 -CHECK-NEXT: } -CHECK-NEXT: Definition { -CHECK-NEXT: Version: 1 -CHECK-NEXT: Flags: 0x0 -CHECK-NEXT: Index: 3 -CHECK-NEXT: Hash: 175630258 -CHECK-NEXT: Name: VERSION2 -CHECK-NEXT: Predecessor: VERSION1 -CHECK-NEXT: } -CHECK-NEXT: } +LLVM-VERDEF: SHT_GNU_verdef { +LLVM-VERDEF-NEXT: Definition { +LLVM-VERDEF-NEXT: Version: 1 +LLVM-VERDEF-NEXT: Flags: Base (0x1) +LLVM-VERDEF-NEXT: Index: 1 +LLVM-VERDEF-NEXT: Hash: 430712 +LLVM-VERDEF-NEXT: Name: blah +LLVM-VERDEF-NEXT: } +LLVM-VERDEF-NEXT: Definition { +LLVM-VERDEF-NEXT: Version: 1 +LLVM-VERDEF-NEXT: Flags: 0x0 +LLVM-VERDEF-NEXT: Index: 2 +LLVM-VERDEF-NEXT: Hash: 175630257 +LLVM-VERDEF-NEXT: Name: VERSION1 +LLVM-VERDEF-NEXT: } +LLVM-VERDEF-NEXT: Definition { +LLVM-VERDEF-NEXT: Version: 1 +LLVM-VERDEF-NEXT: Flags: 0x0 +LLVM-VERDEF-NEXT: Index: 3 +LLVM-VERDEF-NEXT: Hash: 175630258 +LLVM-VERDEF-NEXT: Name: VERSION2 +LLVM-VERDEF-NEXT: Predecessor: VERSION1 +LLVM-VERDEF-NEXT: } +LLVM-VERDEF-NEXT: } -RUN: llvm-readobj -V %p/Inputs/verneed.elf-x86-64 | FileCheck %s --check-prefix=VERNEED +GNU-VERDEF: 0x000000006ffffff0 VERSYM 0x24c +GNU-VERDEF: 0x000000006ffffffc VERDEF 0x25c +GNU-VERDEF: 0x000000006ffffffd VERDEFNUM 3 + +GNU-VERDEF: Dumper for .gnu.version is not implemented +GNU-VERDEF: Dumper for .gnu.version_d is not implemented + +RUN: llvm-readobj -V %p/Inputs/verneed.elf-x86-64 | FileCheck %s --check-prefix=LLVM-VERNEED +RUN: llvm-readelf -V %p/Inputs/verneed.elf-x86-64 | FileCheck %s --check-prefix=GNU-VERNEED + +LLVM-VERNEED: SHT_GNU_verneed { +LLVM-VERNEED-NEXT: Dependency { +LLVM-VERNEED-NEXT: Version: 1 +LLVM-VERNEED-NEXT: Count: 2 +LLVM-VERNEED-NEXT: FileName: verneed1.so.0 +LLVM-VERNEED-NEXT: Entry { +LLVM-VERNEED-NEXT: Hash: 1938 +LLVM-VERNEED-NEXT: Flags: 0x0 +LLVM-VERNEED-NEXT: Index: 3 +LLVM-VERNEED-NEXT: Name: v2 +LLVM-VERNEED-NEXT: } +LLVM-VERNEED-NEXT: Entry { +LLVM-VERNEED-NEXT: Hash: 1939 +LLVM-VERNEED-NEXT: Flags: 0x0 +LLVM-VERNEED-NEXT: Index: 2 +LLVM-VERNEED-NEXT: Name: v3 +LLVM-VERNEED-NEXT: } +LLVM-VERNEED-NEXT: } +LLVM-VERNEED-NEXT: Dependency { +LLVM-VERNEED-NEXT: Version: 1 +LLVM-VERNEED-NEXT: Count: 1 +LLVM-VERNEED-NEXT: FileName: verneed2.so.0 +LLVM-VERNEED-NEXT: Entry { +LLVM-VERNEED-NEXT: Hash: 1937 +LLVM-VERNEED-NEXT: Flags: 0x0 +LLVM-VERNEED-NEXT: Index: 4 +LLVM-VERNEED-NEXT: Name: v1 +LLVM-VERNEED-NEXT: } +LLVM-VERNEED-NEXT: } +LLVM-VERNEED-NEXT: } + +GNU-VERNEED: Dumper for .gnu.version is not implemented +GNU-VERNEED: Dumper for .gnu.version_r is not implemented -VERNEED: SHT_GNU_verneed { -VERNEED-NEXT: Dependency { -VERNEED-NEXT: Version: 1 -VERNEED-NEXT: Count: 2 -VERNEED-NEXT: FileName: verneed1.so.0 -VERNEED-NEXT: Entry { -VERNEED-NEXT: Hash: 1938 -VERNEED-NEXT: Flags: 0x0 -VERNEED-NEXT: Index: 3 -VERNEED-NEXT: Name: v2 -VERNEED-NEXT: } -VERNEED-NEXT: Entry { -VERNEED-NEXT: Hash: 1939 -VERNEED-NEXT: Flags: 0x0 -VERNEED-NEXT: Index: 2 -VERNEED-NEXT: Name: v3 -VERNEED-NEXT: } -VERNEED-NEXT: } -VERNEED-NEXT: Dependency { -VERNEED-NEXT: Version: 1 -VERNEED-NEXT: Count: 1 -VERNEED-NEXT: FileName: verneed2.so.0 -VERNEED-NEXT: Entry { -VERNEED-NEXT: Hash: 1937 -VERNEED-NEXT: Flags: 0x0 -VERNEED-NEXT: Index: 4 -VERNEED-NEXT: Name: v1 -VERNEED-NEXT: } -VERNEED-NEXT: } -VERNEED-NEXT: } diff --git a/llvm/test/tools/yaml2obj/verdef-section.yaml b/llvm/test/tools/yaml2obj/verdef-section.yaml index f81bcf1..deac6e7 100644 --- a/llvm/test/tools/yaml2obj/verdef-section.yaml +++ b/llvm/test/tools/yaml2obj/verdef-section.yaml @@ -1,5 +1,5 @@ # RUN: yaml2obj %s -o %t -# RUN: llvm-readelf -V %t | FileCheck %s +# RUN: llvm-readobj -V %t | FileCheck %s # Check we are able to handle the SHT_GNU_verdef sections. diff --git a/llvm/test/tools/yaml2obj/verneed-section.yaml b/llvm/test/tools/yaml2obj/verneed-section.yaml index 436e54b..2fc58ad 100644 --- a/llvm/test/tools/yaml2obj/verneed-section.yaml +++ b/llvm/test/tools/yaml2obj/verneed-section.yaml @@ -1,5 +1,5 @@ # RUN: yaml2obj %s -o %t -# RUN: llvm-readelf -V %t | FileCheck %s +# RUN: llvm-readobj -V %t | FileCheck %s # Check we are able to handle the SHT_GNU_verneed sections. diff --git a/llvm/test/tools/yaml2obj/versym-section.yaml b/llvm/test/tools/yaml2obj/versym-section.yaml index 31dfecf..3c08ddd 100644 --- a/llvm/test/tools/yaml2obj/versym-section.yaml +++ b/llvm/test/tools/yaml2obj/versym-section.yaml @@ -1,5 +1,5 @@ # RUN: yaml2obj %s -o %t -# RUN: llvm-readelf -V %t | FileCheck %s +# RUN: llvm-readobj -V %t | FileCheck %s ## Check we are able to produce a valid SHT_GNU_versym ## section from its description. diff --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp index 8c1c6fe..0914563 100644 --- a/llvm/tools/llvm-readobj/ELFDumper.cpp +++ b/llvm/tools/llvm-readobj/ELFDumper.cpp @@ -342,6 +342,12 @@ public: virtual void printProgramHeaders(const ELFFile *Obj, bool PrintProgramHeaders, cl::boolOrDefault PrintSectionMapping) = 0; + virtual void printVersionSymbolSection(const ELFFile *Obj, + const Elf_Shdr *Sec) = 0; + virtual void printVersionDefinitionSection(const ELFFile *Obj, + const Elf_Shdr *Sec) = 0; + virtual void printVersionDependencySection(const ELFFile *Obj, + const Elf_Shdr *Sec) = 0; virtual void printHashHistogram(const ELFFile *Obj) = 0; virtual void printCGProfile(const ELFFile *Obj) = 0; virtual void printAddrsig(const ELFFile *Obj) = 0; @@ -376,6 +382,12 @@ public: size_t Offset) override; void printProgramHeaders(const ELFO *Obj, bool PrintProgramHeaders, cl::boolOrDefault PrintSectionMapping) override; + void printVersionSymbolSection(const ELFFile *Obj, + const Elf_Shdr *Sec) override; + void printVersionDefinitionSection(const ELFFile *Obj, + const Elf_Shdr *Sec) override; + void printVersionDependencySection(const ELFFile *Obj, + const Elf_Shdr *Sec) override; void printHashHistogram(const ELFFile *Obj) override; void printCGProfile(const ELFFile *Obj) override; void printAddrsig(const ELFFile *Obj) override; @@ -470,6 +482,12 @@ public: void printDynamicRelocations(const ELFO *Obj) override; void printProgramHeaders(const ELFO *Obj, bool PrintProgramHeaders, cl::boolOrDefault PrintSectionMapping) override; + void printVersionSymbolSection(const ELFFile *Obj, + const Elf_Shdr *Sec) override; + void printVersionDefinitionSection(const ELFFile *Obj, + const Elf_Shdr *Sec) override; + void printVersionDependencySection(const ELFFile *Obj, + const Elf_Shdr *Sec) override; void printHashHistogram(const ELFFile *Obj) override; void printCGProfile(const ELFFile *Obj) override; void printAddrsig(const ELFFile *Obj) override; @@ -607,143 +625,6 @@ template void ELFDumper::LoadVersionMap() const { LoadVersionNeeds(dot_gnu_version_r_sec); } -template -static void printVersionSymbolSection(ELFDumper *Dumper, const ELFO *Obj, - const typename ELFO::Elf_Shdr *Sec, - ScopedPrinter &W) { - DictScope SS(W, "Version symbols"); - if (!Sec) - return; - StringRef Name = unwrapOrError(Obj->getSectionName(Sec)); - W.printNumber("Section Name", Name, Sec->sh_name); - W.printHex("Address", Sec->sh_addr); - W.printHex("Offset", Sec->sh_offset); - W.printNumber("Link", Sec->sh_link); - - const uint8_t *P = (const uint8_t *)Obj->base() + Sec->sh_offset; - StringRef StrTable = Dumper->getDynamicStringTable(); - - // Same number of entries in the dynamic symbol table (DT_SYMTAB). - ListScope Syms(W, "Symbols"); - for (const typename ELFO::Elf_Sym &Sym : Dumper->dynamic_symbols()) { - DictScope S(W, "Symbol"); - std::string FullSymbolName = - Dumper->getFullSymbolName(&Sym, StrTable, true /* IsDynamic */); - W.printNumber("Version", *P); - W.printString("Name", FullSymbolName); - P += sizeof(typename ELFO::Elf_Half); - } -} - -static const EnumEntry SymVersionFlags[] = { - {"Base", "BASE", VER_FLG_BASE}, - {"Weak", "WEAK", VER_FLG_WEAK}, - {"Info", "INFO", VER_FLG_INFO}}; - -template -static void printVersionDefinitionSection(ELFDumper *Dumper, - const ELFO *Obj, - const typename ELFO::Elf_Shdr *Sec, - ScopedPrinter &W) { - using VerDef = typename ELFO::Elf_Verdef; - using VerdAux = typename ELFO::Elf_Verdaux; - - DictScope SD(W, "SHT_GNU_verdef"); - if (!Sec) - return; - - const uint8_t *SecStartAddress = - (const uint8_t *)Obj->base() + Sec->sh_offset; - const uint8_t *SecEndAddress = SecStartAddress + Sec->sh_size; - const uint8_t *P = SecStartAddress; - const typename ELFO::Elf_Shdr *StrTab = - unwrapOrError(Obj->getSection(Sec->sh_link)); - - unsigned VerDefsNum = Sec->sh_info; - while (VerDefsNum--) { - if (P + sizeof(VerDef) > SecEndAddress) - report_fatal_error("invalid offset in the section"); - - auto *VD = reinterpret_cast(P); - DictScope Def(W, "Definition"); - W.printNumber("Version", VD->vd_version); - W.printEnum("Flags", VD->vd_flags, makeArrayRef(SymVersionFlags)); - W.printNumber("Index", VD->vd_ndx); - W.printNumber("Hash", VD->vd_hash); - W.printString("Name", - StringRef((const char *)(Obj->base() + StrTab->sh_offset + - VD->getAux()->vda_name))); - if (!VD->vd_cnt) - report_fatal_error("at least one definition string must exist"); - if (VD->vd_cnt > 2) - report_fatal_error("more than one predecessor is not expected"); - - if (VD->vd_cnt == 2) { - const uint8_t *PAux = P + VD->vd_aux + VD->getAux()->vda_next; - const VerdAux *Aux = reinterpret_cast(PAux); - W.printString("Predecessor", - StringRef((const char *)(Obj->base() + StrTab->sh_offset + - Aux->vda_name))); - } - - P += VD->vd_next; - } -} - -template -static void printVersionDependencySection(ELFDumper *Dumper, - const ELFO *Obj, - const typename ELFO::Elf_Shdr *Sec, - ScopedPrinter &W) { - using VerNeed = typename ELFO::Elf_Verneed; - using VernAux = typename ELFO::Elf_Vernaux; - - DictScope SD(W, "SHT_GNU_verneed"); - if (!Sec) - return; - - const uint8_t *SecData = (const uint8_t *)Obj->base() + Sec->sh_offset; - const typename ELFO::Elf_Shdr *StrTab = - unwrapOrError(Obj->getSection(Sec->sh_link)); - - const uint8_t *P = SecData; - unsigned VerNeedNum = Sec->sh_info; - for (unsigned I = 0; I < VerNeedNum; ++I) { - const VerNeed *Need = reinterpret_cast(P); - DictScope Entry(W, "Dependency"); - W.printNumber("Version", Need->vn_version); - W.printNumber("Count", Need->vn_cnt); - W.printString("FileName", - StringRef((const char *)(Obj->base() + StrTab->sh_offset + - Need->vn_file))); - - const uint8_t *PAux = P + Need->vn_aux; - for (unsigned J = 0; J < Need->vn_cnt; ++J) { - const VernAux *Aux = reinterpret_cast(PAux); - DictScope Entry(W, "Entry"); - W.printNumber("Hash", Aux->vna_hash); - W.printEnum("Flags", Aux->vna_flags, makeArrayRef(SymVersionFlags)); - W.printNumber("Index", Aux->vna_other); - W.printString("Name", - StringRef((const char *)(Obj->base() + StrTab->sh_offset + - Aux->vna_name))); - PAux += Aux->vna_next; - } - P += Need->vn_next; - } -} - -template void ELFDumper::printVersionInfo() { - // Dump version symbol section. - printVersionSymbolSection(this, ObjF->getELFFile(), dot_gnu_version_sec, W); - - // Dump version definition section. - printVersionDefinitionSection(this, ObjF->getELFFile(), dot_gnu_version_d_sec, W); - - // Dump version dependency section. - printVersionDependencySection(this, ObjF->getELFFile(), dot_gnu_version_r_sec, W); -} - template StringRef ELFDumper::getSymbolVersion(StringRef StrTab, const Elf_Sym *symb, @@ -925,6 +806,11 @@ static const EnumEntry ElfOSABI[] = { {"Standalone", "Standalone App", ELF::ELFOSABI_STANDALONE} }; +static const EnumEntry SymVersionFlags[] = { + {"Base", "BASE", VER_FLG_BASE}, + {"Weak", "WEAK", VER_FLG_WEAK}, + {"Info", "INFO", VER_FLG_INFO}}; + static const EnumEntry AMDGPUElfOSABI[] = { {"AMDGPU_HSA", "AMDGPU - HSA", ELF::ELFOSABI_AMDGPU_HSA}, {"AMDGPU_PAL", "AMDGPU - PAL", ELF::ELFOSABI_AMDGPU_PAL}, @@ -1616,6 +1502,20 @@ void ELFDumper::printProgramHeaders( PrintSectionMapping); } +template void ELFDumper::printVersionInfo() { + // Dump version symbol section. + ELFDumperStyle->printVersionSymbolSection(ObjF->getELFFile(), + dot_gnu_version_sec); + + // Dump version definition section. + ELFDumperStyle->printVersionDefinitionSection(ObjF->getELFFile(), + dot_gnu_version_d_sec); + + // Dump version dependency section. + ELFDumperStyle->printVersionDependencySection(ObjF->getELFFile(), + dot_gnu_version_r_sec); +} + template void ELFDumper::printDynamicRelocations() { ELFDumperStyle->printDynamicRelocations(ObjF->getELFFile()); } @@ -3460,6 +3360,36 @@ void GNUStyle::printDynamicRelocations(const ELFO *Obj) { } } +template +void GNUStyle::printVersionSymbolSection(const ELFFile *Obj, + const Elf_Shdr *Sec) { + if (!Sec) + return; + + StringRef SecName = unwrapOrError(Obj->getSectionName(Sec)); + OS << "Dumper for " << SecName << " is not implemented\n"; +} + +template +void GNUStyle::printVersionDefinitionSection(const ELFFile *Obj, + const Elf_Shdr *Sec) { + if (!Sec) + return; + + StringRef SecName = unwrapOrError(Obj->getSectionName(Sec)); + OS << "Dumper for " << SecName << " is not implemented\n"; +} + +template +void GNUStyle::printVersionDependencySection(const ELFFile *Obj, + const Elf_Shdr *Sec) { + if (!Sec) + return; + + StringRef SecName = unwrapOrError(Obj->getSectionName(Sec)); + OS << "Dumper for " << SecName << " is not implemented\n"; +} + // Hash histogram shows statistics of how efficient the hash was for the // dynamic symbol table. The table shows number of hash buckets for different // lengths of chains as absolute number and percentage of the total buckets. @@ -4539,6 +4469,122 @@ void LLVMStyle::printProgramHeaders(const ELFO *Obj) { } template +void LLVMStyle::printVersionSymbolSection(const ELFFile *Obj, + const Elf_Shdr *Sec) { + DictScope SS(W, "Version symbols"); + if (!Sec) + return; + + StringRef SecName = unwrapOrError(Obj->getSectionName(Sec)); + W.printNumber("Section Name", SecName, Sec->sh_name); + W.printHex("Address", Sec->sh_addr); + W.printHex("Offset", Sec->sh_offset); + W.printNumber("Link", Sec->sh_link); + + const uint8_t *VersymBuf = (const uint8_t *)Obj->base() + Sec->sh_offset; + const ELFDumper *Dumper = this->dumper(); + StringRef StrTable = Dumper->getDynamicStringTable(); + + // Same number of entries in the dynamic symbol table (DT_SYMTAB). + ListScope Syms(W, "Symbols"); + for (const Elf_Sym &Sym : Dumper->dynamic_symbols()) { + DictScope S(W, "Symbol"); + const Elf_Versym *Versym = reinterpret_cast(VersymBuf); + std::string FullSymbolName = + Dumper->getFullSymbolName(&Sym, StrTable, true /* IsDynamic */); + W.printNumber("Version", Versym->vs_index & VERSYM_VERSION); + W.printString("Name", FullSymbolName); + VersymBuf += sizeof(Elf_Versym); + } +} + +template +void LLVMStyle::printVersionDefinitionSection(const ELFFile *Obj, + const Elf_Shdr *Sec) { + DictScope SD(W, "SHT_GNU_verdef"); + if (!Sec) + return; + + const uint8_t *SecStartAddress = + (const uint8_t *)Obj->base() + Sec->sh_offset; + const uint8_t *SecEndAddress = SecStartAddress + Sec->sh_size; + const uint8_t *VerdefBuf = SecStartAddress; + const Elf_Shdr *StrTab = unwrapOrError(Obj->getSection(Sec->sh_link)); + + unsigned VerDefsNum = Sec->sh_info; + while (VerDefsNum--) { + if (VerdefBuf + sizeof(Elf_Verdef) > SecEndAddress) + // FIXME: report_fatal_error is not a good way to report error. We should + // emit a parsing error here and below. + report_fatal_error("invalid offset in the section"); + + const Elf_Verdef *Verdef = reinterpret_cast(VerdefBuf); + DictScope Def(W, "Definition"); + W.printNumber("Version", Verdef->vd_version); + W.printEnum("Flags", Verdef->vd_flags, makeArrayRef(SymVersionFlags)); + W.printNumber("Index", Verdef->vd_ndx); + W.printNumber("Hash", Verdef->vd_hash); + W.printString("Name", + StringRef((const char *)(Obj->base() + StrTab->sh_offset + + Verdef->getAux()->vda_name))); + if (!Verdef->vd_cnt) + report_fatal_error("at least one definition string must exist"); + if (Verdef->vd_cnt > 2) + report_fatal_error("more than one predecessor is not expected"); + + if (Verdef->vd_cnt == 2) { + const uint8_t *VerdauxBuf = + VerdefBuf + Verdef->vd_aux + Verdef->getAux()->vda_next; + const Elf_Verdaux *Verdaux = + reinterpret_cast(VerdauxBuf); + W.printString("Predecessor", + StringRef((const char *)(Obj->base() + StrTab->sh_offset + + Verdaux->vda_name))); + } + VerdefBuf += Verdef->vd_next; + } +} + +template +void LLVMStyle::printVersionDependencySection(const ELFFile *Obj, + const Elf_Shdr *Sec) { + DictScope SD(W, "SHT_GNU_verneed"); + if (!Sec) + return; + + const uint8_t *SecData = (const uint8_t *)Obj->base() + Sec->sh_offset; + const Elf_Shdr *StrTab = unwrapOrError(Obj->getSection(Sec->sh_link)); + + const uint8_t *VerneedBuf = SecData; + unsigned VerneedNum = Sec->sh_info; + for (unsigned I = 0; I < VerneedNum; ++I) { + const Elf_Verneed *Verneed = + reinterpret_cast(VerneedBuf); + DictScope Entry(W, "Dependency"); + W.printNumber("Version", Verneed->vn_version); + W.printNumber("Count", Verneed->vn_cnt); + W.printString("FileName", + StringRef((const char *)(Obj->base() + StrTab->sh_offset + + Verneed->vn_file))); + + const uint8_t *VernauxBuf = VerneedBuf + Verneed->vn_aux; + for (unsigned J = 0; J < Verneed->vn_cnt; ++J) { + const Elf_Vernaux *Vernaux = + reinterpret_cast(VernauxBuf); + DictScope Entry(W, "Entry"); + W.printNumber("Hash", Vernaux->vna_hash); + W.printEnum("Flags", Vernaux->vna_flags, makeArrayRef(SymVersionFlags)); + W.printNumber("Index", Vernaux->vna_other); + W.printString("Name", + StringRef((const char *)(Obj->base() + StrTab->sh_offset + + Vernaux->vna_name))); + VernauxBuf += Vernaux->vna_next; + } + VerneedBuf += Verneed->vn_next; + } +} + +template void LLVMStyle::printHashHistogram(const ELFFile *Obj) { W.startLine() << "Hash Histogram not implemented!\n"; }