Skip to content

Commit f67de6c

Browse files
committed
[llvm-objdump] Migrate relocation handling functions from error_code to Error
llvm-svn: 357920
1 parent f6a60f1 commit f67de6c

File tree

6 files changed

+49
-50
lines changed

6 files changed

+49
-50
lines changed

llvm/tools/llvm-objdump/COFFDump.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -468,17 +468,16 @@ static bool getPDataSection(const COFFObjectFile *Obj,
468468
return false;
469469
}
470470

471-
std::error_code
472-
llvm::getCOFFRelocationValueString(const COFFObjectFile *Obj,
473-
const RelocationRef &Rel,
474-
SmallVectorImpl<char> &Result) {
471+
Error llvm::getCOFFRelocationValueString(const COFFObjectFile *Obj,
472+
const RelocationRef &Rel,
473+
SmallVectorImpl<char> &Result) {
475474
symbol_iterator SymI = Rel.getSymbol();
476475
Expected<StringRef> SymNameOrErr = SymI->getName();
477476
if (!SymNameOrErr)
478-
return errorToErrorCode(SymNameOrErr.takeError());
477+
return SymNameOrErr.takeError();
479478
StringRef SymName = *SymNameOrErr;
480479
Result.append(SymName.begin(), SymName.end());
481-
return std::error_code();
480+
return Error::success();
482481
}
483482

484483
static void printWin64EHUnwindInfo(const Win64EH::UnwindInfo *UI) {

llvm/tools/llvm-objdump/ELFDump.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ Expected<StringRef> getDynamicStrTab(const ELFFile<ELFT> *Elf) {
5050
}
5151

5252
template <class ELFT>
53-
static std::error_code getRelocationValueString(const ELFObjectFile<ELFT> *Obj,
54-
const RelocationRef &RelRef,
55-
SmallVectorImpl<char> &Result) {
53+
static Error getRelocationValueString(const ELFObjectFile<ELFT> *Obj,
54+
const RelocationRef &RelRef,
55+
SmallVectorImpl<char> &Result) {
5656
const ELFFile<ELFT> &EF = *Obj->getELFFile();
5757
DataRefImpl Rel = RelRef.getRawDataRefImpl();
5858
auto SecOrErr = EF.getSection(Rel.d.a);
5959
if (!SecOrErr)
60-
return errorToErrorCode(SecOrErr.takeError());
60+
return SecOrErr.takeError();
6161

6262
int64_t Addend = 0;
6363
// 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,
7272
Addend = ERela->r_addend;
7373
Undef = ERela->getSymbol(false) == 0;
7474
} else if ((*SecOrErr)->sh_type != ELF::SHT_REL) {
75-
return object_error::parse_failed;
75+
return make_error<BinaryError>();
7676
}
7777

7878
// 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,
8686
if (Sym->getType() == ELF::STT_SECTION) {
8787
Expected<section_iterator> SymSI = SI->getSection();
8888
if (!SymSI)
89-
return errorToErrorCode(SymSI.takeError());
89+
return SymSI.takeError();
9090
const typename ELFT::Shdr *SymSec =
9191
Obj->getSection((*SymSI)->getRawDataRefImpl());
9292
auto SecName = EF.getSectionName(SymSec);
9393
if (!SecName)
94-
return errorToErrorCode(SecName.takeError());
94+
return SecName.takeError();
9595
Fmt << *SecName;
9696
} else {
9797
Expected<StringRef> SymName = SI->getName();
9898
if (!SymName)
99-
return errorToErrorCode(SymName.takeError());
99+
return SymName.takeError();
100100
if (Demangle)
101101
Fmt << demangle(*SymName);
102102
else
@@ -110,13 +110,12 @@ static std::error_code getRelocationValueString(const ELFObjectFile<ELFT> *Obj,
110110
Fmt << (Addend < 0 ? "" : "+") << Addend;
111111
Fmt.flush();
112112
Result.append(FmtBuf.begin(), FmtBuf.end());
113-
return std::error_code();
113+
return Error::success();
114114
}
115115

116-
std::error_code
117-
llvm::getELFRelocationValueString(const ELFObjectFileBase *Obj,
118-
const RelocationRef &Rel,
119-
SmallVectorImpl<char> &Result) {
116+
Error llvm::getELFRelocationValueString(const ELFObjectFileBase *Obj,
117+
const RelocationRef &Rel,
118+
SmallVectorImpl<char> &Result) {
120119
if (auto *ELF32LE = dyn_cast<ELF32LEObjectFile>(Obj))
121120
return getRelocationValueString(ELF32LE, Rel, Result);
122121
if (auto *ELF64LE = dyn_cast<ELF64LEObjectFile>(Obj))

llvm/tools/llvm-objdump/MachODump.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -412,10 +412,9 @@ static void printRelocationTargetName(const MachOObjectFile *O,
412412
Fmt << S;
413413
}
414414

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

@@ -488,7 +487,7 @@ llvm::getMachORelocationValueString(const MachOObjectFile *Obj,
488487
// Generic relocation types...
489488
switch (Type) {
490489
case MachO::GENERIC_RELOC_PAIR: // prints no info
491-
return std::error_code();
490+
return Error::success();
492491
case MachO::GENERIC_RELOC_SECTDIFF: {
493492
DataRefImpl RelNext = Rel;
494493
Obj->moveRelocationNext(RelNext);
@@ -588,7 +587,7 @@ llvm::getMachORelocationValueString(const MachOObjectFile *Obj,
588587

589588
Fmt.flush();
590589
Result.append(FmtBuf.begin(), FmtBuf.end());
591-
return std::error_code();
590+
return Error::success();
592591
}
593592

594593
static void PrintIndirectSymbolTable(MachOObjectFile *O, bool verbose,

llvm/tools/llvm-objdump/WasmDump.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@ void llvm::printWasmFileHeader(const object::ObjectFile *Obj) {
2626
outs() << "\n";
2727
}
2828

29-
std::error_code
30-
llvm::getWasmRelocationValueString(const WasmObjectFile *Obj,
31-
const RelocationRef &RelRef,
32-
SmallVectorImpl<char> &Result) {
29+
Error llvm::getWasmRelocationValueString(const WasmObjectFile *Obj,
30+
const RelocationRef &RelRef,
31+
SmallVectorImpl<char> &Result) {
3332
const wasm::WasmRelocation &Rel = Obj->getWasmRelocation(RelRef);
3433
symbol_iterator SI = RelRef.getSymbol();
3534
std::string FmtBuf;
@@ -41,12 +40,12 @@ llvm::getWasmRelocationValueString(const WasmObjectFile *Obj,
4140
} else {
4241
Expected<StringRef> SymNameOrErr = SI->getName();
4342
if (!SymNameOrErr)
44-
return errorToErrorCode(SymNameOrErr.takeError());
43+
return SymNameOrErr.takeError();
4544
StringRef SymName = *SymNameOrErr;
4645
Result.append(SymName.begin(), SymName.end());
4746
}
4847
Fmt << (Rel.Addend < 0 ? "" : "+") << Rel.Addend;
4948
Fmt.flush();
5049
Result.append(FmtBuf.begin(), FmtBuf.end());
51-
return std::error_code();
50+
return Error::success();
5251
}

llvm/tools/llvm-objdump/llvm-objdump.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,13 @@ void llvm::error(std::error_code EC) {
330330
exit(1);
331331
}
332332

333+
void llvm::error(Error E) {
334+
if (!E)
335+
return;
336+
WithColor::error(errs(), ToolName) << toString(std::move(E));
337+
exit(1);
338+
}
339+
333340
LLVM_ATTRIBUTE_NORETURN void llvm::error(Twine Message) {
334341
WithColor::error(errs(), ToolName) << Message << ".\n";
335342
errs().flush();
@@ -437,8 +444,8 @@ bool llvm::isRelocAddressLess(RelocationRef A, RelocationRef B) {
437444
return A.getOffset() < B.getOffset();
438445
}
439446

440-
static std::error_code getRelocationValueString(const RelocationRef &Rel,
441-
SmallVectorImpl<char> &Result) {
447+
static Error getRelocationValueString(const RelocationRef &Rel,
448+
SmallVectorImpl<char> &Result) {
442449
const ObjectFile *Obj = Rel.getObject();
443450
if (auto *ELF = dyn_cast<ELFObjectFileBase>(Obj))
444451
return getELFRelocationValueString(ELF, Rel, Result);
@@ -1554,7 +1561,6 @@ void llvm::printSectionHeaders(const ObjectFile *Obj) {
15541561
}
15551562

15561563
void llvm::printSectionContents(const ObjectFile *Obj) {
1557-
std::error_code EC;
15581564
for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
15591565
StringRef Name;
15601566
StringRef Contents;

llvm/tools/llvm-objdump/llvm-objdump.h

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -121,26 +121,23 @@ class SectionFilter {
121121
// Various helper functions.
122122
SectionFilter ToolSectionFilter(llvm::object::ObjectFile const &O);
123123

124-
std::error_code
125-
getELFRelocationValueString(const object::ELFObjectFileBase *Obj,
126-
const object::RelocationRef &Rel,
127-
llvm::SmallVectorImpl<char> &Result);
128-
std::error_code
129-
getCOFFRelocationValueString(const object::COFFObjectFile *Obj,
130-
const object::RelocationRef &Rel,
131-
llvm::SmallVectorImpl<char> &Result);
132-
std::error_code
133-
getWasmRelocationValueString(const object::WasmObjectFile *Obj,
134-
const object::RelocationRef &RelRef,
135-
llvm::SmallVectorImpl<char> &Result);
136-
std::error_code
137-
getMachORelocationValueString(const object::MachOObjectFile *Obj,
138-
const object::RelocationRef &RelRef,
139-
llvm::SmallVectorImpl<char> &Result);
124+
Error getELFRelocationValueString(const object::ELFObjectFileBase *Obj,
125+
const object::RelocationRef &Rel,
126+
llvm::SmallVectorImpl<char> &Result);
127+
Error getCOFFRelocationValueString(const object::COFFObjectFile *Obj,
128+
const object::RelocationRef &Rel,
129+
llvm::SmallVectorImpl<char> &Result);
130+
Error getWasmRelocationValueString(const object::WasmObjectFile *Obj,
131+
const object::RelocationRef &RelRef,
132+
llvm::SmallVectorImpl<char> &Result);
133+
Error getMachORelocationValueString(const object::MachOObjectFile *Obj,
134+
const object::RelocationRef &RelRef,
135+
llvm::SmallVectorImpl<char> &Result);
140136

141137
uint64_t getELFSectionLMA(const object::ELFSectionRef& Sec);
142138

143139
void error(std::error_code ec);
140+
void error(Error E);
144141
bool isRelocAddressLess(object::RelocationRef A, object::RelocationRef B);
145142
void parseInputMachO(StringRef Filename);
146143
void parseInputMachO(object::MachOUniversalBinary *UB);

0 commit comments

Comments
 (0)