[lld] r279013 - [ELF] Allow specifying the stack size

Petr Hosek via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 17 16:59:58 PDT 2016


Author: phosek
Date: Wed Aug 17 18:59:58 2016
New Revision: 279013

URL: http://llvm.org/viewvc/llvm-project?rev=279013&view=rev
Log:
[ELF] Allow specifying the stack size

This option can be used to specify the stack size of the
PT_GNU_STACK segment.

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

Added:
    lld/trunk/test/ELF/zstack-size.s
Modified:
    lld/trunk/ELF/Config.h
    lld/trunk/ELF/Driver.cpp
    lld/trunk/ELF/Writer.cpp

Modified: lld/trunk/ELF/Config.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Config.h?rev=279013&r1=279012&r2=279013&view=diff
==============================================================================
--- lld/trunk/ELF/Config.h (original)
+++ lld/trunk/ELF/Config.h Wed Aug 17 18:59:58 2016
@@ -121,6 +121,7 @@ struct Configuration {
   uint16_t EMachine = llvm::ELF::EM_NONE;
   uint64_t EntryAddr = -1;
   uint64_t ImageBase;
+  uint64_t ZStackSize = -1;
   unsigned LtoJobs;
   unsigned LtoO;
   unsigned Optimize;

Modified: lld/trunk/ELF/Driver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Driver.cpp?rev=279013&r1=279012&r2=279013&view=diff
==============================================================================
--- lld/trunk/ELF/Driver.cpp (original)
+++ lld/trunk/ELF/Driver.cpp Wed Aug 17 18:59:58 2016
@@ -256,6 +256,17 @@ static bool hasZOption(opt::InputArgList
   return false;
 }
 
+static Optional<StringRef>
+getZOptionValue(opt::InputArgList &Args, StringRef Key) {
+  for (auto *Arg : Args.filtered(OPT_z)) {
+    StringRef Value = Arg->getValue();
+    size_t Pos = Value.find("=");
+    if (Pos != StringRef::npos && Key == Value.substr(0, Pos))
+      return Value.substr(Pos + 1);
+  }
+  return None;
+}
+
 void LinkerDriver::main(ArrayRef<const char *> ArgsArr) {
   ELFOptTable Parser;
   opt::InputArgList Args = Parser.parse(ArgsArr.slice(1));
@@ -395,6 +406,10 @@ void LinkerDriver::readConfigs(opt::Inpu
   Config->ZOrigin = hasZOption(Args, "origin");
   Config->ZRelro = !hasZOption(Args, "norelro");
 
+  if (Optional<StringRef> Value = getZOptionValue(Args, "stack-size"))
+    if (Value->getAsInteger(0, Config->ZStackSize))
+      error("invalid stack size: " + *Value);
+
   if (Config->Relocatable)
     Config->StripAll = false;
 

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=279013&r1=279012&r2=279013&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Wed Aug 17 18:59:58 2016
@@ -1005,8 +1005,11 @@ std::vector<PhdrEntry<ELFT>> Writer<ELFT
 
   // PT_GNU_STACK is a special section to tell the loader to make the
   // pages for the stack non-executable.
-  if (!Config->ZExecStack)
-    AddHdr(PT_GNU_STACK, PF_R | PF_W);
+  if (!Config->ZExecStack) {
+    Phdr &Hdr = *AddHdr(PT_GNU_STACK, PF_R | PF_W);
+    if (Config->ZStackSize != uint64_t(-1))
+      Hdr.H.p_memsz = Config->ZStackSize;
+  }
 
   if (Note.First)
     Ret.push_back(std::move(Note));

Added: lld/trunk/test/ELF/zstack-size.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/zstack-size.s?rev=279013&view=auto
==============================================================================
--- lld/trunk/test/ELF/zstack-size.s (added)
+++ lld/trunk/test/ELF/zstack-size.s Wed Aug 17 18:59:58 2016
@@ -0,0 +1,20 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
+# RUN: ld.lld -z stack-size=0x1000 %t -o %t1
+# RUN: llvm-readobj -program-headers %t1 | FileCheck %s
+
+.global _start
+_start:
+  nop
+
+# CHECK:     Type: PT_GNU_STACK (0x6474E551)
+# CHECK-NEXT:     Offset: 0x0
+# CHECK-NEXT:     VirtualAddress: 0x0
+# CHECK-NEXT:     PhysicalAddress: 0x0
+# CHECK-NEXT:     FileSize: 0
+# CHECK-NEXT:     MemSize: 4096
+# CHECK-NEXT:     Flags [ (0x6)
+# CHECK-NEXT:       PF_R (0x4)
+# CHECK-NEXT:       PF_W (0x2)
+# CHECK-NEXT:     ]
+# CHECK-NEXT:     Alignment: 0




More information about the llvm-commits mailing list