From 94cda252778b0ce0dc513b0e9df31ddd9c8f1209 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Sun, 24 Oct 2021 00:40:07 +0900 Subject: [PATCH] [Mach-O] wip --- macho/main.cc | 13 ------------- macho/mold.h | 3 +-- macho/output-chunks.cc | 21 +++++++++++++++++---- 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/macho/main.cc b/macho/main.cc index 9593b50f..823437eb 100644 --- a/macho/main.cc +++ b/macho/main.cc @@ -91,18 +91,6 @@ static void create_synthetic_chunks(Context &ctx) { ctx.linkedit_seg.chunks.push_back(&ctx.strtab); } -static void fill_symtab(Context &ctx) { - for (ObjectFile *obj : ctx.objs) - for (Symbol *sym : obj->syms) - if (sym->file == obj) - ctx.symtab.add(ctx, sym); - - for (DylibFile *dylib : ctx.dylibs) - for (Symbol *sym : dylib->syms) - if (sym->file == dylib && sym->needs_stub) - ctx.symtab.add(ctx, sym); -} - static void export_symbols(Context &ctx) { std::vector syms; @@ -220,7 +208,6 @@ int main(int argc, char **argv) { for (std::unique_ptr &sec : obj->sections) sec->scan_relocations(ctx); - fill_symtab(ctx); export_symbols(ctx); i64 output_size = assign_offsets(ctx); fix_synthetic_symbol_values(ctx); diff --git a/macho/mold.h b/macho/mold.h index 045c9353..8066b986 100644 --- a/macho/mold.h +++ b/macho/mold.h @@ -349,8 +349,7 @@ public: hdr.p2align = __builtin_ctz(8); } - void add(Context &ctx, Symbol *sym); - + void compute_size(Context &ctx) override; void copy_buf(Context &ctx) override; std::vector syms; diff --git a/macho/output-chunks.cc b/macho/output-chunks.cc index 34289785..6af838f2 100644 --- a/macho/output-chunks.cc +++ b/macho/output-chunks.cc @@ -579,10 +579,23 @@ void OutputFunctionStartsSection::copy_buf(Context &ctx) { write_vector(ctx.buf + hdr.offset, contents); } -void OutputSymtabSection::add(Context &ctx, Symbol *sym) { - hdr.size += sizeof(MachSym); - syms.push_back(sym); - stroffs.push_back(ctx.strtab.add_string(sym->name)); +void OutputSymtabSection::compute_size(Context &ctx) { + auto add = [&](Symbol *sym) { + syms.push_back(sym); + stroffs.push_back(ctx.strtab.add_string(sym->name)); + }; + + for (ObjectFile *obj : ctx.objs) + for (Symbol *sym : obj->syms) + if (sym->file == obj) + add(sym); + + for (DylibFile *dylib : ctx.dylibs) + for (Symbol *sym : dylib->syms) + if (sym->file == dylib && sym->needs_stub) + add(sym); + + hdr.size = syms.size() * sizeof(MachSym); } void OutputSymtabSection::copy_buf(Context &ctx) {