dl-sysdep.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* Machine-dependent ELF dynamic relocation.
  2. Parts copied from glibc/sysdeps/xtensa/dl-machine.h
  3. Copyright (C) 2001, 2007 Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. /* Define this if the system uses RELOCA. */
  17. #define ELF_USES_RELOCA
  18. #include <elf.h>
  19. #include <link.h>
  20. /* Translate a processor specific dynamic tag to the index
  21. in l_info array. */
  22. #define DT_XTENSA(x) (DT_XTENSA_##x - DT_LOPROC + DT_NUM + OS_NUM)
  23. typedef struct xtensa_got_location_struct {
  24. Elf32_Off offset;
  25. Elf32_Word length;
  26. } xtensa_got_location;
  27. /* Initialization sequence for the GOT. */
  28. #define INIT_GOT(GOT_BASE, MODULE) \
  29. do { \
  30. xtensa_got_location *got_loc; \
  31. Elf32_Addr l_addr = MODULE->loadaddr; \
  32. Elf32_Addr prev_got_start = 0, prev_got_end = 0; \
  33. int x; \
  34. \
  35. got_loc = (xtensa_got_location *) \
  36. (MODULE->dynamic_info[DT_XTENSA (GOT_LOC_OFF)] + l_addr); \
  37. \
  38. for (x = 0; x < MODULE->dynamic_info[DT_XTENSA (GOT_LOC_SZ)]; x++) \
  39. { \
  40. Elf32_Addr got_start, got_end; \
  41. got_start = got_loc[x].offset & ~(PAGE_SIZE - 1); \
  42. got_end = ((got_loc[x].offset + got_loc[x].length + PAGE_SIZE - 1) \
  43. & ~(PAGE_SIZE - 1)); \
  44. if (got_end >= prev_got_start && got_start <= prev_got_end) \
  45. { \
  46. if (got_end > prev_got_end) \
  47. prev_got_end = got_end; \
  48. if (got_start < prev_got_start) \
  49. prev_got_start = got_start; \
  50. continue; \
  51. } \
  52. else if (prev_got_start != prev_got_end) \
  53. { \
  54. _dl_mprotect ((void *)(prev_got_start + l_addr), \
  55. prev_got_end - prev_got_start, \
  56. PROT_READ | PROT_WRITE | PROT_EXEC); \
  57. } \
  58. prev_got_start = got_start; \
  59. prev_got_end = got_end; \
  60. } \
  61. \
  62. if (prev_got_start != prev_got_end) \
  63. { \
  64. _dl_mprotect ((void *)(prev_got_start + l_addr), \
  65. prev_got_end - prev_got_start, \
  66. PROT_READ | PROT_WRITE | PROT_EXEC); \
  67. } \
  68. \
  69. /* Fill in first GOT entry according to the ABI. */ \
  70. GOT_BASE[0] = (unsigned long) _dl_linux_resolve; \
  71. } while (0)
  72. /* Parse dynamic info */
  73. #define ARCH_NUM 2
  74. #define ARCH_DYNAMIC_INFO(dpnt, dynamic, debug_addr) \
  75. do { \
  76. if (dpnt->d_tag == DT_XTENSA_GOT_LOC_OFF) \
  77. dynamic[DT_XTENSA (GOT_LOC_OFF)] = dpnt->d_un.d_ptr; \
  78. else if (dpnt->d_tag == DT_XTENSA_GOT_LOC_SZ) \
  79. dynamic[DT_XTENSA (GOT_LOC_SZ)] = dpnt->d_un.d_val; \
  80. } while (0)
  81. /* Here we define the magic numbers that this dynamic loader should accept. */
  82. #define MAGIC1 EM_XTENSA
  83. #undef MAGIC2
  84. /* Used for error messages. */
  85. #define ELF_TARGET "Xtensa"
  86. struct elf_resolve;
  87. extern unsigned long _dl_linux_resolver (struct elf_resolve *, int);
  88. /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or
  89. TLS variable, so undefined references should not be allowed to define
  90. the value. */
  91. #define elf_machine_type_class(type) \
  92. (((type) == R_XTENSA_JMP_SLOT || (type) == R_XTENSA_TLS_TPOFF \
  93. || (type) == R_XTENSA_TLSDESC_FN || (type) == R_XTENSA_TLSDESC_ARG) \
  94. * ELF_RTYPE_CLASS_PLT)
  95. /* Return the link-time address of _DYNAMIC. */
  96. static __always_inline Elf32_Addr
  97. elf_machine_dynamic (void)
  98. {
  99. /* This function is only used while bootstrapping the runtime linker.
  100. The "_DYNAMIC" symbol is always local so its GOT entry will initially
  101. contain the link-time address. */
  102. return (Elf32_Addr) &_DYNAMIC;
  103. }
  104. /* Return the run-time load address of the shared object. */
  105. static __always_inline Elf32_Addr
  106. elf_machine_load_address (void)
  107. {
  108. Elf32_Addr addr, tmp;
  109. /* At this point, the runtime linker is being bootstrapped and the GOT
  110. entry used for ".Lhere" will contain the link address. The CALL0 will
  111. produce the dynamic address of ".Lhere" + 3. Thus, the end result is
  112. equal to "dynamic_address(.Lhere) - link_address(.Lhere)". */
  113. __asm__ ("\
  114. movi %0, .Lhere\n\
  115. mov %1, a0\n\
  116. .Lhere: _call0 0f\n\
  117. .align 4\n\
  118. 0: sub %0, a0, %0\n\
  119. mov a0, %1"
  120. : "=a" (addr), "=a" (tmp));
  121. return addr - 3;
  122. }
  123. static __always_inline void
  124. elf_machine_relative (Elf32_Addr load_off, const Elf32_Addr rel_addr,
  125. Elf32_Word relative_count)
  126. {
  127. Elf32_Rela *rpnt = (Elf32_Rela *) rel_addr;
  128. while (relative_count--)
  129. {
  130. Elf32_Addr *const reloc_addr = (Elf32_Addr *) (load_off + rpnt->r_offset);
  131. *reloc_addr += load_off + rpnt->r_addend;
  132. rpnt++;
  133. }
  134. }