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