Skip to main content

link_section/
lib.rs

1#![doc = include_str!("../README.md")]
2#![allow(unsafe_code)]
3
4/// Declarative forms of the `#[section]` and `#[in_section(...)]` macros.
5///
6/// The declarative forms wrap and parse a proc_macro-like syntax like so, and
7/// are identical in expansion to the undecorated procedural macros. The
8/// declarative forms support the same attribute parameters as the procedural
9/// macros.
10pub mod declarative {
11    pub use crate::__in_section_parse as in_section;
12    pub use crate::__section_parse as section;
13}
14
15#[doc(hidden)]
16pub mod __support {
17    pub use crate::__add_section_link_attribute as add_section_link_attribute;
18    pub use crate::__def_section_name as def_section_name;
19    pub use crate::__get_section as get_section;
20    pub use crate::__in_section_crate as in_section_crate;
21    pub use crate::__in_section_parse as in_section_parse;
22    pub use crate::__section_parse as section_parse;
23
24    #[cfg(feature = "proc_macro")]
25    pub use link_section_proc_macro::hash;
26    #[cfg(feature = "proc_macro")]
27    pub use link_section_proc_macro::ident_concat;
28
29    /// Declares the section_name macro.
30    #[macro_export]
31    #[doc(hidden)]
32    macro_rules! __def_section_name {
33        (
34            {$(
35                $__section:ident $__type:ident => $__prefix:tt __ $__suffix:tt;
36            )*}
37            AUXILIARY = $__aux_sep:literal;
38            MAX_LENGTH = $__max_length:literal;
39            HASH_LENGTH = $__hash_length:literal;
40            VALID_SECTION_CHARS = $__valid_section_chars:literal;
41        ) => {
42            /// Internal macro for generating a section name.
43            #[macro_export]
44            #[doc(hidden)]
45            macro_rules! __section_name {
46                $(
47                    (raw $__section $__type $name:ident) => {
48                        concat!(concat! $__prefix, stringify!($name), concat! $__suffix);
49                    };
50                    (raw $__section $__type $aux:ident $name:ident) => {
51                        concat!(concat! $__prefix, stringify!($name), $__aux_sep, stringify!($aux), concat! $__suffix);
52                    };
53                    ($pattern:tt $__section $__type $name:ident) => {
54                        $crate::__support::hash!($pattern ($__prefix) $name ($__suffix) $__hash_length $__max_length $__valid_section_chars);
55                    };
56                    ($pattern:tt $__section $__type $aux:ident $name:ident) => {
57                        $crate::__support::hash!($pattern ($__prefix) ($name $aux) ($__suffix) $__hash_length $__max_length $__valid_section_chars);
58                    };
59                )*
60                ($pattern:tt $unknown_section:ident $unknown_type:ident $name:ident) => {
61                    const _: () = {
62                        compile_error!("Unknown section type: `{}`/`{}`", stringify!($unknown_section), stringify!($unknown_type));
63                    };
64                };
65            }
66        };
67    }
68
69    #[cfg(feature = "proc_macro")]
70    #[doc(hidden)]
71    #[macro_export]
72    macro_rules! __add_section_link_attribute(
73        ($section:ident $type:ident $name:ident $($aux:ident)? #[$attr:ident = __] $item:item) => {
74            $crate::__section_name!(
75                (#[$attr = __] #[allow(unsafe_code)] $item)
76                $section $type $name $($aux)?
77            );
78        }
79    );
80
81    #[cfg(not(feature = "proc_macro"))]
82    #[doc(hidden)]
83    #[macro_export]
84    macro_rules! __add_section_link_attribute(
85        ($section:ident $type:ident $name:ident #[$attr:ident = __] $item:item) => {
86            #[$attr = $crate::__section_name!(
87                raw $section $type $name
88            )] $item
89        }
90    );
91
92    // \x01: "do not mangle" (ref https://github.com/rust-lang/rust-bindgen/issues/2935)
93    #[cfg(target_vendor = "apple")]
94    def_section_name! {
95        {
96            data bare =>    ("__DATA,") __ ();
97            code bare =>    ("__TEXT,") __ ();
98            data section => ("__DATA,") __ (",regular,no_dead_strip");
99            code section => ("__TEXT,") __ (",regular,no_dead_strip");
100            data start =>   ("\x01section$start$__DATA$") __ ();
101            data end =>     ("\x01section$end$__DATA$") __ ();
102        }
103        AUXILIARY = "_";
104        MAX_LENGTH = 16;
105        HASH_LENGTH = 6;
106        VALID_SECTION_CHARS = "_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
107    }
108
109    #[cfg(target_family = "wasm")]
110    def_section_name! {
111        {
112            data bare =>    (".data", ".link_section.") __ ();
113            data section => (".data", ".link_section.") __ ();
114            code bare =>    (".text", ".link_section.") __ ();
115            code section => (".text", ".link_section.") __ ();
116        }
117        AUXILIARY = ".";
118        MAX_LENGTH = 16;
119        HASH_LENGTH = 6;
120        VALID_SECTION_CHARS = "_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
121    }
122
123    #[cfg(all(
124        not(target_vendor = "apple"),
125        not(target_vendor = "pc"),
126        not(target_family = "wasm")
127    ))]
128    def_section_name! {
129        {
130            data bare =>    ("_data", "_link_section_") __ ();
131            data section => ("_data", "_link_section_") __ ();
132            data start =>   ("__start_", "_data", "_link_section_") __ ();
133            data end =>     ("__stop_", "_data", "_link_section_") __ ();
134            code bare =>    ("_text", "_link_section_") __ ();
135            code section => ("_text", "_link_section_") __ ();
136            code start =>   ("__start_", "_text", "_link_section_") __ ();
137            code end =>     ("__stop_", "_text", "_link_section_") __ ();
138        }
139        AUXILIARY = "_";
140        MAX_LENGTH = 64;
141        HASH_LENGTH = 10;
142        VALID_SECTION_CHARS = "_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
143    }
144
145    #[cfg(target_vendor = "pc")]
146    def_section_name! {
147        {
148            data bare =>    (".data", "$") __ ();
149            data section => (".data", "$") __ ("$b");
150            data start =>   (".data", "$") __ ("$a");
151            data end =>     (".data", "$") __ ("$c");
152            code bare =>    (".text", "$") __ ();
153            code section => (".text", "$") __ ("$b");
154            code start =>   (".text", "$") __ ("$a");
155            code end =>     (".text", "$") __ ("$c");
156        }
157        AUXILIARY = "$d$";
158        MAX_LENGTH = 64;
159        HASH_LENGTH = 10;
160        VALID_SECTION_CHARS = "_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
161    }
162
163    #[cfg(not(feature = "proc_macro"))]
164    #[doc(hidden)]
165    #[macro_export]
166    macro_rules! __declare_macro {
167        ($vis:vis $ident:ident $generic_macro:ident $args:tt) => {
168            /// Internal macro for parsing the section. This is exported with
169            /// the same name as the type below.
170            #[doc(hidden)]
171            $vis use $crate::$generic_macro as $ident;
172        };
173    }
174
175    #[cfg(feature = "proc_macro")]
176    #[doc(hidden)]
177    #[macro_export]
178    macro_rules! __declare_macro {
179        ($vis:vis $ident:ident $generic_macro:ident $args:tt) => {
180            $crate::__support::ident_concat!(
181                (#[macro_export]
182                #[doc(hidden)]
183                macro_rules!)  (__ $ident __link_section_private_macro__) ({
184                    ($passthru:tt) => {
185                        $crate::$generic_macro!($passthru $args);
186                    };
187                })
188            );
189
190            $crate::__support::ident_concat!(
191                (#[doc(hidden)] pub use) (__ $ident __link_section_private_macro__) (as $ident;)
192            );
193        }
194    }
195
196    /// Define a link section.
197    #[macro_export]
198    #[doc(hidden)]
199    macro_rules! __section_parse {
200        // Has a generic (note that $generic eats the trailing semicolon)
201        (#[section $($args:tt)*] $(#[$meta:meta])* $vis:vis static $ident:ident : $(:: $path_prefix:ident ::)? $($path:ident)::* < $($generic:tt)*) => {
202            $crate::__section_parse!(@parsed #[section $($args)*] $(#[$meta])* $vis static $ident: ( $(:: $path_prefix ::)? $($path)::* < $($generic)*) ( $($generic)* ) __in_section_helper_macro_generic);
203        };
204        // No generic
205        (#[section $($args:tt)*] $(#[$meta:meta])* $vis:vis static $ident:ident : $(:: $path_prefix:ident ::)? $($path:ident)::* ;) => {
206            $crate::__section_parse!(@parsed #[section $($args)*] $(#[$meta])* $vis static $ident: ( $(:: $path_prefix ::)? $($path)::* ;) ( () > ; ) __in_section_helper_macro_no_generic);
207        };
208        // Both end up here...
209        (@parsed #[section $($args:tt)*] $(#[$meta:meta])* $vis:vis static $ident:ident : ($ty:ty ;) ( $generic_ty:ty > ; ) $generic_macro:ident) => {
210            $crate::__declare_macro!($vis $ident $generic_macro ($($args)*));
211            $crate::__section_parse!(@generate #[section $($args)*] $(#[$meta])* $vis static $ident: $ty, $generic_ty, $generic_macro);
212        };
213        (@generate #[section $($args:tt)*] $(#[$meta:meta])* $vis:vis static $ident:ident : $ty:ty, $generic_ty:ty, __in_section_helper_macro_generic) => {
214            $crate::__section_parse!(@generate #[section $($args)*] $(#[$meta])* $vis static $ident: $ty, $generic_ty, __base_case__);
215
216            impl ::core::iter::IntoIterator for $ident {
217                type Item = &'static $generic_ty;
218                type IntoIter = ::core::slice::Iter<'static, $generic_ty>;
219                fn into_iter(self) -> Self::IntoIter {
220                    $ident.as_slice().iter()
221                }
222            }
223        };
224        (@generate #[section $($args:tt)*] $(#[$meta:meta])* $vis:vis static $ident:ident : $ty:ty, $generic_ty:ty, $generic_macro:ident) => {
225            $(#[$meta])*
226            #[allow(non_camel_case_types)]
227            $vis struct $ident;
228
229            impl $crate::__support::SectionItemType for $ident {
230                type Item = $generic_ty;
231            }
232
233            impl ::core::fmt::Debug for $ident {
234                fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
235                    ::core::ops::Deref::deref(self).fmt(f)
236                }
237            }
238
239            impl ::core::ops::Deref for $ident {
240                type Target = $ty;
241                fn deref(&self) -> &Self::Target {
242                    self.const_deref()
243                }
244            }
245
246            $crate::__section_parse!(@deref #[section $($args)*] $(#[$meta])* $vis static $ident: $ty, $generic_ty, __base_case__);
247        };
248        (@deref #[section] $(#[$meta:meta])* $vis:vis static $ident:ident : $ty:ty, $generic_ty:ty, __base_case__) => {
249            impl $ident {
250                /// Get a `const` reference to the underlying section. In
251                /// non-const contexts, `deref` is sufficient.
252                pub const fn const_deref(&self) -> &$ty {
253                    static SECTION: $ty = {
254                        let section = $crate::__support::get_section!(name=$ident, type=$generic_ty, aux=);
255                        let name = $crate::__section_name!(
256                            raw data bare $ident
257                        );
258                        unsafe { <$ty>::new(name, section.0, section.1) }
259                    };
260                    &SECTION
261                }
262            }
263        };
264        (@deref #[section(aux=$aux:ident)] $(#[$meta:meta])* $vis:vis static $ident:ident : $ty:ty, $generic_ty:ty, __base_case__) => {
265            impl $ident {
266                /// Get a `const` reference to the underlying section. In
267                /// non-const contexts, `deref` is sufficient.
268                pub const fn const_deref(&self) -> &$ty {
269                    static SECTION: $ty = {
270                        let section = $crate::__support::get_section!(name=$ident, type=$generic_ty, aux=$aux);
271                        let name = $crate::__section_name!(
272                            raw data bare $ident $aux
273                        );
274                        unsafe { <$ty>::new(name, section.0, section.1) }
275                    };
276                    &SECTION
277                }
278            }
279        };
280    }
281
282    #[cfg(miri)]
283    #[doc(hidden)]
284    #[macro_export]
285    macro_rules! __get_section {
286        (name=$ident:ident, type=$generic_ty:ty, aux=$($aux:ident)?) => {
287            // Disable link sections for miri (`extern static `␁section$start$__DATA$CTOR` is not supported by Miri`)
288            {
289                (std::ptr::null_mut(), std::ptr::null_mut())
290            }
291        };
292    }
293
294    #[cfg(all(not(miri), target_family = "wasm"))]
295    #[doc(hidden)]
296    #[macro_export]
297    macro_rules! __get_section {
298        (name=$ident:ident, type=$generic_ty:ty, aux=$($aux:ident)?) => {
299            {
300                static __START: ::core::sync::atomic::AtomicPtr::<::core::marker::PhantomData<$generic_ty>> = unsafe {
301                    ::core::sync::atomic::AtomicPtr::<::core::marker::PhantomData<$generic_ty>>::new(::core::ptr::null_mut())
302                };
303                static __END: ::core::sync::atomic::AtomicPtr::<::core::marker::PhantomData<$generic_ty>> = unsafe {
304                    ::core::sync::atomic::AtomicPtr::<::core::marker::PhantomData<$generic_ty>>::new(::core::ptr::null_mut())
305                };
306
307                $crate::__support::ident_concat!((#[no_mangle]pub extern "C" fn) (register_link_section_ $ident) ((data_ptr: *const u8, data_len: usize) {
308                    unsafe {
309                        __START.store(data_ptr as *mut ::core::marker::PhantomData<$generic_ty>, ::core::sync::atomic::Ordering::Relaxed);
310                        __END.store(data_ptr.add(data_len) as *mut ::core::marker::PhantomData<$generic_ty>, ::core::sync::atomic::Ordering::Relaxed);
311                    }
312                }));
313
314                (&__START, &__END)
315            }
316        }
317    }
318
319    #[cfg(all(not(miri), target_vendor = "pc"))]
320    #[doc(hidden)]
321    #[macro_export]
322    macro_rules! __get_section {
323        (name=$ident:ident, type=$generic_ty:ty, aux=$($aux:ident)?) => {
324            {
325                $crate::__support::add_section_link_attribute!(
326                    data start $ident $($aux)?
327                    #[link_section = __]
328                    static __START: [$generic_ty; 0] = [];
329                );
330                $crate::__support::add_section_link_attribute!(
331                    data end $ident $($aux)?
332                    #[link_section = __]
333                    static __END: [$generic_ty; 0] = [];
334                );
335
336                (
337                    unsafe { &raw const __START as $crate::__support::SectionPtr<$generic_ty> },
338                    unsafe { &raw const __END as $crate::__support::SectionPtr<$generic_ty> },
339                )
340            }
341        }
342    }
343
344    #[cfg(all(not(miri), not(target_family = "wasm"), not(target_vendor = "pc")))]
345    #[doc(hidden)]
346    #[macro_export]
347    macro_rules! __get_section {
348        (name=$ident:ident, type=$generic_ty:ty, aux=$($aux:ident)?) => {
349            {
350                extern "C" {
351                    $crate::__support::add_section_link_attribute!(
352                        data start $ident $($aux)?
353                        #[link_name = __]
354                        static __START: $crate::__support::SectionPtr<$generic_ty>;
355                    );
356                }
357                extern "C" {
358                    $crate::__support::add_section_link_attribute!(
359                        data end $ident $($aux)?
360                        #[link_name = __]
361                        static __END: $crate::__support::SectionPtr<$generic_ty>;
362                    );
363                }
364
365                (
366                    unsafe { &raw const __START as $crate::__support::SectionPtr<$generic_ty> },
367                    unsafe { &raw const __END as $crate::__support::SectionPtr<$generic_ty> },
368                )
369            }
370        }
371    }
372
373    /// Export a symbol into a link section.
374    #[macro_export]
375    #[doc(hidden)]
376    macro_rules! __in_section_parse {
377        // Needs to handle:
378        //  <name>
379        //  :: <name>
380        //  <path> :: <name>
381        //  :: <path> :: <name>
382        //  etc...
383        (#[in_section( $($path:tt)* )] $($item:tt)*) => {
384            $crate::__support::in_section_parse!(path=[$($path)*] #[in_section($($path)*)] $($item)*);
385        };
386        (path=[$orig_path:path] #[in_section($name:ident)] $($item:tt)*) => {
387            $orig_path ! (
388                (v=0 (name=$name (path=[$orig_path] (item=($($item)*) ()))))
389            );
390        };
391        (path=[$orig_path:path] #[in_section(:: $($path:ident)::*)] $($item:tt)*) => {
392            $crate::__support::in_section_parse!(path=[$orig_path] #[in_section($($path)::*)] $($item)*);
393        };
394        (path=[$orig_path:path] #[in_section($prefix:ident :: $($path:ident)::*)] $($item:tt)*) => {
395            $crate::__support::in_section_parse!(path=[$orig_path] #[in_section($($path)::*)] $($item)*);
396        };
397    }
398
399    #[macro_export]
400    #[doc(hidden)]
401    macro_rules! __in_section_helper_macro_generic {
402        ((v=0 (name=$ident:ident (path=[$path:path] (item=$item:tt $rest:tt))))) => {
403            $crate::__support::in_section_crate!($ident,, $path, generic, $item);
404        };
405        ((v=0 (name=$ident:ident (path=[$path:path] (item=$item:tt $rest:tt)))) ((aux=$aux:ident)) )=> {
406            $crate::__support::in_section_crate!($ident, $aux, $path, generic, $item);
407        };
408        ((v=0 (name=$ident:ident (path=[$path:path] (item=$item:tt $rest:tt)))) () )=> {
409            $crate::__support::in_section_crate!($ident,, $path, generic, $item);
410        };
411        (v=$v:literal $rest:tt) => {
412            const _: () = {
413                compile_error!(concat!(
414                    "link-section: Unsupported version: `",
415                    stringify!($v),
416                    "`: ",
417                    stringify!($rest)
418                ));
419            };
420        };
421    }
422
423    #[macro_export]
424    #[doc(hidden)]
425    macro_rules! __in_section_helper_macro_no_generic {
426        ((v=0 (name=$ident:ident (path=[$path:path] (item=$item:tt $rest:tt))))) => {
427            $crate::__support::in_section_crate!($ident,, $path, no_generic, $item);
428        };
429        ((v=0 (name=$ident:ident (path=[$path:path] (item=$item:tt $rest:tt)))) ((aux=$aux:ident)) )=> {
430            $crate::__support::in_section_crate!($ident, $aux, $path, no_generic, $item);
431        };
432        ((v=0 (name=$ident:ident (path=[$path:path] (item=$item:tt $rest:tt)))) () )=> {
433            $crate::__support::in_section_crate!($ident,, $path, no_generic, $item);
434        };
435        (v=$v:literal $rest:tt) => {
436            const _: () = {
437                compile_error!(concat!(
438                    "link-section: Unsupported version: `",
439                    stringify!($v),
440                    "`: ",
441                    stringify!($rest)
442                ));
443            };
444        };
445    }
446
447    #[macro_export]
448    #[doc(hidden)]
449    #[allow(unknown_lints, edition_2024_expr_fragment_specifier)]
450    macro_rules! __in_section_crate {
451        ($ident:ident, $($aux:ident)?, $path:path, generic, ($(#[$meta:meta])* $vis:vis fn $ident_fn:ident($($args:tt)*) $(-> $ret:ty)? $body:block)) => {
452            $crate::__add_section_link_attribute!(
453                data section $ident $($aux)?
454                #[link_section = __]
455                // Split the function into a static item and a function pointer
456                $(#[$meta])*
457                #[used]
458                #[allow(non_upper_case_globals)]
459                $vis static $ident_fn: <$path as $crate::__support::SectionItemType>::Item =
460                    {
461                        fn $ident_fn($($args)*) $(-> $ret)? $body
462                        $ident_fn as _
463                    };
464            );
465        };
466        ($ident:ident, $($aux:ident)?, $path:path, generic, ($(#[$meta:meta])* $vis:vis static _ : $ty:ty = $value:expr;)) => {
467            const _: () = {
468                $crate::__add_section_link_attribute!(
469                    data section $ident $($aux)?
470                    #[link_section = __]
471                    $(#[$meta])* #[used] $vis static ANONYMOUS: <$path as $crate::__support::SectionItemType>::Item = $value;
472                );
473            };
474        };
475        ($ident:ident, $($aux:ident)?, $path:path, generic, ($(#[$meta:meta])* $vis:vis static $ident_static:ident : $ty:ty = $value:expr;)) => {
476            $crate::__add_section_link_attribute!(
477                data section $ident $($aux)?
478                #[link_section = __]
479                $(#[$meta])* #[used] $vis static $ident_static: <$path as $crate::__support::SectionItemType>::Item = $value;
480            );
481        };
482        ($ident:ident, $($aux:ident)?, $path:path, no_generic, ($(#[$meta:meta])* $vis:vis fn $ident_fn:ident($($args:tt)*) $(-> $ret:ty)? $body:block)) => {
483            $crate::__add_section_link_attribute!(
484                data section $ident $($aux)?
485                #[link_section = __]
486                $(#[$meta])*
487                #[used]
488                #[allow(non_upper_case_globals)]
489                $vis static $ident_fn: fn($($args)*) $(-> $ret)? =
490                    {
491                        $crate::__section_name!(
492                            (#[link_section = __] fn $ident_fn($($args)*) $(-> $ret)? $body)
493                            code section $ident
494                        );
495                        $ident_fn
496                    };
497            );
498        };
499        ($ident:ident, $($aux:ident)?, $path:path, no_generic, ($(#[$meta:meta])* $item:item)) => {
500            $crate::__add_section_link_attribute!(
501                data section $ident $($aux)?
502                #[link_section = __]
503                $(#[$meta])* #[used] $item
504            );
505        };
506    }
507
508    pub trait SectionItemType {
509        type Item;
510    }
511
512    impl<T> SectionItemType for super::TypedSection<T> {
513        type Item = T;
514    }
515
516    /// On Apple platforms, the linker provides a pointer to the start and end
517    /// of the section regardless of the section's name.
518    #[cfg(all(not(target_vendor = "pc"), not(target_family = "wasm")))]
519    pub type SectionPtr<T> = *const ::core::marker::PhantomData<T>;
520    /// On LLVM/GCC/MSVC platforms, we cannot use start/end symbols for sections
521    /// without C-compatible names, so instead we drop a [T; 0] at the start and
522    /// end of the section.
523    #[cfg(target_vendor = "pc")]
524    pub type SectionPtr<T> = *const [T; 0];
525    /// On WASM, we use an atomic pointer to the start and end of the section.
526    #[cfg(target_family = "wasm")]
527    pub type SectionPtr<T> =
528        &'static ::core::sync::atomic::AtomicPtr<::core::marker::PhantomData<T>>;
529}
530
531/// Define a link section.
532///
533/// # Example
534/// ```rust
535/// use link_section::{in_section, section};
536///
537/// #[section]
538/// pub static DATA_SECTION: link_section::Section;
539///
540/// #[in_section(DATA_SECTION)]
541/// pub fn data_function() {
542///     println!("data_function");
543/// }
544/// ```
545#[cfg(feature = "proc_macro")]
546pub use ::link_section_proc_macro::section;
547
548/// Place an item into a link section.
549///
550/// # Functions and typed sections
551///
552/// As a special case, since function declarations by themselves are not sized,
553/// functions in typed sections are split and stored as function pointers.
554#[cfg(feature = "proc_macro")]
555pub use ::link_section_proc_macro::in_section;
556
557/// An untyped link section that can be used to store any type. The underlying
558/// data is not enumerable.
559#[repr(C)]
560pub struct Section {
561    name: &'static str,
562    start: __support::SectionPtr<()>,
563    end: __support::SectionPtr<()>,
564}
565
566impl Section {
567    #[doc(hidden)]
568    pub const unsafe fn new(
569        name: &'static str,
570        start: __support::SectionPtr<()>,
571        end: __support::SectionPtr<()>,
572    ) -> Self {
573        Self { name, start, end }
574    }
575}
576
577#[cfg(target_family = "wasm")]
578impl Section {
579    /// The start address of the section.
580    pub fn start_ptr(&self) -> *const () {
581        let ptr = self.start.load(::core::sync::atomic::Ordering::Relaxed) as *const ();
582        if ptr.is_null() {
583            panic!(
584                "Section {} was not initialized by the host environment",
585                self.name
586            );
587        }
588        ptr
589    }
590    /// The end address of the section.
591    pub fn end_ptr(&self) -> *const () {
592        let ptr = self.end.load(::core::sync::atomic::Ordering::Relaxed) as *const ();
593        if ptr.is_null() {
594            panic!(
595                "Section {} was not initialized by the host environment",
596                self.name
597            );
598        }
599        ptr
600    }
601    /// The byte length of the section.
602    pub fn byte_len(&self) -> usize {
603        unsafe { (self.end_ptr() as *const u8).offset_from(self.start_ptr() as *const u8) as usize }
604    }
605}
606
607#[cfg(not(target_family = "wasm"))]
608impl Section {
609    /// The start address of the section.
610    pub const fn start_ptr(&self) -> *const () {
611        self.start as *const ()
612    }
613    /// The end address of the section.
614    pub const fn end_ptr(&self) -> *const () {
615        self.end as *const ()
616    }
617    /// The byte length of the section.
618    pub const fn byte_len(&self) -> usize {
619        unsafe { (self.end_ptr() as *const u8).offset_from(self.start_ptr() as *const u8) as usize }
620    }
621}
622
623impl ::core::fmt::Debug for Section {
624    fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
625        f.debug_struct("Section")
626            .field("name", &self.name)
627            .field("start", &self.start_ptr())
628            .field("end", &self.end_ptr())
629            .field("byte_len", &self.byte_len())
630            .finish()
631    }
632}
633
634unsafe impl Sync for Section {}
635unsafe impl Send for Section {}
636
637/// A typed link section that can be used to store any sized type. The
638/// underlying data is enumerable.
639#[repr(C)]
640pub struct TypedSection<T: 'static> {
641    name: &'static str,
642    start: __support::SectionPtr<T>,
643    end: __support::SectionPtr<T>,
644    _phantom: ::core::marker::PhantomData<T>,
645}
646
647#[cfg(target_family = "wasm")]
648impl<T: 'static> TypedSection<T> {
649    /// The start address of the section.
650    pub fn start_ptr(&self) -> *const T {
651        let ptr = self.start.load(::core::sync::atomic::Ordering::Relaxed) as *const T;
652        if ptr.is_null() {
653            panic!(
654                "TypedSection {} was not initialized by the host environment",
655                self.name
656            );
657        }
658        ptr
659    }
660
661    /// The end address of the section.
662    pub fn end_ptr(&self) -> *const T {
663        let ptr = self.end.load(::core::sync::atomic::Ordering::Relaxed) as *const T;
664        if ptr.is_null() {
665            panic!(
666                "TypedSection {} was not initialized by the host environment",
667                self.name
668            );
669        }
670        ptr
671    }
672
673    /// The byte length of the section.
674    pub fn byte_len(&self) -> usize {
675        unsafe { (self.end_ptr() as *const u8).offset_from(self.start_ptr() as *const u8) as usize }
676    }
677
678    /// The number of elements in the section.
679    pub fn len(&self) -> usize {
680        self.byte_len() / self.stride()
681    }
682
683    /// True if the section is empty.
684    pub fn is_empty(&self) -> bool {
685        self.len() == 0
686    }
687
688    /// The section as a slice.
689    pub fn as_slice(&self) -> &[T] {
690        if self.is_empty() {
691            &[]
692        } else {
693            unsafe { ::core::slice::from_raw_parts(self.start_ptr(), self.len()) }
694        }
695    }
696}
697
698#[cfg(not(target_family = "wasm"))]
699impl<T: 'static> TypedSection<T> {
700    /// The start address of the section.
701    pub const fn start_ptr(&self) -> *const T {
702        self.start as *const T
703    }
704
705    /// The end address of the section.
706    pub const fn end_ptr(&self) -> *const T {
707        self.end as *const T
708    }
709
710    /// The byte length of the section.
711    pub const fn byte_len(&self) -> usize {
712        unsafe { (self.end_ptr() as *const u8).offset_from(self.start_ptr() as *const u8) as usize }
713    }
714
715    /// The number of elements in the section.
716    pub const fn len(&self) -> usize {
717        self.byte_len() / self.stride()
718    }
719
720    /// True if the section is empty.
721    pub const fn is_empty(&self) -> bool {
722        self.len() == 0
723    }
724
725    /// The section as a slice.
726    pub const fn as_slice(&self) -> &[T] {
727        if self.is_empty() {
728            &[]
729        } else {
730            unsafe { ::core::slice::from_raw_parts(self.start_ptr(), self.len()) }
731        }
732    }
733}
734
735// Non-const, shared functions (or functions that don't depend on the pointers)
736impl<T: 'static> TypedSection<T> {
737    #[doc(hidden)]
738    pub const unsafe fn new(
739        name: &'static str,
740        start: __support::SectionPtr<T>,
741        end: __support::SectionPtr<T>,
742    ) -> Self {
743        Self {
744            name,
745            start,
746            end,
747            _phantom: ::core::marker::PhantomData,
748        }
749    }
750
751    /// The stride of the typed section.
752    pub const fn stride(&self) -> usize {
753        assert!(
754            ::core::mem::size_of::<T>() > 0
755                && ::core::mem::size_of::<T>() * 2 == ::core::mem::size_of::<[T; 2]>()
756        );
757        ::core::mem::size_of::<T>()
758    }
759
760    /// The offset of the item in the section, if it is in the section.
761    pub fn offset_of(&self, item: &T) -> Option<usize> {
762        let ptr = item as *const T;
763        if ptr < self.start_ptr() || ptr >= self.end_ptr() {
764            None
765        } else {
766            Some(unsafe { ptr.offset_from(self.start_ptr()) as usize })
767        }
768    }
769
770    /// The section as a mutable slice.
771    ///
772    /// # Safety
773    ///
774    /// This cannot be safely used and is _absolutely unsound_ if any other
775    /// slices are live.
776    #[allow(clippy::mut_from_ref)]
777    pub unsafe fn as_mut_slice(&self) -> &mut [T] {
778        if self.is_empty() {
779            &mut []
780        } else {
781            unsafe { ::core::slice::from_raw_parts_mut(self.start_ptr() as *mut T, self.len()) }
782        }
783    }
784}
785
786impl<'a, T> ::core::iter::IntoIterator for &'a TypedSection<T> {
787    type Item = &'a T;
788    type IntoIter = ::core::slice::Iter<'a, T>;
789    fn into_iter(self) -> Self::IntoIter {
790        self.as_slice().iter()
791    }
792}
793
794impl<T> ::core::ops::Deref for TypedSection<T> {
795    type Target = [T];
796    fn deref(&self) -> &Self::Target {
797        self.as_slice()
798    }
799}
800
801impl<T> ::core::fmt::Debug for TypedSection<T> {
802    fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
803        f.debug_struct("TypedSection")
804            .field("name", &self.name)
805            .field("start", &self.start_ptr())
806            .field("end", &self.end_ptr())
807            .field("len", &self.len())
808            .field("stride", &self.stride())
809            .finish()
810    }
811}
812
813unsafe impl<T> Sync for TypedSection<T> where T: Sync {}
814unsafe impl<T> Send for TypedSection<T> where T: Send {}