[llvm] [SPARC][Utilities] Add names for SPARC ELF flags in llvm-readobj (PR #102843)

via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 12 04:25:35 PDT 2024


https://github.com/koachan updated https://github.com/llvm/llvm-project/pull/102843

>From b7e2ee82e9db4c2d45cb2721240a0fedf43b501a Mon Sep 17 00:00:00 2001
From: Koakuma <koachan at protonmail.com>
Date: Mon, 12 Aug 2024 09:55:53 +0700
Subject: [PATCH 1/2] [SPARC][Utilities] Add names for SPARC ELF flags in
 llvm-readobj

This allows us to print readable names in llvm-readobj output.
---
 llvm/test/MC/Sparc/elf-sparc-flags.s  | 20 ++++++++++++++++++++
 llvm/tools/llvm-readobj/ELFDumper.cpp | 18 ++++++++++++++++++
 2 files changed, 38 insertions(+)
 create mode 100644 llvm/test/MC/Sparc/elf-sparc-flags.s

diff --git a/llvm/test/MC/Sparc/elf-sparc-flags.s b/llvm/test/MC/Sparc/elf-sparc-flags.s
new file mode 100644
index 00000000000000..f250aadfc61203
--- /dev/null
+++ b/llvm/test/MC/Sparc/elf-sparc-flags.s
@@ -0,0 +1,20 @@
+## Emit correct flags depending on triple, cpu, and memory model options.
+## - `-triple sparc` sets the flag field to 0x0
+## - `-triple sparc -mattr=+v8plus` adds an EF_SPARC_32PLUS (0x100)
+## - Currently, for sparc64 we always compile for TSO memory model, so
+##   `-triple sparcv9` sets the memory model flag to EF_SPARCV9_TSO (0x0)
+##   (i.e the last two bits have to be a zero).
+
+# RUN: llvm-mc -filetype=obj -triple sparc                  %s -o - | llvm-readobj -h - | FileCheck --check-prefixes=SPARC       %s
+# RUN: llvm-mc -filetype=obj -triple sparc   -mattr=+v8plus %s -o - | llvm-readobj -h - | FileCheck --check-prefixes=SPARC32PLUS %s
+# RUN: llvm-mc -filetype=obj -triple sparcv9                %s -o - | llvm-readobj -h - | FileCheck --check-prefixes=SPARCV9     %s
+
+# SPARC:      Flags [ (0x0)
+# SPARC-NEXT: ]
+
+# SPARC32PLUS:      Flags [ (0x100)
+# SPARC32PLUS-NEXT:   EF_SPARC_32PLUS (0x100)
+# SPARC32PLUS-NEXT: ]
+
+# SPARCV9:      Flags [ (0x0)
+# SPARCV9-NEXT: ]
diff --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp
index 1457ae8cb41733..3cc87e8b49fb2a 100644
--- a/llvm/tools/llvm-readobj/ELFDumper.cpp
+++ b/llvm/tools/llvm-readobj/ELFDumper.cpp
@@ -1684,6 +1684,18 @@ const EnumEntry<unsigned> ElfHeaderRISCVFlags[] = {
   ENUM_ENT(EF_RISCV_TSO, "TSO"),
 };
 
+const EnumEntry<unsigned> ElfHeaderSPARCFlags[] = {
+    ENUM_ENT(EF_SPARC_EXT_MASK, "SPARC extension mask"),
+    ENUM_ENT(EF_SPARC_32PLUS, "V8+ ABI"),
+    ENUM_ENT(EF_SPARC_SUN_US1, "Sun UltraSPARC I extensions"),
+    ENUM_ENT(EF_SPARC_HAL_R1, "HAL/Fujitsu R1 extensions"),
+    ENUM_ENT(EF_SPARC_SUN_US3, "Sun UltraSPARC III extensions"),
+    ENUM_ENT(EF_SPARCV9_MM, "SPARC memory model mask"),
+    ENUM_ENT(EF_SPARCV9_TSO, "Total Store Ordering"),
+    ENUM_ENT(EF_SPARCV9_PSO, "Partial Store Ordering"),
+    ENUM_ENT(EF_SPARCV9_RMO, "Relaxed Memory Ordering"),
+};
+
 const EnumEntry<unsigned> ElfHeaderAVRFlags[] = {
   LLVM_READOBJ_ENUM_ENT(ELF, EF_AVR_ARCH_AVR1),
   LLVM_READOBJ_ENUM_ENT(ELF, EF_AVR_ARCH_AVR2),
@@ -3625,6 +3637,9 @@ template <class ELFT> void GNUELFDumper<ELFT>::printFileHeaders() {
         unsigned(ELF::EF_MIPS_ABI), unsigned(ELF::EF_MIPS_MACH));
   else if (e.e_machine == EM_RISCV)
     ElfFlags = printFlags(e.e_flags, ArrayRef(ElfHeaderRISCVFlags));
+  else if (e.e_machine == EM_SPARC || e.e_machine == EM_SPARC32PLUS ||
+           e.e_machine == EM_SPARCV9)
+    ElfFlags = printFlags(e.e_flags, ArrayRef(ElfHeaderSPARCFlags));
   else if (e.e_machine == EM_AVR)
     ElfFlags = printFlags(e.e_flags, ArrayRef(ElfHeaderAVRFlags),
                           unsigned(ELF::EF_AVR_ARCH_MASK));
@@ -7117,6 +7132,9 @@ template <class ELFT> void LLVMELFDumper<ELFT>::printFileHeaders() {
       }
     } else if (E.e_machine == EM_RISCV)
       W.printFlags("Flags", E.e_flags, ArrayRef(ElfHeaderRISCVFlags));
+    else if (E.e_machine == EM_SPARC || E.e_machine == EM_SPARC32PLUS ||
+             E.e_machine == EM_SPARCV9)
+      W.printFlags("Flags", E.e_flags, ArrayRef(ElfHeaderSPARCFlags));
     else if (E.e_machine == EM_AVR)
       W.printFlags("Flags", E.e_flags, ArrayRef(ElfHeaderAVRFlags),
                    unsigned(ELF::EF_AVR_ARCH_MASK));

>From 6ec2ef4a64b09f2a89e7b3a994aabd79afdd7972 Mon Sep 17 00:00:00 2001
From: Koakuma <koachan at protonmail.com>
Date: Mon, 12 Aug 2024 18:24:14 +0700
Subject: [PATCH 2/2] Properly use the flag masks when printing

---
 llvm/tools/llvm-readobj/ELFDumper.cpp | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp
index 3cc87e8b49fb2a..d27d695dad0987 100644
--- a/llvm/tools/llvm-readobj/ELFDumper.cpp
+++ b/llvm/tools/llvm-readobj/ELFDumper.cpp
@@ -1685,12 +1685,10 @@ const EnumEntry<unsigned> ElfHeaderRISCVFlags[] = {
 };
 
 const EnumEntry<unsigned> ElfHeaderSPARCFlags[] = {
-    ENUM_ENT(EF_SPARC_EXT_MASK, "SPARC extension mask"),
     ENUM_ENT(EF_SPARC_32PLUS, "V8+ ABI"),
     ENUM_ENT(EF_SPARC_SUN_US1, "Sun UltraSPARC I extensions"),
     ENUM_ENT(EF_SPARC_HAL_R1, "HAL/Fujitsu R1 extensions"),
     ENUM_ENT(EF_SPARC_SUN_US3, "Sun UltraSPARC III extensions"),
-    ENUM_ENT(EF_SPARCV9_MM, "SPARC memory model mask"),
     ENUM_ENT(EF_SPARCV9_TSO, "Total Store Ordering"),
     ENUM_ENT(EF_SPARCV9_PSO, "Partial Store Ordering"),
     ENUM_ENT(EF_SPARCV9_RMO, "Relaxed Memory Ordering"),
@@ -3639,7 +3637,9 @@ template <class ELFT> void GNUELFDumper<ELFT>::printFileHeaders() {
     ElfFlags = printFlags(e.e_flags, ArrayRef(ElfHeaderRISCVFlags));
   else if (e.e_machine == EM_SPARC || e.e_machine == EM_SPARC32PLUS ||
            e.e_machine == EM_SPARCV9)
-    ElfFlags = printFlags(e.e_flags, ArrayRef(ElfHeaderSPARCFlags));
+    ElfFlags = printFlags(e.e_flags, ArrayRef(ElfHeaderSPARCFlags),
+                          unsigned(ELF::EF_SPARC_EXT_MASK),
+                          unsigned(ELF::EF_SPARCV9_MM));
   else if (e.e_machine == EM_AVR)
     ElfFlags = printFlags(e.e_flags, ArrayRef(ElfHeaderAVRFlags),
                           unsigned(ELF::EF_AVR_ARCH_MASK));
@@ -7134,7 +7134,9 @@ template <class ELFT> void LLVMELFDumper<ELFT>::printFileHeaders() {
       W.printFlags("Flags", E.e_flags, ArrayRef(ElfHeaderRISCVFlags));
     else if (E.e_machine == EM_SPARC || E.e_machine == EM_SPARC32PLUS ||
              E.e_machine == EM_SPARCV9)
-      W.printFlags("Flags", E.e_flags, ArrayRef(ElfHeaderSPARCFlags));
+      W.printFlags("Flags", E.e_flags, ArrayRef(ElfHeaderSPARCFlags),
+                   unsigned(ELF::EF_SPARC_EXT_MASK),
+                   unsigned(ELF::EF_SPARCV9_MM));
     else if (E.e_machine == EM_AVR)
       W.printFlags("Flags", E.e_flags, ArrayRef(ElfHeaderAVRFlags),
                    unsigned(ELF::EF_AVR_ARCH_MASK));



More information about the llvm-commits mailing list