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