summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--llvm/include/llvm/CodeGen/TargetLoweringObjectFile.h3
-rw-r--r--llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h3
-rw-r--r--llvm/include/llvm/IR/Mangler.h3
-rw-r--r--llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp30
-rw-r--r--llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp5
-rw-r--r--llvm/lib/IR/Mangler.cpp10
-rw-r--r--llvm/test/CodeGen/X86/coff-no-dead-strip.ll20
7 files changed, 74 insertions, 0 deletions
diff --git a/llvm/include/llvm/CodeGen/TargetLoweringObjectFile.h b/llvm/include/llvm/CodeGen/TargetLoweringObjectFile.h
index fe77c295412..9877072012d 100644
--- a/llvm/include/llvm/CodeGen/TargetLoweringObjectFile.h
+++ b/llvm/include/llvm/CodeGen/TargetLoweringObjectFile.h
@@ -183,6 +183,9 @@ public:
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,
diff --git a/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h b/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
index 69de9f8cb35..8ccb51c2af6 100644
--- a/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
+++ b/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
@@ -163,6 +163,9 @@ public:
void emitLinkerFlagsForGlobal(raw_ostream &OS,
const GlobalValue *GV) const override;
+
+ void emitLinkerFlagsForUsed(raw_ostream &OS,
+ const GlobalValue *GV) const override;
};
class TargetLoweringObjectFileWasm : public TargetLoweringObjectFile {
diff --git a/llvm/include/llvm/IR/Mangler.h b/llvm/include/llvm/IR/Mangler.h
index 56ee21392cc..0261c00f524 100644
--- a/llvm/include/llvm/IR/Mangler.h
+++ b/llvm/include/llvm/IR/Mangler.h
@@ -50,6 +50,9 @@ public:
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
diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index 2a20c64e82c..87d91377dc9 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -1434,6 +1434,7 @@ bool AsmPrinter::doFinalization(Module &M) {
// 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 @@ bool AsmPrinter::doFinalization(Module &M) {
}
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<ArrayType>(LU->getValueType()) &&
+ "expected llvm.used to be an array type");
+ if (const auto *A = cast<ConstantArray>(LU->getInitializer())) {
+ for (const Value *Op : A->operands()) {
+ const auto *GV =
+ cast<GlobalValue>(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,
diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
index bdc4bc0302b..0e90df901fd 100644
--- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
@@ -1250,6 +1250,11 @@ void TargetLoweringObjectFileCOFF::emitLinkerFlagsForGlobal(
emitLinkerFlagsForGlobalCOFF(OS, GV, getTargetTriple(), getMangler());
}
+void TargetLoweringObjectFileCOFF::emitLinkerFlagsForUsed(
+ raw_ostream &OS, const GlobalValue *GV) const {
+ emitLinkerFlagsForUsedCOFF(OS, GV, getTargetTriple(), getMangler());
+}
+
//===----------------------------------------------------------------------===//
// Wasm
//===----------------------------------------------------------------------===//
diff --git a/llvm/lib/IR/Mangler.cpp b/llvm/lib/IR/Mangler.cpp
index 03723bfd2dd..7adcc59f571 100644
--- a/llvm/lib/IR/Mangler.cpp
+++ b/llvm/lib/IR/Mangler.cpp
@@ -204,3 +204,13 @@ void llvm::emitLinkerFlagsForGlobalCOFF(raw_ostream &OS, const GlobalValue *GV,
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);
+}
+
diff --git a/llvm/test/CodeGen/X86/coff-no-dead-strip.ll b/llvm/test/CodeGen/X86/coff-no-dead-strip.ll
new file mode 100644
index 00000000000..adac90da885
--- /dev/null
+++ b/llvm/test/CodeGen/X86/coff-no-dead-strip.ll
@@ -0,0 +1,20 @@
+; 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 -check-prefix CHECK-NOULP
+
+@i = global i32 0
+@j = weak global i32 0
+@k = internal global i32 0
+declare x86_vectorcallcc void @l()
+
+@llvm.used = appending global [4 x i8*] [i8* bitcast (i32* @i to i8*), i8* bitcast (i32* @j to i8*), i8* bitcast (i32* @k to i8*), i8* bitcast (void ()* @l to i8*)]
+
+; CHECK: .section .drectve
+; CHECK-ULP: .ascii " /INCLUDE:_i"
+; CHECK-ULP: .ascii " /INCLUDE:_j"
+; CHECK-ULP-NOT: .ascii " /INCLUDE:_k"
+; CHECK-NOULP: .ascii " /INCLUDE:i"
+; CHECK-NOULP: .ascii " /INCLUDE:j"
+; CHECK-NOULP-NOT: .ascii " /INCLUDE:k"
+; CHECK: .ascii " /INCLUDE:l@@0"
+
OpenPOWER on IntegriCloud