LLVM 20.0.0git
Mangler.cpp
Go to the documentation of this file.
1//===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// Unified name mangler for assembly backends.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/IR/Mangler.h"
16#include "llvm/ADT/Twine.h"
17#include "llvm/IR/DataLayout.h"
19#include "llvm/IR/Function.h"
20#include "llvm/IR/Module.h"
23
24using namespace llvm;
25
26namespace {
27enum ManglerPrefixTy {
28 Default, ///< Emit default string before each symbol.
29 Private, ///< Emit "private" prefix before each symbol.
30 LinkerPrivate ///< Emit "linker private" prefix before each symbol.
31};
32}
33
34static void getNameWithPrefixImpl(raw_ostream &OS, const Twine &GVName,
35 ManglerPrefixTy PrefixTy,
36 const DataLayout &DL, char Prefix) {
37 SmallString<256> TmpData;
38 StringRef Name = GVName.toStringRef(TmpData);
39 assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
40
41 // No need to do anything special if the global has the special "do not
42 // mangle" flag in the name.
43 if (Name[0] == '\1') {
44 OS << Name.substr(1);
45 return;
46 }
47
48 if (DL.doNotMangleLeadingQuestionMark() && Name[0] == '?')
49 Prefix = '\0';
50
51 if (PrefixTy == Private)
52 OS << DL.getPrivateGlobalPrefix();
53 else if (PrefixTy == LinkerPrivate)
54 OS << DL.getLinkerPrivateGlobalPrefix();
55
56 if (Prefix != '\0')
57 OS << Prefix;
58
59 // If this is a simple string that doesn't need escaping, just append it.
60 OS << Name;
61}
62
63static void getNameWithPrefixImpl(raw_ostream &OS, const Twine &GVName,
64 const DataLayout &DL,
65 ManglerPrefixTy PrefixTy) {
66 char Prefix = DL.getGlobalPrefix();
67 return getNameWithPrefixImpl(OS, GVName, PrefixTy, DL, Prefix);
68}
69
71 const DataLayout &DL) {
72 return getNameWithPrefixImpl(OS, GVName, DL, Default);
73}
74
76 const Twine &GVName, const DataLayout &DL) {
77 raw_svector_ostream OS(OutName);
78 char Prefix = DL.getGlobalPrefix();
79 return getNameWithPrefixImpl(OS, GVName, Default, DL, Prefix);
80}
81
83 switch (CC) {
87 return true;
88 default:
89 return false;
90 }
91}
92
93/// Microsoft fastcall and stdcall functions require a suffix on their name
94/// indicating the number of words of arguments they take.
96 const DataLayout &DL) {
97 // Calculate arguments size total.
98 unsigned ArgWords = 0;
99
100 const unsigned PtrSize = DL.getPointerSize();
101
102 for (const Argument &A : F->args()) {
103 // For the purposes of the byte count suffix, structs returned by pointer
104 // do not count as function arguments.
105 if (A.hasStructRetAttr())
106 continue;
107
108 // 'Dereference' type in case of byval or inalloca parameter attribute.
109 uint64_t AllocSize = A.hasPassPointeeByValueCopyAttr() ?
110 A.getPassPointeeByValueCopySize(DL) :
111 DL.getTypeAllocSize(A.getType());
112
113 // Size should be aligned to pointer size.
114 ArgWords += alignTo(AllocSize, PtrSize);
115 }
116
117 OS << '@' << ArgWords;
118}
119
121 bool CannotUsePrivateLabel) const {
122 ManglerPrefixTy PrefixTy = Default;
123 assert(GV != nullptr && "Invalid Global Value");
124 if (GV->hasPrivateLinkage()) {
125 if (CannotUsePrivateLabel)
126 PrefixTy = LinkerPrivate;
127 else
128 PrefixTy = Private;
129 }
130
131 const DataLayout &DL = GV->getDataLayout();
132 if (!GV->hasName()) {
133 // Get the ID for the global, assigning a new one if we haven't got one
134 // already.
135 unsigned &ID = AnonGlobalIDs[GV];
136 if (ID == 0)
137 ID = AnonGlobalIDs.size();
138
139 // Must mangle the global into a unique ID.
140 getNameWithPrefixImpl(OS, "__unnamed_" + Twine(ID), DL, PrefixTy);
141 return;
142 }
143
144 StringRef Name = GV->getName();
145 char Prefix = DL.getGlobalPrefix();
146
147 // Mangle functions with Microsoft calling conventions specially. Only do
148 // this mangling for x86_64 vectorcall and 32-bit x86.
149 const Function *MSFunc = dyn_cast_or_null<Function>(GV->getAliaseeObject());
150
151 // Don't add byte count suffixes when '\01' or '?' are in the first
152 // character.
153 if (Name.starts_with("\01") ||
154 (DL.doNotMangleLeadingQuestionMark() && Name.starts_with("?")))
155 MSFunc = nullptr;
156
158 MSFunc ? MSFunc->getCallingConv() : (unsigned)CallingConv::C;
159 if (!DL.hasMicrosoftFastStdCallMangling() &&
161 MSFunc = nullptr;
162 if (MSFunc) {
164 Prefix = '@'; // fastcall functions have an @ prefix instead of _.
166 Prefix = '\0'; // vectorcall functions have no prefix.
167 }
168
169 getNameWithPrefixImpl(OS, Name, PrefixTy, DL, Prefix);
170
171 if (!MSFunc)
172 return;
173
174 // If we are supposed to add a microsoft-style suffix for stdcall, fastcall,
175 // or vectorcall, add it. These functions have a suffix of @N where N is the
176 // cumulative byte size of all of the parameters to the function in decimal.
178 OS << '@'; // vectorcall functions use a double @ suffix.
179 FunctionType *FT = MSFunc->getFunctionType();
180 if (hasByteCountSuffix(CC) &&
181 // "Pure" variadic functions do not receive @0 suffix.
182 (!FT->isVarArg() || FT->getNumParams() == 0 ||
183 (FT->getNumParams() == 1 && MSFunc->hasStructRetAttr())))
184 addByteCountSuffix(OS, MSFunc, DL);
185}
186
188 const GlobalValue *GV,
189 bool CannotUsePrivateLabel) const {
190 raw_svector_ostream OS(OutName);
191 getNameWithPrefix(OS, GV, CannotUsePrivateLabel);
192}
193
194// Check if the name needs quotes to be safe for the linker to interpret.
195static bool canBeUnquotedInDirective(char C) {
196 return isAlnum(C) || C == '_' || C == '@' || C == '#';
197}
198
200 if (Name.empty())
201 return false;
202
203 // If any of the characters in the string is an unacceptable character, force
204 // quotes.
205 for (char C : Name) {
207 return false;
208 }
209
210 return true;
211}
212
214 const Triple &TT, Mangler &Mangler) {
215 if (GV->hasDLLExportStorageClass() && !GV->isDeclaration()) {
216
217 if (TT.isWindowsMSVCEnvironment())
218 OS << " /EXPORT:";
219 else
220 OS << " -export:";
221
222 bool NeedQuotes = GV->hasName() && !canBeUnquotedInDirective(GV->getName());
223 if (NeedQuotes)
224 OS << "\"";
225 if (TT.isWindowsGNUEnvironment() || TT.isWindowsCygwinEnvironment()) {
226 std::string Flag;
227 raw_string_ostream FlagOS(Flag);
228 Mangler.getNameWithPrefix(FlagOS, GV, false);
229 FlagOS.flush();
230 if (Flag[0] == GV->getDataLayout().getGlobalPrefix())
231 OS << Flag.substr(1);
232 else
233 OS << Flag;
234 } else {
235 Mangler.getNameWithPrefix(OS, GV, false);
236 }
237 if (TT.isWindowsArm64EC()) {
238 // Use EXPORTAS for mangled ARM64EC symbols.
239 // FIXME: During LTO, we're invoked prior to the EC lowering pass,
240 // so symbols are not yet mangled. Emitting the unmangled name
241 // typically functions correctly; the linker can resolve the export
242 // with the demangled alias.
243 if (std::optional<std::string> demangledName =
245 OS << ",EXPORTAS," << *demangledName;
246 }
247 if (NeedQuotes)
248 OS << "\"";
249
250 if (!GV->getValueType()->isFunctionTy()) {
251 if (TT.isWindowsMSVCEnvironment())
252 OS << ",DATA";
253 else
254 OS << ",data";
255 }
256 }
257 if (GV->hasHiddenVisibility() && !GV->isDeclaration() && TT.isOSCygMing()) {
258
259 OS << " -exclude-symbols:";
260
261 bool NeedQuotes = GV->hasName() && !canBeUnquotedInDirective(GV->getName());
262 if (NeedQuotes)
263 OS << "\"";
264
265 std::string Flag;
266 raw_string_ostream FlagOS(Flag);
267 Mangler.getNameWithPrefix(FlagOS, GV, false);
268 FlagOS.flush();
269 if (Flag[0] == GV->getDataLayout().getGlobalPrefix())
270 OS << Flag.substr(1);
271 else
272 OS << Flag;
273
274 if (NeedQuotes)
275 OS << "\"";
276 }
277}
278
280 const Triple &T, Mangler &M) {
281 if (!T.isWindowsMSVCEnvironment())
282 return;
283
284 OS << " /INCLUDE:";
285 bool NeedQuotes = GV->hasName() && !canBeUnquotedInDirective(GV->getName());
286 if (NeedQuotes)
287 OS << "\"";
288 M.getNameWithPrefix(OS, GV, false);
289 if (NeedQuotes)
290 OS << "\"";
291}
292
294 bool IsCppFn = Name[0] == '?';
295 if (IsCppFn && Name.contains("$$h"))
296 return std::nullopt;
297 if (!IsCppFn && Name[0] == '#')
298 return std::nullopt;
299
300 StringRef Prefix = "$$h";
301 size_t InsertIdx = 0;
302 if (IsCppFn) {
303 InsertIdx = Name.find("@@");
304 size_t ThreeAtSignsIdx = Name.find("@@@");
305 if (InsertIdx != std::string::npos && InsertIdx != ThreeAtSignsIdx) {
306 InsertIdx += 2;
307 } else {
308 InsertIdx = Name.find("@");
309 if (InsertIdx != std::string::npos)
310 InsertIdx++;
311 }
312 } else {
313 Prefix = "#";
314 }
315
316 return std::optional<std::string>(
317 (Name.substr(0, InsertIdx) + Prefix + Name.substr(InsertIdx)).str());
318}
319
320std::optional<std::string>
322 if (Name[0] == '#')
323 return std::optional<std::string>(Name.substr(1));
324 if (Name[0] != '?')
325 return std::nullopt;
326
327 std::pair<StringRef, StringRef> Pair = Name.split("$$h");
328 if (Pair.second.empty())
329 return std::nullopt;
330 return std::optional<std::string>((Pair.first + Pair.second).str());
331}
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
@ Default
Definition: DwarfDebug.cpp:87
std::string Name
#define F(x, y, z)
Definition: MD5.cpp:55
static bool canBeUnquotedInDirective(char C)
Definition: Mangler.cpp:195
static void getNameWithPrefixImpl(raw_ostream &OS, const Twine &GVName, ManglerPrefixTy PrefixTy, const DataLayout &DL, char Prefix)
Definition: Mangler.cpp:34
static void addByteCountSuffix(raw_ostream &OS, const Function *F, const DataLayout &DL)
Microsoft fastcall and stdcall functions require a suffix on their name indicating the number of word...
Definition: Mangler.cpp:95
static bool hasByteCountSuffix(CallingConv::ID CC)
Definition: Mangler.cpp:82
Module.h This file contains the declarations for the Module class.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
This file defines the SmallString class.
This file contains some functions that are useful when dealing with strings.
This class represents an incoming formal argument to a Function.
Definition: Argument.h:31
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:63
char getGlobalPrefix() const
Definition: DataLayout.h:273
Class to represent function types.
Definition: DerivedTypes.h:103
unsigned getNumParams() const
Return the number of fixed parameters this function type requires.
Definition: DerivedTypes.h:142
bool isVarArg() const
Definition: DerivedTypes.h:123
FunctionType * getFunctionType() const
Returns the FunctionType for me.
Definition: Function.h:214
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition: Function.h:281
bool hasStructRetAttr() const
Determine if the function returns a structure through first or second pointer argument.
Definition: Function.h:686
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:290
bool hasPrivateLinkage() const
Definition: GlobalValue.h:527
bool hasHiddenVisibility() const
Definition: GlobalValue.h:250
bool hasDLLExportStorageClass() const
Definition: GlobalValue.h:281
const GlobalObject * getAliaseeObject() const
Definition: Globals.cpp:394
const DataLayout & getDataLayout() const
Get the data layout of the module this global belongs to.
Definition: Globals.cpp:124
Type * getValueType() const
Definition: GlobalValue.h:296
void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, bool CannotUsePrivateLabel) const
Print the appropriate prefix and the specified global variable's name.
Definition: Mangler.cpp:120
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
StringRef toStringRef(SmallVectorImpl< char > &Out) const
This returns the twine as a single StringRef if it can be represented as such.
Definition: Twine.h:492
bool isFunctionTy() const
True if this is an instance of FunctionType.
Definition: Type.h:242
bool hasName() const
Definition: Value.h:261
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:661
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:691
@ X86_StdCall
stdcall is mostly used by the Win32 API.
Definition: CallingConv.h:99
@ X86_VectorCall
MSVC calling convention that passes vectors and vector aggregates in SSE registers.
Definition: CallingConv.h:163
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
@ X86_FastCall
'fast' analog of X86_StdCall.
Definition: CallingConv.h:103
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::optional< std::string > getArm64ECMangledFunctionName(StringRef Name)
Definition: Mangler.cpp:293
std::optional< std::string > getArm64ECDemangledFunctionName(StringRef Name)
Definition: Mangler.cpp:321
void emitLinkerFlagsForUsedCOFF(raw_ostream &OS, const GlobalValue *GV, const Triple &T, Mangler &M)
Definition: Mangler.cpp:279
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:155
void emitLinkerFlagsForGlobalCOFF(raw_ostream &OS, const GlobalValue *GV, const Triple &TT, Mangler &Mangler)
Definition: Mangler.cpp:213
@ Default
The result values are uniform if and only if all operands are uniform.