[llvm] [MC] Add ELF::getSymbolInfo and fix bitwise OR between enumeration types. NFC (PR #226860)
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Sun Sep 27 17:45:04 PDT 2026
https://github.com/MaskRay created https://github.com/llvm/llvm-project/pull/226860
`ELF::STT_FILE | ELF::STB_LOCAL` puts the binding in the type bits,
which only works because STB_LOCAL is 0.
It combines enumerators from two distinct unnamed enums, which is
deprecated in C++20 and ill-formed in C++26 (P2864R2).
>From f4367100ac5ce6018087935f7219b1ed831031c7 Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Sun, 27 Sep 2026 17:18:26 -0700
Subject: [PATCH] [MC] Add ELF::getSymbolInfo and fix bitwise OR between
enumeration types. NFC
`ELF::STT_FILE | ELF::STB_LOCAL` puts the binding in the type bits,
which only works because STB_LOCAL is 0.
It combines enumerators from two distinct unnamed enums, which is
deprecated in C++20 and ill-formed in C++26 (P2864R2).
---
llvm/include/llvm/BinaryFormat/ELF.h | 10 ++++++++--
llvm/include/llvm/Object/ELFTypes.h | 2 +-
llvm/lib/MC/ELFObjectWriter.cpp | 10 +++++-----
3 files changed, 14 insertions(+), 8 deletions(-)
diff --git a/llvm/include/llvm/BinaryFormat/ELF.h b/llvm/include/llvm/BinaryFormat/ELF.h
index 6bb387799c4a1..e530c4d8e08f8 100644
--- a/llvm/include/llvm/BinaryFormat/ELF.h
+++ b/llvm/include/llvm/BinaryFormat/ELF.h
@@ -1364,6 +1364,12 @@ enum : unsigned {
GRP_MASKPROC = 0xf0000000
};
+// Combine a symbol's binding and type into st_info (ELF32_ST_INFO,
+// ELF64_ST_INFO).
+constexpr uint8_t getSymbolInfo(uint8_t Binding, uint8_t Type) {
+ return (Binding << 4) | (Type & 0x0f);
+}
+
// Symbol table entries for ELF32.
struct Elf32_Sym {
Elf32_Word st_name; // Symbol name (index into string table)
@@ -1380,7 +1386,7 @@ struct Elf32_Sym {
void setBinding(unsigned char b) { setBindingAndType(b, getType()); }
void setType(unsigned char t) { setBindingAndType(getBinding(), t); }
void setBindingAndType(unsigned char b, unsigned char t) {
- st_info = (b << 4) + (t & 0x0f);
+ st_info = getSymbolInfo(b, t);
}
};
@@ -1400,7 +1406,7 @@ struct Elf64_Sym {
void setBinding(unsigned char b) { setBindingAndType(b, getType()); }
void setType(unsigned char t) { setBindingAndType(getBinding(), t); }
void setBindingAndType(unsigned char b, unsigned char t) {
- st_info = (b << 4) + (t & 0x0f);
+ st_info = getSymbolInfo(b, t);
}
};
diff --git a/llvm/include/llvm/Object/ELFTypes.h b/llvm/include/llvm/Object/ELFTypes.h
index 6d5a0bdb21130..925e800495d78 100644
--- a/llvm/include/llvm/Object/ELFTypes.h
+++ b/llvm/include/llvm/Object/ELFTypes.h
@@ -244,7 +244,7 @@ struct Elf_Sym_Impl : Elf_Sym_Base<ELFT> {
void setType(unsigned char t) { setBindingAndType(getBinding(), t); }
void setBindingAndType(unsigned char b, unsigned char t) {
- st_info = (b << 4) + (t & 0x0f);
+ st_info = ELF::getSymbolInfo(b, t);
}
/// Access to the STV_xxx flag stored in the first two bits of st_other.
diff --git a/llvm/lib/MC/ELFObjectWriter.cpp b/llvm/lib/MC/ELFObjectWriter.cpp
index 3af4851d39d94..6e52fcbdbb0fe 100644
--- a/llvm/lib/MC/ELFObjectWriter.cpp
+++ b/llvm/lib/MC/ELFObjectWriter.cpp
@@ -423,7 +423,7 @@ void ELFWriter::writeSymbol(SymbolTableWriter &Writer, uint32_t StringIndex,
if (Base) {
Type = mergeTypeForSet(Type, Base->getType());
}
- uint8_t Info = (Binding << 4) | Type;
+ uint8_t Info = ELF::getSymbolInfo(Binding, Type);
// Other and Visibility share the same byte with Visibility using the lower
// 2 bits
@@ -610,8 +610,8 @@ void ELFWriter::computeSymbolTable(const RevGroupMapTy &RevGroupMap) {
for (; FileNameIt != FileNames.end() && FileNameIt->second <= MSD.Order;
++FileNameIt) {
Writer.writeSymbol(StrTabBuilder.getOffset(FileNameIt->first),
- ELF::STT_FILE | ELF::STB_LOCAL, 0, 0, ELF::STV_DEFAULT,
- ELF::SHN_ABS, true);
+ ELF::getSymbolInfo(ELF::STB_LOCAL, ELF::STT_FILE), 0,
+ 0, ELF::STV_DEFAULT, ELF::SHN_ABS, true);
++Index;
}
@@ -623,8 +623,8 @@ void ELFWriter::computeSymbolTable(const RevGroupMapTy &RevGroupMap) {
}
for (; FileNameIt != FileNames.end(); ++FileNameIt) {
Writer.writeSymbol(StrTabBuilder.getOffset(FileNameIt->first),
- ELF::STT_FILE | ELF::STB_LOCAL, 0, 0, ELF::STV_DEFAULT,
- ELF::SHN_ABS, true);
+ ELF::getSymbolInfo(ELF::STB_LOCAL, ELF::STT_FILE), 0, 0,
+ ELF::STV_DEFAULT, ELF::SHN_ABS, true);
++Index;
}
More information about the llvm-commits
mailing list