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 {...@@ -481,8 +481,7 @@ fn updateMetadata(self: *Zld) !void {
481 .reserved3 = 0,481 .reserved3 = 0,
482 });482 });
483 }483 }
484 } else if (mem.eql(u8, segname, "__DATA")) {484 } else if (mem.eql(u8, segname, "__DATA") or mem.eql(u8, segname, "__DATA_CONST")) {
485 if (!mem.eql(u8, sectname, "__const")) continue;
486 if (self.data_const_section_index != null) continue;485 if (self.data_const_section_index != null) continue;
487486
488 self.data_const_section_index = @intCast(u16, data_const_seg.sections.items.len);487 self.data_const_section_index = @intCast(u16, data_const_seg.sections.items.len);
...@@ -503,7 +502,6 @@ fn updateMetadata(self: *Zld) !void {...@@ -503,7 +502,6 @@ fn updateMetadata(self: *Zld) !void {
503 }502 }
504 },503 },
505 macho.S_CSTRING_LITERALS => {504 macho.S_CSTRING_LITERALS => {
506 if (!mem.eql(u8, segname, "__TEXT")) continue;
507 if (self.cstring_section_index != null) continue;505 if (self.cstring_section_index != null) continue;
508506
509 self.cstring_section_index = @intCast(u16, text_seg.sections.items.len);507 self.cstring_section_index = @intCast(u16, text_seg.sections.items.len);
...@@ -523,7 +521,6 @@ fn updateMetadata(self: *Zld) !void {...@@ -523,7 +521,6 @@ fn updateMetadata(self: *Zld) !void {
523 });521 });
524 },522 },
525 macho.S_MOD_INIT_FUNC_POINTERS => {523 macho.S_MOD_INIT_FUNC_POINTERS => {
526 if (!mem.eql(u8, segname, "__DATA")) continue;
527 if (self.mod_init_func_section_index != null) continue;524 if (self.mod_init_func_section_index != null) continue;
528525
529 self.mod_init_func_section_index = @intCast(u16, data_const_seg.sections.items.len);526 self.mod_init_func_section_index = @intCast(u16, data_const_seg.sections.items.len);
...@@ -543,7 +540,6 @@ fn updateMetadata(self: *Zld) !void {...@@ -543,7 +540,6 @@ fn updateMetadata(self: *Zld) !void {
543 });540 });
544 },541 },
545 macho.S_MOD_TERM_FUNC_POINTERS => {542 macho.S_MOD_TERM_FUNC_POINTERS => {
546 if (!mem.eql(u8, segname, "__DATA")) continue;
547 if (self.mod_term_func_section_index != null) continue;543 if (self.mod_term_func_section_index != null) continue;
548544
549 self.mod_term_func_section_index = @intCast(u16, data_const_seg.sections.items.len);545 self.mod_term_func_section_index = @intCast(u16, data_const_seg.sections.items.len);
...@@ -563,7 +559,6 @@ fn updateMetadata(self: *Zld) !void {...@@ -563,7 +559,6 @@ fn updateMetadata(self: *Zld) !void {
563 });559 });
564 },560 },
565 macho.S_ZEROFILL => {561 macho.S_ZEROFILL => {
566 if (!mem.eql(u8, segname, "__DATA")) continue;
567 if (mem.eql(u8, sectname, "__common")) {562 if (mem.eql(u8, sectname, "__common")) {
568 if (self.common_section_index != null) continue;563 if (self.common_section_index != null) continue;
569564
...@@ -603,7 +598,6 @@ fn updateMetadata(self: *Zld) !void {...@@ -603,7 +598,6 @@ fn updateMetadata(self: *Zld) !void {
603 }598 }
604 },599 },
605 macho.S_THREAD_LOCAL_VARIABLES => {600 macho.S_THREAD_LOCAL_VARIABLES => {
606 if (!mem.eql(u8, segname, "__DATA")) continue;
607 if (self.tlv_section_index != null) continue;601 if (self.tlv_section_index != null) continue;
608602
609 self.tlv_section_index = @intCast(u16, data_seg.sections.items.len);603 self.tlv_section_index = @intCast(u16, data_seg.sections.items.len);
...@@ -623,7 +617,6 @@ fn updateMetadata(self: *Zld) !void {...@@ -623,7 +617,6 @@ fn updateMetadata(self: *Zld) !void {
623 });617 });
624 },618 },
625 macho.S_THREAD_LOCAL_REGULAR => {619 macho.S_THREAD_LOCAL_REGULAR => {
626 if (!mem.eql(u8, segname, "__DATA")) continue;
627 if (self.tlv_data_section_index != null) continue;620 if (self.tlv_data_section_index != null) continue;
628621
629 self.tlv_data_section_index = @intCast(u16, data_seg.sections.items.len);622 self.tlv_data_section_index = @intCast(u16, data_seg.sections.items.len);
...@@ -643,7 +636,6 @@ fn updateMetadata(self: *Zld) !void {...@@ -643,7 +636,6 @@ fn updateMetadata(self: *Zld) !void {
643 });636 });
644 },637 },
645 macho.S_THREAD_LOCAL_ZEROFILL => {638 macho.S_THREAD_LOCAL_ZEROFILL => {
646 if (!mem.eql(u8, segname, "__DATA")) continue;
647 if (self.tlv_bss_section_index != null) continue;639 if (self.tlv_bss_section_index != null) continue;
648640
649 self.tlv_bss_section_index = @intCast(u16, data_seg.sections.items.len);641 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 {...@@ -863,6 +855,12 @@ fn getMatchingSection(self: *Zld, section: macho.section_64) ?MatchingSection {
863 .sect = self.text_const_section_index.?,855 .sect = self.text_const_section_index.?,
864 };856 };
865 }857 }
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 }
866 if (mem.eql(u8, segname, "__DATA")) {864 if (mem.eql(u8, segname, "__DATA")) {
867 if (mem.eql(u8, sectname, "__const")) {865 if (mem.eql(u8, sectname, "__const")) {
868 break :blk .{866 break :blk .{
...@@ -1900,7 +1898,12 @@ fn relocTargetAddr(self: *Zld, object: *const Object, target: reloc.Relocation.T...@@ -1900,7 +1898,12 @@ fn relocTargetAddr(self: *Zld, object: *const Object, target: reloc.Relocation.T
1900 }1898 }
1901 },1899 },
1902 .section => |sect_id| {1900 .section => |sect_id| {
1901 log.debug(" | section offset", .{});
1903 const source_sect = object.sections.items[sect_id];1902 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 });
1904 const target_map = source_sect.target_map orelse unreachable;1907 const target_map = source_sect.target_map orelse unreachable;
1905 const target_seg = self.load_commands.items[target_map.segment_id].Segment;1908 const target_seg = self.load_commands.items[target_map.segment_id].Segment;
1906 const target_sect = target_seg.sections.items[target_map.section_id];1909 const target_sect = target_seg.sections.items[target_map.section_id];