authorgravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2021-11-26 20:07:34+01:00
committergravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2021-11-27 15:11:03+01:00
log6e88df44a29e0c30c341f113cf4771e08fc1f0fe
tree93f5cab9205d166060d39fda5c38544d093f4bd1
parenta54ac0888542d027b09e5c43729a7cfbe4a51393
signaturelock-open Commit is signed but in an unrecognized format.

wasm-linker: Link into binary during flush

This contains a few additions: - Proper stack pointer calculation keeping alignment in mind. - Setting up memory layout (including user flags). - Export or import memory - Handle 'easy' linker tasks during incremental compilation, while offloading heavy-tracking/computation tasks to `flush()` - This architecture allows us to easily integrate with the rest of 'zwld' to implement linking stage2 code with external object files.

2 files changed, 272 insertions(+), 142 deletions(-)

src/link/Wasm.zig+262-116
......@@ -39,8 +39,6 @@ llvm_object: ?*LlvmObject = null,
3939/// to support existing code.
4040/// TODO: Allow setting this through a flag?
4141host_name: []const u8 = "env",
42/// The last `DeclBlock` that was initialized will be saved here.
43last_atom: ?*Atom = null,
4442/// List of all `Decl` that are currently alive.
4543/// This is ment for bookkeeping so we can safely cleanup all codegen memory
4644/// when calling `deinit`
......@@ -57,10 +55,8 @@ code_section_index: ?u32 = null,
5755/// The count of imported functions. This number will be appended
5856/// to the function indexes as their index starts at the lowest non-extern function.
5957imported_functions_count: u32 = 0,
60/// List of all 'extern' declarations
61imports: std.ArrayListUnmanaged(wasm.Import) = .{},
62/// List of indexes of symbols representing extern declarations.
63import_symbols: std.ArrayListUnmanaged(u32) = .{},
58/// Map of symbol indexes, represented by its `wasm.Import`
59imports: std.AutoHashMapUnmanaged(u32, wasm.Import) = .{},
6460/// Represents non-synthetic section entries.
6561/// Used for code, data and custom sections.
6662segments: std.ArrayListUnmanaged(Segment) = .{},
......@@ -77,6 +73,8 @@ func_types: std.ArrayListUnmanaged(wasm.Type) = .{},
7773functions: std.ArrayListUnmanaged(wasm.Func) = .{},
7874/// Output global section
7975globals: std.ArrayListUnmanaged(wasm.Global) = .{},
76/// Memory section
77memories: wasm.Memory = .{ .limits = .{ .min = 0, .max = null } },
8078
8179/// Indirect function table, used to call function pointers
8280/// When this is non-zero, we must emit a table entry,
......@@ -180,7 +178,6 @@ pub fn deinit(self: *Wasm) void {
180178
181179 // free output sections
182180 self.imports.deinit(self.base.allocator);
183 self.import_symbols.deinit(self.base.allocator);
184181 self.func_types.deinit(self.base.allocator);
185182 self.functions.deinit(self.base.allocator);
186183 self.globals.deinit(self.base.allocator);
......@@ -221,6 +218,8 @@ pub fn updateFunc(self: *Wasm, module: *Module, func: *Module.Fn, air: Air, live
221218 const decl = func.owner_decl;
222219 assert(decl.link.wasm.sym_index != 0); // Must call allocateDeclIndexes()
223220
221 decl.link.wasm.clear();
222
224223 var codegen: CodeGen = .{
225224 .gpa = self.base.allocator,
226225 .air = air,
......@@ -259,6 +258,8 @@ pub fn updateDecl(self: *Wasm, module: *Module, decl: *Module.Decl) !void {
259258 }
260259 assert(decl.link.wasm.sym_index != 0); // Must call allocateDeclIndexes()
261260
261 decl.link.wasm.clear();
262
262263 var codegen: CodeGen = .{
263264 .gpa = self.base.allocator,
264265 .air = undefined,
......@@ -293,19 +294,14 @@ fn finishUpdateDecl(self: *Wasm, decl: *Module.Decl, result: CodeGen.Result, cod
293294 .externally_managed => |payload| payload,
294295 };
295296
297 if (decl.isExtern()) {
298 try self.addOrUpdateImport(decl);
299 }
300
301 if (code.len == 0) return;
296302 const atom: *Atom = &decl.link.wasm;
297303 atom.size = @intCast(u32, code.len);
298304 try atom.code.appendSlice(self.base.allocator, code);
299
300 // If we're updating an existing decl, unplug it first
301 // to avoid infinite loops due to earlier links
302 atom.unplug();
303
304 if (decl.isExtern()) {
305 try self.createUndefinedSymbol(decl, atom.sym_index);
306 } else {
307 try self.createDefinedSymbol(decl, atom.sym_index, atom);
308 }
309305}
310306
311307pub fn updateDeclExports(
......@@ -326,60 +322,62 @@ pub fn freeDecl(self: *Wasm, decl: *Module.Decl) void {
326322 if (build_options.have_llvm) {
327323 if (self.llvm_object) |llvm_object| return llvm_object.freeDecl(decl);
328324 }
329
330325 const atom = &decl.link.wasm;
331
332 if (self.last_atom == atom) {
333 self.last_atom = atom.prev;
334 }
335
336 atom.unplug();
337326 self.symbols_free_list.append(self.base.allocator, atom.sym_index) catch {};
338327 atom.deinit(self.base.allocator);
339328 _ = self.decls.remove(decl);
329
330 if (decl.isExtern()) {
331 const import = self.imports.fetchRemove(decl.link.wasm.sym_index).?.value;
332 switch (import.kind) {
333 .function => self.imported_functions_count -= 1,
334 else => unreachable,
335 }
336 }
340337}
341338
342fn createUndefinedSymbol(self: *Wasm, decl: *Module.Decl, symbol_index: u32) !void {
343 var symbol: *Symbol = &self.symbols.items[symbol_index];
339fn addOrUpdateImport(self: *Wasm, decl: *Module.Decl) !void {
340 const symbol_index = decl.link.wasm.sym_index;
341 const symbol: *Symbol = &self.symbols.items[symbol_index];
344342 symbol.name = decl.name;
345343 symbol.setUndefined(true);
346344 switch (decl.ty.zigTypeTag()) {
347345 .Fn => {
348 symbol.index = self.imported_functions_count;
349 self.imported_functions_count += 1;
350 try self.import_symbols.append(self.base.allocator, symbol_index);
351 try self.imports.append(self.base.allocator, .{
352 .module_name = self.host_name,
353 .name = std.mem.span(decl.name),
354 .kind = .{ .function = decl.fn_link.wasm.type_index },
355 });
346 const gop = try self.imports.getOrPut(self.base.allocator, symbol_index);
347 if (!gop.found_existing) {
348 self.imported_functions_count += 1;
349 gop.value_ptr.* = .{
350 .module_name = self.host_name,
351 .name = std.mem.span(symbol.name),
352 .kind = .{ .function = decl.fn_link.wasm.type_index },
353 };
354 }
356355 },
357356 else => @panic("TODO: Implement undefined symbols for non-function declarations"),
358357 }
359358}
360359
361/// Creates a defined symbol, as well as inserts the given `atom` into the chain
362fn createDefinedSymbol(self: *Wasm, decl: *Module.Decl, symbol_index: u32, atom: *Atom) !void {
363 const symbol: *Symbol = &self.symbols.items[symbol_index];
360fn parseDeclIntoAtom(self: *Wasm, decl: *Module.Decl) !void {
361 const atom: *Atom = &decl.link.wasm;
362 const symbol: *Symbol = &self.symbols.items[atom.sym_index];
364363 symbol.name = decl.name;
365 const final_index = switch (decl.ty.zigTypeTag()) {
364 atom.alignment = decl.ty.abiAlignment(self.base.options.target);
365 const final_index: u32 = switch (decl.ty.zigTypeTag()) {
366366 .Fn => result: {
367 const type_index = decl.fn_link.wasm.type_index;
368 const index = @intCast(u32, self.functions.items.len);
367 const fn_data = decl.fn_link.wasm;
368 const type_index = fn_data.type_index;
369 const index = @intCast(u32, self.functions.items.len + self.imported_functions_count);
369370 try self.functions.append(self.base.allocator, .{ .type_index = type_index });
370371 symbol.tag = .function;
371372 symbol.index = index;
372 atom.alignment = 1;
373373
374374 if (self.code_section_index == null) {
375375 self.code_section_index = @intCast(u32, self.segments.items.len);
376376 try self.segments.append(self.base.allocator, .{
377377 .alignment = atom.alignment,
378378 .size = atom.size,
379 .offset = atom.offset,
379 .offset = 0,
380380 });
381 } else {
382 self.segments.items[self.code_section_index.?].size += atom.size;
383381 }
384382
385383 break :result self.code_section_index.?;
......@@ -390,11 +388,11 @@ fn createDefinedSymbol(self: *Wasm, decl: *Module.Decl, symbol_index: u32, atom:
390388 self.segments.items[gop.value_ptr.*].size += atom.size;
391389 break :blk gop.value_ptr.*;
392390 } else blk: {
393 const index = @intCast(u32, self.segments.items.len) - @boolToInt(self.code_section_index != null);
391 const index = @intCast(u32, self.segments.items.len);
394392 try self.segments.append(self.base.allocator, .{
395393 .alignment = atom.alignment,
396 .size = atom.size,
397 .offset = atom.offset,
394 .size = 0,
395 .offset = 0,
398396 });
399397 gop.value_ptr.* = index;
400398 break :blk index;
......@@ -413,20 +411,153 @@ fn createDefinedSymbol(self: *Wasm, decl: *Module.Decl, symbol_index: u32, atom:
413411 symbol.tag = .data;
414412 symbol.index = info_index;
415413 atom.alignment = decl.ty.abiAlignment(self.base.options.target);
414
416415 break :result atom_index;
417416 },
418417 };
419418
419 const segment: *Segment = &self.segments.items[final_index];
420 segment.alignment = std.math.max(segment.alignment, atom.alignment);
421 segment.size = std.mem.alignForwardGeneric(
422 u32,
423 std.mem.alignForwardGeneric(u32, segment.size, atom.alignment) + atom.size,
424 segment.alignment,
425 );
426
420427 if (self.atoms.getPtr(final_index)) |last| {
421428 last.*.next = atom;
422429 atom.prev = last.*;
423 atom.offset = last.*.offset + last.*.size;
424430 last.* = atom;
425431 } else {
426432 try self.atoms.putNoClobber(self.base.allocator, final_index, atom);
427433 }
428434}
429435
436fn allocateAtoms(self: *Wasm) !void {
437 var it = self.atoms.iterator();
438 while (it.next()) |entry| {
439 var atom: *Atom = entry.value_ptr.*.getFirst();
440 var offset: u32 = 0;
441 while (true) {
442 offset = std.mem.alignForwardGeneric(u32, offset, atom.alignment);
443 atom.offset = offset;
444 log.debug("Atom '{s}' allocated from 0x{x:0>8} to 0x{x:0>8} size={d}", .{
445 self.symbols.items[atom.sym_index].name,
446 offset,
447 offset + atom.size,
448 atom.size,
449 });
450 offset += atom.size;
451 atom = atom.next orelse break;
452 }
453 }
454}
455
456fn setupImports(self: *Wasm) void {
457 var function_index: u32 = 0;
458 var it = self.imports.iterator();
459 while (it.next()) |entry| {
460 const symbol = &self.symbols.items[entry.key_ptr.*];
461 const import: wasm.Import = entry.value_ptr.*;
462 switch (import.kind) {
463 .function => {
464 symbol.index = function_index;
465 function_index += 1;
466 },
467 else => unreachable,
468 }
469 }
470}
471
472/// Sets up the memory section of the wasm module, as well as the stack.
473fn setupMemory(self: *Wasm) !void {
474 log.debug("Setting up memory layout", .{});
475 const page_size = 64 * 1024;
476 const stack_size = self.base.options.stack_size_override orelse page_size * 1;
477 const stack_alignment = 16;
478 var memory_ptr: u64 = self.base.options.global_base orelse 1024;
479 memory_ptr = std.mem.alignForwardGeneric(u64, memory_ptr, stack_alignment);
480
481 var offset: u32 = @intCast(u32, memory_ptr);
482 for (self.segments.items) |*segment, i| {
483 // skip 'code' segments
484 if (self.code_section_index) |index| {
485 if (index == i) continue;
486 }
487 memory_ptr = std.mem.alignForwardGeneric(u64, memory_ptr, segment.alignment);
488 memory_ptr += segment.size;
489 segment.offset = offset;
490 offset += segment.size;
491 }
492
493 memory_ptr = std.mem.alignForwardGeneric(u64, memory_ptr, stack_alignment);
494 memory_ptr += stack_size;
495
496 // Setup the max amount of pages
497 // For now we only support wasm32 by setting the maximum allowed memory size 2^32-1
498 const max_memory_allowed: u64 = (1 << 32) - 1;
499
500 if (self.base.options.initial_memory) |initial_memory| {
501 if (!std.mem.isAlignedGeneric(u64, initial_memory, page_size)) {
502 log.err("Initial memory must be {d}-byte aligned", .{page_size});
503 return error.MissAlignment;
504 }
505 if (memory_ptr > initial_memory) {
506 log.err("Initial memory too small, must be at least {d} bytes", .{memory_ptr});
507 return error.MemoryTooSmall;
508 }
509 if (initial_memory > max_memory_allowed) {
510 log.err("Initial memory exceeds maximum memory {d}", .{max_memory_allowed});
511 return error.MemoryTooBig;
512 }
513 memory_ptr = initial_memory;
514 }
515
516 // In case we do not import memory, but define it ourselves,
517 // set the minimum amount of pages on the memory section.
518 self.memories.limits.min = @intCast(u32, std.mem.alignForwardGeneric(u64, memory_ptr, page_size) / page_size);
519 log.debug("Total memory pages: {d}", .{self.memories.limits.min});
520
521 if (self.base.options.max_memory) |max_memory| {
522 if (!std.mem.isAlignedGeneric(u64, max_memory, page_size)) {
523 log.err("Maximum memory must be {d}-byte aligned", .{page_size});
524 return error.MissAlignment;
525 }
526 if (memory_ptr > max_memory) {
527 log.err("Maxmimum memory too small, must be at least {d} bytes", .{memory_ptr});
528 return error.MemoryTooSmall;
529 }
530 if (max_memory > max_memory_allowed) {
531 log.err("Maximum memory exceeds maxmium amount {d}", .{max_memory_allowed});
532 return error.MemoryTooBig;
533 }
534 self.memories.limits.max = @intCast(u32, max_memory / page_size);
535 log.debug("Maximum memory pages: {d}", .{self.memories.limits.max});
536 }
537
538 // We always put the stack pointer global at index 0
539 self.globals.items[0].init.i32_const = @bitCast(i32, @intCast(u32, memory_ptr));
540}
541
542fn resetState(self: *Wasm) void {
543 for (self.segment_info.items) |*segment_info| {
544 self.base.allocator.free(segment_info.name);
545 }
546 var decl_it = self.decls.keyIterator();
547 while (decl_it.next()) |decl| {
548 const atom = &decl.*.link.wasm;
549 atom.next = null;
550 atom.prev = null;
551 }
552 self.functions.clearRetainingCapacity();
553 self.segments.clearRetainingCapacity();
554 self.segment_info.clearRetainingCapacity();
555 self.data_segments.clearRetainingCapacity();
556 self.function_table.clearRetainingCapacity();
557 self.atoms.clearRetainingCapacity();
558 self.code_section_index = null;
559}
560
430561pub fn flush(self: *Wasm, comp: *Compilation) !void {
431562 if (build_options.have_llvm and self.base.options.use_lld) {
432563 return self.linkWithLLD(comp);
......@@ -440,22 +571,21 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void {
440571 const tracy = trace(@src());
441572 defer tracy.end();
442573
443 const file = self.base.file.?;
444 const header_size = 5 + 1;
445 // The size of the emulated stack
446 const stack_size = @intCast(u32, self.base.options.stack_size_override orelse std.wasm.page_size);
447
448 var data_size: u32 = 0;
449 for (self.segments.items) |segment, index| {
450 // skip 'code' segments as they do not count towards data section size
451 if (self.code_section_index) |code_index| {
452 if (index == code_index) continue;
453 }
454 data_size += segment.size;
574 // When we finish/error we reset the state of the linker
575 // So we can rebuild the binary file on each incremental update
576 defer self.resetState();
577 self.setupImports();
578 var decl_it = self.decls.keyIterator();
579 while (decl_it.next()) |decl| {
580 if (decl.*.isExtern()) continue;
581 try self.parseDeclIntoAtom(decl.*);
455582 }
456583
457 // set the stack size on the global
458 self.globals.items[0].init.i32_const = @bitCast(i32, data_size + stack_size);
584 try self.setupMemory();
585 try self.allocateAtoms();
586
587 const file = self.base.file.?;
588 const header_size = 5 + 1;
459589
460590 // No need to rewrite the magic/version header
461591 try file.setEndPos(@sizeOf(@TypeOf(wasm.magic ++ wasm.version)));
......@@ -484,42 +614,34 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void {
484614 }
485615
486616 // Import section
487 if (self.import_symbols.items.len > 0) {
617 const import_mem = self.base.options.import_memory;
618 if (self.imports.count() != 0 or import_mem) {
488619 const header_offset = try reserveVecSectionHeader(file);
489620 const writer = file.writer();
490 for (self.import_symbols.items) |symbol_index| {
491 const import_symbol = self.symbols.items[symbol_index];
621
622 var it = self.imports.iterator();
623 while (it.next()) |entry| {
624 const import_symbol = self.symbols.items[entry.key_ptr.*];
492625 std.debug.assert(import_symbol.isUndefined());
493 try leb.writeULEB128(writer, @intCast(u32, self.host_name.len));
494 try writer.writeAll(self.host_name);
495
496 const name = std.mem.span(import_symbol.name);
497 try leb.writeULEB128(writer, @intCast(u32, name.len));
498 try writer.writeAll(name);
499
500 try writer.writeByte(wasm.externalKind(import_symbol.tag.externalType()));
501 const import = self.findImport(import_symbol.index, import_symbol.tag.externalType()).?;
502 switch (import.kind) {
503 .function => |type_index| try leb.writeULEB128(writer, type_index),
504 .global => |global_type| {
505 try leb.writeULEB128(writer, wasm.valtype(global_type.valtype));
506 try writer.writeByte(@boolToInt(global_type.mutable));
507 },
508 .table => |table| {
509 try leb.writeULEB128(writer, wasm.reftype(table.reftype));
510 try emitLimits(writer, table.limits);
511 },
512 .memory => |limits| {
513 try emitLimits(writer, limits);
514 },
515 }
626 const import = entry.value_ptr.*;
627 try emitImport(writer, import);
516628 }
629
630 if (import_mem) {
631 const mem_imp: wasm.Import = .{
632 .module_name = self.host_name,
633 .name = "memory",
634 .kind = .{ .memory = self.memories.limits },
635 };
636 try emitImport(writer, mem_imp);
637 }
638
517639 try writeVecSectionHeader(
518640 file,
519641 header_offset,
520642 .import,
521643 @intCast(u32, (try file.getPos()) - header_offset - header_size),
522 @intCast(u32, self.imports.items.len),
644 @intCast(u32, self.imports.count() + @boolToInt(import_mem)),
523645 );
524646 }
525647
......@@ -541,21 +663,11 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void {
541663 }
542664
543665 // Memory section
544 {
666 if (!self.base.options.import_memory) {
545667 const header_offset = try reserveVecSectionHeader(file);
546668 const writer = file.writer();
547669
548 try leb.writeULEB128(writer, @as(u32, 0));
549 // Calculate the amount of memory pages are required and write them.
550 // Wasm uses 64kB page sizes. Round up to ensure the data segments fit into the memory
551 try leb.writeULEB128(
552 writer,
553 try std.math.divCeil(
554 u32,
555 data_size + stack_size,
556 std.wasm.page_size,
557 ),
558 );
670 try emitLimits(writer, self.memories.limits);
559671 try writeVecSectionHeader(
560672 file,
561673 header_offset,
......@@ -590,7 +702,6 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void {
590702 const header_offset = try reserveVecSectionHeader(file);
591703 const writer = file.writer();
592704 var count: u32 = 0;
593 var func_index: u32 = self.imported_functions_count;
594705 for (module.decl_exports.values()) |exports| {
595706 for (exports) |exprt| {
596707 // Export name length + name
......@@ -599,11 +710,13 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void {
599710
600711 switch (exprt.exported_decl.ty.zigTypeTag()) {
601712 .Fn => {
713 const target = exprt.exported_decl.link.wasm.sym_index;
714 const target_symbol = self.symbols.items[target];
715 std.debug.assert(target_symbol.tag == .function);
602716 // Type of the export
603717 try writer.writeByte(wasm.externalKind(.function));
604718 // Exported function index
605 try leb.writeULEB128(writer, func_index);
606 func_index += 1;
719 try leb.writeULEB128(writer, target_symbol.index);
607720 },
608721 else => return error.TODOImplementNonFnDeclsForWasm,
609722 }
......@@ -613,7 +726,7 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void {
613726 }
614727
615728 // export memory if size is not 0
616 if (data_size != 0) {
729 if (!self.base.options.import_memory) {
617730 try leb.writeULEB128(writer, @intCast(u32, "memory".len));
618731 try writer.writeAll("memory");
619732 try writer.writeByte(wasm.externalKind(.memory));
......@@ -639,7 +752,6 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void {
639752 try atom.resolveRelocs(self);
640753 try leb.writeULEB128(writer, atom.size);
641754 try writer.writeAll(atom.code.items);
642
643755 atom = atom.next orelse break;
644756 }
645757 try writeVecSectionHeader(
......@@ -657,37 +769,47 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void {
657769 const writer = file.writer();
658770
659771 var it = self.data_segments.iterator();
772 var segment_count: u32 = 0;
660773 while (it.next()) |entry| {
661774 // do not output 'bss' section
662775 if (std.mem.eql(u8, entry.key_ptr.*, ".bss")) continue;
776 segment_count += 1;
663777 const atom_index = entry.value_ptr.*;
664 var atom = self.atoms.getPtr(atom_index).?.*.getFirst();
778 var atom: *Atom = self.atoms.getPtr(atom_index).?.*.getFirst();
665779 var segment = self.segments.items[atom_index];
666780
667781 // flag and index to memory section (currently, there can only be 1 memory section in wasm)
668782 try leb.writeULEB128(writer, @as(u32, 0));
669
670783 // offset into data section
671 try writer.writeByte(wasm.opcode(.i32_const));
672 try leb.writeILEB128(writer, @as(i32, 0));
673 try writer.writeByte(wasm.opcode(.end));
674
675 // offset table + data size
784 try emitInit(writer, .{ .i32_const = @bitCast(i32, segment.offset) });
676785 try leb.writeULEB128(writer, segment.size);
677786
678787 // fill in the offset table and the data segments
679788 var current_offset: u32 = 0;
680789 while (true) {
681790 try atom.resolveRelocs(self);
791
792 // Pad with zeroes to ensure all segments are aligned
793 if (current_offset != atom.offset) {
794 const diff = atom.offset - current_offset;
795 try writer.writeByteNTimes(0, diff);
796 current_offset += diff;
797 }
682798 std.debug.assert(current_offset == atom.offset);
683799 std.debug.assert(atom.code.items.len == atom.size);
684
685800 try writer.writeAll(atom.code.items);
686801
687802 current_offset += atom.size;
688803 if (atom.next) |next| {
689804 atom = next;
690 } else break;
805 } else {
806 // also pad with zeroes when last atom to ensure
807 // segments are aligned.
808 if (current_offset != segment.size) {
809 try writer.writeByteNTimes(0, segment.size - current_offset);
810 }
811 break;
812 }
691813 }
692814 }
693815
......@@ -696,7 +818,7 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void {
696818 header_offset,
697819 .data,
698820 @intCast(u32, (try file.getPos()) - header_offset - header_size),
699 @intCast(u32, 1), // only 1 data section
821 @intCast(u32, segment_count),
700822 );
701823 }
702824}
......@@ -735,6 +857,30 @@ fn emitInit(writer: anytype, init_expr: wasm.InitExpression) !void {
735857 try writer.writeByte(wasm.opcode(.end));
736858}
737859
860fn emitImport(writer: anytype, import: wasm.Import) !void {
861 try leb.writeULEB128(writer, @intCast(u32, import.module_name.len));
862 try writer.writeAll(import.module_name);
863
864 try leb.writeULEB128(writer, @intCast(u32, import.name.len));
865 try writer.writeAll(import.name);
866
867 try writer.writeByte(@enumToInt(import.kind));
868 switch (import.kind) {
869 .function => |type_index| try leb.writeULEB128(writer, type_index),
870 .global => |global_type| {
871 try leb.writeULEB128(writer, wasm.valtype(global_type.valtype));
872 try writer.writeByte(@boolToInt(global_type.mutable));
873 },
874 .table => |table| {
875 try leb.writeULEB128(writer, wasm.reftype(table.reftype));
876 try emitLimits(writer, table.limits);
877 },
878 .memory => |limits| {
879 try emitLimits(writer, limits);
880 },
881 }
882}
883
738884fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
739885 const tracy = trace(@src());
740886 defer tracy.end();
src/link/Wasm/Atom.zig+10-26
......@@ -6,7 +6,7 @@ const Wasm = @import("../Wasm.zig");
66const Symbol = @import("Symbol.zig");
77
88const leb = std.leb;
9const log = std.log.scoped(.zld);
9const log = std.log.scoped(.link);
1010const mem = std.mem;
1111const Allocator = mem.Allocator;
1212
......@@ -42,12 +42,18 @@ pub const empty: Atom = .{
4242};
4343
4444/// Frees all resources owned by this `Atom`.
45/// Also destroys itself, making any usage of this atom illegal.
4645pub fn deinit(self: *Atom, gpa: *Allocator) void {
4746 self.relocs.deinit(gpa);
4847 self.code.deinit(gpa);
4948}
5049
50/// Sets the length of relocations and code to '0',
51/// effectively resetting them and allowing them to be re-populated.
52pub fn clear(self: *Atom) void {
53 self.relocs.clearRetainingCapacity();
54 self.code.clearRetainingCapacity();
55}
56
5157pub fn format(self: Atom, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void {
5258 _ = fmt;
5359 _ = options;
......@@ -66,26 +72,6 @@ pub fn getFirst(self: *Atom) *Atom {
6672 return tmp;
6773}
6874
69/// Returns the last `Atom` from a given atom
70pub fn getLast(self: *Atom) *Atom {
71 var tmp = self;
72 while (tmp.next) |next| tmp = next;
73 return tmp;
74}
75
76/// Unplugs the `Atom` from the chain
77pub fn unplug(self: *Atom) void {
78 if (self.prev) |prev| {
79 prev.next = self.next;
80 }
81
82 if (self.next) |next| {
83 next.prev = self.prev;
84 }
85 self.next = null;
86 self.prev = null;
87}
88
8975/// Resolves the relocations within the atom, writing the new value
9076/// at the calculated offset.
9177pub fn resolveRelocs(self: *Atom, wasm_bin: *const Wasm) !void {
......@@ -163,12 +149,10 @@ fn relocationValue(relocation: types.Relocation, wasm_bin: *const Wasm) !u64 {
163149 var target_atom = wasm_bin.atoms.getPtr(atom_index).?.*.getFirst();
164150 while (true) {
165151 if (target_atom.sym_index == relocation.index) break;
166 if (target_atom.next) |next| {
167 target_atom = next;
168 } else break;
152 target_atom = target_atom.next orelse break;
169153 }
170154 const segment = wasm_bin.segments.items[atom_index];
171 const base = wasm_bin.base.options.global_base orelse 0;
155 const base = wasm_bin.base.options.global_base orelse 1024;
172156 const offset = target_atom.offset + segment.offset;
173157 break :blk offset + base + (relocation.addend orelse 0);
174158 },