crt1.S 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * This file is subject to the terms and conditions of the LGPL V2.1
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2018 Kalray Inc.
  7. */
  8. /* Startup code compliant to the ELF KVX ABI */
  9. #include <libc-symbols.h>
  10. #include <features.h>
  11. .type main,@function
  12. .type __uClibc_main,@function
  13. /*
  14. * When we enter this piece of code, the program stack has been
  15. * layed out by the kernel like this:
  16. * argc argument counter (integer)
  17. * argv[0] program name (pointer)
  18. * argv[1...N] program args (pointers)
  19. * argv[argc-1] end of args (integer)
  20. * NULL
  21. * env[0...N] environment variables (pointers)
  22. * NULL
  23. *
  24. * Moreover, when using dynamic loader, $r0 contains the rtld_fini
  25. * address
  26. *
  27. * And we need to call the following function:
  28. * __uClibc_main (int (*main) (int, char **, char **), int argc,
  29. * char **argv, void (*init) (void), void (*fini) (void),
  30. * void (*rtld_fini) (void), void *stack_end)
  31. */
  32. .text
  33. .globl _start
  34. .type _start,@function
  35. .align 8
  36. C_SYMBOL_NAME(_start):
  37. /* Load argc from stack */
  38. ld $r1 = 0[$sp]
  39. /* Load argv addr from stack */
  40. addd $r2 = $sp, 0x8
  41. #ifdef __PIC__
  42. pcrel $r7 = @gotaddr()
  43. #endif
  44. ;;
  45. /* $r0 contains rtld_fini when run by dynamic loader */
  46. copyd $r5 = $r0
  47. /* prepare __uClibc_main arg */
  48. #ifndef __PIC__
  49. make $r3 = _init
  50. make $r4 = _fini
  51. #endif
  52. ;;
  53. /* Align stack to 32-byte boundary */
  54. andd $sp = $sp, -32
  55. make $r8 = 0
  56. make $fp = 0
  57. /* prepare __uClibc_main arg */
  58. #ifdef __PIC__
  59. ld $r3 = @got(_init)[$r7]
  60. #endif
  61. ;;
  62. /* Setup stack_end for __uClibc_main */
  63. copyd $r6 = $sp
  64. /* Clear compute status */
  65. set $cs = $r8
  66. #ifdef __PIC__
  67. ld $r4 = @got(_fini)[$r7]
  68. #endif
  69. ;;
  70. #ifdef __PIC__
  71. ld $r0 = @got(main)[$r7]
  72. #else
  73. make $r0 = main
  74. #endif
  75. goto __uClibc_main
  76. ;;
  77. /* We should never return ! */
  78. errop
  79. ;;