authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-08-25 17:23:30+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-08-25 17:23:30+02:00
logee786e5c3c1171af082f9463ca46b9ed08d2601e
treeb3bf14f254ab889b98594606e7a6c95e6e134593
parentaf57ccbe279d73f91358ec28fb4afd54868650e8

macho: add GOT entries as actual atoms


3 files changed, 73 insertions(+), 231 deletions(-)

src/codegen.zig+17-45
......@@ -2668,24 +2668,21 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
26682668 if (self.air.value(callee)) |func_value| {
26692669 if (func_value.castTag(.function)) |func_payload| {
26702670 const func = func_payload.data;
2671 const got_addr = blk: {
2672 const seg = macho_file.load_commands.items[macho_file.data_const_segment_cmd_index.?].Segment;
2673 const got = seg.sections.items[macho_file.got_section_index.?];
2674 const got_index = macho_file.got_entries_map.get(.{
2675 .where = .local,
2676 .where_index = func.owner_decl.link.macho.local_sym_index,
2677 }) orelse unreachable;
2678 break :blk got.addr + got_index * @sizeOf(u64);
2679 };
2671 // TODO I'm hacking my way through here by repurposing .memory for storing
2672 // index to the GOT target symbol index.
26802673 switch (arch) {
26812674 .x86_64 => {
2682 try self.genSetReg(Type.initTag(.u64), .rax, .{ .memory = got_addr });
2675 try self.genSetReg(Type.initTag(.u64), .rax, .{
2676 .memory = func.owner_decl.link.macho.local_sym_index,
2677 });
26832678 // callq *%rax
26842679 try self.code.ensureCapacity(self.code.items.len + 2);
26852680 self.code.appendSliceAssumeCapacity(&[2]u8{ 0xff, 0xd0 });
26862681 },
26872682 .aarch64 => {
2688 try self.genSetReg(Type.initTag(.u64), .x30, .{ .memory = got_addr });
2683 try self.genSetReg(Type.initTag(.u64), .x30, .{
2684 .memory = func.owner_decl.link.macho.local_sym_index,
2685 });
26892686 // blr x30
26902687 writeInt(u32, try self.code.addManyAsArray(4), Instruction.blr(.x30).toU32());
26912688 },
......@@ -4206,29 +4203,20 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
42064203 }).toU32());
42074204
42084205 if (self.bin_file.cast(link.File.MachO)) |macho_file| {
4209 // TODO this is super awkward. We are reversing the address of the GOT entry here.
4210 // We should probably have it cached or move the reloc adding somewhere else.
4211 const got_addr = blk: {
4212 const seg = macho_file.load_commands.items[macho_file.data_const_segment_cmd_index.?].Segment;
4213 const got = seg.sections.items[macho_file.got_section_index.?];
4214 break :blk got.addr;
4215 };
4216 const where_index = blk: for (macho_file.got_entries.items) |key, id| {
4217 if (got_addr + id * @sizeOf(u64) == addr) break :blk key.where_index;
4218 } else unreachable;
4206 // TODO I think the reloc might be in the wrong place.
42194207 const decl = macho_file.active_decl.?;
42204208 // Page reloc for adrp instruction.
42214209 try decl.link.macho.relocs.append(self.bin_file.allocator, .{
42224210 .offset = offset,
42234211 .where = .local,
4224 .where_index = where_index,
4212 .where_index = @intCast(u32, addr),
42254213 .payload = .{ .page = .{ .kind = .got } },
42264214 });
42274215 // Pageoff reloc for adrp instruction.
42284216 try decl.link.macho.relocs.append(self.bin_file.allocator, .{
42294217 .offset = offset + 4,
42304218 .where = .local,
4231 .where_index = where_index,
4219 .where_index = @intCast(u32, addr),
42324220 .payload = .{ .page_off = .{ .kind = .got } },
42334221 });
42344222 } else {
......@@ -4489,22 +4477,13 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
44894477 const offset = @intCast(u32, self.code.items.len);
44904478
44914479 if (self.bin_file.cast(link.File.MachO)) |macho_file| {
4492 // TODO this is super awkward. We are reversing the address of the GOT entry here.
4493 // We should probably have it cached or move the reloc adding somewhere else.
4494 const got_addr = blk: {
4495 const seg = macho_file.load_commands.items[macho_file.data_const_segment_cmd_index.?].Segment;
4496 const got = seg.sections.items[macho_file.got_section_index.?];
4497 break :blk got.addr;
4498 };
4499 const where_index = blk: for (macho_file.got_entries.items) |key, id| {
4500 if (got_addr + id * @sizeOf(u64) == x) break :blk key.where_index;
4501 } else unreachable;
4480 // TODO I think the reloc might be in the wrong place.
45024481 const decl = macho_file.active_decl.?;
45034482 // Load reloc for LEA instruction.
45044483 try decl.link.macho.relocs.append(self.bin_file.allocator, .{
45054484 .offset = offset - 4,
45064485 .where = .local,
4507 .where_index = where_index,
4486 .where_index = @intCast(u32, x),
45084487 .payload = .{ .load = .{ .kind = .got } },
45094488 });
45104489 } else {
......@@ -4720,17 +4699,10 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
47204699 const got = &elf_file.program_headers.items[elf_file.phdr_got_index.?];
47214700 const got_addr = got.p_vaddr + decl.link.elf.offset_table_index * ptr_bytes;
47224701 return MCValue{ .memory = got_addr };
4723 } else if (self.bin_file.cast(link.File.MachO)) |macho_file| {
4724 const got_addr = blk: {
4725 const seg = macho_file.load_commands.items[macho_file.data_const_segment_cmd_index.?].Segment;
4726 const got = seg.sections.items[macho_file.got_section_index.?];
4727 const got_index = macho_file.got_entries_map.get(.{
4728 .where = .local,
4729 .where_index = decl.link.macho.local_sym_index,
4730 }) orelse unreachable;
4731 break :blk got.addr + got_index * ptr_bytes;
4732 };
4733 return MCValue{ .memory = got_addr };
4702 } else if (self.bin_file.cast(link.File.MachO)) |_| {
4703 // TODO I'm hacking my way through here by repurposing .memory for storing
4704 // index to the GOT target symbol index.
4705 return MCValue{ .memory = decl.link.macho.local_sym_index };
47344706 } else if (self.bin_file.cast(link.File.Coff)) |coff_file| {
47354707 const got_addr = coff_file.offset_table_virtual_address + decl.link.coff.offset_table_index * ptr_bytes;
47364708 return MCValue{ .memory = got_addr };
src/link/MachO.zig+41-179
......@@ -154,17 +154,13 @@ stub_preamble_sym_index: ?u32 = null,
154154strtab: std.ArrayListUnmanaged(u8) = .{},
155155strtab_dir: std.HashMapUnmanaged(u32, u32, StringIndexContext, std.hash_map.default_max_load_percentage) = .{},
156156
157got_entries: std.ArrayListUnmanaged(GotIndirectionKey) = .{},
158got_entries_map: std.AutoHashMapUnmanaged(GotIndirectionKey, u32) = .{},
159
160got_entries_free_list: std.ArrayListUnmanaged(u32) = .{},
157got_entries_map: std.AutoArrayHashMapUnmanaged(GotIndirectionKey, *TextBlock) = .{},
161158
162159stubs: std.ArrayListUnmanaged(u32) = .{},
163160stubs_map: std.AutoHashMapUnmanaged(u32, u32) = .{},
164161
165162error_flags: File.ErrorFlags = File.ErrorFlags{},
166163
167got_entries_count_dirty: bool = false,
168164load_commands_dirty: bool = false,
169165rebase_info_dirty: bool = false,
170166binding_info_dirty: bool = false,
......@@ -876,7 +872,6 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
876872 self.error_flags.no_entry_point_found = false;
877873 }
878874
879 assert(!self.got_entries_count_dirty);
880875 assert(!self.load_commands_dirty);
881876 assert(!self.rebase_info_dirty);
882877 assert(!self.binding_info_dirty);
......@@ -1731,16 +1726,9 @@ fn allocateTextSegment(self: *MachO) !void {
17311726
17321727fn allocateDataConstSegment(self: *MachO) !void {
17331728 const seg = &self.load_commands.items[self.data_const_segment_cmd_index.?].Segment;
1734 const nentries = @intCast(u32, self.got_entries.items.len);
1735
17361729 const text_seg = self.load_commands.items[self.text_segment_cmd_index.?].Segment;
17371730 seg.inner.fileoff = text_seg.inner.fileoff + text_seg.inner.filesize;
17381731 seg.inner.vmaddr = text_seg.inner.vmaddr + text_seg.inner.vmsize;
1739
1740 // Set got size
1741 const got = &seg.sections.items[self.got_section_index.?];
1742 got.size += nentries * @sizeOf(u64);
1743
17441732 try self.allocateSegment(self.data_const_segment_cmd_index.?, 0);
17451733}
17461734
......@@ -1927,7 +1915,7 @@ fn createEmptyAtom(self: *MachO, local_sym_index: u32, size: u64, alignment: u32
19271915 return atom;
19281916}
19291917
1930fn allocateAtom(self: *MachO, atom: *TextBlock, match: MatchingSection) !u64 {
1918pub fn allocateAtom(self: *MachO, atom: *TextBlock, match: MatchingSection) !u64 {
19311919 // TODO converge with `allocateTextBlock`
19321920 const seg = self.load_commands.items[match.seg].Segment;
19331921 const sect = seg.sections.items[match.sect];
......@@ -1936,7 +1924,8 @@ fn allocateAtom(self: *MachO, atom: *TextBlock, match: MatchingSection) !u64 {
19361924 const last_atom_sym = self.locals.items[last.local_sym_index];
19371925 break :blk last_atom_sym.n_value + last.size;
19381926 } else sect.addr;
1939 const atom_alignment = try math.powi(u32, 2, atom.alignment);
1927 // const atom_alignment = try math.powi(u32, 2, atom.alignment); TODO
1928 const atom_alignment = math.powi(u32, 2, atom.alignment) catch unreachable;
19401929 const vaddr = mem.alignForwardGeneric(u64, base_addr, atom_alignment);
19411930 log.debug("allocating atom for symbol {s} at address 0x{x}", .{ self.getString(sym.n_strx), vaddr });
19421931
......@@ -1956,7 +1945,7 @@ fn allocateAtom(self: *MachO, atom: *TextBlock, match: MatchingSection) !u64 {
19561945 return vaddr;
19571946}
19581947
1959fn writeAtom(self: *MachO, atom: *TextBlock, match: MatchingSection) !void {
1948pub fn writeAtom(self: *MachO, atom: *TextBlock, match: MatchingSection) !void {
19601949 const seg = self.load_commands.items[match.seg].Segment;
19611950 const sect = seg.sections.items[match.sect];
19621951 const sym = self.locals.items[atom.local_sym_index];
......@@ -1967,7 +1956,7 @@ fn writeAtom(self: *MachO, atom: *TextBlock, match: MatchingSection) !void {
19671956 try self.writeLocalSymbol(atom.local_sym_index);
19681957}
19691958
1970fn allocateAtomStage1(self: *MachO, atom: *TextBlock, match: MatchingSection) !void {
1959pub fn allocateAtomStage1(self: *MachO, atom: *TextBlock, match: MatchingSection) !void {
19711960 // Update target section's metadata
19721961 // TODO should we update segment's size here too?
19731962 // How does it tie with incremental space allocs?
......@@ -1988,7 +1977,7 @@ fn allocateAtomStage1(self: *MachO, atom: *TextBlock, match: MatchingSection) !v
19881977 }
19891978}
19901979
1991fn createGotAtom(self: *MachO, key: GotIndirectionKey) !*TextBlock {
1980pub fn createGotAtom(self: *MachO, key: GotIndirectionKey) !*TextBlock {
19921981 const local_sym_index = @intCast(u32, self.locals.items.len);
19931982 try self.locals.append(self.base.allocator, .{
19941983 .n_strx = try self.makeString("got_entry"),
......@@ -2804,15 +2793,12 @@ fn resolveDyldStubBinder(self: *MachO) !void {
28042793 }
28052794
28062795 // Add dyld_stub_binder as the final GOT entry.
2807 const got_index = @intCast(u32, self.got_entries.items.len);
28082796 const got_entry = GotIndirectionKey{
28092797 .where = .undef,
28102798 .where_index = self.dyld_stub_binder_index.?,
28112799 };
2812 try self.got_entries.append(self.base.allocator, got_entry);
2813 try self.got_entries_map.putNoClobber(self.base.allocator, got_entry, got_index);
2814
28152800 const atom = try self.createGotAtom(got_entry);
2801 try self.got_entries_map.putNoClobber(self.base.allocator, got_entry, atom);
28162802 const match = MatchingSection{
28172803 .seg = self.data_const_segment_cmd_index.?,
28182804 .sect = self.got_section_index.?,
......@@ -2917,7 +2903,6 @@ fn flushZld(self: *MachO) !void {
29172903 sect.offset = 0;
29182904 }
29192905
2920 try self.writeGotEntries();
29212906 try self.setEntryPoint();
29222907 try self.writeRebaseInfoTableZld();
29232908 try self.writeBindInfoTableZld();
......@@ -2952,29 +2937,6 @@ fn flushZld(self: *MachO) !void {
29522937 }
29532938}
29542939
2955fn writeGotEntries(self: *MachO) !void {
2956 const seg = self.load_commands.items[self.data_const_segment_cmd_index.?].Segment;
2957 const sect = seg.sections.items[self.got_section_index.?];
2958
2959 var buffer = try self.base.allocator.alloc(u8, self.got_entries.items.len * @sizeOf(u64));
2960 defer self.base.allocator.free(buffer);
2961
2962 var stream = std.io.fixedBufferStream(buffer);
2963 var writer = stream.writer();
2964
2965 for (self.got_entries.items) |key| {
2966 const address: u64 = switch (key.where) {
2967 .local => self.locals.items[key.where_index].n_value,
2968 .undef => 0,
2969 };
2970 try writer.writeIntLittle(u64, address);
2971 }
2972
2973 log.debug("writing GOT pointers at 0x{x} to 0x{x}", .{ sect.offset, sect.offset + buffer.len });
2974
2975 try self.base.file.?.pwriteAll(buffer, sect.offset);
2976}
2977
29782940fn setEntryPoint(self: *MachO) !void {
29792941 if (self.base.options.output_mode != .Exe) return;
29802942
......@@ -3028,22 +2990,6 @@ fn writeRebaseInfoTableZld(self: *MachO) !void {
30282990 }
30292991 }
30302992
3031 if (self.got_section_index) |idx| {
3032 const seg = self.load_commands.items[self.data_const_segment_cmd_index.?].Segment;
3033 const sect = seg.sections.items[idx];
3034 const base_offset = sect.addr - seg.inner.vmaddr;
3035 const segment_id = @intCast(u16, self.data_const_segment_cmd_index.?);
3036
3037 for (self.got_entries.items) |entry, i| {
3038 if (entry.where == .undef) continue;
3039
3040 try pointers.append(.{
3041 .offset = base_offset + i * @sizeOf(u64),
3042 .segment_id = segment_id,
3043 });
3044 }
3045 }
3046
30472993 std.sort.sort(bind.Pointer, pointers.items, {}, bind.pointerCmp);
30482994
30492995 const size = try bind.rebaseInfoSize(pointers.items);
......@@ -3068,25 +3014,6 @@ fn writeBindInfoTableZld(self: *MachO) !void {
30683014 var pointers = std.ArrayList(bind.Pointer).init(self.base.allocator);
30693015 defer pointers.deinit();
30703016
3071 if (self.got_section_index) |idx| {
3072 const seg = self.load_commands.items[self.data_const_segment_cmd_index.?].Segment;
3073 const sect = seg.sections.items[idx];
3074 const base_offset = sect.addr - seg.inner.vmaddr;
3075 const segment_id = @intCast(u16, self.data_const_segment_cmd_index.?);
3076
3077 for (self.got_entries.items) |entry, i| {
3078 if (entry.where == .local) continue;
3079
3080 const sym = self.undefs.items[entry.where_index];
3081 try pointers.append(.{
3082 .offset = base_offset + i * @sizeOf(u64),
3083 .segment_id = segment_id,
3084 .dylib_ordinal = @divExact(sym.n_desc, macho.N_SYMBOL_RESOLVER),
3085 .name = self.getString(sym.n_strx),
3086 });
3087 }
3088 }
3089
30903017 {
30913018 var it = self.blocks.iterator();
30923019 while (it.next()) |entry| {
......@@ -3319,7 +3246,7 @@ fn writeSymbolTable(self: *MachO) !void {
33193246 const la_symbol_ptr = &data_segment.sections.items[self.la_symbol_ptr_section_index.?];
33203247
33213248 const nstubs = @intCast(u32, self.stubs.items.len);
3322 const ngot_entries = @intCast(u32, self.got_entries.items.len);
3249 const ngot_entries = @intCast(u32, self.got_entries_map.keys().len);
33233250
33243251 dysymtab.indirectsymoff = @intCast(u32, seg.inner.fileoff + seg.inner.filesize);
33253252 dysymtab.nindirectsyms = nstubs * 2 + ngot_entries;
......@@ -3344,10 +3271,10 @@ fn writeSymbolTable(self: *MachO) !void {
33443271 }
33453272
33463273 got.reserved1 = nstubs;
3347 for (self.got_entries.items) |entry| {
3348 switch (entry.where) {
3274 for (self.got_entries_map.keys()) |key| {
3275 switch (key.where) {
33493276 .undef => {
3350 try writer.writeIntLittle(u32, dysymtab.iundefsym + entry.where_index);
3277 try writer.writeIntLittle(u32, dysymtab.iundefsym + key.where_index);
33513278 },
33523279 .local => {
33533280 try writer.writeIntLittle(u32, macho.INDIRECT_SYMBOL_LOCAL);
......@@ -3373,9 +3300,7 @@ pub fn deinit(self: *MachO) void {
33733300 }
33743301
33753302 self.section_ordinals.deinit(self.base.allocator);
3376 self.got_entries.deinit(self.base.allocator);
33773303 self.got_entries_map.deinit(self.base.allocator);
3378 self.got_entries_free_list.deinit(self.base.allocator);
33793304 self.stubs.deinit(self.base.allocator);
33803305 self.stubs_map.deinit(self.base.allocator);
33813306 self.strtab_dir.deinit(self.base.allocator);
......@@ -3539,8 +3464,6 @@ pub fn allocateDeclIndexes(self: *MachO, decl: *Module.Decl) !void {
35393464 if (decl.link.macho.local_sym_index != 0) return;
35403465
35413466 try self.locals.ensureUnusedCapacity(self.base.allocator, 1);
3542 try self.got_entries.ensureUnusedCapacity(self.base.allocator, 1);
3543
35443467 try self.decls.putNoClobber(self.base.allocator, decl, {});
35453468
35463469 if (self.locals_free_list.popOrNull()) |i| {
......@@ -3552,20 +3475,6 @@ pub fn allocateDeclIndexes(self: *MachO, decl: *Module.Decl) !void {
35523475 _ = self.locals.addOneAssumeCapacity();
35533476 }
35543477
3555 const got_index: u32 = blk: {
3556 if (self.got_entries_free_list.popOrNull()) |i| {
3557 log.debug("reusing GOT entry index {d} for {s}", .{ i, decl.name });
3558 break :blk i;
3559 } else {
3560 const got_index = @intCast(u32, self.got_entries.items.len);
3561 log.debug("allocating GOT entry index {d} for {s}", .{ got_index, decl.name });
3562 _ = self.got_entries.addOneAssumeCapacity();
3563 self.got_entries_count_dirty = true;
3564 self.rebase_info_dirty = true;
3565 break :blk got_index;
3566 }
3567 };
3568
35693478 self.locals.items[decl.link.macho.local_sym_index] = .{
35703479 .n_strx = 0,
35713480 .n_type = 0,
......@@ -3573,12 +3482,19 @@ pub fn allocateDeclIndexes(self: *MachO, decl: *Module.Decl) !void {
35733482 .n_desc = 0,
35743483 .n_value = 0,
35753484 };
3576 const got_entry = GotIndirectionKey{
3485
3486 // TODO try popping from free list first before allocating a new GOT atom.
3487 const key = GotIndirectionKey{
35773488 .where = .local,
35783489 .where_index = decl.link.macho.local_sym_index,
35793490 };
3580 self.got_entries.items[got_index] = got_entry;
3581 try self.got_entries_map.putNoClobber(self.base.allocator, got_entry, got_index);
3491 const got_atom = try self.createGotAtom(key);
3492 _ = try self.allocateAtom(got_atom, .{
3493 .seg = self.data_const_segment_cmd_index.?,
3494 .sect = self.got_section_index.?,
3495 });
3496 try self.got_entries_map.put(self.base.allocator, key, got_atom);
3497 self.rebase_info_dirty = true;
35823498}
35833499
35843500pub fn updateFunc(self: *MachO, module: *Module, func: *Module.Fn, air: Air, liveness: Liveness) !void {
......@@ -3760,11 +3676,16 @@ fn placeDecl(self: *MachO, decl: *Module.Decl, code_len: usize) !*macho.nlist_64
37603676
37613677 if (vaddr != symbol.n_value) {
37623678 log.debug(" (writing new GOT entry)", .{});
3763 const got_index = self.got_entries_map.get(.{
3679 const match = MatchingSection{
3680 .seg = self.data_const_segment_cmd_index.?,
3681 .sect = self.got_section_index.?,
3682 };
3683 const got_atom = self.got_entries_map.get(.{
37643684 .where = .local,
37653685 .where_index = decl.link.macho.local_sym_index,
37663686 }) orelse unreachable;
3767 try self.writeGotEntry(got_index);
3687 // _ = try self.allocateAtom(got_atom, match);
3688 try self.writeAtom(got_atom, match);
37683689 }
37693690
37703691 symbol.n_value = vaddr;
......@@ -3802,11 +3723,16 @@ fn placeDecl(self: *MachO, decl: *Module.Decl, code_len: usize) !*macho.nlist_64
38023723 .n_desc = 0,
38033724 .n_value = addr,
38043725 };
3805 const got_index = self.got_entries_map.get(.{
3726 const match = MatchingSection{
3727 .seg = self.data_const_segment_cmd_index.?,
3728 .sect = self.got_section_index.?,
3729 };
3730 const got_atom = self.got_entries_map.get(.{
38063731 .where = .local,
38073732 .where_index = decl.link.macho.local_sym_index,
38083733 }) orelse unreachable;
3809 try self.writeGotEntry(got_index);
3734 // _ = try self.allocateAtom(got_atom, match);
3735 try self.writeAtom(got_atom, match);
38103736
38113737 try self.writeLocalSymbol(decl.link.macho.local_sym_index);
38123738 if (self.d_sym) |*ds|
......@@ -3955,13 +3881,7 @@ pub fn freeDecl(self: *MachO, decl: *Module.Decl) void {
39553881 if (decl.link.macho.local_sym_index != 0) {
39563882 self.locals_free_list.append(self.base.allocator, decl.link.macho.local_sym_index) catch {};
39573883
3958 const got_key = GotIndirectionKey{
3959 .where = .local,
3960 .where_index = decl.link.macho.local_sym_index,
3961 };
3962 const got_index = self.got_entries_map.get(got_key) orelse unreachable;
3963 _ = self.got_entries_map.remove(got_key);
3964 self.got_entries_free_list.append(self.base.allocator, got_index) catch {};
3884 // TODO free GOT atom here.
39653885
39663886 self.locals.items[decl.link.macho.local_sym_index].n_type = 0;
39673887 decl.link.macho.local_sym_index = 0;
......@@ -4789,29 +4709,6 @@ fn findFreeSpaceLinkedit(self: *MachO, object_size: u64, min_alignment: u16, sta
47894709 return st;
47904710}
47914711
4792fn writeGotEntry(self: *MachO, index: usize) !void {
4793 const seg = &self.load_commands.items[self.data_const_segment_cmd_index.?].Segment;
4794 const sect = &seg.sections.items[self.got_section_index.?];
4795 const off = sect.offset + @sizeOf(u64) * index;
4796
4797 if (self.got_entries_count_dirty) {
4798 // TODO relocate.
4799 self.got_entries_count_dirty = false;
4800 }
4801
4802 const got_entry = self.got_entries.items[index];
4803 const sym = switch (got_entry.where) {
4804 .local => self.locals.items[got_entry.where_index],
4805 .undef => self.undefs.items[got_entry.where_index],
4806 };
4807 log.debug("writing offset table entry [ 0x{x} => 0x{x} ({s}) ]", .{
4808 off,
4809 sym.n_value,
4810 self.getString(sym.n_strx),
4811 });
4812 try self.base.file.?.pwriteAll(mem.asBytes(&sym.n_value), off);
4813}
4814
48154712fn relocateSymbolTable(self: *MachO) !void {
48164713 const symtab = &self.load_commands.items[self.symtab_cmd_index.?].Symtab;
48174714 const nlocals = self.locals.items.len;
......@@ -4901,7 +4798,7 @@ fn writeIndirectSymbolTable(self: *MachO) !void {
49014798 const dysymtab = &self.load_commands.items[self.dysymtab_cmd_index.?].Dysymtab;
49024799
49034800 const nstubs = @intCast(u32, self.stubs.items.len);
4904 const ngot_entries = @intCast(u32, self.got_entries.items.len);
4801 const ngot_entries = @intCast(u32, self.got_entries_map.keys().len);
49054802 const allocated_size = self.allocatedSizeLinkedit(dysymtab.indirectsymoff);
49064803 const nindirectsyms = nstubs * 2 + ngot_entries;
49074804 const needed_size = @intCast(u32, nindirectsyms * @sizeOf(u32));
......@@ -4927,10 +4824,10 @@ fn writeIndirectSymbolTable(self: *MachO) !void {
49274824 }
49284825
49294826 got.reserved1 = nstubs;
4930 for (self.got_entries.items) |entry| {
4931 switch (entry.where) {
4827 for (self.got_entries_map.keys()) |key| {
4828 switch (key.where) {
49324829 .undef => {
4933 try writer.writeIntLittle(u32, dysymtab.iundefsym + entry.where_index);
4830 try writer.writeIntLittle(u32, dysymtab.iundefsym + key.where_index);
49344831 },
49354832 .local => {
49364833 try writer.writeIntLittle(u32, macho.INDIRECT_SYMBOL_LOCAL);
......@@ -5147,22 +5044,6 @@ fn writeRebaseInfoTable(self: *MachO) !void {
51475044 }
51485045 }
51495046
5150 if (self.got_section_index) |idx| {
5151 const seg = self.load_commands.items[self.data_const_segment_cmd_index.?].Segment;
5152 const sect = seg.sections.items[idx];
5153 const base_offset = sect.addr - seg.inner.vmaddr;
5154 const segment_id = @intCast(u16, self.data_const_segment_cmd_index.?);
5155
5156 for (self.got_entries.items) |entry, i| {
5157 if (entry.where == .undef) continue;
5158
5159 try pointers.append(.{
5160 .offset = base_offset + i * @sizeOf(u64),
5161 .segment_id = segment_id,
5162 });
5163 }
5164 }
5165
51665047 std.sort.sort(bind.Pointer, pointers.items, {}, bind.pointerCmp);
51675048
51685049 const size = try bind.rebaseInfoSize(pointers.items);
......@@ -5199,25 +5080,6 @@ fn writeBindInfoTable(self: *MachO) !void {
51995080 var pointers = std.ArrayList(bind.Pointer).init(self.base.allocator);
52005081 defer pointers.deinit();
52015082
5202 if (self.got_section_index) |idx| {
5203 const seg = self.load_commands.items[self.data_const_segment_cmd_index.?].Segment;
5204 const sect = seg.sections.items[idx];
5205 const base_offset = sect.addr - seg.inner.vmaddr;
5206 const segment_id = @intCast(u16, self.data_const_segment_cmd_index.?);
5207
5208 for (self.got_entries.items) |entry, i| {
5209 if (entry.where == .local) continue;
5210
5211 const sym = self.undefs.items[entry.where_index];
5212 try pointers.append(.{
5213 .offset = base_offset + i * @sizeOf(u64),
5214 .segment_id = segment_id,
5215 .dylib_ordinal = @divExact(sym.n_desc, macho.N_SYMBOL_RESOLVER),
5216 .name = self.getString(sym.n_strx),
5217 });
5218 }
5219 }
5220
52215083 {
52225084 var it = self.blocks.iterator();
52235085 while (it.next()) |entry| {
src/link/MachO/TextBlock.zig+15-7
......@@ -1,6 +1,7 @@
11const TextBlock = @This();
22
33const std = @import("std");
4const build_options = @import("build_options");
45const aarch64 = @import("../../codegen/aarch64.zig");
56const assert = std.debug.assert;
67const commands = @import("commands.zig");
......@@ -830,9 +831,18 @@ pub fn parseRelocs(self: *TextBlock, relocs: []macho.relocation_info, context: R
830831 };
831832 if (context.macho_file.got_entries_map.contains(key)) break :blk;
832833
833 const got_index = @intCast(u32, context.macho_file.got_entries.items.len);
834 try context.macho_file.got_entries.append(context.allocator, key);
835 try context.macho_file.got_entries_map.putNoClobber(context.allocator, key, got_index);
834 const atom = try context.macho_file.createGotAtom(key);
835 try context.macho_file.got_entries_map.putNoClobber(context.macho_file.base.allocator, key, atom);
836 const match = MachO.MatchingSection{
837 .seg = context.macho_file.data_const_segment_cmd_index.?,
838 .sect = context.macho_file.got_section_index.?,
839 };
840 if (!(build_options.is_stage1 and context.macho_file.base.options.use_stage1)) {
841 _ = try context.macho_file.allocateAtom(atom, match);
842 try context.macho_file.writeAtom(atom, match);
843 } else {
844 try context.macho_file.allocateAtomStage1(atom, match);
845 }
836846 } else if (parsed_rel.payload == .unsigned) {
837847 switch (parsed_rel.where) {
838848 .undef => {
......@@ -1082,9 +1092,7 @@ pub fn resolveRelocs(self: *TextBlock, macho_file: *MachO) !void {
10821092 };
10831093
10841094 if (is_via_got) {
1085 const dc_seg = macho_file.load_commands.items[macho_file.data_const_segment_cmd_index.?].Segment;
1086 const got = dc_seg.sections.items[macho_file.got_section_index.?];
1087 const got_index = macho_file.got_entries_map.get(.{
1095 const atom = macho_file.got_entries_map.get(.{
10881096 .where = switch (rel.where) {
10891097 .local => .local,
10901098 .undef => .undef,
......@@ -1099,7 +1107,7 @@ pub fn resolveRelocs(self: *TextBlock, macho_file: *MachO) !void {
10991107 log.err(" this is an internal linker error", .{});
11001108 return error.FailedToResolveRelocationTarget;
11011109 };
1102 break :blk got.addr + got_index * @sizeOf(u64);
1110 break :blk macho_file.locals.items[atom.local_sym_index].n_value;
11031111 }
11041112
11051113 switch (rel.where) {