authorgravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2022-02-18 21:47:57+01:00
committergravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2022-02-23 16:07:36+01:00
log2b0431a8d3933a46fecba2bf064bf45df54a111d
tree1ec5e1afa83d482e751e603488e8bf19cc7fa8bc
parentdaf741318e51c9eea38cad80c996536093f0fcef

wasm-linker: Do not merge data segments for obj

When creating a relocatable object file, we do no longer perform the following actions: - Merge data segments - Calculate stack size - Relocations We now also make the stack pointer symbol `undefined` for this use case as well as add the symbol as an import.

3 files changed, 129 insertions(+), 93 deletions(-)

src/link/Wasm.zig+114-80
......@@ -163,14 +163,6 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option
163163 try file.writeAll(&(wasm.magic ++ wasm.version));
164164
165165 // As sym_index '0' is reserved, we use it for our stack pointer symbol
166 const global = try wasm_bin.wasm_globals.addOne(allocator);
167 global.* = .{
168 .global_type = .{
169 .valtype = .i32,
170 .mutable = true,
171 },
172 .init = .{ .i32_const = 0 },
173 };
174166 const symbol = try wasm_bin.symbols.addOne(allocator);
175167 symbol.* = .{
176168 .name = "__stack_pointer",
......@@ -178,6 +170,28 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option
178170 .flags = 0,
179171 .index = 0,
180172 };
173 // For object files we will import the stack pointer symbol
174 if (options.output_mode == .Obj) {
175 symbol.setUndefined(true);
176 try wasm_bin.imports.putNoClobber(
177 allocator,
178 .{ .file = null, .index = 0 },
179 .{
180 .module_name = wasm_bin.host_name,
181 .name = "__stack_pointer",
182 .kind = .{ .global = .{ .valtype = .i32, .mutable = true } },
183 },
184 );
185 } else {
186 const global = try wasm_bin.wasm_globals.addOne(allocator);
187 global.* = .{
188 .global_type = .{
189 .valtype = .i32,
190 .mutable = true,
191 },
192 .init = .{ .i32_const = 0 },
193 };
194 }
181195 return wasm_bin;
182196}
183197
......@@ -651,36 +665,50 @@ fn parseAtom(self: *Wasm, atom: *Atom, kind: Kind) !void {
651665 break :result self.code_section_index.?;
652666 },
653667 .data => result: {
654 const gop = try self.data_segments.getOrPut(self.base.allocator, ".rodata");
655 const atom_index = if (gop.found_existing) blk: {
656 self.segments.items[gop.value_ptr.*].size += atom.size;
657 break :blk gop.value_ptr.*;
658 } else blk: {
659 const index = @intCast(u32, self.segments.items.len);
660 try self.segments.append(self.base.allocator, .{
661 .alignment = atom.alignment,
662 .size = 0,
663 .offset = 0,
664 });
665 gop.value_ptr.* = index;
666 break :blk index;
667 };
668 const info_index = @intCast(u32, self.segment_info.items.len);
669668 // TODO: Add mutables global decls to .bss section instead
670669 const segment_name = try std.mem.concat(self.base.allocator, u8, &.{
671670 ".rodata.",
672671 std.mem.span(symbol.name),
673672 });
674673 errdefer self.base.allocator.free(segment_name);
675 try self.segment_info.append(self.base.allocator, .{
674 const segment_info: types.Segment = .{
676675 .name = segment_name,
677676 .alignment = atom.alignment,
678677 .flags = 0,
679 });
678 };
680679 symbol.tag = .data;
681 symbol.index = info_index;
682680
683 break :result atom_index;
681 const should_merge = self.base.options.output_mode != .Obj;
682 const gop = try self.data_segments.getOrPut(self.base.allocator, segment_info.outputName(should_merge));
683 if (gop.found_existing) {
684 const index = gop.value_ptr.*;
685 self.segments.items[index].size += atom.size;
686
687 // segment indexes can be off by 1 due to also containing a segment
688 // for the code section, so we must check if the existing segment
689 // is larger than that of the code section, and substract the index by 1 in such case.
690 const info_add = if (self.code_section_index) |idx| blk: {
691 if (idx < index) break :blk @as(u32, 1);
692 break :blk 0;
693 } else @as(u32, 0);
694 symbol.index = index - info_add;
695 // segment info already exists, so free its memory
696 self.base.allocator.free(segment_name);
697 break :result index;
698 } else {
699 const index = @intCast(u32, self.segments.items.len);
700 try self.segments.append(self.base.allocator, .{
701 .alignment = atom.alignment,
702 .size = 0,
703 .offset = 0,
704 });
705 gop.value_ptr.* = index;
706
707 const info_index = @intCast(u32, self.segment_info.items.len);
708 try self.segment_info.append(self.base.allocator, segment_info);
709 symbol.index = info_index;
710 break :result index;
711 }
684712 },
685713 };
686714
......@@ -932,7 +960,9 @@ fn setupMemory(self: *Wasm) !void {
932960 break :blk base;
933961 } else 0;
934962
935 if (place_stack_first) {
963 const is_obj = self.base.options.output_mode == .Obj;
964
965 if (place_stack_first and !is_obj) {
936966 memory_ptr = std.mem.alignForwardGeneric(u64, memory_ptr, stack_alignment);
937967 memory_ptr += stack_size;
938968 // We always put the stack pointer global at index 0
......@@ -951,7 +981,7 @@ fn setupMemory(self: *Wasm) !void {
951981 offset += segment.size;
952982 }
953983
954 if (!place_stack_first) {
984 if (!place_stack_first and !is_obj) {
955985 memory_ptr = std.mem.alignForwardGeneric(u64, memory_ptr, stack_alignment);
956986 memory_ptr += stack_size;
957987 self.wasm_globals.items[0].init.i32_const = @bitCast(i32, @intCast(u32, memory_ptr));
......@@ -1011,7 +1041,8 @@ pub fn getMatchingSegment(self: *Wasm, object_index: u16, relocatable_index: u32
10111041 switch (relocatable_data.type) {
10121042 .data => {
10131043 const segment_info = object.segment_info[relocatable_data.index];
1014 const result = try self.data_segments.getOrPut(self.base.allocator, segment_info.outputName());
1044 const merge_segment = self.base.options.output_mode != .Obj;
1045 const result = try self.data_segments.getOrPut(self.base.allocator, segment_info.outputName(merge_segment));
10151046 if (!result.found_existing) {
10161047 result.value_ptr.* = index;
10171048 try self.segments.append(self.base.allocator, .{
......@@ -1368,7 +1399,9 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void {
13681399 const writer = file.writer();
13691400 var atom: *Atom = self.atoms.get(code_index).?.getFirst();
13701401 while (true) {
1371 try atom.resolveRelocs(self);
1402 if (!is_obj) {
1403 try atom.resolveRelocs(self);
1404 }
13721405 try leb.writeULEB128(writer, atom.size);
13731406 try writer.writeAll(atom.code.items);
13741407 atom = atom.next orelse break;
......@@ -1390,8 +1423,9 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void {
13901423 var it = self.data_segments.iterator();
13911424 var segment_count: u32 = 0;
13921425 while (it.next()) |entry| {
1393 // do not output 'bss' section
1394 if (std.mem.eql(u8, entry.key_ptr.*, ".bss")) continue;
1426 // do not output 'bss' section unless we import memory and therefore
1427 // want to guarantee the data is zero initialized
1428 if (std.mem.eql(u8, entry.key_ptr.*, ".bss") and !import_memory) continue;
13951429 segment_count += 1;
13961430 const atom_index = entry.value_ptr.*;
13971431 var atom: *Atom = self.atoms.getPtr(atom_index).?.*.getFirst();
......@@ -1406,7 +1440,9 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void {
14061440 // fill in the offset table and the data segments
14071441 var current_offset: u32 = 0;
14081442 while (true) {
1409 try atom.resolveRelocs(self);
1443 if (!is_obj) {
1444 try atom.resolveRelocs(self);
1445 }
14101446
14111447 // Pad with zeroes to ensure all segments are aligned
14121448 if (current_offset != atom.offset) {
......@@ -1443,60 +1479,58 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void {
14431479 );
14441480 }
14451481
1446 // Custom section "name" which contains symbol names
1447 if (!is_obj) {
1448 const Name = struct {
1449 index: u32,
1450 name: []const u8,
1451
1452 fn lessThan(context: void, lhs: @This(), rhs: @This()) bool {
1453 _ = context;
1454 return lhs.index < rhs.index;
1455 }
1456 };
1482 if (is_obj) {
1483 try self.emitLinkSection(file, arena);
1484 } else {
1485 try self.emitNameSection(file, arena);
1486 }
1487}
14571488
1458 var funcs = try std.ArrayList(Name).initCapacity(self.base.allocator, self.functions.items.len + self.imported_functions_count);
1459 defer funcs.deinit();
1460 var globals = try std.ArrayList(Name).initCapacity(self.base.allocator, self.wasm_globals.items.len);
1461 defer globals.deinit();
1462 var segments = try std.ArrayList(Name).initCapacity(self.base.allocator, self.data_segments.count());
1463 defer segments.deinit();
1489fn emitNameSection(self: *Wasm, file: fs.File, arena: Allocator) !void {
1490 const Name = struct {
1491 index: u32,
1492 name: []const u8,
14641493
1465 for (self.resolved_symbols.keys()) |sym_loc| {
1466 const symbol = sym_loc.getSymbol(self).*;
1467 switch (symbol.tag) {
1468 .function => funcs.appendAssumeCapacity(.{ .index = symbol.index, .name = mem.sliceTo(symbol.name, 0) }),
1469 .global => globals.appendAssumeCapacity(.{ .index = symbol.index, .name = mem.sliceTo(symbol.name, 0) }),
1470 else => {},
1471 }
1472 }
1473 // data segments are already 'ordered'
1474 for (self.data_segments.keys()) |key, index| {
1475 segments.appendAssumeCapacity(.{ .index = @intCast(u32, index), .name = key });
1494 fn lessThan(context: void, lhs: @This(), rhs: @This()) bool {
1495 _ = context;
1496 return lhs.index < rhs.index;
14761497 }
1498 };
14771499
1478 std.sort.sort(Name, funcs.items, {}, Name.lessThan);
1479 std.sort.sort(Name, globals.items, {}, Name.lessThan);
1500 var funcs = try std.ArrayList(Name).initCapacity(arena, self.functions.items.len + self.imported_functions_count);
1501 var globals = try std.ArrayList(Name).initCapacity(arena, self.wasm_globals.items.len);
1502 var segments = try std.ArrayList(Name).initCapacity(arena, self.data_segments.count());
14801503
1481 const header_offset = try reserveCustomSectionHeader(file);
1482 const writer = file.writer();
1483 try leb.writeULEB128(writer, @intCast(u32, "name".len));
1484 try writer.writeAll("name");
1504 for (self.resolved_symbols.keys()) |sym_loc| {
1505 const symbol = sym_loc.getSymbol(self).*;
1506 switch (symbol.tag) {
1507 .function => funcs.appendAssumeCapacity(.{ .index = symbol.index, .name = mem.sliceTo(symbol.name, 0) }),
1508 .global => globals.appendAssumeCapacity(.{ .index = symbol.index, .name = mem.sliceTo(symbol.name, 0) }),
1509 else => {},
1510 }
1511 }
1512 // data segments are already 'ordered'
1513 for (self.data_segments.keys()) |key, index| {
1514 segments.appendAssumeCapacity(.{ .index = @intCast(u32, index), .name = key });
1515 }
14851516
1486 try self.emitNameSubsection(.function, funcs.items, writer);
1487 try self.emitNameSubsection(.global, globals.items, writer);
1488 try self.emitNameSubsection(.data_segment, segments.items, writer);
1517 std.sort.sort(Name, funcs.items, {}, Name.lessThan);
1518 std.sort.sort(Name, globals.items, {}, Name.lessThan);
14891519
1490 try writeCustomSectionHeader(
1491 file,
1492 header_offset,
1493 @intCast(u32, (try file.getPos()) - header_offset - header_size),
1494 );
1495 }
1520 const header_offset = try reserveCustomSectionHeader(file);
1521 const writer = file.writer();
1522 try leb.writeULEB128(writer, @intCast(u32, "name".len));
1523 try writer.writeAll("name");
14961524
1497 if (is_obj) {
1498 try self.emitLinkSection(file, arena);
1499 }
1525 try self.emitNameSubsection(.function, funcs.items, writer);
1526 try self.emitNameSubsection(.global, globals.items, writer);
1527 try self.emitNameSubsection(.data_segment, segments.items, writer);
1528
1529 try writeCustomSectionHeader(
1530 file,
1531 header_offset,
1532 @intCast(u32, (try file.getPos()) - header_offset - 6),
1533 );
15001534}
15011535
15021536fn emitNameSubsection(self: *Wasm, section_id: std.wasm.NameSubsection, names: anytype, writer: anytype) !void {
src/link/Wasm/Atom.zig+13-12
......@@ -147,33 +147,34 @@ pub fn resolveRelocs(self: *Atom, wasm_bin: *const Wasm) !void {
147147fn relocationValue(self: Atom, relocation: types.Relocation, wasm_bin: *const Wasm) !u64 {
148148 const target_loc: Wasm.SymbolLoc = .{ .file = self.file, .index = relocation.index };
149149 const symbol = target_loc.getSymbol(wasm_bin).*;
150 return switch (relocation.relocation_type) {
151 .R_WASM_FUNCTION_INDEX_LEB => symbol.index,
152 .R_WASM_TABLE_NUMBER_LEB => symbol.index,
150 switch (relocation.relocation_type) {
151 .R_WASM_FUNCTION_INDEX_LEB => return symbol.index,
152 .R_WASM_TABLE_NUMBER_LEB => return symbol.index,
153153 .R_WASM_TABLE_INDEX_I32,
154154 .R_WASM_TABLE_INDEX_I64,
155155 .R_WASM_TABLE_INDEX_SLEB,
156156 .R_WASM_TABLE_INDEX_SLEB64,
157157 => return wasm_bin.function_table.get(relocation.index) orelse 0,
158 .R_WASM_TYPE_INDEX_LEB => wasm_bin.functions.items[symbol.index].type_index,
158 .R_WASM_TYPE_INDEX_LEB => return wasm_bin.functions.items[symbol.index].type_index,
159159 .R_WASM_GLOBAL_INDEX_I32,
160160 .R_WASM_GLOBAL_INDEX_LEB,
161 => symbol.index,
161 => return symbol.index,
162162 .R_WASM_MEMORY_ADDR_I32,
163163 .R_WASM_MEMORY_ADDR_I64,
164164 .R_WASM_MEMORY_ADDR_LEB,
165165 .R_WASM_MEMORY_ADDR_LEB64,
166166 .R_WASM_MEMORY_ADDR_SLEB,
167167 .R_WASM_MEMORY_ADDR_SLEB64,
168 => blk: {
168 => {
169169 if (symbol.isUndefined() and (symbol.tag == .data or symbol.isWeak())) {
170170 return 0;
171171 }
172 const segment_name = wasm_bin.segment_info.items[symbol.index].outputName();
172 const merge_segment = wasm_bin.base.options.output_mode != .Obj;
173 const segment_name = wasm_bin.segment_info.items[symbol.index].outputName(merge_segment);
173174 const atom_index = wasm_bin.data_segments.get(segment_name).?;
174175 var target_atom = wasm_bin.atoms.getPtr(atom_index).?.*.getFirst();
175176 while (true) {
176 // TODO: Can we simplify this by providing the ability to find and atom
177 // TODO: Can we simplify this by providing the ability to find an atom
177178 // based on a symbol location.
178179 if (target_atom.sym_index == relocation.index) {
179180 if (target_atom.file) |file| {
......@@ -183,11 +184,11 @@ fn relocationValue(self: Atom, relocation: types.Relocation, wasm_bin: *const Wa
183184 target_atom = target_atom.next orelse break;
184185 }
185186 const segment = wasm_bin.segments.items[atom_index];
186 break :blk target_atom.offset + segment.offset + (relocation.addend orelse 0);
187 return target_atom.offset + segment.offset + (relocation.addend orelse 0);
187188 },
188 .R_WASM_EVENT_INDEX_LEB => symbol.index,
189 .R_WASM_EVENT_INDEX_LEB => return symbol.index,
189190 .R_WASM_SECTION_OFFSET_I32,
190191 .R_WASM_FUNCTION_OFFSET_I32,
191 => relocation.offset,
192 };
192 => return relocation.offset,
193 }
193194}
src/link/Wasm/types.zig+2-1
......@@ -93,7 +93,8 @@ pub const Segment = struct {
9393 /// Bitfield containing flags for a segment
9494 flags: u32,
9595
96 pub fn outputName(self: Segment) []const u8 {
96 pub fn outputName(self: Segment, merge_segments: bool) []const u8 {
97 if (!merge_segments) return self.name;
9798 if (std.mem.startsWith(u8, self.name, ".rodata.")) {
9899 return ".rodata";
99100 } else if (std.mem.startsWith(u8, self.name, ".text.")) {