blob: be3086cfcf05d5f599de80b48d431b900b3029a9 [file] [log] [blame]
Brian Gaekeb198ca32003-07-24 20:20:58 +00001//===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Brian Gaekeb198ca32003-07-24 20:20:58 +00009//
Chris Lattnerc94c8252010-01-16 21:08:46 +000010// Unified name mangler for assembly backends.
Brian Gaekeb198ca32003-07-24 20:20:58 +000011//
12//===----------------------------------------------------------------------===//
13
Rafael Espindolab56c57b2014-01-07 21:19:40 +000014#include "llvm/IR/Mangler.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000015#include "llvm/ADT/SmallString.h"
Peter Collingbourne8dc921e2017-03-31 04:46:50 +000016#include "llvm/ADT/Triple.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000017#include "llvm/ADT/Twine.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000018#include "llvm/IR/DataLayout.h"
19#include "llvm/IR/DerivedTypes.h"
20#include "llvm/IR/Function.h"
Rafael Espindola9758b4a2015-06-23 13:59:29 +000021#include "llvm/IR/Module.h"
Chris Lattner8a29fa62010-03-12 21:03:47 +000022#include "llvm/Support/raw_ostream.h"
Chris Lattner2cdd21c2003-12-14 21:35:53 +000023using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000024
Rafael Espindola0e928842015-06-23 14:11:09 +000025namespace {
26enum 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.
30};
31}
32
Rafael Espindolab9ed9af2015-06-23 12:21:54 +000033static void getNameWithPrefixImpl(raw_ostream &OS, const Twine &GVName,
Rafael Espindola0e928842015-06-23 14:11:09 +000034 ManglerPrefixTy PrefixTy,
Rafael Espindolab9ed9af2015-06-23 12:21:54 +000035 const DataLayout &DL, char Prefix) {
Chris Lattner0e7ab8c2010-01-13 07:01:09 +000036 SmallString<256> TmpData;
Benjamin Kramerb357e062010-01-13 12:45:23 +000037 StringRef Name = GVName.toStringRef(TmpData);
Chris Lattner0e7ab8c2010-01-13 07:01:09 +000038 assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
Rafael Espindola8e0f67d2014-01-03 19:21:54 +000039
Rafael Espindolaa36429f2014-08-01 14:16:40 +000040 // 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') {
43 OS << Name.substr(1);
44 return;
45 }
46
Reid Kleckner025ee4f2018-03-16 20:13:32 +000047 if (DL.doNotMangleLeadingQuestionMark() && Name[0] == '?')
48 Prefix = '\0';
49
Rafael Espindola0e928842015-06-23 14:11:09 +000050 if (PrefixTy == Private)
Rafael Espindola916d3122014-01-29 02:30:38 +000051 OS << DL.getPrivateGlobalPrefix();
Rafael Espindola0e928842015-06-23 14:11:09 +000052 else if (PrefixTy == LinkerPrivate)
Rafael Espindola916d3122014-01-29 02:30:38 +000053 OS << DL.getLinkerPrivateGlobalPrefix();
Chris Lattnerc0dba722010-01-17 18:22:35 +000054
Reid Klecknerd5de3272014-10-28 01:29:26 +000055 if (Prefix != '\0')
56 OS << Prefix;
Rafael Espindolade9a1a22013-11-13 14:01:59 +000057
Chris Lattneracd03ae2010-01-17 19:23:46 +000058 // If this is a simple string that doesn't need escaping, just append it.
Rafael Espindola916d3122014-01-29 02:30:38 +000059 OS << Name;
60}
61
Rafael Espindola0e928842015-06-23 14:11:09 +000062static void getNameWithPrefixImpl(raw_ostream &OS, const Twine &GVName,
63 const DataLayout &DL,
64 ManglerPrefixTy PrefixTy) {
Rafael Espindola9758b4a2015-06-23 13:59:29 +000065 char Prefix = DL.getGlobalPrefix();
66 return getNameWithPrefixImpl(OS, GVName, PrefixTy, DL, Prefix);
Rafael Espindolab9ed9af2015-06-23 12:21:54 +000067}
68
Rafael Espindola0e928842015-06-23 14:11:09 +000069void Mangler::getNameWithPrefix(raw_ostream &OS, const Twine &GVName,
70 const DataLayout &DL) {
71 return getNameWithPrefixImpl(OS, GVName, DL, Default);
72}
73
Rafael Espindolab9ed9af2015-06-23 12:21:54 +000074void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
Rafael Espindola0e928842015-06-23 14:11:09 +000075 const Twine &GVName, const DataLayout &DL) {
Rafael Espindolab9ed9af2015-06-23 12:21:54 +000076 raw_svector_ostream OS(OutName);
77 char Prefix = DL.getGlobalPrefix();
Rafael Espindola0e928842015-06-23 14:11:09 +000078 return getNameWithPrefixImpl(OS, GVName, Default, DL, Prefix);
Chris Lattner0e7ab8c2010-01-13 07:01:09 +000079}
80
Reid Klecknerd5de3272014-10-28 01:29:26 +000081static bool hasByteCountSuffix(CallingConv::ID CC) {
82 switch (CC) {
83 case CallingConv::X86_FastCall:
84 case CallingConv::X86_StdCall:
85 case CallingConv::X86_VectorCall:
86 return true;
87 default:
88 return false;
89 }
90}
91
92/// Microsoft fastcall and stdcall functions require a suffix on their name
93/// indicating the number of words of arguments they take.
94static void addByteCountSuffix(raw_ostream &OS, const Function *F,
Mehdi Amini529919f2015-03-10 02:37:25 +000095 const DataLayout &DL) {
Chris Lattner8a29fa62010-03-12 21:03:47 +000096 // Calculate arguments size total.
97 unsigned ArgWords = 0;
98 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
99 AI != AE; ++AI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000100 Type *Ty = AI->getType();
Reid Kleckner8a24e832014-01-31 23:50:57 +0000101 // 'Dereference' type in case of byval or inalloca parameter attribute.
102 if (AI->hasByValOrInAllocaAttr())
Chris Lattner8a29fa62010-03-12 21:03:47 +0000103 Ty = cast<PointerType>(Ty)->getElementType();
Reid Klecknerd5de3272014-10-28 01:29:26 +0000104 // Size should be aligned to pointer size.
Mehdi Amini529919f2015-03-10 02:37:25 +0000105 unsigned PtrSize = DL.getPointerSize();
Rui Ueyama3edb0ec2016-01-14 21:06:47 +0000106 ArgWords += alignTo(DL.getTypeAllocSize(Ty), PtrSize);
Chris Lattner8a29fa62010-03-12 21:03:47 +0000107 }
Rafael Espindola916d3122014-01-29 02:30:38 +0000108
109 OS << '@' << ArgWords;
Chris Lattner8a29fa62010-03-12 21:03:47 +0000110}
111
Rafael Espindola737c9f62014-02-19 17:23:20 +0000112void Mangler::getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV,
David Majnemer61295382015-03-17 20:40:21 +0000113 bool CannotUsePrivateLabel) const {
Rafael Espindola0e928842015-06-23 14:11:09 +0000114 ManglerPrefixTy PrefixTy = Default;
David Majnemer61295382015-03-17 20:40:21 +0000115 if (GV->hasPrivateLinkage()) {
Rafael Espindola737c9f62014-02-19 17:23:20 +0000116 if (CannotUsePrivateLabel)
Rafael Espindola0e928842015-06-23 14:11:09 +0000117 PrefixTy = LinkerPrivate;
Rafael Espindola737c9f62014-02-19 17:23:20 +0000118 else
Rafael Espindola0e928842015-06-23 14:11:09 +0000119 PrefixTy = Private;
Rafael Espindola737c9f62014-02-19 17:23:20 +0000120 }
Nico Rieck1b1321f2014-01-14 11:53:26 +0000121
Rafael Espindola9758b4a2015-06-23 13:59:29 +0000122 const DataLayout &DL = GV->getParent()->getDataLayout();
Rafael Espindola916d3122014-01-29 02:30:38 +0000123 if (!GV->hasName()) {
Chris Lattner8a29fa62010-03-12 21:03:47 +0000124 // Get the ID for the global, assigning a new one if we haven't got one
125 // already.
126 unsigned &ID = AnonGlobalIDs[GV];
Rafael Espindola916d3122014-01-29 02:30:38 +0000127 if (ID == 0)
Eric Christopherb660c902016-09-29 02:03:50 +0000128 ID = AnonGlobalIDs.size();
Rafael Espindola916d3122014-01-29 02:30:38 +0000129
Chris Lattner8a29fa62010-03-12 21:03:47 +0000130 // Must mangle the global into a unique ID.
Rafael Espindola0e928842015-06-23 14:11:09 +0000131 getNameWithPrefixImpl(OS, "__unnamed_" + Twine(ID), DL, PrefixTy);
Rafael Espindola916d3122014-01-29 02:30:38 +0000132 return;
Chris Lattner8a29fa62010-03-12 21:03:47 +0000133 }
Rafael Espindola8e0f67d2014-01-03 19:21:54 +0000134
Rafael Espindola916d3122014-01-29 02:30:38 +0000135 StringRef Name = GV->getName();
Rafael Espindola9758b4a2015-06-23 13:59:29 +0000136 char Prefix = DL.getGlobalPrefix();
Rafael Espindola916d3122014-01-29 02:30:38 +0000137
Reid Klecknerd5de3272014-10-28 01:29:26 +0000138 // Mangle functions with Microsoft calling conventions specially. Only do
139 // this mangling for x86_64 vectorcall and 32-bit x86.
140 const Function *MSFunc = dyn_cast<Function>(GV);
Reid Kleckner025ee4f2018-03-16 20:13:32 +0000141
142 // Don't add byte count suffixes when '\01' or '?' are in the first
143 // character.
144 if (Name.startswith("\01") ||
145 (DL.doNotMangleLeadingQuestionMark() && Name.startswith("?")))
146 MSFunc = nullptr;
147
Aaron Ballman7435fa32014-10-28 13:12:13 +0000148 CallingConv::ID CC =
149 MSFunc ? MSFunc->getCallingConv() : (unsigned)CallingConv::C;
Rafael Espindola9758b4a2015-06-23 13:59:29 +0000150 if (!DL.hasMicrosoftFastStdCallMangling() &&
Reid Klecknerd5de3272014-10-28 01:29:26 +0000151 CC != CallingConv::X86_VectorCall)
152 MSFunc = nullptr;
153 if (MSFunc) {
154 if (CC == CallingConv::X86_FastCall)
155 Prefix = '@'; // fastcall functions have an @ prefix instead of _.
156 else if (CC == CallingConv::X86_VectorCall)
157 Prefix = '\0'; // vectorcall functions have no prefix.
Rafael Espindola916d3122014-01-29 02:30:38 +0000158 }
159
Rafael Espindola9758b4a2015-06-23 13:59:29 +0000160 getNameWithPrefixImpl(OS, Name, PrefixTy, DL, Prefix);
Rafael Espindola916d3122014-01-29 02:30:38 +0000161
162 if (!MSFunc)
163 return;
164
Reid Klecknerd5de3272014-10-28 01:29:26 +0000165 // If we are supposed to add a microsoft-style suffix for stdcall, fastcall,
166 // or vectorcall, add it. These functions have a suffix of @N where N is the
167 // cumulative byte size of all of the parameters to the function in decimal.
168 if (CC == CallingConv::X86_VectorCall)
169 OS << '@'; // vectorcall functions use a double @ suffix.
Rafael Espindola916d3122014-01-29 02:30:38 +0000170 FunctionType *FT = MSFunc->getFunctionType();
Reid Klecknerd5de3272014-10-28 01:29:26 +0000171 if (hasByteCountSuffix(CC) &&
Rafael Espindola916d3122014-01-29 02:30:38 +0000172 // "Pure" variadic functions do not receive @0 suffix.
173 (!FT->isVarArg() || FT->getNumParams() == 0 ||
174 (FT->getNumParams() == 1 && MSFunc->hasStructRetAttr())))
Rafael Espindola9758b4a2015-06-23 13:59:29 +0000175 addByteCountSuffix(OS, MSFunc, DL);
Rafael Espindola916d3122014-01-29 02:30:38 +0000176}
177
178void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
Rafael Espindola737c9f62014-02-19 17:23:20 +0000179 const GlobalValue *GV,
David Majnemer61295382015-03-17 20:40:21 +0000180 bool CannotUsePrivateLabel) const {
Rafael Espindola916d3122014-01-29 02:30:38 +0000181 raw_svector_ostream OS(OutName);
David Majnemer61295382015-03-17 20:40:21 +0000182 getNameWithPrefix(OS, GV, CannotUsePrivateLabel);
Chris Lattner5b7dfee2009-09-11 05:40:42 +0000183}
Peter Collingbourne8dc921e2017-03-31 04:46:50 +0000184
185void llvm::emitLinkerFlagsForGlobalCOFF(raw_ostream &OS, const GlobalValue *GV,
186 const Triple &TT, Mangler &Mangler) {
187 if (!GV->hasDLLExportStorageClass() || GV->isDeclaration())
188 return;
189
190 if (TT.isKnownWindowsMSVCEnvironment())
191 OS << " /EXPORT:";
192 else
193 OS << " -export:";
194
195 if (TT.isWindowsGNUEnvironment() || TT.isWindowsCygwinEnvironment()) {
196 std::string Flag;
197 raw_string_ostream FlagOS(Flag);
198 Mangler.getNameWithPrefix(FlagOS, GV, false);
199 FlagOS.flush();
200 if (Flag[0] == GV->getParent()->getDataLayout().getGlobalPrefix())
201 OS << Flag.substr(1);
202 else
203 OS << Flag;
204 } else {
205 Mangler.getNameWithPrefix(OS, GV, false);
206 }
207
208 if (!GV->getValueType()->isFunctionTy()) {
209 if (TT.isKnownWindowsMSVCEnvironment())
210 OS << ",DATA";
211 else
212 OS << ",data";
213 }
214}
Saleem Abdulrasool860652c2018-01-20 00:28:02 +0000215
216void llvm::emitLinkerFlagsForUsedCOFF(raw_ostream &OS, const GlobalValue *GV,
217 const Triple &T, Mangler &M) {
218 if (!T.isKnownWindowsMSVCEnvironment())
219 return;
220
221 OS << " /INCLUDE:";
222 M.getNameWithPrefix(OS, GV, false);
223}
224