dl-machine-x86_64.h 5.7 KB

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