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