authorgravatar for kenta@lithdew.netKenta Iwasaki <kenta@lithdew.net> 2022-01-01 06:32:37+09:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-01-19 11:22:10-07:00
log5ae3e4e9bd5216c7cc2305c8996ed413c89c59e7
treefc0a9852022f89d33d249540527296e7477e7f19
parentbeb7495e19d23b4814e16772888e80688ad10e47

lld: allow for entrypoint symbol name to be set

This commit enables for the entrypoint symbol to be set when linking ELF or WebAssembly modules with lld using the Zig compiler.

8 files changed, 38 insertions(+), 0 deletions(-)

lib/std/build.zig+8
...@@ -1554,6 +1554,9 @@ pub const LibExeObjStep = struct {...@@ -1554,6 +1554,9 @@ pub const LibExeObjStep = struct {
15541554
1555 subsystem: ?std.Target.SubSystem = null,1555 subsystem: ?std.Target.SubSystem = null,
15561556
1557 /// Entrypoint symbol name
1558 entry: ?[]const u8 = null,
1559
1557 /// Overrides the default stack size1560 /// Overrides the default stack size
1558 stack_size: ?u64 = null,1561 stack_size: ?u64 = null,
15591562
...@@ -2255,6 +2258,11 @@ pub const LibExeObjStep = struct {...@@ -2255,6 +2258,11 @@ pub const LibExeObjStep = struct {
2255 try zig_args.append(@tagName(builder.color));2258 try zig_args.append(@tagName(builder.color));
2256 }2259 }
22572260
2261 if (self.entry) |entry| {
2262 try zig_args.append("--entry");
2263 try zig_args.append(entry);
2264 }
2265
2258 if (self.stack_size) |stack_size| {2266 if (self.stack_size) |stack_size| {
2259 try zig_args.append("--stack");2267 try zig_args.append("--stack");
2260 try zig_args.append(try std.fmt.allocPrint(builder.allocator, "{}", .{stack_size}));2268 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/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+1
...@@ -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);
src/link/Elf.zig+6
...@@ -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}));
src/link/MachO.zig+1
...@@ -509,6 +509,7 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {...@@ -509,6 +509,7 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
509 try man.addOptionalFile(module_obj_path);509 try man.addOptionalFile(module_obj_path);
510 // We can skip hashing libc and libc++ components that we are in charge of building from Zig510 // We can skip hashing libc and libc++ components that we are in charge of building from Zig
511 // installation sources because they are always a product of the compiler version + target information.511 // installation sources because they are always a product of the compiler version + target information.
512 man.hash.addOptionalBytes(self.base.options.entry);
512 man.hash.add(stack_size);513 man.hash.add(stack_size);
513 man.hash.addListOfBytes(self.base.options.lib_dirs);514 man.hash.addListOfBytes(self.base.options.lib_dirs);
514 man.hash.addListOfBytes(self.base.options.framework_dirs);515 man.hash.addListOfBytes(self.base.options.framework_dirs);
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+13
...@@ -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;
...@@ -1624,6 +1630,12 @@ fn buildOutputType(...@@ -1624,6 +1630,12 @@ fn buildOutputType(
1624 fatal("unable to parse '{s}': {s}", .{ arg, @errorName(err) });1630 fatal("unable to parse '{s}': {s}", .{ arg, @errorName(err) });
1625 };1631 };
1626 have_version = true;1632 have_version = true;
1633 } else if (mem.eql(u8, arg, "-e") or mem.eql(u8, arg, "--entry")) {
1634 i += 1;
1635 if (i >= linker_args.items.len) {
1636 fatal("expected linker arg after '{s}'", .{arg});
1637 }
1638 entry = linker_args.items[i];
1627 } else if (mem.eql(u8, arg, "--stack")) {1639 } else if (mem.eql(u8, arg, "--stack")) {
1628 i += 1;1640 i += 1;
1629 if (i >= linker_args.items.len) {1641 if (i >= linker_args.items.len) {
...@@ -2500,6 +2512,7 @@ fn buildOutputType(...@@ -2500,6 +2512,7 @@ fn buildOutputType(
2500 .minor_subsystem_version = minor_subsystem_version,2512 .minor_subsystem_version = minor_subsystem_version,
2501 .link_eh_frame_hdr = link_eh_frame_hdr,2513 .link_eh_frame_hdr = link_eh_frame_hdr,
2502 .link_emit_relocs = link_emit_relocs,2514 .link_emit_relocs = link_emit_relocs,
2515 .entry = entry,
2503 .stack_size_override = stack_size_override,2516 .stack_size_override = stack_size_override,
2504 .image_base_override = image_base_override,2517 .image_base_override = image_base_override,
2505 .strip = strip,2518 .strip = strip,