Return ErrorOr from getSymbolAddress.

It can fail trying to get the section on ELF and COFF. This makes sure the
error is handled.

llvm-svn: 241366
This commit is contained in:
Rafael Espindola 2015-07-03 18:19:00 +00:00
parent 7af299bc9a
commit 06691d6e5a
18 changed files with 80 additions and 61 deletions

View File

@ -648,8 +648,7 @@ public:
protected:
void moveSymbolNext(DataRefImpl &Symb) const override;
ErrorOr<StringRef> getSymbolName(DataRefImpl Symb) const override;
std::error_code getSymbolAddress(DataRefImpl Symb,
uint64_t &Res) const override;
ErrorOr<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
uint64_t getSymbolValue(DataRefImpl Symb) const override;
uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
uint32_t getSymbolFlags(DataRefImpl Symb) const override;

View File

@ -196,8 +196,7 @@ protected:
void moveSymbolNext(DataRefImpl &Symb) const override;
ErrorOr<StringRef> getSymbolName(DataRefImpl Symb) const override;
std::error_code getSymbolAddress(DataRefImpl Symb,
uint64_t &Res) const override;
ErrorOr<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
uint64_t getSymbolValue(DataRefImpl Symb) const override;
uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
@ -400,15 +399,15 @@ uint64_t ELFObjectFile<ELFT>::getSymbolValue(DataRefImpl Symb) const {
}
template <class ELFT>
std::error_code ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb,
uint64_t &Result) const {
Result = getSymbolValue(Symb);
ErrorOr<uint64_t>
ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb) const {
uint64_t Result = getSymbolValue(Symb);
const Elf_Sym *ESym = getSymbol(Symb);
switch (ESym->st_shndx) {
case ELF::SHN_COMMON:
case ELF::SHN_UNDEF:
case ELF::SHN_ABS:
return std::error_code();
return Result;
}
const Elf_Ehdr *Header = EF.getHeader();
@ -422,7 +421,7 @@ std::error_code ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb,
Result += Section->sh_addr;
}
return std::error_code();
return Result;
}
template <class ELFT>

View File

@ -205,8 +205,7 @@ public:
std::error_code getIndirectName(DataRefImpl Symb, StringRef &Res) const;
unsigned getSectionType(SectionRef Sec) const;
std::error_code getSymbolAddress(DataRefImpl Symb,
uint64_t &Res) const override;
ErrorOr<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
uint64_t getSymbolValue(DataRefImpl Symb) const override;
uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;

View File

@ -135,7 +135,7 @@ public:
ErrorOr<StringRef> getName() const;
/// Returns the symbol virtual address (i.e. address at which it will be
/// mapped).
std::error_code getAddress(uint64_t &Result) const;
ErrorOr<uint64_t> getAddress() const;
/// Return the value of the symbol depending on the object this can be an
/// offset or a virtual address.
@ -198,8 +198,7 @@ protected:
virtual ErrorOr<StringRef> getSymbolName(DataRefImpl Symb) const = 0;
std::error_code printSymbolName(raw_ostream &OS,
DataRefImpl Symb) const override;
virtual std::error_code getSymbolAddress(DataRefImpl Symb,
uint64_t &Res) const = 0;
virtual ErrorOr<uint64_t> getSymbolAddress(DataRefImpl Symb) const = 0;
virtual uint64_t getSymbolValue(DataRefImpl Symb) const = 0;
virtual uint32_t getSymbolAlignment(DataRefImpl Symb) const;
virtual uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const = 0;
@ -308,8 +307,8 @@ inline ErrorOr<StringRef> SymbolRef::getName() const {
return getObject()->getSymbolName(getRawDataRefImpl());
}
inline std::error_code SymbolRef::getAddress(uint64_t &Result) const {
return getObject()->getSymbolAddress(getRawDataRefImpl(), Result);
inline ErrorOr<uint64_t> SymbolRef::getAddress() const {
return getObject()->getSymbolAddress(getRawDataRefImpl());
}
inline uint64_t SymbolRef::getValue() const {

View File

@ -677,7 +677,13 @@ DWARFContextInMemory::DWARFContextInMemory(const object::ObjectFile &Obj,
// First calculate the address of the symbol or section as it appears
// in the objct file
if (Sym != Obj.symbol_end()) {
Sym->getAddress(SymAddr);
ErrorOr<uint64_t> SymAddrOrErr = Sym->getAddress();
if (std::error_code EC = SymAddrOrErr.getError()) {
errs() << "error: failed to compute symbol address: "
<< EC.message() << '\n';
continue;
}
SymAddr = *SymAddrOrErr;
// Also remember what section this symbol is in for later
Sym->getSection(RSec);
} else if (auto *MObj = dyn_cast<MachOObjectFile>(&Obj)) {

View File

@ -114,9 +114,10 @@ void RuntimeDyldImpl::mapSectionAddress(const void *LocalAddress,
}
static std::error_code getOffset(const SymbolRef &Sym, uint64_t &Result) {
uint64_t Address;
if (std::error_code EC = Sym.getAddress(Address))
ErrorOr<uint64_t> AddressOrErr = Sym.getAddress();
if (std::error_code EC = AddressOrErr.getError())
return EC;
uint64_t Address = *AddressOrErr;
if (Address == UnknownAddress) {
Result = UnknownAddress;

View File

@ -163,21 +163,20 @@ uint64_t COFFObjectFile::getSymbolValue(DataRefImpl Ref) const {
return Sym.getValue();
}
std::error_code COFFObjectFile::getSymbolAddress(DataRefImpl Ref,
uint64_t &Result) const {
Result = getSymbolValue(Ref);
ErrorOr<uint64_t> COFFObjectFile::getSymbolAddress(DataRefImpl Ref) const {
uint64_t Result = getSymbolValue(Ref);
COFFSymbolRef Symb = getCOFFSymbol(Ref);
int32_t SectionNumber = Symb.getSectionNumber();
if (Symb.isAnyUndefined() || Symb.isCommon() ||
COFF::isReservedSectionNumber(SectionNumber))
return std::error_code();
return Result;
const coff_section *Section = nullptr;
if (std::error_code EC = getSection(SectionNumber, Section))
return EC;
Result += Section->VirtualAddress;
return std::error_code();
return Result;
}
SymbolRef::Type COFFObjectFile::getSymbolType(DataRefImpl Ref) const {

View File

@ -376,10 +376,8 @@ uint64_t MachOObjectFile::getSymbolValue(DataRefImpl Sym) const {
return NValue;
}
std::error_code MachOObjectFile::getSymbolAddress(DataRefImpl Sym,
uint64_t &Res) const {
Res = getSymbolValue(Sym);
return std::error_code();
ErrorOr<uint64_t> MachOObjectFile::getSymbolAddress(DataRefImpl Sym) const {
return getSymbolValue(Sym);
}
uint32_t MachOObjectFile::getSymbolAlignment(DataRefImpl DRI) const {

View File

@ -180,10 +180,10 @@ const char *LLVMGetSymbolName(LLVMSymbolIteratorRef SI) {
}
uint64_t LLVMGetSymbolAddress(LLVMSymbolIteratorRef SI) {
uint64_t ret;
if (std::error_code ec = (*unwrap(SI))->getAddress(ret))
report_fatal_error(ec.message());
return ret;
ErrorOr<uint64_t> Ret = (*unwrap(SI))->getAddress();
if (std::error_code EC = Ret.getError())
report_fatal_error(EC.message());
return *Ret;
}
uint64_t LLVMGetSymbolSize(LLVMSymbolIteratorRef SI) {

View File

@ -34,14 +34,16 @@ public:
report_fatal_error(EC.message());
StringRef SymName = *SymNameOrErr;
uint64_t SymAddr; SymI->getAddress(SymAddr);
ErrorOr<uint64_t> SymAddr = SymI->getAddress();
if (std::error_code EC = SymAddr.getError())
report_fatal_error(EC.message());
uint64_t SymSize = SymI->getSize();
int64_t Addend = *ELFRelocationRef(Rel).getAddend();
MCSymbol *Sym = Ctx.getOrCreateSymbol(SymName);
// FIXME: check that the value is actually the same.
if (!Sym->isVariable())
Sym->setVariableValue(MCConstantExpr::create(SymAddr, Ctx));
Sym->setVariableValue(MCConstantExpr::create(*SymAddr, Ctx));
const MCExpr *Expr = nullptr;
// If hasAddend is true, then we need to add Addend (r_addend) to Expr.

View File

@ -207,9 +207,10 @@ static void dumpCXXData(const ObjectFile *Obj) {
StringRef SecContents;
if (error(Sec.getContents(SecContents)))
return;
uint64_t SymAddress;
if (error(Sym.getAddress(SymAddress)))
ErrorOr<uint64_t> SymAddressOrErr = Sym.getAddress();
if (error(SymAddressOrErr.getError()))
return;
uint64_t SymAddress = *SymAddressOrErr;
uint64_t SecAddress = Sec.getAddress();
uint64_t SecSize = Sec.getSize();
uint64_t SymOffset = SymAddress - SecAddress;

View File

@ -901,8 +901,10 @@ static void dumpSymbolNamesFromObject(SymbolicFile &Obj, bool printName,
S.Size = ELFSymbolRef(Sym).getSize();
}
if (PrintAddress && isa<ObjectFile>(Obj)) {
if (error(SymbolRef(Sym).getAddress(S.Address)))
ErrorOr<uint64_t> AddressOrErr = SymbolRef(Sym).getAddress();
if (error(AddressOrErr.getError()))
break;
S.Address = *AddressOrErr;
}
S.TypeChar = getNMTypeChar(Obj, Sym);
if (error(Sym.printName(OS)))

View File

@ -161,8 +161,10 @@ static std::error_code
resolveSectionAndAddress(const COFFObjectFile *Obj, const SymbolRef &Sym,
const coff_section *&ResolvedSection,
uint64_t &ResolvedAddr) {
if (std::error_code EC = Sym.getAddress(ResolvedAddr))
ErrorOr<uint64_t> ResolvedAddrOrErr = Sym.getAddress();
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))
return EC;

View File

@ -455,13 +455,12 @@ static void printRelocationTargetName(const MachOObjectFile *O,
for (const SymbolRef &Symbol : O->symbols()) {
std::error_code ec;
uint64_t Addr;
ErrorOr<StringRef> Name = Symbol.getName();
if ((ec = Symbol.getAddress(Addr)))
ErrorOr<uint64_t> Addr = Symbol.getAddress();
if ((ec = Addr.getError()))
report_fatal_error(ec.message());
if (Addr != Val)
if (*Addr != Val)
continue;
ErrorOr<StringRef> Name = Symbol.getName();
if (std::error_code EC = Name.getError())
report_fatal_error(EC.message());
fmt << *Name;
@ -824,9 +823,10 @@ static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
std::vector<std::pair<uint64_t, StringRef>> Symbols;
for (const SymbolRef &Symbol : Obj->symbols()) {
if (Section.containsSymbol(Symbol)) {
uint64_t Address;
if (error(Symbol.getAddress(Address)))
ErrorOr<uint64_t> AddressOrErr = Symbol.getAddress();
if (error(AddressOrErr.getError()))
break;
uint64_t Address = *AddressOrErr;
if (Address == UnknownAddress)
continue;
Address -= SectionAddr;
@ -1113,12 +1113,13 @@ void llvm::PrintSymbolTable(const ObjectFile *o) {
return;
}
for (const SymbolRef &Symbol : o->symbols()) {
uint64_t Address;
ErrorOr<uint64_t> AddressOrError = Symbol.getAddress();
if (error(AddressOrError.getError()))
continue;
uint64_t Address = *AddressOrError;
SymbolRef::Type Type = Symbol.getType();
uint32_t Flags = Symbol.getFlags();
section_iterator Section = o->section_end();
if (error(Symbol.getAddress(Address)))
continue;
if (error(Symbol.getSection(Section)))
continue;
StringRef Name;

View File

@ -201,10 +201,10 @@ ErrorOr<object::SymbolRef> Decoder::getSymbol(const COFFObjectFile &COFF,
if (FunctionOnly && Symbol.getType() != SymbolRef::ST_Function)
continue;
uint64_t Address;
if (std::error_code EC = Symbol.getAddress(Address))
ErrorOr<uint64_t> Address = Symbol.getAddress();
if (std::error_code EC = Address.getError())
return EC;
if (Address == VA)
if (*Address == VA)
return Symbol;
}
return readobj_error::unknown_symbol;
@ -605,7 +605,10 @@ bool Decoder::dumpUnpackedEntry(const COFFObjectFile &COFF,
if (std::error_code EC = FunctionNameOrErr.getError())
report_fatal_error(EC.message());
FunctionName = *FunctionNameOrErr;
Function->getAddress(FunctionAddress);
ErrorOr<uint64_t> FunctionAddressOrErr = Function->getAddress();
if (std::error_code EC = FunctionAddressOrErr.getError())
report_fatal_error(EC.message());
FunctionAddress = *FunctionAddressOrErr;
} else {
const pe32_header *PEHeader;
if (COFF.getPE32Header(PEHeader))
@ -620,8 +623,10 @@ bool Decoder::dumpUnpackedEntry(const COFFObjectFile &COFF,
if (std::error_code EC = Name.getError())
report_fatal_error(EC.message());
uint64_t Address;
XDataRecord->getAddress(Address);
ErrorOr<uint64_t> AddressOrErr = XDataRecord->getAddress();
if (std::error_code EC = AddressOrErr.getError())
report_fatal_error(EC.message());
uint64_t Address = *AddressOrErr;
SW.printString("ExceptionRecord", formatSymbol(*Name, Address));
@ -666,7 +671,8 @@ bool Decoder::dumpPackedEntry(const object::COFFObjectFile &COFF,
if (std::error_code EC = FunctionNameOrErr.getError())
report_fatal_error(EC.message());
FunctionName = *FunctionNameOrErr;
Function->getAddress(FunctionAddress);
ErrorOr<uint64_t> FunctionAddressOrErr = Function->getAddress();
FunctionAddress = *FunctionAddressOrErr;
} else {
const pe32_header *PEHeader;
if (COFF.getPE32Header(PEHeader))

View File

@ -144,8 +144,10 @@ static std::error_code resolveRelocation(const Dumper::Context &Ctx,
Ctx.ResolveSymbol(Section, Offset, Symbol, Ctx.UserData))
return EC;
if (std::error_code EC = Symbol.getAddress(ResolvedAddress))
ErrorOr<uint64_t> ResolvedAddressOrErr = Symbol.getAddress();
if (std::error_code EC = ResolvedAddressOrErr.getError())
return EC;
ResolvedAddress = *ResolvedAddressOrErr;
section_iterator SI = Ctx.COFF.section_begin();
if (std::error_code EC = Symbol.getSection(SI))

View File

@ -269,9 +269,10 @@ static int printLineInfoForInput(bool LoadObjects, bool UseDebugObj) {
ErrorOr<StringRef> Name = Sym.getName();
if (!Name)
continue;
uint64_t Addr;
if (Sym.getAddress(Addr))
ErrorOr<uint64_t> AddrOrErr = Sym.getAddress();
if (!AddrOrErr)
continue;
uint64_t Addr = *AddrOrErr;
uint64_t Size = P.second;
// If we're not using the debug object, compute the address of the

View File

@ -84,9 +84,11 @@ void ModuleInfo::addSymbol(const SymbolRef &Symbol, uint64_t SymbolSize,
SymbolRef::Type SymbolType = Symbol.getType();
if (SymbolType != SymbolRef::ST_Function && SymbolType != SymbolRef::ST_Data)
return;
uint64_t SymbolAddress;
if (error(Symbol.getAddress(SymbolAddress)) ||
SymbolAddress == UnknownAddress)
ErrorOr<uint64_t> SymbolAddressOrErr = Symbol.getAddress();
if (error(SymbolAddressOrErr.getError()))
return;
uint64_t SymbolAddress = *SymbolAddressOrErr;
if (SymbolAddress == UnknownAddress)
return;
if (OpdExtractor) {
// For big-endian PowerPC64 ELF, symbols in the .opd section refer to