xref: /onnv-gate/usr/src/cmd/file/elf_read.c (revision 5565:538e7adac11a)
14195Sny155746 /*
24195Sny155746  * CDDL HEADER START
34195Sny155746  *
44195Sny155746  * The contents of this file are subject to the terms of the
54195Sny155746  * Common Development and Distribution License (the "License").
64195Sny155746  * You may not use this file except in compliance with the License.
74195Sny155746  *
84195Sny155746  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
94195Sny155746  * or http://www.opensolaris.org/os/licensing.
104195Sny155746  * See the License for the specific language governing permissions
114195Sny155746  * and limitations under the License.
124195Sny155746  *
134195Sny155746  * When distributing Covered Code, include this CDDL HEADER in each
144195Sny155746  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
154195Sny155746  * If applicable, add the following below this CDDL HEADER, with the
164195Sny155746  * fields enclosed by brackets "[]" replaced with your own identifying
174195Sny155746  * information: Portions Copyright [yyyy] [name of copyright owner]
184195Sny155746  *
194195Sny155746  * CDDL HEADER END
204195Sny155746  */
214195Sny155746 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
224195Sny155746 /*	  All Rights Reserved  	*/
234195Sny155746 
244195Sny155746 
254195Sny155746 /*	Copyright (c) 1987, 1988 Microsoft Corporation	*/
264195Sny155746 /*	  All Rights Reserved	*/
274195Sny155746 
284195Sny155746 /*
294195Sny155746  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
304195Sny155746  * Use is subject to license terms.
314195Sny155746  */
324195Sny155746 
334195Sny155746 #pragma ident	"%Z%%M%	%I%	%E% SMI"
344195Sny155746 
355409Sab196087 /*
365409Sab196087  * ELF files can exceed 2GB in size. A standard 32-bit program
375409Sab196087  * like 'file' cannot read past 2GB, and will be unable to see
385409Sab196087  * the ELF section headers that typically are at the end of the
395409Sab196087  * object. The simplest solution to this problem would be to make
405409Sab196087  * the 'file' command a 64-bit application. However, as a matter of
415409Sab196087  * policy, we do not want to require this. A simple command like
425409Sab196087  * 'file' should not carry such a requirement, especially as we
435409Sab196087  * support 32-bit only hardware.
445409Sab196087  *
455409Sab196087  * An alternative solution is to build this code as 32-bit
465409Sab196087  * large file aware. The usual way to do this is to define a pair
475409Sab196087  * of preprocessor definitions:
485409Sab196087  *
495409Sab196087  *	_LARGEFILE64_SOURCE
505409Sab196087  *		Map standard I/O routines to their largefile aware versions.
515409Sab196087  *
525409Sab196087  *	_FILE_OFFSET_BITS=64
535409Sab196087  *		Map off_t to off64_t
545409Sab196087  *
555409Sab196087  * The problem with this solution is that libelf is not large file capable,
565409Sab196087  * and the libelf header file will prevent compilation if
575409Sab196087  * _FILE_OFFSET_BITS is set to 64.
585409Sab196087  *
595409Sab196087  * So, the solution used in this code is to define _LARGEFILE64_SOURCE
605409Sab196087  * to get access to the 64-bit APIs, not to define _FILE_OFFSET_BITS, and to
615409Sab196087  * use our own types in place of off_t, and size_t. We read all the file
625409Sab196087  * data directly using pread64(), and avoid the use of libelf for anything
635409Sab196087  * other than the xlate functionality.
645409Sab196087  */
654195Sny155746 #define	_LARGEFILE64_SOURCE
665409Sab196087 #define	FILE_ELF_OFF_T	off64_t
675409Sab196087 #define	FILE_ELF_SIZE_T	uint64_t
684195Sny155746 
694195Sny155746 #include <ctype.h>
704195Sny155746 #include <unistd.h>
714195Sny155746 #include <fcntl.h>
724195Sny155746 #include <stdio.h>
734195Sny155746 #include <libelf.h>
744195Sny155746 #include <stdlib.h>
754195Sny155746 #include <limits.h>
764195Sny155746 #include <locale.h>
774195Sny155746 #include <string.h>
784195Sny155746 #include <errno.h>
794195Sny155746 #include <procfs.h>
804195Sny155746 #include <sys/param.h>
814195Sny155746 #include <sys/types.h>
824195Sny155746 #include <sys/stat.h>
834195Sny155746 #include <sys/elf.h>
844195Sny155746 #include <elfcap.h>
854195Sny155746 #include "file.h"
864195Sny155746 #include "elf_read.h"
874195Sny155746 
884195Sny155746 extern const char *File;
894195Sny155746 
904195Sny155746 static int get_class(void);
914195Sny155746 static int get_version(void);
924195Sny155746 static int get_format(void);
934195Sny155746 static int process_shdr(Elf_Info *);
944195Sny155746 static int process_phdr(Elf_Info *);
954195Sny155746 static int file_xlatetom(Elf_Type, char *);
964195Sny155746 static int xlatetom_nhdr(Elf_Nhdr *);
974195Sny155746 static int get_phdr(Elf_Info *, int);
984195Sny155746 static int get_shdr(Elf_Info *, int);
994195Sny155746 
1005066Sab196087 static Elf_Ehdr	EI_Ehdr;		/* Elf_Ehdr to be stored */
1015066Sab196087 static Elf_Word	EI_Ehdr_shnum;		/* # section headers */
1025066Sab196087 static Elf_Word	EI_Ehdr_phnum;		/* # program headers */
1035066Sab196087 static Elf_Word	EI_Ehdr_shstrndx;	/* Index of section hdr string table */
1045066Sab196087 static Elf_Shdr	EI_Shdr;		/* recent Elf_Shdr to be stored */
1055066Sab196087 static Elf_Phdr	EI_Phdr;		/* recent Elf_Phdr to be stored */
1064195Sny155746 
1074195Sny155746 
1084195Sny155746 static int
get_class(void)1094195Sny155746 get_class(void)
1104195Sny155746 {
1114195Sny155746 	return (EI_Ehdr.e_ident[EI_CLASS]);
1124195Sny155746 }
1134195Sny155746 
1144195Sny155746 static int
get_version(void)1154195Sny155746 get_version(void)
1164195Sny155746 {
1174195Sny155746 	/* do as what libelf:_elf_config() does */
1184195Sny155746 	return (EI_Ehdr.e_ident[EI_VERSION] ?
1194195Sny155746 	    EI_Ehdr.e_ident[EI_VERSION] : 1);
1204195Sny155746 }
1214195Sny155746 
1224195Sny155746 static int
get_format(void)1234195Sny155746 get_format(void)
1244195Sny155746 {
1254195Sny155746 	return (EI_Ehdr.e_ident[EI_DATA]);
1264195Sny155746 }
1274195Sny155746 
1284195Sny155746 /*
1294195Sny155746  * file_xlatetom:	translate different headers from file
1304195Sny155746  * 			representation to memory representaion.
1314195Sny155746  */
1324195Sny155746 #define	HDRSZ 512
1334195Sny155746 static int
file_xlatetom(Elf_Type type,char * hdr)1344195Sny155746 file_xlatetom(Elf_Type type, char *hdr)
1354195Sny155746 {
1364195Sny155746 	Elf_Data src, dst;
1374195Sny155746 	char *hbuf[HDRSZ];
1384195Sny155746 	int version, format;
1394195Sny155746 
1404195Sny155746 	version = get_version();
1414195Sny155746 	format = get_format();
1424195Sny155746 
1434195Sny155746 	/* will convert only these types */
1444195Sny155746 	if (type != ELF_T_EHDR && type != ELF_T_PHDR &&
1454195Sny155746 	    type != ELF_T_SHDR && type != ELF_T_WORD &&
1464195Sny155746 	    type != ELF_T_CAP)
1474195Sny155746 		return (ELF_READ_FAIL);
1484195Sny155746 
1494195Sny155746 	src.d_buf = (Elf_Void *)hdr;
1504195Sny155746 	src.d_type = type;
1514195Sny155746 	src.d_version = version;
1524195Sny155746 
1534195Sny155746 	dst.d_buf = (Elf_Void *)&hbuf;
1544195Sny155746 	dst.d_version = EV_CURRENT;
1554195Sny155746 
1564195Sny155746 	src.d_size = elf_fsize(type, 1, version);
1574195Sny155746 	dst.d_size = elf_fsize(type, 1, EV_CURRENT);
1584195Sny155746 	if (elf_xlatetom(&dst, &src, format) == NULL)
1594195Sny155746 		return (ELF_READ_FAIL);
1604195Sny155746 
1614195Sny155746 	(void) memcpy(hdr, &hbuf, dst.d_size);
1624195Sny155746 	return (ELF_READ_OKAY);
1634195Sny155746 }
1644195Sny155746 
1654195Sny155746 /*
1664195Sny155746  * xlatetom_nhdr:	There is no routine to convert Note header
1674195Sny155746  * 			so we convert each field of this header.
1684195Sny155746  */
1694195Sny155746 static int
xlatetom_nhdr(Elf_Nhdr * nhdr)1704195Sny155746 xlatetom_nhdr(Elf_Nhdr *nhdr)
1714195Sny155746 {
1724195Sny155746 	int r = ELF_READ_FAIL;
1734195Sny155746 
1744195Sny155746 	r |= file_xlatetom(ELF_T_WORD, (char *)&nhdr->n_namesz);
1754195Sny155746 	r |= file_xlatetom(ELF_T_WORD, (char *)&nhdr->n_descsz);
1764195Sny155746 	r |= file_xlatetom(ELF_T_WORD, (char *)&nhdr->n_type);
1774195Sny155746 	return (r);
1784195Sny155746 }
1794195Sny155746 
1804195Sny155746 /*
1814195Sny155746  * elf_read:	reads elf header, program, section headers to
1824195Sny155746  * 		collect all information needed for file(1)
1834195Sny155746  *		output and stores them in Elf_Info.
1844195Sny155746  */
1854195Sny155746 int
elf_read(int fd,Elf_Info * EI)1864195Sny155746 elf_read(int fd, Elf_Info *EI)
1874195Sny155746 {
1885409Sab196087 	FILE_ELF_SIZE_T	size;
1895409Sab196087 	int		ret = 1;
1904195Sny155746 
1914195Sny155746 	Elf_Ehdr *ehdr = &EI_Ehdr;
1924195Sny155746 
1934195Sny155746 	EI->elffd = fd;
1944195Sny155746 	size = sizeof (Elf_Ehdr);
1954195Sny155746 
1964195Sny155746 	if (pread64(EI->elffd, (void*)ehdr, size, 0) != size)
1974195Sny155746 		ret = 0;
1984195Sny155746 
1995066Sab196087 
2004195Sny155746 	if (file_xlatetom(ELF_T_EHDR, (char *)ehdr) == ELF_READ_FAIL)
2014195Sny155746 		ret = 0;
2024195Sny155746 
2034195Sny155746 	if (EI->file == NULL)
2044195Sny155746 		return (ELF_READ_FAIL);
2054195Sny155746 
2065066Sab196087 	/*
2075066Sab196087 	 * Extended section or program indexes in use? If so, special
2085066Sab196087 	 * values in the ELF header redirect us to get the real values
2095066Sab196087 	 * from shdr[0].
2105066Sab196087 	 */
2115066Sab196087 	EI_Ehdr_shnum = EI_Ehdr.e_shnum;
2125066Sab196087 	EI_Ehdr_phnum = EI_Ehdr.e_phnum;
2135066Sab196087 	EI_Ehdr_shstrndx = EI_Ehdr.e_shstrndx;
2145066Sab196087 	if (((EI_Ehdr_shnum == 0) || (EI_Ehdr_phnum == PN_XNUM)) &&
2155066Sab196087 	    (EI_Ehdr.e_shoff != 0)) {
2165074Sab196087 		if (get_shdr(EI, 0) == ELF_READ_FAIL)
2175074Sab196087 			return (ELF_READ_FAIL);
2185066Sab196087 		if (EI_Ehdr_shnum == 0)
2195066Sab196087 			EI_Ehdr_shnum = EI_Shdr.sh_size;
2205066Sab196087 		if ((EI_Ehdr_phnum == PN_XNUM) && (EI_Shdr.sh_info != 0))
2215066Sab196087 			EI_Ehdr_phnum = EI_Shdr.sh_info;
2225066Sab196087 		if (EI_Ehdr_shstrndx == SHN_XINDEX)
2235066Sab196087 			EI_Ehdr_shstrndx = EI_Shdr.sh_link;
2245066Sab196087 	}
2255066Sab196087 
2264195Sny155746 	EI->type = ehdr->e_type;
2274195Sny155746 	EI->machine = ehdr->e_machine;
2284195Sny155746 	EI->flags = ehdr->e_flags;
2294195Sny155746 
2304195Sny155746 	if (ret == 0) {
2314195Sny155746 		(void) fprintf(stderr, gettext("%s: %s: can't "
2324195Sny155746 		    "read ELF header\n"), File, EI->file);
2334195Sny155746 		return (ELF_READ_FAIL);
2344195Sny155746 	}
2354195Sny155746 	if (process_phdr(EI) == ELF_READ_FAIL)
2364195Sny155746 		return (ELF_READ_FAIL);
2374195Sny155746 
2384195Sny155746 	/* We don't need section info for core files */
2394195Sny155746 	if (ehdr->e_type != ET_CORE)
2404195Sny155746 		if (process_shdr(EI) == ELF_READ_FAIL)
2414195Sny155746 			return (ELF_READ_FAIL);
2424195Sny155746 
2434195Sny155746 	return (ELF_READ_OKAY);
2444195Sny155746 }
2454195Sny155746 
2464195Sny155746 /*
2474195Sny155746  * get_phdr:	reads program header of specified index.
2484195Sny155746  */
2494195Sny155746 static int
get_phdr(Elf_Info * EI,int inx)2504195Sny155746 get_phdr(Elf_Info *EI, int inx)
2514195Sny155746 {
2525409Sab196087 	FILE_ELF_OFF_T	off = 0;
2535409Sab196087 	FILE_ELF_SIZE_T	size;
2544195Sny155746 
2555066Sab196087 	if (inx >= EI_Ehdr_phnum)
2564195Sny155746 		return (ELF_READ_FAIL);
2574195Sny155746 
2584195Sny155746 	size = sizeof (Elf_Phdr);
2595409Sab196087 	off = (FILE_ELF_OFF_T)EI_Ehdr.e_phoff + (inx * size);
2604195Sny155746 	if (pread64(EI->elffd, (void *)&EI_Phdr, size, off) != size)
2614195Sny155746 		return (ELF_READ_FAIL);
2624195Sny155746 
2634195Sny155746 	if (file_xlatetom(ELF_T_PHDR, (char *)&EI_Phdr) == ELF_READ_FAIL)
2644195Sny155746 		return (ELF_READ_FAIL);
2654195Sny155746 
2664195Sny155746 	return (ELF_READ_OKAY);
2674195Sny155746 }
2684195Sny155746 
2694195Sny155746 /*
2704195Sny155746  * get_shdr:	reads section header of specified index.
2714195Sny155746  */
2724195Sny155746 static int
get_shdr(Elf_Info * EI,int inx)2734195Sny155746 get_shdr(Elf_Info *EI, int inx)
2744195Sny155746 {
2755409Sab196087 	FILE_ELF_OFF_T	off = 0;
2765409Sab196087 	FILE_ELF_SIZE_T	size;
2774195Sny155746 
2785066Sab196087 	/*
2795066Sab196087 	 * Prevent access to non-existent section headers.
2805066Sab196087 	 *
2815066Sab196087 	 * A value of 0 for e_shoff means that there is no section header
2825066Sab196087 	 * array in the file. A value of 0 for e_shndx does not necessarily
2835066Sab196087 	 * mean this - there can still be a 1-element section header array
2845066Sab196087 	 * to support extended section or program header indexes that
2855066Sab196087 	 * exceed the 16-bit fields used in the ELF header to represent them.
2865066Sab196087 	 */
2875066Sab196087 	if ((EI_Ehdr.e_shoff == 0) || ((inx > 0) && (inx >= EI_Ehdr_shnum)))
2884195Sny155746 		return (ELF_READ_FAIL);
2894195Sny155746 
2904195Sny155746 	size = sizeof (Elf_Shdr);
2915409Sab196087 	off = (FILE_ELF_OFF_T)EI_Ehdr.e_shoff + (inx * size);
2924195Sny155746 
2934195Sny155746 	if (pread64(EI->elffd, (void *)&EI_Shdr, size, off) != size)
2944195Sny155746 		return (ELF_READ_FAIL);
2954195Sny155746 
2964195Sny155746 	if (file_xlatetom(ELF_T_SHDR, (char *)&EI_Shdr) == ELF_READ_FAIL)
2974195Sny155746 		return (ELF_READ_FAIL);
2984195Sny155746 
2994195Sny155746 	return (ELF_READ_OKAY);
3004195Sny155746 }
3014195Sny155746 
3024195Sny155746 /*
3034195Sny155746  * process_phdr:	Read Program Headers and see if it is a core
3044195Sny155746  *			file of either new or (pre-restructured /proc)
3054195Sny155746  * 			type, read the name of the file that dumped this
3064195Sny155746  *			core, else see if this is a dynamically linked.
3074195Sny155746  */
3084195Sny155746 static int
process_phdr(Elf_Info * EI)3094195Sny155746 process_phdr(Elf_Info *EI)
3104195Sny155746 {
3114195Sny155746 	register int inx;
3124195Sny155746 
3135409Sab196087 	Elf_Nhdr	Nhdr, *nhdr;	/* note header just read */
3144195Sny155746 	Elf_Phdr	*phdr = &EI_Phdr;
3154195Sny155746 
3165409Sab196087 	FILE_ELF_SIZE_T	nsz, nmsz, dsz;
3175409Sab196087 	FILE_ELF_OFF_T	offset;
3185409Sab196087 	int	class;
3195409Sab196087 	int	ntype;
3205409Sab196087 	char	*psinfo, *fname;
3214195Sny155746 
3224195Sny155746 	nsz = sizeof (Elf_Nhdr);
3234195Sny155746 	nhdr = &Nhdr;
3244195Sny155746 	class = get_class();
3255066Sab196087 	for (inx = 0; inx < EI_Ehdr_phnum; inx++) {
3264195Sny155746 		if (get_phdr(EI, inx) == ELF_READ_FAIL)
3274195Sny155746 			return (ELF_READ_FAIL);
3284195Sny155746 
3294195Sny155746 		/* read the note if it is a core */
3304195Sny155746 		if (phdr->p_type == PT_NOTE &&
3314195Sny155746 		    EI_Ehdr.e_type == ET_CORE) {
3324195Sny155746 			/*
3334195Sny155746 			 * If the next segment is also a note, use it instead.
3344195Sny155746 			 */
3354195Sny155746 			if (get_phdr(EI, inx+1) == ELF_READ_FAIL)
3364195Sny155746 				return (ELF_READ_FAIL);
3374195Sny155746 			if (phdr->p_type != PT_NOTE) {
3384195Sny155746 				/* read the first phdr back */
3394195Sny155746 				if (get_phdr(EI, inx) == ELF_READ_FAIL)
3404195Sny155746 					return (ELF_READ_FAIL);
3414195Sny155746 			}
3424195Sny155746 			offset = phdr->p_offset;
3434195Sny155746 			if (pread64(EI->elffd, (void *)nhdr, nsz, offset)
3445066Sab196087 			    != nsz)
3454195Sny155746 				return (ELF_READ_FAIL);
3464195Sny155746 
3474195Sny155746 			/* Translate the ELF note header */
3484195Sny155746 			if (xlatetom_nhdr(nhdr) == ELF_READ_FAIL)
3494195Sny155746 				return (ELF_READ_FAIL);
3504195Sny155746 
3514195Sny155746 			ntype = nhdr->n_type;
3524195Sny155746 			nmsz = nhdr->n_namesz;
3534195Sny155746 			dsz = nhdr->n_descsz;
3544195Sny155746 
3554195Sny155746 			offset += nsz + ((nmsz + 0x03) & ~0x3);
3564195Sny155746 			if ((psinfo = malloc(dsz)) == NULL) {
3574195Sny155746 				int err = errno;
3584195Sny155746 				(void) fprintf(stderr, gettext("%s: malloc "
3594195Sny155746 				    "failed: %s\n"), File, strerror(err));
3604195Sny155746 				exit(1);
3614195Sny155746 			}
3624195Sny155746 			if (pread64(EI->elffd, psinfo, dsz, offset) != dsz)
3634195Sny155746 				return (ELF_READ_FAIL);
3644195Sny155746 			/*
3654195Sny155746 			 * We want to print the string contained
3664195Sny155746 			 * in psinfo->pr_fname[], where 'psinfo'
3674195Sny155746 			 * is either an old NT_PRPSINFO structure
3684195Sny155746 			 * or a new NT_PSINFO structure.
3694195Sny155746 			 *
3704195Sny155746 			 * Old core files have only type NT_PRPSINFO.
3714195Sny155746 			 * New core files have type NT_PSINFO.
3724195Sny155746 			 *
3734195Sny155746 			 * These structures are also different by
3744195Sny155746 			 * virtue of being contained in a core file
3754195Sny155746 			 * of either 32-bit or 64-bit type.
3764195Sny155746 			 *
3774195Sny155746 			 * To further complicate matters, we ourself
3784195Sny155746 			 * might be compiled either 32-bit or 64-bit.
3794195Sny155746 			 *
3804195Sny155746 			 * For these reason, we just *know* the offsets of
3814195Sny155746 			 * pr_fname[] into the four different structures
3824195Sny155746 			 * here, regardless of how we are compiled.
3834195Sny155746 			 */
3844195Sny155746 			if (class == ELFCLASS32) {
3854195Sny155746 				/* 32-bit core file, 32-bit structures */
3864195Sny155746 				if (ntype == NT_PSINFO)
3874195Sny155746 					fname = psinfo + 88;
3884195Sny155746 				else	/* old: NT_PRPSINFO */
3894195Sny155746 					fname = psinfo + 84;
3904195Sny155746 			} else if (class == ELFCLASS64) {
3914195Sny155746 				/* 64-bit core file, 64-bit structures */
3924195Sny155746 				if (ntype == NT_PSINFO)
3934195Sny155746 					fname = psinfo + 136;
3944195Sny155746 				else	/* old: NT_PRPSINFO */
3954195Sny155746 					fname = psinfo + 120;
3964195Sny155746 			}
3974195Sny155746 			EI->core_type = (ntype == NT_PRPSINFO)?
3985066Sab196087 			    EC_OLDCORE : EC_NEWCORE;
3994195Sny155746 			(void) memcpy(EI->fname, fname, strlen(fname));
4004195Sny155746 			free(psinfo);
4014195Sny155746 		}
4024195Sny155746 		if (phdr->p_type == PT_DYNAMIC) {
4034195Sny155746 			EI->dynamic = B_TRUE;
4044195Sny155746 		}
4054195Sny155746 	}
4064195Sny155746 	return (ELF_READ_OKAY);
4074195Sny155746 }
4084195Sny155746 
4094195Sny155746 /*
4104195Sny155746  * process_shdr:	Read Section Headers to attempt to get HW/SW
4114195Sny155746  *			capabilities by looking at the SUNW_cap
4124195Sny155746  *			section and set string in Elf_Info.
4134195Sny155746  *			Also look for symbol tables and debug
4144195Sny155746  *			information sections. Set the "stripped" field
4154195Sny155746  *			in Elf_Info with corresponding flags.
4164195Sny155746  */
4174195Sny155746 static int
process_shdr(Elf_Info * EI)4184195Sny155746 process_shdr(Elf_Info *EI)
4194195Sny155746 {
4204195Sny155746 	int 		capn, mac;
4214195Sny155746 	int 		i, j, idx;
4225409Sab196087 	FILE_ELF_OFF_T	cap_off;
4235409Sab196087 	FILE_ELF_SIZE_T	csize;
4244195Sny155746 	char		*section_name;
4254195Sny155746 	Elf_Cap 	Chdr;
4264195Sny155746 	Elf_Shdr	*shdr = &EI_Shdr;
4274195Sny155746 
4284195Sny155746 
4294195Sny155746 	csize = sizeof (Elf_Cap);
4304195Sny155746 	mac = EI_Ehdr.e_machine;
4314195Sny155746 
4324195Sny155746 	/* if there are no sections, return success anyway */
4335066Sab196087 	if (EI_Ehdr.e_shoff == 0 && EI_Ehdr_shnum == 0)
4344195Sny155746 		return (ELF_READ_OKAY);
4354195Sny155746 
4364195Sny155746 	/* read section names from String Section */
4375066Sab196087 	if (get_shdr(EI, EI_Ehdr_shstrndx) == ELF_READ_FAIL)
4384195Sny155746 		return (ELF_READ_FAIL);
4394195Sny155746 
4404195Sny155746 	if ((section_name = malloc(shdr->sh_size)) == NULL)
4414195Sny155746 		return (ELF_READ_FAIL);
4424195Sny155746 
4434195Sny155746 	if (pread64(EI->elffd, section_name, shdr->sh_size, shdr->sh_offset)
4444195Sny155746 	    != shdr->sh_size)
4454195Sny155746 		return (ELF_READ_FAIL);
4464195Sny155746 
4474195Sny155746 	/* read all the sections and process them */
4485066Sab196087 	for (idx = 1, i = 0; i < EI_Ehdr_shnum; idx++, i++) {
4494195Sny155746 		char *str;
4504195Sny155746 
4514195Sny155746 		if (get_shdr(EI, i) == ELF_READ_FAIL)
4524195Sny155746 			return (ELF_READ_FAIL);
4534195Sny155746 
4544195Sny155746 		if (shdr->sh_type == SHT_NULL) {
4554195Sny155746 			idx--;
4564195Sny155746 			continue;
4574195Sny155746 		}
4584195Sny155746 
4594195Sny155746 		cap_off = shdr->sh_offset;
4604195Sny155746 		if (shdr->sh_type == SHT_SUNW_cap) {
4614195Sny155746 			if (shdr->sh_size == 0 || shdr->sh_entsize == 0) {
4624195Sny155746 				(void) fprintf(stderr, ELF_ERR_ELFCAP1,
4634195Sny155746 				    File, EI->file);
4644195Sny155746 				return (ELF_READ_FAIL);
4654195Sny155746 			}
4664195Sny155746 			capn = (shdr->sh_size / shdr->sh_entsize);
4674195Sny155746 			for (j = 0; j < capn; j++) {
4684195Sny155746 				/*
4694195Sny155746 				 * read cap and xlate the values
4704195Sny155746 				 */
4714195Sny155746 				if (pread64(EI->elffd, &Chdr, csize, cap_off)
4725066Sab196087 				    != csize ||
4735066Sab196087 				    file_xlatetom(ELF_T_CAP, (char *)&Chdr)
4745066Sab196087 				    == 0) {
4754195Sny155746 					(void) fprintf(stderr, ELF_ERR_ELFCAP2,
4764195Sny155746 					    File, EI->file);
4774195Sny155746 					return (ELF_READ_FAIL);
4784195Sny155746 				}
4794195Sny155746 
4804195Sny155746 				if (Chdr.c_tag != CA_SUNW_NULL) {
481*5565Sab196087 					(void) elfcap_tag_to_str(
482*5565Sab196087 					    ELFCAP_STYLE_UC, Chdr.c_tag,
4835066Sab196087 					    Chdr.c_un.c_val, EI->cap_str,
484*5565Sab196087 					    sizeof (EI->cap_str),
485*5565Sab196087 					    ELFCAP_FMT_SNGSPACE, mac);
4864195Sny155746 				}
4874195Sny155746 				cap_off += csize;
4884195Sny155746 			}
4894195Sny155746 		}
4904195Sny155746 
4914195Sny155746 		/*
4924195Sny155746 		 * Definition time:
4934195Sny155746 		 *	- "not stripped" means that an executable file
4944195Sny155746 		 *	contains a Symbol Table (.symtab)
4954195Sny155746 		 *	- "stripped" means that an executable file
4964195Sny155746 		 *	does not contain a Symbol Table.
4974195Sny155746 		 * When strip -l or strip -x is run, it strips the
4984195Sny155746 		 * debugging information (.line section name (strip -l),
4994195Sny155746 		 * .line, .debug*, .stabs*, .dwarf* section names
5004195Sny155746 		 * and SHT_SUNW_DEBUGSTR and SHT_SUNW_DEBUG
5014195Sny155746 		 * section types (strip -x), however the Symbol
5024195Sny155746 		 * Table will still be present.
5034195Sny155746 		 * Therefore, if
5044195Sny155746 		 *	- No Symbol Table present, then report
5054195Sny155746 		 *		"stripped"
5064195Sny155746 		 *	- Symbol Table present with debugging
5074195Sny155746 		 *	information (line number or debug section names,
5084195Sny155746 		 *	or SHT_SUNW_DEBUGSTR or SHT_SUNW_DEBUG section
5094195Sny155746 		 *	types) then report:
5104195Sny155746 		 *		"not stripped"
5114195Sny155746 		 *	- Symbol Table present with no debugging
5124195Sny155746 		 *	information (line number or debug section names,
5134195Sny155746 		 *	or SHT_SUNW_DEBUGSTR or SHT_SUNW_DEBUG section
5144195Sny155746 		 *	types) then report:
5154195Sny155746 		 *		"not stripped, no debugging information
5164195Sny155746 		 *		available"
5174195Sny155746 		 */
5184195Sny155746 		if ((EI->stripped & E_NOSTRIP) == E_NOSTRIP)
5194195Sny155746 			continue;
5204195Sny155746 
5214195Sny155746 		if (!(EI->stripped & E_SYMTAB) &&
5224195Sny155746 		    (shdr->sh_type == SHT_SYMTAB)) {
5234195Sny155746 			EI->stripped |= E_SYMTAB;
5244195Sny155746 			continue;
5254195Sny155746 		}
5264195Sny155746 
5274195Sny155746 		str = &section_name[shdr->sh_name];
5284195Sny155746 
5294195Sny155746 		if (!(EI->stripped & E_DBGINF) &&
5304195Sny155746 		    ((shdr->sh_type == SHT_SUNW_DEBUG) ||
5314195Sny155746 		    (shdr->sh_type == SHT_SUNW_DEBUGSTR) ||
5324195Sny155746 		    (is_in_list(str)))) {
5334195Sny155746 			EI->stripped |= E_DBGINF;
5344195Sny155746 		}
5354195Sny155746 	}
5364195Sny155746 	free(section_name);
5374195Sny155746 
5384195Sny155746 	return (ELF_READ_OKAY);
5394195Sny155746 }
540