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

230 lines
6.3 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-10-16 10:38:03 +03:00
using namespace llvm::ELF;
2020-10-22 10:35:17 +03:00
static StringRef get_output_name(StringRef name) {
static StringRef common_names[] = {
".text.", ".data.rel.ro.", ".data.", ".rodata.", ".bss.rel.ro.",
".bss.", ".init_array.", ".fini_array.", ".tbss.", ".tdata.",
};
for (StringRef s : common_names)
if (name.startswith(s) || name == s.drop_back())
return s.drop_back();
return name;
}
2020-10-27 06:45:59 +03:00
OutputSection *
OutputSection::get_instance(StringRef 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-09 15:32:13 +03:00
void OutputSection::copy_to(u8 *buf) {
2020-11-09 16:25:17 +03:00
if (shdr.sh_type == llvm::ELF::SHT_NOBITS)
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
members[i]->copy_to(buf);
// 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;
memset(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
}
bool OutputSection::empty() const {
if (!members.empty())
for (InputChunk *mem : members)
if (mem->shdr.sh_size)
return false;
return true;
}
2020-11-13 07:39:29 +03:00
void GotPltSection::initialize(u8 *buf) {
u8 *base = buf + shdr.sh_offset;
memset(base, 0, shdr.sh_size);
if (out::dynamic)
*(u64 *)base = out::dynamic->shdr.sh_addr;
}
2020-11-13 06:30:27 +03:00
void PltSection::initialize(u8 *buf) {
const u8 data[] = {
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
};
u8 *base = buf + shdr.sh_offset;
memcpy(base, data, sizeof(data));
*(u32 *)(base + 2) = out::gotplt->shdr.sh_addr - shdr.sh_addr + 2;
*(u32 *)(base + 8) = out::gotplt->shdr.sh_addr - shdr.sh_addr + 4;
}
2020-11-13 10:16:40 +03:00
void PltSection::write_entry(u8 *buf, Symbol *sym) {
2020-11-15 08:07:40 +03:00
u8 *base = buf + shdr.sh_offset + sym->file->plt_offset + sym->plt_idx * PLT_SIZE;
2020-11-15 07:01:38 +03:00
if (sym->got_idx == -1) {
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(base, data, sizeof(data));
*(u32 *)(base + 2) = sym->get_gotplt_addr() - sym->get_plt_addr() - 6;
2020-11-16 07:29:50 +03:00
*(u32 *)(base + 7) = sym->file->relplt_offset / sizeof(ELF64LE::Rela) +
sym->relplt_idx;
2020-11-15 07:01:38 +03:00
*(u32 *)(base + 12) = shdr.sh_addr - sym->get_plt_addr() - 16;
} else {
const u8 data[] = {
2020-11-15 08:07:40 +03:00
0xff, 0x25, 0, 0, 0, 0, // jmp *foo@GOTPLT
2020-11-15 07:01:38 +03:00
0x66, 0x66, 0x66, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nop
};
memcpy(base, data, sizeof(data));
*(u32 *)(base + 2) = sym->get_got_addr() - sym->get_plt_addr() - 6;
}
2020-11-13 10:16:40 +03:00
}
2020-11-16 17:48:20 +03:00
void DynsymSection::add_symbols(ArrayRef<Symbol *> syms) {
for (Symbol *sym : syms) {
sym->dynsym_idx = symbols.size() + 1;
symbols.push_back(sym);
sym->dynstr_offset = out::dynstr->shdr.sh_size;
out::dynstr->shdr.sh_size += sym->name.size() + 1;
}
shdr.sh_size = (symbols.size() + 1) * sizeof(ELF64LE::Sym);
}
2020-11-16 17:40:01 +03:00
void DynsymSection::initialize(u8 *buf) {
shdr.sh_link = out::dynstr->shndx;
memset(buf + shdr.sh_offset, 0, sizeof(ELF64LE::Sym));
}
2020-11-16 17:48:20 +03:00
void DynsymSection::copy_to(u8 *buf) {
u8 *dynsym_buf = buf + shdr.sh_offset;
u8 *dynstr_buf = buf + out::dynstr->shdr.sh_offset;
tbb::parallel_for_each(symbols, [&](Symbol *sym) {
// Write to .dynsym
auto &esym = *(ELF64LE::Sym *)(dynsym_buf + sym->dynsym_idx * sizeof(ELF64LE::Sym));
memset(&esym, 0, sizeof(esym));
esym.st_name = sym->dynstr_offset;
esym.setType(sym->type);
esym.setBinding(sym->esym->getBinding());
if (sym->file->is_dso || sym->esym->isUndefined()) {
esym.st_shndx = SHN_UNDEF;
} else if (!sym->input_section) {
esym.st_shndx = SHN_ABS;
esym.st_value = sym->get_addr();
} else {
esym.st_shndx = sym->input_section->output_section->shndx;
esym.st_value = sym->get_addr();
}
// Write to .dynstr
write_string(dynstr_buf + sym->dynstr_offset, sym->name);
});
2020-11-16 17:58:40 +03:00
}
void HashSection::update_size() {
int header_size = 8;
int num_slots = out::dynsym->symbols.size() + 1;
shdr.sh_size = header_size + num_slots * 8;
}
void HashSection::copy_to(u8 *buf) {
u8 *base = buf + shdr.sh_offset;
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) {
u32 i = hash(sym->name) % num_slots;
chains[sym->dynsym_idx] = buckets[i];
buckets[i] = sym->dynsym_idx;
}
2020-11-16 17:48:20 +03:00
}
2020-11-16 17:58:40 +03:00
u32 HashSection::hash(StringRef name) {
u32 h = 0;
for (char c : name) {
h = (h << 4) + c;
u32 g = h & 0xf0000000;
if (g != 0)
h ^= g >> 24;
h &= ~g;
}
return h;
2020-11-15 11:01:52 +03:00
}
2020-11-07 15:53:21 +03:00
MergedSection *
MergedSection::get_instance(StringRef 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;
}