diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index c9f3ea38f249..a3f3af38d968 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -593,7 +593,7 @@ CodeGenModule::getFunctionLinkage(GlobalDecl GD) { return llvm::Function::InternalLinkage; if (D->hasAttr()) - return llvm::Function::DLLExportLinkage; + return llvm::Function::ExternalLinkage; if (D->hasAttr()) return llvm::Function::WeakAnyLinkage; @@ -808,7 +808,8 @@ void CodeGenModule::SetFunctionAttributes(GlobalDecl GD, // overridden by a definition. if (FD->hasAttr()) { - F->setLinkage(llvm::Function::DLLImportLinkage); + F->setLinkage(llvm::Function::ExternalLinkage); + F->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); } else if (FD->hasAttr() || FD->isWeakImported()) { // "extern_weak" is overloaded in LLVM; we probably should have @@ -816,6 +817,8 @@ void CodeGenModule::SetFunctionAttributes(GlobalDecl GD, F->setLinkage(llvm::Function::ExternalWeakLinkage); } else { F->setLinkage(llvm::Function::ExternalLinkage); + if (FD->hasAttr()) + F->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass); LinkageInfo LV = FD->getLinkageAndVisibility(); if (LV.getLinkage() == ExternalLinkage && LV.isVisibilityExplicit()) { @@ -1608,9 +1611,10 @@ CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName, if (LV.getLinkage() != ExternalLinkage) { // Don't set internal linkage on declarations. } else { - if (D->hasAttr()) - GV->setLinkage(llvm::GlobalValue::DLLImportLinkage); - else if (D->hasAttr() || D->isWeakImported()) + if (D->hasAttr()) { + GV->setLinkage(llvm::GlobalValue::ExternalLinkage); + GV->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); + } else if (D->hasAttr() || D->isWeakImported()) GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); // Set visibility on a declaration only if it's explicit. @@ -1883,6 +1887,10 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) { llvm::GlobalValue::LinkageTypes Linkage = GetLLVMLinkageVarDefinition(D, GV->isConstant()); GV->setLinkage(Linkage); + if (D->hasAttr()) + GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass); + else if (D->hasAttr()) + GV->setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass); // If required by the ABI, give definitions of static data members with inline // initializers linkonce_odr linkage. @@ -1925,9 +1933,9 @@ CodeGenModule::GetLLVMLinkageVarDefinition(const VarDecl *D, bool isConstant) { if (Linkage == GVA_Internal) return llvm::Function::InternalLinkage; else if (D->hasAttr()) - return llvm::Function::DLLImportLinkage; + return llvm::Function::ExternalLinkage; else if (D->hasAttr()) - return llvm::Function::DLLExportLinkage; + return llvm::Function::ExternalLinkage; else if (D->hasAttr()) { // selectany symbols are externally visible, so use weak instead of // linkonce. MSVC optimizes away references to const selectany globals, so @@ -2250,9 +2258,9 @@ void CodeGenModule::EmitAliasDefinition(GlobalDecl GD) { if (const FunctionDecl *FD = dyn_cast(D)) { // The dllexport attribute is ignored for undefined symbols. if (FD->hasBody()) - GA->setLinkage(llvm::Function::DLLExportLinkage); + GA->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass); } else { - GA->setLinkage(llvm::Function::DLLExportLinkage); + GA->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass); } } else if (D->hasAttr() || D->hasAttr() || diff --git a/clang/test/CodeGen/dllimport-dllexport.c b/clang/test/CodeGen/dllimport-dllexport.c index e70ac03f04c7..b005e34b788d 100644 --- a/clang/test/CodeGen/dllimport-dllexport.c +++ b/clang/test/CodeGen/dllimport-dllexport.c @@ -1,5 +1,10 @@ // RUN: %clang_cc1 -triple i386-mingw32 -emit-llvm < %s | FileCheck %s +__attribute__((dllexport)) int bar1 = 2; +// CHECK-LABEL: @bar1 = dllexport global i32 2 +__attribute__((dllimport)) extern int bar2; +// CHECK-LABEL: @bar2 = external dllimport global i32 + void __attribute__((dllimport)) foo1(); void __attribute__((dllexport)) foo1(){} // CHECK-LABEL: define dllexport void @foo1 @@ -10,3 +15,8 @@ __declspec(dllimport) void foo3(); __declspec(dllexport) void foo3(){} // CHECK-LABEL: define dllexport void @foo3 __declspec(dllexport) void foo4(); + +__declspec(dllimport) void foo5(); +// CHECK-LABEL: declare dllimport void @foo5 + +int use() { foo5(); return bar2; }