authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-05-13 15:59:24-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-05-13 22:42:57-04:00
log826179bff40fdbd8c3b11138897fcfbb3367def8
tree23fcdea829179045785ac4303a3e694cc7e12170
parent76a259799d5bac3effabd1df44c0dec9e4fa16d4

stage2: -lunwind is handled specially

* `-lc++` now implies `-lc`. * `-lunwind` is now pulled out into a separate `link_libunwind` flag in the frontend driver code. This allows a project to request zig to provide libunwind even if the default situation that causes it to be implicitly added, is not active. * build.zig: ask for -lunwind when building the self-hosted compiler on Linux. Otherwise we get linker errors with unresolved symbols to libunwind.

5 files changed, 24 insertions(+), 12 deletions(-)

build.zig+1-2
......@@ -397,8 +397,7 @@ fn addCmakeCfgOptionsToExe(
397397 },
398398 else => |e| return e,
399399 };
400
401 exe.linkSystemLibrary("pthread");
400 exe.linkSystemLibrary("unwind");
402401 } else if (exe.target.isFreeBSD()) {
403402 try addCxxKnownPath(b, cfg, exe, "libc++.a", null, need_cpp_includes);
404403 exe.linkSystemLibrary("pthread");
src/Compilation.zig+10-7
......@@ -544,6 +544,7 @@ pub const InitOptions = struct {
544544 system_libs: []const []const u8 = &[0][]const u8{},
545545 link_libc: bool = false,
546546 link_libcpp: bool = false,
547 link_libunwind: bool = false,
547548 want_pic: ?bool = null,
548549 /// This means that if the output mode is an executable it will be a
549550 /// Position Independent Executable. If the output mode is not an
......@@ -791,8 +792,13 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
791792 };
792793
793794 const tsan = options.want_tsan orelse false;
795 // TSAN is implemented in C++ so it requires linking libc++.
796 const link_libcpp = options.link_libcpp or tsan;
797 const link_libc = link_libcpp or options.link_libc or
798 target_util.osRequiresLibC(options.target);
794799
795 const link_libc = options.link_libc or target_util.osRequiresLibC(options.target) or tsan;
800 const link_libunwind = options.link_libunwind or
801 (link_libcpp and target_util.libcNeedsLibUnwind(options.target));
796802
797803 const must_dynamic_link = dl: {
798804 if (target_util.cannotDynamicLink(options.target))
......@@ -878,9 +884,6 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
878884 break :pic explicit;
879885 } else pie or must_pic;
880886
881 // TSAN is implemented in C++ so it requires linking libc++.
882 const link_libcpp = options.link_libcpp or tsan;
883
884887 // Make a decision on whether to use Clang for translate-c and compiling C files.
885888 const use_clang = if (options.use_clang) |explicit| explicit else blk: {
886889 if (build_options.have_llvm) {
......@@ -973,6 +976,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
973976 cache.hash.add(strip);
974977 cache.hash.add(link_libc);
975978 cache.hash.add(link_libcpp);
979 cache.hash.add(link_libunwind);
976980 cache.hash.add(options.output_mode);
977981 cache.hash.add(options.machine_code_model);
978982 cache.hash.addOptionalEmitLoc(options.emit_bin);
......@@ -1157,6 +1161,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
11571161 .system_linker_hack = darwin_options.system_linker_hack,
11581162 .link_libc = link_libc,
11591163 .link_libcpp = link_libcpp,
1164 .link_libunwind = link_libunwind,
11601165 .objects = options.link_objects,
11611166 .frameworks = options.frameworks,
11621167 .framework_dirs = options.framework_dirs,
......@@ -3022,9 +3027,7 @@ fn wantBuildLibUnwindFromSource(comp: *Compilation) bool {
30223027 .Lib => comp.bin_file.options.link_mode == .Dynamic,
30233028 .Exe => true,
30243029 };
3025 return comp.bin_file.options.link_libc and is_exe_or_dyn_lib and
3026 comp.bin_file.options.link_libcpp and
3027 target_util.libcNeedsLibUnwind(comp.getTarget());
3030 return is_exe_or_dyn_lib and comp.bin_file.options.link_libunwind;
30283031}
30293032
30303033fn updateBuiltinZigFile(comp: *Compilation, mod: *Module) Allocator.Error!void {
src/link.zig+1
......@@ -63,6 +63,7 @@ pub const Options = struct {
6363 system_linker_hack: bool,
6464 link_libc: bool,
6565 link_libcpp: bool,
66 link_libunwind: bool,
6667 function_sections: bool,
6768 eh_frame_hdr: bool,
6869 emit_relocs: bool,
src/link/Elf.zig+5-3
......@@ -1643,9 +1643,11 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
16431643 if (self.base.options.link_libcpp) {
16441644 try argv.append(comp.libcxxabi_static_lib.?.full_object_path);
16451645 try argv.append(comp.libcxx_static_lib.?.full_object_path);
1646 if (target_util.libcNeedsLibUnwind(target)) {
1647 try argv.append(comp.libunwind_static_lib.?.full_object_path);
1648 }
1646 }
1647
1648 // libunwind dep
1649 if (self.base.options.link_libunwind) {
1650 try argv.append(comp.libunwind_static_lib.?.full_object_path);
16491651 }
16501652
16511653 // libc dep
src/main.zig+7
......@@ -520,6 +520,7 @@ fn buildOutputType(
520520 var ensure_libcpp_on_non_freestanding = false;
521521 var link_libc = false;
522522 var link_libcpp = false;
523 var link_libunwind = false;
523524 var want_native_include_dirs = false;
524525 var enable_cache: ?bool = null;
525526 var want_pic: ?bool = null;
......@@ -1532,6 +1533,11 @@ fn buildOutputType(
15321533 _ = system_libs.orderedRemove(i);
15331534 continue;
15341535 }
1536 if (mem.eql(u8, lib_name, "unwind")) {
1537 link_libunwind = true;
1538 _ = system_libs.orderedRemove(i);
1539 continue;
1540 }
15351541 if (std.fs.path.isAbsolute(lib_name)) {
15361542 fatal("cannot use absolute path as a system library: {s}", .{lib_name});
15371543 }
......@@ -1847,6 +1853,7 @@ fn buildOutputType(
18471853 .system_libs = system_libs.items,
18481854 .link_libc = link_libc,
18491855 .link_libcpp = link_libcpp,
1856 .link_libunwind = link_libunwind,
18501857 .want_pic = want_pic,
18511858 .want_pie = want_pie,
18521859 .want_lto = want_lto,