]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/i386/elf/start.S
update from main archive 970129
[thirdparty/glibc.git] / sysdeps / i386 / elf / start.S
1 /* Startup code compliant to the ELF i386 ABI.
2 Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
19
20 /* This is the canonical entry point, usually the first thing in the text
21 segment. The SVR4/i386 ABI (pages 3-31, 3-32) says that when the entry
22 point runs, most registers' values are unspecified, except for:
23
24 %edx Contains a function pointer to be registered with `atexit'.
25 This is how the dynamic linker arranges to have DT_FINI
26 functions called for shared libraries that have been loaded
27 before this code runs.
28
29 %esp The stack contains the arguments and environment:
30 0(%esp) argc
31 4(%esp) argv[0]
32 ...
33 (4*argc)(%esp) NULL
34 (4*(argc+1))(%esp) envp[0]
35 ...
36 NULL
37 */
38
39 .text
40 .globl _start
41 _start:
42 /* Clear the frame pointer. The ABI suggests this be done, to mark
43 the outermost frame obviously. */
44 xorl %ebp, %ebp
45
46 /* %edx contains the address of the shared library termination
47 function, which we will register with `atexit' to be called by
48 `exit'. I suspect that on some systems, and when statically
49 linked, this will not be set by anything to any function
50 pointer; hopefully it will be zero so we don't try to call
51 random pointers. */
52 testl %edx,%edx
53 jz nofini
54 pushl %edx
55 call atexit
56 popl %eax /* Pop value to unused register to remove
57 argument from stack. */
58 nofini:
59
60 /* Do essential libc initialization. In statically linked
61 programs under the GNU Hurd, this is what sets up the
62 arguments on the stack for the code below. */
63 call __libc_init_first
64
65 /* Extract the arguments and environment as encoded on the stack
66 and set up the arguments for `main': argc, argv, envp. */
67 popl %esi /* Pop the argument count. */
68 leal 4(%esp,%esi,4), %eax /* envp = &argv[argc + 1] */
69 movl %eax, _environ /* Store it in the global variable. */
70 movl %esp, %edx /* argv starts just at the current stack top.*/
71
72 /* Before pushing the arguments align the stack to a double word
73 boundary to avoid penalties from misaligned accesses. Thanks
74 to Edward Seidl <seidl@janed.com> for pointing this out. */
75 andl $0xfffffff8, %esp
76
77 pushl %eax /* Push third argument: envp. */
78 pushl %edx /* Push second argument: argv. */
79 pushl %esi /* Push first argument: argc. */
80
81 /* Call `_init', which is the entry point to our own `.init'
82 section; and register with `atexit' to have `exit' call
83 `_fini', which is the entry point to our own `.fini' section. */
84 call _init
85 pushl $_fini
86 call atexit
87 popl %eax
88
89 /* Call the user's main function, and exit with its value. */
90 call main
91 pushl %eax
92 call exit
93 hlt /* Crash if somehow `exit' does return. */
94
95 /* Define a symbol for the first piece of initialized data. */
96 .data
97 .globl __data_start
98 __data_start:
99 .long 0
100 .weak data_start
101 data_start = __data_start