authorgravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2022-09-10 13:49:39+02:00
committergravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2022-09-12 21:19:13+02:00
loga01b1448e2927146cde9f7861605585f96b5084d
tree9fcaa97cbb0fbf7cf0068f2fbacdb2f107ee564f
parente323cf1264f390911dcc2efea71d46be1d631d92
signaturelock-open Commit is signed but in an unrecognized format.

wasm-linker: write magic bytes only on success

By writing them at the very end, we can easily detect where the writing of the binary went wrong as tools will indicate the missing of the magic bytes.

1 files changed, 12 insertions(+), 8 deletions(-)

src/link/Wasm.zig+12-8
......@@ -324,8 +324,6 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option
324324 wasm_bin.base.file = file;
325325 wasm_bin.name = sub_path;
326326
327 try file.writeAll(&(wasm.magic ++ wasm.version));
328
329327 // As sym_index '0' is reserved, we use it for our stack pointer symbol
330328 const sym_name = try wasm_bin.string_table.put(allocator, "__stack_pointer");
331329 const symbol = try wasm_bin.symbols.addOne(allocator);
......@@ -363,7 +361,11 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option
363361 };
364362 }
365363
366 try wasm_bin.initDebugSections();
364 if (!options.strip and options.module != null) {
365 wasm_bin.dwarf = Dwarf.init(allocator, .wasm, options.target);
366 try wasm_bin.initDebugSections();
367 }
368
367369 return wasm_bin;
368370}
369371
......@@ -380,10 +382,6 @@ pub fn createEmpty(gpa: Allocator, options: link.Options) !*Wasm {
380382 .name = undefined,
381383 };
382384
383 if (!options.strip and options.module != null) {
384 self.dwarf = Dwarf.init(gpa, .wasm, options.target);
385 }
386
387385 const use_llvm = build_options.have_llvm and options.use_llvm;
388386 const use_stage1 = build_options.have_stage1 and options.use_stage1;
389387 if (use_llvm and !use_stage1) {
......@@ -2213,7 +2211,8 @@ pub fn flushModule(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Nod
22132211 const header_size = 5 + 1;
22142212 const is_obj = self.base.options.output_mode == .Obj;
22152213
2216 // No need to rewrite the magic/version header
2214 // We write the magic bytes at the end so they will only be written
2215 // if everything succeeded as expected.
22172216 try file.setEndPos(@sizeOf(@TypeOf(wasm.magic ++ wasm.version)));
22182217 try file.seekTo(@sizeOf(@TypeOf(wasm.magic ++ wasm.version)));
22192218
......@@ -2599,6 +2598,11 @@ pub fn flushModule(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Nod
25992598 }
26002599 try self.emitNameSection(file, arena);
26012600 }
2601
2602 // Only when writing all sections executed properly we write the magic
2603 // bytes. This allows us to easily detect what went wrong while generating
2604 // the final binary.
2605 try file.pwriteAll(&(wasm.magic ++ wasm.version), 0);
26022606}
26032607
26042608fn emitDebugSection(file: fs.File, data: []const u8, name: []const u8) !void {