#include "macho_synth.hpp" #include #include #include #include namespace voidlibc::loader::macos { namespace { // ── On-disk structs (x86_64 LE host = no byte-swap needed) ───────────────── constexpr std::uint32_t kMhMagic64 = 0xFEEDFACFu; constexpr std::int32_t kCpuTypeX86_64 = 0x01000007; constexpr std::int32_t kCpuSubX86All = 3; constexpr std::uint32_t kMhDylib = 6; // DYLDLINK | TWOLEVEL | NO_REEXPORTED_DYLIBS constexpr std::uint32_t kMhFlags = 0x00100084u; constexpr std::uint32_t kLcSegment64 = 0x19u; constexpr std::uint32_t kLcDyldInfoOnly = 0x80000022u; constexpr std::uint32_t kLcSymtab = 0x02u; constexpr std::uint32_t kLcDysymtab = 0x0bu; constexpr std::uint32_t kLcIdDylib = 0x0du; constexpr std::uint32_t kLcLoadDylib = 0x0cu; constexpr std::uint32_t kLcVersionMinMacOS = 0x24u; constexpr std::int32_t kVmProtR = 0x1; constexpr std::int32_t kVmProtW = 0x2; constexpr std::int32_t kVmProtX = 0x4; constexpr std::uint32_t kSAttrPureInstr = 0x80000000u; constexpr std::uint32_t kSAttrSomeInstr = 0x00000400u; constexpr std::uint32_t kSSymbolStubs = 0x00000008u; constexpr std::uint32_t kSNonLazySymPtrs = 0x00000006u; constexpr std::uint8_t kNExt = 0x01u; constexpr std::uint8_t kNSect = 0x0eu; constexpr std::uint8_t kNUndf = 0x00u; constexpr std::uint8_t kBindOpDone = 0x00u; constexpr std::uint8_t kBindOpSetDylibOrdinalImm = 0x10u; constexpr std::uint8_t kBindOpSetSymbolFlags = 0x40u; constexpr std::uint8_t kBindOpSetTypeImm = 0x50u; constexpr std::uint8_t kBindOpSetSegAndOff = 0x70u; constexpr std::uint8_t kBindOpDoBind = 0x90u; constexpr std::uint8_t kBindTypePointer = 1u; constexpr std::uint64_t kPageSize = 0x1000u; #pragma pack(push, 1) struct MachHeader64 { std::uint32_t magic, cputype_u, cpusubtype_u, filetype; std::uint32_t ncmds, sizeofcmds, flags, reserved; }; static_assert(sizeof(MachHeader64) == 32); struct SegCmd64 { std::uint32_t cmd, cmdsize; char segname[16]; std::uint64_t vmaddr, vmsize, fileoff, filesize; std::int32_t maxprot, initprot; std::uint32_t nsects, flags; }; static_assert(sizeof(SegCmd64) == 72); // segment_command_64 is 72 bytes (not 64) struct Sec64 { char sectname[16], segname[16]; std::uint64_t addr, size; std::uint32_t offset, align, reloff, nreloc, flags, reserved1, reserved2, reserved3; }; static_assert(sizeof(Sec64) == 80); struct DyldInfoCmd { std::uint32_t cmd, cmdsize; std::uint32_t rebase_off, rebase_size; std::uint32_t bind_off, bind_size; std::uint32_t weak_bind_off, weak_bind_size; std::uint32_t lazy_bind_off, lazy_bind_size; std::uint32_t export_off, export_size; }; static_assert(sizeof(DyldInfoCmd) == 48); struct SymtabCmd { std::uint32_t cmd, cmdsize, symoff, nsyms, stroff, strsize; }; static_assert(sizeof(SymtabCmd) == 24); struct DysymtabCmd { std::uint32_t cmd, cmdsize; std::uint32_t ilocalsym, nlocalsym, iextdefsym, nextdefsym, iundefsym, nundefsym; std::uint32_t tocoff, ntoc, modtaboff, nmodtab, extrefsymoff, nextrefsyms; std::uint32_t indirectsymoff, nindirectsyms, extreloff, nextrel, locreloff, nlocrel; }; static_assert(sizeof(DysymtabCmd) == 80); struct NList64 { std::uint32_t n_strx; std::uint8_t n_type, n_sect; std::uint16_t n_desc; std::uint64_t n_value; }; static_assert(sizeof(NList64) == 16); struct VersionMinCmd { std::uint32_t cmd, cmdsize, version, sdk; }; static_assert(sizeof(VersionMinCmd) == 16); #pragma pack(pop) // ── Helpers ───────────────────────────────────────────────────────────────── static constexpr std::uint64_t align_up(std::uint64_t v, std::uint64_t a) { return (v + (a-1)) & ~(a-1); } static void push_uleb(std::vector& b, std::uint64_t v) { do { std::uint8_t byte = v & 0x7f; v >>= 7; if (v) byte |= 0x80; b.push_back(byte); } while (v); } // Fixed 2-byte ULEB128 (covers 0..16383, enough for any trie offset in MVP). static void push_uleb2(std::vector& b, std::uint64_t v) { b.push_back(static_cast((v & 0x7f) | 0x80)); b.push_back(static_cast((v >> 7) & 0x7f)); } static void write32le(std::vector& buf, std::uint64_t off, std::int32_t v) { std::uint32_t u; std::memcpy(&u, &v, 4); buf[off+0] = u & 0xff; buf[off+1] = (u >> 8) & 0xff; buf[off+2] = (u >> 16) & 0xff; buf[off+3] = (u >> 24) & 0xff; } static void set_name(char (&out)[16], const char* src) { std::memset(out, 0, 16); std::strncpy(out, src, 16); } // Flat export trie: root → one leaf per symbol (full symbol name as edge label). // `sym_vmaddr` = symbol vmaddr from image base (= text_sect_vmaddr + code_offset). static std::vector build_export_trie( const std::vector& exports, std::uint64_t text_sect_vmaddr) { if (exports.empty()) return {0, 0}; struct Leaf { std::string name; std::vector bytes; }; std::vector leaves; for (const auto& e : exports) { Leaf lf; lf.name = e.name; std::vector td; push_uleb(td, 0); // flags = regular push_uleb(td, text_sect_vmaddr + e.code_offset); // vmaddr from image base push_uleb(lf.bytes, td.size()); // terminal_size lf.bytes.insert(lf.bytes.end(), td.begin(), td.end()); lf.bytes.push_back(0); // num_children leaves.push_back(std::move(lf)); } // Root node size with 2-byte fixed ULEBs for child offsets: // 1 (terminal_size=0) + 1 (num_children) + Σ(name.size()+1+2) per child std::size_t root_sz = 2; for (const auto& lf : leaves) root_sz += lf.name.size() + 1 + 2; std::vector leaf_offs; std::size_t cursor = root_sz; for (const auto& lf : leaves) { leaf_offs.push_back(cursor); cursor += lf.bytes.size(); } std::vector trie; trie.push_back(0); // not terminal trie.push_back(static_cast(exports.size())); // num_children for (std::size_t i = 0; i < exports.size(); ++i) { const auto& lf = leaves[i]; trie.insert(trie.end(), lf.name.begin(), lf.name.end()); trie.push_back(0); push_uleb2(trie, leaf_offs[i]); } for (const auto& lf : leaves) trie.insert(trie.end(), lf.bytes.begin(), lf.bytes.end()); while (trie.size() % 8) trie.push_back(0); return trie; } // Non-lazy bind opcodes. Each import gets one slot in __nl_symbol_ptr. // Slots [0..nstubs-1] = function call stubs; slots [nstubs..] = GOT data. // lib_ordinals: dylib install name → 1-based LC_LOAD_DYLIB ordinal. static std::vector build_bind_opcodes( const std::vector& ordered_imports, const std::unordered_map& lib_ordinals, std::uint32_t data_seg_idx, std::uint64_t nl_sym_off_in_data_seg) { if (ordered_imports.empty()) return {kBindOpDone}; static constexpr const char* kDefaultLib = "/usr/lib/libSystem.B.dylib"; std::vector buf; for (std::size_t i = 0; i < ordered_imports.size(); ++i) { if (!ordered_imports[i]) continue; const auto& im = *ordered_imports[i]; const std::string& lib = im.library.empty() ? kDefaultLib : im.library; auto oit = lib_ordinals.find(lib); const int ordinal = (oit != lib_ordinals.end()) ? oit->second : 1; buf.push_back(kBindOpSetDylibOrdinalImm | static_cast(ordinal & 0xf)); buf.push_back(kBindOpSetSymbolFlags | 0); buf.insert(buf.end(), im.name.begin(), im.name.end()); buf.push_back(0); buf.push_back(kBindOpSetTypeImm | kBindTypePointer); buf.push_back(kBindOpSetSegAndOff | (data_seg_idx & 0xf)); push_uleb(buf, nl_sym_off_in_data_seg + i * 8); buf.push_back(kBindOpDoBind); } buf.push_back(kBindOpDone); while (buf.size() % 8) buf.push_back(0); return buf; } } // anonymous namespace MachoSynthOutput synthesize_macho(const MachoSynthInput& input) { MachoSynthOutput out{}; if (input.code.empty()) { out.ok = false; out.error = "macho_synth: code is empty"; return out; } // ── Classify imports ──────────────────────────────────────────────────── std::unordered_set internal_ids, data_ids; for (const auto& s : input.internal_symbols) internal_ids.insert(s.sym_id); for (const auto& s : input.data_symbols) data_ids.insert(s.sym_id); // PLT32 → stub; GotPC32* (external) → nl_symbol_ptr GOT slot std::unordered_map imp_to_stub, imp_to_got; { std::uint32_t si = 0, gi = 0; using format::RelocOp; for (const auto& r : input.relocs) { if (r.op == RelocOp::PLT32 && !internal_ids.count(r.sym_id) && !data_ids.count(r.sym_id) && !imp_to_stub.count(r.sym_id)) imp_to_stub[r.sym_id] = si++; else if ((r.op == RelocOp::GotPC32 || r.op == RelocOp::GotPC32X || r.op == RelocOp::GotPC32XRex) && !internal_ids.count(r.sym_id) && !data_ids.count(r.sym_id) && !imp_to_got.count(r.sym_id)) imp_to_got[r.sym_id] = gi++; } } const auto nstubs = static_cast(imp_to_stub.size()); const auto ngot = static_cast(imp_to_got.size()); const auto n_nlsym = nstubs + ngot; // ── Load command sizes ────────────────────────────────────────────────── const std::uint32_t ntext_sects = (nstubs > 0) ? 2u : 1u; const bool has_data_blob = !input.data_blob.empty(); const bool has_nlsym = (n_nlsym > 0); const bool has_data_seg = has_data_blob || has_nlsym; const auto ndata_sects = static_cast( (has_data_blob ? 1u : 0u) + (has_nlsym ? 1u : 0u)); const std::string dylib_name = input.soname.empty() ? "/usr/local/lib/libvoid_module.dylib" : input.soname; const std::uint32_t iddylib_sz = static_cast( align_up(8u + 16u + dylib_name.size() + 1u, 4u)); std::vector needed_libs; if (!input.needed_libs.empty()) { needed_libs = input.needed_libs; } else if (nstubs > 0 || ngot > 0) { // Collect unique libraries in insertion order from per-import library fields. std::unordered_map seen; for (const auto& im : input.imports) { const std::string& lib = im.library.empty() ? "/usr/lib/libSystem.B.dylib" : im.library; if (seen.emplace(lib, true).second) needed_libs.push_back(lib); } if (needed_libs.empty()) needed_libs.push_back("/usr/lib/libSystem.B.dylib"); } // 1-based ordinal map (ordinal 1 = first LC_LOAD_DYLIB). std::unordered_map lib_ordinal; for (std::size_t i = 0; i < needed_libs.size(); ++i) lib_ordinal[needed_libs[i]] = static_cast(i + 1); std::vector loaddylib_szs; for (const auto& lib : needed_libs) loaddylib_szs.push_back(static_cast( align_up(8u + 16u + lib.size() + 1u, 4u))); std::uint32_t total_lc_sz = 0; total_lc_sz += sizeof(SegCmd64) + ntext_sects * sizeof(Sec64); // __TEXT if (has_data_seg) total_lc_sz += sizeof(SegCmd64) + ndata_sects * sizeof(Sec64); // __DATA total_lc_sz += sizeof(SegCmd64); // __LINKEDIT total_lc_sz += sizeof(DyldInfoCmd); total_lc_sz += sizeof(SymtabCmd); total_lc_sz += sizeof(DysymtabCmd); total_lc_sz += iddylib_sz; for (std::uint32_t sz : loaddylib_szs) total_lc_sz += sz; total_lc_sz += sizeof(VersionMinCmd); const std::uint64_t hdr_lc_sz = sizeof(MachHeader64) + total_lc_sz; // ── Vmaddr layout ──────────────────────────────────────────────────────── // __TEXT: vmaddr=0, fileoff=0; vmaddr == fileoff throughout. const std::uint64_t text_sect_va = align_up(hdr_lc_sz, 16); const std::uint64_t text_sect_sz = input.code.size(); const std::uint64_t stubs_va = align_up(text_sect_va + text_sect_sz, 2); const std::uint64_t stubs_sz = nstubs * 6; const std::uint64_t text_seg_end = stubs_va + stubs_sz; const std::uint64_t data_seg_va = align_up(text_seg_end, kPageSize); const std::uint64_t data_sect_va = data_seg_va; const std::uint64_t data_sect_sz = input.data_blob.size(); const std::uint64_t nlsym_va = align_up(data_sect_va + data_sect_sz, 8); const std::uint64_t nlsym_sz = n_nlsym * 8; const std::uint64_t data_seg_end = nlsym_va + nlsym_sz; const std::uint64_t linkedit_va = has_data_seg ? align_up(data_seg_end, kPageSize) : align_up(text_seg_end, kPageSize); // ── Build LINKEDIT content ─────────────────────────────────────────────── // Bind imports: stubs first [0..nstubs-1], then GOT data [nstubs..nstubs+ngot-1]. std::vector stub_order(nstubs, nullptr); std::vector got_order(ngot, nullptr); for (const auto& im : input.imports) { auto sit = imp_to_stub.find(im.sym_id); if (sit != imp_to_stub.end()) stub_order[sit->second] = &im; auto git = imp_to_got.find(im.sym_id); if (git != imp_to_got.end()) got_order[git->second] = &im; } std::vector bind_order; bind_order.insert(bind_order.end(), stub_order.begin(), stub_order.end()); bind_order.insert(bind_order.end(), got_order.begin(), got_order.end()); const std::uint32_t data_seg_idx = has_data_seg ? 1u : 0u; // 0-based const std::uint64_t nlsym_off_in_data = nlsym_va - data_seg_va; auto bind_bytes = build_bind_opcodes(bind_order, lib_ordinal, data_seg_idx, nlsym_off_in_data); auto export_trie = build_export_trie(input.exports, text_sect_va); // Symbol table: extdef (exports) then undef (imports). std::vector strtab; strtab.push_back(0); std::vector nlist; const std::uint32_t iextdefsym = 0; for (const auto& e : input.exports) { NList64 n{}; n.n_strx = static_cast(strtab.size()); strtab.insert(strtab.end(), e.name.begin(), e.name.end()); strtab.push_back(0); n.n_type = kNExt | kNSect; n.n_sect = 1; // 1-based: __text is section 1 n.n_value = text_sect_va + e.code_offset; nlist.push_back(n); } const std::uint32_t iundefsym = static_cast(nlist.size()); for (const auto& im : input.imports) { NList64 n{}; n.n_strx = static_cast(strtab.size()); strtab.insert(strtab.end(), im.name.begin(), im.name.end()); strtab.push_back(0); n.n_type = kNExt | kNUndf; n.n_sect = 0; // NO_SECT nlist.push_back(n); } while (strtab.size() % 4) strtab.push_back(0); // LINKEDIT offsets std::uint64_t li = linkedit_va; const std::uint64_t bind_off = li; li += bind_bytes.size(); const std::uint64_t exp_off = li; li += export_trie.size(); li = align_up(li, 8); const std::uint64_t sym_off = li; li += nlist.size() * sizeof(NList64); li = align_up(li, 4); const std::uint64_t str_off = li; li += strtab.size(); const std::uint64_t linkedit_filesz = li - linkedit_va; // ── Apply reloc IR to code blob ───────────────────────────────────────── std::vector code_buf(input.code); // Build stub bytes (6-byte JMP stubs → nl_symbol_ptr slots). // Each stub: FF 25 (jmpq *slot(%rip)) // Stubs use slots [0..nstubs-1] in nl_symbol_ptr. std::vector stub_buf(stubs_sz, 0); for (std::uint32_t i = 0; i < nstubs; ++i) { const std::uint64_t stub_va = stubs_va + i * 6; const std::uint64_t slot_va = nlsym_va + i * 8; const std::int32_t disp = static_cast( static_cast(slot_va) - static_cast(stub_va + 6)); stub_buf[i*6 + 0] = 0xff; stub_buf[i*6 + 1] = 0x25; std::uint32_t u; std::memcpy(&u, &disp, 4); stub_buf[i*6 + 2] = u & 0xff; stub_buf[i*6 + 3] = (u >> 8) & 0xff; stub_buf[i*6 + 4] = (u >> 16) & 0xff; stub_buf[i*6 + 5] = (u >> 24) & 0xff; } // vmaddr maps std::unordered_map sym_va, dat_va; for (const auto& s : input.internal_symbols) sym_va[s.sym_id] = text_sect_va + s.code_offset; for (const auto& s : input.data_symbols) dat_va[s.sym_id] = data_sect_va + s.data_offset; using format::RelocOp; for (const auto& r : input.relocs) { const std::uint64_t coff = r.dst_offset; if (coff + 4 > code_buf.size()) { out.ok = false; std::ostringstream os; os << "macho_synth: reloc at 0x" << std::hex << coff << " out of range"; out.error = os.str(); return out; } const std::uint64_t site_va = text_sect_va + coff; std::int64_t disp = 0; bool patched = false; switch (r.op) { case RelocOp::PC32: case RelocOp::PC32_1: case RelocOp::PC32_2: case RelocOp::PC32_4: case RelocOp::PC32_5: { std::int64_t bias = 0; if (r.op == RelocOp::PC32_1) bias = -1; else if (r.op == RelocOp::PC32_2) bias = -2; else if (r.op == RelocOp::PC32_4) bias = -4; else if (r.op == RelocOp::PC32_5) bias = -5; auto ic = sym_va.find(r.sym_id); if (ic != sym_va.end()) { disp = static_cast(ic->second) - static_cast(site_va + 4) + r.addend + bias; patched = true; break; } auto id = dat_va.find(r.sym_id); if (id != dat_va.end()) { disp = static_cast(id->second) - static_cast(site_va + 4) + r.addend + bias; patched = true; break; } out.ok = false; { std::ostringstream os; os << "PC32: sym_id " << r.sym_id << " not found"; out.error = os.str(); } return out; } case RelocOp::PLT32: { auto ic = sym_va.find(r.sym_id); if (ic != sym_va.end()) { disp = static_cast(ic->second) - static_cast(site_va + 4) + r.addend; patched = true; break; } auto sit = imp_to_stub.find(r.sym_id); if (sit != imp_to_stub.end()) { const std::uint64_t sv = stubs_va + sit->second * 6; disp = static_cast(sv) - static_cast(site_va + 4) + r.addend; patched = true; break; } out.ok = false; { std::ostringstream os; os << "PLT32: sym_id " << r.sym_id << " not found"; out.error = os.str(); } return out; } case RelocOp::GotPC32: case RelocOp::GotPC32X: case RelocOp::GotPC32XRex: { const bool relax = r.op == RelocOp::GotPC32X || r.op == RelocOp::GotPC32XRex; auto ic = sym_va.find(r.sym_id); auto id = dat_va.find(r.sym_id); if ((ic != sym_va.end() || id != dat_va.end()) && relax) { if (coff < 2 || code_buf[coff-1] != 0x8b) { out.ok = false; out.error = "GOTPCREL relax: expected MOV"; return out; } code_buf[coff-1] = 0x8d; std::uint64_t tv = (ic != sym_va.end()) ? ic->second : id->second; disp = static_cast(tv) - static_cast(site_va + 4) + r.addend; patched = true; break; } auto git = imp_to_got.find(r.sym_id); if (git != imp_to_got.end()) { const std::uint64_t sv = nlsym_va + (nstubs + git->second) * 8; disp = static_cast(sv) - static_cast(site_va + 4) + r.addend; patched = true; break; } out.ok = false; { std::ostringstream os; os << "GOTPCREL: sym_id " << r.sym_id << " not found"; out.error = os.str(); } return out; } case RelocOp::Abs64: case RelocOp::Abs32: case RelocOp::Abs32Signed: out.ok = false; out.error = "Abs reloc not yet supported in macho_synth"; return out; case RelocOp::TlsLE: case RelocOp::TlsIE: case RelocOp::TlsGD: case RelocOp::TlsLD: out.ok = false; out.error = "TLS not yet supported in macho_synth"; return out; } if (patched) write32le(code_buf, coff, static_cast(disp)); } // ── Emit file ──────────────────────────────────────────────────────────── const std::uint64_t file_sz = li; out.bytes.assign(file_sz, 0); auto write_at = [&](std::uint64_t off, const void* src, std::uint64_t n) { std::memcpy(out.bytes.data() + off, src, n); }; // -- Mach header -- std::uint32_t ncmds = 0; ncmds++; if (has_data_seg) ncmds++; ncmds++; // segments ncmds += 4; // DyldInfo, Symtab, Dysymtab, VersionMin ncmds++; // ID_DYLIB ncmds += static_cast(needed_libs.size()); MachHeader64 mh{}; mh.magic = kMhMagic64; mh.cputype_u = static_cast(kCpuTypeX86_64); mh.cpusubtype_u = static_cast(kCpuSubX86All); mh.filetype = kMhDylib; mh.ncmds = ncmds; mh.sizeofcmds = total_lc_sz; mh.flags = kMhFlags; write_at(0, &mh, sizeof(mh)); std::uint64_t lc = sizeof(MachHeader64); auto emit = [&](const void* p, std::uint32_t n) { write_at(lc, p, n); lc += n; }; // __TEXT segment { const std::uint32_t text_cmd_sz = sizeof(SegCmd64) + ntext_sects * sizeof(Sec64); SegCmd64 seg{}; seg.cmd = kLcSegment64; seg.cmdsize = text_cmd_sz; set_name(seg.segname, "__TEXT"); seg.vmsize = seg.filesize = align_up(text_seg_end, kPageSize); seg.maxprot = kVmProtR | kVmProtX; seg.initprot = kVmProtR | kVmProtX; seg.nsects = ntext_sects; emit(&seg, sizeof(seg)); Sec64 ts{}; set_name(ts.sectname, "__text"); set_name(ts.segname, "__TEXT"); ts.addr = text_sect_va; ts.size = text_sect_sz; ts.offset = static_cast(text_sect_va); ts.align = 4; // 2^4 = 16 ts.flags = kSAttrPureInstr | kSAttrSomeInstr; emit(&ts, sizeof(ts)); if (nstubs > 0) { Sec64 ss{}; set_name(ss.sectname, "__stubs"); set_name(ss.segname, "__TEXT"); ss.addr = stubs_va; ss.size = stubs_sz; ss.offset = static_cast(stubs_va); ss.align = 1; // 2^1 = 2 ss.flags = kSSymbolStubs | kSAttrSomeInstr | kSAttrPureInstr; ss.reserved2 = 6; // stub entry size emit(&ss, sizeof(ss)); } } // __DATA segment if (has_data_seg) { const std::uint32_t data_cmd_sz = sizeof(SegCmd64) + ndata_sects * sizeof(Sec64); SegCmd64 seg{}; seg.cmd = kLcSegment64; seg.cmdsize = data_cmd_sz; set_name(seg.segname, "__DATA"); seg.vmaddr = seg.fileoff = data_seg_va; seg.vmsize = seg.filesize = align_up(data_seg_end - data_seg_va, kPageSize); seg.maxprot = kVmProtR | kVmProtW; seg.initprot = kVmProtR | kVmProtW; seg.nsects = ndata_sects; emit(&seg, sizeof(seg)); if (has_data_blob) { Sec64 ds{}; set_name(ds.sectname, "__data"); set_name(ds.segname, "__DATA"); ds.addr = data_sect_va; ds.size = data_sect_sz; ds.offset = static_cast(data_sect_va); ds.align = 3; emit(&ds, sizeof(ds)); } if (has_nlsym) { Sec64 ns{}; set_name(ns.sectname, "__nl_symbol_ptr"); set_name(ns.segname, "__DATA"); ns.addr = nlsym_va; ns.size = nlsym_sz; ns.offset = static_cast(nlsym_va); ns.align = 3; ns.flags = kSNonLazySymPtrs; emit(&ns, sizeof(ns)); } } // __LINKEDIT segment { SegCmd64 seg{}; seg.cmd = kLcSegment64; seg.cmdsize = sizeof(SegCmd64); set_name(seg.segname, "__LINKEDIT"); seg.vmaddr = seg.fileoff = linkedit_va; seg.vmsize = seg.filesize = align_up(linkedit_filesz, kPageSize); seg.maxprot = kVmProtR; seg.initprot = kVmProtR; emit(&seg, sizeof(seg)); } // LC_DYLD_INFO_ONLY { DyldInfoCmd dc{}; dc.cmd = kLcDyldInfoOnly; dc.cmdsize = sizeof(dc); dc.bind_off = static_cast(bind_off); dc.bind_size = static_cast(bind_bytes.size()); dc.export_off = static_cast(exp_off); dc.export_size = static_cast(export_trie.size()); emit(&dc, sizeof(dc)); } // LC_SYMTAB { SymtabCmd sc{}; sc.cmd = kLcSymtab; sc.cmdsize = sizeof(sc); sc.symoff = static_cast(sym_off); sc.nsyms = static_cast(nlist.size()); sc.stroff = static_cast(str_off); sc.strsize = static_cast(strtab.size()); emit(&sc, sizeof(sc)); } // LC_DYSYMTAB { DysymtabCmd dc{}; dc.cmd = kLcDysymtab; dc.cmdsize = sizeof(dc); dc.iextdefsym = iextdefsym; dc.nextdefsym = iundefsym - iextdefsym; dc.iundefsym = iundefsym; dc.nundefsym = static_cast(nlist.size()) - iundefsym; emit(&dc, sizeof(dc)); } // LC_ID_DYLIB { std::vector lc_buf(iddylib_sz, 0); auto* u = reinterpret_cast(lc_buf.data()); u[0] = kLcIdDylib; u[1] = iddylib_sz; u[2] = 8u + 16u; // name offset from cmd start u[3] = 0; u[4] = 0x00010000u; u[5] = 0x00010000u; std::memcpy(lc_buf.data() + 8 + 16, dylib_name.c_str(), dylib_name.size()); emit(lc_buf.data(), iddylib_sz); } // LC_LOAD_DYLIB entries for (std::size_t i = 0; i < needed_libs.size(); ++i) { const std::uint32_t sz = loaddylib_szs[i]; std::vector lc_buf(sz, 0); auto* u = reinterpret_cast(lc_buf.data()); u[0] = kLcLoadDylib; u[1] = sz; u[2] = 8u + 16u; u[3] = 0; u[4] = 0x00010000u; u[5] = 0x00010000u; std::memcpy(lc_buf.data() + 8 + 16, needed_libs[i].c_str(), needed_libs[i].size()); emit(lc_buf.data(), sz); } // LC_VERSION_MIN_MACOSX { VersionMinCmd vc{}; vc.cmd = kLcVersionMinMacOS; vc.cmdsize = sizeof(vc); vc.version = 0x000c0000u; // 12.0.0 vc.sdk = 0x000c0000u; emit(&vc, sizeof(vc)); } // -- Section data -- write_at(text_sect_va, code_buf.data(), code_buf.size()); if (stubs_sz > 0) write_at(stubs_va, stub_buf.data(), stub_buf.size()); if (has_data_blob) write_at(data_sect_va, input.data_blob.data(), input.data_blob.size()); // __nl_symbol_ptr: zeroed — dyld fills via bind opcodes // -- LINKEDIT data -- if (!bind_bytes.empty()) write_at(bind_off, bind_bytes.data(), bind_bytes.size()); write_at(exp_off, export_trie.data(), export_trie.size()); if (!nlist.empty()) write_at(sym_off, nlist.data(), nlist.size() * sizeof(NList64)); write_at(str_off, strtab.data(), strtab.size()); return out; } } // namespace voidlibc::loader::macos