From c8fcd2ff2c032b2de8cc1a57e075552d1cab35df Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 1 Jul 2025 18:14:45 -0700 Subject: MachO: update to new std.io APIs --- src/link/MachO/Symbol.zig | 99 +++++++++++++++++++---------------------------- 1 file changed, 40 insertions(+), 59 deletions(-) (limited to 'src/link/MachO/Symbol.zig') diff --git a/src/link/MachO/Symbol.zig b/src/link/MachO/Symbol.zig index be126b0963..b205740055 100644 --- a/src/link/MachO/Symbol.zig +++ b/src/link/MachO/Symbol.zig @@ -286,71 +286,51 @@ pub fn setOutputSym(symbol: Symbol, macho_file: *MachO, out: *macho.nlist_64) vo } } -pub fn format( - symbol: Symbol, - comptime unused_fmt_string: []const u8, - options: std.fmt.FormatOptions, - writer: anytype, -) !void { - _ = symbol; - _ = unused_fmt_string; - _ = options; - _ = writer; - @compileError("do not format symbols directly"); -} - -const FormatContext = struct { - symbol: Symbol, - macho_file: *MachO, -}; - -pub fn fmt(symbol: Symbol, macho_file: *MachO) std.fmt.Formatter(format2) { +pub fn fmt(symbol: Symbol, macho_file: *MachO) std.fmt.Formatter(Format, Format.default) { return .{ .data = .{ .symbol = symbol, .macho_file = macho_file, } }; } -fn format2( - ctx: FormatContext, - comptime unused_fmt_string: []const u8, - options: std.fmt.FormatOptions, - writer: anytype, -) !void { - _ = options; - _ = unused_fmt_string; - const symbol = ctx.symbol; - try writer.print("%{d} : {s} : @{x}", .{ - symbol.nlist_idx, - symbol.getName(ctx.macho_file), - symbol.getAddress(.{}, ctx.macho_file), - }); - if (symbol.getFile(ctx.macho_file)) |file| { - if (symbol.getOutputSectionIndex(ctx.macho_file) != 0) { - try writer.print(" : sect({d})", .{symbol.getOutputSectionIndex(ctx.macho_file)}); - } - if (symbol.getAtom(ctx.macho_file)) |atom| { - try writer.print(" : atom({d})", .{atom.atom_index}); - } - var buf: [3]u8 = .{'_'} ** 3; - if (symbol.flags.@"export") buf[0] = 'E'; - if (symbol.flags.import) buf[1] = 'I'; - switch (symbol.visibility) { - .local => buf[2] = 'L', - .hidden => buf[2] = 'H', - .global => buf[2] = 'G', - } - try writer.print(" : {s}", .{&buf}); - if (symbol.flags.weak) try writer.writeAll(" : weak"); - if (symbol.isSymbolStab(ctx.macho_file)) try writer.writeAll(" : stab"); - switch (file) { - .zig_object => |x| try writer.print(" : zig_object({d})", .{x.index}), - .internal => |x| try writer.print(" : internal({d})", .{x.index}), - .object => |x| try writer.print(" : object({d})", .{x.index}), - .dylib => |x| try writer.print(" : dylib({d})", .{x.index}), - } - } else try writer.writeAll(" : unresolved"); -} +const Format = struct { + symbol: Symbol, + macho_file: *MachO, + + fn format2(f: Format, w: *Writer) Writer.Error!void { + const symbol = f.symbol; + try w.print("%{d} : {s} : @{x}", .{ + symbol.nlist_idx, + symbol.getName(f.macho_file), + symbol.getAddress(.{}, f.macho_file), + }); + if (symbol.getFile(f.macho_file)) |file| { + if (symbol.getOutputSectionIndex(f.macho_file) != 0) { + try w.print(" : sect({d})", .{symbol.getOutputSectionIndex(f.macho_file)}); + } + if (symbol.getAtom(f.macho_file)) |atom| { + try w.print(" : atom({d})", .{atom.atom_index}); + } + var buf: [3]u8 = .{'_'} ** 3; + if (symbol.flags.@"export") buf[0] = 'E'; + if (symbol.flags.import) buf[1] = 'I'; + switch (symbol.visibility) { + .local => buf[2] = 'L', + .hidden => buf[2] = 'H', + .global => buf[2] = 'G', + } + try w.print(" : {s}", .{&buf}); + if (symbol.flags.weak) try w.writeAll(" : weak"); + if (symbol.isSymbolStab(f.macho_file)) try w.writeAll(" : stab"); + switch (file) { + .zig_object => |x| try w.print(" : zig_object({d})", .{x.index}), + .internal => |x| try w.print(" : internal({d})", .{x.index}), + .object => |x| try w.print(" : object({d})", .{x.index}), + .dylib => |x| try w.print(" : dylib({d})", .{x.index}), + } + } else try w.writeAll(" : unresolved"); + } +}; pub const Flags = packed struct { /// Whether the symbol is imported at runtime. @@ -437,6 +417,7 @@ pub const Index = u32; const assert = std.debug.assert; const macho = std.macho; const std = @import("std"); +const Writer = std.io.Writer; const Atom = @import("Atom.zig"); const File = @import("file.zig").File; -- cgit v1.2.3