authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-06-21 14:16:10+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-06-24 18:56:09+02:00
loga480ae6e37199a3b210a4029f3d3c728e959a21d
treed68a2ef6eac3d64af9adf990dc985bd8ed698592
parent5f9b4cba6da9540d5a2fd03d9b379d0b7eb5937d

zld: handle objc-specific sections


1 files changed, 138 insertions(+), 1 deletions(-)

src/link/MachO/Zld.zig+138-1
......@@ -73,6 +73,11 @@ gcc_except_tab_section_index: ?u16 = null,
7373unwind_info_section_index: ?u16 = null,
7474eh_frame_section_index: ?u16 = null,
7575
76objc_methlist_section_index: ?u16 = null,
77objc_methname_section_index: ?u16 = null,
78objc_methtype_section_index: ?u16 = null,
79objc_classname_section_index: ?u16 = null,
80
7681// __DATA_CONST segment sections
7782got_section_index: ?u16 = null,
7883mod_init_func_section_index: ?u16 = null,
......@@ -80,6 +85,8 @@ mod_term_func_section_index: ?u16 = null,
8085data_const_section_index: ?u16 = null,
8186
8287objc_cfstring_section_index: ?u16 = null,
88objc_classlist_section_index: ?u16 = null,
89objc_imageinfo_section_index: ?u16 = null,
8390
8491// __DATA segment sections
8592tlv_section_index: ?u16 = null,
......@@ -90,6 +97,11 @@ data_section_index: ?u16 = null,
9097bss_section_index: ?u16 = null,
9198common_section_index: ?u16 = null,
9299
100objc_const_section_index: ?u16 = null,
101objc_selrefs_section_index: ?u16 = null,
102objc_classrefs_section_index: ?u16 = null,
103objc_data_section_index: ?u16 = null,
104
93105globals: std.StringArrayHashMapUnmanaged(*Symbol) = .{},
94106imports: std.StringArrayHashMapUnmanaged(*Symbol) = .{},
95107unresolved: std.StringArrayHashMapUnmanaged(*Symbol) = .{},
......@@ -612,7 +624,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {
612624
613625 const res: ?MatchingSection = blk: {
614626 switch (sect.sectionType()) {
615 macho.S_4BYTE_LITERALS, macho.S_8BYTE_LITERALS, macho.S_16BYTE_LITERALS, macho.S_LITERAL_POINTERS => {
627 macho.S_4BYTE_LITERALS, macho.S_8BYTE_LITERALS, macho.S_16BYTE_LITERALS => {
616628 if (self.text_const_section_index == null) {
617629 self.text_const_section_index = @intCast(u16, text_seg.sections.items.len);
618630 try text_seg.addSection(self.allocator, "__const", "__TEXT", .{});
......@@ -624,6 +636,44 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {
624636 };
625637 },
626638 macho.S_CSTRING_LITERALS => {
639 if (mem.eql(u8, sectname, "__objc_methname")) {
640 // TODO it seems the common values within the sections in objects are deduplicated/merged
641 // on merging the sections' contents.
642 if (self.objc_methname_section_index == null) {
643 self.objc_methname_section_index = @intCast(u16, text_seg.sections.items.len);
644 try text_seg.addSection(self.allocator, "__objc_methname", "__TEXT", .{
645 .flags = macho.S_CSTRING_LITERALS,
646 });
647 }
648
649 break :blk .{
650 .seg = self.text_segment_cmd_index.?,
651 .sect = self.objc_methname_section_index.?,
652 };
653 } else if (mem.eql(u8, sectname, "__objc_methtype")) {
654 if (self.objc_methtype_section_index == null) {
655 self.objc_methtype_section_index = @intCast(u16, text_seg.sections.items.len);
656 try text_seg.addSection(self.allocator, "__objc_methtype", "__TEXT", .{
657 .flags = macho.S_CSTRING_LITERALS,
658 });
659 }
660
661 break :blk .{
662 .seg = self.text_segment_cmd_index.?,
663 .sect = self.objc_methtype_section_index.?,
664 };
665 } else if (mem.eql(u8, sectname, "__objc_classname")) {
666 if (self.objc_classname_section_index == null) {
667 self.objc_classname_section_index = @intCast(u16, text_seg.sections.items.len);
668 try text_seg.addSection(self.allocator, "__objc_classname", "__TEXT", .{});
669 }
670
671 break :blk .{
672 .seg = self.text_segment_cmd_index.?,
673 .sect = self.objc_classname_section_index.?,
674 };
675 }
676
627677 if (self.cstring_section_index == null) {
628678 self.cstring_section_index = @intCast(u16, text_seg.sections.items.len);
629679 try text_seg.addSection(self.allocator, "__cstring", "__TEXT", .{
......@@ -636,6 +686,24 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {
636686 .sect = self.cstring_section_index.?,
637687 };
638688 },
689 macho.S_LITERAL_POINTERS => {
690 if (mem.eql(u8, segname, "__DATA") and mem.eql(u8, sectname, "__objc_selrefs")) {
691 if (self.objc_selrefs_section_index == null) {
692 self.objc_selrefs_section_index = @intCast(u16, data_seg.sections.items.len);
693 try data_seg.addSection(self.allocator, "__objc_selrefs", "__DATA", .{
694 .flags = macho.S_LITERAL_POINTERS,
695 });
696 }
697
698 break :blk .{
699 .seg = self.data_segment_cmd_index.?,
700 .sect = self.objc_selrefs_section_index.?,
701 };
702 }
703
704 // TODO investigate
705 break :blk null;
706 },
639707 macho.S_MOD_INIT_FUNC_POINTERS => {
640708 if (self.mod_init_func_section_index == null) {
641709 self.mod_init_func_section_index = @intCast(u16, data_const_seg.sections.items.len);
......@@ -799,6 +867,16 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {
799867 .seg = self.text_segment_cmd_index.?,
800868 .sect = self.gcc_except_tab_section_index.?,
801869 };
870 } else if (mem.eql(u8, sectname, "__objc_methlist")) {
871 if (self.objc_methlist_section_index == null) {
872 self.objc_methlist_section_index = @intCast(u16, text_seg.sections.items.len);
873 try text_seg.addSection(self.allocator, "__objc_methlist", "__TEXT", .{});
874 }
875
876 break :blk .{
877 .seg = self.text_segment_cmd_index.?,
878 .sect = self.objc_methlist_section_index.?,
879 };
802880 } else {
803881 if (self.text_const_section_index == null) {
804882 self.text_const_section_index = @intCast(u16, text_seg.sections.items.len);
......@@ -845,6 +923,56 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection {
845923 .seg = self.data_const_segment_cmd_index.?,
846924 .sect = self.objc_cfstring_section_index.?,
847925 };
926 } else if (mem.eql(u8, sectname, "__objc_classlist")) {
927 if (self.objc_classlist_section_index == null) {
928 self.objc_classlist_section_index = @intCast(u16, data_const_seg.sections.items.len);
929 try data_const_seg.addSection(self.allocator, "__objc_classlist", "__DATA_CONST", .{});
930 }
931
932 break :blk .{
933 .seg = self.data_const_segment_cmd_index.?,
934 .sect = self.objc_classlist_section_index.?,
935 };
936 } else if (mem.eql(u8, sectname, "__objc_imageinfo")) {
937 if (self.objc_imageinfo_section_index == null) {
938 self.objc_imageinfo_section_index = @intCast(u16, data_const_seg.sections.items.len);
939 try data_const_seg.addSection(self.allocator, "__objc_imageinfo", "__DATA_CONST", .{});
940 }
941
942 break :blk .{
943 .seg = self.data_const_segment_cmd_index.?,
944 .sect = self.objc_imageinfo_section_index.?,
945 };
946 } else if (mem.eql(u8, sectname, "__objc_const")) {
947 if (self.objc_const_section_index == null) {
948 self.objc_const_section_index = @intCast(u16, data_seg.sections.items.len);
949 try data_seg.addSection(self.allocator, "__objc_const", "__DATA", .{});
950 }
951
952 break :blk .{
953 .seg = self.data_segment_cmd_index.?,
954 .sect = self.objc_const_section_index.?,
955 };
956 } else if (mem.eql(u8, sectname, "__objc_classrefs")) {
957 if (self.objc_classrefs_section_index == null) {
958 self.objc_classrefs_section_index = @intCast(u16, data_seg.sections.items.len);
959 try data_seg.addSection(self.allocator, "__objc_classrefs", "__DATA", .{});
960 }
961
962 break :blk .{
963 .seg = self.data_segment_cmd_index.?,
964 .sect = self.objc_classrefs_section_index.?,
965 };
966 } else if (mem.eql(u8, sectname, "__objc_data")) {
967 if (self.objc_data_section_index == null) {
968 self.objc_data_section_index = @intCast(u16, data_seg.sections.items.len);
969 try data_seg.addSection(self.allocator, "__objc_data", "__DATA", .{});
970 }
971
972 break :blk .{
973 .seg = self.data_segment_cmd_index.?,
974 .sect = self.objc_data_section_index.?,
975 };
848976 } else {
849977 if (self.data_section_index == null) {
850978 self.data_section_index = @intCast(u16, data_seg.sections.items.len);
......@@ -896,6 +1024,9 @@ fn sortSections(self: *Zld) !void {
8961024 &self.cstring_section_index,
8971025 &self.ustring_section_index,
8981026 &self.text_const_section_index,
1027 &self.objc_methname_section_index,
1028 &self.objc_methtype_section_index,
1029 &self.objc_classname_section_index,
8991030 &self.eh_frame_section_index,
9001031 };
9011032 for (indices) |maybe_index| {
......@@ -922,6 +1053,8 @@ fn sortSections(self: *Zld) !void {
9221053 &self.mod_term_func_section_index,
9231054 &self.data_const_section_index,
9241055 &self.objc_cfstring_section_index,
1056 &self.objc_classlist_section_index,
1057 &self.objc_imageinfo_section_index,
9251058 };
9261059 for (indices) |maybe_index| {
9271060 const new_index: u16 = if (maybe_index.*) |index| blk: {
......@@ -944,6 +1077,10 @@ fn sortSections(self: *Zld) !void {
9441077 // __DATA segment
9451078 const indices = &[_]*?u16{
9461079 &self.la_symbol_ptr_section_index,
1080 &self.objc_const_section_index,
1081 &self.objc_selrefs_section_index,
1082 &self.objc_classrefs_section_index,
1083 &self.objc_data_section_index,
9471084 &self.data_section_index,
9481085 &self.tlv_section_index,
9491086 &self.tlv_data_section_index,