Skip to content

Commit

Permalink
macho: add more tracy hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
kubkon committed Dec 21, 2023
1 parent 5c1d917 commit 628b379
Show file tree
Hide file tree
Showing 10 changed files with 201 additions and 19 deletions.
49 changes: 49 additions & 0 deletions src/MachO.zig
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,9 @@ fn parseObject(self: *MachO, arena: Allocator, obj: LinkObject) !bool {
}

fn parseArchive(self: *MachO, arena: Allocator, obj: LinkObject) !bool {
const tracy = trace(@src());
defer tracy.end();

const gpa = self.base.allocator;

const file = try std.fs.cwd().openFile(obj.path, .{});
Expand Down Expand Up @@ -861,6 +864,9 @@ fn parseDependentDylibs(
lib_dirs: []const []const u8,
framework_dirs: []const []const u8,
) !void {
const tracy = trace(@src());
defer tracy.end();

const gpa = self.base.allocator;

if (self.dylibs.items.len == 0) return;
Expand Down Expand Up @@ -987,6 +993,9 @@ fn parseDependentDylibs(
/// 5. Remove references to dead objects/shared objects
/// 6. Re-run symbol resolution on pruned objects and shared objects sets.
pub fn resolveSymbols(self: *MachO) !void {
const tracy = trace(@src());
defer tracy.end();

// Resolve symbols on the set of all objects and shared objects (even if some are unneeded).
for (self.objects.items) |index| self.getFile(index).?.resolveSymbols(self);
for (self.dylibs.items) |index| self.getFile(index).?.resolveSymbols(self);
Expand All @@ -1013,6 +1022,9 @@ pub fn resolveSymbols(self: *MachO) !void {
}

fn markLive(self: *MachO) void {
const tracy = trace(@src());
defer tracy.end();

for (self.undefined_symbols.items) |index| {
if (self.getSymbol(index).getFile(self)) |file| {
if (file == .object) file.object.alive = true;
Expand Down Expand Up @@ -1241,6 +1253,9 @@ fn claimUnresolved(self: *MachO) error{OutOfMemory}!void {
}

fn scanRelocs(self: *MachO) !void {
const tracy = trace(@src());
defer tracy.end();

for (self.objects.items) |index| {
try self.getFile(index).?.object.scanRelocs(self);
}
Expand Down Expand Up @@ -1287,6 +1302,9 @@ fn scanRelocs(self: *MachO) !void {
}

fn reportUndefs(self: *MachO) !void {
const tracy = trace(@src());
defer tracy.end();

if (self.options.undefined_treatment == .suppress or
self.options.undefined_treatment == .dynamic_lookup) return;

Expand Down Expand Up @@ -1577,6 +1595,9 @@ fn sortSections(self: *MachO) !void {
}

fn addAtomsToSections(self: *MachO) !void {
const tracy = trace(@src());
defer tracy.end();

for (self.objects.items) |index| {
const object = self.getFile(index).?.object;
for (object.atoms.items) |atom_index| {
Expand Down Expand Up @@ -1611,6 +1632,9 @@ fn addAtomsToSections(self: *MachO) !void {
}

fn generateUnwindInfo(self: *MachO) !void {
const tracy = trace(@src());
defer tracy.end();

if (self.eh_frame_sect_index) |index| {
const sect = &self.sections.items(.header)[index];
sect.size = try eh_frame.calcSize(self);
Expand Down Expand Up @@ -1969,6 +1993,9 @@ fn allocateSyntheticSymbols(self: *MachO) void {
}

fn initDyldInfoSections(self: *MachO) !void {
const tracy = trace(@src());
defer tracy.end();

const gpa = self.base.allocator;

if (self.got_sect_index != null) try self.got.addDyldRelocs(self);
Expand All @@ -1991,6 +2018,9 @@ fn initDyldInfoSections(self: *MachO) !void {
}

fn initExportTrie(self: *MachO) !void {
const tracy = trace(@src());
defer tracy.end();

const gpa = self.base.allocator;
try self.export_trie.init(gpa);

Expand Down Expand Up @@ -2031,6 +2061,9 @@ fn initExportTrie(self: *MachO) !void {
}

fn writeAtoms(self: *MachO) !void {
const tracy = trace(@src());
defer tracy.end();

const gpa = self.base.allocator;
const cpu_arch = self.options.cpu_arch.?;
const slice = self.sections.slice();
Expand Down Expand Up @@ -2072,6 +2105,9 @@ fn writeAtoms(self: *MachO) !void {
}

fn writeUnwindInfo(self: *MachO) !void {
const tracy = trace(@src());
defer tracy.end();

const gpa = self.base.allocator;

if (self.eh_frame_sect_index) |index| {
Expand All @@ -2092,7 +2128,10 @@ fn writeUnwindInfo(self: *MachO) !void {
}

fn finalizeDyldInfoSections(self: *MachO) !void {
const tracy = trace(@src());
defer tracy.end();
const gpa = self.base.allocator;

try self.rebase.finalize(gpa);
try self.bind.finalize(gpa, self);
try self.weak_bind.finalize(gpa, self);
Expand All @@ -2101,6 +2140,9 @@ fn finalizeDyldInfoSections(self: *MachO) !void {
}

fn writeSyntheticSections(self: *MachO) !void {
const tracy = trace(@src());
defer tracy.end();

const gpa = self.base.allocator;

if (self.got_sect_index) |sect_id| {
Expand Down Expand Up @@ -2173,6 +2215,9 @@ fn getNextLinkeditOffset(self: *MachO, alignment: u64) !u64 {
}

fn writeDyldInfoSections(self: *MachO) !void {
const tracy = trace(@src());
defer tracy.end();

const gpa = self.base.allocator;
const cmd = &self.dyld_info_cmd;
var needed_size: u32 = 0;
Expand Down Expand Up @@ -2282,6 +2327,8 @@ fn writeDataInCode(self: *MachO) !void {
}

fn calcSymtabSize(self: *MachO) !void {
const tracy = trace(@src());
defer tracy.end();
const gpa = self.base.allocator;

var nlocals: u32 = 0;
Expand Down Expand Up @@ -2342,6 +2389,8 @@ fn calcSymtabSize(self: *MachO) !void {
}

fn writeSymtab(self: *MachO) !void {
const tracy = trace(@src());
defer tracy.end();
const gpa = self.base.allocator;
const cmd = &self.symtab_cmd;
const off = try self.getNextLinkeditOffset(@alignOf(u64));
Expand Down
7 changes: 7 additions & 0 deletions src/MachO/Atom.zig
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ pub fn initOutputSection(sect: macho.section_64, macho_file: *MachO) !u8 {
}

pub fn scanRelocs(self: Atom, macho_file: *MachO) !void {
const tracy = trace(@src());
defer tracy.end();

const object = self.getFile(macho_file).object;
const relocs = self.getRelocs(macho_file);

Expand Down Expand Up @@ -281,6 +284,9 @@ fn reportUndefSymbol(self: Atom, rel: Relocation, macho_file: *MachO) !bool {
}

pub fn resolveRelocs(self: Atom, macho_file: *MachO, buffer: []u8) !void {
const tracy = trace(@src());
defer tracy.end();

assert(!self.getInputSection(macho_file).isZerofill());
const relocs = self.getRelocs(macho_file);
const file = self.getFile(macho_file);
Expand Down Expand Up @@ -741,6 +747,7 @@ const mem = std.mem;
const log = std.log.scoped(.link);
const relocs_log = std.log.scoped(.relocs);
const std = @import("std");
const trace = @import("../tracy.zig").trace;

const Allocator = mem.Allocator;
const Atom = @This();
Expand Down
13 changes: 13 additions & 0 deletions src/MachO/DwarfInfo.zig
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ fn getString(dw: DwarfInfo, off: u64) [:0]const u8 {
}

fn parseAbbrevTables(dw: *DwarfInfo, allocator: Allocator) !void {
const tracy = trace(@src());
defer tracy.end();

const debug_abbrev = dw.debug_abbrev;
var stream = std.io.fixedBufferStream(debug_abbrev);
var creader = std.io.countingReader(stream.reader());
Expand Down Expand Up @@ -75,6 +78,9 @@ fn parseAbbrevTables(dw: *DwarfInfo, allocator: Allocator) !void {
}

fn parseCompileUnits(dw: *DwarfInfo, allocator: Allocator) !void {
const tracy = trace(@src());
defer tracy.end();

const debug_info = dw.debug_info;
var stream = std.io.fixedBufferStream(debug_info);
var creader = std.io.countingReader(stream.reader());
Expand Down Expand Up @@ -113,6 +119,9 @@ fn parseDie(
parent: ?u32,
creader: anytype,
) anyerror!void {
const tracy = trace(@src());
defer tracy.end();

while (creader.bytes_read < cu.nextCompileUnitOffset()) {
const die = try cu.addDie(allocator);
cu.diePtr(die).* = .{ .code = undefined };
Expand Down Expand Up @@ -149,6 +158,9 @@ fn parseDie(
}

fn advanceByFormSize(cu: *CompileUnit, form: Form, creader: anytype) !void {
const tracy = trace(@src());
defer tracy.end();

const reader = creader.reader();
switch (form) {
dwarf.FORM.strp,
Expand Down Expand Up @@ -449,6 +461,7 @@ const leb = std.leb;
const log = std.log.scoped(.link);
const mem = std.mem;
const std = @import("std");
const trace = @import("../tracy.zig").trace;

const Allocator = mem.Allocator;
const DwarfInfo = @This();
Expand Down
22 changes: 22 additions & 0 deletions src/MachO/Dylib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ pub fn deinit(self: *Dylib, allocator: Allocator) void {
}

pub fn parse(self: *Dylib, macho_file: *MachO) !void {
const tracy = trace(@src());
defer tracy.end();

const gpa = macho_file.base.allocator;
var stream = std.io.fixedBufferStream(self.data);
const reader = stream.reader();
Expand Down Expand Up @@ -133,6 +136,8 @@ fn parseTrieNode(
arena: Allocator,
prefix: []const u8,
) !void {
const tracy = trace(@src());
defer tracy.end();
const size = try it.readULEB128();
if (size > 0) {
const flags = try it.readULEB128();
Expand Down Expand Up @@ -170,6 +175,8 @@ fn parseTrieNode(
}

fn parseTrie(self: *Dylib, data: []const u8, macho_file: *MachO) !void {
const tracy = trace(@src());
defer tracy.end();
const gpa = macho_file.base.allocator;
var arena = std.heap.ArenaAllocator.init(gpa);
defer arena.deinit();
Expand All @@ -185,6 +192,8 @@ pub fn parseTbd(
lib_stub: LibStub,
macho_file: *MachO,
) !void {
const tracy = trace(@src());
defer tracy.end();
const gpa = macho_file.base.allocator;

log.debug("parsing dylib from stub", .{});
Expand Down Expand Up @@ -447,6 +456,9 @@ fn initPlatform(self: *Dylib) void {
}

pub fn resolveSymbols(self: *Dylib, macho_file: *MachO) void {
const tracy = trace(@src());
defer tracy.end();

if (!self.explicit and !self.hoisted) return;

for (self.symbols.items, self.exports.items(.flags)) |index, flags| {
Expand Down Expand Up @@ -483,6 +495,9 @@ pub fn isAlive(self: Dylib, macho_file: *MachO) bool {
}

pub fn markReferenced(self: *Dylib, macho_file: *MachO) void {
const tracy = trace(@src());
defer tracy.end();

for (self.symbols.items) |global_index| {
const global = macho_file.getSymbol(global_index);
const file_ptr = global.getFile(macho_file) orelse continue;
Expand All @@ -494,6 +509,9 @@ pub fn markReferenced(self: *Dylib, macho_file: *MachO) void {
}

pub fn calcSymtabSize(self: *Dylib, macho_file: *MachO) !void {
const tracy = trace(@src());
defer tracy.end();

for (self.symbols.items) |global_index| {
const global = macho_file.getSymbol(global_index);
const file_ptr = global.getFile(macho_file) orelse continue;
Expand All @@ -508,6 +526,9 @@ pub fn calcSymtabSize(self: *Dylib, macho_file: *MachO) !void {
}

pub fn writeSymtab(self: Dylib, macho_file: *MachO) void {
const tracy = trace(@src());
defer tracy.end();

for (self.symbols.items) |global_index| {
const global = macho_file.getSymbol(global_index);
const file = global.getFile(macho_file) orelse continue;
Expand Down Expand Up @@ -794,6 +815,7 @@ const macho = std.macho;
const math = std.math;
const mem = std.mem;
const tapi = @import("../tapi.zig");
const trace = @import("../tracy.zig").trace;
const std = @import("std");

const Allocator = mem.Allocator;
Expand Down
Loading

0 comments on commit 628b379

Please sign in to comment.