readelf.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2013-2018 K Lange
  5. *
  6. * readelf - Show information about ELF objects
  7. *
  8. * This is a very custom implementation and nothing remotely
  9. * like the version that comes with binutils. Making it more
  10. * like that version might be worthwhile.
  11. */
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <stdint.h>
  15. #include <string.h>
  16. #include <errno.h>
  17. #include <kernel/elf.h>
  18. /**
  19. * Show usage for the readelf application.
  20. * @param argc Argument count (unused)
  21. * @param argv Arguments to binary
  22. */
  23. void usage(int argc, char ** argv) {
  24. /* Show usage */
  25. printf("%s [filename]\n", argv[0]);
  26. printf("\tDisplays information on ELF binaries such as section names,\n");
  27. printf("\tlocations, sizes, and loading positions in memory.\n");
  28. exit(1);
  29. }
  30. /**
  31. * Application entry point.
  32. * @returns 0 on sucess, 1 on failure
  33. */
  34. int main(int argc, char ** argv) {
  35. /* Process arguments */
  36. if (argc < 2) usage(argc,argv);
  37. FILE * binary; /**< File pointer for requested executable */
  38. size_t binary_size; /**< Size of the file */
  39. char * binary_buf; /**< Buffer to store the binary in memory */
  40. Elf32_Header * header; /**< ELF header */
  41. char * string_table = NULL; /**< The section header string table */
  42. char * sym_string_table = NULL; /**< The symbol string table */
  43. /* Open the requested binary */
  44. binary = fopen(argv[1], "r");
  45. if (!binary) {
  46. fprintf(stderr, "%s: %s: %s\n", argv[0], argv[1], strerror(errno));
  47. return 1;
  48. }
  49. /* Jump to the end so we can get the size */
  50. fseek(binary, 0, SEEK_END);
  51. binary_size = ftell(binary);
  52. fseek(binary, 0, SEEK_SET);
  53. /* Some sanity checks */
  54. if (binary_size < 4 || binary_size > 0xFFFFFFF) {
  55. printf("Oh no! I don't quite like the size of this binary.\n");
  56. return 1;
  57. }
  58. printf("Binary is %u bytes.\n", (unsigned int)binary_size);
  59. /* Read the binary into a buffer */
  60. binary_buf = malloc(binary_size);
  61. fread((void *)binary_buf, binary_size, 1, binary);
  62. /* Let's start considering this guy an elf, 'eh? */
  63. header = (Elf32_Header *)binary_buf;
  64. /* Verify the magic */
  65. if ( header->e_ident[0] != ELFMAG0 ||
  66. header->e_ident[1] != ELFMAG1 ||
  67. header->e_ident[2] != ELFMAG2 ||
  68. header->e_ident[3] != ELFMAG3) {
  69. printf("Header magic is wrong!\n");
  70. printf("Are you sure this is a 32-bit ELF binary or object file?\n");
  71. return 1;
  72. }
  73. /* Let's print out some of the header information, shall we? */
  74. printf("\033[1mELF Header\033[0m\n");
  75. /* File type */
  76. printf("[Type %d] ", header->e_type);
  77. switch (header->e_type) {
  78. case ET_NONE:
  79. printf("No file type.\n");
  80. break;
  81. case ET_REL:
  82. printf("Relocatable file.\n");
  83. break;
  84. case ET_EXEC:
  85. printf("Executable file.\n");
  86. break;
  87. case ET_DYN:
  88. printf("Shared object file.\n");
  89. break;
  90. case ET_CORE:
  91. printf("Core file.\n");
  92. break;
  93. default:
  94. printf("(Unknown file type)\n");
  95. break;
  96. }
  97. /* Machine Type */
  98. switch (header->e_machine) {
  99. case EM_386:
  100. printf("Intel x86\n");
  101. break;
  102. default:
  103. printf("Unknown machine: %d\n", header->e_machine);
  104. break;
  105. }
  106. /* Version == EV_CURRENT? */
  107. if (header->e_version == EV_CURRENT) {
  108. printf("ELF version is 1, as it should be.\n");
  109. }
  110. /* Entry point in memory */
  111. printf("Binary entry point in virtual memory is at 0x%x\n", (unsigned int)header->e_entry);
  112. /* Program header table offset */
  113. printf("Program header table is at +0x%x and one entry is 0x%x bytes.\n"
  114. "There are %d total program headers.\n",
  115. (unsigned int)header->e_phoff, (unsigned int)header->e_phentsize, (unsigned int)header->e_phnum);
  116. /* Section header table offset */
  117. printf("Section header table is at +0x%x and one entry is 0x%x bytes.\n"
  118. "There are %d total section headers.\n",
  119. (unsigned int)header->e_shoff, (unsigned int)header->e_shentsize, (unsigned int)header->e_shnum);
  120. /* Read the program headers */
  121. printf("\033[1mProgram Headers\033[0m\n");
  122. for (uint32_t x = 0; x < header->e_phentsize * header->e_phnum; x += header->e_phentsize) {
  123. if (header->e_phoff + x > binary_size) {
  124. printf("Tried to read beyond the end of the file.\n");
  125. return 1;
  126. }
  127. /* Grab the program header */
  128. Elf32_Phdr * phdr = (Elf32_Phdr *)((uintptr_t)binary_buf + (header->e_phoff + x));
  129. /* Print the header type */
  130. switch (phdr->p_type) {
  131. case PT_LOAD:
  132. printf("[Loadable Segment]\n");
  133. break;
  134. case PT_DYNAMIC:
  135. printf("[Dynamic Loading Information]\n");
  136. break;
  137. case PT_INTERP:
  138. printf("[Interpreter Path]\n");
  139. break;
  140. default:
  141. printf("[Unused Segement]\n");
  142. break;
  143. }
  144. }
  145. uint32_t i = 0;
  146. for (uint32_t x = 0; x < header->e_shentsize * header->e_shnum; x += header->e_shentsize) {
  147. if (header->e_shoff + x > binary_size) {
  148. printf("Tried to read beyond the end of the file.\n");
  149. return 1;
  150. }
  151. Elf32_Shdr * shdr = (Elf32_Shdr *)((uintptr_t)binary_buf + (header->e_shoff + x));
  152. if (shdr->sh_type == SHT_STRTAB) {
  153. if (i == header->e_shstrndx) {
  154. string_table = (char *)((uintptr_t)binary_buf + shdr->sh_offset);
  155. printf("Found the section string table at 0x%x\n", (unsigned int)shdr->sh_offset);
  156. }
  157. }
  158. i++;
  159. }
  160. if (!string_table) {
  161. printf("No string table, skipping rest of output.\n");
  162. return 1;
  163. }
  164. /* Find the (hopefully two) string tables */
  165. printf("\033[1mString Tables\033[0m\n");
  166. for (uint32_t x = 0; x < header->e_shentsize * header->e_shnum; x += header->e_shentsize) {
  167. if (header->e_shoff + x > binary_size) {
  168. printf("Tried to read beyond the end of the file.\n");
  169. return 1;
  170. }
  171. Elf32_Shdr * shdr = (Elf32_Shdr *)((uintptr_t)binary_buf + (header->e_shoff + x));
  172. if (shdr->sh_type == SHT_STRTAB) {
  173. if (!strcmp((char *)((uintptr_t)string_table + shdr->sh_name), ".strtab")) {
  174. sym_string_table = (char *)((uintptr_t)binary_buf + shdr->sh_offset);
  175. printf("Found the symbol string table at 0x%x\n", (unsigned int)shdr->sh_offset);
  176. }
  177. printf("Displaying string table at 0x%x\n", (unsigned int)shdr->sh_offset);
  178. char * _string_table = (char *)((uintptr_t)binary_buf + shdr->sh_offset);
  179. unsigned int j = 1;
  180. int k = 0;
  181. printf("%d\n", (unsigned int)shdr->sh_size);
  182. while (j < shdr->sh_size) {
  183. int t = strlen((char *)((uintptr_t)_string_table + j));
  184. if (t) {
  185. printf("%d [%d] %s\n", k, j, (char *)((uintptr_t)_string_table + j));
  186. k++;
  187. j += t;
  188. } else {
  189. j += 1;
  190. }
  191. }
  192. }
  193. }
  194. /* Read the section headers */
  195. printf("\033[1mSection Headers\033[0m\n");
  196. for (uint32_t x = 0; x < header->e_shentsize * header->e_shnum; x += header->e_shentsize) {
  197. if (header->e_shoff + x > binary_size) {
  198. printf("Tried to read beyond the end of the file.\n");
  199. return 1;
  200. }
  201. Elf32_Shdr * shdr = (Elf32_Shdr *)((uintptr_t)binary_buf + (header->e_shoff + x));
  202. printf("[%d] %s\n", (unsigned int)shdr->sh_type, (char *)((uintptr_t)string_table + shdr->sh_name));
  203. printf("Section starts at 0x%x and is 0x%x bytes long.\n", (unsigned int)shdr->sh_offset, (unsigned int)shdr->sh_size);
  204. if (shdr->sh_addr) {
  205. printf("It should be loaded at 0x%x.\n", (unsigned int)shdr->sh_addr);
  206. }
  207. }
  208. #if 1
  209. printf("\033[1mSymbol Tables\033[0m\n");
  210. for (uint32_t x = 0; x < header->e_shentsize * header->e_shnum; x += header->e_shentsize) {
  211. if (header->e_shoff + x > binary_size) {
  212. printf("Tried to read beyond the end of the file.\n");
  213. return 1;
  214. }
  215. Elf32_Shdr * shdr = (Elf32_Shdr *)((uintptr_t)binary_buf + (header->e_shoff + x));
  216. if (shdr->sh_type == SHT_SYMTAB) {
  217. printf("Found symbol table: %s\n", (char *)((uintptr_t)string_table + shdr->sh_name));
  218. Elf32_Sym * table = (Elf32_Sym *)((uintptr_t)binary_buf + (shdr->sh_offset));
  219. while ((uintptr_t)table - ((uintptr_t)binary_buf + shdr->sh_offset) < shdr->sh_size) {
  220. printf("%s: 0x%x [0x%x]\n", (char *)((uintptr_t)sym_string_table + table->st_name), (unsigned int)table->st_value, (unsigned int)table->st_size);
  221. table++;
  222. }
  223. }
  224. }
  225. #endif
  226. return 0;
  227. }