選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

elf.c 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. FILE_LICENCE ( GPL2_OR_LATER );
  20. /**
  21. * @file
  22. *
  23. * ELF image format
  24. *
  25. * A "pure" ELF image is not a bootable image. There are various
  26. * bootable formats based upon ELF (e.g. Multiboot), which share
  27. * common ELF-related functionality.
  28. */
  29. #include <errno.h>
  30. #include <elf.h>
  31. #include <ipxe/uaccess.h>
  32. #include <ipxe/segment.h>
  33. #include <ipxe/image.h>
  34. #include <ipxe/elf.h>
  35. typedef Elf32_Ehdr Elf_Ehdr;
  36. typedef Elf32_Phdr Elf_Phdr;
  37. typedef Elf32_Off Elf_Off;
  38. #define ELFCLASS ELFCLASS32
  39. /**
  40. * Load ELF segment into memory
  41. *
  42. * @v image ELF file
  43. * @v phdr ELF program header
  44. * @v ehdr ELF executable header
  45. * @ret entry Entry point, if found
  46. * @ret max Maximum used address
  47. * @ret rc Return status code
  48. */
  49. static int elf_load_segment ( struct image *image, Elf_Phdr *phdr,
  50. Elf_Ehdr *ehdr, physaddr_t *entry,
  51. physaddr_t *max ) {
  52. physaddr_t dest;
  53. physaddr_t end;
  54. userptr_t buffer;
  55. unsigned long e_offset;
  56. int rc;
  57. /* Do nothing for non-PT_LOAD segments */
  58. if ( phdr->p_type != PT_LOAD )
  59. return 0;
  60. /* Check segment lies within image */
  61. if ( ( phdr->p_offset + phdr->p_filesz ) > image->len ) {
  62. DBGC ( image, "ELF %p segment outside image\n", image );
  63. return -ENOEXEC;
  64. }
  65. /* Find start address: use physical address for preference,
  66. * fall back to virtual address if no physical address
  67. * supplied.
  68. */
  69. dest = phdr->p_paddr;
  70. if ( ! dest )
  71. dest = phdr->p_vaddr;
  72. if ( ! dest ) {
  73. DBGC ( image, "ELF %p segment loads to physical address 0\n",
  74. image );
  75. return -ENOEXEC;
  76. }
  77. buffer = phys_to_user ( dest );
  78. end = ( dest + phdr->p_memsz );
  79. DBGC ( image, "ELF %p loading segment [%x,%x) to [%x,%x,%x)\n", image,
  80. phdr->p_offset, ( phdr->p_offset + phdr->p_filesz ),
  81. phdr->p_paddr, ( phdr->p_paddr + phdr->p_filesz ),
  82. ( phdr->p_paddr + phdr->p_memsz ) );
  83. /* Verify and prepare segment */
  84. if ( ( rc = prep_segment ( buffer, phdr->p_filesz,
  85. phdr->p_memsz ) ) != 0 ) {
  86. DBGC ( image, "ELF %p could not prepare segment: %s\n",
  87. image, strerror ( rc ) );
  88. return rc;
  89. }
  90. /* Update maximum used address, if applicable */
  91. if ( end > *max )
  92. *max = end;
  93. /* Copy image to segment */
  94. memcpy_user ( buffer, 0, image->data, phdr->p_offset, phdr->p_filesz );
  95. /* Set execution address, if it lies within this segment */
  96. if ( ( e_offset = ( ehdr->e_entry - dest ) ) < phdr->p_filesz ) {
  97. *entry = ehdr->e_entry;
  98. DBGC ( image, "ELF %p found physical entry point at %lx\n",
  99. image, *entry );
  100. } else if ( ( e_offset = ( ehdr->e_entry - phdr->p_vaddr ) )
  101. < phdr->p_filesz ) {
  102. if ( ! *entry ) {
  103. *entry = ( dest + e_offset );
  104. DBGC ( image, "ELF %p found virtual entry point at %lx"
  105. " (virt %lx)\n", image, *entry,
  106. ( ( unsigned long ) ehdr->e_entry ) );
  107. }
  108. }
  109. return 0;
  110. }
  111. /**
  112. * Load ELF image into memory
  113. *
  114. * @v image ELF file
  115. * @ret entry Entry point
  116. * @ret max Maximum used address
  117. * @ret rc Return status code
  118. */
  119. int elf_load ( struct image *image, physaddr_t *entry, physaddr_t *max ) {
  120. static const uint8_t e_ident[] = {
  121. [EI_MAG0] = ELFMAG0,
  122. [EI_MAG1] = ELFMAG1,
  123. [EI_MAG2] = ELFMAG2,
  124. [EI_MAG3] = ELFMAG3,
  125. [EI_CLASS] = ELFCLASS,
  126. };
  127. Elf_Ehdr ehdr;
  128. Elf_Phdr phdr;
  129. Elf_Off phoff;
  130. unsigned int phnum;
  131. int rc;
  132. /* Read ELF header */
  133. copy_from_user ( &ehdr, image->data, 0, sizeof ( ehdr ) );
  134. if ( memcmp ( &ehdr.e_ident[EI_MAG0], e_ident,
  135. sizeof ( e_ident ) ) != 0 ) {
  136. DBGC ( image, "ELF %p has invalid signature\n", image );
  137. return -ENOEXEC;
  138. }
  139. /* Initialise maximum used address */
  140. *max = 0;
  141. /* Invalidate entry point */
  142. *entry = 0;
  143. /* Read ELF program headers */
  144. for ( phoff = ehdr.e_phoff , phnum = ehdr.e_phnum ; phnum ;
  145. phoff += ehdr.e_phentsize, phnum-- ) {
  146. if ( phoff > image->len ) {
  147. DBGC ( image, "ELF %p program header %d outside "
  148. "image\n", image, phnum );
  149. return -ENOEXEC;
  150. }
  151. copy_from_user ( &phdr, image->data, phoff, sizeof ( phdr ) );
  152. if ( ( rc = elf_load_segment ( image, &phdr, &ehdr,
  153. entry, max ) ) != 0 ) {
  154. return rc;
  155. }
  156. }
  157. /* Check for a valid execution address */
  158. if ( ! *entry ) {
  159. DBGC ( image, "ELF %p entry point %lx outside image\n",
  160. image, ( ( unsigned long ) ehdr.e_entry ) );
  161. return -ENOEXEC;
  162. }
  163. return 0;
  164. }