1
1
mirror of https://github.com/rui314/mold.git synced 2024-09-21 18:08:01 +03:00

Inline a few functions

This commit is contained in:
Rui Ueyama 2021-04-05 23:47:31 +09:00
parent 2e37f3c040
commit 59ee95a797
3 changed files with 68 additions and 58 deletions

View File

@ -11,44 +11,39 @@ static u64 read64be(u8 *buf) {
}
template <typename E>
InputSection<E>::InputSection(Context<E> &ctx, ObjectFile<E> &file,
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) {
u8 *buf = new u8[size];
unsigned long size2 = size;
if (uncompress(buf, &size2, (u8 *)&data[0], data.size()) != Z_OK)
Fatal(ctx) << file << ": " << name << ": uncompress failed";
if (size != size2)
Fatal(ctx) << file << ": " << name << ": uncompress: invalid size";
contents = {(char *)buf, size};
};
void InputSection<E>::do_uncompress(Context<E> &ctx, std::string_view data,
u64 size) {
u8 *buf = new u8[size];
unsigned long size2 = size;
if (uncompress(buf, &size2, (u8 *)&data[0], data.size()) != Z_OK)
Fatal(ctx) << file << ": " << name << ": uncompress failed";
if (size != size2)
Fatal(ctx) << file << ": " << name << ": uncompress: invalid size";
contents = {(char *)buf, size};
}
if (name.starts_with(".zdebug")) {
// Old-style compressed section
std::string_view data = file.get_string(ctx, shdr);
if (!data.starts_with("ZLIB") || data.size() <= 12)
Fatal(ctx) << file << ": " << name << ": corrupted compressed section";
// Uncompress old-style compressed section
template <typename E>
void InputSection<E>::uncompress_old_style(Context<E> &ctx) {
std::string_view data = file.get_string(ctx, shdr);
if (!data.starts_with("ZLIB") || data.size() <= 12)
Fatal(ctx) << file << ": " << name << ": corrupted compressed section";
u64 size = read64be((u8 *)&data[4]);
do_uncompress(ctx, data.substr(12), size);
}
u64 size = read64be((u8 *)&data[4]);
do_uncompress(data.substr(12), size);
} else if (shdr.sh_flags & SHF_COMPRESSED) {
// New-style compressed section
std::string_view data = file.get_string(ctx, shdr);
if (data.size() < sizeof(ElfChdr<E>))
Fatal(ctx) << file << ": " << name << ": corrupted compressed section";
// Uncompress new-style compressed section
template <typename E>
void InputSection<E>::uncompress_new_style(Context<E> &ctx) {
// New-style compressed section
std::string_view data = file.get_string(ctx, shdr);
if (data.size() < sizeof(ElfChdr<E>))
Fatal(ctx) << file << ": " << name << ": corrupted compressed section";
ElfChdr<E> &hdr = *(ElfChdr<E> *)&data[0];
if (hdr.ch_type != ELFCOMPRESS_ZLIB)
Fatal(ctx) << file << ": " << name << ": unsupported compression type";
do_uncompress(data.substr(sizeof(ElfChdr<E>)), hdr.ch_size);
} else if (shdr.sh_type != SHT_NOBITS) {
contents = file.get_string(ctx, shdr);
}
output_section =
OutputSection<E>::get_instance(name, shdr.sh_type, shdr.sh_flags);
ElfChdr<E> &hdr = *(ElfChdr<E> *)&data[0];
if (hdr.ch_type != ELFCOMPRESS_ZLIB)
Fatal(ctx) << file << ": " << name << ": unsupported compression type";
do_uncompress(ctx, data.substr(sizeof(ElfChdr<E>)), hdr.ch_size);
}
template <typename E>

40
mold.h
View File

@ -219,7 +219,18 @@ template <typename E>
class InputSection {
public:
InputSection(Context<E> &ctx, ObjectFile<E> &file, const ElfShdr<E> &shdr,
std::string_view name, i64 section_idx);
std::string_view name, i64 section_idx)
: file(file), shdr(shdr), name(name), section_idx(section_idx) {
if (name.starts_with(".zdebug"))
uncompress_old_style(ctx);
else if (shdr.sh_flags & SHF_COMPRESSED)
uncompress_new_style(ctx);
else if (shdr.sh_type != SHT_NOBITS)
contents = file.get_string(ctx, shdr);
output_section =
OutputSection<E>::get_instance(name, shdr.sh_type, shdr.sh_flags);
}
void scan_relocations(Context<E> &ctx);
void report_undefined_symbols();
@ -267,6 +278,10 @@ public:
private:
typedef enum { NONE, ERROR, COPYREL, PLT, DYNREL, BASEREL } Action;
void uncompress_old_style(Context<E> &ctx);
void uncompress_new_style(Context<E> &ctx);
void do_uncompress(Context<E> &ctx, std::string_view data, u64 size);
void dispatch(Context<E> &ctx, Action table[3][4], u16 rel_type, i64 i);
};
@ -799,8 +814,8 @@ public:
InputFile(Context<E> &ctx, MemoryMappedFile<E> *mb);
InputFile() : name("<internal>") {}
std::string_view get_string(Context<E> &ctx, const ElfShdr<E> &shdr);
std::string_view get_string(Context<E> &ctx, i64 idx);
inline std::string_view get_string(Context<E> &ctx, const ElfShdr<E> &shdr);
inline std::string_view get_string(Context<E> &ctx, i64 idx);
MemoryMappedFile<E> *mb;
std::span<ElfShdr<E>> elf_sections;
@ -1750,6 +1765,25 @@ inline std::span<ElfRel<E>> InputSection<E>::get_rels(Context<E> &ctx) const {
return file.template get_data<ElfRel<E>>(ctx, file.elf_sections[relsec_idx]);
}
template <typename E>
inline 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)
Fatal(ctx) << *this << ": shdr corrupted";
return {(char *)begin, (char *)end};
}
template <typename E>
inline std::string_view InputFile<E>::get_string(Context<E> &ctx, i64 idx) {
assert(idx < elf_sections.size());
if (elf_sections.size() <= idx)
Fatal(ctx) << *this << ": invalid section index: " << idx;
return this->get_string(ctx, elf_sections[idx]);
}
template <typename E>
inline i64 ObjectFile<E>::get_shndx(const ElfSym<E> &esym) {
assert(&elf_syms[0] <= &esym);

View File

@ -85,25 +85,6 @@ InputFile<E>::InputFile(Context<E> &ctx, MemoryMappedFile<E> *mb)
shstrtab = this->get_string(ctx, shstrtab_idx);
}
template <typename E>
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)
Fatal(ctx) << *this << ": shdr corrupted";
return {(char *)begin, (char *)end};
}
template <typename E>
std::string_view InputFile<E>::get_string(Context<E> &ctx, i64 idx) {
assert(idx < elf_sections.size());
if (elf_sections.size() <= idx)
Fatal(ctx) << *this << ": invalid section index: " << idx;
return this->get_string(ctx, elf_sections[idx]);
}
template <typename E>
template <typename T>
std::span<T> InputFile<E>::get_data(Context<E> &ctx, const ElfShdr<E> &shdr) {
@ -212,7 +193,7 @@ void ObjectFile<E>::initialize_sections(Context<E> &ctx) {
if (shdr.sh_info >= sections.size())
Fatal(ctx) << *this << ": invalid relocated section index: "
<< (u32)shdr.sh_info;
<< (u32)shdr.sh_info;
if (InputSection<E> *target = sections[shdr.sh_info]) {
assert(target->relsec_idx == -1);