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