[llvm] [Support][Object/ELF][NFC] Use new enum table (PR #206068)

Alexis Engelke via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 26 07:35:01 PDT 2026


https://github.com/aengelke updated https://github.com/llvm/llvm-project/pull/206068

>From 2bd96d0370da361d5b0403385ee94b090727d1a7 Mon Sep 17 00:00:00 2001
From: Alexis Engelke <engelke at in.tum.de>
Date: Fri, 26 Jun 2026 14:01:38 +0000
Subject: [PATCH 1/2] [spr] initial version

Created using spr 1.3.8-wip
---
 llvm/include/llvm/Object/ELFObjectFile.h  | 14 ++------
 llvm/lib/Object/ELFObjectFile.cpp         | 40 +++++++++++++----------
 llvm/lib/Support/ELFAttrParserCompact.cpp | 16 +++++----
 llvm/tools/llvm-readobj/ELFDumper.cpp     | 14 ++++----
 4 files changed, 41 insertions(+), 43 deletions(-)

diff --git a/llvm/include/llvm/Object/ELFObjectFile.h b/llvm/include/llvm/Object/ELFObjectFile.h
index 7b6348095679f..a511271e31786 100644
--- a/llvm/include/llvm/Object/ELFObjectFile.h
+++ b/llvm/include/llvm/Object/ELFObjectFile.h
@@ -14,6 +14,7 @@
 #define LLVM_OBJECT_ELFOBJECTFILE_H
 
 #include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/Enum.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/ADT/iterator_range.h"
@@ -33,7 +34,6 @@
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/LEB128.h"
 #include "llvm/Support/MemoryBufferRef.h"
-#include "llvm/Support/ScopedPrinter.h"
 #include "llvm/TargetParser/SubtargetFeature.h"
 #include "llvm/TargetParser/Triple.h"
 #include <cassert>
@@ -45,9 +45,7 @@ template <typename T> class SmallVectorImpl;
 
 namespace object {
 
-constexpr int NumElfSymbolTypes = 16;
-LLVM_ABI extern const llvm::EnumEntry<unsigned>
-    ElfSymbolTypes[NumElfSymbolTypes];
+EnumStrings<uint8_t, 2> getElfSymbolTypes();
 
 class elf_symbol_iterator;
 
@@ -197,13 +195,7 @@ class ELFSymbolRef : public SymbolRef {
   }
 
   StringRef getELFTypeName() const {
-    uint8_t Type = getELFType();
-    for (const auto &EE : ElfSymbolTypes) {
-      if (EE.Value == Type) {
-        return EE.AltName;
-      }
-    }
-    return "";
+    return getElfSymbolTypes().toString(getELFType(), 1);
   }
 };
 
diff --git a/llvm/lib/Object/ELFObjectFile.cpp b/llvm/lib/Object/ELFObjectFile.cpp
index bb400e13b25be..2409a1b6fccef 100644
--- a/llvm/lib/Object/ELFObjectFile.cpp
+++ b/llvm/lib/Object/ELFObjectFile.cpp
@@ -37,24 +37,28 @@
 using namespace llvm;
 using namespace object;
 
-const EnumEntry<unsigned> llvm::object::ElfSymbolTypes[NumElfSymbolTypes] = {
-    {"None", "NOTYPE", ELF::STT_NOTYPE},
-    {"Object", "OBJECT", ELF::STT_OBJECT},
-    {"Function", "FUNC", ELF::STT_FUNC},
-    {"Section", "SECTION", ELF::STT_SECTION},
-    {"File", "FILE", ELF::STT_FILE},
-    {"Common", "COMMON", ELF::STT_COMMON},
-    {"TLS", "TLS", ELF::STT_TLS},
-    {"Unknown", "<unknown>: 7", 7},
-    {"Unknown", "<unknown>: 8", 8},
-    {"Unknown", "<unknown>: 9", 9},
-    {"GNU_IFunc", "IFUNC", ELF::STT_GNU_IFUNC},
-    {"OS Specific", "<OS specific>: 11", 11},
-    {"OS Specific", "<OS specific>: 12", 12},
-    {"Proc Specific", "<processor specific>: 13", 13},
-    {"Proc Specific", "<processor specific>: 14", 14},
-    {"Proc Specific", "<processor specific>: 15", 15}
-};
+EnumStrings<uint8_t, 2> llvm::object::getElfSymbolTypes() {
+  constexpr EnumStringDef<uint8_t, 2> ElfSymbolTypeDefs[] = {
+      {{"None", "NOTYPE"}, ELF::STT_NOTYPE},
+      {{"Object", "OBJECT"}, ELF::STT_OBJECT},
+      {{"Function", "FUNC"}, ELF::STT_FUNC},
+      {{"Section", "SECTION"}, ELF::STT_SECTION},
+      {{"File", "FILE"}, ELF::STT_FILE},
+      {{"Common", "COMMON"}, ELF::STT_COMMON},
+      {{"TLS", "TLS"}, ELF::STT_TLS},
+      {{"Unknown", "<unknown>: 7"}, 7},
+      {{"Unknown", "<unknown>: 8"}, 8},
+      {{"Unknown", "<unknown>: 9"}, 9},
+      {{"GNU_IFunc", "IFUNC"}, ELF::STT_GNU_IFUNC},
+      {{"OS Specific", "<OS specific>: 11"}, 11},
+      {{"OS Specific", "<OS specific>: 12"}, 12},
+      {{"Proc Specific", "<processor specific>: 13"}, 13},
+      {{"Proc Specific", "<processor specific>: 14"}, 14},
+      {{"Proc Specific", "<processor specific>: 15"}, 15},
+  };
+  static constexpr auto ElfSymbolTypes = BUILD_ENUM_STRINGS(ElfSymbolTypeDefs);
+  return ElfSymbolTypes;
+}
 
 ELFObjectFileBase::ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source)
     : ObjectFile(Type, Source) {}
diff --git a/llvm/lib/Support/ELFAttrParserCompact.cpp b/llvm/lib/Support/ELFAttrParserCompact.cpp
index 9665a82374113..52b0781fb1671 100644
--- a/llvm/lib/Support/ELFAttrParserCompact.cpp
+++ b/llvm/lib/Support/ELFAttrParserCompact.cpp
@@ -10,6 +10,7 @@
 //===--------------------------------------------------------------------===//
 
 #include "llvm/Support/ELFAttrParserCompact.h"
+#include "llvm/ADT/Enum.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/Support/Errc.h"
 #include "llvm/Support/ScopedPrinter.h"
@@ -17,12 +18,6 @@
 using namespace llvm;
 using namespace llvm::ELFAttrs;
 
-static constexpr EnumEntry<unsigned> tagNames[] = {
-    {"Tag_File", ELFAttrs::File},
-    {"Tag_Section", ELFAttrs::Section},
-    {"Tag_Symbol", ELFAttrs::Symbol},
-};
-
 Error ELFCompactAttrParser::parseStringAttribute(
     const char *name, unsigned tag, ArrayRef<const char *> strings) {
   uint64_t value = de.getULEB128(cursor);
@@ -147,7 +142,14 @@ Error ELFCompactAttrParser::parseSubsection(uint32_t length) {
       return cursor.takeError();
 
     if (sw) {
-      sw->printEnum("Tag", tag, ArrayRef(tagNames));
+      constexpr EnumStringDef<unsigned> TagNameDefs[] = {
+          {{"Tag_File"}, ELFAttrs::File},
+          {{"Tag_Section"}, ELFAttrs::Section},
+          {{"Tag_Symbol"}, ELFAttrs::Symbol},
+      };
+      static constexpr auto TagNames = BUILD_ENUM_STRINGS(TagNameDefs);
+
+      sw->printEnum("Tag", tag, EnumStrings(TagNames));
       sw->printNumber("Size", size);
     }
     if (size < 5)
diff --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp
index 56c94694acce1..23b76d507ef43 100644
--- a/llvm/tools/llvm-readobj/ELFDumper.cpp
+++ b/llvm/tools/llvm-readobj/ELFDumper.cpp
@@ -4365,7 +4365,7 @@ void GNUELFDumper<ELFT>::printSymbol(const Elf_Sym &Symbol, unsigned SymIndex,
       SymbolType >= ELF::STT_LOOS && SymbolType < ELF::STT_HIOS)
     Fields[3].Str = enumToString(SymbolType, ArrayRef(AMDGPUSymbolTypes));
   else
-    Fields[3].Str = enumToString(SymbolType, ArrayRef(ElfSymbolTypes));
+    Fields[3].Str = getElfSymbolTypes().toStringOrHex(SymbolType, 1);
 
   Fields[4].Str =
       enumToString(Symbol.getBinding(), ArrayRef(ElfSymbolBindings));
@@ -4430,7 +4430,7 @@ void GNUELFDumper<ELFT>::printHashedSymbol(const Elf_Sym *Symbol,
       SymbolType >= ELF::STT_LOOS && SymbolType < ELF::STT_HIOS)
     Fields[4].Str = enumToString(SymbolType, ArrayRef(AMDGPUSymbolTypes));
   else
-    Fields[4].Str = enumToString(SymbolType, ArrayRef(ElfSymbolTypes));
+    Fields[4].Str = getElfSymbolTypes().toStringOrHex(SymbolType, 1);
 
   Fields[5].Str =
       enumToString(Symbol->getBinding(), ArrayRef(ElfSymbolBindings));
@@ -7373,7 +7373,7 @@ void GNUELFDumper<ELFT>::printMipsGOT(const MipsGOTParser<ELFT> &Parser) {
       OS.PadToColumn(31 + 2 * Bias);
       OS << to_string(format_hex_no_prefix(Sym.st_value, 8 + Bias));
       OS.PadToColumn(40 + 3 * Bias);
-      OS << enumToString(Sym.getType(), ArrayRef(ElfSymbolTypes));
+      OS << getElfSymbolTypes().toStringOrHex(Sym.getType(), 1);
       OS.PadToColumn(48 + 3 * Bias);
       OS << getSymbolSectionNdx(Sym, &Sym - this->dynamic_symbols().begin(),
                                 ShndxTable);
@@ -7427,7 +7427,7 @@ void GNUELFDumper<ELFT>::printMipsPLT(const MipsGOTParser<ELFT> &Parser) {
       OS.PadToColumn(20 + 2 * Bias);
       OS << to_string(format_hex_no_prefix(Sym.st_value, 8 + Bias));
       OS.PadToColumn(29 + 3 * Bias);
-      OS << enumToString(Sym.getType(), ArrayRef(ElfSymbolTypes));
+      OS << getElfSymbolTypes().toStringOrHex(Sym.getType(), 1);
       OS.PadToColumn(37 + 3 * Bias);
       OS << getSymbolSectionNdx(Sym, &Sym - this->dynamic_symbols().begin(),
                                 ShndxTable);
@@ -7873,7 +7873,7 @@ void LLVMELFDumper<ELFT>::printSymbol(const Elf_Sym &Symbol, unsigned SymIndex,
       SymbolType >= ELF::STT_LOOS && SymbolType < ELF::STT_HIOS)
     W.printEnum("Type", SymbolType, ArrayRef(AMDGPUSymbolTypes));
   else
-    W.printEnum("Type", SymbolType, ArrayRef(ElfSymbolTypes));
+    W.printEnum("Type", SymbolType, getElfSymbolTypes());
   if (Symbol.st_other == 0)
     printZeroSymbolOtherField(Symbol);
   else
@@ -8798,7 +8798,7 @@ void LLVMELFDumper<ELFT>::printMipsGOT(const MipsGOTParser<ELFT> &Parser) {
 
       const Elf_Sym &Sym = *Parser.getGotSym(&E);
       W.printHex("Value", Sym.st_value);
-      W.printEnum("Type", Sym.getType(), ArrayRef(ElfSymbolTypes));
+      W.printEnum("Type", Sym.getType(), getElfSymbolTypes());
 
       const unsigned SymIndex = &Sym - this->dynamic_symbols().begin();
       DataRegion<Elf_Word> ShndxTable(
@@ -8848,7 +8848,7 @@ void LLVMELFDumper<ELFT>::printMipsPLT(const MipsGOTParser<ELFT> &Parser) {
 
       const Elf_Sym &Sym = *Parser.getPltSym(&E);
       W.printHex("Value", Sym.st_value);
-      W.printEnum("Type", Sym.getType(), ArrayRef(ElfSymbolTypes));
+      W.printEnum("Type", Sym.getType(), getElfSymbolTypes());
       printSymbolSection(Sym, &Sym - this->dynamic_symbols().begin(),
                          ShndxTable);
 

>From f7035c8b0dc0f8506f36b0d6888ca646a41d1580 Mon Sep 17 00:00:00 2001
From: Alexis Engelke <engelke at in.tum.de>
Date: Fri, 26 Jun 2026 14:34:19 +0000
Subject: [PATCH 2/2] add missing includes

Created using spr 1.3.8-wip
---
 bolt/lib/Core/BinaryContext.cpp               | 1 +
 bolt/lib/Passes/BinaryPasses.cpp              | 1 +
 bolt/lib/Passes/CacheMetrics.cpp              | 1 +
 bolt/lib/Passes/ProfileQualityStats.cpp       | 1 +
 bolt/lib/Passes/RetpolineInsertion.cpp        | 1 +
 llvm/tools/llvm-dwarfutil/DebugInfoLinker.cpp | 1 +
 llvm/tools/llvm-dwarfutil/llvm-dwarfutil.cpp  | 1 +
 7 files changed, 7 insertions(+)

diff --git a/bolt/lib/Core/BinaryContext.cpp b/bolt/lib/Core/BinaryContext.cpp
index 43ce56ef083b5..eb9caca3ea16e 100644
--- a/bolt/lib/Core/BinaryContext.cpp
+++ b/bolt/lib/Core/BinaryContext.cpp
@@ -35,6 +35,7 @@
 #include "llvm/Support/Error.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/Regex.h"
+#include "llvm/Support/ScopedPrinter.h"
 #include <algorithm>
 #include <functional>
 #include <iterator>
diff --git a/bolt/lib/Passes/BinaryPasses.cpp b/bolt/lib/Passes/BinaryPasses.cpp
index 89737b906e82b..adf2bbae52d11 100644
--- a/bolt/lib/Passes/BinaryPasses.cpp
+++ b/bolt/lib/Passes/BinaryPasses.cpp
@@ -18,6 +18,7 @@
 #include "bolt/Utils/CommandLineOpts.h"
 #include "llvm/Support/CommandLine.h"
 #include <atomic>
+#include <cmath>
 #include <mutex>
 #include <numeric>
 #include <vector>
diff --git a/bolt/lib/Passes/CacheMetrics.cpp b/bolt/lib/Passes/CacheMetrics.cpp
index 8c6f3ff7c4308..7514096d41d64 100644
--- a/bolt/lib/Passes/CacheMetrics.cpp
+++ b/bolt/lib/Passes/CacheMetrics.cpp
@@ -14,6 +14,7 @@
 #include "bolt/Passes/CacheMetrics.h"
 #include "bolt/Core/BinaryBasicBlock.h"
 #include "bolt/Core/BinaryFunction.h"
+#include <cmath>
 
 using namespace llvm;
 using namespace bolt;
diff --git a/bolt/lib/Passes/ProfileQualityStats.cpp b/bolt/lib/Passes/ProfileQualityStats.cpp
index b2303bdfc8d1d..f88133714e206 100644
--- a/bolt/lib/Passes/ProfileQualityStats.cpp
+++ b/bolt/lib/Passes/ProfileQualityStats.cpp
@@ -15,6 +15,7 @@
 #include "bolt/Core/BinaryFunction.h"
 #include "bolt/Utils/CommandLineOpts.h"
 #include "llvm/Support/CommandLine.h"
+#include <cmath>
 #include <queue>
 #include <unordered_map>
 #include <unordered_set>
diff --git a/bolt/lib/Passes/RetpolineInsertion.cpp b/bolt/lib/Passes/RetpolineInsertion.cpp
index 68d3e4cf570e3..4000e59e80ba2 100644
--- a/bolt/lib/Passes/RetpolineInsertion.cpp
+++ b/bolt/lib/Passes/RetpolineInsertion.cpp
@@ -23,6 +23,7 @@
 
 #include "bolt/Passes/RetpolineInsertion.h"
 #include "llvm/MC/MCInstPrinter.h"
+#include "llvm/Support/ScopedPrinter.h"
 #include "llvm/Support/raw_ostream.h"
 
 #define DEBUG_TYPE "bolt-retpoline"
diff --git a/llvm/tools/llvm-dwarfutil/DebugInfoLinker.cpp b/llvm/tools/llvm-dwarfutil/DebugInfoLinker.cpp
index 70f4b05ba2943..06a751e225b5f 100644
--- a/llvm/tools/llvm-dwarfutil/DebugInfoLinker.cpp
+++ b/llvm/tools/llvm-dwarfutil/DebugInfoLinker.cpp
@@ -15,6 +15,7 @@
 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
 #include "llvm/DebugInfo/DWARF/LowLevel/DWARFExpression.h"
 #include "llvm/Object/ObjectFile.h"
+#include "llvm/Support/FormatVariadic.h"
 #include "llvm/Support/ThreadPool.h"
 #include "llvm/Support/Threading.h"
 #include <memory>
diff --git a/llvm/tools/llvm-dwarfutil/llvm-dwarfutil.cpp b/llvm/tools/llvm-dwarfutil/llvm-dwarfutil.cpp
index 0180abb834f9d..3fa64dba7979e 100644
--- a/llvm/tools/llvm-dwarfutil/llvm-dwarfutil.cpp
+++ b/llvm/tools/llvm-dwarfutil/llvm-dwarfutil.cpp
@@ -21,6 +21,7 @@
 #include "llvm/Support/CRC.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/FileUtilities.h"
+#include "llvm/Support/FormatVariadic.h"
 #include "llvm/Support/InitLLVM.h"
 #include "llvm/Support/PrettyStackTrace.h"
 #include "llvm/Support/Process.h"



More information about the llvm-commits mailing list