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