authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-01-19 13:43:40-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-02 21:19:18-07:00
log18fabd99ccf4f8a53eed8da68ddc45345d1a2081
tree2e01092650d9111da3073ac383ef688266267740
parentafd1f7ed47884ca39082287c267fbb9c5d1ebe68

Merge pull request #10475 from lithdew/master

lld: allow for entrypoint symbol name to be set

9 files changed, 63 insertions(+), 3 deletions(-)

lib/std/build.zig+7
......@@ -1552,6 +1552,8 @@ pub const LibExeObjStep = struct {
15521552
15531553 subsystem: ?std.Target.SubSystem = null,
15541554
1555 entry_symbol_name: ?[]const u8 = null,
1556
15551557 /// Overrides the default stack size
15561558 stack_size: ?u64 = null,
15571559
......@@ -2253,6 +2255,11 @@ pub const LibExeObjStep = struct {
22532255 try zig_args.append(@tagName(builder.color));
22542256 }
22552257
2258 if (self.entry_symbol_name) |entry| {
2259 try zig_args.append("--entry");
2260 try zig_args.append(entry);
2261 }
2262
22562263 if (self.stack_size) |stack_size| {
22572264 try zig_args.append("--stack");
22582265 try zig_args.append(try std.fmt.allocPrint(builder.allocator, "{}", .{stack_size}));
src/Compilation.zig+2
......@@ -765,6 +765,7 @@ pub const InitOptions = struct {
765765 /// infinite recursion.
766766 skip_linker_dependencies: bool = false,
767767 parent_compilation_link_libc: bool = false,
768 entry: ?[]const u8 = null,
768769 stack_size_override: ?u64 = null,
769770 image_base_override: ?u64 = null,
770771 self_exe_path: ?[]const u8 = null,
......@@ -1478,6 +1479,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
14781479 .linker_optimization = linker_optimization,
14791480 .major_subsystem_version = options.major_subsystem_version,
14801481 .minor_subsystem_version = options.minor_subsystem_version,
1482 .entry = options.entry,
14811483 .stack_size_override = options.stack_size_override,
14821484 .image_base_override = options.image_base_override,
14831485 .include_compiler_rt = include_compiler_rt,
src/clang_options_data.zig+9-2
......@@ -1655,7 +1655,7 @@ flagpsl("MT"),
16551655.{
16561656 .name = "entry",
16571657 .syntax = .flag,
1658 .zig_equivalent = .other,
1658 .zig_equivalent = .entry,
16591659 .pd1 = false,
16601660 .pd2 = true,
16611661 .psl = false,
......@@ -6701,7 +6701,14 @@ joinpd1("Z"),
67016701joinpd1("a"),
67026702jspd1("b"),
67036703joinpd1("d"),
6704jspd1("e"),
6704.{
6705 .name = "e",
6706 .syntax = .joined_or_separate,
6707 .zig_equivalent = .entry,
6708 .pd1 = true,
6709 .pd2 = false,
6710 .psl = false,
6711},
67056712.{
67066713 .name = "l",
67076714 .syntax = .joined_or_separate,
src/link.zig+1
......@@ -68,6 +68,7 @@ pub const Options = struct {
6868 /// the binary file does not already have such a section.
6969 program_code_size_hint: u64 = 256 * 1024,
7070 entry_addr: ?u64 = null,
71 entry: ?[]const u8,
7172 stack_size_override: ?u64,
7273 image_base_override: ?u64,
7374 include_compiler_rt: bool,
src/link/Coff.zig+5
......@@ -925,6 +925,7 @@ fn linkWithLLD(self: *Coff, comp: *Compilation) !void {
925925 _ = try man.addFile(key.status.success.object_path, null);
926926 }
927927 try man.addOptionalFile(module_obj_path);
928 man.hash.addOptionalBytes(self.base.options.entry);
928929 man.hash.addOptional(self.base.options.stack_size_override);
929930 man.hash.addOptional(self.base.options.image_base_override);
930931 man.hash.addListOfBytes(self.base.options.lib_dirs);
......@@ -1045,6 +1046,10 @@ fn linkWithLLD(self: *Coff, comp: *Compilation) !void {
10451046 try argv.append("-DLL");
10461047 }
10471048
1049 if (self.base.options.entry) |entry| {
1050 try argv.append(try allocPrint(arena, "-ENTRY:{s}", .{entry}));
1051 }
1052
10481053 if (self.base.options.tsaware) {
10491054 try argv.append("-tsaware");
10501055 }
src/link/Elf.zig+8-1
......@@ -1316,6 +1316,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
13161316
13171317 // We can skip hashing libc and libc++ components that we are in charge of building from Zig
13181318 // installation sources because they are always a product of the compiler version + target information.
1319 man.hash.addOptionalBytes(self.base.options.entry);
13191320 man.hash.add(stack_size);
13201321 man.hash.addOptional(self.base.options.image_base_override);
13211322 man.hash.add(gc_sections);
......@@ -1438,6 +1439,11 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
14381439 self.base.options.linker_optimization,
14391440 }));
14401441
1442 if (self.base.options.entry) |entry| {
1443 try argv.append("--entry");
1444 try argv.append(entry);
1445 }
1446
14411447 if (self.base.options.output_mode == .Exe) {
14421448 try argv.append("-z");
14431449 try argv.append(try std.fmt.allocPrint(arena, "stack-size={d}", .{stack_size}));
......@@ -2786,7 +2792,8 @@ pub fn updateDeclExports(
27862792 const stb_bits: u8 = switch (exp.options.linkage) {
27872793 .Internal => elf.STB_LOCAL,
27882794 .Strong => blk: {
2789 if (mem.eql(u8, exp.options.name, "_start")) {
2795 const entry_name = self.base.options.entry orelse "_start";
2796 if (mem.eql(u8, exp.options.name, entry_name)) {
27902797 self.entry_addr = decl_sym.st_value;
27912798 }
27922799 break :blk elf.STB_GLOBAL;
src/link/Wasm.zig+6
......@@ -1018,6 +1018,7 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
10181018 }
10191019 try man.addOptionalFile(module_obj_path);
10201020 try man.addOptionalFile(compiler_rt_path);
1021 man.hash.addOptionalBytes(self.base.options.entry);
10211022 man.hash.addOptional(self.base.options.stack_size_override);
10221023 man.hash.add(self.base.options.import_memory);
10231024 man.hash.addOptional(self.base.options.initial_memory);
......@@ -1165,6 +1166,11 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
11651166 }
11661167 }
11671168
1169 if (self.base.options.entry) |entry| {
1170 try argv.append("--entry");
1171 try argv.append(entry);
1172 }
1173
11681174 if (self.base.options.output_mode == .Exe) {
11691175 // Increase the default stack size to a more reasonable value of 1MB instead of
11701176 // the default of 1 Wasm page being 64KB, unless overridden by the user.
src/main.zig+17
......@@ -400,6 +400,7 @@ const usage_build_generic =
400400 \\ --dynamic-linker [path] Set the dynamic interpreter path (usually ld.so)
401401 \\ --sysroot [path] Set the system root directory (usually /)
402402 \\ --version [ver] Dynamic library semver
403 \\ --entry [name] Set the entrypoint symbol name
403404 \\ -fsoname[=name] Override the default SONAME value
404405 \\ -fno-soname Disable emitting a SONAME
405406 \\ -fLLD Force using LLD as the linker
......@@ -643,6 +644,7 @@ fn buildOutputType(
643644 var linker_optimization: ?u8 = null;
644645 var test_evented_io = false;
645646 var test_no_exec = false;
647 var entry: ?[]const u8 = null;
646648 var stack_size_override: ?u64 = null;
647649 var image_base_override: ?u64 = null;
648650 var use_llvm: ?bool = null;
......@@ -843,6 +845,10 @@ fn buildOutputType(
843845 if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg});
844846 i += 1;
845847 optimize_mode_string = args[i];
848 } else if (mem.eql(u8, arg, "--entry")) {
849 if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg});
850 i += 1;
851 entry = args[i];
846852 } else if (mem.eql(u8, arg, "--stack")) {
847853 if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg});
848854 i += 1;
......@@ -1471,6 +1477,9 @@ fn buildOutputType(
14711477 .sysroot => {
14721478 sysroot = it.only_arg;
14731479 },
1480 .entry => {
1481 entry = it.only_arg;
1482 },
14741483 }
14751484 }
14761485 // Parse linker args.
......@@ -1612,6 +1621,12 @@ fn buildOutputType(
16121621 fatal("unable to parse '{s}': {s}", .{ arg, @errorName(err) });
16131622 };
16141623 have_version = true;
1624 } else if (mem.eql(u8, arg, "-e") or mem.eql(u8, arg, "--entry")) {
1625 i += 1;
1626 if (i >= linker_args.items.len) {
1627 fatal("expected linker arg after '{s}'", .{arg});
1628 }
1629 entry = linker_args.items[i];
16151630 } else if (mem.eql(u8, arg, "--stack")) {
16161631 i += 1;
16171632 if (i >= linker_args.items.len) {
......@@ -2477,6 +2492,7 @@ fn buildOutputType(
24772492 .minor_subsystem_version = minor_subsystem_version,
24782493 .link_eh_frame_hdr = link_eh_frame_hdr,
24792494 .link_emit_relocs = link_emit_relocs,
2495 .entry = entry,
24802496 .stack_size_override = stack_size_override,
24812497 .image_base_override = image_base_override,
24822498 .strip = strip,
......@@ -4119,6 +4135,7 @@ pub const ClangArgIterator = struct {
41194135 exec_model,
41204136 emit_llvm,
41214137 sysroot,
4138 entry,
41224139 };
41234140
41244141 const Args = struct {
tools/update_clang_options.zig+8
......@@ -416,6 +416,14 @@ const known_options = [_]KnownOpt{
416416 .name = "sysroot",
417417 .ident = "sysroot",
418418 },
419 .{
420 .name = "entry",
421 .ident = "entry",
422 },
423 .{
424 .name = "e",
425 .ident = "entry",
426 },
419427};
420428
421429const blacklisted_options = [_][]const u8{};