authorgravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2022-02-17 22:01:12+01:00
committergravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2022-02-23 16:07:36+01:00
logdaf741318e51c9eea38cad80c996536093f0fcef
treed0f067e7a19e1fda6baaf0325c7f034f4a7becc9
parentf7f3678b485fff790af278ff5436e0b63db998f8

wasm-linker: Emit segment info

When creating a relocatable object file, also emit the segment information

1 files changed, 27 insertions(+), 1 deletions(-)

src/link/Wasm.zig+27-1
...@@ -2049,6 +2049,7 @@ fn emitLinkSection(self: *Wasm, file: fs.File, arena: Allocator) !void {...@@ -2049,6 +2049,7 @@ fn emitLinkSection(self: *Wasm, file: fs.File, arena: Allocator) !void {
2049 // For each subsection type (found in types.Subsection) we can emit a section.2049 // For each subsection type (found in types.Subsection) we can emit a section.
2050 // Currently, we only support emitting segment info and the symbol table.2050 // Currently, we only support emitting segment info and the symbol table.
2051 try self.emitSymbolTable(file, arena);2051 try self.emitSymbolTable(file, arena);
2052 try self.emitSegmentInfo(file, arena);
20522053
2053 const size = @intCast(u32, (try file.getPos()) - offset - 6);2054 const size = @intCast(u32, (try file.getPos()) - offset - 6);
2054 try writeCustomSectionHeader(file, offset, size);2055 try writeCustomSectionHeader(file, offset, size);
...@@ -2064,7 +2065,6 @@ fn emitSymbolTable(self: *Wasm, file: fs.File, arena: Allocator) !void {...@@ -2064,7 +2065,6 @@ fn emitSymbolTable(self: *Wasm, file: fs.File, arena: Allocator) !void {
2064 try leb.writeULEB128(file.writer(), @enumToInt(types.SubsectionType.WASM_SYMBOL_TABLE));2065 try leb.writeULEB128(file.writer(), @enumToInt(types.SubsectionType.WASM_SYMBOL_TABLE));
20652066
2066 var symbol_count: u32 = 0;2067 var symbol_count: u32 = 0;
2067
2068 var atom_it = self.atoms.valueIterator();2068 var atom_it = self.atoms.valueIterator();
2069 while (atom_it.next()) |next_atom| {2069 while (atom_it.next()) |next_atom| {
2070 var atom: ?*Atom = next_atom.*.getFirst();2070 var atom: ?*Atom = next_atom.*.getFirst();
...@@ -2073,6 +2073,7 @@ fn emitSymbolTable(self: *Wasm, file: fs.File, arena: Allocator) !void {...@@ -2073,6 +2073,7 @@ fn emitSymbolTable(self: *Wasm, file: fs.File, arena: Allocator) !void {
2073 const symbol = sym_loc.getSymbol(self).*;2073 const symbol = sym_loc.getSymbol(self).*;
2074 if (symbol.tag == .dead) continue; // Do not emit dead symbols2074 if (symbol.tag == .dead) continue; // Do not emit dead symbols
2075 symbol_count += 1;2075 symbol_count += 1;
2076 log.debug("Emit symbol: {}", .{symbol});
2076 try leb.writeULEB128(writer, @enumToInt(symbol.tag));2077 try leb.writeULEB128(writer, @enumToInt(symbol.tag));
2077 try leb.writeULEB128(writer, symbol.flags);2078 try leb.writeULEB128(writer, symbol.flags);
20782079
...@@ -2116,6 +2117,31 @@ fn emitSymbolTable(self: *Wasm, file: fs.File, arena: Allocator) !void {...@@ -2116,6 +2117,31 @@ fn emitSymbolTable(self: *Wasm, file: fs.File, arena: Allocator) !void {
2116 try file.writevAll(&.{iovec});2117 try file.writevAll(&.{iovec});
2117}2118}
21182119
2120fn emitSegmentInfo(self: *Wasm, file: fs.File, arena: Allocator) !void {
2121 var payload = std.ArrayList(u8).init(arena);
2122 const writer = payload.writer();
2123 try leb.writeULEB128(file.writer(), @enumToInt(types.SubsectionType.WASM_SEGMENT_INFO));
2124 try leb.writeULEB128(writer, @intCast(u32, self.segment_info.items.len));
2125 for (self.segment_info.items) |segment_info| {
2126 log.debug("Emit segment: {s} align({d}) flags({b})", .{
2127 segment_info.name,
2128 @ctz(u32, segment_info.alignment),
2129 segment_info.flags,
2130 });
2131 try leb.writeULEB128(writer, @intCast(u32, segment_info.name.len));
2132 try writer.writeAll(segment_info.name);
2133 try leb.writeULEB128(writer, @ctz(u32, segment_info.alignment));
2134 try leb.writeULEB128(writer, segment_info.flags);
2135 }
2136
2137 try leb.writeULEB128(file.writer(), @intCast(u32, payload.items.len));
2138 const iovec: std.os.iovec_const = .{
2139 .iov_base = payload.items.ptr,
2140 .iov_len = payload.items.len,
2141 };
2142 try file.writevAll(&.{iovec});
2143}
2144
2119/// Searches for an a matching function signature, when not found2145/// Searches for an a matching function signature, when not found
2120/// a new entry will be made. The index of the existing/new signature will be returned.2146/// a new entry will be made. The index of the existing/new signature will be returned.
2121pub fn putOrGetFuncType(self: *Wasm, func_type: wasm.Type) !u32 {2147pub fn putOrGetFuncType(self: *Wasm, func_type: wasm.Type) !u32 {