xref: /titanic_41/usr/src/cmd/file/elf_read.c (revision 3b0698c8dbcbe033637b9dbd0034b0deb50426c9)
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 
330fe5e696Sab196087 /*
340fe5e696Sab196087  * ELF files can exceed 2GB in size. A standard 32-bit program
350fe5e696Sab196087  * like 'file' cannot read past 2GB, and will be unable to see
360fe5e696Sab196087  * the ELF section headers that typically are at the end of the
370fe5e696Sab196087  * object. The simplest solution to this problem would be to make
380fe5e696Sab196087  * the 'file' command a 64-bit application. However, as a matter of
390fe5e696Sab196087  * policy, we do not want to require this. A simple command like
400fe5e696Sab196087  * 'file' should not carry such a requirement, especially as we
410fe5e696Sab196087  * support 32-bit only hardware.
420fe5e696Sab196087  *
430fe5e696Sab196087  * An alternative solution is to build this code as 32-bit
440fe5e696Sab196087  * large file aware. The usual way to do this is to define a pair
450fe5e696Sab196087  * of preprocessor definitions:
460fe5e696Sab196087  *
470fe5e696Sab196087  *	_LARGEFILE64_SOURCE
480fe5e696Sab196087  *		Map standard I/O routines to their largefile aware versions.
490fe5e696Sab196087  *
500fe5e696Sab196087  *	_FILE_OFFSET_BITS=64
510fe5e696Sab196087  *		Map off_t to off64_t
520fe5e696Sab196087  *
530fe5e696Sab196087  * The problem with this solution is that libelf is not large file capable,
540fe5e696Sab196087  * and the libelf header file will prevent compilation if
550fe5e696Sab196087  * _FILE_OFFSET_BITS is set to 64.
560fe5e696Sab196087  *
570fe5e696Sab196087  * So, the solution used in this code is to define _LARGEFILE64_SOURCE
580fe5e696Sab196087  * to get access to the 64-bit APIs, not to define _FILE_OFFSET_BITS, and to
590fe5e696Sab196087  * use our own types in place of off_t, and size_t. We read all the file
600fe5e696Sab196087  * data directly using pread64(), and avoid the use of libelf for anything
610fe5e696Sab196087  * other than the xlate functionality.
620fe5e696Sab196087  */
63c2c65e21Sny155746 #define	_LARGEFILE64_SOURCE
640fe5e696Sab196087 #define	FILE_ELF_OFF_T	off64_t
650fe5e696Sab196087 #define	FILE_ELF_SIZE_T	uint64_t
66c2c65e21Sny155746 
67c2c65e21Sny155746 #include <ctype.h>
68c2c65e21Sny155746 #include <unistd.h>
69c2c65e21Sny155746 #include <fcntl.h>
70c2c65e21Sny155746 #include <stdio.h>
71c2c65e21Sny155746 #include <libelf.h>
72c2c65e21Sny155746 #include <stdlib.h>
73c2c65e21Sny155746 #include <limits.h>
74c2c65e21Sny155746 #include <locale.h>
75c2c65e21Sny155746 #include <string.h>
76c2c65e21Sny155746 #include <errno.h>
77c2c65e21Sny155746 #include <procfs.h>
78c2c65e21Sny155746 #include <sys/param.h>
79c2c65e21Sny155746 #include <sys/types.h>
80c2c65e21Sny155746 #include <sys/stat.h>
81c2c65e21Sny155746 #include <sys/elf.h>
82c2c65e21Sny155746 #include <elfcap.h>
83c2c65e21Sny155746 #include "file.h"
84c2c65e21Sny155746 #include "elf_read.h"
85c2c65e21Sny155746 
86c2c65e21Sny155746 extern const char *File;
87c2c65e21Sny155746 
88c2c65e21Sny155746 static int get_class(void);
89c2c65e21Sny155746 static int get_version(void);
90c2c65e21Sny155746 static int get_format(void);
91c2c65e21Sny155746 static int process_shdr(Elf_Info *);
92c2c65e21Sny155746 static int process_phdr(Elf_Info *);
93c2c65e21Sny155746 static int file_xlatetom(Elf_Type, char *);
94c2c65e21Sny155746 static int xlatetom_nhdr(Elf_Nhdr *);
95c2c65e21Sny155746 static int get_phdr(Elf_Info *, int);
96c2c65e21Sny155746 static int get_shdr(Elf_Info *, int);
97c2c65e21Sny155746 
98c2c65e21Sny155746 static Elf_Ehdr	EI_Ehdr;		/* Elf_Ehdr to be stored */
9997cca090Sab196087 static Elf_Word	EI_Ehdr_shnum;		/* # section headers */
10097cca090Sab196087 static Elf_Word	EI_Ehdr_phnum;		/* # program headers */
10197cca090Sab196087 static Elf_Word	EI_Ehdr_shstrndx;	/* Index of section hdr string table */
102c2c65e21Sny155746 static Elf_Shdr	EI_Shdr;		/* recent Elf_Shdr to be stored */
103c2c65e21Sny155746 static Elf_Phdr	EI_Phdr;		/* recent Elf_Phdr to be stored */
104c2c65e21Sny155746 
105c2c65e21Sny155746 
106c2c65e21Sny155746 static int
get_class(void)107c2c65e21Sny155746 get_class(void)
108c2c65e21Sny155746 {
109c2c65e21Sny155746 	return (EI_Ehdr.e_ident[EI_CLASS]);
110c2c65e21Sny155746 }
111c2c65e21Sny155746 
112c2c65e21Sny155746 static int
get_version(void)113c2c65e21Sny155746 get_version(void)
114c2c65e21Sny155746 {
115c2c65e21Sny155746 	/* do as what libelf:_elf_config() does */
116c2c65e21Sny155746 	return (EI_Ehdr.e_ident[EI_VERSION] ?
117c2c65e21Sny155746 	    EI_Ehdr.e_ident[EI_VERSION] : 1);
118c2c65e21Sny155746 }
119c2c65e21Sny155746 
120c2c65e21Sny155746 static int
get_format(void)121c2c65e21Sny155746 get_format(void)
122c2c65e21Sny155746 {
123c2c65e21Sny155746 	return (EI_Ehdr.e_ident[EI_DATA]);
124c2c65e21Sny155746 }
125c2c65e21Sny155746 
126c2c65e21Sny155746 /*
127c2c65e21Sny155746  * file_xlatetom:	translate different headers from file
128c2c65e21Sny155746  * 			representation to memory representaion.
129c2c65e21Sny155746  */
130c2c65e21Sny155746 #define	HDRSZ 512
131c2c65e21Sny155746 static int
file_xlatetom(Elf_Type type,char * hdr)132c2c65e21Sny155746 file_xlatetom(Elf_Type type, char *hdr)
133c2c65e21Sny155746 {
134c2c65e21Sny155746 	Elf_Data src, dst;
135c2c65e21Sny155746 	char *hbuf[HDRSZ];
136c2c65e21Sny155746 	int version, format;
137c2c65e21Sny155746 
138c2c65e21Sny155746 	version = get_version();
139c2c65e21Sny155746 	format = get_format();
140c2c65e21Sny155746 
141c2c65e21Sny155746 	/* will convert only these types */
142c2c65e21Sny155746 	if (type != ELF_T_EHDR && type != ELF_T_PHDR &&
143c2c65e21Sny155746 	    type != ELF_T_SHDR && type != ELF_T_WORD &&
144c2c65e21Sny155746 	    type != ELF_T_CAP)
145c2c65e21Sny155746 		return (ELF_READ_FAIL);
146c2c65e21Sny155746 
147c2c65e21Sny155746 	src.d_buf = (Elf_Void *)hdr;
148c2c65e21Sny155746 	src.d_type = type;
149c2c65e21Sny155746 	src.d_version = version;
150c2c65e21Sny155746 
151c2c65e21Sny155746 	dst.d_buf = (Elf_Void *)&hbuf;
152c2c65e21Sny155746 	dst.d_version = EV_CURRENT;
153c2c65e21Sny155746 
154c2c65e21Sny155746 	src.d_size = elf_fsize(type, 1, version);
155c2c65e21Sny155746 	dst.d_size = elf_fsize(type, 1, EV_CURRENT);
156c2c65e21Sny155746 	if (elf_xlatetom(&dst, &src, format) == NULL)
157c2c65e21Sny155746 		return (ELF_READ_FAIL);
158c2c65e21Sny155746 
159c2c65e21Sny155746 	(void) memcpy(hdr, &hbuf, dst.d_size);
160c2c65e21Sny155746 	return (ELF_READ_OKAY);
161c2c65e21Sny155746 }
162c2c65e21Sny155746 
163c2c65e21Sny155746 /*
164c2c65e21Sny155746  * xlatetom_nhdr:	There is no routine to convert Note header
165c2c65e21Sny155746  * 			so we convert each field of this header.
166c2c65e21Sny155746  */
167c2c65e21Sny155746 static int
xlatetom_nhdr(Elf_Nhdr * nhdr)168c2c65e21Sny155746 xlatetom_nhdr(Elf_Nhdr *nhdr)
169c2c65e21Sny155746 {
170c2c65e21Sny155746 	int r = ELF_READ_FAIL;
171c2c65e21Sny155746 
172c2c65e21Sny155746 	r |= file_xlatetom(ELF_T_WORD, (char *)&nhdr->n_namesz);
173c2c65e21Sny155746 	r |= file_xlatetom(ELF_T_WORD, (char *)&nhdr->n_descsz);
174c2c65e21Sny155746 	r |= file_xlatetom(ELF_T_WORD, (char *)&nhdr->n_type);
175c2c65e21Sny155746 	return (r);
176c2c65e21Sny155746 }
177c2c65e21Sny155746 
178c2c65e21Sny155746 /*
179c2c65e21Sny155746  * elf_read:	reads elf header, program, section headers to
180c2c65e21Sny155746  * 		collect all information needed for file(1)
181c2c65e21Sny155746  *		output and stores them in Elf_Info.
182c2c65e21Sny155746  */
183c2c65e21Sny155746 int
elf_read(int fd,Elf_Info * EI)184c2c65e21Sny155746 elf_read(int fd, Elf_Info *EI)
185c2c65e21Sny155746 {
1860fe5e696Sab196087 	FILE_ELF_SIZE_T	size;
187c2c65e21Sny155746 	int		ret = 1;
188c2c65e21Sny155746 
189c2c65e21Sny155746 	Elf_Ehdr *ehdr = &EI_Ehdr;
190c2c65e21Sny155746 
191c2c65e21Sny155746 	EI->elffd = fd;
192c2c65e21Sny155746 	size = sizeof (Elf_Ehdr);
193c2c65e21Sny155746 
194c2c65e21Sny155746 	if (pread64(EI->elffd, (void*)ehdr, size, 0) != size)
195c2c65e21Sny155746 		ret = 0;
196c2c65e21Sny155746 
19797cca090Sab196087 
198c2c65e21Sny155746 	if (file_xlatetom(ELF_T_EHDR, (char *)ehdr) == ELF_READ_FAIL)
199c2c65e21Sny155746 		ret = 0;
200c2c65e21Sny155746 
201c2c65e21Sny155746 	if (EI->file == NULL)
202c2c65e21Sny155746 		return (ELF_READ_FAIL);
203c2c65e21Sny155746 
20497cca090Sab196087 	/*
20597cca090Sab196087 	 * Extended section or program indexes in use? If so, special
20697cca090Sab196087 	 * values in the ELF header redirect us to get the real values
20797cca090Sab196087 	 * from shdr[0].
20897cca090Sab196087 	 */
20997cca090Sab196087 	EI_Ehdr_shnum = EI_Ehdr.e_shnum;
21097cca090Sab196087 	EI_Ehdr_phnum = EI_Ehdr.e_phnum;
21197cca090Sab196087 	EI_Ehdr_shstrndx = EI_Ehdr.e_shstrndx;
21297cca090Sab196087 	if (((EI_Ehdr_shnum == 0) || (EI_Ehdr_phnum == PN_XNUM)) &&
21397cca090Sab196087 	    (EI_Ehdr.e_shoff != 0)) {
21453841456Sab196087 		if (get_shdr(EI, 0) == ELF_READ_FAIL)
21553841456Sab196087 			return (ELF_READ_FAIL);
21697cca090Sab196087 		if (EI_Ehdr_shnum == 0)
21797cca090Sab196087 			EI_Ehdr_shnum = EI_Shdr.sh_size;
21897cca090Sab196087 		if ((EI_Ehdr_phnum == PN_XNUM) && (EI_Shdr.sh_info != 0))
21997cca090Sab196087 			EI_Ehdr_phnum = EI_Shdr.sh_info;
22097cca090Sab196087 		if (EI_Ehdr_shstrndx == SHN_XINDEX)
22197cca090Sab196087 			EI_Ehdr_shstrndx = EI_Shdr.sh_link;
22297cca090Sab196087 	}
22397cca090Sab196087 
224c2c65e21Sny155746 	EI->type = ehdr->e_type;
225c2c65e21Sny155746 	EI->machine = ehdr->e_machine;
226c2c65e21Sny155746 	EI->flags = ehdr->e_flags;
227c2c65e21Sny155746 
228c2c65e21Sny155746 	if (ret == 0) {
229c2c65e21Sny155746 		(void) fprintf(stderr, gettext("%s: %s: can't "
230c2c65e21Sny155746 		    "read ELF header\n"), File, EI->file);
231c2c65e21Sny155746 		return (ELF_READ_FAIL);
232c2c65e21Sny155746 	}
233c2c65e21Sny155746 	if (process_phdr(EI) == ELF_READ_FAIL)
234c2c65e21Sny155746 		return (ELF_READ_FAIL);
235c2c65e21Sny155746 
236c2c65e21Sny155746 	/* We don't need section info for core files */
237c2c65e21Sny155746 	if (ehdr->e_type != ET_CORE)
238c2c65e21Sny155746 		if (process_shdr(EI) == ELF_READ_FAIL)
239c2c65e21Sny155746 			return (ELF_READ_FAIL);
240c2c65e21Sny155746 
241c2c65e21Sny155746 	return (ELF_READ_OKAY);
242c2c65e21Sny155746 }
243c2c65e21Sny155746 
244c2c65e21Sny155746 /*
245c2c65e21Sny155746  * get_phdr:	reads program header of specified index.
246c2c65e21Sny155746  */
247c2c65e21Sny155746 static int
get_phdr(Elf_Info * EI,int inx)248c2c65e21Sny155746 get_phdr(Elf_Info *EI, int inx)
249c2c65e21Sny155746 {
2500fe5e696Sab196087 	FILE_ELF_OFF_T	off = 0;
2510fe5e696Sab196087 	FILE_ELF_SIZE_T	size;
252c2c65e21Sny155746 
25397cca090Sab196087 	if (inx >= EI_Ehdr_phnum)
254c2c65e21Sny155746 		return (ELF_READ_FAIL);
255c2c65e21Sny155746 
256c2c65e21Sny155746 	size = sizeof (Elf_Phdr);
2570fe5e696Sab196087 	off = (FILE_ELF_OFF_T)EI_Ehdr.e_phoff + (inx * size);
258c2c65e21Sny155746 	if (pread64(EI->elffd, (void *)&EI_Phdr, size, off) != size)
259c2c65e21Sny155746 		return (ELF_READ_FAIL);
260c2c65e21Sny155746 
261c2c65e21Sny155746 	if (file_xlatetom(ELF_T_PHDR, (char *)&EI_Phdr) == ELF_READ_FAIL)
262c2c65e21Sny155746 		return (ELF_READ_FAIL);
263c2c65e21Sny155746 
264c2c65e21Sny155746 	return (ELF_READ_OKAY);
265c2c65e21Sny155746 }
266c2c65e21Sny155746 
267c2c65e21Sny155746 /*
268c2c65e21Sny155746  * get_shdr:	reads section header of specified index.
269c2c65e21Sny155746  */
270c2c65e21Sny155746 static int
get_shdr(Elf_Info * EI,int inx)271c2c65e21Sny155746 get_shdr(Elf_Info *EI, int inx)
272c2c65e21Sny155746 {
2730fe5e696Sab196087 	FILE_ELF_OFF_T	off = 0;
2740fe5e696Sab196087 	FILE_ELF_SIZE_T	size;
275c2c65e21Sny155746 
27697cca090Sab196087 	/*
27797cca090Sab196087 	 * Prevent access to non-existent section headers.
27897cca090Sab196087 	 *
27997cca090Sab196087 	 * A value of 0 for e_shoff means that there is no section header
28097cca090Sab196087 	 * array in the file. A value of 0 for e_shndx does not necessarily
28197cca090Sab196087 	 * mean this - there can still be a 1-element section header array
28297cca090Sab196087 	 * to support extended section or program header indexes that
28397cca090Sab196087 	 * exceed the 16-bit fields used in the ELF header to represent them.
28497cca090Sab196087 	 */
28597cca090Sab196087 	if ((EI_Ehdr.e_shoff == 0) || ((inx > 0) && (inx >= EI_Ehdr_shnum)))
286c2c65e21Sny155746 		return (ELF_READ_FAIL);
287c2c65e21Sny155746 
288c2c65e21Sny155746 	size = sizeof (Elf_Shdr);
2890fe5e696Sab196087 	off = (FILE_ELF_OFF_T)EI_Ehdr.e_shoff + (inx * size);
290c2c65e21Sny155746 
291c2c65e21Sny155746 	if (pread64(EI->elffd, (void *)&EI_Shdr, size, off) != size)
292c2c65e21Sny155746 		return (ELF_READ_FAIL);
293c2c65e21Sny155746 
294c2c65e21Sny155746 	if (file_xlatetom(ELF_T_SHDR, (char *)&EI_Shdr) == ELF_READ_FAIL)
295c2c65e21Sny155746 		return (ELF_READ_FAIL);
296c2c65e21Sny155746 
297c2c65e21Sny155746 	return (ELF_READ_OKAY);
298c2c65e21Sny155746 }
299c2c65e21Sny155746 
300c2c65e21Sny155746 /*
301c2c65e21Sny155746  * process_phdr:	Read Program Headers and see if it is a core
302c2c65e21Sny155746  *			file of either new or (pre-restructured /proc)
303c2c65e21Sny155746  * 			type, read the name of the file that dumped this
304c2c65e21Sny155746  *			core, else see if this is a dynamically linked.
305c2c65e21Sny155746  */
306c2c65e21Sny155746 static int
process_phdr(Elf_Info * EI)307c2c65e21Sny155746 process_phdr(Elf_Info *EI)
308c2c65e21Sny155746 {
309c2c65e21Sny155746 	register int inx;
310c2c65e21Sny155746 
311c2c65e21Sny155746 	Elf_Nhdr	Nhdr, *nhdr;	/* note header just read */
312c2c65e21Sny155746 	Elf_Phdr	*phdr = &EI_Phdr;
313c2c65e21Sny155746 
3140fe5e696Sab196087 	FILE_ELF_SIZE_T	nsz, nmsz, dsz;
3150fe5e696Sab196087 	FILE_ELF_OFF_T	offset;
316c2c65e21Sny155746 	int	class;
317c2c65e21Sny155746 	int	ntype;
318c2c65e21Sny155746 	char	*psinfo, *fname;
319c2c65e21Sny155746 
320c2c65e21Sny155746 	nsz = sizeof (Elf_Nhdr);
321c2c65e21Sny155746 	nhdr = &Nhdr;
322c2c65e21Sny155746 	class = get_class();
32397cca090Sab196087 	for (inx = 0; inx < EI_Ehdr_phnum; inx++) {
324c2c65e21Sny155746 		if (get_phdr(EI, inx) == ELF_READ_FAIL)
325c2c65e21Sny155746 			return (ELF_READ_FAIL);
326c2c65e21Sny155746 
327c2c65e21Sny155746 		/* read the note if it is a core */
328c2c65e21Sny155746 		if (phdr->p_type == PT_NOTE &&
329c2c65e21Sny155746 		    EI_Ehdr.e_type == ET_CORE) {
330c2c65e21Sny155746 			/*
331c2c65e21Sny155746 			 * If the next segment is also a note, use it instead.
332c2c65e21Sny155746 			 */
333c2c65e21Sny155746 			if (get_phdr(EI, inx+1) == ELF_READ_FAIL)
334c2c65e21Sny155746 				return (ELF_READ_FAIL);
335c2c65e21Sny155746 			if (phdr->p_type != PT_NOTE) {
336c2c65e21Sny155746 				/* read the first phdr back */
337c2c65e21Sny155746 				if (get_phdr(EI, inx) == ELF_READ_FAIL)
338c2c65e21Sny155746 					return (ELF_READ_FAIL);
339c2c65e21Sny155746 			}
340c2c65e21Sny155746 			offset = phdr->p_offset;
341c2c65e21Sny155746 			if (pread64(EI->elffd, (void *)nhdr, nsz, offset)
342c2c65e21Sny155746 			    != nsz)
343c2c65e21Sny155746 				return (ELF_READ_FAIL);
344c2c65e21Sny155746 
345c2c65e21Sny155746 			/* Translate the ELF note header */
346c2c65e21Sny155746 			if (xlatetom_nhdr(nhdr) == ELF_READ_FAIL)
347c2c65e21Sny155746 				return (ELF_READ_FAIL);
348c2c65e21Sny155746 
349c2c65e21Sny155746 			ntype = nhdr->n_type;
350c2c65e21Sny155746 			nmsz = nhdr->n_namesz;
351c2c65e21Sny155746 			dsz = nhdr->n_descsz;
352c2c65e21Sny155746 
353c2c65e21Sny155746 			offset += nsz + ((nmsz + 0x03) & ~0x3);
354c2c65e21Sny155746 			if ((psinfo = malloc(dsz)) == NULL) {
355c2c65e21Sny155746 				int err = errno;
356c2c65e21Sny155746 				(void) fprintf(stderr, gettext("%s: malloc "
357c2c65e21Sny155746 				    "failed: %s\n"), File, strerror(err));
358c2c65e21Sny155746 				exit(1);
359c2c65e21Sny155746 			}
360c2c65e21Sny155746 			if (pread64(EI->elffd, psinfo, dsz, offset) != dsz)
361c2c65e21Sny155746 				return (ELF_READ_FAIL);
362c2c65e21Sny155746 			/*
363c2c65e21Sny155746 			 * We want to print the string contained
364c2c65e21Sny155746 			 * in psinfo->pr_fname[], where 'psinfo'
365c2c65e21Sny155746 			 * is either an old NT_PRPSINFO structure
366c2c65e21Sny155746 			 * or a new NT_PSINFO structure.
367c2c65e21Sny155746 			 *
368c2c65e21Sny155746 			 * Old core files have only type NT_PRPSINFO.
369c2c65e21Sny155746 			 * New core files have type NT_PSINFO.
370c2c65e21Sny155746 			 *
371c2c65e21Sny155746 			 * These structures are also different by
372c2c65e21Sny155746 			 * virtue of being contained in a core file
373c2c65e21Sny155746 			 * of either 32-bit or 64-bit type.
374c2c65e21Sny155746 			 *
375c2c65e21Sny155746 			 * To further complicate matters, we ourself
376c2c65e21Sny155746 			 * might be compiled either 32-bit or 64-bit.
377c2c65e21Sny155746 			 *
378c2c65e21Sny155746 			 * For these reason, we just *know* the offsets of
379c2c65e21Sny155746 			 * pr_fname[] into the four different structures
380c2c65e21Sny155746 			 * here, regardless of how we are compiled.
381c2c65e21Sny155746 			 */
382c2c65e21Sny155746 			if (class == ELFCLASS32) {
383c2c65e21Sny155746 				/* 32-bit core file, 32-bit structures */
384c2c65e21Sny155746 				if (ntype == NT_PSINFO)
385c2c65e21Sny155746 					fname = psinfo + 88;
386c2c65e21Sny155746 				else	/* old: NT_PRPSINFO */
387c2c65e21Sny155746 					fname = psinfo + 84;
388c2c65e21Sny155746 			} else if (class == ELFCLASS64) {
389c2c65e21Sny155746 				/* 64-bit core file, 64-bit structures */
390c2c65e21Sny155746 				if (ntype == NT_PSINFO)
391c2c65e21Sny155746 					fname = psinfo + 136;
392c2c65e21Sny155746 				else	/* old: NT_PRPSINFO */
393c2c65e21Sny155746 					fname = psinfo + 120;
394c2c65e21Sny155746 			}
395c2c65e21Sny155746 			EI->core_type = (ntype == NT_PRPSINFO)?
396c2c65e21Sny155746 			    EC_OLDCORE : EC_NEWCORE;
397c2c65e21Sny155746 			(void) memcpy(EI->fname, fname, strlen(fname));
398c2c65e21Sny155746 			free(psinfo);
399c2c65e21Sny155746 		}
400c2c65e21Sny155746 		if (phdr->p_type == PT_DYNAMIC) {
401c2c65e21Sny155746 			EI->dynamic = B_TRUE;
402c2c65e21Sny155746 		}
403c2c65e21Sny155746 	}
404c2c65e21Sny155746 	return (ELF_READ_OKAY);
405c2c65e21Sny155746 }
406c2c65e21Sny155746 
407c2c65e21Sny155746 /*
408c2c65e21Sny155746  * process_shdr:	Read Section Headers to attempt to get HW/SW
409c2c65e21Sny155746  *			capabilities by looking at the SUNW_cap
410c2c65e21Sny155746  *			section and set string in Elf_Info.
411c2c65e21Sny155746  *			Also look for symbol tables and debug
412c2c65e21Sny155746  *			information sections. Set the "stripped" field
413c2c65e21Sny155746  *			in Elf_Info with corresponding flags.
414c2c65e21Sny155746  */
415c2c65e21Sny155746 static int
process_shdr(Elf_Info * EI)416c2c65e21Sny155746 process_shdr(Elf_Info *EI)
417c2c65e21Sny155746 {
418c2c65e21Sny155746 	int 		capn, mac;
419c2c65e21Sny155746 	int 		i, j, idx;
4200fe5e696Sab196087 	FILE_ELF_OFF_T	cap_off;
4210fe5e696Sab196087 	FILE_ELF_SIZE_T	csize;
422*3b0698c8SRichard Lowe 	char		*strtab;
423*3b0698c8SRichard Lowe 	size_t		strtab_sz;
424c2c65e21Sny155746 	Elf_Cap 	Chdr;
425c2c65e21Sny155746 	Elf_Shdr	*shdr = &EI_Shdr;
426c2c65e21Sny155746 
427c2c65e21Sny155746 
428c2c65e21Sny155746 	csize = sizeof (Elf_Cap);
429c2c65e21Sny155746 	mac = EI_Ehdr.e_machine;
430c2c65e21Sny155746 
431c2c65e21Sny155746 	/* if there are no sections, return success anyway */
43297cca090Sab196087 	if (EI_Ehdr.e_shoff == 0 && EI_Ehdr_shnum == 0)
433c2c65e21Sny155746 		return (ELF_READ_OKAY);
434c2c65e21Sny155746 
435c2c65e21Sny155746 	/* read section names from String Section */
43697cca090Sab196087 	if (get_shdr(EI, EI_Ehdr_shstrndx) == ELF_READ_FAIL)
437c2c65e21Sny155746 		return (ELF_READ_FAIL);
438c2c65e21Sny155746 
439*3b0698c8SRichard Lowe 	if ((strtab = malloc(shdr->sh_size)) == NULL)
440c2c65e21Sny155746 		return (ELF_READ_FAIL);
441c2c65e21Sny155746 
442*3b0698c8SRichard Lowe 	if (pread64(EI->elffd, strtab, shdr->sh_size, shdr->sh_offset)
443c2c65e21Sny155746 	    != shdr->sh_size)
444c2c65e21Sny155746 		return (ELF_READ_FAIL);
445c2c65e21Sny155746 
446*3b0698c8SRichard Lowe 	strtab_sz = shdr->sh_size;
447*3b0698c8SRichard Lowe 
448c2c65e21Sny155746 	/* read all the sections and process them */
44997cca090Sab196087 	for (idx = 1, i = 0; i < EI_Ehdr_shnum; idx++, i++) {
450*3b0698c8SRichard Lowe 		char *shnam;
451c2c65e21Sny155746 
452c2c65e21Sny155746 		if (get_shdr(EI, i) == ELF_READ_FAIL)
453c2c65e21Sny155746 			return (ELF_READ_FAIL);
454c2c65e21Sny155746 
455c2c65e21Sny155746 		if (shdr->sh_type == SHT_NULL) {
456c2c65e21Sny155746 			idx--;
457c2c65e21Sny155746 			continue;
458c2c65e21Sny155746 		}
459c2c65e21Sny155746 
460c2c65e21Sny155746 		cap_off = shdr->sh_offset;
461c2c65e21Sny155746 		if (shdr->sh_type == SHT_SUNW_cap) {
4623d6a6d03SRichard Lowe 			char capstr[128];
4633d6a6d03SRichard Lowe 
464c2c65e21Sny155746 			if (shdr->sh_size == 0 || shdr->sh_entsize == 0) {
465c2c65e21Sny155746 				(void) fprintf(stderr, ELF_ERR_ELFCAP1,
466c2c65e21Sny155746 				    File, EI->file);
467c2c65e21Sny155746 				return (ELF_READ_FAIL);
468c2c65e21Sny155746 			}
469c2c65e21Sny155746 			capn = (shdr->sh_size / shdr->sh_entsize);
470c2c65e21Sny155746 			for (j = 0; j < capn; j++) {
471c2c65e21Sny155746 				/*
472c2c65e21Sny155746 				 * read cap and xlate the values
473c2c65e21Sny155746 				 */
474c2c65e21Sny155746 				if (pread64(EI->elffd, &Chdr, csize, cap_off)
475c2c65e21Sny155746 				    != csize ||
476c2c65e21Sny155746 				    file_xlatetom(ELF_T_CAP, (char *)&Chdr)
477c2c65e21Sny155746 				    == 0) {
478c2c65e21Sny155746 					(void) fprintf(stderr, ELF_ERR_ELFCAP2,
479c2c65e21Sny155746 					    File, EI->file);
480c2c65e21Sny155746 					return (ELF_READ_FAIL);
481c2c65e21Sny155746 				}
482c2c65e21Sny155746 
483c2c65e21Sny155746 				cap_off += csize;
4843d6a6d03SRichard Lowe 
4853d6a6d03SRichard Lowe 				/*
4863d6a6d03SRichard Lowe 				 * Each capatibility group is terminated with
4873d6a6d03SRichard Lowe 				 * CA_SUNW_NULL.  Groups other than the first
4883d6a6d03SRichard Lowe 				 * represent symbol capabilities, and aren't
4893d6a6d03SRichard Lowe 				 * interesting here.
4903d6a6d03SRichard Lowe 				 */
4913d6a6d03SRichard Lowe 				if (Chdr.c_tag == CA_SUNW_NULL)
4923d6a6d03SRichard Lowe 					break;
4933d6a6d03SRichard Lowe 
4943d6a6d03SRichard Lowe 				(void) elfcap_tag_to_str(ELFCAP_STYLE_UC,
4953d6a6d03SRichard Lowe 				    Chdr.c_tag, Chdr.c_un.c_val, capstr,
4963d6a6d03SRichard Lowe 				    sizeof (capstr), ELFCAP_FMT_SNGSPACE,
4973d6a6d03SRichard Lowe 				    mac);
4983d6a6d03SRichard Lowe 
4993d6a6d03SRichard Lowe 				if ((*EI->cap_str != '\0') && (*capstr != '\0'))
5003d6a6d03SRichard Lowe 					(void) strlcat(EI->cap_str, " ",
5013d6a6d03SRichard Lowe 					    sizeof (EI->cap_str));
5023d6a6d03SRichard Lowe 
5033d6a6d03SRichard Lowe 				(void) strlcat(EI->cap_str, capstr,
5043d6a6d03SRichard Lowe 				    sizeof (EI->cap_str));
505c2c65e21Sny155746 			}
506c2c65e21Sny155746 		}
507c2c65e21Sny155746 
508c2c65e21Sny155746 		/*
509c2c65e21Sny155746 		 * Definition time:
510c2c65e21Sny155746 		 *	- "not stripped" means that an executable file
511c2c65e21Sny155746 		 *	contains a Symbol Table (.symtab)
512c2c65e21Sny155746 		 *	- "stripped" means that an executable file
513c2c65e21Sny155746 		 *	does not contain a Symbol Table.
514c2c65e21Sny155746 		 * When strip -l or strip -x is run, it strips the
515c2c65e21Sny155746 		 * debugging information (.line section name (strip -l),
516c2c65e21Sny155746 		 * .line, .debug*, .stabs*, .dwarf* section names
517c2c65e21Sny155746 		 * and SHT_SUNW_DEBUGSTR and SHT_SUNW_DEBUG
518c2c65e21Sny155746 		 * section types (strip -x), however the Symbol
519c2c65e21Sny155746 		 * Table will still be present.
520c2c65e21Sny155746 		 * Therefore, if
521c2c65e21Sny155746 		 *	- No Symbol Table present, then report
522c2c65e21Sny155746 		 *		"stripped"
523c2c65e21Sny155746 		 *	- Symbol Table present with debugging
524c2c65e21Sny155746 		 *	information (line number or debug section names,
525c2c65e21Sny155746 		 *	or SHT_SUNW_DEBUGSTR or SHT_SUNW_DEBUG section
526c2c65e21Sny155746 		 *	types) then report:
527c2c65e21Sny155746 		 *		"not stripped"
528c2c65e21Sny155746 		 *	- Symbol Table present with no debugging
529c2c65e21Sny155746 		 *	information (line number or debug section names,
530c2c65e21Sny155746 		 *	or SHT_SUNW_DEBUGSTR or SHT_SUNW_DEBUG section
531c2c65e21Sny155746 		 *	types) then report:
532c2c65e21Sny155746 		 *		"not stripped, no debugging information
533c2c65e21Sny155746 		 *		available"
534c2c65e21Sny155746 		 */
535c2c65e21Sny155746 		if ((EI->stripped & E_NOSTRIP) == E_NOSTRIP)
536c2c65e21Sny155746 			continue;
537c2c65e21Sny155746 
538c2c65e21Sny155746 		if (!(EI->stripped & E_SYMTAB) &&
539c2c65e21Sny155746 		    (shdr->sh_type == SHT_SYMTAB)) {
540c2c65e21Sny155746 			EI->stripped |= E_SYMTAB;
541c2c65e21Sny155746 			continue;
542c2c65e21Sny155746 		}
543c2c65e21Sny155746 
544*3b0698c8SRichard Lowe 		if (shdr->sh_name >= strtab_sz)
545*3b0698c8SRichard Lowe 			shnam = NULL;
546*3b0698c8SRichard Lowe 		else
547*3b0698c8SRichard Lowe 			shnam = &strtab[shdr->sh_name];
548c2c65e21Sny155746 
549c2c65e21Sny155746 		if (!(EI->stripped & E_DBGINF) &&
550c2c65e21Sny155746 		    ((shdr->sh_type == SHT_SUNW_DEBUG) ||
551c2c65e21Sny155746 		    (shdr->sh_type == SHT_SUNW_DEBUGSTR) ||
552*3b0698c8SRichard Lowe 		    (shnam != NULL && is_in_list(shnam)))) {
553c2c65e21Sny155746 			EI->stripped |= E_DBGINF;
554c2c65e21Sny155746 		}
555c2c65e21Sny155746 	}
556*3b0698c8SRichard Lowe 	free(strtab);
557c2c65e21Sny155746 
558c2c65e21Sny155746 	return (ELF_READ_OKAY);
559c2c65e21Sny155746 }
560