Commit 49b3986417

Jakub Konka <kubkon@jakubkonka.com>
2021-06-25 18:20:59
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.
1 parent 411f9c6
Changed files (2)
src
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 {
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);