diff options
author | James Henderson <jh7370@my.bristol.ac.uk> | 2019-03-29 11:47:19 +0000 |
---|---|---|
committer | James Henderson <jh7370@my.bristol.ac.uk> | 2019-03-29 11:47:19 +0000 |
commit | 814ab373ac3742fa9b4e4f6c4c0fe89e6c7f6892 (patch) | |
tree | 39cb8ebc22d7697bdf219f2d2f1b8d5c330da908 /llvm/tools/llvm-readobj | |
parent | b8fb15d4122b04d620c1d4a89449b6eba2f4b0c0 (diff) | |
download | bcm5719-llvm-814ab373ac3742fa9b4e4f6c4c0fe89e6c7f6892.tar.gz bcm5719-llvm-814ab373ac3742fa9b4e4f6c4c0fe89e6c7f6892.zip |
[llvm-readelf]Merge dynamic and static relocation printing to avoid code duplication
The majority of the printRelocation and printDynamicRelocation functions
were identical. This patch factors this all out into a new function.
There are a couple of minor differences to do with printing of symbols
without names, but I think these are harmless, and in some cases a small
improvement.
Reviewed by: grimar, rupprecht, Higuoxing
Differential Revision: https://reviews.llvm.org/D59823
llvm-svn: 357246
Diffstat (limited to 'llvm/tools/llvm-readobj')
-rw-r--r-- | llvm/tools/llvm-readobj/ELFDumper.cpp | 68 |
1 files changed, 23 insertions, 45 deletions
diff --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp index 8be71b08efc..1bd7737bc31 100644 --- a/llvm/tools/llvm-readobj/ELFDumper.cpp +++ b/llvm/tools/llvm-readobj/ELFDumper.cpp @@ -455,6 +455,8 @@ private: void printRelocHeader(unsigned SType); void printRelocation(const ELFO *Obj, const Elf_Shdr *SymTab, const Elf_Rela &R, bool IsRela); + void printRelocation(const ELFO *Obj, const Elf_Sym *Sym, + StringRef SymbolName, const Elf_Rela &R, bool IsRela); void printSymbol(const ELFO *Obj, const Elf_Sym *Symbol, const Elf_Sym *First, StringRef StrTable, bool IsDynamic) override; std::string getSymbolSectionNdx(const ELFO *Obj, const Elf_Sym *Symbol, @@ -2602,12 +2604,6 @@ template <class ELFT> void GNUStyle<ELFT>::printGroupSections(const ELFO *Obj) { template <class ELFT> void GNUStyle<ELFT>::printRelocation(const ELFO *Obj, const Elf_Shdr *SymTab, const Elf_Rela &R, bool IsRela) { - // First two fields are bit width dependent. The rest of them are after are - // fixed width. - unsigned Bias = ELFT::Is64Bits ? 8 : 0; - Field Fields[5] = {0, 10 + Bias, 19 + 2 * Bias, 42 + 2 * Bias, 53 + 2 * Bias}; - SmallString<32> RelocName; - Obj->getRelocationTypeName(R.getType(Obj->isMips64EL()), RelocName); const Elf_Sym *Sym = unwrapOrError(Obj->getRelocationSymbol(&R, SymTab)); std::string TargetName; if (Sym && Sym->getType() == ELF::STT_SECTION) { @@ -2619,21 +2615,36 @@ void GNUStyle<ELFT>::printRelocation(const ELFO *Obj, const Elf_Shdr *SymTab, TargetName = this->dumper()->getFullSymbolName( Sym, StrTable, SymTab->sh_type == SHT_DYNSYM /* IsDynamic */); } + printRelocation(Obj, Sym, TargetName, R, IsRela); +} +template <class ELFT> +void GNUStyle<ELFT>::printRelocation(const ELFO *Obj, const Elf_Sym *Sym, + StringRef SymbolName, const Elf_Rela &R, + bool IsRela) { + // First two fields are bit width dependent. The rest of them are fixed width. + unsigned Bias = ELFT::Is64Bits ? 8 : 0; + Field Fields[5] = {0, 10 + Bias, 19 + 2 * Bias, 42 + 2 * Bias, 53 + 2 * Bias}; unsigned Width = ELFT::Is64Bits ? 16 : 8; + Fields[0].Str = to_string(format_hex_no_prefix(R.r_offset, Width)); Fields[1].Str = to_string(format_hex_no_prefix(R.r_info, Width)); - Fields[2].Str = RelocName.str(); - if (Sym) + + SmallString<32> RelocName; + Obj->getRelocationTypeName(R.getType(Obj->isMips64EL()), RelocName); + Fields[2].Str = RelocName.c_str(); + + if (Sym && (!SymbolName.empty() || Sym->getValue() != 0)) Fields[3].Str = to_string(format_hex_no_prefix(Sym->getValue(), Width)); - Fields[4].Str = TargetName; - for (auto &F : Fields) + + Fields[4].Str = SymbolName; + for (const Field &F : Fields) printField(F); std::string Addend; if (IsRela) { int64_t RelAddend = R.r_addend; - if (Sym) { + if (!SymbolName.empty()) { if (R.r_addend < 0) { Addend = " - "; RelAddend = std::abs(RelAddend); @@ -3267,44 +3278,11 @@ void GNUStyle<ELFT>::printSectionMapping(const ELFO *Obj) { template <class ELFT> void GNUStyle<ELFT>::printDynamicRelocation(const ELFO *Obj, Elf_Rela R, bool IsRela) { - unsigned Bias = ELFT::Is64Bits ? 8 : 0; - // First two fields are bit width dependent. The rest of them are after are - // fixed width. - Field Fields[5] = {0, 10 + Bias, 19 + 2 * Bias, 42 + 2 * Bias, 53 + 2 * Bias}; - - unsigned Width = ELFT::Is64Bits ? 16 : 8; - Fields[0].Str = to_string(format_hex_no_prefix(R.r_offset, Width)); - Fields[1].Str = to_string(format_hex_no_prefix(R.r_info, Width)); - uint32_t SymIndex = R.getSymbol(Obj->isMips64EL()); const Elf_Sym *Sym = this->dumper()->dynamic_symbols().begin() + SymIndex; - SmallString<32> RelocName; - Obj->getRelocationTypeName(R.getType(Obj->isMips64EL()), RelocName); - Fields[2].Str = RelocName.c_str(); - std::string SymbolName = maybeDemangle( unwrapOrError(Sym->getName(this->dumper()->getDynamicStringTable()))); - - if (!SymbolName.empty() || Sym->getValue() != 0) - Fields[3].Str = to_string(format_hex_no_prefix(Sym->getValue(), Width)); - - Fields[4].Str = SymbolName; - for (auto &Field : Fields) - printField(Field); - - std::string Addend; - if (IsRela) { - int64_t RelAddend = R.r_addend; - if (!SymbolName.empty()) { - if (R.r_addend < 0) { - Addend = " - "; - RelAddend = std::abs(RelAddend); - } else - Addend = " + "; - } - Addend += to_string(format_hex_no_prefix(RelAddend, 1)); - } - OS << Addend << "\n"; + printRelocation(Obj, Sym, SymbolName, R, IsRela); } template <class ELFT> |