You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

elf.c 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. /**
  25. * @file
  26. *
  27. * ELF image format
  28. *
  29. * A "pure" ELF image is not a bootable image. There are various
  30. * bootable formats based upon ELF (e.g. Multiboot), which share
  31. * common ELF-related functionality.
  32. */
  33. #include <errno.h>
  34. #include <elf.h>
  35. #include <ipxe/uaccess.h>
  36. #include <ipxe/segment.h>
  37. #include <ipxe/image.h>
  38. #include <ipxe/elf.h>
  39. /**
  40. * Load ELF segment into memory
  41. *
  42. * @v image ELF file
  43. * @v phdr ELF program header
  44. * @v dest Destination address
  45. * @ret rc Return status code
  46. */
  47. static int elf_load_segment ( struct image *image, Elf_Phdr *phdr,
  48. physaddr_t dest ) {
  49. userptr_t buffer = phys_to_user ( dest );
  50. int rc;
  51. DBGC ( image, "ELF %p loading segment [%x,%x) to [%lx,%lx,%lx)\n",
  52. image, phdr->p_offset, ( phdr->p_offset + phdr->p_filesz ),
  53. dest, ( dest + phdr->p_filesz ), ( dest + phdr->p_memsz ) );
  54. /* Verify and prepare segment */
  55. if ( ( rc = prep_segment ( buffer, phdr->p_filesz,
  56. phdr->p_memsz ) ) != 0 ) {
  57. DBGC ( image, "ELF %p could not prepare segment: %s\n",
  58. image, strerror ( rc ) );
  59. return rc;
  60. }
  61. /* Copy image to segment */
  62. memcpy_user ( buffer, 0, image->data, phdr->p_offset, phdr->p_filesz );
  63. return 0;
  64. }
  65. /**
  66. * Process ELF segment
  67. *
  68. * @v image ELF file
  69. * @v ehdr ELF executable header
  70. * @v phdr ELF program header
  71. * @v process Segment processor
  72. * @ret entry Entry point, if found
  73. * @ret max Maximum used address
  74. * @ret rc Return status code
  75. */
  76. static int elf_segment ( struct image *image, Elf_Ehdr *ehdr, Elf_Phdr *phdr,
  77. int ( * process ) ( struct image *image,
  78. Elf_Phdr *phdr, physaddr_t dest ),
  79. physaddr_t *entry, physaddr_t *max ) {
  80. physaddr_t dest;
  81. physaddr_t end;
  82. unsigned long e_offset;
  83. int rc;
  84. /* Do nothing for non-PT_LOAD segments */
  85. if ( phdr->p_type != PT_LOAD )
  86. return 0;
  87. /* Check segment lies within image */
  88. if ( ( phdr->p_offset + phdr->p_filesz ) > image->len ) {
  89. DBGC ( image, "ELF %p segment outside image\n", image );
  90. return -ENOEXEC;
  91. }
  92. /* Find start address: use physical address for preference,
  93. * fall back to virtual address if no physical address
  94. * supplied.
  95. */
  96. dest = phdr->p_paddr;
  97. if ( ! dest )
  98. dest = phdr->p_vaddr;
  99. if ( ! dest ) {
  100. DBGC ( image, "ELF %p segment loads to physical address 0\n",
  101. image );
  102. return -ENOEXEC;
  103. }
  104. end = ( dest + phdr->p_memsz );
  105. /* Update maximum used address, if applicable */
  106. if ( end > *max )
  107. *max = end;
  108. /* Process segment */
  109. if ( ( rc = process ( image, phdr, dest ) ) != 0 )
  110. return rc;
  111. /* Set execution address, if it lies within this segment */
  112. if ( ( e_offset = ( ehdr->e_entry - dest ) ) < phdr->p_filesz ) {
  113. *entry = ehdr->e_entry;
  114. DBGC ( image, "ELF %p found physical entry point at %lx\n",
  115. image, *entry );
  116. } else if ( ( e_offset = ( ehdr->e_entry - phdr->p_vaddr ) )
  117. < phdr->p_filesz ) {
  118. if ( ! *entry ) {
  119. *entry = ( dest + e_offset );
  120. DBGC ( image, "ELF %p found virtual entry point at %lx"
  121. " (virt %lx)\n", image, *entry,
  122. ( ( unsigned long ) ehdr->e_entry ) );
  123. }
  124. }
  125. return 0;
  126. }
  127. /**
  128. * Process ELF segments
  129. *
  130. * @v image ELF file
  131. * @v ehdr ELF executable header
  132. * @v process Segment processor
  133. * @ret entry Entry point, if found
  134. * @ret max Maximum used address
  135. * @ret rc Return status code
  136. */
  137. int elf_segments ( struct image *image, Elf_Ehdr *ehdr,
  138. int ( * process ) ( struct image *image, Elf_Phdr *phdr,
  139. physaddr_t dest ),
  140. physaddr_t *entry, physaddr_t *max ) {
  141. Elf_Phdr phdr;
  142. Elf_Off phoff;
  143. unsigned int phnum;
  144. int rc;
  145. /* Initialise maximum used address */
  146. *max = 0;
  147. /* Invalidate entry point */
  148. *entry = 0;
  149. /* Read and process ELF program headers */
  150. for ( phoff = ehdr->e_phoff , phnum = ehdr->e_phnum ; phnum ;
  151. phoff += ehdr->e_phentsize, phnum-- ) {
  152. if ( phoff > image->len ) {
  153. DBGC ( image, "ELF %p program header %d outside "
  154. "image\n", image, phnum );
  155. return -ENOEXEC;
  156. }
  157. copy_from_user ( &phdr, image->data, phoff, sizeof ( phdr ) );
  158. if ( ( rc = elf_segment ( image, ehdr, &phdr, process,
  159. entry, max ) ) != 0 )
  160. return rc;
  161. }
  162. /* Check for a valid execution address */
  163. if ( ! *entry ) {
  164. DBGC ( image, "ELF %p entry point %lx outside image\n",
  165. image, ( ( unsigned long ) ehdr->e_entry ) );
  166. return -ENOEXEC;
  167. }
  168. return 0;
  169. }
  170. /**
  171. * Load ELF image into memory
  172. *
  173. * @v image ELF file
  174. * @ret entry Entry point
  175. * @ret max Maximum used address
  176. * @ret rc Return status code
  177. */
  178. int elf_load ( struct image *image, physaddr_t *entry, physaddr_t *max ) {
  179. static const uint8_t e_ident[] = {
  180. [EI_MAG0] = ELFMAG0,
  181. [EI_MAG1] = ELFMAG1,
  182. [EI_MAG2] = ELFMAG2,
  183. [EI_MAG3] = ELFMAG3,
  184. [EI_CLASS] = ELFCLASS,
  185. };
  186. Elf_Ehdr ehdr;
  187. int rc;
  188. /* Read ELF header */
  189. copy_from_user ( &ehdr, image->data, 0, sizeof ( ehdr ) );
  190. if ( memcmp ( &ehdr.e_ident[EI_MAG0], e_ident,
  191. sizeof ( e_ident ) ) != 0 ) {
  192. DBGC ( image, "ELF %p has invalid signature\n", image );
  193. return -ENOEXEC;
  194. }
  195. /* Load ELF segments into memory */
  196. if ( ( rc = elf_segments ( image, &ehdr, elf_load_segment,
  197. entry, max ) ) != 0 )
  198. return rc;
  199. return 0;
  200. }