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_do.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 07:20:51 +03:00
|
|
|
Context ctx;
|
2021-03-28 11:55:23 +03:00
|
|
|
|
2021-03-08 14:48:21 +03:00
|
|
|
i64 BuildId::size() const {
|
|
|
|
switch (kind) {
|
|
|
|
case HEX:
|
|
|
|
return value.size();
|
|
|
|
case HASH:
|
|
|
|
return hash_size;
|
|
|
|
case UUID:
|
|
|
|
return 16;
|
|
|
|
}
|
|
|
|
unreachable();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-03-29 08:36:55 +03:00
|
|
|
static ObjectFile *new_object_file(Context &ctx, MemoryMappedFile *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 09:16:53 +03:00
|
|
|
ObjectFile *file = new ObjectFile(ctx, mb, archive_name, in_lib);
|
|
|
|
ctx.tg.run([file, &ctx]() { file->parse(ctx); });
|
2021-03-29 07:20:51 +03:00
|
|
|
if (ctx.arg.trace)
|
2021-03-12 10:28:13 +03:00
|
|
|
SyncOut() << "trace: " << *file;
|
2020-12-21 13:35:33 +03:00
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2021-03-29 08:36:55 +03:00
|
|
|
static SharedFile *new_shared_file(Context &ctx, MemoryMappedFile *mb) {
|
2021-03-29 09:16:53 +03:00
|
|
|
SharedFile *file = new SharedFile(ctx, mb);
|
|
|
|
ctx.tg.run([file, &ctx]() { file->parse(ctx); });
|
2021-03-29 07:20:51 +03:00
|
|
|
if (ctx.arg.trace)
|
2021-03-12 10:28:13 +03:00
|
|
|
SyncOut() << "trace: " << *file;
|
2020-12-21 13:35:33 +03:00
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2021-01-15 09:50:10 +03:00
|
|
|
template <typename T>
|
|
|
|
class FileCache {
|
|
|
|
public:
|
|
|
|
void store(MemoryMappedFile *mb, T *obj) {
|
|
|
|
Key k(mb->name, mb->size(), mb->mtime);
|
|
|
|
cache[k].push_back(obj);
|
|
|
|
}
|
2020-12-22 14:10:04 +03:00
|
|
|
|
2021-01-15 09:50:10 +03:00
|
|
|
std::vector<T *> get(MemoryMappedFile *mb) {
|
|
|
|
Key k(mb->name, mb->size(), mb->mtime);
|
|
|
|
std::vector<T *> objs = cache[k];
|
2020-12-22 14:20:31 +03:00
|
|
|
cache[k].clear();
|
|
|
|
return objs;
|
2021-01-15 09:50:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
T *get_one(MemoryMappedFile *mb) {
|
|
|
|
std::vector<T *> objs = get(mb);
|
|
|
|
return objs.empty() ? nullptr : objs[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-01-24 06:01:43 +03:00
|
|
|
typedef std::tuple<std::string, i64, i64> Key;
|
2021-01-15 09:50:10 +03:00
|
|
|
std::map<Key, std::vector<T *>> cache;
|
|
|
|
};
|
|
|
|
|
2021-03-29 08:36:55 +03:00
|
|
|
void read_file(Context &ctx, MemoryMappedFile *mb) {
|
|
|
|
if (ctx.visited.contains(mb->name))
|
2021-03-13 18:58:13 +03:00
|
|
|
return;
|
|
|
|
|
2021-01-15 09:50:10 +03:00
|
|
|
static FileCache<ObjectFile> obj_cache;
|
|
|
|
static FileCache<SharedFile> dso_cache;
|
2020-12-22 14:20:31 +03:00
|
|
|
|
2021-03-29 08:36:55 +03:00
|
|
|
if (ctx.is_preloading) {
|
2021-01-15 09:41:09 +03:00
|
|
|
switch (get_file_type(mb)) {
|
|
|
|
case FileType::OBJ:
|
2021-03-29 08:36:55 +03:00
|
|
|
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-03-29 08:36:55 +03:00
|
|
|
dso_cache.store(mb, new_shared_file(ctx, mb));
|
2021-01-15 09:41:09 +03:00
|
|
|
return;
|
|
|
|
case FileType::AR:
|
|
|
|
for (MemoryMappedFile *child : read_fat_archive_members(mb))
|
2021-03-23 10:37:09 +03:00
|
|
|
if (get_file_type(child) == FileType::OBJ)
|
2021-03-29 08:36:55 +03:00
|
|
|
obj_cache.store(mb, new_object_file(ctx, child, mb->name));
|
2021-01-15 09:41:09 +03:00
|
|
|
return;
|
|
|
|
case FileType::THIN_AR:
|
|
|
|
for (MemoryMappedFile *child : read_thin_archive_members(mb))
|
2021-03-23 10:37:09 +03:00
|
|
|
if (get_file_type(child) == FileType::OBJ)
|
2021-03-29 08:36:55 +03:00
|
|
|
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-01-15 09:41:09 +03:00
|
|
|
Fatal() << mb->name << ": unknown file type";
|
|
|
|
}
|
2020-12-22 14:32:49 +03:00
|
|
|
|
2021-01-15 09:41:09 +03:00
|
|
|
switch (get_file_type(mb)) {
|
|
|
|
case FileType::OBJ:
|
2021-01-15 09:50:10 +03:00
|
|
|
if (ObjectFile *obj = 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-01-15 09:50:10 +03:00
|
|
|
if (SharedFile *obj = 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-01-09 13:21:28 +03:00
|
|
|
case FileType::AR:
|
2021-01-15 09:50:10 +03:00
|
|
|
if (std::vector<ObjectFile *> objs = obj_cache.get(mb); !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-23 10:37:09 +03:00
|
|
|
for (MemoryMappedFile *child : read_fat_archive_members(mb))
|
|
|
|
if (get_file_type(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-01-09 13:21:28 +03:00
|
|
|
case FileType::THIN_AR:
|
2020-12-22 14:32:49 +03:00
|
|
|
for (MemoryMappedFile *child : read_thin_archive_members(mb)) {
|
2021-01-15 09:50:10 +03:00
|
|
|
if (ObjectFile *obj = obj_cache.get_one(child))
|
2021-03-29 07:20:51 +03:00
|
|
|
ctx.objs.push_back(obj);
|
2021-03-23 10:37:09 +03:00
|
|
|
else if (get_file_type(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-19 12:31:46 +03:00
|
|
|
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);
|
2020-12-19 12:31:46 +03:00
|
|
|
return;
|
|
|
|
}
|
2021-01-15 09:41:09 +03:00
|
|
|
Fatal() << mb->name << ": unknown file type";
|
2020-10-10 06:47:12 +03:00
|
|
|
}
|
|
|
|
|
2020-10-28 07:42:05 +03:00
|
|
|
template <typename T>
|
2021-01-24 06:01:43 +03:00
|
|
|
static std::vector<std::span<T>> split(std::vector<T> &input, i64 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;
|
|
|
|
}
|
|
|
|
|
2021-03-29 10:24:40 +03:00
|
|
|
static void apply_exclude_libs(Context &ctx) {
|
2021-03-08 09:25:23 +03:00
|
|
|
Timer t("apply_exclude_libs");
|
|
|
|
|
2021-03-29 07:20:51 +03:00
|
|
|
if (ctx.arg.exclude_libs.empty())
|
2021-03-08 09:25:23 +03:00
|
|
|
return;
|
|
|
|
|
2021-03-29 07:20:51 +03:00
|
|
|
std::unordered_set<std::string_view> set(ctx.arg.exclude_libs.begin(),
|
|
|
|
ctx.arg.exclude_libs.end());
|
2021-03-08 09:25:23 +03:00
|
|
|
|
2021-03-29 07:20:51 +03:00
|
|
|
for (ObjectFile *file : ctx.objs)
|
2021-03-08 09:25:23 +03:00
|
|
|
if (!file->archive_name.empty())
|
|
|
|
if (set.contains("ALL") || set.contains(file->archive_name))
|
|
|
|
file->exclude_libs = true;
|
|
|
|
}
|
|
|
|
|
2021-03-29 10:24:40 +03:00
|
|
|
static void create_synthetic_sections(Context &ctx) {
|
|
|
|
auto add = [&](OutputChunk *chunk) {
|
2021-03-29 07:20:51 +03:00
|
|
|
ctx.chunks.push_back(chunk);
|
2021-03-13 07:35:59 +03:00
|
|
|
};
|
|
|
|
|
2021-03-29 07:20:51 +03:00
|
|
|
add(ctx.ehdr = new OutputEhdr);
|
|
|
|
add(ctx.phdr = new OutputPhdr);
|
|
|
|
add(ctx.shdr = new OutputShdr);
|
|
|
|
add(ctx.got = new GotSection);
|
|
|
|
add(ctx.gotplt = new GotPltSection);
|
|
|
|
add(ctx.relplt = new RelPltSection);
|
|
|
|
add(ctx.strtab = new StrtabSection);
|
|
|
|
add(ctx.shstrtab = new ShstrtabSection);
|
|
|
|
add(ctx.plt = new PltSection);
|
|
|
|
add(ctx.pltgot = new PltGotSection);
|
|
|
|
add(ctx.symtab = new SymtabSection);
|
|
|
|
add(ctx.dynsym = new DynsymSection);
|
|
|
|
add(ctx.dynstr = new DynstrSection);
|
|
|
|
add(ctx.eh_frame = new EhFrameSection);
|
|
|
|
add(ctx.dynbss = new DynbssSection(false));
|
|
|
|
add(ctx.dynbss_relro = new DynbssSection(true));
|
|
|
|
|
|
|
|
if (!ctx.arg.dynamic_linker.empty())
|
|
|
|
add(ctx.interp = new InterpSection);
|
|
|
|
if (ctx.arg.build_id.kind != BuildId::NONE)
|
|
|
|
add(ctx.buildid = new BuildIdSection);
|
|
|
|
if (ctx.arg.eh_frame_hdr)
|
|
|
|
add(ctx.eh_frame_hdr = new EhFrameHdrSection);
|
|
|
|
if (ctx.arg.hash_style_sysv)
|
|
|
|
add(ctx.hash = new HashSection);
|
|
|
|
if (ctx.arg.hash_style_gnu)
|
|
|
|
add(ctx.gnu_hash = new GnuHashSection);
|
|
|
|
if (!ctx.arg.version_definitions.empty())
|
|
|
|
add(ctx.verdef = new VerdefSection);
|
|
|
|
|
|
|
|
add(ctx.reldyn = new RelDynSection);
|
|
|
|
add(ctx.dynamic = new DynamicSection);
|
|
|
|
add(ctx.versym = new VersymSection);
|
|
|
|
add(ctx.verneed = new VerneedSection);
|
2021-03-10 21:04:00 +03:00
|
|
|
}
|
|
|
|
|
2021-03-29 10:24:40 +03:00
|
|
|
static void set_file_priority(Context &ctx) {
|
2021-03-13 05:13:37 +03:00
|
|
|
// File priority 1 is reserved for the internal file.
|
|
|
|
i64 priority = 2;
|
|
|
|
|
2021-03-29 07:20:51 +03:00
|
|
|
for (ObjectFile *file : ctx.objs)
|
2021-03-13 05:13:37 +03:00
|
|
|
if (!file->is_in_lib)
|
|
|
|
file->priority = priority++;
|
2021-03-29 07:20:51 +03:00
|
|
|
for (ObjectFile *file : ctx.objs)
|
2021-03-13 05:13:37 +03:00
|
|
|
if (file->is_in_lib)
|
|
|
|
file->priority = priority++;
|
2021-03-29 07:20:51 +03:00
|
|
|
for (SharedFile *file : ctx.dsos)
|
2021-03-13 05:13:37 +03:00
|
|
|
file->priority = priority++;
|
|
|
|
}
|
|
|
|
|
2021-03-29 10:24:40 +03:00
|
|
|
static void resolve_obj_symbols(Context &ctx) {
|
2021-03-10 18:59:59 +03:00
|
|
|
Timer t("resolve_obj_symbols");
|
2020-11-11 04:42:26 +03:00
|
|
|
|
2021-03-10 16:39:23 +03:00
|
|
|
// Register archive symbols
|
2021-03-29 09:16:53 +03:00
|
|
|
tbb::parallel_for_each(ctx.objs, [&](ObjectFile *file) {
|
2021-03-10 16:39:23 +03:00
|
|
|
if (file->is_in_lib)
|
2021-03-29 09:16:53 +03:00
|
|
|
file->resolve_lazy_symbols(ctx);
|
2021-03-10 16:39:23 +03:00
|
|
|
});
|
|
|
|
|
2020-11-11 04:51:30 +03:00
|
|
|
// Register defined symbols
|
2021-03-29 09:16:53 +03:00
|
|
|
tbb::parallel_for_each(ctx.objs, [&](ObjectFile *file) {
|
2021-03-10 16:39:23 +03:00
|
|
|
if (!file->is_in_lib)
|
2021-03-29 09:16:53 +03:00
|
|
|
file->resolve_regular_symbols(ctx);
|
2021-01-24 07:34:12 +03:00
|
|
|
});
|
|
|
|
|
2021-03-10 18:59:59 +03:00
|
|
|
// Mark reachable objects to decide which files to include
|
2020-11-30 10:43:47 +03:00
|
|
|
// into an output.
|
2021-01-25 08:02:08 +03:00
|
|
|
std::vector<ObjectFile *> roots;
|
2021-03-29 07:20:51 +03:00
|
|
|
for (ObjectFile *file : ctx.objs)
|
2020-11-24 08:31:05 +03:00
|
|
|
if (file->is_alive)
|
2021-01-25 08:02:08 +03:00
|
|
|
roots.push_back(file);
|
|
|
|
|
2021-03-29 07:20:51 +03:00
|
|
|
for (std::string_view name : ctx.arg.undefined)
|
2021-02-21 11:17:41 +03:00
|
|
|
if (InputFile *file = Symbol::intern(name)->file)
|
|
|
|
if (!file->is_alive.exchange(true) && !file->is_dso)
|
|
|
|
roots.push_back((ObjectFile *)file);
|
|
|
|
|
2021-01-25 08:02:08 +03:00
|
|
|
tbb::parallel_do(roots,
|
|
|
|
[&](ObjectFile *file,
|
|
|
|
tbb::parallel_do_feeder<ObjectFile *> &feeder) {
|
2021-03-29 09:16:53 +03:00
|
|
|
file->mark_live_objects(ctx, [&](ObjectFile *obj) {
|
|
|
|
feeder.add(obj);
|
|
|
|
});
|
2021-01-25 08:02:08 +03:00
|
|
|
});
|
2020-11-11 04:42:26 +03:00
|
|
|
|
2021-03-10 18:59:59 +03:00
|
|
|
// Remove symbols of eliminated objects.
|
2021-03-29 07:20:51 +03:00
|
|
|
tbb::parallel_for_each(ctx.objs, [](ObjectFile *file) {
|
2021-03-13 19:05:46 +03:00
|
|
|
Symbol null_sym;
|
2021-03-10 16:05:31 +03:00
|
|
|
if (!file->is_alive)
|
|
|
|
for (Symbol *sym : file->get_global_syms())
|
|
|
|
if (sym->file == file)
|
2021-03-13 21:18:11 +03:00
|
|
|
sym->clear();
|
2021-03-10 16:05:31 +03:00
|
|
|
});
|
2021-03-13 19:05:46 +03:00
|
|
|
|
|
|
|
// Eliminate unused archive members.
|
2021-03-29 07:20:51 +03:00
|
|
|
erase(ctx.objs, [](InputFile *file) { return !file->is_alive; });
|
2021-03-10 18:59:59 +03:00
|
|
|
}
|
|
|
|
|
2021-03-29 10:24:40 +03:00
|
|
|
static void resolve_dso_symbols(Context &ctx) {
|
2021-03-10 18:59:59 +03:00
|
|
|
Timer t("resolve_dso_symbols");
|
2021-03-10 16:05:31 +03:00
|
|
|
|
2021-03-10 18:59:59 +03:00
|
|
|
// Register DSO symbols
|
2021-03-29 07:20:51 +03:00
|
|
|
tbb::parallel_for_each(ctx.dsos, [](SharedFile *file) {
|
2021-03-10 18:59:59 +03:00
|
|
|
file->resolve_symbols();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Mark live DSOs
|
2021-03-29 07:20:51 +03:00
|
|
|
tbb::parallel_for_each(ctx.objs, [](ObjectFile *file) {
|
2021-03-10 18:59:59 +03:00
|
|
|
for (i64 i = file->first_global; i < file->elf_syms.size(); i++) {
|
|
|
|
const ElfSym &esym = file->elf_syms[i];
|
|
|
|
if (esym.is_defined())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Symbol &sym = *file->symbols[i];
|
|
|
|
if (!sym.file || !sym.file->is_dso)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
sym.file->is_alive = true;
|
|
|
|
|
|
|
|
if (esym.st_bind != STB_WEAK) {
|
|
|
|
std::lock_guard lock(sym.mu);
|
|
|
|
sym.is_weak = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Remove symbols of unreferenced DSOs.
|
2021-03-29 07:20:51 +03:00
|
|
|
tbb::parallel_for_each(ctx.dsos, [](SharedFile *file) {
|
2021-03-13 19:05:46 +03:00
|
|
|
Symbol null_sym;
|
2021-03-10 16:05:31 +03:00
|
|
|
if (!file->is_alive)
|
|
|
|
for (Symbol *sym : file->symbols)
|
|
|
|
if (sym->file == file)
|
2021-03-13 21:18:11 +03:00
|
|
|
sym->clear();
|
2021-03-10 16:05:31 +03:00
|
|
|
});
|
2021-03-13 19:05:46 +03:00
|
|
|
|
|
|
|
// Remove unreferenced DSOs
|
2021-03-29 07:20:51 +03:00
|
|
|
erase(ctx.dsos, [](InputFile *file) { return !file->is_alive; });
|
2020-11-11 04:42:26 +03:00
|
|
|
}
|
|
|
|
|
2021-03-29 10:24:40 +03:00
|
|
|
static void eliminate_comdats(Context &ctx) {
|
2021-03-15 08:07:23 +03:00
|
|
|
Timer t("eliminate_comdats");
|
2020-11-11 04:42:26 +03:00
|
|
|
|
2021-03-29 07:20:51 +03:00
|
|
|
tbb::parallel_for_each(ctx.objs, [](ObjectFile *file) {
|
2020-11-08 12:17:24 +03:00
|
|
|
file->resolve_comdat_groups();
|
|
|
|
});
|
|
|
|
|
2021-03-29 07:20:51 +03:00
|
|
|
tbb::parallel_for_each(ctx.objs, [](ObjectFile *file) {
|
2020-11-08 12:17:24 +03:00
|
|
|
file->eliminate_duplicate_comdat_groups();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-29 10:24:40 +03:00
|
|
|
static void convert_common_symbols(Context &ctx) {
|
2021-03-13 05:13:37 +03:00
|
|
|
Timer t("convert_common_symbols");
|
|
|
|
|
2021-03-29 10:24:40 +03:00
|
|
|
tbb::parallel_for_each(ctx.objs, [&](ObjectFile *file) {
|
2021-03-29 09:16:53 +03:00
|
|
|
file->convert_common_symbols(ctx);
|
2021-03-13 05:13:37 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-29 10:24:40 +03:00
|
|
|
static std::string get_cmdline_args(Context &ctx) {
|
2021-03-28 11:55:23 +03:00
|
|
|
std::stringstream ss;
|
2021-03-29 08:05:55 +03:00
|
|
|
ss << ctx.cmdline_args[0];
|
|
|
|
for (std::string_view arg : std::span(ctx.cmdline_args).subspan(1))
|
2021-03-28 11:55:23 +03:00
|
|
|
ss << " " << arg;
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void add_comment_string(std::string str) {
|
|
|
|
char *buf = strdup(str.c_str());
|
|
|
|
MergedSection *sec =
|
|
|
|
MergedSection::get_instance(".comment", SHT_PROGBITS, 0);
|
|
|
|
SectionFragment *frag = sec->insert({buf, strlen(buf) + 1}, 1);
|
|
|
|
frag->is_alive = true;
|
|
|
|
}
|
|
|
|
|
2021-03-29 10:24:40 +03:00
|
|
|
static void compute_merged_section_sizes(Context &ctx) {
|
2021-03-13 14:36:23 +03:00
|
|
|
Timer t("compute_merged_section_sizes");
|
2020-11-11 04:42:26 +03:00
|
|
|
|
2021-03-17 15:52:58 +03:00
|
|
|
// Mark section fragments referenced by live objects.
|
2021-03-29 07:20:51 +03:00
|
|
|
if (!ctx.arg.gc_sections) {
|
|
|
|
tbb::parallel_for_each(ctx.objs, [](ObjectFile *file) {
|
2021-03-17 15:52:58 +03:00
|
|
|
for (SectionFragment *frag : file->fragments)
|
|
|
|
frag->is_alive = true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-11 11:24:23 +03:00
|
|
|
// Add an identification string to .comment.
|
2021-03-28 11:55:23 +03:00
|
|
|
add_comment_string("mold " GIT_HASH);
|
|
|
|
|
|
|
|
// Also embed command line arguments for now for debugging.
|
2021-03-29 10:24:40 +03:00
|
|
|
add_comment_string("mold command line: " + get_cmdline_args(ctx));
|
2021-03-11 11:24:23 +03:00
|
|
|
|
2021-03-11 11:06:43 +03:00
|
|
|
tbb::parallel_for_each(MergedSection::instances, [](MergedSection *sec) {
|
|
|
|
sec->assign_offsets();
|
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.
|
2021-03-29 10:24:40 +03:00
|
|
|
static void bin_sections(Context &ctx) {
|
2021-01-09 11:45:50 +03:00
|
|
|
Timer t("bin_sections");
|
2020-11-11 04:42:26 +03:00
|
|
|
|
2021-03-29 07:20:51 +03:00
|
|
|
i64 unit = (ctx.objs.size() + 127) / 128;
|
|
|
|
std::vector<std::span<ObjectFile *>> slices = split(ctx.objs, unit);
|
2020-10-26 07:36:56 +03:00
|
|
|
|
2021-01-24 06:01:43 +03:00
|
|
|
i64 num_osec = OutputSection::instances.size();
|
2020-11-08 04:05:59 +03:00
|
|
|
|
2021-01-17 10:40:35 +03:00
|
|
|
std::vector<std::vector<std::vector<InputSection *>>> groups(slices.size());
|
2021-01-24 06:01:43 +03:00
|
|
|
for (i64 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
|
|
|
|
2021-01-24 06:01:43 +03:00
|
|
|
tbb::parallel_for((i64)0, (i64)slices.size(), [&](i64 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
|
|
|
});
|
|
|
|
|
2021-01-24 06:01:43 +03:00
|
|
|
std::vector<i64> sizes(num_osec);
|
2020-10-26 07:36:56 +03:00
|
|
|
|
2021-01-17 10:40:35 +03:00
|
|
|
for (std::span<std::vector<InputSection *>> group : groups)
|
2021-01-24 06:01:43 +03:00
|
|
|
for (i64 i = 0; i < group.size(); i++)
|
2020-10-28 08:22:25 +03:00
|
|
|
sizes[i] += group[i].size();
|
2020-11-08 03:44:27 +03:00
|
|
|
|
2021-01-24 06:01:43 +03:00
|
|
|
tbb::parallel_for((i64)0, num_osec, [&](i64 j) {
|
2020-11-08 06:42:40 +03:00
|
|
|
OutputSection::instances[j]->members.reserve(sizes[j]);
|
2021-01-24 06:01:43 +03:00
|
|
|
for (i64 i = 0; i < groups.size(); i++)
|
2020-12-20 10:52:10 +03:00
|
|
|
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-03-29 10:24:40 +03:00
|
|
|
static void check_duplicate_symbols(Context &ctx) {
|
2021-01-10 10:37:24 +03:00
|
|
|
Timer t("check_dup_syms");
|
2020-12-20 11:08:27 +03:00
|
|
|
|
2021-03-29 07:20:51 +03:00
|
|
|
tbb::parallel_for_each(ctx.objs, [&](ObjectFile *file) {
|
2021-01-24 06:01:43 +03:00
|
|
|
for (i64 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];
|
2021-03-23 19:06:24 +03:00
|
|
|
bool is_common = esym.is_common();
|
2021-01-10 07:52:59 +03:00
|
|
|
bool is_weak = (esym.st_bind == STB_WEAK);
|
|
|
|
bool is_eliminated =
|
2021-03-04 16:08:57 +03:00
|
|
|
!esym.is_abs() && !esym.is_common() && !file->get_section(esym);
|
2021-01-10 07:52:59 +03:00
|
|
|
|
2021-03-23 19:06:24 +03:00
|
|
|
if (sym.file != file && esym.is_defined() && !is_common &&
|
|
|
|
!is_weak && !is_eliminated)
|
2021-01-13 17:52:11 +03:00
|
|
|
Error() << "duplicate symbol: " << *file << ": " << *sym.file
|
2021-02-27 06:08:55 +03:00
|
|
|
<< ": " << sym;
|
2020-11-20 11:12:45 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-01-13 17:52:11 +03:00
|
|
|
Error::checkpoint();
|
2020-11-20 11:12:45 +03:00
|
|
|
}
|
|
|
|
|
2021-03-13 06:44:37 +03:00
|
|
|
std::vector<OutputChunk *> collect_output_sections() {
|
|
|
|
std::vector<OutputChunk *> vec;
|
|
|
|
|
|
|
|
for (OutputSection *osec : OutputSection::instances)
|
2021-03-13 07:05:33 +03:00
|
|
|
if (!osec->members.empty())
|
2021-03-13 06:44:37 +03:00
|
|
|
vec.push_back(osec);
|
|
|
|
for (MergedSection *osec : MergedSection::instances)
|
|
|
|
if (osec->shdr.sh_size)
|
|
|
|
vec.push_back(osec);
|
|
|
|
|
|
|
|
// Sections are added to the section lists in an arbitrary order because
|
|
|
|
// they are created in parallel.
|
|
|
|
// Sort them to to make the output deterministic.
|
|
|
|
sort(vec, [](OutputChunk *x, OutputChunk *y) {
|
|
|
|
return std::tuple(x->name, x->shdr.sh_type, x->shdr.sh_flags) <
|
|
|
|
std::tuple(y->name, y->shdr.sh_type, y->shdr.sh_flags);
|
|
|
|
});
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
2021-03-29 10:24:40 +03:00
|
|
|
static void compute_section_sizes(Context &ctx) {
|
2021-03-15 07:47:34 +03:00
|
|
|
Timer t("compute_section_sizes");
|
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;
|
|
|
|
|
2021-03-13 07:05:33 +03:00
|
|
|
std::vector<std::span<InputSection *>> slices =
|
|
|
|
split(osec->members, 10000);
|
|
|
|
|
2021-01-24 06:01:43 +03:00
|
|
|
std::vector<i64> size(slices.size());
|
|
|
|
std::vector<i64> alignments(slices.size());
|
2020-10-26 08:18:00 +03:00
|
|
|
|
2021-01-24 06:01:43 +03:00
|
|
|
tbb::parallel_for((i64)0, (i64)slices.size(), [&](i64 i) {
|
|
|
|
i64 off = 0;
|
|
|
|
i64 align = 1;
|
2020-10-26 10:12:35 +03:00
|
|
|
|
2021-03-11 11:58:43 +03:00
|
|
|
for (InputSection *isec : slices[i]) {
|
2021-03-11 13:59:01 +03:00
|
|
|
off = align_to(off, isec->shdr.sh_addralign);
|
2020-10-26 10:12:35 +03:00
|
|
|
isec->offset = off;
|
2021-03-11 13:59:01 +03:00
|
|
|
off += isec->shdr.sh_size;
|
|
|
|
align = std::max<i64>(align, isec->shdr.sh_addralign);
|
2020-10-26 10:12:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
size[i] = off;
|
|
|
|
alignments[i] = align;
|
|
|
|
});
|
|
|
|
|
2021-01-24 06:01:43 +03:00
|
|
|
i64 align = *std::max_element(alignments.begin(), alignments.end());
|
2020-10-26 10:12:35 +03:00
|
|
|
|
2021-01-24 06:01:43 +03:00
|
|
|
std::vector<i64> start(slices.size());
|
|
|
|
for (i64 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
|
|
|
|
2021-01-24 06:01:43 +03:00
|
|
|
tbb::parallel_for((i64)1, (i64)slices.size(), [&](i64 i) {
|
2021-03-11 11:58:43 +03:00
|
|
|
for (InputSection *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;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-29 10:24:40 +03:00
|
|
|
static void convert_undefined_weak_symbols(Context &ctx) {
|
2021-03-13 15:29:02 +03:00
|
|
|
Timer t("undef_weak");
|
|
|
|
|
2021-03-29 10:24:40 +03:00
|
|
|
tbb::parallel_for_each(ctx.objs, [&](ObjectFile *file) {
|
2021-03-29 09:16:53 +03:00
|
|
|
file->convert_undefined_weak_symbols(ctx);
|
2021-03-13 15:29:02 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-29 10:24:40 +03:00
|
|
|
static void scan_rels(Context &ctx) {
|
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.
|
2021-03-29 07:20:51 +03:00
|
|
|
tbb::parallel_for_each(ctx.objs, [&](ObjectFile *file) {
|
2021-03-29 09:53:00 +03:00
|
|
|
file->scan_relocations(ctx);
|
2020-11-17 14:54:13 +03:00
|
|
|
});
|
2020-11-17 13:56:02 +03:00
|
|
|
|
2021-01-13 17:52:11 +03:00
|
|
|
// Exit if there was a relocation that refers an undefined symbol.
|
|
|
|
Error::checkpoint();
|
2020-11-26 12:09:32 +03:00
|
|
|
|
2021-03-08 11:58:25 +03:00
|
|
|
// Add imported or exported symbols to .dynsym.
|
2021-03-29 07:20:51 +03:00
|
|
|
tbb::parallel_for_each(ctx.objs, [&](ObjectFile *file) {
|
2021-03-10 15:37:25 +03:00
|
|
|
for (Symbol *sym : file->get_global_syms())
|
2021-03-08 11:36:46 +03:00
|
|
|
if (sym->file == file)
|
|
|
|
if (sym->is_imported || sym->is_exported)
|
|
|
|
sym->flags |= NEEDS_DYNSYM;
|
2021-03-04 10:16:37 +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;
|
2021-03-29 07:20:51 +03:00
|
|
|
append(files, ctx.objs);
|
|
|
|
append(files, ctx.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
|
|
|
|
2021-01-24 06:01:43 +03:00
|
|
|
tbb::parallel_for((i64)0, (i64)files.size(), [&](i64 i) {
|
2020-11-24 08:31:05 +03:00
|
|
|
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)) {
|
2021-03-04 10:16:37 +03:00
|
|
|
if (sym->flags & NEEDS_DYNSYM)
|
2021-03-29 10:02:12 +03:00
|
|
|
ctx.dynsym->add_symbol(ctx, sym);
|
2020-12-13 15:57:45 +03:00
|
|
|
|
2020-12-16 12:38:13 +03:00
|
|
|
if (sym->flags & NEEDS_GOT)
|
2021-03-29 10:02:12 +03:00
|
|
|
ctx.got->add_got_symbol(ctx, sym);
|
2020-11-06 07:54:37 +03:00
|
|
|
|
2021-02-26 14:56:01 +03:00
|
|
|
if (sym->flags & NEEDS_PLT) {
|
|
|
|
if (sym->flags & NEEDS_GOT)
|
2021-03-29 10:02:12 +03:00
|
|
|
ctx.pltgot->add_symbol(ctx, sym);
|
2021-02-26 14:56:01 +03:00
|
|
|
else
|
2021-03-29 10:02:12 +03:00
|
|
|
ctx.plt->add_symbol(ctx, sym);
|
2021-02-26 14:56:01 +03:00
|
|
|
}
|
2020-11-17 14:22:52 +03:00
|
|
|
|
2020-12-16 12:38:13 +03:00
|
|
|
if (sym->flags & NEEDS_GOTTPOFF)
|
2021-03-29 10:02:12 +03:00
|
|
|
ctx.got->add_gottpoff_symbol(ctx, sym);
|
2020-11-17 14:22:52 +03:00
|
|
|
|
2020-12-16 12:38:13 +03:00
|
|
|
if (sym->flags & NEEDS_TLSGD)
|
2021-03-29 10:02:12 +03:00
|
|
|
ctx.got->add_tlsgd_symbol(ctx, sym);
|
2020-11-21 04:48:23 +03:00
|
|
|
|
2021-03-18 08:49:57 +03:00
|
|
|
if (sym->flags & NEEDS_TLSDESC)
|
2021-03-29 10:02:12 +03:00
|
|
|
ctx.got->add_tlsdesc_symbol(ctx, sym);
|
2021-03-18 08:49:57 +03:00
|
|
|
|
2020-12-16 12:38:13 +03:00
|
|
|
if (sym->flags & NEEDS_TLSLD)
|
2021-03-29 10:02:12 +03:00
|
|
|
ctx.got->add_tlsld(ctx);
|
2020-11-25 11:20:48 +03:00
|
|
|
|
2020-12-16 12:38:13 +03:00
|
|
|
if (sym->flags & NEEDS_COPYREL) {
|
2020-11-25 14:35:04 +03:00
|
|
|
assert(sym->file->is_dso);
|
2021-02-28 17:19:29 +03:00
|
|
|
SharedFile *file = (SharedFile *)sym->file;
|
2021-03-12 08:20:10 +03:00
|
|
|
sym->copyrel_readonly = file->is_readonly(sym);
|
2020-11-25 14:35:04 +03:00
|
|
|
|
2021-03-12 08:20:10 +03:00
|
|
|
if (sym->copyrel_readonly)
|
2021-03-29 09:53:00 +03:00
|
|
|
ctx.dynbss_relro->add_symbol(ctx, sym);
|
2021-02-28 17:19:29 +03:00
|
|
|
else
|
2021-03-29 09:53:00 +03:00
|
|
|
ctx.dynbss->add_symbol(ctx, sym);
|
2021-02-28 17:19:29 +03:00
|
|
|
|
|
|
|
for (Symbol *alias : file->find_aliases(sym)) {
|
2021-01-14 14:31:39 +03:00
|
|
|
alias->has_copyrel = true;
|
|
|
|
alias->value = sym->value;
|
2021-03-12 08:20:10 +03:00
|
|
|
alias->copyrel_readonly = sym->copyrel_readonly;
|
2021-03-29 10:02:12 +03:00
|
|
|
ctx.dynsym->add_symbol(ctx, alias);
|
2020-11-25 14:35:04 +03:00
|
|
|
}
|
|
|
|
}
|
2020-11-18 15:45:49 +03:00
|
|
|
}
|
2020-11-16 16:25:58 +03:00
|
|
|
}
|
2020-11-11 11:25:00 +03:00
|
|
|
|
2021-03-29 10:24:40 +03:00
|
|
|
static void apply_version_script(Context &ctx) {
|
2021-03-08 08:07:21 +03:00
|
|
|
Timer t("apply_version_script");
|
2021-03-07 14:36:10 +03:00
|
|
|
|
2021-03-29 07:20:51 +03:00
|
|
|
for (VersionPattern &elem : ctx.arg.version_patterns) {
|
2021-03-27 09:25:45 +03:00
|
|
|
assert(elem.pattern != "*");
|
2021-03-07 14:36:10 +03:00
|
|
|
|
2021-03-27 09:25:45 +03:00
|
|
|
if (!elem.is_extern_cpp &&
|
|
|
|
elem.pattern.find('*') == elem.pattern.npos) {
|
|
|
|
Symbol::intern(elem.pattern)->ver_idx = elem.ver_idx;
|
2021-03-17 10:48:36 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-03-27 09:25:45 +03:00
|
|
|
GlobPattern glob(elem.pattern);
|
2021-03-17 10:48:36 +03:00
|
|
|
|
2021-03-29 07:20:51 +03:00
|
|
|
tbb::parallel_for_each(ctx.objs, [&](ObjectFile *file) {
|
2021-03-27 09:25:45 +03:00
|
|
|
for (Symbol *sym : file->get_global_syms()) {
|
|
|
|
if (sym->file == file) {
|
|
|
|
std::string_view name = elem.is_extern_cpp
|
|
|
|
? sym->get_demangled_name() : sym->name;
|
|
|
|
if (glob.match(name))
|
|
|
|
sym->ver_idx = elem.ver_idx;
|
|
|
|
}
|
|
|
|
}
|
2021-03-17 10:48:36 +03:00
|
|
|
});
|
2021-03-07 14:36:10 +03:00
|
|
|
}
|
2021-03-07 07:43:58 +03:00
|
|
|
}
|
|
|
|
|
2021-03-29 10:24:40 +03:00
|
|
|
static void parse_symbol_version(Context &ctx) {
|
2021-03-17 10:26:56 +03:00
|
|
|
Timer t("parse_symbol_version");
|
2021-03-09 15:28:44 +03:00
|
|
|
|
|
|
|
std::unordered_map<std::string_view, u16> verdefs;
|
2021-03-29 07:20:51 +03:00
|
|
|
for (i64 i = 0; i < ctx.arg.version_definitions.size(); i++)
|
|
|
|
verdefs[ctx.arg.version_definitions[i]] = i + VER_NDX_LAST_RESERVED + 1;
|
2021-03-09 15:28:44 +03:00
|
|
|
|
2021-03-29 07:20:51 +03:00
|
|
|
tbb::parallel_for_each(ctx.objs, [&](ObjectFile *file) {
|
2021-03-09 17:43:36 +03:00
|
|
|
for (i64 i = 0; i < file->symbols.size() - file->first_global; i++) {
|
|
|
|
if (!file->symvers[i])
|
2021-03-09 15:28:44 +03:00
|
|
|
continue;
|
|
|
|
|
2021-03-09 17:43:36 +03:00
|
|
|
Symbol *sym = file->symbols[i + file->first_global];
|
|
|
|
if (sym->file != file)
|
|
|
|
continue;
|
2021-03-09 15:28:44 +03:00
|
|
|
|
2021-03-09 17:43:36 +03:00
|
|
|
std::string_view ver = file->symvers[i];
|
2021-03-09 15:28:44 +03:00
|
|
|
|
|
|
|
bool is_default = false;
|
|
|
|
if (ver.starts_with('@')) {
|
|
|
|
is_default = true;
|
|
|
|
ver = ver.substr(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto it = verdefs.find(ver);
|
|
|
|
if (it == verdefs.end()) {
|
|
|
|
Error() << *file << ": symbol " << *sym << " has undefined version "
|
|
|
|
<< ver;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
sym->ver_idx = it->second;
|
|
|
|
if (!is_default)
|
|
|
|
sym->ver_idx |= VERSYM_HIDDEN;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-29 10:24:40 +03:00
|
|
|
static void compute_import_export(Context &ctx) {
|
2021-03-08 08:07:21 +03:00
|
|
|
Timer t("compute_import_export");
|
|
|
|
|
2021-03-12 14:50:22 +03:00
|
|
|
// Export symbols referenced by DSOs.
|
2021-03-29 07:20:51 +03:00
|
|
|
if (!ctx.arg.shared) {
|
|
|
|
tbb::parallel_for_each(ctx.dsos, [&](SharedFile *file) {
|
2021-03-12 14:50:22 +03:00
|
|
|
for (Symbol *sym : file->undefs)
|
|
|
|
if (sym->file && !sym->file->is_dso && sym->visibility != STV_HIDDEN)
|
|
|
|
sym->is_exported = true;
|
|
|
|
});
|
|
|
|
}
|
2021-03-08 08:07:21 +03:00
|
|
|
|
2021-03-17 10:26:56 +03:00
|
|
|
// Global symbols are exported from DSO by default.
|
2021-03-29 07:20:51 +03:00
|
|
|
if (ctx.arg.shared || ctx.arg.export_dynamic) {
|
2021-03-29 10:24:40 +03:00
|
|
|
tbb::parallel_for_each(ctx.objs, [&](ObjectFile *file) {
|
2021-03-12 14:50:22 +03:00
|
|
|
for (Symbol *sym : file->get_global_syms()) {
|
|
|
|
if (sym->file != file)
|
|
|
|
continue;
|
2021-03-08 08:07:21 +03:00
|
|
|
|
2021-03-12 14:50:22 +03:00
|
|
|
if (sym->visibility == STV_HIDDEN || sym->ver_idx == VER_NDX_LOCAL)
|
|
|
|
continue;
|
2021-03-08 08:07:21 +03:00
|
|
|
|
2021-03-12 14:50:22 +03:00
|
|
|
sym->is_exported = true;
|
2021-03-09 15:28:44 +03:00
|
|
|
|
2021-03-29 07:20:51 +03:00
|
|
|
if (ctx.arg.shared && sym->visibility != STV_PROTECTED &&
|
|
|
|
!ctx.arg.Bsymbolic &&
|
|
|
|
!(ctx.arg.Bsymbolic_functions && sym->get_type() == STT_FUNC))
|
2021-03-12 14:50:22 +03:00
|
|
|
sym->is_imported = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2021-03-08 08:07:21 +03:00
|
|
|
}
|
|
|
|
|
2021-03-29 10:24:40 +03:00
|
|
|
static void fill_verdef(Context &ctx) {
|
2021-03-07 07:43:58 +03:00
|
|
|
Timer t("fill_verdef");
|
|
|
|
|
2021-03-29 07:20:51 +03:00
|
|
|
if (ctx.arg.version_definitions.empty())
|
2021-03-07 07:43:58 +03:00
|
|
|
return;
|
|
|
|
|
2021-03-11 14:19:37 +03:00
|
|
|
// Resize .gnu.version
|
2021-03-29 07:20:51 +03:00
|
|
|
ctx.versym->contents.resize(ctx.dynsym->symbols.size(), 1);
|
|
|
|
ctx.versym->contents[0] = 0;
|
2021-03-07 07:43:58 +03:00
|
|
|
|
|
|
|
// Allocate a buffer for .gnu.version_d.
|
2021-03-29 07:20:51 +03:00
|
|
|
ctx.verdef->contents.resize((sizeof(ElfVerdef) + sizeof(ElfVerdaux)) *
|
|
|
|
(ctx.arg.version_definitions.size() + 1));
|
2021-03-07 07:43:58 +03:00
|
|
|
|
2021-03-29 07:20:51 +03:00
|
|
|
u8 *buf = (u8 *)&ctx.verdef->contents[0];
|
2021-03-07 07:43:58 +03:00
|
|
|
u8 *ptr = buf;
|
|
|
|
ElfVerdef *verdef = nullptr;
|
|
|
|
|
|
|
|
auto write = [&](std::string_view verstr, i64 idx, i64 flags) {
|
2021-03-29 07:20:51 +03:00
|
|
|
ctx.verdef->shdr.sh_info++;
|
2021-03-07 07:43:58 +03:00
|
|
|
if (verdef)
|
|
|
|
verdef->vd_next = ptr - (u8 *)verdef;
|
|
|
|
|
|
|
|
verdef = (ElfVerdef *)ptr;
|
|
|
|
ptr += sizeof(ElfVerdef);
|
|
|
|
|
|
|
|
verdef->vd_version = 1;
|
|
|
|
verdef->vd_flags = flags;
|
|
|
|
verdef->vd_ndx = idx;
|
|
|
|
verdef->vd_cnt = 1;
|
|
|
|
verdef->vd_hash = elf_hash(verstr);
|
|
|
|
verdef->vd_aux = sizeof(ElfVerdef);
|
|
|
|
|
|
|
|
ElfVerdaux *aux = (ElfVerdaux *)ptr;
|
|
|
|
ptr += sizeof(ElfVerdaux);
|
2021-03-29 07:20:51 +03:00
|
|
|
aux->vda_name = ctx.dynstr->add_string(verstr);
|
2021-03-07 07:43:58 +03:00
|
|
|
};
|
|
|
|
|
2021-03-29 07:20:51 +03:00
|
|
|
std::string_view basename = ctx.arg.soname.empty() ?
|
|
|
|
ctx.arg.output : ctx.arg.soname;
|
2021-03-07 07:43:58 +03:00
|
|
|
write(basename, 1, VER_FLG_BASE);
|
|
|
|
|
|
|
|
i64 idx = 2;
|
2021-03-29 07:20:51 +03:00
|
|
|
for (std::string_view verstr : ctx.arg.version_definitions)
|
2021-03-07 07:43:58 +03:00
|
|
|
write(verstr, idx++, 0);
|
|
|
|
|
2021-03-29 07:20:51 +03:00
|
|
|
for (Symbol *sym : std::span(ctx.dynsym->symbols).subspan(1))
|
|
|
|
ctx.versym->contents[sym->dynsym_idx] = sym->ver_idx;
|
2021-03-07 07:43:58 +03:00
|
|
|
}
|
|
|
|
|
2021-03-29 10:24:40 +03:00
|
|
|
static void fill_verneed(Context &ctx) {
|
2021-03-06 14:49:40 +03:00
|
|
|
Timer t("fill_verneed");
|
2020-11-29 05:40:57 +03:00
|
|
|
|
2021-03-29 07:20:51 +03:00
|
|
|
if (ctx.dynsym->symbols.empty())
|
2021-03-23 07:54:40 +03:00
|
|
|
return;
|
|
|
|
|
2020-11-29 05:57:58 +03:00
|
|
|
// Create a list of versioned symbols and sort by file and version.
|
2021-03-29 07:20:51 +03:00
|
|
|
std::vector<Symbol *> syms(ctx.dynsym->symbols.begin() + 1,
|
|
|
|
ctx.dynsym->symbols.end());
|
2021-03-06 05:51:03 +03:00
|
|
|
|
|
|
|
erase(syms, [](Symbol *sym) {
|
2021-03-06 14:49:40 +03:00
|
|
|
return !sym->file->is_dso || sym->ver_idx <= VER_NDX_LAST_RESERVED;
|
2021-03-06 05:51:03 +03:00
|
|
|
});
|
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) {
|
2021-01-20 07:09:54 +03:00
|
|
|
return std::tuple(((SharedFile *)a->file)->soname, a->ver_idx) <
|
|
|
|
std::tuple(((SharedFile *)b->file)->soname, b->ver_idx);
|
2020-11-29 05:40:57 +03:00
|
|
|
});
|
|
|
|
|
2021-03-06 14:49:40 +03:00
|
|
|
// Resize of .gnu.version
|
2021-03-29 07:20:51 +03:00
|
|
|
ctx.versym->contents.resize(ctx.dynsym->symbols.size(), 1);
|
|
|
|
ctx.versym->contents[0] = 0;
|
2020-11-29 05:57:58 +03:00
|
|
|
|
2021-03-07 07:43:58 +03:00
|
|
|
// Allocate a large enough buffer for .gnu.version_r.
|
2021-03-29 07:20:51 +03:00
|
|
|
ctx.verneed->contents.resize((sizeof(ElfVerneed) + sizeof(ElfVernaux)) *
|
2021-03-06 14:49:40 +03:00
|
|
|
syms.size());
|
2020-11-29 05:57:58 +03:00
|
|
|
|
2021-03-19 07:26:27 +03:00
|
|
|
// Fill .gnu.version_r.
|
2021-03-29 07:20:51 +03:00
|
|
|
u8 *buf = (u8 *)&ctx.verneed->contents[0];
|
2021-03-06 14:49:40 +03:00
|
|
|
u8 *ptr = buf;
|
2020-12-10 15:18:20 +03:00
|
|
|
ElfVerneed *verneed = nullptr;
|
|
|
|
ElfVernaux *aux = nullptr;
|
2020-11-29 06:59:08 +03:00
|
|
|
|
2021-03-29 07:20:51 +03:00
|
|
|
u16 veridx = VER_NDX_LAST_RESERVED + ctx.arg.version_definitions.size();
|
2021-03-07 07:43:58 +03:00
|
|
|
|
2021-03-06 14:49:40 +03:00
|
|
|
auto start_group = [&](InputFile *file) {
|
2021-03-29 07:20:51 +03:00
|
|
|
ctx.verneed->shdr.sh_info++;
|
2021-03-06 14:49:40 +03:00
|
|
|
if (verneed)
|
|
|
|
verneed->vn_next = ptr - (u8 *)verneed;
|
2020-11-29 06:59:08 +03:00
|
|
|
|
2021-03-06 14:49:40 +03:00
|
|
|
verneed = (ElfVerneed *)ptr;
|
|
|
|
ptr += sizeof(*verneed);
|
|
|
|
verneed->vn_version = 1;
|
2021-03-29 07:20:51 +03:00
|
|
|
verneed->vn_file = ctx.dynstr->find_string(((SharedFile *)file)->soname);
|
2021-03-06 14:49:40 +03:00
|
|
|
verneed->vn_aux = sizeof(ElfVerneed);
|
|
|
|
aux = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto add_entry = [&](Symbol *sym) {
|
2021-01-09 16:27:53 +03:00
|
|
|
verneed->vn_cnt++;
|
2021-03-06 14:49:40 +03:00
|
|
|
|
2020-11-29 08:05:44 +03:00
|
|
|
if (aux)
|
2020-12-10 15:18:20 +03:00
|
|
|
aux->vna_next = sizeof(ElfVernaux);
|
2021-03-06 14:49:40 +03:00
|
|
|
aux = (ElfVernaux *)ptr;
|
|
|
|
ptr += sizeof(*aux);
|
2020-11-29 08:05:44 +03:00
|
|
|
|
2021-03-06 14:49:40 +03:00
|
|
|
std::string_view verstr = sym->get_version();
|
2020-11-29 06:59:08 +03:00
|
|
|
aux->vna_hash = elf_hash(verstr);
|
2021-03-06 14:49:40 +03:00
|
|
|
aux->vna_other = ++veridx;
|
2021-03-29 07:20:51 +03:00
|
|
|
aux->vna_name = ctx.dynstr->add_string(verstr);
|
2020-11-29 06:59:08 +03:00
|
|
|
};
|
|
|
|
|
2021-03-06 14:49:40 +03:00
|
|
|
for (i64 i = 0; i < syms.size(); i++) {
|
|
|
|
if (i == 0 || syms[i - 1]->file != syms[i]->file) {
|
|
|
|
start_group(syms[i]->file);
|
|
|
|
add_entry(syms[i]);
|
|
|
|
} else if (syms[i - 1]->ver_idx != syms[i]->ver_idx) {
|
|
|
|
add_entry(syms[i]);
|
|
|
|
}
|
2020-11-29 07:14:56 +03:00
|
|
|
|
2021-03-29 07:20:51 +03:00
|
|
|
ctx.versym->contents[syms[i]->dynsym_idx] = veridx;
|
2020-11-29 06:59:08 +03:00
|
|
|
}
|
2021-03-06 14:49:40 +03:00
|
|
|
|
|
|
|
// Resize .gnu.version_r to fit to its contents.
|
2021-03-29 07:20:51 +03:00
|
|
|
ctx.verneed->contents.resize(ptr - buf);
|
2020-11-29 05:40:57 +03:00
|
|
|
}
|
|
|
|
|
2021-03-29 10:24:40 +03:00
|
|
|
static void clear_padding(Context &ctx, i64 filesize) {
|
2021-01-09 11:45:50 +03:00
|
|
|
Timer t("clear_padding");
|
2020-11-11 04:42:26 +03:00
|
|
|
|
2021-03-29 10:24:40 +03:00
|
|
|
auto zero = [&](OutputChunk *chunk, i64 next_start) {
|
2021-01-24 06:01:43 +03:00
|
|
|
i64 pos = chunk->shdr.sh_offset;
|
2020-11-10 11:32:41 +03:00
|
|
|
if (chunk->shdr.sh_type != SHT_NOBITS)
|
|
|
|
pos += chunk->shdr.sh_size;
|
2021-03-29 07:20:51 +03:00
|
|
|
memset(ctx.buf + pos, 0, next_start - pos);
|
2020-11-09 15:50:47 +03:00
|
|
|
};
|
|
|
|
|
2021-03-29 07:20:51 +03:00
|
|
|
for (i64 i = 1; i < ctx.chunks.size(); i++)
|
|
|
|
zero(ctx.chunks[i - 1], ctx.chunks[i]->shdr.sh_offset);
|
|
|
|
zero(ctx.chunks.back(), filesize);
|
2020-11-09 15:50:47 +03:00
|
|
|
}
|
|
|
|
|
2021-03-13 08:34:52 +03:00
|
|
|
// We want to sort output chunks in the following order.
|
2020-10-22 12:54:51 +03:00
|
|
|
//
|
2021-03-13 08:34:52 +03:00
|
|
|
// ELF header
|
|
|
|
// program header
|
|
|
|
// .interp
|
|
|
|
// note
|
|
|
|
// alloc readonly data
|
|
|
|
// alloc readonly code
|
|
|
|
// alloc writable tdata
|
|
|
|
// alloc writable tbss
|
|
|
|
// alloc writable RELRO data
|
|
|
|
// alloc writable RELRO bss
|
|
|
|
// alloc writable non-RELRO data
|
|
|
|
// alloc writable non-RELRO bss
|
|
|
|
// nonalloc
|
|
|
|
// section header
|
2021-03-29 10:24:40 +03:00
|
|
|
static i64 get_section_rank(Context &ctx, OutputChunk *chunk) {
|
2021-03-29 07:20:51 +03:00
|
|
|
if (chunk == ctx.ehdr)
|
2021-03-13 08:34:52 +03:00
|
|
|
return 0;
|
2021-03-29 07:20:51 +03:00
|
|
|
if (chunk == ctx.phdr)
|
2021-03-13 08:34:52 +03:00
|
|
|
return 1;
|
2021-03-29 07:20:51 +03:00
|
|
|
if (chunk == ctx.interp)
|
2021-03-13 08:34:52 +03:00
|
|
|
return 2;
|
2021-03-29 07:20:51 +03:00
|
|
|
if (chunk == ctx.shdr)
|
2021-03-13 08:34:52 +03:00
|
|
|
return 1 << 20;
|
|
|
|
|
|
|
|
u64 type = chunk->shdr.sh_type;
|
|
|
|
u64 flags = chunk->shdr.sh_flags;
|
|
|
|
|
|
|
|
if (type == SHT_NOTE)
|
|
|
|
return 3;
|
|
|
|
if (!(flags & SHF_ALLOC))
|
|
|
|
return (1 << 20) - 1;
|
|
|
|
|
|
|
|
bool reaodnly = !(flags & SHF_WRITE);
|
|
|
|
bool exec = (flags & SHF_EXECINSTR);
|
|
|
|
bool tls = (flags & SHF_TLS);
|
2021-03-29 09:53:00 +03:00
|
|
|
bool relro = is_relro(ctx, chunk);
|
2021-03-13 08:34:52 +03:00
|
|
|
bool hasbits = !(type == SHT_NOBITS);
|
2021-03-12 13:57:34 +03:00
|
|
|
|
2021-03-13 08:34:52 +03:00
|
|
|
return ((!reaodnly << 9) | (exec << 8) | (!tls << 7) |
|
|
|
|
(!relro << 6) | (!hasbits << 5)) + 4;
|
2020-10-22 12:54:51 +03:00
|
|
|
}
|
|
|
|
|
2021-03-20 06:15:07 +03:00
|
|
|
// Returns the smallest number n such that
|
|
|
|
// n >= val and n % align == skew.
|
2021-03-19 06:40:22 +03:00
|
|
|
inline u64 align_with_skew(u64 val, u64 align, u64 skew) {
|
|
|
|
return align_to(val + align - skew, align) - align + skew;
|
|
|
|
}
|
|
|
|
|
2021-03-29 10:24:40 +03:00
|
|
|
static i64 set_osec_offsets(Context &ctx) {
|
2021-01-09 11:45:50 +03:00
|
|
|
Timer t("osec_offset");
|
2020-11-11 04:42:26 +03:00
|
|
|
|
2021-01-24 06:01:43 +03:00
|
|
|
i64 fileoff = 0;
|
2021-03-29 07:20:51 +03:00
|
|
|
i64 vaddr = ctx.arg.image_base;
|
2020-10-26 08:16:13 +03:00
|
|
|
|
2021-03-29 10:24:40 +03:00
|
|
|
for (OutputChunk *chunk : ctx.chunks) {
|
2021-03-17 08:24:28 +03:00
|
|
|
if (chunk->new_page)
|
2020-10-26 08:16:13 +03:00
|
|
|
vaddr = align_to(vaddr, PAGE_SIZE);
|
|
|
|
|
|
|
|
vaddr = align_to(vaddr, chunk->shdr.sh_addralign);
|
2021-03-19 06:40:22 +03:00
|
|
|
fileoff = align_with_skew(fileoff, PAGE_SIZE, vaddr % PAGE_SIZE);
|
2020-10-26 08:16:13 +03:00
|
|
|
|
|
|
|
chunk->shdr.sh_offset = fileoff;
|
|
|
|
if (chunk->shdr.sh_flags & SHF_ALLOC)
|
|
|
|
chunk->shdr.sh_addr = vaddr;
|
|
|
|
|
2021-03-19 06:40:22 +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;
|
2021-03-17 08:24:28 +03:00
|
|
|
|
|
|
|
if (chunk->new_page_end)
|
|
|
|
vaddr = align_to(vaddr, PAGE_SIZE);
|
2020-10-26 08:16:13 +03:00
|
|
|
}
|
|
|
|
return fileoff;
|
|
|
|
}
|
|
|
|
|
2021-03-29 10:24:40 +03:00
|
|
|
static void fix_synthetic_symbols(Context &ctx) {
|
2021-01-13 08:43:34 +03:00
|
|
|
auto start = [](Symbol *sym, OutputChunk *chunk) {
|
2021-02-11 16:50:49 +03:00
|
|
|
if (sym && chunk) {
|
2020-11-11 04:45:52 +03:00
|
|
|
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) {
|
2021-02-11 16:50:49 +03:00
|
|
|
if (sym && chunk) {
|
2020-11-11 04:45:52 +03:00
|
|
|
sym->shndx = chunk->shndx;
|
|
|
|
sym->value = chunk->shdr.sh_addr + chunk->shdr.sh_size;
|
|
|
|
}
|
|
|
|
};
|
2020-11-04 08:23:39 +03:00
|
|
|
|
|
|
|
// __bss_start
|
2021-03-29 10:24:40 +03:00
|
|
|
for (OutputChunk *chunk : ctx.chunks) {
|
2020-11-08 10:09:01 +03:00
|
|
|
if (chunk->kind == OutputChunk::REGULAR && chunk->name == ".bss") {
|
2021-03-29 07:20:51 +03:00
|
|
|
start(ctx.__bss_start, chunk);
|
2020-11-04 08:23:39 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-08 14:35:25 +03:00
|
|
|
// __ehdr_start and __executable_start
|
2021-03-29 10:24:40 +03:00
|
|
|
for (OutputChunk *chunk : ctx.chunks) {
|
2020-11-04 08:23:39 +03:00
|
|
|
if (chunk->shndx == 1) {
|
2021-03-29 07:20:51 +03:00
|
|
|
ctx.__ehdr_start->shndx = 1;
|
|
|
|
ctx.__ehdr_start->value = ctx.ehdr->shdr.sh_addr;
|
2021-03-08 14:35:25 +03:00
|
|
|
|
2021-03-29 07:20:51 +03:00
|
|
|
ctx.__executable_start->shndx = 1;
|
|
|
|
ctx.__executable_start->value = ctx.ehdr->shdr.sh_addr;
|
2020-11-04 08:23:39 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// __rela_iplt_start and __rela_iplt_end
|
2021-03-29 07:20:51 +03:00
|
|
|
start(ctx.__rela_iplt_start, ctx.relplt);
|
|
|
|
stop(ctx.__rela_iplt_end, ctx.relplt);
|
2020-11-04 08:23:39 +03:00
|
|
|
|
|
|
|
// __{init,fini}_array_{start,end}
|
2021-03-29 10:24:40 +03:00
|
|
|
for (OutputChunk *chunk : ctx.chunks) {
|
2020-11-04 08:23:39 +03:00
|
|
|
switch (chunk->shdr.sh_type) {
|
|
|
|
case SHT_INIT_ARRAY:
|
2021-03-29 07:20:51 +03:00
|
|
|
start(ctx.__init_array_start, chunk);
|
|
|
|
stop(ctx.__init_array_end, chunk);
|
2020-11-04 08:23:39 +03:00
|
|
|
break;
|
|
|
|
case SHT_FINI_ARRAY:
|
2021-03-29 07:20:51 +03:00
|
|
|
start(ctx.__fini_array_start, chunk);
|
|
|
|
stop(ctx.__fini_array_end, chunk);
|
2020-11-04 08:23:39 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-08 14:35:25 +03:00
|
|
|
// _end, _etext, _edata and the like
|
2021-03-29 10:24:40 +03:00
|
|
|
for (OutputChunk *chunk : ctx.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-03-29 07:20:51 +03:00
|
|
|
stop(ctx._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-03-29 07:20:51 +03:00
|
|
|
stop(ctx._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-03-29 07:20:51 +03:00
|
|
|
stop(ctx._edata, chunk);
|
2020-11-04 08:23:39 +03:00
|
|
|
}
|
|
|
|
|
2020-11-13 04:19:47 +03:00
|
|
|
// _DYNAMIC
|
2021-03-29 07:20:51 +03:00
|
|
|
start(ctx._DYNAMIC, ctx.dynamic);
|
2020-11-13 04:19:47 +03:00
|
|
|
|
2020-11-16 07:48:14 +03:00
|
|
|
// _GLOBAL_OFFSET_TABLE_
|
2021-03-29 07:20:51 +03:00
|
|
|
start(ctx._GLOBAL_OFFSET_TABLE_, ctx.gotplt);
|
2020-11-16 07:48:14 +03:00
|
|
|
|
2021-02-11 16:17:22 +03:00
|
|
|
// __GNU_EH_FRAME_HDR
|
2021-03-29 07:20:51 +03:00
|
|
|
start(ctx.__GNU_EH_FRAME_HDR, ctx.eh_frame_hdr);
|
2021-02-11 16:17:22 +03:00
|
|
|
|
2020-11-04 08:23:39 +03:00
|
|
|
// __start_ and __stop_ symbols
|
2021-03-29 10:24:40 +03:00
|
|
|
for (OutputChunk *chunk : ctx.chunks) {
|
2020-11-11 08:45:17 +03:00
|
|
|
if (is_c_identifier(chunk->name)) {
|
2021-02-26 19:08:38 +03:00
|
|
|
start(Symbol::intern_alloc("__start_" + std::string(chunk->name)), chunk);
|
|
|
|
stop(Symbol::intern_alloc("__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-03-29 08:36:55 +03:00
|
|
|
MemoryMappedFile *find_library(Context &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 *mb = MemoryMappedFile::open(path))
|
|
|
|
return mb;
|
|
|
|
}
|
|
|
|
Fatal() << "library not found: " << name;
|
|
|
|
}
|
|
|
|
|
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)
|
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-13 17:52:11 +03:00
|
|
|
Fatal() << "library not found: " << name;
|
2020-11-19 12:03:26 +03:00
|
|
|
}
|
|
|
|
|
2021-03-29 08:36:55 +03:00
|
|
|
static void read_input_files(Context &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())
|
|
|
|
Fatal() << "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();
|
2020-12-22 14:12:41 +03:00
|
|
|
} else if (read_arg(args, arg, "l")) {
|
2021-03-29 08:36:55 +03:00
|
|
|
MemoryMappedFile *mb = find_library(ctx, std::string(arg));
|
|
|
|
read_file(ctx, mb);
|
2020-12-22 14:12:41 +03:00
|
|
|
} else {
|
2021-03-29 08:36:55 +03:00
|
|
|
read_file(ctx, MemoryMappedFile::must_open(std::string(args[0])));
|
2020-12-22 14:12:41 +03:00
|
|
|
args = args.subspan(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-29 10:24:40 +03:00
|
|
|
static void show_stats(Context &ctx) {
|
2021-03-29 07:20:51 +03:00
|
|
|
for (ObjectFile *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;
|
2020-12-21 11:51:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Counter num_input_sections("input_sections");
|
2021-03-29 07:20:51 +03:00
|
|
|
for (ObjectFile *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();
|
|
|
|
}
|
|
|
|
|
2020-12-21 10:32:43 +03:00
|
|
|
int main(int argc, char **argv) {
|
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-26 16:12:07 +03:00
|
|
|
process_run_subcommand(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 08:05:55 +03:00
|
|
|
ctx.cmdline_args = expand_response_files(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-01-24 06:01:43 +03:00
|
|
|
if (i64 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-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-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;
|
|
|
|
|
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;
|
|
|
|
daemonize(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)
|
2020-12-21 10:32:43 +03:00
|
|
|
Symbol::intern(arg)->traced = true;
|
|
|
|
|
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
|
|
|
|
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;
|
2021-03-29 07:20:51 +03:00
|
|
|
for (SharedFile *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-01-24 10:12:34 +03:00
|
|
|
gc_sections();
|
|
|
|
|
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-01-27 16:11:47 +03:00
|
|
|
icf_sections();
|
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 07:20:51 +03:00
|
|
|
append(ctx.chunks, collect_output_sections());
|
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 09:16:53 +03:00
|
|
|
ctx.internal_obj = new ObjectFile(ctx);
|
|
|
|
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 07:20:51 +03:00
|
|
|
tbb::parallel_for_each(ctx.objs, [](ObjectFile *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 10:24:40 +03:00
|
|
|
sort(ctx.chunks, [&](OutputChunk *a, OutputChunk *b) {
|
|
|
|
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 07:20:51 +03:00
|
|
|
for (SharedFile *file : ctx.dsos)
|
|
|
|
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 09:16:53 +03:00
|
|
|
tbb::parallel_for_each(ctx.objs, [&](ObjectFile *file) {
|
|
|
|
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 07:20:51 +03:00
|
|
|
erase(ctx.chunks, [](OutputChunk *chunk) {
|
2021-03-10 20:48:36 +03:00
|
|
|
return chunk->kind == OutputChunk::REGULAR &&
|
|
|
|
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 07:20:51 +03:00
|
|
|
for (OutputChunk *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 07:20:51 +03:00
|
|
|
erase(ctx.chunks, [](OutputChunk *chunk) {
|
2021-03-14 10:36:56 +03:00
|
|
|
return chunk->kind == OutputChunk::SYNTHETIC &&
|
|
|
|
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++)
|
|
|
|
if (ctx.chunks[i]->kind != OutputChunk::HEADER)
|
|
|
|
ctx.chunks[i]->shndx = shndx++;
|
2020-12-07 10:12:19 +03:00
|
|
|
|
2021-03-29 07:20:51 +03:00
|
|
|
for (OutputChunk *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 09:53:00 +03:00
|
|
|
for (ElfPhdr 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 07:20:51 +03:00
|
|
|
OutputFile *file = OutputFile::open(ctx.arg.output, filesize);
|
|
|
|
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-03-29 07:20:51 +03:00
|
|
|
tbb::parallel_for_each(ctx.chunks, [&](OutputChunk *chunk) {
|
2021-03-29 09:53:00 +03:00
|
|
|
chunk->copy_buf(ctx);
|
2020-11-09 08:47:51 +03:00
|
|
|
});
|
2021-01-13 17:52:11 +03:00
|
|
|
Error::checkpoint();
|
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-03 11:19:24 +03:00
|
|
|
file->close();
|
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)
|
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
|
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
|
|
|
}
|