xref: /titanic_51/usr/src/cmd/file/elf_read.c (revision 538414560648ad8b2b92897b55592cda64706375)
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 */
6997cca090Sab196087 static Elf_Word	EI_Ehdr_shnum;		/* # section headers */
7097cca090Sab196087 static Elf_Word	EI_Ehdr_phnum;		/* # program headers */
7197cca090Sab196087 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 
16797cca090Sab196087 
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 
17497cca090Sab196087 	/*
17597cca090Sab196087 	 * Extended section or program indexes in use? If so, special
17697cca090Sab196087 	 * values in the ELF header redirect us to get the real values
17797cca090Sab196087 	 * from shdr[0].
17897cca090Sab196087 	 */
17997cca090Sab196087 	EI_Ehdr_shnum = EI_Ehdr.e_shnum;
18097cca090Sab196087 	EI_Ehdr_phnum = EI_Ehdr.e_phnum;
18197cca090Sab196087 	EI_Ehdr_shstrndx = EI_Ehdr.e_shstrndx;
18297cca090Sab196087 	if (((EI_Ehdr_shnum == 0) || (EI_Ehdr_phnum == PN_XNUM)) &&
18397cca090Sab196087 	    (EI_Ehdr.e_shoff != 0)) {
184*53841456Sab196087 		if (get_shdr(EI, 0) == ELF_READ_FAIL)
185*53841456Sab196087 			return (ELF_READ_FAIL);
18697cca090Sab196087 		if (EI_Ehdr_shnum == 0)
18797cca090Sab196087 			EI_Ehdr_shnum = EI_Shdr.sh_size;
18897cca090Sab196087 		if ((EI_Ehdr_phnum == PN_XNUM) && (EI_Shdr.sh_info != 0))
18997cca090Sab196087 			EI_Ehdr_phnum = EI_Shdr.sh_info;
19097cca090Sab196087 		if (EI_Ehdr_shstrndx == SHN_XINDEX)
19197cca090Sab196087 			EI_Ehdr_shstrndx = EI_Shdr.sh_link;
19297cca090Sab196087 	}
19397cca090Sab196087 
194c2c65e21Sny155746 	EI->type = ehdr->e_type;
195c2c65e21Sny155746 	EI->machine = ehdr->e_machine;
196c2c65e21Sny155746 	EI->flags = ehdr->e_flags;
197c2c65e21Sny155746 
198c2c65e21Sny155746 	if (ret == 0) {
199c2c65e21Sny155746 		(void) fprintf(stderr, gettext("%s: %s: can't "
200c2c65e21Sny155746 		    "read ELF header\n"), File, EI->file);
201c2c65e21Sny155746 		return (ELF_READ_FAIL);
202c2c65e21Sny155746 	}
203c2c65e21Sny155746 	if (process_phdr(EI) == ELF_READ_FAIL)
204c2c65e21Sny155746 		return (ELF_READ_FAIL);
205c2c65e21Sny155746 
206c2c65e21Sny155746 	/* We don't need section info for core files */
207c2c65e21Sny155746 	if (ehdr->e_type != ET_CORE)
208c2c65e21Sny155746 		if (process_shdr(EI) == ELF_READ_FAIL)
209c2c65e21Sny155746 			return (ELF_READ_FAIL);
210c2c65e21Sny155746 
211c2c65e21Sny155746 	return (ELF_READ_OKAY);
212c2c65e21Sny155746 }
213c2c65e21Sny155746 
214c2c65e21Sny155746 /*
215c2c65e21Sny155746  * get_phdr:	reads program header of specified index.
216c2c65e21Sny155746  */
217c2c65e21Sny155746 static int
218c2c65e21Sny155746 get_phdr(Elf_Info *EI, int inx)
219c2c65e21Sny155746 {
220c2c65e21Sny155746 	off_t off = 0;
221c2c65e21Sny155746 	size_t size;
222c2c65e21Sny155746 
22397cca090Sab196087 	if (inx >= EI_Ehdr_phnum)
224c2c65e21Sny155746 		return (ELF_READ_FAIL);
225c2c65e21Sny155746 
226c2c65e21Sny155746 	size = sizeof (Elf_Phdr);
22797cca090Sab196087 	off = (off_t)EI_Ehdr.e_phoff + (inx * size);
228c2c65e21Sny155746 	if (pread64(EI->elffd, (void *)&EI_Phdr, size, off) != size)
229c2c65e21Sny155746 		return (ELF_READ_FAIL);
230c2c65e21Sny155746 
231c2c65e21Sny155746 	if (file_xlatetom(ELF_T_PHDR, (char *)&EI_Phdr) == ELF_READ_FAIL)
232c2c65e21Sny155746 		return (ELF_READ_FAIL);
233c2c65e21Sny155746 
234c2c65e21Sny155746 	return (ELF_READ_OKAY);
235c2c65e21Sny155746 }
236c2c65e21Sny155746 
237c2c65e21Sny155746 /*
238c2c65e21Sny155746  * get_shdr:	reads section header of specified index.
239c2c65e21Sny155746  */
240c2c65e21Sny155746 static int
241c2c65e21Sny155746 get_shdr(Elf_Info *EI, int inx)
242c2c65e21Sny155746 {
243c2c65e21Sny155746 	off_t off = 0;
244c2c65e21Sny155746 	size_t size;
245c2c65e21Sny155746 
24697cca090Sab196087 	/*
24797cca090Sab196087 	 * Prevent access to non-existent section headers.
24897cca090Sab196087 	 *
24997cca090Sab196087 	 * A value of 0 for e_shoff means that there is no section header
25097cca090Sab196087 	 * array in the file. A value of 0 for e_shndx does not necessarily
25197cca090Sab196087 	 * mean this - there can still be a 1-element section header array
25297cca090Sab196087 	 * to support extended section or program header indexes that
25397cca090Sab196087 	 * exceed the 16-bit fields used in the ELF header to represent them.
25497cca090Sab196087 	 */
25597cca090Sab196087 	if ((EI_Ehdr.e_shoff == 0) || ((inx > 0) && (inx >= EI_Ehdr_shnum)))
256c2c65e21Sny155746 		return (ELF_READ_FAIL);
257c2c65e21Sny155746 
258c2c65e21Sny155746 	size = sizeof (Elf_Shdr);
25997cca090Sab196087 	off = (off_t)EI_Ehdr.e_shoff + (inx * size);
260c2c65e21Sny155746 
261c2c65e21Sny155746 	if (pread64(EI->elffd, (void *)&EI_Shdr, size, off) != size)
262c2c65e21Sny155746 		return (ELF_READ_FAIL);
263c2c65e21Sny155746 
264c2c65e21Sny155746 	if (file_xlatetom(ELF_T_SHDR, (char *)&EI_Shdr) == ELF_READ_FAIL)
265c2c65e21Sny155746 		return (ELF_READ_FAIL);
266c2c65e21Sny155746 
267c2c65e21Sny155746 	return (ELF_READ_OKAY);
268c2c65e21Sny155746 }
269c2c65e21Sny155746 
270c2c65e21Sny155746 /*
271c2c65e21Sny155746  * process_phdr:	Read Program Headers and see if it is a core
272c2c65e21Sny155746  *			file of either new or (pre-restructured /proc)
273c2c65e21Sny155746  * 			type, read the name of the file that dumped this
274c2c65e21Sny155746  *			core, else see if this is a dynamically linked.
275c2c65e21Sny155746  */
276c2c65e21Sny155746 static int
277c2c65e21Sny155746 process_phdr(Elf_Info *EI)
278c2c65e21Sny155746 {
279c2c65e21Sny155746 	register int inx;
280c2c65e21Sny155746 
281c2c65e21Sny155746 	Elf_Nhdr Nhdr, *nhdr;	/* note header just read */
282c2c65e21Sny155746 	Elf_Phdr	*phdr = &EI_Phdr;
283c2c65e21Sny155746 
284c2c65e21Sny155746 	int class;
285c2c65e21Sny155746 	int ntype;
286c2c65e21Sny155746 	size_t nsz, nmsz, dsz;
287c2c65e21Sny155746 	off_t offset;
288c2c65e21Sny155746 	char *psinfo, *fname;
289c2c65e21Sny155746 
290c2c65e21Sny155746 	nsz = sizeof (Elf_Nhdr);
291c2c65e21Sny155746 	nhdr = &Nhdr;
292c2c65e21Sny155746 	class = get_class();
29397cca090Sab196087 	for (inx = 0; inx < EI_Ehdr_phnum; inx++) {
294c2c65e21Sny155746 		if (get_phdr(EI, inx) == ELF_READ_FAIL)
295c2c65e21Sny155746 			return (ELF_READ_FAIL);
296c2c65e21Sny155746 
297c2c65e21Sny155746 		/* read the note if it is a core */
298c2c65e21Sny155746 		if (phdr->p_type == PT_NOTE &&
299c2c65e21Sny155746 		    EI_Ehdr.e_type == ET_CORE) {
300c2c65e21Sny155746 			/*
301c2c65e21Sny155746 			 * If the next segment is also a note, use it instead.
302c2c65e21Sny155746 			 */
303c2c65e21Sny155746 			if (get_phdr(EI, inx+1) == ELF_READ_FAIL)
304c2c65e21Sny155746 				return (ELF_READ_FAIL);
305c2c65e21Sny155746 			if (phdr->p_type != PT_NOTE) {
306c2c65e21Sny155746 				/* read the first phdr back */
307c2c65e21Sny155746 				if (get_phdr(EI, inx) == ELF_READ_FAIL)
308c2c65e21Sny155746 					return (ELF_READ_FAIL);
309c2c65e21Sny155746 			}
310c2c65e21Sny155746 			offset = phdr->p_offset;
311c2c65e21Sny155746 			if (pread64(EI->elffd, (void *)nhdr, nsz, offset)
312c2c65e21Sny155746 			    != nsz)
313c2c65e21Sny155746 				return (ELF_READ_FAIL);
314c2c65e21Sny155746 
315c2c65e21Sny155746 			/* Translate the ELF note header */
316c2c65e21Sny155746 			if (xlatetom_nhdr(nhdr) == ELF_READ_FAIL)
317c2c65e21Sny155746 				return (ELF_READ_FAIL);
318c2c65e21Sny155746 
319c2c65e21Sny155746 			ntype = nhdr->n_type;
320c2c65e21Sny155746 			nmsz = nhdr->n_namesz;
321c2c65e21Sny155746 			dsz = nhdr->n_descsz;
322c2c65e21Sny155746 
323c2c65e21Sny155746 			offset += nsz + ((nmsz + 0x03) & ~0x3);
324c2c65e21Sny155746 			if ((psinfo = malloc(dsz)) == NULL) {
325c2c65e21Sny155746 				int err = errno;
326c2c65e21Sny155746 				(void) fprintf(stderr, gettext("%s: malloc "
327c2c65e21Sny155746 				    "failed: %s\n"), File, strerror(err));
328c2c65e21Sny155746 				exit(1);
329c2c65e21Sny155746 			}
330c2c65e21Sny155746 			if (pread64(EI->elffd, psinfo, dsz, offset) != dsz)
331c2c65e21Sny155746 				return (ELF_READ_FAIL);
332c2c65e21Sny155746 			/*
333c2c65e21Sny155746 			 * We want to print the string contained
334c2c65e21Sny155746 			 * in psinfo->pr_fname[], where 'psinfo'
335c2c65e21Sny155746 			 * is either an old NT_PRPSINFO structure
336c2c65e21Sny155746 			 * or a new NT_PSINFO structure.
337c2c65e21Sny155746 			 *
338c2c65e21Sny155746 			 * Old core files have only type NT_PRPSINFO.
339c2c65e21Sny155746 			 * New core files have type NT_PSINFO.
340c2c65e21Sny155746 			 *
341c2c65e21Sny155746 			 * These structures are also different by
342c2c65e21Sny155746 			 * virtue of being contained in a core file
343c2c65e21Sny155746 			 * of either 32-bit or 64-bit type.
344c2c65e21Sny155746 			 *
345c2c65e21Sny155746 			 * To further complicate matters, we ourself
346c2c65e21Sny155746 			 * might be compiled either 32-bit or 64-bit.
347c2c65e21Sny155746 			 *
348c2c65e21Sny155746 			 * For these reason, we just *know* the offsets of
349c2c65e21Sny155746 			 * pr_fname[] into the four different structures
350c2c65e21Sny155746 			 * here, regardless of how we are compiled.
351c2c65e21Sny155746 			 */
352c2c65e21Sny155746 			if (class == ELFCLASS32) {
353c2c65e21Sny155746 				/* 32-bit core file, 32-bit structures */
354c2c65e21Sny155746 				if (ntype == NT_PSINFO)
355c2c65e21Sny155746 					fname = psinfo + 88;
356c2c65e21Sny155746 				else	/* old: NT_PRPSINFO */
357c2c65e21Sny155746 					fname = psinfo + 84;
358c2c65e21Sny155746 			} else if (class == ELFCLASS64) {
359c2c65e21Sny155746 				/* 64-bit core file, 64-bit structures */
360c2c65e21Sny155746 				if (ntype == NT_PSINFO)
361c2c65e21Sny155746 					fname = psinfo + 136;
362c2c65e21Sny155746 				else	/* old: NT_PRPSINFO */
363c2c65e21Sny155746 					fname = psinfo + 120;
364c2c65e21Sny155746 			}
365c2c65e21Sny155746 			EI->core_type = (ntype == NT_PRPSINFO)?
366c2c65e21Sny155746 			    EC_OLDCORE : EC_NEWCORE;
367c2c65e21Sny155746 			(void) memcpy(EI->fname, fname, strlen(fname));
368c2c65e21Sny155746 			free(psinfo);
369c2c65e21Sny155746 		}
370c2c65e21Sny155746 		if (phdr->p_type == PT_DYNAMIC) {
371c2c65e21Sny155746 			EI->dynamic = B_TRUE;
372c2c65e21Sny155746 		}
373c2c65e21Sny155746 	}
374c2c65e21Sny155746 	return (ELF_READ_OKAY);
375c2c65e21Sny155746 }
376c2c65e21Sny155746 
377c2c65e21Sny155746 /*
378c2c65e21Sny155746  * process_shdr:	Read Section Headers to attempt to get HW/SW
379c2c65e21Sny155746  *			capabilities by looking at the SUNW_cap
380c2c65e21Sny155746  *			section and set string in Elf_Info.
381c2c65e21Sny155746  *			Also look for symbol tables and debug
382c2c65e21Sny155746  *			information sections. Set the "stripped" field
383c2c65e21Sny155746  *			in Elf_Info with corresponding flags.
384c2c65e21Sny155746  */
385c2c65e21Sny155746 static int
386c2c65e21Sny155746 process_shdr(Elf_Info *EI)
387c2c65e21Sny155746 {
388c2c65e21Sny155746 	int 		capn, mac;
389c2c65e21Sny155746 	int 		i, j, idx;
390c2c65e21Sny155746 	off_t		cap_off;
391c2c65e21Sny155746 	size_t		csize;
392c2c65e21Sny155746 	char		*section_name;
393c2c65e21Sny155746 	Elf_Cap 	Chdr;
394c2c65e21Sny155746 	Elf_Shdr	*shdr = &EI_Shdr;
395c2c65e21Sny155746 
396c2c65e21Sny155746 
397c2c65e21Sny155746 	csize = sizeof (Elf_Cap);
398c2c65e21Sny155746 	mac = EI_Ehdr.e_machine;
399c2c65e21Sny155746 
400c2c65e21Sny155746 	/* if there are no sections, return success anyway */
40197cca090Sab196087 	if (EI_Ehdr.e_shoff == 0 && EI_Ehdr_shnum == 0)
402c2c65e21Sny155746 		return (ELF_READ_OKAY);
403c2c65e21Sny155746 
404c2c65e21Sny155746 	/* read section names from String Section */
40597cca090Sab196087 	if (get_shdr(EI, EI_Ehdr_shstrndx) == ELF_READ_FAIL)
406c2c65e21Sny155746 		return (ELF_READ_FAIL);
407c2c65e21Sny155746 
408c2c65e21Sny155746 	if ((section_name = malloc(shdr->sh_size)) == NULL)
409c2c65e21Sny155746 		return (ELF_READ_FAIL);
410c2c65e21Sny155746 
411c2c65e21Sny155746 	if (pread64(EI->elffd, section_name, shdr->sh_size, shdr->sh_offset)
412c2c65e21Sny155746 	    != shdr->sh_size)
413c2c65e21Sny155746 		return (ELF_READ_FAIL);
414c2c65e21Sny155746 
415c2c65e21Sny155746 	/* read all the sections and process them */
41697cca090Sab196087 	for (idx = 1, i = 0; i < EI_Ehdr_shnum; idx++, i++) {
417c2c65e21Sny155746 		char *str;
418c2c65e21Sny155746 
419c2c65e21Sny155746 		if (get_shdr(EI, i) == ELF_READ_FAIL)
420c2c65e21Sny155746 			return (ELF_READ_FAIL);
421c2c65e21Sny155746 
422c2c65e21Sny155746 		if (shdr->sh_type == SHT_NULL) {
423c2c65e21Sny155746 			idx--;
424c2c65e21Sny155746 			continue;
425c2c65e21Sny155746 		}
426c2c65e21Sny155746 
427c2c65e21Sny155746 		cap_off = shdr->sh_offset;
428c2c65e21Sny155746 		if (shdr->sh_type == SHT_SUNW_cap) {
429c2c65e21Sny155746 			if (shdr->sh_size == 0 || shdr->sh_entsize == 0) {
430c2c65e21Sny155746 				(void) fprintf(stderr, ELF_ERR_ELFCAP1,
431c2c65e21Sny155746 				    File, EI->file);
432c2c65e21Sny155746 				return (ELF_READ_FAIL);
433c2c65e21Sny155746 			}
434c2c65e21Sny155746 			capn = (shdr->sh_size / shdr->sh_entsize);
435c2c65e21Sny155746 			for (j = 0; j < capn; j++) {
436c2c65e21Sny155746 				/*
437c2c65e21Sny155746 				 * read cap and xlate the values
438c2c65e21Sny155746 				 */
439c2c65e21Sny155746 				if (pread64(EI->elffd, &Chdr, csize, cap_off)
440c2c65e21Sny155746 				    != csize ||
441c2c65e21Sny155746 				    file_xlatetom(ELF_T_CAP, (char *)&Chdr)
442c2c65e21Sny155746 				    == 0) {
443c2c65e21Sny155746 					(void) fprintf(stderr, ELF_ERR_ELFCAP2,
444c2c65e21Sny155746 					    File, EI->file);
445c2c65e21Sny155746 					return (ELF_READ_FAIL);
446c2c65e21Sny155746 				}
447c2c65e21Sny155746 
448c2c65e21Sny155746 				if (Chdr.c_tag != CA_SUNW_NULL) {
449c2c65e21Sny155746 					(void) cap_val2str(Chdr.c_tag,
45097cca090Sab196087 					    Chdr.c_un.c_val, EI->cap_str,
45197cca090Sab196087 					    sizeof (EI->cap_str), 0, mac);
452c2c65e21Sny155746 				}
453c2c65e21Sny155746 				cap_off += csize;
454c2c65e21Sny155746 			}
455c2c65e21Sny155746 		}
456c2c65e21Sny155746 
457c2c65e21Sny155746 		/*
458c2c65e21Sny155746 		 * Definition time:
459c2c65e21Sny155746 		 *	- "not stripped" means that an executable file
460c2c65e21Sny155746 		 *	contains a Symbol Table (.symtab)
461c2c65e21Sny155746 		 *	- "stripped" means that an executable file
462c2c65e21Sny155746 		 *	does not contain a Symbol Table.
463c2c65e21Sny155746 		 * When strip -l or strip -x is run, it strips the
464c2c65e21Sny155746 		 * debugging information (.line section name (strip -l),
465c2c65e21Sny155746 		 * .line, .debug*, .stabs*, .dwarf* section names
466c2c65e21Sny155746 		 * and SHT_SUNW_DEBUGSTR and SHT_SUNW_DEBUG
467c2c65e21Sny155746 		 * section types (strip -x), however the Symbol
468c2c65e21Sny155746 		 * Table will still be present.
469c2c65e21Sny155746 		 * Therefore, if
470c2c65e21Sny155746 		 *	- No Symbol Table present, then report
471c2c65e21Sny155746 		 *		"stripped"
472c2c65e21Sny155746 		 *	- Symbol Table present with debugging
473c2c65e21Sny155746 		 *	information (line number or debug section names,
474c2c65e21Sny155746 		 *	or SHT_SUNW_DEBUGSTR or SHT_SUNW_DEBUG section
475c2c65e21Sny155746 		 *	types) then report:
476c2c65e21Sny155746 		 *		"not stripped"
477c2c65e21Sny155746 		 *	- Symbol Table present with no debugging
478c2c65e21Sny155746 		 *	information (line number or debug section names,
479c2c65e21Sny155746 		 *	or SHT_SUNW_DEBUGSTR or SHT_SUNW_DEBUG section
480c2c65e21Sny155746 		 *	types) then report:
481c2c65e21Sny155746 		 *		"not stripped, no debugging information
482c2c65e21Sny155746 		 *		available"
483c2c65e21Sny155746 		 */
484c2c65e21Sny155746 		if ((EI->stripped & E_NOSTRIP) == E_NOSTRIP)
485c2c65e21Sny155746 			continue;
486c2c65e21Sny155746 
487c2c65e21Sny155746 		if (!(EI->stripped & E_SYMTAB) &&
488c2c65e21Sny155746 		    (shdr->sh_type == SHT_SYMTAB)) {
489c2c65e21Sny155746 			EI->stripped |= E_SYMTAB;
490c2c65e21Sny155746 			continue;
491c2c65e21Sny155746 		}
492c2c65e21Sny155746 
493c2c65e21Sny155746 		str = &section_name[shdr->sh_name];
494c2c65e21Sny155746 
495c2c65e21Sny155746 		if (!(EI->stripped & E_DBGINF) &&
496c2c65e21Sny155746 		    ((shdr->sh_type == SHT_SUNW_DEBUG) ||
497c2c65e21Sny155746 		    (shdr->sh_type == SHT_SUNW_DEBUGSTR) ||
498c2c65e21Sny155746 		    (is_in_list(str)))) {
499c2c65e21Sny155746 			EI->stripped |= E_DBGINF;
500c2c65e21Sny155746 		}
501c2c65e21Sny155746 	}
502c2c65e21Sny155746 	free(section_name);
503c2c65e21Sny155746 
504c2c65e21Sny155746 	return (ELF_READ_OKAY);
505c2c65e21Sny155746 }
506