pcc created this revision.
pcc added reviewers: rsmith, rjmccall.
pcc added a subscriber: cfe-commits.

User programs shouldn't be able to observe their addresses without invoking
undefined behavior. This will be needed to create relative references
to runtime functions __cxa_pure_virtual and __cxa_deleted_virtual in the
relative vtable ABI.

http://reviews.llvm.org/D18071

Files:
  lib/CodeGen/CodeGenModule.cpp
  test/CodeGenCXX/dynamic-cast.cpp
  test/CodeGenCXX/exceptions.cpp
  test/CodeGenCXX/runtimecc.cpp

Index: test/CodeGenCXX/runtimecc.cpp
===================================================================
--- test/CodeGenCXX/runtimecc.cpp
+++ test/CodeGenCXX/runtimecc.cpp
@@ -26,7 +26,7 @@
 // CHECK-NEXT: ret void
 }
 
-// CHECK: declare i32 @__cxa_atexit(void (i8*)*, i8*, i8*) [[NOUNWIND]]
+// CHECK: declare i32 @__cxa_atexit(void (i8*)*, i8*, i8*) unnamed_addr 
[[NOUNWIND]]
 
 namespace test1 {
   void test() {
Index: test/CodeGenCXX/exceptions.cpp
===================================================================
--- test/CodeGenCXX/exceptions.cpp
+++ test/CodeGenCXX/exceptions.cpp
@@ -74,7 +74,7 @@
 
   //   rdar://11904428
   //   Terminate landing pads should call __cxa_begin_catch first.
-  // CHECK:      define linkonce_odr hidden void @__clang_call_terminate(i8*) 
[[NI_NR_NUW:#[0-9]+]] comdat
+  // CHECK:      define linkonce_odr hidden void @__clang_call_terminate(i8*) 
unnamed_addr [[NI_NR_NUW:#[0-9]+]] comdat
   // CHECK-NEXT:   [[T0:%.*]] = call i8* @__cxa_begin_catch(i8* %0) 
[[NUW:#[0-9]+]]
   // CHECK-NEXT:   call void @_ZSt9terminatev() [[NR_NUW:#[0-9]+]]
   // CHECK-NEXT:   unreachable
Index: test/CodeGenCXX/dynamic-cast.cpp
===================================================================
--- test/CodeGenCXX/dynamic-cast.cpp
+++ test/CodeGenCXX/dynamic-cast.cpp
@@ -18,7 +18,7 @@
   return fail;
 }
 
-// CHECK: declare i8* @__dynamic_cast(i8*, i8*, i8*, i64) [[NUW_RO:#[0-9]+]]
+// CHECK: declare i8* @__dynamic_cast(i8*, i8*, i8*, i64) unnamed_addr 
[[NUW_RO:#[0-9]+]]
 
 // CHECK: attributes [[NUW_RO]] = { nounwind readonly }
 // CHECK: attributes [[NR]] = { noreturn }
Index: lib/CodeGen/CodeGenModule.cpp
===================================================================
--- lib/CodeGen/CodeGenModule.cpp
+++ lib/CodeGen/CodeGenModule.cpp
@@ -1983,9 +1983,15 @@
   llvm::Constant *C =
       GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(), /*ForVTable=*/false,
                               /*DontDefer=*/false, /*IsThunk=*/false, 
ExtraAttrs);
-  if (auto *F = dyn_cast<llvm::Function>(C))
+  if (auto *F = dyn_cast<llvm::Function>(C)) {
+    // Mark runtime functions with reserved names as unnamed_addr, as user
+    // programs shouldn't be able to observe their addresses without invoking
+    // undefined behavior.
+    if (Name[0] == '_')
+      F->setUnnamedAddr(true);
     if (F->empty())
       F->setCallingConv(getRuntimeCC());
+  }
   return C;
 }
 


Index: test/CodeGenCXX/runtimecc.cpp
===================================================================
--- test/CodeGenCXX/runtimecc.cpp
+++ test/CodeGenCXX/runtimecc.cpp
@@ -26,7 +26,7 @@
 // CHECK-NEXT: ret void
 }
 
-// CHECK: declare i32 @__cxa_atexit(void (i8*)*, i8*, i8*) [[NOUNWIND]]
+// CHECK: declare i32 @__cxa_atexit(void (i8*)*, i8*, i8*) unnamed_addr [[NOUNWIND]]
 
 namespace test1 {
   void test() {
Index: test/CodeGenCXX/exceptions.cpp
===================================================================
--- test/CodeGenCXX/exceptions.cpp
+++ test/CodeGenCXX/exceptions.cpp
@@ -74,7 +74,7 @@
 
   //   rdar://11904428
   //   Terminate landing pads should call __cxa_begin_catch first.
-  // CHECK:      define linkonce_odr hidden void @__clang_call_terminate(i8*) [[NI_NR_NUW:#[0-9]+]] comdat
+  // CHECK:      define linkonce_odr hidden void @__clang_call_terminate(i8*) unnamed_addr [[NI_NR_NUW:#[0-9]+]] comdat
   // CHECK-NEXT:   [[T0:%.*]] = call i8* @__cxa_begin_catch(i8* %0) [[NUW:#[0-9]+]]
   // CHECK-NEXT:   call void @_ZSt9terminatev() [[NR_NUW:#[0-9]+]]
   // CHECK-NEXT:   unreachable
Index: test/CodeGenCXX/dynamic-cast.cpp
===================================================================
--- test/CodeGenCXX/dynamic-cast.cpp
+++ test/CodeGenCXX/dynamic-cast.cpp
@@ -18,7 +18,7 @@
   return fail;
 }
 
-// CHECK: declare i8* @__dynamic_cast(i8*, i8*, i8*, i64) [[NUW_RO:#[0-9]+]]
+// CHECK: declare i8* @__dynamic_cast(i8*, i8*, i8*, i64) unnamed_addr [[NUW_RO:#[0-9]+]]
 
 // CHECK: attributes [[NUW_RO]] = { nounwind readonly }
 // CHECK: attributes [[NR]] = { noreturn }
Index: lib/CodeGen/CodeGenModule.cpp
===================================================================
--- lib/CodeGen/CodeGenModule.cpp
+++ lib/CodeGen/CodeGenModule.cpp
@@ -1983,9 +1983,15 @@
   llvm::Constant *C =
       GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(), /*ForVTable=*/false,
                               /*DontDefer=*/false, /*IsThunk=*/false, ExtraAttrs);
-  if (auto *F = dyn_cast<llvm::Function>(C))
+  if (auto *F = dyn_cast<llvm::Function>(C)) {
+    // Mark runtime functions with reserved names as unnamed_addr, as user
+    // programs shouldn't be able to observe their addresses without invoking
+    // undefined behavior.
+    if (Name[0] == '_')
+      F->setUnnamedAddr(true);
     if (F->empty())
       F->setCallingConv(getRuntimeCC());
+  }
   return C;
 }
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to