Skip to content

Commit eb6c213

Browse files
committed
Add --spare-program-headers
This is a new experimental flag to make room at the end of PHDR so that post-processing tools can add more entries as needed. Fixes #1148
1 parent efdac9a commit eb6c213

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

elf/cmdline.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ inline const char helpmsg[] = R"(
141141
--sort-common Ignored
142142
--sort-section Ignored
143143
--spare-dynamic-tags NUMBER Reserve give number of tags in .dynamic section
144+
--spare-program-headers NUMBER
145+
Reserve give number of slots in the program header
144146
--start-lib Give following object files in-archive-file semantics
145147
--end-lib End the effect of --start-lib
146148
--stats Print input statistics
@@ -606,6 +608,9 @@ std::vector<std::string> parse_nonpositional_args(Context<E> &ctx) {
606608
ctx.arg.shared = true;
607609
} else if (read_arg("spare-dynamic-tags")) {
608610
ctx.arg.spare_dynamic_tags = parse_number(ctx, "spare-dynamic-tags", arg);
611+
} else if (read_arg("spare-program-headers")) {
612+
ctx.arg.spare_program_headers
613+
= parse_number(ctx, "spare-program-headers", arg);
609614
} else if (read_flag("start-lib")) {
610615
remaining.push_back("--start-lib");
611616
} else if (read_flag("start-stop")) {

elf/mold.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,6 +1700,7 @@ struct Context {
17001700
bool z_text = false;
17011701
i64 filler = -1;
17021702
i64 spare_dynamic_tags = 5;
1703+
i64 spare_program_headers = 0;
17031704
i64 thread_count = 0;
17041705
i64 z_stack_size = 0;
17051706
u64 shuffle_sections_seed;

elf/output-chunks.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ static std::vector<ElfPhdr<E>> create_phdr(Context<E> &ctx) {
352352
}
353353
}
354354

355+
vec.resize(vec.size() + ctx.arg.spare_program_headers);
355356
return vec;
356357
}
357358

test/elf/spare-program-headers.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
. $(dirname $0)/common.inc
3+
4+
cat <<EOF | $CC -o $t/a.o -c -xc -
5+
#include <stdio.h>
6+
int main() {
7+
printf("Hello world\n");
8+
}
9+
EOF
10+
11+
$CC -B. -o $t/exe1 $t/a.o
12+
$QEMU $t/exe1 | grep -q 'Hello world'
13+
[ "$(readelf -Wl $t/exe1 | grep NULL | wc -l)" -eq 0 ]
14+
15+
$CC -B. -o $t/exe2 $t/a.o -Wl,--spare-program-headers=0
16+
$QEMU $t/exe2 | grep -q 'Hello world'
17+
[ "$(readelf -Wl $t/exe2 | grep NULL | wc -l)" -eq 0 ]
18+
19+
$CC -B. -o $t/exe3 $t/a.o -Wl,--spare-program-headers=1
20+
$QEMU $t/exe3 | grep -q 'Hello world'
21+
[ "$(readelf -Wl $t/exe3 | grep NULL | wc -l)" -eq 1 ]
22+
23+
$CC -B. -o $t/exe4 $t/a.o -Wl,--spare-program-headers=5
24+
$QEMU $t/exe4 | grep -q 'Hello world'
25+
[ "$(readelf -Wl $t/exe4 | grep NULL | wc -l)" -eq 5 ]

0 commit comments

Comments
 (0)