1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-23 11:13:28 +01:00

[llvm-readobj/llvm-readelf] - Reimplement dumping of the SHT_GNU_verdef section.

Currently we have following issues:
1) We have 2 different implementations with a different behaviors for GNU/LLVM styles.
2) Errors are either not handled at all or we call report_fatal_error with not helpfull messages.
3) There is no test coverage even for those errors that are reported.

This patch reimplements parsing of the SHT_GNU_verdef section entries
in a single place, adds a few error messages and test coverage.

Differential revision: https://reviews.llvm.org/D70495
This commit is contained in:
Georgii Rymar 2019-11-20 17:37:56 +03:00
parent b0d22e3ae4
commit 39d296957d
3 changed files with 390 additions and 73 deletions

View File

@ -63,6 +63,7 @@ Sections:
Names:
- VERSION2
- VERSION1
- VERSION3
- Name: .gnu.version_r
Type: SHT_GNU_verneed
Flags: [ SHF_ALLOC ]
@ -193,7 +194,7 @@ DynamicSymbols:
# LLVM-NEXT: Index: 3
# LLVM-NEXT: Hash: 175630258
# LLVM-NEXT: Name: VERSION2
# LLVM-NEXT: Predecessor: VERSION1
# LLVM-NEXT: Predecessors: [VERSION1, VERSION3]
# LLVM-NEXT: }
# LLVM-NEXT: ]
# LLVM-NEXT: VersionRequirements [
@ -273,11 +274,12 @@ DynamicSymbols:
# GNU-NEXT: 0x0038: Rev: 1 Flags: WEAK Index: 0 Cnt: 1 Name: VERSION1
# GNU-NEXT: 0x0054: Rev: 1 Flags: INFO Index: 0 Cnt: 1 Name: VERSION1
# GNU-NEXT: 0x0070: Rev: 1 Flags: BASE | WEAK | INFO Index: 2 Cnt: 1 Name: VERSION1
# GNU-NEXT: 0x008c: Rev: 1 Flags: <unknown> Index: 3 Cnt: 2 Name: VERSION2
# GNU-NEXT: 0x00a8: Parent 1: VERSION1
# GNU-NEXT: 0x008c: Rev: 1 Flags: <unknown> Index: 3 Cnt: 3 Name: VERSION2
# GNU-NEXT: 0x00b0: Parent 1: VERSION1
# GNU-NEXT: 0x00b0: Parent 2: VERSION3
# GNU-EMPTY:
# GNU-NEXT: Version needs section '.gnu.version_r' contains 2 entries:
# GNU-NEXT: Addr: 0000000000000000 Offset: 0x0000fc Link: 7 (.dynstr)
# GNU-NEXT: Addr: 0000000000000000 Offset: 0x000104 Link: 7 (.dynstr)
# GNU-NEXT: 0x0000: Version: 1 File: verneed1.so.0 Cnt: 5
# GNU-NEXT: 0x0010: Name: v1 Flags: BASE Version: 0
# GNU-NEXT: 0x0020: Name: v1 Flags: WEAK Version: 0
@ -286,3 +288,237 @@ DynamicSymbols:
# GNU-NEXT: 0x0050: Name: v2 Flags: <unknown> 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]]': invalid section linked to SHT_GNU_verdef section with index 1: invalid section index: 255
## TODO: llvm-readelf should also 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]]': invalid string table linked to SHT_GNU_verdef section with index 1: 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 we can't read the content of the SHT_GNU_verdef 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]]': cannot read content of SHT_GNU_verdef section with index 1: 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]]': invalid SHT_GNU_verdef section with index 1: 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]]': invalid SHT_GNU_verdef section with index 1: 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 overrun the linked string table.
# RUN: yaml2obj %s --docnum=7 -o %t7
# RUN: llvm-readobj -V %t7 2>&1 | FileCheck %s --check-prefix=PAST-STRTAB-END-LLVM --implicit-check-not="warning:" -DFILE=%t7
# RUN: llvm-readelf -V %t7 2>&1 | FileCheck %s --check-prefix=PAST-STRTAB-END-GNU --implicit-check-not="warning:" -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: <invalid vda_name: 5>
# 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: <invalid vda_name: 5>
--- !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]]': invalid SHT_GNU_verdef section with index 1: found a misaligned version definition entry at offset 0x0
--- !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
## Check we report a warning when an auxiliary entry is not correctly aligned in memory.
# RUN: yaml2obj %s --docnum=9 -o %t9
# RUN: llvm-readobj -V %t9 2>&1 | FileCheck %s --check-prefix=MISALIGNED-AUX -DFILE=%t9
# RUN: llvm-readelf -V %t9 2>&1 | FileCheck %s --check-prefix=MISALIGNED-AUX -DFILE=%t9
# MISALIGNED-AUX: warning: '[[FILE]]': invalid SHT_GNU_verdef section with index 1: found a misaligned auxiliary entry at offset 0x13
--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_DYN
Machine: EM_X86_64
Sections:
- Name: .gnu.version_d
Type: SHT_GNU_verdef
Flags: [ SHF_ALLOC ]
Link: .dynstr
Info: 0x1
## The byte offset to the auxiliary entry is 0x13, i.e. it is not correctly aligned in memory.
Content: "0000000000000100000000001300000000000000"
DynamicSymbols:
- Name: foo
Binding: STB_GLOBAL

View File

@ -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: ]

View File

@ -151,6 +151,24 @@ struct DynRegionInfo {
}
};
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<VerdAux> AuxV;
};
} // namespace
template <typename ELFT> class ELFDumper : public ObjDumper {
public:
ELFDumper(const object::ELFObjectFile<ELFT> *ObjF, ScopedPrinter &Writer);
@ -324,8 +342,106 @@ public:
const DynRegionInfo &getDynamicTableRegion() const { return DynamicTable; }
const Elf_Hash *getHashTable() const { return HashTable; }
const Elf_GnuHash *getGnuHashTable() const { return GnuHashTable; }
Expected<std::vector<VerDef>>
getVersionDefinitions(const Elf_Shdr *Sec) const;
};
template <class ELFT>
Expected<std::vector<VerDef>>
ELFDumper<ELFT>::getVersionDefinitions(const Elf_Shdr *Sec) const {
const ELFFile<ELFT> *Obj = ObjF->getELFFile();
unsigned SecNdx = Sec - &cantFail(Obj->sections()).front();
Expected<const Elf_Shdr *> StrTabSecOrErr = Obj->getSection(Sec->sh_link);
if (!StrTabSecOrErr)
return createError(
"invalid section linked to SHT_GNU_verdef section with index " +
Twine(SecNdx) + ": " + toString(StrTabSecOrErr.takeError()));
Expected<StringRef> StrTabOrErr = Obj->getStringTable(*StrTabSecOrErr);
if (!StrTabOrErr)
return createError(
"invalid string table linked to SHT_GNU_verdef section with index " +
Twine(SecNdx) + ": " + toString(StrTabOrErr.takeError()));
Expected<ArrayRef<uint8_t>> ContentsOrErr = Obj->getSectionContents(Sec);
if (!ContentsOrErr)
return createError(
"cannot read content of SHT_GNU_verdef section with index " +
Twine(SecNdx) + ": " + 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<VerdAux> {
if (VerdauxBuf + sizeof(Elf_Verdaux) > End)
return createError("invalid SHT_GNU_verdef section with index " +
Twine(SecNdx) + ": version definition " +
Twine(VerDefNdx) +
" refers to an auxiliary entry that goes past the end "
"of the section");
auto *Verdaux = reinterpret_cast<const Elf_Verdaux *>(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 = "<invalid vda_name: " + to_string(Verdaux->vda_name) + ">";
return Aux;
};
std::vector<VerDef> Ret;
const uint8_t *VerdefBuf = Start;
for (unsigned I = 1; I <= /*VerDefsNum=*/Sec->sh_info; ++I) {
if (VerdefBuf + sizeof(Elf_Verdef) > End)
return createError("invalid SHT_GNU_verdef section with index " +
Twine(SecNdx) + ": version definition " + Twine(I) +
" goes past the end of the section");
if (uintptr_t(VerdefBuf) % sizeof(uint32_t) != 0)
return createError(
"invalid SHT_GNU_verdef section with index " + Twine(SecNdx) +
": found a misaligned version definition entry at offset 0x" +
Twine::utohexstr(VerdefBuf - Start));
const Elf_Verdef *D = reinterpret_cast<const Elf_Verdef *>(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) {
if (uintptr_t(VerdauxBuf) % sizeof(uint32_t) != 0)
return createError("invalid SHT_GNU_verdef section with index " +
Twine(SecNdx) +
": found a misaligned auxiliary entry at offset 0x" +
Twine::utohexstr(VerdauxBuf - Start));
Expected<VerdAux> 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 <class ELFT>
void ELFDumper<ELFT>::printSymbolsHelper(bool IsDynamic) const {
StringRef StrTable, SymtabName;
@ -3901,42 +4017,26 @@ void GNUStyle<ELFT>::printVersionDefinitionSection(const ELFFile<ELFT> *Obj,
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<const char *>(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<const Elf_Verdef *>(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<const Elf_Verdaux *>(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<const Elf_Verdaux *>(VerdauxBuf);
OS << format(" 0x%04x: Parent %u: %s\n", VerdauxBuf - Begin, I,
StringTable.drop_front(Verdaux->vda_name).data());
}
VerdefBuf += Verdef->vd_next;
Expected<std::vector<VerDef>> V = this->dumper()->getVersionDefinitions(Sec);
if (!V) {
this->reportUniqueWarning(V.takeError());
return;
}
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 +5813,25 @@ void LLVMStyle<ELFT>::printVersionDefinitionSection(const ELFFile<ELFT> *Obj,
if (!Sec)
return;
const uint8_t *SecStartAddress =
reinterpret_cast<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(this->FileName, Obj->getSection(Sec->sh_link));
Expected<std::vector<VerDef>> V = this->dumper()->getVersionDefinitions(Sec);
if (!V) {
this->reportUniqueWarning(V.takeError());
return;
}
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<const Elf_Verdef *>(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<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");
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 (Verdef->vd_cnt == 2) {
const uint8_t *VerdauxBuf =
VerdefBuf + Verdef->vd_aux + Verdef->getAux()->vda_next;
const Elf_Verdaux *Verdaux =
reinterpret_cast<const Elf_Verdaux *>(VerdauxBuf);
W.printString("Predecessor",
StringRef(reinterpret_cast<const char *>(
Obj->base() + StrTab->sh_offset + Verdaux->vda_name)));
}
VerdefBuf += Verdef->vd_next;
if (D.AuxV.empty())
continue;
W.printList(
"Predecessors", D.AuxV,
[](raw_ostream &OS, const VerdAux &Aux) { OS << Aux.Name.c_str(); });
}
}