authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2025-03-02 16:37:29-05:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2025-03-02 16:39:18-05:00
logd741be512b5c13bec77bde5cc0fd70fbd3535bc0
treedd54188333e02876d57a6f0a8ecff33d8782b0e7
parent725c82582935d2bb84d52d906edb2975827b01ec

link: fixed bugs uncovered by changing the cache mode


3 files changed, 35 insertions(+), 45 deletions(-)

src/link/MachO.zig+6-7
...@@ -3279,13 +3279,13 @@ const InitMetadataOptions = struct {...@@ -3279,13 +3279,13 @@ const InitMetadataOptions = struct {
32793279
3280pub fn closeDebugInfo(self: *MachO) bool {3280pub fn closeDebugInfo(self: *MachO) bool {
3281 const d_sym = &(self.d_sym orelse return false);3281 const d_sym = &(self.d_sym orelse return false);
3282 d_sym.deinit();3282 d_sym.file.?.close();
3283 self.d_sym = null;3283 d_sym.file = null;
3284 return true;3284 return true;
3285}3285}
32863286
3287pub fn reopenDebugInfo(self: *MachO) !void {3287pub fn reopenDebugInfo(self: *MachO) !void {
3288 assert(self.d_sym == null);3288 assert(self.d_sym.?.file == null);
32893289
3290 assert(!self.base.comp.config.use_llvm);3290 assert(!self.base.comp.config.use_llvm);
3291 assert(self.base.comp.config.debug_format == .dwarf);3291 assert(self.base.comp.config.debug_format == .dwarf);
...@@ -3302,13 +3302,10 @@ pub fn reopenDebugInfo(self: *MachO) !void {...@@ -3302,13 +3302,10 @@ pub fn reopenDebugInfo(self: *MachO) !void {
3302 var d_sym_bundle = try self.base.emit.root_dir.handle.makeOpenPath(d_sym_path, .{});3302 var d_sym_bundle = try self.base.emit.root_dir.handle.makeOpenPath(d_sym_path, .{});
3303 defer d_sym_bundle.close();3303 defer d_sym_bundle.close();
33043304
3305 const d_sym_file = try d_sym_bundle.createFile(self.base.emit.sub_path, .{3305 self.d_sym.?.file = try d_sym_bundle.createFile(fs.path.basename(self.base.emit.sub_path), .{
3306 .truncate = false,3306 .truncate = false,
3307 .read = true,3307 .read = true,
3308 });3308 });
3309
3310 self.d_sym = .{ .allocator = gpa, .file = d_sym_file };
3311 try self.d_sym.?.initMetadata(self);
3312}3309}
33133310
3314// TODO: move to ZigObject3311// TODO: move to ZigObject
...@@ -3367,7 +3364,9 @@ fn initMetadata(self: *MachO, options: InitMetadataOptions) !void {...@@ -3367,7 +3364,9 @@ fn initMetadata(self: *MachO, options: InitMetadataOptions) !void {
3367 if (options.zo.dwarf) |*dwarf| {3364 if (options.zo.dwarf) |*dwarf| {
3368 // Create dSYM bundle.3365 // Create dSYM bundle.
3369 log.debug("creating {s}.dSYM bundle", .{options.emit.sub_path});3366 log.debug("creating {s}.dSYM bundle", .{options.emit.sub_path});
3367 self.d_sym = .{ .allocator = self.base.comp.gpa, .file = null };
3370 try self.reopenDebugInfo();3368 try self.reopenDebugInfo();
3369 try self.d_sym.?.initMetadata(self);
3371 try dwarf.initMetadata();3370 try dwarf.initMetadata();
3372 }3371 }
3373 }3372 }
src/link/MachO/DebugSymbols.zig+11-11
...@@ -1,5 +1,5 @@...@@ -1,5 +1,5 @@
1allocator: Allocator,1allocator: Allocator,
2file: fs.File,2file: ?fs.File,
33
4symtab_cmd: macho.symtab_command = .{},4symtab_cmd: macho.symtab_command = .{},
5uuid_cmd: macho.uuid_command = .{ .uuid = [_]u8{0} ** 16 },5uuid_cmd: macho.uuid_command = .{ .uuid = [_]u8{0} ** 16 },
...@@ -118,9 +118,9 @@ pub fn growSection(...@@ -118,9 +118,9 @@ pub fn growSection(
118 });118 });
119119
120 if (requires_file_copy) {120 if (requires_file_copy) {
121 const amt = try self.file.copyRangeAll(121 const amt = try self.file.?.copyRangeAll(
122 sect.offset,122 sect.offset,
123 self.file,123 self.file.?,
124 new_offset,124 new_offset,
125 existing_size,125 existing_size,
126 );126 );
...@@ -129,7 +129,7 @@ pub fn growSection(...@@ -129,7 +129,7 @@ pub fn growSection(
129129
130 sect.offset = @intCast(new_offset);130 sect.offset = @intCast(new_offset);
131 } else if (sect.offset + allocated_size == std.math.maxInt(u64)) {131 } else if (sect.offset + allocated_size == std.math.maxInt(u64)) {
132 try self.file.setEndPos(sect.offset + needed_size);132 try self.file.?.setEndPos(sect.offset + needed_size);
133 }133 }
134134
135 sect.size = needed_size;135 sect.size = needed_size;
...@@ -165,7 +165,7 @@ fn detectAllocCollision(self: *DebugSymbols, start: u64, size: u64) !?u64 {...@@ -165,7 +165,7 @@ fn detectAllocCollision(self: *DebugSymbols, start: u64, size: u64) !?u64 {
165 }165 }
166 }166 }
167167
168 if (at_end) try self.file.setEndPos(end);168 if (at_end) try self.file.?.setEndPos(end);
169 return null;169 return null;
170}170}
171171
...@@ -195,7 +195,7 @@ pub fn flushModule(self: *DebugSymbols, macho_file: *MachO) !void {...@@ -195,7 +195,7 @@ pub fn flushModule(self: *DebugSymbols, macho_file: *MachO) !void {
195 sym_name,195 sym_name,
196 file_offset,196 file_offset,
197 });197 });
198 try self.file.pwriteAll(mem.asBytes(&addr), file_offset);198 try self.file.?.pwriteAll(mem.asBytes(&addr), file_offset);
199 }199 }
200200
201 self.finalizeDwarfSegment(macho_file);201 self.finalizeDwarfSegment(macho_file);
...@@ -208,7 +208,7 @@ pub fn flushModule(self: *DebugSymbols, macho_file: *MachO) !void {...@@ -208,7 +208,7 @@ pub fn flushModule(self: *DebugSymbols, macho_file: *MachO) !void {
208208
209pub fn deinit(self: *DebugSymbols) void {209pub fn deinit(self: *DebugSymbols) void {
210 const gpa = self.allocator;210 const gpa = self.allocator;
211 self.file.close();211 if (self.file) |file| file.close();
212 self.segments.deinit(gpa);212 self.segments.deinit(gpa);
213 self.sections.deinit(gpa);213 self.sections.deinit(gpa);
214 self.relocs.deinit(gpa);214 self.relocs.deinit(gpa);
...@@ -320,7 +320,7 @@ fn writeLoadCommands(self: *DebugSymbols, macho_file: *MachO) !struct { usize, u...@@ -320,7 +320,7 @@ fn writeLoadCommands(self: *DebugSymbols, macho_file: *MachO) !struct { usize, u
320320
321 assert(stream.pos == needed_size);321 assert(stream.pos == needed_size);
322322
323 try self.file.pwriteAll(buffer, @sizeOf(macho.mach_header_64));323 try self.file.?.pwriteAll(buffer, @sizeOf(macho.mach_header_64));
324324
325 return .{ ncmds, buffer.len };325 return .{ ncmds, buffer.len };
326}326}
...@@ -346,7 +346,7 @@ fn writeHeader(self: *DebugSymbols, macho_file: *MachO, ncmds: usize, sizeofcmds...@@ -346,7 +346,7 @@ fn writeHeader(self: *DebugSymbols, macho_file: *MachO, ncmds: usize, sizeofcmds
346346
347 log.debug("writing Mach-O header {}", .{header});347 log.debug("writing Mach-O header {}", .{header});
348348
349 try self.file.pwriteAll(mem.asBytes(&header), 0);349 try self.file.?.pwriteAll(mem.asBytes(&header), 0);
350}350}
351351
352fn allocatedSize(self: *DebugSymbols, start: u64) u64 {352fn allocatedSize(self: *DebugSymbols, start: u64) u64 {
...@@ -404,7 +404,7 @@ pub fn writeSymtab(self: *DebugSymbols, off: u32, macho_file: *MachO) !u32 {...@@ -404,7 +404,7 @@ pub fn writeSymtab(self: *DebugSymbols, off: u32, macho_file: *MachO) !u32 {
404 internal.writeSymtab(macho_file, self);404 internal.writeSymtab(macho_file, self);
405 }405 }
406406
407 try self.file.pwriteAll(mem.sliceAsBytes(self.symtab.items), cmd.symoff);407 try self.file.?.pwriteAll(mem.sliceAsBytes(self.symtab.items), cmd.symoff);
408408
409 return off + cmd.nsyms * @sizeOf(macho.nlist_64);409 return off + cmd.nsyms * @sizeOf(macho.nlist_64);
410}410}
...@@ -412,7 +412,7 @@ pub fn writeSymtab(self: *DebugSymbols, off: u32, macho_file: *MachO) !u32 {...@@ -412,7 +412,7 @@ pub fn writeSymtab(self: *DebugSymbols, off: u32, macho_file: *MachO) !u32 {
412pub fn writeStrtab(self: *DebugSymbols, off: u32) !u32 {412pub fn writeStrtab(self: *DebugSymbols, off: u32) !u32 {
413 const cmd = &self.symtab_cmd;413 const cmd = &self.symtab_cmd;
414 cmd.stroff = off;414 cmd.stroff = off;
415 try self.file.pwriteAll(self.strtab.items, cmd.stroff);415 try self.file.?.pwriteAll(self.strtab.items, cmd.stroff);
416 return off + cmd.strsize;416 return off + cmd.strsize;
417}417}
418418
src/link/SpirV.zig+18-27
...@@ -61,6 +61,18 @@ pub fn createEmpty(...@@ -61,6 +61,18 @@ pub fn createEmpty(
61 const gpa = comp.gpa;61 const gpa = comp.gpa;
62 const target = comp.root_mod.resolved_target.result;62 const target = comp.root_mod.resolved_target.result;
6363
64 assert(!comp.config.use_lld); // Caught by Compilation.Config.resolve
65 assert(!comp.config.use_llvm); // Caught by Compilation.Config.resolve
66 assert(target.ofmt == .spirv); // Caught by Compilation.Config.resolve
67 switch (target.cpu.arch) {
68 .spirv, .spirv32, .spirv64 => {},
69 else => unreachable, // Caught by Compilation.Config.resolve.
70 }
71 switch (target.os.tag) {
72 .opencl, .opengl, .vulkan => {},
73 else => unreachable, // Caught by Compilation.Config.resolve.
74 }
75
64 const self = try arena.create(SpirV);76 const self = try arena.create(SpirV);
65 self.* = .{77 self.* = .{
66 .base = .{78 .base = .{
...@@ -79,15 +91,11 @@ pub fn createEmpty(...@@ -79,15 +91,11 @@ pub fn createEmpty(
79 };91 };
80 errdefer self.deinit();92 errdefer self.deinit();
8193
82 switch (target.cpu.arch) {94 // TODO: read the file and keep valid parts instead of truncating
83 .spirv, .spirv32, .spirv64 => {},95 self.base.file = try emit.root_dir.handle.createFile(emit.sub_path, .{
84 else => unreachable, // Caught by Compilation.Config.resolve.96 .truncate = true,
85 }97 .read = true,
8698 });
87 switch (target.os.tag) {
88 .opencl, .opengl, .vulkan => {},
89 else => unreachable, // Caught by Compilation.Config.resolve.
90 }
9199
92 return self;100 return self;
93}101}
...@@ -98,24 +106,7 @@ pub fn open(...@@ -98,24 +106,7 @@ pub fn open(
98 emit: Path,106 emit: Path,
99 options: link.File.OpenOptions,107 options: link.File.OpenOptions,
100) !*SpirV {108) !*SpirV {
101 const target = comp.root_mod.resolved_target.result;109 return createEmpty(arena, comp, emit, options);
102 const use_lld = build_options.have_llvm and comp.config.use_lld;
103 const use_llvm = comp.config.use_llvm;
104
105 assert(!use_llvm); // Caught by Compilation.Config.resolve.
106 assert(!use_lld); // Caught by Compilation.Config.resolve.
107 assert(target.ofmt == .spirv); // Caught by Compilation.Config.resolve.
108
109 const spirv = try createEmpty(arena, comp, emit, options);
110 errdefer spirv.base.destroy();
111
112 // TODO: read the file and keep valid parts instead of truncating
113 const file = try emit.root_dir.handle.createFile(emit.sub_path, .{
114 .truncate = true,
115 .read = true,
116 });
117 spirv.base.file = file;
118 return spirv;
119}110}
120111
121pub fn deinit(self: *SpirV) void {112pub fn deinit(self: *SpirV) void {