authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-01-26 14:59:15-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-02 21:50:52-07:00
log7e61bdbaa48dcf3a280c5c9a6b10c26ef4f687d5
tree0cb022593e96d3cbb08f0a6a1812e6a62ccc68b9
parent340bb8198f58a6bdae3352314f4ff9bdb03d7c38

zig cc: add --hash-style linker parameter

This is only relevant for ELF files. I also fixed a bug where passing a zig source file to `zig cc` would incorrectly punt to clang because it thought there were no positional arguments.

4 files changed, 32 insertions(+), 1 deletions(-)

src/Compilation.zig+2
......@@ -770,6 +770,7 @@ pub const InitOptions = struct {
770770 /// infinite recursion.
771771 skip_linker_dependencies: bool = false,
772772 parent_compilation_link_libc: bool = false,
773 hash_style: link.HashStyle = .both,
773774 entry: ?[]const u8 = null,
774775 stack_size_override: ?u64 = null,
775776 image_base_override: ?u64 = null,
......@@ -1520,6 +1521,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
15201521 .is_test = options.is_test,
15211522 .wasi_exec_model = wasi_exec_model,
15221523 .use_stage1 = use_stage1,
1524 .hash_style = options.hash_style,
15231525 .enable_link_snapshots = options.enable_link_snapshots,
15241526 .native_darwin_sdk = options.native_darwin_sdk,
15251527 .install_name = options.install_name,
src/link.zig+3
......@@ -127,6 +127,7 @@ pub const Options = struct {
127127 disable_lld_caching: bool,
128128 is_test: bool,
129129 use_stage1: bool,
130 hash_style: HashStyle,
130131 major_subsystem_version: ?u32,
131132 minor_subsystem_version: ?u32,
132133 gc_sections: ?bool = null,
......@@ -166,6 +167,8 @@ pub const Options = struct {
166167 }
167168};
168169
170pub const HashStyle = enum { sysv, gnu, both };
171
169172pub const File = struct {
170173 tag: Tag,
171174 options: Options,
src/link/Elf.zig+7
......@@ -1337,6 +1337,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
13371337 man.hash.add(self.base.options.z_noexecstack);
13381338 man.hash.add(self.base.options.z_now);
13391339 man.hash.add(self.base.options.z_relro);
1340 man.hash.add(self.base.options.hash_style);
13401341 // strip does not need to go into the linker hash because it is part of the hash namespace
13411342 if (self.base.options.link_libc) {
13421343 man.hash.add(self.base.options.libc_installation != null);
......@@ -1448,6 +1449,12 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
14481449 try argv.append(entry);
14491450 }
14501451
1452 switch (self.base.options.hash_style) {
1453 .gnu => try argv.append("--hash-style=gnu"),
1454 .sysv => try argv.append("--hash-style=sysv"),
1455 .both => {}, // this is the default
1456 }
1457
14511458 if (self.base.options.output_mode == .Exe) {
14521459 try argv.append("-z");
14531460 try argv.append(try std.fmt.allocPrint(arena, "stack-size={d}", .{stack_size}));
src/main.zig+20-1
......@@ -672,6 +672,7 @@ fn buildOutputType(
672672 var enable_link_snapshots: bool = false;
673673 var native_darwin_sdk: ?std.zig.system.darwin.DarwinSDK = null;
674674 var install_name: ?[]const u8 = null;
675 var hash_style: link.HashStyle = .both;
675676
676677 // e.g. -m3dnow or -mno-outline-atomics. They correspond to std.Target llvm cpu feature names.
677678 // This array is populated by zig cc frontend and then has to be converted to zig-style
......@@ -1778,6 +1779,19 @@ fn buildOutputType(
17781779 .path = linker_args.items[i],
17791780 .must_link = true,
17801781 });
1782 } else if (mem.eql(u8, arg, "-hash-style") or
1783 mem.eql(u8, arg, "--hash-style"))
1784 {
1785 i += 1;
1786 if (i >= linker_args.items.len) {
1787 fatal("expected linker arg after '{s}'", .{arg});
1788 }
1789 const next_arg = linker_args.items[i];
1790 hash_style = std.meta.stringToEnum(link.HashStyle, next_arg) orelse {
1791 fatal("expected [sysv|gnu|both] after --hash-style, found '{s}'", .{
1792 next_arg,
1793 });
1794 };
17811795 } else {
17821796 warn("unsupported linker arg: {s}", .{arg});
17831797 }
......@@ -1847,8 +1861,12 @@ fn buildOutputType(
18471861 }
18481862 },
18491863 }
1850 if (c_source_files.items.len == 0 and link_objects.items.len == 0) {
1864 if (c_source_files.items.len == 0 and
1865 link_objects.items.len == 0 and
1866 root_src_file == null)
1867 {
18511868 // For example `zig cc` and no args should print the "no input files" message.
1869 // There could be other reasons to punt to clang, for example, --help.
18521870 return punt_to_clang(arena, all_args);
18531871 }
18541872 },
......@@ -2491,6 +2509,7 @@ fn buildOutputType(
24912509 .use_lld = use_lld,
24922510 .use_clang = use_clang,
24932511 .use_stage1 = use_stage1,
2512 .hash_style = hash_style,
24942513 .rdynamic = rdynamic,
24952514 .linker_script = linker_script,
24962515 .version_script = version_script,