| author | |
| committer | |
| log | 12505c6d3d4ccfc859b67e4b43c5b3844bebb475 |
| tree | 37c53b1a33ebfc4f7f72ad54254b3b4556ee353e |
| parent | f6896ef2180709fedeb5bafde3fe58ca6d06aa3a |
| signature |
Also, consolidate the creation of Atoms so they all use `createAtom`.3 files changed, 44 insertions(+), 67 deletions(-)
src/link/Wasm.zig+12-30| ... | ... | @@ -569,6 +569,7 @@ pub fn createEmpty( |
| 569 | 569 | if (!use_llvm) { |
| 570 | 570 | const index: File.Index = @enumFromInt(wasm.files.len); |
| 571 | 571 | var zig_object: ZigObject = .{ |
| 572 | .index = index, | |
| 572 | 573 | .path = try std.fmt.allocPrint(gpa, "{s}.o", .{std.fs.path.stem(zcu.main_mod.root_src_path)}), |
| 573 | 574 | .stack_pointer_sym = undefined, |
| 574 | 575 | }; |
| ... | ... | @@ -663,12 +664,11 @@ fn parseObjectFile(wasm: *Wasm, path: []const u8) !bool { |
| 663 | 664 | } |
| 664 | 665 | |
| 665 | 666 | /// Creates a new empty `Atom` and returns its `Atom.Index` |
| 666 | pub fn createAtom(wasm: *Wasm, sym_index: u32) !Atom.Index { | |
| 667 | pub fn createAtom(wasm: *Wasm, sym_index: u32, file_index: File.Index) !Atom.Index { | |
| 667 | 668 | const gpa = wasm.base.comp.gpa; |
| 668 | 669 | const index: Atom.Index = @intCast(wasm.managed_atoms.items.len); |
| 669 | 670 | const atom = try wasm.managed_atoms.addOne(gpa); |
| 670 | atom.* = Atom.empty; | |
| 671 | atom.sym_index = sym_index; | |
| 671 | atom.* = .{ .file_index = file_index, .sym_index = sym_index }; | |
| 672 | 672 | try wasm.symbol_atom.putNoClobber(gpa, .{ .file = null, .index = sym_index }, index); |
| 673 | 673 | |
| 674 | 674 | return index; |
| ... | ... | @@ -1825,20 +1825,11 @@ fn createSyntheticFunction( |
| 1825 | 1825 | symbol.index = func_index; |
| 1826 | 1826 | |
| 1827 | 1827 | // create the atom that will be output into the final binary |
| 1828 | const atom_index = @as(Atom.Index, @intCast(wasm.managed_atoms.items.len)); | |
| 1829 | const atom = try wasm.managed_atoms.addOne(gpa); | |
| 1830 | atom.* = .{ | |
| 1831 | .size = @as(u32, @intCast(function_body.items.len)), | |
| 1832 | .offset = 0, | |
| 1833 | .sym_index = loc.index, | |
| 1834 | .file = null, | |
| 1835 | .alignment = .@"1", | |
| 1836 | .prev = null, | |
| 1837 | .code = function_body.moveToUnmanaged(), | |
| 1838 | .original_offset = 0, | |
| 1839 | }; | |
| 1828 | const atom_index = try wasm.createAtom(loc.index, .null); | |
| 1829 | const atom = wasm.getAtomPtr(atom_index); | |
| 1830 | atom.code = function_body.moveToUnmanaged(); | |
| 1831 | atom.size = @intCast(function_body.items.len); | |
| 1840 | 1832 | try wasm.appendAtomAtIndex(wasm.code_section_index.?, atom_index); |
| 1841 | try wasm.symbol_atom.putNoClobber(gpa, loc, atom_index); | |
| 1842 | 1833 | } |
| 1843 | 1834 | |
| 1844 | 1835 | /// Unlike `createSyntheticFunction` this function is to be called by |
| ... | ... | @@ -1856,19 +1847,11 @@ pub fn createFunction( |
| 1856 | 1847 | const gpa = wasm.base.comp.gpa; |
| 1857 | 1848 | const loc = try wasm.createSyntheticSymbol(symbol_name, .function); |
| 1858 | 1849 | |
| 1859 | const atom_index: Atom.Index = @intCast(wasm.managed_atoms.items.len); | |
| 1860 | const atom = try wasm.managed_atoms.addOne(gpa); | |
| 1861 | atom.* = .{ | |
| 1862 | .size = @intCast(function_body.items.len), | |
| 1863 | .offset = 0, | |
| 1864 | .sym_index = loc.index, | |
| 1865 | .file = null, | |
| 1866 | .alignment = .@"1", | |
| 1867 | .prev = null, | |
| 1868 | .code = function_body.moveToUnmanaged(), | |
| 1869 | .relocs = relocations.moveToUnmanaged(), | |
| 1870 | .original_offset = 0, | |
| 1871 | }; | |
| 1850 | const atom_index = try wasm.createAtom(loc.index, wasm.zig_object_index); | |
| 1851 | const atom = wasm.getAtomPtr(atom_index); | |
| 1852 | atom.code = function_body.moveToUnmanaged(); | |
| 1853 | atom.relocs = relocations.moveToUnmanaged(); | |
| 1854 | atom.size = @intCast(function_body.items.len); | |
| 1872 | 1855 | const symbol = loc.getSymbol(wasm); |
| 1873 | 1856 | symbol.setFlag(.WASM_SYM_VISIBILITY_HIDDEN); // ensure function does not get exported |
| 1874 | 1857 | |
| ... | ... | @@ -1878,7 +1861,6 @@ pub fn createFunction( |
| 1878 | 1861 | break :idx index; |
| 1879 | 1862 | }; |
| 1880 | 1863 | try wasm.appendAtomAtIndex(section_index, atom_index); |
| 1881 | try wasm.symbol_atom.putNoClobber(gpa, loc, atom_index); | |
| 1882 | 1864 | try wasm.zigObjectPtr().?.atom_types.put( |
| 1883 | 1865 | gpa, |
| 1884 | 1866 | atom_index, |
src/link/Wasm/Atom.zig+24-32| ... | ... | @@ -1,55 +1,36 @@ |
| 1 | const Atom = @This(); | |
| 2 | ||
| 3 | const std = @import("std"); | |
| 4 | const types = @import("types.zig"); | |
| 5 | const Wasm = @import("../Wasm.zig"); | |
| 6 | const Symbol = @import("Symbol.zig"); | |
| 7 | ||
| 8 | const leb = std.leb; | |
| 9 | const log = std.log.scoped(.link); | |
| 10 | const mem = std.mem; | |
| 11 | const Allocator = mem.Allocator; | |
| 12 | ||
| 1 | /// Represents the index of the file this atom was generated from. | |
| 2 | /// This is 'null' when the atom was generated by a synthetic linker symbol. | |
| 3 | file: FileIndex, | |
| 13 | 4 | /// symbol index of the symbol representing this atom |
| 14 | 5 | sym_index: u32, |
| 15 | 6 | /// Size of the atom, used to calculate section sizes in the final binary |
| 16 | size: u32, | |
| 7 | size: u32 = 0, | |
| 17 | 8 | /// List of relocations belonging to this atom |
| 18 | 9 | relocs: std.ArrayListUnmanaged(types.Relocation) = .{}, |
| 19 | 10 | /// Contains the binary data of an atom, which can be non-relocated |
| 20 | 11 | code: std.ArrayListUnmanaged(u8) = .{}, |
| 21 | 12 | /// For code this is 1, for data this is set to the highest value of all segments |
| 22 | alignment: Wasm.Alignment, | |
| 13 | alignment: Wasm.Alignment = .@"1", | |
| 23 | 14 | /// Offset into the section where the atom lives, this already accounts |
| 24 | 15 | /// for alignment. |
| 25 | offset: u32, | |
| 16 | offset: u32 = 0, | |
| 26 | 17 | /// The original offset within the object file. This value is substracted from |
| 27 | 18 | /// relocation offsets to determine where in the `data` to rewrite the value |
| 28 | original_offset: u32, | |
| 29 | /// Represents the index of the file this atom was generated from. | |
| 30 | /// This is 'null' when the atom was generated by a Decl from Zig code. | |
| 31 | file: ?u16, | |
| 19 | original_offset: u32 = 0, | |
| 20 | /// Next atom in relation to this atom. | |
| 21 | /// When null, this atom is the last atom | |
| 22 | next: ?Atom.Index = null, | |
| 32 | 23 | /// Previous atom in relation to this atom. |
| 33 | 24 | /// is null when this atom is the first in its order |
| 34 | prev: ?Atom.Index, | |
| 25 | prev: ?Atom.Index = null, | |
| 35 | 26 | /// Contains atoms local to a decl, all managed by this `Atom`. |
| 36 | 27 | /// When the parent atom is being freed, it will also do so for all local atoms. |
| 37 | 28 | locals: std.ArrayListUnmanaged(Atom.Index) = .{}, |
| 38 | 29 | |
| 39 | /// Alias to an unsigned 32-bit integer | |
| 30 | /// Alias to an unsigned 32-bit integer. | |
| 31 | // TODO: Make this a non-exhaustive enum. | |
| 40 | 32 | pub const Index = u32; |
| 41 | 33 | |
| 42 | /// Represents a default empty wasm `Atom` | |
| 43 | pub const empty: Atom = .{ | |
| 44 | .alignment = .@"1", | |
| 45 | .file = null, | |
| 46 | .offset = 0, | |
| 47 | .prev = null, | |
| 48 | .size = 0, | |
| 49 | .sym_index = 0, | |
| 50 | .original_offset = 0, | |
| 51 | }; | |
| 52 | ||
| 53 | 34 | /// Frees all resources owned by this `Atom`. |
| 54 | 35 | pub fn deinit(atom: *Atom, gpa: std.mem.Allocator) void { |
| 55 | 36 | atom.relocs.deinit(gpa); |
| ... | ... | @@ -217,3 +198,14 @@ fn thombstone(atom: Atom, wasm: *const Wasm) ?i64 { |
| 217 | 198 | } |
| 218 | 199 | return null; |
| 219 | 200 | } |
| 201 | const leb = std.leb; | |
| 202 | const log = std.log.scoped(.link); | |
| 203 | const mem = std.mem; | |
| 204 | const std = @import("std"); | |
| 205 | const types = @import("types.zig"); | |
| 206 | ||
| 207 | const Allocator = mem.Allocator; | |
| 208 | const Atom = @This(); | |
| 209 | const FileIndex = @import("file.zig").File.Index; | |
| 210 | const Symbol = @import("Symbol.zig"); | |
| 211 | const Wasm = @import("../Wasm.zig"); |
src/link/Wasm/ZigObject.zig+8-5| ... | ... | @@ -4,6 +4,8 @@ |
| 4 | 4 | //! Think about this as fake in-memory Object file for the Zig module. |
| 5 | 5 | |
| 6 | 6 | path: []const u8, |
| 7 | /// Index within the list of relocatable objects of the linker driver. | |
| 8 | index: File.Index, | |
| 7 | 9 | /// List of all `Decl` that are currently alive. |
| 8 | 10 | /// Each index maps to the corresponding `Atom.Index`. |
| 9 | 11 | decls: std.AutoHashMapUnmanaged(InternPool.DeclIndex, Atom.Index) = .{}, |
| ... | ... | @@ -292,7 +294,7 @@ pub fn getOrCreateAtomForDecl(zig_object: *ZigObject, wasm_file: *Wasm, decl_ind |
| 292 | 294 | const gop = try zig_object.decls.getOrPut(gpa, decl_index); |
| 293 | 295 | if (!gop.found_existing) { |
| 294 | 296 | const sym_index = try zig_object.allocateSymbol(gpa); |
| 295 | gop.value_ptr.* = try wasm_file.createAtom(sym_index); | |
| 297 | gop.value_ptr.* = try wasm_file.createAtom(sym_index, zig_object.index); | |
| 296 | 298 | const mod = wasm_file.base.comp.module.?; |
| 297 | 299 | const decl = mod.declPtr(decl_index); |
| 298 | 300 | const full_name = mod.intern_pool.stringToSlice(try decl.getFullyQualifiedName(mod)); |
| ... | ... | @@ -379,7 +381,7 @@ fn lowerConst(zig_object: *ZigObject, wasm_file: *Wasm, name: []const u8, tv: Ty |
| 379 | 381 | |
| 380 | 382 | // Create and initialize a new local symbol and atom |
| 381 | 383 | const sym_index = try zig_object.allocateSymbol(gpa); |
| 382 | const atom_index = try wasm_file.createAtom(sym_index); | |
| 384 | const atom_index = try wasm_file.createAtom(sym_index, zig_object.index); | |
| 383 | 385 | var value_bytes = std.ArrayList(u8).init(gpa); |
| 384 | 386 | defer value_bytes.deinit(); |
| 385 | 387 | |
| ... | ... | @@ -432,7 +434,7 @@ pub fn getErrorTableSymbol(zig_object: *ZigObject, wasm_file: *Wasm) !u32 { |
| 432 | 434 | // during `flush` when we know all possible error names. |
| 433 | 435 | const gpa = wasm_file.base.comp.gpa; |
| 434 | 436 | const sym_index = try zig_object.allocateSymbol(gpa); |
| 435 | const atom_index = try wasm_file.createAtom(sym_index); | |
| 437 | const atom_index = try wasm_file.createAtom(sym_index, zig_object.index); | |
| 436 | 438 | const atom = wasm_file.getAtomPtr(atom_index); |
| 437 | 439 | const slice_ty = Type.slice_const_u8_sentinel_0; |
| 438 | 440 | const mod = wasm_file.base.comp.module.?; |
| ... | ... | @@ -468,7 +470,7 @@ fn populateErrorNameTable(zig_object: *ZigObject, wasm_file: *Wasm) !void { |
| 468 | 470 | // we create a symbol for the entire region of error names. We then calculate |
| 469 | 471 | // the pointers into the list using addends which are appended to the relocation. |
| 470 | 472 | const names_sym_index = try zig_object.allocateSymbol(gpa); |
| 471 | const names_atom_index = try wasm_file.createAtom(names_sym_index); | |
| 473 | const names_atom_index = try wasm_file.createAtom(names_sym_index, zig_object.index); | |
| 472 | 474 | const names_atom = wasm_file.getAtomPtr(names_atom_index); |
| 473 | 475 | names_atom.alignment = .@"1"; |
| 474 | 476 | const sym_name = try zig_object.string_table.insert(gpa, "__zig_err_names"); |
| ... | ... | @@ -1087,7 +1089,7 @@ pub fn createDebugSectionForIndex(zig_object: *ZigObject, wasm_file: *Wasm, inde |
| 1087 | 1089 | try zig_object.appendDummySegment(); |
| 1088 | 1090 | |
| 1089 | 1091 | const sym_index = try zig_object.allocateSymbol(gpa); |
| 1090 | const atom_index = try wasm_file.createAtom(sym_index); | |
| 1092 | const atom_index = try wasm_file.createAtom(sym_index, zig_object.index); | |
| 1091 | 1093 | const atom = wasm_file.getAtomPtr(atom_index); |
| 1092 | 1094 | zig_object.symbols.items[sym_index] = .{ |
| 1093 | 1095 | .tag = .section, |
| ... | ... | @@ -1161,6 +1163,7 @@ const types = @import("types.zig"); |
| 1161 | 1163 | const Air = @import("../../Air.zig"); |
| 1162 | 1164 | const Atom = @import("Atom.zig"); |
| 1163 | 1165 | const Dwarf = @import("../Dwarf.zig"); |
| 1166 | const File = @import("file.zig").File; | |
| 1164 | 1167 | const InternPool = @import("../../InternPool.zig"); |
| 1165 | 1168 | const Liveness = @import("../../Liveness.zig"); |
| 1166 | 1169 | const Module = @import("../../Module.zig"); |