1diff --git a/src/dist/component/package.rs b/src/dist/component/package.rs
2index dfccc661..85233f3b 100644
3--- a/src/dist/component/package.rs
4+++ b/src/dist/component/package.rs
5@@ -113,6 +113,7 @@ impl Package for DirectoryPackage {
6 } else {
7 builder.move_file(path.clone(), &src_path)?
8 }
9+ nix_patchelf_if_needed(&target.prefix().path().join(path.clone()))
10 }
11 "dir" => {
12 if self.copy {
13@@ -135,6 +136,176 @@ impl Package for DirectoryPackage {
14 }
15 }
16
17+fn nix_wrap_lld(dest_lld_path: &Path) -> Result<()> {
18+ use std::fs;
19+ use std::io::Write;
20+ use std::os::unix::fs::PermissionsExt;
21+
22+ let path = dest_lld_path.parent().unwrap();
23+ let mut unwrapped_name = path.file_name().unwrap().to_string_lossy().to_string();
24+ unwrapped_name.push_str("-unwrapped");
25+ let unwrapped_dir = path.with_file_name(unwrapped_name);
26+ fs::create_dir_all(&unwrapped_dir).context("failed to create unwrapped directory")?;
27+ let mut unwrapped_lld = unwrapped_dir;
28+ unwrapped_lld.push(dest_lld_path.file_name().unwrap());
29+ fs::rename(dest_lld_path, &unwrapped_lld).context("failed to move file")?;
30+ let mut ld_wrapper_path = std::env::current_exe()?
31+ .parent()
32+ .ok_or(anyhow!("failed to get parent directory"))?
33+ .with_file_name("nix-support");
34+ let mut file = std::fs::File::create(dest_lld_path)?;
35+ ld_wrapper_path.push("ld-wrapper.sh");
36+
37+ let wrapped_script = format!(
38+ "#!/usr/bin/env bash
39+set -eu -o pipefail +o posix
40+shopt -s nullglob
41+export PROG=\"{}\"
42+\"{}\" $@",
43+ unwrapped_lld.to_string_lossy().to_string(),
44+ ld_wrapper_path.to_string_lossy().to_string(),
45+ );
46+ file.write_all(wrapped_script.as_bytes())?;
47+ let mut permissions = file.metadata()?.permissions();
48+ permissions.set_mode(0o755);
49+ file.set_permissions(permissions)?;
50+ Ok(())
51+}
52+
53+fn nix_patchelf_if_needed(dest_path: &Path) {
54+ use std::fs::File;
55+ use std::os::unix::fs::FileExt;
56+ use tracing::{debug, warn};
57+
58+ struct ELFReader<'a> {
59+ file: &'a mut File,
60+ is_32bit: bool,
61+ is_little_end: bool,
62+ }
63+
64+ impl<'a> ELFReader<'a> {
65+ const MAGIC_NUMBER: &'static [u8] = &[0x7F, 0x45, 0x4c, 0x46];
66+ const ET_EXEC: u16 = 0x2;
67+ const ET_DYN: u16 = 0x3;
68+ const PT_INTERP: u32 = 0x3;
69+
70+ fn new(file: &'a mut File) -> Option<Self> {
71+ let mut magic_number = [0; 4];
72+ file.read_exact(&mut magic_number).ok()?;
73+ if Self::MAGIC_NUMBER != magic_number {
74+ return None;
75+ }
76+ let mut ei_class = [0; 1];
77+ file.read_exact_at(&mut ei_class, 0x4).ok()?;
78+ let is_32bit = ei_class[0] == 1;
79+ let mut ei_data = [0; 1];
80+ file.read_exact_at(&mut ei_data, 0x5).ok()?;
81+ let is_little_end = ei_data[0] == 1;
82+ Some(Self {
83+ file,
84+ is_32bit,
85+ is_little_end,
86+ })
87+ }
88+
89+ fn is_exec_or_dyn(&self) -> bool {
90+ let e_type = self.read_u16_at(0x10);
91+ e_type == Self::ET_EXEC || e_type == Self::ET_DYN
92+ }
93+
94+ fn e_phoff(&self) -> u64 {
95+ if self.is_32bit {
96+ self.read_u32_at(0x1C) as u64
97+ } else {
98+ self.read_u64_at(0x20)
99+ }
100+ }
101+
102+ fn e_phentsize(&self) -> u64 {
103+ let offset = if self.is_32bit { 0x2A } else { 0x36 };
104+ self.read_u16_at(offset) as u64
105+ }
106+
107+ fn e_phnum(&self) -> u64 {
108+ let offset = if self.is_32bit { 0x2C } else { 0x38 };
109+ self.read_u16_at(offset) as u64
110+ }
111+
112+ fn has_interp(&self) -> bool {
113+ let e_phoff = self.e_phoff();
114+ let e_phentsize = self.e_phentsize();
115+ let e_phnum = self.e_phnum();
116+ for i in 0..e_phnum {
117+ let p_type = self.read_u32_at(e_phoff + i * e_phentsize);
118+ if p_type == Self::PT_INTERP {
119+ return true;
120+ }
121+ }
122+ false
123+ }
124+
125+ fn read_u16_at(&self, offset: u64) -> u16 {
126+ let mut data = [0; 2];
127+ self.file.read_exact_at(&mut data, offset).unwrap();
128+ if self.is_little_end {
129+ u16::from_le_bytes(data)
130+ } else {
131+ u16::from_be_bytes(data)
132+ }
133+ }
134+
135+ fn read_u32_at(&self, offset: u64) -> u32 {
136+ let mut data = [0; 4];
137+ self.file.read_exact_at(&mut data, offset).unwrap();
138+ if self.is_little_end {
139+ u32::from_le_bytes(data)
140+ } else {
141+ u32::from_be_bytes(data)
142+ }
143+ }
144+
145+ fn read_u64_at(&self, offset: u64) -> u64 {
146+ let mut data = [0; 8];
147+ self.file.read_exact_at(&mut data, offset).unwrap();
148+ if self.is_little_end {
149+ u64::from_le_bytes(data)
150+ } else {
151+ u64::from_be_bytes(data)
152+ }
153+ }
154+ }
155+
156+ let Some(mut dest_file) = File::open(dest_path).ok() else {
157+ return;
158+ };
159+ let Some(elf) = ELFReader::new(&mut dest_file) else {
160+ return;
161+ };
162+ if !elf.is_exec_or_dyn() {
163+ return;
164+ }
165+ let mut patch_command = std::process::Command::new("@patchelf@/bin/patchelf");
166+ if elf.has_interp() {
167+ patch_command
168+ .arg("--set-interpreter")
169+ .arg("@dynamicLinker@");
170+ }
171+ if Some(std::ffi::OsStr::new("rust-lld")) == dest_path.file_name() || !elf.has_interp() {
172+ patch_command.arg("--add-rpath").arg("@libPath@");
173+ }
174+
175+ debug!("patching {dest_path:?} using patchelf");
176+ if let Err(err) = patch_command.arg(dest_path).output() {
177+ warn!("failed to execute patchelf: {err:?}");
178+ }
179+
180+ if Some(std::ffi::OsStr::new("ld.lld")) == dest_path.file_name() {
181+ if let Err(err) = nix_wrap_lld(dest_path) {
182+ warn!("failed to wrap `ld.lld`: {err:?}");
183+ }
184+ }
185+}
186+
187 #[derive(Debug)]
188 pub(crate) struct TarPackage<'a>(DirectoryPackage, temp::Dir<'a>);
189