1 //===-- RecordStreamer.cpp - Record asm defined and used symbols ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "RecordStreamer.h"
11 #include "llvm/IR/Mangler.h"
12 #include "llvm/IR/Module.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCSymbol.h"
18 void RecordStreamer::markDefined(const MCSymbol &Symbol) {
19 State &S = Symbols[Symbol.getName()];
37 void RecordStreamer::markGlobal(const MCSymbol &Symbol,
38 MCSymbolAttr Attribute) {
39 State &S = Symbols[Symbol.getName()];
43 S = (Attribute == MCSA_Weak) ? DefinedWeak : DefinedGlobal;
49 S = (Attribute == MCSA_Weak) ? UndefinedWeak : Global;
57 void RecordStreamer::markUsed(const MCSymbol &Symbol) {
58 State &S = Symbols[Symbol.getName()];
74 void RecordStreamer::visitUsedSymbol(const MCSymbol &Sym) { markUsed(Sym); }
76 RecordStreamer::RecordStreamer(MCContext &Context, const Module &M)
77 : MCStreamer(Context), M(M) {}
79 RecordStreamer::const_iterator RecordStreamer::begin() {
80 return Symbols.begin();
83 RecordStreamer::const_iterator RecordStreamer::end() { return Symbols.end(); }
85 void RecordStreamer::EmitInstruction(const MCInst &Inst,
86 const MCSubtargetInfo &STI, bool) {
87 MCStreamer::EmitInstruction(Inst, STI);
90 void RecordStreamer::EmitLabel(MCSymbol *Symbol, SMLoc Loc) {
91 MCStreamer::EmitLabel(Symbol);
95 void RecordStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
97 MCStreamer::EmitAssignment(Symbol, Value);
100 bool RecordStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
101 MCSymbolAttr Attribute) {
102 if (Attribute == MCSA_Global || Attribute == MCSA_Weak)
103 markGlobal(*Symbol, Attribute);
104 if (Attribute == MCSA_LazyReference)
109 void RecordStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
110 uint64_t Size, unsigned ByteAlignment,
112 markDefined(*Symbol);
115 void RecordStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
116 unsigned ByteAlignment) {
117 markDefined(*Symbol);
120 RecordStreamer::State RecordStreamer::getSymbolState(const MCSymbol *Sym) {
121 auto SI = Symbols.find(Sym->getName());
122 if (SI == Symbols.end())
127 void RecordStreamer::emitELFSymverDirective(StringRef AliasName,
128 const MCSymbol *Aliasee) {
129 SymverAliasMap[Aliasee].push_back(AliasName);
132 iterator_range<RecordStreamer::const_symver_iterator>
133 RecordStreamer::symverAliases() {
134 return {SymverAliasMap.begin(), SymverAliasMap.end()};
137 void RecordStreamer::flushSymverDirectives() {
138 // Mapping from mangled name to GV.
139 StringMap<const GlobalValue *> MangledNameMap;
140 // The name in the assembler will be mangled, but the name in the IR
141 // might not, so we first compute a mapping from mangled name to GV.
143 SmallString<64> MangledName;
144 for (const GlobalValue &GV : M.global_values()) {
148 MangledName.reserve(GV.getName().size() + 1);
149 Mang.getNameWithPrefix(MangledName, &GV, /*CannotUsePrivateLabel=*/false);
150 MangledNameMap[MangledName] = &GV;
153 // Walk all the recorded .symver aliases, and set up the binding
155 for (auto &Symver : SymverAliasMap) {
156 const MCSymbol *Aliasee = Symver.first;
157 MCSymbolAttr Attr = MCSA_Invalid;
158 bool IsDefined = false;
160 // First check if the aliasee binding was recorded in the asm.
161 RecordStreamer::State state = getSymbolState(Aliasee);
163 case RecordStreamer::Global:
164 case RecordStreamer::DefinedGlobal:
167 case RecordStreamer::UndefinedWeak:
168 case RecordStreamer::DefinedWeak:
176 case RecordStreamer::Defined:
177 case RecordStreamer::DefinedGlobal:
178 case RecordStreamer::DefinedWeak:
181 case RecordStreamer::NeverSeen:
182 case RecordStreamer::Global:
183 case RecordStreamer::Used:
184 case RecordStreamer::UndefinedWeak:
188 if (Attr == MCSA_Invalid || !IsDefined) {
189 const GlobalValue *GV = M.getNamedValue(Aliasee->getName());
191 auto MI = MangledNameMap.find(Aliasee->getName());
192 if (MI != MangledNameMap.end())
196 // If we don't have a symbol attribute from assembly, then check if
197 // the aliasee was defined in the IR.
198 if (Attr == MCSA_Invalid) {
199 if (GV->hasExternalLinkage())
201 else if (GV->hasLocalLinkage())
203 else if (GV->isWeakForLinker())
206 IsDefined = IsDefined || !GV->isDeclarationForLinker();
210 // Set the detected binding on each alias with this aliasee.
211 for (auto AliasName : Symver.second) {
212 std::pair<StringRef, StringRef> Split = AliasName.split("@@@");
213 SmallString<128> NewName;
214 if (!Split.second.empty() && !Split.second.startswith("@")) {
215 // Special processing for "@@@" according
216 // https://sourceware.org/binutils/docs/as/Symver.html
217 const char *Separator = IsDefined ? "@@" : "@";
219 (Split.first + Separator + Split.second).toStringRef(NewName);
221 MCSymbol *Alias = getContext().getOrCreateSymbol(AliasName);
222 // TODO: Handle "@@@". Depending on SymbolAttribute value it needs to be
223 // converted into @ or @@.
224 const MCExpr *Value = MCSymbolRefExpr::create(Aliasee, getContext());
227 // Don't use EmitAssignment override as it always marks alias as defined.
228 MCStreamer::EmitAssignment(Alias, Value);
229 if (Attr != MCSA_Invalid)
230 EmitSymbolAttribute(Alias, Attr);