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/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,26 @@ OS << ",data"; } } + +void llvm::emitLinkerFlagsForUsedCOFF(raw_ostream &OS, const GlobalValue *GV, + const Triple &T, Mangler &M) { + if (T.isKnownWindowsMSVCEnvironment()) + OS << " /INCLUDE:"; + else + OS << " -include:"; + + if (T.isWindowsGNUEnvironment() || T.isWindowsCygwinEnvironment()) { + std::string Flag; + raw_string_ostream FlagOS(Flag); + M.getNameWithPrefix(FlagOS, GV, false); + FlagOS.flush(); + + if (Flag[0] == GV->getParent()->getDataLayout().getGlobalPrefix()) + OS << Flag.substr(1); + else + OS << Flag; + } else { + M.getNameWithPrefix(OS, GV, false); + } +} + Index: lib/Target/ARM/ARMAsmPrinter.cpp =================================================================== --- lib/Target/ARM/ARMAsmPrinter.cpp +++ lib/Target/ARM/ARMAsmPrinter.cpp @@ -559,6 +559,17 @@ for (const auto &Alias : M.aliases()) TLOF.emitLinkerFlagsForGlobal(OS, &Alias); + 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 *IA = dyn_cast(LU->getInitializer())) + for (Value *Op : IA->operands()) + TLOF.emitLinkerFlagsForUsed( + OS, cast(Op->stripPointerCastsNoFollowAliases())); + } + OS.flush(); // Output collected flags Index: lib/Target/X86/X86AsmPrinter.cpp =================================================================== --- lib/Target/X86/X86AsmPrinter.cpp +++ lib/Target/X86/X86AsmPrinter.cpp @@ -659,6 +659,18 @@ for (const auto &Alias : M.aliases()) TLOFCOFF.emitLinkerFlagsForGlobal(FlagsOS, &Alias); + 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 *IA = dyn_cast(LU->getInitializer())) + for (Value *Op : IA->operands()) + TLOFCOFF.emitLinkerFlagsForUsed( + FlagsOS, + cast(Op->stripPointerCastsNoFollowAliases())); + } + FlagsOS.flush(); // Output collected flags. Index: test/CodeGen/X86/coff-no-dead-strip.ll =================================================================== --- /dev/null +++ test/CodeGen/X86/coff-no-dead-strip.ll @@ -0,0 +1,13 @@ +; RUN: llc -mtriple i686-windows-msvc -filetype asm -o - %s | FileCheck %s -check-prefix CHECK-X86 +; RUN: llc -mtriple x86_64-windows-msvc -filetype asm -o - %s | FileCheck %s -check-prefix CHECK-X64 +; RUN: llc -mtriple thumbv7-windows-msvc -filetype asm -o - %s | FileCheck %s -check-prefix CHECK-ARM + +@i = global i32 0 +@j = weak global i32 0 + +@llvm.used = appending global [2 x i8*] [i8* bitcast (i32* @i to i8*), i8* bitcast (i32* @j to i8*)] + +; CHECK-X86: .ascii " /INCLUDE:_i /INCLUDE:_j" +; CHECK-X64: .ascii " /INCLUDE:i /INCLUDE:j" +; CHECK-ARM: .ascii " /INCLUDE:i /INCLUDE:j" +