| ... | ... | @@ -324,8 +324,6 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option |
| 324 | 324 | wasm_bin.base.file = file; |
| 325 | 325 | wasm_bin.name = sub_path; |
| 326 | 326 | |
| 327 | | try file.writeAll(&(wasm.magic ++ wasm.version)); |
| 328 | | |
| 329 | 327 | // As sym_index '0' is reserved, we use it for our stack pointer symbol |
| 330 | 328 | const sym_name = try wasm_bin.string_table.put(allocator, "__stack_pointer"); |
| 331 | 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 | 361 | }; |
| 364 | 362 | } |
| 365 | 363 | |
| 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 | 369 | return wasm_bin; |
| 368 | 370 | } |
| 369 | 371 | |
| ... | ... | @@ -380,10 +382,6 @@ pub fn createEmpty(gpa: Allocator, options: link.Options) !*Wasm { |
| 380 | 382 | .name = undefined, |
| 381 | 383 | }; |
| 382 | 384 | |
| 383 | | if (!options.strip and options.module != null) { |
| 384 | | self.dwarf = Dwarf.init(gpa, .wasm, options.target); |
| 385 | | } |
| 386 | | |
| 387 | 385 | const use_llvm = build_options.have_llvm and options.use_llvm; |
| 388 | 386 | const use_stage1 = build_options.have_stage1 and options.use_stage1; |
| 389 | 387 | if (use_llvm and !use_stage1) { |
| ... | ... | @@ -2213,7 +2211,8 @@ pub fn flushModule(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Nod |
| 2213 | 2211 | const header_size = 5 + 1; |
| 2214 | 2212 | const is_obj = self.base.options.output_mode == .Obj; |
| 2215 | 2213 | |
| 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. |
| 2217 | 2216 | try file.setEndPos(@sizeOf(@TypeOf(wasm.magic ++ wasm.version))); |
| 2218 | 2217 | try file.seekTo(@sizeOf(@TypeOf(wasm.magic ++ wasm.version))); |
| 2219 | 2218 | |
| ... | ... | @@ -2599,6 +2598,11 @@ pub fn flushModule(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Nod |
| 2599 | 2598 | } |
| 2600 | 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 | } |
| 2603 | 2607 | |
| 2604 | 2608 | fn emitDebugSection(file: fs.File, data: []const u8, name: []const u8) !void { |