| 1 | From d35c341322f6e84607350058007bd5be3d1d294a 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 | 7 +++++++
|
| 11 | src/link/Lld.zig | 7 +++++++
|
| 12 | src/main.zig | 34 +++++++++++++++++++++++++++++++++-
|
| 13 | 4 files changed, 49 insertions(+), 1 deletion(-)
|
| 14 |
|
| 15 | diff --git a/lib/std/Build/Step/Compile.zig b/lib/std/Build/Step/Compile.zig
|
| 16 | index 79d3694c02..7f6bddbea6 100644
|
| 17 | |
| 18 | |
| 19 | @@ -794,6 +794,8 @@ fn runPkgConfig(compile: *Compile, lib_name: []const u8) !PkgConfigResult {
|
| 20 | try zig_cflags.append(arg);
|
| 21 | } else if (mem.startsWith(u8, arg, wl_rpath_prefix)) {
|
| 22 | try zig_cflags.appendSlice(&[_][]const u8{ "-rpath", arg[wl_rpath_prefix.len..] });
|
| 23 | + } else if (mem.startsWith(u8, arg, "-Wl,-rpath=")) {
|
| 24 | + try zig_libs.appendSlice(&[_][]const u8{ "-L", arg["-Wl,-rpath=".len..] });
|
| 25 | } else if (b.debug_pkg_config) {
|
| 26 | return compile.step.fail("unknown pkg-config flag '{s}'", .{arg});
|
| 27 | }
|
| 28 | diff --git a/src/link/Elf.zig b/src/link/Elf.zig
|
| 29 | index 99d0ad71b0..8017f4c088 100644
|
| 30 | |
| 31 | |
| 32 | @@ -988,6 +988,13 @@ fn dumpArgvInit(self: *Elf, arena: Allocator) !void {
|
| 33 | try argv.appendSlice(gpa, &.{ "-rpath", rpath });
|
| 34 | }
|
| 35 |
|
| 36 | + if (std.zig.system.NativePaths.isGuix(arena) and comp.config.link_libc and comp.config.link_mode == .dynamic) {
|
| 37 | + if (self.base.comp.libc_installation) |libc_installation| {
|
| 38 | + try argv.append(gpa, "-rpath");
|
| 39 | + try argv.append(gpa, libc_installation.crt_dir.?);
|
| 40 | + }
|
| 41 | + }
|
| 42 | +
|
| 43 | try argv.appendSlice(gpa, &.{
|
| 44 | "-z",
|
| 45 | try std.fmt.allocPrint(arena, "stack-size={d}", .{self.base.stack_size}),
|
| 46 | diff --git a/src/link/Lld.zig b/src/link/Lld.zig
|
| 47 | index 48872f7077..814f661692 100644
|
| 48 | |
| 49 | |
| 50 | @@ -1071,6 +1071,13 @@ fn elfLink(lld: *Lld, arena: Allocator) !void {
|
| 51 | }
|
| 52 | }
|
| 53 |
|
| 54 | + if (std.zig.system.NativePaths.isGuix(arena) and comp.config.link_libc and link_mode == .dynamic) {
|
| 55 | + if (comp.libc_installation) |libc_installation| {
|
| 56 | + try argv.append("-rpath");
|
| 57 | + try argv.append(libc_installation.crt_dir.?);
|
| 58 | + }
|
| 59 | + }
|
| 60 | +
|
| 61 | if (have_dynamic_linker and
|
| 62 | (comp.config.link_libc or comp.root_mod.resolved_target.is_explicit_dynamic_linker))
|
| 63 | {
|
| 64 | diff --git a/src/main.zig b/src/main.zig
|
| 65 | index 02b1b8f84b..ddc4ae8014 100644
|
| 66 | |
| 67 | |
| 68 | @@ -3910,7 +3910,7 @@ fn createModule(
|
| 69 | create_module.want_native_include_dirs = true;
|
| 70 | }
|
| 71 |
|
| 72 | - if (create_module.each_lib_rpath orelse resolved_target.is_native_os) {
|
| 73 | + if (create_module.each_lib_rpath orelse false) {
|
| 74 | try create_module.rpath_list.ensureUnusedCapacity(arena, create_module.lib_directories.items.len);
|
| 75 | for (create_module.lib_directories.items) |lib_directory| {
|
| 76 | create_module.rpath_list.appendAssumeCapacity(lib_directory.path.?);
|
| 77 | @@ -3982,6 +3982,28 @@ fn createModule(
|
| 78 | else => {},
|
| 79 | };
|
| 80 |
|
| 81 | + if (std.zig.system.NativePaths.isGuix(arena)) {
|
| 82 | + for (create_module.link_inputs.items) |link_input| {
|
| 83 | + if (link_input.path()) |lib| {
|
| 84 | + const lib_name = lib.sub_path;
|
| 85 | + if (Compilation.classifyFileExt(lib_name) == .shared_library) {
|
| 86 | + if (fs.path.isAbsolute(lib_name)) {
|
| 87 | + const lib_dir_path = fs.path.dirname(lib_name).?;
|
| 88 | + try create_module.rpath_list.append(arena, lib_dir_path);
|
| 89 | + continue;
|
| 90 | + }
|
| 91 | + for (create_module.lib_directories.items) |lib_dir| {
|
| 92 | + const lib_dir_path = lib_dir.path.?;
|
| 93 | + if (try libPathExists(arena, lib_dir_path, lib_name)) {
|
| 94 | + try create_module.rpath_list.append(arena, lib_dir_path);
|
| 95 | + break;
|
| 96 | + }
|
| 97 | + }
|
| 98 | + }
|
| 99 | + }
|
| 100 | + }
|
| 101 | + }
|
| 102 | +
|
| 103 | create_module.resolved_options = Compilation.Config.resolve(create_module.opts) catch |err| switch (err) {
|
| 104 | error.WasiExecModelRequiresWasi => fatal("only WASI OS targets support execution model", .{}),
|
| 105 | error.SharedMemoryIsWasmOnly => fatal("only WebAssembly CPU targets support shared memory", .{}),
|
| 106 | @@ -7633,3 +7655,13 @@ fn addLibDirectoryWarn2(
|
| 107 | .path = path,
|
| 108 | });
|
| 109 | }
|
| 110 | +
|
| 111 | +fn libPathExists(arena: Allocator, lib_dir_path: []const u8, lib_name: []const u8) !bool {
|
| 112 | + const lib_path = try std.fmt.allocPrint(arena, "{s}{s}{s}", .{
|
| 113 | + lib_dir_path,
|
| 114 | + fs.path.sep_str,
|
| 115 | + lib_name,
|
| 116 | + });
|
| 117 | + fs.cwd().access(lib_path, .{}) catch return false;
|
| 118 | + return true;
|
| 119 | +}
|
| 120 | --
|
| 121 | 2.50.1
|
| 122 |
|