1 /* $OpenBSD: readelf.c,v 1.8 2004/05/19 02:32:35 tedu Exp $ */
2 /*
3 * Copyright (c) Christos Zoulas 2003.
4 * All Rights Reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice immediately at the beginning of the file, without modification,
11 * this list of conditions, and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30 #include "file.h"
31
32 #ifdef BUILTIN_ELF
33 #include <sys/cdefs.h>
34 #include <string.h>
35 #include <ctype.h>
36 #include <stdlib.h>
37 #ifdef HAVE_UNISTD_H
38 #include <unistd.h>
39 #endif
40
41 #include "readelf.h"
42
43 __RCSID("$MirOS: src/usr.bin/file/readelf.c,v 1.7 2007/07/19 18:22:15 tg Exp $");
44
45 #ifdef ELFCORE
46 private int dophn_core(struct magic_set *, int, int, int, off_t, int, size_t);
47 #endif
48 private int dophn_exec(struct magic_set *, int, int, int, off_t, int, size_t);
49 private int dophn_noexec(struct magic_set *, int, int, int, off_t, int, size_t);
50 private int doshn(struct magic_set *, int, int, int, off_t, int, size_t);
51 private size_t donote(struct magic_set *, unsigned char *, size_t, size_t, int,
52 int, size_t);
53
54 #define ELF_ALIGN(a) ((((a) + align - 1) / align) * align)
55
56 private uint16_t getu16(int, uint16_t);
57 private uint32_t getu32(int, uint32_t);
58 private uint64_t getu64(int, uint64_t);
59
60 private uint16_t
getu16(int swap,uint16_t value)61 getu16(int swap, uint16_t value)
62 {
63 union {
64 uint16_t ui;
65 char c[2];
66 } retval, tmpval;
67
68 if (swap) {
69 tmpval.ui = value;
70
71 retval.c[0] = tmpval.c[1];
72 retval.c[1] = tmpval.c[0];
73
74 return retval.ui;
75 } else
76 return value;
77 }
78
79 private uint32_t
getu32(int swap,uint32_t value)80 getu32(int swap, uint32_t value)
81 {
82 union {
83 uint32_t ui;
84 char c[4];
85 } retval, tmpval;
86
87 if (swap) {
88 tmpval.ui = value;
89
90 retval.c[0] = tmpval.c[3];
91 retval.c[1] = tmpval.c[2];
92 retval.c[2] = tmpval.c[1];
93 retval.c[3] = tmpval.c[0];
94
95 return retval.ui;
96 } else
97 return value;
98 }
99
100 private uint64_t
getu64(int swap,uint64_t value)101 getu64(int swap, uint64_t value)
102 {
103 union {
104 uint64_t ui;
105 char c[8];
106 } retval, tmpval;
107
108 if (swap) {
109 tmpval.ui = value;
110
111 retval.c[0] = tmpval.c[7];
112 retval.c[1] = tmpval.c[6];
113 retval.c[2] = tmpval.c[5];
114 retval.c[3] = tmpval.c[4];
115 retval.c[4] = tmpval.c[3];
116 retval.c[5] = tmpval.c[2];
117 retval.c[6] = tmpval.c[1];
118 retval.c[7] = tmpval.c[0];
119
120 return retval.ui;
121 } else
122 return value;
123 }
124
125 #define sh_addr (class == ELFCLASS32 \
126 ? (void *) &sh32 \
127 : (void *) &sh64)
128 #define sh_size (class == ELFCLASS32 \
129 ? sizeof sh32 \
130 : sizeof sh64)
131 #define shs_type (class == ELFCLASS32 \
132 ? getu32(swap, sh32.sh_type) \
133 : getu32(swap, sh64.sh_type))
134 #define ph_addr (class == ELFCLASS32 \
135 ? (void *) &ph32 \
136 : (void *) &ph64)
137 #define ph_size (class == ELFCLASS32 \
138 ? sizeof ph32 \
139 : sizeof ph64)
140 #define ph_type (class == ELFCLASS32 \
141 ? getu32(swap, ph32.p_type) \
142 : getu32(swap, ph64.p_type))
143 #define ph_offset (class == ELFCLASS32 \
144 ? getu32(swap, ph32.p_offset) \
145 : getu64(swap, ph64.p_offset))
146 #define ph_align (size_t)((class == ELFCLASS32 \
147 ? (off_t) (ph32.p_align ? \
148 getu32(swap, ph32.p_align) : 4) \
149 : (off_t) (ph64.p_align ? \
150 getu64(swap, ph64.p_align) : 4)))
151 #define nh_size (class == ELFCLASS32 \
152 ? sizeof nh32 \
153 : sizeof nh64)
154 #define nh_type (class == ELFCLASS32 \
155 ? getu32(swap, nh32.n_type) \
156 : getu32(swap, nh64.n_type))
157 #define nh_namesz (class == ELFCLASS32 \
158 ? getu32(swap, nh32.n_namesz) \
159 : getu32(swap, nh64.n_namesz))
160 #define nh_descsz (class == ELFCLASS32 \
161 ? getu32(swap, nh32.n_descsz) \
162 : getu32(swap, nh64.n_descsz))
163 #define prpsoffsets(i) (class == ELFCLASS32 \
164 ? prpsoffsets32[i] \
165 : prpsoffsets64[i])
166
167 #ifdef ELFCORE
168 size_t prpsoffsets32[] = {
169 8, /* FreeBSD */
170 28, /* Linux 2.0.36 */
171 32, /* Linux (I forget which kernel version) */
172 84, /* SunOS 5.x */
173 };
174
175 size_t prpsoffsets64[] = {
176 120, /* SunOS 5.x, 64-bit */
177 };
178
179 #define NOFFSETS32 (sizeof prpsoffsets32 / sizeof prpsoffsets32[0])
180 #define NOFFSETS64 (sizeof prpsoffsets64 / sizeof prpsoffsets64[0])
181
182 #define NOFFSETS (class == ELFCLASS32 ? NOFFSETS32 : NOFFSETS64)
183
184 /*
185 * Look through the program headers of an executable image, searching
186 * for a PT_NOTE section of type NT_PRPSINFO, with a name "CORE" or
187 * "FreeBSD"; if one is found, try looking in various places in its
188 * contents for a 16-character string containing only printable
189 * characters - if found, that string should be the name of the program
190 * that dropped core. Note: right after that 16-character string is,
191 * at least in SunOS 5.x (and possibly other SVR4-flavored systems) and
192 * Linux, a longer string (80 characters, in 5.x, probably other
193 * SVR4-flavored systems, and Linux) containing the start of the
194 * command line for that program.
195 *
196 * The signal number probably appears in a section of type NT_PRSTATUS,
197 * but that's also rather OS-dependent, in ways that are harder to
198 * dissect with heuristics, so I'm not bothering with the signal number.
199 * (I suppose the signal number could be of interest in situations where
200 * you don't have the binary of the program that dropped core; if you
201 * *do* have that binary, the debugger will probably tell you what
202 * signal it was.)
203 */
204
205 #define OS_STYLE_SVR4 0
206 #define OS_STYLE_FREEBSD 1
207 #define OS_STYLE_NETBSD 2
208
209 private const char *os_style_names[] = {
210 "SVR4",
211 "FreeBSD",
212 "NetBSD",
213 };
214
215 private int
dophn_core(struct magic_set * ms,int class,int swap,int fd,off_t off,int num,size_t size)216 dophn_core(struct magic_set *ms, int class, int swap, int fd, off_t off,
217 int num, size_t size)
218 {
219 Elf32_Phdr ph32;
220 Elf64_Phdr ph64;
221 size_t offset;
222 unsigned char nbuf[BUFSIZ];
223 ssize_t bufsize;
224
225 if (size != ph_size) {
226 if (file_printf(ms, ", corrupted program header size") == -1)
227 return -1;
228 return 0;
229 }
230 /*
231 * Loop through all the program headers.
232 */
233 for ( ; num; num--) {
234 if (lseek(fd, off, SEEK_SET) == (off_t)-1) {
235 file_badseek(ms);
236 return -1;
237 }
238 if (read(fd, ph_addr, ph_size) == -1) {
239 file_badread(ms);
240 return -1;
241 }
242 off += size;
243 if (ph_type != PT_NOTE)
244 continue;
245
246 /*
247 * This is a PT_NOTE section; loop through all the notes
248 * in the section.
249 */
250 if (lseek(fd, (off_t) ph_offset, SEEK_SET) == (off_t)-1) {
251 file_badseek(ms);
252 return -1;
253 }
254 bufsize = read(fd, nbuf, BUFSIZ);
255 if (bufsize == -1) {
256 file_badread(ms);
257 return -1;
258 }
259 offset = 0;
260 for (;;) {
261 if (offset >= (size_t)bufsize)
262 break;
263 offset = donote(ms, nbuf, offset, (size_t)bufsize,
264 class, swap, 4);
265 if (offset == 0)
266 break;
267
268 }
269 }
270 return 0;
271 }
272 #endif
273
274 private size_t
donote(struct magic_set * ms,unsigned char * nbuf,size_t offset,size_t size,int class,int swap,size_t align)275 donote(struct magic_set *ms, unsigned char *nbuf, size_t offset, size_t size,
276 int class, int swap, size_t align)
277 {
278 Elf32_Nhdr nh32;
279 Elf64_Nhdr nh64;
280 size_t noff, doff;
281 #ifdef ELFCORE
282 int os_style = -1;
283 #endif
284 uint32_t namesz, descsz;
285
286 if (class == ELFCLASS32)
287 memcpy(&nh32, &nbuf[offset], sizeof(nh32));
288 else
289 memcpy(&nh64, &nbuf[offset], sizeof(nh64));
290 offset += nh_size;
291
292 namesz = nh_namesz;
293 descsz = nh_descsz;
294 if ((namesz == 0) && (descsz == 0)) {
295 /*
296 * We're out of note headers.
297 */
298 return offset;
299 }
300
301 if (namesz & 0x80000000) {
302 (void)file_printf(ms, ", bad note name size 0x%lx",
303 (unsigned long)namesz);
304 return offset;
305 }
306
307 if (descsz & 0x80000000) {
308 (void)file_printf(ms, ", bad note description size 0x%lx",
309 (unsigned long)descsz);
310 return offset;
311 }
312
313
314 noff = offset;
315 doff = ELF_ALIGN(offset + namesz);
316
317 if (offset + namesz >= size) {
318 /*
319 * We're past the end of the buffer.
320 */
321 return doff;
322 }
323
324 offset = ELF_ALIGN(doff + descsz);
325 if (offset + descsz >= size) {
326 return offset;
327 }
328
329 if (namesz == 4 && strcmp((char *)&nbuf[noff], "GNU") == 0 &&
330 nh_type == NT_GNU_VERSION && descsz == 16) {
331 uint32_t desc[4];
332 (void)memcpy(desc, &nbuf[doff], sizeof(desc));
333
334 if (file_printf(ms, ", for GNU/") == -1)
335 return size;
336 switch (getu32(swap, desc[0])) {
337 case GNU_OS_LINUX:
338 if (file_printf(ms, "Linux") == -1)
339 return size;
340 break;
341 case GNU_OS_HURD:
342 if (file_printf(ms, "Hurd") == -1)
343 return size;
344 break;
345 case GNU_OS_SOLARIS:
346 if (file_printf(ms, "Solaris") == -1)
347 return size;
348 break;
349 case GNU_OS_KFREEBSD:
350 if (file_printf(ms, "kFreeBSD") == -1)
351 return size;
352 break;
353 case GNU_OS_KNETBSD:
354 if (file_printf(ms, "kNetBSD") == -1)
355 return size;
356 break;
357 case GNU_OS_SYLLABLE:
358 if (file_printf(ms, "Syllable") == -1)
359 return size;
360 break;
361 default:
362 if (file_printf(ms, "<unknown>") == -1)
363 return size;
364 }
365 if (file_printf(ms, " %d.%d.%d", getu32(swap, desc[1]),
366 getu32(swap, desc[2]), getu32(swap, desc[3])) == -1)
367 return size;
368 return size;
369 }
370
371 if (nh_type == NT_MIRBSD_VERSION &&
372 ((namesz == 7 && strcmp((char *)&nbuf[noff], "MirBSD") == 0) ||
373 (namesz > NT_MIROS_STRTLEN && strncmp((char *)&nbuf[noff],
374 NT_MIROS_STRTEST, NT_MIROS_STRTLEN) == 0))) {
375 const char *tmp = (char *)&nbuf[noff];
376 uint32_t desc;
377
378 if (strcmp((char *)&nbuf[noff], "MirBSD") == 0)
379 tmp = "MirOS BSD";
380 if (file_printf(ms, ", for %s", tmp) == -1)
381 return size;
382
383 /* look for version of note */
384 (void)memcpy(&desc, &nbuf[doff], sizeof(desc));
385 desc = getu32(swap, desc);
386 if (!desc)
387 return size; /* first version */
388
389 /* any newer version */
390 if ((desc & 0xFF) == 0xFF) {
391 if (file_printf(ms, ", wrong endianness") == -1)
392 return size;
393 desc = getu32(1, desc);
394 }
395 if (file_printf(ms, ", crti version %02X", desc & 0xFF) == -1)
396 return size; /* newer version */
397
398 /* can't parse that yet, because it's unspecified */
399 return size;
400 }
401
402 if (namesz == 7 && strcmp((char *)&nbuf[noff], "NetBSD") == 0 &&
403 nh_type == NT_NETBSD_VERSION && descsz == 4) {
404 uint32_t desc;
405 (void)memcpy(&desc, &nbuf[doff], sizeof(desc));
406 desc = getu32(swap, desc);
407
408 if (file_printf(ms, ", for NetBSD") == -1)
409 return size;
410 /*
411 * The version number used to be stuck as 199905, and was thus
412 * basically content-free. Newer versions of NetBSD have fixed
413 * this and now use the encoding of __NetBSD_Version__:
414 *
415 * MMmmrrpp00
416 *
417 * M = major version
418 * m = minor version
419 * r = release ["",A-Z,Z[A-Z] but numeric]
420 * p = patchlevel
421 */
422 if (desc > 100000000U) {
423 u_int ver_patch = (desc / 100) % 100;
424 u_int ver_rel = (desc / 10000) % 100;
425 u_int ver_min = (desc / 1000000) % 100;
426 u_int ver_maj = desc / 100000000;
427
428 if (file_printf(ms, " %u.%u", ver_maj, ver_min) == -1)
429 return size;
430 if (ver_rel == 0 && ver_patch != 0) {
431 if (file_printf(ms, ".%u", ver_patch) == -1)
432 return size;
433 } else if (ver_rel != 0) {
434 while (ver_rel > 26) {
435 file_printf(ms, "Z");
436 ver_rel -= 26;
437 }
438 file_printf(ms, "%c", 'A' + ver_rel - 1);
439 }
440 }
441 return size;
442 }
443
444 if (namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0 &&
445 nh_type == NT_FREEBSD_VERSION && descsz == 4) {
446 uint32_t desc;
447 (void)memcpy(&desc, &nbuf[doff], sizeof(desc));
448 desc = getu32(swap, desc);
449 if (file_printf(ms, ", for FreeBSD") == -1)
450 return size;
451
452 /*
453 * Contents is __FreeBSD_version, whose relation to OS
454 * versions is defined by a huge table in the Porters'
455 * Handbook. For up to 5.x, the first three digits are
456 * the version number. For 5.x and higher, the scheme
457 * is: <major><two digit minor> <0 if release branch,
458 * otherwise 1>xx
459 */
460 if (desc / 100000 < 5) {
461 if (file_printf(ms, " %d.%d", desc / 100000,
462 desc / 10000 % 10) == -1)
463 return size;
464 if (desc / 1000 % 10 > 0)
465 if (file_printf(ms, ".%d", desc / 1000 % 10)
466 == -1)
467 return size;
468 } else {
469 if (file_printf(ms, " %d.%d", desc / 100000,
470 desc / 1000 % 100) == -1)
471 return size;
472 desc %= 1000;
473 if (desc > 100) {
474 if (file_printf(ms, "-CURRENT (rev %d)",
475 desc % 100) == -1)
476 return size;
477 } else if (desc != 0) {
478 if (file_printf(ms, ".%d", desc / 10) == -1)
479 return size;
480 }
481 }
482 return size;
483 }
484
485 if (namesz == 8 && strcmp((char *)&nbuf[noff], "OpenBSD") == 0 &&
486 nh_type == NT_OPENBSD_VERSION && descsz == 4) {
487 if (file_printf(ms, ", for OpenBSD") == -1)
488 return size;
489 /* Content of note is always 0 */
490 return size;
491 }
492
493 /*
494 * Sigh. The 2.0.36 kernel in Debian 2.1, at
495 * least, doesn't correctly implement name
496 * sections, in core dumps, as specified by
497 * the "Program Linking" section of "UNIX(R) System
498 * V Release 4 Programmer's Guide: ANSI C and
499 * Programming Support Tools", because my copy
500 * clearly says "The first 'namesz' bytes in 'name'
501 * contain a *null-terminated* [emphasis mine]
502 * character representation of the entry's owner
503 * or originator", but the 2.0.36 kernel code
504 * doesn't include the terminating null in the
505 * name....
506 */
507 if ((namesz == 4 && strncmp((char *)&nbuf[noff], "CORE", 4) == 0) ||
508 (namesz == 5 && strcmp((char *)&nbuf[noff], "CORE") == 0)) {
509 os_style = OS_STYLE_SVR4;
510 }
511
512 if ((namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0)) {
513 os_style = OS_STYLE_FREEBSD;
514 }
515
516 if ((namesz >= 11 && strncmp((char *)&nbuf[noff], "NetBSD-CORE", 11)
517 == 0)) {
518 os_style = OS_STYLE_NETBSD;
519 }
520
521 #ifdef ELFCORE
522 if (os_style != -1)
523 if (file_printf(ms, ", %s-style", os_style_names[os_style]) == -1)
524 return size;
525
526 if (os_style == OS_STYLE_NETBSD && nh_type == NT_NETBSD_CORE_PROCINFO) {
527 uint32_t signo;
528 /*
529 * Extract the program name. It is at
530 * offset 0x7c, and is up to 32-bytes,
531 * including the terminating NUL.
532 */
533 if (file_printf(ms, ", from '%.31s'", &nbuf[doff + 0x7c]) == -1)
534 return size;
535
536 /*
537 * Extract the signal number. It is at
538 * offset 0x08.
539 */
540 memcpy(&signo, &nbuf[doff + 0x08],
541 sizeof(signo));
542 if (file_printf(ms, " (signal %u)", getu32(swap, signo)) == -1)
543 return size;
544 return size;
545 } else if (os_style != OS_STYLE_NETBSD && nh_type == NT_PRPSINFO) {
546 size_t i, j;
547 unsigned char c;
548 /*
549 * Extract the program name. We assume
550 * it to be 16 characters (that's what it
551 * is in SunOS 5.x and Linux).
552 *
553 * Unfortunately, it's at a different offset
554 * in varous OSes, so try multiple offsets.
555 * If the characters aren't all printable,
556 * reject it.
557 */
558 for (i = 0; i < NOFFSETS; i++) {
559 size_t reloffset = prpsoffsets(i);
560 size_t noffset = doff + reloffset;
561 for (j = 0; j < 16; j++, noffset++, reloffset++) {
562 /*
563 * Make sure we're not past
564 * the end of the buffer; if
565 * we are, just give up.
566 */
567 if (noffset >= size)
568 goto tryanother;
569
570 /*
571 * Make sure we're not past
572 * the end of the contents;
573 * if we are, this obviously
574 * isn't the right offset.
575 */
576 if (reloffset >= descsz)
577 goto tryanother;
578
579 c = nbuf[noffset];
580 if (c == '\0') {
581 /*
582 * A '\0' at the
583 * beginning is
584 * obviously wrong.
585 * Any other '\0'
586 * means we're done.
587 */
588 if (j == 0)
589 goto tryanother;
590 else
591 break;
592 } else {
593 /*
594 * A nonprintable
595 * character is also
596 * wrong.
597 */
598 #define isquote(c) (strchr("'\"`", (c)) != NULL)
599 if (!isprint(c) || isquote(c))
600 goto tryanother;
601 }
602 }
603
604 /*
605 * Well, that worked.
606 */
607 if (file_printf(ms, ", from '%.16s'",
608 &nbuf[doff + prpsoffsets(i)]) == -1)
609 return size;
610 return size;
611
612 tryanother:
613 ;
614 }
615 return offset;
616 }
617 #endif
618 return offset;
619 }
620
621 private int
doshn(struct magic_set * ms,int class,int swap,int fd,off_t off,int num,size_t size)622 doshn(struct magic_set *ms, int class, int swap, int fd, off_t off, int num,
623 size_t size)
624 {
625 Elf32_Shdr sh32;
626 Elf64_Shdr sh64;
627
628 if (size != sh_size) {
629 if (file_printf(ms, ", corrupted section header size") == -1)
630 return -1;
631 return 0;
632 }
633
634 if (lseek(fd, off, SEEK_SET) == (off_t)-1) {
635 file_badseek(ms);
636 return -1;
637 }
638
639 for ( ; num; num--) {
640 if (read(fd, sh_addr, sh_size) == -1) {
641 file_badread(ms);
642 return -1;
643 }
644 if (shs_type == SHT_SYMTAB /* || shs_type == SHT_DYNSYM */) {
645 if (file_printf(ms, ", not stripped") == -1)
646 return -1;
647 return 0;
648 }
649 }
650 if (file_printf(ms, ", stripped") == -1)
651 return -1;
652 return 0;
653 }
654
655 /*
656 * Look through the program headers of an executable image, searching
657 * for a PT_INTERP section; if one is found, it's dynamically linked,
658 * otherwise it's statically linked.
659 */
660 private int
dophn_exec(struct magic_set * ms,int class,int swap,int fd,off_t off,int num,size_t size)661 dophn_exec(struct magic_set *ms, int class, int swap, int fd, off_t off,
662 int num, size_t size)
663 {
664 Elf32_Phdr ph32;
665 Elf64_Phdr ph64;
666 const char *linking_style = "statically";
667 const char *shared_libraries = "";
668 unsigned char nbuf[BUFSIZ];
669 int bufsize;
670 size_t offset, align;
671 off_t savedoffset;
672
673 if (size != ph_size) {
674 if (file_printf(ms, ", corrupted program header size") == -1)
675 return -1;
676 return 0;
677 }
678 if (lseek(fd, off, SEEK_SET) == (off_t)-1) {
679 file_badseek(ms);
680 return -1;
681 }
682
683 for ( ; num; num--) {
684 if (read(fd, ph_addr, ph_size) == -1) {
685 file_badread(ms);
686 return -1;
687 }
688 if ((savedoffset = lseek(fd, (off_t)0, SEEK_CUR)) == (off_t)-1) {
689 file_badseek(ms);
690 return -1;
691 }
692
693 switch (ph_type) {
694 case PT_DYNAMIC:
695 linking_style = "dynamically";
696 break;
697 case PT_INTERP:
698 shared_libraries = " (uses shared libs)";
699 break;
700 case PT_NOTE:
701 if ((align = ph_align) & 0x80000000) {
702 if (file_printf(ms,
703 ", invalid note alignment 0x%lx",
704 (unsigned long)align) == -1)
705 return -1;
706 align = 4;
707 }
708 /*
709 * This is a PT_NOTE section; loop through all the notes
710 * in the section.
711 */
712 if (lseek(fd, (off_t) ph_offset, SEEK_SET)
713 == (off_t)-1) {
714 file_badseek(ms);
715 return -1;
716 }
717 bufsize = read(fd, nbuf, sizeof(nbuf));
718 if (bufsize == -1) {
719 file_badread(ms);
720 return -1;
721 }
722 offset = 0;
723 for (;;) {
724 if (offset >= (size_t)bufsize)
725 break;
726 offset = donote(ms, nbuf, offset,
727 (size_t)bufsize, class, swap, align);
728 if (offset == 0)
729 break;
730 }
731 if (lseek(fd, savedoffset + offset, SEEK_SET)
732 == (off_t)-1) {
733 file_badseek(ms);
734 return -1;
735 }
736 break;
737 }
738 }
739 if (file_printf(ms, ", %s linked%s", linking_style, shared_libraries)
740 == -1)
741 return -1;
742 return 0;
743 }
744
745 private int
dophn_noexec(struct magic_set * ms,int class,int swap,int fd,off_t off,int num,size_t size)746 dophn_noexec(struct magic_set *ms, int class, int swap, int fd, off_t off,
747 int num, size_t size)
748 {
749 Elf32_Phdr ph32;
750 Elf64_Phdr ph64;
751 const char *whatisthis = "dynamic link library";
752 unsigned char nbuf[BUFSIZ];
753 int bufsize;
754 size_t offset, align;
755 off_t savedoffset;
756
757 if (size != ph_size) {
758 if (file_printf(ms, ", corrupted program header size") == -1)
759 return -1;
760 return 0;
761 }
762 if (lseek(fd, off, SEEK_SET) == (off_t)-1) {
763 file_badseek(ms);
764 return -1;
765 }
766
767 for ( ; num; num--) {
768 if (read(fd, ph_addr, ph_size) == -1) {
769 file_badread(ms);
770 return -1;
771 }
772 if ((savedoffset = lseek(fd, (off_t)0, SEEK_CUR)) == (off_t)-1) {
773 file_badseek(ms);
774 return -1;
775 }
776
777 switch (ph_type) {
778 case PT_INTERP:
779 whatisthis = "position independent executable";
780 break;
781 case PT_NOTE:
782 if ((align = ph_align) & 0x80000000) {
783 if (file_printf(ms,
784 ", invalid note alignment 0x%lx",
785 (unsigned long)align) == -1)
786 return -1;
787 align = 4;
788 }
789 /*
790 * This is a PT_NOTE section; loop through all the notes
791 * in the section.
792 */
793 if (lseek(fd, (off_t) ph_offset, SEEK_SET)
794 == (off_t)-1) {
795 file_badseek(ms);
796 return -1;
797 }
798 bufsize = read(fd, nbuf, sizeof(nbuf));
799 if (bufsize == -1) {
800 file_badread(ms);
801 return -1;
802 }
803 offset = 0;
804 for (;;) {
805 if (offset >= (size_t)bufsize)
806 break;
807 offset = donote(ms, nbuf, offset,
808 (size_t)bufsize, class, swap, align);
809 if (offset == 0)
810 break;
811 }
812 if (lseek(fd, savedoffset + offset, SEEK_SET)
813 == (off_t)-1) {
814 file_badseek(ms);
815 return -1;
816 }
817 break;
818 }
819 }
820 if (file_printf(ms, ", %s", whatisthis) == -1)
821 return -1;
822 return 0;
823 }
824
825
826 protected int
file_tryelf(struct magic_set * ms,int fd,const unsigned char * buf,size_t nbytes)827 file_tryelf(struct magic_set *ms, int fd, const unsigned char *buf,
828 size_t nbytes)
829 {
830 union {
831 int32_t l;
832 char c[sizeof (int32_t)];
833 } u;
834 int class;
835 int swap;
836
837 /*
838 * If we cannot seek, it must be a pipe, socket or fifo.
839 */
840 if((lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) && (errno == ESPIPE))
841 fd = file_pipe2file(ms, fd, buf, nbytes);
842
843 /*
844 * ELF executables have multiple section headers in arbitrary
845 * file locations and thus file(1) cannot determine it from easily.
846 * Instead we traverse thru all section headers until a symbol table
847 * one is found or else the binary is stripped.
848 */
849 if (buf[EI_MAG0] != ELFMAG0
850 || (buf[EI_MAG1] != ELFMAG1 && buf[EI_MAG1] != OLFMAG1)
851 || buf[EI_MAG2] != ELFMAG2 || buf[EI_MAG3] != ELFMAG3)
852 return 0;
853
854
855 class = buf[4];
856
857 if (class == ELFCLASS32) {
858 Elf32_Ehdr elfhdr;
859 if (nbytes <= sizeof (Elf32_Ehdr))
860 return 0;
861
862
863 u.l = 1;
864 (void) memcpy(&elfhdr, buf, sizeof elfhdr);
865 swap = (u.c[sizeof(int32_t) - 1] + 1) != elfhdr.e_ident[5];
866
867 if (getu16(swap, elfhdr.e_type) == ET_CORE) {
868 #ifdef ELFCORE
869 if (dophn_core(ms, class, swap, fd,
870 (off_t)getu32(swap, elfhdr.e_phoff),
871 getu16(swap, elfhdr.e_phnum),
872 (size_t)getu16(swap, elfhdr.e_phentsize)) == -1)
873 return -1;
874 #else
875 ;
876 #endif
877 } else {
878 if (getu16(swap, elfhdr.e_type) == ET_EXEC) {
879 if (dophn_exec(ms, class, swap,
880 fd, (off_t)getu32(swap, elfhdr.e_phoff),
881 getu16(swap, elfhdr.e_phnum),
882 (size_t)getu16(swap, elfhdr.e_phentsize))
883 == -1)
884 return -1;
885 } else if (getu16(swap, elfhdr.e_type) == ET_DYN) {
886 if (dophn_noexec(ms, class, swap,
887 fd, (off_t)getu32(swap, elfhdr.e_phoff),
888 getu16(swap, elfhdr.e_phnum),
889 (size_t)getu16(swap, elfhdr.e_phentsize))
890 == -1)
891 return -1;
892 }
893 if (doshn(ms, class, swap, fd,
894 (off_t)getu32(swap, elfhdr.e_shoff),
895 getu16(swap, elfhdr.e_shnum),
896 (size_t)getu16(swap, elfhdr.e_shentsize)) == -1)
897 return -1;
898 }
899 return 1;
900 }
901
902 if (class == ELFCLASS64) {
903 Elf64_Ehdr elfhdr;
904 if (nbytes <= sizeof (Elf64_Ehdr))
905 return 0;
906
907
908 u.l = 1;
909 (void) memcpy(&elfhdr, buf, sizeof elfhdr);
910 swap = (u.c[sizeof(int32_t) - 1] + 1) != elfhdr.e_ident[5];
911
912 if (getu16(swap, elfhdr.e_type) == ET_CORE) {
913 #ifdef ELFCORE
914 if (dophn_core(ms, class, swap, fd,
915 #ifdef USE_ARRAY_FOR_64BIT_TYPES
916 (off_t)getu32(swap, elfhdr.e_phoff[1]),
917 #else
918 (off_t)getu64(swap, elfhdr.e_phoff),
919 #endif
920 getu16(swap, elfhdr.e_phnum),
921 (size_t)getu16(swap, elfhdr.e_phentsize)) == -1)
922 return -1;
923 #else
924 ;
925 #endif
926 } else {
927 if (getu16(swap, elfhdr.e_type) == ET_EXEC) {
928 if (dophn_exec(ms, class, swap, fd,
929 #ifdef USE_ARRAY_FOR_64BIT_TYPES
930 (off_t)getu32(swap, elfhdr.e_phoff[1]),
931 #else
932 (off_t)getu64(swap, elfhdr.e_phoff),
933 #endif
934 getu16(swap, elfhdr.e_phnum),
935 (size_t)getu16(swap, elfhdr.e_phentsize))
936 == -1)
937 return -1;
938 }
939 if (doshn(ms, class, swap, fd,
940 #ifdef USE_ARRAY_FOR_64BIT_TYPES
941 (off_t)getu32(swap, elfhdr.e_shoff[1]),
942 #else
943 (off_t)getu64(swap, elfhdr.e_shoff),
944 #endif
945 getu16(swap, elfhdr.e_shnum),
946 (size_t)getu16(swap, elfhdr.e_shentsize)) == -1)
947 return -1;
948 }
949 return 1;
950 }
951 return 0;
952 }
953 #endif
954