| ... | ... | @@ -17,10 +17,6 @@ const log = std.log.scoped(.link); |
| 17 | 17 | |
| 18 | 18 | /// Wasm spec version used for this `Object` |
| 19 | 19 | version: u32 = 0, |
| 20 | | /// The entire object file is read and parsed in a single pass. |
| 21 | | /// For this reason it's a lot simpler to use an arena and store the entire |
| 22 | | /// state after parsing. This also allows to free all memory at once. |
| 23 | | arena: std.heap.ArenaAllocator.State = .{}, |
| 24 | 20 | /// The file descriptor that represents the wasm object file. |
| 25 | 21 | file: ?std.fs.File = null, |
| 26 | 22 | /// Name (read path) of the object file. |
| ... | ... | @@ -28,15 +24,15 @@ name: []const u8, |
| 28 | 24 | /// Parsed type section |
| 29 | 25 | func_types: []const std.wasm.Type = &.{}, |
| 30 | 26 | /// A list of all imports for this module |
| 31 | | imports: []std.wasm.Import = &.{}, |
| 27 | imports: []const std.wasm.Import = &.{}, |
| 32 | 28 | /// Parsed function section |
| 33 | | functions: []std.wasm.Func = &.{}, |
| 29 | functions: []const std.wasm.Func = &.{}, |
| 34 | 30 | /// Parsed table section |
| 35 | | tables: []std.wasm.Table = &.{}, |
| 31 | tables: []const std.wasm.Table = &.{}, |
| 36 | 32 | /// Parsed memory section |
| 37 | 33 | memories: []const std.wasm.Memory = &.{}, |
| 38 | 34 | /// Parsed global section |
| 39 | | globals: []std.wasm.Global = &.{}, |
| 35 | globals: []const std.wasm.Global = &.{}, |
| 40 | 36 | /// Parsed export section |
| 41 | 37 | exports: []const std.wasm.Export = &.{}, |
| 42 | 38 | /// Parsed element section |
| ... | ... | @@ -62,7 +58,7 @@ init_funcs: []const types.InitFunc = &.{}, |
| 62 | 58 | comdat_info: []const types.Comdat = &.{}, |
| 63 | 59 | /// Represents non-synthetic sections that can essentially be mem-cpy'd into place |
| 64 | 60 | /// after performing relocations. |
| 65 | | relocatable_data: []RelocatableData = &.{}, |
| 61 | relocatable_data: []const RelocatableData = &.{}, |
| 66 | 62 | |
| 67 | 63 | /// Represents a single item within a section (depending on its `type`) |
| 68 | 64 | const RelocatableData = struct { |
| ... | ... | @@ -111,12 +107,9 @@ pub fn create(gpa: Allocator, file: std.fs.File, path: []const u8) InitError!Obj |
| 111 | 107 | .name = path, |
| 112 | 108 | }; |
| 113 | 109 | |
| 114 | | var arena = std.heap.ArenaAllocator.init(gpa); |
| 115 | | errdefer arena.deinit(); |
| 116 | | |
| 117 | 110 | var is_object_file: bool = false; |
| 118 | | try object.parse(arena.allocator(), file.reader(), &is_object_file); |
| 119 | | object.arena = arena.state; |
| 111 | try object.parse(gpa, file.reader(), &is_object_file); |
| 112 | errdefer object.deinit(gpa); |
| 120 | 113 | if (!is_object_file) return error.NotObjectFile; |
| 121 | 114 | |
| 122 | 115 | return object; |
| ... | ... | @@ -125,7 +118,44 @@ pub fn create(gpa: Allocator, file: std.fs.File, path: []const u8) InitError!Obj |
| 125 | 118 | /// Frees all memory of `Object` at once. The given `Allocator` must be |
| 126 | 119 | /// the same allocator that was used when `init` was called. |
| 127 | 120 | pub fn deinit(self: *Object, gpa: Allocator) void { |
| 128 | | self.arena.promote(gpa).deinit(); |
| 121 | for (self.func_types) |func_ty| { |
| 122 | gpa.free(func_ty.params); |
| 123 | gpa.free(func_ty.returns); |
| 124 | } |
| 125 | gpa.free(self.func_types); |
| 126 | for (self.imports) |imp| { |
| 127 | gpa.free(imp.name); |
| 128 | gpa.free(imp.module_name); |
| 129 | } |
| 130 | gpa.free(self.functions); |
| 131 | gpa.free(self.imports); |
| 132 | gpa.free(self.tables); |
| 133 | gpa.free(self.memories); |
| 134 | gpa.free(self.globals); |
| 135 | for (self.exports) |exp| { |
| 136 | gpa.free(exp.name); |
| 137 | } |
| 138 | gpa.free(self.exports); |
| 139 | gpa.free(self.elements); |
| 140 | gpa.free(self.features); |
| 141 | for (self.relocations.values()) |val| { |
| 142 | gpa.free(val); |
| 143 | } |
| 144 | self.relocations.deinit(gpa); |
| 145 | for (self.symtable) |symbol| { |
| 146 | gpa.free(std.mem.sliceTo(symbol.name, 0)); |
| 147 | } |
| 148 | gpa.free(self.symtable); |
| 149 | gpa.free(self.comdat_info); |
| 150 | gpa.free(self.init_funcs); |
| 151 | for (self.segment_info) |info| { |
| 152 | gpa.free(info.name); |
| 153 | } |
| 154 | gpa.free(self.segment_info); |
| 155 | for (self.relocatable_data) |rel_data| { |
| 156 | gpa.free(rel_data.data[0..rel_data.size]); |
| 157 | } |
| 158 | gpa.free(self.relocatable_data); |
| 129 | 159 | self.* = undefined; |
| 130 | 160 | } |
| 131 | 161 | |
| ... | ... | @@ -149,13 +179,6 @@ pub fn importedCountByKind(self: *const Object, kind: std.wasm.ExternalKind) u32 |
| 149 | 179 | } else i; |
| 150 | 180 | } |
| 151 | 181 | |
| 152 | | /// Returns a table by a given id, rather than by its index within the list. |
| 153 | | pub fn getTable(self: *const Object, id: u32) *std.wasm.Table { |
| 154 | | return for (self.tables) |*table| { |
| 155 | | if (table.table_idx == id) break table; |
| 156 | | } else unreachable; |
| 157 | | } |
| 158 | | |
| 159 | 182 | /// Checks if the object file is an MVP version. |
| 160 | 183 | /// When that's the case, we check if there's an import table definiton with its name |
| 161 | 184 | /// set to '__indirect_function_table". When that's also the case, |
| ... | ... | @@ -328,10 +351,12 @@ fn Parser(comptime ReaderType: type) type { |
| 328 | 351 | for (try readVec(&self.object.imports, reader, gpa)) |*import| { |
| 329 | 352 | const module_len = try readLeb(u32, reader); |
| 330 | 353 | const module_name = try gpa.alloc(u8, module_len); |
| 354 | errdefer gpa.free(module_name); |
| 331 | 355 | try reader.readNoEof(module_name); |
| 332 | 356 | |
| 333 | 357 | const name_len = try readLeb(u32, reader); |
| 334 | 358 | const name = try gpa.alloc(u8, name_len); |
| 359 | errdefer gpa.free(name); |
| 335 | 360 | try reader.readNoEof(name); |
| 336 | 361 | |
| 337 | 362 | const kind = try readEnum(std.wasm.ExternalKind, reader); |
| ... | ... | @@ -393,6 +418,7 @@ fn Parser(comptime ReaderType: type) type { |
| 393 | 418 | for (try readVec(&self.object.exports, reader, gpa)) |*exp| { |
| 394 | 419 | const name_len = try readLeb(u32, reader); |
| 395 | 420 | const name = try gpa.alloc(u8, name_len); |
| 421 | errdefer gpa.free(name); |
| 396 | 422 | try reader.readNoEof(name); |
| 397 | 423 | exp.* = .{ |
| 398 | 424 | .name = name, |
| ... | ... | @@ -425,6 +451,7 @@ fn Parser(comptime ReaderType: type) type { |
| 425 | 451 | const code_len = try readLeb(u32, reader); |
| 426 | 452 | const offset = @intCast(u32, start - reader.context.bytes_left); |
| 427 | 453 | const data = try gpa.alloc(u8, code_len); |
| 454 | errdefer gpa.free(data); |
| 428 | 455 | try reader.readNoEof(data); |
| 429 | 456 | try relocatable_data.append(.{ |
| 430 | 457 | .type = .code, |
| ... | ... | @@ -448,6 +475,7 @@ fn Parser(comptime ReaderType: type) type { |
| 448 | 475 | const data_len = try readLeb(u32, reader); |
| 449 | 476 | const offset = @intCast(u32, start - reader.context.bytes_left); |
| 450 | 477 | const data = try gpa.alloc(u8, data_len); |
| 478 | errdefer gpa.free(data); |
| 451 | 479 | try reader.readNoEof(data); |
| 452 | 480 | try relocatable_data.append(.{ |
| 453 | 481 | .type = .data, |
| ... | ... | @@ -478,6 +506,7 @@ fn Parser(comptime ReaderType: type) type { |
| 478 | 506 | const prefix = try readEnum(types.Feature.Prefix, reader); |
| 479 | 507 | const name_len = try leb.readULEB128(u32, reader); |
| 480 | 508 | const name = try gpa.alloc(u8, name_len); |
| 509 | defer gpa.free(name); |
| 481 | 510 | try reader.readNoEof(name); |
| 482 | 511 | |
| 483 | 512 | const tag = types.known_features.get(name) orelse { |
| ... | ... | @@ -499,6 +528,7 @@ fn Parser(comptime ReaderType: type) type { |
| 499 | 528 | const section = try leb.readULEB128(u32, reader); |
| 500 | 529 | const count = try leb.readULEB128(u32, reader); |
| 501 | 530 | const relocations = try gpa.alloc(types.Relocation, count); |
| 531 | errdefer gpa.free(relocations); |
| 502 | 532 | |
| 503 | 533 | log.debug("Found {d} relocations for section ({d})", .{ |
| 504 | 534 | count, |
| ... | ... | @@ -563,9 +593,11 @@ fn Parser(comptime ReaderType: type) type { |
| 563 | 593 | switch (@intToEnum(types.SubsectionType, sub_type)) { |
| 564 | 594 | .WASM_SEGMENT_INFO => { |
| 565 | 595 | const segments = try gpa.alloc(types.Segment, count); |
| 596 | errdefer gpa.free(segments); |
| 566 | 597 | for (segments) |*segment| { |
| 567 | 598 | const name_len = try leb.readULEB128(u32, reader); |
| 568 | 599 | const name = try gpa.alloc(u8, name_len); |
| 600 | errdefer gpa.free(name); |
| 569 | 601 | try reader.readNoEof(name); |
| 570 | 602 | segment.* = .{ |
| 571 | 603 | .name = name, |
| ... | ... | @@ -582,6 +614,7 @@ fn Parser(comptime ReaderType: type) type { |
| 582 | 614 | }, |
| 583 | 615 | .WASM_INIT_FUNCS => { |
| 584 | 616 | const funcs = try gpa.alloc(types.InitFunc, count); |
| 617 | errdefer gpa.free(funcs); |
| 585 | 618 | for (funcs) |*func| { |
| 586 | 619 | func.* = .{ |
| 587 | 620 | .priority = try leb.readULEB128(u32, reader), |
| ... | ... | @@ -593,9 +626,11 @@ fn Parser(comptime ReaderType: type) type { |
| 593 | 626 | }, |
| 594 | 627 | .WASM_COMDAT_INFO => { |
| 595 | 628 | const comdats = try gpa.alloc(types.Comdat, count); |
| 629 | errdefer gpa.free(comdats); |
| 596 | 630 | for (comdats) |*comdat| { |
| 597 | 631 | const name_len = try leb.readULEB128(u32, reader); |
| 598 | 632 | const name = try gpa.alloc(u8, name_len); |
| 633 | errdefer gpa.free(name); |
| 599 | 634 | try reader.readNoEof(name); |
| 600 | 635 | |
| 601 | 636 | const flags = try leb.readULEB128(u32, reader); |
| ... | ... | @@ -605,6 +640,7 @@ fn Parser(comptime ReaderType: type) type { |
| 605 | 640 | |
| 606 | 641 | const symbol_count = try leb.readULEB128(u32, reader); |
| 607 | 642 | const symbols = try gpa.alloc(types.ComdatSym, symbol_count); |
| 643 | errdefer gpa.free(symbols); |
| 608 | 644 | for (symbols) |*symbol| { |
| 609 | 645 | symbol.* = .{ |
| 610 | 646 | .kind = @intToEnum(types.ComdatSym.Type, try leb.readULEB128(u8, reader)), |
| ... | ... | @@ -664,6 +700,7 @@ fn Parser(comptime ReaderType: type) type { |
| 664 | 700 | .data => { |
| 665 | 701 | const name_len = try leb.readULEB128(u32, reader); |
| 666 | 702 | const name = try gpa.allocSentinel(u8, name_len, 0); |
| 703 | errdefer gpa.free(name); |
| 667 | 704 | try reader.readNoEof(name); |
| 668 | 705 | symbol.name = name; |
| 669 | 706 | |
| ... | ... | @@ -691,6 +728,7 @@ fn Parser(comptime ReaderType: type) type { |
| 691 | 728 | if (!(is_undefined and !explicit_name)) { |
| 692 | 729 | const name_len = try leb.readULEB128(u32, reader); |
| 693 | 730 | const name = try gpa.allocSentinel(u8, name_len, 0); |
| 731 | errdefer gpa.free(name); |
| 694 | 732 | try reader.readNoEof(name); |
| 695 | 733 | symbol.name = name; |
| 696 | 734 | } else { |