| ... | ... | @@ -285,8 +285,7 @@ pub fn createEmpty( |
| 285 | 285 | }; |
| 286 | 286 | try self.d_sym.?.initMetadata(self); |
| 287 | 287 | } else { |
| 288 | | try self.reportUnexpectedError("TODO: implement generating and emitting __DWARF in .o file", .{}); |
| 289 | | return error.Unexpected; |
| 288 | @panic("TODO: implement generating and emitting __DWARF in .o file"); |
| 290 | 289 | }, |
| 291 | 290 | .code_view => unreachable, |
| 292 | 291 | } |
| ... | ... | @@ -2025,7 +2024,7 @@ pub fn sortSections(self: *MachO) !void { |
| 2025 | 2024 | |
| 2026 | 2025 | for (zo.symtab.items(.nlist)) |*sym| { |
| 2027 | 2026 | if (sym.sect()) { |
| 2028 | | sym.n_sect = backlinks[sym.n_sect]; |
| 2027 | sym.n_sect = backlinks[sym.n_sect - 1] + 1; |
| 2029 | 2028 | } |
| 2030 | 2029 | } |
| 2031 | 2030 | |
| ... | ... | @@ -3391,8 +3390,6 @@ fn initMetadata(self: *MachO, options: InitMetadataOptions) !void { |
| 3391 | 3390 | .prot = macho.PROT.READ | macho.PROT.WRITE, |
| 3392 | 3391 | }); |
| 3393 | 3392 | } |
| 3394 | | } else { |
| 3395 | | @panic("TODO initMetadata when relocatable"); |
| 3396 | 3393 | } |
| 3397 | 3394 | |
| 3398 | 3395 | const appendSect = struct { |
| ... | ... | @@ -3415,7 +3412,7 @@ fn initMetadata(self: *MachO, options: InitMetadataOptions) !void { |
| 3415 | 3412 | }, |
| 3416 | 3413 | .flags = macho.S_REGULAR | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS, |
| 3417 | 3414 | }); |
| 3418 | | appendSect(self, self.zig_text_sect_index.?, self.zig_text_seg_index.?); |
| 3415 | if (!self.base.isRelocatable()) appendSect(self, self.zig_text_sect_index.?, self.zig_text_seg_index.?); |
| 3419 | 3416 | } |
| 3420 | 3417 | |
| 3421 | 3418 | if (!self.base.isRelocatable()) { |
| ... | ... | @@ -3427,33 +3424,35 @@ fn initMetadata(self: *MachO, options: InitMetadataOptions) !void { |
| 3427 | 3424 | |
| 3428 | 3425 | { |
| 3429 | 3426 | self.zig_const_sect_index = try self.addSection("__CONST_ZIG", "__const_zig", .{}); |
| 3430 | | appendSect(self, self.zig_const_sect_index.?, self.zig_const_seg_index.?); |
| 3427 | if (!self.base.isRelocatable()) appendSect(self, self.zig_const_sect_index.?, self.zig_const_seg_index.?); |
| 3431 | 3428 | } |
| 3432 | 3429 | |
| 3433 | 3430 | { |
| 3434 | 3431 | self.zig_data_sect_index = try self.addSection("__DATA_ZIG", "__data_zig", .{}); |
| 3435 | | appendSect(self, self.zig_data_sect_index.?, self.zig_data_seg_index.?); |
| 3432 | if (!self.base.isRelocatable()) appendSect(self, self.zig_data_sect_index.?, self.zig_data_seg_index.?); |
| 3436 | 3433 | } |
| 3437 | 3434 | |
| 3438 | 3435 | { |
| 3439 | 3436 | self.zig_bss_sect_index = try self.addSection("__BSS_ZIG", "__bss_zig", .{ |
| 3440 | 3437 | .flags = macho.S_ZEROFILL, |
| 3441 | 3438 | }); |
| 3442 | | appendSect(self, self.zig_bss_sect_index.?, self.zig_bss_seg_index.?); |
| 3439 | if (!self.base.isRelocatable()) appendSect(self, self.zig_bss_sect_index.?, self.zig_bss_seg_index.?); |
| 3443 | 3440 | } |
| 3444 | 3441 | } |
| 3445 | 3442 | |
| 3446 | 3443 | pub fn growSection(self: *MachO, sect_index: u8, needed_size: u64) !void { |
| 3447 | 3444 | const sect = &self.sections.items(.header)[sect_index]; |
| 3448 | | const seg_id = self.sections.items(.segment_id)[sect_index]; |
| 3449 | | const seg = &self.segments.items[seg_id]; |
| 3450 | 3445 | |
| 3451 | 3446 | if (needed_size > self.allocatedSize(sect.offset) and !sect.isZerofill()) { |
| 3452 | 3447 | const existing_size = sect.size; |
| 3453 | 3448 | sect.size = 0; |
| 3454 | 3449 | |
| 3455 | 3450 | // Must move the entire section. |
| 3456 | | const new_offset = self.findFreeSpace(needed_size, self.getPageSize()); |
| 3451 | const alignment = if (self.base.isRelocatable()) |
| 3452 | try math.powi(u32, 2, sect.@"align") |
| 3453 | else |
| 3454 | self.getPageSize(); |
| 3455 | const new_offset = self.findFreeSpace(needed_size, alignment); |
| 3457 | 3456 | |
| 3458 | 3457 | log.debug("new '{s},{s}' file offset 0x{x} to 0x{x}", .{ |
| 3459 | 3458 | sect.segName(), |
| ... | ... | @@ -3465,26 +3464,32 @@ pub fn growSection(self: *MachO, sect_index: u8, needed_size: u64) !void { |
| 3465 | 3464 | try self.copyRangeAllZeroOut(sect.offset, new_offset, existing_size); |
| 3466 | 3465 | |
| 3467 | 3466 | sect.offset = @intCast(new_offset); |
| 3468 | | seg.fileoff = new_offset; |
| 3469 | 3467 | } |
| 3470 | 3468 | |
| 3471 | 3469 | sect.size = needed_size; |
| 3472 | | if (!sect.isZerofill()) { |
| 3473 | | seg.filesize = needed_size; |
| 3474 | | } |
| 3475 | 3470 | |
| 3476 | | const mem_capacity = self.allocatedVirtualSize(seg.vmaddr); |
| 3477 | | if (needed_size > mem_capacity) { |
| 3478 | | var err = try self.addErrorWithNotes(2); |
| 3479 | | try err.addMsg(self, "fatal linker error: cannot expand segment seg({d})({s}) in virtual memory", .{ |
| 3480 | | seg_id, |
| 3481 | | seg.segName(), |
| 3482 | | }); |
| 3483 | | try err.addNote(self, "TODO: emit relocations to memory locations in self-hosted backends", .{}); |
| 3484 | | try err.addNote(self, "as a workaround, try increasing pre-allocated virtual memory of each segment", .{}); |
| 3485 | | } |
| 3471 | if (!self.base.isRelocatable()) { |
| 3472 | const seg_id = self.sections.items(.segment_id)[sect_index]; |
| 3473 | const seg = &self.segments.items[seg_id]; |
| 3474 | seg.fileoff = sect.offset; |
| 3486 | 3475 | |
| 3487 | | seg.vmsize = needed_size; |
| 3476 | if (!sect.isZerofill()) { |
| 3477 | seg.filesize = needed_size; |
| 3478 | } |
| 3479 | |
| 3480 | const mem_capacity = self.allocatedVirtualSize(seg.vmaddr); |
| 3481 | if (needed_size > mem_capacity) { |
| 3482 | var err = try self.addErrorWithNotes(2); |
| 3483 | try err.addMsg(self, "fatal linker error: cannot expand segment seg({d})({s}) in virtual memory", .{ |
| 3484 | seg_id, |
| 3485 | seg.segName(), |
| 3486 | }); |
| 3487 | try err.addNote(self, "TODO: emit relocations to memory locations in self-hosted backends", .{}); |
| 3488 | try err.addNote(self, "as a workaround, try increasing pre-allocated virtual memory of each segment", .{}); |
| 3489 | } |
| 3490 | |
| 3491 | seg.vmsize = needed_size; |
| 3492 | } |
| 3488 | 3493 | } |
| 3489 | 3494 | |
| 3490 | 3495 | pub fn getTarget(self: MachO) std.Target { |