[PATCH] D92714: Make -fno-pic respect -fno-direct-access-external-data

Fangrui Song via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 4 19:59:49 PST 2020


MaskRay created this revision.
MaskRay added reviewers: hjl.tools, rnk, tmsriram.
MaskRay requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

D92633 <https://reviews.llvm.org/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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D92714

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


Index: clang/test/CodeGen/dso-local-executable.c
===================================================================
--- clang/test/CodeGen/dso-local-executable.c
+++ clang/test/CodeGen/dso-local-executable.c
@@ -20,6 +20,8 @@
 // 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
 // STATIC:      @baz = dso_local global i32 42
 // STATIC-NEXT: @import_var = external dso_local global i32
@@ -31,6 +33,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
Index: clang/lib/CodeGen/CodeGenModule.cpp
===================================================================
--- clang/lib/CodeGen/CodeGenModule.cpp
+++ clang/lib/CodeGen/CodeGenModule.cpp
@@ -975,8 +975,7 @@
 
   // 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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D92714.309712.patch
Type: text/x-patch
Size: 2420 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20201205/581440fa/attachment-0001.bin>


More information about the cfe-commits mailing list