dl-machine-x86_64.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 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 Lesser 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 Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser 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 <sysdeps/generic/ldsodefs.h>
  26. #include "pal_internal.h"
  27. /* The x86-64 never uses Elf64_Rel relocations. */
  28. #define ELF_MACHINE_NO_REL 1
  29. /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
  30. MAP is the object containing the reloc. */
  31. //#define DEBUG_RELOC
  32. static void
  33. elf_machine_rela (struct link_map *l, Elf64_Rela *reloc, Elf64_Sym *sym,
  34. void *const reloc_addr_arg)
  35. {
  36. Elf64_Addr *const reloc_addr = reloc_addr_arg;
  37. const unsigned long int r_type = ELF64_R_TYPE (reloc->r_info);
  38. const char * __attribute_unused strtab =
  39. (const void *) D_PTR (l->l_info[DT_STRTAB]);
  40. #ifdef DEBUG_RELOC
  41. #define debug_reloc(r_type) \
  42. do { \
  43. if (strtab && sym && sym->st_name) \
  44. printf("%p " #r_type ": %s %p\n", reloc_addr, \
  45. strtab + sym->st_name, value); \
  46. else if (value) \
  47. printf("%p " #r_type ": %p\n", reloc_addr, value); \
  48. else \
  49. printf("%p " #r_type "\n", reloc_addr, value); \
  50. } while (0)
  51. #else
  52. #define debug_reloc(...) do {} while (0)
  53. #endif
  54. if (__builtin_expect (r_type == R_X86_64_RELATIVE, 0)) {
  55. /* This is defined in rtld.c, but nowhere in the static libc.a;
  56. make the reference weak so static programs can still link.
  57. This declaration cannot be done when compiling rtld.c
  58. (i.e. #ifdef RTLD_BOOTSTRAP) because rtld.c contains the
  59. common defn for _dl_rtld_map, which is incompatible with a
  60. weak decl in the same file. */
  61. //*reloc_addr = l->l_addr + reloc->r_addend;
  62. return;
  63. }
  64. if (__builtin_expect (r_type == R_X86_64_NONE, 0))
  65. return;
  66. Elf64_Addr value = l->l_addr + sym->st_value;
  67. #ifndef RTLD_BOOTSTRAP
  68. struct link_map * sym_map = 0;
  69. if (sym->st_shndx == SHN_UNDEF) {
  70. value = RESOLVE_RTLD(strtab + sym->st_name);
  71. if (!value) {
  72. sym_map = RESOLVE_MAP(&strtab, &sym);
  73. if (!sym_map)
  74. return;
  75. assert(sym);
  76. value = sym_map->l_addr + sym->st_value;
  77. }
  78. #if CACHE_LOADED_BINARIES == 1
  79. if (!sym_map || sym_map->l_type == OBJECT_RTLD) {
  80. assert(l->nrelocs < NRELOCS);
  81. l->relocs[l->nrelocs++] = reloc_addr;
  82. }
  83. #endif
  84. }
  85. #endif
  86. if (__builtin_expect (ELFW(ST_TYPE) (sym->st_info) == STT_GNU_IFUNC, 0)
  87. && __builtin_expect (sym->st_shndx != SHN_UNDEF, 1))
  88. value = ((Elf64_Addr (*) (void)) value) ();
  89. /* In the libc loader, they guaranteed that only R_ARCH_RELATIVE,
  90. R_ARCH_GLOB_DAT, R_ARCH_JUMP_SLOT appear in ld.so. We observed
  91. the same thing in libpal.so, so we are gonna to make the same
  92. assumption */
  93. switch (r_type) {
  94. case R_X86_64_GLOB_DAT:
  95. debug_reloc(R_X86_64_GLOB_DAT);
  96. *reloc_addr = value + reloc->r_addend;
  97. break;
  98. case R_X86_64_JUMP_SLOT:
  99. debug_reloc(R_X86_64_JUMP_SLOT);
  100. *reloc_addr = value + reloc->r_addend;
  101. break;
  102. #ifndef RTLD_BOOTSTRAP
  103. case R_X86_64_64:
  104. debug_reloc(R_X86_64_64);
  105. *reloc_addr = value + reloc->r_addend;
  106. break;
  107. case R_X86_64_32:
  108. debug_reloc(R_X86_64_32);
  109. value += reloc->r_addend;
  110. *(Elf64_Addr *) reloc_addr = value;
  111. break;
  112. /* Not needed for dl-conflict.c. */
  113. case R_X86_64_PC32:
  114. debug_reloc(R_X86_64_PC32);
  115. value += reloc->r_addend - (Elf64_Addr) reloc_addr;
  116. *(Elf64_Addr *) reloc_addr = value;
  117. break;
  118. case R_X86_64_IRELATIVE:
  119. debug_reloc(R_X86_64_IRELATIVE);
  120. value = sym_map->l_addr + reloc->r_addend;
  121. value = ((Elf64_Addr (*) (void)) value) ();
  122. *reloc_addr = value;
  123. break;
  124. #endif
  125. default:
  126. return;
  127. }
  128. #ifndef RTLD_BOOTSTRAP
  129. /* We have relocated the symbol, we don't want the
  130. interpreter to relocate it again. */
  131. reloc->r_info ^= ELF64_R_TYPE (reloc->r_info);
  132. #endif
  133. }
  134. static void
  135. elf_machine_rela_relative (struct link_map *l, const Elf64_Rela *reloc,
  136. void *const reloc_addr_arg)
  137. {
  138. Elf64_Addr *const reloc_addr = reloc_addr_arg;
  139. assert (ELF64_R_TYPE (reloc->r_info) == R_X86_64_RELATIVE);
  140. *reloc_addr = l->l_addr + reloc->r_addend;
  141. }
  142. #endif /* !dl_machine_h */