dl-machine-x86_64.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 OSCAR lab, Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS 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
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * dl-machine-x86_64.h
  17. *
  18. * This files contain architecture-specific implementation of ELF dynamic
  19. * relocation function.
  20. * The source code is imported and modified from the GNU C Library.
  21. */
  22. #ifndef dl_machine_h
  23. #define dl_machine_h
  24. #define ELF_MACHINE_NAME "x86_64"
  25. #include <sys/param.h>
  26. #include <sysdep.h>
  27. #include <sysdeps/generic/ldsodefs.h>
  28. #include "pal_internal.h"
  29. /* The x86-64 never uses Elf64_Rel relocations. */
  30. #define ELF_MACHINE_NO_REL 1
  31. /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
  32. MAP is the object containing the reloc. */
  33. //#define DEBUG_RELOC
  34. static void
  35. elf_machine_rela (Elf64_Dyn **l_info, Elf64_Addr l_addr,
  36. Elf64_Rela *reloc, Elf64_Sym *sym, void *const reloc_addr_arg,
  37. bool rel, bool rel_relative)
  38. {
  39. Elf64_Addr *const reloc_addr = reloc_addr_arg;
  40. const unsigned long int r_type = ELF64_R_TYPE (reloc->r_info);
  41. const char * __attribute__ ((unused)) strtab =
  42. (const void *) D_PTR (l_info[DT_STRTAB]);
  43. #ifdef DEBUG_RELOC
  44. #define elf_machine_rela_debug(r_type, sym, value) \
  45. ({ if (strtab && sym && sym->st_name) \
  46. printf(#r_type ": %s\n", strtab + sym->st_name); \
  47. else if (value) \
  48. printf(#r_type ": %p\n", value); \
  49. else \
  50. printf(#r_type "\n", value); \
  51. })
  52. #else
  53. #define elf_machine_rela_debug(...) ({})
  54. #endif
  55. if (__builtin_expect (r_type == R_X86_64_RELATIVE, 0))
  56. {
  57. /* This is defined in rtld.c, but nowhere in the static libc.a;
  58. make the reference weak so static programs can still link.
  59. This declaration cannot be done when compiling rtld.c
  60. (i.e. #ifdef RTLD_BOOTSTRAP) because rtld.c contains the
  61. common defn for _dl_rtld_map, which is incompatible with a
  62. weak decl in the same file. */
  63. if (rel_relative)
  64. {
  65. #ifndef RTLD_BOOTSTRAP
  66. elf_machine_rela_debug (R_X86_64_RELATIVE, sym, 0);
  67. *reloc_addr = l_addr + reloc->r_addend;
  68. #endif
  69. }
  70. return;
  71. }
  72. if (__builtin_expect (r_type == R_X86_64_NONE, 0))
  73. {
  74. elf_machine_rela_debug (R_X86_64_NONE, sym, 0);
  75. return;
  76. }
  77. #ifdef RTLD_BOOTSTRAP
  78. Elf64_Addr value = (sym == NULL ? 0 : l_addr + sym->st_value);
  79. #define SYM (sym)
  80. #else
  81. Elf64_Sym *refsym = sym;
  82. Elf64_Addr value;
  83. Elf64_Addr sym_map;
  84. value = RESOLVE_MAP_IN_RTLD(sym);
  85. if (value) {
  86. /* We can't handle a IRELEATIVE symbol if it's found in RTLD,
  87. they should never exist */
  88. if (r_type == R_X86_64_IRELATIVE)
  89. return;
  90. } else {
  91. sym_map = RESOLVE_MAP(&strtab, &sym) ? : l_addr;
  92. value = sym_map + (sym ? sym->st_value : refsym->st_value);
  93. }
  94. #define SYM (sym ? : refsym)
  95. /* We do a very special relocation for loaded libraries */
  96. if (!rel) {
  97. if (sym && refsym && refsym != sym) {
  98. refsym->st_info = sym->st_info;
  99. refsym->st_size = sym->st_size;
  100. if (__builtin_expect (ELFW(ST_TYPE) (sym->st_info)
  101. == STT_GNU_IFUNC, 0)
  102. && __builtin_expect (sym->st_shndx != SHN_UNDEF, 1))
  103. {
  104. value = ((Elf64_Addr (*) (void)) value) ();
  105. refsym->st_info ^= ELFW(ST_TYPE)(sym->st_info);
  106. refsym->st_info |= STT_FUNC;
  107. }
  108. refsym->st_value = value - l_addr;
  109. } else {
  110. return;
  111. }
  112. }
  113. #endif
  114. if (sym != NULL
  115. && __builtin_expect (ELFW(ST_TYPE) (sym->st_info)
  116. == STT_GNU_IFUNC, 0)
  117. && __builtin_expect (sym->st_shndx != SHN_UNDEF, 1))
  118. value = ((Elf64_Addr (*) (void)) value) ();
  119. /* In the libc loader, they guaranteed that only R_ARCH_RELATIVE,
  120. R_ARCH_GLOB_DAT, R_ARCH_JUMP_SLOT appear in ld.so. We observed
  121. the same thing in libpal.so, so we are gonna to make the same
  122. assumption */
  123. switch (r_type)
  124. {
  125. case R_X86_64_GLOB_DAT:
  126. elf_machine_rela_debug (R_X86_64_GLOB_DAT, SYM, value);
  127. *reloc_addr = value + reloc->r_addend;
  128. break;
  129. case R_X86_64_JUMP_SLOT:
  130. elf_machine_rela_debug (R_X86_64_JUMP_SLOT, SYM, value);
  131. *reloc_addr = value + reloc->r_addend;
  132. break;
  133. #ifndef RTLD_BOOTSTRAP
  134. case R_X86_64_64:
  135. elf_machine_rela_debug (R_X86_64_64, SYM, value);
  136. *reloc_addr = value + reloc->r_addend;
  137. break;
  138. case R_X86_64_32:
  139. elf_machine_rela_debug (R_X86_64_32, SYM, value);
  140. value += reloc->r_addend;
  141. *(Elf64_Addr *) reloc_addr = value;
  142. break;
  143. /* Not needed for dl-conflict.c. */
  144. case R_X86_64_PC32:
  145. elf_machine_rela_debug (R_X86_64_PC32, SYM, value);
  146. value += reloc->r_addend - (Elf64_Addr) reloc_addr;
  147. *(Elf64_Addr *) reloc_addr = value;
  148. break;
  149. case R_X86_64_COPY:
  150. elf_machine_rela_debug (R_X86_64_COPY, SYM, value);
  151. size_t sym_size = sym ? sym->st_size : 0;
  152. size_t ref_size = refsym ? refsym->st_size : 0;
  153. memcpy (reloc_addr_arg, (void *) value, MIN (sym_size,
  154. ref_size));
  155. break;
  156. case R_X86_64_IRELATIVE:
  157. elf_machine_rela_debug (R_X86_64_IRELATIVE, SYM, value);
  158. value = sym_map + reloc->r_addend;
  159. value = ((Elf64_Addr (*) (void)) value) ();
  160. *reloc_addr = value;
  161. break;
  162. #endif
  163. default:
  164. return;
  165. }
  166. if (!rel)
  167. /* We have relocated the symbol, we don't want the
  168. interpreter to relocate it again. */
  169. reloc->r_info ^= ELF64_R_TYPE (reloc->r_info);
  170. }
  171. static void
  172. elf_machine_rela_relative (Elf64_Addr l_addr, const Elf64_Rela *reloc,
  173. void *const reloc_addr_arg)
  174. {
  175. Elf64_Addr *const reloc_addr = reloc_addr_arg;
  176. assert (ELF64_R_TYPE (reloc->r_info) == R_X86_64_RELATIVE);
  177. *reloc_addr = l_addr + reloc->r_addend;
  178. }
  179. #endif /* !dl_machine_h */