si_g.inc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. st.d $t1, $t0, TEntryInformation.OS.stklen
  65. la.got $t2, _FPC_libc_haltproc
  66. st.d $t2, $t0, TEntryInformation.OS.haltproc
  67. move $a0, $t0
  68. bl %plt(SysEntry)
  69. {$else}
  70. { save stack pointer }
  71. la.got $t1, initialstkptr
  72. st.d $sp, $t1, 0
  73. { call PascalMain }
  74. bl %plt(PASCALMAIN)
  75. {$endif}
  76. break 1
  77. end;
  78. procedure ini_dummy;
  79. begin
  80. end;
  81. {******************************************************************************
  82. C library start/halt
  83. ******************************************************************************}
  84. procedure _FPC_libc_start; assembler; nostackframe; public name '_start';
  85. asm
  86. { clear frame pointer }
  87. ori $fp, $zero, 0
  88. {$ifdef FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  89. la.got $t1, SysInitEntryInformation
  90. { argc = *(int *)sp }
  91. ld.w $a1, $sp, 0
  92. st.w $a1, $t1, TEntryInformation.OS.argc
  93. { argv = (char **)(sp + 8) }
  94. addi.d $a2, $sp, 8
  95. st.d $a2, $t1, TEntryInformation.OS.argv
  96. { save envp }
  97. alsl.d $t0, $a1, $a2, 3
  98. addi.d $t0, $t0, 8
  99. st.d $t0, $t1, TEntryInformation.OS.envp
  100. {$else}
  101. { argc = *(int *)sp }
  102. ld.w $a1, $sp, 0
  103. { save operatingsystem parameter argc }
  104. la.got $t0, operatingsystem_parameter_argc
  105. st.w $a1, $t0, 0
  106. { argv = (char **)(sp + 8) }
  107. addi.d $a2, $sp, 8
  108. { save operatingsystem parameter argv }
  109. la.got $t0, operatingsystem_parameter_argv
  110. st.d $a2, $t0, 0
  111. { save operatingsystem parameter envp }
  112. la.got $t0, operatingsystem_parameter_envp
  113. alsl.d $t1, $a1, $a2, 3
  114. addi.d $t1, $t1, 8
  115. st.d $t1, $t0, 0
  116. {$endif}
  117. { adjust $sp for 16-aligned }
  118. bstrins.d $sp, $zero, 3, 0
  119. { call libc_start_main (&main_stub, argc, argv, dummy, dummy, rtld_fini, stkend ) }
  120. la.got $a3, ini_dummy
  121. la.got $a4, ini_dummy
  122. ori $a5, $a0, 0
  123. ori $a6, $sp, 0
  124. la.pcrel $a0, main_stub
  125. bl %plt(libc_start_main)
  126. break 1
  127. end;
  128. procedure _FPC_libc_haltproc(e:longint); assembler; nostackframe; public name '_haltproc';
  129. asm
  130. la.pcrel $t0, fpc_ret_sp
  131. ld.d $sp, $t0, 0
  132. la.pcrel $t1, fpc_ret_ra
  133. ld.d $ra, $t1, 0
  134. jr $ra
  135. end;