si_g.inc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (C) 2022 Loongson Technology Corporation Limited.
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************}
  10. {******************************************************************************
  11. Process start/halt
  12. ******************************************************************************}
  13. {
  14. The entry point's job is to call __libc_start_main. Per the ABI,
  15. a0 contains the address of a function to be passed to atexit.
  16. __libc_start_main wants this in a5.
  17. int __libc_start_main (
  18. int (*main) (int, char **, char **),
  19. int argc,
  20. char **argv,
  21. __typeof (main) init,
  22. void (*fini) (void),
  23. void (*rtld_fini) (void),
  24. void *stack_end);
  25. }
  26. var
  27. libc_environ: pchar; external name '__environ';
  28. libc_fpu_control: word; external name '__fpu_control';
  29. libc_init_proc: procedure; external name '_init';
  30. libc_fini_proc: procedure; external name '_fini';
  31. _etext: pointer; external name '_etext';
  32. fpc_ret_ra,fpc_ret_sp : pointer;
  33. procedure libc_atexit; external name 'atexit';
  34. procedure libc_exit; external name '__libc_exit';
  35. procedure libc_init; external name '__libc_init';
  36. procedure libc_setfpucw; external name '__setfpucw';
  37. procedure libc_start_main; external name '__libc_start_main';
  38. procedure gmon_monstartup; external name 'monstartup';
  39. procedure gmon_mcleanup; external name '_mcleanup';
  40. procedure _FPC_libc_start; forward;
  41. procedure _FPC_libc_haltproc(e:longint); forward;
  42. procedure main_stub; assembler; nostackframe;
  43. asm
  44. { let fp = sp in case of no fp value }
  45. ori $fp, $sp, 0
  46. { save ra and sp }
  47. la.pcrel $t0, fpc_ret_ra
  48. st.d $ra, $t0, 0
  49. la.pcrel $t1, fpc_ret_sp
  50. st.d $sp, $t1, 0
  51. { call gmon_monstartup(&_FPC_libc_start, &_etext) }
  52. la.got $a0, _FPC_libc_start
  53. la.got $a1, _etext
  54. la.got $ra, gmon_monstartup
  55. jirl $ra, $ra, 0
  56. { call libc_atexit(&gmon_mcleanup) }
  57. la.got $a0, gmon_mcleanup
  58. la.got $ra, libc_atexit
  59. jirl $ra, $ra, 0
  60. {$ifdef FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  61. la.got, $t0, SysInitEntryInformation
  62. st.d $sp, $t0, TEntryInformation.OS.stkptr
  63. la.got $t1, StackLength
  64. ld.d $t1, $t1, 0
  65. st.d $t1, $t0, TEntryInformation.OS.stklen
  66. la.got $t2, _FPC_libc_haltproc
  67. st.d $t2, $t0, TEntryInformation.OS.haltproc
  68. move $a0, $t0
  69. bl %plt(SysEntry)
  70. {$else}
  71. { save stack pointer }
  72. la.got $t1, initialstkptr
  73. st.d $sp, $t1, 0
  74. { call PascalMain }
  75. bl %plt(PASCALMAIN)
  76. {$endif}
  77. break 1
  78. end;
  79. procedure ini_dummy;
  80. begin
  81. end;
  82. {******************************************************************************
  83. C library start/halt
  84. ******************************************************************************}
  85. procedure _FPC_libc_start; assembler; nostackframe; public name '_start';
  86. asm
  87. { clear frame pointer }
  88. ori $fp, $zero, 0
  89. {$ifdef FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  90. la.got $t1, SysInitEntryInformation
  91. { argc = *(int *)sp }
  92. ld.w $a1, $sp, 0
  93. st.w $a1, $t1, TEntryInformation.OS.argc
  94. { argv = (char **)(sp + 8) }
  95. addi.d $a2, $sp, 8
  96. st.d $a2, $t1, TEntryInformation.OS.argv
  97. { save envp }
  98. alsl.d $t0, $a1, $a2, 3
  99. addi.d $t0, $t0, 8
  100. st.d $t0, $t1, TEntryInformation.OS.envp
  101. {$else}
  102. { argc = *(int *)sp }
  103. ld.w $a1, $sp, 0
  104. { save operatingsystem parameter argc }
  105. la.got $t0, operatingsystem_parameter_argc
  106. st.w $a1, $t0, 0
  107. { argv = (char **)(sp + 8) }
  108. addi.d $a2, $sp, 8
  109. { save operatingsystem parameter argv }
  110. la.got $t0, operatingsystem_parameter_argv
  111. st.d $a2, $t0, 0
  112. { save operatingsystem parameter envp }
  113. la.got $t0, operatingsystem_parameter_envp
  114. alsl.d $t1, $a1, $a2, 3
  115. addi.d $t1, $t1, 8
  116. st.d $t1, $t0, 0
  117. {$endif}
  118. { adjust $sp for 16-aligned }
  119. bstrins.d $sp, $zero, 3, 0
  120. { call libc_start_main (&main_stub, argc, argv, dummy, dummy, rtld_fini, stkend ) }
  121. la.got $a3, ini_dummy
  122. la.got $a4, ini_dummy
  123. ori $a5, $a0, 0
  124. ori $a6, $sp, 0
  125. la.pcrel $a0, main_stub
  126. bl %plt(libc_start_main)
  127. break 1
  128. end;
  129. procedure _FPC_libc_haltproc(e:longint); assembler; nostackframe; public name '_haltproc';
  130. asm
  131. la.pcrel $t0, fpc_ret_sp
  132. ld.d $sp, $t0, 0
  133. la.pcrel $t1, fpc_ret_ra
  134. ld.d $ra, $t1, 0
  135. jr $ra
  136. end;