Skip to content

Commit 1b0f8c4

Browse files
committed
Export constants for flags to be used with calls
This should make use of the target-specific `Library::open` calls significantly more palatable.
1 parent b82a379 commit 1b0f8c4

File tree

6 files changed

+403
-12
lines changed

6 files changed

+403
-12
lines changed

Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ features = [
2222
"libloaderapi",
2323
]
2424

25+
[target.'cfg(unix)'.dependencies.cfg-if]
26+
version = "0.1"
27+
28+
[dev-dependencies]
29+
libc = "0.2"
30+
static_assertions = "1.1"
31+
2532
[package.metadata.docs.rs]
2633
all-features = true
2734
rustdoc-args = ["--cfg", "docsrs"]

src/changelog.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
/// Release 0.6.3 (2020-08-22)
77
///
88
/// * Improve documentation, allowing to view all of the os-specific functionality from
9-
/// documentation generated for any target.
10-
/// * Add [`os::windows::Library::this`].
9+
/// documentation generated for any target;
10+
/// * Add [`os::windows::Library::this`];
11+
/// * Added constants to use with OS-specific `Library::open`.
1112
///
1213
/// [`os::windows::Library::this`]: crate::os::windows::Library::this
1314

src/os/unix/consts.rs

Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
use std::os::raw::c_int;
2+
3+
/// Perform lazy binding.
4+
///
5+
/// Relocations shall be performed at an implementation-defined time, ranging from the time
6+
/// of the [`Library::open`] call until the first reference to a given symbol occurs.
7+
/// Specifying `RTLD_LAZY` should improve performance on implementations supporting dynamic
8+
/// symbol binding since a process might not reference all of the symbols in an executable
9+
/// object file. And, for systems supporting dynamic symbol resolution for normal process
10+
/// execution, this behavior mimics the normal handling of process execution.
11+
///
12+
/// Conflicts with [`RTLD_NOW`].
13+
///
14+
/// [`Library::open`]: crate::os::unix::Library::open
15+
pub const RTLD_LAZY: c_int = posix::RTLD_LAZY;
16+
17+
/// Perform eager binding.
18+
///
19+
/// All necessary relocations shall be performed when the executable object file is first
20+
/// loaded. This may waste some processing if relocations are performed for symbols
21+
/// that are never referenced. This behavior may be useful for applications that need to
22+
/// know that all symbols referenced during execution will be available before
23+
/// [`Library::open`] returns.
24+
///
25+
/// Conflicts with [`RTLD_LAZY`].
26+
///
27+
/// [`Library::open`]: crate::os::unix::Library::open
28+
pub const RTLD_NOW: c_int = posix::RTLD_NOW;
29+
30+
/// Make loaded symbols available for resolution globally.
31+
///
32+
/// The executable object file's symbols shall be made available for relocation processing of any
33+
/// other executable object file. In addition, calls to [`Library::get`] on `Library` obtained from
34+
/// [`Library::this`] allows executable object files loaded with this mode to be searched.
35+
///
36+
/// [`Library::this`]: crate::os::unix::Library::this
37+
/// [`Library::get`]: crate::os::unix::Library::get
38+
pub const RTLD_GLOBAL: c_int = posix::RTLD_GLOBAL;
39+
40+
/// Load symbols into an isolated namespace.
41+
///
42+
/// The executable object file's symbols shall not be made available for relocation processing of
43+
/// any other executable object file. This mode of operation is most appropriate for e.g. plugins.
44+
pub const RTLD_LOCAL: c_int = posix::RTLD_LOCAL;
45+
46+
#[cfg(docsrs)]
47+
mod posix {
48+
use super::c_int;
49+
pub(super) const RTLD_LAZY: c_int = !0;
50+
pub(super) const RTLD_NOW: c_int = !0;
51+
pub(super) const RTLD_GLOBAL: c_int = !0;
52+
pub(super) const RTLD_LOCAL: c_int = !0;
53+
}
54+
55+
#[cfg(not(docsrs))]
56+
mod posix {
57+
extern crate cfg_if;
58+
use self::cfg_if::cfg_if;
59+
use super::c_int;
60+
cfg_if! {
61+
if #[cfg(target_os = "haiku")] {
62+
pub(super) const RTLD_LAZY: c_int = 0;
63+
} else if #[cfg(any(
64+
target_os = "linux",
65+
target_os = "android",
66+
target_os = "emscripten",
67+
68+
target_os = "macos",
69+
target_os = "ios",
70+
target_os = "freebsd",
71+
target_os = "dragonfly",
72+
target_os = "openbsd",
73+
target_os = "netbsd",
74+
75+
target_os = "solaris",
76+
target_os = "illumos",
77+
78+
target_env = "uclibc",
79+
target_env = "newlib",
80+
81+
target_os = "fuchsia",
82+
target_os = "redox",
83+
))] {
84+
pub(super) const RTLD_LAZY: c_int = 1;
85+
} else {
86+
compile_error!(
87+
"Target has no known `RTLD_LAZY` value. Please submit an issue or PR adding it."
88+
);
89+
}
90+
}
91+
92+
cfg_if! {
93+
if #[cfg(target_os = "haiku")] {
94+
pub(super) const RTLD_NOW: c_int = 1;
95+
} else if #[cfg(any(
96+
target_os = "linux",
97+
all(target_os = "android", target_pointer_width = "64"),
98+
target_os = "emscripten",
99+
100+
target_os = "macos",
101+
target_os = "ios",
102+
target_os = "freebsd",
103+
target_os = "dragonfly",
104+
target_os = "openbsd",
105+
target_os = "netbsd",
106+
107+
target_os = "solaris",
108+
target_os = "illumos",
109+
110+
target_env = "uclibc",
111+
target_env = "newlib",
112+
113+
target_os = "fuchsia",
114+
target_os = "redox",
115+
))] {
116+
pub(super) const RTLD_NOW: c_int = 2;
117+
} else if #[cfg(all(target_os = "android",target_pointer_width = "32"))] {
118+
pub(super) const RTLD_NOW: c_int = 0;
119+
} else {
120+
compile_error!(
121+
"Target has no known `RTLD_NOW` value. Please submit an issue or PR adding it."
122+
);
123+
}
124+
}
125+
126+
cfg_if! {
127+
if #[cfg(any(
128+
target_os = "haiku",
129+
all(target_os = "android",target_pointer_width = "32"),
130+
))] {
131+
pub(super) const RTLD_GLOBAL: c_int = 2;
132+
} else if #[cfg(any(
133+
target_env = "uclibc",
134+
all(target_os = "linux", target_arch = "mips"),
135+
all(target_os = "linux", target_arch = "mips64"),
136+
))] {
137+
pub(super) const RTLD_GLOBAL: c_int = 4;
138+
} else if #[cfg(any(
139+
target_os = "macos",
140+
target_os = "ios",
141+
))] {
142+
pub(super) const RTLD_GLOBAL: c_int = 8;
143+
} else if #[cfg(any(
144+
target_os = "linux",
145+
all(target_os = "android", target_pointer_width = "64"),
146+
target_os = "emscripten",
147+
148+
target_os = "freebsd",
149+
target_os = "dragonfly",
150+
target_os = "openbsd",
151+
target_os = "netbsd",
152+
153+
target_os = "solaris",
154+
target_os = "illumos",
155+
156+
target_env = "newlib",
157+
158+
target_os = "fuchsia",
159+
target_os = "redox",
160+
))] {
161+
pub(super) const RTLD_GLOBAL: c_int = 0x100;
162+
} else {
163+
compile_error!(
164+
"Target has no known `RTLD_GLOBAL` value. Please submit an issue or PR adding it."
165+
);
166+
}
167+
}
168+
169+
cfg_if! {
170+
if #[cfg(target_os = "netbsd")] {
171+
pub(super) const RTLD_LOCAL: c_int = 0x200;
172+
} else if #[cfg(any(
173+
target_os = "macos",
174+
target_os = "ios",
175+
))] {
176+
pub(super) const RTLD_LOCAL: c_int = 4;
177+
} else if #[cfg(any(
178+
target_os = "linux",
179+
target_os = "android",
180+
target_os = "emscripten",
181+
182+
target_os = "freebsd",
183+
target_os = "dragonfly",
184+
target_os = "openbsd",
185+
186+
target_os = "haiku",
187+
188+
target_os = "solaris",
189+
target_os = "illumos",
190+
191+
target_env = "uclibc",
192+
target_env = "newlib",
193+
194+
target_os = "fuchsia",
195+
target_os = "redox",
196+
))] {
197+
pub(super) const RTLD_LOCAL: c_int = 0;
198+
} else {
199+
compile_error!(
200+
"Target has no known `RTLD_LOCAL` value. Please submit an issue or PR adding it."
201+
);
202+
}
203+
}
204+
}
205+
206+
// Other constants that exist but are not bound because they are platform-specific (non-posix)
207+
// extensions. Some of these constants are only relevant to `dlsym` or `dlmopen` calls.
208+
//
209+
// RTLD_CONFGEN
210+
// RTLD_DEFAULT
211+
// RTLD_DI_CONFIGADDR
212+
// RTLD_DI_LINKMAP
213+
// RTLD_DI_LMID
214+
// RTLD_DI_ORIGIN
215+
// RTLD_DI_PROFILENAME
216+
// RTLD_DI_PROFILEOUT
217+
// RTLD_DI_SERINFO
218+
// RTLD_DI_SERINFOSIZE
219+
// RTLD_DI_TLS_DATA
220+
// RTLD_DI_TLS_MODID
221+
// RTLD_FIRST
222+
// RTLD_GROUP
223+
// RTLD_NEXT
224+
// RTLD_PARENT
225+
// RTLD_PROBE
226+
// RTLD_SELF
227+
// RTLD_WORLD
228+
// RTLD_NODELETE
229+
// RTLD_NOLOAD
230+
// RTLD_DEEPBIND

src/os/unix/mod.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#[cfg(all(docsrs, not(unix)))]
44
mod unix_imports {
55
}
6-
#[cfg(unix)]
6+
#[cfg(any(not(docsrs), unix))]
77
mod unix_imports {
88
pub(super) use std::os::unix::ffi::OsStrExt;
99
}
@@ -13,7 +13,9 @@ use util::{ensure_compatible_types, cstr_cow_from_bytes};
1313
use std::ffi::{CStr, OsStr};
1414
use std::{fmt, marker, mem, ptr};
1515
use std::os::raw;
16+
pub use self::consts::*;
1617

18+
mod consts;
1719

1820
// dl* family of functions did not have enough thought put into it.
1921
//
@@ -385,7 +387,6 @@ impl<T> fmt::Debug for Symbol<T> {
385387
}
386388

387389
// Platform specific things
388-
389390
extern {
390391
fn dlopen(filename: *const raw::c_char, flags: raw::c_int) -> *mut raw::c_void;
391392
fn dlclose(handle: *mut raw::c_void) -> raw::c_int;
@@ -394,11 +395,6 @@ extern {
394395
fn dladdr(addr: *mut raw::c_void, info: *mut DlInfo) -> raw::c_int;
395396
}
396397

397-
#[cfg(not(target_os="android"))]
398-
const RTLD_NOW: raw::c_int = 2;
399-
#[cfg(target_os="android")]
400-
const RTLD_NOW: raw::c_int = 0;
401-
402398
#[repr(C)]
403399
struct DlInfo {
404400
dli_fname: *const raw::c_char,

0 commit comments

Comments
 (0)