[llvm] 9262ac3 - Revert "ELFObjectWriter: Optimize isInSymtab"

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 2 00:08:47 PDT 2025


Author: Fangrui Song
Date: 2025-07-02T00:08:42-07:00
New Revision: 9262ac3ee4d570f1f4a7300c1d723900cb7bc9bd

URL: https://github.com/llvm/llvm-project/commit/9262ac3ee4d570f1f4a7300c1d723900cb7bc9bd
DIFF: https://github.com/llvm/llvm-project/commit/9262ac3ee4d570f1f4a7300c1d723900cb7bc9bd.diff

LOG: Revert "ELFObjectWriter: Optimize isInSymtab"

This reverts commit 1108cf64196a056aa350baba98e3fab6d7529a59.

Caused a regression for a weird but interesting case (STT_SECTION symbol
as group signature). We no longer define `sec`
```
.section sec,"ax"
.section .foo,"axG", at progbits,sec
nop
```

Fix #146581

Added: 
    

Modified: 
    llvm/lib/MC/ELFObjectWriter.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/MC/ELFObjectWriter.cpp b/llvm/lib/MC/ELFObjectWriter.cpp
index 816ec2507646d..417b9d280f534 100644
--- a/llvm/lib/MC/ELFObjectWriter.cpp
+++ b/llvm/lib/MC/ELFObjectWriter.cpp
@@ -120,7 +120,7 @@ struct ELFWriter {
   } Mode;
 
   uint64_t symbolValue(const MCSymbol &Sym);
-  bool isInSymtab(const MCSymbolELF &Symbol);
+  bool isInSymtab(const MCSymbolELF &Symbol, bool Used, bool Renamed);
 
   /// Helper struct for containing some precomputed information on symbols.
   struct ELFSymbolData {
@@ -468,7 +468,7 @@ void ELFWriter::writeSymbol(SymbolTableWriter &Writer, uint32_t StringIndex,
                      IsReserved);
 }
 
-bool ELFWriter::isInSymtab(const MCSymbolELF &Symbol) {
+bool ELFWriter::isInSymtab(const MCSymbolELF &Symbol, bool Used, bool Renamed) {
   if (Symbol.isVariable()) {
     const MCExpr *Expr = Symbol.getVariableValue();
     // Target Expressions that are always inlined do not appear in the symtab
@@ -478,18 +478,27 @@ bool ELFWriter::isInSymtab(const MCSymbolELF &Symbol) {
     // The .weakref alias does not appear in the symtab.
     if (Symbol.isWeakref())
       return false;
+  }
 
-    if (Symbol.isUndefined()) {
-      // FIXME: this is here just to diagnose the case of a var = commmon_sym.
-      Asm.getBaseSymbol(Symbol);
-      return false;
-    }
+  if (Used)
+    return true;
+
+  if (Renamed)
+    return false;
+
+  if (Symbol.isVariable() && Symbol.isUndefined()) {
+    // FIXME: this is here just to diagnose the case of a var = commmon_sym.
+    Asm.getBaseSymbol(Symbol);
+    return false;
   }
 
   if (Symbol.isTemporary())
     return false;
 
-  return Symbol.getType() != ELF::STT_SECTION;
+  if (Symbol.getType() == ELF::STT_SECTION)
+    return false;
+
+  return true;
 }
 
 void ELFWriter::computeSymbolTable(const RevGroupMapTy &RevGroupMap) {
@@ -521,7 +530,8 @@ void ELFWriter::computeSymbolTable(const RevGroupMapTy &RevGroupMap) {
     const auto &Symbol = cast<MCSymbolELF>(It.value());
     bool Used = Symbol.isUsedInReloc();
     bool isSignature = Symbol.isSignature();
-    if (!(Used || (!OWriter.Renames.count(&Symbol) && isInSymtab(Symbol))))
+    if (!isInSymtab(Symbol, Used || isSignature,
+                    OWriter.Renames.count(&Symbol)))
       continue;
 
     if (Symbol.isTemporary() && Symbol.isUndefined()) {


        


More information about the llvm-commits mailing list