Skip to main content

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/// A function pointerarray in the `data` section.
28#[section]
29pub static FN_ARRAY: link_section::TypedSection<fn()>;
30
31/// A function in the `FN_ARRAY` section.
32#[in_section(FN_ARRAY)]
33pub fn linked_function() {
34    eprintln!("linked_function");
35}
36
37/// Another function in the `FN_ARRAY` section.
38#[in_section(FN_ARRAY)]
39pub fn linked_function_2() {
40    eprintln!("linked_function_2");
41}
42
43/// Yet another function in the `FN_ARRAY` section.
44#[in_section(FN_ARRAY)]
45pub static OTHER_FN: fn() = link_section_function;
46
47/// A debuggable section in the `data` section.
48#[section]
49pub static DEBUGGABLES: link_section::TypedSection<&'static (dyn ::core::fmt::Debug + Sync)>;
50
51/// A debuggable in the `DEBUGGABLES` section.
52#[in_section(DEBUGGABLES)]
53pub static DEBUGGABLE: &'static (dyn ::core::fmt::Debug + Sync) = &1;
54
55/// Another debuggable in the `DEBUGGABLES` section.
56#[in_section(DEBUGGABLES)]
57pub static DEBUGGABLE_2: &'static (dyn ::core::fmt::Debug + Sync) = &2;
58
59/// A function pointer in the `DEBUGGABLES` section.
60#[in_section(DEBUGGABLES)]
61pub static DEBUGGABLE_FUNCTION: fn() = {
62    fn debuggable_function() {
63        eprintln!("debuggable_function");
64    }
65    &(debuggable_function as fn())
66};
67
68/// Dumps the various sections.
69pub fn main() {
70    eprintln!("LINK_SECTION: {:?}", LINK_SECTION);
71    link_section_function();
72    eprintln!("TYPED_LINK_SECTION: {:?}", TYPED_LINK_SECTION);
73    eprintln!("CODE_SECTION: {:?}", FN_ARRAY);
74    eprintln!("{:?}", FN_ARRAY.as_slice());
75    for f in FN_ARRAY {
76        eprintln!("f: {:?}", f);
77        f();
78    }
79    eprintln!("DEBUGGABLES: {:?}", DEBUGGABLES.as_slice());
80}