authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-01-24 00:32:32+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-01-24 12:34:42+01:00
log508ff1dd144b86e7e561636ebe4ad4410462981a
tree940f22b5c0def3d3fe824bd441d22f70e883a33f
parent8fd4c36bf90baf8d2f7614e61b779aa75c66ee90

macho: add misc fixes targeting macos 11


4 files changed, 28 insertions(+), 24 deletions(-)

src/link/MachO.zig+2-6
...@@ -2162,7 +2162,7 @@ fn calcSectionSizes(self: *MachO) !void {...@@ -2162,7 +2162,7 @@ fn calcSectionSizes(self: *MachO) !void {
2162 const header = &self.sections.items(.header)[idx];2162 const header = &self.sections.items(.header)[idx];
2163 header.size = self.stubs.size(self);2163 header.size = self.stubs.size(self);
2164 header.@"align" = switch (cpu_arch) {2164 header.@"align" = switch (cpu_arch) {
2165 .x86_64 => 0,2165 .x86_64 => 1,
2166 .aarch64 => 2,2166 .aarch64 => 2,
2167 else => 0,2167 else => 0,
2168 };2168 };
...@@ -2171,11 +2171,7 @@ fn calcSectionSizes(self: *MachO) !void {...@@ -2171,11 +2171,7 @@ fn calcSectionSizes(self: *MachO) !void {
2171 if (self.stubs_helper_sect_index) |idx| {2171 if (self.stubs_helper_sect_index) |idx| {
2172 const header = &self.sections.items(.header)[idx];2172 const header = &self.sections.items(.header)[idx];
2173 header.size = self.stubs_helper.size(self);2173 header.size = self.stubs_helper.size(self);
2174 header.@"align" = switch (cpu_arch) {2174 header.@"align" = 2;
2175 .x86_64 => 0,
2176 .aarch64 => 2,
2177 else => 0,
2178 };
2179 }2175 }
21802176
2181 if (self.la_symbol_ptr_sect_index) |idx| {2177 if (self.la_symbol_ptr_sect_index) |idx| {
src/link/MachO/Symbol.zig+2-2
...@@ -223,7 +223,7 @@ pub fn setOutputSym(symbol: Symbol, macho_file: *MachO, out: *macho.nlist_64) vo...@@ -223,7 +223,7 @@ pub fn setOutputSym(symbol: Symbol, macho_file: *MachO, out: *macho.nlist_64) vo
223 out.n_type = if (symbol.flags.abs) macho.N_ABS else macho.N_SECT;223 out.n_type = if (symbol.flags.abs) macho.N_ABS else macho.N_SECT;
224 out.n_sect = if (symbol.flags.abs) 0 else @intCast(symbol.out_n_sect + 1);224 out.n_sect = if (symbol.flags.abs) 0 else @intCast(symbol.out_n_sect + 1);
225 out.n_desc = 0;225 out.n_desc = 0;
226 out.n_value = symbol.getAddress(.{}, macho_file);226 out.n_value = symbol.getAddress(.{ .stubs = false }, macho_file);
227227
228 switch (symbol.visibility) {228 switch (symbol.visibility) {
229 .hidden => out.n_type |= macho.N_PEXT,229 .hidden => out.n_type |= macho.N_PEXT,
...@@ -234,7 +234,7 @@ pub fn setOutputSym(symbol: Symbol, macho_file: *MachO, out: *macho.nlist_64) vo...@@ -234,7 +234,7 @@ pub fn setOutputSym(symbol: Symbol, macho_file: *MachO, out: *macho.nlist_64) vo
234 out.n_type = macho.N_EXT;234 out.n_type = macho.N_EXT;
235 out.n_type |= if (symbol.flags.abs) macho.N_ABS else macho.N_SECT;235 out.n_type |= if (symbol.flags.abs) macho.N_ABS else macho.N_SECT;
236 out.n_sect = if (symbol.flags.abs) 0 else @intCast(symbol.out_n_sect + 1);236 out.n_sect = if (symbol.flags.abs) 0 else @intCast(symbol.out_n_sect + 1);
237 out.n_value = symbol.getAddress(.{}, macho_file);237 out.n_value = symbol.getAddress(.{ .stubs = false }, macho_file);
238 out.n_desc = 0;238 out.n_desc = 0;
239239
240 if (symbol.flags.weak) {240 if (symbol.flags.weak) {
src/link/MachO/dyld_info/Rebase.zig+1-1
...@@ -12,7 +12,7 @@ const Allocator = std.mem.Allocator;...@@ -12,7 +12,7 @@ const Allocator = std.mem.Allocator;
12entries: std.ArrayListUnmanaged(Entry) = .{},12entries: std.ArrayListUnmanaged(Entry) = .{},
13buffer: std.ArrayListUnmanaged(u8) = .{},13buffer: std.ArrayListUnmanaged(u8) = .{},
1414
15const Entry = struct {15pub const Entry = struct {
16 offset: u64,16 offset: u64,
17 segment_id: u8,17 segment_id: u8,
1818
src/link/MachO/synthetic.zig+23-15
...@@ -315,7 +315,7 @@ pub const StubsSection = struct {...@@ -315,7 +315,7 @@ pub const StubsSection = struct {
315pub const StubsHelperSection = struct {315pub const StubsHelperSection = struct {
316 pub inline fn preambleSize(cpu_arch: std.Target.Cpu.Arch) usize {316 pub inline fn preambleSize(cpu_arch: std.Target.Cpu.Arch) usize {
317 return switch (cpu_arch) {317 return switch (cpu_arch) {
318 .x86_64 => 15,318 .x86_64 => 16,
319 .aarch64 => 6 * @sizeOf(u32),319 .aarch64 => 6 * @sizeOf(u32),
320 else => 0,320 else => 0,
321 };321 };
...@@ -408,6 +408,7 @@ pub const StubsHelperSection = struct {...@@ -408,6 +408,7 @@ pub const StubsHelperSection = struct {
408 try writer.writeInt(i32, @intCast(dyld_private_addr - sect.addr - 3 - 4), .little);408 try writer.writeInt(i32, @intCast(dyld_private_addr - sect.addr - 3 - 4), .little);
409 try writer.writeAll(&.{ 0x41, 0x53, 0xff, 0x25 });409 try writer.writeAll(&.{ 0x41, 0x53, 0xff, 0x25 });
410 try writer.writeInt(i32, @intCast(dyld_stub_binder_addr - sect.addr - 11 - 4), .little);410 try writer.writeInt(i32, @intCast(dyld_stub_binder_addr - sect.addr - 11 - 4), .little);
411 try writer.writeByte(0x90);
411 },412 },
412 .aarch64 => {413 .aarch64 => {
413 {414 {
...@@ -460,7 +461,11 @@ pub const LaSymbolPtrSection = struct {...@@ -460,7 +461,11 @@ pub const LaSymbolPtrSection = struct {
460 for (macho_file.stubs.symbols.items, 0..) |sym_index, idx| {461 for (macho_file.stubs.symbols.items, 0..) |sym_index, idx| {
461 const sym = macho_file.getSymbol(sym_index);462 const sym = macho_file.getSymbol(sym_index);
462 const addr = sect.addr + idx * @sizeOf(u64);463 const addr = sect.addr + idx * @sizeOf(u64);
463 const entry = bind.Entry{464 const rebase_entry = Rebase.Entry{
465 .offset = addr - seg.vmaddr,
466 .segment_id = seg_id,
467 };
468 const bind_entry = bind.Entry{
464 .target = sym_index,469 .target = sym_index,
465 .offset = addr - seg.vmaddr,470 .offset = addr - seg.vmaddr,
466 .segment_id = seg_id,471 .segment_id = seg_id,
...@@ -468,20 +473,19 @@ pub const LaSymbolPtrSection = struct {...@@ -468,20 +473,19 @@ pub const LaSymbolPtrSection = struct {
468 };473 };
469 if (sym.flags.import) {474 if (sym.flags.import) {
470 if (sym.flags.weak) {475 if (sym.flags.weak) {
471 try macho_file.bind.entries.append(gpa, entry);476 try macho_file.bind.entries.append(gpa, bind_entry);
472 try macho_file.weak_bind.entries.append(gpa, entry);477 try macho_file.weak_bind.entries.append(gpa, bind_entry);
473 } else {478 } else {
474 try macho_file.lazy_bind.entries.append(gpa, entry);479 try macho_file.lazy_bind.entries.append(gpa, bind_entry);
480 try macho_file.rebase.entries.append(gpa, rebase_entry);
475 }481 }
476 } else {482 } else {
477 if (sym.flags.weak) {483 if (sym.flags.weak) {
478 try macho_file.rebase.entries.append(gpa, .{484 try macho_file.rebase.entries.append(gpa, rebase_entry);
479 .offset = addr - seg.vmaddr,485 try macho_file.weak_bind.entries.append(gpa, bind_entry);
480 .segment_id = seg_id,
481 });
482 try macho_file.weak_bind.entries.append(gpa, entry);
483 } else if (sym.flags.interposable) {486 } else if (sym.flags.interposable) {
484 try macho_file.lazy_bind.entries.append(gpa, entry);487 try macho_file.lazy_bind.entries.append(gpa, bind_entry);
488 try macho_file.rebase.entries.append(gpa, rebase_entry);
485 }489 }
486 }490 }
487 }491 }
...@@ -493,15 +497,19 @@ pub const LaSymbolPtrSection = struct {...@@ -493,15 +497,19 @@ pub const LaSymbolPtrSection = struct {
493 _ = laptr;497 _ = laptr;
494 const cpu_arch = macho_file.getTarget().cpu.arch;498 const cpu_arch = macho_file.getTarget().cpu.arch;
495 const sect = macho_file.sections.items(.header)[macho_file.stubs_helper_sect_index.?];499 const sect = macho_file.sections.items(.header)[macho_file.stubs_helper_sect_index.?];
496 for (macho_file.stubs.symbols.items, 0..) |sym_index, idx| {500 var stub_helper_idx: u32 = 0;
501 for (macho_file.stubs.symbols.items) |sym_index| {
497 const sym = macho_file.getSymbol(sym_index);502 const sym = macho_file.getSymbol(sym_index);
498 const value: u64 = if (sym.flags.@"export")503 const value: u64 = if (sym.flags.@"export")
499 sym.getAddress(.{ .stubs = false }, macho_file)504 sym.getAddress(.{ .stubs = false }, macho_file)
500 else if (sym.flags.weak)505 else if (sym.flags.weak)
501 @as(u64, 0)506 @as(u64, 0)
502 else507 else value: {
503 sect.addr + StubsHelperSection.preambleSize(cpu_arch) +508 const value = sect.addr + StubsHelperSection.preambleSize(cpu_arch) +
504 StubsHelperSection.entrySize(cpu_arch) * idx;509 StubsHelperSection.entrySize(cpu_arch) * stub_helper_idx;
510 stub_helper_idx += 1;
511 break :value value;
512 };
505 try writer.writeInt(u64, @intCast(value), .little);513 try writer.writeInt(u64, @intCast(value), .little);
506 }514 }
507 }515 }