forked from OSchip/llvm-project
Thread Expected<...> up from libObject’s getSymbolAddress() for symbols 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). llvm-svn: 273701
This commit is contained in:
parent
267164df0a
commit
931cb65df2
|
@ -722,7 +722,7 @@ public:
|
|||
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;
|
||||
|
|
|
@ -207,7 +207,7 @@ 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;
|
||||
uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
|
||||
uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
|
|
|
@ -205,7 +205,7 @@ public:
|
|||
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;
|
||||
|
|
|
@ -135,7 +135,7 @@ public:
|
|||
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.
|
||||
|
@ -198,7 +198,7 @@ protected:
|
|||
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;
|
||||
|
@ -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());
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
@ -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;
|
||||
|
||||
|
|
|
@ -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();
|
||||
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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();
|
||||
|
@ -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();
|
||||
|
@ -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)
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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));
|
||||
|
@ -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;
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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()));
|
||||
|
|
Loading…
Reference in New Issue