1
1
mirror of https://github.com/rui314/mold.git synced 2024-11-11 05:46:58 +03:00
mold/commandline.cc

438 lines
15 KiB
C++
Raw Normal View History

2021-02-28 05:32:04 +03:00
#include "mold.h"
#include <tbb/global_control.h>
#include <unordered_set>
static std::vector<std::string_view> read_response_file(std::string_view path) {
std::vector<std::string_view> vec;
MemoryMappedFile *mb = MemoryMappedFile::must_open(std::string(path));
auto read_quoted = [&](i64 i, char quote) {
std::string *buf = new std::string;
while (i < mb->size() && mb->data()[i] != quote) {
if (mb->data()[i] == '\\') {
buf->append(1, mb->data()[i + 1]);
i += 2;
} else {
buf->append(1, mb->data()[i++]);
}
}
if (i >= mb->size())
Fatal() << path << ": premature end of input";
vec.push_back(std::string_view(*buf));
return i + 1;
};
auto read_unquoted = [&](i64 i) {
std::string *buf = new std::string;
while (i < mb->size() && !isspace(mb->data()[i]))
buf->append(1, mb->data()[i++]);
vec.push_back(std::string_view(*buf));
return i;
};
for (i64 i = 0; i < mb->size();) {
if (isspace(mb->data()[i]))
i++;
else if (mb->data()[i] == '\'')
i = read_quoted(i + 1, '\'');
else if (mb->data()[i] == '\"')
i = read_quoted(i + 1, '\"');
else
i = read_unquoted(i);
}
return vec;
}
std::vector<std::string_view> expand_response_files(char **argv) {
std::vector<std::string_view> vec;
for (i64 i = 0; argv[i]; i++) {
if (argv[i][0] == '@')
append(vec, read_response_file(argv[i] + 1));
else
vec.push_back(argv[i]);
}
return vec;
}
static std::vector<std::string> add_dashes(std::string name) {
2021-02-28 08:58:39 +03:00
// Multi-letter linker options can be preceded by either a single
// dash or double dashes except ones starting with "o", which must
// be preceded by double dashes. For example, "-omagic" is
// interpreted as "-o magic". If you really want to specify the
// "omagic" option, you have to pass "--omagic".
2021-02-28 05:36:11 +03:00
if (name[0] == 'o')
return {"-" + name};
return {"-" + name, "--" + name};
2021-02-28 05:32:04 +03:00
}
bool read_arg(std::span<std::string_view> &args, std::string_view &arg,
std::string name) {
if (name.size() == 1) {
if (args[0] == "-" + name) {
if (args.size() == 1)
Fatal() << "option -" << name << ": argument missing";
arg = args[1];
args = args.subspan(2);
return true;
}
if (args[0].starts_with("-" + name)) {
arg = args[0].substr(name.size() + 1);
args = args.subspan(1);
return true;
}
return false;
}
for (std::string opt : add_dashes(name)) {
if (args[0] == opt) {
if (args.size() == 1)
Fatal() << "option " << name << ": argument missing";
arg = args[1];
args = args.subspan(2);
return true;
}
if (args[0].starts_with(opt + "=")) {
arg = args[0].substr(opt.size() + 1);
args = args.subspan(1);
return true;
}
}
return false;
}
bool read_flag(std::span<std::string_view> &args, std::string name) {
for (std::string opt : add_dashes(name)) {
if (args[0] == opt) {
args = args.subspan(1);
return true;
}
}
return false;
}
static bool read_z_flag(std::span<std::string_view> &args, std::string name) {
if (args.size() >= 2 && args[0] == "-z" && args[1] == name) {
args = args.subspan(2);
return true;
}
if (!args.empty() && args[0] == "-z" + name) {
args = args.subspan(1);
return true;
}
return false;
}
static i64 parse_hex(std::string opt, std::string_view value) {
if (!value.starts_with("0x") && !value.starts_with("0X"))
Fatal() << "option -" << opt << ": not a hexadecimal number";
value = value.substr(2);
if (value.find_first_not_of("0123456789abcdefABCDEF") != std::string_view::npos)
Fatal() << "option -" << opt << ": not a hexadecimal number";
return std::stol(std::string(value), nullptr, 16);
}
static i64 parse_number(std::string opt, std::string_view value) {
if (value.find_first_not_of("0123456789") != std::string_view::npos)
Fatal() << "option -" << opt << ": not a number";
2021-03-08 14:48:21 +03:00
return std::stol(std::string(value), nullptr, 16);
}
static std::vector<u8> parse_hex_build_id(std::string_view arg) {
assert(arg.starts_with("0x") || arg.starts_with("0X"));
if (arg.size() % 2)
Fatal() << "invalid build-id: " << arg;
if (arg.substr(2).find_first_not_of("0123456789abcdefABCDEF") != arg.npos)
Fatal() << "invalid build-id: " << arg;
arg = arg.substr(2);
auto fn = [](char c) {
if ('0' <= c && c <= '9')
return c - '0';
if ('a' <= c && c <= 'f')
return c - 'a' + 10;
assert('A' <= c && c <= 'F');
return c - 'A' + 10;
};
std::vector<u8> vec(arg.size() / 2);
for (i64 i = 0; i < vec.size(); i++)
vec[i] = (fn(arg[i * 2]) << 4) | fn(arg[i * 2 + 1]);
return vec;
2021-02-28 05:32:04 +03:00
}
2021-03-08 09:25:23 +03:00
static std::vector<std::string_view>
split(std::string_view str, std::string_view sep) {
std::vector<std::string_view> vec;
for (;;) {
i64 pos = str.find(sep);
if (pos == str.npos) {
vec.push_back(str);
break;
}
vec.push_back(str.substr(0, pos));
str = str.substr(pos);
}
return vec;
}
2021-03-05 13:38:37 +03:00
void parse_nonpositional_args(std::span<std::string_view> args,
std::vector<std::string_view> &remaining) {
2021-03-11 13:35:37 +03:00
// mold doesn't scale above 32 threads.
config.thread_count = tbb::global_control::active_value(
tbb::global_control::max_allowed_parallelism);
if (config.thread_count > 32)
config.thread_count = 32;
2021-02-28 05:32:04 +03:00
while (!args.empty()) {
std::string_view arg;
2021-03-01 07:49:39 +03:00
if (read_flag(args, "v") || read_flag(args, "version")) {
SyncOut() << "mold (compatible with GNU linkers)";
exit(0);
}
2021-02-28 05:32:04 +03:00
if (read_arg(args, arg, "o")) {
2021-03-05 13:38:37 +03:00
config.output = arg;
2021-03-12 14:40:52 +03:00
} else if (read_arg(args, arg, "dynamic-linker") ||
read_arg(args, arg, "I")) {
2021-03-05 13:38:37 +03:00
config.dynamic_linker = arg;
2021-03-12 14:40:52 +03:00
} else if (read_arg(args, arg, "no-dynamic-linker")) {
config.dynamic_linker = "";
2021-03-01 09:41:10 +03:00
} else if (read_flag(args, "export-dynamic") || read_flag(args, "E")) {
2021-03-05 13:38:37 +03:00
config.export_dynamic = true;
2021-03-03 18:30:34 +03:00
} else if (read_flag(args, "no-export-dynamic")) {
2021-03-05 13:38:37 +03:00
config.export_dynamic = false;
2021-03-02 15:47:19 +03:00
} else if (read_flag(args, "Bsymbolic")) {
2021-03-05 13:38:37 +03:00
config.Bsymbolic = true;
2021-03-02 16:08:06 +03:00
} else if (read_flag(args, "Bsymbolic-functions")) {
2021-03-05 13:38:37 +03:00
config.Bsymbolic_functions = true;
2021-02-28 05:32:04 +03:00
} else if (read_arg(args, arg, "e") || read_arg(args, arg, "entry")) {
2021-03-05 13:38:37 +03:00
config.entry = arg;
2021-03-15 18:53:53 +03:00
} else if (read_arg(args, arg, "Map")) {
config.Map = arg;
config.print_map = true;
2021-03-12 11:21:23 +03:00
} else if (read_flag(args, "print-map") || read_flag(args, "M")) {
2021-03-05 13:38:37 +03:00
config.print_map = true;
2021-03-16 15:57:28 +03:00
} else if (read_flag(args, "static") || read_flag(args, "Bstatic")) {
2021-03-05 13:38:37 +03:00
config.is_static = true;
2021-03-16 15:57:28 +03:00
remaining.push_back("-Bstatic");
} else if (read_flag(args, "Bdynamic")) {
config.is_static = false;
remaining.push_back("-Bdynamic");
2021-02-28 05:32:04 +03:00
} else if (read_flag(args, "shared") || read_flag(args, "Bshareable")) {
2021-03-05 13:38:37 +03:00
config.shared = true;
2021-02-28 05:32:04 +03:00
} else if (read_flag(args, "demangle")) {
2021-03-05 13:38:37 +03:00
config.demangle = true;
2021-02-28 05:32:04 +03:00
} else if (read_flag(args, "no-demangle")) {
2021-03-05 13:38:37 +03:00
config.demangle = false;
2021-02-28 05:32:04 +03:00
} else if (read_arg(args, arg, "y") || read_arg(args, arg, "trace-symbol")) {
2021-03-05 13:38:37 +03:00
config.trace_symbol.push_back(arg);
2021-02-28 05:32:04 +03:00
} else if (read_arg(args, arg, "filler")) {
2021-03-05 13:38:37 +03:00
config.filler = parse_hex("filler", arg);
2021-02-28 05:32:04 +03:00
} else if (read_arg(args, arg, "L") || read_arg(args, arg, "library-path")) {
2021-03-05 13:38:37 +03:00
config.library_paths.push_back(arg);
2021-02-28 05:32:04 +03:00
} else if (read_arg(args, arg, "sysroot")) {
2021-03-05 13:38:37 +03:00
config.sysroot = arg;
2021-02-28 05:32:04 +03:00
} else if (read_arg(args, arg, "u") || read_arg(args, arg, "undefined")) {
2021-03-05 13:38:37 +03:00
config.undefined.push_back(arg);
2021-03-01 06:49:09 +03:00
} else if (read_arg(args, arg, "init")) {
2021-03-05 13:38:37 +03:00
config.init = arg;
2021-03-01 06:49:09 +03:00
} else if (read_arg(args, arg, "fini")) {
2021-03-05 13:38:37 +03:00
config.fini = arg;
2021-02-28 05:32:04 +03:00
} else if (read_arg(args, arg, "hash-style")) {
if (arg == "sysv") {
2021-03-05 13:38:37 +03:00
config.hash_style_sysv = true;
config.hash_style_gnu = false;
2021-02-28 05:32:04 +03:00
} else if (arg == "gnu") {
2021-03-05 13:38:37 +03:00
config.hash_style_sysv = false;
config.hash_style_gnu = true;
2021-02-28 05:32:04 +03:00
} else if (arg == "both") {
2021-03-05 13:38:37 +03:00
config.hash_style_sysv = true;
config.hash_style_gnu = true;
2021-02-28 05:32:04 +03:00
} else {
Fatal() << "invalid --hash-style argument: " << arg;
}
2021-03-08 16:19:29 +03:00
} else if (read_arg(args, arg, "soname") || read_arg(args, arg, "h")) {
config.soname = arg;
2021-02-28 05:32:04 +03:00
} else if (read_flag(args, "allow-multiple-definition")) {
2021-03-05 13:38:37 +03:00
config.allow_multiple_definition = true;
2021-02-28 05:32:04 +03:00
} else if (read_flag(args, "trace")) {
2021-03-05 13:38:37 +03:00
config.trace = true;
2021-02-28 05:32:04 +03:00
} else if (read_flag(args, "eh-frame-hdr")) {
2021-03-05 13:38:37 +03:00
config.eh_frame_hdr = true;
2021-02-28 05:32:04 +03:00
} else if (read_flag(args, "no-eh-frame-hdr")) {
2021-03-05 13:38:37 +03:00
config.eh_frame_hdr = false;
2021-02-28 05:32:04 +03:00
} else if (read_flag(args, "pie") || read_flag(args, "pic-executable")) {
2021-03-05 13:38:37 +03:00
config.pic = true;
config.pie = true;
2021-03-12 13:26:22 +03:00
} else if (read_flag(args, "no-pie") ||
read_flag(args, "no-pic-executable")) {
2021-03-05 13:38:37 +03:00
config.pic = false;
config.pie = false;
2021-02-28 05:32:04 +03:00
} else if (read_flag(args, "relax")) {
2021-03-05 13:38:37 +03:00
config.relax = true;
2021-02-28 05:32:04 +03:00
} else if (read_flag(args, "no-relax")) {
2021-03-05 13:38:37 +03:00
config.relax = false;
2021-03-13 15:55:27 +03:00
} else if (read_flag(args, "perf")) {
config.perf = true;
} else if (read_flag(args, "stats")) {
config.stats = true;
2021-03-13 14:28:08 +03:00
Counter::enabled = true;
2021-02-28 05:32:04 +03:00
} else if (read_z_flag(args, "now")) {
2021-03-05 13:38:37 +03:00
config.z_now = true;
2021-03-12 12:09:15 +03:00
} else if (read_z_flag(args, "execstack")) {
config.z_execstack = true;
} else if (read_z_flag(args, "noexecstack")) {
config.z_execstack = false;
2021-03-12 13:57:34 +03:00
} else if (read_z_flag(args, "relro")) {
config.z_relro = true;
} else if (read_z_flag(args, "norelro")) {
config.z_relro = false;
2021-03-13 15:41:26 +03:00
} else if (read_z_flag(args, "defs")) {
config.z_defs = true;
} else if (read_z_flag(args, "nodefs")) {
config.z_defs = false;
} else if (read_flag(args, "no-undefined")) {
config.z_defs = true;
2021-02-28 05:32:04 +03:00
} else if (read_flag(args, "fork")) {
2021-03-05 13:38:37 +03:00
config.fork = true;
2021-02-28 05:32:04 +03:00
} else if (read_flag(args, "no-fork")) {
2021-03-05 13:38:37 +03:00
config.fork = false;
2021-02-28 05:32:04 +03:00
} else if (read_flag(args, "gc-sections")) {
2021-03-05 13:38:37 +03:00
config.gc_sections = true;
2021-02-28 05:32:04 +03:00
} else if (read_flag(args, "no-gc-sections")) {
2021-03-05 13:38:37 +03:00
config.gc_sections = false;
2021-02-28 05:32:04 +03:00
} else if (read_flag(args, "print-gc-sections")) {
2021-03-05 13:38:37 +03:00
config.print_gc_sections = true;
2021-02-28 05:32:04 +03:00
} else if (read_flag(args, "no-print-gc-sections")) {
2021-03-05 13:38:37 +03:00
config.print_gc_sections = false;
2021-02-28 05:32:04 +03:00
} else if (read_flag(args, "icf")) {
2021-03-05 13:38:37 +03:00
config.icf = true;
2021-02-28 05:32:04 +03:00
} else if (read_flag(args, "no-icf")) {
2021-03-05 13:38:37 +03:00
config.icf = false;
2021-02-28 09:07:33 +03:00
} else if (read_flag(args, "quick-exit")) {
2021-03-05 13:38:37 +03:00
config.quick_exit = true;
2021-02-28 09:07:33 +03:00
} else if (read_flag(args, "no-quick-exit")) {
2021-03-05 13:38:37 +03:00
config.quick_exit = false;
2021-02-28 05:32:04 +03:00
} else if (read_flag(args, "print-icf-sections")) {
2021-03-05 13:38:37 +03:00
config.print_icf_sections = true;
2021-02-28 05:32:04 +03:00
} else if (read_flag(args, "no-print-icf-sections")) {
2021-03-05 13:38:37 +03:00
config.print_icf_sections = false;
2021-02-28 05:32:04 +03:00
} else if (read_flag(args, "quick-exit")) {
2021-03-05 13:38:37 +03:00
config.quick_exit = true;
2021-02-28 05:32:04 +03:00
} else if (read_flag(args, "no-quick-exit")) {
2021-03-05 13:38:37 +03:00
config.quick_exit = false;
2021-02-28 05:32:04 +03:00
} else if (read_arg(args, arg, "thread-count")) {
2021-03-05 13:38:37 +03:00
config.thread_count = parse_number("thread-count", arg);
2021-02-28 05:32:04 +03:00
} else if (read_flag(args, "no-threads")) {
2021-03-05 13:38:37 +03:00
config.thread_count = 1;
2021-02-28 05:32:04 +03:00
} else if (read_flag(args, "discard-all") || read_flag(args, "x")) {
2021-03-05 13:38:37 +03:00
config.discard_all = true;
2021-02-28 05:32:04 +03:00
} else if (read_flag(args, "discard-locals") || read_flag(args, "X")) {
2021-03-05 13:38:37 +03:00
config.discard_locals = true;
2021-02-28 05:32:04 +03:00
} else if (read_flag(args, "strip-all") || read_flag(args, "s")) {
2021-03-05 13:38:37 +03:00
config.strip_all = true;
2021-03-14 11:04:45 +03:00
} else if (read_flag(args, "strip-debug") || read_flag(args, "S")) {
config.strip_debug = true;
2021-02-28 05:32:04 +03:00
} else if (read_arg(args, arg, "rpath")) {
2021-03-05 13:38:37 +03:00
if (!config.rpaths.empty())
config.rpaths += ":";
config.rpaths += arg;
2021-02-28 05:32:04 +03:00
} else if (read_arg(args, arg, "version-script")) {
2021-03-05 13:38:37 +03:00
parse_version_script(std::string(arg));
2021-03-08 16:19:29 +03:00
} else if (read_arg(args, arg, "dynamic-list")) {
parse_dynamic_list(std::string(arg));
2021-02-28 05:32:04 +03:00
} else if (read_flag(args, "build-id")) {
2021-03-08 14:48:21 +03:00
config.build_id.kind = BuildId::HASH;
config.build_id.hash_size = 20;
2021-02-28 05:32:04 +03:00
} else if (read_arg(args, arg, "build-id")) {
if (arg == "none") {
2021-03-08 14:48:21 +03:00
config.build_id.kind = BuildId::NONE;
2021-02-28 05:32:04 +03:00
} else if (arg == "uuid") {
2021-03-08 14:48:21 +03:00
config.build_id.kind = BuildId::UUID;
2021-02-28 05:32:04 +03:00
} else if (arg == "md5") {
2021-03-08 14:48:21 +03:00
config.build_id.kind = BuildId::HASH;
config.build_id.hash_size = 16;
2021-02-28 05:32:04 +03:00
} else if (arg == "sha1") {
2021-03-08 14:48:21 +03:00
config.build_id.kind = BuildId::HASH;
config.build_id.hash_size = 20;
2021-02-28 05:32:04 +03:00
} else if (arg == "sha256") {
2021-03-08 14:48:21 +03:00
config.build_id.kind = BuildId::HASH;
config.build_id.hash_size = 32;
} else if (arg.starts_with("0x") || arg.starts_with("0X")) {
config.build_id.kind = BuildId::HEX;
config.build_id.value = parse_hex_build_id(arg);
2021-02-28 05:32:04 +03:00
} else {
Fatal() << "invalid --build-id argument: " << arg;
}
2021-03-07 16:28:49 +03:00
} else if (read_flag(args, "no-build-id")) {
2021-03-08 14:48:21 +03:00
config.build_id.kind = BuildId::NONE;
2021-03-15 18:34:09 +03:00
} else if (read_arg(args, arg, "auxiliary") || read_arg(args, arg, "f")) {
config.auxiliary.push_back(arg);
} else if (read_arg(args, arg, "filter") || read_arg(args, arg, "F")) {
config.filter.push_back(arg);
2021-03-08 09:25:23 +03:00
} else if (read_arg(args, arg, "exclude-libs")) {
config.exclude_libs = split(arg, ",");
2021-02-28 05:32:04 +03:00
} else if (read_flag(args, "preload")) {
2021-03-05 13:38:37 +03:00
config.preload = true;
2021-02-28 05:32:04 +03:00
} else if (read_arg(args, arg, "z")) {
2021-03-01 12:21:10 +03:00
} else if (read_arg(args, arg, "O")) {
} else if (read_flag(args, "O0")) {
} else if (read_flag(args, "O1")) {
} else if (read_flag(args, "O2")) {
2021-03-15 13:23:08 +03:00
} else if (read_flag(args, "color-diagnostics")) {
2021-02-28 05:32:04 +03:00
} else if (read_arg(args, arg, "m")) {
} else if (read_flag(args, "eh-frame-hdr")) {
} else if (read_flag(args, "start-group")) {
} else if (read_flag(args, "end-group")) {
2021-02-28 15:03:13 +03:00
} else if (read_flag(args, "(")) {
} else if (read_flag(args, ")")) {
2021-02-28 05:32:04 +03:00
} else if (read_flag(args, "fatal-warnings")) {
} else if (read_flag(args, "disable-new-dtags")) {
} else if (read_arg(args, arg, "rpath-link")) {
2021-02-28 05:32:04 +03:00
} else if (read_flag(args, "as-needed")) {
remaining.push_back("-as-needed");
} else if (read_flag(args, "no-as-needed")) {
remaining.push_back("-no-as-needed");
} else if (read_flag(args, "whole-archive")) {
remaining.push_back("-whole-archive");
} else if (read_flag(args, "no-whole-archive")) {
remaining.push_back("-no-whole-archive");
} else if (read_arg(args, arg, "l")) {
remaining.push_back("-l");
remaining.push_back(arg);
2021-02-28 09:24:36 +03:00
} else if (read_arg(args, arg, "script") || read_arg(args, arg, "T")) {
remaining.push_back(arg);
2021-03-12 11:37:39 +03:00
} else if (read_flag(args, "push-state")) {
remaining.push_back("-push-state");
} else if (read_flag(args, "pop-state")) {
remaining.push_back("-pop-state");
2021-02-28 05:32:04 +03:00
} else {
if (args[0][0] == '-')
Fatal() << "mold: unknown command line option: " << args[0];
remaining.push_back(args[0]);
args = args.subspan(1);
}
}
2021-03-05 13:38:37 +03:00
if (config.shared) {
config.pic = true;
config.dynamic_linker = "";
2021-02-28 05:32:04 +03:00
}
2021-03-07 14:40:16 +03:00
if (config.pic)
config.image_base = 0;
2021-03-16 10:50:49 +03:00
if (config.output == "")
Fatal() << "-o option is missing";
2021-03-07 14:40:16 +03:00
}