Allow /STACK in #pragma comment(linker, ...)
authorAlex Reinking <alex_reinking@berkeley.edu>
Wed, 5 May 2021 22:54:17 +0000 (15:54 -0700)
committerReid Kleckner <rnk@google.com>
Wed, 5 May 2021 23:00:33 +0000 (16:00 -0700)
The Halide project uses `#pragma comment(linker, "/STACK:...")` to set
the stack size high enough for our embedded compiler to run in end-user
programs on Windows.

Unfortunately, lld-link.exe breaks on this when embedded in a COFF
object, despite supporting the flag on the command line. MSVC's link.exe
supports this fine. This patch extends support for this to lld-link.exe
for better compatibility with MSVC projects.

Differential Revision: https://reviews.llvm.org/D99680

lld/COFF/Driver.cpp
lld/test/COFF/stack-drectve.s [new file with mode: 0644]

index cf11692..e2374b5 100644 (file)
@@ -408,6 +408,10 @@ void LinkerDriver::parseDirectives(InputFile *file) {
     case OPT_section:
       parseSection(arg->getValue());
       break;
+    case OPT_stack:
+      parseNumbers(arg->getValue(), &config->stackReserve,
+                   &config->stackCommit);
+      break;
     case OPT_subsystem: {
       bool gotVersion = false;
       parseSubsystem(arg->getValue(), &config->subsystem,
diff --git a/lld/test/COFF/stack-drectve.s b/lld/test/COFF/stack-drectve.s
new file mode 100644 (file)
index 0000000..0bb717e
--- /dev/null
@@ -0,0 +1,16 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-win32 %s -o %t.obj
+# RUN: lld-link /out:%t.exe /entry:main %t.obj
+# RUN: llvm-readobj --file-headers %t.exe | FileCheck %s
+
+# CHECK: SizeOfStackReserve: 20480
+# CHECK: SizeOfStackCommit: 12288
+
+       .text
+       .globl main
+main:
+       mov $42, %eax
+       ret
+
+       .section        .drectve,"yn"
+       .ascii  " -stack:0x5000,0x3000"