loadcore64.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* i386 CPU-specific part of loadcore.c for 32-bit mode */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 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/err.h>
  20. #include <grub/mm.h>
  21. #include <grub/misc.h>
  22. #include <grub/efiemu/efiemu.h>
  23. #include <grub/cpu/efiemu.h>
  24. #include <grub/elf.h>
  25. #include <grub/i18n.h>
  26. /* Check if EHDR is a valid ELF header. */
  27. int
  28. grub_arch_efiemu_check_header64 (void *ehdr)
  29. {
  30. Elf64_Ehdr *e = ehdr;
  31. return (e->e_ident[EI_CLASS] == ELFCLASS64
  32. && e->e_ident[EI_DATA] == ELFDATA2LSB
  33. && e->e_machine == EM_X86_64);
  34. }
  35. /* Relocate symbols. */
  36. grub_err_t
  37. grub_arch_efiemu_relocate_symbols64 (grub_efiemu_segment_t segs,
  38. struct grub_efiemu_elf_sym *elfsyms,
  39. void *ehdr)
  40. {
  41. unsigned i;
  42. Elf64_Ehdr *e = ehdr;
  43. Elf64_Shdr *s;
  44. grub_err_t err;
  45. for (i = 0, s = (Elf64_Shdr *) ((char *) e + e->e_shoff);
  46. i < e->e_shnum;
  47. i++, s = (Elf64_Shdr *) ((char *) s + e->e_shentsize))
  48. if (s->sh_type == SHT_RELA)
  49. {
  50. grub_efiemu_segment_t seg;
  51. grub_dprintf ("efiemu", "shtrel\n");
  52. /* Find the target segment. */
  53. for (seg = segs; seg; seg = seg->next)
  54. if (seg->section == s->sh_info)
  55. break;
  56. if (seg)
  57. {
  58. Elf64_Rela *rel, *max;
  59. for (rel = (Elf64_Rela *) ((char *) e + s->sh_offset),
  60. max = rel + (unsigned long) s->sh_size
  61. / (unsigned long)s->sh_entsize;
  62. rel < max;
  63. rel++)
  64. {
  65. void *addr;
  66. grub_uint32_t *addr32;
  67. grub_uint64_t *addr64;
  68. struct grub_efiemu_elf_sym sym;
  69. if (seg->size < rel->r_offset)
  70. return grub_error (GRUB_ERR_BAD_MODULE,
  71. "reloc offset is out of the segment");
  72. addr =
  73. ((char *) grub_efiemu_mm_obtain_request (seg->handle)
  74. + seg->off + rel->r_offset);
  75. addr32 = (grub_uint32_t *) addr;
  76. addr64 = (grub_uint64_t *) addr;
  77. sym = elfsyms[ELF64_R_SYM (rel->r_info)];
  78. switch (ELF64_R_TYPE (rel->r_info))
  79. {
  80. case R_X86_64_64:
  81. err = grub_efiemu_write_value (addr,
  82. *addr64 + rel->r_addend
  83. + sym.off, sym.handle,
  84. 0, seg->ptv_rel_needed,
  85. sizeof (grub_uint64_t));
  86. if (err)
  87. return err;
  88. break;
  89. case R_X86_64_PC32:
  90. case R_X86_64_PLT32:
  91. err = grub_efiemu_write_value (addr,
  92. *addr32 + rel->r_addend
  93. + sym.off
  94. - rel->r_offset - seg->off,
  95. sym.handle, seg->handle,
  96. seg->ptv_rel_needed,
  97. sizeof (grub_uint32_t));
  98. if (err)
  99. return err;
  100. break;
  101. case R_X86_64_32:
  102. case R_X86_64_32S:
  103. err = grub_efiemu_write_value (addr,
  104. *addr32 + rel->r_addend
  105. + sym.off, sym.handle,
  106. 0, seg->ptv_rel_needed,
  107. sizeof (grub_uint32_t));
  108. if (err)
  109. return err;
  110. break;
  111. default:
  112. {
  113. char rel_info[17]; /* log16(2^64) = 16, plus NUL. */
  114. grub_snprintf (rel_info, sizeof (rel_info) - 1, "%" PRIxGRUB_UINT64_T,
  115. ELF_R_TYPE (rel->r_info));
  116. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  117. N_("relocation 0x%s is not implemented yet"), rel_info);
  118. }
  119. }
  120. }
  121. }
  122. }
  123. return GRUB_ERR_NONE;
  124. }