[llvm-branch-commits] [clang] 38a716c - Make -fno-pic respect -fno-direct-access-external-data

Fangrui Song via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Sat Jan 9 00:36:41 PST 2021


Author: Fangrui Song
Date: 2021-01-09T00:32:02-08:00
New Revision: 38a716c30f095c7d7148482070ea4a3352b926d5

URL: https://github.com/llvm/llvm-project/commit/38a716c30f095c7d7148482070ea4a3352b926d5
DIFF: https://github.com/llvm/llvm-project/commit/38a716c30f095c7d7148482070ea4a3352b926d5.diff

LOG: Make -fno-pic respect -fno-direct-access-external-data

D92633 added -f[no-]direct-access-external-data to supersede -m[no-]pie-copy-relocations.
(The option works for -fpie but is a no-op for -fno-pic and -fpic.)

This patch makes -fno-pic -fno-direct-access-external-data drop dso_local from
global variable declarations. This usually causes the backend to emit a GOT
indirection for external data access. With a GOT relocation, the subsequent
-no-pie link will not have copy relocation even if the data symbol turns out to
be defined by a shared object.

Differential Revision: https://reviews.llvm.org/D92714

Added: 
    

Modified: 
    clang/lib/CodeGen/CodeGenModule.cpp
    clang/test/CodeGen/dso-local-executable.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 959f149667d9..85b5009dce7e 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -985,8 +985,7 @@ static bool shouldAssumeDSOLocal(const CodeGenModule &CGM,
 
   // If we can use copy relocations we can assume it is local.
   if (auto *Var = dyn_cast<llvm::GlobalVariable>(GV))
-    if (!Var->isThreadLocal() &&
-        (RM == llvm::Reloc::Static || CGOpts.DirectAccessExternalData))
+    if (!Var->isThreadLocal() && CGOpts.DirectAccessExternalData)
       return true;
 
   // If we can use a plt entry as the symbol address we can assume it

diff  --git a/clang/test/CodeGen/dso-local-executable.c b/clang/test/CodeGen/dso-local-executable.c
index 5c5c6f59c09c..19930b068218 100644
--- a/clang/test/CodeGen/dso-local-executable.c
+++ b/clang/test/CodeGen/dso-local-executable.c
@@ -20,7 +20,10 @@
 // MINGW-DAG: define dso_local i32* @zed()
 // MINGW-DAG: declare dllimport void @import_func()
 
+/// Static relocation model defaults to -fdirect-access-external-data and sets
+/// dso_local on most global objects.
 // RUN: %clang_cc1 -triple x86_64 -emit-llvm -mrelocation-model static %s -o - | FileCheck --check-prefix=STATIC %s
+// RUN: %clang_cc1 -triple x86_64 -emit-llvm -mrelocation-model static -fdirect-access-external-data %s -o - | FileCheck --check-prefix=STATIC %s
 // STATIC:      @baz = dso_local global i32 42
 // STATIC-NEXT: @import_var = external dso_local global i32
 // STATIC-NEXT: @weak_bar = extern_weak dso_local global i32
@@ -31,6 +34,19 @@
 // STATIC-DAG: define dso_local i32* @zed()
 // STATIC-DAG: declare dso_local void @import_func()
 
+/// If -fno-direct-access-external-data is set, drop dso_local from global variable
+/// declarations.
+// RUN: %clang_cc1 -triple x86_64 -emit-llvm %s -mrelocation-model static -fno-direct-access-external-data -o - | FileCheck --check-prefix=STATIC-INDIRECT %s
+// STATIC-INDIRECT:      @baz = dso_local global i32 42
+// STATIC-INDIRECT-NEXT: @import_var = external global i32
+// STATIC-INDIRECT-NEXT: @weak_bar = extern_weak global i32
+// STATIC-INDIRECT-NEXT: @bar = external global i32
+// STATIC-INDIRECT-NEXT: @local_thread_var = dso_local thread_local global i32 42
+// STATIC-INDIRECT-NEXT: @thread_var = external thread_local global i32
+// STATIC-INDIRECT-DAG:  declare dso_local void @import_func()
+// STATIC-INDIRECT-DAG:  define dso_local i32* @zed()
+// STATIC-INDIRECT-DAG:  declare dso_local void @foo()
+
 // RUN: %clang_cc1 -triple x86_64 -emit-llvm -pic-level 1 -pic-is-pie %s -o - | FileCheck --check-prefix=PIE %s
 // PIE:      @baz = dso_local global i32 42
 // PIE-NEXT: @import_var = external global i32
@@ -87,7 +103,9 @@
 // PIE-NO-PLT-DAG:  define dso_local i32* @zed()
 // PIE-NO-PLT-DAG:  declare void @foo()
 
+/// -fdirect-access-external-data is currently ignored for -fPIC.
 // RUN: %clang_cc1 -triple x86_64 -emit-llvm -pic-level 2 %s -o - | FileCheck --check-prefix=SHARED %s
+// RUN: %clang_cc1 -triple x86_64 -emit-llvm -pic-level 2 -fdirect-access-external-data %s -o - | FileCheck --check-prefix=SHARED %s
 // SHARED-DAG: @bar = external global i32
 // SHARED-DAG: @weak_bar = extern_weak global i32
 // SHARED-DAG: declare void @foo()


        


More information about the llvm-branch-commits mailing list