diff --git a/llvm/test/tools/llvm-readobj/ELF/llvm-vs-json-format.test b/llvm/test/tools/llvm-readobj/ELF/llvm-vs-json-format.test --- a/llvm/test/tools/llvm-readobj/ELF/llvm-vs-json-format.test +++ b/llvm/test/tools/llvm-readobj/ELF/llvm-vs-json-format.test @@ -69,9 +69,12 @@ # LLVM-64-NEXT: } # LLVM-64-NEXT:] -# JSON-64: "Relocations": [Section (2) .rel.text { -# JSON-64: { -# JSON-64-NEXT: "Relocation": { +# JSON-64: "Relocations": [ +# JSON-64-NEXT: { +# JSON-64-NEXT: "SectionIdx": 2, +# JSON-64-NEXT: "Relocs": [ +# JSON-64-NEXT: { +# JSON-64-NEXT: "Relocation": { # JSON-64-NEXT: "Offset": 0, # JSON-64-NEXT: "Type": { # JSON-64-NEXT: "Name": "R_X86_64_NONE", @@ -121,10 +124,13 @@ # JSON-64-NEXT: "Value": 4 # JSON-64-NEXT: } # JSON-64-NEXT: } -# JSON-64-NEXT: }} -# JSON-64-NEXT: Section (3) .rela.text { -# JSON-64-NEXT: , -# JSON-64-NEXT: { +# JSON-64-NEXT: } +# JSON-64-NEXT: ] +# JSON-64-NEXT: }, +# JSON-64-NEXT: { +# JSON-64-NEXT: "SectionIdx": 3, +# JSON-64-NEXT: "Relocs": [ +# JSON-64-NEXT: { # JSON-64-NEXT: "Relocation": { # JSON-64-NEXT: "Offset": 0, # JSON-64-NEXT: "Type": { @@ -193,8 +199,10 @@ # JSON-64-NEXT: }, # JSON-64-NEXT: "Addend": 9223372036854775807 # JSON-64-NEXT: } -# JSON-64-NEXT: }} -# JSON-64: ] +# JSON-64-NEXT: } +# JSON-64-NEXT: ] +# JSON-64-NEXT: } +# JSON-64-NEXT: ] ## Show that --expand-relocs expands the relocation dump for LLVM style but ## the JSON output is always expanded 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 @@ -714,6 +714,8 @@ virtual void printDefaultRelRelaReloc(const Relocation &R, const StringRef SymbolName, const StringRef RelocName); + virtual void printRelocationInfo(const Elf_Shdr &Sec, const StringRef Name, + const unsigned SecNdx); ScopedPrinter &W; }; @@ -736,6 +738,9 @@ const StringRef SymbolName, const StringRef RelocName) override; + void printRelocationInfo(const Elf_Shdr &Sec, const StringRef Name, + const unsigned SecNdx) override; + private: std::unique_ptr FileScope; }; @@ -6682,11 +6687,7 @@ StringRef Name = this->getPrintableSectionName(Sec); unsigned SecNdx = &Sec - &cantFail(this->Obj.sections()).front(); - W.startLine() << "Section (" << SecNdx << ") " << Name << " {\n"; - W.indent(); - this->printRelocationsHelper(Sec); - W.unindent(); - W.startLine() << "}\n"; + printRelocationInfo(Sec, Name, SecNdx); } } @@ -6719,6 +6720,17 @@ OS << "\n"; } +template +void LLVMELFDumper::printRelocationInfo(const Elf_Shdr &Sec, + const StringRef Name, + const unsigned SecNdx) { + W.startLine() << "Section (" << SecNdx << ") " << Name << " {\n"; + W.indent(); + this->printRelocationsHelper(Sec); + W.unindent(); + W.startLine() << "}\n"; +} + template void LLVMELFDumper::printRelRelaReloc(const Relocation &R, const RelSymbol &RelSym) { @@ -7617,3 +7629,13 @@ const StringRef RelocName) { this->printExpandedRelRelaReloc(R, SymbolName, RelocName); } + +template +void JSONELFDumper::printRelocationInfo(const Elf_Shdr &Sec, + const StringRef Name, + const unsigned SecNdx) { + DictScope Group(this->W); + this->W.printNumber("SectionIdx", SecNdx); + ListScope D(this->W, "Relocs"); + this->printRelocationsHelper(Sec); +}