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

660 lines
18 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>
2020-11-17 07:56:40 +03:00
void OutputEhdr::copy_buf() {
2020-12-10 15:13:02 +03:00
auto &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;
hdr.e_type = ET_EXEC;
hdr.e_machine = EM_X86_64;
hdr.e_version = EV_CURRENT;
hdr.e_entry = Symbol::intern("_start")->get_addr();
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() {
2020-12-10 09:59:24 +03:00
shdr.sh_size = sizeof(ElfShdr);
2020-11-16 18:23:51 +03:00
for (OutputChunk *chunk : out::chunks)
if (chunk->kind != OutputChunk::HEADER)
2020-12-10 09:59:24 +03:00
shdr.sh_size += sizeof(ElfShdr);
2020-11-16 18:23:51 +03:00
}
2020-11-17 07:56:40 +03:00
void OutputShdr::copy_buf() {
u8 *base = out::buf + shdr.sh_offset;
2020-11-16 18:23:51 +03:00
2020-12-10 09:59:24 +03:00
memset(base, 0, sizeof(ElfShdr));
2020-11-16 18:23:51 +03:00
2020-12-10 09:59:24 +03:00
auto *ptr = (ElfShdr *)(base + sizeof(ElfShdr));
2020-11-16 18:23:51 +03:00
for (OutputChunk *chunk : out::chunks)
if (chunk->kind != OutputChunk::HEADER)
*ptr++ = chunk->shdr;
}
2020-11-16 18:33:41 +03:00
static u32 to_phdr_flags(OutputChunk *chunk) {
u32 ret = PF_R;
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
auto define = [&](u32 type, u32 flags, u32 align, OutputChunk *chunk) {
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;
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);
// Create an PT_INTERP.
if (out::interp)
define(PT_INTERP, PF_R, 1, out::interp);
// Create PT_LOAD segments.
for (int i = 0, end = out::chunks.size(); i < end;) {
OutputChunk *first = out::chunks[i++];
if (!(first->shdr.sh_flags & SHF_ALLOC))
break;
u32 flags = to_phdr_flags(first);
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.
for (int i = 0; i < out::chunks.size(); i++) {
if (out::chunks[i]->shdr.sh_flags & SHF_TLS) {
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++]);
}
}
// Add PT_DYNAMIC
if (out::dynamic)
define(PT_DYNAMIC, PF_R | PF_W, out::dynamic->shdr.sh_addralign, out::dynamic);
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
2020-11-25 11:20:48 +03:00
int n = 0;
2020-11-20 07:15:40 +03:00
for (Symbol *sym : out::got->got_syms)
if (sym->is_imported)
2020-11-25 11:20:48 +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::got->tlsld_syms.size();
n += out::got->tlsld_syms.size();
n += out::copyrel->symbols.size();
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-11-25 07:28:29 +03:00
auto write = [&](Symbol *sym, u8 type, u64 offset) {
2020-11-20 07:15:40 +03:00
memset(rel, 0, sizeof(*rel));
2020-12-10 15:11:10 +03:00
rel->r_sym = sym->dynsym_idx;
rel->r_type = type;
2020-11-25 07:28:29 +03:00
rel->r_offset = offset;
2020-11-20 07:15:40 +03:00
rel++;
2020-11-25 07:28:29 +03:00
};
2020-11-21 05:51:21 +03:00
2020-11-25 07:28:29 +03:00
for (Symbol *sym : out::got->got_syms)
if (sym->is_imported)
write(sym, R_X86_64_GLOB_DAT, sym->get_got_addr());
2020-11-21 05:51:21 +03:00
2020-11-25 07:28:29 +03:00
for (Symbol *sym : out::got->tlsgd_syms) {
write(sym, R_X86_64_DTPMOD64, sym->get_tlsgd_addr());
write(sym, R_X86_64_DTPOFF64, sym->get_tlsgd_addr() + GOT_SIZE);
2020-11-21 05:51:21 +03:00
}
2020-11-25 07:28:29 +03:00
for (Symbol *sym : out::got->tlsld_syms)
write(sym, R_X86_64_DTPMOD64, sym->get_tlsld_addr());
for (Symbol *sym : out::got->gottpoff_syms)
if (sym->is_imported)
write(sym, R_X86_64_TPOFF32, sym->get_gottpoff_addr());
2020-11-25 11:20:48 +03:00
for (Symbol *sym : out::copyrel->symbols)
write(sym, R_X86_64_COPY, sym->get_addr());
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';
2020-11-17 07:19:54 +03:00
int 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
}
}
}
2020-12-10 07:44:58 +03:00
u32 DynstrSection::add_string(std::string_view str) {
2020-11-17 07:19:54 +03:00
u32 ret = shdr.sh_size;
shdr.sh_size += str.size() + 1;
contents.push_back(str);
return ret;
}
2020-12-10 07:44:58 +03:00
u32 DynstrSection::find_string(std::string_view str) {
2020-11-30 11:52:08 +03:00
u32 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';
int 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;
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
2020-12-10 07:44:58 +03:00
for (std::string_view path : config.rpaths)
2020-11-30 11:57:58 +03:00
define(DT_RUNPATH, out::dynstr->find_string(path));
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_HASH, out::hash->shdr.sh_addr);
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
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);
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-10 07:44:58 +03:00
for (std::string_view s : common_names)
if (name.starts_with(s) || name == s.substr(0, s.size() - 1))
return s.substr(0, s.size() - 1);
2020-10-22 10:35:17 +03:00
return name;
}
2020-10-27 06:45:59 +03:00
OutputSection *
2020-12-10 07:44:58 +03:00
OutputSection::get_instance(std::string_view name, u64 flags, u32 type) {
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;
2020-11-10 06:23:14 +03:00
int num_members = members.size();
tbb::parallel_for(0, num_members, [&](int i) {
if (members[i]->shdr.sh_type != SHT_NOBITS) {
// Copy section contents to an output file
2020-11-17 07:56:40 +03:00
members[i]->copy_buf();
2020-11-10 06:23:14 +03:00
// Zero-clear trailing padding
u64 this_end = members[i]->offset + members[i]->shdr.sh_size;
u64 next_start = (i == num_members - 1) ? shdr.sh_size : members[i + 1]->offset;
2020-11-17 07:56:40 +03:00
memset(out::buf + shdr.sh_offset + this_end, 0, next_start - this_end);
2020-11-10 06:23:14 +03:00
}
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-20 07:14:00 +03:00
if (sym->is_imported)
2020-11-18 14:29:24 +03:00
out::dynsym->add_symbol(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-25 07:28:29 +03:00
if (sym->is_imported)
out::dynsym->add_symbol(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);
2020-11-21 05:51:21 +03:00
if (sym->is_imported)
out::dynsym->add_symbol(sym);
2020-11-21 04:48:23 +03:00
}
void GotSection::add_tlsld_symbol(Symbol *sym) {
2020-11-21 06:49:28 +03:00
assert(sym->tlsld_idx == -1);
2020-11-21 04:48:23 +03:00
sym->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
tlsld_syms.push_back(sym);
2020-11-21 05:51:21 +03:00
if (sym->is_imported)
out::dynsym->add_symbol(sym);
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;
2020-12-10 15:11:10 +03:00
sym->relplt_idx = out::relplt->shdr.sh_size / sizeof(ElfRela);
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;
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;
*(u32 *)(ent + 7) = sym->relplt_idx;
*(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
for (Symbol *sym : out::plt->symbols) {
if (sym->relplt_idx == -1)
continue;
2020-11-18 09:35:42 +03:00
2020-12-10 15:11:10 +03:00
ElfRela &rel = buf[sym->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
2020-11-18 15:45:49 +03:00
if (sym->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) {
if (sym->dynsym_idx == -1) {
2020-11-16 17:48:20 +03:00
sym->dynsym_idx = symbols.size() + 1;
2020-11-17 07:19:54 +03:00
sym->dynstr_offset = out::dynstr->add_string(sym->name);
2020-11-16 17:48:20 +03:00
symbols.push_back(sym);
}
}
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;
2020-12-10 09:10:18 +03:00
shdr.sh_size = sizeof(ElfSym) * (symbols.size() + 1);
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
tbb::parallel_for_each(symbols, [&](Symbol *sym) {
2020-12-10 09:10:18 +03:00
auto &esym = *(ElfSym *)(base + sym->dynsym_idx * sizeof(ElfSym));
2020-11-16 17:48:20 +03:00
memset(&esym, 0, sizeof(esym));
esym.st_name = sym->dynstr_offset;
2020-12-10 09:31:54 +03:00
esym.st_type = sym->type;
esym.st_bind = sym->esym->st_bind;
2020-11-25 11:20:48 +03:00
esym.st_size = sym->esym->st_size;
2020-11-16 17:48:20 +03:00
2020-11-26 05:59:38 +03:00
if (sym->copyrel_offset != -1) {
esym.st_shndx = out::copyrel->shndx;
esym.st_value = sym->get_addr();
2020-12-10 09:31:54 +03:00
} else if (sym->is_imported || sym->esym->is_undef()) {
2020-12-12 06:58:55 +03:00
esym.st_shndx = SHN_UNDEF;
2020-11-16 17:48:20 +03:00
} else if (!sym->input_section) {
2020-12-12 06:58:55 +03:00
esym.st_shndx = SHN_ABS;
2020-11-16 17:48:20 +03:00
esym.st_value = sym->get_addr();
} else {
esym.st_shndx = sym->input_section->output_section->shndx;
esym.st_value = sym->get_addr();
}
});
2020-11-16 17:58:40 +03:00
}
2020-11-16 18:10:38 +03:00
void HashSection::update_shdr() {
2020-11-16 17:58:40 +03:00
int header_size = 8;
int num_slots = out::dynsym->symbols.size() + 1;
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);
int num_slots = out::dynsym->symbols.size() + 1;
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
2020-11-16 17:58:40 +03:00
for (Symbol *sym : out::dynsym->symbols) {
2020-11-29 06:59:08 +03:00
u32 i = elf_hash(sym->name) % num_slots;
2020-11-16 17:58:40 +03:00
chains[sym->dynsym_idx] = buckets[i];
buckets[i] = sym->dynsym_idx;
}
2020-11-16 17:48:20 +03:00
}
2020-11-07 15:53:21 +03:00
MergedSection *
2020-12-10 07:44:58 +03:00
MergedSection::get_instance(std::string_view name, u64 flags, u32 type) {
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)
2020-11-07 04:13:19 +03:00
if (name == osec->name && flags == osec->shdr.sh_flags &&
type == osec->shdr.sh_type)
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
void CopyrelSection::add_symbol(Symbol *sym) {
assert(sym->is_imported);
2020-11-25 14:35:04 +03:00
if (sym->copyrel_offset != -1)
return;
2020-11-26 05:59:38 +03:00
shdr.sh_size = align_to(shdr.sh_size, shdr.sh_addralign);
sym->copyrel_offset = shdr.sh_size;
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
}