xref: /onnv-gate/usr/src/cmd/sgs/libelf/common/getdata.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
23*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate /*
27*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
28*0Sstevel@tonic-gate  * Use is subject to license terms.
29*0Sstevel@tonic-gate  */
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI" 	/* SVr4.0 1.16	*/
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #pragma weak	elf_getdata = _elf_getdata
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate #include "syn.h"
39*0Sstevel@tonic-gate #include <stdlib.h>
40*0Sstevel@tonic-gate #include <assert.h>
41*0Sstevel@tonic-gate #include <errno.h>
42*0Sstevel@tonic-gate #include <libelf.h>
43*0Sstevel@tonic-gate #include "decl.h"
44*0Sstevel@tonic-gate #include "msg.h"
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate /*
48*0Sstevel@tonic-gate  * Convert data from file format to memory format.
49*0Sstevel@tonic-gate  */
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate static const size_t	align32[ELF_T_NUM] =
53*0Sstevel@tonic-gate {
54*0Sstevel@tonic-gate 	1,			/* ELF_T_BYTE */
55*0Sstevel@tonic-gate 	sizeof (Elf32),		/* ELF_T_ADDR */
56*0Sstevel@tonic-gate 	sizeof (Elf32),		/* ELF_T_DYN */
57*0Sstevel@tonic-gate 	sizeof (Elf32),		/* ELF_T_EHDR */
58*0Sstevel@tonic-gate 	sizeof (Elf32_Half),	/* ELF_T_HALF */
59*0Sstevel@tonic-gate 	sizeof (Elf32),		/* ELF_T_OFF */
60*0Sstevel@tonic-gate 	sizeof (Elf32),		/* ELF_T_PHDR */
61*0Sstevel@tonic-gate 	sizeof (Elf32),		/* ELF_T_RELA */
62*0Sstevel@tonic-gate 	sizeof (Elf32),		/* ELF_T_REL */
63*0Sstevel@tonic-gate 	sizeof (Elf32),		/* ELF_T_SHDR */
64*0Sstevel@tonic-gate 	sizeof (Elf32),		/* ELF_T_SWORD */
65*0Sstevel@tonic-gate 	sizeof (Elf32),		/* ELF_T_SYM */
66*0Sstevel@tonic-gate 	sizeof (Elf32),		/* ELF_T_WORD */
67*0Sstevel@tonic-gate 	sizeof (Elf32),		/* ELF_T_VERDEF */
68*0Sstevel@tonic-gate 	sizeof (Elf32),		/* ELF_T_VERNEED */
69*0Sstevel@tonic-gate 	sizeof (Elf64_Sxword),	/* ELF_T_SXWORD */
70*0Sstevel@tonic-gate 	sizeof (Elf64),		/* ELF_T_XWORD */
71*0Sstevel@tonic-gate 	sizeof (Elf32_Half), 	/* ELF_T_SYMINFO */
72*0Sstevel@tonic-gate 	sizeof (Elf32),		/* ELF_T_NOTE */
73*0Sstevel@tonic-gate 	sizeof (Elf32_Lword),	/* ELF_T_MOVE */
74*0Sstevel@tonic-gate 	sizeof (Elf32_Lword),	/* ELF_T_MOVEP */
75*0Sstevel@tonic-gate 	sizeof (Elf32_Word)	/* ELF_T_CAP */
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate };
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate #define	Nalign32	(sizeof (align32)/sizeof (align32[0]))
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate static const size_t	align64[ELF_T_NUM] =
82*0Sstevel@tonic-gate {
83*0Sstevel@tonic-gate 	1,			/* ELF_T_BYTE */
84*0Sstevel@tonic-gate 	sizeof (Elf64),		/* ELF_T_ADDR */
85*0Sstevel@tonic-gate 	sizeof (Elf64),		/* ELF_T_DYN */
86*0Sstevel@tonic-gate 	sizeof (Elf64),		/* ELF_T_EHDR */
87*0Sstevel@tonic-gate 	sizeof (Elf64_Half),	/* ELF_T_HALF */
88*0Sstevel@tonic-gate 	sizeof (Elf64),		/* ELF_T_OFF */
89*0Sstevel@tonic-gate 	sizeof (Elf64),		/* ELF_T_PHDR */
90*0Sstevel@tonic-gate 	sizeof (Elf64),		/* ELF_T_RELA */
91*0Sstevel@tonic-gate 	sizeof (Elf64),		/* ELF_T_REL */
92*0Sstevel@tonic-gate 	sizeof (Elf64),		/* ELF_T_SHDR */
93*0Sstevel@tonic-gate 	sizeof (Elf64_Word),	/* ELF_T_SWORD */
94*0Sstevel@tonic-gate 	sizeof (Elf64),		/* ELF_T_SYM */
95*0Sstevel@tonic-gate 	sizeof (Elf64_Word),	/* ELF_T_WORD */
96*0Sstevel@tonic-gate 	sizeof (Elf64),		/* ELF_T_VDEF */
97*0Sstevel@tonic-gate 	sizeof (Elf64),		/* ELF_T_VNEED */
98*0Sstevel@tonic-gate 	sizeof (Elf64),		/* ELF_T_SXWORD */
99*0Sstevel@tonic-gate 	sizeof (Elf64),		/* ELF_T_XWORD */
100*0Sstevel@tonic-gate 	sizeof (Elf32_Half), 	/* ELF_T_SYMINFO */
101*0Sstevel@tonic-gate 	sizeof (Elf32),		/* ELF_T_NOTE */
102*0Sstevel@tonic-gate 	sizeof (Elf64),		/* ELF_T_MOVE */
103*0Sstevel@tonic-gate 	sizeof (Elf64),		/* ELF_T_MOVEP */
104*0Sstevel@tonic-gate 	sizeof (Elf64_Word)	/* ELF_T_CAP */
105*0Sstevel@tonic-gate };
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate #define	Nalign64	(sizeof (align64)/sizeof (align64[0]))
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate /*
111*0Sstevel@tonic-gate  * Could use an array indexed by ELFCLASS*, but I'd rather
112*0Sstevel@tonic-gate  * avoid .data over something this infrequently used.  The
113*0Sstevel@tonic-gate  * next choice would be to add extra conditionals.
114*0Sstevel@tonic-gate  */
115*0Sstevel@tonic-gate #define	NALIGN(elf)	((elf->ed_class == ELFCLASS32) ? Nalign32 : Nalign64)
116*0Sstevel@tonic-gate #define	ALIGN(elf)	((elf->ed_class == ELFCLASS32) ? align32 : align64)
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate Elf_Data *
120*0Sstevel@tonic-gate _elf_locked_getdata(Elf_Scn * scn, Elf_Data * data)
121*0Sstevel@tonic-gate {
122*0Sstevel@tonic-gate 	Dnode *		d = (Dnode *)data;
123*0Sstevel@tonic-gate 	Elf *		elf;
124*0Sstevel@tonic-gate 	Elf_Data	src;
125*0Sstevel@tonic-gate 	unsigned	work;
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate 	assert(!elf_threaded || RW_LOCK_HELD(&(scn->s_elf->ed_rwlock)));
128*0Sstevel@tonic-gate 	assert(!elf_threaded || MUTEX_HELD(&(scn->s_mutex)));
129*0Sstevel@tonic-gate 	elf = scn->s_elf;
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate 	if ((scn->s_myflags & SF_READY) == 0) {
132*0Sstevel@tonic-gate 		UPGRADELOCKS(elf, scn)
133*0Sstevel@tonic-gate 		/*
134*0Sstevel@tonic-gate 		 * make sure someone else didn't come along and cook
135*0Sstevel@tonic-gate 		 * this stuff.
136*0Sstevel@tonic-gate 		 */
137*0Sstevel@tonic-gate 		if ((scn->s_myflags & SF_READY) == 0)
138*0Sstevel@tonic-gate 			(void) _elf_cookscn(scn);
139*0Sstevel@tonic-gate 		DOWNGRADELOCKS(elf, scn)
140*0Sstevel@tonic-gate 	}
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate 	if (d == 0)
143*0Sstevel@tonic-gate 		d = scn->s_hdnode;
144*0Sstevel@tonic-gate 	else
145*0Sstevel@tonic-gate 		d = d->db_next;
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate 	if (scn->s_err != 0) {
148*0Sstevel@tonic-gate 		/*LINTED*/
149*0Sstevel@tonic-gate 		_elf_seterr((Msg)scn->s_err, 0);
150*0Sstevel@tonic-gate 		return (0);
151*0Sstevel@tonic-gate 	}
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate 	if (d == 0) {
154*0Sstevel@tonic-gate 		return (0);
155*0Sstevel@tonic-gate 	}
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate 	if (d->db_scn != scn) {
158*0Sstevel@tonic-gate 		_elf_seterr(EREQ_DATA, 0);
159*0Sstevel@tonic-gate 		return (0);
160*0Sstevel@tonic-gate 	}
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate 	if (d->db_myflags & DBF_READY) {
163*0Sstevel@tonic-gate 		return (&d->db_data);
164*0Sstevel@tonic-gate 	}
165*0Sstevel@tonic-gate 	elf = scn->s_elf;
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate 	/*
168*0Sstevel@tonic-gate 	 * Prepare return buffer.  The data comes from the memory
169*0Sstevel@tonic-gate 	 * image of the file.  "Empty" regions get an empty buffer.
170*0Sstevel@tonic-gate 	 *
171*0Sstevel@tonic-gate 	 * Only sections of an ELF_C_READ file can be not READY here.
172*0Sstevel@tonic-gate 	 * Furthermore, the input file must have been cooked or
173*0Sstevel@tonic-gate 	 * frozen by now.  Translate cooked files in place if possible.
174*0Sstevel@tonic-gate 	 */
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate 	ELFACCESSDATA(work, _elf_work)
177*0Sstevel@tonic-gate 	d->db_data.d_version = work;
178*0Sstevel@tonic-gate 	if ((d->db_off == 0) || (d->db_fsz == 0)) {
179*0Sstevel@tonic-gate 		d->db_myflags |= DBF_READY;
180*0Sstevel@tonic-gate 		return (&d->db_data);
181*0Sstevel@tonic-gate 	}
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate 	if (elf->ed_class == ELFCLASS32) {
184*0Sstevel@tonic-gate 		Elf32_Shdr	*sh = scn->s_shdr;
185*0Sstevel@tonic-gate 		size_t		sz = sh->sh_entsize;
186*0Sstevel@tonic-gate 		Elf_Type	t = d->db_data.d_type;
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate 		if ((t != ELF_T_BYTE) &&
189*0Sstevel@tonic-gate 		    (sz > 1) && (sz != elf32_fsize(t, 1, elf->ed_version))) {
190*0Sstevel@tonic-gate 			_elf_seterr(EFMT_ENTSZ, 0);
191*0Sstevel@tonic-gate 			return (0);
192*0Sstevel@tonic-gate 		}
193*0Sstevel@tonic-gate 	} else if (elf->ed_class == ELFCLASS64) {
194*0Sstevel@tonic-gate 		Elf64_Shdr	*sh = scn->s_shdr;
195*0Sstevel@tonic-gate 		Elf64_Xword	sz = sh->sh_entsize;
196*0Sstevel@tonic-gate 		Elf_Type	t = d->db_data.d_type;
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate 		if (t != ELF_T_BYTE && sz > 1 &&
199*0Sstevel@tonic-gate 		    sz != elf64_fsize(t, 1, elf->ed_version)) {
200*0Sstevel@tonic-gate 			_elf_seterr(EFMT_ENTSZ, 0);
201*0Sstevel@tonic-gate 			return (0);
202*0Sstevel@tonic-gate 		}
203*0Sstevel@tonic-gate 	} else {
204*0Sstevel@tonic-gate 		_elf_seterr(EREQ_CLASS, 0);
205*0Sstevel@tonic-gate 		return (0);
206*0Sstevel@tonic-gate 	}
207*0Sstevel@tonic-gate 
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate 	/*
210*0Sstevel@tonic-gate 	 * validate the region
211*0Sstevel@tonic-gate 	 */
212*0Sstevel@tonic-gate 
213*0Sstevel@tonic-gate 	if ((d->db_off < 0) || (d->db_off >= elf->ed_fsz) ||
214*0Sstevel@tonic-gate 	    (elf->ed_fsz - d->db_off < d->db_fsz)) {
215*0Sstevel@tonic-gate 		_elf_seterr(EFMT_DATA, 0);
216*0Sstevel@tonic-gate 		return (0);
217*0Sstevel@tonic-gate 	}
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate 	/*
220*0Sstevel@tonic-gate 	 * set up translation buffers and validate
221*0Sstevel@tonic-gate 	 */
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate 	src.d_buf = (Elf_Void *)(elf->ed_ident + d->db_off);
224*0Sstevel@tonic-gate 	src.d_size = d->db_fsz;
225*0Sstevel@tonic-gate 	src.d_type = d->db_data.d_type;
226*0Sstevel@tonic-gate 	src.d_version = elf->ed_version;
227*0Sstevel@tonic-gate 	if (elf->ed_vm) {
228*0Sstevel@tonic-gate 		UPGRADELOCKS(elf, scn)
229*0Sstevel@tonic-gate 		if (_elf_vm(elf, (size_t)d->db_off, d->db_fsz) != OK_YES) {
230*0Sstevel@tonic-gate 			DOWNGRADELOCKS(elf, scn)
231*0Sstevel@tonic-gate 			return (0);
232*0Sstevel@tonic-gate 		}
233*0Sstevel@tonic-gate 		DOWNGRADELOCKS(elf, scn)
234*0Sstevel@tonic-gate 	}
235*0Sstevel@tonic-gate 
236*0Sstevel@tonic-gate 	/*
237*0Sstevel@tonic-gate 	 * decide where to put destination
238*0Sstevel@tonic-gate 	 */
239*0Sstevel@tonic-gate 
240*0Sstevel@tonic-gate 	switch (elf->ed_status) {
241*0Sstevel@tonic-gate 	case ES_COOKED:
242*0Sstevel@tonic-gate 		if ((size_t)d->db_data.d_type >= NALIGN(elf)) {
243*0Sstevel@tonic-gate 			_elf_seterr(EBUG_COOKTYPE, 0);
244*0Sstevel@tonic-gate 			return (0);
245*0Sstevel@tonic-gate 		}
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate 		/*
248*0Sstevel@tonic-gate 		 * If the destination size (memory) is at least as
249*0Sstevel@tonic-gate 		 * big as the source size (file), reuse the space.
250*0Sstevel@tonic-gate 		 */
251*0Sstevel@tonic-gate 
252*0Sstevel@tonic-gate 		if ((d->db_data.d_size <= src.d_size) &&
253*0Sstevel@tonic-gate 		    (d->db_off % ALIGN(elf)[d->db_data.d_type] == 0)) {
254*0Sstevel@tonic-gate 			d->db_data.d_buf = (Elf_Void *)(elf->ed_ident +
255*0Sstevel@tonic-gate 				d->db_off);
256*0Sstevel@tonic-gate 			break;
257*0Sstevel@tonic-gate 		}
258*0Sstevel@tonic-gate 
259*0Sstevel@tonic-gate 		/*FALLTHRU*/
260*0Sstevel@tonic-gate 	case ES_FROZEN:
261*0Sstevel@tonic-gate 		if ((d->db_buf = malloc(d->db_data.d_size)) == 0) {
262*0Sstevel@tonic-gate 			_elf_seterr(EMEM_DATA, errno);
263*0Sstevel@tonic-gate 			return (0);
264*0Sstevel@tonic-gate 		}
265*0Sstevel@tonic-gate 		d->db_data.d_buf = d->db_buf;
266*0Sstevel@tonic-gate 		break;
267*0Sstevel@tonic-gate 
268*0Sstevel@tonic-gate 	default:
269*0Sstevel@tonic-gate 		_elf_seterr(EBUG_COOKSTAT, 0);
270*0Sstevel@tonic-gate 		return (0);
271*0Sstevel@tonic-gate 	}
272*0Sstevel@tonic-gate 
273*0Sstevel@tonic-gate 	if (elf->ed_class == ELFCLASS32) {
274*0Sstevel@tonic-gate 		if (elf32_xlatetom(&d->db_data, &src, elf->ed_encode) == 0)
275*0Sstevel@tonic-gate 			return (0);
276*0Sstevel@tonic-gate 	} else {	/* ELFCLASS64 */
277*0Sstevel@tonic-gate 		if (elf64_xlatetom(&d->db_data, &src, elf->ed_encode) == 0)
278*0Sstevel@tonic-gate 			return (0);
279*0Sstevel@tonic-gate 	}
280*0Sstevel@tonic-gate 	d->db_myflags |= DBF_READY;
281*0Sstevel@tonic-gate 
282*0Sstevel@tonic-gate 	return (&d->db_data);
283*0Sstevel@tonic-gate }
284*0Sstevel@tonic-gate 
285*0Sstevel@tonic-gate Elf_Data *
286*0Sstevel@tonic-gate elf_getdata(Elf_Scn * scn, Elf_Data * data)
287*0Sstevel@tonic-gate {
288*0Sstevel@tonic-gate 	Elf_Data *	rc;
289*0Sstevel@tonic-gate 	Elf *	elf;
290*0Sstevel@tonic-gate 
291*0Sstevel@tonic-gate 	/*
292*0Sstevel@tonic-gate 	 * trap null args, end of list, previous buffer.
293*0Sstevel@tonic-gate 	 * SHT_NULL sections have no buffer list, so they
294*0Sstevel@tonic-gate 	 * fall out here too.
295*0Sstevel@tonic-gate 	 */
296*0Sstevel@tonic-gate 	if (scn == 0)
297*0Sstevel@tonic-gate 		return (0);
298*0Sstevel@tonic-gate 
299*0Sstevel@tonic-gate 	elf = scn->s_elf;
300*0Sstevel@tonic-gate 	READLOCKS(elf, scn);
301*0Sstevel@tonic-gate 	rc = _elf_locked_getdata(scn, data);
302*0Sstevel@tonic-gate 	READUNLOCKS(elf, scn);
303*0Sstevel@tonic-gate 	return (rc);
304*0Sstevel@tonic-gate }
305