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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. /**
  19. * @file
  20. *
  21. * ELF image format
  22. *
  23. */
  24. #include <errno.h>
  25. #include <elf.h>
  26. #include <gpxe/uaccess.h>
  27. #include <gpxe/segment.h>
  28. #include <gpxe/image.h>
  29. #include <gpxe/elf.h>
  30. struct image_type elf_image_type __image_type ( PROBE_NORMAL );
  31. typedef Elf32_Ehdr Elf_Ehdr;
  32. typedef Elf32_Phdr Elf_Phdr;
  33. typedef Elf32_Off Elf_Off;
  34. /**
  35. * Execute ELF image
  36. *
  37. * @v image ELF file
  38. * @ret rc Return status code
  39. */
  40. static int elf_exec ( struct image *image __unused ) {
  41. return -ENOTSUP;
  42. }
  43. /**
  44. * Load ELF segment into memory
  45. *
  46. * @v image ELF file
  47. * @v phdr ELF program header
  48. * @ret rc Return status code
  49. */
  50. static int elf_load_segment ( struct image *image, Elf_Phdr *phdr ) {
  51. physaddr_t dest;
  52. userptr_t buffer;
  53. int rc;
  54. /* Do nothing for non-PT_LOAD segments */
  55. if ( phdr->p_type != PT_LOAD )
  56. return 0;
  57. /* Check segment lies within image */
  58. if ( ( phdr->p_offset + phdr->p_filesz ) > image->len ) {
  59. DBG ( "ELF segment outside ELF file\n" );
  60. return -ENOEXEC;
  61. }
  62. /* Find start address: use physical address for preference,
  63. * fall back to virtual address if no physical address
  64. * supplied.
  65. */
  66. dest = phdr->p_paddr;
  67. if ( ! dest )
  68. dest = phdr->p_vaddr;
  69. if ( ! dest ) {
  70. DBG ( "ELF segment loads to physical address 0\n" );
  71. return -ENOEXEC;
  72. }
  73. buffer = phys_to_user ( dest );
  74. DBG ( "ELF loading segment [%lx,%lx) to [%lx,%lx,%lx)\n",
  75. phdr->p_offset, ( phdr->p_offset + phdr->p_filesz ),
  76. phdr->p_paddr, ( phdr->p_paddr + phdr->p_filesz ),
  77. ( phdr->p_paddr + phdr->p_memsz ) );
  78. /* Verify and prepare segment */
  79. if ( ( rc = prep_segment ( buffer, phdr->p_filesz,
  80. phdr->p_memsz ) ) != 0 ) {
  81. DBG ( "ELF could not prepare segment: %s\n", 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. return 0;
  87. }
  88. /**
  89. * Load ELF image into memory
  90. *
  91. * @v image ELF file
  92. * @ret rc Return status code
  93. */
  94. int elf_load ( struct image *image ) {
  95. Elf_Ehdr ehdr;
  96. Elf_Phdr phdr;
  97. Elf_Off phoff;
  98. unsigned int phnum;
  99. int rc;
  100. /* Read ELF header */
  101. copy_from_user ( &ehdr, image->data, 0, sizeof ( ehdr ) );
  102. if ( memcmp ( &ehdr.e_ident[EI_MAG0], ELFMAG, SELFMAG ) != 0 ) {
  103. DBG ( "Invalid ELF signature\n" );
  104. return -ENOEXEC;
  105. }
  106. /* This is an ELF image, valid or otherwise */
  107. if ( ! image->type )
  108. image->type = &elf_image_type;
  109. /* Read ELF program headers */
  110. for ( phoff = ehdr.e_phoff , phnum = ehdr.e_phnum ; phnum ;
  111. phoff += ehdr.e_phentsize, phnum-- ) {
  112. if ( phoff > image->len ) {
  113. DBG ( "ELF program header %d outside ELF image\n",
  114. phnum );
  115. return -ENOEXEC;
  116. }
  117. copy_from_user ( &phdr, image->data, phoff, sizeof ( phdr ) );
  118. if ( ( rc = elf_load_segment ( image, &phdr ) ) != 0 )
  119. return rc;
  120. }
  121. /* Record execution entry point in image private data field */
  122. image->priv.phys = ehdr.e_entry;
  123. return 0;
  124. }
  125. /** ELF image type */
  126. struct image_type elf_image_type __image_type ( PROBE_NORMAL ) = {
  127. .name = "ELF",
  128. .load = elf_load,
  129. .exec = elf_exec,
  130. };