1{
2    This file is part of the Free Pascal run time library.
3    Copyright (c) 2005 by Michael Van Canneyt, Peter Vreman,
4    & Daniel Mantione, members of the Free Pascal development team.
5
6    See the file COPYING.FPC, included in this distribution,
7    for details about the copyright.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 **********************************************************************}
14
15{
16 Linux ELF startup code for Free Pascal
17
18
19 Stack layout at program start:
20
21         nil
22         envn
23         ....
24         ....           ENVIRONMENT VARIABLES
25         env1
26         env0
27         nil
28         argn
29         ....
30         ....           COMMAND LINE OPTIONS
31         arg1
32         arg0
33         argc <--- esp
34}
35
36var
37  gmon_etext: pointer; external name '_etext';
38  gmon_monstarted: longint = 0;
39
40procedure gmon_monstartup; external name 'monstartup';
41procedure gmon_mcleanup; external name '_mcleanup';
42
43procedure libc_atexit; external name '__libc_atexit';
44procedure libc_exit; external name '__libc_exit';
45procedure libc_init; external name '__libc_init';
46procedure libc_setfpucw; external name '__setfpucw';
47procedure libc_start_main; external name '__libc_start_main';
48
49{******************************************************************************
50                       Process + profiling start/halt
51 ******************************************************************************}
52{$asmmode ATT}
53
54procedure _FPC_proc_gprof_start; assembler; nostackframe; public name '_start';
55asm
56  { First locate the start of the environment variables }
57  popl    %ecx
58  movl    %esp,%ebx               { Points to the arguments }
59  movl    %ecx,%eax
60  incl    %eax
61  shll    $2,%eax
62  addl    %esp,%eax
63  andl    $0xfffffff8,%esp        { Align stack }
64
65  movl    %eax,operatingsystem_parameter_envp    { Move the environment pointer }
66  movl    %ecx,operatingsystem_parameter_argc    { Move the argument counter    }
67  movl    %ebx,operatingsystem_parameter_argv    { Move the argument pointer    }
68
69  finit                           { initialize fpu }
70  fwait
71  fldcw   Default8087CW
72
73  pushl   $gmon_etext                 { Initialize gmon }
74  pushl   $_FPC_proc_gprof_start
75  call    gmon_monstartup
76  addl    $8,%esp
77  pushl   $gmon_mcleanup
78  call    libc_atexit
79  addl    $4,%esp
80
81  { Save initial stackpointer }
82  movl    %esp,initialstkptr
83
84  xorl    %ebp,%ebp
85  call    PASCALMAIN
86end;
87
88procedure _FPC_proc_gprof_haltproc; assembler; nostackframe; public name '_haltproc';
89asm
90.Lhaltproc:
91 {$ifdef FPC_PIC}
92  call    fpc_geteipasebx
93  addl    $_GLOBAL_OFFSET_TABLE_,%ebx
94  movl    ExitCode@GOT(%ebx),%ebx
95 {$if sizeof(ExitCode)=2}
96  movzwl  (%ebx),%ebx
97 {$else}
98  mov     (%ebx),%ebx
99 {$endif}
100{$else FPC_PIC}
101 {$if sizeof(ExitCode)=2}
102  movzwl  ExitCode,%ebx
103 {$else}
104  mov     ExitCode,%ebx
105 {$endif}
106{$endif FPC_PIC}
107
108  pushl   %ebx
109  call    libc_exit     { call libc exit, this will  write the gmon.out }
110  movl    syscall_nr_exit_group,%eax
111  popl    %ebx
112  int     $0x80
113  jmp     .Lhaltproc
114end;
115
116