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
36procedure PASCALMAIN; external name 'PASCALMAIN';
37
38{******************************************************************************
39                          Process start/halt
40 ******************************************************************************}
41{$asmmode ATT}
42
43var
44  dlexitproc: pointer;
45
46procedure _FPC_proc_start; assembler; nostackframe; public name '_start';
47asm
48  { First locate the start of the environment variables }
49  popl    %ecx                    { Get argc in ecx }
50  movl    %esp,%ebx               { Esp now points to the arguments }
51  leal    4(%esp,%ecx,4),%eax     { The start of the environment is: esp+4*eax+4 }
52  andl    $0xfffffff8,%esp        { Align stack }
53
54  movl    %eax,operatingsystem_parameter_envp
55  movl    %ecx,operatingsystem_parameter_argc
56  movl    %ebx,operatingsystem_parameter_argv
57
58  movl    %edx, dlexitproc
59
60  fninit                           { initialize fpu }
61  fwait
62  fldcw   Default8087CW
63
64  { Initialize gs for thread local storage }
65  // movw    %ds,%ax
66  // movw    %ax,%gs
67
68  { Save initial stackpointer }
69  movl    %esp,initialstkptr
70
71  xorl    %ebp,%ebp
72  call    PASCALMAIN
73end;
74
75procedure _FPC_proc_haltproc; assembler; nostackframe; public name '_haltproc';
76asm
77.Lhaltproc:
78  movl    dlexitproc,%eax
79  testl   %eax,%eax
80  je      .Lnodlexitproc
81  call    *%eax
82.Lnodlexitproc:
83  movl    syscall_nr_exit_group,%eax
84{$if sizeof(ExitCode)=2}
85  movzwl  ExitCode,%ebx
86{$else}
87  mov     ExitCode,%ebx
88{$endif}
89  int     $0x80
90  movl    syscall_nr_exit,%eax
91{$if sizeof(ExitCode)=2}
92  movzwl  ExitCode,%ebx
93{$else}
94  mov     ExitCode,%ebx
95{$endif}
96  int     $0x80
97  jmp     .Lhaltproc
98end;
99
100