Update.
[glibc.git] / sysdeps / i386 / elf / start.S
blob7416c0ace9cb6f05a06ab92bca23468132fff28a
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.
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.
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.
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.  */
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:
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.
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
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
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 .Lnofini
54         pushl %edx
55         call atexit
56         popl %eax               /* Pop value to unused register to remove
57                                    argument from stack.  */
58 .Lnofini:
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
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.*/
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         pushl %eax              /* Push garbage because we allocate
77                                    twelve more bytes.  */
79         pushl %eax              /* Push third argument: envp.  */
80         pushl %edx              /* Push second argument: argv.  */
81         pushl %esi              /* Push first argument: argc.  */
83         /* Call `_init', which is the entry point to our own `.init'
84            section; and register with `atexit' to have `exit' call
85            `_fini', which is the entry point to our own `.fini' section.  */
86         call _init
87         pushl $_fini
88         call atexit
89         popl %eax
91         /* Call the user's main function, and exit with its value.  */
92         call main
93         pushl %eax
94         call exit
95         hlt                     /* Crash if somehow `exit' does return.  */
97 /* To fulfill the System V/i386 ABI we need this symbol.  Yuck, it's so
98    meaningless since we don't support machines < 80386.  */
99         .section .rodata
100         .globl _fp_hw
101         .long 3
102         .size _fp_hw, 4
104 /* Define a symbol for the first piece of initialized data.  */
105         .data
106         .globl __data_start
107 __data_start:
108         .long 0
109         .weak data_start
110         data_start = __data_start