1 //===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===//
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 // Unified name mangler for assembly backends.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/IR/Mangler.h"
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/ADT/StringExtras.h"
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/IR/DataLayout.h"
18 #include "llvm/IR/DerivedTypes.h"
19 #include "llvm/IR/Function.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include "llvm/TargetParser/Triple.h"
26 enum ManglerPrefixTy
{
27 Default
, ///< Emit default string before each symbol.
28 Private
, ///< Emit "private" prefix before each symbol.
29 LinkerPrivate
///< Emit "linker private" prefix before each symbol.
33 static void getNameWithPrefixImpl(raw_ostream
&OS
, const Twine
&GVName
,
34 ManglerPrefixTy PrefixTy
,
35 const DataLayout
&DL
, char Prefix
) {
36 SmallString
<256> TmpData
;
37 StringRef Name
= GVName
.toStringRef(TmpData
);
38 assert(!Name
.empty() && "getNameWithPrefix requires non-empty name");
40 // No need to do anything special if the global has the special "do not
41 // mangle" flag in the name.
42 if (Name
[0] == '\1') {
47 if (DL
.doNotMangleLeadingQuestionMark() && Name
[0] == '?')
50 if (PrefixTy
== Private
)
51 OS
<< DL
.getPrivateGlobalPrefix();
52 else if (PrefixTy
== LinkerPrivate
)
53 OS
<< DL
.getLinkerPrivateGlobalPrefix();
58 // If this is a simple string that doesn't need escaping, just append it.
62 static void getNameWithPrefixImpl(raw_ostream
&OS
, const Twine
&GVName
,
64 ManglerPrefixTy PrefixTy
) {
65 char Prefix
= DL
.getGlobalPrefix();
66 return getNameWithPrefixImpl(OS
, GVName
, PrefixTy
, DL
, Prefix
);
69 void Mangler::getNameWithPrefix(raw_ostream
&OS
, const Twine
&GVName
,
70 const DataLayout
&DL
) {
71 return getNameWithPrefixImpl(OS
, GVName
, DL
, Default
);
74 void Mangler::getNameWithPrefix(SmallVectorImpl
<char> &OutName
,
75 const Twine
&GVName
, const DataLayout
&DL
) {
76 raw_svector_ostream
OS(OutName
);
77 char Prefix
= DL
.getGlobalPrefix();
78 return getNameWithPrefixImpl(OS
, GVName
, Default
, DL
, Prefix
);
81 static bool hasByteCountSuffix(CallingConv::ID CC
) {
83 case CallingConv::X86_FastCall
:
84 case CallingConv::X86_StdCall
:
85 case CallingConv::X86_VectorCall
:
92 /// Microsoft fastcall and stdcall functions require a suffix on their name
93 /// indicating the number of words of arguments they take.
94 static void addByteCountSuffix(raw_ostream
&OS
, const Function
*F
,
95 const DataLayout
&DL
) {
96 // Calculate arguments size total.
97 unsigned ArgWords
= 0;
99 const unsigned PtrSize
= DL
.getPointerSize();
101 for (const Argument
&A
: F
->args()) {
102 // For the purposes of the byte count suffix, structs returned by pointer
103 // do not count as function arguments.
104 if (A
.hasStructRetAttr())
107 // 'Dereference' type in case of byval or inalloca parameter attribute.
108 uint64_t AllocSize
= A
.hasPassPointeeByValueCopyAttr() ?
109 A
.getPassPointeeByValueCopySize(DL
) :
110 DL
.getTypeAllocSize(A
.getType());
112 // Size should be aligned to pointer size.
113 ArgWords
+= alignTo(AllocSize
, PtrSize
);
116 OS
<< '@' << ArgWords
;
119 void Mangler::getNameWithPrefix(raw_ostream
&OS
, const GlobalValue
*GV
,
120 bool CannotUsePrivateLabel
) const {
121 ManglerPrefixTy PrefixTy
= Default
;
122 assert(GV
!= nullptr && "Invalid Global Value");
123 if (GV
->hasPrivateLinkage()) {
124 if (CannotUsePrivateLabel
)
125 PrefixTy
= LinkerPrivate
;
130 const DataLayout
&DL
= GV
->getParent()->getDataLayout();
131 if (!GV
->hasName()) {
132 // Get the ID for the global, assigning a new one if we haven't got one
134 unsigned &ID
= AnonGlobalIDs
[GV
];
136 ID
= AnonGlobalIDs
.size();
138 // Must mangle the global into a unique ID.
139 getNameWithPrefixImpl(OS
, "__unnamed_" + Twine(ID
), DL
, PrefixTy
);
143 StringRef Name
= GV
->getName();
144 char Prefix
= DL
.getGlobalPrefix();
146 // Mangle functions with Microsoft calling conventions specially. Only do
147 // this mangling for x86_64 vectorcall and 32-bit x86.
148 const Function
*MSFunc
= dyn_cast_or_null
<Function
>(GV
->getAliaseeObject());
150 // Don't add byte count suffixes when '\01' or '?' are in the first
152 if (Name
.startswith("\01") ||
153 (DL
.doNotMangleLeadingQuestionMark() && Name
.startswith("?")))
157 MSFunc
? MSFunc
->getCallingConv() : (unsigned)CallingConv::C
;
158 if (!DL
.hasMicrosoftFastStdCallMangling() &&
159 CC
!= CallingConv::X86_VectorCall
)
162 if (CC
== CallingConv::X86_FastCall
)
163 Prefix
= '@'; // fastcall functions have an @ prefix instead of _.
164 else if (CC
== CallingConv::X86_VectorCall
)
165 Prefix
= '\0'; // vectorcall functions have no prefix.
168 getNameWithPrefixImpl(OS
, Name
, PrefixTy
, DL
, Prefix
);
173 // If we are supposed to add a microsoft-style suffix for stdcall, fastcall,
174 // or vectorcall, add it. These functions have a suffix of @N where N is the
175 // cumulative byte size of all of the parameters to the function in decimal.
176 if (CC
== CallingConv::X86_VectorCall
)
177 OS
<< '@'; // vectorcall functions use a double @ suffix.
178 FunctionType
*FT
= MSFunc
->getFunctionType();
179 if (hasByteCountSuffix(CC
) &&
180 // "Pure" variadic functions do not receive @0 suffix.
181 (!FT
->isVarArg() || FT
->getNumParams() == 0 ||
182 (FT
->getNumParams() == 1 && MSFunc
->hasStructRetAttr())))
183 addByteCountSuffix(OS
, MSFunc
, DL
);
186 void Mangler::getNameWithPrefix(SmallVectorImpl
<char> &OutName
,
187 const GlobalValue
*GV
,
188 bool CannotUsePrivateLabel
) const {
189 raw_svector_ostream
OS(OutName
);
190 getNameWithPrefix(OS
, GV
, CannotUsePrivateLabel
);
193 // Check if the name needs quotes to be safe for the linker to interpret.
194 static bool canBeUnquotedInDirective(char C
) {
195 return isAlnum(C
) || C
== '_' || C
== '@';
198 static bool canBeUnquotedInDirective(StringRef Name
) {
202 // If any of the characters in the string is an unacceptable character, force
204 for (char C
: Name
) {
205 if (!canBeUnquotedInDirective(C
))
212 void llvm::emitLinkerFlagsForGlobalCOFF(raw_ostream
&OS
, const GlobalValue
*GV
,
213 const Triple
&TT
, Mangler
&Mangler
) {
214 if (GV
->hasDLLExportStorageClass() && !GV
->isDeclaration()) {
216 if (TT
.isWindowsMSVCEnvironment())
221 bool NeedQuotes
= GV
->hasName() && !canBeUnquotedInDirective(GV
->getName());
224 if (TT
.isWindowsGNUEnvironment() || TT
.isWindowsCygwinEnvironment()) {
226 raw_string_ostream
FlagOS(Flag
);
227 Mangler
.getNameWithPrefix(FlagOS
, GV
, false);
229 if (Flag
[0] == GV
->getParent()->getDataLayout().getGlobalPrefix())
230 OS
<< Flag
.substr(1);
234 Mangler
.getNameWithPrefix(OS
, GV
, false);
239 if (!GV
->getValueType()->isFunctionTy()) {
240 if (TT
.isWindowsMSVCEnvironment())
246 if (GV
->hasHiddenVisibility() && !GV
->isDeclaration() && TT
.isOSCygMing()) {
248 OS
<< " -exclude-symbols:";
250 bool NeedQuotes
= GV
->hasName() && !canBeUnquotedInDirective(GV
->getName());
255 raw_string_ostream
FlagOS(Flag
);
256 Mangler
.getNameWithPrefix(FlagOS
, GV
, false);
258 if (Flag
[0] == GV
->getParent()->getDataLayout().getGlobalPrefix())
259 OS
<< Flag
.substr(1);
268 void llvm::emitLinkerFlagsForUsedCOFF(raw_ostream
&OS
, const GlobalValue
*GV
,
269 const Triple
&T
, Mangler
&M
) {
270 if (!T
.isWindowsMSVCEnvironment())
274 bool NeedQuotes
= GV
->hasName() && !canBeUnquotedInDirective(GV
->getName());
277 M
.getNameWithPrefix(OS
, GV
, false);