llvm.org GIT mirror llvm / 9a4133e
Simplify iterating over program headers and detect corrupt ones. We now use a simple pointer and have range loops. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@242669 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 5 years ago
5 changed file(s) with 49 addition(s) and 56 deletion(s). Raw diff Collapse all Expand all
348348 }
349349
350350 /// \brief Iterate over program header table.
351 typedef ELFEntityIterator Elf_Phdr_Iter;
352
353 Elf_Phdr_Iter program_header_begin() const {
354 return Elf_Phdr_Iter(Header->e_phentsize,
355 (const char*)base() + Header->e_phoff);
356 }
357
358 Elf_Phdr_Iter program_header_end() const {
359 return Elf_Phdr_Iter(Header->e_phentsize,
360 (const char*)base() +
361 Header->e_phoff +
362 (Header->e_phnum * Header->e_phentsize));
351 const Elf_Phdr *program_header_begin() const {
352 if (Header->e_phnum && Header->e_phentsize != sizeof(Elf_Phdr))
353 report_fatal_error("Invalid program header size");
354 return reinterpret_cast(base() + Header->e_phoff);
355 }
356
357 const Elf_Phdr *program_header_end() const {
358 return program_header_begin() + Header->e_phnum;
359 }
360
361 typedef iterator_range Elf_Phdr_Range;
362
363 const Elf_Phdr_Range program_headers() const {
364 return make_range(program_header_begin(), program_header_end());
363365 }
364366
365367 uint64_t getNumSections() const;
734736 // stack doesn't get realigned despite LoadMap having alignment 8 (PR24113).
735737 std::unique_ptr LoadMap(new LoadMapT(Alloc));
736738
737 for (Elf_Phdr_Iter PhdrI = program_header_begin(),
738 PhdrE = program_header_end();
739 PhdrI != PhdrE; ++PhdrI) {
740 if (PhdrI->p_type == ELF::PT_DYNAMIC) {
741 DynamicRegion.Addr = base() + PhdrI->p_offset;
742 DynamicRegion.Size = PhdrI->p_filesz;
739 for (const Elf_Phdr &Phdr : program_headers()) {
740 if (Phdr.p_type == ELF::PT_DYNAMIC) {
741 DynamicRegion.Addr = base() + Phdr.p_offset;
742 DynamicRegion.Size = Phdr.p_filesz;
743743 DynamicRegion.EntSize = sizeof(Elf_Dyn);
744744 continue;
745745 }
746 if (PhdrI->p_type != ELF::PT_LOAD)
746 if (Phdr.p_type != ELF::PT_LOAD || Phdr.p_filesz == 0)
747747 continue;
748 if (PhdrI->p_filesz == 0)
749 continue;
750 LoadMap->insert(PhdrI->p_vaddr, PhdrI->p_vaddr + PhdrI->p_filesz,
751 PhdrI->p_offset);
748 LoadMap->insert(Phdr.p_vaddr, Phdr.p_vaddr + Phdr.p_filesz, Phdr.p_offset);
752749 }
753750
754751 auto toMappedAddr = [&](uint64_t VAddr) -> const uint8_t * {
3030 RUN: 2>&1 | FileCheck --check-prefix=STRTAB %s
3131
3232 STRTAB: Invalid dynamic string table reference
33
34 RUN: not llvm-readobj -program-headers \
35 RUN: %p/Inputs/corrupt-invalid-phentsize.elf.x86-64 2>&1 | \
36 RUN: FileCheck --check-prefix=PHENTSIZE %s
37
38 PHENTSIZE: Invalid program header size
2323 template void printProgramHeaders(const ELFFile *o) {
2424 typedef ELFFile ELFO;
2525 outs() << "Program Header:\n";
26 for (typename ELFO::Elf_Phdr_Iter pi = o->program_header_begin(),
27 pe = o->program_header_end();
28 pi != pe; ++pi) {
29 switch (pi->p_type) {
26 for (const typename ELFO::Elf_Phdr &Phdr : o->program_headers()) {
27 switch (Phdr.p_type) {
3028 case ELF::PT_LOAD:
3129 outs() << " LOAD ";
3230 break;
5452
5553 const char *Fmt = ELFT::Is64Bits ? "0x%016" PRIx64 " " : "0x%08" PRIx64 " ";
5654
57 outs() << "off "
58 << format(Fmt, (uint64_t)pi->p_offset)
59 << "vaddr "
60 << format(Fmt, (uint64_t)pi->p_vaddr)
61 << "paddr "
62 << format(Fmt, (uint64_t)pi->p_paddr)
63 << format("align 2**%u\n", countTrailingZeros(pi->p_align))
64 << " filesz "
65 << format(Fmt, (uint64_t)pi->p_filesz)
66 << "memsz "
67 << format(Fmt, (uint64_t)pi->p_memsz)
68 << "flags "
69 << ((pi->p_flags & ELF::PF_R) ? "r" : "-")
70 << ((pi->p_flags & ELF::PF_W) ? "w" : "-")
71 << ((pi->p_flags & ELF::PF_X) ? "x" : "-")
72 << "\n";
55 outs() << "off " << format(Fmt, (uint64_t)Phdr.p_offset) << "vaddr "
56 << format(Fmt, (uint64_t)Phdr.p_vaddr) << "paddr "
57 << format(Fmt, (uint64_t)Phdr.p_paddr)
58 << format("align 2**%u\n",
59 countTrailingZeros(Phdr.p_align))
60 << " filesz " << format(Fmt, (uint64_t)Phdr.p_filesz)
61 << "memsz " << format(Fmt, (uint64_t)Phdr.p_memsz) << "flags "
62 << ((Phdr.p_flags & ELF::PF_R) ? "r" : "-")
63 << ((Phdr.p_flags & ELF::PF_W) ? "w" : "-")
64 << ((Phdr.p_flags & ELF::PF_X) ? "x" : "-") << "\n";
7365 }
7466 outs() << "\n";
7567 }
11081108 void ELFDumper::printProgramHeaders() {
11091109 ListScope L(W, "ProgramHeaders");
11101110
1111 for (typename ELFO::Elf_Phdr_Iter PI = Obj->program_header_begin(),
1112 PE = Obj->program_header_end();
1113 PI != PE; ++PI) {
1111 for (const typename ELFO::Elf_Phdr &Phdr : Obj->program_headers()) {
11141112 DictScope P(W, "ProgramHeader");
1115 W.printHex ("Type",
1116 getElfSegmentType(Obj->getHeader()->e_machine, PI->p_type),
1117 PI->p_type);
1118 W.printHex ("Offset", PI->p_offset);
1119 W.printHex ("VirtualAddress", PI->p_vaddr);
1120 W.printHex ("PhysicalAddress", PI->p_paddr);
1121 W.printNumber("FileSize", PI->p_filesz);
1122 W.printNumber("MemSize", PI->p_memsz);
1123 W.printFlags ("Flags", PI->p_flags, makeArrayRef(ElfSegmentFlags));
1124 W.printNumber("Alignment", PI->p_align);
1113 W.printHex("Type",
1114 getElfSegmentType(Obj->getHeader()->e_machine, Phdr.p_type),
1115 Phdr.p_type);
1116 W.printHex("Offset", Phdr.p_offset);
1117 W.printHex("VirtualAddress", Phdr.p_vaddr);
1118 W.printHex("PhysicalAddress", Phdr.p_paddr);
1119 W.printNumber("FileSize", Phdr.p_filesz);
1120 W.printNumber("MemSize", Phdr.p_memsz);
1121 W.printFlags("Flags", Phdr.p_flags, makeArrayRef(ElfSegmentFlags));
1122 W.printNumber("Alignment", Phdr.p_align);
11251123 }
11261124 }
11271125