dl-sysdep.h 3.8 KB

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