2020-10-20 08:54:35 +03:00
|
|
|
#include "mold.h"
|
2020-10-02 07:28:26 +03:00
|
|
|
|
2020-12-13 15:07:50 +03:00
|
|
|
#include "tbb/global_control.h"
|
2021-01-10 07:17:13 +03:00
|
|
|
#include "tbb/parallel_do.h"
|
2020-12-20 02:31:17 +03:00
|
|
|
#include "tbb/task_group.h"
|
2020-12-13 15:07:50 +03:00
|
|
|
|
2020-11-09 05:58:48 +03:00
|
|
|
#include <fcntl.h>
|
2020-12-21 13:35:33 +03:00
|
|
|
#include <functional>
|
2020-09-29 09:05:29 +03:00
|
|
|
#include <iostream>
|
2020-12-07 15:04:29 +03:00
|
|
|
#include <regex>
|
2020-12-24 08:39:02 +03:00
|
|
|
#include <signal.h>
|
2020-12-24 16:56:22 +03:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
2020-11-09 05:58:48 +03:00
|
|
|
#include <unistd.h>
|
2020-11-20 07:54:29 +03:00
|
|
|
#include <unordered_set>
|
2020-09-29 09:05:29 +03:00
|
|
|
|
2020-12-22 14:10:04 +03:00
|
|
|
static tbb::task_group parser_tg;
|
|
|
|
static bool preloading;
|
|
|
|
|
2020-12-22 11:37:49 +03:00
|
|
|
static bool is_text_file(MemoryMappedFile *mb) {
|
|
|
|
return mb->size() >= 4 &&
|
|
|
|
isprint(mb->data()[0]) &&
|
|
|
|
isprint(mb->data()[1]) &&
|
|
|
|
isprint(mb->data()[2]) &&
|
|
|
|
isprint(mb->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
|
|
|
|
2020-12-22 11:37:49 +03:00
|
|
|
static FileType get_file_type(MemoryMappedFile *mb) {
|
|
|
|
if (mb->size() >= 20 && memcmp(mb->data(), "\177ELF", 4) == 0) {
|
|
|
|
ElfEhdr &ehdr = *(ElfEhdr *)mb->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
|
|
|
}
|
|
|
|
|
2020-12-22 11:37:49 +03:00
|
|
|
if (mb->size() >= 8 && memcmp(mb->data(), "!<arch>\n", 8) == 0)
|
2021-01-09 13:21:28 +03:00
|
|
|
return FileType::AR;
|
2020-12-22 11:37:49 +03:00
|
|
|
if (mb->size() >= 8 && memcmp(mb->data(), "!<thin>\n", 8) == 0)
|
2021-01-09 13:21:28 +03:00
|
|
|
return FileType::THIN_AR;
|
2020-12-20 14:58:22 +03:00
|
|
|
if (is_text_file(mb))
|
2021-01-09 13:21:28 +03:00
|
|
|
return FileType::TEXT;
|
|
|
|
return FileType::UNKNOWN;
|
2020-12-20 14:58:22 +03:00
|
|
|
}
|
|
|
|
|
2020-12-22 11:37:49 +03:00
|
|
|
static ObjectFile *new_object_file(MemoryMappedFile *mb, std::string archive_name) {
|
2020-12-21 13:35:33 +03:00
|
|
|
ObjectFile *file = new ObjectFile(mb, archive_name);
|
|
|
|
parser_tg.run([=]() { file->parse(); });
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2020-12-22 11:37:49 +03:00
|
|
|
static SharedFile *new_shared_file(MemoryMappedFile *mb, bool as_needed) {
|
2020-12-21 13:35:33 +03:00
|
|
|
SharedFile *file = new SharedFile(mb, as_needed);
|
|
|
|
parser_tg.run([=]() { file->parse(); });
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2020-12-22 11:37:49 +03:00
|
|
|
void read_file(MemoryMappedFile *mb, bool as_needed) {
|
2020-12-22 14:10:04 +03:00
|
|
|
typedef std::tuple<std::string, u64, u64> Key;
|
|
|
|
static std::map<Key, std::vector<ObjectFile *>> cache;
|
|
|
|
|
|
|
|
auto get_key = [](MemoryMappedFile *mb) -> Key {
|
|
|
|
return {mb->name, mb->size(), mb->mtime};
|
|
|
|
};
|
|
|
|
|
2020-12-22 14:20:31 +03:00
|
|
|
auto lookup = [&](MemoryMappedFile *mb) {
|
|
|
|
Key k = get_key(mb);
|
|
|
|
std::vector<ObjectFile *> objs = cache[k];
|
|
|
|
cache[k].clear();
|
|
|
|
return objs;
|
|
|
|
};
|
|
|
|
|
2020-12-20 14:58:22 +03:00
|
|
|
switch (get_file_type(mb)) {
|
2021-01-09 13:21:28 +03:00
|
|
|
case FileType::OBJ:
|
2020-12-22 14:32:49 +03:00
|
|
|
if (preloading) {
|
2020-12-22 14:10:04 +03:00
|
|
|
cache[get_key(mb)] = {new_object_file(mb, "")};
|
2020-12-22 14:32:49 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-24 11:37:02 +03:00
|
|
|
if (std::vector<ObjectFile *> objs = lookup(mb); !objs.empty())
|
2020-12-22 14:10:04 +03:00
|
|
|
out::objs.push_back(objs[0]);
|
2020-12-24 11:37:02 +03:00
|
|
|
else
|
|
|
|
out::objs.push_back(new_object_file(mb, ""));
|
2020-12-20 14:58:22 +03:00
|
|
|
return;
|
2021-01-09 13:21:28 +03:00
|
|
|
case FileType::DSO:
|
2020-12-21 13:35:33 +03:00
|
|
|
out::dsos.push_back(new_shared_file(mb, as_needed));
|
2020-12-20 14:58:22 +03:00
|
|
|
return;
|
2021-01-09 13:21:28 +03:00
|
|
|
case FileType::AR:
|
2020-12-22 14:10:04 +03:00
|
|
|
if (preloading) {
|
|
|
|
for (MemoryMappedFile *child : read_fat_archive_members(mb))
|
|
|
|
cache[get_key(mb)].push_back(new_object_file(child, mb->name));
|
2020-12-22 14:32:49 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (std::vector<ObjectFile *> objs = lookup(mb); !objs.empty()) {
|
2020-12-22 14:10:04 +03:00
|
|
|
append(out::objs, objs);
|
2020-12-24 11:37:02 +03:00
|
|
|
} else {
|
|
|
|
for (MemoryMappedFile *child : read_archive_members(mb))
|
|
|
|
out::objs.push_back(new_object_file(child, mb->name));
|
2020-12-22 14:10:04 +03:00
|
|
|
}
|
|
|
|
return;
|
2021-01-09 13:21:28 +03:00
|
|
|
case FileType::THIN_AR:
|
2020-12-22 14:10:04 +03:00
|
|
|
if (preloading) {
|
|
|
|
for (MemoryMappedFile *child : read_thin_archive_members(mb))
|
|
|
|
cache[get_key(child)].push_back(new_object_file(child, mb->name));
|
2020-12-22 14:32:49 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (MemoryMappedFile *child : read_thin_archive_members(mb)) {
|
2020-12-24 11:37:02 +03:00
|
|
|
if (std::vector<ObjectFile *> objs = lookup(child); !objs.empty())
|
2020-12-22 14:32:49 +03:00
|
|
|
out::objs.push_back(objs[0]);
|
2020-12-24 11:37:02 +03:00
|
|
|
else
|
2020-12-22 14:32:49 +03:00
|
|
|
out::objs.push_back(new_object_file(child, mb->name));
|
2020-12-22 14:10:04 +03:00
|
|
|
}
|
2020-12-19 12:31:46 +03:00
|
|
|
return;
|
2021-01-09 13:21:28 +03:00
|
|
|
case FileType::TEXT:
|
2020-12-20 14:14:55 +03:00
|
|
|
parse_linker_script(mb, as_needed);
|
2020-12-19 12:31:46 +03:00
|
|
|
return;
|
2020-12-20 14:58:22 +03:00
|
|
|
default:
|
2021-01-09 05:19:57 +03:00
|
|
|
Error() << mb->name << ": unknown file type";
|
2020-12-19 12:31:46 +03:00
|
|
|
}
|
2020-10-10 06:47:12 +03:00
|
|
|
}
|
|
|
|
|
2020-10-28 07:42:05 +03:00
|
|
|
template <typename T>
|
2020-12-10 14:20:46 +03:00
|
|
|
static std::vector<std::span<T>> split(std::vector<T> &input, int unit) {
|
2020-12-21 10:32:43 +03:00
|
|
|
assert(input.size() > 0);
|
2020-12-10 14:20:46 +03:00
|
|
|
std::span<T> span(input);
|
|
|
|
std::vector<std::span<T>> vec;
|
2020-10-28 07:42:05 +03:00
|
|
|
|
2020-12-10 14:20:46 +03:00
|
|
|
while (span.size() >= unit) {
|
|
|
|
vec.push_back(span.subspan(0, unit));
|
|
|
|
span = span.subspan(unit);
|
2020-10-28 07:42:05 +03:00
|
|
|
}
|
2020-12-10 14:20:46 +03:00
|
|
|
if (!span.empty())
|
|
|
|
vec.push_back(span);
|
2020-10-28 07:42:05 +03:00
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
2020-11-17 08:05:40 +03:00
|
|
|
static void resolve_symbols() {
|
2021-01-09 11:45:50 +03:00
|
|
|
Timer t("resolve_symbols");
|
2020-11-11 04:42:26 +03:00
|
|
|
|
2020-11-11 04:51:30 +03:00
|
|
|
// Register defined symbols
|
2020-11-24 08:31:05 +03:00
|
|
|
tbb::parallel_for_each(out::objs, [](ObjectFile *file) { file->resolve_symbols(); });
|
2020-11-24 10:22:32 +03:00
|
|
|
tbb::parallel_for_each(out::dsos, [](SharedFile *file) { file->resolve_symbols(); });
|
2020-11-11 04:42:26 +03:00
|
|
|
|
2020-11-30 10:43:47 +03:00
|
|
|
// Mark reachable objects and DSOs to decide which files to include
|
|
|
|
// into an output.
|
2020-11-11 04:42:26 +03:00
|
|
|
std::vector<ObjectFile *> root;
|
2020-11-24 07:56:14 +03:00
|
|
|
for (ObjectFile *file : out::objs)
|
2020-11-24 08:31:05 +03:00
|
|
|
if (file->is_alive)
|
2020-11-11 04:42:26 +03:00
|
|
|
root.push_back(file);
|
|
|
|
|
|
|
|
tbb::parallel_do(
|
|
|
|
root,
|
|
|
|
[&](ObjectFile *file, tbb::parallel_do_feeder<ObjectFile *> &feeder) {
|
2020-12-20 10:42:17 +03:00
|
|
|
for (ObjectFile *obj : file->mark_live_objects())
|
|
|
|
feeder.add(obj);
|
2020-11-11 04:42:26 +03:00
|
|
|
});
|
|
|
|
|
2020-11-30 10:43:47 +03:00
|
|
|
// Eliminate unused archive members and as-needed DSOs.
|
2020-12-20 10:42:17 +03:00
|
|
|
erase(out::objs, [](InputFile *file) { return !file->is_alive; });
|
|
|
|
erase(out::dsos, [](InputFile *file) { return !file->is_alive; });
|
2020-11-11 04:42:26 +03:00
|
|
|
}
|
|
|
|
|
2020-11-17 08:04:53 +03:00
|
|
|
static void eliminate_comdats() {
|
2021-01-09 11:45:50 +03:00
|
|
|
Timer t("comdat");
|
2020-11-11 04:42:26 +03:00
|
|
|
|
2020-11-24 07:56:14 +03:00
|
|
|
tbb::parallel_for_each(out::objs, [](ObjectFile *file) {
|
2020-11-08 12:17:24 +03:00
|
|
|
file->resolve_comdat_groups();
|
|
|
|
});
|
|
|
|
|
2020-11-24 07:56:14 +03:00
|
|
|
tbb::parallel_for_each(out::objs, [](ObjectFile *file) {
|
2020-11-08 12:17:24 +03:00
|
|
|
file->eliminate_duplicate_comdat_groups();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-17 08:04:53 +03:00
|
|
|
static void handle_mergeable_strings() {
|
2021-01-09 11:45:50 +03:00
|
|
|
Timer t("resolve_strings");
|
2020-11-11 04:42:26 +03:00
|
|
|
|
2020-11-07 15:53:21 +03:00
|
|
|
// Resolve mergeable string pieces
|
2020-11-24 07:56:14 +03:00
|
|
|
tbb::parallel_for_each(out::objs, [](ObjectFile *file) {
|
2020-12-13 19:18:25 +03:00
|
|
|
for (MergeableSection *m : file->mergeable_sections) {
|
2021-01-09 09:43:28 +03:00
|
|
|
for (StringPiece *piece : m->pieces) {
|
|
|
|
MergeableSection *cur = piece->isec;
|
2020-12-13 19:18:25 +03:00
|
|
|
while (!cur || cur->file->priority > m->file->priority)
|
2021-01-09 09:43:28 +03:00
|
|
|
if (piece->isec.compare_exchange_weak(cur, m))
|
2020-11-07 15:53:21 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Calculate the total bytes of mergeable strings for each input section.
|
2020-11-24 07:56:14 +03:00
|
|
|
tbb::parallel_for_each(out::objs, [](ObjectFile *file) {
|
2020-12-13 19:18:25 +03:00
|
|
|
for (MergeableSection *m : file->mergeable_sections) {
|
2020-11-07 15:53:21 +03:00
|
|
|
u32 offset = 0;
|
2021-01-09 09:43:28 +03:00
|
|
|
for (StringPiece *piece : m->pieces) {
|
|
|
|
if (piece->isec == m && piece->output_offset == -1) {
|
|
|
|
piece->output_offset = offset;
|
|
|
|
offset += piece->size;
|
2020-11-07 15:53:21 +03:00
|
|
|
}
|
|
|
|
}
|
2020-12-13 19:18:25 +03:00
|
|
|
m->size = offset;
|
2020-11-07 15:53:21 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Assign each mergeable input section a unique index.
|
2020-11-24 07:56:14 +03:00
|
|
|
for (ObjectFile *file : out::objs) {
|
2020-12-13 19:18:25 +03:00
|
|
|
for (MergeableSection *m : file->mergeable_sections) {
|
2020-12-13 19:21:39 +03:00
|
|
|
m->offset = m->parent.shdr.sh_size;
|
|
|
|
m->parent.shdr.sh_size += m->size;
|
2020-11-07 15:53:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-11 04:51:30 +03:00
|
|
|
// So far, each input section has a pointer to its corresponding
|
|
|
|
// output section, but there's no reverse edge to get a list of
|
|
|
|
// input sections from an output section. This function creates it.
|
|
|
|
//
|
|
|
|
// An output section may contain millions of input sections.
|
|
|
|
// So, we append input sections to output sections in parallel.
|
2020-11-17 08:04:53 +03:00
|
|
|
static void bin_sections() {
|
2021-01-09 11:45:50 +03:00
|
|
|
Timer t("bin_sections");
|
2020-11-11 04:42:26 +03:00
|
|
|
|
2020-11-24 07:56:14 +03:00
|
|
|
int unit = (out::objs.size() + 127) / 128;
|
2020-12-10 14:20:46 +03:00
|
|
|
std::vector<std::span<ObjectFile *>> slices = split(out::objs, unit);
|
2020-10-26 07:36:56 +03:00
|
|
|
|
2020-11-08 04:05:59 +03:00
|
|
|
int num_osec = OutputSection::instances.size();
|
|
|
|
|
2020-11-08 06:36:08 +03:00
|
|
|
std::vector<std::vector<std::vector<InputChunk *>>> groups(slices.size());
|
2020-10-28 08:22:25 +03:00
|
|
|
for (int i = 0; i < groups.size(); i++)
|
2020-11-08 04:05:59 +03:00
|
|
|
groups[i].resize(num_osec);
|
2020-10-28 08:06:35 +03:00
|
|
|
|
|
|
|
tbb::parallel_for(0, (int)slices.size(), [&](int i) {
|
2020-12-21 13:35:33 +03:00
|
|
|
for (ObjectFile *file : slices[i])
|
2020-12-20 10:52:10 +03:00
|
|
|
for (InputSection *isec : file->sections)
|
|
|
|
if (isec)
|
|
|
|
groups[i][isec->output_section->idx].push_back(isec);
|
2020-10-28 08:06:35 +03:00
|
|
|
});
|
|
|
|
|
2020-11-08 04:05:59 +03:00
|
|
|
std::vector<int> sizes(num_osec);
|
2020-10-26 07:36:56 +03:00
|
|
|
|
2020-12-10 14:20:46 +03:00
|
|
|
for (std::span<std::vector<InputChunk *>> group : groups)
|
2020-10-28 08:22:25 +03:00
|
|
|
for (int i = 0; i < group.size(); i++)
|
|
|
|
sizes[i] += group[i].size();
|
2020-11-08 03:44:27 +03:00
|
|
|
|
2020-11-08 04:05:59 +03:00
|
|
|
tbb::parallel_for(0, num_osec, [&](int j) {
|
2020-11-08 06:42:40 +03:00
|
|
|
OutputSection::instances[j]->members.reserve(sizes[j]);
|
2020-12-20 10:52:10 +03:00
|
|
|
for (int i = 0; i < groups.size(); i++)
|
|
|
|
append(OutputSection::instances[j]->members, groups[i][j]);
|
2020-11-08 04:05:59 +03:00
|
|
|
});
|
2020-10-26 07:36:56 +03:00
|
|
|
}
|
2020-10-22 17:19:48 +03:00
|
|
|
|
2021-01-10 08:23:41 +03:00
|
|
|
static void check_errors() {
|
|
|
|
bool has_error = false;
|
|
|
|
|
|
|
|
for (ObjectFile *file : out::objs) {
|
|
|
|
if (file->err_out.tellp()) {
|
|
|
|
std::cerr << file->err_out.str();
|
|
|
|
has_error = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (has_error) {
|
|
|
|
cleanup();
|
|
|
|
_exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-26 12:09:32 +03:00
|
|
|
static void check_duplicate_symbols() {
|
2021-01-10 10:37:24 +03:00
|
|
|
Timer t("check_dup_syms");
|
2020-12-20 11:08:27 +03:00
|
|
|
|
2020-11-30 12:19:33 +03:00
|
|
|
tbb::parallel_for_each(out::objs, [&](ObjectFile *file) {
|
2020-11-26 12:09:32 +03:00
|
|
|
for (int i = file->first_global; i < file->elf_syms.size(); i++) {
|
2021-01-10 07:52:59 +03:00
|
|
|
const ElfSym &esym = file->elf_syms[i];
|
|
|
|
Symbol &sym = *file->symbols[i];
|
|
|
|
bool is_weak = (esym.st_bind == STB_WEAK);
|
|
|
|
bool is_eliminated =
|
|
|
|
!esym.is_abs() && !esym.is_common() && !file->sections[esym.st_shndx];
|
|
|
|
|
2021-01-10 08:23:41 +03:00
|
|
|
if (esym.is_defined() && !is_weak && !is_eliminated && sym.file != file)
|
|
|
|
file->err_out << "duplicate symbol: " << *file << ": " << *sym.file
|
|
|
|
<< ": " << sym.name << "\n";
|
2020-11-20 11:12:45 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-01-10 08:23:41 +03:00
|
|
|
check_errors();
|
2020-11-20 11:12:45 +03:00
|
|
|
}
|
|
|
|
|
2020-10-26 08:18:00 +03:00
|
|
|
static void set_isec_offsets() {
|
2021-01-09 11:45:50 +03:00
|
|
|
Timer t("isec_offsets");
|
2020-11-11 04:42:26 +03:00
|
|
|
|
2020-11-07 18:47:34 +03:00
|
|
|
tbb::parallel_for_each(OutputSection::instances, [&](OutputSection *osec) {
|
2020-11-08 06:42:40 +03:00
|
|
|
if (osec->members.empty())
|
2020-10-27 07:52:10 +03:00
|
|
|
return;
|
|
|
|
|
2020-12-10 14:20:46 +03:00
|
|
|
std::vector<std::span<InputChunk *>> slices = split(osec->members, 10000);
|
2020-10-29 10:27:11 +03:00
|
|
|
std::vector<u64> size(slices.size());
|
|
|
|
std::vector<u32> alignments(slices.size());
|
2020-10-26 08:18:00 +03:00
|
|
|
|
2020-10-28 07:42:05 +03:00
|
|
|
tbb::parallel_for(0, (int)slices.size(), [&](int i) {
|
2020-10-29 10:27:11 +03:00
|
|
|
u64 off = 0;
|
|
|
|
u32 align = 1;
|
2020-10-26 10:12:35 +03:00
|
|
|
|
2020-11-08 06:36:08 +03:00
|
|
|
for (InputChunk *isec : slices[i]) {
|
2020-10-26 10:12:35 +03:00
|
|
|
off = align_to(off, isec->shdr.sh_addralign);
|
|
|
|
isec->offset = off;
|
|
|
|
off += isec->shdr.sh_size;
|
2020-10-29 10:27:11 +03:00
|
|
|
align = std::max<u32>(align, isec->shdr.sh_addralign);
|
2020-10-26 10:12:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
size[i] = off;
|
|
|
|
alignments[i] = align;
|
|
|
|
});
|
|
|
|
|
2020-10-29 10:27:11 +03:00
|
|
|
u32 align = *std::max_element(alignments.begin(), alignments.end());
|
2020-10-26 10:12:35 +03:00
|
|
|
|
2020-10-29 10:27:11 +03:00
|
|
|
std::vector<u64> start(slices.size());
|
2020-10-28 07:42:05 +03:00
|
|
|
for (int i = 1; i < slices.size(); i++)
|
2020-11-10 06:23:14 +03:00
|
|
|
start[i] = align_to(start[i - 1] + size[i - 1], align);
|
2020-10-26 10:58:49 +03:00
|
|
|
|
2020-10-28 07:42:05 +03:00
|
|
|
tbb::parallel_for(1, (int)slices.size(), [&](int i) {
|
2020-11-08 06:36:08 +03:00
|
|
|
for (InputChunk *isec : slices[i])
|
2020-10-26 10:12:35 +03:00
|
|
|
isec->offset += start[i];
|
|
|
|
});
|
|
|
|
|
|
|
|
osec->shdr.sh_size = start.back() + size.back();
|
2020-10-26 08:18:00 +03:00
|
|
|
osec->shdr.sh_addralign = align;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-29 05:10:33 +03:00
|
|
|
static void scan_rels() {
|
2021-01-09 11:45:50 +03:00
|
|
|
Timer t("scan_rels");
|
2020-11-15 10:19:21 +03:00
|
|
|
|
2020-11-29 05:10:33 +03:00
|
|
|
// Scan relocations to find dynamic symbols.
|
2020-11-24 07:56:14 +03:00
|
|
|
tbb::parallel_for_each(out::objs, [&](ObjectFile *file) {
|
2021-01-10 09:38:10 +03:00
|
|
|
for (InputSection *isec : file->sections)
|
2020-11-15 10:19:21 +03:00
|
|
|
if (isec)
|
|
|
|
isec->scan_relocations();
|
2020-11-17 14:54:13 +03:00
|
|
|
});
|
2020-11-17 13:56:02 +03:00
|
|
|
|
2020-11-29 05:10:33 +03:00
|
|
|
// If there was a relocation that refers an undefined symbol,
|
|
|
|
// report an error.
|
2021-01-10 08:23:41 +03:00
|
|
|
check_errors();
|
2020-11-26 12:09:32 +03:00
|
|
|
|
2020-11-29 05:10:33 +03:00
|
|
|
// Aggregate dynamic symbols to a single vector.
|
2020-11-24 10:22:32 +03:00
|
|
|
std::vector<InputFile *> files;
|
2020-12-20 09:51:37 +03:00
|
|
|
append(files, out::objs);
|
|
|
|
append(files, out::dsos);
|
2020-11-24 10:22:32 +03:00
|
|
|
|
2020-11-24 08:31:05 +03:00
|
|
|
std::vector<std::vector<Symbol *>> vec(files.size());
|
2020-11-17 14:54:13 +03:00
|
|
|
|
2020-11-24 08:31:05 +03:00
|
|
|
tbb::parallel_for(0, (int)files.size(), [&](int i) {
|
|
|
|
for (Symbol *sym : files[i]->symbols)
|
2021-01-10 09:38:10 +03:00
|
|
|
if (sym->flags && sym->file == files[i])
|
|
|
|
vec[i].push_back(sym);
|
2020-11-15 07:01:38 +03:00
|
|
|
});
|
2020-11-06 06:01:52 +03:00
|
|
|
|
2020-11-29 05:10:33 +03:00
|
|
|
// Assign offsets in additional tables for each dynamic symbol.
|
|
|
|
for (Symbol *sym : flatten(vec)) {
|
2020-12-16 14:56:04 +03:00
|
|
|
if (sym->is_imported || (sym->flags & NEEDS_DYNSYM))
|
2020-12-13 15:57:45 +03:00
|
|
|
out::dynsym->add_symbol(sym);
|
|
|
|
|
2020-12-16 12:38:13 +03:00
|
|
|
if (sym->flags & NEEDS_GOT)
|
2020-11-21 06:49:28 +03:00
|
|
|
out::got->add_got_symbol(sym);
|
2020-11-06 07:54:37 +03:00
|
|
|
|
2020-12-16 12:38:13 +03:00
|
|
|
if (sym->flags & NEEDS_PLT)
|
2020-11-18 15:45:49 +03:00
|
|
|
out::plt->add_symbol(sym);
|
2020-11-17 14:22:52 +03:00
|
|
|
|
2020-12-16 12:38:13 +03:00
|
|
|
if (sym->flags & NEEDS_GOTTPOFF)
|
2020-11-21 04:48:51 +03:00
|
|
|
out::got->add_gottpoff_symbol(sym);
|
2020-11-17 14:22:52 +03:00
|
|
|
|
2020-12-16 12:38:13 +03:00
|
|
|
if (sym->flags & NEEDS_TLSGD)
|
2020-11-21 04:48:23 +03:00
|
|
|
out::got->add_tlsgd_symbol(sym);
|
|
|
|
|
2020-12-16 12:38:13 +03:00
|
|
|
if (sym->flags & NEEDS_TLSLD)
|
2020-11-21 04:48:23 +03:00
|
|
|
out::got->add_tlsld_symbol(sym);
|
2020-11-25 11:20:48 +03:00
|
|
|
|
2020-12-16 12:38:13 +03:00
|
|
|
if (sym->flags & NEEDS_COPYREL) {
|
2020-11-25 11:20:48 +03:00
|
|
|
out::copyrel->add_symbol(sym);
|
2020-11-25 14:35:04 +03:00
|
|
|
assert(sym->file->is_dso);
|
|
|
|
|
|
|
|
for (Symbol *alias : ((SharedFile *)sym->file)->find_aliases(sym)) {
|
2020-11-29 05:06:11 +03:00
|
|
|
if (sym == alias)
|
|
|
|
continue;
|
|
|
|
assert(alias->copyrel_offset == -1);
|
2020-11-25 14:35:04 +03:00
|
|
|
alias->copyrel_offset = sym->copyrel_offset;
|
|
|
|
out::dynsym->add_symbol(alias);
|
|
|
|
}
|
|
|
|
}
|
2020-11-18 15:45:49 +03:00
|
|
|
}
|
2020-11-16 16:25:58 +03:00
|
|
|
}
|
2020-11-11 11:25:00 +03:00
|
|
|
|
2020-12-07 09:50:13 +03:00
|
|
|
static void export_dynamic() {
|
2021-01-09 11:45:50 +03:00
|
|
|
Timer t("export_dynamic");
|
2020-12-07 15:04:29 +03:00
|
|
|
|
|
|
|
tbb::parallel_for(0, (int)out::objs.size(), [&](int i) {
|
|
|
|
ObjectFile *file = out::objs[i];
|
2020-12-10 14:20:46 +03:00
|
|
|
for (Symbol *sym : std::span(file->symbols).subspan(file->first_global))
|
2020-12-07 16:13:42 +03:00
|
|
|
if (sym->file == file && config.export_dynamic)
|
2020-12-07 15:04:29 +03:00
|
|
|
sym->ver_idx = VER_NDX_GLOBAL;
|
|
|
|
});
|
2020-12-07 16:13:42 +03:00
|
|
|
|
2020-12-10 07:44:58 +03:00
|
|
|
for (std::string_view name : config.globals)
|
2020-12-07 16:13:42 +03:00
|
|
|
Symbol::intern(name)->ver_idx = VER_NDX_GLOBAL;
|
2020-12-07 15:04:29 +03:00
|
|
|
|
2020-12-07 09:50:13 +03:00
|
|
|
std::vector<std::vector<Symbol *>> vec(out::objs.size());
|
|
|
|
|
|
|
|
tbb::parallel_for(0, (int)out::objs.size(), [&](int i) {
|
2020-12-07 10:19:56 +03:00
|
|
|
ObjectFile *file = out::objs[i];
|
2020-12-10 14:20:46 +03:00
|
|
|
for (Symbol *sym : std::span(file->symbols).subspan(file->first_global))
|
2020-12-07 15:04:29 +03:00
|
|
|
if (sym->file == file && sym->ver_idx != VER_NDX_LOCAL)
|
2020-12-07 09:50:13 +03:00
|
|
|
vec[i].push_back(sym);
|
|
|
|
});
|
|
|
|
|
|
|
|
for (Symbol *sym : flatten(vec))
|
|
|
|
out::dynsym->add_symbol(sym);
|
|
|
|
}
|
|
|
|
|
2020-11-29 05:40:57 +03:00
|
|
|
static void fill_symbol_versions() {
|
2021-01-09 11:45:50 +03:00
|
|
|
Timer t("fill_symbol_versions");
|
2020-11-29 05:40:57 +03:00
|
|
|
|
2020-11-29 05:57:58 +03:00
|
|
|
// Create a list of versioned symbols and sort by file and version.
|
2020-11-29 05:40:57 +03:00
|
|
|
std::vector<Symbol *> syms = out::dynsym->symbols;
|
2020-12-13 16:31:50 +03:00
|
|
|
erase(syms, [](Symbol *sym){ return sym->ver_idx < 2; });
|
2020-11-29 05:40:57 +03:00
|
|
|
|
2020-11-29 05:57:58 +03:00
|
|
|
if (syms.empty())
|
|
|
|
return;
|
|
|
|
|
2020-12-21 12:42:14 +03:00
|
|
|
sort(syms, [](Symbol *a, Symbol *b) {
|
|
|
|
return std::make_tuple(((SharedFile *)a->file)->soname, a->ver_idx) <
|
|
|
|
std::make_tuple(((SharedFile *)b->file)->soname, b->ver_idx);
|
2020-11-29 05:40:57 +03:00
|
|
|
});
|
|
|
|
|
2020-11-29 05:57:58 +03:00
|
|
|
// Compute sizes of .gnu.version and .gnu.version_r sections.
|
2020-11-29 07:02:51 +03:00
|
|
|
out::versym->contents.resize(out::dynsym->symbols.size() + 1, 1);
|
2020-11-29 05:57:58 +03:00
|
|
|
out::versym->contents[0] = 0;
|
|
|
|
|
2020-12-10 15:18:20 +03:00
|
|
|
int sz = sizeof(ElfVerneed) + sizeof(ElfVernaux);
|
2020-11-29 05:57:58 +03:00
|
|
|
for (int i = 1; i < syms.size(); i++) {
|
|
|
|
if (syms[i - 1]->file != syms[i]->file)
|
2020-12-10 15:18:20 +03:00
|
|
|
sz += sizeof(ElfVerneed) + sizeof(ElfVernaux);
|
2020-11-29 05:57:58 +03:00
|
|
|
else if (syms[i - 1]->ver_idx != syms[i]->ver_idx)
|
2020-12-10 15:18:20 +03:00
|
|
|
sz += sizeof(ElfVernaux);
|
2020-11-29 05:40:57 +03:00
|
|
|
}
|
2020-11-29 05:57:58 +03:00
|
|
|
out::verneed->contents.resize(sz);
|
|
|
|
|
|
|
|
// Fill .gnu.versoin_r.
|
|
|
|
u8 *buf = (u8 *)&out::verneed->contents[0];
|
2020-11-29 06:59:08 +03:00
|
|
|
u16 version = 1;
|
2020-12-10 15:18:20 +03:00
|
|
|
ElfVerneed *verneed = nullptr;
|
|
|
|
ElfVernaux *aux = nullptr;
|
2020-11-29 06:59:08 +03:00
|
|
|
|
2020-11-29 08:05:44 +03:00
|
|
|
auto add_aux = [&](Symbol *sym) {
|
2020-11-29 06:59:08 +03:00
|
|
|
SharedFile *file = (SharedFile *)sym->file;
|
2020-12-10 07:44:58 +03:00
|
|
|
std::string_view verstr = file->version_strings[sym->ver_idx];
|
2020-11-29 06:59:08 +03:00
|
|
|
|
2021-01-09 16:27:53 +03:00
|
|
|
verneed->vn_cnt++;
|
2020-11-29 08:05:44 +03:00
|
|
|
if (aux)
|
2020-12-10 15:18:20 +03:00
|
|
|
aux->vna_next = sizeof(ElfVernaux);
|
2020-11-29 08:05:44 +03:00
|
|
|
|
2020-12-10 15:18:20 +03:00
|
|
|
aux = (ElfVernaux *)buf;
|
2020-11-29 06:59:08 +03:00
|
|
|
buf += sizeof(*aux);
|
|
|
|
aux->vna_hash = elf_hash(verstr);
|
|
|
|
aux->vna_other = ++version;
|
|
|
|
aux->vna_name = out::dynstr->add_string(verstr);
|
|
|
|
};
|
|
|
|
|
2020-11-29 08:05:44 +03:00
|
|
|
auto add_verneed = [&](Symbol *sym) {
|
2020-11-29 06:59:08 +03:00
|
|
|
SharedFile *file = (SharedFile *)sym->file;
|
|
|
|
|
2021-01-09 16:27:53 +03:00
|
|
|
out::verneed->shdr.sh_info++;
|
2020-11-29 08:05:44 +03:00
|
|
|
if (verneed)
|
|
|
|
verneed->vn_next = buf - (u8 *)verneed;
|
|
|
|
|
2020-12-10 15:18:20 +03:00
|
|
|
verneed = (ElfVerneed *)buf;
|
2020-11-29 08:05:44 +03:00
|
|
|
buf += sizeof(*verneed);
|
|
|
|
verneed->vn_version = 1;
|
2020-11-30 11:52:08 +03:00
|
|
|
verneed->vn_file = out::dynstr->find_string(file->soname);
|
2020-12-10 15:18:20 +03:00
|
|
|
verneed->vn_aux = sizeof(ElfVerneed);
|
2020-11-29 08:05:44 +03:00
|
|
|
|
|
|
|
aux = nullptr;
|
|
|
|
add_aux(sym);
|
2020-11-29 06:59:08 +03:00
|
|
|
};
|
|
|
|
|
2020-11-29 08:05:44 +03:00
|
|
|
add_verneed(syms[0]);
|
2020-11-29 07:14:56 +03:00
|
|
|
out::versym->contents[syms[0]->dynsym_idx] = version;
|
|
|
|
|
2020-11-29 06:59:08 +03:00
|
|
|
for (int i = 1; i < syms.size(); i++) {
|
|
|
|
if (syms[i - 1]->file != syms[i]->file)
|
2020-11-29 08:05:44 +03:00
|
|
|
add_verneed(syms[i]);
|
2020-11-29 06:59:08 +03:00
|
|
|
else if (syms[i - 1]->ver_idx != syms[i]->ver_idx)
|
2020-11-29 08:05:44 +03:00
|
|
|
add_aux(syms[i]);
|
2020-11-29 06:59:08 +03:00
|
|
|
out::versym->contents[syms[i]->dynsym_idx] = version;
|
|
|
|
}
|
2020-11-29 05:40:57 +03:00
|
|
|
}
|
|
|
|
|
2020-11-17 07:59:24 +03:00
|
|
|
static void clear_padding(u64 filesize) {
|
2021-01-09 11:45:50 +03:00
|
|
|
Timer t("clear_padding");
|
2020-11-11 04:42:26 +03:00
|
|
|
|
2020-11-29 14:39:44 +03:00
|
|
|
auto zero = [](OutputChunk *chunk, u64 next_start) {
|
2020-11-10 11:32:41 +03:00
|
|
|
u64 pos = chunk->shdr.sh_offset;
|
|
|
|
if (chunk->shdr.sh_type != SHT_NOBITS)
|
|
|
|
pos += chunk->shdr.sh_size;
|
2020-11-17 07:59:24 +03:00
|
|
|
memset(out::buf + pos, 0, next_start - pos);
|
2020-11-09 15:50:47 +03:00
|
|
|
};
|
|
|
|
|
2020-11-17 07:59:24 +03:00
|
|
|
for (int i = 1; i < out::chunks.size(); i++)
|
|
|
|
zero(out::chunks[i - 1], out::chunks[i]->shdr.sh_offset);
|
|
|
|
zero(out::chunks.back(), filesize);
|
2020-11-09 15:50:47 +03:00
|
|
|
}
|
|
|
|
|
2020-10-22 12:54:51 +03:00
|
|
|
// We want to sort output sections in the following order.
|
|
|
|
//
|
2020-10-22 17:19:48 +03:00
|
|
|
// alloc readonly data
|
|
|
|
// alloc readonly code
|
|
|
|
// alloc writable tdata
|
|
|
|
// alloc writable tbss
|
|
|
|
// alloc writable data
|
|
|
|
// alloc writable bss
|
|
|
|
// nonalloc
|
2020-12-10 09:59:24 +03:00
|
|
|
static int get_section_rank(const ElfShdr &shdr) {
|
2020-10-29 12:31:06 +03:00
|
|
|
bool alloc = shdr.sh_flags & SHF_ALLOC;
|
|
|
|
bool writable = shdr.sh_flags & SHF_WRITE;
|
|
|
|
bool exec = shdr.sh_flags & SHF_EXECINSTR;
|
|
|
|
bool tls = shdr.sh_flags & SHF_TLS;
|
2020-10-30 06:47:35 +03:00
|
|
|
bool nobits = shdr.sh_type == SHT_NOBITS;
|
2020-11-17 08:50:18 +03:00
|
|
|
return (!alloc << 5) | (writable << 4) | (exec << 3) | (!tls << 2) | nobits;
|
2020-10-22 12:54:51 +03:00
|
|
|
}
|
|
|
|
|
2020-12-10 14:20:46 +03:00
|
|
|
static u64 set_osec_offsets(std::span<OutputChunk *> chunks) {
|
2021-01-09 11:45:50 +03:00
|
|
|
Timer t("osec_offset");
|
2020-11-11 04:42:26 +03:00
|
|
|
|
2020-10-29 10:27:11 +03:00
|
|
|
u64 fileoff = 0;
|
2020-11-17 09:05:53 +03:00
|
|
|
u64 vaddr = config.image_base;
|
2020-10-26 08:16:13 +03:00
|
|
|
|
2020-11-11 08:13:39 +03:00
|
|
|
for (OutputChunk *chunk : chunks) {
|
2020-10-30 05:40:38 +03:00
|
|
|
if (chunk->starts_new_ptload)
|
2020-10-26 08:16:13 +03:00
|
|
|
vaddr = align_to(vaddr, PAGE_SIZE);
|
|
|
|
|
2020-11-24 16:54:21 +03:00
|
|
|
if (vaddr % PAGE_SIZE > fileoff % PAGE_SIZE)
|
|
|
|
fileoff += vaddr % PAGE_SIZE - fileoff % PAGE_SIZE;
|
|
|
|
else if (vaddr % PAGE_SIZE < fileoff % PAGE_SIZE)
|
|
|
|
fileoff = align_to(fileoff, PAGE_SIZE) + vaddr % PAGE_SIZE;
|
2020-10-30 05:40:38 +03:00
|
|
|
|
|
|
|
fileoff = align_to(fileoff, chunk->shdr.sh_addralign);
|
2020-10-26 08:16:13 +03:00
|
|
|
vaddr = align_to(vaddr, chunk->shdr.sh_addralign);
|
|
|
|
|
|
|
|
chunk->shdr.sh_offset = fileoff;
|
|
|
|
if (chunk->shdr.sh_flags & SHF_ALLOC)
|
|
|
|
chunk->shdr.sh_addr = vaddr;
|
|
|
|
|
2020-11-24 16:54:21 +03:00
|
|
|
bool is_bss = chunk->shdr.sh_type == SHT_NOBITS;
|
2020-11-03 14:37:27 +03:00
|
|
|
if (!is_bss)
|
2020-11-03 14:13:03 +03:00
|
|
|
fileoff += chunk->shdr.sh_size;
|
2020-10-30 05:40:38 +03:00
|
|
|
|
2020-11-03 14:37:27 +03:00
|
|
|
bool is_tbss = is_bss && (chunk->shdr.sh_flags & SHF_TLS);
|
2020-10-30 05:40:38 +03:00
|
|
|
if (!is_tbss)
|
2020-11-03 14:13:03 +03:00
|
|
|
vaddr += chunk->shdr.sh_size;
|
2020-10-26 08:16:13 +03:00
|
|
|
}
|
|
|
|
return fileoff;
|
|
|
|
}
|
|
|
|
|
2020-12-10 14:20:46 +03:00
|
|
|
static void fix_synthetic_symbols(std::span<OutputChunk *> chunks) {
|
2021-01-13 08:43:34 +03:00
|
|
|
auto start = [](Symbol *sym, OutputChunk *chunk) {
|
2020-11-11 04:45:52 +03:00
|
|
|
if (sym) {
|
|
|
|
sym->shndx = chunk->shndx;
|
|
|
|
sym->value = chunk->shdr.sh_addr;
|
|
|
|
}
|
|
|
|
};
|
2020-11-04 08:23:39 +03:00
|
|
|
|
2021-01-13 08:43:34 +03:00
|
|
|
auto stop = [](Symbol *sym, OutputChunk *chunk) {
|
2020-11-11 04:45:52 +03:00
|
|
|
if (sym) {
|
|
|
|
sym->shndx = chunk->shndx;
|
|
|
|
sym->value = chunk->shdr.sh_addr + chunk->shdr.sh_size;
|
|
|
|
}
|
|
|
|
};
|
2020-11-04 08:23:39 +03:00
|
|
|
|
|
|
|
// __bss_start
|
2020-11-11 08:13:39 +03:00
|
|
|
for (OutputChunk *chunk : chunks) {
|
2020-11-08 10:09:01 +03:00
|
|
|
if (chunk->kind == OutputChunk::REGULAR && chunk->name == ".bss") {
|
2021-01-13 08:43:34 +03:00
|
|
|
start(out::__bss_start, chunk);
|
2020-11-04 08:23:39 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// __ehdr_start
|
2020-11-11 08:13:39 +03:00
|
|
|
for (OutputChunk *chunk : chunks) {
|
2020-11-04 08:23:39 +03:00
|
|
|
if (chunk->shndx == 1) {
|
2020-11-04 08:41:40 +03:00
|
|
|
out::__ehdr_start->shndx = 1;
|
2020-11-12 07:15:29 +03:00
|
|
|
out::__ehdr_start->value = out::ehdr->shdr.sh_addr;
|
2020-11-04 08:23:39 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// __rela_iplt_start and __rela_iplt_end
|
2021-01-13 08:43:34 +03:00
|
|
|
start(out::__rela_iplt_start, out::relplt);
|
|
|
|
stop(out::__rela_iplt_end, out::relplt);
|
2020-11-04 08:23:39 +03:00
|
|
|
|
|
|
|
// __{init,fini}_array_{start,end}
|
2020-11-11 08:13:39 +03:00
|
|
|
for (OutputChunk *chunk : chunks) {
|
2020-11-04 08:23:39 +03:00
|
|
|
switch (chunk->shdr.sh_type) {
|
|
|
|
case SHT_INIT_ARRAY:
|
2021-01-13 08:43:34 +03:00
|
|
|
start(out::__init_array_start, chunk);
|
|
|
|
stop(out::__init_array_end, chunk);
|
2020-11-04 08:23:39 +03:00
|
|
|
break;
|
|
|
|
case SHT_FINI_ARRAY:
|
2021-01-13 08:43:34 +03:00
|
|
|
start(out::__fini_array_start, chunk);
|
|
|
|
stop(out::__fini_array_end, chunk);
|
2020-11-04 08:23:39 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// _end, end, _etext, etext, _edata and edata
|
2020-11-11 08:13:39 +03:00
|
|
|
for (OutputChunk *chunk : chunks) {
|
2020-11-08 10:09:01 +03:00
|
|
|
if (chunk->kind == OutputChunk::HEADER)
|
2020-11-04 08:23:39 +03:00
|
|
|
continue;
|
|
|
|
|
2020-11-12 07:19:19 +03:00
|
|
|
if (chunk->shdr.sh_flags & SHF_ALLOC)
|
2021-01-13 08:43:34 +03:00
|
|
|
stop(out::_end, chunk);
|
2020-11-04 08:23:39 +03:00
|
|
|
|
2020-11-12 07:19:19 +03:00
|
|
|
if (chunk->shdr.sh_flags & SHF_EXECINSTR)
|
2021-01-13 08:43:34 +03:00
|
|
|
stop(out::_etext, chunk);
|
2020-11-04 08:23:39 +03:00
|
|
|
|
2020-11-12 07:19:19 +03:00
|
|
|
if (chunk->shdr.sh_type != SHT_NOBITS && chunk->shdr.sh_flags & SHF_ALLOC)
|
2021-01-13 08:43:34 +03:00
|
|
|
stop(out::_edata, chunk);
|
2020-11-04 08:23:39 +03:00
|
|
|
}
|
|
|
|
|
2020-11-13 04:19:47 +03:00
|
|
|
// _DYNAMIC
|
|
|
|
if (out::dynamic)
|
2021-01-13 08:43:34 +03:00
|
|
|
start(out::_DYNAMIC, out::dynamic);
|
2020-11-13 04:19:47 +03:00
|
|
|
|
2020-11-16 07:48:14 +03:00
|
|
|
// _GLOBAL_OFFSET_TABLE_
|
|
|
|
if (out::gotplt)
|
2021-01-13 08:43:34 +03:00
|
|
|
start(out::_GLOBAL_OFFSET_TABLE_, out::gotplt);
|
2020-11-16 07:48:14 +03:00
|
|
|
|
2020-11-04 08:23:39 +03:00
|
|
|
// __start_ and __stop_ symbols
|
2020-11-11 08:13:39 +03:00
|
|
|
for (OutputChunk *chunk : chunks) {
|
2020-11-11 08:45:17 +03:00
|
|
|
if (is_c_identifier(chunk->name)) {
|
2021-01-13 08:43:34 +03:00
|
|
|
start(Symbol::intern("__start_" + std::string(chunk->name)), chunk);
|
|
|
|
stop(Symbol::intern("__stop_" + std::string(chunk->name)), chunk);
|
2020-11-11 08:45:17 +03:00
|
|
|
}
|
2020-11-04 08:23:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-24 14:15:17 +03:00
|
|
|
void cleanup() {
|
2021-01-09 11:07:19 +03:00
|
|
|
if (OutputFile::tmpfile)
|
|
|
|
unlink(OutputFile::tmpfile);
|
2020-12-25 07:34:12 +03:00
|
|
|
if (socket_tmpfile)
|
|
|
|
unlink(socket_tmpfile);
|
2020-12-24 14:15:17 +03:00
|
|
|
}
|
|
|
|
|
2020-12-25 07:34:12 +03:00
|
|
|
static void signal_handler(int) {
|
2020-12-24 14:15:17 +03:00
|
|
|
cleanup();
|
2020-12-24 08:39:02 +03:00
|
|
|
_exit(1);
|
|
|
|
}
|
|
|
|
|
2021-01-09 16:30:14 +03:00
|
|
|
MemoryMappedFile *find_library(std::string name,
|
|
|
|
std::span<std::string_view> lib_paths) {
|
2020-12-21 00:47:10 +03:00
|
|
|
for (std::string_view dir : lib_paths) {
|
2020-12-10 07:44:58 +03:00
|
|
|
std::string root = dir.starts_with("/") ? config.sysroot : "";
|
|
|
|
std::string stem = root + std::string(dir) + "/lib" + name;
|
2020-11-19 12:03:26 +03:00
|
|
|
if (!config.is_static)
|
2020-12-22 12:33:16 +03:00
|
|
|
if (MemoryMappedFile *mb = MemoryMappedFile::open(stem + ".so"))
|
2020-12-22 11:37:49 +03:00
|
|
|
return mb;
|
2020-12-22 12:33:16 +03:00
|
|
|
if (MemoryMappedFile *mb = MemoryMappedFile::open(stem + ".a"))
|
2020-12-22 11:37:49 +03:00
|
|
|
return mb;
|
2020-11-19 12:03:26 +03:00
|
|
|
}
|
2021-01-09 05:19:57 +03:00
|
|
|
Error() << "library not found: " << name;
|
2020-11-19 12:03:26 +03:00
|
|
|
}
|
|
|
|
|
2020-12-12 07:20:26 +03:00
|
|
|
static std::vector<std::string> add_dashes(std::string name) {
|
|
|
|
std::vector<std::string> opts;
|
|
|
|
opts.push_back("-" + name);
|
|
|
|
if (!name.starts_with("o"))
|
|
|
|
opts.push_back("--" + name);
|
|
|
|
return opts;
|
|
|
|
}
|
|
|
|
|
2020-12-18 13:58:52 +03:00
|
|
|
static bool read_arg(std::span<std::string_view> &args, std::string_view &arg,
|
|
|
|
std::string name) {
|
2020-12-11 06:43:14 +03:00
|
|
|
if (name.size() == 1) {
|
|
|
|
if (args[0] == "-" + name) {
|
|
|
|
if (args.size() == 1)
|
2021-01-09 05:19:57 +03:00
|
|
|
Error() << "option -" << name << ": argument missing";
|
2020-12-11 06:43:14 +03:00
|
|
|
arg = args[1];
|
|
|
|
args = args.subspan(2);
|
|
|
|
return true;
|
|
|
|
}
|
2020-10-02 10:47:51 +03:00
|
|
|
|
2020-12-18 13:58:52 +03:00
|
|
|
if (args[0].starts_with("-" + name)) {
|
|
|
|
arg = args[0].substr(name.size() + 1);
|
2020-12-11 06:43:14 +03:00
|
|
|
args = args.subspan(1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2020-10-30 07:47:51 +03:00
|
|
|
|
2020-12-12 07:20:26 +03:00
|
|
|
for (std::string opt : add_dashes(name)) {
|
2020-12-11 06:43:14 +03:00
|
|
|
if (args[0] == opt) {
|
|
|
|
if (args.size() == 1)
|
2021-01-09 05:19:57 +03:00
|
|
|
Error() << "option " << name << ": argument missing";
|
2020-12-11 06:43:14 +03:00
|
|
|
arg = args[1];
|
|
|
|
args = args.subspan(2);
|
|
|
|
return true;
|
|
|
|
}
|
2020-11-03 12:02:28 +03:00
|
|
|
|
2020-12-18 13:58:52 +03:00
|
|
|
if (args[0].starts_with(opt + "=")) {
|
|
|
|
arg = args[0].substr(opt.size() + 1);
|
2020-12-11 06:43:14 +03:00
|
|
|
args = args.subspan(1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-12-18 13:58:52 +03:00
|
|
|
static bool read_flag(std::span<std::string_view> &args, std::string name) {
|
2020-12-12 07:20:26 +03:00
|
|
|
for (std::string opt : add_dashes(name)) {
|
|
|
|
if (args[0] == opt) {
|
|
|
|
args = args.subspan(1);
|
|
|
|
return true;
|
|
|
|
}
|
2020-12-17 16:29:43 +03:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-12-18 13:58:52 +03:00
|
|
|
static bool read_z_flag(std::span<std::string_view> &args, std::string name) {
|
|
|
|
if (args.size() >= 2 && args[0] == "-z" && args[1] == name) {
|
2020-12-17 16:29:43 +03:00
|
|
|
args = args.subspan(2);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-12-18 13:58:52 +03:00
|
|
|
if (!args.empty() && args[0] == "-z" + name) {
|
2020-12-17 16:29:43 +03:00
|
|
|
args = args.subspan(1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-12-12 07:20:26 +03:00
|
|
|
return false;
|
|
|
|
}
|
2020-10-04 12:00:33 +03:00
|
|
|
|
2020-12-18 13:58:52 +03:00
|
|
|
static bool read_equal(std::span<std::string_view> &args, std::string_view &arg,
|
2020-12-12 07:20:26 +03:00
|
|
|
std::string name, std::string default_) {
|
|
|
|
for (std::string opt : add_dashes(name)) {
|
2020-12-11 06:43:14 +03:00
|
|
|
if (args[0] == opt) {
|
2020-12-12 07:20:26 +03:00
|
|
|
arg = default_;
|
2020-12-11 06:43:14 +03:00
|
|
|
args = args.subspan(1);
|
|
|
|
return true;
|
|
|
|
}
|
2020-12-17 16:29:43 +03:00
|
|
|
}
|
2020-12-12 07:20:26 +03:00
|
|
|
|
|
|
|
for (std::string opt : add_dashes(name)) {
|
2020-12-18 13:58:52 +03:00
|
|
|
if (args[0].starts_with(opt + "=")) {
|
|
|
|
arg = args[0].substr(opt.size() + 1);
|
2020-12-12 07:20:26 +03:00
|
|
|
args = args.subspan(1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2020-12-11 06:43:14 +03:00
|
|
|
return false;
|
|
|
|
}
|
2020-10-29 06:24:54 +03:00
|
|
|
|
2020-12-11 06:43:14 +03:00
|
|
|
static u64 parse_hex(std::string opt, std::string_view value) {
|
|
|
|
if (!value.starts_with("0x") && !value.starts_with("0X"))
|
2021-01-09 05:19:57 +03:00
|
|
|
Error() << "option -" << opt << ": not a hexadecimal number";
|
2020-12-11 06:43:14 +03:00
|
|
|
value = value.substr(2);
|
|
|
|
if (value.find_first_not_of("0123456789abcdefABCDEF") != std::string_view::npos)
|
2021-01-09 05:19:57 +03:00
|
|
|
Error() << "option -" << opt << ": not a hexadecimal number";
|
2020-12-11 06:43:14 +03:00
|
|
|
return std::stol(std::string(value), nullptr, 16);
|
|
|
|
}
|
2020-11-30 11:57:58 +03:00
|
|
|
|
2020-12-11 06:43:14 +03:00
|
|
|
static u64 parse_number(std::string opt, std::string_view value) {
|
|
|
|
if (value.find_first_not_of("0123456789") != std::string_view::npos)
|
2021-01-09 05:19:57 +03:00
|
|
|
Error() << "option -" << opt << ": not a number";
|
2020-12-11 06:43:14 +03:00
|
|
|
return std::stol(std::string(value));
|
|
|
|
}
|
2020-12-07 14:04:44 +03:00
|
|
|
|
2020-12-20 10:17:24 +03:00
|
|
|
static std::vector<std::string_view> read_response_file(std::string_view path) {
|
|
|
|
std::vector<std::string_view> vec;
|
2020-12-22 12:33:16 +03:00
|
|
|
MemoryMappedFile *mb = MemoryMappedFile::must_open(std::string(path));
|
2020-12-20 10:17:24 +03:00
|
|
|
|
|
|
|
auto read_quoted = [&](int i, char quote) {
|
|
|
|
std::string *buf = new std::string;
|
2020-12-22 11:37:49 +03:00
|
|
|
while (i < mb->size() && mb->data()[i] != quote) {
|
|
|
|
if (mb->data()[i] == '\\') {
|
|
|
|
buf->append(1, mb->data()[i + 1]);
|
2020-12-20 10:17:24 +03:00
|
|
|
i += 2;
|
|
|
|
} else {
|
2020-12-22 11:37:49 +03:00
|
|
|
buf->append(1, mb->data()[i++]);
|
2020-12-20 10:17:24 +03:00
|
|
|
}
|
|
|
|
}
|
2020-12-22 11:37:49 +03:00
|
|
|
if (i >= mb->size())
|
2021-01-09 05:19:57 +03:00
|
|
|
Error() << path << ": premature end of input";
|
2020-12-20 10:17:24 +03:00
|
|
|
vec.push_back(std::string_view(*buf));
|
|
|
|
return i + 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto read_unquoted = [&](int i) {
|
|
|
|
std::string *buf = new std::string;
|
2020-12-22 11:37:49 +03:00
|
|
|
while (i < mb->size() && !isspace(mb->data()[i]))
|
|
|
|
buf->append(1, mb->data()[i++]);
|
2020-12-20 10:17:24 +03:00
|
|
|
vec.push_back(std::string_view(*buf));
|
|
|
|
return i;
|
|
|
|
};
|
|
|
|
|
2020-12-22 11:37:49 +03:00
|
|
|
for (int i = 0; i < mb->size();) {
|
|
|
|
if (isspace(mb->data()[i]))
|
2020-12-20 10:17:24 +03:00
|
|
|
i++;
|
2020-12-22 11:37:49 +03:00
|
|
|
else if (mb->data()[i] == '\'')
|
2020-12-20 10:17:24 +03:00
|
|
|
i = read_quoted(i + 1, '\'');
|
2020-12-22 11:37:49 +03:00
|
|
|
else if (mb->data()[i] == '\"')
|
2020-12-20 10:17:24 +03:00
|
|
|
i = read_quoted(i + 1, '\"');
|
|
|
|
else
|
|
|
|
i = read_unquoted(i);
|
|
|
|
}
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
2020-12-21 10:32:43 +03:00
|
|
|
static std::vector<std::string_view> expand_response_files(char **argv) {
|
|
|
|
std::vector<std::string_view> vec;
|
|
|
|
|
|
|
|
for (int 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;
|
|
|
|
}
|
|
|
|
|
2020-12-20 16:52:31 +03:00
|
|
|
static std::vector<std::string_view> get_input_files(std::span<std::string_view> args) {
|
2020-12-21 00:47:10 +03:00
|
|
|
static std::unordered_set<std::string_view> needs_arg({
|
|
|
|
"o", "dynamic-linker", "export-dynamic", "e", "entry", "y",
|
|
|
|
"trace-symbol", "filler", "sysroot", "thread-count", "z",
|
|
|
|
"hash-style", "m", "build-id", "rpath", "version-script",
|
|
|
|
});
|
2020-12-20 16:52:31 +03:00
|
|
|
|
|
|
|
std::vector<std::string_view> vec;
|
2020-12-21 00:47:10 +03:00
|
|
|
std::vector<std::string> library_paths;
|
2020-12-20 16:52:31 +03:00
|
|
|
|
|
|
|
while (args.empty()) {
|
2020-12-21 00:47:10 +03:00
|
|
|
if (needs_arg.contains(args[0])) {
|
2020-12-20 16:52:31 +03:00
|
|
|
if (args.size() == 1)
|
2021-01-09 05:19:57 +03:00
|
|
|
Error() << args[0] << ": missing argument";
|
2020-12-20 16:52:31 +03:00
|
|
|
args = args.subspan(2);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-12-21 00:47:10 +03:00
|
|
|
std::string_view arg;
|
|
|
|
|
|
|
|
if (read_arg(args, arg, "L") || read_arg(args, arg, "library-path")) {
|
|
|
|
library_paths.push_back(std::string(arg));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (read_arg(args, arg, "l")) {
|
2020-12-20 16:52:31 +03:00
|
|
|
vec.push_back(arg);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args[0].starts_with("-")) {
|
|
|
|
args = args.subspan(1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
vec.push_back(args[0]);
|
|
|
|
args = args.subspan(1);
|
|
|
|
}
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
2020-12-21 10:32:43 +03:00
|
|
|
static Config parse_nonpositional_args(std::span<std::string_view> args,
|
|
|
|
std::vector<std::string_view> &remaining) {
|
|
|
|
Config conf;
|
2020-12-21 12:48:17 +03:00
|
|
|
conf.thread_count =
|
|
|
|
tbb::global_control::active_value(tbb::global_control::max_allowed_parallelism);
|
2020-12-11 10:51:20 +03:00
|
|
|
|
2020-12-21 10:32:43 +03:00
|
|
|
while (!args.empty()) {
|
2020-12-17 09:03:17 +03:00
|
|
|
std::string_view arg;
|
2020-12-11 06:43:14 +03:00
|
|
|
|
|
|
|
if (read_arg(args, arg, "o")) {
|
2020-12-21 10:32:43 +03:00
|
|
|
conf.output = arg;
|
2020-12-12 07:20:26 +03:00
|
|
|
} else if (read_arg(args, arg, "dynamic-linker")) {
|
2020-12-21 10:32:43 +03:00
|
|
|
conf.dynamic_linker = arg;
|
2020-12-14 11:01:07 +03:00
|
|
|
} else if (read_flag(args, "export-dynamic")) {
|
2020-12-21 10:32:43 +03:00
|
|
|
conf.export_dynamic = true;
|
2020-12-14 04:07:14 +03:00
|
|
|
} else if (read_arg(args, arg, "e") || read_arg(args, arg, "entry")) {
|
2020-12-21 10:32:43 +03:00
|
|
|
conf.entry = arg;
|
2020-12-11 06:43:14 +03:00
|
|
|
} else if (read_flag(args, "print-map")) {
|
2020-12-21 10:32:43 +03:00
|
|
|
conf.print_map = true;
|
2020-12-11 06:43:14 +03:00
|
|
|
} else if (read_flag(args, "stat")) {
|
2020-12-21 10:32:43 +03:00
|
|
|
conf.stat = true;
|
2020-12-11 06:43:14 +03:00
|
|
|
} else if (read_flag(args, "static")) {
|
2020-12-21 10:32:43 +03:00
|
|
|
conf.is_static = true;
|
2020-12-11 06:43:14 +03:00
|
|
|
} else if (read_arg(args, arg, "y") || read_arg(args, arg, "trace-symbol")) {
|
2020-12-21 10:32:43 +03:00
|
|
|
conf.trace_symbol.push_back(arg);
|
2020-12-11 06:43:14 +03:00
|
|
|
} else if (read_arg(args, arg, "filler")) {
|
2020-12-21 10:32:43 +03:00
|
|
|
conf.filler = parse_hex("filler", arg);
|
2020-12-11 06:43:14 +03:00
|
|
|
} else if (read_arg(args, arg, "L") || read_arg(args, arg, "library-path")) {
|
2020-12-21 10:32:43 +03:00
|
|
|
conf.library_paths.push_back(arg);
|
2020-12-11 06:43:14 +03:00
|
|
|
} else if (read_arg(args, arg, "sysroot")) {
|
2020-12-21 10:32:43 +03:00
|
|
|
conf.sysroot = arg;
|
2020-12-11 06:43:14 +03:00
|
|
|
} else if (read_flag(args, "trace")) {
|
2020-12-21 10:32:43 +03:00
|
|
|
conf.trace = true;
|
2020-12-16 15:16:34 +03:00
|
|
|
} else if (read_flag(args, "pie")) {
|
2020-12-21 10:32:43 +03:00
|
|
|
conf.pie = true;
|
2020-12-17 15:36:38 +03:00
|
|
|
} else if (read_flag(args, "no-pie")) {
|
2020-12-21 10:32:43 +03:00
|
|
|
conf.pie = false;
|
2020-12-13 16:41:50 +03:00
|
|
|
} else if (read_flag(args, "perf")) {
|
2020-12-21 10:32:43 +03:00
|
|
|
conf.perf = true;
|
2020-12-17 16:29:43 +03:00
|
|
|
} else if (read_z_flag(args, "now")) {
|
2020-12-21 10:32:43 +03:00
|
|
|
conf.z_now = true;
|
2020-12-20 03:20:24 +03:00
|
|
|
} else if (read_flag(args, "no-fork")) {
|
2020-12-21 10:32:43 +03:00
|
|
|
conf.fork = false;
|
2020-12-20 02:31:17 +03:00
|
|
|
} else if (read_arg(args, arg, "thread-count")) {
|
2020-12-21 10:32:43 +03:00
|
|
|
conf.thread_count = parse_number("thread-count", arg);
|
2020-12-20 04:20:24 +03:00
|
|
|
} else if (read_flag(args, "discard-all") || read_flag(args, "x")) {
|
2020-12-21 10:32:43 +03:00
|
|
|
conf.discard_all = true;
|
2020-12-20 03:20:24 +03:00
|
|
|
} else if (read_flag(args, "discard-locals") || read_flag(args, "X")) {
|
2020-12-21 10:32:43 +03:00
|
|
|
conf.discard_locals = true;
|
2020-12-20 04:27:01 +03:00
|
|
|
} else if (read_flag(args, "strip-all") || read_flag(args, "s")) {
|
2020-12-21 10:32:43 +03:00
|
|
|
conf.strip_all = true;
|
|
|
|
} else if (read_arg(args, arg, "rpath")) {
|
2020-12-21 12:35:10 +03:00
|
|
|
conf.rpaths.push_back(arg);
|
2020-12-21 10:32:43 +03:00
|
|
|
} else if (read_arg(args, arg, "version-script")) {
|
|
|
|
conf.version_script.push_back(arg);
|
2020-12-22 15:38:41 +03:00
|
|
|
} else if (read_flag(args, "preload")) {
|
|
|
|
conf.preload = true;
|
2020-12-20 03:20:24 +03:00
|
|
|
} else if (read_arg(args, arg, "z")) {
|
2020-12-12 07:20:26 +03:00
|
|
|
} else if (read_arg(args, arg, "hash-style")) {
|
|
|
|
} else if (read_arg(args, arg, "m")) {
|
|
|
|
} else if (read_equal(args, arg, "build-id", "none")) {
|
|
|
|
} else if (read_flag(args, "eh-frame-hdr")) {
|
|
|
|
} else if (read_flag(args, "start-group")) {
|
|
|
|
} else if (read_flag(args, "end-group")) {
|
|
|
|
} else if (read_flag(args, "fatal-warnings")) {
|
|
|
|
} else if (read_flag(args, "disable-new-dtags")) {
|
2020-12-20 03:20:24 +03:00
|
|
|
} else if (read_flag(args, "as-needed")) {
|
2020-12-21 10:32:43 +03:00
|
|
|
remaining.push_back("-as-needed");
|
2020-12-20 03:20:24 +03:00
|
|
|
} else if (read_flag(args, "no-as-needed")) {
|
2020-12-21 10:32:43 +03:00
|
|
|
remaining.push_back("-no-as-needed");
|
2020-12-20 03:20:24 +03:00
|
|
|
} else if (read_arg(args, arg, "l")) {
|
2020-12-21 10:32:43 +03:00
|
|
|
remaining.push_back("-l");
|
|
|
|
remaining.push_back(arg);
|
2020-12-11 06:43:14 +03:00
|
|
|
} else {
|
2020-12-20 03:20:24 +03:00
|
|
|
if (args[0][0] == '-')
|
2021-01-09 05:19:57 +03:00
|
|
|
Error() << "unknown command line option: " << args[0];
|
2020-12-21 10:32:43 +03:00
|
|
|
remaining.push_back(args[0]);
|
2020-12-11 06:43:14 +03:00
|
|
|
args = args.subspan(1);
|
2020-11-19 12:03:26 +03:00
|
|
|
}
|
2020-11-07 17:00:01 +03:00
|
|
|
}
|
2020-12-21 10:32:43 +03:00
|
|
|
return conf;
|
|
|
|
}
|
|
|
|
|
2020-12-22 14:12:41 +03:00
|
|
|
static void read_input_files(std::span<std::string_view> args) {
|
|
|
|
bool as_needed = false;
|
|
|
|
|
|
|
|
while (!args.empty()) {
|
|
|
|
std::string_view arg;
|
|
|
|
|
|
|
|
if (read_flag(args, "as-needed")) {
|
|
|
|
as_needed = true;
|
|
|
|
} else if (read_flag(args, "no-as-needed")) {
|
|
|
|
as_needed = false;
|
|
|
|
} else if (read_arg(args, arg, "l")) {
|
|
|
|
read_file(find_library(std::string(arg), config.library_paths), as_needed);
|
|
|
|
} else {
|
|
|
|
read_file(MemoryMappedFile::must_open(std::string(args[0])), as_needed);
|
|
|
|
args = args.subspan(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
parser_tg.wait();
|
|
|
|
}
|
|
|
|
|
2020-12-21 11:51:20 +03:00
|
|
|
static void show_stats() {
|
|
|
|
for (ObjectFile *obj : out::objs) {
|
|
|
|
static Counter defined("defined_syms");
|
|
|
|
defined.inc(obj->first_global - 1);
|
|
|
|
|
|
|
|
static Counter undefined("undefined_syms");
|
|
|
|
undefined.inc(obj->symbols.size() - obj->first_global);
|
|
|
|
}
|
|
|
|
|
|
|
|
Counter num_input_sections("input_sections");
|
|
|
|
for (ObjectFile *file : out::objs)
|
|
|
|
num_input_sections.inc(file->sections.size());
|
|
|
|
|
|
|
|
static Counter merged_strings("merged_strings");
|
|
|
|
for (MergedSection *osec : MergedSection::instances)
|
2020-12-23 11:23:58 +03:00
|
|
|
merged_strings.inc(osec->map.size());
|
2020-12-21 11:51:20 +03:00
|
|
|
|
|
|
|
Counter num_output_chunks("output_out::chunks", out::chunks.size());
|
|
|
|
Counter num_objs("num_objs", out::objs.size());
|
|
|
|
Counter num_dsos("num_dsos", out::dsos.size());
|
|
|
|
|
|
|
|
Counter::print();
|
|
|
|
}
|
|
|
|
|
2020-12-21 10:32:43 +03:00
|
|
|
int main(int argc, char **argv) {
|
|
|
|
Timer t_all("all");
|
|
|
|
|
|
|
|
// Parse non-positional command line options
|
|
|
|
std::vector<std::string_view> arg_vector = expand_response_files(argv + 1);
|
|
|
|
std::vector<std::string_view> file_args;
|
|
|
|
config = parse_nonpositional_args(arg_vector, file_args);
|
|
|
|
|
2020-12-21 12:52:45 +03:00
|
|
|
if (config.output == "")
|
2021-01-09 05:19:57 +03:00
|
|
|
Error() << "-o option is missing";
|
2020-12-21 12:52:45 +03:00
|
|
|
|
2021-01-09 15:14:52 +03:00
|
|
|
if (!config.preload)
|
|
|
|
if (int code; resume_daemon(argv, &code))
|
2021-01-09 15:33:46 +03:00
|
|
|
exit(code);
|
2020-12-24 17:14:37 +03:00
|
|
|
|
2021-01-09 15:14:52 +03:00
|
|
|
signal(SIGINT, signal_handler);
|
|
|
|
signal(SIGTERM, signal_handler);
|
2020-12-24 17:14:37 +03:00
|
|
|
|
2020-12-24 17:25:07 +03:00
|
|
|
// Preload input files
|
|
|
|
std::function<void()> on_complete;
|
|
|
|
|
|
|
|
if (config.preload) {
|
2021-01-09 15:14:52 +03:00
|
|
|
std::function<void()> wait_for_client;
|
|
|
|
daemonize(argv, &wait_for_client, &on_complete);
|
2020-12-24 17:25:07 +03:00
|
|
|
preloading = true;
|
|
|
|
read_input_files(file_args);
|
2021-01-09 15:14:52 +03:00
|
|
|
wait_for_client();
|
2020-12-24 17:25:07 +03:00
|
|
|
} else if (config.fork) {
|
2020-12-20 03:20:24 +03:00
|
|
|
on_complete = fork_child();
|
2021-01-09 15:14:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
tbb::global_control tbb_cont(tbb::global_control::max_allowed_parallelism,
|
|
|
|
config.thread_count);
|
2020-12-20 03:20:24 +03:00
|
|
|
|
2020-12-21 10:32:43 +03:00
|
|
|
if (config.stat)
|
|
|
|
Counter::enabled = true;
|
|
|
|
if (config.pie)
|
|
|
|
config.image_base = 0;
|
|
|
|
|
|
|
|
for (std::string_view arg : config.trace_symbol)
|
|
|
|
Symbol::intern(arg)->traced = true;
|
|
|
|
|
|
|
|
for (std::string_view arg : config.version_script)
|
|
|
|
parse_version_script(std::string(arg));
|
2020-12-20 03:20:24 +03:00
|
|
|
|
|
|
|
// Parse input files
|
|
|
|
{
|
2021-01-09 11:45:50 +03:00
|
|
|
Timer t("parse");
|
2020-12-22 14:20:31 +03:00
|
|
|
preloading = false;
|
2020-12-22 14:12:41 +03:00
|
|
|
read_input_files(file_args);
|
2020-12-21 10:32:43 +03:00
|
|
|
}
|
2020-12-17 15:36:38 +03:00
|
|
|
|
2020-11-20 07:54:29 +03:00
|
|
|
// Uniquify shared object files with soname
|
|
|
|
{
|
2020-11-29 16:05:34 +03:00
|
|
|
std::vector<SharedFile *> vec;
|
2020-12-11 06:46:03 +03:00
|
|
|
std::unordered_set<std::string_view> seen;
|
2020-11-29 16:05:34 +03:00
|
|
|
for (SharedFile *file : out::dsos)
|
|
|
|
if (seen.insert(file->soname).second)
|
|
|
|
vec.push_back(file);
|
|
|
|
out::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
|
|
|
|
2020-11-16 18:17:01 +03:00
|
|
|
out::ehdr = new OutputEhdr;
|
2020-11-16 18:23:51 +03:00
|
|
|
out::shdr = new OutputShdr;
|
2020-11-16 18:33:41 +03:00
|
|
|
out::phdr = new OutputPhdr;
|
2020-11-18 11:11:58 +03:00
|
|
|
out::got = new GotSection;
|
2020-11-13 06:43:59 +03:00
|
|
|
out::gotplt = new GotPltSection;
|
2020-11-17 07:34:02 +03:00
|
|
|
out::relplt = new RelPltSection;
|
2020-11-19 10:13:19 +03:00
|
|
|
out::strtab = new StrtabSection;
|
2020-11-17 06:20:56 +03:00
|
|
|
out::shstrtab = new ShstrtabSection;
|
2020-11-11 15:32:41 +03:00
|
|
|
out::plt = new PltSection;
|
2020-11-17 07:32:22 +03:00
|
|
|
out::symtab = new SymtabSection;
|
2020-11-16 17:40:01 +03:00
|
|
|
out::dynsym = new DynsymSection;
|
2020-11-17 07:19:54 +03:00
|
|
|
out::dynstr = new DynstrSection;
|
2020-11-25 11:20:48 +03:00
|
|
|
out::copyrel = new CopyrelSection;
|
2020-11-12 08:40:39 +03:00
|
|
|
|
2020-11-10 13:33:27 +03:00
|
|
|
if (!config.is_static) {
|
2020-11-16 19:05:01 +03:00
|
|
|
out::interp = new InterpSection;
|
2020-11-16 18:43:32 +03:00
|
|
|
out::dynamic = new DynamicSection;
|
2020-11-17 07:30:33 +03:00
|
|
|
out::reldyn = new RelDynSection;
|
2020-11-11 15:32:41 +03:00
|
|
|
out::hash = new HashSection;
|
2020-11-29 05:57:58 +03:00
|
|
|
out::versym = new VersymSection;
|
|
|
|
out::verneed = new VerneedSection;
|
2020-11-10 13:33:27 +03:00
|
|
|
}
|
|
|
|
|
2020-11-20 06:44:02 +03:00
|
|
|
out::chunks.push_back(out::got);
|
|
|
|
out::chunks.push_back(out::plt);
|
|
|
|
out::chunks.push_back(out::gotplt);
|
|
|
|
out::chunks.push_back(out::relplt);
|
|
|
|
out::chunks.push_back(out::reldyn);
|
|
|
|
out::chunks.push_back(out::dynamic);
|
|
|
|
out::chunks.push_back(out::dynsym);
|
|
|
|
out::chunks.push_back(out::dynstr);
|
|
|
|
out::chunks.push_back(out::shstrtab);
|
|
|
|
out::chunks.push_back(out::symtab);
|
|
|
|
out::chunks.push_back(out::strtab);
|
|
|
|
out::chunks.push_back(out::hash);
|
2020-11-25 11:20:48 +03:00
|
|
|
out::chunks.push_back(out::copyrel);
|
2020-11-29 06:59:08 +03:00
|
|
|
out::chunks.push_back(out::versym);
|
|
|
|
out::chunks.push_back(out::verneed);
|
2020-11-20 06:44:02 +03:00
|
|
|
|
2020-11-22 05:10:38 +03:00
|
|
|
// Set priorities to files. File priority 1 is reserved for the internal file.
|
|
|
|
int priority = 2;
|
2020-11-24 07:56:14 +03:00
|
|
|
for (ObjectFile *file : out::objs)
|
2020-11-24 08:31:05 +03:00
|
|
|
if (!file->is_in_archive)
|
2020-10-28 04:15:05 +03:00
|
|
|
file->priority = priority++;
|
2020-11-24 07:56:14 +03:00
|
|
|
for (ObjectFile *file : out::objs)
|
2020-11-24 08:31:05 +03:00
|
|
|
if (file->is_in_archive)
|
2020-10-28 04:15:05 +03:00
|
|
|
file->priority = priority++;
|
2020-11-24 10:22:32 +03:00
|
|
|
for (SharedFile *file : out::dsos)
|
2020-11-24 08:31:05 +03:00
|
|
|
file->priority = priority++;
|
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.
|
2020-11-17 08:05:40 +03:00
|
|
|
resolve_symbols();
|
2020-10-19 15:50:33 +03:00
|
|
|
|
2020-12-11 06:43:14 +03:00
|
|
|
if (config.trace) {
|
2020-11-24 07:56:14 +03:00
|
|
|
for (ObjectFile *file : out::objs)
|
2021-01-09 06:49:39 +03:00
|
|
|
SyncOut() << *file;
|
2020-11-24 10:22:32 +03:00
|
|
|
for (SharedFile *file : out::dsos)
|
2021-01-09 06:49:39 +03:00
|
|
|
SyncOut() << *file;
|
2020-11-24 08:31:05 +03:00
|
|
|
}
|
2020-11-05 03:24:47 +03:00
|
|
|
|
2020-11-11 04:42:26 +03:00
|
|
|
// Remove redundant comdat sections (e.g. duplicate inline functions).
|
2020-11-17 08:04:53 +03:00
|
|
|
eliminate_comdats();
|
2020-10-10 06:47:12 +03:00
|
|
|
|
2020-11-11 04:42:26 +03:00
|
|
|
// Merge strings constants in SHF_MERGE sections.
|
2020-11-17 08:04:53 +03:00
|
|
|
handle_mergeable_strings();
|
2020-11-07 14:29:06 +03:00
|
|
|
|
2020-10-27 06:50:25 +03:00
|
|
|
// Create .bss sections for common symbols.
|
|
|
|
{
|
2021-01-09 11:45:50 +03:00
|
|
|
Timer t("common");
|
2020-12-13 16:23:30 +03:00
|
|
|
tbb::parallel_for_each(out::objs, [](ObjectFile *file) {
|
|
|
|
file->convert_common_symbols();
|
|
|
|
});
|
2020-10-27 06:50:25 +03:00
|
|
|
}
|
|
|
|
|
2020-10-26 05:34:26 +03:00
|
|
|
// Bin input sections into output sections
|
2020-11-17 08:04:53 +03:00
|
|
|
bin_sections();
|
2020-10-23 04:27:11 +03:00
|
|
|
|
2020-10-29 12:19:10 +03:00
|
|
|
// Assign offsets within an output section to input sections.
|
2020-11-11 04:42:26 +03:00
|
|
|
set_isec_offsets();
|
2020-10-22 10:35:17 +03:00
|
|
|
|
2020-11-12 10:09:17 +03:00
|
|
|
// Sections are added to the section lists in an arbitrary order because
|
2020-12-13 16:23:30 +03:00
|
|
|
// they are created in parallel. Sort them to to make the output deterministic.
|
2020-11-12 09:19:30 +03:00
|
|
|
auto section_compare = [](OutputChunk *x, OutputChunk *y) {
|
|
|
|
return std::make_tuple(x->name, (u32)x->shdr.sh_type, (u64)x->shdr.sh_flags) <
|
|
|
|
std::make_tuple(y->name, (u32)y->shdr.sh_type, (u64)y->shdr.sh_flags);
|
|
|
|
};
|
|
|
|
|
2020-12-21 12:42:14 +03:00
|
|
|
sort(OutputSection::instances, section_compare);
|
|
|
|
sort(MergedSection::instances, section_compare);
|
2020-11-12 09:19:30 +03:00
|
|
|
|
2020-11-12 10:09:17 +03:00
|
|
|
// Add sections to the section lists
|
2020-11-04 04:39:17 +03:00
|
|
|
for (OutputSection *osec : OutputSection::instances)
|
2020-11-11 16:14:12 +03:00
|
|
|
if (osec->shdr.sh_size)
|
2020-11-17 07:48:11 +03:00
|
|
|
out::chunks.push_back(osec);
|
2020-11-07 15:53:21 +03:00
|
|
|
for (MergedSection *osec : MergedSection::instances)
|
2020-11-11 16:14:12 +03:00
|
|
|
if (osec->shdr.sh_size)
|
2020-11-17 07:48:11 +03:00
|
|
|
out::chunks.push_back(osec);
|
2020-11-07 14:31:09 +03:00
|
|
|
|
2020-12-13 16:31:50 +03:00
|
|
|
erase(out::chunks, [](OutputChunk *c) { return !c; });
|
2020-11-20 06:44:02 +03:00
|
|
|
|
|
|
|
// Sort the sections by section flags so that we'll have to create
|
|
|
|
// as few segments as possible.
|
2020-12-21 12:42:14 +03:00
|
|
|
sort(out::chunks, [](OutputChunk *a, OutputChunk *b) {
|
|
|
|
return get_section_rank(a->shdr) < get_section_rank(b->shdr);
|
|
|
|
});
|
2020-11-20 06:44:02 +03:00
|
|
|
|
2020-11-04 04:39:17 +03:00
|
|
|
// Create a dummy file containing linker-synthesized symbols
|
|
|
|
// (e.g. `__bss_start`).
|
2021-01-12 15:17:53 +03:00
|
|
|
ObjectFile *internal_file = new ObjectFile;
|
2020-11-20 06:44:02 +03:00
|
|
|
internal_file->resolve_symbols();
|
2020-11-24 07:56:14 +03:00
|
|
|
out::objs.push_back(internal_file);
|
2020-11-04 04:39:17 +03:00
|
|
|
|
2020-11-20 05:08:06 +03:00
|
|
|
// Convert weak symbols to absolute symbols with value 0.
|
2021-01-10 11:14:08 +03:00
|
|
|
{
|
|
|
|
Timer t("undef_weak");
|
|
|
|
tbb::parallel_for_each(out::objs, [](ObjectFile *file) {
|
|
|
|
file->handle_undefined_weak_symbols();
|
|
|
|
});
|
|
|
|
}
|
2020-11-20 05:08:06 +03:00
|
|
|
|
2020-11-09 05:31:00 +03:00
|
|
|
// Beyond this point, no new symbols will be added to the result.
|
|
|
|
|
2020-12-13 16:23:30 +03:00
|
|
|
// Copy shared object name strings to .dynstr.
|
2020-11-24 10:22:32 +03:00
|
|
|
for (SharedFile *file : out::dsos)
|
2020-11-30 11:52:08 +03:00
|
|
|
out::dynstr->add_string(file->soname);
|
2020-11-12 18:14:15 +03:00
|
|
|
|
2020-11-30 11:57:58 +03:00
|
|
|
// Copy DT_RUNPATH strings to .dynstr.
|
2020-12-10 07:44:58 +03:00
|
|
|
for (std::string_view path : config.rpaths)
|
2020-11-30 11:57:58 +03:00
|
|
|
out::dynstr->add_string(path);
|
|
|
|
|
2020-11-09 03:47:58 +03:00
|
|
|
// Add headers and sections that have to be at the beginning
|
|
|
|
// or the ending of a file.
|
2020-11-17 07:48:11 +03:00
|
|
|
out::chunks.insert(out::chunks.begin(), out::ehdr);
|
|
|
|
out::chunks.insert(out::chunks.begin() + 1, out::phdr);
|
2020-11-11 15:32:41 +03:00
|
|
|
if (out::interp)
|
2020-11-17 07:48:11 +03:00
|
|
|
out::chunks.insert(out::chunks.begin() + 2, out::interp);
|
|
|
|
out::chunks.push_back(out::shdr);
|
2020-10-27 11:36:55 +03:00
|
|
|
|
2020-11-26 12:09:32 +03:00
|
|
|
// Make sure that all symbols have been resolved.
|
|
|
|
check_duplicate_symbols();
|
|
|
|
|
2020-11-29 05:06:11 +03:00
|
|
|
// Scan relocations to find symbols that need entries in .got, .plt,
|
|
|
|
// .got.plt, .dynsym, .dynstr, etc.
|
2020-11-29 05:10:33 +03:00
|
|
|
scan_rels();
|
2020-11-20 06:44:02 +03:00
|
|
|
|
2020-12-07 09:50:13 +03:00
|
|
|
// Put symbols to .dynsym.
|
|
|
|
export_dynamic();
|
|
|
|
|
2020-11-29 05:40:57 +03:00
|
|
|
// Fill .gnu.version and .gnu.version_r section contents.
|
|
|
|
fill_symbol_versions();
|
|
|
|
|
2020-11-29 12:58:36 +03:00
|
|
|
// Compute .symtab and .strtab sizes for each file.
|
|
|
|
tbb::parallel_for_each(out::objs, [](ObjectFile *file) {
|
|
|
|
file->compute_symtab();
|
|
|
|
});
|
|
|
|
|
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.
|
2020-12-07 10:12:19 +03:00
|
|
|
for (OutputChunk *chunk : out::chunks)
|
|
|
|
chunk->update_shdr();
|
|
|
|
|
2020-12-13 16:31:50 +03:00
|
|
|
erase(out::chunks, [](OutputChunk *c) { return c->shdr.sh_size == 0; });
|
2020-12-07 10:12:19 +03:00
|
|
|
|
|
|
|
// Set section indices.
|
|
|
|
for (int i = 0, shndx = 1; i < out::chunks.size(); i++)
|
|
|
|
if (out::chunks[i]->kind != OutputChunk::HEADER)
|
|
|
|
out::chunks[i]->shndx = shndx++;
|
|
|
|
|
|
|
|
for (OutputChunk *chunk : out::chunks)
|
2020-12-07 09:50:13 +03:00
|
|
|
chunk->update_shdr();
|
2020-11-16 18:45:02 +03:00
|
|
|
|
2020-10-30 10:55:59 +03:00
|
|
|
// Assign offsets to output sections
|
2020-11-17 07:48:11 +03:00
|
|
|
u64 filesize = set_osec_offsets(out::chunks);
|
2020-10-19 17:37:29 +03:00
|
|
|
|
2020-11-03 10:51:28 +03:00
|
|
|
// Fix linker-synthesized symbol addresses.
|
2020-11-17 07:48:11 +03:00
|
|
|
fix_synthetic_symbols(out::chunks);
|
2020-11-01 07:05:51 +03:00
|
|
|
|
2020-11-09 03:58:35 +03:00
|
|
|
// At this point, file layout is fixed. Beyond this, you can assume
|
|
|
|
// that symbol addresses including their GOT/PLT/etc addresses have
|
|
|
|
// a correct final value.
|
|
|
|
|
2020-11-11 04:42:26 +03:00
|
|
|
// Some types of relocations for TLS symbols need the ending address
|
|
|
|
// of the TLS section. Find it out now.
|
2020-12-10 15:13:02 +03:00
|
|
|
for (ElfPhdr phdr : create_phdr())
|
2020-11-29 14:19:59 +03:00
|
|
|
if (phdr.p_type == PT_TLS)
|
|
|
|
out::tls_end = align_to(phdr.p_vaddr + phdr.p_memsz, phdr.p_align);
|
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-01-09 11:07:19 +03:00
|
|
|
OutputFile *file = OutputFile::open(config.output, filesize);
|
|
|
|
out::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");
|
2020-11-17 07:48:11 +03:00
|
|
|
tbb::parallel_for_each(out::chunks, [&](OutputChunk *chunk) {
|
2020-11-17 07:56:40 +03:00
|
|
|
chunk->copy_buf();
|
2020-11-09 08:47:51 +03:00
|
|
|
});
|
2021-01-10 08:23:41 +03:00
|
|
|
check_errors();
|
2020-10-30 05:40:38 +03:00
|
|
|
}
|
2020-10-20 03:20:52 +03:00
|
|
|
|
2020-11-09 15:50:47 +03:00
|
|
|
// Zero-clear paddings between sections
|
2020-11-17 07:59:24 +03:00
|
|
|
clear_padding(filesize);
|
2020-11-09 15:50:47 +03:00
|
|
|
|
2020-11-09 10:41:26 +03:00
|
|
|
// Commit
|
2021-01-09 11:07:19 +03:00
|
|
|
file->close();
|
2020-10-14 12:41:09 +03:00
|
|
|
|
2020-12-11 10:51:20 +03:00
|
|
|
t_copy.stop();
|
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
|
|
|
|
2020-12-11 10:51:20 +03:00
|
|
|
if (config.print_map)
|
2020-11-24 08:31:05 +03:00
|
|
|
print_map();
|
2020-10-29 06:24:54 +03:00
|
|
|
|
2020-12-12 06:57:56 +03:00
|
|
|
// Show stats numbers
|
2020-12-21 11:51:20 +03:00
|
|
|
if (Counter::enabled)
|
|
|
|
show_stats();
|
2020-12-13 19:28:43 +03:00
|
|
|
|
2020-12-13 16:41:50 +03:00
|
|
|
if (config.perf)
|
|
|
|
Timer::print();
|
|
|
|
|
|
|
|
std::cout << std::flush;
|
|
|
|
std::cerr << std::flush;
|
2020-12-21 12:52:45 +03:00
|
|
|
if (on_complete)
|
|
|
|
on_complete();
|
2020-12-13 19:30:08 +03:00
|
|
|
std::quick_exit(0);
|
2020-09-29 09:05:29 +03:00
|
|
|
}
|