authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-03-29 09:21:52+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-03-29 09:21:52+02:00
log46b2f1f705bec886ffb8b0473e0ce43c74145a8d
tree0b542353cbe2c5aa438c605832838179e3f46281
parentdd66e0addb30d795a04324096c913ca89ccbcf40
parent17ec2cea6455148526f56fa17cb704fd1d656b06
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #15105 from ziglang/hcs-win-poc

coff: improve handling of relocs and general linker fixes

8 files changed, 134 insertions(+), 185 deletions(-)

src/link.zig+2
......@@ -395,6 +395,7 @@ pub const File = struct {
395395 .macos => base.cast(MachO).?.ptraceAttach(pid) catch |err| {
396396 log.warn("attaching failed with error: {s}", .{@errorName(err)});
397397 },
398 .windows => {},
398399 else => return error.HotSwapUnavailableOnHostOperatingSystem,
399400 }
400401 }
......@@ -436,6 +437,7 @@ pub const File = struct {
436437 .macos => base.cast(MachO).?.ptraceDetach(pid) catch |err| {
437438 log.warn("detaching failed with error: {s}", .{@errorName(err)});
438439 },
440 .windows => {},
439441 else => return error.HotSwapUnavailableOnHostOperatingSystem,
440442 }
441443 }
src/link/Coff.zig+98-115
......@@ -49,11 +49,8 @@ imports_count_dirty: bool = true,
4949/// Virtual address of the entry point procedure relative to image base.
5050entry_addr: ?u32 = null,
5151
52/// Table of Decls that are currently alive.
53/// We store them here so that we can properly dispose of any allocated
54/// memory within the atom in the incremental linker.
55/// TODO consolidate this.
56decls: std.AutoHashMapUnmanaged(Module.Decl.Index, DeclMetadata) = .{},
52/// Table of tracked Decls.
53decls: std.AutoArrayHashMapUnmanaged(Module.Decl.Index, DeclMetadata) = .{},
5754
5855/// List of atoms that are either synthetic or map directly to the Zig source program.
5956atoms: std.ArrayListUnmanaged(Atom) = .{},
......@@ -98,9 +95,9 @@ const Entry = struct {
9895 sym_index: u32,
9996};
10097
101const RelocTable = std.AutoHashMapUnmanaged(Atom.Index, std.ArrayListUnmanaged(Relocation));
102const BaseRelocationTable = std.AutoHashMapUnmanaged(Atom.Index, std.ArrayListUnmanaged(u32));
103const UnnamedConstTable = std.AutoHashMapUnmanaged(Module.Decl.Index, std.ArrayListUnmanaged(Atom.Index));
98const RelocTable = std.AutoArrayHashMapUnmanaged(Atom.Index, std.ArrayListUnmanaged(Relocation));
99const BaseRelocationTable = std.AutoArrayHashMapUnmanaged(Atom.Index, std.ArrayListUnmanaged(u32));
100const UnnamedConstTable = std.AutoArrayHashMapUnmanaged(Module.Decl.Index, std.ArrayListUnmanaged(Atom.Index));
104101
105102const default_file_alignment: u16 = 0x200;
106103const default_size_of_stack_reserve: u32 = 0x1000000;
......@@ -137,6 +134,10 @@ const DeclMetadata = struct {
137134 /// A list of all exports aliases of this Decl.
138135 exports: std.ArrayListUnmanaged(u32) = .{},
139136
137 fn deinit(m: *DeclMetadata, allocator: Allocator) void {
138 m.exports.deinit(allocator);
139 }
140
140141 fn getExport(m: DeclMetadata, coff_file: *const Coff, name: []const u8) ?u32 {
141142 for (m.exports.items) |exp| {
142143 if (mem.eql(u8, name, coff_file.getSymbolName(.{
......@@ -293,39 +294,27 @@ pub fn deinit(self: *Coff) void {
293294 }
294295 self.import_tables.deinit(gpa);
295296
296 {
297 var it = self.decls.iterator();
298 while (it.next()) |entry| {
299 entry.value_ptr.exports.deinit(gpa);
300 }
301 self.decls.deinit(gpa);
297 for (self.decls.values()) |*metadata| {
298 metadata.deinit(gpa);
302299 }
300 self.decls.deinit(gpa);
303301
304302 self.atom_by_index_table.deinit(gpa);
305303
306 {
307 var it = self.unnamed_const_atoms.valueIterator();
308 while (it.next()) |atoms| {
309 atoms.deinit(gpa);
310 }
311 self.unnamed_const_atoms.deinit(gpa);
304 for (self.unnamed_const_atoms.values()) |*atoms| {
305 atoms.deinit(gpa);
312306 }
307 self.unnamed_const_atoms.deinit(gpa);
313308
314 {
315 var it = self.relocs.valueIterator();
316 while (it.next()) |relocs| {
317 relocs.deinit(gpa);
318 }
319 self.relocs.deinit(gpa);
309 for (self.relocs.values()) |*relocs| {
310 relocs.deinit(gpa);
320311 }
312 self.relocs.deinit(gpa);
321313
322 {
323 var it = self.base_relocs.valueIterator();
324 while (it.next()) |relocs| {
325 relocs.deinit(gpa);
326 }
327 self.base_relocs.deinit(gpa);
314 for (self.base_relocs.values()) |*relocs| {
315 relocs.deinit(gpa);
328316 }
317 self.base_relocs.deinit(gpa);
329318}
330319
331320fn populateMissingMetadata(self: *Coff) !void {
......@@ -455,7 +444,44 @@ fn allocateSection(self: *Coff, name: []const u8, size: u32, flags: coff.Section
455444 return index;
456445}
457446
458fn growSectionVM(self: *Coff, sect_id: u32, needed_size: u32) !void {
447fn growSection(self: *Coff, sect_id: u32, needed_size: u32) !void {
448 const header = &self.sections.items(.header)[sect_id];
449 const maybe_last_atom_index = self.sections.items(.last_atom_index)[sect_id];
450 const sect_capacity = self.allocatedSize(header.pointer_to_raw_data);
451
452 if (needed_size > sect_capacity) {
453 const new_offset = self.findFreeSpace(needed_size, default_file_alignment);
454 const current_size = if (maybe_last_atom_index) |last_atom_index| blk: {
455 const last_atom = self.getAtom(last_atom_index);
456 const sym = last_atom.getSymbol(self);
457 break :blk (sym.value + last_atom.size) - header.virtual_address;
458 } else 0;
459 log.debug("moving {s} from 0x{x} to 0x{x}", .{
460 self.getSectionName(header),
461 header.pointer_to_raw_data,
462 new_offset,
463 });
464 const amt = try self.base.file.?.copyRangeAll(
465 header.pointer_to_raw_data,
466 self.base.file.?,
467 new_offset,
468 current_size,
469 );
470 if (amt != current_size) return error.InputOutput;
471 header.pointer_to_raw_data = new_offset;
472 }
473
474 const sect_vm_capacity = self.allocatedVirtualSize(header.virtual_address);
475 if (needed_size > sect_vm_capacity) {
476 try self.growSectionVirtualMemory(sect_id, needed_size);
477 self.markRelocsDirtyByAddress(header.virtual_address + needed_size);
478 }
479
480 header.virtual_size = @max(header.virtual_size, needed_size);
481 header.size_of_raw_data = needed_size;
482}
483
484fn growSectionVirtualMemory(self: *Coff, sect_id: u32, needed_size: u32) !void {
459485 const header = &self.sections.items(.header)[sect_id];
460486 const increased_size = padToIdeal(needed_size);
461487 const old_aligned_end = header.virtual_address + mem.alignForwardGeneric(u32, header.virtual_size, self.page_size);
......@@ -562,38 +588,8 @@ fn allocateAtom(self: *Coff, atom_index: Atom.Index, new_atom_size: u32, alignme
562588 else
563589 true;
564590 if (expand_section) {
565 const sect_capacity = self.allocatedSize(header.pointer_to_raw_data);
566591 const needed_size: u32 = (vaddr + new_atom_size) - header.virtual_address;
567 if (needed_size > sect_capacity) {
568 const new_offset = self.findFreeSpace(needed_size, default_file_alignment);
569 const current_size = if (maybe_last_atom_index.*) |last_atom_index| blk: {
570 const last_atom = self.getAtom(last_atom_index);
571 const sym = last_atom.getSymbol(self);
572 break :blk (sym.value + last_atom.size) - header.virtual_address;
573 } else 0;
574 log.debug("moving {s} from 0x{x} to 0x{x}", .{
575 self.getSectionName(header),
576 header.pointer_to_raw_data,
577 new_offset,
578 });
579 const amt = try self.base.file.?.copyRangeAll(
580 header.pointer_to_raw_data,
581 self.base.file.?,
582 new_offset,
583 current_size,
584 );
585 if (amt != current_size) return error.InputOutput;
586 header.pointer_to_raw_data = new_offset;
587 }
588
589 const sect_vm_capacity = self.allocatedVirtualSize(header.virtual_address);
590 if (needed_size > sect_vm_capacity) {
591 try self.growSectionVM(sect_id, needed_size);
592 self.markRelocsDirtyByAddress(header.virtual_address + needed_size);
593 }
594
595 header.virtual_size = @max(header.virtual_size, needed_size);
596 header.size_of_raw_data = needed_size;
592 try self.growSection(sect_id, needed_size);
597593 maybe_last_atom_index.* = atom_index;
598594 }
599595
......@@ -771,7 +767,7 @@ fn shrinkAtom(self: *Coff, atom_index: Atom.Index, new_block_size: u32) void {
771767 // capacity, insert a free list node for it.
772768}
773769
774fn writeAtom(self: *Coff, atom_index: Atom.Index, code: []const u8) !void {
770fn writeAtom(self: *Coff, atom_index: Atom.Index, code: []u8) !void {
775771 const atom = self.getAtom(atom_index);
776772 const sym = atom.getSymbol(self);
777773 const section = self.sections.get(@enumToInt(sym.section_number) - 1);
......@@ -781,8 +777,8 @@ fn writeAtom(self: *Coff, atom_index: Atom.Index, code: []const u8) !void {
781777 file_offset,
782778 file_offset + code.len,
783779 });
780 self.resolveRelocs(atom_index, code);
784781 try self.base.file.?.pwriteAll(code, file_offset);
785 try self.resolveRelocs(atom_index);
786782}
787783
788784fn writePtrWidthAtom(self: *Coff, atom_index: Atom.Index) !void {
......@@ -800,8 +796,7 @@ fn writePtrWidthAtom(self: *Coff, atom_index: Atom.Index) !void {
800796
801797fn markRelocsDirtyByTarget(self: *Coff, target: SymbolWithLoc) void {
802798 // TODO: reverse-lookup might come in handy here
803 var it = self.relocs.valueIterator();
804 while (it.next()) |relocs| {
799 for (self.relocs.values()) |*relocs| {
805800 for (relocs.items) |*reloc| {
806801 if (!reloc.target.eql(target)) continue;
807802 reloc.dirty = true;
......@@ -810,8 +805,7 @@ fn markRelocsDirtyByTarget(self: *Coff, target: SymbolWithLoc) void {
810805}
811806
812807fn markRelocsDirtyByAddress(self: *Coff, addr: u32) void {
813 var it = self.relocs.valueIterator();
814 while (it.next()) |relocs| {
808 for (self.relocs.values()) |*relocs| {
815809 for (relocs.items) |*reloc| {
816810 const target_vaddr = reloc.getTargetAddress(self) orelse continue;
817811 if (target_vaddr < addr) continue;
......@@ -820,14 +814,16 @@ fn markRelocsDirtyByAddress(self: *Coff, addr: u32) void {
820814 }
821815}
822816
823fn resolveRelocs(self: *Coff, atom_index: Atom.Index) !void {
824 const relocs = self.relocs.get(atom_index) orelse return;
817fn resolveRelocs(self: *Coff, atom_index: Atom.Index, code: []u8) void {
818 const relocs = self.relocs.getPtr(atom_index) orelse return;
825819
826820 log.debug("relocating '{s}'", .{self.getAtom(atom_index).getName(self)});
827821
828822 for (relocs.items) |*reloc| {
829823 if (!reloc.dirty) continue;
830 try reloc.resolve(atom_index, self);
824 if (reloc.resolve(atom_index, code, self)) {
825 reloc.dirty = false;
826 }
831827 }
832828}
833829
......@@ -944,7 +940,7 @@ pub fn updateFunc(self: *Coff, module: *Module, func: *Module.Fn, air: Air, live
944940 &code_buffer,
945941 .none,
946942 );
947 const code = switch (res) {
943 var code = switch (res) {
948944 .ok => code_buffer.items,
949945 .fail => |em| {
950946 decl.analysis = .codegen_failure;
......@@ -994,7 +990,7 @@ pub fn lowerUnnamedConst(self: *Coff, tv: TypedValue, decl_index: Module.Decl.In
994990 const res = try codegen.generateSymbol(&self.base, decl.srcLoc(), tv, &code_buffer, .none, .{
995991 .parent_atom_index = self.getAtom(atom_index).getSymbolIndex().?,
996992 });
997 const code = switch (res) {
993 var code = switch (res) {
998994 .ok => code_buffer.items,
999995 .fail => |em| {
1000996 decl.analysis = .codegen_failure;
......@@ -1057,7 +1053,7 @@ pub fn updateDecl(self: *Coff, module: *Module, decl_index: Module.Decl.Index) !
10571053 }, &code_buffer, .none, .{
10581054 .parent_atom_index = atom.getSymbolIndex().?,
10591055 });
1060 const code = switch (res) {
1056 var code = switch (res) {
10611057 .ok => code_buffer.items,
10621058 .fail => |em| {
10631059 decl.analysis = .codegen_failure;
......@@ -1110,7 +1106,7 @@ fn getDeclOutputSection(self: *Coff, decl_index: Module.Decl.Index) u16 {
11101106 return index;
11111107}
11121108
1113fn updateDeclCode(self: *Coff, decl_index: Module.Decl.Index, code: []const u8, complex_type: coff.ComplexType) !void {
1109fn updateDeclCode(self: *Coff, decl_index: Module.Decl.Index, code: []u8, complex_type: coff.ComplexType) !void {
11141110 const gpa = self.base.allocator;
11151111 const mod = self.base.options.module.?;
11161112 const decl = mod.declPtr(decl_index);
......@@ -1195,7 +1191,7 @@ pub fn freeDecl(self: *Coff, decl_index: Module.Decl.Index) void {
11951191
11961192 log.debug("freeDecl {*}", .{decl});
11971193
1198 if (self.decls.fetchRemove(decl_index)) |const_kv| {
1194 if (self.decls.fetchOrderedRemove(decl_index)) |const_kv| {
11991195 var kv = const_kv;
12001196 self.freeAtom(kv.value.atom);
12011197 self.freeUnnamedConsts(decl_index);
......@@ -1422,12 +1418,29 @@ pub fn flushModule(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod
14221418 }
14231419
14241420 try self.writeImportTables();
1425 {
1426 var it = self.relocs.keyIterator();
1427 while (it.next()) |atom| {
1428 try self.resolveRelocs(atom.*);
1429 }
1421
1422 for (self.relocs.keys(), self.relocs.values()) |atom_index, relocs| {
1423 const needs_update = for (relocs.items) |reloc| {
1424 if (reloc.dirty) break true;
1425 } else false;
1426
1427 if (!needs_update) continue;
1428
1429 const atom = self.getAtom(atom_index);
1430 const sym = atom.getSymbol(self);
1431 const section = self.sections.get(@enumToInt(sym.section_number) - 1).header;
1432 const file_offset = section.pointer_to_raw_data + sym.value - section.virtual_address;
1433
1434 var code = std.ArrayList(u8).init(gpa);
1435 defer code.deinit();
1436 try code.resize(math.cast(usize, atom.size) orelse return error.Overflow);
1437
1438 const amt = try self.base.file.?.preadAll(code.items, file_offset);
1439 if (amt != code.items.len) return error.InputOutput;
1440
1441 try self.writeAtom(atom_index, code.items);
14301442 }
1443
14311444 try self.writeBaseRelocations();
14321445
14331446 if (self.getEntryPoint()) |entry_sym_loc| {
......@@ -1576,25 +1589,8 @@ fn writeBaseRelocations(self: *Coff) !void {
15761589 }
15771590
15781591 const header = &self.sections.items(.header)[self.reloc_section_index.?];
1579 const sect_capacity = self.allocatedSize(header.pointer_to_raw_data);
15801592 const needed_size = @intCast(u32, buffer.items.len);
1581 if (needed_size > sect_capacity) {
1582 const new_offset = self.findFreeSpace(needed_size, default_file_alignment);
1583 log.debug("moving {s} from 0x{x} to 0x{x}", .{
1584 self.getSectionName(header),
1585 header.pointer_to_raw_data,
1586 new_offset,
1587 });
1588 header.pointer_to_raw_data = new_offset;
1589
1590 const sect_vm_capacity = self.allocatedVirtualSize(header.virtual_address);
1591 if (needed_size > sect_vm_capacity) {
1592 // TODO: we want to enforce .reloc after every alloc section.
1593 try self.growSectionVM(self.reloc_section_index.?, needed_size);
1594 }
1595 }
1596 header.virtual_size = @max(header.virtual_size, needed_size);
1597 header.size_of_raw_data = needed_size;
1593 try self.growSection(self.reloc_section_index.?, needed_size);
15981594
15991595 try self.base.file.?.pwriteAll(buffer.items, header.pointer_to_raw_data);
16001596
......@@ -1633,20 +1629,7 @@ fn writeImportTables(self: *Coff) !void {
16331629 }
16341630
16351631 const needed_size = iat_size + dir_table_size + lookup_table_size + names_table_size + dll_names_size;
1636 const sect_capacity = self.allocatedSize(header.pointer_to_raw_data);
1637 if (needed_size > sect_capacity) {
1638 const new_offset = self.findFreeSpace(needed_size, default_file_alignment);
1639 log.debug("moving .idata from 0x{x} to 0x{x}", .{ header.pointer_to_raw_data, new_offset });
1640 header.pointer_to_raw_data = new_offset;
1641
1642 const sect_vm_capacity = self.allocatedVirtualSize(header.virtual_address);
1643 if (needed_size > sect_vm_capacity) {
1644 try self.growSectionVM(self.idata_section_index.?, needed_size);
1645 }
1646
1647 header.virtual_size = @max(header.virtual_size, needed_size);
1648 header.size_of_raw_data = needed_size;
1649 }
1632 try self.growSection(self.idata_section_index.?, needed_size);
16501633
16511634 // Do the actual writes
16521635 var buffer = std.ArrayList(u8).init(gpa);
src/link/Coff/Atom.zig+2-2
......@@ -121,8 +121,8 @@ pub fn addBaseRelocation(coff_file: *Coff, atom_index: Index, offset: u32) !void
121121
122122pub fn freeRelocations(coff_file: *Coff, atom_index: Index) void {
123123 const gpa = coff_file.base.allocator;
124 var removed_relocs = coff_file.relocs.fetchRemove(atom_index);
124 var removed_relocs = coff_file.relocs.fetchOrderedRemove(atom_index);
125125 if (removed_relocs) |*relocs| relocs.value.deinit(gpa);
126 var removed_base_relocs = coff_file.base_relocs.fetchRemove(atom_index);
126 var removed_base_relocs = coff_file.base_relocs.fetchOrderedRemove(atom_index);
127127 if (removed_base_relocs) |*base_relocs| base_relocs.value.deinit(gpa);
128128}
src/link/Coff/ImportTable.zig+1-1
......@@ -121,7 +121,7 @@ pub fn fmtDebug(itab: ImportTable, ctx: Context) std.fmt.Formatter(fmt) {
121121 return .{ .data = .{ .itab = itab, .ctx = ctx } };
122122}
123123
124const ImportIndex = u32;
124pub const ImportIndex = u32;
125125const ImportTable = @This();
126126
127127const std = @import("std");
src/link/Coff/Relocation.zig+23-49
......@@ -72,62 +72,50 @@ pub fn getTargetAddress(self: Relocation, coff_file: *const Coff) ?u32 {
7272 }
7373}
7474
75pub fn resolve(self: *Relocation, atom_index: Atom.Index, coff_file: *Coff) !void {
75/// Returns `false` if obtaining the target address has been deferred until `flushModule`.
76/// This can happen when trying to resolve address of an import table entry ahead of time.
77pub fn resolve(self: Relocation, atom_index: Atom.Index, code: []u8, coff_file: *Coff) bool {
7678 const atom = coff_file.getAtom(atom_index);
7779 const source_sym = atom.getSymbol(coff_file);
78 const source_section = coff_file.sections.get(@enumToInt(source_sym.section_number) - 1).header;
7980 const source_vaddr = source_sym.value + self.offset;
8081
81 const file_offset = source_section.pointer_to_raw_data + source_sym.value - source_section.virtual_address;
82
83 const target_vaddr = self.getTargetAddress(coff_file) orelse return;
82 const target_vaddr = self.getTargetAddress(coff_file) orelse return false;
8483 const target_vaddr_with_addend = target_vaddr + self.addend;
8584
86 log.debug(" ({x}: [() => 0x{x} ({s})) ({s}) (in file at 0x{x})", .{
85 log.debug(" ({x}: [() => 0x{x} ({s})) ({s}) ", .{
8786 source_vaddr,
8887 target_vaddr_with_addend,
8988 coff_file.getSymbolName(self.target),
9089 @tagName(self.type),
91 file_offset + self.offset,
9290 });
9391
9492 const ctx: Context = .{
9593 .source_vaddr = source_vaddr,
9694 .target_vaddr = target_vaddr_with_addend,
97 .file_offset = file_offset,
9895 .image_base = coff_file.getImageBase(),
96 .code = code,
97 .ptr_width = coff_file.ptr_width,
9998 };
10099
101100 switch (coff_file.base.options.target.cpu.arch) {
102 .aarch64 => try self.resolveAarch64(ctx, coff_file),
103 .x86, .x86_64 => try self.resolveX86(ctx, coff_file),
101 .aarch64 => self.resolveAarch64(ctx),
102 .x86, .x86_64 => self.resolveX86(ctx),
104103 else => unreachable, // unhandled target architecture
105104 }
106105
107 self.dirty = false;
106 return true;
108107}
109108
110109const Context = struct {
111110 source_vaddr: u32,
112111 target_vaddr: u32,
113 file_offset: u32,
114112 image_base: u64,
113 code: []u8,
114 ptr_width: Coff.PtrWidth,
115115};
116116
117fn resolveAarch64(self: Relocation, ctx: Context, coff_file: *Coff) !void {
118 var buffer: [@sizeOf(u64)]u8 = undefined;
119 switch (self.length) {
120 2 => {
121 const amt = try coff_file.base.file.?.preadAll(buffer[0..4], ctx.file_offset + self.offset);
122 if (amt != 4) return error.InputOutput;
123 },
124 3 => {
125 const amt = try coff_file.base.file.?.preadAll(&buffer, ctx.file_offset + self.offset);
126 if (amt != 8) return error.InputOutput;
127 },
128 else => unreachable,
129 }
130
117fn resolveAarch64(self: Relocation, ctx: Context) void {
118 var buffer = ctx.code[self.offset..];
131119 switch (self.type) {
132120 .got_page, .import_page, .page => {
133121 const source_page = @intCast(i32, ctx.source_vaddr >> 12);
......@@ -188,7 +176,7 @@ fn resolveAarch64(self: Relocation, ctx: Context, coff_file: *Coff) !void {
188176 buffer[0..4],
189177 @truncate(u32, ctx.target_vaddr + ctx.image_base),
190178 ),
191 3 => mem.writeIntLittle(u64, &buffer, ctx.target_vaddr + ctx.image_base),
179 3 => mem.writeIntLittle(u64, buffer[0..8], ctx.target_vaddr + ctx.image_base),
192180 else => unreachable,
193181 }
194182 },
......@@ -196,15 +184,10 @@ fn resolveAarch64(self: Relocation, ctx: Context, coff_file: *Coff) !void {
196184 .got => unreachable,
197185 .import => unreachable,
198186 }
199
200 switch (self.length) {
201 2 => try coff_file.base.file.?.pwriteAll(buffer[0..4], ctx.file_offset + self.offset),
202 3 => try coff_file.base.file.?.pwriteAll(&buffer, ctx.file_offset + self.offset),
203 else => unreachable,
204 }
205187}
206188
207fn resolveX86(self: Relocation, ctx: Context, coff_file: *Coff) !void {
189fn resolveX86(self: Relocation, ctx: Context) void {
190 var buffer = ctx.code[self.offset..];
208191 switch (self.type) {
209192 .got_page => unreachable,
210193 .got_pageoff => unreachable,
......@@ -216,26 +199,17 @@ fn resolveX86(self: Relocation, ctx: Context, coff_file: *Coff) !void {
216199 .got, .import => {
217200 assert(self.pcrel);
218201 const disp = @intCast(i32, ctx.target_vaddr) - @intCast(i32, ctx.source_vaddr) - 4;
219 try coff_file.base.file.?.pwriteAll(mem.asBytes(&disp), ctx.file_offset + self.offset);
202 mem.writeIntLittle(i32, buffer[0..4], disp);
220203 },
221204 .direct => {
222205 if (self.pcrel) {
223206 const disp = @intCast(i32, ctx.target_vaddr) - @intCast(i32, ctx.source_vaddr) - 4;
224 try coff_file.base.file.?.pwriteAll(mem.asBytes(&disp), ctx.file_offset + self.offset);
225 } else switch (coff_file.ptr_width) {
226 .p32 => try coff_file.base.file.?.pwriteAll(
227 mem.asBytes(&@intCast(u32, ctx.target_vaddr + ctx.image_base)),
228 ctx.file_offset + self.offset,
229 ),
207 mem.writeIntLittle(i32, buffer[0..4], disp);
208 } else switch (ctx.ptr_width) {
209 .p32 => mem.writeIntLittle(u32, buffer[0..4], @intCast(u32, ctx.target_vaddr + ctx.image_base)),
230210 .p64 => switch (self.length) {
231 2 => try coff_file.base.file.?.pwriteAll(
232 mem.asBytes(&@truncate(u32, ctx.target_vaddr + ctx.image_base)),
233 ctx.file_offset + self.offset,
234 ),
235 3 => try coff_file.base.file.?.pwriteAll(
236 mem.asBytes(&(ctx.target_vaddr + ctx.image_base)),
237 ctx.file_offset + self.offset,
238 ),
211 2 => mem.writeIntLittle(u32, buffer[0..4], @truncate(u32, ctx.target_vaddr + ctx.image_base)),
212 3 => mem.writeIntLittle(u64, buffer[0..8], ctx.target_vaddr + ctx.image_base),
239213 else => unreachable,
240214 },
241215 }
src/link/MachO.zig+1-1
......@@ -1091,7 +1091,7 @@ pub fn writeAtom(self: *MachO, atom_index: Atom.Index, code: []u8) !void {
10911091 log.debug("writing atom for symbol {s} at file offset 0x{x}", .{ atom.getName(self), file_offset });
10921092
10931093 if (self.relocs.get(atom_index)) |relocs| {
1094 try Atom.resolveRelocations(self, atom_index, relocs.items, code);
1094 Atom.resolveRelocations(self, atom_index, relocs.items, code);
10951095 }
10961096
10971097 if (is_hot_update_compatible) {
src/link/MachO/Atom.zig+2-2
......@@ -183,11 +183,11 @@ pub fn addLazyBinding(macho_file: *MachO, atom_index: Index, binding: Binding) !
183183 try gop.value_ptr.append(gpa, binding);
184184}
185185
186pub fn resolveRelocations(macho_file: *MachO, atom_index: Index, relocs: []Relocation, code: []u8) !void {
186pub fn resolveRelocations(macho_file: *MachO, atom_index: Index, relocs: []Relocation, code: []u8) void {
187187 log.debug("relocating '{s}'", .{macho_file.getAtom(atom_index).getName(macho_file)});
188188 for (relocs) |*reloc| {
189189 if (!reloc.dirty) continue;
190 try reloc.resolve(macho_file, atom_index, code);
190 reloc.resolve(macho_file, atom_index, code);
191191 reloc.dirty = false;
192192 }
193193}
src/link/MachO/Relocation.zig+5-15
......@@ -50,7 +50,7 @@ pub fn getTargetAtomIndex(self: Relocation, macho_file: *MachO) ?Atom.Index {
5050 return macho_file.getAtomIndexForSymbol(self.target);
5151}
5252
53pub fn resolve(self: Relocation, macho_file: *MachO, atom_index: Atom.Index, code: []u8) !void {
53pub fn resolve(self: Relocation, macho_file: *MachO, atom_index: Atom.Index, code: []u8) void {
5454 const arch = macho_file.base.options.target.cpu.arch;
5555 const atom = macho_file.getAtom(atom_index);
5656 const source_sym = atom.getSymbol(macho_file);
......@@ -68,18 +68,13 @@ pub fn resolve(self: Relocation, macho_file: *MachO, atom_index: Atom.Index, cod
6868 });
6969
7070 switch (arch) {
71 .aarch64 => return self.resolveAarch64(source_addr, target_addr, code),
72 .x86_64 => return self.resolveX8664(source_addr, target_addr, code),
71 .aarch64 => self.resolveAarch64(source_addr, target_addr, code),
72 .x86_64 => self.resolveX8664(source_addr, target_addr, code),
7373 else => unreachable,
7474 }
7575}
7676
77fn resolveAarch64(
78 self: Relocation,
79 source_addr: u64,
80 target_addr: i64,
81 code: []u8,
82) !void {
77fn resolveAarch64(self: Relocation, source_addr: u64, target_addr: i64, code: []u8) void {
8378 const rel_type = @intToEnum(macho.reloc_type_arm64, self.type);
8479 if (rel_type == .ARM64_RELOC_UNSIGNED) {
8580 return switch (self.length) {
......@@ -212,12 +207,7 @@ fn resolveAarch64(
212207 }
213208}
214209
215fn resolveX8664(
216 self: Relocation,
217 source_addr: u64,
218 target_addr: i64,
219 code: []u8,
220) !void {
210fn resolveX8664(self: Relocation, source_addr: u64, target_addr: i64, code: []u8) void {
221211 const rel_type = @intToEnum(macho.reloc_type_x86_64, self.type);
222212 switch (rel_type) {
223213 .X86_64_RELOC_BRANCH,