Index: lib/CodeGen/CodeGenModule.cpp =================================================================== --- lib/CodeGen/CodeGenModule.cpp +++ lib/CodeGen/CodeGenModule.cpp @@ -730,6 +730,14 @@ return false; const llvm::Triple &TT = CGM.getTriple(); + if (TT.isWindowsGNUEnvironment()) { + // In MinGW, variables without DLLImport can still be automatically + // imported from a DLL by the linker; don't mark variables that + // potentially could come from another DLL as DSO local. + if (GV->hasExternalLinkage() && GV->isDeclaration() && + isa(GV) && !GV->isThreadLocal()) + 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: test/CodeGen/dllimport.c =================================================================== --- test/CodeGen/dllimport.c +++ test/CodeGen/dllimport.c @@ -39,7 +39,8 @@ // NB: MSVC issues a warning and makes GlobalRedecl3 dllexport. We follow GCC // and drop the dllimport with a warning. -// CHECK: @GlobalRedecl3 = external dso_local global i32 +// MS: @GlobalRedecl3 = external dso_local global i32 +// GNU: @GlobalRedecl3 = external global i32 __declspec(dllimport) extern int GlobalRedecl3; extern int GlobalRedecl3; // dllimport ignored USEVAR(GlobalRedecl3) Index: test/CodeGen/dso-local-executable.c =================================================================== --- test/CodeGen/dso-local-executable.c +++ test/CodeGen/dso-local-executable.c @@ -9,6 +9,17 @@ // COFF-DAG: @import_var = external dllimport global i32 // COFF-DAG: declare dllimport void @import_func() +// RUN: %clang_cc1 -triple x86_64-w64-mingw32 -emit-llvm %s -o - | FileCheck -allow-deprecated-dag-overlap --check-prefix=MINGW %s +// MINGW-DAG: @bar = external global i32 +// MINGW-DAG: @weak_bar = extern_weak dso_local global i32 +// MINGW-DAG: declare dso_local void @foo() +// MINGW-DAG: @baz = dso_local global i32 42 +// MINGW-DAG: define dso_local i32* @zed() +// MINGW-DAG: @thread_var = external dso_local thread_local global i32 +// MINGW-DAG: @local_thread_var = dso_local thread_local global i32 42 +// MINGW-DAG: @import_var = external dllimport global i32 +// MINGW-DAG: declare dllimport void @import_func() + // RUN: %clang_cc1 -triple x86_64-pc-linux -emit-llvm -mrelocation-model static %s -o - | FileCheck -allow-deprecated-dag-overlap --check-prefix=STATIC %s // STATIC-DAG: @bar = external dso_local global i32 // STATIC-DAG: @weak_bar = extern_weak dso_local global i32 Index: test/CodeGenCXX/dllexport.cpp =================================================================== --- test/CodeGenCXX/dllexport.cpp +++ test/CodeGenCXX/dllexport.cpp @@ -43,7 +43,7 @@ // M64-DAG: @__ImageBase = external dso_local constant i8 -// GNU-DAG: @_ZTVN10__cxxabiv117__class_type_infoE = external dso_local global +// GNU-DAG: @_ZTVN10__cxxabiv117__class_type_infoE = external global // dllexport implies a definition. // MSC-DAG: @"?GlobalDef@@3HA" = dso_local dllexport global i32 0, align 4 @@ -137,7 +137,7 @@ // Declarations are not exported. // MSC-DAG: @"??$VarTmplImplicitDef@UImplicitInst_Exported@@@@3HA" = external dso_local global -// GNU-DAG: @_Z18VarTmplImplicitDefI21ImplicitInst_ExportedE = external dso_local global +// GNU-DAG: @_Z18VarTmplImplicitDefI21ImplicitInst_ExportedE = external global template __declspec(dllexport) extern int VarTmplImplicitDef; USEVAR(VarTmplImplicitDef) Index: test/CodeGenCXX/dllimport-members.cpp =================================================================== --- test/CodeGenCXX/dllimport-members.cpp +++ test/CodeGenCXX/dllimport-members.cpp @@ -854,7 +854,7 @@ // Not importing specialization of a member variable template without explicit // dllimport. // MSC-DAG: @"??$ImportedStaticVar@UExplicitSpec_NotImported@@@MemVarTmpl@@2HB" = external dso_local constant i32 -// GNU-DAG: @_ZN10MemVarTmpl17ImportedStaticVarI24ExplicitSpec_NotImportedEE = external dso_local constant i32 +// GNU-DAG: @_ZN10MemVarTmpl17ImportedStaticVarI24ExplicitSpec_NotImportedEE = external constant i32 template<> const int MemVarTmpl::ImportedStaticVar; USEMV(MemVarTmpl, ImportedStaticVar) Index: test/CodeGenCXX/dllimport.cpp =================================================================== --- test/CodeGenCXX/dllimport.cpp +++ test/CodeGenCXX/dllimport.cpp @@ -78,7 +78,7 @@ // NB: MSC issues a warning and makes GlobalRedecl3 dllexport. We follow GCC // and drop the dllimport with a warning. // MSC-DAG: @"?GlobalRedecl3@@3HA" = external dso_local global i32 -// GNU-DAG: @GlobalRedecl3 = external dso_local global i32 +// GNU-DAG: @GlobalRedecl3 = external global i32 __declspec(dllimport) extern int GlobalRedecl3; extern int GlobalRedecl3; // dllimport ignored USEVAR(GlobalRedecl3) @@ -137,7 +137,7 @@ USEVAR(VarTmplRedecl2) // MSC-DAG: @"??$VarTmplRedecl3@UImplicitInst_Imported@@@@3HA" = external dso_local global i32 -// GNU-DAG: @_Z14VarTmplRedecl3I21ImplicitInst_ImportedE = external dso_local global i32 +// GNU-DAG: @_Z14VarTmplRedecl3I21ImplicitInst_ImportedE = external global i32 template __declspec(dllimport) extern int VarTmplRedecl3; template extern int VarTmplRedecl3; // dllimport ignored USEVAR(VarTmplRedecl3)