loadcore32.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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_header32 (void *ehdr)
  29. {
  30. Elf32_Ehdr *e = ehdr;
  31. /* Check the magic numbers. */
  32. return (e->e_ident[EI_CLASS] == ELFCLASS32
  33. && e->e_ident[EI_DATA] == ELFDATA2LSB
  34. && e->e_machine == EM_386);
  35. }
  36. /* Relocate symbols. */
  37. grub_err_t
  38. grub_arch_efiemu_relocate_symbols32 (grub_efiemu_segment_t segs,
  39. struct grub_efiemu_elf_sym *elfsyms,
  40. void *ehdr)
  41. {
  42. unsigned i;
  43. Elf32_Ehdr *e = ehdr;
  44. Elf32_Shdr *s;
  45. grub_err_t err;
  46. grub_dprintf ("efiemu", "relocating symbols %d %d\n",
  47. e->e_shoff, e->e_shnum);
  48. for (i = 0, s = (Elf32_Shdr *) ((char *) e + e->e_shoff);
  49. i < e->e_shnum;
  50. i++, s = (Elf32_Shdr *) ((char *) s + e->e_shentsize))
  51. if (s->sh_type == SHT_REL)
  52. {
  53. grub_efiemu_segment_t seg;
  54. grub_dprintf ("efiemu", "shtrel\n");
  55. /* Find the target segment. */
  56. for (seg = segs; seg; seg = seg->next)
  57. if (seg->section == s->sh_info)
  58. break;
  59. if (seg)
  60. {
  61. Elf32_Rel *rel, *max;
  62. for (rel = (Elf32_Rel *) ((char *) e + s->sh_offset),
  63. max = rel + s->sh_size / s->sh_entsize;
  64. rel < max;
  65. rel++)
  66. {
  67. Elf32_Word *addr;
  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 = (Elf32_Word *)
  73. ((char *) grub_efiemu_mm_obtain_request (seg->handle)
  74. + seg->off + rel->r_offset);
  75. sym = elfsyms[ELF32_R_SYM (rel->r_info)];
  76. switch (ELF32_R_TYPE (rel->r_info))
  77. {
  78. case R_386_32:
  79. err = grub_efiemu_write_value (addr, sym.off + *addr,
  80. sym.handle, 0,
  81. seg->ptv_rel_needed,
  82. sizeof (grub_uint32_t));
  83. if (err)
  84. return err;
  85. break;
  86. case R_386_PC32:
  87. err = grub_efiemu_write_value (addr, sym.off + *addr
  88. - rel->r_offset
  89. - seg->off, sym.handle,
  90. seg->handle,
  91. seg->ptv_rel_needed,
  92. sizeof (grub_uint32_t));
  93. if (err)
  94. return err;
  95. break;
  96. default:
  97. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  98. N_("relocation 0x%x is not implemented yet"),
  99. ELF_R_TYPE (rel->r_info));
  100. }
  101. }
  102. }
  103. }
  104. return GRUB_ERR_NONE;
  105. }