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.
This commit is contained in:
Andrew Kelley 2022-01-26 14:59:15 -07:00
parent 35503b3d3f
commit 40c9ce2caf
7 changed files with 39 additions and 7 deletions

View File

@ -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);

View File

@ -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,

View File

@ -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);

View File

@ -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}));

View File

@ -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);

View File

@ -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);

View File

@ -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,