diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2022-01-26 14:59:15 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2022-01-26 15:01:59 -0700 |
| commit | 40c9ce2caf8f024a249b3524813eb495cf1341b3 (patch) | |
| tree | 84dd4cfca306b33956746650155dd14d4e4fafac | |
| parent | 35503b3d3fe1bfce19f1ea3e78a75ce87b0ed646 (diff) | |
| download | zig-40c9ce2caf8f024a249b3524813eb495cf1341b3.tar.gz zig-40c9ce2caf8f024a249b3524813eb495cf1341b3.zip | |
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.
| -rw-r--r-- | src/Compilation.zig | 7 | ||||
| -rw-r--r-- | src/link.zig | 3 | ||||
| -rw-r--r-- | src/link/Coff.zig | 2 | ||||
| -rw-r--r-- | src/link/Elf.zig | 9 | ||||
| -rw-r--r-- | src/link/MachO.zig | 2 | ||||
| -rw-r--r-- | src/link/Wasm.zig | 2 | ||||
| -rw-r--r-- | src/main.zig | 21 |
7 files changed, 39 insertions, 7 deletions
diff --git a/src/Compilation.zig b/src/Compilation.zig index dec98ed781..b804227bb9 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -791,6 +791,7 @@ pub const InitOptions = struct { /// infinite recursion. skip_linker_dependencies: bool = false, parent_compilation_link_libc: bool = false, + hash_style: link.HashStyle = .both, entry: ?[]const u8 = null, stack_size_override: ?u64 = null, image_base_override: ?u64 = null, @@ -1610,6 +1611,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation { .is_test = options.is_test, .wasi_exec_model = wasi_exec_model, .use_stage1 = use_stage1, + .hash_style = options.hash_style, .enable_link_snapshots = options.enable_link_snapshots, .native_darwin_sdk = options.native_darwin_sdk, .install_name = options.install_name, @@ -2227,7 +2229,7 @@ fn prepareWholeEmitSubPath(arena: Allocator, opt_emit: ?EmitLoc) error{OutOfMemo /// to remind the programmer to update multiple related pieces of code that /// are in different locations. Bump this number when adding or deleting /// anything from the link cache manifest. -pub const link_hash_implementation_version = 1; +pub const link_hash_implementation_version = 2; fn addNonIncrementalStuffToCacheManifest(comp: *Compilation, man: *Cache.Manifest) !void { const gpa = comp.gpa; @@ -2237,7 +2239,7 @@ fn addNonIncrementalStuffToCacheManifest(comp: *Compilation, man: *Cache.Manifes defer arena_allocator.deinit(); const arena = arena_allocator.allocator(); - comptime assert(link_hash_implementation_version == 1); + comptime assert(link_hash_implementation_version == 2); if (comp.bin_file.options.module) |mod| { const main_zig_file = try mod.main_pkg.root_src_directory.join(arena, &[_][]const u8{ @@ -2308,6 +2310,7 @@ fn addNonIncrementalStuffToCacheManifest(comp: *Compilation, man: *Cache.Manifes man.hash.add(comp.bin_file.options.z_noexecstack); man.hash.add(comp.bin_file.options.z_now); man.hash.add(comp.bin_file.options.z_relro); + man.hash.add(comp.bin_file.options.hash_style); man.hash.add(comp.bin_file.options.include_compiler_rt); if (comp.bin_file.options.link_libc) { man.hash.add(comp.bin_file.options.libc_installation != null); diff --git a/src/link.zig b/src/link.zig index 817cb4c052..883d79de34 100644 --- a/src/link.zig +++ b/src/link.zig @@ -146,6 +146,7 @@ pub const Options = struct { disable_lld_caching: bool, is_test: bool, use_stage1: bool, + hash_style: HashStyle, major_subsystem_version: ?u32, minor_subsystem_version: ?u32, gc_sections: ?bool = null, @@ -191,6 +192,8 @@ pub const Options = struct { } }; +pub const HashStyle = enum { sysv, gnu, both }; + pub const File = struct { tag: Tag, options: Options, diff --git a/src/link/Coff.zig b/src/link/Coff.zig index 20a76f49a2..152e078b66 100644 --- a/src/link/Coff.zig +++ b/src/link/Coff.zig @@ -941,7 +941,7 @@ fn linkWithLLD(self: *Coff, comp: *Compilation) !void { man = comp.cache_parent.obtain(); self.base.releaseLock(); - comptime assert(Compilation.link_hash_implementation_version == 1); + comptime assert(Compilation.link_hash_implementation_version == 2); for (self.base.options.objects) |obj| { _ = try man.addFile(obj.path, null); diff --git a/src/link/Elf.zig b/src/link/Elf.zig index 15fff6db79..8a9d98189a 100644 --- a/src/link/Elf.zig +++ b/src/link/Elf.zig @@ -1413,7 +1413,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void { // We are about to obtain this lock, so here we give other processes a chance first. self.base.releaseLock(); - comptime assert(Compilation.link_hash_implementation_version == 1); + comptime assert(Compilation.link_hash_implementation_version == 2); try man.addOptionalFile(self.base.options.linker_script); try man.addOptionalFile(self.base.options.version_script); @@ -1447,6 +1447,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void { man.hash.add(self.base.options.z_noexecstack); man.hash.add(self.base.options.z_now); man.hash.add(self.base.options.z_relro); + man.hash.add(self.base.options.hash_style); // strip does not need to go into the linker hash because it is part of the hash namespace if (self.base.options.link_libc) { man.hash.add(self.base.options.libc_installation != null); @@ -1558,6 +1559,12 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void { try argv.append(entry); } + switch (self.base.options.hash_style) { + .gnu => try argv.append("--hash-style=gnu"), + .sysv => try argv.append("--hash-style=sysv"), + .both => {}, // this is the default + } + if (self.base.options.output_mode == .Exe) { try argv.append("-z"); try argv.append(try std.fmt.allocPrint(arena, "stack-size={d}", .{stack_size})); diff --git a/src/link/MachO.zig b/src/link/MachO.zig index cd1d197010..a9950fcc8d 100644 --- a/src/link/MachO.zig +++ b/src/link/MachO.zig @@ -497,7 +497,7 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void { // We are about to obtain this lock, so here we give other processes a chance first. self.base.releaseLock(); - comptime assert(Compilation.link_hash_implementation_version == 1); + comptime assert(Compilation.link_hash_implementation_version == 2); for (self.base.options.objects) |obj| { _ = try man.addFile(obj.path, null); diff --git a/src/link/Wasm.zig b/src/link/Wasm.zig index 9a9c6c464c..7d01ef4083 100644 --- a/src/link/Wasm.zig +++ b/src/link/Wasm.zig @@ -1203,7 +1203,7 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void { // We are about to obtain this lock, so here we give other processes a chance first. self.base.releaseLock(); - comptime assert(Compilation.link_hash_implementation_version == 1); + comptime assert(Compilation.link_hash_implementation_version == 2); for (self.base.options.objects) |obj| { _ = try man.addFile(obj.path, null); diff --git a/src/main.zig b/src/main.zig index 3f74a64c36..b32da74ef6 100644 --- a/src/main.zig +++ b/src/main.zig @@ -676,6 +676,7 @@ fn buildOutputType( var enable_link_snapshots: bool = false; var native_darwin_sdk: ?std.zig.system.darwin.DarwinSDK = null; var install_name: ?[]const u8 = null; + var hash_style: link.HashStyle = .both; // e.g. -m3dnow or -mno-outline-atomics. They correspond to std.Target llvm cpu feature names. // This array is populated by zig cc frontend and then has to be converted to zig-style @@ -1790,6 +1791,19 @@ fn buildOutputType( .path = linker_args.items[i], .must_link = true, }); + } else if (mem.eql(u8, arg, "-hash-style") or + mem.eql(u8, arg, "--hash-style")) + { + i += 1; + if (i >= linker_args.items.len) { + fatal("expected linker arg after '{s}'", .{arg}); + } + const next_arg = linker_args.items[i]; + hash_style = std.meta.stringToEnum(link.HashStyle, next_arg) orelse { + fatal("expected [sysv|gnu|both] after --hash-style, found '{s}'", .{ + next_arg, + }); + }; } else { warn("unsupported linker arg: {s}", .{arg}); } @@ -1859,8 +1873,12 @@ fn buildOutputType( } }, } - if (c_source_files.items.len == 0 and link_objects.items.len == 0) { + if (c_source_files.items.len == 0 and + link_objects.items.len == 0 and + root_src_file == null) + { // For example `zig cc` and no args should print the "no input files" message. + // There could be other reasons to punt to clang, for example, --help. return punt_to_clang(arena, all_args); } }, @@ -2503,6 +2521,7 @@ fn buildOutputType( .use_lld = use_lld, .use_clang = use_clang, .use_stage1 = use_stage1, + .hash_style = hash_style, .rdynamic = rdynamic, .linker_script = linker_script, .version_script = version_script, |
