si_g.inc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2019 by Jeppe Johansen.
  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. {$linklib c}
  14. {$linklib gcc}
  15. var
  16. BSS_START: record end; external name '__bss_start';
  17. _etext: pointer; external name '_etext';
  18. { as we do not call these procedures directly, calling conventions do not matter and
  19. even if we did, we use c calling conventions anyways }
  20. procedure __libc_csu_init; external name '__libc_csu_init';
  21. procedure __libc_csu_fini; external name '__libc_csu_fini';
  22. procedure libc_start_main(main: TProcedure; argc: ptruint; argv: ppchar; init, fini, rtld_fini: TProcedure; stack_end: pointer); cdecl; external name '__libc_start_main';
  23. procedure libc_exit(code: ptruint); cdecl; external name 'exit';
  24. procedure monstartup(low_pc,high_pc: pointer); cdecl; external;
  25. procedure _mcleanup; cdecl; external;
  26. procedure atexit(p: pointer); cdecl; external;
  27. procedure _FPC_rv_enter(at_exit: TProcedure; sp: pptruint);
  28. var
  29. argc: ptruint;
  30. argv: ppchar;
  31. begin
  32. argc:=sp[0];
  33. argv:=@sp[1];
  34. initialstkptr:=sp;
  35. operatingsystem_parameter_argc:=argc;
  36. operatingsystem_parameter_argv:=argv;
  37. operatingsystem_parameter_envp:=@sp[argc+2];
  38. monstartup(@_FPC_rv_enter,@_etext);
  39. atexit(@_mcleanup);
  40. libc_start_main(@PascalMain, argc, argv, @__libc_csu_init, @__libc_csu_fini, at_exit, sp);
  41. end;
  42. procedure _FPC_proc_start; assembler; nostackframe; public name '_start';
  43. asm
  44. { set up GP }
  45. .option push
  46. .option norelax
  47. .L1:
  48. auipc gp, %pcrel_hi(BSS_START+0x800)
  49. addi gp, gp, %pcrel_lo(.L1)
  50. .option pop
  51. { Initialise FP to zero }
  52. addi fp, x0, 0
  53. { atexit is in a0 }
  54. addi a1, sp, 0
  55. jal x1, _FPC_rv_enter
  56. end;
  57. procedure _FPC_rv_exit(e:longint); assembler; nostackframe;
  58. asm
  59. addi a7, x0, 94
  60. ecall
  61. end;
  62. procedure _FPC_proc_haltproc(e:longint); cdecl; public name '_haltproc';
  63. begin
  64. while true do
  65. begin
  66. libc_exit(e);
  67. _FPC_rv_exit(e);
  68. end;
  69. end;
  70. procedure initgp; assembler; nostackframe;
  71. asm
  72. .Linitgp:
  73. .option push
  74. .option norelax
  75. .L1:
  76. auipc gp, %pcrel_hi(BSS_START+0x800)
  77. addi gp, gp, %pcrel_lo(.L1)
  78. .option pop
  79. jalr x0, x1
  80. .section ".preinit_array","aw"
  81. .dc.a .Linitgp
  82. .text
  83. end;