Skip to content

Commit

Permalink
Thread Expected<...> up from libObject’s getSymbolAddress() for symbo…
Browse files Browse the repository at this point in the history
…ls to allow

a good error message to be produced.

This is nearly the last libObject interface that used ErrorOr and the last one
that appears in llvm/include/llvm/Object/MachO.h .  For Mach-O objects this is
just a clean up because it’s version of getSymbolAddress() can’t return an
error.

I will leave it to the experts on COFF and ELF to actually add meaning full
error messages in their tests if they wish.  And also leave it to these experts
to change the last two ErrorOr interfaces in llvm/include/llvm/Object/ObjectFile.h
for createCOFFObjectFile() and createELFObjectFile() if they wish.

Since there are no test cases for COFF and ELF error cases with respect to
getSymbolAddress() in the test suite this is no functional change (NFC).


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@273701 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
enderby committed Jun 24, 2016
1 parent 546c8eb commit 317de7c
Show file tree
Hide file tree
Showing 18 changed files with 92 additions and 55 deletions.
2 changes: 1 addition & 1 deletion include/llvm/Object/COFF.h
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ class COFFObjectFile : public ObjectFile {
protected:
void moveSymbolNext(DataRefImpl &Symb) const override;
Expected<StringRef> getSymbolName(DataRefImpl Symb) const override;
ErrorOr<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
uint32_t getSymbolFlags(DataRefImpl Symb) const override;
Expand Down
6 changes: 3 additions & 3 deletions include/llvm/Object/ELFObjectFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {

void moveSymbolNext(DataRefImpl &Symb) const override;
Expected<StringRef> getSymbolName(DataRefImpl Symb) const override;
ErrorOr<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
Expand Down Expand Up @@ -397,7 +397,7 @@ uint64_t ELFObjectFile<ELFT>::getSymbolValueImpl(DataRefImpl Symb) const {
}

template <class ELFT>
ErrorOr<uint64_t>
Expected<uint64_t>
ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb) const {
uint64_t Result = getSymbolValue(Symb);
const Elf_Sym *ESym = getSymbol(Symb);
Expand All @@ -415,7 +415,7 @@ ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb) const {
ErrorOr<const Elf_Shdr *> SectionOrErr =
EF.getSection(ESym, SymTab, ShndxTable);
if (std::error_code EC = SectionOrErr.getError())
return EC;
return errorCodeToError(EC);
const Elf_Shdr *Section = *SectionOrErr;
if (Section)
Result += Section->sh_addr;
Expand Down
2 changes: 1 addition & 1 deletion include/llvm/Object/MachO.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ class MachOObjectFile : public ObjectFile {
std::error_code getIndirectName(DataRefImpl Symb, StringRef &Res) const;
unsigned getSectionType(SectionRef Sec) const;

ErrorOr<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
Expected<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const override;
Expand Down
6 changes: 3 additions & 3 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 {
Expected<StringRef> getName() const;
/// Returns the symbol virtual address (i.e. address at which it will be
/// mapped).
ErrorOr<uint64_t> getAddress() const;
Expected<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,7 +198,7 @@ class ObjectFile : public SymbolicFile {
virtual Expected<StringRef> getSymbolName(DataRefImpl Symb) const = 0;
std::error_code printSymbolName(raw_ostream &OS,
DataRefImpl Symb) const override;
virtual ErrorOr<uint64_t> getSymbolAddress(DataRefImpl Symb) const = 0;
virtual Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const = 0;
virtual uint64_t getSymbolValueImpl(DataRefImpl Symb) const = 0;
virtual uint32_t getSymbolAlignment(DataRefImpl Symb) const;
virtual uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const = 0;
Expand Down Expand Up @@ -312,7 +312,7 @@ inline Expected<StringRef> SymbolRef::getName() const {
return getObject()->getSymbolName(getRawDataRefImpl());
}

inline ErrorOr<uint64_t> SymbolRef::getAddress() const {
inline Expected<uint64_t> SymbolRef::getAddress() const {
return getObject()->getSymbolAddress(getRawDataRefImpl());
}

Expand Down
10 changes: 7 additions & 3 deletions lib/DebugInfo/DWARF/DWARFContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -793,10 +793,14 @@ 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()) {
ErrorOr<uint64_t> SymAddrOrErr = Sym->getAddress();
if (std::error_code EC = SymAddrOrErr.getError()) {
Expected<uint64_t> SymAddrOrErr = Sym->getAddress();
if (!SymAddrOrErr) {
std::string Buf;
raw_string_ostream OS(Buf);
logAllUnhandledErrors(SymAddrOrErr.takeError(), OS, "");
OS.flush();
errs() << "error: failed to compute symbol address: "
<< EC.message() << '\n';
<< Buf << '\n';
continue;
}
SymAddr = *SymAddrOrErr;
Expand Down
6 changes: 3 additions & 3 deletions lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ std::error_code SymbolizableObjectFile::addSymbol(const SymbolRef &Symbol,
SymbolRef::Type SymbolType = *SymbolTypeOrErr;
if (SymbolType != SymbolRef::ST_Function && SymbolType != SymbolRef::ST_Data)
return std::error_code();
ErrorOr<uint64_t> SymbolAddressOrErr = Symbol.getAddress();
if (auto EC = SymbolAddressOrErr.getError())
return EC;
Expected<uint64_t> SymbolAddressOrErr = Symbol.getAddress();
if (!SymbolAddressOrErr)
return errorToErrorCode(SymbolAddressOrErr.takeError());
uint64_t SymbolAddress = *SymbolAddressOrErr;
if (OpdExtractor) {
// For big-endian PowerPC64 ELF, symbols in the .opd section refer to
Expand Down
8 changes: 4 additions & 4 deletions lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,9 @@ void RuntimeDyldImpl::mapSectionAddress(const void *LocalAddress,

static Error getOffset(const SymbolRef &Sym, SectionRef Sec,
uint64_t &Result) {
ErrorOr<uint64_t> AddressOrErr = Sym.getAddress();
if (std::error_code EC = AddressOrErr.getError())
return errorCodeToError(EC);
Expected<uint64_t> AddressOrErr = Sym.getAddress();
if (!AddressOrErr)
return AddressOrErr.takeError();
Result = *AddressOrErr - Sec.getAddress();
return Error::success();
}
Expand Down Expand Up @@ -236,7 +236,7 @@ RuntimeDyldImpl::loadObjectImpl(const object::ObjectFile &Obj) {
if (auto AddrOrErr = I->getAddress())
Addr = *AddrOrErr;
else
return errorCodeToError(AddrOrErr.getError());
return AddrOrErr.takeError();

unsigned SectionID = AbsoluteSymbolSection;

Expand Down
4 changes: 2 additions & 2 deletions lib/Object/COFFObjectFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ uint64_t COFFObjectFile::getSymbolValueImpl(DataRefImpl Ref) const {
return getCOFFSymbol(Ref).getValue();
}

ErrorOr<uint64_t> COFFObjectFile::getSymbolAddress(DataRefImpl Ref) const {
Expected<uint64_t> COFFObjectFile::getSymbolAddress(DataRefImpl Ref) const {
uint64_t Result = getSymbolValue(Ref);
COFFSymbolRef Symb = getCOFFSymbol(Ref);
int32_t SectionNumber = Symb.getSectionNumber();
Expand All @@ -168,7 +168,7 @@ ErrorOr<uint64_t> COFFObjectFile::getSymbolAddress(DataRefImpl Ref) const {

const coff_section *Section = nullptr;
if (std::error_code EC = getSection(SectionNumber, Section))
return EC;
return errorCodeToError(EC);
Result += Section->VirtualAddress;

// The section VirtualAddress does not include ImageBase, and we want to
Expand Down
2 changes: 1 addition & 1 deletion lib/Object/MachOObjectFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ uint64_t MachOObjectFile::getSymbolValueImpl(DataRefImpl Sym) const {
return getNValue(Sym);
}

ErrorOr<uint64_t> MachOObjectFile::getSymbolAddress(DataRefImpl Sym) const {
Expected<uint64_t> MachOObjectFile::getSymbolAddress(DataRefImpl Sym) const {
return getSymbolValue(Sym);
}

Expand Down
11 changes: 8 additions & 3 deletions lib/Object/Object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,14 @@ const char *LLVMGetSymbolName(LLVMSymbolIteratorRef SI) {
}

uint64_t LLVMGetSymbolAddress(LLVMSymbolIteratorRef SI) {
ErrorOr<uint64_t> Ret = (*unwrap(SI))->getAddress();
if (std::error_code EC = Ret.getError())
report_fatal_error(EC.message());
Expected<uint64_t> Ret = (*unwrap(SI))->getAddress();
if (!Ret) {
std::string Buf;
raw_string_ostream OS(Buf);
logAllUnhandledErrors(Ret.takeError(), OS, "");
OS.flush();
report_fatal_error(Buf);
}
return *Ret;
}

Expand Down
4 changes: 2 additions & 2 deletions tools/llvm-cxxdump/llvm-cxxdump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ static void dumpCXXData(const ObjectFile *Obj) {
continue;
StringRef SecContents;
error(Sec.getContents(SecContents));
ErrorOr<uint64_t> SymAddressOrErr = Sym.getAddress();
error(SymAddressOrErr.getError());
Expected<uint64_t> SymAddressOrErr = Sym.getAddress();
error(errorToErrorCode(SymAddressOrErr.takeError()));
uint64_t SymAddress = *SymAddressOrErr;
uint64_t SecAddress = Sec.getAddress();
uint64_t SecSize = Sec.getSize();
Expand Down
6 changes: 4 additions & 2 deletions tools/llvm-nm/llvm-nm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1015,9 +1015,11 @@ dumpSymbolNamesFromObject(SymbolicFile &Obj, bool printName,
}
if (PrintAddress && isa<ObjectFile>(Obj)) {
SymbolRef SymRef(Sym);
ErrorOr<uint64_t> AddressOrErr = SymRef.getAddress();
if (error(AddressOrErr.getError()))
Expected<uint64_t> AddressOrErr = SymRef.getAddress();
if (!AddressOrErr) {
consumeError(AddressOrErr.takeError());
break;
}
S.Address = *AddressOrErr;
}
S.TypeChar = getNMTypeChar(Obj, Sym);
Expand Down
6 changes: 3 additions & 3 deletions tools/llvm-objdump/COFFDump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ static std::error_code
resolveSectionAndAddress(const COFFObjectFile *Obj, const SymbolRef &Sym,
const coff_section *&ResolvedSection,
uint64_t &ResolvedAddr) {
ErrorOr<uint64_t> ResolvedAddrOrErr = Sym.getAddress();
if (std::error_code EC = ResolvedAddrOrErr.getError())
return EC;
Expected<uint64_t> ResolvedAddrOrErr = Sym.getAddress();
if (!ResolvedAddrOrErr)
return errorToErrorCode(ResolvedAddrOrErr.takeError());
ResolvedAddr = *ResolvedAddrOrErr;
Expected<section_iterator> Iter = Sym.getSection();
if (!Iter)
Expand Down
20 changes: 13 additions & 7 deletions tools/llvm-objdump/llvm-objdump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -649,9 +649,14 @@ static void printRelocationTargetName(const MachOObjectFile *O,

for (const SymbolRef &Symbol : O->symbols()) {
std::error_code ec;
ErrorOr<uint64_t> Addr = Symbol.getAddress();
if ((ec = Addr.getError()))
report_fatal_error(ec.message());
Expected<uint64_t> Addr = Symbol.getAddress();
if (!Addr) {
std::string Buf;
raw_string_ostream OS(Buf);
logAllUnhandledErrors(Addr.takeError(), OS, "");
OS.flush();
report_fatal_error(Buf);
}
if (*Addr != Val)
continue;
Expected<StringRef> Name = Symbol.getName();
Expand Down Expand Up @@ -992,8 +997,8 @@ static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
typedef std::vector<std::pair<uint64_t, StringRef>> SectionSymbolsTy;
std::map<SectionRef, SectionSymbolsTy> AllSymbols;
for (const SymbolRef &Symbol : Obj->symbols()) {
ErrorOr<uint64_t> AddressOrErr = Symbol.getAddress();
error(AddressOrErr.getError());
Expected<uint64_t> AddressOrErr = Symbol.getAddress();
error(errorToErrorCode(AddressOrErr.takeError()));
uint64_t Address = *AddressOrErr;

Expected<StringRef> Name = Symbol.getName();
Expand Down Expand Up @@ -1389,8 +1394,9 @@ void llvm::PrintSymbolTable(const ObjectFile *o, StringRef ArchiveName,
return;
}
for (const SymbolRef &Symbol : o->symbols()) {
ErrorOr<uint64_t> AddressOrError = Symbol.getAddress();
error(AddressOrError.getError());
Expected<uint64_t> AddressOrError = Symbol.getAddress();
if (!AddressOrError)
report_error(ArchiveName, o->getFileName(), AddressOrError.takeError());
uint64_t Address = *AddressOrError;
Expected<SymbolRef::Type> TypeOrError = Symbol.getType();
if (!TypeOrError)
Expand Down
37 changes: 27 additions & 10 deletions tools/llvm-readobj/ARMWinEHPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,9 @@ ErrorOr<object::SymbolRef> Decoder::getSymbol(const COFFObjectFile &COFF,
if (FunctionOnly && *Type != SymbolRef::ST_Function)
continue;

ErrorOr<uint64_t> Address = Symbol.getAddress();
if (std::error_code EC = Address.getError())
return EC;
Expected<uint64_t> Address = Symbol.getAddress();
if (!Address)
return errorToErrorCode(Address.takeError());
if (*Address == VA)
return Symbol;
}
Expand Down Expand Up @@ -618,9 +618,14 @@ bool Decoder::dumpUnpackedEntry(const COFFObjectFile &COFF,
report_fatal_error(Buf);
}
FunctionName = *FunctionNameOrErr;
ErrorOr<uint64_t> FunctionAddressOrErr = Function->getAddress();
if (std::error_code EC = FunctionAddressOrErr.getError())
report_fatal_error(EC.message());
Expected<uint64_t> FunctionAddressOrErr = Function->getAddress();
if (!FunctionAddressOrErr) {
std::string Buf;
llvm::raw_string_ostream OS(Buf);
logAllUnhandledErrors(FunctionAddressOrErr.takeError(), OS, "");
OS.flush();
report_fatal_error(Buf);
}
FunctionAddress = *FunctionAddressOrErr;
} else {
const pe32_header *PEHeader;
Expand All @@ -641,9 +646,14 @@ bool Decoder::dumpUnpackedEntry(const COFFObjectFile &COFF,
report_fatal_error(Buf);
}

ErrorOr<uint64_t> AddressOrErr = XDataRecord->getAddress();
if (std::error_code EC = AddressOrErr.getError())
report_fatal_error(EC.message());
Expected<uint64_t> AddressOrErr = XDataRecord->getAddress();
if (!AddressOrErr) {
std::string Buf;
llvm::raw_string_ostream OS(Buf);
logAllUnhandledErrors(AddressOrErr.takeError(), OS, "");
OS.flush();
report_fatal_error(Buf);
}
uint64_t Address = *AddressOrErr;

SW.printString("ExceptionRecord", formatSymbol(*Name, Address));
Expand Down Expand Up @@ -698,7 +708,14 @@ bool Decoder::dumpPackedEntry(const object::COFFObjectFile &COFF,
report_fatal_error(Buf);
}
FunctionName = *FunctionNameOrErr;
ErrorOr<uint64_t> FunctionAddressOrErr = Function->getAddress();
Expected<uint64_t> FunctionAddressOrErr = Function->getAddress();
if (!FunctionAddressOrErr) {
std::string Buf;
llvm::raw_string_ostream OS(Buf);
logAllUnhandledErrors(FunctionAddressOrErr.takeError(), OS, "");
OS.flush();
report_fatal_error(Buf);
}
FunctionAddress = *FunctionAddressOrErr;
} else {
const pe32_header *PEHeader;
Expand Down
6 changes: 3 additions & 3 deletions tools/llvm-readobj/Win64EHDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ static std::error_code resolveRelocation(const Dumper::Context &Ctx,
Ctx.ResolveSymbol(Section, Offset, Symbol, Ctx.UserData))
return EC;

ErrorOr<uint64_t> ResolvedAddressOrErr = Symbol.getAddress();
if (std::error_code EC = ResolvedAddressOrErr.getError())
return EC;
Expected<uint64_t> ResolvedAddressOrErr = Symbol.getAddress();
if (!ResolvedAddressOrErr)
return errorToErrorCode(ResolvedAddressOrErr.takeError());
ResolvedAddress = *ResolvedAddressOrErr;

Expected<section_iterator> SI = Symbol.getSection();
Expand Down
7 changes: 5 additions & 2 deletions tools/llvm-rtdyld/llvm-rtdyld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,12 @@ static int printLineInfoForInput(bool LoadObjects, bool UseDebugObj) {
consumeError(Name.takeError());
continue;
}
ErrorOr<uint64_t> AddrOrErr = Sym.getAddress();
if (!AddrOrErr)
Expected<uint64_t> AddrOrErr = Sym.getAddress();
if (!AddrOrErr) {
// TODO: Actually report errors helpfully.
consumeError(AddrOrErr.takeError());
continue;
}
uint64_t Addr = *AddrOrErr;

uint64_t Size = P.second;
Expand Down
4 changes: 2 additions & 2 deletions tools/sancov/sancov.cc
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,8 @@ findSanitizerCovFunctions(const object::ObjectFile &O) {
std::set<uint64_t> Result;

for (const object::SymbolRef &Symbol : O.symbols()) {
ErrorOr<uint64_t> AddressOrErr = Symbol.getAddress();
FailIfError(AddressOrErr);
Expected<uint64_t> AddressOrErr = Symbol.getAddress();
FailIfError(errorToErrorCode(AddressOrErr.takeError()));

Expected<StringRef> NameOrErr = Symbol.getName();
FailIfError(errorToErrorCode(NameOrErr.takeError()));
Expand Down

0 comments on commit 317de7c

Please sign in to comment.