From a96bd4324609c084690c815653348524f3b16538 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Tue, 16 Mar 2021 21:57:28 +0900 Subject: [PATCH] Add -Bstatic and -Bdynamic --- commandline.cc | 6 +++++- linker_script.cc | 7 ++++--- main.cc | 19 +++++++++++++------ mold.h | 4 +++- 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/commandline.cc b/commandline.cc index 324bdfae..1b6fe86d 100644 --- a/commandline.cc +++ b/commandline.cc @@ -222,8 +222,12 @@ void parse_nonpositional_args(std::span args, config.print_map = true; } else if (read_flag(args, "print-map") || read_flag(args, "M")) { config.print_map = true; - } else if (read_flag(args, "static")) { + } else if (read_flag(args, "static") || read_flag(args, "Bstatic")) { config.is_static = true; + remaining.push_back("-Bstatic"); + } else if (read_flag(args, "Bdynamic")) { + config.is_static = false; + remaining.push_back("-Bdynamic"); } else if (read_flag(args, "shared") || read_flag(args, "Bshareable")) { config.shared = true; } else if (read_flag(args, "demangle")) { diff --git a/linker_script.cc b/linker_script.cc index 5937779f..9cb45396 100644 --- a/linker_script.cc +++ b/linker_script.cc @@ -76,12 +76,12 @@ static std::span read_output_format(std::span tok, ReadContext &ctx) { continue; } - read_file(resolve_path(std::string(unquote(tok[0]))), ctx); + MemoryMappedFile *mb = resolve_path(std::string(unquote(tok[0])), ctx); + read_file(mb, ctx); tok = tok.subspan(1); } diff --git a/main.cc b/main.cc index 75267301..86e36690 100644 --- a/main.cc +++ b/main.cc @@ -983,11 +983,12 @@ static void signal_handler(int) { } MemoryMappedFile *find_library(std::string name, - std::span lib_paths) { + std::span lib_paths, + ReadContext &ctx) { for (std::string_view dir : lib_paths) { std::string root = dir.starts_with("/") ? config.sysroot : ""; std::string stem = root + std::string(dir) + "/lib" + name; - if (!config.is_static) + if (!ctx.is_static) if (MemoryMappedFile *mb = MemoryMappedFile::open(stem + ".so")) return mb; if (MemoryMappedFile *mb = MemoryMappedFile::open(stem + ".a")) @@ -998,7 +999,7 @@ MemoryMappedFile *find_library(std::string name, static void read_input_files(std::span args, ReadContext &ctx) { - std::vector> state; + std::vector> state; while (!args.empty()) { std::string_view arg; @@ -1011,15 +1012,21 @@ static void read_input_files(std::span args, ctx.whole_archive = true; } else if (read_flag(args, "no-whole-archive")) { ctx.whole_archive = false; + } else if (read_flag(args, "Bstatic")) { + ctx.is_static = true; + } else if (read_flag(args, "Bdynamic")) { + ctx.is_static = false; } else if (read_flag(args, "push-state")) { - state.push_back({ctx.as_needed, ctx.whole_archive}); + state.push_back({ctx.as_needed, ctx.whole_archive, ctx.is_static}); } 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(); + std::tie(ctx.as_needed, ctx.whole_archive, ctx.is_static) = state.back(); state.pop_back(); } else if (read_arg(args, arg, "l")) { - read_file(find_library(std::string(arg), config.library_paths), ctx); + MemoryMappedFile *mb = + find_library(std::string(arg), config.library_paths, ctx); + read_file(mb, ctx); } else { read_file(MemoryMappedFile::must_open(std::string(args[0])), ctx); args = args.subspan(1); diff --git a/mold.h b/mold.h index 45b291c2..c30cca55 100644 --- a/mold.h +++ b/mold.h @@ -1207,12 +1207,14 @@ public: bool as_needed = false; bool whole_archive = false; bool is_preloading = false; + bool is_static = config.is_static; std::unordered_set visited; tbb::task_group tg; }; MemoryMappedFile *find_library(std::string path, - std::span lib_paths); + std::span lib_paths, + ReadContext &ctx); void read_file(MemoryMappedFile *mb, ReadContext &ctx);