| ... | ... | @@ -337,9 +337,8 @@ pub const StubsHelperSection = struct { |
| 337 | 337 | var s: usize = preambleSize(cpu_arch); |
| 338 | 338 | for (macho_file.stubs.symbols.items) |sym_index| { |
| 339 | 339 | const sym = macho_file.getSymbol(sym_index); |
| 340 | | if ((sym.flags.import and !sym.flags.weak) or (!sym.flags.weak and sym.flags.interposable)) { |
| 341 | | s += entrySize(cpu_arch); |
| 342 | | } |
| 340 | if (sym.flags.weak) continue; |
| 341 | s += entrySize(cpu_arch); |
| 343 | 342 | } |
| 344 | 343 | return s; |
| 345 | 344 | } |
| ... | ... | @@ -358,35 +357,34 @@ pub const StubsHelperSection = struct { |
| 358 | 357 | var idx: usize = 0; |
| 359 | 358 | for (macho_file.stubs.symbols.items) |sym_index| { |
| 360 | 359 | const sym = macho_file.getSymbol(sym_index); |
| 361 | | if ((sym.flags.import and !sym.flags.weak) or (!sym.flags.weak and sym.flags.interposable)) { |
| 362 | | const offset = macho_file.lazy_bind.offsets.items[idx]; |
| 363 | | const source: i64 = @intCast(sect.addr + preamble_size + entry_size * idx); |
| 364 | | const target: i64 = @intCast(sect.addr); |
| 365 | | switch (cpu_arch) { |
| 366 | | .x86_64 => { |
| 367 | | try writer.writeByte(0x68); |
| 368 | | try writer.writeInt(u32, offset, .little); |
| 369 | | try writer.writeByte(0xe9); |
| 370 | | try writer.writeInt(i32, @intCast(target - source - 6 - 4), .little); |
| 371 | | }, |
| 372 | | .aarch64 => { |
| 373 | | const literal = blk: { |
| 374 | | const div_res = try std.math.divExact(u64, entry_size - @sizeOf(u32), 4); |
| 375 | | break :blk std.math.cast(u18, div_res) orelse return error.Overflow; |
| 376 | | }; |
| 377 | | try writer.writeInt(u32, aarch64.Instruction.ldrLiteral( |
| 378 | | .w16, |
| 379 | | literal, |
| 380 | | ).toU32(), .little); |
| 381 | | const disp = math.cast(i28, @as(i64, @intCast(target)) - @as(i64, @intCast(source + 4))) orelse |
| 382 | | return error.Overflow; |
| 383 | | try writer.writeInt(u32, aarch64.Instruction.b(disp).toU32(), .little); |
| 384 | | try writer.writeAll(&.{ 0x0, 0x0, 0x0, 0x0 }); |
| 385 | | }, |
| 386 | | else => unreachable, |
| 387 | | } |
| 388 | | idx += 1; |
| 360 | if (sym.flags.weak) continue; |
| 361 | const offset = macho_file.lazy_bind.offsets.items[idx]; |
| 362 | const source: i64 = @intCast(sect.addr + preamble_size + entry_size * idx); |
| 363 | const target: i64 = @intCast(sect.addr); |
| 364 | switch (cpu_arch) { |
| 365 | .x86_64 => { |
| 366 | try writer.writeByte(0x68); |
| 367 | try writer.writeInt(u32, offset, .little); |
| 368 | try writer.writeByte(0xe9); |
| 369 | try writer.writeInt(i32, @intCast(target - source - 6 - 4), .little); |
| 370 | }, |
| 371 | .aarch64 => { |
| 372 | const literal = blk: { |
| 373 | const div_res = try std.math.divExact(u64, entry_size - @sizeOf(u32), 4); |
| 374 | break :blk std.math.cast(u18, div_res) orelse return error.Overflow; |
| 375 | }; |
| 376 | try writer.writeInt(u32, aarch64.Instruction.ldrLiteral( |
| 377 | .w16, |
| 378 | literal, |
| 379 | ).toU32(), .little); |
| 380 | const disp = math.cast(i28, @as(i64, @intCast(target)) - @as(i64, @intCast(source + 4))) orelse |
| 381 | return error.Overflow; |
| 382 | try writer.writeInt(u32, aarch64.Instruction.b(disp).toU32(), .little); |
| 383 | try writer.writeAll(&.{ 0x0, 0x0, 0x0, 0x0 }); |
| 384 | }, |
| 385 | else => unreachable, |
| 389 | 386 | } |
| 387 | idx += 1; |
| 390 | 388 | } |
| 391 | 389 | } |
| 392 | 390 | |
| ... | ... | @@ -500,17 +498,15 @@ pub const LaSymbolPtrSection = struct { |
| 500 | 498 | var stub_helper_idx: u32 = 0; |
| 501 | 499 | for (macho_file.stubs.symbols.items) |sym_index| { |
| 502 | 500 | const sym = macho_file.getSymbol(sym_index); |
| 503 | | const value: u64 = if (sym.flags.@"export") |
| 504 | | sym.getAddress(.{ .stubs = false }, macho_file) |
| 505 | | else if (sym.flags.weak) |
| 506 | | @as(u64, 0) |
| 507 | | else value: { |
| 501 | if (sym.flags.weak) { |
| 502 | const value = sym.getAddress(.{ .stubs = false }, macho_file); |
| 503 | try writer.writeInt(u64, @intCast(value), .little); |
| 504 | } else { |
| 508 | 505 | const value = sect.addr + StubsHelperSection.preambleSize(cpu_arch) + |
| 509 | 506 | StubsHelperSection.entrySize(cpu_arch) * stub_helper_idx; |
| 510 | 507 | stub_helper_idx += 1; |
| 511 | | break :value value; |
| 512 | | }; |
| 513 | | try writer.writeInt(u64, @intCast(value), .little); |
| 508 | try writer.writeInt(u64, @intCast(value), .little); |
| 509 | } |
| 514 | 510 | } |
| 515 | 511 | } |
| 516 | 512 | }; |