llvm.org GIT mirror llvm / c771cb1
Add error handling to getEntry. Issue found by inspection. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@285951 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 3 years ago
6 changed file(s) with 32 addition(s) and 18 deletion(s). Raw diff Collapse all Expand all
7474 template
7575 ErrorOr getEntry(uint32_t Section, uint32_t Entry) const;
7676 template
77 const T *getEntry(const Elf_Shdr *Section, uint32_t Entry) const;
77 ErrorOr getEntry(const Elf_Shdr *Section, uint32_t Entry) const;
7878
7979 ErrorOr getStringTable(const Elf_Shdr *Section) const;
8080 ErrorOr getStringTableForSymtab(const Elf_Shdr &Section) const;
9292 SmallVectorImpl &Result) const;
9393
9494 /// \brief Get the symbol for a given relocation.
95 const Elf_Sym *getRelocationSymbol(const Elf_Rel *Rel,
96 const Elf_Shdr *SymTab) const;
95 ErrorOr getRelocationSymbol(const Elf_Rel *Rel,
96 const Elf_Shdr *SymTab) const;
9797
9898 ELFFile(StringRef Object, std::error_code &EC);
9999
309309 }
310310
311311 template
312 const typename ELFT::Sym *
312 ErrorOr
313313 ELFFile::getRelocationSymbol(const Elf_Rel *Rel,
314314 const Elf_Shdr *SymTab) const {
315315 uint32_t Index = Rel->getSymbol(isMips64EL());
412412
413413 template
414414 template
415 const T *ELFFile::getEntry(const Elf_Shdr *Section,
416 uint32_t Entry) const {
417 return reinterpret_cast(base() + Section->sh_offset +
418 (Entry * Section->sh_entsize));
415 ErrorOr ELFFile::getEntry(const Elf_Shdr *Section,
416 uint32_t Entry) const {
417 if (sizeof(T) != Section->sh_entsize)
418 return object_error::parse_failed;
419 size_t Pos = Section->sh_offset + Entry * sizeof(T);
420 if (Pos + sizeof(T) > Buf.size())
421 return object_error::parse_failed;
422 return reinterpret_cast(base() + Pos);
419423 }
420424
421425 template
7777
7878 RUN: not llvm-readobj -t %p/Inputs/invalid-sections-num.elf 2>&1 | FileCheck --check-prefix=INVALID-SECTION-NUM %s
7979 INVALID-SECTION-NUM: Invalid data was encountered while parsing the file.
80
81 RUN: not llvm-readobj -r %p/Inputs/invalid-rel-sym.elf 2>&1 | FileCheck --check-prefix=INVALID-REL-SYM %s
82 INVALID-REL-SYM: Invalid data was encountered while parsing the file.
395395 RelA.r_info = R.r_info;
396396 RelA.r_addend = 0;
397397
398 const Elf_Sym *Symbol = ELF->getRelocationSymbol(&RelA, SymTab);
398 const Elf_Sym *Symbol =
399 unwrapOrError(ELF->getRelocationSymbol(&RelA, SymTab));
399400
400401 ErrorOr Ret =
401402 ELF->getSection(Symbol, SymTab, ShndxTable);
646646 sizeof(Elf_Sym);
647647
648648 // Get the corresponding version index entry
649 const Elf_Versym *vs =
650 Obj->template getEntry(dot_gnu_version_sec, entry_index);
649 const Elf_Versym *vs = unwrapOrError(
650 Obj->template getEntry(dot_gnu_version_sec, entry_index));
651651 size_t version_index = vs->vs_index & ELF::VERSYM_VERSION;
652652
653653 // Special markers for unversioned symbols.
20672067 switch (PLTRelShdr->sh_type) {
20682068 case ELF::SHT_REL:
20692069 for (const Elf_Rel &Rel : Obj->rels(PLTRelShdr)) {
2070 const Elf_Sym *Sym = Obj->getRelocationSymbol(&Rel, SymTable);
2070 const Elf_Sym *Sym =
2071 unwrapOrError(Obj->getRelocationSymbol(&Rel, SymTable));
20712072 printPLTEntry(PLTShdr->sh_addr, PLTBegin, It, StrTable, Sym);
20722073 if (++It == PLTEnd)
20732074 break;
20752076 break;
20762077 case ELF::SHT_RELA:
20772078 for (const Elf_Rela &Rel : Obj->relas(PLTRelShdr)) {
2078 const Elf_Sym *Sym = Obj->getRelocationSymbol(&Rel, SymTable);
2079 const Elf_Sym *Sym =
2080 unwrapOrError(Obj->getRelocationSymbol(&Rel, SymTable));
20792081 printPLTEntry(PLTShdr->sh_addr, PLTBegin, It, StrTable, Sym);
20802082 if (++It == PLTEnd)
20812083 break;
24272429 const Elf_Shdr *Symtab = unwrapOrError(Obj->getSection(Sec.sh_link));
24282430 StringRef StrTable = unwrapOrError(Obj->getStringTableForSymtab(*Symtab));
24292431 const Elf_Sym *Signature =
2430 Obj->template getEntry(Symtab, Sec.sh_info);
2432 unwrapOrError(Obj->template getEntry(Symtab, Sec.sh_info));
24312433 ArrayRef Data = unwrapOrError(
24322434 Obj->template getSectionContentsAsArray(&Sec));
24332435 StringRef Name = unwrapOrError(Obj->getSectionName(&Sec));
24642466 // fixed width.
24652467 Field Fields[5] = {0, 10 + Bias, 19 + 2 * Bias, 42 + 2 * Bias, 53 + 2 * Bias};
24662468 Obj->getRelocationTypeName(R.getType(Obj->isMips64EL()), RelocName);
2467 Sym = Obj->getRelocationSymbol(&R, SymTab);
2469 Sym = unwrapOrError(Obj->getRelocationSymbol(&R, SymTab));
24682470 if (Sym && Sym->getType() == ELF::STT_SECTION) {
24692471 const Elf_Shdr *Sec = unwrapOrError(
24702472 Obj->getSection(Sym, SymTab, this->dumper()->getShndxTable()));
33313333 HasGroups = true;
33323334 const Elf_Shdr *Symtab = unwrapOrError(Obj->getSection(Sec.sh_link));
33333335 StringRef StrTable = unwrapOrError(Obj->getStringTableForSymtab(*Symtab));
3334 const Elf_Sym *Sym = Obj->template getEntry(Symtab, Sec.sh_info);
3336 const Elf_Sym *Sym =
3337 unwrapOrError(Obj->template getEntry(Symtab, Sec.sh_info));
33353338 auto Data = unwrapOrError(
33363339 Obj->template getSectionContentsAsArray(&Sec));
33373340 DictScope D(W, "Group");
34053408 SmallString<32> RelocName;
34063409 Obj->getRelocationTypeName(Rel.getType(Obj->isMips64EL()), RelocName);
34073410 StringRef TargetName;
3408 const Elf_Sym *Sym = Obj->getRelocationSymbol(&Rel, SymTab);
3411 const Elf_Sym *Sym = unwrapOrError(Obj->getRelocationSymbol(&Rel, SymTab));
34093412 if (Sym && Sym->getType() == ELF::STT_SECTION) {
34103413 const Elf_Shdr *Sec = unwrapOrError(
34113414 Obj->getSection(Sym, SymTab, this->dumper()->getShndxTable()));
215215 R.Offset = Rel->r_offset;
216216 R.Addend = 0;
217217
218 const Elf_Sym *Sym = Obj.getRelocationSymbol(Rel, SymTab);
218 auto SymOrErr = Obj.getRelocationSymbol(Rel, SymTab);
219 if (std::error_code EC = SymOrErr.getError())
220 return EC;
221 const Elf_Sym *Sym = *SymOrErr;
219222 ErrorOr StrTabSec = Obj.getSection(SymTab->sh_link);
220223 if (std::error_code EC = StrTabSec.getError())
221224 return EC;