dl.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* dl.c - arch-dependent part of loadable module support */
  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/cpu/reloc.h>
  26. #define LDR 0x58000050
  27. #define BR 0xd61f0200
  28. /*
  29. * Check if EHDR is a valid ELF header.
  30. */
  31. grub_err_t
  32. grub_arch_dl_check_header (void *ehdr)
  33. {
  34. Elf_Ehdr *e = ehdr;
  35. /* Check the magic numbers. */
  36. if (e->e_ident[EI_CLASS] != ELFCLASS64
  37. || e->e_ident[EI_DATA] != ELFDATA2LSB || e->e_machine != EM_AARCH64)
  38. return grub_error (GRUB_ERR_BAD_OS,
  39. N_("invalid arch-dependent ELF magic"));
  40. return GRUB_ERR_NONE;
  41. }
  42. #pragma GCC diagnostic ignored "-Wcast-align"
  43. /*
  44. * Unified function for both REL and RELA
  45. */
  46. grub_err_t
  47. grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
  48. Elf_Shdr *s, grub_dl_segment_t seg)
  49. {
  50. Elf_Rel *rel, *max;
  51. unsigned unmatched_adr_got_page = 0;
  52. for (rel = (Elf_Rel *) ((char *) ehdr + s->sh_offset),
  53. max = (Elf_Rel *) ((char *) rel + s->sh_size);
  54. rel < max;
  55. rel = (Elf_Rel *) ((char *) rel + s->sh_entsize))
  56. {
  57. Elf_Sym *sym;
  58. void *place;
  59. grub_uint64_t sym_addr;
  60. if (rel->r_offset >= seg->size)
  61. return grub_error (GRUB_ERR_BAD_MODULE,
  62. "reloc offset is out of the segment");
  63. sym = (Elf_Sym *) ((char *) mod->symtab
  64. + mod->symsize * ELF_R_SYM (rel->r_info));
  65. sym_addr = sym->st_value;
  66. if (s->sh_type == SHT_RELA)
  67. sym_addr += ((Elf_Rela *) rel)->r_addend;
  68. place = (void *) ((grub_addr_t) seg->addr + rel->r_offset);
  69. switch (ELF_R_TYPE (rel->r_info))
  70. {
  71. case R_AARCH64_ABS64:
  72. {
  73. grub_uint64_t *abs_place = place;
  74. grub_dprintf ("dl", " reloc_abs64 %p => 0x%016llx\n",
  75. place, (unsigned long long) sym_addr);
  76. *abs_place = (grub_uint64_t) sym_addr;
  77. }
  78. break;
  79. case R_AARCH64_ADD_ABS_LO12_NC:
  80. grub_arm64_set_abs_lo12 (place, sym_addr);
  81. break;
  82. case R_AARCH64_LDST64_ABS_LO12_NC:
  83. grub_arm64_set_abs_lo12_ldst64 (place, sym_addr);
  84. break;
  85. case R_AARCH64_CALL26:
  86. case R_AARCH64_JUMP26:
  87. {
  88. grub_int64_t offset = sym_addr - (grub_uint64_t) place;
  89. if (!grub_arm_64_check_xxxx26_offset (offset))
  90. {
  91. struct grub_arm64_trampoline *tp = mod->trampptr;
  92. mod->trampptr = tp + 1;
  93. tp->ldr = LDR;
  94. tp->br = BR;
  95. tp->addr = sym_addr;
  96. offset = (grub_uint8_t *) tp - (grub_uint8_t *) place;
  97. }
  98. if (!grub_arm_64_check_xxxx26_offset (offset))
  99. return grub_error (GRUB_ERR_BAD_MODULE,
  100. "trampoline out of range");
  101. grub_arm64_set_xxxx26_offset (place, offset);
  102. }
  103. break;
  104. case R_AARCH64_PREL32:
  105. {
  106. grub_int64_t value;
  107. Elf64_Word *addr32 = place;
  108. value = ((grub_int32_t) *addr32) + sym_addr -
  109. (Elf64_Xword) (grub_addr_t) seg->addr - rel->r_offset;
  110. if (value != (grub_int32_t) value)
  111. return grub_error (GRUB_ERR_BAD_MODULE, "relocation out of range");
  112. grub_dprintf("dl", " reloc_prel32 %p => 0x%016llx\n",
  113. place, (unsigned long long) sym_addr);
  114. *addr32 = value;
  115. }
  116. break;
  117. case R_AARCH64_ADR_GOT_PAGE:
  118. {
  119. grub_uint64_t *gp = mod->gotptr;
  120. Elf_Rela *rel2;
  121. grub_int64_t gpoffset = ((grub_uint64_t) gp & ~0xfffULL) - (((grub_uint64_t) place) & ~0xfffULL);
  122. *gp = (grub_uint64_t) sym_addr;
  123. mod->gotptr = gp + 1;
  124. unmatched_adr_got_page++;
  125. grub_dprintf("dl", " reloc_got %p => 0x%016llx (0x%016llx)\n",
  126. place, (unsigned long long) sym_addr, (unsigned long long) gp);
  127. if (!grub_arm64_check_hi21_signed (gpoffset))
  128. return grub_error (GRUB_ERR_BAD_MODULE,
  129. "HI21 out of range");
  130. grub_arm64_set_hi21(place, gpoffset);
  131. for (rel2 = (Elf_Rela *) ((char *) rel + s->sh_entsize);
  132. rel2 < (Elf_Rela *) max;
  133. rel2 = (Elf_Rela *) ((char *) rel2 + s->sh_entsize))
  134. if (ELF_R_SYM (rel2->r_info)
  135. == ELF_R_SYM (rel->r_info)
  136. && ((Elf_Rela *) rel)->r_addend == rel2->r_addend
  137. && ELF_R_TYPE (rel2->r_info) == R_AARCH64_LD64_GOT_LO12_NC)
  138. {
  139. grub_arm64_set_abs_lo12_ldst64 ((void *) ((grub_addr_t) seg->addr + rel2->r_offset),
  140. (grub_uint64_t)gp);
  141. break;
  142. }
  143. if (rel2 >= (Elf_Rela *) max)
  144. return grub_error (GRUB_ERR_BAD_MODULE,
  145. "ADR_GOT_PAGE without matching LD64_GOT_LO12_NC");
  146. }
  147. break;
  148. case R_AARCH64_LD64_GOT_LO12_NC:
  149. if (unmatched_adr_got_page == 0)
  150. return grub_error (GRUB_ERR_BAD_MODULE,
  151. "LD64_GOT_LO12_NC without matching ADR_GOT_PAGE");
  152. unmatched_adr_got_page--;
  153. break;
  154. case R_AARCH64_ADR_PREL_PG_HI21:
  155. {
  156. grub_int64_t offset = (sym_addr & ~0xfffULL) - (((grub_uint64_t) place) & ~0xfffULL);
  157. if (!grub_arm64_check_hi21_signed (offset))
  158. return grub_error (GRUB_ERR_BAD_MODULE,
  159. "HI21 out of range");
  160. grub_arm64_set_hi21 (place, offset);
  161. }
  162. break;
  163. default:
  164. {
  165. char rel_info[17]; /* log16(2^64) = 16, plus NUL. */
  166. grub_snprintf (rel_info, sizeof (rel_info) - 1, "%" PRIxGRUB_UINT64_T,
  167. ELF_R_TYPE (rel->r_info));
  168. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  169. N_("relocation 0x%s is not implemented yet"), rel_info);
  170. }
  171. }
  172. }
  173. return GRUB_ERR_NONE;
  174. }