From 0542a2254b5ce0aac4a32f17b46f22ed25fec2b7 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Fri, 4 Feb 2022 16:49:54 +0900 Subject: [PATCH] Refactor --- elf/input-files.cc | 12 ++++++------ elf/mold.h | 4 ++-- elf/output-chunks.cc | 19 +++++++------------ 3 files changed, 15 insertions(+), 20 deletions(-) diff --git a/elf/input-files.cc b/elf/input-files.cc index 6ef10bbe..b31c4c06 100644 --- a/elf/input-files.cc +++ b/elf/input-files.cc @@ -1092,14 +1092,14 @@ void ObjectFile::compute_symtab(Context &ctx) { template void ObjectFile::write_symtab(Context &ctx) { - u8 *symtab_base = ctx.buf + ctx.symtab->shdr.sh_offset; + ElfSym *symtab_base = (ElfSym *)(ctx.buf + ctx.symtab->shdr.sh_offset); + i64 symtab_idx; + u8 *strtab_base = ctx.buf + ctx.strtab->shdr.sh_offset; i64 strtab_off = strtab_offset; - i64 symtab_off; auto write_sym = [&](Symbol &sym) { - ElfSym &esym = *(ElfSym *)(symtab_base + symtab_off); - symtab_off += sizeof(esym); + ElfSym &esym = symtab_base[symtab_idx++]; esym = sym.esym(); esym.st_name = strtab_off; @@ -1122,14 +1122,14 @@ void ObjectFile::write_symtab(Context &ctx) { strtab_off += sym.name().size() + 1; }; - symtab_off = local_symtab_offset; + symtab_idx = local_symtab_idx; for (i64 i = 1; i < this->first_global; i++) { Symbol &sym = *this->symbols[i]; if (sym.write_to_symtab) write_sym(sym); } - symtab_off = global_symtab_offset; + symtab_idx = global_symtab_idx; for (i64 i = this->first_global; i < this->elf_syms.size(); i++) { Symbol &sym = *this->symbols[i]; if (sym.file == this && sym.write_to_symtab) diff --git a/elf/mold.h b/elf/mold.h index 214bc53e..8b4afc00 100644 --- a/elf/mold.h +++ b/elf/mold.h @@ -1007,8 +1007,8 @@ public: u64 num_dynrel = 0; u64 reldyn_offset = 0; - u64 local_symtab_offset = 0; - u64 global_symtab_offset = 0; + u64 local_symtab_idx = 0; + u64 global_symtab_idx = 0; u64 num_local_symtab = 0; u64 num_global_symtab = 0; u64 strtab_offset = 0; diff --git a/elf/output-chunks.cc b/elf/output-chunks.cc index 2434bf92..c96fbb0c 100644 --- a/elf/output-chunks.cc +++ b/elf/output-chunks.cc @@ -458,26 +458,21 @@ void DynstrSection::copy_buf(Context &ctx) { template void SymtabSection::update_shdr(Context &ctx) { - this->shdr.sh_size = sizeof(ElfSym); + i64 nsyms = 1; for (ObjectFile *file : ctx.objs) { - file->local_symtab_offset = this->shdr.sh_size; - this->shdr.sh_size += file->num_local_symtab * sizeof(ElfSym); + file->local_symtab_idx = nsyms; + nsyms += file->num_local_symtab; } for (ObjectFile *file : ctx.objs) { - file->global_symtab_offset = this->shdr.sh_size; - this->shdr.sh_size += file->num_global_symtab * sizeof(ElfSym); + file->global_symtab_idx = nsyms; + nsyms += file->num_global_symtab; } - this->shdr.sh_info = ctx.objs[0]->global_symtab_offset / sizeof(ElfSym); + this->shdr.sh_info = ctx.objs[0]->global_symtab_idx; this->shdr.sh_link = ctx.strtab->shndx; - - if (this->shdr.sh_size == sizeof(ElfSym)) - this->shdr.sh_size = 0; - - static Counter counter("symtab"); - counter += this->shdr.sh_size / sizeof(ElfSym); + this->shdr.sh_size = (nsyms == 1) ? 0 : nsyms * sizeof(ElfSym); } template