Skip to content

Commit 8825ed7

Browse files
authored
Merge pull request #1506 from minhbq-99/fix-pop-state
Correctly track --static value inside --push-state/--pop-state
2 parents 4aa1a32 + 001f718 commit 8825ed7

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/cmdline.cc

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,7 @@ std::vector<std::string> parse_nonpositional_args(Context<E> &ctx) {
633633
std::optional<u64> shuffle_sections_seed;
634634
std::unordered_set<std::string_view> rpaths;
635635
std::vector<std::string_view> version_scripts;
636+
int state_stack_depth = 0;
636637

637638
auto add_rpath = [&](std::string_view arg) {
638639
if (rpaths.insert(arg).second) {
@@ -837,10 +838,14 @@ std::vector<std::string> parse_nonpositional_args(Context<E> &ctx) {
837838
} else if (read_flag("print-map") || read_flag("M")) {
838839
ctx.arg.print_map = true;
839840
} else if (read_flag("Bstatic") || read_flag("dn") || read_flag("static")) {
840-
ctx.arg.static_ = true;
841+
if (state_stack_depth == 0)
842+
ctx.arg.static_ = true;
843+
841844
remaining.emplace_back("--Bstatic");
842845
} else if (read_flag("Bdynamic") || read_flag("dy")) {
843-
ctx.arg.static_ = false;
846+
if (state_stack_depth == 0)
847+
ctx.arg.static_ = false;
848+
844849
remaining.emplace_back("--Bdynamic");
845850
} else if (read_flag("shared") || read_flag("Bshareable")) {
846851
ctx.arg.shared = true;
@@ -1417,8 +1422,10 @@ std::vector<std::string> parse_nonpositional_args(Context<E> &ctx) {
14171422
} else if (read_arg("script") || read_arg("T")) {
14181423
remaining.emplace_back(arg);
14191424
} else if (read_flag("push-state")) {
1425+
state_stack_depth++;
14201426
remaining.emplace_back("--push-state");
14211427
} else if (read_flag("pop-state")) {
1428+
state_stack_depth--;
14221429
remaining.emplace_back("--pop-state");
14231430
} else if (args[0].starts_with("-z") && args[0].size() > 2) {
14241431
Warn(ctx) << "unknown command line option: " << args[0];

test/push-pop-state.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,10 @@ $CC -B. -o $t/exe $t/c.o -Wl,-as-needed \
1919
readelf --dynamic $t/exe > $t/log
2020
grep -F a.so $t/log
2121
not grep -F b.so $t/log
22+
23+
$CC -no-pie -B. -o $t/exe $t/c.o -Wl,-no-as-needed \
24+
-Wl,-push-state,-Bstatic -lstdc++ -Wl,-pop-state -lc
25+
26+
readelf --dynamic $t/exe > $t/log
27+
not grep -F libstdc++ $t/log
28+
grep -F libc $t/log

0 commit comments

Comments
 (0)