Skip to content

Commit 212fdde

Browse files
committed
Remember if a weakref of a symbol has been used.
This avoids yet another last minute patching of the binding. While at it, also simplify the weakref implementation a bit by not walking past it in the expression evaluation. llvm-svn: 238982
1 parent 9f0c63b commit 212fdde

File tree

4 files changed

+33
-27
lines changed

4 files changed

+33
-27
lines changed

llvm/include/llvm/MC/MCSymbolELF.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ class MCSymbolELF : public MCSymbol {
1919

2020
mutable unsigned BindingSet : 1;
2121
mutable unsigned UsedInReloc : 1;
22+
mutable unsigned WeakrefUsedInReloc : 1;
2223
mutable unsigned IsSignature : 1;
2324

2425
public:
2526
MCSymbolELF(const StringMapEntry<bool> *Name, bool isTemporary)
26-
: MCSymbol(true, Name, isTemporary), BindingSet(false) {}
27+
: MCSymbol(true, Name, isTemporary), BindingSet(false),
28+
UsedInReloc(false), WeakrefUsedInReloc(false), IsSignature(false) {}
2729
void setSize(const MCExpr *SS) { SymbolSize = SS; }
2830

2931
const MCExpr *getSize() const { return SymbolSize; }
@@ -45,6 +47,9 @@ class MCSymbolELF : public MCSymbol {
4547
void setUsedInReloc() const;
4648
bool isUsedInReloc() const;
4749

50+
void setIsWeakrefUsedInReloc() const;
51+
bool isWeakrefUsedInReloc() const;
52+
4853
void setIsSignature() const;
4954
bool isSignature() const;
5055

llvm/lib/MC/ELFObjectWriter.cpp

+14-26
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ class ELFObjectWriter : public MCObjectWriter {
9898
/// The target specific ELF writer instance.
9999
std::unique_ptr<MCELFObjectTargetWriter> TargetObjectWriter;
100100

101-
SmallPtrSet<const MCSymbol *, 16> WeakrefUsedInReloc;
102101
DenseMap<const MCSymbolELF *, const MCSymbolELF *> Renames;
103102

104103
llvm::DenseMap<const MCSectionELF *, std::vector<ELFRelocationEntry>>
@@ -141,7 +140,6 @@ class ELFObjectWriter : public MCObjectWriter {
141140
: MCObjectWriter(OS, IsLittleEndian), TargetObjectWriter(MOTW) {}
142141

143142
void reset() override {
144-
WeakrefUsedInReloc.clear();
145143
Renames.clear();
146144
Relocations.clear();
147145
StrTabBuilder.clear();
@@ -589,25 +587,6 @@ bool ELFObjectWriter::shouldRelocateWithSymbol(const MCAssembler &Asm,
589587
return false;
590588
}
591589

592-
static const MCSymbol *getWeakRef(const MCSymbolRefExpr &Ref) {
593-
const MCSymbol &Sym = Ref.getSymbol();
594-
595-
if (Ref.getKind() == MCSymbolRefExpr::VK_WEAKREF)
596-
return &Sym;
597-
598-
if (!Sym.isVariable())
599-
return nullptr;
600-
601-
const MCExpr *Expr = Sym.getVariableValue();
602-
const auto *Inner = dyn_cast<MCSymbolRefExpr>(Expr);
603-
if (!Inner)
604-
return nullptr;
605-
606-
if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF)
607-
return &Inner->getSymbol();
608-
return nullptr;
609-
}
610-
611590
// True if the assembler knows nothing about the final value of the symbol.
612591
// This doesn't cover the comdat issues, since in those cases the assembler
613592
// can at least know that all symbols in the section will move together.
@@ -681,6 +660,17 @@ void ELFObjectWriter::RecordRelocation(MCAssembler &Asm,
681660
const MCSymbolRefExpr *RefA = Target.getSymA();
682661
const auto *SymA = RefA ? cast<MCSymbolELF>(&RefA->getSymbol()) : nullptr;
683662

663+
bool ViaWeakRef = false;
664+
if (SymA && SymA->isVariable()) {
665+
const MCExpr *Expr = SymA->getVariableValue();
666+
if (const auto *Inner = dyn_cast<MCSymbolRefExpr>(Expr)) {
667+
if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF) {
668+
SymA = cast<MCSymbolELF>(&Inner->getSymbol());
669+
ViaWeakRef = true;
670+
}
671+
}
672+
}
673+
684674
unsigned Type = GetRelocType(Target, Fixup, IsPCRel);
685675
bool RelocateWithSymbol = shouldRelocateWithSymbol(Asm, RefA, SymA, C, Type);
686676
if (!RelocateWithSymbol && SymA && !SymA->isUndefined())
@@ -709,8 +699,8 @@ void ELFObjectWriter::RecordRelocation(MCAssembler &Asm,
709699
if (const MCSymbolELF *R = Renames.lookup(SymA))
710700
SymA = R;
711701

712-
if (const MCSymbol *WeakRef = getWeakRef(*RefA))
713-
WeakrefUsedInReloc.insert(WeakRef);
702+
if (ViaWeakRef)
703+
SymA->setIsWeakrefUsedInReloc();
714704
else
715705
SymA->setUsedInReloc();
716706
}
@@ -785,7 +775,7 @@ void ELFObjectWriter::computeSymbolTable(
785775
for (const MCSymbol &S : Asm.symbols()) {
786776
const auto &Symbol = cast<MCSymbolELF>(S);
787777
bool Used = Symbol.isUsedInReloc();
788-
bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
778+
bool WeakrefUsed = Symbol.isWeakrefUsedInReloc();
789779
bool isSignature = Symbol.isSignature();
790780

791781
if (!isInSymtab(Layout, Symbol, Used || WeakrefUsed || isSignature,
@@ -809,8 +799,6 @@ void ELFObjectWriter::computeSymbolTable(
809799
} else {
810800
MSD.SectionIndex = ELF::SHN_UNDEF;
811801
}
812-
if (!Used && WeakrefUsed)
813-
Symbol.setBinding(ELF::STB_WEAK);
814802
} else {
815803
const MCSectionELF &Section =
816804
static_cast<const MCSectionELF &>(Symbol.getSection());

llvm/lib/MC/MCExpr.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,13 @@ bool MCExpr::evaluateAsValue(MCValue &Res, const MCAsmLayout &Layout) const {
601601
}
602602

603603
static bool canExpand(const MCSymbol &Sym, const MCAssembler *Asm, bool InSet) {
604+
const MCExpr *Expr = Sym.getVariableValue();
605+
const auto *Inner = dyn_cast<MCSymbolRefExpr>(Expr);
606+
if (Inner) {
607+
if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF)
608+
return false;
609+
}
610+
604611
if (InSet)
605612
return true;
606613
if (!Asm)

llvm/lib/MC/MCSymbolELF.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ unsigned MCSymbolELF::getBinding() const {
3535
return ELF::STB_LOCAL;
3636
if (isUsedInReloc())
3737
return ELF::STB_GLOBAL;
38+
if (isWeakrefUsedInReloc())
39+
return ELF::STB_WEAK;
3840
if (isSignature())
3941
return ELF::STB_LOCAL;
4042
return ELF::STB_GLOBAL;
@@ -96,6 +98,10 @@ bool MCSymbolELF::isUsedInReloc() const {
9698
return UsedInReloc;
9799
}
98100

101+
void MCSymbolELF::setIsWeakrefUsedInReloc() const { WeakrefUsedInReloc = true; }
102+
103+
bool MCSymbolELF::isWeakrefUsedInReloc() const { return WeakrefUsedInReloc; }
104+
99105
void MCSymbolELF::setIsSignature() const { IsSignature = true; }
100106

101107
bool MCSymbolELF::isSignature() const { return IsSignature; }

0 commit comments

Comments
 (0)