Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

elf.c 5.2KB

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