xref: /illumos-gate/usr/src/cmd/file/elf_read.c (revision 9b8f1941)
1c2c65e21Sny /*
2c2c65e21Sny  * CDDL HEADER START
3c2c65e21Sny  *
4c2c65e21Sny  * The contents of this file are subject to the terms of the
5c2c65e21Sny  * Common Development and Distribution License (the "License").
6c2c65e21Sny  * You may not use this file except in compliance with the License.
7c2c65e21Sny  *
8c2c65e21Sny  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9c2c65e21Sny  * or http://www.opensolaris.org/os/licensing.
10c2c65e21Sny  * See the License for the specific language governing permissions
11c2c65e21Sny  * and limitations under the License.
12c2c65e21Sny  *
13c2c65e21Sny  * When distributing Covered Code, include this CDDL HEADER in each
14c2c65e21Sny  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15c2c65e21Sny  * If applicable, add the following below this CDDL HEADER, with the
16c2c65e21Sny  * fields enclosed by brackets "[]" replaced with your own identifying
17c2c65e21Sny  * information: Portions Copyright [yyyy] [name of copyright owner]
18c2c65e21Sny  *
19c2c65e21Sny  * CDDL HEADER END
20c2c65e21Sny  */
21c2c65e21Sny /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
22b6a0e2cdSRichard Lowe /*	  All Rights Reserved	*/
23c2c65e21Sny 
24c2c65e21Sny 
25c2c65e21Sny /*	Copyright (c) 1987, 1988 Microsoft Corporation	*/
26c2c65e21Sny /*	  All Rights Reserved	*/
27c2c65e21Sny 
28c2c65e21Sny /*
29c2c65e21Sny  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
30c2c65e21Sny  * Use is subject to license terms.
31c2c65e21Sny  */
32c2c65e21Sny 
330fe5e696Sab /*
340fe5e696Sab  * ELF files can exceed 2GB in size. A standard 32-bit program
350fe5e696Sab  * like 'file' cannot read past 2GB, and will be unable to see
360fe5e696Sab  * the ELF section headers that typically are at the end of the
370fe5e696Sab  * object. The simplest solution to this problem would be to make
380fe5e696Sab  * the 'file' command a 64-bit application. However, as a matter of
390fe5e696Sab  * policy, we do not want to require this. A simple command like
400fe5e696Sab  * 'file' should not carry such a requirement, especially as we
410fe5e696Sab  * support 32-bit only hardware.
420fe5e696Sab  *
430fe5e696Sab  * An alternative solution is to build this code as 32-bit
440fe5e696Sab  * large file aware. The usual way to do this is to define a pair
450fe5e696Sab  * of preprocessor definitions:
460fe5e696Sab  *
470fe5e696Sab  *	_LARGEFILE64_SOURCE
480fe5e696Sab  *		Map standard I/O routines to their largefile aware versions.
490fe5e696Sab  *
500fe5e696Sab  *	_FILE_OFFSET_BITS=64
510fe5e696Sab  *		Map off_t to off64_t
520fe5e696Sab  *
530fe5e696Sab  * The problem with this solution is that libelf is not large file capable,
540fe5e696Sab  * and the libelf header file will prevent compilation if
550fe5e696Sab  * _FILE_OFFSET_BITS is set to 64.
560fe5e696Sab  *
570fe5e696Sab  * So, the solution used in this code is to define _LARGEFILE64_SOURCE
580fe5e696Sab  * to get access to the 64-bit APIs, not to define _FILE_OFFSET_BITS, and to
590fe5e696Sab  * use our own types in place of off_t, and size_t. We read all the file
600fe5e696Sab  * data directly using pread64(), and avoid the use of libelf for anything
610fe5e696Sab  * other than the xlate functionality.
620fe5e696Sab  */
63c2c65e21Sny #define	_LARGEFILE64_SOURCE
640fe5e696Sab #define	FILE_ELF_OFF_T	off64_t
650fe5e696Sab #define	FILE_ELF_SIZE_T	uint64_t
66c2c65e21Sny 
67c2c65e21Sny #include <ctype.h>
68c2c65e21Sny #include <unistd.h>
69c2c65e21Sny #include <fcntl.h>
70c2c65e21Sny #include <stdio.h>
71c2c65e21Sny #include <libelf.h>
72c2c65e21Sny #include <stdlib.h>
73c2c65e21Sny #include <limits.h>
74c2c65e21Sny #include <locale.h>
75c2c65e21Sny #include <string.h>
76c2c65e21Sny #include <errno.h>
77c2c65e21Sny #include <procfs.h>
78c2c65e21Sny #include <sys/param.h>
79c2c65e21Sny #include <sys/types.h>
80c2c65e21Sny #include <sys/stat.h>
81c2c65e21Sny #include <sys/elf.h>
82b6a0e2cdSRichard Lowe #include <sys/link.h>
83c2c65e21Sny #include <elfcap.h>
84c2c65e21Sny #include "file.h"
85c2c65e21Sny #include "elf_read.h"
86c2c65e21Sny 
87c2c65e21Sny extern const char *File;
88c2c65e21Sny 
89c2c65e21Sny static int get_class(void);
90c2c65e21Sny static int get_version(void);
91c2c65e21Sny static int get_format(void);
92c2c65e21Sny static int process_shdr(Elf_Info *);
93c2c65e21Sny static int process_phdr(Elf_Info *);
94c2c65e21Sny static int file_xlatetom(Elf_Type, char *);
95c2c65e21Sny static int xlatetom_nhdr(Elf_Nhdr *);
96c2c65e21Sny static int get_phdr(Elf_Info *, int);
97c2c65e21Sny static int get_shdr(Elf_Info *, int);
98c2c65e21Sny 
9997cca090Sab static Elf_Ehdr	EI_Ehdr;		/* Elf_Ehdr to be stored */
10097cca090Sab static Elf_Word	EI_Ehdr_shnum;		/* # section headers */
10197cca090Sab static Elf_Word	EI_Ehdr_phnum;		/* # program headers */
10297cca090Sab static Elf_Word	EI_Ehdr_shstrndx;	/* Index of section hdr string table */
10397cca090Sab static Elf_Shdr	EI_Shdr;		/* recent Elf_Shdr to be stored */
10497cca090Sab static Elf_Phdr	EI_Phdr;		/* recent Elf_Phdr to be stored */
105c2c65e21Sny 
106c2c65e21Sny 
107c2c65e21Sny static int
get_class(void)108c2c65e21Sny get_class(void)
109c2c65e21Sny {
110c2c65e21Sny 	return (EI_Ehdr.e_ident[EI_CLASS]);
111c2c65e21Sny }
112c2c65e21Sny 
113c2c65e21Sny static int
get_version(void)114c2c65e21Sny get_version(void)
115c2c65e21Sny {
116c2c65e21Sny 	/* do as what libelf:_elf_config() does */
117c2c65e21Sny 	return (EI_Ehdr.e_ident[EI_VERSION] ?
118c2c65e21Sny 	    EI_Ehdr.e_ident[EI_VERSION] : 1);
119c2c65e21Sny }
120c2c65e21Sny 
121c2c65e21Sny static int
get_format(void)122c2c65e21Sny get_format(void)
123c2c65e21Sny {
124c2c65e21Sny 	return (EI_Ehdr.e_ident[EI_DATA]);
125c2c65e21Sny }
126c2c65e21Sny 
127c2c65e21Sny /*
128c2c65e21Sny  * file_xlatetom:	translate different headers from file
129b6a0e2cdSRichard Lowe  *			representation to memory representaion.
130c2c65e21Sny  */
131c2c65e21Sny #define	HDRSZ 512
132c2c65e21Sny static int
file_xlatetom(Elf_Type type,char * hdr)133c2c65e21Sny file_xlatetom(Elf_Type type, char *hdr)
134c2c65e21Sny {
135c2c65e21Sny 	Elf_Data src, dst;
136c2c65e21Sny 	char *hbuf[HDRSZ];
137c2c65e21Sny 	int version, format;
138c2c65e21Sny 
139c2c65e21Sny 	version = get_version();
140c2c65e21Sny 	format = get_format();
141c2c65e21Sny 
142c2c65e21Sny 	/* will convert only these types */
143c2c65e21Sny 	if (type != ELF_T_EHDR && type != ELF_T_PHDR &&
144c2c65e21Sny 	    type != ELF_T_SHDR && type != ELF_T_WORD &&
145b6a0e2cdSRichard Lowe 	    type != ELF_T_CAP && type != ELF_T_DYN)
146c2c65e21Sny 		return (ELF_READ_FAIL);
147c2c65e21Sny 
148c2c65e21Sny 	src.d_buf = (Elf_Void *)hdr;
149c2c65e21Sny 	src.d_type = type;
150c2c65e21Sny 	src.d_version = version;
151c2c65e21Sny 
152c2c65e21Sny 	dst.d_buf = (Elf_Void *)&hbuf;
153c2c65e21Sny 	dst.d_version = EV_CURRENT;
154c2c65e21Sny 
155c2c65e21Sny 	src.d_size = elf_fsize(type, 1, version);
156c2c65e21Sny 	dst.d_size = elf_fsize(type, 1, EV_CURRENT);
157c2c65e21Sny 	if (elf_xlatetom(&dst, &src, format) == NULL)
158c2c65e21Sny 		return (ELF_READ_FAIL);
159c2c65e21Sny 
160c2c65e21Sny 	(void) memcpy(hdr, &hbuf, dst.d_size);
161c2c65e21Sny 	return (ELF_READ_OKAY);
162c2c65e21Sny }
163c2c65e21Sny 
164c2c65e21Sny /*
165c2c65e21Sny  * xlatetom_nhdr:	There is no routine to convert Note header
166b6a0e2cdSRichard Lowe  *			so we convert each field of this header.
167c2c65e21Sny  */
168c2c65e21Sny static int
xlatetom_nhdr(Elf_Nhdr * nhdr)169c2c65e21Sny xlatetom_nhdr(Elf_Nhdr *nhdr)
170c2c65e21Sny {
171c2c65e21Sny 	int r = ELF_READ_FAIL;
172c2c65e21Sny 
173c2c65e21Sny 	r |= file_xlatetom(ELF_T_WORD, (char *)&nhdr->n_namesz);
174c2c65e21Sny 	r |= file_xlatetom(ELF_T_WORD, (char *)&nhdr->n_descsz);
175c2c65e21Sny 	r |= file_xlatetom(ELF_T_WORD, (char *)&nhdr->n_type);
176c2c65e21Sny 	return (r);
177c2c65e21Sny }
178c2c65e21Sny 
179c2c65e21Sny /*
180c2c65e21Sny  * elf_read:	reads elf header, program, section headers to
181b6a0e2cdSRichard Lowe  *		collect all information needed for file(1)
182c2c65e21Sny  *		output and stores them in Elf_Info.
183c2c65e21Sny  */
184c2c65e21Sny int
elf_read(int fd,Elf_Info * EI)185c2c65e21Sny elf_read(int fd, Elf_Info *EI)
186c2c65e21Sny {
1870fe5e696Sab 	FILE_ELF_SIZE_T	size;
1880fe5e696Sab 	int		ret = 1;
189c2c65e21Sny 
190c2c65e21Sny 	Elf_Ehdr *ehdr = &EI_Ehdr;
191c2c65e21Sny 
192c2c65e21Sny 	EI->elffd = fd;
193c2c65e21Sny 	size = sizeof (Elf_Ehdr);
194c2c65e21Sny 
195c2c65e21Sny 	if (pread64(EI->elffd, (void*)ehdr, size, 0) != size)
196c2c65e21Sny 		ret = 0;
197c2c65e21Sny 
19897cca090Sab 
199c2c65e21Sny 	if (file_xlatetom(ELF_T_EHDR, (char *)ehdr) == ELF_READ_FAIL)
200c2c65e21Sny 		ret = 0;
201c2c65e21Sny 
202c2c65e21Sny 	if (EI->file == NULL)
203c2c65e21Sny 		return (ELF_READ_FAIL);
204c2c65e21Sny 
20597cca090Sab 	/*
20697cca090Sab 	 * Extended section or program indexes in use? If so, special
20797cca090Sab 	 * values in the ELF header redirect us to get the real values
20897cca090Sab 	 * from shdr[0].
20997cca090Sab 	 */
21097cca090Sab 	EI_Ehdr_shnum = EI_Ehdr.e_shnum;
21197cca090Sab 	EI_Ehdr_phnum = EI_Ehdr.e_phnum;
21297cca090Sab 	EI_Ehdr_shstrndx = EI_Ehdr.e_shstrndx;
21397cca090Sab 	if (((EI_Ehdr_shnum == 0) || (EI_Ehdr_phnum == PN_XNUM)) &&
21497cca090Sab 	    (EI_Ehdr.e_shoff != 0)) {
21553841456Sab 		if (get_shdr(EI, 0) == ELF_READ_FAIL)
21653841456Sab 			return (ELF_READ_FAIL);
21797cca090Sab 		if (EI_Ehdr_shnum == 0)
21897cca090Sab 			EI_Ehdr_shnum = EI_Shdr.sh_size;
21997cca090Sab 		if ((EI_Ehdr_phnum == PN_XNUM) && (EI_Shdr.sh_info != 0))
22097cca090Sab 			EI_Ehdr_phnum = EI_Shdr.sh_info;
22197cca090Sab 		if (EI_Ehdr_shstrndx == SHN_XINDEX)
22297cca090Sab 			EI_Ehdr_shstrndx = EI_Shdr.sh_link;
22397cca090Sab 	}
22497cca090Sab 
225c2c65e21Sny 	EI->type = ehdr->e_type;
226c2c65e21Sny 	EI->machine = ehdr->e_machine;
227c2c65e21Sny 	EI->flags = ehdr->e_flags;
228c2c65e21Sny 
229c2c65e21Sny 	if (ret == 0) {
230c2c65e21Sny 		(void) fprintf(stderr, gettext("%s: %s: can't "
231c2c65e21Sny 		    "read ELF header\n"), File, EI->file);
232c2c65e21Sny 		return (ELF_READ_FAIL);
233c2c65e21Sny 	}
234c2c65e21Sny 	if (process_phdr(EI) == ELF_READ_FAIL)
235c2c65e21Sny 		return (ELF_READ_FAIL);
236c2c65e21Sny 
237c2c65e21Sny 	/* We don't need section info for core files */
238c2c65e21Sny 	if (ehdr->e_type != ET_CORE)
239c2c65e21Sny 		if (process_shdr(EI) == ELF_READ_FAIL)
240c2c65e21Sny 			return (ELF_READ_FAIL);
241c2c65e21Sny 
242c2c65e21Sny 	return (ELF_READ_OKAY);
243c2c65e21Sny }
244c2c65e21Sny 
245c2c65e21Sny /*
246c2c65e21Sny  * get_phdr:	reads program header of specified index.
247c2c65e21Sny  */
248c2c65e21Sny static int
get_phdr(Elf_Info * EI,int inx)249c2c65e21Sny get_phdr(Elf_Info *EI, int inx)
250c2c65e21Sny {
2510fe5e696Sab 	FILE_ELF_OFF_T	off = 0;
2520fe5e696Sab 	FILE_ELF_SIZE_T	size;
253c2c65e21Sny 
25497cca090Sab 	if (inx >= EI_Ehdr_phnum)
255c2c65e21Sny 		return (ELF_READ_FAIL);
256c2c65e21Sny 
257c2c65e21Sny 	size = sizeof (Elf_Phdr);
2580fe5e696Sab 	off = (FILE_ELF_OFF_T)EI_Ehdr.e_phoff + (inx * size);
259c2c65e21Sny 	if (pread64(EI->elffd, (void *)&EI_Phdr, size, off) != size)
260c2c65e21Sny 		return (ELF_READ_FAIL);
261c2c65e21Sny 
262c2c65e21Sny 	if (file_xlatetom(ELF_T_PHDR, (char *)&EI_Phdr) == ELF_READ_FAIL)
263c2c65e21Sny 		return (ELF_READ_FAIL);
264c2c65e21Sny 
265c2c65e21Sny 	return (ELF_READ_OKAY);
266c2c65e21Sny }
267c2c65e21Sny 
268c2c65e21Sny /*
269c2c65e21Sny  * get_shdr:	reads section header of specified index.
270c2c65e21Sny  */
271c2c65e21Sny static int
get_shdr(Elf_Info * EI,int inx)272c2c65e21Sny get_shdr(Elf_Info *EI, int inx)
273c2c65e21Sny {
2740fe5e696Sab 	FILE_ELF_OFF_T	off = 0;
2750fe5e696Sab 	FILE_ELF_SIZE_T	size;
276c2c65e21Sny 
27797cca090Sab 	/*
27897cca090Sab 	 * Prevent access to non-existent section headers.
27997cca090Sab 	 *
28097cca090Sab 	 * A value of 0 for e_shoff means that there is no section header
28197cca090Sab 	 * array in the file. A value of 0 for e_shndx does not necessarily
28297cca090Sab 	 * mean this - there can still be a 1-element section header array
28397cca090Sab 	 * to support extended section or program header indexes that
28497cca090Sab 	 * exceed the 16-bit fields used in the ELF header to represent them.
28597cca090Sab 	 */
28697cca090Sab 	if ((EI_Ehdr.e_shoff == 0) || ((inx > 0) && (inx >= EI_Ehdr_shnum)))
287c2c65e21Sny 		return (ELF_READ_FAIL);
288c2c65e21Sny 
289c2c65e21Sny 	size = sizeof (Elf_Shdr);
2900fe5e696Sab 	off = (FILE_ELF_OFF_T)EI_Ehdr.e_shoff + (inx * size);
291c2c65e21Sny 
292c2c65e21Sny 	if (pread64(EI->elffd, (void *)&EI_Shdr, size, off) != size)
293c2c65e21Sny 		return (ELF_READ_FAIL);
294c2c65e21Sny 
295c2c65e21Sny 	if (file_xlatetom(ELF_T_SHDR, (char *)&EI_Shdr) == ELF_READ_FAIL)
296c2c65e21Sny 		return (ELF_READ_FAIL);
297c2c65e21Sny 
298c2c65e21Sny 	return (ELF_READ_OKAY);
299c2c65e21Sny }
300c2c65e21Sny 
301c2c65e21Sny /*
302c2c65e21Sny  * process_phdr:	Read Program Headers and see if it is a core
303c2c65e21Sny  *			file of either new or (pre-restructured /proc)
304b6a0e2cdSRichard Lowe  *			type, read the name of the file that dumped this
305c2c65e21Sny  *			core, else see if this is a dynamically linked.
306c2c65e21Sny  */
307c2c65e21Sny static int
process_phdr(Elf_Info * EI)308c2c65e21Sny process_phdr(Elf_Info *EI)
309c2c65e21Sny {
310c2c65e21Sny 	register int inx;
311c2c65e21Sny 
3120fe5e696Sab 	Elf_Nhdr	Nhdr, *nhdr;	/* note header just read */
313c2c65e21Sny 	Elf_Phdr	*phdr = &EI_Phdr;
314c2c65e21Sny 
3150fe5e696Sab 	FILE_ELF_SIZE_T	nsz, nmsz, dsz;
3160fe5e696Sab 	FILE_ELF_OFF_T	offset;
3170fe5e696Sab 	int	class;
3180fe5e696Sab 	int	ntype;
3190fe5e696Sab 	char	*psinfo, *fname;
320c2c65e21Sny 
321c2c65e21Sny 	nsz = sizeof (Elf_Nhdr);
322c2c65e21Sny 	nhdr = &Nhdr;
323c2c65e21Sny 	class = get_class();
32497cca090Sab 	for (inx = 0; inx < EI_Ehdr_phnum; inx++) {
325c2c65e21Sny 		if (get_phdr(EI, inx) == ELF_READ_FAIL)
326c2c65e21Sny 			return (ELF_READ_FAIL);
327c2c65e21Sny 
328c2c65e21Sny 		/* read the note if it is a core */
329c2c65e21Sny 		if (phdr->p_type == PT_NOTE &&
330c2c65e21Sny 		    EI_Ehdr.e_type == ET_CORE) {
331c2c65e21Sny 			/*
332c2c65e21Sny 			 * If the next segment is also a note, use it instead.
333c2c65e21Sny 			 */
334c2c65e21Sny 			if (get_phdr(EI, inx+1) == ELF_READ_FAIL)
335c2c65e21Sny 				return (ELF_READ_FAIL);
336c2c65e21Sny 			if (phdr->p_type != PT_NOTE) {
337c2c65e21Sny 				/* read the first phdr back */
338c2c65e21Sny 				if (get_phdr(EI, inx) == ELF_READ_FAIL)
339c2c65e21Sny 					return (ELF_READ_FAIL);
340c2c65e21Sny 			}
341c2c65e21Sny 			offset = phdr->p_offset;
342c2c65e21Sny 			if (pread64(EI->elffd, (void *)nhdr, nsz, offset)
34397cca090Sab 			    != nsz)
344c2c65e21Sny 				return (ELF_READ_FAIL);
345c2c65e21Sny 
346c2c65e21Sny 			/* Translate the ELF note header */
347c2c65e21Sny 			if (xlatetom_nhdr(nhdr) == ELF_READ_FAIL)
348c2c65e21Sny 				return (ELF_READ_FAIL);
349c2c65e21Sny 
350c2c65e21Sny 			ntype = nhdr->n_type;
351c2c65e21Sny 			nmsz = nhdr->n_namesz;
352c2c65e21Sny 			dsz = nhdr->n_descsz;
353c2c65e21Sny 
354c2c65e21Sny 			offset += nsz + ((nmsz + 0x03) & ~0x3);
355c2c65e21Sny 			if ((psinfo = malloc(dsz)) == NULL) {
356c2c65e21Sny 				int err = errno;
357c2c65e21Sny 				(void) fprintf(stderr, gettext("%s: malloc "
358c2c65e21Sny 				    "failed: %s\n"), File, strerror(err));
359c2c65e21Sny 				exit(1);
360c2c65e21Sny 			}
361c2c65e21Sny 			if (pread64(EI->elffd, psinfo, dsz, offset) != dsz)
362c2c65e21Sny 				return (ELF_READ_FAIL);
363c2c65e21Sny 			/*
364c2c65e21Sny 			 * We want to print the string contained
365c2c65e21Sny 			 * in psinfo->pr_fname[], where 'psinfo'
366c2c65e21Sny 			 * is either an old NT_PRPSINFO structure
367c2c65e21Sny 			 * or a new NT_PSINFO structure.
368c2c65e21Sny 			 *
369c2c65e21Sny 			 * Old core files have only type NT_PRPSINFO.
370c2c65e21Sny 			 * New core files have type NT_PSINFO.
371c2c65e21Sny 			 *
372c2c65e21Sny 			 * These structures are also different by
373c2c65e21Sny 			 * virtue of being contained in a core file
374c2c65e21Sny 			 * of either 32-bit or 64-bit type.
375c2c65e21Sny 			 *
376c2c65e21Sny 			 * To further complicate matters, we ourself
377c2c65e21Sny 			 * might be compiled either 32-bit or 64-bit.
378c2c65e21Sny 			 *
379c2c65e21Sny 			 * For these reason, we just *know* the offsets of
380c2c65e21Sny 			 * pr_fname[] into the four different structures
381c2c65e21Sny 			 * here, regardless of how we are compiled.
382c2c65e21Sny 			 */
383c2c65e21Sny 			if (class == ELFCLASS32) {
384c2c65e21Sny 				/* 32-bit core file, 32-bit structures */
385c2c65e21Sny 				if (ntype == NT_PSINFO)
386c2c65e21Sny 					fname = psinfo + 88;
387c2c65e21Sny 				else	/* old: NT_PRPSINFO */
388c2c65e21Sny 					fname = psinfo + 84;
389c2c65e21Sny 			} else if (class == ELFCLASS64) {
390c2c65e21Sny 				/* 64-bit core file, 64-bit structures */
391c2c65e21Sny 				if (ntype == NT_PSINFO)
392c2c65e21Sny 					fname = psinfo + 136;
393c2c65e21Sny 				else	/* old: NT_PRPSINFO */
394c2c65e21Sny 					fname = psinfo + 120;
395c2c65e21Sny 			}
396c2c65e21Sny 			EI->core_type = (ntype == NT_PRPSINFO)?
39797cca090Sab 			    EC_OLDCORE : EC_NEWCORE;
398c2c65e21Sny 			(void) memcpy(EI->fname, fname, strlen(fname));
399c2c65e21Sny 			free(psinfo);
400c2c65e21Sny 		}
401c2c65e21Sny 		if (phdr->p_type == PT_DYNAMIC) {
402c2c65e21Sny 			EI->dynamic = B_TRUE;
403c2c65e21Sny 		}
404c2c65e21Sny 	}
405c2c65e21Sny 	return (ELF_READ_OKAY);
406c2c65e21Sny }
407c2c65e21Sny 
408c2c65e21Sny /*
409c2c65e21Sny  * process_shdr:	Read Section Headers to attempt to get HW/SW
410c2c65e21Sny  *			capabilities by looking at the SUNW_cap
411c2c65e21Sny  *			section and set string in Elf_Info.
412c2c65e21Sny  *			Also look for symbol tables and debug
413c2c65e21Sny  *			information sections. Set the "stripped" field
414c2c65e21Sny  *			in Elf_Info with corresponding flags.
415c2c65e21Sny  */
416c2c65e21Sny static int
process_shdr(Elf_Info * EI)417c2c65e21Sny process_shdr(Elf_Info *EI)
418c2c65e21Sny {
419b6a0e2cdSRichard Lowe 	int		mac;
420b6a0e2cdSRichard Lowe 	int		i, idx;
42191600d91SRichard Lowe 	char		*strtab;
42291600d91SRichard Lowe 	size_t		strtab_sz;
423b6a0e2cdSRichard Lowe 	uint64_t	j;
424c2c65e21Sny 	Elf_Shdr	*shdr = &EI_Shdr;
425c2c65e21Sny 
426c2c65e21Sny 	mac = EI_Ehdr.e_machine;
427c2c65e21Sny 
428c2c65e21Sny 	/* if there are no sections, return success anyway */
42997cca090Sab 	if (EI_Ehdr.e_shoff == 0 && EI_Ehdr_shnum == 0)
430c2c65e21Sny 		return (ELF_READ_OKAY);
431c2c65e21Sny 
432c2c65e21Sny 	/* read section names from String Section */
43397cca090Sab 	if (get_shdr(EI, EI_Ehdr_shstrndx) == ELF_READ_FAIL)
434c2c65e21Sny 		return (ELF_READ_FAIL);
435c2c65e21Sny 
43691600d91SRichard Lowe 	if ((strtab = malloc(shdr->sh_size)) == NULL)
437c2c65e21Sny 		return (ELF_READ_FAIL);
438c2c65e21Sny 
43991600d91SRichard Lowe 	if (pread64(EI->elffd, strtab, shdr->sh_size, shdr->sh_offset)
440c2c65e21Sny 	    != shdr->sh_size)
441c2c65e21Sny 		return (ELF_READ_FAIL);
442c2c65e21Sny 
44391600d91SRichard Lowe 	strtab_sz = shdr->sh_size;
44491600d91SRichard Lowe 
445c2c65e21Sny 	/* read all the sections and process them */
44697cca090Sab 	for (idx = 1, i = 0; i < EI_Ehdr_shnum; idx++, i++) {
44791600d91SRichard Lowe 		char *shnam;
448c2c65e21Sny 
449c2c65e21Sny 		if (get_shdr(EI, i) == ELF_READ_FAIL)
450c2c65e21Sny 			return (ELF_READ_FAIL);
451c2c65e21Sny 
452c2c65e21Sny 		if (shdr->sh_type == SHT_NULL) {
453c2c65e21Sny 			idx--;
454c2c65e21Sny 			continue;
455c2c65e21Sny 		}
456c2c65e21Sny 
457c2c65e21Sny 		if (shdr->sh_type == SHT_SUNW_cap) {
458b6a0e2cdSRichard Lowe 			char		capstr[128];
459b6a0e2cdSRichard Lowe 			Elf_Cap		Chdr;
460b6a0e2cdSRichard Lowe 			FILE_ELF_OFF_T	cap_off;
461b6a0e2cdSRichard Lowe 			FILE_ELF_SIZE_T	csize;
462b6a0e2cdSRichard Lowe 			uint64_t capn;
463b6a0e2cdSRichard Lowe 
464b6a0e2cdSRichard Lowe 			cap_off = shdr->sh_offset;
465b6a0e2cdSRichard Lowe 			csize = sizeof (Elf_Cap);
4663d6a6d03SRichard Lowe 
467c2c65e21Sny 			if (shdr->sh_size == 0 || shdr->sh_entsize == 0) {
468c2c65e21Sny 				(void) fprintf(stderr, ELF_ERR_ELFCAP1,
469c2c65e21Sny 				    File, EI->file);
470c2c65e21Sny 				return (ELF_READ_FAIL);
471c2c65e21Sny 			}
472c2c65e21Sny 			capn = (shdr->sh_size / shdr->sh_entsize);
473c2c65e21Sny 			for (j = 0; j < capn; j++) {
474c2c65e21Sny 				/*
475c2c65e21Sny 				 * read cap and xlate the values
476c2c65e21Sny 				 */
477b6a0e2cdSRichard Lowe 				if ((pread64(EI->elffd, &Chdr, csize, cap_off)
478b6a0e2cdSRichard Lowe 				    != csize) ||
47997cca090Sab 				    file_xlatetom(ELF_T_CAP, (char *)&Chdr)
48097cca090Sab 				    == 0) {
481c2c65e21Sny 					(void) fprintf(stderr, ELF_ERR_ELFCAP2,
482c2c65e21Sny 					    File, EI->file);
483c2c65e21Sny 					return (ELF_READ_FAIL);
484c2c65e21Sny 				}
485c2c65e21Sny 
486c2c65e21Sny 				cap_off += csize;
4873d6a6d03SRichard Lowe 
4883d6a6d03SRichard Lowe 				/*
4893d6a6d03SRichard Lowe 				 * Each capatibility group is terminated with
4903d6a6d03SRichard Lowe 				 * CA_SUNW_NULL.  Groups other than the first
4913d6a6d03SRichard Lowe 				 * represent symbol capabilities, and aren't
4923d6a6d03SRichard Lowe 				 * interesting here.
4933d6a6d03SRichard Lowe 				 */
4943d6a6d03SRichard Lowe 				if (Chdr.c_tag == CA_SUNW_NULL)
4953d6a6d03SRichard Lowe 					break;
4963d6a6d03SRichard Lowe 
4973d6a6d03SRichard Lowe 				(void) elfcap_tag_to_str(ELFCAP_STYLE_UC,
4983d6a6d03SRichard Lowe 				    Chdr.c_tag, Chdr.c_un.c_val, capstr,
4993d6a6d03SRichard Lowe 				    sizeof (capstr), ELFCAP_FMT_SNGSPACE,
5003d6a6d03SRichard Lowe 				    mac);
5013d6a6d03SRichard Lowe 
5023d6a6d03SRichard Lowe 				if ((*EI->cap_str != '\0') && (*capstr != '\0'))
5033d6a6d03SRichard Lowe 					(void) strlcat(EI->cap_str, " ",
5043d6a6d03SRichard Lowe 					    sizeof (EI->cap_str));
5053d6a6d03SRichard Lowe 
5063d6a6d03SRichard Lowe 				(void) strlcat(EI->cap_str, capstr,
5073d6a6d03SRichard Lowe 				    sizeof (EI->cap_str));
508c2c65e21Sny 			}
509b6a0e2cdSRichard Lowe 		} else if (shdr->sh_type == SHT_DYNAMIC) {
510b6a0e2cdSRichard Lowe 			Elf_Dyn dyn;
511b6a0e2cdSRichard Lowe 			FILE_ELF_SIZE_T dsize;
512b6a0e2cdSRichard Lowe 			FILE_ELF_OFF_T doff;
513*9b8f1941SRichard Lowe 			uint64_t dynn;
514b6a0e2cdSRichard Lowe 
515b6a0e2cdSRichard Lowe 			doff = shdr->sh_offset;
516b6a0e2cdSRichard Lowe 			dsize = sizeof (Elf_Dyn);
517b6a0e2cdSRichard Lowe 
518b6a0e2cdSRichard Lowe 			if (shdr->sh_size == 0 || shdr->sh_entsize == 0) {
519b6a0e2cdSRichard Lowe 				(void) fprintf(stderr, ELF_ERR_DYNAMIC1,
520b6a0e2cdSRichard Lowe 				    File, EI->file);
521b6a0e2cdSRichard Lowe 				return (ELF_READ_FAIL);
522b6a0e2cdSRichard Lowe 			}
523b6a0e2cdSRichard Lowe 
524b6a0e2cdSRichard Lowe 			dynn = (shdr->sh_size / shdr->sh_entsize);
525b6a0e2cdSRichard Lowe 			for (j = 0; j < dynn; j++) {
526b6a0e2cdSRichard Lowe 				if (pread64(EI->elffd, &dyn, dsize, doff)
527b6a0e2cdSRichard Lowe 				    != dsize ||
528b6a0e2cdSRichard Lowe 				    file_xlatetom(ELF_T_DYN, (char *)&dyn)
529b6a0e2cdSRichard Lowe 				    == 0) {
530b6a0e2cdSRichard Lowe 					(void) fprintf(stderr, ELF_ERR_DYNAMIC2,
531b6a0e2cdSRichard Lowe 					    File, EI->file);
532b6a0e2cdSRichard Lowe 					return (ELF_READ_FAIL);
533b6a0e2cdSRichard Lowe 				}
534b6a0e2cdSRichard Lowe 
535b6a0e2cdSRichard Lowe 				doff += dsize;
536b6a0e2cdSRichard Lowe 
537b6a0e2cdSRichard Lowe 				if ((dyn.d_tag == DT_SUNW_KMOD) &&
538b6a0e2cdSRichard Lowe 				    (dyn.d_un.d_val == 1)) {
539b6a0e2cdSRichard Lowe 					EI->kmod = B_TRUE;
540b6a0e2cdSRichard Lowe 				}
541b6a0e2cdSRichard Lowe 			}
542c2c65e21Sny 		}
543c2c65e21Sny 
544c2c65e21Sny 		/*
545c2c65e21Sny 		 * Definition time:
546c2c65e21Sny 		 *	- "not stripped" means that an executable file
547c2c65e21Sny 		 *	contains a Symbol Table (.symtab)
548c2c65e21Sny 		 *	- "stripped" means that an executable file
549c2c65e21Sny 		 *	does not contain a Symbol Table.
550c2c65e21Sny 		 * When strip -l or strip -x is run, it strips the
551c2c65e21Sny 		 * debugging information (.line section name (strip -l),
552c2c65e21Sny 		 * .line, .debug*, .stabs*, .dwarf* section names
553c2c65e21Sny 		 * and SHT_SUNW_DEBUGSTR and SHT_SUNW_DEBUG
554c2c65e21Sny 		 * section types (strip -x), however the Symbol
555c2c65e21Sny 		 * Table will still be present.
556c2c65e21Sny 		 * Therefore, if
557c2c65e21Sny 		 *	- No Symbol Table present, then report
558c2c65e21Sny 		 *		"stripped"
559c2c65e21Sny 		 *	- Symbol Table present with debugging
560c2c65e21Sny 		 *	information (line number or debug section names,
561c2c65e21Sny 		 *	or SHT_SUNW_DEBUGSTR or SHT_SUNW_DEBUG section
562c2c65e21Sny 		 *	types) then report:
563c2c65e21Sny 		 *		"not stripped"
564c2c65e21Sny 		 *	- Symbol Table present with no debugging
565c2c65e21Sny 		 *	information (line number or debug section names,
566c2c65e21Sny 		 *	or SHT_SUNW_DEBUGSTR or SHT_SUNW_DEBUG section
567c2c65e21Sny 		 *	types) then report:
568c2c65e21Sny 		 *		"not stripped, no debugging information
569c2c65e21Sny 		 *		available"
570c2c65e21Sny 		 */
571c2c65e21Sny 		if ((EI->stripped & E_NOSTRIP) == E_NOSTRIP)
572c2c65e21Sny 			continue;
573c2c65e21Sny 
574c2c65e21Sny 		if (!(EI->stripped & E_SYMTAB) &&
575c2c65e21Sny 		    (shdr->sh_type == SHT_SYMTAB)) {
576c2c65e21Sny 			EI->stripped |= E_SYMTAB;
577c2c65e21Sny 			continue;
578c2c65e21Sny 		}
579c2c65e21Sny 
58091600d91SRichard Lowe 		if (shdr->sh_name >= strtab_sz)
58191600d91SRichard Lowe 			shnam = NULL;
58291600d91SRichard Lowe 		else
58391600d91SRichard Lowe 			shnam = &strtab[shdr->sh_name];
584c2c65e21Sny 
585c2c65e21Sny 		if (!(EI->stripped & E_DBGINF) &&
586c2c65e21Sny 		    ((shdr->sh_type == SHT_SUNW_DEBUG) ||
587c2c65e21Sny 		    (shdr->sh_type == SHT_SUNW_DEBUGSTR) ||
58891600d91SRichard Lowe 		    (shnam != NULL && is_in_list(shnam)))) {
589c2c65e21Sny 			EI->stripped |= E_DBGINF;
590c2c65e21Sny 		}
591c2c65e21Sny 	}
59291600d91SRichard Lowe 	free(strtab);
593c2c65e21Sny 
594c2c65e21Sny 	return (ELF_READ_OKAY);
595c2c65e21Sny }
596