Commit 788b7f8f11
Changed files (3)
src
link
src/link/Wasm/Flush.zig
@@ -188,8 +188,6 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void {
for (wasm.object_indirect_function_set.keys()) |object_function_index|
f.indirect_function_table.putAssumeCapacity(.fromObjectFunction(wasm, object_function_index), {});
- // TODO only include init functions for objects with must_link=true or
- // which have any alive functions inside them.
if (wasm.object_init_funcs.items.len > 0) {
// Zig has no constructors so these are only for object file inputs.
mem.sortUnstable(Wasm.InitFunc, wasm.object_init_funcs.items, {}, Wasm.InitFunc.lessThan);
@@ -692,7 +690,7 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void {
}
// When the shared-memory option is enabled, we *must* emit the 'data count' section.
- if (f.data_segment_groups.items.len > 0 and shared_memory) {
+ {
const header_offset = try reserveVecSectionHeader(gpa, binary_bytes);
replaceVecSectionHeader(binary_bytes, header_offset, .data_count, @intCast(f.data_segment_groups.items.len));
}
@@ -1644,6 +1642,7 @@ fn emitCallCtorsFunction(wasm: *const Wasm, binary_bytes: *std.ArrayListUnmanage
for (wasm.object_init_funcs.items) |init_func| {
const func = init_func.function_index.ptr(wasm);
+ if (!func.object_index.ptr(wasm).is_included) continue;
const ty = func.type_index.ptr(wasm);
const n_returns = ty.returns.slice(wasm).len;
src/link/Wasm/Object.zig
@@ -48,6 +48,7 @@ code_section_index: ?Wasm.ObjectSectionIndex,
global_section_index: ?Wasm.ObjectSectionIndex,
/// Guaranteed to be non-null when data segments has nonzero length.
data_section_index: ?Wasm.ObjectSectionIndex,
+is_included: bool,
pub const RelativeSlice = struct {
off: u32,
@@ -1414,6 +1415,7 @@ pub fn parse(
.code_section_index = code_section_index,
.global_section_index = global_section_index,
.data_section_index = data_section_index,
+ .is_included = must_link,
};
}
src/link/Wasm.zig
@@ -117,7 +117,7 @@ object_memories: std.ArrayListUnmanaged(ObjectMemory) = .empty,
object_relocations: std.MultiArrayList(ObjectRelocation) = .empty,
/// List of initialization functions. These must be called in order of priority
-/// by the (synthetic) __wasm_call_ctors function.
+/// by the (synthetic) `__wasm_call_ctors` function.
object_init_funcs: std.ArrayListUnmanaged(InitFunc) = .empty,
/// The data section of an object has many segments. Each segment corresponds
@@ -3308,7 +3308,10 @@ pub fn prelink(wasm: *Wasm, prog_node: std.Progress.Node) link.File.FlushError!v
}
// Also treat init functions as roots.
for (wasm.object_init_funcs.items) |init_func| {
- try markFunction(wasm, init_func.function_index);
+ const func = init_func.function_index.ptr(wasm);
+ if (func.object_index.ptr(wasm).is_included) {
+ try markFunction(wasm, init_func.function_index);
+ }
}
wasm.functions_end_prelink = @intCast(wasm.functions.entries.len);
wasm.function_exports_len = @intCast(wasm.function_exports.items.len);
@@ -3383,6 +3386,7 @@ fn markFunction(wasm: *Wasm, i: ObjectFunctionIndex) link.File.FlushError!void {
const rdynamic = comp.config.rdynamic;
const is_obj = comp.config.output_mode == .Obj;
const function = i.ptr(wasm);
+ markObject(wasm, function.object_index);
if (!is_obj and function.flags.isExported(rdynamic)) try wasm.function_exports.append(gpa, .{
.name = function.name.unwrap().?,
@@ -3392,6 +3396,10 @@ fn markFunction(wasm: *Wasm, i: ObjectFunctionIndex) link.File.FlushError!void {
try wasm.markRelocations(function.relocations(wasm));
}
+fn markObject(wasm: *Wasm, i: ObjectIndex) void {
+ i.ptr(wasm).is_included = true;
+}
+
/// Recursively mark alive everything referenced by the global.
fn markGlobalImport(
wasm: *Wasm,