xref: /netbsd-src/external/bsd/file/dist/src/readelf.c (revision 796c32c94f6e154afc9de0f63da35c91bb739b45)
1 /*	$NetBSD: readelf.c,v 1.16 2017/09/08 13:40:25 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.138 2017/08/27 07:55:02 christos Exp $")
34 #else
35 __RCSID("$NetBSD: readelf.c,v 1.16 2017/09/08 13:40:25 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_CORE_STYLE		0x003
320 
321 #define FLAGS_DID_CORE			0x004
322 #define FLAGS_DID_OS_NOTE		0x008
323 #define FLAGS_DID_BUILD_ID		0x010
324 #define FLAGS_DID_CORE_STYLE		0x020
325 #define FLAGS_DID_NETBSD_PAX		0x040
326 #define FLAGS_DID_NETBSD_MARCH		0x080
327 #define FLAGS_DID_NETBSD_CMODEL		0x100
328 #define FLAGS_DID_NETBSD_UNKNOWN	0x200
329 #define FLAGS_IS_CORE			0x400
330 #define FLAGS_DID_AUXV			0x800
331 
332 private int
333 dophn_core(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
334     int num, size_t size, off_t fsize, int *flags, uint16_t *notecount)
335 {
336 	Elf32_Phdr ph32;
337 	Elf64_Phdr ph64;
338 	size_t offset, len;
339 	unsigned char nbuf[BUFSIZ];
340 	ssize_t bufsize;
341 	off_t ph_off = off;
342 	int ph_num = num;
343 
344 	if (size != xph_sizeof) {
345 		if (file_printf(ms, ", corrupted program header size") == -1)
346 			return -1;
347 		return 0;
348 	}
349 
350 	/*
351 	 * Loop through all the program headers.
352 	 */
353 	for ( ; num; num--) {
354 		if (pread(fd, xph_addr, xph_sizeof, off) < (ssize_t)xph_sizeof) {
355 			file_badread(ms);
356 			return -1;
357 		}
358 		off += size;
359 
360 		if (fsize != SIZE_UNKNOWN && xph_offset > fsize) {
361 			/* Perhaps warn here */
362 			continue;
363 		}
364 
365 		if (xph_type != PT_NOTE)
366 			continue;
367 
368 		/*
369 		 * This is a PT_NOTE section; loop through all the notes
370 		 * in the section.
371 		 */
372 		len = xph_filesz < sizeof(nbuf) ? xph_filesz : sizeof(nbuf);
373 		if ((bufsize = pread(fd, nbuf, len, xph_offset)) == -1) {
374 			file_badread(ms);
375 			return -1;
376 		}
377 		offset = 0;
378 		for (;;) {
379 			if (offset >= (size_t)bufsize)
380 				break;
381 			offset = donote(ms, nbuf, offset, (size_t)bufsize,
382 			    clazz, swap, 4, flags, notecount, fd, ph_off,
383 			    ph_num, fsize);
384 			if (offset == 0)
385 				break;
386 
387 		}
388 	}
389 	return 0;
390 }
391 #endif
392 
393 static void
394 do_note_netbsd_version(struct magic_set *ms, int swap, void *v)
395 {
396 	uint32_t desc;
397 	(void)memcpy(&desc, v, sizeof(desc));
398 	desc = elf_getu32(swap, desc);
399 
400 	if (file_printf(ms, ", for NetBSD") == -1)
401 		return;
402 	/*
403 	 * The version number used to be stuck as 199905, and was thus
404 	 * basically content-free.  Newer versions of NetBSD have fixed
405 	 * this and now use the encoding of __NetBSD_Version__:
406 	 *
407 	 *	MMmmrrpp00
408 	 *
409 	 * M = major version
410 	 * m = minor version
411 	 * r = release ["",A-Z,Z[A-Z] but numeric]
412 	 * p = patchlevel
413 	 */
414 	if (desc > 100000000U) {
415 		uint32_t ver_patch = (desc / 100) % 100;
416 		uint32_t ver_rel = (desc / 10000) % 100;
417 		uint32_t ver_min = (desc / 1000000) % 100;
418 		uint32_t ver_maj = desc / 100000000;
419 
420 		if (file_printf(ms, " %u.%u", ver_maj, ver_min) == -1)
421 			return;
422 		if (ver_rel == 0 && ver_patch != 0) {
423 			if (file_printf(ms, ".%u", ver_patch) == -1)
424 				return;
425 		} else if (ver_rel != 0) {
426 			while (ver_rel > 26) {
427 				if (file_printf(ms, "Z") == -1)
428 					return;
429 				ver_rel -= 26;
430 			}
431 			if (file_printf(ms, "%c", 'A' + ver_rel - 1)
432 			    == -1)
433 				return;
434 		}
435 	}
436 }
437 
438 static void
439 do_note_freebsd_version(struct magic_set *ms, int swap, void *v)
440 {
441 	uint32_t desc;
442 
443 	(void)memcpy(&desc, v, sizeof(desc));
444 	desc = elf_getu32(swap, desc);
445 	if (file_printf(ms, ", for FreeBSD") == -1)
446 		return;
447 
448 	/*
449 	 * Contents is __FreeBSD_version, whose relation to OS
450 	 * versions is defined by a huge table in the Porter's
451 	 * Handbook.  This is the general scheme:
452 	 *
453 	 * Releases:
454 	 * 	Mmp000 (before 4.10)
455 	 * 	Mmi0p0 (before 5.0)
456 	 * 	Mmm0p0
457 	 *
458 	 * Development branches:
459 	 * 	Mmpxxx (before 4.6)
460 	 * 	Mmp1xx (before 4.10)
461 	 * 	Mmi1xx (before 5.0)
462 	 * 	M000xx (pre-M.0)
463 	 * 	Mmm1xx
464 	 *
465 	 * M = major version
466 	 * m = minor version
467 	 * i = minor version increment (491000 -> 4.10)
468 	 * p = patchlevel
469 	 * x = revision
470 	 *
471 	 * The first release of FreeBSD to use ELF by default
472 	 * was version 3.0.
473 	 */
474 	if (desc == 460002) {
475 		if (file_printf(ms, " 4.6.2") == -1)
476 			return;
477 	} else if (desc < 460100) {
478 		if (file_printf(ms, " %d.%d", desc / 100000,
479 		    desc / 10000 % 10) == -1)
480 			return;
481 		if (desc / 1000 % 10 > 0)
482 			if (file_printf(ms, ".%d", desc / 1000 % 10) == -1)
483 				return;
484 		if ((desc % 1000 > 0) || (desc % 100000 == 0))
485 			if (file_printf(ms, " (%d)", desc) == -1)
486 				return;
487 	} else if (desc < 500000) {
488 		if (file_printf(ms, " %d.%d", desc / 100000,
489 		    desc / 10000 % 10 + desc / 1000 % 10) == -1)
490 			return;
491 		if (desc / 100 % 10 > 0) {
492 			if (file_printf(ms, " (%d)", desc) == -1)
493 				return;
494 		} else if (desc / 10 % 10 > 0) {
495 			if (file_printf(ms, ".%d", desc / 10 % 10) == -1)
496 				return;
497 		}
498 	} else {
499 		if (file_printf(ms, " %d.%d", desc / 100000,
500 		    desc / 1000 % 100) == -1)
501 			return;
502 		if ((desc / 100 % 10 > 0) ||
503 		    (desc % 100000 / 100 == 0)) {
504 			if (file_printf(ms, " (%d)", desc) == -1)
505 				return;
506 		} else if (desc / 10 % 10 > 0) {
507 			if (file_printf(ms, ".%d", desc / 10 % 10) == -1)
508 				return;
509 		}
510 	}
511 }
512 
513 private int
514 /*ARGSUSED*/
515 do_bid_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type,
516     int swap __attribute__((__unused__)), uint32_t namesz, uint32_t descsz,
517     size_t noff, size_t doff, int *flags)
518 {
519 	if (namesz == 4 && strcmp((char *)&nbuf[noff], "GNU") == 0 &&
520 	    type == NT_GNU_BUILD_ID && (descsz >= 4 && descsz <= 20)) {
521 		uint8_t desc[20];
522 		const char *btype;
523 		uint32_t i;
524 		*flags |= FLAGS_DID_BUILD_ID;
525 		switch (descsz) {
526 		case 8:
527 		    btype = "xxHash";
528 		    break;
529 		case 16:
530 		    btype = "md5/uuid";
531 		    break;
532 		case 20:
533 		    btype = "sha1";
534 		    break;
535 		default:
536 		    btype = "unknown";
537 		    break;
538 		}
539 		if (file_printf(ms, ", BuildID[%s]=", btype) == -1)
540 			return 1;
541 		(void)memcpy(desc, &nbuf[doff], descsz);
542 		for (i = 0; i < descsz; i++)
543 		    if (file_printf(ms, "%02x", desc[i]) == -1)
544 			return 1;
545 		return 1;
546 	}
547 	return 0;
548 }
549 
550 private int
551 do_os_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type,
552     int swap, uint32_t namesz, uint32_t descsz,
553     size_t noff, size_t doff, int *flags)
554 {
555 	if (namesz == 5 && strcmp((char *)&nbuf[noff], "SuSE") == 0 &&
556 	    type == NT_GNU_VERSION && descsz == 2) {
557 	    *flags |= FLAGS_DID_OS_NOTE;
558 	    file_printf(ms, ", for SuSE %d.%d", nbuf[doff], nbuf[doff + 1]);
559 	    return 1;
560 	}
561 
562 	if (namesz == 4 && strcmp((char *)&nbuf[noff], "GNU") == 0 &&
563 	    type == NT_GNU_VERSION && descsz == 16) {
564 		uint32_t desc[4];
565 		(void)memcpy(desc, &nbuf[doff], sizeof(desc));
566 
567 		*flags |= FLAGS_DID_OS_NOTE;
568 		if (file_printf(ms, ", for GNU/") == -1)
569 			return 1;
570 		switch (elf_getu32(swap, desc[0])) {
571 		case GNU_OS_LINUX:
572 			if (file_printf(ms, "Linux") == -1)
573 				return 1;
574 			break;
575 		case GNU_OS_HURD:
576 			if (file_printf(ms, "Hurd") == -1)
577 				return 1;
578 			break;
579 		case GNU_OS_SOLARIS:
580 			if (file_printf(ms, "Solaris") == -1)
581 				return 1;
582 			break;
583 		case GNU_OS_KFREEBSD:
584 			if (file_printf(ms, "kFreeBSD") == -1)
585 				return 1;
586 			break;
587 		case GNU_OS_KNETBSD:
588 			if (file_printf(ms, "kNetBSD") == -1)
589 				return 1;
590 			break;
591 		default:
592 			if (file_printf(ms, "<unknown>") == -1)
593 				return 1;
594 		}
595 		if (file_printf(ms, " %d.%d.%d", elf_getu32(swap, desc[1]),
596 		    elf_getu32(swap, desc[2]), elf_getu32(swap, desc[3])) == -1)
597 			return 1;
598 		return 1;
599 	}
600 
601 	if (namesz == 7 && strcmp((char *)&nbuf[noff], "NetBSD") == 0) {
602 	    	if (type == NT_NETBSD_VERSION && descsz == 4) {
603 			*flags |= FLAGS_DID_OS_NOTE;
604 			do_note_netbsd_version(ms, swap, &nbuf[doff]);
605 			return 1;
606 		}
607 	}
608 
609 	if (namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0) {
610 	    	if (type == NT_FREEBSD_VERSION && descsz == 4) {
611 			*flags |= FLAGS_DID_OS_NOTE;
612 			do_note_freebsd_version(ms, swap, &nbuf[doff]);
613 			return 1;
614 		}
615 	}
616 
617 	if (namesz == 8 && strcmp((char *)&nbuf[noff], "OpenBSD") == 0 &&
618 	    type == NT_OPENBSD_VERSION && descsz == 4) {
619 		*flags |= FLAGS_DID_OS_NOTE;
620 		if (file_printf(ms, ", for OpenBSD") == -1)
621 			return 1;
622 		/* Content of note is always 0 */
623 		return 1;
624 	}
625 
626 	if (namesz == 10 && strcmp((char *)&nbuf[noff], "DragonFly") == 0 &&
627 	    type == NT_DRAGONFLY_VERSION && descsz == 4) {
628 		uint32_t desc;
629 		*flags |= FLAGS_DID_OS_NOTE;
630 		if (file_printf(ms, ", for DragonFly") == -1)
631 			return 1;
632 		(void)memcpy(&desc, &nbuf[doff], sizeof(desc));
633 		desc = elf_getu32(swap, desc);
634 		if (file_printf(ms, " %d.%d.%d", desc / 100000,
635 		    desc / 10000 % 10, desc % 10000) == -1)
636 			return 1;
637 		return 1;
638 	}
639 	return 0;
640 }
641 
642 private int
643 do_pax_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type,
644     int swap, uint32_t namesz, uint32_t descsz,
645     size_t noff, size_t doff, int *flags)
646 {
647 	if (namesz == 4 && strcmp((char *)&nbuf[noff], "PaX") == 0 &&
648 	    type == NT_NETBSD_PAX && descsz == 4) {
649 		static const char *pax[] = {
650 		    "+mprotect",
651 		    "-mprotect",
652 		    "+segvguard",
653 		    "-segvguard",
654 		    "+ASLR",
655 		    "-ASLR",
656 		};
657 		uint32_t desc;
658 		size_t i;
659 		int did = 0;
660 
661 		*flags |= FLAGS_DID_NETBSD_PAX;
662 		(void)memcpy(&desc, &nbuf[doff], sizeof(desc));
663 		desc = elf_getu32(swap, desc);
664 
665 		if (desc && file_printf(ms, ", PaX: ") == -1)
666 			return 1;
667 
668 		for (i = 0; i < __arraycount(pax); i++) {
669 			if (((1 << (int)i) & desc) == 0)
670 				continue;
671 			if (file_printf(ms, "%s%s", did++ ? "," : "",
672 			    pax[i]) == -1)
673 				return 1;
674 		}
675 		return 1;
676 	}
677 	return 0;
678 }
679 
680 private int
681 do_core_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type,
682     int swap, uint32_t namesz, uint32_t descsz,
683     size_t noff, size_t doff, int *flags, size_t size, int clazz)
684 {
685 #ifdef ELFCORE
686 	int os_style = -1;
687 	/*
688 	 * Sigh.  The 2.0.36 kernel in Debian 2.1, at
689 	 * least, doesn't correctly implement name
690 	 * sections, in core dumps, as specified by
691 	 * the "Program Linking" section of "UNIX(R) System
692 	 * V Release 4 Programmer's Guide: ANSI C and
693 	 * Programming Support Tools", because my copy
694 	 * clearly says "The first 'namesz' bytes in 'name'
695 	 * contain a *null-terminated* [emphasis mine]
696 	 * character representation of the entry's owner
697 	 * or originator", but the 2.0.36 kernel code
698 	 * doesn't include the terminating null in the
699 	 * name....
700 	 */
701 	if ((namesz == 4 && strncmp((char *)&nbuf[noff], "CORE", 4) == 0) ||
702 	    (namesz == 5 && strcmp((char *)&nbuf[noff], "CORE") == 0)) {
703 		os_style = OS_STYLE_SVR4;
704 	}
705 
706 	if ((namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0)) {
707 		os_style = OS_STYLE_FREEBSD;
708 	}
709 
710 	if ((namesz >= 11 && strncmp((char *)&nbuf[noff], "NetBSD-CORE", 11)
711 	    == 0)) {
712 		os_style = OS_STYLE_NETBSD;
713 	}
714 
715 	if (os_style != -1 && (*flags & FLAGS_DID_CORE_STYLE) == 0) {
716 		if (file_printf(ms, ", %s-style", os_style_names[os_style])
717 		    == -1)
718 			return 1;
719 		*flags |= FLAGS_DID_CORE_STYLE;
720 		*flags |= os_style;
721 	}
722 
723 	switch (os_style) {
724 	case OS_STYLE_NETBSD:
725 		if (type == NT_NETBSD_CORE_PROCINFO) {
726 			char sbuf[512];
727 			struct NetBSD_elfcore_procinfo pi;
728 			memset(&pi, 0, sizeof(pi));
729 			memcpy(&pi, nbuf + doff, descsz);
730 
731 			if (file_printf(ms, ", from '%.31s', pid=%u, uid=%u, "
732 			    "gid=%u, nlwps=%u, lwp=%u (signal %u/code %u)",
733 			    file_printable(sbuf, sizeof(sbuf),
734 			    CAST(char *, pi.cpi_name)),
735 			    elf_getu32(swap, (uint32_t)pi.cpi_pid),
736 			    elf_getu32(swap, pi.cpi_euid),
737 			    elf_getu32(swap, pi.cpi_egid),
738 			    elf_getu32(swap, pi.cpi_nlwps),
739 			    elf_getu32(swap, (uint32_t)pi.cpi_siglwp),
740 			    elf_getu32(swap, pi.cpi_signo),
741 			    elf_getu32(swap, pi.cpi_sigcode)) == -1)
742 				return 1;
743 
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 /*ARGSUSED*/
918 private int
919 do_auxv_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type,
920     int swap, uint32_t namesz __attribute__((__unused__)),
921     uint32_t descsz __attribute__((__unused__)),
922     size_t noff __attribute__((__unused__)), size_t doff,
923     int *flags, size_t size __attribute__((__unused__)), int clazz,
924     int fd, off_t ph_off, int ph_num, off_t fsize)
925 {
926 #ifdef ELFCORE
927 	Aux32Info auxv32;
928 	Aux64Info auxv64;
929 	size_t elsize = xauxv_sizeof;
930 	const char *tag;
931 	int is_string;
932 	size_t nval;
933 
934 	if ((*flags & (FLAGS_IS_CORE|FLAGS_DID_CORE_STYLE)) !=
935 	    (FLAGS_IS_CORE|FLAGS_DID_CORE_STYLE))
936 		return 0;
937 
938 	switch (*flags & FLAGS_CORE_STYLE) {
939 	case OS_STYLE_SVR4:
940 		if (type != NT_AUXV)
941 			return 0;
942 		break;
943 #ifdef notyet
944 	case OS_STYLE_NETBSD:
945 		if (type != NT_NETBSD_CORE_AUXV)
946 			return 0;
947 		break;
948 	case OS_STYLE_FREEBSD:
949 		if (type != NT_FREEBSD_PROCSTAT_AUXV)
950 			return 0;
951 		break;
952 #endif
953 	default:
954 		return 0;
955 	}
956 
957 	*flags |= FLAGS_DID_AUXV;
958 
959 	nval = 0;
960 	for (size_t off = 0; off + elsize <= descsz; off += elsize) {
961 		(void)memcpy(xauxv_addr, &nbuf[doff + off], xauxv_sizeof);
962 		/* Limit processing to 50 vector entries to prevent DoS */
963 		if (nval++ >= 50) {
964 			file_error(ms, 0, "Too many ELF Auxv elements");
965 			return 1;
966 		}
967 
968 		switch(xauxv_type) {
969 		case AT_LINUX_EXECFN:
970 			is_string = 1;
971 			tag = "execfn";
972 			break;
973 		case AT_LINUX_PLATFORM:
974 			is_string = 1;
975 			tag = "platform";
976 			break;
977 		case AT_LINUX_UID:
978 			is_string = 0;
979 			tag = "real uid";
980 			break;
981 		case AT_LINUX_GID:
982 			is_string = 0;
983 			tag = "real gid";
984 			break;
985 		case AT_LINUX_EUID:
986 			is_string = 0;
987 			tag = "effective uid";
988 			break;
989 		case AT_LINUX_EGID:
990 			is_string = 0;
991 			tag = "effective gid";
992 			break;
993 		default:
994 			is_string = 0;
995 			tag = NULL;
996 			break;
997 		}
998 
999 		if (tag == NULL)
1000 			continue;
1001 
1002 		if (is_string) {
1003 			char buf[256];
1004 			ssize_t buflen;
1005 			buflen = get_string_on_virtaddr(ms, swap, clazz, fd,
1006 			    ph_off, ph_num, fsize, xauxv_val, buf, sizeof(buf));
1007 
1008 			if (buflen == 0)
1009 				continue;
1010 
1011 			if (file_printf(ms, ", %s: '%s'", tag, buf) == -1)
1012 				return 0;
1013 		} else {
1014 			if (file_printf(ms, ", %s: %d", tag, (int) xauxv_val)
1015 			    == -1)
1016 				return 0;
1017 		}
1018 	}
1019 	return 1;
1020 #else
1021 	return 0;
1022 #endif
1023 }
1024 
1025 private size_t
1026 donote(struct magic_set *ms, void *vbuf, size_t offset, size_t size,
1027     int clazz, int swap, size_t align, int *flags, uint16_t *notecount,
1028     int fd, off_t ph_off, int ph_num, off_t fsize)
1029 {
1030 	Elf32_Nhdr nh32;
1031 	Elf64_Nhdr nh64;
1032 	size_t noff, doff;
1033 	uint32_t namesz, descsz;
1034 	unsigned char *nbuf = CAST(unsigned char *, vbuf);
1035 
1036 	if (*notecount == 0)
1037 		return 0;
1038 	--*notecount;
1039 
1040 	if (xnh_sizeof + offset > size) {
1041 		/*
1042 		 * We're out of note headers.
1043 		 */
1044 		return xnh_sizeof + offset;
1045 	}
1046 
1047 	(void)memcpy(xnh_addr, &nbuf[offset], xnh_sizeof);
1048 	offset += xnh_sizeof;
1049 
1050 	namesz = xnh_namesz;
1051 	descsz = xnh_descsz;
1052 
1053 	if ((namesz == 0) && (descsz == 0)) {
1054 		/*
1055 		 * We're out of note headers.
1056 		 */
1057 		return (offset >= size) ? offset : size;
1058 	}
1059 
1060 	if (namesz & 0x80000000) {
1061 	    (void)file_printf(ms, ", bad note name size %#lx",
1062 		(unsigned long)namesz);
1063 	    return 0;
1064 	}
1065 
1066 	if (descsz & 0x80000000) {
1067 	    (void)file_printf(ms, ", bad note description size %#lx",
1068 		(unsigned long)descsz);
1069 	    return 0;
1070 	}
1071 
1072 	noff = offset;
1073 	doff = ELF_ALIGN(offset + namesz);
1074 
1075 	if (offset + namesz > size) {
1076 		/*
1077 		 * We're past the end of the buffer.
1078 		 */
1079 		return doff;
1080 	}
1081 
1082 	offset = ELF_ALIGN(doff + descsz);
1083 	if (doff + descsz > size) {
1084 		/*
1085 		 * We're past the end of the buffer.
1086 		 */
1087 		return (offset >= size) ? offset : size;
1088 	}
1089 
1090 
1091 	if ((*flags & FLAGS_DID_OS_NOTE) == 0) {
1092 		if (do_os_note(ms, nbuf, xnh_type, swap,
1093 		    namesz, descsz, noff, doff, flags))
1094 			return offset;
1095 	}
1096 
1097 	if ((*flags & FLAGS_DID_BUILD_ID) == 0) {
1098 		if (do_bid_note(ms, nbuf, xnh_type, swap,
1099 		    namesz, descsz, noff, doff, flags))
1100 			return offset;
1101 	}
1102 
1103 	if ((*flags & FLAGS_DID_NETBSD_PAX) == 0) {
1104 		if (do_pax_note(ms, nbuf, xnh_type, swap,
1105 		    namesz, descsz, noff, doff, flags))
1106 			return offset;
1107 	}
1108 
1109 	if ((*flags & FLAGS_DID_CORE) == 0) {
1110 		if (do_core_note(ms, nbuf, xnh_type, swap,
1111 		    namesz, descsz, noff, doff, flags, size, clazz))
1112 			return offset;
1113 	}
1114 
1115 	if ((*flags & FLAGS_DID_AUXV) == 0) {
1116 		if (do_auxv_note(ms, nbuf, xnh_type, swap,
1117 			namesz, descsz, noff, doff, flags, size, clazz,
1118 			fd, ph_off, ph_num, fsize))
1119 			return offset;
1120 	}
1121 
1122 	if (namesz == 7 && strcmp((char *)&nbuf[noff], "NetBSD") == 0) {
1123 		if (descsz > 100)
1124 			descsz = 100;
1125 		switch (xnh_type) {
1126 	    	case NT_NETBSD_VERSION:
1127 			return offset;
1128 		case NT_NETBSD_MARCH:
1129 			if (*flags & FLAGS_DID_NETBSD_MARCH)
1130 				return offset;
1131 			*flags |= FLAGS_DID_NETBSD_MARCH;
1132 			if (file_printf(ms, ", compiled for: %.*s",
1133 			    (int)descsz, (const char *)&nbuf[doff]) == -1)
1134 				return offset;
1135 			break;
1136 		case NT_NETBSD_CMODEL:
1137 			if (*flags & FLAGS_DID_NETBSD_CMODEL)
1138 				return offset;
1139 			*flags |= FLAGS_DID_NETBSD_CMODEL;
1140 			if (file_printf(ms, ", compiler model: %.*s",
1141 			    (int)descsz, (const char *)&nbuf[doff]) == -1)
1142 				return offset;
1143 			break;
1144 		default:
1145 			if (*flags & FLAGS_DID_NETBSD_UNKNOWN)
1146 				return offset;
1147 			*flags |= FLAGS_DID_NETBSD_UNKNOWN;
1148 			if (file_printf(ms, ", note=%u", xnh_type) == -1)
1149 				return offset;
1150 			break;
1151 		}
1152 		return offset;
1153 	}
1154 
1155 	return offset;
1156 }
1157 
1158 /* SunOS 5.x hardware capability descriptions */
1159 typedef struct cap_desc {
1160 	uint64_t cd_mask;
1161 	const char *cd_name;
1162 } cap_desc_t;
1163 
1164 static const cap_desc_t cap_desc_sparc[] = {
1165 	{ AV_SPARC_MUL32,		"MUL32" },
1166 	{ AV_SPARC_DIV32,		"DIV32" },
1167 	{ AV_SPARC_FSMULD,		"FSMULD" },
1168 	{ AV_SPARC_V8PLUS,		"V8PLUS" },
1169 	{ AV_SPARC_POPC,		"POPC" },
1170 	{ AV_SPARC_VIS,			"VIS" },
1171 	{ AV_SPARC_VIS2,		"VIS2" },
1172 	{ AV_SPARC_ASI_BLK_INIT,	"ASI_BLK_INIT" },
1173 	{ AV_SPARC_FMAF,		"FMAF" },
1174 	{ AV_SPARC_FJFMAU,		"FJFMAU" },
1175 	{ AV_SPARC_IMA,			"IMA" },
1176 	{ 0, NULL }
1177 };
1178 
1179 static const cap_desc_t cap_desc_386[] = {
1180 	{ AV_386_FPU,			"FPU" },
1181 	{ AV_386_TSC,			"TSC" },
1182 	{ AV_386_CX8,			"CX8" },
1183 	{ AV_386_SEP,			"SEP" },
1184 	{ AV_386_AMD_SYSC,		"AMD_SYSC" },
1185 	{ AV_386_CMOV,			"CMOV" },
1186 	{ AV_386_MMX,			"MMX" },
1187 	{ AV_386_AMD_MMX,		"AMD_MMX" },
1188 	{ AV_386_AMD_3DNow,		"AMD_3DNow" },
1189 	{ AV_386_AMD_3DNowx,		"AMD_3DNowx" },
1190 	{ AV_386_FXSR,			"FXSR" },
1191 	{ AV_386_SSE,			"SSE" },
1192 	{ AV_386_SSE2,			"SSE2" },
1193 	{ AV_386_PAUSE,			"PAUSE" },
1194 	{ AV_386_SSE3,			"SSE3" },
1195 	{ AV_386_MON,			"MON" },
1196 	{ AV_386_CX16,			"CX16" },
1197 	{ AV_386_AHF,			"AHF" },
1198 	{ AV_386_TSCP,			"TSCP" },
1199 	{ AV_386_AMD_SSE4A,		"AMD_SSE4A" },
1200 	{ AV_386_POPCNT,		"POPCNT" },
1201 	{ AV_386_AMD_LZCNT,		"AMD_LZCNT" },
1202 	{ AV_386_SSSE3,			"SSSE3" },
1203 	{ AV_386_SSE4_1,		"SSE4.1" },
1204 	{ AV_386_SSE4_2,		"SSE4.2" },
1205 	{ 0, NULL }
1206 };
1207 
1208 private int
1209 doshn(struct magic_set *ms, int clazz, int swap, int fd, off_t off, int num,
1210     size_t size, off_t fsize, int mach, int strtab, int *flags,
1211     uint16_t *notecount)
1212 {
1213 	Elf32_Shdr sh32;
1214 	Elf64_Shdr sh64;
1215 	int stripped = 1, has_debug_info = 0;
1216 	size_t nbadcap = 0;
1217 	void *nbuf;
1218 	off_t noff, coff, name_off;
1219 	uint64_t cap_hw1 = 0;	/* SunOS 5.x hardware capabilities */
1220 	uint64_t cap_sf1 = 0;	/* SunOS 5.x software capabilities */
1221 	char name[50];
1222 	ssize_t namesize;
1223 
1224 	if (size != xsh_sizeof) {
1225 		if (file_printf(ms, ", corrupted section header size") == -1)
1226 			return -1;
1227 		return 0;
1228 	}
1229 
1230 	/* Read offset of name section to be able to read section names later */
1231 	if (pread(fd, xsh_addr, xsh_sizeof, CAST(off_t, (off + size * strtab)))
1232 	    < (ssize_t)xsh_sizeof) {
1233 		if (file_printf(ms, ", missing section headers") == -1)
1234 			return -1;
1235 		return 0;
1236 	}
1237 	name_off = xsh_offset;
1238 
1239 	for ( ; num; num--) {
1240 		/* Read the name of this section. */
1241 		if ((namesize = pread(fd, name, sizeof(name) - 1, name_off + xsh_name)) == -1) {
1242 			file_badread(ms);
1243 			return -1;
1244 		}
1245 		name[namesize] = '\0';
1246 		if (strcmp(name, ".debug_info") == 0) {
1247 			has_debug_info = 1;
1248 			stripped = 0;
1249 		}
1250 
1251 		if (pread(fd, xsh_addr, xsh_sizeof, off) < (ssize_t)xsh_sizeof) {
1252 			file_badread(ms);
1253 			return -1;
1254 		}
1255 		off += size;
1256 
1257 		/* Things we can determine before we seek */
1258 		switch (xsh_type) {
1259 		case SHT_SYMTAB:
1260 #if 0
1261 		case SHT_DYNSYM:
1262 #endif
1263 			stripped = 0;
1264 			break;
1265 		default:
1266 			if (fsize != SIZE_UNKNOWN && xsh_offset > fsize) {
1267 				/* Perhaps warn here */
1268 				continue;
1269 			}
1270 			break;
1271 		}
1272 
1273 
1274 		/* Things we can determine when we seek */
1275 		switch (xsh_type) {
1276 		case SHT_NOTE:
1277 			if ((uintmax_t)(xsh_size + xsh_offset) >
1278 			    (uintmax_t)fsize) {
1279 				if (file_printf(ms,
1280 				    ", note offset/size %#" INTMAX_T_FORMAT
1281 				    "x+%#" INTMAX_T_FORMAT "x exceeds"
1282 				    " file size %#" INTMAX_T_FORMAT "x",
1283 				    (uintmax_t)xsh_offset, (uintmax_t)xsh_size,
1284 				    (uintmax_t)fsize) == -1)
1285 					return -1;
1286 				return 0;
1287 			}
1288 			if ((nbuf = malloc(xsh_size)) == NULL) {
1289 				file_error(ms, errno, "Cannot allocate memory"
1290 				    " for note");
1291 				return -1;
1292 			}
1293 			if (pread(fd, nbuf, xsh_size, xsh_offset) <
1294 			    (ssize_t)xsh_size) {
1295 				file_badread(ms);
1296 				free(nbuf);
1297 				return -1;
1298 			}
1299 
1300 			noff = 0;
1301 			for (;;) {
1302 				if (noff >= (off_t)xsh_size)
1303 					break;
1304 				noff = donote(ms, nbuf, (size_t)noff,
1305 				    xsh_size, clazz, swap, 4, flags, notecount,
1306 				    fd, 0, 0, 0);
1307 				if (noff == 0)
1308 					break;
1309 			}
1310 			free(nbuf);
1311 			break;
1312 		case SHT_SUNW_cap:
1313 			switch (mach) {
1314 			case EM_SPARC:
1315 			case EM_SPARCV9:
1316 			case EM_IA_64:
1317 			case EM_386:
1318 			case EM_AMD64:
1319 				break;
1320 			default:
1321 				goto skip;
1322 			}
1323 
1324 			if (nbadcap > 5)
1325 				break;
1326 			if (lseek(fd, xsh_offset, SEEK_SET) == (off_t)-1) {
1327 				file_badseek(ms);
1328 				return -1;
1329 			}
1330 			coff = 0;
1331 			for (;;) {
1332 				Elf32_Cap cap32;
1333 				Elf64_Cap cap64;
1334 				char cbuf[/*CONSTCOND*/
1335 				    MAX(sizeof cap32, sizeof cap64)];
1336 				if ((coff += xcap_sizeof) > (off_t)xsh_size)
1337 					break;
1338 				if (read(fd, cbuf, (size_t)xcap_sizeof) !=
1339 				    (ssize_t)xcap_sizeof) {
1340 					file_badread(ms);
1341 					return -1;
1342 				}
1343 				if (cbuf[0] == 'A') {
1344 #ifdef notyet
1345 					char *p = cbuf + 1;
1346 					uint32_t len, tag;
1347 					memcpy(&len, p, sizeof(len));
1348 					p += 4;
1349 					len = getu32(swap, len);
1350 					if (memcmp("gnu", p, 3) != 0) {
1351 					    if (file_printf(ms,
1352 						", unknown capability %.3s", p)
1353 						== -1)
1354 						return -1;
1355 					    break;
1356 					}
1357 					p += strlen(p) + 1;
1358 					tag = *p++;
1359 					memcpy(&len, p, sizeof(len));
1360 					p += 4;
1361 					len = getu32(swap, len);
1362 					if (tag != 1) {
1363 					    if (file_printf(ms, ", unknown gnu"
1364 						" capability tag %d", tag)
1365 						== -1)
1366 						return -1;
1367 					    break;
1368 					}
1369 					// gnu attributes
1370 #endif
1371 					break;
1372 				}
1373 				(void)memcpy(xcap_addr, cbuf, xcap_sizeof);
1374 				switch (xcap_tag) {
1375 				case CA_SUNW_NULL:
1376 					break;
1377 				case CA_SUNW_HW_1:
1378 					cap_hw1 |= xcap_val;
1379 					break;
1380 				case CA_SUNW_SF_1:
1381 					cap_sf1 |= xcap_val;
1382 					break;
1383 				default:
1384 					if (file_printf(ms,
1385 					    ", with unknown capability "
1386 					    "%#" INT64_T_FORMAT "x = %#"
1387 					    INT64_T_FORMAT "x",
1388 					    (unsigned long long)xcap_tag,
1389 					    (unsigned long long)xcap_val) == -1)
1390 						return -1;
1391 					if (nbadcap++ > 2)
1392 						coff = xsh_size;
1393 					break;
1394 				}
1395 			}
1396 			/*FALLTHROUGH*/
1397 		skip:
1398 		default:
1399 			break;
1400 		}
1401 	}
1402 
1403 	if (has_debug_info) {
1404 		if (file_printf(ms, ", with debug_info") == -1)
1405 			return -1;
1406 	}
1407 	if (file_printf(ms, ", %sstripped", stripped ? "" : "not ") == -1)
1408 		return -1;
1409 	if (cap_hw1) {
1410 		const cap_desc_t *cdp;
1411 		switch (mach) {
1412 		case EM_SPARC:
1413 		case EM_SPARC32PLUS:
1414 		case EM_SPARCV9:
1415 			cdp = cap_desc_sparc;
1416 			break;
1417 		case EM_386:
1418 		case EM_IA_64:
1419 		case EM_AMD64:
1420 			cdp = cap_desc_386;
1421 			break;
1422 		default:
1423 			cdp = NULL;
1424 			break;
1425 		}
1426 		if (file_printf(ms, ", uses") == -1)
1427 			return -1;
1428 		if (cdp) {
1429 			while (cdp->cd_name) {
1430 				if (cap_hw1 & cdp->cd_mask) {
1431 					if (file_printf(ms,
1432 					    " %s", cdp->cd_name) == -1)
1433 						return -1;
1434 					cap_hw1 &= ~cdp->cd_mask;
1435 				}
1436 				++cdp;
1437 			}
1438 			if (cap_hw1)
1439 				if (file_printf(ms,
1440 				    " unknown hardware capability %#"
1441 				    INT64_T_FORMAT "x",
1442 				    (unsigned long long)cap_hw1) == -1)
1443 					return -1;
1444 		} else {
1445 			if (file_printf(ms,
1446 			    " hardware capability %#" INT64_T_FORMAT "x",
1447 			    (unsigned long long)cap_hw1) == -1)
1448 				return -1;
1449 		}
1450 	}
1451 	if (cap_sf1) {
1452 		if (cap_sf1 & SF1_SUNW_FPUSED) {
1453 			if (file_printf(ms,
1454 			    (cap_sf1 & SF1_SUNW_FPKNWN)
1455 			    ? ", uses frame pointer"
1456 			    : ", not known to use frame pointer") == -1)
1457 				return -1;
1458 		}
1459 		cap_sf1 &= ~SF1_SUNW_MASK;
1460 		if (cap_sf1)
1461 			if (file_printf(ms,
1462 			    ", with unknown software capability %#"
1463 			    INT64_T_FORMAT "x",
1464 			    (unsigned long long)cap_sf1) == -1)
1465 				return -1;
1466 	}
1467 	return 0;
1468 }
1469 
1470 /*
1471  * Look through the program headers of an executable image, searching
1472  * for a PT_INTERP section; if one is found, it's dynamically linked,
1473  * otherwise it's statically linked.
1474  */
1475 private int
1476 dophn_exec(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
1477     int num, size_t size, off_t fsize, int sh_num, int *flags,
1478     uint16_t *notecount)
1479 {
1480 	Elf32_Phdr ph32;
1481 	Elf64_Phdr ph64;
1482 	const char *linking_style = "statically";
1483 	const char *interp = "";
1484 	unsigned char nbuf[BUFSIZ];
1485 	char ibuf[BUFSIZ];
1486 	ssize_t bufsize;
1487 	size_t offset, align, len;
1488 
1489 	if (size != xph_sizeof) {
1490 		if (file_printf(ms, ", corrupted program header size") == -1)
1491 			return -1;
1492 		return 0;
1493 	}
1494 
1495   	for ( ; num; num--) {
1496 		if (pread(fd, xph_addr, xph_sizeof, off) < (ssize_t)xph_sizeof) {
1497 			file_badread(ms);
1498 			return -1;
1499 		}
1500 
1501 		off += size;
1502 		bufsize = 0;
1503 		align = 4;
1504 
1505 		/* Things we can determine before we seek */
1506 		switch (xph_type) {
1507 		case PT_DYNAMIC:
1508 			linking_style = "dynamically";
1509 			break;
1510 		case PT_NOTE:
1511 			if (sh_num)	/* Did this through section headers */
1512 				continue;
1513 			if (((align = xph_align) & 0x80000000UL) != 0 ||
1514 			    align < 4) {
1515 				if (file_printf(ms,
1516 				    ", invalid note alignment %#lx",
1517 				    (unsigned long)align) == -1)
1518 					return -1;
1519 				align = 4;
1520 			}
1521 			/*FALLTHROUGH*/
1522 		case PT_INTERP:
1523 			len = xph_filesz < sizeof(nbuf) ? xph_filesz
1524 			    : sizeof(nbuf);
1525 			bufsize = pread(fd, nbuf, len, xph_offset);
1526 			if (bufsize == -1) {
1527 				file_badread(ms);
1528 				return -1;
1529 			}
1530 			break;
1531 		default:
1532 			if (fsize != SIZE_UNKNOWN && xph_offset > fsize) {
1533 				/* Maybe warn here? */
1534 				continue;
1535 			}
1536 			break;
1537 		}
1538 
1539 		/* Things we can determine when we seek */
1540 		switch (xph_type) {
1541 		case PT_INTERP:
1542 			if (bufsize && nbuf[0]) {
1543 				nbuf[bufsize - 1] = '\0';
1544 				interp = (const char *)nbuf;
1545 			} else
1546 				interp = "*empty*";
1547 			break;
1548 		case PT_NOTE:
1549 			/*
1550 			 * This is a PT_NOTE section; loop through all the notes
1551 			 * in the section.
1552 			 */
1553 			offset = 0;
1554 			for (;;) {
1555 				if (offset >= (size_t)bufsize)
1556 					break;
1557 				offset = donote(ms, nbuf, offset,
1558 				    (size_t)bufsize, clazz, swap, align,
1559 				    flags, notecount, fd, 0, 0, 0);
1560 				if (offset == 0)
1561 					break;
1562 			}
1563 			break;
1564 		default:
1565 			break;
1566 		}
1567 	}
1568 	if (file_printf(ms, ", %s linked", linking_style)
1569 	    == -1)
1570 		return -1;
1571 	if (interp[0])
1572 		if (file_printf(ms, ", interpreter %s",
1573 		    file_printable(ibuf, sizeof(ibuf), interp)) == -1)
1574 			return -1;
1575 	return 0;
1576 }
1577 
1578 
1579 protected int
1580 file_tryelf(struct magic_set *ms, int fd, const unsigned char *buf,
1581     size_t nbytes)
1582 {
1583 	union {
1584 		int32_t l;
1585 		char c[sizeof (int32_t)];
1586 	} u;
1587 	int clazz;
1588 	int swap;
1589 	struct stat st;
1590 	off_t fsize;
1591 	int flags = 0;
1592 	Elf32_Ehdr elf32hdr;
1593 	Elf64_Ehdr elf64hdr;
1594 	uint16_t type, phnum, shnum, notecount;
1595 
1596 	if (ms->flags & (MAGIC_MIME|MAGIC_APPLE|MAGIC_EXTENSION))
1597 		return 0;
1598 	/*
1599 	 * ELF executables have multiple section headers in arbitrary
1600 	 * file locations and thus file(1) cannot determine it from easily.
1601 	 * Instead we traverse thru all section headers until a symbol table
1602 	 * one is found or else the binary is stripped.
1603 	 * Return immediately if it's not ELF (so we avoid pipe2file unless needed).
1604 	 */
1605 	if (buf[EI_MAG0] != ELFMAG0
1606 	    || (buf[EI_MAG1] != ELFMAG1 && buf[EI_MAG1] != OLFMAG1)
1607 	    || buf[EI_MAG2] != ELFMAG2 || buf[EI_MAG3] != ELFMAG3)
1608 		return 0;
1609 
1610 	/*
1611 	 * If we cannot seek, it must be a pipe, socket or fifo.
1612 	 */
1613 	if((lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) && (errno == ESPIPE))
1614 		fd = file_pipe2file(ms, fd, buf, nbytes);
1615 
1616 	if (fstat(fd, &st) == -1) {
1617   		file_badread(ms);
1618 		return -1;
1619 	}
1620 	if (S_ISREG(st.st_mode) || st.st_size != 0)
1621 		fsize = st.st_size;
1622 	else
1623 		fsize = SIZE_UNKNOWN;
1624 
1625 	clazz = buf[EI_CLASS];
1626 
1627 	switch (clazz) {
1628 	case ELFCLASS32:
1629 #undef elf_getu
1630 #define elf_getu(a, b)	elf_getu32(a, b)
1631 #undef elfhdr
1632 #define elfhdr elf32hdr
1633 #include "elfclass.h"
1634 	case ELFCLASS64:
1635 #undef elf_getu
1636 #define elf_getu(a, b)	elf_getu64(a, b)
1637 #undef elfhdr
1638 #define elfhdr elf64hdr
1639 #include "elfclass.h"
1640 	default:
1641 	    if (file_printf(ms, ", unknown class %d", clazz) == -1)
1642 		    return -1;
1643 	    break;
1644 	}
1645 	return 0;
1646 }
1647 #endif
1648