1133359Sobrien/*
2133359Sobrien * Copyright (c) Christos Zoulas 2003.
3133359Sobrien * All Rights Reserved.
4133359Sobrien *
5133359Sobrien * Redistribution and use in source and binary forms, with or without
6133359Sobrien * modification, are permitted provided that the following conditions
7133359Sobrien * are met:
8133359Sobrien * 1. Redistributions of source code must retain the above copyright
9133359Sobrien *    notice immediately at the beginning of the file, without modification,
10133359Sobrien *    this list of conditions, and the following disclaimer.
11133359Sobrien * 2. Redistributions in binary form must reproduce the above copyright
12133359Sobrien *    notice, this list of conditions and the following disclaimer in the
13133359Sobrien *    documentation and/or other materials provided with the distribution.
14133359Sobrien *
15133359Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16133359Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17133359Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18133359Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19133359Sobrien * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20133359Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21133359Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22133359Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23133359Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24133359Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25133359Sobrien * SUCH DAMAGE.
26133359Sobrien */
2768349Sobrien#include "file.h"
2868349Sobrien
29191771Sobrien#ifndef lint
30234449SobrienFILE_RCSID("@(#)$File: readelf.c,v 1.90 2011/08/23 08:01:12 christos Exp $")
31191771Sobrien#endif
32191771Sobrien
3368349Sobrien#ifdef BUILTIN_ELF
3468349Sobrien#include <string.h>
3568349Sobrien#include <ctype.h>
3668349Sobrien#include <stdlib.h>
3768349Sobrien#ifdef HAVE_UNISTD_H
3868349Sobrien#include <unistd.h>
3968349Sobrien#endif
4068349Sobrien
4168349Sobrien#include "readelf.h"
42186691Sobrien#include "magic.h"
4368349Sobrien
4468349Sobrien#ifdef	ELFCORE
45169942Sobrienprivate int dophn_core(struct magic_set *, int, int, int, off_t, int, size_t,
46169942Sobrien    off_t, int *);
4768349Sobrien#endif
48169942Sobrienprivate int dophn_exec(struct magic_set *, int, int, int, off_t, int, size_t,
49186691Sobrien    off_t, int *, int);
50234449Sobrienprivate int doshn(struct magic_set *, int, int, int, off_t, int, size_t,
51234449Sobrien    off_t, int *, int);
52186691Sobrienprivate size_t donote(struct magic_set *, void *, size_t, size_t, int,
53159764Sobrien    int, size_t, int *);
5468349Sobrien
55133359Sobrien#define	ELF_ALIGN(a)	((((a) + align - 1) / align) * align)
5668349Sobrien
57159764Sobrien#define isquote(c) (strchr("'\"`", (c)) != NULL)
58159764Sobrien
59133359Sobrienprivate uint16_t getu16(int, uint16_t);
60133359Sobrienprivate uint32_t getu32(int, uint32_t);
61133359Sobrienprivate uint64_t getu64(int, uint64_t);
62133359Sobrien
63275671Sdelphij#define MAX_PHNUM	256
64275671Sdelphij#define	MAX_SHNUM	1024
65275671Sdelphij
66275671Sdelphijprivate int
67275671Sdelphijtoomany(struct magic_set *ms, const char *name, uint16_t num)
68275671Sdelphij{
69275671Sdelphij	if (file_printf(ms, ", too many %s header sections (%u)", name, num
70275671Sdelphij	    ) == -1)
71275671Sdelphij		return -1;
72275671Sdelphij	return 0;
73275671Sdelphij}
74275671Sdelphij
75133359Sobrienprivate uint16_t
76103373Sobriengetu16(int swap, uint16_t value)
7768349Sobrien{
7868349Sobrien	union {
7968349Sobrien		uint16_t ui;
8068349Sobrien		char c[2];
8168349Sobrien	} retval, tmpval;
8268349Sobrien
8368349Sobrien	if (swap) {
8468349Sobrien		tmpval.ui = value;
8568349Sobrien
8668349Sobrien		retval.c[0] = tmpval.c[1];
8768349Sobrien		retval.c[1] = tmpval.c[0];
8868349Sobrien
8968349Sobrien		return retval.ui;
9068349Sobrien	} else
9168349Sobrien		return value;
9268349Sobrien}
9368349Sobrien
94133359Sobrienprivate uint32_t
95103373Sobriengetu32(int swap, uint32_t value)
9668349Sobrien{
9768349Sobrien	union {
9868349Sobrien		uint32_t ui;
9968349Sobrien		char c[4];
10068349Sobrien	} retval, tmpval;
10168349Sobrien
10268349Sobrien	if (swap) {
10368349Sobrien		tmpval.ui = value;
10468349Sobrien
10568349Sobrien		retval.c[0] = tmpval.c[3];
10668349Sobrien		retval.c[1] = tmpval.c[2];
10768349Sobrien		retval.c[2] = tmpval.c[1];
10868349Sobrien		retval.c[3] = tmpval.c[0];
10968349Sobrien
11068349Sobrien		return retval.ui;
11168349Sobrien	} else
11268349Sobrien		return value;
11368349Sobrien}
11468349Sobrien
115133359Sobrienprivate uint64_t
116103373Sobriengetu64(int swap, uint64_t value)
11768349Sobrien{
11868349Sobrien	union {
11968349Sobrien		uint64_t ui;
12068349Sobrien		char c[8];
12168349Sobrien	} retval, tmpval;
12268349Sobrien
12368349Sobrien	if (swap) {
12468349Sobrien		tmpval.ui = value;
12568349Sobrien
12668349Sobrien		retval.c[0] = tmpval.c[7];
12768349Sobrien		retval.c[1] = tmpval.c[6];
12868349Sobrien		retval.c[2] = tmpval.c[5];
12968349Sobrien		retval.c[3] = tmpval.c[4];
13068349Sobrien		retval.c[4] = tmpval.c[3];
13168349Sobrien		retval.c[5] = tmpval.c[2];
13268349Sobrien		retval.c[6] = tmpval.c[1];
13368349Sobrien		retval.c[7] = tmpval.c[0];
13468349Sobrien
13568349Sobrien		return retval.ui;
13668349Sobrien	} else
13768349Sobrien		return value;
13868349Sobrien}
13968349Sobrien
140186691Sobrien#define elf_getu16(swap, value) getu16(swap, value)
141186691Sobrien#define elf_getu32(swap, value) getu32(swap, value)
142159764Sobrien#ifdef USE_ARRAY_FOR_64BIT_TYPES
143159764Sobrien# define elf_getu64(swap, array) \
144186691Sobrien	((swap ? ((uint64_t)elf_getu32(swap, array[0])) << 32 : elf_getu32(swap, array[0])) + \
145186691Sobrien	 (swap ? elf_getu32(swap, array[1]) : ((uint64_t)elf_getu32(swap, array[1]) << 32)))
146159764Sobrien#else
147159764Sobrien# define elf_getu64(swap, value) getu64(swap, value)
148159764Sobrien#endif
149159764Sobrien
150186691Sobrien#define xsh_addr	(clazz == ELFCLASS32			\
151186691Sobrien			 ? (void *) &sh32			\
15268349Sobrien			 : (void *) &sh64)
153186691Sobrien#define xsh_sizeof	(clazz == ELFCLASS32			\
154186691Sobrien			 ? sizeof sh32				\
155111658Sobrien			 : sizeof sh64)
156186691Sobrien#define xsh_size	(clazz == ELFCLASS32			\
157186691Sobrien			 ? elf_getu32(swap, sh32.sh_size)	\
158186691Sobrien			 : elf_getu64(swap, sh64.sh_size))
159234449Sobrien#define xsh_offset	(off_t)(clazz == ELFCLASS32		\
160186691Sobrien			 ? elf_getu32(swap, sh32.sh_offset)	\
161186691Sobrien			 : elf_getu64(swap, sh64.sh_offset))
162186691Sobrien#define xsh_type	(clazz == ELFCLASS32			\
163186691Sobrien			 ? elf_getu32(swap, sh32.sh_type)	\
164186691Sobrien			 : elf_getu32(swap, sh64.sh_type))
165186691Sobrien#define xph_addr	(clazz == ELFCLASS32			\
166186691Sobrien			 ? (void *) &ph32			\
16768349Sobrien			 : (void *) &ph64)
168186691Sobrien#define xph_sizeof	(clazz == ELFCLASS32			\
169186691Sobrien			 ? sizeof ph32				\
170111658Sobrien			 : sizeof ph64)
171186691Sobrien#define xph_type	(clazz == ELFCLASS32			\
172186691Sobrien			 ? elf_getu32(swap, ph32.p_type)	\
173186691Sobrien			 : elf_getu32(swap, ph64.p_type))
174186691Sobrien#define xph_offset	(off_t)(clazz == ELFCLASS32		\
175186691Sobrien			 ? elf_getu32(swap, ph32.p_offset)	\
176186691Sobrien			 : elf_getu64(swap, ph64.p_offset))
177186691Sobrien#define xph_align	(size_t)((clazz == ELFCLASS32		\
178186691Sobrien			 ? (off_t) (ph32.p_align ? 		\
179186691Sobrien			    elf_getu32(swap, ph32.p_align) : 4) \
180186691Sobrien			 : (off_t) (ph64.p_align ?		\
181186691Sobrien			    elf_getu64(swap, ph64.p_align) : 4)))
182186691Sobrien#define xph_filesz	(size_t)((clazz == ELFCLASS32		\
183186691Sobrien			 ? elf_getu32(swap, ph32.p_filesz)	\
184186691Sobrien			 : elf_getu64(swap, ph64.p_filesz)))
185186691Sobrien#define xnh_addr	(clazz == ELFCLASS32			\
186186691Sobrien			 ? (void *) &nh32			\
187159764Sobrien			 : (void *) &nh64)
188186691Sobrien#define xph_memsz	(size_t)((clazz == ELFCLASS32		\
189186691Sobrien			 ? elf_getu32(swap, ph32.p_memsz)	\
190186691Sobrien			 : elf_getu64(swap, ph64.p_memsz)))
191186691Sobrien#define xnh_sizeof	(clazz == ELFCLASS32			\
192186691Sobrien			 ? sizeof nh32				\
193133359Sobrien			 : sizeof nh64)
194186691Sobrien#define xnh_type	(clazz == ELFCLASS32			\
195186691Sobrien			 ? elf_getu32(swap, nh32.n_type)	\
196186691Sobrien			 : elf_getu32(swap, nh64.n_type))
197186691Sobrien#define xnh_namesz	(clazz == ELFCLASS32			\
198186691Sobrien			 ? elf_getu32(swap, nh32.n_namesz)	\
199186691Sobrien			 : elf_getu32(swap, nh64.n_namesz))
200186691Sobrien#define xnh_descsz	(clazz == ELFCLASS32			\
201186691Sobrien			 ? elf_getu32(swap, nh32.n_descsz)	\
202186691Sobrien			 : elf_getu32(swap, nh64.n_descsz))
203186691Sobrien#define prpsoffsets(i)	(clazz == ELFCLASS32			\
204186691Sobrien			 ? prpsoffsets32[i]			\
20568349Sobrien			 : prpsoffsets64[i])
206186691Sobrien#define xcap_addr	(clazz == ELFCLASS32			\
207186691Sobrien			 ? (void *) &cap32			\
208186691Sobrien			 : (void *) &cap64)
209186691Sobrien#define xcap_sizeof	(clazz == ELFCLASS32			\
210186691Sobrien			 ? sizeof cap32				\
211186691Sobrien			 : sizeof cap64)
212186691Sobrien#define xcap_tag	(clazz == ELFCLASS32			\
213186691Sobrien			 ? elf_getu32(swap, cap32.c_tag)	\
214186691Sobrien			 : elf_getu64(swap, cap64.c_tag))
215186691Sobrien#define xcap_val	(clazz == ELFCLASS32			\
216186691Sobrien			 ? elf_getu32(swap, cap32.c_un.c_val)	\
217186691Sobrien			 : elf_getu64(swap, cap64.c_un.c_val))
21868349Sobrien
21968349Sobrien#ifdef ELFCORE
220186691Sobrien/*
221186691Sobrien * Try larger offsets first to avoid false matches
222186691Sobrien * from earlier data that happen to look like strings.
223186691Sobrien */
224186691Sobrienstatic const size_t	prpsoffsets32[] = {
225186691Sobrien#ifdef USE_NT_PSINFO
226186691Sobrien	104,		/* SunOS 5.x (command line) */
227186691Sobrien	88,		/* SunOS 5.x (short name) */
228186691Sobrien#endif /* USE_NT_PSINFO */
229186691Sobrien
230186691Sobrien	100,		/* SunOS 5.x (command line) */
231186691Sobrien	84,		/* SunOS 5.x (short name) */
232186691Sobrien
233186691Sobrien	44,		/* Linux (command line) */
234186691Sobrien	28,		/* Linux 2.0.36 (short name) */
235186691Sobrien
23668349Sobrien	8,		/* FreeBSD */
23768349Sobrien};
23868349Sobrien
239186691Sobrienstatic const size_t	prpsoffsets64[] = {
240186691Sobrien#ifdef USE_NT_PSINFO
241186691Sobrien	152,		/* SunOS 5.x (command line) */
242186691Sobrien	136,		/* SunOS 5.x (short name) */
243186691Sobrien#endif /* USE_NT_PSINFO */
244186691Sobrien
245186691Sobrien	136,		/* SunOS 5.x, 64-bit (command line) */
246186691Sobrien	120,		/* SunOS 5.x, 64-bit (short name) */
247186691Sobrien
248186691Sobrien	56,		/* Linux (command line) */
249186691Sobrien	40,             /* Linux (tested on core from 2.4.x, short name) */
250186691Sobrien
251159764Sobrien	16,		/* FreeBSD, 64-bit */
25268349Sobrien};
25368349Sobrien
25468349Sobrien#define	NOFFSETS32	(sizeof prpsoffsets32 / sizeof prpsoffsets32[0])
25568349Sobrien#define NOFFSETS64	(sizeof prpsoffsets64 / sizeof prpsoffsets64[0])
25668349Sobrien
257186691Sobrien#define NOFFSETS	(clazz == ELFCLASS32 ? NOFFSETS32 : NOFFSETS64)
25868349Sobrien
25968349Sobrien/*
26068349Sobrien * Look through the program headers of an executable image, searching
26168349Sobrien * for a PT_NOTE section of type NT_PRPSINFO, with a name "CORE" or
26268349Sobrien * "FreeBSD"; if one is found, try looking in various places in its
26368349Sobrien * contents for a 16-character string containing only printable
26468349Sobrien * characters - if found, that string should be the name of the program
26568349Sobrien * that dropped core.  Note: right after that 16-character string is,
26668349Sobrien * at least in SunOS 5.x (and possibly other SVR4-flavored systems) and
26768349Sobrien * Linux, a longer string (80 characters, in 5.x, probably other
26868349Sobrien * SVR4-flavored systems, and Linux) containing the start of the
26968349Sobrien * command line for that program.
27068349Sobrien *
271186691Sobrien * SunOS 5.x core files contain two PT_NOTE sections, with the types
272186691Sobrien * NT_PRPSINFO (old) and NT_PSINFO (new).  These structs contain the
273186691Sobrien * same info about the command name and command line, so it probably
274186691Sobrien * isn't worthwhile to look for NT_PSINFO, but the offsets are provided
275186691Sobrien * above (see USE_NT_PSINFO), in case we ever decide to do so.  The
276186691Sobrien * NT_PRPSINFO and NT_PSINFO sections are always in order and adjacent;
277186691Sobrien * the SunOS 5.x file command relies on this (and prefers the latter).
278186691Sobrien *
27968349Sobrien * The signal number probably appears in a section of type NT_PRSTATUS,
28068349Sobrien * but that's also rather OS-dependent, in ways that are harder to
28168349Sobrien * dissect with heuristics, so I'm not bothering with the signal number.
28268349Sobrien * (I suppose the signal number could be of interest in situations where
28368349Sobrien * you don't have the binary of the program that dropped core; if you
28468349Sobrien * *do* have that binary, the debugger will probably tell you what
28568349Sobrien * signal it was.)
28668349Sobrien */
287103373Sobrien
288103373Sobrien#define	OS_STYLE_SVR4		0
289103373Sobrien#define	OS_STYLE_FREEBSD	1
290103373Sobrien#define	OS_STYLE_NETBSD		2
291103373Sobrien
292186691Sobrienprivate const char os_style_names[][8] = {
293103373Sobrien	"SVR4",
294103373Sobrien	"FreeBSD",
295103373Sobrien	"NetBSD",
296103373Sobrien};
297103373Sobrien
298234449Sobrien#define FLAGS_DID_CORE		0x01
299234449Sobrien#define FLAGS_DID_NOTE		0x02
300234449Sobrien#define FLAGS_DID_BUILD_ID	0x04
301234449Sobrien#define FLAGS_DID_CORE_STYLE	0x08
302234449Sobrien#define FLAGS_IS_CORE		0x10
303159764Sobrien
304133359Sobrienprivate int
305186691Sobriendophn_core(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
306169942Sobrien    int num, size_t size, off_t fsize, int *flags)
30768349Sobrien{
30868349Sobrien	Elf32_Phdr ph32;
30968349Sobrien	Elf64_Phdr ph64;
310133359Sobrien	size_t offset;
311133359Sobrien	unsigned char nbuf[BUFSIZ];
312133359Sobrien	ssize_t bufsize;
31368349Sobrien
314159764Sobrien	if (size != xph_sizeof) {
315133359Sobrien		if (file_printf(ms, ", corrupted program header size") == -1)
316133359Sobrien			return -1;
317133359Sobrien		return 0;
318133359Sobrien	}
319159764Sobrien
32068349Sobrien	/*
32168349Sobrien	 * Loop through all the program headers.
32268349Sobrien	 */
32368349Sobrien	for ( ; num; num--) {
324234449Sobrien		if (lseek(fd, off, SEEK_SET) == (off_t)-1) {
325133359Sobrien			file_badseek(ms);
326133359Sobrien			return -1;
327133359Sobrien		}
328159764Sobrien		if (read(fd, xph_addr, xph_sizeof) == -1) {
329133359Sobrien			file_badread(ms);
330133359Sobrien			return -1;
331133359Sobrien		}
332234449Sobrien		off += size;
333234449Sobrien
334169942Sobrien		if (xph_offset > fsize) {
335234449Sobrien			/* Perhaps warn here */
336169942Sobrien			continue;
337169942Sobrien		}
338169942Sobrien
339159764Sobrien		if (xph_type != PT_NOTE)
34068349Sobrien			continue;
34168349Sobrien
34268349Sobrien		/*
34368349Sobrien		 * This is a PT_NOTE section; loop through all the notes
34468349Sobrien		 * in the section.
34568349Sobrien		 */
346169962Sobrien		if (lseek(fd, xph_offset, SEEK_SET) == (off_t)-1) {
347133359Sobrien			file_badseek(ms);
348133359Sobrien			return -1;
349133359Sobrien		}
350139368Sobrien		bufsize = read(fd, nbuf,
351159764Sobrien		    ((xph_filesz < sizeof(nbuf)) ? xph_filesz : sizeof(nbuf)));
352133359Sobrien		if (bufsize == -1) {
353133359Sobrien			file_badread(ms);
354133359Sobrien			return -1;
355133359Sobrien		}
35668349Sobrien		offset = 0;
35768349Sobrien		for (;;) {
358133359Sobrien			if (offset >= (size_t)bufsize)
35968349Sobrien				break;
360133359Sobrien			offset = donote(ms, nbuf, offset, (size_t)bufsize,
361186691Sobrien			    clazz, swap, 4, flags);
362133359Sobrien			if (offset == 0)
363133359Sobrien				break;
36468349Sobrien
365133359Sobrien		}
366133359Sobrien	}
367133359Sobrien	return 0;
368133359Sobrien}
369133359Sobrien#endif
370133359Sobrien
371133359Sobrienprivate size_t
372186691Sobriendonote(struct magic_set *ms, void *vbuf, size_t offset, size_t size,
373186691Sobrien    int clazz, int swap, size_t align, int *flags)
374133359Sobrien{
375133359Sobrien	Elf32_Nhdr nh32;
376133359Sobrien	Elf64_Nhdr nh64;
377133359Sobrien	size_t noff, doff;
378133359Sobrien#ifdef ELFCORE
379133359Sobrien	int os_style = -1;
380133359Sobrien#endif
381133359Sobrien	uint32_t namesz, descsz;
382186691Sobrien	unsigned char *nbuf = CAST(unsigned char *, vbuf);
383133359Sobrien
384159764Sobrien	(void)memcpy(xnh_addr, &nbuf[offset], xnh_sizeof);
385159764Sobrien	offset += xnh_sizeof;
386133359Sobrien
387159764Sobrien	namesz = xnh_namesz;
388159764Sobrien	descsz = xnh_descsz;
389133359Sobrien	if ((namesz == 0) && (descsz == 0)) {
390133359Sobrien		/*
391133359Sobrien		 * We're out of note headers.
392133359Sobrien		 */
393169942Sobrien		return (offset >= size) ? offset : size;
394133359Sobrien	}
395133359Sobrien
396133359Sobrien	if (namesz & 0x80000000) {
397133359Sobrien	    (void)file_printf(ms, ", bad note name size 0x%lx",
398133359Sobrien		(unsigned long)namesz);
399275671Sdelphij	    return 0;
400133359Sobrien	}
401133359Sobrien
402133359Sobrien	if (descsz & 0x80000000) {
403133359Sobrien	    (void)file_printf(ms, ", bad note description size 0x%lx",
404133359Sobrien		(unsigned long)descsz);
405275671Sdelphij	    return 0;
406133359Sobrien	}
407133359Sobrien
408133359Sobrien
409133359Sobrien	noff = offset;
410133359Sobrien	doff = ELF_ALIGN(offset + namesz);
411133359Sobrien
412133359Sobrien	if (offset + namesz > size) {
413133359Sobrien		/*
414133359Sobrien		 * We're past the end of the buffer.
415133359Sobrien		 */
416133359Sobrien		return doff;
417133359Sobrien	}
418133359Sobrien
419133359Sobrien	offset = ELF_ALIGN(doff + descsz);
420139368Sobrien	if (doff + descsz > size) {
421169942Sobrien		/*
422169942Sobrien		 * We're past the end of the buffer.
423169942Sobrien		 */
424169942Sobrien		return (offset >= size) ? offset : size;
425133359Sobrien	}
426133359Sobrien
427234449Sobrien	if ((*flags & (FLAGS_DID_NOTE|FLAGS_DID_BUILD_ID)) ==
428234449Sobrien	    (FLAGS_DID_NOTE|FLAGS_DID_BUILD_ID))
429169942Sobrien		goto core;
430169942Sobrien
431133359Sobrien	if (namesz == 4 && strcmp((char *)&nbuf[noff], "GNU") == 0 &&
432159764Sobrien	    xnh_type == NT_GNU_VERSION && descsz == 16) {
433133359Sobrien		uint32_t desc[4];
434133359Sobrien		(void)memcpy(desc, &nbuf[doff], sizeof(desc));
435133359Sobrien
436133359Sobrien		if (file_printf(ms, ", for GNU/") == -1)
437133359Sobrien			return size;
438186691Sobrien		switch (elf_getu32(swap, desc[0])) {
439133359Sobrien		case GNU_OS_LINUX:
440133359Sobrien			if (file_printf(ms, "Linux") == -1)
441133359Sobrien				return size;
442133359Sobrien			break;
443133359Sobrien		case GNU_OS_HURD:
444133359Sobrien			if (file_printf(ms, "Hurd") == -1)
445133359Sobrien				return size;
446133359Sobrien			break;
447133359Sobrien		case GNU_OS_SOLARIS:
448133359Sobrien			if (file_printf(ms, "Solaris") == -1)
449133359Sobrien				return size;
450133359Sobrien			break;
451186691Sobrien		case GNU_OS_KFREEBSD:
452186691Sobrien			if (file_printf(ms, "kFreeBSD") == -1)
453186691Sobrien				return size;
454186691Sobrien			break;
455186691Sobrien		case GNU_OS_KNETBSD:
456186691Sobrien			if (file_printf(ms, "kNetBSD") == -1)
457186691Sobrien				return size;
458186691Sobrien			break;
459133359Sobrien		default:
460133359Sobrien			if (file_printf(ms, "<unknown>") == -1)
461133359Sobrien				return size;
462133359Sobrien		}
463186691Sobrien		if (file_printf(ms, " %d.%d.%d", elf_getu32(swap, desc[1]),
464186691Sobrien		    elf_getu32(swap, desc[2]), elf_getu32(swap, desc[3])) == -1)
465133359Sobrien			return size;
466169942Sobrien		*flags |= FLAGS_DID_NOTE;
467133359Sobrien		return size;
468133359Sobrien	}
469133359Sobrien
470234449Sobrien	if (namesz == 4 && strcmp((char *)&nbuf[noff], "GNU") == 0 &&
471234449Sobrien	    xnh_type == NT_GNU_BUILD_ID && (descsz == 16 || descsz == 20)) {
472234449Sobrien	    uint32_t desc[5], i;
473234449Sobrien	    if (file_printf(ms, ", BuildID[%s]=0x", descsz == 16 ? "md5/uuid" :
474234449Sobrien		"sha1") == -1)
475234449Sobrien		    return size;
476234449Sobrien	    (void)memcpy(desc, &nbuf[doff], descsz);
477234449Sobrien	    for (i = 0; i < descsz >> 2; i++)
478234449Sobrien		if (file_printf(ms, "%.8x", desc[i]) == -1)
479234449Sobrien		    return size;
480234449Sobrien	    *flags |= FLAGS_DID_BUILD_ID;
481234449Sobrien	}
482234449Sobrien
483133359Sobrien	if (namesz == 7 && strcmp((char *)&nbuf[noff], "NetBSD") == 0 &&
484159764Sobrien	    xnh_type == NT_NETBSD_VERSION && descsz == 4) {
485133359Sobrien		uint32_t desc;
486133359Sobrien		(void)memcpy(&desc, &nbuf[doff], sizeof(desc));
487186691Sobrien		desc = elf_getu32(swap, desc);
488133359Sobrien
489133359Sobrien		if (file_printf(ms, ", for NetBSD") == -1)
490133359Sobrien			return size;
491133359Sobrien		/*
492133359Sobrien		 * The version number used to be stuck as 199905, and was thus
493133359Sobrien		 * basically content-free.  Newer versions of NetBSD have fixed
494133359Sobrien		 * this and now use the encoding of __NetBSD_Version__:
495133359Sobrien		 *
496133359Sobrien		 *	MMmmrrpp00
497133359Sobrien		 *
498133359Sobrien		 * M = major version
499133359Sobrien		 * m = minor version
500133359Sobrien		 * r = release ["",A-Z,Z[A-Z] but numeric]
501133359Sobrien		 * p = patchlevel
502133359Sobrien		 */
503133359Sobrien		if (desc > 100000000U) {
504169942Sobrien			uint32_t ver_patch = (desc / 100) % 100;
505169942Sobrien			uint32_t ver_rel = (desc / 10000) % 100;
506169942Sobrien			uint32_t ver_min = (desc / 1000000) % 100;
507169942Sobrien			uint32_t ver_maj = desc / 100000000;
508133359Sobrien
509133359Sobrien			if (file_printf(ms, " %u.%u", ver_maj, ver_min) == -1)
510133359Sobrien				return size;
511133359Sobrien			if (ver_rel == 0 && ver_patch != 0) {
512133359Sobrien				if (file_printf(ms, ".%u", ver_patch) == -1)
513133359Sobrien					return size;
514133359Sobrien			} else if (ver_rel != 0) {
515133359Sobrien				while (ver_rel > 26) {
516169942Sobrien					if (file_printf(ms, "Z") == -1)
517169942Sobrien						return size;
518133359Sobrien					ver_rel -= 26;
519133359Sobrien				}
520169942Sobrien				if (file_printf(ms, "%c", 'A' + ver_rel - 1)
521169942Sobrien				    == -1)
522169942Sobrien					return size;
52368349Sobrien			}
524133359Sobrien		}
525169942Sobrien		*flags |= FLAGS_DID_NOTE;
526133359Sobrien		return size;
527133359Sobrien	}
52868349Sobrien
529133359Sobrien	if (namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0 &&
530159764Sobrien	    xnh_type == NT_FREEBSD_VERSION && descsz == 4) {
531133359Sobrien		uint32_t desc;
532133359Sobrien		(void)memcpy(&desc, &nbuf[doff], sizeof(desc));
533186691Sobrien		desc = elf_getu32(swap, desc);
534133359Sobrien		if (file_printf(ms, ", for FreeBSD") == -1)
535133359Sobrien			return size;
53668349Sobrien
537133359Sobrien		/*
538133359Sobrien		 * Contents is __FreeBSD_version, whose relation to OS
539139368Sobrien		 * versions is defined by a huge table in the Porter's
540139368Sobrien		 * Handbook.  This is the general scheme:
541139368Sobrien		 *
542139368Sobrien		 * Releases:
543139368Sobrien		 * 	Mmp000 (before 4.10)
544139368Sobrien		 * 	Mmi0p0 (before 5.0)
545139368Sobrien		 * 	Mmm0p0
546139368Sobrien		 *
547139368Sobrien		 * Development branches:
548139368Sobrien		 * 	Mmpxxx (before 4.6)
549139368Sobrien		 * 	Mmp1xx (before 4.10)
550139368Sobrien		 * 	Mmi1xx (before 5.0)
551139368Sobrien		 * 	M000xx (pre-M.0)
552139368Sobrien		 * 	Mmm1xx
553139368Sobrien		 *
554139368Sobrien		 * M = major version
555139368Sobrien		 * m = minor version
556139368Sobrien		 * i = minor version increment (491000 -> 4.10)
557139368Sobrien		 * p = patchlevel
558139368Sobrien		 * x = revision
559139368Sobrien		 *
560139368Sobrien		 * The first release of FreeBSD to use ELF by default
561139368Sobrien		 * was version 3.0.
562133359Sobrien		 */
563139368Sobrien		if (desc == 460002) {
564139368Sobrien			if (file_printf(ms, " 4.6.2") == -1)
565139368Sobrien				return size;
566139368Sobrien		} else if (desc < 460100) {
567139368Sobrien			if (file_printf(ms, " %d.%d", desc / 100000,
568139368Sobrien			    desc / 10000 % 10) == -1)
569139368Sobrien				return size;
570139368Sobrien			if (desc / 1000 % 10 > 0)
571139368Sobrien				if (file_printf(ms, ".%d", desc / 1000 % 10)
572139368Sobrien				    == -1)
573133359Sobrien					return size;
574139368Sobrien			if ((desc % 1000 > 0) || (desc % 100000 == 0))
575139368Sobrien				if (file_printf(ms, " (%d)", desc) == -1)
576133359Sobrien					return size;
577139368Sobrien		} else if (desc < 500000) {
578139368Sobrien			if (file_printf(ms, " %d.%d", desc / 100000,
579139368Sobrien			    desc / 10000 % 10 + desc / 1000 % 10) == -1)
580139368Sobrien				return size;
581139368Sobrien			if (desc / 100 % 10 > 0) {
582139368Sobrien				if (file_printf(ms, " (%d)", desc) == -1)
583139368Sobrien					return size;
584139368Sobrien			} else if (desc / 10 % 10 > 0) {
585139368Sobrien				if (file_printf(ms, ".%d", desc / 10 % 10)
586139368Sobrien				    == -1)
587139368Sobrien					return size;
588103373Sobrien			}
589133359Sobrien		} else {
590133359Sobrien			if (file_printf(ms, " %d.%d", desc / 100000,
591133359Sobrien			    desc / 1000 % 100) == -1)
592133359Sobrien				return size;
593139368Sobrien			if ((desc / 100 % 10 > 0) ||
594139368Sobrien			    (desc % 100000 / 100 == 0)) {
595139368Sobrien				if (file_printf(ms, " (%d)", desc) == -1)
596133359Sobrien					return size;
597139368Sobrien			} else if (desc / 10 % 10 > 0) {
598139368Sobrien				if (file_printf(ms, ".%d", desc / 10 % 10)
599139368Sobrien				    == -1)
600133359Sobrien					return size;
601133359Sobrien			}
602133359Sobrien		}
603169942Sobrien		*flags |= FLAGS_DID_NOTE;
604133359Sobrien		return size;
605133359Sobrien	}
606103373Sobrien
607133359Sobrien	if (namesz == 8 && strcmp((char *)&nbuf[noff], "OpenBSD") == 0 &&
608159764Sobrien	    xnh_type == NT_OPENBSD_VERSION && descsz == 4) {
609133359Sobrien		if (file_printf(ms, ", for OpenBSD") == -1)
610133359Sobrien			return size;
611133359Sobrien		/* Content of note is always 0 */
612169942Sobrien		*flags |= FLAGS_DID_NOTE;
613133359Sobrien		return size;
614133359Sobrien	}
615103373Sobrien
616159764Sobrien	if (namesz == 10 && strcmp((char *)&nbuf[noff], "DragonFly") == 0 &&
617159764Sobrien	    xnh_type == NT_DRAGONFLY_VERSION && descsz == 4) {
618159764Sobrien		uint32_t desc;
619159764Sobrien		if (file_printf(ms, ", for DragonFly") == -1)
620159764Sobrien			return size;
621159764Sobrien		(void)memcpy(&desc, &nbuf[doff], sizeof(desc));
622186691Sobrien		desc = elf_getu32(swap, desc);
623159764Sobrien		if (file_printf(ms, " %d.%d.%d", desc / 100000,
624159764Sobrien		    desc / 10000 % 10, desc % 10000) == -1)
625159764Sobrien			return size;
626169942Sobrien		*flags |= FLAGS_DID_NOTE;
627159764Sobrien		return size;
628159764Sobrien	}
629159764Sobrien
630169942Sobriencore:
631133359Sobrien	/*
632133359Sobrien	 * Sigh.  The 2.0.36 kernel in Debian 2.1, at
633133359Sobrien	 * least, doesn't correctly implement name
634133359Sobrien	 * sections, in core dumps, as specified by
635133359Sobrien	 * the "Program Linking" section of "UNIX(R) System
636133359Sobrien	 * V Release 4 Programmer's Guide: ANSI C and
637133359Sobrien	 * Programming Support Tools", because my copy
638133359Sobrien	 * clearly says "The first 'namesz' bytes in 'name'
639133359Sobrien	 * contain a *null-terminated* [emphasis mine]
640133359Sobrien	 * character representation of the entry's owner
641133359Sobrien	 * or originator", but the 2.0.36 kernel code
642133359Sobrien	 * doesn't include the terminating null in the
643133359Sobrien	 * name....
644133359Sobrien	 */
645133359Sobrien	if ((namesz == 4 && strncmp((char *)&nbuf[noff], "CORE", 4) == 0) ||
646133359Sobrien	    (namesz == 5 && strcmp((char *)&nbuf[noff], "CORE") == 0)) {
647133359Sobrien		os_style = OS_STYLE_SVR4;
648133359Sobrien	}
649133359Sobrien
650133359Sobrien	if ((namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0)) {
651133359Sobrien		os_style = OS_STYLE_FREEBSD;
652133359Sobrien	}
653133359Sobrien
654133359Sobrien	if ((namesz >= 11 && strncmp((char *)&nbuf[noff], "NetBSD-CORE", 11)
655133359Sobrien	    == 0)) {
656133359Sobrien		os_style = OS_STYLE_NETBSD;
657133359Sobrien	}
658133359Sobrien
659133359Sobrien#ifdef ELFCORE
660169942Sobrien	if ((*flags & FLAGS_DID_CORE) != 0)
661169942Sobrien		return size;
662169942Sobrien
663175296Sobrien	if (os_style != -1 && (*flags & FLAGS_DID_CORE_STYLE) == 0) {
664169942Sobrien		if (file_printf(ms, ", %s-style", os_style_names[os_style])
665169942Sobrien		    == -1)
666169942Sobrien			return size;
667175296Sobrien		*flags |= FLAGS_DID_CORE_STYLE;
668159764Sobrien	}
669133359Sobrien
670159764Sobrien	switch (os_style) {
671159764Sobrien	case OS_STYLE_NETBSD:
672159764Sobrien		if (xnh_type == NT_NETBSD_CORE_PROCINFO) {
673159764Sobrien			uint32_t signo;
674159764Sobrien			/*
675159764Sobrien			 * Extract the program name.  It is at
676159764Sobrien			 * offset 0x7c, and is up to 32-bytes,
677159764Sobrien			 * including the terminating NUL.
678159764Sobrien			 */
679159764Sobrien			if (file_printf(ms, ", from '%.31s'",
680159764Sobrien			    &nbuf[doff + 0x7c]) == -1)
681159764Sobrien				return size;
682159764Sobrien
683159764Sobrien			/*
684159764Sobrien			 * Extract the signal number.  It is at
685159764Sobrien			 * offset 0x08.
686159764Sobrien			 */
687159764Sobrien			(void)memcpy(&signo, &nbuf[doff + 0x08],
688159764Sobrien			    sizeof(signo));
689159764Sobrien			if (file_printf(ms, " (signal %u)",
690186691Sobrien			    elf_getu32(swap, signo)) == -1)
691159764Sobrien				return size;
692175296Sobrien			*flags |= FLAGS_DID_CORE;
693133359Sobrien			return size;
694159764Sobrien		}
695159764Sobrien		break;
696133359Sobrien
697159764Sobrien	default:
698234449Sobrien		if (xnh_type == NT_PRPSINFO && *flags & FLAGS_IS_CORE) {
699159764Sobrien			size_t i, j;
700159764Sobrien			unsigned char c;
701159764Sobrien			/*
702159764Sobrien			 * Extract the program name.  We assume
703159764Sobrien			 * it to be 16 characters (that's what it
704159764Sobrien			 * is in SunOS 5.x and Linux).
705159764Sobrien			 *
706159764Sobrien			 * Unfortunately, it's at a different offset
707175296Sobrien			 * in various OSes, so try multiple offsets.
708159764Sobrien			 * If the characters aren't all printable,
709159764Sobrien			 * reject it.
710159764Sobrien			 */
711159764Sobrien			for (i = 0; i < NOFFSETS; i++) {
712175296Sobrien				unsigned char *cname, *cp;
713159764Sobrien				size_t reloffset = prpsoffsets(i);
714159764Sobrien				size_t noffset = doff + reloffset;
715234449Sobrien				size_t k;
716159764Sobrien				for (j = 0; j < 16; j++, noffset++,
717159764Sobrien				    reloffset++) {
71868349Sobrien					/*
719159764Sobrien					 * Make sure we're not past
720159764Sobrien					 * the end of the buffer; if
721159764Sobrien					 * we are, just give up.
72268349Sobrien					 */
723159764Sobrien					if (noffset >= size)
724133359Sobrien						goto tryanother;
725159764Sobrien
726133359Sobrien					/*
727159764Sobrien					 * Make sure we're not past
728159764Sobrien					 * the end of the contents;
729159764Sobrien					 * if we are, this obviously
730159764Sobrien					 * isn't the right offset.
731133359Sobrien					 */
732159764Sobrien					if (reloffset >= descsz)
733133359Sobrien						goto tryanother;
734159764Sobrien
735159764Sobrien					c = nbuf[noffset];
736159764Sobrien					if (c == '\0') {
737159764Sobrien						/*
738159764Sobrien						 * A '\0' at the
739159764Sobrien						 * beginning is
740159764Sobrien						 * obviously wrong.
741159764Sobrien						 * Any other '\0'
742159764Sobrien						 * means we're done.
743159764Sobrien						 */
744159764Sobrien						if (j == 0)
745159764Sobrien							goto tryanother;
746159764Sobrien						else
747159764Sobrien							break;
748159764Sobrien					} else {
749159764Sobrien						/*
750159764Sobrien						 * A nonprintable
751159764Sobrien						 * character is also
752159764Sobrien						 * wrong.
753159764Sobrien						 */
754159764Sobrien						if (!isprint(c) || isquote(c))
755159764Sobrien							goto tryanother;
756159764Sobrien					}
75768349Sobrien				}
758159764Sobrien				/*
759159764Sobrien				 * Well, that worked.
760159764Sobrien				 */
761234449Sobrien
762234449Sobrien				/*
763234449Sobrien				 * Try next offsets, in case this match is
764234449Sobrien				 * in the middle of a string.
765234449Sobrien				 */
766234449Sobrien				for (k = i + 1 ; k < NOFFSETS ; k++) {
767234449Sobrien					size_t no;
768234449Sobrien					int adjust = 1;
769234449Sobrien					if (prpsoffsets(k) >= prpsoffsets(i))
770234449Sobrien						continue;
771234449Sobrien					for (no = doff + prpsoffsets(k);
772234449Sobrien					     no < doff + prpsoffsets(i); no++)
773234449Sobrien						adjust = adjust
774234449Sobrien						         && isprint(nbuf[no]);
775234449Sobrien					if (adjust)
776234449Sobrien						i = k;
777234449Sobrien				}
778234449Sobrien
779175296Sobrien				cname = (unsigned char *)
780175296Sobrien				    &nbuf[doff + prpsoffsets(i)];
781175296Sobrien				for (cp = cname; *cp && isprint(*cp); cp++)
782175296Sobrien					continue;
783186691Sobrien				/*
784186691Sobrien				 * Linux apparently appends a space at the end
785186691Sobrien				 * of the command line: remove it.
786186691Sobrien				 */
787186691Sobrien				while (cp > cname && isspace(cp[-1]))
788175296Sobrien					cp--;
789175296Sobrien				if (file_printf(ms, ", from '%.*s'",
790175296Sobrien				    (int)(cp - cname), cname) == -1)
791159764Sobrien					return size;
792175296Sobrien				*flags |= FLAGS_DID_CORE;
793133359Sobrien				return size;
794133359Sobrien
795159764Sobrien			tryanother:
796159764Sobrien				;
797159764Sobrien			}
79868349Sobrien		}
799159764Sobrien		break;
80068349Sobrien	}
801133359Sobrien#endif
802133359Sobrien	return offset;
80368349Sobrien}
80468349Sobrien
805186691Sobrien/* SunOS 5.x hardware capability descriptions */
806186691Sobrientypedef struct cap_desc {
807186691Sobrien	uint64_t cd_mask;
808186691Sobrien	const char *cd_name;
809186691Sobrien} cap_desc_t;
810186691Sobrien
811186691Sobrienstatic const cap_desc_t cap_desc_sparc[] = {
812186691Sobrien	{ AV_SPARC_MUL32,		"MUL32" },
813186691Sobrien	{ AV_SPARC_DIV32,		"DIV32" },
814186691Sobrien	{ AV_SPARC_FSMULD,		"FSMULD" },
815186691Sobrien	{ AV_SPARC_V8PLUS,		"V8PLUS" },
816186691Sobrien	{ AV_SPARC_POPC,		"POPC" },
817186691Sobrien	{ AV_SPARC_VIS,			"VIS" },
818186691Sobrien	{ AV_SPARC_VIS2,		"VIS2" },
819186691Sobrien	{ AV_SPARC_ASI_BLK_INIT,	"ASI_BLK_INIT" },
820186691Sobrien	{ AV_SPARC_FMAF,		"FMAF" },
821186691Sobrien	{ AV_SPARC_FJFMAU,		"FJFMAU" },
822186691Sobrien	{ AV_SPARC_IMA,			"IMA" },
823186691Sobrien	{ 0, NULL }
824186691Sobrien};
825186691Sobrien
826186691Sobrienstatic const cap_desc_t cap_desc_386[] = {
827186691Sobrien	{ AV_386_FPU,			"FPU" },
828186691Sobrien	{ AV_386_TSC,			"TSC" },
829186691Sobrien	{ AV_386_CX8,			"CX8" },
830186691Sobrien	{ AV_386_SEP,			"SEP" },
831186691Sobrien	{ AV_386_AMD_SYSC,		"AMD_SYSC" },
832186691Sobrien	{ AV_386_CMOV,			"CMOV" },
833186691Sobrien	{ AV_386_MMX,			"MMX" },
834186691Sobrien	{ AV_386_AMD_MMX,		"AMD_MMX" },
835186691Sobrien	{ AV_386_AMD_3DNow,		"AMD_3DNow" },
836186691Sobrien	{ AV_386_AMD_3DNowx,		"AMD_3DNowx" },
837186691Sobrien	{ AV_386_FXSR,			"FXSR" },
838186691Sobrien	{ AV_386_SSE,			"SSE" },
839186691Sobrien	{ AV_386_SSE2,			"SSE2" },
840186691Sobrien	{ AV_386_PAUSE,			"PAUSE" },
841186691Sobrien	{ AV_386_SSE3,			"SSE3" },
842186691Sobrien	{ AV_386_MON,			"MON" },
843186691Sobrien	{ AV_386_CX16,			"CX16" },
844186691Sobrien	{ AV_386_AHF,			"AHF" },
845186691Sobrien	{ AV_386_TSCP,			"TSCP" },
846186691Sobrien	{ AV_386_AMD_SSE4A,		"AMD_SSE4A" },
847186691Sobrien	{ AV_386_POPCNT,		"POPCNT" },
848186691Sobrien	{ AV_386_AMD_LZCNT,		"AMD_LZCNT" },
849186691Sobrien	{ AV_386_SSSE3,			"SSSE3" },
850186691Sobrien	{ AV_386_SSE4_1,		"SSE4.1" },
851186691Sobrien	{ AV_386_SSE4_2,		"SSE4.2" },
852186691Sobrien	{ 0, NULL }
853186691Sobrien};
854186691Sobrien
855133359Sobrienprivate int
856186691Sobriendoshn(struct magic_set *ms, int clazz, int swap, int fd, off_t off, int num,
857234449Sobrien    size_t size, off_t fsize, int *flags, int mach)
85868349Sobrien{
859133359Sobrien	Elf32_Shdr sh32;
860133359Sobrien	Elf64_Shdr sh64;
861159764Sobrien	int stripped = 1;
862275671Sdelphij	size_t nbadcap = 0;
863159764Sobrien	void *nbuf;
864234449Sobrien	off_t noff, coff;
865186691Sobrien	uint64_t cap_hw1 = 0;	/* SunOS 5.x hardware capabilites */
866186691Sobrien	uint64_t cap_sf1 = 0;	/* SunOS 5.x software capabilites */
867133359Sobrien
868159764Sobrien	if (size != xsh_sizeof) {
869133359Sobrien		if (file_printf(ms, ", corrupted section header size") == -1)
870133359Sobrien			return -1;
871133359Sobrien		return 0;
872133359Sobrien	}
873133359Sobrien
874133359Sobrien	for ( ; num; num--) {
875234449Sobrien		if (lseek(fd, off, SEEK_SET) == (off_t)-1) {
876234449Sobrien			file_badseek(ms);
877234449Sobrien			return -1;
878234449Sobrien		}
879159764Sobrien		if (read(fd, xsh_addr, xsh_sizeof) == -1) {
880133359Sobrien			file_badread(ms);
881133359Sobrien			return -1;
882133359Sobrien		}
883234449Sobrien		off += size;
884234449Sobrien
885234449Sobrien		/* Things we can determine before we seek */
886159764Sobrien		switch (xsh_type) {
887159764Sobrien		case SHT_SYMTAB:
888159764Sobrien#if 0
889159764Sobrien		case SHT_DYNSYM:
890159764Sobrien#endif
891159764Sobrien			stripped = 0;
892159764Sobrien			break;
893234449Sobrien		default:
894234449Sobrien			if (xsh_offset > fsize) {
895234449Sobrien				/* Perhaps warn here */
896234449Sobrien				continue;
897234449Sobrien			}
898234449Sobrien			break;
899234449Sobrien		}
900234449Sobrien
901234449Sobrien		/* Things we can determine when we seek */
902234449Sobrien		switch (xsh_type) {
903159764Sobrien		case SHT_NOTE:
904159764Sobrien			if ((nbuf = malloc((size_t)xsh_size)) == NULL) {
905159764Sobrien				file_error(ms, errno, "Cannot allocate memory"
906159764Sobrien				    " for note");
907159764Sobrien				return -1;
908159764Sobrien			}
909159764Sobrien			if ((noff = lseek(fd, (off_t)xsh_offset, SEEK_SET)) ==
910159764Sobrien			    (off_t)-1) {
911159764Sobrien				file_badread(ms);
912159764Sobrien				free(nbuf);
913159764Sobrien				return -1;
914159764Sobrien			}
915159764Sobrien			if (read(fd, nbuf, (size_t)xsh_size) !=
916159764Sobrien			    (ssize_t)xsh_size) {
917159764Sobrien				free(nbuf);
918159764Sobrien				file_badread(ms);
919159764Sobrien				return -1;
920159764Sobrien			}
921159764Sobrien
922159764Sobrien			noff = 0;
923159764Sobrien			for (;;) {
924191771Sobrien				if (noff >= (off_t)xsh_size)
925159764Sobrien					break;
926159764Sobrien				noff = donote(ms, nbuf, (size_t)noff,
927186691Sobrien				    (size_t)xsh_size, clazz, swap, 4,
928169942Sobrien				    flags);
929159764Sobrien				if (noff == 0)
930159764Sobrien					break;
931159764Sobrien			}
932159764Sobrien			free(nbuf);
933159764Sobrien			break;
934186691Sobrien		case SHT_SUNW_cap:
935275671Sdelphij			if (nbadcap > 5)
936275671Sdelphij				break;
937186691Sobrien			if (lseek(fd, (off_t)xsh_offset, SEEK_SET) ==
938186691Sobrien			    (off_t)-1) {
939234449Sobrien				file_badseek(ms);
940186691Sobrien				return -1;
941186691Sobrien			}
942186691Sobrien			coff = 0;
943186691Sobrien			for (;;) {
944186691Sobrien				Elf32_Cap cap32;
945186691Sobrien				Elf64_Cap cap64;
946191771Sobrien				char cbuf[/*CONSTCOND*/
947191771Sobrien				    MAX(sizeof cap32, sizeof cap64)];
948234449Sobrien				if ((coff += xcap_sizeof) > (off_t)xsh_size)
949186691Sobrien					break;
950186691Sobrien				if (read(fd, cbuf, (size_t)xcap_sizeof) !=
951186691Sobrien				    (ssize_t)xcap_sizeof) {
952186691Sobrien					file_badread(ms);
953186691Sobrien					return -1;
954186691Sobrien				}
955186691Sobrien				(void)memcpy(xcap_addr, cbuf, xcap_sizeof);
956186691Sobrien				switch (xcap_tag) {
957186691Sobrien				case CA_SUNW_NULL:
958186691Sobrien					break;
959186691Sobrien				case CA_SUNW_HW_1:
960186691Sobrien					cap_hw1 |= xcap_val;
961186691Sobrien					break;
962186691Sobrien				case CA_SUNW_SF_1:
963186691Sobrien					cap_sf1 |= xcap_val;
964186691Sobrien					break;
965186691Sobrien				default:
966186691Sobrien					if (file_printf(ms,
967186691Sobrien					    ", with unknown capability "
968234449Sobrien					    "0x%" INT64_T_FORMAT "x = 0x%"
969234449Sobrien					    INT64_T_FORMAT "x",
970191771Sobrien					    (unsigned long long)xcap_tag,
971191771Sobrien					    (unsigned long long)xcap_val) == -1)
972186691Sobrien						return -1;
973275671Sdelphij					if (nbadcap++ > 2)
974275671Sdelphij						coff = xsh_size;
975186691Sobrien					break;
976186691Sobrien				}
977186691Sobrien			}
978186691Sobrien			break;
979234449Sobrien
980234449Sobrien		default:
981234449Sobrien			break;
982133359Sobrien		}
983133359Sobrien	}
984159764Sobrien	if (file_printf(ms, ", %sstripped", stripped ? "" : "not ") == -1)
985133359Sobrien		return -1;
986186691Sobrien	if (cap_hw1) {
987186691Sobrien		const cap_desc_t *cdp;
988186691Sobrien		switch (mach) {
989186691Sobrien		case EM_SPARC:
990186691Sobrien		case EM_SPARC32PLUS:
991186691Sobrien		case EM_SPARCV9:
992186691Sobrien			cdp = cap_desc_sparc;
993186691Sobrien			break;
994186691Sobrien		case EM_386:
995186691Sobrien		case EM_IA_64:
996186691Sobrien		case EM_AMD64:
997186691Sobrien			cdp = cap_desc_386;
998186691Sobrien			break;
999186691Sobrien		default:
1000186691Sobrien			cdp = NULL;
1001186691Sobrien			break;
1002186691Sobrien		}
1003186691Sobrien		if (file_printf(ms, ", uses") == -1)
1004186691Sobrien			return -1;
1005186691Sobrien		if (cdp) {
1006186691Sobrien			while (cdp->cd_name) {
1007186691Sobrien				if (cap_hw1 & cdp->cd_mask) {
1008186691Sobrien					if (file_printf(ms,
1009186691Sobrien					    " %s", cdp->cd_name) == -1)
1010186691Sobrien						return -1;
1011186691Sobrien					cap_hw1 &= ~cdp->cd_mask;
1012186691Sobrien				}
1013186691Sobrien				++cdp;
1014186691Sobrien			}
1015186691Sobrien			if (cap_hw1)
1016186691Sobrien				if (file_printf(ms,
1017234449Sobrien				    " unknown hardware capability 0x%"
1018234449Sobrien				    INT64_T_FORMAT "x",
1019191771Sobrien				    (unsigned long long)cap_hw1) == -1)
1020186691Sobrien					return -1;
1021186691Sobrien		} else {
1022186691Sobrien			if (file_printf(ms,
1023234449Sobrien			    " hardware capability 0x%" INT64_T_FORMAT "x",
1024191771Sobrien			    (unsigned long long)cap_hw1) == -1)
1025186691Sobrien				return -1;
1026186691Sobrien		}
1027186691Sobrien	}
1028186691Sobrien	if (cap_sf1) {
1029186691Sobrien		if (cap_sf1 & SF1_SUNW_FPUSED) {
1030186691Sobrien			if (file_printf(ms,
1031186691Sobrien			    (cap_sf1 & SF1_SUNW_FPKNWN)
1032186691Sobrien			    ? ", uses frame pointer"
1033186691Sobrien			    : ", not known to use frame pointer") == -1)
1034186691Sobrien				return -1;
1035186691Sobrien		}
1036186691Sobrien		cap_sf1 &= ~SF1_SUNW_MASK;
1037186691Sobrien		if (cap_sf1)
1038186691Sobrien			if (file_printf(ms,
1039234449Sobrien			    ", with unknown software capability 0x%"
1040234449Sobrien			    INT64_T_FORMAT "x",
1041191771Sobrien			    (unsigned long long)cap_sf1) == -1)
1042186691Sobrien				return -1;
1043186691Sobrien	}
1044133359Sobrien	return 0;
1045133359Sobrien}
1046133359Sobrien
1047133359Sobrien/*
1048133359Sobrien * Look through the program headers of an executable image, searching
1049133359Sobrien * for a PT_INTERP section; if one is found, it's dynamically linked,
1050133359Sobrien * otherwise it's statically linked.
1051133359Sobrien */
1052133359Sobrienprivate int
1053186691Sobriendophn_exec(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
1054186691Sobrien    int num, size_t size, off_t fsize, int *flags, int sh_num)
1055133359Sobrien{
1056133359Sobrien	Elf32_Phdr ph32;
1057133359Sobrien	Elf64_Phdr ph64;
1058133359Sobrien	const char *linking_style = "statically";
1059133359Sobrien	const char *shared_libraries = "";
1060133359Sobrien	unsigned char nbuf[BUFSIZ];
1061234449Sobrien	ssize_t bufsize;
1062133359Sobrien	size_t offset, align;
1063169942Sobrien
1064159764Sobrien	if (size != xph_sizeof) {
1065133359Sobrien		if (file_printf(ms, ", corrupted program header size") == -1)
1066186691Sobrien			return -1;
1067133359Sobrien		return 0;
1068133359Sobrien	}
1069169942Sobrien
1070133359Sobrien  	for ( ; num; num--) {
1071234449Sobrien		if (lseek(fd, off, SEEK_SET) == (off_t)-1) {
1072234449Sobrien			file_badseek(ms);
1073133359Sobrien			return -1;
1074133359Sobrien		}
1075169942Sobrien
1076234449Sobrien  		if (read(fd, xph_addr, xph_sizeof) == -1) {
1077234449Sobrien  			file_badread(ms);
1078133359Sobrien			return -1;
1079133359Sobrien		}
1080133359Sobrien
1081234449Sobrien		off += size;
1082169942Sobrien
1083234449Sobrien		/* Things we can determine before we seek */
1084159764Sobrien		switch (xph_type) {
1085133359Sobrien		case PT_DYNAMIC:
1086133359Sobrien			linking_style = "dynamically";
1087133359Sobrien			break;
1088133359Sobrien		case PT_INTERP:
1089133359Sobrien			shared_libraries = " (uses shared libs)";
1090133359Sobrien			break;
1091234449Sobrien		default:
1092234449Sobrien			if (xph_offset > fsize) {
1093234449Sobrien				/* Maybe warn here? */
1094234449Sobrien				continue;
1095234449Sobrien			}
1096234449Sobrien			break;
1097234449Sobrien		}
1098234449Sobrien
1099234449Sobrien		/* Things we can determine when we seek */
1100234449Sobrien		switch (xph_type) {
1101133359Sobrien		case PT_NOTE:
1102234449Sobrien			if ((align = xph_align) & 0x80000000UL) {
1103133359Sobrien				if (file_printf(ms,
1104133359Sobrien				    ", invalid note alignment 0x%lx",
1105133359Sobrien				    (unsigned long)align) == -1)
1106133359Sobrien					return -1;
1107133359Sobrien				align = 4;
1108133359Sobrien			}
1109186691Sobrien			if (sh_num)
1110186691Sobrien				break;
1111133359Sobrien			/*
1112133359Sobrien			 * This is a PT_NOTE section; loop through all the notes
1113133359Sobrien			 * in the section.
1114133359Sobrien			 */
1115234449Sobrien			if (lseek(fd, xph_offset, SEEK_SET) == (off_t)-1) {
1116133359Sobrien				file_badseek(ms);
1117133359Sobrien				return -1;
1118133359Sobrien			}
1119159764Sobrien			bufsize = read(fd, nbuf, ((xph_filesz < sizeof(nbuf)) ?
1120159764Sobrien			    xph_filesz : sizeof(nbuf)));
1121133359Sobrien			if (bufsize == -1) {
1122133359Sobrien				file_badread(ms);
1123133359Sobrien				return -1;
1124133359Sobrien			}
1125133359Sobrien			offset = 0;
1126133359Sobrien			for (;;) {
1127133359Sobrien				if (offset >= (size_t)bufsize)
1128133359Sobrien					break;
1129133359Sobrien				offset = donote(ms, nbuf, offset,
1130186691Sobrien				    (size_t)bufsize, clazz, swap, align,
1131169942Sobrien				    flags);
1132133359Sobrien				if (offset == 0)
1133133359Sobrien					break;
1134133359Sobrien			}
1135133359Sobrien			break;
1136175296Sobrien		default:
1137175296Sobrien			break;
1138133359Sobrien		}
1139133359Sobrien	}
1140133359Sobrien	if (file_printf(ms, ", %s linked%s", linking_style, shared_libraries)
1141133359Sobrien	    == -1)
1142133359Sobrien	    return -1;
1143133359Sobrien	return 0;
1144133359Sobrien}
1145133359Sobrien
1146133359Sobrien
1147133359Sobrienprotected int
1148133359Sobrienfile_tryelf(struct magic_set *ms, int fd, const unsigned char *buf,
1149133359Sobrien    size_t nbytes)
1150133359Sobrien{
115168349Sobrien	union {
1152103373Sobrien		int32_t l;
1153103373Sobrien		char c[sizeof (int32_t)];
115468349Sobrien	} u;
1155186691Sobrien	int clazz;
115668349Sobrien	int swap;
1157169942Sobrien	struct stat st;
1158169942Sobrien	off_t fsize;
1159169942Sobrien	int flags = 0;
1160186691Sobrien	Elf32_Ehdr elf32hdr;
1161186691Sobrien	Elf64_Ehdr elf64hdr;
1162275671Sdelphij	uint16_t type, phnum, shnum;
116368349Sobrien
1164191771Sobrien	if (ms->flags & (MAGIC_MIME|MAGIC_APPLE))
1165186691Sobrien		return 0;
116668349Sobrien	/*
116768349Sobrien	 * ELF executables have multiple section headers in arbitrary
116868349Sobrien	 * file locations and thus file(1) cannot determine it from easily.
116968349Sobrien	 * Instead we traverse thru all section headers until a symbol table
117068349Sobrien	 * one is found or else the binary is stripped.
1171186691Sobrien	 * Return immediately if it's not ELF (so we avoid pipe2file unless needed).
117268349Sobrien	 */
117368349Sobrien	if (buf[EI_MAG0] != ELFMAG0
117468349Sobrien	    || (buf[EI_MAG1] != ELFMAG1 && buf[EI_MAG1] != OLFMAG1)
117568349Sobrien	    || buf[EI_MAG2] != ELFMAG2 || buf[EI_MAG3] != ELFMAG3)
1176186691Sobrien		return 0;
117768349Sobrien
1178186691Sobrien	/*
1179186691Sobrien	 * If we cannot seek, it must be a pipe, socket or fifo.
1180186691Sobrien	 */
1181186691Sobrien	if((lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) && (errno == ESPIPE))
1182186691Sobrien		fd = file_pipe2file(ms, fd, buf, nbytes);
118368349Sobrien
1184186691Sobrien	if (fstat(fd, &st) == -1) {
1185186691Sobrien  		file_badread(ms);
1186186691Sobrien		return -1;
118768349Sobrien	}
1188186691Sobrien	fsize = st.st_size;
118968349Sobrien
1190186691Sobrien	clazz = buf[EI_CLASS];
119168349Sobrien
1192186691Sobrien	switch (clazz) {
1193186691Sobrien	case ELFCLASS32:
1194186691Sobrien#undef elf_getu
1195186691Sobrien#define elf_getu(a, b)	elf_getu32(a, b)
1196186691Sobrien#undef elfhdr
1197186691Sobrien#define elfhdr elf32hdr
1198186691Sobrien#include "elfclass.h"
1199186691Sobrien	case ELFCLASS64:
1200186691Sobrien#undef elf_getu
1201186691Sobrien#define elf_getu(a, b)	elf_getu64(a, b)
1202186691Sobrien#undef elfhdr
1203186691Sobrien#define elfhdr elf64hdr
1204186691Sobrien#include "elfclass.h"
1205186691Sobrien	default:
1206186691Sobrien	    if (file_printf(ms, ", unknown class %d", clazz) == -1)
1207186691Sobrien		    return -1;
1208186691Sobrien	    break;
120968349Sobrien	}
1210133359Sobrien	return 0;
121168349Sobrien}
121268349Sobrien#endif
1213