si_c21.inc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {
  12. Linux ELF startup code for Free Pascal
  13. Stack layout at program start:
  14. nil
  15. envn
  16. ....
  17. .... ENVIRONMENT VARIABLES
  18. env1
  19. env0
  20. nil
  21. argn
  22. ....
  23. .... COMMAND LINE OPTIONS
  24. arg1
  25. arg0
  26. argc <--- esp
  27. }
  28. {$asmmode att}
  29. procedure ini_dummy;
  30. begin
  31. end;
  32. procedure libc_start_main; external name '__libc_start_main';
  33. procedure libc_exit(code: longint); cdecl; external name 'exit';
  34. {******************************************************************************
  35. glibc 2.1 lib + profiling start/halt
  36. ******************************************************************************}
  37. procedure _FPC_libc21_start; assembler; nostackframe; public name '_start';
  38. asm
  39. xorl %ebp,%ebp
  40. { First locate the start of the environment variables }
  41. popl %ecx { Get argc in ecx }
  42. movl %esp,%esi { Esp now points to the arguments }
  43. leal 4(%esp,%ecx,4),%eax { The start of the environment is: esp+4*eax+4 }
  44. andl $0xfffffff0,%esp { Align stack to 16 bytes }
  45. {$ifdef FPC_PIC}
  46. call .Lpiclab
  47. .Lpiclab:
  48. popl %ebx
  49. addl $_GLOBAL_OFFSET_TABLE_+1,%ebx
  50. movl operatingsystem_parameter_envp@GOT(%ebx),%edi
  51. movl %eax,(%edi)
  52. movl operatingsystem_parameter_argc@GOT(%ebx),%edi
  53. movl %ecx,(%edi)
  54. movl operatingsystem_parameter_argv@GOT(%ebx),%edi
  55. movl %esi,(%edi)
  56. movl initialstkptr@GOT(%ebx),%edi
  57. movl %esp,(%edi)
  58. {$else FPC_PIC}
  59. movl %eax,operatingsystem_parameter_envp
  60. movl %ecx,operatingsystem_parameter_argc
  61. movl %esi,operatingsystem_parameter_argv
  62. movl %esp,initialstkptr
  63. {$endif FPC_PIC}
  64. { int __libc_start_main(
  65. int *(main) (int, char **, char **),
  66. int argc,
  67. AnsiChar * * ubp_av,
  68. void (*init) (void),
  69. void (*fini) (void),
  70. void (*rtld_fini) (void),
  71. void (* stack_end)); }
  72. pushl %ebp { padding }
  73. pushl %esp { stack_end }
  74. pushl %edx { function to be registered with
  75. atexit(), passed by loader }
  76. pushl $ini_dummy
  77. pushl $ini_dummy
  78. pushl %esi { Push second argument: argv. }
  79. pushl %ecx { Push first argument: argc. }
  80. pushl $PASCALMAIN
  81. call libc_start_main
  82. hlt
  83. end;
  84. procedure _FPC_libc21_haltproc(e: longint); cdecl; public name '_haltproc';
  85. begin
  86. libc_exit(e);
  87. { we should never return from libc_exit }
  88. asm
  89. hlt
  90. end;
  91. end;