dl-sysdep.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* yoinked from glibc/sysdeps/x86_64/dl-machine.h */
  2. /* Machine-dependent ELF dynamic relocation inline functions. x86-64 version.
  3. Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. Contributed by Andreas Jaeger <aj@suse.de>.
  6. The GNU C Library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Lesser General Public
  8. License as published by the Free Software Foundation; either
  9. version 2.1 of the License, or (at your option) any later version.
  10. The GNU C Library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with the GNU C Library; if not, see
  16. <http://www.gnu.org/licenses/>. */
  17. /* Define this if the system uses RELOCA. */
  18. #define ELF_USES_RELOCA
  19. #include <elf.h>
  20. #include <link.h>
  21. /* Initialization sequence for the GOT. */
  22. #define INIT_GOT(GOT_BASE,MODULE) \
  23. do { \
  24. GOT_BASE[2] = (unsigned long) _dl_linux_resolve; \
  25. GOT_BASE[1] = (unsigned long) MODULE; \
  26. } while(0)
  27. /* Here we define the magic numbers that this dynamic loader should accept */
  28. #define MAGIC1 EM_X86_64
  29. #undef MAGIC2
  30. /* Used for error messages */
  31. #define ELF_TARGET "x86_64"
  32. struct elf_resolve;
  33. extern unsigned long _dl_linux_resolver(struct elf_resolve * tpnt, int reloc_entry);
  34. /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or
  35. TLS variable, so undefined references should not be allowed to
  36. define the value.
  37. ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
  38. of the main executable's symbols, as for a COPY reloc. */
  39. #define elf_machine_type_class(type) \
  40. ((((type) == R_X86_64_JUMP_SLOT \
  41. || (type) == R_X86_64_DTPMOD64 \
  42. || (type) == R_X86_64_DTPOFF64 \
  43. || (type) == R_X86_64_TPOFF64) \
  44. * ELF_RTYPE_CLASS_PLT) \
  45. | (((type) == R_X86_64_COPY) * ELF_RTYPE_CLASS_COPY))
  46. /* Return the link-time address of _DYNAMIC. Conveniently, this is the
  47. first element of the GOT. This must be inlined in a function which
  48. uses global data. */
  49. static __always_inline Elf64_Addr __attribute__ ((unused))
  50. elf_machine_dynamic (void)
  51. {
  52. Elf64_Addr addr;
  53. /* This works because we have our GOT address available in the small PIC
  54. model. */
  55. addr = (Elf64_Addr) &_DYNAMIC;
  56. return addr;
  57. }
  58. /* Return the run-time load address of the shared object. */
  59. static __always_inline Elf64_Addr __attribute__ ((unused))
  60. elf_machine_load_address (void)
  61. {
  62. register Elf64_Addr addr, tmp;
  63. /* The easy way is just the same as on x86:
  64. leaq _dl_start, %0
  65. leaq _dl_start(%%rip), %1
  66. subq %0, %1
  67. but this does not work with binutils since we then have
  68. a R_X86_64_32S relocation in a shared lib.
  69. Instead we store the address of _dl_start in the data section
  70. and compare it with the current value that we can get via
  71. an RIP relative addressing mode. */
  72. __asm__ ("movq 1f(%%rip), %1\n"
  73. "0:\tleaq _dl_start(%%rip), %0\n\t"
  74. "subq %1, %0\n\t"
  75. ".section\t.data\n"
  76. "1:\t.quad _dl_start\n\t"
  77. ".previous\n\t"
  78. : "=r" (addr), "=r" (tmp) : : "cc");
  79. return addr;
  80. }
  81. static __always_inline void
  82. elf_machine_relative(Elf64_Addr load_off, const Elf64_Addr rel_addr,
  83. Elf64_Word relative_count)
  84. {
  85. Elf64_Rela *rpnt = (Elf64_Rela*)rel_addr;
  86. --rpnt;
  87. do {
  88. Elf64_Addr *const reloc_addr = (Elf64_Addr*)(load_off + (++rpnt)->r_offset);
  89. *reloc_addr = load_off + rpnt->r_addend;
  90. } while (--relative_count);
  91. }