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 {...@@ -1554,6 +1554,8 @@ pub const LibExeObjStep = struct {
15541554
1555 subsystem: ?std.Target.SubSystem = null,1555 subsystem: ?std.Target.SubSystem = null,
15561556
1557 entry_symbol_name: ?[]const u8 = null,
1558
1557 /// Overrides the default stack size1559 /// Overrides the default stack size
1558 stack_size: ?u64 = null,1560 stack_size: ?u64 = null,
15591561
...@@ -2255,6 +2257,11 @@ pub const LibExeObjStep = struct {...@@ -2255,6 +2257,11 @@ pub const LibExeObjStep = struct {
2255 try zig_args.append(@tagName(builder.color));2257 try zig_args.append(@tagName(builder.color));
2256 }2258 }
22572259
2260 if (self.entry_symbol_name) |entry| {
2261 try zig_args.append("--entry");
2262 try zig_args.append(entry);
2263 }
2264
2258 if (self.stack_size) |stack_size| {2265 if (self.stack_size) |stack_size| {
2259 try zig_args.append("--stack");2266 try zig_args.append("--stack");
2260 try zig_args.append(try std.fmt.allocPrint(builder.allocator, "{}", .{stack_size}));2267 try zig_args.append(try std.fmt.allocPrint(builder.allocator, "{}", .{stack_size}));
src/Compilation.zig+2
...@@ -791,6 +791,7 @@ pub const InitOptions = struct {...@@ -791,6 +791,7 @@ pub const InitOptions = struct {
791 /// infinite recursion.791 /// infinite recursion.
792 skip_linker_dependencies: bool = false,792 skip_linker_dependencies: bool = false,
793 parent_compilation_link_libc: bool = false,793 parent_compilation_link_libc: bool = false,
794 entry: ?[]const u8 = null,
794 stack_size_override: ?u64 = null,795 stack_size_override: ?u64 = null,
795 image_base_override: ?u64 = null,796 image_base_override: ?u64 = null,
796 self_exe_path: ?[]const u8 = null,797 self_exe_path: ?[]const u8 = null,
...@@ -1572,6 +1573,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {...@@ -1572,6 +1573,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
1572 .linker_optimization = linker_optimization,1573 .linker_optimization = linker_optimization,
1573 .major_subsystem_version = options.major_subsystem_version,1574 .major_subsystem_version = options.major_subsystem_version,
1574 .minor_subsystem_version = options.minor_subsystem_version,1575 .minor_subsystem_version = options.minor_subsystem_version,
1576 .entry = options.entry,
1575 .stack_size_override = options.stack_size_override,1577 .stack_size_override = options.stack_size_override,
1576 .image_base_override = options.image_base_override,1578 .image_base_override = options.image_base_override,
1577 .include_compiler_rt = include_compiler_rt,1579 .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
...@@ -84,6 +84,7 @@ pub const Options = struct {...@@ -84,6 +84,7 @@ pub const Options = struct {
84 /// the binary file does not already have such a section.84 /// the binary file does not already have such a section.
85 program_code_size_hint: u64 = 256 * 1024,85 program_code_size_hint: u64 = 256 * 1024,
86 entry_addr: ?u64 = null,86 entry_addr: ?u64 = null,
87 entry: ?[]const u8,
87 stack_size_override: ?u64,88 stack_size_override: ?u64,
88 image_base_override: ?u64,89 image_base_override: ?u64,
89 cache_mode: CacheMode,90 cache_mode: CacheMode,
src/link/Coff.zig+5
...@@ -951,6 +951,7 @@ fn linkWithLLD(self: *Coff, comp: *Compilation) !void {...@@ -951,6 +951,7 @@ fn linkWithLLD(self: *Coff, comp: *Compilation) !void {
951 _ = try man.addFile(key.status.success.object_path, null);951 _ = try man.addFile(key.status.success.object_path, null);
952 }952 }
953 try man.addOptionalFile(module_obj_path);953 try man.addOptionalFile(module_obj_path);
954 man.hash.addOptionalBytes(self.base.options.entry);
954 man.hash.addOptional(self.base.options.stack_size_override);955 man.hash.addOptional(self.base.options.stack_size_override);
955 man.hash.addOptional(self.base.options.image_base_override);956 man.hash.addOptional(self.base.options.image_base_override);
956 man.hash.addListOfBytes(self.base.options.lib_dirs);957 man.hash.addListOfBytes(self.base.options.lib_dirs);
...@@ -1070,6 +1071,10 @@ fn linkWithLLD(self: *Coff, comp: *Compilation) !void {...@@ -1070,6 +1071,10 @@ fn linkWithLLD(self: *Coff, comp: *Compilation) !void {
1070 try argv.append("-DLL");1071 try argv.append("-DLL");
1071 }1072 }
10721073
1074 if (self.base.options.entry) |entry| {
1075 try argv.append(try allocPrint(arena, "-ENTRY:{s}", .{entry}));
1076 }
1077
1073 if (self.base.options.tsaware) {1078 if (self.base.options.tsaware) {
1074 try argv.append("-tsaware");1079 try argv.append("-tsaware");
1075 }1080 }
src/link/Elf.zig+8-1
...@@ -1396,6 +1396,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {...@@ -1396,6 +1396,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
13961396
1397 // We can skip hashing libc and libc++ components that we are in charge of building from Zig1397 // We can skip hashing libc and libc++ components that we are in charge of building from Zig
1398 // installation sources because they are always a product of the compiler version + target information.1398 // installation sources because they are always a product of the compiler version + target information.
1399 man.hash.addOptionalBytes(self.base.options.entry);
1399 man.hash.add(stack_size);1400 man.hash.add(stack_size);
1400 man.hash.addOptional(self.base.options.image_base_override);1401 man.hash.addOptional(self.base.options.image_base_override);
1401 man.hash.add(gc_sections);1402 man.hash.add(gc_sections);
...@@ -1518,6 +1519,11 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {...@@ -1518,6 +1519,11 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
1518 self.base.options.linker_optimization,1519 self.base.options.linker_optimization,
1519 }));1520 }));
15201521
1522 if (self.base.options.entry) |entry| {
1523 try argv.append("--entry");
1524 try argv.append(entry);
1525 }
1526
1521 if (self.base.options.output_mode == .Exe) {1527 if (self.base.options.output_mode == .Exe) {
1522 try argv.append("-z");1528 try argv.append("-z");
1523 try argv.append(try std.fmt.allocPrint(arena, "stack-size={d}", .{stack_size}));1529 try argv.append(try std.fmt.allocPrint(arena, "stack-size={d}", .{stack_size}));
...@@ -2883,7 +2889,8 @@ pub fn updateDeclExports(...@@ -2883,7 +2889,8 @@ pub fn updateDeclExports(
2883 const stb_bits: u8 = switch (exp.options.linkage) {2889 const stb_bits: u8 = switch (exp.options.linkage) {
2884 .Internal => elf.STB_LOCAL,2890 .Internal => elf.STB_LOCAL,
2885 .Strong => blk: {2891 .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)) {
2887 self.entry_addr = decl_sym.st_value;2894 self.entry_addr = decl_sym.st_value;
2888 }2895 }
2889 break :blk elf.STB_GLOBAL;2896 break :blk elf.STB_GLOBAL;
src/link/Wasm.zig+6
...@@ -1137,6 +1137,7 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {...@@ -1137,6 +1137,7 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
1137 }1137 }
1138 try man.addOptionalFile(module_obj_path);1138 try man.addOptionalFile(module_obj_path);
1139 try man.addOptionalFile(compiler_rt_path);1139 try man.addOptionalFile(compiler_rt_path);
1140 man.hash.addOptionalBytes(self.base.options.entry);
1140 man.hash.addOptional(self.base.options.stack_size_override);1141 man.hash.addOptional(self.base.options.stack_size_override);
1141 man.hash.add(self.base.options.import_memory);1142 man.hash.add(self.base.options.import_memory);
1142 man.hash.add(self.base.options.import_table);1143 man.hash.add(self.base.options.import_table);
...@@ -1295,6 +1296,11 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {...@@ -1295,6 +1296,11 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
1295 }1296 }
1296 }1297 }
12971298
1299 if (self.base.options.entry) |entry| {
1300 try argv.append("--entry");
1301 try argv.append(entry);
1302 }
1303
1298 if (self.base.options.output_mode == .Exe) {1304 if (self.base.options.output_mode == .Exe) {
1299 // Increase the default stack size to a more reasonable value of 1MB instead of1305 // Increase the default stack size to a more reasonable value of 1MB instead of
1300 // the default of 1 Wasm page being 64KB, unless overridden by the user.1306 // 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
...@@ -647,6 +648,7 @@ fn buildOutputType(...@@ -647,6 +648,7 @@ fn buildOutputType(
647 var linker_optimization: ?u8 = null;648 var linker_optimization: ?u8 = null;
648 var test_evented_io = false;649 var test_evented_io = false;
649 var test_no_exec = false;650 var test_no_exec = false;
651 var entry: ?[]const u8 = null;
650 var stack_size_override: ?u64 = null;652 var stack_size_override: ?u64 = null;
651 var image_base_override: ?u64 = null;653 var image_base_override: ?u64 = null;
652 var use_llvm: ?bool = null;654 var use_llvm: ?bool = null;
...@@ -847,6 +849,10 @@ fn buildOutputType(...@@ -847,6 +849,10 @@ fn buildOutputType(
847 if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg});849 if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg});
848 i += 1;850 i += 1;
849 optimize_mode_string = args[i];851 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];
850 } else if (mem.eql(u8, arg, "--stack")) {856 } else if (mem.eql(u8, arg, "--stack")) {
851 if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg});857 if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg});
852 i += 1;858 i += 1;
...@@ -1479,6 +1485,9 @@ fn buildOutputType(...@@ -1479,6 +1485,9 @@ fn buildOutputType(
1479 .sysroot => {1485 .sysroot => {
1480 sysroot = it.only_arg;1486 sysroot = it.only_arg;
1481 },1487 },
1488 .entry => {
1489 entry = it.only_arg;
1490 },
1482 }1491 }
1483 }1492 }
1484 // Parse linker args.1493 // Parse linker args.
...@@ -1624,6 +1633,12 @@ fn buildOutputType(...@@ -1624,6 +1633,12 @@ fn buildOutputType(
1624 fatal("unable to parse '{s}': {s}", .{ arg, @errorName(err) });1633 fatal("unable to parse '{s}': {s}", .{ arg, @errorName(err) });
1625 };1634 };
1626 have_version = true;1635 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];
1627 } else if (mem.eql(u8, arg, "--stack")) {1642 } else if (mem.eql(u8, arg, "--stack")) {
1628 i += 1;1643 i += 1;
1629 if (i >= linker_args.items.len) {1644 if (i >= linker_args.items.len) {
...@@ -2500,6 +2515,7 @@ fn buildOutputType(...@@ -2500,6 +2515,7 @@ fn buildOutputType(
2500 .minor_subsystem_version = minor_subsystem_version,2515 .minor_subsystem_version = minor_subsystem_version,
2501 .link_eh_frame_hdr = link_eh_frame_hdr,2516 .link_eh_frame_hdr = link_eh_frame_hdr,
2502 .link_emit_relocs = link_emit_relocs,2517 .link_emit_relocs = link_emit_relocs,
2518 .entry = entry,
2503 .stack_size_override = stack_size_override,2519 .stack_size_override = stack_size_override,
2504 .image_base_override = image_base_override,2520 .image_base_override = image_base_override,
2505 .strip = strip,2521 .strip = strip,
...@@ -4143,6 +4159,7 @@ pub const ClangArgIterator = struct {...@@ -4143,6 +4159,7 @@ pub const ClangArgIterator = struct {
4143 exec_model,4159 exec_model,
4144 emit_llvm,4160 emit_llvm,
4145 sysroot,4161 sysroot,
4162 entry,
4146 };4163 };
41474164
4148 const Args = struct {4165 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{};