xref: /onnv-gate/usr/src/cmd/sgs/libelf/common/getdata.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51558Sab196087  * Common Development and Distribution License (the "License").
61558Sab196087  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate 
220Sstevel@tonic-gate /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
27*6812Sraf /*	Copyright (c) 1988 AT&T	*/
28*6812Sraf /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
30*6812Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <stdlib.h>
330Sstevel@tonic-gate #include <assert.h>
340Sstevel@tonic-gate #include <errno.h>
350Sstevel@tonic-gate #include <libelf.h>
360Sstevel@tonic-gate #include "decl.h"
370Sstevel@tonic-gate #include "msg.h"
380Sstevel@tonic-gate 
390Sstevel@tonic-gate 
400Sstevel@tonic-gate /*
410Sstevel@tonic-gate  * Convert data from file format to memory format.
420Sstevel@tonic-gate  */
430Sstevel@tonic-gate 
440Sstevel@tonic-gate 
450Sstevel@tonic-gate static const size_t	align32[ELF_T_NUM] =
460Sstevel@tonic-gate {
470Sstevel@tonic-gate 	1,			/* ELF_T_BYTE */
480Sstevel@tonic-gate 	sizeof (Elf32),		/* ELF_T_ADDR */
490Sstevel@tonic-gate 	sizeof (Elf32),		/* ELF_T_DYN */
500Sstevel@tonic-gate 	sizeof (Elf32),		/* ELF_T_EHDR */
510Sstevel@tonic-gate 	sizeof (Elf32_Half),	/* ELF_T_HALF */
520Sstevel@tonic-gate 	sizeof (Elf32),		/* ELF_T_OFF */
530Sstevel@tonic-gate 	sizeof (Elf32),		/* ELF_T_PHDR */
540Sstevel@tonic-gate 	sizeof (Elf32),		/* ELF_T_RELA */
550Sstevel@tonic-gate 	sizeof (Elf32),		/* ELF_T_REL */
560Sstevel@tonic-gate 	sizeof (Elf32),		/* ELF_T_SHDR */
570Sstevel@tonic-gate 	sizeof (Elf32),		/* ELF_T_SWORD */
580Sstevel@tonic-gate 	sizeof (Elf32),		/* ELF_T_SYM */
590Sstevel@tonic-gate 	sizeof (Elf32),		/* ELF_T_WORD */
600Sstevel@tonic-gate 	sizeof (Elf32),		/* ELF_T_VERDEF */
610Sstevel@tonic-gate 	sizeof (Elf32),		/* ELF_T_VERNEED */
620Sstevel@tonic-gate 	sizeof (Elf64_Sxword),	/* ELF_T_SXWORD */
630Sstevel@tonic-gate 	sizeof (Elf64),		/* ELF_T_XWORD */
640Sstevel@tonic-gate 	sizeof (Elf32_Half), 	/* ELF_T_SYMINFO */
650Sstevel@tonic-gate 	sizeof (Elf32),		/* ELF_T_NOTE */
660Sstevel@tonic-gate 	sizeof (Elf32_Lword),	/* ELF_T_MOVE */
670Sstevel@tonic-gate 	sizeof (Elf32_Lword),	/* ELF_T_MOVEP */
680Sstevel@tonic-gate 	sizeof (Elf32_Word)	/* ELF_T_CAP */
690Sstevel@tonic-gate 
700Sstevel@tonic-gate };
710Sstevel@tonic-gate 
720Sstevel@tonic-gate #define	Nalign32	(sizeof (align32)/sizeof (align32[0]))
730Sstevel@tonic-gate 
740Sstevel@tonic-gate static const size_t	align64[ELF_T_NUM] =
750Sstevel@tonic-gate {
760Sstevel@tonic-gate 	1,			/* ELF_T_BYTE */
770Sstevel@tonic-gate 	sizeof (Elf64),		/* ELF_T_ADDR */
780Sstevel@tonic-gate 	sizeof (Elf64),		/* ELF_T_DYN */
790Sstevel@tonic-gate 	sizeof (Elf64),		/* ELF_T_EHDR */
800Sstevel@tonic-gate 	sizeof (Elf64_Half),	/* ELF_T_HALF */
810Sstevel@tonic-gate 	sizeof (Elf64),		/* ELF_T_OFF */
820Sstevel@tonic-gate 	sizeof (Elf64),		/* ELF_T_PHDR */
830Sstevel@tonic-gate 	sizeof (Elf64),		/* ELF_T_RELA */
840Sstevel@tonic-gate 	sizeof (Elf64),		/* ELF_T_REL */
850Sstevel@tonic-gate 	sizeof (Elf64),		/* ELF_T_SHDR */
860Sstevel@tonic-gate 	sizeof (Elf64_Word),	/* ELF_T_SWORD */
870Sstevel@tonic-gate 	sizeof (Elf64),		/* ELF_T_SYM */
880Sstevel@tonic-gate 	sizeof (Elf64_Word),	/* ELF_T_WORD */
890Sstevel@tonic-gate 	sizeof (Elf64),		/* ELF_T_VDEF */
900Sstevel@tonic-gate 	sizeof (Elf64),		/* ELF_T_VNEED */
910Sstevel@tonic-gate 	sizeof (Elf64),		/* ELF_T_SXWORD */
920Sstevel@tonic-gate 	sizeof (Elf64),		/* ELF_T_XWORD */
930Sstevel@tonic-gate 	sizeof (Elf32_Half), 	/* ELF_T_SYMINFO */
940Sstevel@tonic-gate 	sizeof (Elf32),		/* ELF_T_NOTE */
950Sstevel@tonic-gate 	sizeof (Elf64),		/* ELF_T_MOVE */
960Sstevel@tonic-gate 	sizeof (Elf64),		/* ELF_T_MOVEP */
970Sstevel@tonic-gate 	sizeof (Elf64_Word)	/* ELF_T_CAP */
980Sstevel@tonic-gate };
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate #define	Nalign64	(sizeof (align64)/sizeof (align64[0]))
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate /*
1040Sstevel@tonic-gate  * Could use an array indexed by ELFCLASS*, but I'd rather
1050Sstevel@tonic-gate  * avoid .data over something this infrequently used.  The
1060Sstevel@tonic-gate  * next choice would be to add extra conditionals.
1070Sstevel@tonic-gate  */
1080Sstevel@tonic-gate #define	NALIGN(elf)	((elf->ed_class == ELFCLASS32) ? Nalign32 : Nalign64)
1090Sstevel@tonic-gate #define	ALIGN(elf)	((elf->ed_class == ELFCLASS32) ? align32 : align64)
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate Elf_Data *
_elf_locked_getdata(Elf_Scn * scn,Elf_Data * data)1130Sstevel@tonic-gate _elf_locked_getdata(Elf_Scn * scn, Elf_Data * data)
1140Sstevel@tonic-gate {
1150Sstevel@tonic-gate 	Dnode *		d = (Dnode *)data;
1160Sstevel@tonic-gate 	Elf *		elf;
1170Sstevel@tonic-gate 	Elf_Data	src;
1180Sstevel@tonic-gate 	unsigned	work;
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	assert(!elf_threaded || RW_LOCK_HELD(&(scn->s_elf->ed_rwlock)));
1210Sstevel@tonic-gate 	assert(!elf_threaded || MUTEX_HELD(&(scn->s_mutex)));
1220Sstevel@tonic-gate 	elf = scn->s_elf;
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	if ((scn->s_myflags & SF_READY) == 0) {
1250Sstevel@tonic-gate 		UPGRADELOCKS(elf, scn)
1260Sstevel@tonic-gate 		/*
1270Sstevel@tonic-gate 		 * make sure someone else didn't come along and cook
1280Sstevel@tonic-gate 		 * this stuff.
1290Sstevel@tonic-gate 		 */
1300Sstevel@tonic-gate 		if ((scn->s_myflags & SF_READY) == 0)
1310Sstevel@tonic-gate 			(void) _elf_cookscn(scn);
1320Sstevel@tonic-gate 		DOWNGRADELOCKS(elf, scn)
1330Sstevel@tonic-gate 	}
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	if (d == 0)
1360Sstevel@tonic-gate 		d = scn->s_hdnode;
1370Sstevel@tonic-gate 	else
1380Sstevel@tonic-gate 		d = d->db_next;
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	if (scn->s_err != 0) {
1410Sstevel@tonic-gate 		/*LINTED*/
1420Sstevel@tonic-gate 		_elf_seterr((Msg)scn->s_err, 0);
1430Sstevel@tonic-gate 		return (0);
1440Sstevel@tonic-gate 	}
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	if (d == 0) {
1470Sstevel@tonic-gate 		return (0);
1480Sstevel@tonic-gate 	}
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	if (d->db_scn != scn) {
1510Sstevel@tonic-gate 		_elf_seterr(EREQ_DATA, 0);
1520Sstevel@tonic-gate 		return (0);
1530Sstevel@tonic-gate 	}
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	if (d->db_myflags & DBF_READY) {
1560Sstevel@tonic-gate 		return (&d->db_data);
1570Sstevel@tonic-gate 	}
1580Sstevel@tonic-gate 	elf = scn->s_elf;
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	/*
1610Sstevel@tonic-gate 	 * Prepare return buffer.  The data comes from the memory
1620Sstevel@tonic-gate 	 * image of the file.  "Empty" regions get an empty buffer.
1630Sstevel@tonic-gate 	 *
1640Sstevel@tonic-gate 	 * Only sections of an ELF_C_READ file can be not READY here.
1650Sstevel@tonic-gate 	 * Furthermore, the input file must have been cooked or
1660Sstevel@tonic-gate 	 * frozen by now.  Translate cooked files in place if possible.
1670Sstevel@tonic-gate 	 */
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	ELFACCESSDATA(work, _elf_work)
1700Sstevel@tonic-gate 	d->db_data.d_version = work;
1710Sstevel@tonic-gate 	if ((d->db_off == 0) || (d->db_fsz == 0)) {
1720Sstevel@tonic-gate 		d->db_myflags |= DBF_READY;
1730Sstevel@tonic-gate 		return (&d->db_data);
1740Sstevel@tonic-gate 	}
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	if (elf->ed_class == ELFCLASS32) {
1770Sstevel@tonic-gate 		Elf32_Shdr	*sh = scn->s_shdr;
1780Sstevel@tonic-gate 		size_t		sz = sh->sh_entsize;
1790Sstevel@tonic-gate 		Elf_Type	t = d->db_data.d_type;
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 		if ((t != ELF_T_BYTE) &&
1820Sstevel@tonic-gate 		    (sz > 1) && (sz != elf32_fsize(t, 1, elf->ed_version))) {
1830Sstevel@tonic-gate 			_elf_seterr(EFMT_ENTSZ, 0);
1840Sstevel@tonic-gate 			return (0);
1850Sstevel@tonic-gate 		}
1860Sstevel@tonic-gate 	} else if (elf->ed_class == ELFCLASS64) {
1870Sstevel@tonic-gate 		Elf64_Shdr	*sh = scn->s_shdr;
1880Sstevel@tonic-gate 		Elf64_Xword	sz = sh->sh_entsize;
1890Sstevel@tonic-gate 		Elf_Type	t = d->db_data.d_type;
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 		if (t != ELF_T_BYTE && sz > 1 &&
1920Sstevel@tonic-gate 		    sz != elf64_fsize(t, 1, elf->ed_version)) {
1930Sstevel@tonic-gate 			_elf_seterr(EFMT_ENTSZ, 0);
1940Sstevel@tonic-gate 			return (0);
1950Sstevel@tonic-gate 		}
1960Sstevel@tonic-gate 	} else {
1970Sstevel@tonic-gate 		_elf_seterr(EREQ_CLASS, 0);
1980Sstevel@tonic-gate 		return (0);
1990Sstevel@tonic-gate 	}
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	/*
2030Sstevel@tonic-gate 	 * validate the region
2040Sstevel@tonic-gate 	 */
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	if ((d->db_off < 0) || (d->db_off >= elf->ed_fsz) ||
2070Sstevel@tonic-gate 	    (elf->ed_fsz - d->db_off < d->db_fsz)) {
2080Sstevel@tonic-gate 		_elf_seterr(EFMT_DATA, 0);
2090Sstevel@tonic-gate 		return (0);
2100Sstevel@tonic-gate 	}
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	/*
2130Sstevel@tonic-gate 	 * set up translation buffers and validate
2140Sstevel@tonic-gate 	 */
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	src.d_buf = (Elf_Void *)(elf->ed_ident + d->db_off);
2170Sstevel@tonic-gate 	src.d_size = d->db_fsz;
2180Sstevel@tonic-gate 	src.d_type = d->db_data.d_type;
2190Sstevel@tonic-gate 	src.d_version = elf->ed_version;
2200Sstevel@tonic-gate 	if (elf->ed_vm) {
2210Sstevel@tonic-gate 		UPGRADELOCKS(elf, scn)
2220Sstevel@tonic-gate 		if (_elf_vm(elf, (size_t)d->db_off, d->db_fsz) != OK_YES) {
2230Sstevel@tonic-gate 			DOWNGRADELOCKS(elf, scn)
2240Sstevel@tonic-gate 			return (0);
2250Sstevel@tonic-gate 		}
2260Sstevel@tonic-gate 		DOWNGRADELOCKS(elf, scn)
2270Sstevel@tonic-gate 	}
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	/*
2300Sstevel@tonic-gate 	 * decide where to put destination
2310Sstevel@tonic-gate 	 */
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	switch (elf->ed_status) {
2340Sstevel@tonic-gate 	case ES_COOKED:
2350Sstevel@tonic-gate 		if ((size_t)d->db_data.d_type >= NALIGN(elf)) {
2360Sstevel@tonic-gate 			_elf_seterr(EBUG_COOKTYPE, 0);
2370Sstevel@tonic-gate 			return (0);
2380Sstevel@tonic-gate 		}
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 		/*
2410Sstevel@tonic-gate 		 * If the destination size (memory) is at least as
2421558Sab196087 		 * big as the source size (file), and has the necessary
2431558Sab196087 		 * alignment, reuse the space.
2441558Sab196087 		 *
2451558Sab196087 		 * Note that it is not sufficient to check the alignment
2461558Sab196087 		 * of the offset within the object. Rather, we must check
2471558Sab196087 		 * the alignment of the actual data buffer. The offset is
2481558Sab196087 		 * sufficient if the file is a plain object file, which
2491558Sab196087 		 * will always be mapped on a page boundary. In an archive
2501558Sab196087 		 * however, the only guarantee is that the object will start
2511558Sab196087 		 * on an even boundary within the archive file. The
2521558Sab196087 		 * Solaris ar(1) adds padding in most (but not all cases)
2531558Sab196087 		 * which minimizes this issue, but it is still important
2541558Sab196087 		 * for the remaining cases that do not get padded. It also
2551558Sab196087 		 * matters with archives produced by other versions of
2561558Sab196087 		 * ar(1), such as the GNU version, or one from another
2571558Sab196087 		 * ELF based operating system.
2580Sstevel@tonic-gate 		 */
2590Sstevel@tonic-gate 
2601558Sab196087 		if (d->db_data.d_size <= src.d_size) {
2610Sstevel@tonic-gate 			d->db_data.d_buf = (Elf_Void *)(elf->ed_ident +
262*6812Sraf 			    d->db_off);
2631558Sab196087 			if (((uintptr_t)d->db_data.d_buf
264*6812Sraf 			    % ALIGN(elf)[d->db_data.d_type]) == 0) {
2651558Sab196087 				break;
2661558Sab196087 			} else {   /* Failure: Restore NULL buffer pointer */
2671558Sab196087 				d->db_data.d_buf = 0;
2681558Sab196087 			}
2690Sstevel@tonic-gate 		}
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 		/*FALLTHRU*/
2720Sstevel@tonic-gate 	case ES_FROZEN:
2730Sstevel@tonic-gate 		if ((d->db_buf = malloc(d->db_data.d_size)) == 0) {
2740Sstevel@tonic-gate 			_elf_seterr(EMEM_DATA, errno);
2750Sstevel@tonic-gate 			return (0);
2760Sstevel@tonic-gate 		}
2770Sstevel@tonic-gate 		d->db_data.d_buf = d->db_buf;
2780Sstevel@tonic-gate 		break;
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	default:
2810Sstevel@tonic-gate 		_elf_seterr(EBUG_COOKSTAT, 0);
2820Sstevel@tonic-gate 		return (0);
2830Sstevel@tonic-gate 	}
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	if (elf->ed_class == ELFCLASS32) {
2860Sstevel@tonic-gate 		if (elf32_xlatetom(&d->db_data, &src, elf->ed_encode) == 0)
2870Sstevel@tonic-gate 			return (0);
2880Sstevel@tonic-gate 	} else {	/* ELFCLASS64 */
2890Sstevel@tonic-gate 		if (elf64_xlatetom(&d->db_data, &src, elf->ed_encode) == 0)
2900Sstevel@tonic-gate 			return (0);
2910Sstevel@tonic-gate 	}
2920Sstevel@tonic-gate 	d->db_myflags |= DBF_READY;
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	return (&d->db_data);
2950Sstevel@tonic-gate }
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate Elf_Data *
elf_getdata(Elf_Scn * scn,Elf_Data * data)2980Sstevel@tonic-gate elf_getdata(Elf_Scn * scn, Elf_Data * data)
2990Sstevel@tonic-gate {
3000Sstevel@tonic-gate 	Elf_Data *	rc;
3010Sstevel@tonic-gate 	Elf *	elf;
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	/*
3040Sstevel@tonic-gate 	 * trap null args, end of list, previous buffer.
3050Sstevel@tonic-gate 	 * SHT_NULL sections have no buffer list, so they
3060Sstevel@tonic-gate 	 * fall out here too.
3070Sstevel@tonic-gate 	 */
3080Sstevel@tonic-gate 	if (scn == 0)
3090Sstevel@tonic-gate 		return (0);
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 	elf = scn->s_elf;
3120Sstevel@tonic-gate 	READLOCKS(elf, scn);
3130Sstevel@tonic-gate 	rc = _elf_locked_getdata(scn, data);
3140Sstevel@tonic-gate 	READUNLOCKS(elf, scn);
3150Sstevel@tonic-gate 	return (rc);
3160Sstevel@tonic-gate }
317