[llvm] Scopedprinter printhex json (PR #200071)

Shawn Zhong via llvm-commits llvm-commits at lists.llvm.org
Wed May 27 15:05:00 PDT 2026


https://github.com/ShawnZhong created https://github.com/llvm/llvm-project/pull/200071

Previously, `llvm-readobj` generated invalid JSON with unknown Machine:

```
 yaml2obj llvm/test/tools/llvm-readobj/ELF/file-headers.test --docnum=5 \
   | llvm-readobj --elf-output-style=JSON --pretty-print --file-header - \
   | grep Machine
```

```
      "Type": "SharedObject (0x3)"Machine: 0x9026
```

<details>
<summary>Full output</summary>
<pre><code>
[
  {
    "FileSummary": {
      "File": "<stdin>",
      "Format": "elf64-unknown",
      "Arch": "unknown",
      "AddressSize": "64bit",
      "LoadName": "<Not found>"
    },
    "ElfHeader": {
      "Ident": {
        "Magic": {
          "Offset": 0,
          "Bytes": [
            127,
            69,
            76,
            70
          ]
        },
        "Class": {
          "Name": "64-bit",
          "Value": 2
        },
        "DataEncoding": {
          "Name": "LittleEndian",
          "Value": 1
        },
        "FileVersion": 1,
        "OS/ABI": {
          "Name": "SystemV",
          "Value": 0
        },
        "ABIVersion": 0,
        "Unused": {
          "Offset": 0,
          "Bytes": [
            0,
            0,
            0,
            0,
            0,
            0,
            0
          ]
        }
      },
      "Type": "SharedObject (0x3)"Machine: 0x9026
,
      "Version": 1,
      "Entry": 0,
      "ProgramHeaderOffset": 0,
      "SectionHeaderOffset": 88,
      "Flags": {
        "Value": 0,
        "Flags": []
      },
      "HeaderSize": 64,
      "ProgramHeaderEntrySize": 0,
      "ProgramHeaderCount": 0,
      "SectionHeaderEntrySize": 64,
      "SectionHeaderCount": "3",
      "StringTableSectionIndex": "2"
    }
  }
]
</code></pre>
</details>

>From 9fa18ebed853ba23d454b07c88c327dc60563e64 Mon Sep 17 00:00:00 2001
From: ShawnZhong <github at shawnzhong.com>
Date: Wed, 27 May 2026 16:56:48 -0500
Subject: [PATCH 1/2] [llvm-readobj][test] Add JSON test for unknown e_machine

---
 .../tools/llvm-readobj/ELF/file-headers.test    | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/llvm/test/tools/llvm-readobj/ELF/file-headers.test b/llvm/test/tools/llvm-readobj/ELF/file-headers.test
index 97ab9f092b228..b06989a8bb9c4 100644
--- a/llvm/test/tools/llvm-readobj/ELF/file-headers.test
+++ b/llvm/test/tools/llvm-readobj/ELF/file-headers.test
@@ -233,3 +233,20 @@ Sections:
 # RUN:  | FileCheck %s -DFILE=%t.invalid2 -DSECHDRCOUNT="<?>" -DSECHDRSTRTABINDEX="<?>" --check-prefix=INVALID-LLVM
 # RUN: not llvm-readelf --file-headers %t.invalid2 2>&1 \
 # RUN:  | FileCheck %s -DFILE=%t.invalid2 -DSECHDRCOUNT="<?>" -DSECHDRSTRTABINDEX="<?>" --check-prefix=INVALID-GNU
+
+## --elf-output-style=JSON must emit well-formed JSON when e_machine is not
+## in any known enum table (regression: raw "Machine: 0x..." inside JSON).
+# RUN: yaml2obj %s --docnum=5 -o %t.unknown-machine
+# RUN: llvm-readobj --elf-output-style=JSON --pretty-print --file-header \
+# RUN:   %t.unknown-machine | FileCheck %s --check-prefix=JSON-UNKNOWN-MACHINE
+
+# JSON-UNKNOWN-MACHINE:      "Type": "SharedObject (0x3)",
+# JSON-UNKNOWN-MACHINE-NEXT: "Machine": 36902,
+# JSON-UNKNOWN-MACHINE-NEXT: "Version": 1,
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_DYN
+  Machine: 0x9026

>From 8c1d558bf8a6017bcd9ffdeca629134bfb1204b9 Mon Sep 17 00:00:00 2001
From: ShawnZhong <github at shawnzhong.com>
Date: Wed, 27 May 2026 16:57:01 -0500
Subject: [PATCH 2/2] [Support][ScopedPrinter] Emit valid JSON in
 printHex<ulittle16_t>

---
 llvm/include/llvm/Support/ScopedPrinter.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/include/llvm/Support/ScopedPrinter.h b/llvm/include/llvm/Support/ScopedPrinter.h
index e9e9903b2482a..adce6c185c22e 100644
--- a/llvm/include/llvm/Support/ScopedPrinter.h
+++ b/llvm/include/llvm/Support/ScopedPrinter.h
@@ -549,7 +549,7 @@ template <>
 inline void
 ScopedPrinter::printHex<support::ulittle16_t>(StringRef Label,
                                               support::ulittle16_t Value) {
-  startLine() << Label << ": " << hex(Value) << "\n";
+  printHexImpl(Label, hex(static_cast<uint16_t>(Value)));
 }
 
 struct DelimitedScope {



More information about the llvm-commits mailing list