multiboot_elfxx.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2007,2008,2009 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #if defined(MULTIBOOT_LOAD_ELF32)
  19. # define XX 32
  20. # define E_MACHINE MULTIBOOT_ELF32_MACHINE
  21. # define ELFCLASSXX ELFCLASS32
  22. # define Elf_Ehdr Elf32_Ehdr
  23. # define Elf_Phdr Elf32_Phdr
  24. # define Elf_Shdr Elf32_Shdr
  25. #elif defined(MULTIBOOT_LOAD_ELF64)
  26. # define XX 64
  27. # define E_MACHINE MULTIBOOT_ELF64_MACHINE
  28. # define ELFCLASSXX ELFCLASS64
  29. # define Elf_Ehdr Elf64_Ehdr
  30. # define Elf_Phdr Elf64_Phdr
  31. # define Elf_Shdr Elf64_Shdr
  32. #else
  33. #error "I'm confused"
  34. #endif
  35. #include <grub/i386/relocator.h>
  36. #define CONCAT(a,b) CONCAT_(a, b)
  37. #define CONCAT_(a,b) a ## b
  38. #pragma GCC diagnostic ignored "-Wcast-align"
  39. /* Check if BUFFER contains ELF32 (or ELF64). */
  40. static int
  41. CONCAT(grub_multiboot_is_elf, XX) (void *buffer)
  42. {
  43. Elf_Ehdr *ehdr = (Elf_Ehdr *) buffer;
  44. return ehdr->e_ident[EI_CLASS] == ELFCLASSXX;
  45. }
  46. static grub_err_t
  47. CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char *filename, void *buffer)
  48. {
  49. Elf_Ehdr *ehdr = (Elf_Ehdr *) buffer;
  50. char *phdr_base;
  51. int i;
  52. if (ehdr->e_ident[EI_MAG0] != ELFMAG0
  53. || ehdr->e_ident[EI_MAG1] != ELFMAG1
  54. || ehdr->e_ident[EI_MAG2] != ELFMAG2
  55. || ehdr->e_ident[EI_MAG3] != ELFMAG3
  56. || ehdr->e_ident[EI_DATA] != ELFDATA2LSB)
  57. return grub_error(GRUB_ERR_UNKNOWN_OS, N_("invalid arch-independent ELF magic"));
  58. if (ehdr->e_ident[EI_CLASS] != ELFCLASSXX || ehdr->e_machine != E_MACHINE
  59. || ehdr->e_version != EV_CURRENT)
  60. return grub_error (GRUB_ERR_UNKNOWN_OS, N_("invalid arch-dependent ELF magic"));
  61. if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN)
  62. return grub_error (GRUB_ERR_UNKNOWN_OS, N_("this ELF file is not of the right type"));
  63. /* FIXME: Should we support program headers at strange locations? */
  64. if (ehdr->e_phoff + (grub_uint32_t) ehdr->e_phnum * ehdr->e_phentsize > MULTIBOOT_SEARCH)
  65. return grub_error (GRUB_ERR_BAD_OS, "program header at a too high offset");
  66. phdr_base = (char *) buffer + ehdr->e_phoff;
  67. #define phdr(i) ((Elf_Phdr *) (phdr_base + (i) * ehdr->e_phentsize))
  68. /* Load every loadable segment in memory. */
  69. for (i = 0; i < ehdr->e_phnum; i++)
  70. {
  71. if (phdr(i)->p_type == PT_LOAD)
  72. {
  73. grub_err_t err;
  74. void *source;
  75. if (phdr(i)->p_paddr + phdr(i)->p_memsz > highest_load)
  76. highest_load = phdr(i)->p_paddr + phdr(i)->p_memsz;
  77. grub_dprintf ("multiboot_loader", "segment %d: paddr=0x%lx, memsz=0x%lx, vaddr=0x%lx\n",
  78. i, (long) phdr(i)->p_paddr, (long) phdr(i)->p_memsz, (long) phdr(i)->p_vaddr);
  79. {
  80. grub_relocator_chunk_t ch;
  81. err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator,
  82. &ch, phdr(i)->p_paddr,
  83. phdr(i)->p_memsz);
  84. if (err)
  85. {
  86. grub_dprintf ("multiboot_loader", "Error loading phdr %d\n", i);
  87. return err;
  88. }
  89. source = get_virtual_current_address (ch);
  90. }
  91. if (phdr(i)->p_filesz != 0)
  92. {
  93. if (grub_file_seek (file, (grub_off_t) phdr(i)->p_offset)
  94. == (grub_off_t) -1)
  95. return grub_errno;
  96. if (grub_file_read (file, source, phdr(i)->p_filesz)
  97. != (grub_ssize_t) phdr(i)->p_filesz)
  98. {
  99. if (!grub_errno)
  100. grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"),
  101. filename);
  102. return grub_errno;
  103. }
  104. }
  105. if (phdr(i)->p_filesz < phdr(i)->p_memsz)
  106. grub_memset ((grub_uint8_t *) source + phdr(i)->p_filesz, 0,
  107. phdr(i)->p_memsz - phdr(i)->p_filesz);
  108. }
  109. }
  110. for (i = 0; i < ehdr->e_phnum; i++)
  111. if (phdr(i)->p_vaddr <= ehdr->e_entry
  112. && phdr(i)->p_vaddr + phdr(i)->p_memsz > ehdr->e_entry)
  113. {
  114. grub_multiboot_payload_eip = (ehdr->e_entry - phdr(i)->p_vaddr)
  115. + phdr(i)->p_paddr;
  116. #ifdef MULTIBOOT_LOAD_ELF64
  117. # ifdef __mips
  118. /* We still in 32-bit mode. */
  119. if ((ehdr->e_entry - phdr(i)->p_vaddr)
  120. + phdr(i)->p_paddr < 0xffffffff80000000ULL)
  121. return grub_error (GRUB_ERR_BAD_OS, "invalid entry point for ELF64");
  122. # else
  123. /* We still in 32-bit mode. */
  124. if ((ehdr->e_entry - phdr(i)->p_vaddr)
  125. + phdr(i)->p_paddr > 0xffffffff)
  126. return grub_error (GRUB_ERR_BAD_OS, "invalid entry point for ELF64");
  127. # endif
  128. #endif
  129. break;
  130. }
  131. if (i == ehdr->e_phnum)
  132. return grub_error (GRUB_ERR_BAD_OS, "entry point isn't in a segment");
  133. #if defined (__i386__) || defined (__x86_64__)
  134. #elif defined (__mips)
  135. grub_multiboot_payload_eip |= 0x80000000;
  136. #else
  137. #error Please complete this
  138. #endif
  139. if (ehdr->e_shnum)
  140. {
  141. grub_uint8_t *shdr, *shdrptr;
  142. shdr = grub_malloc ((grub_uint32_t) ehdr->e_shnum * ehdr->e_shentsize);
  143. if (!shdr)
  144. return grub_errno;
  145. if (grub_file_seek (file, ehdr->e_shoff) == (grub_off_t) -1)
  146. {
  147. grub_free (shdr);
  148. return grub_errno;
  149. }
  150. if (grub_file_read (file, shdr, (grub_uint32_t) ehdr->e_shnum * ehdr->e_shentsize)
  151. != (grub_ssize_t) ehdr->e_shnum * ehdr->e_shentsize)
  152. {
  153. if (!grub_errno)
  154. grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"),
  155. filename);
  156. return grub_errno;
  157. }
  158. for (shdrptr = shdr, i = 0; i < ehdr->e_shnum;
  159. shdrptr += ehdr->e_shentsize, i++)
  160. {
  161. Elf_Shdr *sh = (Elf_Shdr *) shdrptr;
  162. void *src;
  163. grub_addr_t target;
  164. grub_err_t err;
  165. /* This section is a loaded section,
  166. so we don't care. */
  167. if (sh->sh_addr != 0)
  168. continue;
  169. /* This section is empty, so we don't care. */
  170. if (sh->sh_size == 0)
  171. continue;
  172. {
  173. grub_relocator_chunk_t ch;
  174. err = grub_relocator_alloc_chunk_align (grub_multiboot_relocator,
  175. &ch, 0,
  176. (0xffffffff - sh->sh_size)
  177. + 1, sh->sh_size,
  178. sh->sh_addralign,
  179. GRUB_RELOCATOR_PREFERENCE_NONE,
  180. 0);
  181. if (err)
  182. {
  183. grub_dprintf ("multiboot_loader", "Error loading shdr %d\n", i);
  184. return err;
  185. }
  186. src = get_virtual_current_address (ch);
  187. target = get_physical_target_address (ch);
  188. }
  189. if (grub_file_seek (file, sh->sh_offset) == (grub_off_t) -1)
  190. return grub_errno;
  191. if (grub_file_read (file, src, sh->sh_size)
  192. != (grub_ssize_t) sh->sh_size)
  193. {
  194. if (!grub_errno)
  195. grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"),
  196. filename);
  197. return grub_errno;
  198. }
  199. sh->sh_addr = target;
  200. }
  201. grub_multiboot_add_elfsyms (ehdr->e_shnum, ehdr->e_shentsize,
  202. ehdr->e_shstrndx, shdr);
  203. }
  204. #undef phdr
  205. return grub_errno;
  206. }
  207. #undef XX
  208. #undef E_MACHINE
  209. #undef ELFCLASSXX
  210. #undef Elf_Ehdr
  211. #undef Elf_Phdr
  212. #undef Elf_Shdr