1
1
mirror of https://github.com/rui314/mold.git synced 2024-09-20 09:27:45 +03:00
mold/macho/cmdline.cc

357 lines
12 KiB
C++
Raw Normal View History

2021-10-03 11:49:33 +03:00
#include "mold.h"
#include "../cmdline.h"
2021-10-03 11:49:33 +03:00
2021-12-01 13:45:49 +03:00
#include <optional>
2021-10-03 11:49:33 +03:00
#include <sstream>
#include <sys/stat.h>
#include <sys/types.h>
2021-10-21 17:48:08 +03:00
#include <regex>
2021-10-03 11:49:33 +03:00
#include <unistd.h>
#include <unordered_set>
namespace mold::macho {
static const char helpmsg[] = R"(
Options:
2021-11-27 13:57:38 +03:00
-F<PATH> Add DIR to framework search path
2021-10-22 09:34:16 +03:00
-L<PATH> Add DIR to library search path
2021-11-21 13:54:47 +03:00
-ObjC Load all static archive members that implement
an Objective-C class or category
2022-05-10 13:57:24 +03:00
-U <SYMBOL> Allow a symbol to be undefined
-Z Do not search the standard directories when
searching for libraries and frameworks
2021-11-02 09:23:06 +03:00
-adhoc_codesign Add ad-hoc code signature to the output file
-no_adhoc_codesign
2022-05-09 11:41:20 +03:00
-all_load Include all objects from static archives
-noall_load
2021-10-22 09:34:16 +03:00
-arch <ARCH_NAME> Specify target architecture
2021-12-05 10:52:29 +03:00
-bundle Produce a mach-o bundle
2021-11-12 13:28:55 +03:00
-dead_strip Remove unreachable functions and data
2021-11-30 13:20:29 +03:00
-dead_strip_dylibs Remove unreachable dylibs from dependencies
2021-10-21 16:57:25 +03:00
-demangle Demangle C++ symbols in log messages (default)
2021-11-29 12:12:48 +03:00
-dylib Produce a dynamic library
2021-10-21 17:11:29 +03:00
-dynamic Link against dylibs (default)
2021-11-12 13:00:49 +03:00
-e <SYMBOL> Specify the entry point of a main executable
2021-11-29 12:12:48 +03:00
-execute Produce an executable (default)
2021-11-20 13:26:29 +03:00
-filelist <FILE>[,<DIR>] Specify the list of input file names
2022-05-09 12:35:16 +03:00
-force_load <FILE> Include all objects from a given static archive
2021-11-27 13:57:38 +03:00
-framework <NAME>,[,<SUFFIX>]
Search for a given framework
2021-10-25 14:14:47 +03:00
-headerpad <SIZE> Allocate the size of padding after load commands
-headerpad_max_install_names
Allocate MAXPATHLEN byte padding after load commands
2021-10-21 16:57:25 +03:00
-help Report usage information
2022-05-12 13:55:56 +03:00
-install_name <NAME>
2021-10-22 09:38:00 +03:00
-l<LIB> Search for a given library
2021-10-22 09:34:16 +03:00
-lto_library <FILE> Ignored
2022-05-12 12:46:06 +03:00
-macos_version_min <VERSION>
2021-11-05 08:14:59 +03:00
-map <FILE> Write map file to a given file
2021-11-30 12:05:28 +03:00
-needed-l<LIB> Search for a given library
2021-12-01 07:30:45 +03:00
-needed-framework <NAME>[,<SUFFIX>]
Search for a given framework
2021-10-27 15:15:29 +03:00
-no_deduplicate Ignored
2021-11-05 08:14:59 +03:00
-o <FILE> Set output filename
2021-11-14 08:22:31 +03:00
-pagezero_size <SIZE> Specify the size of the __PAGEZERO segment
2021-10-22 09:34:16 +03:00
-platform_version <PLATFORM> <MIN_VERSION> <SDK_VERSION>
2021-10-21 17:48:08 +03:00
Set platform, platform version and SDK version
2021-11-20 13:01:18 +03:00
-rpath <PATH> Add PATH to the runpath search path list
2022-05-12 14:06:47 +03:00
-stack_size <SIZE>
2021-10-22 09:34:16 +03:00
-syslibroot <DIR> Prepend DIR to library search paths
-search_paths_first
-search_dylibs_first
2021-11-05 12:40:59 +03:00
-t Print out each file the linker loads
2021-10-21 16:57:25 +03:00
-v Report version information)";
2021-10-03 11:49:33 +03:00
2021-12-01 15:50:11 +03:00
template <typename E>
static i64 parse_platform(Context<E> &ctx, std::string_view arg) {
2021-10-21 17:48:08 +03:00
static std::regex re(R"(\d+)", std::regex_constants::ECMAScript);
if (std::regex_match(arg.begin(), arg.end(), re))
return stoi(std::string(arg));
if (arg == "macos")
return PLATFORM_MACOS;
if (arg == "ios")
return PLATFORM_IOS;
if (arg == "tvos")
return PLATFORM_TVOS;
if (arg == "watchos")
return PLATFORM_WATCHOS;
if (arg == "bridgeos")
return PLATFORM_BRIDGEOS;
if (arg == "mac-catalyst")
return PLATFORM_MACCATALYST;
if (arg == "ios-simulator")
return PLATFORM_IOSSIMULATOR;
if (arg == "tvos-simulator")
return PLATFORM_TVOSSIMULATOR;
if (arg == "watchos-simulator")
return PLATFORM_WATCHOSSIMULATOR;
if (arg == "driverkit")
return PLATFORM_DRIVERKIT;
Fatal(ctx) << "unknown -platform_version name: " << arg;
}
2021-12-01 15:50:11 +03:00
template <typename E>
static i64 parse_version(Context<E> &ctx, std::string_view arg) {
2021-10-23 10:49:06 +03:00
static std::regex re(R"((\d+)(?:\.(\d+))?(?:\.(\d+))?)",
2021-10-21 17:48:08 +03:00
std::regex_constants::ECMAScript);
std::cmatch m;
if (!std::regex_match(arg.begin(), arg.end(), m, re))
Fatal(ctx) << "malformed version number: " << arg;
2021-10-23 10:49:06 +03:00
i64 major = (m[1].length() == 0) ? 0 : stoi(m[1]);
i64 minor = (m[2].length() == 0) ? 0 : stoi(m[2]);
i64 patch = (m[3].length() == 0) ? 0 : stoi(m[3]);
return (major << 16) | (minor << 8) | patch;
2021-10-21 17:48:08 +03:00
}
static bool is_directory(std::filesystem::path path) {
std::error_code ec;
return std::filesystem::is_directory(path, ec) && !ec;
}
2021-12-01 15:50:11 +03:00
template <typename E>
2022-05-08 14:56:36 +03:00
std::vector<std::string> parse_nonpositional_args(Context<E> &ctx) {
std::vector<std::string_view> &args = ctx.cmdline_args;
2022-05-08 14:56:36 +03:00
std::vector<std::string> remaining;
i64 i = 1;
2021-10-03 11:49:33 +03:00
2021-11-27 13:57:38 +03:00
std::vector<std::string> framework_paths;
std::vector<std::string> library_paths;
2021-11-28 03:58:25 +03:00
bool nostdlib = false;
std::optional<i64> pagezero_size;
2021-11-27 13:57:38 +03:00
while (i < args.size()) {
2021-10-03 11:49:33 +03:00
std::string_view arg;
2021-10-21 17:48:08 +03:00
std::string_view arg2;
std::string_view arg3;
2022-05-12 13:02:05 +03:00
u64 hex_arg;
2021-10-03 11:49:33 +03:00
auto read_arg = [&](std::string name) {
if (args[i] == name) {
if (args.size() <= i + 1)
2021-10-03 11:49:33 +03:00
Fatal(ctx) << "option -" << name << ": argument missing";
arg = args[i + 1];
i += 2;
2021-10-03 11:49:33 +03:00
return true;
}
return false;
};
2021-10-21 17:48:08 +03:00
auto read_arg3 = [&](std::string name) {
if (args[i] == name) {
if (args.size() <= i + 3)
2021-10-21 17:48:08 +03:00
Fatal(ctx) << "option -" << name << ": argument missing";
arg = args[i + 1];
arg2 = args[i + 2];
arg3 = args[i + 3];
i += 4;
2021-10-21 17:48:08 +03:00
return true;
}
return false;
};
2021-10-22 09:38:00 +03:00
auto read_joined = [&](std::string name) {
if (read_arg(name))
return true;
if (args[i].starts_with(name)) {
2021-11-30 12:05:28 +03:00
arg = args[i].substr(name.size());
i++;
2021-10-22 09:38:00 +03:00
return true;
}
return false;
};
2021-10-03 11:49:33 +03:00
auto read_flag = [&](std::string name) {
if (args[i] == name) {
i++;
2021-10-03 11:49:33 +03:00
return true;
}
return false;
};
2022-05-12 13:02:05 +03:00
auto read_hex = [&](std::string name) {
if (!read_arg(name))
return false;
size_t pos;
hex_arg = std::stol(std::string(arg), &pos, 16);
if (pos != arg.size())
Fatal(ctx) << "malformed " << name << ": " << arg;
return true;
};
if (args[i].starts_with('@')) {
std::vector<std::string_view> vec =
read_response_file(ctx, args[i].substr(1));
args.erase(args.begin() + i);
args.insert(args.begin() + i, vec.begin(), vec.end());
continue;
}
2021-10-03 11:49:33 +03:00
if (read_flag("-help") || read_flag("--help")) {
2022-05-08 14:56:36 +03:00
SyncOut(ctx) << "Usage: " << ctx.cmdline_args[0]
2021-10-03 11:49:33 +03:00
<< " [options] file...\n" << helpmsg;
exit(0);
}
2021-11-27 13:57:38 +03:00
if (read_joined("-F")) {
framework_paths.push_back(std::string(arg));
} else if (read_joined("-L")) {
library_paths.push_back(std::string(arg));
2021-11-28 03:58:25 +03:00
} else if (read_flag("-Z")) {
nostdlib = true;
2021-11-21 13:54:47 +03:00
} else if (read_flag("-ObjC")) {
ctx.arg.ObjC = true;
2022-05-10 13:57:24 +03:00
} else if (read_arg("-U")) {
ctx.arg.U.push_back(std::string(arg));
2021-11-02 09:23:06 +03:00
} else if (read_flag("-adhoc_codesign")) {
ctx.arg.adhoc_codesign = true;
} else if (read_flag("-no_adhoc_codesign")) {
ctx.arg.adhoc_codesign = false;
2022-05-09 11:41:20 +03:00
} else if (read_flag("-all_load")) {
remaining.push_back("-all_load");
} else if (read_flag("-noall_load")) {
remaining.push_back("-noall_load");
2021-10-22 09:34:16 +03:00
} else if (read_arg("-arch")) {
2021-12-02 05:35:48 +03:00
if (arg == "x86_64")
ctx.arg.arch = CPU_TYPE_X86_64;
else if (arg == "arm64")
ctx.arg.arch = CPU_TYPE_ARM64;
else
2021-10-21 17:18:15 +03:00
Fatal(ctx) << "unknown -arch: " << arg;
2021-12-05 10:52:29 +03:00
} else if (read_flag("-bundle")) {
ctx.output_type = MH_BUNDLE;
2021-11-21 09:32:36 +03:00
} else if (read_flag("-color-diagnostics") ||
read_flag("--color-diagnostics")) {
2022-05-09 11:41:20 +03:00
ctx.arg.color_diagnostics = true;
2021-11-12 13:28:55 +03:00
} else if (read_flag("-dead_strip")) {
ctx.arg.dead_strip = true;
2021-11-30 13:20:29 +03:00
} else if (read_flag("-dead_strip_dylibs")) {
ctx.arg.dead_strip_dylibs = true;
2021-10-21 17:18:15 +03:00
} else if (read_flag("-demangle")) {
2021-10-21 16:57:25 +03:00
ctx.arg.demangle = true;
2021-11-29 12:12:48 +03:00
} else if (read_flag("-dylib")) {
ctx.output_type = MH_DYLIB;
2022-05-12 13:02:05 +03:00
} else if (read_hex("-headerpad")) {
ctx.arg.headerpad = hex_arg;
} else if (read_flag("-headerpad_max_install_names")) {
ctx.arg.headerpad = 1024;
2021-10-21 17:11:29 +03:00
} else if (read_flag("-dynamic")) {
ctx.arg.dynamic = true;
2021-11-12 13:00:49 +03:00
} else if (read_arg("-e")) {
ctx.arg.entry = arg;
2021-11-29 12:12:48 +03:00
} else if (read_flag("-execute")) {
ctx.output_type = MH_EXECUTE;
2021-11-21 09:31:23 +03:00
} else if (read_arg("-fatal_warnings")) {
2021-11-20 13:26:29 +03:00
} else if (read_arg("-filelist")) {
remaining.push_back("-filelist");
remaining.push_back(std::string(arg));
2022-05-09 12:35:16 +03:00
} else if (read_arg("-force_load")) {
remaining.push_back("-force_load");
remaining.push_back(std::string(arg));
2021-11-27 13:57:38 +03:00
} else if (read_arg("-framework")) {
remaining.push_back("-framework");
remaining.push_back(std::string(arg));
2021-10-23 11:06:36 +03:00
} else if (read_arg("-lto_library")) {
2022-05-12 12:46:06 +03:00
} else if (read_arg("-macos_version_min")) {
ctx.arg.platform = PLATFORM_MACOS;
ctx.arg.platform_min_version = parse_version(ctx, arg);
2022-05-12 13:55:56 +03:00
} else if (read_arg("-install_name")) {
ctx.arg.install_name = arg;
2021-10-22 09:38:00 +03:00
} else if (read_joined("-l")) {
2021-12-01 07:50:34 +03:00
remaining.push_back("-l");
remaining.push_back(std::string(arg));
2021-11-05 08:14:59 +03:00
} else if (read_arg("-map")) {
ctx.arg.map = arg;
2021-11-30 12:05:28 +03:00
} else if (read_joined("-needed-l")) {
2021-12-01 07:50:34 +03:00
remaining.push_back("-needed-l");
remaining.push_back(std::string(arg));
2021-12-01 07:30:45 +03:00
} else if (read_arg("-needed_framework")) {
remaining.push_back("-needed_framework");
remaining.push_back(std::string(arg));
2021-10-27 15:15:29 +03:00
} else if (read_flag("-no_deduplicate")) {
2021-10-03 11:49:33 +03:00
} else if (read_arg("-o")) {
ctx.arg.output = arg;
2022-05-12 13:02:05 +03:00
} else if (read_hex("-pagezero_size")) {
pagezero_size = hex_arg;
2021-10-21 17:48:08 +03:00
} else if (read_arg3("-platform_version")) {
ctx.arg.platform = parse_platform(ctx, arg);
ctx.arg.platform_min_version = parse_version(ctx, arg2);
ctx.arg.platform_sdk_version = parse_version(ctx, arg3);
2021-11-20 13:01:18 +03:00
} else if (read_arg("-rpath")) {
ctx.arg.rpath.push_back(std::string(arg));
2022-05-12 14:06:47 +03:00
} else if (read_hex("-stack_size")) {
ctx.arg.stack_size = hex_arg;
2021-10-22 09:34:16 +03:00
} else if (read_arg("-syslibroot")) {
2021-11-03 16:07:50 +03:00
ctx.arg.syslibroot.push_back(std::string(arg));
} else if (read_flag("-search_paths_first")) {
ctx.arg.search_paths_first = true;
} else if (read_flag("-search_dylibs_first")) {
ctx.arg.search_paths_first = false;
2021-11-05 12:40:59 +03:00
} else if (read_flag("-t")) {
ctx.arg.trace = true;
2021-10-21 16:57:25 +03:00
} else if (read_flag("-v")) {
SyncOut(ctx) << mold_version;
2021-10-03 11:49:33 +03:00
} else {
2021-11-22 09:36:31 +03:00
if (args[i][0] == '-')
Fatal(ctx) << "unknown command line option: " << args[i];
remaining.push_back(std::string(args[i]));
i++;
2021-10-03 11:49:33 +03:00
}
}
2021-11-30 09:23:00 +03:00
auto add_search_path = [&](std::vector<std::string> &vec, std::string path) {
2021-11-27 13:57:38 +03:00
if (!path.starts_with('/') || ctx.arg.syslibroot.empty()) {
if (is_directory(path))
2021-11-30 09:23:00 +03:00
vec.push_back(path);
return;
2021-11-27 13:57:38 +03:00
}
2021-11-30 09:23:00 +03:00
bool found = false;
for (std::string &dir : ctx.arg.syslibroot) {
std::string str = path_clean(dir + "/" + path);
if (is_directory(str)) {
2021-11-30 09:23:00 +03:00
vec.push_back(str);
found = true;
}
}
if (!found && is_directory(path))
2021-11-30 09:23:00 +03:00
vec.push_back(path);
2021-11-27 13:57:38 +03:00
};
2021-11-03 16:07:50 +03:00
2021-11-27 13:57:38 +03:00
for (std::string &path : library_paths)
2021-11-30 09:23:00 +03:00
add_search_path(ctx.arg.library_paths, path);
2021-11-28 03:58:25 +03:00
if (!nostdlib) {
2021-11-30 09:23:00 +03:00
add_search_path(ctx.arg.library_paths, "/usr/lib");
add_search_path(ctx.arg.library_paths, "/usr/local/lib");
2021-11-28 03:58:25 +03:00
}
2021-11-03 16:07:50 +03:00
2021-11-27 13:57:38 +03:00
for (std::string &path : framework_paths)
2021-11-30 09:23:00 +03:00
add_search_path(ctx.arg.framework_paths, path);
2021-11-28 03:58:25 +03:00
if (!nostdlib) {
2021-11-30 09:23:00 +03:00
add_search_path(ctx.arg.framework_paths, "/Library/Frameworks");
add_search_path(ctx.arg.framework_paths, "/System/Library/Frameworks");
2021-11-28 03:58:25 +03:00
}
if (pagezero_size.has_value()) {
if (ctx.output_type != MH_EXECUTE)
2022-05-12 13:02:05 +03:00
Fatal(ctx) << "-pagezero_size option can only be used when"
<< " linking a main executable";
ctx.arg.pagezero_size = *pagezero_size;
} else {
ctx.arg.pagezero_size = (ctx.output_type == MH_EXECUTE) ? 0x100000000 : 0;
}
2022-05-08 14:56:36 +03:00
return remaining;
2021-10-03 11:49:33 +03:00
}
2021-12-01 15:50:11 +03:00
#define INSTANTIATE(E) \
2022-05-08 14:56:36 +03:00
template std::vector<std::string> parse_nonpositional_args(Context<E> &)
2021-12-01 15:50:11 +03:00
INSTANTIATE_ALL;
2021-12-01 15:50:11 +03:00
2021-10-03 11:49:33 +03:00
} // namespace mold::macho