blob: 0d7833a7e8b9e07bfd3aa6f79956d59f5ba29a7e [file] [log] [blame]
Brian Gaeked4dff192003-07-24 20:20:58 +00001//===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Misha Brukmanb1c93172005-04-21 23:48:37 +00006//
John Criswell482202a2003-10-20 19:43:21 +00007//===----------------------------------------------------------------------===//
Brian Gaeked4dff192003-07-24 20:20:58 +00008//
Chris Lattner1376b022010-01-16 21:08:46 +00009// Unified name mangler for assembly backends.
Brian Gaeked4dff192003-07-24 20:20:58 +000010//
11//===----------------------------------------------------------------------===//
12
Rafael Espindola894843c2014-01-07 21:19:40 +000013#include "llvm/IR/Mangler.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000014#include "llvm/ADT/SmallString.h"
Peter Collingbournec66018e2017-03-31 04:46:50 +000015#include "llvm/ADT/Triple.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/ADT/Twine.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#include "llvm/IR/DataLayout.h"
18#include "llvm/IR/DerivedTypes.h"
19#include "llvm/IR/Function.h"
Rafael Espindolac233f742015-06-23 13:59:29 +000020#include "llvm/IR/Module.h"
Chris Lattner8d99c762010-03-12 21:03:47 +000021#include "llvm/Support/raw_ostream.h"
Chris Lattnerc9499b62003-12-14 21:35:53 +000022using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000023
Rafael Espindola8c5f5372015-06-23 14:11:09 +000024namespace {
25enum ManglerPrefixTy {
26 Default, ///< Emit default string before each symbol.
27 Private, ///< Emit "private" prefix before each symbol.
28 LinkerPrivate ///< Emit "linker private" prefix before each symbol.
29};
30}
31
Rafael Espindolace4c2bc2015-06-23 12:21:54 +000032static void getNameWithPrefixImpl(raw_ostream &OS, const Twine &GVName,
Rafael Espindola8c5f5372015-06-23 14:11:09 +000033 ManglerPrefixTy PrefixTy,
Rafael Espindolace4c2bc2015-06-23 12:21:54 +000034 const DataLayout &DL, char Prefix) {
Chris Lattner33535b32010-01-13 07:01:09 +000035 SmallString<256> TmpData;
Benjamin Kramer2e06b932010-01-13 12:45:23 +000036 StringRef Name = GVName.toStringRef(TmpData);
Chris Lattner33535b32010-01-13 07:01:09 +000037 assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
Rafael Espindola58873562014-01-03 19:21:54 +000038
Rafael Espindola5d457de2014-08-01 14:16:40 +000039 // No need to do anything special if the global has the special "do not
40 // mangle" flag in the name.
41 if (Name[0] == '\1') {
42 OS << Name.substr(1);
43 return;
44 }
45
Reid Klecknerf8b51c52018-03-16 20:13:32 +000046 if (DL.doNotMangleLeadingQuestionMark() && Name[0] == '?')
47 Prefix = '\0';
48
Rafael Espindola8c5f5372015-06-23 14:11:09 +000049 if (PrefixTy == Private)
Rafael Espindola310f5012014-01-29 02:30:38 +000050 OS << DL.getPrivateGlobalPrefix();
Rafael Espindola8c5f5372015-06-23 14:11:09 +000051 else if (PrefixTy == LinkerPrivate)
Rafael Espindola310f5012014-01-29 02:30:38 +000052 OS << DL.getLinkerPrivateGlobalPrefix();
Chris Lattnerb4ffc892010-01-17 18:22:35 +000053
Reid Kleckner9ccce992014-10-28 01:29:26 +000054 if (Prefix != '\0')
55 OS << Prefix;
Rafael Espindolafdc88132013-11-13 14:01:59 +000056
Chris Lattner83e872e2010-01-17 19:23:46 +000057 // If this is a simple string that doesn't need escaping, just append it.
Rafael Espindola310f5012014-01-29 02:30:38 +000058 OS << Name;
59}
60
Rafael Espindola8c5f5372015-06-23 14:11:09 +000061static void getNameWithPrefixImpl(raw_ostream &OS, const Twine &GVName,
62 const DataLayout &DL,
63 ManglerPrefixTy PrefixTy) {
Rafael Espindolac233f742015-06-23 13:59:29 +000064 char Prefix = DL.getGlobalPrefix();
65 return getNameWithPrefixImpl(OS, GVName, PrefixTy, DL, Prefix);
Rafael Espindolace4c2bc2015-06-23 12:21:54 +000066}
67
Rafael Espindola8c5f5372015-06-23 14:11:09 +000068void Mangler::getNameWithPrefix(raw_ostream &OS, const Twine &GVName,
69 const DataLayout &DL) {
70 return getNameWithPrefixImpl(OS, GVName, DL, Default);
71}
72
Rafael Espindolace4c2bc2015-06-23 12:21:54 +000073void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
Rafael Espindola8c5f5372015-06-23 14:11:09 +000074 const Twine &GVName, const DataLayout &DL) {
Rafael Espindolace4c2bc2015-06-23 12:21:54 +000075 raw_svector_ostream OS(OutName);
76 char Prefix = DL.getGlobalPrefix();
Rafael Espindola8c5f5372015-06-23 14:11:09 +000077 return getNameWithPrefixImpl(OS, GVName, Default, DL, Prefix);
Chris Lattner33535b32010-01-13 07:01:09 +000078}
79
Reid Kleckner9ccce992014-10-28 01:29:26 +000080static bool hasByteCountSuffix(CallingConv::ID CC) {
81 switch (CC) {
82 case CallingConv::X86_FastCall:
83 case CallingConv::X86_StdCall:
84 case CallingConv::X86_VectorCall:
85 return true;
86 default:
87 return false;
88 }
89}
90
91/// Microsoft fastcall and stdcall functions require a suffix on their name
92/// indicating the number of words of arguments they take.
93static void addByteCountSuffix(raw_ostream &OS, const Function *F,
Mehdi Aminia28d91d2015-03-10 02:37:25 +000094 const DataLayout &DL) {
Chris Lattner8d99c762010-03-12 21:03:47 +000095 // Calculate arguments size total.
96 unsigned ArgWords = 0;
97 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
98 AI != AE; ++AI) {
Chris Lattner229907c2011-07-18 04:54:35 +000099 Type *Ty = AI->getType();
Reid Klecknerf5b76512014-01-31 23:50:57 +0000100 // 'Dereference' type in case of byval or inalloca parameter attribute.
101 if (AI->hasByValOrInAllocaAttr())
Chris Lattner8d99c762010-03-12 21:03:47 +0000102 Ty = cast<PointerType>(Ty)->getElementType();
Reid Kleckner9ccce992014-10-28 01:29:26 +0000103 // Size should be aligned to pointer size.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000104 unsigned PtrSize = DL.getPointerSize();
Rui Ueyamada00f2f2016-01-14 21:06:47 +0000105 ArgWords += alignTo(DL.getTypeAllocSize(Ty), PtrSize);
Chris Lattner8d99c762010-03-12 21:03:47 +0000106 }
Rafael Espindola310f5012014-01-29 02:30:38 +0000107
108 OS << '@' << ArgWords;
Chris Lattner8d99c762010-03-12 21:03:47 +0000109}
110
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000111void Mangler::getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV,
David Majnemer21fecf92015-03-17 20:40:21 +0000112 bool CannotUsePrivateLabel) const {
Rafael Espindola8c5f5372015-06-23 14:11:09 +0000113 ManglerPrefixTy PrefixTy = Default;
David Majnemer21fecf92015-03-17 20:40:21 +0000114 if (GV->hasPrivateLinkage()) {
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000115 if (CannotUsePrivateLabel)
Rafael Espindola8c5f5372015-06-23 14:11:09 +0000116 PrefixTy = LinkerPrivate;
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000117 else
Rafael Espindola8c5f5372015-06-23 14:11:09 +0000118 PrefixTy = Private;
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000119 }
Nico Rieckda881a22014-01-14 11:53:26 +0000120
Rafael Espindolac233f742015-06-23 13:59:29 +0000121 const DataLayout &DL = GV->getParent()->getDataLayout();
Rafael Espindola310f5012014-01-29 02:30:38 +0000122 if (!GV->hasName()) {
Chris Lattner8d99c762010-03-12 21:03:47 +0000123 // Get the ID for the global, assigning a new one if we haven't got one
124 // already.
125 unsigned &ID = AnonGlobalIDs[GV];
Rafael Espindola310f5012014-01-29 02:30:38 +0000126 if (ID == 0)
Eric Christopher1c3bb792016-09-29 02:03:50 +0000127 ID = AnonGlobalIDs.size();
Rafael Espindola310f5012014-01-29 02:30:38 +0000128
Chris Lattner8d99c762010-03-12 21:03:47 +0000129 // Must mangle the global into a unique ID.
Rafael Espindola8c5f5372015-06-23 14:11:09 +0000130 getNameWithPrefixImpl(OS, "__unnamed_" + Twine(ID), DL, PrefixTy);
Rafael Espindola310f5012014-01-29 02:30:38 +0000131 return;
Chris Lattner8d99c762010-03-12 21:03:47 +0000132 }
Rafael Espindola58873562014-01-03 19:21:54 +0000133
Rafael Espindola310f5012014-01-29 02:30:38 +0000134 StringRef Name = GV->getName();
Rafael Espindolac233f742015-06-23 13:59:29 +0000135 char Prefix = DL.getGlobalPrefix();
Rafael Espindola310f5012014-01-29 02:30:38 +0000136
Reid Kleckner9ccce992014-10-28 01:29:26 +0000137 // Mangle functions with Microsoft calling conventions specially. Only do
138 // this mangling for x86_64 vectorcall and 32-bit x86.
139 const Function *MSFunc = dyn_cast<Function>(GV);
Reid Klecknerf8b51c52018-03-16 20:13:32 +0000140
141 // Don't add byte count suffixes when '\01' or '?' are in the first
142 // character.
143 if (Name.startswith("\01") ||
144 (DL.doNotMangleLeadingQuestionMark() && Name.startswith("?")))
145 MSFunc = nullptr;
146
Aaron Ballman5af8ba42014-10-28 13:12:13 +0000147 CallingConv::ID CC =
148 MSFunc ? MSFunc->getCallingConv() : (unsigned)CallingConv::C;
Rafael Espindolac233f742015-06-23 13:59:29 +0000149 if (!DL.hasMicrosoftFastStdCallMangling() &&
Reid Kleckner9ccce992014-10-28 01:29:26 +0000150 CC != CallingConv::X86_VectorCall)
151 MSFunc = nullptr;
152 if (MSFunc) {
153 if (CC == CallingConv::X86_FastCall)
154 Prefix = '@'; // fastcall functions have an @ prefix instead of _.
155 else if (CC == CallingConv::X86_VectorCall)
156 Prefix = '\0'; // vectorcall functions have no prefix.
Rafael Espindola310f5012014-01-29 02:30:38 +0000157 }
158
Rafael Espindolac233f742015-06-23 13:59:29 +0000159 getNameWithPrefixImpl(OS, Name, PrefixTy, DL, Prefix);
Rafael Espindola310f5012014-01-29 02:30:38 +0000160
161 if (!MSFunc)
162 return;
163
Reid Kleckner9ccce992014-10-28 01:29:26 +0000164 // If we are supposed to add a microsoft-style suffix for stdcall, fastcall,
165 // or vectorcall, add it. These functions have a suffix of @N where N is the
166 // cumulative byte size of all of the parameters to the function in decimal.
167 if (CC == CallingConv::X86_VectorCall)
168 OS << '@'; // vectorcall functions use a double @ suffix.
Rafael Espindola310f5012014-01-29 02:30:38 +0000169 FunctionType *FT = MSFunc->getFunctionType();
Reid Kleckner9ccce992014-10-28 01:29:26 +0000170 if (hasByteCountSuffix(CC) &&
Rafael Espindola310f5012014-01-29 02:30:38 +0000171 // "Pure" variadic functions do not receive @0 suffix.
172 (!FT->isVarArg() || FT->getNumParams() == 0 ||
173 (FT->getNumParams() == 1 && MSFunc->hasStructRetAttr())))
Rafael Espindolac233f742015-06-23 13:59:29 +0000174 addByteCountSuffix(OS, MSFunc, DL);
Rafael Espindola310f5012014-01-29 02:30:38 +0000175}
176
177void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000178 const GlobalValue *GV,
David Majnemer21fecf92015-03-17 20:40:21 +0000179 bool CannotUsePrivateLabel) const {
Rafael Espindola310f5012014-01-29 02:30:38 +0000180 raw_svector_ostream OS(OutName);
David Majnemer21fecf92015-03-17 20:40:21 +0000181 getNameWithPrefix(OS, GV, CannotUsePrivateLabel);
Chris Lattner840c8d72009-09-11 05:40:42 +0000182}
Peter Collingbournec66018e2017-03-31 04:46:50 +0000183
184void llvm::emitLinkerFlagsForGlobalCOFF(raw_ostream &OS, const GlobalValue *GV,
185 const Triple &TT, Mangler &Mangler) {
186 if (!GV->hasDLLExportStorageClass() || GV->isDeclaration())
187 return;
188
189 if (TT.isKnownWindowsMSVCEnvironment())
190 OS << " /EXPORT:";
191 else
192 OS << " -export:";
193
194 if (TT.isWindowsGNUEnvironment() || TT.isWindowsCygwinEnvironment()) {
195 std::string Flag;
196 raw_string_ostream FlagOS(Flag);
197 Mangler.getNameWithPrefix(FlagOS, GV, false);
198 FlagOS.flush();
199 if (Flag[0] == GV->getParent()->getDataLayout().getGlobalPrefix())
200 OS << Flag.substr(1);
201 else
202 OS << Flag;
203 } else {
204 Mangler.getNameWithPrefix(OS, GV, false);
205 }
206
207 if (!GV->getValueType()->isFunctionTy()) {
208 if (TT.isKnownWindowsMSVCEnvironment())
209 OS << ",DATA";
210 else
211 OS << ",data";
212 }
213}
Saleem Abdulrasool99f479a2018-01-20 00:28:02 +0000214
215void llvm::emitLinkerFlagsForUsedCOFF(raw_ostream &OS, const GlobalValue *GV,
216 const Triple &T, Mangler &M) {
217 if (!T.isKnownWindowsMSVCEnvironment())
218 return;
219
220 OS << " /INCLUDE:";
221 M.getNameWithPrefix(OS, GV, false);
222}
223