Skip to content

Commit

Permalink
Return ErrorOr from getSymbolAddress.
Browse files Browse the repository at this point in the history
It can fail trying to get the section on ELF and COFF. This makes sure the
error is handled.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@241366 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
espindola committed Jul 3, 2015
1 parent 115b2eb commit 5954faa
Show file tree
Hide file tree
Showing 18 changed files with 80 additions and 61 deletions.
3 changes: 1 addition & 2 deletions include/llvm/Object/COFF.h
Original file line number Diff line number Diff line change
Expand Up @@ -648,8 +648,7 @@ class COFFObjectFile : public ObjectFile {
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;
Expand Down
13 changes: 6 additions & 7 deletions include/llvm/Object/ELFObjectFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,7 @@ template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {

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;
Expand Down Expand Up @@ -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();
Expand All @@ -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>
Expand Down
3 changes: 1 addition & 2 deletions include/llvm/Object/MachO.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,7 @@ class MachOObjectFile : public ObjectFile {
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;
Expand Down
9 changes: 4 additions & 5 deletions include/llvm/Object/ObjectFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class SymbolRef : public BasicSymbolRef {
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.
Expand Down Expand Up @@ -198,8 +198,7 @@ class ObjectFile : public SymbolicFile {
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;
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 7 additions & 1 deletion lib/DebugInfo/DWARF/DWARFContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
5 changes: 3 additions & 2 deletions lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 4 additions & 5 deletions lib/Object/COFFObjectFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 2 additions & 4 deletions lib/Object/MachOObjectFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions lib/Object/Object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 4 additions & 2 deletions lib/Target/X86/MCTargetDesc/X86ELFRelocationInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,16 @@ class X86_64ELFRelocationInfo : public MCRelocationInfo {
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.
Expand Down
5 changes: 3 additions & 2 deletions tools/llvm-cxxdump/llvm-cxxdump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion tools/llvm-nm/llvm-nm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
4 changes: 3 additions & 1 deletion tools/llvm-objdump/COFFDump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
21 changes: 11 additions & 10 deletions tools/llvm-objdump/llvm-objdump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
20 changes: 13 additions & 7 deletions tools/llvm-readobj/ARMWinEHPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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))
Expand All @@ -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));

Expand Down Expand Up @@ -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))
Expand Down
4 changes: 3 additions & 1 deletion tools/llvm-readobj/Win64EHDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
5 changes: 3 additions & 2 deletions tools/llvm-rtdyld/llvm-rtdyld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions tools/llvm-symbolizer/LLVMSymbolize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5954faa

Please sign in to comment.