authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-12-04 13:05:42+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-12-04 16:58:45+01:00
logaa8b26cf25c0d0e5e50d51f993147be8773a9c1e
tree95fa03ffc9fca856de5ea716691350f6463455c5
parentd67de87baa9ebc45f51e3a83fed761e03367f904

dsym: refactor API - do not store ptr to MachO


3 files changed, 85 insertions(+), 72 deletions(-)

src/link/Dwarf.zig+1-1
......@@ -1318,7 +1318,7 @@ pub fn commitDeclState(
13181318 .macho => {
13191319 const macho_file = file.cast(File.MachO).?;
13201320 const d_sym = &macho_file.d_sym.?;
1321 try d_sym.relocs.append(d_sym.base.base.allocator, .{
1321 try d_sym.relocs.append(d_sym.allocator, .{
13221322 .type = switch (reloc.type) {
13231323 .direct_load => .direct_load,
13241324 .got_load => .got_load,
src/link/MachO.zig+5-4
......@@ -347,9 +347,10 @@ pub fn openPath(allocator: Allocator, options: link.Options) !*MachO {
347347 });
348348
349349 self.d_sym = .{
350 .base = self,
350 .allocator = allocator,
351351 .dwarf = link.File.Dwarf.init(allocator, .macho, options.target),
352352 .file = d_sym_file,
353 .page_size = self.page_size,
353354 };
354355 }
355356
......@@ -366,7 +367,7 @@ pub fn openPath(allocator: Allocator, options: link.Options) !*MachO {
366367 try self.populateMissingMetadata();
367368
368369 if (self.d_sym) |*d_sym| {
369 try d_sym.populateMissingMetadata(allocator);
370 try d_sym.populateMissingMetadata();
370371 }
371372
372373 return self;
......@@ -629,7 +630,7 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No
629630
630631 if (self.d_sym) |*d_sym| {
631632 // Flush debug symbols bundle.
632 try d_sym.flushModule(self.base.allocator, self.base.options);
633 try d_sym.flushModule(self);
633634 }
634635
635636 // if (build_options.enable_link_snapshots) {
......@@ -1900,7 +1901,7 @@ pub fn deinit(self: *MachO) void {
19001901 }
19011902
19021903 if (self.d_sym) |*d_sym| {
1903 d_sym.deinit(gpa);
1904 d_sym.deinit();
19041905 }
19051906
19061907 self.got_entries.deinit(gpa);
src/link/MachO/DebugSymbols.zig+79-67
......@@ -20,9 +20,10 @@ const Module = @import("../../Module.zig");
2020const StringTable = @import("../strtab.zig").StringTable;
2121const Type = @import("../../type.zig").Type;
2222
23base: *MachO,
23allocator: Allocator,
2424dwarf: Dwarf,
2525file: fs.File,
26page_size: u16,
2627
2728segments: std.ArrayListUnmanaged(macho.segment_command_64) = .{},
2829sections: std.ArrayListUnmanaged(macho.section_64) = .{},
......@@ -59,21 +60,17 @@ pub const Reloc = struct {
5960
6061/// You must call this function *after* `MachO.populateMissingMetadata()`
6162/// has been called to get a viable debug symbols output.
62pub fn populateMissingMetadata(self: *DebugSymbols, gpa: Allocator) !void {
63pub fn populateMissingMetadata(self: *DebugSymbols) !void {
6364 if (self.dwarf_segment_cmd_index == null) {
6465 self.dwarf_segment_cmd_index = @intCast(u8, self.segments.items.len);
6566
66 const off = @intCast(u64, self.base.page_size);
67 const off = @intCast(u64, self.page_size);
6768 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);
7370
7471 log.debug("found __DWARF segment free space 0x{x} to 0x{x}", .{ off, off + needed_size });
7572
76 try self.segments.append(gpa, .{
73 try self.segments.append(self.allocator, .{
7774 .segname = makeStaticString("__DWARF"),
7875 .vmsize = needed_size,
7976 .fileoff = off,
......@@ -114,7 +111,7 @@ pub fn populateMissingMetadata(self: *DebugSymbols, gpa: Allocator) !void {
114111
115112 if (self.linkedit_segment_cmd_index == null) {
116113 self.linkedit_segment_cmd_index = @intCast(u8, self.segments.items.len);
117 try self.segments.append(gpa, .{
114 try self.segments.append(self.allocator, .{
118115 .segname = makeStaticString("__LINKEDIT"),
119116 .maxprot = macho.PROT.READ,
120117 .initprot = macho.PROT.READ,
......@@ -124,8 +121,6 @@ pub fn populateMissingMetadata(self: *DebugSymbols, gpa: Allocator) !void {
124121}
125122
126123fn allocateSection(self: *DebugSymbols, sectname: []const u8, size: u64, alignment: u16) !u8 {
127 const gpa = self.base.base.allocator;
128
129124 const segment = self.getDwarfSegmentPtr();
130125 var sect = macho.section_64{
131126 .sectname = makeStaticString(sectname),
......@@ -147,7 +142,7 @@ fn allocateSection(self: *DebugSymbols, sectname: []const u8, size: u64, alignme
147142 sect.offset = @intCast(u32, off);
148143
149144 const index = @intCast(u8, self.sections.items.len);
150 try self.sections.append(gpa, sect);
145 try self.sections.append(self.allocator, sect);
151146 segment.cmdsize += @sizeOf(macho.section_64);
152147 segment.nsects += 1;
153148
......@@ -175,34 +170,35 @@ pub fn findFreeSpace(self: *DebugSymbols, object_size: u64, min_alignment: u64)
175170 return offset;
176171}
177172
178pub 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.
173pub 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;
181177 const module = options.module orelse return error.LinkingWithoutZigSourceUnimplemented;
182178
183179 for (self.relocs.items) |*reloc| {
184180 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 }),
186182 .got_load => blk: {
187 const got_index = self.base.got_entries_table.get(.{
183 const got_index = macho_file.got_entries_table.get(.{
188184 .sym_index = reloc.target,
189185 .file = null,
190186 }).?;
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);
193189 },
194190 };
195191 if (sym.n_value == reloc.prev_vaddr) continue;
196192
197193 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 }),
199195 .got_load => blk: {
200 const got_index = self.base.got_entries_table.get(.{
196 const got_index = macho_file.got_entries_table.get(.{
201197 .sym_index = reloc.target,
202198 .file = null,
203199 }).?;
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);
206202 },
207203 };
208204 const sect = &self.sections.items[self.debug_info_section_index.?];
......@@ -218,30 +214,30 @@ pub fn flushModule(self: *DebugSymbols, allocator: Allocator, options: link.Opti
218214 }
219215
220216 if (self.debug_abbrev_section_dirty) {
221 try self.dwarf.writeDbgAbbrev(&self.base.base);
217 try self.dwarf.writeDbgAbbrev(&macho_file.base);
222218 self.debug_abbrev_section_dirty = false;
223219 }
224220
225221 if (self.debug_info_header_dirty) {
226222 // Currently only one compilation unit is supported, so the address range is simply
227223 // 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.?];
229225 const low_pc = text_section.addr;
230226 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);
232228 self.debug_info_header_dirty = false;
233229 }
234230
235231 if (self.debug_aranges_section_dirty) {
236232 // Currently only one compilation unit is supported, so the address range is simply
237233 // 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);
240236 self.debug_aranges_section_dirty = false;
241237 }
242238
243239 if (self.debug_line_header_dirty) {
244 try self.dwarf.writeDbgLineHeader(&self.base.base, module);
240 try self.dwarf.writeDbgLineHeader(&macho_file.base, module);
245241 self.debug_line_header_dirty = false;
246242 }
247243
......@@ -270,40 +266,45 @@ pub fn flushModule(self: *DebugSymbols, allocator: Allocator, options: link.Opti
270266 }
271267 }
272268
273 var lc_buffer = std.ArrayList(u8).init(allocator);
269 var lc_buffer = std.ArrayList(u8).init(self.allocator);
274270 defer lc_buffer.deinit();
275271 const lc_writer = lc_buffer.writer();
276272 var ncmds: u32 = 0;
277273
278 self.finalizeDwarfSegment();
279 try self.writeLinkeditSegmentData(&ncmds, lc_writer);
274 self.finalizeDwarfSegment(macho_file);
275 try self.writeLinkeditSegmentData(macho_file, &ncmds, lc_writer);
280276
281277 {
282 try lc_writer.writeStruct(self.base.uuid);
278 try lc_writer.writeStruct(macho_file.uuid);
283279 ncmds += 1;
284280 }
285281
286 var headers_buf = std.ArrayList(u8).init(allocator);
282 var headers_buf = std.ArrayList(u8).init(self.allocator);
287283 defer headers_buf.deinit();
288 try self.writeSegmentHeaders(&ncmds, headers_buf.writer());
284 try self.writeSegmentHeaders(macho_file, &ncmds, headers_buf.writer());
289285
290286 try self.file.pwriteAll(headers_buf.items, @sizeOf(macho.mach_header_64));
291287 try self.file.pwriteAll(lc_buffer.items, @sizeOf(macho.mach_header_64) + headers_buf.items.len);
292288
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 );
294294
295295 assert(!self.debug_abbrev_section_dirty);
296296 assert(!self.debug_aranges_section_dirty);
297297 assert(!self.debug_string_table_dirty);
298298}
299299
300pub fn deinit(self: *DebugSymbols, allocator: Allocator) void {
300pub fn deinit(self: *DebugSymbols) void {
301 const gpa = self.allocator;
301302 self.file.close();
302 self.segments.deinit(allocator);
303 self.sections.deinit(allocator);
303 self.segments.deinit(gpa);
304 self.sections.deinit(gpa);
304305 self.dwarf.deinit();
305 self.strtab.deinit(allocator);
306 self.relocs.deinit(allocator);
306 self.strtab.deinit(gpa);
307 self.relocs.deinit(gpa);
307308}
308309
309310pub fn swapRemoveRelocs(self: *DebugSymbols, target: u32) void {
......@@ -319,16 +320,22 @@ pub fn swapRemoveRelocs(self: *DebugSymbols, target: u32) void {
319320 }
320321}
321322
322fn finalizeDwarfSegment(self: *DebugSymbols) void {
323fn finalizeDwarfSegment(self: *DebugSymbols, macho_file: *MachO) void {
323324 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();
325332 break :blk last_seg.vmaddr + last_seg.vmsize;
326333 };
327334 const dwarf_segment = self.getDwarfSegmentPtr();
328335 const aligned_size = mem.alignForwardGeneric(
329336 u64,
330337 dwarf_segment.filesize,
331 self.base.page_size,
338 self.page_size,
332339 );
333340 dwarf_segment.vmaddr = base_vmaddr;
334341 dwarf_segment.filesize = aligned_size;
......@@ -338,21 +345,21 @@ fn finalizeDwarfSegment(self: *DebugSymbols) void {
338345 linkedit.vmaddr = mem.alignForwardGeneric(
339346 u64,
340347 dwarf_segment.vmaddr + aligned_size,
341 self.base.page_size,
348 self.page_size,
342349 );
343350 linkedit.fileoff = mem.alignForwardGeneric(
344351 u64,
345352 dwarf_segment.fileoff + aligned_size,
346 self.base.page_size,
353 self.page_size,
347354 );
348355 log.debug("found __LINKEDIT segment free space at 0x{x}", .{linkedit.fileoff});
349356}
350357
351fn writeSegmentHeaders(self: *DebugSymbols, ncmds: *u32, writer: anytype) !void {
358fn writeSegmentHeaders(self: *DebugSymbols, macho_file: *MachO, ncmds: *u32, writer: anytype) !void {
352359 // 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));
356363 var out_seg = seg;
357364 out_seg.fileoff = 0;
358365 out_seg.filesize = 0;
......@@ -361,7 +368,7 @@ fn writeSegmentHeaders(self: *DebugSymbols, ncmds: *u32, writer: anytype) !void
361368
362369 // Update section headers count; any section with size of 0 is excluded
363370 // 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| {
365372 if (header.size == 0) continue;
366373 out_seg.cmdsize += @sizeOf(macho.section_64);
367374 out_seg.nsects += 1;
......@@ -372,7 +379,7 @@ fn writeSegmentHeaders(self: *DebugSymbols, ncmds: *u32, writer: anytype) !void
372379 mem.eql(u8, out_seg.segName(), "__DATA"))) continue;
373380
374381 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| {
376383 if (header.size == 0) continue;
377384 var out_header = header;
378385 out_header.offset = 0;
......@@ -392,11 +399,11 @@ fn writeSegmentHeaders(self: *DebugSymbols, ncmds: *u32, writer: anytype) !void
392399 }
393400}
394401
395fn writeHeader(self: *DebugSymbols, ncmds: u32, sizeofcmds: u32) !void {
402fn writeHeader(self: *DebugSymbols, macho_file: *MachO, ncmds: u32, sizeofcmds: u32) !void {
396403 var header: macho.mach_header_64 = .{};
397404 header.filetype = macho.MH_DSYM;
398405
399 switch (self.base.base.options.target.cpu.arch) {
406 switch (macho_file.base.options.target.cpu.arch) {
400407 .aarch64 => {
401408 header.cputype = macho.CPU_TYPE_ARM64;
402409 header.cpusubtype = macho.CPU_SUBTYPE_ARM_ALL;
......@@ -427,7 +434,12 @@ pub fn allocatedSize(self: *DebugSymbols, start: u64) u64 {
427434 return min_pos - start;
428435}
429436
430fn writeLinkeditSegmentData(self: *DebugSymbols, ncmds: *u32, lc_writer: anytype) !void {
437fn writeLinkeditSegmentData(
438 self: *DebugSymbols,
439 macho_file: *MachO,
440 ncmds: *u32,
441 lc_writer: anytype,
442) !void {
431443 const tracy = trace(@src());
432444 defer tracy.end();
433445
......@@ -438,43 +450,43 @@ fn writeLinkeditSegmentData(self: *DebugSymbols, ncmds: *u32, lc_writer: anytype
438450 .stroff = 0,
439451 .strsize = 0,
440452 };
441 try self.writeSymtab(&symtab_cmd);
453 try self.writeSymtab(macho_file, &symtab_cmd);
442454 try self.writeStrtab(&symtab_cmd);
443455 try lc_writer.writeStruct(symtab_cmd);
444456 ncmds.* += 1;
445457
446458 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);
448460 seg.vmsize = aligned_size;
449461}
450462
451fn writeSymtab(self: *DebugSymbols, lc: *macho.symtab_command) !void {
463fn writeSymtab(self: *DebugSymbols, macho_file: *MachO, lc: *macho.symtab_command) !void {
452464 const tracy = trace(@src());
453465 defer tracy.end();
454466
455 const gpa = self.base.base.allocator;
467 const gpa = self.allocator;
456468
457469 var locals = std.ArrayList(macho.nlist_64).init(gpa);
458470 defer locals.deinit();
459471
460 for (self.base.locals.items) |sym, sym_id| {
472 for (macho_file.locals.items) |sym, sym_id| {
461473 if (sym.n_strx == 0) continue; // no name, skip
462474 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
465477 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));
467479 try locals.append(out_sym);
468480 }
469481
470482 var exports = std.ArrayList(macho.nlist_64).init(gpa);
471483 defer exports.deinit();
472484
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);
475487 if (sym.undf()) continue; // import, skip
476488 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));
478490 try exports.append(out_sym);
479491 }
480492