[PATCH] D143310: [llvm-readobj] Make relocations output valid JSON

Aiden Grossman via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 3 21:50:01 PST 2023


aidengrossman created this revision.
Herald added a subscriber: emaste.
Herald added a reviewer: jhenderson.
Herald added a project: All.
aidengrossman requested review of this revision.
Herald added subscribers: llvm-commits, MaskRay.
Herald added a project: LLVM.

Currently, outputting relocations using --relocs --expand-relocs in
llvm-readobj will produce invalid JSON since the section scopes are
outputted directly. This patch changes the section scopes to use a
DictScope which is automatically printed correctly by the
JSONScopedPrinter, making the JSON valid.

Regression test added.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D143310

Files:
  llvm/test/tools/llvm-readobj/ELF/json-relocations.test
  llvm/tools/llvm-readobj/ELFDumper.cpp


Index: llvm/tools/llvm-readobj/ELFDumper.cpp
===================================================================
--- llvm/tools/llvm-readobj/ELFDumper.cpp
+++ llvm/tools/llvm-readobj/ELFDumper.cpp
@@ -67,6 +67,7 @@
 #include <iterator>
 #include <memory>
 #include <optional>
+#include <sstream>
 #include <string>
 #include <system_error>
 #include <vector>
@@ -6636,11 +6637,10 @@
 
     StringRef Name = this->getPrintableSectionName(Sec);
     unsigned SecNdx = &Sec - &cantFail(this->Obj.sections()).front();
-    W.startLine() << "Section (" << SecNdx << ") " << Name << " {\n";
-    W.indent();
+    std::stringstream DictName;
+    DictName << "Section (" << std::to_string(SecNdx) << ") " << Name.str();
+    DictScope D(W, DictName.str());
     this->printRelocationsHelper(Sec);
-    W.unindent();
-    W.startLine() << "}\n";
   }
 }
 
Index: llvm/test/tools/llvm-readobj/ELF/json-relocations.test
===================================================================
--- /dev/null
+++ llvm/test/tools/llvm-readobj/ELF/json-relocations.test
@@ -0,0 +1,34 @@
+## Check llvm-readobj is able to dump relocations that
+## do not use symbols. 
+
+# RUN: yaml2obj %s -o %t
+# RUN: llvm-readobj -r --expand-relocs --elf-output-style=JSON --pretty-print %t | FileCheck %s
+
+# CHECK:        "Section (1) .rela.text": {
+# CHECK-NEXT:          "Relocation": {
+# CHECK-NEXT:            "Offset": 0,
+# CHECK-NEXT:            "Type": {
+# CHECK-NEXT:              "Value": "R_X86_64_PC32",
+# CHECK-NEXT:              "RawValue": 2
+# CHECK-NEXT:            },
+# CHECK-NEXT:            "Symbol": {
+# CHECK-NEXT:              "Value": "-",
+# CHECK-NEXT:              "RawValue": 0
+# CHECK-NEXT:            },
+# CHECK-NEXT:            "Addend": 18446744073709551612
+# CHECK-NEXT:          }
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_EXEC
+  Machine: EM_X86_64
+Sections:
+  - Name:  .rela.text
+    Type:  SHT_RELA
+    Flags: [ SHF_INFO_LINK ]
+    Relocations:
+      - Offset: 0x0
+        Type:   R_X86_64_PC32
+        Addend: 0xFFFFFFFFFFFFFFFC


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D143310.494791.patch
Type: text/x-patch
Size: 2100 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230204/1c6cf5b4/attachment.bin>


More information about the llvm-commits mailing list