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

1054 lines
29 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
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
2020-11-17 07:56:40 +03:00
void OutputEhdr::copy_buf() {
2021-01-23 18:03:35 +03:00
ElfEhdr &hdr = *(ElfEhdr *)(out::buf + 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);
hdr.e_ident[EI_CLASS] = ELFCLASS64;
hdr.e_ident[EI_DATA] = ELFDATA2LSB;
hdr.e_ident[EI_VERSION] = EV_CURRENT;
2020-12-17 02:01:43 +03:00
hdr.e_type = config.pie ? ET_DYN : ET_EXEC;
2020-11-16 18:17:01 +03:00
hdr.e_machine = EM_X86_64;
hdr.e_version = EV_CURRENT;
2020-12-14 04:07:14 +03:00
hdr.e_entry = Symbol::intern(config.entry)->get_addr();
2020-11-16 18:17:01 +03:00
hdr.e_phoff = out::phdr->shdr.sh_offset;
hdr.e_shoff = out::shdr->shdr.sh_offset;
2020-12-10 15:13:02 +03:00
hdr.e_ehsize = sizeof(ElfEhdr);
hdr.e_phentsize = sizeof(ElfPhdr);
hdr.e_phnum = out::phdr->shdr.sh_size / sizeof(ElfPhdr);
2020-12-10 09:59:24 +03:00
hdr.e_shentsize = sizeof(ElfShdr);
hdr.e_shnum = out::shdr->shdr.sh_size / sizeof(ElfShdr);
2020-11-16 18:17:01 +03:00
hdr.e_shstrndx = out::shstrtab->shndx;
}
2020-11-16 18:23:51 +03:00
void OutputShdr::update_shdr() {
2021-01-24 06:01:43 +03:00
i64 n = 1;
2020-11-16 18:23:51 +03:00
for (OutputChunk *chunk : out::chunks)
if (chunk->kind != OutputChunk::HEADER)
2021-01-23 18:03:35 +03:00
n++;
shdr.sh_size = n * sizeof(ElfShdr);
2020-11-16 18:23:51 +03:00
}
2020-11-17 07:56:40 +03:00
void OutputShdr::copy_buf() {
2021-01-24 03:38:23 +03:00
ElfShdr *hdr = (ElfShdr *)(out::buf + shdr.sh_offset);
hdr[0] = {};
2020-11-16 18:23:51 +03:00
2021-01-24 06:01:43 +03:00
i64 i = 1;
2020-11-16 18:23:51 +03:00
for (OutputChunk *chunk : out::chunks)
if (chunk->kind != OutputChunk::HEADER)
2021-01-24 03:38:23 +03:00
hdr[i++] = chunk->shdr;
2020-11-16 18:23:51 +03:00
}
2021-01-24 06:01:43 +03:00
static i64 to_phdr_flags(OutputChunk *chunk) {
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;
}
2020-12-10 15:13:02 +03:00
std::vector<ElfPhdr> create_phdr() {
std::vector<ElfPhdr> vec;
2020-11-16 18:33:41 +03:00
2021-01-24 06:01:43 +03:00
auto define = [&](u64 type, u64 flags, i64 align, OutputChunk *chunk) {
2020-11-16 18:33:41 +03:00
vec.push_back({});
2020-12-10 15:13:02 +03:00
ElfPhdr &phdr = vec.back();
2020-11-16 18:33:41 +03:00
phdr.p_type = type;
phdr.p_flags = flags;
phdr.p_align = std::max<u64>(align, chunk->shdr.sh_addralign);
phdr.p_offset = chunk->shdr.sh_offset;
phdr.p_filesz = (chunk->shdr.sh_type == SHT_NOBITS) ? 0 : chunk->shdr.sh_size;
phdr.p_vaddr = chunk->shdr.sh_addr;
2020-12-12 14:44:12 +03:00
phdr.p_paddr = chunk->shdr.sh_addr;
2020-11-16 18:33:41 +03:00
phdr.p_memsz = chunk->shdr.sh_size;
if (type == PT_LOAD)
chunk->starts_new_ptload = true;
};
auto append = [&](OutputChunk *chunk) {
2020-12-10 15:13:02 +03:00
ElfPhdr &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;
};
auto is_bss = [](OutputChunk *chunk) {
return chunk->shdr.sh_type == SHT_NOBITS && !(chunk->shdr.sh_flags & SHF_TLS);
};
// Create a PT_PHDR for the program header itself.
define(PT_PHDR, PF_R, 8, out::phdr);
2021-01-16 05:57:36 +03:00
// Create a PT_INTERP.
2020-11-16 18:33:41 +03:00
if (out::interp)
define(PT_INTERP, PF_R, 1, out::interp);
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-01-24 06:01:43 +03:00
for (i64 i = 0, end = out::chunks.size(); i < end;) {
2021-01-16 05:57:36 +03:00
OutputChunk *first = out::chunks[i++];
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);
while (i < end && out::chunks[i]->shdr.sh_type == SHT_NOTE &&
to_phdr_flags(out::chunks[i]) == flags &&
out::chunks[i]->shdr.sh_addralign == alignment)
append(out::chunks[i++]);
}
2020-11-16 18:33:41 +03:00
// Create PT_LOAD segments.
2021-01-24 06:01:43 +03:00
for (i64 i = 0, end = out::chunks.size(); i < end;) {
2020-11-16 18:33:41 +03:00
OutputChunk *first = out::chunks[i++];
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);
if (!is_bss(first))
while (i < end && !is_bss(out::chunks[i]) &&
to_phdr_flags(out::chunks[i]) == flags)
append(out::chunks[i++]);
while (i < end && is_bss(out::chunks[i]) &&
to_phdr_flags(out::chunks[i]) == flags)
append(out::chunks[i++]);
}
// Create a PT_TLS.
2021-01-24 06:01:43 +03:00
for (i64 i = 0; i < out::chunks.size(); i++) {
2021-01-16 05:57:36 +03:00
if (!(out::chunks[i]->shdr.sh_flags & SHF_TLS))
continue;
define(PT_TLS, to_phdr_flags(out::chunks[i]), 1, out::chunks[i]);
i++;
while (i < out::chunks.size() && (out::chunks[i]->shdr.sh_flags & SHF_TLS))
append(out::chunks[i++]);
2020-11-16 18:33:41 +03:00
}
// Add PT_DYNAMIC
if (out::dynamic)
define(PT_DYNAMIC, PF_R | PF_W, out::dynamic->shdr.sh_addralign, out::dynamic);
2021-01-21 03:58:46 +03:00
// Add PT_GNU_EH_FRAME
if (out::eh_frame_hdr)
define(PT_GNU_EH_FRAME, PF_R, out::eh_frame_hdr->shdr.sh_addralign,
out::eh_frame_hdr);
2020-12-17 16:06:51 +03:00
// Add PT_GNU_STACK, which is a marker segment that doesn't really
// contain any segments. If exists, the runtime turn on the No Exeecute
// bit for stack pages.
vec.push_back({});
vec.back().p_type = PT_GNU_STACK;
vec.back().p_flags = PF_R | PF_W;
2020-11-16 18:33:41 +03:00
return vec;
}
void OutputPhdr::update_shdr() {
2020-12-10 15:13:02 +03:00
shdr.sh_size = create_phdr().size() * sizeof(ElfPhdr);
2020-11-16 18:33:41 +03:00
}
2020-11-17 07:56:40 +03:00
void OutputPhdr::copy_buf() {
write_vector(out::buf + shdr.sh_offset, create_phdr());
2020-11-16 18:43:32 +03:00
}
2020-11-17 07:56:40 +03:00
void InterpSection::copy_buf() {
write_string(out::buf + shdr.sh_offset, config.dynamic_linker);
2020-11-16 19:05:01 +03:00
}
2020-11-17 07:30:33 +03:00
void RelDynSection::update_shdr() {
shdr.sh_link = out::dynsym->shndx;
2020-11-18 14:29:24 +03:00
2021-01-24 06:01:43 +03:00
i64 n = 0;
2020-12-17 08:32:18 +03:00
for (Symbol *sym : out::got->got_syms)
2021-01-13 09:42:07 +03:00
if (sym->is_imported || (config.pie && sym->is_relative()))
2020-12-17 08:32:18 +03:00
n++;
2020-11-21 05:51:21 +03:00
2020-11-25 11:20:48 +03:00
n += out::got->tlsgd_syms.size() * 2;
n += out::copyrel->symbols.size();
2020-12-16 14:56:04 +03:00
2021-01-14 13:58:21 +03:00
if (out::got->tlsld_idx != -1)
n++;
2020-12-16 14:56:04 +03:00
for (ObjectFile *file : out::objs) {
file->reldyn_offset = n * sizeof(ElfRela);
2020-12-18 04:56:56 +03:00
n += file->num_dynrel;
2020-12-16 14:56:04 +03:00
}
2020-12-10 15:11:10 +03:00
shdr.sh_size = n * sizeof(ElfRela);
2020-11-18 14:29:24 +03:00
}
void RelDynSection::copy_buf() {
2020-12-10 15:11:10 +03:00
ElfRela *rel = (ElfRela *)(out::buf + shdr.sh_offset);
2020-11-18 14:29:24 +03:00
2020-12-17 02:01:43 +03:00
for (Symbol *sym : out::got->got_syms) {
2021-01-14 08:48:54 +03:00
if (sym->is_imported)
2021-01-14 10:16:50 +03:00
*rel++ = {sym->get_got_addr(), R_X86_64_GLOB_DAT, sym->dynsym_idx, 0};
2021-01-14 08:48:54 +03:00
else if (config.pie && sym->is_relative())
2021-01-14 10:16:50 +03:00
*rel++ = {sym->get_got_addr(), R_X86_64_RELATIVE, 0, (i64)sym->get_addr()};
2020-12-17 02:01:43 +03:00
}
2020-11-21 05:51:21 +03:00
2020-11-25 07:28:29 +03:00
for (Symbol *sym : out::got->tlsgd_syms) {
2021-01-14 10:16:50 +03:00
*rel++ = {sym->get_tlsgd_addr(), R_X86_64_DTPMOD64, sym->dynsym_idx, 0};
*rel++ = {sym->get_tlsgd_addr() + GOT_SIZE, R_X86_64_DTPOFF64, sym->dynsym_idx, 0};
2020-11-21 05:51:21 +03:00
}
2021-01-14 13:58:21 +03:00
if (out::got->tlsld_idx != -1)
*rel++ = {out::got->get_tlsld_addr(), R_X86_64_DTPMOD64, 0, 0};
2020-11-25 07:28:29 +03:00
for (Symbol *sym : out::got->gottpoff_syms)
if (sym->is_imported)
2021-01-14 10:16:50 +03:00
*rel++ = {sym->get_gottpoff_addr(), R_X86_64_TPOFF32, sym->dynsym_idx, 0};
2020-11-25 11:20:48 +03:00
for (Symbol *sym : out::copyrel->symbols)
2021-01-14 10:16:50 +03:00
*rel++ = {sym->get_addr(), R_X86_64_COPY, sym->dynsym_idx, 0};
2020-11-17 07:30:33 +03:00
}
2020-11-29 12:58:36 +03:00
void StrtabSection::update_shdr() {
2020-11-29 15:14:49 +03:00
shdr.sh_size = 1;
2020-11-29 12:58:36 +03:00
for (ObjectFile *file : out::objs) {
2020-11-29 15:14:49 +03:00
file->strtab_offset = shdr.sh_size;
shdr.sh_size += file->strtab_size;
2020-11-29 12:58:36 +03:00
}
}
2020-11-17 06:20:56 +03:00
void ShstrtabSection::update_shdr() {
shdr.sh_size = 1;
for (OutputChunk *chunk : out::chunks) {
if (!chunk->name.empty()) {
chunk->shdr.sh_name = shdr.sh_size;
shdr.sh_size += chunk->name.size() + 1;
}
2020-11-18 15:45:49 +03:00
}
2020-11-17 06:20:56 +03:00
}
2020-11-17 07:56:40 +03:00
void ShstrtabSection::copy_buf() {
u8 *base = out::buf + 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;
2020-11-17 06:20:56 +03:00
for (OutputChunk *chunk : out::chunks) {
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-01-24 06:01:43 +03:00
i64 DynstrSection::add_string(std::string_view str) {
i64 ret = shdr.sh_size;
2020-11-17 07:19:54 +03:00
shdr.sh_size += str.size() + 1;
contents.push_back(str);
return ret;
}
2021-01-24 06:01:43 +03:00
i64 DynstrSection::find_string(std::string_view str) {
i64 i = 1;
2020-12-10 07:44:58 +03:00
for (std::string_view s : contents) {
2020-11-30 11:52:08 +03:00
if (s == str)
return i;
i += s.size() + 1;
}
unreachable();
}
2020-11-17 07:56:40 +03:00
void DynstrSection::copy_buf() {
u8 *base = out::buf + shdr.sh_offset;
2020-11-17 07:19:54 +03:00
base[0] = '\0';
2021-01-24 06:01:43 +03:00
i64 i = 1;
2020-12-10 07:44:58 +03:00
for (std::string_view s : contents) {
2020-11-17 07:19:54 +03:00
write_string(base + i, s);
i += s.size() + 1;
}
}
2020-11-17 07:32:22 +03:00
void SymtabSection::update_shdr() {
2020-12-10 09:10:18 +03:00
shdr.sh_size = sizeof(ElfSym);
2020-11-17 08:28:53 +03:00
2020-11-29 12:58:36 +03:00
for (ObjectFile *file : out::objs) {
2020-11-29 15:14:49 +03:00
file->local_symtab_offset = shdr.sh_size;
shdr.sh_size += file->local_symtab_size;
2020-11-29 12:45:33 +03:00
}
2020-11-17 08:28:53 +03:00
2020-11-29 12:58:36 +03:00
for (ObjectFile *file : out::objs) {
2020-11-29 15:14:49 +03:00
file->global_symtab_offset = shdr.sh_size;
shdr.sh_size += file->global_symtab_size;
2020-11-17 08:28:53 +03:00
}
2020-12-10 09:10:18 +03:00
shdr.sh_info = out::objs[0]->global_symtab_offset / sizeof(ElfSym);
2020-11-29 12:58:36 +03:00
shdr.sh_link = out::strtab->shndx;
2021-01-09 18:49:07 +03:00
static Counter counter("symtab");
counter.inc(shdr.sh_size / sizeof(ElfSym));
2020-11-17 07:32:22 +03:00
}
2020-11-17 08:28:53 +03:00
void SymtabSection::copy_buf() {
2020-12-10 09:10:18 +03:00
memset(out::buf + shdr.sh_offset, 0, sizeof(ElfSym));
2020-11-29 13:50:59 +03:00
out::buf[out::strtab->shdr.sh_offset] = '\0';
2020-11-29 12:31:19 +03:00
tbb::parallel_for_each(out::objs, [](ObjectFile *file) { file->write_symtab(); });
2020-11-17 07:51:44 +03:00
}
2020-11-18 13:27:29 +03:00
static std::vector<u64> create_dynamic_section() {
std::vector<u64> vec;
auto define = [&](u64 tag, u64 val) {
vec.push_back(tag);
vec.push_back(val);
};
2020-11-30 11:10:01 +03:00
for (SharedFile *file : out::dsos)
2020-11-30 11:52:08 +03:00
define(DT_NEEDED, out::dynstr->find_string(file->soname));
2020-11-18 13:27:29 +03:00
2021-01-15 05:17:52 +03:00
define(DT_RUNPATH, out::dynstr->find_string(config.rpaths));
2020-11-18 13:27:29 +03:00
define(DT_RELA, out::reldyn->shdr.sh_addr);
define(DT_RELASZ, out::reldyn->shdr.sh_size);
2020-12-10 15:11:10 +03:00
define(DT_RELAENT, sizeof(ElfRela));
2020-11-18 13:27:29 +03:00
define(DT_JMPREL, out::relplt->shdr.sh_addr);
define(DT_PLTRELSZ, out::relplt->shdr.sh_size);
define(DT_PLTGOT, out::gotplt->shdr.sh_addr);
define(DT_PLTREL, DT_RELA);
define(DT_SYMTAB, out::dynsym->shdr.sh_addr);
2020-12-10 09:10:18 +03:00
define(DT_SYMENT, sizeof(ElfSym));
2020-11-18 13:27:29 +03:00
define(DT_STRTAB, out::dynstr->shdr.sh_addr);
define(DT_STRSZ, out::dynstr->shdr.sh_size);
define(DT_INIT_ARRAY, out::__init_array_start->value);
define(DT_INIT_ARRAYSZ, out::__init_array_end->value - out::__init_array_start->value);
define(DT_FINI_ARRAY, out::__fini_array_start->value);
define(DT_FINI_ARRAYSZ, out::__fini_array_end->value - out::__fini_array_start->value);
2020-11-29 07:14:56 +03:00
define(DT_VERSYM, out::versym->shdr.sh_addr);
define(DT_VERNEED, out::verneed->shdr.sh_addr);
define(DT_VERNEEDNUM, out::verneed->shdr.sh_info);
2020-11-27 09:31:20 +03:00
define(DT_DEBUG, 0);
2020-11-20 04:09:40 +03:00
2021-01-23 09:01:49 +03:00
if (out::hash)
define(DT_HASH, out::hash->shdr.sh_addr);
if (out::gnu_hash)
define(DT_GNU_HASH, out::gnu_hash->shdr.sh_addr);
2020-12-10 07:44:58 +03:00
auto find = [](std::string_view name) -> OutputChunk * {
2020-11-20 04:09:40 +03:00
for (OutputChunk *chunk : out::chunks)
if (chunk->name == name)
return chunk;
return nullptr;
};
if (OutputChunk *chunk = find(".init"))
define(DT_INIT, chunk->shdr.sh_addr);
if (OutputChunk *chunk = find(".fini"))
define(DT_FINI, chunk->shdr.sh_addr);
2021-01-24 06:01:43 +03:00
i64 flags = 0;
i64 flags1 = 0;
2020-12-17 16:29:43 +03:00
if (config.pie)
flags1 |= DF_1_PIE;
if (config.z_now) {
flags |= DF_BIND_NOW;
flags1 |= DF_1_NOW;
}
if (flags)
define(DT_FLAGS, flags);
if (flags1)
define(DT_FLAGS_1, flags1);
2020-11-18 13:27:29 +03:00
define(DT_NULL, 0);
return vec;
}
2020-11-16 18:43:32 +03:00
void DynamicSection::update_shdr() {
shdr.sh_size = create_dynamic_section().size() * 8;
shdr.sh_link = out::dynstr->shndx;
}
2020-11-17 07:56:40 +03:00
void DynamicSection::copy_buf() {
write_vector(out::buf + shdr.sh_offset, create_dynamic_section());
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) {
static std::string_view common_names[] = {
2020-10-22 10:35:17 +03:00
".text.", ".data.rel.ro.", ".data.", ".rodata.", ".bss.rel.ro.",
".bss.", ".init_array.", ".fini_array.", ".tbss.", ".tdata.",
};
2020-12-13 15:52:59 +03:00
for (std::string_view s1 : common_names) {
std::string_view s2 = s1.substr(0, s1.size() - 1);
if (name.starts_with(s1) || name == s2)
return s2;
}
2020-10-22 10:35:17 +03:00
return name;
}
2020-10-27 06:45:59 +03:00
OutputSection *
2021-01-24 06:01:43 +03:00
OutputSection::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);
2020-11-07 04:13:19 +03:00
flags = flags & ~(u64)SHF_GROUP;
2020-10-22 10:35:17 +03:00
auto find = [&]() -> OutputSection * {
2020-10-28 08:06:35 +03:00
for (OutputSection *osec : OutputSection::instances)
2020-11-13 06:43:59 +03:00
if (name == osec->name && type == osec->shdr.sh_type &&
flags == (osec->shdr.sh_flags & ~SHF_GROUP))
2020-10-22 10:35:17 +03:00
return osec;
return nullptr;
};
// Search for an exiting output section.
static std::shared_mutex mu;
2020-11-07 04:13:19 +03:00
{
std::shared_lock lock(mu);
if (OutputSection *osec = find())
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);
2020-10-22 10:35:17 +03:00
if (OutputSection *osec = find())
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
2020-11-17 07:56:40 +03:00
void OutputSection::copy_buf() {
2020-12-10 15:39:34 +03:00
if (shdr.sh_type == SHT_NOBITS)
2020-11-09 16:25:17 +03:00
return;
2021-01-24 06:01:43 +03:00
tbb::parallel_for((i64)0, (i64)members.size(), [&](u64 i) {
2021-01-20 10:28:18 +03:00
InputSection &isec = *members[i];
if (isec.shdr.sh_type == SHT_NOBITS)
return;
// Copy section contents to an output file
isec.copy_buf();
// Zero-clear trailing padding
u64 this_end = isec.offset + isec.shdr.sh_size;
u64 next_start = (i == members.size() - 1) ?
shdr.sh_size : members[i + 1]->offset;
memset(out::buf + 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
}
2020-11-21 06:49:28 +03:00
void GotSection::add_got_symbol(Symbol *sym) {
assert(sym->got_idx == -1);
2020-11-18 14:29:24 +03:00
sym->got_idx = shdr.sh_size / GOT_SIZE;
shdr.sh_size += GOT_SIZE;
got_syms.push_back(sym);
}
2020-11-21 04:48:51 +03:00
void GotSection::add_gottpoff_symbol(Symbol *sym) {
2020-11-21 06:49:28 +03:00
assert(sym->gottpoff_idx == -1);
2020-11-21 04:48:51 +03:00
sym->gottpoff_idx = shdr.sh_size / GOT_SIZE;
2020-11-18 14:29:24 +03:00
shdr.sh_size += GOT_SIZE;
2020-11-21 04:48:51 +03:00
gottpoff_syms.push_back(sym);
2020-11-18 14:29:24 +03:00
}
2020-11-21 04:48:23 +03:00
void GotSection::add_tlsgd_symbol(Symbol *sym) {
2020-11-21 06:49:28 +03:00
assert(sym->tlsgd_idx == -1);
2020-11-21 04:48:23 +03:00
sym->tlsgd_idx = shdr.sh_size / GOT_SIZE;
shdr.sh_size += GOT_SIZE * 2;
tlsgd_syms.push_back(sym);
}
2021-01-14 13:58:21 +03:00
void GotSection::add_tlsld() {
if (tlsld_idx != -1)
return;
tlsld_idx = shdr.sh_size / GOT_SIZE;
2020-11-21 05:51:21 +03:00
shdr.sh_size += GOT_SIZE * 2;
2020-11-21 04:48:23 +03:00
}
2020-11-18 14:29:24 +03:00
void GotSection::copy_buf() {
u64 *buf = (u64 *)(out::buf + shdr.sh_offset);
memset(buf, 0, shdr.sh_size);
2020-11-20 07:14:00 +03:00
for (Symbol *sym : got_syms)
2020-11-20 07:15:40 +03:00
if (!sym->is_imported)
2020-11-18 14:29:24 +03:00
buf[sym->got_idx] = sym->get_addr();
2020-11-21 04:48:51 +03:00
for (Symbol *sym : gottpoff_syms)
2020-11-25 07:28:29 +03:00
if (!sym->is_imported)
buf[sym->gottpoff_idx] = sym->get_addr() - out::tls_end;
2020-11-18 14:29:24 +03:00
}
2020-11-18 15:45:49 +03:00
void GotPltSection::copy_buf() {
u64 *buf = (u64 *)(out::buf + shdr.sh_offset);
buf[0] = out::dynamic ? out::dynamic->shdr.sh_addr : 0;
buf[1] = 0;
buf[2] = 0;
for (Symbol *sym : out::plt->symbols)
2020-11-20 03:46:49 +03:00
if (sym->gotplt_idx != -1)
buf[sym->gotplt_idx] = sym->get_plt_addr() + 6;
2020-11-18 15:45:49 +03:00
}
void PltSection::add_symbol(Symbol *sym) {
2020-11-21 06:49:28 +03:00
assert(sym->plt_idx == -1);
2020-11-18 15:45:49 +03:00
sym->plt_idx = shdr.sh_size / PLT_SIZE;
shdr.sh_size += PLT_SIZE;
symbols.push_back(sym);
if (sym->got_idx == -1) {
sym->gotplt_idx = out::gotplt->shdr.sh_size / GOT_SIZE;
out::gotplt->shdr.sh_size += GOT_SIZE;
2021-01-14 14:27:32 +03:00
sym->has_relplt = true;
2020-12-10 15:11:10 +03:00
out::relplt->shdr.sh_size += sizeof(ElfRela);
2020-11-18 15:45:49 +03:00
out::dynsym->add_symbol(sym);
2020-11-20 07:15:40 +03:00
}
2020-11-13 07:39:29 +03:00
}
2020-11-18 15:45:49 +03:00
void PltSection::copy_buf() {
u8 *buf = out::buf + shdr.sh_offset;
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));
*(u32 *)(buf + 2) = out::gotplt->shdr.sh_addr - shdr.sh_addr + 2;
*(u32 *)(buf + 8) = out::gotplt->shdr.sh_addr - shdr.sh_addr + 4;
2021-01-24 06:01:43 +03:00
i64 relplt_idx = 0;
2021-01-14 14:27:32 +03:00
2020-11-18 15:45:49 +03:00
for (Symbol *sym : symbols) {
u8 *ent = buf + sym->plt_idx * PLT_SIZE;
2020-11-21 06:18:39 +03:00
if (sym->gotplt_idx != -1) {
2020-11-18 15:45:49 +03:00
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]
};
memcpy(ent, data, sizeof(data));
*(u32 *)(ent + 2) = sym->get_gotplt_addr() - sym->get_plt_addr() - 6;
2021-01-14 14:27:32 +03:00
*(u32 *)(ent + 7) = relplt_idx++;
2020-11-18 15:45:49 +03:00
*(u32 *)(ent + 12) = shdr.sh_addr - sym->get_plt_addr() - 16;
} else {
const u8 data[] = {
0xff, 0x25, 0, 0, 0, 0, // jmp *foo@GOT
0x66, 0x66, 0x66, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nop
};
memcpy(ent, data, sizeof(data));
*(u32 *)(ent + 2) = sym->get_got_addr() - sym->get_plt_addr() - 6;
}
}
2020-11-13 06:30:27 +03:00
}
2020-11-18 15:45:49 +03:00
void RelPltSection::update_shdr() {
shdr.sh_link = out::dynsym->shndx;
2020-11-13 10:16:40 +03:00
}
2020-11-18 15:45:49 +03:00
void RelPltSection::copy_buf() {
2020-12-10 15:11:10 +03:00
ElfRela *buf = (ElfRela *)(out::buf + shdr.sh_offset);
2020-11-21 06:18:39 +03:00
memset(buf, 0, 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
2020-11-18 15:45:49 +03:00
for (Symbol *sym : out::plt->symbols) {
2021-01-14 14:27:32 +03:00
if (!sym->has_relplt)
2020-11-18 15:45:49 +03:00
continue;
2020-11-18 09:35:42 +03:00
2021-01-14 14:27:32 +03:00
ElfRela &rel = buf[relplt_idx++];
2020-11-18 15:45:49 +03:00
memset(&rel, 0, sizeof(rel));
2020-12-10 15:11:10 +03:00
rel.r_sym = sym->dynsym_idx;
2020-11-18 15:45:49 +03:00
rel.r_offset = sym->get_gotplt_addr();
2020-11-18 09:35:42 +03:00
2021-01-11 08:47:06 +03:00
if (sym->st_type == STT_GNU_IFUNC) {
2020-12-10 15:11:10 +03:00
rel.r_type = R_X86_64_IRELATIVE;
2020-11-18 15:45:49 +03:00
rel.r_addend = sym->get_addr();
} else {
2020-12-10 15:11:10 +03:00
rel.r_type = R_X86_64_JUMP_SLOT;
2020-11-18 15:45:49 +03:00
}
2020-11-18 09:35:42 +03:00
}
}
2020-11-17 14:22:52 +03:00
void DynsymSection::add_symbol(Symbol *sym) {
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-01-14 11:23:14 +03:00
void DynsymSection::sort_symbols() {
2021-01-23 09:01:49 +03:00
// In any ELF file, local symbols should precede global symbols.
2021-01-15 13:03:52 +03:00
auto first_global = std::stable_partition(
2021-01-23 09:01:49 +03:00
symbols.begin() + 1, symbols.end(),
2021-01-15 13:03:52 +03:00
[](Symbol *sym) { return sym->esym->st_bind == STB_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.
shdr.sh_info = first_global - symbols.begin();
// If we have .gnu.hash section, it imposes more constraints
// on the order of symbols.
if (out::gnu_hash) {
auto first_defined = std::stable_partition(
first_global, symbols.end(),
[](Symbol *sym) { return sym->is_imported || sym->esym->is_undef(); });
2021-01-24 06:01:43 +03:00
i64 num_defined = symbols.end() - first_defined;
2021-01-23 18:03:35 +03:00
out::gnu_hash->num_buckets = num_defined / out::gnu_hash->LOAD_FACTOR + 1;
2021-01-23 09:01:49 +03:00
out::gnu_hash->symoffset = first_defined - symbols.begin();
std::stable_sort(first_defined, symbols.end(), [&](Symbol *a, Symbol *b) {
2021-01-24 06:01:43 +03:00
i64 x = gnu_hash(a->name) % out::gnu_hash->num_buckets;
i64 y = gnu_hash(b->name) % out::gnu_hash->num_buckets;
2021-01-23 09:01:49 +03:00
return x < y;
});
}
2021-01-14 11:23:14 +03:00
2021-01-24 06:01:43 +03:00
for (i64 i = 1; i < symbols.size(); i++) {
2021-01-23 09:01:49 +03:00
name_indices.push_back(out::dynstr->add_string(symbols[i]->name));
symbols[i]->dynsym_idx = i;
}
2021-01-14 11:23:14 +03:00
}
2020-11-16 19:30:24 +03:00
void DynsymSection::update_shdr() {
2020-11-16 17:40:01 +03:00
shdr.sh_link = out::dynstr->shndx;
2021-01-23 09:01:49 +03:00
shdr.sh_size = sizeof(ElfSym) * symbols.size();
2020-11-16 19:30:24 +03:00
}
2020-11-17 07:56:40 +03:00
void DynsymSection::copy_buf() {
u8 *base = out::buf + shdr.sh_offset;
2020-12-10 09:10:18 +03:00
memset(base, 0, sizeof(ElfSym));
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-01-14 14:20:23 +03:00
Symbol &sym = *symbols[i];
ElfSym &esym = *(ElfSym *)(base + sym.dynsym_idx * sizeof(ElfSym));
2020-11-16 17:48:20 +03:00
memset(&esym, 0, sizeof(esym));
2021-01-14 14:20:23 +03:00
esym.st_name = name_indices[i];
esym.st_type = sym.st_type;
esym.st_bind = sym.esym->st_bind;
esym.st_size = sym.esym->st_size;
2020-11-16 17:48:20 +03:00
2021-01-14 14:31:39 +03:00
if (sym.has_copyrel) {
2020-11-26 05:59:38 +03:00
esym.st_shndx = out::copyrel->shndx;
2021-01-14 14:20:23 +03:00
esym.st_value = sym.get_addr();
} else if (sym.is_imported || sym.esym->is_undef()) {
2020-12-12 06:58:55 +03:00
esym.st_shndx = SHN_UNDEF;
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-01-14 14:20:23 +03:00
esym.st_value = sym.get_addr();
} else if (sym.st_type == STT_TLS) {
esym.st_shndx = sym.input_section->output_section->shndx;
esym.st_value = sym.get_addr() - out::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;
esym.st_value = sym.get_addr();
2020-11-16 17:48:20 +03:00
}
2021-01-10 09:38:10 +03:00
}
2020-11-16 17:58:40 +03:00
}
2020-11-16 18:10:38 +03:00
void HashSection::update_shdr() {
2021-01-24 06:01:43 +03:00
i64 header_size = 8;
i64 num_slots = out::dynsym->symbols.size();
2020-11-16 17:58:40 +03:00
shdr.sh_size = header_size + num_slots * 8;
2020-11-16 18:45:02 +03:00
shdr.sh_link = out::dynsym->shndx;
2020-11-16 17:58:40 +03:00
}
2020-11-17 07:56:40 +03:00
void HashSection::copy_buf() {
u8 *base = out::buf + shdr.sh_offset;
2020-11-16 17:58:40 +03:00
memset(base, 0, shdr.sh_size);
2021-01-24 06:01:43 +03:00
i64 num_slots = out::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-01-24 06:01:43 +03:00
for (i64 i = 1; i < out::dynsym->symbols.size(); i++) {
2021-01-23 09:01:49 +03:00
Symbol *sym = out::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;
}
}
void GnuHashSection::update_shdr() {
shdr.sh_link = out::dynsym->shndx;
2021-01-24 06:01:43 +03:00
if (i64 num_symbols = out::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-01-24 06:01:43 +03:00
i64 num_symbols = out::dynsym->symbols.size() - symoffset;
2021-01-23 09:01:49 +03:00
2021-01-23 18:03:35 +03:00
shdr.sh_size = HEADER_SIZE; // Header
shdr.sh_size += num_bloom * ELFCLASS_BITS / 8; // Bloom filter
shdr.sh_size += num_buckets * 4; // Hash buckets
shdr.sh_size += num_symbols * 4; // Hash values
2021-01-23 09:01:49 +03:00
}
void GnuHashSection::copy_buf() {
u8 *base = out::buf + shdr.sh_offset;
memset(base, 0, shdr.sh_size);
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;
std::span<Symbol *> symbols = std::span(out::dynsym->symbols).subspan(symoffset);
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-01-24 06:01:43 +03:00
for (i64 i = 1; i < hashes.size(); i++) {
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
table[i] = hashes[i] & ~(u32)1;
2020-11-16 17:58:40 +03:00
}
2020-11-16 17:48:20 +03:00
}
2020-11-07 15:53:21 +03:00
MergedSection *
2021-01-24 06:01:43 +03:00
MergedSection::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
2020-12-13 19:40:23 +03:00
void MergedSection::copy_buf() {
2020-12-13 19:52:22 +03:00
u8 *base = out::buf + shdr.sh_offset;
2021-01-22 14:52:16 +03:00
tbb::parallel_for_each(out::objs, [&](ObjectFile *file) {
for (MergeableSection *isec : file->mergeable_sections) {
if (&isec->parent != this)
continue;
// Clear padding between input sections
if (isec->padding)
memset(base + isec->offset - isec->padding, 0, isec->padding);
2021-01-24 06:01:43 +03:00
i64 offset = 0;
for (SectionFragment *frag : isec->fragments) {
if (frag->isec != isec)
continue;
2021-01-22 14:35:33 +03:00
if (frag->offset < offset)
continue;
// Clear padding between section fragments
2021-01-22 14:35:33 +03:00
if (offset < frag->offset) {
memset(base + isec->offset + offset, 0, frag->offset - offset);
offset = frag->offset;
}
2021-01-22 14:35:33 +03:00
memcpy(base + isec->offset + frag->offset,
frag->data.data(), frag->data.size());
offset += frag->data.size();
}
}
2021-01-22 14:52:16 +03:00
});
2021-01-22 10:14:49 +03:00
static Counter merged_strings("merged_strings");
2021-01-22 11:13:52 +03:00
merged_strings.inc(map.size());
2020-12-13 19:40:23 +03:00
}
2021-01-19 09:16:20 +03:00
void EhFrameSection::construct() {
2021-01-20 08:27:30 +03:00
// Remove dead FDEs and assign them offsets within their corresponding
// CIE group.
2021-01-24 06:01:43 +03:00
tbb::parallel_for((i64)0, (i64)out::objs.size(), [&](i64 i) {
2021-01-19 11:09:10 +03:00
ObjectFile *file = out::objs[i];
2021-01-24 06:01:43 +03:00
i64 count = 0;
2021-01-20 16:41:32 +03:00
2021-01-19 11:09:10 +03:00
for (CieRecord &cie : file->cies) {
2021-01-24 06:01:43 +03:00
i64 offset = 0;
2021-01-19 12:39:57 +03:00
for (FdeRecord &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 16:41:32 +03:00
2021-01-24 06:01:43 +03:00
for (i64 i = 0; i < file->sections.size(); i++) {
2021-01-20 16:41:32 +03:00
if (file->sections[i] && file->sections[i]->is_ehframe)
file->sections[i] = nullptr;
}
2021-01-19 11:09:10 +03:00
});
2021-01-20 08:27:30 +03:00
// Aggreagate CIEs.
2021-01-19 12:39:57 +03:00
cies.reserve(out::objs.size());
2021-01-19 09:16:20 +03:00
for (ObjectFile *file : out::objs)
for (CieRecord &cie : file->cies)
2021-01-19 12:39:57 +03:00
cies.push_back(&cie);
2021-01-18 15:07:31 +03:00
2021-01-21 01:15:09 +03:00
// Record the total number of FDes for .eh_frame_hdr.
for (CieRecord *cie : cies) {
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-01-20 14:47:15 +03:00
auto should_merge = [](CieRecord &a, CieRecord &b) {
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-01-19 13:24:00 +03:00
CieRecord &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-01-19 12:39:57 +03:00
shdr.sh_size = offset;
2021-01-21 02:53:07 +03:00
if (out::eh_frame_hdr)
out::eh_frame_hdr->shdr.sh_size =
out::eh_frame_hdr->HEADER_SIZE + num_fdes * 8;
2021-01-17 10:55:39 +03:00
}
void EhFrameSection::copy_buf() {
u8 *base = out::buf + shdr.sh_offset;
2021-01-21 16:25:21 +03:00
2021-01-23 18:03:35 +03:00
u8 *hdr_base = nullptr;
if (out::eh_frame_hdr)
2021-01-21 16:25:21 +03:00
hdr_base = out::buf + out::eh_frame_hdr->shdr.sh_offset;
2021-01-24 03:38:23 +03:00
auto apply_reloc = [&](EhReloc &rel, u64 loc, u64 val) {
2021-01-24 04:25:37 +03:00
if (rel.type == R_X86_64_32)
2021-01-24 03:38:23 +03:00
*(u32 *)(base + loc) = val;
2021-01-24 04:25:37 +03:00
else if (rel.type == R_X86_64_PC32)
2021-01-24 03:38:23 +03:00
*(u32 *)(base + loc) = val - shdr.sh_addr - loc;
2021-01-19 13:24:00 +03:00
else
unreachable();
};
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.
tbb::parallel_for_each(cies, [&](CieRecord *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;
if (out::eh_frame_hdr)
entry = (Entry *)(hdr_base + out::eh_frame_hdr->HEADER_SIZE) + cie->fde_idx;
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-01-20 08:27:30 +03:00
for (EhReloc &rel : cie->rels) {
2021-01-24 03:38:23 +03:00
u64 loc = cie->offset + rel.offset;
2021-01-25 04:42:38 +03:00
u64 val = rel.sym.get_addr() + 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.
for (FdeRecord &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-01-21 16:25:21 +03:00
EhReloc &rel = fde.rels[i];
2021-01-24 03:38:23 +03:00
u64 loc = fde_off + rel.offset;
2021-01-25 04:42:38 +03:00
u64 val = rel.sym.get_addr() + 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
if (out::eh_frame_hdr && i == 0) {
assert(rel.offset == 8);
2021-01-24 03:38:23 +03:00
entry->init_addr = val - out::eh_frame_hdr->shdr.sh_addr;
2021-01-21 16:25:21 +03:00
entry->fde_addr = shdr.sh_addr + fde_off - out::eh_frame_hdr->shdr.sh_addr;
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
if (out::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;
*(u32 *)(hdr_base + 4) = shdr.sh_addr - out::eh_frame_hdr->shdr.sh_addr - 4;
*(u32 *)(hdr_base + 8) = num_fdes;
// Sort .eh_frame_hdr contents
2021-01-21 16:25:21 +03:00
Entry *begin = (Entry *)(hdr_base + out::eh_frame_hdr->HEADER_SIZE);
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-01-20 14:47:15 +03:00
u64 EhFrameSection::get_addr(const Symbol &sym) {
InputSection &isec = *sym.input_section;
ObjectFile &file = *isec.file;
2021-01-22 11:13:52 +03:00
const char *section_begin = isec.get_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);
};
for (CieRecord &cie : file.cies) {
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-01-22 14:35:33 +03:00
u64 cie_addr = 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();
}
for (FdeRecord &fde : cie.fdes) {
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-01-22 14:35:33 +03:00
u64 fde_addr = 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();
}
}
Fatal() << file << ": .eh_frame has bad symbol: " << sym.name;
}
2020-11-25 11:20:48 +03:00
void CopyrelSection::add_symbol(Symbol *sym) {
assert(sym->is_imported);
2021-01-14 14:31:39 +03:00
if (sym->has_copyrel)
2020-11-25 14:35:04 +03:00
return;
2020-11-26 05:59:38 +03:00
shdr.sh_size = align_to(shdr.sh_size, shdr.sh_addralign);
2021-01-14 14:31:39 +03:00
sym->value = shdr.sh_size;
sym->has_copyrel = true;
2020-11-25 11:20:48 +03:00
shdr.sh_size += sym->esym->st_size;
symbols.push_back(sym);
out::dynsym->add_symbol(sym);
}
2020-11-27 11:32:25 +03:00
void VersymSection::update_shdr() {
2020-11-29 06:59:08 +03:00
shdr.sh_size = contents.size() * sizeof(contents[0]);
2020-11-27 11:32:25 +03:00
shdr.sh_link = out::dynsym->shndx;
}
void VersymSection::copy_buf() {
2020-11-29 05:40:57 +03:00
write_vector(out::buf + shdr.sh_offset, contents);
2020-11-27 11:32:25 +03:00
}
void VerneedSection::update_shdr() {
2020-11-29 06:59:08 +03:00
shdr.sh_size = contents.size() * sizeof(contents[0]);
2020-11-27 11:32:25 +03:00
shdr.sh_link = out::dynstr->shndx;
}
void VerneedSection::copy_buf() {
2020-11-29 05:40:57 +03:00
write_vector(out::buf + shdr.sh_offset, contents);
2020-11-27 11:32:25 +03:00
}
2021-01-15 14:14:01 +03:00
void BuildIdSection::copy_buf() {
u32 *base = (u32 *)(out::buf + shdr.sh_offset);
memset(base, 0, shdr.sh_size);
base[0] = 4; // Name size
2021-01-15 16:34:52 +03:00
base[1] = SHA256_SIZE; // Hash size
2021-01-15 14:14:01 +03:00
base[2] = NT_GNU_BUILD_ID; // Type
memcpy(base + 3, "GNU", 4); // Name string
}
2021-01-24 05:41:36 +03:00
void BuildIdSection::write_buildid(u8 *digest) {
memcpy(out::buf + shdr.sh_offset + 16, digest, SHA256_SIZE);
2021-01-15 14:14:01 +03:00
}