Skip to content

Commit

Permalink
[llvm-objdump] Migrate relocation handling functions from error_code …
Browse files Browse the repository at this point in the history
…to Error

llvm-svn: 357920
  • Loading branch information
MaskRay committed Apr 8, 2019
1 parent f6a60f1 commit f67de6c
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 50 deletions.
11 changes: 5 additions & 6 deletions llvm/tools/llvm-objdump/COFFDump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,17 +468,16 @@ static bool getPDataSection(const COFFObjectFile *Obj,
return false;
}

std::error_code
llvm::getCOFFRelocationValueString(const COFFObjectFile *Obj,
const RelocationRef &Rel,
SmallVectorImpl<char> &Result) {
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) {
Expand Down
25 changes: 12 additions & 13 deletions llvm/tools/llvm-objdump/ELFDump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ Expected<StringRef> getDynamicStrTab(const ELFFile<ELFT> *Elf) {
}

template <class ELFT>
static std::error_code getRelocationValueString(const ELFObjectFile<ELFT> *Obj,
const RelocationRef &RelRef,
SmallVectorImpl<char> &Result) {
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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -110,13 +110,12 @@ 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,
const RelocationRef &Rel,
SmallVectorImpl<char> &Result) {
Error llvm::getELFRelocationValueString(const ELFObjectFileBase *Obj,
const RelocationRef &Rel,
SmallVectorImpl<char> &Result) {
if (auto *ELF32LE = dyn_cast<ELF32LEObjectFile>(Obj))
return getRelocationValueString(ELF32LE, Rel, Result);
if (auto *ELF64LE = dyn_cast<ELF64LEObjectFile>(Obj))
Expand Down
11 changes: 5 additions & 6 deletions llvm/tools/llvm-objdump/MachODump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,10 +412,9 @@ static void printRelocationTargetName(const MachOObjectFile *O,
Fmt << S;
}

std::error_code
llvm::getMachORelocationValueString(const MachOObjectFile *Obj,
const RelocationRef &RelRef,
SmallVectorImpl<char> &Result) {
Error llvm::getMachORelocationValueString(const MachOObjectFile *Obj,
const RelocationRef &RelRef,
SmallVectorImpl<char> &Result) {
DataRefImpl Rel = RelRef.getRawDataRefImpl();
MachO::any_relocation_info RE = Obj->getRelocation(Rel);

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 5 additions & 6 deletions llvm/tools/llvm-objdump/WasmDump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ void llvm::printWasmFileHeader(const object::ObjectFile *Obj) {
outs() << "\n";
}

std::error_code
llvm::getWasmRelocationValueString(const WasmObjectFile *Obj,
const RelocationRef &RelRef,
SmallVectorImpl<char> &Result) {
Error llvm::getWasmRelocationValueString(const WasmObjectFile *Obj,
const RelocationRef &RelRef,
SmallVectorImpl<char> &Result) {
const wasm::WasmRelocation &Rel = Obj->getWasmRelocation(RelRef);
symbol_iterator SI = RelRef.getSymbol();
std::string FmtBuf;
Expand All @@ -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();
}
12 changes: 9 additions & 3 deletions llvm/tools/llvm-objdump/llvm-objdump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -437,8 +444,8 @@ bool llvm::isRelocAddressLess(RelocationRef A, RelocationRef B) {
return A.getOffset() < B.getOffset();
}

static std::error_code getRelocationValueString(const RelocationRef &Rel,
SmallVectorImpl<char> &Result) {
static Error getRelocationValueString(const RelocationRef &Rel,
SmallVectorImpl<char> &Result) {
const ObjectFile *Obj = Rel.getObject();
if (auto *ELF = dyn_cast<ELFObjectFileBase>(Obj))
return getELFRelocationValueString(ELF, Rel, Result);
Expand Down Expand Up @@ -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;
Expand Down
29 changes: 13 additions & 16 deletions llvm/tools/llvm-objdump/llvm-objdump.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,26 +121,23 @@ class SectionFilter {
// Various helper functions.
SectionFilter ToolSectionFilter(llvm::object::ObjectFile const &O);

std::error_code
getELFRelocationValueString(const object::ELFObjectFileBase *Obj,
const object::RelocationRef &Rel,
llvm::SmallVectorImpl<char> &Result);
std::error_code
getCOFFRelocationValueString(const object::COFFObjectFile *Obj,
const object::RelocationRef &Rel,
llvm::SmallVectorImpl<char> &Result);
std::error_code
getWasmRelocationValueString(const object::WasmObjectFile *Obj,
const object::RelocationRef &RelRef,
llvm::SmallVectorImpl<char> &Result);
std::error_code
getMachORelocationValueString(const object::MachOObjectFile *Obj,
const object::RelocationRef &RelRef,
llvm::SmallVectorImpl<char> &Result);
Error getELFRelocationValueString(const object::ELFObjectFileBase *Obj,
const object::RelocationRef &Rel,
llvm::SmallVectorImpl<char> &Result);
Error getCOFFRelocationValueString(const object::COFFObjectFile *Obj,
const object::RelocationRef &Rel,
llvm::SmallVectorImpl<char> &Result);
Error getWasmRelocationValueString(const object::WasmObjectFile *Obj,
const object::RelocationRef &RelRef,
llvm::SmallVectorImpl<char> &Result);
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);
Expand Down

0 comments on commit f67de6c

Please sign in to comment.