1
1
mirror of https://github.com/rui314/mold.git synced 2024-09-22 18:40:59 +03:00
mold/output_chunks.cc

1535 lines
45 KiB
C++
Raw Normal View History

2020-10-20 08:54:35 +03:00
#include "mold.h"
2020-10-13 14:35:35 +03:00
2021-01-26 13:46:34 +03:00
#include <openssl/rand.h>
#include <openssl/sha.h>
2020-11-03 14:47:39 +03:00
#include <shared_mutex>
2021-01-16 05:57:36 +03:00
#include <tbb/parallel_for_each.h>
2021-01-21 03:58:46 +03:00
#include <tbb/parallel_sort.h>
2020-11-03 14:47:39 +03:00
2021-03-29 14:29:57 +03:00
template <typename E>
i64 BuildId::size(Context<E> &ctx) const {
switch (kind) {
case HEX:
return value.size();
case HASH:
return hash_size;
case UUID:
return 16;
}
2021-03-29 14:55:27 +03:00
unreachable(ctx);
2021-03-29 14:29:57 +03:00
}
template <typename E>
void OutputEhdr<E>::copy_buf(Context<E> &ctx) {
2021-03-29 15:02:12 +03:00
ElfEhdr<E> &hdr = *(ElfEhdr<E> *)(ctx.buf + this->shdr.sh_offset);
2020-11-16 18:17:01 +03:00
memset(&hdr, 0, sizeof(hdr));
2020-12-12 06:59:54 +03:00
2020-11-16 18:17:01 +03:00
memcpy(&hdr.e_ident, "\177ELF", 4);
2021-03-30 10:19:18 +03:00
hdr.e_ident[EI_CLASS] = E::is_64 ? ELFCLASS64 : ELFCLASS32;
hdr.e_ident[EI_DATA] = E::is_le ? ELFDATA2LSB : ELFDATA2MSB;
2020-11-16 18:17:01 +03:00
hdr.e_ident[EI_VERSION] = EV_CURRENT;
2021-03-29 07:20:51 +03:00
hdr.e_type = ctx.arg.pic ? ET_DYN : ET_EXEC;
2021-03-30 10:19:18 +03:00
hdr.e_machine = E::e_machine;
2020-11-16 18:17:01 +03:00
hdr.e_version = EV_CURRENT;
2021-03-29 07:20:51 +03:00
if (!ctx.arg.entry.empty())
2021-03-29 17:50:19 +03:00
hdr.e_entry = Symbol<E>::intern(ctx, ctx.arg.entry)->get_addr(ctx);
2021-03-29 07:20:51 +03:00
hdr.e_phoff = ctx.phdr->shdr.sh_offset;
hdr.e_shoff = ctx.shdr->shdr.sh_offset;
2021-03-29 15:02:12 +03:00
hdr.e_ehsize = sizeof(ElfEhdr<E>);
2021-03-29 15:05:52 +03:00
hdr.e_phentsize = sizeof(ElfPhdr<E>);
hdr.e_phnum = ctx.phdr->shdr.sh_size / sizeof(ElfPhdr<E>);
2021-03-29 15:03:09 +03:00
hdr.e_shentsize = sizeof(ElfShdr<E>);
hdr.e_shnum = ctx.shdr->shdr.sh_size / sizeof(ElfShdr<E>);
2021-03-29 07:20:51 +03:00
hdr.e_shstrndx = ctx.shstrtab->shndx;
2020-11-16 18:17:01 +03:00
}
2021-03-29 14:29:57 +03:00
template <typename E>
void OutputShdr<E>::update_shdr(Context<E> &ctx) {
2021-01-24 06:01:43 +03:00
i64 n = 1;
2021-03-29 14:29:57 +03:00
for (OutputChunk<E> *chunk : ctx.chunks)
if (chunk->kind != OutputChunk<E>::HEADER)
2021-01-23 18:03:35 +03:00
n++;
2021-03-29 15:03:09 +03:00
this->shdr.sh_size = n * sizeof(ElfShdr<E>);
2020-11-16 18:23:51 +03:00
}
2021-03-29 14:29:57 +03:00
template <typename E>
void OutputShdr<E>::copy_buf(Context<E> &ctx) {
2021-03-29 15:03:09 +03:00
ElfShdr<E> *hdr = (ElfShdr<E> *)(ctx.buf + this->shdr.sh_offset);
2021-01-24 03:38:23 +03:00
hdr[0] = {};
2020-11-16 18:23:51 +03:00
2021-01-24 06:01:43 +03:00
i64 i = 1;
2021-03-29 14:29:57 +03:00
for (OutputChunk<E> *chunk : ctx.chunks)
if (chunk->kind != OutputChunk<E>::HEADER)
2021-01-24 03:38:23 +03:00
hdr[i++] = chunk->shdr;
2020-11-16 18:23:51 +03:00
}
2021-03-29 14:29:57 +03:00
template <typename E>
static i64 to_phdr_flags(OutputChunk<E> *chunk) {
2021-01-24 06:01:43 +03:00
i64 ret = PF_R;
2020-11-16 18:33:41 +03:00
if (chunk->shdr.sh_flags & SHF_WRITE)
ret |= PF_W;
if (chunk->shdr.sh_flags & SHF_EXECINSTR)
ret |= PF_X;
return ret;
}
2021-03-29 14:29:57 +03:00
template <typename E>
bool is_relro(Context<E> &ctx, OutputChunk<E> *chunk) {
2021-03-12 13:57:34 +03:00
u64 flags = chunk->shdr.sh_flags;
u64 type = chunk->shdr.sh_type;
std::string_view name = chunk->name;
bool match = (flags & SHF_TLS) || type == SHT_INIT_ARRAY ||
type == SHT_FINI_ARRAY || type == SHT_PREINIT_ARRAY ||
2021-03-29 07:20:51 +03:00
chunk == ctx.got || chunk == ctx.dynamic ||
2021-03-13 07:21:31 +03:00
name.ends_with(".rel.ro");
2021-03-12 13:57:34 +03:00
return (flags & SHF_WRITE) && match;
}
2021-03-29 14:29:57 +03:00
template <typename E>
2021-03-29 15:05:52 +03:00
std::vector<ElfPhdr<E>> create_phdr(Context<E> &ctx) {
std::vector<ElfPhdr<E>> vec;
2020-11-16 18:33:41 +03:00
2021-03-29 14:29:57 +03:00
auto define = [&](u64 type, u64 flags, i64 min_align, OutputChunk<E> *chunk) {
2020-11-16 18:33:41 +03:00
vec.push_back({});
2021-03-29 15:05:52 +03:00
ElfPhdr<E> &phdr = vec.back();
2020-11-16 18:33:41 +03:00
phdr.p_type = type;
phdr.p_flags = flags;
2021-03-12 07:17:45 +03:00
phdr.p_align = std::max<u64>(min_align, chunk->shdr.sh_addralign);
2020-11-16 18:33:41 +03:00
phdr.p_offset = chunk->shdr.sh_offset;
2021-03-13 07:23:32 +03:00
phdr.p_filesz =
(chunk->shdr.sh_type == SHT_NOBITS) ? 0 : chunk->shdr.sh_size;
2020-11-16 18:33:41 +03:00
phdr.p_vaddr = chunk->shdr.sh_addr;
phdr.p_memsz = chunk->shdr.sh_size;
};
2021-03-29 14:29:57 +03:00
auto append = [&](OutputChunk<E> *chunk) {
2021-03-29 15:05:52 +03:00
ElfPhdr<E> &phdr = vec.back();
2020-11-16 18:33:41 +03:00
phdr.p_align = std::max<u64>(phdr.p_align, chunk->shdr.sh_addralign);
phdr.p_filesz = (chunk->shdr.sh_type == SHT_NOBITS)
? chunk->shdr.sh_offset - phdr.p_offset
: chunk->shdr.sh_offset + chunk->shdr.sh_size - phdr.p_offset;
phdr.p_memsz = chunk->shdr.sh_addr + chunk->shdr.sh_size - phdr.p_vaddr;
};
2021-03-29 14:29:57 +03:00
auto is_bss = [](OutputChunk<E> *chunk) {
2021-03-13 07:23:32 +03:00
return chunk->shdr.sh_type == SHT_NOBITS &&
!(chunk->shdr.sh_flags & SHF_TLS);
2020-11-16 18:33:41 +03:00
};
// Create a PT_PHDR for the program header itself.
2021-03-29 07:20:51 +03:00
define(PT_PHDR, PF_R, 8, ctx.phdr);
2020-11-16 18:33:41 +03:00
2021-01-16 05:57:36 +03:00
// Create a PT_INTERP.
2021-03-29 07:20:51 +03:00
if (ctx.interp)
define(PT_INTERP, PF_R, 1, ctx.interp);
2020-11-16 18:33:41 +03:00
2021-01-16 05:57:36 +03:00
// Create a PT_NOTE for each group of SHF_NOTE sections with the same
// alignment requirement.
2021-03-29 07:20:51 +03:00
for (i64 i = 0, end = ctx.chunks.size(); i < end;) {
2021-03-29 14:29:57 +03:00
OutputChunk<E> *first = ctx.chunks[i++];
2021-01-16 05:57:36 +03:00
if (first->shdr.sh_type != SHT_NOTE)
continue;
2021-01-24 06:01:43 +03:00
i64 flags = to_phdr_flags(first);
i64 alignment = first->shdr.sh_addralign;
2021-01-16 05:57:36 +03:00
define(PT_NOTE, flags, alignment, first);
2021-03-29 07:20:51 +03:00
while (i < end && ctx.chunks[i]->shdr.sh_type == SHT_NOTE &&
to_phdr_flags(ctx.chunks[i]) == flags &&
ctx.chunks[i]->shdr.sh_addralign == alignment)
append(ctx.chunks[i++]);
2021-01-16 05:57:36 +03:00
}
2020-11-16 18:33:41 +03:00
// Create PT_LOAD segments.
2021-03-29 07:20:51 +03:00
for (i64 i = 0, end = ctx.chunks.size(); i < end;) {
2021-03-29 14:29:57 +03:00
OutputChunk<E> *first = ctx.chunks[i++];
2020-11-16 18:33:41 +03:00
if (!(first->shdr.sh_flags & SHF_ALLOC))
break;
2021-01-24 06:01:43 +03:00
i64 flags = to_phdr_flags(first);
2020-11-16 18:33:41 +03:00
define(PT_LOAD, flags, PAGE_SIZE, first);
first->new_page = true;
2020-11-16 18:33:41 +03:00
if (!is_bss(first))
2021-03-29 07:20:51 +03:00
while (i < end && !is_bss(ctx.chunks[i]) &&
to_phdr_flags(ctx.chunks[i]) == flags)
append(ctx.chunks[i++]);
2020-11-16 18:33:41 +03:00
2021-03-29 07:20:51 +03:00
while (i < end && is_bss(ctx.chunks[i]) &&
to_phdr_flags(ctx.chunks[i]) == flags)
append(ctx.chunks[i++]);
2020-11-16 18:33:41 +03:00
}
// Create a PT_TLS.
2021-03-29 07:20:51 +03:00
for (i64 i = 0; i < ctx.chunks.size(); i++) {
if (!(ctx.chunks[i]->shdr.sh_flags & SHF_TLS))
2021-01-16 05:57:36 +03:00
continue;
2021-03-29 07:20:51 +03:00
define(PT_TLS, to_phdr_flags(ctx.chunks[i]), 1, ctx.chunks[i]);
2021-01-16 05:57:36 +03:00
i++;
2021-03-29 07:20:51 +03:00
while (i < ctx.chunks.size() && (ctx.chunks[i]->shdr.sh_flags & SHF_TLS))
append(ctx.chunks[i++]);
2020-11-16 18:33:41 +03:00
}
// Add PT_DYNAMIC
2021-03-29 07:20:51 +03:00
if (ctx.dynamic->shdr.sh_size)
define(PT_DYNAMIC, PF_R | PF_W, 1, ctx.dynamic);
2020-11-16 18:33:41 +03:00
2021-01-21 03:58:46 +03:00
// Add PT_GNU_EH_FRAME
2021-03-29 07:20:51 +03:00
if (ctx.eh_frame_hdr)
define(PT_GNU_EH_FRAME, PF_R, 1, ctx.eh_frame_hdr);
2021-01-21 03:58:46 +03:00
2020-12-17 16:06:51 +03:00
// Add PT_GNU_STACK, which is a marker segment that doesn't really
2021-03-12 12:09:15 +03:00
// contain any segments. It controls executable bit of stack area.
2021-03-30 08:00:12 +03:00
ElfPhdr<E> phdr = {};
phdr.p_type = PT_GNU_STACK,
phdr.p_flags = ctx.arg.z_execstack ? (PF_R | PF_W | PF_X) : (PF_R | PF_W),
vec.push_back(phdr);
2020-12-17 16:06:51 +03:00
// Create a PT_GNU_RELRO.
2021-03-29 07:20:51 +03:00
if (ctx.arg.z_relro) {
for (i64 i = 0; i < ctx.chunks.size(); i++) {
2021-03-29 09:53:00 +03:00
if (!is_relro(ctx, ctx.chunks[i]))
continue;
2021-03-29 07:20:51 +03:00
define(PT_GNU_RELRO, PF_R, 1, ctx.chunks[i]);
ctx.chunks[i]->new_page = true;
i++;
2021-03-29 09:53:00 +03:00
while (i < ctx.chunks.size() && is_relro(ctx, ctx.chunks[i]))
2021-03-29 07:20:51 +03:00
append(ctx.chunks[i++]);
ctx.chunks[i - 1]->new_page_end = true;
}
}
2020-11-16 18:33:41 +03:00
return vec;
}
2021-03-29 14:29:57 +03:00
template <typename E>
void OutputPhdr<E>::update_shdr(Context<E> &ctx) {
2021-03-29 15:05:52 +03:00
this->shdr.sh_size = create_phdr(ctx).size() * sizeof(ElfPhdr<E>);
2020-11-16 18:33:41 +03:00
}
2021-03-29 14:29:57 +03:00
template <typename E>
void OutputPhdr<E>::copy_buf(Context<E> &ctx) {
write_vector(ctx.buf + this->shdr.sh_offset, create_phdr(ctx));
2021-03-29 07:20:51 +03:00
}
2021-03-29 14:29:57 +03:00
template <typename E>
void InterpSection<E>::update_shdr(Context<E> &ctx) {
this->shdr.sh_size = ctx.arg.dynamic_linker.size() + 1;
2020-11-16 18:43:32 +03:00
}
2021-03-29 14:29:57 +03:00
template <typename E>
void InterpSection<E>::copy_buf(Context<E> &ctx) {
write_string(ctx.buf + this->shdr.sh_offset, ctx.arg.dynamic_linker);
2020-11-16 19:05:01 +03:00
}
2021-03-29 14:29:57 +03:00
template <typename E>
void RelDynSection<E>::update_shdr(Context<E> &ctx) {
this->shdr.sh_link = ctx.dynsym->shndx;
2020-11-18 14:29:24 +03:00
2021-03-29 14:29:57 +03:00
// .rel.dyn contents are filled by GotSection::copy_buf(Context<E> &ctx) and
2021-03-16 09:38:17 +03:00
// InputSection::apply_reloc_alloc().
2021-03-29 10:02:12 +03:00
i64 offset = ctx.got->get_reldyn_size(ctx);
2021-03-29 14:29:57 +03:00
for (ObjectFile<E> *file : ctx.objs) {
2021-03-16 09:38:17 +03:00
file->reldyn_offset = offset;
2021-03-30 09:42:11 +03:00
offset += file->num_dynrel * sizeof(ElfRel<E>);
2020-12-16 14:56:04 +03:00
}
2021-03-29 14:29:57 +03:00
this->shdr.sh_size = offset;
2020-11-17 07:30:33 +03:00
}
2021-03-29 14:29:57 +03:00
template <typename E>
void RelDynSection<E>::sort(Context<E> &ctx) {
2021-03-17 14:57:09 +03:00
Timer t("sort_dynamic_relocs");
2021-03-30 09:42:11 +03:00
ElfRel<E> *begin = (ElfRel<E> *)(ctx.buf + this->shdr.sh_offset);
ElfRel<E> *end =
(ElfRel<E> *)(ctx.buf + this->shdr.sh_offset + this->shdr.sh_size);
2021-03-17 14:57:09 +03:00
2021-03-30 09:42:11 +03:00
tbb::parallel_sort(begin, end, [](const ElfRel<E> &a, const ElfRel<E> &b) {
2021-03-17 14:57:09 +03:00
return std::tuple(a.r_sym, a.r_offset) <
std::tuple(b.r_sym, b.r_offset);
});
}
2021-03-29 14:29:57 +03:00
template <typename E>
void StrtabSection<E>::update_shdr(Context<E> &ctx) {
this->shdr.sh_size = 1;
for (ObjectFile<E> *file : ctx.objs) {
file->strtab_offset = this->shdr.sh_size;
this->shdr.sh_size += file->strtab_size;
2020-11-29 12:58:36 +03:00
}
}
2021-03-29 14:29:57 +03:00
template <typename E>
void ShstrtabSection<E>::update_shdr(Context<E> &ctx) {
this->shdr.sh_size = 1;
for (OutputChunk<E> *chunk : ctx.chunks) {
2020-11-17 06:20:56 +03:00
if (!chunk->name.empty()) {
2021-03-29 14:29:57 +03:00
chunk->shdr.sh_name = this->shdr.sh_size;
this->shdr.sh_size += chunk->name.size() + 1;
2020-11-17 06:20:56 +03:00
}
2020-11-18 15:45:49 +03:00
}
2020-11-17 06:20:56 +03:00
}
2021-03-29 14:29:57 +03:00
template <typename E>
void ShstrtabSection<E>::copy_buf(Context<E> &ctx) {
u8 *base = ctx.buf + this->shdr.sh_offset;
2020-11-17 06:20:56 +03:00
base[0] = '\0';
2021-01-24 06:01:43 +03:00
i64 i = 1;
2021-03-29 14:29:57 +03:00
for (OutputChunk<E> *chunk : ctx.chunks) {
2020-11-17 06:20:56 +03:00
if (!chunk->name.empty()) {
2020-11-17 07:19:54 +03:00
write_string(base + i, chunk->name);
i += chunk->name.size() + 1;
2020-11-17 06:20:56 +03:00
}
}
}
2021-03-29 14:29:57 +03:00
template <typename E>
i64 DynstrSection<E>::add_string(std::string_view str) {
2021-03-23 07:54:40 +03:00
if (strings.empty())
2021-03-29 14:29:57 +03:00
this->shdr.sh_size = 1;
2021-03-23 07:54:40 +03:00
2021-03-29 14:29:57 +03:00
auto [it, inserted] = strings.insert({str, this->shdr.sh_size});
2021-01-26 14:34:41 +03:00
if (inserted)
2021-03-29 14:29:57 +03:00
this->shdr.sh_size += str.size() + 1;
2021-01-26 14:34:41 +03:00
return it->second;
2020-11-17 07:19:54 +03:00
}
2021-03-29 14:29:57 +03:00
template <typename E>
i64 DynstrSection<E>::find_string(std::string_view str) {
2021-01-26 14:34:41 +03:00
auto it = strings.find(str);
assert(it != strings.end());
return it->second;
2020-11-30 11:52:08 +03:00
}
2021-03-29 14:29:57 +03:00
template <typename E>
void DynstrSection<E>::update_shdr(Context<E> &ctx) {
if (this->shdr.sh_size == 1)
this->shdr.sh_size = 0;
2021-03-23 07:54:40 +03:00
}
2021-03-29 14:29:57 +03:00
template <typename E>
void DynstrSection<E>::copy_buf(Context<E> &ctx) {
u8 *base = ctx.buf + this->shdr.sh_offset;
2020-11-17 07:19:54 +03:00
base[0] = '\0';
2021-03-15 08:04:26 +03:00
2021-01-26 14:34:41 +03:00
for (std::pair<std::string_view, i64> pair : strings)
write_string(base + pair.second, pair.first);
2021-03-15 08:04:26 +03:00
2021-03-29 07:20:51 +03:00
if (!ctx.dynsym->symbols.empty()) {
2021-03-23 07:54:40 +03:00
i64 offset = dynsym_offset;
2021-03-29 14:29:57 +03:00
for (Symbol<E> *sym :
std::span<Symbol<E> *>(ctx.dynsym->symbols).subspan(1)) {
2021-03-23 07:54:40 +03:00
write_string(base + offset, sym->name);
offset += sym->name.size() + 1;
}
2021-03-15 08:04:26 +03:00
}
2020-11-17 07:19:54 +03:00
}
2021-03-29 14:29:57 +03:00
template <typename E>
void SymtabSection<E>::update_shdr(Context<E> &ctx) {
this->shdr.sh_size = sizeof(ElfSym<E>);
2020-11-17 08:28:53 +03:00
2021-03-29 14:29:57 +03:00
for (ObjectFile<E> *file : ctx.objs) {
file->local_symtab_offset = this->shdr.sh_size;
this->shdr.sh_size += file->num_local_symtab * sizeof(ElfSym<E>);
2020-11-29 12:45:33 +03:00
}
2020-11-17 08:28:53 +03:00
2021-03-29 14:29:57 +03:00
for (ObjectFile<E> *file : ctx.objs) {
file->global_symtab_offset = this->shdr.sh_size;
this->shdr.sh_size += file->num_global_symtab * sizeof(ElfSym<E>);
2020-11-17 08:28:53 +03:00
}
2021-03-29 14:29:57 +03:00
this->shdr.sh_info = ctx.objs[0]->global_symtab_offset / sizeof(ElfSym<E>);
this->shdr.sh_link = ctx.strtab->shndx;
2021-01-09 18:49:07 +03:00
2021-03-29 14:29:57 +03:00
if (this->shdr.sh_size == sizeof(ElfSym<E>))
this->shdr.sh_size = 0;
2021-03-23 06:39:53 +03:00
2021-01-09 18:49:07 +03:00
static Counter counter("symtab");
2021-03-29 14:29:57 +03:00
counter += this->shdr.sh_size / sizeof(ElfSym<E>);
2020-11-17 07:32:22 +03:00
}
2021-03-29 14:29:57 +03:00
template <typename E>
void SymtabSection<E>::copy_buf(Context<E> &ctx) {
memset(ctx.buf + this->shdr.sh_offset, 0, sizeof(ElfSym<E>));
2021-03-29 07:20:51 +03:00
ctx.buf[ctx.strtab->shdr.sh_offset] = '\0';
2020-11-29 13:50:59 +03:00
2021-03-29 14:29:57 +03:00
tbb::parallel_for_each(ctx.objs, [&](ObjectFile<E> *file) {
2021-03-29 09:16:53 +03:00
file->write_symtab(ctx);
2021-03-13 07:23:32 +03:00
});
2020-11-17 07:51:44 +03:00
}
2021-03-29 14:29:57 +03:00
template <typename E>
static bool has_init_array(Context<E> &ctx) {
for (OutputChunk<E> *chunk : ctx.chunks)
if (chunk->shdr.sh_type == SHT_INIT_ARRAY)
return true;
return false;
}
2021-03-29 14:29:57 +03:00
template <typename E>
static bool has_fini_array(Context<E> &ctx) {
for (OutputChunk<E> *chunk : ctx.chunks)
if (chunk->shdr.sh_type == SHT_FINI_ARRAY)
return true;
return false;
}
2021-03-29 14:29:57 +03:00
template <typename E>
2021-03-30 18:11:39 +03:00
static std::vector<typename E::word> create_dynamic_section(Context<E> &ctx) {
std::vector<typename E::word> vec;
2020-11-18 13:27:29 +03:00
auto define = [&](u64 tag, u64 val) {
vec.push_back(tag);
vec.push_back(val);
};
2021-03-29 14:29:57 +03:00
for (SharedFile<E> *file : ctx.dsos)
2021-03-29 07:20:51 +03:00
define(DT_NEEDED, ctx.dynstr->find_string(file->soname));
2020-11-18 13:27:29 +03:00
2021-03-29 07:20:51 +03:00
if (!ctx.arg.rpaths.empty())
define(DT_RUNPATH, ctx.dynstr->find_string(ctx.arg.rpaths));
2021-02-25 14:46:43 +03:00
2021-03-29 07:20:51 +03:00
if (!ctx.arg.soname.empty())
define(DT_SONAME, ctx.dynstr->find_string(ctx.arg.soname));
2021-03-01 07:17:52 +03:00
2021-03-29 07:20:51 +03:00
for (std::string_view str : ctx.arg.auxiliary)
define(DT_AUXILIARY, ctx.dynstr->find_string(str));
2021-03-15 18:34:09 +03:00
2021-03-29 07:20:51 +03:00
for (std::string_view str : ctx.arg.filter)
define(DT_FILTER, ctx.dynstr->find_string(str));
2021-03-15 18:34:09 +03:00
2021-03-29 07:20:51 +03:00
if (ctx.reldyn->shdr.sh_size) {
2021-03-30 18:05:46 +03:00
bool is_rel = (E::rel_type == SHT_REL);
define(is_rel ? DT_REL : DT_RELA, ctx.reldyn->shdr.sh_addr);
define(is_rel ? DT_RELSZ : DT_RELASZ, ctx.reldyn->shdr.sh_size);
define(is_rel ? DT_RELENT : DT_RELAENT, sizeof(ElfRel<E>));
}
2021-03-29 07:20:51 +03:00
if (ctx.relplt->shdr.sh_size) {
define(DT_JMPREL, ctx.relplt->shdr.sh_addr);
define(DT_PLTRELSZ, ctx.relplt->shdr.sh_size);
2021-03-30 18:05:46 +03:00
define(DT_PLTREL, (E::rel_type == SHT_REL) ? DT_REL : DT_RELA);
}
2021-03-29 07:20:51 +03:00
if (ctx.gotplt->shdr.sh_size)
define(DT_PLTGOT, ctx.gotplt->shdr.sh_addr);
2021-03-29 07:20:51 +03:00
if (ctx.dynsym->shdr.sh_size) {
define(DT_SYMTAB, ctx.dynsym->shdr.sh_addr);
2021-03-29 14:29:57 +03:00
define(DT_SYMENT, sizeof(ElfSym<E>));
2021-03-23 06:39:53 +03:00
}
2021-03-29 07:20:51 +03:00
if (ctx.dynstr->shdr.sh_size) {
define(DT_STRTAB, ctx.dynstr->shdr.sh_addr);
define(DT_STRSZ, ctx.dynstr->shdr.sh_size);
2021-03-23 07:54:40 +03:00
}
2021-03-29 10:02:12 +03:00
if (has_init_array(ctx)) {
2021-03-29 07:20:51 +03:00
define(DT_INIT_ARRAY, ctx.__init_array_start->value);
define(DT_INIT_ARRAYSZ,
2021-03-29 07:20:51 +03:00
ctx.__init_array_end->value - ctx.__init_array_start->value);
}
2021-03-29 10:02:12 +03:00
if (has_fini_array(ctx)) {
2021-03-29 07:20:51 +03:00
define(DT_FINI_ARRAY, ctx.__fini_array_start->value);
define(DT_FINI_ARRAYSZ,
2021-03-29 07:20:51 +03:00
ctx.__fini_array_end->value - ctx.__fini_array_start->value);
}
2021-03-29 07:20:51 +03:00
if (ctx.versym->shdr.sh_size)
define(DT_VERSYM, ctx.versym->shdr.sh_addr);
2021-03-23 07:54:40 +03:00
2021-03-29 07:20:51 +03:00
if (ctx.verneed->shdr.sh_size) {
define(DT_VERNEED, ctx.verneed->shdr.sh_addr);
define(DT_VERNEEDNUM, ctx.verneed->shdr.sh_info);
2021-03-23 07:54:40 +03:00
}
2021-03-29 07:20:51 +03:00
if (ctx.verdef) {
define(DT_VERDEF, ctx.verdef->shdr.sh_addr);
define(DT_VERDEFNUM, ctx.verdef->shdr.sh_info);
2021-03-07 07:43:58 +03:00
}
2021-03-29 17:50:19 +03:00
if (Symbol<E> *sym = Symbol<E>::intern(ctx, ctx.arg.init); sym->file)
2021-03-29 10:17:33 +03:00
define(DT_INIT, sym->get_addr(ctx));
2021-03-29 17:50:19 +03:00
if (Symbol<E> *sym = Symbol<E>::intern(ctx, ctx.arg.fini); sym->file)
2021-03-29 10:17:33 +03:00
define(DT_FINI, sym->get_addr(ctx));
2021-03-01 06:49:09 +03:00
2021-03-29 07:20:51 +03:00
if (ctx.hash)
define(DT_HASH, ctx.hash->shdr.sh_addr);
if (ctx.gnu_hash)
define(DT_GNU_HASH, ctx.gnu_hash->shdr.sh_addr);
2021-01-23 09:01:49 +03:00
2021-01-24 06:01:43 +03:00
i64 flags = 0;
i64 flags1 = 0;
2020-12-17 16:29:43 +03:00
2021-03-29 07:20:51 +03:00
if (ctx.arg.pie)
2020-12-17 16:29:43 +03:00
flags1 |= DF_1_PIE;
2021-03-29 07:20:51 +03:00
if (ctx.arg.z_now) {
2020-12-17 16:29:43 +03:00
flags |= DF_BIND_NOW;
flags1 |= DF_1_NOW;
}
2021-03-29 07:20:51 +03:00
if (!ctx.arg.z_dlopen)
2021-03-18 07:16:52 +03:00
flags1 |= DF_1_NOOPEN;
2021-03-29 07:20:51 +03:00
if (!ctx.arg.z_delete)
2021-03-18 07:16:52 +03:00
flags1 |= DF_1_NODELETE;
2021-03-29 07:20:51 +03:00
if (ctx.has_gottpoff)
flags |= DF_STATIC_TLS;
2020-12-17 16:29:43 +03:00
if (flags)
define(DT_FLAGS, flags);
if (flags1)
define(DT_FLAGS_1, flags1);
2021-03-23 07:54:40 +03:00
define(DT_DEBUG, 0);
2020-11-18 13:27:29 +03:00
define(DT_NULL, 0);
return vec;
}
2021-03-29 14:29:57 +03:00
template <typename E>
void DynamicSection<E>::update_shdr(Context<E> &ctx) {
2021-03-29 07:20:51 +03:00
if (ctx.arg.is_static)
2021-03-23 07:54:40 +03:00
return;
2021-03-29 07:20:51 +03:00
if (!ctx.arg.shared && ctx.dsos.empty())
2021-03-23 07:54:40 +03:00
return;
2021-03-30 18:11:39 +03:00
this->shdr.sh_size = create_dynamic_section(ctx).size() * E::wordsize;
2021-03-29 14:29:57 +03:00
this->shdr.sh_link = ctx.dynstr->shndx;
2020-11-16 18:43:32 +03:00
}
2021-03-29 14:29:57 +03:00
template <typename E>
void DynamicSection<E>::copy_buf(Context<E> &ctx) {
2021-03-30 18:11:39 +03:00
std::vector<typename E::word> contents = create_dynamic_section(ctx);
2021-03-29 14:29:57 +03:00
assert(this->shdr.sh_size == contents.size() * sizeof(contents[0]));
write_vector(ctx.buf + this->shdr.sh_offset, contents);
2020-11-16 18:33:41 +03:00
}
2020-12-10 07:44:58 +03:00
static std::string_view get_output_name(std::string_view name) {
2021-03-22 10:46:40 +03:00
static std::string_view prefixes[] = {
2020-10-22 10:35:17 +03:00
".text.", ".data.rel.ro.", ".data.", ".rodata.", ".bss.rel.ro.",
".bss.", ".init_array.", ".fini_array.", ".tbss.", ".tdata.",
};
2021-03-22 10:46:40 +03:00
for (std::string_view prefix : prefixes)
if (name.starts_with(prefix))
return prefix.substr(0, prefix.size() - 1);
if (name == ".zdebug_info")
return ".debug_info";
if (name == ".zdebug_aranges")
return ".debug_aranges";
if (name == ".zdebug_str")
return ".debug_str";
2020-10-22 10:35:17 +03:00
return name;
}
2021-03-29 14:29:57 +03:00
template <typename E>
OutputSection<E>::OutputSection(std::string_view name, u32 type, u64 flags)
: OutputChunk<E>(OutputChunk<E>::REGULAR) {
2021-03-14 10:31:47 +03:00
this->name = name;
2021-03-29 14:29:57 +03:00
this->shdr.sh_type = type;
this->shdr.sh_flags = flags;
2021-03-14 10:31:47 +03:00
idx = instances.size();
instances.push_back(this);
}
2021-03-29 14:29:57 +03:00
template <typename E>
OutputSection<E> *
OutputSection<E>::get_instance(std::string_view name, u64 type, u64 flags) {
2021-01-17 10:03:01 +03:00
if (name == ".eh_frame" && type == SHT_X86_64_UNWIND)
type = SHT_PROGBITS;
2020-12-17 15:27:58 +03:00
2020-10-27 06:45:59 +03:00
name = get_output_name(name);
2021-03-22 10:46:40 +03:00
flags = flags & ~(u64)SHF_GROUP & ~(u64)SHF_COMPRESSED;
2020-10-22 10:35:17 +03:00
2021-03-29 14:29:57 +03:00
auto find = [&]() -> OutputSection<E> * {
for (OutputSection<E> *osec : OutputSection::instances)
2020-11-13 06:43:59 +03:00
if (name == osec->name && type == osec->shdr.sh_type &&
2021-03-22 10:46:40 +03:00
flags == osec->shdr.sh_flags)
2020-10-22 10:35:17 +03:00
return osec;
return nullptr;
};
static std::shared_mutex mu;
2021-03-12 07:17:45 +03:00
// Search for an exiting output section.
2020-11-07 04:13:19 +03:00
{
std::shared_lock lock(mu);
2021-03-29 14:29:57 +03:00
if (OutputSection<E> *osec = find())
2020-11-07 04:13:19 +03:00
return osec;
}
2020-10-22 10:35:17 +03:00
// Create a new output section.
2020-11-07 04:13:19 +03:00
std::unique_lock lock(mu);
2021-03-29 14:29:57 +03:00
if (OutputSection<E> *osec = find())
2020-10-22 10:35:17 +03:00
return osec;
2020-11-13 06:43:59 +03:00
return new OutputSection(name, type, flags);
2020-10-22 10:35:17 +03:00
}
2020-11-07 04:13:19 +03:00
2021-03-29 14:29:57 +03:00
template <typename E>
void OutputSection<E>::copy_buf(Context<E> &ctx) {
if (this->shdr.sh_type == SHT_NOBITS)
2020-11-09 16:25:17 +03:00
return;
2021-03-15 18:01:10 +03:00
tbb::parallel_for((i64)0, (i64)members.size(), [&](i64 i) {
2021-01-20 10:28:18 +03:00
// Copy section contents to an output file
2021-03-29 14:29:57 +03:00
InputSection<E> &isec = *members[i];
2021-03-29 09:53:00 +03:00
isec.copy_buf(ctx);
2021-01-20 10:28:18 +03:00
// Zero-clear trailing padding
2021-03-11 13:59:01 +03:00
u64 this_end = isec.offset + isec.shdr.sh_size;
2021-01-20 10:28:18 +03:00
u64 next_start = (i == members.size() - 1) ?
2021-03-29 14:29:57 +03:00
this->shdr.sh_size : members[i + 1]->offset;
memset(ctx.buf + this->shdr.sh_offset + this_end, 0, next_start - this_end);
2020-11-09 16:25:17 +03:00
});
2020-11-09 15:32:13 +03:00
}
2021-03-29 14:29:57 +03:00
template <typename E>
void GotSection<E>::add_got_symbol(Context<E> &ctx, Symbol<E> *sym) {
2020-11-21 06:49:28 +03:00
assert(sym->got_idx == -1);
2021-03-29 14:29:57 +03:00
sym->got_idx = this->shdr.sh_size / GOT_SIZE;
this->shdr.sh_size += GOT_SIZE;
2020-11-18 14:29:24 +03:00
got_syms.push_back(sym);
2021-03-11 15:40:02 +03:00
if (sym->is_imported)
2021-03-29 10:02:12 +03:00
ctx.dynsym->add_symbol(ctx, sym);
2020-11-18 14:29:24 +03:00
}
2021-03-29 14:29:57 +03:00
template <typename E>
void GotSection<E>::add_gottpoff_symbol(Context<E> &ctx, Symbol<E> *sym) {
2020-11-21 06:49:28 +03:00
assert(sym->gottpoff_idx == -1);
2021-03-29 14:29:57 +03:00
sym->gottpoff_idx = this->shdr.sh_size / GOT_SIZE;
this->shdr.sh_size += GOT_SIZE;
2020-11-21 04:48:51 +03:00
gottpoff_syms.push_back(sym);
2021-03-27 08:47:30 +03:00
if (sym->is_imported)
2021-03-29 10:02:12 +03:00
ctx.dynsym->add_symbol(ctx, sym);
2020-11-18 14:29:24 +03:00
}
2021-03-29 14:29:57 +03:00
template <typename E>
void GotSection<E>::add_tlsgd_symbol(Context<E> &ctx, Symbol<E> *sym) {
2020-11-21 06:49:28 +03:00
assert(sym->tlsgd_idx == -1);
2021-03-29 14:29:57 +03:00
sym->tlsgd_idx = this->shdr.sh_size / GOT_SIZE;
this->shdr.sh_size += GOT_SIZE * 2;
2020-11-21 04:48:23 +03:00
tlsgd_syms.push_back(sym);
2021-03-29 10:02:12 +03:00
ctx.dynsym->add_symbol(ctx, sym);
2020-11-21 04:48:23 +03:00
}
2021-03-29 14:29:57 +03:00
template <typename E>
void GotSection<E>::add_tlsdesc_symbol(Context<E> &ctx, Symbol<E> *sym) {
2021-03-18 08:49:57 +03:00
assert(sym->tlsdesc_idx == -1);
2021-03-29 14:29:57 +03:00
sym->tlsdesc_idx = this->shdr.sh_size / GOT_SIZE;
this->shdr.sh_size += GOT_SIZE * 2;
2021-03-18 08:49:57 +03:00
tlsdesc_syms.push_back(sym);
2021-03-29 10:02:12 +03:00
ctx.dynsym->add_symbol(ctx, sym);
2021-03-18 08:49:57 +03:00
}
2021-03-29 14:29:57 +03:00
template <typename E>
void GotSection<E>::add_tlsld(Context<E> &ctx) {
2021-01-14 13:58:21 +03:00
if (tlsld_idx != -1)
return;
2021-03-29 14:29:57 +03:00
tlsld_idx = this->shdr.sh_size / GOT_SIZE;
this->shdr.sh_size += GOT_SIZE * 2;
2020-11-21 04:48:23 +03:00
}
2021-03-29 14:29:57 +03:00
template <typename E>
i64 GotSection<E>::get_reldyn_size(Context<E> &ctx) const {
2021-03-16 09:38:17 +03:00
i64 n = 0;
2021-03-29 14:29:57 +03:00
for (Symbol<E> *sym : got_syms)
2021-03-29 10:48:23 +03:00
if (sym->is_imported || (ctx.arg.pic && sym->is_relative(ctx)))
2021-03-16 09:38:17 +03:00
n++;
n += tlsgd_syms.size() * 2;
2021-03-18 08:49:57 +03:00
n += tlsdesc_syms.size() * 2;
2021-03-16 09:38:17 +03:00
2021-03-29 14:29:57 +03:00
for (Symbol<E> *sym : gottpoff_syms)
2021-03-16 09:38:17 +03:00
if (sym->is_imported)
n++;
if (tlsld_idx != -1)
n++;
2021-03-29 07:20:51 +03:00
n += ctx.dynbss->symbols.size();
n += ctx.dynbss_relro->symbols.size();
2021-03-16 09:38:17 +03:00
2021-03-30 09:42:11 +03:00
return n * sizeof(ElfRel<E>);
2021-03-16 09:38:17 +03:00
}
2021-03-30 13:27:00 +03:00
template <typename E>
static ElfRel<E> reloc(u64 offset, u32 type, u32 sym, i64 addend);
template <>
ElfRel<X86_64> reloc<X86_64>(u64 offset, u32 type, u32 sym, i64 addend) {
return {offset, type, sym, addend};
}
template <>
ElfRel<I386> reloc<I386>(u64 offset, u32 type, u32 sym, i64 addend) {
return {(u32)offset, type, sym};
}
2021-03-16 09:38:17 +03:00
// Fill .got and .rel.dyn.
2021-03-29 14:29:57 +03:00
template <typename E>
void GotSection<E>::copy_buf(Context<E> &ctx) {
u64 *buf = (u64 *)(ctx.buf + this->shdr.sh_offset);
memset(buf, 0, this->shdr.sh_size);
2020-11-18 14:29:24 +03:00
2021-03-30 09:42:11 +03:00
ElfRel<E> *rel = (ElfRel<E> *)(ctx.buf + ctx.reldyn->shdr.sh_offset);
2021-03-16 09:38:17 +03:00
2021-03-29 14:29:57 +03:00
for (Symbol<E> *sym : got_syms) {
2021-03-29 10:17:33 +03:00
u64 addr = sym->get_got_addr(ctx);
2021-03-16 09:38:17 +03:00
if (sym->is_imported) {
2021-03-30 13:27:00 +03:00
*rel++ = reloc<E>(addr, R_X86_64_GLOB_DAT, sym->dynsym_idx, 0);
2021-03-16 09:38:17 +03:00
} else {
2021-03-29 10:17:33 +03:00
buf[sym->got_idx] = sym->get_addr(ctx);
2021-03-29 10:48:23 +03:00
if (ctx.arg.pic && sym->is_relative(ctx))
2021-03-30 13:27:00 +03:00
*rel++ = reloc<E>(addr, R_X86_64_RELATIVE, 0, (i64)sym->get_addr(ctx));
2021-03-16 09:38:17 +03:00
}
}
2020-11-18 14:29:24 +03:00
2021-03-29 14:29:57 +03:00
for (Symbol<E> *sym : tlsgd_syms) {
2021-03-29 10:17:33 +03:00
u64 addr = sym->get_tlsgd_addr(ctx);
2021-03-30 13:27:00 +03:00
*rel++ = reloc<E>(addr, R_X86_64_DTPMOD64, sym->dynsym_idx, 0);
*rel++ = reloc<E>(addr + GOT_SIZE, R_X86_64_DTPOFF64, sym->dynsym_idx, 0);
2021-03-16 09:38:17 +03:00
}
2021-03-29 14:29:57 +03:00
for (Symbol<E> *sym : tlsdesc_syms)
2021-03-30 13:27:00 +03:00
*rel++ = reloc<E>(sym->get_tlsdesc_addr(ctx), R_X86_64_TLSDESC,
2021-03-30 15:13:04 +03:00
sym->dynsym_idx, 0);
2021-03-18 08:49:57 +03:00
2021-03-29 14:29:57 +03:00
for (Symbol<E> *sym : gottpoff_syms) {
2021-03-16 09:38:17 +03:00
if (sym->is_imported)
2021-03-30 13:27:00 +03:00
*rel++ = reloc<E>(sym->get_gottpoff_addr(ctx), R_X86_64_TPOFF64,
2021-03-30 15:13:04 +03:00
sym->dynsym_idx, 0);
2021-03-16 09:38:17 +03:00
else
2021-03-29 10:17:33 +03:00
buf[sym->gottpoff_idx] = sym->get_addr(ctx) - ctx.tls_end;
2021-03-16 09:38:17 +03:00
}
if (tlsld_idx != -1)
2021-03-30 13:27:00 +03:00
*rel++ = reloc<E>(get_tlsld_addr(ctx), R_X86_64_DTPMOD64, 0, 0);
2021-03-16 09:38:17 +03:00
2021-03-29 14:29:57 +03:00
for (Symbol<E> *sym : ctx.dynbss->symbols)
2021-03-30 13:27:00 +03:00
*rel++ = reloc<E>(sym->get_addr(ctx), R_X86_64_COPY, sym->dynsym_idx, 0);
2021-03-16 09:38:17 +03:00
2021-03-29 14:29:57 +03:00
for (Symbol<E> *sym : ctx.dynbss_relro->symbols)
2021-03-30 13:27:00 +03:00
*rel++ = reloc<E>(sym->get_addr(ctx), R_X86_64_COPY, sym->dynsym_idx, 0);
2020-11-18 14:29:24 +03:00
}
2021-03-29 14:29:57 +03:00
template <typename E>
void GotPltSection<E>::copy_buf(Context<E> &ctx) {
u64 *buf = (u64 *)(ctx.buf + this->shdr.sh_offset);
2020-11-18 15:45:49 +03:00
2021-03-17 19:31:16 +03:00
// The first slot of .got.plt points to _DYNAMIC, as requested by
// the x86-64 psABI. The second and the third slots are reserved by
// the psABI.
2021-03-29 07:20:51 +03:00
buf[0] = ctx.dynamic ? ctx.dynamic->shdr.sh_addr : 0;
2020-11-18 15:45:49 +03:00
buf[1] = 0;
buf[2] = 0;
2021-03-29 14:29:57 +03:00
for (Symbol<E> *sym : ctx.plt->symbols)
2020-11-20 03:46:49 +03:00
if (sym->gotplt_idx != -1)
2021-03-29 10:17:33 +03:00
buf[sym->gotplt_idx] = sym->get_plt_addr(ctx) + 6;
2020-11-18 15:45:49 +03:00
}
2021-03-29 14:29:57 +03:00
template <typename E>
void PltSection<E>::add_symbol(Context<E> &ctx, Symbol<E> *sym) {
2020-11-21 06:49:28 +03:00
assert(sym->plt_idx == -1);
2021-02-26 14:56:01 +03:00
assert(sym->got_idx == -1);
2021-03-29 14:29:57 +03:00
if (this->shdr.sh_size == 0) {
this->shdr.sh_size = PLT_SIZE;
2021-03-29 07:20:51 +03:00
ctx.gotplt->shdr.sh_size = GOT_SIZE * 3;
}
2021-03-29 14:29:57 +03:00
sym->plt_idx = this->shdr.sh_size / PLT_SIZE;
this->shdr.sh_size += PLT_SIZE;
2020-11-18 15:45:49 +03:00
symbols.push_back(sym);
2021-03-29 07:20:51 +03:00
sym->gotplt_idx = ctx.gotplt->shdr.sh_size / GOT_SIZE;
ctx.gotplt->shdr.sh_size += GOT_SIZE;
2021-03-30 09:42:11 +03:00
ctx.relplt->shdr.sh_size += sizeof(ElfRel<E>);
2021-03-29 10:02:12 +03:00
ctx.dynsym->add_symbol(ctx, sym);
2020-11-13 07:39:29 +03:00
}
2021-03-29 14:29:57 +03:00
template <typename E>
void PltSection<E>::copy_buf(Context<E> &ctx) {
u8 *buf = ctx.buf + this->shdr.sh_offset;
2020-11-18 15:45:49 +03:00
2021-02-26 14:56:01 +03:00
static const u8 plt0[] = {
2020-11-13 06:30:27 +03:00
0xff, 0x35, 0, 0, 0, 0, // pushq GOTPLT+8(%rip)
0xff, 0x25, 0, 0, 0, 0, // jmp *GOTPLT+16(%rip)
0x0f, 0x1f, 0x40, 0x00, // nop
};
2020-11-18 15:45:49 +03:00
memcpy(buf, plt0, sizeof(plt0));
2021-03-29 14:29:57 +03:00
*(u32 *)(buf + 2) = ctx.gotplt->shdr.sh_addr - this->shdr.sh_addr + 2;
*(u32 *)(buf + 8) = ctx.gotplt->shdr.sh_addr - this->shdr.sh_addr + 4;
2020-11-18 15:45:49 +03:00
2021-01-24 06:01:43 +03:00
i64 relplt_idx = 0;
2021-01-14 14:27:32 +03:00
2021-02-26 14:56:01 +03:00
static const u8 data[] = {
0xff, 0x25, 0, 0, 0, 0, // jmp *foo@GOTPLT
0x68, 0, 0, 0, 0, // push $index_in_relplt
0xe9, 0, 0, 0, 0, // jmp PLT[0]
};
2021-03-29 14:29:57 +03:00
for (Symbol<E> *sym : symbols) {
2020-11-18 15:45:49 +03:00
u8 *ent = buf + sym->plt_idx * PLT_SIZE;
2021-02-26 14:56:01 +03:00
memcpy(ent, data, sizeof(data));
2021-03-29 10:17:33 +03:00
*(u32 *)(ent + 2) = sym->get_gotplt_addr(ctx) - sym->get_plt_addr(ctx) - 6;
2021-02-26 14:56:01 +03:00
*(u32 *)(ent + 7) = relplt_idx++;
2021-03-29 14:29:57 +03:00
*(u32 *)(ent + 12) = this->shdr.sh_addr - sym->get_plt_addr(ctx) - 16;
2021-02-26 14:56:01 +03:00
}
}
2020-11-18 15:45:49 +03:00
2021-03-29 14:29:57 +03:00
template <typename E>
void PltGotSection<E>::add_symbol(Context<E> &ctx, Symbol<E> *sym) {
2021-02-26 14:56:01 +03:00
assert(sym->plt_idx == -1);
assert(sym->got_idx != -1);
2020-11-18 15:45:49 +03:00
2021-03-29 14:29:57 +03:00
sym->plt_idx = this->shdr.sh_size / PLT_GOT_SIZE;
this->shdr.sh_size += PLT_GOT_SIZE;
2021-02-26 14:56:01 +03:00
symbols.push_back(sym);
}
2021-03-29 14:29:57 +03:00
template <typename E>
void PltGotSection<E>::copy_buf(Context<E> &ctx) {
u8 *buf = ctx.buf + this->shdr.sh_offset;
2021-02-26 14:56:01 +03:00
static const u8 data[] = {
0xff, 0x25, 0, 0, 0, 0, // jmp *foo@GOT
0x66, 0x90, // nop
};
2021-03-29 14:29:57 +03:00
for (Symbol<E> *sym : symbols) {
2021-02-26 14:56:01 +03:00
u8 *ent = buf + sym->plt_idx * PLT_GOT_SIZE;
memcpy(ent, data, sizeof(data));
2021-03-29 10:17:33 +03:00
*(u32 *)(ent + 2) = sym->get_got_addr(ctx) - sym->get_plt_addr(ctx) - 6;
2020-11-18 15:45:49 +03:00
}
2020-11-13 06:30:27 +03:00
}
2021-03-29 14:29:57 +03:00
template <typename E>
void RelPltSection<E>::update_shdr(Context<E> &ctx) {
this->shdr.sh_link = ctx.dynsym->shndx;
2020-11-13 10:16:40 +03:00
}
2021-03-30 15:13:04 +03:00
template <typename E>
void RelPltSection<E>::copy_buf(Context<E> &ctx) {
ElfRel<E> *buf = (ElfRel<E> *)(ctx.buf + this->shdr.sh_offset);
2021-03-29 14:29:57 +03:00
memset(buf, 0, this->shdr.sh_size);
2020-11-18 15:45:49 +03:00
2021-01-24 06:01:43 +03:00
i64 relplt_idx = 0;
2021-01-14 14:27:32 +03:00
2021-03-30 15:13:04 +03:00
for (Symbol<E> *sym : ctx.plt->symbols) {
u64 r_offset = sym->get_gotplt_addr(ctx);
u64 r_sym = sym->dynsym_idx;
u32 r_type = 0;
u32 r_addend = 0;
2020-11-18 09:35:42 +03:00
2021-03-05 04:35:08 +03:00
if (sym->get_type() == STT_GNU_IFUNC) {
2021-03-30 18:29:11 +03:00
r_type = E::R_IRELATIVE;
2021-03-30 15:13:04 +03:00
r_addend = sym->input_section->get_addr() + sym->value;
2020-11-18 15:45:49 +03:00
} else {
2021-03-30 18:29:11 +03:00
r_type = E::R_JUMP_SLOT;
2020-11-18 15:45:49 +03:00
}
2021-03-30 13:27:00 +03:00
2021-03-30 15:13:04 +03:00
ElfRel<E> *rel = buf + relplt_idx++;
*rel = reloc<E>(r_offset, r_type, r_sym, r_addend);
2021-03-30 13:27:00 +03:00
}
}
2021-03-29 14:29:57 +03:00
template <typename E>
void DynsymSection<E>::add_symbol(Context<E> &ctx, Symbol<E> *sym) {
2021-03-23 07:54:40 +03:00
if (symbols.empty())
symbols.push_back({});
2021-03-23 06:39:53 +03:00
2020-12-13 15:57:45 +03:00
if (sym->dynsym_idx != -1)
return;
2021-01-14 11:23:14 +03:00
sym->dynsym_idx = -2;
2020-12-13 15:57:45 +03:00
symbols.push_back(sym);
2020-11-16 17:48:20 +03:00
}
2021-03-29 14:29:57 +03:00
template <typename E>
void DynsymSection<E>::sort_symbols(Context<E> &ctx) {
2021-03-14 15:09:47 +03:00
Timer t("sort_dynsyms");
2021-03-15 05:58:42 +03:00
struct T {
2021-03-29 14:29:57 +03:00
Symbol<E> *sym;
2021-03-15 05:58:42 +03:00
i32 idx;
u32 hash;
bool is_local() const {
return sym->esym->st_bind == STB_LOCAL;
}
};
std::vector<T> vec(symbols.size());
for (i32 i = 1; i < symbols.size(); i++)
vec[i] = {symbols[i], i, 0};
2021-03-14 15:34:35 +03:00
2021-01-23 09:01:49 +03:00
// In any ELF file, local symbols should precede global symbols.
2021-03-15 05:58:42 +03:00
tbb::parallel_sort(vec.begin() + 1, vec.end(), [](const T &a, const T &b) {
2021-03-17 08:39:49 +03:00
return std::tuple(a.is_local(), a.idx) < std::tuple(b.is_local(), b.idx);
2021-03-14 15:34:35 +03:00
});
2021-03-15 05:58:42 +03:00
auto first_global = std::partition_point(vec.begin() + 1, vec.end(),
[](const T &x) {
return x.is_local();
});
2021-01-14 11:23:14 +03:00
2021-01-23 09:01:49 +03:00
// In any ELF file, the index of the first global symbols can be
// found in the symtab's sh_info field.
2021-03-29 14:29:57 +03:00
this->shdr.sh_info = first_global - vec.begin();
2021-01-23 09:01:49 +03:00
// If we have .gnu.hash section, it imposes more constraints
// on the order of symbols.
2021-03-29 07:20:51 +03:00
if (ctx.gnu_hash) {
2021-03-15 05:58:42 +03:00
i64 num_globals = vec.end() - first_global;
2021-03-29 07:20:51 +03:00
ctx.gnu_hash->num_buckets = num_globals / ctx.gnu_hash->LOAD_FACTOR + 1;
ctx.gnu_hash->symoffset = first_global - vec.begin();
2021-03-15 05:58:42 +03:00
2021-03-29 10:02:12 +03:00
tbb::parallel_for_each(first_global, vec.end(), [&](T &x) {
2021-03-29 07:20:51 +03:00
x.hash = gnu_hash(x.sym->name) % ctx.gnu_hash->num_buckets;
2021-03-15 05:58:42 +03:00
});
tbb::parallel_sort(first_global, vec.end(), [&](const T &a, const T &b) {
2021-03-17 08:39:49 +03:00
return std::tuple(a.hash, a.idx) < std::tuple(b.hash, b.idx);
2021-01-23 09:01:49 +03:00
});
}
2021-01-14 11:23:14 +03:00
2021-03-29 07:20:51 +03:00
ctx.dynstr->dynsym_offset = ctx.dynstr->shdr.sh_size;
2021-03-15 08:04:26 +03:00
2021-01-24 06:01:43 +03:00
for (i64 i = 1; i < symbols.size(); i++) {
2021-03-15 05:58:42 +03:00
symbols[i] = vec[i].sym;
2021-01-23 09:01:49 +03:00
symbols[i]->dynsym_idx = i;
2021-03-29 07:20:51 +03:00
ctx.dynstr->shdr.sh_size += symbols[i]->name.size() + 1;
2021-01-23 09:01:49 +03:00
}
2021-01-14 11:23:14 +03:00
}
2021-03-29 14:29:57 +03:00
template <typename E>
void DynsymSection<E>::update_shdr(Context<E> &ctx) {
this->shdr.sh_link = ctx.dynstr->shndx;
this->shdr.sh_size = sizeof(ElfSym<E>) * symbols.size();
2020-11-16 19:30:24 +03:00
}
2021-03-29 14:29:57 +03:00
template <typename E>
void DynsymSection<E>::copy_buf(Context<E> &ctx) {
u8 *base = ctx.buf + this->shdr.sh_offset;
memset(base, 0, sizeof(ElfSym<E>));
2021-03-29 07:20:51 +03:00
i64 name_offset = ctx.dynstr->dynsym_offset;
2020-11-16 17:48:20 +03:00
2021-01-24 06:01:43 +03:00
for (i64 i = 1; i < symbols.size(); i++) {
2021-03-29 14:29:57 +03:00
Symbol<E> &sym = *symbols[i];
2021-01-14 14:20:23 +03:00
2021-03-29 14:29:57 +03:00
ElfSym<E> &esym = *(ElfSym<E> *)(base + sym.dynsym_idx * sizeof(ElfSym<E>));
2020-11-16 17:48:20 +03:00
memset(&esym, 0, sizeof(esym));
2021-03-14 09:09:12 +03:00
esym.st_type = sym.get_type();
2021-01-14 14:20:23 +03:00
esym.st_size = sym.esym->st_size;
2020-11-16 17:48:20 +03:00
2021-03-24 19:48:12 +03:00
if (sym.is_weak)
esym.st_bind = STB_WEAK;
else if (sym.file->is_dso)
esym.st_bind = STB_GLOBAL;
else
esym.st_bind = sym.esym->st_bind;
2021-03-15 08:04:26 +03:00
esym.st_name = name_offset;
name_offset += sym.name.size() + 1;
2021-01-14 14:31:39 +03:00
if (sym.has_copyrel) {
2021-03-12 08:20:10 +03:00
esym.st_shndx = sym.copyrel_readonly
2021-03-29 07:20:51 +03:00
? ctx.dynbss_relro->shndx : ctx.dynbss->shndx;
2021-03-29 10:17:33 +03:00
esym.st_value = sym.get_addr(ctx);
} else if (sym.file->is_dso || sym.esym->is_undef()) {
2020-12-12 06:58:55 +03:00
esym.st_shndx = SHN_UNDEF;
2021-02-22 08:43:13 +03:00
esym.st_size = 0;
2021-03-29 07:20:51 +03:00
if (!ctx.arg.shared && sym.plt_idx != -1 && sym.got_idx == -1) {
2021-03-01 17:33:53 +03:00
// Emit an address for a canonical PLT
2021-03-29 10:17:33 +03:00
esym.st_value = sym.get_plt_addr(ctx);
2021-03-01 17:33:53 +03:00
}
2021-01-14 14:20:23 +03:00
} else if (!sym.input_section) {
2020-12-12 06:58:55 +03:00
esym.st_shndx = SHN_ABS;
2021-03-29 10:17:33 +03:00
esym.st_value = sym.get_addr(ctx);
2021-03-05 04:35:08 +03:00
} else if (sym.get_type() == STT_TLS) {
2021-01-14 14:20:23 +03:00
esym.st_shndx = sym.input_section->output_section->shndx;
2021-03-29 10:17:33 +03:00
esym.st_value = sym.get_addr(ctx) - ctx.tls_begin;
2020-11-16 17:48:20 +03:00
} else {
2021-01-14 14:20:23 +03:00
esym.st_shndx = sym.input_section->output_section->shndx;
2021-03-29 10:17:33 +03:00
esym.st_value = sym.get_addr(ctx);
2020-11-16 17:48:20 +03:00
}
2021-01-10 09:38:10 +03:00
}
2020-11-16 17:58:40 +03:00
}
2021-03-29 14:29:57 +03:00
template <typename E>
void HashSection<E>::update_shdr(Context<E> &ctx) {
2021-03-29 07:20:51 +03:00
if (ctx.dynsym->symbols.empty())
2021-03-23 07:54:40 +03:00
return;
2021-01-24 06:01:43 +03:00
i64 header_size = 8;
2021-03-29 07:20:51 +03:00
i64 num_slots = ctx.dynsym->symbols.size();
2021-03-29 14:29:57 +03:00
this->shdr.sh_size = header_size + num_slots * 8;
this->shdr.sh_link = ctx.dynsym->shndx;
2020-11-16 17:58:40 +03:00
}
2021-03-29 14:29:57 +03:00
template <typename E>
void HashSection<E>::copy_buf(Context<E> &ctx) {
u8 *base = ctx.buf + this->shdr.sh_offset;
memset(base, 0, this->shdr.sh_size);
2020-11-16 17:58:40 +03:00
2021-03-29 07:20:51 +03:00
i64 num_slots = ctx.dynsym->symbols.size();
2020-11-16 18:08:53 +03:00
u32 *hdr = (u32 *)base;
2020-11-16 17:58:40 +03:00
u32 *buckets = (u32 *)(base + 8);
u32 *chains = buckets + num_slots;
hdr[0] = hdr[1] = num_slots;
2020-11-16 17:48:20 +03:00
2021-03-29 07:20:51 +03:00
for (i64 i = 1; i < ctx.dynsym->symbols.size(); i++) {
2021-03-29 14:29:57 +03:00
Symbol<E> *sym = ctx.dynsym->symbols[i];
2021-01-24 06:01:43 +03:00
i64 idx = elf_hash(sym->name) % num_slots;
2021-01-23 09:01:49 +03:00
chains[sym->dynsym_idx] = buckets[idx];
buckets[idx] = sym->dynsym_idx;
}
}
2021-03-29 14:29:57 +03:00
template <typename E>
void GnuHashSection<E>::update_shdr(Context<E> &ctx) {
2021-03-29 07:20:51 +03:00
if (ctx.dynsym->symbols.empty())
2021-03-23 07:54:40 +03:00
return;
2021-03-29 14:29:57 +03:00
this->shdr.sh_link = ctx.dynsym->shndx;
2021-01-23 09:01:49 +03:00
2021-03-29 07:20:51 +03:00
if (i64 num_symbols = ctx.dynsym->symbols.size() - symoffset) {
2021-01-23 09:01:49 +03:00
// We allocate 12 bits for each symbol in the bloom filter.
2021-01-24 06:01:43 +03:00
i64 num_bits = num_symbols * 12;
2021-01-23 18:03:35 +03:00
num_bloom = next_power_of_two(num_bits / ELFCLASS_BITS);
2021-01-23 09:01:49 +03:00
}
2021-03-29 07:20:51 +03:00
i64 num_symbols = ctx.dynsym->symbols.size() - symoffset;
2021-01-23 09:01:49 +03:00
2021-03-29 14:29:57 +03:00
this->shdr.sh_size = HEADER_SIZE; // Header
this->shdr.sh_size += num_bloom * ELFCLASS_BITS / 8; // Bloom filter
this->shdr.sh_size += num_buckets * 4; // Hash buckets
this->shdr.sh_size += num_symbols * 4; // Hash values
2021-01-23 09:01:49 +03:00
}
2021-03-29 14:29:57 +03:00
template <typename E>
void GnuHashSection<E>::copy_buf(Context<E> &ctx) {
u8 *base = ctx.buf + this->shdr.sh_offset;
memset(base, 0, this->shdr.sh_size);
2021-01-23 09:01:49 +03:00
2021-01-23 18:03:35 +03:00
*(u32 *)base = num_buckets;
2021-01-23 09:01:49 +03:00
*(u32 *)(base + 4) = symoffset;
2021-01-23 18:03:35 +03:00
*(u32 *)(base + 8) = num_bloom;
2021-01-23 09:01:49 +03:00
*(u32 *)(base + 12) = BLOOM_SHIFT;
2021-03-29 14:29:57 +03:00
std::span<Symbol<E> *> symbols =
std::span<Symbol<E> *>(ctx.dynsym->symbols).subspan(symoffset);
2021-03-12 07:17:45 +03:00
2021-01-23 09:01:49 +03:00
std::vector<u32> hashes(symbols.size());
2021-01-24 06:01:43 +03:00
for (i64 i = 0; i < symbols.size(); i++)
2021-01-23 09:01:49 +03:00
hashes[i] = gnu_hash(symbols[i]->name);
// Write a bloom filter
u64 *bloom = (u64 *)(base + HEADER_SIZE);
2021-01-24 06:01:43 +03:00
for (i64 hash : hashes) {
i64 idx = (hash / 64) % num_bloom;
2021-01-23 18:03:35 +03:00
bloom[idx] |= (u64)1 << (hash % ELFCLASS_BITS);
bloom[idx] |= (u64)1 << ((hash >> BLOOM_SHIFT) % ELFCLASS_BITS);
2021-01-23 09:01:49 +03:00
}
// Write hash bucket indices
2021-01-23 18:03:35 +03:00
u32 *buckets = (u32 *)(bloom + num_bloom);
2021-03-08 16:42:59 +03:00
for (i64 i = 0; i < hashes.size(); i++) {
2021-01-24 06:01:43 +03:00
i64 idx = hashes[i] % num_buckets;
2021-01-23 09:01:49 +03:00
if (!buckets[idx])
buckets[idx] = i + symoffset;
}
// Write a hash table
2021-01-23 18:03:35 +03:00
u32 *table = buckets + num_buckets;
2021-01-24 06:01:43 +03:00
for (i64 i = 0; i < symbols.size(); i++) {
2021-01-23 18:03:35 +03:00
bool is_last = false;
2021-01-23 09:01:49 +03:00
if (i == symbols.size() - 1 ||
2021-01-23 18:03:35 +03:00
(hashes[i] % num_buckets) != (hashes[i + 1] % num_buckets))
2021-01-23 09:01:49 +03:00
is_last = true;
if (is_last)
table[i] = hashes[i] | 1;
else
2021-03-12 07:17:45 +03:00
table[i] = hashes[i] & ~1;
2020-11-16 17:58:40 +03:00
}
2020-11-16 17:48:20 +03:00
}
2021-03-29 14:29:57 +03:00
template <typename E>
MergedSection<E> *
MergedSection<E>::get_instance(std::string_view name, u64 type, u64 flags) {
2020-11-07 04:13:19 +03:00
name = get_output_name(name);
flags = flags & ~(u64)SHF_MERGE & ~(u64)SHF_STRINGS;
2020-11-07 15:53:21 +03:00
auto find = [&]() -> MergedSection * {
for (MergedSection *osec : MergedSection::instances)
2021-01-22 11:13:52 +03:00
if (std::tuple(name, flags, type) ==
std::tuple(osec->name, osec->shdr.sh_flags, osec->shdr.sh_type))
2020-11-07 04:13:19 +03:00
return osec;
return nullptr;
};
// Search for an exiting output section.
static std::shared_mutex mu;
{
std::shared_lock lock(mu);
2020-11-07 15:53:21 +03:00
if (MergedSection *osec = find())
2020-11-07 04:13:19 +03:00
return osec;
}
// Create a new output section.
std::unique_lock lock(mu);
2020-11-07 15:53:21 +03:00
if (MergedSection *osec = find())
2020-11-07 04:13:19 +03:00
return osec;
2020-11-07 15:53:21 +03:00
auto *osec = new MergedSection(name, flags, type);
MergedSection::instances.push_back(osec);
2020-11-07 04:13:19 +03:00
return osec;
}
2020-11-25 11:20:48 +03:00
2021-03-13 17:34:51 +03:00
static void update_atomic_max(std::atomic_uint16_t &atom, i16 val) {
u16 cur = atom;
while (cur < val)
if (atom.compare_exchange_strong(cur, val))
break;
}
2021-03-29 14:29:57 +03:00
template <typename E>
SectionFragment<E> *
MergedSection<E>::insert(std::string_view data, i64 alignment) {
2021-03-11 11:06:43 +03:00
assert(alignment < UINT16_MAX);
std::string_view suffix = data;
if (suffix.size() > 32)
suffix = suffix.substr(suffix.size() - 32);
2021-03-15 09:35:15 +03:00
i64 shard = hash_string(suffix) % NUM_SHARDS;
2021-03-11 11:06:43 +03:00
2021-03-29 14:29:57 +03:00
typename MapTy::const_accessor acc;
2021-03-11 11:06:43 +03:00
bool inserted =
maps[shard].insert(acc, std::pair(data, SectionFragment(this, data)));
2021-03-29 14:29:57 +03:00
SectionFragment<E> *frag = const_cast<SectionFragment<E> *>(&acc->second);
2021-03-11 11:06:43 +03:00
2021-03-13 17:34:51 +03:00
update_atomic_max(frag->alignment, alignment);
update_atomic_max(max_alignment, alignment);
2021-03-11 11:06:43 +03:00
return frag;
}
2020-12-13 19:52:22 +03:00
2021-03-29 14:29:57 +03:00
template <typename E>
void MergedSection<E>::assign_offsets() {
std::vector<SectionFragment<E> *> fragments[NUM_SHARDS];
2021-03-13 17:53:16 +03:00
i64 sizes[NUM_SHARDS] = {};
2021-03-11 11:06:43 +03:00
tbb::parallel_for((i64)0, NUM_SHARDS, [&](i64 i) {
2021-03-13 17:53:16 +03:00
for (auto it = maps[i].begin(); it != maps[i].end(); it++)
2021-03-29 14:29:57 +03:00
if (SectionFragment<E> &frag = it->second; frag.is_alive)
2021-03-15 08:33:14 +03:00
fragments[i].push_back(&frag);
2021-03-11 13:29:06 +03:00
// Sort section fragments to make an output deterministic.
2021-03-15 08:33:14 +03:00
std::sort(fragments[i].begin(), fragments[i].end(),
2021-03-29 14:29:57 +03:00
[&](SectionFragment<E> *a, SectionFragment<E> *b) {
2021-03-13 05:13:37 +03:00
if (a->alignment != b->alignment)
return a->alignment > b->alignment;
2021-03-11 11:06:43 +03:00
if (a->data.size() != b->data.size())
return a->data.size() < b->data.size();
return a->data < b->data;
});
2021-03-13 17:53:16 +03:00
i64 offset = 0;
2021-03-29 14:29:57 +03:00
for (SectionFragment<E> *frag : fragments[i]) {
2021-03-13 17:53:16 +03:00
offset = align_to(offset, frag->alignment);
frag->offset = offset;
offset += frag->data.size();
}
sizes[i] = offset;
2021-03-11 11:06:43 +03:00
});
2021-03-13 17:53:16 +03:00
for (i64 i = 1; i < NUM_SHARDS + 1; i++)
shard_offsets[i] =
align_to(shard_offsets[i - 1] + sizes[i - 1], max_alignment);
2021-03-13 17:53:16 +03:00
tbb::parallel_for((i64)1, NUM_SHARDS, [&](i64 i) {
2021-03-29 14:29:57 +03:00
for (SectionFragment<E> *frag : fragments[i])
2021-03-15 08:33:14 +03:00
frag->offset += shard_offsets[i];
2021-03-13 17:53:16 +03:00
});
2021-03-13 17:34:51 +03:00
2021-03-29 14:29:57 +03:00
this->shdr.sh_size = shard_offsets[NUM_SHARDS];
this->shdr.sh_addralign = max_alignment;
2021-03-15 08:33:14 +03:00
static Counter merged_strings("merged_strings");
2021-03-29 14:29:57 +03:00
for (std::span<SectionFragment<E> *> span : fragments)
2021-03-15 08:33:14 +03:00
merged_strings += span.size();
2021-03-11 11:06:43 +03:00
}
2021-03-29 14:29:57 +03:00
template <typename E>
void MergedSection<E>::copy_buf(Context<E> &ctx) {
u8 *base = ctx.buf + this->shdr.sh_offset;
2021-03-11 11:06:43 +03:00
2021-03-13 17:53:16 +03:00
tbb::parallel_for((i64)0, NUM_SHARDS, [&](i64 i) {
memset(base + shard_offsets[i], 0, shard_offsets[i + 1] - shard_offsets[i]);
2021-01-22 10:14:49 +03:00
2021-03-13 17:53:16 +03:00
for (auto it = maps[i].begin(); it != maps[i].end(); it++)
2021-03-29 14:29:57 +03:00
if (SectionFragment<E> &frag = it->second; frag.is_alive)
2021-03-13 17:53:16 +03:00
memcpy(base + frag.offset, frag.data.data(), frag.data.size());
});
2020-12-13 19:40:23 +03:00
}
2021-03-29 14:29:57 +03:00
template <typename E>
void EhFrameSection<E>::construct(Context<E> &ctx) {
2021-01-20 08:27:30 +03:00
// Remove dead FDEs and assign them offsets within their corresponding
// CIE group.
2021-03-29 07:20:51 +03:00
tbb::parallel_for((i64)0, (i64)ctx.objs.size(), [&](i64 i) {
2021-03-29 14:29:57 +03:00
ObjectFile<E> *file = ctx.objs[i];
2021-01-24 06:01:43 +03:00
i64 count = 0;
2021-01-20 16:41:32 +03:00
2021-03-29 14:29:57 +03:00
for (CieRecord<E> &cie : file->cies) {
2021-01-24 06:01:43 +03:00
i64 offset = 0;
2021-03-29 14:29:57 +03:00
for (FdeRecord<E> &fde : cie.fdes) {
2021-01-25 06:25:03 +03:00
if (!fde.is_alive)
2021-01-20 09:57:37 +03:00
continue;
2021-01-22 14:35:33 +03:00
fde.offset = offset;
2021-01-19 12:39:57 +03:00
offset += fde.contents.size();
2021-01-21 01:15:09 +03:00
cie.num_fdes++;
2021-01-19 12:39:57 +03:00
}
cie.fde_size = offset;
2021-01-19 11:09:10 +03:00
}
});
2021-01-20 08:27:30 +03:00
// Aggreagate CIEs.
2021-03-29 07:20:51 +03:00
cies.reserve(ctx.objs.size());
2021-03-29 14:29:57 +03:00
for (ObjectFile<E> *file : ctx.objs)
for (CieRecord<E> &cie : file->cies)
2021-01-19 12:39:57 +03:00
cies.push_back(&cie);
2021-01-18 15:07:31 +03:00
2021-03-17 08:39:49 +03:00
// Record the total number of FDEs for .eh_frame_hdr.
2021-03-29 14:29:57 +03:00
for (CieRecord<E> *cie : cies) {
2021-01-21 01:15:09 +03:00
cie->fde_idx = num_fdes;
num_fdes += cie->num_fdes;
}
2021-01-20 08:27:30 +03:00
// Assign offsets within the output section to CIEs.
2021-03-29 14:29:57 +03:00
auto should_merge = [](CieRecord<E> &a, CieRecord<E> &b) {
2021-01-20 14:47:15 +03:00
return a.contents == b.contents && a.rels == b.rels;
};
2021-01-24 06:01:43 +03:00
i64 offset = 0;
for (i64 i = 0; i < cies.size(); i++) {
2021-03-29 14:29:57 +03:00
CieRecord<E> &cie = *cies[i];
2021-01-22 14:35:33 +03:00
cie.offset = offset;
2021-01-19 13:24:00 +03:00
2021-01-20 14:47:15 +03:00
if (i == 0 || !should_merge(cie, *cies[i - 1])) {
2021-01-19 13:24:00 +03:00
cie.leader_offset = offset;
offset += cie.contents.size() + cie.fde_size;
2021-01-19 11:48:35 +03:00
} else {
2021-01-19 14:38:42 +03:00
cie.leader_offset = cies[i - 1]->leader_offset;
2021-01-19 13:24:00 +03:00
offset += cie.fde_size;
2021-01-19 11:48:35 +03:00
}
2021-01-18 15:25:58 +03:00
}
2021-03-29 14:29:57 +03:00
this->shdr.sh_size = offset;
2021-01-21 02:53:07 +03:00
2021-03-29 07:20:51 +03:00
if (ctx.eh_frame_hdr)
ctx.eh_frame_hdr->shdr.sh_size =
ctx.eh_frame_hdr->HEADER_SIZE + num_fdes * 8;
2021-01-17 10:55:39 +03:00
}
2021-03-29 14:29:57 +03:00
template <typename E>
void EhFrameSection<E>::copy_buf(Context<E> &ctx) {
u8 *base = ctx.buf + this->shdr.sh_offset;
2021-01-21 16:25:21 +03:00
2021-01-23 18:03:35 +03:00
u8 *hdr_base = nullptr;
2021-03-29 07:20:51 +03:00
if (ctx.eh_frame_hdr)
hdr_base = ctx.buf + ctx.eh_frame_hdr->shdr.sh_offset;
2021-01-21 16:25:21 +03:00
2021-03-29 14:29:57 +03:00
auto apply_reloc = [&](EhReloc<E> &rel, u64 loc, u64 val) {
switch (rel.type) {
case R_X86_64_32:
2021-01-24 03:38:23 +03:00
*(u32 *)(base + loc) = val;
return;
case R_X86_64_64:
*(u64 *)(base + loc) = val;
return;
case R_X86_64_PC32:
2021-03-29 14:29:57 +03:00
*(u32 *)(base + loc) = val - this->shdr.sh_addr - loc;
return;
case R_X86_64_PC64:
2021-03-29 14:29:57 +03:00
*(u64 *)(base + loc) = val - this->shdr.sh_addr - loc;
return;
}
2021-03-29 14:55:27 +03:00
unreachable(ctx);
2021-01-19 13:24:00 +03:00
};
2021-01-21 16:25:21 +03:00
struct Entry {
i32 init_addr;
i32 fde_addr;
};
2021-01-20 08:27:30 +03:00
// Copy CIEs and FDEs.
2021-03-29 14:29:57 +03:00
tbb::parallel_for_each(cies, [&](CieRecord<E> *cie) {
2021-01-24 06:01:43 +03:00
i64 cie_size = 0;
2021-01-19 12:39:57 +03:00
2021-01-21 16:25:21 +03:00
Entry *entry = nullptr;
2021-03-29 07:20:51 +03:00
if (ctx.eh_frame_hdr)
entry = (Entry *)(hdr_base + ctx.eh_frame_hdr->HEADER_SIZE) +
2021-03-12 07:17:45 +03:00
cie->fde_idx;
2021-01-21 16:25:21 +03:00
2021-01-20 08:27:30 +03:00
// Copy a CIE.
2021-01-22 14:35:33 +03:00
if (cie->offset == cie->leader_offset) {
memcpy(base + cie->offset, cie->contents.data(), cie->contents.size());
2021-01-20 08:27:30 +03:00
cie_size = cie->contents.size();
2021-01-19 12:39:57 +03:00
2021-03-29 14:29:57 +03:00
for (EhReloc<E> &rel : cie->rels) {
2021-01-24 03:38:23 +03:00
u64 loc = cie->offset + rel.offset;
2021-03-29 10:17:33 +03:00
u64 val = rel.sym.get_addr(ctx) + rel.addend;
2021-01-24 03:38:23 +03:00
apply_reloc(rel, loc, val);
2021-01-19 12:39:57 +03:00
}
2021-01-19 11:48:35 +03:00
}
2021-01-20 08:27:30 +03:00
// Copy FDEs.
2021-03-29 14:29:57 +03:00
for (FdeRecord<E> &fde : cie->fdes) {
2021-01-22 14:35:33 +03:00
if (fde.offset == -1)
2021-01-20 09:57:37 +03:00
continue;
2021-01-24 06:01:43 +03:00
i64 fde_off = cie->offset + cie_size + fde.offset;
2021-01-19 12:39:57 +03:00
memcpy(base + fde_off, fde.contents.data(), fde.contents.size());
2021-01-20 08:27:30 +03:00
*(u32 *)(base + fde_off + 4) = fde_off + 4 - cie->leader_offset;
2021-01-19 11:48:35 +03:00
2021-01-24 06:01:43 +03:00
for (i64 i = 0; i < fde.rels.size(); i++) {
2021-03-29 14:29:57 +03:00
EhReloc<E> &rel = fde.rels[i];
2021-01-24 03:38:23 +03:00
u64 loc = fde_off + rel.offset;
2021-03-29 10:17:33 +03:00
u64 val = rel.sym.get_addr(ctx) + rel.addend;
2021-01-24 03:38:23 +03:00
apply_reloc(rel, loc, val);
2021-01-21 16:25:21 +03:00
// Write to .eh_frame_hdr
2021-03-29 07:20:51 +03:00
if (ctx.eh_frame_hdr && i == 0) {
assert(rel.offset == 8);
2021-03-29 07:20:51 +03:00
entry->init_addr = val - ctx.eh_frame_hdr->shdr.sh_addr;
2021-03-12 07:17:45 +03:00
entry->fde_addr =
2021-03-29 14:29:57 +03:00
this->shdr.sh_addr + fde_off - ctx.eh_frame_hdr->shdr.sh_addr;
2021-01-21 16:25:21 +03:00
entry++;
}
2021-01-19 11:48:35 +03:00
}
2021-01-19 11:09:10 +03:00
}
2021-01-19 12:39:57 +03:00
});
2021-01-21 16:25:21 +03:00
2021-03-29 07:20:51 +03:00
if (ctx.eh_frame_hdr) {
2021-01-23 18:03:35 +03:00
// Write .eh_frame_hdr header
hdr_base[0] = 1;
hdr_base[1] = DW_EH_PE_pcrel | DW_EH_PE_sdata4;
hdr_base[2] = DW_EH_PE_udata4;
hdr_base[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4;
2021-03-12 07:17:45 +03:00
*(u32 *)(hdr_base + 4) =
2021-03-29 14:29:57 +03:00
this->shdr.sh_addr - ctx.eh_frame_hdr->shdr.sh_addr - 4;
2021-01-23 18:03:35 +03:00
*(u32 *)(hdr_base + 8) = num_fdes;
// Sort .eh_frame_hdr contents
2021-03-29 07:20:51 +03:00
Entry *begin = (Entry *)(hdr_base + ctx.eh_frame_hdr->HEADER_SIZE);
2021-01-21 16:25:21 +03:00
Entry *end = begin + num_fdes;
tbb::parallel_sort(begin, end, [](const Entry &a, const Entry &b) {
return a.init_addr < b.init_addr;
});
}
2021-01-17 10:55:39 +03:00
}
2021-03-17 08:39:49 +03:00
// Compiler-generated object files don't usually contain symbols
// referring a .eh_frame section, but crtend.o contains such symbol
// (i.e. "__FRAME_END__"). So we need to handle such symbol.
// This function is slow, but it's okay because they are rare.
2021-03-29 14:29:57 +03:00
template <typename E>
u64 EhFrameSection<E>::get_addr(Context<E> &ctx, const Symbol<E> &sym) {
InputSection<E> &isec = *sym.input_section;
2021-02-27 07:44:30 +03:00
const char *section_begin = isec.contents.data();
2021-01-20 14:47:15 +03:00
auto contains = [](std::string_view str, const char *ptr) {
const char *begin = str.data();
const char *end = begin + str.size();
return (begin == ptr) || (begin < ptr && ptr < end);
};
2021-03-29 14:29:57 +03:00
for (CieRecord<E> &cie : isec.file.cies) {
2021-01-20 14:47:15 +03:00
u64 offset = 0;
2021-01-22 14:35:33 +03:00
if (cie.offset == cie.leader_offset) {
2021-01-20 14:47:15 +03:00
if (contains(cie.contents, section_begin + offset)) {
2021-03-29 14:29:57 +03:00
u64 cie_addr = this->shdr.sh_addr + cie.offset;
2021-01-20 14:47:15 +03:00
u64 addend = sym.value - offset;
return cie_addr + addend;
}
offset += cie.contents.size();
}
2021-03-29 14:29:57 +03:00
for (FdeRecord<E> &fde : cie.fdes) {
2021-01-20 14:47:15 +03:00
if (contains(fde.contents, section_begin + offset)) {
2021-01-25 06:25:03 +03:00
if (!fde.is_alive)
2021-01-20 14:47:15 +03:00
return 0;
2021-03-29 14:29:57 +03:00
u64 fde_addr = this->shdr.sh_addr + cie.offset + offset;
2021-01-20 14:47:15 +03:00
u64 addend = sym.value - offset;
return fde_addr + addend;
}
offset += fde.contents.size();
}
}
2021-03-29 10:48:23 +03:00
Fatal(ctx) << isec.file << ": .eh_frame has bad symbol: " << sym;
2021-01-20 14:47:15 +03:00
}
2021-03-29 14:29:57 +03:00
template <typename E>
void DynbssSection<E>::add_symbol(Context<E> &ctx, Symbol<E> *sym) {
2021-01-14 14:31:39 +03:00
if (sym->has_copyrel)
2020-11-25 14:35:04 +03:00
return;
2021-03-29 07:20:51 +03:00
assert(!ctx.arg.shared);
2021-03-17 08:39:49 +03:00
assert(sym->file->is_dso);
2021-03-29 14:29:57 +03:00
this->shdr.sh_size = align_to(this->shdr.sh_size, this->shdr.sh_addralign);
sym->value = this->shdr.sh_size;
2021-01-14 14:31:39 +03:00
sym->has_copyrel = true;
2021-03-29 14:29:57 +03:00
this->shdr.sh_size += sym->esym->st_size;
2020-11-25 11:20:48 +03:00
symbols.push_back(sym);
2021-03-29 10:02:12 +03:00
ctx.dynsym->add_symbol(ctx, sym);
2020-11-25 11:20:48 +03:00
}
2020-11-27 11:32:25 +03:00
2021-03-29 14:29:57 +03:00
template <typename E>
void VersymSection<E>::update_shdr(Context<E> &ctx) {
this->shdr.sh_size = contents.size() * sizeof(contents[0]);
this->shdr.sh_link = ctx.dynsym->shndx;
2020-11-27 11:32:25 +03:00
}
2021-03-29 14:29:57 +03:00
template <typename E>
void VersymSection<E>::copy_buf(Context<E> &ctx) {
write_vector(ctx.buf + this->shdr.sh_offset, contents);
2020-11-27 11:32:25 +03:00
}
2021-03-29 14:29:57 +03:00
template <typename E>
void VerneedSection<E>::update_shdr(Context<E> &ctx) {
this->shdr.sh_size = contents.size();
this->shdr.sh_link = ctx.dynstr->shndx;
2020-11-27 11:32:25 +03:00
}
2021-03-29 14:29:57 +03:00
template <typename E>
void VerneedSection<E>::copy_buf(Context<E> &ctx) {
write_vector(ctx.buf + this->shdr.sh_offset, contents);
2020-11-27 11:32:25 +03:00
}
2021-01-15 14:14:01 +03:00
2021-03-29 14:29:57 +03:00
template <typename E>
void VerdefSection<E>::update_shdr(Context<E> &ctx) {
this->shdr.sh_size = contents.size();
this->shdr.sh_link = ctx.dynstr->shndx;
2021-03-07 07:43:58 +03:00
}
2021-03-29 14:29:57 +03:00
template <typename E>
void VerdefSection<E>::copy_buf(Context<E> &ctx) {
write_vector(ctx.buf + this->shdr.sh_offset, contents);
2021-03-07 07:43:58 +03:00
}
2021-03-29 14:29:57 +03:00
template <typename E>
void BuildIdSection<E>::update_shdr(Context<E> &ctx) {
this->shdr.sh_size = HEADER_SIZE + ctx.arg.build_id.size(ctx);
2021-01-26 13:46:34 +03:00
}
2021-03-29 14:29:57 +03:00
template <typename E>
void BuildIdSection<E>::copy_buf(Context<E> &ctx) {
u32 *base = (u32 *)(ctx.buf + this->shdr.sh_offset);
memset(base, 0, this->shdr.sh_size);
2021-03-29 10:48:23 +03:00
base[0] = 4; // Name size
base[1] = ctx.arg.build_id.size(ctx); // Hash size
base[2] = NT_GNU_BUILD_ID; // Type
memcpy(base + 3, "GNU", 4); // Name string
2021-01-26 13:46:34 +03:00
}
static void compute_sha256(u8 *buf, i64 size, u8 *digest) {
i64 shard_size = 1024 * 1024;
i64 num_shards = size / shard_size + 1;
std::vector<u8> shards(num_shards * SHA256_SIZE);
tbb::parallel_for((i64)0, num_shards, [&](i64 i) {
u8 *begin = buf + shard_size * i;
i64 sz = (i < num_shards - 1) ? shard_size : (size % shard_size);
SHA256(begin, sz, shards.data() + i * SHA256_SIZE);
});
SHA256(shards.data(), shards.size(), digest);
2021-01-15 14:14:01 +03:00
}
2021-03-29 14:29:57 +03:00
template <typename E>
void BuildIdSection<E>::write_buildid(Context<E> &ctx, i64 filesize) {
2021-03-29 07:20:51 +03:00
switch (ctx.arg.build_id.kind) {
2021-03-08 14:48:21 +03:00
case BuildId::HEX:
2021-03-29 14:29:57 +03:00
write_vector(ctx.buf + this->shdr.sh_offset + HEADER_SIZE,
2021-03-29 07:20:51 +03:00
ctx.arg.build_id.value);
2021-03-08 14:48:21 +03:00
return;
case BuildId::HASH: {
// Modern x86 processors have purpose-built instructions to accelerate
// SHA256 computation, and SHA256 outperforms MD5 on such computers.
// So, we always compute SHA256 and truncate it if smaller digest was
// requested.
u8 digest[SHA256_SIZE];
2021-03-29 10:48:23 +03:00
assert(ctx.arg.build_id.size(ctx) <= SHA256_SIZE);
2021-03-29 07:20:51 +03:00
compute_sha256(ctx.buf, filesize, digest);
2021-03-29 14:29:57 +03:00
memcpy(ctx.buf + this->shdr.sh_offset + HEADER_SIZE, digest,
2021-03-29 10:48:23 +03:00
ctx.arg.build_id.size(ctx));
2021-03-08 14:48:21 +03:00
return;
}
case BuildId::UUID:
2021-03-29 14:29:57 +03:00
if (!RAND_bytes(ctx.buf + this->shdr.sh_offset + HEADER_SIZE,
2021-03-29 10:48:23 +03:00
ctx.arg.build_id.size(ctx)))
Fatal(ctx) << "RAND_bytes failed";
2021-01-26 13:46:34 +03:00
return;
}
2021-03-29 14:55:27 +03:00
unreachable(ctx);
2021-01-15 14:14:01 +03:00
}
2021-03-29 14:29:57 +03:00
2021-03-30 16:26:03 +03:00
#define INSTANTIATE(E) \
template class OutputChunk<E>; \
template class OutputEhdr<E>; \
template class OutputShdr<E>; \
template class OutputPhdr<E>; \
template class InterpSection<E>; \
template class OutputSection<E>; \
template class GotSection<E>; \
template class GotPltSection<E>; \
template class PltSection<E>; \
template class PltGotSection<E>; \
template class RelPltSection<E>; \
template class RelDynSection<E>; \
template class StrtabSection<E>; \
template class ShstrtabSection<E>; \
template class DynstrSection<E>; \
template class DynamicSection<E>; \
template class SymtabSection<E>; \
template class DynsymSection<E>; \
template class HashSection<E>; \
template class GnuHashSection<E>; \
template class MergedSection<E>; \
template class EhFrameSection<E>; \
template class EhFrameHdrSection<E>; \
template class DynbssSection<E>; \
template class VersymSection<E>; \
template class VerneedSection<E>; \
template class VerdefSection<E>; \
template class BuildIdSection<E>; \
template i64 BuildId::size(Context<E> &) const; \
template bool is_relro(Context<E> &, OutputChunk<E> *); \
template std::vector<ElfPhdr<E>> create_phdr(Context<E> &)
INSTANTIATE(X86_64);
INSTANTIATE(I386);