llvm.org GIT mirror llvm / 8b80d4e
[llvm-readobj] - Teach tool to dump objects with >= SHN_LORESERVE of sections. http://www.sco.com/developers/gabi/2003-12-17/ch4.eheader.html says that e_shnum and/or e_shstrndx may have special values if "the number of sections is greater than or equal to SHN_LORESERVE" or "the section name string table section index is greater than or equal to SHN_LORESERVE (0xff00)" Previously llvm-readobj was unable to dump such files, patch changes that. I had to add a precompiled test case because it does not seem possible to prepare a test using yaml2obj or llvm-mc (not clear how to make .shstrtab to have index >= SHN_LORESERVE). Differential revision: https://reviews.llvm.org/D49369 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@337360 91177308-0d34-0410-b5e6-96231b3b80d8 George Rimar 1 year, 10 months ago
4 changed file(s) with 64 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
0 ## many-sections.elf-x86_64 is a file that was generated to simulate
1 ## an object with more than ~65k sections. When an ELF object
2 ## has SHN_LORESERVE (0xff00) or more sections, its e_shnum field
3 ## should be zero and sh_size of the section header at index 0 is used
4 ## to store the value. If the section name string table section index is
5 ## greater than or equal to SHN_LORESERVE, then e_shstrndx field
6 ## should have the value of SHN_XINDEX and sh_link of the section header
7 ## at index 0 is used to store the value.
8 ##
9 ## many-sections.elf-x86_64 has few sections to save disk
10 ## space, but its e_shnum, e_shstrndx, sh_size and sh_link fields are set
11 ## according to the above description, so that we can test the dumper.
12
13 # RUN: llvm-readobj -file-headers -elf-output-style GNU \
14 # RUN: %p/Inputs/many-sections.elf-x86_64 | FileCheck %s --check-prefix=GNU1
15 # GNU1: Number of section headers: 0 (5)
16 # GNU1: Section header string table index: 65535 (3)
17
18 # RUN: llvm-readobj -file-headers -elf-output-style LLVM \
19 # RUN: %p/Inputs/many-sections.elf-x86_64 | FileCheck %s --check-prefix=LLVM1
20 # LLVM1: SectionHeaderCount: 0 (5)
21 # LLVM1: StringTableSectionIndex: 65535 (3)
22
23 ## many-sections-stripped.elf-x86_64 is many-sections.elf-x86_64 with
24 ## e_shoff field set to zero, but not e_shstrndx, to show that
25 ## this corrupt case is handled correctly.
26
27 # RUN: llvm-readobj -file-headers -elf-output-style GNU \
28 # RUN: %p/Inputs/many-sections-stripped.elf-x86_64 | FileCheck %s --check-prefix=GNU2
29 # GNU2: Number of section headers: 0
30 # GNU2: Section header string table index: 65535 (corrupt: out of range)
31
32 # RUN: llvm-readobj -file-headers -elf-output-style LLVM \
33 # RUN: %p/Inputs/many-sections-stripped.elf-x86_64 | FileCheck %s --check-prefix=LLVM2
34 # LLVM2: SectionHeaderCount: 0
35 # LLVM2: StringTableSectionIndex: 65535 (corrupt: out of range)
24852485 OS.flush();
24862486 }
24872487
2488 template
2489 static std::string getSectionHeadersNumString(const ELFFile *Obj) {
2490 const typename ELFT::Ehdr *ElfHeader = Obj->getHeader();
2491 if (ElfHeader->e_shnum != 0)
2492 return to_string(ElfHeader->e_shnum);
2493
2494 ArrayRef Arr = unwrapOrError(Obj->sections());
2495 if (Arr.empty())
2496 return "0";
2497 return "0 (" + to_string(Arr[0].sh_size) + ")";
2498 }
2499
2500 template
2501 static std::string getSectionHeaderTableIndexString(const ELFFile *Obj) {
2502 const typename ELFT::Ehdr *ElfHeader = Obj->getHeader();
2503 if (ElfHeader->e_shstrndx != SHN_XINDEX)
2504 return to_string(ElfHeader->e_shstrndx);
2505
2506 ArrayRef Arr = unwrapOrError(Obj->sections());
2507 if (Arr.empty())
2508 return "65535 (corrupt: out of range)";
2509 return to_string(ElfHeader->e_shstrndx) + " (" + to_string(Arr[0].sh_link) + ")";
2510 }
2511
24882512 template void GNUStyle::printFileHeaders(const ELFO *Obj) {
24892513 const Elf_Ehdr *e = Obj->getHeader();
24902514 OS << "ELF Header:\n";
25302554 printFields(OS, "Number of program headers:", Str);
25312555 Str = to_string(e->e_shentsize) + " (bytes)";
25322556 printFields(OS, "Size of section headers:", Str);
2533 Str = to_string(e->e_shnum);
2557 Str = getSectionHeadersNumString(Obj);
25342558 printFields(OS, "Number of section headers:", Str);
2535 Str = to_string(e->e_shstrndx);
2559 Str = getSectionHeaderTableIndexString(Obj);
25362560 printFields(OS, "Section header string table index:", Str);
25372561 }
25382562
40184042 W.printNumber("ProgramHeaderEntrySize", e->e_phentsize);
40194043 W.printNumber("ProgramHeaderCount", e->e_phnum);
40204044 W.printNumber("SectionHeaderEntrySize", e->e_shentsize);
4021 W.printNumber("SectionHeaderCount", e->e_shnum);
4022 W.printNumber("StringTableSectionIndex", e->e_shstrndx);
4045 W.printString("SectionHeaderCount", getSectionHeadersNumString(Obj));
4046 W.printString("StringTableSectionIndex", getSectionHeaderTableIndexString(Obj));
40234047 }
40244048 }
40254049