Return ErrorOr from getSection.

This also improves the logic of what is an error:

* getSection(uint_32): only return an error if the index is out of bounds. The
  index 0 corresponds to a perfectly valid entry.
* getSection(Elf_Sym): Returns null for symbols that normally don't have
  sections and error for out of bound indexes.

In many places this just moves the report_fatal_error up the stack, but those
can then be fixed in smaller patches.

llvm-svn: 241156
This commit is contained in:
Rafael Espindola 2015-07-01 12:56:27 +00:00
parent 6ae400d122
commit 6def304209
9 changed files with 144 additions and 75 deletions

View File

@ -357,8 +357,8 @@ public:
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;
@ -465,11 +465,12 @@ ELFFile<ELFT>::getExtendedSymbolTableIndex(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);
}
@ -532,7 +533,10 @@ std::pair<const typename ELFFile<ELFT>::Elf_Shdr *,
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())));
}
@ -615,7 +619,10 @@ ELFFile<ELFT>::ELFFile(StringRef Object, std::error_code &EC)
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;
@ -629,7 +636,10 @@ ELFFile<ELFT>::ELFFile(StringRef Object, std::error_code &EC)
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;
@ -673,8 +683,11 @@ ELFFile<ELFT>::ELFFile(StringRef Object, std::error_code &EC)
}
// 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;
@ -825,7 +838,10 @@ StringRef ELFFile<ELFT>::getLoadName() const {
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>
@ -837,17 +853,15 @@ const T *ELFFile<ELFT>::getEntry(const Elf_Shdr *Section,
}
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>

View File

@ -241,7 +241,10 @@ protected:
/// \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;
@ -406,8 +409,11 @@ std::error_code ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb,
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;
}
@ -511,14 +517,17 @@ uint32_t ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Sym) const {
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>
@ -629,8 +638,10 @@ ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
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
@ -651,7 +662,10 @@ ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const {
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) {
@ -676,8 +690,11 @@ ELFObjectFile<ELFT>::getRelocationAddress(DataRefImpl Rel) const {
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;
}

View File

@ -28,6 +28,7 @@ enum class object_error {
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,

View File

@ -43,6 +43,8 @@ std::string _object_error_category::message(int EV) const {
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:

View File

@ -40,7 +40,7 @@ INVALID-SYM-SIZE: Invalid symbol size
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

View File

@ -319,12 +319,20 @@ static std::error_code getRelocationValueString(const ELFObjectFile<ELFT> *Obj,
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;
@ -332,7 +340,7 @@ static std::error_code getRelocationValueString(const ELFObjectFile<ELFT> *Obj,
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: {
@ -349,11 +357,13 @@ static std::error_code getRelocationValueString(const ELFObjectFile<ELFT> *Obj,
}
}
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;

View File

@ -377,7 +377,10 @@ PrinterContext<ET>::FindExceptionTable(unsigned IndexSectionIndex,
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;
}
}
}

View File

@ -162,8 +162,9 @@ getSectionNameIndex(const ELFO &Obj, const typename ELFO::Elf_Sym *Symbol,
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));
}
}
@ -644,7 +645,10 @@ void ELFDumper<ELFT>::printSections() {
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);
}
}
@ -746,16 +750,20 @@ void ELFDumper<ELFT>::printRelocation(const Elf_Shdr *Sec,
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) {

View File

@ -157,7 +157,10 @@ std::error_code ELFDumper<ELFT>::dumpSymbol(const Elf_Sym *Sym, bool IsDynamic,
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;
@ -183,8 +186,10 @@ std::error_code ELFDumper<ELFT>::dumpRelocation(const Elf_Shdr *Shdr,
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;
@ -211,12 +216,13 @@ std::error_code ELFDumper<ELFT>::dumpCommonSection(const Elf_Shdr *Shdr,
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;
@ -229,12 +235,14 @@ ELFDumper<ELFT>::dumpCommonRelocationSection(const Elf_Shdr *Shdr,
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;
}
@ -304,9 +312,13 @@ ErrorOr<ELFYAML::Group *> ELFDumper<ELFT>::dumpGroup(const Elf_Shdr *Shdr) {
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;
@ -325,8 +337,10 @@ ErrorOr<ELFYAML::Group *> ELFDumper<ELFT>::dumpGroup(const Elf_Shdr *Shdr) {
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;