Commit fd6d1fe015

Andrew Kelley <andrew@ziglang.org>
2022-01-19 19:41:08
stage2: improvements to entry point handling
* rename `entry` to `entry_symbol_name` for the zig build API * integrate with `zig cc` command line options * integrate with COFF linking with LLD * integrate with self-hosted ELF linker * don't put it in the hash for MachO since it is ignored
1 parent 5ae3e4e
lib/std/build.zig
@@ -1554,8 +1554,7 @@ pub const LibExeObjStep = struct {
 
     subsystem: ?std.Target.SubSystem = null,
 
-    /// Entrypoint symbol name
-    entry: ?[]const u8 = null,
+    entry_symbol_name: ?[]const u8 = null,
 
     /// Overrides the default stack size
     stack_size: ?u64 = null,
@@ -2258,7 +2257,7 @@ pub const LibExeObjStep = struct {
             try zig_args.append(@tagName(builder.color));
         }
 
-        if (self.entry) |entry| {
+        if (self.entry_symbol_name) |entry| {
             try zig_args.append("--entry");
             try zig_args.append(entry);
         }
src/link/Coff.zig
@@ -1071,6 +1071,10 @@ fn linkWithLLD(self: *Coff, comp: *Compilation) !void {
             try argv.append("-DLL");
         }
 
+        if (self.base.options.entry) |entry| {
+            try argv.append(try allocPrint(arena, "-ENTRY:{s}", .{entry}));
+        }
+
         if (self.base.options.tsaware) {
             try argv.append("-tsaware");
         }
src/link/Elf.zig
@@ -2889,7 +2889,8 @@ pub fn updateDeclExports(
         const stb_bits: u8 = switch (exp.options.linkage) {
             .Internal => elf.STB_LOCAL,
             .Strong => blk: {
-                if (mem.eql(u8, exp.options.name, "_start")) {
+                const entry_name = self.base.options.entry orelse "_start";
+                if (mem.eql(u8, exp.options.name, entry_name)) {
                     self.entry_addr = decl_sym.st_value;
                 }
                 break :blk elf.STB_GLOBAL;
src/link/MachO.zig
@@ -509,7 +509,6 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
         try man.addOptionalFile(module_obj_path);
         // We can skip hashing libc and libc++ components that we are in charge of building from Zig
         // installation sources because they are always a product of the compiler version + target information.
-        man.hash.addOptionalBytes(self.base.options.entry);
         man.hash.add(stack_size);
         man.hash.addListOfBytes(self.base.options.lib_dirs);
         man.hash.addListOfBytes(self.base.options.framework_dirs);
src/clang_options_data.zig
@@ -1655,7 +1655,7 @@ flagpsl("MT"),
 .{
     .name = "entry",
     .syntax = .flag,
-    .zig_equivalent = .other,
+    .zig_equivalent = .entry,
     .pd1 = false,
     .pd2 = true,
     .psl = false,
@@ -6701,7 +6701,14 @@ joinpd1("Z"),
 joinpd1("a"),
 jspd1("b"),
 joinpd1("d"),
-jspd1("e"),
+.{
+    .name = "e",
+    .syntax = .joined_or_separate,
+    .zig_equivalent = .entry,
+    .pd1 = true,
+    .pd2 = false,
+    .psl = false,
+},
 .{
     .name = "l",
     .syntax = .joined_or_separate,
src/main.zig
@@ -1485,6 +1485,9 @@ fn buildOutputType(
                     .sysroot => {
                         sysroot = it.only_arg;
                     },
+                    .entry => {
+                        entry = it.only_arg;
+                    },
                 }
             }
             // Parse linker args.
@@ -4156,6 +4159,7 @@ pub const ClangArgIterator = struct {
         exec_model,
         emit_llvm,
         sysroot,
+        entry,
     };
 
     const Args = struct {
tools/update_clang_options.zig
@@ -416,6 +416,14 @@ const known_options = [_]KnownOpt{
         .name = "sysroot",
         .ident = "sysroot",
     },
+    .{
+        .name = "entry",
+        .ident = "entry",
+    },
+    .{
+        .name = "e",
+        .ident = "entry",
+    },
 };
 
 const blacklisted_options = [_][]const u8{};