Skip to content

Commit ecb4fd5

Browse files
authored
Replace the origin-program, origin-thread and origin-signal features with a single take-charge feature (#121)
* Split libc and linux_raw implementations for program into separate modules * Replace the origin-program, origin-thread and origin-signal features with a single take-charge feature If you enable one of them you will have to enable the others anyway as you can't safely use libc anymore when you handle any of these things behind it's back. * Remove dependency on the memoffset crate * Simplify cfg's for the arch code The arch code is disabled when take-charge isn't enabled, so checking for take-charge in the arch code is not necessary. * Mention take-charge feature in the program doc comment
1 parent 2ded72b commit ecb4fd5

16 files changed

Lines changed: 192 additions & 175 deletions

File tree

Cargo.toml

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@ include = ["src", "Cargo.toml", "COPYRIGHT", "LICENSE*", "/*.md"]
1717
linux-raw-sys = { version = "0.4.9", default-features = false, features = ["general", "no_std", "elf"] }
1818
rustix = { version = "0.38.34", default-features = false }
1919
bitflags = { version = "2.4.0", default-features = false }
20-
memoffset = { version = "0.9.0", optional = true }
2120
log = { version = "0.4.14", default-features = false, optional = true }
2221
rustix-futex-sync = { version = "0.2.1", optional = true }
2322
smallvec = { version = "1.11.1", optional = true, features = ["const_new"] }
2423

25-
# Optional logging backends for use with "origin-program". You can use any
24+
# Optional logging backends for use with "take-charge". You can use any
2625
# external logger, but using these features allows origin to initialize the
2726
# logger before `origin_main`, so that you can see the log messages emitted
2827
# before `origin_main` is called.
@@ -66,26 +65,20 @@ rustc-dep-of-std = [
6665
"dep:compiler_builtins",
6766
]
6867

69-
# Use origin's implementation of program startup and shutdown.
70-
origin-program = ["rustix/use-explicitly-provided-auxv", "rustix/runtime"]
71-
72-
# Use origin's implementation of thread startup and shutdown.
73-
#
74-
# To use threads, it's also necessary to enable the "thread" feature.
75-
origin-thread = ["memoffset", "origin-program"]
76-
77-
# Use origin's implementation of signal handle registrtion.
68+
# Use origin's implementation of program and thread startup and shutdown as
69+
# well as signal handler registration.
7870
#
79-
# To use threads, it's also necessary to enable the "signal" feature.
80-
origin-signal = ["rustix/runtime"]
71+
# To use threads, it is also necessary to enable the "thread" feature.
72+
# To use signals, it is also necessary to enable the "signal" feature.
73+
take-charge = ["rustix/use-explicitly-provided-auxv", "rustix/runtime"]
8174

8275
# Use origin's `_start` definition.
83-
origin-start = ["origin-program"]
76+
origin-start = ["take-charge"]
8477

8578
# Don't use origin's `_start` definition, but export a `start` function which
8679
# is meant to be run very early in program startup and passed a pointer to
8780
# the initial stack. Don't enable this when enabling "origin-start".
88-
external-start = ["origin-program"]
81+
external-start = ["take-charge"]
8982

9083
# The loggers depend on a `.init_array` entry to initialize themselves, and
9184
# `env_logger` needs it so that `c-scape` can initialize environment variables
@@ -129,4 +122,4 @@ experimental-relocate = ["rustix/mm", "rustix/runtime"]
129122
unstable-errno = ["thread"]
130123

131124
[package.metadata.docs.rs]
132-
features = ["origin-thread", "origin-signal", "origin-start"]
125+
features = ["take-charge", "origin-start", "thread", "signal"]

example-crates/external-start/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ publish = false
77
[dependencies]
88
# Origin can be depended on just like any other crate. For no_std, disable
99
# the default features, and the desired features.
10-
origin = { path = "../..", default-features = false, features = ["origin-thread", "external-start", "thread", "alloc"] }
10+
origin = { path = "../..", default-features = false, features = ["take-charge", "external-start", "thread", "alloc"] }
1111

1212
# Ensure that libc gets linked.
1313
libc = { version = "0.2", default-features = false }

example-crates/origin-start-dynamic-linker/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ crate-type = ["cdylib"]
1010
[dependencies]
1111
# Origin can be depended on just like any other crate. For no_std, disable
1212
# the default features, and the desired features.
13-
origin = { path = "../..", default-features = false, features = ["origin-thread", "origin-start", "thread", "alloc", "experimental-relocate"] }
13+
origin = { path = "../..", default-features = false, features = ["take-charge", "origin-start", "thread", "alloc", "experimental-relocate"] }
1414

1515
# Crates to help writing no_std code.
1616
atomic-dbg = { version = "0.1.8", default-features = false }

example-crates/origin-start-lto/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ publish = false
77
[dependencies]
88
# Origin can be depended on just like any other crate. For no_std, disable
99
# the default features, and the desired features.
10-
origin = { path = "../..", default-features = false, features = ["origin-thread", "origin-start", "thread", "alloc"] }
10+
origin = { path = "../..", default-features = false, features = ["take-charge", "origin-start", "thread", "alloc"] }
1111

1212
# Crates to help writing no_std code.
1313
atomic-dbg = { version = "0.1.8", default-features = false }

example-crates/origin-start/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ publish = false
77
[dependencies]
88
# Origin can be depended on just like any other crate. For no_std, disable
99
# the default features, and the desired features.
10-
origin = { path = "../..", default-features = false, features = ["origin-thread", "origin-start", "thread", "alloc"] }
10+
origin = { path = "../..", default-features = false, features = ["take-charge", "origin-start", "thread", "alloc"] }
1111

1212
# Crates to help writing no_std code.
1313
atomic-dbg = { version = "0.1.8", default-features = false }

src/arch/aarch64.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ use core::arch::asm;
44
#[cfg(all(feature = "experimental-relocate", feature = "origin-start"))]
55
#[cfg(relocation_model = "pic")]
66
use linux_raw_sys::elf::{Elf_Dyn, Elf_Ehdr};
7-
#[cfg(feature = "origin-signal")]
7+
#[cfg(feature = "signal")]
88
use linux_raw_sys::general::__NR_rt_sigreturn;
99
#[cfg(all(feature = "experimental-relocate", feature = "origin-start"))]
1010
#[cfg(relocation_model = "pic")]
1111
use linux_raw_sys::general::{__NR_mprotect, PROT_READ};
12-
#[cfg(all(feature = "origin-thread", feature = "thread"))]
12+
#[cfg(feature = "thread")]
1313
use {
1414
core::ffi::c_void,
1515
linux_raw_sys::general::{__NR_clone, __NR_exit, __NR_munmap},
@@ -165,15 +165,15 @@ pub(super) unsafe fn relocation_mprotect_readonly(ptr: usize, len: usize) {
165165
}
166166

167167
/// The required alignment for the stack pointer.
168-
#[cfg(all(feature = "origin-thread", feature = "thread"))]
168+
#[cfg(feature = "thread")]
169169
pub(super) const STACK_ALIGNMENT: usize = 16;
170170

171171
/// A wrapper around the Linux `clone` system call.
172172
///
173173
/// This can't be implemented in `rustix` because the child starts executing at
174174
/// the same point as the parent and we need to use inline asm to have the
175175
/// child jump to our new-thread entrypoint.
176-
#[cfg(all(feature = "origin-thread", feature = "thread"))]
176+
#[cfg(feature = "thread")]
177177
#[inline]
178178
pub(super) unsafe fn clone(
179179
flags: u32,
@@ -215,15 +215,15 @@ pub(super) unsafe fn clone(
215215
}
216216

217217
/// Write a value to the platform thread-pointer register.
218-
#[cfg(all(feature = "origin-thread", feature = "thread"))]
218+
#[cfg(feature = "thread")]
219219
#[inline]
220220
pub(super) unsafe fn set_thread_pointer(ptr: *mut c_void) {
221221
asm!("msr tpidr_el0, {}", in(reg) ptr);
222222
debug_assert_eq!(thread_pointer(), ptr);
223223
}
224224

225225
/// Read the value of the platform thread-pointer register.
226-
#[cfg(feature = "origin-thread")]
226+
#[cfg(feature = "thread")]
227227
#[inline]
228228
pub(super) fn thread_pointer() -> *mut c_void {
229229
let ptr;
@@ -235,12 +235,12 @@ pub(super) fn thread_pointer() -> *mut c_void {
235235
}
236236

237237
/// TLS data starts at the location pointed to by the thread pointer.
238-
#[cfg(all(feature = "origin-thread", feature = "thread"))]
238+
#[cfg(feature = "thread")]
239239
pub(super) const TLS_OFFSET: usize = 0;
240240

241241
/// `munmap` the current thread, then carefully exit the thread without
242242
/// touching the deallocated stack.
243-
#[cfg(all(feature = "origin-thread", feature = "thread"))]
243+
#[cfg(feature = "thread")]
244244
#[inline]
245245
pub(super) unsafe fn munmap_and_exit_thread(map_addr: *mut c_void, map_len: usize) -> ! {
246246
asm!(
@@ -264,7 +264,7 @@ pub(super) unsafe fn munmap_and_exit_thread(map_addr: *mut c_void, map_len: usiz
264264
///
265265
/// This function must never be called other than by the `sa_restorer`
266266
/// mechanism.
267-
#[cfg(feature = "origin-signal")]
267+
#[cfg(feature = "signal")]
268268
#[naked]
269269
pub(super) unsafe extern "C" fn return_from_signal_handler() {
270270
asm!(
@@ -284,5 +284,5 @@ pub(super) unsafe extern "C" fn return_from_signal_handler() {
284284
///
285285
/// This function must never be called other than by the `sa_restorer`
286286
/// mechanism.
287-
#[cfg(feature = "origin-signal")]
287+
#[cfg(feature = "signal")]
288288
pub(super) use return_from_signal_handler as return_from_signal_handler_noinfo;

src/arch/arm.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ use linux_raw_sys::elf::{Elf_Dyn, Elf_Ehdr};
77
#[cfg(all(feature = "experimental-relocate", feature = "origin-start"))]
88
#[cfg(relocation_model = "pic")]
99
use linux_raw_sys::general::{__NR_mprotect, PROT_READ};
10-
#[cfg(feature = "origin-signal")]
10+
#[cfg(feature = "signal")]
1111
use linux_raw_sys::general::{__NR_rt_sigreturn, __NR_sigreturn};
12-
#[cfg(all(feature = "origin-thread", feature = "thread"))]
12+
#[cfg(feature = "thread")]
1313
use {
1414
core::ffi::c_void,
1515
linux_raw_sys::general::{__NR_clone, __NR_exit, __NR_munmap},
@@ -177,15 +177,15 @@ pub(super) unsafe fn relocation_mprotect_readonly(ptr: usize, len: usize) {
177177
}
178178

179179
/// The required alignment for the stack pointer.
180-
#[cfg(all(feature = "origin-thread", feature = "thread"))]
180+
#[cfg(feature = "thread")]
181181
pub(super) const STACK_ALIGNMENT: usize = 4;
182182

183183
/// A wrapper around the Linux `clone` system call.
184184
///
185185
/// This can't be implemented in `rustix` because the child starts executing at
186186
/// the same point as the parent and we need to use inline asm to have the
187187
/// child jump to our new-thread entrypoint.
188-
#[cfg(all(feature = "origin-thread", feature = "thread"))]
188+
#[cfg(feature = "thread")]
189189
#[inline]
190190
pub(super) unsafe fn clone(
191191
flags: u32,
@@ -228,15 +228,15 @@ pub(super) unsafe fn clone(
228228
}
229229

230230
/// Write a value to the platform thread-pointer register.
231-
#[cfg(all(feature = "origin-thread", feature = "thread"))]
231+
#[cfg(feature = "thread")]
232232
#[inline]
233233
pub(super) unsafe fn set_thread_pointer(ptr: *mut c_void) {
234234
rustix::runtime::arm_set_tls(ptr).expect("arm_set_tls");
235235
debug_assert_eq!(thread_pointer(), ptr);
236236
}
237237

238238
/// Read the value of the platform thread-pointer register.
239-
#[cfg(all(feature = "origin-thread", feature = "thread"))]
239+
#[cfg(feature = "thread")]
240240
#[inline]
241241
pub(super) fn thread_pointer() -> *mut c_void {
242242
let ptr;
@@ -248,12 +248,12 @@ pub(super) fn thread_pointer() -> *mut c_void {
248248
}
249249

250250
/// TLS data starts at the location pointed to by the thread pointer.
251-
#[cfg(all(feature = "origin-thread", feature = "thread"))]
251+
#[cfg(feature = "thread")]
252252
pub(super) const TLS_OFFSET: usize = 0;
253253

254254
/// `munmap` the current thread, then carefully exit the thread without
255255
/// touching the deallocated stack.
256-
#[cfg(all(feature = "origin-thread", feature = "thread"))]
256+
#[cfg(feature = "thread")]
257257
#[inline]
258258
pub(super) unsafe fn munmap_and_exit_thread(map_addr: *mut c_void, map_len: usize) -> ! {
259259
asm!(
@@ -277,7 +277,7 @@ pub(super) unsafe fn munmap_and_exit_thread(map_addr: *mut c_void, map_len: usiz
277277
///
278278
/// This function must never be called other than by the `sa_restorer`
279279
/// mechanism.
280-
#[cfg(feature = "origin-signal")]
280+
#[cfg(feature = "signal")]
281281
#[naked]
282282
pub(super) unsafe extern "C" fn return_from_signal_handler() {
283283
asm!(
@@ -296,7 +296,7 @@ pub(super) unsafe extern "C" fn return_from_signal_handler() {
296296
///
297297
/// This function must never be called other than by the `sa_restorer`
298298
/// mechanism.
299-
#[cfg(feature = "origin-signal")]
299+
#[cfg(feature = "signal")]
300300
#[naked]
301301
pub(super) unsafe extern "C" fn return_from_signal_handler_noinfo() {
302302
asm!(

src/arch/riscv64.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
//! Architecture-specific assembly code.
22
3-
#[cfg(any(
4-
all(feature = "origin-thread", feature = "thread"),
5-
feature = "origin-start"
6-
))]
3+
#[cfg(any(feature = "thread", feature = "origin-start"))]
74
use core::arch::asm;
85
#[cfg(all(feature = "experimental-relocate", feature = "origin-start"))]
96
#[cfg(relocation_model = "pic")]
107
use linux_raw_sys::elf::{Elf_Dyn, Elf_Ehdr};
118
#[cfg(all(feature = "experimental-relocate", feature = "origin-start"))]
129
#[cfg(relocation_model = "pic")]
1310
use linux_raw_sys::general::{__NR_mprotect, PROT_READ};
14-
#[cfg(all(feature = "origin-thread", feature = "thread"))]
11+
#[cfg(feature = "thread")]
1512
use {
1613
core::ffi::c_void,
1714
linux_raw_sys::general::{__NR_clone, __NR_exit, __NR_munmap},
@@ -165,15 +162,15 @@ pub(super) unsafe fn relocation_mprotect_readonly(ptr: usize, len: usize) {
165162
}
166163

167164
/// The required alignment for the stack pointer.
168-
#[cfg(all(feature = "origin-thread", feature = "thread"))]
165+
#[cfg(feature = "thread")]
169166
pub(super) const STACK_ALIGNMENT: usize = 16;
170167

171168
/// A wrapper around the Linux `clone` system call.
172169
///
173170
/// This can't be implemented in `rustix` because the child starts executing at
174171
/// the same point as the parent and we need to use inline asm to have the
175172
/// child jump to our new-thread entrypoint.
176-
#[cfg(all(feature = "origin-thread", feature = "thread"))]
173+
#[cfg(feature = "thread")]
177174
#[inline]
178175
pub(super) unsafe fn clone(
179176
flags: u32,
@@ -215,15 +212,15 @@ pub(super) unsafe fn clone(
215212
}
216213

217214
/// Write a value to the platform thread-pointer register.
218-
#[cfg(all(feature = "origin-thread", feature = "thread"))]
215+
#[cfg(feature = "thread")]
219216
#[inline]
220217
pub(super) unsafe fn set_thread_pointer(ptr: *mut c_void) {
221218
asm!("mv tp, {}", in(reg) ptr);
222219
debug_assert_eq!(thread_pointer(), ptr);
223220
}
224221

225222
/// Read the value of the platform thread-pointer register.
226-
#[cfg(all(feature = "origin-thread", feature = "thread"))]
223+
#[cfg(feature = "thread")]
227224
#[inline]
228225
pub(super) fn thread_pointer() -> *mut c_void {
229226
let ptr;
@@ -236,12 +233,12 @@ pub(super) fn thread_pointer() -> *mut c_void {
236233

237234
/// TLS data starts 0x800 bytes below the location pointed to by the thread
238235
/// pointer.
239-
#[cfg(all(feature = "origin-thread", feature = "thread"))]
236+
#[cfg(feature = "thread")]
240237
pub(super) const TLS_OFFSET: usize = 0x800;
241238

242239
/// `munmap` the current thread, then carefully exit the thread without
243240
/// touching the deallocated stack.
244-
#[cfg(all(feature = "origin-thread", feature = "thread"))]
241+
#[cfg(feature = "thread")]
245242
#[inline]
246243
pub(super) unsafe fn munmap_and_exit_thread(map_addr: *mut c_void, map_len: usize) -> ! {
247244
asm!(

0 commit comments

Comments
 (0)