authorgravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2024-01-15 16:43:22+01:00
committergravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2024-02-29 15:23:03+01:00
log12505c6d3d4ccfc859b67e4b43c5b3844bebb475
tree37c53b1a33ebfc4f7f72ad54254b3b4556ee353e
parentf6896ef2180709fedeb5bafde3fe58ca6d06aa3a
signaturelock-open Commit is signed but in an unrecognized format.

wasm: store `File.Index` on the Atom

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(
569569 if (!use_llvm) {
570570 const index: File.Index = @enumFromInt(wasm.files.len);
571571 var zig_object: ZigObject = .{
572 .index = index,
572573 .path = try std.fmt.allocPrint(gpa, "{s}.o", .{std.fs.path.stem(zcu.main_mod.root_src_path)}),
573574 .stack_pointer_sym = undefined,
574575 };
......@@ -663,12 +664,11 @@ fn parseObjectFile(wasm: *Wasm, path: []const u8) !bool {
663664}
664665
665666/// Creates a new empty `Atom` and returns its `Atom.Index`
666pub fn createAtom(wasm: *Wasm, sym_index: u32) !Atom.Index {
667pub fn createAtom(wasm: *Wasm, sym_index: u32, file_index: File.Index) !Atom.Index {
667668 const gpa = wasm.base.comp.gpa;
668669 const index: Atom.Index = @intCast(wasm.managed_atoms.items.len);
669670 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 };
672672 try wasm.symbol_atom.putNoClobber(gpa, .{ .file = null, .index = sym_index }, index);
673673
674674 return index;
......@@ -1825,20 +1825,11 @@ fn createSyntheticFunction(
18251825 symbol.index = func_index;
18261826
18271827 // 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);
18401832 try wasm.appendAtomAtIndex(wasm.code_section_index.?, atom_index);
1841 try wasm.symbol_atom.putNoClobber(gpa, loc, atom_index);
18421833}
18431834
18441835/// Unlike `createSyntheticFunction` this function is to be called by
......@@ -1856,19 +1847,11 @@ pub fn createFunction(
18561847 const gpa = wasm.base.comp.gpa;
18571848 const loc = try wasm.createSyntheticSymbol(symbol_name, .function);
18581849
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);
18721855 const symbol = loc.getSymbol(wasm);
18731856 symbol.setFlag(.WASM_SYM_VISIBILITY_HIDDEN); // ensure function does not get exported
18741857
......@@ -1878,7 +1861,6 @@ pub fn createFunction(
18781861 break :idx index;
18791862 };
18801863 try wasm.appendAtomAtIndex(section_index, atom_index);
1881 try wasm.symbol_atom.putNoClobber(gpa, loc, atom_index);
18821864 try wasm.zigObjectPtr().?.atom_types.put(
18831865 gpa,
18841866 atom_index,
src/link/Wasm/Atom.zig+24-32
......@@ -1,55 +1,36 @@
1const Atom = @This();
2
3const std = @import("std");
4const types = @import("types.zig");
5const Wasm = @import("../Wasm.zig");
6const Symbol = @import("Symbol.zig");
7
8const leb = std.leb;
9const log = std.log.scoped(.link);
10const mem = std.mem;
11const 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.
3file: FileIndex,
134/// symbol index of the symbol representing this atom
145sym_index: u32,
156/// Size of the atom, used to calculate section sizes in the final binary
16size: u32,
7size: u32 = 0,
178/// List of relocations belonging to this atom
189relocs: std.ArrayListUnmanaged(types.Relocation) = .{},
1910/// Contains the binary data of an atom, which can be non-relocated
2011code: std.ArrayListUnmanaged(u8) = .{},
2112/// For code this is 1, for data this is set to the highest value of all segments
22alignment: Wasm.Alignment,
13alignment: Wasm.Alignment = .@"1",
2314/// Offset into the section where the atom lives, this already accounts
2415/// for alignment.
25offset: u32,
16offset: u32 = 0,
2617/// The original offset within the object file. This value is substracted from
2718/// relocation offsets to determine where in the `data` to rewrite the value
28original_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.
31file: ?u16,
19original_offset: u32 = 0,
20/// Next atom in relation to this atom.
21/// When null, this atom is the last atom
22next: ?Atom.Index = null,
3223/// Previous atom in relation to this atom.
3324/// is null when this atom is the first in its order
34prev: ?Atom.Index,
25prev: ?Atom.Index = null,
3526/// Contains atoms local to a decl, all managed by this `Atom`.
3627/// When the parent atom is being freed, it will also do so for all local atoms.
3728locals: std.ArrayListUnmanaged(Atom.Index) = .{},
3829
39/// Alias to an unsigned 32-bit integer
30/// Alias to an unsigned 32-bit integer.
31// TODO: Make this a non-exhaustive enum.
4032pub const Index = u32;
4133
42/// Represents a default empty wasm `Atom`
43pub 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
5334/// Frees all resources owned by this `Atom`.
5435pub fn deinit(atom: *Atom, gpa: std.mem.Allocator) void {
5536 atom.relocs.deinit(gpa);
......@@ -217,3 +198,14 @@ fn thombstone(atom: Atom, wasm: *const Wasm) ?i64 {
217198 }
218199 return null;
219200}
201const leb = std.leb;
202const log = std.log.scoped(.link);
203const mem = std.mem;
204const std = @import("std");
205const types = @import("types.zig");
206
207const Allocator = mem.Allocator;
208const Atom = @This();
209const FileIndex = @import("file.zig").File.Index;
210const Symbol = @import("Symbol.zig");
211const Wasm = @import("../Wasm.zig");
src/link/Wasm/ZigObject.zig+8-5
......@@ -4,6 +4,8 @@
44//! Think about this as fake in-memory Object file for the Zig module.
55
66path: []const u8,
7/// Index within the list of relocatable objects of the linker driver.
8index: File.Index,
79/// List of all `Decl` that are currently alive.
810/// Each index maps to the corresponding `Atom.Index`.
911decls: std.AutoHashMapUnmanaged(InternPool.DeclIndex, Atom.Index) = .{},
......@@ -292,7 +294,7 @@ pub fn getOrCreateAtomForDecl(zig_object: *ZigObject, wasm_file: *Wasm, decl_ind
292294 const gop = try zig_object.decls.getOrPut(gpa, decl_index);
293295 if (!gop.found_existing) {
294296 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);
296298 const mod = wasm_file.base.comp.module.?;
297299 const decl = mod.declPtr(decl_index);
298300 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
379381
380382 // Create and initialize a new local symbol and atom
381383 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);
383385 var value_bytes = std.ArrayList(u8).init(gpa);
384386 defer value_bytes.deinit();
385387
......@@ -432,7 +434,7 @@ pub fn getErrorTableSymbol(zig_object: *ZigObject, wasm_file: *Wasm) !u32 {
432434 // during `flush` when we know all possible error names.
433435 const gpa = wasm_file.base.comp.gpa;
434436 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);
436438 const atom = wasm_file.getAtomPtr(atom_index);
437439 const slice_ty = Type.slice_const_u8_sentinel_0;
438440 const mod = wasm_file.base.comp.module.?;
......@@ -468,7 +470,7 @@ fn populateErrorNameTable(zig_object: *ZigObject, wasm_file: *Wasm) !void {
468470 // we create a symbol for the entire region of error names. We then calculate
469471 // the pointers into the list using addends which are appended to the relocation.
470472 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);
472474 const names_atom = wasm_file.getAtomPtr(names_atom_index);
473475 names_atom.alignment = .@"1";
474476 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
10871089 try zig_object.appendDummySegment();
10881090
10891091 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);
10911093 const atom = wasm_file.getAtomPtr(atom_index);
10921094 zig_object.symbols.items[sym_index] = .{
10931095 .tag = .section,
......@@ -1161,6 +1163,7 @@ const types = @import("types.zig");
11611163const Air = @import("../../Air.zig");
11621164const Atom = @import("Atom.zig");
11631165const Dwarf = @import("../Dwarf.zig");
1166const File = @import("file.zig").File;
11641167const InternPool = @import("../../InternPool.zig");
11651168const Liveness = @import("../../Liveness.zig");
11661169const Module = @import("../../Module.zig");