Skip to content

Commit

Permalink
Add -z stack-size
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed Jun 29, 2023
1 parent 2cb41d8 commit b04aba8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
3 changes: 3 additions & 0 deletions elf/cmdline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ inline const char helpmsg[] = R"(
Separate all loadable segments to different pages
-z separate-code Separate code and data into different pages
-z noseparate-code Allow overlap in pages
-z stack-size=VALUE Set size of stack segment
-z relro Make some sections read-only after relocation (default)
-z norelro
-z text Report error if DT_TEXTREL is set
Expand Down Expand Up @@ -866,6 +867,8 @@ std::vector<std::string> parse_nonpositional_args(Context<E> &ctx) {
z_separate_code = SEPARATE_CODE;
} else if (read_z_flag("noseparate-code")) {
z_separate_code = NOSEPARATE_CODE;
} else if (read_z_arg("stack-size")) {
ctx.arg.z_stack_size = parse_number(ctx, "-z stack-size", arg);
} else if (read_z_flag("dynamic-undefined-weak")) {
ctx.arg.z_dynamic_undefined_weak = true;
} else if (read_z_flag("nodynamic-undefined-weak")) {
Expand Down
1 change: 1 addition & 0 deletions elf/mold.h
Original file line number Diff line number Diff line change
Expand Up @@ -1702,6 +1702,7 @@ struct Context {
i64 filler = -1;
i64 spare_dynamic_tags = 5;
i64 thread_count = 0;
i64 z_stack_size = 0;
std::string_view emulation;
std::optional<Glob> unique;
std::optional<u64> physical_image_base;
Expand Down
13 changes: 8 additions & 5 deletions elf/output-chunks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,14 @@ static std::vector<ElfPhdr<E>> create_phdr(Context<E> &ctx) {

// Add PT_GNU_STACK, which is a marker segment that doesn't really
// contain any segments. It controls executable bit of stack area.
vec.push_back(ElfPhdr<E>{
.p_type = PT_GNU_STACK,
.p_flags = ctx.arg.z_execstack ? (PF_R | PF_W | PF_X) : (PF_R | PF_W),
.p_align = 1,
});
{
ElfPhdr<E> phdr = {};
phdr.p_type = PT_GNU_STACK;
phdr.p_flags = ctx.arg.z_execstack ? (PF_R | PF_W | PF_X) : (PF_R | PF_W);
phdr.p_memsz = ctx.arg.z_stack_size;
phdr.p_align = 1;
vec.push_back(phdr);
}

// Create a PT_GNU_RELRO.
if (ctx.arg.z_relro) {
Expand Down
9 changes: 9 additions & 0 deletions test/elf/z-stack-size.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
. $(dirname $0)/common.inc

cat <<EOF | $CC -o $t/a.o -c -xc -
int main() {}
EOF

$CC -B. -o $t/exe $t/a.o -Wl,-z,stack-size=0x900000
readelf -W --segments $t/exe | grep -q 'GNU_STACK .* 0x900000 RW'

0 comments on commit b04aba8

Please sign in to comment.