Index: llvm/test/tools/llvm-readobj/elf-versioninfo.test =================================================================== --- llvm/test/tools/llvm-readobj/elf-versioninfo.test +++ llvm/test/tools/llvm-readobj/elf-versioninfo.test @@ -193,7 +193,7 @@ # LLVM-NEXT: Index: 3 # LLVM-NEXT: Hash: 175630258 # LLVM-NEXT: Name: VERSION2 -# LLVM-NEXT: Predecessor: VERSION1 +# LLVM-NEXT: Predecessors: [VERSION1] # LLVM-NEXT: } # LLVM-NEXT: ] # LLVM-NEXT: VersionRequirements [ @@ -286,3 +286,211 @@ # GNU-NEXT: 0x0050: Name: v2 Flags: Version: 5 # GNU-NEXT: 0x0060: Version: 1 File: verneed2.so.0 Cnt: 1 # GNU-NEXT: 0x0070: Name: v3 Flags: none Version: 6 + +## Check that we report a warning when sh_link references a non-existent section. + +# RUN: yaml2obj %s --docnum=2 -o %t2 +# RUN: llvm-readobj -V %t2 2>&1 | FileCheck %s --check-prefix=INVALID-LINK-LLVM -DFILE=%t2 +# RUN: not llvm-readelf -V %t2 2>&1 | FileCheck %s --check-prefix=INVALID-LINK-GNU -DFILE=%t2 + +# INVALID-LINK-LLVM: warning: '[[FILE]]': SHT_GNU_verdef section: can't get a string table linked: invalid section index: 255 + +## TODO: llvm-readelf also should report a meaningful warning instead of an error. +# INVALID-LINK-GNU: Version definition +# INVALID-LINK-GNU: error: '[[FILE]]': invalid section index: 255 + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_DYN + Machine: EM_X86_64 +Sections: + - Name: .gnu.version_d + Type: SHT_GNU_verdef + Link: 0xFF + Info: 0x0 + Entries: [] + +## Check that we report a warning when the sh_link field of a SHT_GNU_verdef section references a non-string table section. + +# RUN: yaml2obj %s --docnum=3 -o %t3 +# RUN: llvm-readobj -V %t3 2>&1 | FileCheck %s --check-prefix=INVALID-STRING-TABLE -DFILE=%t3 +# RUN: llvm-readelf -V %t3 2>&1 | FileCheck %s --check-prefix=INVALID-STRING-TABLE -DFILE=%t3 + +# INVALID-STRING-TABLE: warning: '[[FILE]]': SHT_GNU_verdef section: can't use the string table linked: invalid sh_type for string table section [index 0]: expected SHT_STRTAB, but got SHT_NULL + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_DYN + Machine: EM_X86_64 +Sections: + - Name: .gnu.version_d + Type: SHT_GNU_verdef + Link: 0x0 + Info: 0x0 + Entries: [] + +## Check that we report a warning when a SHT_GNU_verdef section is linked with a non-string table section. + +# RUN: yaml2obj %s --docnum=4 -o %t4 +# RUN: llvm-readobj -V %t4 2>&1 | FileCheck %s --check-prefix=INVALID-DATA -DFILE=%t4 +# RUN: llvm-readelf -V %t4 2>&1 | FileCheck %s --check-prefix=INVALID-DATA -DFILE=%t4 + +# INVALID-DATA: warning: '[[FILE]]': SHT_GNU_verdef section: can't get content: section [index 1] has a sh_offset (0xffffffff) + sh_size (0x0) that cannot be represented + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_DYN + Machine: EM_X86_64 +Sections: + - Name: .gnu.version_d + Type: SHT_GNU_verdef + Link: .dynstr + Info: 0x0 + Entries: [] + ShOffset: 0xFFFFFFFF +DynamicSymbols: + - Name: foo + +## Check that we report a warning when a SHT_GNU_verdef section contains a version definition +## that goes past the end of the section. + +# RUN: yaml2obj %s --docnum=5 -o %t5 +# RUN: llvm-readobj -V %t5 2>&1 | FileCheck %s --check-prefix=DEF-PAST-END -DFILE=%t5 +# RUN: llvm-readelf -V %t5 2>&1 | FileCheck %s --check-prefix=DEF-PAST-END -DFILE=%t5 + +# DEF-PAST-END: warning: '[[FILE]]': SHT_GNU_verdef section: version definition 1 goes past the end of the section + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_DYN + Machine: EM_X86_64 +Sections: + - Name: .gnu.version_d + Type: SHT_GNU_verdef + Link: .dynstr + Info: 0x1 + Entries: + - Version: 0 + Flags: 0 + VersionNdx: 0 + Hash: 0 + Names: + - FOO + ShSize: 1 +DynamicSymbols: + - Name: foo + +## Check that we report a warning when a SHT_GNU_verdef section contains a version definition +## that refers to an auxiliary entry that goes past the end of the section. + +# RUN: yaml2obj %s --docnum=6 -o %t6 +# RUN: llvm-readobj -V %t6 2>&1 | FileCheck %s --check-prefix=AUX-PAST-END -DFILE=%t6 +# RUN: llvm-readelf -V %t6 2>&1 | FileCheck %s --check-prefix=AUX-PAST-END -DFILE=%t6 + +# AUX-PAST-END: warning: '[[FILE]]': SHT_GNU_verdef section: a version definition 1 refers to an auxiliary entry that goes past the end of the section + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_DYN + Machine: EM_X86_64 +Sections: + - Name: .gnu.version_d + Type: SHT_GNU_verdef + Link: .dynstr + Info: 0x1 + Entries: + - Version: 0 + Flags: 0 + VersionNdx: 0 + Hash: 0 + Names: + - FOO + ShSize: 21 +DynamicSymbols: + - Name: foo + +## Check that we can dump a SHT_GNU_verdef section properly even if it contains version names strings +## that overruns the string table linked. + +# RUN: yaml2obj %s --docnum=7 -o %t7 +# RUN: llvm-readobj -V %t7 2>&1 | FileCheck %s --check-prefix=PAST-STRTAB-END-LLVM -DFILE=%t7 +# RUN: llvm-readelf -V %t7 2>&1 | FileCheck %s --check-prefix=PAST-STRTAB-END-GNU -DFILE=%t7 + +# PAST-STRTAB-END-LLVM: VersionDefinitions [ +# PAST-STRTAB-END-LLVM-NEXT: Definition { +# PAST-STRTAB-END-LLVM-NEXT: Version: 0 +# PAST-STRTAB-END-LLVM-NEXT: Flags [ (0x0) +# PAST-STRTAB-END-LLVM-NEXT: ] +# PAST-STRTAB-END-LLVM-NEXT: Index: 0 +# PAST-STRTAB-END-LLVM-NEXT: Hash: 0 +# PAST-STRTAB-END-LLVM-NEXT: Name: +# PAST-STRTAB-END-LLVM-NEXT: } +# PAST-STRTAB-END-LLVM-NEXT: ] + +# PAST-STRTAB-END-GNU: Version definition section '.gnu.version_d' contains 1 entries: +# PAST-STRTAB-END-GNU-NEXT: Addr: 0000000000000000 Offset: 0x000040 Link: 2 (.strtab) +# PAST-STRTAB-END-GNU-NEXT: 0x0000: Rev: 0 Flags: none Index: 0 Cnt: 1 Name: + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_DYN + Machine: EM_X86_64 +Sections: + - Name: .gnu.version_d + Type: SHT_GNU_verdef + Link: .strtab + Info: 0x1 + Entries: + - Version: 0 + Flags: 0 + VersionNdx: 0 + Hash: 0 + Names: + - FOO + - Name: .strtab + Type: SHT_STRTAB +DynamicSymbols: + - Name: BAR + +## Check we report a warning when a version definition is not correctly aligned in memory. + +# RUN: yaml2obj %s --docnum=8 -o %t8 +# RUN: llvm-readobj -V %t8 2>&1 | FileCheck %s --check-prefix=MISALIGNED-DEF -DFILE=%t8 +# RUN: llvm-readelf -V %t8 2>&1 | FileCheck %s --check-prefix=MISALIGNED-DEF -DFILE=%t8 + +# MISALIGNED-DEF: warning: '[[FILE]]': SHT_GNU_verdef section: found a misaligned version definition entry + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_DYN + Machine: EM_X86_64 +Sections: + - Type: Fill + Size: 0x1 + - Name: .gnu.version_d + Type: SHT_GNU_verdef + Link: .dynstr + Info: 0x1 + Entries: + - Version: 0 + Flags: 0 + VersionNdx: 0 + Hash: 0 + Names: + - FOO +DynamicSymbols: + - Name: foo Index: llvm/test/tools/yaml2obj/ELF/verdef-section.yaml =================================================================== --- llvm/test/tools/yaml2obj/ELF/verdef-section.yaml +++ llvm/test/tools/yaml2obj/ELF/verdef-section.yaml @@ -31,7 +31,7 @@ # CHECK-NEXT: Index: 3 # CHECK-NEXT: Hash: 108387922 # CHECK-NEXT: Name: VERSION_2 -# CHECK-NEXT: Predecessor: VERSION_3 +# CHECK-NEXT: Predecessors: [VERSION_3] # CHECK-NEXT: } # CHECK-NEXT: ] Index: llvm/tools/llvm-readobj/ELFDumper.cpp =================================================================== --- llvm/tools/llvm-readobj/ELFDumper.cpp +++ llvm/tools/llvm-readobj/ELFDumper.cpp @@ -364,6 +364,24 @@ NonVisibilityBitsUsed); } +namespace { +struct VerdAux { + unsigned Offset; + std::string Name; +}; + +struct VerDef { + unsigned Offset; + unsigned Version; + unsigned Flags; + unsigned Ndx; + unsigned Cnt; + unsigned Hash; + std::string Name; + std::vector AuxV; +}; +} // namespace + template class MipsGOTParser; template class DumpStyle { @@ -371,6 +389,8 @@ using Elf_Shdr = typename ELFT::Shdr; using Elf_Sym = typename ELFT::Sym; using Elf_Addr = typename ELFT::Addr; + using Elf_Verdef = typename ELFT::Verdef; + using Elf_Verdaux = typename ELFT::Verdaux; DumpStyle(ELFDumper *Dumper) : Dumper(Dumper) { FileName = this->Dumper->getElfObject()->getFileName(); @@ -403,6 +423,10 @@ virtual void printProgramHeaders(const ELFFile *Obj, bool PrintProgramHeaders, cl::boolOrDefault PrintSectionMapping) = 0; + + Expected> getVersionDefinitions(const ELFFile *Obj, + const Elf_Shdr *Sec); + virtual void printVersionSymbolSection(const ELFFile *Obj, const Elf_Shdr *Sec) = 0; virtual void printVersionDefinitionSection(const ELFFile *Obj, @@ -3895,48 +3919,119 @@ return Ret; } +template +Expected> +DumpStyle::getVersionDefinitions(const ELFFile *Obj, + const Elf_Shdr *Sec) { + Expected StrTabSecOrErr = Obj->getSection(Sec->sh_link); + if (!StrTabSecOrErr) + return createError( + "SHT_GNU_verdef section: can't get a string table linked: " + + toString(StrTabSecOrErr.takeError())); + + Expected StrTabOrErr = Obj->getStringTable(*StrTabSecOrErr); + if (!StrTabOrErr) + return createError( + "SHT_GNU_verdef section: can't use the string table linked: " + + toString(StrTabOrErr.takeError())); + + Expected> ContentsOrErr = Obj->getSectionContents(Sec); + if (!ContentsOrErr) + return createError("SHT_GNU_verdef section: can't get content: " + + toString(ContentsOrErr.takeError())); + + const uint8_t *Start = ContentsOrErr->data(); + const uint8_t *End = Start + ContentsOrErr->size(); + + auto ExtractNextAux = [&](const uint8_t *&VerdauxBuf, + unsigned VerDefNdx) -> Expected { + if (VerdauxBuf + sizeof(Elf_Verdaux) > End) + return createError("SHT_GNU_verdef section: a version definition " + + Twine(VerDefNdx) + + " refers to an auxiliary entry that goes past the end " + "of the section"); + + auto *Verdaux = reinterpret_cast(VerdauxBuf); + VerdauxBuf += Verdaux->vda_next; + + VerdAux Aux; + Aux.Offset = VerdauxBuf - Start; + if (Verdaux->vda_name <= StrTabOrErr->size()) + Aux.Name = StrTabOrErr->drop_front(Verdaux->vda_name); + else + Aux.Name = "vda_name) + ">"; + return Aux; + }; + + std::vector Ret; + const uint8_t *VerdefBuf = Start; + for (unsigned I = 1; I <= /*VerDefsNum=*/Sec->sh_info; ++I) { + if (VerdefBuf + sizeof(Elf_Verdef) > End) + return createError("SHT_GNU_verdef section: version definition " + + Twine(I) + " goes past the end of the section"); + + if (uintptr_t(VerdefBuf) % sizeof(uint32_t) != 0) + return createError("SHT_GNU_verdef section: found a misaligned version " + "definition entry"); + + const Elf_Verdef *D = reinterpret_cast(VerdefBuf); + VerDef &VD = *Ret.emplace(Ret.end()); + VD.Offset = VerdefBuf - Start; + VD.Version = D->vd_version; + VD.Flags = D->vd_flags; + VD.Ndx = D->vd_ndx; + VD.Cnt = D->vd_cnt; + VD.Hash = D->vd_hash; + + const uint8_t *VerdauxBuf = VerdefBuf + D->vd_aux; + for (unsigned J = 0; J < D->vd_cnt; ++J) { + // TODO: this error is untested. + if (uintptr_t(VerdauxBuf) % sizeof(uint32_t) != 0) + return createError( + "SHT_GNU_verdef section: found a misaligned auxiliary entry"); + + Expected AuxOrErr = ExtractNextAux(VerdauxBuf, I); + if (!AuxOrErr) + return AuxOrErr.takeError(); + + if (J == 0) + VD.Name = AuxOrErr->Name; + else + VD.AuxV.push_back(*AuxOrErr); + } + + VerdefBuf += D->vd_next; + } + + return Ret; +} + template void GNUStyle::printVersionDefinitionSection(const ELFFile *Obj, const Elf_Shdr *Sec) { if (!Sec) return; - unsigned VerDefsNum = Sec->sh_info; - printGNUVersionSectionProlog(OS, "Version definition", VerDefsNum, Obj, Sec, + printGNUVersionSectionProlog(OS, "Version definition", Sec->sh_info, Obj, Sec, this->FileName); - const Elf_Shdr *StrTabSec = - unwrapOrError(this->FileName, Obj->getSection(Sec->sh_link)); - StringRef StringTable( - reinterpret_cast(Obj->base() + StrTabSec->sh_offset), - (size_t)StrTabSec->sh_size); - - const uint8_t *VerdefBuf = - unwrapOrError(this->FileName, Obj->getSectionContents(Sec)).data(); - const uint8_t *Begin = VerdefBuf; - - while (VerDefsNum--) { - const Elf_Verdef *Verdef = reinterpret_cast(VerdefBuf); - OS << format(" 0x%04x: Rev: %u Flags: %s Index: %u Cnt: %u", - VerdefBuf - Begin, (unsigned)Verdef->vd_version, - versionFlagToString(Verdef->vd_flags).c_str(), - (unsigned)Verdef->vd_ndx, (unsigned)Verdef->vd_cnt); - - const uint8_t *VerdauxBuf = VerdefBuf + Verdef->vd_aux; - const Elf_Verdaux *Verdaux = - reinterpret_cast(VerdauxBuf); - OS << format(" Name: %s\n", - StringTable.drop_front(Verdaux->vda_name).data()); - - for (unsigned I = 1; I < Verdef->vd_cnt; ++I) { - VerdauxBuf += Verdaux->vda_next; - Verdaux = reinterpret_cast(VerdauxBuf); - OS << format(" 0x%04x: Parent %u: %s\n", VerdauxBuf - Begin, I, - StringTable.drop_front(Verdaux->vda_name).data()); - } + Expected> V = this->getVersionDefinitions(Obj, Sec); + if (!V) { + reportWarning(V.takeError(), this->FileName); + return; + } - VerdefBuf += Verdef->vd_next; + for (const VerDef &Def : *V) { + OS << format(" 0x%04x: Rev: %u Flags: %s Index: %u Cnt: %u Name: %s\n", + Def.Offset, Def.Version, + versionFlagToString(Def.Flags).c_str(), Def.Ndx, Def.Cnt, + Def.Name.data()); + unsigned I = 0; + for (const VerdAux &Aux : Def.AuxV) + OS << format(" 0x%04x: Parent %u: %s\n", Aux.Offset, ++I, + Aux.Name.data()); } + OS << '\n'; } @@ -5713,44 +5808,25 @@ if (!Sec) return; - const uint8_t *SecStartAddress = - reinterpret_cast(Obj->base() + Sec->sh_offset); - const uint8_t *SecEndAddress = SecStartAddress + Sec->sh_size; - const uint8_t *VerdefBuf = SecStartAddress; - const Elf_Shdr *StrTab = - unwrapOrError(this->FileName, 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"); + Expected> V = this->getVersionDefinitions(Obj, Sec); + if (!V) { + reportWarning(V.takeError(), this->FileName); + return; + } - const Elf_Verdef *Verdef = reinterpret_cast(VerdefBuf); + for (const VerDef &D : *V) { DictScope Def(W, "Definition"); - W.printNumber("Version", Verdef->vd_version); - W.printFlags("Flags", Verdef->vd_flags, makeArrayRef(SymVersionFlags)); - W.printNumber("Index", Verdef->vd_ndx); - W.printNumber("Hash", Verdef->vd_hash); - W.printString("Name", StringRef(reinterpret_cast( - 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(reinterpret_cast( - Obj->base() + StrTab->sh_offset + Verdaux->vda_name))); - } - VerdefBuf += Verdef->vd_next; + W.printNumber("Version", D.Version); + W.printFlags("Flags", D.Flags, makeArrayRef(SymVersionFlags)); + W.printNumber("Index", D.Ndx); + W.printNumber("Hash", D.Hash); + W.printString("Name", D.Name.c_str()); + + if (D.AuxV.empty()) + continue; + W.printList( + "Predecessors", D.AuxV, + [](raw_ostream &OS, const VerdAux &Aux) { OS << Aux.Name.c_str(); }); } }