Commit 88d1258e08

Andrew Kelley <andrew@ziglang.org>
2022-02-23 21:25:10
stage2: make -lgcc_s additionally link libunwind
Previously, Zig ignored -lgcc_s with a warning that this dependency is redundant because it is satisfied by compiler-rt. However, sfackler pointed out that it also provides exception handling functions. So if Zig sees -lgcc_s on the linker line, it needs to fulfill this dependency with libunwind. I also made link_libc inferred to be on if libunwind is linked since libunwind depends on libc.
1 parent acec06c
src/Compilation.zig
@@ -962,7 +962,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
         const tsan = options.want_tsan orelse false;
         // TSAN is implemented in C++ so it requires linking libc++.
         const link_libcpp = options.link_libcpp or tsan;
-        const link_libc = link_libcpp or options.link_libc or
+        const link_libc = link_libcpp or options.link_libc or options.link_libunwind or
             target_util.osRequiresLibC(options.target);
 
         const link_libunwind = options.link_libunwind or
src/main.zig
@@ -2048,19 +2048,24 @@ fn buildOutputType(
                 system_libs.orderedRemoveAt(i);
                 continue;
             }
-            if (mem.eql(u8, lib_name, "unwind")) {
-                link_libunwind = true;
-                system_libs.orderedRemoveAt(i);
-                continue;
-            }
-            if (target_util.is_compiler_rt_lib_name(target_info.target, lib_name)) {
-                std.log.warn("ignoring superfluous library '{s}': this dependency is fulfilled instead by compiler-rt which zig unconditionally provides", .{lib_name});
-                system_libs.orderedRemoveAt(i);
-                continue;
+            switch (target_util.classifyCompilerRtLibName(target_info.target, lib_name)) {
+                .none => {},
+                .only_libunwind, .both => {
+                    link_libunwind = true;
+                    system_libs.orderedRemoveAt(i);
+                    continue;
+                },
+                .only_compiler_rt => {
+                    std.log.warn("ignoring superfluous library '{s}': this dependency is fulfilled instead by compiler-rt which zig unconditionally provides", .{lib_name});
+                    system_libs.orderedRemoveAt(i);
+                    continue;
+                },
             }
+
             if (std.fs.path.isAbsolute(lib_name)) {
                 fatal("cannot use absolute path as a system library: {s}", .{lib_name});
             }
+
             if (target_info.target.os.tag == .wasi) {
                 if (wasi_libc.getEmulatedLibCRTFile(lib_name)) |crt_file| {
                     try wasi_emulated_libs.append(crt_file);
src/target.zig
@@ -427,14 +427,22 @@ pub fn is_libcpp_lib_name(target: std.Target, name: []const u8) bool {
         eqlIgnoreCase(ignore_case, name, "c++abi");
 }
 
-pub fn is_compiler_rt_lib_name(target: std.Target, name: []const u8) bool {
+pub const CompilerRtClassification = enum { none, only_compiler_rt, only_libunwind, both };
+
+pub fn classifyCompilerRtLibName(target: std.Target, name: []const u8) CompilerRtClassification {
     if (target.abi.isGnu() and std.mem.eql(u8, name, "gcc_s")) {
-        return true;
+        // libgcc_s includes exception handling functions, so if linking this library
+        // is requested, zig needs to instead link libunwind. Otherwise we end up with
+        // the linker unable to find `_Unwind_RaiseException` and other related symbols.
+        return .both;
     }
     if (std.mem.eql(u8, name, "compiler_rt")) {
-        return true;
+        return .only_compiler_rt;
     }
-    return false;
+    if (std.mem.eql(u8, name, "unwind")) {
+        return .only_libunwind;
+    }
+    return .none;
 }
 
 pub fn hasDebugInfo(target: std.Target) bool {