2021-03-30 10:55:06 +03:00
|
|
|
#include "mold.h"
|
2021-10-05 05:35:20 +03:00
|
|
|
#include "../output-file.h"
|
2021-03-30 10:55:06 +03:00
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <map>
|
2021-05-30 19:10:00 +03:00
|
|
|
#include <regex>
|
2021-03-30 10:55:06 +03:00
|
|
|
#include <tbb/parallel_for_each.h>
|
2021-04-17 17:45:40 +03:00
|
|
|
#include <tbb/parallel_scan.h>
|
|
|
|
#include <tbb/partitioner.h>
|
2021-03-30 10:55:06 +03:00
|
|
|
#include <unordered_set>
|
|
|
|
|
2021-09-02 12:19:43 +03:00
|
|
|
namespace mold::elf {
|
|
|
|
|
2021-03-30 10:55:06 +03:00
|
|
|
template <typename E>
|
|
|
|
void apply_exclude_libs(Context<E> &ctx) {
|
2021-04-07 09:38:31 +03:00
|
|
|
Timer t(ctx, "apply_exclude_libs");
|
2021-03-30 10:55:06 +03:00
|
|
|
|
|
|
|
if (ctx.arg.exclude_libs.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::unordered_set<std::string_view> set(ctx.arg.exclude_libs.begin(),
|
|
|
|
ctx.arg.exclude_libs.end());
|
|
|
|
|
|
|
|
for (ObjectFile<E> *file : ctx.objs)
|
|
|
|
if (!file->archive_name.empty())
|
2021-04-11 07:16:11 +03:00
|
|
|
if (set.contains("ALL") ||
|
|
|
|
set.contains(path_filename(file->archive_name)))
|
2021-03-30 10:55:06 +03:00
|
|
|
file->exclude_libs = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename E>
|
|
|
|
void create_synthetic_sections(Context<E> &ctx) {
|
2021-04-06 06:25:15 +03:00
|
|
|
auto add = [&](auto &chunk) {
|
|
|
|
ctx.chunks.push_back(chunk.get());
|
2021-03-30 10:55:06 +03:00
|
|
|
};
|
|
|
|
|
2021-04-06 06:25:15 +03:00
|
|
|
add(ctx.ehdr = std::make_unique<OutputEhdr<E>>());
|
|
|
|
add(ctx.phdr = std::make_unique<OutputPhdr<E>>());
|
|
|
|
add(ctx.shdr = std::make_unique<OutputShdr<E>>());
|
|
|
|
add(ctx.got = std::make_unique<GotSection<E>>());
|
|
|
|
add(ctx.gotplt = std::make_unique<GotPltSection<E>>());
|
2021-10-12 10:56:00 +03:00
|
|
|
add(ctx.reldyn = std::make_unique<RelDynSection<E>>());
|
2021-04-06 06:25:15 +03:00
|
|
|
add(ctx.relplt = std::make_unique<RelPltSection<E>>());
|
|
|
|
add(ctx.strtab = std::make_unique<StrtabSection<E>>());
|
|
|
|
add(ctx.shstrtab = std::make_unique<ShstrtabSection<E>>());
|
|
|
|
add(ctx.plt = std::make_unique<PltSection<E>>());
|
|
|
|
add(ctx.pltgot = std::make_unique<PltGotSection<E>>());
|
|
|
|
add(ctx.symtab = std::make_unique<SymtabSection<E>>());
|
|
|
|
add(ctx.dynsym = std::make_unique<DynsymSection<E>>());
|
|
|
|
add(ctx.dynstr = std::make_unique<DynstrSection<E>>());
|
|
|
|
add(ctx.eh_frame = std::make_unique<EhFrameSection<E>>());
|
|
|
|
add(ctx.dynbss = std::make_unique<DynbssSection<E>>(false));
|
|
|
|
add(ctx.dynbss_relro = std::make_unique<DynbssSection<E>>(true));
|
2021-03-30 10:55:06 +03:00
|
|
|
|
|
|
|
if (!ctx.arg.dynamic_linker.empty())
|
2021-04-06 06:25:15 +03:00
|
|
|
add(ctx.interp = std::make_unique<InterpSection<E>>());
|
2021-03-30 10:55:06 +03:00
|
|
|
if (ctx.arg.build_id.kind != BuildId::NONE)
|
2021-04-06 06:25:15 +03:00
|
|
|
add(ctx.buildid = std::make_unique<BuildIdSection<E>>());
|
2021-03-30 10:55:06 +03:00
|
|
|
if (ctx.arg.eh_frame_hdr)
|
2021-04-06 06:25:15 +03:00
|
|
|
add(ctx.eh_frame_hdr = std::make_unique<EhFrameHdrSection<E>>());
|
2021-03-30 10:55:06 +03:00
|
|
|
if (ctx.arg.hash_style_sysv)
|
2021-04-06 06:25:15 +03:00
|
|
|
add(ctx.hash = std::make_unique<HashSection<E>>());
|
2021-03-30 10:55:06 +03:00
|
|
|
if (ctx.arg.hash_style_gnu)
|
2021-04-06 06:25:15 +03:00
|
|
|
add(ctx.gnu_hash = std::make_unique<GnuHashSection<E>>());
|
2021-03-30 10:55:06 +03:00
|
|
|
if (!ctx.arg.version_definitions.empty())
|
2021-04-06 06:25:15 +03:00
|
|
|
add(ctx.verdef = std::make_unique<VerdefSection<E>>());
|
2021-03-30 10:55:06 +03:00
|
|
|
|
2021-04-06 06:25:15 +03:00
|
|
|
add(ctx.dynamic = std::make_unique<DynamicSection<E>>());
|
|
|
|
add(ctx.versym = std::make_unique<VersymSection<E>>());
|
|
|
|
add(ctx.verneed = std::make_unique<VerneedSection<E>>());
|
2021-04-30 18:27:38 +03:00
|
|
|
add(ctx.note_property = std::make_unique<NotePropertySection<E>>());
|
2021-05-02 13:21:24 +03:00
|
|
|
|
|
|
|
if (ctx.arg.repro)
|
|
|
|
add(ctx.repro = std::make_unique<ReproSection<E>>());
|
2021-03-30 10:55:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename E>
|
2021-06-19 14:29:16 +03:00
|
|
|
void resolve_symbols(Context<E> &ctx) {
|
2021-04-07 09:38:31 +03:00
|
|
|
Timer t(ctx, "resolve_obj_symbols");
|
2021-03-30 10:55:06 +03:00
|
|
|
|
2021-06-23 14:42:47 +03:00
|
|
|
// Register object symbols
|
2021-03-30 10:55:06 +03:00
|
|
|
tbb::parallel_for_each(ctx.objs, [&](ObjectFile<E> *file) {
|
|
|
|
if (file->is_in_lib)
|
|
|
|
file->resolve_lazy_symbols(ctx);
|
2021-06-23 14:42:47 +03:00
|
|
|
else
|
|
|
|
file->resolve_regular_symbols(ctx);
|
2021-03-30 10:55:06 +03:00
|
|
|
});
|
|
|
|
|
2021-05-31 07:26:55 +03:00
|
|
|
// Register DSO symbols
|
|
|
|
tbb::parallel_for_each(ctx.dsos, [&](SharedFile<E> *file) {
|
|
|
|
file->resolve_dso_symbols(ctx);
|
|
|
|
});
|
|
|
|
|
2021-03-30 10:55:06 +03:00
|
|
|
// Mark reachable objects to decide which files to include
|
|
|
|
// into an output.
|
2021-06-25 16:16:56 +03:00
|
|
|
std::vector<ObjectFile<E> *> live_objs = ctx.objs;
|
|
|
|
erase(live_objs, [](InputFile<E> *file) { return !file->is_alive; });
|
2021-03-30 10:55:06 +03:00
|
|
|
|
2021-09-12 12:34:41 +03:00
|
|
|
auto load = [&](std::string_view name) {
|
2021-10-05 02:31:26 +03:00
|
|
|
if (InputFile<E> *file = intern(ctx, name)->file)
|
2021-06-21 07:17:52 +03:00
|
|
|
if (!file->is_alive.exchange(true) && !file->is_dso)
|
2021-06-25 16:16:56 +03:00
|
|
|
live_objs.push_back((ObjectFile<E> *)file);
|
2021-09-12 12:34:41 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
for (std::string_view name : ctx.arg.undefined)
|
|
|
|
load(name);
|
|
|
|
for (std::string_view name : ctx.arg.require_defined)
|
|
|
|
load(name);
|
2021-03-30 10:55:06 +03:00
|
|
|
|
2021-07-02 17:11:48 +03:00
|
|
|
tbb::parallel_for_each(live_objs,
|
2021-06-21 07:17:52 +03:00
|
|
|
[&](ObjectFile<E> *file,
|
2021-07-02 17:11:48 +03:00
|
|
|
tbb::feeder<ObjectFile<E> *> &feeder) {
|
2021-06-25 16:16:56 +03:00
|
|
|
file->mark_live_objects(ctx, [&](ObjectFile<E> *obj) { feeder.add(obj); });
|
2021-05-31 06:48:26 +03:00
|
|
|
});
|
2021-03-30 10:55:06 +03:00
|
|
|
|
|
|
|
// Remove symbols of eliminated objects.
|
|
|
|
tbb::parallel_for_each(ctx.objs, [](ObjectFile<E> *file) {
|
|
|
|
if (!file->is_alive)
|
|
|
|
for (Symbol<E> *sym : file->get_global_syms())
|
|
|
|
if (sym->file == file)
|
2021-04-12 14:14:37 +03:00
|
|
|
new (sym) Symbol<E>(sym->name());
|
2021-03-30 10:55:06 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
// Eliminate unused archive members.
|
|
|
|
erase(ctx.objs, [](InputFile<E> *file) { return !file->is_alive; });
|
|
|
|
|
|
|
|
// Mark live DSOs
|
|
|
|
tbb::parallel_for_each(ctx.objs, [](ObjectFile<E> *file) {
|
|
|
|
for (i64 i = file->first_global; i < file->elf_syms.size(); i++) {
|
|
|
|
const ElfSym<E> &esym = file->elf_syms[i];
|
|
|
|
Symbol<E> &sym = *file->symbols[i];
|
2021-06-21 07:17:52 +03:00
|
|
|
if (esym.is_undef_strong() && sym.file && sym.file->is_dso) {
|
2021-03-30 10:55:06 +03:00
|
|
|
std::lock_guard lock(sym.mu);
|
2021-06-21 07:17:52 +03:00
|
|
|
sym.file->is_alive = true;
|
2021-03-30 10:55:06 +03:00
|
|
|
sym.is_weak = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-06-21 07:17:52 +03:00
|
|
|
// DSOs referenced by live DSOs are also alive.
|
2021-06-25 16:16:56 +03:00
|
|
|
std::vector<SharedFile<E> *> live_dsos = ctx.dsos;
|
|
|
|
erase(live_dsos, [](SharedFile<E> *file) { return !file->is_alive; });
|
2021-06-21 07:17:52 +03:00
|
|
|
|
2021-07-02 17:11:48 +03:00
|
|
|
tbb::parallel_for_each(live_dsos,
|
|
|
|
[&](SharedFile<E> *file,
|
|
|
|
tbb::feeder<SharedFile<E> *> &feeder) {
|
2021-06-21 07:17:52 +03:00
|
|
|
for (Symbol<E> *sym : file->globals)
|
|
|
|
if (sym->file && sym->file != file && sym->file->is_dso &&
|
|
|
|
!sym->file->is_alive.exchange(true))
|
|
|
|
feeder.add(file);
|
|
|
|
});
|
|
|
|
|
2021-03-30 10:55:06 +03:00
|
|
|
// Remove symbols of unreferenced DSOs.
|
|
|
|
tbb::parallel_for_each(ctx.dsos, [](SharedFile<E> *file) {
|
|
|
|
if (!file->is_alive)
|
|
|
|
for (Symbol<E> *sym : file->symbols)
|
|
|
|
if (sym->file == file)
|
2021-04-12 14:14:37 +03:00
|
|
|
new (sym) Symbol<E>(sym->name());
|
2021-03-30 10:55:06 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
// Remove unreferenced DSOs
|
|
|
|
erase(ctx.dsos, [](InputFile<E> *file) { return !file->is_alive; });
|
2021-06-25 09:22:52 +03:00
|
|
|
|
|
|
|
// Register common symbols
|
|
|
|
tbb::parallel_for_each(ctx.objs, [&](ObjectFile<E> *file) {
|
|
|
|
file->resolve_common_symbols(ctx);
|
|
|
|
});
|
2021-06-26 08:57:43 +03:00
|
|
|
|
2021-10-05 02:31:26 +03:00
|
|
|
if (Symbol<E> *sym = intern(ctx, "__gnu_lto_slim"); sym->file)
|
2021-06-26 08:57:43 +03:00
|
|
|
Fatal(ctx) << *sym->file << ": looks like this file contains a GCC "
|
|
|
|
<< "intermediate code, but mold does not support LTO";
|
2021-03-30 10:55:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename E>
|
|
|
|
void eliminate_comdats(Context<E> &ctx) {
|
2021-04-07 09:38:31 +03:00
|
|
|
Timer t(ctx, "eliminate_comdats");
|
2021-03-30 10:55:06 +03:00
|
|
|
|
|
|
|
tbb::parallel_for_each(ctx.objs, [](ObjectFile<E> *file) {
|
|
|
|
file->resolve_comdat_groups();
|
|
|
|
});
|
|
|
|
|
|
|
|
tbb::parallel_for_each(ctx.objs, [](ObjectFile<E> *file) {
|
|
|
|
file->eliminate_duplicate_comdat_groups();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename E>
|
|
|
|
void convert_common_symbols(Context<E> &ctx) {
|
2021-04-07 09:38:31 +03:00
|
|
|
Timer t(ctx, "convert_common_symbols");
|
2021-03-30 10:55:06 +03:00
|
|
|
|
|
|
|
tbb::parallel_for_each(ctx.objs, [&](ObjectFile<E> *file) {
|
|
|
|
file->convert_common_symbols(ctx);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename E>
|
|
|
|
static std::string get_cmdline_args(Context<E> &ctx) {
|
|
|
|
std::stringstream ss;
|
2021-05-08 11:50:09 +03:00
|
|
|
ss << ctx.cmdline_args[1];
|
|
|
|
for (i64 i = 2; i < ctx.cmdline_args.size(); i++)
|
2021-04-17 15:14:41 +03:00
|
|
|
ss << " " << ctx.cmdline_args[i];
|
2021-03-30 10:55:06 +03:00
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename E>
|
|
|
|
void add_comment_string(Context<E> &ctx, std::string str) {
|
2021-04-07 09:38:31 +03:00
|
|
|
std::string_view buf = save_string(ctx, str);
|
2021-03-30 10:55:06 +03:00
|
|
|
MergedSection<E> *sec =
|
2021-04-07 09:38:31 +03:00
|
|
|
MergedSection<E>::get_instance(ctx, ".comment", SHT_PROGBITS, 0);
|
2021-07-12 05:00:10 +03:00
|
|
|
std::string_view data(buf.data(), buf.size() + 1);
|
2021-09-29 08:33:12 +03:00
|
|
|
Subsection<E> *subsec = sec->insert(data, hash_string(data), 1);
|
|
|
|
subsec->is_alive = true;
|
2021-03-30 10:55:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename E>
|
|
|
|
void compute_merged_section_sizes(Context<E> &ctx) {
|
2021-04-07 09:38:31 +03:00
|
|
|
Timer t(ctx, "compute_merged_section_sizes");
|
2021-03-30 10:55:06 +03:00
|
|
|
|
2021-09-29 08:33:12 +03:00
|
|
|
// Mark section subsections referenced by live objects.
|
2021-03-30 10:55:06 +03:00
|
|
|
if (!ctx.arg.gc_sections) {
|
|
|
|
tbb::parallel_for_each(ctx.objs, [](ObjectFile<E> *file) {
|
2021-09-29 08:33:12 +03:00
|
|
|
for (Subsection<E> *subsec : file->subsections)
|
|
|
|
subsec->is_alive.store(true, std::memory_order_relaxed);
|
2021-03-30 10:55:06 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add an identification string to .comment.
|
2021-09-28 10:40:05 +03:00
|
|
|
add_comment_string(ctx, mold_version);
|
2021-03-30 10:55:06 +03:00
|
|
|
|
2021-06-25 16:16:56 +03:00
|
|
|
// Embed command line arguments for debugging.
|
2021-06-14 07:49:58 +03:00
|
|
|
if (char *env = getenv("MOLD_DEBUG"); env && env[0])
|
|
|
|
add_comment_string(ctx, "mold command line: " + get_cmdline_args(ctx));
|
2021-03-30 10:55:06 +03:00
|
|
|
|
2021-07-12 05:00:10 +03:00
|
|
|
Timer t2(ctx, "MergedSection assign_offsets");
|
2021-04-07 09:38:31 +03:00
|
|
|
tbb::parallel_for_each(ctx.merged_sections,
|
2021-07-12 05:00:10 +03:00
|
|
|
[&](std::unique_ptr<MergedSection<E>> &sec) {
|
|
|
|
sec->assign_offsets(ctx);
|
2021-03-30 10:55:06 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static std::vector<std::span<T>> split(std::vector<T> &input, i64 unit) {
|
2021-09-08 13:51:02 +03:00
|
|
|
assert(input.size() > 0);
|
2021-03-30 10:55:06 +03:00
|
|
|
std::span<T> span(input);
|
|
|
|
std::vector<std::span<T>> vec;
|
|
|
|
|
|
|
|
while (span.size() >= unit) {
|
|
|
|
vec.push_back(span.subspan(0, unit));
|
|
|
|
span = span.subspan(unit);
|
|
|
|
}
|
|
|
|
if (!span.empty())
|
|
|
|
vec.push_back(span);
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
template <typename E>
|
|
|
|
void bin_sections(Context<E> &ctx) {
|
2021-04-07 09:38:31 +03:00
|
|
|
Timer t(ctx, "bin_sections");
|
2021-03-30 10:55:06 +03:00
|
|
|
|
2021-06-25 16:16:56 +03:00
|
|
|
static constexpr i64 num_shards = 128;
|
|
|
|
i64 unit = (ctx.objs.size() + num_shards - 1) / num_shards;
|
2021-03-30 10:55:06 +03:00
|
|
|
std::vector<std::span<ObjectFile<E> *>> slices = split(ctx.objs, unit);
|
|
|
|
|
2021-04-07 09:38:31 +03:00
|
|
|
i64 num_osec = ctx.output_sections.size();
|
2021-03-30 10:55:06 +03:00
|
|
|
|
|
|
|
std::vector<std::vector<std::vector<InputSection<E> *>>> groups(slices.size());
|
|
|
|
for (i64 i = 0; i < groups.size(); i++)
|
|
|
|
groups[i].resize(num_osec);
|
|
|
|
|
|
|
|
tbb::parallel_for((i64)0, (i64)slices.size(), [&](i64 i) {
|
|
|
|
for (ObjectFile<E> *file : slices[i])
|
2021-04-06 08:36:31 +03:00
|
|
|
for (std::unique_ptr<InputSection<E>> &isec : file->sections)
|
2021-04-06 07:37:01 +03:00
|
|
|
if (isec && isec->is_alive)
|
2021-04-06 08:36:31 +03:00
|
|
|
groups[i][isec->output_section->idx].push_back(isec.get());
|
2021-03-30 10:55:06 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
std::vector<i64> sizes(num_osec);
|
|
|
|
|
|
|
|
for (std::span<std::vector<InputSection<E> *>> group : groups)
|
|
|
|
for (i64 i = 0; i < group.size(); i++)
|
|
|
|
sizes[i] += group[i].size();
|
|
|
|
|
|
|
|
tbb::parallel_for((i64)0, num_osec, [&](i64 j) {
|
2021-04-07 09:38:31 +03:00
|
|
|
ctx.output_sections[j]->members.reserve(sizes[j]);
|
2021-03-30 10:55:06 +03:00
|
|
|
for (i64 i = 0; i < groups.size(); i++)
|
2021-04-07 09:38:31 +03:00
|
|
|
append(ctx.output_sections[j]->members, groups[i][j]);
|
2021-03-30 10:55:06 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-29 14:57:05 +03:00
|
|
|
// Create a dummy object file containing linker-synthesized
|
|
|
|
// symbols.
|
|
|
|
template <typename E>
|
|
|
|
ObjectFile<E> *create_internal_file(Context<E> &ctx) {
|
|
|
|
ObjectFile<E> *obj = new ObjectFile<E>;
|
2021-10-03 10:01:55 +03:00
|
|
|
ctx.obj_pool.push_back(std::unique_ptr<ObjectFile<E>>(obj));
|
2021-07-29 14:57:05 +03:00
|
|
|
|
|
|
|
// Create linker-synthesized symbols.
|
|
|
|
auto *esyms = new std::vector<ElfSym<E>>(1);
|
|
|
|
obj->symbols.push_back(new Symbol<E>);
|
|
|
|
obj->first_global = 1;
|
|
|
|
obj->is_alive = true;
|
|
|
|
obj->priority = 1;
|
|
|
|
|
|
|
|
auto add = [&](std::string_view name) {
|
|
|
|
ElfSym<E> esym = {};
|
|
|
|
esym.st_type = STT_NOTYPE;
|
|
|
|
esym.st_shndx = SHN_ABS;
|
|
|
|
esym.st_bind = STB_GLOBAL;
|
|
|
|
esym.st_visibility = STV_HIDDEN;
|
|
|
|
esyms->push_back(esym);
|
|
|
|
|
2021-10-05 02:31:26 +03:00
|
|
|
Symbol<E> *sym = intern(ctx, name);
|
2021-07-29 14:57:05 +03:00
|
|
|
obj->symbols.push_back(sym);
|
|
|
|
return sym;
|
|
|
|
};
|
|
|
|
|
|
|
|
ctx.__ehdr_start = add("__ehdr_start");
|
|
|
|
ctx.__init_array_start = add("__init_array_start");
|
|
|
|
ctx.__init_array_end = add("__init_array_end");
|
|
|
|
ctx.__fini_array_start = add("__fini_array_start");
|
|
|
|
ctx.__fini_array_end = add("__fini_array_end");
|
|
|
|
ctx.__preinit_array_start = add("__preinit_array_start");
|
|
|
|
ctx.__preinit_array_end = add("__preinit_array_end");
|
|
|
|
ctx._DYNAMIC = add("_DYNAMIC");
|
|
|
|
ctx._GLOBAL_OFFSET_TABLE_ = add("_GLOBAL_OFFSET_TABLE_");
|
|
|
|
ctx.__bss_start = add("__bss_start");
|
|
|
|
ctx._end = add("_end");
|
|
|
|
ctx._etext = add("_etext");
|
|
|
|
ctx._edata = add("_edata");
|
|
|
|
ctx.__executable_start = add("__executable_start");
|
|
|
|
|
|
|
|
ctx.__rel_iplt_start =
|
|
|
|
add(E::is_rel ? "__rel_iplt_start" : "__rela_iplt_start");
|
|
|
|
ctx.__rel_iplt_end =
|
|
|
|
add(E::is_rel ? "__rel_iplt_end" : "__rela_iplt_end");
|
|
|
|
|
|
|
|
if (ctx.arg.eh_frame_hdr)
|
|
|
|
ctx.__GNU_EH_FRAME_HDR = add("__GNU_EH_FRAME_HDR");
|
|
|
|
|
2021-10-05 02:31:26 +03:00
|
|
|
if (!intern(ctx, "end")->file)
|
2021-07-29 14:57:05 +03:00
|
|
|
ctx.end = add("end");
|
2021-10-05 02:31:26 +03:00
|
|
|
if (!intern(ctx, "etext")->file)
|
2021-07-29 14:57:05 +03:00
|
|
|
ctx.etext = add("etext");
|
2021-10-05 02:31:26 +03:00
|
|
|
if (!intern(ctx, "edata")->file)
|
2021-07-29 14:57:05 +03:00
|
|
|
ctx.edata = add("edata");
|
|
|
|
|
2021-10-04 04:16:24 +03:00
|
|
|
for (Chunk<E> *chunk : ctx.chunks) {
|
2021-07-29 14:57:05 +03:00
|
|
|
if (!is_c_identifier(chunk->name))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
add(save_string(ctx, "__start_" + std::string(chunk->name)));
|
|
|
|
add(save_string(ctx, "__stop_" + std::string(chunk->name)));
|
|
|
|
}
|
|
|
|
|
|
|
|
obj->elf_syms = *esyms;
|
2021-09-29 08:33:12 +03:00
|
|
|
obj->sym_subsections.resize(obj->elf_syms.size());
|
2021-07-29 14:57:05 +03:00
|
|
|
|
|
|
|
i64 num_globals = obj->elf_syms.size() - obj->first_global;
|
|
|
|
obj->symvers.resize(num_globals);
|
|
|
|
|
|
|
|
ctx.on_exit.push_back([=]() {
|
|
|
|
delete esyms;
|
|
|
|
delete obj->symbols[0];
|
|
|
|
});
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2021-03-30 10:55:06 +03:00
|
|
|
template <typename E>
|
|
|
|
void check_duplicate_symbols(Context<E> &ctx) {
|
2021-04-07 09:38:31 +03:00
|
|
|
Timer t(ctx, "check_dup_syms");
|
2021-03-30 10:55:06 +03:00
|
|
|
|
|
|
|
tbb::parallel_for_each(ctx.objs, [&](ObjectFile<E> *file) {
|
|
|
|
for (i64 i = file->first_global; i < file->elf_syms.size(); i++) {
|
|
|
|
const ElfSym<E> &esym = file->elf_syms[i];
|
|
|
|
Symbol<E> &sym = *file->symbols[i];
|
2021-04-06 08:36:31 +03:00
|
|
|
|
2021-05-28 18:31:40 +03:00
|
|
|
if (sym.file == file || sym.file == ctx.internal_obj ||
|
|
|
|
esym.is_undef() || esym.is_common() || (esym.st_bind == STB_WEAK))
|
2021-04-16 17:59:57 +03:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!esym.is_abs() && !file->get_section(esym)->is_alive)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Error(ctx) << "duplicate symbol: " << *file << ": " << *sym.file
|
|
|
|
<< ": " << sym;
|
2021-03-30 10:55:06 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-09-15 09:32:42 +03:00
|
|
|
ctx.checkpoint();
|
2021-03-30 10:55:06 +03:00
|
|
|
}
|
|
|
|
|
2021-05-30 19:10:00 +03:00
|
|
|
template <typename E>
|
|
|
|
void sort_init_fini(Context<E> &ctx) {
|
|
|
|
Timer t(ctx, "sort_init_fini");
|
|
|
|
|
|
|
|
auto get_priority = [](InputSection<E> *isec) {
|
2021-07-07 15:46:23 +03:00
|
|
|
static std::regex re(R"(_array\.(\d+)$)", std::regex_constants::optimize);
|
2021-05-30 19:10:00 +03:00
|
|
|
std::string name = isec->name().begin();
|
|
|
|
std::smatch m;
|
|
|
|
if (std::regex_search(name, m, re))
|
|
|
|
return std::stoi(m[1]);
|
|
|
|
return 65536;
|
|
|
|
};
|
|
|
|
|
|
|
|
for (std::unique_ptr<OutputSection<E>> &osec : ctx.output_sections) {
|
|
|
|
if (osec->name == ".init_array" || osec->name == ".fini_array") {
|
|
|
|
sort(osec->members, [&](InputSection<E> *a, InputSection<E> *b) {
|
|
|
|
return get_priority(a) < get_priority(b);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-30 10:55:06 +03:00
|
|
|
template <typename E>
|
2021-10-04 04:16:24 +03:00
|
|
|
std::vector<Chunk<E> *> collect_output_sections(Context<E> &ctx) {
|
|
|
|
std::vector<Chunk<E> *> vec;
|
2021-03-30 10:55:06 +03:00
|
|
|
|
2021-04-07 09:38:31 +03:00
|
|
|
for (std::unique_ptr<OutputSection<E>> &osec : ctx.output_sections)
|
2021-03-30 10:55:06 +03:00
|
|
|
if (!osec->members.empty())
|
2021-04-07 09:38:31 +03:00
|
|
|
vec.push_back(osec.get());
|
|
|
|
for (std::unique_ptr<MergedSection<E>> &osec : ctx.merged_sections)
|
2021-03-30 10:55:06 +03:00
|
|
|
if (osec->shdr.sh_size)
|
2021-04-07 09:38:31 +03:00
|
|
|
vec.push_back(osec.get());
|
2021-03-30 10:55:06 +03:00
|
|
|
|
|
|
|
// 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.
|
2021-10-04 04:16:24 +03:00
|
|
|
sort(vec, [](Chunk<E> *x, Chunk<E> *y) {
|
2021-03-30 10:55:06 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename E>
|
|
|
|
void compute_section_sizes(Context<E> &ctx) {
|
2021-04-07 09:38:31 +03:00
|
|
|
Timer t(ctx, "compute_section_sizes");
|
2021-03-30 10:55:06 +03:00
|
|
|
|
2021-04-07 09:38:31 +03:00
|
|
|
tbb::parallel_for_each(ctx.output_sections,
|
|
|
|
[&](std::unique_ptr<OutputSection<E>> &osec) {
|
2021-03-30 10:55:06 +03:00
|
|
|
if (osec->members.empty())
|
|
|
|
return;
|
|
|
|
|
2021-04-17 17:45:40 +03:00
|
|
|
struct T {
|
|
|
|
i64 offset;
|
|
|
|
i64 align;
|
|
|
|
};
|
|
|
|
|
|
|
|
T sum = tbb::parallel_scan(
|
|
|
|
tbb::blocked_range<i64>(0, osec->members.size(), 10000),
|
|
|
|
T{0, 1},
|
|
|
|
[&](const tbb::blocked_range<i64> &r, T sum, bool is_final) {
|
|
|
|
for (i64 i = r.begin(); i < r.end(); i++) {
|
|
|
|
InputSection<E> &isec = *osec->members[i];
|
2021-04-17 17:50:18 +03:00
|
|
|
sum.offset = align_to(sum.offset, isec.shdr.sh_addralign);
|
2021-04-17 17:45:40 +03:00
|
|
|
if (is_final)
|
|
|
|
isec.offset = sum.offset;
|
2021-04-17 17:50:18 +03:00
|
|
|
sum.offset += isec.shdr.sh_size;
|
2021-04-17 17:45:40 +03:00
|
|
|
sum.align = std::max<i64>(sum.align, isec.shdr.sh_addralign);
|
|
|
|
}
|
|
|
|
return sum;
|
|
|
|
},
|
|
|
|
[](T lhs, T rhs) {
|
|
|
|
i64 offset = align_to(lhs.offset, rhs.align) + rhs.offset;
|
|
|
|
i64 align = std::max(lhs.align, rhs.align);
|
|
|
|
return T{offset, align};
|
|
|
|
},
|
|
|
|
tbb::simple_partitioner());
|
|
|
|
|
|
|
|
osec->shdr.sh_size = sum.offset;
|
|
|
|
osec->shdr.sh_addralign = sum.align;
|
2021-03-30 10:55:06 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename E>
|
2021-06-24 18:36:47 +03:00
|
|
|
void claim_unresolved_symbols(Context<E> &ctx) {
|
|
|
|
Timer t(ctx, "claim_unresolved_symbols");
|
2021-03-30 10:55:06 +03:00
|
|
|
tbb::parallel_for_each(ctx.objs, [&](ObjectFile<E> *file) {
|
2021-06-24 18:36:47 +03:00
|
|
|
file->claim_unresolved_symbols(ctx);
|
2021-03-30 10:55:06 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename E>
|
|
|
|
void scan_rels(Context<E> &ctx) {
|
2021-04-07 09:38:31 +03:00
|
|
|
Timer t(ctx, "scan_rels");
|
2021-03-30 10:55:06 +03:00
|
|
|
|
|
|
|
// Scan relocations to find dynamic symbols.
|
|
|
|
tbb::parallel_for_each(ctx.objs, [&](ObjectFile<E> *file) {
|
|
|
|
file->scan_relocations(ctx);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Exit if there was a relocation that refers an undefined symbol.
|
2021-09-15 09:32:42 +03:00
|
|
|
ctx.checkpoint();
|
2021-03-30 10:55:06 +03:00
|
|
|
|
2021-04-01 06:14:27 +03:00
|
|
|
// Add symbol aliases for COPYREL.
|
|
|
|
tbb::parallel_for_each(ctx.dsos, [&](SharedFile<E> *file) {
|
|
|
|
for (Symbol<E> *sym : file->symbols)
|
2021-06-17 09:09:24 +03:00
|
|
|
if (sym->file == file && (sym->flags & NEEDS_COPYREL))
|
2021-04-01 06:14:27 +03:00
|
|
|
for (Symbol<E> *alias : file->find_aliases(sym))
|
|
|
|
alias->flags |= NEEDS_DYNSYM;
|
|
|
|
});
|
|
|
|
|
2021-03-30 10:55:06 +03:00
|
|
|
// Aggregate dynamic symbols to a single vector.
|
|
|
|
std::vector<InputFile<E> *> files;
|
|
|
|
append(files, ctx.objs);
|
|
|
|
append(files, ctx.dsos);
|
|
|
|
|
|
|
|
std::vector<std::vector<Symbol<E> *>> vec(files.size());
|
|
|
|
|
|
|
|
tbb::parallel_for((i64)0, (i64)files.size(), [&](i64 i) {
|
2021-04-13 18:16:39 +03:00
|
|
|
for (Symbol<E> *sym : files[i]->symbols) {
|
2021-05-10 08:05:21 +03:00
|
|
|
if (!files[i]->is_dso && (sym->is_imported || sym->is_exported))
|
2021-04-13 18:16:39 +03:00
|
|
|
sym->flags |= NEEDS_DYNSYM;
|
2021-05-10 08:05:21 +03:00
|
|
|
if (sym->file == files[i] && sym->flags)
|
2021-03-30 10:55:06 +03:00
|
|
|
vec[i].push_back(sym);
|
2021-04-13 18:16:39 +03:00
|
|
|
}
|
2021-03-30 10:55:06 +03:00
|
|
|
});
|
|
|
|
|
2021-04-01 06:14:27 +03:00
|
|
|
std::vector<Symbol<E> *> syms = flatten(vec);
|
|
|
|
|
|
|
|
ctx.symbol_aux.resize(syms.size());
|
|
|
|
for (i64 i = 0; i < syms.size(); i++)
|
|
|
|
syms[i]->aux_idx = i;
|
|
|
|
|
2021-03-30 10:55:06 +03:00
|
|
|
// Assign offsets in additional tables for each dynamic symbol.
|
2021-04-01 06:14:27 +03:00
|
|
|
for (Symbol<E> *sym : syms) {
|
2021-03-30 10:55:06 +03:00
|
|
|
if (sym->flags & NEEDS_DYNSYM)
|
|
|
|
ctx.dynsym->add_symbol(ctx, sym);
|
|
|
|
|
|
|
|
if (sym->flags & NEEDS_GOT)
|
|
|
|
ctx.got->add_got_symbol(ctx, sym);
|
|
|
|
|
|
|
|
if (sym->flags & NEEDS_PLT) {
|
2021-09-26 05:12:16 +03:00
|
|
|
// If we need to create a canonical PLT (i.e. ctx.arg.pic is false),
|
2021-09-25 12:10:26 +03:00
|
|
|
// we can't use .plt.got because otherwise .plt.got and .plt would
|
|
|
|
// refer each other, resulting in an infinite loop at runtime.
|
|
|
|
if ((sym->flags & NEEDS_GOT) && !ctx.arg.pic)
|
2021-03-30 10:55:06 +03:00
|
|
|
ctx.pltgot->add_symbol(ctx, sym);
|
|
|
|
else
|
|
|
|
ctx.plt->add_symbol(ctx, sym);
|
|
|
|
}
|
|
|
|
|
2021-03-31 14:18:12 +03:00
|
|
|
if (sym->flags & NEEDS_GOTTP)
|
|
|
|
ctx.got->add_gottp_symbol(ctx, sym);
|
2021-03-30 10:55:06 +03:00
|
|
|
|
|
|
|
if (sym->flags & NEEDS_TLSGD)
|
|
|
|
ctx.got->add_tlsgd_symbol(ctx, sym);
|
|
|
|
|
|
|
|
if (sym->flags & NEEDS_TLSDESC)
|
|
|
|
ctx.got->add_tlsdesc_symbol(ctx, sym);
|
|
|
|
|
|
|
|
if (sym->flags & NEEDS_TLSLD)
|
|
|
|
ctx.got->add_tlsld(ctx);
|
|
|
|
|
|
|
|
if (sym->flags & NEEDS_COPYREL) {
|
2021-09-08 13:51:02 +03:00
|
|
|
assert(sym->file->is_dso);
|
2021-03-30 10:55:06 +03:00
|
|
|
SharedFile<E> *file = (SharedFile<E> *)sym->file;
|
|
|
|
sym->copyrel_readonly = file->is_readonly(ctx, sym);
|
|
|
|
|
|
|
|
if (sym->copyrel_readonly)
|
|
|
|
ctx.dynbss_relro->add_symbol(ctx, sym);
|
|
|
|
else
|
|
|
|
ctx.dynbss->add_symbol(ctx, sym);
|
|
|
|
|
|
|
|
for (Symbol<E> *alias : file->find_aliases(sym)) {
|
|
|
|
alias->has_copyrel = true;
|
|
|
|
alias->value = sym->value;
|
|
|
|
alias->copyrel_readonly = sym->copyrel_readonly;
|
|
|
|
ctx.dynsym->add_symbol(ctx, alias);
|
|
|
|
}
|
|
|
|
}
|
2021-05-30 12:03:19 +03:00
|
|
|
|
|
|
|
sym->flags = 0;
|
2021-03-30 10:55:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename E>
|
|
|
|
void apply_version_script(Context<E> &ctx) {
|
2021-04-07 09:38:31 +03:00
|
|
|
Timer t(ctx, "apply_version_script");
|
2021-03-30 10:55:06 +03:00
|
|
|
|
|
|
|
for (VersionPattern &elem : ctx.arg.version_patterns) {
|
2021-09-08 13:51:02 +03:00
|
|
|
assert(elem.pattern != "*");
|
2021-03-30 10:55:06 +03:00
|
|
|
|
|
|
|
if (!elem.is_extern_cpp &&
|
|
|
|
elem.pattern.find('*') == elem.pattern.npos) {
|
2021-10-05 02:31:26 +03:00
|
|
|
Symbol<E> *sym = intern(ctx, elem.pattern);
|
2021-06-27 15:37:19 +03:00
|
|
|
if (sym->file && !sym->file->is_dso)
|
|
|
|
sym->ver_idx = elem.ver_idx;
|
2021-03-30 10:55:06 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-06-19 18:49:41 +03:00
|
|
|
std::regex re = glob_to_regex(elem.pattern);
|
2021-03-30 10:55:06 +03:00
|
|
|
|
|
|
|
tbb::parallel_for_each(ctx.objs, [&](ObjectFile<E> *file) {
|
|
|
|
for (Symbol<E> *sym : file->get_global_syms()) {
|
|
|
|
if (sym->file == file) {
|
2021-09-26 13:55:46 +03:00
|
|
|
std::string_view name = sym->name();
|
|
|
|
if (elem.is_extern_cpp)
|
|
|
|
name = demangle(name);
|
2021-06-19 18:49:41 +03:00
|
|
|
if (std::regex_match(name.begin(), name.end(), re))
|
2021-03-30 10:55:06 +03:00
|
|
|
sym->ver_idx = elem.ver_idx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename E>
|
|
|
|
void parse_symbol_version(Context<E> &ctx) {
|
2021-05-30 07:32:30 +03:00
|
|
|
if (!ctx.arg.shared)
|
|
|
|
return;
|
|
|
|
|
2021-04-07 09:38:31 +03:00
|
|
|
Timer t(ctx, "parse_symbol_version");
|
2021-03-30 10:55:06 +03:00
|
|
|
|
|
|
|
std::unordered_map<std::string_view, u16> verdefs;
|
|
|
|
for (i64 i = 0; i < ctx.arg.version_definitions.size(); i++)
|
|
|
|
verdefs[ctx.arg.version_definitions[i]] = i + VER_NDX_LAST_RESERVED + 1;
|
|
|
|
|
|
|
|
tbb::parallel_for_each(ctx.objs, [&](ObjectFile<E> *file) {
|
|
|
|
for (i64 i = 0; i < file->symbols.size() - file->first_global; i++) {
|
|
|
|
if (!file->symvers[i])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Symbol<E> *sym = file->symbols[i + file->first_global];
|
|
|
|
if (sym->file != file)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
std::string_view ver = file->symvers[i];
|
|
|
|
|
|
|
|
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(ctx) << *file << ": symbol " << *sym << " has undefined version "
|
|
|
|
<< ver;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
sym->ver_idx = it->second;
|
|
|
|
if (!is_default)
|
|
|
|
sym->ver_idx |= VERSYM_HIDDEN;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename E>
|
|
|
|
void compute_import_export(Context<E> &ctx) {
|
2021-04-07 09:38:31 +03:00
|
|
|
Timer t(ctx, "compute_import_export");
|
2021-03-30 10:55:06 +03:00
|
|
|
|
|
|
|
// Export symbols referenced by DSOs.
|
|
|
|
if (!ctx.arg.shared) {
|
|
|
|
tbb::parallel_for_each(ctx.dsos, [&](SharedFile<E> *file) {
|
2021-06-14 18:45:22 +03:00
|
|
|
for (Symbol<E> *sym : file->globals) {
|
|
|
|
if (sym->file && !sym->file->is_dso && sym->visibility != STV_HIDDEN) {
|
|
|
|
std::lock_guard lock(sym->mu);
|
2021-03-30 10:55:06 +03:00
|
|
|
sym->is_exported = true;
|
2021-06-14 18:45:22 +03:00
|
|
|
}
|
|
|
|
}
|
2021-03-30 10:55:06 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Global symbols are exported from DSO by default.
|
|
|
|
if (ctx.arg.shared || ctx.arg.export_dynamic) {
|
|
|
|
tbb::parallel_for_each(ctx.objs, [&](ObjectFile<E> *file) {
|
|
|
|
for (Symbol<E> *sym : file->get_global_syms()) {
|
|
|
|
if (sym->file != file)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (sym->visibility == STV_HIDDEN || sym->ver_idx == VER_NDX_LOCAL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
sym->is_exported = true;
|
|
|
|
|
|
|
|
if (ctx.arg.shared && sym->visibility != STV_PROTECTED &&
|
|
|
|
!ctx.arg.Bsymbolic &&
|
|
|
|
!(ctx.arg.Bsymbolic_functions && sym->get_type() == STT_FUNC))
|
|
|
|
sym->is_imported = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename E>
|
2021-05-05 17:37:48 +03:00
|
|
|
void clear_padding(Context<E> &ctx) {
|
2021-04-07 09:38:31 +03:00
|
|
|
Timer t(ctx, "clear_padding");
|
2021-03-30 10:55:06 +03:00
|
|
|
|
2021-10-04 04:16:24 +03:00
|
|
|
auto zero = [&](Chunk<E> *chunk, i64 next_start) {
|
2021-03-30 10:55:06 +03:00
|
|
|
i64 pos = chunk->shdr.sh_offset;
|
|
|
|
if (chunk->shdr.sh_type != SHT_NOBITS)
|
|
|
|
pos += chunk->shdr.sh_size;
|
|
|
|
memset(ctx.buf + pos, 0, next_start - pos);
|
|
|
|
};
|
|
|
|
|
|
|
|
for (i64 i = 1; i < ctx.chunks.size(); i++)
|
|
|
|
zero(ctx.chunks[i - 1], ctx.chunks[i]->shdr.sh_offset);
|
2021-05-05 17:37:48 +03:00
|
|
|
zero(ctx.chunks.back(), ctx.output_file->filesize);
|
2021-03-30 10:55:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// We want to sort output chunks in the following order.
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
template <typename E>
|
2021-10-04 04:16:24 +03:00
|
|
|
i64 get_section_rank(Context<E> &ctx, Chunk<E> *chunk) {
|
2021-03-30 10:55:06 +03:00
|
|
|
u64 type = chunk->shdr.sh_type;
|
|
|
|
u64 flags = chunk->shdr.sh_flags;
|
|
|
|
|
2021-04-12 08:10:28 +03:00
|
|
|
if (chunk == ctx.ehdr.get())
|
|
|
|
return -4;
|
|
|
|
if (chunk == ctx.phdr.get())
|
|
|
|
return -3;
|
|
|
|
if (chunk == ctx.interp.get())
|
|
|
|
return -2;
|
2021-05-31 18:53:06 +03:00
|
|
|
if (type == SHT_NOTE && (flags & SHF_ALLOC))
|
2021-04-12 08:10:28 +03:00
|
|
|
return -1;
|
|
|
|
if (chunk == ctx.shdr.get())
|
|
|
|
return 1 << 6;
|
2021-03-30 10:55:06 +03:00
|
|
|
if (!(flags & SHF_ALLOC))
|
2021-04-12 08:10:28 +03:00
|
|
|
return 1 << 5;
|
2021-03-30 10:55:06 +03:00
|
|
|
|
2021-04-12 08:10:28 +03:00
|
|
|
bool writable = (flags & SHF_WRITE);
|
2021-03-30 10:55:06 +03:00
|
|
|
bool exec = (flags & SHF_EXECINSTR);
|
|
|
|
bool tls = (flags & SHF_TLS);
|
|
|
|
bool relro = is_relro(ctx, chunk);
|
2021-04-12 08:10:28 +03:00
|
|
|
bool is_bss = (type == SHT_NOBITS);
|
2021-03-30 10:55:06 +03:00
|
|
|
|
2021-04-12 08:10:28 +03:00
|
|
|
return (writable << 4) | (exec << 3) | (!tls << 2) |
|
|
|
|
(!relro << 1) | is_bss;
|
2021-03-30 10:55:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the smallest number n such that
|
|
|
|
// n >= val and n % align == skew.
|
|
|
|
inline u64 align_with_skew(u64 val, u64 align, u64 skew) {
|
|
|
|
return align_to(val + align - skew, align) - align + skew;
|
|
|
|
}
|
|
|
|
|
2021-06-17 15:10:25 +03:00
|
|
|
// Assign virtual addresses and file offsets to output sections.
|
2021-03-30 10:55:06 +03:00
|
|
|
template <typename E>
|
|
|
|
i64 set_osec_offsets(Context<E> &ctx) {
|
2021-04-07 09:38:31 +03:00
|
|
|
Timer t(ctx, "osec_offset");
|
2021-03-30 10:55:06 +03:00
|
|
|
|
2021-06-17 15:10:25 +03:00
|
|
|
u64 fileoff = 0;
|
|
|
|
u64 vaddr = ctx.arg.image_base;
|
2021-03-30 10:55:06 +03:00
|
|
|
|
2021-06-17 15:10:25 +03:00
|
|
|
i64 i = 0;
|
|
|
|
i64 end = 0;
|
|
|
|
while (ctx.chunks[end]->shdr.sh_flags & SHF_ALLOC)
|
|
|
|
end++;
|
2021-03-30 10:55:06 +03:00
|
|
|
|
2021-06-17 15:10:25 +03:00
|
|
|
while (i < end) {
|
2021-07-16 09:06:28 +03:00
|
|
|
fileoff = align_with_skew(fileoff, COMMON_PAGE_SIZE, vaddr % COMMON_PAGE_SIZE);
|
2021-03-30 10:55:06 +03:00
|
|
|
|
2021-06-25 16:16:56 +03:00
|
|
|
// Each group consists of zero or more non-BSS sections followed
|
|
|
|
// by zero or more BSS sections. Virtual addresses of non-BSS
|
|
|
|
// sections need to be congruent to file offsets modulo the page size.
|
|
|
|
// BSS sections don't increment file offsets.
|
2021-06-17 15:10:25 +03:00
|
|
|
for (; i < end && ctx.chunks[i]->shdr.sh_type != SHT_NOBITS; i++) {
|
2021-10-04 04:16:24 +03:00
|
|
|
Chunk<E> &chunk = *ctx.chunks[i];
|
2021-06-17 15:10:25 +03:00
|
|
|
u64 prev_vaddr = vaddr;
|
2021-03-30 10:55:06 +03:00
|
|
|
|
2021-06-17 15:10:25 +03:00
|
|
|
if (chunk.new_page)
|
2021-07-16 09:06:28 +03:00
|
|
|
vaddr = align_to(vaddr, COMMON_PAGE_SIZE);
|
2021-06-17 15:10:25 +03:00
|
|
|
vaddr = align_to(vaddr, chunk.shdr.sh_addralign);
|
|
|
|
fileoff += vaddr - prev_vaddr;
|
2021-03-30 10:55:06 +03:00
|
|
|
|
2021-06-17 15:10:25 +03:00
|
|
|
chunk.shdr.sh_addr = vaddr;
|
|
|
|
vaddr += chunk.shdr.sh_size;
|
2021-03-30 10:55:06 +03:00
|
|
|
|
2021-06-17 15:10:25 +03:00
|
|
|
chunk.shdr.sh_offset = fileoff;
|
|
|
|
fileoff += chunk.shdr.sh_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; i < end && ctx.chunks[i]->shdr.sh_type == SHT_NOBITS; i++) {
|
2021-10-04 04:16:24 +03:00
|
|
|
Chunk<E> &chunk = *ctx.chunks[i];
|
2021-06-17 15:10:25 +03:00
|
|
|
|
|
|
|
if (chunk.new_page)
|
2021-07-16 09:06:28 +03:00
|
|
|
vaddr = align_to(vaddr, COMMON_PAGE_SIZE);
|
2021-06-17 15:10:25 +03:00
|
|
|
vaddr = align_to(vaddr, chunk.shdr.sh_addralign);
|
2021-07-16 09:06:28 +03:00
|
|
|
fileoff = align_with_skew(fileoff, COMMON_PAGE_SIZE, vaddr % COMMON_PAGE_SIZE);
|
2021-06-17 15:10:25 +03:00
|
|
|
|
|
|
|
chunk.shdr.sh_addr = vaddr;
|
|
|
|
chunk.shdr.sh_offset = fileoff;
|
|
|
|
if (!(chunk.shdr.sh_flags & SHF_TLS))
|
|
|
|
vaddr += chunk.shdr.sh_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; i < ctx.chunks.size(); i++) {
|
2021-10-04 04:16:24 +03:00
|
|
|
Chunk<E> &chunk = *ctx.chunks[i];
|
2021-09-08 13:51:02 +03:00
|
|
|
assert(!(chunk.shdr.sh_flags & SHF_ALLOC));
|
2021-06-17 15:10:25 +03:00
|
|
|
fileoff = align_to(fileoff, chunk.shdr.sh_addralign);
|
|
|
|
chunk.shdr.sh_offset = fileoff;
|
|
|
|
fileoff += chunk.shdr.sh_size;
|
2021-03-30 10:55:06 +03:00
|
|
|
}
|
|
|
|
return fileoff;
|
|
|
|
}
|
|
|
|
|
2021-04-01 13:02:12 +03:00
|
|
|
template <typename E>
|
|
|
|
static i64 get_num_irelative_relocs(Context<E> &ctx) {
|
|
|
|
i64 n = 0;
|
|
|
|
for (Symbol<E> *sym : ctx.got->got_syms)
|
|
|
|
if (sym->get_type() == STT_GNU_IFUNC)
|
|
|
|
n++;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2021-03-30 10:55:06 +03:00
|
|
|
template <typename E>
|
|
|
|
void fix_synthetic_symbols(Context<E> &ctx) {
|
2021-04-06 06:25:15 +03:00
|
|
|
auto start = [](Symbol<E> *sym, auto &chunk) {
|
2021-03-30 10:55:06 +03:00
|
|
|
if (sym && chunk) {
|
|
|
|
sym->shndx = chunk->shndx;
|
|
|
|
sym->value = chunk->shdr.sh_addr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-04-06 06:25:15 +03:00
|
|
|
auto stop = [](Symbol<E> *sym, auto &chunk) {
|
2021-03-30 10:55:06 +03:00
|
|
|
if (sym && chunk) {
|
|
|
|
sym->shndx = chunk->shndx;
|
|
|
|
sym->value = chunk->shdr.sh_addr + chunk->shdr.sh_size;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// __bss_start
|
2021-10-04 04:16:24 +03:00
|
|
|
for (Chunk<E> *chunk : ctx.chunks) {
|
|
|
|
if (chunk->kind == Chunk<E>::REGULAR && chunk->name == ".bss") {
|
2021-03-30 10:55:06 +03:00
|
|
|
start(ctx.__bss_start, chunk);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// __ehdr_start and __executable_start
|
2021-10-04 04:16:24 +03:00
|
|
|
for (Chunk<E> *chunk : ctx.chunks) {
|
2021-03-30 10:55:06 +03:00
|
|
|
if (chunk->shndx == 1) {
|
|
|
|
ctx.__ehdr_start->shndx = 1;
|
|
|
|
ctx.__ehdr_start->value = ctx.ehdr->shdr.sh_addr;
|
|
|
|
|
|
|
|
ctx.__executable_start->shndx = 1;
|
|
|
|
ctx.__executable_start->value = ctx.ehdr->shdr.sh_addr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-01 11:18:55 +03:00
|
|
|
// __rel_iplt_start
|
2021-04-01 11:11:22 +03:00
|
|
|
start(ctx.__rel_iplt_start, ctx.reldyn);
|
2021-04-01 11:18:55 +03:00
|
|
|
|
|
|
|
// __rel_iplt_end
|
|
|
|
ctx.__rel_iplt_end->shndx = ctx.reldyn->shndx;
|
2021-04-01 13:02:12 +03:00
|
|
|
ctx.__rel_iplt_end->value = ctx.reldyn->shdr.sh_addr +
|
|
|
|
get_num_irelative_relocs(ctx) * sizeof(ElfRel<E>);
|
2021-03-30 10:55:06 +03:00
|
|
|
|
|
|
|
// __{init,fini}_array_{start,end}
|
2021-10-04 04:16:24 +03:00
|
|
|
for (Chunk<E> *chunk : ctx.chunks) {
|
2021-03-30 10:55:06 +03:00
|
|
|
switch (chunk->shdr.sh_type) {
|
|
|
|
case SHT_INIT_ARRAY:
|
|
|
|
start(ctx.__init_array_start, chunk);
|
|
|
|
stop(ctx.__init_array_end, chunk);
|
|
|
|
break;
|
|
|
|
case SHT_FINI_ARRAY:
|
|
|
|
start(ctx.__fini_array_start, chunk);
|
|
|
|
stop(ctx.__fini_array_end, chunk);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// _end, _etext, _edata and the like
|
2021-10-04 04:16:24 +03:00
|
|
|
for (Chunk<E> *chunk : ctx.chunks) {
|
|
|
|
if (chunk->kind == Chunk<E>::HEADER)
|
2021-03-30 10:55:06 +03:00
|
|
|
continue;
|
|
|
|
|
2021-05-28 20:05:32 +03:00
|
|
|
if (chunk->shdr.sh_flags & SHF_ALLOC) {
|
2021-03-30 10:55:06 +03:00
|
|
|
stop(ctx._end, chunk);
|
2021-05-28 20:05:32 +03:00
|
|
|
stop(ctx.end, chunk);
|
|
|
|
}
|
2021-03-30 10:55:06 +03:00
|
|
|
|
2021-05-28 20:05:32 +03:00
|
|
|
if (chunk->shdr.sh_flags & SHF_EXECINSTR) {
|
2021-03-30 10:55:06 +03:00
|
|
|
stop(ctx._etext, chunk);
|
2021-05-28 20:05:32 +03:00
|
|
|
stop(ctx.etext, chunk);
|
|
|
|
}
|
2021-03-30 10:55:06 +03:00
|
|
|
|
2021-05-28 20:05:32 +03:00
|
|
|
if (chunk->shdr.sh_type != SHT_NOBITS &&
|
|
|
|
(chunk->shdr.sh_flags & SHF_ALLOC)) {
|
2021-03-30 10:55:06 +03:00
|
|
|
stop(ctx._edata, chunk);
|
2021-05-28 20:05:32 +03:00
|
|
|
stop(ctx.edata, chunk);
|
|
|
|
}
|
2021-03-30 10:55:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// _DYNAMIC
|
|
|
|
start(ctx._DYNAMIC, ctx.dynamic);
|
|
|
|
|
|
|
|
// _GLOBAL_OFFSET_TABLE_
|
2021-07-07 06:51:23 +03:00
|
|
|
if (E::e_machine == EM_X86_64 || E::e_machine == EM_386)
|
|
|
|
start(ctx._GLOBAL_OFFSET_TABLE_, ctx.gotplt);
|
|
|
|
else if (E::e_machine == EM_AARCH64)
|
|
|
|
start(ctx._GLOBAL_OFFSET_TABLE_, ctx.got);
|
|
|
|
else
|
2021-09-30 18:35:42 +03:00
|
|
|
unreachable();
|
2021-03-30 10:55:06 +03:00
|
|
|
|
|
|
|
// __GNU_EH_FRAME_HDR
|
|
|
|
start(ctx.__GNU_EH_FRAME_HDR, ctx.eh_frame_hdr);
|
|
|
|
|
|
|
|
// __start_ and __stop_ symbols
|
2021-10-04 04:16:24 +03:00
|
|
|
for (Chunk<E> *chunk : ctx.chunks) {
|
2021-03-30 10:55:06 +03:00
|
|
|
if (is_c_identifier(chunk->name)) {
|
2021-04-06 12:18:45 +03:00
|
|
|
std::string_view sym1 =
|
|
|
|
save_string(ctx, "__start_" + std::string(chunk->name));
|
|
|
|
std::string_view sym2 =
|
|
|
|
save_string(ctx, "__stop_" + std::string(chunk->name));
|
|
|
|
|
2021-10-05 02:31:26 +03:00
|
|
|
start(intern(ctx, sym1), chunk);
|
|
|
|
stop(intern(ctx, sym2), chunk);
|
2021-03-30 10:55:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-06 05:29:02 +03:00
|
|
|
template <typename E>
|
|
|
|
void compress_debug_sections(Context<E> &ctx) {
|
|
|
|
Timer t(ctx, "compress_debug_sections");
|
|
|
|
|
|
|
|
tbb::parallel_for((i64)0, (i64)ctx.chunks.size(), [&](i64 i) {
|
2021-10-04 04:16:24 +03:00
|
|
|
Chunk<E> &chunk = *ctx.chunks[i];
|
2021-05-06 05:29:02 +03:00
|
|
|
|
2021-06-03 16:22:29 +03:00
|
|
|
if ((chunk.shdr.sh_flags & SHF_ALLOC) || chunk.shdr.sh_size == 0 ||
|
|
|
|
!chunk.name.starts_with(".debug"))
|
|
|
|
return;
|
|
|
|
|
2021-10-04 04:16:24 +03:00
|
|
|
Chunk<E> *comp = nullptr;
|
2021-06-03 16:22:29 +03:00
|
|
|
if (ctx.arg.compress_debug_sections == COMPRESS_GABI)
|
|
|
|
comp = new GabiCompressedSection<E>(ctx, chunk);
|
|
|
|
else if (ctx.arg.compress_debug_sections == COMPRESS_GNU)
|
|
|
|
comp = new GnuCompressedSection<E>(ctx, chunk);
|
2021-09-08 13:51:02 +03:00
|
|
|
assert(comp);
|
2021-06-03 16:22:29 +03:00
|
|
|
|
2021-10-04 04:16:24 +03:00
|
|
|
ctx.output_chunks.push_back(std::unique_ptr<Chunk<E>>(comp));
|
2021-06-03 16:22:29 +03:00
|
|
|
ctx.chunks[i] = comp;
|
2021-05-06 05:29:02 +03:00
|
|
|
});
|
|
|
|
|
2021-06-03 16:22:29 +03:00
|
|
|
ctx.shstrtab->update_shdr(ctx);
|
2021-05-06 05:29:02 +03:00
|
|
|
ctx.ehdr->update_shdr(ctx);
|
|
|
|
ctx.shdr->update_shdr(ctx);
|
|
|
|
}
|
|
|
|
|
2021-03-30 20:10:56 +03:00
|
|
|
#define INSTANTIATE(E) \
|
|
|
|
template void apply_exclude_libs(Context<E> &ctx); \
|
|
|
|
template void create_synthetic_sections(Context<E> &ctx); \
|
2021-06-19 14:29:16 +03:00
|
|
|
template void resolve_symbols(Context<E> &ctx); \
|
2021-03-30 20:10:56 +03:00
|
|
|
template void eliminate_comdats(Context<E> &ctx); \
|
|
|
|
template void convert_common_symbols(Context<E> &ctx); \
|
|
|
|
template void compute_merged_section_sizes(Context<E> &ctx); \
|
|
|
|
template void bin_sections(Context<E> &ctx); \
|
2021-07-29 14:57:05 +03:00
|
|
|
template ObjectFile<E> *create_internal_file(Context<E> &ctx); \
|
2021-03-30 20:10:56 +03:00
|
|
|
template void check_duplicate_symbols(Context<E> &ctx); \
|
2021-05-30 19:10:00 +03:00
|
|
|
template void sort_init_fini(Context<E> &ctx); \
|
2021-10-04 04:16:24 +03:00
|
|
|
template std::vector<Chunk<E> *> collect_output_sections(Context<E> &ctx); \
|
2021-03-30 20:10:56 +03:00
|
|
|
template void compute_section_sizes(Context<E> &ctx); \
|
2021-06-25 16:16:56 +03:00
|
|
|
template void claim_unresolved_symbols(Context<E> &ctx); \
|
2021-03-30 20:10:56 +03:00
|
|
|
template void scan_rels(Context<E> &ctx); \
|
|
|
|
template void apply_version_script(Context<E> &ctx); \
|
|
|
|
template void parse_symbol_version(Context<E> &ctx); \
|
|
|
|
template void compute_import_export(Context<E> &ctx); \
|
2021-05-05 17:37:48 +03:00
|
|
|
template void clear_padding(Context<E> &ctx); \
|
2021-10-04 04:16:24 +03:00
|
|
|
template i64 get_section_rank(Context<E> &ctx, Chunk<E> *chunk); \
|
2021-03-30 20:10:56 +03:00
|
|
|
template i64 set_osec_offsets(Context<E> &ctx); \
|
2021-05-06 05:29:02 +03:00
|
|
|
template void fix_synthetic_symbols(Context<E> &ctx); \
|
|
|
|
template void compress_debug_sections(Context<E> &ctx);
|
2021-03-30 20:10:56 +03:00
|
|
|
|
|
|
|
INSTANTIATE(X86_64);
|
|
|
|
INSTANTIATE(I386);
|
2021-07-03 15:12:08 +03:00
|
|
|
INSTANTIATE(AARCH64);
|
2021-09-02 12:19:43 +03:00
|
|
|
|
|
|
|
} // namespace mold::elf
|