dl.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* dl.c - arch-dependent part of loadable module support */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2002,2004,2005,2007,2009 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/i18n.h>
  24. /* Check if EHDR is a valid ELF header. */
  25. grub_err_t
  26. grub_arch_dl_check_header (void *ehdr)
  27. {
  28. Elf_Ehdr *e = ehdr;
  29. /* Check the magic numbers. */
  30. if (e->e_ident[EI_CLASS] != ELFCLASS64
  31. || e->e_ident[EI_DATA] != ELFDATA2MSB
  32. || e->e_machine != EM_SPARCV9)
  33. return grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-dependent ELF magic"));
  34. return GRUB_ERR_NONE;
  35. }
  36. #pragma GCC diagnostic ignored "-Wcast-align"
  37. struct trampoline
  38. {
  39. grub_uint8_t code[0x28];
  40. grub_uint64_t addr;
  41. };
  42. static const grub_uint8_t trampoline_code[0x28] =
  43. {
  44. /* 0: */ 0x82, 0x10, 0x00, 0x0f, /* mov %o7, %g1 */
  45. /* 4: */ 0x40, 0x00, 0x00, 0x02, /* call 0xc */
  46. /* 8: */ 0x01, 0x00, 0x00, 0x00, /* nop */
  47. /* c: */ 0x9e, 0x1b, 0xc0, 0x01, /* xor %o7, %g1, %o7 */
  48. /* 10: */ 0x82, 0x18, 0x40, 0x0f, /* xor %g1, %o7, %g1 */
  49. /* 14: */ 0x9e, 0x1b, 0xc0, 0x01, /* xor %o7, %g1, %o7 */
  50. /* 18: */ 0xc2, 0x58, 0x60, 0x24, /* ldx [ %g1 + 0x24 ], %g1 */
  51. /* 1c: */ 0x81, 0xc0, 0x40, 0x00, /* jmp %g1 */
  52. /* 20: */ 0x01, 0x00, 0x00, 0x00, /* nop */
  53. /* 24: */ 0x01, 0x00, 0x00, 0x00, /* nop */
  54. };
  55. grub_err_t
  56. grub_arch_dl_get_tramp_got_size (const void *ehdr, grub_size_t *tramp,
  57. grub_size_t *got)
  58. {
  59. const Elf_Ehdr *e = ehdr;
  60. const Elf_Shdr *s;
  61. unsigned i;
  62. *tramp = 0;
  63. *got = 0;
  64. for (i = 0, s = (const Elf_Shdr *) ((grub_addr_t) e + e->e_shoff);
  65. i < e->e_shnum;
  66. i++, s = (const Elf_Shdr *) ((grub_addr_t) s + e->e_shentsize))
  67. if (s->sh_type == SHT_REL || s->sh_type == SHT_RELA)
  68. {
  69. const Elf_Rel *rel, *max;
  70. for (rel = (const Elf_Rel *) ((grub_addr_t) e + s->sh_offset),
  71. max = rel + s->sh_size / s->sh_entsize;
  72. rel < max;
  73. rel = (const Elf_Rel *) ((grub_addr_t) rel + s->sh_entsize))
  74. switch (ELF_R_TYPE (rel->r_info))
  75. {
  76. case R_SPARC_WDISP30:
  77. {
  78. *tramp += sizeof (struct trampoline);
  79. break;
  80. }
  81. }
  82. }
  83. return GRUB_ERR_NONE;
  84. }
  85. /* Relocate symbols. */
  86. grub_err_t
  87. grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
  88. Elf_Shdr *s, grub_dl_segment_t seg)
  89. {
  90. Elf_Rela *rel, *max;
  91. for (rel = (Elf_Rela *) ((char *) ehdr + s->sh_offset),
  92. max = (Elf_Rela *) ((char *) rel + s->sh_size);
  93. rel < max;
  94. rel = (Elf_Rela *) ((char *) rel + s->sh_entsize))
  95. {
  96. Elf_Word *addr;
  97. Elf_Sym *sym;
  98. Elf_Addr value;
  99. if (seg->size < rel->r_offset)
  100. return grub_error (GRUB_ERR_BAD_MODULE,
  101. "reloc offset is out of the segment");
  102. addr = (Elf_Word *) ((char *) seg->addr + rel->r_offset);
  103. sym = (Elf_Sym *) ((char *) mod->symtab
  104. + mod->symsize * ELF_R_SYM (rel->r_info));
  105. value = sym->st_value + rel->r_addend;
  106. switch (ELF_R_TYPE (rel->r_info) & 0xff)
  107. {
  108. case R_SPARC_32: /* 3 V-word32 */
  109. if (value & 0xFFFFFFFF00000000)
  110. return grub_error (GRUB_ERR_BAD_MODULE,
  111. "address out of 32 bits range");
  112. *addr = value;
  113. break;
  114. case R_SPARC_WDISP30: /* 7 V-disp30 */
  115. if (((value - (Elf_Addr) addr) & 0xFFFFFFFF00000000) &&
  116. (((value - (Elf_Addr) addr) & 0xFFFFFFFF00000000)
  117. != 0xFFFFFFFF00000000))
  118. {
  119. struct trampoline *tp = mod->trampptr;
  120. mod->trampptr = tp + 1;
  121. grub_memcpy (tp->code, trampoline_code, sizeof (tp->code));
  122. tp->addr = value;
  123. value = (Elf_Addr) tp;
  124. }
  125. if (((value - (Elf_Addr) addr) & 0xFFFFFFFF00000000) &&
  126. (((value - (Elf_Addr) addr) & 0xFFFFFFFF00000000)
  127. != 0xFFFFFFFF00000000))
  128. return grub_error (GRUB_ERR_BAD_MODULE,
  129. "displacement out of 30 bits range");
  130. *addr = (*addr & 0xC0000000) |
  131. (((grub_int32_t) ((value - (Elf_Addr) addr) >> 2)) &
  132. 0x3FFFFFFF);
  133. break;
  134. case R_SPARC_HH22: /* 9 V-imm22 */
  135. *addr = (*addr & 0xFFC00000) | ((value >> 42) & 0x3FFFFF);
  136. break;
  137. case R_SPARC_HM10: /* 12 T-simm13 */
  138. *addr = (*addr & 0xFFFFFC00) | ((value >> 32) & 0x3FF);
  139. break;
  140. case R_SPARC_HI22: /* 9 V-imm22 */
  141. if (value >> 32)
  142. return grub_error (GRUB_ERR_BAD_MODULE,
  143. "address out of 32 bits range");
  144. /* Fallthrough. */
  145. case R_SPARC_LM22:
  146. *addr = (*addr & 0xFFC00000) | ((value >> 10) & 0x3FFFFF);
  147. break;
  148. case R_SPARC_LO10: /* 12 T-simm13 */
  149. *addr = (*addr & 0xFFFFFC00) | (value & 0x3FF);
  150. break;
  151. case R_SPARC_64: /* 32 V-xwords64 */
  152. *(Elf_Xword *) addr = value;
  153. break;
  154. case R_SPARC_OLO10:
  155. *addr = (*addr & ~0x1fff)
  156. | (((value & 0x3ff) +
  157. (ELF_R_TYPE (rel->r_info) >> 8))
  158. & 0x1fff);
  159. break;
  160. default:
  161. {
  162. char rel_info[17]; /* log16(2^64) = 16, plus NUL. */
  163. grub_snprintf (rel_info, sizeof (rel_info) - 1, "%" PRIxGRUB_UINT64_T,
  164. ELF_R_TYPE (rel->r_info));
  165. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  166. N_("relocation 0x%s is not implemented yet"), rel_info);
  167. }
  168. }
  169. }
  170. return GRUB_ERR_NONE;
  171. }