dl-startup.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Xtensa ELF code used by dl-startup.c.
  3. *
  4. * Copyright (C) 2007 Tensilica Inc.
  5. *
  6. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  7. * Parts taken from glibc/sysdeps/xtensa/dl-machine.h.
  8. */
  9. __asm__ (
  10. " .text\n"
  11. " .align 4\n"
  12. " .literal_position\n"
  13. " .global _start\n"
  14. " .type _start, @function\n"
  15. " .hidden _start\n"
  16. "_start:\n"
  17. " # Compute load offset in a2: the GOT has not yet been relocated\n"
  18. " # but the entries for local symbols contain the relative offsets\n"
  19. " # and we can explicitly add the load offset in this code.\n"
  20. " _call0 0f\n"
  21. " .align 4\n"
  22. "0: movi a3, _start+3\n"
  23. " sub a2, a0, a3\n"
  24. #if defined(__XTENSA_WINDOWED_ABI__)
  25. " # Make sure a0 is cleared to mark the top of stack.\n"
  26. " movi a0, 0\n"
  27. " # user_entry_point = _dl_start(pointer to argument block)\n"
  28. " movi a4, _dl_start\n"
  29. " mov a6, sp\n"
  30. " add a4, a4, a2\n"
  31. " callx4 a4\n"
  32. " # Save user_entry_point so we can jump to it.\n"
  33. " mov a3, a6\n"
  34. #elif defined(__XTENSA_CALL0_ABI__)
  35. " # user_entry_point = _dl_start(pointer to argument block)\n"
  36. " movi a0, _dl_start\n"
  37. " add a0, a0, a2\n"
  38. " mov a2, sp\n"
  39. " callx0 a0\n"
  40. " # Save user_entry_point so we can jump to it.\n"
  41. " mov a3, a2\n"
  42. #else
  43. #error Unsupported Xtensa ABI
  44. #endif
  45. " l32i a7, sp, 0 # load argc\n"
  46. " # Load _dl_skip_args into a4.\n"
  47. " movi a4, _dl_skip_args\n"
  48. " l32i a4, a4, 0\n"
  49. " bnez a4, .Lfixup_stack\n"
  50. ".Lfixup_stack_ret:\n"
  51. " # Pass finalizer (_dl_fini) in a2 to the user entry point.\n"
  52. " movi a2, _dl_fini\n"
  53. " # Jump to user's entry point (_start).\n"
  54. " jx a3\n"
  55. ".Lfixup_stack:\n"
  56. " # argc -= _dl_skip_args (with argc @ sp+0)\n"
  57. " sub a7, a7, a4\n"
  58. " s32i a7, sp, 0\n"
  59. " # Shift everything by _dl_skip_args.\n"
  60. " addi a5, sp, 4 # a5 = destination ptr = argv\n"
  61. " add a4, a5, a4 # a4 = source ptr = argv + _dl_skip_args\n"
  62. " # Shift argv.\n"
  63. "1: l32i a6, a4, 0\n"
  64. " addi a4, a4, 4\n"
  65. " s32i a6, a5, 0\n"
  66. " addi a5, a5, 4\n"
  67. " bnez a6, 1b\n"
  68. " # Shift envp.\n"
  69. "2: l32i a6, a4, 0\n"
  70. " addi a4, a4, 4\n"
  71. " s32i a6, a5, 0\n"
  72. " addi a5, a5, 4\n"
  73. " bnez a6, 2b\n"
  74. " # Shift auxiliary table.\n"
  75. "3: l32i a6, a4, 0\n"
  76. " l32i a8, a4, 4\n"
  77. " addi a4, a4, 8\n"
  78. " s32i a6, a5, 0\n"
  79. " s32i a8, a5, 4\n"
  80. " addi a5, a5, 8\n"
  81. " bnez a6, 3b\n"
  82. " j .Lfixup_stack_ret");
  83. /* Get a pointer to the argv value. */
  84. #define GET_ARGV(ARGVP, ARGS) ARGVP = (((unsigned long *) ARGS) + 1)
  85. /* Function calls are not safe until the GOT relocations have been done. */
  86. #define NO_FUNCS_BEFORE_BOOTSTRAP
  87. #define PERFORM_BOOTSTRAP_GOT(tpnt) \
  88. do { \
  89. xtensa_got_location *got_loc; \
  90. unsigned long l_addr = tpnt->loadaddr; \
  91. Elf32_Word relative_count; \
  92. unsigned long rel_addr; \
  93. Elf32_Addr prev_got_start = 0, prev_got_end = 0; \
  94. int x; \
  95. \
  96. got_loc = (xtensa_got_location *) \
  97. (tpnt->dynamic_info[DT_XTENSA (GOT_LOC_OFF)] + l_addr); \
  98. \
  99. for (x = 0; x < tpnt->dynamic_info[DT_XTENSA (GOT_LOC_SZ)]; x++) { \
  100. Elf32_Addr got_start, got_end; \
  101. got_start = got_loc[x].offset & ~(PAGE_SIZE - 1); \
  102. got_end = ((got_loc[x].offset + got_loc[x].length + PAGE_SIZE - 1) \
  103. & ~(PAGE_SIZE - 1)); \
  104. if (got_end >= prev_got_start && got_start <= prev_got_end) { \
  105. if (got_end > prev_got_end) \
  106. prev_got_end = got_end; \
  107. if (got_start < prev_got_start) \
  108. prev_got_start = got_start; \
  109. continue; \
  110. } else if (prev_got_start != prev_got_end) { \
  111. _dl_mprotect ((void *)(prev_got_start + l_addr), \
  112. prev_got_end - prev_got_start, \
  113. PROT_READ | PROT_WRITE | PROT_EXEC); \
  114. } \
  115. prev_got_start = got_start; \
  116. prev_got_end = got_end; \
  117. } \
  118. \
  119. if (prev_got_start != prev_got_end) { \
  120. _dl_mprotect ((void *)(prev_got_start + l_addr), \
  121. prev_got_end - prev_got_start, \
  122. PROT_READ | PROT_WRITE | PROT_EXEC); \
  123. } \
  124. \
  125. /* The following is a stripped down version of the code following \
  126. the invocation of PERFORM_BOOTSTRAP_GOT in dl-startup.c. That \
  127. code is skipped when PERFORM_BOOTSTRAP_GOT is defined, so it has \
  128. to be done here instead. */ \
  129. relative_count = tpnt->dynamic_info[DT_RELCONT_IDX]; \
  130. rel_addr = tpnt->dynamic_info[DT_RELOC_TABLE_ADDR]; \
  131. if (rel_addr) \
  132. elf_machine_relative(load_addr, rel_addr, relative_count); \
  133. } while (0)