| author | |
| committer | |
| log | d722f0cc627c34f2c18681202a512d9aaa58fb18 |
| tree | cd332e7430de06d0fba74aad5104e2ee6da99edf |
| parent | 5269cbea202c6a81108390f4c47bc8a04fc6d1ff |
Remove currently obsolete AtomParser from Object.3 files changed, 14 insertions(+), 169 deletions(-)
src/link/MachO.zig+12-7| ... | @@ -1869,7 +1869,7 @@ fn writeAtoms(self: *MachO) !void { | ... | @@ -1869,7 +1869,7 @@ fn writeAtoms(self: *MachO) !void { |
| 1869 | pub fn createGotAtom(self: *MachO, key: GotIndirectionKey) !*Atom { | 1869 | pub fn createGotAtom(self: *MachO, key: GotIndirectionKey) !*Atom { |
| 1870 | const local_sym_index = @intCast(u32, self.locals.items.len); | 1870 | const local_sym_index = @intCast(u32, self.locals.items.len); |
| 1871 | try self.locals.append(self.base.allocator, .{ | 1871 | try self.locals.append(self.base.allocator, .{ |
| 1872 | .n_strx = try self.makeString("l_zld_got_entry"), | 1872 | .n_strx = 0, |
| 1873 | .n_type = macho.N_SECT, | 1873 | .n_type = macho.N_SECT, |
| 1874 | .n_sect = 0, | 1874 | .n_sect = 0, |
| 1875 | .n_desc = 0, | 1875 | .n_desc = 0, |
| ... | @@ -1907,7 +1907,7 @@ fn createDyldPrivateAtom(self: *MachO) !void { | ... | @@ -1907,7 +1907,7 @@ fn createDyldPrivateAtom(self: *MachO) !void { |
| 1907 | const local_sym_index = @intCast(u32, self.locals.items.len); | 1907 | const local_sym_index = @intCast(u32, self.locals.items.len); |
| 1908 | const sym = try self.locals.addOne(self.base.allocator); | 1908 | const sym = try self.locals.addOne(self.base.allocator); |
| 1909 | sym.* = .{ | 1909 | sym.* = .{ |
| 1910 | .n_strx = try self.makeString("l_zld_dyld_private"), | 1910 | .n_strx = 0, |
| 1911 | .n_type = macho.N_SECT, | 1911 | .n_type = macho.N_SECT, |
| 1912 | .n_sect = 0, | 1912 | .n_sect = 0, |
| 1913 | .n_desc = 0, | 1913 | .n_desc = 0, |
| ... | @@ -1941,7 +1941,7 @@ fn createStubHelperPreambleAtom(self: *MachO) !void { | ... | @@ -1941,7 +1941,7 @@ fn createStubHelperPreambleAtom(self: *MachO) !void { |
| 1941 | const local_sym_index = @intCast(u32, self.locals.items.len); | 1941 | const local_sym_index = @intCast(u32, self.locals.items.len); |
| 1942 | const sym = try self.locals.addOne(self.base.allocator); | 1942 | const sym = try self.locals.addOne(self.base.allocator); |
| 1943 | sym.* = .{ | 1943 | sym.* = .{ |
| 1944 | .n_strx = try self.makeString("l_zld_stub_preamble"), | 1944 | .n_strx = 0, |
| 1945 | .n_type = macho.N_SECT, | 1945 | .n_type = macho.N_SECT, |
| 1946 | .n_sect = 0, | 1946 | .n_sect = 0, |
| 1947 | .n_desc = 0, | 1947 | .n_desc = 0, |
| ... | @@ -2083,7 +2083,7 @@ pub fn createStubHelperAtom(self: *MachO) !*Atom { | ... | @@ -2083,7 +2083,7 @@ pub fn createStubHelperAtom(self: *MachO) !*Atom { |
| 2083 | }; | 2083 | }; |
| 2084 | const local_sym_index = @intCast(u32, self.locals.items.len); | 2084 | const local_sym_index = @intCast(u32, self.locals.items.len); |
| 2085 | try self.locals.append(self.base.allocator, .{ | 2085 | try self.locals.append(self.base.allocator, .{ |
| 2086 | .n_strx = try self.makeString("l_zld_stub_in_stub_helper"), | 2086 | .n_strx = 0, |
| 2087 | .n_type = macho.N_SECT, | 2087 | .n_type = macho.N_SECT, |
| 2088 | .n_sect = 0, | 2088 | .n_sect = 0, |
| 2089 | .n_desc = 0, | 2089 | .n_desc = 0, |
| ... | @@ -2138,7 +2138,7 @@ pub fn createStubHelperAtom(self: *MachO) !*Atom { | ... | @@ -2138,7 +2138,7 @@ pub fn createStubHelperAtom(self: *MachO) !*Atom { |
| 2138 | pub fn createLazyPointerAtom(self: *MachO, stub_sym_index: u32, lazy_binding_sym_index: u32) !*Atom { | 2138 | pub fn createLazyPointerAtom(self: *MachO, stub_sym_index: u32, lazy_binding_sym_index: u32) !*Atom { |
| 2139 | const local_sym_index = @intCast(u32, self.locals.items.len); | 2139 | const local_sym_index = @intCast(u32, self.locals.items.len); |
| 2140 | try self.locals.append(self.base.allocator, .{ | 2140 | try self.locals.append(self.base.allocator, .{ |
| 2141 | .n_strx = try self.makeString("l_zld_lazy_ptr"), | 2141 | .n_strx = 0, |
| 2142 | .n_type = macho.N_SECT, | 2142 | .n_type = macho.N_SECT, |
| 2143 | .n_sect = 0, | 2143 | .n_sect = 0, |
| 2144 | .n_desc = 0, | 2144 | .n_desc = 0, |
| ... | @@ -2179,7 +2179,7 @@ pub fn createStubAtom(self: *MachO, laptr_sym_index: u32) !*Atom { | ... | @@ -2179,7 +2179,7 @@ pub fn createStubAtom(self: *MachO, laptr_sym_index: u32) !*Atom { |
| 2179 | }; | 2179 | }; |
| 2180 | const local_sym_index = @intCast(u32, self.locals.items.len); | 2180 | const local_sym_index = @intCast(u32, self.locals.items.len); |
| 2181 | try self.locals.append(self.base.allocator, .{ | 2181 | try self.locals.append(self.base.allocator, .{ |
| 2182 | .n_strx = try self.makeString("l_zld_stub"), | 2182 | .n_strx = 0, |
| 2183 | .n_type = macho.N_SECT, | 2183 | .n_type = macho.N_SECT, |
| 2184 | .n_sect = 0, | 2184 | .n_sect = 0, |
| 2185 | .n_desc = 0, | 2185 | .n_desc = 0, |
| ... | @@ -4741,7 +4741,12 @@ fn writeSymbolTable(self: *MachO) !void { | ... | @@ -4741,7 +4741,12 @@ fn writeSymbolTable(self: *MachO) !void { |
| 4741 | 4741 | ||
| 4742 | var locals = std.ArrayList(macho.nlist_64).init(self.base.allocator); | 4742 | var locals = std.ArrayList(macho.nlist_64).init(self.base.allocator); |
| 4743 | defer locals.deinit(); | 4743 | defer locals.deinit(); |
| 4744 | try locals.appendSlice(self.locals.items); | 4744 | |
| 4745 | for (self.locals.items) |sym| { | ||
| 4746 | if (sym.n_strx == 0) continue; | ||
| 4747 | if (symbolIsTemp(sym, self.getString(sym.n_strx))) continue; | ||
| 4748 | try locals.append(sym); | ||
| 4749 | } | ||
| 4745 | 4750 | ||
| 4746 | if (self.has_stabs) { | 4751 | if (self.has_stabs) { |
| 4747 | for (self.objects.items) |object| { | 4752 | for (self.objects.items) |object| { |
src/link/MachO/Atom.zig+1-8| ... | @@ -663,15 +663,8 @@ fn initRelocFromObject(rel: macho.relocation_info, context: RelocContext) !Reloc | ... | @@ -663,15 +663,8 @@ fn initRelocFromObject(rel: macho.relocation_info, context: RelocContext) !Reloc |
| 663 | const sect = seg.sections.items[sect_id]; | 663 | const sect = seg.sections.items[sect_id]; |
| 664 | const match = (try context.macho_file.getMatchingSection(sect)) orelse unreachable; | 664 | const match = (try context.macho_file.getMatchingSection(sect)) orelse unreachable; |
| 665 | const local_sym_index = @intCast(u32, context.macho_file.locals.items.len); | 665 | const local_sym_index = @intCast(u32, context.macho_file.locals.items.len); |
| 666 | const sym_name = try std.fmt.allocPrint(context.allocator, "l_{s}_{s}_{s}", .{ | ||
| 667 | context.object.name, | ||
| 668 | commands.segmentName(sect), | ||
| 669 | commands.sectionName(sect), | ||
| 670 | }); | ||
| 671 | defer context.allocator.free(sym_name); | ||
| 672 | |||
| 673 | try context.macho_file.locals.append(context.allocator, .{ | 666 | try context.macho_file.locals.append(context.allocator, .{ |
| 674 | .n_strx = try context.macho_file.makeString(sym_name), | 667 | .n_strx = 0, |
| 675 | .n_type = macho.N_SECT, | 668 | .n_type = macho.N_SECT, |
| 676 | .n_sect = @intCast(u8, context.macho_file.section_ordinals.getIndex(match).? + 1), | 669 | .n_sect = @intCast(u8, context.macho_file.section_ordinals.getIndex(match).? + 1), |
| 677 | .n_desc = 0, | 670 | .n_desc = 0, |
src/link/MachO/Object.zig+1-154| ... | @@ -375,152 +375,6 @@ fn filterDice(dices: []macho.data_in_code_entry, start_addr: u64, end_addr: u64) | ... | @@ -375,152 +375,6 @@ fn filterDice(dices: []macho.data_in_code_entry, start_addr: u64, end_addr: u64) |
| 375 | return dices[start..end]; | 375 | return dices[start..end]; |
| 376 | } | 376 | } |
| 377 | 377 | ||
| 378 | const Context = struct { | ||
| 379 | allocator: *Allocator, | ||
| 380 | object: *Object, | ||
| 381 | macho_file: *MachO, | ||
| 382 | match: MachO.MatchingSection, | ||
| 383 | }; | ||
| 384 | |||
| 385 | const AtomParser = struct { | ||
| 386 | section: macho.section_64, | ||
| 387 | code: []u8, | ||
| 388 | relocs: []macho.relocation_info, | ||
| 389 | nlists: []NlistWithIndex, | ||
| 390 | index: u32 = 0, | ||
| 391 | |||
| 392 | fn peek(self: AtomParser) ?NlistWithIndex { | ||
| 393 | return if (self.index + 1 < self.nlists.len) self.nlists[self.index + 1] else null; | ||
| 394 | } | ||
| 395 | |||
| 396 | fn lessThanBySeniority(context: Context, lhs: NlistWithIndex, rhs: NlistWithIndex) bool { | ||
| 397 | if (!MachO.symbolIsExt(rhs.nlist)) { | ||
| 398 | return MachO.symbolIsTemp(lhs.nlist, context.object.getString(lhs.nlist.n_strx)); | ||
| 399 | } else if (MachO.symbolIsPext(rhs.nlist) or MachO.symbolIsWeakDef(rhs.nlist)) { | ||
| 400 | return !MachO.symbolIsExt(lhs.nlist); | ||
| 401 | } else { | ||
| 402 | return false; | ||
| 403 | } | ||
| 404 | } | ||
| 405 | |||
| 406 | pub fn next(self: *AtomParser, context: Context) !?*Atom { | ||
| 407 | if (self.index == self.nlists.len) return null; | ||
| 408 | |||
| 409 | const tracy = trace(@src()); | ||
| 410 | defer tracy.end(); | ||
| 411 | |||
| 412 | var aliases = std.ArrayList(NlistWithIndex).init(context.allocator); | ||
| 413 | defer aliases.deinit(); | ||
| 414 | |||
| 415 | const next_nlist: ?NlistWithIndex = blk: while (true) { | ||
| 416 | const curr_nlist = self.nlists[self.index]; | ||
| 417 | try aliases.append(curr_nlist); | ||
| 418 | |||
| 419 | if (self.peek()) |next_nlist| { | ||
| 420 | if (curr_nlist.nlist.n_value == next_nlist.nlist.n_value) { | ||
| 421 | self.index += 1; | ||
| 422 | continue; | ||
| 423 | } | ||
| 424 | break :blk next_nlist; | ||
| 425 | } | ||
| 426 | break :blk null; | ||
| 427 | } else null; | ||
| 428 | |||
| 429 | for (aliases.items) |*nlist_with_index| { | ||
| 430 | nlist_with_index.index = context.object.symbol_mapping.get(nlist_with_index.index) orelse unreachable; | ||
| 431 | } | ||
| 432 | |||
| 433 | if (aliases.items.len > 1) { | ||
| 434 | // Bubble-up senior symbol as the main link to the atom. | ||
| 435 | sort.sort( | ||
| 436 | NlistWithIndex, | ||
| 437 | aliases.items, | ||
| 438 | context, | ||
| 439 | AtomParser.lessThanBySeniority, | ||
| 440 | ); | ||
| 441 | } | ||
| 442 | |||
| 443 | const senior_nlist = aliases.pop(); | ||
| 444 | const senior_sym = &context.macho_file.locals.items[senior_nlist.index]; | ||
| 445 | senior_sym.n_sect = @intCast(u8, context.macho_file.section_ordinals.getIndex(context.match).? + 1); | ||
| 446 | |||
| 447 | const start_addr = senior_nlist.nlist.n_value - self.section.addr; | ||
| 448 | const end_addr = if (next_nlist) |n| n.nlist.n_value - self.section.addr else self.section.size; | ||
| 449 | |||
| 450 | const code = self.code[start_addr..end_addr]; | ||
| 451 | const size = code.len; | ||
| 452 | |||
| 453 | const max_align = self.section.@"align"; | ||
| 454 | const actual_align = if (senior_nlist.nlist.n_value > 0) | ||
| 455 | math.min(@ctz(u64, senior_nlist.nlist.n_value), max_align) | ||
| 456 | else | ||
| 457 | max_align; | ||
| 458 | |||
| 459 | const stab: ?Atom.Stab = if (context.object.debug_info) |di| blk: { | ||
| 460 | // TODO there has to be a better to handle this. | ||
| 461 | for (di.inner.func_list.items) |func| { | ||
| 462 | if (func.pc_range) |range| { | ||
| 463 | if (senior_nlist.nlist.n_value >= range.start and senior_nlist.nlist.n_value < range.end) { | ||
| 464 | break :blk Atom.Stab{ | ||
| 465 | .function = range.end - range.start, | ||
| 466 | }; | ||
| 467 | } | ||
| 468 | } | ||
| 469 | } | ||
| 470 | // TODO | ||
| 471 | // if (self.macho_file.globals.contains(self.macho_file.getString(senior_sym.strx))) break :blk .global; | ||
| 472 | break :blk .static; | ||
| 473 | } else null; | ||
| 474 | |||
| 475 | const atom = try context.macho_file.createEmptyAtom(senior_nlist.index, size, actual_align); | ||
| 476 | atom.stab = stab; | ||
| 477 | |||
| 478 | const is_zerofill = blk: { | ||
| 479 | const section_type = commands.sectionType(self.section); | ||
| 480 | break :blk section_type == macho.S_ZEROFILL or section_type == macho.S_THREAD_LOCAL_ZEROFILL; | ||
| 481 | }; | ||
| 482 | if (!is_zerofill) { | ||
| 483 | mem.copy(u8, atom.code.items, code); | ||
| 484 | } | ||
| 485 | |||
| 486 | try atom.aliases.ensureTotalCapacity(context.allocator, aliases.items.len); | ||
| 487 | for (aliases.items) |alias| { | ||
| 488 | atom.aliases.appendAssumeCapacity(alias.index); | ||
| 489 | const sym = &context.macho_file.locals.items[alias.index]; | ||
| 490 | sym.n_sect = @intCast(u8, context.macho_file.section_ordinals.getIndex(context.match).? + 1); | ||
| 491 | } | ||
| 492 | |||
| 493 | try atom.parseRelocs(self.relocs, .{ | ||
| 494 | .base_addr = self.section.addr, | ||
| 495 | .base_offset = start_addr, | ||
| 496 | .allocator = context.allocator, | ||
| 497 | .object = context.object, | ||
| 498 | .macho_file = context.macho_file, | ||
| 499 | }); | ||
| 500 | |||
| 501 | if (context.macho_file.has_dices) { | ||
| 502 | const dices = filterDice( | ||
| 503 | context.object.data_in_code_entries.items, | ||
| 504 | senior_nlist.nlist.n_value, | ||
| 505 | senior_nlist.nlist.n_value + size, | ||
| 506 | ); | ||
| 507 | try atom.dices.ensureTotalCapacity(context.allocator, dices.len); | ||
| 508 | |||
| 509 | for (dices) |dice| { | ||
| 510 | atom.dices.appendAssumeCapacity(.{ | ||
| 511 | .offset = dice.offset - try math.cast(u32, senior_nlist.nlist.n_value), | ||
| 512 | .length = dice.length, | ||
| 513 | .kind = dice.kind, | ||
| 514 | }); | ||
| 515 | } | ||
| 516 | } | ||
| 517 | |||
| 518 | self.index += 1; | ||
| 519 | |||
| 520 | return atom; | ||
| 521 | } | ||
| 522 | }; | ||
| 523 | |||
| 524 | pub fn parseIntoAtoms(self: *Object, allocator: *Allocator, macho_file: *MachO) !void { | 378 | pub fn parseIntoAtoms(self: *Object, allocator: *Allocator, macho_file: *MachO) !void { |
| 525 | const tracy = trace(@src()); | 379 | const tracy = trace(@src()); |
| 526 | defer tracy.end(); | 380 | defer tracy.end(); |
| ... | @@ -603,17 +457,10 @@ pub fn parseIntoAtoms(self: *Object, allocator: *Allocator, macho_file: *MachO) | ... | @@ -603,17 +457,10 @@ pub fn parseIntoAtoms(self: *Object, allocator: *Allocator, macho_file: *MachO) |
| 603 | // Since there is no symbol to refer to this atom, we create | 457 | // Since there is no symbol to refer to this atom, we create |
| 604 | // a temp one, unless we already did that when working out the relocations | 458 | // a temp one, unless we already did that when working out the relocations |
| 605 | // of other atoms. | 459 | // of other atoms. |
| 606 | const sym_name = try std.fmt.allocPrint(allocator, "l_{s}_{s}_{s}", .{ | ||
| 607 | self.name, | ||
| 608 | segmentName(sect), | ||
| 609 | sectionName(sect), | ||
| 610 | }); | ||
| 611 | defer allocator.free(sym_name); | ||
| 612 | |||
| 613 | const atom_local_sym_index = self.sections_as_symbols.get(sect_id) orelse blk: { | 460 | const atom_local_sym_index = self.sections_as_symbols.get(sect_id) orelse blk: { |
| 614 | const atom_local_sym_index = @intCast(u32, macho_file.locals.items.len); | 461 | const atom_local_sym_index = @intCast(u32, macho_file.locals.items.len); |
| 615 | try macho_file.locals.append(allocator, .{ | 462 | try macho_file.locals.append(allocator, .{ |
| 616 | .n_strx = try macho_file.makeString(sym_name), | 463 | .n_strx = 0, |
| 617 | .n_type = macho.N_SECT, | 464 | .n_type = macho.N_SECT, |
| 618 | .n_sect = @intCast(u8, macho_file.section_ordinals.getIndex(match).? + 1), | 465 | .n_sect = @intCast(u8, macho_file.section_ordinals.getIndex(match).? + 1), |
| 619 | .n_desc = 0, | 466 | .n_desc = 0, |