From e517d5a51ec37e6f89fdb142c7d9674abb12cc72 Mon Sep 17 00:00:00 2001 From: Jan200101 Date: Thu, 26 Oct 2023 21:39:40 +0200 Subject: [PATCH 1/2] std.zig.system.NativePaths: add NixOS lib dirs to the rpath --- lib/std/zig/system/NativePaths.zig | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/std/zig/system/NativePaths.zig b/lib/std/zig/system/NativePaths.zig index 715c172c39bc..5fd4d56f303c 100644 --- a/lib/std/zig/system/NativePaths.zig +++ b/lib/std/zig/system/NativePaths.zig @@ -59,6 +59,7 @@ pub fn detect(arena: Allocator, native_target: std.Target) !NativePaths { } else if (word.len > 2 and word[0] == '-' and word[1] == 'L') { const lib_path = word[2..]; try self.addLibDir(lib_path); + try self.addRPath(lib_path); } else { try self.addWarningFmt("Unrecognized C flag from NIX_LDFLAGS: {s}", .{word}); break; From 8707555c0b1fd071fc87be8b74b06ad7bd71098f Mon Sep 17 00:00:00 2001 From: Jan200101 Date: Tue, 7 Nov 2023 18:58:56 +0100 Subject: [PATCH 2/2] compiler: move each_lib_rpath to frontend Co-authored-by: Andrew Kelley --- src/Compilation.zig | 6 ----- src/link.zig | 1 - src/link/Elf.zig | 63 --------------------------------------------- src/main.zig | 12 ++++++--- 4 files changed, 8 insertions(+), 74 deletions(-) diff --git a/src/Compilation.zig b/src/Compilation.zig index fdc7d08e984f..8b6c7abbadae 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -1049,7 +1049,6 @@ pub const CreateOptions = struct { linker_print_icf_sections: bool = false, linker_print_map: bool = false, llvm_opt_bisect_limit: i32 = -1, - each_lib_rpath: ?bool = null, build_id: ?std.zig.BuildId = null, disable_c_depfile: bool = false, linker_z_nodelete: bool = false, @@ -1341,9 +1340,6 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil const error_limit = options.error_limit orelse (std.math.maxInt(u16) - 1); - const each_lib_rpath = options.each_lib_rpath orelse - options.root_mod.resolved_target.is_native_os; - // We put everything into the cache hash that *cannot be modified // during an incremental update*. For example, one cannot change the // target between updates, but one can change source files, so the @@ -1578,7 +1574,6 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil .emit_relocs = options.link_emit_relocs, .soname = options.soname, .compatibility_version = options.compatibility_version, - .each_lib_rpath = each_lib_rpath, .build_id = build_id, .disable_lld_caching = options.disable_lld_caching or options.cache_mode == .whole, .subsystem = options.subsystem, @@ -2552,7 +2547,6 @@ fn addNonIncrementalStuffToCacheManifest( man.hash.addListOfBytes(opts.lib_dirs); man.hash.addListOfBytes(opts.rpath_list); man.hash.addListOfBytes(opts.symbol_wrap_set.keys()); - man.hash.add(opts.each_lib_rpath); if (comp.config.link_libc) { man.hash.add(comp.libc_installation != null); const target = comp.root_mod.resolved_target.result; diff --git a/src/link.zig b/src/link.zig index e4c46a8af918..ec5138fcdeb4 100644 --- a/src/link.zig +++ b/src/link.zig @@ -104,7 +104,6 @@ pub const File = struct { max_memory: ?u64, export_symbol_names: []const []const u8, global_base: ?u64, - each_lib_rpath: bool, build_id: std.zig.BuildId, disable_lld_caching: bool, hash_style: Elf.HashStyle, diff --git a/src/link/Elf.zig b/src/link/Elf.zig index ef06c8e1dd11..0fba1d026961 100644 --- a/src/link/Elf.zig +++ b/src/link/Elf.zig @@ -16,7 +16,6 @@ lib_dirs: []const []const u8, hash_style: HashStyle, compress_debug_sections: CompressDebugSections, symbol_wrap_set: std.StringArrayHashMapUnmanaged(void), -each_lib_rpath: bool, sort_section: ?SortSection, soname: ?[]const u8, bind_global_refs_locally: bool, @@ -320,7 +319,6 @@ pub fn createEmpty( .hash_style = options.hash_style, .compress_debug_sections = options.compress_debug_sections, .symbol_wrap_set = options.symbol_wrap_set, - .each_lib_rpath = options.each_lib_rpath, .sort_section = options.sort_section, .soname = options.soname, .bind_global_refs_locally = options.bind_global_refs_locally, @@ -1108,25 +1106,6 @@ pub fn flushModule(self: *Elf, arena: Allocator, prog_node: *std.Progress.Node) _ = try rpath_table.put(rpath, {}); } - if (self.each_lib_rpath) { - var test_path = std.ArrayList(u8).init(gpa); - defer test_path.deinit(); - for (self.lib_dirs) |lib_dir_path| { - for (comp.system_libs.keys()) |link_lib| { - if (!(try self.accessLibPath(&test_path, null, lib_dir_path, link_lib, .Dynamic))) - continue; - _ = try rpath_table.put(lib_dir_path, {}); - } - } - for (comp.objects) |obj| { - if (Compilation.classifyFileExt(obj.path) == .shared_library) { - const lib_dir_path = std.fs.path.dirname(obj.path) orelse continue; - if (obj.loption) continue; - _ = try rpath_table.put(lib_dir_path, {}); - } - } - } - // TSAN if (comp.config.any_sanitize_thread) { try positionals.append(.{ .path = comp.tsan_static_lib.?.full_object_path }); @@ -1693,22 +1672,6 @@ fn dumpArgv(self: *Elf, comp: *Compilation) !void { try argv.append(rpath); } - if (self.each_lib_rpath) { - for (self.lib_dirs) |lib_dir_path| { - try argv.append("-rpath"); - try argv.append(lib_dir_path); - } - for (comp.objects) |obj| { - if (Compilation.classifyFileExt(obj.path) == .shared_library) { - const lib_dir_path = std.fs.path.dirname(obj.path) orelse continue; - if (obj.loption) continue; - - try argv.append("-rpath"); - try argv.append(lib_dir_path); - } - } - } - try argv.appendSlice(&.{ "-z", try std.fmt.allocPrint(arena, "stack-size={d}", .{self.base.stack_size}), @@ -2439,7 +2402,6 @@ fn linkWithLLD(self: *Elf, arena: Allocator, prog_node: *std.Progress.Node) !voi man.hash.add(comp.config.rdynamic); man.hash.addListOfBytes(self.lib_dirs); man.hash.addListOfBytes(self.base.rpath_list); - man.hash.add(self.each_lib_rpath); if (output_mode == .Exe) { man.hash.add(self.base.stack_size); man.hash.add(self.base.build_id); @@ -2739,31 +2701,6 @@ fn linkWithLLD(self: *Elf, arena: Allocator, prog_node: *std.Progress.Node) !voi try argv.appendSlice(&.{ "-wrap", symbol_name }); } - if (self.each_lib_rpath) { - var test_path = std.ArrayList(u8).init(arena); - for (self.lib_dirs) |lib_dir_path| { - for (comp.system_libs.keys()) |link_lib| { - if (!(try self.accessLibPath(&test_path, null, lib_dir_path, link_lib, .Dynamic))) - continue; - if ((try rpath_table.fetchPut(lib_dir_path, {})) == null) { - try argv.append("-rpath"); - try argv.append(lib_dir_path); - } - } - } - for (comp.objects) |obj| { - if (Compilation.classifyFileExt(obj.path) == .shared_library) { - const lib_dir_path = std.fs.path.dirname(obj.path) orelse continue; - if (obj.loption) continue; - - if ((try rpath_table.fetchPut(lib_dir_path, {})) == null) { - try argv.append("-rpath"); - try argv.append(lib_dir_path); - } - } - } - } - for (self.lib_dirs) |lib_dir| { try argv.append("-L"); try argv.append(lib_dir); diff --git a/src/main.zig b/src/main.zig index 5dbb4a0cf71a..77e380aee8ec 100644 --- a/src/main.zig +++ b/src/main.zig @@ -866,7 +866,6 @@ fn buildOutputType( var image_base: ?u64 = null; var link_eh_frame_hdr = false; var link_emit_relocs = false; - var each_lib_rpath: ?bool = null; var build_id: ?std.zig.BuildId = null; var runtime_args_start: ?usize = null; var test_filter: ?[]const u8 = null; @@ -964,6 +963,7 @@ fn buildOutputType( .frameworks = .{}, .framework_dirs = .{}, .rpath_list = .{}, + .each_lib_rpath = null, .libc_paths_file = try EnvVar.ZIG_LIBC.get(arena), .link_objects = .{}, .native_system_include_paths = &.{}, @@ -1334,9 +1334,9 @@ fn buildOutputType( } else if (mem.eql(u8, arg, "-fno-compiler-rt")) { want_compiler_rt = false; } else if (mem.eql(u8, arg, "-feach-lib-rpath")) { - each_lib_rpath = true; + create_module.each_lib_rpath = true; } else if (mem.eql(u8, arg, "-fno-each-lib-rpath")) { - each_lib_rpath = false; + create_module.each_lib_rpath = false; } else if (mem.eql(u8, arg, "--test-cmd-bin")) { try test_exec_args.append(null); } else if (mem.eql(u8, arg, "--test-evented-io")) { @@ -3236,7 +3236,6 @@ fn buildOutputType( .verbose_llvm_cpu_features = verbose_llvm_cpu_features, .time_report = time_report, .stack_report = stack_report, - .each_lib_rpath = each_lib_rpath, .build_id = build_id, .test_filter = test_filter, .test_name_prefix = test_name_prefix, @@ -3450,6 +3449,7 @@ const CreateModule = struct { native_system_include_paths: []const []const u8, framework_dirs: std.ArrayListUnmanaged([]const u8), rpath_list: std.ArrayListUnmanaged([]const u8), + each_lib_rpath: ?bool, libc_paths_file: ?[]const u8, link_objects: std.ArrayListUnmanaged(Compilation.LinkObject), }; @@ -3644,6 +3644,10 @@ fn createModule( create_module.want_native_include_dirs = true; } + if (create_module.each_lib_rpath orelse resolved_target.is_native_os) { + try create_module.rpath_list.appendSlice(arena, create_module.lib_dirs.items); + } + // Trigger native system library path detection if necessary. if (create_module.sysroot == null and resolved_target.is_native_os and resolved_target.is_native_abi and