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-11-06 14:58:10 +03:00
|
|
|
void OutputEhdr::copy_to(u8 *buf) {
|
2020-11-06 15:18:43 +03:00
|
|
|
auto &hdr = *(ELF64LE::Ehdr *)buf;
|
|
|
|
memset(&hdr, 0, sizeof(hdr));
|
|
|
|
|
|
|
|
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_ident[EI_OSABI] = 0;
|
|
|
|
hdr.e_ident[EI_ABIVERSION] = 0;
|
|
|
|
hdr.e_type = ET_EXEC;
|
|
|
|
hdr.e_machine = EM_X86_64;
|
|
|
|
hdr.e_version = EV_CURRENT;
|
|
|
|
hdr.e_entry = Symbol::intern("_start")->get_addr();
|
2020-11-09 04:44:31 +03:00
|
|
|
hdr.e_phoff = out::phdr.shdr.sh_offset;
|
|
|
|
hdr.e_shoff = out::shdr.shdr.sh_offset;
|
2020-11-06 15:18:43 +03:00
|
|
|
hdr.e_flags = 0;
|
|
|
|
hdr.e_ehsize = sizeof(ELF64LE::Ehdr);
|
|
|
|
hdr.e_phentsize = sizeof(ELF64LE::Phdr);
|
2020-11-09 04:44:31 +03:00
|
|
|
hdr.e_phnum = out::phdr.shdr.sh_size / sizeof(ELF64LE::Phdr);
|
2020-11-06 15:18:43 +03:00
|
|
|
hdr.e_shentsize = sizeof(ELF64LE::Shdr);
|
2020-11-09 04:44:31 +03:00
|
|
|
hdr.e_shnum = out::shdr.entries.size();
|
|
|
|
hdr.e_shstrndx = out::shstrtab.shndx;
|
2020-10-14 10:27:45 +03:00
|
|
|
}
|
|
|
|
|
2020-11-06 14:58:10 +03:00
|
|
|
void OutputPhdr::copy_to(u8 *buf) {
|
2020-11-03 14:26:22 +03:00
|
|
|
for (Entry &ent : entries) {
|
2020-10-25 06:57:23 +03:00
|
|
|
OutputChunk *front = ent.members.front();
|
|
|
|
OutputChunk *back = ent.members.back();
|
|
|
|
|
2020-10-26 06:09:37 +03:00
|
|
|
ent.phdr.p_offset = front->shdr.sh_offset;
|
2020-11-03 14:37:27 +03:00
|
|
|
ent.phdr.p_filesz = (back->shdr.sh_type == SHT_NOBITS)
|
2020-11-02 08:43:36 +03:00
|
|
|
? back->shdr.sh_offset - front->shdr.sh_offset
|
2020-11-03 14:13:03 +03:00
|
|
|
: back->shdr.sh_offset - front->shdr.sh_offset + back->shdr.sh_size;
|
2020-10-26 06:09:37 +03:00
|
|
|
ent.phdr.p_vaddr = front->shdr.sh_addr;
|
2020-10-25 11:37:24 +03:00
|
|
|
ent.phdr.p_memsz =
|
2020-11-03 14:13:03 +03:00
|
|
|
back->shdr.sh_addr + back->shdr.sh_size - front->shdr.sh_addr;
|
2020-10-25 06:57:23 +03:00
|
|
|
}
|
|
|
|
|
2020-10-26 06:09:37 +03:00
|
|
|
auto *p = (ELF64LE::Phdr *)(buf + shdr.sh_offset);
|
2020-11-03 14:26:22 +03:00
|
|
|
for (Entry &ent : entries)
|
2020-10-25 06:57:23 +03:00
|
|
|
*p++ = ent.phdr;
|
|
|
|
}
|
|
|
|
|
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 *
|
2020-10-29 10:27:11 +03:00
|
|
|
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-10-27 06:45:59 +03:00
|
|
|
if (name == osec->name && flags == (osec->shdr.sh_flags & ~SHF_GROUP) &&
|
|
|
|
type == osec->shdr.sh_type)
|
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-10-27 06:45:59 +03:00
|
|
|
return new OutputSection(name, flags, type);
|
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) {
|
|
|
|
if (shdr.sh_type != llvm::ELF::SHT_NOBITS)
|
|
|
|
tbb::parallel_for_each(members, [&](InputChunk *mem) { mem->copy_to(buf); });
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OutputSection::empty() const {
|
|
|
|
if (!members.empty())
|
|
|
|
for (InputChunk *mem : members)
|
|
|
|
if (mem->shdr.sh_size)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|