diff --git a/llvm/test/tools/llvm-readobj/ELF/relocations.test b/llvm/test/tools/llvm-readobj/ELF/relocations.test index 360d3689d665..5c9e0293e496 100644 --- a/llvm/test/tools/llvm-readobj/ELF/relocations.test +++ b/llvm/test/tools/llvm-readobj/ELF/relocations.test @@ -47,8 +47,7 @@ # GNU-64-NEXT:ffffffffffffffff 0000000800000001 R_X86_64_64 0000000000000003 rela_minneg - 8000000000000000 # GNU-64-NEXT:0000000000000009 000000090000000b R_X86_64_32S ffffffffffffffff rela_maxpos + 7fffffffffffffff -## Show that --expand-relocs expands the relocation dump for LLVM style only -## (and not GNU). +## Show that --expand-relocs expands the relocation dump for LLVM style and not GNU. # RUN: llvm-readobj -r --expand-relocs %t64 \ # RUN: | FileCheck %s --check-prefix=LLVM-EXPAND-64 --match-full-lines --strict-whitespace # RUN: llvm-readelf -r --expand-relocs %t64 \ @@ -111,6 +110,137 @@ # LLVM-EXPAND-64-NEXT: } # LLVM-EXPAND-64-NEXT:] +## Show that JSON relocations are always expanded. +# RUN: llvm-readobj -r %t64 --elf-output-style=JSON --pretty-print \ +# RUN: | FileCheck %s --check-prefix=JSON-EXPAND-64 +# RUN: llvm-readobj -r --expand-relocs %t64 --elf-output-style=JSON --pretty-print \ +# RUN: | FileCheck %s --check-prefix=JSON-EXPAND-64 + +# JSON-EXPAND-64: "Relocations": [Section (2) .rel.text { +# JSON-EXPAND-64: "Relocation": { +# JSON-EXPAND-64-NEXT: "Offset": 0, +# JSON-EXPAND-64-NEXT: "Type": { +# JSON-EXPAND-64-NEXT: "Value": "R_X86_64_NONE", +# JSON-EXPAND-64-NEXT: "RawValue": 0 +# JSON-EXPAND-64-NEXT: }, +# JSON-EXPAND-64-NEXT: "Symbol": { +# JSON-EXPAND-64-NEXT: "Value": "rel_0", +# JSON-EXPAND-64-NEXT: "RawValue": 1 +# JSON-EXPAND-64-NEXT: } +# JSON-EXPAND-64-NEXT: } +# JSON-EXPAND-64-NEXT: }, +# JSON-EXPAND-64-NEXT: { +# JSON-EXPAND-64-NEXT: "Relocation": { +# JSON-EXPAND-64-NEXT: "Offset": 1, +# JSON-EXPAND-64-NEXT: "Type": { +# JSON-EXPAND-64-NEXT: "Value": "R_X86_64_PC32", +# JSON-EXPAND-64-NEXT: "RawValue": 2 +# JSON-EXPAND-64-NEXT: }, +# JSON-EXPAND-64-NEXT: "Symbol": { +# JSON-EXPAND-64-NEXT: "Value": "rel_neg", +# JSON-EXPAND-64-NEXT: "RawValue": 2 +# JSON-EXPAND-64-NEXT: } +# JSON-EXPAND-64-NEXT: } +# JSON-EXPAND-64-NEXT: }, +# JSON-EXPAND-64-NEXT: { +# JSON-EXPAND-64-NEXT: "Relocation": { +# JSON-EXPAND-64-NEXT: "Offset": 5, +# JSON-EXPAND-64-NEXT: "Type": { +# JSON-EXPAND-64-NEXT: "Value": "R_X86_64_PLT32", +# JSON-EXPAND-64-NEXT: "RawValue": 4 +# JSON-EXPAND-64-NEXT: }, +# JSON-EXPAND-64-NEXT: "Symbol": { +# JSON-EXPAND-64-NEXT: "Value": "rel_pos", +# JSON-EXPAND-64-NEXT: "RawValue": 3 +# JSON-EXPAND-64-NEXT: } +# JSON-EXPAND-64-NEXT: } +# JSON-EXPAND-64-NEXT: }, +# JSON-EXPAND-64-NEXT: { +# JSON-EXPAND-64-NEXT: "Relocation": { +# JSON-EXPAND-64-NEXT: "Offset": 9, +# JSON-EXPAND-64-NEXT: "Type": { +# JSON-EXPAND-64-NEXT: "Value": "R_X86_64_64", +# JSON-EXPAND-64-NEXT: "RawValue": 1 +# JSON-EXPAND-64-NEXT: }, +# JSON-EXPAND-64-NEXT: "Symbol": { +# JSON-EXPAND-64-NEXT: "Value": "rel_64", +# JSON-EXPAND-64-NEXT: "RawValue": 4 +# JSON-EXPAND-64-NEXT: } +# JSON-EXPAND-64-NEXT: } +# JSON-EXPAND-64-NEXT: }} +# JSON-EXPAND-64-NEXT: Section (3) .rela.text { +# JSON-EXPAND-64-NEXT: , +# JSON-EXPAND-64-NEXT: { +# JSON-EXPAND-64-NEXT: "Relocation": { +# JSON-EXPAND-64-NEXT: "Offset": 0, +# JSON-EXPAND-64-NEXT: "Type": { +# JSON-EXPAND-64-NEXT: "Value": "R_X86_64_NONE", +# JSON-EXPAND-64-NEXT: "RawValue": 0 +# JSON-EXPAND-64-NEXT: }, +# JSON-EXPAND-64-NEXT: "Symbol": { +# JSON-EXPAND-64-NEXT: "Value": "rela_0", +# JSON-EXPAND-64-NEXT: "RawValue": 5 +# JSON-EXPAND-64-NEXT: }, +# JSON-EXPAND-64-NEXT: "Addend": 0 +# JSON-EXPAND-64-NEXT: } +# JSON-EXPAND-64-NEXT: }, +# JSON-EXPAND-64-NEXT: { +# JSON-EXPAND-64-NEXT: "Relocation": { +# JSON-EXPAND-64-NEXT: "Offset": 1, +# JSON-EXPAND-64-NEXT: "Type": { +# JSON-EXPAND-64-NEXT: "Value": "R_X86_64_PC32", +# JSON-EXPAND-64-NEXT: "RawValue": 2 +# JSON-EXPAND-64-NEXT: }, +# JSON-EXPAND-64-NEXT: "Symbol": { +# JSON-EXPAND-64-NEXT: "Value": "rela_neg", +# JSON-EXPAND-64-NEXT: "RawValue": 6 +# JSON-EXPAND-64-NEXT: }, +# JSON-EXPAND-64-NEXT: "Addend": 18446744073709551615 +# JSON-EXPAND-64-NEXT: } +# JSON-EXPAND-64-NEXT: }, +# JSON-EXPAND-64-NEXT: { +# JSON-EXPAND-64-NEXT: "Relocation": { +# JSON-EXPAND-64-NEXT: "Offset": 5, +# JSON-EXPAND-64-NEXT: "Type": { +# JSON-EXPAND-64-NEXT: "Value": "R_X86_64_PLT32", +# JSON-EXPAND-64-NEXT: "RawValue": 4 +# JSON-EXPAND-64-NEXT: }, +# JSON-EXPAND-64-NEXT: "Symbol": { +# JSON-EXPAND-64-NEXT: "Value": "rela_pos", +# JSON-EXPAND-64-NEXT: "RawValue": 7 +# JSON-EXPAND-64-NEXT: }, +# JSON-EXPAND-64-NEXT: "Addend": 2 +# JSON-EXPAND-64-NEXT: } +# JSON-EXPAND-64-NEXT: }, +# JSON-EXPAND-64-NEXT: { +# JSON-EXPAND-64-NEXT: "Relocation": { +# JSON-EXPAND-64-NEXT: "Offset": 18446744073709551615, +# JSON-EXPAND-64-NEXT: "Type": { +# JSON-EXPAND-64-NEXT: "Value": "R_X86_64_64", +# JSON-EXPAND-64-NEXT: "RawValue": 1 +# JSON-EXPAND-64-NEXT: }, +# JSON-EXPAND-64-NEXT: "Symbol": { +# JSON-EXPAND-64-NEXT: "Value": "rela_minneg", +# JSON-EXPAND-64-NEXT: "RawValue": 8 +# JSON-EXPAND-64-NEXT: }, +# JSON-EXPAND-64-NEXT: "Addend": 9223372036854775808 +# JSON-EXPAND-64-NEXT: } +# JSON-EXPAND-64-NEXT: }, +# JSON-EXPAND-64-NEXT: { +# JSON-EXPAND-64-NEXT: "Relocation": { +# JSON-EXPAND-64-NEXT: "Offset": 9, +# JSON-EXPAND-64-NEXT: "Type": { +# JSON-EXPAND-64-NEXT: "Value": "R_X86_64_32S", +# JSON-EXPAND-64-NEXT: "RawValue": 11 +# JSON-EXPAND-64-NEXT: }, +# JSON-EXPAND-64-NEXT: "Symbol": { +# JSON-EXPAND-64-NEXT: "Value": "rela_maxpos", +# JSON-EXPAND-64-NEXT: "RawValue": 9 +# JSON-EXPAND-64-NEXT: }, +# JSON-EXPAND-64-NEXT: "Addend": 9223372036854775807 +# JSON-EXPAND-64-NEXT: } +# JSON-EXPAND-64-NEXT: }} + --- !ELF FileHeader: Class: ELFCLASS64 @@ -330,8 +460,7 @@ Symbols: # GNU-32-NEXT:ffffffff 00000701 R_386_32 00000003 rela_minneg - 80000000 # GNU-32-NEXT:00000009 00000803 R_386_GOT32 ffffffff rela_maxpos + 7fffffff -## Show that --expand-relocs expands the relocation dump for LLVM style only -## (and not GNU). +## Show that --expand-relocs expands the relocation dump for LLVM style and not GNU. # RUN: llvm-readobj -r --expand-relocs %t32 \ # RUN: | FileCheck %s --check-prefix=LLVM-EXPAND-32 --strict-whitespace --match-full-lines # RUN: llvm-readelf -r --expand-relocs %t32 \ diff --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp index 7e832ed6d787..a0911de8f12e 100644 --- a/llvm/tools/llvm-readobj/ELFDumper.cpp +++ b/llvm/tools/llvm-readobj/ELFDumper.cpp @@ -713,6 +713,13 @@ private: protected: void printSymbolOtherField(const Elf_Sym &Symbol) const; + virtual void printExpandedRelRelaReloc(const Relocation &R, + StringRef SymbolName, + StringRef RelocName); + virtual void printDefaultRelRelaReloc(const Relocation &R, + StringRef SymbolName, + StringRef RelocName); + ScopedPrinter &W; }; @@ -730,6 +737,10 @@ public: const Archive *A) override; virtual void printZeroSymbolOtherField(const Elf_Sym &Symbol) const override; + void printDefaultRelRelaReloc(const Relocation &R, + StringRef SymbolName, + StringRef RelocName) override; + private: std::unique_ptr FileScope; }; @@ -6743,6 +6754,30 @@ void LLVMELFDumper::printRelrReloc(const Elf_Relr &R) { W.startLine() << W.hex(R) << "\n"; } +template +void LLVMELFDumper::printExpandedRelRelaReloc(const Relocation &R, + StringRef SymbolName, + StringRef RelocName) { + DictScope Group(W, "Relocation"); + W.printHex("Offset", R.Offset); + W.printNumber("Type", RelocName, R.Type); + W.printNumber("Symbol", !SymbolName.empty() ? SymbolName : "-", R.Symbol); + if (R.Addend) + W.printHex("Addend", (uintX_t)*R.Addend); +} + +template +void LLVMELFDumper::printDefaultRelRelaReloc(const Relocation &R, + StringRef SymbolName, + StringRef RelocName) { + raw_ostream &OS = W.startLine(); + OS << W.hex(R.Offset) << " " << RelocName << " " + << (!SymbolName.empty() ? SymbolName : "-"); + if (R.Addend) + OS << " " << W.hex((uintX_t)*R.Addend); + OS << "\n"; +} + template void LLVMELFDumper::printRelRelaReloc(const Relocation &R, const RelSymbol &RelSym) { @@ -6751,19 +6786,9 @@ void LLVMELFDumper::printRelRelaReloc(const Relocation &R, this->Obj.getRelocationTypeName(R.Type, RelocName); if (opts::ExpandRelocs) { - DictScope Group(W, "Relocation"); - W.printHex("Offset", R.Offset); - W.printNumber("Type", RelocName, R.Type); - W.printNumber("Symbol", !SymbolName.empty() ? SymbolName : "-", R.Symbol); - if (R.Addend) - W.printHex("Addend", (uintX_t)*R.Addend); + printExpandedRelRelaReloc(R, SymbolName, RelocName); } else { - raw_ostream &OS = W.startLine(); - OS << W.hex(R.Offset) << " " << RelocName << " " - << (!SymbolName.empty() ? SymbolName : "-"); - if (R.Addend) - OS << " " << W.hex((uintX_t)*R.Addend); - OS << "\n"; + printDefaultRelRelaReloc(R, SymbolName, RelocName); } } @@ -7682,3 +7707,10 @@ void JSONELFDumper::printZeroSymbolOtherField( // always print the `Other` field the same way. this->printSymbolOtherField(Symbol); } + +template +void JSONELFDumper::printDefaultRelRelaReloc(const Relocation &R, + StringRef SymbolName, + StringRef RelocName) { + this->printExpandedRelRelaReloc(R, SymbolName, RelocName); +}