From e2186926728dbef0c8b3aedbd2bfa51d3ea88c84 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Thu, 14 Oct 2021 20:54:28 +0900 Subject: [PATCH] [Mach-O] wip --- macho/main.cc | 9 ++++----- macho/mold.h | 7 +++---- macho/output-chunks.cc | 25 +++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/macho/main.cc b/macho/main.cc index 91d69ed9..c4139665 100644 --- a/macho/main.cc +++ b/macho/main.cc @@ -38,11 +38,10 @@ static void create_synthetic_chunks(Context &ctx) { ctx.padding.hdr.size = 14808; - OutputSection *text = new OutputSection("__text"); - text->hdr.attr = S_ATTR_PURE_INSTRUCTIONS | S_ATTR_SOME_INSTRUCTIONS; - text->hdr.p2align = 4; - add_section(ctx, *text, "__TEXT", "__text"); - ctx.text_seg.chunks.push_back(text); + ctx.text.hdr.attr = S_ATTR_PURE_INSTRUCTIONS | S_ATTR_SOME_INSTRUCTIONS; + ctx.text.hdr.p2align = 4; + add_section(ctx, ctx.text, "__TEXT", "__text"); + ctx.text_seg.chunks.push_back(&ctx.text); ctx.text_seg.chunks.push_back(&ctx.stubs); ctx.text_seg.chunks.push_back(&ctx.stub_helper); diff --git a/macho/mold.h b/macho/mold.h index e2317f79..d3aa4df2 100644 --- a/macho/mold.h +++ b/macho/mold.h @@ -303,14 +303,12 @@ class OutputFunctionStartsSection : public Chunk { public: OutputFunctionStartsSection() { is_hidden = true; - hdr.size = contents.size(); } + void compute_size(Context &ctx) override; void copy_buf(Context &ctx) override; - std::vector contents = { - 0xd0, 0x7e, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00 - }; + std::vector contents; }; class OutputSymtabSection : public Chunk { @@ -519,6 +517,7 @@ struct Context { OutputSymtabSection symtab; OutputIndirectSymtabSection indir_symtab; OutputStrtabSection strtab; + OutputSection text{"__text"}; std::vector segments; }; diff --git a/macho/output-chunks.cc b/macho/output-chunks.cc index 3dbeed13..59f3b50d 100644 --- a/macho/output-chunks.cc +++ b/macho/output-chunks.cc @@ -544,6 +544,31 @@ void OutputExportSection::copy_buf(Context &ctx) { enc.write_trie(ctx.buf + hdr.offset); } +void OutputFunctionStartsSection::compute_size(Context &ctx) { + std::vector addrs; + + for (ObjectFile *obj : ctx.objs) + for (Symbol *sym : obj->syms) + if (sym->file == obj) + if (sym->subsec->isec.osec == &ctx.text) + addrs.push_back(sym->get_addr(ctx)); + + std::sort(addrs.begin(), addrs.end()); + + contents.resize(addrs.size() * 5); + + u8 *p = contents.data(); + u64 last = PAGE_ZERO_SIZE; + + for (u64 val : addrs) { + p += write_uleb(p, val - last); + last = val; + } + + hdr.size = p - contents.data(); + contents.resize(hdr.size); +} + void OutputFunctionStartsSection::copy_buf(Context &ctx) { write_vector(ctx.buf + hdr.offset, contents); }