authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-03-08 17:21:25+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-03-17 19:59:13+01:00
log349f878ecf0b6ad5eee6b1cdfdba90014cbcb619
tree20a4616ade9191ad714c0804a12f248ecc5b3ebb
parent62f43fbc068ae63b6492af20a7cfd38b7426cb92

zld: mimick Apple and add __DATA_CONST seg


1 files changed, 176 insertions(+), 153 deletions(-)

src/link/MachO/Zld.zig+176-153
......@@ -37,6 +37,7 @@ load_commands: std.ArrayListUnmanaged(LoadCommand) = .{},
3737
3838pagezero_segment_cmd_index: ?u16 = null,
3939text_segment_cmd_index: ?u16 = null,
40data_const_segment_cmd_index: ?u16 = null,
4041data_segment_cmd_index: ?u16 = null,
4142linkedit_segment_cmd_index: ?u16 = null,
4243dyld_info_cmd_index: ?u16 = null,
......@@ -277,6 +278,7 @@ pub fn link(self: *Zld, files: []const []const u8, out_path: []const u8) !void {
277278 try self.sortSections();
278279 try self.resolveImports();
279280 try self.allocateTextSegment();
281 try self.allocateDataConstSegment();
280282 try self.allocateDataSegment();
281283 self.allocateLinkeditSegment();
282284 try self.writeStubHelperCommon();
......@@ -362,6 +364,7 @@ fn updateMetadata(self: *Zld, object_id: u16) !void {
362364 const object = self.objects.items[object_id];
363365 const object_seg = object.load_commands.items[object.segment_cmd_index.?].Segment;
364366 const text_seg = &self.load_commands.items[self.text_segment_cmd_index.?].Segment;
367 const data_const_seg = &self.load_commands.items[self.data_const_segment_cmd_index.?].Segment;
365368 const data_seg = &self.load_commands.items[self.data_segment_cmd_index.?].Segment;
366369
367370 // Create missing metadata
......@@ -395,10 +398,10 @@ fn updateMetadata(self: *Zld, object_id: u16) !void {
395398 if (!mem.eql(u8, sectname, "__const")) continue;
396399 if (self.data_const_section_index != null) continue;
397400
398 self.data_const_section_index = @intCast(u16, data_seg.sections.items.len);
399 try data_seg.addSection(self.allocator, .{
401 self.data_const_section_index = @intCast(u16, data_const_seg.sections.items.len);
402 try data_const_seg.addSection(self.allocator, .{
400403 .sectname = makeStaticString("__const"),
401 .segname = makeStaticString("__DATA"),
404 .segname = makeStaticString("__DATA_CONST"),
402405 .addr = 0,
403406 .size = 0,
404407 .offset = 0,
......@@ -611,7 +614,7 @@ fn getMatchingSection(self: *Zld, section: macho.section_64) ?MatchingSection {
611614 };
612615 } else if (mem.eql(u8, sectname, "__const")) {
613616 break :blk .{
614 .seg = self.data_segment_cmd_index.?,
617 .seg = self.data_const_segment_cmd_index.?,
615618 .sect = self.data_const_section_index.?,
616619 };
617620 }
......@@ -627,100 +630,85 @@ fn getMatchingSection(self: *Zld, section: macho.section_64) ?MatchingSection {
627630}
628631
629632fn sortSections(self: *Zld) !void {
630 const text_seg = &self.load_commands.items[self.text_segment_cmd_index.?].Segment;
631 var text_sections = text_seg.sections.toOwnedSlice(self.allocator);
632 defer self.allocator.free(text_sections);
633 try text_seg.sections.ensureCapacity(self.allocator, text_sections.len);
634
635 const data_seg = &self.load_commands.items[self.data_segment_cmd_index.?].Segment;
636 var data_sections = data_seg.sections.toOwnedSlice(self.allocator);
637 defer self.allocator.free(data_sections);
638 try data_seg.sections.ensureCapacity(self.allocator, data_sections.len);
639
640633 var text_index_mapping = std.AutoHashMap(u16, u16).init(self.allocator);
641634 defer text_index_mapping.deinit();
642
635 var data_const_index_mapping = std.AutoHashMap(u16, u16).init(self.allocator);
636 defer data_const_index_mapping.deinit();
643637 var data_index_mapping = std.AutoHashMap(u16, u16).init(self.allocator);
644638 defer data_index_mapping.deinit();
645639
646 if (self.text_section_index) |index| {
647 const new_index = @intCast(u16, text_seg.sections.items.len);
648 self.text_section_index = new_index;
649 text_seg.sections.appendAssumeCapacity(text_sections[index]);
650 try text_index_mapping.putNoClobber(index, new_index);
651 }
652 if (self.stubs_section_index) |index| {
653 const new_index = @intCast(u16, text_seg.sections.items.len);
654 self.stubs_section_index = new_index;
655 text_seg.sections.appendAssumeCapacity(text_sections[index]);
656 try text_index_mapping.putNoClobber(index, new_index);
657 }
658 if (self.stub_helper_section_index) |index| {
659 const new_index = @intCast(u16, text_seg.sections.items.len);
660 self.stub_helper_section_index = new_index;
661 text_seg.sections.appendAssumeCapacity(text_sections[index]);
662 try text_index_mapping.putNoClobber(index, new_index);
663 }
664 if (self.text_const_section_index) |index| {
665 const new_index = @intCast(u16, text_seg.sections.items.len);
666 self.text_const_section_index = new_index;
667 text_seg.sections.appendAssumeCapacity(text_sections[index]);
668 try text_index_mapping.putNoClobber(index, new_index);
669 }
670 if (self.cstring_section_index) |index| {
671 const new_index = @intCast(u16, text_seg.sections.items.len);
672 self.cstring_section_index = new_index;
673 text_seg.sections.appendAssumeCapacity(text_sections[index]);
674 try text_index_mapping.putNoClobber(index, new_index);
675 }
676
677 if (self.got_section_index) |index| {
678 const new_index = @intCast(u16, data_seg.sections.items.len);
679 self.got_section_index = new_index;
680 data_seg.sections.appendAssumeCapacity(data_sections[index]);
681 try data_index_mapping.putNoClobber(index, new_index);
682 }
683 if (self.data_const_section_index) |index| {
684 const new_index = @intCast(u16, data_seg.sections.items.len);
685 self.data_const_section_index = new_index;
686 data_seg.sections.appendAssumeCapacity(data_sections[index]);
687 try data_index_mapping.putNoClobber(index, new_index);
688 }
689 if (self.la_symbol_ptr_section_index) |index| {
690 const new_index = @intCast(u16, data_seg.sections.items.len);
691 self.la_symbol_ptr_section_index = new_index;
692 data_seg.sections.appendAssumeCapacity(data_sections[index]);
693 try data_index_mapping.putNoClobber(index, new_index);
694 }
695 if (self.tlv_section_index) |index| {
696 const new_index = @intCast(u16, data_seg.sections.items.len);
697 self.tlv_section_index = new_index;
698 data_seg.sections.appendAssumeCapacity(data_sections[index]);
699 try data_index_mapping.putNoClobber(index, new_index);
700 }
701 if (self.data_section_index) |index| {
702 const new_index = @intCast(u16, data_seg.sections.items.len);
703 self.data_section_index = new_index;
704 data_seg.sections.appendAssumeCapacity(data_sections[index]);
705 try data_index_mapping.putNoClobber(index, new_index);
706 }
707 if (self.tlv_data_section_index) |index| {
708 const new_index = @intCast(u16, data_seg.sections.items.len);
709 self.tlv_data_section_index = new_index;
710 data_seg.sections.appendAssumeCapacity(data_sections[index]);
711 try data_index_mapping.putNoClobber(index, new_index);
640 {
641 // __TEXT segment
642 const seg = &self.load_commands.items[self.text_segment_cmd_index.?].Segment;
643 var sections = seg.sections.toOwnedSlice(self.allocator);
644 defer self.allocator.free(sections);
645 try seg.sections.ensureCapacity(self.allocator, sections.len);
646
647 const indices = &[_]*?u16{
648 &self.text_section_index,
649 &self.stubs_section_index,
650 &self.stub_helper_section_index,
651 &self.text_const_section_index,
652 &self.cstring_section_index,
653 };
654 for (indices) |maybe_index| {
655 const new_index: u16 = if (maybe_index.*) |index| blk: {
656 const idx = @intCast(u16, seg.sections.items.len);
657 seg.sections.appendAssumeCapacity(sections[index]);
658 try text_index_mapping.putNoClobber(index, idx);
659 break :blk idx;
660 } else continue;
661 maybe_index.* = new_index;
662 }
712663 }
713 if (self.tlv_bss_section_index) |index| {
714 const new_index = @intCast(u16, data_seg.sections.items.len);
715 self.tlv_bss_section_index = new_index;
716 data_seg.sections.appendAssumeCapacity(data_sections[index]);
717 try data_index_mapping.putNoClobber(index, new_index);
664
665 {
666 // __DATA_CONST segment
667 const seg = &self.load_commands.items[self.data_const_segment_cmd_index.?].Segment;
668 var sections = seg.sections.toOwnedSlice(self.allocator);
669 defer self.allocator.free(sections);
670 try seg.sections.ensureCapacity(self.allocator, sections.len);
671
672 const indices = &[_]*?u16{
673 &self.got_section_index,
674 &self.data_const_section_index,
675 };
676 for (indices) |maybe_index| {
677 const new_index: u16 = if (maybe_index.*) |index| blk: {
678 const idx = @intCast(u16, seg.sections.items.len);
679 seg.sections.appendAssumeCapacity(sections[index]);
680 try data_const_index_mapping.putNoClobber(index, idx);
681 break :blk idx;
682 } else continue;
683 maybe_index.* = new_index;
684 }
718685 }
719 if (self.bss_section_index) |index| {
720 const new_index = @intCast(u16, data_seg.sections.items.len);
721 self.bss_section_index = new_index;
722 data_seg.sections.appendAssumeCapacity(data_sections[index]);
723 try data_index_mapping.putNoClobber(index, new_index);
686
687 {
688 // __DATA segment
689 const seg = &self.load_commands.items[self.data_segment_cmd_index.?].Segment;
690 var sections = seg.sections.toOwnedSlice(self.allocator);
691 defer self.allocator.free(sections);
692 try seg.sections.ensureCapacity(self.allocator, sections.len);
693
694 // __DATA segment
695 const indices = &[_]*?u16{
696 &self.la_symbol_ptr_section_index,
697 &self.tlv_section_index,
698 &self.data_section_index,
699 &self.tlv_data_section_index,
700 &self.tlv_bss_section_index,
701 &self.bss_section_index,
702 };
703 for (indices) |maybe_index| {
704 const new_index: u16 = if (maybe_index.*) |index| blk: {
705 const idx = @intCast(u16, seg.sections.items.len);
706 seg.sections.appendAssumeCapacity(sections[index]);
707 try data_index_mapping.putNoClobber(index, idx);
708 break :blk idx;
709 } else continue;
710 maybe_index.* = new_index;
711 }
724712 }
725713
726714 var it = self.mappings.iterator();
......@@ -729,6 +717,9 @@ fn sortSections(self: *Zld) !void {
729717 if (self.text_segment_cmd_index.? == mapping.target_seg_id) {
730718 const new_index = text_index_mapping.get(mapping.target_sect_id) orelse unreachable;
731719 mapping.target_sect_id = new_index;
720 } else if (self.data_const_segment_cmd_index.? == mapping.target_seg_id) {
721 const new_index = data_const_index_mapping.get(mapping.target_sect_id) orelse unreachable;
722 mapping.target_sect_id = new_index;
732723 } else if (self.data_segment_cmd_index.? == mapping.target_seg_id) {
733724 const new_index = data_index_mapping.get(mapping.target_sect_id) orelse unreachable;
734725 mapping.target_sect_id = new_index;
......@@ -874,10 +865,9 @@ fn allocateTextSegment(self: *Zld) !void {
874865 }
875866}
876867
877fn allocateDataSegment(self: *Zld) !void {
878 const seg = &self.load_commands.items[self.data_segment_cmd_index.?].Segment;
868fn allocateDataConstSegment(self: *Zld) !void {
869 const seg = &self.load_commands.items[self.data_const_segment_cmd_index.?].Segment;
879870 const nonlazy = @intCast(u32, self.nonlazy_imports.items().len);
880 const lazy = @intCast(u32, self.lazy_imports.items().len);
881871
882872 const text_seg = self.load_commands.items[self.text_segment_cmd_index.?].Segment;
883873 seg.inner.fileoff = text_seg.inner.fileoff + text_seg.inner.filesize;
......@@ -887,6 +877,17 @@ fn allocateDataSegment(self: *Zld) !void {
887877 const got = &seg.sections.items[self.got_section_index.?];
888878 got.size += nonlazy * @sizeOf(u64);
889879
880 try self.allocateSegment(self.data_const_segment_cmd_index.?, 0);
881}
882
883fn allocateDataSegment(self: *Zld) !void {
884 const seg = &self.load_commands.items[self.data_segment_cmd_index.?].Segment;
885 const lazy = @intCast(u32, self.lazy_imports.items().len);
886
887 const data_const_seg = self.load_commands.items[self.data_const_segment_cmd_index.?].Segment;
888 seg.inner.fileoff = data_const_seg.inner.fileoff + data_const_seg.inner.filesize;
889 seg.inner.vmaddr = data_const_seg.inner.vmaddr + data_const_seg.inner.vmsize;
890
890891 // Set la_symbol_ptr and data size
891892 const la_symbol_ptr = &seg.sections.items[self.la_symbol_ptr_section_index.?];
892893 const data = &seg.sections.items[self.data_section_index.?];
......@@ -926,10 +927,11 @@ fn allocateSegment(self: *Zld, index: u16, offset: u64) !void {
926927fn writeStubHelperCommon(self: *Zld) !void {
927928 const text_segment = &self.load_commands.items[self.text_segment_cmd_index.?].Segment;
928929 const stub_helper = &text_segment.sections.items[self.stub_helper_section_index.?];
930 const data_const_segment = &self.load_commands.items[self.data_const_segment_cmd_index.?].Segment;
931 const got = &data_const_segment.sections.items[self.got_section_index.?];
929932 const data_segment = &self.load_commands.items[self.data_segment_cmd_index.?].Segment;
930933 const data = &data_segment.sections.items[self.data_section_index.?];
931934 const la_symbol_ptr = data_segment.sections.items[self.la_symbol_ptr_section_index.?];
932 const got = &data_segment.sections.items[self.got_section_index.?];
933935
934936 self.stub_helper_stubs_start_off = blk: {
935937 switch (self.arch.?) {
......@@ -1247,15 +1249,16 @@ fn resolveSymbols(self: *Zld) !void {
12471249
12481250 log.debug("resolving '{s}':{} as {s} symbol at 0x{x}", .{ sym_name, sym, tt, n_value });
12491251
1250 // TODO this assumes only two symbol-filled segments. Also, there might be a more
1251 // generic way of doing this.
1252 const n_sect = blk: {
1253 if (self.text_segment_cmd_index.? == target_mapping.target_seg_id) {
1254 break :blk target_mapping.target_sect_id + 1;
1252 // TODO there might be a more generic way of doing this.
1253 var n_sect: u16 = 0;
1254 for (self.load_commands.items) |cmd, cmd_id| {
1255 if (cmd != .Segment) break;
1256 if (cmd_id == target_mapping.target_seg_id) {
1257 n_sect += target_mapping.target_sect_id + 1;
1258 break;
12551259 }
1256 const prev_seg = self.load_commands.items[self.text_segment_cmd_index.?].Segment;
1257 break :blk @intCast(u16, prev_seg.sections.items.len + target_mapping.target_sect_id + 1);
1258 };
1260 n_sect += @intCast(u16, cmd.Segment.sections.items.len);
1261 }
12591262
12601263 const n_strx = try self.makeString(sym_name);
12611264 try locs.entry.value.append(self.allocator, .{
......@@ -1460,23 +1463,24 @@ fn doRelocs(self: *Zld) !void {
14601463 mem.writeIntLittle(u64, inst, @bitCast(u64, result));
14611464 sub = null;
14621465
1463 // TODO should handle this better.
14641466 outer: {
14651467 var hit: bool = false;
1466 if (self.data_section_index) |index| inner: {
1467 if (index != target_mapping.target_sect_id) break :inner;
1468 hit = true;
1468 if (target_mapping.target_seg_id == self.data_segment_cmd_index.?) {
1469 if (self.data_section_index) |index| {
1470 if (index == target_mapping.target_sect_id) hit = true;
1471 }
14691472 }
1470 if (self.data_const_section_index) |index| inner: {
1471 if (index != target_mapping.target_sect_id) break :inner;
1472 hit = true;
1473 if (target_mapping.target_seg_id == self.data_const_segment_cmd_index.?) {
1474 if (self.data_const_section_index) |index| {
1475 if (index == target_mapping.target_sect_id) hit = true;
1476 }
14731477 }
1478
14741479 if (!hit) break :outer;
1475 const this_seg = self.load_commands.items[self.data_segment_cmd_index.?].Segment;
1476 const this_offset = target_sect_addr + off - this_seg.inner.vmaddr;
1480
14771481 try self.local_rebases.append(self.allocator, .{
1478 .offset = this_offset,
1479 .segment_id = @intCast(u16, self.data_segment_cmd_index.?),
1482 .offset = this_addr - target_seg.inner.vmaddr,
1483 .segment_id = target_mapping.target_seg_id,
14801484 });
14811485 }
14821486 },
......@@ -1642,23 +1646,24 @@ fn doRelocs(self: *Zld) !void {
16421646 mem.writeIntLittle(u64, inst, @bitCast(u64, result));
16431647 sub = null;
16441648
1645 // TODO should handle this better.
16461649 outer: {
16471650 var hit: bool = false;
1648 if (self.data_section_index) |index| inner: {
1649 if (index != target_mapping.target_sect_id) break :inner;
1650 hit = true;
1651 if (target_mapping.target_seg_id == self.data_segment_cmd_index.?) {
1652 if (self.data_section_index) |index| {
1653 if (index == target_mapping.target_sect_id) hit = true;
1654 }
16511655 }
1652 if (self.data_const_section_index) |index| inner: {
1653 if (index != target_mapping.target_sect_id) break :inner;
1654 hit = true;
1656 if (target_mapping.target_seg_id == self.data_const_segment_cmd_index.?) {
1657 if (self.data_const_section_index) |index| {
1658 if (index == target_mapping.target_sect_id) hit = true;
1659 }
16551660 }
1661
16561662 if (!hit) break :outer;
1657 const this_seg = self.load_commands.items[self.data_segment_cmd_index.?].Segment;
1658 const this_offset = target_sect_addr + off - this_seg.inner.vmaddr;
1663
16591664 try self.local_rebases.append(self.allocator, .{
1660 .offset = this_offset,
1661 .segment_id = @intCast(u16, self.data_segment_cmd_index.?),
1665 .offset = this_addr - target_seg.inner.vmaddr,
1666 .segment_id = target_mapping.target_seg_id,
16621667 });
16631668 }
16641669 },
......@@ -1763,7 +1768,7 @@ fn relocTargetAddr(self: *Zld, object_id: u16, rel: macho.relocation_info) !u64
17631768 const stubs = segment.sections.items[self.stubs_section_index.?];
17641769 break :blk stubs.addr + ext.index * stubs.reserved2;
17651770 } else if (self.nonlazy_imports.get(sym_name)) |ext| {
1766 const segment = self.load_commands.items[self.data_segment_cmd_index.?].Segment;
1771 const segment = self.load_commands.items[self.data_const_segment_cmd_index.?].Segment;
17671772 const got = segment.sections.items[self.got_section_index.?];
17681773 break :blk got.addr + ext.index * @sizeOf(u64);
17691774 } else if (self.threadlocal_imports.get(sym_name)) |ext| {
......@@ -1916,13 +1921,13 @@ fn populateMetadata(self: *Zld) !void {
19161921 });
19171922 }
19181923
1919 if (self.data_segment_cmd_index == null) {
1920 self.data_segment_cmd_index = @intCast(u16, self.load_commands.items.len);
1924 if (self.data_const_segment_cmd_index == null) {
1925 self.data_const_segment_cmd_index = @intCast(u16, self.load_commands.items.len);
19211926 try self.load_commands.append(self.allocator, .{
19221927 .Segment = SegmentCommand.empty(.{
19231928 .cmd = macho.LC_SEGMENT_64,
19241929 .cmdsize = @sizeOf(macho.segment_command_64),
1925 .segname = makeStaticString("__DATA"),
1930 .segname = makeStaticString("__DATA_CONST"),
19261931 .vmaddr = 0,
19271932 .vmsize = 0,
19281933 .fileoff = 0,
......@@ -1936,11 +1941,11 @@ fn populateMetadata(self: *Zld) !void {
19361941 }
19371942
19381943 if (self.got_section_index == null) {
1939 const data_seg = &self.load_commands.items[self.data_segment_cmd_index.?].Segment;
1940 self.got_section_index = @intCast(u16, data_seg.sections.items.len);
1941 try data_seg.addSection(self.allocator, .{
1944 const data_const_seg = &self.load_commands.items[self.data_const_segment_cmd_index.?].Segment;
1945 self.got_section_index = @intCast(u16, data_const_seg.sections.items.len);
1946 try data_const_seg.addSection(self.allocator, .{
19421947 .sectname = makeStaticString("__got"),
1943 .segname = makeStaticString("__DATA"),
1948 .segname = makeStaticString("__DATA_CONST"),
19441949 .addr = 0,
19451950 .size = 0,
19461951 .offset = 0,
......@@ -1954,6 +1959,25 @@ fn populateMetadata(self: *Zld) !void {
19541959 });
19551960 }
19561961
1962 if (self.data_segment_cmd_index == null) {
1963 self.data_segment_cmd_index = @intCast(u16, self.load_commands.items.len);
1964 try self.load_commands.append(self.allocator, .{
1965 .Segment = SegmentCommand.empty(.{
1966 .cmd = macho.LC_SEGMENT_64,
1967 .cmdsize = @sizeOf(macho.segment_command_64),
1968 .segname = makeStaticString("__DATA"),
1969 .vmaddr = 0,
1970 .vmsize = 0,
1971 .fileoff = 0,
1972 .filesize = 0,
1973 .maxprot = macho.VM_PROT_READ | macho.VM_PROT_WRITE,
1974 .initprot = macho.VM_PROT_READ | macho.VM_PROT_WRITE,
1975 .nsects = 0,
1976 .flags = 0,
1977 }),
1978 });
1979 }
1980
19571981 if (self.la_symbol_ptr_section_index == null) {
19581982 const data_seg = &self.load_commands.items[self.data_segment_cmd_index.?].Segment;
19591983 self.la_symbol_ptr_section_index = @intCast(u16, data_seg.sections.items.len);
......@@ -2269,15 +2293,17 @@ fn setEntryPoint(self: *Zld) !void {
22692293}
22702294
22712295fn writeRebaseInfoTable(self: *Zld) !void {
2272 const data_seg = self.load_commands.items[self.data_segment_cmd_index.?].Segment;
2273
22742296 var pointers = std.ArrayList(Pointer).init(self.allocator);
22752297 defer pointers.deinit();
2276 try pointers.ensureCapacity(self.lazy_imports.items().len);
2298
2299 try pointers.ensureCapacity(pointers.items.len + self.local_rebases.items.len);
2300 pointers.appendSliceAssumeCapacity(self.local_rebases.items);
22772301
22782302 if (self.la_symbol_ptr_section_index) |idx| {
2279 const sect = data_seg.sections.items[idx];
2280 const base_offset = sect.addr - data_seg.inner.vmaddr;
2303 try pointers.ensureCapacity(pointers.items.len + self.lazy_imports.items().len);
2304 const seg = self.load_commands.items[self.data_segment_cmd_index.?].Segment;
2305 const sect = seg.sections.items[idx];
2306 const base_offset = sect.addr - seg.inner.vmaddr;
22812307 const segment_id = @intCast(u16, self.data_segment_cmd_index.?);
22822308 for (self.lazy_imports.items()) |entry| {
22832309 pointers.appendAssumeCapacity(.{
......@@ -2287,9 +2313,6 @@ fn writeRebaseInfoTable(self: *Zld) !void {
22872313 }
22882314 }
22892315
2290 try pointers.ensureCapacity(pointers.items.len + self.local_rebases.items.len);
2291 pointers.appendSliceAssumeCapacity(self.local_rebases.items);
2292
22932316 std.sort.sort(Pointer, pointers.items, {}, pointerCmp);
22942317
22952318 const size = try rebaseInfoSize(pointers.items);
......@@ -2319,16 +2342,15 @@ fn pointerCmp(context: void, a: Pointer, b: Pointer) bool {
23192342}
23202343
23212344fn writeBindInfoTable(self: *Zld) !void {
2322 const data_seg = self.load_commands.items[self.data_segment_cmd_index.?].Segment;
2323
23242345 var pointers = std.ArrayList(Pointer).init(self.allocator);
23252346 defer pointers.deinit();
23262347 try pointers.ensureCapacity(self.nonlazy_imports.items().len + self.threadlocal_imports.items().len);
23272348
23282349 if (self.got_section_index) |idx| {
2329 const sect = data_seg.sections.items[idx];
2330 const base_offset = sect.addr - data_seg.inner.vmaddr;
2331 const segment_id = @intCast(u16, self.data_segment_cmd_index.?);
2350 const seg = self.load_commands.items[self.data_const_segment_cmd_index.?].Segment;
2351 const sect = seg.sections.items[idx];
2352 const base_offset = sect.addr - seg.inner.vmaddr;
2353 const segment_id = @intCast(u16, self.data_const_segment_cmd_index.?);
23322354 for (self.nonlazy_imports.items()) |entry| {
23332355 pointers.appendAssumeCapacity(.{
23342356 .offset = base_offset + entry.value.index * @sizeOf(u64),
......@@ -2340,8 +2362,9 @@ fn writeBindInfoTable(self: *Zld) !void {
23402362 }
23412363
23422364 if (self.tlv_section_index) |idx| {
2343 const sect = data_seg.sections.items[idx];
2344 const base_offset = sect.addr - data_seg.inner.vmaddr;
2365 const seg = self.load_commands.items[self.data_segment_cmd_index.?].Segment;
2366 const sect = seg.sections.items[idx];
2367 const base_offset = sect.addr - seg.inner.vmaddr;
23452368 const segment_id = @intCast(u16, self.data_segment_cmd_index.?);
23462369 for (self.threadlocal_imports.items()) |entry| {
23472370 pointers.appendAssumeCapacity(.{
......@@ -2372,15 +2395,14 @@ fn writeBindInfoTable(self: *Zld) !void {
23722395}
23732396
23742397fn writeLazyBindInfoTable(self: *Zld) !void {
2375 const data_seg = self.load_commands.items[self.data_segment_cmd_index.?].Segment;
2376
23772398 var pointers = std.ArrayList(Pointer).init(self.allocator);
23782399 defer pointers.deinit();
23792400 try pointers.ensureCapacity(self.lazy_imports.items().len);
23802401
23812402 if (self.la_symbol_ptr_section_index) |idx| {
2382 const sect = data_seg.sections.items[idx];
2383 const base_offset = sect.addr - data_seg.inner.vmaddr;
2403 const seg = self.load_commands.items[self.data_segment_cmd_index.?].Segment;
2404 const sect = seg.sections.items[idx];
2405 const base_offset = sect.addr - seg.inner.vmaddr;
23842406 const segment_id = @intCast(u16, self.data_segment_cmd_index.?);
23852407 for (self.lazy_imports.items()) |entry| {
23862408 pointers.appendAssumeCapacity(.{
......@@ -2725,8 +2747,9 @@ fn writeDynamicSymbolTable(self: *Zld) !void {
27252747 const seg = &self.load_commands.items[self.linkedit_segment_cmd_index.?].Segment;
27262748 const text_segment = &self.load_commands.items[self.text_segment_cmd_index.?].Segment;
27272749 const stubs = &text_segment.sections.items[self.stubs_section_index.?];
2750 const data_const_segment = &self.load_commands.items[self.data_const_segment_cmd_index.?].Segment;
2751 const got = &data_const_segment.sections.items[self.got_section_index.?];
27282752 const data_segment = &self.load_commands.items[self.data_segment_cmd_index.?].Segment;
2729 const got = &data_segment.sections.items[self.got_section_index.?];
27302753 const la_symbol_ptr = &data_segment.sections.items[self.la_symbol_ptr_section_index.?];
27312754 const dysymtab = &self.load_commands.items[self.dysymtab_cmd_index.?].Dysymtab;
27322755