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(...@@ -397,8 +397,7 @@ fn addCmakeCfgOptionsToExe(
397 },397 },
398 else => |e| return e,398 else => |e| return e,
399 };399 };
400400 exe.linkSystemLibrary("unwind");
401 exe.linkSystemLibrary("pthread");
402 } else if (exe.target.isFreeBSD()) {401 } else if (exe.target.isFreeBSD()) {
403 try addCxxKnownPath(b, cfg, exe, "libc++.a", null, need_cpp_includes);402 try addCxxKnownPath(b, cfg, exe, "libc++.a", null, need_cpp_includes);
404 exe.linkSystemLibrary("pthread");403 exe.linkSystemLibrary("pthread");
src/Compilation.zig+10-7
...@@ -544,6 +544,7 @@ pub const InitOptions = struct {...@@ -544,6 +544,7 @@ pub const InitOptions = struct {
544 system_libs: []const []const u8 = &[0][]const u8{},544 system_libs: []const []const u8 = &[0][]const u8{},
545 link_libc: bool = false,545 link_libc: bool = false,
546 link_libcpp: bool = false,546 link_libcpp: bool = false,
547 link_libunwind: bool = false,
547 want_pic: ?bool = null,548 want_pic: ?bool = null,
548 /// This means that if the output mode is an executable it will be a549 /// This means that if the output mode is an executable it will be a
549 /// Position Independent Executable. If the output mode is not an550 /// Position Independent Executable. If the output mode is not an
...@@ -791,8 +792,13 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {...@@ -791,8 +792,13 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
791 };792 };
792793
793 const tsan = options.want_tsan orelse false;794 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
797 const must_dynamic_link = dl: {803 const must_dynamic_link = dl: {
798 if (target_util.cannotDynamicLink(options.target))804 if (target_util.cannotDynamicLink(options.target))
...@@ -878,9 +884,6 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {...@@ -878,9 +884,6 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
878 break :pic explicit;884 break :pic explicit;
879 } else pie or must_pic;885 } 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
884 // Make a decision on whether to use Clang for translate-c and compiling C files.887 // Make a decision on whether to use Clang for translate-c and compiling C files.
885 const use_clang = if (options.use_clang) |explicit| explicit else blk: {888 const use_clang = if (options.use_clang) |explicit| explicit else blk: {
886 if (build_options.have_llvm) {889 if (build_options.have_llvm) {
...@@ -973,6 +976,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {...@@ -973,6 +976,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
973 cache.hash.add(strip);976 cache.hash.add(strip);
974 cache.hash.add(link_libc);977 cache.hash.add(link_libc);
975 cache.hash.add(link_libcpp);978 cache.hash.add(link_libcpp);
979 cache.hash.add(link_libunwind);
976 cache.hash.add(options.output_mode);980 cache.hash.add(options.output_mode);
977 cache.hash.add(options.machine_code_model);981 cache.hash.add(options.machine_code_model);
978 cache.hash.addOptionalEmitLoc(options.emit_bin);982 cache.hash.addOptionalEmitLoc(options.emit_bin);
...@@ -1157,6 +1161,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {...@@ -1157,6 +1161,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
1157 .system_linker_hack = darwin_options.system_linker_hack,1161 .system_linker_hack = darwin_options.system_linker_hack,
1158 .link_libc = link_libc,1162 .link_libc = link_libc,
1159 .link_libcpp = link_libcpp,1163 .link_libcpp = link_libcpp,
1164 .link_libunwind = link_libunwind,
1160 .objects = options.link_objects,1165 .objects = options.link_objects,
1161 .frameworks = options.frameworks,1166 .frameworks = options.frameworks,
1162 .framework_dirs = options.framework_dirs,1167 .framework_dirs = options.framework_dirs,
...@@ -3022,9 +3027,7 @@ fn wantBuildLibUnwindFromSource(comp: *Compilation) bool {...@@ -3022,9 +3027,7 @@ fn wantBuildLibUnwindFromSource(comp: *Compilation) bool {
3022 .Lib => comp.bin_file.options.link_mode == .Dynamic,3027 .Lib => comp.bin_file.options.link_mode == .Dynamic,
3023 .Exe => true,3028 .Exe => true,
3024 };3029 };
3025 return comp.bin_file.options.link_libc and is_exe_or_dyn_lib and3030 return is_exe_or_dyn_lib and comp.bin_file.options.link_libunwind;
3026 comp.bin_file.options.link_libcpp and
3027 target_util.libcNeedsLibUnwind(comp.getTarget());
3028}3031}
30293032
3030fn updateBuiltinZigFile(comp: *Compilation, mod: *Module) Allocator.Error!void {3033fn updateBuiltinZigFile(comp: *Compilation, mod: *Module) Allocator.Error!void {
src/link.zig+1
...@@ -63,6 +63,7 @@ pub const Options = struct {...@@ -63,6 +63,7 @@ pub const Options = struct {
63 system_linker_hack: bool,63 system_linker_hack: bool,
64 link_libc: bool,64 link_libc: bool,
65 link_libcpp: bool,65 link_libcpp: bool,
66 link_libunwind: bool,
66 function_sections: bool,67 function_sections: bool,
67 eh_frame_hdr: bool,68 eh_frame_hdr: bool,
68 emit_relocs: bool,69 emit_relocs: bool,
src/link/Elf.zig+5-3
...@@ -1643,9 +1643,11 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {...@@ -1643,9 +1643,11 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
1643 if (self.base.options.link_libcpp) {1643 if (self.base.options.link_libcpp) {
1644 try argv.append(comp.libcxxabi_static_lib.?.full_object_path);1644 try argv.append(comp.libcxxabi_static_lib.?.full_object_path);
1645 try argv.append(comp.libcxx_static_lib.?.full_object_path);1645 try argv.append(comp.libcxx_static_lib.?.full_object_path);
1646 if (target_util.libcNeedsLibUnwind(target)) {1646 }
1647 try argv.append(comp.libunwind_static_lib.?.full_object_path);1647
1648 }1648 // libunwind dep
1649 if (self.base.options.link_libunwind) {
1650 try argv.append(comp.libunwind_static_lib.?.full_object_path);
1649 }1651 }
16501652
1651 // libc dep1653 // libc dep
src/main.zig+7
...@@ -520,6 +520,7 @@ fn buildOutputType(...@@ -520,6 +520,7 @@ fn buildOutputType(
520 var ensure_libcpp_on_non_freestanding = false;520 var ensure_libcpp_on_non_freestanding = false;
521 var link_libc = false;521 var link_libc = false;
522 var link_libcpp = false;522 var link_libcpp = false;
523 var link_libunwind = false;
523 var want_native_include_dirs = false;524 var want_native_include_dirs = false;
524 var enable_cache: ?bool = null;525 var enable_cache: ?bool = null;
525 var want_pic: ?bool = null;526 var want_pic: ?bool = null;
...@@ -1532,6 +1533,11 @@ fn buildOutputType(...@@ -1532,6 +1533,11 @@ fn buildOutputType(
1532 _ = system_libs.orderedRemove(i);1533 _ = system_libs.orderedRemove(i);
1533 continue;1534 continue;
1534 }1535 }
1536 if (mem.eql(u8, lib_name, "unwind")) {
1537 link_libunwind = true;
1538 _ = system_libs.orderedRemove(i);
1539 continue;
1540 }
1535 if (std.fs.path.isAbsolute(lib_name)) {1541 if (std.fs.path.isAbsolute(lib_name)) {
1536 fatal("cannot use absolute path as a system library: {s}", .{lib_name});1542 fatal("cannot use absolute path as a system library: {s}", .{lib_name});
1537 }1543 }
...@@ -1847,6 +1853,7 @@ fn buildOutputType(...@@ -1847,6 +1853,7 @@ fn buildOutputType(
1847 .system_libs = system_libs.items,1853 .system_libs = system_libs.items,
1848 .link_libc = link_libc,1854 .link_libc = link_libc,
1849 .link_libcpp = link_libcpp,1855 .link_libcpp = link_libcpp,
1856 .link_libunwind = link_libunwind,
1850 .want_pic = want_pic,1857 .want_pic = want_pic,
1851 .want_pie = want_pie,1858 .want_pie = want_pie,
1852 .want_lto = want_lto,1859 .want_lto = want_lto,