si_g.inc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. procedure ini_dummy;
  19. begin
  20. end;
  21. procedure libc_start_main(main: TProcedure; argc: ptruint; argv: PPAnsiChar; init, fini, rtld_fini: TProcedure; stack_end: pointer); cdecl; external name '__libc_start_main';
  22. procedure libc_exit(code: ptruint); cdecl; external name 'exit';
  23. procedure monstartup(low_pc,high_pc: pointer); cdecl; external;
  24. procedure _mcleanup; cdecl; external;
  25. procedure atexit(p: pointer); cdecl; external;
  26. procedure _FPC_rv_enter(at_exit: TProcedure; sp: pptruint);
  27. var
  28. argc: ptruint;
  29. argv: PPAnsiChar;
  30. begin
  31. argc:=sp[0];
  32. argv:=@sp[1];
  33. initialstkptr:=sp;
  34. operatingsystem_parameter_argc:=argc;
  35. operatingsystem_parameter_argv:=argv;
  36. operatingsystem_parameter_envp:=@sp[argc+2];
  37. monstartup(@_FPC_rv_enter,@_etext);
  38. atexit(@_mcleanup);
  39. libc_start_main(@PascalMain, argc, argv, @ini_dummy, @ini_dummy, at_exit, sp);
  40. end;
  41. procedure _FPC_proc_start; assembler; nostackframe; public name '_start';
  42. asm
  43. { set up GP }
  44. .option push
  45. .option norelax
  46. .L1:
  47. auipc gp, %pcrel_hi(BSS_START+0x800)
  48. addi gp, gp, %pcrel_lo(.L1)
  49. .option pop
  50. { Initialise FP to zero }
  51. addi fp, x0, 0
  52. { atexit is in a0 }
  53. addi a1, sp, 0
  54. jal x1, _FPC_rv_enter
  55. end;
  56. procedure _FPC_rv_exit(e:longint); assembler; nostackframe;
  57. asm
  58. addi a7, x0, 94
  59. ecall
  60. end;
  61. procedure _FPC_proc_haltproc(e:longint); cdecl; public name '_haltproc';
  62. begin
  63. while true do
  64. begin
  65. libc_exit(e);
  66. _FPC_rv_exit(e);
  67. end;
  68. end;
  69. procedure initgp; assembler; nostackframe;
  70. asm
  71. .Linitgp:
  72. .option push
  73. .option norelax
  74. .L1:
  75. auipc gp, %pcrel_hi(BSS_START+0x800)
  76. addi gp, gp, %pcrel_lo(.L1)
  77. .option pop
  78. jalr x0, x1
  79. .section ".preinit_array","aw"
  80. .dc.a .Linitgp
  81. .text
  82. end;