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...@@ -324,8 +324,6 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option
324 wasm_bin.base.file = file;324 wasm_bin.base.file = file;
325 wasm_bin.name = sub_path;325 wasm_bin.name = sub_path;
326326
327 try file.writeAll(&(wasm.magic ++ wasm.version));
328
329 // As sym_index '0' is reserved, we use it for our stack pointer symbol327 // As sym_index '0' is reserved, we use it for our stack pointer symbol
330 const sym_name = try wasm_bin.string_table.put(allocator, "__stack_pointer");328 const sym_name = try wasm_bin.string_table.put(allocator, "__stack_pointer");
331 const symbol = try wasm_bin.symbols.addOne(allocator);329 const symbol = try wasm_bin.symbols.addOne(allocator);
...@@ -363,7 +361,11 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option...@@ -363,7 +361,11 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option
363 };361 };
364 }362 }
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
367 return wasm_bin;369 return wasm_bin;
368}370}
369371
...@@ -380,10 +382,6 @@ pub fn createEmpty(gpa: Allocator, options: link.Options) !*Wasm {...@@ -380,10 +382,6 @@ pub fn createEmpty(gpa: Allocator, options: link.Options) !*Wasm {
380 .name = undefined,382 .name = undefined,
381 };383 };
382384
383 if (!options.strip and options.module != null) {
384 self.dwarf = Dwarf.init(gpa, .wasm, options.target);
385 }
386
387 const use_llvm = build_options.have_llvm and options.use_llvm;385 const use_llvm = build_options.have_llvm and options.use_llvm;
388 const use_stage1 = build_options.have_stage1 and options.use_stage1;386 const use_stage1 = build_options.have_stage1 and options.use_stage1;
389 if (use_llvm and !use_stage1) {387 if (use_llvm and !use_stage1) {
...@@ -2213,7 +2211,8 @@ pub fn flushModule(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Nod...@@ -2213,7 +2211,8 @@ pub fn flushModule(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Nod
2213 const header_size = 5 + 1;2211 const header_size = 5 + 1;
2214 const is_obj = self.base.options.output_mode == .Obj;2212 const is_obj = self.base.options.output_mode == .Obj;
22152213
2216 // No need to rewrite the magic/version header2214 // We write the magic bytes at the end so they will only be written
2215 // if everything succeeded as expected.
2217 try file.setEndPos(@sizeOf(@TypeOf(wasm.magic ++ wasm.version)));2216 try file.setEndPos(@sizeOf(@TypeOf(wasm.magic ++ wasm.version)));
2218 try file.seekTo(@sizeOf(@TypeOf(wasm.magic ++ wasm.version)));2217 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...@@ -2599,6 +2598,11 @@ pub fn flushModule(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Nod
2599 }2598 }
2600 try self.emitNameSection(file, arena);2599 try self.emitNameSection(file, arena);
2601 }2600 }
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);
2602}2606}
26032607
2604fn emitDebugSection(file: fs.File, data: []const u8, name: []const u8) !void {2608fn emitDebugSection(file: fs.File, data: []const u8, name: []const u8) !void {