uintX_t getStringTableIndex() const;
ELF::Elf64_Word getExtendedSymbolTableIndex(const Elf_Sym *symb) const;
const Elf_Ehdr *getHeader() const { return Header; }
- const Elf_Shdr *getSection(const Elf_Sym *symb) const;
- const Elf_Shdr *getSection(uint32_t Index) const;
+ ErrorOr<const Elf_Shdr *> getSection(const Elf_Sym *symb) const;
+ ErrorOr<const Elf_Shdr *> getSection(uint32_t Index) const;
const Elf_Sym *getSymbol(uint32_t index) const;
ErrorOr<StringRef> getStaticSymbolName(const Elf_Sym *Symb) const;
}
template <class ELFT>
-const typename ELFFile<ELFT>::Elf_Shdr *
+ErrorOr<const typename ELFFile<ELFT>::Elf_Shdr *>
ELFFile<ELFT>::getSection(const Elf_Sym *symb) const {
- if (symb->st_shndx == ELF::SHN_XINDEX)
+ uint32_t Index = symb->st_shndx;
+ if (Index == ELF::SHN_XINDEX)
return getSection(ExtendedSymbolTable.lookup(symb));
- if (symb->st_shndx >= ELF::SHN_LORESERVE)
+ if (Index == ELF::SHN_UNDEF || Index >= ELF::SHN_LORESERVE)
return nullptr;
return getSection(symb->st_shndx);
}
ELFFile<ELFT>::getRelocationSymbol(const Elf_Shdr *Sec, const RelT *Rel) const {
if (!Sec->sh_link)
return std::make_pair(nullptr, nullptr);
- const Elf_Shdr *SymTable = getSection(Sec->sh_link);
+ ErrorOr<const Elf_Shdr *> SymTableOrErr = getSection(Sec->sh_link);
+ if (std::error_code EC = SymTableOrErr.getError())
+ report_fatal_error(EC.message());
+ const Elf_Shdr *SymTable = *SymTableOrErr;
return std::make_pair(
SymTable, getEntry<Elf_Sym>(SymTable, Rel->getSymbol(isMips64EL())));
}
return;
}
dot_symtab_sec = &Sec;
- ErrorOr<StringRef> SymtabOrErr = getStringTable(getSection(Sec.sh_link));
+ ErrorOr<const Elf_Shdr *> SectionOrErr = getSection(Sec.sh_link);
+ if ((EC = SectionOrErr.getError()))
+ return;
+ ErrorOr<StringRef> SymtabOrErr = getStringTable(*SectionOrErr);
if ((EC = SymtabOrErr.getError()))
return;
DotStrtab = *SymtabOrErr;
DynSymRegion.Addr = base() + Sec.sh_offset;
DynSymRegion.Size = Sec.sh_size;
DynSymRegion.EntSize = Sec.sh_entsize;
- const Elf_Shdr *DynStr = getSection(Sec.sh_link);
+ ErrorOr<const Elf_Shdr *> DynStrOrErr = getSection(Sec.sh_link);
+ if ((EC = DynStrOrErr.getError()))
+ return;
+ const Elf_Shdr *DynStr = *DynStrOrErr;
DynStrRegion.Addr = base() + DynStr->sh_offset;
DynStrRegion.Size = DynStr->sh_size;
DynStrRegion.EntSize = DynStr->sh_entsize;
}
// Get string table sections.
- ErrorOr<StringRef> SymtabOrErr =
- getStringTable(getSection(getStringTableIndex()));
+ ErrorOr<const Elf_Shdr *> StrTabSecOrErr = getSection(getStringTableIndex());
+ if ((EC = StrTabSecOrErr.getError()))
+ return;
+
+ ErrorOr<StringRef> SymtabOrErr = getStringTable(*StrTabSecOrErr);
if ((EC = SymtabOrErr.getError()))
return;
DotShstrtab = *SymtabOrErr;
template <class ELFT>
template <typename T>
const T *ELFFile<ELFT>::getEntry(uint32_t Section, uint32_t Entry) const {
- return getEntry<T>(getSection(Section), Entry);
+ ErrorOr<const Elf_Shdr *> Sec = getSection(Section);
+ if (std::error_code EC = Sec.getError())
+ report_fatal_error(EC.message());
+ return getEntry<T>(*Sec, Entry);
}
template <class ELFT>
}
template <class ELFT>
-const typename ELFFile<ELFT>::Elf_Shdr *
-ELFFile<ELFT>::getSection(uint32_t index) const {
- if (index == 0)
- return nullptr;
- if (!SectionHeaderTable || index >= getNumSections())
- // FIXME: Proper error handling.
- report_fatal_error("Invalid section index!");
+ErrorOr<const typename ELFFile<ELFT>::Elf_Shdr *>
+ELFFile<ELFT>::getSection(uint32_t Index) const {
+ assert(SectionHeaderTable && "SectionHeaderTable not initialized!");
+ if (Index >= getNumSections())
+ return object_error::invalid_section_index;
return reinterpret_cast<const Elf_Shdr *>(
- reinterpret_cast<const char *>(SectionHeaderTable)
- + (index * Header->e_shentsize));
+ reinterpret_cast<const char *>(SectionHeaderTable) +
+ (Index * Header->e_shentsize));
}
template <class ELFT>
/// \brief Get the relocation section that contains \a Rel.
const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
- return EF.getSection(Rel.d.a);
+ ErrorOr<const Elf_Shdr *> Sec = EF.getSection(Rel.d.a);
+ if (std::error_code EC = Sec.getError())
+ report_fatal_error(EC.message());
+ return *Sec;
}
const Elf_Rel *getRel(DataRefImpl Rel) const;
const Elf_Ehdr *Header = EF.getHeader();
if (Header->e_type == ELF::ET_REL) {
- const typename ELFFile<ELFT>::Elf_Shdr * Section = EF.getSection(ESym);
- if (Section != nullptr)
+ ErrorOr<const Elf_Shdr *> SectionOrErr = EF.getSection(ESym);
+ if (std::error_code EC = SectionOrErr.getError())
+ return EC;
+ const Elf_Shdr *Section = *SectionOrErr;
+ if (Section)
Result += Section->sh_addr;
}
template <class ELFT>
section_iterator
ELFObjectFile<ELFT>::getSymbolSection(const Elf_Sym *ESym) const {
- const Elf_Shdr *ESec = EF.getSection(ESym);
+ ErrorOr<const Elf_Shdr *> ESecOrErr = EF.getSection(ESym);
+ if (std::error_code EC = ESecOrErr.getError())
+ report_fatal_error(EC.message());
+
+ const Elf_Shdr *ESec = *ESecOrErr;
if (!ESec)
return section_end();
- else {
- DataRefImpl Sec;
- Sec.p = reinterpret_cast<intptr_t>(ESec);
- return section_iterator(SectionRef(Sec, this));
- }
+
+ DataRefImpl Sec;
+ Sec.p = reinterpret_cast<intptr_t>(ESec);
+ return section_iterator(SectionRef(Sec, this));
}
template <class ELFT>
if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
return section_end();
- const Elf_Shdr *R = EF.getSection(EShdr->sh_info);
- return section_iterator(SectionRef(toDRI(R), this));
+ ErrorOr<const Elf_Shdr *> R = EF.getSection(EShdr->sh_info);
+ if (std::error_code EC = R.getError())
+ report_fatal_error(EC.message());
+ return section_iterator(SectionRef(toDRI(*R), this));
}
// Relocations
if (!symbolIdx)
return symbol_end();
- const Elf_Shdr *SymSec = EF.getSection(sec->sh_link);
+ ErrorOr<const Elf_Shdr *> SymSecOrErr = EF.getSection(sec->sh_link);
+ if (std::error_code EC = SymSecOrErr.getError())
+ report_fatal_error(EC.message());
+ const Elf_Shdr *SymSec = *SymSecOrErr;
DataRefImpl SymbolData;
switch (SymSec->sh_type) {
if (Header->e_type == ELF::ET_REL) {
const Elf_Shdr *RelocationSec = getRelSection(Rel);
- const Elf_Shdr *RelocatedSec = EF.getSection(RelocationSec->sh_info);
- return ROffset + RelocatedSec->sh_addr;
+ ErrorOr<const Elf_Shdr *> RelocatedSec =
+ EF.getSection(RelocationSec->sh_info);
+ if (std::error_code EC = RelocatedSec.getError())
+ return EC;
+ return ROffset + (*RelocatedSec)->sh_addr;
}
return ROffset;
}
parse_failed,
unexpected_eof,
string_table_non_null_end,
+ invalid_section_index,
bitcode_section_not_found,
macho_small_load_command,
macho_load_segment_too_many_sections,
return "The end of the file was unexpectedly encountered";
case object_error::string_table_non_null_end:
return "String table must end with a null terminator";
+ case object_error::invalid_section_index:
+ return "Invalid section index";
case object_error::bitcode_section_not_found:
return "Bitcode section not found in object file";
case object_error::macho_small_load_command:
RUN: not llvm-readobj -t %p/Inputs/invalid-section-index.elf 2>&1 | FileCheck --check-prefix=INVALID-SECTION-INDEX %s
-INVALID-SECTION-INDEX: Invalid section index!
+INVALID-SECTION-INDEX: Invalid section index
RUN: not llvm-readobj -s %p/Inputs/invalid-section-size.elf 2>&1 | FileCheck --check-prefix=INVALID-SECTION-SIZE %s
INVALID-SECTION-SIZE: Invalid section header size
typedef typename ELFObjectFile<ELFT>::Elf_Shdr Elf_Shdr;
const ELFFile<ELFT> &EF = *Obj->getELFFile();
- const Elf_Shdr *sec = EF.getSection(Rel.d.a);
- const Elf_Shdr *SymTab = EF.getSection(sec->sh_link);
+ ErrorOr<const Elf_Shdr *> SecOrErr = EF.getSection(Rel.d.a);
+ if (std::error_code EC = SecOrErr.getError())
+ return EC;
+ const Elf_Shdr *Sec = *SecOrErr;
+ ErrorOr<const Elf_Shdr *> SymTabOrErr = EF.getSection(Sec->sh_link);
+ if (std::error_code EC = SymTabOrErr.getError())
+ return EC;
+ const Elf_Shdr *SymTab = *SymTabOrErr;
assert(SymTab->sh_type == ELF::SHT_SYMTAB ||
SymTab->sh_type == ELF::SHT_DYNSYM);
- const Elf_Shdr *StrTabSec = EF.getSection(SymTab->sh_link);
- ErrorOr<StringRef> StrTabOrErr = EF.getStringTable(StrTabSec);
+ ErrorOr<const Elf_Shdr *> StrTabSec = EF.getSection(SymTab->sh_link);
+ if (std::error_code EC = StrTabSec.getError())
+ return EC;
+ ErrorOr<StringRef> StrTabOrErr = EF.getStringTable(*StrTabSec);
if (std::error_code EC = StrTabOrErr.getError())
return EC;
StringRef StrTab = *StrTabOrErr;
StringRef res;
int64_t addend = 0;
uint16_t symbol_index = 0;
- switch (sec->sh_type) {
+ switch (Sec->sh_type) {
default:
return object_error::parse_failed;
case ELF::SHT_REL: {
}
}
const Elf_Sym *symb =
- EF.template getEntry<Elf_Sym>(sec->sh_link, symbol_index);
+ EF.template getEntry<Elf_Sym>(Sec->sh_link, symbol_index);
StringRef Target;
- const Elf_Shdr *SymSec = EF.getSection(symb);
+ ErrorOr<const Elf_Shdr *> SymSec = EF.getSection(symb);
+ if (std::error_code EC = SymSec.getError())
+ return EC;
if (symb->getType() == ELF::STT_SECTION) {
- ErrorOr<StringRef> SecName = EF.getSectionName(SymSec);
+ ErrorOr<StringRef> SecName = EF.getSectionName(*SymSec);
if (std::error_code EC = SecName.getError())
return EC;
Target = *SecName;
std::pair<const Elf_Shdr *, const Elf_Sym *> Symbol =
ELF->getRelocationSymbol(&Sec, &RelA);
- return ELF->getSection(Symbol.second);
+ ErrorOr<const Elf_Shdr *> Ret = ELF->getSection(Symbol.second);
+ if (std::error_code EC = Ret.getError())
+ report_fatal_error(EC.message());
+ return *Ret;
}
}
}
else {
if (SectionIndex == SHN_XINDEX)
SectionIndex = Obj.getExtendedSymbolTableIndex(&*Symbol);
- const typename ELFO::Elf_Shdr *Sec = Obj.getSection(SectionIndex);
- SectionName = errorOrDefault(Obj.getSectionName(Sec));
+ ErrorOr<const typename ELFO::Elf_Shdr *> Sec = Obj.getSection(SectionIndex);
+ if (!error(Sec.getError()))
+ SectionName = errorOrDefault(Obj.getSectionName(*Sec));
}
}
if (opts::SectionSymbols) {
ListScope D(W, "Symbols");
for (const typename ELFO::Elf_Sym &Sym : Obj->symbols()) {
- if (Obj->getSection(&Sym) == &Sec)
+ ErrorOr<const Elf_Shdr *> SymSec = Obj->getSection(&Sym);
+ if (!SymSec)
+ continue;
+ if (*SymSec == &Sec)
printSymbol(&Sym, false);
}
}
std::pair<const Elf_Shdr *, const Elf_Sym *> Sym =
Obj->getRelocationSymbol(Sec, &Rel);
if (Sym.second && Sym.second->getType() == ELF::STT_SECTION) {
- const Elf_Shdr *Sec = Obj->getSection(Sym.second);
- ErrorOr<StringRef> SecName = Obj->getSectionName(Sec);
- if (SecName)
- TargetName = SecName.get();
+ ErrorOr<const Elf_Shdr *> Sec = Obj->getSection(Sym.second);
+ if (!error(Sec.getError())) {
+ ErrorOr<StringRef> SecName = Obj->getSectionName(*Sec);
+ if (SecName)
+ TargetName = SecName.get();
+ }
} else if (Sym.first) {
const Elf_Shdr *SymTable = Sym.first;
- const Elf_Shdr *StrTableSec = Obj->getSection(SymTable->sh_link);
- ErrorOr<StringRef> StrTableOrErr = Obj->getStringTable(StrTableSec);
- if (!error(StrTableOrErr.getError()))
- TargetName = errorOrDefault(Sym.second->getName(*StrTableOrErr));
+ ErrorOr<const Elf_Shdr *> StrTableSec = Obj->getSection(SymTable->sh_link);
+ if (!error(StrTableSec.getError())) {
+ ErrorOr<StringRef> StrTableOrErr = Obj->getStringTable(*StrTableSec);
+ if (!error(StrTableOrErr.getError()))
+ TargetName = errorOrDefault(Sym.second->getName(*StrTableOrErr));
+ }
}
if (opts::ExpandRelocs) {
return EC;
S.Name = NameOrErr.get();
- const Elf_Shdr *Shdr = Obj.getSection(&*Sym);
+ ErrorOr<const Elf_Shdr *> ShdrOrErr = Obj.getSection(&*Sym);
+ if (std::error_code EC = ShdrOrErr.getError())
+ return EC;
+ const Elf_Shdr *Shdr = *ShdrOrErr;
if (!Shdr)
return obj2yaml_error::success;
return obj2yaml_error::success;
const Elf_Shdr *SymTab = NamePair.first;
- const Elf_Shdr *StrTabSec = Obj.getSection(SymTab->sh_link);
- ErrorOr<StringRef> StrTabOrErr = Obj.getStringTable(StrTabSec);
+ ErrorOr<const Elf_Shdr *> StrTabSec = Obj.getSection(SymTab->sh_link);
+ if (std::error_code EC = StrTabSec.getError())
+ return EC;
+ ErrorOr<StringRef> StrTabOrErr = Obj.getStringTable(*StrTabSec);
if (std::error_code EC = StrTabOrErr.getError())
return EC;
StringRef StrTab = *StrTabOrErr;
S.Name = NameOrErr.get();
if (Shdr->sh_link != ELF::SHN_UNDEF) {
- if (const Elf_Shdr *LinkSection = Obj.getSection(Shdr->sh_link)) {
- NameOrErr = Obj.getSectionName(LinkSection);
- if (std::error_code EC = NameOrErr.getError())
- return EC;
- S.Link = NameOrErr.get();
- }
+ ErrorOr<const Elf_Shdr *> LinkSection = Obj.getSection(Shdr->sh_link);
+ if (std::error_code EC = LinkSection.getError())
+ return EC;
+ NameOrErr = Obj.getSectionName(*LinkSection);
+ if (std::error_code EC = NameOrErr.getError())
+ return EC;
+ S.Link = NameOrErr.get();
}
return obj2yaml_error::success;
if (std::error_code EC = dumpCommonSection(Shdr, S))
return EC;
- if (const Elf_Shdr *InfoSection = Obj.getSection(Shdr->sh_info)) {
- ErrorOr<StringRef> NameOrErr = Obj.getSectionName(InfoSection);
- if (std::error_code EC = NameOrErr.getError())
- return EC;
- S.Info = NameOrErr.get();
- }
+ ErrorOr<const Elf_Shdr *> InfoSection = Obj.getSection(Shdr->sh_info);
+ if (std::error_code EC = InfoSection.getError())
+ return EC;
+
+ ErrorOr<StringRef> NameOrErr = Obj.getSectionName(*InfoSection);
+ if (std::error_code EC = NameOrErr.getError())
+ return EC;
+ S.Info = NameOrErr.get();
return obj2yaml_error::success;
}
return EC;
// Get sh_info which is the signature.
const Elf_Sym *symbol = Obj.getSymbol(Shdr->sh_info);
- const Elf_Shdr *symtab = Obj.getSection(Shdr->sh_link);
- const Elf_Shdr *StrTabSec = Obj.getSection(symtab->sh_link);
- ErrorOr<StringRef> StrTabOrErr = Obj.getStringTable(StrTabSec);
+ ErrorOr<const Elf_Shdr *> Symtab = Obj.getSection(Shdr->sh_link);
+ if (std::error_code EC = Symtab.getError())
+ return EC;
+ ErrorOr<const Elf_Shdr *> StrTabSec = Obj.getSection((*Symtab)->sh_link);
+ if (std::error_code EC = StrTabSec.getError())
+ return EC;
+ ErrorOr<StringRef> StrTabOrErr = Obj.getStringTable(*StrTabSec);
if (std::error_code EC = StrTabOrErr.getError())
return EC;
StringRef StrTab = *StrTabOrErr;
if (groupMembers[i] == llvm::ELF::GRP_COMDAT) {
s.sectionNameOrType = "GRP_COMDAT";
} else {
- const Elf_Shdr *sHdr = Obj.getSection(groupMembers[i]);
- ErrorOr<StringRef> sectionName = Obj.getSectionName(sHdr);
+ ErrorOr<const Elf_Shdr *> sHdr = Obj.getSection(groupMembers[i]);
+ if (std::error_code EC = sHdr.getError())
+ return EC;
+ ErrorOr<StringRef> sectionName = Obj.getSectionName(*sHdr);
if (std::error_code ec = sectionName.getError())
return ec;
s.sectionNameOrType = *sectionName;