Debian Codegraph @

debian/rust-vendor/link-section/examples/example.rs

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