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