1 //===-- RecordStreamer.cpp - Record asm defined and used symbols ----------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "RecordStreamer.h"
10 #include "llvm/IR/Mangler.h"
11 #include "llvm/IR/Module.h"
12 #include "llvm/MC/MCContext.h"
13 #include "llvm/MC/MCSymbol.h"
17 void RecordStreamer::markDefined(const MCSymbol
&Symbol
) {
18 State
&S
= Symbols
[Symbol
.getName()];
36 void RecordStreamer::markGlobal(const MCSymbol
&Symbol
,
37 MCSymbolAttr Attribute
) {
38 State
&S
= Symbols
[Symbol
.getName()];
42 S
= (Attribute
== MCSA_Weak
) ? DefinedWeak
: DefinedGlobal
;
48 S
= (Attribute
== MCSA_Weak
) ? UndefinedWeak
: Global
;
56 void RecordStreamer::markUsed(const MCSymbol
&Symbol
) {
57 State
&S
= Symbols
[Symbol
.getName()];
73 void RecordStreamer::visitUsedSymbol(const MCSymbol
&Sym
) { markUsed(Sym
); }
75 RecordStreamer::RecordStreamer(MCContext
&Context
, const Module
&M
)
76 : MCStreamer(Context
), M(M
) {}
78 RecordStreamer::const_iterator
RecordStreamer::begin() {
79 return Symbols
.begin();
82 RecordStreamer::const_iterator
RecordStreamer::end() { return Symbols
.end(); }
84 void RecordStreamer::EmitInstruction(const MCInst
&Inst
,
85 const MCSubtargetInfo
&STI
) {
86 MCStreamer::EmitInstruction(Inst
, STI
);
89 void RecordStreamer::EmitLabel(MCSymbol
*Symbol
, SMLoc Loc
) {
90 MCStreamer::EmitLabel(Symbol
);
94 void RecordStreamer::EmitAssignment(MCSymbol
*Symbol
, const MCExpr
*Value
) {
96 MCStreamer::EmitAssignment(Symbol
, Value
);
99 bool RecordStreamer::EmitSymbolAttribute(MCSymbol
*Symbol
,
100 MCSymbolAttr Attribute
) {
101 if (Attribute
== MCSA_Global
|| Attribute
== MCSA_Weak
)
102 markGlobal(*Symbol
, Attribute
);
103 if (Attribute
== MCSA_LazyReference
)
108 void RecordStreamer::EmitZerofill(MCSection
*Section
, MCSymbol
*Symbol
,
109 uint64_t Size
, unsigned ByteAlignment
,
111 markDefined(*Symbol
);
114 void RecordStreamer::EmitCommonSymbol(MCSymbol
*Symbol
, uint64_t Size
,
115 unsigned ByteAlignment
) {
116 markDefined(*Symbol
);
119 RecordStreamer::State
RecordStreamer::getSymbolState(const MCSymbol
*Sym
) {
120 auto SI
= Symbols
.find(Sym
->getName());
121 if (SI
== Symbols
.end())
126 void RecordStreamer::emitELFSymverDirective(StringRef AliasName
,
127 const MCSymbol
*Aliasee
) {
128 SymverAliasMap
[Aliasee
].push_back(AliasName
);
131 iterator_range
<RecordStreamer::const_symver_iterator
>
132 RecordStreamer::symverAliases() {
133 return {SymverAliasMap
.begin(), SymverAliasMap
.end()};
136 void 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.
142 SmallString
<64> MangledName
;
143 for (const GlobalValue
&GV
: M
.global_values()) {
147 MangledName
.reserve(GV
.getName().size() + 1);
148 Mang
.getNameWithPrefix(MangledName
, &GV
, /*CannotUsePrivateLabel=*/false);
149 MangledNameMap
[MangledName
] = &GV
;
152 // Walk all the recorded .symver aliases, and set up the binding
154 for (auto &Symver
: SymverAliasMap
) {
155 const MCSymbol
*Aliasee
= Symver
.first
;
156 MCSymbolAttr Attr
= MCSA_Invalid
;
157 bool IsDefined
= false;
159 // First check if the aliasee binding was recorded in the asm.
160 RecordStreamer::State state
= getSymbolState(Aliasee
);
162 case RecordStreamer::Global
:
163 case RecordStreamer::DefinedGlobal
:
166 case RecordStreamer::UndefinedWeak
:
167 case RecordStreamer::DefinedWeak
:
175 case RecordStreamer::Defined
:
176 case RecordStreamer::DefinedGlobal
:
177 case RecordStreamer::DefinedWeak
:
180 case RecordStreamer::NeverSeen
:
181 case RecordStreamer::Global
:
182 case RecordStreamer::Used
:
183 case RecordStreamer::UndefinedWeak
:
187 if (Attr
== MCSA_Invalid
|| !IsDefined
) {
188 const GlobalValue
*GV
= M
.getNamedValue(Aliasee
->getName());
190 auto MI
= MangledNameMap
.find(Aliasee
->getName());
191 if (MI
!= MangledNameMap
.end())
195 // 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())
200 else if (GV
->hasLocalLinkage())
202 else if (GV
->isWeakForLinker())
205 IsDefined
= IsDefined
|| !GV
->isDeclarationForLinker();
209 // Set the detected binding on each alias with this aliasee.
210 for (auto AliasName
: Symver
.second
) {
211 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
? "@@" : "@";
218 (Split
.first
+ Separator
+ Split
.second
).toStringRef(NewName
);
220 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());
226 // Don't use EmitAssignment override as it always marks alias as defined.
227 MCStreamer::EmitAssignment(Alias
, Value
);
228 if (Attr
!= MCSA_Invalid
)
229 EmitSymbolAttribute(Alias
, Attr
);