Skip to main content

link_section_example/
example.rs

1//! Example usage of the `link-section` crate.
2
3use link_section::{in_section, section};
4
5/// An untyped link section with `code` linkage.
6#[section]
7pub static LINK_SECTION: link_section::Section;
8
9/// A function in the `LINK_SECTION` section.
10#[in_section(LINK_SECTION)]
11pub fn link_section_function() {
12    println!("link_section_function");
13}
14
15/// A typed link section with `data` linkage.
16#[section]
17pub static TYPED_LINK_SECTION: link_section::TypedSection<u32>;
18
19/// A `u32` in the `TYPED_LINK_SECTION` section.
20#[in_section(TYPED_LINK_SECTION)]
21pub static LINKED_U32: u32 = 1;
22
23/// Another `u32` in the `TYPED_LINK_SECTION` section.
24#[in_section(TYPED_LINK_SECTION)]
25pub static LINKED_U32_2: u32 = 2;
26
27/// Create an aux link section for `TYPED_LINK_SECTION`.
28#[section(aux = TYPED_LINK_SECTION)]
29pub static AUX_LINK_SECTION: link_section::TypedSection<u32>;
30
31/// An auxiliary section item.
32#[in_section(AUX_LINK_SECTION)]
33pub static AUX_LINKED_U32: u32 = 3;
34
35/// A function pointerarray in the `data` section.
36#[section]
37pub static FN_ARRAY: link_section::TypedSection<fn()>;
38
39/// A function in the `FN_ARRAY` section.
40#[in_section(FN_ARRAY)]
41pub fn linked_function() {
42    eprintln!("linked_function");
43}
44
45/// Another function in the `FN_ARRAY` section.
46#[in_section(FN_ARRAY)]
47pub fn linked_function_2() {
48    eprintln!("linked_function_2");
49}
50
51/// Yet another function in the `FN_ARRAY` section.
52#[in_section(FN_ARRAY)]
53pub static OTHER_FN: fn() = link_section_function;
54
55/// A debuggable section in the `data` section.
56#[section]
57pub static DEBUGGABLES: link_section::TypedSection<&'static (dyn ::core::fmt::Debug + Sync)>;
58
59/// A debuggable in the `DEBUGGABLES` section.
60#[in_section(DEBUGGABLES)]
61pub static DEBUGGABLE: &'static (dyn ::core::fmt::Debug + Sync) = &1;
62
63/// Another debuggable in the `DEBUGGABLES` section.
64#[in_section(DEBUGGABLES)]
65pub static DEBUGGABLE_2: &'static (dyn ::core::fmt::Debug + Sync) = &2;
66
67/// A function pointer in the `DEBUGGABLES` section.
68#[in_section(DEBUGGABLES)]
69pub static DEBUGGABLE_FUNCTION: fn() = {
70    fn debuggable_function() {
71        eprintln!("debuggable_function");
72    }
73    &(debuggable_function as fn())
74};
75
76/// Dumps the various sections.
77pub fn main() {
78    eprintln!("LINK_SECTION: {:?}", LINK_SECTION);
79    link_section_function();
80    eprintln!("TYPED_LINK_SECTION: {:?}", TYPED_LINK_SECTION);
81    assert!(TYPED_LINK_SECTION.offset_of(&LINKED_U32).is_some());
82    assert!(TYPED_LINK_SECTION.offset_of(&LINKED_U32_2).is_some());
83    eprintln!("AUX_LINK_SECTION: {:?}", AUX_LINK_SECTION);
84    assert!(AUX_LINK_SECTION.len() == 1);
85    let random_u32 = 1234567890;
86    assert!(TYPED_LINK_SECTION.offset_of(&random_u32).is_none());
87    eprintln!("CODE_SECTION: {:?}", FN_ARRAY);
88    eprintln!("{:?}", FN_ARRAY.as_slice());
89    for f in FN_ARRAY {
90        eprintln!("f: {:?}", f);
91        f();
92        assert!(FN_ARRAY.offset_of(f).is_some());
93    }
94    eprintln!("DEBUGGABLES: {:?}", DEBUGGABLES.as_slice());
95}