llvm.org GIT mirror llvm / 42b70df
Refactor to use getSectionContentsAsArray. This centralizes quite a bit of error checking. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@283454 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 3 years ago
4 changed file(s) with 42 addition(s) and 76 deletion(s). Raw diff Collapse all Expand all
114114 return makeArrayRef(section_begin(), section_end());
115115 }
116116
117 const Elf_Sym *symbol_begin(const Elf_Shdr *Sec) const {
117 Elf_Sym_Range symbols(const Elf_Shdr *Sec) const {
118118 if (!Sec)
119 return nullptr;
119 return makeArrayRef(nullptr, nullptr);
120120 if (Sec->sh_entsize != sizeof(Elf_Sym))
121121 report_fatal_error("Invalid symbol size");
122 return reinterpret_cast(base() + Sec->sh_offset);
123 }
124 const Elf_Sym *symbol_end(const Elf_Shdr *Sec) const {
125 if (!Sec)
126 return nullptr;
127 uint64_t Size = Sec->sh_size;
128 if (Size % sizeof(Elf_Sym))
129 report_fatal_error("Invalid symbol table size");
130 return symbol_begin(Sec) + Size / sizeof(Elf_Sym);
131 }
132 Elf_Sym_Range symbols(const Elf_Shdr *Sec) const {
133 return makeArrayRef(symbol_begin(Sec), symbol_end(Sec));
134 }
135
136 const Elf_Rela *rela_begin(const Elf_Shdr *sec) const {
137 if (sec->sh_entsize != sizeof(Elf_Rela))
122 auto V = getSectionContentsAsArray(Sec);
123 if (!V)
124 report_fatal_error(V.getError().message());
125 return *V;
126 }
127
128 Elf_Rela_Range relas(const Elf_Shdr *Sec) const {
129 if (Sec->sh_entsize != sizeof(Elf_Rela))
138130 report_fatal_error("Invalid relocation entry size");
139 if (sec->sh_offset >= Buf.size())
140 report_fatal_error("Invalid relocation entry offset");
141 return reinterpret_cast(base() + sec->sh_offset);
142 }
143
144 const Elf_Rela *rela_end(const Elf_Shdr *sec) const {
145 uint64_t Size = sec->sh_size;
146 if (Size % sizeof(Elf_Rela))
147 report_fatal_error("Invalid relocation table size");
148 return rela_begin(sec) + Size / sizeof(Elf_Rela);
149 }
150
151 Elf_Rela_Range relas(const Elf_Shdr *Sec) const {
152 return makeArrayRef(rela_begin(Sec), rela_end(Sec));
153 }
154
155 const Elf_Rel *rel_begin(const Elf_Shdr *sec) const {
156 if (sec->sh_entsize != sizeof(Elf_Rel))
131 auto V = getSectionContentsAsArray(Sec);
132 if (!V)
133 report_fatal_error(V.getError().message());
134 return *V;
135 }
136
137 Elf_Rel_Range rels(const Elf_Shdr *Sec) const {
138 if (Sec->sh_entsize != sizeof(Elf_Rel))
157139 report_fatal_error("Invalid relocation entry size");
158 if (sec->sh_offset >= Buf.size())
159 report_fatal_error("Invalid relocation entry offset");
160 return reinterpret_cast(base() + sec->sh_offset);
161 }
162
163 const Elf_Rel *rel_end(const Elf_Shdr *sec) const {
164 uint64_t Size = sec->sh_size;
165 if (Size % sizeof(Elf_Rel))
166 report_fatal_error("Invalid relocation table size");
167 return rel_begin(sec) + Size / sizeof(Elf_Rel);
168 }
169
170 Elf_Rel_Range rels(const Elf_Shdr *Sec) const {
171 return makeArrayRef(rel_begin(Sec), rel_end(Sec));
140 auto V = getSectionContentsAsArray(Sec);
141 if (!V)
142 report_fatal_error(V.getError().message());
143 return *V;
172144 }
173145
174146 /// \brief Iterate over program header table.
201173 ErrorOr getSection(uint32_t Index) const;
202174
203175 const Elf_Sym *getSymbol(const Elf_Shdr *Sec, uint32_t Index) const {
204 return &*(symbol_begin(Sec) + Index);
176 return &symbols(Sec)[Index];
205177 }
206178
207179 ErrorOr getSectionName(const Elf_Shdr *Section) const;
219191 uint32_t ELFFile::getExtendedSymbolTableIndex(
220192 const Elf_Sym *Sym, const Elf_Shdr *SymTab,
221193 ArrayRef ShndxTable) const {
222 return getExtendedSymbolTableIndex(Sym, symbol_begin(SymTab), ShndxTable);
194 return getExtendedSymbolTableIndex(Sym, symbols(SymTab).begin(), ShndxTable);
223195 }
224196
225197 template
439411 ELFFile::getStringTable(const Elf_Shdr *Section) const {
440412 if (Section->sh_type != ELF::SHT_STRTAB)
441413 return object_error::parse_failed;
442 uint64_t Offset = Section->sh_offset;
443 uint64_t Size = Section->sh_size;
444 if (Offset + Size > Buf.size())
445 return object_error::parse_failed;
446 StringRef Data((const char *)base() + Section->sh_offset, Size);
447 if (Data[Size - 1] != '\0')
414 auto V = getSectionContentsAsArray(Section);
415 if (std::error_code EC = V.getError())
416 return EC;
417 ArrayRef Data = *V;
418 if (Data.back() != '\0')
448419 return object_error::string_table_non_null_end;
449 return Data;
420 return StringRef(Data.begin(), Data.size());
450421 }
451422
452423 template
453424 ErrorOr::Elf_Word>>
454425 ELFFile::getSHNDXTable(const Elf_Shdr &Section) const {
455426 assert(Section.sh_type == ELF::SHT_SYMTAB_SHNDX);
456 const Elf_Word *ShndxTableBegin =
457 reinterpret_cast(base() + Section.sh_offset);
458 uintX_t Size = Section.sh_size;
459 if (Size % sizeof(uint32_t))
460 return object_error::parse_failed;
461 uintX_t NumSymbols = Size / sizeof(uint32_t);
462 const Elf_Word *ShndxTableEnd = ShndxTableBegin + NumSymbols;
463 if (reinterpret_cast(ShndxTableEnd) > Buf.end())
464 return object_error::parse_failed;
427 auto VOrErr = getSectionContentsAsArray(&Section);
428 if (std::error_code EC = VOrErr.getError())
429 return EC;
430 ArrayRef V = *VOrErr;
465431 ErrorOr SymTableOrErr = getSection(Section.sh_link);
466432 if (std::error_code EC = SymTableOrErr.getError())
467433 return EC;
469435 if (SymTable.sh_type != ELF::SHT_SYMTAB &&
470436 SymTable.sh_type != ELF::SHT_DYNSYM)
471437 return object_error::parse_failed;
472 if (NumSymbols != (SymTable.sh_size / sizeof(Elf_Sym)))
473 return object_error::parse_failed;
474 return makeArrayRef(ShndxTableBegin, ShndxTableEnd);
438 if (V.size() != (SymTable.sh_size / sizeof(Elf_Sym)))
439 return object_error::parse_failed;
440 return V;
475441 }
476442
477443 template
495495 Result |= SymbolRef::SF_Absolute;
496496
497497 if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION ||
498 ESym == EF.symbol_begin(DotSymtabSec) ||
499 ESym == EF.symbol_begin(DotDynSymSec))
498 ESym == EF.symbols(DotSymtabSec).begin() ||
499 ESym == EF.symbols(DotDynSymSec).begin())
500500 Result |= SymbolRef::SF_FormatSpecific;
501501
502502 if (EF.getHeader()->e_machine == ELF::EM_ARM) {
4848
4949
5050 RUN: not llvm-readobj -t %p/Inputs/invalid-symbol-table-size.elf 2>&1 | FileCheck --check-prefix=INVALID-SYMTAB-SIZE %s
51 INVALID-SYMTAB-SIZE: Invalid symbol table size
51 INVALID-SYMTAB-SIZE: Invalid data was encountered while parsing the file
5252
5353
5454 RUN: not llvm-readobj -t %p/Inputs/invalid-xindex-size.elf 2>&1 | FileCheck --check-prefix=INVALID-XINDEX-SIZE %s
6262 RUN: FileCheck --check-prefix=INVALID-RELOC-SH-OFFSET %s
6363 RUN: not llvm-readobj -r %p/Inputs/invalid-relocation-sec-sh_offset.elf-x86-64 2>&1 | \
6464 RUN: FileCheck --check-prefix=INVALID-RELOC-SH-OFFSET %s
65 INVALID-RELOC-SH-OFFSET: Invalid relocation entry offset
65 INVALID-RELOC-SH-OFFSET: Invalid data was encountered while parsing the file
34923492 const Elf_Shdr *SymSec = unwrapOrError(
34933493 Obj->getSection(&Sym, Symtab, this->dumper()->getShndxTable()));
34943494 if (SymSec == &Sec)
3495 printSymbol(Obj, &Sym, Obj->symbol_begin(Symtab), StrTable, false);
3495 printSymbol(Obj, &Sym, Obj->symbols(Symtab).begin(), StrTable, false);
34963496 }
34973497 }
34983498