| ... | ... | @@ -8,7 +8,6 @@ const assert = std.debug.assert; |
| 8 | 8 | const fs = std.fs; |
| 9 | 9 | const leb = std.leb; |
| 10 | 10 | const log = std.log.scoped(.link); |
| 11 | | const wasm = std.wasm; |
| 12 | 11 | |
| 13 | 12 | const Atom = @import("Wasm/Atom.zig"); |
| 14 | 13 | const Dwarf = @import("Dwarf.zig"); |
| ... | ... | @@ -106,17 +105,17 @@ dwarf: ?Dwarf = null, |
| 106 | 105 | |
| 107 | 106 | // Output sections |
| 108 | 107 | /// Output type section |
| 109 | | func_types: std.ArrayListUnmanaged(wasm.Type) = .{}, |
| 108 | func_types: std.ArrayListUnmanaged(std.wasm.Type) = .{}, |
| 110 | 109 | /// Output function section where the key is the original |
| 111 | 110 | /// function index and the value is function. |
| 112 | 111 | /// This allows us to map multiple symbols to the same function. |
| 113 | | functions: std.AutoArrayHashMapUnmanaged(struct { file: ?u16, index: u32 }, wasm.Func) = .{}, |
| 112 | functions: std.AutoArrayHashMapUnmanaged(struct { file: ?u16, index: u32 }, std.wasm.Func) = .{}, |
| 114 | 113 | /// Output global section |
| 115 | | wasm_globals: std.ArrayListUnmanaged(wasm.Global) = .{}, |
| 114 | wasm_globals: std.ArrayListUnmanaged(std.wasm.Global) = .{}, |
| 116 | 115 | /// Memory section |
| 117 | | memories: wasm.Memory = .{ .limits = .{ .min = 0, .max = null } }, |
| 116 | memories: std.wasm.Memory = .{ .limits = .{ .min = 0, .max = null } }, |
| 118 | 117 | /// Output table section |
| 119 | | tables: std.ArrayListUnmanaged(wasm.Table) = .{}, |
| 118 | tables: std.ArrayListUnmanaged(std.wasm.Table) = .{}, |
| 120 | 119 | /// Output export section |
| 121 | 120 | exports: std.ArrayListUnmanaged(types.Export) = .{}, |
| 122 | 121 | |
| ... | ... | @@ -203,39 +202,39 @@ pub const SymbolLoc = struct { |
| 203 | 202 | file: ?u16, |
| 204 | 203 | |
| 205 | 204 | /// From a given location, returns the corresponding symbol in the wasm binary |
| 206 | | pub fn getSymbol(self: SymbolLoc, wasm_bin: *const Wasm) *Symbol { |
| 207 | | if (wasm_bin.discarded.get(self)) |new_loc| { |
| 205 | pub fn getSymbol(loc: SymbolLoc, wasm_bin: *const Wasm) *Symbol { |
| 206 | if (wasm_bin.discarded.get(loc)) |new_loc| { |
| 208 | 207 | return new_loc.getSymbol(wasm_bin); |
| 209 | 208 | } |
| 210 | | if (self.file) |object_index| { |
| 209 | if (loc.file) |object_index| { |
| 211 | 210 | const object = wasm_bin.objects.items[object_index]; |
| 212 | | return &object.symtable[self.index]; |
| 211 | return &object.symtable[loc.index]; |
| 213 | 212 | } |
| 214 | | return &wasm_bin.symbols.items[self.index]; |
| 213 | return &wasm_bin.symbols.items[loc.index]; |
| 215 | 214 | } |
| 216 | 215 | |
| 217 | 216 | /// From a given location, returns the name of the symbol. |
| 218 | | pub fn getName(self: SymbolLoc, wasm_bin: *const Wasm) []const u8 { |
| 219 | | if (wasm_bin.discarded.get(self)) |new_loc| { |
| 217 | pub fn getName(loc: SymbolLoc, wasm_bin: *const Wasm) []const u8 { |
| 218 | if (wasm_bin.discarded.get(loc)) |new_loc| { |
| 220 | 219 | return new_loc.getName(wasm_bin); |
| 221 | 220 | } |
| 222 | | if (self.file) |object_index| { |
| 221 | if (loc.file) |object_index| { |
| 223 | 222 | const object = wasm_bin.objects.items[object_index]; |
| 224 | | return object.string_table.get(object.symtable[self.index].name); |
| 223 | return object.string_table.get(object.symtable[loc.index].name); |
| 225 | 224 | } |
| 226 | | return wasm_bin.string_table.get(wasm_bin.symbols.items[self.index].name); |
| 225 | return wasm_bin.string_table.get(wasm_bin.symbols.items[loc.index].name); |
| 227 | 226 | } |
| 228 | 227 | |
| 229 | 228 | /// From a given symbol location, returns the final location. |
| 230 | 229 | /// e.g. when a symbol was resolved and replaced by the symbol |
| 231 | 230 | /// in a different file, this will return said location. |
| 232 | 231 | /// If the symbol wasn't replaced by another, this will return |
| 233 | | /// the given location itself. |
| 234 | | pub fn finalLoc(self: SymbolLoc, wasm_bin: *const Wasm) SymbolLoc { |
| 235 | | if (wasm_bin.discarded.get(self)) |new_loc| { |
| 232 | /// the given location itwasm. |
| 233 | pub fn finalLoc(loc: SymbolLoc, wasm_bin: *const Wasm) SymbolLoc { |
| 234 | if (wasm_bin.discarded.get(loc)) |new_loc| { |
| 236 | 235 | return new_loc.finalLoc(wasm_bin); |
| 237 | 236 | } |
| 238 | | return self; |
| 237 | return loc; |
| 239 | 238 | } |
| 240 | 239 | }; |
| 241 | 240 | |
| ... | ... | @@ -258,12 +257,12 @@ pub const StringTable = struct { |
| 258 | 257 | /// When found, de-duplicates the string and returns the existing offset instead. |
| 259 | 258 | /// When the string is not found in the `string_table`, a new entry will be inserted |
| 260 | 259 | /// and the new offset to its data will be returned. |
| 261 | | pub fn put(self: *StringTable, allocator: Allocator, string: []const u8) !u32 { |
| 262 | | const gop = try self.string_table.getOrPutContextAdapted( |
| 260 | pub fn put(table: *StringTable, allocator: Allocator, string: []const u8) !u32 { |
| 261 | const gop = try table.string_table.getOrPutContextAdapted( |
| 263 | 262 | allocator, |
| 264 | 263 | string, |
| 265 | | std.hash_map.StringIndexAdapter{ .bytes = &self.string_data }, |
| 266 | | .{ .bytes = &self.string_data }, |
| 264 | std.hash_map.StringIndexAdapter{ .bytes = &table.string_data }, |
| 265 | .{ .bytes = &table.string_data }, |
| 267 | 266 | ); |
| 268 | 267 | if (gop.found_existing) { |
| 269 | 268 | const off = gop.key_ptr.*; |
| ... | ... | @@ -271,13 +270,13 @@ pub const StringTable = struct { |
| 271 | 270 | return off; |
| 272 | 271 | } |
| 273 | 272 | |
| 274 | | try self.string_data.ensureUnusedCapacity(allocator, string.len + 1); |
| 275 | | const offset = @intCast(u32, self.string_data.items.len); |
| 273 | try table.string_data.ensureUnusedCapacity(allocator, string.len + 1); |
| 274 | const offset = @intCast(u32, table.string_data.items.len); |
| 276 | 275 | |
| 277 | 276 | log.debug("writing new string '{s}' at offset 0x{x}", .{ string, offset }); |
| 278 | 277 | |
| 279 | | self.string_data.appendSliceAssumeCapacity(string); |
| 280 | | self.string_data.appendAssumeCapacity(0); |
| 278 | table.string_data.appendSliceAssumeCapacity(string); |
| 279 | table.string_data.appendAssumeCapacity(0); |
| 281 | 280 | |
| 282 | 281 | gop.key_ptr.* = offset; |
| 283 | 282 | |
| ... | ... | @@ -286,26 +285,26 @@ pub const StringTable = struct { |
| 286 | 285 | |
| 287 | 286 | /// From a given offset, returns its corresponding string value. |
| 288 | 287 | /// Asserts offset does not exceed bounds. |
| 289 | | pub fn get(self: StringTable, off: u32) []const u8 { |
| 290 | | assert(off < self.string_data.items.len); |
| 291 | | return mem.sliceTo(@ptrCast([*:0]const u8, self.string_data.items.ptr + off), 0); |
| 288 | pub fn get(table: StringTable, off: u32) []const u8 { |
| 289 | assert(off < table.string_data.items.len); |
| 290 | return mem.sliceTo(@ptrCast([*:0]const u8, table.string_data.items.ptr + off), 0); |
| 292 | 291 | } |
| 293 | 292 | |
| 294 | 293 | /// Returns the offset of a given string when it exists. |
| 295 | 294 | /// Will return null if the given string does not yet exist within the string table. |
| 296 | | pub fn getOffset(self: *StringTable, string: []const u8) ?u32 { |
| 297 | | return self.string_table.getKeyAdapted( |
| 295 | pub fn getOffset(table: *StringTable, string: []const u8) ?u32 { |
| 296 | return table.string_table.getKeyAdapted( |
| 298 | 297 | string, |
| 299 | | std.hash_map.StringIndexAdapter{ .bytes = &self.string_data }, |
| 298 | std.hash_map.StringIndexAdapter{ .bytes = &table.string_data }, |
| 300 | 299 | ); |
| 301 | 300 | } |
| 302 | 301 | |
| 303 | 302 | /// Frees all resources of the string table. Any references pointing |
| 304 | 303 | /// to the strings will be invalid. |
| 305 | | pub fn deinit(self: *StringTable, allocator: Allocator) void { |
| 306 | | self.string_data.deinit(allocator); |
| 307 | | self.string_table.deinit(allocator); |
| 308 | | self.* = undefined; |
| 304 | pub fn deinit(table: *StringTable, allocator: Allocator) void { |
| 305 | table.string_data.deinit(allocator); |
| 306 | table.string_table.deinit(allocator); |
| 307 | table.* = undefined; |
| 309 | 308 | } |
| 310 | 309 | }; |
| 311 | 310 | |
| ... | ... | @@ -370,9 +369,9 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option |
| 370 | 369 | } |
| 371 | 370 | |
| 372 | 371 | pub fn createEmpty(gpa: Allocator, options: link.Options) !*Wasm { |
| 373 | | const self = try gpa.create(Wasm); |
| 374 | | errdefer gpa.destroy(self); |
| 375 | | self.* = .{ |
| 372 | const wasm = try gpa.create(Wasm); |
| 373 | errdefer gpa.destroy(wasm); |
| 374 | wasm.* = .{ |
| 376 | 375 | .base = .{ |
| 377 | 376 | .tag = .wasm, |
| 378 | 377 | .options = options, |
| ... | ... | @@ -385,33 +384,33 @@ pub fn createEmpty(gpa: Allocator, options: link.Options) !*Wasm { |
| 385 | 384 | const use_llvm = build_options.have_llvm and options.use_llvm; |
| 386 | 385 | const use_stage1 = build_options.have_stage1 and options.use_stage1; |
| 387 | 386 | if (use_llvm and !use_stage1) { |
| 388 | | self.llvm_object = try LlvmObject.create(gpa, options); |
| 387 | wasm.llvm_object = try LlvmObject.create(gpa, options); |
| 389 | 388 | } |
| 390 | | return self; |
| 389 | return wasm; |
| 391 | 390 | } |
| 392 | 391 | |
| 393 | 392 | /// Initializes symbols and atoms for the debug sections |
| 394 | 393 | /// Initialization is only done when compiling Zig code. |
| 395 | 394 | /// When Zig is invoked as a linker instead, the atoms |
| 396 | 395 | /// and symbols come from the object files instead. |
| 397 | | pub fn initDebugSections(self: *Wasm) !void { |
| 398 | | if (self.dwarf == null) return; // not compiling Zig code, so no need to pre-initialize debug sections |
| 399 | | assert(self.debug_info_index == null); |
| 396 | pub fn initDebugSections(wasm: *Wasm) !void { |
| 397 | if (wasm.dwarf == null) return; // not compiling Zig code, so no need to pre-initialize debug sections |
| 398 | assert(wasm.debug_info_index == null); |
| 400 | 399 | // this will create an Atom and set the index for us. |
| 401 | | self.debug_info_atom = try self.createDebugSectionForIndex(&self.debug_info_index, ".debug_info"); |
| 402 | | self.debug_line_atom = try self.createDebugSectionForIndex(&self.debug_line_index, ".debug_line"); |
| 403 | | self.debug_loc_atom = try self.createDebugSectionForIndex(&self.debug_loc_index, ".debug_loc"); |
| 404 | | self.debug_abbrev_atom = try self.createDebugSectionForIndex(&self.debug_abbrev_index, ".debug_abbrev"); |
| 405 | | self.debug_ranges_atom = try self.createDebugSectionForIndex(&self.debug_ranges_index, ".debug_ranges"); |
| 406 | | self.debug_str_atom = try self.createDebugSectionForIndex(&self.debug_str_index, ".debug_str"); |
| 407 | | self.debug_pubnames_atom = try self.createDebugSectionForIndex(&self.debug_pubnames_index, ".debug_pubnames"); |
| 408 | | self.debug_pubtypes_atom = try self.createDebugSectionForIndex(&self.debug_pubtypes_index, ".debug_pubtypes"); |
| 400 | wasm.debug_info_atom = try wasm.createDebugSectionForIndex(&wasm.debug_info_index, ".debug_info"); |
| 401 | wasm.debug_line_atom = try wasm.createDebugSectionForIndex(&wasm.debug_line_index, ".debug_line"); |
| 402 | wasm.debug_loc_atom = try wasm.createDebugSectionForIndex(&wasm.debug_loc_index, ".debug_loc"); |
| 403 | wasm.debug_abbrev_atom = try wasm.createDebugSectionForIndex(&wasm.debug_abbrev_index, ".debug_abbrev"); |
| 404 | wasm.debug_ranges_atom = try wasm.createDebugSectionForIndex(&wasm.debug_ranges_index, ".debug_ranges"); |
| 405 | wasm.debug_str_atom = try wasm.createDebugSectionForIndex(&wasm.debug_str_index, ".debug_str"); |
| 406 | wasm.debug_pubnames_atom = try wasm.createDebugSectionForIndex(&wasm.debug_pubnames_index, ".debug_pubnames"); |
| 407 | wasm.debug_pubtypes_atom = try wasm.createDebugSectionForIndex(&wasm.debug_pubtypes_index, ".debug_pubtypes"); |
| 409 | 408 | } |
| 410 | 409 | |
| 411 | | fn parseInputFiles(self: *Wasm, files: []const []const u8) !void { |
| 410 | fn parseInputFiles(wasm: *Wasm, files: []const []const u8) !void { |
| 412 | 411 | for (files) |path| { |
| 413 | | if (try self.parseObjectFile(path)) continue; |
| 414 | | if (try self.parseArchive(path, false)) continue; // load archives lazily |
| 412 | if (try wasm.parseObjectFile(path)) continue; |
| 413 | if (try wasm.parseArchive(path, false)) continue; // load archives lazily |
| 415 | 414 | log.warn("Unexpected file format at path: '{s}'", .{path}); |
| 416 | 415 | } |
| 417 | 416 | } |
| ... | ... | @@ -419,16 +418,16 @@ fn parseInputFiles(self: *Wasm, files: []const []const u8) !void { |
| 419 | 418 | /// Parses the object file from given path. Returns true when the given file was an object |
| 420 | 419 | /// file and parsed successfully. Returns false when file is not an object file. |
| 421 | 420 | /// May return an error instead when parsing failed. |
| 422 | | fn parseObjectFile(self: *Wasm, path: []const u8) !bool { |
| 421 | fn parseObjectFile(wasm: *Wasm, path: []const u8) !bool { |
| 423 | 422 | const file = try fs.cwd().openFile(path, .{}); |
| 424 | 423 | errdefer file.close(); |
| 425 | 424 | |
| 426 | | var object = Object.create(self.base.allocator, file, path, null) catch |err| switch (err) { |
| 425 | var object = Object.create(wasm.base.allocator, file, path, null) catch |err| switch (err) { |
| 427 | 426 | error.InvalidMagicByte, error.NotObjectFile => return false, |
| 428 | 427 | else => |e| return e, |
| 429 | 428 | }; |
| 430 | | errdefer object.deinit(self.base.allocator); |
| 431 | | try self.objects.append(self.base.allocator, object); |
| 429 | errdefer object.deinit(wasm.base.allocator); |
| 430 | try wasm.objects.append(wasm.base.allocator, object); |
| 432 | 431 | return true; |
| 433 | 432 | } |
| 434 | 433 | |
| ... | ... | @@ -440,7 +439,7 @@ fn parseObjectFile(self: *Wasm, path: []const u8) !bool { |
| 440 | 439 | /// When `force_load` is `true`, it will for link all object files in the archive. |
| 441 | 440 | /// When false, it will only link with object files that contain symbols that |
| 442 | 441 | /// are referenced by other object files or Zig code. |
| 443 | | fn parseArchive(self: *Wasm, path: []const u8, force_load: bool) !bool { |
| 442 | fn parseArchive(wasm: *Wasm, path: []const u8, force_load: bool) !bool { |
| 444 | 443 | const file = try fs.cwd().openFile(path, .{}); |
| 445 | 444 | errdefer file.close(); |
| 446 | 445 | |
| ... | ... | @@ -448,25 +447,25 @@ fn parseArchive(self: *Wasm, path: []const u8, force_load: bool) !bool { |
| 448 | 447 | .file = file, |
| 449 | 448 | .name = path, |
| 450 | 449 | }; |
| 451 | | archive.parse(self.base.allocator) catch |err| switch (err) { |
| 450 | archive.parse(wasm.base.allocator) catch |err| switch (err) { |
| 452 | 451 | error.EndOfStream, error.NotArchive => { |
| 453 | | archive.deinit(self.base.allocator); |
| 452 | archive.deinit(wasm.base.allocator); |
| 454 | 453 | return false; |
| 455 | 454 | }, |
| 456 | 455 | else => |e| return e, |
| 457 | 456 | }; |
| 458 | 457 | |
| 459 | 458 | if (!force_load) { |
| 460 | | errdefer archive.deinit(self.base.allocator); |
| 461 | | try self.archives.append(self.base.allocator, archive); |
| 459 | errdefer archive.deinit(wasm.base.allocator); |
| 460 | try wasm.archives.append(wasm.base.allocator, archive); |
| 462 | 461 | return true; |
| 463 | 462 | } |
| 464 | | defer archive.deinit(self.base.allocator); |
| 463 | defer archive.deinit(wasm.base.allocator); |
| 465 | 464 | |
| 466 | 465 | // In this case we must force link all embedded object files within the archive |
| 467 | 466 | // We loop over all symbols, and then group them by offset as the offset |
| 468 | 467 | // notates where the object file starts. |
| 469 | | var offsets = std.AutoArrayHashMap(u32, void).init(self.base.allocator); |
| 468 | var offsets = std.AutoArrayHashMap(u32, void).init(wasm.base.allocator); |
| 470 | 469 | defer offsets.deinit(); |
| 471 | 470 | for (archive.toc.values()) |symbol_offsets| { |
| 472 | 471 | for (symbol_offsets.items) |sym_offset| { |
| ... | ... | @@ -475,15 +474,15 @@ fn parseArchive(self: *Wasm, path: []const u8, force_load: bool) !bool { |
| 475 | 474 | } |
| 476 | 475 | |
| 477 | 476 | for (offsets.keys()) |file_offset| { |
| 478 | | const object = try self.objects.addOne(self.base.allocator); |
| 479 | | object.* = try archive.parseObject(self.base.allocator, file_offset); |
| 477 | const object = try wasm.objects.addOne(wasm.base.allocator); |
| 478 | object.* = try archive.parseObject(wasm.base.allocator, file_offset); |
| 480 | 479 | } |
| 481 | 480 | |
| 482 | 481 | return true; |
| 483 | 482 | } |
| 484 | 483 | |
| 485 | | fn resolveSymbolsInObject(self: *Wasm, object_index: u16) !void { |
| 486 | | const object: Object = self.objects.items[object_index]; |
| 484 | fn resolveSymbolsInObject(wasm: *Wasm, object_index: u16) !void { |
| 485 | const object: Object = wasm.objects.items[object_index]; |
| 487 | 486 | log.debug("Resolving symbols in object: '{s}'", .{object.name}); |
| 488 | 487 | |
| 489 | 488 | for (object.symtable) |symbol, i| { |
| ... | ... | @@ -496,7 +495,7 @@ fn resolveSymbolsInObject(self: *Wasm, object_index: u16) !void { |
| 496 | 495 | if (mem.eql(u8, sym_name, "__indirect_function_table")) { |
| 497 | 496 | continue; |
| 498 | 497 | } |
| 499 | | const sym_name_index = try self.string_table.put(self.base.allocator, sym_name); |
| 498 | const sym_name_index = try wasm.string_table.put(wasm.base.allocator, sym_name); |
| 500 | 499 | |
| 501 | 500 | if (symbol.isLocal()) { |
| 502 | 501 | if (symbol.isUndefined()) { |
| ... | ... | @@ -504,27 +503,27 @@ fn resolveSymbolsInObject(self: *Wasm, object_index: u16) !void { |
| 504 | 503 | log.err(" symbol '{s}' defined in '{s}'", .{ sym_name, object.name }); |
| 505 | 504 | return error.undefinedLocal; |
| 506 | 505 | } |
| 507 | | try self.resolved_symbols.putNoClobber(self.base.allocator, location, {}); |
| 506 | try wasm.resolved_symbols.putNoClobber(wasm.base.allocator, location, {}); |
| 508 | 507 | continue; |
| 509 | 508 | } |
| 510 | 509 | |
| 511 | | const maybe_existing = try self.globals.getOrPut(self.base.allocator, sym_name_index); |
| 510 | const maybe_existing = try wasm.globals.getOrPut(wasm.base.allocator, sym_name_index); |
| 512 | 511 | if (!maybe_existing.found_existing) { |
| 513 | 512 | maybe_existing.value_ptr.* = location; |
| 514 | | try self.resolved_symbols.putNoClobber(self.base.allocator, location, {}); |
| 513 | try wasm.resolved_symbols.putNoClobber(wasm.base.allocator, location, {}); |
| 515 | 514 | |
| 516 | 515 | if (symbol.isUndefined()) { |
| 517 | | try self.undefs.putNoClobber(self.base.allocator, sym_name, location); |
| 516 | try wasm.undefs.putNoClobber(wasm.base.allocator, sym_name, location); |
| 518 | 517 | } |
| 519 | 518 | continue; |
| 520 | 519 | } |
| 521 | 520 | |
| 522 | 521 | const existing_loc = maybe_existing.value_ptr.*; |
| 523 | | const existing_sym: *Symbol = existing_loc.getSymbol(self); |
| 522 | const existing_sym: *Symbol = existing_loc.getSymbol(wasm); |
| 524 | 523 | |
| 525 | 524 | const existing_file_path = if (existing_loc.file) |file| blk: { |
| 526 | | break :blk self.objects.items[file].name; |
| 527 | | } else self.name; |
| 525 | break :blk wasm.objects.items[file].name; |
| 526 | } else wasm.name; |
| 528 | 527 | |
| 529 | 528 | if (!existing_sym.isUndefined()) outer: { |
| 530 | 529 | if (!symbol.isUndefined()) inner: { |
| ... | ... | @@ -541,7 +540,7 @@ fn resolveSymbolsInObject(self: *Wasm, object_index: u16) !void { |
| 541 | 540 | return error.SymbolCollision; |
| 542 | 541 | } |
| 543 | 542 | |
| 544 | | try self.discarded.put(self.base.allocator, location, existing_loc); |
| 543 | try wasm.discarded.put(wasm.base.allocator, location, existing_loc); |
| 545 | 544 | continue; // Do not overwrite defined symbols with undefined symbols |
| 546 | 545 | } |
| 547 | 546 | |
| ... | ... | @@ -554,12 +553,12 @@ fn resolveSymbolsInObject(self: *Wasm, object_index: u16) !void { |
| 554 | 553 | |
| 555 | 554 | if (existing_sym.isUndefined() and symbol.isUndefined()) { |
| 556 | 555 | const existing_name = if (existing_loc.file) |file_index| blk: { |
| 557 | | const obj = self.objects.items[file_index]; |
| 556 | const obj = wasm.objects.items[file_index]; |
| 558 | 557 | const name_index = obj.findImport(symbol.tag.externalType(), existing_sym.index).module_name; |
| 559 | 558 | break :blk obj.string_table.get(name_index); |
| 560 | 559 | } else blk: { |
| 561 | | const name_index = self.imports.get(existing_loc).?.module_name; |
| 562 | | break :blk self.string_table.get(name_index); |
| 560 | const name_index = wasm.imports.get(existing_loc).?.module_name; |
| 561 | break :blk wasm.string_table.get(name_index); |
| 563 | 562 | }; |
| 564 | 563 | |
| 565 | 564 | const module_index = object.findImport(symbol.tag.externalType(), symbol.index).module_name; |
| ... | ... | @@ -577,8 +576,8 @@ fn resolveSymbolsInObject(self: *Wasm, object_index: u16) !void { |
| 577 | 576 | } |
| 578 | 577 | |
| 579 | 578 | if (existing_sym.tag == .global) { |
| 580 | | const existing_ty = self.getGlobalType(existing_loc); |
| 581 | | const new_ty = self.getGlobalType(location); |
| 579 | const existing_ty = wasm.getGlobalType(existing_loc); |
| 580 | const new_ty = wasm.getGlobalType(location); |
| 582 | 581 | if (existing_ty.mutable != new_ty.mutable or existing_ty.valtype != new_ty.valtype) { |
| 583 | 582 | log.err("symbol '{s}' mismatching global types", .{sym_name}); |
| 584 | 583 | log.err(" first definition in '{s}'", .{existing_file_path}); |
| ... | ... | @@ -588,8 +587,8 @@ fn resolveSymbolsInObject(self: *Wasm, object_index: u16) !void { |
| 588 | 587 | } |
| 589 | 588 | |
| 590 | 589 | if (existing_sym.tag == .function) { |
| 591 | | const existing_ty = self.getFunctionSignature(existing_loc); |
| 592 | | const new_ty = self.getFunctionSignature(location); |
| 590 | const existing_ty = wasm.getFunctionSignature(existing_loc); |
| 591 | const new_ty = wasm.getFunctionSignature(location); |
| 593 | 592 | if (!existing_ty.eql(new_ty)) { |
| 594 | 593 | log.err("symbol '{s}' mismatching function signatures.", .{sym_name}); |
| 595 | 594 | log.err(" expected signature {}, but found signature {}", .{ existing_ty, new_ty }); |
| ... | ... | @@ -601,7 +600,7 @@ fn resolveSymbolsInObject(self: *Wasm, object_index: u16) !void { |
| 601 | 600 | |
| 602 | 601 | // when both symbols are weak, we skip overwriting |
| 603 | 602 | if (existing_sym.isWeak() and symbol.isWeak()) { |
| 604 | | try self.discarded.put(self.base.allocator, location, existing_loc); |
| 603 | try wasm.discarded.put(wasm.base.allocator, location, existing_loc); |
| 605 | 604 | continue; |
| 606 | 605 | } |
| 607 | 606 | |
| ... | ... | @@ -609,27 +608,27 @@ fn resolveSymbolsInObject(self: *Wasm, object_index: u16) !void { |
| 609 | 608 | log.debug("Overwriting symbol '{s}'", .{sym_name}); |
| 610 | 609 | log.debug(" old definition in '{s}'", .{existing_file_path}); |
| 611 | 610 | log.debug(" new definition in '{s}'", .{object.name}); |
| 612 | | try self.discarded.putNoClobber(self.base.allocator, existing_loc, location); |
| 611 | try wasm.discarded.putNoClobber(wasm.base.allocator, existing_loc, location); |
| 613 | 612 | maybe_existing.value_ptr.* = location; |
| 614 | | try self.globals.put(self.base.allocator, sym_name_index, location); |
| 615 | | try self.resolved_symbols.put(self.base.allocator, location, {}); |
| 616 | | assert(self.resolved_symbols.swapRemove(existing_loc)); |
| 613 | try wasm.globals.put(wasm.base.allocator, sym_name_index, location); |
| 614 | try wasm.resolved_symbols.put(wasm.base.allocator, location, {}); |
| 615 | assert(wasm.resolved_symbols.swapRemove(existing_loc)); |
| 617 | 616 | if (existing_sym.isUndefined()) { |
| 618 | | assert(self.undefs.swapRemove(sym_name)); |
| 617 | assert(wasm.undefs.swapRemove(sym_name)); |
| 619 | 618 | } |
| 620 | 619 | } |
| 621 | 620 | } |
| 622 | 621 | |
| 623 | | fn resolveSymbolsInArchives(self: *Wasm) !void { |
| 624 | | if (self.archives.items.len == 0) return; |
| 622 | fn resolveSymbolsInArchives(wasm: *Wasm) !void { |
| 623 | if (wasm.archives.items.len == 0) return; |
| 625 | 624 | |
| 626 | 625 | log.debug("Resolving symbols in archives", .{}); |
| 627 | 626 | var index: u32 = 0; |
| 628 | | undef_loop: while (index < self.undefs.count()) { |
| 629 | | const undef_sym_loc = self.undefs.values()[index]; |
| 630 | | const sym_name = undef_sym_loc.getName(self); |
| 627 | undef_loop: while (index < wasm.undefs.count()) { |
| 628 | const undef_sym_loc = wasm.undefs.values()[index]; |
| 629 | const sym_name = undef_sym_loc.getName(wasm); |
| 631 | 630 | |
| 632 | | for (self.archives.items) |archive| { |
| 631 | for (wasm.archives.items) |archive| { |
| 633 | 632 | const offset = archive.toc.get(sym_name) orelse { |
| 634 | 633 | // symbol does not exist in this archive |
| 635 | 634 | continue; |
| ... | ... | @@ -639,10 +638,10 @@ fn resolveSymbolsInArchives(self: *Wasm) !void { |
| 639 | 638 | // Symbol is found in unparsed object file within current archive. |
| 640 | 639 | // Parse object and and resolve symbols again before we check remaining |
| 641 | 640 | // undefined symbols. |
| 642 | | const object_file_index = @intCast(u16, self.objects.items.len); |
| 643 | | var object = try archive.parseObject(self.base.allocator, offset.items[0]); |
| 644 | | try self.objects.append(self.base.allocator, object); |
| 645 | | try self.resolveSymbolsInObject(object_file_index); |
| 641 | const object_file_index = @intCast(u16, wasm.objects.items.len); |
| 642 | var object = try archive.parseObject(wasm.base.allocator, offset.items[0]); |
| 643 | try wasm.objects.append(wasm.base.allocator, object); |
| 644 | try wasm.resolveSymbolsInObject(object_file_index); |
| 646 | 645 | |
| 647 | 646 | // continue loop for any remaining undefined symbols that still exist |
| 648 | 647 | // after resolving last object file |
| ... | ... | @@ -652,18 +651,18 @@ fn resolveSymbolsInArchives(self: *Wasm) !void { |
| 652 | 651 | } |
| 653 | 652 | } |
| 654 | 653 | |
| 655 | | fn checkUndefinedSymbols(self: *const Wasm) !void { |
| 656 | | if (self.base.options.output_mode == .Obj) return; |
| 654 | fn checkUndefinedSymbols(wasm: *const Wasm) !void { |
| 655 | if (wasm.base.options.output_mode == .Obj) return; |
| 657 | 656 | |
| 658 | 657 | var found_undefined_symbols = false; |
| 659 | | for (self.undefs.values()) |undef| { |
| 660 | | const symbol = undef.getSymbol(self); |
| 658 | for (wasm.undefs.values()) |undef| { |
| 659 | const symbol = undef.getSymbol(wasm); |
| 661 | 660 | if (symbol.tag == .data) { |
| 662 | 661 | found_undefined_symbols = true; |
| 663 | 662 | const file_name = if (undef.file) |file_index| name: { |
| 664 | | break :name self.objects.items[file_index].name; |
| 665 | | } else self.name; |
| 666 | | log.err("could not resolve undefined symbol '{s}'", .{undef.getName(self)}); |
| 663 | break :name wasm.objects.items[file_index].name; |
| 664 | } else wasm.name; |
| 665 | log.err("could not resolve undefined symbol '{s}'", .{undef.getName(wasm)}); |
| 667 | 666 | log.err(" defined in '{s}'", .{file_name}); |
| 668 | 667 | } |
| 669 | 668 | } |
| ... | ... | @@ -672,80 +671,80 @@ fn checkUndefinedSymbols(self: *const Wasm) !void { |
| 672 | 671 | } |
| 673 | 672 | } |
| 674 | 673 | |
| 675 | | pub fn deinit(self: *Wasm) void { |
| 676 | | const gpa = self.base.allocator; |
| 674 | pub fn deinit(wasm: *Wasm) void { |
| 675 | const gpa = wasm.base.allocator; |
| 677 | 676 | if (build_options.have_llvm) { |
| 678 | | if (self.llvm_object) |llvm_object| llvm_object.destroy(gpa); |
| 677 | if (wasm.llvm_object) |llvm_object| llvm_object.destroy(gpa); |
| 679 | 678 | } |
| 680 | 679 | |
| 681 | | if (self.base.options.module) |mod| { |
| 682 | | var decl_it = self.decls.keyIterator(); |
| 680 | if (wasm.base.options.module) |mod| { |
| 681 | var decl_it = wasm.decls.keyIterator(); |
| 683 | 682 | while (decl_it.next()) |decl_index_ptr| { |
| 684 | 683 | const decl = mod.declPtr(decl_index_ptr.*); |
| 685 | 684 | decl.link.wasm.deinit(gpa); |
| 686 | 685 | } |
| 687 | 686 | } else { |
| 688 | | assert(self.decls.count() == 0); |
| 687 | assert(wasm.decls.count() == 0); |
| 689 | 688 | } |
| 690 | 689 | |
| 691 | | for (self.func_types.items) |*func_type| { |
| 690 | for (wasm.func_types.items) |*func_type| { |
| 692 | 691 | func_type.deinit(gpa); |
| 693 | 692 | } |
| 694 | | for (self.segment_info.values()) |segment_info| { |
| 693 | for (wasm.segment_info.values()) |segment_info| { |
| 695 | 694 | gpa.free(segment_info.name); |
| 696 | 695 | } |
| 697 | | for (self.objects.items) |*object| { |
| 696 | for (wasm.objects.items) |*object| { |
| 698 | 697 | object.deinit(gpa); |
| 699 | 698 | } |
| 700 | 699 | |
| 701 | | for (self.archives.items) |*archive| { |
| 700 | for (wasm.archives.items) |*archive| { |
| 702 | 701 | archive.deinit(gpa); |
| 703 | 702 | } |
| 704 | 703 | |
| 705 | | self.decls.deinit(gpa); |
| 706 | | self.symbols.deinit(gpa); |
| 707 | | self.symbols_free_list.deinit(gpa); |
| 708 | | self.globals.deinit(gpa); |
| 709 | | self.resolved_symbols.deinit(gpa); |
| 710 | | self.undefs.deinit(gpa); |
| 711 | | self.discarded.deinit(gpa); |
| 712 | | self.symbol_atom.deinit(gpa); |
| 713 | | self.export_names.deinit(gpa); |
| 714 | | self.atoms.deinit(gpa); |
| 715 | | for (self.managed_atoms.items) |managed_atom| { |
| 704 | wasm.decls.deinit(gpa); |
| 705 | wasm.symbols.deinit(gpa); |
| 706 | wasm.symbols_free_list.deinit(gpa); |
| 707 | wasm.globals.deinit(gpa); |
| 708 | wasm.resolved_symbols.deinit(gpa); |
| 709 | wasm.undefs.deinit(gpa); |
| 710 | wasm.discarded.deinit(gpa); |
| 711 | wasm.symbol_atom.deinit(gpa); |
| 712 | wasm.export_names.deinit(gpa); |
| 713 | wasm.atoms.deinit(gpa); |
| 714 | for (wasm.managed_atoms.items) |managed_atom| { |
| 716 | 715 | managed_atom.deinit(gpa); |
| 717 | 716 | gpa.destroy(managed_atom); |
| 718 | 717 | } |
| 719 | | self.managed_atoms.deinit(gpa); |
| 720 | | self.segments.deinit(gpa); |
| 721 | | self.data_segments.deinit(gpa); |
| 722 | | self.segment_info.deinit(gpa); |
| 723 | | self.objects.deinit(gpa); |
| 724 | | self.archives.deinit(gpa); |
| 718 | wasm.managed_atoms.deinit(gpa); |
| 719 | wasm.segments.deinit(gpa); |
| 720 | wasm.data_segments.deinit(gpa); |
| 721 | wasm.segment_info.deinit(gpa); |
| 722 | wasm.objects.deinit(gpa); |
| 723 | wasm.archives.deinit(gpa); |
| 725 | 724 | |
| 726 | 725 | // free output sections |
| 727 | | self.imports.deinit(gpa); |
| 728 | | self.func_types.deinit(gpa); |
| 729 | | self.functions.deinit(gpa); |
| 730 | | self.wasm_globals.deinit(gpa); |
| 731 | | self.function_table.deinit(gpa); |
| 732 | | self.tables.deinit(gpa); |
| 733 | | self.exports.deinit(gpa); |
| 726 | wasm.imports.deinit(gpa); |
| 727 | wasm.func_types.deinit(gpa); |
| 728 | wasm.functions.deinit(gpa); |
| 729 | wasm.wasm_globals.deinit(gpa); |
| 730 | wasm.function_table.deinit(gpa); |
| 731 | wasm.tables.deinit(gpa); |
| 732 | wasm.exports.deinit(gpa); |
| 734 | 733 | |
| 735 | | self.string_table.deinit(gpa); |
| 734 | wasm.string_table.deinit(gpa); |
| 736 | 735 | |
| 737 | | if (self.dwarf) |*dwarf| { |
| 736 | if (wasm.dwarf) |*dwarf| { |
| 738 | 737 | dwarf.deinit(); |
| 739 | 738 | } |
| 740 | 739 | } |
| 741 | 740 | |
| 742 | | pub fn allocateDeclIndexes(self: *Wasm, decl_index: Module.Decl.Index) !void { |
| 743 | | if (self.llvm_object) |_| return; |
| 744 | | const decl = self.base.options.module.?.declPtr(decl_index); |
| 741 | pub fn allocateDeclIndexes(wasm: *Wasm, decl_index: Module.Decl.Index) !void { |
| 742 | if (wasm.llvm_object) |_| return; |
| 743 | const decl = wasm.base.options.module.?.declPtr(decl_index); |
| 745 | 744 | if (decl.link.wasm.sym_index != 0) return; |
| 746 | 745 | |
| 747 | | try self.symbols.ensureUnusedCapacity(self.base.allocator, 1); |
| 748 | | try self.decls.putNoClobber(self.base.allocator, decl_index, {}); |
| 746 | try wasm.symbols.ensureUnusedCapacity(wasm.base.allocator, 1); |
| 747 | try wasm.decls.putNoClobber(wasm.base.allocator, decl_index, {}); |
| 749 | 748 | |
| 750 | 749 | const atom = &decl.link.wasm; |
| 751 | 750 | |
| ... | ... | @@ -756,22 +755,22 @@ pub fn allocateDeclIndexes(self: *Wasm, decl_index: Module.Decl.Index) !void { |
| 756 | 755 | .index = undefined, // will be set after updateDecl |
| 757 | 756 | }; |
| 758 | 757 | |
| 759 | | if (self.symbols_free_list.popOrNull()) |index| { |
| 758 | if (wasm.symbols_free_list.popOrNull()) |index| { |
| 760 | 759 | atom.sym_index = index; |
| 761 | | self.symbols.items[index] = symbol; |
| 760 | wasm.symbols.items[index] = symbol; |
| 762 | 761 | } else { |
| 763 | | atom.sym_index = @intCast(u32, self.symbols.items.len); |
| 764 | | self.symbols.appendAssumeCapacity(symbol); |
| 762 | atom.sym_index = @intCast(u32, wasm.symbols.items.len); |
| 763 | wasm.symbols.appendAssumeCapacity(symbol); |
| 765 | 764 | } |
| 766 | | try self.symbol_atom.putNoClobber(self.base.allocator, atom.symbolLoc(), atom); |
| 765 | try wasm.symbol_atom.putNoClobber(wasm.base.allocator, atom.symbolLoc(), atom); |
| 767 | 766 | } |
| 768 | 767 | |
| 769 | | pub fn updateFunc(self: *Wasm, mod: *Module, func: *Module.Fn, air: Air, liveness: Liveness) !void { |
| 768 | pub fn updateFunc(wasm: *Wasm, mod: *Module, func: *Module.Fn, air: Air, liveness: Liveness) !void { |
| 770 | 769 | if (build_options.skip_non_native and builtin.object_format != .wasm) { |
| 771 | 770 | @panic("Attempted to compile for object format that was disabled by build configuration"); |
| 772 | 771 | } |
| 773 | 772 | if (build_options.have_llvm) { |
| 774 | | if (self.llvm_object) |llvm_object| return llvm_object.updateFunc(mod, func, air, liveness); |
| 773 | if (wasm.llvm_object) |llvm_object| return llvm_object.updateFunc(mod, func, air, liveness); |
| 775 | 774 | } |
| 776 | 775 | |
| 777 | 776 | const tracy = trace(@src()); |
| ... | ... | @@ -783,13 +782,13 @@ pub fn updateFunc(self: *Wasm, mod: *Module, func: *Module.Fn, air: Air, livenes |
| 783 | 782 | |
| 784 | 783 | decl.link.wasm.clear(); |
| 785 | 784 | |
| 786 | | var decl_state: ?Dwarf.DeclState = if (self.dwarf) |*dwarf| try dwarf.initDeclState(mod, decl) else null; |
| 785 | var decl_state: ?Dwarf.DeclState = if (wasm.dwarf) |*dwarf| try dwarf.initDeclState(mod, decl) else null; |
| 787 | 786 | defer if (decl_state) |*ds| ds.deinit(); |
| 788 | 787 | |
| 789 | | var code_writer = std.ArrayList(u8).init(self.base.allocator); |
| 788 | var code_writer = std.ArrayList(u8).init(wasm.base.allocator); |
| 790 | 789 | defer code_writer.deinit(); |
| 791 | 790 | const result = try codegen.generateFunction( |
| 792 | | &self.base, |
| 791 | &wasm.base, |
| 793 | 792 | decl.srcLoc(), |
| 794 | 793 | func, |
| 795 | 794 | air, |
| ... | ... | @@ -807,9 +806,9 @@ pub fn updateFunc(self: *Wasm, mod: *Module, func: *Module.Fn, air: Air, livenes |
| 807 | 806 | }, |
| 808 | 807 | }; |
| 809 | 808 | |
| 810 | | if (self.dwarf) |*dwarf| { |
| 809 | if (wasm.dwarf) |*dwarf| { |
| 811 | 810 | try dwarf.commitDeclState( |
| 812 | | &self.base, |
| 811 | &wasm.base, |
| 813 | 812 | mod, |
| 814 | 813 | decl, |
| 815 | 814 | // Actual value will be written after relocation. |
| ... | ... | @@ -820,17 +819,17 @@ pub fn updateFunc(self: *Wasm, mod: *Module, func: *Module.Fn, air: Air, livenes |
| 820 | 819 | &decl_state.?, |
| 821 | 820 | ); |
| 822 | 821 | } |
| 823 | | return self.finishUpdateDecl(decl, code); |
| 822 | return wasm.finishUpdateDecl(decl, code); |
| 824 | 823 | } |
| 825 | 824 | |
| 826 | 825 | // Generate code for the Decl, storing it in memory to be later written to |
| 827 | 826 | // the file on flush(). |
| 828 | | pub fn updateDecl(self: *Wasm, mod: *Module, decl_index: Module.Decl.Index) !void { |
| 827 | pub fn updateDecl(wasm: *Wasm, mod: *Module, decl_index: Module.Decl.Index) !void { |
| 829 | 828 | if (build_options.skip_non_native and builtin.object_format != .wasm) { |
| 830 | 829 | @panic("Attempted to compile for object format that was disabled by build configuration"); |
| 831 | 830 | } |
| 832 | 831 | if (build_options.have_llvm) { |
| 833 | | if (self.llvm_object) |llvm_object| return llvm_object.updateDecl(mod, decl_index); |
| 832 | if (wasm.llvm_object) |llvm_object| return llvm_object.updateDecl(mod, decl_index); |
| 834 | 833 | } |
| 835 | 834 | |
| 836 | 835 | const tracy = trace(@src()); |
| ... | ... | @@ -850,15 +849,15 @@ pub fn updateDecl(self: *Wasm, mod: *Module, decl_index: Module.Decl.Index) !voi |
| 850 | 849 | if (decl.isExtern()) { |
| 851 | 850 | const variable = decl.getVariable().?; |
| 852 | 851 | const name = mem.sliceTo(decl.name, 0); |
| 853 | | return self.addOrUpdateImport(name, decl.link.wasm.sym_index, variable.lib_name, null); |
| 852 | return wasm.addOrUpdateImport(name, decl.link.wasm.sym_index, variable.lib_name, null); |
| 854 | 853 | } |
| 855 | 854 | const val = if (decl.val.castTag(.variable)) |payload| payload.data.init else decl.val; |
| 856 | 855 | |
| 857 | | var code_writer = std.ArrayList(u8).init(self.base.allocator); |
| 856 | var code_writer = std.ArrayList(u8).init(wasm.base.allocator); |
| 858 | 857 | defer code_writer.deinit(); |
| 859 | 858 | |
| 860 | 859 | const res = try codegen.generateSymbol( |
| 861 | | &self.base, |
| 860 | &wasm.base, |
| 862 | 861 | decl.srcLoc(), |
| 863 | 862 | .{ .ty = decl.ty, .val = val }, |
| 864 | 863 | &code_writer, |
| ... | ... | @@ -876,46 +875,46 @@ pub fn updateDecl(self: *Wasm, mod: *Module, decl_index: Module.Decl.Index) !voi |
| 876 | 875 | }, |
| 877 | 876 | }; |
| 878 | 877 | |
| 879 | | return self.finishUpdateDecl(decl, code); |
| 878 | return wasm.finishUpdateDecl(decl, code); |
| 880 | 879 | } |
| 881 | 880 | |
| 882 | | pub fn updateDeclLineNumber(self: *Wasm, mod: *Module, decl: *const Module.Decl) !void { |
| 883 | | if (self.llvm_object) |_| return; |
| 884 | | if (self.dwarf) |*dw| { |
| 881 | pub fn updateDeclLineNumber(wasm: *Wasm, mod: *Module, decl: *const Module.Decl) !void { |
| 882 | if (wasm.llvm_object) |_| return; |
| 883 | if (wasm.dwarf) |*dw| { |
| 885 | 884 | const tracy = trace(@src()); |
| 886 | 885 | defer tracy.end(); |
| 887 | 886 | |
| 888 | 887 | const decl_name = try decl.getFullyQualifiedName(mod); |
| 889 | | defer self.base.allocator.free(decl_name); |
| 888 | defer wasm.base.allocator.free(decl_name); |
| 890 | 889 | |
| 891 | 890 | log.debug("updateDeclLineNumber {s}{*}", .{ decl_name, decl }); |
| 892 | | try dw.updateDeclLineNumber(&self.base, decl); |
| 891 | try dw.updateDeclLineNumber(&wasm.base, decl); |
| 893 | 892 | } |
| 894 | 893 | } |
| 895 | 894 | |
| 896 | | fn finishUpdateDecl(self: *Wasm, decl: *Module.Decl, code: []const u8) !void { |
| 897 | | const mod = self.base.options.module.?; |
| 895 | fn finishUpdateDecl(wasm: *Wasm, decl: *Module.Decl, code: []const u8) !void { |
| 896 | const mod = wasm.base.options.module.?; |
| 898 | 897 | const atom: *Atom = &decl.link.wasm; |
| 899 | | const symbol = &self.symbols.items[atom.sym_index]; |
| 898 | const symbol = &wasm.symbols.items[atom.sym_index]; |
| 900 | 899 | const full_name = try decl.getFullyQualifiedName(mod); |
| 901 | | defer self.base.allocator.free(full_name); |
| 902 | | symbol.name = try self.string_table.put(self.base.allocator, full_name); |
| 903 | | try atom.code.appendSlice(self.base.allocator, code); |
| 904 | | try self.resolved_symbols.put(self.base.allocator, atom.symbolLoc(), {}); |
| 900 | defer wasm.base.allocator.free(full_name); |
| 901 | symbol.name = try wasm.string_table.put(wasm.base.allocator, full_name); |
| 902 | try atom.code.appendSlice(wasm.base.allocator, code); |
| 903 | try wasm.resolved_symbols.put(wasm.base.allocator, atom.symbolLoc(), {}); |
| 905 | 904 | |
| 906 | 905 | if (code.len == 0) return; |
| 907 | 906 | atom.size = @intCast(u32, code.len); |
| 908 | | atom.alignment = decl.ty.abiAlignment(self.base.options.target); |
| 907 | atom.alignment = decl.ty.abiAlignment(wasm.base.options.target); |
| 909 | 908 | } |
| 910 | 909 | |
| 911 | 910 | /// From a given symbol location, returns its `wasm.GlobalType`. |
| 912 | 911 | /// Asserts the Symbol represents a global. |
| 913 | | fn getGlobalType(self: *const Wasm, loc: SymbolLoc) wasm.GlobalType { |
| 914 | | const symbol = loc.getSymbol(self); |
| 912 | fn getGlobalType(wasm: *const Wasm, loc: SymbolLoc) std.wasm.GlobalType { |
| 913 | const symbol = loc.getSymbol(wasm); |
| 915 | 914 | assert(symbol.tag == .global); |
| 916 | 915 | const is_undefined = symbol.isUndefined(); |
| 917 | 916 | if (loc.file) |file_index| { |
| 918 | | const obj: Object = self.objects.items[file_index]; |
| 917 | const obj: Object = wasm.objects.items[file_index]; |
| 919 | 918 | if (is_undefined) { |
| 920 | 919 | return obj.findImport(.global, symbol.index).kind.global; |
| 921 | 920 | } |
| ... | ... | @@ -923,19 +922,19 @@ fn getGlobalType(self: *const Wasm, loc: SymbolLoc) wasm.GlobalType { |
| 923 | 922 | return obj.globals[symbol.index - import_global_count].global_type; |
| 924 | 923 | } |
| 925 | 924 | if (is_undefined) { |
| 926 | | return self.imports.get(loc).?.kind.global; |
| 925 | return wasm.imports.get(loc).?.kind.global; |
| 927 | 926 | } |
| 928 | | return self.wasm_globals.items[symbol.index].global_type; |
| 927 | return wasm.wasm_globals.items[symbol.index].global_type; |
| 929 | 928 | } |
| 930 | 929 | |
| 931 | 930 | /// From a given symbol location, returns its `wasm.Type`. |
| 932 | 931 | /// Asserts the Symbol represents a function. |
| 933 | | fn getFunctionSignature(self: *const Wasm, loc: SymbolLoc) wasm.Type { |
| 934 | | const symbol = loc.getSymbol(self); |
| 932 | fn getFunctionSignature(wasm: *const Wasm, loc: SymbolLoc) std.wasm.Type { |
| 933 | const symbol = loc.getSymbol(wasm); |
| 935 | 934 | assert(symbol.tag == .function); |
| 936 | 935 | const is_undefined = symbol.isUndefined(); |
| 937 | 936 | if (loc.file) |file_index| { |
| 938 | | const obj: Object = self.objects.items[file_index]; |
| 937 | const obj: Object = wasm.objects.items[file_index]; |
| 939 | 938 | if (is_undefined) { |
| 940 | 939 | const ty_index = obj.findImport(.function, symbol.index).kind.function; |
| 941 | 940 | return obj.func_types[ty_index]; |
| ... | ... | @@ -945,55 +944,55 @@ fn getFunctionSignature(self: *const Wasm, loc: SymbolLoc) wasm.Type { |
| 945 | 944 | return obj.func_types[type_index]; |
| 946 | 945 | } |
| 947 | 946 | if (is_undefined) { |
| 948 | | const ty_index = self.imports.get(loc).?.kind.function; |
| 949 | | return self.func_types.items[ty_index]; |
| 947 | const ty_index = wasm.imports.get(loc).?.kind.function; |
| 948 | return wasm.func_types.items[ty_index]; |
| 950 | 949 | } |
| 951 | | return self.func_types.items[self.functions.get(.{ .file = loc.file, .index = loc.index }).?.type_index]; |
| 950 | return wasm.func_types.items[wasm.functions.get(.{ .file = loc.file, .index = loc.index }).?.type_index]; |
| 952 | 951 | } |
| 953 | 952 | |
| 954 | 953 | /// Lowers a constant typed value to a local symbol and atom. |
| 955 | 954 | /// Returns the symbol index of the local |
| 956 | 955 | /// The given `decl` is the parent decl whom owns the constant. |
| 957 | | pub fn lowerUnnamedConst(self: *Wasm, tv: TypedValue, decl_index: Module.Decl.Index) !u32 { |
| 956 | pub fn lowerUnnamedConst(wasm: *Wasm, tv: TypedValue, decl_index: Module.Decl.Index) !u32 { |
| 958 | 957 | assert(tv.ty.zigTypeTag() != .Fn); // cannot create local symbols for functions |
| 959 | 958 | |
| 960 | | const mod = self.base.options.module.?; |
| 959 | const mod = wasm.base.options.module.?; |
| 961 | 960 | const decl = mod.declPtr(decl_index); |
| 962 | 961 | |
| 963 | 962 | // Create and initialize a new local symbol and atom |
| 964 | 963 | const local_index = decl.link.wasm.locals.items.len; |
| 965 | 964 | const fqdn = try decl.getFullyQualifiedName(mod); |
| 966 | | defer self.base.allocator.free(fqdn); |
| 967 | | const name = try std.fmt.allocPrintZ(self.base.allocator, "__unnamed_{s}_{d}", .{ fqdn, local_index }); |
| 968 | | defer self.base.allocator.free(name); |
| 965 | defer wasm.base.allocator.free(fqdn); |
| 966 | const name = try std.fmt.allocPrintZ(wasm.base.allocator, "__unnamed_{s}_{d}", .{ fqdn, local_index }); |
| 967 | defer wasm.base.allocator.free(name); |
| 969 | 968 | var symbol: Symbol = .{ |
| 970 | | .name = try self.string_table.put(self.base.allocator, name), |
| 969 | .name = try wasm.string_table.put(wasm.base.allocator, name), |
| 971 | 970 | .flags = 0, |
| 972 | 971 | .tag = .data, |
| 973 | 972 | .index = undefined, |
| 974 | 973 | }; |
| 975 | 974 | symbol.setFlag(.WASM_SYM_BINDING_LOCAL); |
| 976 | 975 | |
| 977 | | const atom = try decl.link.wasm.locals.addOne(self.base.allocator); |
| 976 | const atom = try decl.link.wasm.locals.addOne(wasm.base.allocator); |
| 978 | 977 | atom.* = Atom.empty; |
| 979 | | atom.alignment = tv.ty.abiAlignment(self.base.options.target); |
| 980 | | try self.symbols.ensureUnusedCapacity(self.base.allocator, 1); |
| 978 | atom.alignment = tv.ty.abiAlignment(wasm.base.options.target); |
| 979 | try wasm.symbols.ensureUnusedCapacity(wasm.base.allocator, 1); |
| 981 | 980 | |
| 982 | | if (self.symbols_free_list.popOrNull()) |index| { |
| 981 | if (wasm.symbols_free_list.popOrNull()) |index| { |
| 983 | 982 | atom.sym_index = index; |
| 984 | | self.symbols.items[index] = symbol; |
| 983 | wasm.symbols.items[index] = symbol; |
| 985 | 984 | } else { |
| 986 | | atom.sym_index = @intCast(u32, self.symbols.items.len); |
| 987 | | self.symbols.appendAssumeCapacity(symbol); |
| 985 | atom.sym_index = @intCast(u32, wasm.symbols.items.len); |
| 986 | wasm.symbols.appendAssumeCapacity(symbol); |
| 988 | 987 | } |
| 989 | | try self.resolved_symbols.putNoClobber(self.base.allocator, atom.symbolLoc(), {}); |
| 990 | | try self.symbol_atom.putNoClobber(self.base.allocator, atom.symbolLoc(), atom); |
| 988 | try wasm.resolved_symbols.putNoClobber(wasm.base.allocator, atom.symbolLoc(), {}); |
| 989 | try wasm.symbol_atom.putNoClobber(wasm.base.allocator, atom.symbolLoc(), atom); |
| 991 | 990 | |
| 992 | | var value_bytes = std.ArrayList(u8).init(self.base.allocator); |
| 991 | var value_bytes = std.ArrayList(u8).init(wasm.base.allocator); |
| 993 | 992 | defer value_bytes.deinit(); |
| 994 | 993 | |
| 995 | 994 | const result = try codegen.generateSymbol( |
| 996 | | &self.base, |
| 995 | &wasm.base, |
| 997 | 996 | decl.srcLoc(), |
| 998 | 997 | tv, |
| 999 | 998 | &value_bytes, |
| ... | ... | @@ -1014,7 +1013,7 @@ pub fn lowerUnnamedConst(self: *Wasm, tv: TypedValue, decl_index: Module.Decl.In |
| 1014 | 1013 | }; |
| 1015 | 1014 | |
| 1016 | 1015 | atom.size = @intCast(u32, code.len); |
| 1017 | | try atom.code.appendSlice(self.base.allocator, code); |
| 1016 | try atom.code.appendSlice(wasm.base.allocator, code); |
| 1018 | 1017 | return atom.sym_index; |
| 1019 | 1018 | } |
| 1020 | 1019 | |
| ... | ... | @@ -1022,9 +1021,9 @@ pub fn lowerUnnamedConst(self: *Wasm, tv: TypedValue, decl_index: Module.Decl.In |
| 1022 | 1021 | /// such as an exported or imported symbol. |
| 1023 | 1022 | /// If the symbol does not yet exist, creates a new one symbol instead |
| 1024 | 1023 | /// and then returns the index to it. |
| 1025 | | pub fn getGlobalSymbol(self: *Wasm, name: []const u8) !u32 { |
| 1026 | | const name_index = try self.string_table.put(self.base.allocator, name); |
| 1027 | | const gop = try self.globals.getOrPut(self.base.allocator, name_index); |
| 1024 | pub fn getGlobalSymbol(wasm: *Wasm, name: []const u8) !u32 { |
| 1025 | const name_index = try wasm.string_table.put(wasm.base.allocator, name); |
| 1026 | const gop = try wasm.globals.getOrPut(wasm.base.allocator, name_index); |
| 1028 | 1027 | if (gop.found_existing) { |
| 1029 | 1028 | return gop.value_ptr.*.index; |
| 1030 | 1029 | } |
| ... | ... | @@ -1038,46 +1037,46 @@ pub fn getGlobalSymbol(self: *Wasm, name: []const u8) !u32 { |
| 1038 | 1037 | symbol.setGlobal(true); |
| 1039 | 1038 | symbol.setUndefined(true); |
| 1040 | 1039 | |
| 1041 | | const sym_index = if (self.symbols_free_list.popOrNull()) |index| index else blk: { |
| 1042 | | var index = @intCast(u32, self.symbols.items.len); |
| 1043 | | try self.symbols.ensureUnusedCapacity(self.base.allocator, 1); |
| 1044 | | self.symbols.items.len += 1; |
| 1040 | const sym_index = if (wasm.symbols_free_list.popOrNull()) |index| index else blk: { |
| 1041 | var index = @intCast(u32, wasm.symbols.items.len); |
| 1042 | try wasm.symbols.ensureUnusedCapacity(wasm.base.allocator, 1); |
| 1043 | wasm.symbols.items.len += 1; |
| 1045 | 1044 | break :blk index; |
| 1046 | 1045 | }; |
| 1047 | | self.symbols.items[sym_index] = symbol; |
| 1046 | wasm.symbols.items[sym_index] = symbol; |
| 1048 | 1047 | gop.value_ptr.* = .{ .index = sym_index, .file = null }; |
| 1049 | | try self.resolved_symbols.put(self.base.allocator, gop.value_ptr.*, {}); |
| 1050 | | try self.undefs.putNoClobber(self.base.allocator, name, gop.value_ptr.*); |
| 1048 | try wasm.resolved_symbols.put(wasm.base.allocator, gop.value_ptr.*, {}); |
| 1049 | try wasm.undefs.putNoClobber(wasm.base.allocator, name, gop.value_ptr.*); |
| 1051 | 1050 | return sym_index; |
| 1052 | 1051 | } |
| 1053 | 1052 | |
| 1054 | 1053 | /// For a given decl, find the given symbol index's atom, and create a relocation for the type. |
| 1055 | 1054 | /// Returns the given pointer address |
| 1056 | 1055 | pub fn getDeclVAddr( |
| 1057 | | self: *Wasm, |
| 1056 | wasm: *Wasm, |
| 1058 | 1057 | decl_index: Module.Decl.Index, |
| 1059 | 1058 | reloc_info: link.File.RelocInfo, |
| 1060 | 1059 | ) !u64 { |
| 1061 | | const mod = self.base.options.module.?; |
| 1060 | const mod = wasm.base.options.module.?; |
| 1062 | 1061 | const decl = mod.declPtr(decl_index); |
| 1063 | 1062 | const target_symbol_index = decl.link.wasm.sym_index; |
| 1064 | 1063 | assert(target_symbol_index != 0); |
| 1065 | 1064 | assert(reloc_info.parent_atom_index != 0); |
| 1066 | | const atom = self.symbol_atom.get(.{ .file = null, .index = reloc_info.parent_atom_index }).?; |
| 1067 | | const is_wasm32 = self.base.options.target.cpu.arch == .wasm32; |
| 1065 | const atom = wasm.symbol_atom.get(.{ .file = null, .index = reloc_info.parent_atom_index }).?; |
| 1066 | const is_wasm32 = wasm.base.options.target.cpu.arch == .wasm32; |
| 1068 | 1067 | if (decl.ty.zigTypeTag() == .Fn) { |
| 1069 | 1068 | assert(reloc_info.addend == 0); // addend not allowed for function relocations |
| 1070 | 1069 | // We found a function pointer, so add it to our table, |
| 1071 | 1070 | // as function pointers are not allowed to be stored inside the data section. |
| 1072 | 1071 | // They are instead stored in a function table which are called by index. |
| 1073 | | try self.addTableFunction(target_symbol_index); |
| 1074 | | try atom.relocs.append(self.base.allocator, .{ |
| 1072 | try wasm.addTableFunction(target_symbol_index); |
| 1073 | try atom.relocs.append(wasm.base.allocator, .{ |
| 1075 | 1074 | .index = target_symbol_index, |
| 1076 | 1075 | .offset = @intCast(u32, reloc_info.offset), |
| 1077 | 1076 | .relocation_type = if (is_wasm32) .R_WASM_TABLE_INDEX_I32 else .R_WASM_TABLE_INDEX_I64, |
| 1078 | 1077 | }); |
| 1079 | 1078 | } else { |
| 1080 | | try atom.relocs.append(self.base.allocator, .{ |
| 1079 | try atom.relocs.append(wasm.base.allocator, .{ |
| 1081 | 1080 | .index = target_symbol_index, |
| 1082 | 1081 | .offset = @intCast(u32, reloc_info.offset), |
| 1083 | 1082 | .relocation_type = if (is_wasm32) .R_WASM_MEMORY_ADDR_I32 else .R_WASM_MEMORY_ADDR_I64, |
| ... | ... | @@ -1091,22 +1090,22 @@ pub fn getDeclVAddr( |
| 1091 | 1090 | return target_symbol_index; |
| 1092 | 1091 | } |
| 1093 | 1092 | |
| 1094 | | pub fn deleteExport(self: *Wasm, exp: Export) void { |
| 1095 | | if (self.llvm_object) |_| return; |
| 1093 | pub fn deleteExport(wasm: *Wasm, exp: Export) void { |
| 1094 | if (wasm.llvm_object) |_| return; |
| 1096 | 1095 | const sym_index = exp.sym_index orelse return; |
| 1097 | 1096 | const loc: SymbolLoc = .{ .file = null, .index = sym_index }; |
| 1098 | | const symbol = loc.getSymbol(self); |
| 1099 | | const symbol_name = self.string_table.get(symbol.name); |
| 1097 | const symbol = loc.getSymbol(wasm); |
| 1098 | const symbol_name = wasm.string_table.get(symbol.name); |
| 1100 | 1099 | log.debug("Deleting export for decl '{s}'", .{symbol_name}); |
| 1101 | | if (self.export_names.fetchRemove(loc)) |kv| { |
| 1102 | | assert(self.globals.remove(kv.value)); |
| 1100 | if (wasm.export_names.fetchRemove(loc)) |kv| { |
| 1101 | assert(wasm.globals.remove(kv.value)); |
| 1103 | 1102 | } else { |
| 1104 | | assert(self.globals.remove(symbol.name)); |
| 1103 | assert(wasm.globals.remove(symbol.name)); |
| 1105 | 1104 | } |
| 1106 | 1105 | } |
| 1107 | 1106 | |
| 1108 | 1107 | pub fn updateDeclExports( |
| 1109 | | self: *Wasm, |
| 1108 | wasm: *Wasm, |
| 1110 | 1109 | mod: *Module, |
| 1111 | 1110 | decl_index: Module.Decl.Index, |
| 1112 | 1111 | exports: []const *Module.Export, |
| ... | ... | @@ -1115,7 +1114,7 @@ pub fn updateDeclExports( |
| 1115 | 1114 | @panic("Attempted to compile for object format that was disabled by build configuration"); |
| 1116 | 1115 | } |
| 1117 | 1116 | if (build_options.have_llvm) { |
| 1118 | | if (self.llvm_object) |llvm_object| return llvm_object.updateDeclExports(mod, decl_index, exports); |
| 1117 | if (wasm.llvm_object) |llvm_object| return llvm_object.updateDeclExports(mod, decl_index, exports); |
| 1119 | 1118 | } |
| 1120 | 1119 | |
| 1121 | 1120 | const decl = mod.declPtr(decl_index); |
| ... | ... | @@ -1131,10 +1130,10 @@ pub fn updateDeclExports( |
| 1131 | 1130 | continue; |
| 1132 | 1131 | } |
| 1133 | 1132 | |
| 1134 | | const export_name = try self.string_table.put(self.base.allocator, exp.options.name); |
| 1135 | | if (self.globals.getPtr(export_name)) |existing_loc| { |
| 1133 | const export_name = try wasm.string_table.put(wasm.base.allocator, exp.options.name); |
| 1134 | if (wasm.globals.getPtr(export_name)) |existing_loc| { |
| 1136 | 1135 | if (existing_loc.index == decl.link.wasm.sym_index) continue; |
| 1137 | | const existing_sym: Symbol = existing_loc.getSymbol(self).*; |
| 1136 | const existing_sym: Symbol = existing_loc.getSymbol(wasm).*; |
| 1138 | 1137 | |
| 1139 | 1138 | const exp_is_weak = exp.options.linkage == .Internal or exp.options.linkage == .Weak; |
| 1140 | 1139 | // When both the to-bo-exported symbol and the already existing symbol |
| ... | ... | @@ -1148,7 +1147,7 @@ pub fn updateDeclExports( |
| 1148 | 1147 | \\ first definition in '{s}' |
| 1149 | 1148 | \\ next definition in '{s}' |
| 1150 | 1149 | , |
| 1151 | | .{ exp.options.name, self.name, self.name }, |
| 1150 | .{ exp.options.name, wasm.name, wasm.name }, |
| 1152 | 1151 | )); |
| 1153 | 1152 | continue; |
| 1154 | 1153 | } else if (exp_is_weak) { |
| ... | ... | @@ -1163,7 +1162,7 @@ pub fn updateDeclExports( |
| 1163 | 1162 | const exported_decl = mod.declPtr(exp.exported_decl); |
| 1164 | 1163 | const sym_index = exported_decl.link.wasm.sym_index; |
| 1165 | 1164 | const sym_loc = exported_decl.link.wasm.symbolLoc(); |
| 1166 | | const symbol = sym_loc.getSymbol(self); |
| 1165 | const symbol = sym_loc.getSymbol(wasm); |
| 1167 | 1166 | switch (exp.options.linkage) { |
| 1168 | 1167 | .Internal => { |
| 1169 | 1168 | symbol.setFlag(.WASM_SYM_VISIBILITY_HIDDEN); |
| ... | ... | @@ -1183,68 +1182,68 @@ pub fn updateDeclExports( |
| 1183 | 1182 | }, |
| 1184 | 1183 | } |
| 1185 | 1184 | // Ensure the symbol will be exported using the given name |
| 1186 | | if (!mem.eql(u8, exp.options.name, sym_loc.getName(self))) { |
| 1187 | | try self.export_names.put(self.base.allocator, sym_loc, export_name); |
| 1185 | if (!mem.eql(u8, exp.options.name, sym_loc.getName(wasm))) { |
| 1186 | try wasm.export_names.put(wasm.base.allocator, sym_loc, export_name); |
| 1188 | 1187 | } |
| 1189 | 1188 | |
| 1190 | 1189 | symbol.setGlobal(true); |
| 1191 | 1190 | symbol.setUndefined(false); |
| 1192 | | try self.globals.put( |
| 1193 | | self.base.allocator, |
| 1191 | try wasm.globals.put( |
| 1192 | wasm.base.allocator, |
| 1194 | 1193 | export_name, |
| 1195 | 1194 | sym_loc, |
| 1196 | 1195 | ); |
| 1197 | 1196 | |
| 1198 | 1197 | // if the symbol was previously undefined, remove it as an import |
| 1199 | | _ = self.imports.remove(sym_loc); |
| 1200 | | _ = self.undefs.swapRemove(exp.options.name); |
| 1198 | _ = wasm.imports.remove(sym_loc); |
| 1199 | _ = wasm.undefs.swapRemove(exp.options.name); |
| 1201 | 1200 | exp.link.wasm.sym_index = sym_index; |
| 1202 | 1201 | } |
| 1203 | 1202 | } |
| 1204 | 1203 | |
| 1205 | | pub fn freeDecl(self: *Wasm, decl_index: Module.Decl.Index) void { |
| 1204 | pub fn freeDecl(wasm: *Wasm, decl_index: Module.Decl.Index) void { |
| 1206 | 1205 | if (build_options.have_llvm) { |
| 1207 | | if (self.llvm_object) |llvm_object| return llvm_object.freeDecl(decl_index); |
| 1206 | if (wasm.llvm_object) |llvm_object| return llvm_object.freeDecl(decl_index); |
| 1208 | 1207 | } |
| 1209 | | const mod = self.base.options.module.?; |
| 1208 | const mod = wasm.base.options.module.?; |
| 1210 | 1209 | const decl = mod.declPtr(decl_index); |
| 1211 | 1210 | const atom = &decl.link.wasm; |
| 1212 | | self.symbols_free_list.append(self.base.allocator, atom.sym_index) catch {}; |
| 1213 | | _ = self.decls.remove(decl_index); |
| 1214 | | self.symbols.items[atom.sym_index].tag = .dead; |
| 1211 | wasm.symbols_free_list.append(wasm.base.allocator, atom.sym_index) catch {}; |
| 1212 | _ = wasm.decls.remove(decl_index); |
| 1213 | wasm.symbols.items[atom.sym_index].tag = .dead; |
| 1215 | 1214 | for (atom.locals.items) |local_atom| { |
| 1216 | | const local_symbol = &self.symbols.items[local_atom.sym_index]; |
| 1215 | const local_symbol = &wasm.symbols.items[local_atom.sym_index]; |
| 1217 | 1216 | local_symbol.tag = .dead; // also for any local symbol |
| 1218 | | self.symbols_free_list.append(self.base.allocator, local_atom.sym_index) catch {}; |
| 1219 | | assert(self.resolved_symbols.swapRemove(local_atom.symbolLoc())); |
| 1220 | | assert(self.symbol_atom.remove(local_atom.symbolLoc())); |
| 1217 | wasm.symbols_free_list.append(wasm.base.allocator, local_atom.sym_index) catch {}; |
| 1218 | assert(wasm.resolved_symbols.swapRemove(local_atom.symbolLoc())); |
| 1219 | assert(wasm.symbol_atom.remove(local_atom.symbolLoc())); |
| 1221 | 1220 | } |
| 1222 | 1221 | |
| 1223 | 1222 | if (decl.isExtern()) { |
| 1224 | | _ = self.imports.remove(atom.symbolLoc()); |
| 1223 | _ = wasm.imports.remove(atom.symbolLoc()); |
| 1225 | 1224 | } |
| 1226 | | _ = self.resolved_symbols.swapRemove(atom.symbolLoc()); |
| 1227 | | _ = self.symbol_atom.remove(atom.symbolLoc()); |
| 1225 | _ = wasm.resolved_symbols.swapRemove(atom.symbolLoc()); |
| 1226 | _ = wasm.symbol_atom.remove(atom.symbolLoc()); |
| 1228 | 1227 | |
| 1229 | | if (self.dwarf) |*dwarf| { |
| 1228 | if (wasm.dwarf) |*dwarf| { |
| 1230 | 1229 | dwarf.freeDecl(decl); |
| 1231 | 1230 | dwarf.freeAtom(&atom.dbg_info_atom); |
| 1232 | 1231 | } |
| 1233 | 1232 | |
| 1234 | | atom.deinit(self.base.allocator); |
| 1233 | atom.deinit(wasm.base.allocator); |
| 1235 | 1234 | } |
| 1236 | 1235 | |
| 1237 | 1236 | /// Appends a new entry to the indirect function table |
| 1238 | | pub fn addTableFunction(self: *Wasm, symbol_index: u32) !void { |
| 1239 | | const index = @intCast(u32, self.function_table.count()); |
| 1240 | | try self.function_table.put(self.base.allocator, .{ .file = null, .index = symbol_index }, index); |
| 1237 | pub fn addTableFunction(wasm: *Wasm, symbol_index: u32) !void { |
| 1238 | const index = @intCast(u32, wasm.function_table.count()); |
| 1239 | try wasm.function_table.put(wasm.base.allocator, .{ .file = null, .index = symbol_index }, index); |
| 1241 | 1240 | } |
| 1242 | 1241 | |
| 1243 | 1242 | /// Assigns indexes to all indirect functions. |
| 1244 | 1243 | /// Starts at offset 1, where the value `0` represents an unresolved function pointer |
| 1245 | 1244 | /// or null-pointer |
| 1246 | | fn mapFunctionTable(self: *Wasm) void { |
| 1247 | | var it = self.function_table.valueIterator(); |
| 1245 | fn mapFunctionTable(wasm: *Wasm) void { |
| 1246 | var it = wasm.function_table.valueIterator(); |
| 1248 | 1247 | var index: u32 = 1; |
| 1249 | 1248 | while (it.next()) |value_ptr| : (index += 1) { |
| 1250 | 1249 | value_ptr.* = index; |
| ... | ... | @@ -1255,7 +1254,7 @@ fn mapFunctionTable(self: *Wasm) void { |
| 1255 | 1254 | /// When `type_index` is non-null, we assume an external function. |
| 1256 | 1255 | /// In all other cases, a data-symbol will be created instead. |
| 1257 | 1256 | pub fn addOrUpdateImport( |
| 1258 | | self: *Wasm, |
| 1257 | wasm: *Wasm, |
| 1259 | 1258 | /// Name of the import |
| 1260 | 1259 | name: []const u8, |
| 1261 | 1260 | /// Symbol index that is external |
| ... | ... | @@ -1268,28 +1267,28 @@ pub fn addOrUpdateImport( |
| 1268 | 1267 | type_index: ?u32, |
| 1269 | 1268 | ) !void { |
| 1270 | 1269 | assert(symbol_index != 0); |
| 1271 | | // For the import name itself, we use the decl's name, rather than the fully qualified name |
| 1272 | | const decl_name_index = try self.string_table.put(self.base.allocator, name); |
| 1273 | | const symbol: *Symbol = &self.symbols.items[symbol_index]; |
| 1270 | // For the import name itwasm, we use the decl's name, rather than the fully qualified name |
| 1271 | const decl_name_index = try wasm.string_table.put(wasm.base.allocator, name); |
| 1272 | const symbol: *Symbol = &wasm.symbols.items[symbol_index]; |
| 1274 | 1273 | symbol.setUndefined(true); |
| 1275 | 1274 | symbol.setGlobal(true); |
| 1276 | 1275 | symbol.name = decl_name_index; |
| 1277 | | const global_gop = try self.globals.getOrPut(self.base.allocator, decl_name_index); |
| 1276 | const global_gop = try wasm.globals.getOrPut(wasm.base.allocator, decl_name_index); |
| 1278 | 1277 | if (!global_gop.found_existing) { |
| 1279 | 1278 | const loc: SymbolLoc = .{ .file = null, .index = symbol_index }; |
| 1280 | 1279 | global_gop.value_ptr.* = loc; |
| 1281 | | try self.resolved_symbols.put(self.base.allocator, loc, {}); |
| 1282 | | try self.undefs.putNoClobber(self.base.allocator, name, loc); |
| 1280 | try wasm.resolved_symbols.put(wasm.base.allocator, loc, {}); |
| 1281 | try wasm.undefs.putNoClobber(wasm.base.allocator, name, loc); |
| 1283 | 1282 | } |
| 1284 | 1283 | |
| 1285 | 1284 | if (type_index) |ty_index| { |
| 1286 | | const gop = try self.imports.getOrPut(self.base.allocator, .{ .index = symbol_index, .file = null }); |
| 1285 | const gop = try wasm.imports.getOrPut(wasm.base.allocator, .{ .index = symbol_index, .file = null }); |
| 1287 | 1286 | const module_name = if (lib_name) |l_name| blk: { |
| 1288 | 1287 | break :blk mem.sliceTo(l_name, 0); |
| 1289 | | } else self.host_name; |
| 1288 | } else wasm.host_name; |
| 1290 | 1289 | if (!gop.found_existing) { |
| 1291 | 1290 | gop.value_ptr.* = .{ |
| 1292 | | .module_name = try self.string_table.put(self.base.allocator, module_name), |
| 1291 | .module_name = try wasm.string_table.put(wasm.base.allocator, module_name), |
| 1293 | 1292 | .name = decl_name_index, |
| 1294 | 1293 | .kind = .{ .function = ty_index }, |
| 1295 | 1294 | }; |
| ... | ... | @@ -1326,36 +1325,36 @@ const Kind = union(enum) { |
| 1326 | 1325 | }; |
| 1327 | 1326 | |
| 1328 | 1327 | /// Parses an Atom and inserts its metadata into the corresponding sections. |
| 1329 | | fn parseAtom(self: *Wasm, atom: *Atom, kind: Kind) !void { |
| 1330 | | const symbol = (SymbolLoc{ .file = null, .index = atom.sym_index }).getSymbol(self); |
| 1328 | fn parseAtom(wasm: *Wasm, atom: *Atom, kind: Kind) !void { |
| 1329 | const symbol = (SymbolLoc{ .file = null, .index = atom.sym_index }).getSymbol(wasm); |
| 1331 | 1330 | const final_index: u32 = switch (kind) { |
| 1332 | 1331 | .function => |fn_data| result: { |
| 1333 | | const index = @intCast(u32, self.functions.count() + self.imported_functions_count); |
| 1334 | | try self.functions.putNoClobber( |
| 1335 | | self.base.allocator, |
| 1332 | const index = @intCast(u32, wasm.functions.count() + wasm.imported_functions_count); |
| 1333 | try wasm.functions.putNoClobber( |
| 1334 | wasm.base.allocator, |
| 1336 | 1335 | .{ .file = null, .index = index }, |
| 1337 | 1336 | .{ .type_index = fn_data.type_index }, |
| 1338 | 1337 | ); |
| 1339 | 1338 | symbol.tag = .function; |
| 1340 | 1339 | symbol.index = index; |
| 1341 | 1340 | |
| 1342 | | if (self.code_section_index == null) { |
| 1343 | | self.code_section_index = @intCast(u32, self.segments.items.len); |
| 1344 | | try self.segments.append(self.base.allocator, .{ |
| 1341 | if (wasm.code_section_index == null) { |
| 1342 | wasm.code_section_index = @intCast(u32, wasm.segments.items.len); |
| 1343 | try wasm.segments.append(wasm.base.allocator, .{ |
| 1345 | 1344 | .alignment = atom.alignment, |
| 1346 | 1345 | .size = atom.size, |
| 1347 | 1346 | .offset = 0, |
| 1348 | 1347 | }); |
| 1349 | 1348 | } |
| 1350 | 1349 | |
| 1351 | | break :result self.code_section_index.?; |
| 1350 | break :result wasm.code_section_index.?; |
| 1352 | 1351 | }, |
| 1353 | 1352 | .data => result: { |
| 1354 | | const segment_name = try std.mem.concat(self.base.allocator, u8, &.{ |
| 1353 | const segment_name = try std.mem.concat(wasm.base.allocator, u8, &.{ |
| 1355 | 1354 | kind.segmentName(), |
| 1356 | | self.string_table.get(symbol.name), |
| 1355 | wasm.string_table.get(symbol.name), |
| 1357 | 1356 | }); |
| 1358 | | errdefer self.base.allocator.free(segment_name); |
| 1357 | errdefer wasm.base.allocator.free(segment_name); |
| 1359 | 1358 | const segment_info: types.Segment = .{ |
| 1360 | 1359 | .name = segment_name, |
| 1361 | 1360 | .alignment = atom.alignment, |
| ... | ... | @@ -1367,59 +1366,59 @@ fn parseAtom(self: *Wasm, atom: *Atom, kind: Kind) !void { |
| 1367 | 1366 | // we set the entire region of it to zeroes. |
| 1368 | 1367 | // We do not have to do this when exporting the memory (the default) because the runtime |
| 1369 | 1368 | // will do it for us, and we do not emit the bss segment at all. |
| 1370 | | if ((self.base.options.output_mode == .Obj or self.base.options.import_memory) and kind.data == .uninitialized) { |
| 1369 | if ((wasm.base.options.output_mode == .Obj or wasm.base.options.import_memory) and kind.data == .uninitialized) { |
| 1371 | 1370 | std.mem.set(u8, atom.code.items, 0); |
| 1372 | 1371 | } |
| 1373 | 1372 | |
| 1374 | | const should_merge = self.base.options.output_mode != .Obj; |
| 1375 | | const gop = try self.data_segments.getOrPut(self.base.allocator, segment_info.outputName(should_merge)); |
| 1373 | const should_merge = wasm.base.options.output_mode != .Obj; |
| 1374 | const gop = try wasm.data_segments.getOrPut(wasm.base.allocator, segment_info.outputName(should_merge)); |
| 1376 | 1375 | if (gop.found_existing) { |
| 1377 | 1376 | const index = gop.value_ptr.*; |
| 1378 | | self.segments.items[index].size += atom.size; |
| 1377 | wasm.segments.items[index].size += atom.size; |
| 1379 | 1378 | |
| 1380 | | symbol.index = @intCast(u32, self.segment_info.getIndex(index).?); |
| 1379 | symbol.index = @intCast(u32, wasm.segment_info.getIndex(index).?); |
| 1381 | 1380 | // segment info already exists, so free its memory |
| 1382 | | self.base.allocator.free(segment_name); |
| 1381 | wasm.base.allocator.free(segment_name); |
| 1383 | 1382 | break :result index; |
| 1384 | 1383 | } else { |
| 1385 | | const index = @intCast(u32, self.segments.items.len); |
| 1386 | | try self.segments.append(self.base.allocator, .{ |
| 1384 | const index = @intCast(u32, wasm.segments.items.len); |
| 1385 | try wasm.segments.append(wasm.base.allocator, .{ |
| 1387 | 1386 | .alignment = atom.alignment, |
| 1388 | 1387 | .size = 0, |
| 1389 | 1388 | .offset = 0, |
| 1390 | 1389 | }); |
| 1391 | 1390 | gop.value_ptr.* = index; |
| 1392 | 1391 | |
| 1393 | | const info_index = @intCast(u32, self.segment_info.count()); |
| 1394 | | try self.segment_info.put(self.base.allocator, index, segment_info); |
| 1392 | const info_index = @intCast(u32, wasm.segment_info.count()); |
| 1393 | try wasm.segment_info.put(wasm.base.allocator, index, segment_info); |
| 1395 | 1394 | symbol.index = info_index; |
| 1396 | 1395 | break :result index; |
| 1397 | 1396 | } |
| 1398 | 1397 | }, |
| 1399 | 1398 | }; |
| 1400 | 1399 | |
| 1401 | | const segment: *Segment = &self.segments.items[final_index]; |
| 1400 | const segment: *Segment = &wasm.segments.items[final_index]; |
| 1402 | 1401 | segment.alignment = std.math.max(segment.alignment, atom.alignment); |
| 1403 | 1402 | |
| 1404 | | try self.appendAtomAtIndex(final_index, atom); |
| 1403 | try wasm.appendAtomAtIndex(final_index, atom); |
| 1405 | 1404 | } |
| 1406 | 1405 | |
| 1407 | 1406 | /// From a given index, append the given `Atom` at the back of the linked list. |
| 1408 | 1407 | /// Simply inserts it into the map of atoms when it doesn't exist yet. |
| 1409 | | pub fn appendAtomAtIndex(self: *Wasm, index: u32, atom: *Atom) !void { |
| 1410 | | if (self.atoms.getPtr(index)) |last| { |
| 1408 | pub fn appendAtomAtIndex(wasm: *Wasm, index: u32, atom: *Atom) !void { |
| 1409 | if (wasm.atoms.getPtr(index)) |last| { |
| 1411 | 1410 | last.*.next = atom; |
| 1412 | 1411 | atom.prev = last.*; |
| 1413 | 1412 | last.* = atom; |
| 1414 | 1413 | } else { |
| 1415 | | try self.atoms.putNoClobber(self.base.allocator, index, atom); |
| 1414 | try wasm.atoms.putNoClobber(wasm.base.allocator, index, atom); |
| 1416 | 1415 | } |
| 1417 | 1416 | } |
| 1418 | 1417 | |
| 1419 | 1418 | /// Allocates debug atoms into their respective debug sections |
| 1420 | 1419 | /// to merge them with maybe-existing debug atoms from object files. |
| 1421 | | fn allocateDebugAtoms(self: *Wasm) !void { |
| 1422 | | if (self.dwarf == null) return; |
| 1420 | fn allocateDebugAtoms(wasm: *Wasm) !void { |
| 1421 | if (wasm.dwarf == null) return; |
| 1423 | 1422 | |
| 1424 | 1423 | const allocAtom = struct { |
| 1425 | 1424 | fn f(bin: *Wasm, maybe_index: *?u32, atom: *Atom) !void { |
| ... | ... | @@ -1435,24 +1434,24 @@ fn allocateDebugAtoms(self: *Wasm) !void { |
| 1435 | 1434 | } |
| 1436 | 1435 | }.f; |
| 1437 | 1436 | |
| 1438 | | try allocAtom(self, &self.debug_info_index, self.debug_info_atom.?); |
| 1439 | | try allocAtom(self, &self.debug_line_index, self.debug_line_atom.?); |
| 1440 | | try allocAtom(self, &self.debug_loc_index, self.debug_loc_atom.?); |
| 1441 | | try allocAtom(self, &self.debug_str_index, self.debug_str_atom.?); |
| 1442 | | try allocAtom(self, &self.debug_ranges_index, self.debug_ranges_atom.?); |
| 1443 | | try allocAtom(self, &self.debug_abbrev_index, self.debug_abbrev_atom.?); |
| 1444 | | try allocAtom(self, &self.debug_pubnames_index, self.debug_pubnames_atom.?); |
| 1445 | | try allocAtom(self, &self.debug_pubtypes_index, self.debug_pubtypes_atom.?); |
| 1437 | try allocAtom(wasm, &wasm.debug_info_index, wasm.debug_info_atom.?); |
| 1438 | try allocAtom(wasm, &wasm.debug_line_index, wasm.debug_line_atom.?); |
| 1439 | try allocAtom(wasm, &wasm.debug_loc_index, wasm.debug_loc_atom.?); |
| 1440 | try allocAtom(wasm, &wasm.debug_str_index, wasm.debug_str_atom.?); |
| 1441 | try allocAtom(wasm, &wasm.debug_ranges_index, wasm.debug_ranges_atom.?); |
| 1442 | try allocAtom(wasm, &wasm.debug_abbrev_index, wasm.debug_abbrev_atom.?); |
| 1443 | try allocAtom(wasm, &wasm.debug_pubnames_index, wasm.debug_pubnames_atom.?); |
| 1444 | try allocAtom(wasm, &wasm.debug_pubtypes_index, wasm.debug_pubtypes_atom.?); |
| 1446 | 1445 | } |
| 1447 | 1446 | |
| 1448 | | fn allocateAtoms(self: *Wasm) !void { |
| 1447 | fn allocateAtoms(wasm: *Wasm) !void { |
| 1449 | 1448 | // first sort the data segments |
| 1450 | | try sortDataSegments(self); |
| 1451 | | try allocateDebugAtoms(self); |
| 1449 | try sortDataSegments(wasm); |
| 1450 | try allocateDebugAtoms(wasm); |
| 1452 | 1451 | |
| 1453 | | var it = self.atoms.iterator(); |
| 1452 | var it = wasm.atoms.iterator(); |
| 1454 | 1453 | while (it.next()) |entry| { |
| 1455 | | const segment = &self.segments.items[entry.key_ptr.*]; |
| 1454 | const segment = &wasm.segments.items[entry.key_ptr.*]; |
| 1456 | 1455 | var atom: *Atom = entry.value_ptr.*.getFirst(); |
| 1457 | 1456 | var offset: u32 = 0; |
| 1458 | 1457 | while (true) { |
| ... | ... | @@ -1460,26 +1459,26 @@ fn allocateAtoms(self: *Wasm) !void { |
| 1460 | 1459 | atom.offset = offset; |
| 1461 | 1460 | const symbol_loc = atom.symbolLoc(); |
| 1462 | 1461 | log.debug("Atom '{s}' allocated from 0x{x:0>8} to 0x{x:0>8} size={d}", .{ |
| 1463 | | symbol_loc.getName(self), |
| 1462 | symbol_loc.getName(wasm), |
| 1464 | 1463 | offset, |
| 1465 | 1464 | offset + atom.size, |
| 1466 | 1465 | atom.size, |
| 1467 | 1466 | }); |
| 1468 | 1467 | offset += atom.size; |
| 1469 | | try self.symbol_atom.put(self.base.allocator, atom.symbolLoc(), atom); // Update atom pointers |
| 1468 | try wasm.symbol_atom.put(wasm.base.allocator, atom.symbolLoc(), atom); // Update atom pointers |
| 1470 | 1469 | atom = atom.next orelse break; |
| 1471 | 1470 | } |
| 1472 | 1471 | segment.size = std.mem.alignForwardGeneric(u32, offset, segment.alignment); |
| 1473 | 1472 | } |
| 1474 | 1473 | } |
| 1475 | 1474 | |
| 1476 | | fn sortDataSegments(self: *Wasm) !void { |
| 1475 | fn sortDataSegments(wasm: *Wasm) !void { |
| 1477 | 1476 | var new_mapping: std.StringArrayHashMapUnmanaged(u32) = .{}; |
| 1478 | | try new_mapping.ensureUnusedCapacity(self.base.allocator, self.data_segments.count()); |
| 1479 | | errdefer new_mapping.deinit(self.base.allocator); |
| 1477 | try new_mapping.ensureUnusedCapacity(wasm.base.allocator, wasm.data_segments.count()); |
| 1478 | errdefer new_mapping.deinit(wasm.base.allocator); |
| 1480 | 1479 | |
| 1481 | | const keys = try self.base.allocator.dupe([]const u8, self.data_segments.keys()); |
| 1482 | | defer self.base.allocator.free(keys); |
| 1480 | const keys = try wasm.base.allocator.dupe([]const u8, wasm.data_segments.keys()); |
| 1481 | defer wasm.base.allocator.free(keys); |
| 1483 | 1482 | |
| 1484 | 1483 | const SortContext = struct { |
| 1485 | 1484 | fn sort(_: void, lhs: []const u8, rhs: []const u8) bool { |
| ... | ... | @@ -1496,63 +1495,63 @@ fn sortDataSegments(self: *Wasm) !void { |
| 1496 | 1495 | |
| 1497 | 1496 | std.sort.sort([]const u8, keys, {}, SortContext.sort); |
| 1498 | 1497 | for (keys) |key| { |
| 1499 | | const segment_index = self.data_segments.get(key).?; |
| 1498 | const segment_index = wasm.data_segments.get(key).?; |
| 1500 | 1499 | new_mapping.putAssumeCapacity(key, segment_index); |
| 1501 | 1500 | } |
| 1502 | | self.data_segments.deinit(self.base.allocator); |
| 1503 | | self.data_segments = new_mapping; |
| 1501 | wasm.data_segments.deinit(wasm.base.allocator); |
| 1502 | wasm.data_segments = new_mapping; |
| 1504 | 1503 | } |
| 1505 | 1504 | |
| 1506 | | fn setupImports(self: *Wasm) !void { |
| 1505 | fn setupImports(wasm: *Wasm) !void { |
| 1507 | 1506 | log.debug("Merging imports", .{}); |
| 1508 | | var discarded_it = self.discarded.keyIterator(); |
| 1507 | var discarded_it = wasm.discarded.keyIterator(); |
| 1509 | 1508 | while (discarded_it.next()) |discarded| { |
| 1510 | 1509 | if (discarded.file == null) { |
| 1511 | 1510 | // remove an import if it was resolved |
| 1512 | | if (self.imports.remove(discarded.*)) { |
| 1511 | if (wasm.imports.remove(discarded.*)) { |
| 1513 | 1512 | log.debug("Removed symbol '{s}' as an import", .{ |
| 1514 | | discarded.getName(self), |
| 1513 | discarded.getName(wasm), |
| 1515 | 1514 | }); |
| 1516 | 1515 | } |
| 1517 | 1516 | } |
| 1518 | 1517 | } |
| 1519 | 1518 | |
| 1520 | | for (self.resolved_symbols.keys()) |symbol_loc| { |
| 1519 | for (wasm.resolved_symbols.keys()) |symbol_loc| { |
| 1521 | 1520 | if (symbol_loc.file == null) { |
| 1522 | 1521 | // imports generated by Zig code are already in the `import` section |
| 1523 | 1522 | continue; |
| 1524 | 1523 | } |
| 1525 | 1524 | |
| 1526 | | const symbol = symbol_loc.getSymbol(self); |
| 1527 | | if (std.mem.eql(u8, symbol_loc.getName(self), "__indirect_function_table")) { |
| 1525 | const symbol = symbol_loc.getSymbol(wasm); |
| 1526 | if (std.mem.eql(u8, symbol_loc.getName(wasm), "__indirect_function_table")) { |
| 1528 | 1527 | continue; |
| 1529 | 1528 | } |
| 1530 | 1529 | if (!symbol.requiresImport()) { |
| 1531 | 1530 | continue; |
| 1532 | 1531 | } |
| 1533 | 1532 | |
| 1534 | | log.debug("Symbol '{s}' will be imported from the host", .{symbol_loc.getName(self)}); |
| 1535 | | const object = self.objects.items[symbol_loc.file.?]; |
| 1533 | log.debug("Symbol '{s}' will be imported from the host", .{symbol_loc.getName(wasm)}); |
| 1534 | const object = wasm.objects.items[symbol_loc.file.?]; |
| 1536 | 1535 | const import = object.findImport(symbol.tag.externalType(), symbol.index); |
| 1537 | 1536 | |
| 1538 | 1537 | // We copy the import to a new import to ensure the names contain references |
| 1539 | 1538 | // to the internal string table, rather than of the object file. |
| 1540 | 1539 | var new_imp: types.Import = .{ |
| 1541 | | .module_name = try self.string_table.put(self.base.allocator, object.string_table.get(import.module_name)), |
| 1542 | | .name = try self.string_table.put(self.base.allocator, object.string_table.get(import.name)), |
| 1540 | .module_name = try wasm.string_table.put(wasm.base.allocator, object.string_table.get(import.module_name)), |
| 1541 | .name = try wasm.string_table.put(wasm.base.allocator, object.string_table.get(import.name)), |
| 1543 | 1542 | .kind = import.kind, |
| 1544 | 1543 | }; |
| 1545 | 1544 | // TODO: De-duplicate imports when they contain the same names and type |
| 1546 | | try self.imports.putNoClobber(self.base.allocator, symbol_loc, new_imp); |
| 1545 | try wasm.imports.putNoClobber(wasm.base.allocator, symbol_loc, new_imp); |
| 1547 | 1546 | } |
| 1548 | 1547 | |
| 1549 | 1548 | // Assign all indexes of the imports to their representing symbols |
| 1550 | 1549 | var function_index: u32 = 0; |
| 1551 | 1550 | var global_index: u32 = 0; |
| 1552 | 1551 | var table_index: u32 = 0; |
| 1553 | | var it = self.imports.iterator(); |
| 1552 | var it = wasm.imports.iterator(); |
| 1554 | 1553 | while (it.next()) |entry| { |
| 1555 | | const symbol = entry.key_ptr.*.getSymbol(self); |
| 1554 | const symbol = entry.key_ptr.*.getSymbol(wasm); |
| 1556 | 1555 | const import: types.Import = entry.value_ptr.*; |
| 1557 | 1556 | switch (import.kind) { |
| 1558 | 1557 | .function => { |
| ... | ... | @@ -1570,9 +1569,9 @@ fn setupImports(self: *Wasm) !void { |
| 1570 | 1569 | else => unreachable, |
| 1571 | 1570 | } |
| 1572 | 1571 | } |
| 1573 | | self.imported_functions_count = function_index; |
| 1574 | | self.imported_globals_count = global_index; |
| 1575 | | self.imported_tables_count = table_index; |
| 1572 | wasm.imported_functions_count = function_index; |
| 1573 | wasm.imported_globals_count = global_index; |
| 1574 | wasm.imported_tables_count = table_index; |
| 1576 | 1575 | |
| 1577 | 1576 | log.debug("Merged ({d}) functions, ({d}) globals, and ({d}) tables into import section", .{ |
| 1578 | 1577 | function_index, |
| ... | ... | @@ -1583,26 +1582,26 @@ fn setupImports(self: *Wasm) !void { |
| 1583 | 1582 | |
| 1584 | 1583 | /// Takes the global, function and table section from each linked object file |
| 1585 | 1584 | /// and merges it into a single section for each. |
| 1586 | | fn mergeSections(self: *Wasm) !void { |
| 1585 | fn mergeSections(wasm: *Wasm) !void { |
| 1587 | 1586 | // append the indirect function table if initialized |
| 1588 | | if (self.string_table.getOffset("__indirect_function_table")) |offset| { |
| 1589 | | const sym_loc = self.globals.get(offset).?; |
| 1590 | | const table: wasm.Table = .{ |
| 1591 | | .limits = .{ .min = @intCast(u32, self.function_table.count()), .max = null }, |
| 1587 | if (wasm.string_table.getOffset("__indirect_function_table")) |offset| { |
| 1588 | const sym_loc = wasm.globals.get(offset).?; |
| 1589 | const table: std.wasm.Table = .{ |
| 1590 | .limits = .{ .min = @intCast(u32, wasm.function_table.count()), .max = null }, |
| 1592 | 1591 | .reftype = .funcref, |
| 1593 | 1592 | }; |
| 1594 | | sym_loc.getSymbol(self).index = @intCast(u32, self.tables.items.len) + self.imported_tables_count; |
| 1595 | | try self.tables.append(self.base.allocator, table); |
| 1593 | sym_loc.getSymbol(wasm).index = @intCast(u32, wasm.tables.items.len) + wasm.imported_tables_count; |
| 1594 | try wasm.tables.append(wasm.base.allocator, table); |
| 1596 | 1595 | } |
| 1597 | 1596 | |
| 1598 | | for (self.resolved_symbols.keys()) |sym_loc| { |
| 1597 | for (wasm.resolved_symbols.keys()) |sym_loc| { |
| 1599 | 1598 | if (sym_loc.file == null) { |
| 1600 | 1599 | // Zig code-generated symbols are already within the sections and do not |
| 1601 | 1600 | // require to be merged |
| 1602 | 1601 | continue; |
| 1603 | 1602 | } |
| 1604 | 1603 | |
| 1605 | | const object = self.objects.items[sym_loc.file.?]; |
| 1604 | const object = wasm.objects.items[sym_loc.file.?]; |
| 1606 | 1605 | const symbol = &object.symtable[sym_loc.index]; |
| 1607 | 1606 | if (symbol.isUndefined() or (symbol.tag != .function and symbol.tag != .global and symbol.tag != .table)) { |
| 1608 | 1607 | // Skip undefined symbols as they go in the `import` section |
| ... | ... | @@ -1615,51 +1614,51 @@ fn mergeSections(self: *Wasm) !void { |
| 1615 | 1614 | switch (symbol.tag) { |
| 1616 | 1615 | .function => { |
| 1617 | 1616 | const original_func = object.functions[index]; |
| 1618 | | const gop = try self.functions.getOrPut( |
| 1619 | | self.base.allocator, |
| 1617 | const gop = try wasm.functions.getOrPut( |
| 1618 | wasm.base.allocator, |
| 1620 | 1619 | .{ .file = sym_loc.file, .index = symbol.index }, |
| 1621 | 1620 | ); |
| 1622 | 1621 | if (!gop.found_existing) { |
| 1623 | 1622 | gop.value_ptr.* = original_func; |
| 1624 | 1623 | } |
| 1625 | | symbol.index = @intCast(u32, gop.index) + self.imported_functions_count; |
| 1624 | symbol.index = @intCast(u32, gop.index) + wasm.imported_functions_count; |
| 1626 | 1625 | }, |
| 1627 | 1626 | .global => { |
| 1628 | 1627 | const original_global = object.globals[index]; |
| 1629 | | symbol.index = @intCast(u32, self.wasm_globals.items.len) + self.imported_globals_count; |
| 1630 | | try self.wasm_globals.append(self.base.allocator, original_global); |
| 1628 | symbol.index = @intCast(u32, wasm.wasm_globals.items.len) + wasm.imported_globals_count; |
| 1629 | try wasm.wasm_globals.append(wasm.base.allocator, original_global); |
| 1631 | 1630 | }, |
| 1632 | 1631 | .table => { |
| 1633 | 1632 | const original_table = object.tables[index]; |
| 1634 | | symbol.index = @intCast(u32, self.tables.items.len) + self.imported_tables_count; |
| 1635 | | try self.tables.append(self.base.allocator, original_table); |
| 1633 | symbol.index = @intCast(u32, wasm.tables.items.len) + wasm.imported_tables_count; |
| 1634 | try wasm.tables.append(wasm.base.allocator, original_table); |
| 1636 | 1635 | }, |
| 1637 | 1636 | else => unreachable, |
| 1638 | 1637 | } |
| 1639 | 1638 | } |
| 1640 | 1639 | |
| 1641 | | log.debug("Merged ({d}) functions", .{self.functions.count()}); |
| 1642 | | log.debug("Merged ({d}) globals", .{self.wasm_globals.items.len}); |
| 1643 | | log.debug("Merged ({d}) tables", .{self.tables.items.len}); |
| 1640 | log.debug("Merged ({d}) functions", .{wasm.functions.count()}); |
| 1641 | log.debug("Merged ({d}) globals", .{wasm.wasm_globals.items.len}); |
| 1642 | log.debug("Merged ({d}) tables", .{wasm.tables.items.len}); |
| 1644 | 1643 | } |
| 1645 | 1644 | |
| 1646 | 1645 | /// Merges function types of all object files into the final |
| 1647 | 1646 | /// 'types' section, while assigning the type index to the representing |
| 1648 | 1647 | /// section (import, export, function). |
| 1649 | | fn mergeTypes(self: *Wasm) !void { |
| 1648 | fn mergeTypes(wasm: *Wasm) !void { |
| 1650 | 1649 | // A map to track which functions have already had their |
| 1651 | 1650 | // type inserted. If we do this for the same function multiple times, |
| 1652 | 1651 | // it will be overwritten with the incorrect type. |
| 1653 | | var dirty = std.AutoHashMap(u32, void).init(self.base.allocator); |
| 1654 | | try dirty.ensureUnusedCapacity(@intCast(u32, self.functions.count())); |
| 1652 | var dirty = std.AutoHashMap(u32, void).init(wasm.base.allocator); |
| 1653 | try dirty.ensureUnusedCapacity(@intCast(u32, wasm.functions.count())); |
| 1655 | 1654 | defer dirty.deinit(); |
| 1656 | 1655 | |
| 1657 | | for (self.resolved_symbols.keys()) |sym_loc| { |
| 1656 | for (wasm.resolved_symbols.keys()) |sym_loc| { |
| 1658 | 1657 | if (sym_loc.file == null) { |
| 1659 | 1658 | // zig code-generated symbols are already present in final type section |
| 1660 | 1659 | continue; |
| 1661 | 1660 | } |
| 1662 | | const object = self.objects.items[sym_loc.file.?]; |
| 1661 | const object = wasm.objects.items[sym_loc.file.?]; |
| 1663 | 1662 | const symbol = object.symtable[sym_loc.index]; |
| 1664 | 1663 | if (symbol.tag != .function) { |
| 1665 | 1664 | // Only functions have types |
| ... | ... | @@ -1667,32 +1666,32 @@ fn mergeTypes(self: *Wasm) !void { |
| 1667 | 1666 | } |
| 1668 | 1667 | |
| 1669 | 1668 | if (symbol.isUndefined()) { |
| 1670 | | log.debug("Adding type from extern function '{s}'", .{sym_loc.getName(self)}); |
| 1671 | | const import: *types.Import = self.imports.getPtr(sym_loc).?; |
| 1669 | log.debug("Adding type from extern function '{s}'", .{sym_loc.getName(wasm)}); |
| 1670 | const import: *types.Import = wasm.imports.getPtr(sym_loc).?; |
| 1672 | 1671 | const original_type = object.func_types[import.kind.function]; |
| 1673 | | import.kind.function = try self.putOrGetFuncType(original_type); |
| 1672 | import.kind.function = try wasm.putOrGetFuncType(original_type); |
| 1674 | 1673 | } else if (!dirty.contains(symbol.index)) { |
| 1675 | | log.debug("Adding type from function '{s}'", .{sym_loc.getName(self)}); |
| 1676 | | const func = &self.functions.values()[symbol.index - self.imported_functions_count]; |
| 1677 | | func.type_index = try self.putOrGetFuncType(object.func_types[func.type_index]); |
| 1674 | log.debug("Adding type from function '{s}'", .{sym_loc.getName(wasm)}); |
| 1675 | const func = &wasm.functions.values()[symbol.index - wasm.imported_functions_count]; |
| 1676 | func.type_index = try wasm.putOrGetFuncType(object.func_types[func.type_index]); |
| 1678 | 1677 | dirty.putAssumeCapacityNoClobber(symbol.index, {}); |
| 1679 | 1678 | } |
| 1680 | 1679 | } |
| 1681 | | log.debug("Completed merging and deduplicating types. Total count: ({d})", .{self.func_types.items.len}); |
| 1680 | log.debug("Completed merging and deduplicating types. Total count: ({d})", .{wasm.func_types.items.len}); |
| 1682 | 1681 | } |
| 1683 | 1682 | |
| 1684 | | fn setupExports(self: *Wasm) !void { |
| 1685 | | if (self.base.options.output_mode == .Obj) return; |
| 1683 | fn setupExports(wasm: *Wasm) !void { |
| 1684 | if (wasm.base.options.output_mode == .Obj) return; |
| 1686 | 1685 | log.debug("Building exports from symbols", .{}); |
| 1687 | 1686 | |
| 1688 | | for (self.resolved_symbols.keys()) |sym_loc| { |
| 1689 | | const symbol = sym_loc.getSymbol(self); |
| 1687 | for (wasm.resolved_symbols.keys()) |sym_loc| { |
| 1688 | const symbol = sym_loc.getSymbol(wasm); |
| 1690 | 1689 | if (!symbol.isExported()) continue; |
| 1691 | 1690 | |
| 1692 | | const sym_name = sym_loc.getName(self); |
| 1693 | | const export_name = if (self.export_names.get(sym_loc)) |name| name else blk: { |
| 1691 | const sym_name = sym_loc.getName(wasm); |
| 1692 | const export_name = if (wasm.export_names.get(sym_loc)) |name| name else blk: { |
| 1694 | 1693 | if (sym_loc.file == null) break :blk symbol.name; |
| 1695 | | break :blk try self.string_table.put(self.base.allocator, sym_name); |
| 1694 | break :blk try wasm.string_table.put(wasm.base.allocator, sym_name); |
| 1696 | 1695 | }; |
| 1697 | 1696 | const exp: types.Export = .{ |
| 1698 | 1697 | .name = export_name, |
| ... | ... | @@ -1701,21 +1700,21 @@ fn setupExports(self: *Wasm) !void { |
| 1701 | 1700 | }; |
| 1702 | 1701 | log.debug("Exporting symbol '{s}' as '{s}' at index: ({d})", .{ |
| 1703 | 1702 | sym_name, |
| 1704 | | self.string_table.get(exp.name), |
| 1703 | wasm.string_table.get(exp.name), |
| 1705 | 1704 | exp.index, |
| 1706 | 1705 | }); |
| 1707 | | try self.exports.append(self.base.allocator, exp); |
| 1706 | try wasm.exports.append(wasm.base.allocator, exp); |
| 1708 | 1707 | } |
| 1709 | 1708 | |
| 1710 | | log.debug("Completed building exports. Total count: ({d})", .{self.exports.items.len}); |
| 1709 | log.debug("Completed building exports. Total count: ({d})", .{wasm.exports.items.len}); |
| 1711 | 1710 | } |
| 1712 | 1711 | |
| 1713 | | fn setupStart(self: *Wasm) !void { |
| 1714 | | const entry_name = self.base.options.entry orelse "_start"; |
| 1712 | fn setupStart(wasm: *Wasm) !void { |
| 1713 | const entry_name = wasm.base.options.entry orelse "_start"; |
| 1715 | 1714 | |
| 1716 | | const symbol_name_offset = self.string_table.getOffset(entry_name) orelse { |
| 1717 | | if (self.base.options.output_mode == .Exe) { |
| 1718 | | if (self.base.options.wasi_exec_model == .reactor) return; // Not required for reactors |
| 1715 | const symbol_name_offset = wasm.string_table.getOffset(entry_name) orelse { |
| 1716 | if (wasm.base.options.output_mode == .Exe) { |
| 1717 | if (wasm.base.options.wasi_exec_model == .reactor) return; // Not required for reactors |
| 1719 | 1718 | } else { |
| 1720 | 1719 | return; // No entry point needed for non-executable wasm files |
| 1721 | 1720 | } |
| ... | ... | @@ -1723,45 +1722,45 @@ fn setupStart(self: *Wasm) !void { |
| 1723 | 1722 | return error.MissingSymbol; |
| 1724 | 1723 | }; |
| 1725 | 1724 | |
| 1726 | | const symbol_loc = self.globals.get(symbol_name_offset).?; |
| 1727 | | const symbol = symbol_loc.getSymbol(self); |
| 1725 | const symbol_loc = wasm.globals.get(symbol_name_offset).?; |
| 1726 | const symbol = symbol_loc.getSymbol(wasm); |
| 1728 | 1727 | if (symbol.tag != .function) { |
| 1729 | 1728 | log.err("Entry symbol '{s}' is not a function", .{entry_name}); |
| 1730 | 1729 | return error.InvalidEntryKind; |
| 1731 | 1730 | } |
| 1732 | 1731 | |
| 1733 | 1732 | // Ensure the symbol is exported so host environment can access it |
| 1734 | | if (self.base.options.output_mode != .Obj) { |
| 1733 | if (wasm.base.options.output_mode != .Obj) { |
| 1735 | 1734 | symbol.setFlag(.WASM_SYM_EXPORTED); |
| 1736 | 1735 | } |
| 1737 | 1736 | } |
| 1738 | 1737 | |
| 1739 | 1738 | /// Sets up the memory section of the wasm module, as well as the stack. |
| 1740 | | fn setupMemory(self: *Wasm) !void { |
| 1739 | fn setupMemory(wasm: *Wasm) !void { |
| 1741 | 1740 | log.debug("Setting up memory layout", .{}); |
| 1742 | 1741 | const page_size = 64 * 1024; |
| 1743 | | const stack_size = self.base.options.stack_size_override orelse page_size * 1; |
| 1742 | const stack_size = wasm.base.options.stack_size_override orelse page_size * 1; |
| 1744 | 1743 | const stack_alignment = 16; // wasm's stack alignment as specified by tool-convention |
| 1745 | 1744 | // Always place the stack at the start by default |
| 1746 | 1745 | // unless the user specified the global-base flag |
| 1747 | 1746 | var place_stack_first = true; |
| 1748 | | var memory_ptr: u64 = if (self.base.options.global_base) |base| blk: { |
| 1747 | var memory_ptr: u64 = if (wasm.base.options.global_base) |base| blk: { |
| 1749 | 1748 | place_stack_first = false; |
| 1750 | 1749 | break :blk base; |
| 1751 | 1750 | } else 0; |
| 1752 | 1751 | |
| 1753 | | const is_obj = self.base.options.output_mode == .Obj; |
| 1752 | const is_obj = wasm.base.options.output_mode == .Obj; |
| 1754 | 1753 | |
| 1755 | 1754 | if (place_stack_first and !is_obj) { |
| 1756 | 1755 | memory_ptr = std.mem.alignForwardGeneric(u64, memory_ptr, stack_alignment); |
| 1757 | 1756 | memory_ptr += stack_size; |
| 1758 | 1757 | // We always put the stack pointer global at index 0 |
| 1759 | | self.wasm_globals.items[0].init.i32_const = @bitCast(i32, @intCast(u32, memory_ptr)); |
| 1758 | wasm.wasm_globals.items[0].init.i32_const = @bitCast(i32, @intCast(u32, memory_ptr)); |
| 1760 | 1759 | } |
| 1761 | 1760 | |
| 1762 | 1761 | var offset: u32 = @intCast(u32, memory_ptr); |
| 1763 | | for (self.data_segments.values()) |segment_index| { |
| 1764 | | const segment = &self.segments.items[segment_index]; |
| 1762 | for (wasm.data_segments.values()) |segment_index| { |
| 1763 | const segment = &wasm.segments.items[segment_index]; |
| 1765 | 1764 | memory_ptr = std.mem.alignForwardGeneric(u64, memory_ptr, segment.alignment); |
| 1766 | 1765 | memory_ptr += segment.size; |
| 1767 | 1766 | segment.offset = offset; |
| ... | ... | @@ -1771,14 +1770,14 @@ fn setupMemory(self: *Wasm) !void { |
| 1771 | 1770 | if (!place_stack_first and !is_obj) { |
| 1772 | 1771 | memory_ptr = std.mem.alignForwardGeneric(u64, memory_ptr, stack_alignment); |
| 1773 | 1772 | memory_ptr += stack_size; |
| 1774 | | self.wasm_globals.items[0].init.i32_const = @bitCast(i32, @intCast(u32, memory_ptr)); |
| 1773 | wasm.wasm_globals.items[0].init.i32_const = @bitCast(i32, @intCast(u32, memory_ptr)); |
| 1775 | 1774 | } |
| 1776 | 1775 | |
| 1777 | 1776 | // Setup the max amount of pages |
| 1778 | 1777 | // For now we only support wasm32 by setting the maximum allowed memory size 2^32-1 |
| 1779 | 1778 | const max_memory_allowed: u64 = (1 << 32) - 1; |
| 1780 | 1779 | |
| 1781 | | if (self.base.options.initial_memory) |initial_memory| { |
| 1780 | if (wasm.base.options.initial_memory) |initial_memory| { |
| 1782 | 1781 | if (!std.mem.isAlignedGeneric(u64, initial_memory, page_size)) { |
| 1783 | 1782 | log.err("Initial memory must be {d}-byte aligned", .{page_size}); |
| 1784 | 1783 | return error.MissAlignment; |
| ... | ... | @@ -1796,10 +1795,10 @@ fn setupMemory(self: *Wasm) !void { |
| 1796 | 1795 | |
| 1797 | 1796 | // In case we do not import memory, but define it ourselves, |
| 1798 | 1797 | // set the minimum amount of pages on the memory section. |
| 1799 | | self.memories.limits.min = @intCast(u32, std.mem.alignForwardGeneric(u64, memory_ptr, page_size) / page_size); |
| 1800 | | log.debug("Total memory pages: {d}", .{self.memories.limits.min}); |
| 1798 | wasm.memories.limits.min = @intCast(u32, std.mem.alignForwardGeneric(u64, memory_ptr, page_size) / page_size); |
| 1799 | log.debug("Total memory pages: {d}", .{wasm.memories.limits.min}); |
| 1801 | 1800 | |
| 1802 | | if (self.base.options.max_memory) |max_memory| { |
| 1801 | if (wasm.base.options.max_memory) |max_memory| { |
| 1803 | 1802 | if (!std.mem.isAlignedGeneric(u64, max_memory, page_size)) { |
| 1804 | 1803 | log.err("Maximum memory must be {d}-byte aligned", .{page_size}); |
| 1805 | 1804 | return error.MissAlignment; |
| ... | ... | @@ -1812,83 +1811,83 @@ fn setupMemory(self: *Wasm) !void { |
| 1812 | 1811 | log.err("Maximum memory exceeds maxmium amount {d}", .{max_memory_allowed}); |
| 1813 | 1812 | return error.MemoryTooBig; |
| 1814 | 1813 | } |
| 1815 | | self.memories.limits.max = @intCast(u32, max_memory / page_size); |
| 1816 | | log.debug("Maximum memory pages: {?d}", .{self.memories.limits.max}); |
| 1814 | wasm.memories.limits.max = @intCast(u32, max_memory / page_size); |
| 1815 | log.debug("Maximum memory pages: {?d}", .{wasm.memories.limits.max}); |
| 1817 | 1816 | } |
| 1818 | 1817 | } |
| 1819 | 1818 | |
| 1820 | 1819 | /// From a given object's index and the index of the segment, returns the corresponding |
| 1821 | 1820 | /// index of the segment within the final data section. When the segment does not yet |
| 1822 | 1821 | /// exist, a new one will be initialized and appended. The new index will be returned in that case. |
| 1823 | | pub fn getMatchingSegment(self: *Wasm, object_index: u16, relocatable_index: u32) !?u32 { |
| 1824 | | const object: Object = self.objects.items[object_index]; |
| 1822 | pub fn getMatchingSegment(wasm: *Wasm, object_index: u16, relocatable_index: u32) !?u32 { |
| 1823 | const object: Object = wasm.objects.items[object_index]; |
| 1825 | 1824 | const relocatable_data = object.relocatable_data[relocatable_index]; |
| 1826 | | const index = @intCast(u32, self.segments.items.len); |
| 1825 | const index = @intCast(u32, wasm.segments.items.len); |
| 1827 | 1826 | |
| 1828 | 1827 | switch (relocatable_data.type) { |
| 1829 | 1828 | .data => { |
| 1830 | 1829 | const segment_info = object.segment_info[relocatable_data.index]; |
| 1831 | | const merge_segment = self.base.options.output_mode != .Obj; |
| 1832 | | const result = try self.data_segments.getOrPut(self.base.allocator, segment_info.outputName(merge_segment)); |
| 1830 | const merge_segment = wasm.base.options.output_mode != .Obj; |
| 1831 | const result = try wasm.data_segments.getOrPut(wasm.base.allocator, segment_info.outputName(merge_segment)); |
| 1833 | 1832 | if (!result.found_existing) { |
| 1834 | 1833 | result.value_ptr.* = index; |
| 1835 | | try self.appendDummySegment(); |
| 1834 | try wasm.appendDummySegment(); |
| 1836 | 1835 | return index; |
| 1837 | 1836 | } else return result.value_ptr.*; |
| 1838 | 1837 | }, |
| 1839 | | .code => return self.code_section_index orelse blk: { |
| 1840 | | self.code_section_index = index; |
| 1841 | | try self.appendDummySegment(); |
| 1838 | .code => return wasm.code_section_index orelse blk: { |
| 1839 | wasm.code_section_index = index; |
| 1840 | try wasm.appendDummySegment(); |
| 1842 | 1841 | break :blk index; |
| 1843 | 1842 | }, |
| 1844 | 1843 | .debug => { |
| 1845 | 1844 | const debug_name = object.getDebugName(relocatable_data); |
| 1846 | 1845 | if (mem.eql(u8, debug_name, ".debug_info")) { |
| 1847 | | return self.debug_info_index orelse blk: { |
| 1848 | | self.debug_info_index = index; |
| 1849 | | try self.appendDummySegment(); |
| 1846 | return wasm.debug_info_index orelse blk: { |
| 1847 | wasm.debug_info_index = index; |
| 1848 | try wasm.appendDummySegment(); |
| 1850 | 1849 | break :blk index; |
| 1851 | 1850 | }; |
| 1852 | 1851 | } else if (mem.eql(u8, debug_name, ".debug_line")) { |
| 1853 | | return self.debug_line_index orelse blk: { |
| 1854 | | self.debug_line_index = index; |
| 1855 | | try self.appendDummySegment(); |
| 1852 | return wasm.debug_line_index orelse blk: { |
| 1853 | wasm.debug_line_index = index; |
| 1854 | try wasm.appendDummySegment(); |
| 1856 | 1855 | break :blk index; |
| 1857 | 1856 | }; |
| 1858 | 1857 | } else if (mem.eql(u8, debug_name, ".debug_loc")) { |
| 1859 | | return self.debug_loc_index orelse blk: { |
| 1860 | | self.debug_loc_index = index; |
| 1861 | | try self.appendDummySegment(); |
| 1858 | return wasm.debug_loc_index orelse blk: { |
| 1859 | wasm.debug_loc_index = index; |
| 1860 | try wasm.appendDummySegment(); |
| 1862 | 1861 | break :blk index; |
| 1863 | 1862 | }; |
| 1864 | 1863 | } else if (mem.eql(u8, debug_name, ".debug_ranges")) { |
| 1865 | | return self.debug_line_index orelse blk: { |
| 1866 | | self.debug_ranges_index = index; |
| 1867 | | try self.appendDummySegment(); |
| 1864 | return wasm.debug_line_index orelse blk: { |
| 1865 | wasm.debug_ranges_index = index; |
| 1866 | try wasm.appendDummySegment(); |
| 1868 | 1867 | break :blk index; |
| 1869 | 1868 | }; |
| 1870 | 1869 | } else if (mem.eql(u8, debug_name, ".debug_pubnames")) { |
| 1871 | | return self.debug_pubnames_index orelse blk: { |
| 1872 | | self.debug_pubnames_index = index; |
| 1873 | | try self.appendDummySegment(); |
| 1870 | return wasm.debug_pubnames_index orelse blk: { |
| 1871 | wasm.debug_pubnames_index = index; |
| 1872 | try wasm.appendDummySegment(); |
| 1874 | 1873 | break :blk index; |
| 1875 | 1874 | }; |
| 1876 | 1875 | } else if (mem.eql(u8, debug_name, ".debug_pubtypes")) { |
| 1877 | | return self.debug_pubtypes_index orelse blk: { |
| 1878 | | self.debug_pubtypes_index = index; |
| 1879 | | try self.appendDummySegment(); |
| 1876 | return wasm.debug_pubtypes_index orelse blk: { |
| 1877 | wasm.debug_pubtypes_index = index; |
| 1878 | try wasm.appendDummySegment(); |
| 1880 | 1879 | break :blk index; |
| 1881 | 1880 | }; |
| 1882 | 1881 | } else if (mem.eql(u8, debug_name, ".debug_abbrev")) { |
| 1883 | | return self.debug_abbrev_index orelse blk: { |
| 1884 | | self.debug_abbrev_index = index; |
| 1885 | | try self.appendDummySegment(); |
| 1882 | return wasm.debug_abbrev_index orelse blk: { |
| 1883 | wasm.debug_abbrev_index = index; |
| 1884 | try wasm.appendDummySegment(); |
| 1886 | 1885 | break :blk index; |
| 1887 | 1886 | }; |
| 1888 | 1887 | } else if (mem.eql(u8, debug_name, ".debug_str")) { |
| 1889 | | return self.debug_str_index orelse blk: { |
| 1890 | | self.debug_str_index = index; |
| 1891 | | try self.appendDummySegment(); |
| 1888 | return wasm.debug_str_index orelse blk: { |
| 1889 | wasm.debug_str_index = index; |
| 1890 | try wasm.appendDummySegment(); |
| 1892 | 1891 | break :blk index; |
| 1893 | 1892 | }; |
| 1894 | 1893 | } else { |
| ... | ... | @@ -1901,8 +1900,8 @@ pub fn getMatchingSegment(self: *Wasm, object_index: u16, relocatable_index: u32 |
| 1901 | 1900 | } |
| 1902 | 1901 | |
| 1903 | 1902 | /// Appends a new segment with default field values |
| 1904 | | fn appendDummySegment(self: *Wasm) !void { |
| 1905 | | try self.segments.append(self.base.allocator, .{ |
| 1903 | fn appendDummySegment(wasm: *Wasm) !void { |
| 1904 | try wasm.segments.append(wasm.base.allocator, .{ |
| 1906 | 1905 | .alignment = 1, |
| 1907 | 1906 | .size = 0, |
| 1908 | 1907 | .offset = 0, |
| ... | ... | @@ -1912,8 +1911,8 @@ fn appendDummySegment(self: *Wasm) !void { |
| 1912 | 1911 | /// Returns the symbol index of the error name table. |
| 1913 | 1912 | /// |
| 1914 | 1913 | /// When the symbol does not yet exist, it will create a new one instead. |
| 1915 | | pub fn getErrorTableSymbol(self: *Wasm) !u32 { |
| 1916 | | if (self.error_table_symbol) |symbol| { |
| 1914 | pub fn getErrorTableSymbol(wasm: *Wasm) !u32 { |
| 1915 | if (wasm.error_table_symbol) |symbol| { |
| 1917 | 1916 | return symbol; |
| 1918 | 1917 | } |
| 1919 | 1918 | |
| ... | ... | @@ -1922,14 +1921,14 @@ pub fn getErrorTableSymbol(self: *Wasm) !u32 { |
| 1922 | 1921 | // during `flush` when we know all possible error names. |
| 1923 | 1922 | |
| 1924 | 1923 | // As sym_index '0' is reserved, we use it for our stack pointer symbol |
| 1925 | | const symbol_index = self.symbols_free_list.popOrNull() orelse blk: { |
| 1926 | | const index = @intCast(u32, self.symbols.items.len); |
| 1927 | | _ = try self.symbols.addOne(self.base.allocator); |
| 1924 | const symbol_index = wasm.symbols_free_list.popOrNull() orelse blk: { |
| 1925 | const index = @intCast(u32, wasm.symbols.items.len); |
| 1926 | _ = try wasm.symbols.addOne(wasm.base.allocator); |
| 1928 | 1927 | break :blk index; |
| 1929 | 1928 | }; |
| 1930 | 1929 | |
| 1931 | | const sym_name = try self.string_table.put(self.base.allocator, "__zig_err_name_table"); |
| 1932 | | const symbol = &self.symbols.items[symbol_index]; |
| 1930 | const sym_name = try wasm.string_table.put(wasm.base.allocator, "__zig_err_name_table"); |
| 1931 | const symbol = &wasm.symbols.items[symbol_index]; |
| 1933 | 1932 | symbol.* = .{ |
| 1934 | 1933 | .name = sym_name, |
| 1935 | 1934 | .tag = .data, |
| ... | ... | @@ -1940,17 +1939,17 @@ pub fn getErrorTableSymbol(self: *Wasm) !u32 { |
| 1940 | 1939 | |
| 1941 | 1940 | const slice_ty = Type.initTag(.const_slice_u8_sentinel_0); |
| 1942 | 1941 | |
| 1943 | | const atom = try self.base.allocator.create(Atom); |
| 1942 | const atom = try wasm.base.allocator.create(Atom); |
| 1944 | 1943 | atom.* = Atom.empty; |
| 1945 | 1944 | atom.sym_index = symbol_index; |
| 1946 | | atom.alignment = slice_ty.abiAlignment(self.base.options.target); |
| 1947 | | try self.managed_atoms.append(self.base.allocator, atom); |
| 1945 | atom.alignment = slice_ty.abiAlignment(wasm.base.options.target); |
| 1946 | try wasm.managed_atoms.append(wasm.base.allocator, atom); |
| 1948 | 1947 | const loc = atom.symbolLoc(); |
| 1949 | | try self.resolved_symbols.put(self.base.allocator, loc, {}); |
| 1950 | | try self.symbol_atom.put(self.base.allocator, loc, atom); |
| 1948 | try wasm.resolved_symbols.put(wasm.base.allocator, loc, {}); |
| 1949 | try wasm.symbol_atom.put(wasm.base.allocator, loc, atom); |
| 1951 | 1950 | |
| 1952 | 1951 | log.debug("Error name table was created with symbol index: ({d})", .{symbol_index}); |
| 1953 | | self.error_table_symbol = symbol_index; |
| 1952 | wasm.error_table_symbol = symbol_index; |
| 1954 | 1953 | return symbol_index; |
| 1955 | 1954 | } |
| 1956 | 1955 | |
| ... | ... | @@ -1958,24 +1957,24 @@ pub fn getErrorTableSymbol(self: *Wasm) !u32 { |
| 1958 | 1957 | /// |
| 1959 | 1958 | /// This creates a table that consists of pointers and length to each error name. |
| 1960 | 1959 | /// The table is what is being pointed to within the runtime bodies that are generated. |
| 1961 | | fn populateErrorNameTable(self: *Wasm) !void { |
| 1962 | | const symbol_index = self.error_table_symbol orelse return; |
| 1963 | | const atom: *Atom = self.symbol_atom.get(.{ .file = null, .index = symbol_index }).?; |
| 1960 | fn populateErrorNameTable(wasm: *Wasm) !void { |
| 1961 | const symbol_index = wasm.error_table_symbol orelse return; |
| 1962 | const atom: *Atom = wasm.symbol_atom.get(.{ .file = null, .index = symbol_index }).?; |
| 1964 | 1963 | // Rather than creating a symbol for each individual error name, |
| 1965 | 1964 | // we create a symbol for the entire region of error names. We then calculate |
| 1966 | 1965 | // the pointers into the list using addends which are appended to the relocation. |
| 1967 | | const names_atom = try self.base.allocator.create(Atom); |
| 1966 | const names_atom = try wasm.base.allocator.create(Atom); |
| 1968 | 1967 | names_atom.* = Atom.empty; |
| 1969 | | try self.managed_atoms.append(self.base.allocator, names_atom); |
| 1970 | | const names_symbol_index = self.symbols_free_list.popOrNull() orelse blk: { |
| 1971 | | const index = @intCast(u32, self.symbols.items.len); |
| 1972 | | _ = try self.symbols.addOne(self.base.allocator); |
| 1968 | try wasm.managed_atoms.append(wasm.base.allocator, names_atom); |
| 1969 | const names_symbol_index = wasm.symbols_free_list.popOrNull() orelse blk: { |
| 1970 | const index = @intCast(u32, wasm.symbols.items.len); |
| 1971 | _ = try wasm.symbols.addOne(wasm.base.allocator); |
| 1973 | 1972 | break :blk index; |
| 1974 | 1973 | }; |
| 1975 | 1974 | names_atom.sym_index = names_symbol_index; |
| 1976 | 1975 | names_atom.alignment = 1; |
| 1977 | | const sym_name = try self.string_table.put(self.base.allocator, "__zig_err_names"); |
| 1978 | | const names_symbol = &self.symbols.items[names_symbol_index]; |
| 1976 | const sym_name = try wasm.string_table.put(wasm.base.allocator, "__zig_err_names"); |
| 1977 | const names_symbol = &wasm.symbols.items[names_symbol_index]; |
| 1979 | 1978 | names_symbol.* = .{ |
| 1980 | 1979 | .name = sym_name, |
| 1981 | 1980 | .tag = .data, |
| ... | ... | @@ -1988,27 +1987,27 @@ fn populateErrorNameTable(self: *Wasm) !void { |
| 1988 | 1987 | |
| 1989 | 1988 | // Addend for each relocation to the table |
| 1990 | 1989 | var addend: u32 = 0; |
| 1991 | | const mod = self.base.options.module.?; |
| 1990 | const mod = wasm.base.options.module.?; |
| 1992 | 1991 | for (mod.error_name_list.items) |error_name| { |
| 1993 | 1992 | const len = @intCast(u32, error_name.len + 1); // names are 0-termianted |
| 1994 | 1993 | |
| 1995 | 1994 | const slice_ty = Type.initTag(.const_slice_u8_sentinel_0); |
| 1996 | 1995 | const offset = @intCast(u32, atom.code.items.len); |
| 1997 | 1996 | // first we create the data for the slice of the name |
| 1998 | | try atom.code.appendNTimes(self.base.allocator, 0, 4); // ptr to name, will be relocated |
| 1999 | | try atom.code.writer(self.base.allocator).writeIntLittle(u32, len - 1); |
| 1997 | try atom.code.appendNTimes(wasm.base.allocator, 0, 4); // ptr to name, will be relocated |
| 1998 | try atom.code.writer(wasm.base.allocator).writeIntLittle(u32, len - 1); |
| 2000 | 1999 | // create relocation to the error name |
| 2001 | | try atom.relocs.append(self.base.allocator, .{ |
| 2000 | try atom.relocs.append(wasm.base.allocator, .{ |
| 2002 | 2001 | .index = names_symbol_index, |
| 2003 | 2002 | .relocation_type = .R_WASM_MEMORY_ADDR_I32, |
| 2004 | 2003 | .offset = offset, |
| 2005 | 2004 | .addend = addend, |
| 2006 | 2005 | }); |
| 2007 | | atom.size += @intCast(u32, slice_ty.abiSize(self.base.options.target)); |
| 2006 | atom.size += @intCast(u32, slice_ty.abiSize(wasm.base.options.target)); |
| 2008 | 2007 | addend += len; |
| 2009 | 2008 | |
| 2010 | 2009 | // as we updated the error name table, we now store the actual name within the names atom |
| 2011 | | try names_atom.code.ensureUnusedCapacity(self.base.allocator, len); |
| 2010 | try names_atom.code.ensureUnusedCapacity(wasm.base.allocator, len); |
| 2012 | 2011 | names_atom.code.appendSliceAssumeCapacity(error_name); |
| 2013 | 2012 | names_atom.code.appendAssumeCapacity(0); |
| 2014 | 2013 | |
| ... | ... | @@ -2017,51 +2016,51 @@ fn populateErrorNameTable(self: *Wasm) !void { |
| 2017 | 2016 | names_atom.size = addend; |
| 2018 | 2017 | |
| 2019 | 2018 | const name_loc = names_atom.symbolLoc(); |
| 2020 | | try self.resolved_symbols.put(self.base.allocator, name_loc, {}); |
| 2021 | | try self.symbol_atom.put(self.base.allocator, name_loc, names_atom); |
| 2019 | try wasm.resolved_symbols.put(wasm.base.allocator, name_loc, {}); |
| 2020 | try wasm.symbol_atom.put(wasm.base.allocator, name_loc, names_atom); |
| 2022 | 2021 | |
| 2023 | 2022 | // link the atoms with the rest of the binary so they can be allocated |
| 2024 | 2023 | // and relocations will be performed. |
| 2025 | | try self.parseAtom(atom, .{ .data = .read_only }); |
| 2026 | | try self.parseAtom(names_atom, .{ .data = .read_only }); |
| 2024 | try wasm.parseAtom(atom, .{ .data = .read_only }); |
| 2025 | try wasm.parseAtom(names_atom, .{ .data = .read_only }); |
| 2027 | 2026 | } |
| 2028 | 2027 | |
| 2029 | 2028 | /// From a given index variable, creates a new debug section. |
| 2030 | 2029 | /// This initializes the index, appends a new segment, |
| 2031 | 2030 | /// and finally, creates a managed `Atom`. |
| 2032 | | pub fn createDebugSectionForIndex(self: *Wasm, index: *?u32, name: []const u8) !*Atom { |
| 2033 | | const new_index = @intCast(u32, self.segments.items.len); |
| 2031 | pub fn createDebugSectionForIndex(wasm: *Wasm, index: *?u32, name: []const u8) !*Atom { |
| 2032 | const new_index = @intCast(u32, wasm.segments.items.len); |
| 2034 | 2033 | index.* = new_index; |
| 2035 | | try self.appendDummySegment(); |
| 2034 | try wasm.appendDummySegment(); |
| 2036 | 2035 | // _ = index; |
| 2037 | 2036 | |
| 2038 | | const sym_index = self.symbols_free_list.popOrNull() orelse idx: { |
| 2039 | | const tmp_index = @intCast(u32, self.symbols.items.len); |
| 2040 | | _ = try self.symbols.addOne(self.base.allocator); |
| 2037 | const sym_index = wasm.symbols_free_list.popOrNull() orelse idx: { |
| 2038 | const tmp_index = @intCast(u32, wasm.symbols.items.len); |
| 2039 | _ = try wasm.symbols.addOne(wasm.base.allocator); |
| 2041 | 2040 | break :idx tmp_index; |
| 2042 | 2041 | }; |
| 2043 | | self.symbols.items[sym_index] = .{ |
| 2042 | wasm.symbols.items[sym_index] = .{ |
| 2044 | 2043 | .tag = .section, |
| 2045 | | .name = try self.string_table.put(self.base.allocator, name), |
| 2044 | .name = try wasm.string_table.put(wasm.base.allocator, name), |
| 2046 | 2045 | .index = 0, |
| 2047 | 2046 | .flags = @enumToInt(Symbol.Flag.WASM_SYM_BINDING_LOCAL), |
| 2048 | 2047 | }; |
| 2049 | 2048 | |
| 2050 | | const atom = try self.base.allocator.create(Atom); |
| 2049 | const atom = try wasm.base.allocator.create(Atom); |
| 2051 | 2050 | atom.* = Atom.empty; |
| 2052 | 2051 | atom.alignment = 1; // debug sections are always 1-byte-aligned |
| 2053 | 2052 | atom.sym_index = sym_index; |
| 2054 | | try self.managed_atoms.append(self.base.allocator, atom); |
| 2055 | | try self.symbol_atom.put(self.base.allocator, atom.symbolLoc(), atom); |
| 2053 | try wasm.managed_atoms.append(wasm.base.allocator, atom); |
| 2054 | try wasm.symbol_atom.put(wasm.base.allocator, atom.symbolLoc(), atom); |
| 2056 | 2055 | return atom; |
| 2057 | 2056 | } |
| 2058 | 2057 | |
| 2059 | | fn resetState(self: *Wasm) void { |
| 2060 | | for (self.segment_info.values()) |segment_info| { |
| 2061 | | self.base.allocator.free(segment_info.name); |
| 2058 | fn resetState(wasm: *Wasm) void { |
| 2059 | for (wasm.segment_info.values()) |segment_info| { |
| 2060 | wasm.base.allocator.free(segment_info.name); |
| 2062 | 2061 | } |
| 2063 | | if (self.base.options.module) |mod| { |
| 2064 | | var decl_it = self.decls.keyIterator(); |
| 2062 | if (wasm.base.options.module) |mod| { |
| 2063 | var decl_it = wasm.decls.keyIterator(); |
| 2065 | 2064 | while (decl_it.next()) |decl_index_ptr| { |
| 2066 | 2065 | const decl = mod.declPtr(decl_index_ptr.*); |
| 2067 | 2066 | const atom = &decl.link.wasm; |
| ... | ... | @@ -2074,46 +2073,46 @@ fn resetState(self: *Wasm) void { |
| 2074 | 2073 | } |
| 2075 | 2074 | } |
| 2076 | 2075 | } |
| 2077 | | self.functions.clearRetainingCapacity(); |
| 2078 | | self.exports.clearRetainingCapacity(); |
| 2079 | | self.segments.clearRetainingCapacity(); |
| 2080 | | self.segment_info.clearRetainingCapacity(); |
| 2081 | | self.data_segments.clearRetainingCapacity(); |
| 2082 | | self.atoms.clearRetainingCapacity(); |
| 2083 | | self.symbol_atom.clearRetainingCapacity(); |
| 2084 | | self.code_section_index = null; |
| 2085 | | self.debug_info_index = null; |
| 2086 | | self.debug_line_index = null; |
| 2087 | | self.debug_loc_index = null; |
| 2088 | | self.debug_str_index = null; |
| 2089 | | self.debug_ranges_index = null; |
| 2090 | | self.debug_abbrev_index = null; |
| 2091 | | self.debug_pubnames_index = null; |
| 2092 | | self.debug_pubtypes_index = null; |
| 2076 | wasm.functions.clearRetainingCapacity(); |
| 2077 | wasm.exports.clearRetainingCapacity(); |
| 2078 | wasm.segments.clearRetainingCapacity(); |
| 2079 | wasm.segment_info.clearRetainingCapacity(); |
| 2080 | wasm.data_segments.clearRetainingCapacity(); |
| 2081 | wasm.atoms.clearRetainingCapacity(); |
| 2082 | wasm.symbol_atom.clearRetainingCapacity(); |
| 2083 | wasm.code_section_index = null; |
| 2084 | wasm.debug_info_index = null; |
| 2085 | wasm.debug_line_index = null; |
| 2086 | wasm.debug_loc_index = null; |
| 2087 | wasm.debug_str_index = null; |
| 2088 | wasm.debug_ranges_index = null; |
| 2089 | wasm.debug_abbrev_index = null; |
| 2090 | wasm.debug_pubnames_index = null; |
| 2091 | wasm.debug_pubtypes_index = null; |
| 2093 | 2092 | } |
| 2094 | 2093 | |
| 2095 | | pub fn flush(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) !void { |
| 2096 | | if (self.base.options.emit == null) { |
| 2094 | pub fn flush(wasm: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) !void { |
| 2095 | if (wasm.base.options.emit == null) { |
| 2097 | 2096 | if (build_options.have_llvm) { |
| 2098 | | if (self.llvm_object) |llvm_object| { |
| 2097 | if (wasm.llvm_object) |llvm_object| { |
| 2099 | 2098 | return try llvm_object.flushModule(comp, prog_node); |
| 2100 | 2099 | } |
| 2101 | 2100 | } |
| 2102 | 2101 | return; |
| 2103 | 2102 | } |
| 2104 | | if (build_options.have_llvm and self.base.options.use_lld) { |
| 2105 | | return self.linkWithLLD(comp, prog_node); |
| 2103 | if (build_options.have_llvm and wasm.base.options.use_lld) { |
| 2104 | return wasm.linkWithLLD(comp, prog_node); |
| 2106 | 2105 | } else { |
| 2107 | | return self.flushModule(comp, prog_node); |
| 2106 | return wasm.flushModule(comp, prog_node); |
| 2108 | 2107 | } |
| 2109 | 2108 | } |
| 2110 | 2109 | |
| 2111 | | pub fn flushModule(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) !void { |
| 2110 | pub fn flushModule(wasm: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) !void { |
| 2112 | 2111 | const tracy = trace(@src()); |
| 2113 | 2112 | defer tracy.end(); |
| 2114 | 2113 | |
| 2115 | 2114 | if (build_options.have_llvm) { |
| 2116 | | if (self.llvm_object) |llvm_object| { |
| 2115 | if (wasm.llvm_object) |llvm_object| { |
| 2117 | 2116 | return try llvm_object.flushModule(comp, prog_node); |
| 2118 | 2117 | } |
| 2119 | 2118 | } |
| ... | ... | @@ -2123,7 +2122,7 @@ pub fn flushModule(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Nod |
| 2123 | 2122 | defer sub_prog_node.end(); |
| 2124 | 2123 | |
| 2125 | 2124 | // ensure the error names table is populated when an error name is referenced |
| 2126 | | try self.populateErrorNameTable(); |
| 2125 | try wasm.populateErrorNameTable(); |
| 2127 | 2126 | |
| 2128 | 2127 | // The amount of sections that will be written |
| 2129 | 2128 | var section_count: u32 = 0; |
| ... | ... | @@ -2133,15 +2132,15 @@ pub fn flushModule(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Nod |
| 2133 | 2132 | var data_section_index: ?u32 = null; |
| 2134 | 2133 | |
| 2135 | 2134 | // Used for all temporary memory allocated during flushin |
| 2136 | | var arena_instance = std.heap.ArenaAllocator.init(self.base.allocator); |
| 2135 | var arena_instance = std.heap.ArenaAllocator.init(wasm.base.allocator); |
| 2137 | 2136 | defer arena_instance.deinit(); |
| 2138 | 2137 | const arena = arena_instance.allocator(); |
| 2139 | 2138 | |
| 2140 | 2139 | // Positional arguments to the linker such as object files and static archives. |
| 2141 | 2140 | var positionals = std.ArrayList([]const u8).init(arena); |
| 2142 | | try positionals.ensureUnusedCapacity(self.base.options.objects.len); |
| 2141 | try positionals.ensureUnusedCapacity(wasm.base.options.objects.len); |
| 2143 | 2142 | |
| 2144 | | for (self.base.options.objects) |object| { |
| 2143 | for (wasm.base.options.objects) |object| { |
| 2145 | 2144 | positionals.appendAssumeCapacity(object.path); |
| 2146 | 2145 | } |
| 2147 | 2146 | |
| ... | ... | @@ -2153,66 +2152,66 @@ pub fn flushModule(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Nod |
| 2153 | 2152 | try positionals.append(lib.full_object_path); |
| 2154 | 2153 | } |
| 2155 | 2154 | |
| 2156 | | try self.parseInputFiles(positionals.items); |
| 2155 | try wasm.parseInputFiles(positionals.items); |
| 2157 | 2156 | |
| 2158 | | for (self.objects.items) |_, object_index| { |
| 2159 | | try self.resolveSymbolsInObject(@intCast(u16, object_index)); |
| 2157 | for (wasm.objects.items) |_, object_index| { |
| 2158 | try wasm.resolveSymbolsInObject(@intCast(u16, object_index)); |
| 2160 | 2159 | } |
| 2161 | 2160 | |
| 2162 | | try self.resolveSymbolsInArchives(); |
| 2163 | | try self.checkUndefinedSymbols(); |
| 2161 | try wasm.resolveSymbolsInArchives(); |
| 2162 | try wasm.checkUndefinedSymbols(); |
| 2164 | 2163 | |
| 2165 | 2164 | // When we finish/error we reset the state of the linker |
| 2166 | 2165 | // So we can rebuild the binary file on each incremental update |
| 2167 | | defer self.resetState(); |
| 2168 | | try self.setupStart(); |
| 2169 | | try self.setupImports(); |
| 2170 | | if (self.base.options.module) |mod| { |
| 2171 | | var decl_it = self.decls.keyIterator(); |
| 2166 | defer wasm.resetState(); |
| 2167 | try wasm.setupStart(); |
| 2168 | try wasm.setupImports(); |
| 2169 | if (wasm.base.options.module) |mod| { |
| 2170 | var decl_it = wasm.decls.keyIterator(); |
| 2172 | 2171 | while (decl_it.next()) |decl_index_ptr| { |
| 2173 | 2172 | const decl = mod.declPtr(decl_index_ptr.*); |
| 2174 | 2173 | if (decl.isExtern()) continue; |
| 2175 | 2174 | const atom = &decl.*.link.wasm; |
| 2176 | 2175 | if (decl.ty.zigTypeTag() == .Fn) { |
| 2177 | | try self.parseAtom(atom, .{ .function = decl.fn_link.wasm }); |
| 2176 | try wasm.parseAtom(atom, .{ .function = decl.fn_link.wasm }); |
| 2178 | 2177 | } else if (decl.getVariable()) |variable| { |
| 2179 | 2178 | if (!variable.is_mutable) { |
| 2180 | | try self.parseAtom(atom, .{ .data = .read_only }); |
| 2179 | try wasm.parseAtom(atom, .{ .data = .read_only }); |
| 2181 | 2180 | } else if (variable.init.isUndefDeep()) { |
| 2182 | | try self.parseAtom(atom, .{ .data = .uninitialized }); |
| 2181 | try wasm.parseAtom(atom, .{ .data = .uninitialized }); |
| 2183 | 2182 | } else { |
| 2184 | | try self.parseAtom(atom, .{ .data = .initialized }); |
| 2183 | try wasm.parseAtom(atom, .{ .data = .initialized }); |
| 2185 | 2184 | } |
| 2186 | 2185 | } else { |
| 2187 | | try self.parseAtom(atom, .{ .data = .read_only }); |
| 2186 | try wasm.parseAtom(atom, .{ .data = .read_only }); |
| 2188 | 2187 | } |
| 2189 | 2188 | |
| 2190 | 2189 | // also parse atoms for a decl's locals |
| 2191 | 2190 | for (atom.locals.items) |*local_atom| { |
| 2192 | | try self.parseAtom(local_atom, .{ .data = .read_only }); |
| 2191 | try wasm.parseAtom(local_atom, .{ .data = .read_only }); |
| 2193 | 2192 | } |
| 2194 | 2193 | } |
| 2195 | 2194 | |
| 2196 | | if (self.dwarf) |*dwarf| { |
| 2197 | | try dwarf.flushModule(&self.base, self.base.options.module.?); |
| 2195 | if (wasm.dwarf) |*dwarf| { |
| 2196 | try dwarf.flushModule(&wasm.base, wasm.base.options.module.?); |
| 2198 | 2197 | } |
| 2199 | 2198 | } |
| 2200 | 2199 | |
| 2201 | | for (self.objects.items) |*object, object_index| { |
| 2202 | | try object.parseIntoAtoms(self.base.allocator, @intCast(u16, object_index), self); |
| 2200 | for (wasm.objects.items) |*object, object_index| { |
| 2201 | try object.parseIntoAtoms(wasm.base.allocator, @intCast(u16, object_index), wasm); |
| 2203 | 2202 | } |
| 2204 | 2203 | |
| 2205 | | try self.allocateAtoms(); |
| 2206 | | try self.setupMemory(); |
| 2207 | | self.mapFunctionTable(); |
| 2208 | | try self.mergeSections(); |
| 2209 | | try self.mergeTypes(); |
| 2210 | | try self.setupExports(); |
| 2204 | try wasm.allocateAtoms(); |
| 2205 | try wasm.setupMemory(); |
| 2206 | wasm.mapFunctionTable(); |
| 2207 | try wasm.mergeSections(); |
| 2208 | try wasm.mergeTypes(); |
| 2209 | try wasm.setupExports(); |
| 2211 | 2210 | |
| 2212 | 2211 | const header_size = 5 + 1; |
| 2213 | | const is_obj = self.base.options.output_mode == .Obj; |
| 2212 | const is_obj = wasm.base.options.output_mode == .Obj; |
| 2214 | 2213 | |
| 2215 | | var binary_bytes = std.ArrayList(u8).init(self.base.allocator); |
| 2214 | var binary_bytes = std.ArrayList(u8).init(wasm.base.allocator); |
| 2216 | 2215 | defer binary_bytes.deinit(); |
| 2217 | 2216 | const binary_writer = binary_bytes.writer(); |
| 2218 | 2217 | |
| ... | ... | @@ -2221,18 +2220,18 @@ pub fn flushModule(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Nod |
| 2221 | 2220 | try binary_writer.writeAll(&[_]u8{0} ** 8); |
| 2222 | 2221 | |
| 2223 | 2222 | // Type section |
| 2224 | | if (self.func_types.items.len != 0) { |
| 2223 | if (wasm.func_types.items.len != 0) { |
| 2225 | 2224 | const header_offset = try reserveVecSectionHeader(&binary_bytes); |
| 2226 | | log.debug("Writing type section. Count: ({d})", .{self.func_types.items.len}); |
| 2227 | | for (self.func_types.items) |func_type| { |
| 2228 | | try leb.writeULEB128(binary_writer, wasm.function_type); |
| 2225 | log.debug("Writing type section. Count: ({d})", .{wasm.func_types.items.len}); |
| 2226 | for (wasm.func_types.items) |func_type| { |
| 2227 | try leb.writeULEB128(binary_writer, std.wasm.function_type); |
| 2229 | 2228 | try leb.writeULEB128(binary_writer, @intCast(u32, func_type.params.len)); |
| 2230 | 2229 | for (func_type.params) |param_ty| { |
| 2231 | | try leb.writeULEB128(binary_writer, wasm.valtype(param_ty)); |
| 2230 | try leb.writeULEB128(binary_writer, std.wasm.valtype(param_ty)); |
| 2232 | 2231 | } |
| 2233 | 2232 | try leb.writeULEB128(binary_writer, @intCast(u32, func_type.returns.len)); |
| 2234 | 2233 | for (func_type.returns) |ret_ty| { |
| 2235 | | try leb.writeULEB128(binary_writer, wasm.valtype(ret_ty)); |
| 2234 | try leb.writeULEB128(binary_writer, std.wasm.valtype(ret_ty)); |
| 2236 | 2235 | } |
| 2237 | 2236 | } |
| 2238 | 2237 | |
| ... | ... | @@ -2241,50 +2240,50 @@ pub fn flushModule(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Nod |
| 2241 | 2240 | header_offset, |
| 2242 | 2241 | .type, |
| 2243 | 2242 | @intCast(u32, binary_bytes.items.len - header_offset - header_size), |
| 2244 | | @intCast(u32, self.func_types.items.len), |
| 2243 | @intCast(u32, wasm.func_types.items.len), |
| 2245 | 2244 | ); |
| 2246 | 2245 | section_count += 1; |
| 2247 | 2246 | } |
| 2248 | 2247 | |
| 2249 | 2248 | // Import section |
| 2250 | | const import_memory = self.base.options.import_memory or is_obj; |
| 2251 | | const import_table = self.base.options.import_table or is_obj; |
| 2252 | | if (self.imports.count() != 0 or import_memory or import_table) { |
| 2249 | const import_memory = wasm.base.options.import_memory or is_obj; |
| 2250 | const import_table = wasm.base.options.import_table or is_obj; |
| 2251 | if (wasm.imports.count() != 0 or import_memory or import_table) { |
| 2253 | 2252 | const header_offset = try reserveVecSectionHeader(&binary_bytes); |
| 2254 | 2253 | |
| 2255 | 2254 | // import table is always first table so emit that first |
| 2256 | 2255 | if (import_table) { |
| 2257 | 2256 | const table_imp: types.Import = .{ |
| 2258 | | .module_name = try self.string_table.put(self.base.allocator, self.host_name), |
| 2259 | | .name = try self.string_table.put(self.base.allocator, "__indirect_function_table"), |
| 2257 | .module_name = try wasm.string_table.put(wasm.base.allocator, wasm.host_name), |
| 2258 | .name = try wasm.string_table.put(wasm.base.allocator, "__indirect_function_table"), |
| 2260 | 2259 | .kind = .{ |
| 2261 | 2260 | .table = .{ |
| 2262 | 2261 | .limits = .{ |
| 2263 | | .min = @intCast(u32, self.function_table.count()), |
| 2262 | .min = @intCast(u32, wasm.function_table.count()), |
| 2264 | 2263 | .max = null, |
| 2265 | 2264 | }, |
| 2266 | 2265 | .reftype = .funcref, |
| 2267 | 2266 | }, |
| 2268 | 2267 | }, |
| 2269 | 2268 | }; |
| 2270 | | try self.emitImport(binary_writer, table_imp); |
| 2269 | try wasm.emitImport(binary_writer, table_imp); |
| 2271 | 2270 | } |
| 2272 | 2271 | |
| 2273 | | var it = self.imports.iterator(); |
| 2272 | var it = wasm.imports.iterator(); |
| 2274 | 2273 | while (it.next()) |entry| { |
| 2275 | | assert(entry.key_ptr.*.getSymbol(self).isUndefined()); |
| 2274 | assert(entry.key_ptr.*.getSymbol(wasm).isUndefined()); |
| 2276 | 2275 | const import = entry.value_ptr.*; |
| 2277 | | try self.emitImport(binary_writer, import); |
| 2276 | try wasm.emitImport(binary_writer, import); |
| 2278 | 2277 | } |
| 2279 | 2278 | |
| 2280 | 2279 | if (import_memory) { |
| 2281 | 2280 | const mem_name = if (is_obj) "__linear_memory" else "memory"; |
| 2282 | 2281 | const mem_imp: types.Import = .{ |
| 2283 | | .module_name = try self.string_table.put(self.base.allocator, self.host_name), |
| 2284 | | .name = try self.string_table.put(self.base.allocator, mem_name), |
| 2285 | | .kind = .{ .memory = self.memories.limits }, |
| 2282 | .module_name = try wasm.string_table.put(wasm.base.allocator, wasm.host_name), |
| 2283 | .name = try wasm.string_table.put(wasm.base.allocator, mem_name), |
| 2284 | .kind = .{ .memory = wasm.memories.limits }, |
| 2286 | 2285 | }; |
| 2287 | | try self.emitImport(binary_writer, mem_imp); |
| 2286 | try wasm.emitImport(binary_writer, mem_imp); |
| 2288 | 2287 | } |
| 2289 | 2288 | |
| 2290 | 2289 | try writeVecSectionHeader( |
| ... | ... | @@ -2292,15 +2291,15 @@ pub fn flushModule(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Nod |
| 2292 | 2291 | header_offset, |
| 2293 | 2292 | .import, |
| 2294 | 2293 | @intCast(u32, binary_bytes.items.len - header_offset - header_size), |
| 2295 | | @intCast(u32, self.imports.count() + @boolToInt(import_memory) + @boolToInt(import_table)), |
| 2294 | @intCast(u32, wasm.imports.count() + @boolToInt(import_memory) + @boolToInt(import_table)), |
| 2296 | 2295 | ); |
| 2297 | 2296 | section_count += 1; |
| 2298 | 2297 | } |
| 2299 | 2298 | |
| 2300 | 2299 | // Function section |
| 2301 | | if (self.functions.count() != 0) { |
| 2300 | if (wasm.functions.count() != 0) { |
| 2302 | 2301 | const header_offset = try reserveVecSectionHeader(&binary_bytes); |
| 2303 | | for (self.functions.values()) |function| { |
| 2302 | for (wasm.functions.values()) |function| { |
| 2304 | 2303 | try leb.writeULEB128(binary_writer, function.type_index); |
| 2305 | 2304 | } |
| 2306 | 2305 | |
| ... | ... | @@ -2309,19 +2308,19 @@ pub fn flushModule(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Nod |
| 2309 | 2308 | header_offset, |
| 2310 | 2309 | .function, |
| 2311 | 2310 | @intCast(u32, binary_bytes.items.len - header_offset - header_size), |
| 2312 | | @intCast(u32, self.functions.count()), |
| 2311 | @intCast(u32, wasm.functions.count()), |
| 2313 | 2312 | ); |
| 2314 | 2313 | section_count += 1; |
| 2315 | 2314 | } |
| 2316 | 2315 | |
| 2317 | 2316 | // Table section |
| 2318 | | const export_table = self.base.options.export_table; |
| 2319 | | if (!import_table and self.function_table.count() != 0) { |
| 2317 | const export_table = wasm.base.options.export_table; |
| 2318 | if (!import_table and wasm.function_table.count() != 0) { |
| 2320 | 2319 | const header_offset = try reserveVecSectionHeader(&binary_bytes); |
| 2321 | 2320 | |
| 2322 | | try leb.writeULEB128(binary_writer, wasm.reftype(.funcref)); |
| 2321 | try leb.writeULEB128(binary_writer, std.wasm.reftype(.funcref)); |
| 2323 | 2322 | try emitLimits(binary_writer, .{ |
| 2324 | | .min = @intCast(u32, self.function_table.count()) + 1, |
| 2323 | .min = @intCast(u32, wasm.function_table.count()) + 1, |
| 2325 | 2324 | .max = null, |
| 2326 | 2325 | }); |
| 2327 | 2326 | |
| ... | ... | @@ -2339,7 +2338,7 @@ pub fn flushModule(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Nod |
| 2339 | 2338 | if (!import_memory) { |
| 2340 | 2339 | const header_offset = try reserveVecSectionHeader(&binary_bytes); |
| 2341 | 2340 | |
| 2342 | | try emitLimits(binary_writer, self.memories.limits); |
| 2341 | try emitLimits(binary_writer, wasm.memories.limits); |
| 2343 | 2342 | try writeVecSectionHeader( |
| 2344 | 2343 | binary_bytes.items, |
| 2345 | 2344 | header_offset, |
| ... | ... | @@ -2351,11 +2350,11 @@ pub fn flushModule(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Nod |
| 2351 | 2350 | } |
| 2352 | 2351 | |
| 2353 | 2352 | // Global section (used to emit stack pointer) |
| 2354 | | if (self.wasm_globals.items.len > 0) { |
| 2353 | if (wasm.wasm_globals.items.len > 0) { |
| 2355 | 2354 | const header_offset = try reserveVecSectionHeader(&binary_bytes); |
| 2356 | 2355 | |
| 2357 | | for (self.wasm_globals.items) |global| { |
| 2358 | | try binary_writer.writeByte(wasm.valtype(global.global_type.valtype)); |
| 2356 | for (wasm.wasm_globals.items) |global| { |
| 2357 | try binary_writer.writeByte(std.wasm.valtype(global.global_type.valtype)); |
| 2359 | 2358 | try binary_writer.writeByte(@boolToInt(global.global_type.mutable)); |
| 2360 | 2359 | try emitInit(binary_writer, global.init); |
| 2361 | 2360 | } |
| ... | ... | @@ -2365,17 +2364,17 @@ pub fn flushModule(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Nod |
| 2365 | 2364 | header_offset, |
| 2366 | 2365 | .global, |
| 2367 | 2366 | @intCast(u32, binary_bytes.items.len - header_offset - header_size), |
| 2368 | | @intCast(u32, self.wasm_globals.items.len), |
| 2367 | @intCast(u32, wasm.wasm_globals.items.len), |
| 2369 | 2368 | ); |
| 2370 | 2369 | section_count += 1; |
| 2371 | 2370 | } |
| 2372 | 2371 | |
| 2373 | 2372 | // Export section |
| 2374 | | if (self.exports.items.len != 0 or export_table or !import_memory) { |
| 2373 | if (wasm.exports.items.len != 0 or export_table or !import_memory) { |
| 2375 | 2374 | const header_offset = try reserveVecSectionHeader(&binary_bytes); |
| 2376 | 2375 | |
| 2377 | | for (self.exports.items) |exp| { |
| 2378 | | const name = self.string_table.get(exp.name); |
| 2376 | for (wasm.exports.items) |exp| { |
| 2377 | const name = wasm.string_table.get(exp.name); |
| 2379 | 2378 | try leb.writeULEB128(binary_writer, @intCast(u32, name.len)); |
| 2380 | 2379 | try binary_writer.writeAll(name); |
| 2381 | 2380 | try leb.writeULEB128(binary_writer, @enumToInt(exp.kind)); |
| ... | ... | @@ -2385,14 +2384,14 @@ pub fn flushModule(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Nod |
| 2385 | 2384 | if (export_table) { |
| 2386 | 2385 | try leb.writeULEB128(binary_writer, @intCast(u32, "__indirect_function_table".len)); |
| 2387 | 2386 | try binary_writer.writeAll("__indirect_function_table"); |
| 2388 | | try binary_writer.writeByte(wasm.externalKind(.table)); |
| 2387 | try binary_writer.writeByte(std.wasm.externalKind(.table)); |
| 2389 | 2388 | try leb.writeULEB128(binary_writer, @as(u32, 0)); // function table is always the first table |
| 2390 | 2389 | } |
| 2391 | 2390 | |
| 2392 | 2391 | if (!import_memory) { |
| 2393 | 2392 | try leb.writeULEB128(binary_writer, @intCast(u32, "memory".len)); |
| 2394 | 2393 | try binary_writer.writeAll("memory"); |
| 2395 | | try binary_writer.writeByte(wasm.externalKind(.memory)); |
| 2394 | try binary_writer.writeByte(std.wasm.externalKind(.memory)); |
| 2396 | 2395 | try leb.writeULEB128(binary_writer, @as(u32, 0)); |
| 2397 | 2396 | } |
| 2398 | 2397 | |
| ... | ... | @@ -2401,13 +2400,13 @@ pub fn flushModule(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Nod |
| 2401 | 2400 | header_offset, |
| 2402 | 2401 | .@"export", |
| 2403 | 2402 | @intCast(u32, binary_bytes.items.len - header_offset - header_size), |
| 2404 | | @intCast(u32, self.exports.items.len) + @boolToInt(export_table) + @boolToInt(!import_memory), |
| 2403 | @intCast(u32, wasm.exports.items.len) + @boolToInt(export_table) + @boolToInt(!import_memory), |
| 2405 | 2404 | ); |
| 2406 | 2405 | section_count += 1; |
| 2407 | 2406 | } |
| 2408 | 2407 | |
| 2409 | 2408 | // element section (function table) |
| 2410 | | if (self.function_table.count() > 0) { |
| 2409 | if (wasm.function_table.count() > 0) { |
| 2411 | 2410 | const header_offset = try reserveVecSectionHeader(&binary_bytes); |
| 2412 | 2411 | |
| 2413 | 2412 | var flags: u32 = 0x2; // Yes we have a table |
| ... | ... | @@ -2415,10 +2414,10 @@ pub fn flushModule(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Nod |
| 2415 | 2414 | try leb.writeULEB128(binary_writer, @as(u32, 0)); // index of that table. TODO: Store synthetic symbols |
| 2416 | 2415 | try emitInit(binary_writer, .{ .i32_const = 1 }); // We start at index 1, so unresolved function pointers are invalid |
| 2417 | 2416 | try leb.writeULEB128(binary_writer, @as(u8, 0)); |
| 2418 | | try leb.writeULEB128(binary_writer, @intCast(u32, self.function_table.count())); |
| 2419 | | var symbol_it = self.function_table.keyIterator(); |
| 2417 | try leb.writeULEB128(binary_writer, @intCast(u32, wasm.function_table.count())); |
| 2418 | var symbol_it = wasm.function_table.keyIterator(); |
| 2420 | 2419 | while (symbol_it.next()) |symbol_loc_ptr| { |
| 2421 | | try leb.writeULEB128(binary_writer, symbol_loc_ptr.*.getSymbol(self).index); |
| 2420 | try leb.writeULEB128(binary_writer, symbol_loc_ptr.*.getSymbol(wasm).index); |
| 2422 | 2421 | } |
| 2423 | 2422 | |
| 2424 | 2423 | try writeVecSectionHeader( |
| ... | ... | @@ -2433,17 +2432,17 @@ pub fn flushModule(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Nod |
| 2433 | 2432 | |
| 2434 | 2433 | // Code section |
| 2435 | 2434 | var code_section_size: u32 = 0; |
| 2436 | | if (self.code_section_index) |code_index| { |
| 2435 | if (wasm.code_section_index) |code_index| { |
| 2437 | 2436 | const header_offset = try reserveVecSectionHeader(&binary_bytes); |
| 2438 | | var atom: *Atom = self.atoms.get(code_index).?.getFirst(); |
| 2437 | var atom: *Atom = wasm.atoms.get(code_index).?.getFirst(); |
| 2439 | 2438 | |
| 2440 | 2439 | // The code section must be sorted in line with the function order. |
| 2441 | | var sorted_atoms = try std.ArrayList(*Atom).initCapacity(self.base.allocator, self.functions.count()); |
| 2440 | var sorted_atoms = try std.ArrayList(*Atom).initCapacity(wasm.base.allocator, wasm.functions.count()); |
| 2442 | 2441 | defer sorted_atoms.deinit(); |
| 2443 | 2442 | |
| 2444 | 2443 | while (true) { |
| 2445 | 2444 | if (!is_obj) { |
| 2446 | | atom.resolveRelocs(self); |
| 2445 | atom.resolveRelocs(wasm); |
| 2447 | 2446 | } |
| 2448 | 2447 | sorted_atoms.appendAssumeCapacity(atom); |
| 2449 | 2448 | atom = atom.next orelse break; |
| ... | ... | @@ -2457,7 +2456,7 @@ pub fn flushModule(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Nod |
| 2457 | 2456 | } |
| 2458 | 2457 | }.sort; |
| 2459 | 2458 | |
| 2460 | | std.sort.sort(*Atom, sorted_atoms.items, self, atom_sort_fn); |
| 2459 | std.sort.sort(*Atom, sorted_atoms.items, wasm, atom_sort_fn); |
| 2461 | 2460 | |
| 2462 | 2461 | for (sorted_atoms.items) |sorted_atom| { |
| 2463 | 2462 | try leb.writeULEB128(binary_writer, sorted_atom.size); |
| ... | ... | @@ -2470,17 +2469,17 @@ pub fn flushModule(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Nod |
| 2470 | 2469 | header_offset, |
| 2471 | 2470 | .code, |
| 2472 | 2471 | code_section_size, |
| 2473 | | @intCast(u32, self.functions.count()), |
| 2472 | @intCast(u32, wasm.functions.count()), |
| 2474 | 2473 | ); |
| 2475 | 2474 | code_section_index = section_count; |
| 2476 | 2475 | section_count += 1; |
| 2477 | 2476 | } |
| 2478 | 2477 | |
| 2479 | 2478 | // Data section |
| 2480 | | if (self.data_segments.count() != 0) { |
| 2479 | if (wasm.data_segments.count() != 0) { |
| 2481 | 2480 | const header_offset = try reserveVecSectionHeader(&binary_bytes); |
| 2482 | 2481 | |
| 2483 | | var it = self.data_segments.iterator(); |
| 2482 | var it = wasm.data_segments.iterator(); |
| 2484 | 2483 | var segment_count: u32 = 0; |
| 2485 | 2484 | while (it.next()) |entry| { |
| 2486 | 2485 | // do not output 'bss' section unless we import memory and therefore |
| ... | ... | @@ -2488,8 +2487,8 @@ pub fn flushModule(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Nod |
| 2488 | 2487 | if (!import_memory and std.mem.eql(u8, entry.key_ptr.*, ".bss")) continue; |
| 2489 | 2488 | segment_count += 1; |
| 2490 | 2489 | const atom_index = entry.value_ptr.*; |
| 2491 | | var atom: *Atom = self.atoms.getPtr(atom_index).?.*.getFirst(); |
| 2492 | | const segment = self.segments.items[atom_index]; |
| 2490 | var atom: *Atom = wasm.atoms.getPtr(atom_index).?.*.getFirst(); |
| 2491 | const segment = wasm.segments.items[atom_index]; |
| 2493 | 2492 | |
| 2494 | 2493 | // flag and index to memory section (currently, there can only be 1 memory section in wasm) |
| 2495 | 2494 | try leb.writeULEB128(binary_writer, @as(u32, 0)); |
| ... | ... | @@ -2501,7 +2500,7 @@ pub fn flushModule(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Nod |
| 2501 | 2500 | var current_offset: u32 = 0; |
| 2502 | 2501 | while (true) { |
| 2503 | 2502 | if (!is_obj) { |
| 2504 | | atom.resolveRelocs(self); |
| 2503 | atom.resolveRelocs(wasm); |
| 2505 | 2504 | } |
| 2506 | 2505 | |
| 2507 | 2506 | // Pad with zeroes to ensure all segments are aligned |
| ... | ... | @@ -2546,25 +2545,25 @@ pub fn flushModule(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Nod |
| 2546 | 2545 | // we never store all symbols in a single table, but store a location reference instead. |
| 2547 | 2546 | // This means that for a relocatable object file, we need to generate one and provide it to the relocation sections. |
| 2548 | 2547 | var symbol_table = std.AutoArrayHashMap(SymbolLoc, u32).init(arena); |
| 2549 | | try self.emitLinkSection(&binary_bytes, &symbol_table); |
| 2548 | try wasm.emitLinkSection(&binary_bytes, &symbol_table); |
| 2550 | 2549 | if (code_section_index) |code_index| { |
| 2551 | | try self.emitCodeRelocations(&binary_bytes, code_index, symbol_table); |
| 2550 | try wasm.emitCodeRelocations(&binary_bytes, code_index, symbol_table); |
| 2552 | 2551 | } |
| 2553 | 2552 | if (data_section_index) |data_index| { |
| 2554 | | try self.emitDataRelocations(&binary_bytes, data_index, symbol_table); |
| 2553 | try wasm.emitDataRelocations(&binary_bytes, data_index, symbol_table); |
| 2555 | 2554 | } |
| 2556 | | } else if (!self.base.options.strip) { |
| 2557 | | if (self.dwarf) |*dwarf| { |
| 2558 | | const mod = self.base.options.module.?; |
| 2559 | | try dwarf.writeDbgAbbrev(&self.base); |
| 2555 | } else if (!wasm.base.options.strip) { |
| 2556 | if (wasm.dwarf) |*dwarf| { |
| 2557 | const mod = wasm.base.options.module.?; |
| 2558 | try dwarf.writeDbgAbbrev(&wasm.base); |
| 2560 | 2559 | // for debug info and ranges, the address is always 0, |
| 2561 | 2560 | // as locations are always offsets relative to 'code' section. |
| 2562 | | try dwarf.writeDbgInfoHeader(&self.base, mod, 0, code_section_size); |
| 2563 | | try dwarf.writeDbgAranges(&self.base, 0, code_section_size); |
| 2564 | | try dwarf.writeDbgLineHeader(&self.base, mod); |
| 2561 | try dwarf.writeDbgInfoHeader(&wasm.base, mod, 0, code_section_size); |
| 2562 | try dwarf.writeDbgAranges(&wasm.base, 0, code_section_size); |
| 2563 | try dwarf.writeDbgLineHeader(&wasm.base, mod); |
| 2565 | 2564 | } |
| 2566 | 2565 | |
| 2567 | | var debug_bytes = std.ArrayList(u8).init(self.base.allocator); |
| 2566 | var debug_bytes = std.ArrayList(u8).init(wasm.base.allocator); |
| 2568 | 2567 | defer debug_bytes.deinit(); |
| 2569 | 2568 | |
| 2570 | 2569 | const DebugSection = struct { |
| ... | ... | @@ -2573,21 +2572,21 @@ pub fn flushModule(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Nod |
| 2573 | 2572 | }; |
| 2574 | 2573 | |
| 2575 | 2574 | const debug_sections: []const DebugSection = &.{ |
| 2576 | | .{ .name = ".debug_info", .index = self.debug_info_index }, |
| 2577 | | .{ .name = ".debug_pubtypes", .index = self.debug_pubtypes_index }, |
| 2578 | | .{ .name = ".debug_abbrev", .index = self.debug_abbrev_index }, |
| 2579 | | .{ .name = ".debug_line", .index = self.debug_line_index }, |
| 2580 | | .{ .name = ".debug_str", .index = self.debug_str_index }, |
| 2581 | | .{ .name = ".debug_pubnames", .index = self.debug_pubnames_index }, |
| 2582 | | .{ .name = ".debug_loc", .index = self.debug_loc_index }, |
| 2583 | | .{ .name = ".debug_ranges", .index = self.debug_ranges_index }, |
| 2575 | .{ .name = ".debug_info", .index = wasm.debug_info_index }, |
| 2576 | .{ .name = ".debug_pubtypes", .index = wasm.debug_pubtypes_index }, |
| 2577 | .{ .name = ".debug_abbrev", .index = wasm.debug_abbrev_index }, |
| 2578 | .{ .name = ".debug_line", .index = wasm.debug_line_index }, |
| 2579 | .{ .name = ".debug_str", .index = wasm.debug_str_index }, |
| 2580 | .{ .name = ".debug_pubnames", .index = wasm.debug_pubnames_index }, |
| 2581 | .{ .name = ".debug_loc", .index = wasm.debug_loc_index }, |
| 2582 | .{ .name = ".debug_ranges", .index = wasm.debug_ranges_index }, |
| 2584 | 2583 | }; |
| 2585 | 2584 | |
| 2586 | 2585 | for (debug_sections) |item| { |
| 2587 | 2586 | if (item.index) |index| { |
| 2588 | | var atom = self.atoms.get(index).?.getFirst(); |
| 2587 | var atom = wasm.atoms.get(index).?.getFirst(); |
| 2589 | 2588 | while (true) { |
| 2590 | | atom.resolveRelocs(self); |
| 2589 | atom.resolveRelocs(wasm); |
| 2591 | 2590 | try debug_bytes.appendSlice(atom.code.items); |
| 2592 | 2591 | atom = atom.next orelse break; |
| 2593 | 2592 | } |
| ... | ... | @@ -2595,20 +2594,20 @@ pub fn flushModule(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Nod |
| 2595 | 2594 | debug_bytes.clearRetainingCapacity(); |
| 2596 | 2595 | } |
| 2597 | 2596 | } |
| 2598 | | try self.emitNameSection(&binary_bytes, arena); |
| 2597 | try wasm.emitNameSection(&binary_bytes, arena); |
| 2599 | 2598 | } |
| 2600 | 2599 | |
| 2601 | 2600 | // Only when writing all sections executed properly we write the magic |
| 2602 | 2601 | // bytes. This allows us to easily detect what went wrong while generating |
| 2603 | 2602 | // the final binary. |
| 2604 | | mem.copy(u8, binary_bytes.items, &(wasm.magic ++ wasm.version)); |
| 2603 | mem.copy(u8, binary_bytes.items, &(std.wasm.magic ++ std.wasm.version)); |
| 2605 | 2604 | |
| 2606 | 2605 | // finally, write the entire binary into the file. |
| 2607 | 2606 | var iovec = [_]std.os.iovec_const{.{ |
| 2608 | 2607 | .iov_base = binary_bytes.items.ptr, |
| 2609 | 2608 | .iov_len = binary_bytes.items.len, |
| 2610 | 2609 | }}; |
| 2611 | | try self.base.file.?.writevAll(&iovec); |
| 2610 | try wasm.base.file.?.writevAll(&iovec); |
| 2612 | 2611 | } |
| 2613 | 2612 | |
| 2614 | 2613 | fn emitDebugSection(binary_bytes: *std.ArrayList(u8), data: []const u8, name: []const u8) !void { |
| ... | ... | @@ -2629,7 +2628,7 @@ fn emitDebugSection(binary_bytes: *std.ArrayList(u8), data: []const u8, name: [] |
| 2629 | 2628 | ); |
| 2630 | 2629 | } |
| 2631 | 2630 | |
| 2632 | | fn emitNameSection(self: *Wasm, binary_bytes: *std.ArrayList(u8), arena: std.mem.Allocator) !void { |
| 2631 | fn emitNameSection(wasm: *Wasm, binary_bytes: *std.ArrayList(u8), arena: std.mem.Allocator) !void { |
| 2633 | 2632 | const Name = struct { |
| 2634 | 2633 | index: u32, |
| 2635 | 2634 | name: []const u8, |
| ... | ... | @@ -2642,15 +2641,15 @@ fn emitNameSection(self: *Wasm, binary_bytes: *std.ArrayList(u8), arena: std.mem |
| 2642 | 2641 | |
| 2643 | 2642 | // we must de-duplicate symbols that point to the same function |
| 2644 | 2643 | var funcs = std.AutoArrayHashMap(u32, Name).init(arena); |
| 2645 | | try funcs.ensureUnusedCapacity(self.functions.count() + self.imported_functions_count); |
| 2646 | | var globals = try std.ArrayList(Name).initCapacity(arena, self.wasm_globals.items.len + self.imported_globals_count); |
| 2647 | | var segments = try std.ArrayList(Name).initCapacity(arena, self.data_segments.count()); |
| 2644 | try funcs.ensureUnusedCapacity(wasm.functions.count() + wasm.imported_functions_count); |
| 2645 | var globals = try std.ArrayList(Name).initCapacity(arena, wasm.wasm_globals.items.len + wasm.imported_globals_count); |
| 2646 | var segments = try std.ArrayList(Name).initCapacity(arena, wasm.data_segments.count()); |
| 2648 | 2647 | |
| 2649 | | for (self.resolved_symbols.keys()) |sym_loc| { |
| 2650 | | const symbol = sym_loc.getSymbol(self).*; |
| 2648 | for (wasm.resolved_symbols.keys()) |sym_loc| { |
| 2649 | const symbol = sym_loc.getSymbol(wasm).*; |
| 2651 | 2650 | const name = if (symbol.isUndefined()) blk: { |
| 2652 | | break :blk self.string_table.get(self.imports.get(sym_loc).?.name); |
| 2653 | | } else sym_loc.getName(self); |
| 2651 | break :blk wasm.string_table.get(wasm.imports.get(sym_loc).?.name); |
| 2652 | } else sym_loc.getName(wasm); |
| 2654 | 2653 | switch (symbol.tag) { |
| 2655 | 2654 | .function => { |
| 2656 | 2655 | const gop = funcs.getOrPutAssumeCapacity(symbol.index); |
| ... | ... | @@ -2664,10 +2663,10 @@ fn emitNameSection(self: *Wasm, binary_bytes: *std.ArrayList(u8), arena: std.mem |
| 2664 | 2663 | } |
| 2665 | 2664 | // data segments are already 'ordered' |
| 2666 | 2665 | var data_segment_index: u32 = 0; |
| 2667 | | for (self.data_segments.keys()) |key| { |
| 2666 | for (wasm.data_segments.keys()) |key| { |
| 2668 | 2667 | // bss section is not emitted when this condition holds true, so we also |
| 2669 | 2668 | // do not output a name for it. |
| 2670 | | if (!self.base.options.import_memory and std.mem.eql(u8, key, ".bss")) continue; |
| 2669 | if (!wasm.base.options.import_memory and std.mem.eql(u8, key, ".bss")) continue; |
| 2671 | 2670 | segments.appendAssumeCapacity(.{ .index = data_segment_index, .name = key }); |
| 2672 | 2671 | data_segment_index += 1; |
| 2673 | 2672 | } |
| ... | ... | @@ -2680,9 +2679,9 @@ fn emitNameSection(self: *Wasm, binary_bytes: *std.ArrayList(u8), arena: std.mem |
| 2680 | 2679 | try leb.writeULEB128(writer, @intCast(u32, "name".len)); |
| 2681 | 2680 | try writer.writeAll("name"); |
| 2682 | 2681 | |
| 2683 | | try self.emitNameSubsection(.function, funcs.values(), writer); |
| 2684 | | try self.emitNameSubsection(.global, globals.items, writer); |
| 2685 | | try self.emitNameSubsection(.data_segment, segments.items, writer); |
| 2682 | try wasm.emitNameSubsection(.function, funcs.values(), writer); |
| 2683 | try wasm.emitNameSubsection(.global, globals.items, writer); |
| 2684 | try wasm.emitNameSubsection(.data_segment, segments.items, writer); |
| 2686 | 2685 | |
| 2687 | 2686 | try writeCustomSectionHeader( |
| 2688 | 2687 | binary_bytes.items, |
| ... | ... | @@ -2691,9 +2690,9 @@ fn emitNameSection(self: *Wasm, binary_bytes: *std.ArrayList(u8), arena: std.mem |
| 2691 | 2690 | ); |
| 2692 | 2691 | } |
| 2693 | 2692 | |
| 2694 | | fn emitNameSubsection(self: *Wasm, section_id: std.wasm.NameSubsection, names: anytype, writer: anytype) !void { |
| 2693 | fn emitNameSubsection(wasm: *Wasm, section_id: std.wasm.NameSubsection, names: anytype, writer: anytype) !void { |
| 2695 | 2694 | // We must emit subsection size, so first write to a temporary list |
| 2696 | | var section_list = std.ArrayList(u8).init(self.base.allocator); |
| 2695 | var section_list = std.ArrayList(u8).init(wasm.base.allocator); |
| 2697 | 2696 | defer section_list.deinit(); |
| 2698 | 2697 | const sub_writer = section_list.writer(); |
| 2699 | 2698 | |
| ... | ... | @@ -2711,7 +2710,7 @@ fn emitNameSubsection(self: *Wasm, section_id: std.wasm.NameSubsection, names: a |
| 2711 | 2710 | try writer.writeAll(section_list.items); |
| 2712 | 2711 | } |
| 2713 | 2712 | |
| 2714 | | fn emitLimits(writer: anytype, limits: wasm.Limits) !void { |
| 2713 | fn emitLimits(writer: anytype, limits: std.wasm.Limits) !void { |
| 2715 | 2714 | try leb.writeULEB128(writer, @boolToInt(limits.max != null)); |
| 2716 | 2715 | try leb.writeULEB128(writer, limits.min); |
| 2717 | 2716 | if (limits.max) |max| { |
| ... | ... | @@ -2719,38 +2718,38 @@ fn emitLimits(writer: anytype, limits: wasm.Limits) !void { |
| 2719 | 2718 | } |
| 2720 | 2719 | } |
| 2721 | 2720 | |
| 2722 | | fn emitInit(writer: anytype, init_expr: wasm.InitExpression) !void { |
| 2721 | fn emitInit(writer: anytype, init_expr: std.wasm.InitExpression) !void { |
| 2723 | 2722 | switch (init_expr) { |
| 2724 | 2723 | .i32_const => |val| { |
| 2725 | | try writer.writeByte(wasm.opcode(.i32_const)); |
| 2724 | try writer.writeByte(std.wasm.opcode(.i32_const)); |
| 2726 | 2725 | try leb.writeILEB128(writer, val); |
| 2727 | 2726 | }, |
| 2728 | 2727 | .i64_const => |val| { |
| 2729 | | try writer.writeByte(wasm.opcode(.i64_const)); |
| 2728 | try writer.writeByte(std.wasm.opcode(.i64_const)); |
| 2730 | 2729 | try leb.writeILEB128(writer, val); |
| 2731 | 2730 | }, |
| 2732 | 2731 | .f32_const => |val| { |
| 2733 | | try writer.writeByte(wasm.opcode(.f32_const)); |
| 2732 | try writer.writeByte(std.wasm.opcode(.f32_const)); |
| 2734 | 2733 | try writer.writeIntLittle(u32, @bitCast(u32, val)); |
| 2735 | 2734 | }, |
| 2736 | 2735 | .f64_const => |val| { |
| 2737 | | try writer.writeByte(wasm.opcode(.f64_const)); |
| 2736 | try writer.writeByte(std.wasm.opcode(.f64_const)); |
| 2738 | 2737 | try writer.writeIntLittle(u64, @bitCast(u64, val)); |
| 2739 | 2738 | }, |
| 2740 | 2739 | .global_get => |val| { |
| 2741 | | try writer.writeByte(wasm.opcode(.global_get)); |
| 2740 | try writer.writeByte(std.wasm.opcode(.global_get)); |
| 2742 | 2741 | try leb.writeULEB128(writer, val); |
| 2743 | 2742 | }, |
| 2744 | 2743 | } |
| 2745 | | try writer.writeByte(wasm.opcode(.end)); |
| 2744 | try writer.writeByte(std.wasm.opcode(.end)); |
| 2746 | 2745 | } |
| 2747 | 2746 | |
| 2748 | | fn emitImport(self: *Wasm, writer: anytype, import: types.Import) !void { |
| 2749 | | const module_name = self.string_table.get(import.module_name); |
| 2747 | fn emitImport(wasm: *Wasm, writer: anytype, import: types.Import) !void { |
| 2748 | const module_name = wasm.string_table.get(import.module_name); |
| 2750 | 2749 | try leb.writeULEB128(writer, @intCast(u32, module_name.len)); |
| 2751 | 2750 | try writer.writeAll(module_name); |
| 2752 | 2751 | |
| 2753 | | const name = self.string_table.get(import.name); |
| 2752 | const name = wasm.string_table.get(import.name); |
| 2754 | 2753 | try leb.writeULEB128(writer, @intCast(u32, name.len)); |
| 2755 | 2754 | try writer.writeAll(name); |
| 2756 | 2755 | |
| ... | ... | @@ -2758,11 +2757,11 @@ fn emitImport(self: *Wasm, writer: anytype, import: types.Import) !void { |
| 2758 | 2757 | switch (import.kind) { |
| 2759 | 2758 | .function => |type_index| try leb.writeULEB128(writer, type_index), |
| 2760 | 2759 | .global => |global_type| { |
| 2761 | | try leb.writeULEB128(writer, wasm.valtype(global_type.valtype)); |
| 2760 | try leb.writeULEB128(writer, std.wasm.valtype(global_type.valtype)); |
| 2762 | 2761 | try writer.writeByte(@boolToInt(global_type.mutable)); |
| 2763 | 2762 | }, |
| 2764 | 2763 | .table => |table| { |
| 2765 | | try leb.writeULEB128(writer, wasm.reftype(table.reftype)); |
| 2764 | try leb.writeULEB128(writer, std.wasm.reftype(table.reftype)); |
| 2766 | 2765 | try emitLimits(writer, table.limits); |
| 2767 | 2766 | }, |
| 2768 | 2767 | .memory => |limits| { |
| ... | ... | @@ -2771,28 +2770,28 @@ fn emitImport(self: *Wasm, writer: anytype, import: types.Import) !void { |
| 2771 | 2770 | } |
| 2772 | 2771 | } |
| 2773 | 2772 | |
| 2774 | | fn linkWithLLD(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) !void { |
| 2773 | fn linkWithLLD(wasm: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) !void { |
| 2775 | 2774 | const tracy = trace(@src()); |
| 2776 | 2775 | defer tracy.end(); |
| 2777 | 2776 | |
| 2778 | | var arena_allocator = std.heap.ArenaAllocator.init(self.base.allocator); |
| 2777 | var arena_allocator = std.heap.ArenaAllocator.init(wasm.base.allocator); |
| 2779 | 2778 | defer arena_allocator.deinit(); |
| 2780 | 2779 | const arena = arena_allocator.allocator(); |
| 2781 | 2780 | |
| 2782 | | const directory = self.base.options.emit.?.directory; // Just an alias to make it shorter to type. |
| 2783 | | const full_out_path = try directory.join(arena, &[_][]const u8{self.base.options.emit.?.sub_path}); |
| 2781 | const directory = wasm.base.options.emit.?.directory; // Just an alias to make it shorter to type. |
| 2782 | const full_out_path = try directory.join(arena, &[_][]const u8{wasm.base.options.emit.?.sub_path}); |
| 2784 | 2783 | |
| 2785 | 2784 | // If there is no Zig code to compile, then we should skip flushing the output file because it |
| 2786 | 2785 | // will not be part of the linker line anyway. |
| 2787 | | const module_obj_path: ?[]const u8 = if (self.base.options.module) |mod| blk: { |
| 2788 | | const use_stage1 = build_options.have_stage1 and self.base.options.use_stage1; |
| 2786 | const module_obj_path: ?[]const u8 = if (wasm.base.options.module) |mod| blk: { |
| 2787 | const use_stage1 = build_options.have_stage1 and wasm.base.options.use_stage1; |
| 2789 | 2788 | if (use_stage1) { |
| 2790 | 2789 | const obj_basename = try std.zig.binNameAlloc(arena, .{ |
| 2791 | | .root_name = self.base.options.root_name, |
| 2792 | | .target = self.base.options.target, |
| 2790 | .root_name = wasm.base.options.root_name, |
| 2791 | .target = wasm.base.options.target, |
| 2793 | 2792 | .output_mode = .Obj, |
| 2794 | 2793 | }); |
| 2795 | | switch (self.base.options.cache_mode) { |
| 2794 | switch (wasm.base.options.cache_mode) { |
| 2796 | 2795 | .incremental => break :blk try mod.zig_cache_artifact_directory.join( |
| 2797 | 2796 | arena, |
| 2798 | 2797 | &[_][]const u8{obj_basename}, |
| ... | ... | @@ -2803,12 +2802,12 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) ! |
| 2803 | 2802 | } |
| 2804 | 2803 | } |
| 2805 | 2804 | |
| 2806 | | try self.flushModule(comp, prog_node); |
| 2805 | try wasm.flushModule(comp, prog_node); |
| 2807 | 2806 | |
| 2808 | 2807 | if (fs.path.dirname(full_out_path)) |dirname| { |
| 2809 | | break :blk try fs.path.join(arena, &.{ dirname, self.base.intermediary_basename.? }); |
| 2808 | break :blk try fs.path.join(arena, &.{ dirname, wasm.base.intermediary_basename.? }); |
| 2810 | 2809 | } else { |
| 2811 | | break :blk self.base.intermediary_basename.?; |
| 2810 | break :blk wasm.base.intermediary_basename.?; |
| 2812 | 2811 | } |
| 2813 | 2812 | } else null; |
| 2814 | 2813 | |
| ... | ... | @@ -2817,31 +2816,31 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) ! |
| 2817 | 2816 | sub_prog_node.context.refresh(); |
| 2818 | 2817 | defer sub_prog_node.end(); |
| 2819 | 2818 | |
| 2820 | | const is_obj = self.base.options.output_mode == .Obj; |
| 2819 | const is_obj = wasm.base.options.output_mode == .Obj; |
| 2821 | 2820 | |
| 2822 | | const compiler_rt_path: ?[]const u8 = if (self.base.options.include_compiler_rt and !is_obj) |
| 2821 | const compiler_rt_path: ?[]const u8 = if (wasm.base.options.include_compiler_rt and !is_obj) |
| 2823 | 2822 | comp.compiler_rt_lib.?.full_object_path |
| 2824 | 2823 | else |
| 2825 | 2824 | null; |
| 2826 | 2825 | |
| 2827 | | const target = self.base.options.target; |
| 2826 | const target = wasm.base.options.target; |
| 2828 | 2827 | |
| 2829 | 2828 | const id_symlink_basename = "lld.id"; |
| 2830 | 2829 | |
| 2831 | 2830 | var man: Cache.Manifest = undefined; |
| 2832 | | defer if (!self.base.options.disable_lld_caching) man.deinit(); |
| 2831 | defer if (!wasm.base.options.disable_lld_caching) man.deinit(); |
| 2833 | 2832 | |
| 2834 | 2833 | var digest: [Cache.hex_digest_len]u8 = undefined; |
| 2835 | 2834 | |
| 2836 | | if (!self.base.options.disable_lld_caching) { |
| 2835 | if (!wasm.base.options.disable_lld_caching) { |
| 2837 | 2836 | man = comp.cache_parent.obtain(); |
| 2838 | 2837 | |
| 2839 | 2838 | // We are about to obtain this lock, so here we give other processes a chance first. |
| 2840 | | self.base.releaseLock(); |
| 2839 | wasm.base.releaseLock(); |
| 2841 | 2840 | |
| 2842 | 2841 | comptime assert(Compilation.link_hash_implementation_version == 7); |
| 2843 | 2842 | |
| 2844 | | for (self.base.options.objects) |obj| { |
| 2843 | for (wasm.base.options.objects) |obj| { |
| 2845 | 2844 | _ = try man.addFile(obj.path, null); |
| 2846 | 2845 | man.hash.add(obj.must_link); |
| 2847 | 2846 | } |
| ... | ... | @@ -2850,18 +2849,18 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) ! |
| 2850 | 2849 | } |
| 2851 | 2850 | try man.addOptionalFile(module_obj_path); |
| 2852 | 2851 | try man.addOptionalFile(compiler_rt_path); |
| 2853 | | man.hash.addOptionalBytes(self.base.options.entry); |
| 2854 | | man.hash.addOptional(self.base.options.stack_size_override); |
| 2855 | | man.hash.add(self.base.options.import_memory); |
| 2856 | | man.hash.add(self.base.options.import_table); |
| 2857 | | man.hash.add(self.base.options.export_table); |
| 2858 | | man.hash.addOptional(self.base.options.initial_memory); |
| 2859 | | man.hash.addOptional(self.base.options.max_memory); |
| 2860 | | man.hash.add(self.base.options.shared_memory); |
| 2861 | | man.hash.addOptional(self.base.options.global_base); |
| 2862 | | man.hash.add(self.base.options.export_symbol_names.len); |
| 2852 | man.hash.addOptionalBytes(wasm.base.options.entry); |
| 2853 | man.hash.addOptional(wasm.base.options.stack_size_override); |
| 2854 | man.hash.add(wasm.base.options.import_memory); |
| 2855 | man.hash.add(wasm.base.options.import_table); |
| 2856 | man.hash.add(wasm.base.options.export_table); |
| 2857 | man.hash.addOptional(wasm.base.options.initial_memory); |
| 2858 | man.hash.addOptional(wasm.base.options.max_memory); |
| 2859 | man.hash.add(wasm.base.options.shared_memory); |
| 2860 | man.hash.addOptional(wasm.base.options.global_base); |
| 2861 | man.hash.add(wasm.base.options.export_symbol_names.len); |
| 2863 | 2862 | // strip does not need to go into the linker hash because it is part of the hash namespace |
| 2864 | | for (self.base.options.export_symbol_names) |symbol_name| { |
| 2863 | for (wasm.base.options.export_symbol_names) |symbol_name| { |
| 2865 | 2864 | man.hash.addBytes(symbol_name); |
| 2866 | 2865 | } |
| 2867 | 2866 | |
| ... | ... | @@ -2882,7 +2881,7 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) ! |
| 2882 | 2881 | if (mem.eql(u8, prev_digest, &digest)) { |
| 2883 | 2882 | log.debug("WASM LLD digest={s} match - skipping invocation", .{std.fmt.fmtSliceHexLower(&digest)}); |
| 2884 | 2883 | // Hot diggity dog! The output binary is already there. |
| 2885 | | self.base.lock = man.toOwnedLock(); |
| 2884 | wasm.base.lock = man.toOwnedLock(); |
| 2886 | 2885 | return; |
| 2887 | 2886 | } |
| 2888 | 2887 | log.debug("WASM LLD prev_digest={s} new_digest={s}", .{ std.fmt.fmtSliceHexLower(prev_digest), std.fmt.fmtSliceHexLower(&digest) }); |
| ... | ... | @@ -2899,8 +2898,8 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) ! |
| 2899 | 2898 | // here. TODO: think carefully about how we can avoid this redundant operation when doing |
| 2900 | 2899 | // build-obj. See also the corresponding TODO in linkAsArchive. |
| 2901 | 2900 | const the_object_path = blk: { |
| 2902 | | if (self.base.options.objects.len != 0) |
| 2903 | | break :blk self.base.options.objects[0].path; |
| 2901 | if (wasm.base.options.objects.len != 0) |
| 2902 | break :blk wasm.base.options.objects[0].path; |
| 2904 | 2903 | |
| 2905 | 2904 | if (comp.c_object_table.count() != 0) |
| 2906 | 2905 | break :blk comp.c_object_table.keys()[0].status.success.object_path; |
| ... | ... | @@ -2919,7 +2918,7 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) ! |
| 2919 | 2918 | } |
| 2920 | 2919 | } else { |
| 2921 | 2920 | // Create an LLD command line and invoke it. |
| 2922 | | var argv = std.ArrayList([]const u8).init(self.base.allocator); |
| 2921 | var argv = std.ArrayList([]const u8).init(wasm.base.allocator); |
| 2923 | 2922 | defer argv.deinit(); |
| 2924 | 2923 | // We will invoke ourselves as a child process to gain access to LLD. |
| 2925 | 2924 | // This is necessary because LLD does not behave properly as a library - |
| ... | ... | @@ -2927,47 +2926,47 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) ! |
| 2927 | 2926 | try argv.appendSlice(&[_][]const u8{ comp.self_exe_path.?, "wasm-ld" }); |
| 2928 | 2927 | try argv.append("-error-limit=0"); |
| 2929 | 2928 | |
| 2930 | | if (self.base.options.lto) { |
| 2931 | | switch (self.base.options.optimize_mode) { |
| 2929 | if (wasm.base.options.lto) { |
| 2930 | switch (wasm.base.options.optimize_mode) { |
| 2932 | 2931 | .Debug => {}, |
| 2933 | 2932 | .ReleaseSmall => try argv.append("-O2"), |
| 2934 | 2933 | .ReleaseFast, .ReleaseSafe => try argv.append("-O3"), |
| 2935 | 2934 | } |
| 2936 | 2935 | } |
| 2937 | 2936 | |
| 2938 | | if (self.base.options.import_memory) { |
| 2937 | if (wasm.base.options.import_memory) { |
| 2939 | 2938 | try argv.append("--import-memory"); |
| 2940 | 2939 | } |
| 2941 | 2940 | |
| 2942 | | if (self.base.options.import_table) { |
| 2943 | | assert(!self.base.options.export_table); |
| 2941 | if (wasm.base.options.import_table) { |
| 2942 | assert(!wasm.base.options.export_table); |
| 2944 | 2943 | try argv.append("--import-table"); |
| 2945 | 2944 | } |
| 2946 | 2945 | |
| 2947 | | if (self.base.options.export_table) { |
| 2948 | | assert(!self.base.options.import_table); |
| 2946 | if (wasm.base.options.export_table) { |
| 2947 | assert(!wasm.base.options.import_table); |
| 2949 | 2948 | try argv.append("--export-table"); |
| 2950 | 2949 | } |
| 2951 | 2950 | |
| 2952 | | if (self.base.options.strip) { |
| 2951 | if (wasm.base.options.strip) { |
| 2953 | 2952 | try argv.append("-s"); |
| 2954 | 2953 | } |
| 2955 | 2954 | |
| 2956 | | if (self.base.options.initial_memory) |initial_memory| { |
| 2955 | if (wasm.base.options.initial_memory) |initial_memory| { |
| 2957 | 2956 | const arg = try std.fmt.allocPrint(arena, "--initial-memory={d}", .{initial_memory}); |
| 2958 | 2957 | try argv.append(arg); |
| 2959 | 2958 | } |
| 2960 | 2959 | |
| 2961 | | if (self.base.options.max_memory) |max_memory| { |
| 2960 | if (wasm.base.options.max_memory) |max_memory| { |
| 2962 | 2961 | const arg = try std.fmt.allocPrint(arena, "--max-memory={d}", .{max_memory}); |
| 2963 | 2962 | try argv.append(arg); |
| 2964 | 2963 | } |
| 2965 | 2964 | |
| 2966 | | if (self.base.options.shared_memory) { |
| 2965 | if (wasm.base.options.shared_memory) { |
| 2967 | 2966 | try argv.append("--shared-memory"); |
| 2968 | 2967 | } |
| 2969 | 2968 | |
| 2970 | | if (self.base.options.global_base) |global_base| { |
| 2969 | if (wasm.base.options.global_base) |global_base| { |
| 2971 | 2970 | const arg = try std.fmt.allocPrint(arena, "--global-base={d}", .{global_base}); |
| 2972 | 2971 | try argv.append(arg); |
| 2973 | 2972 | } else { |
| ... | ... | @@ -2980,29 +2979,29 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) ! |
| 2980 | 2979 | |
| 2981 | 2980 | var auto_export_symbols = true; |
| 2982 | 2981 | // Users are allowed to specify which symbols they want to export to the wasm host. |
| 2983 | | for (self.base.options.export_symbol_names) |symbol_name| { |
| 2982 | for (wasm.base.options.export_symbol_names) |symbol_name| { |
| 2984 | 2983 | const arg = try std.fmt.allocPrint(arena, "--export={s}", .{symbol_name}); |
| 2985 | 2984 | try argv.append(arg); |
| 2986 | 2985 | auto_export_symbols = false; |
| 2987 | 2986 | } |
| 2988 | 2987 | |
| 2989 | | if (self.base.options.rdynamic) { |
| 2988 | if (wasm.base.options.rdynamic) { |
| 2990 | 2989 | try argv.append("--export-dynamic"); |
| 2991 | 2990 | auto_export_symbols = false; |
| 2992 | 2991 | } |
| 2993 | 2992 | |
| 2994 | 2993 | if (auto_export_symbols) { |
| 2995 | | if (self.base.options.module) |mod| { |
| 2994 | if (wasm.base.options.module) |mod| { |
| 2996 | 2995 | // when we use stage1, we use the exports that stage1 provided us. |
| 2997 | 2996 | // For stage2, we can directly retrieve them from the module. |
| 2998 | | const use_stage1 = build_options.have_stage1 and self.base.options.use_stage1; |
| 2997 | const use_stage1 = build_options.have_stage1 and wasm.base.options.use_stage1; |
| 2999 | 2998 | if (use_stage1) { |
| 3000 | 2999 | for (comp.export_symbol_names.items) |symbol_name| { |
| 3001 | 3000 | try argv.append(try std.fmt.allocPrint(arena, "--export={s}", .{symbol_name})); |
| 3002 | 3001 | } |
| 3003 | 3002 | } else { |
| 3004 | 3003 | const skip_export_non_fn = target.os.tag == .wasi and |
| 3005 | | self.base.options.wasi_exec_model == .command; |
| 3004 | wasm.base.options.wasi_exec_model == .command; |
| 3006 | 3005 | for (mod.decl_exports.values()) |exports| { |
| 3007 | 3006 | for (exports) |exprt| { |
| 3008 | 3007 | const exported_decl = mod.declPtr(exprt.exported_decl); |
| ... | ... | @@ -3020,7 +3019,7 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) ! |
| 3020 | 3019 | } |
| 3021 | 3020 | } |
| 3022 | 3021 | |
| 3023 | | if (self.base.options.entry) |entry| { |
| 3022 | if (wasm.base.options.entry) |entry| { |
| 3024 | 3023 | try argv.append("--entry"); |
| 3025 | 3024 | try argv.append(entry); |
| 3026 | 3025 | } |
| ... | ... | @@ -3028,16 +3027,16 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) ! |
| 3028 | 3027 | // Increase the default stack size to a more reasonable value of 1MB instead of |
| 3029 | 3028 | // the default of 1 Wasm page being 64KB, unless overridden by the user. |
| 3030 | 3029 | try argv.append("-z"); |
| 3031 | | const stack_size = self.base.options.stack_size_override orelse wasm.page_size * 16; |
| 3030 | const stack_size = wasm.base.options.stack_size_override orelse std.wasm.page_size * 16; |
| 3032 | 3031 | const arg = try std.fmt.allocPrint(arena, "stack-size={d}", .{stack_size}); |
| 3033 | 3032 | try argv.append(arg); |
| 3034 | 3033 | |
| 3035 | | if (self.base.options.output_mode == .Exe) { |
| 3036 | | if (self.base.options.wasi_exec_model == .reactor) { |
| 3034 | if (wasm.base.options.output_mode == .Exe) { |
| 3035 | if (wasm.base.options.wasi_exec_model == .reactor) { |
| 3037 | 3036 | // Reactor execution model does not have _start so lld doesn't look for it. |
| 3038 | 3037 | try argv.append("--no-entry"); |
| 3039 | 3038 | } |
| 3040 | | } else if (self.base.options.entry == null) { |
| 3039 | } else if (wasm.base.options.entry == null) { |
| 3041 | 3040 | try argv.append("--no-entry"); // So lld doesn't look for _start. |
| 3042 | 3041 | } |
| 3043 | 3042 | try argv.appendSlice(&[_][]const u8{ |
| ... | ... | @@ -3051,10 +3050,10 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) ! |
| 3051 | 3050 | } |
| 3052 | 3051 | |
| 3053 | 3052 | if (target.os.tag == .wasi) { |
| 3054 | | const is_exe_or_dyn_lib = self.base.options.output_mode == .Exe or |
| 3055 | | (self.base.options.output_mode == .Lib and self.base.options.link_mode == .Dynamic); |
| 3053 | const is_exe_or_dyn_lib = wasm.base.options.output_mode == .Exe or |
| 3054 | (wasm.base.options.output_mode == .Lib and wasm.base.options.link_mode == .Dynamic); |
| 3056 | 3055 | if (is_exe_or_dyn_lib) { |
| 3057 | | const wasi_emulated_libs = self.base.options.wasi_emulated_libs; |
| 3056 | const wasi_emulated_libs = wasm.base.options.wasi_emulated_libs; |
| 3058 | 3057 | for (wasi_emulated_libs) |crt_file| { |
| 3059 | 3058 | try argv.append(try comp.get_libc_crt_file( |
| 3060 | 3059 | arena, |
| ... | ... | @@ -3062,15 +3061,15 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) ! |
| 3062 | 3061 | )); |
| 3063 | 3062 | } |
| 3064 | 3063 | |
| 3065 | | if (self.base.options.link_libc) { |
| 3064 | if (wasm.base.options.link_libc) { |
| 3066 | 3065 | try argv.append(try comp.get_libc_crt_file( |
| 3067 | 3066 | arena, |
| 3068 | | wasi_libc.execModelCrtFileFullName(self.base.options.wasi_exec_model), |
| 3067 | wasi_libc.execModelCrtFileFullName(wasm.base.options.wasi_exec_model), |
| 3069 | 3068 | )); |
| 3070 | 3069 | try argv.append(try comp.get_libc_crt_file(arena, "libc.a")); |
| 3071 | 3070 | } |
| 3072 | 3071 | |
| 3073 | | if (self.base.options.link_libcpp) { |
| 3072 | if (wasm.base.options.link_libcpp) { |
| 3074 | 3073 | try argv.append(comp.libcxx_static_lib.?.full_object_path); |
| 3075 | 3074 | try argv.append(comp.libcxxabi_static_lib.?.full_object_path); |
| 3076 | 3075 | } |
| ... | ... | @@ -3079,7 +3078,7 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) ! |
| 3079 | 3078 | |
| 3080 | 3079 | // Positional arguments to the linker such as object files. |
| 3081 | 3080 | var whole_archive = false; |
| 3082 | | for (self.base.options.objects) |obj| { |
| 3081 | for (wasm.base.options.objects) |obj| { |
| 3083 | 3082 | if (obj.must_link and !whole_archive) { |
| 3084 | 3083 | try argv.append("-whole-archive"); |
| 3085 | 3084 | whole_archive = true; |
| ... | ... | @@ -3101,9 +3100,9 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) ! |
| 3101 | 3100 | try argv.append(p); |
| 3102 | 3101 | } |
| 3103 | 3102 | |
| 3104 | | if (self.base.options.output_mode != .Obj and |
| 3105 | | !self.base.options.skip_linker_dependencies and |
| 3106 | | !self.base.options.link_libc) |
| 3103 | if (wasm.base.options.output_mode != .Obj and |
| 3104 | !wasm.base.options.skip_linker_dependencies and |
| 3105 | !wasm.base.options.link_libc) |
| 3107 | 3106 | { |
| 3108 | 3107 | try argv.append(comp.libc_static_lib.?.full_object_path); |
| 3109 | 3108 | } |
| ... | ... | @@ -3112,7 +3111,7 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) ! |
| 3112 | 3111 | try argv.append(p); |
| 3113 | 3112 | } |
| 3114 | 3113 | |
| 3115 | | if (self.base.options.verbose_link) { |
| 3114 | if (wasm.base.options.verbose_link) { |
| 3116 | 3115 | // Skip over our own name so that the LLD linker name is the first argv item. |
| 3117 | 3116 | Compilation.dump_argv(argv.items[1..]); |
| 3118 | 3117 | } |
| ... | ... | @@ -3129,7 +3128,7 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) ! |
| 3129 | 3128 | |
| 3130 | 3129 | const term = child.spawnAndWait() catch |err| { |
| 3131 | 3130 | log.err("unable to spawn {s}: {s}", .{ argv.items[0], @errorName(err) }); |
| 3132 | | return error.UnableToSpawnSelf; |
| 3131 | return error.UnableToSpawnwasm; |
| 3133 | 3132 | }; |
| 3134 | 3133 | switch (term) { |
| 3135 | 3134 | .Exited => |code| { |
| ... | ... | @@ -3150,7 +3149,7 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) ! |
| 3150 | 3149 | |
| 3151 | 3150 | const term = child.wait() catch |err| { |
| 3152 | 3151 | log.err("unable to spawn {s}: {s}", .{ argv.items[0], @errorName(err) }); |
| 3153 | | return error.UnableToSpawnSelf; |
| 3152 | return error.UnableToSpawnwasm; |
| 3154 | 3153 | }; |
| 3155 | 3154 | |
| 3156 | 3155 | switch (term) { |
| ... | ... | @@ -3184,7 +3183,7 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) ! |
| 3184 | 3183 | } |
| 3185 | 3184 | } |
| 3186 | 3185 | |
| 3187 | | if (!self.base.options.disable_lld_caching) { |
| 3186 | if (!wasm.base.options.disable_lld_caching) { |
| 3188 | 3187 | // Update the file with the digest. If it fails we can continue; it only |
| 3189 | 3188 | // means that the next invocation will have an unnecessary cache miss. |
| 3190 | 3189 | Cache.writeSmallFile(directory.handle, id_symlink_basename, &digest) catch |err| { |
| ... | ... | @@ -3196,7 +3195,7 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) ! |
| 3196 | 3195 | }; |
| 3197 | 3196 | // We hang on to this lock so that the output file path can be used without |
| 3198 | 3197 | // other processes clobbering it. |
| 3199 | | self.base.lock = man.toOwnedLock(); |
| 3198 | wasm.base.lock = man.toOwnedLock(); |
| 3200 | 3199 | } |
| 3201 | 3200 | } |
| 3202 | 3201 | |
| ... | ... | @@ -3208,7 +3207,7 @@ fn reserveVecSectionHeader(bytes: *std.ArrayList(u8)) !u32 { |
| 3208 | 3207 | return offset; |
| 3209 | 3208 | } |
| 3210 | 3209 | |
| 3211 | | fn reserveCustomSectionHeader(bytes: *std.ArrayList(u8)) !u64 { |
| 3210 | fn reserveCustomSectionHeader(bytes: *std.ArrayList(u8)) !u32 { |
| 3212 | 3211 | // unlike regular section, we don't emit the count |
| 3213 | 3212 | const header_size = 1 + 5; |
| 3214 | 3213 | const offset = @intCast(u32, bytes.items.len); |
| ... | ... | @@ -3216,7 +3215,7 @@ fn reserveCustomSectionHeader(bytes: *std.ArrayList(u8)) !u64 { |
| 3216 | 3215 | return offset; |
| 3217 | 3216 | } |
| 3218 | 3217 | |
| 3219 | | fn writeVecSectionHeader(buffer: []u8, offset: u32, section: wasm.Section, size: u32, items: u32) !void { |
| 3218 | fn writeVecSectionHeader(buffer: []u8, offset: u32, section: std.wasm.Section, size: u32, items: u32) !void { |
| 3220 | 3219 | var buf: [1 + 5 + 5]u8 = undefined; |
| 3221 | 3220 | buf[0] = @enumToInt(section); |
| 3222 | 3221 | leb.writeUnsignedFixed(5, buf[1..6], size); |
| ... | ... | @@ -3224,14 +3223,14 @@ fn writeVecSectionHeader(buffer: []u8, offset: u32, section: wasm.Section, size: |
| 3224 | 3223 | mem.copy(u8, buffer[offset..], &buf); |
| 3225 | 3224 | } |
| 3226 | 3225 | |
| 3227 | | fn writeCustomSectionHeader(buffer: []u8, offset: u64, size: u32) !void { |
| 3226 | fn writeCustomSectionHeader(buffer: []u8, offset: u32, size: u32) !void { |
| 3228 | 3227 | var buf: [1 + 5]u8 = undefined; |
| 3229 | 3228 | buf[0] = 0; // 0 = 'custom' section |
| 3230 | 3229 | leb.writeUnsignedFixed(5, buf[1..6], size); |
| 3231 | 3230 | mem.copy(u8, buffer[offset..], &buf); |
| 3232 | 3231 | } |
| 3233 | 3232 | |
| 3234 | | fn emitLinkSection(self: *Wasm, binary_bytes: *std.ArrayList(u8), symbol_table: *std.AutoArrayHashMap(SymbolLoc, u32)) !void { |
| 3233 | fn emitLinkSection(wasm: *Wasm, binary_bytes: *std.ArrayList(u8), symbol_table: *std.AutoArrayHashMap(SymbolLoc, u32)) !void { |
| 3235 | 3234 | const offset = try reserveCustomSectionHeader(binary_bytes); |
| 3236 | 3235 | const writer = binary_bytes.writer(); |
| 3237 | 3236 | // emit "linking" custom section name |
| ... | ... | @@ -3244,22 +3243,22 @@ fn emitLinkSection(self: *Wasm, binary_bytes: *std.ArrayList(u8), symbol_table: |
| 3244 | 3243 | |
| 3245 | 3244 | // For each subsection type (found in types.Subsection) we can emit a section. |
| 3246 | 3245 | // Currently, we only support emitting segment info and the symbol table. |
| 3247 | | try self.emitSymbolTable(binary_bytes, symbol_table); |
| 3248 | | try self.emitSegmentInfo(binary_bytes); |
| 3246 | try wasm.emitSymbolTable(binary_bytes, symbol_table); |
| 3247 | try wasm.emitSegmentInfo(binary_bytes); |
| 3249 | 3248 | |
| 3250 | 3249 | const size = @intCast(u32, binary_bytes.items.len - offset - 6); |
| 3251 | 3250 | try writeCustomSectionHeader(binary_bytes.items, offset, size); |
| 3252 | 3251 | } |
| 3253 | 3252 | |
| 3254 | | fn emitSymbolTable(self: *Wasm, binary_bytes: *std.ArrayList(u8), symbol_table: *std.AutoArrayHashMap(SymbolLoc, u32)) !void { |
| 3253 | fn emitSymbolTable(wasm: *Wasm, binary_bytes: *std.ArrayList(u8), symbol_table: *std.AutoArrayHashMap(SymbolLoc, u32)) !void { |
| 3255 | 3254 | const writer = binary_bytes.writer(); |
| 3256 | 3255 | |
| 3257 | 3256 | try leb.writeULEB128(writer, @enumToInt(types.SubsectionType.WASM_SYMBOL_TABLE)); |
| 3258 | 3257 | const table_offset = binary_bytes.items.len; |
| 3259 | 3258 | |
| 3260 | 3259 | var symbol_count: u32 = 0; |
| 3261 | | for (self.resolved_symbols.keys()) |sym_loc| { |
| 3262 | | const symbol = sym_loc.getSymbol(self).*; |
| 3260 | for (wasm.resolved_symbols.keys()) |sym_loc| { |
| 3261 | const symbol = sym_loc.getSymbol(wasm).*; |
| 3263 | 3262 | if (symbol.tag == .dead) continue; // Do not emit dead symbols |
| 3264 | 3263 | try symbol_table.putNoClobber(sym_loc, symbol_count); |
| 3265 | 3264 | symbol_count += 1; |
| ... | ... | @@ -3267,7 +3266,7 @@ fn emitSymbolTable(self: *Wasm, binary_bytes: *std.ArrayList(u8), symbol_table: |
| 3267 | 3266 | try leb.writeULEB128(writer, @enumToInt(symbol.tag)); |
| 3268 | 3267 | try leb.writeULEB128(writer, symbol.flags); |
| 3269 | 3268 | |
| 3270 | | const sym_name = if (self.export_names.get(sym_loc)) |exp_name| self.string_table.get(exp_name) else sym_loc.getName(self); |
| 3269 | const sym_name = if (wasm.export_names.get(sym_loc)) |exp_name| wasm.string_table.get(exp_name) else sym_loc.getName(wasm); |
| 3271 | 3270 | switch (symbol.tag) { |
| 3272 | 3271 | .data => { |
| 3273 | 3272 | try leb.writeULEB128(writer, @intCast(u32, sym_name.len)); |
| ... | ... | @@ -3275,7 +3274,7 @@ fn emitSymbolTable(self: *Wasm, binary_bytes: *std.ArrayList(u8), symbol_table: |
| 3275 | 3274 | |
| 3276 | 3275 | if (symbol.isDefined()) { |
| 3277 | 3276 | try leb.writeULEB128(writer, symbol.index); |
| 3278 | | const atom = self.symbol_atom.get(sym_loc).?; |
| 3277 | const atom = wasm.symbol_atom.get(sym_loc).?; |
| 3279 | 3278 | try leb.writeULEB128(writer, @as(u32, atom.offset)); |
| 3280 | 3279 | try leb.writeULEB128(writer, @as(u32, atom.size)); |
| 3281 | 3280 | } |
| ... | ... | @@ -3299,13 +3298,13 @@ fn emitSymbolTable(self: *Wasm, binary_bytes: *std.ArrayList(u8), symbol_table: |
| 3299 | 3298 | try binary_bytes.insertSlice(table_offset, &buf); |
| 3300 | 3299 | } |
| 3301 | 3300 | |
| 3302 | | fn emitSegmentInfo(self: *Wasm, binary_bytes: *std.ArrayList(u8)) !void { |
| 3301 | fn emitSegmentInfo(wasm: *Wasm, binary_bytes: *std.ArrayList(u8)) !void { |
| 3303 | 3302 | const writer = binary_bytes.writer(); |
| 3304 | 3303 | try leb.writeULEB128(writer, @enumToInt(types.SubsectionType.WASM_SEGMENT_INFO)); |
| 3305 | 3304 | const segment_offset = binary_bytes.items.len; |
| 3306 | 3305 | |
| 3307 | | try leb.writeULEB128(writer, @intCast(u32, self.segment_info.count())); |
| 3308 | | for (self.segment_info.values()) |segment_info| { |
| 3306 | try leb.writeULEB128(writer, @intCast(u32, wasm.segment_info.count())); |
| 3307 | for (wasm.segment_info.values()) |segment_info| { |
| 3309 | 3308 | log.debug("Emit segment: {s} align({d}) flags({b})", .{ |
| 3310 | 3309 | segment_info.name, |
| 3311 | 3310 | @ctz(segment_info.alignment), |
| ... | ... | @@ -3336,12 +3335,12 @@ pub fn getULEB128Size(uint_value: anytype) u32 { |
| 3336 | 3335 | |
| 3337 | 3336 | /// For each relocatable section, emits a custom "relocation.<section_name>" section |
| 3338 | 3337 | fn emitCodeRelocations( |
| 3339 | | self: *Wasm, |
| 3338 | wasm: *Wasm, |
| 3340 | 3339 | binary_bytes: *std.ArrayList(u8), |
| 3341 | 3340 | section_index: u32, |
| 3342 | 3341 | symbol_table: std.AutoArrayHashMap(SymbolLoc, u32), |
| 3343 | 3342 | ) !void { |
| 3344 | | const code_index = self.code_section_index orelse return; |
| 3343 | const code_index = wasm.code_section_index orelse return; |
| 3345 | 3344 | const writer = binary_bytes.writer(); |
| 3346 | 3345 | const header_offset = try reserveCustomSectionHeader(binary_bytes); |
| 3347 | 3346 | |
| ... | ... | @@ -3353,7 +3352,7 @@ fn emitCodeRelocations( |
| 3353 | 3352 | const reloc_start = binary_bytes.items.len; |
| 3354 | 3353 | |
| 3355 | 3354 | var count: u32 = 0; |
| 3356 | | var atom: *Atom = self.atoms.get(code_index).?.getFirst(); |
| 3355 | var atom: *Atom = wasm.atoms.get(code_index).?.getFirst(); |
| 3357 | 3356 | // for each atom, we calculate the uleb size and append that |
| 3358 | 3357 | var size_offset: u32 = 5; // account for code section size leb128 |
| 3359 | 3358 | while (true) { |
| ... | ... | @@ -3382,12 +3381,12 @@ fn emitCodeRelocations( |
| 3382 | 3381 | } |
| 3383 | 3382 | |
| 3384 | 3383 | fn emitDataRelocations( |
| 3385 | | self: *Wasm, |
| 3384 | wasm: *Wasm, |
| 3386 | 3385 | binary_bytes: *std.ArrayList(u8), |
| 3387 | 3386 | section_index: u32, |
| 3388 | 3387 | symbol_table: std.AutoArrayHashMap(SymbolLoc, u32), |
| 3389 | 3388 | ) !void { |
| 3390 | | if (self.data_segments.count() == 0) return; |
| 3389 | if (wasm.data_segments.count() == 0) return; |
| 3391 | 3390 | const writer = binary_bytes.writer(); |
| 3392 | 3391 | const header_offset = try reserveCustomSectionHeader(binary_bytes); |
| 3393 | 3392 | |
| ... | ... | @@ -3401,8 +3400,8 @@ fn emitDataRelocations( |
| 3401 | 3400 | var count: u32 = 0; |
| 3402 | 3401 | // for each atom, we calculate the uleb size and append that |
| 3403 | 3402 | var size_offset: u32 = 5; // account for code section size leb128 |
| 3404 | | for (self.data_segments.values()) |segment_index| { |
| 3405 | | var atom: *Atom = self.atoms.get(segment_index).?.getFirst(); |
| 3403 | for (wasm.data_segments.values()) |segment_index| { |
| 3404 | var atom: *Atom = wasm.atoms.get(segment_index).?.getFirst(); |
| 3406 | 3405 | while (true) { |
| 3407 | 3406 | size_offset += getULEB128Size(atom.size); |
| 3408 | 3407 | for (atom.relocs.items) |relocation| { |
| ... | ... | @@ -3435,18 +3434,18 @@ fn emitDataRelocations( |
| 3435 | 3434 | |
| 3436 | 3435 | /// Searches for an a matching function signature, when not found |
| 3437 | 3436 | /// a new entry will be made. The index of the existing/new signature will be returned. |
| 3438 | | pub fn putOrGetFuncType(self: *Wasm, func_type: wasm.Type) !u32 { |
| 3437 | pub fn putOrGetFuncType(wasm: *Wasm, func_type: std.wasm.Type) !u32 { |
| 3439 | 3438 | var index: u32 = 0; |
| 3440 | | while (index < self.func_types.items.len) : (index += 1) { |
| 3441 | | if (self.func_types.items[index].eql(func_type)) return index; |
| 3439 | while (index < wasm.func_types.items.len) : (index += 1) { |
| 3440 | if (wasm.func_types.items[index].eql(func_type)) return index; |
| 3442 | 3441 | } |
| 3443 | 3442 | |
| 3444 | 3443 | // functype does not exist. |
| 3445 | | const params = try self.base.allocator.dupe(wasm.Valtype, func_type.params); |
| 3446 | | errdefer self.base.allocator.free(params); |
| 3447 | | const returns = try self.base.allocator.dupe(wasm.Valtype, func_type.returns); |
| 3448 | | errdefer self.base.allocator.free(returns); |
| 3449 | | try self.func_types.append(self.base.allocator, .{ |
| 3444 | const params = try wasm.base.allocator.dupe(std.wasm.Valtype, func_type.params); |
| 3445 | errdefer wasm.base.allocator.free(params); |
| 3446 | const returns = try wasm.base.allocator.dupe(std.wasm.Valtype, func_type.returns); |
| 3447 | errdefer wasm.base.allocator.free(returns); |
| 3448 | try wasm.func_types.append(wasm.base.allocator, .{ |
| 3450 | 3449 | .params = params, |
| 3451 | 3450 | .returns = returns, |
| 3452 | 3451 | }); |