dl-machine-x86_64.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * dl-machine-x86_64.h
  15. *
  16. * This files contain architecture-specific implementation of ELF dynamic
  17. * relocation function.
  18. * The source code is imported and modified from the GNU C Library.
  19. */
  20. #ifndef DL_MACHINE_H
  21. #define DL_MACHINE_H
  22. #define ELF_MACHINE_NAME "x86_64"
  23. #include <sysdeps/generic/ldsodefs.h>
  24. #include "pal_internal.h"
  25. #include "pal_rtld.h"
  26. /* The x86-64 never uses Elf64_Rel relocations. */
  27. #define ELF_MACHINE_NO_REL 1
  28. /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
  29. MAP is the object containing the reloc. */
  30. //#define DEBUG_RELOC
  31. static void
  32. elf_machine_rela (struct link_map *l, Elf64_Rela *reloc, Elf64_Sym *sym,
  33. void *const reloc_addr_arg)
  34. {
  35. Elf64_Addr *const reloc_addr = reloc_addr_arg;
  36. const unsigned long int r_type = ELF64_R_TYPE (reloc->r_info);
  37. const char * __attribute_unused strtab =
  38. (const void *) D_PTR (l->l_info[DT_STRTAB]);
  39. #ifdef DEBUG_RELOC
  40. #define debug_reloc(r_type) \
  41. do { \
  42. if (strtab && sym && sym->st_name) \
  43. printf("%p " #r_type ": %s %p\n", reloc_addr, \
  44. strtab + sym->st_name, value); \
  45. else if (value) \
  46. printf("%p " #r_type ": %p\n", reloc_addr, value); \
  47. else \
  48. printf("%p " #r_type "\n", reloc_addr, value); \
  49. } while (0)
  50. #else
  51. #define debug_reloc(...) do {} while (0)
  52. #endif
  53. if (__builtin_expect (r_type == R_X86_64_RELATIVE, 0)) {
  54. /* This is defined in rtld.c, but nowhere in the static libc.a;
  55. make the reference weak so static programs can still link.
  56. This declaration cannot be done when compiling rtld.c
  57. (i.e. #ifdef RTLD_BOOTSTRAP) because rtld.c contains the
  58. common defn for _dl_rtld_map, which is incompatible with a
  59. weak decl in the same file. */
  60. //*reloc_addr = l->l_addr + reloc->r_addend;
  61. return;
  62. }
  63. if (__builtin_expect (r_type == R_X86_64_NONE, 0))
  64. return;
  65. Elf64_Addr value = l->l_addr + sym->st_value;
  66. #ifndef RTLD_BOOTSTRAP
  67. struct link_map * sym_map = 0;
  68. if (sym->st_shndx == SHN_UNDEF) {
  69. value = RESOLVE_RTLD(strtab + sym->st_name);
  70. if (!value) {
  71. sym_map = RESOLVE_MAP(&strtab, &sym);
  72. if (!sym_map)
  73. return;
  74. assert(sym);
  75. value = sym_map->l_addr + sym->st_value;
  76. }
  77. #if CACHE_LOADED_BINARIES == 1
  78. if (!sym_map || sym_map->l_type == OBJECT_RTLD) {
  79. assert(l->nrelocs < NRELOCS);
  80. l->relocs[l->nrelocs++] = reloc_addr;
  81. }
  82. #endif
  83. }
  84. #endif
  85. if (__builtin_expect (ELFW(ST_TYPE) (sym->st_info) == STT_GNU_IFUNC, 0)
  86. && __builtin_expect (sym->st_shndx != SHN_UNDEF, 1))
  87. value = ((Elf64_Addr (*) (void)) value) ();
  88. /* In the libc loader, they guaranteed that only R_ARCH_RELATIVE,
  89. R_ARCH_GLOB_DAT, R_ARCH_JUMP_SLOT appear in ld.so. We observed
  90. the same thing in libpal.so, so we are gonna to make the same
  91. assumption */
  92. switch (r_type) {
  93. case R_X86_64_GLOB_DAT:
  94. debug_reloc(R_X86_64_GLOB_DAT);
  95. *reloc_addr = value + reloc->r_addend;
  96. break;
  97. case R_X86_64_JUMP_SLOT:
  98. debug_reloc(R_X86_64_JUMP_SLOT);
  99. *reloc_addr = value + reloc->r_addend;
  100. break;
  101. #ifndef RTLD_BOOTSTRAP
  102. case R_X86_64_64:
  103. debug_reloc(R_X86_64_64);
  104. *reloc_addr = value + reloc->r_addend;
  105. break;
  106. case R_X86_64_32:
  107. debug_reloc(R_X86_64_32);
  108. value += reloc->r_addend;
  109. *(Elf64_Addr *) reloc_addr = value;
  110. break;
  111. /* Not needed for dl-conflict.c. */
  112. case R_X86_64_PC32:
  113. debug_reloc(R_X86_64_PC32);
  114. value += reloc->r_addend - (Elf64_Addr) reloc_addr;
  115. *(Elf64_Addr *) reloc_addr = value;
  116. break;
  117. case R_X86_64_IRELATIVE:
  118. debug_reloc(R_X86_64_IRELATIVE);
  119. value = sym_map->l_addr + reloc->r_addend;
  120. value = ((Elf64_Addr (*) (void)) value) ();
  121. *reloc_addr = value;
  122. break;
  123. #endif
  124. default:
  125. return;
  126. }
  127. #ifndef RTLD_BOOTSTRAP
  128. /* We have relocated the symbol, we don't want the
  129. interpreter to relocate it again. */
  130. reloc->r_info ^= ELF64_R_TYPE (reloc->r_info);
  131. #endif
  132. }
  133. static void
  134. elf_machine_rela_relative (struct link_map *l, const Elf64_Rela *reloc,
  135. void *const reloc_addr_arg)
  136. {
  137. Elf64_Addr *const reloc_addr = reloc_addr_arg;
  138. assert (ELF64_R_TYPE (reloc->r_info) == R_X86_64_RELATIVE);
  139. *reloc_addr = l->l_addr + reloc->r_addend;
  140. }
  141. #endif /* !DL_MACHINE_H */