authorgravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2023-11-02 19:06:19+01:00
committergravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2023-11-03 12:48:53+01:00
logc893f837151d4764fd34911376836a01192b4d75
tree5b27b35ef9c77e343a39ab1bdc1f24270e56bec2
parent5b2ee5eacc177873ce674a307a1bebdfffeeae10
signaturelock-open Commit is signed but in an unrecognized format.

cli: consolidate entry point flags


21 files changed, 86 insertions(+), 70 deletions(-)

lib/std/Build/Step/Compile.zig+21-11
......@@ -64,8 +64,6 @@ initial_memory: ?u64 = null,
6464max_memory: ?u64 = null,
6565shared_memory: bool = false,
6666global_base: ?u64 = null,
67/// For WebAssembly only. Tells the linker to not output an entry point.
68no_entry: ?bool = null,
6967c_std: std.Build.CStd,
7068/// Set via options; intended to be read-only after that.
7169zig_lib_dir: ?LazyPath,
......@@ -191,7 +189,8 @@ dll_export_fns: ?bool = null,
191189
192190subsystem: ?std.Target.SubSystem = null,
193191
194entry_symbol_name: ?[]const u8 = null,
192/// How the linker must handle the entry point of the executable.
193entry: Entry = .default,
195194
196195/// List of symbols forced as undefined in the symbol table
197196/// thus forcing their resolution by the linker.
......@@ -306,6 +305,18 @@ const FrameworkLinkInfo = struct {
306305 weak: bool = false,
307306};
308307
308const Entry = union(enum) {
309 /// Let the compiler decide whether to make an entry point and what to name
310 /// it.
311 default,
312 /// The executable will have no entry point.
313 disabled,
314 /// The executable will have an entry point with the default symbol name.
315 enabled,
316 /// The executable will have an entry point with the specified symbol name.
317 symbol_name: []const u8,
318};
319
309320pub const IncludeDir = union(enum) {
310321 path: LazyPath,
311322 path_system: LazyPath,
......@@ -1420,9 +1431,13 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
14201431 try zig_args.append(try std.fmt.allocPrint(b.allocator, "-ofmt={s}", .{@tagName(ofmt)}));
14211432 }
14221433
1423 if (self.entry_symbol_name) |entry| {
1424 try zig_args.append("--entry");
1425 try zig_args.append(entry);
1434 switch (self.entry) {
1435 .default => {},
1436 .disabled => try zig_args.append("-fno-entry"),
1437 .enabled => try zig_args.append("-fentry"),
1438 .symbol_name => |entry_name| {
1439 try zig_args.append(try std.fmt.allocPrint(b.allocator, "-fentry={s}", .{entry_name}));
1440 },
14261441 }
14271442
14281443 {
......@@ -1853,11 +1868,6 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
18531868 if (self.global_base) |global_base| {
18541869 try zig_args.append(b.fmt("--global-base={d}", .{global_base}));
18551870 }
1856 // invert the value due to naming so when `no_entry` is set to 'true'
1857 // we actually emit the flag `-fno_entry`.
1858 if (self.no_entry) |no_entry| {
1859 try addFlag(&zig_args, "entry", !no_entry);
1860 }
18611871
18621872 if (self.code_model != .default) {
18631873 try zig_args.append("-mcmodel");
src/Compilation.zig-3
......@@ -643,7 +643,6 @@ pub const InitOptions = struct {
643643 linker_import_symbols: bool = false,
644644 linker_import_table: bool = false,
645645 linker_export_table: bool = false,
646 linker_no_entry: bool = false,
647646 linker_initial_memory: ?u64 = null,
648647 linker_max_memory: ?u64 = null,
649648 linker_shared_memory: bool = false,
......@@ -1615,7 +1614,6 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
16151614 .import_symbols = options.linker_import_symbols,
16161615 .import_table = options.linker_import_table,
16171616 .export_table = options.linker_export_table,
1618 .no_entry = options.linker_no_entry,
16191617 .initial_memory = options.linker_initial_memory,
16201618 .max_memory = options.linker_max_memory,
16211619 .shared_memory = options.linker_shared_memory,
......@@ -2579,7 +2577,6 @@ fn addNonIncrementalStuffToCacheManifest(comp: *Compilation, man: *Cache.Manifes
25792577 man.hash.addOptional(comp.bin_file.options.max_memory);
25802578 man.hash.add(comp.bin_file.options.shared_memory);
25812579 man.hash.addOptional(comp.bin_file.options.global_base);
2582 man.hash.add(comp.bin_file.options.no_entry);
25832580
25842581 // Mach-O specific stuff
25852582 man.hash.addListOfBytes(comp.bin_file.options.framework_dirs);
src/link.zig-1
......@@ -166,7 +166,6 @@ pub const Options = struct {
166166 export_table: bool,
167167 initial_memory: ?u64,
168168 max_memory: ?u64,
169 no_entry: bool,
170169 shared_memory: bool,
171170 export_symbol_names: []const []const u8,
172171 global_base: ?u64,
src/link/Wasm.zig+5-7
......@@ -2817,11 +2817,11 @@ fn setupExports(wasm: *Wasm) !void {
28172817}
28182818
28192819fn setupStart(wasm: *Wasm) !void {
2820 if (wasm.base.options.no_entry) return;
2821 const entry_name = wasm.base.options.entry orelse "_start";
2820 // do not export entry point if user set none or no default was set.
2821 const entry_name = wasm.base.options.entry orelse return;
28222822
28232823 const symbol_loc = wasm.findGlobalSymbol(entry_name) orelse {
2824 log.err("Entry symbol '{s}' missing", .{entry_name});
2824 log.err("Entry symbol '{s}' missing, use '-fno-entry' to suppress", .{entry_name});
28252825 return error.MissingSymbol;
28262826 };
28272827
......@@ -4531,6 +4531,8 @@ fn linkWithLLD(wasm: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) !
45314531 if (wasm.base.options.entry) |entry| {
45324532 try argv.append("--entry");
45334533 try argv.append(entry);
4534 } else {
4535 try argv.append("--no-entry");
45344536 }
45354537
45364538 // Increase the default stack size to a more reasonable value of 1MB instead of
......@@ -4544,10 +4546,6 @@ fn linkWithLLD(wasm: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) !
45444546 try argv.append("--allow-undefined");
45454547 }
45464548
4547 if (wasm.base.options.no_entry) {
4548 try argv.append("--no-entry");
4549 }
4550
45514549 if (wasm.base.options.output_mode == .Lib and wasm.base.options.link_mode == .Dynamic) {
45524550 try argv.append("--shared");
45534551 }
src/main.zig+38-26
......@@ -509,7 +509,9 @@ const usage_build_generic =
509509 \\ --dynamic-linker [path] Set the dynamic interpreter path (usually ld.so)
510510 \\ --sysroot [path] Set the system root directory (usually /)
511511 \\ --version [ver] Dynamic library semver
512 \\ --entry [name] Set the entrypoint symbol name
512 \\ -fentry Enable entry point with default symbol name
513 \\ -fentry=[name] Override the entry point symbol name
514 \\ -fno-entry Do not output any entry point
513515 \\ --force_undefined [name] Specify the symbol must be defined for the link to succeed
514516 \\ -fsoname[=name] Override the default SONAME value
515517 \\ -fno-soname Disable emitting a SONAME
......@@ -577,8 +579,6 @@ const usage_build_generic =
577579 \\ --shared-memory (WebAssembly) use shared linear memory
578580 \\ --global-base=[addr] (WebAssembly) where to start to place global data
579581 \\ --export=[value] (WebAssembly) Force a symbol to be exported
580 \\ -fentry (WebAssembly) Force output an entry point
581 \\ -fno-entry (WebAssembly) Do not output any entry point
582582 \\
583583 \\Test Options:
584584 \\ --test-filter [text] Skip tests that do not match filter
......@@ -837,7 +837,7 @@ fn buildOutputType(
837837 var linker_import_symbols: bool = false;
838838 var linker_import_table: bool = false;
839839 var linker_export_table: bool = false;
840 var linker_no_entry: ?bool = null;
840 var linker_force_entry: ?bool = null;
841841 var linker_initial_memory: ?u64 = null;
842842 var linker_max_memory: ?u64 = null;
843843 var linker_shared_memory: bool = false;
......@@ -1074,8 +1074,8 @@ fn buildOutputType(
10741074 subsystem = try parseSubSystem(args_iter.nextOrFatal());
10751075 } else if (mem.eql(u8, arg, "-O")) {
10761076 optimize_mode_string = args_iter.nextOrFatal();
1077 } else if (mem.eql(u8, arg, "--entry")) {
1078 entry = args_iter.nextOrFatal();
1077 } else if (mem.startsWith(u8, arg, "-fentry=")) {
1078 entry = arg["-fentry=".len..];
10791079 } else if (mem.eql(u8, arg, "--force_undefined")) {
10801080 try force_undefined_symbols.put(gpa, args_iter.nextOrFatal(), {});
10811081 } else if (mem.eql(u8, arg, "--stack")) {
......@@ -1507,9 +1507,9 @@ fn buildOutputType(
15071507 } else if (mem.eql(u8, arg, "--import-memory")) {
15081508 linker_import_memory = true;
15091509 } else if (mem.eql(u8, arg, "-fentry")) {
1510 linker_no_entry = false;
1510 linker_force_entry = true;
15111511 } else if (mem.eql(u8, arg, "-fno-entry")) {
1512 linker_no_entry = true;
1512 linker_force_entry = false;
15131513 } else if (mem.eql(u8, arg, "--export-memory")) {
15141514 linker_export_memory = true;
15151515 } else if (mem.eql(u8, arg, "--import-symbols")) {
......@@ -2142,7 +2142,7 @@ fn buildOutputType(
21422142 } else if (mem.eql(u8, arg, "--export-table")) {
21432143 linker_export_table = true;
21442144 } else if (mem.eql(u8, arg, "--no-entry")) {
2145 linker_no_entry = true;
2145 linker_force_entry = false;
21462146 } else if (mem.eql(u8, arg, "--initial-memory")) {
21472147 const next_arg = linker_args_it.nextOrFatal();
21482148 linker_initial_memory = std.fmt.parseUnsigned(u32, eatIntPrefix(next_arg, 16), 16) catch |err| {
......@@ -2605,6 +2605,23 @@ fn buildOutputType(
26052605 link_libcpp = true;
26062606 }
26072607
2608 if (linker_force_entry) |force| {
2609 if (!force) {
2610 entry = null;
2611 } else if (entry == null and output_mode == .Exe) {
2612 entry = switch (target_info.target.ofmt) {
2613 .coff => "wWinMainCRTStartup",
2614 .macho => "_main",
2615 .elf, .plan9 => "_start",
2616 .wasm => defaultWasmEntryName(wasi_exec_model),
2617 else => |tag| fatal("No default entry point available for output format {s}", .{@tagName(tag)}),
2618 };
2619 }
2620 } else if (entry == null and target_info.target.isWasm() and output_mode == .Exe) {
2621 // For WebAssembly the compiler defaults to setting the entry name when no flags are set.
2622 entry = defaultWasmEntryName(wasi_exec_model);
2623 }
2624
26082625 if (target_info.target.ofmt == .coff) {
26092626 // Now that we know the target supports resources,
26102627 // we can add the res files as link objects.
......@@ -2637,25 +2654,13 @@ fn buildOutputType(
26372654 linker_export_memory = false;
26382655 }
26392656 }
2640 if (wasi_exec_model) |model| {
2641 if (model == .reactor) {
2642 if (linker_no_entry != null and !linker_no_entry.?) {
2643 fatal("WASI exucution model 'reactor' incompatible with flag '-fentry'. Reactor execution model has no entry point", .{});
2644 }
2645 if (entry) |entry_name| {
2646 if (!mem.eql(u8, "_initialize", entry_name)) {
2647 fatal("the entry symbol of the reactor model must be '_initialize', but found '{s}'", .{entry_name});
2648 }
2649 } else {
2650 entry = "_initialize";
2657 if (wasi_exec_model != null and wasi_exec_model.? == .reactor) {
2658 if (entry) |entry_name| {
2659 if (!mem.eql(u8, "_initialize", entry_name)) {
2660 fatal("the entry symbol of the reactor model must be '_initialize', but found '{s}'", .{entry_name});
26512661 }
26522662 }
26532663 }
2654 if (linker_no_entry) |no_entry| {
2655 if (no_entry and entry != null) {
2656 fatal("combination of '--entry' and `-fno-entry` are incompatible", .{});
2657 }
2658 }
26592664 if (linker_shared_memory) {
26602665 if (output_mode == .Obj) {
26612666 fatal("shared memory is not allowed in object files", .{});
......@@ -3503,7 +3508,6 @@ fn buildOutputType(
35033508 .linker_import_symbols = linker_import_symbols,
35043509 .linker_import_table = linker_import_table,
35053510 .linker_export_table = linker_export_table,
3506 .linker_no_entry = linker_no_entry orelse false,
35073511 .linker_initial_memory = linker_initial_memory,
35083512 .linker_max_memory = linker_max_memory,
35093513 .linker_shared_memory = linker_shared_memory,
......@@ -7253,3 +7257,11 @@ fn createDependenciesModule(
72537257 try main_mod.deps.put(arena, "@dependencies", deps_mod);
72547258 return deps_mod;
72557259}
7260
7261fn defaultWasmEntryName(exec_model: ?std.builtin.WasiExecModel) []const u8 {
7262 const model = exec_model orelse .command;
7263 if (model == .reactor) {
7264 return "_initialize";
7265 }
7266 return "_start";
7267}
test/link/elf.zig+2-2
......@@ -651,7 +651,7 @@ fn testEntryPoint(b: *Build, opts: Options) *Step {
651651 const exe = addExecutable(b, "main", opts);
652652 exe.addObject(a_o);
653653 exe.addObject(b_o);
654 exe.entry_symbol_name = "foo";
654 exe.entry = .{ .symbol_name = "foo" };
655655
656656 const check = exe.checkObject();
657657 check.checkStart();
......@@ -667,7 +667,7 @@ fn testEntryPoint(b: *Build, opts: Options) *Step {
667667 const exe = addExecutable(b, "other", opts);
668668 exe.addObject(a_o);
669669 exe.addObject(b_o);
670 exe.entry_symbol_name = "bar";
670 exe.entry = .{ .symbol_name = "bar" };
671671
672672 const check = exe.checkObject();
673673 check.checkStart();
test/link/macho/entry/build.zig+1-1
......@@ -20,7 +20,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
2020 });
2121 exe.addCSourceFile(.{ .file = .{ .path = "main.c" }, .flags = &.{} });
2222 exe.linkLibC();
23 exe.entry_symbol_name = "_non_main";
23 exe.entry = .{ .symbol_name = "_non_main" };
2424
2525 const check_exe = exe.checkObject();
2626
test/link/macho/entry_in_dylib/build.zig+1-1
......@@ -30,7 +30,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
3030 exe.addCSourceFile(.{ .file = .{ .path = "main.c" }, .flags = &.{} });
3131 exe.linkLibrary(lib);
3232 exe.linkLibC();
33 exe.entry_symbol_name = "_bootstrap";
33 exe.entry = .{ .symbol_name = "_bootstrap" };
3434 exe.forceUndefinedSymbol("_my_main");
3535
3636 const check_exe = exe.checkObject();
test/link/wasm/archive/build.zig+1-1
......@@ -21,7 +21,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
2121 .optimize = optimize,
2222 .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
2323 });
24 lib.no_entry = true;
24 lib.entry = .disabled;
2525 lib.use_llvm = false;
2626 lib.use_lld = false;
2727 lib.strip = false;
test/link/wasm/basic-features/build.zig+1-1
......@@ -15,7 +15,7 @@ pub fn build(b: *std.Build) void {
1515 .os_tag = .freestanding,
1616 },
1717 });
18 lib.no_entry = true;
18 lib.entry = .disabled;
1919 lib.use_llvm = false;
2020 lib.use_lld = false;
2121
test/link/wasm/bss/build.zig+2-2
......@@ -20,7 +20,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize_mode: std.builtin.Opt
2020 .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
2121 .optimize = optimize_mode,
2222 });
23 lib.no_entry = true;
23 lib.entry = .disabled;
2424 lib.use_llvm = false;
2525 lib.use_lld = false;
2626 lib.strip = false;
......@@ -67,7 +67,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize_mode: std.builtin.Opt
6767 .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
6868 .optimize = optimize_mode,
6969 });
70 lib.no_entry = true;
70 lib.entry = .disabled;
7171 lib.use_llvm = false;
7272 lib.use_lld = false;
7373 lib.strip = false;
test/link/wasm/export-data/build.zig+1-1
......@@ -15,7 +15,7 @@ pub fn build(b: *std.Build) void {
1515 .optimize = .ReleaseSafe, // to make the output deterministic in address positions
1616 .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
1717 });
18 lib.no_entry = true;
18 lib.entry = .disabled;
1919 lib.use_lld = false;
2020 lib.export_symbol_names = &.{ "foo", "bar" };
2121 lib.global_base = 0; // put data section at address 0 to make data symbols easier to parse
test/link/wasm/export/build.zig+3-3
......@@ -19,7 +19,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
1919 .optimize = optimize,
2020 .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
2121 });
22 no_export.no_entry = true;
22 no_export.entry = .disabled;
2323 no_export.use_llvm = false;
2424 no_export.use_lld = false;
2525
......@@ -29,7 +29,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
2929 .optimize = optimize,
3030 .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
3131 });
32 dynamic_export.no_entry = true;
32 dynamic_export.entry = .disabled;
3333 dynamic_export.rdynamic = true;
3434 dynamic_export.use_llvm = false;
3535 dynamic_export.use_lld = false;
......@@ -40,7 +40,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
4040 .optimize = optimize,
4141 .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
4242 });
43 force_export.no_entry = true;
43 force_export.entry = .disabled;
4444 force_export.export_symbol_names = &.{"foo"};
4545 force_export.use_llvm = false;
4646 force_export.use_lld = false;
test/link/wasm/extern-mangle/build.zig+1-1
......@@ -17,7 +17,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
1717 .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
1818 .optimize = optimize,
1919 });
20 lib.no_entry = true;
20 lib.entry = .disabled;
2121 lib.import_symbols = true; // import `a` and `b`
2222 lib.rdynamic = true; // export `foo`
2323
test/link/wasm/function-table/build.zig+3-3
......@@ -19,7 +19,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
1919 .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
2020 .optimize = optimize,
2121 });
22 import_table.no_entry = true;
22 import_table.entry = .disabled;
2323 import_table.use_llvm = false;
2424 import_table.use_lld = false;
2525 import_table.import_table = true;
......@@ -30,7 +30,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
3030 .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
3131 .optimize = optimize,
3232 });
33 export_table.no_entry = true;
33 export_table.entry = .disabled;
3434 export_table.use_llvm = false;
3535 export_table.use_lld = false;
3636 export_table.export_table = true;
......@@ -41,7 +41,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
4141 .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
4242 .optimize = optimize,
4343 });
44 regular_table.no_entry = true;
44 regular_table.entry = .disabled;
4545 regular_table.use_llvm = false;
4646 regular_table.use_lld = false;
4747
test/link/wasm/infer-features/build.zig+1-1
......@@ -27,7 +27,7 @@ pub fn build(b: *std.Build) void {
2727 .os_tag = .freestanding,
2828 },
2929 });
30 lib.no_entry = true;
30 lib.entry = .disabled;
3131 lib.use_llvm = false;
3232 lib.use_lld = false;
3333 lib.addObject(c_obj);
test/link/wasm/producers/build.zig+1-1
......@@ -20,7 +20,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
2020 .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
2121 .optimize = optimize,
2222 });
23 lib.no_entry = true;
23 lib.entry = .disabled;
2424 lib.use_llvm = false;
2525 lib.use_lld = false;
2626 lib.strip = false;
test/link/wasm/segments/build.zig+1-1
......@@ -19,7 +19,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
1919 .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
2020 .optimize = optimize,
2121 });
22 lib.no_entry = true;
22 lib.entry = .disabled;
2323 lib.use_llvm = false;
2424 lib.use_lld = false;
2525 lib.strip = false;
test/link/wasm/shared-memory/build.zig+1-1
......@@ -22,7 +22,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize_mode: std.builtin.Opt
2222 },
2323 .optimize = optimize_mode,
2424 });
25 lib.no_entry = true;
25 lib.entry = .disabled;
2626 lib.use_lld = false;
2727 lib.strip = false;
2828 lib.import_memory = true;
test/link/wasm/stack_pointer/build.zig+1-1
......@@ -19,7 +19,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
1919 .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
2020 .optimize = optimize,
2121 });
22 lib.no_entry = true;
22 lib.entry = .disabled;
2323 lib.use_llvm = false;
2424 lib.use_lld = false;
2525 lib.strip = false;
test/link/wasm/type/build.zig+1-1
......@@ -19,7 +19,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
1919 .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
2020 .optimize = optimize,
2121 });
22 lib.no_entry = true;
22 lib.entry = .disabled;
2323 lib.use_llvm = false;
2424 lib.use_lld = false;
2525 lib.strip = false;