| author | |
| committer | |
| log | 174de37cefde3b22b02fee5982e7bacead4d200f |
| tree | de8e99729e11de1b9116ebd3da5077ad722d9993 |
| parent | 5b4c0cc1f9c4cb047064cffb70bc649b83681814 |
17 files changed, 352 insertions(+), 281 deletions(-)
src/arch/x86_64/CodeGen.zig+6-4| ... | ... | @@ -12362,8 +12362,9 @@ fn genCall(self: *Self, info: union(enum) { |
| 12362 | 12362 | try self.genSetReg(.rax, Type.usize, .{ .lea_got = sym_index }, .{}); |
| 12363 | 12363 | try self.asmRegister(.{ ._, .call }, .rax); |
| 12364 | 12364 | } else if (self.bin_file.cast(link.File.MachO)) |macho_file| { |
| 12365 | const sym_index = try macho_file.getZigObject().?.getOrCreateMetadataForDecl(macho_file, func.owner_decl); | |
| 12366 | const sym = macho_file.getSymbol(sym_index); | |
| 12365 | const zo = macho_file.getZigObject().?; | |
| 12366 | const sym_index = try zo.getOrCreateMetadataForDecl(macho_file, func.owner_decl); | |
| 12367 | const sym = zo.symbols.items[sym_index]; | |
| 12367 | 12368 | try self.genSetReg( |
| 12368 | 12369 | .rax, |
| 12369 | 12370 | Type.usize, |
| ... | ... | @@ -15396,9 +15397,10 @@ fn genLazySymbolRef( |
| 15396 | 15397 | else => unreachable, |
| 15397 | 15398 | } |
| 15398 | 15399 | } else if (self.bin_file.cast(link.File.MachO)) |macho_file| { |
| 15399 | const sym_index = macho_file.getZigObject().?.getOrCreateMetadataForLazySymbol(macho_file, pt, lazy_sym) catch |err| | |
| 15400 | const zo = macho_file.getZigObject().?; | |
| 15401 | const sym_index = zo.getOrCreateMetadataForLazySymbol(macho_file, pt, lazy_sym) catch |err| | |
| 15400 | 15402 | return self.fail("{s} creating lazy symbol", .{@errorName(err)}); |
| 15401 | const sym = macho_file.getSymbol(sym_index); | |
| 15403 | const sym = zo.symbols.items[sym_index]; | |
| 15402 | 15404 | switch (tag) { |
| 15403 | 15405 | .lea, .call => try self.genSetReg( |
| 15404 | 15406 | reg, |
src/arch/x86_64/Emit.zig+1-1| ... | ... | @@ -162,7 +162,7 @@ pub fn emitMir(emit: *Emit) Error!void { |
| 162 | 162 | }; |
| 163 | 163 | const zo = macho_file.getZigObject().?; |
| 164 | 164 | const atom = zo.symbols.items[data.atom_index].getAtom(macho_file).?; |
| 165 | const sym = zo.symbols.items[data.sym_index]; | |
| 165 | const sym = &zo.symbols.items[data.sym_index]; | |
| 166 | 166 | if (sym.flags.needs_zig_got and !is_obj_or_static_lib) { |
| 167 | 167 | _ = try sym.getOrCreateZigGotEntry(data.sym_index, macho_file); |
| 168 | 168 | } |
src/link/MachO.zig+36-29| ... | ... | @@ -529,7 +529,7 @@ pub fn flushModule(self: *MachO, arena: Allocator, tid: Zcu.PerThread.Id, prog_n |
| 529 | 529 | dylib.ordinal = @intCast(ord); |
| 530 | 530 | } |
| 531 | 531 | |
| 532 | try self.claimUnresolved(); | |
| 532 | self.claimUnresolved(); | |
| 533 | 533 | |
| 534 | 534 | self.scanRelocs() catch |err| switch (err) { |
| 535 | 535 | error.HasUndefinedSymbols => return error.FlushFailure, |
| ... | ... | @@ -603,7 +603,12 @@ pub fn flushModule(self: *MachO, arena: Allocator, tid: Zcu.PerThread.Id, prog_n |
| 603 | 603 | |
| 604 | 604 | if (has_resolve_error) return error.FlushFailure; |
| 605 | 605 | } |
| 606 | try self.writeSectionsAndUpdateLinkeditSizes(); | |
| 606 | self.writeSectionsAndUpdateLinkeditSizes() catch |err| { | |
| 607 | switch (err) { | |
| 608 | error.ResolveFailed => return error.FlushFailure, | |
| 609 | else => |e| return e, | |
| 610 | } | |
| 611 | }; | |
| 607 | 612 | |
| 608 | 613 | try self.writeSectionsToFile(); |
| 609 | 614 | try self.allocateLinkeditSegment(); |
| ... | ... | @@ -1450,12 +1455,12 @@ pub fn dedupLiterals(self: *MachO) !void { |
| 1450 | 1455 | } |
| 1451 | 1456 | } |
| 1452 | 1457 | |
| 1453 | fn claimUnresolved(self: *MachO) error{OutOfMemory}!void { | |
| 1458 | fn claimUnresolved(self: *MachO) void { | |
| 1454 | 1459 | if (self.getZigObject()) |zo| { |
| 1455 | try zo.asFile().claimUnresolved(self); | |
| 1460 | zo.claimUnresolved(self); | |
| 1456 | 1461 | } |
| 1457 | 1462 | for (self.objects.items) |index| { |
| 1458 | try self.getFile(index).?.claimUnresolved(self); | |
| 1463 | self.getFile(index).?.object.claimUnresolved(self); | |
| 1459 | 1464 | } |
| 1460 | 1465 | } |
| 1461 | 1466 | |
| ... | ... | @@ -2402,7 +2407,7 @@ fn writeSectionsAndUpdateLinkeditSizes(self: *MachO) !void { |
| 2402 | 2407 | } |
| 2403 | 2408 | |
| 2404 | 2409 | if (self.la_symbol_ptr_sect_index) |_| { |
| 2405 | try self.updatelazyBindSize(); | |
| 2410 | try self.updateLazyBindSize(); | |
| 2406 | 2411 | } |
| 2407 | 2412 | |
| 2408 | 2413 | try self.rebase.updateSize(self); |
| ... | ... | @@ -2412,16 +2417,16 @@ fn writeSectionsAndUpdateLinkeditSizes(self: *MachO) !void { |
| 2412 | 2417 | try self.data_in_code.updateSize(self); |
| 2413 | 2418 | |
| 2414 | 2419 | if (self.getZigObject()) |zo| { |
| 2415 | zo.asFile().writeSymtab(self); | |
| 2420 | zo.asFile().writeSymtab(self, self); | |
| 2416 | 2421 | } |
| 2417 | 2422 | for (self.objects.items) |index| { |
| 2418 | self.getFile(index).?.writeSymtab(self); | |
| 2423 | self.getFile(index).?.writeSymtab(self, self); | |
| 2419 | 2424 | } |
| 2420 | 2425 | for (self.dylibs.items) |index| { |
| 2421 | self.getFile(index).?.writeSymtab(self); | |
| 2426 | self.getFile(index).?.writeSymtab(self, self); | |
| 2422 | 2427 | } |
| 2423 | 2428 | if (self.getInternalObject()) |obj| { |
| 2424 | obj.asFile().writeSymtab(self); | |
| 2429 | obj.asFile().writeSymtab(self, self); | |
| 2425 | 2430 | } |
| 2426 | 2431 | } |
| 2427 | 2432 | |
| ... | ... | @@ -2484,7 +2489,7 @@ fn writeSectionsToFile(self: *MachO) !void { |
| 2484 | 2489 | |
| 2485 | 2490 | const slice = self.sections.slice(); |
| 2486 | 2491 | for (slice.items(.header), slice.items(.out)) |header, out| { |
| 2487 | try self.base.file.pwriteAll(out.items, header.offset); | |
| 2492 | try self.base.file.?.pwriteAll(out.items, header.offset); | |
| 2488 | 2493 | } |
| 2489 | 2494 | } |
| 2490 | 2495 | |
| ... | ... | @@ -2501,7 +2506,7 @@ fn writeDyldInfo(self: *MachO) !void { |
| 2501 | 2506 | const tracy = trace(@src()); |
| 2502 | 2507 | defer tracy.end(); |
| 2503 | 2508 | |
| 2504 | const gpa = self.base.allocator; | |
| 2509 | const gpa = self.base.comp.gpa; | |
| 2505 | 2510 | const base_off = self.getLinkeditSegment().fileoff; |
| 2506 | 2511 | const cmd = self.dyld_info_cmd; |
| 2507 | 2512 | var needed_size: u32 = 0; |
| ... | ... | @@ -2527,14 +2532,14 @@ fn writeDyldInfo(self: *MachO) !void { |
| 2527 | 2532 | try self.lazy_bind.write(writer); |
| 2528 | 2533 | try stream.seekTo(cmd.export_off - base_off); |
| 2529 | 2534 | try self.export_trie.write(writer); |
| 2530 | try self.base.file.pwriteAll(buffer, cmd.rebase_off); | |
| 2535 | try self.base.file.?.pwriteAll(buffer, cmd.rebase_off); | |
| 2531 | 2536 | } |
| 2532 | 2537 | |
| 2533 | 2538 | pub fn writeDataInCode(self: *MachO) !void { |
| 2534 | 2539 | const tracy = trace(@src()); |
| 2535 | 2540 | defer tracy.end(); |
| 2536 | 2541 | const cmd = self.data_in_code_cmd; |
| 2537 | try self.base.file.pwriteAll(mem.sliceAsBytes(self.data_in_code.entries.items), cmd.dataoff); | |
| 2542 | try self.base.file.?.pwriteAll(mem.sliceAsBytes(self.data_in_code.entries.items), cmd.dataoff); | |
| 2538 | 2543 | } |
| 2539 | 2544 | |
| 2540 | 2545 | fn writeIndsymtab(self: *MachO) !void { |
| ... | ... | @@ -2546,15 +2551,15 @@ fn writeIndsymtab(self: *MachO) !void { |
| 2546 | 2551 | var buffer = try std.ArrayList(u8).initCapacity(gpa, needed_size); |
| 2547 | 2552 | defer buffer.deinit(); |
| 2548 | 2553 | try self.indsymtab.write(self, buffer.writer()); |
| 2549 | try self.base.file.pwriteAll(buffer.items, cmd.indirectsymoff); | |
| 2554 | try self.base.file.?.pwriteAll(buffer.items, cmd.indirectsymoff); | |
| 2550 | 2555 | } |
| 2551 | 2556 | |
| 2552 | 2557 | pub fn writeSymtabToFile(self: *MachO) !void { |
| 2553 | 2558 | const tracy = trace(@src()); |
| 2554 | 2559 | defer tracy.end(); |
| 2555 | 2560 | const cmd = self.symtab_cmd; |
| 2556 | try self.base.file.pwriteAll(mem.sliceAsBytes(self.symtab.items), cmd.symoff); | |
| 2557 | try self.base.file.pwriteAll(self.strtab.items, cmd.stroff); | |
| 2561 | try self.base.file.?.pwriteAll(mem.sliceAsBytes(self.symtab.items), cmd.symoff); | |
| 2562 | try self.base.file.?.pwriteAll(self.strtab.items, cmd.stroff); | |
| 2558 | 2563 | } |
| 2559 | 2564 | |
| 2560 | 2565 | fn writeUnwindInfo(self: *MachO) !void { |
| ... | ... | @@ -2687,18 +2692,20 @@ fn writeLoadCommands(self: *MachO) !struct { usize, usize, u64 } { |
| 2687 | 2692 | try load_commands.writeDylinkerLC(writer); |
| 2688 | 2693 | ncmds += 1; |
| 2689 | 2694 | |
| 2690 | if (self.entry_index) |global_index| { | |
| 2691 | const sym = self.getSymbol(global_index); | |
| 2692 | const seg = self.getTextSegment(); | |
| 2693 | const entryoff: u32 = if (sym.getFile(self) == null) | |
| 2694 | 0 | |
| 2695 | else | |
| 2696 | @as(u32, @intCast(sym.getAddress(.{ .stubs = true }, self) - seg.vmaddr)); | |
| 2697 | try writer.writeStruct(macho.entry_point_command{ | |
| 2698 | .entryoff = entryoff, | |
| 2699 | .stacksize = self.base.stack_size, | |
| 2700 | }); | |
| 2701 | ncmds += 1; | |
| 2695 | if (self.getInternalObject()) |obj| { | |
| 2696 | if (obj.getEntryRef(self)) |ref| { | |
| 2697 | const sym = ref.getSymbol(self).?; | |
| 2698 | const seg = self.getTextSegment(); | |
| 2699 | const entryoff: u32 = if (sym.getFile(self) == null) | |
| 2700 | 0 | |
| 2701 | else | |
| 2702 | @as(u32, @intCast(sym.getAddress(.{ .stubs = true }, self) - seg.vmaddr)); | |
| 2703 | try writer.writeStruct(macho.entry_point_command{ | |
| 2704 | .entryoff = entryoff, | |
| 2705 | .stacksize = self.base.stack_size, | |
| 2706 | }); | |
| 2707 | ncmds += 1; | |
| 2708 | } | |
| 2702 | 2709 | } |
| 2703 | 2710 | |
| 2704 | 2711 | if (self.base.isDynLib()) { |
src/link/MachO/Atom.zig+19-15| ... | ... | @@ -213,7 +213,8 @@ pub fn initOutputSection(sect: macho.section_64, macho_file: *MachO) !u8 { |
| 213 | 213 | /// File offset relocation happens transparently, so it is not included in |
| 214 | 214 | /// this calculation. |
| 215 | 215 | pub fn capacity(self: Atom, macho_file: *MachO) u64 { |
| 216 | const next_addr = if (macho_file.getAtom(self.next_index)) |next| | |
| 216 | const zo = macho_file.getZigObject().?; | |
| 217 | const next_addr = if (zo.getAtom(self.next_index)) |next| | |
| 217 | 218 | next.getAddress(macho_file) |
| 218 | 219 | else |
| 219 | 220 | std.math.maxInt(u32); |
| ... | ... | @@ -222,7 +223,8 @@ pub fn capacity(self: Atom, macho_file: *MachO) u64 { |
| 222 | 223 | |
| 223 | 224 | pub fn freeListEligible(self: Atom, macho_file: *MachO) bool { |
| 224 | 225 | // No need to keep a free list node for the last block. |
| 225 | const next = macho_file.getAtom(self.next_index) orelse return false; | |
| 226 | const zo = macho_file.getZigObject().?; | |
| 227 | const next = zo.getAtom(self.next_index) orelse return false; | |
| 226 | 228 | const cap = next.getAddress(macho_file) - self.getAddress(macho_file); |
| 227 | 229 | const ideal_cap = MachO.padToIdeal(self.size); |
| 228 | 230 | if (cap <= ideal_cap) return false; |
| ... | ... | @@ -231,6 +233,7 @@ pub fn freeListEligible(self: Atom, macho_file: *MachO) bool { |
| 231 | 233 | } |
| 232 | 234 | |
| 233 | 235 | pub fn allocate(self: *Atom, macho_file: *MachO) !void { |
| 236 | const zo = macho_file.getZigObject().?; | |
| 234 | 237 | const sect = &macho_file.sections.items(.header)[self.out_n_sect]; |
| 235 | 238 | const free_list = &macho_file.sections.items(.free_list)[self.out_n_sect]; |
| 236 | 239 | const last_atom_index = &macho_file.sections.items(.last_atom_index)[self.out_n_sect]; |
| ... | ... | @@ -250,7 +253,7 @@ pub fn allocate(self: *Atom, macho_file: *MachO) !void { |
| 250 | 253 | var i: usize = free_list.items.len; |
| 251 | 254 | while (i < free_list.items.len) { |
| 252 | 255 | const big_atom_index = free_list.items[i]; |
| 253 | const big_atom = macho_file.getAtom(big_atom_index).?; | |
| 256 | const big_atom = zo.getAtom(big_atom_index).?; | |
| 254 | 257 | // We now have a pointer to a live atom that has too much capacity. |
| 255 | 258 | // Is it enough that we could fit this new atom? |
| 256 | 259 | const cap = big_atom.capacity(macho_file); |
| ... | ... | @@ -282,7 +285,7 @@ pub fn allocate(self: *Atom, macho_file: *MachO) !void { |
| 282 | 285 | free_list_removal = i; |
| 283 | 286 | } |
| 284 | 287 | break :blk new_start_vaddr; |
| 285 | } else if (macho_file.getAtom(last_atom_index.*)) |last| { | |
| 288 | } else if (zo.getAtom(last_atom_index.*)) |last| { | |
| 286 | 289 | const ideal_capacity = MachO.padToIdeal(last.size); |
| 287 | 290 | const ideal_capacity_end_vaddr = last.value + ideal_capacity; |
| 288 | 291 | const new_start_vaddr = self.alignment.forward(ideal_capacity_end_vaddr); |
| ... | ... | @@ -302,7 +305,7 @@ pub fn allocate(self: *Atom, macho_file: *MachO) !void { |
| 302 | 305 | }); |
| 303 | 306 | |
| 304 | 307 | const expand_section = if (atom_placement) |placement_index| |
| 305 | macho_file.getAtom(placement_index).?.next_index == 0 | |
| 308 | zo.getAtom(placement_index).?.next_index == 0 | |
| 306 | 309 | else |
| 307 | 310 | true; |
| 308 | 311 | if (expand_section) { |
| ... | ... | @@ -327,15 +330,15 @@ pub fn allocate(self: *Atom, macho_file: *MachO) !void { |
| 327 | 330 | // This function can also reallocate an atom. |
| 328 | 331 | // In this case we need to "unplug" it from its previous location before |
| 329 | 332 | // plugging it in to its new location. |
| 330 | if (macho_file.getAtom(self.prev_index)) |prev| { | |
| 333 | if (zo.getAtom(self.prev_index)) |prev| { | |
| 331 | 334 | prev.next_index = self.next_index; |
| 332 | 335 | } |
| 333 | if (macho_file.getAtom(self.next_index)) |next| { | |
| 336 | if (zo.getAtom(self.next_index)) |next| { | |
| 334 | 337 | next.prev_index = self.prev_index; |
| 335 | 338 | } |
| 336 | 339 | |
| 337 | 340 | if (atom_placement) |big_atom_index| { |
| 338 | const big_atom = macho_file.getAtom(big_atom_index).?; | |
| 341 | const big_atom = zo.getAtom(big_atom_index).?; | |
| 339 | 342 | self.prev_index = big_atom_index; |
| 340 | 343 | self.next_index = big_atom.next_index; |
| 341 | 344 | big_atom.next_index = self.atom_index; |
| ... | ... | @@ -365,6 +368,7 @@ pub fn free(self: *Atom, macho_file: *MachO) void { |
| 365 | 368 | |
| 366 | 369 | const comp = macho_file.base.comp; |
| 367 | 370 | const gpa = comp.gpa; |
| 371 | const zo = macho_file.getZigObject().?; | |
| 368 | 372 | const free_list = &macho_file.sections.items(.free_list)[self.out_n_sect]; |
| 369 | 373 | const last_atom_index = &macho_file.sections.items(.last_atom_index)[self.out_n_sect]; |
| 370 | 374 | var already_have_free_list_node = false; |
| ... | ... | @@ -383,9 +387,9 @@ pub fn free(self: *Atom, macho_file: *MachO) void { |
| 383 | 387 | } |
| 384 | 388 | } |
| 385 | 389 | |
| 386 | if (macho_file.getAtom(last_atom_index.*)) |last_atom| { | |
| 390 | if (zo.getAtom(last_atom_index.*)) |last_atom| { | |
| 387 | 391 | if (last_atom.atom_index == self.atom_index) { |
| 388 | if (macho_file.getAtom(self.prev_index)) |_| { | |
| 392 | if (zo.getAtom(self.prev_index)) |_| { | |
| 389 | 393 | // TODO shrink the section size here |
| 390 | 394 | last_atom_index.* = self.prev_index; |
| 391 | 395 | } else { |
| ... | ... | @@ -394,7 +398,7 @@ pub fn free(self: *Atom, macho_file: *MachO) void { |
| 394 | 398 | } |
| 395 | 399 | } |
| 396 | 400 | |
| 397 | if (macho_file.getAtom(self.prev_index)) |prev| { | |
| 401 | if (zo.getAtom(self.prev_index)) |prev| { | |
| 398 | 402 | prev.next_index = self.next_index; |
| 399 | 403 | if (!already_have_free_list_node and prev.*.freeListEligible(macho_file)) { |
| 400 | 404 | // The free list is heuristics, it doesn't have to be perfect, so we can |
| ... | ... | @@ -405,7 +409,7 @@ pub fn free(self: *Atom, macho_file: *MachO) void { |
| 405 | 409 | self.prev_index = 0; |
| 406 | 410 | } |
| 407 | 411 | |
| 408 | if (macho_file.getAtom(self.next_index)) |next| { | |
| 412 | if (zo.getAtom(self.next_index)) |next| { | |
| 409 | 413 | next.prev_index = self.prev_index; |
| 410 | 414 | } else { |
| 411 | 415 | self.next_index = 0; |
| ... | ... | @@ -423,7 +427,7 @@ pub fn addReloc(self: *Atom, macho_file: *MachO, reloc: Relocation) !void { |
| 423 | 427 | const gpa = macho_file.base.comp.gpa; |
| 424 | 428 | const file = self.getFile(macho_file); |
| 425 | 429 | assert(file == .zig_object); |
| 426 | var extra = self.getExtra(macho_file).?; | |
| 430 | var extra = self.getExtra(macho_file); | |
| 427 | 431 | const rels = &file.zig_object.relocs.items[extra.rel_index]; |
| 428 | 432 | try rels.append(gpa, reloc); |
| 429 | 433 | extra.rel_count += 1; |
| ... | ... | @@ -432,7 +436,7 @@ pub fn addReloc(self: *Atom, macho_file: *MachO, reloc: Relocation) !void { |
| 432 | 436 | |
| 433 | 437 | pub fn freeRelocs(self: *Atom, macho_file: *MachO) void { |
| 434 | 438 | self.getFile(macho_file).zig_object.freeAtomRelocs(self.*, macho_file); |
| 435 | var extra = self.getExtra(macho_file).?; | |
| 439 | var extra = self.getExtra(macho_file); | |
| 436 | 440 | extra.rel_count = 0; |
| 437 | 441 | self.setExtra(extra, macho_file); |
| 438 | 442 | } |
| ... | ... | @@ -630,7 +634,7 @@ fn resolveRelocInner( |
| 630 | 634 | const TLS = @as(i64, @intCast(macho_file.getTlsAddress())); |
| 631 | 635 | const SUB = if (subtractor) |sub| @as(i64, @intCast(sub.getTargetAddress(self, macho_file))) else 0; |
| 632 | 636 | // Address of the __got_zig table entry if any. |
| 633 | const ZIG_GOT = @as(i64, @intCast(rel.getZigGotTargetAddress(self, macho_file))); | |
| 637 | const ZIG_GOT = @as(i64, @intCast(rel.getZigGotTargetAddress(macho_file))); | |
| 634 | 638 | |
| 635 | 639 | const divExact = struct { |
| 636 | 640 | fn divExact(atom: Atom, r: Relocation, num: u12, den: u12, ctx: *MachO) !u12 { |
src/link/MachO/DebugSymbols.zig+6-6| ... | ... | @@ -175,8 +175,9 @@ fn findFreeSpace(self: *DebugSymbols, object_size: u64, min_alignment: u64) u64 |
| 175 | 175 | } |
| 176 | 176 | |
| 177 | 177 | pub fn flushModule(self: *DebugSymbols, macho_file: *MachO) !void { |
| 178 | const zo = macho_file.getZigObject().?; | |
| 178 | 179 | for (self.relocs.items) |*reloc| { |
| 179 | const sym = macho_file.getSymbol(reloc.target); | |
| 180 | const sym = zo.symbols.items[reloc.target]; | |
| 180 | 181 | const sym_name = sym.getName(macho_file); |
| 181 | 182 | const addr = switch (reloc.type) { |
| 182 | 183 | .direct_load => sym.getAddress(.{}, macho_file), |
| ... | ... | @@ -382,23 +383,22 @@ pub fn writeSymtab(self: *DebugSymbols, off: u32, macho_file: *MachO) !u32 { |
| 382 | 383 | cmd.symoff = off; |
| 383 | 384 | |
| 384 | 385 | try self.symtab.resize(gpa, cmd.nsyms); |
| 385 | try self.strtab.ensureUnusedCapacity(gpa, cmd.strsize - 1); | |
| 386 | try self.strtab.resize(gpa, cmd.strsize); | |
| 387 | self.strtab.items[0] = 0; | |
| 386 | 388 | |
| 387 | 389 | if (macho_file.getZigObject()) |zo| { |
| 388 | 390 | zo.writeSymtab(macho_file, self); |
| 389 | 391 | } |
| 390 | 392 | for (macho_file.objects.items) |index| { |
| 391 | try macho_file.getFile(index).?.writeSymtab(macho_file, self); | |
| 393 | macho_file.getFile(index).?.writeSymtab(macho_file, self); | |
| 392 | 394 | } |
| 393 | 395 | for (macho_file.dylibs.items) |index| { |
| 394 | try macho_file.getFile(index).?.writeSymtab(macho_file, self); | |
| 396 | macho_file.getFile(index).?.writeSymtab(macho_file, self); | |
| 395 | 397 | } |
| 396 | 398 | if (macho_file.getInternalObject()) |internal| { |
| 397 | 399 | internal.writeSymtab(macho_file, self); |
| 398 | 400 | } |
| 399 | 401 | |
| 400 | assert(self.strtab.items.len == cmd.strsize); | |
| 401 | ||
| 402 | 402 | try self.file.pwriteAll(mem.sliceAsBytes(self.symtab.items), cmd.symoff); |
| 403 | 403 | |
| 404 | 404 | return off + cmd.nsyms * @sizeOf(macho.nlist_64); |
src/link/MachO/Dylib.zig+15-9| ... | ... | @@ -6,7 +6,7 @@ strtab: std.ArrayListUnmanaged(u8) = .{}, |
| 6 | 6 | id: ?Id = null, |
| 7 | 7 | ordinal: u16 = 0, |
| 8 | 8 | |
| 9 | symbols: std.ArrayListUnmanaged(Symbol.Index) = .{}, | |
| 9 | symbols: std.ArrayListUnmanaged(Symbol) = .{}, | |
| 10 | 10 | symbols_extra: std.ArrayListUnmanaged(u32) = .{}, |
| 11 | 11 | globals: std.ArrayListUnmanaged(MachO.SymbolResolver.Index) = .{}, |
| 12 | 12 | dependents: std.ArrayListUnmanaged(Id) = .{}, |
| ... | ... | @@ -516,7 +516,7 @@ pub fn initSymbols(self: *Dylib, macho_file: *MachO) !void { |
| 516 | 516 | } |
| 517 | 517 | } |
| 518 | 518 | |
| 519 | pub fn resolveSymbols(self: *Dylib, macho_file: *MachO) void { | |
| 519 | pub fn resolveSymbols(self: *Dylib, macho_file: *MachO) !void { | |
| 520 | 520 | const tracy = trace(@src()); |
| 521 | 521 | defer tracy.end(); |
| 522 | 522 | |
| ... | ... | @@ -584,7 +584,7 @@ pub fn calcSymtabSize(self: *Dylib, macho_file: *MachO) void { |
| 584 | 584 | } |
| 585 | 585 | } |
| 586 | 586 | |
| 587 | pub fn writeSymtab(self: Dylib, macho_file: *MachO) void { | |
| 587 | pub fn writeSymtab(self: Dylib, macho_file: *MachO, ctx: anytype) void { | |
| 588 | 588 | const tracy = trace(@src()); |
| 589 | 589 | defer tracy.end(); |
| 590 | 590 | |
| ... | ... | @@ -594,13 +594,13 @@ pub fn writeSymtab(self: Dylib, macho_file: *MachO) void { |
| 594 | 594 | const file = ref.getFile(macho_file) orelse continue; |
| 595 | 595 | if (file.getIndex() != self.index) continue; |
| 596 | 596 | const idx = sym.getOutputSymtabIndex(macho_file) orelse continue; |
| 597 | const out_sym = &macho_file.symtab.items[idx]; | |
| 597 | const out_sym = &ctx.symtab.items[idx]; | |
| 598 | 598 | out_sym.n_strx = n_strx; |
| 599 | 599 | sym.setOutputSym(macho_file, out_sym); |
| 600 | 600 | const name = sym.getName(macho_file); |
| 601 | @memcpy(macho_file.strtab.items[n_strx..][0..name.len], name); | |
| 601 | @memcpy(ctx.strtab.items[n_strx..][0..name.len], name); | |
| 602 | 602 | n_strx += @intCast(name.len); |
| 603 | macho_file.strtab.items[n_strx] = 0; | |
| 603 | ctx.strtab.items[n_strx] = 0; | |
| 604 | 604 | n_strx += 1; |
| 605 | 605 | } |
| 606 | 606 | } |
| ... | ... | @@ -718,10 +718,16 @@ fn formatSymtab( |
| 718 | 718 | _ = unused_fmt_string; |
| 719 | 719 | _ = options; |
| 720 | 720 | const dylib = ctx.dylib; |
| 721 | const macho_file = ctx.macho_file; | |
| 721 | 722 | try writer.writeAll(" globals\n"); |
| 722 | for (dylib.symbols.items) |index| { | |
| 723 | const global = ctx.macho_file.getSymbol(index); | |
| 724 | try writer.print(" {}\n", .{global.fmt(ctx.macho_file)}); | |
| 723 | for (dylib.symbols.items, 0..) |sym, i| { | |
| 724 | const ref = dylib.getSymbolRef(@intCast(i), macho_file); | |
| 725 | if (ref.getFile(macho_file) == null) { | |
| 726 | // TODO any better way of handling this? | |
| 727 | try writer.print(" {s} : unclaimed\n", .{sym.getName(macho_file)}); | |
| 728 | } else { | |
| 729 | try writer.print(" {}\n", .{ref.getSymbol(macho_file).?.fmt(macho_file)}); | |
| 730 | } | |
| 725 | 731 | } |
| 726 | 732 | } |
| 727 | 733 |
src/link/MachO/InternalObject.zig+19-19| ... | ... | @@ -16,7 +16,7 @@ objc_selrefs: [@sizeOf(u64)]u8 = [_]u8{0} ** @sizeOf(u64), |
| 16 | 16 | force_undefined: std.ArrayListUnmanaged(Symbol.Index) = .{}, |
| 17 | 17 | entry_index: ?Symbol.Index = null, |
| 18 | 18 | dyld_stub_binder_index: ?Symbol.Index = null, |
| 19 | dyld_private: ?Symbol.Index = null, | |
| 19 | dyld_private_index: ?Symbol.Index = null, | |
| 20 | 20 | objc_msg_send_index: ?Symbol.Index = null, |
| 21 | 21 | mh_execute_header_index: ?Symbol.Index = null, |
| 22 | 22 | mh_dylib_header_index: ?Symbol.Index = null, |
| ... | ... | @@ -206,7 +206,7 @@ pub fn resolveBoundarySymbols(self: *InternalObject, macho_file: *MachO) !void { |
| 206 | 206 | const nlist_idx: u32 = @intCast(self.symtab.items.len); |
| 207 | 207 | const nlist = self.symtab.addOneAssumeCapacity(); |
| 208 | 208 | nlist.* = .{ |
| 209 | .n_strx = name_off.pos, | |
| 209 | .n_strx = name_off, | |
| 210 | 210 | .n_type = macho.N_SECT, |
| 211 | 211 | .n_sect = 0, |
| 212 | 212 | .n_desc = 0, |
| ... | ... | @@ -273,7 +273,7 @@ fn addObjcMethnameSection(self: *InternalObject, methname: []const u8, macho_fil |
| 273 | 273 | const nlist_idx: u32 = @intCast(self.symtab.items.len); |
| 274 | 274 | const nlist = try self.symtab.addOne(gpa); |
| 275 | 275 | nlist.* = .{ |
| 276 | .n_strx = name_str.pos, | |
| 276 | .n_strx = name_str, | |
| 277 | 277 | .n_type = macho.N_SECT, |
| 278 | 278 | .n_sect = @intCast(n_sect + 1), |
| 279 | 279 | .n_desc = 0, |
| ... | ... | @@ -286,12 +286,12 @@ fn addObjcMethnameSection(self: *InternalObject, methname: []const u8, macho_fil |
| 286 | 286 | } |
| 287 | 287 | |
| 288 | 288 | fn addObjcSelrefsSection(self: *InternalObject, methname_sym_index: Symbol.Index, macho_file: *MachO) !Symbol.Index { |
| 289 | const gpa = macho_file.base.allocator; | |
| 289 | const gpa = macho_file.base.comp.gpa; | |
| 290 | 290 | const atom_index = try self.addAtom(gpa); |
| 291 | 291 | try self.atoms_indexes.append(gpa, atom_index); |
| 292 | 292 | const atom = self.getAtom(atom_index).?; |
| 293 | 293 | atom.size = @sizeOf(u64); |
| 294 | atom.alignment = 3; | |
| 294 | atom.alignment = .@"8"; | |
| 295 | 295 | |
| 296 | 296 | const n_sect = try self.addSection(gpa, "__DATA", "__objc_selrefs"); |
| 297 | 297 | const sect = &self.sections.items(.header)[n_sect]; |
| ... | ... | @@ -389,7 +389,7 @@ pub fn resolveObjcMsgSendSymbols(self: *InternalObject, macho_file: *MachO) !voi |
| 389 | 389 | }; |
| 390 | 390 | sym.nlist_idx = nlist_idx; |
| 391 | 391 | sym.extra = try self.addSymbolExtra(gpa, .{ .objc_selrefs = selrefs_index }); |
| 392 | sym.setSectionFlags(.{ .objc_stubs = true }); | |
| 392 | sym.flags.objc_stubs = true; | |
| 393 | 393 | |
| 394 | 394 | const idx = ref.getFile(macho_file).?.object.globals.items[ref.index]; |
| 395 | 395 | try self.globals.append(gpa, idx); |
| ... | ... | @@ -417,7 +417,7 @@ pub fn resolveLiterals(self: *InternalObject, lp: *MachO.LiteralPool, macho_file |
| 417 | 417 | const target = rel.getTargetSymbol(atom.*, macho_file).getAtom(macho_file).?; |
| 418 | 418 | try buffer.ensureUnusedCapacity(target.size); |
| 419 | 419 | buffer.resize(target.size) catch unreachable; |
| 420 | @memcpy(buffer.items, self.getSectionData(target.n_sect)); | |
| 420 | @memcpy(buffer.items, try self.getSectionData(target.n_sect)); | |
| 421 | 421 | const res = try lp.insert(gpa, header.type(), buffer.items); |
| 422 | 422 | buffer.clearRetainingCapacity(); |
| 423 | 423 | if (!res.found_existing) { |
| ... | ... | @@ -425,7 +425,7 @@ pub fn resolveLiterals(self: *InternalObject, lp: *MachO.LiteralPool, macho_file |
| 425 | 425 | } else { |
| 426 | 426 | const lp_sym = lp.getSymbol(res.index, macho_file); |
| 427 | 427 | const lp_atom = lp_sym.getAtom(macho_file).?; |
| 428 | lp_atom.alignment = @max(lp_atom.alignment, atom.alignment); | |
| 428 | lp_atom.alignment = lp_atom.alignment.max(atom.alignment); | |
| 429 | 429 | atom.flags.alive = false; |
| 430 | 430 | } |
| 431 | 431 | atom.addExtra(.{ .literal_pool_index = res.index }, macho_file); |
| ... | ... | @@ -438,7 +438,7 @@ pub fn dedupLiterals(self: *InternalObject, lp: MachO.LiteralPool, macho_file: * |
| 438 | 438 | |
| 439 | 439 | for (self.getAtoms()) |atom_index| { |
| 440 | 440 | const atom = self.getAtom(atom_index) orelse continue; |
| 441 | if (!atom.alive.load(.seq_cst)) continue; | |
| 441 | if (!atom.flags.alive) continue; | |
| 442 | 442 | |
| 443 | 443 | const relocs = blk: { |
| 444 | 444 | const extra = atom.getExtra(macho_file); |
| ... | ... | @@ -463,7 +463,7 @@ pub fn dedupLiterals(self: *InternalObject, lp: MachO.LiteralPool, macho_file: * |
| 463 | 463 | } |
| 464 | 464 | |
| 465 | 465 | for (self.symbols.items) |*sym| { |
| 466 | if (!sym.getSectionFlags().objc_stubs) continue; | |
| 466 | if (!sym.flags.objc_stubs) continue; | |
| 467 | 467 | const extra = sym.getExtra(macho_file); |
| 468 | 468 | const file = sym.getFile(macho_file).?; |
| 469 | 469 | if (file.getIndex() != self.index) continue; |
| ... | ... | @@ -495,14 +495,14 @@ pub fn scanRelocs(self: *InternalObject, macho_file: *MachO) void { |
| 495 | 495 | if (self.getDyldStubBinderRef(macho_file)) |ref| { |
| 496 | 496 | if (ref.getFile(macho_file) != null) { |
| 497 | 497 | const sym = ref.getSymbol(macho_file).?; |
| 498 | sym.flags.got = true; | |
| 498 | sym.flags.needs_got = true; | |
| 499 | 499 | } |
| 500 | 500 | } |
| 501 | 501 | if (self.getObjcMsgSendRef(macho_file)) |ref| { |
| 502 | 502 | if (ref.getFile(macho_file) != null) { |
| 503 | 503 | const sym = ref.getSymbol(macho_file).?; |
| 504 | 504 | // TODO is it always needed, or only if we are synthesising fast stubs |
| 505 | sym.flags.got = true; | |
| 505 | sym.flags.needs_got = true; | |
| 506 | 506 | } |
| 507 | 507 | } |
| 508 | 508 | } |
| ... | ... | @@ -569,30 +569,30 @@ pub fn writeAtoms(self: *InternalObject, macho_file: *MachO) !void { |
| 569 | 569 | |
| 570 | 570 | for (self.getAtoms()) |atom_index| { |
| 571 | 571 | const atom = self.getAtom(atom_index) orelse continue; |
| 572 | if (!atom.alive.load(.seq_cst)) continue; | |
| 572 | if (!atom.flags.alive) continue; | |
| 573 | 573 | const sect = atom.getInputSection(macho_file); |
| 574 | 574 | if (sect.isZerofill()) continue; |
| 575 | 575 | const off = atom.value; |
| 576 | 576 | const buffer = macho_file.sections.items(.out)[atom.out_n_sect].items[off..][0..atom.size]; |
| 577 | @memcpy(buffer, self.getSectionData(atom.n_sect)); | |
| 577 | @memcpy(buffer, try self.getSectionData(atom.n_sect)); | |
| 578 | 578 | try atom.resolveRelocs(macho_file, buffer); |
| 579 | 579 | } |
| 580 | 580 | } |
| 581 | 581 | |
| 582 | pub fn writeSymtab(self: InternalObject, macho_file: *MachO) void { | |
| 582 | pub fn writeSymtab(self: InternalObject, macho_file: *MachO, ctx: anytype) void { | |
| 583 | 583 | var n_strx = self.output_symtab_ctx.stroff; |
| 584 | 584 | for (self.symbols.items, 0..) |sym, i| { |
| 585 | 585 | const ref = self.getSymbolRef(@intCast(i), macho_file); |
| 586 | 586 | const file = ref.getFile(macho_file) orelse continue; |
| 587 | 587 | if (file.getIndex() != self.index) continue; |
| 588 | 588 | const idx = sym.getOutputSymtabIndex(macho_file) orelse continue; |
| 589 | const out_sym = &macho_file.symtab.items[idx]; | |
| 589 | const out_sym = &ctx.symtab.items[idx]; | |
| 590 | 590 | out_sym.n_strx = n_strx; |
| 591 | 591 | sym.setOutputSym(macho_file, out_sym); |
| 592 | 592 | const name = sym.getName(macho_file); |
| 593 | @memcpy(macho_file.strtab.items[n_strx..][0..name.len], name); | |
| 593 | @memcpy(ctx.strtab.items[n_strx..][0..name.len], name); | |
| 594 | 594 | n_strx += @intCast(name.len); |
| 595 | macho_file.strtab.items[n_strx] = 0; | |
| 595 | ctx.strtab.items[n_strx] = 0; | |
| 596 | 596 | n_strx += 1; |
| 597 | 597 | } |
| 598 | 598 | } |
| ... | ... | @@ -640,7 +640,7 @@ pub fn asFile(self: *InternalObject) File { |
| 640 | 640 | } |
| 641 | 641 | |
| 642 | 642 | pub fn getAtomRelocs(self: *const InternalObject, atom: Atom, macho_file: *MachO) []const Relocation { |
| 643 | const extra = atom.getExtra(macho_file).?; | |
| 643 | const extra = atom.getExtra(macho_file); | |
| 644 | 644 | const relocs = self.sections.items(.relocs)[atom.n_sect]; |
| 645 | 645 | return relocs.items[extra.rel_index..][0..extra.rel_count]; |
| 646 | 646 | } |
src/link/MachO/Object.zig+138-116| ... | ... | @@ -11,10 +11,10 @@ sections: std.MultiArrayList(Section) = .{}, |
| 11 | 11 | symtab: std.MultiArrayList(Nlist) = .{}, |
| 12 | 12 | strtab: std.ArrayListUnmanaged(u8) = .{}, |
| 13 | 13 | |
| 14 | symbols: std.ArrayListUnmanaged(Symbol.Index) = .{}, | |
| 14 | symbols: std.ArrayListUnmanaged(Symbol) = .{}, | |
| 15 | 15 | symbols_extra: std.ArrayListUnmanaged(u32) = .{}, |
| 16 | 16 | globals: std.ArrayListUnmanaged(MachO.SymbolResolver.Index) = .{}, |
| 17 | atoms: std.ArrayListUnmanaged(Atom.Index) = .{}, | |
| 17 | atoms: std.ArrayListUnmanaged(Atom) = .{}, | |
| 18 | 18 | atoms_indexes: std.ArrayListUnmanaged(Atom.Index) = .{}, |
| 19 | 19 | atoms_extra: std.ArrayListUnmanaged(u32) = .{}, |
| 20 | 20 | |
| ... | ... | @@ -228,7 +228,7 @@ pub fn parse(self: *Object, macho_file: *MachO) !void { |
| 228 | 228 | |
| 229 | 229 | // Parse DWARF __TEXT,__eh_frame section |
| 230 | 230 | if (self.eh_frame_sect_index) |index| { |
| 231 | try self.initEhFrameRecords(index, macho_file); | |
| 231 | try self.initEhFrameRecords(gpa, index, handle, macho_file); | |
| 232 | 232 | } |
| 233 | 233 | |
| 234 | 234 | // Parse Apple's __LD,__compact_unwind section |
| ... | ... | @@ -261,7 +261,7 @@ pub fn parse(self: *Object, macho_file: *MachO) !void { |
| 261 | 261 | |
| 262 | 262 | try self.parseDebugInfo(macho_file); |
| 263 | 263 | |
| 264 | for (self.atoms.items) |atom_index| { | |
| 264 | for (self.getAtoms()) |atom_index| { | |
| 265 | 265 | const atom = self.getAtom(atom_index) orelse continue; |
| 266 | 266 | const isec = atom.getInputSection(macho_file); |
| 267 | 267 | if (mem.eql(u8, isec.sectName(), "__eh_frame") or |
| ... | ... | @@ -606,7 +606,7 @@ fn initPointerLiterals(self: *Object, allocator: Allocator, macho_file: *MachO) |
| 606 | 606 | } |
| 607 | 607 | } |
| 608 | 608 | |
| 609 | pub fn resolveLiterals(self: Object, lp: *MachO.LiteralPool, macho_file: *MachO) !void { | |
| 609 | pub fn resolveLiterals(self: *Object, lp: *MachO.LiteralPool, macho_file: *MachO) !void { | |
| 610 | 610 | const tracy = trace(@src()); |
| 611 | 611 | defer tracy.end(); |
| 612 | 612 | |
| ... | ... | @@ -646,7 +646,7 @@ pub fn resolveLiterals(self: Object, lp: *MachO.LiteralPool, macho_file: *MachO) |
| 646 | 646 | } else { |
| 647 | 647 | const lp_sym = lp.getSymbol(res.index, macho_file); |
| 648 | 648 | const lp_atom = lp_sym.getAtom(macho_file).?; |
| 649 | lp_atom.alignment = @max(lp_atom.alignment, atom.alignment); | |
| 649 | lp_atom.alignment = lp_atom.alignment.max(atom.alignment); | |
| 650 | 650 | atom.flags.alive = false; |
| 651 | 651 | } |
| 652 | 652 | atom.addExtra(.{ .literal_pool_index = res.index }, macho_file); |
| ... | ... | @@ -684,7 +684,7 @@ pub fn resolveLiterals(self: Object, lp: *MachO.LiteralPool, macho_file: *MachO) |
| 684 | 684 | } else { |
| 685 | 685 | const lp_sym = lp.getSymbol(res.index, macho_file); |
| 686 | 686 | const lp_atom = lp_sym.getAtom(macho_file).?; |
| 687 | lp_atom.alignment = @max(lp_atom.alignment, atom.alignment); | |
| 687 | lp_atom.alignment = lp_atom.alignment.max(atom.alignment); | |
| 688 | 688 | atom.flags.alive = false; |
| 689 | 689 | } |
| 690 | 690 | atom.addExtra(.{ .literal_pool_index = res.index }, macho_file); |
| ... | ... | @@ -823,7 +823,7 @@ fn initSymbols(self: *Object, allocator: Allocator, macho_file: *MachO) !void { |
| 823 | 823 | const index = self.addSymbolAssumeCapacity(); |
| 824 | 824 | const symbol = &self.symbols.items[index]; |
| 825 | 825 | symbol.value = nlist.n_value; |
| 826 | symbol.name = .{ .pos = nlist.n_strx, .len = @intCast(self.getNStrx(nlist.n_strx).len + 1) }; | |
| 826 | symbol.name = nlist.n_strx; | |
| 827 | 827 | symbol.nlist_idx = @intCast(i); |
| 828 | 828 | symbol.extra = self.addSymbolExtraAssumeCapacity(.{}); |
| 829 | 829 | |
| ... | ... | @@ -838,7 +838,9 @@ fn initSymbols(self: *Object, allocator: Allocator, macho_file: *MachO) !void { |
| 838 | 838 | symbol.flags.tentative = nlist.tentative(); |
| 839 | 839 | symbol.flags.no_dead_strip = symbol.flags.no_dead_strip or nlist.noDeadStrip(); |
| 840 | 840 | symbol.flags.dyn_ref = nlist.n_desc & macho.REFERENCED_DYNAMICALLY != 0; |
| 841 | symbol.flags.interposable = nlist.ext() and (nlist.sect() or nlist.abs()) and macho_file.options.dylib and macho_file.options.namespace == .flat and !nlist.pext(); | |
| 841 | symbol.flags.interposable = nlist.ext() and (nlist.sect() or nlist.abs()) and macho_file.base.isDynLib() and !nlist.pext(); | |
| 842 | // TODO | |
| 843 | // symbol.flags.interposable = nlist.ext() and (nlist.sect() or nlist.abs()) and macho_file.base.isDynLib() and macho_file.options.namespace == .flat and !nlist.pext(); | |
| 842 | 844 | |
| 843 | 845 | if (nlist.sect() and |
| 844 | 846 | self.sections.items(.header)[nlist.n_sect - 1].type() == macho.S_THREAD_LOCAL_VARIABLES) |
| ... | ... | @@ -869,7 +871,7 @@ fn initSymbolStabs(self: *Object, allocator: Allocator, nlists: anytype, macho_f |
| 869 | 871 | fn find(fs: @This(), addr: u64) ?Symbol.Index { |
| 870 | 872 | // TODO binary search since we have the list sorted |
| 871 | 873 | for (fs.entries) |nlist| { |
| 872 | if (nlist.nlist.n_value == addr) return fs.ctx.symbols.items[nlist.idx]; | |
| 874 | if (nlist.nlist.n_value == addr) return @intCast(nlist.idx); | |
| 873 | 875 | } |
| 874 | 876 | return null; |
| 875 | 877 | } |
| ... | ... | @@ -920,17 +922,17 @@ fn initSymbolStabs(self: *Object, allocator: Allocator, nlists: anytype, macho_f |
| 920 | 922 | switch (nlist.n_type) { |
| 921 | 923 | macho.N_BNSYM => { |
| 922 | 924 | stab.is_func = true; |
| 923 | stab.symbol = sym_lookup.find(nlist.n_value); | |
| 925 | stab.index = sym_lookup.find(nlist.n_value); | |
| 924 | 926 | // TODO validate |
| 925 | 927 | i += 3; |
| 926 | 928 | }, |
| 927 | 929 | macho.N_GSYM => { |
| 928 | 930 | stab.is_func = false; |
| 929 | stab.symbol = sym_lookup.find(addr_lookup.get(self.getString(nlist.n_strx)).?); | |
| 931 | stab.index = sym_lookup.find(addr_lookup.get(self.getString(nlist.n_strx)).?); | |
| 930 | 932 | }, |
| 931 | 933 | macho.N_STSYM => { |
| 932 | 934 | stab.is_func = false; |
| 933 | stab.symbol = sym_lookup.find(nlist.n_value); | |
| 935 | stab.index = sym_lookup.find(nlist.n_value); | |
| 934 | 936 | }, |
| 935 | 937 | else => { |
| 936 | 938 | try macho_file.reportParseError2(self.index, "unhandled symbol stab type 0x{x}", .{ |
| ... | ... | @@ -1103,9 +1105,9 @@ fn initUnwindRecords(self: *Object, allocator: Allocator, sect_id: u8, file: Fil |
| 1103 | 1105 | ctx: *const Object, |
| 1104 | 1106 | |
| 1105 | 1107 | fn find(fs: @This(), addr: u64) ?Symbol.Index { |
| 1106 | for (fs.ctx.symbols.items, 0..) |sym_index, i| { | |
| 1108 | for (0..fs.ctx.symbols.items.len) |i| { | |
| 1107 | 1109 | const nlist = fs.ctx.symtab.items(.nlist)[i]; |
| 1108 | if (nlist.ext() and nlist.n_value == addr) return sym_index; | |
| 1110 | if (nlist.ext() and nlist.n_value == addr) return @intCast(i); | |
| 1109 | 1111 | } |
| 1110 | 1112 | return null; |
| 1111 | 1113 | } |
| ... | ... | @@ -1274,7 +1276,7 @@ fn parseUnwindRecords(self: *Object, allocator: Allocator, cpu_arch: std.Target. |
| 1274 | 1276 | // Create a null record |
| 1275 | 1277 | const rec_index = try self.addUnwindRecord(allocator); |
| 1276 | 1278 | const rec = self.getUnwindRecord(rec_index); |
| 1277 | const atom = macho_file.getAtom(meta.atom).?; | |
| 1279 | const atom = self.getAtom(meta.atom).?; | |
| 1278 | 1280 | try self.unwind_records_indexes.append(allocator, rec_index); |
| 1279 | 1281 | rec.length = @intCast(meta.size); |
| 1280 | 1282 | rec.atom = meta.atom; |
| ... | ... | @@ -1468,17 +1470,17 @@ fn findCompileUnit(self: *Object, args: struct { |
| 1468 | 1470 | }; |
| 1469 | 1471 | } |
| 1470 | 1472 | |
| 1471 | pub fn resolveSymbols(self: *Object, macho_file: *MachO) void { | |
| 1473 | pub fn resolveSymbols(self: *Object, macho_file: *MachO) !void { | |
| 1472 | 1474 | const tracy = trace(@src()); |
| 1473 | 1475 | defer tracy.end(); |
| 1474 | 1476 | |
| 1475 | const gpa = macho_file.base.allocator; | |
| 1477 | const gpa = macho_file.base.comp.gpa; | |
| 1476 | 1478 | |
| 1477 | 1479 | for (self.symtab.items(.nlist), self.symtab.items(.atom), self.globals.items, 0..) |nlist, atom_index, *global, i| { |
| 1478 | 1480 | if (!nlist.ext()) continue; |
| 1479 | 1481 | if (nlist.sect()) { |
| 1480 | 1482 | const atom = self.getAtom(atom_index).?; |
| 1481 | if (!atom.alive.load(.seq_cst)) continue; | |
| 1483 | if (!atom.flags.alive) continue; | |
| 1482 | 1484 | } |
| 1483 | 1485 | |
| 1484 | 1486 | const gop = try macho_file.resolver.getOrPut(gpa, .{ |
| ... | ... | @@ -1643,7 +1645,7 @@ pub fn claimUnresolved(self: *Object, macho_file: *MachO) void { |
| 1643 | 1645 | |
| 1644 | 1646 | if (self.getSymbolRef(@intCast(i), macho_file).getFile(macho_file) != null) continue; |
| 1645 | 1647 | |
| 1646 | const is_import = switch (macho_file.options.undefined_treatment) { | |
| 1648 | const is_import = switch (macho_file.undefined_treatment) { | |
| 1647 | 1649 | .@"error" => false, |
| 1648 | 1650 | .warn, .suppress => nlist.weakRef(), |
| 1649 | 1651 | .dynamic_lookup => true, |
| ... | ... | @@ -1682,8 +1684,8 @@ pub fn claimUnresolvedRelocatable(self: *Object, macho_file: *MachO) void { |
| 1682 | 1684 | } |
| 1683 | 1685 | } |
| 1684 | 1686 | |
| 1685 | fn addSection(self: *Object, allocator: Allocator, segname: []const u8, sectname: []const u8) !u32 { | |
| 1686 | const n_sect = @as(u32, @intCast(try self.sections.addOne(allocator))); | |
| 1687 | fn addSection(self: *Object, allocator: Allocator, segname: []const u8, sectname: []const u8) !u8 { | |
| 1688 | const n_sect = @as(u8, @intCast(try self.sections.addOne(allocator))); | |
| 1687 | 1689 | self.sections.set(n_sect, .{ |
| 1688 | 1690 | .header = .{ |
| 1689 | 1691 | .sectname = MachO.makeStaticString(sectname), |
| ... | ... | @@ -1799,7 +1801,7 @@ pub fn writeAr(self: Object, ar_format: Archive.Format, macho_file: *MachO, writ |
| 1799 | 1801 | try writer.writeAll(data); |
| 1800 | 1802 | } |
| 1801 | 1803 | |
| 1802 | pub fn calcSymtabSize(self: *Object, macho_file: *MachO) !void { | |
| 1804 | pub fn calcSymtabSize(self: *Object, macho_file: *MachO) void { | |
| 1803 | 1805 | const tracy = trace(@src()); |
| 1804 | 1806 | defer tracy.end(); |
| 1805 | 1807 | |
| ... | ... | @@ -1821,27 +1823,27 @@ pub fn calcSymtabSize(self: *Object, macho_file: *MachO) !void { |
| 1821 | 1823 | continue; |
| 1822 | 1824 | sym.flags.output_symtab = true; |
| 1823 | 1825 | if (sym.isLocal()) { |
| 1824 | try sym.addExtra(.{ .symtab = self.output_symtab_ctx.nlocals }, macho_file); | |
| 1826 | sym.addExtra(.{ .symtab = self.output_symtab_ctx.nlocals }, macho_file); | |
| 1825 | 1827 | self.output_symtab_ctx.nlocals += 1; |
| 1826 | 1828 | } else if (sym.flags.@"export") { |
| 1827 | try sym.addExtra(.{ .symtab = self.output_symtab_ctx.nexports }, macho_file); | |
| 1829 | sym.addExtra(.{ .symtab = self.output_symtab_ctx.nexports }, macho_file); | |
| 1828 | 1830 | self.output_symtab_ctx.nexports += 1; |
| 1829 | 1831 | } else { |
| 1830 | 1832 | assert(sym.flags.import); |
| 1831 | try sym.addExtra(.{ .symtab = self.output_symtab_ctx.nimports }, macho_file); | |
| 1833 | sym.addExtra(.{ .symtab = self.output_symtab_ctx.nimports }, macho_file); | |
| 1832 | 1834 | self.output_symtab_ctx.nimports += 1; |
| 1833 | 1835 | } |
| 1834 | 1836 | self.output_symtab_ctx.strsize += @as(u32, @intCast(sym.getName(macho_file).len + 1)); |
| 1835 | 1837 | } |
| 1836 | 1838 | |
| 1837 | 1839 | if (macho_file.base.comp.config.debug_format != .strip and self.hasDebugInfo()) |
| 1838 | try self.calcStabsSize(macho_file); | |
| 1840 | self.calcStabsSize(macho_file); | |
| 1839 | 1841 | } |
| 1840 | 1842 | |
| 1841 | pub fn calcStabsSize(self: *Object, macho_file: *MachO) error{Overflow}!void { | |
| 1843 | pub fn calcStabsSize(self: *Object, macho_file: *MachO) void { | |
| 1842 | 1844 | if (self.compile_unit) |cu| { |
| 1843 | const comp_dir = cu.getCompDir(self); | |
| 1844 | const tu_name = cu.getTuName(self); | |
| 1845 | const comp_dir = cu.getCompDir(self.*); | |
| 1846 | const tu_name = cu.getTuName(self.*); | |
| 1845 | 1847 | |
| 1846 | 1848 | self.output_symtab_ctx.nstabs += 4; // N_SO, N_SO, N_OSO, N_SO |
| 1847 | 1849 | self.output_symtab_ctx.strsize += @as(u32, @intCast(comp_dir.len + 1)); // comp_dir |
| ... | ... | @@ -1876,12 +1878,12 @@ pub fn calcStabsSize(self: *Object, macho_file: *MachO) error{Overflow}!void { |
| 1876 | 1878 | |
| 1877 | 1879 | for (self.stab_files.items) |sf| { |
| 1878 | 1880 | self.output_symtab_ctx.nstabs += 4; // N_SO, N_SO, N_OSO, N_SO |
| 1879 | self.output_symtab_ctx.strsize += @as(u32, @intCast(sf.getCompDir(self).len + 1)); // comp_dir | |
| 1880 | self.output_symtab_ctx.strsize += @as(u32, @intCast(sf.getTuName(self).len + 1)); // tu_name | |
| 1881 | self.output_symtab_ctx.strsize += @as(u32, @intCast(sf.getOsoPath(self).len + 1)); // path | |
| 1881 | self.output_symtab_ctx.strsize += @as(u32, @intCast(sf.getCompDir(self.*).len + 1)); // comp_dir | |
| 1882 | self.output_symtab_ctx.strsize += @as(u32, @intCast(sf.getTuName(self.*).len + 1)); // tu_name | |
| 1883 | self.output_symtab_ctx.strsize += @as(u32, @intCast(sf.getOsoPath(self.*).len + 1)); // path | |
| 1882 | 1884 | |
| 1883 | 1885 | for (sf.stabs.items) |stab| { |
| 1884 | const sym = stab.getSymbol(macho_file) orelse continue; | |
| 1886 | const sym = stab.getSymbol(self.*) orelse continue; | |
| 1885 | 1887 | const file = sym.getFile(macho_file).?; |
| 1886 | 1888 | if (file.getIndex() != self.index) continue; |
| 1887 | 1889 | if (!sym.flags.output_symtab) continue; |
| ... | ... | @@ -2065,7 +2067,7 @@ pub fn writeCompactUnwindRelocatable(self: *Object, macho_file: *MachO) !void { |
| 2065 | 2067 | } |
| 2066 | 2068 | } |
| 2067 | 2069 | |
| 2068 | pub fn writeSymtab(self: Object, macho_file: *MachO) void { | |
| 2070 | pub fn writeSymtab(self: Object, macho_file: *MachO, ctx: anytype) void { | |
| 2069 | 2071 | const tracy = trace(@src()); |
| 2070 | 2072 | defer tracy.end(); |
| 2071 | 2073 | |
| ... | ... | @@ -2075,21 +2077,21 @@ pub fn writeSymtab(self: Object, macho_file: *MachO) void { |
| 2075 | 2077 | const file = ref.getFile(macho_file) orelse continue; |
| 2076 | 2078 | if (file.getIndex() != self.index) continue; |
| 2077 | 2079 | const idx = sym.getOutputSymtabIndex(macho_file) orelse continue; |
| 2078 | const out_sym = &macho_file.symtab.items[idx]; | |
| 2080 | const out_sym = &ctx.symtab.items[idx]; | |
| 2079 | 2081 | out_sym.n_strx = n_strx; |
| 2080 | 2082 | sym.setOutputSym(macho_file, out_sym); |
| 2081 | 2083 | const name = sym.getName(macho_file); |
| 2082 | @memcpy(macho_file.strtab.items[n_strx..][0..name.len], name); | |
| 2084 | @memcpy(ctx.strtab.items[n_strx..][0..name.len], name); | |
| 2083 | 2085 | n_strx += @intCast(name.len); |
| 2084 | macho_file.strtab.items[n_strx] = 0; | |
| 2086 | ctx.strtab.items[n_strx] = 0; | |
| 2085 | 2087 | n_strx += 1; |
| 2086 | 2088 | } |
| 2087 | 2089 | |
| 2088 | 2090 | if (macho_file.base.comp.config.debug_format != .strip and self.hasDebugInfo()) |
| 2089 | try self.writeStabs(n_strx, macho_file); | |
| 2091 | self.writeStabs(n_strx, macho_file, ctx); | |
| 2090 | 2092 | } |
| 2091 | 2093 | |
| 2092 | pub fn writeStabs(self: *const Object, stroff: u32, macho_file: *MachO) void { | |
| 2094 | pub fn writeStabs(self: Object, stroff: u32, macho_file: *MachO, ctx: anytype) void { | |
| 2093 | 2095 | const writeFuncStab = struct { |
| 2094 | 2096 | inline fn writeFuncStab( |
| 2095 | 2097 | n_strx: u32, |
| ... | ... | @@ -2097,7 +2099,7 @@ pub fn writeStabs(self: *const Object, stroff: u32, macho_file: *MachO) void { |
| 2097 | 2099 | n_value: u64, |
| 2098 | 2100 | size: u64, |
| 2099 | 2101 | index: u32, |
| 2100 | context: *MachO, | |
| 2102 | context: anytype, | |
| 2101 | 2103 | ) void { |
| 2102 | 2104 | context.symtab.items[index] = .{ |
| 2103 | 2105 | .n_strx = 0, |
| ... | ... | @@ -2139,7 +2141,7 @@ pub fn writeStabs(self: *const Object, stroff: u32, macho_file: *MachO) void { |
| 2139 | 2141 | |
| 2140 | 2142 | // Open scope |
| 2141 | 2143 | // N_SO comp_dir |
| 2142 | macho_file.symtab.items[index] = .{ | |
| 2144 | ctx.symtab.items[index] = .{ | |
| 2143 | 2145 | .n_strx = n_strx, |
| 2144 | 2146 | .n_type = macho.N_SO, |
| 2145 | 2147 | .n_sect = 0, |
| ... | ... | @@ -2147,9 +2149,9 @@ pub fn writeStabs(self: *const Object, stroff: u32, macho_file: *MachO) void { |
| 2147 | 2149 | .n_value = 0, |
| 2148 | 2150 | }; |
| 2149 | 2151 | index += 1; |
| 2150 | @memcpy(macho_file.strtab.items[n_strx..][0..comp_dir.len], comp_dir); | |
| 2152 | @memcpy(ctx.strtab.items[n_strx..][0..comp_dir.len], comp_dir); | |
| 2151 | 2153 | n_strx += @intCast(comp_dir.len); |
| 2152 | macho_file.strtab.items[n_strx] = 0; | |
| 2154 | ctx.strtab.items[n_strx] = 0; | |
| 2153 | 2155 | n_strx += 1; |
| 2154 | 2156 | // N_SO tu_name |
| 2155 | 2157 | macho_file.symtab.items[index] = .{ |
| ... | ... | @@ -2160,12 +2162,12 @@ pub fn writeStabs(self: *const Object, stroff: u32, macho_file: *MachO) void { |
| 2160 | 2162 | .n_value = 0, |
| 2161 | 2163 | }; |
| 2162 | 2164 | index += 1; |
| 2163 | @memcpy(macho_file.strtab.items[n_strx..][0..tu_name.len], tu_name); | |
| 2165 | @memcpy(ctx.strtab.items[n_strx..][0..tu_name.len], tu_name); | |
| 2164 | 2166 | n_strx += @intCast(tu_name.len); |
| 2165 | macho_file.strtab.items[n_strx] = 0; | |
| 2167 | ctx.strtab.items[n_strx] = 0; | |
| 2166 | 2168 | n_strx += 1; |
| 2167 | 2169 | // N_OSO path |
| 2168 | macho_file.symtab.items[index] = .{ | |
| 2170 | ctx.symtab.items[index] = .{ | |
| 2169 | 2171 | .n_strx = n_strx, |
| 2170 | 2172 | .n_type = macho.N_OSO, |
| 2171 | 2173 | .n_sect = 0, |
| ... | ... | @@ -2174,20 +2176,20 @@ pub fn writeStabs(self: *const Object, stroff: u32, macho_file: *MachO) void { |
| 2174 | 2176 | }; |
| 2175 | 2177 | index += 1; |
| 2176 | 2178 | if (self.in_archive) |ar| { |
| 2177 | @memcpy(macho_file.strtab.items[n_strx..][0..ar.path.len], ar.path); | |
| 2179 | @memcpy(ctx.strtab.items[n_strx..][0..ar.path.len], ar.path); | |
| 2178 | 2180 | n_strx += @intCast(ar.path.len); |
| 2179 | macho_file.strtab.items[n_strx] = '('; | |
| 2181 | ctx.strtab.items[n_strx] = '('; | |
| 2180 | 2182 | n_strx += 1; |
| 2181 | @memcpy(macho_file.strtab.items[n_strx..][0..self.path.len], self.path); | |
| 2183 | @memcpy(ctx.strtab.items[n_strx..][0..self.path.len], self.path); | |
| 2182 | 2184 | n_strx += @intCast(self.path.len); |
| 2183 | macho_file.strtab.items[n_strx] = ')'; | |
| 2185 | ctx.strtab.items[n_strx] = ')'; | |
| 2184 | 2186 | n_strx += 1; |
| 2185 | macho_file.strtab.items[n_strx] = 0; | |
| 2187 | ctx.strtab.items[n_strx] = 0; | |
| 2186 | 2188 | n_strx += 1; |
| 2187 | 2189 | } else { |
| 2188 | @memcpy(macho_file.strtab.items[n_strx..][0..self.path.len], self.path); | |
| 2190 | @memcpy(ctx.strtab.items[n_strx..][0..self.path.len], self.path); | |
| 2189 | 2191 | n_strx += @intCast(self.path.len); |
| 2190 | macho_file.strtab.items[n_strx] = 0; | |
| 2192 | ctx.strtab.items[n_strx] = 0; | |
| 2191 | 2193 | n_strx += 1; |
| 2192 | 2194 | } |
| 2193 | 2195 | |
| ... | ... | @@ -2203,17 +2205,17 @@ pub fn writeStabs(self: *const Object, stroff: u32, macho_file: *MachO) void { |
| 2203 | 2205 | const sect = macho_file.sections.items(.header)[sym.getOutputSectionIndex(macho_file)]; |
| 2204 | 2206 | const sym_n_strx = n_strx: { |
| 2205 | 2207 | const symtab_index = sym.getOutputSymtabIndex(macho_file).?; |
| 2206 | const osym = macho_file.symtab.items[symtab_index]; | |
| 2208 | const osym = ctx.symtab.items[symtab_index]; | |
| 2207 | 2209 | break :n_strx osym.n_strx; |
| 2208 | 2210 | }; |
| 2209 | 2211 | const sym_n_sect: u8 = if (!sym.flags.abs) @intCast(sym.getOutputSectionIndex(macho_file) + 1) else 0; |
| 2210 | 2212 | const sym_n_value = sym.getAddress(.{}, macho_file); |
| 2211 | 2213 | const sym_size = sym.getSize(macho_file); |
| 2212 | 2214 | if (sect.isCode()) { |
| 2213 | writeFuncStab(sym_n_strx, sym_n_sect, sym_n_value, sym_size, index, macho_file); | |
| 2215 | writeFuncStab(sym_n_strx, sym_n_sect, sym_n_value, sym_size, index, ctx); | |
| 2214 | 2216 | index += 4; |
| 2215 | 2217 | } else if (sym.visibility == .global) { |
| 2216 | macho_file.symtab.items[index] = .{ | |
| 2218 | ctx.symtab.items[index] = .{ | |
| 2217 | 2219 | .n_strx = sym_n_strx, |
| 2218 | 2220 | .n_type = macho.N_GSYM, |
| 2219 | 2221 | .n_sect = sym_n_sect, |
| ... | ... | @@ -2222,7 +2224,7 @@ pub fn writeStabs(self: *const Object, stroff: u32, macho_file: *MachO) void { |
| 2222 | 2224 | }; |
| 2223 | 2225 | index += 1; |
| 2224 | 2226 | } else { |
| 2225 | macho_file.symtab.items[index] = .{ | |
| 2227 | ctx.symtab.items[index] = .{ | |
| 2226 | 2228 | .n_strx = sym_n_strx, |
| 2227 | 2229 | .n_type = macho.N_STSYM, |
| 2228 | 2230 | .n_sect = sym_n_sect, |
| ... | ... | @@ -2235,7 +2237,7 @@ pub fn writeStabs(self: *const Object, stroff: u32, macho_file: *MachO) void { |
| 2235 | 2237 | |
| 2236 | 2238 | // Close scope |
| 2237 | 2239 | // N_SO |
| 2238 | macho_file.symtab.items[index] = .{ | |
| 2240 | ctx.symtab.items[index] = .{ | |
| 2239 | 2241 | .n_strx = 0, |
| 2240 | 2242 | .n_type = macho.N_SO, |
| 2241 | 2243 | .n_sect = 0, |
| ... | ... | @@ -2246,13 +2248,13 @@ pub fn writeStabs(self: *const Object, stroff: u32, macho_file: *MachO) void { |
| 2246 | 2248 | assert(self.hasSymbolStabs()); |
| 2247 | 2249 | |
| 2248 | 2250 | for (self.stab_files.items) |sf| { |
| 2249 | const comp_dir = sf.getCombDir(self); | |
| 2251 | const comp_dir = sf.getCompDir(self); | |
| 2250 | 2252 | const tu_name = sf.getTuName(self); |
| 2251 | 2253 | const oso_path = sf.getOsoPath(self); |
| 2252 | 2254 | |
| 2253 | 2255 | // Open scope |
| 2254 | 2256 | // N_SO comp_dir |
| 2255 | macho_file.symtab.items[index] = .{ | |
| 2257 | ctx.symtab.items[index] = .{ | |
| 2256 | 2258 | .n_strx = n_strx, |
| 2257 | 2259 | .n_type = macho.N_SO, |
| 2258 | 2260 | .n_sect = 0, |
| ... | ... | @@ -2260,12 +2262,12 @@ pub fn writeStabs(self: *const Object, stroff: u32, macho_file: *MachO) void { |
| 2260 | 2262 | .n_value = 0, |
| 2261 | 2263 | }; |
| 2262 | 2264 | index += 1; |
| 2263 | @memcpy(macho_file.strtab.items[n_strx..][0..comp_dir.len], comp_dir); | |
| 2265 | @memcpy(ctx.strtab.items[n_strx..][0..comp_dir.len], comp_dir); | |
| 2264 | 2266 | n_strx += @intCast(comp_dir.len); |
| 2265 | macho_file.strtab.items[n_strx] = 0; | |
| 2267 | ctx.strtab.items[n_strx] = 0; | |
| 2266 | 2268 | n_strx += 1; |
| 2267 | 2269 | // N_SO tu_name |
| 2268 | macho_file.symtab.items[index] = .{ | |
| 2270 | ctx.symtab.items[index] = .{ | |
| 2269 | 2271 | .n_strx = n_strx, |
| 2270 | 2272 | .n_type = macho.N_SO, |
| 2271 | 2273 | .n_sect = 0, |
| ... | ... | @@ -2273,12 +2275,12 @@ pub fn writeStabs(self: *const Object, stroff: u32, macho_file: *MachO) void { |
| 2273 | 2275 | .n_value = 0, |
| 2274 | 2276 | }; |
| 2275 | 2277 | index += 1; |
| 2276 | @memcpy(macho_file.strtab.items[n_strx..][0..tu_name.len], tu_name); | |
| 2278 | @memcpy(ctx.strtab.items[n_strx..][0..tu_name.len], tu_name); | |
| 2277 | 2279 | n_strx += @intCast(tu_name.len); |
| 2278 | macho_file.strtab.items[n_strx] = 0; | |
| 2280 | ctx.strtab.items[n_strx] = 0; | |
| 2279 | 2281 | n_strx += 1; |
| 2280 | 2282 | // N_OSO path |
| 2281 | macho_file.symtab.items[index] = .{ | |
| 2283 | ctx.symtab.items[index] = .{ | |
| 2282 | 2284 | .n_strx = n_strx, |
| 2283 | 2285 | .n_type = macho.N_OSO, |
| 2284 | 2286 | .n_sect = 0, |
| ... | ... | @@ -2286,29 +2288,29 @@ pub fn writeStabs(self: *const Object, stroff: u32, macho_file: *MachO) void { |
| 2286 | 2288 | .n_value = sf.getOsoModTime(self), |
| 2287 | 2289 | }; |
| 2288 | 2290 | index += 1; |
| 2289 | @memcpy(macho_file.strtab.items[n_strx..][0..oso_path.len], oso_path); | |
| 2291 | @memcpy(ctx.strtab.items[n_strx..][0..oso_path.len], oso_path); | |
| 2290 | 2292 | n_strx += @intCast(oso_path.len); |
| 2291 | macho_file.strtab.items[n_strx] = 0; | |
| 2293 | ctx.strtab.items[n_strx] = 0; | |
| 2292 | 2294 | n_strx += 1; |
| 2293 | 2295 | |
| 2294 | 2296 | for (sf.stabs.items) |stab| { |
| 2295 | const sym = stab.getSymbol(macho_file) orelse continue; | |
| 2297 | const sym = stab.getSymbol(self) orelse continue; | |
| 2296 | 2298 | const file = sym.getFile(macho_file).?; |
| 2297 | 2299 | if (file.getIndex() != self.index) continue; |
| 2298 | 2300 | if (!sym.flags.output_symtab) continue; |
| 2299 | 2301 | const sym_n_strx = n_strx: { |
| 2300 | 2302 | const symtab_index = sym.getOutputSymtabIndex(macho_file).?; |
| 2301 | const osym = macho_file.symtab.items[symtab_index]; | |
| 2303 | const osym = ctx.symtab.items[symtab_index]; | |
| 2302 | 2304 | break :n_strx osym.n_strx; |
| 2303 | 2305 | }; |
| 2304 | 2306 | const sym_n_sect: u8 = if (!sym.flags.abs) @intCast(sym.getOutputSectionIndex(macho_file) + 1) else 0; |
| 2305 | 2307 | const sym_n_value = sym.getAddress(.{}, macho_file); |
| 2306 | 2308 | const sym_size = sym.getSize(macho_file); |
| 2307 | 2309 | if (stab.is_func) { |
| 2308 | writeFuncStab(sym_n_strx, sym_n_sect, sym_n_value, sym_size, index, macho_file); | |
| 2310 | writeFuncStab(sym_n_strx, sym_n_sect, sym_n_value, sym_size, index, ctx); | |
| 2309 | 2311 | index += 4; |
| 2310 | 2312 | } else if (sym.visibility == .global) { |
| 2311 | macho_file.symtab.items[index] = .{ | |
| 2313 | ctx.symtab.items[index] = .{ | |
| 2312 | 2314 | .n_strx = sym_n_strx, |
| 2313 | 2315 | .n_type = macho.N_GSYM, |
| 2314 | 2316 | .n_sect = sym_n_sect, |
| ... | ... | @@ -2317,7 +2319,7 @@ pub fn writeStabs(self: *const Object, stroff: u32, macho_file: *MachO) void { |
| 2317 | 2319 | }; |
| 2318 | 2320 | index += 1; |
| 2319 | 2321 | } else { |
| 2320 | macho_file.symtab.items[index] = .{ | |
| 2322 | ctx.symtab.items[index] = .{ | |
| 2321 | 2323 | .n_strx = sym_n_strx, |
| 2322 | 2324 | .n_type = macho.N_STSYM, |
| 2323 | 2325 | .n_sect = sym_n_sect, |
| ... | ... | @@ -2330,7 +2332,7 @@ pub fn writeStabs(self: *const Object, stroff: u32, macho_file: *MachO) void { |
| 2330 | 2332 | |
| 2331 | 2333 | // Close scope |
| 2332 | 2334 | // N_SO |
| 2333 | macho_file.symtab.items[index] = .{ | |
| 2335 | ctx.symtab.items[index] = .{ | |
| 2334 | 2336 | .n_strx = 0, |
| 2335 | 2337 | .n_type = macho.N_SO, |
| 2336 | 2338 | .n_sect = 0, |
| ... | ... | @@ -2343,7 +2345,7 @@ pub fn writeStabs(self: *const Object, stroff: u32, macho_file: *MachO) void { |
| 2343 | 2345 | } |
| 2344 | 2346 | |
| 2345 | 2347 | pub fn getAtomRelocs(self: *const Object, atom: Atom, macho_file: *MachO) []const Relocation { |
| 2346 | const extra = atom.getExtra(macho_file).?; | |
| 2348 | const extra = atom.getExtra(macho_file); | |
| 2347 | 2349 | const relocs = self.sections.items(.relocs)[atom.n_sect]; |
| 2348 | 2350 | return relocs.items[extra.rel_index..][0..extra.rel_count]; |
| 2349 | 2351 | } |
| ... | ... | @@ -2402,7 +2404,7 @@ pub fn asFile(self: *Object) File { |
| 2402 | 2404 | } |
| 2403 | 2405 | |
| 2404 | 2406 | const AddAtomArgs = struct { |
| 2405 | name: MachO.String, | |
| 2407 | name: u32, | |
| 2406 | 2408 | n_sect: u8, |
| 2407 | 2409 | off: u64, |
| 2408 | 2410 | size: u64, |
| ... | ... | @@ -2420,7 +2422,7 @@ fn addAtom(self: *Object, allocator: Allocator, args: AddAtomArgs) !Atom.Index { |
| 2420 | 2422 | .size = args.size, |
| 2421 | 2423 | .off = args.off, |
| 2422 | 2424 | .extra = try self.addAtomExtra(allocator, .{}), |
| 2423 | .alignment = args.alignment, | |
| 2425 | .alignment = Atom.Alignment.fromLog2Units(args.alignment), | |
| 2424 | 2426 | }; |
| 2425 | 2427 | return atom_index; |
| 2426 | 2428 | } |
| ... | ... | @@ -2693,12 +2695,12 @@ fn formatSymtab( |
| 2693 | 2695 | } |
| 2694 | 2696 | for (object.stab_files.items) |sf| { |
| 2695 | 2697 | try writer.print(" stabs({s},{s},{s})\n", .{ |
| 2696 | sf.getCompDir(object), | |
| 2697 | sf.getTuName(object), | |
| 2698 | sf.getOsoPath(object), | |
| 2698 | sf.getCompDir(object.*), | |
| 2699 | sf.getTuName(object.*), | |
| 2700 | sf.getOsoPath(object.*), | |
| 2699 | 2701 | }); |
| 2700 | 2702 | for (sf.stabs.items) |stab| { |
| 2701 | try writer.print(" {}", .{stab.fmt(object)}); | |
| 2703 | try writer.print(" {}", .{stab.fmt(object.*)}); | |
| 2702 | 2704 | } |
| 2703 | 2705 | } |
| 2704 | 2706 | } |
| ... | ... | @@ -2744,33 +2746,33 @@ const StabFile = struct { |
| 2744 | 2746 | comp_dir: u32, |
| 2745 | 2747 | stabs: std.ArrayListUnmanaged(Stab) = .{}, |
| 2746 | 2748 | |
| 2747 | fn getCompDir(sf: StabFile, object: *const Object) [:0]const u8 { | |
| 2749 | fn getCompDir(sf: StabFile, object: Object) [:0]const u8 { | |
| 2748 | 2750 | const nlist = object.symtab.items(.nlist)[sf.comp_dir]; |
| 2749 | 2751 | return object.getString(nlist.n_strx); |
| 2750 | 2752 | } |
| 2751 | 2753 | |
| 2752 | fn getTuName(sf: StabFile, object: *const Object) [:0]const u8 { | |
| 2754 | fn getTuName(sf: StabFile, object: Object) [:0]const u8 { | |
| 2753 | 2755 | const nlist = object.symtab.items(.nlist)[sf.comp_dir + 1]; |
| 2754 | 2756 | return object.getString(nlist.n_strx); |
| 2755 | 2757 | } |
| 2756 | 2758 | |
| 2757 | fn getOsoPath(sf: StabFile, object: *const Object) [:0]const u8 { | |
| 2759 | fn getOsoPath(sf: StabFile, object: Object) [:0]const u8 { | |
| 2758 | 2760 | const nlist = object.symtab.items(.nlist)[sf.comp_dir + 2]; |
| 2759 | 2761 | return object.getString(nlist.n_strx); |
| 2760 | 2762 | } |
| 2761 | 2763 | |
| 2762 | fn getOsoModTime(sf: StabFile, object: *const Object) u64 { | |
| 2764 | fn getOsoModTime(sf: StabFile, object: Object) u64 { | |
| 2763 | 2765 | const nlist = object.symtab.items(.nlist)[sf.comp_dir + 2]; |
| 2764 | 2766 | return nlist.n_value; |
| 2765 | 2767 | } |
| 2766 | 2768 | |
| 2767 | 2769 | const Stab = struct { |
| 2768 | 2770 | is_func: bool = true, |
| 2769 | symbol: ?Symbol.Index = null, | |
| 2771 | index: ?Symbol.Index = null, | |
| 2770 | 2772 | |
| 2771 | fn getSymbol(stab: Stab, object: *const Object) ?*Symbol { | |
| 2773 | fn getSymbol(stab: Stab, object: Object) ?Symbol { | |
| 2772 | 2774 | const index = stab.index orelse return null; |
| 2773 | return &object.symbols.items[index]; | |
| 2775 | return object.symbols.items[index]; | |
| 2774 | 2776 | } |
| 2775 | 2777 | |
| 2776 | 2778 | pub fn format( |
| ... | ... | @@ -2786,9 +2788,9 @@ const StabFile = struct { |
| 2786 | 2788 | @compileError("do not format stabs directly"); |
| 2787 | 2789 | } |
| 2788 | 2790 | |
| 2789 | const StabFormatContext = struct { Stab, *const Object }; | |
| 2791 | const StabFormatContext = struct { Stab, Object }; | |
| 2790 | 2792 | |
| 2791 | pub fn fmt(stab: Stab, object: *const Object) std.fmt.Formatter(format2) { | |
| 2793 | pub fn fmt(stab: Stab, object: Object) std.fmt.Formatter(format2) { | |
| 2792 | 2794 | return .{ .data = .{ stab, object } }; |
| 2793 | 2795 | } |
| 2794 | 2796 | |
| ... | ... | @@ -2817,11 +2819,11 @@ const CompileUnit = struct { |
| 2817 | 2819 | comp_dir: u32, |
| 2818 | 2820 | tu_name: u32, |
| 2819 | 2821 | |
| 2820 | fn getCompDir(cu: CompileUnit, object: *const Object) [:0]const u8 { | |
| 2822 | fn getCompDir(cu: CompileUnit, object: Object) [:0]const u8 { | |
| 2821 | 2823 | return object.getString(cu.comp_dir); |
| 2822 | 2824 | } |
| 2823 | 2825 | |
| 2824 | fn getTuName(cu: CompileUnit, object: *const Object) [:0]const u8 { | |
| 2826 | fn getTuName(cu: CompileUnit, object: Object) [:0]const u8 { | |
| 2825 | 2827 | return object.getString(cu.tu_name); |
| 2826 | 2828 | } |
| 2827 | 2829 | }; |
| ... | ... | @@ -2840,15 +2842,14 @@ const CompactUnwindCtx = struct { |
| 2840 | 2842 | |
| 2841 | 2843 | const x86_64 = struct { |
| 2842 | 2844 | fn parseRelocs( |
| 2843 | self: *const Object, | |
| 2844 | n_sect: u8, | |
| 2845 | self: *Object, | |
| 2845 | 2846 | sect: macho.section_64, |
| 2846 | 2847 | out: *std.ArrayListUnmanaged(Relocation), |
| 2848 | handle: File.Handle, | |
| 2847 | 2849 | macho_file: *MachO, |
| 2848 | 2850 | ) !void { |
| 2849 | 2851 | const gpa = macho_file.base.comp.gpa; |
| 2850 | 2852 | |
| 2851 | const handle = macho_file.getFileHandle(self.file_handle); | |
| 2852 | 2853 | const relocs_buffer = try gpa.alloc(u8, sect.nreloc * @sizeOf(macho.relocation_info)); |
| 2853 | 2854 | defer gpa.free(relocs_buffer); |
| 2854 | 2855 | { |
| ... | ... | @@ -2857,8 +2858,12 @@ const x86_64 = struct { |
| 2857 | 2858 | } |
| 2858 | 2859 | const relocs = @as([*]align(1) const macho.relocation_info, @ptrCast(relocs_buffer.ptr))[0..sect.nreloc]; |
| 2859 | 2860 | |
| 2860 | const code = try self.getSectionData(@intCast(n_sect), macho_file); | |
| 2861 | const code = try gpa.alloc(u8, sect.size); | |
| 2861 | 2862 | defer gpa.free(code); |
| 2863 | { | |
| 2864 | const amt = try handle.preadAll(code, sect.offset + self.offset); | |
| 2865 | if (amt != code.len) return error.InputOutput; | |
| 2866 | } | |
| 2862 | 2867 | |
| 2863 | 2868 | try out.ensureTotalCapacityPrecise(gpa, relocs.len); |
| 2864 | 2869 | |
| ... | ... | @@ -2880,8 +2885,9 @@ const x86_64 = struct { |
| 2880 | 2885 | .X86_64_RELOC_SIGNED_4 => 4, |
| 2881 | 2886 | else => 0, |
| 2882 | 2887 | }; |
| 2888 | var is_extern = rel.r_extern == 1; | |
| 2883 | 2889 | |
| 2884 | const target = if (rel.r_extern == 0) blk: { | |
| 2890 | const target = if (!is_extern) blk: { | |
| 2885 | 2891 | const nsect = rel.r_symbolnum - 1; |
| 2886 | 2892 | const taddr: i64 = if (rel.r_pcrel == 1) |
| 2887 | 2893 | @as(i64, @intCast(sect.addr)) + rel.r_address + addend + 4 |
| ... | ... | @@ -2893,9 +2899,15 @@ const x86_64 = struct { |
| 2893 | 2899 | }); |
| 2894 | 2900 | return error.MalformedObject; |
| 2895 | 2901 | }; |
| 2896 | addend = taddr - @as(i64, @intCast(macho_file.getAtom(target).?.getInputAddress(macho_file))); | |
| 2902 | const target_atom = self.getAtom(target).?; | |
| 2903 | addend = taddr - @as(i64, @intCast(target_atom.getInputAddress(macho_file))); | |
| 2904 | const isec = target_atom.getInputSection(macho_file); | |
| 2905 | if (isCstringLiteral(isec) or isFixedSizeLiteral(isec) or isPtrLiteral(isec)) { | |
| 2906 | is_extern = true; | |
| 2907 | break :blk target_atom.getExtra(macho_file).literal_symbol_index; | |
| 2908 | } | |
| 2897 | 2909 | break :blk target; |
| 2898 | } else self.symbols.items[rel.r_symbolnum]; | |
| 2910 | } else rel.r_symbolnum; | |
| 2899 | 2911 | |
| 2900 | 2912 | const has_subtractor = if (i > 0 and |
| 2901 | 2913 | @as(macho.reloc_type_x86_64, @enumFromInt(relocs[i - 1].r_type)) == .X86_64_RELOC_SUBTRACTOR) |
| ... | ... | @@ -2909,7 +2921,7 @@ const x86_64 = struct { |
| 2909 | 2921 | break :blk true; |
| 2910 | 2922 | } else false; |
| 2911 | 2923 | |
| 2912 | const @"type": Relocation.Type = validateRelocType(rel, rel_type) catch |err| { | |
| 2924 | const @"type": Relocation.Type = validateRelocType(rel, rel_type, is_extern) catch |err| { | |
| 2913 | 2925 | switch (err) { |
| 2914 | 2926 | error.Pcrel => try macho_file.reportParseError2( |
| 2915 | 2927 | self.index, |
| ... | ... | @@ -2936,7 +2948,7 @@ const x86_64 = struct { |
| 2936 | 2948 | }; |
| 2937 | 2949 | |
| 2938 | 2950 | out.appendAssumeCapacity(.{ |
| 2939 | .tag = if (rel.r_extern == 1) .@"extern" else .local, | |
| 2951 | .tag = if (is_extern) .@"extern" else .local, | |
| 2940 | 2952 | .offset = @as(u32, @intCast(rel.r_address)), |
| 2941 | 2953 | .target = target, |
| 2942 | 2954 | .addend = addend, |
| ... | ... | @@ -2951,7 +2963,7 @@ const x86_64 = struct { |
| 2951 | 2963 | } |
| 2952 | 2964 | } |
| 2953 | 2965 | |
| 2954 | fn validateRelocType(rel: macho.relocation_info, rel_type: macho.reloc_type_x86_64) !Relocation.Type { | |
| 2966 | fn validateRelocType(rel: macho.relocation_info, rel_type: macho.reloc_type_x86_64, is_extern: bool) !Relocation.Type { | |
| 2955 | 2967 | switch (rel_type) { |
| 2956 | 2968 | .X86_64_RELOC_UNSIGNED => { |
| 2957 | 2969 | if (rel.r_pcrel == 1) return error.Pcrel; |
| ... | ... | @@ -2971,7 +2983,7 @@ const x86_64 = struct { |
| 2971 | 2983 | => { |
| 2972 | 2984 | if (rel.r_pcrel == 0) return error.NonPcrel; |
| 2973 | 2985 | if (rel.r_length != 2) return error.InvalidLength; |
| 2974 | if (rel.r_extern == 0) return error.NonExtern; | |
| 2986 | if (!is_extern) return error.NonExtern; | |
| 2975 | 2987 | return switch (rel_type) { |
| 2976 | 2988 | .X86_64_RELOC_BRANCH => .branch, |
| 2977 | 2989 | .X86_64_RELOC_GOT_LOAD => .got_load, |
| ... | ... | @@ -3002,15 +3014,14 @@ const x86_64 = struct { |
| 3002 | 3014 | |
| 3003 | 3015 | const aarch64 = struct { |
| 3004 | 3016 | fn parseRelocs( |
| 3005 | self: *const Object, | |
| 3006 | n_sect: u8, | |
| 3017 | self: *Object, | |
| 3007 | 3018 | sect: macho.section_64, |
| 3008 | 3019 | out: *std.ArrayListUnmanaged(Relocation), |
| 3020 | handle: File.Handle, | |
| 3009 | 3021 | macho_file: *MachO, |
| 3010 | 3022 | ) !void { |
| 3011 | 3023 | const gpa = macho_file.base.comp.gpa; |
| 3012 | 3024 | |
| 3013 | const handle = macho_file.getFileHandle(self.file_handle); | |
| 3014 | 3025 | const relocs_buffer = try gpa.alloc(u8, sect.nreloc * @sizeOf(macho.relocation_info)); |
| 3015 | 3026 | defer gpa.free(relocs_buffer); |
| 3016 | 3027 | { |
| ... | ... | @@ -3019,8 +3030,12 @@ const aarch64 = struct { |
| 3019 | 3030 | } |
| 3020 | 3031 | const relocs = @as([*]align(1) const macho.relocation_info, @ptrCast(relocs_buffer.ptr))[0..sect.nreloc]; |
| 3021 | 3032 | |
| 3022 | const code = try self.getSectionData(@intCast(n_sect), macho_file); | |
| 3033 | const code = try gpa.alloc(u8, sect.size); | |
| 3023 | 3034 | defer gpa.free(code); |
| 3035 | { | |
| 3036 | const amt = try handle.preadAll(code, sect.offset + self.offset); | |
| 3037 | if (amt != code.len) return error.InputOutput; | |
| 3038 | } | |
| 3024 | 3039 | |
| 3025 | 3040 | try out.ensureTotalCapacityPrecise(gpa, relocs.len); |
| 3026 | 3041 | |
| ... | ... | @@ -3066,8 +3081,9 @@ const aarch64 = struct { |
| 3066 | 3081 | } |
| 3067 | 3082 | |
| 3068 | 3083 | const rel_type: macho.reloc_type_arm64 = @enumFromInt(rel.r_type); |
| 3084 | var is_extern = rel.r_extern == 1; | |
| 3069 | 3085 | |
| 3070 | const target = if (rel.r_extern == 0) blk: { | |
| 3086 | const target = if (!is_extern) blk: { | |
| 3071 | 3087 | const nsect = rel.r_symbolnum - 1; |
| 3072 | 3088 | const taddr: i64 = if (rel.r_pcrel == 1) |
| 3073 | 3089 | @as(i64, @intCast(sect.addr)) + rel.r_address + addend |
| ... | ... | @@ -3079,9 +3095,15 @@ const aarch64 = struct { |
| 3079 | 3095 | }); |
| 3080 | 3096 | return error.MalformedObject; |
| 3081 | 3097 | }; |
| 3082 | addend = taddr - @as(i64, @intCast(macho_file.getAtom(target).?.getInputAddress(macho_file))); | |
| 3098 | const target_atom = self.getAtom(target).?; | |
| 3099 | addend = taddr - @as(i64, @intCast(target_atom.getInputAddress(macho_file))); | |
| 3100 | const isec = target_atom.getInputSection(macho_file); | |
| 3101 | if (isCstringLiteral(isec) or isFixedSizeLiteral(isec) or isPtrLiteral(isec)) { | |
| 3102 | is_extern = true; | |
| 3103 | break :blk target_atom.getExtra(macho_file).literal_symbol_index; | |
| 3104 | } | |
| 3083 | 3105 | break :blk target; |
| 3084 | } else self.symbols.items[rel.r_symbolnum]; | |
| 3106 | } else rel.r_symbolnum; | |
| 3085 | 3107 | |
| 3086 | 3108 | const has_subtractor = if (i > 0 and |
| 3087 | 3109 | @as(macho.reloc_type_arm64, @enumFromInt(relocs[i - 1].r_type)) == .ARM64_RELOC_SUBTRACTOR) |
| ... | ... | @@ -3095,7 +3117,7 @@ const aarch64 = struct { |
| 3095 | 3117 | break :blk true; |
| 3096 | 3118 | } else false; |
| 3097 | 3119 | |
| 3098 | const @"type": Relocation.Type = validateRelocType(rel, rel_type) catch |err| { | |
| 3120 | const @"type": Relocation.Type = validateRelocType(rel, rel_type, is_extern) catch |err| { | |
| 3099 | 3121 | switch (err) { |
| 3100 | 3122 | error.Pcrel => try macho_file.reportParseError2( |
| 3101 | 3123 | self.index, |
| ... | ... | @@ -3122,7 +3144,7 @@ const aarch64 = struct { |
| 3122 | 3144 | }; |
| 3123 | 3145 | |
| 3124 | 3146 | out.appendAssumeCapacity(.{ |
| 3125 | .tag = if (rel.r_extern == 1) .@"extern" else .local, | |
| 3147 | .tag = if (is_extern) .@"extern" else .local, | |
| 3126 | 3148 | .offset = @as(u32, @intCast(rel.r_address)), |
| 3127 | 3149 | .target = target, |
| 3128 | 3150 | .addend = addend, |
| ... | ... | @@ -3137,7 +3159,7 @@ const aarch64 = struct { |
| 3137 | 3159 | } |
| 3138 | 3160 | } |
| 3139 | 3161 | |
| 3140 | fn validateRelocType(rel: macho.relocation_info, rel_type: macho.reloc_type_arm64) !Relocation.Type { | |
| 3162 | fn validateRelocType(rel: macho.relocation_info, rel_type: macho.reloc_type_arm64, is_extern: bool) !Relocation.Type { | |
| 3141 | 3163 | switch (rel_type) { |
| 3142 | 3164 | .ARM64_RELOC_UNSIGNED => { |
| 3143 | 3165 | if (rel.r_pcrel == 1) return error.Pcrel; |
| ... | ... | @@ -3158,7 +3180,7 @@ const aarch64 = struct { |
| 3158 | 3180 | => { |
| 3159 | 3181 | if (rel.r_pcrel == 0) return error.NonPcrel; |
| 3160 | 3182 | if (rel.r_length != 2) return error.InvalidLength; |
| 3161 | if (rel.r_extern == 0) return error.NonExtern; | |
| 3183 | if (!is_extern) return error.NonExtern; | |
| 3162 | 3184 | return switch (rel_type) { |
| 3163 | 3185 | .ARM64_RELOC_BRANCH26 => .branch, |
| 3164 | 3186 | .ARM64_RELOC_PAGE21 => .page, |
| ... | ... | @@ -3175,7 +3197,7 @@ const aarch64 = struct { |
| 3175 | 3197 | => { |
| 3176 | 3198 | if (rel.r_pcrel == 1) return error.Pcrel; |
| 3177 | 3199 | if (rel.r_length != 2) return error.InvalidLength; |
| 3178 | if (rel.r_extern == 0) return error.NonExtern; | |
| 3200 | if (!is_extern) return error.NonExtern; | |
| 3179 | 3201 | return switch (rel_type) { |
| 3180 | 3202 | .ARM64_RELOC_PAGEOFF12 => .pageoff, |
| 3181 | 3203 | .ARM64_RELOC_GOT_LOAD_PAGEOFF12 => .got_load_pageoff, |
src/link/MachO/Symbol.zig+2-2| ... | ... | @@ -150,7 +150,7 @@ pub fn getObjcSelrefsAddress(symbol: Symbol, macho_file: *MachO) u64 { |
| 150 | 150 | const file = symbol.getFile(macho_file).?; |
| 151 | 151 | return switch (file) { |
| 152 | 152 | .dylib, .zig_object => unreachable, |
| 153 | .object, .internal => |x| x.symbols.items[extra.objc_selrefs].getAddress(.{}, macho_file), | |
| 153 | inline else => |x| x.symbols.items[extra.objc_selrefs].getAddress(.{}, macho_file), | |
| 154 | 154 | }; |
| 155 | 155 | } |
| 156 | 156 | |
| ... | ... | @@ -186,7 +186,7 @@ pub fn getOutputSymtabIndex(symbol: Symbol, macho_file: *MachO) ?u32 { |
| 186 | 186 | const symtab_ctx = switch (file) { |
| 187 | 187 | inline else => |x| x.output_symtab_ctx, |
| 188 | 188 | }; |
| 189 | var idx = symbol.getExtra(macho_file).?.symtab; | |
| 189 | var idx = symbol.getExtra(macho_file).symtab; | |
| 190 | 190 | if (symbol.isLocal()) { |
| 191 | 191 | idx += symtab_ctx.ilocal; |
| 192 | 192 | } else if (symbol.flags.@"export") { |
src/link/MachO/UnwindInfo.zig+2-2| ... | ... | @@ -473,11 +473,11 @@ pub const Record = struct { |
| 473 | 473 | } |
| 474 | 474 | |
| 475 | 475 | pub fn getAtom(rec: Record, macho_file: *MachO) *Atom { |
| 476 | return macho_file.getAtom(rec.atom).?; | |
| 476 | return rec.getObject(macho_file).getAtom(rec.atom).?; | |
| 477 | 477 | } |
| 478 | 478 | |
| 479 | 479 | pub fn getLsdaAtom(rec: Record, macho_file: *MachO) ?*Atom { |
| 480 | return macho_file.getAtom(rec.lsda); | |
| 480 | return rec.getObject(macho_file).getAtom(rec.lsda); | |
| 481 | 481 | } |
| 482 | 482 | |
| 483 | 483 | pub fn getPersonality(rec: Record, macho_file: *MachO) ?*Symbol { |
src/link/MachO/ZigObject.zig+35-5| ... | ... | @@ -343,6 +343,36 @@ pub fn writeAr(self: ZigObject, ar_format: Archive.Format, writer: anytype) !voi |
| 343 | 343 | try writer.writeAll(self.data.items); |
| 344 | 344 | } |
| 345 | 345 | |
| 346 | pub fn claimUnresolved(self: *ZigObject, macho_file: *MachO) void { | |
| 347 | const tracy = trace(@src()); | |
| 348 | defer tracy.end(); | |
| 349 | ||
| 350 | for (self.symbols.items, 0..) |*sym, i| { | |
| 351 | const nlist = self.symtab.items(.nlist)[i]; | |
| 352 | if (!nlist.ext()) continue; | |
| 353 | if (!nlist.undf()) continue; | |
| 354 | ||
| 355 | if (self.getSymbolRef(@intCast(i), macho_file).getFile(macho_file) != null) continue; | |
| 356 | ||
| 357 | const is_import = switch (macho_file.undefined_treatment) { | |
| 358 | .@"error" => false, | |
| 359 | .warn, .suppress => nlist.weakRef(), | |
| 360 | .dynamic_lookup => true, | |
| 361 | }; | |
| 362 | if (is_import) { | |
| 363 | sym.value = 0; | |
| 364 | sym.atom_ref = .{ .index = 0, .file = 0 }; | |
| 365 | sym.flags.weak = false; | |
| 366 | sym.flags.weak_ref = nlist.weakRef(); | |
| 367 | sym.flags.import = is_import; | |
| 368 | sym.visibility = .global; | |
| 369 | ||
| 370 | const idx = self.globals.items[i]; | |
| 371 | macho_file.resolver.values.items[idx - 1] = .{ .index = @intCast(i), .file = self.index }; | |
| 372 | } | |
| 373 | } | |
| 374 | } | |
| 375 | ||
| 346 | 376 | pub fn scanRelocs(self: *ZigObject, macho_file: *MachO) !void { |
| 347 | 377 | for (self.getAtoms()) |atom_index| { |
| 348 | 378 | const atom = self.getAtom(atom_index) orelse continue; |
| ... | ... | @@ -378,7 +408,7 @@ pub fn calcSymtabSize(self: *ZigObject, macho_file: *MachO) void { |
| 378 | 408 | } |
| 379 | 409 | } |
| 380 | 410 | |
| 381 | pub fn writeSymtab(self: ZigObject, macho_file: *MachO) void { | |
| 411 | pub fn writeSymtab(self: ZigObject, macho_file: *MachO, ctx: anytype) void { | |
| 382 | 412 | const tracy = trace(@src()); |
| 383 | 413 | defer tracy.end(); |
| 384 | 414 | |
| ... | ... | @@ -388,13 +418,13 @@ pub fn writeSymtab(self: ZigObject, macho_file: *MachO) void { |
| 388 | 418 | const file = ref.getFile(macho_file) orelse continue; |
| 389 | 419 | if (file.getIndex() != self.index) continue; |
| 390 | 420 | const idx = sym.getOutputSymtabIndex(macho_file) orelse continue; |
| 391 | const out_sym = &macho_file.symtab.items[idx]; | |
| 421 | const out_sym = &ctx.symtab.items[idx]; | |
| 392 | 422 | out_sym.n_strx = n_strx; |
| 393 | 423 | sym.setOutputSym(macho_file, out_sym); |
| 394 | 424 | const name = sym.getName(macho_file); |
| 395 | @memcpy(macho_file.strtab.items[n_strx..][0..name.len], name); | |
| 425 | @memcpy(ctx.strtab.items[n_strx..][0..name.len], name); | |
| 396 | 426 | n_strx += @intCast(name.len); |
| 397 | macho_file.strtab.items[n_strx] = 0; | |
| 427 | ctx.strtab.items[n_strx] = 0; | |
| 398 | 428 | n_strx += 1; |
| 399 | 429 | } |
| 400 | 430 | } |
| ... | ... | @@ -1098,7 +1128,7 @@ pub fn lowerUnnamedConst( |
| 1098 | 1128 | }, |
| 1099 | 1129 | }; |
| 1100 | 1130 | const sym = self.symbols.items[sym_index]; |
| 1101 | try unnamed_consts.append(gpa, sym.atom); | |
| 1131 | try unnamed_consts.append(gpa, sym.atom_ref.index); | |
| 1102 | 1132 | return sym_index; |
| 1103 | 1133 | } |
| 1104 | 1134 |
src/link/MachO/dyld_info/Rebase.zig+5-5| ... | ... | @@ -34,7 +34,7 @@ pub fn updateSize(rebase: *Rebase, macho_file: *MachO) !void { |
| 34 | 34 | for (objects.items) |index| { |
| 35 | 35 | const file = macho_file.getFile(index).?; |
| 36 | 36 | for (file.getAtoms()) |atom_index| { |
| 37 | const atom = macho_file.getAtom(atom_index) orelse continue; | |
| 37 | const atom = file.getAtom(atom_index) orelse continue; | |
| 38 | 38 | if (!atom.flags.alive) continue; |
| 39 | 39 | if (atom.getInputSection(macho_file).isZerofill()) continue; |
| 40 | 40 | const atom_addr = atom.getAddress(macho_file); |
| ... | ... | @@ -43,7 +43,7 @@ pub fn updateSize(rebase: *Rebase, macho_file: *MachO) !void { |
| 43 | 43 | for (atom.getRelocs(macho_file)) |rel| { |
| 44 | 44 | if (rel.type != .unsigned or rel.meta.length != 3) continue; |
| 45 | 45 | if (rel.tag == .@"extern") { |
| 46 | const sym = rel.getTargetSymbol(macho_file); | |
| 46 | const sym = rel.getTargetSymbol(atom.*, macho_file); | |
| 47 | 47 | if (sym.isTlvInit(macho_file)) continue; |
| 48 | 48 | if (sym.flags.import) continue; |
| 49 | 49 | } |
| ... | ... | @@ -72,7 +72,7 @@ pub fn updateSize(rebase: *Rebase, macho_file: *MachO) !void { |
| 72 | 72 | const seg_id = macho_file.sections.items(.segment_id)[sid]; |
| 73 | 73 | const seg = macho_file.segments.items[seg_id]; |
| 74 | 74 | for (macho_file.got.symbols.items, 0..) |ref, idx| { |
| 75 | const sym = macho_file.getSymbol(ref); | |
| 75 | const sym = ref.getSymbol(macho_file).?; | |
| 76 | 76 | const addr = macho_file.got.getAddress(@intCast(idx), macho_file); |
| 77 | 77 | if (!sym.flags.import) { |
| 78 | 78 | try rebase.entries.append(gpa, .{ |
| ... | ... | @@ -88,7 +88,7 @@ pub fn updateSize(rebase: *Rebase, macho_file: *MachO) !void { |
| 88 | 88 | const seg_id = macho_file.sections.items(.segment_id)[sid]; |
| 89 | 89 | const seg = macho_file.segments.items[seg_id]; |
| 90 | 90 | for (macho_file.stubs.symbols.items, 0..) |ref, idx| { |
| 91 | const sym = macho_file.getSymbol(ref); | |
| 91 | const sym = ref.getSymbol(macho_file).?; | |
| 92 | 92 | const addr = sect.addr + idx * @sizeOf(u64); |
| 93 | 93 | const rebase_entry = Rebase.Entry{ |
| 94 | 94 | .offset = addr - seg.vmaddr, |
| ... | ... | @@ -104,7 +104,7 @@ pub fn updateSize(rebase: *Rebase, macho_file: *MachO) !void { |
| 104 | 104 | const seg_id = macho_file.sections.items(.segment_id)[sid]; |
| 105 | 105 | const seg = macho_file.segments.items[seg_id]; |
| 106 | 106 | for (macho_file.tlv_ptr.symbols.items, 0..) |ref, idx| { |
| 107 | const sym = macho_file.getSymbol(ref); | |
| 107 | const sym = ref.getSymbol(macho_file).?; | |
| 108 | 108 | const addr = macho_file.tlv_ptr.getAddress(@intCast(idx), macho_file); |
| 109 | 109 | if (!sym.flags.import) { |
| 110 | 110 | try rebase.entries.append(gpa, .{ |
src/link/MachO/dyld_info/Trie.zig+22-24| ... | ... | @@ -94,33 +94,31 @@ pub fn updateSize(self: *Trie, macho_file: *MachO) !void { |
| 94 | 94 | const gpa = macho_file.base.comp.gpa; |
| 95 | 95 | |
| 96 | 96 | try self.init(gpa); |
| 97 | // TODO | |
| 98 | // try self.nodes.ensureUnusedCapacity(gpa, macho_file.resolver.values.items.len * 2); | |
| 99 | // try self.edges.ensureUnusedCapacity(gpa, macho_file.resolver.values.items.len * 2); | |
| 97 | try self.nodes.ensureUnusedCapacity(gpa, macho_file.resolver.values.items.len * 2); | |
| 98 | try self.edges.ensureUnusedCapacity(gpa, macho_file.resolver.values.items.len * 2); | |
| 100 | 99 | |
| 101 | 100 | const seg = macho_file.getTextSegment(); |
| 102 | for (macho_file.objects.items) |index| { | |
| 103 | for (macho_file.getFile(index).?.getSymbols()) |ref| { | |
| 104 | const sym = macho_file.getSymbol(ref); | |
| 105 | if (!sym.flags.@"export") continue; | |
| 106 | if (sym.getAtom(macho_file)) |atom| if (!atom.flags.alive) continue; | |
| 107 | var flags: u64 = if (sym.flags.abs) | |
| 108 | macho.EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE | |
| 109 | else if (sym.flags.tlv) | |
| 110 | macho.EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL | |
| 111 | else | |
| 112 | macho.EXPORT_SYMBOL_FLAGS_KIND_REGULAR; | |
| 113 | if (sym.flags.weak) { | |
| 114 | flags |= macho.EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION; | |
| 115 | macho_file.weak_defines = true; | |
| 116 | macho_file.binds_to_weak = true; | |
| 117 | } | |
| 118 | try self.put(gpa, .{ | |
| 119 | .name = sym.getName(macho_file), | |
| 120 | .vmaddr_offset = sym.getAddress(.{ .stubs = false }, macho_file) - seg.vmaddr, | |
| 121 | .export_flags = flags, | |
| 122 | }); | |
| 101 | for (macho_file.resolver.values.items) |ref| { | |
| 102 | if (ref.getFile(macho_file) == null) continue; | |
| 103 | const sym = ref.getSymbol(macho_file).?; | |
| 104 | if (!sym.flags.@"export") continue; | |
| 105 | if (sym.getAtom(macho_file)) |atom| if (!atom.flags.alive) continue; | |
| 106 | var flags: u64 = if (sym.flags.abs) | |
| 107 | macho.EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE | |
| 108 | else if (sym.flags.tlv) | |
| 109 | macho.EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL | |
| 110 | else | |
| 111 | macho.EXPORT_SYMBOL_FLAGS_KIND_REGULAR; | |
| 112 | if (sym.flags.weak) { | |
| 113 | flags |= macho.EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION; | |
| 114 | macho_file.weak_defines = true; | |
| 115 | macho_file.binds_to_weak = true; | |
| 123 | 116 | } |
| 117 | try self.put(gpa, .{ | |
| 118 | .name = sym.getName(macho_file), | |
| 119 | .vmaddr_offset = sym.getAddress(.{ .stubs = false }, macho_file) - seg.vmaddr, | |
| 120 | .export_flags = flags, | |
| 121 | }); | |
| 124 | 122 | } |
| 125 | 123 | |
| 126 | 124 | try self.finalize(gpa); |
src/link/MachO/dyld_info/bind.zig+27-25| ... | ... | @@ -1,17 +1,19 @@ |
| 1 | 1 | pub const Entry = struct { |
| 2 | target: Symbol.Index, | |
| 2 | target: MachO.Ref, | |
| 3 | 3 | offset: u64, |
| 4 | 4 | segment_id: u8, |
| 5 | 5 | addend: i64, |
| 6 | 6 | |
| 7 | 7 | pub fn lessThan(ctx: *MachO, entry: Entry, other: Entry) bool { |
| 8 | _ = ctx; | |
| 8 | 9 | if (entry.segment_id == other.segment_id) { |
| 9 | if (entry.target == other.target) { | |
| 10 | if (entry.target.eql(other.target)) { | |
| 10 | 11 | return entry.offset < other.offset; |
| 11 | 12 | } |
| 12 | const entry_name = ctx.getSymbol(entry.target).getName(ctx); | |
| 13 | const other_name = ctx.getSymbol(other.target).getName(ctx); | |
| 14 | return std.mem.lessThan(u8, entry_name, other_name); | |
| 13 | if (entry.target.file == other.target.file) { | |
| 14 | return entry.target.index < other.target.index; | |
| 15 | } | |
| 16 | return entry.target.file < other.target.file; | |
| 15 | 17 | } |
| 16 | 18 | return entry.segment_id < other.segment_id; |
| 17 | 19 | } |
| ... | ... | @@ -44,7 +46,7 @@ pub const Bind = struct { |
| 44 | 46 | for (objects.items) |index| { |
| 45 | 47 | const file = macho_file.getFile(index).?; |
| 46 | 48 | for (file.getAtoms()) |atom_index| { |
| 47 | const atom = macho_file.getAtom(atom_index) orelse continue; | |
| 49 | const atom = file.getAtom(atom_index) orelse continue; | |
| 48 | 50 | if (!atom.flags.alive) continue; |
| 49 | 51 | if (atom.getInputSection(macho_file).isZerofill()) continue; |
| 50 | 52 | const atom_addr = atom.getAddress(macho_file); |
| ... | ... | @@ -55,10 +57,10 @@ pub const Bind = struct { |
| 55 | 57 | if (rel.type != .unsigned or rel.meta.length != 3 or rel.tag != .@"extern") continue; |
| 56 | 58 | const rel_offset = rel.offset - atom.off; |
| 57 | 59 | const addend = rel.addend + rel.getRelocAddend(cpu_arch); |
| 58 | const sym = rel.getTargetSymbol(macho_file); | |
| 60 | const sym = rel.getTargetSymbol(atom.*, macho_file); | |
| 59 | 61 | if (sym.isTlvInit(macho_file)) continue; |
| 60 | 62 | const entry = Entry{ |
| 61 | .target = rel.target, | |
| 63 | .target = rel.getTargetSymbolRef(atom.*, macho_file), | |
| 62 | 64 | .offset = atom_addr + rel_offset - seg.vmaddr, |
| 63 | 65 | .segment_id = seg_id, |
| 64 | 66 | .addend = addend, |
| ... | ... | @@ -74,7 +76,7 @@ pub const Bind = struct { |
| 74 | 76 | const seg_id = macho_file.sections.items(.segment_id)[sid]; |
| 75 | 77 | const seg = macho_file.segments.items[seg_id]; |
| 76 | 78 | for (macho_file.got.symbols.items, 0..) |ref, idx| { |
| 77 | const sym = macho_file.getSymbol(ref); | |
| 79 | const sym = ref.getSymbol(macho_file).?; | |
| 78 | 80 | const addr = macho_file.got.getAddress(@intCast(idx), macho_file); |
| 79 | 81 | const entry = Entry{ |
| 80 | 82 | .target = ref, |
| ... | ... | @@ -93,7 +95,7 @@ pub const Bind = struct { |
| 93 | 95 | const seg_id = macho_file.sections.items(.segment_id)[sid]; |
| 94 | 96 | const seg = macho_file.segments.items[seg_id]; |
| 95 | 97 | for (macho_file.stubs.symbols.items, 0..) |ref, idx| { |
| 96 | const sym = macho_file.getSymbol(ref); | |
| 98 | const sym = ref.getSymbol(macho_file).?; | |
| 97 | 99 | const addr = sect.addr + idx * @sizeOf(u64); |
| 98 | 100 | const bind_entry = Entry{ |
| 99 | 101 | .target = ref, |
| ... | ... | @@ -112,7 +114,7 @@ pub const Bind = struct { |
| 112 | 114 | const seg = macho_file.segments.items[seg_id]; |
| 113 | 115 | |
| 114 | 116 | for (macho_file.tlv_ptr.symbols.items, 0..) |ref, idx| { |
| 115 | const sym = macho_file.getSymbol(ref); | |
| 117 | const sym = ref.getSymbol(macho_file).?; | |
| 116 | 118 | const addr = macho_file.tlv_ptr.getAddress(@intCast(idx), macho_file); |
| 117 | 119 | const entry = Entry{ |
| 118 | 120 | .target = ref, |
| ... | ... | @@ -162,7 +164,7 @@ pub const Bind = struct { |
| 162 | 164 | var addend: i64 = 0; |
| 163 | 165 | var count: usize = 0; |
| 164 | 166 | var skip: u64 = 0; |
| 165 | var target: ?Symbol.Index = null; | |
| 167 | var target: ?MachO.Ref = null; | |
| 166 | 168 | |
| 167 | 169 | var state: enum { |
| 168 | 170 | start, |
| ... | ... | @@ -173,7 +175,7 @@ pub const Bind = struct { |
| 173 | 175 | var i: usize = 0; |
| 174 | 176 | while (i < entries.len) : (i += 1) { |
| 175 | 177 | const current = entries[i]; |
| 176 | if (target == null or target.? != current.target) { | |
| 178 | if (target == null or !target.?.eql(current.target)) { | |
| 177 | 179 | switch (state) { |
| 178 | 180 | .start => {}, |
| 179 | 181 | .bind_single => try doBind(writer), |
| ... | ... | @@ -182,7 +184,7 @@ pub const Bind = struct { |
| 182 | 184 | state = .start; |
| 183 | 185 | target = current.target; |
| 184 | 186 | |
| 185 | const sym = ctx.getSymbol(current.target); | |
| 187 | const sym = current.target.getSymbol(ctx).?; | |
| 186 | 188 | const name = sym.getName(ctx); |
| 187 | 189 | const flags: u8 = if (sym.weakRef(ctx)) macho.BIND_SYMBOL_FLAGS_WEAK_IMPORT else 0; |
| 188 | 190 | const ordinal: i16 = ord: { |
| ... | ... | @@ -296,7 +298,7 @@ pub const WeakBind = struct { |
| 296 | 298 | for (objects.items) |index| { |
| 297 | 299 | const file = macho_file.getFile(index).?; |
| 298 | 300 | for (file.getAtoms()) |atom_index| { |
| 299 | const atom = macho_file.getAtom(atom_index) orelse continue; | |
| 301 | const atom = file.getAtom(atom_index) orelse continue; | |
| 300 | 302 | if (!atom.flags.alive) continue; |
| 301 | 303 | if (atom.getInputSection(macho_file).isZerofill()) continue; |
| 302 | 304 | const atom_addr = atom.getAddress(macho_file); |
| ... | ... | @@ -307,10 +309,10 @@ pub const WeakBind = struct { |
| 307 | 309 | if (rel.type != .unsigned or rel.meta.length != 3 or rel.tag != .@"extern") continue; |
| 308 | 310 | const rel_offset = rel.offset - atom.off; |
| 309 | 311 | const addend = rel.addend + rel.getRelocAddend(cpu_arch); |
| 310 | const sym = rel.getTargetSymbol(macho_file); | |
| 312 | const sym = rel.getTargetSymbol(atom.*, macho_file); | |
| 311 | 313 | if (sym.isTlvInit(macho_file)) continue; |
| 312 | 314 | const entry = Entry{ |
| 313 | .target = rel.target, | |
| 315 | .target = rel.getTargetSymbolRef(atom.*, macho_file), | |
| 314 | 316 | .offset = atom_addr + rel_offset - seg.vmaddr, |
| 315 | 317 | .segment_id = seg_id, |
| 316 | 318 | .addend = addend, |
| ... | ... | @@ -326,7 +328,7 @@ pub const WeakBind = struct { |
| 326 | 328 | const seg_id = macho_file.sections.items(.segment_id)[sid]; |
| 327 | 329 | const seg = macho_file.segments.items[seg_id]; |
| 328 | 330 | for (macho_file.got.symbols.items, 0..) |ref, idx| { |
| 329 | const sym = macho_file.getSymbol(ref); | |
| 331 | const sym = ref.getSymbol(macho_file).?; | |
| 330 | 332 | const addr = macho_file.got.getAddress(@intCast(idx), macho_file); |
| 331 | 333 | const entry = Entry{ |
| 332 | 334 | .target = ref, |
| ... | ... | @@ -346,7 +348,7 @@ pub const WeakBind = struct { |
| 346 | 348 | const seg = macho_file.segments.items[seg_id]; |
| 347 | 349 | |
| 348 | 350 | for (macho_file.stubs.symbols.items, 0..) |ref, idx| { |
| 349 | const sym = macho_file.getSymbol(ref); | |
| 351 | const sym = ref.getSymbol(macho_file).?; | |
| 350 | 352 | const addr = sect.addr + idx * @sizeOf(u64); |
| 351 | 353 | const bind_entry = Entry{ |
| 352 | 354 | .target = ref, |
| ... | ... | @@ -365,7 +367,7 @@ pub const WeakBind = struct { |
| 365 | 367 | const seg = macho_file.segments.items[seg_id]; |
| 366 | 368 | |
| 367 | 369 | for (macho_file.tlv_ptr.symbols.items, 0..) |ref, idx| { |
| 368 | const sym = macho_file.getSymbol(ref); | |
| 370 | const sym = ref.getSymbol(macho_file).?; | |
| 369 | 371 | const addr = macho_file.tlv_ptr.getAddress(@intCast(idx), macho_file); |
| 370 | 372 | const entry = Entry{ |
| 371 | 373 | .target = ref, |
| ... | ... | @@ -415,7 +417,7 @@ pub const WeakBind = struct { |
| 415 | 417 | var addend: i64 = 0; |
| 416 | 418 | var count: usize = 0; |
| 417 | 419 | var skip: u64 = 0; |
| 418 | var target: ?Symbol.Index = null; | |
| 420 | var target: ?MachO.Ref = null; | |
| 419 | 421 | |
| 420 | 422 | var state: enum { |
| 421 | 423 | start, |
| ... | ... | @@ -426,7 +428,7 @@ pub const WeakBind = struct { |
| 426 | 428 | var i: usize = 0; |
| 427 | 429 | while (i < entries.len) : (i += 1) { |
| 428 | 430 | const current = entries[i]; |
| 429 | if (target == null or target.? != current.target) { | |
| 431 | if (target == null or !target.?.eql(current.target)) { | |
| 430 | 432 | switch (state) { |
| 431 | 433 | .start => {}, |
| 432 | 434 | .bind_single => try doBind(writer), |
| ... | ... | @@ -435,7 +437,7 @@ pub const WeakBind = struct { |
| 435 | 437 | state = .start; |
| 436 | 438 | target = current.target; |
| 437 | 439 | |
| 438 | const sym = ctx.getSymbol(current.target); | |
| 440 | const sym = current.target.getSymbol(ctx).?; | |
| 439 | 441 | const name = sym.getName(ctx); |
| 440 | 442 | const flags: u8 = 0; // TODO NON_WEAK_DEFINITION |
| 441 | 443 | |
| ... | ... | @@ -536,7 +538,7 @@ pub const LazyBind = struct { |
| 536 | 538 | const seg = macho_file.segments.items[seg_id]; |
| 537 | 539 | |
| 538 | 540 | for (macho_file.stubs.symbols.items, 0..) |ref, idx| { |
| 539 | const sym = macho_file.getSymbol(ref); | |
| 541 | const sym = ref.getSymbol(macho_file).?; | |
| 540 | 542 | const addr = sect.addr + idx * @sizeOf(u64); |
| 541 | 543 | const bind_entry = Entry{ |
| 542 | 544 | .target = ref, |
| ... | ... | @@ -565,7 +567,7 @@ pub const LazyBind = struct { |
| 565 | 567 | for (self.entries.items) |entry| { |
| 566 | 568 | self.offsets.appendAssumeCapacity(@intCast(self.buffer.items.len)); |
| 567 | 569 | |
| 568 | const sym = ctx.getSymbol(entry.target); | |
| 570 | const sym = entry.target.getSymbol(ctx).?; | |
| 569 | 571 | const name = sym.getName(ctx); |
| 570 | 572 | const flags: u8 = if (sym.weakRef(ctx)) macho.BIND_SYMBOL_FLAGS_WEAK_IMPORT else 0; |
| 571 | 573 | const ordinal: i16 = ord: { |
src/link/MachO/file.zig+11-11| ... | ... | @@ -30,10 +30,10 @@ pub const File = union(enum) { |
| 30 | 30 | } |
| 31 | 31 | } |
| 32 | 32 | |
| 33 | pub fn resolveSymbols(file: File, macho_file: *MachO) void { | |
| 34 | switch (file) { | |
| 33 | pub fn resolveSymbols(file: File, macho_file: *MachO) !void { | |
| 34 | return switch (file) { | |
| 35 | 35 | inline else => |x| x.resolveSymbols(macho_file), |
| 36 | } | |
| 36 | }; | |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | 39 | pub fn scanRelocs(file: File, macho_file: *MachO) !void { |
| ... | ... | @@ -147,19 +147,19 @@ pub const File = union(enum) { |
| 147 | 147 | if (ref.getFile(macho_file) == null) continue; |
| 148 | 148 | if (ref.file != file.getIndex()) continue; |
| 149 | 149 | const sym = ref.getSymbol(macho_file).?; |
| 150 | if (sym.getSectionFlags().got) { | |
| 150 | if (sym.flags.needs_got) { | |
| 151 | 151 | log.debug("'{s}' needs GOT", .{sym.getName(macho_file)}); |
| 152 | 152 | try macho_file.got.addSymbol(ref, macho_file); |
| 153 | 153 | } |
| 154 | if (sym.getSectionFlags().stubs) { | |
| 154 | if (sym.flags.stubs) { | |
| 155 | 155 | log.debug("'{s}' needs STUBS", .{sym.getName(macho_file)}); |
| 156 | 156 | try macho_file.stubs.addSymbol(ref, macho_file); |
| 157 | 157 | } |
| 158 | if (sym.getSectionFlags().tlv_ptr) { | |
| 158 | if (sym.flags.tlv_ptr) { | |
| 159 | 159 | log.debug("'{s}' needs TLV pointer", .{sym.getName(macho_file)}); |
| 160 | 160 | try macho_file.tlv_ptr.addSymbol(ref, macho_file); |
| 161 | 161 | } |
| 162 | if (sym.getSectionFlags().objc_stubs) { | |
| 162 | if (sym.flags.objc_stubs) { | |
| 163 | 163 | log.debug("'{s}' needs OBJC STUBS", .{sym.getName(macho_file)}); |
| 164 | 164 | try macho_file.objc_stubs.addSymbol(ref, macho_file); |
| 165 | 165 | } |
| ... | ... | @@ -171,7 +171,7 @@ pub const File = union(enum) { |
| 171 | 171 | defer tracy.end(); |
| 172 | 172 | for (file.getAtoms()) |atom_index| { |
| 173 | 173 | const atom = file.getAtom(atom_index) orelse continue; |
| 174 | if (!atom.alive.load(.seq_cst)) continue; | |
| 174 | if (!atom.flags.alive) continue; | |
| 175 | 175 | atom.out_n_sect = try Atom.initOutputSection(atom.getInputSection(macho_file), macho_file); |
| 176 | 176 | } |
| 177 | 177 | } |
| ... | ... | @@ -185,18 +185,18 @@ pub const File = union(enum) { |
| 185 | 185 | |
| 186 | 186 | pub fn writeAtoms(file: File, macho_file: *MachO) !void { |
| 187 | 187 | return switch (file) { |
| 188 | .dylib => unreachable, | |
| 188 | .dylib, .zig_object => unreachable, | |
| 189 | 189 | inline else => |x| x.writeAtoms(macho_file), |
| 190 | 190 | }; |
| 191 | 191 | } |
| 192 | 192 | |
| 193 | pub fn calcSymtabSize(file: File, macho_file: *MachO) !void { | |
| 193 | pub fn calcSymtabSize(file: File, macho_file: *MachO) void { | |
| 194 | 194 | return switch (file) { |
| 195 | 195 | inline else => |x| x.calcSymtabSize(macho_file), |
| 196 | 196 | }; |
| 197 | 197 | } |
| 198 | 198 | |
| 199 | pub fn writeSymtab(file: File, macho_file: *MachO, ctx: anytype) !void { | |
| 199 | pub fn writeSymtab(file: File, macho_file: *MachO, ctx: anytype) void { | |
| 200 | 200 | return switch (file) { |
| 201 | 201 | inline else => |x| x.writeSymtab(macho_file, ctx), |
| 202 | 202 | }; |
src/link/MachO/synthetic.zig+5-5| ... | ... | @@ -23,7 +23,7 @@ pub const ZigGotSection = struct { |
| 23 | 23 | const index = try zig_got.allocateEntry(gpa); |
| 24 | 24 | const entry = &zig_got.entries.items[index]; |
| 25 | 25 | entry.* = sym_index; |
| 26 | const symbol = zo.getSymbol(sym_index); | |
| 26 | const symbol = &zo.symbols.items[sym_index]; | |
| 27 | 27 | assert(symbol.flags.needs_zig_got); |
| 28 | 28 | symbol.flags.has_zig_got = true; |
| 29 | 29 | symbol.addExtra(.{ .zig_got = index }, macho_file); |
| ... | ... | @@ -56,7 +56,7 @@ pub const ZigGotSection = struct { |
| 56 | 56 | const zo = macho_file.getZigObject().?; |
| 57 | 57 | const off = zig_got.entryOffset(index, macho_file); |
| 58 | 58 | const entry = zig_got.entries.items[index]; |
| 59 | const value = zo.getSymbol(entry).getAddress(.{ .stubs = false }, macho_file); | |
| 59 | const value = zo.symbols.items[entry].getAddress(.{ .stubs = false }, macho_file); | |
| 60 | 60 | |
| 61 | 61 | var buf: [8]u8 = undefined; |
| 62 | 62 | std.mem.writeInt(u64, &buf, value, .little); |
| ... | ... | @@ -66,7 +66,7 @@ pub const ZigGotSection = struct { |
| 66 | 66 | pub fn writeAll(zig_got: ZigGotSection, macho_file: *MachO, writer: anytype) !void { |
| 67 | 67 | const zo = macho_file.getZigObject().?; |
| 68 | 68 | for (zig_got.entries.items) |entry| { |
| 69 | const symbol = zo.getSymbol(entry); | |
| 69 | const symbol = zo.symbols.items[entry]; | |
| 70 | 70 | const value = symbol.address(.{ .stubs = false }, macho_file); |
| 71 | 71 | try writer.writeInt(u64, value, .little); |
| 72 | 72 | } |
| ... | ... | @@ -94,7 +94,7 @@ pub const ZigGotSection = struct { |
| 94 | 94 | const zo = macho_file.getZigObject().?; |
| 95 | 95 | try writer.writeAll("__zig_got\n"); |
| 96 | 96 | for (zig_got.entries.items, 0..) |entry, index| { |
| 97 | const symbol = zo.getSymbol(entry); | |
| 97 | const symbol = zo.symbols.items[entry]; | |
| 98 | 98 | try writer.print(" {d}@0x{x} => {d}@0x{x} ({s})\n", .{ |
| 99 | 99 | index, |
| 100 | 100 | zig_got.entryAddress(@intCast(index), macho_file), |
| ... | ... | @@ -694,7 +694,7 @@ pub const DataInCode = struct { |
| 694 | 694 | dices[next_dice].offset < end_off) : (next_dice += 1) |
| 695 | 695 | {} |
| 696 | 696 | |
| 697 | if (atom.alive.load(.seq_cst)) for (dices[start_dice..next_dice]) |d| { | |
| 697 | if (atom.flags.alive) for (dices[start_dice..next_dice]) |d| { | |
| 698 | 698 | dice.entries.appendAssumeCapacity(.{ |
| 699 | 699 | .offset = @intCast(atom.getAddress(macho_file) + d.offset - start_off - base_address), |
| 700 | 700 | .length = d.length, |
src/link/MachO/thunks.zig+3-3| ... | ... | @@ -37,7 +37,7 @@ pub fn createThunks(sect_id: u8, macho_file: *MachO) !void { |
| 37 | 37 | |
| 38 | 38 | // Scan relocs in the group and create trampolines for any unreachable callsite |
| 39 | 39 | try scanRelocs(thunk_index, gpa, atoms[start..i], macho_file); |
| 40 | thunk.value = try advance(header, thunk.size(), .@"4"); | |
| 40 | thunk.value = advance(header, thunk.size(), .@"4"); | |
| 41 | 41 | |
| 42 | 42 | log.debug("thunk({d}) : {}", .{ thunk_index, thunk.fmt(macho_file) }); |
| 43 | 43 | } |
| ... | ... | @@ -99,8 +99,8 @@ pub const Thunk = struct { |
| 99 | 99 | return header.addr + thunk.value; |
| 100 | 100 | } |
| 101 | 101 | |
| 102 | pub fn getTargetAddress(thunk: Thunk, sym_index: Symbol.Index, macho_file: *MachO) u64 { | |
| 103 | return thunk.getAddress(macho_file) + thunk.symbols.getIndex(sym_index).? * trampoline_size; | |
| 102 | pub fn getTargetAddress(thunk: Thunk, ref: MachO.Ref, macho_file: *MachO) u64 { | |
| 103 | return thunk.getAddress(macho_file) + thunk.symbols.getIndex(ref).? * trampoline_size; | |
| 104 | 104 | } |
| 105 | 105 | |
| 106 | 106 | pub fn write(thunk: Thunk, macho_file: *MachO, writer: anytype) !void { |