| author | |
| committer | |
| log | 989b0e620be22278d32ec848069c17667cbd2f4f |
| tree | db956d0f30778310fa0e164a128e1d4aa4a6b20b |
| parent | efbb6128bbfdd0b731578dcc55d4a55732d20d8a |
| parent | bcd16b270861e814e59c699837b1e35db84ab091 |
| signature |
macho+zld: add improved dyld opcodes emitters7 files changed, 1465 insertions(+), 451 deletions(-)
CMakeLists.txt+2-1| ... | ... | @@ -594,7 +594,8 @@ set(ZIG_STAGE2_SOURCES |
| 594 | 594 | "${CMAKE_SOURCE_DIR}/src/link/MachO/Relocation.zig" |
| 595 | 595 | "${CMAKE_SOURCE_DIR}/src/link/MachO/Trie.zig" |
| 596 | 596 | "${CMAKE_SOURCE_DIR}/src/link/MachO/ZldAtom.zig" |
| 597 | "${CMAKE_SOURCE_DIR}/src/link/MachO/bind.zig" | |
| 597 | "${CMAKE_SOURCE_DIR}/src/link/MachO/dyld_info/bind.zig" | |
| 598 | "${CMAKE_SOURCE_DIR}/src/link/MachO/dyld_info/Rebase.zig" | |
| 598 | 599 | "${CMAKE_SOURCE_DIR}/src/link/MachO/dead_strip.zig" |
| 599 | 600 | "${CMAKE_SOURCE_DIR}/src/link/MachO/fat.zig" |
| 600 | 601 | "${CMAKE_SOURCE_DIR}/src/link/MachO/load_commands.zig" |
src/link/MachO.zig+59-151| ... | ... | @@ -14,7 +14,6 @@ const mem = std.mem; |
| 14 | 14 | const meta = std.meta; |
| 15 | 15 | |
| 16 | 16 | const aarch64 = @import("../arch/aarch64/bits.zig"); |
| 17 | const bind = @import("MachO/bind.zig"); | |
| 18 | 17 | const codegen = @import("../codegen.zig"); |
| 19 | 18 | const dead_strip = @import("MachO/dead_strip.zig"); |
| 20 | 19 | const fat = @import("MachO/fat.zig"); |
| ... | ... | @@ -50,6 +49,10 @@ const Value = @import("../value.zig").Value; |
| 50 | 49 | |
| 51 | 50 | pub const DebugSymbols = @import("MachO/DebugSymbols.zig"); |
| 52 | 51 | |
| 52 | const Bind = @import("MachO/dyld_info/bind.zig").Bind(*const MachO, MachO.SymbolWithLoc); | |
| 53 | const LazyBind = @import("MachO/dyld_info/bind.zig").LazyBind(*const MachO, MachO.SymbolWithLoc); | |
| 54 | const Rebase = @import("MachO/dyld_info/Rebase.zig"); | |
| 55 | ||
| 53 | 56 | pub const base_tag: File.Tag = File.Tag.macho; |
| 54 | 57 | |
| 55 | 58 | pub const SearchStrategy = enum { |
| ... | ... | @@ -3192,32 +3195,14 @@ fn writeLinkeditSegmentData(self: *MachO) !void { |
| 3192 | 3195 | seg.vmsize = mem.alignForwardGeneric(u64, seg.filesize, self.page_size); |
| 3193 | 3196 | } |
| 3194 | 3197 | |
| 3195 | const AtomLessThanByAddressContext = struct { | |
| 3196 | macho_file: *MachO, | |
| 3197 | }; | |
| 3198 | ||
| 3199 | fn atomLessThanByAddress(ctx: AtomLessThanByAddressContext, lhs: *Atom, rhs: *Atom) bool { | |
| 3200 | return lhs.getSymbol(ctx.macho_file).n_value < rhs.getSymbol(ctx.macho_file).n_value; | |
| 3201 | } | |
| 3202 | ||
| 3203 | fn collectRebaseData(self: *MachO, pointers: *std.ArrayList(bind.Pointer)) !void { | |
| 3198 | fn collectRebaseData(self: *MachO, rebase: *Rebase) !void { | |
| 3204 | 3199 | const gpa = self.base.allocator; |
| 3205 | ||
| 3206 | var sorted_atoms_by_address = std.ArrayList(*Atom).init(gpa); | |
| 3207 | defer sorted_atoms_by_address.deinit(); | |
| 3208 | try sorted_atoms_by_address.ensureTotalCapacityPrecise(self.rebases.count()); | |
| 3209 | ||
| 3200 | const slice = self.sections.slice(); | |
| 3210 | 3201 | var it = self.rebases.keyIterator(); |
| 3211 | while (it.next()) |key_ptr| { | |
| 3212 | sorted_atoms_by_address.appendAssumeCapacity(key_ptr.*); | |
| 3213 | } | |
| 3214 | 3202 | |
| 3215 | std.sort.sort(*Atom, sorted_atoms_by_address.items, AtomLessThanByAddressContext{ | |
| 3216 | .macho_file = self, | |
| 3217 | }, atomLessThanByAddress); | |
| 3203 | while (it.next()) |key_ptr| { | |
| 3204 | const atom = key_ptr.*; | |
| 3218 | 3205 | |
| 3219 | const slice = self.sections.slice(); | |
| 3220 | for (sorted_atoms_by_address.items) |atom| { | |
| 3221 | 3206 | log.debug(" ATOM(%{d}, '{s}')", .{ atom.sym_index, atom.getName(self) }); |
| 3222 | 3207 | |
| 3223 | 3208 | const sym = atom.getSymbol(self); |
| ... | ... | @@ -3227,36 +3212,29 @@ fn collectRebaseData(self: *MachO, pointers: *std.ArrayList(bind.Pointer)) !void |
| 3227 | 3212 | const base_offset = sym.n_value - seg.vmaddr; |
| 3228 | 3213 | |
| 3229 | 3214 | const rebases = self.rebases.get(atom).?; |
| 3230 | try pointers.ensureUnusedCapacity(rebases.items.len); | |
| 3215 | try rebase.entries.ensureUnusedCapacity(gpa, rebases.items.len); | |
| 3216 | ||
| 3231 | 3217 | for (rebases.items) |offset| { |
| 3232 | 3218 | log.debug(" | rebase at {x}", .{base_offset + offset}); |
| 3233 | 3219 | |
| 3234 | pointers.appendAssumeCapacity(.{ | |
| 3220 | rebase.entries.appendAssumeCapacity(.{ | |
| 3235 | 3221 | .offset = base_offset + offset, |
| 3236 | 3222 | .segment_id = segment_index, |
| 3237 | 3223 | }); |
| 3238 | 3224 | } |
| 3239 | 3225 | } |
| 3226 | ||
| 3227 | try rebase.finalize(gpa); | |
| 3240 | 3228 | } |
| 3241 | 3229 | |
| 3242 | fn collectBindData(self: *MachO, pointers: *std.ArrayList(bind.Pointer), raw_bindings: anytype) !void { | |
| 3230 | fn collectBindData(self: *MachO, bind: anytype, raw_bindings: anytype) !void { | |
| 3243 | 3231 | const gpa = self.base.allocator; |
| 3244 | ||
| 3245 | var sorted_atoms_by_address = std.ArrayList(*Atom).init(gpa); | |
| 3246 | defer sorted_atoms_by_address.deinit(); | |
| 3247 | try sorted_atoms_by_address.ensureTotalCapacityPrecise(raw_bindings.count()); | |
| 3248 | ||
| 3232 | const slice = self.sections.slice(); | |
| 3249 | 3233 | var it = raw_bindings.keyIterator(); |
| 3250 | while (it.next()) |key_ptr| { | |
| 3251 | sorted_atoms_by_address.appendAssumeCapacity(key_ptr.*); | |
| 3252 | } | |
| 3253 | 3234 | |
| 3254 | std.sort.sort(*Atom, sorted_atoms_by_address.items, AtomLessThanByAddressContext{ | |
| 3255 | .macho_file = self, | |
| 3256 | }, atomLessThanByAddress); | |
| 3235 | while (it.next()) |key_ptr| { | |
| 3236 | const atom = key_ptr.*; | |
| 3257 | 3237 | |
| 3258 | const slice = self.sections.slice(); | |
| 3259 | for (sorted_atoms_by_address.items) |atom| { | |
| 3260 | 3238 | log.debug(" ATOM(%{d}, '{s}')", .{ atom.sym_index, atom.getName(self) }); |
| 3261 | 3239 | |
| 3262 | 3240 | const sym = atom.getSymbol(self); |
| ... | ... | @@ -3266,7 +3244,8 @@ fn collectBindData(self: *MachO, pointers: *std.ArrayList(bind.Pointer), raw_bin |
| 3266 | 3244 | const base_offset = sym.n_value - seg.vmaddr; |
| 3267 | 3245 | |
| 3268 | 3246 | const bindings = raw_bindings.get(atom).?; |
| 3269 | try pointers.ensureUnusedCapacity(bindings.items.len); | |
| 3247 | try bind.entries.ensureUnusedCapacity(gpa, bindings.items.len); | |
| 3248 | ||
| 3270 | 3249 | for (bindings.items) |binding| { |
| 3271 | 3250 | const bind_sym = self.getSymbol(binding.target); |
| 3272 | 3251 | const bind_sym_name = self.getSymbolName(binding.target); |
| ... | ... | @@ -3274,7 +3253,6 @@ fn collectBindData(self: *MachO, pointers: *std.ArrayList(bind.Pointer), raw_bin |
| 3274 | 3253 | @bitCast(i16, bind_sym.n_desc), |
| 3275 | 3254 | macho.N_SYMBOL_RESOLVER, |
| 3276 | 3255 | ); |
| 3277 | var flags: u4 = 0; | |
| 3278 | 3256 | log.debug(" | bind at {x}, import('{s}') in dylib({d})", .{ |
| 3279 | 3257 | binding.offset + base_offset, |
| 3280 | 3258 | bind_sym_name, |
| ... | ... | @@ -3282,17 +3260,17 @@ fn collectBindData(self: *MachO, pointers: *std.ArrayList(bind.Pointer), raw_bin |
| 3282 | 3260 | }); |
| 3283 | 3261 | if (bind_sym.weakRef()) { |
| 3284 | 3262 | log.debug(" | marking as weak ref ", .{}); |
| 3285 | flags |= @truncate(u4, macho.BIND_SYMBOL_FLAGS_WEAK_IMPORT); | |
| 3286 | 3263 | } |
| 3287 | pointers.appendAssumeCapacity(.{ | |
| 3264 | bind.entries.appendAssumeCapacity(.{ | |
| 3265 | .target = binding.target, | |
| 3288 | 3266 | .offset = binding.offset + base_offset, |
| 3289 | 3267 | .segment_id = segment_index, |
| 3290 | .dylib_ordinal = dylib_ordinal, | |
| 3291 | .name = bind_sym_name, | |
| 3292 | .bind_flags = flags, | |
| 3268 | .addend = 0, | |
| 3293 | 3269 | }); |
| 3294 | 3270 | } |
| 3295 | 3271 | } |
| 3272 | ||
| 3273 | try bind.finalize(gpa, self); | |
| 3296 | 3274 | } |
| 3297 | 3275 | |
| 3298 | 3276 | fn collectExportData(self: *MachO, trie: *Trie) !void { |
| ... | ... | @@ -3345,17 +3323,17 @@ fn writeDyldInfoData(self: *MachO) !void { |
| 3345 | 3323 | |
| 3346 | 3324 | const gpa = self.base.allocator; |
| 3347 | 3325 | |
| 3348 | var rebase_pointers = std.ArrayList(bind.Pointer).init(gpa); | |
| 3349 | defer rebase_pointers.deinit(); | |
| 3350 | try self.collectRebaseData(&rebase_pointers); | |
| 3326 | var rebase = Rebase{}; | |
| 3327 | defer rebase.deinit(gpa); | |
| 3328 | try self.collectRebaseData(&rebase); | |
| 3351 | 3329 | |
| 3352 | var bind_pointers = std.ArrayList(bind.Pointer).init(gpa); | |
| 3353 | defer bind_pointers.deinit(); | |
| 3354 | try self.collectBindData(&bind_pointers, self.bindings); | |
| 3330 | var bind = Bind{}; | |
| 3331 | defer bind.deinit(gpa); | |
| 3332 | try self.collectBindData(&bind, self.bindings); | |
| 3355 | 3333 | |
| 3356 | var lazy_bind_pointers = std.ArrayList(bind.Pointer).init(gpa); | |
| 3357 | defer lazy_bind_pointers.deinit(); | |
| 3358 | try self.collectBindData(&lazy_bind_pointers, self.lazy_bindings); | |
| 3334 | var lazy_bind = LazyBind{}; | |
| 3335 | defer lazy_bind.deinit(gpa); | |
| 3336 | try self.collectBindData(&lazy_bind, self.lazy_bindings); | |
| 3359 | 3337 | |
| 3360 | 3338 | var trie: Trie = .{}; |
| 3361 | 3339 | defer trie.deinit(gpa); |
| ... | ... | @@ -3364,17 +3342,17 @@ fn writeDyldInfoData(self: *MachO) !void { |
| 3364 | 3342 | const link_seg = self.getLinkeditSegmentPtr(); |
| 3365 | 3343 | assert(mem.isAlignedGeneric(u64, link_seg.fileoff, @alignOf(u64))); |
| 3366 | 3344 | const rebase_off = link_seg.fileoff; |
| 3367 | const rebase_size = try bind.rebaseInfoSize(rebase_pointers.items); | |
| 3345 | const rebase_size = rebase.size(); | |
| 3368 | 3346 | const rebase_size_aligned = mem.alignForwardGeneric(u64, rebase_size, @alignOf(u64)); |
| 3369 | 3347 | log.debug("writing rebase info from 0x{x} to 0x{x}", .{ rebase_off, rebase_off + rebase_size_aligned }); |
| 3370 | 3348 | |
| 3371 | 3349 | const bind_off = rebase_off + rebase_size_aligned; |
| 3372 | const bind_size = try bind.bindInfoSize(bind_pointers.items); | |
| 3350 | const bind_size = bind.size(); | |
| 3373 | 3351 | const bind_size_aligned = mem.alignForwardGeneric(u64, bind_size, @alignOf(u64)); |
| 3374 | 3352 | log.debug("writing bind info from 0x{x} to 0x{x}", .{ bind_off, bind_off + bind_size_aligned }); |
| 3375 | 3353 | |
| 3376 | 3354 | const lazy_bind_off = bind_off + bind_size_aligned; |
| 3377 | const lazy_bind_size = try bind.lazyBindInfoSize(lazy_bind_pointers.items); | |
| 3355 | const lazy_bind_size = lazy_bind.size(); | |
| 3378 | 3356 | const lazy_bind_size_aligned = mem.alignForwardGeneric(u64, lazy_bind_size, @alignOf(u64)); |
| 3379 | 3357 | log.debug("writing lazy bind info from 0x{x} to 0x{x}", .{ |
| 3380 | 3358 | lazy_bind_off, |
| ... | ... | @@ -3398,13 +3376,13 @@ fn writeDyldInfoData(self: *MachO) !void { |
| 3398 | 3376 | var stream = std.io.fixedBufferStream(buffer); |
| 3399 | 3377 | const writer = stream.writer(); |
| 3400 | 3378 | |
| 3401 | try bind.writeRebaseInfo(rebase_pointers.items, writer); | |
| 3379 | try rebase.write(writer); | |
| 3402 | 3380 | try stream.seekTo(bind_off - rebase_off); |
| 3403 | 3381 | |
| 3404 | try bind.writeBindInfo(bind_pointers.items, writer); | |
| 3382 | try bind.write(writer); | |
| 3405 | 3383 | try stream.seekTo(lazy_bind_off - rebase_off); |
| 3406 | 3384 | |
| 3407 | try bind.writeLazyBindInfo(lazy_bind_pointers.items, writer); | |
| 3385 | try lazy_bind.write(writer); | |
| 3408 | 3386 | try stream.seekTo(export_off - rebase_off); |
| 3409 | 3387 | |
| 3410 | 3388 | _ = try trie.write(writer); |
| ... | ... | @@ -3415,9 +3393,7 @@ fn writeDyldInfoData(self: *MachO) !void { |
| 3415 | 3393 | }); |
| 3416 | 3394 | |
| 3417 | 3395 | try self.base.file.?.pwriteAll(buffer, rebase_off); |
| 3418 | const start = math.cast(usize, lazy_bind_off - rebase_off) orelse return error.Overflow; | |
| 3419 | const end = start + (math.cast(usize, lazy_bind_size) orelse return error.Overflow); | |
| 3420 | try self.populateLazyBindOffsetsInStubHelper(buffer[start..end]); | |
| 3396 | try self.populateLazyBindOffsetsInStubHelper(lazy_bind); | |
| 3421 | 3397 | |
| 3422 | 3398 | self.dyld_info_cmd.rebase_off = @intCast(u32, rebase_off); |
| 3423 | 3399 | self.dyld_info_cmd.rebase_size = @intCast(u32, rebase_size_aligned); |
| ... | ... | @@ -3429,102 +3405,33 @@ fn writeDyldInfoData(self: *MachO) !void { |
| 3429 | 3405 | self.dyld_info_cmd.export_size = @intCast(u32, export_size_aligned); |
| 3430 | 3406 | } |
| 3431 | 3407 | |
| 3432 | fn populateLazyBindOffsetsInStubHelper(self: *MachO, buffer: []const u8) !void { | |
| 3433 | const gpa = self.base.allocator; | |
| 3408 | fn populateLazyBindOffsetsInStubHelper(self: *MachO, lazy_bind: LazyBind) !void { | |
| 3409 | if (lazy_bind.size() == 0) return; | |
| 3434 | 3410 | |
| 3435 | const stub_helper_section_index = self.stub_helper_section_index orelse return; | |
| 3436 | if (self.stub_helper_preamble_atom == null) return; | |
| 3411 | const stub_helper_section_index = self.stub_helper_section_index.?; | |
| 3412 | assert(self.stub_helper_preamble_atom != null); | |
| 3437 | 3413 | |
| 3438 | 3414 | const section = self.sections.get(stub_helper_section_index); |
| 3439 | const last_atom = section.last_atom orelse return; | |
| 3440 | if (last_atom == self.stub_helper_preamble_atom.?) return; // TODO is this a redundant check? | |
| 3441 | ||
| 3442 | var table = std.AutoHashMap(i64, *Atom).init(gpa); | |
| 3443 | defer table.deinit(); | |
| 3444 | 3415 | |
| 3445 | { | |
| 3446 | var stub_atom = last_atom; | |
| 3447 | var laptr_atom = self.sections.items(.last_atom)[self.la_symbol_ptr_section_index.?].?; | |
| 3448 | const base_addr = self.getSegment(self.la_symbol_ptr_section_index.?).vmaddr; | |
| 3449 | ||
| 3450 | while (true) { | |
| 3451 | const laptr_off = blk: { | |
| 3452 | const sym = laptr_atom.getSymbol(self); | |
| 3453 | break :blk @intCast(i64, sym.n_value - base_addr); | |
| 3454 | }; | |
| 3455 | try table.putNoClobber(laptr_off, stub_atom); | |
| 3456 | if (laptr_atom.prev) |prev| { | |
| 3457 | laptr_atom = prev; | |
| 3458 | stub_atom = stub_atom.prev.?; | |
| 3459 | } else break; | |
| 3460 | } | |
| 3461 | } | |
| 3462 | ||
| 3463 | var stream = std.io.fixedBufferStream(buffer); | |
| 3464 | var reader = stream.reader(); | |
| 3465 | var offsets = std.ArrayList(struct { sym_offset: i64, offset: u32 }).init(gpa); | |
| 3466 | try offsets.append(.{ .sym_offset = undefined, .offset = 0 }); | |
| 3467 | defer offsets.deinit(); | |
| 3468 | var valid_block = false; | |
| 3469 | ||
| 3470 | while (true) { | |
| 3471 | const inst = reader.readByte() catch |err| switch (err) { | |
| 3472 | error.EndOfStream => break, | |
| 3473 | }; | |
| 3474 | const opcode: u8 = inst & macho.BIND_OPCODE_MASK; | |
| 3475 | ||
| 3476 | switch (opcode) { | |
| 3477 | macho.BIND_OPCODE_DO_BIND => { | |
| 3478 | valid_block = true; | |
| 3479 | }, | |
| 3480 | macho.BIND_OPCODE_DONE => { | |
| 3481 | if (valid_block) { | |
| 3482 | const offset = try stream.getPos(); | |
| 3483 | try offsets.append(.{ .sym_offset = undefined, .offset = @intCast(u32, offset) }); | |
| 3484 | } | |
| 3485 | valid_block = false; | |
| 3486 | }, | |
| 3487 | macho.BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM => { | |
| 3488 | var next = try reader.readByte(); | |
| 3489 | while (next != @as(u8, 0)) { | |
| 3490 | next = try reader.readByte(); | |
| 3491 | } | |
| 3492 | }, | |
| 3493 | macho.BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB => { | |
| 3494 | var inserted = offsets.pop(); | |
| 3495 | inserted.sym_offset = try std.leb.readILEB128(i64, reader); | |
| 3496 | try offsets.append(inserted); | |
| 3497 | }, | |
| 3498 | macho.BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB => { | |
| 3499 | _ = try std.leb.readULEB128(u64, reader); | |
| 3500 | }, | |
| 3501 | macho.BIND_OPCODE_SET_ADDEND_SLEB => { | |
| 3502 | _ = try std.leb.readILEB128(i64, reader); | |
| 3503 | }, | |
| 3504 | else => {}, | |
| 3505 | } | |
| 3506 | } | |
| 3507 | ||
| 3508 | const header = self.sections.items(.header)[stub_helper_section_index]; | |
| 3509 | 3416 | const stub_offset: u4 = switch (self.base.options.target.cpu.arch) { |
| 3510 | 3417 | .x86_64 => 1, |
| 3511 | 3418 | .aarch64 => 2 * @sizeOf(u32), |
| 3512 | 3419 | else => unreachable, |
| 3513 | 3420 | }; |
| 3514 | var buf: [@sizeOf(u32)]u8 = undefined; | |
| 3515 | _ = offsets.pop(); | |
| 3421 | const header = section.header; | |
| 3422 | var atom = section.last_atom.?; | |
| 3516 | 3423 | |
| 3517 | while (offsets.popOrNull()) |bind_offset| { | |
| 3518 | const atom = table.get(bind_offset.sym_offset).?; | |
| 3424 | var index: usize = 0; | |
| 3425 | while (index < lazy_bind.offsets.items.len) : (index += 1) { | |
| 3519 | 3426 | const sym = atom.getSymbol(self); |
| 3520 | 3427 | const file_offset = header.offset + sym.n_value - header.addr + stub_offset; |
| 3521 | mem.writeIntLittle(u32, &buf, bind_offset.offset); | |
| 3522 | log.debug("writing lazy bind offset in stub helper of 0x{x} for symbol {s} at offset 0x{x}", .{ | |
| 3523 | bind_offset.offset, | |
| 3524 | atom.getName(self), | |
| 3525 | file_offset, | |
| 3526 | }); | |
| 3527 | try self.base.file.?.pwriteAll(&buf, file_offset); | |
| 3428 | const bind_offset = lazy_bind.offsets.items[index]; | |
| 3429 | ||
| 3430 | log.debug("writing lazy bind offset 0x{x} in stub helper at 0x{x}", .{ bind_offset, file_offset }); | |
| 3431 | ||
| 3432 | try self.base.file.?.pwriteAll(mem.asBytes(&bind_offset), file_offset); | |
| 3433 | ||
| 3434 | atom = atom.prev.?; | |
| 3528 | 3435 | } |
| 3529 | 3436 | } |
| 3530 | 3437 | |
| ... | ... | @@ -3912,12 +3819,13 @@ pub fn getSymbolPtr(self: *MachO, sym_with_loc: SymbolWithLoc) *macho.nlist_64 { |
| 3912 | 3819 | } |
| 3913 | 3820 | |
| 3914 | 3821 | /// Returns symbol described by `sym_with_loc` descriptor. |
| 3915 | pub fn getSymbol(self: *MachO, sym_with_loc: SymbolWithLoc) macho.nlist_64 { | |
| 3916 | return self.getSymbolPtr(sym_with_loc).*; | |
| 3822 | pub fn getSymbol(self: *const MachO, sym_with_loc: SymbolWithLoc) macho.nlist_64 { | |
| 3823 | assert(sym_with_loc.file == null); | |
| 3824 | return self.locals.items[sym_with_loc.sym_index]; | |
| 3917 | 3825 | } |
| 3918 | 3826 | |
| 3919 | 3827 | /// Returns name of the symbol described by `sym_with_loc` descriptor. |
| 3920 | pub fn getSymbolName(self: *MachO, sym_with_loc: SymbolWithLoc) []const u8 { | |
| 3828 | pub fn getSymbolName(self: *const MachO, sym_with_loc: SymbolWithLoc) []const u8 { | |
| 3921 | 3829 | assert(sym_with_loc.file == null); |
| 3922 | 3830 | const sym = self.locals.items[sym_with_loc.sym_index]; |
| 3923 | 3831 | return self.strtab.get(sym.n_strx).?; |
src/link/MachO/bind.zig deleted-138| ... | ... | @@ -1,138 +0,0 @@ |
| 1 | const std = @import("std"); | |
| 2 | const leb = std.leb; | |
| 3 | const macho = std.macho; | |
| 4 | ||
| 5 | pub const Pointer = struct { | |
| 6 | offset: u64, | |
| 7 | segment_id: u16, | |
| 8 | dylib_ordinal: ?i64 = null, | |
| 9 | name: ?[]const u8 = null, | |
| 10 | bind_flags: u4 = 0, | |
| 11 | }; | |
| 12 | ||
| 13 | pub fn rebaseInfoSize(pointers: []const Pointer) !u64 { | |
| 14 | var stream = std.io.countingWriter(std.io.null_writer); | |
| 15 | var writer = stream.writer(); | |
| 16 | var size: u64 = 0; | |
| 17 | ||
| 18 | for (pointers) |pointer| { | |
| 19 | size += 2; | |
| 20 | try leb.writeILEB128(writer, pointer.offset); | |
| 21 | size += 1; | |
| 22 | } | |
| 23 | ||
| 24 | size += 1 + stream.bytes_written; | |
| 25 | return size; | |
| 26 | } | |
| 27 | ||
| 28 | pub fn writeRebaseInfo(pointers: []const Pointer, writer: anytype) !void { | |
| 29 | for (pointers) |pointer| { | |
| 30 | try writer.writeByte(macho.REBASE_OPCODE_SET_TYPE_IMM | @truncate(u4, macho.REBASE_TYPE_POINTER)); | |
| 31 | try writer.writeByte(macho.REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB | @truncate(u4, pointer.segment_id)); | |
| 32 | ||
| 33 | try leb.writeILEB128(writer, pointer.offset); | |
| 34 | try writer.writeByte(macho.REBASE_OPCODE_DO_REBASE_IMM_TIMES | @truncate(u4, 1)); | |
| 35 | } | |
| 36 | try writer.writeByte(macho.REBASE_OPCODE_DONE); | |
| 37 | } | |
| 38 | ||
| 39 | pub fn bindInfoSize(pointers: []const Pointer) !u64 { | |
| 40 | var stream = std.io.countingWriter(std.io.null_writer); | |
| 41 | var writer = stream.writer(); | |
| 42 | var size: u64 = 0; | |
| 43 | ||
| 44 | for (pointers) |pointer| { | |
| 45 | size += 1; | |
| 46 | if (pointer.dylib_ordinal.? > 15) { | |
| 47 | try leb.writeULEB128(writer, @bitCast(u64, pointer.dylib_ordinal.?)); | |
| 48 | } | |
| 49 | size += 1; | |
| 50 | ||
| 51 | size += 1; | |
| 52 | size += pointer.name.?.len; | |
| 53 | size += 1; | |
| 54 | ||
| 55 | size += 1; | |
| 56 | ||
| 57 | try leb.writeILEB128(writer, pointer.offset); | |
| 58 | size += 1; | |
| 59 | } | |
| 60 | ||
| 61 | size += stream.bytes_written + 1; | |
| 62 | return size; | |
| 63 | } | |
| 64 | ||
| 65 | pub fn writeBindInfo(pointers: []const Pointer, writer: anytype) !void { | |
| 66 | for (pointers) |pointer| { | |
| 67 | if (pointer.dylib_ordinal.? > 15) { | |
| 68 | try writer.writeByte(macho.BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB); | |
| 69 | try leb.writeULEB128(writer, @bitCast(u64, pointer.dylib_ordinal.?)); | |
| 70 | } else if (pointer.dylib_ordinal.? > 0) { | |
| 71 | try writer.writeByte(macho.BIND_OPCODE_SET_DYLIB_ORDINAL_IMM | @truncate(u4, @bitCast(u64, pointer.dylib_ordinal.?))); | |
| 72 | } else { | |
| 73 | try writer.writeByte(macho.BIND_OPCODE_SET_DYLIB_SPECIAL_IMM | @truncate(u4, @bitCast(u64, pointer.dylib_ordinal.?))); | |
| 74 | } | |
| 75 | try writer.writeByte(macho.BIND_OPCODE_SET_TYPE_IMM | @truncate(u4, macho.BIND_TYPE_POINTER)); | |
| 76 | ||
| 77 | try writer.writeByte(macho.BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM | pointer.bind_flags); | |
| 78 | try writer.writeAll(pointer.name.?); | |
| 79 | try writer.writeByte(0); | |
| 80 | ||
| 81 | try writer.writeByte(macho.BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB | @truncate(u4, pointer.segment_id)); | |
| 82 | ||
| 83 | try leb.writeILEB128(writer, pointer.offset); | |
| 84 | try writer.writeByte(macho.BIND_OPCODE_DO_BIND); | |
| 85 | } | |
| 86 | ||
| 87 | try writer.writeByte(macho.BIND_OPCODE_DONE); | |
| 88 | } | |
| 89 | ||
| 90 | pub fn lazyBindInfoSize(pointers: []const Pointer) !u64 { | |
| 91 | var stream = std.io.countingWriter(std.io.null_writer); | |
| 92 | var writer = stream.writer(); | |
| 93 | var size: u64 = 0; | |
| 94 | ||
| 95 | for (pointers) |pointer| { | |
| 96 | size += 1; | |
| 97 | ||
| 98 | try leb.writeILEB128(writer, pointer.offset); | |
| 99 | ||
| 100 | size += 1; | |
| 101 | if (pointer.dylib_ordinal.? > 15) { | |
| 102 | try leb.writeULEB128(writer, @bitCast(u64, pointer.dylib_ordinal.?)); | |
| 103 | } | |
| 104 | ||
| 105 | size += 1; | |
| 106 | size += pointer.name.?.len; | |
| 107 | size += 1; | |
| 108 | ||
| 109 | size += 2; | |
| 110 | } | |
| 111 | ||
| 112 | size += stream.bytes_written; | |
| 113 | return size; | |
| 114 | } | |
| 115 | ||
| 116 | pub fn writeLazyBindInfo(pointers: []const Pointer, writer: anytype) !void { | |
| 117 | for (pointers) |pointer| { | |
| 118 | try writer.writeByte(macho.BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB | @truncate(u4, pointer.segment_id)); | |
| 119 | ||
| 120 | try leb.writeILEB128(writer, pointer.offset); | |
| 121 | ||
| 122 | if (pointer.dylib_ordinal.? > 15) { | |
| 123 | try writer.writeByte(macho.BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB); | |
| 124 | try leb.writeULEB128(writer, @bitCast(u64, pointer.dylib_ordinal.?)); | |
| 125 | } else if (pointer.dylib_ordinal.? > 0) { | |
| 126 | try writer.writeByte(macho.BIND_OPCODE_SET_DYLIB_ORDINAL_IMM | @truncate(u4, @bitCast(u64, pointer.dylib_ordinal.?))); | |
| 127 | } else { | |
| 128 | try writer.writeByte(macho.BIND_OPCODE_SET_DYLIB_SPECIAL_IMM | @truncate(u4, @bitCast(u64, pointer.dylib_ordinal.?))); | |
| 129 | } | |
| 130 | ||
| 131 | try writer.writeByte(macho.BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM | pointer.bind_flags); | |
| 132 | try writer.writeAll(pointer.name.?); | |
| 133 | try writer.writeByte(0); | |
| 134 | ||
| 135 | try writer.writeByte(macho.BIND_OPCODE_DO_BIND); | |
| 136 | try writer.writeByte(macho.BIND_OPCODE_DONE); | |
| 137 | } | |
| 138 | } |
src/link/MachO/dyld_info/Rebase.zig created+574| ... | ... | @@ -0,0 +1,574 @@ |
| 1 | const Rebase = @This(); | |
| 2 | ||
| 3 | const std = @import("std"); | |
| 4 | const assert = std.debug.assert; | |
| 5 | const leb = std.leb; | |
| 6 | const log = std.log.scoped(.dyld_info); | |
| 7 | const macho = std.macho; | |
| 8 | const testing = std.testing; | |
| 9 | ||
| 10 | const Allocator = std.mem.Allocator; | |
| 11 | ||
| 12 | entries: std.ArrayListUnmanaged(Entry) = .{}, | |
| 13 | buffer: std.ArrayListUnmanaged(u8) = .{}, | |
| 14 | ||
| 15 | const Entry = struct { | |
| 16 | offset: u64, | |
| 17 | segment_id: u8, | |
| 18 | ||
| 19 | pub fn lessThan(ctx: void, entry: Entry, other: Entry) bool { | |
| 20 | _ = ctx; | |
| 21 | if (entry.segment_id == other.segment_id) { | |
| 22 | return entry.offset < other.offset; | |
| 23 | } | |
| 24 | return entry.segment_id < other.segment_id; | |
| 25 | } | |
| 26 | }; | |
| 27 | ||
| 28 | pub fn deinit(rebase: *Rebase, gpa: Allocator) void { | |
| 29 | rebase.entries.deinit(gpa); | |
| 30 | rebase.buffer.deinit(gpa); | |
| 31 | } | |
| 32 | ||
| 33 | pub fn size(rebase: Rebase) u64 { | |
| 34 | return @intCast(u64, rebase.buffer.items.len); | |
| 35 | } | |
| 36 | ||
| 37 | pub fn finalize(rebase: *Rebase, gpa: Allocator) !void { | |
| 38 | if (rebase.entries.items.len == 0) return; | |
| 39 | ||
| 40 | const writer = rebase.buffer.writer(gpa); | |
| 41 | ||
| 42 | std.sort.sort(Entry, rebase.entries.items, {}, Entry.lessThan); | |
| 43 | ||
| 44 | try setTypePointer(writer); | |
| 45 | ||
| 46 | var start: usize = 0; | |
| 47 | var seg_id: ?u8 = null; | |
| 48 | for (rebase.entries.items) |entry, i| { | |
| 49 | if (seg_id != null and seg_id.? == entry.segment_id) continue; | |
| 50 | try finalizeSegment(rebase.entries.items[start..i], writer); | |
| 51 | seg_id = entry.segment_id; | |
| 52 | start = i; | |
| 53 | } | |
| 54 | ||
| 55 | try finalizeSegment(rebase.entries.items[start..], writer); | |
| 56 | try done(writer); | |
| 57 | } | |
| 58 | ||
| 59 | fn finalizeSegment(entries: []const Entry, writer: anytype) !void { | |
| 60 | if (entries.len == 0) return; | |
| 61 | ||
| 62 | const segment_id = entries[0].segment_id; | |
| 63 | var offset = entries[0].offset; | |
| 64 | try setSegmentOffset(segment_id, offset, writer); | |
| 65 | ||
| 66 | var count: usize = 0; | |
| 67 | var skip: u64 = 0; | |
| 68 | var state: enum { | |
| 69 | start, | |
| 70 | times, | |
| 71 | times_skip, | |
| 72 | } = .times; | |
| 73 | ||
| 74 | var i: usize = 0; | |
| 75 | while (i < entries.len) : (i += 1) { | |
| 76 | log.debug("{x}, {d}, {x}, {s}", .{ offset, count, skip, @tagName(state) }); | |
| 77 | const current_offset = entries[i].offset; | |
| 78 | log.debug(" => {x}", .{current_offset}); | |
| 79 | switch (state) { | |
| 80 | .start => { | |
| 81 | if (offset < current_offset) { | |
| 82 | const delta = current_offset - offset; | |
| 83 | try addAddr(delta, writer); | |
| 84 | offset += delta; | |
| 85 | } | |
| 86 | state = .times; | |
| 87 | offset += @sizeOf(u64); | |
| 88 | count = 1; | |
| 89 | }, | |
| 90 | .times => { | |
| 91 | const delta = current_offset - offset; | |
| 92 | if (delta == 0) { | |
| 93 | count += 1; | |
| 94 | offset += @sizeOf(u64); | |
| 95 | continue; | |
| 96 | } | |
| 97 | if (count == 1) { | |
| 98 | state = .times_skip; | |
| 99 | skip = delta; | |
| 100 | offset += skip; | |
| 101 | i -= 1; | |
| 102 | } else { | |
| 103 | try rebaseTimes(count, writer); | |
| 104 | state = .start; | |
| 105 | i -= 1; | |
| 106 | } | |
| 107 | }, | |
| 108 | .times_skip => { | |
| 109 | if (current_offset < offset) { | |
| 110 | count -= 1; | |
| 111 | if (count == 1) { | |
| 112 | try rebaseAddAddr(skip, writer); | |
| 113 | } else { | |
| 114 | try rebaseTimesSkip(count, skip, writer); | |
| 115 | } | |
| 116 | state = .start; | |
| 117 | offset = offset - (@sizeOf(u64) + skip); | |
| 118 | i -= 2; | |
| 119 | continue; | |
| 120 | } | |
| 121 | ||
| 122 | const delta = current_offset - offset; | |
| 123 | if (delta == 0) { | |
| 124 | count += 1; | |
| 125 | offset += @sizeOf(u64) + skip; | |
| 126 | } else { | |
| 127 | try rebaseTimesSkip(count, skip, writer); | |
| 128 | state = .start; | |
| 129 | i -= 1; | |
| 130 | } | |
| 131 | }, | |
| 132 | } | |
| 133 | } | |
| 134 | ||
| 135 | switch (state) { | |
| 136 | .start => unreachable, | |
| 137 | .times => { | |
| 138 | try rebaseTimes(count, writer); | |
| 139 | }, | |
| 140 | .times_skip => { | |
| 141 | try rebaseTimesSkip(count, skip, writer); | |
| 142 | }, | |
| 143 | } | |
| 144 | } | |
| 145 | ||
| 146 | fn setTypePointer(writer: anytype) !void { | |
| 147 | log.debug(">>> set type: {d}", .{macho.REBASE_TYPE_POINTER}); | |
| 148 | try writer.writeByte(macho.REBASE_OPCODE_SET_TYPE_IMM | @truncate(u4, macho.REBASE_TYPE_POINTER)); | |
| 149 | } | |
| 150 | ||
| 151 | fn setSegmentOffset(segment_id: u8, offset: u64, writer: anytype) !void { | |
| 152 | log.debug(">>> set segment: {d} and offset: {x}", .{ segment_id, offset }); | |
| 153 | try writer.writeByte(macho.REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB | @truncate(u4, segment_id)); | |
| 154 | try std.leb.writeULEB128(writer, offset); | |
| 155 | } | |
| 156 | ||
| 157 | fn rebaseAddAddr(addr: u64, writer: anytype) !void { | |
| 158 | log.debug(">>> rebase with add: {x}", .{addr}); | |
| 159 | try writer.writeByte(macho.REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB); | |
| 160 | try std.leb.writeULEB128(writer, addr); | |
| 161 | } | |
| 162 | ||
| 163 | fn rebaseTimes(count: usize, writer: anytype) !void { | |
| 164 | log.debug(">>> rebase with count: {d}", .{count}); | |
| 165 | if (count <= 0xf) { | |
| 166 | try writer.writeByte(macho.REBASE_OPCODE_DO_REBASE_IMM_TIMES | @truncate(u4, count)); | |
| 167 | } else { | |
| 168 | try writer.writeByte(macho.REBASE_OPCODE_DO_REBASE_ULEB_TIMES); | |
| 169 | try std.leb.writeULEB128(writer, count); | |
| 170 | } | |
| 171 | } | |
| 172 | ||
| 173 | fn rebaseTimesSkip(count: usize, skip: u64, writer: anytype) !void { | |
| 174 | log.debug(">>> rebase with count: {d} and skip: {x}", .{ count, skip }); | |
| 175 | try writer.writeByte(macho.REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB); | |
| 176 | try std.leb.writeULEB128(writer, count); | |
| 177 | try std.leb.writeULEB128(writer, skip); | |
| 178 | } | |
| 179 | ||
| 180 | fn addAddr(addr: u64, writer: anytype) !void { | |
| 181 | log.debug(">>> add: {x}", .{addr}); | |
| 182 | if (std.mem.isAlignedGeneric(u64, addr, @sizeOf(u64))) { | |
| 183 | const imm = @divExact(addr, @sizeOf(u64)); | |
| 184 | if (imm <= 0xf) { | |
| 185 | try writer.writeByte(macho.REBASE_OPCODE_ADD_ADDR_IMM_SCALED | @truncate(u4, imm)); | |
| 186 | return; | |
| 187 | } | |
| 188 | } | |
| 189 | try writer.writeByte(macho.REBASE_OPCODE_ADD_ADDR_ULEB); | |
| 190 | try std.leb.writeULEB128(writer, addr); | |
| 191 | } | |
| 192 | ||
| 193 | fn done(writer: anytype) !void { | |
| 194 | log.debug(">>> done", .{}); | |
| 195 | try writer.writeByte(macho.REBASE_OPCODE_DONE); | |
| 196 | } | |
| 197 | ||
| 198 | pub fn write(rebase: Rebase, writer: anytype) !void { | |
| 199 | if (rebase.size() == 0) return; | |
| 200 | try writer.writeAll(rebase.buffer.items); | |
| 201 | } | |
| 202 | ||
| 203 | test "rebase - no entries" { | |
| 204 | const gpa = testing.allocator; | |
| 205 | ||
| 206 | var rebase = Rebase{}; | |
| 207 | defer rebase.deinit(gpa); | |
| 208 | ||
| 209 | try rebase.finalize(gpa); | |
| 210 | try testing.expectEqual(@as(u64, 0), rebase.size()); | |
| 211 | } | |
| 212 | ||
| 213 | test "rebase - single entry" { | |
| 214 | const gpa = testing.allocator; | |
| 215 | ||
| 216 | var rebase = Rebase{}; | |
| 217 | defer rebase.deinit(gpa); | |
| 218 | try rebase.entries.append(gpa, .{ | |
| 219 | .segment_id = 1, | |
| 220 | .offset = 0x10, | |
| 221 | }); | |
| 222 | try rebase.finalize(gpa); | |
| 223 | try testing.expectEqualSlices(u8, &[_]u8{ | |
| 224 | macho.REBASE_OPCODE_SET_TYPE_IMM | macho.REBASE_TYPE_POINTER, | |
| 225 | macho.REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB | 1, | |
| 226 | 0x10, | |
| 227 | macho.REBASE_OPCODE_DO_REBASE_IMM_TIMES | 1, | |
| 228 | macho.REBASE_OPCODE_DONE, | |
| 229 | }, rebase.buffer.items); | |
| 230 | } | |
| 231 | ||
| 232 | test "rebase - emitTimes - IMM" { | |
| 233 | const gpa = testing.allocator; | |
| 234 | ||
| 235 | var rebase = Rebase{}; | |
| 236 | defer rebase.deinit(gpa); | |
| 237 | ||
| 238 | var i: u64 = 0; | |
| 239 | while (i < 10) : (i += 1) { | |
| 240 | try rebase.entries.append(gpa, .{ | |
| 241 | .segment_id = 1, | |
| 242 | .offset = i * @sizeOf(u64), | |
| 243 | }); | |
| 244 | } | |
| 245 | ||
| 246 | try rebase.finalize(gpa); | |
| 247 | ||
| 248 | try testing.expectEqualSlices(u8, &[_]u8{ | |
| 249 | macho.REBASE_OPCODE_SET_TYPE_IMM | macho.REBASE_TYPE_POINTER, | |
| 250 | macho.REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB | 1, | |
| 251 | 0x0, | |
| 252 | macho.REBASE_OPCODE_DO_REBASE_IMM_TIMES | 10, | |
| 253 | macho.REBASE_OPCODE_DONE, | |
| 254 | }, rebase.buffer.items); | |
| 255 | } | |
| 256 | ||
| 257 | test "rebase - emitTimes - ULEB" { | |
| 258 | const gpa = testing.allocator; | |
| 259 | ||
| 260 | var rebase = Rebase{}; | |
| 261 | defer rebase.deinit(gpa); | |
| 262 | ||
| 263 | var i: u64 = 0; | |
| 264 | while (i < 100) : (i += 1) { | |
| 265 | try rebase.entries.append(gpa, .{ | |
| 266 | .segment_id = 1, | |
| 267 | .offset = i * @sizeOf(u64), | |
| 268 | }); | |
| 269 | } | |
| 270 | ||
| 271 | try rebase.finalize(gpa); | |
| 272 | ||
| 273 | try testing.expectEqualSlices(u8, &[_]u8{ | |
| 274 | macho.REBASE_OPCODE_SET_TYPE_IMM | macho.REBASE_TYPE_POINTER, | |
| 275 | macho.REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB | 1, | |
| 276 | 0x0, | |
| 277 | macho.REBASE_OPCODE_DO_REBASE_ULEB_TIMES, | |
| 278 | 0x64, | |
| 279 | macho.REBASE_OPCODE_DONE, | |
| 280 | }, rebase.buffer.items); | |
| 281 | } | |
| 282 | ||
| 283 | test "rebase - emitTimes followed by addAddr followed by emitTimes" { | |
| 284 | const gpa = testing.allocator; | |
| 285 | ||
| 286 | var rebase = Rebase{}; | |
| 287 | defer rebase.deinit(gpa); | |
| 288 | ||
| 289 | var offset: u64 = 0; | |
| 290 | var i: u64 = 0; | |
| 291 | while (i < 15) : (i += 1) { | |
| 292 | try rebase.entries.append(gpa, .{ | |
| 293 | .segment_id = 1, | |
| 294 | .offset = offset, | |
| 295 | }); | |
| 296 | offset += @sizeOf(u64); | |
| 297 | } | |
| 298 | ||
| 299 | offset += @sizeOf(u64); | |
| 300 | ||
| 301 | try rebase.entries.append(gpa, .{ | |
| 302 | .segment_id = 1, | |
| 303 | .offset = offset, | |
| 304 | }); | |
| 305 | ||
| 306 | try rebase.finalize(gpa); | |
| 307 | ||
| 308 | try testing.expectEqualSlices(u8, &[_]u8{ | |
| 309 | macho.REBASE_OPCODE_SET_TYPE_IMM | macho.REBASE_TYPE_POINTER, | |
| 310 | macho.REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB | 1, | |
| 311 | 0x0, | |
| 312 | macho.REBASE_OPCODE_DO_REBASE_IMM_TIMES | 15, | |
| 313 | macho.REBASE_OPCODE_ADD_ADDR_IMM_SCALED | 1, | |
| 314 | macho.REBASE_OPCODE_DO_REBASE_IMM_TIMES | 1, | |
| 315 | macho.REBASE_OPCODE_DONE, | |
| 316 | }, rebase.buffer.items); | |
| 317 | } | |
| 318 | ||
| 319 | test "rebase - emitTimesSkip" { | |
| 320 | const gpa = testing.allocator; | |
| 321 | ||
| 322 | var rebase = Rebase{}; | |
| 323 | defer rebase.deinit(gpa); | |
| 324 | ||
| 325 | var offset: u64 = 0; | |
| 326 | var i: u64 = 0; | |
| 327 | while (i < 15) : (i += 1) { | |
| 328 | try rebase.entries.append(gpa, .{ | |
| 329 | .segment_id = 1, | |
| 330 | .offset = offset, | |
| 331 | }); | |
| 332 | offset += 2 * @sizeOf(u64); | |
| 333 | } | |
| 334 | ||
| 335 | try rebase.finalize(gpa); | |
| 336 | ||
| 337 | try testing.expectEqualSlices(u8, &[_]u8{ | |
| 338 | macho.REBASE_OPCODE_SET_TYPE_IMM | macho.REBASE_TYPE_POINTER, | |
| 339 | macho.REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB | 1, | |
| 340 | 0x0, | |
| 341 | macho.REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB, | |
| 342 | 0xf, | |
| 343 | 0x8, | |
| 344 | macho.REBASE_OPCODE_DONE, | |
| 345 | }, rebase.buffer.items); | |
| 346 | } | |
| 347 | ||
| 348 | test "rebase - complex" { | |
| 349 | const gpa = testing.allocator; | |
| 350 | ||
| 351 | var rebase = Rebase{}; | |
| 352 | defer rebase.deinit(gpa); | |
| 353 | ||
| 354 | try rebase.entries.append(gpa, .{ | |
| 355 | .segment_id = 1, | |
| 356 | .offset = 0, | |
| 357 | }); | |
| 358 | try rebase.entries.append(gpa, .{ | |
| 359 | .segment_id = 1, | |
| 360 | .offset = 0x10, | |
| 361 | }); | |
| 362 | try rebase.entries.append(gpa, .{ | |
| 363 | .segment_id = 1, | |
| 364 | .offset = 0x40, | |
| 365 | }); | |
| 366 | try rebase.entries.append(gpa, .{ | |
| 367 | .segment_id = 1, | |
| 368 | .offset = 0x48, | |
| 369 | }); | |
| 370 | try rebase.entries.append(gpa, .{ | |
| 371 | .segment_id = 1, | |
| 372 | .offset = 0x50, | |
| 373 | }); | |
| 374 | try rebase.entries.append(gpa, .{ | |
| 375 | .segment_id = 1, | |
| 376 | .offset = 0x58, | |
| 377 | }); | |
| 378 | try rebase.entries.append(gpa, .{ | |
| 379 | .segment_id = 1, | |
| 380 | .offset = 0x70, | |
| 381 | }); | |
| 382 | try rebase.finalize(gpa); | |
| 383 | ||
| 384 | try testing.expectEqualSlices(u8, &[_]u8{ | |
| 385 | macho.REBASE_OPCODE_SET_TYPE_IMM | macho.REBASE_TYPE_POINTER, | |
| 386 | macho.REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB | 1, | |
| 387 | 0x0, | |
| 388 | macho.REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB, | |
| 389 | 0x2, | |
| 390 | 0x8, | |
| 391 | macho.REBASE_OPCODE_ADD_ADDR_IMM_SCALED | 4, | |
| 392 | macho.REBASE_OPCODE_DO_REBASE_IMM_TIMES | 4, | |
| 393 | macho.REBASE_OPCODE_ADD_ADDR_IMM_SCALED | 2, | |
| 394 | macho.REBASE_OPCODE_DO_REBASE_IMM_TIMES | 1, | |
| 395 | macho.REBASE_OPCODE_DONE, | |
| 396 | }, rebase.buffer.items); | |
| 397 | } | |
| 398 | ||
| 399 | test "rebase - complex 2" { | |
| 400 | const gpa = testing.allocator; | |
| 401 | ||
| 402 | var rebase = Rebase{}; | |
| 403 | defer rebase.deinit(gpa); | |
| 404 | ||
| 405 | try rebase.entries.append(gpa, .{ | |
| 406 | .segment_id = 1, | |
| 407 | .offset = 0, | |
| 408 | }); | |
| 409 | try rebase.entries.append(gpa, .{ | |
| 410 | .segment_id = 1, | |
| 411 | .offset = 0x10, | |
| 412 | }); | |
| 413 | try rebase.entries.append(gpa, .{ | |
| 414 | .segment_id = 1, | |
| 415 | .offset = 0x28, | |
| 416 | }); | |
| 417 | try rebase.entries.append(gpa, .{ | |
| 418 | .segment_id = 1, | |
| 419 | .offset = 0x48, | |
| 420 | }); | |
| 421 | try rebase.entries.append(gpa, .{ | |
| 422 | .segment_id = 1, | |
| 423 | .offset = 0x78, | |
| 424 | }); | |
| 425 | try rebase.entries.append(gpa, .{ | |
| 426 | .segment_id = 1, | |
| 427 | .offset = 0xb8, | |
| 428 | }); | |
| 429 | try rebase.entries.append(gpa, .{ | |
| 430 | .segment_id = 2, | |
| 431 | .offset = 0x0, | |
| 432 | }); | |
| 433 | try rebase.entries.append(gpa, .{ | |
| 434 | .segment_id = 2, | |
| 435 | .offset = 0x8, | |
| 436 | }); | |
| 437 | try rebase.entries.append(gpa, .{ | |
| 438 | .segment_id = 2, | |
| 439 | .offset = 0x10, | |
| 440 | }); | |
| 441 | try rebase.entries.append(gpa, .{ | |
| 442 | .segment_id = 2, | |
| 443 | .offset = 0x18, | |
| 444 | }); | |
| 445 | try rebase.entries.append(gpa, .{ | |
| 446 | .segment_id = 3, | |
| 447 | .offset = 0x0, | |
| 448 | }); | |
| 449 | try rebase.entries.append(gpa, .{ | |
| 450 | .segment_id = 3, | |
| 451 | .offset = 0x20, | |
| 452 | }); | |
| 453 | try rebase.entries.append(gpa, .{ | |
| 454 | .segment_id = 3, | |
| 455 | .offset = 0x40, | |
| 456 | }); | |
| 457 | try rebase.entries.append(gpa, .{ | |
| 458 | .segment_id = 3, | |
| 459 | .offset = 0x60, | |
| 460 | }); | |
| 461 | try rebase.entries.append(gpa, .{ | |
| 462 | .segment_id = 3, | |
| 463 | .offset = 0x68, | |
| 464 | }); | |
| 465 | try rebase.finalize(gpa); | |
| 466 | ||
| 467 | try testing.expectEqualSlices(u8, &[_]u8{ | |
| 468 | macho.REBASE_OPCODE_SET_TYPE_IMM | macho.REBASE_TYPE_POINTER, | |
| 469 | macho.REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB | 1, | |
| 470 | 0x0, | |
| 471 | macho.REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB, | |
| 472 | 0x2, | |
| 473 | 0x8, | |
| 474 | macho.REBASE_OPCODE_ADD_ADDR_IMM_SCALED | 1, | |
| 475 | macho.REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB, | |
| 476 | 0x2, | |
| 477 | 0x18, | |
| 478 | macho.REBASE_OPCODE_ADD_ADDR_IMM_SCALED | 2, | |
| 479 | macho.REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB, | |
| 480 | 0x2, | |
| 481 | 0x38, | |
| 482 | macho.REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB | 2, | |
| 483 | 0x0, | |
| 484 | macho.REBASE_OPCODE_DO_REBASE_IMM_TIMES | 4, | |
| 485 | macho.REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB | 3, | |
| 486 | 0x0, | |
| 487 | macho.REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB, | |
| 488 | 0x3, | |
| 489 | 0x18, | |
| 490 | macho.REBASE_OPCODE_DO_REBASE_IMM_TIMES | 2, | |
| 491 | macho.REBASE_OPCODE_DONE, | |
| 492 | }, rebase.buffer.items); | |
| 493 | } | |
| 494 | ||
| 495 | test "rebase - composite" { | |
| 496 | const gpa = testing.allocator; | |
| 497 | ||
| 498 | var rebase = Rebase{}; | |
| 499 | defer rebase.deinit(gpa); | |
| 500 | ||
| 501 | try rebase.entries.append(gpa, .{ | |
| 502 | .segment_id = 1, | |
| 503 | .offset = 0x8, | |
| 504 | }); | |
| 505 | try rebase.entries.append(gpa, .{ | |
| 506 | .segment_id = 1, | |
| 507 | .offset = 0x38, | |
| 508 | }); | |
| 509 | try rebase.entries.append(gpa, .{ | |
| 510 | .segment_id = 1, | |
| 511 | .offset = 0xa0, | |
| 512 | }); | |
| 513 | try rebase.entries.append(gpa, .{ | |
| 514 | .segment_id = 1, | |
| 515 | .offset = 0xa8, | |
| 516 | }); | |
| 517 | try rebase.entries.append(gpa, .{ | |
| 518 | .segment_id = 1, | |
| 519 | .offset = 0xb0, | |
| 520 | }); | |
| 521 | try rebase.entries.append(gpa, .{ | |
| 522 | .segment_id = 1, | |
| 523 | .offset = 0xc0, | |
| 524 | }); | |
| 525 | try rebase.entries.append(gpa, .{ | |
| 526 | .segment_id = 1, | |
| 527 | .offset = 0xc8, | |
| 528 | }); | |
| 529 | try rebase.entries.append(gpa, .{ | |
| 530 | .segment_id = 1, | |
| 531 | .offset = 0xd0, | |
| 532 | }); | |
| 533 | try rebase.entries.append(gpa, .{ | |
| 534 | .segment_id = 1, | |
| 535 | .offset = 0xd8, | |
| 536 | }); | |
| 537 | try rebase.entries.append(gpa, .{ | |
| 538 | .segment_id = 1, | |
| 539 | .offset = 0xe0, | |
| 540 | }); | |
| 541 | try rebase.entries.append(gpa, .{ | |
| 542 | .segment_id = 1, | |
| 543 | .offset = 0xe8, | |
| 544 | }); | |
| 545 | try rebase.entries.append(gpa, .{ | |
| 546 | .segment_id = 1, | |
| 547 | .offset = 0xf0, | |
| 548 | }); | |
| 549 | try rebase.entries.append(gpa, .{ | |
| 550 | .segment_id = 1, | |
| 551 | .offset = 0xf8, | |
| 552 | }); | |
| 553 | try rebase.entries.append(gpa, .{ | |
| 554 | .segment_id = 1, | |
| 555 | .offset = 0x108, | |
| 556 | }); | |
| 557 | try rebase.finalize(gpa); | |
| 558 | ||
| 559 | try testing.expectEqualSlices(u8, &[_]u8{ | |
| 560 | macho.REBASE_OPCODE_SET_TYPE_IMM | macho.REBASE_TYPE_POINTER, | |
| 561 | macho.REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB | 1, | |
| 562 | 0x8, | |
| 563 | macho.REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB, | |
| 564 | 0x2, | |
| 565 | 0x28, | |
| 566 | macho.REBASE_OPCODE_ADD_ADDR_IMM_SCALED | 7, | |
| 567 | macho.REBASE_OPCODE_DO_REBASE_IMM_TIMES | 3, | |
| 568 | macho.REBASE_OPCODE_ADD_ADDR_IMM_SCALED | 1, | |
| 569 | macho.REBASE_OPCODE_DO_REBASE_IMM_TIMES | 8, | |
| 570 | macho.REBASE_OPCODE_ADD_ADDR_IMM_SCALED | 1, | |
| 571 | macho.REBASE_OPCODE_DO_REBASE_IMM_TIMES | 1, | |
| 572 | macho.REBASE_OPCODE_DONE, | |
| 573 | }, rebase.buffer.items); | |
| 574 | } |
src/link/MachO/dyld_info/bind.zig created+740| ... | ... | @@ -0,0 +1,740 @@ |
| 1 | const std = @import("std"); | |
| 2 | const assert = std.debug.assert; | |
| 3 | const leb = std.leb; | |
| 4 | const log = std.log.scoped(.dyld_info); | |
| 5 | const macho = std.macho; | |
| 6 | const testing = std.testing; | |
| 7 | ||
| 8 | const Allocator = std.mem.Allocator; | |
| 9 | ||
| 10 | pub fn Bind(comptime Ctx: type, comptime Target: type) type { | |
| 11 | return struct { | |
| 12 | entries: std.ArrayListUnmanaged(Entry) = .{}, | |
| 13 | buffer: std.ArrayListUnmanaged(u8) = .{}, | |
| 14 | ||
| 15 | const Self = @This(); | |
| 16 | ||
| 17 | const Entry = struct { | |
| 18 | target: Target, | |
| 19 | offset: u64, | |
| 20 | segment_id: u8, | |
| 21 | addend: i64, | |
| 22 | ||
| 23 | pub fn lessThan(ctx: Ctx, entry: Entry, other: Entry) bool { | |
| 24 | if (entry.segment_id == other.segment_id) { | |
| 25 | if (entry.target.eql(other.target)) { | |
| 26 | return entry.offset < other.offset; | |
| 27 | } | |
| 28 | const entry_name = ctx.getSymbolName(entry.target); | |
| 29 | const other_name = ctx.getSymbolName(other.target); | |
| 30 | return std.mem.lessThan(u8, entry_name, other_name); | |
| 31 | } | |
| 32 | return entry.segment_id < other.segment_id; | |
| 33 | } | |
| 34 | }; | |
| 35 | ||
| 36 | pub fn deinit(self: *Self, gpa: Allocator) void { | |
| 37 | self.entries.deinit(gpa); | |
| 38 | self.buffer.deinit(gpa); | |
| 39 | } | |
| 40 | ||
| 41 | pub fn size(self: Self) u64 { | |
| 42 | return @intCast(u64, self.buffer.items.len); | |
| 43 | } | |
| 44 | ||
| 45 | pub fn finalize(self: *Self, gpa: Allocator, ctx: Ctx) !void { | |
| 46 | if (self.entries.items.len == 0) return; | |
| 47 | ||
| 48 | const writer = self.buffer.writer(gpa); | |
| 49 | ||
| 50 | std.sort.sort(Entry, self.entries.items, ctx, Entry.lessThan); | |
| 51 | ||
| 52 | var start: usize = 0; | |
| 53 | var seg_id: ?u8 = null; | |
| 54 | for (self.entries.items) |entry, i| { | |
| 55 | if (seg_id != null and seg_id.? == entry.segment_id) continue; | |
| 56 | try finalizeSegment(self.entries.items[start..i], ctx, writer); | |
| 57 | seg_id = entry.segment_id; | |
| 58 | start = i; | |
| 59 | } | |
| 60 | ||
| 61 | try finalizeSegment(self.entries.items[start..], ctx, writer); | |
| 62 | try done(writer); | |
| 63 | } | |
| 64 | ||
| 65 | fn finalizeSegment(entries: []const Entry, ctx: Ctx, writer: anytype) !void { | |
| 66 | if (entries.len == 0) return; | |
| 67 | ||
| 68 | const seg_id = entries[0].segment_id; | |
| 69 | try setSegmentOffset(seg_id, 0, writer); | |
| 70 | ||
| 71 | var offset: u64 = 0; | |
| 72 | var addend: i64 = 0; | |
| 73 | var count: usize = 0; | |
| 74 | var skip: u64 = 0; | |
| 75 | var target: ?Target = null; | |
| 76 | ||
| 77 | var state: enum { | |
| 78 | start, | |
| 79 | bind_single, | |
| 80 | bind_times_skip, | |
| 81 | } = .start; | |
| 82 | ||
| 83 | var i: usize = 0; | |
| 84 | while (i < entries.len) : (i += 1) { | |
| 85 | const current = entries[i]; | |
| 86 | if (target == null or !target.?.eql(current.target)) { | |
| 87 | switch (state) { | |
| 88 | .start => {}, | |
| 89 | .bind_single => try doBind(writer), | |
| 90 | .bind_times_skip => try doBindTimesSkip(count, skip, writer), | |
| 91 | } | |
| 92 | state = .start; | |
| 93 | target = current.target; | |
| 94 | ||
| 95 | const sym = ctx.getSymbol(current.target); | |
| 96 | const name = ctx.getSymbolName(current.target); | |
| 97 | const flags: u8 = if (sym.weakRef()) macho.BIND_SYMBOL_FLAGS_WEAK_IMPORT else 0; | |
| 98 | const ordinal = @divTrunc(@bitCast(i16, sym.n_desc), macho.N_SYMBOL_RESOLVER); | |
| 99 | ||
| 100 | try setSymbol(name, flags, writer); | |
| 101 | try setTypePointer(writer); | |
| 102 | try setDylibOrdinal(ordinal, writer); | |
| 103 | ||
| 104 | if (current.addend != addend) { | |
| 105 | addend = current.addend; | |
| 106 | try setAddend(addend, writer); | |
| 107 | } | |
| 108 | } | |
| 109 | ||
| 110 | log.debug("{x}, {d}, {x}, {?x}, {s}", .{ offset, count, skip, addend, @tagName(state) }); | |
| 111 | log.debug(" => {x}", .{current.offset}); | |
| 112 | switch (state) { | |
| 113 | .start => { | |
| 114 | if (current.offset < offset) { | |
| 115 | try addAddr(@bitCast(u64, @intCast(i64, current.offset) - @intCast(i64, offset)), writer); | |
| 116 | offset = offset - (offset - current.offset); | |
| 117 | } else if (current.offset > offset) { | |
| 118 | const delta = current.offset - offset; | |
| 119 | try addAddr(delta, writer); | |
| 120 | offset += delta; | |
| 121 | } | |
| 122 | state = .bind_single; | |
| 123 | offset += @sizeOf(u64); | |
| 124 | count = 1; | |
| 125 | }, | |
| 126 | .bind_single => { | |
| 127 | if (current.offset == offset) { | |
| 128 | try doBind(writer); | |
| 129 | state = .start; | |
| 130 | } else if (current.offset > offset) { | |
| 131 | const delta = current.offset - offset; | |
| 132 | state = .bind_times_skip; | |
| 133 | skip = @intCast(u64, delta); | |
| 134 | offset += skip; | |
| 135 | } else unreachable; | |
| 136 | i -= 1; | |
| 137 | }, | |
| 138 | .bind_times_skip => { | |
| 139 | if (current.offset < offset) { | |
| 140 | count -= 1; | |
| 141 | if (count == 1) { | |
| 142 | try doBindAddAddr(skip, writer); | |
| 143 | } else { | |
| 144 | try doBindTimesSkip(count, skip, writer); | |
| 145 | } | |
| 146 | state = .start; | |
| 147 | offset = offset - (@sizeOf(u64) + skip); | |
| 148 | i -= 2; | |
| 149 | } else if (current.offset == offset) { | |
| 150 | count += 1; | |
| 151 | offset += @sizeOf(u64) + skip; | |
| 152 | } else { | |
| 153 | try doBindTimesSkip(count, skip, writer); | |
| 154 | state = .start; | |
| 155 | i -= 1; | |
| 156 | } | |
| 157 | }, | |
| 158 | } | |
| 159 | } | |
| 160 | ||
| 161 | switch (state) { | |
| 162 | .start => unreachable, | |
| 163 | .bind_single => try doBind(writer), | |
| 164 | .bind_times_skip => try doBindTimesSkip(count, skip, writer), | |
| 165 | } | |
| 166 | } | |
| 167 | ||
| 168 | pub fn write(self: Self, writer: anytype) !void { | |
| 169 | if (self.size() == 0) return; | |
| 170 | try writer.writeAll(self.buffer.items); | |
| 171 | } | |
| 172 | }; | |
| 173 | } | |
| 174 | ||
| 175 | pub fn LazyBind(comptime Ctx: type, comptime Target: type) type { | |
| 176 | return struct { | |
| 177 | entries: std.ArrayListUnmanaged(Entry) = .{}, | |
| 178 | buffer: std.ArrayListUnmanaged(u8) = .{}, | |
| 179 | offsets: std.ArrayListUnmanaged(u32) = .{}, | |
| 180 | ||
| 181 | const Self = @This(); | |
| 182 | ||
| 183 | const Entry = struct { | |
| 184 | target: Target, | |
| 185 | offset: u64, | |
| 186 | segment_id: u8, | |
| 187 | addend: i64, | |
| 188 | }; | |
| 189 | ||
| 190 | pub fn deinit(self: *Self, gpa: Allocator) void { | |
| 191 | self.entries.deinit(gpa); | |
| 192 | self.buffer.deinit(gpa); | |
| 193 | self.offsets.deinit(gpa); | |
| 194 | } | |
| 195 | ||
| 196 | pub fn size(self: Self) u64 { | |
| 197 | return @intCast(u64, self.buffer.items.len); | |
| 198 | } | |
| 199 | ||
| 200 | pub fn finalize(self: *Self, gpa: Allocator, ctx: Ctx) !void { | |
| 201 | if (self.entries.items.len == 0) return; | |
| 202 | ||
| 203 | try self.offsets.ensureTotalCapacityPrecise(gpa, self.entries.items.len); | |
| 204 | ||
| 205 | var cwriter = std.io.countingWriter(self.buffer.writer(gpa)); | |
| 206 | const writer = cwriter.writer(); | |
| 207 | ||
| 208 | var addend: i64 = 0; | |
| 209 | ||
| 210 | for (self.entries.items) |entry| { | |
| 211 | self.offsets.appendAssumeCapacity(@intCast(u32, cwriter.bytes_written)); | |
| 212 | ||
| 213 | const sym = ctx.getSymbol(entry.target); | |
| 214 | const name = ctx.getSymbolName(entry.target); | |
| 215 | const flags: u8 = if (sym.weakRef()) macho.BIND_SYMBOL_FLAGS_WEAK_IMPORT else 0; | |
| 216 | const ordinal = @divTrunc(@bitCast(i16, sym.n_desc), macho.N_SYMBOL_RESOLVER); | |
| 217 | ||
| 218 | try setSegmentOffset(entry.segment_id, entry.offset, writer); | |
| 219 | try setSymbol(name, flags, writer); | |
| 220 | try setDylibOrdinal(ordinal, writer); | |
| 221 | ||
| 222 | if (entry.addend != addend) { | |
| 223 | try setAddend(entry.addend, writer); | |
| 224 | addend = entry.addend; | |
| 225 | } | |
| 226 | ||
| 227 | try doBind(writer); | |
| 228 | try done(writer); | |
| 229 | } | |
| 230 | } | |
| 231 | ||
| 232 | pub fn write(self: Self, writer: anytype) !void { | |
| 233 | if (self.size() == 0) return; | |
| 234 | try writer.writeAll(self.buffer.items); | |
| 235 | } | |
| 236 | }; | |
| 237 | } | |
| 238 | ||
| 239 | fn setSegmentOffset(segment_id: u8, offset: u64, writer: anytype) !void { | |
| 240 | log.debug(">>> set segment: {d} and offset: {x}", .{ segment_id, offset }); | |
| 241 | try writer.writeByte(macho.BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB | @truncate(u4, segment_id)); | |
| 242 | try std.leb.writeULEB128(writer, offset); | |
| 243 | } | |
| 244 | ||
| 245 | fn setSymbol(name: []const u8, flags: u8, writer: anytype) !void { | |
| 246 | log.debug(">>> set symbol: {s} with flags: {x}", .{ name, flags }); | |
| 247 | try writer.writeByte(macho.BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM | @truncate(u4, flags)); | |
| 248 | try writer.writeAll(name); | |
| 249 | try writer.writeByte(0); | |
| 250 | } | |
| 251 | ||
| 252 | fn setTypePointer(writer: anytype) !void { | |
| 253 | log.debug(">>> set type: {d}", .{macho.BIND_TYPE_POINTER}); | |
| 254 | try writer.writeByte(macho.BIND_OPCODE_SET_TYPE_IMM | @truncate(u4, macho.BIND_TYPE_POINTER)); | |
| 255 | } | |
| 256 | ||
| 257 | fn setDylibOrdinal(ordinal: i16, writer: anytype) !void { | |
| 258 | if (ordinal <= 0) { | |
| 259 | switch (ordinal) { | |
| 260 | macho.BIND_SPECIAL_DYLIB_SELF, | |
| 261 | macho.BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE, | |
| 262 | macho.BIND_SPECIAL_DYLIB_FLAT_LOOKUP, | |
| 263 | => {}, | |
| 264 | else => unreachable, // Invalid dylib special binding | |
| 265 | } | |
| 266 | log.debug(">>> set dylib special: {d}", .{ordinal}); | |
| 267 | const cast = @bitCast(u16, ordinal); | |
| 268 | try writer.writeByte(macho.BIND_OPCODE_SET_DYLIB_SPECIAL_IMM | @truncate(u4, cast)); | |
| 269 | } else { | |
| 270 | const cast = @bitCast(u16, ordinal); | |
| 271 | log.debug(">>> set dylib ordinal: {d}", .{ordinal}); | |
| 272 | if (cast <= 0xf) { | |
| 273 | try writer.writeByte(macho.BIND_OPCODE_SET_DYLIB_ORDINAL_IMM | @truncate(u4, cast)); | |
| 274 | } else { | |
| 275 | try writer.writeByte(macho.BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB); | |
| 276 | try std.leb.writeULEB128(writer, cast); | |
| 277 | } | |
| 278 | } | |
| 279 | } | |
| 280 | ||
| 281 | fn setAddend(addend: i64, writer: anytype) !void { | |
| 282 | log.debug(">>> set addend: {x}", .{addend}); | |
| 283 | try writer.writeByte(macho.BIND_OPCODE_SET_ADDEND_SLEB); | |
| 284 | try std.leb.writeILEB128(writer, addend); | |
| 285 | } | |
| 286 | ||
| 287 | fn doBind(writer: anytype) !void { | |
| 288 | log.debug(">>> bind", .{}); | |
| 289 | try writer.writeByte(macho.BIND_OPCODE_DO_BIND); | |
| 290 | } | |
| 291 | ||
| 292 | fn doBindAddAddr(addr: u64, writer: anytype) !void { | |
| 293 | log.debug(">>> bind with add: {x}", .{addr}); | |
| 294 | if (std.mem.isAlignedGeneric(u64, addr, @sizeOf(u64))) { | |
| 295 | const imm = @divExact(addr, @sizeOf(u64)); | |
| 296 | if (imm <= 0xf) { | |
| 297 | try writer.writeByte( | |
| 298 | macho.BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED | @truncate(u4, imm), | |
| 299 | ); | |
| 300 | return; | |
| 301 | } | |
| 302 | } | |
| 303 | try writer.writeByte(macho.BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB); | |
| 304 | try std.leb.writeULEB128(writer, addr); | |
| 305 | } | |
| 306 | ||
| 307 | fn doBindTimesSkip(count: usize, skip: u64, writer: anytype) !void { | |
| 308 | log.debug(">>> bind with count: {d} and skip: {x}", .{ count, skip }); | |
| 309 | try writer.writeByte(macho.BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB); | |
| 310 | try std.leb.writeULEB128(writer, count); | |
| 311 | try std.leb.writeULEB128(writer, skip); | |
| 312 | } | |
| 313 | ||
| 314 | fn addAddr(addr: u64, writer: anytype) !void { | |
| 315 | log.debug(">>> add: {x}", .{addr}); | |
| 316 | try writer.writeByte(macho.BIND_OPCODE_ADD_ADDR_ULEB); | |
| 317 | try std.leb.writeULEB128(writer, addr); | |
| 318 | } | |
| 319 | ||
| 320 | fn done(writer: anytype) !void { | |
| 321 | log.debug(">>> done", .{}); | |
| 322 | try writer.writeByte(macho.BIND_OPCODE_DONE); | |
| 323 | } | |
| 324 | ||
| 325 | const TestContext = struct { | |
| 326 | symbols: std.ArrayListUnmanaged(macho.nlist_64) = .{}, | |
| 327 | strtab: std.ArrayListUnmanaged(u8) = .{}, | |
| 328 | ||
| 329 | const Target = struct { | |
| 330 | index: u32, | |
| 331 | ||
| 332 | fn eql(this: Target, other: Target) bool { | |
| 333 | return this.index == other.index; | |
| 334 | } | |
| 335 | }; | |
| 336 | ||
| 337 | fn deinit(ctx: *TestContext, gpa: Allocator) void { | |
| 338 | ctx.symbols.deinit(gpa); | |
| 339 | ctx.strtab.deinit(gpa); | |
| 340 | } | |
| 341 | ||
| 342 | fn addSymbol(ctx: *TestContext, gpa: Allocator, name: []const u8, ordinal: i16, flags: u16) !void { | |
| 343 | const n_strx = try ctx.addString(gpa, name); | |
| 344 | var n_desc = @bitCast(u16, ordinal * macho.N_SYMBOL_RESOLVER); | |
| 345 | n_desc |= flags; | |
| 346 | try ctx.symbols.append(gpa, .{ | |
| 347 | .n_value = 0, | |
| 348 | .n_strx = n_strx, | |
| 349 | .n_desc = n_desc, | |
| 350 | .n_type = macho.N_EXT, | |
| 351 | .n_sect = 0, | |
| 352 | }); | |
| 353 | } | |
| 354 | ||
| 355 | fn addString(ctx: *TestContext, gpa: Allocator, name: []const u8) !u32 { | |
| 356 | const n_strx = @intCast(u32, ctx.strtab.items.len); | |
| 357 | try ctx.strtab.appendSlice(gpa, name); | |
| 358 | try ctx.strtab.append(gpa, 0); | |
| 359 | return n_strx; | |
| 360 | } | |
| 361 | ||
| 362 | fn getSymbol(ctx: TestContext, target: Target) macho.nlist_64 { | |
| 363 | return ctx.symbols.items[target.index]; | |
| 364 | } | |
| 365 | ||
| 366 | fn getSymbolName(ctx: TestContext, target: Target) []const u8 { | |
| 367 | const sym = ctx.getSymbol(target); | |
| 368 | assert(sym.n_strx < ctx.strtab.items.len); | |
| 369 | return std.mem.sliceTo(@ptrCast([*:0]const u8, ctx.strtab.items.ptr + sym.n_strx), 0); | |
| 370 | } | |
| 371 | }; | |
| 372 | ||
| 373 | fn generateTestContext() !TestContext { | |
| 374 | const gpa = testing.allocator; | |
| 375 | var ctx = TestContext{}; | |
| 376 | try ctx.addSymbol(gpa, "_import_1", 1, 0); | |
| 377 | try ctx.addSymbol(gpa, "_import_2", 1, 0); | |
| 378 | try ctx.addSymbol(gpa, "_import_3", 1, 0); | |
| 379 | try ctx.addSymbol(gpa, "_import_4", 2, 0); | |
| 380 | try ctx.addSymbol(gpa, "_import_5_weak", 2, macho.N_WEAK_REF); | |
| 381 | try ctx.addSymbol(gpa, "_import_6", 2, 0); | |
| 382 | return ctx; | |
| 383 | } | |
| 384 | ||
| 385 | test "bind - no entries" { | |
| 386 | const gpa = testing.allocator; | |
| 387 | ||
| 388 | var test_context = try generateTestContext(); | |
| 389 | defer test_context.deinit(gpa); | |
| 390 | ||
| 391 | var bind = Bind(TestContext, TestContext.Target){}; | |
| 392 | defer bind.deinit(gpa); | |
| 393 | ||
| 394 | try bind.finalize(gpa, test_context); | |
| 395 | try testing.expectEqual(@as(u64, 0), bind.size()); | |
| 396 | } | |
| 397 | ||
| 398 | test "bind - single entry" { | |
| 399 | const gpa = testing.allocator; | |
| 400 | ||
| 401 | var test_context = try generateTestContext(); | |
| 402 | defer test_context.deinit(gpa); | |
| 403 | ||
| 404 | var bind = Bind(TestContext, TestContext.Target){}; | |
| 405 | defer bind.deinit(gpa); | |
| 406 | ||
| 407 | try bind.entries.append(gpa, .{ | |
| 408 | .offset = 0x10, | |
| 409 | .segment_id = 1, | |
| 410 | .target = TestContext.Target{ .index = 0 }, | |
| 411 | .addend = 0, | |
| 412 | }); | |
| 413 | try bind.finalize(gpa, test_context); | |
| 414 | try testing.expectEqualSlices(u8, &[_]u8{ | |
| 415 | macho.BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB | 1, | |
| 416 | 0x0, | |
| 417 | macho.BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM | 0, | |
| 418 | 0x5f, | |
| 419 | 0x69, | |
| 420 | 0x6d, | |
| 421 | 0x70, | |
| 422 | 0x6f, | |
| 423 | 0x72, | |
| 424 | 0x74, | |
| 425 | 0x5f, | |
| 426 | 0x31, | |
| 427 | 0x0, | |
| 428 | macho.BIND_OPCODE_SET_TYPE_IMM | 1, | |
| 429 | macho.BIND_OPCODE_SET_DYLIB_ORDINAL_IMM | 1, | |
| 430 | macho.BIND_OPCODE_ADD_ADDR_ULEB, | |
| 431 | 0x10, | |
| 432 | macho.BIND_OPCODE_DO_BIND, | |
| 433 | macho.BIND_OPCODE_DONE, | |
| 434 | }, bind.buffer.items); | |
| 435 | } | |
| 436 | ||
| 437 | test "bind - multiple occurrences within the same segment" { | |
| 438 | const gpa = testing.allocator; | |
| 439 | ||
| 440 | var test_context = try generateTestContext(); | |
| 441 | defer test_context.deinit(gpa); | |
| 442 | ||
| 443 | var bind = Bind(TestContext, TestContext.Target){}; | |
| 444 | defer bind.deinit(gpa); | |
| 445 | ||
| 446 | try bind.entries.append(gpa, .{ | |
| 447 | .offset = 0x10, | |
| 448 | .segment_id = 1, | |
| 449 | .target = TestContext.Target{ .index = 0 }, | |
| 450 | .addend = 0, | |
| 451 | }); | |
| 452 | try bind.entries.append(gpa, .{ | |
| 453 | .offset = 0x18, | |
| 454 | .segment_id = 1, | |
| 455 | .target = TestContext.Target{ .index = 0 }, | |
| 456 | .addend = 0, | |
| 457 | }); | |
| 458 | try bind.entries.append(gpa, .{ | |
| 459 | .offset = 0x20, | |
| 460 | .segment_id = 1, | |
| 461 | .target = TestContext.Target{ .index = 0 }, | |
| 462 | .addend = 0, | |
| 463 | }); | |
| 464 | try bind.entries.append(gpa, .{ | |
| 465 | .offset = 0x28, | |
| 466 | .segment_id = 1, | |
| 467 | .target = TestContext.Target{ .index = 0 }, | |
| 468 | .addend = 0, | |
| 469 | }); | |
| 470 | ||
| 471 | try bind.finalize(gpa, test_context); | |
| 472 | try testing.expectEqualSlices(u8, &[_]u8{ | |
| 473 | macho.BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB | 1, | |
| 474 | 0x0, | |
| 475 | macho.BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM | 0, | |
| 476 | 0x5f, | |
| 477 | 0x69, | |
| 478 | 0x6d, | |
| 479 | 0x70, | |
| 480 | 0x6f, | |
| 481 | 0x72, | |
| 482 | 0x74, | |
| 483 | 0x5f, | |
| 484 | 0x31, | |
| 485 | 0x0, | |
| 486 | macho.BIND_OPCODE_SET_TYPE_IMM | 1, | |
| 487 | macho.BIND_OPCODE_SET_DYLIB_ORDINAL_IMM | 1, | |
| 488 | macho.BIND_OPCODE_ADD_ADDR_ULEB, | |
| 489 | 0x10, | |
| 490 | macho.BIND_OPCODE_DO_BIND, | |
| 491 | macho.BIND_OPCODE_DO_BIND, | |
| 492 | macho.BIND_OPCODE_DO_BIND, | |
| 493 | macho.BIND_OPCODE_DO_BIND, | |
| 494 | macho.BIND_OPCODE_DONE, | |
| 495 | }, bind.buffer.items); | |
| 496 | } | |
| 497 | ||
| 498 | test "bind - multiple occurrences with skip and addend" { | |
| 499 | const gpa = testing.allocator; | |
| 500 | ||
| 501 | var test_context = try generateTestContext(); | |
| 502 | defer test_context.deinit(gpa); | |
| 503 | ||
| 504 | var bind = Bind(TestContext, TestContext.Target){}; | |
| 505 | defer bind.deinit(gpa); | |
| 506 | ||
| 507 | try bind.entries.append(gpa, .{ | |
| 508 | .offset = 0x0, | |
| 509 | .segment_id = 1, | |
| 510 | .target = TestContext.Target{ .index = 0 }, | |
| 511 | .addend = 0x10, | |
| 512 | }); | |
| 513 | try bind.entries.append(gpa, .{ | |
| 514 | .offset = 0x10, | |
| 515 | .segment_id = 1, | |
| 516 | .target = TestContext.Target{ .index = 0 }, | |
| 517 | .addend = 0x10, | |
| 518 | }); | |
| 519 | try bind.entries.append(gpa, .{ | |
| 520 | .offset = 0x20, | |
| 521 | .segment_id = 1, | |
| 522 | .target = TestContext.Target{ .index = 0 }, | |
| 523 | .addend = 0x10, | |
| 524 | }); | |
| 525 | try bind.entries.append(gpa, .{ | |
| 526 | .offset = 0x30, | |
| 527 | .segment_id = 1, | |
| 528 | .target = TestContext.Target{ .index = 0 }, | |
| 529 | .addend = 0x10, | |
| 530 | }); | |
| 531 | ||
| 532 | try bind.finalize(gpa, test_context); | |
| 533 | try testing.expectEqualSlices(u8, &[_]u8{ | |
| 534 | macho.BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB | 1, | |
| 535 | 0x0, | |
| 536 | macho.BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM | 0, | |
| 537 | 0x5f, | |
| 538 | 0x69, | |
| 539 | 0x6d, | |
| 540 | 0x70, | |
| 541 | 0x6f, | |
| 542 | 0x72, | |
| 543 | 0x74, | |
| 544 | 0x5f, | |
| 545 | 0x31, | |
| 546 | 0x0, | |
| 547 | macho.BIND_OPCODE_SET_TYPE_IMM | 1, | |
| 548 | macho.BIND_OPCODE_SET_DYLIB_ORDINAL_IMM | 1, | |
| 549 | macho.BIND_OPCODE_SET_ADDEND_SLEB, | |
| 550 | 0x10, | |
| 551 | macho.BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB, | |
| 552 | 0x4, | |
| 553 | 0x8, | |
| 554 | macho.BIND_OPCODE_DONE, | |
| 555 | }, bind.buffer.items); | |
| 556 | } | |
| 557 | ||
| 558 | test "bind - complex" { | |
| 559 | const gpa = testing.allocator; | |
| 560 | ||
| 561 | var test_context = try generateTestContext(); | |
| 562 | defer test_context.deinit(gpa); | |
| 563 | ||
| 564 | var bind = Bind(TestContext, TestContext.Target){}; | |
| 565 | defer bind.deinit(gpa); | |
| 566 | ||
| 567 | try bind.entries.append(gpa, .{ | |
| 568 | .offset = 0x58, | |
| 569 | .segment_id = 1, | |
| 570 | .target = TestContext.Target{ .index = 0 }, | |
| 571 | .addend = 0, | |
| 572 | }); | |
| 573 | try bind.entries.append(gpa, .{ | |
| 574 | .offset = 0x100, | |
| 575 | .segment_id = 1, | |
| 576 | .target = TestContext.Target{ .index = 1 }, | |
| 577 | .addend = 0x10, | |
| 578 | }); | |
| 579 | try bind.entries.append(gpa, .{ | |
| 580 | .offset = 0x110, | |
| 581 | .segment_id = 1, | |
| 582 | .target = TestContext.Target{ .index = 1 }, | |
| 583 | .addend = 0x10, | |
| 584 | }); | |
| 585 | try bind.entries.append(gpa, .{ | |
| 586 | .offset = 0x130, | |
| 587 | .segment_id = 1, | |
| 588 | .target = TestContext.Target{ .index = 1 }, | |
| 589 | .addend = 0x10, | |
| 590 | }); | |
| 591 | try bind.entries.append(gpa, .{ | |
| 592 | .offset = 0x140, | |
| 593 | .segment_id = 1, | |
| 594 | .target = TestContext.Target{ .index = 1 }, | |
| 595 | .addend = 0x10, | |
| 596 | }); | |
| 597 | try bind.entries.append(gpa, .{ | |
| 598 | .offset = 0x148, | |
| 599 | .segment_id = 1, | |
| 600 | .target = TestContext.Target{ .index = 2 }, | |
| 601 | .addend = 0, | |
| 602 | }); | |
| 603 | ||
| 604 | try bind.finalize(gpa, test_context); | |
| 605 | try testing.expectEqualSlices(u8, &[_]u8{ | |
| 606 | macho.BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB | 1, | |
| 607 | 0x0, | |
| 608 | macho.BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM | 0, | |
| 609 | 0x5f, | |
| 610 | 0x69, | |
| 611 | 0x6d, | |
| 612 | 0x70, | |
| 613 | 0x6f, | |
| 614 | 0x72, | |
| 615 | 0x74, | |
| 616 | 0x5f, | |
| 617 | 0x31, | |
| 618 | 0x0, | |
| 619 | macho.BIND_OPCODE_SET_TYPE_IMM | 1, | |
| 620 | macho.BIND_OPCODE_SET_DYLIB_ORDINAL_IMM | 1, | |
| 621 | macho.BIND_OPCODE_ADD_ADDR_ULEB, | |
| 622 | 0x58, | |
| 623 | macho.BIND_OPCODE_DO_BIND, | |
| 624 | macho.BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM | 0, | |
| 625 | 0x5f, | |
| 626 | 0x69, | |
| 627 | 0x6d, | |
| 628 | 0x70, | |
| 629 | 0x6f, | |
| 630 | 0x72, | |
| 631 | 0x74, | |
| 632 | 0x5f, | |
| 633 | 0x32, | |
| 634 | 0x0, | |
| 635 | macho.BIND_OPCODE_SET_TYPE_IMM | 1, | |
| 636 | macho.BIND_OPCODE_SET_DYLIB_ORDINAL_IMM | 1, | |
| 637 | macho.BIND_OPCODE_SET_ADDEND_SLEB, | |
| 638 | 0x10, | |
| 639 | macho.BIND_OPCODE_ADD_ADDR_ULEB, | |
| 640 | 0xa0, | |
| 641 | 0x1, | |
| 642 | macho.BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB, | |
| 643 | 0x2, | |
| 644 | 0x8, | |
| 645 | macho.BIND_OPCODE_ADD_ADDR_ULEB, | |
| 646 | 0x10, | |
| 647 | macho.BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB, | |
| 648 | 0x2, | |
| 649 | 0x8, | |
| 650 | macho.BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM | 0, | |
| 651 | 0x5f, | |
| 652 | 0x69, | |
| 653 | 0x6d, | |
| 654 | 0x70, | |
| 655 | 0x6f, | |
| 656 | 0x72, | |
| 657 | 0x74, | |
| 658 | 0x5f, | |
| 659 | 0x33, | |
| 660 | 0x0, | |
| 661 | macho.BIND_OPCODE_SET_TYPE_IMM | 1, | |
| 662 | macho.BIND_OPCODE_SET_DYLIB_ORDINAL_IMM | 1, | |
| 663 | macho.BIND_OPCODE_SET_ADDEND_SLEB, | |
| 664 | 0x0, | |
| 665 | macho.BIND_OPCODE_ADD_ADDR_ULEB, | |
| 666 | 0xf8, | |
| 667 | 0xff, | |
| 668 | 0xff, | |
| 669 | 0xff, | |
| 670 | 0xff, | |
| 671 | 0xff, | |
| 672 | 0xff, | |
| 673 | 0xff, | |
| 674 | 0xff, | |
| 675 | 0x1, | |
| 676 | macho.BIND_OPCODE_DO_BIND, | |
| 677 | macho.BIND_OPCODE_DONE, | |
| 678 | }, bind.buffer.items); | |
| 679 | } | |
| 680 | ||
| 681 | test "lazy bind" { | |
| 682 | const gpa = testing.allocator; | |
| 683 | ||
| 684 | var test_context = try generateTestContext(); | |
| 685 | defer test_context.deinit(gpa); | |
| 686 | ||
| 687 | var bind = LazyBind(TestContext, TestContext.Target){}; | |
| 688 | defer bind.deinit(gpa); | |
| 689 | ||
| 690 | try bind.entries.append(gpa, .{ | |
| 691 | .offset = 0x10, | |
| 692 | .segment_id = 1, | |
| 693 | .target = TestContext.Target{ .index = 0 }, | |
| 694 | .addend = 0, | |
| 695 | }); | |
| 696 | try bind.entries.append(gpa, .{ | |
| 697 | .offset = 0x20, | |
| 698 | .segment_id = 2, | |
| 699 | .target = TestContext.Target{ .index = 1 }, | |
| 700 | .addend = 0x10, | |
| 701 | }); | |
| 702 | ||
| 703 | try bind.finalize(gpa, test_context); | |
| 704 | try testing.expectEqualSlices(u8, &[_]u8{ | |
| 705 | macho.BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB | 1, | |
| 706 | 0x10, | |
| 707 | macho.BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM | 0, | |
| 708 | 0x5f, | |
| 709 | 0x69, | |
| 710 | 0x6d, | |
| 711 | 0x70, | |
| 712 | 0x6f, | |
| 713 | 0x72, | |
| 714 | 0x74, | |
| 715 | 0x5f, | |
| 716 | 0x31, | |
| 717 | 0x0, | |
| 718 | macho.BIND_OPCODE_SET_DYLIB_ORDINAL_IMM | 1, | |
| 719 | macho.BIND_OPCODE_DO_BIND, | |
| 720 | macho.BIND_OPCODE_DONE, | |
| 721 | macho.BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB | 2, | |
| 722 | 0x20, | |
| 723 | macho.BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM | 0, | |
| 724 | 0x5f, | |
| 725 | 0x69, | |
| 726 | 0x6d, | |
| 727 | 0x70, | |
| 728 | 0x6f, | |
| 729 | 0x72, | |
| 730 | 0x74, | |
| 731 | 0x5f, | |
| 732 | 0x32, | |
| 733 | 0x0, | |
| 734 | macho.BIND_OPCODE_SET_DYLIB_ORDINAL_IMM | 1, | |
| 735 | macho.BIND_OPCODE_SET_ADDEND_SLEB, | |
| 736 | 0x10, | |
| 737 | macho.BIND_OPCODE_DO_BIND, | |
| 738 | macho.BIND_OPCODE_DONE, | |
| 739 | }, bind.buffer.items); | |
| 740 | } |
src/link/MachO/zld.zig+84-155| ... | ... | @@ -9,7 +9,6 @@ const math = std.math; |
| 9 | 9 | const mem = std.mem; |
| 10 | 10 | |
| 11 | 11 | const aarch64 = @import("../../arch/aarch64/bits.zig"); |
| 12 | const bind = @import("bind.zig"); | |
| 13 | 12 | const dead_strip = @import("dead_strip.zig"); |
| 14 | 13 | const fat = @import("fat.zig"); |
| 15 | 14 | const link = @import("../../link.zig"); |
| ... | ... | @@ -32,6 +31,10 @@ const Object = @import("Object.zig"); |
| 32 | 31 | const StringTable = @import("../strtab.zig").StringTable; |
| 33 | 32 | const Trie = @import("Trie.zig"); |
| 34 | 33 | |
| 34 | const Bind = @import("dyld_info/bind.zig").Bind(*const Zld, SymbolWithLoc); | |
| 35 | const LazyBind = @import("dyld_info/bind.zig").LazyBind(*const Zld, SymbolWithLoc); | |
| 36 | const Rebase = @import("dyld_info/Rebase.zig"); | |
| 37 | ||
| 35 | 38 | pub const Zld = struct { |
| 36 | 39 | gpa: Allocator, |
| 37 | 40 | file: fs.File, |
| ... | ... | @@ -1778,14 +1781,14 @@ pub const Zld = struct { |
| 1778 | 1781 | fn collectRebaseDataFromContainer( |
| 1779 | 1782 | self: *Zld, |
| 1780 | 1783 | sect_id: u8, |
| 1781 | pointers: *std.ArrayList(bind.Pointer), | |
| 1784 | rebase: *Rebase, | |
| 1782 | 1785 | container: anytype, |
| 1783 | 1786 | ) !void { |
| 1784 | 1787 | const slice = self.sections.slice(); |
| 1785 | 1788 | const segment_index = slice.items(.segment_index)[sect_id]; |
| 1786 | 1789 | const seg = self.getSegment(sect_id); |
| 1787 | 1790 | |
| 1788 | try pointers.ensureUnusedCapacity(container.items.len); | |
| 1791 | try rebase.entries.ensureUnusedCapacity(self.gpa, container.items.len); | |
| 1789 | 1792 | |
| 1790 | 1793 | for (container.items) |entry| { |
| 1791 | 1794 | const target_sym = entry.getTargetSymbol(self); |
| ... | ... | @@ -1796,19 +1799,19 @@ pub const Zld = struct { |
| 1796 | 1799 | |
| 1797 | 1800 | log.debug(" | rebase at {x}", .{base_offset}); |
| 1798 | 1801 | |
| 1799 | pointers.appendAssumeCapacity(.{ | |
| 1802 | rebase.entries.appendAssumeCapacity(.{ | |
| 1800 | 1803 | .offset = base_offset, |
| 1801 | 1804 | .segment_id = segment_index, |
| 1802 | 1805 | }); |
| 1803 | 1806 | } |
| 1804 | 1807 | } |
| 1805 | 1808 | |
| 1806 | fn collectRebaseData(self: *Zld, pointers: *std.ArrayList(bind.Pointer)) !void { | |
| 1809 | fn collectRebaseData(self: *Zld, rebase: *Rebase, reverse_lookups: [][]u32) !void { | |
| 1807 | 1810 | log.debug("collecting rebase data", .{}); |
| 1808 | 1811 | |
| 1809 | 1812 | // First, unpack GOT entries |
| 1810 | 1813 | if (self.getSectionByName("__DATA_CONST", "__got")) |sect_id| { |
| 1811 | try self.collectRebaseDataFromContainer(sect_id, pointers, self.got_entries); | |
| 1814 | try self.collectRebaseDataFromContainer(sect_id, rebase, self.got_entries); | |
| 1812 | 1815 | } |
| 1813 | 1816 | |
| 1814 | 1817 | const slice = self.sections.slice(); |
| ... | ... | @@ -1820,7 +1823,7 @@ pub const Zld = struct { |
| 1820 | 1823 | const seg = self.getSegment(sect_id); |
| 1821 | 1824 | var atom_index = slice.items(.first_atom_index)[sect_id]; |
| 1822 | 1825 | |
| 1823 | try pointers.ensureUnusedCapacity(self.stubs.items.len); | |
| 1826 | try rebase.entries.ensureUnusedCapacity(self.gpa, self.stubs.items.len); | |
| 1824 | 1827 | |
| 1825 | 1828 | while (true) { |
| 1826 | 1829 | const atom = self.getAtom(atom_index); |
| ... | ... | @@ -1829,7 +1832,7 @@ pub const Zld = struct { |
| 1829 | 1832 | |
| 1830 | 1833 | log.debug(" | rebase at {x}", .{base_offset}); |
| 1831 | 1834 | |
| 1832 | pointers.appendAssumeCapacity(.{ | |
| 1835 | rebase.entries.appendAssumeCapacity(.{ | |
| 1833 | 1836 | .offset = base_offset, |
| 1834 | 1837 | .segment_id = segment_index, |
| 1835 | 1838 | }); |
| ... | ... | @@ -1896,13 +1899,16 @@ pub const Zld = struct { |
| 1896 | 1899 | }, |
| 1897 | 1900 | else => unreachable, |
| 1898 | 1901 | } |
| 1902 | const target = Atom.parseRelocTarget(self, atom_index, rel, reverse_lookups[atom.getFile().?]); | |
| 1903 | const target_sym = self.getSymbol(target); | |
| 1904 | if (target_sym.undf()) continue; | |
| 1899 | 1905 | |
| 1900 | 1906 | const base_offset = @intCast(i32, sym.n_value - segment.vmaddr); |
| 1901 | 1907 | const rel_offset = rel.r_address - base_rel_offset; |
| 1902 | 1908 | const offset = @intCast(u64, base_offset + rel_offset); |
| 1903 | 1909 | log.debug(" | rebase at {x}", .{offset}); |
| 1904 | 1910 | |
| 1905 | try pointers.append(.{ | |
| 1911 | try rebase.entries.append(self.gpa, .{ | |
| 1906 | 1912 | .offset = offset, |
| 1907 | 1913 | .segment_id = segment_index, |
| 1908 | 1914 | }); |
| ... | ... | @@ -1914,19 +1920,21 @@ pub const Zld = struct { |
| 1914 | 1920 | } else break; |
| 1915 | 1921 | } |
| 1916 | 1922 | } |
| 1923 | ||
| 1924 | try rebase.finalize(self.gpa); | |
| 1917 | 1925 | } |
| 1918 | 1926 | |
| 1919 | 1927 | fn collectBindDataFromContainer( |
| 1920 | 1928 | self: *Zld, |
| 1921 | 1929 | sect_id: u8, |
| 1922 | pointers: *std.ArrayList(bind.Pointer), | |
| 1930 | bind: *Bind, | |
| 1923 | 1931 | container: anytype, |
| 1924 | 1932 | ) !void { |
| 1925 | 1933 | const slice = self.sections.slice(); |
| 1926 | 1934 | const segment_index = slice.items(.segment_index)[sect_id]; |
| 1927 | 1935 | const seg = self.getSegment(sect_id); |
| 1928 | 1936 | |
| 1929 | try pointers.ensureUnusedCapacity(container.items.len); | |
| 1937 | try bind.entries.ensureUnusedCapacity(self.gpa, container.items.len); | |
| 1930 | 1938 | |
| 1931 | 1939 | for (container.items) |entry| { |
| 1932 | 1940 | const bind_sym_name = entry.getTargetSymbolName(self); |
| ... | ... | @@ -1937,7 +1945,6 @@ pub const Zld = struct { |
| 1937 | 1945 | const base_offset = sym.n_value - seg.vmaddr; |
| 1938 | 1946 | |
| 1939 | 1947 | const dylib_ordinal = @divTrunc(@bitCast(i16, bind_sym.n_desc), macho.N_SYMBOL_RESOLVER); |
| 1940 | var flags: u4 = 0; | |
| 1941 | 1948 | log.debug(" | bind at {x}, import('{s}') in dylib({d})", .{ |
| 1942 | 1949 | base_offset, |
| 1943 | 1950 | bind_sym_name, |
| ... | ... | @@ -1945,29 +1952,27 @@ pub const Zld = struct { |
| 1945 | 1952 | }); |
| 1946 | 1953 | if (bind_sym.weakRef()) { |
| 1947 | 1954 | log.debug(" | marking as weak ref ", .{}); |
| 1948 | flags |= @truncate(u4, macho.BIND_SYMBOL_FLAGS_WEAK_IMPORT); | |
| 1949 | 1955 | } |
| 1950 | pointers.appendAssumeCapacity(.{ | |
| 1956 | bind.entries.appendAssumeCapacity(.{ | |
| 1957 | .target = entry.target, | |
| 1951 | 1958 | .offset = base_offset, |
| 1952 | 1959 | .segment_id = segment_index, |
| 1953 | .dylib_ordinal = dylib_ordinal, | |
| 1954 | .name = bind_sym_name, | |
| 1955 | .bind_flags = flags, | |
| 1960 | .addend = 0, | |
| 1956 | 1961 | }); |
| 1957 | 1962 | } |
| 1958 | 1963 | } |
| 1959 | 1964 | |
| 1960 | fn collectBindData(self: *Zld, pointers: *std.ArrayList(bind.Pointer), reverse_lookups: [][]u32) !void { | |
| 1965 | fn collectBindData(self: *Zld, bind: *Bind, reverse_lookups: [][]u32) !void { | |
| 1961 | 1966 | log.debug("collecting bind data", .{}); |
| 1962 | 1967 | |
| 1963 | 1968 | // First, unpack GOT section |
| 1964 | 1969 | if (self.getSectionByName("__DATA_CONST", "__got")) |sect_id| { |
| 1965 | try self.collectBindDataFromContainer(sect_id, pointers, self.got_entries); | |
| 1970 | try self.collectBindDataFromContainer(sect_id, bind, self.got_entries); | |
| 1966 | 1971 | } |
| 1967 | 1972 | |
| 1968 | 1973 | // Next, unpack TLV pointers section |
| 1969 | 1974 | if (self.getSectionByName("__DATA", "__thread_ptrs")) |sect_id| { |
| 1970 | try self.collectBindDataFromContainer(sect_id, pointers, self.tlv_ptr_entries); | |
| 1975 | try self.collectBindDataFromContainer(sect_id, bind, self.tlv_ptr_entries); | |
| 1971 | 1976 | } |
| 1972 | 1977 | |
| 1973 | 1978 | // Finally, unpack the rest. |
| ... | ... | @@ -2033,27 +2038,27 @@ pub const Zld = struct { |
| 2033 | 2038 | const bind_sym = self.getSymbol(global); |
| 2034 | 2039 | if (!bind_sym.undf()) continue; |
| 2035 | 2040 | |
| 2036 | const base_offset = @intCast(i32, sym.n_value - segment.vmaddr); | |
| 2037 | const rel_offset = rel.r_address - base_rel_offset; | |
| 2041 | const base_offset = sym.n_value - segment.vmaddr; | |
| 2042 | const rel_offset = @intCast(u32, rel.r_address - base_rel_offset); | |
| 2038 | 2043 | const offset = @intCast(u64, base_offset + rel_offset); |
| 2044 | const code = Atom.getAtomCode(self, atom_index); | |
| 2045 | const addend = mem.readIntLittle(i64, code[rel_offset..][0..8]); | |
| 2039 | 2046 | |
| 2040 | 2047 | const dylib_ordinal = @divTrunc(@bitCast(i16, bind_sym.n_desc), macho.N_SYMBOL_RESOLVER); |
| 2041 | var flags: u4 = 0; | |
| 2042 | 2048 | log.debug(" | bind at {x}, import('{s}') in dylib({d})", .{ |
| 2043 | 2049 | base_offset, |
| 2044 | 2050 | bind_sym_name, |
| 2045 | 2051 | dylib_ordinal, |
| 2046 | 2052 | }); |
| 2053 | log.debug(" | with addend {x}", .{addend}); | |
| 2047 | 2054 | if (bind_sym.weakRef()) { |
| 2048 | 2055 | log.debug(" | marking as weak ref ", .{}); |
| 2049 | flags |= @truncate(u4, macho.BIND_SYMBOL_FLAGS_WEAK_IMPORT); | |
| 2050 | 2056 | } |
| 2051 | try pointers.append(.{ | |
| 2057 | try bind.entries.append(self.gpa, .{ | |
| 2058 | .target = global, | |
| 2052 | 2059 | .offset = offset, |
| 2053 | 2060 | .segment_id = segment_index, |
| 2054 | .dylib_ordinal = dylib_ordinal, | |
| 2055 | .name = bind_sym_name, | |
| 2056 | .bind_flags = flags, | |
| 2061 | .addend = addend, | |
| 2057 | 2062 | }); |
| 2058 | 2063 | } |
| 2059 | 2064 | } |
| ... | ... | @@ -2062,9 +2067,11 @@ pub const Zld = struct { |
| 2062 | 2067 | } else break; |
| 2063 | 2068 | } |
| 2064 | 2069 | } |
| 2070 | ||
| 2071 | try bind.finalize(self.gpa, self); | |
| 2065 | 2072 | } |
| 2066 | 2073 | |
| 2067 | fn collectLazyBindData(self: *Zld, pointers: *std.ArrayList(bind.Pointer)) !void { | |
| 2074 | fn collectLazyBindData(self: *Zld, lazy_bind: *LazyBind) !void { | |
| 2068 | 2075 | const sect_id = self.getSectionByName("__DATA", "__la_symbol_ptr") orelse return; |
| 2069 | 2076 | |
| 2070 | 2077 | log.debug("collecting lazy bind data", .{}); |
| ... | ... | @@ -2075,7 +2082,7 @@ pub const Zld = struct { |
| 2075 | 2082 | var atom_index = slice.items(.first_atom_index)[sect_id]; |
| 2076 | 2083 | |
| 2077 | 2084 | // TODO: we actually don't need to store lazy pointer atoms as they are synthetically generated by the linker |
| 2078 | try pointers.ensureUnusedCapacity(self.stubs.items.len); | |
| 2085 | try lazy_bind.entries.ensureUnusedCapacity(self.gpa, self.stubs.items.len); | |
| 2079 | 2086 | |
| 2080 | 2087 | var count: u32 = 0; |
| 2081 | 2088 | while (true) : (count += 1) { |
| ... | ... | @@ -2090,7 +2097,6 @@ pub const Zld = struct { |
| 2090 | 2097 | const bind_sym = stub_entry.getTargetSymbol(self); |
| 2091 | 2098 | const bind_sym_name = stub_entry.getTargetSymbolName(self); |
| 2092 | 2099 | const dylib_ordinal = @divTrunc(@bitCast(i16, bind_sym.n_desc), macho.N_SYMBOL_RESOLVER); |
| 2093 | var flags: u4 = 0; | |
| 2094 | 2100 | log.debug(" | lazy bind at {x}, import('{s}') in dylib({d})", .{ |
| 2095 | 2101 | base_offset, |
| 2096 | 2102 | bind_sym_name, |
| ... | ... | @@ -2098,20 +2104,20 @@ pub const Zld = struct { |
| 2098 | 2104 | }); |
| 2099 | 2105 | if (bind_sym.weakRef()) { |
| 2100 | 2106 | log.debug(" | marking as weak ref ", .{}); |
| 2101 | flags |= @truncate(u4, macho.BIND_SYMBOL_FLAGS_WEAK_IMPORT); | |
| 2102 | 2107 | } |
| 2103 | pointers.appendAssumeCapacity(.{ | |
| 2108 | lazy_bind.entries.appendAssumeCapacity(.{ | |
| 2109 | .target = stub_entry.target, | |
| 2104 | 2110 | .offset = base_offset, |
| 2105 | 2111 | .segment_id = segment_index, |
| 2106 | .dylib_ordinal = dylib_ordinal, | |
| 2107 | .name = bind_sym_name, | |
| 2108 | .bind_flags = flags, | |
| 2112 | .addend = 0, | |
| 2109 | 2113 | }); |
| 2110 | 2114 | |
| 2111 | 2115 | if (atom.next_index) |next_index| { |
| 2112 | 2116 | atom_index = next_index; |
| 2113 | 2117 | } else break; |
| 2114 | 2118 | } |
| 2119 | ||
| 2120 | try lazy_bind.finalize(self.gpa, self); | |
| 2115 | 2121 | } |
| 2116 | 2122 | |
| 2117 | 2123 | fn collectExportData(self: *Zld, trie: *Trie) !void { |
| ... | ... | @@ -2161,17 +2167,17 @@ pub const Zld = struct { |
| 2161 | 2167 | fn writeDyldInfoData(self: *Zld, reverse_lookups: [][]u32) !void { |
| 2162 | 2168 | const gpa = self.gpa; |
| 2163 | 2169 | |
| 2164 | var rebase_pointers = std.ArrayList(bind.Pointer).init(gpa); | |
| 2165 | defer rebase_pointers.deinit(); | |
| 2166 | try self.collectRebaseData(&rebase_pointers); | |
| 2170 | var rebase = Rebase{}; | |
| 2171 | defer rebase.deinit(gpa); | |
| 2172 | try self.collectRebaseData(&rebase, reverse_lookups); | |
| 2167 | 2173 | |
| 2168 | var bind_pointers = std.ArrayList(bind.Pointer).init(gpa); | |
| 2169 | defer bind_pointers.deinit(); | |
| 2170 | try self.collectBindData(&bind_pointers, reverse_lookups); | |
| 2174 | var bind = Bind{}; | |
| 2175 | defer bind.deinit(gpa); | |
| 2176 | try self.collectBindData(&bind, reverse_lookups); | |
| 2171 | 2177 | |
| 2172 | var lazy_bind_pointers = std.ArrayList(bind.Pointer).init(gpa); | |
| 2173 | defer lazy_bind_pointers.deinit(); | |
| 2174 | try self.collectLazyBindData(&lazy_bind_pointers); | |
| 2178 | var lazy_bind = LazyBind{}; | |
| 2179 | defer lazy_bind.deinit(gpa); | |
| 2180 | try self.collectLazyBindData(&lazy_bind); | |
| 2175 | 2181 | |
| 2176 | 2182 | var trie = Trie{}; |
| 2177 | 2183 | defer trie.deinit(gpa); |
| ... | ... | @@ -2180,17 +2186,17 @@ pub const Zld = struct { |
| 2180 | 2186 | const link_seg = self.getLinkeditSegmentPtr(); |
| 2181 | 2187 | assert(mem.isAlignedGeneric(u64, link_seg.fileoff, @alignOf(u64))); |
| 2182 | 2188 | const rebase_off = link_seg.fileoff; |
| 2183 | const rebase_size = try bind.rebaseInfoSize(rebase_pointers.items); | |
| 2189 | const rebase_size = rebase.size(); | |
| 2184 | 2190 | const rebase_size_aligned = mem.alignForwardGeneric(u64, rebase_size, @alignOf(u64)); |
| 2185 | 2191 | log.debug("writing rebase info from 0x{x} to 0x{x}", .{ rebase_off, rebase_off + rebase_size_aligned }); |
| 2186 | 2192 | |
| 2187 | 2193 | const bind_off = rebase_off + rebase_size_aligned; |
| 2188 | const bind_size = try bind.bindInfoSize(bind_pointers.items); | |
| 2194 | const bind_size = bind.size(); | |
| 2189 | 2195 | const bind_size_aligned = mem.alignForwardGeneric(u64, bind_size, @alignOf(u64)); |
| 2190 | 2196 | log.debug("writing bind info from 0x{x} to 0x{x}", .{ bind_off, bind_off + bind_size_aligned }); |
| 2191 | 2197 | |
| 2192 | 2198 | const lazy_bind_off = bind_off + bind_size_aligned; |
| 2193 | const lazy_bind_size = try bind.lazyBindInfoSize(lazy_bind_pointers.items); | |
| 2199 | const lazy_bind_size = lazy_bind.size(); | |
| 2194 | 2200 | const lazy_bind_size_aligned = mem.alignForwardGeneric(u64, lazy_bind_size, @alignOf(u64)); |
| 2195 | 2201 | log.debug("writing lazy bind info from 0x{x} to 0x{x}", .{ |
| 2196 | 2202 | lazy_bind_off, |
| ... | ... | @@ -2214,13 +2220,13 @@ pub const Zld = struct { |
| 2214 | 2220 | var stream = std.io.fixedBufferStream(buffer); |
| 2215 | 2221 | const writer = stream.writer(); |
| 2216 | 2222 | |
| 2217 | try bind.writeRebaseInfo(rebase_pointers.items, writer); | |
| 2223 | try rebase.write(writer); | |
| 2218 | 2224 | try stream.seekTo(bind_off - rebase_off); |
| 2219 | 2225 | |
| 2220 | try bind.writeBindInfo(bind_pointers.items, writer); | |
| 2226 | try bind.write(writer); | |
| 2221 | 2227 | try stream.seekTo(lazy_bind_off - rebase_off); |
| 2222 | 2228 | |
| 2223 | try bind.writeLazyBindInfo(lazy_bind_pointers.items, writer); | |
| 2229 | try lazy_bind.write(writer); | |
| 2224 | 2230 | try stream.seekTo(export_off - rebase_off); |
| 2225 | 2231 | |
| 2226 | 2232 | _ = try trie.write(writer); |
| ... | ... | @@ -2231,10 +2237,7 @@ pub const Zld = struct { |
| 2231 | 2237 | }); |
| 2232 | 2238 | |
| 2233 | 2239 | try self.file.pwriteAll(buffer, rebase_off); |
| 2234 | ||
| 2235 | const offset = math.cast(usize, lazy_bind_off - rebase_off) orelse return error.Overflow; | |
| 2236 | const size = math.cast(usize, lazy_bind_size) orelse return error.Overflow; | |
| 2237 | try self.populateLazyBindOffsetsInStubHelper(buffer[offset..][0..size]); | |
| 2240 | try self.populateLazyBindOffsetsInStubHelper(lazy_bind); | |
| 2238 | 2241 | |
| 2239 | 2242 | self.dyld_info_cmd.rebase_off = @intCast(u32, rebase_off); |
| 2240 | 2243 | self.dyld_info_cmd.rebase_size = @intCast(u32, rebase_size_aligned); |
| ... | ... | @@ -2246,116 +2249,37 @@ pub const Zld = struct { |
| 2246 | 2249 | self.dyld_info_cmd.export_size = @intCast(u32, export_size_aligned); |
| 2247 | 2250 | } |
| 2248 | 2251 | |
| 2249 | fn populateLazyBindOffsetsInStubHelper(self: *Zld, buffer: []const u8) !void { | |
| 2250 | const gpa = self.gpa; | |
| 2251 | ||
| 2252 | const stub_helper_section_index = self.getSectionByName("__TEXT", "__stub_helper") orelse return; | |
| 2253 | const la_symbol_ptr_section_index = self.getSectionByName("__DATA", "__la_symbol_ptr") orelse return; | |
| 2252 | fn populateLazyBindOffsetsInStubHelper(self: *Zld, lazy_bind: LazyBind) !void { | |
| 2253 | if (lazy_bind.size() == 0) return; | |
| 2254 | 2254 | |
| 2255 | if (self.stub_helper_preamble_sym_index == null) return; | |
| 2255 | const stub_helper_section_index = self.getSectionByName("__TEXT", "__stub_helper").?; | |
| 2256 | assert(self.stub_helper_preamble_sym_index != null); | |
| 2256 | 2257 | |
| 2257 | 2258 | const section = self.sections.get(stub_helper_section_index); |
| 2258 | const last_atom_index = section.last_atom_index; | |
| 2259 | ||
| 2260 | var table = std.AutoHashMap(i64, AtomIndex).init(gpa); | |
| 2261 | defer table.deinit(); | |
| 2262 | ||
| 2263 | { | |
| 2264 | var stub_atom_index = last_atom_index; | |
| 2265 | var laptr_atom_index = self.sections.items(.last_atom_index)[la_symbol_ptr_section_index]; | |
| 2266 | ||
| 2267 | const base_addr = blk: { | |
| 2268 | const segment_index = self.getSegmentByName("__DATA").?; | |
| 2269 | const seg = self.segments.items[segment_index]; | |
| 2270 | break :blk seg.vmaddr; | |
| 2271 | }; | |
| 2272 | ||
| 2273 | while (true) { | |
| 2274 | const stub_atom = self.getAtom(stub_atom_index); | |
| 2275 | const laptr_atom = self.getAtom(laptr_atom_index); | |
| 2276 | const laptr_off = blk: { | |
| 2277 | const sym = self.getSymbolPtr(laptr_atom.getSymbolWithLoc()); | |
| 2278 | break :blk @intCast(i64, sym.n_value - base_addr); | |
| 2279 | }; | |
| 2280 | ||
| 2281 | try table.putNoClobber(laptr_off, stub_atom_index); | |
| 2282 | ||
| 2283 | if (laptr_atom.prev_index) |prev_index| { | |
| 2284 | laptr_atom_index = prev_index; | |
| 2285 | stub_atom_index = stub_atom.prev_index.?; | |
| 2286 | } else break; | |
| 2287 | } | |
| 2288 | } | |
| 2289 | ||
| 2290 | var stream = std.io.fixedBufferStream(buffer); | |
| 2291 | var reader = stream.reader(); | |
| 2292 | var offsets = std.ArrayList(struct { sym_offset: i64, offset: u32 }).init(gpa); | |
| 2293 | try offsets.append(.{ .sym_offset = undefined, .offset = 0 }); | |
| 2294 | defer offsets.deinit(); | |
| 2295 | var valid_block = false; | |
| 2296 | ||
| 2297 | while (true) { | |
| 2298 | const inst = reader.readByte() catch |err| switch (err) { | |
| 2299 | error.EndOfStream => break, | |
| 2300 | }; | |
| 2301 | const opcode: u8 = inst & macho.BIND_OPCODE_MASK; | |
| 2302 | ||
| 2303 | switch (opcode) { | |
| 2304 | macho.BIND_OPCODE_DO_BIND => { | |
| 2305 | valid_block = true; | |
| 2306 | }, | |
| 2307 | macho.BIND_OPCODE_DONE => { | |
| 2308 | if (valid_block) { | |
| 2309 | const offset = try stream.getPos(); | |
| 2310 | try offsets.append(.{ .sym_offset = undefined, .offset = @intCast(u32, offset) }); | |
| 2311 | } | |
| 2312 | valid_block = false; | |
| 2313 | }, | |
| 2314 | macho.BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM => { | |
| 2315 | var next = try reader.readByte(); | |
| 2316 | while (next != @as(u8, 0)) { | |
| 2317 | next = try reader.readByte(); | |
| 2318 | } | |
| 2319 | }, | |
| 2320 | macho.BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB => { | |
| 2321 | var inserted = offsets.pop(); | |
| 2322 | inserted.sym_offset = try std.leb.readILEB128(i64, reader); | |
| 2323 | try offsets.append(inserted); | |
| 2324 | }, | |
| 2325 | macho.BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB => { | |
| 2326 | _ = try std.leb.readULEB128(u64, reader); | |
| 2327 | }, | |
| 2328 | macho.BIND_OPCODE_SET_ADDEND_SLEB => { | |
| 2329 | _ = try std.leb.readILEB128(i64, reader); | |
| 2330 | }, | |
| 2331 | else => {}, | |
| 2332 | } | |
| 2333 | } | |
| 2334 | ||
| 2335 | const header = self.sections.items(.header)[stub_helper_section_index]; | |
| 2336 | 2259 | const stub_offset: u4 = switch (self.options.target.cpu.arch) { |
| 2337 | 2260 | .x86_64 => 1, |
| 2338 | 2261 | .aarch64 => 2 * @sizeOf(u32), |
| 2339 | 2262 | else => unreachable, |
| 2340 | 2263 | }; |
| 2341 | var buf: [@sizeOf(u32)]u8 = undefined; | |
| 2342 | _ = offsets.pop(); | |
| 2264 | const header = section.header; | |
| 2265 | var atom_index = section.first_atom_index; | |
| 2266 | atom_index = self.getAtom(atom_index).next_index.?; // skip preamble | |
| 2343 | 2267 | |
| 2344 | while (offsets.popOrNull()) |bind_offset| { | |
| 2345 | const atom_index = table.get(bind_offset.sym_offset).?; | |
| 2268 | var index: usize = 0; | |
| 2269 | while (true) { | |
| 2346 | 2270 | const atom = self.getAtom(atom_index); |
| 2347 | const sym = self.getSymbol(atom.getSymbolWithLoc()); | |
| 2271 | const atom_sym = self.getSymbol(atom.getSymbolWithLoc()); | |
| 2272 | const file_offset = header.offset + atom_sym.n_value - header.addr + stub_offset; | |
| 2273 | const bind_offset = lazy_bind.offsets.items[index]; | |
| 2348 | 2274 | |
| 2349 | const file_offset = header.offset + sym.n_value - header.addr + stub_offset; | |
| 2350 | mem.writeIntLittle(u32, &buf, bind_offset.offset); | |
| 2275 | log.debug("writing lazy bind offset 0x{x} in stub helper at 0x{x}", .{ bind_offset, file_offset }); | |
| 2351 | 2276 | |
| 2352 | log.debug("writing lazy bind offset in stub helper of 0x{x} for symbol {s} at offset 0x{x}", .{ | |
| 2353 | bind_offset.offset, | |
| 2354 | self.getSymbolName(atom.getSymbolWithLoc()), | |
| 2355 | file_offset, | |
| 2356 | }); | |
| 2277 | try self.file.pwriteAll(mem.asBytes(&bind_offset), file_offset); | |
| 2357 | 2278 | |
| 2358 | try self.file.pwriteAll(&buf, file_offset); | |
| 2279 | if (atom.next_index) |next_index| { | |
| 2280 | atom_index = next_index; | |
| 2281 | index += 1; | |
| 2282 | } else break; | |
| 2359 | 2283 | } |
| 2360 | 2284 | } |
| 2361 | 2285 | |
| ... | ... | @@ -3018,12 +2942,17 @@ pub const Zld = struct { |
| 3018 | 2942 | } |
| 3019 | 2943 | |
| 3020 | 2944 | /// Returns symbol described by `sym_with_loc` descriptor. |
| 3021 | pub fn getSymbol(self: *Zld, sym_with_loc: SymbolWithLoc) macho.nlist_64 { | |
| 3022 | return self.getSymbolPtr(sym_with_loc).*; | |
| 2945 | pub fn getSymbol(self: *const Zld, sym_with_loc: SymbolWithLoc) macho.nlist_64 { | |
| 2946 | if (sym_with_loc.getFile()) |file| { | |
| 2947 | const object = &self.objects.items[file]; | |
| 2948 | return object.symtab[sym_with_loc.sym_index]; | |
| 2949 | } else { | |
| 2950 | return self.locals.items[sym_with_loc.sym_index]; | |
| 2951 | } | |
| 3023 | 2952 | } |
| 3024 | 2953 | |
| 3025 | 2954 | /// Returns name of the symbol described by `sym_with_loc` descriptor. |
| 3026 | pub fn getSymbolName(self: *Zld, sym_with_loc: SymbolWithLoc) []const u8 { | |
| 2955 | pub fn getSymbolName(self: *const Zld, sym_with_loc: SymbolWithLoc) []const u8 { | |
| 3027 | 2956 | if (sym_with_loc.getFile()) |file| { |
| 3028 | 2957 | const object = self.objects.items[file]; |
| 3029 | 2958 | return object.getSymbolName(sym_with_loc.sym_index); |
test/link/macho/uuid/build.zig+6-6| ... | ... | @@ -12,18 +12,18 @@ pub fn build(b: *Builder) void { |
| 12 | 12 | .os_tag = .macos, |
| 13 | 13 | }; |
| 14 | 14 | |
| 15 | testUuid(b, test_step, .ReleaseSafe, aarch64_macos, "46b333df88f5314686fc0cba3b939ca8"); | |
| 16 | testUuid(b, test_step, .ReleaseFast, aarch64_macos, "46b333df88f5314686fc0cba3b939ca8"); | |
| 17 | testUuid(b, test_step, .ReleaseSmall, aarch64_macos, "46b333df88f5314686fc0cba3b939ca8"); | |
| 15 | testUuid(b, test_step, .ReleaseSafe, aarch64_macos, "af0f4c21a07c30daba59213d80262e45"); | |
| 16 | testUuid(b, test_step, .ReleaseFast, aarch64_macos, "af0f4c21a07c30daba59213d80262e45"); | |
| 17 | testUuid(b, test_step, .ReleaseSmall, aarch64_macos, "af0f4c21a07c30daba59213d80262e45"); | |
| 18 | 18 | |
| 19 | 19 | const x86_64_macos = std.zig.CrossTarget{ |
| 20 | 20 | .cpu_arch = .x86_64, |
| 21 | 21 | .os_tag = .macos, |
| 22 | 22 | }; |
| 23 | 23 | |
| 24 | testUuid(b, test_step, .ReleaseSafe, x86_64_macos, "342ac765194131e1bad5692b9e0e54a4"); | |
| 25 | testUuid(b, test_step, .ReleaseFast, x86_64_macos, "342ac765194131e1bad5692b9e0e54a4"); | |
| 26 | testUuid(b, test_step, .ReleaseSmall, x86_64_macos, "f119310e24773ecf8ec42e09d0379dad"); | |
| 24 | testUuid(b, test_step, .ReleaseSafe, x86_64_macos, "63f47191c7153f5fba48bd63cb2f5f57"); | |
| 25 | testUuid(b, test_step, .ReleaseFast, x86_64_macos, "63f47191c7153f5fba48bd63cb2f5f57"); | |
| 26 | testUuid(b, test_step, .ReleaseSmall, x86_64_macos, "e7bba66220e33eda9e73ab293ccf93d2"); | |
| 27 | 27 | } |
| 28 | 28 | |
| 29 | 29 | fn testUuid( |