[llvm-readobj] Fix "Section" output when emitting relocations in JSON
authorPaul Kirth <paulkirth@google.com>
Sat, 18 Mar 2023 00:05:41 +0000 (00:05 +0000)
committerPaul Kirth <paulkirth@google.com>
Sat, 18 Mar 2023 01:35:50 +0000 (01:35 +0000)
commit681d5eecf7b32748763e5a8f56f7c334d8d6901e
tree185a0361fa6a2c150ad9721c84ba10d4959c79f1
parent711ff37b554b2819f5fad02bc263c8044d762a3c
[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
llvm/test/tools/llvm-readobj/ELF/relocations.test
llvm/tools/llvm-readobj/ELFDumper.cpp