| ... | @@ -22,6 +22,7 @@ const link = @import("../link.zig"); | ... | @@ -22,6 +22,7 @@ const link = @import("../link.zig"); |
| 22 | const llvm_backend = @import("../codegen/llvm.zig"); | 22 | const llvm_backend = @import("../codegen/llvm.zig"); |
| 23 | const target_util = @import("../target.zig"); | 23 | const target_util = @import("../target.zig"); |
| 24 | const trace = @import("../tracy.zig").trace; | 24 | const trace = @import("../tracy.zig").trace; |
| | 25 | const zld = @import("MachO/zld.zig"); |
| 25 | | 26 | |
| 26 | const Air = @import("../Air.zig"); | 27 | const Air = @import("../Air.zig"); |
| 27 | const Allocator = mem.Allocator; | 28 | const Allocator = mem.Allocator; |
| ... | @@ -38,6 +39,7 @@ const LibStub = @import("tapi.zig").LibStub; | ... | @@ -38,6 +39,7 @@ const LibStub = @import("tapi.zig").LibStub; |
| 38 | const Liveness = @import("../Liveness.zig"); | 39 | const Liveness = @import("../Liveness.zig"); |
| 39 | const LlvmObject = @import("../codegen/llvm.zig").Object; | 40 | const LlvmObject = @import("../codegen/llvm.zig").Object; |
| 40 | const Module = @import("../Module.zig"); | 41 | const Module = @import("../Module.zig"); |
| | 42 | const Relocation = @import("MachO/Relocation.zig"); |
| 41 | const StringTable = @import("strtab.zig").StringTable; | 43 | const StringTable = @import("strtab.zig").StringTable; |
| 42 | const Trie = @import("MachO/Trie.zig"); | 44 | const Trie = @import("MachO/Trie.zig"); |
| 43 | const Type = @import("../type.zig").Type; | 45 | const Type = @import("../type.zig").Type; |
| ... | @@ -55,11 +57,6 @@ pub const SearchStrategy = enum { | ... | @@ -55,11 +57,6 @@ pub const SearchStrategy = enum { |
| 55 | | 57 | |
| 56 | pub const N_DESC_GCED: u16 = @bitCast(u16, @as(i16, -1)); | 58 | pub const N_DESC_GCED: u16 = @bitCast(u16, @as(i16, -1)); |
| 57 | | 59 | |
| 58 | const SystemLib = struct { | | |
| 59 | needed: bool = false, | | |
| 60 | weak: bool = false, | | |
| 61 | }; | | |
| 62 | | | |
| 63 | const Section = struct { | 60 | const Section = struct { |
| 64 | header: macho.section_64, | 61 | header: macho.section_64, |
| 65 | segment_index: u8, | 62 | segment_index: u8, |
| ... | @@ -118,6 +115,7 @@ segments: std.ArrayListUnmanaged(macho.segment_command_64) = .{}, | ... | @@ -118,6 +115,7 @@ segments: std.ArrayListUnmanaged(macho.segment_command_64) = .{}, |
| 118 | sections: std.MultiArrayList(Section) = .{}, | 115 | sections: std.MultiArrayList(Section) = .{}, |
| 119 | | 116 | |
| 120 | pagezero_segment_cmd_index: ?u8 = null, | 117 | pagezero_segment_cmd_index: ?u8 = null, |
| | 118 | header_segment_cmd_index: ?u8 = null, |
| 121 | text_segment_cmd_index: ?u8 = null, | 119 | text_segment_cmd_index: ?u8 = null, |
| 122 | data_const_segment_cmd_index: ?u8 = null, | 120 | data_const_segment_cmd_index: ?u8 = null, |
| 123 | data_segment_cmd_index: ?u8 = null, | 121 | data_segment_cmd_index: ?u8 = null, |
| ... | @@ -127,6 +125,7 @@ text_section_index: ?u8 = null, | ... | @@ -127,6 +125,7 @@ text_section_index: ?u8 = null, |
| 127 | stubs_section_index: ?u8 = null, | 125 | stubs_section_index: ?u8 = null, |
| 128 | stub_helper_section_index: ?u8 = null, | 126 | stub_helper_section_index: ?u8 = null, |
| 129 | got_section_index: ?u8 = null, | 127 | got_section_index: ?u8 = null, |
| | 128 | data_const_section_index: ?u8 = null, |
| 130 | la_symbol_ptr_section_index: ?u8 = null, | 129 | la_symbol_ptr_section_index: ?u8 = null, |
| 131 | data_section_index: ?u8 = null, | 130 | data_section_index: ?u8 = null, |
| 132 | | 131 | |
| ... | @@ -160,6 +159,8 @@ stubs_table: std.AutoHashMapUnmanaged(SymbolWithLoc, u32) = .{}, | ... | @@ -160,6 +159,8 @@ stubs_table: std.AutoHashMapUnmanaged(SymbolWithLoc, u32) = .{}, |
| 160 | | 159 | |
| 161 | error_flags: File.ErrorFlags = File.ErrorFlags{}, | 160 | error_flags: File.ErrorFlags = File.ErrorFlags{}, |
| 162 | | 161 | |
| | 162 | segment_table_dirty: bool = false, |
| | 163 | |
| 163 | /// A helper var to indicate if we are at the start of the incremental updates, or | 164 | /// A helper var to indicate if we are at the start of the incremental updates, or |
| 164 | /// already somewhere further along the update-and-run chain. | 165 | /// already somewhere further along the update-and-run chain. |
| 165 | /// TODO once we add opening a prelinked output binary from file, this will become | 166 | /// TODO once we add opening a prelinked output binary from file, this will become |
| ... | @@ -193,6 +194,26 @@ atom_by_index_table: std.AutoHashMapUnmanaged(u32, *Atom) = .{}, | ... | @@ -193,6 +194,26 @@ atom_by_index_table: std.AutoHashMapUnmanaged(u32, *Atom) = .{}, |
| 193 | /// with `Decl` `main`, and lives as long as that `Decl`. | 194 | /// with `Decl` `main`, and lives as long as that `Decl`. |
| 194 | unnamed_const_atoms: UnnamedConstTable = .{}, | 195 | unnamed_const_atoms: UnnamedConstTable = .{}, |
| 195 | | 196 | |
| | 197 | /// A table of relocations indexed by the owning them `Atom`. |
| | 198 | /// Note that once we refactor `Atom`'s lifetime and ownership rules, |
| | 199 | /// this will be a table indexed by index into the list of Atoms. |
| | 200 | relocs: RelocationTable = .{}, |
| | 201 | |
| | 202 | /// A table of rebases indexed by the owning them `Atom`. |
| | 203 | /// Note that once we refactor `Atom`'s lifetime and ownership rules, |
| | 204 | /// this will be a table indexed by index into the list of Atoms. |
| | 205 | rebases: RebaseTable = .{}, |
| | 206 | |
| | 207 | /// A table of bindings indexed by the owning them `Atom`. |
| | 208 | /// Note that once we refactor `Atom`'s lifetime and ownership rules, |
| | 209 | /// this will be a table indexed by index into the list of Atoms. |
| | 210 | bindings: BindingTable = .{}, |
| | 211 | |
| | 212 | /// A table of lazy bindings indexed by the owning them `Atom`. |
| | 213 | /// Note that once we refactor `Atom`'s lifetime and ownership rules, |
| | 214 | /// this will be a table indexed by index into the list of Atoms. |
| | 215 | lazy_bindings: BindingTable = .{}, |
| | 216 | |
| 196 | /// Table of Decls that are currently alive. | 217 | /// Table of Decls that are currently alive. |
| 197 | /// We store them here so that we can properly dispose of any allocated | 218 | /// We store them here so that we can properly dispose of any allocated |
| 198 | /// memory within the atom in the incremental linker. | 219 | /// memory within the atom in the incremental linker. |
| ... | @@ -212,8 +233,8 @@ const Entry = struct { | ... | @@ -212,8 +233,8 @@ const Entry = struct { |
| 212 | return macho_file.getSymbolPtr(.{ .sym_index = entry.sym_index, .file = null }); | 233 | return macho_file.getSymbolPtr(.{ .sym_index = entry.sym_index, .file = null }); |
| 213 | } | 234 | } |
| 214 | | 235 | |
| 215 | pub fn getAtom(entry: Entry, macho_file: *MachO) *Atom { | 236 | pub fn getAtom(entry: Entry, macho_file: *MachO) ?*Atom { |
| 216 | return macho_file.getAtomForSymbol(.{ .sym_index = entry.sym_index, .file = null }).?; | 237 | return macho_file.getAtomForSymbol(.{ .sym_index = entry.sym_index, .file = null }); |
| 217 | } | 238 | } |
| 218 | | 239 | |
| 219 | pub fn getName(entry: Entry, macho_file: *MachO) []const u8 { | 240 | pub fn getName(entry: Entry, macho_file: *MachO) []const u8 { |
| ... | @@ -221,7 +242,10 @@ const Entry = struct { | ... | @@ -221,7 +242,10 @@ const Entry = struct { |
| 221 | } | 242 | } |
| 222 | }; | 243 | }; |
| 223 | | 244 | |
| | 245 | const BindingTable = std.AutoHashMapUnmanaged(*Atom, std.ArrayListUnmanaged(Atom.Binding)); |
| 224 | const UnnamedConstTable = std.AutoHashMapUnmanaged(Module.Decl.Index, std.ArrayListUnmanaged(*Atom)); | 246 | const UnnamedConstTable = std.AutoHashMapUnmanaged(Module.Decl.Index, std.ArrayListUnmanaged(*Atom)); |
| | 247 | const RebaseTable = std.AutoHashMapUnmanaged(*Atom, std.ArrayListUnmanaged(u32)); |
| | 248 | const RelocationTable = std.AutoHashMapUnmanaged(*Atom, std.ArrayListUnmanaged(Relocation)); |
| 225 | | 249 | |
| 226 | const PendingUpdate = union(enum) { | 250 | const PendingUpdate = union(enum) { |
| 227 | resolve_undef: u32, | 251 | resolve_undef: u32, |
| ... | @@ -235,11 +259,21 @@ pub const SymbolWithLoc = struct { | ... | @@ -235,11 +259,21 @@ pub const SymbolWithLoc = struct { |
| 235 | | 259 | |
| 236 | // null means it's a synthetic global. | 260 | // null means it's a synthetic global. |
| 237 | file: ?u32 = null, | 261 | file: ?u32 = null, |
| | 262 | |
| | 263 | pub fn eql(this: SymbolWithLoc, other: SymbolWithLoc) bool { |
| | 264 | if (this.file == null and other.file == null) { |
| | 265 | return this.sym_index == other.sym_index; |
| | 266 | } |
| | 267 | if (this.file != null and other.file != null) { |
| | 268 | return this.sym_index == other.sym_index and this.file.? == other.file.?; |
| | 269 | } |
| | 270 | return false; |
| | 271 | } |
| 238 | }; | 272 | }; |
| 239 | | 273 | |
| 240 | /// When allocating, the ideal_capacity is calculated by | 274 | /// When allocating, the ideal_capacity is calculated by |
| 241 | /// actual_capacity + (actual_capacity / ideal_factor) | 275 | /// actual_capacity + (actual_capacity / ideal_factor) |
| 242 | const ideal_factor = 4; | 276 | const ideal_factor = 3; |
| 243 | | 277 | |
| 244 | /// Default path to dyld | 278 | /// Default path to dyld |
| 245 | const default_dyld_path: [*:0]const u8 = "/usr/lib/dyld"; | 279 | const default_dyld_path: [*:0]const u8 = "/usr/lib/dyld"; |
| ... | @@ -301,13 +335,9 @@ pub fn openPath(allocator: Allocator, options: link.Options) !*MachO { | ... | @@ -301,13 +335,9 @@ pub fn openPath(allocator: Allocator, options: link.Options) !*MachO { |
| 301 | errdefer file.close(); | 335 | errdefer file.close(); |
| 302 | self.base.file = file; | 336 | self.base.file = file; |
| 303 | | 337 | |
| 304 | if (!options.strip and options.module != null) blk: { | 338 | if (self.mode == .one_shot) return self; |
| 305 | // TODO once I add support for converting (and relocating) DWARF info from relocatable | | |
| 306 | // object files, this check becomes unnecessary. | | |
| 307 | // For now, for LLVM backend we fallback to the old-fashioned stabs approach used by | | |
| 308 | // stage1. | | |
| 309 | if (build_options.have_llvm and options.use_llvm) break :blk; | | |
| 310 | | 339 | |
| | 340 | if (!options.strip and options.module != null) { |
| 311 | // Create dSYM bundle. | 341 | // Create dSYM bundle. |
| 312 | const dir = options.module.?.zig_cache_artifact_directory; | 342 | const dir = options.module.?.zig_cache_artifact_directory; |
| 313 | log.debug("creating {s}.dSYM bundle in {?s}", .{ emit.sub_path, dir.path }); | 343 | log.debug("creating {s}.dSYM bundle in {?s}", .{ emit.sub_path, dir.path }); |
| ... | @@ -405,7 +435,7 @@ pub fn flush(self: *MachO, comp: *Compilation, prog_node: *std.Progress.Node) !v | ... | @@ -405,7 +435,7 @@ pub fn flush(self: *MachO, comp: *Compilation, prog_node: *std.Progress.Node) !v |
| 405 | } | 435 | } |
| 406 | | 436 | |
| 407 | switch (self.mode) { | 437 | switch (self.mode) { |
| 408 | .one_shot => return self.linkOneShot(comp, prog_node), | 438 | .one_shot => return zld.linkWithZld(self, comp, prog_node), |
| 409 | .incremental => return self.flushModule(comp, prog_node), | 439 | .incremental => return self.flushModule(comp, prog_node), |
| 410 | } | 440 | } |
| 411 | } | 441 | } |
| ... | @@ -434,10 +464,10 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No | ... | @@ -434,10 +464,10 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No |
| 434 | try d_sym.dwarf.flushModule(&self.base, module); | 464 | try d_sym.dwarf.flushModule(&self.base, module); |
| 435 | } | 465 | } |
| 436 | | 466 | |
| 437 | var libs = std.StringArrayHashMap(SystemLib).init(arena); | 467 | var libs = std.StringArrayHashMap(link.SystemLib).init(arena); |
| 438 | try self.resolveLibSystem(arena, comp, &.{}, &libs); | 468 | try self.resolveLibSystem(arena, comp, &.{}, &libs); |
| 439 | | 469 | |
| 440 | const id_symlink_basename = "zld.id"; | 470 | const id_symlink_basename = "link.id"; |
| 441 | | 471 | |
| 442 | const cache_dir_handle = module.zig_cache_artifact_directory.handle; | 472 | const cache_dir_handle = module.zig_cache_artifact_directory.handle; |
| 443 | var man: Cache.Manifest = undefined; | 473 | var man: Cache.Manifest = undefined; |
| ... | @@ -518,14 +548,19 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No | ... | @@ -518,14 +548,19 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No |
| 518 | | 548 | |
| 519 | try self.allocateSpecialSymbols(); | 549 | try self.allocateSpecialSymbols(); |
| 520 | | 550 | |
| | 551 | { |
| | 552 | var it = self.relocs.keyIterator(); |
| | 553 | while (it.next()) |atom| { |
| | 554 | try atom.*.resolveRelocations(self); |
| | 555 | } |
| | 556 | } |
| | 557 | |
| 521 | if (build_options.enable_logging) { | 558 | if (build_options.enable_logging) { |
| 522 | self.logSymtab(); | 559 | self.logSymtab(); |
| 523 | self.logSections(); | 560 | self.logSections(); |
| 524 | self.logAtoms(); | 561 | self.logAtoms(); |
| 525 | } | 562 | } |
| 526 | | 563 | |
| 527 | try self.writeAtomsIncremental(); | | |
| 528 | | | |
| 529 | var lc_buffer = std.ArrayList(u8).init(arena); | 564 | var lc_buffer = std.ArrayList(u8).init(arena); |
| 530 | const lc_writer = lc_buffer.writer(); | 565 | const lc_writer = lc_buffer.writer(); |
| 531 | var ncmds: u32 = 0; | 566 | var ncmds: u32 = 0; |
| ... | @@ -624,635 +659,7 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No | ... | @@ -624,635 +659,7 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No |
| 624 | self.cold_start = false; | 659 | self.cold_start = false; |
| 625 | } | 660 | } |
| 626 | | 661 | |
| 627 | fn linkOneShot(self: *MachO, comp: *Compilation, prog_node: *std.Progress.Node) !void { | 662 | pub fn resolveLibSystem( |
| 628 | const tracy = trace(@src()); | | |
| 629 | defer tracy.end(); | | |
| 630 | | | |
| 631 | const gpa = self.base.allocator; | | |
| 632 | var arena_allocator = std.heap.ArenaAllocator.init(gpa); | | |
| 633 | defer arena_allocator.deinit(); | | |
| 634 | const arena = arena_allocator.allocator(); | | |
| 635 | | | |
| 636 | const directory = self.base.options.emit.?.directory; // Just an alias to make it shorter to type. | | |
| 637 | const full_out_path = try directory.join(arena, &[_][]const u8{self.base.options.emit.?.sub_path}); | | |
| 638 | | | |
| 639 | // If there is no Zig code to compile, then we should skip flushing the output file because it | | |
| 640 | // will not be part of the linker line anyway. | | |
| 641 | const module_obj_path: ?[]const u8 = if (self.base.options.module) |module| blk: { | | |
| 642 | if (self.base.options.use_stage1) { | | |
| 643 | const obj_basename = try std.zig.binNameAlloc(arena, .{ | | |
| 644 | .root_name = self.base.options.root_name, | | |
| 645 | .target = self.base.options.target, | | |
| 646 | .output_mode = .Obj, | | |
| 647 | }); | | |
| 648 | switch (self.base.options.cache_mode) { | | |
| 649 | .incremental => break :blk try module.zig_cache_artifact_directory.join( | | |
| 650 | arena, | | |
| 651 | &[_][]const u8{obj_basename}, | | |
| 652 | ), | | |
| 653 | .whole => break :blk try fs.path.join(arena, &.{ | | |
| 654 | fs.path.dirname(full_out_path).?, obj_basename, | | |
| 655 | }), | | |
| 656 | } | | |
| 657 | } | | |
| 658 | | | |
| 659 | try self.flushModule(comp, prog_node); | | |
| 660 | | | |
| 661 | if (fs.path.dirname(full_out_path)) |dirname| { | | |
| 662 | break :blk try fs.path.join(arena, &.{ dirname, self.base.intermediary_basename.? }); | | |
| 663 | } else { | | |
| 664 | break :blk self.base.intermediary_basename.?; | | |
| 665 | } | | |
| 666 | } else null; | | |
| 667 | | | |
| 668 | var sub_prog_node = prog_node.start("MachO Flush", 0); | | |
| 669 | sub_prog_node.activate(); | | |
| 670 | sub_prog_node.context.refresh(); | | |
| 671 | defer sub_prog_node.end(); | | |
| 672 | | | |
| 673 | const cpu_arch = self.base.options.target.cpu.arch; | | |
| 674 | const os_tag = self.base.options.target.os.tag; | | |
| 675 | const abi = self.base.options.target.abi; | | |
| 676 | const is_lib = self.base.options.output_mode == .Lib; | | |
| 677 | const is_dyn_lib = self.base.options.link_mode == .Dynamic and is_lib; | | |
| 678 | const is_exe_or_dyn_lib = is_dyn_lib or self.base.options.output_mode == .Exe; | | |
| 679 | const stack_size = self.base.options.stack_size_override orelse 0; | | |
| 680 | const is_debug_build = self.base.options.optimize_mode == .Debug; | | |
| 681 | const gc_sections = self.base.options.gc_sections orelse !is_debug_build; | | |
| 682 | | | |
| 683 | const id_symlink_basename = "zld.id"; | | |
| 684 | | | |
| 685 | var man: Cache.Manifest = undefined; | | |
| 686 | defer if (!self.base.options.disable_lld_caching) man.deinit(); | | |
| 687 | | | |
| 688 | var digest: [Cache.hex_digest_len]u8 = undefined; | | |
| 689 | | | |
| 690 | if (!self.base.options.disable_lld_caching) { | | |
| 691 | man = comp.cache_parent.obtain(); | | |
| 692 | | | |
| 693 | // We are about to obtain this lock, so here we give other processes a chance first. | | |
| 694 | self.base.releaseLock(); | | |
| 695 | | | |
| 696 | comptime assert(Compilation.link_hash_implementation_version == 7); | | |
| 697 | | | |
| 698 | for (self.base.options.objects) |obj| { | | |
| 699 | _ = try man.addFile(obj.path, null); | | |
| 700 | man.hash.add(obj.must_link); | | |
| 701 | } | | |
| 702 | for (comp.c_object_table.keys()) |key| { | | |
| 703 | _ = try man.addFile(key.status.success.object_path, null); | | |
| 704 | } | | |
| 705 | try man.addOptionalFile(module_obj_path); | | |
| 706 | // We can skip hashing libc and libc++ components that we are in charge of building from Zig | | |
| 707 | // installation sources because they are always a product of the compiler version + target information. | | |
| 708 | man.hash.add(stack_size); | | |
| 709 | man.hash.addOptional(self.base.options.pagezero_size); | | |
| 710 | man.hash.addOptional(self.base.options.search_strategy); | | |
| 711 | man.hash.addOptional(self.base.options.headerpad_size); | | |
| 712 | man.hash.add(self.base.options.headerpad_max_install_names); | | |
| 713 | man.hash.add(gc_sections); | | |
| 714 | man.hash.add(self.base.options.dead_strip_dylibs); | | |
| 715 | man.hash.add(self.base.options.strip); | | |
| 716 | man.hash.addListOfBytes(self.base.options.lib_dirs); | | |
| 717 | man.hash.addListOfBytes(self.base.options.framework_dirs); | | |
| 718 | link.hashAddSystemLibs(&man.hash, self.base.options.frameworks); | | |
| 719 | man.hash.addListOfBytes(self.base.options.rpath_list); | | |
| 720 | if (is_dyn_lib) { | | |
| 721 | man.hash.addOptionalBytes(self.base.options.install_name); | | |
| 722 | man.hash.addOptional(self.base.options.version); | | |
| 723 | } | | |
| 724 | link.hashAddSystemLibs(&man.hash, self.base.options.system_libs); | | |
| 725 | man.hash.addOptionalBytes(self.base.options.sysroot); | | |
| 726 | try man.addOptionalFile(self.base.options.entitlements); | | |
| 727 | | | |
| 728 | // We don't actually care whether it's a cache hit or miss; we just need the digest and the lock. | | |
| 729 | _ = try man.hit(); | | |
| 730 | digest = man.final(); | | |
| 731 | | | |
| 732 | var prev_digest_buf: [digest.len]u8 = undefined; | | |
| 733 | const prev_digest: []u8 = Cache.readSmallFile( | | |
| 734 | directory.handle, | | |
| 735 | id_symlink_basename, | | |
| 736 | &prev_digest_buf, | | |
| 737 | ) catch |err| blk: { | | |
| 738 | log.debug("MachO Zld new_digest={s} error: {s}", .{ | | |
| 739 | std.fmt.fmtSliceHexLower(&digest), | | |
| 740 | @errorName(err), | | |
| 741 | }); | | |
| 742 | // Handle this as a cache miss. | | |
| 743 | break :blk prev_digest_buf[0..0]; | | |
| 744 | }; | | |
| 745 | if (mem.eql(u8, prev_digest, &digest)) { | | |
| 746 | // Hot diggity dog! The output binary is already there. | | |
| 747 | log.debug("MachO Zld digest={s} match - skipping invocation", .{ | | |
| 748 | std.fmt.fmtSliceHexLower(&digest), | | |
| 749 | }); | | |
| 750 | self.base.lock = man.toOwnedLock(); | | |
| 751 | return; | | |
| 752 | } | | |
| 753 | log.debug("MachO Zld prev_digest={s} new_digest={s}", .{ | | |
| 754 | std.fmt.fmtSliceHexLower(prev_digest), | | |
| 755 | std.fmt.fmtSliceHexLower(&digest), | | |
| 756 | }); | | |
| 757 | | | |
| 758 | // We are about to change the output file to be different, so we invalidate the build hash now. | | |
| 759 | directory.handle.deleteFile(id_symlink_basename) catch |err| switch (err) { | | |
| 760 | error.FileNotFound => {}, | | |
| 761 | else => |e| return e, | | |
| 762 | }; | | |
| 763 | } | | |
| 764 | | | |
| 765 | if (self.base.options.output_mode == .Obj) { | | |
| 766 | // LLD's MachO driver does not support the equivalent of `-r` so we do a simple file copy | | |
| 767 | // here. TODO: think carefully about how we can avoid this redundant operation when doing | | |
| 768 | // build-obj. See also the corresponding TODO in linkAsArchive. | | |
| 769 | const the_object_path = blk: { | | |
| 770 | if (self.base.options.objects.len != 0) { | | |
| 771 | break :blk self.base.options.objects[0].path; | | |
| 772 | } | | |
| 773 | | | |
| 774 | if (comp.c_object_table.count() != 0) | | |
| 775 | break :blk comp.c_object_table.keys()[0].status.success.object_path; | | |
| 776 | | | |
| 777 | if (module_obj_path) |p| | | |
| 778 | break :blk p; | | |
| 779 | | | |
| 780 | // TODO I think this is unreachable. Audit this situation when solving the above TODO | | |
| 781 | // regarding eliding redundant object -> object transformations. | | |
| 782 | return error.NoObjectsToLink; | | |
| 783 | }; | | |
| 784 | // This can happen when using --enable-cache and using the stage1 backend. In this case | | |
| 785 | // we can skip the file copy. | | |
| 786 | if (!mem.eql(u8, the_object_path, full_out_path)) { | | |
| 787 | try fs.cwd().copyFile(the_object_path, fs.cwd(), full_out_path, .{}); | | |
| 788 | } | | |
| 789 | } else { | | |
| 790 | const sub_path = self.base.options.emit.?.sub_path; | | |
| 791 | if (self.base.file == null) { | | |
| 792 | self.base.file = try directory.handle.createFile(sub_path, .{ | | |
| 793 | .truncate = true, | | |
| 794 | .read = true, | | |
| 795 | .mode = link.determineMode(self.base.options), | | |
| 796 | }); | | |
| 797 | } | | |
| 798 | // Index 0 is always a null symbol. | | |
| 799 | try self.locals.append(gpa, .{ | | |
| 800 | .n_strx = 0, | | |
| 801 | .n_type = 0, | | |
| 802 | .n_sect = 0, | | |
| 803 | .n_desc = 0, | | |
| 804 | .n_value = 0, | | |
| 805 | }); | | |
| 806 | try self.strtab.buffer.append(gpa, 0); | | |
| 807 | try self.populateMissingMetadata(); | | |
| 808 | | | |
| 809 | var lib_not_found = false; | | |
| 810 | var framework_not_found = false; | | |
| 811 | | | |
| 812 | // Positional arguments to the linker such as object files and static archives. | | |
| 813 | var positionals = std.ArrayList([]const u8).init(arena); | | |
| 814 | try positionals.ensureUnusedCapacity(self.base.options.objects.len); | | |
| 815 | | | |
| 816 | var must_link_archives = std.StringArrayHashMap(void).init(arena); | | |
| 817 | try must_link_archives.ensureUnusedCapacity(self.base.options.objects.len); | | |
| 818 | | | |
| 819 | for (self.base.options.objects) |obj| { | | |
| 820 | if (must_link_archives.contains(obj.path)) continue; | | |
| 821 | if (obj.must_link) { | | |
| 822 | _ = must_link_archives.getOrPutAssumeCapacity(obj.path); | | |
| 823 | } else { | | |
| 824 | _ = positionals.appendAssumeCapacity(obj.path); | | |
| 825 | } | | |
| 826 | } | | |
| 827 | | | |
| 828 | for (comp.c_object_table.keys()) |key| { | | |
| 829 | try positionals.append(key.status.success.object_path); | | |
| 830 | } | | |
| 831 | | | |
| 832 | if (module_obj_path) |p| { | | |
| 833 | try positionals.append(p); | | |
| 834 | } | | |
| 835 | | | |
| 836 | if (comp.compiler_rt_lib) |lib| { | | |
| 837 | try positionals.append(lib.full_object_path); | | |
| 838 | } | | |
| 839 | | | |
| 840 | // libc++ dep | | |
| 841 | if (self.base.options.link_libcpp) { | | |
| 842 | try positionals.append(comp.libcxxabi_static_lib.?.full_object_path); | | |
| 843 | try positionals.append(comp.libcxx_static_lib.?.full_object_path); | | |
| 844 | } | | |
| 845 | | | |
| 846 | // Shared and static libraries passed via `-l` flag. | | |
| 847 | var candidate_libs = std.StringArrayHashMap(SystemLib).init(arena); | | |
| 848 | | | |
| 849 | const system_lib_names = self.base.options.system_libs.keys(); | | |
| 850 | for (system_lib_names) |system_lib_name| { | | |
| 851 | // By this time, we depend on these libs being dynamically linked libraries and not static libraries | | |
| 852 | // (the check for that needs to be earlier), but they could be full paths to .dylib files, in which | | |
| 853 | // case we want to avoid prepending "-l". | | |
| 854 | if (Compilation.classifyFileExt(system_lib_name) == .shared_library) { | | |
| 855 | try positionals.append(system_lib_name); | | |
| 856 | continue; | | |
| 857 | } | | |
| 858 | | | |
| 859 | const system_lib_info = self.base.options.system_libs.get(system_lib_name).?; | | |
| 860 | try candidate_libs.put(system_lib_name, .{ | | |
| 861 | .needed = system_lib_info.needed, | | |
| 862 | .weak = system_lib_info.weak, | | |
| 863 | }); | | |
| 864 | } | | |
| 865 | | | |
| 866 | var lib_dirs = std.ArrayList([]const u8).init(arena); | | |
| 867 | for (self.base.options.lib_dirs) |dir| { | | |
| 868 | if (try resolveSearchDir(arena, dir, self.base.options.sysroot)) |search_dir| { | | |
| 869 | try lib_dirs.append(search_dir); | | |
| 870 | } else { | | |
| 871 | log.warn("directory not found for '-L{s}'", .{dir}); | | |
| 872 | } | | |
| 873 | } | | |
| 874 | | | |
| 875 | var libs = std.StringArrayHashMap(SystemLib).init(arena); | | |
| 876 | | | |
| 877 | // Assume ld64 default -search_paths_first if no strategy specified. | | |
| 878 | const search_strategy = self.base.options.search_strategy orelse .paths_first; | | |
| 879 | outer: for (candidate_libs.keys()) |lib_name| { | | |
| 880 | switch (search_strategy) { | | |
| 881 | .paths_first => { | | |
| 882 | // Look in each directory for a dylib (stub first), and then for archive | | |
| 883 | for (lib_dirs.items) |dir| { | | |
| 884 | for (&[_][]const u8{ ".tbd", ".dylib", ".a" }) |ext| { | | |
| 885 | if (try resolveLib(arena, dir, lib_name, ext)) |full_path| { | | |
| 886 | try libs.put(full_path, candidate_libs.get(lib_name).?); | | |
| 887 | continue :outer; | | |
| 888 | } | | |
| 889 | } | | |
| 890 | } else { | | |
| 891 | log.warn("library not found for '-l{s}'", .{lib_name}); | | |
| 892 | lib_not_found = true; | | |
| 893 | } | | |
| 894 | }, | | |
| 895 | .dylibs_first => { | | |
| 896 | // First, look for a dylib in each search dir | | |
| 897 | for (lib_dirs.items) |dir| { | | |
| 898 | for (&[_][]const u8{ ".tbd", ".dylib" }) |ext| { | | |
| 899 | if (try resolveLib(arena, dir, lib_name, ext)) |full_path| { | | |
| 900 | try libs.put(full_path, candidate_libs.get(lib_name).?); | | |
| 901 | continue :outer; | | |
| 902 | } | | |
| 903 | } | | |
| 904 | } else for (lib_dirs.items) |dir| { | | |
| 905 | if (try resolveLib(arena, dir, lib_name, ".a")) |full_path| { | | |
| 906 | try libs.put(full_path, candidate_libs.get(lib_name).?); | | |
| 907 | } else { | | |
| 908 | log.warn("library not found for '-l{s}'", .{lib_name}); | | |
| 909 | lib_not_found = true; | | |
| 910 | } | | |
| 911 | } | | |
| 912 | }, | | |
| 913 | } | | |
| 914 | } | | |
| 915 | | | |
| 916 | if (lib_not_found) { | | |
| 917 | log.warn("Library search paths:", .{}); | | |
| 918 | for (lib_dirs.items) |dir| { | | |
| 919 | log.warn(" {s}", .{dir}); | | |
| 920 | } | | |
| 921 | } | | |
| 922 | | | |
| 923 | try self.resolveLibSystem(arena, comp, lib_dirs.items, &libs); | | |
| 924 | | | |
| 925 | // frameworks | | |
| 926 | var framework_dirs = std.ArrayList([]const u8).init(arena); | | |
| 927 | for (self.base.options.framework_dirs) |dir| { | | |
| 928 | if (try resolveSearchDir(arena, dir, self.base.options.sysroot)) |search_dir| { | | |
| 929 | try framework_dirs.append(search_dir); | | |
| 930 | } else { | | |
| 931 | log.warn("directory not found for '-F{s}'", .{dir}); | | |
| 932 | } | | |
| 933 | } | | |
| 934 | | | |
| 935 | outer: for (self.base.options.frameworks.keys()) |f_name| { | | |
| 936 | for (framework_dirs.items) |dir| { | | |
| 937 | for (&[_][]const u8{ ".tbd", ".dylib", "" }) |ext| { | | |
| 938 | if (try resolveFramework(arena, dir, f_name, ext)) |full_path| { | | |
| 939 | const info = self.base.options.frameworks.get(f_name).?; | | |
| 940 | try libs.put(full_path, .{ | | |
| 941 | .needed = info.needed, | | |
| 942 | .weak = info.weak, | | |
| 943 | }); | | |
| 944 | continue :outer; | | |
| 945 | } | | |
| 946 | } | | |
| 947 | } else { | | |
| 948 | log.warn("framework not found for '-framework {s}'", .{f_name}); | | |
| 949 | framework_not_found = true; | | |
| 950 | } | | |
| 951 | } | | |
| 952 | | | |
| 953 | if (framework_not_found) { | | |
| 954 | log.warn("Framework search paths:", .{}); | | |
| 955 | for (framework_dirs.items) |dir| { | | |
| 956 | log.warn(" {s}", .{dir}); | | |
| 957 | } | | |
| 958 | } | | |
| 959 | | | |
| 960 | if (self.base.options.verbose_link) { | | |
| 961 | var argv = std.ArrayList([]const u8).init(arena); | | |
| 962 | | | |
| 963 | try argv.append("zig"); | | |
| 964 | try argv.append("ld"); | | |
| 965 | | | |
| 966 | if (is_exe_or_dyn_lib) { | | |
| 967 | try argv.append("-dynamic"); | | |
| 968 | } | | |
| 969 | | | |
| 970 | if (is_dyn_lib) { | | |
| 971 | try argv.append("-dylib"); | | |
| 972 | | | |
| 973 | if (self.base.options.install_name) |install_name| { | | |
| 974 | try argv.append("-install_name"); | | |
| 975 | try argv.append(install_name); | | |
| 976 | } | | |
| 977 | } | | |
| 978 | | | |
| 979 | if (self.base.options.sysroot) |syslibroot| { | | |
| 980 | try argv.append("-syslibroot"); | | |
| 981 | try argv.append(syslibroot); | | |
| 982 | } | | |
| 983 | | | |
| 984 | for (self.base.options.rpath_list) |rpath| { | | |
| 985 | try argv.append("-rpath"); | | |
| 986 | try argv.append(rpath); | | |
| 987 | } | | |
| 988 | | | |
| 989 | if (self.base.options.pagezero_size) |pagezero_size| { | | |
| 990 | try argv.append("-pagezero_size"); | | |
| 991 | try argv.append(try std.fmt.allocPrint(arena, "0x{x}", .{pagezero_size})); | | |
| 992 | } | | |
| 993 | | | |
| 994 | if (self.base.options.search_strategy) |strat| switch (strat) { | | |
| 995 | .paths_first => try argv.append("-search_paths_first"), | | |
| 996 | .dylibs_first => try argv.append("-search_dylibs_first"), | | |
| 997 | }; | | |
| 998 | | | |
| 999 | if (self.base.options.headerpad_size) |headerpad_size| { | | |
| 1000 | try argv.append("-headerpad_size"); | | |
| 1001 | try argv.append(try std.fmt.allocPrint(arena, "0x{x}", .{headerpad_size})); | | |
| 1002 | } | | |
| 1003 | | | |
| 1004 | if (self.base.options.headerpad_max_install_names) { | | |
| 1005 | try argv.append("-headerpad_max_install_names"); | | |
| 1006 | } | | |
| 1007 | | | |
| 1008 | if (gc_sections) { | | |
| 1009 | try argv.append("-dead_strip"); | | |
| 1010 | } | | |
| 1011 | | | |
| 1012 | if (self.base.options.dead_strip_dylibs) { | | |
| 1013 | try argv.append("-dead_strip_dylibs"); | | |
| 1014 | } | | |
| 1015 | | | |
| 1016 | if (self.base.options.entry) |entry| { | | |
| 1017 | try argv.append("-e"); | | |
| 1018 | try argv.append(entry); | | |
| 1019 | } | | |
| 1020 | | | |
| 1021 | for (self.base.options.objects) |obj| { | | |
| 1022 | try argv.append(obj.path); | | |
| 1023 | } | | |
| 1024 | | | |
| 1025 | for (comp.c_object_table.keys()) |key| { | | |
| 1026 | try argv.append(key.status.success.object_path); | | |
| 1027 | } | | |
| 1028 | | | |
| 1029 | if (module_obj_path) |p| { | | |
| 1030 | try argv.append(p); | | |
| 1031 | } | | |
| 1032 | | | |
| 1033 | if (comp.compiler_rt_lib) |lib| { | | |
| 1034 | try argv.append(lib.full_object_path); | | |
| 1035 | } | | |
| 1036 | | | |
| 1037 | if (self.base.options.link_libcpp) { | | |
| 1038 | try argv.append(comp.libcxxabi_static_lib.?.full_object_path); | | |
| 1039 | try argv.append(comp.libcxx_static_lib.?.full_object_path); | | |
| 1040 | } | | |
| 1041 | | | |
| 1042 | try argv.append("-o"); | | |
| 1043 | try argv.append(full_out_path); | | |
| 1044 | | | |
| 1045 | try argv.append("-lSystem"); | | |
| 1046 | try argv.append("-lc"); | | |
| 1047 | | | |
| 1048 | for (self.base.options.system_libs.keys()) |l_name| { | | |
| 1049 | const info = self.base.options.system_libs.get(l_name).?; | | |
| 1050 | const arg = if (info.needed) | | |
| 1051 | try std.fmt.allocPrint(arena, "-needed-l{s}", .{l_name}) | | |
| 1052 | else if (info.weak) | | |
| 1053 | try std.fmt.allocPrint(arena, "-weak-l{s}", .{l_name}) | | |
| 1054 | else | | |
| 1055 | try std.fmt.allocPrint(arena, "-l{s}", .{l_name}); | | |
| 1056 | try argv.append(arg); | | |
| 1057 | } | | |
| 1058 | | | |
| 1059 | for (self.base.options.lib_dirs) |lib_dir| { | | |
| 1060 | try argv.append(try std.fmt.allocPrint(arena, "-L{s}", .{lib_dir})); | | |
| 1061 | } | | |
| 1062 | | | |
| 1063 | for (self.base.options.frameworks.keys()) |framework| { | | |
| 1064 | const info = self.base.options.frameworks.get(framework).?; | | |
| 1065 | const arg = if (info.needed) | | |
| 1066 | try std.fmt.allocPrint(arena, "-needed_framework {s}", .{framework}) | | |
| 1067 | else if (info.weak) | | |
| 1068 | try std.fmt.allocPrint(arena, "-weak_framework {s}", .{framework}) | | |
| 1069 | else | | |
| 1070 | try std.fmt.allocPrint(arena, "-framework {s}", .{framework}); | | |
| 1071 | try argv.append(arg); | | |
| 1072 | } | | |
| 1073 | | | |
| 1074 | for (self.base.options.framework_dirs) |framework_dir| { | | |
| 1075 | try argv.append(try std.fmt.allocPrint(arena, "-F{s}", .{framework_dir})); | | |
| 1076 | } | | |
| 1077 | | | |
| 1078 | if (is_dyn_lib and (self.base.options.allow_shlib_undefined orelse false)) { | | |
| 1079 | try argv.append("-undefined"); | | |
| 1080 | try argv.append("dynamic_lookup"); | | |
| 1081 | } | | |
| 1082 | | | |
| 1083 | for (must_link_archives.keys()) |lib| { | | |
| 1084 | try argv.append(try std.fmt.allocPrint(arena, "-force_load {s}", .{lib})); | | |
| 1085 | } | | |
| 1086 | | | |
| 1087 | Compilation.dump_argv(argv.items); | | |
| 1088 | } | | |
| 1089 | | | |
| 1090 | var dependent_libs = std.fifo.LinearFifo(struct { | | |
| 1091 | id: Dylib.Id, | | |
| 1092 | parent: u16, | | |
| 1093 | }, .Dynamic).init(arena); | | |
| 1094 | | | |
| 1095 | try self.parseInputFiles(positionals.items, self.base.options.sysroot, &dependent_libs); | | |
| 1096 | try self.parseAndForceLoadStaticArchives(must_link_archives.keys()); | | |
| 1097 | try self.parseLibs(libs.keys(), libs.values(), self.base.options.sysroot, &dependent_libs); | | |
| 1098 | try self.parseDependentLibs(self.base.options.sysroot, &dependent_libs); | | |
| 1099 | | | |
| 1100 | for (self.objects.items) |_, object_id| { | | |
| 1101 | try self.resolveSymbolsInObject(@intCast(u16, object_id)); | | |
| 1102 | } | | |
| 1103 | | | |
| 1104 | try self.resolveSymbolsInArchives(); | | |
| 1105 | try self.resolveDyldStubBinder(); | | |
| 1106 | try self.resolveSymbolsInDylibs(); | | |
| 1107 | try self.createMhExecuteHeaderSymbol(); | | |
| 1108 | try self.createDsoHandleSymbol(); | | |
| 1109 | try self.resolveSymbolsAtLoading(); | | |
| 1110 | | | |
| 1111 | if (self.unresolved.count() > 0) { | | |
| 1112 | return error.UndefinedSymbolReference; | | |
| 1113 | } | | |
| 1114 | if (lib_not_found) { | | |
| 1115 | return error.LibraryNotFound; | | |
| 1116 | } | | |
| 1117 | if (framework_not_found) { | | |
| 1118 | return error.FrameworkNotFound; | | |
| 1119 | } | | |
| 1120 | | | |
| 1121 | for (self.objects.items) |*object| { | | |
| 1122 | try object.scanInputSections(self); | | |
| 1123 | } | | |
| 1124 | | | |
| 1125 | try self.createDyldPrivateAtom(); | | |
| 1126 | try self.createTentativeDefAtoms(); | | |
| 1127 | try self.createStubHelperPreambleAtom(); | | |
| 1128 | | | |
| 1129 | for (self.objects.items) |*object, object_id| { | | |
| 1130 | try object.splitIntoAtomsOneShot(self, @intCast(u32, object_id)); | | |
| 1131 | } | | |
| 1132 | | | |
| 1133 | if (gc_sections) { | | |
| 1134 | try dead_strip.gcAtoms(self); | | |
| 1135 | } | | |
| 1136 | | | |
| 1137 | try self.allocateSegments(); | | |
| 1138 | try self.allocateSymbols(); | | |
| 1139 | | | |
| 1140 | try self.allocateSpecialSymbols(); | | |
| 1141 | | | |
| 1142 | if (build_options.enable_logging or true) { | | |
| 1143 | self.logSymtab(); | | |
| 1144 | self.logSections(); | | |
| 1145 | self.logAtoms(); | | |
| 1146 | } | | |
| 1147 | | | |
| 1148 | try self.writeAtomsOneShot(); | | |
| 1149 | | | |
| 1150 | var lc_buffer = std.ArrayList(u8).init(arena); | | |
| 1151 | const lc_writer = lc_buffer.writer(); | | |
| 1152 | var ncmds: u32 = 0; | | |
| 1153 | | | |
| 1154 | try self.writeLinkeditSegmentData(&ncmds, lc_writer); | | |
| 1155 | | | |
| 1156 | // If the last section of __DATA segment is zerofill section, we need to ensure | | |
| 1157 | // that the free space between the end of the last non-zerofill section of __DATA | | |
| 1158 | // segment and the beginning of __LINKEDIT segment is zerofilled as the loader will | | |
| 1159 | // copy-paste this space into memory for quicker zerofill operation. | | |
| 1160 | if (self.data_segment_cmd_index) |data_seg_id| blk: { | | |
| 1161 | var physical_zerofill_start: u64 = 0; | | |
| 1162 | const section_indexes = self.getSectionIndexes(data_seg_id); | | |
| 1163 | for (self.sections.items(.header)[section_indexes.start..section_indexes.end]) |header| { | | |
| 1164 | if (header.isZerofill() and header.size > 0) break; | | |
| 1165 | physical_zerofill_start = header.offset + header.size; | | |
| 1166 | } else break :blk; | | |
| 1167 | const linkedit = self.segments.items[self.linkedit_segment_cmd_index.?]; | | |
| 1168 | const physical_zerofill_size = math.cast(usize, linkedit.fileoff - physical_zerofill_start) orelse | | |
| 1169 | return error.Overflow; | | |
| 1170 | if (physical_zerofill_size > 0) { | | |
| 1171 | var padding = try self.base.allocator.alloc(u8, physical_zerofill_size); | | |
| 1172 | defer self.base.allocator.free(padding); | | |
| 1173 | mem.set(u8, padding, 0); | | |
| 1174 | try self.base.file.?.pwriteAll(padding, physical_zerofill_start); | | |
| 1175 | } | | |
| 1176 | } | | |
| 1177 | | | |
| 1178 | try writeDylinkerLC(&ncmds, lc_writer); | | |
| 1179 | try self.writeMainLC(&ncmds, lc_writer); | | |
| 1180 | try self.writeDylibIdLC(&ncmds, lc_writer); | | |
| 1181 | try self.writeRpathLCs(&ncmds, lc_writer); | | |
| 1182 | | | |
| 1183 | { | | |
| 1184 | try lc_writer.writeStruct(macho.source_version_command{ | | |
| 1185 | .cmdsize = @sizeOf(macho.source_version_command), | | |
| 1186 | .version = 0x0, | | |
| 1187 | }); | | |
| 1188 | ncmds += 1; | | |
| 1189 | } | | |
| 1190 | | | |
| 1191 | try self.writeBuildVersionLC(&ncmds, lc_writer); | | |
| 1192 | | | |
| 1193 | { | | |
| 1194 | var uuid_lc = macho.uuid_command{ | | |
| 1195 | .cmdsize = @sizeOf(macho.uuid_command), | | |
| 1196 | .uuid = undefined, | | |
| 1197 | }; | | |
| 1198 | std.crypto.random.bytes(&uuid_lc.uuid); | | |
| 1199 | try lc_writer.writeStruct(uuid_lc); | | |
| 1200 | ncmds += 1; | | |
| 1201 | } | | |
| 1202 | | | |
| 1203 | try self.writeLoadDylibLCs(&ncmds, lc_writer); | | |
| 1204 | | | |
| 1205 | const requires_codesig = blk: { | | |
| 1206 | if (self.base.options.entitlements) |_| break :blk true; | | |
| 1207 | if (cpu_arch == .aarch64 and (os_tag == .macos or abi == .simulator)) break :blk true; | | |
| 1208 | break :blk false; | | |
| 1209 | }; | | |
| 1210 | var codesig_offset: ?u32 = null; | | |
| 1211 | var codesig: ?CodeSignature = if (requires_codesig) blk: { | | |
| 1212 | // Preallocate space for the code signature. | | |
| 1213 | // We need to do this at this stage so that we have the load commands with proper values | | |
| 1214 | // written out to the file. | | |
| 1215 | // The most important here is to have the correct vm and filesize of the __LINKEDIT segment | | |
| 1216 | // where the code signature goes into. | | |
| 1217 | var codesig = CodeSignature.init(self.page_size); | | |
| 1218 | codesig.code_directory.ident = self.base.options.emit.?.sub_path; | | |
| 1219 | if (self.base.options.entitlements) |path| { | | |
| 1220 | try codesig.addEntitlements(arena, path); | | |
| 1221 | } | | |
| 1222 | codesig_offset = try self.writeCodeSignaturePadding(&codesig, &ncmds, lc_writer); | | |
| 1223 | break :blk codesig; | | |
| 1224 | } else null; | | |
| 1225 | | | |
| 1226 | var headers_buf = std.ArrayList(u8).init(arena); | | |
| 1227 | try self.writeSegmentHeaders(&ncmds, headers_buf.writer()); | | |
| 1228 | | | |
| 1229 | try self.base.file.?.pwriteAll(headers_buf.items, @sizeOf(macho.mach_header_64)); | | |
| 1230 | try self.base.file.?.pwriteAll(lc_buffer.items, @sizeOf(macho.mach_header_64) + headers_buf.items.len); | | |
| 1231 | | | |
| 1232 | try self.writeHeader(ncmds, @intCast(u32, lc_buffer.items.len + headers_buf.items.len)); | | |
| 1233 | | | |
| 1234 | if (codesig) |*csig| { | | |
| 1235 | try self.writeCodeSignature(csig, codesig_offset.?); // code signing always comes last | | |
| 1236 | } | | |
| 1237 | } | | |
| 1238 | | | |
| 1239 | if (!self.base.options.disable_lld_caching) { | | |
| 1240 | // Update the file with the digest. If it fails we can continue; it only | | |
| 1241 | // means that the next invocation will have an unnecessary cache miss. | | |
| 1242 | Cache.writeSmallFile(directory.handle, id_symlink_basename, &digest) catch |err| { | | |
| 1243 | log.debug("failed to save linking hash digest file: {s}", .{@errorName(err)}); | | |
| 1244 | }; | | |
| 1245 | // Again failure here only means an unnecessary cache miss. | | |
| 1246 | man.writeManifest() catch |err| { | | |
| 1247 | log.debug("failed to write cache manifest when linking: {s}", .{@errorName(err)}); | | |
| 1248 | }; | | |
| 1249 | // We hang on to this lock so that the output file path can be used without | | |
| 1250 | // other processes clobbering it. | | |
| 1251 | self.base.lock = man.toOwnedLock(); | | |
| 1252 | } | | |
| 1253 | } | | |
| 1254 | | | |
| 1255 | fn resolveLibSystem( | | |
| 1256 | self: *MachO, | 663 | self: *MachO, |
| 1257 | arena: Allocator, | 664 | arena: Allocator, |
| 1258 | comp: *Compilation, | 665 | comp: *Compilation, |
| ... | @@ -1295,7 +702,7 @@ fn resolveLibSystem( | ... | @@ -1295,7 +702,7 @@ fn resolveLibSystem( |
| 1295 | } | 702 | } |
| 1296 | } | 703 | } |
| 1297 | | 704 | |
| 1298 | fn resolveSearchDir( | 705 | pub fn resolveSearchDir( |
| 1299 | arena: Allocator, | 706 | arena: Allocator, |
| 1300 | dir: []const u8, | 707 | dir: []const u8, |
| 1301 | syslibroot: ?[]const u8, | 708 | syslibroot: ?[]const u8, |
| ... | @@ -1337,17 +744,7 @@ fn resolveSearchDir( | ... | @@ -1337,17 +744,7 @@ fn resolveSearchDir( |
| 1337 | return null; | 744 | return null; |
| 1338 | } | 745 | } |
| 1339 | | 746 | |
| 1340 | fn resolveSearchDirs(arena: Allocator, dirs: []const []const u8, syslibroot: ?[]const u8, out_dirs: anytype) !void { | 747 | pub fn resolveLib( |
| 1341 | for (dirs) |dir| { | | |
| 1342 | if (try resolveSearchDir(arena, dir, syslibroot)) |search_dir| { | | |
| 1343 | try out_dirs.append(search_dir); | | |
| 1344 | } else { | | |
| 1345 | log.warn("directory not found for '-L{s}'", .{dir}); | | |
| 1346 | } | | |
| 1347 | } | | |
| 1348 | } | | |
| 1349 | | | |
| 1350 | fn resolveLib( | | |
| 1351 | arena: Allocator, | 748 | arena: Allocator, |
| 1352 | search_dir: []const u8, | 749 | search_dir: []const u8, |
| 1353 | name: []const u8, | 750 | name: []const u8, |
| ... | @@ -1366,7 +763,7 @@ fn resolveLib( | ... | @@ -1366,7 +763,7 @@ fn resolveLib( |
| 1366 | return full_path; | 763 | return full_path; |
| 1367 | } | 764 | } |
| 1368 | | 765 | |
| 1369 | fn resolveFramework( | 766 | pub fn resolveFramework( |
| 1370 | arena: Allocator, | 767 | arena: Allocator, |
| 1371 | search_dir: []const u8, | 768 | search_dir: []const u8, |
| 1372 | name: []const u8, | 769 | name: []const u8, |
| ... | @@ -1576,7 +973,7 @@ pub fn parseDylib( | ... | @@ -1576,7 +973,7 @@ pub fn parseDylib( |
| 1576 | return true; | 973 | return true; |
| 1577 | } | 974 | } |
| 1578 | | 975 | |
| 1579 | fn parseInputFiles(self: *MachO, files: []const []const u8, syslibroot: ?[]const u8, dependent_libs: anytype) !void { | 976 | pub fn parseInputFiles(self: *MachO, files: []const []const u8, syslibroot: ?[]const u8, dependent_libs: anytype) !void { |
| 1580 | for (files) |file_name| { | 977 | for (files) |file_name| { |
| 1581 | const full_path = full_path: { | 978 | const full_path = full_path: { |
| 1582 | var buffer: [fs.MAX_PATH_BYTES]u8 = undefined; | 979 | var buffer: [fs.MAX_PATH_BYTES]u8 = undefined; |
| ... | @@ -1594,7 +991,7 @@ fn parseInputFiles(self: *MachO, files: []const []const u8, syslibroot: ?[]const | ... | @@ -1594,7 +991,7 @@ fn parseInputFiles(self: *MachO, files: []const []const u8, syslibroot: ?[]const |
| 1594 | } | 991 | } |
| 1595 | } | 992 | } |
| 1596 | | 993 | |
| 1597 | fn parseAndForceLoadStaticArchives(self: *MachO, files: []const []const u8) !void { | 994 | pub fn parseAndForceLoadStaticArchives(self: *MachO, files: []const []const u8) !void { |
| 1598 | for (files) |file_name| { | 995 | for (files) |file_name| { |
| 1599 | const full_path = full_path: { | 996 | const full_path = full_path: { |
| 1600 | var buffer: [fs.MAX_PATH_BYTES]u8 = undefined; | 997 | var buffer: [fs.MAX_PATH_BYTES]u8 = undefined; |
| ... | @@ -1607,10 +1004,10 @@ fn parseAndForceLoadStaticArchives(self: *MachO, files: []const []const u8) !voi | ... | @@ -1607,10 +1004,10 @@ fn parseAndForceLoadStaticArchives(self: *MachO, files: []const []const u8) !voi |
| 1607 | } | 1004 | } |
| 1608 | } | 1005 | } |
| 1609 | | 1006 | |
| 1610 | fn parseLibs( | 1007 | pub fn parseLibs( |
| 1611 | self: *MachO, | 1008 | self: *MachO, |
| 1612 | lib_names: []const []const u8, | 1009 | lib_names: []const []const u8, |
| 1613 | lib_infos: []const SystemLib, | 1010 | lib_infos: []const link.SystemLib, |
| 1614 | syslibroot: ?[]const u8, | 1011 | syslibroot: ?[]const u8, |
| 1615 | dependent_libs: anytype, | 1012 | dependent_libs: anytype, |
| 1616 | ) !void { | 1013 | ) !void { |
| ... | @@ -1628,7 +1025,7 @@ fn parseLibs( | ... | @@ -1628,7 +1025,7 @@ fn parseLibs( |
| 1628 | } | 1025 | } |
| 1629 | } | 1026 | } |
| 1630 | | 1027 | |
| 1631 | fn parseDependentLibs(self: *MachO, syslibroot: ?[]const u8, dependent_libs: anytype) !void { | 1028 | pub fn parseDependentLibs(self: *MachO, syslibroot: ?[]const u8, dependent_libs: anytype) !void { |
| 1632 | // At this point, we can now parse dependents of dylibs preserving the inclusion order of: | 1029 | // At this point, we can now parse dependents of dylibs preserving the inclusion order of: |
| 1633 | // 1) anything on the linker line is parsed first | 1030 | // 1) anything on the linker line is parsed first |
| 1634 | // 2) afterwards, we parse dependents of the included dylibs | 1031 | // 2) afterwards, we parse dependents of the included dylibs |
| ... | @@ -1673,168 +1070,6 @@ fn parseDependentLibs(self: *MachO, syslibroot: ?[]const u8, dependent_libs: any | ... | @@ -1673,168 +1070,6 @@ fn parseDependentLibs(self: *MachO, syslibroot: ?[]const u8, dependent_libs: any |
| 1673 | } | 1070 | } |
| 1674 | } | 1071 | } |
| 1675 | | 1072 | |
| 1676 | pub fn getOutputSection(self: *MachO, sect: macho.section_64) !?u8 { | | |
| 1677 | const segname = sect.segName(); | | |
| 1678 | const sectname = sect.sectName(); | | |
| 1679 | const res: ?u8 = blk: { | | |
| 1680 | if (mem.eql(u8, "__LLVM", segname)) { | | |
| 1681 | log.debug("TODO LLVM section: type 0x{x}, name '{s},{s}'", .{ | | |
| 1682 | sect.flags, segname, sectname, | | |
| 1683 | }); | | |
| 1684 | break :blk null; | | |
| 1685 | } | | |
| 1686 | | | |
| 1687 | if (sect.isCode()) { | | |
| 1688 | if (self.text_section_index == null) { | | |
| 1689 | self.text_section_index = try self.initSection( | | |
| 1690 | "__TEXT", | | |
| 1691 | "__text", | | |
| 1692 | sect.size, | | |
| 1693 | sect.@"align", | | |
| 1694 | .{ | | |
| 1695 | .flags = macho.S_REGULAR | | | |
| 1696 | macho.S_ATTR_PURE_INSTRUCTIONS | | | |
| 1697 | macho.S_ATTR_SOME_INSTRUCTIONS, | | |
| 1698 | }, | | |
| 1699 | ); | | |
| 1700 | } | | |
| 1701 | break :blk self.text_section_index.?; | | |
| 1702 | } | | |
| 1703 | | | |
| 1704 | if (sect.isDebug()) { | | |
| 1705 | // TODO debug attributes | | |
| 1706 | if (mem.eql(u8, "__LD", segname) and mem.eql(u8, "__compact_unwind", sectname)) { | | |
| 1707 | log.debug("TODO compact unwind section: type 0x{x}, name '{s},{s}'", .{ | | |
| 1708 | sect.flags, segname, sectname, | | |
| 1709 | }); | | |
| 1710 | } | | |
| 1711 | break :blk null; | | |
| 1712 | } | | |
| 1713 | | | |
| 1714 | switch (sect.@"type"()) { | | |
| 1715 | macho.S_4BYTE_LITERALS, | | |
| 1716 | macho.S_8BYTE_LITERALS, | | |
| 1717 | macho.S_16BYTE_LITERALS, | | |
| 1718 | => { | | |
| 1719 | break :blk self.getSectionByName("__TEXT", "__const") orelse try self.initSection( | | |
| 1720 | "__TEXT", | | |
| 1721 | "__const", | | |
| 1722 | sect.size, | | |
| 1723 | sect.@"align", | | |
| 1724 | .{}, | | |
| 1725 | ); | | |
| 1726 | }, | | |
| 1727 | macho.S_CSTRING_LITERALS => { | | |
| 1728 | if (mem.startsWith(u8, sectname, "__objc")) { | | |
| 1729 | break :blk self.getSectionByName(segname, sectname) orelse try self.initSection( | | |
| 1730 | segname, | | |
| 1731 | sectname, | | |
| 1732 | sect.size, | | |
| 1733 | sect.@"align", | | |
| 1734 | .{}, | | |
| 1735 | ); | | |
| 1736 | } | | |
| 1737 | break :blk self.getSectionByName("__TEXT", "__cstring") orelse try self.initSection( | | |
| 1738 | "__TEXT", | | |
| 1739 | "__cstring", | | |
| 1740 | sect.size, | | |
| 1741 | sect.@"align", | | |
| 1742 | .{ .flags = macho.S_CSTRING_LITERALS }, | | |
| 1743 | ); | | |
| 1744 | }, | | |
| 1745 | macho.S_MOD_INIT_FUNC_POINTERS, | | |
| 1746 | macho.S_MOD_TERM_FUNC_POINTERS, | | |
| 1747 | => { | | |
| 1748 | break :blk self.getSectionByName("__DATA_CONST", sectname) orelse try self.initSection( | | |
| 1749 | "__DATA_CONST", | | |
| 1750 | sectname, | | |
| 1751 | sect.size, | | |
| 1752 | sect.@"align", | | |
| 1753 | .{ .flags = sect.flags }, | | |
| 1754 | ); | | |
| 1755 | }, | | |
| 1756 | macho.S_LITERAL_POINTERS, | | |
| 1757 | macho.S_ZEROFILL, | | |
| 1758 | macho.S_THREAD_LOCAL_VARIABLES, | | |
| 1759 | macho.S_THREAD_LOCAL_VARIABLE_POINTERS, | | |
| 1760 | macho.S_THREAD_LOCAL_REGULAR, | | |
| 1761 | macho.S_THREAD_LOCAL_ZEROFILL, | | |
| 1762 | => { | | |
| 1763 | break :blk self.getSectionByName(segname, sectname) orelse try self.initSection( | | |
| 1764 | segname, | | |
| 1765 | sectname, | | |
| 1766 | sect.size, | | |
| 1767 | sect.@"align", | | |
| 1768 | .{ .flags = sect.flags }, | | |
| 1769 | ); | | |
| 1770 | }, | | |
| 1771 | macho.S_COALESCED => { | | |
| 1772 | break :blk self.getSectionByName(segname, sectname) orelse try self.initSection( | | |
| 1773 | segname, | | |
| 1774 | sectname, | | |
| 1775 | sect.size, | | |
| 1776 | sect.@"align", | | |
| 1777 | .{}, | | |
| 1778 | ); | | |
| 1779 | }, | | |
| 1780 | macho.S_REGULAR => { | | |
| 1781 | if (mem.eql(u8, segname, "__TEXT")) { | | |
| 1782 | if (mem.eql(u8, sectname, "__rodata") or | | |
| 1783 | mem.eql(u8, sectname, "__typelink") or | | |
| 1784 | mem.eql(u8, sectname, "__itablink") or | | |
| 1785 | mem.eql(u8, sectname, "__gosymtab") or | | |
| 1786 | mem.eql(u8, sectname, "__gopclntab")) | | |
| 1787 | { | | |
| 1788 | break :blk self.getSectionByName("__DATA_CONST", "__const") orelse try self.initSection( | | |
| 1789 | "__DATA_CONST", | | |
| 1790 | "__const", | | |
| 1791 | sect.size, | | |
| 1792 | sect.@"align", | | |
| 1793 | .{}, | | |
| 1794 | ); | | |
| 1795 | } | | |
| 1796 | } | | |
| 1797 | if (mem.eql(u8, segname, "__DATA")) { | | |
| 1798 | if (mem.eql(u8, sectname, "__const") or | | |
| 1799 | mem.eql(u8, sectname, "__cfstring") or | | |
| 1800 | mem.eql(u8, sectname, "__objc_classlist") or | | |
| 1801 | mem.eql(u8, sectname, "__objc_imageinfo")) | | |
| 1802 | { | | |
| 1803 | break :blk self.getSectionByName("__DATA_CONST", sectname) orelse | | |
| 1804 | try self.initSection( | | |
| 1805 | "__DATA_CONST", | | |
| 1806 | sectname, | | |
| 1807 | sect.size, | | |
| 1808 | sect.@"align", | | |
| 1809 | .{}, | | |
| 1810 | ); | | |
| 1811 | } else if (mem.eql(u8, sectname, "__data")) { | | |
| 1812 | if (self.data_section_index == null) { | | |
| 1813 | self.data_section_index = try self.initSection( | | |
| 1814 | segname, | | |
| 1815 | sectname, | | |
| 1816 | sect.size, | | |
| 1817 | sect.@"align", | | |
| 1818 | .{}, | | |
| 1819 | ); | | |
| 1820 | } | | |
| 1821 | break :blk self.data_section_index.?; | | |
| 1822 | } | | |
| 1823 | } | | |
| 1824 | break :blk self.getSectionByName(segname, sectname) orelse try self.initSection( | | |
| 1825 | segname, | | |
| 1826 | sectname, | | |
| 1827 | sect.size, | | |
| 1828 | sect.@"align", | | |
| 1829 | .{}, | | |
| 1830 | ); | | |
| 1831 | }, | | |
| 1832 | else => break :blk null, | | |
| 1833 | } | | |
| 1834 | }; | | |
| 1835 | return res; | | |
| 1836 | } | | |
| 1837 | | | |
| 1838 | pub fn createEmptyAtom(gpa: Allocator, sym_index: u32, size: u64, alignment: u32) !*Atom { | 1073 | pub fn createEmptyAtom(gpa: Allocator, sym_index: u32, size: u64, alignment: u32) !*Atom { |
| 1839 | const size_usize = math.cast(usize, size) orelse return error.Overflow; | 1074 | const size_usize = math.cast(usize, size) orelse return error.Overflow; |
| 1840 | const atom = try gpa.create(Atom); | 1075 | const atom = try gpa.create(Atom); |
| ... | @@ -1850,64 +1085,51 @@ pub fn createEmptyAtom(gpa: Allocator, sym_index: u32, size: u64, alignment: u32 | ... | @@ -1850,64 +1085,51 @@ pub fn createEmptyAtom(gpa: Allocator, sym_index: u32, size: u64, alignment: u32 |
| 1850 | return atom; | 1085 | return atom; |
| 1851 | } | 1086 | } |
| 1852 | | 1087 | |
| 1853 | pub fn writeAtom(self: *MachO, atom: *Atom, sect_id: u8) !void { | 1088 | pub fn writeAtom(self: *MachO, atom: *Atom, code: []const u8) !void { |
| 1854 | const section = self.sections.get(sect_id); | 1089 | // TODO: temporary sanity check |
| | 1090 | assert(atom.code.items.len == 0); |
| | 1091 | assert(atom.relocs.items.len == 0); |
| | 1092 | assert(atom.rebases.items.len == 0); |
| | 1093 | assert(atom.bindings.items.len == 0); |
| | 1094 | assert(atom.lazy_bindings.items.len == 0); |
| | 1095 | |
| 1855 | const sym = atom.getSymbol(self); | 1096 | const sym = atom.getSymbol(self); |
| | 1097 | const section = self.sections.get(sym.n_sect - 1); |
| 1856 | const file_offset = section.header.offset + sym.n_value - section.header.addr; | 1098 | const file_offset = section.header.offset + sym.n_value - section.header.addr; |
| 1857 | try atom.resolveRelocs(self); | | |
| 1858 | log.debug("writing atom for symbol {s} at file offset 0x{x}", .{ atom.getName(self), file_offset }); | 1099 | log.debug("writing atom for symbol {s} at file offset 0x{x}", .{ atom.getName(self), file_offset }); |
| 1859 | try self.base.file.?.pwriteAll(atom.code.items, file_offset); | 1100 | try self.base.file.?.pwriteAll(code, file_offset); |
| | 1101 | try atom.resolveRelocations(self); |
| 1860 | } | 1102 | } |
| 1861 | | 1103 | |
| 1862 | fn allocateSymbols(self: *MachO) !void { | 1104 | fn writePtrWidthAtom(self: *MachO, atom: *Atom) !void { |
| 1863 | const slice = self.sections.slice(); | 1105 | var buffer: [@sizeOf(u64)]u8 = [_]u8{0} ** @sizeOf(u64); |
| 1864 | for (slice.items(.last_atom)) |last_atom, sect_id| { | 1106 | try self.writeAtom(atom, &buffer); |
| 1865 | const header = slice.items(.header)[sect_id]; | 1107 | } |
| 1866 | var atom = last_atom orelse continue; | | |
| 1867 | | 1108 | |
| 1868 | while (atom.prev) |prev| { | 1109 | fn markRelocsDirtyByTarget(self: *MachO, target: SymbolWithLoc) void { |
| 1869 | atom = prev; | 1110 | // TODO: reverse-lookup might come in handy here |
| | 1111 | var it = self.relocs.valueIterator(); |
| | 1112 | while (it.next()) |relocs| { |
| | 1113 | for (relocs.items) |*reloc| { |
| | 1114 | if (!reloc.target.eql(target)) continue; |
| | 1115 | reloc.dirty = true; |
| 1870 | } | 1116 | } |
| | 1117 | } |
| | 1118 | } |
| 1871 | | 1119 | |
| 1872 | const n_sect = @intCast(u8, sect_id + 1); | 1120 | fn markRelocsDirtyByAddress(self: *MachO, addr: u64) void { |
| 1873 | var base_vaddr = header.addr; | 1121 | var it = self.relocs.valueIterator(); |
| 1874 | | 1122 | while (it.next()) |relocs| { |
| 1875 | log.debug("allocating local symbols in sect({d}, '{s},{s}')", .{ | 1123 | for (relocs.items) |*reloc| { |
| 1876 | n_sect, | 1124 | const target_atom = reloc.getTargetAtom(self) orelse continue; |
| 1877 | header.segName(), | 1125 | const target_sym = target_atom.getSymbol(self); |
| 1878 | header.sectName(), | 1126 | if (target_sym.n_value < addr) continue; |
| 1879 | }); | 1127 | reloc.dirty = true; |
| 1880 | | | |
| 1881 | while (true) { | | |
| 1882 | const alignment = try math.powi(u32, 2, atom.alignment); | | |
| 1883 | base_vaddr = mem.alignForwardGeneric(u64, base_vaddr, alignment); | | |
| 1884 | | | |
| 1885 | const sym = atom.getSymbolPtr(self); | | |
| 1886 | sym.n_value = base_vaddr; | | |
| 1887 | sym.n_sect = n_sect; | | |
| 1888 | | | |
| 1889 | log.debug(" ATOM(%{d}, '{s}') @{x}", .{ atom.sym_index, atom.getName(self), base_vaddr }); | | |
| 1890 | | | |
| 1891 | // Update each symbol contained within the atom | | |
| 1892 | for (atom.contained.items) |sym_at_off| { | | |
| 1893 | const contained_sym = self.getSymbolPtr(.{ | | |
| 1894 | .sym_index = sym_at_off.sym_index, | | |
| 1895 | .file = atom.file, | | |
| 1896 | }); | | |
| 1897 | contained_sym.n_value = base_vaddr + sym_at_off.offset; | | |
| 1898 | contained_sym.n_sect = n_sect; | | |
| 1899 | } | | |
| 1900 | | | |
| 1901 | base_vaddr += atom.size; | | |
| 1902 | | | |
| 1903 | if (atom.next) |next| { | | |
| 1904 | atom = next; | | |
| 1905 | } else break; | | |
| 1906 | } | 1128 | } |
| 1907 | } | 1129 | } |
| 1908 | } | 1130 | } |
| 1909 | | 1131 | |
| 1910 | fn allocateSpecialSymbols(self: *MachO) !void { | 1132 | pub fn allocateSpecialSymbols(self: *MachO) !void { |
| 1911 | for (&[_][]const u8{ | 1133 | for (&[_][]const u8{ |
| 1912 | "___dso_handle", | 1134 | "___dso_handle", |
| 1913 | "__mh_execute_header", | 1135 | "__mh_execute_header", |
| ... | @@ -1915,7 +1137,10 @@ fn allocateSpecialSymbols(self: *MachO) !void { | ... | @@ -1915,7 +1137,10 @@ fn allocateSpecialSymbols(self: *MachO) !void { |
| 1915 | const global = self.getGlobal(name) orelse continue; | 1137 | const global = self.getGlobal(name) orelse continue; |
| 1916 | if (global.file != null) continue; | 1138 | if (global.file != null) continue; |
| 1917 | const sym = self.getSymbolPtr(global); | 1139 | const sym = self.getSymbolPtr(global); |
| 1918 | const seg = self.segments.items[self.text_segment_cmd_index.?]; | 1140 | const seg = switch (self.mode) { |
| | 1141 | .incremental => self.getSegment(self.text_section_index.?), |
| | 1142 | .one_shot => self.segments.items[self.text_segment_cmd_index.?], |
| | 1143 | }; |
| 1919 | sym.n_sect = 1; | 1144 | sym.n_sect = 1; |
| 1920 | sym.n_value = seg.vmaddr; | 1145 | sym.n_value = seg.vmaddr; |
| 1921 | | 1146 | |
| ... | @@ -1926,214 +1151,133 @@ fn allocateSpecialSymbols(self: *MachO) !void { | ... | @@ -1926,214 +1151,133 @@ fn allocateSpecialSymbols(self: *MachO) !void { |
| 1926 | } | 1151 | } |
| 1927 | } | 1152 | } |
| 1928 | | 1153 | |
| 1929 | fn writeAtomsOneShot(self: *MachO) !void { | 1154 | pub fn createGotAtom(self: *MachO, target: SymbolWithLoc) !*Atom { |
| 1930 | assert(self.mode == .one_shot); | | |
| 1931 | | | |
| 1932 | const gpa = self.base.allocator; | 1155 | const gpa = self.base.allocator; |
| 1933 | const slice = self.sections.slice(); | | |
| 1934 | | 1156 | |
| 1935 | for (slice.items(.last_atom)) |last_atom, sect_id| { | 1157 | const sym_index = try self.allocateSymbol(); |
| 1936 | const header = slice.items(.header)[sect_id]; | 1158 | const atom = switch (self.mode) { |
| 1937 | if (header.size == 0) continue; | 1159 | .incremental => blk: { |
| 1938 | var atom = last_atom.?; | 1160 | const atom = try gpa.create(Atom); |
| | 1161 | atom.* = Atom.empty; |
| | 1162 | atom.sym_index = sym_index; |
| | 1163 | atom.size = @sizeOf(u64); |
| | 1164 | atom.alignment = @alignOf(u64); |
| | 1165 | break :blk atom; |
| | 1166 | }, |
| | 1167 | .one_shot => try MachO.createEmptyAtom(gpa, sym_index, @sizeOf(u64), 3), |
| | 1168 | }; |
| | 1169 | errdefer gpa.destroy(atom); |
| 1939 | | 1170 | |
| 1940 | if (header.isZerofill()) continue; | 1171 | try self.managed_atoms.append(gpa, atom); |
| | 1172 | try self.atom_by_index_table.putNoClobber(gpa, atom.sym_index, atom); |
| 1941 | | 1173 | |
| 1942 | var buffer = std.ArrayList(u8).init(gpa); | 1174 | const sym = atom.getSymbolPtr(self); |
| 1943 | defer buffer.deinit(); | 1175 | sym.n_type = macho.N_SECT; |
| 1944 | try buffer.ensureTotalCapacity(math.cast(usize, header.size) orelse return error.Overflow); | 1176 | sym.n_sect = self.got_section_index.? + 1; |
| 1945 | | 1177 | |
| 1946 | log.debug("writing atoms in {s},{s}", .{ header.segName(), header.sectName() }); | 1178 | if (self.mode == .incremental) { |
| | 1179 | sym.n_value = try self.allocateAtom(atom, atom.size, @alignOf(u64)); |
| 1947 | | 1180 | |
| 1948 | while (atom.prev) |prev| { | 1181 | log.debug("allocated GOT atom at 0x{x}", .{sym.n_value}); |
| 1949 | atom = prev; | | |
| 1950 | } | | |
| 1951 | | 1182 | |
| 1952 | while (true) { | 1183 | try atom.addRelocation(self, .{ |
| 1953 | const this_sym = atom.getSymbol(self); | 1184 | .@"type" = switch (self.base.options.target.cpu.arch) { |
| 1954 | const padding_size: usize = if (atom.next) |next| blk: { | 1185 | .aarch64 => @enumToInt(macho.reloc_type_arm64.ARM64_RELOC_UNSIGNED), |
| 1955 | const next_sym = next.getSymbol(self); | 1186 | .x86_64 => @enumToInt(macho.reloc_type_x86_64.X86_64_RELOC_UNSIGNED), |
| 1956 | const size = next_sym.n_value - (this_sym.n_value + atom.size); | 1187 | else => unreachable, |
| 1957 | break :blk math.cast(usize, size) orelse return error.Overflow; | 1188 | }, |
| 1958 | } else 0; | 1189 | .target = target, |
| | 1190 | .offset = 0, |
| | 1191 | .addend = 0, |
| | 1192 | .pcrel = false, |
| | 1193 | .length = 3, |
| | 1194 | }); |
| 1959 | | 1195 | |
| 1960 | log.debug(" (adding ATOM(%{d}, '{s}') from object({?d}) to buffer)", .{ | 1196 | const target_sym = self.getSymbol(target); |
| 1961 | atom.sym_index, | 1197 | if (target_sym.undf()) { |
| 1962 | atom.getName(self), | 1198 | try atom.addBinding(self, .{ |
| 1963 | atom.file, | 1199 | .target = self.getGlobal(self.getSymbolName(target)).?, |
| | 1200 | .offset = 0, |
| 1964 | }); | 1201 | }); |
| 1965 | if (padding_size > 0) { | 1202 | } else { |
| 1966 | log.debug(" (with padding {x})", .{padding_size}); | 1203 | try atom.addRebase(self, 0); |
| 1967 | } | | |
| 1968 | | | |
| 1969 | try atom.resolveRelocs(self); | | |
| 1970 | buffer.appendSliceAssumeCapacity(atom.code.items); | | |
| 1971 | | | |
| 1972 | var i: usize = 0; | | |
| 1973 | while (i < padding_size) : (i += 1) { | | |
| 1974 | // TODO with NOPs | | |
| 1975 | buffer.appendAssumeCapacity(0); | | |
| 1976 | } | | |
| 1977 | | | |
| 1978 | if (atom.next) |next| { | | |
| 1979 | atom = next; | | |
| 1980 | } else { | | |
| 1981 | assert(buffer.items.len == header.size); | | |
| 1982 | log.debug(" (writing at file offset 0x{x})", .{header.offset}); | | |
| 1983 | try self.base.file.?.pwriteAll(buffer.items, header.offset); | | |
| 1984 | break; | | |
| 1985 | } | | |
| 1986 | } | 1204 | } |
| 1987 | } | 1205 | } else { |
| 1988 | } | 1206 | try atom.relocs.append(gpa, .{ |
| 1989 | | 1207 | .offset = 0, |
| 1990 | fn writePadding(self: *MachO, sect_id: u8, size: usize, writer: anytype) !void { | 1208 | .target = target, |
| 1991 | const header = self.sections.items(.header)[sect_id]; | 1209 | .addend = 0, |
| 1992 | const min_alignment: u3 = if (!header.isCode()) | 1210 | .subtractor = null, |
| 1993 | 1 | 1211 | .pcrel = false, |
| 1994 | else switch (self.base.options.target.cpu.arch) { | 1212 | .length = 3, |
| 1995 | .aarch64 => @sizeOf(u32), | 1213 | .@"type" = switch (self.base.options.target.cpu.arch) { |
| 1996 | .x86_64 => @as(u3, 1), | 1214 | .aarch64 => @enumToInt(macho.reloc_type_arm64.ARM64_RELOC_UNSIGNED), |
| 1997 | else => unreachable, | 1215 | .x86_64 => @enumToInt(macho.reloc_type_x86_64.X86_64_RELOC_UNSIGNED), |
| 1998 | }; | 1216 | else => unreachable, |
| 1999 | | | |
| 2000 | const len = @divExact(size, min_alignment); | | |
| 2001 | var i: usize = 0; | | |
| 2002 | while (i < len) : (i += 1) { | | |
| 2003 | if (!header.isCode()) { | | |
| 2004 | try writer.writeByte(0); | | |
| 2005 | } else switch (self.base.options.target.cpu.arch) { | | |
| 2006 | .aarch64 => { | | |
| 2007 | const inst = aarch64.Instruction.nop(); | | |
| 2008 | try writer.writeIntLittle(u32, inst.toU32()); | | |
| 2009 | }, | | |
| 2010 | .x86_64 => { | | |
| 2011 | try writer.writeByte(0x90); | | |
| 2012 | }, | 1217 | }, |
| 2013 | else => unreachable, | 1218 | }); |
| 2014 | } | | |
| 2015 | } | | |
| 2016 | } | | |
| 2017 | | | |
| 2018 | fn writeAtomsIncremental(self: *MachO) !void { | | |
| 2019 | assert(self.mode == .incremental); | | |
| 2020 | | | |
| 2021 | const slice = self.sections.slice(); | | |
| 2022 | for (slice.items(.last_atom)) |last, i| { | | |
| 2023 | var atom: *Atom = last orelse continue; | | |
| 2024 | const sect_i = @intCast(u8, i); | | |
| 2025 | const header = slice.items(.header)[sect_i]; | | |
| 2026 | | | |
| 2027 | if (header.isZerofill()) continue; | | |
| 2028 | | | |
| 2029 | log.debug("writing atoms in {s},{s}", .{ header.segName(), header.sectName() }); | | |
| 2030 | | | |
| 2031 | while (true) { | | |
| 2032 | if (atom.dirty) { | | |
| 2033 | try self.writeAtom(atom, sect_i); | | |
| 2034 | atom.dirty = false; | | |
| 2035 | } | | |
| 2036 | | 1219 | |
| 2037 | if (atom.prev) |prev| { | 1220 | const target_sym = self.getSymbol(target); |
| 2038 | atom = prev; | 1221 | if (target_sym.undf()) { |
| 2039 | } else break; | 1222 | const global = self.getGlobal(self.getSymbolName(target)).?; |
| | 1223 | try atom.bindings.append(gpa, .{ |
| | 1224 | .target = global, |
| | 1225 | .offset = 0, |
| | 1226 | }); |
| | 1227 | } else { |
| | 1228 | try atom.rebases.append(gpa, 0); |
| 2040 | } | 1229 | } |
| 2041 | } | | |
| 2042 | } | | |
| 2043 | | | |
| 2044 | pub fn createGotAtom(self: *MachO, target: SymbolWithLoc) !*Atom { | | |
| 2045 | const gpa = self.base.allocator; | | |
| 2046 | const sym_index = try self.allocateSymbol(); | | |
| 2047 | const atom = try MachO.createEmptyAtom(gpa, sym_index, @sizeOf(u64), 3); | | |
| 2048 | const sym = atom.getSymbolPtr(self); | | |
| 2049 | sym.n_type = macho.N_SECT; | | |
| 2050 | | | |
| 2051 | try atom.relocs.append(gpa, .{ | | |
| 2052 | .offset = 0, | | |
| 2053 | .target = target, | | |
| 2054 | .addend = 0, | | |
| 2055 | .subtractor = null, | | |
| 2056 | .pcrel = false, | | |
| 2057 | .length = 3, | | |
| 2058 | .@"type" = switch (self.base.options.target.cpu.arch) { | | |
| 2059 | .aarch64 => @enumToInt(macho.reloc_type_arm64.ARM64_RELOC_UNSIGNED), | | |
| 2060 | .x86_64 => @enumToInt(macho.reloc_type_x86_64.X86_64_RELOC_UNSIGNED), | | |
| 2061 | else => unreachable, | | |
| 2062 | }, | | |
| 2063 | }); | | |
| 2064 | | 1230 | |
| 2065 | const target_sym = self.getSymbol(target); | 1231 | try self.addAtomToSection(atom); |
| 2066 | if (target_sym.undf()) { | | |
| 2067 | const global = self.getGlobal(self.getSymbolName(target)).?; | | |
| 2068 | try atom.bindings.append(gpa, .{ | | |
| 2069 | .target = global, | | |
| 2070 | .offset = 0, | | |
| 2071 | }); | | |
| 2072 | } else { | | |
| 2073 | try atom.rebases.append(gpa, 0); | | |
| 2074 | } | 1232 | } |
| 2075 | | 1233 | |
| 2076 | try self.managed_atoms.append(gpa, atom); | | |
| 2077 | try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom); | | |
| 2078 | | | |
| 2079 | try self.allocateAtomCommon(atom, self.got_section_index.?); | | |
| 2080 | | | |
| 2081 | return atom; | | |
| 2082 | } | | |
| 2083 | | | |
| 2084 | pub fn createTlvPtrAtom(self: *MachO, target: SymbolWithLoc) !*Atom { | | |
| 2085 | const gpa = self.base.allocator; | | |
| 2086 | const sym_index = try self.allocateSymbol(); | | |
| 2087 | const atom = try MachO.createEmptyAtom(gpa, sym_index, @sizeOf(u64), 3); | | |
| 2088 | const sym = atom.getSymbolPtr(self); | | |
| 2089 | sym.n_type = macho.N_SECT; | | |
| 2090 | | | |
| 2091 | const target_sym = self.getSymbol(target); | | |
| 2092 | assert(target_sym.undf()); | | |
| 2093 | | | |
| 2094 | const global = self.getGlobal(self.getSymbolName(target)).?; | | |
| 2095 | try atom.bindings.append(gpa, .{ | | |
| 2096 | .target = global, | | |
| 2097 | .offset = 0, | | |
| 2098 | }); | | |
| 2099 | | | |
| 2100 | try self.managed_atoms.append(gpa, atom); | | |
| 2101 | try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom); | | |
| 2102 | | | |
| 2103 | const match = (try self.getOutputSection(.{ | | |
| 2104 | .segname = makeStaticString("__DATA"), | | |
| 2105 | .sectname = makeStaticString("__thread_ptrs"), | | |
| 2106 | .flags = macho.S_THREAD_LOCAL_VARIABLE_POINTERS, | | |
| 2107 | })).?; | | |
| 2108 | try self.allocateAtomCommon(atom, match); | | |
| 2109 | | | |
| 2110 | return atom; | 1234 | return atom; |
| 2111 | } | 1235 | } |
| 2112 | | 1236 | |
| 2113 | fn createDyldPrivateAtom(self: *MachO) !void { | 1237 | pub fn createDyldPrivateAtom(self: *MachO) !void { |
| 2114 | if (self.dyld_stub_binder_index == null) return; | 1238 | if (self.dyld_stub_binder_index == null) return; |
| 2115 | if (self.dyld_private_atom != null) return; | 1239 | if (self.dyld_private_atom != null) return; |
| 2116 | | 1240 | |
| 2117 | const gpa = self.base.allocator; | 1241 | const gpa = self.base.allocator; |
| | 1242 | |
| 2118 | const sym_index = try self.allocateSymbol(); | 1243 | const sym_index = try self.allocateSymbol(); |
| 2119 | const atom = try MachO.createEmptyAtom(gpa, sym_index, @sizeOf(u64), 3); | 1244 | const atom = switch (self.mode) { |
| | 1245 | .incremental => blk: { |
| | 1246 | const atom = try gpa.create(Atom); |
| | 1247 | atom.* = Atom.empty; |
| | 1248 | atom.sym_index = sym_index; |
| | 1249 | atom.size = @sizeOf(u64); |
| | 1250 | atom.alignment = @alignOf(u64); |
| | 1251 | break :blk atom; |
| | 1252 | }, |
| | 1253 | .one_shot => try MachO.createEmptyAtom(gpa, sym_index, @sizeOf(u64), 3), |
| | 1254 | }; |
| | 1255 | errdefer gpa.destroy(atom); |
| | 1256 | |
| 2120 | const sym = atom.getSymbolPtr(self); | 1257 | const sym = atom.getSymbolPtr(self); |
| 2121 | sym.n_type = macho.N_SECT; | 1258 | sym.n_type = macho.N_SECT; |
| | 1259 | sym.n_sect = self.data_section_index.? + 1; |
| 2122 | self.dyld_private_atom = atom; | 1260 | self.dyld_private_atom = atom; |
| 2123 | | 1261 | |
| 2124 | try self.allocateAtomCommon(atom, self.data_section_index.?); | | |
| 2125 | | | |
| 2126 | try self.managed_atoms.append(gpa, atom); | 1262 | try self.managed_atoms.append(gpa, atom); |
| 2127 | try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom); | 1263 | try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom); |
| | 1264 | |
| | 1265 | if (self.mode == .incremental) { |
| | 1266 | sym.n_value = try self.allocateAtom(atom, atom.size, @alignOf(u64)); |
| | 1267 | log.debug("allocated dyld_private atom at 0x{x}", .{sym.n_value}); |
| | 1268 | try self.writePtrWidthAtom(atom); |
| | 1269 | } else { |
| | 1270 | try self.addAtomToSection(atom); |
| | 1271 | } |
| 2128 | } | 1272 | } |
| 2129 | | 1273 | |
| 2130 | fn createStubHelperPreambleAtom(self: *MachO) !void { | 1274 | pub fn createStubHelperPreambleAtom(self: *MachO) !void { |
| 2131 | if (self.dyld_stub_binder_index == null) return; | 1275 | if (self.dyld_stub_binder_index == null) return; |
| 2132 | if (self.stub_helper_preamble_atom != null) return; | 1276 | if (self.stub_helper_preamble_atom != null) return; |
| 2133 | | 1277 | |
| 2134 | const gpa = self.base.allocator; | 1278 | const gpa = self.base.allocator; |
| 2135 | const arch = self.base.options.target.cpu.arch; | 1279 | const arch = self.base.options.target.cpu.arch; |
| 2136 | const size: u64 = switch (arch) { | 1280 | const size: u5 = switch (arch) { |
| 2137 | .x86_64 => 15, | 1281 | .x86_64 => 15, |
| 2138 | .aarch64 => 6 * @sizeOf(u32), | 1282 | .aarch64 => 6 * @sizeOf(u32), |
| 2139 | else => unreachable, | 1283 | else => unreachable, |
| ... | @@ -2144,117 +1288,200 @@ fn createStubHelperPreambleAtom(self: *MachO) !void { | ... | @@ -2144,117 +1288,200 @@ fn createStubHelperPreambleAtom(self: *MachO) !void { |
| 2144 | else => unreachable, | 1288 | else => unreachable, |
| 2145 | }; | 1289 | }; |
| 2146 | const sym_index = try self.allocateSymbol(); | 1290 | const sym_index = try self.allocateSymbol(); |
| 2147 | const atom = try MachO.createEmptyAtom(gpa, sym_index, size, alignment); | 1291 | const atom = switch (self.mode) { |
| | 1292 | .incremental => blk: { |
| | 1293 | const atom = try gpa.create(Atom); |
| | 1294 | atom.* = Atom.empty; |
| | 1295 | atom.sym_index = sym_index; |
| | 1296 | atom.size = size; |
| | 1297 | atom.alignment = switch (arch) { |
| | 1298 | .x86_64 => 1, |
| | 1299 | .aarch64 => @alignOf(u32), |
| | 1300 | else => unreachable, |
| | 1301 | }; |
| | 1302 | break :blk atom; |
| | 1303 | }, |
| | 1304 | .one_shot => try MachO.createEmptyAtom(gpa, sym_index, size, alignment), |
| | 1305 | }; |
| | 1306 | errdefer gpa.destroy(atom); |
| | 1307 | |
| 2148 | const sym = atom.getSymbolPtr(self); | 1308 | const sym = atom.getSymbolPtr(self); |
| 2149 | sym.n_type = macho.N_SECT; | 1309 | sym.n_type = macho.N_SECT; |
| | 1310 | sym.n_sect = self.stub_helper_section_index.? + 1; |
| 2150 | | 1311 | |
| 2151 | const dyld_private_sym_index = self.dyld_private_atom.?.sym_index; | 1312 | const dyld_private_sym_index = self.dyld_private_atom.?.sym_index; |
| | 1313 | |
| | 1314 | const code = try gpa.alloc(u8, size); |
| | 1315 | defer gpa.free(code); |
| | 1316 | mem.set(u8, code, 0); |
| | 1317 | |
| 2152 | switch (arch) { | 1318 | switch (arch) { |
| 2153 | .x86_64 => { | 1319 | .x86_64 => { |
| 2154 | try atom.relocs.ensureUnusedCapacity(self.base.allocator, 2); | | |
| 2155 | // lea %r11, [rip + disp] | 1320 | // lea %r11, [rip + disp] |
| 2156 | atom.code.items[0] = 0x4c; | 1321 | code[0] = 0x4c; |
| 2157 | atom.code.items[1] = 0x8d; | 1322 | code[1] = 0x8d; |
| 2158 | atom.code.items[2] = 0x1d; | 1323 | code[2] = 0x1d; |
| 2159 | atom.relocs.appendAssumeCapacity(.{ | | |
| 2160 | .offset = 3, | | |
| 2161 | .target = .{ .sym_index = dyld_private_sym_index, .file = null }, | | |
| 2162 | .addend = 0, | | |
| 2163 | .subtractor = null, | | |
| 2164 | .pcrel = true, | | |
| 2165 | .length = 2, | | |
| 2166 | .@"type" = @enumToInt(macho.reloc_type_x86_64.X86_64_RELOC_SIGNED), | | |
| 2167 | }); | | |
| 2168 | // push %r11 | 1324 | // push %r11 |
| 2169 | atom.code.items[7] = 0x41; | 1325 | code[7] = 0x41; |
| 2170 | atom.code.items[8] = 0x53; | 1326 | code[8] = 0x53; |
| 2171 | // jmp [rip + disp] | 1327 | // jmp [rip + disp] |
| 2172 | atom.code.items[9] = 0xff; | 1328 | code[9] = 0xff; |
| 2173 | atom.code.items[10] = 0x25; | 1329 | code[10] = 0x25; |
| 2174 | atom.relocs.appendAssumeCapacity(.{ | 1330 | |
| 2175 | .offset = 11, | 1331 | if (self.mode == .incremental) { |
| 2176 | .target = .{ .sym_index = self.dyld_stub_binder_index.?, .file = null }, | 1332 | try atom.addRelocations(self, 2, .{ .{ |
| 2177 | .addend = 0, | 1333 | .@"type" = @enumToInt(macho.reloc_type_x86_64.X86_64_RELOC_SIGNED), |
| 2178 | .subtractor = null, | 1334 | .target = .{ .sym_index = dyld_private_sym_index, .file = null }, |
| 2179 | .pcrel = true, | 1335 | .offset = 3, |
| 2180 | .length = 2, | 1336 | .addend = 0, |
| 2181 | .@"type" = @enumToInt(macho.reloc_type_x86_64.X86_64_RELOC_GOT), | 1337 | .pcrel = true, |
| 2182 | }); | 1338 | .length = 2, |
| | 1339 | }, .{ |
| | 1340 | .@"type" = @enumToInt(macho.reloc_type_x86_64.X86_64_RELOC_GOT), |
| | 1341 | .target = .{ .sym_index = self.dyld_stub_binder_index.?, .file = null }, |
| | 1342 | .offset = 11, |
| | 1343 | .addend = 0, |
| | 1344 | .pcrel = true, |
| | 1345 | .length = 2, |
| | 1346 | } }); |
| | 1347 | } else { |
| | 1348 | try atom.relocs.ensureUnusedCapacity(self.base.allocator, 2); |
| | 1349 | atom.relocs.appendAssumeCapacity(.{ |
| | 1350 | .offset = 3, |
| | 1351 | .target = .{ .sym_index = dyld_private_sym_index, .file = null }, |
| | 1352 | .addend = 0, |
| | 1353 | .subtractor = null, |
| | 1354 | .pcrel = true, |
| | 1355 | .length = 2, |
| | 1356 | .@"type" = @enumToInt(macho.reloc_type_x86_64.X86_64_RELOC_SIGNED), |
| | 1357 | }); |
| | 1358 | atom.relocs.appendAssumeCapacity(.{ |
| | 1359 | .offset = 11, |
| | 1360 | .target = .{ .sym_index = self.dyld_stub_binder_index.?, .file = null }, |
| | 1361 | .addend = 0, |
| | 1362 | .subtractor = null, |
| | 1363 | .pcrel = true, |
| | 1364 | .length = 2, |
| | 1365 | .@"type" = @enumToInt(macho.reloc_type_x86_64.X86_64_RELOC_GOT), |
| | 1366 | }); |
| | 1367 | } |
| 2183 | }, | 1368 | }, |
| | 1369 | |
| 2184 | .aarch64 => { | 1370 | .aarch64 => { |
| 2185 | try atom.relocs.ensureUnusedCapacity(self.base.allocator, 4); | | |
| 2186 | // adrp x17, 0 | 1371 | // adrp x17, 0 |
| 2187 | mem.writeIntLittle(u32, atom.code.items[0..][0..4], aarch64.Instruction.adrp(.x17, 0).toU32()); | 1372 | mem.writeIntLittle(u32, code[0..][0..4], aarch64.Instruction.adrp(.x17, 0).toU32()); |
| 2188 | atom.relocs.appendAssumeCapacity(.{ | | |
| 2189 | .offset = 0, | | |
| 2190 | .target = .{ .sym_index = dyld_private_sym_index, .file = null }, | | |
| 2191 | .addend = 0, | | |
| 2192 | .subtractor = null, | | |
| 2193 | .pcrel = true, | | |
| 2194 | .length = 2, | | |
| 2195 | .@"type" = @enumToInt(macho.reloc_type_arm64.ARM64_RELOC_PAGE21), | | |
| 2196 | }); | | |
| 2197 | // add x17, x17, 0 | 1373 | // add x17, x17, 0 |
| 2198 | mem.writeIntLittle(u32, atom.code.items[4..][0..4], aarch64.Instruction.add(.x17, .x17, 0, false).toU32()); | 1374 | mem.writeIntLittle(u32, code[4..][0..4], aarch64.Instruction.add(.x17, .x17, 0, false).toU32()); |
| 2199 | atom.relocs.appendAssumeCapacity(.{ | | |
| 2200 | .offset = 4, | | |
| 2201 | .target = .{ .sym_index = dyld_private_sym_index, .file = null }, | | |
| 2202 | .addend = 0, | | |
| 2203 | .subtractor = null, | | |
| 2204 | .pcrel = false, | | |
| 2205 | .length = 2, | | |
| 2206 | .@"type" = @enumToInt(macho.reloc_type_arm64.ARM64_RELOC_PAGEOFF12), | | |
| 2207 | }); | | |
| 2208 | // stp x16, x17, [sp, #-16]! | 1375 | // stp x16, x17, [sp, #-16]! |
| 2209 | mem.writeIntLittle(u32, atom.code.items[8..][0..4], aarch64.Instruction.stp( | 1376 | mem.writeIntLittle(u32, code[8..][0..4], aarch64.Instruction.stp( |
| 2210 | .x16, | 1377 | .x16, |
| 2211 | .x17, | 1378 | .x17, |
| 2212 | aarch64.Register.sp, | 1379 | aarch64.Register.sp, |
| 2213 | aarch64.Instruction.LoadStorePairOffset.pre_index(-16), | 1380 | aarch64.Instruction.LoadStorePairOffset.pre_index(-16), |
| 2214 | ).toU32()); | 1381 | ).toU32()); |
| 2215 | // adrp x16, 0 | 1382 | // adrp x16, 0 |
| 2216 | mem.writeIntLittle(u32, atom.code.items[12..][0..4], aarch64.Instruction.adrp(.x16, 0).toU32()); | 1383 | mem.writeIntLittle(u32, code[12..][0..4], aarch64.Instruction.adrp(.x16, 0).toU32()); |
| 2217 | atom.relocs.appendAssumeCapacity(.{ | | |
| 2218 | .offset = 12, | | |
| 2219 | .target = .{ .sym_index = self.dyld_stub_binder_index.?, .file = null }, | | |
| 2220 | .addend = 0, | | |
| 2221 | .subtractor = null, | | |
| 2222 | .pcrel = true, | | |
| 2223 | .length = 2, | | |
| 2224 | .@"type" = @enumToInt(macho.reloc_type_arm64.ARM64_RELOC_GOT_LOAD_PAGE21), | | |
| 2225 | }); | | |
| 2226 | // ldr x16, [x16, 0] | 1384 | // ldr x16, [x16, 0] |
| 2227 | mem.writeIntLittle(u32, atom.code.items[16..][0..4], aarch64.Instruction.ldr( | 1385 | mem.writeIntLittle(u32, code[16..][0..4], aarch64.Instruction.ldr( |
| 2228 | .x16, | 1386 | .x16, |
| 2229 | .x16, | 1387 | .x16, |
| 2230 | aarch64.Instruction.LoadStoreOffset.imm(0), | 1388 | aarch64.Instruction.LoadStoreOffset.imm(0), |
| 2231 | ).toU32()); | 1389 | ).toU32()); |
| 2232 | atom.relocs.appendAssumeCapacity(.{ | | |
| 2233 | .offset = 16, | | |
| 2234 | .target = .{ .sym_index = self.dyld_stub_binder_index.?, .file = null }, | | |
| 2235 | .addend = 0, | | |
| 2236 | .subtractor = null, | | |
| 2237 | .pcrel = false, | | |
| 2238 | .length = 2, | | |
| 2239 | .@"type" = @enumToInt(macho.reloc_type_arm64.ARM64_RELOC_GOT_LOAD_PAGEOFF12), | | |
| 2240 | }); | | |
| 2241 | // br x16 | 1390 | // br x16 |
| 2242 | mem.writeIntLittle(u32, atom.code.items[20..][0..4], aarch64.Instruction.br(.x16).toU32()); | 1391 | mem.writeIntLittle(u32, code[20..][0..4], aarch64.Instruction.br(.x16).toU32()); |
| | 1392 | |
| | 1393 | if (self.mode == .incremental) { |
| | 1394 | try atom.addRelocations(self, 4, .{ .{ |
| | 1395 | .@"type" = @enumToInt(macho.reloc_type_arm64.ARM64_RELOC_PAGE21), |
| | 1396 | .target = .{ .sym_index = dyld_private_sym_index, .file = null }, |
| | 1397 | .offset = 0, |
| | 1398 | .addend = 0, |
| | 1399 | .pcrel = true, |
| | 1400 | .length = 2, |
| | 1401 | }, .{ |
| | 1402 | .@"type" = @enumToInt(macho.reloc_type_arm64.ARM64_RELOC_PAGEOFF12), |
| | 1403 | .target = .{ .sym_index = dyld_private_sym_index, .file = null }, |
| | 1404 | .offset = 4, |
| | 1405 | .addend = 0, |
| | 1406 | .pcrel = false, |
| | 1407 | .length = 2, |
| | 1408 | }, .{ |
| | 1409 | .@"type" = @enumToInt(macho.reloc_type_arm64.ARM64_RELOC_GOT_LOAD_PAGE21), |
| | 1410 | .target = .{ .sym_index = self.dyld_stub_binder_index.?, .file = null }, |
| | 1411 | .offset = 12, |
| | 1412 | .addend = 0, |
| | 1413 | .pcrel = true, |
| | 1414 | .length = 2, |
| | 1415 | }, .{ |
| | 1416 | .@"type" = @enumToInt(macho.reloc_type_arm64.ARM64_RELOC_GOT_LOAD_PAGEOFF12), |
| | 1417 | .target = .{ .sym_index = self.dyld_stub_binder_index.?, .file = null }, |
| | 1418 | .offset = 16, |
| | 1419 | .addend = 0, |
| | 1420 | .pcrel = false, |
| | 1421 | .length = 2, |
| | 1422 | } }); |
| | 1423 | } else { |
| | 1424 | try atom.relocs.ensureUnusedCapacity(gpa, 4); |
| | 1425 | atom.relocs.appendAssumeCapacity(.{ |
| | 1426 | .offset = 0, |
| | 1427 | .target = .{ .sym_index = dyld_private_sym_index, .file = null }, |
| | 1428 | .addend = 0, |
| | 1429 | .subtractor = null, |
| | 1430 | .pcrel = true, |
| | 1431 | .length = 2, |
| | 1432 | .@"type" = @enumToInt(macho.reloc_type_arm64.ARM64_RELOC_PAGE21), |
| | 1433 | }); |
| | 1434 | atom.relocs.appendAssumeCapacity(.{ |
| | 1435 | .offset = 4, |
| | 1436 | .target = .{ .sym_index = dyld_private_sym_index, .file = null }, |
| | 1437 | .addend = 0, |
| | 1438 | .subtractor = null, |
| | 1439 | .pcrel = false, |
| | 1440 | .length = 2, |
| | 1441 | .@"type" = @enumToInt(macho.reloc_type_arm64.ARM64_RELOC_PAGEOFF12), |
| | 1442 | }); |
| | 1443 | atom.relocs.appendAssumeCapacity(.{ |
| | 1444 | .offset = 12, |
| | 1445 | .target = .{ .sym_index = self.dyld_stub_binder_index.?, .file = null }, |
| | 1446 | .addend = 0, |
| | 1447 | .subtractor = null, |
| | 1448 | .pcrel = true, |
| | 1449 | .length = 2, |
| | 1450 | .@"type" = @enumToInt(macho.reloc_type_arm64.ARM64_RELOC_GOT_LOAD_PAGE21), |
| | 1451 | }); |
| | 1452 | atom.relocs.appendAssumeCapacity(.{ |
| | 1453 | .offset = 16, |
| | 1454 | .target = .{ .sym_index = self.dyld_stub_binder_index.?, .file = null }, |
| | 1455 | .addend = 0, |
| | 1456 | .subtractor = null, |
| | 1457 | .pcrel = false, |
| | 1458 | .length = 2, |
| | 1459 | .@"type" = @enumToInt(macho.reloc_type_arm64.ARM64_RELOC_GOT_LOAD_PAGEOFF12), |
| | 1460 | }); |
| | 1461 | } |
| 2243 | }, | 1462 | }, |
| | 1463 | |
| 2244 | else => unreachable, | 1464 | else => unreachable, |
| 2245 | } | 1465 | } |
| 2246 | self.stub_helper_preamble_atom = atom; | 1466 | self.stub_helper_preamble_atom = atom; |
| 2247 | | 1467 | |
| 2248 | try self.allocateAtomCommon(atom, self.stub_helper_section_index.?); | | |
| 2249 | | | |
| 2250 | try self.managed_atoms.append(gpa, atom); | 1468 | try self.managed_atoms.append(gpa, atom); |
| 2251 | try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom); | 1469 | try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom); |
| | 1470 | |
| | 1471 | if (self.mode == .incremental) { |
| | 1472 | sym.n_value = try self.allocateAtom(atom, size, atom.alignment); |
| | 1473 | log.debug("allocated stub preamble atom at 0x{x}", .{sym.n_value}); |
| | 1474 | try self.writeAtom(atom, code); |
| | 1475 | } else { |
| | 1476 | mem.copy(u8, atom.code.items, code); |
| | 1477 | try self.addAtomToSection(atom); |
| | 1478 | } |
| 2252 | } | 1479 | } |
| 2253 | | 1480 | |
| 2254 | pub fn createStubHelperAtom(self: *MachO) !*Atom { | 1481 | pub fn createStubHelperAtom(self: *MachO) !*Atom { |
| 2255 | const gpa = self.base.allocator; | 1482 | const gpa = self.base.allocator; |
| 2256 | const arch = self.base.options.target.cpu.arch; | 1483 | const arch = self.base.options.target.cpu.arch; |
| 2257 | const stub_size: u4 = switch (arch) { | 1484 | const size: u4 = switch (arch) { |
| 2258 | .x86_64 => 10, | 1485 | .x86_64 => 10, |
| 2259 | .aarch64 => 3 * @sizeOf(u32), | 1486 | .aarch64 => 3 * @sizeOf(u32), |
| 2260 | else => unreachable, | 1487 | else => unreachable, |
| ... | @@ -2265,51 +1492,96 @@ pub fn createStubHelperAtom(self: *MachO) !*Atom { | ... | @@ -2265,51 +1492,96 @@ pub fn createStubHelperAtom(self: *MachO) !*Atom { |
| 2265 | else => unreachable, | 1492 | else => unreachable, |
| 2266 | }; | 1493 | }; |
| 2267 | const sym_index = try self.allocateSymbol(); | 1494 | const sym_index = try self.allocateSymbol(); |
| 2268 | const atom = try MachO.createEmptyAtom(gpa, sym_index, stub_size, alignment); | 1495 | const atom = switch (self.mode) { |
| | 1496 | .incremental => blk: { |
| | 1497 | const atom = try gpa.create(Atom); |
| | 1498 | atom.* = Atom.empty; |
| | 1499 | atom.sym_index = sym_index; |
| | 1500 | atom.size = size; |
| | 1501 | atom.alignment = switch (arch) { |
| | 1502 | .x86_64 => 1, |
| | 1503 | .aarch64 => @alignOf(u32), |
| | 1504 | else => unreachable, |
| | 1505 | }; |
| | 1506 | break :blk atom; |
| | 1507 | }, |
| | 1508 | .one_shot => try MachO.createEmptyAtom(gpa, sym_index, size, alignment), |
| | 1509 | }; |
| | 1510 | errdefer gpa.destroy(atom); |
| | 1511 | |
| 2269 | const sym = atom.getSymbolPtr(self); | 1512 | const sym = atom.getSymbolPtr(self); |
| 2270 | sym.n_type = macho.N_SECT; | 1513 | sym.n_type = macho.N_SECT; |
| | 1514 | sym.n_sect = self.stub_helper_section_index.? + 1; |
| 2271 | | 1515 | |
| 2272 | try atom.relocs.ensureTotalCapacity(gpa, 1); | 1516 | const code = try gpa.alloc(u8, size); |
| | 1517 | defer gpa.free(code); |
| | 1518 | mem.set(u8, code, 0); |
| 2273 | | 1519 | |
| 2274 | switch (arch) { | 1520 | switch (arch) { |
| 2275 | .x86_64 => { | 1521 | .x86_64 => { |
| 2276 | // pushq | 1522 | // pushq |
| 2277 | atom.code.items[0] = 0x68; | 1523 | code[0] = 0x68; |
| 2278 | // Next 4 bytes 1..4 are just a placeholder populated in `populateLazyBindOffsetsInStubHelper`. | 1524 | // Next 4 bytes 1..4 are just a placeholder populated in `populateLazyBindOffsetsInStubHelper`. |
| 2279 | // jmpq | 1525 | // jmpq |
| 2280 | atom.code.items[5] = 0xe9; | 1526 | code[5] = 0xe9; |
| 2281 | atom.relocs.appendAssumeCapacity(.{ | 1527 | |
| 2282 | .offset = 6, | 1528 | if (self.mode == .incremental) { |
| 2283 | .target = .{ .sym_index = self.stub_helper_preamble_atom.?.sym_index, .file = null }, | 1529 | try atom.addRelocation(self, .{ |
| 2284 | .addend = 0, | 1530 | .@"type" = @enumToInt(macho.reloc_type_x86_64.X86_64_RELOC_BRANCH), |
| 2285 | .subtractor = null, | 1531 | .target = .{ .sym_index = self.stub_helper_preamble_atom.?.sym_index, .file = null }, |
| 2286 | .pcrel = true, | 1532 | .offset = 6, |
| 2287 | .length = 2, | 1533 | .addend = 0, |
| 2288 | .@"type" = @enumToInt(macho.reloc_type_x86_64.X86_64_RELOC_BRANCH), | 1534 | .pcrel = true, |
| 2289 | }); | 1535 | .length = 2, |
| | 1536 | }); |
| | 1537 | } else { |
| | 1538 | try atom.relocs.ensureTotalCapacity(gpa, 1); |
| | 1539 | atom.relocs.appendAssumeCapacity(.{ |
| | 1540 | .offset = 6, |
| | 1541 | .target = .{ .sym_index = self.stub_helper_preamble_atom.?.sym_index, .file = null }, |
| | 1542 | .addend = 0, |
| | 1543 | .subtractor = null, |
| | 1544 | .pcrel = true, |
| | 1545 | .length = 2, |
| | 1546 | .@"type" = @enumToInt(macho.reloc_type_x86_64.X86_64_RELOC_BRANCH), |
| | 1547 | }); |
| | 1548 | } |
| 2290 | }, | 1549 | }, |
| 2291 | .aarch64 => { | 1550 | .aarch64 => { |
| 2292 | const literal = blk: { | 1551 | const literal = blk: { |
| 2293 | const div_res = try math.divExact(u64, stub_size - @sizeOf(u32), 4); | 1552 | const div_res = try math.divExact(u64, size - @sizeOf(u32), 4); |
| 2294 | break :blk math.cast(u18, div_res) orelse return error.Overflow; | 1553 | break :blk math.cast(u18, div_res) orelse return error.Overflow; |
| 2295 | }; | 1554 | }; |
| 2296 | // ldr w16, literal | 1555 | // ldr w16, literal |
| 2297 | mem.writeIntLittle(u32, atom.code.items[0..4], aarch64.Instruction.ldrLiteral( | 1556 | mem.writeIntLittle(u32, code[0..4], aarch64.Instruction.ldrLiteral( |
| 2298 | .w16, | 1557 | .w16, |
| 2299 | literal, | 1558 | literal, |
| 2300 | ).toU32()); | 1559 | ).toU32()); |
| 2301 | // b disp | 1560 | // b disp |
| 2302 | mem.writeIntLittle(u32, atom.code.items[4..8], aarch64.Instruction.b(0).toU32()); | 1561 | mem.writeIntLittle(u32, code[4..8], aarch64.Instruction.b(0).toU32()); |
| 2303 | atom.relocs.appendAssumeCapacity(.{ | | |
| 2304 | .offset = 4, | | |
| 2305 | .target = .{ .sym_index = self.stub_helper_preamble_atom.?.sym_index, .file = null }, | | |
| 2306 | .addend = 0, | | |
| 2307 | .subtractor = null, | | |
| 2308 | .pcrel = true, | | |
| 2309 | .length = 2, | | |
| 2310 | .@"type" = @enumToInt(macho.reloc_type_arm64.ARM64_RELOC_BRANCH26), | | |
| 2311 | }); | | |
| 2312 | // Next 4 bytes 8..12 are just a placeholder populated in `populateLazyBindOffsetsInStubHelper`. | 1562 | // Next 4 bytes 8..12 are just a placeholder populated in `populateLazyBindOffsetsInStubHelper`. |
| | 1563 | |
| | 1564 | if (self.mode == .incremental) { |
| | 1565 | try atom.addRelocation(self, .{ |
| | 1566 | .@"type" = @enumToInt(macho.reloc_type_arm64.ARM64_RELOC_BRANCH26), |
| | 1567 | .target = .{ .sym_index = self.stub_helper_preamble_atom.?.sym_index, .file = null }, |
| | 1568 | .offset = 4, |
| | 1569 | .addend = 0, |
| | 1570 | .pcrel = true, |
| | 1571 | .length = 2, |
| | 1572 | }); |
| | 1573 | } else { |
| | 1574 | try atom.relocs.ensureTotalCapacity(gpa, 1); |
| | 1575 | atom.relocs.appendAssumeCapacity(.{ |
| | 1576 | .offset = 4, |
| | 1577 | .target = .{ .sym_index = self.stub_helper_preamble_atom.?.sym_index, .file = null }, |
| | 1578 | .addend = 0, |
| | 1579 | .subtractor = null, |
| | 1580 | .pcrel = true, |
| | 1581 | .length = 2, |
| | 1582 | .@"type" = @enumToInt(macho.reloc_type_arm64.ARM64_RELOC_BRANCH26), |
| | 1583 | }); |
| | 1584 | } |
| 2313 | }, | 1585 | }, |
| 2314 | else => unreachable, | 1586 | else => unreachable, |
| 2315 | } | 1587 | } |
| ... | @@ -2317,7 +1589,14 @@ pub fn createStubHelperAtom(self: *MachO) !*Atom { | ... | @@ -2317,7 +1589,14 @@ pub fn createStubHelperAtom(self: *MachO) !*Atom { |
| 2317 | try self.managed_atoms.append(gpa, atom); | 1589 | try self.managed_atoms.append(gpa, atom); |
| 2318 | try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom); | 1590 | try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom); |
| 2319 | | 1591 | |
| 2320 | try self.allocateAtomCommon(atom, self.stub_helper_section_index.?); | 1592 | if (self.mode == .incremental) { |
| | 1593 | sym.n_value = try self.allocateAtom(atom, size, atom.alignment); |
| | 1594 | log.debug("allocated stub helper atom at 0x{x}", .{sym.n_value}); |
| | 1595 | try self.writeAtom(atom, code); |
| | 1596 | } else { |
| | 1597 | mem.copy(u8, atom.code.items, code); |
| | 1598 | try self.addAtomToSection(atom); |
| | 1599 | } |
| 2321 | | 1600 | |
| 2322 | return atom; | 1601 | return atom; |
| 2323 | } | 1602 | } |
| ... | @@ -2325,35 +1604,73 @@ pub fn createStubHelperAtom(self: *MachO) !*Atom { | ... | @@ -2325,35 +1604,73 @@ pub fn createStubHelperAtom(self: *MachO) !*Atom { |
| 2325 | pub fn createLazyPointerAtom(self: *MachO, stub_sym_index: u32, target: SymbolWithLoc) !*Atom { | 1604 | pub fn createLazyPointerAtom(self: *MachO, stub_sym_index: u32, target: SymbolWithLoc) !*Atom { |
| 2326 | const gpa = self.base.allocator; | 1605 | const gpa = self.base.allocator; |
| 2327 | const sym_index = try self.allocateSymbol(); | 1606 | const sym_index = try self.allocateSymbol(); |
| 2328 | const atom = try MachO.createEmptyAtom(gpa, sym_index, @sizeOf(u64), 3); | 1607 | const atom = switch (self.mode) { |
| | 1608 | .incremental => blk: { |
| | 1609 | const atom = try gpa.create(Atom); |
| | 1610 | atom.* = Atom.empty; |
| | 1611 | atom.sym_index = sym_index; |
| | 1612 | atom.size = @sizeOf(u64); |
| | 1613 | atom.alignment = @alignOf(u64); |
| | 1614 | break :blk atom; |
| | 1615 | }, |
| | 1616 | .one_shot => try MachO.createEmptyAtom(gpa, sym_index, @sizeOf(u64), 3), |
| | 1617 | }; |
| | 1618 | errdefer gpa.destroy(atom); |
| | 1619 | |
| 2329 | const sym = atom.getSymbolPtr(self); | 1620 | const sym = atom.getSymbolPtr(self); |
| 2330 | sym.n_type = macho.N_SECT; | 1621 | sym.n_type = macho.N_SECT; |
| | 1622 | sym.n_sect = self.la_symbol_ptr_section_index.? + 1; |
| 2331 | | 1623 | |
| 2332 | try atom.relocs.append(gpa, .{ | 1624 | if (self.mode == .incremental) { |
| 2333 | .offset = 0, | 1625 | try atom.addRelocation(self, .{ |
| 2334 | .target = .{ .sym_index = stub_sym_index, .file = null }, | 1626 | .@"type" = switch (self.base.options.target.cpu.arch) { |
| 2335 | .addend = 0, | 1627 | .aarch64 => @enumToInt(macho.reloc_type_arm64.ARM64_RELOC_UNSIGNED), |
| 2336 | .subtractor = null, | 1628 | .x86_64 => @enumToInt(macho.reloc_type_x86_64.X86_64_RELOC_UNSIGNED), |
| 2337 | .pcrel = false, | 1629 | else => unreachable, |
| 2338 | .length = 3, | 1630 | }, |
| 2339 | .@"type" = switch (self.base.options.target.cpu.arch) { | 1631 | .target = .{ .sym_index = stub_sym_index, .file = null }, |
| 2340 | .aarch64 => @enumToInt(macho.reloc_type_arm64.ARM64_RELOC_UNSIGNED), | 1632 | .offset = 0, |
| 2341 | .x86_64 => @enumToInt(macho.reloc_type_x86_64.X86_64_RELOC_UNSIGNED), | 1633 | .addend = 0, |
| 2342 | else => unreachable, | 1634 | .pcrel = false, |
| 2343 | }, | 1635 | .length = 3, |
| 2344 | }); | 1636 | }); |
| 2345 | try atom.rebases.append(gpa, 0); | 1637 | try atom.addRebase(self, 0); |
| 2346 | | 1638 | try atom.addLazyBinding(self, .{ |
| 2347 | const global = self.getGlobal(self.getSymbolName(target)).?; | 1639 | .target = self.getGlobal(self.getSymbolName(target)).?, |
| 2348 | try atom.lazy_bindings.append(gpa, .{ | 1640 | .offset = 0, |
| 2349 | .target = global, | 1641 | }); |
| 2350 | .offset = 0, | 1642 | } else { |
| 2351 | }); | 1643 | try atom.relocs.append(gpa, .{ |
| | 1644 | .offset = 0, |
| | 1645 | .target = .{ .sym_index = stub_sym_index, .file = null }, |
| | 1646 | .addend = 0, |
| | 1647 | .subtractor = null, |
| | 1648 | .pcrel = false, |
| | 1649 | .length = 3, |
| | 1650 | .@"type" = switch (self.base.options.target.cpu.arch) { |
| | 1651 | .aarch64 => @enumToInt(macho.reloc_type_arm64.ARM64_RELOC_UNSIGNED), |
| | 1652 | .x86_64 => @enumToInt(macho.reloc_type_x86_64.X86_64_RELOC_UNSIGNED), |
| | 1653 | else => unreachable, |
| | 1654 | }, |
| | 1655 | }); |
| | 1656 | try atom.rebases.append(gpa, 0); |
| | 1657 | const global = self.getGlobal(self.getSymbolName(target)).?; |
| | 1658 | try atom.lazy_bindings.append(gpa, .{ |
| | 1659 | .target = global, |
| | 1660 | .offset = 0, |
| | 1661 | }); |
| | 1662 | } |
| 2352 | | 1663 | |
| 2353 | try self.managed_atoms.append(gpa, atom); | 1664 | try self.managed_atoms.append(gpa, atom); |
| 2354 | try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom); | 1665 | try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom); |
| 2355 | | 1666 | |
| 2356 | try self.allocateAtomCommon(atom, self.la_symbol_ptr_section_index.?); | 1667 | if (self.mode == .incremental) { |
| | 1668 | sym.n_value = try self.allocateAtom(atom, atom.size, @alignOf(u64)); |
| | 1669 | log.debug("allocated lazy pointer atom at 0x{x}", .{sym.n_value}); |
| | 1670 | try self.writePtrWidthAtom(atom); |
| | 1671 | } else { |
| | 1672 | try self.addAtomToSection(atom); |
| | 1673 | } |
| 2357 | | 1674 | |
| 2358 | return atom; | 1675 | return atom; |
| 2359 | } | 1676 | } |
| ... | @@ -2366,61 +1683,117 @@ pub fn createStubAtom(self: *MachO, laptr_sym_index: u32) !*Atom { | ... | @@ -2366,61 +1683,117 @@ pub fn createStubAtom(self: *MachO, laptr_sym_index: u32) !*Atom { |
| 2366 | .aarch64 => 2, | 1683 | .aarch64 => 2, |
| 2367 | else => unreachable, // unhandled architecture type | 1684 | else => unreachable, // unhandled architecture type |
| 2368 | }; | 1685 | }; |
| 2369 | const stub_size: u4 = switch (arch) { | 1686 | const size: u4 = switch (arch) { |
| 2370 | .x86_64 => 6, | 1687 | .x86_64 => 6, |
| 2371 | .aarch64 => 3 * @sizeOf(u32), | 1688 | .aarch64 => 3 * @sizeOf(u32), |
| 2372 | else => unreachable, // unhandled architecture type | 1689 | else => unreachable, // unhandled architecture type |
| 2373 | }; | 1690 | }; |
| 2374 | const sym_index = try self.allocateSymbol(); | 1691 | const sym_index = try self.allocateSymbol(); |
| 2375 | const atom = try MachO.createEmptyAtom(gpa, sym_index, stub_size, alignment); | 1692 | const atom = switch (self.mode) { |
| | 1693 | .incremental => blk: { |
| | 1694 | const atom = try gpa.create(Atom); |
| | 1695 | atom.* = Atom.empty; |
| | 1696 | atom.sym_index = sym_index; |
| | 1697 | atom.size = size; |
| | 1698 | atom.alignment = switch (arch) { |
| | 1699 | .x86_64 => 1, |
| | 1700 | .aarch64 => @alignOf(u32), |
| | 1701 | else => unreachable, // unhandled architecture type |
| | 1702 | |
| | 1703 | }; |
| | 1704 | break :blk atom; |
| | 1705 | }, |
| | 1706 | .one_shot => try MachO.createEmptyAtom(gpa, sym_index, size, alignment), |
| | 1707 | }; |
| | 1708 | errdefer gpa.destroy(atom); |
| | 1709 | |
| 2376 | const sym = atom.getSymbolPtr(self); | 1710 | const sym = atom.getSymbolPtr(self); |
| 2377 | sym.n_type = macho.N_SECT; | 1711 | sym.n_type = macho.N_SECT; |
| | 1712 | sym.n_sect = self.stubs_section_index.? + 1; |
| | 1713 | |
| | 1714 | const code = try gpa.alloc(u8, size); |
| | 1715 | defer gpa.free(code); |
| | 1716 | mem.set(u8, code, 0); |
| 2378 | | 1717 | |
| 2379 | switch (arch) { | 1718 | switch (arch) { |
| 2380 | .x86_64 => { | 1719 | .x86_64 => { |
| 2381 | // jmp | 1720 | // jmp |
| 2382 | atom.code.items[0] = 0xff; | 1721 | code[0] = 0xff; |
| 2383 | atom.code.items[1] = 0x25; | 1722 | code[1] = 0x25; |
| 2384 | try atom.relocs.append(gpa, .{ | 1723 | |
| 2385 | .offset = 2, | 1724 | if (self.mode == .incremental) { |
| 2386 | .target = .{ .sym_index = laptr_sym_index, .file = null }, | 1725 | try atom.addRelocation(self, .{ |
| 2387 | .addend = 0, | 1726 | .@"type" = @enumToInt(macho.reloc_type_x86_64.X86_64_RELOC_BRANCH), |
| 2388 | .subtractor = null, | 1727 | .target = .{ .sym_index = laptr_sym_index, .file = null }, |
| 2389 | .pcrel = true, | 1728 | .offset = 2, |
| 2390 | .length = 2, | 1729 | .addend = 0, |
| 2391 | .@"type" = @enumToInt(macho.reloc_type_x86_64.X86_64_RELOC_BRANCH), | 1730 | .pcrel = true, |
| 2392 | }); | 1731 | .length = 2, |
| | 1732 | }); |
| | 1733 | } else { |
| | 1734 | try atom.relocs.append(gpa, .{ |
| | 1735 | .offset = 2, |
| | 1736 | .target = .{ .sym_index = laptr_sym_index, .file = null }, |
| | 1737 | .addend = 0, |
| | 1738 | .subtractor = null, |
| | 1739 | .pcrel = true, |
| | 1740 | .length = 2, |
| | 1741 | .@"type" = @enumToInt(macho.reloc_type_x86_64.X86_64_RELOC_BRANCH), |
| | 1742 | }); |
| | 1743 | } |
| 2393 | }, | 1744 | }, |
| 2394 | .aarch64 => { | 1745 | .aarch64 => { |
| 2395 | try atom.relocs.ensureTotalCapacity(gpa, 2); | | |
| 2396 | // adrp x16, pages | 1746 | // adrp x16, pages |
| 2397 | mem.writeIntLittle(u32, atom.code.items[0..4], aarch64.Instruction.adrp(.x16, 0).toU32()); | 1747 | mem.writeIntLittle(u32, code[0..4], aarch64.Instruction.adrp(.x16, 0).toU32()); |
| 2398 | atom.relocs.appendAssumeCapacity(.{ | | |
| 2399 | .offset = 0, | | |
| 2400 | .target = .{ .sym_index = laptr_sym_index, .file = null }, | | |
| 2401 | .addend = 0, | | |
| 2402 | .subtractor = null, | | |
| 2403 | .pcrel = true, | | |
| 2404 | .length = 2, | | |
| 2405 | .@"type" = @enumToInt(macho.reloc_type_arm64.ARM64_RELOC_PAGE21), | | |
| 2406 | }); | | |
| 2407 | // ldr x16, x16, offset | 1748 | // ldr x16, x16, offset |
| 2408 | mem.writeIntLittle(u32, atom.code.items[4..8], aarch64.Instruction.ldr( | 1749 | mem.writeIntLittle(u32, code[4..8], aarch64.Instruction.ldr( |
| 2409 | .x16, | 1750 | .x16, |
| 2410 | .x16, | 1751 | .x16, |
| 2411 | aarch64.Instruction.LoadStoreOffset.imm(0), | 1752 | aarch64.Instruction.LoadStoreOffset.imm(0), |
| 2412 | ).toU32()); | 1753 | ).toU32()); |
| 2413 | atom.relocs.appendAssumeCapacity(.{ | | |
| 2414 | .offset = 4, | | |
| 2415 | .target = .{ .sym_index = laptr_sym_index, .file = null }, | | |
| 2416 | .addend = 0, | | |
| 2417 | .subtractor = null, | | |
| 2418 | .pcrel = false, | | |
| 2419 | .length = 2, | | |
| 2420 | .@"type" = @enumToInt(macho.reloc_type_arm64.ARM64_RELOC_PAGEOFF12), | | |
| 2421 | }); | | |
| 2422 | // br x16 | 1754 | // br x16 |
| 2423 | mem.writeIntLittle(u32, atom.code.items[8..12], aarch64.Instruction.br(.x16).toU32()); | 1755 | mem.writeIntLittle(u32, code[8..12], aarch64.Instruction.br(.x16).toU32()); |
| | 1756 | |
| | 1757 | if (self.mode == .incremental) { |
| | 1758 | try atom.addRelocations(self, 2, .{ |
| | 1759 | .{ |
| | 1760 | .@"type" = @enumToInt(macho.reloc_type_arm64.ARM64_RELOC_PAGE21), |
| | 1761 | .target = .{ .sym_index = laptr_sym_index, .file = null }, |
| | 1762 | .offset = 0, |
| | 1763 | .addend = 0, |
| | 1764 | .pcrel = true, |
| | 1765 | .length = 2, |
| | 1766 | }, |
| | 1767 | .{ |
| | 1768 | .@"type" = @enumToInt(macho.reloc_type_arm64.ARM64_RELOC_PAGEOFF12), |
| | 1769 | .target = .{ .sym_index = laptr_sym_index, .file = null }, |
| | 1770 | .offset = 4, |
| | 1771 | .addend = 0, |
| | 1772 | .pcrel = false, |
| | 1773 | .length = 2, |
| | 1774 | }, |
| | 1775 | }); |
| | 1776 | } else { |
| | 1777 | try atom.relocs.ensureTotalCapacity(gpa, 2); |
| | 1778 | atom.relocs.appendAssumeCapacity(.{ |
| | 1779 | .offset = 0, |
| | 1780 | .target = .{ .sym_index = laptr_sym_index, .file = null }, |
| | 1781 | .addend = 0, |
| | 1782 | .subtractor = null, |
| | 1783 | .pcrel = true, |
| | 1784 | .length = 2, |
| | 1785 | .@"type" = @enumToInt(macho.reloc_type_arm64.ARM64_RELOC_PAGE21), |
| | 1786 | }); |
| | 1787 | atom.relocs.appendAssumeCapacity(.{ |
| | 1788 | .offset = 4, |
| | 1789 | .target = .{ .sym_index = laptr_sym_index, .file = null }, |
| | 1790 | .addend = 0, |
| | 1791 | .subtractor = null, |
| | 1792 | .pcrel = false, |
| | 1793 | .length = 2, |
| | 1794 | .@"type" = @enumToInt(macho.reloc_type_arm64.ARM64_RELOC_PAGEOFF12), |
| | 1795 | }); |
| | 1796 | } |
| 2424 | }, | 1797 | }, |
| 2425 | else => unreachable, | 1798 | else => unreachable, |
| 2426 | } | 1799 | } |
| ... | @@ -2428,12 +1801,53 @@ pub fn createStubAtom(self: *MachO, laptr_sym_index: u32) !*Atom { | ... | @@ -2428,12 +1801,53 @@ pub fn createStubAtom(self: *MachO, laptr_sym_index: u32) !*Atom { |
| 2428 | try self.managed_atoms.append(gpa, atom); | 1801 | try self.managed_atoms.append(gpa, atom); |
| 2429 | try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom); | 1802 | try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom); |
| 2430 | | 1803 | |
| 2431 | try self.allocateAtomCommon(atom, self.stubs_section_index.?); | 1804 | if (self.mode == .incremental) { |
| | 1805 | sym.n_value = try self.allocateAtom(atom, size, atom.alignment); |
| | 1806 | log.debug("allocated stub atom at 0x{x}", .{sym.n_value}); |
| | 1807 | try self.writeAtom(atom, code); |
| | 1808 | } else { |
| | 1809 | mem.copy(u8, atom.code.items, code); |
| | 1810 | try self.addAtomToSection(atom); |
| | 1811 | } |
| | 1812 | |
| | 1813 | return atom; |
| | 1814 | } |
| | 1815 | |
| | 1816 | pub fn createTlvPtrAtom(self: *MachO, target: SymbolWithLoc) !*Atom { |
| | 1817 | assert(self.mode == .one_shot); |
| | 1818 | |
| | 1819 | const gpa = self.base.allocator; |
| | 1820 | const sym_index = try self.allocateSymbol(); |
| | 1821 | const atom = try MachO.createEmptyAtom(gpa, sym_index, @sizeOf(u64), 3); |
| | 1822 | |
| | 1823 | const target_sym = self.getSymbol(target); |
| | 1824 | assert(target_sym.undf()); |
| | 1825 | |
| | 1826 | const global = self.getGlobal(self.getSymbolName(target)).?; |
| | 1827 | try atom.bindings.append(gpa, .{ |
| | 1828 | .target = global, |
| | 1829 | .offset = 0, |
| | 1830 | }); |
| | 1831 | |
| | 1832 | try self.managed_atoms.append(gpa, atom); |
| | 1833 | try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom); |
| | 1834 | |
| | 1835 | const sym = atom.getSymbolPtr(self); |
| | 1836 | sym.n_type = macho.N_SECT; |
| | 1837 | const sect_id = (try self.getOutputSection(.{ |
| | 1838 | .segname = makeStaticString("__DATA"), |
| | 1839 | .sectname = makeStaticString("__thread_ptrs"), |
| | 1840 | .flags = macho.S_THREAD_LOCAL_VARIABLE_POINTERS, |
| | 1841 | })).?; |
| | 1842 | sym.n_sect = sect_id + 1; |
| | 1843 | |
| | 1844 | try self.addAtomToSection(atom); |
| 2432 | | 1845 | |
| 2433 | return atom; | 1846 | return atom; |
| 2434 | } | 1847 | } |
| 2435 | | 1848 | |
| 2436 | fn createTentativeDefAtoms(self: *MachO) !void { | 1849 | pub fn createTentativeDefAtoms(self: *MachO) !void { |
| | 1850 | assert(self.mode == .one_shot); |
| 2437 | const gpa = self.base.allocator; | 1851 | const gpa = self.base.allocator; |
| 2438 | | 1852 | |
| 2439 | for (self.globals.items) |global| { | 1853 | for (self.globals.items) |global| { |
| ... | @@ -2448,16 +1862,15 @@ fn createTentativeDefAtoms(self: *MachO) !void { | ... | @@ -2448,16 +1862,15 @@ fn createTentativeDefAtoms(self: *MachO) !void { |
| 2448 | // text blocks for each tentative definition. | 1862 | // text blocks for each tentative definition. |
| 2449 | const size = sym.n_value; | 1863 | const size = sym.n_value; |
| 2450 | const alignment = (sym.n_desc >> 8) & 0x0f; | 1864 | const alignment = (sym.n_desc >> 8) & 0x0f; |
| 2451 | const n_sect = (try self.getOutputSection(.{ | 1865 | const sect_id = (try self.getOutputSection(.{ |
| 2452 | .segname = makeStaticString("__DATA"), | 1866 | .segname = makeStaticString("__DATA"), |
| 2453 | .sectname = makeStaticString("__bss"), | 1867 | .sectname = makeStaticString("__bss"), |
| 2454 | .flags = macho.S_ZEROFILL, | 1868 | .flags = macho.S_ZEROFILL, |
| 2455 | })).?; | 1869 | })).?; |
| 2456 | | | |
| 2457 | sym.* = .{ | 1870 | sym.* = .{ |
| 2458 | .n_strx = sym.n_strx, | 1871 | .n_strx = sym.n_strx, |
| 2459 | .n_type = macho.N_SECT | macho.N_EXT, | 1872 | .n_type = macho.N_SECT | macho.N_EXT, |
| 2460 | .n_sect = n_sect, | 1873 | .n_sect = sect_id + 1, |
| 2461 | .n_desc = 0, | 1874 | .n_desc = 0, |
| 2462 | .n_value = 0, | 1875 | .n_value = 0, |
| 2463 | }; | 1876 | }; |
| ... | @@ -2465,7 +1878,7 @@ fn createTentativeDefAtoms(self: *MachO) !void { | ... | @@ -2465,7 +1878,7 @@ fn createTentativeDefAtoms(self: *MachO) !void { |
| 2465 | const atom = try MachO.createEmptyAtom(gpa, global.sym_index, size, alignment); | 1878 | const atom = try MachO.createEmptyAtom(gpa, global.sym_index, size, alignment); |
| 2466 | atom.file = global.file; | 1879 | atom.file = global.file; |
| 2467 | | 1880 | |
| 2468 | try self.allocateAtomCommon(atom, n_sect); | 1881 | try self.addAtomToSection(atom); |
| 2469 | | 1882 | |
| 2470 | if (global.file) |file| { | 1883 | if (global.file) |file| { |
| 2471 | const object = &self.objects.items[file]; | 1884 | const object = &self.objects.items[file]; |
| ... | @@ -2478,7 +1891,7 @@ fn createTentativeDefAtoms(self: *MachO) !void { | ... | @@ -2478,7 +1891,7 @@ fn createTentativeDefAtoms(self: *MachO) !void { |
| 2478 | } | 1891 | } |
| 2479 | } | 1892 | } |
| 2480 | | 1893 | |
| 2481 | fn createMhExecuteHeaderSymbol(self: *MachO) !void { | 1894 | pub fn createMhExecuteHeaderSymbol(self: *MachO) !void { |
| 2482 | if (self.base.options.output_mode != .Exe) return; | 1895 | if (self.base.options.output_mode != .Exe) return; |
| 2483 | if (self.getGlobal("__mh_execute_header")) |global| { | 1896 | if (self.getGlobal("__mh_execute_header")) |global| { |
| 2484 | const sym = self.getSymbol(global); | 1897 | const sym = self.getSymbol(global); |
| ... | @@ -2501,7 +1914,7 @@ fn createMhExecuteHeaderSymbol(self: *MachO) !void { | ... | @@ -2501,7 +1914,7 @@ fn createMhExecuteHeaderSymbol(self: *MachO) !void { |
| 2501 | gop.value_ptr.* = sym_loc; | 1914 | gop.value_ptr.* = sym_loc; |
| 2502 | } | 1915 | } |
| 2503 | | 1916 | |
| 2504 | fn createDsoHandleSymbol(self: *MachO) !void { | 1917 | pub fn createDsoHandleSymbol(self: *MachO) !void { |
| 2505 | const global = self.getGlobalPtr("___dso_handle") orelse return; | 1918 | const global = self.getGlobalPtr("___dso_handle") orelse return; |
| 2506 | if (!self.getSymbol(global.*).undf()) return; | 1919 | if (!self.getSymbol(global.*).undf()) return; |
| 2507 | | 1920 | |
| ... | @@ -2576,7 +1989,7 @@ fn resolveGlobalSymbol(self: *MachO, current: SymbolWithLoc) !void { | ... | @@ -2576,7 +1989,7 @@ fn resolveGlobalSymbol(self: *MachO, current: SymbolWithLoc) !void { |
| 2576 | gop.value_ptr.* = current; | 1989 | gop.value_ptr.* = current; |
| 2577 | } | 1990 | } |
| 2578 | | 1991 | |
| 2579 | fn resolveSymbolsInObject(self: *MachO, object_id: u16) !void { | 1992 | pub fn resolveSymbolsInObject(self: *MachO, object_id: u16) !void { |
| 2580 | const object = &self.objects.items[object_id]; | 1993 | const object = &self.objects.items[object_id]; |
| 2581 | log.debug("resolving symbols in '{s}'", .{object.name}); | 1994 | log.debug("resolving symbols in '{s}'", .{object.name}); |
| 2582 | | 1995 | |
| ... | @@ -2629,7 +2042,7 @@ fn resolveSymbolsInObject(self: *MachO, object_id: u16) !void { | ... | @@ -2629,7 +2042,7 @@ fn resolveSymbolsInObject(self: *MachO, object_id: u16) !void { |
| 2629 | } | 2042 | } |
| 2630 | } | 2043 | } |
| 2631 | | 2044 | |
| 2632 | fn resolveSymbolsInArchives(self: *MachO) !void { | 2045 | pub fn resolveSymbolsInArchives(self: *MachO) !void { |
| 2633 | if (self.archives.items.len == 0) return; | 2046 | if (self.archives.items.len == 0) return; |
| 2634 | | 2047 | |
| 2635 | const gpa = self.base.allocator; | 2048 | const gpa = self.base.allocator; |
| ... | @@ -2660,7 +2073,7 @@ fn resolveSymbolsInArchives(self: *MachO) !void { | ... | @@ -2660,7 +2073,7 @@ fn resolveSymbolsInArchives(self: *MachO) !void { |
| 2660 | } | 2073 | } |
| 2661 | } | 2074 | } |
| 2662 | | 2075 | |
| 2663 | fn resolveSymbolsInDylibs(self: *MachO) !void { | 2076 | pub fn resolveSymbolsInDylibs(self: *MachO) !void { |
| 2664 | if (self.dylibs.items.len == 0) return; | 2077 | if (self.dylibs.items.len == 0) return; |
| 2665 | | 2078 | |
| 2666 | const gpa = self.base.allocator; | 2079 | const gpa = self.base.allocator; |
| ... | @@ -2697,6 +2110,7 @@ fn resolveSymbolsInDylibs(self: *MachO) !void { | ... | @@ -2697,6 +2110,7 @@ fn resolveSymbolsInDylibs(self: *MachO) !void { |
| 2697 | const laptr_atom = try self.createLazyPointerAtom(stub_helper_atom.sym_index, global); | 2110 | const laptr_atom = try self.createLazyPointerAtom(stub_helper_atom.sym_index, global); |
| 2698 | const stub_atom = try self.createStubAtom(laptr_atom.sym_index); | 2111 | const stub_atom = try self.createStubAtom(laptr_atom.sym_index); |
| 2699 | self.stubs.items[stub_index].sym_index = stub_atom.sym_index; | 2112 | self.stubs.items[stub_index].sym_index = stub_atom.sym_index; |
| | 2113 | self.markRelocsDirtyByTarget(global); |
| 2700 | } | 2114 | } |
| 2701 | | 2115 | |
| 2702 | continue :loop; | 2116 | continue :loop; |
| ... | @@ -2706,7 +2120,7 @@ fn resolveSymbolsInDylibs(self: *MachO) !void { | ... | @@ -2706,7 +2120,7 @@ fn resolveSymbolsInDylibs(self: *MachO) !void { |
| 2706 | } | 2120 | } |
| 2707 | } | 2121 | } |
| 2708 | | 2122 | |
| 2709 | fn resolveSymbolsAtLoading(self: *MachO) !void { | 2123 | pub fn resolveSymbolsAtLoading(self: *MachO) !void { |
| 2710 | const is_lib = self.base.options.output_mode == .Lib; | 2124 | const is_lib = self.base.options.output_mode == .Lib; |
| 2711 | const is_dyn_lib = self.base.options.link_mode == .Dynamic and is_lib; | 2125 | const is_dyn_lib = self.base.options.link_mode == .Dynamic and is_lib; |
| 2712 | const allow_undef = is_dyn_lib and (self.base.options.allow_shlib_undefined orelse false); | 2126 | const allow_undef = is_dyn_lib and (self.base.options.allow_shlib_undefined orelse false); |
| ... | @@ -2749,7 +2163,7 @@ fn resolveSymbolsAtLoading(self: *MachO) !void { | ... | @@ -2749,7 +2163,7 @@ fn resolveSymbolsAtLoading(self: *MachO) !void { |
| 2749 | } | 2163 | } |
| 2750 | } | 2164 | } |
| 2751 | | 2165 | |
| 2752 | fn resolveDyldStubBinder(self: *MachO) !void { | 2166 | pub fn resolveDyldStubBinder(self: *MachO) !void { |
| 2753 | if (self.dyld_stub_binder_index != null) return; | 2167 | if (self.dyld_stub_binder_index != null) return; |
| 2754 | if (self.unresolved.count() == 0) return; // no need for a stub binder if we don't have any imports | 2168 | if (self.unresolved.count() == 0) return; // no need for a stub binder if we don't have any imports |
| 2755 | | 2169 | |
| ... | @@ -2794,9 +2208,13 @@ fn resolveDyldStubBinder(self: *MachO) !void { | ... | @@ -2794,9 +2208,13 @@ fn resolveDyldStubBinder(self: *MachO) !void { |
| 2794 | const got_index = try self.allocateGotEntry(global); | 2208 | const got_index = try self.allocateGotEntry(global); |
| 2795 | const got_atom = try self.createGotAtom(global); | 2209 | const got_atom = try self.createGotAtom(global); |
| 2796 | self.got_entries.items[got_index].sym_index = got_atom.sym_index; | 2210 | self.got_entries.items[got_index].sym_index = got_atom.sym_index; |
| | 2211 | |
| | 2212 | if (self.mode == .incremental) { |
| | 2213 | try self.writePtrWidthAtom(got_atom); |
| | 2214 | } |
| 2797 | } | 2215 | } |
| 2798 | | 2216 | |
| 2799 | fn writeDylinkerLC(ncmds: *u32, lc_writer: anytype) !void { | 2217 | pub fn writeDylinkerLC(ncmds: *u32, lc_writer: anytype) !void { |
| 2800 | const name_len = mem.sliceTo(default_dyld_path, 0).len; | 2218 | const name_len = mem.sliceTo(default_dyld_path, 0).len; |
| 2801 | const cmdsize = @intCast(u32, mem.alignForwardGeneric( | 2219 | const cmdsize = @intCast(u32, mem.alignForwardGeneric( |
| 2802 | u64, | 2220 | u64, |
| ... | @@ -2816,9 +2234,13 @@ fn writeDylinkerLC(ncmds: *u32, lc_writer: anytype) !void { | ... | @@ -2816,9 +2234,13 @@ fn writeDylinkerLC(ncmds: *u32, lc_writer: anytype) !void { |
| 2816 | ncmds.* += 1; | 2234 | ncmds.* += 1; |
| 2817 | } | 2235 | } |
| 2818 | | 2236 | |
| 2819 | fn writeMainLC(self: *MachO, ncmds: *u32, lc_writer: anytype) !void { | 2237 | pub fn writeMainLC(self: *MachO, ncmds: *u32, lc_writer: anytype) !void { |
| 2820 | if (self.base.options.output_mode != .Exe) return; | 2238 | if (self.base.options.output_mode != .Exe) return; |
| 2821 | const seg = self.segments.items[self.text_segment_cmd_index.?]; | 2239 | const seg_id = switch (self.mode) { |
| | 2240 | .incremental => self.header_segment_cmd_index.?, |
| | 2241 | .one_shot => self.text_segment_cmd_index.?, |
| | 2242 | }; |
| | 2243 | const seg = self.segments.items[seg_id]; |
| 2822 | const global = try self.getEntryPoint(); | 2244 | const global = try self.getEntryPoint(); |
| 2823 | const sym = self.getSymbol(global); | 2245 | const sym = self.getSymbol(global); |
| 2824 | try lc_writer.writeStruct(macho.entry_point_command{ | 2246 | try lc_writer.writeStruct(macho.entry_point_command{ |
| ... | @@ -2838,7 +2260,7 @@ const WriteDylibLCCtx = struct { | ... | @@ -2838,7 +2260,7 @@ const WriteDylibLCCtx = struct { |
| 2838 | compatibility_version: u32 = 0x10000, | 2260 | compatibility_version: u32 = 0x10000, |
| 2839 | }; | 2261 | }; |
| 2840 | | 2262 | |
| 2841 | fn writeDylibLC(ctx: WriteDylibLCCtx, ncmds: *u32, lc_writer: anytype) !void { | 2263 | pub fn writeDylibLC(ctx: WriteDylibLCCtx, ncmds: *u32, lc_writer: anytype) !void { |
| 2842 | const name_len = ctx.name.len + 1; | 2264 | const name_len = ctx.name.len + 1; |
| 2843 | const cmdsize = @intCast(u32, mem.alignForwardGeneric( | 2265 | const cmdsize = @intCast(u32, mem.alignForwardGeneric( |
| 2844 | u64, | 2266 | u64, |
| ... | @@ -2864,7 +2286,7 @@ fn writeDylibLC(ctx: WriteDylibLCCtx, ncmds: *u32, lc_writer: anytype) !void { | ... | @@ -2864,7 +2286,7 @@ fn writeDylibLC(ctx: WriteDylibLCCtx, ncmds: *u32, lc_writer: anytype) !void { |
| 2864 | ncmds.* += 1; | 2286 | ncmds.* += 1; |
| 2865 | } | 2287 | } |
| 2866 | | 2288 | |
| 2867 | fn writeDylibIdLC(self: *MachO, ncmds: *u32, lc_writer: anytype) !void { | 2289 | pub fn writeDylibIdLC(self: *MachO, ncmds: *u32, lc_writer: anytype) !void { |
| 2868 | if (self.base.options.output_mode != .Lib) return; | 2290 | if (self.base.options.output_mode != .Lib) return; |
| 2869 | const install_name = self.base.options.install_name orelse self.base.options.emit.?.sub_path; | 2291 | const install_name = self.base.options.install_name orelse self.base.options.emit.?.sub_path; |
| 2870 | const curr = self.base.options.version orelse std.builtin.Version{ | 2292 | const curr = self.base.options.version orelse std.builtin.Version{ |
| ... | @@ -2910,7 +2332,7 @@ const RpathIterator = struct { | ... | @@ -2910,7 +2332,7 @@ const RpathIterator = struct { |
| 2910 | } | 2332 | } |
| 2911 | }; | 2333 | }; |
| 2912 | | 2334 | |
| 2913 | fn writeRpathLCs(self: *MachO, ncmds: *u32, lc_writer: anytype) !void { | 2335 | pub fn writeRpathLCs(self: *MachO, ncmds: *u32, lc_writer: anytype) !void { |
| 2914 | const gpa = self.base.allocator; | 2336 | const gpa = self.base.allocator; |
| 2915 | | 2337 | |
| 2916 | var it = RpathIterator.init(gpa, self.base.options.rpath_list); | 2338 | var it = RpathIterator.init(gpa, self.base.options.rpath_list); |
| ... | @@ -2937,7 +2359,7 @@ fn writeRpathLCs(self: *MachO, ncmds: *u32, lc_writer: anytype) !void { | ... | @@ -2937,7 +2359,7 @@ fn writeRpathLCs(self: *MachO, ncmds: *u32, lc_writer: anytype) !void { |
| 2937 | } | 2359 | } |
| 2938 | } | 2360 | } |
| 2939 | | 2361 | |
| 2940 | fn writeBuildVersionLC(self: *MachO, ncmds: *u32, lc_writer: anytype) !void { | 2362 | pub fn writeBuildVersionLC(self: *MachO, ncmds: *u32, lc_writer: anytype) !void { |
| 2941 | const cmdsize = @sizeOf(macho.build_version_command) + @sizeOf(macho.build_tool_version); | 2363 | const cmdsize = @sizeOf(macho.build_version_command) + @sizeOf(macho.build_tool_version); |
| 2942 | const platform_version = blk: { | 2364 | const platform_version = blk: { |
| 2943 | const ver = self.base.options.target.os.version_range.semver.min; | 2365 | const ver = self.base.options.target.os.version_range.semver.min; |
| ... | @@ -2970,7 +2392,7 @@ fn writeBuildVersionLC(self: *MachO, ncmds: *u32, lc_writer: anytype) !void { | ... | @@ -2970,7 +2392,7 @@ fn writeBuildVersionLC(self: *MachO, ncmds: *u32, lc_writer: anytype) !void { |
| 2970 | ncmds.* += 1; | 2392 | ncmds.* += 1; |
| 2971 | } | 2393 | } |
| 2972 | | 2394 | |
| 2973 | fn writeLoadDylibLCs(self: *MachO, ncmds: *u32, lc_writer: anytype) !void { | 2395 | pub fn writeLoadDylibLCs(self: *MachO, ncmds: *u32, lc_writer: anytype) !void { |
| 2974 | for (self.referenced_dylibs.keys()) |id| { | 2396 | for (self.referenced_dylibs.keys()) |id| { |
| 2975 | const dylib = self.dylibs.items[id]; | 2397 | const dylib = self.dylibs.items[id]; |
| 2976 | const dylib_id = dylib.id orelse unreachable; | 2398 | const dylib_id = dylib.id orelse unreachable; |
| ... | @@ -3069,14 +2491,49 @@ pub fn deinit(self: *MachO) void { | ... | @@ -3069,14 +2491,49 @@ pub fn deinit(self: *MachO) void { |
| 3069 | } | 2491 | } |
| 3070 | | 2492 | |
| 3071 | self.atom_by_index_table.deinit(gpa); | 2493 | self.atom_by_index_table.deinit(gpa); |
| | 2494 | |
| | 2495 | { |
| | 2496 | var it = self.relocs.valueIterator(); |
| | 2497 | while (it.next()) |relocs| { |
| | 2498 | relocs.deinit(gpa); |
| | 2499 | } |
| | 2500 | self.relocs.deinit(gpa); |
| | 2501 | } |
| | 2502 | |
| | 2503 | { |
| | 2504 | var it = self.rebases.valueIterator(); |
| | 2505 | while (it.next()) |rebases| { |
| | 2506 | rebases.deinit(gpa); |
| | 2507 | } |
| | 2508 | self.rebases.deinit(gpa); |
| | 2509 | } |
| | 2510 | |
| | 2511 | { |
| | 2512 | var it = self.bindings.valueIterator(); |
| | 2513 | while (it.next()) |bindings| { |
| | 2514 | bindings.deinit(gpa); |
| | 2515 | } |
| | 2516 | self.bindings.deinit(gpa); |
| | 2517 | } |
| | 2518 | |
| | 2519 | { |
| | 2520 | var it = self.lazy_bindings.valueIterator(); |
| | 2521 | while (it.next()) |bindings| { |
| | 2522 | bindings.deinit(gpa); |
| | 2523 | } |
| | 2524 | self.lazy_bindings.deinit(gpa); |
| | 2525 | } |
| 3072 | } | 2526 | } |
| 3073 | | 2527 | |
| 3074 | fn freeAtom(self: *MachO, atom: *Atom, sect_id: u8, owns_atom: bool) void { | 2528 | fn freeAtom(self: *MachO, atom: *Atom, owns_atom: bool) void { |
| 3075 | log.debug("freeAtom {*}", .{atom}); | 2529 | log.debug("freeAtom {*}", .{atom}); |
| 3076 | if (!owns_atom) { | 2530 | if (!owns_atom) { |
| 3077 | atom.deinit(self.base.allocator); | 2531 | atom.deinit(self.base.allocator); |
| 3078 | } | 2532 | } |
| | 2533 | // Remove any relocs and base relocs associated with this Atom |
| | 2534 | self.freeRelocationsForAtom(atom); |
| 3079 | | 2535 | |
| | 2536 | const sect_id = atom.getSymbol(self).n_sect - 1; |
| 3080 | const free_list = &self.sections.items(.free_list)[sect_id]; | 2537 | const free_list = &self.sections.items(.free_list)[sect_id]; |
| 3081 | var already_have_free_list_node = false; | 2538 | var already_have_free_list_node = false; |
| 3082 | { | 2539 | { |
| ... | @@ -3129,21 +2586,20 @@ fn freeAtom(self: *MachO, atom: *Atom, sect_id: u8, owns_atom: bool) void { | ... | @@ -3129,21 +2586,20 @@ fn freeAtom(self: *MachO, atom: *Atom, sect_id: u8, owns_atom: bool) void { |
| 3129 | } | 2586 | } |
| 3130 | } | 2587 | } |
| 3131 | | 2588 | |
| 3132 | fn shrinkAtom(self: *MachO, atom: *Atom, new_block_size: u64, sect_id: u8) void { | 2589 | fn shrinkAtom(self: *MachO, atom: *Atom, new_block_size: u64) void { |
| 3133 | _ = self; | 2590 | _ = self; |
| 3134 | _ = atom; | 2591 | _ = atom; |
| 3135 | _ = new_block_size; | 2592 | _ = new_block_size; |
| 3136 | _ = sect_id; | | |
| 3137 | // TODO check the new capacity, and if it crosses the size threshold into a big enough | 2593 | // TODO check the new capacity, and if it crosses the size threshold into a big enough |
| 3138 | // capacity, insert a free list node for it. | 2594 | // capacity, insert a free list node for it. |
| 3139 | } | 2595 | } |
| 3140 | | 2596 | |
| 3141 | fn growAtom(self: *MachO, atom: *Atom, new_atom_size: u64, alignment: u64, sect_id: u8) !u64 { | 2597 | fn growAtom(self: *MachO, atom: *Atom, new_atom_size: u64, alignment: u64) !u64 { |
| 3142 | const sym = atom.getSymbol(self); | 2598 | const sym = atom.getSymbol(self); |
| 3143 | const align_ok = mem.alignBackwardGeneric(u64, sym.n_value, alignment) == sym.n_value; | 2599 | const align_ok = mem.alignBackwardGeneric(u64, sym.n_value, alignment) == sym.n_value; |
| 3144 | const need_realloc = !align_ok or new_atom_size > atom.capacity(self); | 2600 | const need_realloc = !align_ok or new_atom_size > atom.capacity(self); |
| 3145 | if (!need_realloc) return sym.n_value; | 2601 | if (!need_realloc) return sym.n_value; |
| 3146 | return self.allocateAtom(atom, new_atom_size, alignment, sect_id); | 2602 | return self.allocateAtom(atom, new_atom_size, alignment); |
| 3147 | } | 2603 | } |
| 3148 | | 2604 | |
| 3149 | fn allocateSymbol(self: *MachO) !u32 { | 2605 | fn allocateSymbol(self: *MachO) !u32 { |
| ... | @@ -3282,13 +2738,7 @@ pub fn updateFunc(self: *MachO, module: *Module, func: *Module.Fn, air: Air, liv | ... | @@ -3282,13 +2738,7 @@ pub fn updateFunc(self: *MachO, module: *Module, func: *Module.Fn, air: Air, liv |
| 3282 | const decl_index = func.owner_decl; | 2738 | const decl_index = func.owner_decl; |
| 3283 | const decl = module.declPtr(decl_index); | 2739 | const decl = module.declPtr(decl_index); |
| 3284 | self.freeUnnamedConsts(decl_index); | 2740 | self.freeUnnamedConsts(decl_index); |
| 3285 | | 2741 | self.freeRelocationsForAtom(&decl.link.macho); |
| 3286 | // TODO clearing the code and relocs buffer should probably be orchestrated | | |
| 3287 | // in a different, smarter, more automatic way somewhere else, in a more centralised | | |
| 3288 | // way than this. | | |
| 3289 | // If we don't clear the buffers here, we are up for some nasty surprises when | | |
| 3290 | // this atom is reused later on and was not freed by freeAtom(). | | |
| 3291 | decl.link.macho.clearRetainingCapacity(); | | |
| 3292 | | 2742 | |
| 3293 | var code_buffer = std.ArrayList(u8).init(self.base.allocator); | 2743 | var code_buffer = std.ArrayList(u8).init(self.base.allocator); |
| 3294 | defer code_buffer.deinit(); | 2744 | defer code_buffer.deinit(); |
| ... | @@ -3306,18 +2756,16 @@ pub fn updateFunc(self: *MachO, module: *Module, func: *Module.Fn, air: Air, liv | ... | @@ -3306,18 +2756,16 @@ pub fn updateFunc(self: *MachO, module: *Module, func: *Module.Fn, air: Air, liv |
| 3306 | else | 2756 | else |
| 3307 | try codegen.generateFunction(&self.base, decl.srcLoc(), func, air, liveness, &code_buffer, .none); | 2757 | try codegen.generateFunction(&self.base, decl.srcLoc(), func, air, liveness, &code_buffer, .none); |
| 3308 | | 2758 | |
| 3309 | switch (res) { | 2759 | const code = switch (res) { |
| 3310 | .appended => { | 2760 | .appended => code_buffer.items, |
| 3311 | try decl.link.macho.code.appendSlice(self.base.allocator, code_buffer.items); | | |
| 3312 | }, | | |
| 3313 | .fail => |em| { | 2761 | .fail => |em| { |
| 3314 | decl.analysis = .codegen_failure; | 2762 | decl.analysis = .codegen_failure; |
| 3315 | try module.failed_decls.put(module.gpa, decl_index, em); | 2763 | try module.failed_decls.put(module.gpa, decl_index, em); |
| 3316 | return; | 2764 | return; |
| 3317 | }, | 2765 | }, |
| 3318 | } | 2766 | }; |
| 3319 | | 2767 | |
| 3320 | const addr = try self.placeDecl(decl_index, decl.link.macho.code.items.len); | 2768 | const addr = try self.updateDeclCode(decl_index, code); |
| 3321 | | 2769 | |
| 3322 | if (decl_state) |*ds| { | 2770 | if (decl_state) |*ds| { |
| 3323 | try self.d_sym.?.dwarf.commitDeclState( | 2771 | try self.d_sym.?.dwarf.commitDeclState( |
| ... | @@ -3363,20 +2811,17 @@ pub fn lowerUnnamedConst(self: *MachO, typed_value: TypedValue, decl_index: Modu | ... | @@ -3363,20 +2811,17 @@ pub fn lowerUnnamedConst(self: *MachO, typed_value: TypedValue, decl_index: Modu |
| 3363 | | 2811 | |
| 3364 | log.debug("allocating symbol indexes for {?s}", .{name}); | 2812 | log.debug("allocating symbol indexes for {?s}", .{name}); |
| 3365 | | 2813 | |
| 3366 | const required_alignment = typed_value.ty.abiAlignment(self.base.options.target); | 2814 | const atom = try gpa.create(Atom); |
| 3367 | const sym_index = try self.allocateSymbol(); | 2815 | errdefer gpa.destroy(atom); |
| 3368 | const atom = try MachO.createEmptyAtom( | 2816 | atom.* = Atom.empty; |
| 3369 | gpa, | 2817 | |
| 3370 | sym_index, | 2818 | atom.sym_index = try self.allocateSymbol(); |
| 3371 | @sizeOf(u64), | | |
| 3372 | math.log2(required_alignment), | | |
| 3373 | ); | | |
| 3374 | | 2819 | |
| 3375 | try self.managed_atoms.append(gpa, atom); | 2820 | try self.managed_atoms.append(gpa, atom); |
| 3376 | try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom); | 2821 | try self.atom_by_index_table.putNoClobber(gpa, atom.sym_index, atom); |
| 3377 | | 2822 | |
| 3378 | const res = try codegen.generateSymbol(&self.base, decl.srcLoc(), typed_value, &code_buffer, .none, .{ | 2823 | const res = try codegen.generateSymbol(&self.base, decl.srcLoc(), typed_value, &code_buffer, .none, .{ |
| 3379 | .parent_atom_index = sym_index, | 2824 | .parent_atom_index = atom.sym_index, |
| 3380 | }); | 2825 | }); |
| 3381 | const code = switch (res) { | 2826 | const code = switch (res) { |
| 3382 | .externally_managed => |x| x, | 2827 | .externally_managed => |x| x, |
| ... | @@ -3389,33 +2834,25 @@ pub fn lowerUnnamedConst(self: *MachO, typed_value: TypedValue, decl_index: Modu | ... | @@ -3389,33 +2834,25 @@ pub fn lowerUnnamedConst(self: *MachO, typed_value: TypedValue, decl_index: Modu |
| 3389 | }, | 2834 | }, |
| 3390 | }; | 2835 | }; |
| 3391 | | 2836 | |
| 3392 | atom.code.clearRetainingCapacity(); | 2837 | const required_alignment = typed_value.ty.abiAlignment(self.base.options.target); |
| 3393 | try atom.code.appendSlice(gpa, code); | 2838 | atom.size = code.len; |
| | 2839 | atom.alignment = required_alignment; |
| | 2840 | // TODO: work out logic for disambiguating functions from function pointers |
| | 2841 | // const sect_id = self.getDeclOutputSection(decl); |
| | 2842 | const sect_id = self.data_const_section_index.?; |
| | 2843 | const symbol = atom.getSymbolPtr(self); |
| | 2844 | symbol.n_strx = name_str_index; |
| | 2845 | symbol.n_type = macho.N_SECT; |
| | 2846 | symbol.n_sect = sect_id + 1; |
| | 2847 | symbol.n_value = try self.allocateAtom(atom, code.len, required_alignment); |
| | 2848 | errdefer self.freeAtom(atom, true); |
| 3394 | | 2849 | |
| 3395 | const sect_id = try self.getOutputSectionAtom( | 2850 | try unnamed_consts.append(gpa, atom); |
| 3396 | atom, | | |
| 3397 | decl_name, | | |
| 3398 | typed_value.ty, | | |
| 3399 | typed_value.val, | | |
| 3400 | required_alignment, | | |
| 3401 | ); | | |
| 3402 | const addr = try self.allocateAtom(atom, code.len, required_alignment, sect_id); | | |
| 3403 | | 2851 | |
| 3404 | log.debug("allocated atom for {?s} at 0x{x}", .{ name, addr }); | 2852 | log.debug("allocated atom for {?s} at 0x{x}", .{ name, symbol.n_value }); |
| 3405 | log.debug(" (required alignment 0x{x})", .{required_alignment}); | 2853 | log.debug(" (required alignment 0x{x})", .{required_alignment}); |
| 3406 | | 2854 | |
| 3407 | errdefer self.freeAtom(atom, sect_id, true); | 2855 | try self.writeAtom(atom, code); |
| 3408 | | | |
| 3409 | const symbol = atom.getSymbolPtr(self); | | |
| 3410 | symbol.* = .{ | | |
| 3411 | .n_strx = name_str_index, | | |
| 3412 | .n_type = macho.N_SECT, | | |
| 3413 | .n_sect = sect_id + 1, | | |
| 3414 | .n_desc = 0, | | |
| 3415 | .n_value = addr, | | |
| 3416 | }; | | |
| 3417 | | | |
| 3418 | try unnamed_consts.append(gpa, atom); | | |
| 3419 | | 2856 | |
| 3420 | return atom.sym_index; | 2857 | return atom.sym_index; |
| 3421 | } | 2858 | } |
| ... | @@ -3442,6 +2879,8 @@ pub fn updateDecl(self: *MachO, module: *Module, decl_index: Module.Decl.Index) | ... | @@ -3442,6 +2879,8 @@ pub fn updateDecl(self: *MachO, module: *Module, decl_index: Module.Decl.Index) |
| 3442 | } | 2879 | } |
| 3443 | } | 2880 | } |
| 3444 | | 2881 | |
| | 2882 | self.freeRelocationsForAtom(&decl.link.macho); |
| | 2883 | |
| 3445 | var code_buffer = std.ArrayList(u8).init(self.base.allocator); | 2884 | var code_buffer = std.ArrayList(u8).init(self.base.allocator); |
| 3446 | defer code_buffer.deinit(); | 2885 | defer code_buffer.deinit(); |
| 3447 | | 2886 | |
| ... | @@ -3469,27 +2908,16 @@ pub fn updateDecl(self: *MachO, module: *Module, decl_index: Module.Decl.Index) | ... | @@ -3469,27 +2908,16 @@ pub fn updateDecl(self: *MachO, module: *Module, decl_index: Module.Decl.Index) |
| 3469 | .parent_atom_index = decl.link.macho.sym_index, | 2908 | .parent_atom_index = decl.link.macho.sym_index, |
| 3470 | }); | 2909 | }); |
| 3471 | | 2910 | |
| 3472 | const code = blk: { | 2911 | const code = switch (res) { |
| 3473 | switch (res) { | 2912 | .externally_managed => |x| x, |
| 3474 | .externally_managed => |x| break :blk x, | 2913 | .appended => code_buffer.items, |
| 3475 | .appended => { | 2914 | .fail => |em| { |
| 3476 | // TODO clearing the code and relocs buffer should probably be orchestrated | 2915 | decl.analysis = .codegen_failure; |
| 3477 | // in a different, smarter, more automatic way somewhere else, in a more centralised | 2916 | try module.failed_decls.put(module.gpa, decl_index, em); |
| 3478 | // way than this. | 2917 | return; |
| 3479 | // If we don't clear the buffers here, we are up for some nasty surprises when | 2918 | }, |
| 3480 | // this atom is reused later on and was not freed by freeAtom(). | | |
| 3481 | decl.link.macho.code.clearAndFree(self.base.allocator); | | |
| 3482 | try decl.link.macho.code.appendSlice(self.base.allocator, code_buffer.items); | | |
| 3483 | break :blk decl.link.macho.code.items; | | |
| 3484 | }, | | |
| 3485 | .fail => |em| { | | |
| 3486 | decl.analysis = .codegen_failure; | | |
| 3487 | try module.failed_decls.put(module.gpa, decl_index, em); | | |
| 3488 | return; | | |
| 3489 | }, | | |
| 3490 | } | | |
| 3491 | }; | 2919 | }; |
| 3492 | const addr = try self.placeDecl(decl_index, code.len); | 2920 | const addr = try self.updateDeclCode(decl_index, code); |
| 3493 | | 2921 | |
| 3494 | if (decl_state) |*ds| { | 2922 | if (decl_state) |*ds| { |
| 3495 | try self.d_sym.?.dwarf.commitDeclState( | 2923 | try self.d_sym.?.dwarf.commitDeclState( |
| ... | @@ -3508,82 +2936,16 @@ pub fn updateDecl(self: *MachO, module: *Module, decl_index: Module.Decl.Index) | ... | @@ -3508,82 +2936,16 @@ pub fn updateDecl(self: *MachO, module: *Module, decl_index: Module.Decl.Index) |
| 3508 | try self.updateDeclExports(module, decl_index, decl_exports); | 2936 | try self.updateDeclExports(module, decl_index, decl_exports); |
| 3509 | } | 2937 | } |
| 3510 | | 2938 | |
| 3511 | /// Checks if the value, or any of its embedded values stores a pointer, and thus requires | 2939 | fn getDeclOutputSection(self: *MachO, decl: *Module.Decl) u8 { |
| 3512 | /// a rebase opcode for the dynamic linker. | 2940 | const ty = decl.ty; |
| 3513 | fn needsPointerRebase(ty: Type, val: Value, mod: *Module) bool { | 2941 | const val = decl.val; |
| 3514 | if (ty.zigTypeTag() == .Fn) { | | |
| 3515 | return false; | | |
| 3516 | } | | |
| 3517 | if (val.pointerDecl()) |_| { | | |
| 3518 | return true; | | |
| 3519 | } | | |
| 3520 | | | |
| 3521 | switch (ty.zigTypeTag()) { | | |
| 3522 | .Fn => unreachable, | | |
| 3523 | .Pointer => return true, | | |
| 3524 | .Array, .Vector => { | | |
| 3525 | if (ty.arrayLen() == 0) return false; | | |
| 3526 | const elem_ty = ty.childType(); | | |
| 3527 | var elem_value_buf: Value.ElemValueBuffer = undefined; | | |
| 3528 | const elem_val = val.elemValueBuffer(mod, 0, &elem_value_buf); | | |
| 3529 | return needsPointerRebase(elem_ty, elem_val, mod); | | |
| 3530 | }, | | |
| 3531 | .Struct => { | | |
| 3532 | const fields = ty.structFields().values(); | | |
| 3533 | if (fields.len == 0) return false; | | |
| 3534 | if (val.castTag(.aggregate)) |payload| { | | |
| 3535 | const field_values = payload.data; | | |
| 3536 | for (field_values) |field_val, i| { | | |
| 3537 | if (needsPointerRebase(fields[i].ty, field_val, mod)) return true; | | |
| 3538 | } else return false; | | |
| 3539 | } else return false; | | |
| 3540 | }, | | |
| 3541 | .Optional => { | | |
| 3542 | if (val.castTag(.opt_payload)) |payload| { | | |
| 3543 | const sub_val = payload.data; | | |
| 3544 | var buffer: Type.Payload.ElemType = undefined; | | |
| 3545 | const sub_ty = ty.optionalChild(&buffer); | | |
| 3546 | return needsPointerRebase(sub_ty, sub_val, mod); | | |
| 3547 | } else return false; | | |
| 3548 | }, | | |
| 3549 | .Union => { | | |
| 3550 | const union_obj = val.cast(Value.Payload.Union).?.data; | | |
| 3551 | const active_field_ty = ty.unionFieldType(union_obj.tag, mod); | | |
| 3552 | return needsPointerRebase(active_field_ty, union_obj.val, mod); | | |
| 3553 | }, | | |
| 3554 | .ErrorUnion => { | | |
| 3555 | if (val.castTag(.eu_payload)) |payload| { | | |
| 3556 | const payload_ty = ty.errorUnionPayload(); | | |
| 3557 | return needsPointerRebase(payload_ty, payload.data, mod); | | |
| 3558 | } else return false; | | |
| 3559 | }, | | |
| 3560 | else => return false, | | |
| 3561 | } | | |
| 3562 | } | | |
| 3563 | | | |
| 3564 | fn getOutputSectionAtom( | | |
| 3565 | self: *MachO, | | |
| 3566 | atom: *Atom, | | |
| 3567 | name: []const u8, | | |
| 3568 | ty: Type, | | |
| 3569 | val: Value, | | |
| 3570 | alignment: u32, | | |
| 3571 | ) !u8 { | | |
| 3572 | const code = atom.code.items; | | |
| 3573 | const mod = self.base.options.module.?; | | |
| 3574 | const align_log_2 = math.log2(alignment); | | |
| 3575 | const zig_ty = ty.zigTypeTag(); | 2942 | const zig_ty = ty.zigTypeTag(); |
| 3576 | const mode = self.base.options.optimize_mode; | 2943 | const mode = self.base.options.optimize_mode; |
| 3577 | const sect_id: u8 = blk: { | 2944 | const sect_id: u8 = blk: { |
| 3578 | // TODO finish and audit this function | 2945 | // TODO finish and audit this function |
| 3579 | if (val.isUndefDeep()) { | 2946 | if (val.isUndefDeep()) { |
| 3580 | if (mode == .ReleaseFast or mode == .ReleaseSmall) { | 2947 | if (mode == .ReleaseFast or mode == .ReleaseSmall) { |
| 3581 | break :blk (try self.getOutputSection(.{ | 2948 | @panic("TODO __DATA,__bss"); |
| 3582 | .segname = makeStaticString("__DATA"), | | |
| 3583 | .sectname = makeStaticString("__bss"), | | |
| 3584 | .size = code.len, | | |
| 3585 | .@"align" = align_log_2, | | |
| 3586 | })).?; | | |
| 3587 | } else { | 2949 | } else { |
| 3588 | break :blk self.data_section_index.?; | 2950 | break :blk self.data_section_index.?; |
| 3589 | } | 2951 | } |
| ... | @@ -3593,129 +2955,207 @@ fn getOutputSectionAtom( | ... | @@ -3593,129 +2955,207 @@ fn getOutputSectionAtom( |
| 3593 | break :blk self.data_section_index.?; | 2955 | break :blk self.data_section_index.?; |
| 3594 | } | 2956 | } |
| 3595 | | 2957 | |
| 3596 | if (needsPointerRebase(ty, val, mod)) { | | |
| 3597 | break :blk (try self.getOutputSection(.{ | | |
| 3598 | .segname = makeStaticString("__DATA_CONST"), | | |
| 3599 | .sectname = makeStaticString("__const"), | | |
| 3600 | .size = code.len, | | |
| 3601 | .@"align" = align_log_2, | | |
| 3602 | })).?; | | |
| 3603 | } | | |
| 3604 | | | |
| 3605 | switch (zig_ty) { | 2958 | switch (zig_ty) { |
| 3606 | .Fn => { | 2959 | // TODO: what if this is a function pointer? |
| 3607 | break :blk self.text_section_index.?; | 2960 | .Fn => break :blk self.text_section_index.?, |
| 3608 | }, | 2961 | else => { |
| 3609 | .Array => { | 2962 | if (val.castTag(.variable)) |_| { |
| 3610 | if (val.tag() == .bytes) { | 2963 | break :blk self.data_section_index.?; |
| 3611 | switch (ty.tag()) { | | |
| 3612 | .array_u8_sentinel_0, | | |
| 3613 | .const_slice_u8_sentinel_0, | | |
| 3614 | .manyptr_const_u8_sentinel_0, | | |
| 3615 | => { | | |
| 3616 | break :blk (try self.getOutputSection(.{ | | |
| 3617 | .segname = makeStaticString("__TEXT"), | | |
| 3618 | .sectname = makeStaticString("__cstring"), | | |
| 3619 | .flags = macho.S_CSTRING_LITERALS, | | |
| 3620 | .size = code.len, | | |
| 3621 | .@"align" = align_log_2, | | |
| 3622 | })).?; | | |
| 3623 | }, | | |
| 3624 | else => {}, | | |
| 3625 | } | | |
| 3626 | } | 2964 | } |
| | 2965 | break :blk self.data_const_section_index.?; |
| 3627 | }, | 2966 | }, |
| 3628 | else => {}, | | |
| 3629 | } | 2967 | } |
| 3630 | break :blk (try self.getOutputSection(.{ | | |
| 3631 | .segname = makeStaticString("__TEXT"), | | |
| 3632 | .sectname = makeStaticString("__const"), | | |
| 3633 | .size = code.len, | | |
| 3634 | .@"align" = align_log_2, | | |
| 3635 | })).?; | | |
| 3636 | }; | 2968 | }; |
| 3637 | const header = self.sections.items(.header)[sect_id]; | | |
| 3638 | log.debug(" allocating atom '{s}' in '{s},{s}', ord({d})", .{ | | |
| 3639 | name, | | |
| 3640 | header.segName(), | | |
| 3641 | header.sectName(), | | |
| 3642 | sect_id, | | |
| 3643 | }); | | |
| 3644 | return sect_id; | 2969 | return sect_id; |
| 3645 | } | 2970 | } |
| 3646 | | 2971 | |
| 3647 | fn placeDecl(self: *MachO, decl_index: Module.Decl.Index, code_len: usize) !u64 { | 2972 | pub fn getOutputSection(self: *MachO, sect: macho.section_64) !?u8 { |
| 3648 | const module = self.base.options.module.?; | 2973 | const segname = sect.segName(); |
| 3649 | const decl = module.declPtr(decl_index); | 2974 | const sectname = sect.sectName(); |
| 3650 | const required_alignment = decl.getAlignment(self.base.options.target); | 2975 | const sect_id: ?u8 = blk: { |
| 3651 | assert(decl.link.macho.sym_index != 0); // Caller forgot to call allocateDeclIndexes() | 2976 | if (mem.eql(u8, "__LLVM", segname)) { |
| 3652 | | 2977 | log.debug("TODO LLVM section: type 0x{x}, name '{s},{s}'", .{ |
| 3653 | const sym_name = try decl.getFullyQualifiedName(module); | 2978 | sect.flags, segname, sectname, |
| 3654 | defer self.base.allocator.free(sym_name); | 2979 | }); |
| 3655 | | 2980 | break :blk null; |
| 3656 | const decl_ptr = self.decls.getPtr(decl_index).?; | | |
| 3657 | if (decl_ptr.* == null) { | | |
| 3658 | decl_ptr.* = try self.getOutputSectionAtom( | | |
| 3659 | &decl.link.macho, | | |
| 3660 | sym_name, | | |
| 3661 | decl.ty, | | |
| 3662 | decl.val, | | |
| 3663 | required_alignment, | | |
| 3664 | ); | | |
| 3665 | } | | |
| 3666 | const match = decl_ptr.*.?; | | |
| 3667 | | | |
| 3668 | if (decl.link.macho.size != 0) { | | |
| 3669 | const symbol = decl.link.macho.getSymbolPtr(self); | | |
| 3670 | const capacity = decl.link.macho.capacity(self); | | |
| 3671 | const need_realloc = code_len > capacity or !mem.isAlignedGeneric(u64, symbol.n_value, required_alignment); | | |
| 3672 | | | |
| 3673 | if (need_realloc) { | | |
| 3674 | const vaddr = try self.growAtom(&decl.link.macho, code_len, required_alignment, match); | | |
| 3675 | log.debug("growing {s} and moving from 0x{x} to 0x{x}", .{ sym_name, symbol.n_value, vaddr }); | | |
| 3676 | log.debug(" (required alignment 0x{x})", .{required_alignment}); | | |
| 3677 | symbol.n_value = vaddr; | | |
| 3678 | | | |
| 3679 | const got_atom = self.getGotAtomForSymbol(.{ | | |
| 3680 | .sym_index = decl.link.macho.sym_index, | | |
| 3681 | .file = null, | | |
| 3682 | }).?; | | |
| 3683 | got_atom.dirty = true; | | |
| 3684 | } else if (code_len < decl.link.macho.size) { | | |
| 3685 | self.shrinkAtom(&decl.link.macho, code_len, match); | | |
| 3686 | } | 2981 | } |
| 3687 | decl.link.macho.size = code_len; | | |
| 3688 | decl.link.macho.dirty = true; | | |
| 3689 | | | |
| 3690 | symbol.n_strx = try self.strtab.insert(self.base.allocator, sym_name); | | |
| 3691 | symbol.n_type = macho.N_SECT; | | |
| 3692 | symbol.n_sect = @intCast(u8, self.text_section_index.?) + 1; | | |
| 3693 | symbol.n_desc = 0; | | |
| 3694 | } else { | | |
| 3695 | const name_str_index = try self.strtab.insert(self.base.allocator, sym_name); | | |
| 3696 | const addr = try self.allocateAtom(&decl.link.macho, code_len, required_alignment, match); | | |
| 3697 | | 2982 | |
| 3698 | log.debug("allocated atom for {s} at 0x{x}", .{ sym_name, addr }); | 2983 | if (sect.isCode()) { |
| 3699 | log.debug(" (required alignment 0x{x})", .{required_alignment}); | 2984 | if (self.text_section_index == null) { |
| | 2985 | self.text_section_index = try self.initSection("__TEXT", "__text", .{ |
| | 2986 | .flags = macho.S_REGULAR | |
| | 2987 | macho.S_ATTR_PURE_INSTRUCTIONS | |
| | 2988 | macho.S_ATTR_SOME_INSTRUCTIONS, |
| | 2989 | }); |
| | 2990 | } |
| | 2991 | break :blk self.text_section_index.?; |
| | 2992 | } |
| 3700 | | 2993 | |
| 3701 | errdefer self.freeAtom(&decl.link.macho, match, false); | 2994 | if (sect.isDebug()) { |
| | 2995 | // TODO debug attributes |
| | 2996 | if (mem.eql(u8, "__LD", segname) and mem.eql(u8, "__compact_unwind", sectname)) { |
| | 2997 | log.debug("TODO compact unwind section: type 0x{x}, name '{s},{s}'", .{ |
| | 2998 | sect.flags, segname, sectname, |
| | 2999 | }); |
| | 3000 | } |
| | 3001 | break :blk null; |
| | 3002 | } |
| 3702 | | 3003 | |
| 3703 | const symbol = decl.link.macho.getSymbolPtr(self); | 3004 | switch (sect.@"type"()) { |
| 3704 | symbol.* = .{ | 3005 | macho.S_4BYTE_LITERALS, |
| 3705 | .n_strx = name_str_index, | 3006 | macho.S_8BYTE_LITERALS, |
| 3706 | .n_type = macho.N_SECT, | 3007 | macho.S_16BYTE_LITERALS, |
| 3707 | .n_sect = match + 1, | 3008 | => { |
| 3708 | .n_desc = 0, | 3009 | if (self.getSectionByName("__TEXT", "__const")) |sect_id| break :blk sect_id; |
| 3709 | .n_value = addr, | 3010 | break :blk try self.initSection("__TEXT", "__const", .{}); |
| 3710 | }; | 3011 | }, |
| | 3012 | macho.S_CSTRING_LITERALS => { |
| | 3013 | if (mem.startsWith(u8, sectname, "__objc")) { |
| | 3014 | if (self.getSectionByName(segname, sectname)) |sect_id| break :blk sect_id; |
| | 3015 | break :blk try self.initSection(segname, sectname, .{}); |
| | 3016 | } |
| | 3017 | if (self.getSectionByName("__TEXT", "__cstring")) |sect_id| break :blk sect_id; |
| | 3018 | break :blk try self.initSection("__TEXT", "__cstring", .{ |
| | 3019 | .flags = macho.S_CSTRING_LITERALS, |
| | 3020 | }); |
| | 3021 | }, |
| | 3022 | macho.S_MOD_INIT_FUNC_POINTERS, |
| | 3023 | macho.S_MOD_TERM_FUNC_POINTERS, |
| | 3024 | => { |
| | 3025 | if (self.getSectionByName("__DATA_CONST", sectname)) |sect_id| break :blk sect_id; |
| | 3026 | break :blk try self.initSection("__DATA_CONST", sectname, .{ |
| | 3027 | .flags = sect.flags, |
| | 3028 | }); |
| | 3029 | }, |
| | 3030 | macho.S_LITERAL_POINTERS, |
| | 3031 | macho.S_ZEROFILL, |
| | 3032 | macho.S_THREAD_LOCAL_VARIABLES, |
| | 3033 | macho.S_THREAD_LOCAL_VARIABLE_POINTERS, |
| | 3034 | macho.S_THREAD_LOCAL_REGULAR, |
| | 3035 | macho.S_THREAD_LOCAL_ZEROFILL, |
| | 3036 | => { |
| | 3037 | if (self.getSectionByName(segname, sectname)) |sect_id| break :blk sect_id; |
| | 3038 | break :blk try self.initSection(segname, sectname, .{ .flags = sect.flags }); |
| | 3039 | }, |
| | 3040 | macho.S_COALESCED => { |
| | 3041 | if (self.getSectionByName(segname, sectname)) |sect_id| break :blk sect_id; |
| | 3042 | break :blk try self.initSection(segname, sectname, .{}); |
| | 3043 | }, |
| | 3044 | macho.S_REGULAR => { |
| | 3045 | if (mem.eql(u8, segname, "__TEXT")) { |
| | 3046 | if (mem.eql(u8, sectname, "__rodata") or |
| | 3047 | mem.eql(u8, sectname, "__typelink") or |
| | 3048 | mem.eql(u8, sectname, "__itablink") or |
| | 3049 | mem.eql(u8, sectname, "__gosymtab") or |
| | 3050 | mem.eql(u8, sectname, "__gopclntab")) |
| | 3051 | { |
| | 3052 | if (self.getSectionByName("__DATA_CONST", "__const")) |sect_id| break :blk sect_id; |
| | 3053 | break :blk try self.initSection("__DATA_CONST", "__const", .{}); |
| | 3054 | } |
| | 3055 | } |
| | 3056 | if (mem.eql(u8, segname, "__DATA")) { |
| | 3057 | if (mem.eql(u8, sectname, "__const") or |
| | 3058 | mem.eql(u8, sectname, "__cfstring") or |
| | 3059 | mem.eql(u8, sectname, "__objc_classlist") or |
| | 3060 | mem.eql(u8, sectname, "__objc_imageinfo")) |
| | 3061 | { |
| | 3062 | if (self.getSectionByName("__DATA_CONST", sectname)) |sect_id| break :blk sect_id; |
| | 3063 | break :blk try self.initSection("__DATA_CONST", sectname, .{}); |
| | 3064 | } else if (mem.eql(u8, sectname, "__data")) { |
| | 3065 | if (self.data_section_index == null) { |
| | 3066 | self.data_section_index = try self.initSection(segname, sectname, .{}); |
| | 3067 | } |
| | 3068 | break :blk self.data_section_index.?; |
| | 3069 | } |
| | 3070 | } |
| | 3071 | if (self.getSectionByName(segname, sectname)) |sect_id| break :blk sect_id; |
| | 3072 | break :blk try self.initSection(segname, sectname, .{}); |
| | 3073 | }, |
| | 3074 | else => break :blk null, |
| | 3075 | } |
| | 3076 | }; |
| | 3077 | return sect_id; |
| | 3078 | } |
| | 3079 | |
| | 3080 | fn updateDeclCode(self: *MachO, decl_index: Module.Decl.Index, code: []const u8) !u64 { |
| | 3081 | const gpa = self.base.allocator; |
| | 3082 | const mod = self.base.options.module.?; |
| | 3083 | const decl = mod.declPtr(decl_index); |
| | 3084 | |
| | 3085 | const required_alignment = decl.getAlignment(self.base.options.target); |
| | 3086 | assert(decl.link.macho.sym_index != 0); // Caller forgot to call allocateDeclIndexes() |
| | 3087 | |
| | 3088 | const sym_name = try decl.getFullyQualifiedName(mod); |
| | 3089 | defer self.base.allocator.free(sym_name); |
| | 3090 | |
| | 3091 | const atom = &decl.link.macho; |
| | 3092 | const decl_ptr = self.decls.getPtr(decl_index).?; |
| | 3093 | if (decl_ptr.* == null) { |
| | 3094 | decl_ptr.* = self.getDeclOutputSection(decl); |
| | 3095 | } |
| | 3096 | const sect_id = decl_ptr.*.?; |
| | 3097 | const code_len = code.len; |
| | 3098 | |
| | 3099 | if (atom.size != 0) { |
| | 3100 | const sym = atom.getSymbolPtr(self); |
| | 3101 | sym.n_strx = try self.strtab.insert(gpa, sym_name); |
| | 3102 | sym.n_type = macho.N_SECT; |
| | 3103 | sym.n_sect = sect_id + 1; |
| | 3104 | sym.n_desc = 0; |
| | 3105 | |
| | 3106 | const capacity = decl.link.macho.capacity(self); |
| | 3107 | const need_realloc = code_len > capacity or !mem.isAlignedGeneric(u64, sym.n_value, required_alignment); |
| | 3108 | |
| | 3109 | if (need_realloc) { |
| | 3110 | const vaddr = try self.growAtom(atom, code_len, required_alignment); |
| | 3111 | log.debug("growing {s} and moving from 0x{x} to 0x{x}", .{ sym_name, sym.n_value, vaddr }); |
| | 3112 | log.debug(" (required alignment 0x{x})", .{required_alignment}); |
| | 3113 | |
| | 3114 | if (vaddr != sym.n_value) { |
| | 3115 | sym.n_value = vaddr; |
| | 3116 | log.debug(" (updating GOT entry)", .{}); |
| | 3117 | const got_target = SymbolWithLoc{ .sym_index = atom.sym_index, .file = null }; |
| | 3118 | const got_atom = self.getGotAtomForSymbol(got_target).?; |
| | 3119 | self.markRelocsDirtyByTarget(got_target); |
| | 3120 | try self.writePtrWidthAtom(got_atom); |
| | 3121 | } |
| | 3122 | } else if (code_len < atom.size) { |
| | 3123 | self.shrinkAtom(atom, code_len); |
| | 3124 | } else if (atom.next == null) { |
| | 3125 | const header = &self.sections.items(.header)[sect_id]; |
| | 3126 | const segment = self.getSegment(sect_id); |
| | 3127 | const needed_size = (sym.n_value + code_len) - segment.vmaddr; |
| | 3128 | header.size = needed_size; |
| | 3129 | } |
| | 3130 | atom.size = code_len; |
| | 3131 | } else { |
| | 3132 | const name_str_index = try self.strtab.insert(gpa, sym_name); |
| | 3133 | const sym = atom.getSymbolPtr(self); |
| | 3134 | sym.n_strx = name_str_index; |
| | 3135 | sym.n_type = macho.N_SECT; |
| | 3136 | sym.n_sect = sect_id + 1; |
| | 3137 | sym.n_desc = 0; |
| | 3138 | |
| | 3139 | const vaddr = try self.allocateAtom(atom, code_len, required_alignment); |
| | 3140 | errdefer self.freeAtom(atom, false); |
| | 3141 | |
| | 3142 | log.debug("allocated atom for {s} at 0x{x}", .{ sym_name, vaddr }); |
| | 3143 | log.debug(" (required alignment 0x{x})", .{required_alignment}); |
| | 3144 | |
| | 3145 | atom.size = code_len; |
| | 3146 | sym.n_value = vaddr; |
| 3711 | | 3147 | |
| 3712 | const got_target = SymbolWithLoc{ .sym_index = decl.link.macho.sym_index, .file = null }; | 3148 | const got_target = SymbolWithLoc{ .sym_index = atom.sym_index, .file = null }; |
| 3713 | const got_index = try self.allocateGotEntry(got_target); | 3149 | const got_index = try self.allocateGotEntry(got_target); |
| 3714 | const got_atom = try self.createGotAtom(got_target); | 3150 | const got_atom = try self.createGotAtom(got_target); |
| 3715 | self.got_entries.items[got_index].sym_index = got_atom.sym_index; | 3151 | self.got_entries.items[got_index].sym_index = got_atom.sym_index; |
| | 3152 | try self.writePtrWidthAtom(got_atom); |
| 3716 | } | 3153 | } |
| 3717 | | 3154 | |
| 3718 | return decl.link.macho.getSymbol(self).n_value; | 3155 | self.markRelocsDirtyByTarget(atom.getSymbolWithLoc()); |
| | 3156 | try self.writeAtom(atom, code); |
| | 3157 | |
| | 3158 | return atom.getSymbol(self).n_value; |
| 3719 | } | 3159 | } |
| 3720 | | 3160 | |
| 3721 | pub fn updateDeclLineNumber(self: *MachO, module: *Module, decl: *const Module.Decl) !void { | 3161 | pub fn updateDeclLineNumber(self: *MachO, module: *Module, decl: *const Module.Decl) !void { |
| ... | @@ -3863,20 +3303,25 @@ pub fn deleteExport(self: *MachO, exp: Export) void { | ... | @@ -3863,20 +3303,25 @@ pub fn deleteExport(self: *MachO, exp: Export) void { |
| 3863 | } | 3303 | } |
| 3864 | } | 3304 | } |
| 3865 | | 3305 | |
| | 3306 | fn freeRelocationsForAtom(self: *MachO, atom: *Atom) void { |
| | 3307 | _ = self.relocs.remove(atom); |
| | 3308 | _ = self.rebases.remove(atom); |
| | 3309 | _ = self.bindings.remove(atom); |
| | 3310 | _ = self.lazy_bindings.remove(atom); |
| | 3311 | } |
| | 3312 | |
| 3866 | fn freeUnnamedConsts(self: *MachO, decl_index: Module.Decl.Index) void { | 3313 | fn freeUnnamedConsts(self: *MachO, decl_index: Module.Decl.Index) void { |
| | 3314 | const gpa = self.base.allocator; |
| 3867 | const unnamed_consts = self.unnamed_const_atoms.getPtr(decl_index) orelse return; | 3315 | const unnamed_consts = self.unnamed_const_atoms.getPtr(decl_index) orelse return; |
| 3868 | for (unnamed_consts.items) |atom| { | 3316 | for (unnamed_consts.items) |atom| { |
| 3869 | // TODO | 3317 | self.freeAtom(atom, true); |
| 3870 | // const sect_id = atom.getSymbol(self).n_sect; | 3318 | self.locals_free_list.append(gpa, atom.sym_index) catch {}; |
| 3871 | const sect_id = self.getSectionByName("__TEXT", "__const").?; | | |
| 3872 | self.freeAtom(atom, sect_id, true); | | |
| 3873 | self.locals_free_list.append(self.base.allocator, atom.sym_index) catch {}; | | |
| 3874 | self.locals.items[atom.sym_index].n_type = 0; | 3319 | self.locals.items[atom.sym_index].n_type = 0; |
| 3875 | _ = self.atom_by_index_table.remove(atom.sym_index); | 3320 | _ = self.atom_by_index_table.remove(atom.sym_index); |
| 3876 | log.debug(" adding local symbol index {d} to free list", .{atom.sym_index}); | 3321 | log.debug(" adding local symbol index {d} to free list", .{atom.sym_index}); |
| 3877 | atom.sym_index = 0; | 3322 | atom.sym_index = 0; |
| 3878 | } | 3323 | } |
| 3879 | unnamed_consts.clearAndFree(self.base.allocator); | 3324 | unnamed_consts.clearAndFree(gpa); |
| 3880 | } | 3325 | } |
| 3881 | | 3326 | |
| 3882 | pub fn freeDecl(self: *MachO, decl_index: Module.Decl.Index) void { | 3327 | pub fn freeDecl(self: *MachO, decl_index: Module.Decl.Index) void { |
| ... | @@ -3885,20 +3330,25 @@ pub fn freeDecl(self: *MachO, decl_index: Module.Decl.Index) void { | ... | @@ -3885,20 +3330,25 @@ pub fn freeDecl(self: *MachO, decl_index: Module.Decl.Index) void { |
| 3885 | } | 3330 | } |
| 3886 | const mod = self.base.options.module.?; | 3331 | const mod = self.base.options.module.?; |
| 3887 | const decl = mod.declPtr(decl_index); | 3332 | const decl = mod.declPtr(decl_index); |
| | 3333 | |
| 3888 | log.debug("freeDecl {*}", .{decl}); | 3334 | log.debug("freeDecl {*}", .{decl}); |
| | 3335 | |
| 3889 | const kv = self.decls.fetchSwapRemove(decl_index); | 3336 | const kv = self.decls.fetchSwapRemove(decl_index); |
| 3890 | if (kv.?.value) |match| { | 3337 | if (kv.?.value) |_| { |
| 3891 | self.freeAtom(&decl.link.macho, match, false); | 3338 | self.freeAtom(&decl.link.macho, false); |
| 3892 | self.freeUnnamedConsts(decl_index); | 3339 | self.freeUnnamedConsts(decl_index); |
| 3893 | } | 3340 | } |
| | 3341 | |
| 3894 | // Appending to free lists is allowed to fail because the free lists are heuristics based anyway. | 3342 | // Appending to free lists is allowed to fail because the free lists are heuristics based anyway. |
| 3895 | if (decl.link.macho.sym_index != 0) { | 3343 | const gpa = self.base.allocator; |
| 3896 | self.locals_free_list.append(self.base.allocator, decl.link.macho.sym_index) catch {}; | 3344 | const sym_index = decl.link.macho.sym_index; |
| | 3345 | if (sym_index != 0) { |
| | 3346 | self.locals_free_list.append(gpa, sym_index) catch {}; |
| 3897 | | 3347 | |
| 3898 | // Try freeing GOT atom if this decl had one | 3348 | // Try freeing GOT atom if this decl had one |
| 3899 | const got_target = SymbolWithLoc{ .sym_index = decl.link.macho.sym_index, .file = null }; | 3349 | const got_target = SymbolWithLoc{ .sym_index = sym_index, .file = null }; |
| 3900 | if (self.got_entries_table.get(got_target)) |got_index| { | 3350 | if (self.got_entries_table.get(got_target)) |got_index| { |
| 3901 | self.got_entries_free_list.append(self.base.allocator, @intCast(u32, got_index)) catch {}; | 3351 | self.got_entries_free_list.append(gpa, @intCast(u32, got_index)) catch {}; |
| 3902 | self.got_entries.items[got_index] = .{ | 3352 | self.got_entries.items[got_index] = .{ |
| 3903 | .target = .{ .sym_index = 0, .file = null }, | 3353 | .target = .{ .sym_index = 0, .file = null }, |
| 3904 | .sym_index = 0, | 3354 | .sym_index = 0, |
| ... | @@ -3906,20 +3356,18 @@ pub fn freeDecl(self: *MachO, decl_index: Module.Decl.Index) void { | ... | @@ -3906,20 +3356,18 @@ pub fn freeDecl(self: *MachO, decl_index: Module.Decl.Index) void { |
| 3906 | _ = self.got_entries_table.remove(got_target); | 3356 | _ = self.got_entries_table.remove(got_target); |
| 3907 | | 3357 | |
| 3908 | if (self.d_sym) |*d_sym| { | 3358 | if (self.d_sym) |*d_sym| { |
| 3909 | d_sym.swapRemoveRelocs(decl.link.macho.sym_index); | 3359 | d_sym.swapRemoveRelocs(sym_index); |
| 3910 | } | 3360 | } |
| 3911 | | 3361 | |
| 3912 | log.debug(" adding GOT index {d} to free list (target local@{d})", .{ | 3362 | log.debug(" adding GOT index {d} to free list (target local@{d})", .{ got_index, sym_index }); |
| 3913 | got_index, | | |
| 3914 | decl.link.macho.sym_index, | | |
| 3915 | }); | | |
| 3916 | } | 3363 | } |
| 3917 | | 3364 | |
| 3918 | self.locals.items[decl.link.macho.sym_index].n_type = 0; | 3365 | self.locals.items[sym_index].n_type = 0; |
| 3919 | _ = self.atom_by_index_table.remove(decl.link.macho.sym_index); | 3366 | _ = self.atom_by_index_table.remove(sym_index); |
| 3920 | log.debug(" adding local symbol index {d} to free list", .{decl.link.macho.sym_index}); | 3367 | log.debug(" adding local symbol index {d} to free list", .{sym_index}); |
| 3921 | decl.link.macho.sym_index = 0; | 3368 | decl.link.macho.sym_index = 0; |
| 3922 | } | 3369 | } |
| | 3370 | |
| 3923 | if (self.d_sym) |*d_sym| { | 3371 | if (self.d_sym) |*d_sym| { |
| 3924 | d_sym.dwarf.freeDecl(decl); | 3372 | d_sym.dwarf.freeDecl(decl); |
| 3925 | } | 3373 | } |
| ... | @@ -3932,260 +3380,155 @@ pub fn getDeclVAddr(self: *MachO, decl_index: Module.Decl.Index, reloc_info: Fil | ... | @@ -3932,260 +3380,155 @@ pub fn getDeclVAddr(self: *MachO, decl_index: Module.Decl.Index, reloc_info: Fil |
| 3932 | assert(self.llvm_object == null); | 3380 | assert(self.llvm_object == null); |
| 3933 | assert(decl.link.macho.sym_index != 0); | 3381 | assert(decl.link.macho.sym_index != 0); |
| 3934 | | 3382 | |
| 3935 | const atom = self.atom_by_index_table.get(reloc_info.parent_atom_index).?; | 3383 | const atom = self.getAtomForSymbol(.{ .sym_index = reloc_info.parent_atom_index, .file = null }).?; |
| 3936 | try atom.relocs.append(self.base.allocator, .{ | 3384 | try atom.addRelocation(self, .{ |
| 3937 | .offset = @intCast(u32, reloc_info.offset), | | |
| 3938 | .target = .{ .sym_index = decl.link.macho.sym_index, .file = null }, | | |
| 3939 | .addend = reloc_info.addend, | | |
| 3940 | .subtractor = null, | | |
| 3941 | .pcrel = false, | | |
| 3942 | .length = 3, | | |
| 3943 | .@"type" = switch (self.base.options.target.cpu.arch) { | 3385 | .@"type" = switch (self.base.options.target.cpu.arch) { |
| 3944 | .aarch64 => @enumToInt(macho.reloc_type_arm64.ARM64_RELOC_UNSIGNED), | 3386 | .aarch64 => @enumToInt(macho.reloc_type_arm64.ARM64_RELOC_UNSIGNED), |
| 3945 | .x86_64 => @enumToInt(macho.reloc_type_x86_64.X86_64_RELOC_UNSIGNED), | 3387 | .x86_64 => @enumToInt(macho.reloc_type_x86_64.X86_64_RELOC_UNSIGNED), |
| 3946 | else => unreachable, | 3388 | else => unreachable, |
| 3947 | }, | 3389 | }, |
| | 3390 | .target = .{ .sym_index = decl.link.macho.sym_index, .file = null }, |
| | 3391 | .offset = @intCast(u32, reloc_info.offset), |
| | 3392 | .addend = reloc_info.addend, |
| | 3393 | .pcrel = false, |
| | 3394 | .length = 3, |
| 3948 | }); | 3395 | }); |
| 3949 | try atom.rebases.append(self.base.allocator, reloc_info.offset); | 3396 | try atom.addRebase(self, @intCast(u32, reloc_info.offset)); |
| 3950 | | 3397 | |
| 3951 | return 0; | 3398 | return 0; |
| 3952 | } | 3399 | } |
| 3953 | | 3400 | |
| 3954 | fn populateMissingMetadata(self: *MachO) !void { | 3401 | pub fn populateMissingMetadata(self: *MachO) !void { |
| | 3402 | assert(self.mode == .incremental); |
| | 3403 | |
| 3955 | const gpa = self.base.allocator; | 3404 | const gpa = self.base.allocator; |
| 3956 | const cpu_arch = self.base.options.target.cpu.arch; | 3405 | const cpu_arch = self.base.options.target.cpu.arch; |
| 3957 | const pagezero_vmsize = self.base.options.pagezero_size orelse default_pagezero_vmsize; | 3406 | const pagezero_vmsize = self.calcPagezeroSize(); |
| 3958 | const aligned_pagezero_vmsize = mem.alignBackwardGeneric(u64, pagezero_vmsize, self.page_size); | 3407 | |
| 3959 | | 3408 | if (self.pagezero_segment_cmd_index == null) { |
| 3960 | if (self.pagezero_segment_cmd_index == null) blk: { | 3409 | if (pagezero_vmsize > 0) { |
| 3961 | if (self.base.options.output_mode == .Lib) break :blk; | 3410 | self.pagezero_segment_cmd_index = @intCast(u8, self.segments.items.len); |
| 3962 | if (aligned_pagezero_vmsize == 0) break :blk; | 3411 | try self.segments.append(gpa, .{ |
| 3963 | if (aligned_pagezero_vmsize != pagezero_vmsize) { | 3412 | .segname = makeStaticString("__PAGEZERO"), |
| 3964 | log.warn("requested __PAGEZERO size (0x{x}) is not page aligned", .{pagezero_vmsize}); | 3413 | .vmsize = pagezero_vmsize, |
| 3965 | log.warn(" rounding down to 0x{x}", .{aligned_pagezero_vmsize}); | 3414 | .cmdsize = @sizeOf(macho.segment_command_64), |
| | 3415 | }); |
| 3966 | } | 3416 | } |
| 3967 | self.pagezero_segment_cmd_index = @intCast(u8, self.segments.items.len); | | |
| 3968 | try self.segments.append(gpa, .{ | | |
| 3969 | .segname = makeStaticString("__PAGEZERO"), | | |
| 3970 | .vmsize = aligned_pagezero_vmsize, | | |
| 3971 | .cmdsize = @sizeOf(macho.segment_command_64), | | |
| 3972 | }); | | |
| 3973 | } | 3417 | } |
| 3974 | | 3418 | |
| 3975 | if (self.text_segment_cmd_index == null) { | 3419 | if (self.header_segment_cmd_index == null) { |
| 3976 | self.text_segment_cmd_index = @intCast(u8, self.segments.items.len); | 3420 | // The first __TEXT segment is immovable and covers MachO header and load commands. |
| 3977 | const needed_size = if (self.mode == .incremental) blk: { | 3421 | self.header_segment_cmd_index = @intCast(u8, self.segments.items.len); |
| 3978 | const headerpad_size = @maximum(self.base.options.headerpad_size orelse 0, default_headerpad_size); | 3422 | const ideal_size = @maximum(self.base.options.headerpad_size orelse 0, default_headerpad_size); |
| 3979 | const program_code_size_hint = self.base.options.program_code_size_hint; | 3423 | const needed_size = mem.alignForwardGeneric(u64, padToIdeal(ideal_size), self.page_size); |
| 3980 | const got_size_hint = @sizeOf(u64) * self.base.options.symbol_count_hint; | 3424 | |
| 3981 | const ideal_size = headerpad_size + program_code_size_hint + got_size_hint; | 3425 | log.debug("found __TEXT segment (header-only) free space 0x{x} to 0x{x}", .{ 0, needed_size }); |
| 3982 | const needed_size = mem.alignForwardGeneric(u64, padToIdeal(ideal_size), self.page_size); | 3426 | |
| 3983 | log.debug("found __TEXT segment free space 0x{x} to 0x{x}", .{ 0, needed_size }); | | |
| 3984 | break :blk needed_size; | | |
| 3985 | } else 0; | | |
| 3986 | try self.segments.append(gpa, .{ | 3427 | try self.segments.append(gpa, .{ |
| 3987 | .segname = makeStaticString("__TEXT"), | 3428 | .segname = makeStaticString("__TEXT"), |
| 3988 | .vmaddr = aligned_pagezero_vmsize, | 3429 | .vmaddr = pagezero_vmsize, |
| 3989 | .vmsize = needed_size, | 3430 | .vmsize = needed_size, |
| 3990 | .filesize = needed_size, | 3431 | .filesize = needed_size, |
| 3991 | .maxprot = macho.PROT.READ | macho.PROT.EXEC, | 3432 | .maxprot = macho.PROT.READ | macho.PROT.EXEC, |
| 3992 | .initprot = macho.PROT.READ | macho.PROT.EXEC, | 3433 | .initprot = macho.PROT.READ | macho.PROT.EXEC, |
| 3993 | .cmdsize = @sizeOf(macho.segment_command_64), | 3434 | .cmdsize = @sizeOf(macho.segment_command_64), |
| 3994 | }); | 3435 | }); |
| | 3436 | self.segment_table_dirty = true; |
| 3995 | } | 3437 | } |
| 3996 | | 3438 | |
| 3997 | if (self.text_section_index == null) { | 3439 | if (self.text_section_index == null) { |
| 3998 | const alignment: u2 = switch (cpu_arch) { | 3440 | // Sadly, segments need unique string identfiers for some reason. |
| 3999 | .x86_64 => 0, | 3441 | self.text_section_index = try self.allocateSection("__TEXT1", "__text", .{ |
| 4000 | .aarch64 => 2, | 3442 | .size = self.base.options.program_code_size_hint, |
| 4001 | else => unreachable, // unhandled architecture type | 3443 | .alignment = switch (cpu_arch) { |
| 4002 | }; | 3444 | .x86_64 => 1, |
| 4003 | const needed_size = if (self.mode == .incremental) self.base.options.program_code_size_hint else 0; | 3445 | .aarch64 => @sizeOf(u32), |
| 4004 | self.text_section_index = try self.initSection( | 3446 | else => unreachable, // unhandled architecture type |
| 4005 | "__TEXT", | | |
| 4006 | "__text", | | |
| 4007 | needed_size, | | |
| 4008 | alignment, | | |
| 4009 | .{ | | |
| 4010 | .flags = macho.S_REGULAR | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS, | | |
| 4011 | }, | 3447 | }, |
| 4012 | ); | 3448 | .flags = macho.S_REGULAR | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS, |
| | 3449 | .prot = macho.PROT.READ | macho.PROT.EXEC, |
| | 3450 | }); |
| | 3451 | self.segment_table_dirty = true; |
| 4013 | } | 3452 | } |
| 4014 | | 3453 | |
| 4015 | if (self.stubs_section_index == null) { | 3454 | if (self.stubs_section_index == null) { |
| 4016 | const alignment: u2 = switch (cpu_arch) { | 3455 | const stub_size: u32 = switch (cpu_arch) { |
| 4017 | .x86_64 => 0, | | |
| 4018 | .aarch64 => 2, | | |
| 4019 | else => unreachable, // unhandled architecture type | | |
| 4020 | }; | | |
| 4021 | const stub_size: u4 = switch (cpu_arch) { | | |
| 4022 | .x86_64 => 6, | 3456 | .x86_64 => 6, |
| 4023 | .aarch64 => 3 * @sizeOf(u32), | 3457 | .aarch64 => 3 * @sizeOf(u32), |
| 4024 | else => unreachable, // unhandled architecture type | 3458 | else => unreachable, // unhandled architecture type |
| 4025 | }; | 3459 | }; |
| 4026 | const needed_size = if (self.mode == .incremental) stub_size * self.base.options.symbol_count_hint else 0; | 3460 | self.stubs_section_index = try self.allocateSection("__TEXT2", "__stubs", .{ |
| 4027 | self.stubs_section_index = try self.initSection( | 3461 | .size = stub_size, |
| 4028 | "__TEXT", | 3462 | .alignment = switch (cpu_arch) { |
| 4029 | "__stubs", | 3463 | .x86_64 => 1, |
| 4030 | needed_size, | 3464 | .aarch64 => @sizeOf(u32), |
| 4031 | alignment, | 3465 | else => unreachable, // unhandled architecture type |
| 4032 | .{ | | |
| 4033 | .flags = macho.S_SYMBOL_STUBS | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS, | | |
| 4034 | .reserved2 = stub_size, | | |
| 4035 | }, | 3466 | }, |
| 4036 | ); | 3467 | .flags = macho.S_SYMBOL_STUBS | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS, |
| | 3468 | .reserved2 = stub_size, |
| | 3469 | .prot = macho.PROT.READ | macho.PROT.EXEC, |
| | 3470 | }); |
| | 3471 | self.segment_table_dirty = true; |
| 4037 | } | 3472 | } |
| 4038 | | 3473 | |
| 4039 | if (self.stub_helper_section_index == null) { | 3474 | if (self.stub_helper_section_index == null) { |
| 4040 | const alignment: u2 = switch (cpu_arch) { | 3475 | self.stub_helper_section_index = try self.allocateSection("__TEXT3", "__stub_helper", .{ |
| 4041 | .x86_64 => 0, | 3476 | .size = @sizeOf(u32), |
| 4042 | .aarch64 => 2, | 3477 | .alignment = switch (cpu_arch) { |
| 4043 | else => unreachable, // unhandled architecture type | 3478 | .x86_64 => 1, |
| 4044 | }; | 3479 | .aarch64 => @sizeOf(u32), |
| 4045 | const preamble_size: u6 = switch (cpu_arch) { | 3480 | else => unreachable, // unhandled architecture type |
| 4046 | .x86_64 => 15, | | |
| 4047 | .aarch64 => 6 * @sizeOf(u32), | | |
| 4048 | else => unreachable, | | |
| 4049 | }; | | |
| 4050 | const stub_size: u4 = switch (cpu_arch) { | | |
| 4051 | .x86_64 => 10, | | |
| 4052 | .aarch64 => 3 * @sizeOf(u32), | | |
| 4053 | else => unreachable, | | |
| 4054 | }; | | |
| 4055 | const needed_size = if (self.mode == .incremental) | | |
| 4056 | stub_size * self.base.options.symbol_count_hint + preamble_size | | |
| 4057 | else | | |
| 4058 | 0; | | |
| 4059 | self.stub_helper_section_index = try self.initSection( | | |
| 4060 | "__TEXT", | | |
| 4061 | "__stub_helper", | | |
| 4062 | needed_size, | | |
| 4063 | alignment, | | |
| 4064 | .{ | | |
| 4065 | .flags = macho.S_REGULAR | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS, | | |
| 4066 | }, | 3481 | }, |
| 4067 | ); | 3482 | .flags = macho.S_REGULAR | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS, |
| 4068 | } | 3483 | .prot = macho.PROT.READ | macho.PROT.EXEC, |
| 4069 | | | |
| 4070 | if (self.data_const_segment_cmd_index == null) { | | |
| 4071 | self.data_const_segment_cmd_index = @intCast(u8, self.segments.items.len); | | |
| 4072 | var vmaddr: u64 = 0; | | |
| 4073 | var fileoff: u64 = 0; | | |
| 4074 | var needed_size: u64 = 0; | | |
| 4075 | if (self.mode == .incremental) { | | |
| 4076 | const base = self.getSegmentAllocBase(&.{self.text_segment_cmd_index.?}); | | |
| 4077 | vmaddr = base.vmaddr; | | |
| 4078 | fileoff = base.fileoff; | | |
| 4079 | const ideal_size = @sizeOf(u64) * self.base.options.symbol_count_hint; | | |
| 4080 | needed_size = mem.alignForwardGeneric(u64, padToIdeal(ideal_size), self.page_size); | | |
| 4081 | log.debug("found __DATA_CONST segment free space 0x{x} to 0x{x}", .{ | | |
| 4082 | fileoff, | | |
| 4083 | fileoff + needed_size, | | |
| 4084 | }); | | |
| 4085 | } | | |
| 4086 | try self.segments.append(gpa, .{ | | |
| 4087 | .segname = makeStaticString("__DATA_CONST"), | | |
| 4088 | .vmaddr = vmaddr, | | |
| 4089 | .vmsize = needed_size, | | |
| 4090 | .fileoff = fileoff, | | |
| 4091 | .filesize = needed_size, | | |
| 4092 | .maxprot = macho.PROT.READ | macho.PROT.WRITE, | | |
| 4093 | .initprot = macho.PROT.READ | macho.PROT.WRITE, | | |
| 4094 | .cmdsize = @sizeOf(macho.segment_command_64), | | |
| 4095 | }); | 3484 | }); |
| | 3485 | self.segment_table_dirty = true; |
| 4096 | } | 3486 | } |
| 4097 | | 3487 | |
| 4098 | if (self.got_section_index == null) { | 3488 | if (self.got_section_index == null) { |
| 4099 | const needed_size = if (self.mode == .incremental) | 3489 | self.got_section_index = try self.allocateSection("__DATA_CONST", "__got", .{ |
| 4100 | @sizeOf(u64) * self.base.options.symbol_count_hint | 3490 | .size = @sizeOf(u64) * self.base.options.symbol_count_hint, |
| 4101 | else | 3491 | .alignment = @alignOf(u64), |
| 4102 | 0; | 3492 | .flags = macho.S_NON_LAZY_SYMBOL_POINTERS, |
| 4103 | const alignment: u16 = 3; // 2^3 = @sizeOf(u64) | 3493 | .prot = macho.PROT.READ | macho.PROT.WRITE, |
| 4104 | self.got_section_index = try self.initSection( | 3494 | }); |
| 4105 | "__DATA_CONST", | 3495 | self.segment_table_dirty = true; |
| 4106 | "__got", | | |
| 4107 | needed_size, | | |
| 4108 | alignment, | | |
| 4109 | .{ | | |
| 4110 | .flags = macho.S_NON_LAZY_SYMBOL_POINTERS, | | |
| 4111 | }, | | |
| 4112 | ); | | |
| 4113 | } | 3496 | } |
| 4114 | | 3497 | |
| 4115 | if (self.data_segment_cmd_index == null) { | 3498 | if (self.data_const_section_index == null) { |
| 4116 | self.data_segment_cmd_index = @intCast(u8, self.segments.items.len); | 3499 | self.data_const_section_index = try self.allocateSection("__DATA_CONST1", "__const", .{ |
| 4117 | var vmaddr: u64 = 0; | 3500 | .size = @sizeOf(u64), |
| 4118 | var fileoff: u64 = 0; | 3501 | .alignment = @alignOf(u64), |
| 4119 | var needed_size: u64 = 0; | 3502 | .flags = macho.S_REGULAR, |
| 4120 | if (self.mode == .incremental) { | 3503 | .prot = macho.PROT.READ | macho.PROT.WRITE, |
| 4121 | const base = self.getSegmentAllocBase(&.{self.data_const_segment_cmd_index.?}); | | |
| 4122 | vmaddr = base.vmaddr; | | |
| 4123 | fileoff = base.fileoff; | | |
| 4124 | const ideal_size = 2 * @sizeOf(u64) * self.base.options.symbol_count_hint; | | |
| 4125 | needed_size = mem.alignForwardGeneric(u64, padToIdeal(ideal_size), self.page_size); | | |
| 4126 | log.debug("found __DATA segment free space 0x{x} to 0x{x}", .{ | | |
| 4127 | fileoff, | | |
| 4128 | fileoff + needed_size, | | |
| 4129 | }); | | |
| 4130 | } | | |
| 4131 | try self.segments.append(gpa, .{ | | |
| 4132 | .segname = makeStaticString("__DATA"), | | |
| 4133 | .vmaddr = vmaddr, | | |
| 4134 | .vmsize = needed_size, | | |
| 4135 | .fileoff = fileoff, | | |
| 4136 | .filesize = needed_size, | | |
| 4137 | .maxprot = macho.PROT.READ | macho.PROT.WRITE, | | |
| 4138 | .initprot = macho.PROT.READ | macho.PROT.WRITE, | | |
| 4139 | .cmdsize = @sizeOf(macho.segment_command_64), | | |
| 4140 | }); | 3504 | }); |
| | 3505 | self.segment_table_dirty = true; |
| 4141 | } | 3506 | } |
| 4142 | | 3507 | |
| 4143 | if (self.la_symbol_ptr_section_index == null) { | 3508 | if (self.la_symbol_ptr_section_index == null) { |
| 4144 | const needed_size = if (self.mode == .incremental) | 3509 | self.la_symbol_ptr_section_index = try self.allocateSection("__DATA", "__la_symbol_ptr", .{ |
| 4145 | @sizeOf(u64) * self.base.options.symbol_count_hint | 3510 | .size = @sizeOf(u64), |
| 4146 | else | 3511 | .alignment = @alignOf(u64), |
| 4147 | 0; | 3512 | .flags = macho.S_LAZY_SYMBOL_POINTERS, |
| 4148 | const alignment: u16 = 3; // 2^3 = @sizeOf(u64) | 3513 | .prot = macho.PROT.READ | macho.PROT.WRITE, |
| 4149 | self.la_symbol_ptr_section_index = try self.initSection( | 3514 | }); |
| 4150 | "__DATA", | 3515 | self.segment_table_dirty = true; |
| 4151 | "__la_symbol_ptr", | | |
| 4152 | needed_size, | | |
| 4153 | alignment, | | |
| 4154 | .{ | | |
| 4155 | .flags = macho.S_LAZY_SYMBOL_POINTERS, | | |
| 4156 | }, | | |
| 4157 | ); | | |
| 4158 | } | 3516 | } |
| 4159 | | 3517 | |
| 4160 | if (self.data_section_index == null) { | 3518 | if (self.data_section_index == null) { |
| 4161 | const needed_size = if (self.mode == .incremental) | 3519 | self.data_section_index = try self.allocateSection("__DATA1", "__data", .{ |
| 4162 | @sizeOf(u64) * self.base.options.symbol_count_hint | 3520 | .size = @sizeOf(u64), |
| 4163 | else | 3521 | .alignment = @alignOf(u64), |
| 4164 | 0; | 3522 | .flags = macho.S_REGULAR, |
| 4165 | const alignment: u16 = 3; // 2^3 = @sizeOf(u64) | 3523 | .prot = macho.PROT.READ | macho.PROT.WRITE, |
| 4166 | self.data_section_index = try self.initSection( | 3524 | }); |
| 4167 | "__DATA", | 3525 | self.segment_table_dirty = true; |
| 4168 | "__data", | | |
| 4169 | needed_size, | | |
| 4170 | alignment, | | |
| 4171 | .{}, | | |
| 4172 | ); | | |
| 4173 | } | 3526 | } |
| 4174 | | 3527 | |
| 4175 | if (self.linkedit_segment_cmd_index == null) { | 3528 | if (self.linkedit_segment_cmd_index == null) { |
| 4176 | self.linkedit_segment_cmd_index = @intCast(u8, self.segments.items.len); | 3529 | self.linkedit_segment_cmd_index = @intCast(u8, self.segments.items.len); |
| 4177 | var vmaddr: u64 = 0; | | |
| 4178 | var fileoff: u64 = 0; | | |
| 4179 | if (self.mode == .incremental) { | | |
| 4180 | const base = self.getSegmentAllocBase(&.{self.data_segment_cmd_index.?}); | | |
| 4181 | vmaddr = base.vmaddr; | | |
| 4182 | fileoff = base.fileoff; | | |
| 4183 | log.debug("found __LINKEDIT segment free space at 0x{x}", .{fileoff}); | | |
| 4184 | } | | |
| 4185 | try self.segments.append(gpa, .{ | 3530 | try self.segments.append(gpa, .{ |
| 4186 | .segname = makeStaticString("__LINKEDIT"), | 3531 | .segname = makeStaticString("__LINKEDIT"), |
| 4187 | .vmaddr = vmaddr, | | |
| 4188 | .fileoff = fileoff, | | |
| 4189 | .maxprot = macho.PROT.READ, | 3532 | .maxprot = macho.PROT.READ, |
| 4190 | .initprot = macho.PROT.READ, | 3533 | .initprot = macho.PROT.READ, |
| 4191 | .cmdsize = @sizeOf(macho.segment_command_64), | 3534 | .cmdsize = @sizeOf(macho.segment_command_64), |
| ... | @@ -4283,7 +3626,19 @@ fn calcLCsSize(self: *MachO, assume_max_path_len: bool) !u32 { | ... | @@ -4283,7 +3626,19 @@ fn calcLCsSize(self: *MachO, assume_max_path_len: bool) !u32 { |
| 4283 | return @intCast(u32, sizeofcmds); | 3626 | return @intCast(u32, sizeofcmds); |
| 4284 | } | 3627 | } |
| 4285 | | 3628 | |
| 4286 | fn calcMinHeaderPad(self: *MachO) !u64 { | 3629 | pub fn calcPagezeroSize(self: *MachO) u64 { |
| | 3630 | const pagezero_vmsize = self.base.options.pagezero_size orelse default_pagezero_vmsize; |
| | 3631 | const aligned_pagezero_vmsize = mem.alignBackwardGeneric(u64, pagezero_vmsize, self.page_size); |
| | 3632 | if (self.base.options.output_mode == .Lib) return 0; |
| | 3633 | if (aligned_pagezero_vmsize == 0) return 0; |
| | 3634 | if (aligned_pagezero_vmsize != pagezero_vmsize) { |
| | 3635 | log.warn("requested __PAGEZERO size (0x{x}) is not page aligned", .{pagezero_vmsize}); |
| | 3636 | log.warn(" rounding down to 0x{x}", .{aligned_pagezero_vmsize}); |
| | 3637 | } |
| | 3638 | return aligned_pagezero_vmsize; |
| | 3639 | } |
| | 3640 | |
| | 3641 | pub fn calcMinHeaderPad(self: *MachO) !u64 { |
| 4287 | var padding: u32 = (try self.calcLCsSize(false)) + (self.base.options.headerpad_size orelse 0); | 3642 | var padding: u32 = (try self.calcLCsSize(false)) + (self.base.options.headerpad_size orelse 0); |
| 4288 | log.debug("minimum requested headerpad size 0x{x}", .{padding + @sizeOf(macho.mach_header_64)}); | 3643 | log.debug("minimum requested headerpad size 0x{x}", .{padding + @sizeOf(macho.mach_header_64)}); |
| 4289 | | 3644 | |
| ... | @@ -4300,444 +3655,118 @@ fn calcMinHeaderPad(self: *MachO) !u64 { | ... | @@ -4300,444 +3655,118 @@ fn calcMinHeaderPad(self: *MachO) !u64 { |
| 4300 | return offset; | 3655 | return offset; |
| 4301 | } | 3656 | } |
| 4302 | | 3657 | |
| 4303 | fn allocateSegments(self: *MachO) !void { | 3658 | fn allocateSection(self: *MachO, segname: []const u8, sectname: []const u8, opts: struct { |
| 4304 | try self.allocateSegment(self.text_segment_cmd_index, &.{ | 3659 | size: u64 = 0, |
| 4305 | self.pagezero_segment_cmd_index, | 3660 | alignment: u32 = 0, |
| 4306 | }, try self.calcMinHeaderPad()); | 3661 | prot: macho.vm_prot_t = macho.PROT.NONE, |
| 4307 | | | |
| 4308 | if (self.text_segment_cmd_index) |index| blk: { | | |
| 4309 | const indexes = self.getSectionIndexes(index); | | |
| 4310 | if (indexes.start == indexes.end) break :blk; | | |
| 4311 | const seg = self.segments.items[index]; | | |
| 4312 | | | |
| 4313 | // Shift all sections to the back to minimize jump size between __TEXT and __DATA segments. | | |
| 4314 | var min_alignment: u32 = 0; | | |
| 4315 | for (self.sections.items(.header)[indexes.start..indexes.end]) |header| { | | |
| 4316 | const alignment = try math.powi(u32, 2, header.@"align"); | | |
| 4317 | min_alignment = math.max(min_alignment, alignment); | | |
| 4318 | } | | |
| 4319 | | | |
| 4320 | assert(min_alignment > 0); | | |
| 4321 | const last_header = self.sections.items(.header)[indexes.end - 1]; | | |
| 4322 | const shift: u32 = shift: { | | |
| 4323 | const diff = seg.filesize - last_header.offset - last_header.size; | | |
| 4324 | const factor = @divTrunc(diff, min_alignment); | | |
| 4325 | break :shift @intCast(u32, factor * min_alignment); | | |
| 4326 | }; | | |
| 4327 | | | |
| 4328 | if (shift > 0) { | | |
| 4329 | for (self.sections.items(.header)[indexes.start..indexes.end]) |*header| { | | |
| 4330 | header.offset += shift; | | |
| 4331 | header.addr += shift; | | |
| 4332 | } | | |
| 4333 | } | | |
| 4334 | } | | |
| 4335 | | | |
| 4336 | try self.allocateSegment(self.data_const_segment_cmd_index, &.{ | | |
| 4337 | self.text_segment_cmd_index, | | |
| 4338 | self.pagezero_segment_cmd_index, | | |
| 4339 | }, 0); | | |
| 4340 | | | |
| 4341 | try self.allocateSegment(self.data_segment_cmd_index, &.{ | | |
| 4342 | self.data_const_segment_cmd_index, | | |
| 4343 | self.text_segment_cmd_index, | | |
| 4344 | self.pagezero_segment_cmd_index, | | |
| 4345 | }, 0); | | |
| 4346 | | | |
| 4347 | try self.allocateSegment(self.linkedit_segment_cmd_index, &.{ | | |
| 4348 | self.data_segment_cmd_index, | | |
| 4349 | self.data_const_segment_cmd_index, | | |
| 4350 | self.text_segment_cmd_index, | | |
| 4351 | self.pagezero_segment_cmd_index, | | |
| 4352 | }, 0); | | |
| 4353 | } | | |
| 4354 | | | |
| 4355 | fn allocateSegment(self: *MachO, maybe_index: ?u8, indices: []const ?u8, init_size: u64) !void { | | |
| 4356 | const index = maybe_index orelse return; | | |
| 4357 | const seg = &self.segments.items[index]; | | |
| 4358 | | | |
| 4359 | const base = self.getSegmentAllocBase(indices); | | |
| 4360 | seg.vmaddr = base.vmaddr; | | |
| 4361 | seg.fileoff = base.fileoff; | | |
| 4362 | seg.filesize = init_size; | | |
| 4363 | seg.vmsize = init_size; | | |
| 4364 | | | |
| 4365 | // Allocate the sections according to their alignment at the beginning of the segment. | | |
| 4366 | const indexes = self.getSectionIndexes(index); | | |
| 4367 | var start = init_size; | | |
| 4368 | const slice = self.sections.slice(); | | |
| 4369 | for (slice.items(.header)[indexes.start..indexes.end]) |*header| { | | |
| 4370 | const alignment = try math.powi(u32, 2, header.@"align"); | | |
| 4371 | const start_aligned = mem.alignForwardGeneric(u64, start, alignment); | | |
| 4372 | | | |
| 4373 | header.offset = if (header.isZerofill()) | | |
| 4374 | 0 | | |
| 4375 | else | | |
| 4376 | @intCast(u32, seg.fileoff + start_aligned); | | |
| 4377 | header.addr = seg.vmaddr + start_aligned; | | |
| 4378 | | | |
| 4379 | start = start_aligned + header.size; | | |
| 4380 | | | |
| 4381 | if (!header.isZerofill()) { | | |
| 4382 | seg.filesize = start; | | |
| 4383 | } | | |
| 4384 | seg.vmsize = start; | | |
| 4385 | } | | |
| 4386 | | | |
| 4387 | seg.filesize = mem.alignForwardGeneric(u64, seg.filesize, self.page_size); | | |
| 4388 | seg.vmsize = mem.alignForwardGeneric(u64, seg.vmsize, self.page_size); | | |
| 4389 | } | | |
| 4390 | | | |
| 4391 | const InitSectionOpts = struct { | | |
| 4392 | flags: u32 = macho.S_REGULAR, | 3662 | flags: u32 = macho.S_REGULAR, |
| 4393 | reserved1: u32 = 0, | | |
| 4394 | reserved2: u32 = 0, | 3663 | reserved2: u32 = 0, |
| 4395 | }; | 3664 | }) !u8 { |
| | 3665 | const gpa = self.base.allocator; |
| | 3666 | // In incremental context, we create one section per segment pairing. This way, |
| | 3667 | // we can move the segment in raw file as we please. |
| | 3668 | const segment_id = @intCast(u8, self.segments.items.len); |
| | 3669 | const section_id = @intCast(u8, self.sections.slice().len); |
| | 3670 | const vmaddr = blk: { |
| | 3671 | const prev_segment = self.segments.items[segment_id - 1]; |
| | 3672 | break :blk mem.alignForwardGeneric(u64, prev_segment.vmaddr + prev_segment.vmsize, self.page_size); |
| | 3673 | }; |
| | 3674 | // We commit more memory than needed upfront so that we don't have to reallocate too soon. |
| | 3675 | const vmsize = mem.alignForwardGeneric(u64, opts.size, self.page_size); |
| | 3676 | const off = self.findFreeSpace(opts.size, self.page_size); |
| | 3677 | |
| | 3678 | log.debug("found {s},{s} free space 0x{x} to 0x{x} (0x{x} - 0x{x})", .{ |
| | 3679 | segname, |
| | 3680 | sectname, |
| | 3681 | off, |
| | 3682 | off + opts.size, |
| | 3683 | vmaddr, |
| | 3684 | vmaddr + vmsize, |
| | 3685 | }); |
| 4396 | | 3686 | |
| 4397 | fn initSection( | 3687 | const seg = try self.segments.addOne(gpa); |
| 4398 | self: *MachO, | 3688 | seg.* = .{ |
| 4399 | segname: []const u8, | 3689 | .segname = makeStaticString(segname), |
| 4400 | sectname: []const u8, | 3690 | .vmaddr = vmaddr, |
| 4401 | size: u64, | 3691 | .vmsize = vmsize, |
| 4402 | alignment: u32, | 3692 | .fileoff = off, |
| 4403 | opts: InitSectionOpts, | 3693 | .filesize = vmsize, |
| 4404 | ) !u8 { | 3694 | .maxprot = opts.prot, |
| 4405 | const segment_id = self.getSegmentByName(segname).?; | 3695 | .initprot = opts.prot, |
| 4406 | const seg = &self.segments.items[segment_id]; | 3696 | .nsects = 1, |
| 4407 | const index = try self.insertSection(segment_id, .{ | 3697 | .cmdsize = @sizeOf(macho.segment_command_64) + @sizeOf(macho.section_64), |
| | 3698 | }; |
| | 3699 | |
| | 3700 | var section = macho.section_64{ |
| 4408 | .sectname = makeStaticString(sectname), | 3701 | .sectname = makeStaticString(sectname), |
| 4409 | .segname = seg.segname, | 3702 | .segname = makeStaticString(segname), |
| | 3703 | .addr = mem.alignForwardGeneric(u64, vmaddr, opts.alignment), |
| | 3704 | .offset = mem.alignForwardGeneric(u32, @intCast(u32, off), opts.alignment), |
| | 3705 | .size = opts.size, |
| | 3706 | .@"align" = math.log2(opts.alignment), |
| 4410 | .flags = opts.flags, | 3707 | .flags = opts.flags, |
| 4411 | .reserved1 = opts.reserved1, | | |
| 4412 | .reserved2 = opts.reserved2, | 3708 | .reserved2 = opts.reserved2, |
| 4413 | }); | 3709 | }; |
| 4414 | seg.cmdsize += @sizeOf(macho.section_64); | 3710 | assert(!section.isZerofill()); // TODO zerofill sections |
| 4415 | seg.nsects += 1; | | |
| 4416 | | | |
| 4417 | if (self.mode == .incremental) { | | |
| 4418 | const header = &self.sections.items(.header)[index]; | | |
| 4419 | header.size = size; | | |
| 4420 | header.@"align" = alignment; | | |
| 4421 | | | |
| 4422 | const prev_end_off = if (index > 0) blk: { | | |
| 4423 | const prev_section = self.sections.get(index - 1); | | |
| 4424 | if (prev_section.segment_index == segment_id) { | | |
| 4425 | const prev_header = prev_section.header; | | |
| 4426 | break :blk prev_header.offset + padToIdeal(prev_header.size); | | |
| 4427 | } else break :blk seg.fileoff; | | |
| 4428 | } else 0; | | |
| 4429 | const alignment_pow_2 = try math.powi(u32, 2, alignment); | | |
| 4430 | // TODO better prealloc for __text section | | |
| 4431 | // const padding: u64 = if (index == 0) try self.calcMinHeaderPad() else 0; | | |
| 4432 | const padding: u64 = if (index == 0) 0x1000 else 0; | | |
| 4433 | const off = mem.alignForwardGeneric(u64, padding + prev_end_off, alignment_pow_2); | | |
| 4434 | | | |
| 4435 | if (!header.isZerofill()) { | | |
| 4436 | header.offset = @intCast(u32, off); | | |
| 4437 | } | | |
| 4438 | header.addr = seg.vmaddr + off - seg.fileoff; | | |
| 4439 | | | |
| 4440 | // TODO Will this break if we are inserting section that is not the last section | | |
| 4441 | // in a segment? | | |
| 4442 | const max_size = self.allocatedSize(segment_id, off); | | |
| 4443 | | | |
| 4444 | if (size > max_size) { | | |
| 4445 | try self.growSection(index, @intCast(u32, size)); | | |
| 4446 | } | | |
| 4447 | | | |
| 4448 | log.debug("allocating {s},{s} section at 0x{x}", .{ header.segName(), header.sectName(), off }); | | |
| 4449 | | | |
| 4450 | self.updateSectionOrdinals(index + 1); | | |
| 4451 | } | | |
| 4452 | | | |
| 4453 | return index; | | |
| 4454 | } | | |
| 4455 | | 3711 | |
| 4456 | fn getSectionPrecedence(header: macho.section_64) u4 { | 3712 | try self.sections.append(gpa, .{ |
| 4457 | if (header.isCode()) { | 3713 | .segment_index = segment_id, |
| 4458 | if (mem.eql(u8, "__text", header.sectName())) return 0x0; | 3714 | .header = section, |
| 4459 | if (header.@"type"() == macho.S_SYMBOL_STUBS) return 0x1; | 3715 | }); |
| 4460 | return 0x2; | 3716 | return section_id; |
| 4461 | } | | |
| 4462 | switch (header.@"type"()) { | | |
| 4463 | macho.S_NON_LAZY_SYMBOL_POINTERS, | | |
| 4464 | macho.S_LAZY_SYMBOL_POINTERS, | | |
| 4465 | => return 0x0, | | |
| 4466 | macho.S_MOD_INIT_FUNC_POINTERS => return 0x1, | | |
| 4467 | macho.S_MOD_TERM_FUNC_POINTERS => return 0x2, | | |
| 4468 | macho.S_ZEROFILL => return 0xf, | | |
| 4469 | macho.S_THREAD_LOCAL_REGULAR => return 0xd, | | |
| 4470 | macho.S_THREAD_LOCAL_ZEROFILL => return 0xe, | | |
| 4471 | else => if (mem.eql(u8, "__eh_frame", header.sectName())) | | |
| 4472 | return 0xf | | |
| 4473 | else | | |
| 4474 | return 0x3, | | |
| 4475 | } | | |
| 4476 | } | 3717 | } |
| 4477 | | 3718 | |
| 4478 | fn insertSection(self: *MachO, segment_index: u8, header: macho.section_64) !u8 { | 3719 | fn moveSectionInVirtualMemory(self: *MachO, sect_id: u8, needed_size: u64) !void { |
| 4479 | const precedence = getSectionPrecedence(header); | 3720 | const header = &self.sections.items(.header)[sect_id]; |
| 4480 | const indexes = self.getSectionIndexes(segment_index); | 3721 | const segment = self.getSegmentPtr(sect_id); |
| 4481 | const insertion_index = for (self.sections.items(.header)[indexes.start..indexes.end]) |hdr, i| { | 3722 | const increased_size = padToIdeal(needed_size); |
| 4482 | if (getSectionPrecedence(hdr) > precedence) break @intCast(u8, i + indexes.start); | 3723 | const old_aligned_end = segment.vmaddr + segment.vmsize; |
| 4483 | } else indexes.end; | 3724 | const new_aligned_end = segment.vmaddr + mem.alignForwardGeneric(u64, increased_size, self.page_size); |
| 4484 | log.debug("inserting section '{s},{s}' at index {d}", .{ | 3725 | const diff = new_aligned_end - old_aligned_end; |
| | 3726 | log.debug("shifting every segment after {s},{s} in virtual memory by {x}", .{ |
| 4485 | header.segName(), | 3727 | header.segName(), |
| 4486 | header.sectName(), | 3728 | header.sectName(), |
| 4487 | insertion_index, | 3729 | diff, |
| 4488 | }); | | |
| 4489 | for (&[_]*?u8{ | | |
| 4490 | &self.text_section_index, | | |
| 4491 | &self.stubs_section_index, | | |
| 4492 | &self.stub_helper_section_index, | | |
| 4493 | &self.got_section_index, | | |
| 4494 | &self.la_symbol_ptr_section_index, | | |
| 4495 | &self.data_section_index, | | |
| 4496 | }) |maybe_index| { | | |
| 4497 | const index = maybe_index.* orelse continue; | | |
| 4498 | if (insertion_index <= index) maybe_index.* = index + 1; | | |
| 4499 | } | | |
| 4500 | try self.sections.insert(self.base.allocator, insertion_index, .{ | | |
| 4501 | .segment_index = segment_index, | | |
| 4502 | .header = header, | | |
| 4503 | }); | 3730 | }); |
| 4504 | return insertion_index; | | |
| 4505 | } | | |
| 4506 | | | |
| 4507 | fn updateSectionOrdinals(self: *MachO, start: u8) void { | | |
| 4508 | const tracy = trace(@src()); | | |
| 4509 | defer tracy.end(); | | |
| 4510 | | | |
| 4511 | const slice = self.sections.slice(); | | |
| 4512 | for (slice.items(.last_atom)[start..]) |last_atom| { | | |
| 4513 | var atom = last_atom orelse continue; | | |
| 4514 | | | |
| 4515 | while (true) { | | |
| 4516 | const sym = atom.getSymbolPtr(self); | | |
| 4517 | sym.n_sect = start + 1; | | |
| 4518 | | 3731 | |
| 4519 | for (atom.contained.items) |sym_at_off| { | 3732 | // TODO: enforce order by increasing VM addresses in self.sections container. |
| 4520 | const contained_sym = self.getSymbolPtr(.{ | 3733 | for (self.sections.items(.header)[sect_id + 1 ..]) |*next_header, next_sect_id| { |
| 4521 | .sym_index = sym_at_off.sym_index, | 3734 | const index = @intCast(u8, sect_id + 1 + next_sect_id); |
| 4522 | .file = atom.file, | 3735 | const maybe_last_atom = &self.sections.items(.last_atom)[index]; |
| 4523 | }); | 3736 | const next_segment = self.getSegmentPtr(index); |
| 4524 | contained_sym.n_sect = start + 1; | 3737 | next_header.addr += diff; |
| | 3738 | next_segment.vmaddr += diff; |
| | 3739 | |
| | 3740 | if (maybe_last_atom.*) |last_atom| { |
| | 3741 | var atom = last_atom; |
| | 3742 | while (true) { |
| | 3743 | const sym = atom.getSymbolPtr(self); |
| | 3744 | sym.n_value += diff; |
| | 3745 | |
| | 3746 | if (atom.prev) |prev| { |
| | 3747 | atom = prev; |
| | 3748 | } else break; |
| 4525 | } | 3749 | } |
| 4526 | | | |
| 4527 | if (atom.prev) |prev| { | | |
| 4528 | atom = prev; | | |
| 4529 | } else break; | | |
| 4530 | } | | |
| 4531 | } | | |
| 4532 | } | | |
| 4533 | | | |
| 4534 | fn shiftLocalsByOffset(self: *MachO, sect_id: u8, offset: i64) !void { | | |
| 4535 | var atom = self.sections.items(.last_atom)[sect_id] orelse return; | | |
| 4536 | | | |
| 4537 | while (true) { | | |
| 4538 | const atom_sym = atom.getSymbolPtr(self); | | |
| 4539 | atom_sym.n_value = @intCast(u64, @intCast(i64, atom_sym.n_value) + offset); | | |
| 4540 | | | |
| 4541 | for (atom.contained.items) |sym_at_off| { | | |
| 4542 | const contained_sym = self.getSymbolPtr(.{ | | |
| 4543 | .sym_index = sym_at_off.sym_index, | | |
| 4544 | .file = atom.file, | | |
| 4545 | }); | | |
| 4546 | contained_sym.n_value = @intCast(u64, @intCast(i64, contained_sym.n_value) + offset); | | |
| 4547 | } | | |
| 4548 | | | |
| 4549 | if (atom.prev) |prev| { | | |
| 4550 | atom = prev; | | |
| 4551 | } else break; | | |
| 4552 | } | | |
| 4553 | } | | |
| 4554 | | | |
| 4555 | fn growSegment(self: *MachO, segment_index: u8, new_size: u64) !void { | | |
| 4556 | const segment = &self.segments.items[segment_index]; | | |
| 4557 | const new_segment_size = mem.alignForwardGeneric(u64, new_size, self.page_size); | | |
| 4558 | assert(new_segment_size > segment.filesize); | | |
| 4559 | const offset_amt = new_segment_size - segment.filesize; | | |
| 4560 | log.debug("growing segment {s} from 0x{x} to 0x{x}", .{ | | |
| 4561 | segment.segname, | | |
| 4562 | segment.filesize, | | |
| 4563 | new_segment_size, | | |
| 4564 | }); | | |
| 4565 | segment.filesize = new_segment_size; | | |
| 4566 | segment.vmsize = new_segment_size; | | |
| 4567 | | | |
| 4568 | log.debug(" (new segment file offsets from 0x{x} to 0x{x} (in memory 0x{x} to 0x{x}))", .{ | | |
| 4569 | segment.fileoff, | | |
| 4570 | segment.fileoff + segment.filesize, | | |
| 4571 | segment.vmaddr, | | |
| 4572 | segment.vmaddr + segment.vmsize, | | |
| 4573 | }); | | |
| 4574 | | | |
| 4575 | var next: u8 = segment_index + 1; | | |
| 4576 | while (next < self.linkedit_segment_cmd_index.? + 1) : (next += 1) { | | |
| 4577 | const next_segment = &self.segments.items[next]; | | |
| 4578 | | | |
| 4579 | try MachO.copyRangeAllOverlappingAlloc( | | |
| 4580 | self.base.allocator, | | |
| 4581 | self.base.file.?, | | |
| 4582 | next_segment.fileoff, | | |
| 4583 | next_segment.fileoff + offset_amt, | | |
| 4584 | math.cast(usize, next_segment.filesize) orelse return error.Overflow, | | |
| 4585 | ); | | |
| 4586 | | | |
| 4587 | next_segment.fileoff += offset_amt; | | |
| 4588 | next_segment.vmaddr += offset_amt; | | |
| 4589 | | | |
| 4590 | log.debug(" (new {s} segment file offsets from 0x{x} to 0x{x} (in memory 0x{x} to 0x{x}))", .{ | | |
| 4591 | next_segment.segname, | | |
| 4592 | next_segment.fileoff, | | |
| 4593 | next_segment.fileoff + next_segment.filesize, | | |
| 4594 | next_segment.vmaddr, | | |
| 4595 | next_segment.vmaddr + next_segment.vmsize, | | |
| 4596 | }); | | |
| 4597 | | | |
| 4598 | const indexes = self.getSectionIndexes(next); | | |
| 4599 | for (self.sections.items(.header)[indexes.start..indexes.end]) |*header, i| { | | |
| 4600 | header.offset += @intCast(u32, offset_amt); | | |
| 4601 | header.addr += offset_amt; | | |
| 4602 | | | |
| 4603 | log.debug(" (new {s},{s} file offsets from 0x{x} to 0x{x} (in memory 0x{x} to 0x{x}))", .{ | | |
| 4604 | header.segName(), | | |
| 4605 | header.sectName(), | | |
| 4606 | header.offset, | | |
| 4607 | header.offset + header.size, | | |
| 4608 | header.addr, | | |
| 4609 | header.addr + header.size, | | |
| 4610 | }); | | |
| 4611 | | | |
| 4612 | try self.shiftLocalsByOffset(@intCast(u8, i + indexes.start), @intCast(i64, offset_amt)); | | |
| 4613 | } | 3750 | } |
| 4614 | } | 3751 | } |
| 4615 | } | 3752 | } |
| 4616 | | 3753 | |
| 4617 | fn growSection(self: *MachO, sect_id: u8, new_size: u32) !void { | 3754 | fn allocateAtom(self: *MachO, atom: *Atom, new_atom_size: u64, alignment: u64) !u64 { |
| 4618 | const tracy = trace(@src()); | | |
| 4619 | defer tracy.end(); | | |
| 4620 | | | |
| 4621 | const section = self.sections.get(sect_id); | | |
| 4622 | const segment_index = section.segment_index; | | |
| 4623 | const header = section.header; | | |
| 4624 | const segment = self.segments.items[segment_index]; | | |
| 4625 | | | |
| 4626 | const alignment = try math.powi(u32, 2, header.@"align"); | | |
| 4627 | const max_size = self.allocatedSize(segment_index, header.offset); | | |
| 4628 | const ideal_size = padToIdeal(new_size); | | |
| 4629 | const needed_size = mem.alignForwardGeneric(u32, ideal_size, alignment); | | |
| 4630 | | | |
| 4631 | if (needed_size > max_size) blk: { | | |
| 4632 | log.debug(" (need to grow! needed 0x{x}, max 0x{x})", .{ needed_size, max_size }); | | |
| 4633 | | | |
| 4634 | const indexes = self.getSectionIndexes(segment_index); | | |
| 4635 | if (sect_id == indexes.end - 1) { | | |
| 4636 | // Last section, just grow segments | | |
| 4637 | try self.growSegment(segment_index, segment.filesize + needed_size - max_size); | | |
| 4638 | break :blk; | | |
| 4639 | } | | |
| 4640 | | | |
| 4641 | // Need to move all sections below in file and address spaces. | | |
| 4642 | const offset_amt = offset: { | | |
| 4643 | const max_alignment = try self.getSectionMaxAlignment(sect_id + 1, indexes.end); | | |
| 4644 | break :offset mem.alignForwardGeneric(u64, needed_size - max_size, max_alignment); | | |
| 4645 | }; | | |
| 4646 | | | |
| 4647 | // Before we commit to this, check if the segment needs to grow too. | | |
| 4648 | // We assume that each section header is growing linearly with the increasing | | |
| 4649 | // file offset / virtual memory address space. | | |
| 4650 | const last_sect_header = self.sections.items(.header)[indexes.end - 1]; | | |
| 4651 | const last_sect_off = last_sect_header.offset + last_sect_header.size; | | |
| 4652 | const seg_off = segment.fileoff + segment.filesize; | | |
| 4653 | | | |
| 4654 | if (last_sect_off + offset_amt > seg_off) { | | |
| 4655 | // Need to grow segment first. | | |
| 4656 | const spill_size = (last_sect_off + offset_amt) - seg_off; | | |
| 4657 | try self.growSegment(segment_index, segment.filesize + spill_size); | | |
| 4658 | } | | |
| 4659 | | | |
| 4660 | // We have enough space to expand within the segment, so move all sections by | | |
| 4661 | // the required amount and update their header offsets. | | |
| 4662 | const next_sect = self.sections.items(.header)[sect_id + 1]; | | |
| 4663 | const total_size = last_sect_off - next_sect.offset; | | |
| 4664 | | | |
| 4665 | try MachO.copyRangeAllOverlappingAlloc( | | |
| 4666 | self.base.allocator, | | |
| 4667 | self.base.file.?, | | |
| 4668 | next_sect.offset, | | |
| 4669 | next_sect.offset + offset_amt, | | |
| 4670 | math.cast(usize, total_size) orelse return error.Overflow, | | |
| 4671 | ); | | |
| 4672 | | | |
| 4673 | for (self.sections.items(.header)[sect_id + 1 .. indexes.end]) |*moved_sect, i| { | | |
| 4674 | moved_sect.offset += @intCast(u32, offset_amt); | | |
| 4675 | moved_sect.addr += offset_amt; | | |
| 4676 | | | |
| 4677 | log.debug(" (new {s},{s} file offsets from 0x{x} to 0x{x} (in memory 0x{x} to 0x{x}))", .{ | | |
| 4678 | moved_sect.segName(), | | |
| 4679 | moved_sect.sectName(), | | |
| 4680 | moved_sect.offset, | | |
| 4681 | moved_sect.offset + moved_sect.size, | | |
| 4682 | moved_sect.addr, | | |
| 4683 | moved_sect.addr + moved_sect.size, | | |
| 4684 | }); | | |
| 4685 | | | |
| 4686 | try self.shiftLocalsByOffset(@intCast(u8, sect_id + 1 + i), @intCast(i64, offset_amt)); | | |
| 4687 | } | | |
| 4688 | } | | |
| 4689 | } | | |
| 4690 | | | |
| 4691 | fn allocatedSize(self: MachO, segment_id: u8, start: u64) u64 { | | |
| 4692 | const segment = self.segments.items[segment_id]; | | |
| 4693 | const indexes = self.getSectionIndexes(segment_id); | | |
| 4694 | assert(start >= segment.fileoff); | | |
| 4695 | var min_pos: u64 = segment.fileoff + segment.filesize; | | |
| 4696 | if (start > min_pos) return 0; | | |
| 4697 | for (self.sections.items(.header)[indexes.start..indexes.end]) |header| { | | |
| 4698 | if (header.offset <= start) continue; | | |
| 4699 | if (header.offset < min_pos) min_pos = header.offset; | | |
| 4700 | } | | |
| 4701 | return min_pos - start; | | |
| 4702 | } | | |
| 4703 | | | |
| 4704 | fn getSectionMaxAlignment(self: *MachO, start: u8, end: u8) !u32 { | | |
| 4705 | var max_alignment: u32 = 1; | | |
| 4706 | const slice = self.sections.slice(); | | |
| 4707 | for (slice.items(.header)[start..end]) |header| { | | |
| 4708 | const alignment = try math.powi(u32, 2, header.@"align"); | | |
| 4709 | max_alignment = math.max(max_alignment, alignment); | | |
| 4710 | } | | |
| 4711 | return max_alignment; | | |
| 4712 | } | | |
| 4713 | | | |
| 4714 | fn allocateAtomCommon(self: *MachO, atom: *Atom, sect_id: u8) !void { | | |
| 4715 | const sym = atom.getSymbolPtr(self); | | |
| 4716 | if (self.mode == .incremental) { | | |
| 4717 | const size = atom.size; | | |
| 4718 | const alignment = try math.powi(u32, 2, atom.alignment); | | |
| 4719 | const vaddr = try self.allocateAtom(atom, size, alignment, sect_id); | | |
| 4720 | const sym_name = atom.getName(self); | | |
| 4721 | log.debug("allocated {s} atom at 0x{x}", .{ sym_name, vaddr }); | | |
| 4722 | sym.n_value = vaddr; | | |
| 4723 | } else try self.addAtomToSection(atom, sect_id); | | |
| 4724 | sym.n_sect = sect_id + 1; | | |
| 4725 | } | | |
| 4726 | | | |
| 4727 | fn allocateAtom( | | |
| 4728 | self: *MachO, | | |
| 4729 | atom: *Atom, | | |
| 4730 | new_atom_size: u64, | | |
| 4731 | alignment: u64, | | |
| 4732 | sect_id: u8, | | |
| 4733 | ) !u64 { | | |
| 4734 | const tracy = trace(@src()); | 3755 | const tracy = trace(@src()); |
| 4735 | defer tracy.end(); | 3756 | defer tracy.end(); |
| 4736 | | 3757 | |
| | 3758 | const sect_id = atom.getSymbol(self).n_sect - 1; |
| | 3759 | const segment = self.getSegmentPtr(sect_id); |
| 4737 | const header = &self.sections.items(.header)[sect_id]; | 3760 | const header = &self.sections.items(.header)[sect_id]; |
| 4738 | const free_list = &self.sections.items(.free_list)[sect_id]; | 3761 | const free_list = &self.sections.items(.free_list)[sect_id]; |
| 4739 | const maybe_last_atom = &self.sections.items(.last_atom)[sect_id]; | 3762 | const maybe_last_atom = &self.sections.items(.last_atom)[sect_id]; |
| 4740 | const new_atom_ideal_capacity = if (header.isCode()) padToIdeal(new_atom_size) else new_atom_size; | 3763 | const requires_padding = blk: { |
| | 3764 | if (!header.isCode()) break :blk false; |
| | 3765 | if (header.isSymbolStubs()) break :blk false; |
| | 3766 | if (mem.eql(u8, "__stub_helper", header.sectName())) break :blk false; |
| | 3767 | break :blk true; |
| | 3768 | }; |
| | 3769 | const new_atom_ideal_capacity = if (requires_padding) padToIdeal(new_atom_size) else new_atom_size; |
| 4741 | | 3770 | |
| 4742 | // We use these to indicate our intention to update metadata, placing the new atom, | 3771 | // We use these to indicate our intention to update metadata, placing the new atom, |
| 4743 | // and possibly removing a free list node. | 3772 | // and possibly removing a free list node. |
| ... | @@ -4757,7 +3786,7 @@ fn allocateAtom( | ... | @@ -4757,7 +3786,7 @@ fn allocateAtom( |
| 4757 | // Is it enough that we could fit this new atom? | 3786 | // Is it enough that we could fit this new atom? |
| 4758 | const sym = big_atom.getSymbol(self); | 3787 | const sym = big_atom.getSymbol(self); |
| 4759 | const capacity = big_atom.capacity(self); | 3788 | const capacity = big_atom.capacity(self); |
| 4760 | const ideal_capacity = if (header.isCode()) padToIdeal(capacity) else capacity; | 3789 | const ideal_capacity = if (requires_padding) padToIdeal(capacity) else capacity; |
| 4761 | const ideal_capacity_end_vaddr = math.add(u64, sym.n_value, ideal_capacity) catch ideal_capacity; | 3790 | const ideal_capacity_end_vaddr = math.add(u64, sym.n_value, ideal_capacity) catch ideal_capacity; |
| 4762 | const capacity_end_vaddr = sym.n_value + capacity; | 3791 | const capacity_end_vaddr = sym.n_value + capacity; |
| 4763 | const new_start_vaddr_unaligned = capacity_end_vaddr - new_atom_ideal_capacity; | 3792 | const new_start_vaddr_unaligned = capacity_end_vaddr - new_atom_ideal_capacity; |
| ... | @@ -4787,29 +3816,63 @@ fn allocateAtom( | ... | @@ -4787,29 +3816,63 @@ fn allocateAtom( |
| 4787 | break :blk new_start_vaddr; | 3816 | break :blk new_start_vaddr; |
| 4788 | } else if (maybe_last_atom.*) |last| { | 3817 | } else if (maybe_last_atom.*) |last| { |
| 4789 | const last_symbol = last.getSymbol(self); | 3818 | const last_symbol = last.getSymbol(self); |
| 4790 | const ideal_capacity = if (header.isCode()) padToIdeal(last.size) else last.size; | 3819 | const ideal_capacity = if (requires_padding) padToIdeal(last.size) else last.size; |
| 4791 | const ideal_capacity_end_vaddr = last_symbol.n_value + ideal_capacity; | 3820 | const ideal_capacity_end_vaddr = last_symbol.n_value + ideal_capacity; |
| 4792 | const new_start_vaddr = mem.alignForwardGeneric(u64, ideal_capacity_end_vaddr, alignment); | 3821 | const new_start_vaddr = mem.alignForwardGeneric(u64, ideal_capacity_end_vaddr, alignment); |
| 4793 | atom_placement = last; | 3822 | atom_placement = last; |
| 4794 | break :blk new_start_vaddr; | 3823 | break :blk new_start_vaddr; |
| 4795 | } else { | 3824 | } else { |
| 4796 | break :blk mem.alignForwardGeneric(u64, header.addr, alignment); | 3825 | break :blk mem.alignForwardGeneric(u64, segment.vmaddr, alignment); |
| 4797 | } | 3826 | } |
| 4798 | }; | 3827 | }; |
| 4799 | | 3828 | |
| 4800 | const expand_section = atom_placement == null or atom_placement.?.next == null; | 3829 | const expand_section = atom_placement == null or atom_placement.?.next == null; |
| 4801 | if (expand_section) { | 3830 | if (expand_section) { |
| 4802 | const needed_size = @intCast(u32, (vaddr + new_atom_size) - header.addr); | 3831 | const sect_capacity = self.allocatedSize(header.offset); |
| 4803 | try self.growSection(sect_id, needed_size); | 3832 | const needed_size = (vaddr + new_atom_size) - segment.vmaddr; |
| 4804 | maybe_last_atom.* = atom; | 3833 | if (needed_size > sect_capacity) { |
| | 3834 | const new_offset = self.findFreeSpace(needed_size, self.page_size); |
| | 3835 | const current_size = if (maybe_last_atom.*) |last_atom| blk: { |
| | 3836 | const sym = last_atom.getSymbol(self); |
| | 3837 | break :blk (sym.n_value + last_atom.size) - segment.vmaddr; |
| | 3838 | } else 0; |
| | 3839 | |
| | 3840 | log.debug("moving {s},{s} from 0x{x} to 0x{x}", .{ |
| | 3841 | header.segName(), |
| | 3842 | header.sectName(), |
| | 3843 | header.offset, |
| | 3844 | new_offset, |
| | 3845 | }); |
| | 3846 | |
| | 3847 | const amt = try self.base.file.?.copyRangeAll( |
| | 3848 | header.offset, |
| | 3849 | self.base.file.?, |
| | 3850 | new_offset, |
| | 3851 | current_size, |
| | 3852 | ); |
| | 3853 | if (amt != current_size) return error.InputOutput; |
| | 3854 | header.offset = @intCast(u32, new_offset); |
| | 3855 | segment.fileoff = new_offset; |
| | 3856 | } |
| | 3857 | |
| | 3858 | const sect_vm_capacity = self.allocatedVirtualSize(segment.vmaddr); |
| | 3859 | if (needed_size > sect_vm_capacity) { |
| | 3860 | self.markRelocsDirtyByAddress(segment.vmaddr + needed_size); |
| | 3861 | try self.moveSectionInVirtualMemory(sect_id, needed_size); |
| | 3862 | } |
| | 3863 | |
| 4805 | header.size = needed_size; | 3864 | header.size = needed_size; |
| | 3865 | segment.filesize = mem.alignForwardGeneric(u64, needed_size, self.page_size); |
| | 3866 | segment.vmsize = mem.alignForwardGeneric(u64, needed_size, self.page_size); |
| | 3867 | maybe_last_atom.* = atom; |
| | 3868 | |
| | 3869 | self.segment_table_dirty = true; |
| 4806 | } | 3870 | } |
| | 3871 | |
| 4807 | const align_pow = @intCast(u32, math.log2(alignment)); | 3872 | const align_pow = @intCast(u32, math.log2(alignment)); |
| 4808 | if (header.@"align" < align_pow) { | 3873 | if (header.@"align" < align_pow) { |
| 4809 | header.@"align" = align_pow; | 3874 | header.@"align" = align_pow; |
| 4810 | } | 3875 | } |
| 4811 | atom.size = new_atom_size; | | |
| 4812 | atom.alignment = align_pow; | | |
| 4813 | | 3876 | |
| 4814 | if (atom.prev) |prev| { | 3877 | if (atom.prev) |prev| { |
| 4815 | prev.next = atom.next; | 3878 | prev.next = atom.next; |
| ... | @@ -4833,7 +3896,80 @@ fn allocateAtom( | ... | @@ -4833,7 +3896,80 @@ fn allocateAtom( |
| 4833 | return vaddr; | 3896 | return vaddr; |
| 4834 | } | 3897 | } |
| 4835 | | 3898 | |
| 4836 | pub fn addAtomToSection(self: *MachO, atom: *Atom, sect_id: u8) !void { | 3899 | fn getSectionPrecedence(header: macho.section_64) u4 { |
| | 3900 | if (header.isCode()) { |
| | 3901 | if (mem.eql(u8, "__text", header.sectName())) return 0x0; |
| | 3902 | if (header.@"type"() == macho.S_SYMBOL_STUBS) return 0x1; |
| | 3903 | return 0x2; |
| | 3904 | } |
| | 3905 | switch (header.@"type"()) { |
| | 3906 | macho.S_NON_LAZY_SYMBOL_POINTERS, |
| | 3907 | macho.S_LAZY_SYMBOL_POINTERS, |
| | 3908 | => return 0x0, |
| | 3909 | macho.S_MOD_INIT_FUNC_POINTERS => return 0x1, |
| | 3910 | macho.S_MOD_TERM_FUNC_POINTERS => return 0x2, |
| | 3911 | macho.S_ZEROFILL => return 0xf, |
| | 3912 | macho.S_THREAD_LOCAL_REGULAR => return 0xd, |
| | 3913 | macho.S_THREAD_LOCAL_ZEROFILL => return 0xe, |
| | 3914 | else => if (mem.eql(u8, "__eh_frame", header.sectName())) |
| | 3915 | return 0xf |
| | 3916 | else |
| | 3917 | return 0x3, |
| | 3918 | } |
| | 3919 | } |
| | 3920 | |
| | 3921 | const InitSectionOpts = struct { |
| | 3922 | flags: u32 = macho.S_REGULAR, |
| | 3923 | reserved1: u32 = 0, |
| | 3924 | reserved2: u32 = 0, |
| | 3925 | }; |
| | 3926 | |
| | 3927 | pub fn initSection(self: *MachO, segname: []const u8, sectname: []const u8, opts: InitSectionOpts) !u8 { |
| | 3928 | const segment_id = self.getSegmentByName(segname).?; |
| | 3929 | const seg = &self.segments.items[segment_id]; |
| | 3930 | const index = try self.insertSection(segment_id, .{ |
| | 3931 | .sectname = makeStaticString(sectname), |
| | 3932 | .segname = seg.segname, |
| | 3933 | .flags = opts.flags, |
| | 3934 | .reserved1 = opts.reserved1, |
| | 3935 | .reserved2 = opts.reserved2, |
| | 3936 | }); |
| | 3937 | seg.cmdsize += @sizeOf(macho.section_64); |
| | 3938 | seg.nsects += 1; |
| | 3939 | return index; |
| | 3940 | } |
| | 3941 | |
| | 3942 | fn insertSection(self: *MachO, segment_index: u8, header: macho.section_64) !u8 { |
| | 3943 | const precedence = getSectionPrecedence(header); |
| | 3944 | const indexes = self.getSectionIndexes(segment_index); |
| | 3945 | const insertion_index = for (self.sections.items(.header)[indexes.start..indexes.end]) |hdr, i| { |
| | 3946 | if (getSectionPrecedence(hdr) > precedence) break @intCast(u8, i + indexes.start); |
| | 3947 | } else indexes.end; |
| | 3948 | log.debug("inserting section '{s},{s}' at index {d}", .{ |
| | 3949 | header.segName(), |
| | 3950 | header.sectName(), |
| | 3951 | insertion_index, |
| | 3952 | }); |
| | 3953 | for (&[_]*?u8{ |
| | 3954 | &self.text_section_index, |
| | 3955 | &self.stubs_section_index, |
| | 3956 | &self.stub_helper_section_index, |
| | 3957 | &self.got_section_index, |
| | 3958 | &self.la_symbol_ptr_section_index, |
| | 3959 | &self.data_section_index, |
| | 3960 | }) |maybe_index| { |
| | 3961 | const index = maybe_index.* orelse continue; |
| | 3962 | if (insertion_index <= index) maybe_index.* = index + 1; |
| | 3963 | } |
| | 3964 | try self.sections.insert(self.base.allocator, insertion_index, .{ |
| | 3965 | .segment_index = segment_index, |
| | 3966 | .header = header, |
| | 3967 | }); |
| | 3968 | return insertion_index; |
| | 3969 | } |
| | 3970 | |
| | 3971 | pub fn addAtomToSection(self: *MachO, atom: *Atom) !void { |
| | 3972 | const sect_id = atom.getSymbol(self).n_sect - 1; |
| 4837 | var section = self.sections.get(sect_id); | 3973 | var section = self.sections.get(sect_id); |
| 4838 | if (section.header.size > 0) { | 3974 | if (section.header.size > 0) { |
| 4839 | section.last_atom.?.next = atom; | 3975 | section.last_atom.?.next = atom; |
| ... | @@ -4872,203 +4008,209 @@ pub fn getGlobalSymbol(self: *MachO, name: []const u8) !u32 { | ... | @@ -4872,203 +4008,209 @@ pub fn getGlobalSymbol(self: *MachO, name: []const u8) !u32 { |
| 4872 | return global_index; | 4008 | return global_index; |
| 4873 | } | 4009 | } |
| 4874 | | 4010 | |
| 4875 | fn getSegmentAllocBase(self: MachO, indices: []const ?u8) struct { vmaddr: u64, fileoff: u64 } { | | |
| 4876 | for (indices) |maybe_prev_id| { | | |
| 4877 | const prev_id = maybe_prev_id orelse continue; | | |
| 4878 | const prev = self.segments.items[prev_id]; | | |
| 4879 | return .{ | | |
| 4880 | .vmaddr = prev.vmaddr + prev.vmsize, | | |
| 4881 | .fileoff = prev.fileoff + prev.filesize, | | |
| 4882 | }; | | |
| 4883 | } | | |
| 4884 | return .{ .vmaddr = 0, .fileoff = 0 }; | | |
| 4885 | } | | |
| 4886 | | | |
| 4887 | fn writeSegmentHeaders(self: *MachO, ncmds: *u32, writer: anytype) !void { | 4011 | fn writeSegmentHeaders(self: *MachO, ncmds: *u32, writer: anytype) !void { |
| 4888 | for (self.segments.items) |seg, i| { | 4012 | for (self.segments.items) |seg, i| { |
| 4889 | const indexes = self.getSectionIndexes(@intCast(u8, i)); | 4013 | const indexes = self.getSectionIndexes(@intCast(u8, i)); |
| 4890 | var out_seg = seg; | 4014 | try writer.writeStruct(seg); |
| 4891 | out_seg.cmdsize = @sizeOf(macho.segment_command_64); | | |
| 4892 | out_seg.nsects = 0; | | |
| 4893 | | | |
| 4894 | // Update section headers count; any section with size of 0 is excluded | | |
| 4895 | // since it doesn't have any data in the final binary file. | | |
| 4896 | for (self.sections.items(.header)[indexes.start..indexes.end]) |header| { | | |
| 4897 | if (header.size == 0) continue; | | |
| 4898 | out_seg.cmdsize += @sizeOf(macho.section_64); | | |
| 4899 | out_seg.nsects += 1; | | |
| 4900 | } | | |
| 4901 | | | |
| 4902 | if (out_seg.nsects == 0 and | | |
| 4903 | (mem.eql(u8, out_seg.segName(), "__DATA_CONST") or | | |
| 4904 | mem.eql(u8, out_seg.segName(), "__DATA"))) continue; | | |
| 4905 | | | |
| 4906 | try writer.writeStruct(out_seg); | | |
| 4907 | for (self.sections.items(.header)[indexes.start..indexes.end]) |header| { | 4015 | for (self.sections.items(.header)[indexes.start..indexes.end]) |header| { |
| 4908 | if (header.size == 0) continue; | | |
| 4909 | try writer.writeStruct(header); | 4016 | try writer.writeStruct(header); |
| 4910 | } | 4017 | } |
| 4911 | | | |
| 4912 | ncmds.* += 1; | 4018 | ncmds.* += 1; |
| 4913 | } | 4019 | } |
| 4914 | } | 4020 | } |
| 4915 | | 4021 | |
| 4916 | fn writeLinkeditSegmentData(self: *MachO, ncmds: *u32, lc_writer: anytype) !void { | 4022 | fn writeLinkeditSegmentData(self: *MachO, ncmds: *u32, lc_writer: anytype) !void { |
| 4917 | const seg = &self.segments.items[self.linkedit_segment_cmd_index.?]; | 4023 | const seg = self.getLinkeditSegmentPtr(); |
| 4918 | seg.filesize = 0; | 4024 | seg.filesize = 0; |
| 4919 | seg.vmsize = 0; | 4025 | seg.vmsize = 0; |
| 4920 | | 4026 | |
| | 4027 | for (self.segments.items) |segment, id| { |
| | 4028 | if (self.linkedit_segment_cmd_index.? == @intCast(u8, id)) continue; |
| | 4029 | if (seg.vmaddr < segment.vmaddr + segment.vmsize) { |
| | 4030 | seg.vmaddr = mem.alignForwardGeneric(u64, segment.vmaddr + segment.vmsize, self.page_size); |
| | 4031 | } |
| | 4032 | if (seg.fileoff < segment.fileoff + segment.filesize) { |
| | 4033 | seg.fileoff = mem.alignForwardGeneric(u64, segment.fileoff + segment.filesize, self.page_size); |
| | 4034 | } |
| | 4035 | } |
| | 4036 | |
| 4921 | try self.writeDyldInfoData(ncmds, lc_writer); | 4037 | try self.writeDyldInfoData(ncmds, lc_writer); |
| 4922 | try self.writeFunctionStarts(ncmds, lc_writer); | | |
| 4923 | try self.writeDataInCode(ncmds, lc_writer); | | |
| 4924 | try self.writeSymtabs(ncmds, lc_writer); | 4038 | try self.writeSymtabs(ncmds, lc_writer); |
| 4925 | | 4039 | |
| 4926 | seg.vmsize = mem.alignForwardGeneric(u64, seg.filesize, self.page_size); | 4040 | seg.vmsize = mem.alignForwardGeneric(u64, seg.filesize, self.page_size); |
| 4927 | } | 4041 | } |
| 4928 | | 4042 | |
| 4929 | fn writeDyldInfoData(self: *MachO, ncmds: *u32, lc_writer: anytype) !void { | 4043 | const AtomLessThanByAddressContext = struct { |
| 4930 | const tracy = trace(@src()); | 4044 | macho_file: *MachO, |
| 4931 | defer tracy.end(); | 4045 | }; |
| | 4046 | |
| | 4047 | fn atomLessThanByAddress(ctx: AtomLessThanByAddressContext, lhs: *Atom, rhs: *Atom) bool { |
| | 4048 | return lhs.getSymbol(ctx.macho_file).n_value < rhs.getSymbol(ctx.macho_file).n_value; |
| | 4049 | } |
| 4932 | | 4050 | |
| | 4051 | fn collectRebaseData(self: *MachO, pointers: *std.ArrayList(bind.Pointer)) !void { |
| 4933 | const gpa = self.base.allocator; | 4052 | const gpa = self.base.allocator; |
| 4934 | | 4053 | |
| 4935 | var rebase_pointers = std.ArrayList(bind.Pointer).init(gpa); | 4054 | var sorted_atoms_by_address = std.ArrayList(*Atom).init(gpa); |
| 4936 | defer rebase_pointers.deinit(); | 4055 | defer sorted_atoms_by_address.deinit(); |
| 4937 | var bind_pointers = std.ArrayList(bind.Pointer).init(gpa); | 4056 | try sorted_atoms_by_address.ensureTotalCapacityPrecise(self.rebases.count()); |
| 4938 | defer bind_pointers.deinit(); | 4057 | |
| 4939 | var lazy_bind_pointers = std.ArrayList(bind.Pointer).init(gpa); | 4058 | var it = self.rebases.keyIterator(); |
| 4940 | defer lazy_bind_pointers.deinit(); | 4059 | while (it.next()) |key_ptr| { |
| | 4060 | sorted_atoms_by_address.appendAssumeCapacity(key_ptr.*); |
| | 4061 | } |
| | 4062 | |
| | 4063 | std.sort.sort(*Atom, sorted_atoms_by_address.items, AtomLessThanByAddressContext{ |
| | 4064 | .macho_file = self, |
| | 4065 | }, atomLessThanByAddress); |
| 4941 | | 4066 | |
| 4942 | const slice = self.sections.slice(); | 4067 | const slice = self.sections.slice(); |
| 4943 | for (slice.items(.last_atom)) |last_atom, sect_id| { | 4068 | for (sorted_atoms_by_address.items) |atom| { |
| 4944 | var atom = last_atom orelse continue; | 4069 | log.debug(" ATOM(%{d}, '{s}')", .{ atom.sym_index, atom.getName(self) }); |
| 4945 | const segment_index = slice.items(.segment_index)[sect_id]; | | |
| 4946 | const header = slice.items(.header)[sect_id]; | | |
| 4947 | | 4070 | |
| 4948 | if (mem.eql(u8, header.segName(), "__TEXT")) continue; // __TEXT is non-writable | 4071 | const sym = atom.getSymbol(self); |
| | 4072 | const segment_index = slice.items(.segment_index)[sym.n_sect - 1]; |
| | 4073 | const seg = self.getSegment(sym.n_sect - 1); |
| 4949 | | 4074 | |
| 4950 | log.debug("dyld info for {s},{s}", .{ header.segName(), header.sectName() }); | 4075 | const base_offset = sym.n_value - seg.vmaddr; |
| 4951 | | 4076 | |
| 4952 | const seg = self.segments.items[segment_index]; | 4077 | const rebases = self.rebases.get(atom).?; |
| | 4078 | try pointers.ensureUnusedCapacity(rebases.items.len); |
| | 4079 | for (rebases.items) |offset| { |
| | 4080 | log.debug(" | rebase at {x}", .{base_offset + offset}); |
| 4953 | | 4081 | |
| 4954 | while (true) { | 4082 | pointers.appendAssumeCapacity(.{ |
| 4955 | log.debug(" ATOM(%{d}, '{s}')", .{ atom.sym_index, atom.getName(self) }); | 4083 | .offset = base_offset + offset, |
| 4956 | const sym = atom.getSymbol(self); | 4084 | .segment_id = segment_index, |
| 4957 | const base_offset = sym.n_value - seg.vmaddr; | 4085 | }); |
| 4958 | | 4086 | } |
| 4959 | for (atom.rebases.items) |offset| { | 4087 | } |
| 4960 | log.debug(" | rebase at {x}", .{base_offset + offset}); | 4088 | } |
| 4961 | try rebase_pointers.append(.{ | | |
| 4962 | .offset = base_offset + offset, | | |
| 4963 | .segment_id = segment_index, | | |
| 4964 | }); | | |
| 4965 | } | | |
| 4966 | | 4089 | |
| 4967 | for (atom.bindings.items) |binding| { | 4090 | fn collectBindData(self: *MachO, pointers: *std.ArrayList(bind.Pointer), raw_bindings: anytype) !void { |
| 4968 | const bind_sym = self.getSymbol(binding.target); | 4091 | const gpa = self.base.allocator; |
| 4969 | const bind_sym_name = self.getSymbolName(binding.target); | | |
| 4970 | const dylib_ordinal = @divTrunc( | | |
| 4971 | @bitCast(i16, bind_sym.n_desc), | | |
| 4972 | macho.N_SYMBOL_RESOLVER, | | |
| 4973 | ); | | |
| 4974 | var flags: u4 = 0; | | |
| 4975 | log.debug(" | bind at {x}, import('{s}') in dylib({d})", .{ | | |
| 4976 | binding.offset + base_offset, | | |
| 4977 | bind_sym_name, | | |
| 4978 | dylib_ordinal, | | |
| 4979 | }); | | |
| 4980 | if (bind_sym.weakRef()) { | | |
| 4981 | log.debug(" | marking as weak ref ", .{}); | | |
| 4982 | flags |= @truncate(u4, macho.BIND_SYMBOL_FLAGS_WEAK_IMPORT); | | |
| 4983 | } | | |
| 4984 | try bind_pointers.append(.{ | | |
| 4985 | .offset = binding.offset + base_offset, | | |
| 4986 | .segment_id = segment_index, | | |
| 4987 | .dylib_ordinal = dylib_ordinal, | | |
| 4988 | .name = bind_sym_name, | | |
| 4989 | .bind_flags = flags, | | |
| 4990 | }); | | |
| 4991 | } | | |
| 4992 | | 4092 | |
| 4993 | for (atom.lazy_bindings.items) |binding| { | 4093 | var sorted_atoms_by_address = std.ArrayList(*Atom).init(gpa); |
| 4994 | const bind_sym = self.getSymbol(binding.target); | 4094 | defer sorted_atoms_by_address.deinit(); |
| 4995 | const bind_sym_name = self.getSymbolName(binding.target); | 4095 | try sorted_atoms_by_address.ensureTotalCapacityPrecise(raw_bindings.count()); |
| 4996 | const dylib_ordinal = @divTrunc( | | |
| 4997 | @bitCast(i16, bind_sym.n_desc), | | |
| 4998 | macho.N_SYMBOL_RESOLVER, | | |
| 4999 | ); | | |
| 5000 | var flags: u4 = 0; | | |
| 5001 | log.debug(" | lazy bind at {x} import('{s}') ord({d})", .{ | | |
| 5002 | binding.offset + base_offset, | | |
| 5003 | bind_sym_name, | | |
| 5004 | dylib_ordinal, | | |
| 5005 | }); | | |
| 5006 | if (bind_sym.weakRef()) { | | |
| 5007 | log.debug(" | marking as weak ref ", .{}); | | |
| 5008 | flags |= @truncate(u4, macho.BIND_SYMBOL_FLAGS_WEAK_IMPORT); | | |
| 5009 | } | | |
| 5010 | try lazy_bind_pointers.append(.{ | | |
| 5011 | .offset = binding.offset + base_offset, | | |
| 5012 | .segment_id = segment_index, | | |
| 5013 | .dylib_ordinal = dylib_ordinal, | | |
| 5014 | .name = bind_sym_name, | | |
| 5015 | .bind_flags = flags, | | |
| 5016 | }); | | |
| 5017 | } | | |
| 5018 | | 4096 | |
| 5019 | if (atom.prev) |prev| { | 4097 | var it = raw_bindings.keyIterator(); |
| 5020 | atom = prev; | 4098 | while (it.next()) |key_ptr| { |
| 5021 | } else break; | 4099 | sorted_atoms_by_address.appendAssumeCapacity(key_ptr.*); |
| 5022 | } | | |
| 5023 | } | 4100 | } |
| 5024 | | 4101 | |
| 5025 | var trie: Trie = .{}; | 4102 | std.sort.sort(*Atom, sorted_atoms_by_address.items, AtomLessThanByAddressContext{ |
| 5026 | defer trie.deinit(gpa); | 4103 | .macho_file = self, |
| | 4104 | }, atomLessThanByAddress); |
| 5027 | | 4105 | |
| 5028 | { | 4106 | const slice = self.sections.slice(); |
| 5029 | // TODO handle macho.EXPORT_SYMBOL_FLAGS_REEXPORT and macho.EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER. | 4107 | for (sorted_atoms_by_address.items) |atom| { |
| 5030 | log.debug("generating export trie", .{}); | 4108 | log.debug(" ATOM(%{d}, '{s}')", .{ atom.sym_index, atom.getName(self) }); |
| 5031 | | 4109 | |
| 5032 | const text_segment = self.segments.items[self.text_segment_cmd_index.?]; | 4110 | const sym = atom.getSymbol(self); |
| 5033 | const base_address = text_segment.vmaddr; | 4111 | const segment_index = slice.items(.segment_index)[sym.n_sect - 1]; |
| 5034 | | 4112 | const seg = self.getSegment(sym.n_sect - 1); |
| 5035 | if (self.base.options.output_mode == .Exe) { | 4113 | |
| 5036 | for (&[_]SymbolWithLoc{ | 4114 | const base_offset = sym.n_value - seg.vmaddr; |
| 5037 | try self.getEntryPoint(), | 4115 | |
| 5038 | self.getGlobal("__mh_execute_header").?, | 4116 | const bindings = raw_bindings.get(atom).?; |
| 5039 | }) |global| { | 4117 | try pointers.ensureUnusedCapacity(bindings.items.len); |
| 5040 | const sym = self.getSymbol(global); | 4118 | for (bindings.items) |binding| { |
| 5041 | const sym_name = self.getSymbolName(global); | 4119 | const bind_sym = self.getSymbol(binding.target); |
| 5042 | log.debug(" (putting '{s}' defined at 0x{x})", .{ sym_name, sym.n_value }); | 4120 | const bind_sym_name = self.getSymbolName(binding.target); |
| 5043 | try trie.put(gpa, .{ | 4121 | const dylib_ordinal = @divTrunc( |
| 5044 | .name = sym_name, | 4122 | @bitCast(i16, bind_sym.n_desc), |
| 5045 | .vmaddr_offset = sym.n_value - base_address, | 4123 | macho.N_SYMBOL_RESOLVER, |
| 5046 | .export_flags = macho.EXPORT_SYMBOL_FLAGS_KIND_REGULAR, | 4124 | ); |
| 5047 | }); | 4125 | var flags: u4 = 0; |
| 5048 | } | 4126 | log.debug(" | bind at {x}, import('{s}') in dylib({d})", .{ |
| 5049 | } else { | 4127 | binding.offset + base_offset, |
| 5050 | assert(self.base.options.output_mode == .Lib); | 4128 | bind_sym_name, |
| 5051 | for (self.globals.items) |global| { | 4129 | dylib_ordinal, |
| 5052 | const sym = self.getSymbol(global); | 4130 | }); |
| 5053 | | 4131 | if (bind_sym.weakRef()) { |
| 5054 | if (sym.undf()) continue; | 4132 | log.debug(" | marking as weak ref ", .{}); |
| 5055 | if (!sym.ext()) continue; | 4133 | flags |= @truncate(u4, macho.BIND_SYMBOL_FLAGS_WEAK_IMPORT); |
| 5056 | if (sym.n_desc == N_DESC_GCED) continue; | | |
| 5057 | | | |
| 5058 | const sym_name = self.getSymbolName(global); | | |
| 5059 | log.debug(" (putting '{s}' defined at 0x{x})", .{ sym_name, sym.n_value }); | | |
| 5060 | try trie.put(gpa, .{ | | |
| 5061 | .name = sym_name, | | |
| 5062 | .vmaddr_offset = sym.n_value - base_address, | | |
| 5063 | .export_flags = macho.EXPORT_SYMBOL_FLAGS_KIND_REGULAR, | | |
| 5064 | }); | | |
| 5065 | } | 4134 | } |
| | 4135 | pointers.appendAssumeCapacity(.{ |
| | 4136 | .offset = binding.offset + base_offset, |
| | 4137 | .segment_id = segment_index, |
| | 4138 | .dylib_ordinal = dylib_ordinal, |
| | 4139 | .name = bind_sym_name, |
| | 4140 | .bind_flags = flags, |
| | 4141 | }); |
| | 4142 | } |
| | 4143 | } |
| | 4144 | } |
| | 4145 | |
| | 4146 | fn collectExportData(self: *MachO, trie: *Trie) !void { |
| | 4147 | const gpa = self.base.allocator; |
| | 4148 | |
| | 4149 | // TODO handle macho.EXPORT_SYMBOL_FLAGS_REEXPORT and macho.EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER. |
| | 4150 | log.debug("generating export trie", .{}); |
| | 4151 | |
| | 4152 | const exec_segment = self.segments.items[self.header_segment_cmd_index.?]; |
| | 4153 | const base_address = exec_segment.vmaddr; |
| | 4154 | |
| | 4155 | if (self.base.options.output_mode == .Exe) { |
| | 4156 | for (&[_]SymbolWithLoc{ |
| | 4157 | try self.getEntryPoint(), |
| | 4158 | self.getGlobal("__mh_execute_header").?, |
| | 4159 | }) |global| { |
| | 4160 | const sym = self.getSymbol(global); |
| | 4161 | const sym_name = self.getSymbolName(global); |
| | 4162 | log.debug(" (putting '{s}' defined at 0x{x})", .{ sym_name, sym.n_value }); |
| | 4163 | try trie.put(gpa, .{ |
| | 4164 | .name = sym_name, |
| | 4165 | .vmaddr_offset = sym.n_value - base_address, |
| | 4166 | .export_flags = macho.EXPORT_SYMBOL_FLAGS_KIND_REGULAR, |
| | 4167 | }); |
| 5066 | } | 4168 | } |
| | 4169 | } else { |
| | 4170 | assert(self.base.options.output_mode == .Lib); |
| | 4171 | for (self.globals.items) |global| { |
| | 4172 | const sym = self.getSymbol(global); |
| | 4173 | |
| | 4174 | if (sym.undf()) continue; |
| | 4175 | if (!sym.ext()) continue; |
| | 4176 | if (sym.n_desc == N_DESC_GCED) continue; |
| 5067 | | 4177 | |
| 5068 | try trie.finalize(gpa); | 4178 | const sym_name = self.getSymbolName(global); |
| | 4179 | log.debug(" (putting '{s}' defined at 0x{x})", .{ sym_name, sym.n_value }); |
| | 4180 | try trie.put(gpa, .{ |
| | 4181 | .name = sym_name, |
| | 4182 | .vmaddr_offset = sym.n_value - base_address, |
| | 4183 | .export_flags = macho.EXPORT_SYMBOL_FLAGS_KIND_REGULAR, |
| | 4184 | }); |
| | 4185 | } |
| 5069 | } | 4186 | } |
| 5070 | | 4187 | |
| 5071 | const link_seg = &self.segments.items[self.linkedit_segment_cmd_index.?]; | 4188 | try trie.finalize(gpa); |
| | 4189 | } |
| | 4190 | |
| | 4191 | fn writeDyldInfoData(self: *MachO, ncmds: *u32, lc_writer: anytype) !void { |
| | 4192 | const tracy = trace(@src()); |
| | 4193 | defer tracy.end(); |
| | 4194 | |
| | 4195 | const gpa = self.base.allocator; |
| | 4196 | |
| | 4197 | var rebase_pointers = std.ArrayList(bind.Pointer).init(gpa); |
| | 4198 | defer rebase_pointers.deinit(); |
| | 4199 | try self.collectRebaseData(&rebase_pointers); |
| | 4200 | |
| | 4201 | var bind_pointers = std.ArrayList(bind.Pointer).init(gpa); |
| | 4202 | defer bind_pointers.deinit(); |
| | 4203 | try self.collectBindData(&bind_pointers, self.bindings); |
| | 4204 | |
| | 4205 | var lazy_bind_pointers = std.ArrayList(bind.Pointer).init(gpa); |
| | 4206 | defer lazy_bind_pointers.deinit(); |
| | 4207 | try self.collectBindData(&lazy_bind_pointers, self.lazy_bindings); |
| | 4208 | |
| | 4209 | var trie: Trie = .{}; |
| | 4210 | defer trie.deinit(gpa); |
| | 4211 | try self.collectExportData(&trie); |
| | 4212 | |
| | 4213 | const link_seg = self.getLinkeditSegmentPtr(); |
| 5072 | const rebase_off = mem.alignForwardGeneric(u64, link_seg.fileoff, @alignOf(u64)); | 4214 | const rebase_off = mem.alignForwardGeneric(u64, link_seg.fileoff, @alignOf(u64)); |
| 5073 | assert(rebase_off == link_seg.fileoff); | 4215 | assert(rebase_off == link_seg.fileoff); |
| 5074 | const rebase_size = try bind.rebaseInfoSize(rebase_pointers.items); | 4216 | const rebase_size = try bind.rebaseInfoSize(rebase_pointers.items); |
| ... | @@ -5150,10 +4292,7 @@ fn populateLazyBindOffsetsInStubHelper(self: *MachO, buffer: []const u8) !void { | ... | @@ -5150,10 +4292,7 @@ fn populateLazyBindOffsetsInStubHelper(self: *MachO, buffer: []const u8) !void { |
| 5150 | { | 4292 | { |
| 5151 | var stub_atom = last_atom; | 4293 | var stub_atom = last_atom; |
| 5152 | var laptr_atom = self.sections.items(.last_atom)[self.la_symbol_ptr_section_index.?].?; | 4294 | var laptr_atom = self.sections.items(.last_atom)[self.la_symbol_ptr_section_index.?].?; |
| 5153 | const base_addr = blk: { | 4295 | const base_addr = self.getSegment(self.la_symbol_ptr_section_index.?).vmaddr; |
| 5154 | const seg = self.segments.items[self.data_segment_cmd_index.?]; | | |
| 5155 | break :blk seg.vmaddr; | | |
| 5156 | }; | | |
| 5157 | | 4296 | |
| 5158 | while (true) { | 4297 | while (true) { |
| 5159 | const laptr_off = blk: { | 4298 | const laptr_off = blk: { |
| ... | @@ -5236,154 +4375,6 @@ fn populateLazyBindOffsetsInStubHelper(self: *MachO, buffer: []const u8) !void { | ... | @@ -5236,154 +4375,6 @@ fn populateLazyBindOffsetsInStubHelper(self: *MachO, buffer: []const u8) !void { |
| 5236 | } | 4375 | } |
| 5237 | } | 4376 | } |
| 5238 | | 4377 | |
| 5239 | const asc_u64 = std.sort.asc(u64); | | |
| 5240 | | | |
| 5241 | fn writeFunctionStarts(self: *MachO, ncmds: *u32, lc_writer: anytype) !void { | | |
| 5242 | const tracy = trace(@src()); | | |
| 5243 | defer tracy.end(); | | |
| 5244 | | | |
| 5245 | const text_seg_index = self.text_segment_cmd_index orelse return; | | |
| 5246 | const text_sect_index = self.text_section_index orelse return; | | |
| 5247 | const text_seg = self.segments.items[text_seg_index]; | | |
| 5248 | | | |
| 5249 | const gpa = self.base.allocator; | | |
| 5250 | | | |
| 5251 | // We need to sort by address first | | |
| 5252 | var addresses = std.ArrayList(u64).init(gpa); | | |
| 5253 | defer addresses.deinit(); | | |
| 5254 | try addresses.ensureTotalCapacityPrecise(self.globals.items.len); | | |
| 5255 | | | |
| 5256 | for (self.globals.items) |global| { | | |
| 5257 | const sym = self.getSymbol(global); | | |
| 5258 | if (sym.undf()) continue; | | |
| 5259 | if (sym.n_desc == N_DESC_GCED) continue; | | |
| 5260 | const sect_id = sym.n_sect - 1; | | |
| 5261 | if (sect_id != text_sect_index) continue; | | |
| 5262 | | | |
| 5263 | addresses.appendAssumeCapacity(sym.n_value); | | |
| 5264 | } | | |
| 5265 | | | |
| 5266 | std.sort.sort(u64, addresses.items, {}, asc_u64); | | |
| 5267 | | | |
| 5268 | var offsets = std.ArrayList(u32).init(gpa); | | |
| 5269 | defer offsets.deinit(); | | |
| 5270 | try offsets.ensureTotalCapacityPrecise(addresses.items.len); | | |
| 5271 | | | |
| 5272 | var last_off: u32 = 0; | | |
| 5273 | for (addresses.items) |addr| { | | |
| 5274 | const offset = @intCast(u32, addr - text_seg.vmaddr); | | |
| 5275 | const diff = offset - last_off; | | |
| 5276 | | | |
| 5277 | if (diff == 0) continue; | | |
| 5278 | | | |
| 5279 | offsets.appendAssumeCapacity(diff); | | |
| 5280 | last_off = offset; | | |
| 5281 | } | | |
| 5282 | | | |
| 5283 | var buffer = std.ArrayList(u8).init(gpa); | | |
| 5284 | defer buffer.deinit(); | | |
| 5285 | | | |
| 5286 | const max_size = @intCast(usize, offsets.items.len * @sizeOf(u64)); | | |
| 5287 | try buffer.ensureTotalCapacity(max_size); | | |
| 5288 | | | |
| 5289 | for (offsets.items) |offset| { | | |
| 5290 | try std.leb.writeULEB128(buffer.writer(), offset); | | |
| 5291 | } | | |
| 5292 | | | |
| 5293 | const link_seg = &self.segments.items[self.linkedit_segment_cmd_index.?]; | | |
| 5294 | const offset = mem.alignForwardGeneric(u64, link_seg.fileoff + link_seg.filesize, @alignOf(u64)); | | |
| 5295 | const needed_size = buffer.items.len; | | |
| 5296 | link_seg.filesize = offset + needed_size - link_seg.fileoff; | | |
| 5297 | | | |
| 5298 | log.debug("writing function starts info from 0x{x} to 0x{x}", .{ offset, offset + needed_size }); | | |
| 5299 | | | |
| 5300 | try self.base.file.?.pwriteAll(buffer.items, offset); | | |
| 5301 | | | |
| 5302 | try lc_writer.writeStruct(macho.linkedit_data_command{ | | |
| 5303 | .cmd = .FUNCTION_STARTS, | | |
| 5304 | .cmdsize = @sizeOf(macho.linkedit_data_command), | | |
| 5305 | .dataoff = @intCast(u32, offset), | | |
| 5306 | .datasize = @intCast(u32, needed_size), | | |
| 5307 | }); | | |
| 5308 | ncmds.* += 1; | | |
| 5309 | } | | |
| 5310 | | | |
| 5311 | fn filterDataInCode( | | |
| 5312 | dices: []align(1) const macho.data_in_code_entry, | | |
| 5313 | start_addr: u64, | | |
| 5314 | end_addr: u64, | | |
| 5315 | ) []align(1) const macho.data_in_code_entry { | | |
| 5316 | const Predicate = struct { | | |
| 5317 | addr: u64, | | |
| 5318 | | | |
| 5319 | pub fn predicate(self: @This(), dice: macho.data_in_code_entry) bool { | | |
| 5320 | return dice.offset >= self.addr; | | |
| 5321 | } | | |
| 5322 | }; | | |
| 5323 | | | |
| 5324 | const start = MachO.findFirst(macho.data_in_code_entry, dices, 0, Predicate{ .addr = start_addr }); | | |
| 5325 | const end = MachO.findFirst(macho.data_in_code_entry, dices, start, Predicate{ .addr = end_addr }); | | |
| 5326 | | | |
| 5327 | return dices[start..end]; | | |
| 5328 | } | | |
| 5329 | | | |
| 5330 | fn writeDataInCode(self: *MachO, ncmds: *u32, lc_writer: anytype) !void { | | |
| 5331 | const tracy = trace(@src()); | | |
| 5332 | defer tracy.end(); | | |
| 5333 | | | |
| 5334 | var out_dice = std.ArrayList(macho.data_in_code_entry).init(self.base.allocator); | | |
| 5335 | defer out_dice.deinit(); | | |
| 5336 | | | |
| 5337 | const text_sect_id = self.text_section_index orelse return; | | |
| 5338 | const text_sect_header = self.sections.items(.header)[text_sect_id]; | | |
| 5339 | | | |
| 5340 | for (self.objects.items) |object| { | | |
| 5341 | const dice = object.parseDataInCode() orelse continue; | | |
| 5342 | try out_dice.ensureUnusedCapacity(dice.len); | | |
| 5343 | | | |
| 5344 | for (object.managed_atoms.items) |atom| { | | |
| 5345 | const sym = atom.getSymbol(self); | | |
| 5346 | if (sym.n_desc == N_DESC_GCED) continue; | | |
| 5347 | | | |
| 5348 | const sect_id = sym.n_sect - 1; | | |
| 5349 | if (sect_id != self.text_section_index.?) { | | |
| 5350 | continue; | | |
| 5351 | } | | |
| 5352 | | | |
| 5353 | const source_sym = object.getSourceSymbol(atom.sym_index) orelse continue; | | |
| 5354 | const source_addr = math.cast(u32, source_sym.n_value) orelse return error.Overflow; | | |
| 5355 | const filtered_dice = filterDataInCode(dice, source_addr, source_addr + atom.size); | | |
| 5356 | const base = math.cast(u32, sym.n_value - text_sect_header.addr + text_sect_header.offset) orelse | | |
| 5357 | return error.Overflow; | | |
| 5358 | | | |
| 5359 | for (filtered_dice) |single| { | | |
| 5360 | const offset = single.offset - source_addr + base; | | |
| 5361 | out_dice.appendAssumeCapacity(.{ | | |
| 5362 | .offset = offset, | | |
| 5363 | .length = single.length, | | |
| 5364 | .kind = single.kind, | | |
| 5365 | }); | | |
| 5366 | } | | |
| 5367 | } | | |
| 5368 | } | | |
| 5369 | | | |
| 5370 | const seg = &self.segments.items[self.linkedit_segment_cmd_index.?]; | | |
| 5371 | const offset = mem.alignForwardGeneric(u64, seg.fileoff + seg.filesize, @alignOf(u64)); | | |
| 5372 | const needed_size = out_dice.items.len * @sizeOf(macho.data_in_code_entry); | | |
| 5373 | seg.filesize = offset + needed_size - seg.fileoff; | | |
| 5374 | | | |
| 5375 | log.debug("writing data-in-code from 0x{x} to 0x{x}", .{ offset, offset + needed_size }); | | |
| 5376 | | | |
| 5377 | try self.base.file.?.pwriteAll(mem.sliceAsBytes(out_dice.items), offset); | | |
| 5378 | try lc_writer.writeStruct(macho.linkedit_data_command{ | | |
| 5379 | .cmd = .DATA_IN_CODE, | | |
| 5380 | .cmdsize = @sizeOf(macho.linkedit_data_command), | | |
| 5381 | .dataoff = @intCast(u32, offset), | | |
| 5382 | .datasize = @intCast(u32, needed_size), | | |
| 5383 | }); | | |
| 5384 | ncmds.* += 1; | | |
| 5385 | } | | |
| 5386 | | | |
| 5387 | fn writeSymtabs(self: *MachO, ncmds: *u32, lc_writer: anytype) !void { | 4378 | fn writeSymtabs(self: *MachO, ncmds: *u32, lc_writer: anytype) !void { |
| 5388 | var symtab_cmd = macho.symtab_command{ | 4379 | var symtab_cmd = macho.symtab_command{ |
| 5389 | .cmdsize = @sizeOf(macho.symtab_command), | 4380 | .cmdsize = @sizeOf(macho.symtab_command), |
| ... | @@ -5448,10 +4439,6 @@ fn writeSymtab(self: *MachO, lc: *macho.symtab_command) !SymtabCtx { | ... | @@ -5448,10 +4439,6 @@ fn writeSymtab(self: *MachO, lc: *macho.symtab_command) !SymtabCtx { |
| 5448 | out_sym.n_strx = try self.strtab.insert(gpa, self.getSymbolName(sym_loc)); | 4439 | out_sym.n_strx = try self.strtab.insert(gpa, self.getSymbolName(sym_loc)); |
| 5449 | try locals.append(out_sym); | 4440 | try locals.append(out_sym); |
| 5450 | } | 4441 | } |
| 5451 | | | |
| 5452 | if (!self.base.options.strip) { | | |
| 5453 | try self.generateSymbolStabs(object, &locals); | | |
| 5454 | } | | |
| 5455 | } | 4442 | } |
| 5456 | | 4443 | |
| 5457 | var exports = std.ArrayList(macho.nlist_64).init(gpa); | 4444 | var exports = std.ArrayList(macho.nlist_64).init(gpa); |
| ... | @@ -5487,7 +4474,7 @@ fn writeSymtab(self: *MachO, lc: *macho.symtab_command) !SymtabCtx { | ... | @@ -5487,7 +4474,7 @@ fn writeSymtab(self: *MachO, lc: *macho.symtab_command) !SymtabCtx { |
| 5487 | const nimports = @intCast(u32, imports.items.len); | 4474 | const nimports = @intCast(u32, imports.items.len); |
| 5488 | const nsyms = nlocals + nexports + nimports; | 4475 | const nsyms = nlocals + nexports + nimports; |
| 5489 | | 4476 | |
| 5490 | const seg = &self.segments.items[self.linkedit_segment_cmd_index.?]; | 4477 | const seg = self.getLinkeditSegmentPtr(); |
| 5491 | const offset = mem.alignForwardGeneric( | 4478 | const offset = mem.alignForwardGeneric( |
| 5492 | u64, | 4479 | u64, |
| 5493 | seg.fileoff + seg.filesize, | 4480 | seg.fileoff + seg.filesize, |
| ... | @@ -5518,7 +4505,7 @@ fn writeSymtab(self: *MachO, lc: *macho.symtab_command) !SymtabCtx { | ... | @@ -5518,7 +4505,7 @@ fn writeSymtab(self: *MachO, lc: *macho.symtab_command) !SymtabCtx { |
| 5518 | } | 4505 | } |
| 5519 | | 4506 | |
| 5520 | fn writeStrtab(self: *MachO, lc: *macho.symtab_command) !void { | 4507 | fn writeStrtab(self: *MachO, lc: *macho.symtab_command) !void { |
| 5521 | const seg = &self.segments.items[self.linkedit_segment_cmd_index.?]; | 4508 | const seg = self.getLinkeditSegmentPtr(); |
| 5522 | const offset = mem.alignForwardGeneric(u64, seg.fileoff + seg.filesize, @alignOf(u64)); | 4509 | const offset = mem.alignForwardGeneric(u64, seg.fileoff + seg.filesize, @alignOf(u64)); |
| 5523 | const needed_size = self.strtab.buffer.items.len; | 4510 | const needed_size = self.strtab.buffer.items.len; |
| 5524 | seg.filesize = offset + needed_size - seg.fileoff; | 4511 | seg.filesize = offset + needed_size - seg.fileoff; |
| ... | @@ -5546,7 +4533,7 @@ fn writeDysymtab(self: *MachO, ctx: SymtabCtx, lc: *macho.dysymtab_command) !voi | ... | @@ -5546,7 +4533,7 @@ fn writeDysymtab(self: *MachO, ctx: SymtabCtx, lc: *macho.dysymtab_command) !voi |
| 5546 | const iextdefsym = ctx.nlocalsym; | 4533 | const iextdefsym = ctx.nlocalsym; |
| 5547 | const iundefsym = iextdefsym + ctx.nextdefsym; | 4534 | const iundefsym = iextdefsym + ctx.nextdefsym; |
| 5548 | | 4535 | |
| 5549 | const seg = &self.segments.items[self.linkedit_segment_cmd_index.?]; | 4536 | const seg = self.getLinkeditSegmentPtr(); |
| 5550 | const offset = mem.alignForwardGeneric(u64, seg.fileoff + seg.filesize, @alignOf(u64)); | 4537 | const offset = mem.alignForwardGeneric(u64, seg.fileoff + seg.filesize, @alignOf(u64)); |
| 5551 | const needed_size = nindirectsyms * @sizeOf(u32); | 4538 | const needed_size = nindirectsyms * @sizeOf(u32); |
| 5552 | seg.filesize = offset + needed_size - seg.fileoff; | 4539 | seg.filesize = offset + needed_size - seg.fileoff; |
| ... | @@ -5618,7 +4605,7 @@ fn writeCodeSignaturePadding( | ... | @@ -5618,7 +4605,7 @@ fn writeCodeSignaturePadding( |
| 5618 | ncmds: *u32, | 4605 | ncmds: *u32, |
| 5619 | lc_writer: anytype, | 4606 | lc_writer: anytype, |
| 5620 | ) !u32 { | 4607 | ) !u32 { |
| 5621 | const seg = &self.segments.items[self.linkedit_segment_cmd_index.?]; | 4608 | const seg = self.getLinkeditSegmentPtr(); |
| 5622 | // Code signature data has to be 16-bytes aligned for Apple tools to recognize the file | 4609 | // Code signature data has to be 16-bytes aligned for Apple tools to recognize the file |
| 5623 | // https://github.com/opensource-apple/cctools/blob/fdb4825f303fd5c0751be524babd32958181b3ed/libstuff/checkout.c#L271 | 4610 | // https://github.com/opensource-apple/cctools/blob/fdb4825f303fd5c0751be524babd32958181b3ed/libstuff/checkout.c#L271 |
| 5624 | const offset = mem.alignForwardGeneric(u64, seg.fileoff + seg.filesize, 16); | 4611 | const offset = mem.alignForwardGeneric(u64, seg.fileoff + seg.filesize, 16); |
| ... | @@ -5642,7 +4629,7 @@ fn writeCodeSignaturePadding( | ... | @@ -5642,7 +4629,7 @@ fn writeCodeSignaturePadding( |
| 5642 | } | 4629 | } |
| 5643 | | 4630 | |
| 5644 | fn writeCodeSignature(self: *MachO, code_sig: *CodeSignature, offset: u32) !void { | 4631 | fn writeCodeSignature(self: *MachO, code_sig: *CodeSignature, offset: u32) !void { |
| 5645 | const seg = self.segments.items[self.text_segment_cmd_index.?]; | 4632 | const seg = self.getSegment(self.text_section_index.?); |
| 5646 | | 4633 | |
| 5647 | var buffer = std.ArrayList(u8).init(self.base.allocator); | 4634 | var buffer = std.ArrayList(u8).init(self.base.allocator); |
| 5648 | defer buffer.deinit(); | 4635 | defer buffer.deinit(); |
| ... | @@ -5713,6 +4700,57 @@ pub fn padToIdeal(actual_size: anytype) @TypeOf(actual_size) { | ... | @@ -5713,6 +4700,57 @@ pub fn padToIdeal(actual_size: anytype) @TypeOf(actual_size) { |
| 5713 | std.math.maxInt(@TypeOf(actual_size)); | 4700 | std.math.maxInt(@TypeOf(actual_size)); |
| 5714 | } | 4701 | } |
| 5715 | | 4702 | |
| | 4703 | fn detectAllocCollision(self: *MachO, start: u64, size: u64) ?u64 { |
| | 4704 | // TODO: header and load commands have to be part of the __TEXT segment |
| | 4705 | const header_size = self.segments.items[self.header_segment_cmd_index.?].filesize; |
| | 4706 | if (start < header_size) |
| | 4707 | return header_size; |
| | 4708 | |
| | 4709 | const end = start + padToIdeal(size); |
| | 4710 | |
| | 4711 | for (self.sections.items(.header)) |header| { |
| | 4712 | const tight_size = header.size; |
| | 4713 | const increased_size = padToIdeal(tight_size); |
| | 4714 | const test_end = header.offset + increased_size; |
| | 4715 | if (end > header.offset and start < test_end) { |
| | 4716 | return test_end; |
| | 4717 | } |
| | 4718 | } |
| | 4719 | |
| | 4720 | return null; |
| | 4721 | } |
| | 4722 | |
| | 4723 | fn allocatedSize(self: *MachO, start: u64) u64 { |
| | 4724 | if (start == 0) |
| | 4725 | return 0; |
| | 4726 | var min_pos: u64 = std.math.maxInt(u64); |
| | 4727 | for (self.sections.items(.header)) |header| { |
| | 4728 | if (header.offset <= start) continue; |
| | 4729 | if (header.offset < min_pos) min_pos = header.offset; |
| | 4730 | } |
| | 4731 | return min_pos - start; |
| | 4732 | } |
| | 4733 | |
| | 4734 | fn findFreeSpace(self: *MachO, object_size: u64, min_alignment: u32) u64 { |
| | 4735 | var start: u64 = 0; |
| | 4736 | while (self.detectAllocCollision(start, object_size)) |item_end| { |
| | 4737 | start = mem.alignForwardGeneric(u64, item_end, min_alignment); |
| | 4738 | } |
| | 4739 | return start; |
| | 4740 | } |
| | 4741 | |
| | 4742 | pub fn allocatedVirtualSize(self: *MachO, start: u64) u64 { |
| | 4743 | if (start == 0) |
| | 4744 | return 0; |
| | 4745 | var min_pos: u64 = std.math.maxInt(u64); |
| | 4746 | for (self.sections.items(.segment_index)) |seg_id| { |
| | 4747 | const segment = self.segments.items[seg_id]; |
| | 4748 | if (segment.vmaddr <= start) continue; |
| | 4749 | if (segment.vmaddr < min_pos) min_pos = segment.vmaddr; |
| | 4750 | } |
| | 4751 | return min_pos - start; |
| | 4752 | } |
| | 4753 | |
| 5716 | pub fn makeStaticString(bytes: []const u8) [16]u8 { | 4754 | pub fn makeStaticString(bytes: []const u8) [16]u8 { |
| 5717 | var buf = [_]u8{0} ** 16; | 4755 | var buf = [_]u8{0} ** 16; |
| 5718 | assert(bytes.len <= buf.len); | 4756 | assert(bytes.len <= buf.len); |
| ... | @@ -5726,6 +4764,21 @@ fn getSegmentByName(self: MachO, segname: []const u8) ?u8 { | ... | @@ -5726,6 +4764,21 @@ fn getSegmentByName(self: MachO, segname: []const u8) ?u8 { |
| 5726 | } else return null; | 4764 | } else return null; |
| 5727 | } | 4765 | } |
| 5728 | | 4766 | |
| | 4767 | pub fn getSegment(self: MachO, sect_id: u8) macho.segment_command_64 { |
| | 4768 | const index = self.sections.items(.segment_index)[sect_id]; |
| | 4769 | return self.segments.items[index]; |
| | 4770 | } |
| | 4771 | |
| | 4772 | pub fn getSegmentPtr(self: *MachO, sect_id: u8) *macho.segment_command_64 { |
| | 4773 | const index = self.sections.items(.segment_index)[sect_id]; |
| | 4774 | return &self.segments.items[index]; |
| | 4775 | } |
| | 4776 | |
| | 4777 | pub fn getLinkeditSegmentPtr(self: *MachO) *macho.segment_command_64 { |
| | 4778 | const index = self.linkedit_segment_cmd_index.?; |
| | 4779 | return &self.segments.items[index]; |
| | 4780 | } |
| | 4781 | |
| 5729 | pub fn getSectionByName(self: MachO, segname: []const u8, sectname: []const u8) ?u8 { | 4782 | pub fn getSectionByName(self: MachO, segname: []const u8, sectname: []const u8) ?u8 { |
| 5730 | // TODO investigate caching with a hashmap | 4783 | // TODO investigate caching with a hashmap |
| 5731 | for (self.sections.items(.header)) |header, i| { | 4784 | for (self.sections.items(.header)) |header, i| { |
| ... | @@ -5877,161 +4930,6 @@ pub fn findFirst(comptime T: type, haystack: []align(1) const T, start: usize, p | ... | @@ -5877,161 +4930,6 @@ pub fn findFirst(comptime T: type, haystack: []align(1) const T, start: usize, p |
| 5877 | return i; | 4930 | return i; |
| 5878 | } | 4931 | } |
| 5879 | | 4932 | |
| 5880 | pub fn generateSymbolStabs( | | |
| 5881 | self: *MachO, | | |
| 5882 | object: Object, | | |
| 5883 | locals: *std.ArrayList(macho.nlist_64), | | |
| 5884 | ) !void { | | |
| 5885 | assert(!self.base.options.strip); | | |
| 5886 | | | |
| 5887 | log.debug("parsing debug info in '{s}'", .{object.name}); | | |
| 5888 | | | |
| 5889 | const gpa = self.base.allocator; | | |
| 5890 | var debug_info = try object.parseDwarfInfo(); | | |
| 5891 | defer debug_info.deinit(gpa); | | |
| 5892 | try dwarf.openDwarfDebugInfo(&debug_info, gpa); | | |
| 5893 | | | |
| 5894 | // We assume there is only one CU. | | |
| 5895 | const compile_unit = debug_info.findCompileUnit(0x0) catch |err| switch (err) { | | |
| 5896 | error.MissingDebugInfo => { | | |
| 5897 | // TODO audit cases with missing debug info and audit our dwarf.zig module. | | |
| 5898 | log.debug("invalid or missing debug info in {s}; skipping", .{object.name}); | | |
| 5899 | return; | | |
| 5900 | }, | | |
| 5901 | else => |e| return e, | | |
| 5902 | }; | | |
| 5903 | | | |
| 5904 | const tu_name = try compile_unit.die.getAttrString(&debug_info, dwarf.AT.name, debug_info.debug_str, compile_unit.*); | | |
| 5905 | const tu_comp_dir = try compile_unit.die.getAttrString(&debug_info, dwarf.AT.comp_dir, debug_info.debug_str, compile_unit.*); | | |
| 5906 | | | |
| 5907 | // Open scope | | |
| 5908 | try locals.ensureUnusedCapacity(3); | | |
| 5909 | locals.appendAssumeCapacity(.{ | | |
| 5910 | .n_strx = try self.strtab.insert(gpa, tu_comp_dir), | | |
| 5911 | .n_type = macho.N_SO, | | |
| 5912 | .n_sect = 0, | | |
| 5913 | .n_desc = 0, | | |
| 5914 | .n_value = 0, | | |
| 5915 | }); | | |
| 5916 | locals.appendAssumeCapacity(.{ | | |
| 5917 | .n_strx = try self.strtab.insert(gpa, tu_name), | | |
| 5918 | .n_type = macho.N_SO, | | |
| 5919 | .n_sect = 0, | | |
| 5920 | .n_desc = 0, | | |
| 5921 | .n_value = 0, | | |
| 5922 | }); | | |
| 5923 | locals.appendAssumeCapacity(.{ | | |
| 5924 | .n_strx = try self.strtab.insert(gpa, object.name), | | |
| 5925 | .n_type = macho.N_OSO, | | |
| 5926 | .n_sect = 0, | | |
| 5927 | .n_desc = 1, | | |
| 5928 | .n_value = object.mtime, | | |
| 5929 | }); | | |
| 5930 | | | |
| 5931 | var stabs_buf: [4]macho.nlist_64 = undefined; | | |
| 5932 | | | |
| 5933 | for (object.managed_atoms.items) |atom| { | | |
| 5934 | const stabs = try self.generateSymbolStabsForSymbol( | | |
| 5935 | atom.getSymbolWithLoc(), | | |
| 5936 | debug_info, | | |
| 5937 | &stabs_buf, | | |
| 5938 | ); | | |
| 5939 | try locals.appendSlice(stabs); | | |
| 5940 | | | |
| 5941 | for (atom.contained.items) |sym_at_off| { | | |
| 5942 | const sym_loc = SymbolWithLoc{ | | |
| 5943 | .sym_index = sym_at_off.sym_index, | | |
| 5944 | .file = atom.file, | | |
| 5945 | }; | | |
| 5946 | const contained_stabs = try self.generateSymbolStabsForSymbol( | | |
| 5947 | sym_loc, | | |
| 5948 | debug_info, | | |
| 5949 | &stabs_buf, | | |
| 5950 | ); | | |
| 5951 | try locals.appendSlice(contained_stabs); | | |
| 5952 | } | | |
| 5953 | } | | |
| 5954 | | | |
| 5955 | // Close scope | | |
| 5956 | try locals.append(.{ | | |
| 5957 | .n_strx = 0, | | |
| 5958 | .n_type = macho.N_SO, | | |
| 5959 | .n_sect = 0, | | |
| 5960 | .n_desc = 0, | | |
| 5961 | .n_value = 0, | | |
| 5962 | }); | | |
| 5963 | } | | |
| 5964 | | | |
| 5965 | fn generateSymbolStabsForSymbol( | | |
| 5966 | self: *MachO, | | |
| 5967 | sym_loc: SymbolWithLoc, | | |
| 5968 | debug_info: dwarf.DwarfInfo, | | |
| 5969 | buf: *[4]macho.nlist_64, | | |
| 5970 | ) ![]const macho.nlist_64 { | | |
| 5971 | const gpa = self.base.allocator; | | |
| 5972 | const object = self.objects.items[sym_loc.file.?]; | | |
| 5973 | const sym = self.getSymbol(sym_loc); | | |
| 5974 | const sym_name = self.getSymbolName(sym_loc); | | |
| 5975 | | | |
| 5976 | if (sym.n_strx == 0) return buf[0..0]; | | |
| 5977 | if (sym.n_desc == N_DESC_GCED) return buf[0..0]; | | |
| 5978 | if (self.symbolIsTemp(sym_loc)) return buf[0..0]; | | |
| 5979 | | | |
| 5980 | const source_sym = object.getSourceSymbol(sym_loc.sym_index) orelse return buf[0..0]; | | |
| 5981 | const size: ?u64 = size: { | | |
| 5982 | if (source_sym.tentative()) break :size null; | | |
| 5983 | for (debug_info.func_list.items) |func| { | | |
| 5984 | if (func.pc_range) |range| { | | |
| 5985 | if (source_sym.n_value >= range.start and source_sym.n_value < range.end) { | | |
| 5986 | break :size range.end - range.start; | | |
| 5987 | } | | |
| 5988 | } | | |
| 5989 | } | | |
| 5990 | break :size null; | | |
| 5991 | }; | | |
| 5992 | | | |
| 5993 | if (size) |ss| { | | |
| 5994 | buf[0] = .{ | | |
| 5995 | .n_strx = 0, | | |
| 5996 | .n_type = macho.N_BNSYM, | | |
| 5997 | .n_sect = sym.n_sect, | | |
| 5998 | .n_desc = 0, | | |
| 5999 | .n_value = sym.n_value, | | |
| 6000 | }; | | |
| 6001 | buf[1] = .{ | | |
| 6002 | .n_strx = try self.strtab.insert(gpa, sym_name), | | |
| 6003 | .n_type = macho.N_FUN, | | |
| 6004 | .n_sect = sym.n_sect, | | |
| 6005 | .n_desc = 0, | | |
| 6006 | .n_value = sym.n_value, | | |
| 6007 | }; | | |
| 6008 | buf[2] = .{ | | |
| 6009 | .n_strx = 0, | | |
| 6010 | .n_type = macho.N_FUN, | | |
| 6011 | .n_sect = 0, | | |
| 6012 | .n_desc = 0, | | |
| 6013 | .n_value = ss, | | |
| 6014 | }; | | |
| 6015 | buf[3] = .{ | | |
| 6016 | .n_strx = 0, | | |
| 6017 | .n_type = macho.N_ENSYM, | | |
| 6018 | .n_sect = sym.n_sect, | | |
| 6019 | .n_desc = 0, | | |
| 6020 | .n_value = ss, | | |
| 6021 | }; | | |
| 6022 | return buf; | | |
| 6023 | } else { | | |
| 6024 | buf[0] = .{ | | |
| 6025 | .n_strx = try self.strtab.insert(gpa, sym_name), | | |
| 6026 | .n_type = macho.N_STSYM, | | |
| 6027 | .n_sect = sym.n_sect, | | |
| 6028 | .n_desc = 0, | | |
| 6029 | .n_value = sym.n_value, | | |
| 6030 | }; | | |
| 6031 | return buf[0..1]; | | |
| 6032 | } | | |
| 6033 | } | | |
| 6034 | | | |
| 6035 | // fn snapshotState(self: *MachO) !void { | 4933 | // fn snapshotState(self: *MachO) !void { |
| 6036 | // const emit = self.base.options.emit orelse { | 4934 | // const emit = self.base.options.emit orelse { |
| 6037 | // log.debug("no emit directory found; skipping snapshot...", .{}); | 4935 | // log.debug("no emit directory found; skipping snapshot...", .{}); |
| ... | @@ -6287,7 +5185,7 @@ fn generateSymbolStabsForSymbol( | ... | @@ -6287,7 +5185,7 @@ fn generateSymbolStabsForSymbol( |
| 6287 | // try writer.writeByte(']'); | 5185 | // try writer.writeByte(']'); |
| 6288 | // } | 5186 | // } |
| 6289 | | 5187 | |
| 6290 | fn logSections(self: *MachO) void { | 5188 | pub fn logSections(self: *MachO) void { |
| 6291 | log.debug("sections:", .{}); | 5189 | log.debug("sections:", .{}); |
| 6292 | for (self.sections.items(.header)) |header, i| { | 5190 | for (self.sections.items(.header)) |header, i| { |
| 6293 | log.debug(" sect({d}): {s},{s} @{x}, sizeof({x})", .{ | 5191 | log.debug(" sect({d}): {s},{s} @{x}, sizeof({x})", .{ |
| ... | @@ -6325,7 +5223,7 @@ fn logSymAttributes(sym: macho.nlist_64, buf: *[9]u8) []const u8 { | ... | @@ -6325,7 +5223,7 @@ fn logSymAttributes(sym: macho.nlist_64, buf: *[9]u8) []const u8 { |
| 6325 | return buf[0..]; | 5223 | return buf[0..]; |
| 6326 | } | 5224 | } |
| 6327 | | 5225 | |
| 6328 | fn logSymtab(self: *MachO) void { | 5226 | pub fn logSymtab(self: *MachO) void { |
| 6329 | var buf: [9]u8 = undefined; | 5227 | var buf: [9]u8 = undefined; |
| 6330 | | 5228 | |
| 6331 | log.debug("symtab:", .{}); | 5229 | log.debug("symtab:", .{}); |
| ... | @@ -6336,7 +5234,7 @@ fn logSymtab(self: *MachO) void { | ... | @@ -6336,7 +5234,7 @@ fn logSymtab(self: *MachO) void { |
| 6336 | const def_index = if (sym.undf() and !sym.tentative()) | 5234 | const def_index = if (sym.undf() and !sym.tentative()) |
| 6337 | @divTrunc(sym.n_desc, macho.N_SYMBOL_RESOLVER) | 5235 | @divTrunc(sym.n_desc, macho.N_SYMBOL_RESOLVER) |
| 6338 | else | 5236 | else |
| 6339 | sym.n_sect; | 5237 | sym.n_sect + 1; |
| 6340 | log.debug(" %{d}: {s} @{x} in {s}({d}), {s}", .{ | 5238 | log.debug(" %{d}: {s} @{x} in {s}({d}), {s}", .{ |
| 6341 | sym_id, | 5239 | sym_id, |
| 6342 | object.getString(sym.n_strx), | 5240 | object.getString(sym.n_strx), |
| ... | @@ -6353,7 +5251,7 @@ fn logSymtab(self: *MachO) void { | ... | @@ -6353,7 +5251,7 @@ fn logSymtab(self: *MachO) void { |
| 6353 | const def_index = if (sym.undf() and !sym.tentative()) | 5251 | const def_index = if (sym.undf() and !sym.tentative()) |
| 6354 | @divTrunc(sym.n_desc, macho.N_SYMBOL_RESOLVER) | 5252 | @divTrunc(sym.n_desc, macho.N_SYMBOL_RESOLVER) |
| 6355 | else | 5253 | else |
| 6356 | sym.n_sect; | 5254 | sym.n_sect + 1; |
| 6357 | log.debug(" %{d}: {?s} @{x} in {s}({d}), {s}", .{ | 5255 | log.debug(" %{d}: {?s} @{x} in {s}({d}), {s}", .{ |
| 6358 | sym_id, | 5256 | sym_id, |
| 6359 | self.strtab.get(sym.n_strx), | 5257 | self.strtab.get(sym.n_strx), |
| ... | @@ -6418,7 +5316,7 @@ fn logSymtab(self: *MachO) void { | ... | @@ -6418,7 +5316,7 @@ fn logSymtab(self: *MachO) void { |
| 6418 | } | 5316 | } |
| 6419 | } | 5317 | } |
| 6420 | | 5318 | |
| 6421 | fn logAtoms(self: *MachO) void { | 5319 | pub fn logAtoms(self: *MachO) void { |
| 6422 | log.debug("atoms:", .{}); | 5320 | log.debug("atoms:", .{}); |
| 6423 | | 5321 | |
| 6424 | const slice = self.sections.slice(); | 5322 | const slice = self.sections.slice(); |
| ... | @@ -6471,19 +5369,3 @@ pub fn logAtom(self: *MachO, atom: *const Atom) void { | ... | @@ -6471,19 +5369,3 @@ pub fn logAtom(self: *MachO, atom: *const Atom) void { |
| 6471 | }); | 5369 | }); |
| 6472 | } | 5370 | } |
| 6473 | } | 5371 | } |
| 6474 | | | |
| 6475 | /// Since `os.copy_file_range` cannot be used when copying overlapping ranges within the same file, | | |
| 6476 | /// and since `File.copyRangeAll` uses `os.copy_file_range` under-the-hood, we use heap allocated | | |
| 6477 | /// buffers on all hosts except Linux (if `copy_file_range` syscall is available). | | |
| 6478 | pub fn copyRangeAllOverlappingAlloc( | | |
| 6479 | allocator: Allocator, | | |
| 6480 | file: std.fs.File, | | |
| 6481 | in_offset: u64, | | |
| 6482 | out_offset: u64, | | |
| 6483 | len: usize, | | |
| 6484 | ) !void { | | |
| 6485 | const buf = try allocator.alloc(u8, len); | | |
| 6486 | defer allocator.free(buf); | | |
| 6487 | _ = try file.preadAll(buf, in_offset); | | |
| 6488 | try file.pwriteAll(buf, out_offset); | | |
| 6489 | } | | |