From 2ec3b16ffd817a7a5f01a191bd6a46b8d25c0329 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Wed, 27 Oct 2021 20:53:27 +0900 Subject: [PATCH] [Mach-O] wip --- macho/main.cc | 29 +++++++++++++++-------------- macho/mold.h | 2 ++ 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/macho/main.cc b/macho/main.cc index c955acce..4d932422 100644 --- a/macho/main.cc +++ b/macho/main.cc @@ -33,13 +33,21 @@ static void create_internal_file(Context &ctx) { } static void create_synthetic_chunks(Context &ctx) { - for (ObjectFile *obj : ctx.objs) - for (std::unique_ptr &sec : obj->sections) - for (Subsection &subsec : sec->subsections) - sec->osec.members.push_back(&subsec); + for (ObjectFile *obj : ctx.objs) { + for (std::unique_ptr &isec : obj->sections) { + for (Subsection &subsec : isec->subsections) + isec->osec.members.push_back(&subsec); + isec->osec.hdr.attr |= isec->hdr.attr; + isec->osec.hdr.p2align = + std::max(isec->osec.hdr.p2align, isec->hdr.p2align); + } + } ctx.text = OutputSection::get_instance(ctx, "__TEXT", "__text"); ctx.data = OutputSection::get_instance(ctx, "__DATA", "__data"); + ctx.cstring = OutputSection::get_instance(ctx, "__TEXT", "__cstring"); + + ctx.cstring->hdr.type = S_CSTRING_LITERALS; ctx.segments.push_back(&ctx.text_seg); ctx.segments.push_back(&ctx.data_const_seg); @@ -49,19 +57,10 @@ static void create_synthetic_chunks(Context &ctx) { ctx.text_seg.chunks.push_back(&ctx.mach_hdr); ctx.text_seg.chunks.push_back(&ctx.load_cmd); ctx.text_seg.chunks.push_back(&ctx.headerpad); - - ctx.headerpad.hdr.size = ctx.arg.headerpad; - - ctx.text->hdr.attr = S_ATTR_PURE_INSTRUCTIONS | S_ATTR_SOME_INSTRUCTIONS; - ctx.text->hdr.p2align = 4; 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); - - OutputSection *cstring = OutputSection::get_instance(ctx, "__TEXT", "__cstring"); - cstring->hdr.type = S_CSTRING_LITERALS; - ctx.text_seg.chunks.push_back(cstring); + ctx.text_seg.chunks.push_back(ctx.cstring); ctx.text_seg.chunks.push_back(&ctx.unwind_info); ctx.data_const_seg.chunks.push_back(&ctx.got); @@ -77,6 +76,8 @@ static void create_synthetic_chunks(Context &ctx) { ctx.linkedit_seg.chunks.push_back(&ctx.symtab); ctx.linkedit_seg.chunks.push_back(&ctx.indir_symtab); ctx.linkedit_seg.chunks.push_back(&ctx.strtab); + + ctx.headerpad.hdr.size = ctx.arg.headerpad; } static void export_symbols(Context &ctx) { diff --git a/macho/mold.h b/macho/mold.h index f98ca67b..345cb1bd 100644 --- a/macho/mold.h +++ b/macho/mold.h @@ -634,8 +634,10 @@ struct Context { OutputSymtabSection symtab; OutputIndirectSymtabSection indir_symtab; OutputStrtabSection strtab; + OutputSection *text = nullptr; OutputSection *data = nullptr; + OutputSection *cstring = nullptr; std::vector segments; };