si_uc.inc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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/uClibc startup code for Free Pascal
  13. taken from uClibc/sysdeps/linux/i386/crt1.S
  14. %edx Contains a function pointer to be registered with `atexit'.
  15. This is how the dynamic linker arranges to have DT_FINI
  16. functions called for shared libraries that have been loaded
  17. before this code runs.
  18. Stack layout at program start:
  19. nil
  20. envn
  21. ....
  22. .... ENVIRONMENT VARIABLES
  23. env1
  24. env0
  25. nil
  26. argn
  27. ....
  28. .... COMMAND LINE OPTIONS
  29. arg1
  30. arg0
  31. argc <--- esp
  32. }
  33. var
  34. dlexitproc: pointer; { atexit from loader }
  35. procedure uclibc_init; external name '__uClibc_init';
  36. procedure uclibc_fini; external name '__uClibc_fini';
  37. procedure uclibc_exit; external name 'exit';
  38. procedure uclibc_main; external name '__uClibc_main';
  39. { Some helpers }
  40. {$ifdef FPC_PIC}
  41. function get3eipasebx : pointer; compilerproc; nostackframe; assembler;
  42. asm
  43. movl (%esp),%ebx
  44. ret
  45. end;
  46. {$endif FPC_PIC}
  47. procedure _init_fini_dummy; compilerproc; nostackframe; assembler;
  48. asm
  49. ret
  50. end;
  51. {******************************************************************************
  52. glibc 2.1 lib + profiling start/halt
  53. ******************************************************************************}
  54. {$asmmode ATT}
  55. procedure _FPC_libc21_start; assembler; nostackframe; public name '_start';
  56. asm
  57. xorl %ebp,%ebp
  58. { First locate the start of the environment variables }
  59. popl %ecx { Get argc in ecx }
  60. movl %esp,%ebx { Esp now points to the arguments }
  61. leal 4(%esp,%ecx,4),%eax { The start of the environment is: esp+4*eax+4 }
  62. andl $0xfffffff8,%esp { Align stack }
  63. {$ifdef FPC_PIC}
  64. pushl %edx
  65. pushl %ebx
  66. pushl %ecx
  67. call get3eipasebx
  68. addl $_GLOBAL_OFFSET_TABLE_,%ebx
  69. movl dlexitproc@GOT(%ebx),%ecx
  70. movl %edx,(%ecx)
  71. movl operatingsystem_parameter_envp@GOT(%ebx),%ecx
  72. movl %eax,(%ecx)
  73. movl operatingsystem_parameter_argc@GOT(%ebx),%edx
  74. popl %ecx
  75. movl %ecx,(%edx)
  76. movl operatingsystem_parameter_argv@GOT(%ebx),%edx
  77. popl %ebx
  78. movl %ebx,(%edx)
  79. popl %edx
  80. {$else FPC_PIC}
  81. movl %edx, dlexitproc
  82. movl %eax,operatingsystem_parameter_envp
  83. movl %ecx,operatingsystem_parameter_argc
  84. movl %ebx,operatingsystem_parameter_argv
  85. {$endif FPC_PIC}
  86. { Save initial stackpointer }
  87. {$ifdef FPC_PIC}
  88. pushl %ebx
  89. call get3eipasebx
  90. addl $_GLOBAL_OFFSET_TABLE_,%ebx
  91. movl initialstkptr@GOT(%ebx),%ebx
  92. movl %esp,(%ebx)
  93. popl %ebx
  94. {$else FPC_PIC}
  95. movl %esp,initialstkptr
  96. {$endif FPC_PIC}
  97. { int __libc_start_main(
  98. int *(main) (int, char **, char **),
  99. int argc,
  100. char ** ubp_av,
  101. void (*init) (void),
  102. void (*fini) (void),
  103. void (*rtld_fini) (void),
  104. void (* stack_end)); }
  105. pushl %ebp { padding }
  106. pushl %esp { stack_end }
  107. pushl %edx { function to be registered with
  108. atexit(), passed by loader }
  109. pushl $_init_fini_dummy
  110. pushl $_init_fini_dummy
  111. pushl %ebx { Push second argument: argv. }
  112. pushl %ecx { Push first argument: argc. }
  113. pushl $PASCALMAIN
  114. call uclibc_main
  115. hlt
  116. end;
  117. procedure _FPC_libc21_haltproc(e: longint); assembler; public name '_haltproc';
  118. asm
  119. .Lhaltproc:
  120. movl e,%ebx
  121. pushl %ebx
  122. call uclibc_exit
  123. xorl %eax,%eax
  124. incl %eax { eax=1, exit call }
  125. popl %ebx
  126. int $0x80
  127. jmp .Lhaltproc
  128. end;