xref: /netbsd-src/external/bsd/file/dist/src/readelf.c (revision e89934bbf778a6d6d6894877c4da59d0c7835b0f)
1 /*	$NetBSD: readelf.c,v 1.14 2017/02/11 18:17:29 christos Exp $	*/
2 
3 /*
4  * Copyright (c) Christos Zoulas 2003.
5  * All Rights Reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice immediately at the beginning of the file, without modification,
12  *    this list of conditions, and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 #include "file.h"
30 
31 #ifndef lint
32 #if 0
33 FILE_RCSID("@(#)$File: readelf.c,v 1.130 2017/01/29 19:34:24 christos Exp $")
34 #else
35 __RCSID("$NetBSD: readelf.c,v 1.14 2017/02/11 18:17:29 christos Exp $");
36 #endif
37 #endif
38 
39 #ifdef BUILTIN_ELF
40 #include <string.h>
41 #include <ctype.h>
42 #include <stdlib.h>
43 #ifdef HAVE_UNISTD_H
44 #include <unistd.h>
45 #endif
46 
47 #include "readelf.h"
48 #include "magic.h"
49 
50 #ifdef	ELFCORE
51 private int dophn_core(struct magic_set *, int, int, int, off_t, int, size_t,
52     off_t, int *, uint16_t *);
53 #endif
54 private int dophn_exec(struct magic_set *, int, int, int, off_t, int, size_t,
55     off_t, int, int *, uint16_t *);
56 private int doshn(struct magic_set *, int, int, int, off_t, int, size_t,
57     off_t, int, int, int *, uint16_t *);
58 private size_t donote(struct magic_set *, void *, size_t, size_t, int,
59     int, size_t, int *, uint16_t *, int, off_t, int, off_t);
60 
61 #define	ELF_ALIGN(a)	((((a) + align - 1) / align) * align)
62 
63 #define isquote(c) (strchr("'\"`", (c)) != NULL)
64 
65 private uint16_t getu16(int, uint16_t);
66 private uint32_t getu32(int, uint32_t);
67 private uint64_t getu64(int, uint64_t);
68 
69 #define MAX_PHNUM	128
70 #define	MAX_SHNUM	32768
71 #define SIZE_UNKNOWN	((off_t)-1)
72 
73 private int
74 toomany(struct magic_set *ms, const char *name, uint16_t num)
75 {
76 	if (file_printf(ms, ", too many %s (%u)", name, num
77 	    ) == -1)
78 		return -1;
79 	return 0;
80 }
81 
82 private uint16_t
83 getu16(int swap, uint16_t value)
84 {
85 	union {
86 		uint16_t ui;
87 		char c[2];
88 	} retval, tmpval;
89 
90 	if (swap) {
91 		tmpval.ui = value;
92 
93 		retval.c[0] = tmpval.c[1];
94 		retval.c[1] = tmpval.c[0];
95 
96 		return retval.ui;
97 	} else
98 		return value;
99 }
100 
101 private uint32_t
102 getu32(int swap, uint32_t value)
103 {
104 	union {
105 		uint32_t ui;
106 		char c[4];
107 	} retval, tmpval;
108 
109 	if (swap) {
110 		tmpval.ui = value;
111 
112 		retval.c[0] = tmpval.c[3];
113 		retval.c[1] = tmpval.c[2];
114 		retval.c[2] = tmpval.c[1];
115 		retval.c[3] = tmpval.c[0];
116 
117 		return retval.ui;
118 	} else
119 		return value;
120 }
121 
122 private uint64_t
123 getu64(int swap, uint64_t value)
124 {
125 	union {
126 		uint64_t ui;
127 		char c[8];
128 	} retval, tmpval;
129 
130 	if (swap) {
131 		tmpval.ui = value;
132 
133 		retval.c[0] = tmpval.c[7];
134 		retval.c[1] = tmpval.c[6];
135 		retval.c[2] = tmpval.c[5];
136 		retval.c[3] = tmpval.c[4];
137 		retval.c[4] = tmpval.c[3];
138 		retval.c[5] = tmpval.c[2];
139 		retval.c[6] = tmpval.c[1];
140 		retval.c[7] = tmpval.c[0];
141 
142 		return retval.ui;
143 	} else
144 		return value;
145 }
146 
147 #define elf_getu16(swap, value) getu16(swap, value)
148 #define elf_getu32(swap, value) getu32(swap, value)
149 #define elf_getu64(swap, value) getu64(swap, value)
150 
151 #define xsh_addr	(clazz == ELFCLASS32			\
152 			 ? (void *)&sh32			\
153 			 : (void *)&sh64)
154 #define xsh_sizeof	(clazz == ELFCLASS32			\
155 			 ? sizeof(sh32)				\
156 			 : sizeof(sh64))
157 #define xsh_size	(size_t)(clazz == ELFCLASS32		\
158 			 ? elf_getu32(swap, sh32.sh_size)	\
159 			 : elf_getu64(swap, sh64.sh_size))
160 #define xsh_offset	(off_t)(clazz == ELFCLASS32		\
161 			 ? elf_getu32(swap, sh32.sh_offset)	\
162 			 : elf_getu64(swap, sh64.sh_offset))
163 #define xsh_type	(clazz == ELFCLASS32			\
164 			 ? elf_getu32(swap, sh32.sh_type)	\
165 			 : elf_getu32(swap, sh64.sh_type))
166 #define xsh_name    	(clazz == ELFCLASS32			\
167 			 ? elf_getu32(swap, sh32.sh_name)	\
168 			 : elf_getu32(swap, sh64.sh_name))
169 #define xph_addr	(clazz == ELFCLASS32			\
170 			 ? (void *) &ph32			\
171 			 : (void *) &ph64)
172 #define xph_sizeof	(clazz == ELFCLASS32			\
173 			 ? sizeof(ph32)				\
174 			 : sizeof(ph64))
175 #define xph_type	(clazz == ELFCLASS32			\
176 			 ? elf_getu32(swap, ph32.p_type)	\
177 			 : elf_getu32(swap, ph64.p_type))
178 #define xph_offset	(off_t)(clazz == ELFCLASS32		\
179 			 ? elf_getu32(swap, ph32.p_offset)	\
180 			 : elf_getu64(swap, ph64.p_offset))
181 #define xph_align	(size_t)((clazz == ELFCLASS32		\
182 			 ? (off_t) (ph32.p_align ? 		\
183 			    elf_getu32(swap, ph32.p_align) : 4) \
184 			 : (off_t) (ph64.p_align ?		\
185 			    elf_getu64(swap, ph64.p_align) : 4)))
186 #define xph_vaddr	(size_t)((clazz == ELFCLASS32		\
187 			 ? (off_t) (ph32.p_vaddr ? 		\
188 			    elf_getu32(swap, ph32.p_vaddr) : 4) \
189 			 : (off_t) (ph64.p_vaddr ?		\
190 			    elf_getu64(swap, ph64.p_vaddr) : 4)))
191 #define xph_filesz	(size_t)((clazz == ELFCLASS32		\
192 			 ? elf_getu32(swap, ph32.p_filesz)	\
193 			 : elf_getu64(swap, ph64.p_filesz)))
194 #define xnh_addr	(clazz == ELFCLASS32			\
195 			 ? (void *)&nh32			\
196 			 : (void *)&nh64)
197 #define xph_memsz	(size_t)((clazz == ELFCLASS32		\
198 			 ? elf_getu32(swap, ph32.p_memsz)	\
199 			 : elf_getu64(swap, ph64.p_memsz)))
200 #define xnh_sizeof	(clazz == ELFCLASS32			\
201 			 ? sizeof(nh32)				\
202 			 : sizeof(nh64))
203 #define xnh_type	(clazz == ELFCLASS32			\
204 			 ? elf_getu32(swap, nh32.n_type)	\
205 			 : elf_getu32(swap, nh64.n_type))
206 #define xnh_namesz	(clazz == ELFCLASS32			\
207 			 ? elf_getu32(swap, nh32.n_namesz)	\
208 			 : elf_getu32(swap, nh64.n_namesz))
209 #define xnh_descsz	(clazz == ELFCLASS32			\
210 			 ? elf_getu32(swap, nh32.n_descsz)	\
211 			 : elf_getu32(swap, nh64.n_descsz))
212 #define prpsoffsets(i)	(clazz == ELFCLASS32			\
213 			 ? prpsoffsets32[i]			\
214 			 : prpsoffsets64[i])
215 #define xcap_addr	(clazz == ELFCLASS32			\
216 			 ? (void *)&cap32			\
217 			 : (void *)&cap64)
218 #define xcap_sizeof	(clazz == ELFCLASS32			\
219 			 ? sizeof cap32				\
220 			 : sizeof cap64)
221 #define xcap_tag	(clazz == ELFCLASS32			\
222 			 ? elf_getu32(swap, cap32.c_tag)	\
223 			 : elf_getu64(swap, cap64.c_tag))
224 #define xcap_val	(clazz == ELFCLASS32			\
225 			 ? elf_getu32(swap, cap32.c_un.c_val)	\
226 			 : elf_getu64(swap, cap64.c_un.c_val))
227 #define xauxv_addr	(clazz == ELFCLASS32			\
228 			 ? (void *)&auxv32			\
229 			 : (void *)&auxv64)
230 #define xauxv_sizeof	(clazz == ELFCLASS32			\
231 			 ? sizeof(auxv32)			\
232 			 : sizeof(auxv64))
233 #define xauxv_type	(clazz == ELFCLASS32			\
234 			 ? elf_getu32(swap, auxv32.a_type)	\
235 			 : elf_getu64(swap, auxv64.a_type))
236 #define xauxv_val	(clazz == ELFCLASS32			\
237 			 ? elf_getu32(swap, auxv32.a_v)		\
238 			 : elf_getu64(swap, auxv64.a_v))
239 
240 #ifdef ELFCORE
241 /*
242  * Try larger offsets first to avoid false matches
243  * from earlier data that happen to look like strings.
244  */
245 static const size_t	prpsoffsets32[] = {
246 #ifdef USE_NT_PSINFO
247 	104,		/* SunOS 5.x (command line) */
248 	88,		/* SunOS 5.x (short name) */
249 #endif /* USE_NT_PSINFO */
250 
251 	100,		/* SunOS 5.x (command line) */
252 	84,		/* SunOS 5.x (short name) */
253 
254 	44,		/* Linux (command line) */
255 	28,		/* Linux 2.0.36 (short name) */
256 
257 	8,		/* FreeBSD */
258 };
259 
260 static const size_t	prpsoffsets64[] = {
261 #ifdef USE_NT_PSINFO
262 	152,		/* SunOS 5.x (command line) */
263 	136,		/* SunOS 5.x (short name) */
264 #endif /* USE_NT_PSINFO */
265 
266 	136,		/* SunOS 5.x, 64-bit (command line) */
267 	120,		/* SunOS 5.x, 64-bit (short name) */
268 
269 	56,		/* Linux (command line) */
270 	40,             /* Linux (tested on core from 2.4.x, short name) */
271 
272 	16,		/* FreeBSD, 64-bit */
273 };
274 
275 #define	NOFFSETS32	(sizeof prpsoffsets32 / sizeof prpsoffsets32[0])
276 #define NOFFSETS64	(sizeof prpsoffsets64 / sizeof prpsoffsets64[0])
277 
278 #define NOFFSETS	(clazz == ELFCLASS32 ? NOFFSETS32 : NOFFSETS64)
279 
280 /*
281  * Look through the program headers of an executable image, searching
282  * for a PT_NOTE section of type NT_PRPSINFO, with a name "CORE" or
283  * "FreeBSD"; if one is found, try looking in various places in its
284  * contents for a 16-character string containing only printable
285  * characters - if found, that string should be the name of the program
286  * that dropped core.  Note: right after that 16-character string is,
287  * at least in SunOS 5.x (and possibly other SVR4-flavored systems) and
288  * Linux, a longer string (80 characters, in 5.x, probably other
289  * SVR4-flavored systems, and Linux) containing the start of the
290  * command line for that program.
291  *
292  * SunOS 5.x core files contain two PT_NOTE sections, with the types
293  * NT_PRPSINFO (old) and NT_PSINFO (new).  These structs contain the
294  * same info about the command name and command line, so it probably
295  * isn't worthwhile to look for NT_PSINFO, but the offsets are provided
296  * above (see USE_NT_PSINFO), in case we ever decide to do so.  The
297  * NT_PRPSINFO and NT_PSINFO sections are always in order and adjacent;
298  * the SunOS 5.x file command relies on this (and prefers the latter).
299  *
300  * The signal number probably appears in a section of type NT_PRSTATUS,
301  * but that's also rather OS-dependent, in ways that are harder to
302  * dissect with heuristics, so I'm not bothering with the signal number.
303  * (I suppose the signal number could be of interest in situations where
304  * you don't have the binary of the program that dropped core; if you
305  * *do* have that binary, the debugger will probably tell you what
306  * signal it was.)
307  */
308 
309 #define	OS_STYLE_SVR4		0
310 #define	OS_STYLE_FREEBSD	1
311 #define	OS_STYLE_NETBSD		2
312 
313 private const char os_style_names[][8] = {
314 	"SVR4",
315 	"FreeBSD",
316 	"NetBSD",
317 };
318 
319 #define FLAGS_DID_CORE			0x001
320 #define FLAGS_DID_OS_NOTE		0x002
321 #define FLAGS_DID_BUILD_ID		0x004
322 #define FLAGS_DID_CORE_STYLE		0x008
323 #define FLAGS_DID_NETBSD_PAX		0x010
324 #define FLAGS_DID_NETBSD_MARCH		0x020
325 #define FLAGS_DID_NETBSD_CMODEL		0x040
326 #define FLAGS_DID_NETBSD_UNKNOWN	0x080
327 #define FLAGS_IS_CORE			0x100
328 #define FLAGS_DID_AUXV			0x200
329 
330 private int
331 dophn_core(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
332     int num, size_t size, off_t fsize, int *flags, uint16_t *notecount)
333 {
334 	Elf32_Phdr ph32;
335 	Elf64_Phdr ph64;
336 	size_t offset, len;
337 	unsigned char nbuf[BUFSIZ];
338 	ssize_t bufsize;
339 	off_t ph_off = off;
340 	int ph_num = num;
341 
342 	if (size != xph_sizeof) {
343 		if (file_printf(ms, ", corrupted program header size") == -1)
344 			return -1;
345 		return 0;
346 	}
347 
348 	/*
349 	 * Loop through all the program headers.
350 	 */
351 	for ( ; num; num--) {
352 		if (pread(fd, xph_addr, xph_sizeof, off) < (ssize_t)xph_sizeof) {
353 			file_badread(ms);
354 			return -1;
355 		}
356 		off += size;
357 
358 		if (fsize != SIZE_UNKNOWN && xph_offset > fsize) {
359 			/* Perhaps warn here */
360 			continue;
361 		}
362 
363 		if (xph_type != PT_NOTE)
364 			continue;
365 
366 		/*
367 		 * This is a PT_NOTE section; loop through all the notes
368 		 * in the section.
369 		 */
370 		len = xph_filesz < sizeof(nbuf) ? xph_filesz : sizeof(nbuf);
371 		if ((bufsize = pread(fd, nbuf, len, xph_offset)) == -1) {
372 			file_badread(ms);
373 			return -1;
374 		}
375 		offset = 0;
376 		for (;;) {
377 			if (offset >= (size_t)bufsize)
378 				break;
379 			offset = donote(ms, nbuf, offset, (size_t)bufsize,
380 			    clazz, swap, 4, flags, notecount, fd, ph_off,
381 			    ph_num, fsize);
382 			if (offset == 0)
383 				break;
384 
385 		}
386 	}
387 	return 0;
388 }
389 #endif
390 
391 static void
392 do_note_netbsd_version(struct magic_set *ms, int swap, void *v)
393 {
394 	uint32_t desc;
395 	(void)memcpy(&desc, v, sizeof(desc));
396 	desc = elf_getu32(swap, desc);
397 
398 	if (file_printf(ms, ", for NetBSD") == -1)
399 		return;
400 	/*
401 	 * The version number used to be stuck as 199905, and was thus
402 	 * basically content-free.  Newer versions of NetBSD have fixed
403 	 * this and now use the encoding of __NetBSD_Version__:
404 	 *
405 	 *	MMmmrrpp00
406 	 *
407 	 * M = major version
408 	 * m = minor version
409 	 * r = release ["",A-Z,Z[A-Z] but numeric]
410 	 * p = patchlevel
411 	 */
412 	if (desc > 100000000U) {
413 		uint32_t ver_patch = (desc / 100) % 100;
414 		uint32_t ver_rel = (desc / 10000) % 100;
415 		uint32_t ver_min = (desc / 1000000) % 100;
416 		uint32_t ver_maj = desc / 100000000;
417 
418 		if (file_printf(ms, " %u.%u", ver_maj, ver_min) == -1)
419 			return;
420 		if (ver_rel == 0 && ver_patch != 0) {
421 			if (file_printf(ms, ".%u", ver_patch) == -1)
422 				return;
423 		} else if (ver_rel != 0) {
424 			while (ver_rel > 26) {
425 				if (file_printf(ms, "Z") == -1)
426 					return;
427 				ver_rel -= 26;
428 			}
429 			if (file_printf(ms, "%c", 'A' + ver_rel - 1)
430 			    == -1)
431 				return;
432 		}
433 	}
434 }
435 
436 static void
437 do_note_freebsd_version(struct magic_set *ms, int swap, void *v)
438 {
439 	uint32_t desc;
440 
441 	(void)memcpy(&desc, v, sizeof(desc));
442 	desc = elf_getu32(swap, desc);
443 	if (file_printf(ms, ", for FreeBSD") == -1)
444 		return;
445 
446 	/*
447 	 * Contents is __FreeBSD_version, whose relation to OS
448 	 * versions is defined by a huge table in the Porter's
449 	 * Handbook.  This is the general scheme:
450 	 *
451 	 * Releases:
452 	 * 	Mmp000 (before 4.10)
453 	 * 	Mmi0p0 (before 5.0)
454 	 * 	Mmm0p0
455 	 *
456 	 * Development branches:
457 	 * 	Mmpxxx (before 4.6)
458 	 * 	Mmp1xx (before 4.10)
459 	 * 	Mmi1xx (before 5.0)
460 	 * 	M000xx (pre-M.0)
461 	 * 	Mmm1xx
462 	 *
463 	 * M = major version
464 	 * m = minor version
465 	 * i = minor version increment (491000 -> 4.10)
466 	 * p = patchlevel
467 	 * x = revision
468 	 *
469 	 * The first release of FreeBSD to use ELF by default
470 	 * was version 3.0.
471 	 */
472 	if (desc == 460002) {
473 		if (file_printf(ms, " 4.6.2") == -1)
474 			return;
475 	} else if (desc < 460100) {
476 		if (file_printf(ms, " %d.%d", desc / 100000,
477 		    desc / 10000 % 10) == -1)
478 			return;
479 		if (desc / 1000 % 10 > 0)
480 			if (file_printf(ms, ".%d", desc / 1000 % 10) == -1)
481 				return;
482 		if ((desc % 1000 > 0) || (desc % 100000 == 0))
483 			if (file_printf(ms, " (%d)", desc) == -1)
484 				return;
485 	} else if (desc < 500000) {
486 		if (file_printf(ms, " %d.%d", desc / 100000,
487 		    desc / 10000 % 10 + desc / 1000 % 10) == -1)
488 			return;
489 		if (desc / 100 % 10 > 0) {
490 			if (file_printf(ms, " (%d)", desc) == -1)
491 				return;
492 		} else if (desc / 10 % 10 > 0) {
493 			if (file_printf(ms, ".%d", desc / 10 % 10) == -1)
494 				return;
495 		}
496 	} else {
497 		if (file_printf(ms, " %d.%d", desc / 100000,
498 		    desc / 1000 % 100) == -1)
499 			return;
500 		if ((desc / 100 % 10 > 0) ||
501 		    (desc % 100000 / 100 == 0)) {
502 			if (file_printf(ms, " (%d)", desc) == -1)
503 				return;
504 		} else if (desc / 10 % 10 > 0) {
505 			if (file_printf(ms, ".%d", desc / 10 % 10) == -1)
506 				return;
507 		}
508 	}
509 }
510 
511 private int
512 /*ARGSUSED*/
513 do_bid_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type,
514     int swap __attribute__((__unused__)), uint32_t namesz, uint32_t descsz,
515     size_t noff, size_t doff, int *flags)
516 {
517 	if (namesz == 4 && strcmp((char *)&nbuf[noff], "GNU") == 0 &&
518 	    type == NT_GNU_BUILD_ID && (descsz >= 4 || descsz <= 20)) {
519 		uint8_t desc[20];
520 		const char *btype;
521 		uint32_t i;
522 		*flags |= FLAGS_DID_BUILD_ID;
523 		switch (descsz) {
524 		case 8:
525 		    btype = "xxHash";
526 		    break;
527 		case 16:
528 		    btype = "md5/uuid";
529 		    break;
530 		case 20:
531 		    btype = "sha1";
532 		    break;
533 		default:
534 		    btype = "unknown";
535 		    break;
536 		}
537 		if (file_printf(ms, ", BuildID[%s]=", btype) == -1)
538 			return 1;
539 		(void)memcpy(desc, &nbuf[doff], descsz);
540 		for (i = 0; i < descsz; i++)
541 		    if (file_printf(ms, "%02x", desc[i]) == -1)
542 			return 1;
543 		return 1;
544 	}
545 	return 0;
546 }
547 
548 private int
549 do_os_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type,
550     int swap, uint32_t namesz, uint32_t descsz,
551     size_t noff, size_t doff, int *flags)
552 {
553 	if (namesz == 5 && strcmp((char *)&nbuf[noff], "SuSE") == 0 &&
554 	    type == NT_GNU_VERSION && descsz == 2) {
555 	    *flags |= FLAGS_DID_OS_NOTE;
556 	    file_printf(ms, ", for SuSE %d.%d", nbuf[doff], nbuf[doff + 1]);
557 	    return 1;
558 	}
559 
560 	if (namesz == 4 && strcmp((char *)&nbuf[noff], "GNU") == 0 &&
561 	    type == NT_GNU_VERSION && descsz == 16) {
562 		uint32_t desc[4];
563 		(void)memcpy(desc, &nbuf[doff], sizeof(desc));
564 
565 		*flags |= FLAGS_DID_OS_NOTE;
566 		if (file_printf(ms, ", for GNU/") == -1)
567 			return 1;
568 		switch (elf_getu32(swap, desc[0])) {
569 		case GNU_OS_LINUX:
570 			if (file_printf(ms, "Linux") == -1)
571 				return 1;
572 			break;
573 		case GNU_OS_HURD:
574 			if (file_printf(ms, "Hurd") == -1)
575 				return 1;
576 			break;
577 		case GNU_OS_SOLARIS:
578 			if (file_printf(ms, "Solaris") == -1)
579 				return 1;
580 			break;
581 		case GNU_OS_KFREEBSD:
582 			if (file_printf(ms, "kFreeBSD") == -1)
583 				return 1;
584 			break;
585 		case GNU_OS_KNETBSD:
586 			if (file_printf(ms, "kNetBSD") == -1)
587 				return 1;
588 			break;
589 		default:
590 			if (file_printf(ms, "<unknown>") == -1)
591 				return 1;
592 		}
593 		if (file_printf(ms, " %d.%d.%d", elf_getu32(swap, desc[1]),
594 		    elf_getu32(swap, desc[2]), elf_getu32(swap, desc[3])) == -1)
595 			return 1;
596 		return 1;
597 	}
598 
599 	if (namesz == 7 && strcmp((char *)&nbuf[noff], "NetBSD") == 0) {
600 	    	if (type == NT_NETBSD_VERSION && descsz == 4) {
601 			*flags |= FLAGS_DID_OS_NOTE;
602 			do_note_netbsd_version(ms, swap, &nbuf[doff]);
603 			return 1;
604 		}
605 	}
606 
607 	if (namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0) {
608 	    	if (type == NT_FREEBSD_VERSION && descsz == 4) {
609 			*flags |= FLAGS_DID_OS_NOTE;
610 			do_note_freebsd_version(ms, swap, &nbuf[doff]);
611 			return 1;
612 		}
613 	}
614 
615 	if (namesz == 8 && strcmp((char *)&nbuf[noff], "OpenBSD") == 0 &&
616 	    type == NT_OPENBSD_VERSION && descsz == 4) {
617 		*flags |= FLAGS_DID_OS_NOTE;
618 		if (file_printf(ms, ", for OpenBSD") == -1)
619 			return 1;
620 		/* Content of note is always 0 */
621 		return 1;
622 	}
623 
624 	if (namesz == 10 && strcmp((char *)&nbuf[noff], "DragonFly") == 0 &&
625 	    type == NT_DRAGONFLY_VERSION && descsz == 4) {
626 		uint32_t desc;
627 		*flags |= FLAGS_DID_OS_NOTE;
628 		if (file_printf(ms, ", for DragonFly") == -1)
629 			return 1;
630 		(void)memcpy(&desc, &nbuf[doff], sizeof(desc));
631 		desc = elf_getu32(swap, desc);
632 		if (file_printf(ms, " %d.%d.%d", desc / 100000,
633 		    desc / 10000 % 10, desc % 10000) == -1)
634 			return 1;
635 		return 1;
636 	}
637 	return 0;
638 }
639 
640 private int
641 do_pax_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type,
642     int swap, uint32_t namesz, uint32_t descsz,
643     size_t noff, size_t doff, int *flags)
644 {
645 	if (namesz == 4 && strcmp((char *)&nbuf[noff], "PaX") == 0 &&
646 	    type == NT_NETBSD_PAX && descsz == 4) {
647 		static const char *pax[] = {
648 		    "+mprotect",
649 		    "-mprotect",
650 		    "+segvguard",
651 		    "-segvguard",
652 		    "+ASLR",
653 		    "-ASLR",
654 		};
655 		uint32_t desc;
656 		size_t i;
657 		int did = 0;
658 
659 		*flags |= FLAGS_DID_NETBSD_PAX;
660 		(void)memcpy(&desc, &nbuf[doff], sizeof(desc));
661 		desc = elf_getu32(swap, desc);
662 
663 		if (desc && file_printf(ms, ", PaX: ") == -1)
664 			return 1;
665 
666 		for (i = 0; i < __arraycount(pax); i++) {
667 			if (((1 << (int)i) & desc) == 0)
668 				continue;
669 			if (file_printf(ms, "%s%s", did++ ? "," : "",
670 			    pax[i]) == -1)
671 				return 1;
672 		}
673 		return 1;
674 	}
675 	return 0;
676 }
677 
678 private int
679 do_core_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type,
680     int swap, uint32_t namesz, uint32_t descsz,
681     size_t noff, size_t doff, int *flags, size_t size, int clazz)
682 {
683 #ifdef ELFCORE
684 	int os_style = -1;
685 	/*
686 	 * Sigh.  The 2.0.36 kernel in Debian 2.1, at
687 	 * least, doesn't correctly implement name
688 	 * sections, in core dumps, as specified by
689 	 * the "Program Linking" section of "UNIX(R) System
690 	 * V Release 4 Programmer's Guide: ANSI C and
691 	 * Programming Support Tools", because my copy
692 	 * clearly says "The first 'namesz' bytes in 'name'
693 	 * contain a *null-terminated* [emphasis mine]
694 	 * character representation of the entry's owner
695 	 * or originator", but the 2.0.36 kernel code
696 	 * doesn't include the terminating null in the
697 	 * name....
698 	 */
699 	if ((namesz == 4 && strncmp((char *)&nbuf[noff], "CORE", 4) == 0) ||
700 	    (namesz == 5 && strcmp((char *)&nbuf[noff], "CORE") == 0)) {
701 		os_style = OS_STYLE_SVR4;
702 	}
703 
704 	if ((namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0)) {
705 		os_style = OS_STYLE_FREEBSD;
706 	}
707 
708 	if ((namesz >= 11 && strncmp((char *)&nbuf[noff], "NetBSD-CORE", 11)
709 	    == 0)) {
710 		os_style = OS_STYLE_NETBSD;
711 	}
712 
713 	if (os_style != -1 && (*flags & FLAGS_DID_CORE_STYLE) == 0) {
714 		if (file_printf(ms, ", %s-style", os_style_names[os_style])
715 		    == -1)
716 			return 1;
717 		*flags |= FLAGS_DID_CORE_STYLE;
718 	}
719 
720 	switch (os_style) {
721 	case OS_STYLE_NETBSD:
722 		if (type == NT_NETBSD_CORE_PROCINFO) {
723 			char sbuf[512];
724 			uint32_t signo;
725 			/*
726 			 * Extract the program name.  It is at
727 			 * offset 0x7c, and is up to 32-bytes,
728 			 * including the terminating NUL.
729 			 */
730 			if (file_printf(ms, ", from '%.31s'",
731 			    file_printable(sbuf, sizeof(sbuf),
732 			    (const char *)&nbuf[doff + 0x7c])) == -1)
733 				return 1;
734 
735 			/*
736 			 * Extract the signal number.  It is at
737 			 * offset 0x08.
738 			 */
739 			(void)memcpy(&signo, &nbuf[doff + 0x08],
740 			    sizeof(signo));
741 			if (file_printf(ms, " (signal %u)",
742 			    elf_getu32(swap, signo)) == -1)
743 				return 1;
744 			*flags |= FLAGS_DID_CORE;
745 			return 1;
746 		}
747 		break;
748 
749 	default:
750 		if (type == NT_PRPSINFO && *flags & FLAGS_IS_CORE) {
751 			size_t i, j;
752 			unsigned char c;
753 			/*
754 			 * Extract the program name.  We assume
755 			 * it to be 16 characters (that's what it
756 			 * is in SunOS 5.x and Linux).
757 			 *
758 			 * Unfortunately, it's at a different offset
759 			 * in various OSes, so try multiple offsets.
760 			 * If the characters aren't all printable,
761 			 * reject it.
762 			 */
763 			for (i = 0; i < NOFFSETS; i++) {
764 				unsigned char *cname, *cp;
765 				size_t reloffset = prpsoffsets(i);
766 				size_t noffset = doff + reloffset;
767 				size_t k;
768 				for (j = 0; j < 16; j++, noffset++,
769 				    reloffset++) {
770 					/*
771 					 * Make sure we're not past
772 					 * the end of the buffer; if
773 					 * we are, just give up.
774 					 */
775 					if (noffset >= size)
776 						goto tryanother;
777 
778 					/*
779 					 * Make sure we're not past
780 					 * the end of the contents;
781 					 * if we are, this obviously
782 					 * isn't the right offset.
783 					 */
784 					if (reloffset >= descsz)
785 						goto tryanother;
786 
787 					c = nbuf[noffset];
788 					if (c == '\0') {
789 						/*
790 						 * A '\0' at the
791 						 * beginning is
792 						 * obviously wrong.
793 						 * Any other '\0'
794 						 * means we're done.
795 						 */
796 						if (j == 0)
797 							goto tryanother;
798 						else
799 							break;
800 					} else {
801 						/*
802 						 * A nonprintable
803 						 * character is also
804 						 * wrong.
805 						 */
806 						if (!isprint(c) || isquote(c))
807 							goto tryanother;
808 					}
809 				}
810 				/*
811 				 * Well, that worked.
812 				 */
813 
814 				/*
815 				 * Try next offsets, in case this match is
816 				 * in the middle of a string.
817 				 */
818 				for (k = i + 1 ; k < NOFFSETS; k++) {
819 					size_t no;
820 					int adjust = 1;
821 					if (prpsoffsets(k) >= prpsoffsets(i))
822 						continue;
823 					for (no = doff + prpsoffsets(k);
824 					     no < doff + prpsoffsets(i); no++)
825 						adjust = adjust
826 						         && isprint(nbuf[no]);
827 					if (adjust)
828 						i = k;
829 				}
830 
831 				cname = (unsigned char *)
832 				    &nbuf[doff + prpsoffsets(i)];
833 				for (cp = cname; *cp && isprint(*cp); cp++)
834 					continue;
835 				/*
836 				 * Linux apparently appends a space at the end
837 				 * of the command line: remove it.
838 				 */
839 				while (cp > cname && isspace(cp[-1]))
840 					cp--;
841 				if (file_printf(ms, ", from '%.*s'",
842 				    (int)(cp - cname), cname) == -1)
843 					return 1;
844 				*flags |= FLAGS_DID_CORE;
845 				return 1;
846 
847 			tryanother:
848 				;
849 			}
850 		}
851 		break;
852 	}
853 #endif
854 	return 0;
855 }
856 
857 private off_t
858 get_offset_from_virtaddr(struct magic_set *ms, int swap, int clazz, int fd,
859     off_t off, int num, off_t fsize, uint64_t virtaddr)
860 {
861 	Elf32_Phdr ph32;
862 	Elf64_Phdr ph64;
863 
864 	/*
865 	 * Loop through all the program headers and find the header with
866 	 * virtual address in which the "virtaddr" belongs to.
867 	 */
868 	for ( ; num; num--) {
869 		if (pread(fd, xph_addr, xph_sizeof, off) < (ssize_t)xph_sizeof) {
870 			file_badread(ms);
871 			return -1;
872 		}
873 		off += xph_sizeof;
874 
875 		if (fsize != SIZE_UNKNOWN && xph_offset > fsize) {
876 			/* Perhaps warn here */
877 			continue;
878 		}
879 
880 		if (virtaddr >= xph_vaddr && virtaddr < xph_vaddr + xph_filesz)
881 			return xph_offset + (virtaddr - xph_vaddr);
882 	}
883 	return 0;
884 }
885 
886 private size_t
887 get_string_on_virtaddr(struct magic_set *ms,
888     int swap, int clazz, int fd, off_t ph_off, int ph_num,
889     off_t fsize, uint64_t virtaddr, char *buf, ssize_t buflen)
890 {
891 	char *bptr;
892 	off_t offset;
893 
894 	if (buflen == 0)
895 		return 0;
896 
897 	offset = get_offset_from_virtaddr(ms, swap, clazz, fd, ph_off, ph_num,
898 	    fsize, virtaddr);
899 	if ((buflen = pread(fd, buf, CAST(size_t, buflen), offset)) <= 0) {
900 		file_badread(ms);
901 		return 0;
902 	}
903 
904 	buf[buflen - 1] = '\0';
905 
906 	/* We expect only printable characters, so return if buffer contains
907 	 * non-printable character before the '\0' or just '\0'. */
908 	for (bptr = buf; *bptr && isprint((unsigned char)*bptr); bptr++)
909 		continue;
910 	if (*bptr != '\0')
911 		return 0;
912 
913 	return bptr - buf;
914 }
915 
916 
917 private int
918 do_auxv_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type,
919     int swap, uint32_t namesz __attribute__((__unused__)),
920     uint32_t descsz __attribute__((__unused__)),
921     size_t noff __attribute__((__unused__)), size_t doff,
922     int *flags, size_t size __attribute__((__unused__)), int clazz,
923     int fd, off_t ph_off, int ph_num, off_t fsize)
924 {
925 #ifdef ELFCORE
926 	Aux32Info auxv32;
927 	Aux64Info auxv64;
928 	size_t elsize = xauxv_sizeof;
929 	const char *tag;
930 	int is_string;
931 	size_t nval;
932 
933 	if (type != NT_AUXV || (*flags & FLAGS_IS_CORE) == 0)
934 		return 0;
935 
936 	*flags |= FLAGS_DID_AUXV;
937 
938 	nval = 0;
939 	for (size_t off = 0; off + elsize <= descsz; off += elsize) {
940 		(void)memcpy(xauxv_addr, &nbuf[doff + off], xauxv_sizeof);
941 		/* Limit processing to 50 vector entries to prevent DoS */
942 		if (nval++ >= 50) {
943 			file_error(ms, 0, "Too many ELF Auxv elements");
944 			return 1;
945 		}
946 
947 		switch(xauxv_type) {
948 		case AT_LINUX_EXECFN:
949 			is_string = 1;
950 			tag = "execfn";
951 			break;
952 		case AT_LINUX_PLATFORM:
953 			is_string = 1;
954 			tag = "platform";
955 			break;
956 		case AT_LINUX_UID:
957 			is_string = 0;
958 			tag = "real uid";
959 			break;
960 		case AT_LINUX_GID:
961 			is_string = 0;
962 			tag = "real gid";
963 			break;
964 		case AT_LINUX_EUID:
965 			is_string = 0;
966 			tag = "effective uid";
967 			break;
968 		case AT_LINUX_EGID:
969 			is_string = 0;
970 			tag = "effective gid";
971 			break;
972 		default:
973 			is_string = 0;
974 			tag = NULL;
975 			break;
976 		}
977 
978 		if (tag == NULL)
979 			continue;
980 
981 		if (is_string) {
982 			char buf[256];
983 			ssize_t buflen;
984 			buflen = get_string_on_virtaddr(ms, swap, clazz, fd,
985 			    ph_off, ph_num, fsize, xauxv_val, buf, sizeof(buf));
986 
987 			if (buflen == 0)
988 				continue;
989 
990 			if (file_printf(ms, ", %s: '%s'", tag, buf) == -1)
991 				return 0;
992 		} else {
993 			if (file_printf(ms, ", %s: %d", tag, (int) xauxv_val)
994 			    == -1)
995 				return 0;
996 		}
997 	}
998 	return 1;
999 #else
1000 	return 0;
1001 #endif
1002 }
1003 
1004 private size_t
1005 donote(struct magic_set *ms, void *vbuf, size_t offset, size_t size,
1006     int clazz, int swap, size_t align, int *flags, uint16_t *notecount,
1007     int fd, off_t ph_off, int ph_num, off_t fsize)
1008 {
1009 	Elf32_Nhdr nh32;
1010 	Elf64_Nhdr nh64;
1011 	size_t noff, doff;
1012 	uint32_t namesz, descsz;
1013 	unsigned char *nbuf = CAST(unsigned char *, vbuf);
1014 
1015 	if (*notecount == 0)
1016 		return 0;
1017 	--*notecount;
1018 
1019 	if (xnh_sizeof + offset > size) {
1020 		/*
1021 		 * We're out of note headers.
1022 		 */
1023 		return xnh_sizeof + offset;
1024 	}
1025 
1026 	(void)memcpy(xnh_addr, &nbuf[offset], xnh_sizeof);
1027 	offset += xnh_sizeof;
1028 
1029 	namesz = xnh_namesz;
1030 	descsz = xnh_descsz;
1031 
1032 	if ((namesz == 0) && (descsz == 0)) {
1033 		/*
1034 		 * We're out of note headers.
1035 		 */
1036 		return (offset >= size) ? offset : size;
1037 	}
1038 
1039 	if (namesz & 0x80000000) {
1040 	    (void)file_printf(ms, ", bad note name size 0x%lx",
1041 		(unsigned long)namesz);
1042 	    return 0;
1043 	}
1044 
1045 	if (descsz & 0x80000000) {
1046 	    (void)file_printf(ms, ", bad note description size 0x%lx",
1047 		(unsigned long)descsz);
1048 	    return 0;
1049 	}
1050 
1051 	noff = offset;
1052 	doff = ELF_ALIGN(offset + namesz);
1053 
1054 	if (offset + namesz > size) {
1055 		/*
1056 		 * We're past the end of the buffer.
1057 		 */
1058 		return doff;
1059 	}
1060 
1061 	offset = ELF_ALIGN(doff + descsz);
1062 	if (doff + descsz > size) {
1063 		/*
1064 		 * We're past the end of the buffer.
1065 		 */
1066 		return (offset >= size) ? offset : size;
1067 	}
1068 
1069 
1070 	if ((*flags & FLAGS_DID_OS_NOTE) == 0) {
1071 		if (do_os_note(ms, nbuf, xnh_type, swap,
1072 		    namesz, descsz, noff, doff, flags))
1073 			return offset;
1074 	}
1075 
1076 	if ((*flags & FLAGS_DID_BUILD_ID) == 0) {
1077 		if (do_bid_note(ms, nbuf, xnh_type, swap,
1078 		    namesz, descsz, noff, doff, flags))
1079 			return offset;
1080 	}
1081 
1082 	if ((*flags & FLAGS_DID_NETBSD_PAX) == 0) {
1083 		if (do_pax_note(ms, nbuf, xnh_type, swap,
1084 		    namesz, descsz, noff, doff, flags))
1085 			return offset;
1086 	}
1087 
1088 	if ((*flags & FLAGS_DID_CORE) == 0) {
1089 		if (do_core_note(ms, nbuf, xnh_type, swap,
1090 		    namesz, descsz, noff, doff, flags, size, clazz))
1091 			return offset;
1092 	}
1093 
1094 	if ((*flags & FLAGS_DID_AUXV) == 0) {
1095 		if (do_auxv_note(ms, nbuf, xnh_type, swap,
1096 			namesz, descsz, noff, doff, flags, size, clazz,
1097 			fd, ph_off, ph_num, fsize))
1098 			return offset;
1099 	}
1100 
1101 	if (namesz == 7 && strcmp((char *)&nbuf[noff], "NetBSD") == 0) {
1102 		if (descsz > 100)
1103 			descsz = 100;
1104 		switch (xnh_type) {
1105 	    	case NT_NETBSD_VERSION:
1106 			return offset;
1107 		case NT_NETBSD_MARCH:
1108 			if (*flags & FLAGS_DID_NETBSD_MARCH)
1109 				return offset;
1110 			*flags |= FLAGS_DID_NETBSD_MARCH;
1111 			if (file_printf(ms, ", compiled for: %.*s",
1112 			    (int)descsz, (const char *)&nbuf[doff]) == -1)
1113 				return offset;
1114 			break;
1115 		case NT_NETBSD_CMODEL:
1116 			if (*flags & FLAGS_DID_NETBSD_CMODEL)
1117 				return offset;
1118 			*flags |= FLAGS_DID_NETBSD_CMODEL;
1119 			if (file_printf(ms, ", compiler model: %.*s",
1120 			    (int)descsz, (const char *)&nbuf[doff]) == -1)
1121 				return offset;
1122 			break;
1123 		default:
1124 			if (*flags & FLAGS_DID_NETBSD_UNKNOWN)
1125 				return offset;
1126 			*flags |= FLAGS_DID_NETBSD_UNKNOWN;
1127 			if (file_printf(ms, ", note=%u", xnh_type) == -1)
1128 				return offset;
1129 			break;
1130 		}
1131 		return offset;
1132 	}
1133 
1134 	return offset;
1135 }
1136 
1137 /* SunOS 5.x hardware capability descriptions */
1138 typedef struct cap_desc {
1139 	uint64_t cd_mask;
1140 	const char *cd_name;
1141 } cap_desc_t;
1142 
1143 static const cap_desc_t cap_desc_sparc[] = {
1144 	{ AV_SPARC_MUL32,		"MUL32" },
1145 	{ AV_SPARC_DIV32,		"DIV32" },
1146 	{ AV_SPARC_FSMULD,		"FSMULD" },
1147 	{ AV_SPARC_V8PLUS,		"V8PLUS" },
1148 	{ AV_SPARC_POPC,		"POPC" },
1149 	{ AV_SPARC_VIS,			"VIS" },
1150 	{ AV_SPARC_VIS2,		"VIS2" },
1151 	{ AV_SPARC_ASI_BLK_INIT,	"ASI_BLK_INIT" },
1152 	{ AV_SPARC_FMAF,		"FMAF" },
1153 	{ AV_SPARC_FJFMAU,		"FJFMAU" },
1154 	{ AV_SPARC_IMA,			"IMA" },
1155 	{ 0, NULL }
1156 };
1157 
1158 static const cap_desc_t cap_desc_386[] = {
1159 	{ AV_386_FPU,			"FPU" },
1160 	{ AV_386_TSC,			"TSC" },
1161 	{ AV_386_CX8,			"CX8" },
1162 	{ AV_386_SEP,			"SEP" },
1163 	{ AV_386_AMD_SYSC,		"AMD_SYSC" },
1164 	{ AV_386_CMOV,			"CMOV" },
1165 	{ AV_386_MMX,			"MMX" },
1166 	{ AV_386_AMD_MMX,		"AMD_MMX" },
1167 	{ AV_386_AMD_3DNow,		"AMD_3DNow" },
1168 	{ AV_386_AMD_3DNowx,		"AMD_3DNowx" },
1169 	{ AV_386_FXSR,			"FXSR" },
1170 	{ AV_386_SSE,			"SSE" },
1171 	{ AV_386_SSE2,			"SSE2" },
1172 	{ AV_386_PAUSE,			"PAUSE" },
1173 	{ AV_386_SSE3,			"SSE3" },
1174 	{ AV_386_MON,			"MON" },
1175 	{ AV_386_CX16,			"CX16" },
1176 	{ AV_386_AHF,			"AHF" },
1177 	{ AV_386_TSCP,			"TSCP" },
1178 	{ AV_386_AMD_SSE4A,		"AMD_SSE4A" },
1179 	{ AV_386_POPCNT,		"POPCNT" },
1180 	{ AV_386_AMD_LZCNT,		"AMD_LZCNT" },
1181 	{ AV_386_SSSE3,			"SSSE3" },
1182 	{ AV_386_SSE4_1,		"SSE4.1" },
1183 	{ AV_386_SSE4_2,		"SSE4.2" },
1184 	{ 0, NULL }
1185 };
1186 
1187 private int
1188 doshn(struct magic_set *ms, int clazz, int swap, int fd, off_t off, int num,
1189     size_t size, off_t fsize, int mach, int strtab, int *flags,
1190     uint16_t *notecount)
1191 {
1192 	Elf32_Shdr sh32;
1193 	Elf64_Shdr sh64;
1194 	int stripped = 1, has_debug_info = 0;
1195 	size_t nbadcap = 0;
1196 	void *nbuf;
1197 	off_t noff, coff, name_off;
1198 	uint64_t cap_hw1 = 0;	/* SunOS 5.x hardware capabilites */
1199 	uint64_t cap_sf1 = 0;	/* SunOS 5.x software capabilites */
1200 	char name[50];
1201 	ssize_t namesize;
1202 
1203 	if (size != xsh_sizeof) {
1204 		if (file_printf(ms, ", corrupted section header size") == -1)
1205 			return -1;
1206 		return 0;
1207 	}
1208 
1209 	/* Read offset of name section to be able to read section names later */
1210 	if (pread(fd, xsh_addr, xsh_sizeof, CAST(off_t, (off + size * strtab)))
1211 	    < (ssize_t)xsh_sizeof) {
1212 		if (file_printf(ms, ", missing section headers") == -1)
1213 			return -1;
1214 		return 0;
1215 	}
1216 	name_off = xsh_offset;
1217 
1218 	for ( ; num; num--) {
1219 		/* Read the name of this section. */
1220 		if ((namesize = pread(fd, name, sizeof(name) - 1, name_off + xsh_name)) == -1) {
1221 			file_badread(ms);
1222 			return -1;
1223 		}
1224 		name[namesize] = '\0';
1225 		if (strcmp(name, ".debug_info") == 0) {
1226 			has_debug_info = 1;
1227 			stripped = 0;
1228 		}
1229 
1230 		if (pread(fd, xsh_addr, xsh_sizeof, off) < (ssize_t)xsh_sizeof) {
1231 			file_badread(ms);
1232 			return -1;
1233 		}
1234 		off += size;
1235 
1236 		/* Things we can determine before we seek */
1237 		switch (xsh_type) {
1238 		case SHT_SYMTAB:
1239 #if 0
1240 		case SHT_DYNSYM:
1241 #endif
1242 			stripped = 0;
1243 			break;
1244 		default:
1245 			if (fsize != SIZE_UNKNOWN && xsh_offset > fsize) {
1246 				/* Perhaps warn here */
1247 				continue;
1248 			}
1249 			break;
1250 		}
1251 
1252 
1253 		/* Things we can determine when we seek */
1254 		switch (xsh_type) {
1255 		case SHT_NOTE:
1256 			if ((uintmax_t)(xsh_size + xsh_offset) >
1257 			    (uintmax_t)fsize) {
1258 				if (file_printf(ms,
1259 				    ", note offset/size 0x%" INTMAX_T_FORMAT
1260 				    "x+0x%" INTMAX_T_FORMAT "x exceeds"
1261 				    " file size 0x%" INTMAX_T_FORMAT "x",
1262 				    (uintmax_t)xsh_offset, (uintmax_t)xsh_size,
1263 				    (uintmax_t)fsize) == -1)
1264 					return -1;
1265 				return 0;
1266 			}
1267 			if ((nbuf = malloc(xsh_size)) == NULL) {
1268 				file_error(ms, errno, "Cannot allocate memory"
1269 				    " for note");
1270 				return -1;
1271 			}
1272 			if (pread(fd, nbuf, xsh_size, xsh_offset) <
1273 			    (ssize_t)xsh_size) {
1274 				file_badread(ms);
1275 				free(nbuf);
1276 				return -1;
1277 			}
1278 
1279 			noff = 0;
1280 			for (;;) {
1281 				if (noff >= (off_t)xsh_size)
1282 					break;
1283 				noff = donote(ms, nbuf, (size_t)noff,
1284 				    xsh_size, clazz, swap, 4, flags, notecount,
1285 				    fd, 0, 0, 0);
1286 				if (noff == 0)
1287 					break;
1288 			}
1289 			free(nbuf);
1290 			break;
1291 		case SHT_SUNW_cap:
1292 			switch (mach) {
1293 			case EM_SPARC:
1294 			case EM_SPARCV9:
1295 			case EM_IA_64:
1296 			case EM_386:
1297 			case EM_AMD64:
1298 				break;
1299 			default:
1300 				goto skip;
1301 			}
1302 
1303 			if (nbadcap > 5)
1304 				break;
1305 			if (lseek(fd, xsh_offset, SEEK_SET) == (off_t)-1) {
1306 				file_badseek(ms);
1307 				return -1;
1308 			}
1309 			coff = 0;
1310 			for (;;) {
1311 				Elf32_Cap cap32;
1312 				Elf64_Cap cap64;
1313 				char cbuf[/*CONSTCOND*/
1314 				    MAX(sizeof cap32, sizeof cap64)];
1315 				if ((coff += xcap_sizeof) > (off_t)xsh_size)
1316 					break;
1317 				if (read(fd, cbuf, (size_t)xcap_sizeof) !=
1318 				    (ssize_t)xcap_sizeof) {
1319 					file_badread(ms);
1320 					return -1;
1321 				}
1322 				if (cbuf[0] == 'A') {
1323 #ifdef notyet
1324 					char *p = cbuf + 1;
1325 					uint32_t len, tag;
1326 					memcpy(&len, p, sizeof(len));
1327 					p += 4;
1328 					len = getu32(swap, len);
1329 					if (memcmp("gnu", p, 3) != 0) {
1330 					    if (file_printf(ms,
1331 						", unknown capability %.3s", p)
1332 						== -1)
1333 						return -1;
1334 					    break;
1335 					}
1336 					p += strlen(p) + 1;
1337 					tag = *p++;
1338 					memcpy(&len, p, sizeof(len));
1339 					p += 4;
1340 					len = getu32(swap, len);
1341 					if (tag != 1) {
1342 					    if (file_printf(ms, ", unknown gnu"
1343 						" capability tag %d", tag)
1344 						== -1)
1345 						return -1;
1346 					    break;
1347 					}
1348 					// gnu attributes
1349 #endif
1350 					break;
1351 				}
1352 				(void)memcpy(xcap_addr, cbuf, xcap_sizeof);
1353 				switch (xcap_tag) {
1354 				case CA_SUNW_NULL:
1355 					break;
1356 				case CA_SUNW_HW_1:
1357 					cap_hw1 |= xcap_val;
1358 					break;
1359 				case CA_SUNW_SF_1:
1360 					cap_sf1 |= xcap_val;
1361 					break;
1362 				default:
1363 					if (file_printf(ms,
1364 					    ", with unknown capability "
1365 					    "0x%" INT64_T_FORMAT "x = 0x%"
1366 					    INT64_T_FORMAT "x",
1367 					    (unsigned long long)xcap_tag,
1368 					    (unsigned long long)xcap_val) == -1)
1369 						return -1;
1370 					if (nbadcap++ > 2)
1371 						coff = xsh_size;
1372 					break;
1373 				}
1374 			}
1375 			/*FALLTHROUGH*/
1376 		skip:
1377 		default:
1378 			break;
1379 		}
1380 	}
1381 
1382 	if (file_printf(ms, ", %sstripped", stripped ? "" : "not ") == -1)
1383 		return -1;
1384 	if (has_debug_info) {
1385 		if (file_printf(ms, ", with debug_info") == -1)
1386 			return -1;
1387 	}
1388 	if (cap_hw1) {
1389 		const cap_desc_t *cdp;
1390 		switch (mach) {
1391 		case EM_SPARC:
1392 		case EM_SPARC32PLUS:
1393 		case EM_SPARCV9:
1394 			cdp = cap_desc_sparc;
1395 			break;
1396 		case EM_386:
1397 		case EM_IA_64:
1398 		case EM_AMD64:
1399 			cdp = cap_desc_386;
1400 			break;
1401 		default:
1402 			cdp = NULL;
1403 			break;
1404 		}
1405 		if (file_printf(ms, ", uses") == -1)
1406 			return -1;
1407 		if (cdp) {
1408 			while (cdp->cd_name) {
1409 				if (cap_hw1 & cdp->cd_mask) {
1410 					if (file_printf(ms,
1411 					    " %s", cdp->cd_name) == -1)
1412 						return -1;
1413 					cap_hw1 &= ~cdp->cd_mask;
1414 				}
1415 				++cdp;
1416 			}
1417 			if (cap_hw1)
1418 				if (file_printf(ms,
1419 				    " unknown hardware capability 0x%"
1420 				    INT64_T_FORMAT "x",
1421 				    (unsigned long long)cap_hw1) == -1)
1422 					return -1;
1423 		} else {
1424 			if (file_printf(ms,
1425 			    " hardware capability 0x%" INT64_T_FORMAT "x",
1426 			    (unsigned long long)cap_hw1) == -1)
1427 				return -1;
1428 		}
1429 	}
1430 	if (cap_sf1) {
1431 		if (cap_sf1 & SF1_SUNW_FPUSED) {
1432 			if (file_printf(ms,
1433 			    (cap_sf1 & SF1_SUNW_FPKNWN)
1434 			    ? ", uses frame pointer"
1435 			    : ", not known to use frame pointer") == -1)
1436 				return -1;
1437 		}
1438 		cap_sf1 &= ~SF1_SUNW_MASK;
1439 		if (cap_sf1)
1440 			if (file_printf(ms,
1441 			    ", with unknown software capability 0x%"
1442 			    INT64_T_FORMAT "x",
1443 			    (unsigned long long)cap_sf1) == -1)
1444 				return -1;
1445 	}
1446 	return 0;
1447 }
1448 
1449 /*
1450  * Look through the program headers of an executable image, searching
1451  * for a PT_INTERP section; if one is found, it's dynamically linked,
1452  * otherwise it's statically linked.
1453  */
1454 private int
1455 dophn_exec(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
1456     int num, size_t size, off_t fsize, int sh_num, int *flags,
1457     uint16_t *notecount)
1458 {
1459 	Elf32_Phdr ph32;
1460 	Elf64_Phdr ph64;
1461 	const char *linking_style = "statically";
1462 	const char *interp = "";
1463 	unsigned char nbuf[BUFSIZ];
1464 	char ibuf[BUFSIZ];
1465 	ssize_t bufsize;
1466 	size_t offset, align, len;
1467 
1468 	if (size != xph_sizeof) {
1469 		if (file_printf(ms, ", corrupted program header size") == -1)
1470 			return -1;
1471 		return 0;
1472 	}
1473 
1474   	for ( ; num; num--) {
1475 		if (pread(fd, xph_addr, xph_sizeof, off) < (ssize_t)xph_sizeof) {
1476 			file_badread(ms);
1477 			return -1;
1478 		}
1479 
1480 		off += size;
1481 		bufsize = 0;
1482 		align = 4;
1483 
1484 		/* Things we can determine before we seek */
1485 		switch (xph_type) {
1486 		case PT_DYNAMIC:
1487 			linking_style = "dynamically";
1488 			break;
1489 		case PT_NOTE:
1490 			if (sh_num)	/* Did this through section headers */
1491 				continue;
1492 			if (((align = xph_align) & 0x80000000UL) != 0 ||
1493 			    align < 4) {
1494 				if (file_printf(ms,
1495 				    ", invalid note alignment 0x%lx",
1496 				    (unsigned long)align) == -1)
1497 					return -1;
1498 				align = 4;
1499 			}
1500 			/*FALLTHROUGH*/
1501 		case PT_INTERP:
1502 			len = xph_filesz < sizeof(nbuf) ? xph_filesz
1503 			    : sizeof(nbuf);
1504 			bufsize = pread(fd, nbuf, len, xph_offset);
1505 			if (bufsize == -1) {
1506 				file_badread(ms);
1507 				return -1;
1508 			}
1509 			break;
1510 		default:
1511 			if (fsize != SIZE_UNKNOWN && xph_offset > fsize) {
1512 				/* Maybe warn here? */
1513 				continue;
1514 			}
1515 			break;
1516 		}
1517 
1518 		/* Things we can determine when we seek */
1519 		switch (xph_type) {
1520 		case PT_INTERP:
1521 			if (bufsize && nbuf[0]) {
1522 				nbuf[bufsize - 1] = '\0';
1523 				interp = (const char *)nbuf;
1524 			} else
1525 				interp = "*empty*";
1526 			break;
1527 		case PT_NOTE:
1528 			/*
1529 			 * This is a PT_NOTE section; loop through all the notes
1530 			 * in the section.
1531 			 */
1532 			offset = 0;
1533 			for (;;) {
1534 				if (offset >= (size_t)bufsize)
1535 					break;
1536 				offset = donote(ms, nbuf, offset,
1537 				    (size_t)bufsize, clazz, swap, align,
1538 				    flags, notecount, fd, 0, 0, 0);
1539 				if (offset == 0)
1540 					break;
1541 			}
1542 			break;
1543 		default:
1544 			break;
1545 		}
1546 	}
1547 	if (file_printf(ms, ", %s linked", linking_style)
1548 	    == -1)
1549 		return -1;
1550 	if (interp[0])
1551 		if (file_printf(ms, ", interpreter %s",
1552 		    file_printable(ibuf, sizeof(ibuf), interp)) == -1)
1553 			return -1;
1554 	return 0;
1555 }
1556 
1557 
1558 protected int
1559 file_tryelf(struct magic_set *ms, int fd, const unsigned char *buf,
1560     size_t nbytes)
1561 {
1562 	union {
1563 		int32_t l;
1564 		char c[sizeof (int32_t)];
1565 	} u;
1566 	int clazz;
1567 	int swap;
1568 	struct stat st;
1569 	off_t fsize;
1570 	int flags = 0;
1571 	Elf32_Ehdr elf32hdr;
1572 	Elf64_Ehdr elf64hdr;
1573 	uint16_t type, phnum, shnum, notecount;
1574 
1575 	if (ms->flags & (MAGIC_MIME|MAGIC_APPLE|MAGIC_EXTENSION))
1576 		return 0;
1577 	/*
1578 	 * ELF executables have multiple section headers in arbitrary
1579 	 * file locations and thus file(1) cannot determine it from easily.
1580 	 * Instead we traverse thru all section headers until a symbol table
1581 	 * one is found or else the binary is stripped.
1582 	 * Return immediately if it's not ELF (so we avoid pipe2file unless needed).
1583 	 */
1584 	if (buf[EI_MAG0] != ELFMAG0
1585 	    || (buf[EI_MAG1] != ELFMAG1 && buf[EI_MAG1] != OLFMAG1)
1586 	    || buf[EI_MAG2] != ELFMAG2 || buf[EI_MAG3] != ELFMAG3)
1587 		return 0;
1588 
1589 	/*
1590 	 * If we cannot seek, it must be a pipe, socket or fifo.
1591 	 */
1592 	if((lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) && (errno == ESPIPE))
1593 		fd = file_pipe2file(ms, fd, buf, nbytes);
1594 
1595 	if (fstat(fd, &st) == -1) {
1596   		file_badread(ms);
1597 		return -1;
1598 	}
1599 	if (S_ISREG(st.st_mode) || st.st_size != 0)
1600 		fsize = st.st_size;
1601 	else
1602 		fsize = SIZE_UNKNOWN;
1603 
1604 	clazz = buf[EI_CLASS];
1605 
1606 	switch (clazz) {
1607 	case ELFCLASS32:
1608 #undef elf_getu
1609 #define elf_getu(a, b)	elf_getu32(a, b)
1610 #undef elfhdr
1611 #define elfhdr elf32hdr
1612 #include "elfclass.h"
1613 	case ELFCLASS64:
1614 #undef elf_getu
1615 #define elf_getu(a, b)	elf_getu64(a, b)
1616 #undef elfhdr
1617 #define elfhdr elf64hdr
1618 #include "elfclass.h"
1619 	default:
1620 	    if (file_printf(ms, ", unknown class %d", clazz) == -1)
1621 		    return -1;
1622 	    break;
1623 	}
1624 	return 0;
1625 }
1626 #endif
1627