authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-01-15 18:19:46-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-01-15 18:19:46-08:00
logdcb4134835e0c0bb0c130731efec741b41aba522
treea6ca84efebf4d3b448a0836dd109bed111bc1c0e
parent9dd6efb7e422ddc54ca34dc76b0d9efab746f8bf

wasm linker: delete commented out code

this logic has not yet been ported to the new design, but the logic is safe and sound in the git history and does not need to also live as commented out code

1 files changed, 0 insertions(+), 210 deletions(-)

src/link/Wasm/Flush.zig-210
......@@ -1008,15 +1008,6 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void {
10081008
10091009 if (is_obj) {
10101010 @panic("TODO emit link section for object file and emit modified relocations");
1011 //var symbol_table = std.AutoArrayHashMap(SymbolLoc, u32).init(arena);
1012 //try wasm.emitLinkSection(binary_bytes, &symbol_table);
1013 //if (code_section_index) |code_index| {
1014 // try wasm.emitCodeRelocations(binary_bytes, code_index, symbol_table);
1015 //}
1016 //if (data_section_index) |data_index| {
1017 // if (f.data_segments.count() > 0)
1018 // try wasm.emitDataRelocations(binary_bytes, data_index, symbol_table);
1019 //}
10201011 } else if (comp.config.debug_format != .strip) {
10211012 try emitNameSection(wasm, f.data_segment_groups.items, binary_bytes);
10221013 }
......@@ -1245,105 +1236,6 @@ fn emitProducerSection(gpa: Allocator, binary_bytes: *std.ArrayListUnmanaged(u8)
12451236 }
12461237}
12471238
1248///// For each relocatable section, emits a custom "relocation.<section_name>" section
1249//fn emitCodeRelocations(
1250// wasm: *Wasm,
1251// binary_bytes: *std.ArrayListUnmanaged(u8),
1252// section_index: u32,
1253// symbol_table: std.AutoArrayHashMapUnmanaged(SymbolLoc, u32),
1254//) !void {
1255// const comp = wasm.base.comp;
1256// const gpa = comp.gpa;
1257// const code_index = wasm.code_section_index.unwrap() orelse return;
1258// const writer = binary_bytes.writer(gpa);
1259// const header_offset = try reserveCustomSectionHeader(gpa, binary_bytes);
1260//
1261// // write custom section information
1262// const name = "reloc.CODE";
1263// try leb.writeUleb128(writer, @as(u32, @intCast(name.len)));
1264// try writer.writeAll(name);
1265// try leb.writeUleb128(writer, section_index);
1266// const reloc_start = binary_bytes.items.len;
1267//
1268// var count: u32 = 0;
1269// var atom: *Atom = wasm.atoms.get(code_index).?.ptr(wasm);
1270// // for each atom, we calculate the uleb size and append that
1271// var size_offset: u32 = 5; // account for code section size leb128
1272// while (true) {
1273// size_offset += getUleb128Size(atom.code.len);
1274// for (atom.relocSlice(wasm)) |relocation| {
1275// count += 1;
1276// const sym_loc: SymbolLoc = .{ .file = atom.file, .index = @enumFromInt(relocation.index) };
1277// const symbol_index = symbol_table.get(sym_loc).?;
1278// try leb.writeUleb128(writer, @intFromEnum(relocation.tag));
1279// const offset = atom.offset + relocation.offset + size_offset;
1280// try leb.writeUleb128(writer, offset);
1281// try leb.writeUleb128(writer, symbol_index);
1282// if (relocation.tag.addendIsPresent()) {
1283// try leb.writeIleb128(writer, relocation.addend);
1284// }
1285// log.debug("Emit relocation: {}", .{relocation});
1286// }
1287// if (atom.prev == .none) break;
1288// atom = atom.prev.ptr(wasm);
1289// }
1290// if (count == 0) return;
1291// var buf: [5]u8 = undefined;
1292// leb.writeUnsignedFixed(5, &buf, count);
1293// try binary_bytes.insertSlice(reloc_start, &buf);
1294// writeCustomSectionHeader(binary_bytes, header_offset);
1295//}
1296
1297//fn emitDataRelocations(
1298// wasm: *Wasm,
1299// binary_bytes: *std.ArrayList(u8),
1300// section_index: u32,
1301// symbol_table: std.AutoArrayHashMap(SymbolLoc, u32),
1302//) !void {
1303// const comp = wasm.base.comp;
1304// const gpa = comp.gpa;
1305// const writer = binary_bytes.writer(gpa);
1306// const header_offset = try reserveCustomSectionHeader(gpa, binary_bytes);
1307//
1308// // write custom section information
1309// const name = "reloc.DATA";
1310// try leb.writeUleb128(writer, @as(u32, @intCast(name.len)));
1311// try writer.writeAll(name);
1312// try leb.writeUleb128(writer, section_index);
1313// const reloc_start = binary_bytes.items.len;
1314//
1315// var count: u32 = 0;
1316// // for each atom, we calculate the uleb size and append that
1317// var size_offset: u32 = 5; // account for code section size leb128
1318// for (f.data_segments.values()) |segment_index| {
1319// var atom: *Atom = wasm.atoms.get(segment_index).?.ptr(wasm);
1320// while (true) {
1321// size_offset += getUleb128Size(atom.code.len);
1322// for (atom.relocSlice(wasm)) |relocation| {
1323// count += 1;
1324// const sym_loc: SymbolLoc = .{ .file = atom.file, .index = @enumFromInt(relocation.index) };
1325// const symbol_index = symbol_table.get(sym_loc).?;
1326// try leb.writeUleb128(writer, @intFromEnum(relocation.tag));
1327// const offset = atom.offset + relocation.offset + size_offset;
1328// try leb.writeUleb128(writer, offset);
1329// try leb.writeUleb128(writer, symbol_index);
1330// if (relocation.tag.addendIsPresent()) {
1331// try leb.writeIleb128(writer, relocation.addend);
1332// }
1333// log.debug("Emit relocation: {}", .{relocation});
1334// }
1335// if (atom.prev == .none) break;
1336// atom = atom.prev.ptr(wasm);
1337// }
1338// }
1339// if (count == 0) return;
1340//
1341// var buf: [5]u8 = undefined;
1342// leb.writeUnsignedFixed(5, &buf, count);
1343// try binary_bytes.insertSlice(reloc_start, &buf);
1344// writeCustomSectionHeader(binary_bytes, header_offset);
1345//}
1346
13471239fn splitSegmentName(name: []const u8) struct { []const u8, []const u8 } {
13481240 const start = @intFromBool(name.len >= 1 and name[0] == '.');
13491241 const pivot = mem.indexOfScalarPos(u8, name, start, '.') orelse name.len;
......@@ -1496,31 +1388,6 @@ pub fn emitExpr(wasm: *const Wasm, binary_bytes: *std.ArrayListUnmanaged(u8), ex
14961388 try binary_bytes.appendSlice(gpa, slice[0 .. slice.len + 1]); // +1 to include end opcode
14971389}
14981390
1499//fn emitLinkSection(
1500// wasm: *Wasm,
1501// binary_bytes: *std.ArrayListUnmanaged(u8),
1502// symbol_table: *std.AutoArrayHashMapUnmanaged(SymbolLoc, u32),
1503//) !void {
1504// const gpa = wasm.base.comp.gpa;
1505// const offset = try reserveCustomSectionHeader(gpa, binary_bytes);
1506// const writer = binary_bytes.writer(gpa);
1507// // emit "linking" custom section name
1508// const section_name = "linking";
1509// try leb.writeUleb128(writer, section_name.len);
1510// try writer.writeAll(section_name);
1511//
1512// // meta data version, which is currently '2'
1513// try leb.writeUleb128(writer, @as(u32, 2));
1514//
1515// // For each subsection type (found in Subsection) we can emit a section.
1516// // Currently, we only support emitting segment info and the symbol table.
1517// try wasm.emitSymbolTable(binary_bytes, symbol_table);
1518// try wasm.emitSegmentInfo(binary_bytes);
1519//
1520// const size: u32 = @intCast(binary_bytes.items.len - offset - 6);
1521// writeCustomSectionHeader(binary_bytes, offset, size);
1522//}
1523
15241391fn emitSegmentInfo(wasm: *Wasm, binary_bytes: *std.ArrayList(u8)) !void {
15251392 const gpa = wasm.base.comp.gpa;
15261393 const writer = binary_bytes.writer(gpa);
......@@ -1545,83 +1412,6 @@ fn emitSegmentInfo(wasm: *Wasm, binary_bytes: *std.ArrayList(u8)) !void {
15451412 try binary_bytes.insertSlice(segment_offset, &buf);
15461413}
15471414
1548//fn emitSymbolTable(
1549// wasm: *Wasm,
1550// binary_bytes: *std.ArrayListUnmanaged(u8),
1551// symbol_table: *std.AutoArrayHashMapUnmanaged(SymbolLoc, u32),
1552//) !void {
1553// const gpa = wasm.base.comp.gpa;
1554// const writer = binary_bytes.writer(gpa);
1555//
1556// try leb.writeUleb128(writer, @intFromEnum(SubsectionType.symbol_table));
1557// const table_offset = binary_bytes.items.len;
1558//
1559// var symbol_count: u32 = 0;
1560// for (wasm.resolved_symbols.keys()) |sym_loc| {
1561// const symbol = wasm.finalSymbolByLoc(sym_loc).*;
1562// if (symbol.tag == .dead) continue;
1563// try symbol_table.putNoClobber(gpa, sym_loc, symbol_count);
1564// symbol_count += 1;
1565// log.debug("emit symbol: {}", .{symbol});
1566// try leb.writeUleb128(writer, @intFromEnum(symbol.tag));
1567// try leb.writeUleb128(writer, symbol.flags);
1568//
1569// const sym_name = wasm.symbolLocName(sym_loc);
1570// switch (symbol.tag) {
1571// .data => {
1572// try leb.writeUleb128(writer, @as(u32, @intCast(sym_name.len)));
1573// try writer.writeAll(sym_name);
1574//
1575// if (!symbol.flags.undefined) {
1576// try leb.writeUleb128(writer, @intFromEnum(symbol.pointee.data_out));
1577// const atom_index = wasm.symbol_atom.get(sym_loc).?;
1578// const atom = wasm.getAtom(atom_index);
1579// try leb.writeUleb128(writer, @as(u32, atom.offset));
1580// try leb.writeUleb128(writer, @as(u32, atom.code.len));
1581// }
1582// },
1583// .section => {
1584// try leb.writeUleb128(writer, @intFromEnum(symbol.pointee.section));
1585// },
1586// .function => {
1587// if (symbol.flags.undefined) {
1588// try leb.writeUleb128(writer, @intFromEnum(symbol.pointee.function_import));
1589// } else {
1590// try leb.writeUleb128(writer, @intFromEnum(symbol.pointee.function));
1591// try leb.writeUleb128(writer, @as(u32, @intCast(sym_name.len)));
1592// try writer.writeAll(sym_name);
1593// }
1594// },
1595// .global => {
1596// if (symbol.flags.undefined) {
1597// try leb.writeUleb128(writer, @intFromEnum(symbol.pointee.global_import));
1598// } else {
1599// try leb.writeUleb128(writer, @intFromEnum(symbol.pointee.global));
1600// try leb.writeUleb128(writer, @as(u32, @intCast(sym_name.len)));
1601// try writer.writeAll(sym_name);
1602// }
1603// },
1604// .table => {
1605// if (symbol.flags.undefined) {
1606// try leb.writeUleb128(writer, @intFromEnum(symbol.pointee.table_import));
1607// } else {
1608// try leb.writeUleb128(writer, @intFromEnum(symbol.pointee.table));
1609// try leb.writeUleb128(writer, @as(u32, @intCast(sym_name.len)));
1610// try writer.writeAll(sym_name);
1611// }
1612// },
1613// .event => unreachable,
1614// .dead => unreachable,
1615// .uninitialized => unreachable,
1616// }
1617// }
1618//
1619// var buf: [10]u8 = undefined;
1620// leb.writeUnsignedFixed(5, buf[0..5], @intCast(binary_bytes.items.len - table_offset + 5));
1621// leb.writeUnsignedFixed(5, buf[5..], symbol_count);
1622// try binary_bytes.insertSlice(table_offset, &buf);
1623//}
1624
16251415fn uleb128size(x: u32) u32 {
16261416 var value = x;
16271417 var size: u32 = 0;