[llvm-objdump] Migrate relocation handling functions from error_code to Error

llvm-svn: 357920
This commit is contained in:
Fangrui Song 2019-04-08 16:24:08 +00:00
parent f6a60f1f80
commit f67de6c940
6 changed files with 49 additions and 50 deletions

View File

@ -468,17 +468,16 @@ static bool getPDataSection(const COFFObjectFile *Obj,
return false;
}
std::error_code
llvm::getCOFFRelocationValueString(const COFFObjectFile *Obj,
Error llvm::getCOFFRelocationValueString(const COFFObjectFile *Obj,
const RelocationRef &Rel,
SmallVectorImpl<char> &Result) {
symbol_iterator SymI = Rel.getSymbol();
Expected<StringRef> SymNameOrErr = SymI->getName();
if (!SymNameOrErr)
return errorToErrorCode(SymNameOrErr.takeError());
return SymNameOrErr.takeError();
StringRef SymName = *SymNameOrErr;
Result.append(SymName.begin(), SymName.end());
return std::error_code();
return Error::success();
}
static void printWin64EHUnwindInfo(const Win64EH::UnwindInfo *UI) {

View File

@ -50,14 +50,14 @@ Expected<StringRef> getDynamicStrTab(const ELFFile<ELFT> *Elf) {
}
template <class ELFT>
static std::error_code getRelocationValueString(const ELFObjectFile<ELFT> *Obj,
static Error getRelocationValueString(const ELFObjectFile<ELFT> *Obj,
const RelocationRef &RelRef,
SmallVectorImpl<char> &Result) {
const ELFFile<ELFT> &EF = *Obj->getELFFile();
DataRefImpl Rel = RelRef.getRawDataRefImpl();
auto SecOrErr = EF.getSection(Rel.d.a);
if (!SecOrErr)
return errorToErrorCode(SecOrErr.takeError());
return SecOrErr.takeError();
int64_t Addend = 0;
// If there is no Symbol associated with the relocation, we set the undef
@ -72,7 +72,7 @@ static std::error_code getRelocationValueString(const ELFObjectFile<ELFT> *Obj,
Addend = ERela->r_addend;
Undef = ERela->getSymbol(false) == 0;
} else if ((*SecOrErr)->sh_type != ELF::SHT_REL) {
return object_error::parse_failed;
return make_error<BinaryError>();
}
// Default scheme is to print Target, as well as "+ <addend>" for nonzero
@ -86,17 +86,17 @@ static std::error_code getRelocationValueString(const ELFObjectFile<ELFT> *Obj,
if (Sym->getType() == ELF::STT_SECTION) {
Expected<section_iterator> SymSI = SI->getSection();
if (!SymSI)
return errorToErrorCode(SymSI.takeError());
return SymSI.takeError();
const typename ELFT::Shdr *SymSec =
Obj->getSection((*SymSI)->getRawDataRefImpl());
auto SecName = EF.getSectionName(SymSec);
if (!SecName)
return errorToErrorCode(SecName.takeError());
return SecName.takeError();
Fmt << *SecName;
} else {
Expected<StringRef> SymName = SI->getName();
if (!SymName)
return errorToErrorCode(SymName.takeError());
return SymName.takeError();
if (Demangle)
Fmt << demangle(*SymName);
else
@ -110,11 +110,10 @@ static std::error_code getRelocationValueString(const ELFObjectFile<ELFT> *Obj,
Fmt << (Addend < 0 ? "" : "+") << Addend;
Fmt.flush();
Result.append(FmtBuf.begin(), FmtBuf.end());
return std::error_code();
return Error::success();
}
std::error_code
llvm::getELFRelocationValueString(const ELFObjectFileBase *Obj,
Error llvm::getELFRelocationValueString(const ELFObjectFileBase *Obj,
const RelocationRef &Rel,
SmallVectorImpl<char> &Result) {
if (auto *ELF32LE = dyn_cast<ELF32LEObjectFile>(Obj))

View File

@ -412,8 +412,7 @@ static void printRelocationTargetName(const MachOObjectFile *O,
Fmt << S;
}
std::error_code
llvm::getMachORelocationValueString(const MachOObjectFile *Obj,
Error llvm::getMachORelocationValueString(const MachOObjectFile *Obj,
const RelocationRef &RelRef,
SmallVectorImpl<char> &Result) {
DataRefImpl Rel = RelRef.getRawDataRefImpl();
@ -488,7 +487,7 @@ llvm::getMachORelocationValueString(const MachOObjectFile *Obj,
// Generic relocation types...
switch (Type) {
case MachO::GENERIC_RELOC_PAIR: // prints no info
return std::error_code();
return Error::success();
case MachO::GENERIC_RELOC_SECTDIFF: {
DataRefImpl RelNext = Rel;
Obj->moveRelocationNext(RelNext);
@ -588,7 +587,7 @@ llvm::getMachORelocationValueString(const MachOObjectFile *Obj,
Fmt.flush();
Result.append(FmtBuf.begin(), FmtBuf.end());
return std::error_code();
return Error::success();
}
static void PrintIndirectSymbolTable(MachOObjectFile *O, bool verbose,

View File

@ -26,8 +26,7 @@ void llvm::printWasmFileHeader(const object::ObjectFile *Obj) {
outs() << "\n";
}
std::error_code
llvm::getWasmRelocationValueString(const WasmObjectFile *Obj,
Error llvm::getWasmRelocationValueString(const WasmObjectFile *Obj,
const RelocationRef &RelRef,
SmallVectorImpl<char> &Result) {
const wasm::WasmRelocation &Rel = Obj->getWasmRelocation(RelRef);
@ -41,12 +40,12 @@ llvm::getWasmRelocationValueString(const WasmObjectFile *Obj,
} else {
Expected<StringRef> SymNameOrErr = SI->getName();
if (!SymNameOrErr)
return errorToErrorCode(SymNameOrErr.takeError());
return SymNameOrErr.takeError();
StringRef SymName = *SymNameOrErr;
Result.append(SymName.begin(), SymName.end());
}
Fmt << (Rel.Addend < 0 ? "" : "+") << Rel.Addend;
Fmt.flush();
Result.append(FmtBuf.begin(), FmtBuf.end());
return std::error_code();
return Error::success();
}

View File

@ -330,6 +330,13 @@ void llvm::error(std::error_code EC) {
exit(1);
}
void llvm::error(Error E) {
if (!E)
return;
WithColor::error(errs(), ToolName) << toString(std::move(E));
exit(1);
}
LLVM_ATTRIBUTE_NORETURN void llvm::error(Twine Message) {
WithColor::error(errs(), ToolName) << Message << ".\n";
errs().flush();
@ -437,7 +444,7 @@ bool llvm::isRelocAddressLess(RelocationRef A, RelocationRef B) {
return A.getOffset() < B.getOffset();
}
static std::error_code getRelocationValueString(const RelocationRef &Rel,
static Error getRelocationValueString(const RelocationRef &Rel,
SmallVectorImpl<char> &Result) {
const ObjectFile *Obj = Rel.getObject();
if (auto *ELF = dyn_cast<ELFObjectFileBase>(Obj))
@ -1554,7 +1561,6 @@ void llvm::printSectionHeaders(const ObjectFile *Obj) {
}
void llvm::printSectionContents(const ObjectFile *Obj) {
std::error_code EC;
for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
StringRef Name;
StringRef Contents;

View File

@ -121,26 +121,23 @@ private:
// Various helper functions.
SectionFilter ToolSectionFilter(llvm::object::ObjectFile const &O);
std::error_code
getELFRelocationValueString(const object::ELFObjectFileBase *Obj,
Error getELFRelocationValueString(const object::ELFObjectFileBase *Obj,
const object::RelocationRef &Rel,
llvm::SmallVectorImpl<char> &Result);
std::error_code
getCOFFRelocationValueString(const object::COFFObjectFile *Obj,
Error getCOFFRelocationValueString(const object::COFFObjectFile *Obj,
const object::RelocationRef &Rel,
llvm::SmallVectorImpl<char> &Result);
std::error_code
getWasmRelocationValueString(const object::WasmObjectFile *Obj,
Error getWasmRelocationValueString(const object::WasmObjectFile *Obj,
const object::RelocationRef &RelRef,
llvm::SmallVectorImpl<char> &Result);
std::error_code
getMachORelocationValueString(const object::MachOObjectFile *Obj,
Error getMachORelocationValueString(const object::MachOObjectFile *Obj,
const object::RelocationRef &RelRef,
llvm::SmallVectorImpl<char> &Result);
uint64_t getELFSectionLMA(const object::ELFSectionRef& Sec);
void error(std::error_code ec);
void error(Error E);
bool isRelocAddressLess(object::RelocationRef A, object::RelocationRef B);
void parseInputMachO(StringRef Filename);
void parseInputMachO(object::MachOUniversalBinary *UB);