naked_function/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//! This crate provides the [`naked`] proc macro.
#![no_std]

#[doc(inline)]
pub use naked_function_macro::naked;

// Helper macros to deal with platform-specific differences in assembly code
// between ELF, Mach-O and COFF file formats.
//
// We can't do this within the proc macro itself because Rust don't expose the
// target cfgs to proc macros.

cfg_if::cfg_if! {
    if #[cfg(all(target_arch = "arm", is_thumb))] {
        #[doc(hidden)]
        #[macro_export]
        macro_rules! __asm_arch_prefix {
            () => { ".thumb\n.thumb_func" };
            (arm::a32) => { ".arm" };
            (arm::t32) => { ".thumb\n.thumb_func" };
            ($isa:path) => { compile_error!("invalid instruction set") };
        }
        #[doc(hidden)]
        #[macro_export]
        macro_rules! __asm_arch_suffix {
            () => { ".thumb" };
        }
    } else if #[cfg(all(target_arch = "arm", not(is_thumb)))] {
        #[doc(hidden)]
        #[macro_export]
        macro_rules! __asm_arch_prefix {
            () => { ".arm" };
            (arm::a32) => { ".arm" };
            (arm::t32) => { ".thumb\n.thumb_func" };
            ($isa:path) => { compile_error!("invalid instruction set") };
        }
        #[doc(hidden)]
        #[macro_export]
        macro_rules! __asm_arch_suffix {
            () => { ".arm" };
        }
    } else {
        #[doc(hidden)]
        #[macro_export]
        macro_rules! __asm_arch_prefix {
            () => { "" };
            ($isa:path) => { compile_error!("invalid instruction set") };
        }
        #[doc(hidden)]
        #[macro_export]
        macro_rules! __asm_arch_suffix {
            () => { "" };
        }
    }
}
cfg_if::cfg_if! {
    if #[cfg(any(
            target_vendor = "apple",
            all(windows, target_arch = "x86"),
        ))] {
        #[doc(hidden)]
        #[macro_export]
        macro_rules! __asm_mangle {
            ($symbol:expr) => { concat!("_", $symbol) };
        }
    } else {
        #[doc(hidden)]
        #[macro_export]
        macro_rules! __asm_mangle {
            ($symbol:expr) => { $symbol };
        }
    }
}
cfg_if::cfg_if! {
    if #[cfg(windows)] {
        // COFF
        #[doc(hidden)]
        #[macro_export]
        macro_rules! __asm_function_begin {
            ($symbol:expr, $section:expr, ($($instruction_set:tt)*)) => {
                concat!(
                    ".pushsection ", $section, ",\"xr\"\n",
                    ".balign 4\n",
                    ".globl ", $crate::__asm_mangle!($symbol), "\n",
                    ".def ", $crate::__asm_mangle!($symbol), "\n",
                    ".scl 2\n",
                    ".type 32\n",
                    ".endef ", $crate::__asm_mangle!($symbol), "\n",
                    $crate::__asm_arch_prefix!($($instruction_set)*), "\n",
                    $crate::__asm_mangle!($symbol), ":\n",
                )
            };
        }
        #[doc(hidden)]
        #[macro_export]
        macro_rules! __asm_function_end {
            ($symbol:expr) => {
                concat!(
                    ".popsection\n",
                    $crate::__asm_arch_suffix!(), "\n",
                )
            };
        }
        #[doc(hidden)]
        #[macro_export]
        macro_rules! __asm_default_section {
            ($symbol:expr) => { concat!(".text.", $symbol) };
        }
    } else if #[cfg(target_vendor = "apple")] {
        // Mach-O
        #[doc(hidden)]
        #[macro_export]
        macro_rules! __asm_function_begin {
            ($symbol:expr, $section:expr, ($($instruction_set:tt)*)) => {
                concat!(
                    ".pushsection ", $section, ",regular,pure_instructions\n",
                    ".balign 4\n",
                    ".globl ", $crate::__asm_mangle!($symbol), "\n",
                    ".private_extern ", $crate::__asm_mangle!($symbol), "\n",
                    $crate::__asm_arch_prefix!($($instruction_set)*), "\n",
                    $crate::__asm_mangle!($symbol), ":\n",
                )
            };
        }
        #[doc(hidden)]
        #[macro_export]
        macro_rules! __asm_function_end {
            ($symbol:expr) => {
                concat!(
                    ".popsection\n",
                    $crate::__asm_arch_suffix!(), "\n",
                )
            };
        }
        #[doc(hidden)]
        #[macro_export]
        macro_rules! __asm_default_section {
            ($symbol:expr) => { "__TEXT,__text" };
        }
    } else {
        // Everything else uses ELF. ARM uses % instead of @ for some
        // assembler directives.
        #[cfg(not(target_arch = "arm"))]
        #[doc(hidden)]
        #[macro_export]
        macro_rules! __asm_type {
            ($ty:literal) => { concat!("@", $ty) }
        }
        #[cfg(target_arch = "arm")]
        #[doc(hidden)]
        #[macro_export]
        macro_rules! __asm_type {
            ($ty:literal) => { concat!("%", $ty) }
        }
        #[doc(hidden)]
        #[macro_export]
        macro_rules! __asm_function_begin {
            ($symbol:expr, $section:expr, ($($instruction_set:tt)*)) => {
                concat!(
                    ".pushsection ", $section, ",\"ax\", ", $crate::__asm_type!("progbits"), "\n",
                    ".balign 4\n",
                    ".globl ", $crate::__asm_mangle!($symbol), "\n",
                    ".hidden ", $crate::__asm_mangle!($symbol), "\n",
                    ".type ", $crate::__asm_mangle!($symbol), ", ", $crate::__asm_type!("function"), "\n",
                    $crate::__asm_arch_prefix!($($instruction_set)*), "\n",
                    $crate::__asm_mangle!($symbol), ":\n",
                )
            };
        }
        #[doc(hidden)]
        #[macro_export]
        macro_rules! __asm_function_end {
            ($symbol:expr) => {
                concat!(
                    ".size ", $crate::__asm_mangle!($symbol), ", . - ", $crate::__asm_mangle!($symbol), "\n",
                    ".popsection\n",
                    $crate::__asm_arch_suffix!(), "\n",
                )
            };
        }
        #[doc(hidden)]
        #[macro_export]
        macro_rules! __asm_default_section {
            ($symbol:expr) => { concat!(".text.", $symbol) };
        }
    }
}