From ae38575b42e7ff6f7254c0ae1f1f0519416d83f9 Mon Sep 17 00:00:00 2001 From: Alex Rønne Petersen Date: Sat, 3 May 2025 06:31:12 +0200 Subject: compiler: Rename misleading libcNeedsLibUnwind() function. It's about libc++, not libc. --- src/Compilation/Config.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/Compilation') diff --git a/src/Compilation/Config.zig b/src/Compilation/Config.zig index a4c69454b3..5019f47ebf 100644 --- a/src/Compilation/Config.zig +++ b/src/Compilation/Config.zig @@ -313,7 +313,7 @@ pub fn resolve(options: Options) ResolveError!Config { }; const link_libunwind = b: { - if (link_libcpp and target_util.libcNeedsLibUnwind(target)) { + if (link_libcpp and target_util.libCxxNeedsLibUnwind(target)) { if (options.link_libunwind == false) return error.LibCppRequiresLibUnwind; break :b true; } -- cgit v1.2.3 From f6476e9caeadea0c0c6b18841dffcf72bffdd582 Mon Sep 17 00:00:00 2001 From: Alex Rønne Petersen Date: Sat, 3 May 2025 06:31:51 +0200 Subject: compiler: Allow linking native glibc statically. This is generally ill-advised, but can be useful in some niche situations where the caveats don't apply. It might also be useful when providing a libc.txt that points to Eyra. --- src/Compilation/Config.zig | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/Compilation') diff --git a/src/Compilation/Config.zig b/src/Compilation/Config.zig index 5019f47ebf..1de0d83aa7 100644 --- a/src/Compilation/Config.zig +++ b/src/Compilation/Config.zig @@ -352,7 +352,7 @@ pub fn resolve(options: Options) ResolveError!Config { break :b .static; } if (explicitly_exe_or_dyn_lib and link_libc and - (target.isGnuLibC() or target_util.osRequiresLibC(target))) + (target_util.osRequiresLibC(target) or (target.isGnuLibC() and !options.resolved_target.is_native_abi))) { if (options.link_mode == .static) return error.LibCRequiresDynamicLinking; break :b .dynamic; @@ -367,11 +367,11 @@ pub fn resolve(options: Options) ResolveError!Config { if (options.link_mode) |link_mode| break :b link_mode; - if (explicitly_exe_or_dyn_lib and link_libc and - options.resolved_target.is_native_abi and target.abi.isMusl()) + if (explicitly_exe_or_dyn_lib and link_libc and options.resolved_target.is_native_abi and + (target.isGnuLibC() or target.isMuslLibC())) { // If targeting the system's native ABI and the system's libc is - // musl, link dynamically by default. + // glibc or musl, link dynamically by default. break :b .dynamic; } -- cgit v1.2.3 From d2f92e1797cf30c2fb0993d7e09de73e496144f5 Mon Sep 17 00:00:00 2001 From: Alex Rønne Petersen Date: Sat, 3 May 2025 06:32:15 +0200 Subject: compiler: Link libunwind when linking glibc statically. glibc's libc.a depends on the functions provided by libunwind. --- src/Compilation/Config.zig | 10 +++++++++- src/main.zig | 1 + src/target.zig | 4 ++++ 3 files changed, 14 insertions(+), 1 deletion(-) (limited to 'src/Compilation') diff --git a/src/Compilation/Config.zig b/src/Compilation/Config.zig index 1de0d83aa7..71df1d9311 100644 --- a/src/Compilation/Config.zig +++ b/src/Compilation/Config.zig @@ -129,6 +129,7 @@ pub const ResolveError = error{ LldCannotIncrementallyLink, LtoRequiresLld, SanitizeThreadRequiresLibCpp, + LibCRequiresLibUnwind, LibCppRequiresLibUnwind, OsRequiresLibC, LibCppRequiresLibC, @@ -312,7 +313,7 @@ pub fn resolve(options: Options) ResolveError!Config { break :b false; }; - const link_libunwind = b: { + var link_libunwind = b: { if (link_libcpp and target_util.libCxxNeedsLibUnwind(target)) { if (options.link_libunwind == false) return error.LibCppRequiresLibUnwind; break :b true; @@ -379,6 +380,13 @@ pub fn resolve(options: Options) ResolveError!Config { break :b .static; }; + // This is done here to avoid excessive duplicated logic due to the complex dependencies between these options. + if (options.output_mode == .Exe and link_libc and target_util.libCNeedsLibUnwind(target, link_mode)) { + if (options.link_libunwind == false) return error.LibCRequiresLibUnwind; + + link_libunwind = true; + } + const import_memory = options.import_memory orelse (options.output_mode == .Obj); const export_memory = b: { if (link_mode == .dynamic) { diff --git a/src/main.zig b/src/main.zig index cac0c78163..7eb5436ee5 100644 --- a/src/main.zig +++ b/src/main.zig @@ -4206,6 +4206,7 @@ fn createModule( error.LldCannotIncrementallyLink => fatal("self-hosted backends do not support linking with LLD", .{}), error.LtoRequiresLld => fatal("LTO requires using LLD", .{}), error.SanitizeThreadRequiresLibCpp => fatal("thread sanitization is (for now) implemented in C++, so it requires linking libc++", .{}), + error.LibCRequiresLibUnwind => fatal("libc of the specified target requires linking libunwind", .{}), error.LibCppRequiresLibUnwind => fatal("libc++ requires linking libunwind", .{}), error.OsRequiresLibC => fatal("the target OS requires using libc as the stable syscall interface", .{}), error.LibCppRequiresLibC => fatal("libc++ requires linking libc", .{}), diff --git a/src/target.zig b/src/target.zig index 1bafb1b8f7..21b701fc37 100644 --- a/src/target.zig +++ b/src/target.zig @@ -23,6 +23,10 @@ pub fn osRequiresLibC(target: std.Target) bool { return target.os.requiresLibC(); } +pub fn libCNeedsLibUnwind(target: std.Target, link_mode: std.builtin.LinkMode) bool { + return target.isGnuLibC() and link_mode == .static; +} + pub fn libCxxNeedsLibUnwind(target: std.Target) bool { return switch (target.os.tag) { .macos, -- cgit v1.2.3