dl-machine-x86_64.h 4.7 KB

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