Mangler.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. #include "llvm/IR/Mangler.h"
  13. #include "llvm/ADT/SmallString.h"
  14. #include "llvm/ADT/Triple.h"
  15. #include "llvm/ADT/Twine.h"
  16. #include "llvm/IR/DataLayout.h"
  17. #include "llvm/IR/DerivedTypes.h"
  18. #include "llvm/IR/Function.h"
  19. #include "llvm/IR/Module.h"
  20. #include "llvm/Support/raw_ostream.h"
  21. using namespace llvm;
  22. namespace {
  23. enum ManglerPrefixTy {
  24. Default, ///< Emit default string before each symbol.
  25. Private, ///< Emit "private" prefix before each symbol.
  26. LinkerPrivate ///< Emit "linker private" prefix before each symbol.
  27. };
  28. }
  29. static void getNameWithPrefixImpl(raw_ostream &OS, const Twine &GVName,
  30. ManglerPrefixTy PrefixTy,
  31. const DataLayout &DL, char Prefix) {
  32. SmallString<256> TmpData;
  33. StringRef Name = GVName.toStringRef(TmpData);
  34. assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
  35. // No need to do anything special if the global has the special "do not
  36. // mangle" flag in the name.
  37. if (Name[0] == '\1') {
  38. OS << Name.substr(1);
  39. return;
  40. }
  41. if (DL.doNotMangleLeadingQuestionMark() && Name[0] == '?')
  42. Prefix = '\0';
  43. if (PrefixTy == Private)
  44. OS << DL.getPrivateGlobalPrefix();
  45. else if (PrefixTy == LinkerPrivate)
  46. OS << DL.getLinkerPrivateGlobalPrefix();
  47. if (Prefix != '\0')
  48. OS << Prefix;
  49. // If this is a simple string that doesn't need escaping, just append it.
  50. OS << Name;
  51. }
  52. static void getNameWithPrefixImpl(raw_ostream &OS, const Twine &GVName,
  53. const DataLayout &DL,
  54. ManglerPrefixTy PrefixTy) {
  55. char Prefix = DL.getGlobalPrefix();
  56. return getNameWithPrefixImpl(OS, GVName, PrefixTy, DL, Prefix);
  57. }
  58. void Mangler::getNameWithPrefix(raw_ostream &OS, const Twine &GVName,
  59. const DataLayout &DL) {
  60. return getNameWithPrefixImpl(OS, GVName, DL, Default);
  61. }
  62. void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
  63. const Twine &GVName, const DataLayout &DL) {
  64. raw_svector_ostream OS(OutName);
  65. char Prefix = DL.getGlobalPrefix();
  66. return getNameWithPrefixImpl(OS, GVName, Default, DL, Prefix);
  67. }
  68. static bool hasByteCountSuffix(CallingConv::ID CC) {
  69. switch (CC) {
  70. case CallingConv::X86_FastCall:
  71. case CallingConv::X86_StdCall:
  72. case CallingConv::X86_VectorCall:
  73. return true;
  74. default:
  75. return false;
  76. }
  77. }
  78. /// Microsoft fastcall and stdcall functions require a suffix on their name
  79. /// indicating the number of words of arguments they take.
  80. static void addByteCountSuffix(raw_ostream &OS, const Function *F,
  81. const DataLayout &DL) {
  82. // Calculate arguments size total.
  83. unsigned ArgWords = 0;
  84. for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
  85. AI != AE; ++AI) {
  86. Type *Ty = AI->getType();
  87. // 'Dereference' type in case of byval or inalloca parameter attribute.
  88. if (AI->hasByValOrInAllocaAttr())
  89. Ty = cast<PointerType>(Ty)->getElementType();
  90. // Size should be aligned to pointer size.
  91. unsigned PtrSize = DL.getPointerSize();
  92. ArgWords += alignTo(DL.getTypeAllocSize(Ty), PtrSize);
  93. }
  94. OS << '@' << ArgWords;
  95. }
  96. void Mangler::getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV,
  97. bool CannotUsePrivateLabel) const {
  98. ManglerPrefixTy PrefixTy = Default;
  99. if (GV->hasPrivateLinkage()) {
  100. if (CannotUsePrivateLabel)
  101. PrefixTy = LinkerPrivate;
  102. else
  103. PrefixTy = Private;
  104. }
  105. const DataLayout &DL = GV->getParent()->getDataLayout();
  106. if (!GV->hasName()) {
  107. // Get the ID for the global, assigning a new one if we haven't got one
  108. // already.
  109. unsigned &ID = AnonGlobalIDs[GV];
  110. if (ID == 0)
  111. ID = AnonGlobalIDs.size();
  112. // Must mangle the global into a unique ID.
  113. getNameWithPrefixImpl(OS, "__unnamed_" + Twine(ID), DL, PrefixTy);
  114. return;
  115. }
  116. StringRef Name = GV->getName();
  117. char Prefix = DL.getGlobalPrefix();
  118. // Mangle functions with Microsoft calling conventions specially. Only do
  119. // this mangling for x86_64 vectorcall and 32-bit x86.
  120. const Function *MSFunc = dyn_cast<Function>(GV);
  121. // Don't add byte count suffixes when '\01' or '?' are in the first
  122. // character.
  123. if (Name.startswith("\01") ||
  124. (DL.doNotMangleLeadingQuestionMark() && Name.startswith("?")))
  125. MSFunc = nullptr;
  126. CallingConv::ID CC =
  127. MSFunc ? MSFunc->getCallingConv() : (unsigned)CallingConv::C;
  128. if (!DL.hasMicrosoftFastStdCallMangling() &&
  129. CC != CallingConv::X86_VectorCall)
  130. MSFunc = nullptr;
  131. if (MSFunc) {
  132. if (CC == CallingConv::X86_FastCall)
  133. Prefix = '@'; // fastcall functions have an @ prefix instead of _.
  134. else if (CC == CallingConv::X86_VectorCall)
  135. Prefix = '\0'; // vectorcall functions have no prefix.
  136. }
  137. getNameWithPrefixImpl(OS, Name, PrefixTy, DL, Prefix);
  138. if (!MSFunc)
  139. return;
  140. // If we are supposed to add a microsoft-style suffix for stdcall, fastcall,
  141. // or vectorcall, add it. These functions have a suffix of @N where N is the
  142. // cumulative byte size of all of the parameters to the function in decimal.
  143. if (CC == CallingConv::X86_VectorCall)
  144. OS << '@'; // vectorcall functions use a double @ suffix.
  145. FunctionType *FT = MSFunc->getFunctionType();
  146. if (hasByteCountSuffix(CC) &&
  147. // "Pure" variadic functions do not receive @0 suffix.
  148. (!FT->isVarArg() || FT->getNumParams() == 0 ||
  149. (FT->getNumParams() == 1 && MSFunc->hasStructRetAttr())))
  150. addByteCountSuffix(OS, MSFunc, DL);
  151. }
  152. void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
  153. const GlobalValue *GV,
  154. bool CannotUsePrivateLabel) const {
  155. raw_svector_ostream OS(OutName);
  156. getNameWithPrefix(OS, GV, CannotUsePrivateLabel);
  157. }
  158. void llvm::emitLinkerFlagsForGlobalCOFF(raw_ostream &OS, const GlobalValue *GV,
  159. const Triple &TT, Mangler &Mangler) {
  160. if (!GV->hasDLLExportStorageClass() || GV->isDeclaration())
  161. return;
  162. if (TT.isWindowsMSVCEnvironment())
  163. OS << " /EXPORT:";
  164. else
  165. OS << " -export:";
  166. if (TT.isWindowsGNUEnvironment() || TT.isWindowsCygwinEnvironment()) {
  167. std::string Flag;
  168. raw_string_ostream FlagOS(Flag);
  169. Mangler.getNameWithPrefix(FlagOS, GV, false);
  170. FlagOS.flush();
  171. if (Flag[0] == GV->getParent()->getDataLayout().getGlobalPrefix())
  172. OS << Flag.substr(1);
  173. else
  174. OS << Flag;
  175. } else {
  176. Mangler.getNameWithPrefix(OS, GV, false);
  177. }
  178. if (!GV->getValueType()->isFunctionTy()) {
  179. if (TT.isWindowsMSVCEnvironment())
  180. OS << ",DATA";
  181. else
  182. OS << ",data";
  183. }
  184. }
  185. void llvm::emitLinkerFlagsForUsedCOFF(raw_ostream &OS, const GlobalValue *GV,
  186. const Triple &T, Mangler &M) {
  187. if (!T.isWindowsMSVCEnvironment())
  188. return;
  189. OS << " /INCLUDE:";
  190. M.getNameWithPrefix(OS, GV, false);
  191. }