dl-machine-x86_64.h 4.4 KB

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