Revert "ELFObjectWriter: Optimize isInSymtab"

This reverts commit 1108cf6419.

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
This commit is contained in:
Fangrui Song
2025-07-02 00:08:41 -07:00
parent eac1a1d3a8
commit 9262ac3ee4

View File

@@ -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()) {
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()) {