dl.c 4.7 KB

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