dl-machine-x86_64.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 <sysdeps/generic/ldsodefs.h>
  27. #include "pal_internal.h"
  28. /* The x86-64 never uses Elf64_Rel relocations. */
  29. #define ELF_MACHINE_NO_REL 1
  30. /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
  31. MAP is the object containing the reloc. */
  32. //#define DEBUG_RELOC
  33. static void
  34. elf_machine_rela (struct link_map *l, Elf64_Rela *reloc, Elf64_Sym *sym,
  35. void *const reloc_addr_arg)
  36. {
  37. Elf64_Addr *const reloc_addr = reloc_addr_arg;
  38. const unsigned long int r_type = ELF64_R_TYPE (reloc->r_info);
  39. const char * __attribute__ ((unused)) strtab =
  40. (const void *) D_PTR (l->l_info[DT_STRTAB]);
  41. #ifdef DEBUG_RELOC
  42. #define debug_reloc(r_type) \
  43. do { \
  44. if (strtab && sym && sym->st_name) \
  45. printf("%p " #r_type ": %s %p\n", reloc_addr, \
  46. strtab + sym->st_name, value); \
  47. else if (value) \
  48. printf("%p " #r_type ": %p\n", reloc_addr, value); \
  49. else \
  50. printf("%p " #r_type "\n", reloc_addr, value); \
  51. } while (0)
  52. #else
  53. #define debug_reloc(...) do {} while (0)
  54. #endif
  55. if (__builtin_expect (r_type == R_X86_64_RELATIVE, 0)) {
  56. /* This is defined in rtld.c, but nowhere in the static libc.a;
  57. make the reference weak so static programs can still link.
  58. This declaration cannot be done when compiling rtld.c
  59. (i.e. #ifdef RTLD_BOOTSTRAP) because rtld.c contains the
  60. common defn for _dl_rtld_map, which is incompatible with a
  61. weak decl in the same file. */
  62. //*reloc_addr = l->l_addr + reloc->r_addend;
  63. return;
  64. }
  65. if (__builtin_expect (r_type == R_X86_64_NONE, 0))
  66. return;
  67. Elf64_Addr value = l->l_addr + sym->st_value;
  68. #ifndef RTLD_BOOTSTRAP
  69. Elf64_Sym * refsym = sym;
  70. Elf64_Addr sym_map = 0;
  71. if (sym->st_shndx == SHN_UNDEF) {
  72. value = RESOLVE_RTLD(strtab + sym->st_name);
  73. if (!value) {
  74. sym_map = RESOLVE_MAP(&strtab, &sym);
  75. if (!sym_map)
  76. return;
  77. assert(sym);
  78. value = sym_map + sym->st_value;
  79. }
  80. if (!sym_map || (void *) sym_map == pal_state.pal_addr) {
  81. assert(l->nrelocs < NRELOCS);
  82. l->relocs[l->nrelocs++] = reloc_addr;
  83. }
  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. #if 0
  119. case R_X86_64_COPY:
  120. debug_reloc(R_X86_64_COPY);
  121. int sym_size = sym ? sym->st_size : 0;
  122. int ref_size = refsym ? refsym->st_size : 0;
  123. memcpy (reloc_addr_arg, (void *) value, MIN (sym_size,
  124. ref_size));
  125. break;
  126. #endif
  127. case R_X86_64_IRELATIVE:
  128. debug_reloc(R_X86_64_IRELATIVE);
  129. value = sym_map + reloc->r_addend;
  130. value = ((Elf64_Addr (*) (void)) value) ();
  131. *reloc_addr = value;
  132. break;
  133. #endif
  134. default:
  135. return;
  136. }
  137. #ifndef RTLD_BOOTSTRAP
  138. /* We have relocated the symbol, we don't want the
  139. interpreter to relocate it again. */
  140. reloc->r_info ^= ELF64_R_TYPE (reloc->r_info);
  141. #endif
  142. }
  143. static void
  144. elf_machine_rela_relative (struct link_map *l, const Elf64_Rela *reloc,
  145. void *const reloc_addr_arg)
  146. {
  147. Elf64_Addr *const reloc_addr = reloc_addr_arg;
  148. assert (ELF64_R_TYPE (reloc->r_info) == R_X86_64_RELATIVE);
  149. *reloc_addr = l->l_addr + reloc->r_addend;
  150. }
  151. #endif /* !dl_machine_h */