dl-sysdep.h 3.8 KB

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