si_c.inc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 _FPC_libc_haltproc(e:longint); forward;
  39. procedure ___libc_csu_init; external name '__libc_csu_init';
  40. {$if FPC_FULLVERSION>30202}
  41. procedure __libc_csu_init; public name '__libc_csu_init'; assembler; nostackframe;
  42. asm
  43. .weak __libc_csu_init
  44. end;
  45. {$endif}
  46. procedure ___libc_csu_fini; external name '__libc_csu_fini';
  47. {$if FPC_FULLVERSION>30202}
  48. procedure __libc_csu_fini; public name '__libc_csu_fini'; assembler; nostackframe;
  49. asm
  50. .weak __libc_csu_fini
  51. end;
  52. {$endif}
  53. procedure main_stub; assembler; nostackframe;
  54. asm
  55. { let fp = sp in case of no fp value }
  56. ori $fp, $sp, 0
  57. { save ra and sp }
  58. la.pcrel $t0, fpc_ret_ra
  59. st.d $ra, $t0, 0
  60. la.pcrel $t1, fpc_ret_sp
  61. st.d $sp, $t1, 0
  62. {$ifdef FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  63. la.got, $t0, SysInitEntryInformation
  64. st.d $sp, $t0, TEntryInformation.OS.stkptr
  65. la.got $t1, StackLength
  66. ld.d $t1, $t1, 0
  67. st.d $t1, $t0, TEntryInformation.OS.stklen
  68. la.got $t2, _FPC_libc_haltproc
  69. st.d $t2, $t0, TEntryInformation.OS.haltproc
  70. move $a0, $t0
  71. bl %plt(SysEntry)
  72. {$else}
  73. { save stack pointer }
  74. la.got $t1, initialstkptr
  75. st.d $sp, $t1, 0
  76. { call PascalMain }
  77. bl %plt(PASCALMAIN)
  78. {$endif}
  79. break 1
  80. end;
  81. procedure ini_dummy;
  82. begin
  83. end;
  84. {******************************************************************************
  85. C library start/halt
  86. ******************************************************************************}
  87. procedure _FPC_libc_start; assembler; nostackframe; public name '_start';
  88. asm
  89. { clear frame pointer }
  90. ori $fp, $zero, 0
  91. {$ifdef FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  92. la.got $t1, SysInitEntryInformation
  93. { argc = *(int *)sp }
  94. ld.w $a1, $sp, 0
  95. st.w $a1, $t1, TEntryInformation.OS.argc
  96. { argv = (char **)(sp + 8) }
  97. addi.d $a2, $sp, 8
  98. st.d $a2, $t1, TEntryInformation.OS.argv
  99. { save envp }
  100. alsl.d $t0, $a1, $a2, 3
  101. addi.d $t0, $t0, 8
  102. st.d $t0, $t1, TEntryInformation.OS.envp
  103. {$else}
  104. { argc = *(int *)sp }
  105. ld.w $a1, $sp, 0
  106. { save operatingsystem parameter argc }
  107. la.got $t0, operatingsystem_parameter_argc
  108. st.w $a1, $t0, 0
  109. { argv = (char **)(sp + 8) }
  110. addi.d $a2, $sp, 8
  111. { save operatingsystem parameter argv }
  112. la.got $t0, operatingsystem_parameter_argv
  113. st.d $a2, $t0, 0
  114. { save operatingsystem parameter envp }
  115. la.got $t0, operatingsystem_parameter_envp
  116. alsl.d $t1, $a1, $a2, 3
  117. addi.d $t1, $t1, 8
  118. st.d $t1, $t0, 0
  119. {$endif}
  120. { adjust $sp for 16-aligned }
  121. bstrins.d $sp, $zero, 3, 0
  122. { call libc_start_main (&main_stub, argc, argv, init, fiit, rtld_fini, stkend ) }
  123. la.got $a3, ___libc_csu_fini
  124. la.got $a4, ___libc_csu_fini
  125. ori $a5, $a0, 0
  126. ori $a6, $sp, 0
  127. la.pcrel $a0, main_stub
  128. bl %plt(libc_start_main)
  129. break 1
  130. end;
  131. procedure _FPC_libc_haltproc(e:longint); assembler; nostackframe; public name '_haltproc';
  132. asm
  133. la.pcrel $t0, fpc_ret_sp
  134. ld.d $sp, $t0, 0
  135. la.pcrel $t1, fpc_ret_ra
  136. ld.d $ra, $t1, 0
  137. jr $ra
  138. end;