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 {
15541554
15551555 subsystem: ?std.Target.SubSystem = null,
15561556
1557 /// Entrypoint symbol name
1558 entry: ?[]const u8 = null,
1559
15571560 /// Overrides the default stack size
15581561 stack_size: ?u64 = null,
15591562
......@@ -2255,6 +2258,11 @@ pub const LibExeObjStep = struct {
22552258 try zig_args.append(@tagName(builder.color));
22562259 }
22572260
2261 if (self.entry) |entry| {
2262 try zig_args.append("--entry");
2263 try zig_args.append(entry);
2264 }
2265
22582266 if (self.stack_size) |stack_size| {
22592267 try zig_args.append("--stack");
22602268 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/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+1
......@@ -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);
src/link/Elf.zig+6
......@@ -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}));
src/link/MachO.zig+1
......@@ -509,6 +509,7 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
509509 try man.addOptionalFile(module_obj_path);
510510 // We can skip hashing libc and libc++ components that we are in charge of building from Zig
511511 // installation sources because they are always a product of the compiler version + target information.
512 man.hash.addOptionalBytes(self.base.options.entry);
512513 man.hash.add(stack_size);
513514 man.hash.addListOfBytes(self.base.options.lib_dirs);
514515 man.hash.addListOfBytes(self.base.options.framework_dirs);
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+13
......@@ -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;
......@@ -1624,6 +1630,12 @@ fn buildOutputType(
16241630 fatal("unable to parse '{s}': {s}", .{ arg, @errorName(err) });
16251631 };
16261632 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];
16271639 } else if (mem.eql(u8, arg, "--stack")) {
16281640 i += 1;
16291641 if (i >= linker_args.items.len) {
......@@ -2500,6 +2512,7 @@ fn buildOutputType(
25002512 .minor_subsystem_version = minor_subsystem_version,
25012513 .link_eh_frame_hdr = link_eh_frame_hdr,
25022514 .link_emit_relocs = link_emit_relocs,
2515 .entry = entry,
25032516 .stack_size_override = stack_size_override,
25042517 .image_base_override = image_base_override,
25052518 .strip = strip,