OSDN Git Service

- tidy up utils_install
[uclinux-h8/uClibc.git] / utils / readelf.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * A small little readelf implementation for uClibc
4  *
5  * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
6  *
7  * Several functions in this file (specifically, elf_find_section_type(),
8  * elf_find_phdr_type(), and elf_find_dynamic(), were stolen from elflib.c from
9  * elfvector (http://www.BitWagon.com/elfvector.html) by John F. Reiser
10  * <jreiser@BitWagon.com>, which is copyright 2000 BitWagon Software LLC
11  * (GPL2).
12  *
13  * Licensed under GPLv2 or later
14  */
15
16
17 #include <fcntl.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <sys/mman.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25
26 #include "bswap.h"
27 #include "link.h"
28 /* makefile will include elf.h for us */
29
30 static int byteswap;
31 static __inline__ uint32_t byteswap32_to_host(uint32_t value)
32 {
33         if (byteswap==1) {
34                 return(bswap_32(value));
35         } else {
36                 return(value);
37         }
38 }
39 static __inline__ uint64_t byteswap64_to_host(uint64_t value)
40 {
41         if (byteswap==1) {
42                 return(bswap_64(value));
43         } else {
44                 return(value);
45         }
46 }
47 #if __WORDSIZE == 64
48 # define byteswap_to_host(x) byteswap64_to_host(x)
49 #else
50 # define byteswap_to_host(x) byteswap32_to_host(x)
51 #endif
52
53 static ElfW(Shdr) * elf_find_section_type( uint32_t key, ElfW(Ehdr) *ehdr)
54 {
55         int j;
56         ElfW(Shdr) *shdr = (ElfW(Shdr) *)(ehdr->e_shoff + (char *)ehdr);
57         for (j = ehdr->e_shnum; --j>=0; ++shdr) {
58                 if (key==byteswap32_to_host(shdr->sh_type)) {
59                         return shdr;
60                 }
61         }
62         return NULL;
63 }
64
65 static ElfW(Phdr) * elf_find_phdr_type( uint32_t type, ElfW(Ehdr) *ehdr)
66 {
67         int j;
68         ElfW(Phdr) *phdr = (ElfW(Phdr) *)(ehdr->e_phoff + (char *)ehdr);
69         for (j = ehdr->e_phnum; --j>=0; ++phdr) {
70                 if (type==byteswap32_to_host(phdr->p_type)) {
71                         return phdr;
72                 }
73         }
74         return NULL;
75 }
76
77 /* Returns value if return_val==1, ptr otherwise */
78 static void * elf_find_dynamic( int64_t const key, ElfW(Dyn) *dynp,
79         ElfW(Ehdr) *ehdr, int return_val)
80 {
81         ElfW(Phdr) *pt_text = elf_find_phdr_type(PT_LOAD, ehdr);
82         ElfW(Addr) tx_reloc = byteswap_to_host(pt_text->p_vaddr) - byteswap_to_host(pt_text->p_offset);
83         for (; DT_NULL!=byteswap_to_host(dynp->d_tag); ++dynp) {
84                 if (key == byteswap_to_host(dynp->d_tag)) {
85                         if (return_val == 1)
86                                 return (void *)byteswap_to_host(dynp->d_un.d_val);
87                         else
88                                 return (void *)(byteswap_to_host(dynp->d_un.d_val) - tx_reloc + (char *)ehdr );
89                 }
90         }
91         return NULL;
92 }
93
94 static int check_elf_header(ElfW(Ehdr) *const ehdr)
95 {
96         if (! ehdr || strncmp((void *)ehdr, ELFMAG, SELFMAG) != 0 ||
97                         (ehdr->e_ident[EI_CLASS] != ELFCLASS32 &&
98                          ehdr->e_ident[EI_CLASS] != ELFCLASS64) ||
99                         ehdr->e_ident[EI_VERSION] != EV_CURRENT)
100         {
101                 return 1;
102         }
103
104         /* Check if the target endianness matches the host's endianness */
105         byteswap = 0;
106 #if __BYTE_ORDER == __LITTLE_ENDIAN
107         if (ehdr->e_ident[5] == ELFDATA2MSB) {
108                 /* Ick -- we will have to byte-swap everything */
109                 byteswap = 1;
110         }
111 #elif __BYTE_ORDER == __BIG_ENDIAN
112         if (ehdr->e_ident[5] == ELFDATA2LSB) {
113                 byteswap = 1;
114         }
115 #else
116 #error Unknown host byte order!
117 #endif
118         /* Be vary lazy, and only byteswap the stuff we use */
119         if (byteswap==1) {
120                 ehdr->e_type=bswap_16(ehdr->e_type);
121                 ehdr->e_machine=bswap_16(ehdr->e_machine);
122                 ehdr->e_phoff=byteswap_to_host(ehdr->e_phoff);
123                 ehdr->e_shoff=byteswap_to_host(ehdr->e_shoff);
124                 ehdr->e_phnum=bswap_16(ehdr->e_phnum);
125                 ehdr->e_shnum=bswap_16(ehdr->e_shnum);
126         }
127         return 0;
128 }
129
130
131 static void describe_elf_hdr(ElfW(Ehdr)* ehdr)
132 {
133         char *tmp, *tmp1;
134
135         switch (ehdr->e_type) {
136                 case ET_NONE:   tmp = "None"; tmp1 = "NONE"; break;
137                 case ET_REL:    tmp = "Relocatable File"; tmp1 = "REL"; break;
138                 case ET_EXEC:   tmp = "Executable file"; tmp1 = "EXEC"; break;
139                 case ET_DYN:    tmp = "Shared object file"; tmp1 = "DYN"; break;
140                 case ET_CORE:   tmp = "Core file"; tmp1 = "CORE"; break;
141                 default:
142                                                 tmp = tmp1 = "Unknown";
143         }
144         printf( "Type:\t\t%s (%s)\n", tmp1, tmp);
145
146         switch (ehdr->e_machine) {
147                 case EM_NONE:           tmp="No machine"; break;
148                 case EM_M32:            tmp="AT&T WE 32100"; break;
149                 case EM_SPARC:          tmp="SUN SPARC"; break;
150                 case EM_386:            tmp="Intel 80386"; break;
151                 case EM_68K:            tmp="Motorola m68k family"; break;
152                 case EM_88K:            tmp="Motorola m88k family"; break;
153                 case EM_486:            tmp="Intel 80486"; break;
154                 case EM_860:            tmp="Intel 80860"; break;
155                 case EM_MIPS:           tmp="MIPS R3000 big-endian"; break;
156                 case EM_S370:           tmp="IBM System/370"; break;
157                 case EM_MIPS_RS3_LE:    tmp="MIPS R3000 little-endian"; break;
158                 case EM_OLD_SPARCV9:    tmp="Sparc v9 (old)"; break;
159                 case EM_PARISC:         tmp="HPPA"; break;
160                 /*case EM_PPC_OLD:      tmp="Power PC (old)"; break;  conflicts with EM_VPP500 */
161                 case EM_SPARC32PLUS:    tmp="Sun's v8plus"; break;
162                 case EM_960:            tmp="Intel 80960"; break;
163                 case EM_PPC:            tmp="PowerPC"; break;
164                 case EM_PPC64:          tmp="PowerPC 64-bit"; break;
165                 case EM_V800:           tmp="NEC V800 series"; break;
166                 case EM_FR20:           tmp="Fujitsu FR20"; break;
167                 case EM_RH32:           tmp="TRW RH-32"; break;
168                 case EM_MCORE:          tmp="MCORE"; break;
169                 case EM_ARM:            tmp="ARM"; break;
170                 case EM_FAKE_ALPHA:     tmp="Digital Alpha"; break;
171                 case EM_SH:                     tmp="Renesas SH"; break;
172                 case EM_SPARCV9:        tmp="SPARC v9 64-bit"; break;
173                 case EM_TRICORE:        tmp="Siemens Tricore"; break;
174                 case EM_ARC:            tmp="Argonaut RISC Core"; break;
175                 case EM_H8_300:         tmp="Renesas H8/300"; break;
176                 case EM_H8_300H:        tmp="Renesas H8/300H"; break;
177                 case EM_H8S:            tmp="Renesas H8S"; break;
178                 case EM_H8_500:         tmp="Renesas H8/500"; break;
179                 case EM_IA_64:          tmp="Intel Merced"; break;
180                 case EM_MIPS_X:         tmp="Stanford MIPS-X"; break;
181                 case EM_COLDFIRE:       tmp="Motorola Coldfire"; break;
182                 case EM_68HC12:         tmp="Motorola M68HC12"; break;
183                 case EM_ALPHA:          tmp="Alpha"; break;
184                 case EM_CYGNUS_D10V:
185                 case EM_D10V:           tmp="Mitsubishi D10V"; break;
186                 case EM_CYGNUS_D30V:
187                 case EM_D30V:           tmp="Mitsubishi D30V"; break;
188                 case EM_CYGNUS_M32R:
189                 case EM_M32R:           tmp="Renesas M32R (formerly Mitsubishi M32r)"; break;
190                 case EM_CYGNUS_V850:
191                 case EM_V850:           tmp="NEC v850"; break;
192                 case EM_CYGNUS_MN10300:
193                 case EM_MN10300:        tmp="Matsushita MN10300"; break;
194                 case EM_CYGNUS_MN10200:
195                 case EM_MN10200:        tmp="Matsushita MN10200"; break;
196                 case EM_CYGNUS_FR30:
197                 case EM_FR30:           tmp="Fujitsu FR30"; break;
198                 case EM_CYGNUS_FRV:
199                 case EM_PJ_OLD:
200                 case EM_PJ:                     tmp="picoJava"; break;
201                 case EM_MMA:            tmp="Fujitsu MMA Multimedia Accelerator"; break;
202                 case EM_PCP:            tmp="Siemens PCP"; break;
203                 case EM_NCPU:           tmp="Sony nCPU embeeded RISC"; break;
204                 case EM_NDR1:           tmp="Denso NDR1 microprocessor"; break;
205                 case EM_STARCORE:       tmp="Motorola Start*Core processor"; break;
206                 case EM_ME16:           tmp="Toyota ME16 processor"; break;
207                 case EM_ST100:          tmp="STMicroelectronic ST100 processor"; break;
208                 case EM_TINYJ:          tmp="Advanced Logic Corp. Tinyj emb.fam"; break;
209                 case EM_FX66:           tmp="Siemens FX66 microcontroller"; break;
210                 case EM_ST9PLUS:        tmp="STMicroelectronics ST9+ 8/16 mc"; break;
211                 case EM_ST7:            tmp="STmicroelectronics ST7 8 bit mc"; break;
212                 case EM_68HC16:         tmp="Motorola MC68HC16 microcontroller"; break;
213                 case EM_68HC11:         tmp="Motorola MC68HC11 microcontroller"; break;
214                 case EM_68HC08:         tmp="Motorola MC68HC08 microcontroller"; break;
215                 case EM_68HC05:         tmp="Motorola MC68HC05 microcontroller"; break;
216                 case EM_SVX:            tmp="Silicon Graphics SVx"; break;
217                 case EM_ST19:           tmp="STMicroelectronics ST19 8 bit mc"; break;
218                 case EM_VAX:            tmp="Digital VAX"; break;
219                 case EM_AVR_OLD:
220                 case EM_AVR:            tmp="Atmel AVR 8-bit microcontroller"; break;
221                 case EM_CRIS:           tmp="Axis Communications 32-bit embedded processor"; break;
222                 case EM_JAVELIN:        tmp="Infineon Technologies 32-bit embedded processor"; break;
223                 case EM_FIREPATH:       tmp="Element 14 64-bit DSP Processor"; break;
224                 case EM_ZSP:            tmp="LSI Logic 16-bit DSP Processor"; break;
225                 case EM_MMIX:           tmp="Donald Knuth's educational 64-bit processor"; break;
226                 case EM_HUANY:          tmp="Harvard University machine-independent object files"; break;
227                 case EM_PRISM:          tmp="SiTera Prism"; break;
228                 case EM_X86_64:         tmp="AMD x86-64 architecture"; break;
229                 case EM_S390_OLD:
230                 case EM_S390:           tmp="IBM S390"; break;
231                 case EM_XSTORMY16:      tmp="Sanyo Xstormy16 CPU core"; break;
232                 case EM_OPENRISC:
233                 case EM_OR32:           tmp="OpenRISC"; break;
234                 case EM_CRX:            tmp="National Semiconductor CRX microprocessor"; break;
235                 case EM_DLX:            tmp="OpenDLX"; break;
236                 case EM_IP2K_OLD:
237                 case EM_IP2K:           tmp="Ubicom IP2xxx 8-bit microcontrollers"; break;
238                 case EM_IQ2000:         tmp="Vitesse IQ2000"; break;
239                 case EM_XTENSA_OLD:
240                 case EM_XTENSA:         tmp="Tensilica Xtensa Processor"; break;
241                 case EM_M32C:           tmp="Renesas M32c"; break;
242                 case EM_MT:                     tmp="Morpho Techologies MT processor"; break;
243                 case EM_BLACKFIN:       tmp="Analog Devices Blackfin"; break;
244                 case EM_NIOS32:         tmp="Altera Nios 32"; break;
245                 case EM_ALTERA_NIOS2:   tmp="Altera Nios II"; break;
246                 case EM_VPP500:         tmp="Fujitsu VPP500"; break;
247                 case EM_PDSP:           tmp="Sony DSP Processor"; break;
248                 default:                        tmp="unknown";
249         }
250         printf( "Machine:\t%s\n", tmp);
251
252         switch (ehdr->e_ident[EI_CLASS]) {
253                 case ELFCLASSNONE: tmp = "Invalid class";  break;
254                 case ELFCLASS32:   tmp = "ELF32"; break;
255                 case ELFCLASS64:   tmp = "ELF64"; break;
256                 default:           tmp = "Unknown";
257         }
258         printf( "Class:\t\t%s\n", tmp);
259
260         switch (ehdr->e_ident[EI_DATA]) {
261                 case ELFDATANONE:  tmp = "Invalid data encoding"; break;
262                 case ELFDATA2LSB:  tmp = "2's complement, little endian"; break;
263                 case ELFDATA2MSB:  tmp = "2's complement, big endian"; break;
264                 default:           tmp = "Unknown";
265         }
266         printf( "Data:\t\t%s\n", tmp);
267
268         printf( "Version:\t%d %s\n", ehdr->e_ident[EI_VERSION],
269                         (ehdr->e_ident[EI_VERSION]==EV_CURRENT)?
270                         "(current)" : "(unknown: %lx)");
271
272         switch (ehdr->e_ident[EI_OSABI]) {
273                 case ELFOSABI_SYSV:       tmp ="UNIX - System V"; break;
274                 case ELFOSABI_HPUX:       tmp ="UNIX - HP-UX"; break;
275                 case ELFOSABI_NETBSD:     tmp ="UNIX - NetBSD"; break;
276                 case ELFOSABI_LINUX:      tmp ="UNIX - Linux"; break;
277                 case ELFOSABI_HURD:       tmp ="GNU/Hurd"; break;
278                 case ELFOSABI_SOLARIS:    tmp ="UNIX - Solaris"; break;
279                 case ELFOSABI_AIX:        tmp ="UNIX - AIX"; break;
280                 case ELFOSABI_IRIX:       tmp ="UNIX - IRIX"; break;
281                 case ELFOSABI_FREEBSD:    tmp ="UNIX - FreeBSD"; break;
282                 case ELFOSABI_TRU64:      tmp ="UNIX - TRU64"; break;
283                 case ELFOSABI_MODESTO:    tmp ="Novell - Modesto"; break;
284                 case ELFOSABI_OPENBSD:    tmp ="UNIX - OpenBSD"; break;
285                 case ELFOSABI_STANDALONE: tmp ="Standalone App"; break;
286                 case ELFOSABI_ARM:        tmp ="ARM"; break;
287                 default:                  tmp = "Unknown";
288         }
289         printf( "OS/ABI:\t\t%s\n", tmp);
290
291         printf( "ABI Version:\t%d\n", ehdr->e_ident[EI_ABIVERSION]);
292 }
293
294 static void list_needed_libraries(ElfW(Dyn)* dynamic, char *strtab)
295 {
296         ElfW(Dyn)  *dyns;
297
298         printf("Dependancies:\n");
299         for (dyns=dynamic; byteswap_to_host(dyns->d_tag)!=DT_NULL; ++dyns) {
300                 if (dyns->d_tag == DT_NEEDED) {
301                         printf("\t%s\n", (char*)strtab + byteswap_to_host(dyns->d_un.d_val));
302                 }
303         }
304 }
305
306 static void describe_elf_interpreter(ElfW(Ehdr)* ehdr)
307 {
308         ElfW(Phdr) *phdr;
309         phdr = elf_find_phdr_type(PT_INTERP, ehdr);
310         if (phdr) {
311                 printf("Interpreter:\t%s\n", (char*)ehdr + byteswap_to_host(phdr->p_offset));
312         }
313 }
314
315 int main( int argc, char** argv)
316 {
317         /* map the .so, and locate interesting pieces */
318         char *dynstr;
319         char *thefilename = argv[1];
320         FILE *thefile;
321         struct stat statbuf;
322         ElfW(Ehdr) *ehdr = 0;
323         ElfW(Shdr) *dynsec;
324         ElfW(Dyn) *dynamic;
325
326         if (argc < 2 || !thefilename) {
327                 fprintf(stderr, "No filename specified.\n");
328                 exit(EXIT_FAILURE);
329         }
330         if (!(thefile = fopen(thefilename, "r"))) {
331                 perror(thefilename);
332                 exit(EXIT_FAILURE);
333         }
334         if (fstat(fileno(thefile), &statbuf) < 0) {
335                 perror(thefilename);
336                 exit(EXIT_FAILURE);
337         }
338
339         if ((size_t)statbuf.st_size < sizeof(ElfW(Ehdr)))
340                 goto foo;
341
342         /* mmap the file to make reading stuff from it effortless */
343         ehdr = (ElfW(Ehdr) *)mmap(0, statbuf.st_size,
344                         PROT_READ|PROT_WRITE, MAP_PRIVATE, fileno(thefile), 0);
345
346 foo:
347         /* Check if this looks legit */
348         if (check_elf_header(ehdr)) {
349                 fprintf(stderr, "This does not appear to be an ELF file.\n");
350                 exit(EXIT_FAILURE);
351         }
352         describe_elf_hdr(ehdr);
353         describe_elf_interpreter(ehdr);
354
355         dynsec = elf_find_section_type(SHT_DYNAMIC, ehdr);
356         if (dynsec) {
357                 dynamic = (ElfW(Dyn)*)(byteswap_to_host(dynsec->sh_offset) + (char *)ehdr);
358                 dynstr = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0);
359                 list_needed_libraries(dynamic, dynstr);
360         }
361
362         return 0;
363 }