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 {
21622162 const header = &self.sections.items(.header)[idx];
21632163 header.size = self.stubs.size(self);
21642164 header.@"align" = switch (cpu_arch) {
2165 .x86_64 => 0,
2165 .x86_64 => 1,
21662166 .aarch64 => 2,
21672167 else => 0,
21682168 };
......@@ -2171,11 +2171,7 @@ fn calcSectionSizes(self: *MachO) !void {
21712171 if (self.stubs_helper_sect_index) |idx| {
21722172 const header = &self.sections.items(.header)[idx];
21732173 header.size = self.stubs_helper.size(self);
2174 header.@"align" = switch (cpu_arch) {
2175 .x86_64 => 0,
2176 .aarch64 => 2,
2177 else => 0,
2178 };
2174 header.@"align" = 2;
21792175 }
21802176
21812177 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
223223 out.n_type = if (symbol.flags.abs) macho.N_ABS else macho.N_SECT;
224224 out.n_sect = if (symbol.flags.abs) 0 else @intCast(symbol.out_n_sect + 1);
225225 out.n_desc = 0;
226 out.n_value = symbol.getAddress(.{}, macho_file);
226 out.n_value = symbol.getAddress(.{ .stubs = false }, macho_file);
227227
228228 switch (symbol.visibility) {
229229 .hidden => out.n_type |= macho.N_PEXT,
......@@ -234,7 +234,7 @@ pub fn setOutputSym(symbol: Symbol, macho_file: *MachO, out: *macho.nlist_64) vo
234234 out.n_type = macho.N_EXT;
235235 out.n_type |= if (symbol.flags.abs) macho.N_ABS else macho.N_SECT;
236236 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);
238238 out.n_desc = 0;
239239
240240 if (symbol.flags.weak) {
src/link/MachO/dyld_info/Rebase.zig+1-1
......@@ -12,7 +12,7 @@ const Allocator = std.mem.Allocator;
1212entries: std.ArrayListUnmanaged(Entry) = .{},
1313buffer: std.ArrayListUnmanaged(u8) = .{},
1414
15const Entry = struct {
15pub const Entry = struct {
1616 offset: u64,
1717 segment_id: u8,
1818
src/link/MachO/synthetic.zig+23-15
......@@ -315,7 +315,7 @@ pub const StubsSection = struct {
315315pub const StubsHelperSection = struct {
316316 pub inline fn preambleSize(cpu_arch: std.Target.Cpu.Arch) usize {
317317 return switch (cpu_arch) {
318 .x86_64 => 15,
318 .x86_64 => 16,
319319 .aarch64 => 6 * @sizeOf(u32),
320320 else => 0,
321321 };
......@@ -408,6 +408,7 @@ pub const StubsHelperSection = struct {
408408 try writer.writeInt(i32, @intCast(dyld_private_addr - sect.addr - 3 - 4), .little);
409409 try writer.writeAll(&.{ 0x41, 0x53, 0xff, 0x25 });
410410 try writer.writeInt(i32, @intCast(dyld_stub_binder_addr - sect.addr - 11 - 4), .little);
411 try writer.writeByte(0x90);
411412 },
412413 .aarch64 => {
413414 {
......@@ -460,7 +461,11 @@ pub const LaSymbolPtrSection = struct {
460461 for (macho_file.stubs.symbols.items, 0..) |sym_index, idx| {
461462 const sym = macho_file.getSymbol(sym_index);
462463 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{
464469 .target = sym_index,
465470 .offset = addr - seg.vmaddr,
466471 .segment_id = seg_id,
......@@ -468,20 +473,19 @@ pub const LaSymbolPtrSection = struct {
468473 };
469474 if (sym.flags.import) {
470475 if (sym.flags.weak) {
471 try macho_file.bind.entries.append(gpa, entry);
472 try macho_file.weak_bind.entries.append(gpa, entry);
476 try macho_file.bind.entries.append(gpa, bind_entry);
477 try macho_file.weak_bind.entries.append(gpa, bind_entry);
473478 } 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);
475481 }
476482 } else {
477483 if (sym.flags.weak) {
478 try macho_file.rebase.entries.append(gpa, .{
479 .offset = addr - seg.vmaddr,
480 .segment_id = seg_id,
481 });
482 try macho_file.weak_bind.entries.append(gpa, entry);
484 try macho_file.rebase.entries.append(gpa, rebase_entry);
485 try macho_file.weak_bind.entries.append(gpa, bind_entry);
483486 } 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);
485489 }
486490 }
487491 }
......@@ -493,15 +497,19 @@ pub const LaSymbolPtrSection = struct {
493497 _ = laptr;
494498 const cpu_arch = macho_file.getTarget().cpu.arch;
495499 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| {
497502 const sym = macho_file.getSymbol(sym_index);
498503 const value: u64 = if (sym.flags.@"export")
499504 sym.getAddress(.{ .stubs = false }, macho_file)
500505 else if (sym.flags.weak)
501506 @as(u64, 0)
502 else
503 sect.addr + StubsHelperSection.preambleSize(cpu_arch) +
504 StubsHelperSection.entrySize(cpu_arch) * idx;
507 else value: {
508 const value = sect.addr + StubsHelperSection.preambleSize(cpu_arch) +
509 StubsHelperSection.entrySize(cpu_arch) * stub_helper_idx;
510 stub_helper_idx += 1;
511 break :value value;
512 };
505513 try writer.writeInt(u64, @intCast(value), .little);
506514 }
507515 }