| ... | ... | @@ -9,13 +9,10 @@ const log = std.log.scoped(.object); |
| 9 | 9 | const macho = std.macho; |
| 10 | 10 | const math = std.math; |
| 11 | 11 | const mem = std.mem; |
| 12 | | const reloc = @import("reloc.zig"); |
| 13 | 12 | const sort = std.sort; |
| 14 | 13 | |
| 15 | 14 | const Allocator = mem.Allocator; |
| 16 | 15 | const Arch = std.Target.Cpu.Arch; |
| 17 | | const Relocation = reloc.Relocation; |
| 18 | | const Symbol = @import("Symbol.zig"); |
| 19 | 16 | const TextBlock = @import("TextBlock.zig"); |
| 20 | 17 | const Zld = @import("Zld.zig"); |
| 21 | 18 | |
| ... | ... | @@ -57,6 +54,8 @@ tu_comp_dir: ?[]const u8 = null, |
| 57 | 54 | mtime: ?u64 = null, |
| 58 | 55 | |
| 59 | 56 | text_blocks: std.ArrayListUnmanaged(*TextBlock) = .{}, |
| 57 | sections_as_symbols: std.AutoHashMapUnmanaged(u16, u32) = .{}, |
| 58 | symbol_mapping: std.AutoHashMapUnmanaged(u32, u32) = .{}, |
| 60 | 59 | |
| 61 | 60 | const DebugInfo = struct { |
| 62 | 61 | inner: dwarf.DwarfInfo, |
| ... | ... | @@ -163,6 +162,8 @@ pub fn deinit(self: *Object) void { |
| 163 | 162 | self.symtab.deinit(self.allocator); |
| 164 | 163 | self.strtab.deinit(self.allocator); |
| 165 | 164 | self.text_blocks.deinit(self.allocator); |
| 165 | self.sections_as_symbols.deinit(self.allocator); |
| 166 | self.symbol_mapping.deinit(self.allocator); |
| 166 | 167 | |
| 167 | 168 | if (self.debug_info) |*db| { |
| 168 | 169 | db.deinit(self.allocator); |
| ... | ... | @@ -372,20 +373,17 @@ const TextBlockParser = struct { |
| 372 | 373 | } |
| 373 | 374 | |
| 374 | 375 | const SeniorityContext = struct { |
| 375 | | zld: *Zld, |
| 376 | object: *Object, |
| 376 | 377 | }; |
| 377 | 378 | |
| 378 | 379 | fn lessThanBySeniority(context: SeniorityContext, lhs: NlistWithIndex, rhs: NlistWithIndex) bool { |
| 379 | | const lsym = context.zld.locals.items[lhs.index]; |
| 380 | | const rsym = context.zld.locals.items[rhs.index]; |
| 381 | | const lreg = lsym.payload.regular; |
| 382 | | const rreg = rsym.payload.regular; |
| 383 | | |
| 384 | | return switch (rreg.linkage) { |
| 385 | | .global => true, |
| 386 | | .linkage_unit => lreg.linkage == .translation_unit, |
| 387 | | else => lsym.isTemp(context.zld), |
| 388 | | }; |
| 380 | if (!Zld.symbolIsExt(rhs.nlist)) { |
| 381 | return Zld.symbolIsTemp(lhs.nlist, context.object.getString(lhs.nlist.n_strx)); |
| 382 | } else if (Zld.symbolIsPext(rhs.nlist) or Zld.symbolIsWeakDef(rhs.nlist)) { |
| 383 | return !Zld.symbolIsExt(lhs.nlist); |
| 384 | } else { |
| 385 | return true; |
| 386 | } |
| 389 | 387 | } |
| 390 | 388 | |
| 391 | 389 | pub fn next(self: *TextBlockParser) !?*TextBlock { |
| ... | ... | @@ -409,6 +407,7 @@ const TextBlockParser = struct { |
| 409 | 407 | } else null; |
| 410 | 408 | |
| 411 | 409 | for (aliases.items) |*nlist_with_index| { |
| 410 | nlist_with_index.index = self.symbol_mapping.get(nlist_with_index.index); |
| 412 | 411 | const sym = self.object.symbols.items[nlist_with_index.index]; |
| 413 | 412 | if (sym.payload != .regular) { |
| 414 | 413 | log.err("expected a regular symbol, found {s}", .{sym.payload}); |
| ... | ... | @@ -424,7 +423,7 @@ const TextBlockParser = struct { |
| 424 | 423 | sort.sort( |
| 425 | 424 | NlistWithIndex, |
| 426 | 425 | aliases.items, |
| 427 | | SeniorityContext{ .zld = self.zld }, |
| 426 | SeniorityContext{ .object = self.object }, |
| 428 | 427 | @This().lessThanBySeniority, |
| 429 | 428 | ); |
| 430 | 429 | } |
| ... | ... | @@ -515,7 +514,7 @@ const TextBlockParser = struct { |
| 515 | 514 | pub fn parseTextBlocks(self: *Object, zld: *Zld) !void { |
| 516 | 515 | const seg = self.load_commands.items[self.segment_cmd_index.?].Segment; |
| 517 | 516 | |
| 518 | | log.debug("analysing {s}", .{self.name.?}); |
| 517 | log.warn("analysing {s}", .{self.name.?}); |
| 519 | 518 | |
| 520 | 519 | const dysymtab = self.load_commands.items[self.dysymtab_cmd_index.?].Dysymtab; |
| 521 | 520 | // We only care about defined symbols, so filter every other out. |
| ... | ... | @@ -536,14 +535,14 @@ pub fn parseTextBlocks(self: *Object, zld: *Zld) !void { |
| 536 | 535 | |
| 537 | 536 | for (seg.sections.items) |sect, id| { |
| 538 | 537 | const sect_id = @intCast(u8, id); |
| 539 | | log.debug("putting section '{s},{s}' as a TextBlock", .{ |
| 538 | log.warn("putting section '{s},{s}' as a TextBlock", .{ |
| 540 | 539 | segmentName(sect), |
| 541 | 540 | sectionName(sect), |
| 542 | 541 | }); |
| 543 | 542 | |
| 544 | 543 | // Get matching segment/section in the final artifact. |
| 545 | 544 | const match = (try zld.getMatchingSection(sect)) orelse { |
| 546 | | log.debug("unhandled section", .{}); |
| 545 | log.warn("unhandled section", .{}); |
| 547 | 546 | continue; |
| 548 | 547 | }; |
| 549 | 548 | |
| ... | ... | @@ -577,200 +576,249 @@ pub fn parseTextBlocks(self: *Object, zld: *Zld) !void { |
| 577 | 576 | }; |
| 578 | 577 | zld.has_stabs = zld.has_stabs or self.debug_info != null; |
| 579 | 578 | |
| 580 | | next: { |
| 581 | | if (is_splittable) blocks: { |
| 582 | | if (filtered_nlists.len == 0) break :blocks; |
| 583 | | |
| 584 | | // If the first nlist does not match the start of the section, |
| 585 | | // then we need encapsulate the memory range [section start, first symbol) |
| 586 | | // as a temporary symbol and insert the matching TextBlock. |
| 587 | | const first_nlist = filtered_nlists[0].nlist; |
| 588 | | if (first_nlist.n_value > sect.addr) { |
| 589 | | const symbol = self.sections_as_symbols.get(sect_id) orelse symbol: { |
| 590 | | const name = try std.fmt.allocPrint(self.allocator, "l_{s}_{s}_{s}", .{ |
| 591 | | self.name.?, |
| 592 | | segmentName(sect), |
| 593 | | sectionName(sect), |
| 594 | | }); |
| 595 | | defer self.allocator.free(name); |
| 596 | | const symbol = try zld.allocator.create(Symbol); |
| 597 | | symbol.* = .{ |
| 598 | | .strx = try zld.makeString(name), |
| 599 | | .payload = .{ .undef = .{} }, |
| 600 | | }; |
| 601 | | try self.sections_as_symbols.putNoClobber(self.allocator, sect_id, symbol); |
| 602 | | break :symbol symbol; |
| 603 | | }; |
| 604 | | |
| 605 | | const local_sym_index = @intCast(u32, zld.locals.items.len); |
| 606 | | symbol.payload = .{ |
| 607 | | .regular = .{ |
| 608 | | .linkage = .translation_unit, |
| 609 | | .address = sect.addr, |
| 610 | | .segment_id = match.seg, |
| 611 | | .section_id = match.sect, |
| 612 | | .file = self, |
| 613 | | .local_sym_index = local_sym_index, |
| 614 | | }, |
| 615 | | }; |
| 616 | | try zld.locals.append(zld.allocator, symbol); |
| 617 | | |
| 618 | | const block_code = code[0 .. first_nlist.n_value - sect.addr]; |
| 619 | | const block_size = block_code.len; |
| 620 | | |
| 621 | | const block = try self.allocator.create(TextBlock); |
| 622 | | errdefer self.allocator.destroy(block); |
| 623 | | |
| 624 | | block.* = TextBlock.init(self.allocator); |
| 625 | | block.local_sym_index = local_sym_index; |
| 626 | | block.code = try self.allocator.dupe(u8, block_code); |
| 627 | | block.size = block_size; |
| 628 | | block.alignment = sect.@"align"; |
| 629 | | |
| 630 | | const block_relocs = filterRelocs(relocs, 0, block_size); |
| 631 | | if (block_relocs.len > 0) { |
| 632 | | try self.parseRelocs(zld, block_relocs, block, 0); |
| 633 | | } |
| 634 | | |
| 635 | | if (zld.has_dices) { |
| 636 | | const dices = filterDice(self.data_in_code_entries.items, sect.addr, sect.addr + block_size); |
| 637 | | try block.dices.ensureTotalCapacity(dices.len); |
| 638 | | |
| 639 | | for (dices) |dice| { |
| 640 | | block.dices.appendAssumeCapacity(.{ |
| 641 | | .offset = dice.offset - try math.cast(u32, sect.addr), |
| 642 | | .length = dice.length, |
| 643 | | .kind = dice.kind, |
| 644 | | }); |
| 645 | | } |
| 646 | | } |
| 647 | | |
| 648 | | // Update target section's metadata |
| 649 | | // TODO should we update segment's size here too? |
| 650 | | // How does it tie with incremental space allocs? |
| 651 | | const tseg = &zld.load_commands.items[match.seg].Segment; |
| 652 | | const tsect = &tseg.sections.items[match.sect]; |
| 653 | | const new_alignment = math.max(tsect.@"align", block.alignment); |
| 654 | | const new_alignment_pow_2 = try math.powi(u32, 2, new_alignment); |
| 655 | | const new_size = mem.alignForwardGeneric(u64, tsect.size, new_alignment_pow_2) + block.size; |
| 656 | | tsect.size = new_size; |
| 657 | | tsect.@"align" = new_alignment; |
| 658 | | |
| 659 | | if (zld.blocks.getPtr(match)) |last| { |
| 660 | | last.*.next = block; |
| 661 | | block.prev = last.*; |
| 662 | | last.* = block; |
| 663 | | } else { |
| 664 | | try zld.blocks.putNoClobber(zld.allocator, match, block); |
| 665 | | } |
| 666 | | |
| 667 | | try self.text_blocks.append(self.allocator, block); |
| 668 | | } |
| 669 | | |
| 670 | | var parser = TextBlockParser{ |
| 671 | | .allocator = self.allocator, |
| 672 | | .section = sect, |
| 673 | | .code = code, |
| 674 | | .relocs = relocs, |
| 675 | | .object = self, |
| 676 | | .zld = zld, |
| 677 | | .nlists = filtered_nlists, |
| 678 | | .match = match, |
| 679 | | }; |
| 680 | | |
| 681 | | while (try parser.next()) |block| { |
| 682 | | const sym = zld.locals.items[block.local_sym_index]; |
| 683 | | const reg = &sym.payload.regular; |
| 684 | | if (reg.file) |file| { |
| 685 | | if (file != self) { |
| 686 | | log.debug("deduping definition of {s} in {s}", .{ zld.getString(sym.strx), self.name.? }); |
| 687 | | block.deinit(); |
| 688 | | self.allocator.destroy(block); |
| 689 | | continue; |
| 690 | | } |
| 691 | | } |
| 692 | | |
| 693 | | if (reg.address == sect.addr) { |
| 694 | | if (self.sections_as_symbols.get(sect_id)) |alias| { |
| 695 | | // Add alias. |
| 696 | | const local_sym_index = @intCast(u32, zld.locals.items.len); |
| 697 | | const reg_alias = &alias.payload.regular; |
| 698 | | reg_alias.segment_id = match.seg; |
| 699 | | reg_alias.section_id = match.sect; |
| 700 | | reg_alias.local_sym_index = local_sym_index; |
| 701 | | try block.aliases.append(local_sym_index); |
| 702 | | try zld.locals.append(zld.allocator, alias); |
| 703 | | } |
| 704 | | } |
| 705 | | |
| 706 | | // Update target section's metadata |
| 707 | | // TODO should we update segment's size here too? |
| 708 | | // How does it tie with incremental space allocs? |
| 709 | | const tseg = &zld.load_commands.items[match.seg].Segment; |
| 710 | | const tsect = &tseg.sections.items[match.sect]; |
| 711 | | const new_alignment = math.max(tsect.@"align", block.alignment); |
| 712 | | const new_alignment_pow_2 = try math.powi(u32, 2, new_alignment); |
| 713 | | const new_size = mem.alignForwardGeneric(u64, tsect.size, new_alignment_pow_2) + block.size; |
| 714 | | tsect.size = new_size; |
| 715 | | tsect.@"align" = new_alignment; |
| 716 | | |
| 717 | | if (zld.blocks.getPtr(match)) |last| { |
| 718 | | last.*.next = block; |
| 719 | | block.prev = last.*; |
| 720 | | last.* = block; |
| 721 | | } else { |
| 722 | | try zld.blocks.putNoClobber(zld.allocator, match, block); |
| 723 | | } |
| 724 | | |
| 725 | | try self.text_blocks.append(self.allocator, block); |
| 726 | | } |
| 727 | | |
| 728 | | break :next; |
| 729 | | } |
| 579 | { |
| 580 | // next: { |
| 581 | // if (is_splittable) blocks: { |
| 582 | // if (filtered_nlists.len == 0) break :blocks; |
| 583 | |
| 584 | // // If the first nlist does not match the start of the section, |
| 585 | // // then we need encapsulate the memory range [section start, first symbol) |
| 586 | // // as a temporary symbol and insert the matching TextBlock. |
| 587 | // const first_nlist = filtered_nlists[0].nlist; |
| 588 | // if (first_nlist.n_value > sect.addr) { |
| 589 | // const symbol = self.sections_as_symbols.get(sect_id) orelse symbol: { |
| 590 | // const name = try std.fmt.allocPrint(self.allocator, "l_{s}_{s}_{s}", .{ |
| 591 | // self.name.?, |
| 592 | // segmentName(sect), |
| 593 | // sectionName(sect), |
| 594 | // }); |
| 595 | // defer self.allocator.free(name); |
| 596 | // const symbol = try zld.allocator.create(Symbol); |
| 597 | // symbol.* = .{ |
| 598 | // .strx = try zld.makeString(name), |
| 599 | // .payload = .{ .undef = .{} }, |
| 600 | // }; |
| 601 | // try self.sections_as_symbols.putNoClobber(self.allocator, sect_id, symbol); |
| 602 | // break :symbol symbol; |
| 603 | // }; |
| 604 | |
| 605 | // const local_sym_index = @intCast(u32, zld.locals.items.len); |
| 606 | // symbol.payload = .{ |
| 607 | // .regular = .{ |
| 608 | // .linkage = .translation_unit, |
| 609 | // .address = sect.addr, |
| 610 | // .segment_id = match.seg, |
| 611 | // .section_id = match.sect, |
| 612 | // .file = self, |
| 613 | // .local_sym_index = local_sym_index, |
| 614 | // }, |
| 615 | // }; |
| 616 | // try zld.locals.append(zld.allocator, symbol); |
| 617 | |
| 618 | // const block_code = code[0 .. first_nlist.n_value - sect.addr]; |
| 619 | // const block_size = block_code.len; |
| 620 | |
| 621 | // const block = try self.allocator.create(TextBlock); |
| 622 | // errdefer self.allocator.destroy(block); |
| 623 | |
| 624 | // block.* = TextBlock.init(self.allocator); |
| 625 | // block.local_sym_index = local_sym_index; |
| 626 | // block.code = try self.allocator.dupe(u8, block_code); |
| 627 | // block.size = block_size; |
| 628 | // block.alignment = sect.@"align"; |
| 629 | |
| 630 | // const block_relocs = filterRelocs(relocs, 0, block_size); |
| 631 | // if (block_relocs.len > 0) { |
| 632 | // try self.parseRelocs(zld, block_relocs, block, 0); |
| 633 | // } |
| 634 | |
| 635 | // if (zld.has_dices) { |
| 636 | // const dices = filterDice(self.data_in_code_entries.items, sect.addr, sect.addr + block_size); |
| 637 | // try block.dices.ensureTotalCapacity(dices.len); |
| 638 | |
| 639 | // for (dices) |dice| { |
| 640 | // block.dices.appendAssumeCapacity(.{ |
| 641 | // .offset = dice.offset - try math.cast(u32, sect.addr), |
| 642 | // .length = dice.length, |
| 643 | // .kind = dice.kind, |
| 644 | // }); |
| 645 | // } |
| 646 | // } |
| 647 | |
| 648 | // // Update target section's metadata |
| 649 | // // TODO should we update segment's size here too? |
| 650 | // // How does it tie with incremental space allocs? |
| 651 | // const tseg = &zld.load_commands.items[match.seg].Segment; |
| 652 | // const tsect = &tseg.sections.items[match.sect]; |
| 653 | // const new_alignment = math.max(tsect.@"align", block.alignment); |
| 654 | // const new_alignment_pow_2 = try math.powi(u32, 2, new_alignment); |
| 655 | // const new_size = mem.alignForwardGeneric(u64, tsect.size, new_alignment_pow_2) + block.size; |
| 656 | // tsect.size = new_size; |
| 657 | // tsect.@"align" = new_alignment; |
| 658 | |
| 659 | // if (zld.blocks.getPtr(match)) |last| { |
| 660 | // last.*.next = block; |
| 661 | // block.prev = last.*; |
| 662 | // last.* = block; |
| 663 | // } else { |
| 664 | // try zld.blocks.putNoClobber(zld.allocator, match, block); |
| 665 | // } |
| 666 | |
| 667 | // try self.text_blocks.append(self.allocator, block); |
| 668 | // } |
| 669 | |
| 670 | // var parser = TextBlockParser{ |
| 671 | // .allocator = self.allocator, |
| 672 | // .section = sect, |
| 673 | // .code = code, |
| 674 | // .relocs = relocs, |
| 675 | // .object = self, |
| 676 | // .zld = zld, |
| 677 | // .nlists = filtered_nlists, |
| 678 | // .match = match, |
| 679 | // }; |
| 680 | |
| 681 | // while (try parser.next()) |block| { |
| 682 | // const sym = zld.locals.items[block.local_sym_index]; |
| 683 | // const reg = &sym.payload.regular; |
| 684 | // if (reg.file) |file| { |
| 685 | // if (file != self) { |
| 686 | // log.debug("deduping definition of {s} in {s}", .{ zld.getString(sym.strx), self.name.? }); |
| 687 | // block.deinit(); |
| 688 | // self.allocator.destroy(block); |
| 689 | // continue; |
| 690 | // } |
| 691 | // } |
| 692 | |
| 693 | // if (reg.address == sect.addr) { |
| 694 | // if (self.sections_as_symbols.get(sect_id)) |alias| { |
| 695 | // // Add alias. |
| 696 | // const local_sym_index = @intCast(u32, zld.locals.items.len); |
| 697 | // const reg_alias = &alias.payload.regular; |
| 698 | // reg_alias.segment_id = match.seg; |
| 699 | // reg_alias.section_id = match.sect; |
| 700 | // reg_alias.local_sym_index = local_sym_index; |
| 701 | // try block.aliases.append(local_sym_index); |
| 702 | // try zld.locals.append(zld.allocator, alias); |
| 703 | // } |
| 704 | // } |
| 705 | |
| 706 | // // Update target section's metadata |
| 707 | // // TODO should we update segment's size here too? |
| 708 | // // How does it tie with incremental space allocs? |
| 709 | // const tseg = &zld.load_commands.items[match.seg].Segment; |
| 710 | // const tsect = &tseg.sections.items[match.sect]; |
| 711 | // const new_alignment = math.max(tsect.@"align", block.alignment); |
| 712 | // const new_alignment_pow_2 = try math.powi(u32, 2, new_alignment); |
| 713 | // const new_size = mem.alignForwardGeneric(u64, tsect.size, new_alignment_pow_2) + block.size; |
| 714 | // tsect.size = new_size; |
| 715 | // tsect.@"align" = new_alignment; |
| 716 | |
| 717 | // if (zld.blocks.getPtr(match)) |last| { |
| 718 | // last.*.next = block; |
| 719 | // block.prev = last.*; |
| 720 | // last.* = block; |
| 721 | // } else { |
| 722 | // try zld.blocks.putNoClobber(zld.allocator, match, block); |
| 723 | // } |
| 724 | |
| 725 | // try self.text_blocks.append(self.allocator, block); |
| 726 | // } |
| 727 | |
| 728 | // break :next; |
| 729 | // } |
| 730 | 730 | |
| 731 | 731 | // Since there is no symbol to refer to this block, we create |
| 732 | 732 | // a temp one, unless we already did that when working out the relocations |
| 733 | 733 | // of other text blocks. |
| 734 | | const symbol = self.sections_as_symbols.get(sect_id) orelse symbol: { |
| 735 | | const name = try std.fmt.allocPrint(self.allocator, "l_{s}_{s}_{s}", .{ |
| 736 | | self.name.?, |
| 737 | | segmentName(sect), |
| 738 | | sectionName(sect), |
| 739 | | }); |
| 740 | | defer self.allocator.free(name); |
| 741 | | const symbol = try zld.allocator.create(Symbol); |
| 742 | | symbol.* = .{ |
| 743 | | .strx = try zld.makeString(name), |
| 744 | | .payload = .{ .undef = .{} }, |
| 745 | | }; |
| 746 | | try self.sections_as_symbols.putNoClobber(self.allocator, sect_id, symbol); |
| 747 | | break :symbol symbol; |
| 748 | | }; |
| 749 | | |
| 750 | | const local_sym_index = @intCast(u32, zld.locals.items.len); |
| 751 | | symbol.payload = .{ |
| 752 | | .regular = .{ |
| 753 | | .linkage = .translation_unit, |
| 754 | | .address = sect.addr, |
| 755 | | .segment_id = match.seg, |
| 756 | | .section_id = match.sect, |
| 757 | | .file = self, |
| 758 | | .local_sym_index = local_sym_index, |
| 759 | | }, |
| 760 | | }; |
| 761 | | try zld.locals.append(zld.allocator, symbol); |
| 734 | const block_local_sym_index = @intCast(u32, zld.locals.items.len); |
| 735 | const sym_name = try std.fmt.allocPrint(self.allocator, "l_{s}_{s}_{s}", .{ |
| 736 | self.name.?, |
| 737 | segmentName(sect), |
| 738 | sectionName(sect), |
| 739 | }); |
| 740 | defer self.allocator.free(sym_name); |
| 741 | try zld.locals.append(zld.allocator, .{ |
| 742 | .n_strx = try zld.makeString(sym_name), |
| 743 | .n_type = macho.N_SECT, |
| 744 | .n_sect = zld.sectionId(match), |
| 745 | .n_desc = 0, |
| 746 | .n_value = sect.addr, |
| 747 | }); |
| 748 | const block_local = &zld.locals.items[block_local_sym_index]; |
| 749 | block_local.n_sect = zld.sectionId(match); |
| 762 | 750 | |
| 763 | 751 | const block = try self.allocator.create(TextBlock); |
| 764 | 752 | errdefer self.allocator.destroy(block); |
| 765 | 753 | |
| 766 | 754 | block.* = TextBlock.init(self.allocator); |
| 767 | | block.local_sym_index = local_sym_index; |
| 755 | block.local_sym_index = block_local_sym_index; |
| 768 | 756 | block.code = try self.allocator.dupe(u8, code); |
| 769 | 757 | block.size = sect.size; |
| 770 | 758 | block.alignment = sect.@"align"; |
| 771 | 759 | |
| 772 | | if (relocs.len > 0) { |
| 773 | | try self.parseRelocs(zld, relocs, block, 0); |
| 760 | try block.relocs.ensureTotalCapacity(relocs.len); |
| 761 | for (relocs) |rel| { |
| 762 | const out_rel: TextBlock.Relocation = outer: { |
| 763 | if (rel.r_extern == 0) { |
| 764 | const rel_sect_id = @intCast(u16, rel.r_symbolnum - 1); |
| 765 | const sect_sym_index = self.sections_as_symbols.get(rel_sect_id) orelse blk: { |
| 766 | const sect_sym_index = @intCast(u32, zld.locals.items.len); |
| 767 | const sect_sym_name = try std.fmt.allocPrint(self.allocator, "l_{s}_{s}_{s}", .{ |
| 768 | self.name.?, |
| 769 | segmentName(sect), |
| 770 | sectionName(sect), |
| 771 | }); |
| 772 | defer self.allocator.free(sect_sym_name); |
| 773 | try zld.locals.append(zld.allocator, .{ |
| 774 | .n_strx = try zld.makeString(sect_sym_name), |
| 775 | .n_type = macho.N_SECT, |
| 776 | .n_sect = 0, |
| 777 | .n_desc = 0, |
| 778 | .n_value = 0, |
| 779 | }); |
| 780 | try self.sections_as_symbols.putNoClobber(self.allocator, rel_sect_id, sect_sym_index); |
| 781 | break :blk sect_sym_index; |
| 782 | }; |
| 783 | break :outer .{ |
| 784 | .inner = rel, |
| 785 | .where = .local, |
| 786 | .where_index = sect_sym_index, |
| 787 | }; |
| 788 | } |
| 789 | |
| 790 | const rel_sym = self.symtab.items[rel.r_symbolnum]; |
| 791 | const rel_sym_name = self.getString(rel_sym.n_strx); |
| 792 | |
| 793 | if (Zld.symbolIsSect(rel_sym) and !Zld.symbolIsExt(rel_sym)) { |
| 794 | const where_index = self.symbol_mapping.get(rel.r_symbolnum) orelse unreachable; |
| 795 | break :outer .{ |
| 796 | .inner = rel, |
| 797 | .where = .local, |
| 798 | .where_index = where_index, |
| 799 | }; |
| 800 | } |
| 801 | |
| 802 | const resolv = zld.symbol_resolver.get(rel_sym_name) orelse unreachable; |
| 803 | switch (resolv.where) { |
| 804 | .global => { |
| 805 | break :outer .{ |
| 806 | .inner = rel, |
| 807 | .where = .local, |
| 808 | .where_index = resolv.local_sym_index, |
| 809 | }; |
| 810 | }, |
| 811 | .import => { |
| 812 | break :outer .{ |
| 813 | .inner = rel, |
| 814 | .where = .import, |
| 815 | .where_index = resolv.where_index, |
| 816 | }; |
| 817 | }, |
| 818 | else => unreachable, |
| 819 | } |
| 820 | }; |
| 821 | block.relocs.appendAssumeCapacity(out_rel); |
| 774 | 822 | } |
| 775 | 823 | |
| 776 | 824 | if (zld.has_dices) { |
| ... | ... | @@ -791,44 +839,41 @@ pub fn parseTextBlocks(self: *Object, zld: *Zld) !void { |
| 791 | 839 | // the filtered symbols and note which symbol is contained within so that |
| 792 | 840 | // we can properly allocate addresses down the line. |
| 793 | 841 | // While we're at it, we need to update segment,section mapping of each symbol too. |
| 794 | | if (filtered_nlists.len > 0) { |
| 795 | | var contained = std.ArrayList(TextBlock.SymbolAtOffset).init(self.allocator); |
| 796 | | defer contained.deinit(); |
| 797 | | try contained.ensureTotalCapacity(filtered_nlists.len); |
| 798 | | |
| 799 | | for (filtered_nlists) |nlist_with_index| { |
| 800 | | const sym = self.symbols.items[nlist_with_index.index]; |
| 801 | | assert(sym.payload == .regular); |
| 802 | | const reg = &sym.payload.regular; |
| 803 | | |
| 804 | | reg.segment_id = match.seg; |
| 805 | | reg.section_id = match.sect; |
| 806 | | |
| 807 | | const stab: ?TextBlock.Stab = if (self.debug_info) |di| blk: { |
| 808 | | // TODO there has to be a better to handle this. |
| 809 | | for (di.inner.func_list.items) |func| { |
| 810 | | if (func.pc_range) |range| { |
| 811 | | if (reg.address >= range.start and reg.address < range.end) { |
| 812 | | break :blk TextBlock.Stab{ |
| 813 | | .function = range.end - range.start, |
| 814 | | }; |
| 815 | | } |
| 842 | var contained = std.ArrayList(TextBlock.SymbolAtOffset).init(self.allocator); |
| 843 | defer contained.deinit(); |
| 844 | try contained.ensureTotalCapacity(filtered_nlists.len); |
| 845 | |
| 846 | for (filtered_nlists) |nlist_with_index| { |
| 847 | const nlist = nlist_with_index.nlist; |
| 848 | const local_sym_index = self.symbol_mapping.get(nlist_with_index.index) orelse unreachable; |
| 849 | const local = &zld.locals.items[local_sym_index]; |
| 850 | local.n_sect = zld.sectionId(match); |
| 851 | |
| 852 | const stab: ?TextBlock.Stab = if (self.debug_info) |di| blk: { |
| 853 | // TODO there has to be a better to handle this. |
| 854 | for (di.inner.func_list.items) |func| { |
| 855 | if (func.pc_range) |range| { |
| 856 | if (nlist.n_value >= range.start and nlist.n_value < range.end) { |
| 857 | break :blk TextBlock.Stab{ |
| 858 | .function = range.end - range.start, |
| 859 | }; |
| 816 | 860 | } |
| 817 | 861 | } |
| 818 | | if (zld.globals.contains(zld.getString(sym.strx))) break :blk .global; |
| 819 | | break :blk .static; |
| 820 | | } else null; |
| 821 | | |
| 822 | | contained.appendAssumeCapacity(.{ |
| 823 | | .local_sym_index = reg.local_sym_index, |
| 824 | | .offset = nlist_with_index.nlist.n_value - sect.addr, |
| 825 | | .stab = stab, |
| 826 | | }); |
| 827 | | } |
| 862 | } |
| 863 | // TODO |
| 864 | // if (zld.globals.contains(zld.getString(sym.strx))) break :blk .global; |
| 865 | break :blk .static; |
| 866 | } else null; |
| 828 | 867 | |
| 829 | | block.contained = contained.toOwnedSlice(); |
| 868 | contained.appendAssumeCapacity(.{ |
| 869 | .local_sym_index = local_sym_index, |
| 870 | .offset = nlist.n_value - sect.addr, |
| 871 | .stab = stab, |
| 872 | }); |
| 830 | 873 | } |
| 831 | 874 | |
| 875 | block.contained = contained.toOwnedSlice(); |
| 876 | |
| 832 | 877 | // Update target section's metadata |
| 833 | 878 | // TODO should we update segment's size here too? |
| 834 | 879 | // How does it tie with incremental space allocs? |
| ... | ... | @@ -853,26 +898,6 @@ pub fn parseTextBlocks(self: *Object, zld: *Zld) !void { |
| 853 | 898 | } |
| 854 | 899 | } |
| 855 | 900 | |
| 856 | | fn parseRelocs( |
| 857 | | self: *Object, |
| 858 | | zld: *Zld, |
| 859 | | relocs: []const macho.relocation_info, |
| 860 | | block: *TextBlock, |
| 861 | | base_addr: u64, |
| 862 | | ) !void { |
| 863 | | var it = reloc.RelocIterator{ |
| 864 | | .buffer = relocs, |
| 865 | | }; |
| 866 | | var parser = reloc.Parser{ |
| 867 | | .object = self, |
| 868 | | .zld = zld, |
| 869 | | .it = &it, |
| 870 | | .block = block, |
| 871 | | .base_addr = base_addr, |
| 872 | | }; |
| 873 | | try parser.parse(); |
| 874 | | } |
| 875 | | |
| 876 | 901 | pub fn symbolFromReloc(self: *Object, zld: *Zld, rel: macho.relocation_info) !*Symbol { |
| 877 | 902 | const symbol = blk: { |
| 878 | 903 | if (rel.r_extern == 1) { |