dl.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. case R_X86_64_PLT32:
  64. {
  65. grub_int64_t value;
  66. value = ((grub_int32_t) *addr32) + rel->r_addend + sym->st_value -
  67. (Elf64_Xword) (grub_addr_t) seg->addr - rel->r_offset;
  68. if (value != (grub_int32_t) value)
  69. return grub_error (GRUB_ERR_BAD_MODULE, "relocation out of range");
  70. *addr32 = value;
  71. }
  72. break;
  73. case R_X86_64_PC64:
  74. {
  75. *addr64 += rel->r_addend + sym->st_value -
  76. (Elf64_Xword) (grub_addr_t) seg->addr - rel->r_offset;
  77. }
  78. break;
  79. case R_X86_64_32:
  80. {
  81. grub_uint64_t value = *addr32 + rel->r_addend + sym->st_value;
  82. if (value != (grub_uint32_t) value)
  83. return grub_error (GRUB_ERR_BAD_MODULE, "relocation out of range");
  84. *addr32 = value;
  85. }
  86. break;
  87. case R_X86_64_32S:
  88. {
  89. grub_int64_t value = ((grub_int32_t) *addr32) + rel->r_addend + sym->st_value;
  90. if (value != (grub_int32_t) value)
  91. return grub_error (GRUB_ERR_BAD_MODULE, "relocation out of range");
  92. *addr32 = value;
  93. }
  94. break;
  95. default:
  96. {
  97. char rel_info[17]; /* log16(2^64) = 16, plus NUL. */
  98. grub_snprintf (rel_info, sizeof (rel_info) - 1, "%" PRIxGRUB_UINT64_T,
  99. ELF_R_TYPE (rel->r_info));
  100. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  101. N_("relocation 0x%s is not implemented yet"), rel_info);
  102. }
  103. }
  104. }
  105. return GRUB_ERR_NONE;
  106. }