dl_helper.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* dl_helper.c - relocation helper functions for modules and grub-mkimage */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2013 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB 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
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/dl.h>
  20. #include <grub/elf.h>
  21. #include <grub/misc.h>
  22. #include <grub/err.h>
  23. #include <grub/mm.h>
  24. #include <grub/i18n.h>
  25. #include <grub/arm64/reloc.h>
  26. /*
  27. * grub_arm64_reloc_xxxx26():
  28. *
  29. * JUMP26/CALL26 relocations for B and BL instructions.
  30. */
  31. int
  32. grub_arm_64_check_xxxx26_offset (grub_int64_t offset)
  33. {
  34. const grub_ssize_t offset_low = -(1 << 27), offset_high = (1 << 27) - 1;
  35. if ((offset < offset_low) || (offset > offset_high))
  36. return 0;
  37. return 1;
  38. }
  39. void
  40. grub_arm64_set_xxxx26_offset (grub_uint32_t *place, grub_int64_t offset)
  41. {
  42. const grub_uint32_t insmask = grub_cpu_to_le32_compile_time (0xfc000000);
  43. grub_dprintf ("dl", " reloc_xxxx64 %p %c= 0x%" PRIxGRUB_INT64_T "\n",
  44. place, offset > 0 ? '+' : '-',
  45. offset < 0 ? -offset : offset);
  46. *place &= insmask;
  47. *place |= grub_cpu_to_le32 (offset >> 2) & ~insmask;
  48. }
  49. int
  50. grub_arm64_check_hi21_signed (grub_int64_t offset)
  51. {
  52. if (offset != (grub_int64_t)(grub_int32_t)offset)
  53. return 0;
  54. return 1;
  55. }
  56. void
  57. grub_arm64_set_hi21 (grub_uint32_t *place, grub_int64_t offset)
  58. {
  59. const grub_uint32_t insmask = grub_cpu_to_le32_compile_time (0x9f00001f);
  60. grub_uint32_t val;
  61. offset >>= 12;
  62. val = ((offset & 3) << 29) | (((offset >> 2) & 0x7ffff) << 5);
  63. *place &= insmask;
  64. *place |= grub_cpu_to_le32 (val) & ~insmask;
  65. }
  66. void
  67. grub_arm64_set_abs_lo12 (grub_uint32_t *place, grub_int64_t target)
  68. {
  69. const grub_uint32_t insmask = grub_cpu_to_le32_compile_time (0xffc003ff);
  70. *place &= insmask;
  71. *place |= grub_cpu_to_le32 (target << 10) & ~insmask;
  72. }
  73. void
  74. grub_arm64_set_abs_lo12_ldst64 (grub_uint32_t *place, grub_int64_t target)
  75. {
  76. const grub_uint32_t insmask = grub_cpu_to_le32_compile_time (0xfff803ff);
  77. *place &= insmask;
  78. *place |= grub_cpu_to_le32 (target << 7) & ~insmask;
  79. }
  80. #pragma GCC diagnostic ignored "-Wcast-align"
  81. grub_err_t
  82. grub_arm64_dl_get_tramp_got_size (const void *ehdr, grub_size_t *tramp,
  83. grub_size_t *got)
  84. {
  85. const Elf64_Ehdr *e = ehdr;
  86. const Elf64_Shdr *s;
  87. unsigned i;
  88. *tramp = 0;
  89. *got = 0;
  90. for (i = 0, s = (Elf64_Shdr *) ((char *) e + grub_le_to_cpu64 (e->e_shoff));
  91. i < grub_le_to_cpu16 (e->e_shnum);
  92. i++, s = (Elf64_Shdr *) ((char *) s + grub_le_to_cpu16 (e->e_shentsize)))
  93. if (s->sh_type == grub_cpu_to_le32_compile_time (SHT_REL)
  94. || s->sh_type == grub_cpu_to_le32_compile_time (SHT_RELA))
  95. {
  96. const Elf64_Rela *rel, *max;
  97. for (rel = (Elf64_Rela *) ((char *) e + grub_le_to_cpu64 (s->sh_offset)),
  98. max = (const Elf64_Rela *) ((char *) rel + grub_le_to_cpu64 (s->sh_size));
  99. rel < max; rel = (const Elf64_Rela *) ((char *) rel + grub_le_to_cpu64 (s->sh_entsize)))
  100. switch (ELF64_R_TYPE (rel->r_info))
  101. {
  102. case R_AARCH64_CALL26:
  103. case R_AARCH64_JUMP26:
  104. *tramp += sizeof (struct grub_arm64_trampoline);
  105. break;
  106. case R_AARCH64_ADR_GOT_PAGE:
  107. *got += 8;
  108. break;
  109. }
  110. }
  111. return GRUB_ERR_NONE;
  112. }