si_c.inc 2.5 KB

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