| ... | ... | @@ -20,9 +20,10 @@ const Module = @import("../../Module.zig"); |
| 20 | 20 | const StringTable = @import("../strtab.zig").StringTable; |
| 21 | 21 | const Type = @import("../../type.zig").Type; |
| 22 | 22 | |
| 23 | | base: *MachO, |
| 23 | allocator: Allocator, |
| 24 | 24 | dwarf: Dwarf, |
| 25 | 25 | file: fs.File, |
| 26 | page_size: u16, |
| 26 | 27 | |
| 27 | 28 | segments: std.ArrayListUnmanaged(macho.segment_command_64) = .{}, |
| 28 | 29 | sections: std.ArrayListUnmanaged(macho.section_64) = .{}, |
| ... | ... | @@ -59,21 +60,17 @@ pub const Reloc = struct { |
| 59 | 60 | |
| 60 | 61 | /// You must call this function *after* `MachO.populateMissingMetadata()` |
| 61 | 62 | /// has been called to get a viable debug symbols output. |
| 62 | | pub fn populateMissingMetadata(self: *DebugSymbols, gpa: Allocator) !void { |
| 63 | pub fn populateMissingMetadata(self: *DebugSymbols) !void { |
| 63 | 64 | if (self.dwarf_segment_cmd_index == null) { |
| 64 | 65 | self.dwarf_segment_cmd_index = @intCast(u8, self.segments.items.len); |
| 65 | 66 | |
| 66 | | const off = @intCast(u64, self.base.page_size); |
| 67 | const off = @intCast(u64, self.page_size); |
| 67 | 68 | const ideal_size: u16 = 200 + 128 + 160 + 250; |
| 68 | | const needed_size = mem.alignForwardGeneric( |
| 69 | | u64, |
| 70 | | padToIdeal(ideal_size), |
| 71 | | self.base.page_size, |
| 72 | | ); |
| 69 | const needed_size = mem.alignForwardGeneric(u64, padToIdeal(ideal_size), self.page_size); |
| 73 | 70 | |
| 74 | 71 | log.debug("found __DWARF segment free space 0x{x} to 0x{x}", .{ off, off + needed_size }); |
| 75 | 72 | |
| 76 | | try self.segments.append(gpa, .{ |
| 73 | try self.segments.append(self.allocator, .{ |
| 77 | 74 | .segname = makeStaticString("__DWARF"), |
| 78 | 75 | .vmsize = needed_size, |
| 79 | 76 | .fileoff = off, |
| ... | ... | @@ -114,7 +111,7 @@ pub fn populateMissingMetadata(self: *DebugSymbols, gpa: Allocator) !void { |
| 114 | 111 | |
| 115 | 112 | if (self.linkedit_segment_cmd_index == null) { |
| 116 | 113 | self.linkedit_segment_cmd_index = @intCast(u8, self.segments.items.len); |
| 117 | | try self.segments.append(gpa, .{ |
| 114 | try self.segments.append(self.allocator, .{ |
| 118 | 115 | .segname = makeStaticString("__LINKEDIT"), |
| 119 | 116 | .maxprot = macho.PROT.READ, |
| 120 | 117 | .initprot = macho.PROT.READ, |
| ... | ... | @@ -124,8 +121,6 @@ pub fn populateMissingMetadata(self: *DebugSymbols, gpa: Allocator) !void { |
| 124 | 121 | } |
| 125 | 122 | |
| 126 | 123 | fn allocateSection(self: *DebugSymbols, sectname: []const u8, size: u64, alignment: u16) !u8 { |
| 127 | | const gpa = self.base.base.allocator; |
| 128 | | |
| 129 | 124 | const segment = self.getDwarfSegmentPtr(); |
| 130 | 125 | var sect = macho.section_64{ |
| 131 | 126 | .sectname = makeStaticString(sectname), |
| ... | ... | @@ -147,7 +142,7 @@ fn allocateSection(self: *DebugSymbols, sectname: []const u8, size: u64, alignme |
| 147 | 142 | sect.offset = @intCast(u32, off); |
| 148 | 143 | |
| 149 | 144 | const index = @intCast(u8, self.sections.items.len); |
| 150 | | try self.sections.append(gpa, sect); |
| 145 | try self.sections.append(self.allocator, sect); |
| 151 | 146 | segment.cmdsize += @sizeOf(macho.section_64); |
| 152 | 147 | segment.nsects += 1; |
| 153 | 148 | |
| ... | ... | @@ -175,34 +170,35 @@ pub fn findFreeSpace(self: *DebugSymbols, object_size: u64, min_alignment: u64) |
| 175 | 170 | return offset; |
| 176 | 171 | } |
| 177 | 172 | |
| 178 | | pub fn flushModule(self: *DebugSymbols, allocator: Allocator, options: link.Options) !void { |
| 179 | | // TODO This linker code currently assumes there is only 1 compilation unit and it corresponds to the |
| 180 | | // Zig source code. |
| 173 | pub fn flushModule(self: *DebugSymbols, macho_file: *MachO) !void { |
| 174 | // TODO This linker code currently assumes there is only 1 compilation unit |
| 175 | // and it corresponds to the Zig source code. |
| 176 | const options = macho_file.base.options; |
| 181 | 177 | const module = options.module orelse return error.LinkingWithoutZigSourceUnimplemented; |
| 182 | 178 | |
| 183 | 179 | for (self.relocs.items) |*reloc| { |
| 184 | 180 | const sym = switch (reloc.type) { |
| 185 | | .direct_load => self.base.getSymbol(.{ .sym_index = reloc.target, .file = null }), |
| 181 | .direct_load => macho_file.getSymbol(.{ .sym_index = reloc.target, .file = null }), |
| 186 | 182 | .got_load => blk: { |
| 187 | | const got_index = self.base.got_entries_table.get(.{ |
| 183 | const got_index = macho_file.got_entries_table.get(.{ |
| 188 | 184 | .sym_index = reloc.target, |
| 189 | 185 | .file = null, |
| 190 | 186 | }).?; |
| 191 | | const got_entry = self.base.got_entries.items[got_index]; |
| 192 | | break :blk got_entry.getSymbol(self.base); |
| 187 | const got_entry = macho_file.got_entries.items[got_index]; |
| 188 | break :blk got_entry.getSymbol(macho_file); |
| 193 | 189 | }, |
| 194 | 190 | }; |
| 195 | 191 | if (sym.n_value == reloc.prev_vaddr) continue; |
| 196 | 192 | |
| 197 | 193 | const sym_name = switch (reloc.type) { |
| 198 | | .direct_load => self.base.getSymbolName(.{ .sym_index = reloc.target, .file = null }), |
| 194 | .direct_load => macho_file.getSymbolName(.{ .sym_index = reloc.target, .file = null }), |
| 199 | 195 | .got_load => blk: { |
| 200 | | const got_index = self.base.got_entries_table.get(.{ |
| 196 | const got_index = macho_file.got_entries_table.get(.{ |
| 201 | 197 | .sym_index = reloc.target, |
| 202 | 198 | .file = null, |
| 203 | 199 | }).?; |
| 204 | | const got_entry = self.base.got_entries.items[got_index]; |
| 205 | | break :blk got_entry.getName(self.base); |
| 200 | const got_entry = macho_file.got_entries.items[got_index]; |
| 201 | break :blk got_entry.getName(macho_file); |
| 206 | 202 | }, |
| 207 | 203 | }; |
| 208 | 204 | const sect = &self.sections.items[self.debug_info_section_index.?]; |
| ... | ... | @@ -218,30 +214,30 @@ pub fn flushModule(self: *DebugSymbols, allocator: Allocator, options: link.Opti |
| 218 | 214 | } |
| 219 | 215 | |
| 220 | 216 | if (self.debug_abbrev_section_dirty) { |
| 221 | | try self.dwarf.writeDbgAbbrev(&self.base.base); |
| 217 | try self.dwarf.writeDbgAbbrev(&macho_file.base); |
| 222 | 218 | self.debug_abbrev_section_dirty = false; |
| 223 | 219 | } |
| 224 | 220 | |
| 225 | 221 | if (self.debug_info_header_dirty) { |
| 226 | 222 | // Currently only one compilation unit is supported, so the address range is simply |
| 227 | 223 | // identical to the main program header virtual address and memory size. |
| 228 | | const text_section = self.base.sections.items(.header)[self.base.text_section_index.?]; |
| 224 | const text_section = macho_file.sections.items(.header)[macho_file.text_section_index.?]; |
| 229 | 225 | const low_pc = text_section.addr; |
| 230 | 226 | const high_pc = text_section.addr + text_section.size; |
| 231 | | try self.dwarf.writeDbgInfoHeader(&self.base.base, module, low_pc, high_pc); |
| 227 | try self.dwarf.writeDbgInfoHeader(&macho_file.base, module, low_pc, high_pc); |
| 232 | 228 | self.debug_info_header_dirty = false; |
| 233 | 229 | } |
| 234 | 230 | |
| 235 | 231 | if (self.debug_aranges_section_dirty) { |
| 236 | 232 | // Currently only one compilation unit is supported, so the address range is simply |
| 237 | 233 | // identical to the main program header virtual address and memory size. |
| 238 | | const text_section = self.base.sections.items(.header)[self.base.text_section_index.?]; |
| 239 | | try self.dwarf.writeDbgAranges(&self.base.base, text_section.addr, text_section.size); |
| 234 | const text_section = macho_file.sections.items(.header)[macho_file.text_section_index.?]; |
| 235 | try self.dwarf.writeDbgAranges(&macho_file.base, text_section.addr, text_section.size); |
| 240 | 236 | self.debug_aranges_section_dirty = false; |
| 241 | 237 | } |
| 242 | 238 | |
| 243 | 239 | if (self.debug_line_header_dirty) { |
| 244 | | try self.dwarf.writeDbgLineHeader(&self.base.base, module); |
| 240 | try self.dwarf.writeDbgLineHeader(&macho_file.base, module); |
| 245 | 241 | self.debug_line_header_dirty = false; |
| 246 | 242 | } |
| 247 | 243 | |
| ... | ... | @@ -270,40 +266,45 @@ pub fn flushModule(self: *DebugSymbols, allocator: Allocator, options: link.Opti |
| 270 | 266 | } |
| 271 | 267 | } |
| 272 | 268 | |
| 273 | | var lc_buffer = std.ArrayList(u8).init(allocator); |
| 269 | var lc_buffer = std.ArrayList(u8).init(self.allocator); |
| 274 | 270 | defer lc_buffer.deinit(); |
| 275 | 271 | const lc_writer = lc_buffer.writer(); |
| 276 | 272 | var ncmds: u32 = 0; |
| 277 | 273 | |
| 278 | | self.finalizeDwarfSegment(); |
| 279 | | try self.writeLinkeditSegmentData(&ncmds, lc_writer); |
| 274 | self.finalizeDwarfSegment(macho_file); |
| 275 | try self.writeLinkeditSegmentData(macho_file, &ncmds, lc_writer); |
| 280 | 276 | |
| 281 | 277 | { |
| 282 | | try lc_writer.writeStruct(self.base.uuid); |
| 278 | try lc_writer.writeStruct(macho_file.uuid); |
| 283 | 279 | ncmds += 1; |
| 284 | 280 | } |
| 285 | 281 | |
| 286 | | var headers_buf = std.ArrayList(u8).init(allocator); |
| 282 | var headers_buf = std.ArrayList(u8).init(self.allocator); |
| 287 | 283 | defer headers_buf.deinit(); |
| 288 | | try self.writeSegmentHeaders(&ncmds, headers_buf.writer()); |
| 284 | try self.writeSegmentHeaders(macho_file, &ncmds, headers_buf.writer()); |
| 289 | 285 | |
| 290 | 286 | try self.file.pwriteAll(headers_buf.items, @sizeOf(macho.mach_header_64)); |
| 291 | 287 | try self.file.pwriteAll(lc_buffer.items, @sizeOf(macho.mach_header_64) + headers_buf.items.len); |
| 292 | 288 | |
| 293 | | try self.writeHeader(ncmds, @intCast(u32, lc_buffer.items.len + headers_buf.items.len)); |
| 289 | try self.writeHeader( |
| 290 | macho_file, |
| 291 | ncmds, |
| 292 | @intCast(u32, lc_buffer.items.len + headers_buf.items.len), |
| 293 | ); |
| 294 | 294 | |
| 295 | 295 | assert(!self.debug_abbrev_section_dirty); |
| 296 | 296 | assert(!self.debug_aranges_section_dirty); |
| 297 | 297 | assert(!self.debug_string_table_dirty); |
| 298 | 298 | } |
| 299 | 299 | |
| 300 | | pub fn deinit(self: *DebugSymbols, allocator: Allocator) void { |
| 300 | pub fn deinit(self: *DebugSymbols) void { |
| 301 | const gpa = self.allocator; |
| 301 | 302 | self.file.close(); |
| 302 | | self.segments.deinit(allocator); |
| 303 | | self.sections.deinit(allocator); |
| 303 | self.segments.deinit(gpa); |
| 304 | self.sections.deinit(gpa); |
| 304 | 305 | self.dwarf.deinit(); |
| 305 | | self.strtab.deinit(allocator); |
| 306 | | self.relocs.deinit(allocator); |
| 306 | self.strtab.deinit(gpa); |
| 307 | self.relocs.deinit(gpa); |
| 307 | 308 | } |
| 308 | 309 | |
| 309 | 310 | pub fn swapRemoveRelocs(self: *DebugSymbols, target: u32) void { |
| ... | ... | @@ -319,16 +320,22 @@ pub fn swapRemoveRelocs(self: *DebugSymbols, target: u32) void { |
| 319 | 320 | } |
| 320 | 321 | } |
| 321 | 322 | |
| 322 | | fn finalizeDwarfSegment(self: *DebugSymbols) void { |
| 323 | fn finalizeDwarfSegment(self: *DebugSymbols, macho_file: *MachO) void { |
| 323 | 324 | const base_vmaddr = blk: { |
| 324 | | const last_seg = self.base.getLinkeditSegmentPtr(); |
| 325 | // Note that we purposely take the last VM address of the MachO binary including |
| 326 | // the binary's LINKEDIT segment. This is in contrast to how dsymutil does it |
| 327 | // which overwrites the the address space taken by the original MachO binary, |
| 328 | // however at the cost of having LINKEDIT preceed DWARF in dSYM binary which we |
| 329 | // do not want as we want to be able to incrementally move DWARF sections in the |
| 330 | // file as we please. |
| 331 | const last_seg = macho_file.getLinkeditSegmentPtr(); |
| 325 | 332 | break :blk last_seg.vmaddr + last_seg.vmsize; |
| 326 | 333 | }; |
| 327 | 334 | const dwarf_segment = self.getDwarfSegmentPtr(); |
| 328 | 335 | const aligned_size = mem.alignForwardGeneric( |
| 329 | 336 | u64, |
| 330 | 337 | dwarf_segment.filesize, |
| 331 | | self.base.page_size, |
| 338 | self.page_size, |
| 332 | 339 | ); |
| 333 | 340 | dwarf_segment.vmaddr = base_vmaddr; |
| 334 | 341 | dwarf_segment.filesize = aligned_size; |
| ... | ... | @@ -338,21 +345,21 @@ fn finalizeDwarfSegment(self: *DebugSymbols) void { |
| 338 | 345 | linkedit.vmaddr = mem.alignForwardGeneric( |
| 339 | 346 | u64, |
| 340 | 347 | dwarf_segment.vmaddr + aligned_size, |
| 341 | | self.base.page_size, |
| 348 | self.page_size, |
| 342 | 349 | ); |
| 343 | 350 | linkedit.fileoff = mem.alignForwardGeneric( |
| 344 | 351 | u64, |
| 345 | 352 | dwarf_segment.fileoff + aligned_size, |
| 346 | | self.base.page_size, |
| 353 | self.page_size, |
| 347 | 354 | ); |
| 348 | 355 | log.debug("found __LINKEDIT segment free space at 0x{x}", .{linkedit.fileoff}); |
| 349 | 356 | } |
| 350 | 357 | |
| 351 | | fn writeSegmentHeaders(self: *DebugSymbols, ncmds: *u32, writer: anytype) !void { |
| 358 | fn writeSegmentHeaders(self: *DebugSymbols, macho_file: *MachO, ncmds: *u32, writer: anytype) !void { |
| 352 | 359 | // Write segment/section headers from the binary file first. |
| 353 | | const end = self.base.linkedit_segment_cmd_index.?; |
| 354 | | for (self.base.segments.items[0..end]) |seg, i| { |
| 355 | | const indexes = self.base.getSectionIndexes(@intCast(u8, i)); |
| 360 | const end = macho_file.linkedit_segment_cmd_index.?; |
| 361 | for (macho_file.segments.items[0..end]) |seg, i| { |
| 362 | const indexes = macho_file.getSectionIndexes(@intCast(u8, i)); |
| 356 | 363 | var out_seg = seg; |
| 357 | 364 | out_seg.fileoff = 0; |
| 358 | 365 | out_seg.filesize = 0; |
| ... | ... | @@ -361,7 +368,7 @@ fn writeSegmentHeaders(self: *DebugSymbols, ncmds: *u32, writer: anytype) !void |
| 361 | 368 | |
| 362 | 369 | // Update section headers count; any section with size of 0 is excluded |
| 363 | 370 | // since it doesn't have any data in the final binary file. |
| 364 | | for (self.base.sections.items(.header)[indexes.start..indexes.end]) |header| { |
| 371 | for (macho_file.sections.items(.header)[indexes.start..indexes.end]) |header| { |
| 365 | 372 | if (header.size == 0) continue; |
| 366 | 373 | out_seg.cmdsize += @sizeOf(macho.section_64); |
| 367 | 374 | out_seg.nsects += 1; |
| ... | ... | @@ -372,7 +379,7 @@ fn writeSegmentHeaders(self: *DebugSymbols, ncmds: *u32, writer: anytype) !void |
| 372 | 379 | mem.eql(u8, out_seg.segName(), "__DATA"))) continue; |
| 373 | 380 | |
| 374 | 381 | try writer.writeStruct(out_seg); |
| 375 | | for (self.base.sections.items(.header)[indexes.start..indexes.end]) |header| { |
| 382 | for (macho_file.sections.items(.header)[indexes.start..indexes.end]) |header| { |
| 376 | 383 | if (header.size == 0) continue; |
| 377 | 384 | var out_header = header; |
| 378 | 385 | out_header.offset = 0; |
| ... | ... | @@ -392,11 +399,11 @@ fn writeSegmentHeaders(self: *DebugSymbols, ncmds: *u32, writer: anytype) !void |
| 392 | 399 | } |
| 393 | 400 | } |
| 394 | 401 | |
| 395 | | fn writeHeader(self: *DebugSymbols, ncmds: u32, sizeofcmds: u32) !void { |
| 402 | fn writeHeader(self: *DebugSymbols, macho_file: *MachO, ncmds: u32, sizeofcmds: u32) !void { |
| 396 | 403 | var header: macho.mach_header_64 = .{}; |
| 397 | 404 | header.filetype = macho.MH_DSYM; |
| 398 | 405 | |
| 399 | | switch (self.base.base.options.target.cpu.arch) { |
| 406 | switch (macho_file.base.options.target.cpu.arch) { |
| 400 | 407 | .aarch64 => { |
| 401 | 408 | header.cputype = macho.CPU_TYPE_ARM64; |
| 402 | 409 | header.cpusubtype = macho.CPU_SUBTYPE_ARM_ALL; |
| ... | ... | @@ -427,7 +434,12 @@ pub fn allocatedSize(self: *DebugSymbols, start: u64) u64 { |
| 427 | 434 | return min_pos - start; |
| 428 | 435 | } |
| 429 | 436 | |
| 430 | | fn writeLinkeditSegmentData(self: *DebugSymbols, ncmds: *u32, lc_writer: anytype) !void { |
| 437 | fn writeLinkeditSegmentData( |
| 438 | self: *DebugSymbols, |
| 439 | macho_file: *MachO, |
| 440 | ncmds: *u32, |
| 441 | lc_writer: anytype, |
| 442 | ) !void { |
| 431 | 443 | const tracy = trace(@src()); |
| 432 | 444 | defer tracy.end(); |
| 433 | 445 | |
| ... | ... | @@ -438,43 +450,43 @@ fn writeLinkeditSegmentData(self: *DebugSymbols, ncmds: *u32, lc_writer: anytype |
| 438 | 450 | .stroff = 0, |
| 439 | 451 | .strsize = 0, |
| 440 | 452 | }; |
| 441 | | try self.writeSymtab(&symtab_cmd); |
| 453 | try self.writeSymtab(macho_file, &symtab_cmd); |
| 442 | 454 | try self.writeStrtab(&symtab_cmd); |
| 443 | 455 | try lc_writer.writeStruct(symtab_cmd); |
| 444 | 456 | ncmds.* += 1; |
| 445 | 457 | |
| 446 | 458 | const seg = &self.segments.items[self.linkedit_segment_cmd_index.?]; |
| 447 | | const aligned_size = mem.alignForwardGeneric(u64, seg.filesize, self.base.page_size); |
| 459 | const aligned_size = mem.alignForwardGeneric(u64, seg.filesize, self.page_size); |
| 448 | 460 | seg.vmsize = aligned_size; |
| 449 | 461 | } |
| 450 | 462 | |
| 451 | | fn writeSymtab(self: *DebugSymbols, lc: *macho.symtab_command) !void { |
| 463 | fn writeSymtab(self: *DebugSymbols, macho_file: *MachO, lc: *macho.symtab_command) !void { |
| 452 | 464 | const tracy = trace(@src()); |
| 453 | 465 | defer tracy.end(); |
| 454 | 466 | |
| 455 | | const gpa = self.base.base.allocator; |
| 467 | const gpa = self.allocator; |
| 456 | 468 | |
| 457 | 469 | var locals = std.ArrayList(macho.nlist_64).init(gpa); |
| 458 | 470 | defer locals.deinit(); |
| 459 | 471 | |
| 460 | | for (self.base.locals.items) |sym, sym_id| { |
| 472 | for (macho_file.locals.items) |sym, sym_id| { |
| 461 | 473 | if (sym.n_strx == 0) continue; // no name, skip |
| 462 | 474 | const sym_loc = MachO.SymbolWithLoc{ .sym_index = @intCast(u32, sym_id), .file = null }; |
| 463 | | if (self.base.symbolIsTemp(sym_loc)) continue; // local temp symbol, skip |
| 464 | | if (self.base.getGlobal(self.base.getSymbolName(sym_loc)) != null) continue; // global symbol is either an export or import, skip |
| 475 | if (macho_file.symbolIsTemp(sym_loc)) continue; // local temp symbol, skip |
| 476 | if (macho_file.getGlobal(macho_file.getSymbolName(sym_loc)) != null) continue; // global symbol is either an export or import, skip |
| 465 | 477 | var out_sym = sym; |
| 466 | | out_sym.n_strx = try self.strtab.insert(gpa, self.base.getSymbolName(sym_loc)); |
| 478 | out_sym.n_strx = try self.strtab.insert(gpa, macho_file.getSymbolName(sym_loc)); |
| 467 | 479 | try locals.append(out_sym); |
| 468 | 480 | } |
| 469 | 481 | |
| 470 | 482 | var exports = std.ArrayList(macho.nlist_64).init(gpa); |
| 471 | 483 | defer exports.deinit(); |
| 472 | 484 | |
| 473 | | for (self.base.globals.items) |global| { |
| 474 | | const sym = self.base.getSymbol(global); |
| 485 | for (macho_file.globals.items) |global| { |
| 486 | const sym = macho_file.getSymbol(global); |
| 475 | 487 | if (sym.undf()) continue; // import, skip |
| 476 | 488 | var out_sym = sym; |
| 477 | | out_sym.n_strx = try self.strtab.insert(gpa, self.base.getSymbolName(global)); |
| 489 | out_sym.n_strx = try self.strtab.insert(gpa, macho_file.getSymbolName(global)); |
| 478 | 490 | try exports.append(out_sym); |
| 479 | 491 | } |
| 480 | 492 | |