1
1
mirror of https://github.com/rui314/mold.git synced 2024-12-26 18:02:30 +03:00
mold/main.cc

692 lines
20 KiB
C++
Raw Normal View History

2020-10-20 08:54:35 +03:00
#include "mold.h"
2020-10-02 07:28:26 +03:00
2020-12-21 13:35:33 +03:00
#include <functional>
2021-06-19 18:49:41 +03:00
#include <iomanip>
2021-01-16 05:57:36 +03:00
#include <map>
2020-12-24 08:39:02 +03:00
#include <signal.h>
2021-01-16 05:57:36 +03:00
#include <tbb/global_control.h>
#include <tbb/parallel_for_each.h>
2021-04-09 10:05:52 +03:00
#include <unistd.h>
2020-11-20 07:54:29 +03:00
#include <unordered_set>
2020-09-29 09:05:29 +03:00
2021-04-06 12:02:50 +03:00
template <typename E>
2021-04-06 12:18:45 +03:00
std::string_view save_string(Context<E> &ctx, const std::string &str) {
u8 *buf = new u8[str.size() + 1];
memcpy(buf, str.data(), str.size());
buf[str.size()] = '\0';
ctx.owning_bufs.push_back(std::unique_ptr<u8[]>(buf));
return {(char *)buf, str.size()};
2021-04-06 12:02:50 +03:00
}
2021-04-09 04:08:56 +03:00
std::string get_version_string() {
if (strlen(GIT_HASH) == 0)
return "mold " MOLD_VERSION " (compatible with GNU ld and GNU gold)";
2021-05-31 06:48:26 +03:00
return "mold " MOLD_VERSION " (" GIT_HASH
"; compatible with GNU ld and GNU gold)";
2021-04-09 04:08:56 +03:00
}
2021-06-19 18:49:41 +03:00
std::regex glob_to_regex(std::string_view pattern) {
std::stringstream ss;
for (u8 c : pattern) {
if (c == '*')
ss << ".*";
else
ss << "\\x" << std::hex << std::setw(2) << std::setfill('0') << (int)c;
}
return std::regex(ss.str(), std::regex::optimize);
}
2021-03-29 14:29:57 +03:00
template <typename E>
static ObjectFile<E> *new_object_file(Context<E> &ctx, MemoryMappedFile<E> *mb,
std::string archive_name) {
2021-03-17 07:21:05 +03:00
static Counter count("parsed_objs");
count++;
2021-03-29 08:36:55 +03:00
bool in_lib = (!archive_name.empty() && !ctx.whole_archive);
2021-04-06 11:03:11 +03:00
ObjectFile<E> *file = ObjectFile<E>::create(ctx, mb, archive_name, in_lib);
file->priority = ctx.file_priority++;
2021-03-29 09:16:53 +03:00
ctx.tg.run([file, &ctx]() { file->parse(ctx); });
2021-03-29 07:20:51 +03:00
if (ctx.arg.trace)
2021-03-29 10:48:23 +03:00
SyncOut(ctx) << "trace: " << *file;
2020-12-21 13:35:33 +03:00
return file;
}
2021-03-29 14:29:57 +03:00
template <typename E>
static SharedFile<E> *new_shared_file(Context<E> &ctx, MemoryMappedFile<E> *mb) {
2021-04-06 12:13:53 +03:00
SharedFile<E> *file = SharedFile<E>::create(ctx, mb);
file->priority = ctx.file_priority++;
2021-03-29 09:16:53 +03:00
ctx.tg.run([file, &ctx]() { file->parse(ctx); });
2021-03-29 07:20:51 +03:00
if (ctx.arg.trace)
2021-03-29 10:48:23 +03:00
SyncOut(ctx) << "trace: " << *file;
2020-12-21 13:35:33 +03:00
return file;
}
2021-03-29 14:29:57 +03:00
template <typename E>
void read_file(Context<E> &ctx, MemoryMappedFile<E> *mb) {
2021-03-29 08:36:55 +03:00
if (ctx.visited.contains(mb->name))
2021-03-13 18:58:13 +03:00
return;
2021-03-29 08:36:55 +03:00
if (ctx.is_preloading) {
2021-03-29 10:48:23 +03:00
switch (get_file_type(ctx, mb)) {
2021-01-15 09:41:09 +03:00
case FileType::OBJ:
2021-04-06 06:08:54 +03:00
ctx.obj_cache.store(mb, new_object_file(ctx, mb, ""));
2020-12-22 14:32:49 +03:00
return;
2021-01-15 09:41:09 +03:00
case FileType::DSO:
2021-04-06 06:08:54 +03:00
ctx.dso_cache.store(mb, new_shared_file(ctx, mb));
2021-01-15 09:41:09 +03:00
return;
case FileType::AR:
2021-03-29 14:29:57 +03:00
for (MemoryMappedFile<E> *child : read_fat_archive_members(ctx, mb))
2021-03-29 10:48:23 +03:00
if (get_file_type(ctx, child) == FileType::OBJ)
2021-04-06 06:08:54 +03:00
ctx.obj_cache.store(mb, new_object_file(ctx, child, mb->name));
2021-01-15 09:41:09 +03:00
return;
case FileType::THIN_AR:
2021-03-29 14:29:57 +03:00
for (MemoryMappedFile<E> *child : read_thin_archive_members(ctx, mb))
2021-03-29 10:48:23 +03:00
if (get_file_type(ctx, child) == FileType::OBJ)
2021-04-06 06:08:54 +03:00
ctx.obj_cache.store(child, new_object_file(ctx, child, mb->name));
2021-01-15 09:41:09 +03:00
return;
case FileType::TEXT:
2021-03-29 08:36:55 +03:00
parse_linker_script(ctx, mb);
2021-01-15 09:41:09 +03:00
return;
2021-05-21 09:21:14 +03:00
default:
Fatal(ctx) << mb->name << ": unknown file type";
2020-12-22 14:32:49 +03:00
}
2021-01-15 09:41:09 +03:00
}
2020-12-22 14:32:49 +03:00
2021-03-29 10:48:23 +03:00
switch (get_file_type(ctx, mb)) {
2021-01-15 09:41:09 +03:00
case FileType::OBJ:
2021-04-06 06:08:54 +03:00
if (ObjectFile<E> *obj = ctx.obj_cache.get_one(mb))
2021-03-29 07:20:51 +03:00
ctx.objs.push_back(obj);
2020-12-24 11:37:02 +03:00
else
2021-03-29 08:36:55 +03:00
ctx.objs.push_back(new_object_file(ctx, mb, ""));
2020-12-20 14:58:22 +03:00
return;
2021-01-09 13:21:28 +03:00
case FileType::DSO:
2021-04-06 06:08:54 +03:00
if (SharedFile<E> *obj = ctx.dso_cache.get_one(mb))
2021-03-29 07:20:51 +03:00
ctx.dsos.push_back(obj);
2021-01-15 09:50:10 +03:00
else
2021-03-29 08:36:55 +03:00
ctx.dsos.push_back(new_shared_file(ctx, mb));
ctx.visited.insert(mb->name);
2020-12-20 14:58:22 +03:00
return;
2021-04-06 06:08:54 +03:00
case FileType::AR: {
std::vector<ObjectFile<E> *> objs = ctx.obj_cache.get(mb);
if (!objs.empty()) {
2021-03-29 07:20:51 +03:00
append(ctx.objs, objs);
2020-12-24 11:37:02 +03:00
} else {
2021-03-29 14:29:57 +03:00
for (MemoryMappedFile<E> *child : read_fat_archive_members(ctx, mb))
2021-03-29 10:48:23 +03:00
if (get_file_type(ctx, child) == FileType::OBJ)
2021-03-29 08:36:55 +03:00
ctx.objs.push_back(new_object_file(ctx, child, mb->name));
2020-12-22 14:10:04 +03:00
}
2021-03-29 08:36:55 +03:00
ctx.visited.insert(mb->name);
2020-12-22 14:10:04 +03:00
return;
2021-04-06 06:08:54 +03:00
}
2021-01-09 13:21:28 +03:00
case FileType::THIN_AR:
2021-03-29 14:29:57 +03:00
for (MemoryMappedFile<E> *child : read_thin_archive_members(ctx, mb)) {
2021-04-06 06:08:54 +03:00
if (ObjectFile<E> *obj = ctx.obj_cache.get_one(child))
2021-03-29 07:20:51 +03:00
ctx.objs.push_back(obj);
2021-03-29 10:48:23 +03:00
else if (get_file_type(ctx, child) == FileType::OBJ)
2021-03-29 08:36:55 +03:00
ctx.objs.push_back(new_object_file(ctx, child, mb->name));
2020-12-22 14:10:04 +03:00
}
2021-03-29 08:36:55 +03:00
ctx.visited.insert(mb->name);
return;
2021-01-09 13:21:28 +03:00
case FileType::TEXT:
2021-03-29 08:36:55 +03:00
parse_linker_script(ctx, mb);
return;
2021-05-21 09:21:14 +03:00
default:
Fatal(ctx) << mb->name << ": unknown file type";
}
2020-10-10 06:47:12 +03:00
}
2021-03-29 14:29:57 +03:00
template <typename E>
2020-12-24 14:15:17 +03:00
void cleanup() {
2021-03-29 14:29:57 +03:00
if (OutputFile<E>::tmpfile)
unlink(OutputFile<E>::tmpfile);
2020-12-25 07:34:12 +03:00
if (socket_tmpfile)
unlink(socket_tmpfile);
2020-12-24 14:15:17 +03:00
}
2021-03-29 14:29:57 +03:00
template <typename E>
2020-12-25 07:34:12 +03:00
static void signal_handler(int) {
2021-03-29 14:29:57 +03:00
cleanup<E>();
2020-12-24 08:39:02 +03:00
_exit(1);
}
// Read the beginning of a given file and returns its machine type
// (e.g. EM_X86_64 or EM_386). Return -1 if unknown.
template <typename E>
static i64 get_machine_type(Context<E> &ctx, MemoryMappedFile<E> *mb) {
switch (get_file_type(ctx, mb)) {
case FileType::DSO:
return ((ElfEhdr<E> *)mb->data(ctx))->e_machine;
case FileType::AR:
for (MemoryMappedFile<E> *child : read_fat_archive_members(ctx, mb))
if (get_file_type(ctx, child) == FileType::OBJ)
return ((ElfEhdr<E> *)child->data(ctx))->e_machine;
return -1;
case FileType::THIN_AR:
for (MemoryMappedFile<E> *child : read_thin_archive_members(ctx, mb))
if (get_file_type(ctx, child) == FileType::OBJ)
return ((ElfEhdr<E> *)child->data(ctx))->e_machine;
return -1;
case FileType::TEXT:
return get_script_output_type(ctx, mb);
default:
return -1;
}
}
template <typename E>
static MemoryMappedFile<E> *open_library(Context<E> &ctx, std::string path) {
MemoryMappedFile<E> *mb = MemoryMappedFile<E>::open(ctx, path);
if (!mb)
return nullptr;
i64 type = get_machine_type(ctx, mb);
if (type == -1 || type == E::e_machine)
return mb;
Warn(ctx) << path << ": skipping incompatible file";
return nullptr;
}
2021-03-29 14:29:57 +03:00
template <typename E>
MemoryMappedFile<E> *find_library(Context<E> &ctx, std::string name) {
2021-03-27 17:15:59 +03:00
if (name.starts_with(':')) {
2021-03-29 08:36:55 +03:00
for (std::string_view dir : ctx.arg.library_paths) {
2021-03-29 07:20:51 +03:00
std::string root = dir.starts_with("/") ? ctx.arg.sysroot : "";
2021-03-27 17:15:59 +03:00
std::string path = root + std::string(dir) + "/" + name.substr(1);
if (MemoryMappedFile<E> *mb = open_library(ctx, path))
2021-03-27 17:15:59 +03:00
return mb;
}
2021-03-29 10:48:23 +03:00
Fatal(ctx) << "library not found: " << name;
2021-03-27 17:15:59 +03:00
}
2021-03-29 08:36:55 +03:00
for (std::string_view dir : ctx.arg.library_paths) {
2021-03-29 07:20:51 +03:00
std::string root = dir.starts_with("/") ? ctx.arg.sysroot : "";
2020-12-10 07:44:58 +03:00
std::string stem = root + std::string(dir) + "/lib" + name;
2021-03-29 08:36:55 +03:00
if (!ctx.is_static)
if (MemoryMappedFile<E> *mb = open_library(ctx, stem + ".so"))
2020-12-22 11:37:49 +03:00
return mb;
if (MemoryMappedFile<E> *mb = open_library(ctx, stem + ".a"))
2020-12-22 11:37:49 +03:00
return mb;
2020-11-19 12:03:26 +03:00
}
2021-03-29 10:48:23 +03:00
Fatal(ctx) << "library not found: " << name;
2020-11-19 12:03:26 +03:00
}
2021-03-29 14:29:57 +03:00
template <typename E>
static void read_input_files(Context<E> &ctx, std::span<std::string_view> args) {
2021-03-16 15:57:28 +03:00
std::vector<std::tuple<bool, bool, bool>> state;
2021-03-12 11:37:39 +03:00
2020-12-22 14:12:41 +03:00
while (!args.empty()) {
std::string_view arg;
if (read_flag(args, "as-needed")) {
2021-03-29 08:36:55 +03:00
ctx.as_needed = true;
2020-12-22 14:12:41 +03:00
} else if (read_flag(args, "no-as-needed")) {
2021-03-29 08:36:55 +03:00
ctx.as_needed = false;
2021-02-09 16:07:44 +03:00
} else if (read_flag(args, "whole-archive")) {
2021-03-29 08:36:55 +03:00
ctx.whole_archive = true;
2021-02-09 16:07:44 +03:00
} else if (read_flag(args, "no-whole-archive")) {
2021-03-29 08:36:55 +03:00
ctx.whole_archive = false;
2021-03-16 15:57:28 +03:00
} else if (read_flag(args, "Bstatic")) {
2021-03-29 08:36:55 +03:00
ctx.is_static = true;
2021-03-16 15:57:28 +03:00
} else if (read_flag(args, "Bdynamic")) {
2021-03-29 08:36:55 +03:00
ctx.is_static = false;
2021-04-09 12:50:24 +03:00
} else if (read_arg(ctx, args, arg, "version-script")) {
parse_version_script(ctx, std::string(arg));
} else if (read_arg(ctx, args, arg, "dynamic-list")) {
parse_dynamic_list(ctx, std::string(arg));
2021-03-12 11:37:39 +03:00
} else if (read_flag(args, "push-state")) {
2021-03-29 08:36:55 +03:00
state.push_back({ctx.as_needed, ctx.whole_archive, ctx.is_static});
2021-03-12 11:37:39 +03:00
} else if (read_flag(args, "pop-state")) {
if (state.empty())
2021-03-29 10:48:23 +03:00
Fatal(ctx) << "no state pushed before popping";
2021-03-29 08:36:55 +03:00
std::tie(ctx.as_needed, ctx.whole_archive, ctx.is_static) = state.back();
2021-03-12 11:37:39 +03:00
state.pop_back();
2021-03-29 10:48:23 +03:00
} else if (read_arg(ctx, args, arg, "l")) {
2021-03-29 14:29:57 +03:00
MemoryMappedFile<E> *mb = find_library(ctx, std::string(arg));
mb->given_fullpath = false;
2021-03-29 08:36:55 +03:00
read_file(ctx, mb);
2020-12-22 14:12:41 +03:00
} else {
2021-03-29 14:29:57 +03:00
read_file(ctx, MemoryMappedFile<E>::must_open(ctx, std::string(args[0])));
2020-12-22 14:12:41 +03:00
args = args.subspan(1);
}
}
}
2021-03-29 14:29:57 +03:00
template <typename E>
static void show_stats(Context<E> &ctx) {
for (ObjectFile<E> *obj : ctx.objs) {
2020-12-21 11:51:20 +03:00
static Counter defined("defined_syms");
2021-01-29 15:44:46 +03:00
defined += obj->first_global - 1;
2020-12-21 11:51:20 +03:00
static Counter undefined("undefined_syms");
2021-01-29 15:44:46 +03:00
undefined += obj->symbols.size() - obj->first_global;
2021-03-30 17:04:54 +03:00
2021-04-06 08:36:31 +03:00
for (std::unique_ptr<InputSection<E>> &sec : obj->sections) {
2021-04-06 07:37:01 +03:00
if (!sec || !sec->is_alive)
2021-03-30 17:04:54 +03:00
continue;
static Counter alloc("reloc_alloc");
static Counter nonalloc("reloc_nonalloc");
if (sec->shdr.sh_flags & SHF_ALLOC)
2021-04-01 19:35:10 +03:00
alloc += sec->get_rels(ctx).size();
2021-03-30 17:04:54 +03:00
else
2021-04-01 19:35:10 +03:00
nonalloc += sec->get_rels(ctx).size();
2021-03-30 17:04:54 +03:00
}
2021-04-06 08:42:58 +03:00
2021-05-01 13:50:22 +03:00
static Counter comdats("comdats");
comdats += obj->comdat_groups.size();
static Counter removed_comdats("removed_comdat_mem");
2021-04-06 08:42:58 +03:00
for (auto &pair : obj->comdat_groups)
if (ComdatGroup *group = pair.first; group->owner != obj->priority)
2021-05-01 13:50:22 +03:00
removed_comdats += pair.second.size();
2020-12-21 11:51:20 +03:00
2021-04-17 14:23:25 +03:00
static Counter num_cies("num_cies");
2021-04-15 15:48:30 +03:00
num_cies += obj->cies.size();
2021-04-17 14:23:25 +03:00
static Counter num_unique_cies("num_unique_cies");
2021-04-15 15:48:30 +03:00
for (CieRecord<E> &cie : obj->cies)
if (cie.is_leader)
num_unique_cies++;
2021-04-14 17:15:16 +03:00
2021-04-17 14:23:25 +03:00
static Counter num_fdes("num_fdes");
2021-04-15 15:48:30 +03:00
num_fdes += obj->fdes.size();
2021-04-14 17:15:16 +03:00
}
2021-04-18 07:30:55 +03:00
static Counter num_bytes("total_input_bytes");
for (std::unique_ptr<MemoryMappedFile<E>> &mb : ctx.owning_mbs)
num_bytes += mb->size();
2021-04-17 14:23:25 +03:00
static Counter num_input_sections("input_sections");
2021-03-29 14:29:57 +03:00
for (ObjectFile<E> *file : ctx.objs)
2021-01-29 15:44:46 +03:00
num_input_sections += file->sections.size();
2020-12-21 11:51:20 +03:00
2021-04-17 14:23:25 +03:00
static Counter num_output_chunks("output_chunks", ctx.chunks.size());
static Counter num_objs("num_objs", ctx.objs.size());
static Counter num_dsos("num_dsos", ctx.dsos.size());
2020-12-21 11:51:20 +03:00
Counter::print();
}
2021-03-29 14:29:57 +03:00
template <typename E>
int do_main(int argc, char **argv) {
Context<E> ctx;
2021-03-29 10:48:23 +03:00
2021-03-26 16:58:28 +03:00
// Process -run option first. process_run_subcommand() does not return.
2021-03-25 10:03:23 +03:00
if (argc >= 2)
if (std::string_view arg = argv[1]; arg == "-run" || arg == "--run")
2021-03-29 10:48:23 +03:00
process_run_subcommand(ctx, argc, argv);
2021-03-25 10:03:23 +03:00
2021-04-07 09:38:31 +03:00
Timer t_all(ctx, "all");
2020-12-21 10:32:43 +03:00
// Parse non-positional command line options
2021-05-08 11:50:09 +03:00
ctx.cmdline_args = expand_response_files(ctx, argv);
2020-12-21 10:32:43 +03:00
std::vector<std::string_view> file_args;
2021-03-29 08:05:55 +03:00
parse_nonpositional_args(ctx, file_args);
2020-12-21 10:32:43 +03:00
if (ctx.arg.relocatable) {
combine_objects(ctx, file_args);
return 0;
}
2021-03-29 07:20:51 +03:00
if (!ctx.arg.preload)
2021-05-29 09:00:18 +03:00
try_resume_daemon(ctx);
2020-12-24 17:14:37 +03:00
2021-01-22 05:08:05 +03:00
tbb::global_control tbb_cont(tbb::global_control::max_allowed_parallelism,
2021-03-29 07:20:51 +03:00
ctx.arg.thread_count);
2021-01-22 05:08:05 +03:00
2021-03-29 14:29:57 +03:00
signal(SIGINT, signal_handler<E>);
signal(SIGTERM, signal_handler<E>);
2020-12-24 17:14:37 +03:00
2021-05-26 16:48:53 +03:00
if (!ctx.arg.directory.empty() && chdir(ctx.arg.directory.c_str()) == -1)
Fatal(ctx) << "chdir failed: " << ctx.arg.directory
<< ": " << strerror(errno);
2021-04-09 10:05:52 +03:00
2021-05-26 16:48:53 +03:00
// Handle --wrap options if any.
2021-05-21 19:12:24 +03:00
for (std::string_view name : ctx.arg.wrap)
Symbol<E>::intern(ctx, name)->wrap = true;
2021-05-21 19:12:24 +03:00
2021-05-26 16:48:53 +03:00
// Handle --retain-symbols-file options if any.
if (ctx.arg.retain_symbols_file)
for (std::string_view name : *ctx.arg.retain_symbols_file)
Symbol<E>::intern(ctx, name)->write_to_symtab = true;
2021-05-26 16:48:53 +03:00
2020-12-24 17:25:07 +03:00
// Preload input files
std::function<void()> on_complete;
2021-03-29 07:20:51 +03:00
if (ctx.arg.preload) {
2021-04-07 09:38:31 +03:00
Timer t(ctx, "preload");
2021-01-09 15:14:52 +03:00
std::function<void()> wait_for_client;
2021-05-29 09:00:18 +03:00
daemonize(ctx, &wait_for_client, &on_complete);
2021-03-12 05:45:52 +03:00
2021-03-29 08:36:55 +03:00
ctx.reset_reader_context(true);
read_input_files(ctx, file_args);
ctx.tg.wait();
2021-03-15 08:58:44 +03:00
t.stop();
2021-03-12 05:45:52 +03:00
2021-04-07 09:38:31 +03:00
Timer t2(ctx, "wait_for_client");
2021-01-09 15:14:52 +03:00
wait_for_client();
2021-03-29 07:20:51 +03:00
} else if (ctx.arg.fork) {
2020-12-20 03:20:24 +03:00
on_complete = fork_child();
2021-01-09 15:14:52 +03:00
}
2021-03-29 07:20:51 +03:00
for (std::string_view arg : ctx.arg.trace_symbol)
2021-03-29 17:50:19 +03:00
Symbol<E>::intern(ctx, arg)->traced = true;
2020-12-21 10:32:43 +03:00
2020-12-20 03:20:24 +03:00
// Parse input files
{
2021-04-07 09:38:31 +03:00
Timer t(ctx, "parse");
2021-03-29 08:36:55 +03:00
ctx.reset_reader_context(false);
read_input_files(ctx, file_args);
ctx.tg.wait();
2020-12-21 10:32:43 +03:00
}
2020-12-17 15:36:38 +03:00
2021-04-05 14:26:32 +03:00
if (ctx.objs.empty())
Fatal(ctx) << "no input files";
2021-05-08 18:17:57 +03:00
// Uniquify shared object files by soname
2020-11-20 07:54:29 +03:00
{
2020-12-11 06:46:03 +03:00
std::unordered_set<std::string_view> seen;
2021-04-17 18:54:02 +03:00
erase(ctx.dsos, [&](SharedFile<E> *file) {
return !seen.insert(file->soname).second;
});
2020-11-20 07:54:29 +03:00
}
2021-04-07 09:38:31 +03:00
Timer t_total(ctx, "total");
Timer t_before_copy(ctx, "before_copy");
2020-11-06 10:58:13 +03:00
2021-03-17 19:09:28 +03:00
// Apply -exclude-libs
2021-03-29 10:24:40 +03:00
apply_exclude_libs(ctx);
2021-03-17 19:09:28 +03:00
2021-03-13 05:13:37 +03:00
// Create instances of linker-synthesized sections such as
2021-03-10 21:04:00 +03:00
// .got or .plt.
2021-03-29 10:24:40 +03:00
create_synthetic_sections(ctx);
2020-11-20 06:44:02 +03:00
2020-11-11 04:42:26 +03:00
// Resolve symbols and fix the set of object files that are
// included to the final output.
2021-06-19 14:29:16 +03:00
resolve_symbols(ctx);
2020-10-19 15:50:33 +03:00
2020-11-11 04:42:26 +03:00
// Remove redundant comdat sections (e.g. duplicate inline functions).
2021-03-29 10:24:40 +03:00
eliminate_comdats(ctx);
2020-10-10 06:47:12 +03:00
2020-10-27 06:50:25 +03:00
// Create .bss sections for common symbols.
2021-03-29 10:24:40 +03:00
convert_common_symbols(ctx);
2020-10-27 06:50:25 +03:00
2021-03-13 09:30:19 +03:00
// Apply version scripts.
2021-03-29 10:24:40 +03:00
apply_version_script(ctx);
2021-03-13 09:30:19 +03:00
// Parse symbol version suffixes (e.g. "foo@ver1").
2021-03-29 10:24:40 +03:00
parse_symbol_version(ctx);
2021-03-13 09:30:19 +03:00
// Set is_import and is_export bits for each symbol.
2021-03-29 10:24:40 +03:00
compute_import_export(ctx);
2021-03-13 09:30:19 +03:00
2021-01-24 10:12:34 +03:00
// Garbage-collect unreachable sections.
2021-03-29 07:20:51 +03:00
if (ctx.arg.gc_sections)
2021-03-29 10:28:39 +03:00
gc_sections(ctx);
2021-01-24 10:12:34 +03:00
2021-01-27 12:18:11 +03:00
// Merge identical read-only sections.
2021-03-29 07:20:51 +03:00
if (ctx.arg.icf)
2021-03-29 10:27:16 +03:00
icf_sections(ctx);
2021-01-27 12:18:11 +03:00
2021-03-13 14:36:23 +03:00
// Compute sizes of sections containing mergeable strings.
2021-03-29 10:24:40 +03:00
compute_merged_section_sizes(ctx);
2021-01-25 07:33:16 +03:00
2021-03-29 10:24:40 +03:00
// ctx input sections into output sections
bin_sections(ctx);
2020-10-23 04:27:11 +03:00
2021-03-13 06:44:37 +03:00
// Get a list of output sections.
2021-03-29 14:29:57 +03:00
append(ctx.chunks, collect_output_sections(ctx));
2020-11-07 14:31:09 +03:00
2020-11-04 04:39:17 +03:00
// Create a dummy file containing linker-synthesized symbols
// (e.g. `__bss_start`).
2021-04-06 11:08:28 +03:00
ctx.internal_obj = ObjectFile<E>::create_internal_file(ctx);
2021-03-29 09:16:53 +03:00
ctx.internal_obj->resolve_regular_symbols(ctx);
2021-03-29 07:20:51 +03:00
ctx.objs.push_back(ctx.internal_obj);
2020-11-04 04:39:17 +03:00
2021-03-29 07:20:51 +03:00
// Beyond this point, no new files will be added to ctx.objs
// or ctx.dsos.
2021-03-13 06:44:37 +03:00
2020-11-20 05:08:06 +03:00
// Convert weak symbols to absolute symbols with value 0.
2021-03-29 10:24:40 +03:00
convert_undefined_weak_symbols(ctx);
2020-11-20 05:08:06 +03:00
// If we do not handle undefined symbols as errors, such symbols
// are converted to absolute symbols with value 0.
if (ctx.arg.unresolved_symbols != UnresolvedKind::ERROR) {
tbb::parallel_for_each(ctx.objs, [&](ObjectFile<E> *file) {
file->ignore_unresolved_symbols(ctx);
});
}
2021-02-25 14:26:10 +03:00
// If we are linking a .so file, remaining undefined symbols does
// not cause a linker error. Instead, they are treated as if they
// were imported symbols.
if (ctx.arg.shared && !ctx.arg.z_defs) {
2021-04-07 09:38:31 +03:00
Timer t(ctx, "claim_unresolved_symbols");
tbb::parallel_for_each(ctx.objs, [&](ObjectFile<E> *file) {
file->claim_unresolved_symbols(ctx);
2021-02-25 14:26:10 +03:00
});
}
2020-11-09 05:31:00 +03:00
// Beyond this point, no new symbols will be added to the result.
2021-01-23 04:46:27 +03:00
// Make sure that all symbols have been resolved.
2021-03-29 07:20:51 +03:00
if (!ctx.arg.allow_multiple_definition)
2021-03-29 10:24:40 +03:00
check_duplicate_symbols(ctx);
2021-01-23 04:46:27 +03:00
2021-05-30 19:10:00 +03:00
// .init_array and .fini_array contents have to be sorted by
// a special rule. Sort them.
sort_init_fini(ctx);
2021-03-17 19:09:28 +03:00
// Compute sizes of output sections while assigning offsets
// within an output section to input sections.
2021-03-29 10:24:40 +03:00
compute_section_sizes(ctx);
2021-03-17 19:09:28 +03:00
// Sort sections by section attributes so that we'll have to
// create as few segments as possible.
2021-03-29 14:29:57 +03:00
sort(ctx.chunks, [&](OutputChunk<E> *a, OutputChunk<E> *b) {
2021-03-29 10:24:40 +03:00
return get_section_rank(ctx, a) < get_section_rank(ctx, b);
2021-03-17 19:09:28 +03:00
});
2021-03-15 18:34:09 +03:00
// Copy string referred by .dynamic to .dynstr.
2021-03-29 14:29:57 +03:00
for (SharedFile<E> *file : ctx.dsos)
2021-03-29 07:20:51 +03:00
ctx.dynstr->add_string(file->soname);
for (std::string_view str : ctx.arg.auxiliary)
ctx.dynstr->add_string(str);
for (std::string_view str : ctx.arg.filter)
ctx.dynstr->add_string(str);
if (!ctx.arg.rpaths.empty())
ctx.dynstr->add_string(ctx.arg.rpaths);
if (!ctx.arg.soname.empty())
ctx.dynstr->add_string(ctx.arg.soname);
2021-03-01 07:17:52 +03:00
2020-11-29 05:06:11 +03:00
// Scan relocations to find symbols that need entries in .got, .plt,
// .got.plt, .dynsym, .dynstr, etc.
2021-03-29 10:24:40 +03:00
scan_rels(ctx);
2020-11-20 06:44:02 +03:00
2021-06-06 08:52:46 +03:00
// Reserve a space for dynamic symbol strings in .dynstr and sort
// .dynsym contents if necessary. Beyond this point, no symbol will
// be added to .dynsym.
ctx.dynsym->finalize(ctx);
2021-01-14 11:23:14 +03:00
2021-03-07 07:43:58 +03:00
// Fill .gnu.version_d section contents.
2021-04-17 10:23:37 +03:00
ctx.verdef->construct(ctx);
2021-03-07 07:43:58 +03:00
2021-03-06 14:49:40 +03:00
// Fill .gnu.version_r section contents.
2021-04-17 10:23:37 +03:00
ctx.verneed->construct(ctx);
2020-11-29 05:40:57 +03:00
2020-11-29 12:58:36 +03:00
// Compute .symtab and .strtab sizes for each file.
2021-01-25 10:26:00 +03:00
{
2021-04-07 09:38:31 +03:00
Timer t(ctx, "compute_symtab");
2021-03-29 14:29:57 +03:00
tbb::parallel_for_each(ctx.objs, [&](ObjectFile<E> *file) {
2021-03-29 09:16:53 +03:00
file->compute_symtab(ctx);
2021-01-25 10:26:00 +03:00
});
}
2020-11-29 12:58:36 +03:00
2021-01-20 16:41:32 +03:00
// .eh_frame is a special section from the linker's point of view,
2021-03-10 20:48:36 +03:00
// as its contents are parsed and reconstructed by the linker,
// unlike other sections that are regarded as opaque bytes.
2021-01-20 16:41:32 +03:00
// Here, we transplant .eh_frame sections from a regular output
// section to the special EHFrameSection.
{
2021-04-07 09:38:31 +03:00
Timer t(ctx, "eh_frame");
2021-03-29 14:29:57 +03:00
erase(ctx.chunks, [](OutputChunk<E> *chunk) {
return chunk->kind == OutputChunk<E>::REGULAR &&
2021-03-10 20:48:36 +03:00
chunk->name == ".eh_frame";
2021-01-20 16:41:32 +03:00
});
2021-03-29 10:02:12 +03:00
ctx.eh_frame->construct(ctx);
2021-01-20 16:41:32 +03:00
}
2020-11-19 10:20:09 +03:00
// Now that we have computed sizes for all sections and assigned
// section indices to them, so we can fix section header contents
// for all output sections.
2021-03-29 14:29:57 +03:00
for (OutputChunk<E> *chunk : ctx.chunks)
2021-03-29 09:53:00 +03:00
chunk->update_shdr(ctx);
2020-12-07 10:12:19 +03:00
2021-03-29 14:29:57 +03:00
erase(ctx.chunks, [](OutputChunk<E> *chunk) {
return chunk->kind == OutputChunk<E>::SYNTHETIC &&
2021-03-14 10:36:56 +03:00
chunk->shdr.sh_size == 0;
});
2020-12-07 10:12:19 +03:00
// Set section indices.
2021-03-29 07:20:51 +03:00
for (i64 i = 0, shndx = 1; i < ctx.chunks.size(); i++)
2021-03-29 14:29:57 +03:00
if (ctx.chunks[i]->kind != OutputChunk<E>::HEADER)
2021-03-29 07:20:51 +03:00
ctx.chunks[i]->shndx = shndx++;
2020-12-07 10:12:19 +03:00
2021-03-29 14:29:57 +03:00
for (OutputChunk<E> *chunk : ctx.chunks)
2021-03-29 09:53:00 +03:00
chunk->update_shdr(ctx);
2020-11-16 18:45:02 +03:00
2020-10-30 10:55:59 +03:00
// Assign offsets to output sections
2021-03-29 10:24:40 +03:00
i64 filesize = set_osec_offsets(ctx);
2020-10-19 17:37:29 +03:00
2021-01-23 04:46:27 +03:00
// Fix linker-synthesized symbol addresses.
2021-03-29 10:24:40 +03:00
fix_synthetic_symbols(ctx);
2021-01-23 04:46:27 +03:00
2021-05-06 05:29:02 +03:00
// If --compress-debug-sections is given, compress .debug_* sections
// using zlib.
2021-06-03 16:22:29 +03:00
if (ctx.arg.compress_debug_sections != COMPRESS_NONE) {
2021-05-06 05:29:02 +03:00
compress_debug_sections(ctx);
filesize = set_osec_offsets(ctx);
}
// At this point, file layout is fixed.
2021-03-17 19:09:28 +03:00
// Beyond this, you can assume that symbol addresses including their
// GOT or PLT addresses have a correct final value.
2021-01-14 09:26:40 +03:00
// Some types of relocations for TLS symbols need the TLS segment
// address. Find it out now.
2021-03-29 15:05:52 +03:00
for (ElfPhdr<E> phdr : create_phdr(ctx)) {
2021-01-14 07:19:21 +03:00
if (phdr.p_type == PT_TLS) {
2021-03-29 07:20:51 +03:00
ctx.tls_begin = phdr.p_vaddr;
ctx.tls_end = align_to(phdr.p_vaddr + phdr.p_memsz, phdr.p_align);
2021-01-23 04:46:27 +03:00
break;
2021-01-14 07:19:21 +03:00
}
}
2020-10-26 08:38:43 +03:00
2020-12-11 10:51:20 +03:00
t_before_copy.stop();
2020-11-09 10:41:26 +03:00
// Create an output file
ctx.output_file = OutputFile<E>::open(ctx, ctx.arg.output, filesize, 0777);
2021-04-28 12:02:32 +03:00
ctx.buf = ctx.output_file->buf;
2020-11-09 10:41:26 +03:00
2021-04-07 09:38:31 +03:00
Timer t_copy(ctx, "copy");
2020-12-11 10:51:20 +03:00
2020-11-17 08:49:07 +03:00
// Copy input sections to the output file
2020-11-12 16:06:47 +03:00
{
2021-04-07 09:38:31 +03:00
Timer t(ctx, "copy_buf");
2021-04-04 12:46:43 +03:00
2021-03-29 14:29:57 +03:00
tbb::parallel_for_each(ctx.chunks, [&](OutputChunk<E> *chunk) {
2021-04-04 12:46:43 +03:00
std::string name(chunk->name);
if (name.empty())
name = "(header)";
2021-04-07 09:38:31 +03:00
Timer t2(ctx, name, &t);
2021-04-04 12:46:43 +03:00
2021-03-29 09:53:00 +03:00
chunk->copy_buf(ctx);
});
2021-04-04 12:46:43 +03:00
2021-03-29 14:29:57 +03:00
Error<E>::checkpoint(ctx);
2020-10-30 05:40:38 +03:00
}
2020-10-20 03:20:52 +03:00
2021-03-17 14:57:09 +03:00
// Dynamic linker works better with sorted .rela.dyn section,
// so we sort them.
2021-03-29 10:05:05 +03:00
ctx.reldyn->sort(ctx);
2021-03-17 14:57:09 +03:00
2020-11-09 15:50:47 +03:00
// Zero-clear paddings between sections
2021-05-05 17:37:48 +03:00
clear_padding(ctx);
2020-11-09 15:50:47 +03:00
2021-03-29 07:20:51 +03:00
if (ctx.buildid) {
2021-04-07 09:38:31 +03:00
Timer t(ctx, "build_id");
2021-05-05 17:37:48 +03:00
ctx.buildid->write_buildid(ctx);
2021-01-24 05:41:36 +03:00
}
2021-03-07 16:38:09 +03:00
t_copy.stop();
2021-03-10 20:48:36 +03:00
// Commit
2021-04-28 12:02:32 +03:00
ctx.output_file->close(ctx);
2020-10-14 12:41:09 +03:00
2020-12-11 11:04:19 +03:00
t_total.stop();
2020-12-11 10:51:20 +03:00
t_all.stop();
2020-11-06 10:58:13 +03:00
2021-03-29 07:20:51 +03:00
if (ctx.arg.print_map)
2021-03-29 10:48:23 +03:00
print_map(ctx);
2020-10-29 06:24:54 +03:00
2020-12-12 06:57:56 +03:00
// Show stats numbers
2021-03-29 07:20:51 +03:00
if (ctx.arg.stats)
2021-03-29 10:24:40 +03:00
show_stats(ctx);
2020-12-13 19:28:43 +03:00
2021-03-29 07:20:51 +03:00
if (ctx.arg.perf)
2021-04-07 09:38:31 +03:00
Timer<E>::print(ctx);
2020-12-13 16:41:50 +03:00
std::cout << std::flush;
std::cerr << std::flush;
2020-12-21 12:52:45 +03:00
if (on_complete)
on_complete();
2021-01-22 15:29:28 +03:00
2021-03-29 07:20:51 +03:00
if (ctx.arg.quick_exit)
2021-01-22 15:29:28 +03:00
std::quick_exit(0);
2021-04-06 13:20:17 +03:00
for (std::function<void()> &fn : ctx.on_exit)
fn();
2021-01-22 15:29:28 +03:00
return 0;
2020-09-29 09:05:29 +03:00
}
2021-03-29 14:29:57 +03:00
2021-03-30 13:27:00 +03:00
enum class MachineType { X86_64, I386 };
static MachineType get_machine_type(int argc, char **argv) {
for (i64 i = 1; i < argc; i++) {
if (std::string_view(argv[i]) == "-m") {
if (i + 1 == argc)
break;
i++;
std::string_view val(argv[i]);
if (val == "elf_x86_64")
return MachineType::X86_64;
if (val == "elf_i386")
return MachineType::I386;
2021-05-10 10:53:04 +03:00
std::cerr << "unknown -m argument: " << val << "\n";
2021-03-30 13:27:00 +03:00
exit(1);
}
}
return MachineType::X86_64;
// std::cerr << "-m is missing";
// exit(1);
}
2021-03-29 14:29:57 +03:00
int main(int argc, char **argv) {
2021-03-30 13:27:00 +03:00
switch (get_machine_type(argc, argv)) {
case MachineType::X86_64:
return do_main<X86_64>(argc, argv);
case MachineType::I386:
return do_main<I386>(argc, argv);
}
2021-03-29 14:29:57 +03:00
}
2021-04-06 12:18:45 +03:00
#define INSTANTIATE(E) \
template void read_file(Context<E> &, MemoryMappedFile<E> *); \
template std::string_view save_string(Context<E> &, const std::string &)
INSTANTIATE(X86_64);
INSTANTIATE(I386);