xref: /titanic_51/usr/src/cmd/file/elf_read.c (revision c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6)
1*c2c65e21Sny155746 /*
2*c2c65e21Sny155746  * CDDL HEADER START
3*c2c65e21Sny155746  *
4*c2c65e21Sny155746  * The contents of this file are subject to the terms of the
5*c2c65e21Sny155746  * Common Development and Distribution License (the "License").
6*c2c65e21Sny155746  * You may not use this file except in compliance with the License.
7*c2c65e21Sny155746  *
8*c2c65e21Sny155746  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*c2c65e21Sny155746  * or http://www.opensolaris.org/os/licensing.
10*c2c65e21Sny155746  * See the License for the specific language governing permissions
11*c2c65e21Sny155746  * and limitations under the License.
12*c2c65e21Sny155746  *
13*c2c65e21Sny155746  * When distributing Covered Code, include this CDDL HEADER in each
14*c2c65e21Sny155746  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*c2c65e21Sny155746  * If applicable, add the following below this CDDL HEADER, with the
16*c2c65e21Sny155746  * fields enclosed by brackets "[]" replaced with your own identifying
17*c2c65e21Sny155746  * information: Portions Copyright [yyyy] [name of copyright owner]
18*c2c65e21Sny155746  *
19*c2c65e21Sny155746  * CDDL HEADER END
20*c2c65e21Sny155746  */
21*c2c65e21Sny155746 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
22*c2c65e21Sny155746 /*	  All Rights Reserved  	*/
23*c2c65e21Sny155746 
24*c2c65e21Sny155746 
25*c2c65e21Sny155746 /*	Copyright (c) 1987, 1988 Microsoft Corporation	*/
26*c2c65e21Sny155746 /*	  All Rights Reserved	*/
27*c2c65e21Sny155746 
28*c2c65e21Sny155746 /*
29*c2c65e21Sny155746  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
30*c2c65e21Sny155746  * Use is subject to license terms.
31*c2c65e21Sny155746  */
32*c2c65e21Sny155746 
33*c2c65e21Sny155746 #pragma ident	"%Z%%M%	%I%	%E% SMI"
34*c2c65e21Sny155746 
35*c2c65e21Sny155746 #define	_LARGEFILE64_SOURCE
36*c2c65e21Sny155746 
37*c2c65e21Sny155746 #include <ctype.h>
38*c2c65e21Sny155746 #include <unistd.h>
39*c2c65e21Sny155746 #include <fcntl.h>
40*c2c65e21Sny155746 #include <stdio.h>
41*c2c65e21Sny155746 #include <libelf.h>
42*c2c65e21Sny155746 #include <stdlib.h>
43*c2c65e21Sny155746 #include <limits.h>
44*c2c65e21Sny155746 #include <locale.h>
45*c2c65e21Sny155746 #include <string.h>
46*c2c65e21Sny155746 #include <errno.h>
47*c2c65e21Sny155746 #include <procfs.h>
48*c2c65e21Sny155746 #include <sys/param.h>
49*c2c65e21Sny155746 #include <sys/types.h>
50*c2c65e21Sny155746 #include <sys/stat.h>
51*c2c65e21Sny155746 #include <sys/elf.h>
52*c2c65e21Sny155746 #include <elfcap.h>
53*c2c65e21Sny155746 #include "file.h"
54*c2c65e21Sny155746 #include "elf_read.h"
55*c2c65e21Sny155746 
56*c2c65e21Sny155746 extern const char *File;
57*c2c65e21Sny155746 
58*c2c65e21Sny155746 static int get_class(void);
59*c2c65e21Sny155746 static int get_version(void);
60*c2c65e21Sny155746 static int get_format(void);
61*c2c65e21Sny155746 static int process_shdr(Elf_Info *);
62*c2c65e21Sny155746 static int process_phdr(Elf_Info *);
63*c2c65e21Sny155746 static int file_xlatetom(Elf_Type, char *);
64*c2c65e21Sny155746 static int xlatetom_nhdr(Elf_Nhdr *);
65*c2c65e21Sny155746 static int get_phdr(Elf_Info *, int);
66*c2c65e21Sny155746 static int get_shdr(Elf_Info *, int);
67*c2c65e21Sny155746 
68*c2c65e21Sny155746 static Elf_Ehdr	EI_Ehdr; /* Elf_Ehdr to be stored */
69*c2c65e21Sny155746 static Elf_Shdr	EI_Shdr; /* recent Elf_Shdr to be stored */
70*c2c65e21Sny155746 static Elf_Phdr	EI_Phdr; /* recent Elf_Phdr to be stored */
71*c2c65e21Sny155746 
72*c2c65e21Sny155746 
73*c2c65e21Sny155746 static int
74*c2c65e21Sny155746 get_class(void)
75*c2c65e21Sny155746 {
76*c2c65e21Sny155746 	return (EI_Ehdr.e_ident[EI_CLASS]);
77*c2c65e21Sny155746 }
78*c2c65e21Sny155746 
79*c2c65e21Sny155746 static int
80*c2c65e21Sny155746 get_version(void)
81*c2c65e21Sny155746 {
82*c2c65e21Sny155746 	/* do as what libelf:_elf_config() does */
83*c2c65e21Sny155746 	return (EI_Ehdr.e_ident[EI_VERSION] ?
84*c2c65e21Sny155746 	    EI_Ehdr.e_ident[EI_VERSION] : 1);
85*c2c65e21Sny155746 }
86*c2c65e21Sny155746 
87*c2c65e21Sny155746 static int
88*c2c65e21Sny155746 get_format(void)
89*c2c65e21Sny155746 {
90*c2c65e21Sny155746 	return (EI_Ehdr.e_ident[EI_DATA]);
91*c2c65e21Sny155746 }
92*c2c65e21Sny155746 
93*c2c65e21Sny155746 /*
94*c2c65e21Sny155746  * file_xlatetom:	translate different headers from file
95*c2c65e21Sny155746  * 			representation to memory representaion.
96*c2c65e21Sny155746  */
97*c2c65e21Sny155746 #define	HDRSZ 512
98*c2c65e21Sny155746 static int
99*c2c65e21Sny155746 file_xlatetom(Elf_Type type, char *hdr)
100*c2c65e21Sny155746 {
101*c2c65e21Sny155746 	Elf_Data src, dst;
102*c2c65e21Sny155746 	char *hbuf[HDRSZ];
103*c2c65e21Sny155746 	int version, format;
104*c2c65e21Sny155746 
105*c2c65e21Sny155746 	version = get_version();
106*c2c65e21Sny155746 	format = get_format();
107*c2c65e21Sny155746 
108*c2c65e21Sny155746 	/* will convert only these types */
109*c2c65e21Sny155746 	if (type != ELF_T_EHDR && type != ELF_T_PHDR &&
110*c2c65e21Sny155746 	    type != ELF_T_SHDR && type != ELF_T_WORD &&
111*c2c65e21Sny155746 	    type != ELF_T_CAP)
112*c2c65e21Sny155746 		return (ELF_READ_FAIL);
113*c2c65e21Sny155746 
114*c2c65e21Sny155746 	src.d_buf = (Elf_Void *)hdr;
115*c2c65e21Sny155746 	src.d_type = type;
116*c2c65e21Sny155746 	src.d_version = version;
117*c2c65e21Sny155746 
118*c2c65e21Sny155746 	dst.d_buf = (Elf_Void *)&hbuf;
119*c2c65e21Sny155746 	dst.d_version = EV_CURRENT;
120*c2c65e21Sny155746 
121*c2c65e21Sny155746 	src.d_size = elf_fsize(type, 1, version);
122*c2c65e21Sny155746 	dst.d_size = elf_fsize(type, 1, EV_CURRENT);
123*c2c65e21Sny155746 	if (elf_xlatetom(&dst, &src, format) == NULL)
124*c2c65e21Sny155746 		return (ELF_READ_FAIL);
125*c2c65e21Sny155746 
126*c2c65e21Sny155746 	(void) memcpy(hdr, &hbuf, dst.d_size);
127*c2c65e21Sny155746 	return (ELF_READ_OKAY);
128*c2c65e21Sny155746 }
129*c2c65e21Sny155746 
130*c2c65e21Sny155746 /*
131*c2c65e21Sny155746  * xlatetom_nhdr:	There is no routine to convert Note header
132*c2c65e21Sny155746  * 			so we convert each field of this header.
133*c2c65e21Sny155746  */
134*c2c65e21Sny155746 static int
135*c2c65e21Sny155746 xlatetom_nhdr(Elf_Nhdr *nhdr)
136*c2c65e21Sny155746 {
137*c2c65e21Sny155746 	int r = ELF_READ_FAIL;
138*c2c65e21Sny155746 
139*c2c65e21Sny155746 	r |= file_xlatetom(ELF_T_WORD, (char *)&nhdr->n_namesz);
140*c2c65e21Sny155746 	r |= file_xlatetom(ELF_T_WORD, (char *)&nhdr->n_descsz);
141*c2c65e21Sny155746 	r |= file_xlatetom(ELF_T_WORD, (char *)&nhdr->n_type);
142*c2c65e21Sny155746 	return (r);
143*c2c65e21Sny155746 }
144*c2c65e21Sny155746 
145*c2c65e21Sny155746 /*
146*c2c65e21Sny155746  * elf_read:	reads elf header, program, section headers to
147*c2c65e21Sny155746  * 		collect all information needed for file(1)
148*c2c65e21Sny155746  *		output and stores them in Elf_Info.
149*c2c65e21Sny155746  */
150*c2c65e21Sny155746 int
151*c2c65e21Sny155746 elf_read(int fd, Elf_Info *EI)
152*c2c65e21Sny155746 {
153*c2c65e21Sny155746 	size_t size;
154*c2c65e21Sny155746 	int ret = 1;
155*c2c65e21Sny155746 
156*c2c65e21Sny155746 	Elf_Ehdr *ehdr = &EI_Ehdr;
157*c2c65e21Sny155746 
158*c2c65e21Sny155746 	EI->elffd = fd;
159*c2c65e21Sny155746 	size = sizeof (Elf_Ehdr);
160*c2c65e21Sny155746 
161*c2c65e21Sny155746 	if (pread64(EI->elffd, (void*)ehdr, size, 0) != size)
162*c2c65e21Sny155746 		ret = 0;
163*c2c65e21Sny155746 
164*c2c65e21Sny155746 	if (file_xlatetom(ELF_T_EHDR, (char *)ehdr) == ELF_READ_FAIL)
165*c2c65e21Sny155746 		ret = 0;
166*c2c65e21Sny155746 
167*c2c65e21Sny155746 	if (EI->file == NULL)
168*c2c65e21Sny155746 		return (ELF_READ_FAIL);
169*c2c65e21Sny155746 
170*c2c65e21Sny155746 	EI->type = ehdr->e_type;
171*c2c65e21Sny155746 	EI->machine = ehdr->e_machine;
172*c2c65e21Sny155746 	EI->flags = ehdr->e_flags;
173*c2c65e21Sny155746 
174*c2c65e21Sny155746 	if (ret == 0) {
175*c2c65e21Sny155746 		(void) fprintf(stderr, gettext("%s: %s: can't "
176*c2c65e21Sny155746 		    "read ELF header\n"), File, EI->file);
177*c2c65e21Sny155746 		return (ELF_READ_FAIL);
178*c2c65e21Sny155746 	}
179*c2c65e21Sny155746 	if (process_phdr(EI) == ELF_READ_FAIL)
180*c2c65e21Sny155746 		return (ELF_READ_FAIL);
181*c2c65e21Sny155746 
182*c2c65e21Sny155746 	/* We don't need section info for core files */
183*c2c65e21Sny155746 	if (ehdr->e_type != ET_CORE)
184*c2c65e21Sny155746 		if (process_shdr(EI) == ELF_READ_FAIL)
185*c2c65e21Sny155746 			return (ELF_READ_FAIL);
186*c2c65e21Sny155746 
187*c2c65e21Sny155746 	return (ELF_READ_OKAY);
188*c2c65e21Sny155746 }
189*c2c65e21Sny155746 
190*c2c65e21Sny155746 /*
191*c2c65e21Sny155746  * get_phdr:	reads program header of specified index.
192*c2c65e21Sny155746  */
193*c2c65e21Sny155746 static int
194*c2c65e21Sny155746 get_phdr(Elf_Info *EI, int inx)
195*c2c65e21Sny155746 {
196*c2c65e21Sny155746 	off_t off = 0;
197*c2c65e21Sny155746 	size_t size;
198*c2c65e21Sny155746 	Elf_Ehdr *ehdr = &EI_Ehdr;
199*c2c65e21Sny155746 
200*c2c65e21Sny155746 	if (inx >= ehdr->e_phnum)
201*c2c65e21Sny155746 		return (ELF_READ_FAIL);
202*c2c65e21Sny155746 
203*c2c65e21Sny155746 	size = sizeof (Elf_Phdr);
204*c2c65e21Sny155746 	off = (off_t)ehdr->e_phoff + (inx * size);
205*c2c65e21Sny155746 	if (pread64(EI->elffd, (void *)&EI_Phdr, size, off) != size)
206*c2c65e21Sny155746 		return (ELF_READ_FAIL);
207*c2c65e21Sny155746 
208*c2c65e21Sny155746 	if (file_xlatetom(ELF_T_PHDR, (char *)&EI_Phdr) == ELF_READ_FAIL)
209*c2c65e21Sny155746 		return (ELF_READ_FAIL);
210*c2c65e21Sny155746 
211*c2c65e21Sny155746 	return (ELF_READ_OKAY);
212*c2c65e21Sny155746 }
213*c2c65e21Sny155746 
214*c2c65e21Sny155746 /*
215*c2c65e21Sny155746  * get_shdr:	reads section header of specified index.
216*c2c65e21Sny155746  */
217*c2c65e21Sny155746 static int
218*c2c65e21Sny155746 get_shdr(Elf_Info *EI, int inx)
219*c2c65e21Sny155746 {
220*c2c65e21Sny155746 	off_t off = 0;
221*c2c65e21Sny155746 	size_t size;
222*c2c65e21Sny155746 	Elf_Ehdr *ehdr = &EI_Ehdr;
223*c2c65e21Sny155746 
224*c2c65e21Sny155746 	if (inx >= ehdr->e_shnum)
225*c2c65e21Sny155746 		return (ELF_READ_FAIL);
226*c2c65e21Sny155746 
227*c2c65e21Sny155746 	size = sizeof (Elf_Shdr);
228*c2c65e21Sny155746 	off = (off_t)ehdr->e_shoff + (inx * size);
229*c2c65e21Sny155746 
230*c2c65e21Sny155746 	if (pread64(EI->elffd, (void *)&EI_Shdr, size, off) != size)
231*c2c65e21Sny155746 		return (ELF_READ_FAIL);
232*c2c65e21Sny155746 
233*c2c65e21Sny155746 	if (file_xlatetom(ELF_T_SHDR, (char *)&EI_Shdr) == ELF_READ_FAIL)
234*c2c65e21Sny155746 		return (ELF_READ_FAIL);
235*c2c65e21Sny155746 
236*c2c65e21Sny155746 	return (ELF_READ_OKAY);
237*c2c65e21Sny155746 }
238*c2c65e21Sny155746 
239*c2c65e21Sny155746 /*
240*c2c65e21Sny155746  * process_phdr:	Read Program Headers and see if it is a core
241*c2c65e21Sny155746  *			file of either new or (pre-restructured /proc)
242*c2c65e21Sny155746  * 			type, read the name of the file that dumped this
243*c2c65e21Sny155746  *			core, else see if this is a dynamically linked.
244*c2c65e21Sny155746  */
245*c2c65e21Sny155746 static int
246*c2c65e21Sny155746 process_phdr(Elf_Info *EI)
247*c2c65e21Sny155746 {
248*c2c65e21Sny155746 	register int inx;
249*c2c65e21Sny155746 
250*c2c65e21Sny155746 	Elf_Nhdr Nhdr, *nhdr;	/* note header just read */
251*c2c65e21Sny155746 	Elf_Phdr	*phdr = &EI_Phdr;
252*c2c65e21Sny155746 
253*c2c65e21Sny155746 	int class;
254*c2c65e21Sny155746 	int ntype;
255*c2c65e21Sny155746 	size_t nsz, nmsz, dsz;
256*c2c65e21Sny155746 	off_t offset;
257*c2c65e21Sny155746 	char *psinfo, *fname;
258*c2c65e21Sny155746 
259*c2c65e21Sny155746 	nsz = sizeof (Elf_Nhdr);
260*c2c65e21Sny155746 	nhdr = &Nhdr;
261*c2c65e21Sny155746 	class = get_class();
262*c2c65e21Sny155746 	for (inx = 0; inx < EI_Ehdr.e_phnum; inx++) {
263*c2c65e21Sny155746 		if (get_phdr(EI, inx) == ELF_READ_FAIL)
264*c2c65e21Sny155746 			return (ELF_READ_FAIL);
265*c2c65e21Sny155746 
266*c2c65e21Sny155746 		/* read the note if it is a core */
267*c2c65e21Sny155746 		if (phdr->p_type == PT_NOTE &&
268*c2c65e21Sny155746 		    EI_Ehdr.e_type == ET_CORE) {
269*c2c65e21Sny155746 			/*
270*c2c65e21Sny155746 			 * If the next segment is also a note, use it instead.
271*c2c65e21Sny155746 			 */
272*c2c65e21Sny155746 			if (get_phdr(EI, inx+1) == ELF_READ_FAIL)
273*c2c65e21Sny155746 				return (ELF_READ_FAIL);
274*c2c65e21Sny155746 			if (phdr->p_type != PT_NOTE) {
275*c2c65e21Sny155746 				/* read the first phdr back */
276*c2c65e21Sny155746 				if (get_phdr(EI, inx) == ELF_READ_FAIL)
277*c2c65e21Sny155746 					return (ELF_READ_FAIL);
278*c2c65e21Sny155746 			}
279*c2c65e21Sny155746 			offset = phdr->p_offset;
280*c2c65e21Sny155746 			if (pread64(EI->elffd, (void *)nhdr, nsz, offset)
281*c2c65e21Sny155746 				!= nsz)
282*c2c65e21Sny155746 				return (ELF_READ_FAIL);
283*c2c65e21Sny155746 
284*c2c65e21Sny155746 			/* Translate the ELF note header */
285*c2c65e21Sny155746 			if (xlatetom_nhdr(nhdr) == ELF_READ_FAIL)
286*c2c65e21Sny155746 				return (ELF_READ_FAIL);
287*c2c65e21Sny155746 
288*c2c65e21Sny155746 			ntype = nhdr->n_type;
289*c2c65e21Sny155746 			nmsz = nhdr->n_namesz;
290*c2c65e21Sny155746 			dsz = nhdr->n_descsz;
291*c2c65e21Sny155746 
292*c2c65e21Sny155746 			offset += nsz + ((nmsz + 0x03) & ~0x3);
293*c2c65e21Sny155746 			if ((psinfo = malloc(dsz)) == NULL) {
294*c2c65e21Sny155746 				int err = errno;
295*c2c65e21Sny155746 				(void) fprintf(stderr, gettext("%s: malloc "
296*c2c65e21Sny155746 				    "failed: %s\n"), File, strerror(err));
297*c2c65e21Sny155746 				exit(1);
298*c2c65e21Sny155746 			}
299*c2c65e21Sny155746 			if (pread64(EI->elffd, psinfo, dsz, offset) != dsz)
300*c2c65e21Sny155746 				return (ELF_READ_FAIL);
301*c2c65e21Sny155746 			/*
302*c2c65e21Sny155746 			 * We want to print the string contained
303*c2c65e21Sny155746 			 * in psinfo->pr_fname[], where 'psinfo'
304*c2c65e21Sny155746 			 * is either an old NT_PRPSINFO structure
305*c2c65e21Sny155746 			 * or a new NT_PSINFO structure.
306*c2c65e21Sny155746 			 *
307*c2c65e21Sny155746 			 * Old core files have only type NT_PRPSINFO.
308*c2c65e21Sny155746 			 * New core files have type NT_PSINFO.
309*c2c65e21Sny155746 			 *
310*c2c65e21Sny155746 			 * These structures are also different by
311*c2c65e21Sny155746 			 * virtue of being contained in a core file
312*c2c65e21Sny155746 			 * of either 32-bit or 64-bit type.
313*c2c65e21Sny155746 			 *
314*c2c65e21Sny155746 			 * To further complicate matters, we ourself
315*c2c65e21Sny155746 			 * might be compiled either 32-bit or 64-bit.
316*c2c65e21Sny155746 			 *
317*c2c65e21Sny155746 			 * For these reason, we just *know* the offsets of
318*c2c65e21Sny155746 			 * pr_fname[] into the four different structures
319*c2c65e21Sny155746 			 * here, regardless of how we are compiled.
320*c2c65e21Sny155746 			 */
321*c2c65e21Sny155746 			if (class == ELFCLASS32) {
322*c2c65e21Sny155746 				/* 32-bit core file, 32-bit structures */
323*c2c65e21Sny155746 				if (ntype == NT_PSINFO)
324*c2c65e21Sny155746 					fname = psinfo + 88;
325*c2c65e21Sny155746 				else	/* old: NT_PRPSINFO */
326*c2c65e21Sny155746 					fname = psinfo + 84;
327*c2c65e21Sny155746 			} else if (class == ELFCLASS64) {
328*c2c65e21Sny155746 				/* 64-bit core file, 64-bit structures */
329*c2c65e21Sny155746 				if (ntype == NT_PSINFO)
330*c2c65e21Sny155746 					fname = psinfo + 136;
331*c2c65e21Sny155746 				else	/* old: NT_PRPSINFO */
332*c2c65e21Sny155746 					fname = psinfo + 120;
333*c2c65e21Sny155746 			}
334*c2c65e21Sny155746 			EI->core_type = (ntype == NT_PRPSINFO)?
335*c2c65e21Sny155746 					EC_OLDCORE : EC_NEWCORE;
336*c2c65e21Sny155746 			(void) memcpy(EI->fname, fname, strlen(fname));
337*c2c65e21Sny155746 			free(psinfo);
338*c2c65e21Sny155746 		}
339*c2c65e21Sny155746 		if (phdr->p_type == PT_DYNAMIC) {
340*c2c65e21Sny155746 			EI->dynamic = B_TRUE;
341*c2c65e21Sny155746 		}
342*c2c65e21Sny155746 	}
343*c2c65e21Sny155746 	return (ELF_READ_OKAY);
344*c2c65e21Sny155746 }
345*c2c65e21Sny155746 
346*c2c65e21Sny155746 /*
347*c2c65e21Sny155746  * process_shdr:	Read Section Headers to attempt to get HW/SW
348*c2c65e21Sny155746  *			capabilities by looking at the SUNW_cap
349*c2c65e21Sny155746  *			section and set string in Elf_Info.
350*c2c65e21Sny155746  *			Also look for symbol tables and debug
351*c2c65e21Sny155746  *			information sections. Set the "stripped" field
352*c2c65e21Sny155746  *			in Elf_Info with corresponding flags.
353*c2c65e21Sny155746  */
354*c2c65e21Sny155746 static int
355*c2c65e21Sny155746 process_shdr(Elf_Info *EI)
356*c2c65e21Sny155746 {
357*c2c65e21Sny155746 	int 		capn, mac;
358*c2c65e21Sny155746 	int 		i, j, idx;
359*c2c65e21Sny155746 	off_t		cap_off;
360*c2c65e21Sny155746 	size_t		csize;
361*c2c65e21Sny155746 	char		*section_name;
362*c2c65e21Sny155746 	Elf_Cap 	Chdr;
363*c2c65e21Sny155746 	Elf_Shdr	*shdr = &EI_Shdr;
364*c2c65e21Sny155746 
365*c2c65e21Sny155746 
366*c2c65e21Sny155746 	csize = sizeof (Elf_Cap);
367*c2c65e21Sny155746 	mac = EI_Ehdr.e_machine;
368*c2c65e21Sny155746 
369*c2c65e21Sny155746 	/* if there are no sections, return success anyway */
370*c2c65e21Sny155746 	if (EI_Ehdr.e_shoff == 0 && EI_Ehdr.e_shnum == 0)
371*c2c65e21Sny155746 		return (ELF_READ_OKAY);
372*c2c65e21Sny155746 
373*c2c65e21Sny155746 	/* read section names from String Section */
374*c2c65e21Sny155746 	if (get_shdr(EI, EI_Ehdr.e_shstrndx) == ELF_READ_FAIL)
375*c2c65e21Sny155746 		return (ELF_READ_FAIL);
376*c2c65e21Sny155746 
377*c2c65e21Sny155746 	if ((section_name = malloc(shdr->sh_size)) == NULL)
378*c2c65e21Sny155746 		return (ELF_READ_FAIL);
379*c2c65e21Sny155746 
380*c2c65e21Sny155746 	if (pread64(EI->elffd, section_name, shdr->sh_size, shdr->sh_offset)
381*c2c65e21Sny155746 	    != shdr->sh_size)
382*c2c65e21Sny155746 		return (ELF_READ_FAIL);
383*c2c65e21Sny155746 
384*c2c65e21Sny155746 	/* read all the sections and process them */
385*c2c65e21Sny155746 	for (idx = 1, i = 0; i < EI_Ehdr.e_shnum; idx++, i++) {
386*c2c65e21Sny155746 		char *str;
387*c2c65e21Sny155746 
388*c2c65e21Sny155746 		if (get_shdr(EI, i) == ELF_READ_FAIL)
389*c2c65e21Sny155746 			return (ELF_READ_FAIL);
390*c2c65e21Sny155746 
391*c2c65e21Sny155746 		if (shdr->sh_type == SHT_NULL) {
392*c2c65e21Sny155746 			idx--;
393*c2c65e21Sny155746 			continue;
394*c2c65e21Sny155746 		}
395*c2c65e21Sny155746 
396*c2c65e21Sny155746 		cap_off = shdr->sh_offset;
397*c2c65e21Sny155746 		if (shdr->sh_type == SHT_SUNW_cap) {
398*c2c65e21Sny155746 			if (shdr->sh_size == 0 || shdr->sh_entsize == 0) {
399*c2c65e21Sny155746 				(void) fprintf(stderr, ELF_ERR_ELFCAP1,
400*c2c65e21Sny155746 				    File, EI->file);
401*c2c65e21Sny155746 				return (ELF_READ_FAIL);
402*c2c65e21Sny155746 			}
403*c2c65e21Sny155746 			capn = (shdr->sh_size / shdr->sh_entsize);
404*c2c65e21Sny155746 			for (j = 0; j < capn; j++) {
405*c2c65e21Sny155746 				/*
406*c2c65e21Sny155746 				 * read cap and xlate the values
407*c2c65e21Sny155746 				 */
408*c2c65e21Sny155746 				if (pread64(EI->elffd, &Chdr, csize, cap_off)
409*c2c65e21Sny155746 					!= csize ||
410*c2c65e21Sny155746 					file_xlatetom(ELF_T_CAP, (char *)&Chdr)
411*c2c65e21Sny155746 					    == 0) {
412*c2c65e21Sny155746 					(void) fprintf(stderr, ELF_ERR_ELFCAP2,
413*c2c65e21Sny155746 					    File, EI->file);
414*c2c65e21Sny155746 					return (ELF_READ_FAIL);
415*c2c65e21Sny155746 				}
416*c2c65e21Sny155746 
417*c2c65e21Sny155746 				if (Chdr.c_tag != CA_SUNW_NULL) {
418*c2c65e21Sny155746 					(void) cap_val2str(Chdr.c_tag,
419*c2c65e21Sny155746 						    Chdr.c_un.c_val,
420*c2c65e21Sny155746 						    EI->cap_str,
421*c2c65e21Sny155746 						    sizeof (EI->cap_str),
422*c2c65e21Sny155746 						    0, mac);
423*c2c65e21Sny155746 				}
424*c2c65e21Sny155746 				cap_off += csize;
425*c2c65e21Sny155746 			}
426*c2c65e21Sny155746 		}
427*c2c65e21Sny155746 
428*c2c65e21Sny155746 		/*
429*c2c65e21Sny155746 		 * Definition time:
430*c2c65e21Sny155746 		 *	- "not stripped" means that an executable file
431*c2c65e21Sny155746 		 *	contains a Symbol Table (.symtab)
432*c2c65e21Sny155746 		 *	- "stripped" means that an executable file
433*c2c65e21Sny155746 		 *	does not contain a Symbol Table.
434*c2c65e21Sny155746 		 * When strip -l or strip -x is run, it strips the
435*c2c65e21Sny155746 		 * debugging information (.line section name (strip -l),
436*c2c65e21Sny155746 		 * .line, .debug*, .stabs*, .dwarf* section names
437*c2c65e21Sny155746 		 * and SHT_SUNW_DEBUGSTR and SHT_SUNW_DEBUG
438*c2c65e21Sny155746 		 * section types (strip -x), however the Symbol
439*c2c65e21Sny155746 		 * Table will still be present.
440*c2c65e21Sny155746 		 * Therefore, if
441*c2c65e21Sny155746 		 *	- No Symbol Table present, then report
442*c2c65e21Sny155746 		 *		"stripped"
443*c2c65e21Sny155746 		 *	- Symbol Table present with debugging
444*c2c65e21Sny155746 		 *	information (line number or debug section names,
445*c2c65e21Sny155746 		 *	or SHT_SUNW_DEBUGSTR or SHT_SUNW_DEBUG section
446*c2c65e21Sny155746 		 *	types) then report:
447*c2c65e21Sny155746 		 *		"not stripped"
448*c2c65e21Sny155746 		 *	- Symbol Table present with no debugging
449*c2c65e21Sny155746 		 *	information (line number or debug section names,
450*c2c65e21Sny155746 		 *	or SHT_SUNW_DEBUGSTR or SHT_SUNW_DEBUG section
451*c2c65e21Sny155746 		 *	types) then report:
452*c2c65e21Sny155746 		 *		"not stripped, no debugging information
453*c2c65e21Sny155746 		 *		available"
454*c2c65e21Sny155746 		 */
455*c2c65e21Sny155746 		if ((EI->stripped & E_NOSTRIP) == E_NOSTRIP)
456*c2c65e21Sny155746 			continue;
457*c2c65e21Sny155746 
458*c2c65e21Sny155746 		if (!(EI->stripped & E_SYMTAB) &&
459*c2c65e21Sny155746 		    (shdr->sh_type == SHT_SYMTAB)) {
460*c2c65e21Sny155746 			EI->stripped |= E_SYMTAB;
461*c2c65e21Sny155746 			continue;
462*c2c65e21Sny155746 		}
463*c2c65e21Sny155746 
464*c2c65e21Sny155746 		str = &section_name[shdr->sh_name];
465*c2c65e21Sny155746 
466*c2c65e21Sny155746 		if (!(EI->stripped & E_DBGINF) &&
467*c2c65e21Sny155746 		    ((shdr->sh_type == SHT_SUNW_DEBUG) ||
468*c2c65e21Sny155746 		    (shdr->sh_type == SHT_SUNW_DEBUGSTR) ||
469*c2c65e21Sny155746 		    (is_in_list(str)))) {
470*c2c65e21Sny155746 			EI->stripped |= E_DBGINF;
471*c2c65e21Sny155746 		}
472*c2c65e21Sny155746 	}
473*c2c65e21Sny155746 	free(section_name);
474*c2c65e21Sny155746 
475*c2c65e21Sny155746 	return (ELF_READ_OKAY);
476*c2c65e21Sny155746 }
477