llvm.org GIT mirror llvm / e1358aa
Add dynamic_table iterators back to ELF.h. In tree they are only used by llvm-readobj, but it is also used by https://github.com/mono/CppSharp. While at it, add some missing error checking. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@244320 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 5 years ago
6 changed file(s) with 72 addition(s) and 22 deletion(s). Raw diff Collapse all Expand all
138138 return Header->e_machine == ELF::EM_MIPS &&
139139 Header->getFileClass() == ELF::ELFCLASS64 &&
140140 Header->getDataEncoding() == ELF::ELFDATA2LSB;
141 }
142
143 ErrorOr dynamic_table_begin(const Elf_Phdr *Phdr) const;
144 ErrorOr dynamic_table_end(const Elf_Phdr *Phdr) const;
145 ErrorOr dynamic_table(const Elf_Phdr *Phdr) const {
146 ErrorOr Begin = dynamic_table_begin(Phdr);
147 if (std::error_code EC = Begin.getError())
148 return EC;
149 ErrorOr End = dynamic_table_end(Phdr);
150 if (std::error_code EC = End.getError())
151 return EC;
152 return make_range(*Begin, *End);
141153 }
142154
143155 const Elf_Shdr *section_begin() const;
465477 }
466478
467479 template
480 ErrorOr::Elf_Dyn *>
481 ELFFile::dynamic_table_begin(const Elf_Phdr *Phdr) const {
482 if (!Phdr)
483 return nullptr;
484 assert(Phdr->p_type == ELF::PT_DYNAMIC && "Got the wrong program header");
485 uintX_t Offset = Phdr->p_offset;
486 if (Offset > Buf.size())
487 return object_error::parse_failed;
488 return reinterpret_cast(base() + Offset);
489 }
490
491 template
492 ErrorOr::Elf_Dyn *>
493 ELFFile::dynamic_table_end(const Elf_Phdr *Phdr) const {
494 if (!Phdr)
495 return nullptr;
496 assert(Phdr->p_type == ELF::PT_DYNAMIC && "Got the wrong program header");
497 uintX_t Size = Phdr->p_filesz;
498 if (Size % sizeof(Elf_Dyn))
499 return object_error::elf_invalid_dynamic_table_size;
500 // FIKME: Check for overflow?
501 uintX_t End = Phdr->p_offset + Size;
502 if (End > Buf.size())
503 return object_error::parse_failed;
504 return reinterpret_cast(base() + End);
505 }
506
507 template
468508 template
469509 const T *ELFFile::getEntry(uint32_t Section, uint32_t Entry) const {
470510 ErrorOr Sec = getSection(Section);
2929 string_table_non_null_end,
3030 invalid_section_index,
3131 bitcode_section_not_found,
32 elf_invalid_dynamic_table_size,
3233 macho_small_load_command,
3334 macho_load_segment_too_many_sections,
3435 macho_load_segment_too_small,
4646 return "Invalid section index";
4747 case object_error::bitcode_section_not_found:
4848 return "Bitcode section not found in object file";
49 case object_error::elf_invalid_dynamic_table_size:
50 return "Invalid dynamic table size";
4951 case object_error::macho_small_load_command:
5052 return "Mach-O load command with size < 8 bytes";
5153 case object_error::macho_load_segment_too_many_sections:
5555 RUN: FileCheck --check-prefix=DYN-TABLE-SIZE %s
5656
5757 DYN-TABLE-SIZE: Invalid dynamic table size
58
59
60 RUN: not llvm-readobj -dyn-relocations \
61 RUN: %p/Inputs/corrupt-invalid-dynamic-table-offset.elf.x86-64 2>&1 | \
62 RUN: FileCheck --check-prefix=DYN-TABLE-OFFSET %s
63
64 DYN-TABLE-OFFSET: Invalid data was encountered while parsing the file.
65
66
67 RUN: not llvm-readobj -dyn-relocations \
68 RUN: %p/Inputs/corrupt-invalid-dynamic-table-too-large.elf.x86-64 2>&1 | \
69 RUN: FileCheck --check-prefix=DYN-TABLE-TOO-LARGE %s
70
71 DYN-TABLE-TOO-LARGE: Invalid data was encountered while parsing the file.
103103 const Elf_Rela *dyn_rela_end() const;
104104 Elf_Rela_Range dyn_relas() const;
105105 StringRef getDynamicString(uint64_t Offset) const;
106 const Elf_Dyn *dynamic_table_begin() const;
107 const Elf_Dyn *dynamic_table_end() const;
106 const Elf_Dyn *dynamic_table_begin() const {
107 ErrorOr Ret = Obj->dynamic_table_begin(DynamicProgHeader);
108 error(Ret.getError());
109 return *Ret;
110 }
111 const Elf_Dyn *dynamic_table_end() const {
112 ErrorOr Ret = Obj->dynamic_table_end(DynamicProgHeader);
113 error(Ret.getError());
114 return *Ret;
115 }
108116 Elf_Dyn_Range dynamic_table() const {
109 return make_range(dynamic_table_begin(), dynamic_table_end());
117 ErrorOr Ret = Obj->dynamic_table(DynamicProgHeader);
118 error(Ret.getError());
119 return *Ret;
110120 }
111121
112122 StringRef getSymbolVersion(StringRef StrTab, const Elf_Sym *symb,
117127
118128 const ELFO *Obj;
119129 DynRegionInfo DynRelaRegion;
120 DynRegionInfo DynamicRegion;
130 const Elf_Phdr *DynamicProgHeader = nullptr;
121131 StringRef DynamicStringTable;
122132 const Elf_Sym *DynSymStart = nullptr;
123133 StringRef SOName;
797807 SmallVector LoadSegments;
798808 for (const Elf_Phdr &Phdr : Obj->program_headers()) {
799809 if (Phdr.p_type == ELF::PT_DYNAMIC) {
800 DynamicRegion.Addr = Obj->base() + Phdr.p_offset;
801 uint64_t Size = Phdr.p_filesz;
802 if (Size % sizeof(Elf_Dyn))
803 report_fatal_error("Invalid dynamic table size");
804 DynamicRegion.Size = Phdr.p_filesz;
810 DynamicProgHeader = &Phdr;
805811 continue;
806812 }
807813 if (Phdr.p_type != ELF::PT_LOAD || Phdr.p_filesz == 0)
901907 template
902908 typename ELFDumper::Elf_Rela_Range ELFDumper::dyn_relas() const {
903909 return make_range(dyn_rela_begin(), dyn_rela_end());
904 }
905
906 template
907 const typename ELFDumper::Elf_Dyn *
908 ELFDumper::dynamic_table_begin() const {
909 return reinterpret_cast(DynamicRegion.Addr);
910 }
911
912 template
913 const typename ELFDumper::Elf_Dyn *
914 ELFDumper::dynamic_table_end() const {
915 uint64_t Size = DynamicRegion.Size;
916 return dynamic_table_begin() + Size / sizeof(Elf_Dyn);
917910 }
918911
919912 template