elf.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /* elf.c - load ELF files */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2003,2004,2005,2006,2007,2008,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/elf.h>
  21. #include <grub/elfload.h>
  22. #include <grub/file.h>
  23. #include <grub/misc.h>
  24. #include <grub/mm.h>
  25. #include <grub/dl.h>
  26. #include <grub/i18n.h>
  27. GRUB_MOD_LICENSE ("GPLv3+");
  28. #pragma GCC diagnostic ignored "-Wcast-align"
  29. #if defined(__powerpc__) && defined(GRUB_MACHINE_IEEE1275)
  30. #define GRUB_ELF_ENABLE_BI_ENDIAN 1
  31. #else
  32. #define GRUB_ELF_ENABLE_BI_ENDIAN 0
  33. #endif
  34. #if defined(GRUB_CPU_WORDS_BIGENDIAN)
  35. #define GRUB_ELF_NATIVE_ENDIANNESS ELFDATA2MSB
  36. #define GRUB_ELF_OPPOSITE_ENDIANNESS ELFDATA2LSB
  37. #else
  38. #define GRUB_ELF_NATIVE_ENDIANNESS ELFDATA2LSB
  39. #define GRUB_ELF_OPPOSITE_ENDIANNESS ELFDATA2MSB
  40. #endif
  41. static int grub_elf32_check_endianess_and_bswap_ehdr (grub_elf_t elf);
  42. static int grub_elf64_check_endianess_and_bswap_ehdr (grub_elf_t elf);
  43. /* Check if EHDR is a valid ELF header. */
  44. static grub_err_t
  45. grub_elf_check_header (grub_elf_t elf)
  46. {
  47. Elf32_Ehdr *e = &elf->ehdr.ehdr32;
  48. if (e->e_ident[EI_MAG0] != ELFMAG0
  49. || e->e_ident[EI_MAG1] != ELFMAG1
  50. || e->e_ident[EI_MAG2] != ELFMAG2
  51. || e->e_ident[EI_MAG3] != ELFMAG3
  52. || e->e_ident[EI_VERSION] != EV_CURRENT)
  53. return grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-independent ELF magic"));
  54. if (grub_elf_is_elf32 (elf))
  55. {
  56. if (!grub_elf32_check_endianess_and_bswap_ehdr (elf)) {
  57. return grub_error (GRUB_ERR_BAD_OS, "invalid ELF endianness magic");
  58. }
  59. }
  60. else if (grub_elf_is_elf64 (elf))
  61. {
  62. if (!grub_elf64_check_endianess_and_bswap_ehdr (elf)) {
  63. return grub_error (GRUB_ERR_BAD_OS, "invalid ELF endianness magic");
  64. }
  65. }
  66. else
  67. return grub_error (GRUB_ERR_BAD_OS, "unknown ELF class");
  68. if (e->e_version != EV_CURRENT)
  69. return grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-independent ELF magic"));
  70. return GRUB_ERR_NONE;
  71. }
  72. grub_err_t
  73. grub_elf_close (grub_elf_t elf)
  74. {
  75. grub_file_t file = elf->file;
  76. grub_free (elf->phdrs);
  77. grub_free (elf->filename);
  78. grub_free (elf);
  79. if (file)
  80. grub_file_close (file);
  81. return grub_errno;
  82. }
  83. grub_elf_t
  84. grub_elf_file (grub_file_t file, const char *filename)
  85. {
  86. grub_elf_t elf;
  87. elf = grub_zalloc (sizeof (*elf));
  88. if (! elf)
  89. return 0;
  90. elf->file = file;
  91. if (grub_file_seek (elf->file, 0) == (grub_off_t) -1)
  92. goto fail;
  93. if (grub_file_read (elf->file, &elf->ehdr, sizeof (elf->ehdr))
  94. != sizeof (elf->ehdr))
  95. {
  96. if (!grub_errno)
  97. grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"),
  98. filename);
  99. goto fail;
  100. }
  101. if (grub_elf_check_header (elf))
  102. goto fail;
  103. elf->filename = grub_strdup (filename);
  104. if (!elf->filename)
  105. goto fail;
  106. return elf;
  107. fail:
  108. grub_free (elf->filename);
  109. grub_free (elf->phdrs);
  110. grub_free (elf);
  111. return 0;
  112. }
  113. grub_elf_t
  114. grub_elf_open (const char *name, enum grub_file_type type)
  115. {
  116. grub_file_t file;
  117. grub_elf_t elf;
  118. file = grub_file_open (name, type);
  119. if (! file)
  120. return 0;
  121. elf = grub_elf_file (file, name);
  122. if (! elf)
  123. grub_file_close (file);
  124. return elf;
  125. }
  126. #define grub_swap_bytes_halfXX grub_swap_bytes16
  127. #define grub_swap_bytes_wordXX grub_swap_bytes32
  128. /* 32-bit */
  129. #define ehdrXX ehdr32
  130. #define ELFCLASSXX ELFCLASS32
  131. #define ElfXX_Addr Elf32_Addr
  132. #define grub_elfXX_size grub_elf32_size
  133. #define grub_elfXX_load grub_elf32_load
  134. #define FOR_ELFXX_PHDRS FOR_ELF32_PHDRS
  135. #define grub_elf_is_elfXX grub_elf_is_elf32
  136. #define grub_elfXX_load_phdrs grub_elf32_load_phdrs
  137. #define ElfXX_Phdr Elf32_Phdr
  138. #define ElfXX_Ehdr Elf32_Ehdr
  139. #define ElfXX_Shdr Elf32_Shdr
  140. #define ElfXX_Word Elf32_Word
  141. #define ElfXX_Shnum Elf32_Shnum
  142. #define grub_uintXX_t grub_uint32_t
  143. #define grub_swap_bytes_addrXX grub_swap_bytes32
  144. #define grub_swap_bytes_offXX grub_swap_bytes32
  145. #define grub_swap_bytes_XwordXX grub_swap_bytes32
  146. #define grub_elfXX_check_endianess_and_bswap_ehdr grub_elf32_check_endianess_and_bswap_ehdr
  147. #define grub_elfXX_get_shnum grub_elf32_get_shnum
  148. #define grub_elfXX_get_shstrndx grub_elf32_get_shstrndx
  149. #define grub_elfXX_get_phnum grub_elf32_get_phnum
  150. #include "elfXX.c"
  151. #undef ehdrXX
  152. #undef ELFCLASSXX
  153. #undef ElfXX_Addr
  154. #undef grub_elfXX_size
  155. #undef grub_elfXX_load
  156. #undef FOR_ELFXX_PHDRS
  157. #undef grub_elf_is_elfXX
  158. #undef grub_elfXX_load_phdrs
  159. #undef ElfXX_Phdr
  160. #undef ElfXX_Ehdr
  161. #undef ElfXX_Shdr
  162. #undef ElfXX_Word
  163. #undef ElfXX_Shnum
  164. #undef grub_uintXX_t
  165. #undef grub_swap_bytes_addrXX
  166. #undef grub_swap_bytes_offXX
  167. #undef grub_swap_bytes_XwordXX
  168. #undef grub_elfXX_check_endianess_and_bswap_ehdr
  169. #undef grub_elfXX_get_shnum
  170. #undef grub_elfXX_get_shstrndx
  171. #undef grub_elfXX_get_phnum
  172. /* 64-bit */
  173. #define ehdrXX ehdr64
  174. #define ELFCLASSXX ELFCLASS64
  175. #define ElfXX_Addr Elf64_Addr
  176. #define grub_elfXX_size grub_elf64_size
  177. #define grub_elfXX_load grub_elf64_load
  178. #define FOR_ELFXX_PHDRS FOR_ELF64_PHDRS
  179. #define grub_elf_is_elfXX grub_elf_is_elf64
  180. #define grub_elfXX_load_phdrs grub_elf64_load_phdrs
  181. #define ElfXX_Phdr Elf64_Phdr
  182. #define ElfXX_Ehdr Elf64_Ehdr
  183. #define ElfXX_Shdr Elf64_Shdr
  184. #define ElfXX_Word Elf64_Word
  185. #define ElfXX_Shnum Elf64_Shnum
  186. #define grub_uintXX_t grub_uint64_t
  187. #define grub_swap_bytes_addrXX grub_swap_bytes64
  188. #define grub_swap_bytes_offXX grub_swap_bytes64
  189. #define grub_swap_bytes_XwordXX grub_swap_bytes64
  190. #define grub_elfXX_check_endianess_and_bswap_ehdr grub_elf64_check_endianess_and_bswap_ehdr
  191. #define grub_elfXX_get_shnum grub_elf64_get_shnum
  192. #define grub_elfXX_get_shstrndx grub_elf64_get_shstrndx
  193. #define grub_elfXX_get_phnum grub_elf64_get_phnum
  194. #include "elfXX.c"