authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-01-19 13:43:40-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-01-19 13:43:40-05:00
log4e8fedffd12e1f995f38369421391f5a12035a99
tree9be7f9b5f04ba747ef275a0f8349274b7c66befa
parentbeb7495e19d23b4814e16772888e80688ad10e47
parentfd6d1fe015c62cbdb52c9703384c0321b8c5ef01
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

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
......@@ -1554,6 +1554,8 @@ pub const LibExeObjStep = struct {
15541554
15551555 subsystem: ?std.Target.SubSystem = null,
15561556
1557 entry_symbol_name: ?[]const u8 = null,
1558
15571559 /// Overrides the default stack size
15581560 stack_size: ?u64 = null,
15591561
......@@ -2255,6 +2257,11 @@ pub const LibExeObjStep = struct {
22552257 try zig_args.append(@tagName(builder.color));
22562258 }
22572259
2260 if (self.entry_symbol_name) |entry| {
2261 try zig_args.append("--entry");
2262 try zig_args.append(entry);
2263 }
2264
22582265 if (self.stack_size) |stack_size| {
22592266 try zig_args.append("--stack");
22602267 try zig_args.append(try std.fmt.allocPrint(builder.allocator, "{}", .{stack_size}));
src/Compilation.zig+2
......@@ -791,6 +791,7 @@ pub const InitOptions = struct {
791791 /// infinite recursion.
792792 skip_linker_dependencies: bool = false,
793793 parent_compilation_link_libc: bool = false,
794 entry: ?[]const u8 = null,
794795 stack_size_override: ?u64 = null,
795796 image_base_override: ?u64 = null,
796797 self_exe_path: ?[]const u8 = null,
......@@ -1572,6 +1573,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
15721573 .linker_optimization = linker_optimization,
15731574 .major_subsystem_version = options.major_subsystem_version,
15741575 .minor_subsystem_version = options.minor_subsystem_version,
1576 .entry = options.entry,
15751577 .stack_size_override = options.stack_size_override,
15761578 .image_base_override = options.image_base_override,
15771579 .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
......@@ -84,6 +84,7 @@ pub const Options = struct {
8484 /// the binary file does not already have such a section.
8585 program_code_size_hint: u64 = 256 * 1024,
8686 entry_addr: ?u64 = null,
87 entry: ?[]const u8,
8788 stack_size_override: ?u64,
8889 image_base_override: ?u64,
8990 cache_mode: CacheMode,
src/link/Coff.zig+5
......@@ -951,6 +951,7 @@ fn linkWithLLD(self: *Coff, comp: *Compilation) !void {
951951 _ = try man.addFile(key.status.success.object_path, null);
952952 }
953953 try man.addOptionalFile(module_obj_path);
954 man.hash.addOptionalBytes(self.base.options.entry);
954955 man.hash.addOptional(self.base.options.stack_size_override);
955956 man.hash.addOptional(self.base.options.image_base_override);
956957 man.hash.addListOfBytes(self.base.options.lib_dirs);
......@@ -1070,6 +1071,10 @@ fn linkWithLLD(self: *Coff, comp: *Compilation) !void {
10701071 try argv.append("-DLL");
10711072 }
10721073
1074 if (self.base.options.entry) |entry| {
1075 try argv.append(try allocPrint(arena, "-ENTRY:{s}", .{entry}));
1076 }
1077
10731078 if (self.base.options.tsaware) {
10741079 try argv.append("-tsaware");
10751080 }
src/link/Elf.zig+8-1
......@@ -1396,6 +1396,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
13961396
13971397 // We can skip hashing libc and libc++ components that we are in charge of building from Zig
13981398 // installation sources because they are always a product of the compiler version + target information.
1399 man.hash.addOptionalBytes(self.base.options.entry);
13991400 man.hash.add(stack_size);
14001401 man.hash.addOptional(self.base.options.image_base_override);
14011402 man.hash.add(gc_sections);
......@@ -1518,6 +1519,11 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
15181519 self.base.options.linker_optimization,
15191520 }));
15201521
1522 if (self.base.options.entry) |entry| {
1523 try argv.append("--entry");
1524 try argv.append(entry);
1525 }
1526
15211527 if (self.base.options.output_mode == .Exe) {
15221528 try argv.append("-z");
15231529 try argv.append(try std.fmt.allocPrint(arena, "stack-size={d}", .{stack_size}));
......@@ -2883,7 +2889,8 @@ pub fn updateDeclExports(
28832889 const stb_bits: u8 = switch (exp.options.linkage) {
28842890 .Internal => elf.STB_LOCAL,
28852891 .Strong => blk: {
2886 if (mem.eql(u8, exp.options.name, "_start")) {
2892 const entry_name = self.base.options.entry orelse "_start";
2893 if (mem.eql(u8, exp.options.name, entry_name)) {
28872894 self.entry_addr = decl_sym.st_value;
28882895 }
28892896 break :blk elf.STB_GLOBAL;
src/link/Wasm.zig+6
......@@ -1137,6 +1137,7 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
11371137 }
11381138 try man.addOptionalFile(module_obj_path);
11391139 try man.addOptionalFile(compiler_rt_path);
1140 man.hash.addOptionalBytes(self.base.options.entry);
11401141 man.hash.addOptional(self.base.options.stack_size_override);
11411142 man.hash.add(self.base.options.import_memory);
11421143 man.hash.add(self.base.options.import_table);
......@@ -1295,6 +1296,11 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
12951296 }
12961297 }
12971298
1299 if (self.base.options.entry) |entry| {
1300 try argv.append("--entry");
1301 try argv.append(entry);
1302 }
1303
12981304 if (self.base.options.output_mode == .Exe) {
12991305 // Increase the default stack size to a more reasonable value of 1MB instead of
13001306 // 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
......@@ -647,6 +648,7 @@ fn buildOutputType(
647648 var linker_optimization: ?u8 = null;
648649 var test_evented_io = false;
649650 var test_no_exec = false;
651 var entry: ?[]const u8 = null;
650652 var stack_size_override: ?u64 = null;
651653 var image_base_override: ?u64 = null;
652654 var use_llvm: ?bool = null;
......@@ -847,6 +849,10 @@ fn buildOutputType(
847849 if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg});
848850 i += 1;
849851 optimize_mode_string = args[i];
852 } else if (mem.eql(u8, arg, "--entry")) {
853 if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg});
854 i += 1;
855 entry = args[i];
850856 } else if (mem.eql(u8, arg, "--stack")) {
851857 if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg});
852858 i += 1;
......@@ -1479,6 +1485,9 @@ fn buildOutputType(
14791485 .sysroot => {
14801486 sysroot = it.only_arg;
14811487 },
1488 .entry => {
1489 entry = it.only_arg;
1490 },
14821491 }
14831492 }
14841493 // Parse linker args.
......@@ -1624,6 +1633,12 @@ fn buildOutputType(
16241633 fatal("unable to parse '{s}': {s}", .{ arg, @errorName(err) });
16251634 };
16261635 have_version = true;
1636 } else if (mem.eql(u8, arg, "-e") or mem.eql(u8, arg, "--entry")) {
1637 i += 1;
1638 if (i >= linker_args.items.len) {
1639 fatal("expected linker arg after '{s}'", .{arg});
1640 }
1641 entry = linker_args.items[i];
16271642 } else if (mem.eql(u8, arg, "--stack")) {
16281643 i += 1;
16291644 if (i >= linker_args.items.len) {
......@@ -2500,6 +2515,7 @@ fn buildOutputType(
25002515 .minor_subsystem_version = minor_subsystem_version,
25012516 .link_eh_frame_hdr = link_eh_frame_hdr,
25022517 .link_emit_relocs = link_emit_relocs,
2518 .entry = entry,
25032519 .stack_size_override = stack_size_override,
25042520 .image_base_override = image_base_override,
25052521 .strip = strip,
......@@ -4143,6 +4159,7 @@ pub const ClangArgIterator = struct {
41434159 exec_model,
41444160 emit_llvm,
41454161 sysroot,
4162 entry,
41464163 };
41474164
41484165 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{};