start.inc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2009 by Pierre Muller,
  4. member of the Free Pascal development team.
  5. Program startup
  6. Adapted from code generated by gcc on opensolaris 2.11
  7. and sparc version in
  8. See the file COPYING.FPC, included in this distribution,
  9. for details about the copyright.
  10. This program 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.
  13. **********************************************************************}
  14. type
  15. TCdeclProcedure = procedure; cdecl;
  16. function atexit(proc:TCdeclProcedure):longint;cdecl;external 'c' name 'atexit'{ @plt };
  17. procedure C_exit;cdecl;external 'c' name 'exit';
  18. procedure _exit;cdecl;external 'c' name '_exit';
  19. //procedure _fini;cdecl;external 'c' name '_fini';
  20. //procedure __fpstart;cdecl;external 'c' name '__fpstart'{ @plt };
  21. //procedure __fsr;cdecl;external 'c' name '__fsr';
  22. //procedure _init;cdecl;external 'c' name '_init';
  23. procedure PascalMain;external name 'PASCALMAIN';
  24. {vars are not correctly transformed :(
  25. var
  26. _DYNAMIC : longint; cvar; external;
  27. __Argv : pointer; cvar; external;
  28. environ : pointer; cvar; external;
  29. __get_exit_frame_monitor_ptr : pointer; cvar; external;
  30. __do_exit_code_ptr : pointer; cvar; external;
  31. }
  32. var
  33. _environ : pointer; cvar; external;
  34. procedure _DYNAMIC;cdecl;external 'c' name '_DYNAMIC'; { should be weak }
  35. procedure __Argv;cdecl;external 'c' name '__Argv';
  36. //procedure environ;cdecl;external 'c' name 'environ';
  37. //procedure __get_exit_frame_monitor_ptr;cdecl;external 'c' name '__get_exit_frame_monitor_ptr';
  38. //procedure __do_exit_code_ptr;cdecl;external 'c' name '__do_exit_code_ptr';
  39. {
  40. (top-gdb) disas _start
  41. Dump of assembler code for function _start: }
  42. procedure _start;assembler;nostackframe;public name '_start';
  43. asm
  44. mov %esp,%eax
  45. {$ifdef FPC_PIC}
  46. call fpc_geteipasebx
  47. addl $_GLOBAL_OFFSET_TABLE_,%ebx
  48. movl StackTopPtr@GOT(%ebx),%ecx
  49. {$else FPC_PIC}
  50. mov $StackTopPtr,%ecx
  51. {$endif FPC_PIC}
  52. mov %eax,(%ecx)
  53. pushl $0x0
  54. pushl $0x0
  55. mov %esp,%ebp
  56. mov $_DYNAMIC,%eax
  57. test %eax,%eax
  58. je .Label1
  59. push %edx
  60. call atexit {@plt}
  61. add $0x4,%esp
  62. .Label1:
  63. (*
  64. push _fini
  65. call atexit {@plt}
  66. add $0x4,%esp
  67. lea __get_exit_frame_monitor_ptr,%eax
  68. mov (%eax),%eax
  69. test %eax,%eax
  70. je .Label2
  71. lea __do_exit_code_ptr,%eax
  72. mov (%eax),%eax
  73. test %eax,%eax
  74. je .Label2
  75. push %eax
  76. call atexit {@plt}
  77. add $0x4,%esp
  78. *)
  79. .Label2:
  80. mov 0x8(%ebp),%eax
  81. {$ifdef FPC_PIC}
  82. movl argc@GOT(%ebx),%ecx
  83. movl %eax,(%ecx)
  84. {$else FPC_PIC}
  85. mov %eax,argc
  86. {$endif FPC_PIC}
  87. {$ifdef FPC_PIC}
  88. movl _environ@GOT(%ebx),%ecx
  89. movl (%ecx),%edx
  90. {$else FPC_PIC}
  91. mov _environ,%edx
  92. {$endif FPC_PIC}
  93. test %edx,%edx
  94. jne .Label3
  95. lea 0x10(%ebp,%eax,4),%edx
  96. .Label3:
  97. {$ifdef FPC_PIC}
  98. movl _environ@GOT(%ebx),%ecx
  99. movl %edx,(%ecx)
  100. {$else FPC_PIC}
  101. movl %edx,_environ
  102. {$endif FPC_PIC}
  103. {$ifdef FPC_PIC}
  104. movl envp@GOT(%ebx),%ecx
  105. movl %edx,(%ecx)
  106. {$else FPC_PIC}
  107. movl %edx,envp
  108. {$endif FPC_PIC}
  109. and $0xfffffff0,%esp
  110. push %edx
  111. lea 0xc(%ebp),%edx
  112. {$ifdef FPC_PIC}
  113. movl argv@GOT(%ebx),%ecx
  114. movl %edx,(%ecx)
  115. {$else FPC_PIC}
  116. mov %edx,argv
  117. {$endif FPC_PIC}
  118. push %edx
  119. push %eax
  120. //call __fpstart {@plt}
  121. //call __fsr
  122. //call _init
  123. call PASCALMAIN {was <main> }
  124. add $0xc,%esp
  125. push %eax
  126. push %eax
  127. call C_exit { was exit@plt }
  128. add $0x4,%esp
  129. call _exit {@plt}
  130. add $0x4,%esp
  131. hlt
  132. end;