| ... | ... | @@ -152,7 +152,7 @@ entry: ?u32 = null, |
| 152 | 152 | function_table: std.AutoHashMapUnmanaged(SymbolLoc, u32) = .{}, |
| 153 | 153 | |
| 154 | 154 | /// All object files and their data which are linked into the final binary |
| 155 | | objects: std.ArrayListUnmanaged(Object) = .{}, |
| 155 | objects: std.ArrayListUnmanaged(File.Index) = .{}, |
| 156 | 156 | /// All archive files that are lazy loaded. |
| 157 | 157 | /// e.g. when an undefined symbol references a symbol from the archive. |
| 158 | 158 | archives: std.ArrayListUnmanaged(Archive) = .{}, |
| ... | ... | @@ -442,7 +442,7 @@ pub fn createEmpty( |
| 442 | 442 | // can be passed to LLD. |
| 443 | 443 | const sub_path = if (use_lld) zcu_object_sub_path.? else emit.sub_path; |
| 444 | 444 | |
| 445 | | const file = try emit.directory.handle.createFile(sub_path, .{ |
| 445 | wasm.base.file = try emit.directory.handle.createFile(sub_path, .{ |
| 446 | 446 | .truncate = true, |
| 447 | 447 | .read = true, |
| 448 | 448 | .mode = if (fs.has_executable_bit) |
| ... | ... | @@ -453,7 +453,6 @@ pub fn createEmpty( |
| 453 | 453 | else |
| 454 | 454 | 0, |
| 455 | 455 | }); |
| 456 | | wasm.base.file = file; |
| 457 | 456 | wasm.name = sub_path; |
| 458 | 457 | |
| 459 | 458 | // create stack pointer symbol |
| ... | ... | @@ -582,6 +581,15 @@ pub fn createEmpty( |
| 582 | 581 | return wasm; |
| 583 | 582 | } |
| 584 | 583 | |
| 584 | pub fn file(wasm: *Wasm, index: File.Index) ?File { |
| 585 | const tag = wasm.files.items(.tags)[index]; |
| 586 | return switch (tag) { |
| 587 | .null => null, |
| 588 | .zig_object => .{ .zig_object = &wasm.files.items(.data)[index].zig_object }, |
| 589 | .object => .{ .object = &wasm.files.items(.data)[index].object }, |
| 590 | }; |
| 591 | } |
| 592 | |
| 585 | 593 | pub fn zigObjectPtr(wasm: *Wasm) ?*ZigObject { |
| 586 | 594 | if (wasm.zig_object_index == .null) return null; |
| 587 | 595 | return &wasm.files.items(.data)[@intFromEnum(wasm.zig_object_index)].zig_object; |
| ... | ... | @@ -650,16 +658,18 @@ fn parseInputFiles(wasm: *Wasm, files: []const []const u8) !void { |
| 650 | 658 | /// file and parsed successfully. Returns false when file is not an object file. |
| 651 | 659 | /// May return an error instead when parsing failed. |
| 652 | 660 | fn parseObjectFile(wasm: *Wasm, path: []const u8) !bool { |
| 653 | | const file = try fs.cwd().openFile(path, .{}); |
| 654 | | errdefer file.close(); |
| 661 | const obj_file = try fs.cwd().openFile(path, .{}); |
| 662 | errdefer obj_file.close(); |
| 655 | 663 | |
| 656 | 664 | const gpa = wasm.base.comp.gpa; |
| 657 | | var object = Object.create(gpa, file, path, null) catch |err| switch (err) { |
| 665 | var object = Object.create(gpa, obj_file, path, null) catch |err| switch (err) { |
| 658 | 666 | error.InvalidMagicByte, error.NotObjectFile => return false, |
| 659 | 667 | else => |e| return e, |
| 660 | 668 | }; |
| 661 | 669 | errdefer object.deinit(gpa); |
| 662 | | try wasm.objects.append(gpa, object); |
| 670 | object.index = @enumFromInt(wasm.files.len); |
| 671 | try wasm.files.append(gpa, .{ .object = object }); |
| 672 | try wasm.objects.append(gpa, object.index); |
| 663 | 673 | return true; |
| 664 | 674 | } |
| 665 | 675 | |
| ... | ... | @@ -693,11 +703,11 @@ pub inline fn getAtomPtr(wasm: *Wasm, index: Atom.Index) *Atom { |
| 693 | 703 | fn parseArchive(wasm: *Wasm, path: []const u8, force_load: bool) !bool { |
| 694 | 704 | const gpa = wasm.base.comp.gpa; |
| 695 | 705 | |
| 696 | | const file = try fs.cwd().openFile(path, .{}); |
| 697 | | errdefer file.close(); |
| 706 | const archive_file = try fs.cwd().openFile(path, .{}); |
| 707 | errdefer archive_file.close(); |
| 698 | 708 | |
| 699 | 709 | var archive: Archive = .{ |
| 700 | | .file = file, |
| 710 | .file = archive_file, |
| 701 | 711 | .name = path, |
| 702 | 712 | }; |
| 703 | 713 | archive.parse(gpa) catch |err| switch (err) { |
| ... | ... | @@ -727,8 +737,10 @@ fn parseArchive(wasm: *Wasm, path: []const u8, force_load: bool) !bool { |
| 727 | 737 | } |
| 728 | 738 | |
| 729 | 739 | for (offsets.keys()) |file_offset| { |
| 730 | | const object = try wasm.objects.addOne(gpa); |
| 731 | | object.* = try archive.parseObject(gpa, file_offset); |
| 740 | var object = try archive.parseObject(gpa, file_offset); |
| 741 | object.index = @enumFromInt(wasm.files.len); |
| 742 | try wasm.files.append(gpa, .{ .object = object }); |
| 743 | try wasm.objects.append(gpa, object.index); |
| 732 | 744 | } |
| 733 | 745 | |
| 734 | 746 | return true; |
| ... | ... | @@ -784,8 +796,8 @@ fn resolveSymbolsInObject(wasm: *Wasm, object_index: u16) !void { |
| 784 | 796 | const existing_loc = maybe_existing.value_ptr.*; |
| 785 | 797 | const existing_sym: *Symbol = existing_loc.getSymbol(wasm); |
| 786 | 798 | |
| 787 | | const existing_file_path = if (existing_loc.file) |file| blk: { |
| 788 | | break :blk wasm.objects.items[file].name; |
| 799 | const existing_file_path = if (existing_loc.file) |file_index| blk: { |
| 800 | break :blk wasm.objects.items[file_index].name; |
| 789 | 801 | } else wasm.name; |
| 790 | 802 | |
| 791 | 803 | if (!existing_sym.isUndefined()) outer: { |
| ... | ... | @@ -911,10 +923,11 @@ fn resolveSymbolsInArchives(wasm: *Wasm) !void { |
| 911 | 923 | // Symbol is found in unparsed object file within current archive. |
| 912 | 924 | // Parse object and and resolve symbols again before we check remaining |
| 913 | 925 | // undefined symbols. |
| 914 | | const object_file_index: u16 = @intCast(wasm.objects.items.len); |
| 915 | | const object = try archive.parseObject(gpa, offset.items[0]); |
| 916 | | try wasm.objects.append(gpa, object); |
| 917 | | try wasm.resolveSymbolsInObject(object_file_index); |
| 926 | var object = try archive.parseObject(gpa, offset.items[0]); |
| 927 | object.index = @enumFromInt(wasm.files.len); |
| 928 | try wasm.files.append(gpa, .{ .object = object }); |
| 929 | try wasm.objects.append(gpa, object.index); |
| 930 | try wasm.resolveSymbolsInObject(object.index); |
| 918 | 931 | |
| 919 | 932 | // continue loop for any remaining undefined symbols that still exist |
| 920 | 933 | // after resolving last object file |
| ... | ... | @@ -1176,9 +1189,10 @@ fn validateFeatures( |
| 1176 | 1189 | |
| 1177 | 1190 | // extract all the used, disallowed and required features from each |
| 1178 | 1191 | // linked object file so we can test them. |
| 1179 | | for (wasm.objects.items, 0..) |object, object_index| { |
| 1192 | for (wasm.objects.items) |file_index| { |
| 1193 | const object: Object = wasm.files.items(.data)[file_index].object; |
| 1180 | 1194 | for (object.features) |feature| { |
| 1181 | | const value = @as(u16, @intCast(object_index)) << 1 | @as(u1, 1); |
| 1195 | const value = @as(u16, @intFromEnum(file_index)) << 1 | @as(u1, 1); |
| 1182 | 1196 | switch (feature.prefix) { |
| 1183 | 1197 | .used => { |
| 1184 | 1198 | used[@intFromEnum(feature.tag)] = value; |
| ... | ... | @@ -1210,7 +1224,7 @@ fn validateFeatures( |
| 1210 | 1224 | emit_features_count.* += @intFromBool(is_enabled); |
| 1211 | 1225 | } else if (is_enabled and !allowed[used_index]) { |
| 1212 | 1226 | log.err("feature '{}' not allowed, but used by linked object", .{@as(types.Feature.Tag, @enumFromInt(used_index))}); |
| 1213 | | log.err(" defined in '{s}'", .{wasm.objects.items[used_set >> 1].name}); |
| 1227 | log.err(" defined in '{s}'", .{wasm.files.items(.data)[used_set >> 1].object.path}); |
| 1214 | 1228 | valid_feature_set = false; |
| 1215 | 1229 | } |
| 1216 | 1230 | } |
| ... | ... | @@ -1224,7 +1238,7 @@ fn validateFeatures( |
| 1224 | 1238 | if (@as(u1, @truncate(disallowed_feature)) != 0) { |
| 1225 | 1239 | log.err( |
| 1226 | 1240 | "shared-memory is disallowed by '{s}' because it wasn't compiled with 'atomics' and 'bulk-memory' features enabled", |
| 1227 | | .{wasm.objects.items[disallowed_feature >> 1].name}, |
| 1241 | .{wasm.files.items(.data)[disallowed_feature >> 1].object.path}, |
| 1228 | 1242 | ); |
| 1229 | 1243 | valid_feature_set = false; |
| 1230 | 1244 | } |
| ... | ... | @@ -1244,16 +1258,17 @@ fn validateFeatures( |
| 1244 | 1258 | } |
| 1245 | 1259 | } |
| 1246 | 1260 | // For each linked object, validate the required and disallowed features |
| 1247 | | for (wasm.objects.items) |object| { |
| 1261 | for (wasm.objects.items) |file_index| { |
| 1248 | 1262 | var object_used_features = [_]bool{false} ** known_features_count; |
| 1263 | const object = wasm.files.items(.data)[file_index].object; |
| 1249 | 1264 | for (object.features) |feature| { |
| 1250 | 1265 | if (feature.prefix == .disallowed) continue; // already defined in 'disallowed' set. |
| 1251 | 1266 | // from here a feature is always used |
| 1252 | 1267 | const disallowed_feature = disallowed[@intFromEnum(feature.tag)]; |
| 1253 | 1268 | if (@as(u1, @truncate(disallowed_feature)) != 0) { |
| 1254 | 1269 | log.err("feature '{}' is disallowed, but used by linked object", .{feature.tag}); |
| 1255 | | log.err(" disallowed by '{s}'", .{wasm.objects.items[disallowed_feature >> 1].name}); |
| 1256 | | log.err(" used in '{s}'", .{object.name}); |
| 1270 | log.err(" disallowed by '{s}'", .{wasm.files.items(.data)[disallowed_feature >> 1].object.path}); |
| 1271 | log.err(" used in '{s}'", .{object.path}); |
| 1257 | 1272 | valid_feature_set = false; |
| 1258 | 1273 | } |
| 1259 | 1274 | |
| ... | ... | @@ -1265,8 +1280,8 @@ fn validateFeatures( |
| 1265 | 1280 | const is_required = @as(u1, @truncate(required_feature)) != 0; |
| 1266 | 1281 | if (is_required and !object_used_features[feature_index]) { |
| 1267 | 1282 | log.err("feature '{}' is required but not used in linked object", .{@as(types.Feature.Tag, @enumFromInt(feature_index))}); |
| 1268 | | log.err(" required by '{s}'", .{wasm.objects.items[required_feature >> 1].name}); |
| 1269 | | log.err(" missing in '{s}'", .{object.name}); |
| 1283 | log.err(" required by '{s}'", .{wasm.files.items(.data)[required_feature >> 1].object.path}); |
| 1284 | log.err(" missing in '{s}'", .{object.path}); |
| 1270 | 1285 | valid_feature_set = false; |
| 1271 | 1286 | } |
| 1272 | 1287 | } |
| ... | ... | @@ -1346,9 +1361,10 @@ fn checkUndefinedSymbols(wasm: *const Wasm) !void { |
| 1346 | 1361 | const symbol = undef.getSymbol(wasm); |
| 1347 | 1362 | if (symbol.tag == .data) { |
| 1348 | 1363 | found_undefined_symbols = true; |
| 1349 | | const file_name = if (undef.file) |file_index| name: { |
| 1350 | | break :name wasm.objects.items[file_index].name; |
| 1351 | | } else wasm.name; |
| 1364 | const file_name = if (undef.file) |file_index| |
| 1365 | wasm.file(file_index).?.path() |
| 1366 | else |
| 1367 | wasm.name; |
| 1352 | 1368 | const symbol_name = undef.getName(wasm); |
| 1353 | 1369 | log.err("could not resolve undefined symbol '{s}'", .{symbol_name}); |
| 1354 | 1370 | log.err(" defined in '{s}'", .{file_name}); |
| ... | ... | @@ -1369,8 +1385,11 @@ pub fn deinit(wasm: *Wasm) void { |
| 1369 | 1385 | for (wasm.segment_info.values()) |segment_info| { |
| 1370 | 1386 | gpa.free(segment_info.name); |
| 1371 | 1387 | } |
| 1372 | | for (wasm.objects.items) |*object| { |
| 1373 | | object.deinit(gpa); |
| 1388 | if (wasm.zigObjectPtr()) |zig_obj| { |
| 1389 | zig_obj.deinit(gpa); |
| 1390 | } |
| 1391 | for (wasm.objects.items) |obj_index| { |
| 1392 | wasm.file(obj_index).?.object.deinit(gpa); |
| 1374 | 1393 | } |
| 1375 | 1394 | |
| 1376 | 1395 | for (wasm.archives.items) |*archive| { |
| ... | ... | @@ -1441,12 +1460,11 @@ fn getGlobalType(wasm: *const Wasm, loc: SymbolLoc) std.wasm.GlobalType { |
| 1441 | 1460 | assert(symbol.tag == .global); |
| 1442 | 1461 | const is_undefined = symbol.isUndefined(); |
| 1443 | 1462 | if (loc.file) |file_index| { |
| 1444 | | const obj: Object = wasm.objects.items[file_index]; |
| 1463 | const obj_file = wasm.file(@enumFromInt(file_index)).?; |
| 1445 | 1464 | if (is_undefined) { |
| 1446 | | return obj.findImport(.global, symbol.index).kind.global; |
| 1465 | return obj_file.import(loc.index).kind.global; |
| 1447 | 1466 | } |
| 1448 | | const import_global_count = obj.importedCountByKind(.global); |
| 1449 | | return obj.globals[symbol.index - import_global_count].global_type; |
| 1467 | return obj_file.globals()[symbol.index - obj_file.importedGlobals()].global_type; |
| 1450 | 1468 | } |
| 1451 | 1469 | if (is_undefined) { |
| 1452 | 1470 | return wasm.imports.get(loc).?.kind.global; |
| ... | ... | @@ -1461,14 +1479,13 @@ fn getFunctionSignature(wasm: *const Wasm, loc: SymbolLoc) std.wasm.Type { |
| 1461 | 1479 | assert(symbol.tag == .function); |
| 1462 | 1480 | const is_undefined = symbol.isUndefined(); |
| 1463 | 1481 | if (loc.file) |file_index| { |
| 1464 | | const obj: Object = wasm.objects.items[file_index]; |
| 1482 | const obj_file = wasm.file(@enumFromInt(file_index)).?; |
| 1465 | 1483 | if (is_undefined) { |
| 1466 | | const ty_index = obj.findImport(.function, symbol.index).kind.function; |
| 1467 | | return obj.func_types[ty_index]; |
| 1484 | const ty_index = obj_file.import(loc.index).kind.function; |
| 1485 | return obj_file.funcTypes()[ty_index]; |
| 1468 | 1486 | } |
| 1469 | | const import_function_count = obj.importedCountByKind(.function); |
| 1470 | | const type_index = obj.functions[symbol.index - import_function_count].type_index; |
| 1471 | | return obj.func_types[type_index]; |
| 1487 | const type_index = obj_file.functions()[symbol.index - obj_file.importedFunctions()].type_index; |
| 1488 | return obj_file.funcTypes()[type_index]; |
| 1472 | 1489 | } |
| 1473 | 1490 | if (is_undefined) { |
| 1474 | 1491 | const ty_index = wasm.imports.get(loc).?.kind.function; |
| ... | ... | @@ -1606,10 +1623,10 @@ fn allocateAtoms(wasm: *Wasm) !void { |
| 1606 | 1623 | // Ensure we get the original symbol, so we verify the correct symbol on whether |
| 1607 | 1624 | // it is dead or not and ensure an atom is removed when dead. |
| 1608 | 1625 | // This is required as we may have parsed aliases into atoms. |
| 1609 | | const sym = if (symbol_loc.file) |object_index| sym: { |
| 1610 | | const object = wasm.objects.items[object_index]; |
| 1611 | | break :sym object.symtable[symbol_loc.index]; |
| 1612 | | } else wasm.synthetic_symbols.items[symbol_loc.index]; |
| 1626 | const sym = if (symbol_loc.file) |object_index| |
| 1627 | wasm.file(object_index).?.symbol(symbol_loc.index).* |
| 1628 | else |
| 1629 | wasm.synthetic_symbols.items[symbol_loc.index]; |
| 1613 | 1630 | |
| 1614 | 1631 | // Dead symbols must be unlinked from the linked-list to prevent them |
| 1615 | 1632 | // from being emit into the binary. |
| ... | ... | @@ -1655,9 +1672,10 @@ fn allocateVirtualAddresses(wasm: *Wasm) void { |
| 1655 | 1672 | |
| 1656 | 1673 | const atom = wasm.getAtom(atom_index); |
| 1657 | 1674 | const merge_segment = wasm.base.comp.config.output_mode != .Obj; |
| 1658 | | const segment_info = if (atom.file) |object_index| blk: { |
| 1659 | | break :blk wasm.objects.items[object_index].segment_info; |
| 1660 | | } else wasm.segment_info.values(); |
| 1675 | const segment_info = if (atom.file) |object_index| |
| 1676 | wasm.file(object_index).?.segmentInfo() |
| 1677 | else |
| 1678 | wasm.segment_info.values(); |
| 1661 | 1679 | const segment_name = segment_info[symbol.index].outputName(merge_segment); |
| 1662 | 1680 | const segment_index = wasm.data_segments.get(segment_name).?; |
| 1663 | 1681 | const segment = wasm.segments.items[segment_index]; |
| ... | ... | @@ -1713,7 +1731,8 @@ fn sortDataSegments(wasm: *Wasm) !void { |
| 1713 | 1731 | /// contain any parameters. |
| 1714 | 1732 | fn setupInitFunctions(wasm: *Wasm) !void { |
| 1715 | 1733 | const gpa = wasm.base.comp.gpa; |
| 1716 | | for (wasm.objects.items, 0..) |object, file_index| { |
| 1734 | for (wasm.objects.items) |file_index| { |
| 1735 | const object = wasm.files.items(.data)[file_index].object; |
| 1717 | 1736 | try wasm.init_funcs.ensureUnusedCapacity(gpa, object.init_funcs.len); |
| 1718 | 1737 | for (object.init_funcs) |init_func| { |
| 1719 | 1738 | const symbol = object.symtable[init_func.symbol_index]; |
| ... | ... | @@ -1961,7 +1980,7 @@ fn setupImports(wasm: *Wasm) !void { |
| 1961 | 1980 | |
| 1962 | 1981 | for (wasm.resolved_symbols.keys()) |symbol_loc| { |
| 1963 | 1982 | const file_index = symbol_loc.file orelse { |
| 1964 | | // imports generated by Zig code are already in the `import` section |
| 1983 | // Synthetic symbols will already exist in the `import` section |
| 1965 | 1984 | continue; |
| 1966 | 1985 | }; |
| 1967 | 1986 | |
| ... | ... | @@ -1974,14 +1993,14 @@ fn setupImports(wasm: *Wasm) !void { |
| 1974 | 1993 | } |
| 1975 | 1994 | |
| 1976 | 1995 | log.debug("Symbol '{s}' will be imported from the host", .{symbol_loc.getName(wasm)}); |
| 1977 | | const object = wasm.objects.items[file_index]; |
| 1978 | | const import = object.findImport(symbol.tag.externalType(), symbol.index); |
| 1996 | const obj_file = wasm.file(file_index).?; |
| 1997 | const import = obj_file.import(symbol_loc.index); |
| 1979 | 1998 | |
| 1980 | 1999 | // We copy the import to a new import to ensure the names contain references |
| 1981 | 2000 | // to the internal string table, rather than of the object file. |
| 1982 | 2001 | const new_imp: types.Import = .{ |
| 1983 | | .module_name = try wasm.string_table.put(gpa, object.string_table.get(import.module_name)), |
| 1984 | | .name = try wasm.string_table.put(gpa, object.string_table.get(import.name)), |
| 2002 | .module_name = try wasm.string_table.put(gpa, obj_file.string(import.module_name)), |
| 2003 | .name = try wasm.string_table.put(gpa, obj_file.string(import.name)), |
| 1985 | 2004 | .kind = import.kind, |
| 1986 | 2005 | }; |
| 1987 | 2006 | // TODO: De-duplicate imports when they contain the same names and type |
| ... | ... | @@ -2032,28 +2051,23 @@ fn mergeSections(wasm: *Wasm) !void { |
| 2032 | 2051 | defer removed_duplicates.deinit(); |
| 2033 | 2052 | |
| 2034 | 2053 | for (wasm.resolved_symbols.keys()) |sym_loc| { |
| 2035 | | if (sym_loc.file == null) { |
| 2054 | const file_index = sym_loc.file orelse { |
| 2036 | 2055 | // Zig code-generated symbols are already within the sections and do not |
| 2037 | 2056 | // require to be merged |
| 2038 | 2057 | continue; |
| 2039 | | } |
| 2058 | }; |
| 2040 | 2059 | |
| 2041 | | const object = &wasm.objects.items[sym_loc.file.?]; |
| 2042 | | const symbol = &object.symtable[sym_loc.index]; |
| 2060 | const obj_file = wasm.file(@enumFromInt(file_index)).?; |
| 2061 | const symbol = obj_file.symbol[sym_loc.index]; |
| 2043 | 2062 | |
| 2044 | | if (symbol.isDead() or |
| 2045 | | symbol.isUndefined() or |
| 2046 | | (symbol.tag != .function and symbol.tag != .global and symbol.tag != .table)) |
| 2047 | | { |
| 2063 | if (symbol.isDead() or symbol.isUndefined()) { |
| 2048 | 2064 | // Skip undefined symbols as they go in the `import` section |
| 2049 | | // Also skip symbols that do not need to have a section merged. |
| 2050 | 2065 | continue; |
| 2051 | 2066 | } |
| 2052 | 2067 | |
| 2053 | | const offset = object.importedCountByKind(symbol.tag.externalType()); |
| 2054 | | const index = symbol.index - offset; |
| 2055 | 2068 | switch (symbol.tag) { |
| 2056 | 2069 | .function => { |
| 2070 | const index = symbol.index - obj_file.importedFunctions(); |
| 2057 | 2071 | const gop = try wasm.functions.getOrPut( |
| 2058 | 2072 | gpa, |
| 2059 | 2073 | .{ .file = sym_loc.file, .index = symbol.index }, |
| ... | ... | @@ -2071,20 +2085,24 @@ fn mergeSections(wasm: *Wasm) !void { |
| 2071 | 2085 | try removed_duplicates.append(sym_loc); |
| 2072 | 2086 | continue; |
| 2073 | 2087 | } |
| 2074 | | gop.value_ptr.* = .{ .func = object.functions[index], .sym_index = sym_loc.index }; |
| 2088 | gop.value_ptr.* = .{ .func = obj_file.functions()[index], .sym_index = sym_loc.index }; |
| 2075 | 2089 | symbol.index = @as(u32, @intCast(gop.index)) + wasm.imported_functions_count; |
| 2076 | 2090 | }, |
| 2077 | 2091 | .global => { |
| 2078 | | const original_global = object.globals[index]; |
| 2092 | const index = symbol.index - obj_file.importedFunctions(); |
| 2093 | const original_global = obj_file.globals()[index]; |
| 2079 | 2094 | symbol.index = @as(u32, @intCast(wasm.wasm_globals.items.len)) + wasm.imported_globals_count; |
| 2080 | 2095 | try wasm.wasm_globals.append(gpa, original_global); |
| 2081 | 2096 | }, |
| 2082 | 2097 | .table => { |
| 2083 | | const original_table = object.tables[index]; |
| 2098 | const index = symbol.index - obj_file.importedFunctions(); |
| 2099 | // assert it's a regular relocatable object file as `ZigObject` will never |
| 2100 | // contain a table. |
| 2101 | const original_table = obj_file.object.tables[index]; |
| 2084 | 2102 | symbol.index = @as(u32, @intCast(wasm.tables.items.len)) + wasm.imported_tables_count; |
| 2085 | 2103 | try wasm.tables.append(gpa, original_table); |
| 2086 | 2104 | }, |
| 2087 | | else => unreachable, |
| 2105 | else => continue, |
| 2088 | 2106 | } |
| 2089 | 2107 | } |
| 2090 | 2108 | |
| ... | ... | @@ -2111,12 +2129,13 @@ fn mergeTypes(wasm: *Wasm) !void { |
| 2111 | 2129 | defer dirty.deinit(); |
| 2112 | 2130 | |
| 2113 | 2131 | for (wasm.resolved_symbols.keys()) |sym_loc| { |
| 2114 | | if (sym_loc.file == null) { |
| 2132 | const file_index = sym_loc.file orelse { |
| 2115 | 2133 | // zig code-generated symbols are already present in final type section |
| 2116 | 2134 | continue; |
| 2117 | | } |
| 2118 | | const object = wasm.objects.items[sym_loc.file.?]; |
| 2119 | | const symbol = object.symtable[sym_loc.index]; |
| 2135 | }; |
| 2136 | |
| 2137 | const obj_file = wasm.file(@enumFromInt(file_index)).?; |
| 2138 | const symbol = obj_file.symbol(sym_loc.index); |
| 2120 | 2139 | if (symbol.tag != .function or symbol.isDead()) { |
| 2121 | 2140 | // Only functions have types. Only retrieve the type of referenced functions. |
| 2122 | 2141 | continue; |
| ... | ... | @@ -2125,12 +2144,12 @@ fn mergeTypes(wasm: *Wasm) !void { |
| 2125 | 2144 | if (symbol.isUndefined()) { |
| 2126 | 2145 | log.debug("Adding type from extern function '{s}'", .{sym_loc.getName(wasm)}); |
| 2127 | 2146 | const import: *types.Import = wasm.imports.getPtr(sym_loc) orelse continue; |
| 2128 | | const original_type = object.func_types[import.kind.function]; |
| 2147 | const original_type = obj_file.funcTypes()[import.kind.function]; |
| 2129 | 2148 | import.kind.function = try wasm.putOrGetFuncType(original_type); |
| 2130 | 2149 | } else if (!dirty.contains(symbol.index)) { |
| 2131 | 2150 | log.debug("Adding type from function '{s}'", .{sym_loc.getName(wasm)}); |
| 2132 | 2151 | const func = &wasm.functions.values()[symbol.index - wasm.imported_functions_count].func; |
| 2133 | | func.type_index = try wasm.putOrGetFuncType(object.func_types[func.type_index]); |
| 2152 | func.type_index = try wasm.putOrGetFuncType(obj_file.funcTypes()[func.type_index]); |
| 2134 | 2153 | dirty.putAssumeCapacityNoClobber(symbol.index, {}); |
| 2135 | 2154 | } |
| 2136 | 2155 | } |
| ... | ... | @@ -2240,11 +2259,18 @@ fn setupMemory(wasm: *Wasm) !void { |
| 2240 | 2259 | |
| 2241 | 2260 | const is_obj = comp.config.output_mode == .Obj; |
| 2242 | 2261 | |
| 2262 | const stack_ptr = if (wasm.findGlobalSymbol("__stack_pointer")) |loc| index: { |
| 2263 | const sym = loc.getSymbol(wasm); |
| 2264 | break :index sym.index - wasm.imported_globals_count; |
| 2265 | } else null; |
| 2266 | |
| 2243 | 2267 | if (place_stack_first and !is_obj) { |
| 2244 | 2268 | memory_ptr = stack_alignment.forward(memory_ptr); |
| 2245 | 2269 | memory_ptr += wasm.base.stack_size; |
| 2246 | 2270 | // We always put the stack pointer global at index 0 |
| 2247 | | wasm.wasm_globals.items[0].init.i32_const = @as(i32, @bitCast(@as(u32, @intCast(memory_ptr)))); |
| 2271 | if (stack_ptr) |index| { |
| 2272 | wasm.wasm_globals.items[index].init.i32_const = @as(i32, @bitCast(@as(u32, @intCast(memory_ptr)))); |
| 2273 | } |
| 2248 | 2274 | } |
| 2249 | 2275 | |
| 2250 | 2276 | var offset: u32 = @as(u32, @intCast(memory_ptr)); |
| ... | ... | @@ -2290,7 +2316,9 @@ fn setupMemory(wasm: *Wasm) !void { |
| 2290 | 2316 | if (!place_stack_first and !is_obj) { |
| 2291 | 2317 | memory_ptr = stack_alignment.forward(memory_ptr); |
| 2292 | 2318 | memory_ptr += wasm.base.stack_size; |
| 2293 | | wasm.wasm_globals.items[0].init.i32_const = @as(i32, @bitCast(@as(u32, @intCast(memory_ptr)))); |
| 2319 | if (stack_ptr) |index| { |
| 2320 | wasm.wasm_globals.items[index].init.i32_const = @as(i32, @bitCast(@as(u32, @intCast(memory_ptr)))); |
| 2321 | } |
| 2294 | 2322 | } |
| 2295 | 2323 | |
| 2296 | 2324 | // One of the linked object files has a reference to the __heap_base symbol. |
| ... | ... | @@ -2355,17 +2383,17 @@ fn setupMemory(wasm: *Wasm) !void { |
| 2355 | 2383 | /// From a given object's index and the index of the segment, returns the corresponding |
| 2356 | 2384 | /// index of the segment within the final data section. When the segment does not yet |
| 2357 | 2385 | /// exist, a new one will be initialized and appended. The new index will be returned in that case. |
| 2358 | | pub fn getMatchingSegment(wasm: *Wasm, object_index: u16, symbol_index: u32) !u32 { |
| 2386 | pub fn getMatchingSegment(wasm: *Wasm, file_index: File.Index, symbol_index: u32) !u32 { |
| 2359 | 2387 | const comp = wasm.base.comp; |
| 2360 | 2388 | const gpa = comp.gpa; |
| 2361 | | const object: Object = wasm.objects.items[object_index]; |
| 2362 | | const symbol = object.symtable[symbol_index]; |
| 2389 | const obj_file = wasm.file(file_index).?; |
| 2390 | const symbol = obj_file.symbols()[symbol_index]; |
| 2363 | 2391 | const index: u32 = @intCast(wasm.segments.items.len); |
| 2364 | 2392 | const shared_memory = comp.config.shared_memory; |
| 2365 | 2393 | |
| 2366 | 2394 | switch (symbol.tag) { |
| 2367 | 2395 | .data => { |
| 2368 | | const segment_info = object.segment_info[symbol.index]; |
| 2396 | const segment_info = obj_file.segmentInfo()[symbol.index]; |
| 2369 | 2397 | const merge_segment = comp.config.output_mode != .Obj; |
| 2370 | 2398 | const result = try wasm.data_segments.getOrPut(gpa, segment_info.outputName(merge_segment)); |
| 2371 | 2399 | if (!result.found_existing) { |
| ... | ... | @@ -2394,7 +2422,7 @@ pub fn getMatchingSegment(wasm: *Wasm, object_index: u16, symbol_index: u32) !u3 |
| 2394 | 2422 | break :blk index; |
| 2395 | 2423 | }, |
| 2396 | 2424 | .section => { |
| 2397 | | const section_name = object.string_table.get(symbol.name); |
| 2425 | const section_name = file.symbolName(symbol.index); |
| 2398 | 2426 | if (mem.eql(u8, section_name, ".debug_info")) { |
| 2399 | 2427 | return wasm.debug_info_index orelse blk: { |
| 2400 | 2428 | wasm.debug_info_index = index; |
| ... | ... | @@ -4291,12 +4319,10 @@ fn markReferences(wasm: *Wasm) !void { |
| 4291 | 4319 | // Debug sections may require to be parsed and marked when it contains |
| 4292 | 4320 | // relocations to alive symbols. |
| 4293 | 4321 | if (sym.tag == .section and comp.config.debug_format != .strip) { |
| 4294 | | const file = sym_loc.file orelse continue; // Incremental debug info is done independently |
| 4295 | | const object = &wasm.objects.items[file]; |
| 4296 | | const atom_index = try Object.parseSymbolIntoAtom(object, file, sym_loc.index, wasm); |
| 4297 | | const atom = wasm.getAtom(atom_index); |
| 4298 | | const atom_sym = atom.symbolLoc().getSymbol(wasm); |
| 4299 | | atom_sym.mark(); |
| 4322 | const file_index = sym_loc.file orelse continue; // Incremental debug info is done independently |
| 4323 | const obj_file = wasm.file(@enumFromInt(file_index)).?; |
| 4324 | _ = try obj_file.parseSymbolIntoAtom(wasm, sym_loc.index); |
| 4325 | sym.mark(); |
| 4300 | 4326 | } |
| 4301 | 4327 | } |
| 4302 | 4328 | } |
| ... | ... | @@ -4319,9 +4345,8 @@ fn mark(wasm: *Wasm, loc: SymbolLoc) !void { |
| 4319 | 4345 | } |
| 4320 | 4346 | |
| 4321 | 4347 | const atom_index = if (loc.file) |file_index| idx: { |
| 4322 | | const object = &wasm.objects.items[file_index]; |
| 4323 | | const atom_index = try object.parseSymbolIntoAtom(file_index, loc.index, wasm); |
| 4324 | | break :idx atom_index; |
| 4348 | const obj_file = wasm.file(@enumFromInt(file_index)).?; |
| 4349 | break :idx try obj_file.parseSymbolIntoAtom(wasm, loc.index); |
| 4325 | 4350 | } else wasm.symbol_atom.get(loc) orelse return; |
| 4326 | 4351 | |
| 4327 | 4352 | const atom = wasm.getAtom(atom_index); |