blob: f49c823c068df75076dd69a752d34f5084667178 [file] [log] [blame]
Eric Christopher276989f2017-03-31 01:46:30 +00001//===-- RecordStreamer.cpp - Record asm defined and used symbols ----------===//
Rafael Espindola13b69d62014-07-03 18:59:23 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Rafael Espindolaba79dba2014-07-04 22:44:18 +000010#include "RecordStreamer.h"
Vitaly Buka0d038812018-03-20 00:38:33 +000011#include "llvm/IR/Mangler.h"
12#include "llvm/IR/Module.h"
Rafael Espindola47b4d6b2018-03-09 18:42:25 +000013#include "llvm/MC/MCContext.h"
Rafael Espindola13b69d62014-07-03 18:59:23 +000014#include "llvm/MC/MCSymbol.h"
Eugene Zelenko1df42fa2017-04-24 23:21:38 +000015
Rafael Espindola13b69d62014-07-03 18:59:23 +000016using namespace llvm;
17
18void RecordStreamer::markDefined(const MCSymbol &Symbol) {
19 State &S = Symbols[Symbol.getName()];
20 switch (S) {
21 case DefinedGlobal:
22 case Global:
23 S = DefinedGlobal;
24 break;
25 case NeverSeen:
26 case Defined:
27 case Used:
28 S = Defined;
29 break;
Davide Italianof7518492016-09-15 17:54:22 +000030 case DefinedWeak:
Davide Italianoec7e29e2016-06-22 20:48:15 +000031 break;
Davide Italianof7518492016-09-15 17:54:22 +000032 case UndefinedWeak:
33 S = DefinedWeak;
Rafael Espindola13b69d62014-07-03 18:59:23 +000034 }
35}
36
Davide Italianoec7e29e2016-06-22 20:48:15 +000037void RecordStreamer::markGlobal(const MCSymbol &Symbol,
38 MCSymbolAttr Attribute) {
Rafael Espindola13b69d62014-07-03 18:59:23 +000039 State &S = Symbols[Symbol.getName()];
40 switch (S) {
41 case DefinedGlobal:
42 case Defined:
Davide Italianof7518492016-09-15 17:54:22 +000043 S = (Attribute == MCSA_Weak) ? DefinedWeak : DefinedGlobal;
Rafael Espindola13b69d62014-07-03 18:59:23 +000044 break;
45
46 case NeverSeen:
47 case Global:
48 case Used:
Davide Italianof7518492016-09-15 17:54:22 +000049 S = (Attribute == MCSA_Weak) ? UndefinedWeak : Global;
Davide Italianoec7e29e2016-06-22 20:48:15 +000050 break;
Davide Italianof7518492016-09-15 17:54:22 +000051 case UndefinedWeak:
52 case DefinedWeak:
Rafael Espindola13b69d62014-07-03 18:59:23 +000053 break;
54 }
55}
56
57void RecordStreamer::markUsed(const MCSymbol &Symbol) {
58 State &S = Symbols[Symbol.getName()];
59 switch (S) {
60 case DefinedGlobal:
61 case Defined:
62 case Global:
Davide Italianof7518492016-09-15 17:54:22 +000063 case DefinedWeak:
64 case UndefinedWeak:
Rafael Espindola13b69d62014-07-03 18:59:23 +000065 break;
66
67 case NeverSeen:
68 case Used:
69 S = Used;
70 break;
71 }
72}
73
74void RecordStreamer::visitUsedSymbol(const MCSymbol &Sym) { markUsed(Sym); }
75
Vitaly Buka0d038812018-03-20 00:38:33 +000076RecordStreamer::RecordStreamer(MCContext &Context, const Module &M)
77 : MCStreamer(Context), M(M) {}
Eugene Zelenko1df42fa2017-04-24 23:21:38 +000078
Rafael Espindola13b69d62014-07-03 18:59:23 +000079RecordStreamer::const_iterator RecordStreamer::begin() {
80 return Symbols.begin();
81}
82
83RecordStreamer::const_iterator RecordStreamer::end() { return Symbols.end(); }
84
Rafael Espindola13b69d62014-07-03 18:59:23 +000085void RecordStreamer::EmitInstruction(const MCInst &Inst,
Andrew V. Tischenko75745d02017-04-14 07:44:23 +000086 const MCSubtargetInfo &STI, bool) {
Rafael Espindola13b69d62014-07-03 18:59:23 +000087 MCStreamer::EmitInstruction(Inst, STI);
88}
89
Rafael Espindolabe991572017-02-10 15:13:12 +000090void RecordStreamer::EmitLabel(MCSymbol *Symbol, SMLoc Loc) {
Rafael Espindola13b69d62014-07-03 18:59:23 +000091 MCStreamer::EmitLabel(Symbol);
92 markDefined(*Symbol);
93}
94
95void RecordStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
96 markDefined(*Symbol);
97 MCStreamer::EmitAssignment(Symbol, Value);
98}
99
100bool RecordStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
101 MCSymbolAttr Attribute) {
Davide Italiano16bfa132016-06-17 18:20:14 +0000102 if (Attribute == MCSA_Global || Attribute == MCSA_Weak)
Davide Italianoec7e29e2016-06-22 20:48:15 +0000103 markGlobal(*Symbol, Attribute);
Mehdi Aminif9721ba2016-08-03 03:51:42 +0000104 if (Attribute == MCSA_LazyReference)
105 markUsed(*Symbol);
Rafael Espindola13b69d62014-07-03 18:59:23 +0000106 return true;
107}
108
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000109void RecordStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
Rafael Espindola13b69d62014-07-03 18:59:23 +0000110 uint64_t Size, unsigned ByteAlignment) {
111 markDefined(*Symbol);
112}
113
114void RecordStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
115 unsigned ByteAlignment) {
116 markDefined(*Symbol);
117}
Teresa Johnsond8204472017-03-09 00:19:49 +0000118
Vitaly Buka0d038812018-03-20 00:38:33 +0000119RecordStreamer::State RecordStreamer::getSymbolState(const MCSymbol *Sym) {
120 auto SI = Symbols.find(Sym->getName());
121 if (SI == Symbols.end())
122 return NeverSeen;
123 return SI->second;
124}
125
Rafael Espindola47b4d6b2018-03-09 18:42:25 +0000126void RecordStreamer::emitELFSymverDirective(StringRef AliasName,
Teresa Johnsond8204472017-03-09 00:19:49 +0000127 const MCSymbol *Aliasee) {
Vitaly Buka0d038812018-03-20 00:38:33 +0000128 SymverAliasMap[Aliasee].push_back(AliasName);
129}
130
Vlad Tsyrklevich230b2562018-04-20 01:36:48 +0000131iterator_range<RecordStreamer::const_symver_iterator>
132RecordStreamer::symverAliases() {
133 return {SymverAliasMap.begin(), SymverAliasMap.end()};
134}
135
Vitaly Buka0d038812018-03-20 00:38:33 +0000136void RecordStreamer::flushSymverDirectives() {
137 // Mapping from mangled name to GV.
138 StringMap<const GlobalValue *> MangledNameMap;
139 // The name in the assembler will be mangled, but the name in the IR
140 // might not, so we first compute a mapping from mangled name to GV.
141 Mangler Mang;
142 SmallString<64> MangledName;
143 for (const GlobalValue &GV : M.global_values()) {
144 if (!GV.hasName())
145 continue;
146 MangledName.clear();
147 MangledName.reserve(GV.getName().size() + 1);
148 Mang.getNameWithPrefix(MangledName, &GV, /*CannotUsePrivateLabel=*/false);
149 MangledNameMap[MangledName] = &GV;
150 }
151
152 // Walk all the recorded .symver aliases, and set up the binding
153 // for each alias.
154 for (auto &Symver : SymverAliasMap) {
155 const MCSymbol *Aliasee = Symver.first;
156 MCSymbolAttr Attr = MCSA_Invalid;
Vitaly Buka849217a2018-03-20 00:45:03 +0000157 bool IsDefined = false;
Vitaly Buka0d038812018-03-20 00:38:33 +0000158
159 // First check if the aliasee binding was recorded in the asm.
160 RecordStreamer::State state = getSymbolState(Aliasee);
161 switch (state) {
162 case RecordStreamer::Global:
163 case RecordStreamer::DefinedGlobal:
164 Attr = MCSA_Global;
165 break;
166 case RecordStreamer::UndefinedWeak:
167 case RecordStreamer::DefinedWeak:
168 Attr = MCSA_Weak;
169 break;
170 default:
171 break;
172 }
173
Vitaly Buka849217a2018-03-20 00:45:03 +0000174 switch (state) {
175 case RecordStreamer::Defined:
176 case RecordStreamer::DefinedGlobal:
177 case RecordStreamer::DefinedWeak:
178 IsDefined = true;
179 break;
180 case RecordStreamer::NeverSeen:
181 case RecordStreamer::Global:
182 case RecordStreamer::Used:
183 case RecordStreamer::UndefinedWeak:
184 break;
185 }
186
187 if (Attr == MCSA_Invalid || !IsDefined) {
188 const GlobalValue *GV = M.getNamedValue(Aliasee->getName());
Vitaly Buka0d038812018-03-20 00:38:33 +0000189 if (!GV) {
190 auto MI = MangledNameMap.find(Aliasee->getName());
191 if (MI != MangledNameMap.end())
192 GV = MI->second;
193 }
194 if (GV) {
Vitaly Buka849217a2018-03-20 00:45:03 +0000195 // If we don't have a symbol attribute from assembly, then check if
196 // the aliasee was defined in the IR.
197 if (Attr == MCSA_Invalid) {
198 if (GV->hasExternalLinkage())
199 Attr = MCSA_Global;
200 else if (GV->hasLocalLinkage())
201 Attr = MCSA_Local;
202 else if (GV->isWeakForLinker())
203 Attr = MCSA_Weak;
204 }
205 IsDefined = IsDefined || !GV->isDeclarationForLinker();
Vitaly Buka0d038812018-03-20 00:38:33 +0000206 }
207 }
Vitaly Buka849217a2018-03-20 00:45:03 +0000208
Vitaly Buka0d038812018-03-20 00:38:33 +0000209 // Set the detected binding on each alias with this aliasee.
210 for (auto AliasName : Symver.second) {
Vitaly Buka849217a2018-03-20 00:45:03 +0000211 std::pair<StringRef, StringRef> Split = AliasName.split("@@@");
212 SmallString<128> NewName;
213 if (!Split.second.empty() && !Split.second.startswith("@")) {
214 // Special processing for "@@@" according
215 // https://sourceware.org/binutils/docs/as/Symver.html
216 const char *Separator = IsDefined ? "@@" : "@";
217 AliasName =
218 (Split.first + Separator + Split.second).toStringRef(NewName);
219 }
Vitaly Buka0d038812018-03-20 00:38:33 +0000220 MCSymbol *Alias = getContext().getOrCreateSymbol(AliasName);
221 // TODO: Handle "@@@". Depending on SymbolAttribute value it needs to be
222 // converted into @ or @@.
223 const MCExpr *Value = MCSymbolRefExpr::create(Aliasee, getContext());
Vitaly Buka6c05a3b2018-04-10 00:53:16 +0000224 if (IsDefined)
225 markDefined(*Alias);
226 // Don't use EmitAssignment override as it always marks alias as defined.
227 MCStreamer::EmitAssignment(Alias, Value);
Vitaly Buka0d038812018-03-20 00:38:33 +0000228 if (Attr != MCSA_Invalid)
229 EmitSymbolAttribute(Alias, Attr);
230 }
231 }
Teresa Johnsond8204472017-03-09 00:19:49 +0000232}