From 9262ac3ee4d570f1f4a7300c1d723900cb7bc9bd Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Wed, 2 Jul 2025 00:08:41 -0700 Subject: [PATCH] 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",@progbits,sec nop ``` Fix #146581 --- llvm/lib/MC/ELFObjectWriter.cpp | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/llvm/lib/MC/ELFObjectWriter.cpp b/llvm/lib/MC/ELFObjectWriter.cpp index 816ec2507646..417b9d280f53 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(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()) {