start.S 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* Startup code for ARM & ELF
  2. Copyright (C) 1995-2026 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. <https://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. At this entry point, most registers' values are unspecified, except:
  35. a1 Contains a function pointer to be registered with `atexit'.
  36. This is how the dynamic linker arranges to have DT_FINI
  37. functions called for shared libraries that have been loaded
  38. before this code runs.
  39. sp The stack contains the arguments and environment:
  40. 0(sp) argc
  41. 4(sp) argv[0]
  42. ...
  43. (4*argc)(sp) NULL
  44. (4*(argc+1))(sp) envp[0]
  45. ...
  46. NULL
  47. */
  48. /* Tag_ABI_align8_preserved: This code preserves 8-byte
  49. alignment in any callee. */
  50. .eabi_attribute 25, 1
  51. /* Tag_ABI_align8_needed: This code may require 8-byte alignment from
  52. the caller. */
  53. .eabi_attribute 24, 1
  54. #if defined(__thumb2__)
  55. .thumb
  56. .syntax unified
  57. #endif
  58. .text
  59. .globl _start
  60. .type _start,#function
  61. _start:
  62. /* Protect against unhandled exceptions. */
  63. .fnstart
  64. /* Clear the frame pointer and link register since this is the outermost frame. */
  65. mov fp, #0
  66. mov lr, #0
  67. /* Pop argc off the stack and save a pointer to argv */
  68. pop { a2 }
  69. mov a3, sp
  70. /* Push stack limit */
  71. push { a3 }
  72. /* Push rtld_fini */
  73. push { a1 }
  74. #ifdef PIC
  75. ldr sl, .L_GOT
  76. adr a4, .L_GOT
  77. add sl, sl, a4
  78. mov a4, #0 /* Used to be init. */
  79. push { a4 } /* Used to be fini. */
  80. ldr a1, .L_GOT+4 /* main */
  81. ldr a1, [sl, a1]
  82. /* __libc_start_main (main, argc, argv, init, fini, rtld_fini, stack_end) */
  83. /* Let the libc call main and exit with its return code. */
  84. bl __libc_start_main(PLT)
  85. #else
  86. mov a4, #0 /* Used to init. */
  87. push { a4 } /* Used to fini. */
  88. ldr a1, =main
  89. /* __libc_start_main (main, argc, argv, init, fini, rtld_fini, stack_end) */
  90. /* Let the libc call main and exit with its return code. */
  91. bl __libc_start_main
  92. #endif
  93. /* should never get here....*/
  94. bl abort
  95. #ifdef PIC
  96. .align 2
  97. .L_GOT:
  98. .word _GLOBAL_OFFSET_TABLE_ - .L_GOT
  99. .word main(GOT)
  100. #endif
  101. .cantunwind
  102. .fnend
  103. /* Define a symbol for the first piece of initialized data. */
  104. .data
  105. .globl __data_start
  106. __data_start:
  107. .long 0
  108. .weak data_start
  109. data_start = __data_start