dl-sysdep.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #include <link.h>
  23. /* Initialization sequence for the GOT. */
  24. #define INIT_GOT(GOT_BASE,MODULE) \
  25. do { \
  26. GOT_BASE[2] = (unsigned long) _dl_linux_resolve; \
  27. GOT_BASE[1] = (unsigned long) MODULE; \
  28. } while(0)
  29. /* Here we define the magic numbers that this dynamic loader should accept */
  30. #define MAGIC1 EM_X86_64
  31. #undef MAGIC2
  32. /* Used for error messages */
  33. #define ELF_TARGET "x86_64"
  34. struct elf_resolve;
  35. extern unsigned long _dl_linux_resolver(struct elf_resolve * tpnt, int reloc_entry);
  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. /* This works because we have our GOT address available in the small PIC
  56. model. */
  57. addr = (Elf64_Addr) &_DYNAMIC;
  58. return addr;
  59. }
  60. /* Return the run-time load address of the shared object. */
  61. static __always_inline Elf64_Addr __attribute__ ((unused))
  62. elf_machine_load_address (void)
  63. {
  64. register Elf64_Addr addr, tmp;
  65. /* The easy way is just the same as on x86:
  66. leaq _dl_start, %0
  67. leaq _dl_start(%%rip), %1
  68. subq %0, %1
  69. but this does not work with binutils since we then have
  70. a R_X86_64_32S relocation in a shared lib.
  71. Instead we store the address of _dl_start in the data section
  72. and compare it with the current value that we can get via
  73. an RIP relative addressing mode. */
  74. __asm__ ("movq 1f(%%rip), %1\n"
  75. "0:\tleaq _dl_start(%%rip), %0\n\t"
  76. "subq %1, %0\n\t"
  77. ".section\t.data\n"
  78. "1:\t.quad _dl_start\n\t"
  79. ".previous\n\t"
  80. : "=r" (addr), "=r" (tmp) : : "cc");
  81. return addr;
  82. }
  83. static __always_inline void
  84. elf_machine_relative(Elf64_Addr load_off, const Elf64_Addr rel_addr,
  85. Elf64_Word relative_count)
  86. {
  87. Elf64_Rela *rpnt = (Elf64_Rela*)rel_addr;
  88. --rpnt;
  89. do {
  90. Elf64_Addr *const reloc_addr = (Elf64_Addr*)(load_off + (++rpnt)->r_offset);
  91. *reloc_addr = load_off + rpnt->r_addend;
  92. } while (--relative_count);
  93. }