| 1 | From c531a68b2dc5d96a50c1783d5f266e8c5b9c456e Mon Sep 17 00:00:00 2001
|
| 2 | From: Hilton Chain <[email protected]>
|
| 3 | Date: Fri, 29 Nov 2024 14:13:46 +0800
|
| 4 | Subject: [PATCH] Fix RUNPATH issue.
|
| 5 |
|
| 6 | Add needed libraries and libc to RUNPATH when CROSS_LIBRARY_PATH or LIBRARY_PATH
|
| 7 | is set.
|
| 8 | ---
|
| 9 | lib/std/Build/Step/Compile.zig | 2 ++
|
| 10 | src/link/Elf.zig | 14 ++++++++++++++
|
| 11 | src/main.zig | 34 +++++++++++++++++++++++++++++++++-
|
| 12 | 3 files changed, 49 insertions(+), 1 deletion(-)
|
| 13 |
|
| 14 | diff --git a/lib/std/Build/Step/Compile.zig b/lib/std/Build/Step/Compile.zig
|
| 15 | index c401a840ba..44ced5823f 100644
|
| 16 | |
| 17 | |
| 18 | @@ -787,6 +787,8 @@ fn runPkgConfig(compile: *Compile, lib_name: []const u8) !PkgConfigResult {
|
| 19 | try zig_cflags.append(arg);
|
| 20 | } else if (mem.startsWith(u8, arg, wl_rpath_prefix)) {
|
| 21 | try zig_cflags.appendSlice(&[_][]const u8{ "-rpath", arg[wl_rpath_prefix.len..] });
|
| 22 | + } else if (mem.startsWith(u8, arg, "-Wl,-rpath=")) {
|
| 23 | + try zig_libs.appendSlice(&[_][]const u8{ "-L", arg["-Wl,-rpath=".len..] });
|
| 24 | } else if (b.debug_pkg_config) {
|
| 25 | return compile.step.fail("unknown pkg-config flag '{s}'", .{arg});
|
| 26 | }
|
| 27 | diff --git a/src/link/Elf.zig b/src/link/Elf.zig
|
| 28 | index 591786cfbc..253c7f6732 100644
|
| 29 | |
| 30 | |
| 31 | @@ -1054,6 +1054,13 @@ fn dumpArgvInit(self: *Elf, arena: Allocator) !void {
|
| 32 | try argv.appendSlice(gpa, &.{ "-rpath", rpath });
|
| 33 | }
|
| 34 |
|
| 35 | + if (std.zig.system.NativePaths.isGuix(arena) and comp.config.link_libc and comp.config.link_mode == .dynamic) {
|
| 36 | + if (self.base.comp.libc_installation) |libc_installation| {
|
| 37 | + try argv.append(gpa, "-rpath");
|
| 38 | + try argv.append(gpa, libc_installation.crt_dir.?);
|
| 39 | + }
|
| 40 | + }
|
| 41 | +
|
| 42 | try argv.appendSlice(gpa, &.{
|
| 43 | "-z",
|
| 44 | try std.fmt.allocPrint(arena, "stack-size={d}", .{self.base.stack_size}),
|
| 45 | @@ -1888,6 +1895,13 @@ fn linkWithLLD(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: s
|
| 46 | try argv.appendSlice(&.{ "-rpath", rpath });
|
| 47 | }
|
| 48 |
|
| 49 | + if (std.zig.system.NativePaths.isGuix(arena) and comp.config.link_libc and link_mode == .dynamic) {
|
| 50 | + if (self.base.comp.libc_installation) |libc_installation| {
|
| 51 | + try argv.append("-rpath");
|
| 52 | + try argv.append(libc_installation.crt_dir.?);
|
| 53 | + }
|
| 54 | + }
|
| 55 | +
|
| 56 | for (self.symbol_wrap_set.keys()) |symbol_name| {
|
| 57 | try argv.appendSlice(&.{ "-wrap", symbol_name });
|
| 58 | }
|
| 59 | diff --git a/src/main.zig b/src/main.zig
|
| 60 | index 141fd42aab..81cdf6a507 100644
|
| 61 | |
| 62 | |
| 63 | @@ -4022,7 +4022,7 @@ fn createModule(
|
| 64 | create_module.want_native_include_dirs = true;
|
| 65 | }
|
| 66 |
|
| 67 | - if (create_module.each_lib_rpath orelse resolved_target.is_native_os) {
|
| 68 | + if (create_module.each_lib_rpath orelse false) {
|
| 69 | try create_module.rpath_list.ensureUnusedCapacity(arena, create_module.lib_directories.items.len);
|
| 70 | for (create_module.lib_directories.items) |lib_directory| {
|
| 71 | create_module.rpath_list.appendAssumeCapacity(lib_directory.path.?);
|
| 72 | @@ -4096,6 +4096,28 @@ fn createModule(
|
| 73 | else => {},
|
| 74 | };
|
| 75 |
|
| 76 | + if (std.zig.system.NativePaths.isGuix(arena)) {
|
| 77 | + for (create_module.link_inputs.items) |link_input| {
|
| 78 | + if (link_input.path()) |lib| {
|
| 79 | + const lib_name = lib.sub_path;
|
| 80 | + if (Compilation.classifyFileExt(lib_name) == .shared_library) {
|
| 81 | + if (fs.path.isAbsolute(lib_name)) {
|
| 82 | + const lib_dir_path = fs.path.dirname(lib_name).?;
|
| 83 | + try create_module.rpath_list.append(arena, lib_dir_path);
|
| 84 | + continue;
|
| 85 | + }
|
| 86 | + for (create_module.lib_directories.items) |lib_dir| {
|
| 87 | + const lib_dir_path = lib_dir.path.?;
|
| 88 | + if (try libPathExists(arena, lib_dir_path, lib_name)) {
|
| 89 | + try create_module.rpath_list.append(arena, lib_dir_path);
|
| 90 | + break;
|
| 91 | + }
|
| 92 | + }
|
| 93 | + }
|
| 94 | + }
|
| 95 | + }
|
| 96 | + }
|
| 97 | +
|
| 98 | create_module.resolved_options = Compilation.Config.resolve(create_module.opts) catch |err| switch (err) {
|
| 99 | error.WasiExecModelRequiresWasi => fatal("only WASI OS targets support execution model", .{}),
|
| 100 | error.SharedMemoryIsWasmOnly => fatal("only WebAssembly CPU targets support shared memory", .{}),
|
| 101 | @@ -7748,3 +7770,13 @@ fn addLibDirectoryWarn2(
|
| 102 | .path = path,
|
| 103 | });
|
| 104 | }
|
| 105 | +
|
| 106 | +fn libPathExists(arena: Allocator, lib_dir_path: []const u8, lib_name: []const u8) !bool {
|
| 107 | + const lib_path = try std.fmt.allocPrint(arena, "{s}{s}{s}", .{
|
| 108 | + lib_dir_path,
|
| 109 | + fs.path.sep_str,
|
| 110 | + lib_name,
|
| 111 | + });
|
| 112 | + fs.cwd().access(lib_path, .{}) catch return false;
|
| 113 | + return true;
|
| 114 | +}
|
| 115 | --
|
| 116 | 2.49.0
|
| 117 |
|