From 49b3986417378278cef4157e71c38a817726ddc8 Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Fri, 25 Jun 2021 18:20:59 +0200 Subject: [PATCH] zld: fix section mapping for Go specific sections which include: * `__TEXT,__rodata` => `__DATA_CONST,__const` * `__TEXT,__typelink` => `__DATA_CONST,__const` * `__TEXT,__itablink` => `__DATA_CONST,__const` * `__TEXT,__gosymtab` => `__DATA_CONST,__const` * `__TEXT,__gopclntab` => `__DATA_CONST,__const` Also, we treat section as containing machine code and mapping it to `__TEXT,__text` if it is `S_REGULAR` and contains either `S_ATTR_PURE_INSTRUCTIONS` or `S_ATTR_SOME_INSTRUCTIONS` or both. --- src/link/MachO/Object.zig | 2 +- src/link/MachO/Zld.zig | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/link/MachO/Object.zig b/src/link/MachO/Object.zig index 671199670f..cb55dd1fd8 100644 --- a/src/link/MachO/Object.zig +++ b/src/link/MachO/Object.zig @@ -94,7 +94,7 @@ pub const Section = struct { pub fn isCode(self: Section) bool { const attr = self.sectionAttrs(); - return attr & macho.S_ATTR_PURE_INSTRUCTIONS != 0 and attr & macho.S_ATTR_SOME_INSTRUCTIONS != 0; + return attr & macho.S_ATTR_PURE_INSTRUCTIONS != 0 or attr & macho.S_ATTR_SOME_INSTRUCTIONS != 0; } pub fn isDebug(self: Section) bool { diff --git a/src/link/MachO/Zld.zig b/src/link/MachO/Zld.zig index f391899159..6cabdfa73e 100644 --- a/src/link/MachO/Zld.zig +++ b/src/link/MachO/Zld.zig @@ -367,6 +367,7 @@ fn mapAndUpdateSections( offset, offset + size, }); + log.debug(" | flags 0x{x}", .{source_sect.inner.flags}); source_sect.target_map = .{ .segment_id = target_seg_id, @@ -740,6 +741,21 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) !?MatchingSection { .seg = self.text_segment_cmd_index.?, .sect = self.objc_methlist_section_index.?, }; + } else if (mem.eql(u8, sectname, "__rodata") or + mem.eql(u8, sectname, "__typelink") or + mem.eql(u8, sectname, "__itablink") or + mem.eql(u8, sectname, "__gosymtab") or + mem.eql(u8, sectname, "__gopclntab")) + { + if (self.data_const_section_index == null) { + self.data_const_section_index = @intCast(u16, data_const_seg.sections.items.len); + try data_const_seg.addSection(self.allocator, "__const", .{}); + } + + break :blk .{ + .seg = self.data_const_segment_cmd_index.?, + .sect = self.data_const_section_index.?, + }; } else { if (self.text_const_section_index == null) { self.text_const_section_index = @intCast(u16, text_seg.sections.items.len);