dl-machine-x86_64.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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.c
  15. *
  16. * This file contains x64-specific codes for relocating ELF binaries.
  17. * Most of the source codes are imported from GNU C library.
  18. */
  19. #ifndef __DL_MACHINE_H__
  20. #define __DL_MACHINE_H__
  21. #define ELF_MACHINE_NAME "x86_64"
  22. #include "ldsodefs.h"
  23. /* Return nonzero iff ELF header is compatible with the running host. */
  24. static inline int __attribute__ ((unused))
  25. elf_machine_matches_host (const Elf64_Ehdr * ehdr)
  26. {
  27. return ehdr->e_machine == EM_X86_64;
  28. }
  29. /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or
  30. TLS variable, so undefined references should not be allowed to
  31. define the value.
  32. ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
  33. of the main executable's symbols, as for a COPY reloc. */
  34. # define elf_machine_type_class(type) \
  35. ((((type) == R_X86_64_JUMP_SLOT \
  36. || (type) == R_X86_64_DTPMOD64 \
  37. || (type) == R_X86_64_DTPOFF64 \
  38. || (type) == R_X86_64_TPOFF64 \
  39. || (type) == R_X86_64_TLSDESC) \
  40. * ELF_RTYPE_CLASS_PLT) \
  41. | (((type) == R_X86_64_COPY) * ELF_RTYPE_CLASS_COPY))
  42. /* The x86-64 never uses Elf64_Rel relocations. */
  43. #define ELF_MACHINE_NO_REL 1
  44. /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
  45. MAP is the object containing the reloc. */
  46. //#define DEBUG_RELOC
  47. static bool
  48. elf_machine_rela (struct link_map * l, ElfW(Rela) * reloc, Elf64_Sym * sym,
  49. void * const reloc_addr_arg)
  50. {
  51. Elf64_Addr * const reloc_addr = reloc_addr_arg;
  52. const unsigned long int r_type = ELF64_R_TYPE (reloc->r_info);
  53. const char * __attribute__ ((unused)) strtab =
  54. (const void *) D_PTR (l->l_info[DT_STRTAB]);
  55. #ifdef DEBUG_RELOC
  56. #define debug_reloc(r_type, sym, value) \
  57. ({ if (strtab && (sym) && (sym)->st_name) \
  58. debug(#r_type ": %s\n", strtab + (sym)->st_name); \
  59. else if (value) \
  60. debug(#r_type ": %p\n", (value)); \
  61. else \
  62. debug(#r_type "\n", (value)); \
  63. })
  64. #else
  65. #define debug_reloc(...) ({})
  66. #endif
  67. if (r_type == R_X86_64_RELATIVE || r_type == R_X86_64_NONE)
  68. return false;
  69. Elf64_Sym * refsym = sym;
  70. Elf64_Addr value;
  71. Elf64_Addr sym_map = RESOLVE_MAP(&strtab, &sym);
  72. if (!sym_map || !sym || refsym == sym)
  73. return false;
  74. value = sym_map + sym->st_value;
  75. /* We do a very special relocation for loaded libraries */
  76. PROTECT_PAGE(l, refsym, sizeof(*refsym));
  77. PROTECT_PAGE(l, reloc_addr, sizeof(*reloc_addr));
  78. refsym->st_info = sym->st_info;
  79. refsym->st_size = sym->st_size;
  80. if (__builtin_expect (ELFW(ST_TYPE) (sym->st_info)
  81. == STT_GNU_IFUNC, 0)
  82. && __builtin_expect (sym->st_shndx != SHN_UNDEF, 1)) {
  83. value = ((Elf64_Addr (*) (void)) value) ();
  84. refsym->st_info ^= ELFW(ST_TYPE)(sym->st_info);
  85. refsym->st_info |= STT_FUNC;
  86. }
  87. debug_reloc("shim symbol", sym, value);
  88. refsym->st_value = value - l->l_addr;
  89. *reloc_addr = value +
  90. ((r_type == R_X86_64_GLOB_DAT ||
  91. r_type == R_X86_64_JUMP_SLOT ||
  92. r_type == R_X86_64_64) ? reloc->r_addend : 0);
  93. /* We have relocated the symbol, we don't want the
  94. interpreter to relocate it again. */
  95. if (r_type != R_X86_64_NONE) {
  96. PROTECT_PAGE(l, reloc, sizeof(*reloc));
  97. reloc->r_info = (reloc->r_info ^ ELF64_R_TYPE (reloc->r_info))|
  98. R_X86_64_NONE;
  99. }
  100. return true;
  101. }
  102. #endif /* !DL_MACHINE_H */