dl-sysdep.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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, write to the Free
  17. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  18. 02111-1307 USA. */
  19. /* Define this if the system uses RELOCA. */
  20. #define ELF_USES_RELOCA
  21. #include <elf.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. #define do_rem(result, n, base) ((result) = (n) % (base))
  36. /* 4096 bytes alignment */
  37. #define PAGE_ALIGN 0xfffff000
  38. #define ADDR_ALIGN 0xfff
  39. #define OFFS_ALIGN 0x7ffff000
  40. /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or
  41. TLS variable, so undefined references should not be allowed to
  42. define the value.
  43. ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
  44. of the main executable's symbols, as for a COPY reloc. */
  45. #define elf_machine_type_class(type) \
  46. ((((type) == R_X86_64_JUMP_SLOT) * ELF_RTYPE_CLASS_PLT) \
  47. | (((type) == R_X86_64_COPY) * ELF_RTYPE_CLASS_COPY))
  48. /* Return the link-time address of _DYNAMIC. Conveniently, this is the
  49. first element of the GOT. This must be inlined in a function which
  50. uses global data. */
  51. static __always_inline Elf64_Addr __attribute__ ((unused))
  52. elf_machine_dynamic (void)
  53. {
  54. Elf64_Addr addr;
  55. #if 0
  56. /* This works because we have our GOT address available in the small PIC
  57. model. */
  58. addr = (Elf64_Addr) &_DYNAMIC;
  59. return addr;
  60. #else
  61. asm("mov 0(%%rip), %0"
  62. : "=r" (addr));
  63. return addr;
  64. #endif
  65. }
  66. /* Return the run-time load address of the shared object. */
  67. static __always_inline Elf64_Addr __attribute__ ((unused))
  68. elf_machine_load_address (void)
  69. {
  70. register Elf64_Addr addr, tmp;
  71. /* The easy way is just the same as on x86:
  72. leaq _dl_start, %0
  73. leaq _dl_start(%%rip), %1
  74. subq %0, %1
  75. but this does not work with binutils since we then have
  76. a R_X86_64_32S relocation in a shared lib.
  77. Instead we store the address of _dl_start in the data section
  78. and compare it with the current value that we can get via
  79. an RIP relative addressing mode. */
  80. asm ("movq 1f(%%rip), %1\n"
  81. "0:\tleaq _dl_start(%%rip), %0\n\t"
  82. "subq %1, %0\n\t"
  83. ".section\t.data\n"
  84. "1:\t.quad _dl_start\n\t"
  85. ".previous\n\t"
  86. : "=r" (addr), "=r" (tmp) : : "cc");
  87. return addr;
  88. }
  89. static __always_inline void
  90. elf_machine_relative (Elf64_Addr load_off, const Elf64_Addr rel_addr,
  91. Elf64_Word relative_count)
  92. {
  93. Elf64_Rel * rpnt = (void *) rel_addr;
  94. --rpnt;
  95. do {
  96. Elf64_Addr *const reloc_addr = (void *) (load_off + (++rpnt)->r_offset);
  97. *reloc_addr += load_off;
  98. } while (--relative_count);
  99. }