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 4.4KB

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