Index: clang/lib/CodeGen/CodeGenModule.cpp =================================================================== --- clang/lib/CodeGen/CodeGenModule.cpp +++ clang/lib/CodeGen/CodeGenModule.cpp @@ -6013,10 +6013,13 @@ getModule(), Type, Constant, Linkage, InitialValue, Name.c_str(), /*InsertBefore=*/nullptr, llvm::GlobalVariable::NotThreadLocal, TargetAS); if (emitter) emitter->finalize(GV); - setGVProperties(GV, VD); - if (GV->getDLLStorageClass() == llvm::GlobalVariable::DLLExportStorageClass) - // The reference temporary should never be dllexport. - GV->setDLLStorageClass(llvm::GlobalVariable::DefaultStorageClass); + // Prevent InternalLinkage) globals getting assigned a dllimport or dllexport. + if (Linkage != llvm::GlobalVariable::InternalLinkage) { + setGVProperties(GV, VD); + if (GV->getDLLStorageClass() == llvm::GlobalVariable::DLLExportStorageClass) + // The reference temporary should never be dllexport. + GV->setDLLStorageClass(llvm::GlobalVariable::DefaultStorageClass); + } GV->setAlignment(Align.getAsAlign()); if (supportsCOMDAT() && GV->isWeakForLinker()) GV->setComdat(TheModule.getOrInsertComdat(GV->getName())); Index: llvm/docs/LangRef.rst =================================================================== --- llvm/docs/LangRef.rst +++ llvm/docs/LangRef.rst @@ -522,6 +522,9 @@ assembler and linker know it is externally referenced and must refrain from deleting the symbol. +A symbol with ``internal`` or ``private`` linkage cannot have a DLL storage +class. + .. _tls_model: Thread Local Storage Models Index: llvm/include/llvm/IR/GlobalValue.h =================================================================== --- llvm/include/llvm/IR/GlobalValue.h +++ llvm/include/llvm/IR/GlobalValue.h @@ -275,7 +275,11 @@ bool hasDLLExportStorageClass() const { return DllStorageClass == DLLExportStorageClass; } - void setDLLStorageClass(DLLStorageClassTypes C) { DllStorageClass = C; } + void setDLLStorageClass(DLLStorageClassTypes C) { + assert((!hasLocalLinkage() || (C == DefaultStorageClass)) && + "local linkage requires DefaultStorageClass"); + DllStorageClass = C; + } bool hasSection() const { return !getSection().empty(); } StringRef getSection() const; @@ -524,8 +528,10 @@ } void setLinkage(LinkageTypes LT) { - if (isLocalLinkage(LT)) + if (isLocalLinkage(LT)) { Visibility = DefaultVisibility; + DllStorageClass = DefaultStorageClass; + } Linkage = LT; if (isImplicitDSOLocal()) setDSOLocal(true); Index: llvm/lib/AsmParser/LLParser.cpp =================================================================== --- llvm/lib/AsmParser/LLParser.cpp +++ llvm/lib/AsmParser/LLParser.cpp @@ -929,6 +929,10 @@ return !GlobalValue::isLocalLinkage((GlobalValue::LinkageTypes)L) || (GlobalValue::VisibilityTypes)V == GlobalValue::DefaultVisibility; } +static bool isValidDLLStorageClassForLinkage(unsigned S, unsigned L) { + return !GlobalValue::isLocalLinkage((GlobalValue::LinkageTypes)L) || + (GlobalValue::DLLStorageClassTypes)S == GlobalValue::DefaultStorageClass; +} // If there was an explicit dso_local, update GV. In the absence of an explicit // dso_local we keep the default value. @@ -982,6 +986,10 @@ return error(NameLoc, "symbol with local linkage must have default visibility"); + if (!isValidDLLStorageClassForLinkage(DLLStorageClass, L)) + return error(NameLoc, + "symbol with local linkage must not have a DLL Storage class"); + Type *Ty; LocTy ExplicitTypeLoc = Lex.getLoc(); if (parseType(Ty) || @@ -1169,6 +1177,10 @@ return error(NameLoc, "symbol with local linkage must have default visibility"); + if (!isValidDLLStorageClassForLinkage(DLLStorageClass, Linkage)) + return error(NameLoc, + "symbol with local linkage must not have a DLL Storage class"); + unsigned AddrSpace; bool IsConstant, IsExternallyInitialized; LocTy IsExternallyInitializedLoc; @@ -5603,6 +5615,10 @@ return error(LinkageLoc, "symbol with local linkage must have default visibility"); + if (!isValidDLLStorageClassForLinkage(DLLStorageClass, Linkage)) + return error(LinkageLoc, + "symbol with local linkage must not have a DLL Storage class"); + if (!FunctionType::isValidReturnType(RetType)) return error(RetTypeLoc, "invalid function return type"); Index: llvm/lib/Bitcode/Reader/BitcodeReader.cpp =================================================================== --- llvm/lib/Bitcode/Reader/BitcodeReader.cpp +++ llvm/lib/Bitcode/Reader/BitcodeReader.cpp @@ -1298,6 +1298,9 @@ } static void upgradeDLLImportExportLinkage(GlobalValue *GV, unsigned Val) { + // Local linkage must not have a DLL Storage class. + if (GV->hasLocalLinkage()) + return; switch (Val) { case 5: GV->setDLLStorageClass(GlobalValue::DLLImportStorageClass); break; case 6: GV->setDLLStorageClass(GlobalValue::DLLExportStorageClass); break; @@ -3764,9 +3767,11 @@ NewGV->setVisibility(Visibility); NewGV->setUnnamedAddr(UnnamedAddr); - if (Record.size() > 10) - NewGV->setDLLStorageClass(getDecodedDLLStorageClass(Record[10])); - else + if (Record.size() > 10) { + // Local linkage should not have a DLL Storage class. + if (!NewGV->hasLocalLinkage()) + NewGV->setDLLStorageClass(getDecodedDLLStorageClass(Record[10])); + } else upgradeDLLImportExportLinkage(NewGV, RawLinkage); ValueList.push_back(NewGV, getVirtualTypeID(NewGV->getType(), TyID)); @@ -3928,9 +3933,11 @@ if (Record.size() > 10) OperandInfo.Prologue = Record[10]; - if (Record.size() > 11) - Func->setDLLStorageClass(getDecodedDLLStorageClass(Record[11])); - else + if (Record.size() > 11) { + // Local linkage must not have a DLL Storage class. + if (!Func->hasLocalLinkage()) + Func->setDLLStorageClass(getDecodedDLLStorageClass(Record[11])); + } else upgradeDLLImportExportLinkage(Func, RawLinkage); if (Record.size() > 12) { @@ -4034,8 +4041,12 @@ } if (BitCode == bitc::MODULE_CODE_ALIAS || BitCode == bitc::MODULE_CODE_ALIAS_OLD) { - if (OpNum != Record.size()) - NewGA->setDLLStorageClass(getDecodedDLLStorageClass(Record[OpNum++])); + if (OpNum != Record.size()) { + auto S = Record[OpNum++]; + // Local linkage must not have a DLL Storage class. + if (!NewGA->hasLocalLinkage()) + NewGA->setDLLStorageClass(getDecodedDLLStorageClass(S)); + } else upgradeDLLImportExportLinkage(NewGA, Linkage); if (OpNum != Record.size()) Index: llvm/test/Assembler/internal-dllexport-alias.ll =================================================================== --- /dev/null +++ llvm/test/Assembler/internal-dllexport-alias.ll @@ -0,0 +1,6 @@ +; RUN: not llvm-as < %s -o /dev/null 2>&1 | FileCheck %s + +@global = global i32 0 + +@alias = internal dllexport alias i32, i32* @global +; CHECK: symbol with local linkage must not have a DLL Storage class Index: llvm/test/Assembler/internal-dllexport-function.ll =================================================================== --- /dev/null +++ llvm/test/Assembler/internal-dllexport-function.ll @@ -0,0 +1,7 @@ +; RUN: not llvm-as < %s -o /dev/null 2>&1 | FileCheck %s + +define internal dllexport void @function() { +; CHECK: symbol with local linkage must not have a DLL Storage class +entry: + ret void +} Index: llvm/test/Assembler/internal-dllexport-variable.ll =================================================================== --- /dev/null +++ llvm/test/Assembler/internal-dllexport-variable.ll @@ -0,0 +1,4 @@ +; RUN: not llvm-as < %s -o /dev/null 2>&1 | FileCheck %s + +@var = internal dllexport global i32 0 +; CHECK: symbol with local linkage must not have a DLL Storage class Index: llvm/test/Assembler/internal-dllimport-function.ll =================================================================== --- /dev/null +++ llvm/test/Assembler/internal-dllimport-function.ll @@ -0,0 +1,7 @@ +; RUN: not llvm-as < %s -o /dev/null 2>&1 | FileCheck %s + +define internal dllimport void @function() { +; CHECK: symbol with local linkage must not have a DLL Storage class +entry: + ret void +} Index: llvm/test/Assembler/private-dllexport-alias.ll =================================================================== --- /dev/null +++ llvm/test/Assembler/private-dllexport-alias.ll @@ -0,0 +1,6 @@ +; RUN: not llvm-as < %s -o /dev/null 2>&1 | FileCheck %s + +@global = global i32 0 + +@alias = private dllexport alias i32, i32* @global +; CHECK: symbol with local linkage must not have a DLL Storage class Index: llvm/test/Assembler/private-dllexport-function.ll =================================================================== --- /dev/null +++ llvm/test/Assembler/private-dllexport-function.ll @@ -0,0 +1,7 @@ +; RUN: not llvm-as < %s -o /dev/null 2>&1 | FileCheck %s + +define private dllexport void @function() { +; CHECK: symbol with local linkage must not have a DLL Storage class +entry: + ret void +} Index: llvm/test/Assembler/private-dllexport-variable.ll =================================================================== --- /dev/null +++ llvm/test/Assembler/private-dllexport-variable.ll @@ -0,0 +1,4 @@ +; RUN: not llvm-as < %s -o /dev/null 2>&1 | FileCheck %s + +@var = private dllexport global i32 0 +; CHECK: symbol with local linkage must not have a DLL Storage class Index: llvm/test/Transforms/GlobalOpt/alias-used-section.ll =================================================================== --- llvm/test/Transforms/GlobalOpt/alias-used-section.ll +++ llvm/test/Transforms/GlobalOpt/alias-used-section.ll @@ -1,8 +1,8 @@ ; RUN: opt -S -passes=globalopt < %s | FileCheck %s @_Z17in_custom_section = internal global i8 42, section "CUSTOM" -@in_custom_section = internal dllexport alias i8, i8* @_Z17in_custom_section +@in_custom_section = internal alias i8, i8* @_Z17in_custom_section -; CHECK: @in_custom_section = internal dllexport global i8 42, section "CUSTOM" +; CHECK: @in_custom_section = internal global i8 42, section "CUSTOM" @llvm.used = appending global [1 x i8*] [i8* @in_custom_section], section "llvm.metadata"