[PATCH] [ms-cxxabi] Mangle dynamic initializer stubs the same way MSVC does

Reid Kleckner rnk at google.com
Thu Aug 22 14:27:41 PDT 2013


Hi timurrrr,

Dynamic initializers are mangled as ??__E <name> YAXXZ.

http://llvm-reviews.chandlerc.com/D1477

Files:
  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: include/clang/AST/Mangle.h
===================================================================
--- include/clang/AST/Mangle.h
+++ include/clang/AST/Mangle.h
@@ -141,6 +141,9 @@
 
   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: lib/AST/ItaniumMangle.cpp
===================================================================
--- lib/AST/ItaniumMangle.cpp
+++ lib/AST/ItaniumMangle.cpp
@@ -153,6 +153,7 @@
                      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 &);
@@ -3706,6 +3707,14 @@
   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: lib/AST/MicrosoftMangle.cpp
===================================================================
--- lib/AST/MicrosoftMangle.cpp
+++ lib/AST/MicrosoftMangle.cpp
@@ -171,8 +171,12 @@
                              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);
 };
 
 }
@@ -1964,17 +1968,29 @@
   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: lib/CodeGen/CGDeclCXX.cpp
===================================================================
--- lib/CodeGen/CGDeclCXX.cpp
+++ lib/CodeGen/CGDeclCXX.cpp
@@ -263,10 +263,15 @@
                                             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: test/CodeGenCXX/microsoft-abi-static-initializers.cpp
===================================================================
--- test/CodeGenCXX/microsoft-abi-static-initializers.cpp
+++ test/CodeGenCXX/microsoft-abi-static-initializers.cpp
@@ -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 at H@@YAXXZ" },
 // CHECK:  { i32, void ()* } { i32 65535, void ()* @_GLOBAL__I_a }]
 
 struct S {
@@ -11,7 +11,7 @@
 
 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 at XZ"
 // CHECK: call i32 @atexit(void ()* @"\01??__Fs@@YAXXZ")
 // CHECK: ret void
@@ -134,7 +134,7 @@
   (void)B<int>::foo;  // (void) - force usage
 }
 
-// CHECK: define internal void [[INIT_foo]]() [[NUW]]
+// CHECK: define internal void @"\01??__Efoo@?$B at H@@YAXXZ"() [[NUW]]
 // CHECK: %{{[.0-9A-Z_a-z]+}} = call x86_thiscallcc %class.A* @"\01??0A@@QAE at XZ"
 // CHECK: call i32 @atexit(void ()* @"\01??__Ffoo@?$B at H@@YAXXZ")
 // CHECK: ret void
@@ -148,7 +148,7 @@
 // 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 }
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1477.1.patch
Type: text/x-patch
Size: 6637 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130822/24a62b99/attachment.bin>


More information about the cfe-commits mailing list