[llvm] 681d5ee - [llvm-readobj] Fix "Section" output when emitting relocations in JSON
Paul Kirth via llvm-commits
llvm-commits at lists.llvm.org
Fri Mar 17 18:35:59 PDT 2023
Author: Paul Kirth
Date: 2023-03-18T01:35:50Z
New Revision: 681d5eecf7b32748763e5a8f56f7c334d8d6901e
URL: https://github.com/llvm/llvm-project/commit/681d5eecf7b32748763e5a8f56f7c334d8d6901e
DIFF: https://github.com/llvm/llvm-project/commit/681d5eecf7b32748763e5a8f56f7c334d8d6901e.diff
LOG: [llvm-readobj] Fix "Section" output when emitting relocations in JSON
Prior to this patch, the JSON output would emit an invalid key from the
shared LLVM implementation. This caused llvm-readobj to output invalid
JSON. This patch introduces a small helper function to print the
relocation information differently between the LLVM and JSON formats.
Before this patch:
```
"Relocations": [Section (2) .rel.text {
{
"Relocation": {
"Offset": 0,
"Type": {
"Value": "R_X86_64_NONE",
"RawValue": 0
},
"Symbol": {
"Value": "rel_0",
"RawValue": 1
}
}
},
...
```
After this patch:
```
"Relocations": [
{
"SectionIdx": 2,
"Relocs": [
{
"Relocation": {
"Offset": 0,
"Type": {
"Name": "R_X86_64_NONE",
"Value": 0
},
"Symbol": {
"Name": "rel_0",
"Value": 1
}
}
},
...
```
Reviewed By: jhenderson
Differential Revision: https://reviews.llvm.org/D137094
Added:
Modified:
llvm/test/tools/llvm-readobj/ELF/relocations.test
llvm/tools/llvm-readobj/ELFDumper.cpp
Removed:
################################################################################
diff --git a/llvm/test/tools/llvm-readobj/ELF/relocations.test b/llvm/test/tools/llvm-readobj/ELF/relocations.test
index 34a610b6af63..34cff4098514 100644
--- a/llvm/test/tools/llvm-readobj/ELF/relocations.test
+++ b/llvm/test/tools/llvm-readobj/ELF/relocations.test
@@ -116,130 +116,140 @@
# 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: "Name": "R_X86_64_NONE",
-# JSON-EXPAND-64-NEXT: "Value": 0
+# JSON-EXPAND-64: "Relocations": [
+# JSON-EXPAND-64-NEXT: {
+# JSON-EXPAND-64-NEXT: "SectionIndex": 2
+# JSON-EXPAND-64-NEXT: "Relocs": [
+# JSON-EXPAND-64-NEXT: {
+# JSON-EXPAND-64-NEXT: "Relocation": {
+# JSON-EXPAND-64-NEXT: "Offset": 0,
+# JSON-EXPAND-64-NEXT: "Type": {
+# JSON-EXPAND-64-NEXT: "Name": "R_X86_64_NONE",
+# JSON-EXPAND-64-NEXT: "Value": 0
+# JSON-EXPAND-64-NEXT: },
+# JSON-EXPAND-64-NEXT: "Symbol": {
+# JSON-EXPAND-64-NEXT: "Name": "rel_0",
+# JSON-EXPAND-64-NEXT: "Value": 1
+# JSON-EXPAND-64-NEXT: }
+# JSON-EXPAND-64-NEXT: }
# JSON-EXPAND-64-NEXT: },
-# JSON-EXPAND-64-NEXT: "Symbol": {
-# JSON-EXPAND-64-NEXT: "Name": "rel_0",
-# JSON-EXPAND-64-NEXT: "Value": 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: "Name": "R_X86_64_PC32",
-# JSON-EXPAND-64-NEXT: "Value": 2
-# JSON-EXPAND-64-NEXT: },
-# JSON-EXPAND-64-NEXT: "Symbol": {
-# JSON-EXPAND-64-NEXT: "Name": "rel_neg",
-# JSON-EXPAND-64-NEXT: "Value": 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: "Name": "R_X86_64_PLT32",
-# JSON-EXPAND-64-NEXT: "Value": 4
+# JSON-EXPAND-64-NEXT: {
+# JSON-EXPAND-64-NEXT: "Relocation": {
+# JSON-EXPAND-64-NEXT: "Offset": 1,
+# JSON-EXPAND-64-NEXT: "Type": {
+# JSON-EXPAND-64-NEXT: "Name": "R_X86_64_PC32",
+# JSON-EXPAND-64-NEXT: "Value": 2
+# JSON-EXPAND-64-NEXT: },
+# JSON-EXPAND-64-NEXT: "Symbol": {
+# JSON-EXPAND-64-NEXT: "Name": "rel_neg",
+# JSON-EXPAND-64-NEXT: "Value": 2
+# JSON-EXPAND-64-NEXT: }
+# JSON-EXPAND-64-NEXT: }
# JSON-EXPAND-64-NEXT: },
-# JSON-EXPAND-64-NEXT: "Symbol": {
-# JSON-EXPAND-64-NEXT: "Name": "rel_pos",
-# JSON-EXPAND-64-NEXT: "Value": 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: "Name": "R_X86_64_64",
-# JSON-EXPAND-64-NEXT: "Value": 1
+# JSON-EXPAND-64-NEXT: {
+# JSON-EXPAND-64-NEXT: "Relocation": {
+# JSON-EXPAND-64-NEXT: "Offset": 5,
+# JSON-EXPAND-64-NEXT: "Type": {
+# JSON-EXPAND-64-NEXT: "Name": "R_X86_64_PLT32",
+# JSON-EXPAND-64-NEXT: "Value": 4
+# JSON-EXPAND-64-NEXT: },
+# JSON-EXPAND-64-NEXT: "Symbol": {
+# JSON-EXPAND-64-NEXT: "Name": "rel_pos",
+# JSON-EXPAND-64-NEXT: "Value": 3
+# JSON-EXPAND-64-NEXT: }
+# JSON-EXPAND-64-NEXT: }
# JSON-EXPAND-64-NEXT: },
-# JSON-EXPAND-64-NEXT: "Symbol": {
-# JSON-EXPAND-64-NEXT: "Name": "rel_64",
-# JSON-EXPAND-64-NEXT: "Value": 4
+# JSON-EXPAND-64-NEXT: {
+# JSON-EXPAND-64-NEXT: "Relocation": {
+# JSON-EXPAND-64-NEXT: "Offset": 9,
+# JSON-EXPAND-64-NEXT: "Type": {
+# JSON-EXPAND-64-NEXT: "Name": "R_X86_64_64",
+# JSON-EXPAND-64-NEXT: "Value": 1
+# JSON-EXPAND-64-NEXT: },
+# JSON-EXPAND-64-NEXT: "Symbol": {
+# JSON-EXPAND-64-NEXT: "Name": "rel_64",
+# JSON-EXPAND-64-NEXT: "Value": 4
+# JSON-EXPAND-64-NEXT: }
+# JSON-EXPAND-64-NEXT: }
# 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: "Name": "R_X86_64_NONE",
-# JSON-EXPAND-64-NEXT: "Value": 0
-# JSON-EXPAND-64-NEXT: },
-# JSON-EXPAND-64-NEXT: "Symbol": {
-# JSON-EXPAND-64-NEXT: "Name": "rela_0",
-# JSON-EXPAND-64-NEXT: "Value": 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: "Name": "R_X86_64_PC32",
-# JSON-EXPAND-64-NEXT: "Value": 2
+# JSON-EXPAND-64-NEXT: ]
+# JSON-EXPAND-64-NEXT: },
+# JSON-EXPAND-64-NEXT: {
+# JSON-EXPAND-64-NEXT: "SectionIndex": 3,
+# JSON-EXPAND-64-NEXT: "Relocs": [
+# JSON-EXPAND-64-NEXT: {
+# JSON-EXPAND-64-NEXT: "Relocation": {
+# JSON-EXPAND-64-NEXT: "Offset": 0,
+# JSON-EXPAND-64-NEXT: "Type": {
+# JSON-EXPAND-64-NEXT: "Name": "R_X86_64_NONE",
+# JSON-EXPAND-64-NEXT: "Value": 0
+# JSON-EXPAND-64-NEXT: },
+# JSON-EXPAND-64-NEXT: "Symbol": {
+# JSON-EXPAND-64-NEXT: "Name": "rela_0",
+# JSON-EXPAND-64-NEXT: "Value": 5
+# JSON-EXPAND-64-NEXT: },
+# JSON-EXPAND-64-NEXT: "Addend": 0
+# JSON-EXPAND-64-NEXT: }
# JSON-EXPAND-64-NEXT: },
-# JSON-EXPAND-64-NEXT: "Symbol": {
-# JSON-EXPAND-64-NEXT: "Name": "rela_neg",
-# JSON-EXPAND-64-NEXT: "Value": 6
+# JSON-EXPAND-64-NEXT: {
+# JSON-EXPAND-64-NEXT: "Relocation": {
+# JSON-EXPAND-64-NEXT: "Offset": 1,
+# JSON-EXPAND-64-NEXT: "Type": {
+# JSON-EXPAND-64-NEXT: "Name": "R_X86_64_PC32",
+# JSON-EXPAND-64-NEXT: "Value": 2
+# JSON-EXPAND-64-NEXT: },
+# JSON-EXPAND-64-NEXT: "Symbol": {
+# JSON-EXPAND-64-NEXT: "Name": "rela_neg",
+# JSON-EXPAND-64-NEXT: "Value": 6
+# JSON-EXPAND-64-NEXT: },
+# JSON-EXPAND-64-NEXT: "Addend": 18446744073709551615
+# JSON-EXPAND-64-NEXT: }
# 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: "Name": "R_X86_64_PLT32",
-# JSON-EXPAND-64-NEXT: "Value": 4
+# JSON-EXPAND-64-NEXT: {
+# JSON-EXPAND-64-NEXT: "Relocation": {
+# JSON-EXPAND-64-NEXT: "Offset": 5,
+# JSON-EXPAND-64-NEXT: "Type": {
+# JSON-EXPAND-64-NEXT: "Name": "R_X86_64_PLT32",
+# JSON-EXPAND-64-NEXT: "Value": 4
+# JSON-EXPAND-64-NEXT: },
+# JSON-EXPAND-64-NEXT: "Symbol": {
+# JSON-EXPAND-64-NEXT: "Name": "rela_pos",
+# JSON-EXPAND-64-NEXT: "Value": 7
+# JSON-EXPAND-64-NEXT: },
+# JSON-EXPAND-64-NEXT: "Addend": 2
+# JSON-EXPAND-64-NEXT: }
# JSON-EXPAND-64-NEXT: },
-# JSON-EXPAND-64-NEXT: "Symbol": {
-# JSON-EXPAND-64-NEXT: "Name": "rela_pos",
-# JSON-EXPAND-64-NEXT: "Value": 7
+# JSON-EXPAND-64-NEXT: {
+# JSON-EXPAND-64-NEXT: "Relocation": {
+# JSON-EXPAND-64-NEXT: "Offset": 18446744073709551615,
+# JSON-EXPAND-64-NEXT: "Type": {
+# JSON-EXPAND-64-NEXT: "Name": "R_X86_64_64",
+# JSON-EXPAND-64-NEXT: "Value": 1
+# JSON-EXPAND-64-NEXT: },
+# JSON-EXPAND-64-NEXT: "Symbol": {
+# JSON-EXPAND-64-NEXT: "Name": "rela_minneg",
+# JSON-EXPAND-64-NEXT: "Value": 8
+# JSON-EXPAND-64-NEXT: },
+# JSON-EXPAND-64-NEXT: "Addend": 9223372036854775808
+# JSON-EXPAND-64-NEXT: }
# 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: "Name": "R_X86_64_64",
-# JSON-EXPAND-64-NEXT: "Value": 1
-# JSON-EXPAND-64-NEXT: },
-# JSON-EXPAND-64-NEXT: "Symbol": {
-# JSON-EXPAND-64-NEXT: "Name": "rela_minneg",
-# JSON-EXPAND-64-NEXT: "Value": 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: "Name": "R_X86_64_32S",
-# JSON-EXPAND-64-NEXT: "Value": 11
-# JSON-EXPAND-64-NEXT: },
-# JSON-EXPAND-64-NEXT: "Symbol": {
-# JSON-EXPAND-64-NEXT: "Name": "rela_maxpos",
-# JSON-EXPAND-64-NEXT: "Value": 9
-# JSON-EXPAND-64-NEXT: },
-# JSON-EXPAND-64-NEXT: "Addend": 9223372036854775807
-# 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: "Name": "R_X86_64_32S",
+# JSON-EXPAND-64-NEXT: "Value": 11
+# JSON-EXPAND-64-NEXT: },
+# JSON-EXPAND-64-NEXT: "Symbol": {
+# JSON-EXPAND-64-NEXT: "Name": "rela_maxpos",
+# JSON-EXPAND-64-NEXT: "Value": 9
+# JSON-EXPAND-64-NEXT: },
+# JSON-EXPAND-64-NEXT: "Addend": 9223372036854775807
+# JSON-EXPAND-64-NEXT: }
+# JSON-EXPAND-64-NEXT: }
+# JSON-EXPAND-64-NEXT: ]
+# JSON-EXPAND-64-NEXT: }
+# JSON-EXPAND-64-NEXT: ]
--- !ELF
FileHeader:
diff --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp
index a0911de8f12e..7ff1435fc808 100644
--- a/llvm/tools/llvm-readobj/ELFDumper.cpp
+++ b/llvm/tools/llvm-readobj/ELFDumper.cpp
@@ -719,6 +719,8 @@ template <typename ELFT> class LLVMELFDumper : public ELFDumper<ELFT> {
virtual void printDefaultRelRelaReloc(const Relocation<ELFT> &R,
StringRef SymbolName,
StringRef RelocName);
+ virtual void printRelocationSectionInfo(const Elf_Shdr &Sec, StringRef Name,
+ const unsigned SecNdx);
ScopedPrinter &W;
};
@@ -741,6 +743,9 @@ template <typename ELFT> class JSONELFDumper : public LLVMELFDumper<ELFT> {
StringRef SymbolName,
StringRef RelocName) override;
+ void printRelocationSectionInfo(const Elf_Shdr &Sec, StringRef Name,
+ const unsigned SecNdx) override;
+
private:
std::unique_ptr<DictScope> FileScope;
};
@@ -6741,11 +6746,7 @@ template <class ELFT> void LLVMELFDumper<ELFT>::printRelocations() {
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";
+ printRelocationSectionInfo(Sec, Name, SecNdx);
}
}
@@ -6778,6 +6779,14 @@ void LLVMELFDumper<ELFT>::printDefaultRelRelaReloc(const Relocation<ELFT> &R,
OS << "\n";
}
+template <class ELFT>
+void LLVMELFDumper<ELFT>::printRelocationSectionInfo(const Elf_Shdr &Sec,
+ StringRef Name,
+ const unsigned SecNdx) {
+ DictScope D(W, (Twine("Section (") + Twine(SecNdx) + ") " + Name).str());
+ this->printRelocationsHelper(Sec);
+}
+
template <class ELFT>
void LLVMELFDumper<ELFT>::printRelRelaReloc(const Relocation<ELFT> &R,
const RelSymbol<ELFT> &RelSym) {
@@ -7714,3 +7723,13 @@ void JSONELFDumper<ELFT>::printDefaultRelRelaReloc(const Relocation<ELFT> &R,
StringRef RelocName) {
this->printExpandedRelRelaReloc(R, SymbolName, RelocName);
}
+
+template <class ELFT>
+void JSONELFDumper<ELFT>::printRelocationSectionInfo(const Elf_Shdr &Sec,
+ StringRef Name,
+ const unsigned SecNdx) {
+ DictScope Group(this->W);
+ this->W.printNumber("SectionIndex", SecNdx);
+ ListScope D(this->W, "Relocs");
+ this->printRelocationsHelper(Sec);
+}
More information about the llvm-commits
mailing list