dl-sysdep.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Various assembly language/system dependent hacks that are required
  3. * so that we can minimize the amount of platform specific code.
  4. * Copyright (C) 2000-2004 by Erik Andersen <andersen@codepoet.org>
  5. * Copyright (C) 2017 by Waldemar Brodkorb <wbx@uclibc-ng.org>
  6. * Ported from GNU C Library
  7. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  8. */
  9. /* Copyright (C) 1995-2016 Free Software Foundation, Inc.
  10. The GNU C Library is free software; you can redistribute it and/or
  11. modify it under the terms of the GNU Lesser General Public License as
  12. published by the Free Software Foundation; either version 2.1 of the
  13. License, or (at your option) any later version.
  14. The GNU C Library is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. Lesser General Public License for more details.
  18. You should have received a copy of the GNU Lesser General Public
  19. License along with the GNU C Library; if not, see
  20. <http://www.gnu.org/licenses/>. */
  21. /* Define this if the system uses RELOCA. */
  22. #define ELF_USES_RELOCA
  23. #include <elf.h>
  24. #include <link.h>
  25. /* Initialization sequence for the GOT. */
  26. #define INIT_GOT(GOT_BASE,MODULE) \
  27. { \
  28. GOT_BASE[2] = (unsigned long) _dl_linux_resolve; \
  29. GOT_BASE[1] = (unsigned long) MODULE; \
  30. }
  31. /* Here we define the magic numbers that this dynamic loader should accept */
  32. #define MAGIC1 EM_AARCH64
  33. #undef MAGIC2
  34. /* Used for error messages */
  35. #define ELF_TARGET "aarch64"
  36. struct elf_resolve;
  37. unsigned long _dl_linux_resolver(struct elf_resolve * tpnt, int reloc_entry);
  38. #define elf_machine_type_class(type) \
  39. ((((type) == R_AARCH64_JUMP_SLOT \
  40. || (type) == R_AARCH64_TLS_DTPMOD \
  41. || (type) == R_AARCH64_TLS_DTPREL \
  42. || (type) == R_AARCH64_TLS_TPREL \
  43. || (type) == R_AARCH64_TLSDESC) * ELF_RTYPE_CLASS_PLT) \
  44. | (((type) == R_AARCH64_COPY) * ELF_RTYPE_CLASS_COPY))
  45. /* Return the link-time address of _DYNAMIC. Conveniently, this is the
  46. first element of the GOT. */
  47. extern const ElfW(Addr) _GLOBAL_OFFSET_TABLE_[] attribute_hidden;
  48. static __always_inline ElfW(Addr) __attribute__ ((unused))
  49. elf_machine_dynamic (void)
  50. {
  51. return _GLOBAL_OFFSET_TABLE_[0];
  52. }
  53. /* Return the run-time load address of the shared object. */
  54. static __always_inline ElfW(Addr) __attribute__ ((unused))
  55. elf_machine_load_address (void)
  56. {
  57. /* To figure out the load address we use the definition that for any symbol:
  58. dynamic_addr(symbol) = static_addr(symbol) + load_addr
  59. The choice of symbol is arbitrary. The static address we obtain
  60. by constructing a non GOT reference to the symbol, the dynamic
  61. address of the symbol we compute using adrp/add to compute the
  62. symbol's address relative to the PC.
  63. This depends on 32/16bit relocations being resolved at link time
  64. and that the static address fits in the 32/16 bits. */
  65. ElfW(Addr) static_addr;
  66. ElfW(Addr) dynamic_addr;
  67. __asm__(" \n"
  68. " adrp %1, _dl_start; \n"
  69. " add %1, %1, #:lo12:_dl_start \n"
  70. " ldr %w0, 1f \n"
  71. " b 2f \n"
  72. "1: \n"
  73. " .word _dl_start \n"
  74. "2: \n"
  75. : "=r" (static_addr), "=r" (dynamic_addr));
  76. return dynamic_addr - static_addr;
  77. }
  78. static __always_inline void
  79. elf_machine_relative(Elf64_Addr load_off, const Elf64_Addr rel_addr,
  80. Elf64_Word relative_count)
  81. {
  82. Elf64_Rela *rpnt = (Elf64_Rela*)rel_addr;
  83. --rpnt;
  84. do {
  85. Elf64_Addr *const reloc_addr = (Elf64_Addr*)(load_off + (++rpnt)->r_offset);
  86. *reloc_addr = load_off + rpnt->r_addend;
  87. } while (--relative_count);
  88. }