Skip to content

Commit 6ce6bfa

Browse files
author
George Rimar
committed
[llvm-objdump] - Simplify the getRelocationValueString. NFCI.
This refactors the getRelocationValueString method. It is a bit overcomplicated and it is possible to reduce it without losing the functionality it seems. Differential revision: https://reviews.llvm.org/D56778 llvm-svn: 351417
1 parent ee61308 commit 6ce6bfa

File tree

1 file changed

+22
-38
lines changed

1 file changed

+22
-38
lines changed

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

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -491,69 +491,53 @@ static std::error_code getRelocationValueString(const ELFObjectFile<ELFT> *Obj,
491491
auto SecOrErr = EF.getSection(Rel.d.a);
492492
if (!SecOrErr)
493493
return errorToErrorCode(SecOrErr.takeError());
494-
const Elf_Shdr *Sec = *SecOrErr;
495-
auto SymTabOrErr = EF.getSection(Sec->sh_link);
496-
if (!SymTabOrErr)
497-
return errorToErrorCode(SymTabOrErr.takeError());
498-
const Elf_Shdr *SymTab = *SymTabOrErr;
499-
assert(SymTab->sh_type == ELF::SHT_SYMTAB ||
500-
SymTab->sh_type == ELF::SHT_DYNSYM);
501-
auto StrTabSec = EF.getSection(SymTab->sh_link);
502-
if (!StrTabSec)
503-
return errorToErrorCode(StrTabSec.takeError());
504-
auto StrTabOrErr = EF.getStringTable(*StrTabSec);
505-
if (!StrTabOrErr)
506-
return errorToErrorCode(StrTabOrErr.takeError());
507-
StringRef StrTab = *StrTabOrErr;
494+
508495
int64_t Addend = 0;
509496
// If there is no Symbol associated with the relocation, we set the undef
510497
// boolean value to 'true'. This will prevent us from calling functions that
511498
// requires the relocation to be associated with a symbol.
499+
//
500+
// In SHT_REL case we would need to read the addend from section data.
501+
// GNU objdump does not do that and we just follow for simplicity atm.
512502
bool Undef = false;
513-
switch (Sec->sh_type) {
514-
default:
515-
return object_error::parse_failed;
516-
case ELF::SHT_REL: {
517-
// TODO: Read implicit addend from section data.
518-
break;
519-
}
520-
case ELF::SHT_RELA: {
503+
if ((*SecOrErr)->sh_type == ELF::SHT_RELA) {
521504
const Elf_Rela *ERela = Obj->getRela(Rel);
522505
Addend = ERela->r_addend;
523506
Undef = ERela->getSymbol(false) == 0;
524-
break;
525-
}
507+
} else if ((*SecOrErr)->sh_type != ELF::SHT_REL) {
508+
return object_error::parse_failed;
526509
}
527-
std::string Target;
510+
511+
// Default scheme is to print Target, as well as "+ <addend>" for nonzero
512+
// addend. Should be acceptable for all normal purposes.
513+
std::string FmtBuf;
514+
raw_string_ostream Fmt(FmtBuf);
515+
528516
if (!Undef) {
529517
symbol_iterator SI = RelRef.getSymbol();
530-
const Elf_Sym *symb = Obj->getSymbol(SI->getRawDataRefImpl());
531-
if (symb->getType() == ELF::STT_SECTION) {
518+
const Elf_Sym *Sym = Obj->getSymbol(SI->getRawDataRefImpl());
519+
if (Sym->getType() == ELF::STT_SECTION) {
532520
Expected<section_iterator> SymSI = SI->getSection();
533521
if (!SymSI)
534522
return errorToErrorCode(SymSI.takeError());
535523
const Elf_Shdr *SymSec = Obj->getSection((*SymSI)->getRawDataRefImpl());
536524
auto SecName = EF.getSectionName(SymSec);
537525
if (!SecName)
538526
return errorToErrorCode(SecName.takeError());
539-
Target = *SecName;
527+
Fmt << *SecName;
540528
} else {
541-
Expected<StringRef> SymName = symb->getName(StrTab);
529+
Expected<StringRef> SymName = SI->getName();
542530
if (!SymName)
543531
return errorToErrorCode(SymName.takeError());
544532
if (Demangle)
545-
Target = demangle(*SymName);
533+
Fmt << demangle(*SymName);
546534
else
547-
Target = *SymName;
535+
Fmt << *SymName;
548536
}
549-
} else
550-
Target = "*ABS*";
537+
} else {
538+
Fmt << "*ABS*";
539+
}
551540

552-
// Default scheme is to print Target, as well as "+ <addend>" for nonzero
553-
// addend. Should be acceptable for all normal purposes.
554-
std::string FmtBuf;
555-
raw_string_ostream Fmt(FmtBuf);
556-
Fmt << Target;
557541
if (Addend != 0)
558542
Fmt << (Addend < 0 ? "" : "+") << Addend;
559543
Fmt.flush();

0 commit comments

Comments
 (0)