authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-06-11 23:06:43+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-06-11 23:06:43+02:00
logac587744e30817df86b9c759b307d41fe7951832
tree381dfc50274b71d2071c115e28e4a1cf527c6810
parentb93fd4bb73506a8feb0b4c312c1afbf88cc9248d

zld: allow for existence of __DATA_CONST segments in objects

Up until now, we only expected old-fashioned objects which carried two basic segments by name: __TEXT and __DATA. Since macOS 11.1, there is a new segment __DATA_CONST, and we should expect and correctly parse sections designated to that segment explicitly as is the case in golang.

1 files changed, 12 insertions(+), 9 deletions(-)

src/link/MachO/Zld.zig+12-9
......@@ -481,8 +481,7 @@ fn updateMetadata(self: *Zld) !void {
481481 .reserved3 = 0,
482482 });
483483 }
484 } else if (mem.eql(u8, segname, "__DATA")) {
485 if (!mem.eql(u8, sectname, "__const")) continue;
484 } else if (mem.eql(u8, segname, "__DATA") or mem.eql(u8, segname, "__DATA_CONST")) {
486485 if (self.data_const_section_index != null) continue;
487486
488487 self.data_const_section_index = @intCast(u16, data_const_seg.sections.items.len);
......@@ -503,7 +502,6 @@ fn updateMetadata(self: *Zld) !void {
503502 }
504503 },
505504 macho.S_CSTRING_LITERALS => {
506 if (!mem.eql(u8, segname, "__TEXT")) continue;
507505 if (self.cstring_section_index != null) continue;
508506
509507 self.cstring_section_index = @intCast(u16, text_seg.sections.items.len);
......@@ -523,7 +521,6 @@ fn updateMetadata(self: *Zld) !void {
523521 });
524522 },
525523 macho.S_MOD_INIT_FUNC_POINTERS => {
526 if (!mem.eql(u8, segname, "__DATA")) continue;
527524 if (self.mod_init_func_section_index != null) continue;
528525
529526 self.mod_init_func_section_index = @intCast(u16, data_const_seg.sections.items.len);
......@@ -543,7 +540,6 @@ fn updateMetadata(self: *Zld) !void {
543540 });
544541 },
545542 macho.S_MOD_TERM_FUNC_POINTERS => {
546 if (!mem.eql(u8, segname, "__DATA")) continue;
547543 if (self.mod_term_func_section_index != null) continue;
548544
549545 self.mod_term_func_section_index = @intCast(u16, data_const_seg.sections.items.len);
......@@ -563,7 +559,6 @@ fn updateMetadata(self: *Zld) !void {
563559 });
564560 },
565561 macho.S_ZEROFILL => {
566 if (!mem.eql(u8, segname, "__DATA")) continue;
567562 if (mem.eql(u8, sectname, "__common")) {
568563 if (self.common_section_index != null) continue;
569564
......@@ -603,7 +598,6 @@ fn updateMetadata(self: *Zld) !void {
603598 }
604599 },
605600 macho.S_THREAD_LOCAL_VARIABLES => {
606 if (!mem.eql(u8, segname, "__DATA")) continue;
607601 if (self.tlv_section_index != null) continue;
608602
609603 self.tlv_section_index = @intCast(u16, data_seg.sections.items.len);
......@@ -623,7 +617,6 @@ fn updateMetadata(self: *Zld) !void {
623617 });
624618 },
625619 macho.S_THREAD_LOCAL_REGULAR => {
626 if (!mem.eql(u8, segname, "__DATA")) continue;
627620 if (self.tlv_data_section_index != null) continue;
628621
629622 self.tlv_data_section_index = @intCast(u16, data_seg.sections.items.len);
......@@ -643,7 +636,6 @@ fn updateMetadata(self: *Zld) !void {
643636 });
644637 },
645638 macho.S_THREAD_LOCAL_ZEROFILL => {
646 if (!mem.eql(u8, segname, "__DATA")) continue;
647639 if (self.tlv_bss_section_index != null) continue;
648640
649641 self.tlv_bss_section_index = @intCast(u16, data_seg.sections.items.len);
......@@ -863,6 +855,12 @@ fn getMatchingSection(self: *Zld, section: macho.section_64) ?MatchingSection {
863855 .sect = self.text_const_section_index.?,
864856 };
865857 }
858 if (mem.eql(u8, segname, "__DATA_CONST")) {
859 break :blk .{
860 .seg = self.data_const_segment_cmd_index.?,
861 .sect = self.data_const_section_index.?,
862 };
863 }
866864 if (mem.eql(u8, segname, "__DATA")) {
867865 if (mem.eql(u8, sectname, "__const")) {
868866 break :blk .{
......@@ -1900,7 +1898,12 @@ fn relocTargetAddr(self: *Zld, object: *const Object, target: reloc.Relocation.T
19001898 }
19011899 },
19021900 .section => |sect_id| {
1901 log.debug(" | section offset", .{});
19031902 const source_sect = object.sections.items[sect_id];
1903 log.debug(" | section '{s},{s}'", .{
1904 parseName(&source_sect.inner.segname),
1905 parseName(&source_sect.inner.sectname),
1906 });
19041907 const target_map = source_sect.target_map orelse unreachable;
19051908 const target_seg = self.load_commands.items[target_map.segment_id].Segment;
19061909 const target_sect = target_seg.sections.items[target_map.section_id];