diff --git a/llvm/test/tools/llvm-readobj/ELF/relocations.test b/llvm/test/tools/llvm-readobj/ELF/relocations.test --- 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 @@ -233,7 +363,9 @@ ## because of broken sh_entsize field. # RUN: yaml2obj %s --docnum=1 -DENTSIZERELA=1 -o %t.broken.rela # RUN: llvm-readobj --relocations %t.broken.rela 2>&1 \ -# RUN: | FileCheck %s -DFILE=%t.broken.rela --check-prefix=BROKEN-RELA-LLVM +# RUN: | FileCheck %s -DFILE=%t.broken.rela --check-prefixes=BROKEN-RELA-LLVM,BROKEN-RELA-COMMON +# RUN: llvm-readobj --relocations %t.broken.rela 2>&1 --elf-output-style=JSON --pretty-print \ +# RUN: | FileCheck %s -DFILE=%t.broken.rela --check-prefix=BROKEN-RELA-COMMON # RUN: llvm-readelf --relocations %t.broken.rela 2>&1 \ # RUN: | FileCheck %s -DFILE=%t.broken.rela --check-prefix=BROKEN-RELA-GNU @@ -244,9 +376,9 @@ # BROKEN-RELA-LLVM-NEXT: 0x5 R_X86_64_PLT32 rel_pos{{$}} # BROKEN-RELA-LLVM-NEXT: 0x9 R_X86_64_64 rel_64{{$}} # BROKEN-RELA-LLVM-NEXT: } -# BROKEN-RELA-LLVM-NEXT: Section (3) .rela.text { -# BROKEN-RELA-LLVM-NEXT: warning: '[[FILE]]': unable to read relocations from SHT_RELA section with index 3: section [index 3] has invalid sh_entsize: expected 24, but got 1 -# BROKEN-RELA-LLVM-NEXT: } +# BROKEN-RELA-COMMON: Section (3) .rela.text { +# BROKEN-RELA-COMMON-NEXT: warning: '[[FILE]]': unable to read relocations from SHT_RELA section with index 3: section [index 3] has invalid sh_entsize: expected 24, but got 1 +# BROKEN-RELA-LLVM: } # BROKEN-RELA-LLVM-NEXT: ] # BROKEN-RELA-GNU: Relocation section '.rel.text' at offset 0x51 contains 4 entries: @@ -330,8 +462,7 @@ # 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 --- a/llvm/tools/llvm-readobj/ELFDumper.cpp +++ b/llvm/tools/llvm-readobj/ELFDumper.cpp @@ -708,6 +708,13 @@ protected: void printSymbolOtherField(const Elf_Sym &Symbol) const; + virtual void printExpandedRelRelaReloc(const Relocation &R, + const StringRef SymbolName, + const StringRef RelocName); + virtual void printDefaultRelRelaReloc(const Relocation &R, + const StringRef SymbolName, + const StringRef RelocName); + ScopedPrinter &W; }; @@ -725,6 +732,10 @@ const Archive *A) override; virtual void printZeroSymbolOtherField(const Elf_Sym &Symbol) const override; + void printDefaultRelRelaReloc(const Relocation &R, + const StringRef SymbolName, + const StringRef RelocName) override; + private: std::unique_ptr FileScope; }; @@ -6684,6 +6695,30 @@ W.startLine() << W.hex(R) << "\n"; } +template +void LLVMELFDumper::printExpandedRelRelaReloc(const Relocation &R, + const StringRef SymbolName, + const 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, + const StringRef SymbolName, + const 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) { @@ -6692,19 +6727,9 @@ 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); } } @@ -7585,3 +7610,10 @@ const Elf_Sym &Symbol) const { this->printSymbolOtherField(Symbol); } + +template +void JSONELFDumper::printDefaultRelRelaReloc(const Relocation &R, + const StringRef SymbolName, + const StringRef RelocName) { + this->printExpandedRelRelaReloc(R, SymbolName, RelocName); +}