[llvm-readobj] Fix JSON output for Relocations

The existing JSON incorrectly outputs line breaks and other invalid JSON.

Example Before this patch:

```
...
"Relocations":[Section (9) .rela.dyn {
  0xA3B0 R_X86_64_RELATIVE - 0x43D0
  0xA3B8 R_X86_64_RELATIVE - 0x4A30
...
```

Example After this patch:

```
...
"Relocations":[Section (9) .rela.dyn {
{"Relocation":{"Offset":41904,"Type":{"Value":"R_X86_64_RELATIVE","RawValue":8},
"Symbol":{"Value":"","RawValue":0},"Addend":17360}},
{"Relocation":{"Offset":41912,"Type":{"Value":"R_X86_64_RELATIVE","RawValue":8},
"Symbol":{"Value":"","RawValue":0},"Addend":18992}},
{"Relocation":{"Offset":41920,"Type":{"Value":"R_X86_64_RELATIVE","RawValue":8},
"Symbol":{"Value":"","RawValue":0},"Addend":17440}},
...
```

Note there are still issues with the Section, but each Relocation is
now a valid JSON object that can be parsed. Future patches will address
the issues regarding JSON output for the Section.

Reviewed By: jhenderson

Differential Revision: https://reviews.llvm.org/D137089
This commit is contained in:
Paul Kirth 2023-03-17 23:40:23 +00:00
parent 8e1746faa3
commit 23fd6e360c
2 changed files with 177 additions and 16 deletions

View File

@ -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 \

View File

@ -713,6 +713,13 @@ private:
protected:
void printSymbolOtherField(const Elf_Sym &Symbol) const;
virtual void printExpandedRelRelaReloc(const Relocation<ELFT> &R,
StringRef SymbolName,
StringRef RelocName);
virtual void printDefaultRelRelaReloc(const Relocation<ELFT> &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<ELFT> &R,
StringRef SymbolName,
StringRef RelocName) override;
private:
std::unique_ptr<DictScope> FileScope;
};
@ -6743,6 +6754,30 @@ void LLVMELFDumper<ELFT>::printRelrReloc(const Elf_Relr &R) {
W.startLine() << W.hex(R) << "\n";
}
template <class ELFT>
void LLVMELFDumper<ELFT>::printExpandedRelRelaReloc(const Relocation<ELFT> &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 <class ELFT>
void LLVMELFDumper<ELFT>::printDefaultRelRelaReloc(const Relocation<ELFT> &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 <class ELFT>
void LLVMELFDumper<ELFT>::printRelRelaReloc(const Relocation<ELFT> &R,
const RelSymbol<ELFT> &RelSym) {
@ -6751,19 +6786,9 @@ void LLVMELFDumper<ELFT>::printRelRelaReloc(const Relocation<ELFT> &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<ELFT>::printZeroSymbolOtherField(
// always print the `Other` field the same way.
this->printSymbolOtherField(Symbol);
}
template <class ELFT>
void JSONELFDumper<ELFT>::printDefaultRelRelaReloc(const Relocation<ELFT> &R,
StringRef SymbolName,
StringRef RelocName) {
this->printExpandedRelRelaReloc(R, SymbolName, RelocName);
}