start.inc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by Michael Van Canneyt,
  4. member of the Free Pascal development team.
  5. Program startup
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. type
  13. TCdeclProcedure = procedure; cdecl;
  14. function atexit(proc:TCdeclProcedure):longint;cdecl;external 'c' name 'atexit';
  15. procedure _cleanup;cdecl;external 'c' name '_cleanup';
  16. procedure _DYNAMIC;cdecl;external 'c' name '_DYNAMIC';
  17. procedure __fpstart;cdecl;external 'c' name '__fpstart';
  18. procedure PascalMain;cdecl;external name 'PASCALMAIN';
  19. procedure _start;assembler;nostackframe;public name '_start';
  20. asm
  21. // Terminate the stack frame, and reserve space for functions to
  22. // drop their arguments.
  23. mov %g0, %fp
  24. sub %sp, 6*4, %sp
  25. // Extract the arguments and environment as encoded on the stack. The
  26. // argument info starts after one register window (16 words) past the SP.
  27. ld [%sp+22*4], %o2
  28. sethi %hi(argc),%o1
  29. or %o1,%lo(argc),%o1
  30. st %o2, [%o1]
  31. add %sp, 23*4, %o0
  32. sethi %hi(argv),%o1
  33. or %o1,%lo(argv),%o1
  34. st %o0, [%o1]
  35. // envp=(argc+1)*4+argv
  36. inc %o2
  37. sll %o2, 2, %o2
  38. add %o2, %o0, %o2
  39. sethi %hi(envp),%o1
  40. or %o1,%lo(envp),%o1
  41. st %o2, [%o1]
  42. // Check to see if there is an _cleanup() function linked in, and if
  43. // so, register it with atexit() as the last thing to be run by
  44. // atexit().
  45. sethi %hi(_cleanup), %o0
  46. or %o0, %lo(_cleanup), %o0
  47. cmp %o0,%g0
  48. be .L1
  49. nop
  50. call atexit
  51. nop
  52. .L1:
  53. // Now check to see if we have an _DYNAMIC table, and if so then
  54. // we need to register the function pointer previously in %edx, but
  55. // now conveniently saved on the stack as the argument to pass to
  56. // atexit().
  57. sethi %hi(_DYNAMIC), %o0
  58. or %o0, %lo(_DYNAMIC), %o0
  59. cmp %o0,%g0
  60. be .L2
  61. nop
  62. call atexit
  63. nop
  64. .L2:
  65. // Register _fini() with atexit(). We will take care of calling _init()
  66. // directly.
  67. //
  68. // sethi %hi(_fini), %o0
  69. // or %o0, %lo(_fini), %o0
  70. // call atexit
  71. // Call _init(argc, argv, environ), _fpstart(argc, argv, environ), and
  72. // main(argc, argv, environ).
  73. ld [%sp+22*4], %o0
  74. add %sp, 23*4, %o1
  75. add %o0, 1, %o2
  76. sll %o2, 2, %o2
  77. add %o2, %o1, %o2
  78. // call __fpstart
  79. // nop
  80. call PASCALMAIN
  81. nop
  82. // Die very horribly if exit returns
  83. unimp
  84. end;