crt1.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* -*-comment-start: "//";comment-end:""-*-
  2. * GNU Mes --- Maxwell Equations of Software
  3. * Copyright © 2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  4. * Copyright © 2020 Danny Milosavljevic <dannym@scratchpost.org>
  5. *
  6. * This file is part of GNU Mes.
  7. *
  8. * GNU Mes is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * GNU Mes is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include "mes/lib-mini.h"
  22. int main (int argc, char *argv[], char *envp[]);
  23. void /* must not return */
  24. _start ()
  25. {
  26. /*
  27. sp+1 argv UNROLLED ON STACK
  28. sp -> argc
  29. environ = &argv[argc + 1]
  30. HOWEVER, the function entry already allocated space for locals on the stack (after saving lr and fp, which moved sp again). Hence, use fp instead of sp.
  31. */
  32. /* stdin = 0 */
  33. asm ("!0 mov____$i8,%r0");
  34. asm ("mov____%r0,0x32 &__stdin");
  35. /* stdout = 1 */
  36. asm ("!1 mov____$i8,%r0");
  37. asm ("mov____%r0,0x32 &__stdout");
  38. /* stderr = 2 */
  39. asm ("!2 mov____$i8,%r0");
  40. asm ("mov____%r0,0x32 &__stderr");
  41. /* Add "environ" to main's arguments */
  42. asm ("!8 ldr____%r0,(%fp,+#$i8)"); /* "argc" */
  43. asm ("mov____%fp,%r1");
  44. asm ("!12 add____%r1,$i8"); /* argv */
  45. asm ("add____%r2,%r1,%r0,lsl#2"); /* "environ": argv + argc */
  46. asm ("!4 add____%r2,$i8"); /* "environ": argv + argc + 1 */
  47. asm ("push___%r2"); /* environ */
  48. asm ("push___%r1"); /* argv */
  49. asm ("push___%r0"); /* argc */
  50. /* environ = r2 */
  51. asm ("mov____%r2,0x32 &environ");
  52. main ();
  53. asm ("SYS_exit mov____$i8,%r7");
  54. asm ("swi____$0");
  55. do
  56. {
  57. asm ("wfi");
  58. }
  59. while (1);
  60. }