]> granicus.if.org Git - clang/commitdiff
[ms-cxxabi] Mangle dynamic initializer stubs the same way MSVC does
authorReid Kleckner <reid@kleckner.net>
Tue, 10 Sep 2013 20:43:12 +0000 (20:43 +0000)
committerReid Kleckner <reid@kleckner.net>
Tue, 10 Sep 2013 20:43:12 +0000 (20:43 +0000)
Summary: Dynamic initializers are mangled as ??__E <name> YAXXZ.

Reviewers: timurrrr

CC: cfe-commits
Differential Revision: http://llvm-reviews.chandlerc.com/D1477

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@190434 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/AST/Mangle.h
lib/AST/ItaniumMangle.cpp
lib/AST/MicrosoftMangle.cpp
lib/CodeGen/CGDeclCXX.cpp
test/CodeGenCXX/microsoft-abi-static-initializers.cpp

index f6aa2fc9766f379bc78bd6b27c7422075358ba5b..34864fa19296c81c843195455c1b301834993c6f 100644 (file)
@@ -141,6 +141,9 @@ public:
 
   virtual void mangleStaticGuardVariable(const VarDecl *D,
                                          raw_ostream &Out) = 0;
+
+  virtual void mangleDynamicInitializer(const VarDecl *D, raw_ostream &Out) = 0;
+
   virtual void mangleDynamicAtExitDestructor(const VarDecl *D,
                                              raw_ostream &Out) = 0;
 
index 1d8a311c6145c7b9f57b29d4c50013662f661aad..38a6223f788079580af8917f4159bedfefdde877 100644 (file)
@@ -153,6 +153,7 @@ public:
                      raw_ostream &);
 
   void mangleStaticGuardVariable(const VarDecl *D, raw_ostream &);
+  void mangleDynamicInitializer(const VarDecl *D, raw_ostream &Out);
   void mangleDynamicAtExitDestructor(const VarDecl *D, raw_ostream &Out);
   void mangleItaniumThreadLocalInit(const VarDecl *D, raw_ostream &);
   void mangleItaniumThreadLocalWrapper(const VarDecl *D, raw_ostream &);
@@ -3701,6 +3702,14 @@ void ItaniumMangleContext::mangleStaticGuardVariable(const VarDecl *D,
   Mangler.mangleName(D);
 }
 
+void ItaniumMangleContext::mangleDynamicInitializer(const VarDecl *MD,
+                                                    raw_ostream &Out) {
+  // These symbols are internal in the Itanium ABI, so the names don't matter.
+  // Clang has traditionally used this symbol and allowed LLVM to adjust it to
+  // avoid duplicate symbols.
+  Out << "__cxx_global_var_init";
+}
+
 void ItaniumMangleContext::mangleDynamicAtExitDestructor(const VarDecl *D,
                                                          raw_ostream &Out) {
   // Prefix the mangling of D with __dtor_.
index 7e3d6c213953a8c266f74e548566cdea1f102761..3ccc2bddf6dc0cba719c7565b1edd966af9862eb 100644 (file)
@@ -170,8 +170,12 @@ public:
                              raw_ostream &);
   virtual void mangleReferenceTemporary(const VarDecl *, raw_ostream &);
   virtual void mangleStaticGuardVariable(const VarDecl *D, raw_ostream &Out);
+  virtual void mangleDynamicInitializer(const VarDecl *D, raw_ostream &Out);
   virtual void mangleDynamicAtExitDestructor(const VarDecl *D,
                                              raw_ostream &Out);
+
+private:
+  void mangleInitFiniStub(const VarDecl *D, raw_ostream &Out, char CharCode);
 };
 
 }
@@ -1941,17 +1945,29 @@ void MicrosoftMangleContext::mangleStaticGuardVariable(const VarDecl *VD,
   Mangler.getStream() << (Visible ? "@51" : "@4IA");
 }
 
-void MicrosoftMangleContext::mangleDynamicAtExitDestructor(const VarDecl *D,
-                                                           raw_ostream &Out) {
-  // <destructor-name> ::= ?__F <postfix> YAXXZ
+void MicrosoftMangleContext::mangleInitFiniStub(const VarDecl *D,
+                                                raw_ostream &Out,
+                                                char CharCode) {
   MicrosoftCXXNameMangler Mangler(*this, Out);
-  Mangler.getStream() << "\01??__F";
+  Mangler.getStream() << "\01??__" << CharCode;
   Mangler.mangleName(D);
-  // This is the mangling of the function type of the stub, which is a global,
-  // non-variadic, cdecl function that returns void and takes no args.
+  // This is the function class mangling.  These stubs are global, non-variadic,
+  // cdecl functions that return void and take no args.
   Mangler.getStream() << "YAXXZ";
 }
 
+void MicrosoftMangleContext::mangleDynamicInitializer(const VarDecl *D,
+                                                      raw_ostream &Out) {
+  // <initializer-name> ::= ?__E <name> YAXXZ
+  mangleInitFiniStub(D, Out, 'E');
+}
+
+void MicrosoftMangleContext::mangleDynamicAtExitDestructor(const VarDecl *D,
+                                                           raw_ostream &Out) {
+  // <destructor-name> ::= ?__F <name> YAXXZ
+  mangleInitFiniStub(D, Out, 'F');
+}
+
 MangleContext *clang::createMicrosoftMangleContext(ASTContext &Context,
                                                    DiagnosticsEngine &Diags) {
   return new MicrosoftMangleContext(Context, Diags);
index 2417873858b45c47a9258eb32fec9c0f5dd0702a..e3249aa232f4dcf590aa0ae8341b1c6ee7c3e93f 100644 (file)
@@ -259,10 +259,15 @@ CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl *D,
                                             llvm::GlobalVariable *Addr,
                                             bool PerformInit) {
   llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
+  SmallString<256> FnName;
+  {
+    llvm::raw_svector_ostream Out(FnName);
+    getCXXABI().getMangleContext().mangleDynamicInitializer(D, Out);
+  }
 
   // Create a variable initialization function.
   llvm::Function *Fn =
-    CreateGlobalInitOrDestructFunction(*this, FTy, "__cxx_global_var_init");
+      CreateGlobalInitOrDestructFunction(*this, FTy, FnName.str());
 
   CodeGenFunction(*this).GenerateCXXGlobalVarDeclInitFunc(Fn, D, Addr,
                                                           PerformInit);
index 6c4e9e72f4ae1b212e42669dd20e7a8729005587..53d82f60b36846e7d97de47f643a650fb6211f5a 100644 (file)
@@ -1,7 +1,7 @@
 // RUN: %clang_cc1 -emit-llvm %s -o - -cxx-abi microsoft -triple=i386-pc-win32 | FileCheck %s
 
 // CHECK: @llvm.global_ctors = appending global [2 x { i32, void ()* }]
-// CHECK: [{ i32, void ()* } { i32 65535, void ()* [[INIT_foo:@.*global_var.*]] },
+// CHECK: [{ i32, void ()* } { i32 65535, void ()* @"\01??__Efoo@?$B@H@@YAXXZ" },
 // CHECK:  { i32, void ()* } { i32 65535, void ()* @_GLOBAL__I_a }]
 
 struct S {
@@ -11,7 +11,7 @@ struct S {
 
 S s;
 
-// CHECK: define internal void [[INIT_s:@.*global_var.*]] [[NUW:#[0-9]+]]
+// CHECK: define internal void @"\01??__Es@@YAXXZ"() [[NUW:#[0-9]+]]
 // CHECK: %{{[.0-9A-Z_a-z]+}} = call x86_thiscallcc %struct.S* @"\01??0S@@QAE@XZ"
 // CHECK: call i32 @atexit(void ()* @"\01??__Fs@@YAXXZ")
 // CHECK: ret void
@@ -134,7 +134,7 @@ void force_usage() {
   (void)B<int>::foo;  // (void) - force usage
 }
 
-// CHECK: define internal void [[INIT_foo]]() [[NUW]]
+// CHECK: define internal void @"\01??__Efoo@?$B@H@@YAXXZ"() [[NUW]]
 // CHECK: %{{[.0-9A-Z_a-z]+}} = call x86_thiscallcc %class.A* @"\01??0A@@QAE@XZ"
 // CHECK: call i32 @atexit(void ()* @"\01??__Ffoo@?$B@H@@YAXXZ")
 // CHECK: ret void
@@ -148,7 +148,7 @@ void force_usage() {
 // CHECK: ret void
 
 // CHECK: define internal void @_GLOBAL__I_a() [[NUW]] {
-// CHECK: call void [[INIT_s]]
+// CHECK: call void @"\01??__Es@@YAXXZ"()
 // CHECK: ret void
 
 // CHECK: attributes [[NUW]] = { nounwind }