]> granicus.if.org Git - clang/commitdiff
[COFF] Use COFF stubs for extern_weak functions
authorReid Kleckner <rnk@google.com>
Tue, 7 May 2019 23:06:21 +0000 (23:06 +0000)
committerReid Kleckner <rnk@google.com>
Tue, 7 May 2019 23:06:21 +0000 (23:06 +0000)
Summary:
A COFF stub indirects the reference to a symbol through memory. A
.refptr.$sym global variable pointer is created to refer to $sym.
Typically mingw uses these for external global variable declarations,
but we can use them for weak function declarations as well.

Updates the dso_local classification to add a special case for
extern_weak symbols on COFF in both clang and LLVM.

Fixes PR37598

Reviewers: smeenai, mstorsjo

Subscribers: hiraditya, cfe-commits, llvm-commits

Tags: #clang, #llvm

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@360207 91177308-0d34-0410-b5e6-96231b3b80d8

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

index b490fa0faf2e949f3d59b5871705314cec73588b..40e18e6e350fc076e0aefc3dea589079ca00d218 100644 (file)
@@ -763,6 +763,13 @@ static bool shouldAssumeDSOLocal(const CodeGenModule &CGM,
         !GV->isThreadLocal())
       return false;
   }
+
+  // On COFF, don't mark 'extern_weak' symbols as DSO local. If these symbols
+  // remain unresolved in the link, they can be resolved to zero, which is
+  // outside the current DSO.
+  if (TT.isOSBinFormatCOFF() && GV->hasExternalWeakLinkage())
+    return false;
+
   // Every other GV is local on COFF.
   // Make an exception for windows OS in the triple: Some firmware builds use
   // *-win32-macho triples. This (accidentally?) produced windows relocations
index 36fd4d0d039611c08e87256738a5dee4056b3d99..13e11158300fa08e385d0c32c9cc92191b8ce784 100644 (file)
@@ -1,6 +1,6 @@
 // RUN: %clang_cc1 -triple x86_64-pc-win32 -emit-llvm %s -o - | FileCheck -allow-deprecated-dag-overlap --check-prefix=COFF %s
 // COFF-DAG: @bar = external dso_local global i32
-// COFF-DAG: @weak_bar = extern_weak dso_local global i32
+// COFF-DAG: @weak_bar = extern_weak global i32
 // COFF-DAG: declare dso_local void @foo()
 // COFF-DAG: @baz = dso_local global i32 42
 // COFF-DAG: define dso_local i32* @zed()