yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame^] | 1 | /* Startup code for ARM & ELF |
| 2 | Copyright (C) 1995, 1996, 1997, 1998, 2001, 2002, 2005 |
| 3 | Free Software Foundation, Inc. |
| 4 | This file is part of the GNU C Library. |
| 5 | |
| 6 | The GNU C Library is free software; you can redistribute it and/or |
| 7 | modify it under the terms of the GNU Lesser General Public |
| 8 | License as published by the Free Software Foundation; either |
| 9 | version 2.1 of the License, or (at your option) any later version. |
| 10 | |
| 11 | In addition to the permissions in the GNU Lesser General Public |
| 12 | License, the Free Software Foundation gives you unlimited |
| 13 | permission to link the compiled version of this file with other |
| 14 | programs, and to distribute those programs without any restriction |
| 15 | coming from the use of this file. (The GNU Lesser General Public |
| 16 | License restrictions do apply in other respects; for example, they |
| 17 | cover modification of the file, and distribution when not linked |
| 18 | into another program.) |
| 19 | |
| 20 | Note that people who make modified versions of this file are not |
| 21 | obligated to grant this special exception for their modified |
| 22 | versions; it is their choice whether to do so. The GNU Lesser |
| 23 | General Public License gives permission to release a modified |
| 24 | version without this exception; this exception also makes it |
| 25 | possible to release a modified version which carries forward this |
| 26 | exception. |
| 27 | |
| 28 | The GNU C Library is distributed in the hope that it will be useful, |
| 29 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 30 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 31 | Lesser General Public License for more details. |
| 32 | |
| 33 | You should have received a copy of the GNU Lesser General Public |
| 34 | License along with the GNU C Library; if not, write to the Free |
| 35 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 36 | 02111-1307 USA. */ |
| 37 | |
| 38 | /* This is the canonical entry point, usually the first thing in the text |
| 39 | segment. |
| 40 | |
| 41 | Note that the code in the .init section has already been run. |
| 42 | This includes _init and _libc_init |
| 43 | |
| 44 | |
| 45 | At this entry point, most registers' values are unspecified, except: |
| 46 | |
| 47 | a1 Contains a function pointer to be registered with `atexit'. |
| 48 | This is how the dynamic linker arranges to have DT_FINI |
| 49 | functions called for shared libraries that have been loaded |
| 50 | before this code runs. |
| 51 | |
| 52 | sp The stack contains the arguments and environment: |
| 53 | 0(sp) argc |
| 54 | 4(sp) argv[0] |
| 55 | ... |
| 56 | (4*argc)(sp) NULL |
| 57 | (4*(argc+1))(sp) envp[0] |
| 58 | ... |
| 59 | NULL |
| 60 | */ |
| 61 | /* |
| 62 | For uClinux it looks like this: |
| 63 | |
| 64 | argc argument counter (integer) |
| 65 | argv char *argv[] |
| 66 | envp char *envp[] |
| 67 | argv[0] program name (pointer) |
| 68 | argv[1...N] program args (pointers) |
| 69 | argv[argc-1] end of args (integer) |
| 70 | NULL |
| 71 | env[0...N] environment variables (pointers) |
| 72 | NULL |
| 73 | |
| 74 | ARM register quick reference: |
| 75 | |
| 76 | Name Number ARM Procedure Calling Standard Role |
| 77 | |
| 78 | a1 r0 argument 1 / integer result / scratch register / argc |
| 79 | a2 r1 argument 2 / scratch register / argv |
| 80 | a3 r2 argument 3 / scratch register / envp |
| 81 | a4 r3 argument 4 / scratch register |
| 82 | v1 r4 register variable |
| 83 | v2 r5 register variable |
| 84 | v3 r6 register variable |
| 85 | v4 r7 register variable |
| 86 | v5 r8 register variable |
| 87 | sb/v6 r9 static base / register variable |
| 88 | sl/v7 r10 stack limit / stack chunk handle / reg. variable |
| 89 | fp r11 frame pointer |
| 90 | ip r12 scratch register / new-sb in inter-link-unit calls |
| 91 | sp r13 lower end of current stack frame |
| 92 | lr r14 link address / scratch register |
| 93 | pc r15 program counter |
| 94 | */ |
| 95 | |
| 96 | #include <features.h> |
| 97 | #include <bits/arm_asm.h> |
| 98 | |
| 99 | .text |
| 100 | .globl _start |
| 101 | .type _start,%function |
| 102 | .type _init,%function |
| 103 | .type _fini,%function |
| 104 | #ifndef __UCLIBC_CTOR_DTOR__ |
| 105 | .weak _init |
| 106 | .weak _fini |
| 107 | #endif |
| 108 | |
| 109 | #if defined(THUMB1_ONLY) |
| 110 | .thumb_func |
| 111 | _start: |
| 112 | /* Clear the frame pointer since this is the outermost frame. */ |
| 113 | mov r3, #0 |
| 114 | mov fp, r3 |
| 115 | |
| 116 | #ifdef __ARCH_USE_MMU__ |
| 117 | /* Pop argc off the stack and save a pointer to argv */ |
| 118 | pop {a2} |
| 119 | mov a3, sp |
| 120 | #else |
| 121 | /* |
| 122 | * uClinux/arm stacks look a little different from normal |
| 123 | * MMU-full Linux/arm stacks (for no good reason) |
| 124 | */ |
| 125 | /* pull argc and argv off the stack. We are going to push 3 |
| 126 | * arguments, so pop one here to maintain doubleword alignment. */ |
| 127 | pop {a2} |
| 128 | ldr a3, [sp] |
| 129 | #endif |
| 130 | |
| 131 | /* Push stack limit and rtld_fini */ |
| 132 | push {a1, a3} |
| 133 | |
| 134 | #ifdef __PIC__ |
| 135 | ldr r4, .L_GOT |
| 136 | adr r5, .L_GOT |
| 137 | add r4, r5, r4 |
| 138 | |
| 139 | ldr r5, .L_GOT+4 /* _fini */ |
| 140 | ldr a1, [r4, r5] |
| 141 | push {a1} /* Push _fini */ |
| 142 | |
| 143 | ldr r5, .L_GOT+8 /* _init */ |
| 144 | ldr a4, [r4, r5] |
| 145 | |
| 146 | ldr r5, .L_GOT+12 /* main */ |
| 147 | ldr a1, [r4, r5] |
| 148 | |
| 149 | #else |
| 150 | /* Fetch address of fini */ |
| 151 | ldr r4, =_fini |
| 152 | /* Push fini */ |
| 153 | push {r4} |
| 154 | |
| 155 | /* Set up the other arguments in registers */ |
| 156 | ldr a1, =main |
| 157 | ldr a4, =_init |
| 158 | #endif |
| 159 | /* __uClibc_main (main, argc, argv, init, fini, rtld_fini, stack_end) */ |
| 160 | /* Let the libc call main and exit with its return code. */ |
| 161 | bl __uClibc_main |
| 162 | |
| 163 | /* should never get here....*/ |
| 164 | bl abort |
| 165 | .pool |
| 166 | |
| 167 | #ifdef __PIC__ |
| 168 | .L_GOT: |
| 169 | .word _GLOBAL_OFFSET_TABLE_-.L_GOT |
| 170 | .word _fini(GOT) |
| 171 | .word _init(GOT) |
| 172 | .word main(GOT) |
| 173 | #endif |
| 174 | #else /* !THUMB1_ONLY */ |
| 175 | _start: |
| 176 | /* Clear the frame pointer and link register since this is the outermost frame. */ |
| 177 | mov fp, #0 |
| 178 | mov lr, #0 |
| 179 | |
| 180 | #ifdef __ARCH_USE_MMU__ |
| 181 | /* Pop argc off the stack and save a pointer to argv */ |
| 182 | ldr a2, [sp], #4 |
| 183 | mov a3, sp |
| 184 | #else |
| 185 | /* |
| 186 | * uClinux/arm stacks look a little different from normal |
| 187 | * MMU-full Linux/arm stacks (for no good reason) |
| 188 | */ |
| 189 | /* pull argc and argv off the stack. We are going to push 3 |
| 190 | * arguments, so pop one here to maintain doubleword alignment. */ |
| 191 | ldr a2, [sp], #4 |
| 192 | ldr a3, [sp] |
| 193 | #endif |
| 194 | |
| 195 | /* Push stack limit */ |
| 196 | str a3, [sp, #-4]! |
| 197 | |
| 198 | /* Push rtld_fini */ |
| 199 | str a1, [sp, #-4]! |
| 200 | |
| 201 | #ifdef __PIC__ |
| 202 | ldr sl, .L_GOT |
| 203 | adr a4, .L_GOT |
| 204 | add sl, sl, a4 |
| 205 | |
| 206 | ldr ip, .L_GOT+4 /* _fini */ |
| 207 | ldr a1, [sl, ip] |
| 208 | str a1, [sp, #-4]! /* Push _fini */ |
| 209 | |
| 210 | ldr ip, .L_GOT+8 /* _init */ |
| 211 | ldr a4, [sl, ip] |
| 212 | |
| 213 | ldr ip, .L_GOT+12 /* main */ |
| 214 | ldr a1, [sl, ip] |
| 215 | |
| 216 | /* __uClibc_main (main, argc, argv, init, fini, rtld_fini, stack_end) */ |
| 217 | /* Let the libc call main and exit with its return code. */ |
| 218 | b __uClibc_main(PLT) |
| 219 | #else |
| 220 | /* Fetch address of fini */ |
| 221 | ldr ip, =_fini |
| 222 | /* Push fini */ |
| 223 | str ip, [sp, #-4]! |
| 224 | |
| 225 | /* Set up the other arguments in registers */ |
| 226 | ldr a1, =main |
| 227 | ldr a4, =_init |
| 228 | |
| 229 | /* __uClibc_main (main, argc, argv, init, fini, rtld_fini, stack_end) */ |
| 230 | |
| 231 | /* Let the libc call main and exit with its return code. */ |
| 232 | b __uClibc_main |
| 233 | #endif |
| 234 | |
| 235 | /* should never get here....*/ |
| 236 | bl abort |
| 237 | |
| 238 | #ifdef __PIC__ |
| 239 | .L_GOT: |
| 240 | .word _GLOBAL_OFFSET_TABLE_ - .L_GOT |
| 241 | .word _fini(GOT) |
| 242 | .word _init(GOT) |
| 243 | .word main(GOT) |
| 244 | #endif |
| 245 | #endif |
| 246 | |
| 247 | /* Define a symbol for the first piece of initialized data. */ |
| 248 | .data |
| 249 | .globl __data_start |
| 250 | __data_start: |
| 251 | .long 0 |
| 252 | .weak data_start |
| 253 | data_start = __data_start |