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 {...@@ -1552,6 +1552,8 @@ pub const LibExeObjStep = struct {
15521552
1553 subsystem: ?std.Target.SubSystem = null,1553 subsystem: ?std.Target.SubSystem = null,
15541554
1555 entry_symbol_name: ?[]const u8 = null,
1556
1555 /// Overrides the default stack size1557 /// Overrides the default stack size
1556 stack_size: ?u64 = null,1558 stack_size: ?u64 = null,
15571559
...@@ -2253,6 +2255,11 @@ pub const LibExeObjStep = struct {...@@ -2253,6 +2255,11 @@ pub const LibExeObjStep = struct {
2253 try zig_args.append(@tagName(builder.color));2255 try zig_args.append(@tagName(builder.color));
2254 }2256 }
22552257
2258 if (self.entry_symbol_name) |entry| {
2259 try zig_args.append("--entry");
2260 try zig_args.append(entry);
2261 }
2262
2256 if (self.stack_size) |stack_size| {2263 if (self.stack_size) |stack_size| {
2257 try zig_args.append("--stack");2264 try zig_args.append("--stack");
2258 try zig_args.append(try std.fmt.allocPrint(builder.allocator, "{}", .{stack_size}));2265 try zig_args.append(try std.fmt.allocPrint(builder.allocator, "{}", .{stack_size}));
src/Compilation.zig+2
...@@ -765,6 +765,7 @@ pub const InitOptions = struct {...@@ -765,6 +765,7 @@ pub const InitOptions = struct {
765 /// infinite recursion.765 /// infinite recursion.
766 skip_linker_dependencies: bool = false,766 skip_linker_dependencies: bool = false,
767 parent_compilation_link_libc: bool = false,767 parent_compilation_link_libc: bool = false,
768 entry: ?[]const u8 = null,
768 stack_size_override: ?u64 = null,769 stack_size_override: ?u64 = null,
769 image_base_override: ?u64 = null,770 image_base_override: ?u64 = null,
770 self_exe_path: ?[]const u8 = null,771 self_exe_path: ?[]const u8 = null,
...@@ -1478,6 +1479,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {...@@ -1478,6 +1479,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
1478 .linker_optimization = linker_optimization,1479 .linker_optimization = linker_optimization,
1479 .major_subsystem_version = options.major_subsystem_version,1480 .major_subsystem_version = options.major_subsystem_version,
1480 .minor_subsystem_version = options.minor_subsystem_version,1481 .minor_subsystem_version = options.minor_subsystem_version,
1482 .entry = options.entry,
1481 .stack_size_override = options.stack_size_override,1483 .stack_size_override = options.stack_size_override,
1482 .image_base_override = options.image_base_override,1484 .image_base_override = options.image_base_override,
1483 .include_compiler_rt = include_compiler_rt,1485 .include_compiler_rt = include_compiler_rt,
src/clang_options_data.zig+9-2
...@@ -1655,7 +1655,7 @@ flagpsl("MT"),...@@ -1655,7 +1655,7 @@ flagpsl("MT"),
1655.{1655.{
1656 .name = "entry",1656 .name = "entry",
1657 .syntax = .flag,1657 .syntax = .flag,
1658 .zig_equivalent = .other,1658 .zig_equivalent = .entry,
1659 .pd1 = false,1659 .pd1 = false,
1660 .pd2 = true,1660 .pd2 = true,
1661 .psl = false,1661 .psl = false,
...@@ -6701,7 +6701,14 @@ joinpd1("Z"),...@@ -6701,7 +6701,14 @@ joinpd1("Z"),
6701joinpd1("a"),6701joinpd1("a"),
6702jspd1("b"),6702jspd1("b"),
6703joinpd1("d"),6703joinpd1("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},
6705.{6712.{
6706 .name = "l",6713 .name = "l",
6707 .syntax = .joined_or_separate,6714 .syntax = .joined_or_separate,
src/link.zig+1
...@@ -68,6 +68,7 @@ pub const Options = struct {...@@ -68,6 +68,7 @@ pub const Options = struct {
68 /// the binary file does not already have such a section.68 /// the binary file does not already have such a section.
69 program_code_size_hint: u64 = 256 * 1024,69 program_code_size_hint: u64 = 256 * 1024,
70 entry_addr: ?u64 = null,70 entry_addr: ?u64 = null,
71 entry: ?[]const u8,
71 stack_size_override: ?u64,72 stack_size_override: ?u64,
72 image_base_override: ?u64,73 image_base_override: ?u64,
73 include_compiler_rt: bool,74 include_compiler_rt: bool,
src/link/Coff.zig+5
...@@ -925,6 +925,7 @@ fn linkWithLLD(self: *Coff, comp: *Compilation) !void {...@@ -925,6 +925,7 @@ fn linkWithLLD(self: *Coff, comp: *Compilation) !void {
925 _ = try man.addFile(key.status.success.object_path, null);925 _ = try man.addFile(key.status.success.object_path, null);
926 }926 }
927 try man.addOptionalFile(module_obj_path);927 try man.addOptionalFile(module_obj_path);
928 man.hash.addOptionalBytes(self.base.options.entry);
928 man.hash.addOptional(self.base.options.stack_size_override);929 man.hash.addOptional(self.base.options.stack_size_override);
929 man.hash.addOptional(self.base.options.image_base_override);930 man.hash.addOptional(self.base.options.image_base_override);
930 man.hash.addListOfBytes(self.base.options.lib_dirs);931 man.hash.addListOfBytes(self.base.options.lib_dirs);
...@@ -1045,6 +1046,10 @@ fn linkWithLLD(self: *Coff, comp: *Compilation) !void {...@@ -1045,6 +1046,10 @@ fn linkWithLLD(self: *Coff, comp: *Compilation) !void {
1045 try argv.append("-DLL");1046 try argv.append("-DLL");
1046 }1047 }
10471048
1049 if (self.base.options.entry) |entry| {
1050 try argv.append(try allocPrint(arena, "-ENTRY:{s}", .{entry}));
1051 }
1052
1048 if (self.base.options.tsaware) {1053 if (self.base.options.tsaware) {
1049 try argv.append("-tsaware");1054 try argv.append("-tsaware");
1050 }1055 }
src/link/Elf.zig+8-1
...@@ -1316,6 +1316,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {...@@ -1316,6 +1316,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
13161316
1317 // We can skip hashing libc and libc++ components that we are in charge of building from Zig1317 // We can skip hashing libc and libc++ components that we are in charge of building from Zig
1318 // installation sources because they are always a product of the compiler version + target information.1318 // installation sources because they are always a product of the compiler version + target information.
1319 man.hash.addOptionalBytes(self.base.options.entry);
1319 man.hash.add(stack_size);1320 man.hash.add(stack_size);
1320 man.hash.addOptional(self.base.options.image_base_override);1321 man.hash.addOptional(self.base.options.image_base_override);
1321 man.hash.add(gc_sections);1322 man.hash.add(gc_sections);
...@@ -1438,6 +1439,11 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {...@@ -1438,6 +1439,11 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
1438 self.base.options.linker_optimization,1439 self.base.options.linker_optimization,
1439 }));1440 }));
14401441
1442 if (self.base.options.entry) |entry| {
1443 try argv.append("--entry");
1444 try argv.append(entry);
1445 }
1446
1441 if (self.base.options.output_mode == .Exe) {1447 if (self.base.options.output_mode == .Exe) {
1442 try argv.append("-z");1448 try argv.append("-z");
1443 try argv.append(try std.fmt.allocPrint(arena, "stack-size={d}", .{stack_size}));1449 try argv.append(try std.fmt.allocPrint(arena, "stack-size={d}", .{stack_size}));
...@@ -2786,7 +2792,8 @@ pub fn updateDeclExports(...@@ -2786,7 +2792,8 @@ pub fn updateDeclExports(
2786 const stb_bits: u8 = switch (exp.options.linkage) {2792 const stb_bits: u8 = switch (exp.options.linkage) {
2787 .Internal => elf.STB_LOCAL,2793 .Internal => elf.STB_LOCAL,
2788 .Strong => blk: {2794 .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)) {
2790 self.entry_addr = decl_sym.st_value;2797 self.entry_addr = decl_sym.st_value;
2791 }2798 }
2792 break :blk elf.STB_GLOBAL;2799 break :blk elf.STB_GLOBAL;
src/link/Wasm.zig+6
...@@ -1018,6 +1018,7 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {...@@ -1018,6 +1018,7 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
1018 }1018 }
1019 try man.addOptionalFile(module_obj_path);1019 try man.addOptionalFile(module_obj_path);
1020 try man.addOptionalFile(compiler_rt_path);1020 try man.addOptionalFile(compiler_rt_path);
1021 man.hash.addOptionalBytes(self.base.options.entry);
1021 man.hash.addOptional(self.base.options.stack_size_override);1022 man.hash.addOptional(self.base.options.stack_size_override);
1022 man.hash.add(self.base.options.import_memory);1023 man.hash.add(self.base.options.import_memory);
1023 man.hash.addOptional(self.base.options.initial_memory);1024 man.hash.addOptional(self.base.options.initial_memory);
...@@ -1165,6 +1166,11 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {...@@ -1165,6 +1166,11 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
1165 }1166 }
1166 }1167 }
11671168
1169 if (self.base.options.entry) |entry| {
1170 try argv.append("--entry");
1171 try argv.append(entry);
1172 }
1173
1168 if (self.base.options.output_mode == .Exe) {1174 if (self.base.options.output_mode == .Exe) {
1169 // Increase the default stack size to a more reasonable value of 1MB instead of1175 // Increase the default stack size to a more reasonable value of 1MB instead of
1170 // the default of 1 Wasm page being 64KB, unless overridden by the user.1176 // 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 =...@@ -400,6 +400,7 @@ const usage_build_generic =
400 \\ --dynamic-linker [path] Set the dynamic interpreter path (usually ld.so)400 \\ --dynamic-linker [path] Set the dynamic interpreter path (usually ld.so)
401 \\ --sysroot [path] Set the system root directory (usually /)401 \\ --sysroot [path] Set the system root directory (usually /)
402 \\ --version [ver] Dynamic library semver402 \\ --version [ver] Dynamic library semver
403 \\ --entry [name] Set the entrypoint symbol name
403 \\ -fsoname[=name] Override the default SONAME value404 \\ -fsoname[=name] Override the default SONAME value
404 \\ -fno-soname Disable emitting a SONAME405 \\ -fno-soname Disable emitting a SONAME
405 \\ -fLLD Force using LLD as the linker406 \\ -fLLD Force using LLD as the linker
...@@ -643,6 +644,7 @@ fn buildOutputType(...@@ -643,6 +644,7 @@ fn buildOutputType(
643 var linker_optimization: ?u8 = null;644 var linker_optimization: ?u8 = null;
644 var test_evented_io = false;645 var test_evented_io = false;
645 var test_no_exec = false;646 var test_no_exec = false;
647 var entry: ?[]const u8 = null;
646 var stack_size_override: ?u64 = null;648 var stack_size_override: ?u64 = null;
647 var image_base_override: ?u64 = null;649 var image_base_override: ?u64 = null;
648 var use_llvm: ?bool = null;650 var use_llvm: ?bool = null;
...@@ -843,6 +845,10 @@ fn buildOutputType(...@@ -843,6 +845,10 @@ fn buildOutputType(
843 if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg});845 if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg});
844 i += 1;846 i += 1;
845 optimize_mode_string = args[i];847 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];
846 } else if (mem.eql(u8, arg, "--stack")) {852 } else if (mem.eql(u8, arg, "--stack")) {
847 if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg});853 if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg});
848 i += 1;854 i += 1;
...@@ -1471,6 +1477,9 @@ fn buildOutputType(...@@ -1471,6 +1477,9 @@ fn buildOutputType(
1471 .sysroot => {1477 .sysroot => {
1472 sysroot = it.only_arg;1478 sysroot = it.only_arg;
1473 },1479 },
1480 .entry => {
1481 entry = it.only_arg;
1482 },
1474 }1483 }
1475 }1484 }
1476 // Parse linker args.1485 // Parse linker args.
...@@ -1612,6 +1621,12 @@ fn buildOutputType(...@@ -1612,6 +1621,12 @@ fn buildOutputType(
1612 fatal("unable to parse '{s}': {s}", .{ arg, @errorName(err) });1621 fatal("unable to parse '{s}': {s}", .{ arg, @errorName(err) });
1613 };1622 };
1614 have_version = true;1623 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];
1615 } else if (mem.eql(u8, arg, "--stack")) {1630 } else if (mem.eql(u8, arg, "--stack")) {
1616 i += 1;1631 i += 1;
1617 if (i >= linker_args.items.len) {1632 if (i >= linker_args.items.len) {
...@@ -2477,6 +2492,7 @@ fn buildOutputType(...@@ -2477,6 +2492,7 @@ fn buildOutputType(
2477 .minor_subsystem_version = minor_subsystem_version,2492 .minor_subsystem_version = minor_subsystem_version,
2478 .link_eh_frame_hdr = link_eh_frame_hdr,2493 .link_eh_frame_hdr = link_eh_frame_hdr,
2479 .link_emit_relocs = link_emit_relocs,2494 .link_emit_relocs = link_emit_relocs,
2495 .entry = entry,
2480 .stack_size_override = stack_size_override,2496 .stack_size_override = stack_size_override,
2481 .image_base_override = image_base_override,2497 .image_base_override = image_base_override,
2482 .strip = strip,2498 .strip = strip,
...@@ -4119,6 +4135,7 @@ pub const ClangArgIterator = struct {...@@ -4119,6 +4135,7 @@ pub const ClangArgIterator = struct {
4119 exec_model,4135 exec_model,
4120 emit_llvm,4136 emit_llvm,
4121 sysroot,4137 sysroot,
4138 entry,
4122 };4139 };
41234140
4124 const Args = struct {4141 const Args = struct {
tools/update_clang_options.zig+8
...@@ -416,6 +416,14 @@ const known_options = [_]KnownOpt{...@@ -416,6 +416,14 @@ const known_options = [_]KnownOpt{
416 .name = "sysroot",416 .name = "sysroot",
417 .ident = "sysroot",417 .ident = "sysroot",
418 },418 },
419 .{
420 .name = "entry",
421 .ident = "entry",
422 },
423 .{
424 .name = "e",
425 .ident = "entry",
426 },
419};427};
420428
421const blacklisted_options = [_][]const u8{};429const blacklisted_options = [_][]const u8{};