Convert getSymbolSection to return an ErrorOr.

This function can actually fail since the symbol contains an index to the
section and that can be invalid.

llvm-svn: 244375
This commit is contained in:
Rafael Espindola 2015-08-07 23:27:14 +00:00
parent eeebc41b58
commit 8bab889b0f
22 changed files with 83 additions and 91 deletions

View File

@ -653,8 +653,7 @@ protected:
uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
uint32_t getSymbolFlags(DataRefImpl Symb) const override;
SymbolRef::Type getSymbolType(DataRefImpl Symb) const override;
std::error_code getSymbolSection(DataRefImpl Symb,
section_iterator &Res) const override;
ErrorOr<section_iterator> getSymbolSection(DataRefImpl Symb) const override;
void moveSectionNext(DataRefImpl &Sec) const override;
std::error_code getSectionName(DataRefImpl Sec,
StringRef &Res) const override;

View File

@ -204,9 +204,8 @@ protected:
uint8_t getSymbolOther(DataRefImpl Symb) const override;
uint8_t getSymbolELFType(DataRefImpl Symb) const override;
SymbolRef::Type getSymbolType(DataRefImpl Symb) const override;
section_iterator getSymbolSection(const Elf_Sym *Symb) const;
std::error_code getSymbolSection(DataRefImpl Symb,
section_iterator &Res) const override;
ErrorOr<section_iterator> getSymbolSection(const Elf_Sym *Symb) const;
ErrorOr<section_iterator> getSymbolSection(DataRefImpl Symb) const override;
void moveSectionNext(DataRefImpl &Sec) const override;
std::error_code getSectionName(DataRefImpl Sec,
@ -505,11 +504,11 @@ uint32_t ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Sym) const {
}
template <class ELFT>
section_iterator
ErrorOr<section_iterator>
ELFObjectFile<ELFT>::getSymbolSection(const Elf_Sym *ESym) const {
ErrorOr<const Elf_Shdr *> ESecOrErr = EF.getSection(ESym);
if (std::error_code EC = ESecOrErr.getError())
report_fatal_error(EC.message());
return EC;
const Elf_Shdr *ESec = *ESecOrErr;
if (!ESec)
@ -521,11 +520,9 @@ ELFObjectFile<ELFT>::getSymbolSection(const Elf_Sym *ESym) const {
}
template <class ELFT>
std::error_code
ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb,
section_iterator &Res) const {
Res = getSymbolSection(getSymbol(Symb));
return std::error_code();
ErrorOr<section_iterator>
ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb) const {
return getSymbolSection(getSymbol(Symb));
}
template <class ELFT>

View File

@ -210,8 +210,7 @@ public:
uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
SymbolRef::Type getSymbolType(DataRefImpl Symb) const override;
uint32_t getSymbolFlags(DataRefImpl Symb) const override;
std::error_code getSymbolSection(DataRefImpl Symb,
section_iterator &Res) const override;
ErrorOr<section_iterator> getSymbolSection(DataRefImpl Symb) const override;
unsigned getSymbolSectionID(SymbolRef Symb) const;
unsigned getSectionID(SectionRef Sec) const;

View File

@ -147,7 +147,7 @@ public:
/// @brief Get section this symbol is defined in reference to. Result is
/// end_sections() if it is undefined or is an absolute symbol.
std::error_code getSection(section_iterator &Result) const;
ErrorOr<section_iterator> getSection() const;
const ObjectFile *getObject() const;
};
@ -202,8 +202,8 @@ protected:
virtual uint32_t getSymbolAlignment(DataRefImpl Symb) const;
virtual uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const = 0;
virtual SymbolRef::Type getSymbolType(DataRefImpl Symb) const = 0;
virtual std::error_code getSymbolSection(DataRefImpl Symb,
section_iterator &Res) const = 0;
virtual ErrorOr<section_iterator>
getSymbolSection(DataRefImpl Symb) const = 0;
// Same as above for SectionRef.
friend class SectionRef;
@ -323,8 +323,8 @@ inline uint64_t SymbolRef::getCommonSize() const {
return getObject()->getCommonSymbolSize(getRawDataRefImpl());
}
inline std::error_code SymbolRef::getSection(section_iterator &Result) const {
return getObject()->getSymbolSection(getRawDataRefImpl(), Result);
inline ErrorOr<section_iterator> SymbolRef::getSection() const {
return getObject()->getSymbolSection(getRawDataRefImpl());
}
inline SymbolRef::Type SymbolRef::getType() const {

View File

@ -685,7 +685,7 @@ DWARFContextInMemory::DWARFContextInMemory(const object::ObjectFile &Obj,
}
SymAddr = *SymAddrOrErr;
// Also remember what section this symbol is in for later
Sym->getSection(RSec);
RSec = *Sym->getSection();
} else if (auto *MObj = dyn_cast<MachOObjectFile>(&Obj)) {
// MachO also has relocations that point to sections and
// scattered relocations.

View File

@ -164,8 +164,9 @@ RuntimeDyldImpl::loadObjectImpl(const object::ObjectFile &Obj) {
ErrorOr<StringRef> NameOrErr = I->getName();
Check(NameOrErr.getError());
StringRef Name = *NameOrErr;
section_iterator SI = Obj.section_end();
Check(I->getSection(SI));
ErrorOr<section_iterator> SIOrErr = I->getSection();
Check(SIOrErr.getError());
section_iterator SI = *SIOrErr;
if (SI == Obj.section_end())
continue;
uint64_t SectOffset;

View File

@ -845,8 +845,9 @@ void RuntimeDyldELF::findOPDEntrySection(const ELFObjectFileBase &Obj,
if (Rel.Addend != (int64_t)TargetSymbolOffset)
continue;
section_iterator tsi(Obj.section_end());
check(TargetSymbol->getSection(tsi));
ErrorOr<section_iterator> TSIOrErr = TargetSymbol->getSection();
check(TSIOrErr.getError());
section_iterator tsi = *TSIOrErr;
bool IsCode = tsi->isText();
Rel.SectionID = findOrEmitSection(Obj, (*tsi), IsCode, LocalSections);
Rel.Addend = (intptr_t)Addend;
@ -1162,8 +1163,7 @@ relocation_iterator RuntimeDyldELF::processRelocationRef(
// TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously
// and can be changed by another developers. Maybe best way is add
// a new symbol type ST_Section to SymbolRef and use it.
section_iterator si(Obj.section_end());
Symbol->getSection(si);
section_iterator si = *Symbol->getSection();
if (si == Obj.section_end())
llvm_unreachable("Symbol section not found, bad object file format!");
DEBUG(dbgs() << "\t\tThis is section symbol\n");

View File

@ -119,8 +119,7 @@ public:
symbol_iterator Symbol = RelI->getSymbol();
if (Symbol == Obj.symbol_end())
report_fatal_error("Unknown symbol in relocation");
section_iterator SecI(Obj.section_end());
Symbol->getSection(SecI);
section_iterator SecI = *Symbol->getSection();
// If there is no section, this must be an external reference.
const bool IsExtern = SecI == Obj.section_end();

View File

@ -238,21 +238,17 @@ uint64_t COFFObjectFile::getCommonSymbolSizeImpl(DataRefImpl Ref) const {
return Symb.getValue();
}
std::error_code
COFFObjectFile::getSymbolSection(DataRefImpl Ref,
section_iterator &Result) const {
ErrorOr<section_iterator>
COFFObjectFile::getSymbolSection(DataRefImpl Ref) const {
COFFSymbolRef Symb = getCOFFSymbol(Ref);
if (COFF::isReservedSectionNumber(Symb.getSectionNumber())) {
Result = section_end();
} else {
const coff_section *Sec = nullptr;
if (std::error_code EC = getSection(Symb.getSectionNumber(), Sec))
return EC;
DataRefImpl Ref;
Ref.p = reinterpret_cast<uintptr_t>(Sec);
Result = section_iterator(SectionRef(Ref, this));
}
return std::error_code();
if (COFF::isReservedSectionNumber(Symb.getSectionNumber()))
return section_end();
const coff_section *Sec = nullptr;
if (std::error_code EC = getSection(Symb.getSectionNumber(), Sec))
return EC;
DataRefImpl Ret;
Ret.p = reinterpret_cast<uintptr_t>(Sec);
return section_iterator(SectionRef(Ret, this));
}
unsigned COFFObjectFile::getSymbolSectionID(SymbolRef Sym) const {

View File

@ -445,22 +445,18 @@ uint32_t MachOObjectFile::getSymbolFlags(DataRefImpl DRI) const {
return Result;
}
std::error_code MachOObjectFile::getSymbolSection(DataRefImpl Symb,
section_iterator &Res) const {
ErrorOr<section_iterator>
MachOObjectFile::getSymbolSection(DataRefImpl Symb) const {
MachO::nlist_base Entry = getSymbolTableEntryBase(this, Symb);
uint8_t index = Entry.n_sect;
if (index == 0) {
Res = section_end();
} else {
DataRefImpl DRI;
DRI.d.a = index - 1;
if (DRI.d.a >= Sections.size())
report_fatal_error("getSymbolSection: Invalid section index.");
Res = section_iterator(SectionRef(DRI, this));
}
return std::error_code();
if (index == 0)
return section_end();
DataRefImpl DRI;
DRI.d.a = index - 1;
if (DRI.d.a >= Sections.size())
report_fatal_error("getSymbolSection: Invalid section index.");
return section_iterator(SectionRef(DRI, this));
}
unsigned MachOObjectFile::getSymbolSectionID(SymbolRef Sym) const {

View File

@ -98,8 +98,10 @@ void LLVMMoveToNextSection(LLVMSectionIteratorRef SI) {
void LLVMMoveToContainingSection(LLVMSectionIteratorRef Sect,
LLVMSymbolIteratorRef Sym) {
if (std::error_code ec = (*unwrap(Sym))->getSection(*unwrap(Sect)))
ErrorOr<section_iterator> SecOrErr = (*unwrap(Sym))->getSection();
if (std::error_code ec = SecOrErr.getError())
report_fatal_error(ec.message());
*unwrap(Sect) = *SecOrErr;
}
// ObjectFile Symbol iterators

View File

@ -29,10 +29,10 @@ ObjectFile::ObjectFile(unsigned int Type, MemoryBufferRef Source)
: SymbolicFile(Type, Source) {}
bool SectionRef::containsSymbol(SymbolRef S) const {
section_iterator SymSec = getObject()->section_end();
if (S.getSection(SymSec))
ErrorOr<section_iterator> SymSec = S.getSection();
if (!SymSec)
return false;
return *this == *SymSec;
return *this == **SymSec;
}
uint64_t ObjectFile::getSymbolValue(DataRefImpl Ref) const {

View File

@ -265,8 +265,13 @@ void MachODebugMapParser::loadMainBinarySymbols(
// are the only ones that need to be queried because the address
// of common data won't be described in the debug map. All other
// addresses should be fetched for the debug map.
if (!(Sym.getFlags() & SymbolRef::SF_Global) || Sym.getSection(Section) ||
Section == MainBinary.section_end() || Section->isText())
if (!(Sym.getFlags() & SymbolRef::SF_Global))
continue;
ErrorOr<section_iterator> SectionOrErr = Sym.getSection();
if (!SectionOrErr)
continue;
Section = *SectionOrErr;
if (Section == MainBinary.section_end() || Section->isText())
continue;
uint64_t Addr = Sym.getValue();
ErrorOr<StringRef> NameOrErr = Sym.getName();

View File

@ -184,8 +184,9 @@ static void dumpCXXData(const ObjectFile *Obj) {
ErrorOr<StringRef> SymNameOrErr = Sym.getName();
error(SymNameOrErr.getError());
StringRef SymName = *SymNameOrErr;
object::section_iterator SecI(Obj->section_begin());
error(Sym.getSection(SecI));
ErrorOr<object::section_iterator> SecIOrErr = Sym.getSection();
error(SecIOrErr.getError());
object::section_iterator SecI = *SecIOrErr;
// Skip external symbols.
if (SecI == Obj->section_end())
continue;

View File

@ -314,8 +314,7 @@ static void darwinPrintSymbol(MachOObjectFile *MachO, SymbolListT::iterator I,
outs() << "(indirect) ";
break;
case MachO::N_SECT: {
section_iterator Sec = MachO->section_end();
MachO->getSymbolSection(I->Sym.getRawDataRefImpl(), Sec);
section_iterator Sec = *MachO->getSymbolSection(I->Sym.getRawDataRefImpl());
DataRefImpl Ref = Sec->getRawDataRefImpl();
StringRef SectionName;
MachO->getSectionName(Ref, SectionName);
@ -594,10 +593,11 @@ static char getSymbolNMTypeChar(ELFObjectFileBase &Obj,
// OK, this is ELF
elf_symbol_iterator SymI(I);
elf_section_iterator SecI = Obj.section_end();
if (error(SymI->getSection(SecI)))
ErrorOr<elf_section_iterator> SecIOrErr = SymI->getSection();
if (error(SecIOrErr.getError()))
return '?';
elf_section_iterator SecI = *SecIOrErr;
if (SecI != Obj.section_end()) {
switch (SecI->getType()) {
case ELF::SHT_PROGBITS:
@ -651,9 +651,10 @@ static char getSymbolNMTypeChar(COFFObjectFile &Obj, symbol_iterator I) {
uint32_t Characteristics = 0;
if (!COFF::isReservedSectionNumber(Symb.getSectionNumber())) {
section_iterator SecI = Obj.section_end();
if (error(SymI->getSection(SecI)))
ErrorOr<section_iterator> SecIOrErr = SymI->getSection();
if (error(SecIOrErr.getError()))
return '?';
section_iterator SecI = *SecIOrErr;
const coff_section *Section = Obj.getCOFFSection(*SecI);
Characteristics = Section->Characteristics;
}
@ -701,8 +702,7 @@ static char getSymbolNMTypeChar(MachOObjectFile &Obj, basic_symbol_iterator I) {
case MachO::N_INDR:
return 'i';
case MachO::N_SECT: {
section_iterator Sec = Obj.section_end();
Obj.getSymbolSection(Symb, Sec);
section_iterator Sec = *Obj.getSymbolSection(Symb);
DataRefImpl Ref = Sec->getRawDataRefImpl();
StringRef SectionName;
Obj.getSectionName(Ref, SectionName);

View File

@ -165,10 +165,10 @@ resolveSectionAndAddress(const COFFObjectFile *Obj, const SymbolRef &Sym,
if (std::error_code EC = ResolvedAddrOrErr.getError())
return EC;
ResolvedAddr = *ResolvedAddrOrErr;
section_iterator iter(Obj->section_begin());
if (std::error_code EC = Sym.getSection(iter))
ErrorOr<section_iterator> Iter = Sym.getSection();
if (std::error_code EC = Iter.getError())
return EC;
ResolvedSection = Obj->getCOFFSection(*iter);
ResolvedSection = Obj->getCOFFSection(**Iter);
return std::error_code();
}

View File

@ -6436,8 +6436,7 @@ static void findUnwindRelocNameAddend(const MachOObjectFile *Obj,
// Go back one so that SymbolAddress <= Addr.
--Sym;
section_iterator SymSection = Obj->section_end();
Sym->second.getSection(SymSection);
section_iterator SymSection = *Sym->second.getSection();
if (RelocSection == *SymSection) {
// There's a valid symbol in the same section before this reference.
ErrorOr<StringRef> NameOrErr = Sym->second.getName();
@ -6780,8 +6779,7 @@ void llvm::printMachOUnwindInfo(const MachOObjectFile *Obj) {
for (const SymbolRef &SymRef : Obj->symbols()) {
// Discard any undefined or absolute symbols. They're not going to take part
// in the convenience lookup for unwind info and just take up resources.
section_iterator Section = Obj->section_end();
SymRef.getSection(Section);
section_iterator Section = *SymRef.getSection();
if (Section == Obj->section_end())
continue;

View File

@ -1225,8 +1225,9 @@ void llvm::PrintSymbolTable(const ObjectFile *o) {
uint64_t Address = *AddressOrError;
SymbolRef::Type Type = Symbol.getType();
uint32_t Flags = Symbol.getFlags();
section_iterator Section = o->section_end();
error(Symbol.getSection(Section));
ErrorOr<section_iterator> SectionOrErr = Symbol.getSection();
error(SectionOrErr.getError());
section_iterator Section = *SectionOrErr;
StringRef Name;
if (Type == SymbolRef::ST_Debug && Section != o->section_end()) {
Section->getName(Name);

View File

@ -630,9 +630,10 @@ bool Decoder::dumpUnpackedEntry(const COFFObjectFile &COFF,
SW.printString("ExceptionRecord", formatSymbol(*Name, Address));
section_iterator SI = COFF.section_end();
if (XDataRecord->getSection(SI))
ErrorOr<section_iterator> SIOrErr = XDataRecord->getSection();
if (!SIOrErr)
return false;
section_iterator SI = *SIOrErr;
return dumpXDataRecord(COFF, *SI, FunctionAddress, Address);
} else {

View File

@ -542,8 +542,9 @@ void MachODumper::printSymbol(const SymbolRef &Symbol) {
getSymbol(Obj, Symbol.getRawDataRefImpl(), MOSymbol);
StringRef SectionName = "";
section_iterator SecI(Obj->section_begin());
error(Symbol.getSection(SecI));
ErrorOr<section_iterator> SecIOrErr = Symbol.getSection();
error(SecIOrErr.getError());
section_iterator SecI = *SecIOrErr;
if (SecI != Obj->section_end())
error(SecI->getName(SectionName));

View File

@ -149,11 +149,8 @@ static std::error_code resolveRelocation(const Dumper::Context &Ctx,
return EC;
ResolvedAddress = *ResolvedAddressOrErr;
section_iterator SI = Ctx.COFF.section_begin();
if (std::error_code EC = Symbol.getSection(SI))
return EC;
ResolvedSection = Ctx.COFF.getCOFFSection(*SI);
ErrorOr<section_iterator> SI = Symbol.getSection();
ResolvedSection = Ctx.COFF.getCOFFSection(**SI);
return std::error_code();
}

View File

@ -304,8 +304,7 @@ static int printLineInfoForInput(bool LoadObjects, bool UseDebugObj) {
// symbol in memory (rather than that in the unrelocated object file)
// and use that to query the DWARFContext.
if (!UseDebugObj && LoadObjects) {
object::section_iterator Sec(SymbolObj->section_end());
Sym.getSection(Sec);
object::section_iterator Sec = *Sym.getSection();
StringRef SecName;
Sec->getName(SecName);
uint64_t SectionLoadAddress =