Index: test/tools/llvm-readobj/elf-invalid-versioning.test =================================================================== --- test/tools/llvm-readobj/elf-invalid-versioning.test +++ test/tools/llvm-readobj/elf-invalid-versioning.test @@ -1,42 +1,188 @@ -# RUN: yaml2obj %s -o %t -# RUN: llvm-readelf -V %t | FileCheck %s --check-prefix=INVALID +## Here we test how llvm-readelf/llvm-readobj behave when inputs have +## invalid versioning sections. -# INVALID: Version symbols section '.gnu.version' contains 2 entries: -# INVALID-NEXT: Addr: 0000000000200210 Offset: 0x000040 Link: 6 (.dynsym) -# INVALID-NEXT: 000: 0 (*local*) 3 (*invalid*) +## In the first case we have a SHT_GNU_versym section that refers to +## a version listed in a SHT_GNU_verneed section. That version has an +## empty name, making it invalid. + +# RUN: yaml2obj --docnum=1 %s -o %t1 +# RUN: llvm-readelf -V %t1 | FileCheck %s --check-prefix=GNU-VERNEED-NAME +# RUN: llvm-readobj -V %t1 | FileCheck %s --check-prefix=LLVM-VERNEED-NAME + +# GNU-VERNEED-NAME: Version symbols section '.gnu.version' contains 2 entries: +# GNU-VERNEED-NAME-NEXT: Addr: 0000000000200210 Offset: 0x000040 Link: 6 (.dynsym) +# GNU-VERNEED-NAME-NEXT: 000: 0 (*local*) 2 (*invalid*) + +# GNU-VERNEED-NAME: Version needs section '.gnu.version_r' contains 1 entries: +# GNU-VERNEED-NAME-NEXT: Addr: 0000000000000000 Offset: 0x000044 Link: 7 (.dynstr) +# GNU-VERNEED-NAME-NEXT: 0x0000: Version: 1 File: somefile Cnt: 1 +# GNU-VERNEED-NAME-NEXT: 0x0010: Name: Flags: none Version: 2 + +# LLVM-VERNEED-NAME: Version symbols { +# LLVM-VERNEED-NAME: Symbol { +# LLVM-VERNEED-NAME-NEXT: Version: 0 +# LLVM-VERNEED-NAME-NEXT: Name: +# LLVM-VERNEED-NAME-NEXT: } +# LLVM-VERNEED-NAME-NEXT: Symbol { +# LLVM-VERNEED-NAME-NEXT: Version: 2 +# LLVM-VERNEED-NAME-NEXT: Name: foo +# LLVM-VERNEED-NAME-NEXT: } +# LLVM-VERNEED-NAME-NEXT: ] + +# LLVM-VERNEED-NAME: SHT_GNU_verneed { +# LLVM-VERNEED-NAME-NEXT: Dependency { +# LLVM-VERNEED-NAME-NEXT: Version: 1 +# LLVM-VERNEED-NAME-NEXT: Count: 1 +# LLVM-VERNEED-NAME-NEXT: FileName: somefile +# LLVM-VERNEED-NAME-NEXT: Entries [ +# LLVM-VERNEED-NAME-NEXT: Entry { +# LLVM-VERNEED-NAME-NEXT: Hash: 0 +# LLVM-VERNEED-NAME-NEXT: Flags: 0x0 +# LLVM-VERNEED-NAME-NEXT: Index: 2 +# LLVM-VERNEED-NAME-NEXT: Name: {{$}} +# LLVM-VERNEED-NAME-NEXT: } +# LLVM-VERNEED-NAME-NEXT: ] +# LLVM-VERNEED-NAME-NEXT: } +# LLVM-VERNEED-NAME-NEXT: } --- !ELF FileHeader: - Class: ELFCLASS64 - Data: ELFDATA2LSB - Type: ET_EXEC - Machine: EM_X86_64 - Entry: 0x0000000000201000 + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_EXEC + Machine: EM_X86_64 Sections: - - Name: .gnu.version - Type: SHT_GNU_versym - Flags: [ SHF_ALLOC ] - Address: 0x0000000000200210 - Link: .dynsym - AddressAlign: 0x0000000000000002 - EntSize: 0x0000000000000002 - Entries: [ 0, 3 ] - - Name: .gnu.version_r - Type: SHT_GNU_verneed - Flags: [ SHF_ALLOC ] - Address: 0x0000000000200250 - Link: .dynstr - AddressAlign: 0x0000000000000004 - Info: 0x0000000000000001 + - Name: .gnu.version + Type: SHT_GNU_versym + Flags: [ SHF_ALLOC ] + Address: 0x200210 + Link: .dynsym + Entries: [ 0, 2 ] + - Name: .gnu.version_r + Type: SHT_GNU_verneed + Flags: [ SHF_ALLOC ] + Link: .dynstr + Info: 1 + AddressAlign: 4 Dependencies: - - Version: 1 - File: somefile + - Version: 1 + File: somefile Entries: - - Name: '' # invalid name - Hash: 1937 - Flags: 233 - Other: 3 + - Name: '' ## invalid name + Hash: 0 + Flags: 0 + Other: 2 DynamicSymbols: - - Name: f - Binding: STB_GLOBAL + - Name: foo + Binding: STB_GLOBAL ... + +## In this case SHT_GNU_verneed is not linked to a dynamic string table. We check we handle +## this situation properly. + +# RUN: yaml2obj --docnum=2 %s -o %t2 +# RUN: llvm-readelf -V %t2 | FileCheck %s --check-prefix=GNU-NOLINK +# RUN: llvm-readobj -V %t2 | FileCheck %s --check-prefix=LLVM-NOLINK + +# GNU-NOLINK: Version symbols section '.gnu.version' contains 2 entries: +# GNU-NOLINK-NEXT: Addr: 0000000000000000 Offset: 0x000040 Link: 6 (.dynsym) +# GNU-NOLINK-NEXT: 000: 0 (*local*) 2 (bar) +# GNU-NOLINK: Version needs section '.gnu.version_r' contains 1 entries: +# GNU-NOLINK-NEXT: Addr: 0000000000000000 Offset: 0x000044 Link: 0 () +# GNU-NOLINK-NEXT: 0x0000: Version: 1 File: Cnt: 1 +# GNU-NOLINK-NEXT: 0x0010: Name: Flags: none Version: 2 + +# LLVM-NOLINK: Version symbols { +# LLVM-NOLINK: Symbol { +# LLVM-NOLINK-NEXT: Version: 0 +# LLVM-NOLINK-NEXT: Name: +# LLVM-NOLINK-NEXT: } +# LLVM-NOLINK-NEXT: Symbol { +# LLVM-NOLINK-NEXT: Version: 2 +# LLVM-NOLINK-NEXT: Name: foo@bar +# LLVM-NOLINK-NEXT: } +# LLVM-NOLINK-NEXT: ] + +# LLVM-NOLINK: SHT_GNU_verneed { +# LLVM-NOLINK-NEXT: Dependency { +# LLVM-NOLINK-NEXT: Version: 1 +# LLVM-NOLINK-NEXT: Count: 1 +# LLVM-NOLINK-NEXT: FileName: +# LLVM-NOLINK-NEXT: Entries [ +# LLVM-NOLINK-NEXT: Entry { +# LLVM-NOLINK-NEXT: Hash: 0 +# LLVM-NOLINK-NEXT: Flags: 0 +# LLVM-NOLINK-NEXT: Index: 2 +# LLVM-NOLINK-NEXT: Name: +# LLVM-NOLINK-NEXT: } +# LLVM-NOLINK-NEXT: ] +# LLVM-NOLINK-NEXT: } +# LLVM-NOLINK-NEXT: } + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_EXEC + Machine: EM_X86_64 +Sections: + - Name: .gnu.version + Type: SHT_GNU_versym + Flags: [ SHF_ALLOC ] + Link: .dynsym + Entries: [ 0, 2 ] + - Name: .gnu.version_r + Type: SHT_GNU_verneed + Flags: [ SHF_ALLOC ] + Link: 0 + Info: 1 + AddressAlign: 4 + Dependencies: + - Version: 1 + File: somefile + Entries: + - Name: 'bar' + Hash: 0 + Flags: 0 + Other: 2 +DynamicSymbols: + - Name: foo + Binding: STB_GLOBAL + +## We can't parse misaligned auxiliary version records. +## Here we have a SHT_GNU_verneed section aligned by 1 byte. +## This makes the first auxiliary record offset % 4 be != 0. + +# RUN: yaml2obj --docnum=3 %s -o %t3 +# RUN: not llvm-readelf -V %t3 2>&1 | FileCheck %s --check-prefix=BROKEN-AUX +# RUN: not llvm-readobj -V %t3 2>&1 | FileCheck %s --check-prefix=BROKEN-AUX + +# BROKEN-AUX: LLVM ERROR: an auxiliary record found is misaligned + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_EXEC + Machine: EM_X86_64 +Sections: + - Name: .gnu.version + Type: SHT_GNU_versym + Flags: [ SHF_ALLOC ] + Link: .dynsym + Entries: [ 2 ] + - Name: .gnu.version_r + Type: SHT_GNU_verneed + Flags: [ SHF_ALLOC ] + Info: 1 + AddressAlign: 1 + Dependencies: + - Version: 1 + File: somefile + Entries: + - Name: 'bar' + Hash: 0 + Flags: 0 + Other: 2 +DynamicSymbols: + - Name: foo Index: tools/llvm-readobj/ELFDumper.cpp =================================================================== --- tools/llvm-readobj/ELFDumper.cpp +++ tools/llvm-readobj/ELFDumper.cpp @@ -667,6 +667,9 @@ if (VernauxBuf + sizeof(Elf_Vernaux) > VerneedEnd) report_fatal_error("Section ended unexpected while scanning auxiliary " "version needed records."); + if ((ptrdiff_t)VernauxBuf % sizeof(uint32_t) != 0) + report_fatal_error("an auxiliary record found is misaligned"); + const Elf_Vernaux *Vernaux = reinterpret_cast(VernauxBuf); size_t Index = Vernaux->vna_other & ELF::VERSYM_VERSION; @@ -3921,10 +3924,13 @@ const Elf_Verneed *Verneed = reinterpret_cast(VerneedBuf); + StringRef File = StringTable.size() >= Verneed->vn_file + ? StringTable.drop_front(Verneed->vn_file) + : ""; + OS << format(" 0x%04x: Version: %u File: %s Cnt: %u\n", reinterpret_cast(Verneed) - SecData.begin(), - (unsigned)Verneed->vn_version, - StringTable.drop_front(Verneed->vn_file).data(), + (unsigned)Verneed->vn_version, File.data(), (unsigned)Verneed->vn_cnt); const uint8_t *VernauxBuf = VerneedBuf + Verneed->vn_aux; @@ -3932,10 +3938,13 @@ const Elf_Vernaux *Vernaux = reinterpret_cast(VernauxBuf); + StringRef Name = StringTable.size() >= Vernaux->vna_name + ? StringTable.drop_front(Vernaux->vna_name) + : ""; + OS << format(" 0x%04x: Name: %s Flags: %s Version: %u\n", reinterpret_cast(Vernaux) - SecData.begin(), - StringTable.drop_front(Vernaux->vna_name).data(), - versionFlagToString(Vernaux->vna_flags).c_str(), + Name.data(), versionFlagToString(Vernaux->vna_flags).c_str(), (unsigned)Vernaux->vna_other); VernauxBuf += Vernaux->vna_next; } @@ -5680,8 +5689,11 @@ const uint8_t *SecData = reinterpret_cast(Obj->base() + Sec->sh_offset); - const Elf_Shdr *StrTab = + 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 *VerneedBuf = SecData; unsigned VerneedNum = Sec->sh_info; @@ -5691,9 +5703,11 @@ DictScope Entry(W, "Dependency"); W.printNumber("Version", Verneed->vn_version); W.printNumber("Count", Verneed->vn_cnt); - W.printString("FileName", - StringRef(reinterpret_cast( - Obj->base() + StrTab->sh_offset + Verneed->vn_file))); + + StringRef FileName = StringTable.size() >= Verneed->vn_file + ? StringTable.drop_front(Verneed->vn_file) + : ""; + W.printString("FileName", FileName.data()); const uint8_t *VernauxBuf = VerneedBuf + Verneed->vn_aux; ListScope L(W, "Entries"); @@ -5704,9 +5718,11 @@ W.printNumber("Hash", Vernaux->vna_hash); W.printEnum("Flags", Vernaux->vna_flags, makeArrayRef(SymVersionFlags)); W.printNumber("Index", Vernaux->vna_other); - W.printString("Name", - StringRef(reinterpret_cast( - Obj->base() + StrTab->sh_offset + Vernaux->vna_name))); + + StringRef Name = StringTable.size() >= Vernaux->vna_name + ? StringTable.drop_front(Vernaux->vna_name) + : ""; + W.printString("Name", Name.data()); VernauxBuf += Vernaux->vna_next; } VerneedBuf += Verneed->vn_next;