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