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