dl.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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/mm.h>
  24. #include <grub/i18n.h>
  25. #include <grub/ia64/reloc.h>
  26. #define MASK19 ((1 << 19) - 1)
  27. #define MASK20 ((1 << 20) - 1)
  28. /* Check if EHDR is a valid ELF header. */
  29. grub_err_t
  30. grub_arch_dl_check_header (void *ehdr)
  31. {
  32. Elf_Ehdr *e = ehdr;
  33. /* Check the magic numbers. */
  34. if (e->e_ident[EI_CLASS] != ELFCLASS64
  35. || e->e_ident[EI_DATA] != ELFDATA2LSB
  36. || e->e_machine != EM_IA_64)
  37. return grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-dependent ELF magic"));
  38. return GRUB_ERR_NONE;
  39. }
  40. #pragma GCC diagnostic ignored "-Wcast-align"
  41. /* Relocate symbols. */
  42. grub_err_t
  43. grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
  44. Elf_Shdr *s, grub_dl_segment_t seg)
  45. {
  46. Elf_Rela *rel, *max;
  47. for (rel = (Elf_Rela *) ((char *) ehdr + s->sh_offset),
  48. max = (Elf_Rela *) ((char *) rel + s->sh_size);
  49. rel < max;
  50. rel = (Elf_Rela *) ((char *) rel + s->sh_entsize))
  51. {
  52. grub_addr_t addr;
  53. Elf_Sym *sym;
  54. grub_uint64_t value;
  55. if (seg->size < (rel->r_offset & ~3))
  56. return grub_error (GRUB_ERR_BAD_MODULE,
  57. "reloc offset is out of the segment");
  58. addr = (grub_addr_t) seg->addr + rel->r_offset;
  59. sym = (Elf_Sym *) ((char *) mod->symtab
  60. + mod->symsize * ELF_R_SYM (rel->r_info));
  61. /* On the PPC the value does not have an explicit
  62. addend, add it. */
  63. value = sym->st_value + rel->r_addend;
  64. switch (ELF_R_TYPE (rel->r_info))
  65. {
  66. case R_IA64_PCREL21B:
  67. {
  68. grub_int64_t noff;
  69. if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
  70. {
  71. struct grub_ia64_trampoline *tr = mod->trampptr;
  72. grub_ia64_make_trampoline (tr, value);
  73. noff = ((char *) tr - (char *) (addr & ~3)) >> 4;
  74. mod->trampptr = tr + 1;
  75. }
  76. else
  77. noff = ((char *) value - (char *) (addr & ~3)) >> 4;
  78. if ((noff & ~MASK19) && ((-noff) & ~MASK19))
  79. return grub_error (GRUB_ERR_BAD_MODULE,
  80. "jump offset too big (%lx)", noff);
  81. grub_ia64_add_value_to_slot_20b (addr, noff);
  82. }
  83. break;
  84. case R_IA64_SEGREL64LSB:
  85. *(grub_uint64_t *) addr += value - (grub_addr_t) seg->addr;
  86. break;
  87. case R_IA64_FPTR64LSB:
  88. case R_IA64_DIR64LSB:
  89. *(grub_uint64_t *) addr += value;
  90. break;
  91. case R_IA64_PCREL64LSB:
  92. *(grub_uint64_t *) addr += value - addr;
  93. break;
  94. case R_IA64_GPREL64I:
  95. grub_ia64_set_immu64 (addr, value - (grub_addr_t) mod->base);
  96. break;
  97. case R_IA64_GPREL22:
  98. if ((value - (grub_addr_t) mod->base) & ~MASK20)
  99. return grub_error (GRUB_ERR_BAD_MODULE,
  100. "gprel offset too big (%lx)",
  101. value - (grub_addr_t) mod->base);
  102. grub_ia64_add_value_to_slot_21 (addr, value - (grub_addr_t) mod->base);
  103. break;
  104. case R_IA64_LTOFF22X:
  105. case R_IA64_LTOFF22:
  106. if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
  107. value = *(grub_uint64_t *) sym->st_value + rel->r_addend;
  108. /* Fallthrough. */
  109. case R_IA64_LTOFF_FPTR22:
  110. {
  111. grub_uint64_t *gpptr = mod->gotptr;
  112. *gpptr = value;
  113. if (((grub_addr_t) gpptr - (grub_addr_t) mod->base) & ~MASK20)
  114. return grub_error (GRUB_ERR_BAD_MODULE,
  115. "gprel offset too big (%lx)",
  116. (grub_addr_t) gpptr - (grub_addr_t) mod->base);
  117. grub_ia64_add_value_to_slot_21 (addr, (grub_addr_t) gpptr - (grub_addr_t) mod->base);
  118. mod->gotptr = gpptr + 1;
  119. break;
  120. }
  121. /* We treat LTOFF22X as LTOFF22, so we can ignore LDXMOV. */
  122. case R_IA64_LDXMOV:
  123. break;
  124. default:
  125. {
  126. char rel_info[17]; /* log16(2^64) = 16, plus NUL. */
  127. grub_snprintf (rel_info, sizeof (rel_info) - 1, "%" PRIxGRUB_UINT64_T,
  128. ELF_R_TYPE (rel->r_info));
  129. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  130. N_("relocation 0x%s is not implemented yet"), rel_info);
  131. }
  132. }
  133. }
  134. return GRUB_ERR_NONE;
  135. }