| ... | @@ -78,6 +78,10 @@ pub fn flush(macho_file: *MachO, comp: *Compilation, module_obj_path: ?[]const u | ... | @@ -78,6 +78,10 @@ pub fn flush(macho_file: *MachO, comp: *Compilation, module_obj_path: ?[]const u |
| 78 | off = mem.alignForward(u32, off, @alignOf(u64)); | 78 | off = mem.alignForward(u32, off, @alignOf(u64)); |
| 79 | off = try macho_file.writeStrtab(off); | 79 | off = try macho_file.writeStrtab(off); |
| 80 | | 80 | |
| | 81 | // In order to please Apple ld (and possibly other MachO linkers in the wild), |
| | 82 | // we will now sanitize segment names of Zig-specific segments. |
| | 83 | sanitizeZigSections(macho_file); |
| | 84 | |
| 81 | const ncmds, const sizeofcmds = try writeLoadCommands(macho_file); | 85 | const ncmds, const sizeofcmds = try writeLoadCommands(macho_file); |
| 82 | try writeHeader(macho_file, ncmds, sizeofcmds); | 86 | try writeHeader(macho_file, ncmds, sizeofcmds); |
| 83 | } | 87 | } |
| ... | @@ -245,6 +249,31 @@ fn allocateSections(macho_file: *MachO) !void { | ... | @@ -245,6 +249,31 @@ fn allocateSections(macho_file: *MachO) !void { |
| 245 | } | 249 | } |
| 246 | } | 250 | } |
| 247 | | 251 | |
| | 252 | /// Renames segment names in Zig sections to standard MachO segment names such as |
| | 253 | /// `__TEXT`, `__DATA_CONST` and `__DATA`. |
| | 254 | /// TODO: I think I may be able to get rid of this if I rework section/segment |
| | 255 | /// allocation mechanism to not rely so much on having `_ZIG` sections always |
| | 256 | /// pushed to the back. For instance, this is not a problem in ELF linker. |
| | 257 | /// Then, we can create sections with the correct name from the start in `MachO.initMetadata`. |
| | 258 | fn sanitizeZigSections(macho_file: *MachO) void { |
| | 259 | if (macho_file.zig_text_sect_index) |index| { |
| | 260 | const header = &macho_file.sections.items(.header)[index]; |
| | 261 | header.segname = MachO.makeStaticString("__TEXT"); |
| | 262 | } |
| | 263 | if (macho_file.zig_const_sect_index) |index| { |
| | 264 | const header = &macho_file.sections.items(.header)[index]; |
| | 265 | header.segname = MachO.makeStaticString("__DATA_CONST"); |
| | 266 | } |
| | 267 | if (macho_file.zig_data_sect_index) |index| { |
| | 268 | const header = &macho_file.sections.items(.header)[index]; |
| | 269 | header.segname = MachO.makeStaticString("__DATA"); |
| | 270 | } |
| | 271 | if (macho_file.zig_bss_sect_index) |index| { |
| | 272 | const header = &macho_file.sections.items(.header)[index]; |
| | 273 | header.segname = MachO.makeStaticString("__DATA"); |
| | 274 | } |
| | 275 | } |
| | 276 | |
| 248 | fn createSegment(macho_file: *MachO) !void { | 277 | fn createSegment(macho_file: *MachO) !void { |
| 249 | const gpa = macho_file.base.comp.gpa; | 278 | const gpa = macho_file.base.comp.gpa; |
| 250 | | 279 | |