1 | /********************************************************************* |
---|
2 | * SEGGER Microcontroller GmbH * |
---|
3 | * The Embedded Experts * |
---|
4 | ********************************************************************** |
---|
5 | * * |
---|
6 | * (c) 2014 - 2024 SEGGER Microcontroller GmbH * |
---|
7 | * * |
---|
8 | * www.segger.com Support: support@segger.com * |
---|
9 | * * |
---|
10 | ********************************************************************** |
---|
11 | * * |
---|
12 | * All rights reserved. * |
---|
13 | * * |
---|
14 | * Redistribution and use in source and binary forms, with or * |
---|
15 | * without modification, are permitted provided that the following * |
---|
16 | * condition is met: * |
---|
17 | * * |
---|
18 | * - Redistributions of source code must retain the above copyright * |
---|
19 | * notice, this condition and the following disclaimer. * |
---|
20 | * * |
---|
21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND * |
---|
22 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, * |
---|
23 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * |
---|
24 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * |
---|
25 | * DISCLAIMED. IN NO EVENT SHALL SEGGER Microcontroller BE LIABLE FOR * |
---|
26 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * |
---|
27 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT * |
---|
28 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * |
---|
29 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * |
---|
30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * |
---|
31 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * |
---|
32 | * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * |
---|
33 | * DAMAGE. * |
---|
34 | * * |
---|
35 | ********************************************************************** |
---|
36 | |
---|
37 | -------------------------- END-OF-HEADER ----------------------------- |
---|
38 | |
---|
39 | File : SEGGER_THUMB_Startup.s |
---|
40 | Purpose : Generic runtime init startup code for ARM CPUs running |
---|
41 | in THUMB mode. |
---|
42 | Designed to work with the SEGGER linker to produce |
---|
43 | smallest possible executables. |
---|
44 | |
---|
45 | This file does not normally require any customization. |
---|
46 | |
---|
47 | Additional information: |
---|
48 | Preprocessor Definitions |
---|
49 | FULL_LIBRARY |
---|
50 | If defined then |
---|
51 | - argc, argv are set up by calling SEGGER_SEMIHOST_GetArgs(). |
---|
52 | - the exit symbol is defined and executes on return from main. |
---|
53 | - the exit symbol calls destructors, atexit functions and then |
---|
54 | calls SEGGER_SEMIHOST_Exit(). |
---|
55 | |
---|
56 | If not defined then |
---|
57 | - argc and argv are not valid (main is assumed to not take parameters) |
---|
58 | - the exit symbol is defined, executes on return from main and |
---|
59 | halts in a loop. |
---|
60 | */ |
---|
61 | |
---|
62 | .syntax unified |
---|
63 | |
---|
64 | /********************************************************************* |
---|
65 | * |
---|
66 | * Defines, configurable |
---|
67 | * |
---|
68 | ********************************************************************** |
---|
69 | */ |
---|
70 | |
---|
71 | #ifndef APP_ENTRY_POINT |
---|
72 | #define APP_ENTRY_POINT main |
---|
73 | #endif |
---|
74 | |
---|
75 | #ifndef ARGSSPACE |
---|
76 | #define ARGSSPACE 128 |
---|
77 | #endif |
---|
78 | |
---|
79 | /********************************************************************* |
---|
80 | * |
---|
81 | * Macros |
---|
82 | * |
---|
83 | ********************************************************************** |
---|
84 | */ |
---|
85 | // |
---|
86 | // Declare a label as function symbol (without switching sections) |
---|
87 | // |
---|
88 | .macro MARK_FUNC Name |
---|
89 | .global \Name |
---|
90 | .thumb_func |
---|
91 | .code 16 |
---|
92 | \Name: |
---|
93 | .endm |
---|
94 | // |
---|
95 | // Declare a regular function. |
---|
96 | // Functions from the startup are placed in the init section. |
---|
97 | // |
---|
98 | .macro START_FUNC Name |
---|
99 | .section .init.\Name, "ax" |
---|
100 | .global \Name |
---|
101 | .balign 2 |
---|
102 | .thumb_func |
---|
103 | .code 16 |
---|
104 | \Name: |
---|
105 | .endm |
---|
106 | |
---|
107 | // |
---|
108 | // Declare a weak function |
---|
109 | // |
---|
110 | .macro WEAK_FUNC Name |
---|
111 | .section .init.\Name, "ax", %progbits |
---|
112 | .weak \Name |
---|
113 | .balign 2 |
---|
114 | .thumb_func |
---|
115 | .code 16 |
---|
116 | \Name: |
---|
117 | .endm |
---|
118 | |
---|
119 | // |
---|
120 | // Mark the end of a function and calculate its size |
---|
121 | // |
---|
122 | .macro END_FUNC name |
---|
123 | .size \name,.-\name |
---|
124 | .endm |
---|
125 | |
---|
126 | /********************************************************************* |
---|
127 | * |
---|
128 | * Externals |
---|
129 | * |
---|
130 | ********************************************************************** |
---|
131 | */ |
---|
132 | .extern APP_ENTRY_POINT // typically main |
---|
133 | |
---|
134 | /********************************************************************* |
---|
135 | * |
---|
136 | * Global functions |
---|
137 | * |
---|
138 | ********************************************************************** |
---|
139 | */ |
---|
140 | /********************************************************************* |
---|
141 | * |
---|
142 | * _start |
---|
143 | * |
---|
144 | * Function description |
---|
145 | * Entry point for the startup code. |
---|
146 | * Usually called by the reset handler. |
---|
147 | * Performs all initialisation, based on the entries in the |
---|
148 | * linker-generated init table, then calls main(). |
---|
149 | * It is device independent, so there should not be any need for an |
---|
150 | * end-user to modify it. |
---|
151 | * |
---|
152 | * Additional information |
---|
153 | * At this point, the stack pointer should already have been |
---|
154 | * initialized |
---|
155 | * - by hardware (such as on Cortex-M), |
---|
156 | * - by the device-specific reset handler, |
---|
157 | * - or by the debugger (such as for RAM Code). |
---|
158 | */ |
---|
159 | #undef L |
---|
160 | #define L(label) .L_start_##label |
---|
161 | |
---|
162 | START_FUNC _start |
---|
163 | // |
---|
164 | // Call linker init functions which in turn performs the following: |
---|
165 | // * Perform segment init |
---|
166 | // * Perform heap init (if used) |
---|
167 | // * Call constructors of global Objects (if any exist) |
---|
168 | // |
---|
169 | ldr R4, =__SEGGER_init_table__ // Set table pointer to start of initialization table |
---|
170 | L(RunInit): |
---|
171 | ldr R0, [R4] // Get next initialization function from table |
---|
172 | adds R4, R4, #4 // Increment table pointer to point to function arguments |
---|
173 | blx R0 // Call initialization function |
---|
174 | b L(RunInit) |
---|
175 | // |
---|
176 | MARK_FUNC __SEGGER_init_done |
---|
177 | MARK_FUNC __startup_complete |
---|
178 | // |
---|
179 | // Time to call main(), the application entry point. |
---|
180 | // |
---|
181 | #ifndef FULL_LIBRARY |
---|
182 | // |
---|
183 | // In a real embedded application ("Free-standing environment"), |
---|
184 | // main() does not get any arguments, |
---|
185 | // which means it is not necessary to init R0 and R1. |
---|
186 | // |
---|
187 | bl APP_ENTRY_POINT // Call to application entry point (usually main()) |
---|
188 | |
---|
189 | END_FUNC _start |
---|
190 | // |
---|
191 | // end of _start |
---|
192 | // Fall-through to exit if main ever returns. |
---|
193 | // |
---|
194 | MARK_FUNC exit |
---|
195 | // |
---|
196 | // In a free-standing environment, if returned from application: |
---|
197 | // Loop forever. |
---|
198 | // |
---|
199 | b . |
---|
200 | .size exit,.-exit |
---|
201 | #else |
---|
202 | // |
---|
203 | // In a hosted environment, |
---|
204 | // we need to load R0 and R1 with argc and argv, in order to handle |
---|
205 | // the command line arguments. |
---|
206 | // This is required for some programs running under control of a |
---|
207 | // debugger, such as automated tests. |
---|
208 | // |
---|
209 | movs R0, #ARGSSPACE |
---|
210 | ldr R1, =__SEGGER_init_arg_data |
---|
211 | bl SEGGER_SEMIHOST_GetArgs |
---|
212 | ldr R1, =__SEGGER_init_arg_data |
---|
213 | bl APP_ENTRY_POINT // Call to application entry point (usually main()) |
---|
214 | bl exit // Call exit function |
---|
215 | b . // If we unexpectedly return from exit, hang. |
---|
216 | END_FUNC _start |
---|
217 | #endif |
---|
218 | // |
---|
219 | #ifdef FULL_LIBRARY |
---|
220 | /********************************************************************* |
---|
221 | * |
---|
222 | * exit |
---|
223 | * |
---|
224 | * Function description |
---|
225 | * Exit of the system. |
---|
226 | * Called on return from application entry point or explicit call |
---|
227 | * to exit. |
---|
228 | * |
---|
229 | * Additional information |
---|
230 | * In a hosted environment exit gracefully, by |
---|
231 | * saving the return value, |
---|
232 | * calling destructurs of global objects, |
---|
233 | * calling registered atexit functions, |
---|
234 | * and notifying the host/debugger. |
---|
235 | */ |
---|
236 | #undef L |
---|
237 | #define L(label) .L_exit_##label |
---|
238 | |
---|
239 | WEAK_FUNC exit |
---|
240 | mov R5, R0 // Save the exit parameter/return result |
---|
241 | // |
---|
242 | // Call destructors |
---|
243 | // |
---|
244 | ldr R0, =__dtors_start__ // Pointer to destructor list |
---|
245 | ldr R1, =__dtors_end__ |
---|
246 | L(Loop): |
---|
247 | cmp R0, R1 |
---|
248 | beq L(End) // Reached end of destructor list? => Done |
---|
249 | ldr R2, [R0] // Load current destructor address into R2 |
---|
250 | adds R0, R0, #4 // Increment pointer |
---|
251 | push {R0-R1} // Save R0 and R1 |
---|
252 | blx R2 // Call destructor |
---|
253 | pop {R0-R1} // Restore R0 and R1 |
---|
254 | b L(Loop) |
---|
255 | L(End): |
---|
256 | // |
---|
257 | // Call atexit functions |
---|
258 | // |
---|
259 | bl __SEGGER_RTL_execute_at_exit_fns |
---|
260 | // |
---|
261 | // Call debug_exit with return result/exit parameter |
---|
262 | // |
---|
263 | mov R0, R5 |
---|
264 | // |
---|
265 | // Entry points for _exit and _Exit, which terminate immediately. |
---|
266 | // Note: Destructors and registered atexit functions are not called. File descriptors are not closed. |
---|
267 | // |
---|
268 | MARK_FUNC _exit |
---|
269 | MARK_FUNC _Exit |
---|
270 | bl SEGGER_SEMIHOST_Exit |
---|
271 | // |
---|
272 | // If execution is not terminated, loop forever |
---|
273 | // |
---|
274 | L(ExitLoop): |
---|
275 | b L(ExitLoop) // Loop forever. |
---|
276 | END_FUNC exit |
---|
277 | #endif |
---|
278 | |
---|
279 | #ifdef FULL_LIBRARY |
---|
280 | .bss |
---|
281 | .balign 4 |
---|
282 | __SEGGER_init_arg_data: |
---|
283 | .space ARGSSPACE |
---|
284 | .size __SEGGER_init_arg_data, .-__SEGGER_init_arg_data |
---|
285 | .type __SEGGER_init_arg_data, %object |
---|
286 | #endif |
---|
287 | |
---|
288 | /*************************** End of file ****************************/ |
---|