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::emitLabel(MCSymbol
*Symbol
, SMLoc Loc
) {
85 MCStreamer::emitLabel(Symbol
);
89 void RecordStreamer::emitAssignment(MCSymbol
*Symbol
, const MCExpr
*Value
) {
91 MCStreamer::emitAssignment(Symbol
, Value
);
94 bool RecordStreamer::emitSymbolAttribute(MCSymbol
*Symbol
,
95 MCSymbolAttr Attribute
) {
96 if (Attribute
== MCSA_Global
|| Attribute
== MCSA_Weak
)
97 markGlobal(*Symbol
, Attribute
);
98 if (Attribute
== MCSA_LazyReference
)
103 void RecordStreamer::emitZerofill(MCSection
*Section
, MCSymbol
*Symbol
,
104 uint64_t Size
, Align ByteAlignment
,
106 markDefined(*Symbol
);
109 void RecordStreamer::emitCommonSymbol(MCSymbol
*Symbol
, uint64_t Size
,
110 Align ByteAlignment
) {
111 markDefined(*Symbol
);
114 RecordStreamer::State
RecordStreamer::getSymbolState(const MCSymbol
*Sym
) {
115 auto SI
= Symbols
.find(Sym
->getName());
116 if (SI
== Symbols
.end())
121 void RecordStreamer::emitELFSymverDirective(const MCSymbol
*OriginalSym
,
123 bool KeepOriginalSym
) {
124 SymverAliasMap
[OriginalSym
].push_back(Name
);
127 iterator_range
<RecordStreamer::const_symver_iterator
>
128 RecordStreamer::symverAliases() {
129 return {SymverAliasMap
.begin(), SymverAliasMap
.end()};
132 void RecordStreamer::flushSymverDirectives() {
133 // Mapping from mangled name to GV.
134 StringMap
<const GlobalValue
*> MangledNameMap
;
135 // The name in the assembler will be mangled, but the name in the IR
136 // might not, so we first compute a mapping from mangled name to GV.
138 SmallString
<64> MangledName
;
139 for (const GlobalValue
&GV
: M
.global_values()) {
143 MangledName
.reserve(GV
.getName().size() + 1);
144 Mang
.getNameWithPrefix(MangledName
, &GV
, /*CannotUsePrivateLabel=*/false);
145 MangledNameMap
[MangledName
] = &GV
;
148 // Walk all the recorded .symver aliases, and set up the binding
150 for (auto &Symver
: SymverAliasMap
) {
151 const MCSymbol
*Aliasee
= Symver
.first
;
152 MCSymbolAttr Attr
= MCSA_Invalid
;
153 bool IsDefined
= false;
155 // First check if the aliasee binding was recorded in the asm.
156 RecordStreamer::State state
= getSymbolState(Aliasee
);
158 case RecordStreamer::Global
:
159 case RecordStreamer::DefinedGlobal
:
162 case RecordStreamer::UndefinedWeak
:
163 case RecordStreamer::DefinedWeak
:
171 case RecordStreamer::Defined
:
172 case RecordStreamer::DefinedGlobal
:
173 case RecordStreamer::DefinedWeak
:
176 case RecordStreamer::NeverSeen
:
177 case RecordStreamer::Global
:
178 case RecordStreamer::Used
:
179 case RecordStreamer::UndefinedWeak
:
183 if (Attr
== MCSA_Invalid
|| !IsDefined
) {
184 const GlobalValue
*GV
= M
.getNamedValue(Aliasee
->getName());
186 auto MI
= MangledNameMap
.find(Aliasee
->getName());
187 if (MI
!= MangledNameMap
.end())
191 // If we don't have a symbol attribute from assembly, then check if
192 // the aliasee was defined in the IR.
193 if (Attr
== MCSA_Invalid
) {
194 if (GV
->hasExternalLinkage())
196 else if (GV
->hasLocalLinkage())
198 else if (GV
->isWeakForLinker())
201 IsDefined
= IsDefined
|| !GV
->isDeclarationForLinker();
205 // Set the detected binding on each alias with this aliasee.
206 for (auto AliasName
: Symver
.second
) {
207 std::pair
<StringRef
, StringRef
> Split
= AliasName
.split("@@@");
208 SmallString
<128> NewName
;
209 if (!Split
.second
.empty() && !Split
.second
.starts_with("@")) {
210 // Special processing for "@@@" according
211 // https://sourceware.org/binutils/docs/as/Symver.html
212 const char *Separator
= IsDefined
? "@@" : "@";
214 (Split
.first
+ Separator
+ Split
.second
).toStringRef(NewName
);
216 MCSymbol
*Alias
= getContext().getOrCreateSymbol(AliasName
);
217 // TODO: Handle "@@@". Depending on SymbolAttribute value it needs to be
218 // converted into @ or @@.
219 const MCExpr
*Value
= MCSymbolRefExpr::create(Aliasee
, getContext());
222 // Don't use EmitAssignment override as it always marks alias as defined.
223 MCStreamer::emitAssignment(Alias
, Value
);
224 if (Attr
!= MCSA_Invalid
)
225 emitSymbolAttribute(Alias
, Attr
);