| ... | ... | @@ -1568,9 +1568,13 @@ fn allocateAtoms(wasm: *Wasm) !void { |
| 1568 | 1568 | var atom: *Atom = entry.value_ptr.*.getFirst(); |
| 1569 | 1569 | var offset: u32 = 0; |
| 1570 | 1570 | while (true) { |
| 1571 | const symbol_loc = atom.symbolLoc(); |
| 1572 | if (!wasm.resolved_symbols.contains(symbol_loc)) { |
| 1573 | atom = atom.next orelse break; |
| 1574 | continue; |
| 1575 | } |
| 1571 | 1576 | offset = std.mem.alignForwardGeneric(u32, offset, atom.alignment); |
| 1572 | 1577 | atom.offset = offset; |
| 1573 | | const symbol_loc = atom.symbolLoc(); |
| 1574 | 1578 | log.debug("Atom '{s}' allocated from 0x{x:0>8} to 0x{x:0>8} size={d}", .{ |
| 1575 | 1579 | symbol_loc.getName(wasm), |
| 1576 | 1580 | offset, |
| ... | ... | @@ -1578,7 +1582,7 @@ fn allocateAtoms(wasm: *Wasm) !void { |
| 1578 | 1582 | atom.size, |
| 1579 | 1583 | }); |
| 1580 | 1584 | offset += atom.size; |
| 1581 | | try wasm.symbol_atom.put(wasm.base.allocator, atom.symbolLoc(), atom); // Update atom pointers |
| 1585 | try wasm.symbol_atom.put(wasm.base.allocator, symbol_loc, atom); // Update atom pointers |
| 1582 | 1586 | atom = atom.next orelse break; |
| 1583 | 1587 | } |
| 1584 | 1588 | segment.size = std.mem.alignForwardGeneric(u32, offset, segment.alignment); |
| ... | ... | @@ -2579,14 +2583,16 @@ fn linkWithZld(wasm: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) l |
| 2579 | 2583 | var atom: *Atom = wasm.atoms.get(code_index).?.getFirst(); |
| 2580 | 2584 | |
| 2581 | 2585 | // The code section must be sorted in line with the function order. |
| 2582 | | var sorted_atoms = try std.ArrayList(*Atom).initCapacity(wasm.base.allocator, wasm.functions.count()); |
| 2586 | var sorted_atoms = try std.ArrayList(*Atom).initCapacity(gpa, wasm.functions.count()); |
| 2583 | 2587 | defer sorted_atoms.deinit(); |
| 2584 | 2588 | |
| 2585 | 2589 | while (true) { |
| 2586 | | if (!is_obj) { |
| 2587 | | atom.resolveRelocs(wasm); |
| 2590 | if (wasm.resolved_symbols.contains(atom.symbolLoc())) { |
| 2591 | if (!is_obj) { |
| 2592 | atom.resolveRelocs(wasm); |
| 2593 | } |
| 2594 | sorted_atoms.appendAssumeCapacity(atom); |
| 2588 | 2595 | } |
| 2589 | | sorted_atoms.appendAssumeCapacity(atom); |
| 2590 | 2596 | atom = atom.next orelse break; |
| 2591 | 2597 | } |
| 2592 | 2598 | |
| ... | ... | @@ -2641,6 +2647,10 @@ fn linkWithZld(wasm: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) l |
| 2641 | 2647 | // fill in the offset table and the data segments |
| 2642 | 2648 | var current_offset: u32 = 0; |
| 2643 | 2649 | while (true) { |
| 2650 | if (!wasm.resolved_symbols.contains(atom.symbolLoc())) { |
| 2651 | atom = atom.next orelse break; |
| 2652 | continue; |
| 2653 | } |
| 2644 | 2654 | if (!is_obj) { |
| 2645 | 2655 | atom.resolveRelocs(wasm); |
| 2646 | 2656 | } |
| ... | ... | @@ -4170,15 +4180,23 @@ fn emitDataRelocations( |
| 4170 | 4180 | try writeCustomSectionHeader(binary_bytes.items, header_offset, size); |
| 4171 | 4181 | } |
| 4172 | 4182 | |
| 4173 | | /// Searches for an a matching function signature, when not found |
| 4174 | | /// a new entry will be made. The index of the existing/new signature will be returned. |
| 4175 | | pub fn putOrGetFuncType(wasm: *Wasm, func_type: std.wasm.Type) !u32 { |
| 4183 | pub fn getTypeIndex(wasm: *const Wasm, func_type: std.wasm.Type) ?u32 { |
| 4176 | 4184 | var index: u32 = 0; |
| 4177 | 4185 | while (index < wasm.func_types.items.len) : (index += 1) { |
| 4178 | 4186 | if (wasm.func_types.items[index].eql(func_type)) return index; |
| 4179 | 4187 | } |
| 4188 | return null; |
| 4189 | } |
| 4190 | |
| 4191 | /// Searches for an a matching function signature, when not found |
| 4192 | /// a new entry will be made. The index of the existing/new signature will be returned. |
| 4193 | pub fn putOrGetFuncType(wasm: *Wasm, func_type: std.wasm.Type) !u32 { |
| 4194 | if (wasm.getTypeIndex(func_type)) |index| { |
| 4195 | return index; |
| 4196 | } |
| 4180 | 4197 | |
| 4181 | 4198 | // functype does not exist. |
| 4199 | const index = @intCast(u32, wasm.func_types.items.len); |
| 4182 | 4200 | const params = try wasm.base.allocator.dupe(std.wasm.Valtype, func_type.params); |
| 4183 | 4201 | errdefer wasm.base.allocator.free(params); |
| 4184 | 4202 | const returns = try wasm.base.allocator.dupe(std.wasm.Valtype, func_type.returns); |