1 | #include "../startup/swi.h" |
---|
2 | |
---|
3 | /* ANSI concatenation macros. */ |
---|
4 | #define CONCAT(a, b) CONCAT2(a, b) |
---|
5 | #define CONCAT2(a, b) a ## b |
---|
6 | |
---|
7 | #ifdef __USER_LABEL_PREFIX__ |
---|
8 | #define FUNCTION( name ) CONCAT (__USER_LABEL_PREFIX__, name) |
---|
9 | #else |
---|
10 | #error __USER_LABEL_PREFIX is not defined |
---|
11 | #endif |
---|
12 | |
---|
13 | /* .text is used instead of .section .text so it works with arm-aout too. */ |
---|
14 | .text |
---|
15 | .code 32 |
---|
16 | .align 0 |
---|
17 | |
---|
18 | .global _mainCRTStartup |
---|
19 | .global _start |
---|
20 | .global start |
---|
21 | start: |
---|
22 | _start: |
---|
23 | _mainCRTStartup: |
---|
24 | |
---|
25 | /* Start by setting up a stack */ |
---|
26 | #ifdef ARM_RDP_MONITOR |
---|
27 | /* Issue Demon SWI to read stack info */ |
---|
28 | swi SWI_GetEnv /* Returns command line in r0 */ |
---|
29 | mov sp,r1 /* and the highest memory address in r1 */ |
---|
30 | ldr sl, .LC2 /* stack limit is at end of data */ |
---|
31 | add sl, sl, #256 /* allow slop for stack overflow handling */ |
---|
32 | /* and small frames */ |
---|
33 | #else |
---|
34 | #ifdef ARM_RDI_MONITOR |
---|
35 | /* Issue Angel SWI to read stack info */ |
---|
36 | mov r0, #AngelSWI_Reason_HeapInfo |
---|
37 | adr r1, .LC0 /* point at ptr to 4 words to receive data */ |
---|
38 | swi AngelSWI_ARM /* We are always in ARM mode for startup */ |
---|
39 | ldr r0, .LC0 /* point at values read */ |
---|
40 | ldr sp, [r0, #8] |
---|
41 | ldr sl, [r0, #12] |
---|
42 | add sl, sl, #256 /* allow slop for stack overflow handling */ |
---|
43 | /* and small frames */ |
---|
44 | #else |
---|
45 | /* Set up the stack pointer to a fixed value */ |
---|
46 | ldr r3, .LC0 |
---|
47 | mov sp, r3 |
---|
48 | /* Setup a default stack-limit in-case the code has been |
---|
49 | compiled with "-mapcs-stack-check". Hard-wiring this value |
---|
50 | is not ideal, since there is currently no support for |
---|
51 | checking that the heap and stack have not collided, or that |
---|
52 | this default 64k is enough for the program being executed. |
---|
53 | However, it ensures that this simple crt0 world will not |
---|
54 | immediately cause an overflow event: */ |
---|
55 | sub sl, sp, #64 << 10 /* Still assumes 256bytes below sl */ |
---|
56 | #endif |
---|
57 | #endif |
---|
58 | mov a2, #0 /* Second arg: fill value */ |
---|
59 | mov fp, a2 /* Null frame pointer */ |
---|
60 | mov r7, a2 /* Null frame pointer for Thumb */ |
---|
61 | |
---|
62 | ldr a1, .LC1 /* First arg: start of memory block */ |
---|
63 | ldr a3, .LC2 |
---|
64 | sub a3, a3, a1 /* Third arg: length of block */ |
---|
65 | |
---|
66 | |
---|
67 | #ifdef __thumb__ /* Enter Thumb mode.... */ |
---|
68 | |
---|
69 | add a4, pc, #1 /* Get the address of the Thumb block */ |
---|
70 | bx a4 /* Go there and start Thumb decoding */ |
---|
71 | |
---|
72 | .code 16 |
---|
73 | .global __change_mode |
---|
74 | .thumb_func |
---|
75 | __change_mode: |
---|
76 | #endif |
---|
77 | |
---|
78 | bl FUNCTION (memset) |
---|
79 | #if !defined (ARM_RDP_MONITOR) && !defined (ARM_RDI_MONITOR) |
---|
80 | mov r0, #0 /* no arguments */ |
---|
81 | mov r1, #0 /* no argv either */ |
---|
82 | #else |
---|
83 | /* Need to set up standard file handles */ |
---|
84 | bl FUNCTION (initialize_monitor_handles) |
---|
85 | |
---|
86 | #ifdef ARM_RDP_MONITOR |
---|
87 | swi SWI_GetEnv /* sets r0 to point to the command line */ |
---|
88 | mov r1, r0 |
---|
89 | #else |
---|
90 | mov r0, #AngelSWI_Reason_GetCmdLine |
---|
91 | adr r1, .LC30 /* Space for command line */ |
---|
92 | swi AngelSWI |
---|
93 | ldr r1, .LC30 |
---|
94 | #endif |
---|
95 | /* Parse string at r1 */ |
---|
96 | mov r0, #0 /* count of arguments so far */ |
---|
97 | .LC10: |
---|
98 | /* Skip leading blanks */ |
---|
99 | #ifdef __thumb__ |
---|
100 | ldrb r3, [r1] |
---|
101 | add r1, #1 |
---|
102 | #else |
---|
103 | ldrb r3, [r1], #1 |
---|
104 | #endif |
---|
105 | cmp r3, #0 |
---|
106 | beq .LC12 |
---|
107 | cmp r3, #' ' |
---|
108 | beq .LC10 |
---|
109 | |
---|
110 | /* See whether we are scanning a string */ |
---|
111 | cmp r3, #'"' |
---|
112 | #ifdef __thumb__ |
---|
113 | beq .LC20 |
---|
114 | cmp r3, #'\'' |
---|
115 | bne .LC21 |
---|
116 | .LC20: |
---|
117 | mov r2, r3 |
---|
118 | b .LC22 |
---|
119 | |
---|
120 | .LC21: |
---|
121 | mov r2, #' ' /* terminator type */ |
---|
122 | sub r1, r1, #1 /* adjust back to point at start char */ |
---|
123 | .LC22: |
---|
124 | #else |
---|
125 | cmpne r3, #'\'' |
---|
126 | moveq r2, r3 |
---|
127 | movne r2, #' ' /* terminator type */ |
---|
128 | subne r1, r1, #1 /* adjust back to point at start char */ |
---|
129 | #endif |
---|
130 | |
---|
131 | /* Stack a pointer to the current argument */ |
---|
132 | #ifdef __thumb__ |
---|
133 | push {r1} |
---|
134 | #else |
---|
135 | stmfd sp!, {r1} |
---|
136 | #endif |
---|
137 | add r0, r0, #1 |
---|
138 | .LC11: |
---|
139 | #ifdef __thumb__ |
---|
140 | ldrb r3, [r1] |
---|
141 | add r1, #1 |
---|
142 | #else |
---|
143 | ldrb r3, [r1], #1 |
---|
144 | #endif |
---|
145 | cmp r3, #0 |
---|
146 | beq .LC12 |
---|
147 | cmp r2, r3 /* reached terminator? */ |
---|
148 | bne .LC11 |
---|
149 | mov r2, #0 |
---|
150 | sub r3, r1, #1 |
---|
151 | strb r2, [r3] /* terminate the arg string */ |
---|
152 | b .LC10 |
---|
153 | |
---|
154 | .LC12: |
---|
155 | mov r1, sp /* point at stacked arg pointers */ |
---|
156 | /* We've now got the stacked args in order reverse the */ |
---|
157 | #ifdef __thumb__ |
---|
158 | mov r2, r0 |
---|
159 | lsl r2, #2 |
---|
160 | add r2, sp |
---|
161 | mov r3, sp |
---|
162 | .LC15: cmp r2, r3 |
---|
163 | bls .LC14 |
---|
164 | sub r2, #4 |
---|
165 | ldr r4, [r2] |
---|
166 | ldr r5, [r3] |
---|
167 | str r5, [r2] |
---|
168 | str r4, [r3] |
---|
169 | add r3, #4 |
---|
170 | b .LC15 |
---|
171 | .LC14: |
---|
172 | #else |
---|
173 | add r2, sp, r0, LSL #2 /* End of args */ |
---|
174 | mov r3, sp /* Start of args */ |
---|
175 | .LC13: cmp r2, r3 |
---|
176 | ldrhi r4,[r2, #-4] /* Reverse ends of list */ |
---|
177 | ldrhi r5, [r3] |
---|
178 | strhi r5, [r2, #-4]! |
---|
179 | strhi r4, [r3], #4 |
---|
180 | bhi .LC13 |
---|
181 | #endif |
---|
182 | |
---|
183 | #endif |
---|
184 | bl FUNCTION (main) |
---|
185 | |
---|
186 | bl FUNCTION (exit) /* Should not return */ |
---|
187 | |
---|
188 | #ifdef __thumb__ |
---|
189 | /* Come out of Thumb mode... This code should be redundant... */ |
---|
190 | |
---|
191 | mov a4, pc |
---|
192 | bx a4 |
---|
193 | |
---|
194 | .code 32 |
---|
195 | .global change_back |
---|
196 | change_back: |
---|
197 | /* Halt the execution. This code should never be executed. */ |
---|
198 | /* |
---|
199 | ** With no debug monitor, this probably aborts (eventually). |
---|
200 | ** With a Demon debug monitor, this halts cleanly. |
---|
201 | ** With an Angel debug monitor, this will report 'Unknown SWI'. |
---|
202 | */ |
---|
203 | swi SWI_Exit |
---|
204 | #endif |
---|
205 | |
---|
206 | /* For Thumb, constants must be after the code since only |
---|
207 | positive offsets are supported for PC relative addresses. */ |
---|
208 | |
---|
209 | .align 0 |
---|
210 | .LC0: |
---|
211 | #ifdef ARM_RDI_MONITOR |
---|
212 | .word HeapBase |
---|
213 | #else |
---|
214 | #ifndef ARM_RDP_MONITOR |
---|
215 | #ifdef __pe__ |
---|
216 | .word 0x800000 |
---|
217 | #else |
---|
218 | /* .word 0x80000 */ /* Top of RAM on the PIE board */ |
---|
219 | #endif |
---|
220 | #endif |
---|
221 | #endif |
---|
222 | .LC1: |
---|
223 | .word _clear_start |
---|
224 | .LC2: |
---|
225 | .word _clear_end |
---|
226 | |
---|
227 | #ifdef ARM_RDI_MONITOR |
---|
228 | .LC30: .word CommandLine |
---|
229 | |
---|
230 | /* Workspace for Angel calls. */ |
---|
231 | .data |
---|
232 | /* Data returned by monitor SWI */ |
---|
233 | HeapBase: .word 0 |
---|
234 | HeapLimit: .word 0 |
---|
235 | StackBase: .word 0 |
---|
236 | StackLimit: .word 0 |
---|
237 | CommandLine: .space 256,0 /* Maximum length of 255 chars handled */ |
---|
238 | #endif |
---|
239 | |
---|
240 | #ifdef __pe__ |
---|
241 | .section .idata$3 |
---|
242 | .long 0,0,0,0,0,0,0,0 |
---|
243 | #endif |
---|
244 | |
---|