Skip to content

Commit d2f92e1

Browse files
committed
compiler: Link libunwind when linking glibc statically.
glibc's libc.a depends on the functions provided by libunwind.
1 parent f6476e9 commit d2f92e1

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

src/Compilation/Config.zig

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ pub const ResolveError = error{
129129
LldCannotIncrementallyLink,
130130
LtoRequiresLld,
131131
SanitizeThreadRequiresLibCpp,
132+
LibCRequiresLibUnwind,
132133
LibCppRequiresLibUnwind,
133134
OsRequiresLibC,
134135
LibCppRequiresLibC,
@@ -312,7 +313,7 @@ pub fn resolve(options: Options) ResolveError!Config {
312313
break :b false;
313314
};
314315

315-
const link_libunwind = b: {
316+
var link_libunwind = b: {
316317
if (link_libcpp and target_util.libCxxNeedsLibUnwind(target)) {
317318
if (options.link_libunwind == false) return error.LibCppRequiresLibUnwind;
318319
break :b true;
@@ -379,6 +380,13 @@ pub fn resolve(options: Options) ResolveError!Config {
379380
break :b .static;
380381
};
381382

383+
// This is done here to avoid excessive duplicated logic due to the complex dependencies between these options.
384+
if (options.output_mode == .Exe and link_libc and target_util.libCNeedsLibUnwind(target, link_mode)) {
385+
if (options.link_libunwind == false) return error.LibCRequiresLibUnwind;
386+
387+
link_libunwind = true;
388+
}
389+
382390
const import_memory = options.import_memory orelse (options.output_mode == .Obj);
383391
const export_memory = b: {
384392
if (link_mode == .dynamic) {

src/main.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4206,6 +4206,7 @@ fn createModule(
42064206
error.LldCannotIncrementallyLink => fatal("self-hosted backends do not support linking with LLD", .{}),
42074207
error.LtoRequiresLld => fatal("LTO requires using LLD", .{}),
42084208
error.SanitizeThreadRequiresLibCpp => fatal("thread sanitization is (for now) implemented in C++, so it requires linking libc++", .{}),
4209+
error.LibCRequiresLibUnwind => fatal("libc of the specified target requires linking libunwind", .{}),
42094210
error.LibCppRequiresLibUnwind => fatal("libc++ requires linking libunwind", .{}),
42104211
error.OsRequiresLibC => fatal("the target OS requires using libc as the stable syscall interface", .{}),
42114212
error.LibCppRequiresLibC => fatal("libc++ requires linking libc", .{}),

src/target.zig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ pub fn osRequiresLibC(target: std.Target) bool {
2323
return target.os.requiresLibC();
2424
}
2525

26+
pub fn libCNeedsLibUnwind(target: std.Target, link_mode: std.builtin.LinkMode) bool {
27+
return target.isGnuLibC() and link_mode == .static;
28+
}
29+
2630
pub fn libCxxNeedsLibUnwind(target: std.Target) bool {
2731
return switch (target.os.tag) {
2832
.macos,

0 commit comments

Comments
 (0)