OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-stable/] [newlib-1.18.0/] [libgloss/] [arm/] [crt0.S] - Blame information for rev 816

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 207 jeremybenn
#include "newlib.h"
2
#include "swi.h"
3
 
4
/* ANSI concatenation macros.  */
5
#define CONCAT(a, b) CONCAT2(a, b)
6
#define CONCAT2(a, b) a ## b
7
 
8
#ifdef __USER_LABEL_PREFIX__
9
#define FUNCTION( name ) CONCAT (__USER_LABEL_PREFIX__, name)
10
#else
11
#error __USER_LABEL_PREFIX is not defined
12
#endif
13
 
14
#ifdef HAVE_INITFINI_ARRAY
15
#define _init   __libc_init_array
16
#define _fini   __libc_fini_array
17
#endif
18
 
19
/* .text is used instead of .section .text so it works with arm-aout too.  */
20
        .text
21
#if defined(__thumb2__)
22
        .syntax unified
23
        .thumb
24
.macro FUNC_START name
25
        .global \name
26
        .thumb_func
27
\name:
28
.endm
29
#else
30
        .code 32
31
.macro FUNC_START name
32
        .global \name
33
\name:
34
.endm
35
#endif
36
        .align  0
37
 
38
        FUNC_START      _mainCRTStartup
39
        FUNC_START      _start
40
        FUNC_START      start
41
#if defined(__ELF__) && !defined(__USING_SJLJ_EXCEPTIONS__)
42
        /* Annotation for EABI unwinding tables.  */
43
        .fnstart
44
#endif
45
 
46
/* Start by setting up a stack */
47
#ifdef ARM_RDP_MONITOR
48
        /*  Issue Demon SWI to read stack info */
49
        swi     SWI_GetEnv      /*  Returns command line in r0 */
50
        mov     sp,r1           /*  and the highest memory address in r1 */
51
        ldr     sl, .LC2        /*  stack limit is at end of data */
52
        add     sl, sl, #256    /*  allow slop for stack overflow handling */
53
                                /*  and small frames */
54
#else
55
#ifdef ARM_RDI_MONITOR
56
        /*  Issue Angel SWI to read stack info */
57
        mov     r0, #AngelSWI_Reason_HeapInfo
58
        adr     r1, .LC0        /*  point at ptr to 4 words to receive data */
59
#if defined(__thumb2__)
60
        bkpt    AngelSWI
61
#else
62
        /*  We are always in ARM mode for startup */
63
        AngelSWIAsm     AngelSWI_ARM
64
#endif
65
        ldr     r0, .LC0        /*  point at values read */
66
        ldr     sp, [r0, #8]
67
        ldr     sl, [r0, #12]
68
        add     sl, sl, #256    /*  allow slop for stack overflow handling */
69
                                /*  and small frames */
70
#else
71
        /*  Set up the stack pointer to a fixed value */
72
        /*  Changes by toralf:
73
            - Allow linker script to provide stack via __stack symbol - see
74
              defintion of .Lstack
75
            - Provide "hooks" that may be used by the application to add
76
              custom init code - see .Lhwinit and .Lswinit
77
            - Go through all execution modes and set up stack for each of them.
78
              Loosely based on init.s from ARM/Motorola example code.
79
              Note: Mode switch via CPSR is not allowed once in non-privileged
80
                    mode, so we take care not to enter "User" to set up its sp,
81
                    and also skip most operations if already in that mode. */
82
 
83
        ldr     r3, .Lstack
84
        cmp     r3, #0
85
#ifdef __thumb2__
86
        it      eq
87
#endif
88
        ldreq   r3, .LC0
89
        /* Note: This 'mov' is essential when starting in User, and ensures we
90
                 always get *some* sp value for the initial mode, even if we
91
                 have somehow missed it below (in which case it gets the same
92
                 value as FIQ - not ideal, but better than nothing.) */
93
        mov     sp, r3
94
#ifdef __thumb2__
95
        /* XXX Fill in stack assignments for interrupt modes.  */
96
#else
97
        mrs     r2, CPSR
98
        tst     r2, #0x0F       /* Test mode bits - in User of all are 0 */
99
        beq     .LC23           /* "eq" means r2 AND #0x0F is 0 */
100
        msr     CPSR_c, #0xD1   /* FIRQ mode, interrupts disabled */
101
        mov     sp, r3
102
        sub     sl, sp, #0x1000 /* This mode also has its own sl (see below) */
103
 
104
        mov     r3, sl
105
        msr     CPSR_c, #0xD7   /* Abort mode, interrupts disabled */
106
        mov     sp, r3
107
        sub     r3, r3, #0x1000
108
 
109
        msr     CPSR_c, #0xDB   /* Undefined mode, interrupts disabled */
110
        mov     sp, r3
111
        sub     r3, r3, #0x1000
112
 
113
        msr     CPSR_c, #0xD2   /* IRQ mode, interrupts disabled */
114
        mov     sp, r3
115
        sub     r3, r3, #0x2000
116
 
117
        msr     CPSR_c, #0xD3   /* Supervisory mode, interrupts disabled */
118
 
119
        mov     sp, r3
120
        sub     r3, r3, #0x8000 /* Min size 32k */
121
        bic     r3, r3, #0x00FF /* Align with current 64k block */
122
        bic     r3, r3, #0xFF00
123
 
124
        str     r3, [r3, #-4]   /* Move value into user mode sp without */
125
        ldmdb   r3, {sp}^       /* changing modes, via '^' form of ldm */
126
        orr     r2, r2, #0xC0   /* Back to original mode, presumably SVC, */
127
        msr     CPSR_c, r2      /* with FIQ/IRQ disable bits forced to 1 */
128
#endif
129
.LC23:
130
        /* Setup a default stack-limit in-case the code has been
131
           compiled with "-mapcs-stack-check".  Hard-wiring this value
132
           is not ideal, since there is currently no support for
133
           checking that the heap and stack have not collided, or that
134
           this default 64k is enough for the program being executed.
135
           However, it ensures that this simple crt0 world will not
136
           immediately cause an overflow event:  */
137
        sub     sl, r3, #64 << 10       /* Still assumes 256bytes below sl */
138
#endif
139
#endif
140
        /* Zero the memory in the .bss section.  */
141
        mov     a2, #0                  /* Second arg: fill value */
142
        mov     fp, a2                  /* Null frame pointer */
143
        mov     r7, a2                  /* Null frame pointer for Thumb */
144
 
145
        ldr     a1, .LC1                /* First arg: start of memory block */
146
        ldr     a3, .LC2
147
        sub     a3, a3, a1              /* Third arg: length of block */
148
 
149
 
150
#if defined(__thumb__) && !defined(__thumb2__)
151
        /* Enter Thumb mode.... */
152
        add     a4, pc, #1      /* Get the address of the Thumb block */
153
        bx      a4              /* Go there and start Thumb decoding  */
154
 
155
        .code 16
156
        .global __change_mode
157
        .thumb_func
158
__change_mode:
159
#endif
160
 
161
        bl      FUNCTION (memset)
162
#if !defined (ARM_RDP_MONITOR) && !defined (ARM_RDI_MONITOR)
163
/* Changes by toralf: Taken from libgloss/m68k/crt0.S
164
 * initialize target specific stuff. Only execute these
165
 * functions it they exist.
166
 */
167
        ldr     r3, .Lhwinit
168
        cmp     r3, #0
169
        beq     .LC24
170
#if defined(__thumb__) || defined(__thumb2__)
171
        blx   r3
172
#else
173
        mov     lr, pc
174
        mov     pc, r3
175
#endif
176
.LC24:
177
        ldr     r3, .Lswinit
178
        cmp     r3, #0
179
        beq     .LC25
180
#if defined(__thumb__) || defined(__thumb2__)
181
        blx   r3
182
#else
183
        mov     lr, pc
184
        mov     pc, r3
185
#endif
186
 
187
.LC25:
188
        mov     r0, #0          /*  no arguments  */
189
        mov     r1, #0          /*  no argv either */
190
#else
191
        /* Need to set up standard file handles */
192
        bl      FUNCTION (initialise_monitor_handles)
193
 
194
#ifdef ARM_RDP_MONITOR
195
        swi     SWI_GetEnv      /*  sets r0 to point to the command line */
196
        mov     r1, r0
197
#else
198
        mov     r0, #AngelSWI_Reason_GetCmdLine
199
        adr     r1, .LC30       /*  Space for command line */
200
        AngelSWIAsm     AngelSWI
201
        ldr     r1, .LC30
202
#endif
203
        /*  Parse string at r1 */
204
        mov     r0, #0          /*  count of arguments so far */
205
        /* Push a NULL argument onto the end of the list.  */
206
#ifdef __thumb__
207
        push    {r0}
208
#else
209
        stmfd   sp!, {r0}
210
#endif
211
.LC10:
212
/*  Skip leading blanks */
213
#ifdef __thumb__
214
        ldrb    r3, [r1]
215
        add     r1, #1
216
#else
217
        ldrb    r3, [r1], #1
218
#endif
219
        cmp     r3, #0
220
        beq     .LC12
221
        cmp     r3, #' '
222
        beq     .LC10
223
 
224
/*  See whether we are scanning a string */
225
        cmp     r3, #'"'
226
#ifdef __thumb__
227
        beq     .LC20
228
        cmp     r3, #'\''
229
        bne     .LC21
230
.LC20:
231
        mov     r2, r3
232
        b       .LC22
233
 
234
.LC21:
235
        mov     r2, #' '        /*  terminator type */
236
        sub     r1, r1, #1      /*  adjust back to point at start char */
237
.LC22:
238
#else
239
        cmpne   r3, #'\''
240
        moveq   r2, r3
241
        movne   r2, #' '        /*  terminator type */
242
        subne   r1, r1, #1      /*  adjust back to point at start char */
243
#endif
244
 
245
/*  Stack a pointer to the current argument */
246
#ifdef __thumb__
247
        push    {r1}
248
#else
249
        stmfd   sp!, {r1}
250
#endif
251
        add     r0, r0, #1
252
.LC11:
253
#ifdef __thumb__
254
        ldrb    r3, [r1]
255
        add     r1, #1
256
#else
257
        ldrb    r3, [r1], #1
258
#endif
259
        cmp     r3, #0
260
        beq     .LC12
261
        cmp     r2, r3          /*  reached terminator? */
262
        bne     .LC11
263
        mov     r2, #0
264
        sub     r3, r1, #1
265
        strb    r2, [r3]        /*  terminate the arg string */
266
        b       .LC10
267
 
268
.LC12:
269
        mov     r1, sp          /*  point at stacked arg pointers */
270
        /* We've now got the stacked args in order reverse the */
271
#ifdef __thumb__
272
        mov     r2, r0
273
        lsl     r2, #2
274
        add     r2, sp
275
        mov     r3, sp
276
.LC15:  cmp     r2, r3
277
        bls     .LC14
278
        sub     r2, #4
279
        ldr     r4, [r2]
280
        ldr     r5, [r3]
281
        str     r5, [r2]
282
        str     r4, [r3]
283
        add     r3, #4
284
        b       .LC15
285
.LC14:
286
        /* Ensure doubleword stack alignment.  */
287
        mov     r4, sp
288
        mov     r5, #7
289
        bic     r4, r5
290
        mov     sp, r4
291
#else
292
        add     r2, sp, r0, LSL #2      /* End of args */
293
        mov     r3, sp                  /* Start of args */
294
.LC13:  cmp     r2, r3
295
        ldrhi   r4,[r2, #-4]            /* Reverse ends of list */
296
        ldrhi   r5, [r3]
297
        strhi   r5, [r2, #-4]!
298
        strhi   r4, [r3], #4
299
        bhi     .LC13
300
        /* Ensure doubleword stack alignment.  */
301
        bic     sp, sp, #7
302
#endif
303
#endif
304
 
305
#ifdef __USES_INITFINI__
306
        /* Some arm/elf targets use the .init and .fini sections
307
           to create constructors and destructors, and for these
308
           targets we need to call the _init function and arrange
309
           for _fini to be called at program exit.  */
310
        mov     r4, r0
311
        mov     r5, r1
312
        ldr     r0, .Lfini
313
        bl      FUNCTION (atexit)
314
        bl      FUNCTION (_init)
315
        mov     r0, r4
316
        mov     r1, r5
317
#endif
318
        bl      FUNCTION (main)
319
 
320
        bl      FUNCTION (exit)         /* Should not return.  */
321
 
322
#if defined(__thumb__) && !defined(__thumb2__)
323
        /* Come out of Thumb mode.  This code should be redundant.  */
324
 
325
        mov     a4, pc
326
        bx      a4
327
 
328
        .code 32
329
        .global change_back
330
change_back:
331
        /* Halt the execution.  This code should never be executed.  */
332
        /* With no debug monitor, this probably aborts (eventually).
333
           With a Demon debug monitor, this halts cleanly.
334
           With an Angel debug monitor, this will report 'Unknown SWI'.  */
335
        swi     SWI_Exit
336
#endif
337
 
338
        /* For Thumb, constants must be after the code since only
339
           positive offsets are supported for PC relative addresses.  */
340
 
341
        .align 0
342
.LC0:
343
#ifdef ARM_RDI_MONITOR
344
        .word   HeapBase
345
#else
346
#ifndef ARM_RDP_MONITOR
347
        /* Changes by toralf: Provide alternative "stack" variable whose value
348
           may be defined externally; .Lstack will be used instead of .LC0 if
349
           it points to a non-0 value. Also set up references to "hooks" that
350
           may be used by the application to provide additional init code. */
351
 
352
#ifdef __pe__
353
        .word   0x800000
354
#else
355
        .word   0x80000                 /* Top of RAM on the PIE board.  */
356
#endif
357
.Lstack:
358
        .word   __stack
359
.Lhwinit:
360
        .word   FUNCTION (hardware_init_hook)
361
.Lswinit:
362
        .word   FUNCTION (software_init_hook)
363
 
364
        /* Set up defaults for the above variables in the form of weak symbols
365
           - so that application will link correctly, and get value 0 in
366
           runtime (meaning "ignore setting") for the variables, when the user
367
           does not provide the symbols. (The linker uses a weak symbol if,
368
           and only if, a normal version of the same symbol isn't provided
369
           e.g. by a linker script or another object file.) */
370
 
371
        .weak __stack
372
        .weak FUNCTION (hardware_init_hook)
373
        .weak FUNCTION (software_init_hook)
374
#endif
375
 
376
#endif
377
#if defined(__ELF__) && !defined(__USING_SJLJ_EXCEPTIONS__)
378
        /* Protect against unhandled exceptions.  */
379
        .cantunwind
380
        .fnend
381
#endif
382
.LC1:
383
        .word   __bss_start__
384
.LC2:
385
        .word   __bss_end__
386
#ifdef __USES_INITFINI__
387
.Lfini:
388
        .word   FUNCTION(_fini)
389
#endif
390
#ifdef ARM_RDI_MONITOR
391
.LC30:
392
        .word   CommandLine
393
        .word   255
394
 
395
/*  Workspace for Angel calls.  */
396
        .data
397
/*  Data returned by monitor SWI.  */
398
.global __stack_base__
399
HeapBase:       .word   0
400
HeapLimit:      .word   0
401
__stack_base__: .word   0
402
StackLimit:     .word   0
403
CommandLine:    .space  256,0   /*  Maximum length of 255 chars handled.  */
404
#endif
405
 
406
#ifdef __pe__
407
        .section .idata$3
408
        .long   0,0,0,0,0,0,0,0
409
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.