dl.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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] != ELFCLASS32
  31. || e->e_ident[EI_DATA] != ELFDATA2MSB
  32. || e->e_machine != EM_PPC)
  33. return grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-dependent ELF magic"));
  34. return GRUB_ERR_NONE;
  35. }
  36. /* For low-endian reverse lis and addr_high as well as ori and addr_low. */
  37. struct trampoline
  38. {
  39. grub_uint32_t lis;
  40. grub_uint32_t ori;
  41. grub_uint32_t mtctr;
  42. grub_uint32_t bctr;
  43. };
  44. static const struct trampoline trampoline_template =
  45. {
  46. 0x3d800000,
  47. 0x618c0000,
  48. 0x7d8903a6,
  49. 0x4e800420,
  50. };
  51. #pragma GCC diagnostic ignored "-Wcast-align"
  52. grub_err_t
  53. grub_arch_dl_get_tramp_got_size (const void *ehdr, grub_size_t *tramp,
  54. grub_size_t *got)
  55. {
  56. const Elf_Ehdr *e = ehdr;
  57. const Elf_Shdr *s;
  58. unsigned i;
  59. *tramp = 0;
  60. *got = 0;
  61. for (i = 0, s = (const Elf_Shdr *) ((const char *) e + e->e_shoff);
  62. i < e->e_shnum;
  63. i++, s = (const Elf_Shdr *) ((const char *) s + e->e_shentsize))
  64. if (s->sh_type == SHT_RELA)
  65. {
  66. const Elf_Rela *rel, *max;
  67. for (rel = (const Elf_Rela *) ((const char *) e + s->sh_offset),
  68. max = rel + s->sh_size / s->sh_entsize;
  69. rel < max;
  70. rel++)
  71. if (ELF_R_TYPE (rel->r_info) == GRUB_ELF_R_PPC_REL24
  72. || ELF_R_TYPE (rel->r_info) == GRUB_ELF_R_PPC_PLTREL24)
  73. (*tramp)++;
  74. }
  75. *tramp *= sizeof (struct trampoline);
  76. return GRUB_ERR_NONE;
  77. }
  78. /* Relocate symbols. */
  79. grub_err_t
  80. grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
  81. Elf_Shdr *s, grub_dl_segment_t seg)
  82. {
  83. Elf_Rela *rel, *max;
  84. for (rel = (Elf_Rela *) ((char *) ehdr + s->sh_offset),
  85. max = (Elf_Rela *) ((char *) rel + s->sh_size);
  86. rel < max;
  87. rel = (Elf_Rela *) ((char *) rel + s->sh_entsize))
  88. {
  89. Elf_Word *addr;
  90. Elf_Sym *sym;
  91. grub_uint32_t value;
  92. if (seg->size < rel->r_offset)
  93. return grub_error (GRUB_ERR_BAD_MODULE,
  94. "reloc offset is out of the segment");
  95. addr = (Elf_Word *) ((char *) seg->addr + rel->r_offset);
  96. sym = (Elf_Sym *) ((char *) mod->symtab
  97. + mod->symsize * ELF_R_SYM (rel->r_info));
  98. /* On the PPC the value does not have an explicit
  99. addend, add it. */
  100. value = sym->st_value + rel->r_addend;
  101. switch (ELF_R_TYPE (rel->r_info))
  102. {
  103. case GRUB_ELF_R_PPC_ADDR16_LO:
  104. *(Elf_Half *) addr = value;
  105. break;
  106. case GRUB_ELF_R_PPC_PLTREL24:
  107. case GRUB_ELF_R_PPC_REL24:
  108. {
  109. Elf_Sword delta = value - (Elf_Word) addr;
  110. if (delta << 6 >> 6 != delta)
  111. {
  112. struct trampoline *tptr = mod->trampptr;
  113. grub_memcpy (tptr, &trampoline_template,
  114. sizeof (*tptr));
  115. delta = (grub_uint8_t *) tptr - (grub_uint8_t *) addr;
  116. tptr->lis |= (((value) >> 16) & 0xffff);
  117. tptr->ori |= ((value) & 0xffff);
  118. mod->trampptr = tptr + 1;
  119. }
  120. if (delta << 6 >> 6 != delta)
  121. return grub_error (GRUB_ERR_BAD_MODULE,
  122. "relocation overflow");
  123. *addr = (*addr & 0xfc000003) | (delta & 0x3fffffc);
  124. break;
  125. }
  126. case GRUB_ELF_R_PPC_ADDR16_HA:
  127. *(Elf_Half *) addr = (value + 0x8000) >> 16;
  128. break;
  129. case GRUB_ELF_R_PPC_ADDR32:
  130. *addr = value;
  131. break;
  132. case GRUB_ELF_R_PPC_REL32:
  133. *addr = value - (Elf_Word) addr;
  134. break;
  135. default:
  136. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  137. N_("relocation 0x%x is not implemented yet"),
  138. ELF_R_TYPE (rel->r_info));
  139. }
  140. }
  141. return GRUB_ERR_NONE;
  142. }