1
1
mirror of https://github.com/rui314/mold.git synced 2024-12-25 09:25:15 +03:00

Add -push-state and -pop-state

This commit is contained in:
Rui Ueyama 2021-03-12 17:37:39 +09:00
parent 487486c503
commit fefa083188
3 changed files with 40 additions and 0 deletions

View File

@ -379,6 +379,10 @@ void parse_nonpositional_args(std::span<std::string_view> args,
remaining.push_back(arg); remaining.push_back(arg);
} else if (read_arg(args, arg, "script") || read_arg(args, arg, "T")) { } else if (read_arg(args, arg, "script") || read_arg(args, arg, "T")) {
remaining.push_back(arg); remaining.push_back(arg);
} else if (read_flag(args, "push-state")) {
remaining.push_back("-push-state");
} else if (read_flag(args, "pop-state")) {
remaining.push_back("-pop-state");
} else { } else {
if (args[0][0] == '-') if (args[0][0] == '-')
Fatal() << "mold: unknown command line option: " << args[0]; Fatal() << "mold: unknown command line option: " << args[0];

View File

@ -934,6 +934,8 @@ MemoryMappedFile *find_library(std::string name,
static void read_input_files(std::span<std::string_view> args, static void read_input_files(std::span<std::string_view> args,
ReadContext &ctx) { ReadContext &ctx) {
std::vector<std::tuple<bool, bool>> state;
while (!args.empty()) { while (!args.empty()) {
std::string_view arg; std::string_view arg;
@ -945,6 +947,13 @@ static void read_input_files(std::span<std::string_view> args,
ctx.whole_archive = true; ctx.whole_archive = true;
} else if (read_flag(args, "no-whole-archive")) { } else if (read_flag(args, "no-whole-archive")) {
ctx.whole_archive = false; ctx.whole_archive = false;
} else if (read_flag(args, "push-state")) {
state.push_back({ctx.as_needed, ctx.whole_archive});
} else if (read_flag(args, "pop-state")) {
if (state.empty())
Fatal() << "no state pushed before popping";
std::tie(ctx.as_needed, ctx.whole_archive) = state.back();
state.pop_back();
} else if (read_arg(args, arg, "l")) { } else if (read_arg(args, arg, "l")) {
read_file(find_library(std::string(arg), config.library_paths), ctx); read_file(find_library(std::string(arg), config.library_paths), ctx);
} else { } else {

27
test/push-pop-state.sh Executable file
View File

@ -0,0 +1,27 @@
#!/bin/bash
set -e
cd $(dirname $0)
echo -n "Testing $(basename -s .sh $0) ... "
t=$(pwd)/tmp/$(basename -s .sh $0)
mkdir -p $t
cat <<EOF | clang -shared -o $t/a.so -xc -
int foo = 1;
EOF
cat <<EOF | clang -shared -o $t/b.so -xc -
int bar = 1;
EOF
cat <<EOF | clang -c -o $t/c.o -xc -
int main() {}
EOF
clang -fuse-ld=`pwd`/../mold -o $t/exe $t/c.o -Wl,-as-needed \
-Wl,-push-state -Wl,-no-as-needed $t/a.so -Wl,-pop-state $t/b.so
readelf --dynamic $t/exe > $t/log
fgrep -q a.so $t/log
! fgrep -q b.so $t/log
echo OK