[llvm] [MC] Fix elf symbol info (PR #226834)

via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 27 13:42:33 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-mc

@llvm/pr-subscribers-llvm-binary-utilities

Author: rdong8

<details>
<summary>Changes</summary>

https://github.com/llvm/llvm-project/blob/32080ffd98b12a7f4733181790b21ff62f9f31ba/llvm/lib/MC/ELFObjectWriter.cpp#L613

Here `ELF::STT_FILE` and `ELF::STB_LOCAL` are enumerators corresponding to different enums. This is seemingly a latent bug since the same file shows the correct way to compute this:

https://github.com/llvm/llvm-project/blob/32080ffd98b12a7f4733181790b21ff62f9f31ba/llvm/lib/MC/ELFObjectWriter.cpp#L426

These two ways happen to give the same value right now only because `ELF::STB_LOCAL == 0`. I added a helper function to compute this.

This issue was found with the help of AI while working on https://github.com/llvm/llvm-project/pull/223284

---
Full diff: https://github.com/llvm/llvm-project/pull/226834.diff


3 Files Affected:

- (modified) llvm/include/llvm/BinaryFormat/ELF.h (+7-2) 
- (modified) llvm/include/llvm/Object/ELFTypes.h (+1-1) 
- (modified) llvm/lib/MC/ELFObjectWriter.cpp (+5-5) 


``````````diff
diff --git a/llvm/include/llvm/BinaryFormat/ELF.h b/llvm/include/llvm/BinaryFormat/ELF.h
index 6bb387799c4a1..9bae08ba51545 100644
--- a/llvm/include/llvm/BinaryFormat/ELF.h
+++ b/llvm/include/llvm/BinaryFormat/ELF.h
@@ -1364,6 +1364,11 @@ enum : unsigned {
   GRP_MASKPROC = 0xf0000000
 };
 
+// Corresponds to the ELF32_ST_INFO / ELF64_ST_INFO macro
+inline 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 +1385,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 +1405,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;
   }
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/226834


More information about the llvm-commits mailing list