start.S 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* Startup code compliant to the ELF RISC-V ABI.
  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. #define __ASSEMBLY__ 1
  31. #include <entry.h>
  32. #include <sysdep.h>
  33. #include <sys/asm.h>
  34. /* The entry point's job is to call __libc_start_main. Per the ABI,
  35. a0 contains the address of a function to be passed to atexit.
  36. __libc_start_main wants this in a5. */
  37. ENTRY (ENTRY_POINT)
  38. /* Terminate call stack by noting ra is undefined. Use a dummy
  39. .cfi_label to force starting the FDE. */
  40. .cfi_label .Ldummy
  41. cfi_undefined (ra)
  42. call load_gp
  43. mv a5, a0 /* rtld_fini. */
  44. /* main may be in a shared library. */
  45. #if defined PIC && !defined SHARED
  46. /* Avoid relocation in static PIE since _start is called before it
  47. is relocated. */
  48. lla a0, __wrap_main
  49. #else
  50. la a0, main
  51. #endif
  52. REG_L a1, 0(sp) /* argc. */
  53. addi a2, sp, SZREG /* argv. */
  54. andi sp, sp, ALMASK /* Align stack. */
  55. li a3, 0 /* Used to be init. */
  56. li a4, 0 /* Used to be fini. */
  57. mv a6, sp /* stack_end. */
  58. call __libc_start_main@plt
  59. ebreak
  60. END (ENTRY_POINT)
  61. #if defined PIC && !defined SHARED
  62. __wrap_main:
  63. tail main@plt
  64. #endif
  65. /* Dynamic links need the global pointer to be initialized prior to calling
  66. any shared library's initializers, so we use preinit_array to load it.
  67. This doesn't cut it for static links, though, since the global pointer
  68. needs to be initialized before calling __libc_start_main in that case.
  69. So we redundantly initialize it at the beginning of _start. */
  70. load_gp:
  71. .option push
  72. .option norelax
  73. lla gp, __global_pointer$
  74. .option pop
  75. ret
  76. .section .preinit_array,"aw"
  77. .align PTRLOG
  78. .dc.a load_gp
  79. /* Define a symbol for the first piece of initialized data. */
  80. .data
  81. .globl __data_start
  82. __data_start:
  83. .weak data_start
  84. data_start = __data_start