dl.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* dl-x86_64.c - arch-dependent part of loadable module support */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2002,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. Elf64_Ehdr *e = ehdr;
  29. /* Check the magic numbers. */
  30. if (e->e_ident[EI_CLASS] != ELFCLASS64
  31. || e->e_ident[EI_DATA] != ELFDATA2LSB
  32. || e->e_machine != EM_X86_64)
  33. return grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-dependent ELF magic"));
  34. return GRUB_ERR_NONE;
  35. }
  36. /* Relocate symbols. */
  37. grub_err_t
  38. grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
  39. Elf_Shdr *s, grub_dl_segment_t seg)
  40. {
  41. Elf64_Rela *rel, *max;
  42. for (rel = (Elf64_Rela *) ((char *) ehdr + s->sh_offset),
  43. max = (Elf64_Rela *) ((char *) rel + s->sh_size);
  44. rel < max;
  45. rel = (Elf64_Rela *) ((char *) rel + s->sh_entsize))
  46. {
  47. Elf64_Word *addr32;
  48. Elf64_Xword *addr64;
  49. Elf64_Sym *sym;
  50. if (seg->size < rel->r_offset)
  51. return grub_error (GRUB_ERR_BAD_MODULE,
  52. "reloc offset is out of the segment");
  53. addr32 = (Elf64_Word *) ((char *) seg->addr + rel->r_offset);
  54. addr64 = (Elf64_Xword *) addr32;
  55. sym = (Elf64_Sym *) ((char *) mod->symtab
  56. + mod->symsize * ELF_R_SYM (rel->r_info));
  57. switch (ELF_R_TYPE (rel->r_info))
  58. {
  59. case R_X86_64_64:
  60. *addr64 += rel->r_addend + sym->st_value;
  61. break;
  62. case R_X86_64_PC32:
  63. {
  64. grub_int64_t value;
  65. value = ((grub_int32_t) *addr32) + rel->r_addend + sym->st_value -
  66. (Elf64_Xword) (grub_addr_t) seg->addr - rel->r_offset;
  67. if (value != (grub_int32_t) value)
  68. return grub_error (GRUB_ERR_BAD_MODULE, "relocation out of range");
  69. *addr32 = value;
  70. }
  71. break;
  72. case R_X86_64_PC64:
  73. {
  74. *addr64 += rel->r_addend + sym->st_value -
  75. (Elf64_Xword) (grub_addr_t) seg->addr - rel->r_offset;
  76. }
  77. break;
  78. case R_X86_64_32:
  79. {
  80. grub_uint64_t value = *addr32 + rel->r_addend + sym->st_value;
  81. if (value != (grub_uint32_t) value)
  82. return grub_error (GRUB_ERR_BAD_MODULE, "relocation out of range");
  83. *addr32 = value;
  84. }
  85. break;
  86. case R_X86_64_32S:
  87. {
  88. grub_int64_t value = ((grub_int32_t) *addr32) + rel->r_addend + sym->st_value;
  89. if (value != (grub_int32_t) value)
  90. return grub_error (GRUB_ERR_BAD_MODULE, "relocation out of range");
  91. *addr32 = value;
  92. }
  93. break;
  94. default:
  95. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  96. N_("relocation 0x%x is not implemented yet"),
  97. ELF_R_TYPE (rel->r_info));
  98. }
  99. }
  100. return GRUB_ERR_NONE;
  101. }