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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. * A "pure" ELF image is not a bootable image. There are various
  24. * bootable formats based upon ELF (e.g. Multiboot), which share
  25. * common ELF-related functionality.
  26. */
  27. #include <errno.h>
  28. #include <elf.h>
  29. #include <gpxe/uaccess.h>
  30. #include <gpxe/segment.h>
  31. #include <gpxe/image.h>
  32. #include <gpxe/elf.h>
  33. typedef Elf32_Ehdr Elf_Ehdr;
  34. typedef Elf32_Phdr Elf_Phdr;
  35. typedef Elf32_Off Elf_Off;
  36. /**
  37. * Load ELF segment into memory
  38. *
  39. * @v image ELF file
  40. * @v phdr ELF program header
  41. * @ret rc Return status code
  42. */
  43. static int elf_load_segment ( struct image *image, Elf_Phdr *phdr ) {
  44. physaddr_t dest;
  45. userptr_t buffer;
  46. int rc;
  47. /* Do nothing for non-PT_LOAD segments */
  48. if ( phdr->p_type != PT_LOAD )
  49. return 0;
  50. /* Check segment lies within image */
  51. if ( ( phdr->p_offset + phdr->p_filesz ) > image->len ) {
  52. DBG ( "ELF segment outside ELF file\n" );
  53. return -ENOEXEC;
  54. }
  55. /* Find start address: use physical address for preference,
  56. * fall back to virtual address if no physical address
  57. * supplied.
  58. */
  59. dest = phdr->p_paddr;
  60. if ( ! dest )
  61. dest = phdr->p_vaddr;
  62. if ( ! dest ) {
  63. DBG ( "ELF segment loads to physical address 0\n" );
  64. return -ENOEXEC;
  65. }
  66. buffer = phys_to_user ( dest );
  67. DBG ( "ELF loading segment [%x,%x) to [%x,%x,%x)\n",
  68. phdr->p_offset, ( phdr->p_offset + phdr->p_filesz ),
  69. phdr->p_paddr, ( phdr->p_paddr + phdr->p_filesz ),
  70. ( phdr->p_paddr + phdr->p_memsz ) );
  71. /* Verify and prepare segment */
  72. if ( ( rc = prep_segment ( buffer, phdr->p_filesz,
  73. phdr->p_memsz ) ) != 0 ) {
  74. DBG ( "ELF could not prepare segment: %s\n", strerror ( rc ) );
  75. return rc;
  76. }
  77. /* Copy image to segment */
  78. memcpy_user ( buffer, 0, image->data, phdr->p_offset, phdr->p_filesz );
  79. return 0;
  80. }
  81. /**
  82. * Load ELF image into memory
  83. *
  84. * @v image ELF file
  85. * @ret rc Return status code
  86. */
  87. int elf_load ( struct image *image ) {
  88. Elf_Ehdr ehdr;
  89. Elf_Phdr phdr;
  90. Elf_Off phoff;
  91. unsigned int phnum;
  92. int rc;
  93. /* Image type must already have been set by caller */
  94. assert ( image->type != NULL );
  95. /* Read ELF header */
  96. copy_from_user ( &ehdr, image->data, 0, sizeof ( ehdr ) );
  97. if ( memcmp ( &ehdr.e_ident[EI_MAG0], ELFMAG, SELFMAG ) != 0 ) {
  98. DBG ( "Invalid ELF signature\n" );
  99. return -ENOEXEC;
  100. }
  101. /* Read ELF program headers */
  102. for ( phoff = ehdr.e_phoff , phnum = ehdr.e_phnum ; phnum ;
  103. phoff += ehdr.e_phentsize, phnum-- ) {
  104. if ( phoff > image->len ) {
  105. DBG ( "ELF program header %d outside ELF image\n",
  106. phnum );
  107. return -ENOEXEC;
  108. }
  109. copy_from_user ( &phdr, image->data, phoff, sizeof ( phdr ) );
  110. if ( ( rc = elf_load_segment ( image, &phdr ) ) != 0 )
  111. return rc;
  112. }
  113. /* Record execution entry point in image private data field */
  114. image->priv.phys = ehdr.e_entry;
  115. return 0;
  116. }