1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// Copyright (c) 2023 Jonathan "Razordor" Alan Thomason
#![allow(clippy::let_unit_value)]
#![allow(unused_imports)]

#[cfg(target_env = "gnu")]
use libc::dl_iterate_phdr;

use crate::sealed::Sealed;
use crate::{weak, Symbol};
#[cfg(unix)]
use std::os::unix::ffi::OsStrExt;
use std::{ffi, io, mem, path::PathBuf, ptr};
use std::{
	marker::PhantomData,
	sync::{
		atomic::{AtomicU32, Ordering},
		Once,
	},
};

#[cfg(not(any(target_os = "linux", target_env = "gnu")))]
use std::sync;

mod c;

#[cfg(not(any(target_os = "linux", target_os = "macos", target_env = "gnu")))]
#[inline]
fn dylib_guard<'a>() -> sync::LockResult<sync::MutexGuard<'a, ()>> {
	static LOCK: sync::Mutex<()> = sync::Mutex::new(());
	LOCK.lock()
}

#[cfg(any(target_os = "linux", target_os = "macos", target_env = "gnu"))]
#[inline(always)]
fn dylib_guard() {}

unsafe fn c_dlerror() -> Option<ffi::CString> {
	let raw = libc::dlerror();
	if raw.is_null() {
		None
	} else {
		Some(ffi::CStr::from_ptr(raw).to_owned())
	}
}

pub(crate) unsafe fn dylib_open(path: &ffi::OsStr) -> io::Result<super::Handle> {
	let _lock = dylib_guard();
	let c_str = ffi::CString::new(path.as_bytes())?;
	let handle: *mut ffi::c_void = libc::dlopen(c_str.as_ptr(), libc::RTLD_NOW | libc::RTLD_LOCAL);
	if let Some(ret) = ptr::NonNull::new(handle) {
		Ok(ret)
	} else {
		let err = c_dlerror().unwrap();
		Err(io::Error::new(io::ErrorKind::Other, err.to_string_lossy()))
	}
}

pub(crate) unsafe fn dylib_this() -> io::Result<super::Handle> {
	let _lock = dylib_guard();
	let handle: *mut ffi::c_void = libc::dlopen(ptr::null(), libc::RTLD_NOW | libc::RTLD_LOCAL);
	if let Some(ret) = ptr::NonNull::new(handle) {
		Ok(ret)
	} else {
		let err = c_dlerror().unwrap();
		Err(io::Error::new(io::ErrorKind::Other, err.to_string_lossy()))
	}
}

pub(crate) unsafe fn dylib_close(lib_handle: super::Handle) -> io::Result<()> {
	let _lock = dylib_guard();
	if libc::dlclose(lib_handle.as_ptr()) != 0 {
		let err = c_dlerror().unwrap();
		Err(io::Error::new(io::ErrorKind::Other, err.to_string_lossy()))
	} else {
		Ok(())
	}
}

pub(crate) unsafe fn dylib_symbol<'a>(
	lib_handle: *mut ffi::c_void,
	name: &str,
) -> io::Result<Symbol<'a>> {
	let _lock = dylib_guard();
	let c_str = ffi::CString::new(name).unwrap();

	let _ = c_dlerror(); // clear existing errors
	let handle: *mut ffi::c_void = libc::dlsym(lib_handle, c_str.as_ptr());

	if let Some(err) = c_dlerror() {
		Err(io::Error::new(io::ErrorKind::Other, err.to_string_lossy()))
	} else {
		Ok(Symbol(handle, PhantomData))
	}
}

pub(crate) unsafe fn dylib_path(handle: super::Handle) -> io::Result<PathBuf> {
	match dylib_this() {
		Ok(this_handle)
			if (cfg!(target_os = "macos")
				&& (this_handle.as_ptr() as isize & (-4)) == (handle.as_ptr() as isize & (-4)))
				|| this_handle == handle =>
		{
			std::env::current_exe()
		}
		_ => {
			#[cfg(target_env = "gnu")]
			{
				if let Some(path) = get_link_map_path(handle) {
					Ok(path)
				} else {
					Err(io::Error::new(
						io::ErrorKind::NotFound,
						"Library path not found",
					))
				}
			}
			#[cfg(target_os = "macos")]
			{
				get_macos_image_path(handle)
			}
			#[cfg(not(any(target_env = "gnu", target_os = "macos")))]
			{
				// Handle other platforms or configurations
				Err(io::Error::new(io::ErrorKind::Other, "Unsupported platform"))
			}
		}
	}
}

#[cfg(target_env = "gnu")]
unsafe fn get_link_map_path(handle: super::Handle) -> Option<PathBuf> {
	use std::os::unix::ffi::OsStringExt;
	let mut map_ptr = ptr::null_mut::<c::link_map>();
	if libc::dlinfo(
		handle.as_ptr(),
		libc::RTLD_DI_LINKMAP,
		&mut map_ptr as *mut _ as *mut _,
	) == 0
	{
		let path = ffi::CStr::from_ptr((*map_ptr).l_name);
		let path = ffi::OsStr::from_bytes(path.to_bytes());
		if !path.is_empty() {
			Some(path.into())
		} else {
			None
		}
	} else {
		None
	}
}

#[cfg(target_os = "macos")]
fn get_image_count() -> &'static AtomicU32 {
	static IMAGE_COUNT: AtomicU32 = AtomicU32::new(0);
	static START: Once = Once::new();
	extern "C" fn increment_count(_: *const c::mach_header, _: isize) {
		IMAGE_COUNT.fetch_add(1, Ordering::SeqCst);
	}
	extern "C" fn decrement_count(_: *const c::mach_header, _: isize) {
		IMAGE_COUNT.fetch_sub(1, Ordering::SeqCst);
	}
	START.call_once(|| unsafe {
		c::_dyld_register_func_for_add_image(increment_count);
		c::_dyld_register_func_for_remove_image(decrement_count);
	});

	&IMAGE_COUNT
}

#[cfg(target_os = "macos")]
unsafe fn get_macos_image_path(handle: super::Handle) -> io::Result<PathBuf> {
	use std::os::unix::ffi::OsStringExt;

	let mut result = Err(io::Error::new(io::ErrorKind::NotFound, "Path not found"));
	let _ = get_image_count().fetch_update(Ordering::SeqCst, Ordering::SeqCst, |image_index| {
		for image_index in (0..image_index).rev() {
			let image_name = c::_dyld_get_image_name(image_index);
			let active_handle = libc::dlopen(
				image_name,
				libc::RTLD_NOW | libc::RTLD_LOCAL | libc::RTLD_NOLOAD,
			);
			if !active_handle.is_null() {
				let _ = libc::dlclose(active_handle);
			}
			if (handle.as_ptr() as isize & (-4)) == (active_handle as isize & (-4)) {
				let path = ffi::CStr::from_ptr(image_name);
				let path = ffi::OsStr::from_bytes(path.to_bytes());
				result = Ok(path.into());
				break;
			}
		}
		Some(image_index)
	});
	result
}

pub(crate) unsafe fn base_addr(symbol: *mut std::ffi::c_void) -> io::Result<*mut super::Header> {
	let mut info = mem::MaybeUninit::<libc::Dl_info>::zeroed();
	if libc::dladdr(symbol, info.as_mut_ptr()) != 0 {
		let info = info.assume_init();
		Ok(info.dli_fbase.cast())
	} else {
		// dlerror is not available for dladdr, so we're giving a generic error.
		Err(io::Error::new(
			io::ErrorKind::Other,
			"failed to get base address",
		))
	}
}

pub(crate) unsafe fn dylib_clone(handle: super::Handle) -> io::Result<super::Handle> {
	let this = dylib_this()?;
	if this == handle {
		Ok(this)
	} else {
		dylib_close(this)?;
		let path = dylib_path(handle)?;
		dylib_open(path.as_os_str())
	}
}

#[derive(Debug)]
pub struct DlInfo {
	pub dli_fname: ffi::CString,
	pub dli_fbase: *mut super::Header,
	pub dli_sname: ffi::CString,
	pub dli_saddr: *mut ffi::c_void,
}

pub trait SymExt: Sealed {
	fn info(&self) -> io::Result<DlInfo>;
}

impl SymExt for Symbol<'_> {
	#[doc(alias = "dladdr")]
	fn info(&self) -> io::Result<DlInfo> {
		let mut info = mem::MaybeUninit::<libc::Dl_info>::zeroed();
		unsafe {
			if libc::dladdr(self.0 as *const _, info.as_mut_ptr()) != 0 {
				let info = info.assume_init();
				Ok(DlInfo {
					dli_fname: ffi::CStr::from_ptr(info.dli_fname).to_owned(),
					dli_fbase: info.dli_fbase.cast(),
					dli_sname: ffi::CStr::from_ptr(info.dli_sname).to_owned(),
					dli_saddr: info.dli_saddr,
				})
			} else {
				// dlerror isn't available for dlinfo, so I can only provide a general error message here
				Err(io::Error::new(
					io::ErrorKind::Other,
					"Failed to retrieve symbol information",
				))
			}
		}
	}
}

#[cfg(target_env = "gnu")]
unsafe fn iter_phdr<F>(mut f: F) -> ffi::c_int
where
	F: FnMut(*mut libc::dl_phdr_info, libc::size_t) -> ffi::c_int,
{
	unsafe extern "C" fn callback<F>(
		info: *mut libc::dl_phdr_info,
		size: libc::size_t,
		data: *mut ffi::c_void,
	) -> ffi::c_int
	where
		F: FnMut(*mut libc::dl_phdr_info, libc::size_t) -> ffi::c_int,
	{
		let f = data as *mut F;
		(*f)(info, size)
	}
	libc::dl_iterate_phdr(Some(callback::<F>), &mut f as *mut _ as *mut _)
}

#[cfg(target_env = "gnu")]
pub(crate) unsafe fn load_objects() -> io::Result<Vec<weak::Weak>> {
	let mut data = Vec::new();
	let _ = iter_phdr(|info, _| {
		let path_name = if (*info).dlpi_name.is_null() {
			None
		} else if (*info).dlpi_name.read() == 0i8 {
			std::env::current_exe().ok()
		} else {
			let path = ffi::CStr::from_ptr((*info).dlpi_name);
			let path = ffi::OsStr::from_bytes(path.to_bytes());
			Some(PathBuf::from(path))
		};
		let weak_ptr = weak::Weak {
			base_addr: (*info).dlpi_addr as *mut super::Header,
			path_name,
		};
		data.push(weak_ptr);
		0
	});
	Ok(data)
}

#[cfg(target_os = "macos")]
pub(crate) unsafe fn load_objects() -> io::Result<Vec<weak::Weak>> {
	use std::os::unix::ffi::OsStringExt;

	let mut data = Vec::new();
	let _ = get_image_count().fetch_update(Ordering::SeqCst, Ordering::SeqCst, |image_index| {
		data.clear();
		for image_index in 0..image_index {
			let path = ffi::CStr::from_ptr(c::_dyld_get_image_name(image_index));
			let path = ffi::OsStr::from_bytes(path.to_bytes());
			let weak_ptr = weak::Weak {
				base_addr: c::_dyld_get_image_header(image_index) as *const super::Header,
				path_name: Some(PathBuf::from(path)),
			};
			data.push(weak_ptr);
		}
		Some(image_index)
	});
	Ok(data)
}

pub(crate) unsafe fn dylib_upgrade(addr: *const super::Header) -> Option<super::Handle> {
	let mut info = mem::MaybeUninit::zeroed();
	if libc::dladdr(addr.cast(), info.as_mut_ptr()) != 0 {
		let info = info.assume_init();
		let handle = libc::dlopen(info.dli_fname, libc::RTLD_NOW | libc::RTLD_LOCAL);
		super::Handle::new(handle)
	} else {
		None
	}
}

// returns null if handle is invalid
#[cfg(target_env = "gnu")]
pub(crate) unsafe fn get_addr(handle: super::Handle) -> *const super::Header {
	use std::os::unix::ffi::OsStringExt;
	let mut map_ptr = ptr::null_mut::<c::link_map>();
	if libc::dlinfo(
		handle.as_ptr(),
		libc::RTLD_DI_LINKMAP,
		&mut map_ptr as *mut _ as *mut _,
	) == 0
	{
		(*map_ptr).l_addr as *const super::Header
	} else {
		ptr::null()
	}
}

// returns null if handle is invalid
#[cfg(target_os = "macos")]
pub(crate) unsafe fn get_addr(handle: super::Handle) -> *const super::Header {
	use std::os::unix::ffi::OsStringExt;

	let mut result = ptr::null();
	let _ = get_image_count().fetch_update(Ordering::SeqCst, Ordering::SeqCst, |image_index| {
		for image_index in (0..image_index).rev() {
			let image_name = c::_dyld_get_image_name(image_index);
			let active_handle = libc::dlopen(
				image_name,
				libc::RTLD_NOW | libc::RTLD_LOCAL | libc::RTLD_NOLOAD,
			);
			if !active_handle.is_null() {
				let _ = libc::dlclose(active_handle);
			}
			if (handle.as_ptr() as isize & (-4)) == (active_handle as isize & (-4)) {
				result = c::_dyld_get_image_header(image_index) as *const super::Header;
				break;
			}
		}
		Some(image_index)
	});
	result
}