| 1 |
pub mod apple; |
| 2 |
pub mod standard; |
| 3 |
pub mod wasm; |
| 4 |
pub mod windows; |
| 5 |
|
| 6 |
#[cfg(target_vendor = "apple")] |
| 7 |
pub use apple::{get_section, section_name}; |
| 8 |
#[cfg(all( |
| 9 |
not(target_family = "wasm"), |
| 10 |
not(target_os = "windows"), |
| 11 |
not(target_vendor = "apple") |
| 12 |
))] |
| 13 |
pub use standard::{get_section, section_name}; |
| 14 |
#[cfg(target_family = "wasm")] |
| 15 |
pub use wasm::{get_section, section_name}; |
| 16 |
#[cfg(target_os = "windows")] |
| 17 |
pub use windows::{get_section, section_name}; |
| 18 |
|
| 19 |
// Select the appropriate bounds and reference-storage types for the platform. |
| 20 |
#[cfg(target_family = "wasm")] |
| 21 |
pub use wasm::{Bounds, MovableBounds, MovableRefStorage, RefStorage}; |
| 22 |
#[cfg(not(target_family = "wasm"))] |
| 23 |
pub use {PtrBounds as Bounds, PtrMovableBounds as MovableBounds}; |
| 24 |
|
| 25 |
/// Rejects section names that cannot be represented on the current target. |
| 26 |
pub const fn validate_section_name(name: &str) { |
| 27 |
if cfg!(target_vendor = "apple") { |
| 28 |
apple::validate_apple_section_name(name); |
| 29 |
} |
| 30 |
if cfg!(all( |
| 31 |
not(target_family = "wasm"), |
| 32 |
not(target_os = "windows"), |
| 33 |
not(target_vendor = "apple") |
| 34 |
)) { |
| 35 |
standard::is_valid_section_name(name); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
/// Launder a pointer's provenance so it appears as an "exposed" pointer. |
| 40 |
pub fn launder_pointer_provenance<T>(ptr: *const T) -> *const T { |
| 41 |
#[cfg(not(windows))] |
| 42 |
{ |
| 43 |
core::ptr::with_exposed_provenance(ptr.expose_provenance()) |
| 44 |
} |
| 45 |
|
| 46 |
// Windows requires a stronger hint to avoid mis-optimization. On Windows, section bounds |
| 47 |
// come from marker sections. LLVM may fold ptrtoint/inttoptr under LTO, which takes our |
| 48 |
// exposed provenance and reverts it, which it then traces to the marker allocation which |
| 49 |
// it then believes all slice loads come from. |
| 50 |
// |
| 51 |
// Treating this provenance round-trip as a no-op is arguably an LLVM optimization issue |
| 52 |
// somewhere between Rust and LLVM. |
| 53 |
#[cfg(windows)] |
| 54 |
{ |
| 55 |
core::hint::black_box(core::ptr::with_exposed_provenance(ptr.expose_provenance())) |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
/// An immutable snapshot of a section's `[start, end)` range. |
| 60 |
/// |
| 61 |
/// Callers resolve a section's bounds once (via [`Bounds::range`] and friends) |
| 62 |
/// and then compute length, slices, and offsets from the snapshot without |
| 63 |
/// re-locking. `start` and `end` share provenance. |
| 64 |
#[derive(Clone, Copy)] |
| 65 |
pub struct SectionRange { |
| 66 |
start: *const (), |
| 67 |
end: *const (), |
| 68 |
} |
| 69 |
|
| 70 |
impl SectionRange { |
| 71 |
/// A range covering `[start, end)`. `start` and `end` must share provenance. |
| 72 |
#[inline(always)] |
| 73 |
pub const fn new(start: *const (), end: *const ()) -> Self { |
| 74 |
Self { start, end } |
| 75 |
} |
| 76 |
|
| 77 |
/// Section start address. |
| 78 |
#[inline(always)] |
| 79 |
pub const fn start_ptr(&self) -> *const () { |
| 80 |
self.start |
| 81 |
} |
| 82 |
|
| 83 |
/// One byte past the last section byte. |
| 84 |
#[inline(always)] |
| 85 |
pub const fn end_ptr(&self) -> *const () { |
| 86 |
self.end |
| 87 |
} |
| 88 |
|
| 89 |
/// Length in bytes (`end - start`). |
| 90 |
#[inline(always)] |
| 91 |
pub fn byte_len(&self) -> usize { |
| 92 |
// Provenance-insensitive difference. |
| 93 |
self.end.addr() - self.start.addr() |
| 94 |
} |
| 95 |
|
| 96 |
/// The range as a typed slice. `stride` must divide `byte_len()`. This is |
| 97 |
/// the shared body of every `as_slice` accessor (see `sections.rs`), kept |
| 98 |
/// here so the empty-range fast path and the `from_raw_parts` cast have one |
| 99 |
/// source of truth. |
| 100 |
/// |
| 101 |
/// The returned slice borrows the underlying section memory, not this |
| 102 |
/// snapshot (which is just two pointers and may be a temporary). The output |
| 103 |
/// lifetime is unbound and the caller must tie it to the real borrow. |
| 104 |
/// |
| 105 |
/// # Safety |
| 106 |
/// |
| 107 |
/// `self` must denote a valid, aligned, readable range of `len = byte_len() |
| 108 |
/// / stride` consecutive `T`s, and the returned slice's lifetime must not |
| 109 |
/// outlive that memory. Callers holding `§ion` tie the lifetime to that |
| 110 |
/// borrow. |
| 111 |
#[inline] |
| 112 |
pub unsafe fn slice_of<'a, T>(self, stride: usize) -> &'a [T] { |
| 113 |
let len = self.byte_len() / stride; |
| 114 |
if len == 0 { |
| 115 |
&[] |
| 116 |
} else { |
| 117 |
unsafe { ::core::slice::from_raw_parts(self.start as *const T, len) } |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
/// The mutable counterpart to [`Self::slice_of`]. |
| 122 |
/// |
| 123 |
/// # Safety |
| 124 |
/// |
| 125 |
/// As [`Self::slice_of`], plus: the caller must uphold exclusivity — no |
| 126 |
/// other reference (shared or mutable) to the same memory may exist for the |
| 127 |
/// returned slice's lifetime. Calling this twice on the same range without |
| 128 |
/// an intervening reborrow is UB. |
| 129 |
#[inline] |
| 130 |
pub unsafe fn slice_of_mut<'a, T>(self, stride: usize) -> &'a mut [T] { |
| 131 |
let len = self.byte_len() / stride; |
| 132 |
if len == 0 { |
| 133 |
&mut [] |
| 134 |
} else { |
| 135 |
unsafe { ::core::slice::from_raw_parts_mut(self.start as *mut T, len) } |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
/// Constant bounds for a pointer-based section. |
| 141 |
pub struct PtrBounds { |
| 142 |
/// Section start address. |
| 143 |
pub start: *const (), |
| 144 |
/// One byte past the last section byte. |
| 145 |
pub end: *const (), |
| 146 |
} |
| 147 |
|
| 148 |
impl PtrBounds { |
| 149 |
/// Bounds covering `[start, end)`. |
| 150 |
pub const fn new(start: *const (), end: *const ()) -> Self { |
| 151 |
Self { start, end } |
| 152 |
} |
| 153 |
|
| 154 |
/// Resolve the section into an immutable [`SectionRange`]. |
| 155 |
#[inline(always)] |
| 156 |
pub fn range(&self) -> SectionRange { |
| 157 |
// Launder the start's provenance and derive the end from it, so both |
| 158 |
// pointers share provenance while the length stays provenance-free. |
| 159 |
let start = launder_pointer_provenance(self.start); |
| 160 |
let byte_len = self.end.addr() - self.start.addr(); |
| 161 |
let end = unsafe { (start as *const u8).add(byte_len) as *const () }; |
| 162 |
SectionRange::new(start, end) |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
/// Bounds for a movable section and its associated backref section. |
| 167 |
pub struct PtrMovableBounds { |
| 168 |
/// Bounds for the submitted values. |
| 169 |
values: PtrBounds, |
| 170 |
/// Bounds for the submitted backrefs. |
| 171 |
refs: PtrBounds, |
| 172 |
} |
| 173 |
|
| 174 |
impl PtrMovableBounds { |
| 175 |
/// Create movable-section bounds. |
| 176 |
pub const fn new(values: PtrBounds, refs: PtrBounds) -> Self { |
| 177 |
Self { values, refs } |
| 178 |
} |
| 179 |
|
| 180 |
/// Resolve the movable item section into an immutable [`SectionRange`]. |
| 181 |
#[inline(always)] |
| 182 |
pub fn range(&self) -> SectionRange { |
| 183 |
self.values.range() |
| 184 |
} |
| 185 |
|
| 186 |
/// Resolve the movable backref section into an immutable [`SectionRange`]. |
| 187 |
#[inline(always)] |
| 188 |
pub fn backrefs_range(&self) -> SectionRange { |
| 189 |
self.refs.range() |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
/// `UnsafeCell` that is `Sync` and `Send`. |
| 194 |
#[repr(transparent)] |
| 195 |
pub struct SyncUnsafeCell<T> { |
| 196 |
#[allow(unused)] |
| 197 |
cell: ::core::cell::UnsafeCell<T>, |
| 198 |
} |
| 199 |
|
| 200 |
impl<T> SyncUnsafeCell<T> { |
| 201 |
/// Create a new `SyncUnsafeCell`. |
| 202 |
pub const fn new(value: T) -> Self { |
| 203 |
Self { |
| 204 |
cell: ::core::cell::UnsafeCell::new(value), |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
/// Get a raw pointer to the contained value. |
| 209 |
#[inline] |
| 210 |
pub const fn get(&self) -> *mut T { |
| 211 |
self.cell.get() |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
unsafe impl<T> Sync for SyncUnsafeCell<T> {} |
| 216 |
unsafe impl<T> Send for SyncUnsafeCell<T> {} |
| 217 |
|
| 218 |
/// Platform storage backing a [`crate::Ref`] off WASM: the value lives inline |
| 219 |
/// (and is placed directly in the linker section), so the handle is layout- |
| 220 |
/// compatible with `T`. |
| 221 |
#[cfg(not(target_family = "wasm"))] |
| 222 |
#[repr(C)] |
| 223 |
pub struct RefStorage<T: 'static> { |
| 224 |
t: T, |
| 225 |
} |
| 226 |
|
| 227 |
#[cfg(not(target_family = "wasm"))] |
| 228 |
impl<T> RefStorage<T> { |
| 229 |
/// Storage holding `t` inline. |
| 230 |
pub const fn new(t: T) -> Self { |
| 231 |
Self { t } |
| 232 |
} |
| 233 |
|
| 234 |
/// Pointer to the inline value. |
| 235 |
pub fn as_ptr(&self) -> *const T { |
| 236 |
&self.t as *const T |
| 237 |
} |
| 238 |
|
| 239 |
/// The inline value. |
| 240 |
pub fn get(&self) -> &T { |
| 241 |
&self.t |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
/// Platform storage backing a [`crate::MovableRef`] off WASM: a stable pointer |
| 246 |
/// slot updated in place when the section is reordered. `slot` is the sole |
| 247 |
/// field, so [`crate::MovableRef::slot_ptr`] is an offset-0 cast. |
| 248 |
#[cfg(not(target_family = "wasm"))] |
| 249 |
#[repr(C)] |
| 250 |
pub struct MovableRefStorage<T: 'static> { |
| 251 |
slot: SyncUnsafeCell<*const T>, |
| 252 |
} |
| 253 |
|
| 254 |
#[cfg(not(target_family = "wasm"))] |
| 255 |
impl<T> MovableRefStorage<T> { |
| 256 |
/// Storage whose slot initially points at `ptr`. |
| 257 |
pub const fn new(ptr: *const T) -> Self { |
| 258 |
Self { |
| 259 |
slot: SyncUnsafeCell::new(ptr), |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
/// The current slot pointer. |
| 264 |
pub const fn as_ptr(&self) -> *const T { |
| 265 |
unsafe { *self.slot.get() } |
| 266 |
} |
| 267 |
|
| 268 |
/// The item currently referenced by the slot. |
| 269 |
pub fn get(&self) -> &T { |
| 270 |
unsafe { self.as_ptr().as_ref().expect("MovableRef not initialized") } |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
/// A non-zero-sized type that is used to align the start and end of the |
| 275 |
/// section. |
| 276 |
#[repr(C)] |
| 277 |
pub struct Alignment<T> { |
| 278 |
_align: [T; 0], |
| 279 |
_padding: u8, |
| 280 |
} |
| 281 |
|
| 282 |
#[allow(clippy::new_without_default)] |
| 283 |
impl<T> Alignment<T> { |
| 284 |
/// Zero-sized alignment anchor. |
| 285 |
pub const fn new() -> Self { |
| 286 |
Self { |
| 287 |
_align: [], |
| 288 |
_padding: 0, |
| 289 |
} |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
/// Declares the section_name macro. |
| 294 |
#[macro_export] |
| 295 |
#[doc(hidden)] |
| 296 |
macro_rules! __def_section_name { |
| 297 |
( |
| 298 |
$__name:ident, |
| 299 |
{$( |
| 300 |
$__section:ident $__type:ident => $__prefix:tt __ $__suffix:tt; |
| 301 |
)*} |
| 302 |
AUXILIARY = $__aux_sep:literal; |
| 303 |
REFS = $__refs_sep:literal; |
| 304 |
MAX_LENGTH = $__max_length:literal; |
| 305 |
HASH_LENGTH = $__hash_length:literal; |
| 306 |
VALID_SECTION_CHARS = $__valid_section_chars:literal; |
| 307 |
) => { |
| 308 |
mod $__name { |
| 309 |
/// Internal macro for generating a section name. |
| 310 |
#[macro_export] |
| 311 |
#[doc(hidden)] |
| 312 |
macro_rules! $__name { |
| 313 |
$( |
| 314 |
(string item $__section $__type ($name:tt () $unsafe:tt)) => { |
| 315 |
$crate::__support::hash!($unsafe ($__prefix) ($name) ($__suffix) $__hash_length $__max_length $__valid_section_chars) |
| 316 |
}; |
| 317 |
(string item $__section $__type ($aux:tt $name:tt $unsafe:tt)) => { |
| 318 |
$crate::__support::hash!($unsafe ($__prefix) ($name $__aux_sep $aux) ($__suffix) $__hash_length $__max_length $__valid_section_chars) |
| 319 |
}; |
| 320 |
(string backref $__section $__type ($name:tt () $unsafe:tt)) => { |
| 321 |
$crate::__support::hash!($unsafe ($__prefix) ($name $__refs_sep) ($__suffix) $__hash_length $__max_length $__valid_section_chars) |
| 322 |
}; |
| 323 |
(string backref $__section $__type ($aux:tt $name:tt $unsafe:tt)) => { |
| 324 |
$crate::__support::hash!($unsafe ($__prefix) ($name $__aux_sep $aux $__refs_sep) ($__suffix) $__hash_length $__max_length $__valid_section_chars) |
| 325 |
}; |
| 326 |
)* |
| 327 |
($pattern:tt $unknown_ref_or_item:ident $unknown_section:ident $unknown_type:ident $name:ident) => { |
| 328 |
const _: () = { |
| 329 |
compile_error!(concat!("Unknown section type: `", stringify!($unknown_ref_or_item), "/", stringify!($unknown_section), "/", stringify!($unknown_type), "`")); |
| 330 |
} |
| 331 |
}; |
| 332 |
} |
| 333 |
|
| 334 |
pub use $__name as section_name; |
| 335 |
} |
| 336 |
|
| 337 |
pub use $__name::section_name; |
| 338 |
|
| 339 |
pub(crate) const MAX_LENGTH: usize = $__max_length; |
| 340 |
pub(crate) const VALID_SECTION_CHARS: &[u8] = $__valid_section_chars.as_bytes(); |
| 341 |
|
| 342 |
#[allow(unused)] |
| 343 |
pub(crate) const fn is_valid_section_char(b: u8) -> bool { |
| 344 |
let mut i = 0; |
| 345 |
while i < VALID_SECTION_CHARS.len() { |
| 346 |
if VALID_SECTION_CHARS[i] == b { |
| 347 |
return true; |
| 348 |
} |
| 349 |
i += 1; |
| 350 |
} |
| 351 |
false |
| 352 |
} |
| 353 |
|
| 354 |
#[allow(unused)] |
| 355 |
pub(crate) const fn is_valid_section_name(name: &str) -> bool { |
| 356 |
let bytes = name.as_bytes(); |
| 357 |
if bytes.is_empty() || bytes.len() > MAX_LENGTH { |
| 358 |
return false; |
| 359 |
} |
| 360 |
let mut i = 0; |
| 361 |
while i < bytes.len() { |
| 362 |
let b = bytes[i]; |
| 363 |
if !is_valid_section_char(b) { |
| 364 |
return false; |
| 365 |
} |
| 366 |
i += 1; |
| 367 |
} |
| 368 |
true |
| 369 |
} |
| 370 |
}; |
| 371 |
} |