From 33c7719838e633ba8c463fcd802395e3b747f8ba Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Mon, 12 Apr 2021 20:18:43 +0900 Subject: [PATCH] wip --- gc_sections.cc | 8 ++++---- icf.cc | 2 +- input_sections.cc | 10 +++++----- mold.h | 13 ++++++++++--- object_file.cc | 6 +++--- 5 files changed, 23 insertions(+), 16 deletions(-) diff --git a/gc_sections.cc b/gc_sections.cc index 049f8a0e..5a6f3507 100644 --- a/gc_sections.cc +++ b/gc_sections.cc @@ -15,10 +15,10 @@ static bool is_init_fini(const InputSection &isec) { return isec.shdr.sh_type == SHT_INIT_ARRAY || isec.shdr.sh_type == SHT_FINI_ARRAY || isec.shdr.sh_type == SHT_PREINIT_ARRAY || - isec.name.starts_with(".ctors") || - isec.name.starts_with(".dtors") || - isec.name.starts_with(".init") || - isec.name.starts_with(".fini"); + isec.name().starts_with(".ctors") || + isec.name().starts_with(".dtors") || + isec.name().starts_with(".init") || + isec.name().starts_with(".fini"); } template diff --git a/icf.cc b/icf.cc index 3307a704..27e99171 100644 --- a/icf.cc +++ b/icf.cc @@ -101,7 +101,7 @@ static void uniquify_cies(Context &ctx) { template static bool is_eligible(InputSection &isec) { const ElfShdr &shdr = isec.shdr; - std::string_view name = isec.name; + std::string_view name = isec.name(); bool is_alloc = (shdr.sh_flags & SHF_ALLOC); bool is_executable = (shdr.sh_flags & SHF_EXECINSTR); diff --git a/input_sections.cc b/input_sections.cc index 9b4d7c9d..ec9ffa0f 100644 --- a/input_sections.cc +++ b/input_sections.cc @@ -18,9 +18,9 @@ void InputSection::do_uncompress(Context &ctx, std::string_view data, unsigned long size2 = size; if (uncompress(buf->data(), &size2, (u8 *)&data[0], data.size()) != Z_OK) - Fatal(ctx) << file << ": " << name << ": uncompress failed"; + Fatal(ctx) << file << ": " << name() << ": uncompress failed"; if (size != size2) - Fatal(ctx) << file << ": " << name << ": uncompress: invalid size"; + Fatal(ctx) << file << ": " << name() << ": uncompress: invalid size"; contents = {(char *)buf->data(), size}; } @@ -29,7 +29,7 @@ template void InputSection::uncompress_old_style(Context &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"; + Fatal(ctx) << file << ": " << name() << ": corrupted compressed section"; u64 size = read64be((u8 *)&data[4]); do_uncompress(ctx, data.substr(12), size); } @@ -40,11 +40,11 @@ void InputSection::uncompress_new_style(Context &ctx) { // New-style compressed section std::string_view data = file.get_string(ctx, shdr); if (data.size() < sizeof(ElfChdr)) - Fatal(ctx) << file << ": " << name << ": corrupted compressed section"; + Fatal(ctx) << file << ": " << name() << ": corrupted compressed section"; ElfChdr &hdr = *(ElfChdr *)&data[0]; if (hdr.ch_type != ELFCOMPRESS_ZLIB) - Fatal(ctx) << file << ": " << name << ": unsupported compression type"; + Fatal(ctx) << file << ": " << name() << ": unsupported compression type"; do_uncompress(ctx, data.substr(sizeof(ElfChdr)), hdr.ch_size); } diff --git a/mold.h b/mold.h index 32589d76..bb445545 100644 --- a/mold.h +++ b/mold.h @@ -225,7 +225,8 @@ class InputSection { public: InputSection(Context &ctx, ObjectFile &file, const ElfShdr &shdr, std::string_view name, i64 section_idx) - : file(file), shdr(shdr), name(name), section_idx(section_idx) { + : file(file), shdr(shdr), nameptr(name.data()), namelen(name.size()), + section_idx(section_idx) { if (name.starts_with(".zdebug")) uncompress_old_style(ctx); else if (shdr.sh_flags & SHF_COMPRESSED) @@ -244,6 +245,10 @@ public: void apply_reloc_nonalloc(Context &ctx, u8 *base); inline void kill(); + inline std::string_view name() const { + return {nameptr, (size_t)namelen}; + } + inline i64 get_priority() const; inline u64 get_addr() const; inline i64 get_addend(const ElfRel &rel) const; @@ -253,13 +258,15 @@ public: const ElfShdr &shdr; OutputSection *output_section = nullptr; - std::string_view name; std::string_view contents; std::unique_ptr[]> rel_fragments; std::unique_ptr rel_types; std::span> fdes; + const char *nameptr = nullptr; + i32 namelen = 0; + u32 offset = -1; u32 section_idx = -1; u32 relsec_idx = -1; @@ -1800,7 +1807,7 @@ std::ostream &operator<<(std::ostream &out, const InputFile &file); template inline std::ostream & operator<<(std::ostream &out, const InputSection &isec) { - out << isec.file << ":(" << isec.name << ")"; + out << isec.file << ":(" << isec.name() << ")"; return out; } diff --git a/object_file.cc b/object_file.cc index 3f3ad3af..19ee1fcb 100644 --- a/object_file.cc +++ b/object_file.cc @@ -242,7 +242,7 @@ template void ObjectFile::initialize_ehframe_sections(Context &ctx) { for (i64 i = 0; i < sections.size(); i++) { std::unique_ptr> &isec = sections[i]; - if (isec && isec->is_alive && isec->name == ".eh_frame") { + if (isec && isec->is_alive && isec->name() == ".eh_frame") { read_ehframe(ctx, *isec); isec->is_ehframe = true; isec->is_alive = false; @@ -427,7 +427,7 @@ void ObjectFile::initialize_symbols(Context &ctx) { if (name.empty() && esym.st_type == STT_SECTION) if (InputSection *sec = get_section(esym)) - name = sec->name; + name = sec->name(); Symbol &sym = this->local_syms[i]; new (&sym) Symbol(name); @@ -519,7 +519,7 @@ split_section(Context &ctx, InputSection &sec) { MergeableSection rec; MergedSection *parent = - MergedSection::get_instance(ctx, sec.name, sec.shdr.sh_type, + MergedSection::get_instance(ctx, sec.name(), sec.shdr.sh_type, sec.shdr.sh_flags); std::string_view data = sec.contents;