1 //===--- Mangle.cpp - Mangle C++ Names --------------------------*- C++ -*-===// 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 // Implements generic name mangling support for blocks and Objective-C. 10 // 11 //===----------------------------------------------------------------------===// 12 #include "clang/AST/Attr.h" 13 #include "clang/AST/ASTContext.h" 14 #include "clang/AST/Decl.h" 15 #include "clang/AST/DeclCXX.h" 16 #include "clang/AST/DeclObjC.h" 17 #include "clang/AST/DeclTemplate.h" 18 #include "clang/AST/ExprCXX.h" 19 #include "clang/AST/Mangle.h" 20 #include "clang/AST/VTableBuilder.h" 21 #include "clang/Basic/ABI.h" 22 #include "clang/Basic/SourceManager.h" 23 #include "clang/Basic/TargetInfo.h" 24 #include "llvm/ADT/StringExtras.h" 25 #include "llvm/IR/DataLayout.h" 26 #include "llvm/IR/Mangler.h" 27 #include "llvm/Support/ErrorHandling.h" 28 #include "llvm/Support/Format.h" 29 #include "llvm/Support/raw_ostream.h" 30 31 using namespace clang; 32 33 // FIXME: For blocks we currently mimic GCC's mangling scheme, which leaves 34 // much to be desired. Come up with a better mangling scheme. 35 36 static void mangleFunctionBlock(MangleContext &Context, 37 StringRef Outer, 38 const BlockDecl *BD, 39 raw_ostream &Out) { 40 unsigned discriminator = Context.getBlockId(BD, true); 41 if (discriminator == 0) 42 Out << "__" << Outer << "_block_invoke"; 43 else 44 Out << "__" << Outer << "_block_invoke_" << discriminator+1; 45 } 46 47 void MangleContext::anchor() { } 48 49 enum CCMangling { 50 CCM_Other, 51 CCM_Fast, 52 CCM_RegCall, 53 CCM_Vector, 54 CCM_Std, 55 CCM_WasmMainArgcArgv 56 }; 57 58 static bool isExternC(const NamedDecl *ND) { 59 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) 60 return FD->isExternC(); 61 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) 62 return VD->isExternC(); 63 return false; 64 } 65 66 static CCMangling getCallingConvMangling(const ASTContext &Context, 67 const NamedDecl *ND) { 68 const TargetInfo &TI = Context.getTargetInfo(); 69 const llvm::Triple &Triple = TI.getTriple(); 70 71 // On wasm, the argc/argv form of "main" is renamed so that the startup code 72 // can call it with the correct function signature. 73 // On Emscripten, users may be exporting "main" and expecting to call it 74 // themselves, so we can't mangle it. 75 if (Triple.isWasm() && !Triple.isOSEmscripten()) 76 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) 77 if (FD->isMain() && FD->hasPrototype() && FD->param_size() == 2) 78 return CCM_WasmMainArgcArgv; 79 80 if (!Triple.isOSWindows() || !Triple.isX86()) 81 return CCM_Other; 82 83 if (Context.getLangOpts().CPlusPlus && !isExternC(ND) && 84 TI.getCXXABI() == TargetCXXABI::Microsoft) 85 return CCM_Other; 86 87 const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND); 88 if (!FD) 89 return CCM_Other; 90 QualType T = FD->getType(); 91 92 const FunctionType *FT = T->castAs<FunctionType>(); 93 94 CallingConv CC = FT->getCallConv(); 95 switch (CC) { 96 default: 97 return CCM_Other; 98 case CC_X86FastCall: 99 return CCM_Fast; 100 case CC_X86StdCall: 101 return CCM_Std; 102 case CC_X86VectorCall: 103 return CCM_Vector; 104 } 105 } 106 107 bool MangleContext::shouldMangleDeclName(const NamedDecl *D) { 108 const ASTContext &ASTContext = getASTContext(); 109 110 CCMangling CC = getCallingConvMangling(ASTContext, D); 111 if (CC != CCM_Other) 112 return true; 113 114 // If the declaration has an owning module for linkage purposes that needs to 115 // be mangled, we must mangle its name. 116 if (!D->hasExternalFormalLinkage() && D->getOwningModuleForLinkage()) 117 return true; 118 119 // C functions with internal linkage have to be mangled with option 120 // -funique-internal-linkage-names. 121 if (!getASTContext().getLangOpts().CPlusPlus && 122 isUniqueInternalLinkageDecl(D)) 123 return true; 124 125 // In C, functions with no attributes never need to be mangled. Fastpath them. 126 if (!getASTContext().getLangOpts().CPlusPlus && !D->hasAttrs()) 127 return false; 128 129 // Any decl can be declared with __asm("foo") on it, and this takes precedence 130 // over all other naming in the .o file. 131 if (D->hasAttr<AsmLabelAttr>()) 132 return true; 133 134 // Declarations that don't have identifier names always need to be mangled. 135 if (isa<MSGuidDecl>(D)) 136 return true; 137 138 return shouldMangleCXXName(D); 139 } 140 141 void MangleContext::mangleName(GlobalDecl GD, raw_ostream &Out) { 142 const NamedDecl *D = cast<NamedDecl>(GD.getDecl()); 143 // Any decl can be declared with __asm("foo") on it, and this takes precedence 144 // over all other naming in the .o file. 145 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) { 146 // If we have an asm name, then we use it as the mangling. 147 148 // If the label isn't literal, or if this is an alias for an LLVM intrinsic, 149 // do not add a "\01" prefix. 150 if (!ALA->getIsLiteralLabel() || ALA->getLabel().startswith("llvm.")) { 151 Out << ALA->getLabel(); 152 return; 153 } 154 155 // Adding the prefix can cause problems when one file has a "foo" and 156 // another has a "\01foo". That is known to happen on ELF with the 157 // tricks normally used for producing aliases (PR9177). Fortunately the 158 // llvm mangler on ELF is a nop, so we can just avoid adding the \01 159 // marker. 160 char GlobalPrefix = 161 getASTContext().getTargetInfo().getDataLayout().getGlobalPrefix(); 162 if (GlobalPrefix) 163 Out << '\01'; // LLVM IR Marker for __asm("foo") 164 165 Out << ALA->getLabel(); 166 return; 167 } 168 169 if (auto *GD = dyn_cast<MSGuidDecl>(D)) 170 return mangleMSGuidDecl(GD, Out); 171 172 const ASTContext &ASTContext = getASTContext(); 173 CCMangling CC = getCallingConvMangling(ASTContext, D); 174 175 if (CC == CCM_WasmMainArgcArgv) { 176 Out << "__main_argc_argv"; 177 return; 178 } 179 180 bool MCXX = shouldMangleCXXName(D); 181 const TargetInfo &TI = Context.getTargetInfo(); 182 if (CC == CCM_Other || (MCXX && TI.getCXXABI() == TargetCXXABI::Microsoft)) { 183 if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) 184 mangleObjCMethodNameAsSourceName(OMD, Out); 185 else 186 mangleCXXName(GD, Out); 187 return; 188 } 189 190 Out << '\01'; 191 if (CC == CCM_Std) 192 Out << '_'; 193 else if (CC == CCM_Fast) 194 Out << '@'; 195 else if (CC == CCM_RegCall) 196 Out << "__regcall3__"; 197 198 if (!MCXX) 199 Out << D->getIdentifier()->getName(); 200 else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) 201 mangleObjCMethodNameAsSourceName(OMD, Out); 202 else 203 mangleCXXName(GD, Out); 204 205 const FunctionDecl *FD = cast<FunctionDecl>(D); 206 const FunctionType *FT = FD->getType()->castAs<FunctionType>(); 207 const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT); 208 if (CC == CCM_Vector) 209 Out << '@'; 210 Out << '@'; 211 if (!Proto) { 212 Out << '0'; 213 return; 214 } 215 assert(!Proto->isVariadic()); 216 unsigned ArgWords = 0; 217 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) 218 if (!MD->isStatic()) 219 ++ArgWords; 220 for (const auto &AT : Proto->param_types()) 221 // Size should be aligned to pointer size. 222 ArgWords += 223 llvm::alignTo(ASTContext.getTypeSize(AT), TI.getPointerWidth(0)) / 224 TI.getPointerWidth(0); 225 Out << ((TI.getPointerWidth(0) / 8) * ArgWords); 226 } 227 228 void MangleContext::mangleMSGuidDecl(const MSGuidDecl *GD, raw_ostream &Out) { 229 // For now, follow the MSVC naming convention for GUID objects on all 230 // targets. 231 MSGuidDecl::Parts P = GD->getParts(); 232 Out << llvm::format("_GUID_%08" PRIx32 "_%04" PRIx32 "_%04" PRIx32 "_", 233 P.Part1, P.Part2, P.Part3); 234 unsigned I = 0; 235 for (uint8_t C : P.Part4And5) { 236 Out << llvm::format("%02" PRIx8, C); 237 if (++I == 2) 238 Out << "_"; 239 } 240 } 241 242 void MangleContext::mangleGlobalBlock(const BlockDecl *BD, 243 const NamedDecl *ID, 244 raw_ostream &Out) { 245 unsigned discriminator = getBlockId(BD, false); 246 if (ID) { 247 if (shouldMangleDeclName(ID)) 248 mangleName(ID, Out); 249 else { 250 Out << ID->getIdentifier()->getName(); 251 } 252 } 253 if (discriminator == 0) 254 Out << "_block_invoke"; 255 else 256 Out << "_block_invoke_" << discriminator+1; 257 } 258 259 void MangleContext::mangleCtorBlock(const CXXConstructorDecl *CD, 260 CXXCtorType CT, const BlockDecl *BD, 261 raw_ostream &ResStream) { 262 SmallString<64> Buffer; 263 llvm::raw_svector_ostream Out(Buffer); 264 mangleName(GlobalDecl(CD, CT), Out); 265 mangleFunctionBlock(*this, Buffer, BD, ResStream); 266 } 267 268 void MangleContext::mangleDtorBlock(const CXXDestructorDecl *DD, 269 CXXDtorType DT, const BlockDecl *BD, 270 raw_ostream &ResStream) { 271 SmallString<64> Buffer; 272 llvm::raw_svector_ostream Out(Buffer); 273 mangleName(GlobalDecl(DD, DT), Out); 274 mangleFunctionBlock(*this, Buffer, BD, ResStream); 275 } 276 277 void MangleContext::mangleBlock(const DeclContext *DC, const BlockDecl *BD, 278 raw_ostream &Out) { 279 assert(!isa<CXXConstructorDecl>(DC) && !isa<CXXDestructorDecl>(DC)); 280 281 SmallString<64> Buffer; 282 llvm::raw_svector_ostream Stream(Buffer); 283 if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC)) { 284 mangleObjCMethodNameAsSourceName(Method, Stream); 285 } else { 286 assert((isa<NamedDecl>(DC) || isa<BlockDecl>(DC)) && 287 "expected a NamedDecl or BlockDecl"); 288 if (isa<BlockDecl>(DC)) 289 for (; DC && isa<BlockDecl>(DC); DC = DC->getParent()) 290 (void) getBlockId(cast<BlockDecl>(DC), true); 291 assert((isa<TranslationUnitDecl>(DC) || isa<NamedDecl>(DC)) && 292 "expected a TranslationUnitDecl or a NamedDecl"); 293 if (const auto *CD = dyn_cast<CXXConstructorDecl>(DC)) 294 mangleCtorBlock(CD, /*CT*/ Ctor_Complete, BD, Out); 295 else if (const auto *DD = dyn_cast<CXXDestructorDecl>(DC)) 296 mangleDtorBlock(DD, /*DT*/ Dtor_Complete, BD, Out); 297 else if (auto ND = dyn_cast<NamedDecl>(DC)) { 298 if (!shouldMangleDeclName(ND) && ND->getIdentifier()) 299 Stream << ND->getIdentifier()->getName(); 300 else { 301 // FIXME: We were doing a mangleUnqualifiedName() before, but that's 302 // a private member of a class that will soon itself be private to the 303 // Itanium C++ ABI object. What should we do now? Right now, I'm just 304 // calling the mangleName() method on the MangleContext; is there a 305 // better way? 306 mangleName(ND, Stream); 307 } 308 } 309 } 310 mangleFunctionBlock(*this, Buffer, BD, Out); 311 } 312 313 void MangleContext::mangleObjCMethodName(const ObjCMethodDecl *MD, 314 raw_ostream &OS, 315 bool includePrefixByte, 316 bool includeCategoryNamespace) { 317 if (getASTContext().getLangOpts().ObjCRuntime.isGNUFamily()) { 318 // This is the mangling we've always used on the GNU runtimes, but it 319 // has obvious collisions in the face of underscores within class 320 // names, category names, and selectors; maybe we should improve it. 321 322 OS << (MD->isClassMethod() ? "_c_" : "_i_") 323 << MD->getClassInterface()->getName() << '_'; 324 325 if (includeCategoryNamespace) { 326 if (auto category = MD->getCategory()) 327 OS << category->getName(); 328 } 329 OS << '_'; 330 331 auto selector = MD->getSelector(); 332 for (unsigned slotIndex = 0, 333 numArgs = selector.getNumArgs(), 334 slotEnd = std::max(numArgs, 1U); 335 slotIndex != slotEnd; ++slotIndex) { 336 if (auto name = selector.getIdentifierInfoForSlot(slotIndex)) 337 OS << name->getName(); 338 339 // Replace all the positions that would've been ':' with '_'. 340 // That's after each slot except that a unary selector doesn't 341 // end in ':'. 342 if (numArgs) 343 OS << '_'; 344 } 345 346 return; 347 } 348 349 // \01+[ContainerName(CategoryName) SelectorName] 350 if (includePrefixByte) { 351 OS << '\01'; 352 } 353 OS << (MD->isInstanceMethod() ? '-' : '+') << '['; 354 if (const auto *CID = MD->getCategory()) { 355 OS << CID->getClassInterface()->getName(); 356 if (includeCategoryNamespace) { 357 OS << '(' << *CID << ')'; 358 } 359 } else if (const auto *CD = 360 dyn_cast<ObjCContainerDecl>(MD->getDeclContext())) { 361 OS << CD->getName(); 362 } else { 363 llvm_unreachable("Unexpected ObjC method decl context"); 364 } 365 OS << ' '; 366 MD->getSelector().print(OS); 367 OS << ']'; 368 } 369 370 void MangleContext::mangleObjCMethodNameAsSourceName(const ObjCMethodDecl *MD, 371 raw_ostream &Out) { 372 SmallString<64> Name; 373 llvm::raw_svector_ostream OS(Name); 374 375 mangleObjCMethodName(MD, OS, /*includePrefixByte=*/false, 376 /*includeCategoryNamespace=*/true); 377 Out << OS.str().size() << OS.str(); 378 } 379 380 class ASTNameGenerator::Implementation { 381 std::unique_ptr<MangleContext> MC; 382 llvm::DataLayout DL; 383 384 public: 385 explicit Implementation(ASTContext &Ctx) 386 : MC(Ctx.createMangleContext()), DL(Ctx.getTargetInfo().getDataLayout()) { 387 } 388 389 bool writeName(const Decl *D, raw_ostream &OS) { 390 // First apply frontend mangling. 391 SmallString<128> FrontendBuf; 392 llvm::raw_svector_ostream FrontendBufOS(FrontendBuf); 393 if (auto *FD = dyn_cast<FunctionDecl>(D)) { 394 if (FD->isDependentContext()) 395 return true; 396 if (writeFuncOrVarName(FD, FrontendBufOS)) 397 return true; 398 } else if (auto *VD = dyn_cast<VarDecl>(D)) { 399 if (writeFuncOrVarName(VD, FrontendBufOS)) 400 return true; 401 } else if (auto *MD = dyn_cast<ObjCMethodDecl>(D)) { 402 MC->mangleObjCMethodName(MD, OS, /*includePrefixByte=*/false, 403 /*includeCategoryNamespace=*/true); 404 return false; 405 } else if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) { 406 writeObjCClassName(ID, FrontendBufOS); 407 } else { 408 return true; 409 } 410 411 // Now apply backend mangling. 412 llvm::Mangler::getNameWithPrefix(OS, FrontendBufOS.str(), DL); 413 return false; 414 } 415 416 std::string getName(const Decl *D) { 417 std::string Name; 418 { 419 llvm::raw_string_ostream OS(Name); 420 writeName(D, OS); 421 } 422 return Name; 423 } 424 425 enum ObjCKind { 426 ObjCClass, 427 ObjCMetaclass, 428 }; 429 430 static StringRef getClassSymbolPrefix(ObjCKind Kind, 431 const ASTContext &Context) { 432 if (Context.getLangOpts().ObjCRuntime.isGNUFamily()) 433 return Kind == ObjCMetaclass ? "_OBJC_METACLASS_" : "_OBJC_CLASS_"; 434 return Kind == ObjCMetaclass ? "OBJC_METACLASS_$_" : "OBJC_CLASS_$_"; 435 } 436 437 std::vector<std::string> getAllManglings(const ObjCContainerDecl *OCD) { 438 StringRef ClassName; 439 if (const auto *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) 440 ClassName = OID->getObjCRuntimeNameAsString(); 441 else if (const auto *OID = dyn_cast<ObjCImplementationDecl>(OCD)) 442 ClassName = OID->getObjCRuntimeNameAsString(); 443 444 if (ClassName.empty()) 445 return {}; 446 447 auto Mangle = [&](ObjCKind Kind, StringRef ClassName) -> std::string { 448 SmallString<40> Mangled; 449 auto Prefix = getClassSymbolPrefix(Kind, OCD->getASTContext()); 450 llvm::Mangler::getNameWithPrefix(Mangled, Prefix + ClassName, DL); 451 return std::string(Mangled.str()); 452 }; 453 454 return { 455 Mangle(ObjCClass, ClassName), 456 Mangle(ObjCMetaclass, ClassName), 457 }; 458 } 459 460 std::vector<std::string> getAllManglings(const Decl *D) { 461 if (const auto *OCD = dyn_cast<ObjCContainerDecl>(D)) 462 return getAllManglings(OCD); 463 464 if (!(isa<CXXRecordDecl>(D) || isa<CXXMethodDecl>(D))) 465 return {}; 466 467 const NamedDecl *ND = cast<NamedDecl>(D); 468 469 ASTContext &Ctx = ND->getASTContext(); 470 std::unique_ptr<MangleContext> M(Ctx.createMangleContext()); 471 472 std::vector<std::string> Manglings; 473 474 auto hasDefaultCXXMethodCC = [](ASTContext &C, const CXXMethodDecl *MD) { 475 auto DefaultCC = C.getDefaultCallingConvention(/*IsVariadic=*/false, 476 /*IsCXXMethod=*/true); 477 auto CC = MD->getType()->castAs<FunctionProtoType>()->getCallConv(); 478 return CC == DefaultCC; 479 }; 480 481 if (const auto *CD = dyn_cast_or_null<CXXConstructorDecl>(ND)) { 482 Manglings.emplace_back(getMangledStructor(CD, Ctor_Base)); 483 484 if (Ctx.getTargetInfo().getCXXABI().isItaniumFamily()) 485 if (!CD->getParent()->isAbstract()) 486 Manglings.emplace_back(getMangledStructor(CD, Ctor_Complete)); 487 488 if (Ctx.getTargetInfo().getCXXABI().isMicrosoft()) 489 if (CD->hasAttr<DLLExportAttr>() && CD->isDefaultConstructor()) 490 if (!(hasDefaultCXXMethodCC(Ctx, CD) && CD->getNumParams() == 0)) 491 Manglings.emplace_back(getMangledStructor(CD, Ctor_DefaultClosure)); 492 } else if (const auto *DD = dyn_cast_or_null<CXXDestructorDecl>(ND)) { 493 Manglings.emplace_back(getMangledStructor(DD, Dtor_Base)); 494 if (Ctx.getTargetInfo().getCXXABI().isItaniumFamily()) { 495 Manglings.emplace_back(getMangledStructor(DD, Dtor_Complete)); 496 if (DD->isVirtual()) 497 Manglings.emplace_back(getMangledStructor(DD, Dtor_Deleting)); 498 } 499 } else if (const auto *MD = dyn_cast_or_null<CXXMethodDecl>(ND)) { 500 Manglings.emplace_back(getName(ND)); 501 if (MD->isVirtual()) 502 if (const auto *TIV = Ctx.getVTableContext()->getThunkInfo(MD)) 503 for (const auto &T : *TIV) 504 Manglings.emplace_back(getMangledThunk(MD, T)); 505 } 506 507 return Manglings; 508 } 509 510 private: 511 bool writeFuncOrVarName(const NamedDecl *D, raw_ostream &OS) { 512 if (MC->shouldMangleDeclName(D)) { 513 GlobalDecl GD; 514 if (const auto *CtorD = dyn_cast<CXXConstructorDecl>(D)) 515 GD = GlobalDecl(CtorD, Ctor_Complete); 516 else if (const auto *DtorD = dyn_cast<CXXDestructorDecl>(D)) 517 GD = GlobalDecl(DtorD, Dtor_Complete); 518 else if (D->hasAttr<CUDAGlobalAttr>()) 519 GD = GlobalDecl(cast<FunctionDecl>(D)); 520 else 521 GD = GlobalDecl(D); 522 MC->mangleName(GD, OS); 523 return false; 524 } else { 525 IdentifierInfo *II = D->getIdentifier(); 526 if (!II) 527 return true; 528 OS << II->getName(); 529 return false; 530 } 531 } 532 533 void writeObjCClassName(const ObjCInterfaceDecl *D, raw_ostream &OS) { 534 OS << getClassSymbolPrefix(ObjCClass, D->getASTContext()); 535 OS << D->getObjCRuntimeNameAsString(); 536 } 537 538 std::string getMangledStructor(const NamedDecl *ND, unsigned StructorType) { 539 std::string FrontendBuf; 540 llvm::raw_string_ostream FOS(FrontendBuf); 541 542 GlobalDecl GD; 543 if (const auto *CD = dyn_cast_or_null<CXXConstructorDecl>(ND)) 544 GD = GlobalDecl(CD, static_cast<CXXCtorType>(StructorType)); 545 else if (const auto *DD = dyn_cast_or_null<CXXDestructorDecl>(ND)) 546 GD = GlobalDecl(DD, static_cast<CXXDtorType>(StructorType)); 547 MC->mangleName(GD, FOS); 548 549 std::string BackendBuf; 550 llvm::raw_string_ostream BOS(BackendBuf); 551 552 llvm::Mangler::getNameWithPrefix(BOS, FOS.str(), DL); 553 554 return BOS.str(); 555 } 556 557 std::string getMangledThunk(const CXXMethodDecl *MD, const ThunkInfo &T) { 558 std::string FrontendBuf; 559 llvm::raw_string_ostream FOS(FrontendBuf); 560 561 MC->mangleThunk(MD, T, FOS); 562 563 std::string BackendBuf; 564 llvm::raw_string_ostream BOS(BackendBuf); 565 566 llvm::Mangler::getNameWithPrefix(BOS, FOS.str(), DL); 567 568 return BOS.str(); 569 } 570 }; 571 572 ASTNameGenerator::ASTNameGenerator(ASTContext &Ctx) 573 : Impl(std::make_unique<Implementation>(Ctx)) {} 574 575 ASTNameGenerator::~ASTNameGenerator() {} 576 577 bool ASTNameGenerator::writeName(const Decl *D, raw_ostream &OS) { 578 return Impl->writeName(D, OS); 579 } 580 581 std::string ASTNameGenerator::getName(const Decl *D) { 582 return Impl->getName(D); 583 } 584 585 std::vector<std::string> ASTNameGenerator::getAllManglings(const Decl *D) { 586 return Impl->getAllManglings(D); 587 } 588