start.S 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* Copyright (C) 1995-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. In addition to the permissions in the GNU Lesser General Public
  8. License, the Free Software Foundation gives you unlimited
  9. permission to link the compiled version of this file with other
  10. programs, and to distribute those programs without any restriction
  11. coming from the use of this file. (The GNU Lesser General Public
  12. License restrictions do apply in other respects; for example, they
  13. cover modification of the file, and distribution when not linked
  14. into another program.)
  15. Note that people who make modified versions of this file are not
  16. obligated to grant this special exception for their modified
  17. versions; it is their choice whether to do so. The GNU Lesser
  18. General Public License gives permission to release a modified
  19. version without this exception; this exception also makes it
  20. possible to release a modified version which carries forward this
  21. exception.
  22. The GNU C Library is distributed in the hope that it will be useful,
  23. but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  25. Lesser General Public License for more details.
  26. You should have received a copy of the GNU Lesser General Public
  27. License along with the GNU C Library; if not, see
  28. <https://www.gnu.org/licenses/>. */
  29. #include <sysdep.h>
  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. x0/w0 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. 8(sp) argv[0]
  42. ...
  43. (8*argc)(sp) NULL
  44. (8*(argc+1))(sp) envp[0]
  45. ...
  46. NULL
  47. */
  48. .text
  49. ENTRY(_start)
  50. /* Create an initial frame with 0 LR and FP */
  51. cfi_undefined (x30)
  52. mov x29, #0
  53. mov x30, #0
  54. /* Setup rtld_fini in argument register */
  55. mov x5, x0
  56. /* Load argc and a pointer to argv */
  57. ldr x1, [sp, #0]
  58. add x2, sp, 8
  59. /* Setup stack limit in argument register */
  60. mov x6, sp
  61. #ifdef PIC
  62. # ifdef SHARED
  63. adrp x0, :got:main
  64. ldr x0, [x0, #:got_lo12:main]
  65. # else
  66. adrp x0, __wrap_main
  67. add x0, x0, :lo12:__wrap_main
  68. # endif
  69. #else
  70. movz x0, :abs_g3:main
  71. movk x0, :abs_g2_nc:main
  72. movk x0, :abs_g1_nc:main
  73. movk x0, :abs_g0_nc:main
  74. #endif
  75. mov x3, #0 /* Used to be init. */
  76. mov x4, #0 /* Used to be fini. */
  77. /* __libc_start_main (main, argc, argv, init, fini, rtld_fini,
  78. stack_end) */
  79. /* Let the libc call main and exit with its return code. */
  80. bl __libc_start_main
  81. /* should never get here....*/
  82. bl abort
  83. #if defined PIC && !defined SHARED
  84. /* When main is not defined in the executable but in a shared library
  85. then a wrapper is needed in crt1.o of the static-pie enabled libc,
  86. because crt1.o and rcrt1.o share code and the later must avoid the
  87. use of GOT relocations before __libc_start_main is called. */
  88. __wrap_main:
  89. bti c
  90. b main
  91. #endif
  92. END(_start)
  93. /* Define a symbol for the first piece of initialized data. */
  94. .data
  95. .globl __data_start
  96. __data_start:
  97. .long 0
  98. .weak data_start
  99. data_start = __data_start