Index: include/llvm/CodeGen/TargetLoweringObjectFile.h =================================================================== --- include/llvm/CodeGen/TargetLoweringObjectFile.h +++ include/llvm/CodeGen/TargetLoweringObjectFile.h @@ -183,6 +183,9 @@ virtual void emitLinkerFlagsForGlobal(raw_ostream &OS, const GlobalValue *GV) const {} + virtual void emitLinkerFlagsForUsed(raw_ostream &OS, + const GlobalValue *GV) const {} + protected: virtual MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, Index: include/llvm/CodeGen/TargetLoweringObjectFileImpl.h =================================================================== --- include/llvm/CodeGen/TargetLoweringObjectFileImpl.h +++ include/llvm/CodeGen/TargetLoweringObjectFileImpl.h @@ -163,6 +163,9 @@ void emitLinkerFlagsForGlobal(raw_ostream &OS, const GlobalValue *GV) const override; + + void emitLinkerFlagsForUsed(raw_ostream &OS, + const GlobalValue *GV) const override; }; class TargetLoweringObjectFileWasm : public TargetLoweringObjectFile { Index: include/llvm/IR/Mangler.h =================================================================== --- include/llvm/IR/Mangler.h +++ include/llvm/IR/Mangler.h @@ -50,6 +50,9 @@ void emitLinkerFlagsForGlobalCOFF(raw_ostream &OS, const GlobalValue *GV, const Triple &TT, Mangler &Mangler); +void emitLinkerFlagsForUsedCOFF(raw_ostream &OS, const GlobalValue *GV, + const Triple &T, Mangler &M); + } // End llvm namespace #endif Index: lib/CodeGen/AsmPrinter/AsmPrinter.cpp =================================================================== --- lib/CodeGen/AsmPrinter/AsmPrinter.cpp +++ lib/CodeGen/AsmPrinter/AsmPrinter.cpp @@ -1434,6 +1434,7 @@ // Emit /EXPORT: flags for each exported global as necessary. const auto &TLOF = getObjFileLowering(); std::string Flags; + for (const GlobalValue &GV : M.global_values()) { raw_string_ostream OS(Flags); TLOF.emitLinkerFlagsForGlobal(OS, &GV); @@ -1444,6 +1445,35 @@ } Flags.clear(); } + + // Emit /INCLUDE: flags for each used global as necessary. + if (const auto *LU = M.getNamedGlobal("llvm.used")) { + assert(LU->hasInitializer() && + "expected llvm.used to have an initializer"); + assert(isa(LU->getValueType()) && + "expected llvm.used to be an array type"); + if (const auto *A = cast(LU->getInitializer())) { + for (const Value *Op : A->operands()) { + const auto *GV = + cast(Op->stripPointerCastsNoFollowAliases()); + // Global symbols with internal linkage are not visible to the linker, + // and thus would cause an error when the linker tried to preserve the + // symbol due to the `/include:` directive. + if (GV->hasInternalLinkage()) + continue; + + raw_string_ostream OS(Flags); + TLOF.emitLinkerFlagsForUsed(OS, GV); + OS.flush(); + + if (!Flags.empty()) { + OutStreamer->SwitchSection(TLOF.getDrectveSection()); + OutStreamer->EmitBytes(Flags); + } + Flags.clear(); + } + } + } } // Allow the target to emit any magic that it wants at the end of the file, Index: lib/CodeGen/TargetLoweringObjectFileImpl.cpp =================================================================== --- lib/CodeGen/TargetLoweringObjectFileImpl.cpp +++ lib/CodeGen/TargetLoweringObjectFileImpl.cpp @@ -1250,6 +1250,11 @@ emitLinkerFlagsForGlobalCOFF(OS, GV, getTargetTriple(), getMangler()); } +void TargetLoweringObjectFileCOFF::emitLinkerFlagsForUsed( + raw_ostream &OS, const GlobalValue *GV) const { + emitLinkerFlagsForUsedCOFF(OS, GV, getTargetTriple(), getMangler()); +} + //===----------------------------------------------------------------------===// // Wasm //===----------------------------------------------------------------------===// Index: lib/IR/Mangler.cpp =================================================================== --- lib/IR/Mangler.cpp +++ lib/IR/Mangler.cpp @@ -204,3 +204,13 @@ OS << ",data"; } } + +void llvm::emitLinkerFlagsForUsedCOFF(raw_ostream &OS, const GlobalValue *GV, + const Triple &T, Mangler &M) { + if (!T.isKnownWindowsMSVCEnvironment()) + return; + + OS << " /INCLUDE:"; + M.getNameWithPrefix(OS, GV, false); +} + Index: test/CodeGen/X86/coff-no-dead-strip.ll =================================================================== --- /dev/null +++ test/CodeGen/X86/coff-no-dead-strip.ll @@ -0,0 +1,16 @@ +; RUN: llc -mtriple i686-windows-msvc -filetype asm -o - %s | FileCheck %s -check-prefix CHECK -check-prefix CHECK-ULP +; RUN: llc -mtriple x86_64-windows-msvc -filetype asm -o - %s | FileCheck %s -check-prefix CHECK -check-prefix CHECK-NOULP +; RUN: llc -mtriple thumbv7-windows-msvc -filetype asm -o - %s | FileCheck %s -check-prefix -check-prefix CHECK-NOULP + +@i = global i32 0 +@j = weak global i32 0 +@k = internal global i32 0 + +@llvm.used = appending global [3 x i8*] [i8* bitcast (i32* @i to i8*), i8* bitcast (i32* @j to i8*), i8* bitcast (i32* @k to i8*)] + +; CHECK: .section .drectve +; CHECK-ULP: .ascii " /INCLUDE:_i" +; CHECK-ULP: .ascii " /INCLUDE:_j" +; CHECK-NOULP: .ascii " /INCLUDE:i" +; CHECK-NOULP: .ascii " /INCLUDE:j" +