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

591 lines
17 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-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>
2020-11-20 07:54:29 +03:00
#include <unordered_set>
2020-09-29 09:05:29 +03:00
2021-03-29 14:29:57 +03:00
template <typename E>
static bool is_text_file(Context<E> &ctx, MemoryMappedFile<E> *mb) {
2021-03-29 10:48:23 +03:00
u8 *data = mb->data(ctx);
2020-12-22 11:37:49 +03:00
return mb->size() >= 4 &&
2021-03-29 10:48:23 +03:00
isprint(data[0]) &&
isprint(data[1]) &&
isprint(data[2]) &&
isprint(data[3]);
2020-12-12 07:08:40 +03:00
}
2020-12-10 16:51:38 +03:00
2021-01-09 13:21:28 +03:00
enum class FileType { UNKNOWN, OBJ, DSO, AR, THIN_AR, TEXT };
2020-12-20 14:58:22 +03:00
2021-03-29 14:29:57 +03:00
template <typename E>
static FileType get_file_type(Context<E> &ctx, MemoryMappedFile<E> *mb) {
2021-03-29 10:48:23 +03:00
u8 *data = mb->data(ctx);
if (mb->size() >= 20 && memcmp(data, "\177ELF", 4) == 0) {
2021-03-29 15:02:12 +03:00
ElfEhdr<E> &ehdr = *(ElfEhdr<E> *)data;
2020-12-20 14:58:22 +03:00
if (ehdr.e_type == ET_REL)
2021-01-09 13:21:28 +03:00
return FileType::OBJ;
2020-12-20 14:58:22 +03:00
if (ehdr.e_type == ET_DYN)
2021-01-09 13:21:28 +03:00
return FileType::DSO;
return FileType::UNKNOWN;
2020-12-20 14:58:22 +03:00
}
2021-03-29 10:48:23 +03:00
if (mb->size() >= 8 && memcmp(data, "!<arch>\n", 8) == 0)
2021-01-09 13:21:28 +03:00
return FileType::AR;
2021-03-29 10:48:23 +03:00
if (mb->size() >= 8 && memcmp(data, "!<thin>\n", 8) == 0)
2021-01-09 13:21:28 +03:00
return FileType::THIN_AR;
2021-03-29 10:48:23 +03:00
if (is_text_file(ctx, mb))
2021-01-09 13:21:28 +03:00
return FileType::TEXT;
return FileType::UNKNOWN;
2020-12-20 14:58:22 +03:00
}
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-03-29 14:29:57 +03:00
ObjectFile<E> *file = new ObjectFile<E>(ctx, mb, archive_name, in_lib);
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) {
SharedFile<E> *file = new SharedFile<E>(ctx, mb);
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;
2020-12-22 14:32:49 +03:00
}
2021-03-29 10:48:23 +03:00
Fatal(ctx) << mb->name << ": unknown file type";
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-03-29 10:48:23 +03:00
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);
}
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);
2021-03-29 14:29:57 +03:00
if (MemoryMappedFile<E> *mb = MemoryMappedFile<E>::open(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)
2021-03-29 14:29:57 +03:00
if (MemoryMappedFile<E> *mb = MemoryMappedFile<E>::open(stem + ".so"))
2020-12-22 11:37:49 +03:00
return mb;
2021-03-29 14:29:57 +03:00
if (MemoryMappedFile<E> *mb = MemoryMappedFile<E>::open(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-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));
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
for (InputSection<E> *sec : obj->sections) {
if (!sec)
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
}
2020-12-21 11:51:20 +03:00
}
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-03-29 07:20:51 +03:00
Counter num_output_chunks("output_chunks", ctx.chunks.size());
Counter num_objs("num_objs", ctx.objs.size());
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
2020-12-21 10:32:43 +03:00
Timer t_all("all");
// Parse non-positional command line options
2021-03-29 10:48:23 +03:00
ctx.cmdline_args = expand_response_files(ctx, argv + 1);
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
2021-03-29 07:20:51 +03:00
if (!ctx.arg.preload)
2021-03-29 10:48:23 +03:00
if (i64 code; resume_daemon(ctx, argv, &code))
2021-01-09 15:33:46 +03:00
exit(code);
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
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-03-15 08:58:44 +03:00
Timer t("preload");
2021-01-09 15:14:52 +03:00
std::function<void()> wait_for_client;
2021-03-29 10:48:23 +03:00
daemonize(ctx, argv, &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-03-15 08:58:44 +03:00
Timer t2("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-01-09 11:45:50 +03:00
Timer t("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";
2020-11-20 07:54:29 +03:00
// Uniquify shared object files with soname
{
2021-03-29 14:29:57 +03:00
std::vector<SharedFile<E> *> vec;
2020-12-11 06:46:03 +03:00
std::unordered_set<std::string_view> seen;
2021-03-29 14:29:57 +03:00
for (SharedFile<E> *file : ctx.dsos)
2020-11-29 16:05:34 +03:00
if (seen.insert(file->soname).second)
vec.push_back(file);
2021-03-29 07:20:51 +03:00
ctx.dsos = vec;
2020-11-20 07:54:29 +03:00
}
2020-12-11 10:51:20 +03:00
Timer t_total("total");
Timer t_before_copy("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
2021-03-17 19:09:28 +03:00
// Set unique indices to files.
2021-03-29 10:24:40 +03:00
set_file_priority(ctx);
2020-10-18 13:05:28 +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-03-29 10:24:40 +03:00
resolve_obj_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-03-29 14:29:57 +03:00
ctx.internal_obj = new ObjectFile<E>(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-13 05:13:37 +03:00
// Add symbols from shared object files.
2021-03-29 10:24:40 +03:00
resolve_dso_symbols(ctx);
2021-03-10 18:59:59 +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
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.
2021-03-29 07:20:51 +03:00
if (ctx.arg.shared && !ctx.arg.z_defs) {
2021-02-25 14:26:10 +03:00
Timer t("claim_unresolved_symbols");
2021-03-29 14:29:57 +03:00
tbb::parallel_for_each(ctx.objs, [](ObjectFile<E> *file) {
2021-02-25 14:26:10 +03:00
file->claim_unresolved_symbols();
});
}
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-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-03-17 19:09:28 +03:00
// Sort .dynsym contents. Beyond this point, no symbol will be
2021-01-18 10:36:13 +03:00
// added to .dynsym.
2021-03-29 10:02:12 +03:00
ctx.dynsym->sort_symbols(ctx);
2021-01-14 11:23:14 +03:00
2021-03-07 07:43:58 +03:00
// Fill .gnu.version_d section contents.
2021-03-29 10:24:40 +03:00
fill_verdef(ctx);
2021-03-07 07:43:58 +03:00
2021-03-06 14:49:40 +03:00
// Fill .gnu.version_r section contents.
2021-03-29 10:24:40 +03:00
fill_verneed(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
{
Timer t("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.
{
Timer t("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-03-17 19:09:28 +03:00
// At this point, file layout is fixed.
2020-11-09 03:58:35 +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-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
2021-03-29 14:29:57 +03:00
OutputFile<E> *file = OutputFile<E>::open(ctx, ctx.arg.output, filesize);
2021-03-29 07:20:51 +03:00
ctx.buf = file->buf;
2020-11-09 10:41:26 +03:00
2020-12-11 10:51:20 +03:00
Timer t_copy("copy");
2020-11-17 08:49:07 +03:00
// Copy input sections to the output file
2020-11-12 16:06:47 +03:00
{
2021-01-09 11:45:50 +03:00
Timer t("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)";
Timer t2(name, &t);
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-03-29 10:24:40 +03:00
clear_padding(ctx, filesize);
2020-11-09 15:50:47 +03:00
2021-03-29 07:20:51 +03:00
if (ctx.buildid) {
2021-01-24 05:41:36 +03:00
Timer t("build_id");
2021-03-29 09:53:00 +03:00
ctx.buildid->write_buildid(ctx, filesize);
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-03-29 10:48:23 +03:00
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)
2020-12-13 16:41:50 +03:00
Timer::print();
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);
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;
std::cerr << "unknown -m argument: " << val;
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-03-30 08:57:38 +03:00
template void read_file(Context<X86_64> &ctx, MemoryMappedFile<X86_64> *mb);