aboutsummaryrefslogtreecommitdiff
path: root/lib/std
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2023-11-05 18:44:12 -0500
committerGitHub <noreply@github.com>2023-11-05 18:44:12 -0500
commit702b809ea3e9b9dbdc1fd6efe9306442487e7103 (patch)
treebd639378ad2931013ff49789aadfc2104093261c /lib/std
parentbec36aa7c028f2eaec94a2358f3e1326fcb9a30c (diff)
parentc893f837151d4764fd34911376836a01192b4d75 (diff)
downloadzig-702b809ea3e9b9dbdc1fd6efe9306442487e7103.tar.gz
zig-702b809ea3e9b9dbdc1fd6efe9306442487e7103.zip
Merge pull request #17815 from Luukdegram/wasm-no-entry
wasm-linker: implement `-fno-entry` and correctly pass `--shared` and `--pie` when given
Diffstat (limited to 'lib/std')
-rw-r--r--lib/std/Build/Step/Compile.zig25
-rw-r--r--lib/std/start.zig8
2 files changed, 27 insertions, 6 deletions
diff --git a/lib/std/Build/Step/Compile.zig b/lib/std/Build/Step/Compile.zig
index 9ede31c917..2d08541545 100644
--- a/lib/std/Build/Step/Compile.zig
+++ b/lib/std/Build/Step/Compile.zig
@@ -189,7 +189,8 @@ dll_export_fns: ?bool = null,
subsystem: ?std.Target.SubSystem = null,
-entry_symbol_name: ?[]const u8 = null,
+/// How the linker must handle the entry point of the executable.
+entry: Entry = .default,
/// List of symbols forced as undefined in the symbol table
/// thus forcing their resolution by the linker.
@@ -304,6 +305,18 @@ const FrameworkLinkInfo = struct {
weak: bool = false,
};
+const Entry = union(enum) {
+ /// Let the compiler decide whether to make an entry point and what to name
+ /// it.
+ default,
+ /// The executable will have no entry point.
+ disabled,
+ /// The executable will have an entry point with the default symbol name.
+ enabled,
+ /// The executable will have an entry point with the specified symbol name.
+ symbol_name: []const u8,
+};
+
pub const IncludeDir = union(enum) {
path: LazyPath,
path_system: LazyPath,
@@ -1418,9 +1431,13 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
try zig_args.append(try std.fmt.allocPrint(b.allocator, "-ofmt={s}", .{@tagName(ofmt)}));
}
- if (self.entry_symbol_name) |entry| {
- try zig_args.append("--entry");
- try zig_args.append(entry);
+ switch (self.entry) {
+ .default => {},
+ .disabled => try zig_args.append("-fno-entry"),
+ .enabled => try zig_args.append("-fentry"),
+ .symbol_name => |entry_name| {
+ try zig_args.append(try std.fmt.allocPrint(b.allocator, "-fentry={s}", .{entry_name}));
+ },
}
{
diff --git a/lib/std/start.zig b/lib/std/start.zig
index 7925ea9168..ff21ed8187 100644
--- a/lib/std/start.zig
+++ b/lib/std/start.zig
@@ -82,11 +82,15 @@ comptime {
.reactor => "_initialize",
.command => "_start",
};
- if (!@hasDecl(root, wasm_start_sym)) {
+ if (!@hasDecl(root, wasm_start_sym) and @hasDecl(root, "main")) {
+ // Only call main when defined. For WebAssembly it's allowed to pass `-fno-entry` in which
+ // case it's not required to provide an entrypoint such as main.
@export(wasi_start, .{ .name = wasm_start_sym });
}
} else if (native_arch.isWasm() and native_os == .freestanding) {
- if (!@hasDecl(root, start_sym_name)) @export(wasm_freestanding_start, .{ .name = start_sym_name });
+ // Only call main when defined. For WebAssembly it's allowed to pass `-fno-entry` in which
+ // case it's not required to provide an entrypoint such as main.
+ if (!@hasDecl(root, start_sym_name) and @hasDecl(root, "main")) @export(wasm_freestanding_start, .{ .name = start_sym_name });
} else if (native_os != .other and native_os != .freestanding) {
if (!@hasDecl(root, start_sym_name)) @export(_start, .{ .name = start_sym_name });
}