r360207 - [COFF] Use COFF stubs for extern_weak functions

Reid Kleckner via cfe-commits cfe-commits at lists.llvm.org
Tue May 7 16:06:21 PDT 2019


Author: rnk
Date: Tue May  7 16:06:21 2019
New Revision: 360207

URL: http://llvm.org/viewvc/llvm-project?rev=360207&view=rev
Log:
[COFF] Use COFF stubs for extern_weak functions

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

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

Modified: cfe/trunk/lib/CodeGen/CodeGenModule.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.cpp?rev=360207&r1=360206&r2=360207&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.cpp Tue May  7 16:06:21 2019
@@ -763,6 +763,13 @@ static bool shouldAssumeDSOLocal(const C
         !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

Modified: cfe/trunk/test/CodeGen/dso-local-executable.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/dso-local-executable.c?rev=360207&r1=360206&r2=360207&view=diff
==============================================================================
--- cfe/trunk/test/CodeGen/dso-local-executable.c (original)
+++ cfe/trunk/test/CodeGen/dso-local-executable.c Tue May  7 16:06:21 2019
@@ -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()




More information about the cfe-commits mailing list