1
1
mirror of https://github.com/rui314/mold.git synced 2024-11-11 16:58:12 +03:00
This commit is contained in:
Rui Ueyama 2021-03-29 21:03:09 +09:00
parent beef12803e
commit a3ceb0528e
6 changed files with 34 additions and 32 deletions

11
elf.h
View File

@ -238,8 +238,7 @@ static constexpr u32 DW_EH_PE_aligned = 0x50;
struct ELF64LE {};
template <typename E>
struct ElfSym;
template <typename E> struct ElfSym;
template <>
struct ElfSym<ELF64LE> {
@ -257,7 +256,10 @@ struct ElfSym<ELF64LE> {
u64 st_size;
};
struct ElfShdr {
template <typename E> struct ElfShdr;
template<>
struct ElfShdr<ELF64LE> {
u32 sh_name;
u32 sh_type;
u64 sh_flags;
@ -270,8 +272,7 @@ struct ElfShdr {
u64 sh_entsize;
};
template <typename E>
struct ElfEhdr;
template <typename E> struct ElfEhdr;
template <>
struct ElfEhdr<ELF64LE> {

2
icf.cc
View File

@ -100,7 +100,7 @@ static void uniquify_cies(Context<E> &ctx) {
template <typename E>
static bool is_eligible(InputSection<E> &isec) {
const ElfShdr &shdr = isec.shdr;
const ElfShdr<E> &shdr = isec.shdr;
std::string_view name = isec.name;
bool is_alloc = (shdr.sh_flags & SHF_ALLOC);

View File

@ -12,7 +12,7 @@ static u64 read64be(u8 *buf) {
template <typename E>
InputSection<E>::InputSection(Context<E> &ctx, ObjectFile<E> &file,
const ElfShdr &shdr,
const ElfShdr<E> &shdr,
std::string_view name, i64 section_idx)
: file(file), shdr(shdr), name(name), section_idx(section_idx) {
auto do_uncompress = [&](std::string_view data, u64 size) {

18
mold.h
View File

@ -284,7 +284,7 @@ struct CieRecord {
template <typename E>
class InputSection {
public:
InputSection(Context<E> &ctx, ObjectFile<E> &file, const ElfShdr &shdr,
InputSection(Context<E> &ctx, ObjectFile<E> &file, const ElfShdr<E> &shdr,
std::string_view name, i64 section_idx);
void scan_relocations(Context<E> &ctx);
@ -298,7 +298,7 @@ public:
inline u64 get_addr() const;
ObjectFile<E> &file;
const ElfShdr &shdr;
const ElfShdr<E> &shdr;
OutputSection<E> *output_section = nullptr;
std::string_view name;
@ -349,7 +349,7 @@ public:
Kind kind;
bool new_page = false;
bool new_page_end = false;
ElfShdr shdr = { .sh_addralign = 1 };
ElfShdr<E> shdr = { .sh_addralign = 1 };
protected:
OutputChunk(Kind kind) : kind(kind) {}
@ -852,11 +852,11 @@ public:
InputFile(Context<E> &ctx, MemoryMappedFile<E> *mb);
InputFile() : name("<internal>") {}
std::string_view get_string(Context<E> &ctx, const ElfShdr &shdr);
std::string_view get_string(Context<E> &ctx, const ElfShdr<E> &shdr);
std::string_view get_string(Context<E> &ctx, i64 idx);
MemoryMappedFile<E> *mb;
std::span<ElfShdr> elf_sections;
std::span<ElfShdr<E>> elf_sections;
std::vector<Symbol<E> *> symbols;
std::string name;
@ -865,9 +865,9 @@ public:
std::atomic_bool is_alive = false;
protected:
template<typename T> std::span<T> get_data(Context<E> &ctx, const ElfShdr &shdr);
template<typename T> std::span<T> get_data(Context<E> &ctx, const ElfShdr<E> &shdr);
template<typename T> std::span<T> get_data(Context<E> &ctx, i64 idx);
ElfShdr *find_section(i64 type);
ElfShdr<E> *find_section(i64 type);
std::string_view shstrtab;
};
@ -932,7 +932,7 @@ private:
bool has_common_symbol;
std::string_view symbol_strtab;
const ElfShdr *symtab_sec;
const ElfShdr<E> *symtab_sec;
std::span<u32> symtab_shndx_sec;
};
@ -957,7 +957,7 @@ private:
std::vector<const ElfSym<E> *> elf_syms;
std::vector<u16> versyms;
std::string_view symbol_strtab;
const ElfShdr *symtab_sec;
const ElfShdr<E> *symtab_sec;
};
//

View File

@ -64,7 +64,7 @@ InputFile<E>::InputFile(Context<E> &ctx, MemoryMappedFile<E> *mb)
ElfEhdr<E> &ehdr = *(ElfEhdr<E> *)mb->data(ctx);
is_dso = (ehdr.e_type == ET_DYN);
ElfShdr *sh_begin = (ElfShdr *)(mb->data(ctx) + ehdr.e_shoff);
ElfShdr<E> *sh_begin = (ElfShdr<E> *)(mb->data(ctx) + ehdr.e_shoff);
// e_shnum contains the total number of sections in an object file.
// Since it is a 16-bit integer field, it's not large enough to
@ -86,7 +86,7 @@ InputFile<E>::InputFile(Context<E> &ctx, MemoryMappedFile<E> *mb)
}
template <typename E>
std::string_view InputFile<E>::get_string(Context<E> &ctx, const ElfShdr &shdr) {
std::string_view InputFile<E>::get_string(Context<E> &ctx, const ElfShdr<E> &shdr) {
u8 *begin = mb->data(ctx) + shdr.sh_offset;
u8 *end = begin + shdr.sh_size;
if (mb->data(ctx) + mb->size() < end)
@ -105,7 +105,7 @@ std::string_view InputFile<E>::get_string(Context<E> &ctx, i64 idx) {
template <typename E>
template <typename T>
std::span<T> InputFile<E>::get_data(Context<E> &ctx, const ElfShdr &shdr) {
std::span<T> InputFile<E>::get_data(Context<E> &ctx, const ElfShdr<E> &shdr) {
std::string_view view = this->get_string(ctx, shdr);
if (view.size() % sizeof(T))
Fatal(ctx) << *this << ": corrupted section";
@ -121,8 +121,8 @@ std::span<T> InputFile<E>::get_data(Context<E> &ctx, i64 idx) {
}
template <typename E>
ElfShdr *InputFile<E>::find_section(i64 type) {
for (ElfShdr &sec : elf_sections)
ElfShdr<E> *InputFile<E>::find_section(i64 type) {
for (ElfShdr<E> &sec : elf_sections)
if (sec.sh_type == type)
return &sec;
return nullptr;
@ -135,7 +135,8 @@ ObjectFile<E>::ObjectFile(Context<E> &ctx, MemoryMappedFile<E> *mb,
this->is_alive = !is_in_lib;
}
static bool is_debug_section(const ElfShdr &shdr, std::string_view name) {
template <typename E>
static bool is_debug_section(const ElfShdr<E> &shdr, std::string_view name) {
return !(shdr.sh_flags & SHF_ALLOC) &&
(name.starts_with(".debug") || name.starts_with(".zdebug"));
}
@ -144,7 +145,7 @@ template <typename E>
void ObjectFile<E>::initialize_sections(Context<E> &ctx) {
// Read sections
for (i64 i = 0; i < this->elf_sections.size(); i++) {
const ElfShdr &shdr = this->elf_sections[i];
const ElfShdr<E> &shdr = this->elf_sections[i];
if ((shdr.sh_flags & SHF_EXCLUDE) && !(shdr.sh_flags & SHF_ALLOC))
continue;
@ -203,7 +204,7 @@ void ObjectFile<E>::initialize_sections(Context<E> &ctx) {
}
// Attach relocation sections to their target sections.
for (const ElfShdr &shdr : this->elf_sections) {
for (const ElfShdr<E> &shdr : this->elf_sections) {
if (shdr.sh_type != SHT_RELA)
continue;
@ -908,7 +909,7 @@ void ObjectFile<E>::convert_common_symbols(Context<E> &ctx) {
assert(sym->esym->st_value);
auto *shdr = new ElfShdr;
auto *shdr = new ElfShdr<E>;
memset(shdr, 0, sizeof(*shdr));
shdr->sh_flags = SHF_ALLOC;
shdr->sh_type = SHT_NOBITS;
@ -1092,7 +1093,7 @@ SharedFile<E>::SharedFile(Context<E> &ctx, MemoryMappedFile<E> *mb)
template <typename E>
std::string_view SharedFile<E>::get_soname(Context<E> &ctx) {
if (ElfShdr *sec = this->find_section(SHT_DYNAMIC))
if (ElfShdr<E> *sec = this->find_section(SHT_DYNAMIC))
for (ElfDyn &dyn : this->template get_data<ElfDyn>(ctx, *sec))
if (dyn.d_tag == DT_SONAME)
return symbol_strtab.data() + dyn.d_val;
@ -1114,7 +1115,7 @@ void SharedFile<E>::parse(Context<E> &ctx) {
std::span<ElfSym<E>> esyms = this->template get_data<ElfSym<E>>(ctx, *symtab_sec);
std::span<u16> vers;
if (ElfShdr *sec = this->find_section(SHT_GNU_VERSYM))
if (ElfShdr<E> *sec = this->find_section(SHT_GNU_VERSYM))
vers = this->template get_data<u16>(ctx, *sec);
for (i64 i = first_global; i < esyms.size(); i++) {
@ -1158,7 +1159,7 @@ template <typename E>
std::vector<std::string_view> SharedFile<E>::read_verdef(Context<E> &ctx) {
std::vector<std::string_view> ret(VER_NDX_LAST_RESERVED + 1);
ElfShdr *verdef_sec = this->find_section(SHT_GNU_VERDEF);
ElfShdr<E> *verdef_sec = this->find_section(SHT_GNU_VERDEF);
if (!verdef_sec)
return ret;

View File

@ -38,8 +38,8 @@ void OutputEhdr<E>::copy_buf(Context<E> &ctx) {
hdr.e_ehsize = sizeof(ElfEhdr<E>);
hdr.e_phentsize = sizeof(ElfPhdr);
hdr.e_phnum = ctx.phdr->shdr.sh_size / sizeof(ElfPhdr);
hdr.e_shentsize = sizeof(ElfShdr);
hdr.e_shnum = ctx.shdr->shdr.sh_size / sizeof(ElfShdr);
hdr.e_shentsize = sizeof(ElfShdr<E>);
hdr.e_shnum = ctx.shdr->shdr.sh_size / sizeof(ElfShdr<E>);
hdr.e_shstrndx = ctx.shstrtab->shndx;
}
@ -49,12 +49,12 @@ void OutputShdr<E>::update_shdr(Context<E> &ctx) {
for (OutputChunk<E> *chunk : ctx.chunks)
if (chunk->kind != OutputChunk<E>::HEADER)
n++;
this->shdr.sh_size = n * sizeof(ElfShdr);
this->shdr.sh_size = n * sizeof(ElfShdr<E>);
}
template <typename E>
void OutputShdr<E>::copy_buf(Context<E> &ctx) {
ElfShdr *hdr = (ElfShdr *)(ctx.buf + this->shdr.sh_offset);
ElfShdr<E> *hdr = (ElfShdr<E> *)(ctx.buf + this->shdr.sh_offset);
hdr[0] = {};
i64 i = 1;