| 1 |
//! Apple platform: `section$start$` and `section$end$` symbols. |
| 2 |
|
| 3 |
/// On Apple platforms, the linker provides a pointer to the start and end of |
| 4 |
/// the section regardless of the section's name. |
| 5 |
#[doc(hidden)] |
| 6 |
#[macro_export] |
| 7 |
macro_rules! __get_section_apple { |
| 8 |
(movable, name=$name:tt, type=$generic_ty:ty) => { |
| 9 |
{ |
| 10 |
$crate::__support::MovableBounds::new( |
| 11 |
$crate::__support::PtrBounds::new( |
| 12 |
$crate::__address_of_symbol!(item data start $name), |
| 13 |
$crate::__address_of_symbol!(item data end $name), |
| 14 |
), |
| 15 |
$crate::__support::PtrBounds::new( |
| 16 |
$crate::__address_of_symbol!(backref data start $name), |
| 17 |
$crate::__address_of_symbol!(backref data end $name), |
| 18 |
), |
| 19 |
) |
| 20 |
} |
| 21 |
}; |
| 22 |
($section_type:ident, name=$name:tt, type=$generic_ty:ty) => { |
| 23 |
{ |
| 24 |
$crate::__support::PtrBounds::new( |
| 25 |
$crate::__address_of_symbol!(item data start $name), |
| 26 |
$crate::__address_of_symbol!(item data end $name), |
| 27 |
) |
| 28 |
} |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
pub use crate::__get_section_apple as get_section; |
| 33 |
|
| 34 |
// \x01: "do not mangle" (ref https://github.com/rust-lang/rust-bindgen/issues/2935) |
| 35 |
crate::__def_section_name! { |
| 36 |
__section_name_apple, |
| 37 |
{ |
| 38 |
data bare => ("__DATA,") __ (); |
| 39 |
code bare => ("__TEXT,") __ (); |
| 40 |
data section => ("__DATA,") __ (",regular,no_dead_strip"); |
| 41 |
code section => ("__TEXT,") __ (",regular,pure_instructions"); |
| 42 |
data start => ("\x01section$start$__DATA$") __ (); |
| 43 |
data end => ("\x01section$end$__DATA$") __ (); |
| 44 |
} |
| 45 |
AUXILIARY = "_"; |
| 46 |
REFS = "_r_"; |
| 47 |
// We use base63 for hashes. 8 characters yields 248,155,780,267,521 |
| 48 |
// possible values. This gives us space for 5,045,539 hashes (which are |
| 49 |
// built on the section's raw name, location information and a hash of the |
| 50 |
// source text) before we have a 5% chance of collision. |
| 51 |
MAX_LENGTH = 16; |
| 52 |
HASH_LENGTH = 8; |
| 53 |
VALID_SECTION_CHARS = "_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; |
| 54 |
} |
| 55 |
|
| 56 |
const fn find_byte(bytes: &[u8], byte: u8) -> Option<usize> { |
| 57 |
let mut i = 0; |
| 58 |
while i < bytes.len() { |
| 59 |
if bytes[i] == byte { |
| 60 |
return Some(i); |
| 61 |
} |
| 62 |
i += 1; |
| 63 |
} |
| 64 |
None |
| 65 |
} |
| 66 |
|
| 67 |
const fn write_bytes<const N: usize>(out: &mut [u8; N], mut pos: usize, bytes: &[u8]) -> usize { |
| 68 |
let mut i = 0; |
| 69 |
while i < bytes.len() { |
| 70 |
out[pos] = bytes[i]; |
| 71 |
pos += 1; |
| 72 |
i += 1; |
| 73 |
} |
| 74 |
pos |
| 75 |
} |
| 76 |
|
| 77 |
const fn panic_invalid_apple_section_name(name: &str, reason: &'static str) -> ! { |
| 78 |
const PREFIX: &str = "link-section: invalid section name `"; |
| 79 |
const SUFFIX: &str = "`: "; |
| 80 |
let mut out = [b' '; 1024]; |
| 81 |
let mut pos = 0; |
| 82 |
pos = write_bytes(&mut out, pos, PREFIX.as_bytes()); |
| 83 |
pos = write_bytes(&mut out, pos, name.as_bytes()); |
| 84 |
pos = write_bytes(&mut out, pos, SUFFIX.as_bytes()); |
| 85 |
write_bytes(&mut out, pos, reason.as_bytes()); |
| 86 |
let msg = match core::str::from_utf8(&out) { |
| 87 |
Ok(s) => s, |
| 88 |
Err(_) => reason, |
| 89 |
}; |
| 90 |
let msg = msg.trim_ascii(); |
| 91 |
panic!("{}", msg); |
| 92 |
} |
| 93 |
|
| 94 |
pub(crate) const fn validate_apple_section_name(name: &str) { |
| 95 |
match validate_apple_section_name_res(name) { |
| 96 |
Ok(()) => {} |
| 97 |
Err(reason) => panic_invalid_apple_section_name(name, reason), |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
const fn validate_apple_section_name_res(name: &str) -> Result<(), &'static str> { |
| 102 |
let bytes = name.as_bytes(); |
| 103 |
let comma = match find_byte(bytes, b',') { |
| 104 |
Some(i) => i, |
| 105 |
None => return Err("section name must contain a comma"), |
| 106 |
}; |
| 107 |
if comma == 0 { |
| 108 |
return Err("section name must have a segment before the comma"); |
| 109 |
} |
| 110 |
|
| 111 |
let mut i = comma + 1; |
| 112 |
if i >= bytes.len() { |
| 113 |
return Err("section name must not be empty"); |
| 114 |
} |
| 115 |
let mut section_len = 0; |
| 116 |
while i < bytes.len() { |
| 117 |
if bytes[i] == b',' { |
| 118 |
break; |
| 119 |
} |
| 120 |
if !is_valid_section_char(bytes[i]) { |
| 121 |
return Err("section name contains invalid character(s)"); |
| 122 |
} |
| 123 |
section_len += 1; |
| 124 |
i += 1; |
| 125 |
} |
| 126 |
if section_len == 0 { |
| 127 |
return Err("Mach-O section name must not be empty"); |
| 128 |
} |
| 129 |
if section_len > MAX_LENGTH { |
| 130 |
if cfg!(feature = "proc_macro") { |
| 131 |
// `hash!` shortens linker section names; const metadata keeps the raw name. |
| 132 |
return Ok(()); |
| 133 |
} |
| 134 |
return Err("Mach-O section name must be 1 to 16 characters"); |
| 135 |
} |
| 136 |
Ok(()) |
| 137 |
} |