readelf.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * A small little readelf implementation for uClibc
  4. *
  5. * Copyright (C) 2000 by Lineo, inc.
  6. * Copyright (C) 2000,2001 Erik Andersen <andersee@debian.org>
  7. * Written by Erik Andersen <andersee@debian.org>
  8. *
  9. * Several functions in this file (specifically, elf_find_section_type(),
  10. * elf_find_phdr_type(), and elf_find_dynamic(), were stolen from elflib.c from
  11. * elfvector (http://www.BitWagon.com/elfvector.html) by John F. Reiser
  12. * <jreiser@BitWagon.com>, which is copyright 2000 BitWagon Software LLC
  13. * (GPL2).
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  23. * General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  28. *
  29. */
  30. #include <elf.h>
  31. #include <fcntl.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <unistd.h>
  36. #include <sys/mman.h>
  37. #include <sys/stat.h>
  38. #include <sys/types.h>
  39. Elf32_Shdr * elf_find_section_type( int key, Elf32_Ehdr *ehdr)
  40. {
  41. int j;
  42. Elf32_Shdr *shdr = (Elf32_Shdr *)(ehdr->e_shoff + (char *)ehdr);
  43. for (j = ehdr->e_shnum; --j>=0; ++shdr) {
  44. if (shdr->sh_type == key) {
  45. return shdr;
  46. }
  47. }
  48. return NULL;
  49. }
  50. Elf32_Phdr * elf_find_phdr_type( int type, Elf32_Ehdr *ehdr)
  51. {
  52. int j;
  53. Elf32_Phdr *phdr = (Elf32_Phdr *)(ehdr->e_phoff + (char *)ehdr);
  54. for (j = ehdr->e_phnum; --j>=0; ++phdr) {
  55. if (type==phdr->p_type) {
  56. return phdr;
  57. }
  58. }
  59. return NULL;
  60. }
  61. /* Returns value if return_val==1, ptr otherwise */
  62. void * elf_find_dynamic(int const key, Elf32_Dyn *dynp,
  63. Elf32_Ehdr *ehdr, int return_val)
  64. {
  65. Elf32_Phdr *pt_text = elf_find_phdr_type(PT_LOAD, ehdr);
  66. unsigned tx_reloc = pt_text->p_vaddr - pt_text->p_offset;
  67. for (; DT_NULL!=dynp->d_tag; ++dynp) {
  68. if (dynp->d_tag == key) {
  69. if (return_val == 1)
  70. return (void *)dynp->d_un.d_val;
  71. else
  72. return (void *)(dynp->d_un.d_val - tx_reloc + (char *)ehdr );
  73. }
  74. }
  75. return NULL;
  76. }
  77. int check_elf_header(Elf32_Ehdr const *const ehdr)
  78. {
  79. if (! ehdr || strncmp((void *)ehdr, ELFMAG, SELFMAG) != 0 ||
  80. ehdr->e_ident[EI_CLASS] != ELFCLASS32 ||
  81. ehdr->e_ident[EI_VERSION] != EV_CURRENT)
  82. {
  83. return 1;
  84. }
  85. return 0;
  86. }
  87. #define ELFOSABI_NONE 0 /* UNIX System V ABI */
  88. #define ELFOSABI_HPUX 1 /* HP-UX operating system */
  89. #define ELFOSABI_NETBSD 2 /* NetBSD */
  90. #define ELFOSABI_LINUX 3 /* GNU/Linux */
  91. #define ELFOSABI_HURD 4 /* GNU/Hurd */
  92. #define ELFOSABI_SOLARIS 6 /* Solaris */
  93. #define ELFOSABI_AIX 7 /* AIX */
  94. #define ELFOSABI_IRIX 8 /* IRIX */
  95. #define ELFOSABI_FREEBSD 9 /* FreeBSD */
  96. #define ELFOSABI_TRU64 10 /* TRU64 UNIX */
  97. #define ELFOSABI_MODESTO 11 /* Novell Modesto */
  98. #define ELFOSABI_OPENBSD 12 /* OpenBSD */
  99. #define ELFOSABI_STANDALONE 255 /* Standalone (embedded) application */
  100. #define ELFOSABI_ARM 97 /* ARM */
  101. static void describe_elf_hdr(Elf32_Ehdr* ehdr)
  102. {
  103. char *tmp, *tmp1;
  104. switch (ehdr->e_type) {
  105. case ET_NONE: tmp = "None"; tmp1 = "NONE"; break;
  106. case ET_REL: tmp = "Relocatable File"; tmp1 = "REL"; break;
  107. case ET_EXEC: tmp = "Executable file"; tmp1 = "EXEC"; break;
  108. case ET_DYN: tmp = "Shared object file"; tmp1 = "DYN"; break;
  109. case ET_CORE: tmp = "Core file"; tmp1 = "CORE"; break;
  110. default:
  111. tmp = tmp1 = "Unknown";
  112. }
  113. printf( "Type:\t\t%s (%s)\n", tmp1, tmp);
  114. switch (ehdr->e_machine) {
  115. case EM_NONE: tmp="No machine"; break;
  116. case EM_M32: tmp="AT&T WE 32100"; break;
  117. case EM_SPARC: tmp="SUN SPARC"; break;
  118. case EM_386: tmp="Intel 80386"; break;
  119. case EM_68K: tmp="Motorola m68k family"; break;
  120. case EM_88K: tmp="Motorola m88k family"; break;
  121. case EM_860: tmp="Intel 80860"; break;
  122. case EM_MIPS: tmp="MIPS R3000 big-endian"; break;
  123. case EM_S370: tmp="IBM System/370"; break;
  124. case EM_MIPS_RS3_LE: tmp="MIPS R3000 little-endian"; break;
  125. case EM_PARISC: tmp="HPPA"; break;
  126. case EM_VPP500: tmp="Fujitsu VPP500"; break;
  127. case EM_SPARC32PLUS: tmp="Sun's v8plus"; break;
  128. case EM_960: tmp="Intel 80960"; break;
  129. case EM_PPC: tmp="PowerPC"; break;
  130. case EM_PPC64: tmp="PowerPC 64-bit"; break;
  131. case EM_S390: tmp="IBM S390"; break;
  132. case EM_V800: tmp="NEC V800 series"; break;
  133. case EM_FR20: tmp="Fujitsu FR20"; break;
  134. case EM_RH32: tmp="TRW RH-32"; break;
  135. case EM_RCE: tmp="Motorola RCE"; break;
  136. case EM_ARM: tmp="ARM"; break;
  137. case EM_FAKE_ALPHA: tmp="Digital Alpha"; break;
  138. case EM_SH: tmp="Hitachi SH"; break;
  139. case EM_SPARCV9: tmp="SPARC v9 64-bit"; break;
  140. case EM_TRICORE: tmp="Siemens Tricore"; break;
  141. case EM_ARC: tmp="Argonaut RISC Core"; break;
  142. case EM_H8_300: tmp="Hitachi H8/300"; break;
  143. case EM_H8_300H: tmp="Hitachi H8/300H"; break;
  144. case EM_H8S: tmp="Hitachi H8S"; break;
  145. case EM_H8_500: tmp="Hitachi H8/500"; break;
  146. case EM_IA_64: tmp="Intel Merced"; break;
  147. case EM_MIPS_X: tmp="Stanford MIPS-X"; break;
  148. case EM_COLDFIRE: tmp="Motorola Coldfire"; break;
  149. case EM_68HC12: tmp="Motorola M68HC12"; break;
  150. case EM_MMA: tmp="Fujitsu MMA Multimedia Accelerator"; break;
  151. case EM_PCP: tmp="Siemens PCP"; break;
  152. case EM_NCPU: tmp="Sony nCPU embeeded RISC"; break;
  153. case EM_NDR1: tmp="Denso NDR1 microprocessor"; break;
  154. case EM_STARCORE: tmp="Motorola Start*Core processor"; break;
  155. case EM_ME16: tmp="Toyota ME16 processor"; break;
  156. case EM_ST100: tmp="STMicroelectronic ST100 processor"; break;
  157. case EM_TINYJ: tmp="Advanced Logic Corp. Tinyj emb.fam"; break;
  158. case EM_X86_64: tmp="AMD x86-64 architecture"; break;
  159. case EM_PDSP: tmp="Sony DSP Processor"; break;
  160. case EM_FX66: tmp="Siemens FX66 microcontroller"; break;
  161. case EM_ST9PLUS: tmp="STMicroelectronics ST9+ 8/16 mc"; break;
  162. case EM_ST7: tmp="STmicroelectronics ST7 8 bit mc"; break;
  163. case EM_68HC16: tmp="Motorola MC68HC16 microcontroller"; break;
  164. case EM_68HC11: tmp="Motorola MC68HC11 microcontroller"; break;
  165. case EM_68HC08: tmp="Motorola MC68HC08 microcontroller"; break;
  166. case EM_68HC05: tmp="Motorola MC68HC05 microcontroller"; break;
  167. case EM_SVX: tmp="Silicon Graphics SVx"; break;
  168. case EM_AT19: tmp="STMicroelectronics ST19 8 bit mc"; break;
  169. case EM_VAX: tmp="Digital VAX"; break;
  170. case EM_CRIS: tmp="Axis Communications 32-bit embedded processor"; break;
  171. case EM_JAVELIN: tmp="Infineon Technologies 32-bit embedded processor"; break;
  172. case EM_FIREPATH: tmp="Element 14 64-bit DSP Processor"; break;
  173. case EM_ZSP: tmp="LSI Logic 16-bit DSP Processor"; break;
  174. case EM_MMIX: tmp="Donald Knuth's educational 64-bit processor"; break;
  175. case EM_HUANY: tmp="Harvard University machine-independent object files"; break;
  176. case EM_PRISM: tmp="SiTera Prism"; break;
  177. case EM_AVR: tmp="Atmel AVR 8-bit microcontroller"; break;
  178. case EM_FR30: tmp="Fujitsu FR30"; break;
  179. case EM_D10V: tmp="Mitsubishi D10V"; break;
  180. case EM_D30V: tmp="Mitsubishi D30V"; break;
  181. case EM_V850: tmp="NEC v850"; break;
  182. case EM_M32R: tmp="Mitsubishi M32R"; break;
  183. case EM_MN10300: tmp="Matsushita MN10300"; break;
  184. case EM_MN10200: tmp="Matsushita MN10200"; break;
  185. case EM_PJ: tmp="picoJava"; break;
  186. default: tmp="unknown";
  187. }
  188. printf( "Machine:\t%s\n", tmp);
  189. switch (ehdr->e_ident[EI_CLASS]) {
  190. case ELFCLASSNONE: tmp = "Invalid class"; break;
  191. case ELFCLASS32: tmp = "ELF32"; break;
  192. case ELFCLASS64: tmp = "ELF64"; break;
  193. default: tmp = "Unknown";
  194. }
  195. printf( "Class:\t\t%s\n", tmp);
  196. switch (ehdr->e_ident[EI_DATA]) {
  197. case ELFDATANONE: tmp = "Invalid data encoding"; break;
  198. case ELFDATA2LSB: tmp = "2's complement, little endian"; break;
  199. case ELFDATA2MSB: tmp = "2's complement, big endian"; break;
  200. default: tmp = "Unknown";
  201. }
  202. printf( "Data:\t\t%s\n", tmp);
  203. printf( "Version:\t%d %s\n", ehdr->e_ident[EI_VERSION],
  204. (ehdr->e_ident[EI_VERSION]==EV_CURRENT)?
  205. "(current)" : "(unknown: %lx)");
  206. switch (ehdr->e_ident[EI_OSABI]) {
  207. case ELFOSABI_SYSV: tmp ="UNIX - System V"; break;
  208. case ELFOSABI_HPUX: tmp ="UNIX - HP-UX"; break;
  209. case ELFOSABI_NETBSD: tmp ="UNIX - NetBSD"; break;
  210. case ELFOSABI_LINUX: tmp ="UNIX - Linux"; break;
  211. case ELFOSABI_HURD: tmp ="GNU/Hurd"; break;
  212. case ELFOSABI_SOLARIS: tmp ="UNIX - Solaris"; break;
  213. case ELFOSABI_AIX: tmp ="UNIX - AIX"; break;
  214. case ELFOSABI_IRIX: tmp ="UNIX - IRIX"; break;
  215. case ELFOSABI_FREEBSD: tmp ="UNIX - FreeBSD"; break;
  216. case ELFOSABI_TRU64: tmp ="UNIX - TRU64"; break;
  217. case ELFOSABI_MODESTO: tmp ="Novell - Modesto"; break;
  218. case ELFOSABI_OPENBSD: tmp ="UNIX - OpenBSD"; break;
  219. case ELFOSABI_STANDALONE: tmp ="Standalone App"; break;
  220. case ELFOSABI_ARM: tmp ="ARM"; break;
  221. default: tmp = "Unknown";
  222. }
  223. printf( "OS/ABI:\t\t%s\n", tmp);
  224. printf( "ABI Version:\t%d\n", ehdr->e_ident[EI_ABIVERSION]);
  225. }
  226. static void list_needed_libraries(Elf32_Dyn* dynamic, char *strtab)
  227. {
  228. Elf32_Dyn *dyns;
  229. printf("Dependancies:\n");
  230. for (dyns=dynamic; dyns->d_tag!=DT_NULL; ++dyns) {
  231. if (dyns->d_tag == DT_NEEDED) {
  232. printf("\t%s\n", (char*)strtab + dyns->d_un.d_val);
  233. }
  234. }
  235. }
  236. static void describe_elf_interpreter(Elf32_Ehdr* ehdr)
  237. {
  238. Elf32_Phdr *phdr;
  239. phdr = elf_find_phdr_type(PT_INTERP, ehdr);
  240. if (phdr) {
  241. printf("Interpreter:\t%s\n", (char*)ehdr + phdr->p_offset);
  242. }
  243. }
  244. int main( int argc, char** argv)
  245. {
  246. /* map the .so, and locate interesting pieces */
  247. char *dynstr;
  248. char *thefilename = argv[1];
  249. FILE *thefile;
  250. struct stat statbuf;
  251. Elf32_Ehdr *ehdr = 0;
  252. Elf32_Shdr *dynsec;
  253. Elf32_Dyn *dynamic;
  254. if (!thefilename) {
  255. fprintf(stderr, "No filename specified.\n");
  256. exit(EXIT_FAILURE);
  257. }
  258. if (!(thefile = fopen(thefilename, "r"))) {
  259. perror(thefilename);
  260. exit(EXIT_FAILURE);
  261. }
  262. if (fstat(fileno(thefile), &statbuf) < 0) {
  263. perror(thefilename);
  264. exit(EXIT_FAILURE);
  265. }
  266. if (statbuf.st_size < sizeof(Elf32_Ehdr))
  267. goto foo;
  268. /* mmap the file to make reading stuff from it effortless */
  269. ehdr = (Elf32_Ehdr *)mmap(0, statbuf.st_size,
  270. PROT_READ|PROT_WRITE, MAP_PRIVATE, fileno(thefile), 0);
  271. foo:
  272. /* Check if this looks legit */
  273. if (check_elf_header(ehdr)) {
  274. fprintf(stderr, "This does not appear to be an ELF file.\n");
  275. exit(EXIT_FAILURE);
  276. }
  277. describe_elf_hdr(ehdr);
  278. describe_elf_interpreter(ehdr);
  279. dynsec = elf_find_section_type(SHT_DYNAMIC, ehdr);
  280. if (dynsec) {
  281. dynamic = (Elf32_Dyn*)(dynsec->sh_offset + (int)ehdr);
  282. dynstr = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0);
  283. list_needed_libraries(dynamic, dynstr);
  284. }
  285. return 0;
  286. }