start.S 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* Startup code for Nios II
  2. Copyright (C) 1995-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. In addition to the permissions in the GNU Lesser General Public
  9. License, the Free Software Foundation gives you unlimited
  10. permission to link the compiled version of this file with other
  11. programs, and to distribute those programs without any restriction
  12. coming from the use of this file. (The GNU Lesser General Public
  13. License restrictions do apply in other respects; for example, they
  14. cover modification of the file, and distribution when not linked
  15. into another program.)
  16. Note that people who make modified versions of this file are not
  17. obligated to grant this special exception for their modified
  18. versions; it is their choice whether to do so. The GNU Lesser
  19. General Public License gives permission to release a modified
  20. version without this exception; this exception also makes it
  21. possible to release a modified version which carries forward this
  22. exception.
  23. The GNU C Library is distributed in the hope that it will be useful,
  24. but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  26. Lesser General Public License for more details.
  27. You should have received a copy of the GNU Lesser General Public
  28. License along with the GNU C Library. If not, see
  29. <http://www.gnu.org/licenses/>. */
  30. /* This is the canonical entry point, usually the first thing in the text
  31. segment.
  32. Note that the code in the .init section has already been run.
  33. This includes _init and _libc_init
  34. The stack pointer, sp, will point to the argument count on the stack.
  35. The initial state of the stack when a userspace process is started is:
  36. Purpose Start Address Length
  37. Unspecified High Addresses
  38. Referenced strings, etc. Varies
  39. Unspecified
  40. Null auxilliary vector entry 4bytes
  41. Auxilliary vector entries 8bytes each
  42. NULL terminator for envp 4bytes
  43. Environment pointers sp+8+4*argc 4bytes each
  44. NULL terminator for argv sp+4+4*argc 4bytes
  45. Argument pointers sp+4 4bytes each
  46. Argument count sp 4bytes
  47. Unspecified Low Addresses
  48. If the application should register a destructor function with atexit,
  49. the pointer will be placed in r4. Otherwise r4 will be zero.
  50. The contents of all other registers are unspecified. User code should
  51. set fp to zero to mark the end of the frame chain.
  52. The auxilliary vector is a series of pairs of 32-bit tag and 32-bit
  53. value, terminated by an AT_NULL tag.
  54. */
  55. .text
  56. .globl _start
  57. .type _start,%function
  58. _start:
  59. /* Set up the global pointer. */
  60. movhi gp, %hiadj(_gp)
  61. addi gp, gp, %lo(_gp)
  62. /* Save the stack pointer. */
  63. mov r2, sp
  64. /* Create room on the stack for the fini, rtld_fini and stack_end args
  65. to __libc_start_main. */
  66. subi sp, sp, 12
  67. /* Push stack_end */
  68. stw r2, 8(sp)
  69. /* Push rtld_fini */
  70. stw r4, 4(sp)
  71. /* Set up the GOT pointer. */
  72. nextpc r22
  73. 1: movhi r2, %hiadj(_gp_got - 1b)
  74. addi r2, r2, %lo(_gp_got - 1b)
  75. add r22, r22, r2
  76. /* Push fini */
  77. movhi r8, %call_hiadj(__libc_csu_fini)
  78. addi r8, r8, %call_lo(__libc_csu_fini)
  79. add r8, r8, r22
  80. ldw r8, 0(r8)
  81. stw r8, 0(sp)
  82. /* r7 == init */
  83. movhi r7, %call_hiadj(__libc_csu_init)
  84. addi r7, r7, %call_lo(__libc_csu_init)
  85. add r7, r7, r22
  86. ldw r7, 0(r7)
  87. /* r6 == argv */
  88. addi r6, sp, 16
  89. /* r5 == argc */
  90. ldw r5, 12(sp)
  91. /* r4 == main */
  92. movhi r4, %call_hiadj(main)
  93. addi r4, r4, %call_lo(main)
  94. add r4, r4, r22
  95. ldw r4, 0(r4)
  96. /* fp == 0 */
  97. mov fp, zero
  98. /* __libc_start_main (main, argc, argv, init, fini, rtld_fini,
  99. stack_end) */
  100. /* Let the libc call main and exit with its return code. */
  101. movhi r2, %call_hiadj(__libc_start_main)
  102. addi r2, r2, %call_lo(__libc_start_main)
  103. add r2, r2, r22
  104. ldw r2, 0(r2)
  105. callr r2
  106. /* should never get here....*/
  107. movhi r2, %call_hiadj(abort)
  108. addi r2, r2, %call_lo(abort)
  109. add r2, r2, r22
  110. ldw r2, 0(r2)
  111. callr r2
  112. /* Define a symbol for the first piece of initialized data. */
  113. .data
  114. .globl __data_start
  115. __data_start:
  116. .long 0
  117. .weak data_start
  118. data_start = __data_start