elf.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* Copyright (C) 2013 by John Cronin <jncronin@tysos.org>
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. * The above copyright notice and this permission notice shall be included in
  10. * all copies or substantial portions of the Software.
  11. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. * THE SOFTWARE.
  18. */
  19. #include <stdio.h>
  20. #include <stdint.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include "elf.h"
  24. int elf32_read_ehdr(FILE *fp, Elf32_Ehdr **ehdr)
  25. {
  26. *ehdr = (Elf32_Ehdr *)malloc(sizeof(Elf32_Ehdr));
  27. fseek(fp, 0, SEEK_SET);
  28. size_t bytes_to_read = sizeof(Elf32_Ehdr);
  29. size_t bytes_read = fread(*ehdr, 1, sizeof(Elf32_Ehdr), fp);
  30. if(bytes_to_read != bytes_read)
  31. {
  32. free(*ehdr);
  33. return ELF_FILE_LOAD_ERROR;
  34. }
  35. // Confirm its an ELF file
  36. if(((*ehdr)->e_ident[0] != 0x7f) || ((*ehdr)->e_ident[1] != 'E') ||
  37. ((*ehdr)->e_ident[2] != 'L') ||
  38. ((*ehdr)->e_ident[3] != 'F'))
  39. {
  40. free(*ehdr);
  41. return ELF_NOT_ELF;
  42. }
  43. // Confirm its a 32 bit file
  44. if((*ehdr)->e_ident[EI_CLASS] != ELFCLASS32)
  45. {
  46. free(*ehdr);
  47. return ELF_NOT_32_BIT;
  48. }
  49. // Confirm its a little-endian file
  50. if((*ehdr)->e_ident[EI_DATA] != ELFDATA2LSB)
  51. {
  52. free(*ehdr);
  53. return ELF_NOT_LITTLE_ENDIAN;
  54. }
  55. // Confirm its an executable file
  56. if((*ehdr)->e_type != ET_EXEC)
  57. {
  58. free(*ehdr);
  59. return ELF_NOT_EXEC;
  60. }
  61. // Confirm its for the ARM architecture
  62. if((*ehdr)->e_machine != EM_ARM)
  63. {
  64. free(*ehdr);
  65. return ELF_NOT_ARM;
  66. }
  67. return ELF_OK;
  68. }
  69. int elf32_read_shdrs(FILE *fp, Elf32_Ehdr *ehdr, uint8_t **shdrs)
  70. {
  71. size_t bytes_to_load = (size_t)(ehdr->e_shentsize * ehdr->e_shnum);
  72. fseek(fp, (long)ehdr->e_shoff, SEEK_SET);
  73. *shdrs = (uint8_t *)malloc(bytes_to_load);
  74. size_t bytes_read = fread(*shdrs, 1, bytes_to_load, fp);
  75. if(bytes_read != bytes_to_load)
  76. {
  77. free(*shdrs);
  78. return ELF_FILE_LOAD_ERROR;
  79. }
  80. return ELF_OK;
  81. }
  82. int elf32_load_section(FILE *fp, Elf32_Shdr *shdr)
  83. {
  84. if(shdr->sh_type == SHT_NOBITS)
  85. memset((void*)shdr->sh_addr, 0, shdr->sh_size);
  86. else
  87. {
  88. if(!shdr->sh_offset)
  89. return ELF_NO_OFFSET;
  90. fseek(fp, (long)shdr->sh_offset, SEEK_SET);
  91. size_t bytes_to_read = (size_t)shdr->sh_size;
  92. size_t bytes_read = fread((void *)shdr->sh_addr,
  93. 1, bytes_to_read, fp);
  94. if(bytes_to_read != bytes_read)
  95. return ELF_FILE_LOAD_ERROR;
  96. }
  97. return ELF_OK;
  98. }
  99. int elf32_read_phdrs(FILE *fp, Elf32_Ehdr *ehdr, uint8_t **phdrs)
  100. {
  101. size_t bytes_to_load = (size_t)(ehdr->e_phentsize * ehdr->e_phnum);
  102. fseek(fp, (long)ehdr->e_phoff, SEEK_SET);
  103. *phdrs = (uint8_t *)malloc(bytes_to_load);
  104. size_t bytes_read = fread(*phdrs, 1, bytes_to_load, fp);
  105. if(bytes_read != bytes_to_load)
  106. {
  107. free(*phdrs);
  108. return ELF_FILE_LOAD_ERROR;
  109. }
  110. return ELF_OK;
  111. }
  112. int elf32_load_segment(FILE *fp, Elf32_Phdr *phdr)
  113. {
  114. uint32_t load_address = phdr->p_vaddr;
  115. if(phdr->p_filesz)
  116. {
  117. // Load the file image
  118. fseek(fp, (long)phdr->p_offset, SEEK_SET);
  119. size_t bytes_to_load = (size_t)phdr->p_filesz;
  120. size_t bytes_read = fread((void*)load_address, 1,
  121. bytes_to_load, fp);
  122. if(bytes_read != bytes_to_load)
  123. return ELF_FILE_LOAD_ERROR;
  124. load_address += phdr->p_filesz;
  125. }
  126. if(phdr->p_memsz - phdr->p_filesz)
  127. {
  128. // Zero out the rest of the memory image
  129. memset((void*)load_address, 0, phdr->p_memsz -
  130. phdr->p_filesz);
  131. }
  132. return ELF_OK;
  133. }