[(questionable but useful) patch] Let codegen write the filename for translation unit initializer functions

Nico Weber thakis at chromium.org
Mon May 5 22:11:12 PDT 2014


Here's a patch which replaces everything not in [a-zA-Z0-9._] with _.
It also adds a "sub_" to the symbol, because a) that's what gcc does
(with -fPIC) b) that way, constructors with priority are
lexicographically in front of constructors without priority.

John, do you think this is ok? This patch adds a filename to global
constructor symbols without priority, i.e. instead of always being
called _GLOBAL__I_a they're now called _GLOBAL__sub_I_my_file.cc. I'm
asking because there are tests for _destructors_ for -fapple-kext, so
maybe kexts care about these symbol names in some way.

On Mon, May 5, 2014 at 4:13 PM, Reid Kleckner <rnk at google.com> wrote:
> Users of ASan's init order checker are ending up confused by _GLOBAL__I_a,
> so I guess I'd like to see this go in.  Feel free to commit with some
> sanitization of the filename.
>
> On Mon, Apr 28, 2014 at 12:26 PM, Nico Weber <thakis at chromium.org> wrote:
>>
>> On Fri, Apr 25, 2014 at 4:33 PM, Reid Kleckner <rnk at google.com> wrote:
>>>
>>> This might give some assemblers heartburn.  Does gcc sanitize the
>>> filename in any way?
>>
>>
>> gcc apparently replaces spaces, '-, '+' etc with '_', so it does some
>> sanitizing. ("test$2.cc" ends up being just "test.cc", so their sanitizing
>> isn't complete.)
>>
>> Does LLVM have a helper function that replaces possibly troubling
>> characters with '_' characters?
>
>
> Not that I know of.
>
>>>
>>> What happens if the main source file is stdin?
>>
>>
>> gcc produces "_GLOBAL__sub_I_" (i.e. "nothing" for stdin). (My patch
>> currently adds a "<stdin>", which seems suboptimal :-) ).
>>
>> gcc apparently only does this in -fPIC mode too, not sure why.
>>
>>>
>>>
>>> On Fri, Apr 25, 2014 at 4:17 PM, Nico Weber <thakis at chromium.org> wrote:
>>>>
>>>> Hi,
>>>>
>>>> gcc calls the static initialization function for file foo.cc
>>>> _GLOBAL__sub_i_foo.cc, while clang always uses _GLOBAL__I_a . Having the
>>>> filename in the symbol is useful for finding where static initializers are
>>>> from; we use this information in chromium on linux. (On Mac, we look at the
>>>> dSYM file instead.)
>>>>
>>>> The attached patch makes clang behave like gcc for initializers. (It
>>>> doesn't change the behavior for __D_ functions, not sure if anyone wants
>>>> that.)
>>>>
>>>> Opinions? Is this useful for anyone else?
>>>>
>>>> (Drawbacks: slightly longer symbols, which has a (small) binary size
>>>> cost, and it looks like __attribute__ ((init_priority(101))) works by naming
>>>> the _GLOBAL__ functions _GLOBAL__I_000101 etc, so if you call your source
>>>> file 000101.cc it might interfere with this patch.
>>>>
>>>> Nico
>>>>
>>>> _______________________________________________
>>>> cfe-commits mailing list
>>>> cfe-commits at cs.uiuc.edu
>>>> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
>>>>
>>>
>>
>
-------------- next part --------------
Index: lib/CodeGen/CGDeclCXX.cpp
===================================================================
--- lib/CodeGen/CGDeclCXX.cpp	(revision 207920)
+++ lib/CodeGen/CGDeclCXX.cpp	(working copy)
@@ -17,6 +17,7 @@
 #include "clang/Frontend/CodeGenOptions.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/IR/Intrinsics.h"
+#include "llvm/Support/Path.h"
 
 using namespace clang;
 using namespace CodeGen;
@@ -362,7 +363,7 @@
       // Compute the function suffix from priority. Prepend with zeroes to make
       // sure the function names are also ordered as priorities.
       std::string PrioritySuffix = llvm::utostr(Priority);
-      // Priority is always <= 65535 (enforced by sema)..
+      // Priority is always <= 65535 (enforced by sema).
       PrioritySuffix = std::string(6-PrioritySuffix.size(), '0')+PrioritySuffix;
       llvm::Function *Fn = 
         CreateGlobalInitOrDestructFunction(*this, FTy,
@@ -376,8 +377,20 @@
     }
   }
   
-  llvm::Function *Fn = 
-    CreateGlobalInitOrDestructFunction(*this, FTy, "_GLOBAL__I_a");
+  // Include the filename in the symbol name. Including "sub_" matches gcc and
+  // makes sure these symbols appear lexicographically behind the symbols with
+  // priority emitted above.
+  SourceManager &SM = Context.getSourceManager();
+  SmallString<128> FileName(llvm::sys::path::filename(
+      SM.getFileEntryForID(SM.getMainFileID())->getName()));
+  for (size_t i = 0; i < FileName.size(); ++i) {
+    // Replace everything that's not [a-zA-Z0-9._] with a _. This set happens
+    // to be the set of C preprocessing numbers.
+    if (!isPreprocessingNumberBody(FileName[i]))
+      FileName[i] = '_';
+  }
+  llvm::Function *Fn = CreateGlobalInitOrDestructFunction(
+      *this, FTy, llvm::Twine("_GLOBAL__sub_I_", FileName));
 
   CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, CXXGlobalInits);
   AddGlobalCtor(Fn);
Index: test/CodeGenCXX/deferred-global-init.cpp
===================================================================
--- test/CodeGenCXX/deferred-global-init.cpp	(revision 207920)
+++ test/CodeGenCXX/deferred-global-init.cpp	(working copy)
@@ -11,6 +11,6 @@
 // CHECK: load i8** @foo
 // CHECK: ret void
 
-// CHECK-LABEL: define internal void @_GLOBAL__I_a
+// CHECK-LABEL: define internal void @_GLOBAL__sub_I_deferred_global_init.cpp
 // CHECK: call void @__cxx_global_var_init()
 // CHECK: ret void
Index: test/CodeGenCXX/global-init.cpp
===================================================================
--- test/CodeGenCXX/global-init.cpp	(revision 207920)
+++ test/CodeGenCXX/global-init.cpp	(working copy)
@@ -195,11 +195,11 @@
 // CHECK-NEXT:   sub
 // CHECK-NEXT:   store i32 {{.*}}, i32* @_ZN5test1L1yE
 
-// CHECK: define internal void @_GLOBAL__I_a() section "__TEXT,__StaticInit,regular,pure_instructions" {
+// CHECK: define internal void @_GLOBAL__sub_I_global_init.cpp() section "__TEXT,__StaticInit,regular,pure_instructions" {
 // CHECK:   call void [[TEST1_Y_INIT]]
 // CHECK:   call void [[TEST1_Z_INIT]]
 
 // rdar://problem/8090834: this should be nounwind
-// CHECK-NOEXC: define internal void @_GLOBAL__I_a() [[NUW:#[0-9]+]] section "__TEXT,__StaticInit,regular,pure_instructions" {
+// CHECK-NOEXC: define internal void @_GLOBAL__sub_I_global_init.cpp() [[NUW:#[0-9]+]] section "__TEXT,__StaticInit,regular,pure_instructions" {
 
 // CHECK-NOEXC: attributes [[NUW]] = { nounwind }
Index: test/CodeGenCXX/globalinit-loc.cpp
===================================================================
--- test/CodeGenCXX/globalinit-loc.cpp	(revision 207920)
+++ test/CodeGenCXX/globalinit-loc.cpp	(working copy)
@@ -4,9 +4,9 @@
 // Verify that the global init helper function does not get associated
 // with any source location.
 //
-// CHECK: define internal void @_GLOBAL__I_a
+// CHECK: define internal void @_GLOBAL__sub_I_globalinit_loc.cpp
 // CHECK: !dbg ![[DBG:.*]]
-// CHECK: "_GLOBAL__I_a", i32 0, {{.*}}, i32 0} ; [ DW_TAG_subprogram ] [line 0] [local] [def]
+// CHECK: "_GLOBAL__sub_I_globalinit_loc.cpp", i32 0, {{.*}}, i32 0} ; [ DW_TAG_subprogram ] [line 0] [local] [def]
 // CHECK: ![[DBG]] = metadata !{i32 0, i32 0,
 # 99 "someheader.h"
 class A {
Index: test/CodeGenCXX/init-priority-attr.cpp
===================================================================
--- test/CodeGenCXX/init-priority-attr.cpp	(revision 207920)
+++ test/CodeGenCXX/init-priority-attr.cpp	(working copy)
@@ -27,7 +27,7 @@
 
 A C::a = A();
 
-// CHECK: @llvm.global_ctors = appending global [3 x { i32, void ()* }] [{ i32, void ()* } { i32 200, void ()* @_GLOBAL__I_000200 }, { i32, void ()* } { i32 300, void ()* @_GLOBAL__I_000300 }, { i32, void ()* } { i32 65535, void ()* @_GLOBAL__I_a }]
+// CHECK: @llvm.global_ctors = appending global [3 x { i32, void ()* }] [{ i32, void ()* } { i32 200, void ()* @_GLOBAL__I_000200 }, { i32, void ()* } { i32 300, void ()* @_GLOBAL__I_000300 }, { i32, void ()* } { i32 65535, void ()* @_GLOBAL__sub_I_init_priority_attr.cpp }]
 
 // CHECK: _GLOBAL__I_000200()
 // CHECK: _Z3fooi(i32 3)
@@ -38,7 +38,7 @@
 // CHECK-NEXT: _Z3fooi(i32 1)
 // CHECK-NEXT: ret void
 
-// CHECK: _GLOBAL__I_a()
+// CHECK: _GLOBAL__sub_I_init_priority_attr.cpp()
 // CHECK: _Z3fooi(i32 1)
 // CHECK-NEXT: _Z3fooi(i32 4)
 // CHECK-NEXT: ret void
Index: test/CodeGenCXX/microsoft-abi-static-initializers.cpp
===================================================================
--- test/CodeGenCXX/microsoft-abi-static-initializers.cpp	(revision 207920)
+++ test/CodeGenCXX/microsoft-abi-static-initializers.cpp	(working copy)
@@ -2,7 +2,7 @@
 
 // CHECK: @llvm.global_ctors = appending global [2 x { i32, void ()* }]
 // CHECK: [{ i32, void ()* } { i32 65535, void ()* @"\01??__Efoo@?$B at H@@2VA@@A at YAXXZ"
-// CHECK:  { i32, void ()* } { i32 65535, void ()* @_GLOBAL__I_a }]
+// CHECK:  { i32, void ()* } { i32 65535, void ()* @_GLOBAL__sub_I_microsoft_abi_static_initializers.cpp }]
 
 struct S {
   S();
@@ -160,7 +160,7 @@
 // CHECK: call x86_thiscallcc void @"\01??1A@@QAE at XZ"{{.*}}foo
 // CHECK: ret void
 
-// CHECK: define internal void @_GLOBAL__I_a() [[NUW]] {
+// CHECK: define internal void @_GLOBAL__sub_I_microsoft_abi_static_initializers.cpp() [[NUW]] {
 // CHECK: call void @"\01??__Es@@YAXXZ"()
 // CHECK: ret void
 
Index: test/CodeGenCXX/runtimecc.cpp
===================================================================
--- test/CodeGenCXX/runtimecc.cpp	(revision 207920)
+++ test/CodeGenCXX/runtimecc.cpp	(working copy)
@@ -45,7 +45,7 @@
 
 // CHECK: declare arm_aapcscc void @__cxa_throw(i8*, i8*, i8*)
 
-// CHECK-LABEL: define internal arm_aapcscc void @_GLOBAL__I_a()
+// CHECK-LABEL: define internal arm_aapcscc void @_GLOBAL__sub_I_runtimecc.cpp()
 // CHECK:   call arm_aapcscc void @__cxx_global_var_init()
 
 
Index: test/CodeGenCXX/static-member-variable-explicit-specialization.cpp
===================================================================
--- test/CodeGenCXX/static-member-variable-explicit-specialization.cpp	(revision 207920)
+++ test/CodeGenCXX/static-member-variable-explicit-specialization.cpp	(working copy)
@@ -19,14 +19,14 @@
 // CHECK:  { i32, void ()* } { i32 65535, void ()* @[[unordered4:[^ ]*]] },
 // CHECK:  { i32, void ()* } { i32 65535, void ()* @[[unordered5:[^ ]*]] },
 // CHECK:  { i32, void ()* } { i32 65535, void ()* @[[unordered6:[^ ]*]] },
-// CHECK:  { i32, void ()* } { i32 65535, void ()* @_GLOBAL__I_a }]
+// CHECK:  { i32, void ()* } { i32 65535, void ()* @_GLOBAL__sub_I_static_member_variable_explicit_specialization.cpp }]
 
 template int A<short>::a;  // Unordered
 int b = foo();
 int c = foo();
 int d = A<void>::a; // Unordered
 
-// An explicit specialization is ordered, and goes in __GLOBAL_I_a.
+// An explicit specialization is ordered, and goes in __GLOBAL_sub_I_static_member_variable_explicit_specialization.cpp.
 template<> struct A<int> { static int a; };
 int A<int>::a = foo();
 
@@ -82,7 +82,7 @@
 // CHECK: store {{.*}} @_Z1xIcE
 // CHECK: ret
 
-// CHECK: define internal void @_GLOBAL__I_a()
+// CHECK: define internal void @_GLOBAL__sub_I_static_member_variable_explicit_specialization.cpp()
 //   We call unique stubs for every ordered dynamic initializer in the TU.
 // CHECK: call
 // CHECK: call
Index: test/CodeGenObjCXX/arc-globals.mm
===================================================================
--- test/CodeGenObjCXX/arc-globals.mm	(revision 207920)
+++ test/CodeGenObjCXX/arc-globals.mm	(working copy)
@@ -19,7 +19,7 @@
 // CHECK-NEXT: ret void
 id global_obj2 = getObject();
 
-// CHECK-LABEL: define internal void @_GLOBAL__I_a
+// CHECK-LABEL: define internal void @_GLOBAL__sub_I_arc_globals.mm
 // CHECK: call i8* @objc_autoreleasePoolPush()
 // CHECK-NEXT: call void @__cxx_global_var_init
 // CHECK-NEXT: call void @__cxx_global_var_init1


More information about the cfe-commits mailing list