xref: /minix3/external/bsd/elftoolchain/dist/libelf/elf_data.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: elf_data.c,v 1.2 2014/03/09 16:58:04 christos Exp $	*/
2*0a6a1f1dSLionel Sambuc 
3*0a6a1f1dSLionel Sambuc /*-
4*0a6a1f1dSLionel Sambuc  * Copyright (c) 2006,2008,2011 Joseph Koshy
5*0a6a1f1dSLionel Sambuc  * All rights reserved.
6*0a6a1f1dSLionel Sambuc  *
7*0a6a1f1dSLionel Sambuc  * Redistribution and use in source and binary forms, with or without
8*0a6a1f1dSLionel Sambuc  * modification, are permitted provided that the following conditions
9*0a6a1f1dSLionel Sambuc  * are met:
10*0a6a1f1dSLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
11*0a6a1f1dSLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
12*0a6a1f1dSLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
13*0a6a1f1dSLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
14*0a6a1f1dSLionel Sambuc  *    documentation and/or other materials provided with the distribution.
15*0a6a1f1dSLionel Sambuc  *
16*0a6a1f1dSLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17*0a6a1f1dSLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*0a6a1f1dSLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*0a6a1f1dSLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20*0a6a1f1dSLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*0a6a1f1dSLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*0a6a1f1dSLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*0a6a1f1dSLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*0a6a1f1dSLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*0a6a1f1dSLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*0a6a1f1dSLionel Sambuc  * SUCH DAMAGE.
27*0a6a1f1dSLionel Sambuc  */
28*0a6a1f1dSLionel Sambuc 
29*0a6a1f1dSLionel Sambuc #if HAVE_NBTOOL_CONFIG_H
30*0a6a1f1dSLionel Sambuc # include "nbtool_config.h"
31*0a6a1f1dSLionel Sambuc #endif
32*0a6a1f1dSLionel Sambuc 
33*0a6a1f1dSLionel Sambuc #include <assert.h>
34*0a6a1f1dSLionel Sambuc #include <errno.h>
35*0a6a1f1dSLionel Sambuc #include <libelf.h>
36*0a6a1f1dSLionel Sambuc #include <stdlib.h>
37*0a6a1f1dSLionel Sambuc 
38*0a6a1f1dSLionel Sambuc #include "_libelf.h"
39*0a6a1f1dSLionel Sambuc 
40*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: elf_data.c,v 1.2 2014/03/09 16:58:04 christos Exp $");
41*0a6a1f1dSLionel Sambuc ELFTC_VCSID("Id: elf_data.c 2921 2013-03-04 16:19:22Z jkoshy ");
42*0a6a1f1dSLionel Sambuc 
43*0a6a1f1dSLionel Sambuc Elf_Data *
elf_getdata(Elf_Scn * s,Elf_Data * ed)44*0a6a1f1dSLionel Sambuc elf_getdata(Elf_Scn *s, Elf_Data *ed)
45*0a6a1f1dSLionel Sambuc {
46*0a6a1f1dSLionel Sambuc 	Elf *e;
47*0a6a1f1dSLionel Sambuc 	unsigned int sh_type;
48*0a6a1f1dSLionel Sambuc 	int elfclass, elftype;
49*0a6a1f1dSLionel Sambuc 	size_t fsz, msz, count;
50*0a6a1f1dSLionel Sambuc 	struct _Libelf_Data *d;
51*0a6a1f1dSLionel Sambuc 	uint64_t sh_align, sh_offset, sh_size;
52*0a6a1f1dSLionel Sambuc 	int (*xlate)(char *_d, size_t _dsz, char *_s, size_t _c, int _swap);
53*0a6a1f1dSLionel Sambuc 
54*0a6a1f1dSLionel Sambuc 	d = (struct _Libelf_Data *) ed;
55*0a6a1f1dSLionel Sambuc 
56*0a6a1f1dSLionel Sambuc 	if (s == NULL || (e = s->s_elf) == NULL ||
57*0a6a1f1dSLionel Sambuc 	    (d != NULL && s != d->d_scn)) {
58*0a6a1f1dSLionel Sambuc 		LIBELF_SET_ERROR(ARGUMENT, 0);
59*0a6a1f1dSLionel Sambuc 		return (NULL);
60*0a6a1f1dSLionel Sambuc 	}
61*0a6a1f1dSLionel Sambuc 
62*0a6a1f1dSLionel Sambuc 	assert(e->e_kind == ELF_K_ELF);
63*0a6a1f1dSLionel Sambuc 
64*0a6a1f1dSLionel Sambuc 	if (d == NULL && (d = STAILQ_FIRST(&s->s_data)) != NULL)
65*0a6a1f1dSLionel Sambuc 		return (&d->d_data);
66*0a6a1f1dSLionel Sambuc 
67*0a6a1f1dSLionel Sambuc 	if (d != NULL)
68*0a6a1f1dSLionel Sambuc 		return (&STAILQ_NEXT(d, d_next)->d_data);
69*0a6a1f1dSLionel Sambuc 
70*0a6a1f1dSLionel Sambuc 	if (e->e_rawfile == NULL) {
71*0a6a1f1dSLionel Sambuc 		/*
72*0a6a1f1dSLionel Sambuc 		 * In the ELF_C_WRITE case, there is no source that
73*0a6a1f1dSLionel Sambuc 		 * can provide data for the section.
74*0a6a1f1dSLionel Sambuc 		 */
75*0a6a1f1dSLionel Sambuc 		LIBELF_SET_ERROR(ARGUMENT, 0);
76*0a6a1f1dSLionel Sambuc 		return (NULL);
77*0a6a1f1dSLionel Sambuc 	}
78*0a6a1f1dSLionel Sambuc 
79*0a6a1f1dSLionel Sambuc 	elfclass = e->e_class;
80*0a6a1f1dSLionel Sambuc 
81*0a6a1f1dSLionel Sambuc 	assert(elfclass == ELFCLASS32 || elfclass == ELFCLASS64);
82*0a6a1f1dSLionel Sambuc 
83*0a6a1f1dSLionel Sambuc 	if (elfclass == ELFCLASS32) {
84*0a6a1f1dSLionel Sambuc 		sh_type   = s->s_shdr.s_shdr32.sh_type;
85*0a6a1f1dSLionel Sambuc 		sh_offset = (uint64_t) s->s_shdr.s_shdr32.sh_offset;
86*0a6a1f1dSLionel Sambuc 		sh_size   = (uint64_t) s->s_shdr.s_shdr32.sh_size;
87*0a6a1f1dSLionel Sambuc 		sh_align  = (uint64_t) s->s_shdr.s_shdr32.sh_addralign;
88*0a6a1f1dSLionel Sambuc 	} else {
89*0a6a1f1dSLionel Sambuc 		sh_type   = s->s_shdr.s_shdr64.sh_type;
90*0a6a1f1dSLionel Sambuc 		sh_offset = s->s_shdr.s_shdr64.sh_offset;
91*0a6a1f1dSLionel Sambuc 		sh_size   = s->s_shdr.s_shdr64.sh_size;
92*0a6a1f1dSLionel Sambuc 		sh_align  = s->s_shdr.s_shdr64.sh_addralign;
93*0a6a1f1dSLionel Sambuc 	}
94*0a6a1f1dSLionel Sambuc 
95*0a6a1f1dSLionel Sambuc 	if (sh_type == SHT_NULL) {
96*0a6a1f1dSLionel Sambuc 		LIBELF_SET_ERROR(SECTION, 0);
97*0a6a1f1dSLionel Sambuc 		return (NULL);
98*0a6a1f1dSLionel Sambuc 	}
99*0a6a1f1dSLionel Sambuc 
100*0a6a1f1dSLionel Sambuc 	if ((elftype = _libelf_xlate_shtype(sh_type)) < ELF_T_FIRST ||
101*0a6a1f1dSLionel Sambuc 	    elftype > ELF_T_LAST || (sh_type != SHT_NOBITS &&
102*0a6a1f1dSLionel Sambuc 	    sh_offset + sh_size > (uint64_t) e->e_rawsize)) {
103*0a6a1f1dSLionel Sambuc 		LIBELF_SET_ERROR(SECTION, 0);
104*0a6a1f1dSLionel Sambuc 		return (NULL);
105*0a6a1f1dSLionel Sambuc 	}
106*0a6a1f1dSLionel Sambuc 
107*0a6a1f1dSLionel Sambuc 	if ((fsz = (elfclass == ELFCLASS32 ? elf32_fsize : elf64_fsize)
108*0a6a1f1dSLionel Sambuc             (elftype, (size_t) 1, e->e_version)) == 0) {
109*0a6a1f1dSLionel Sambuc 		LIBELF_SET_ERROR(UNIMPL, 0);
110*0a6a1f1dSLionel Sambuc 		return (NULL);
111*0a6a1f1dSLionel Sambuc 	}
112*0a6a1f1dSLionel Sambuc 
113*0a6a1f1dSLionel Sambuc 	if (sh_size % fsz) {
114*0a6a1f1dSLionel Sambuc 		LIBELF_SET_ERROR(SECTION, 0);
115*0a6a1f1dSLionel Sambuc 		return (NULL);
116*0a6a1f1dSLionel Sambuc 	}
117*0a6a1f1dSLionel Sambuc 
118*0a6a1f1dSLionel Sambuc 	count = sh_size / fsz;
119*0a6a1f1dSLionel Sambuc 
120*0a6a1f1dSLionel Sambuc 	msz = _libelf_msize(elftype, elfclass, e->e_version);
121*0a6a1f1dSLionel Sambuc 
122*0a6a1f1dSLionel Sambuc 	assert(msz > 0);
123*0a6a1f1dSLionel Sambuc 
124*0a6a1f1dSLionel Sambuc 	if ((d = _libelf_allocate_data(s)) == NULL)
125*0a6a1f1dSLionel Sambuc 		return (NULL);
126*0a6a1f1dSLionel Sambuc 
127*0a6a1f1dSLionel Sambuc 	d->d_data.d_buf     = NULL;
128*0a6a1f1dSLionel Sambuc 	d->d_data.d_off     = 0;
129*0a6a1f1dSLionel Sambuc 	d->d_data.d_align   = sh_align;
130*0a6a1f1dSLionel Sambuc 	d->d_data.d_size    = msz * count;
131*0a6a1f1dSLionel Sambuc 	d->d_data.d_type    = elftype;
132*0a6a1f1dSLionel Sambuc 	d->d_data.d_version = e->e_version;
133*0a6a1f1dSLionel Sambuc 
134*0a6a1f1dSLionel Sambuc 	if (sh_type == SHT_NOBITS || sh_size == 0) {
135*0a6a1f1dSLionel Sambuc 	        STAILQ_INSERT_TAIL(&s->s_data, d, d_next);
136*0a6a1f1dSLionel Sambuc 		return (&d->d_data);
137*0a6a1f1dSLionel Sambuc         }
138*0a6a1f1dSLionel Sambuc 
139*0a6a1f1dSLionel Sambuc 	if ((d->d_data.d_buf = malloc(msz*count)) == NULL) {
140*0a6a1f1dSLionel Sambuc 		(void) _libelf_release_data(d);
141*0a6a1f1dSLionel Sambuc 		LIBELF_SET_ERROR(RESOURCE, 0);
142*0a6a1f1dSLionel Sambuc 		return (NULL);
143*0a6a1f1dSLionel Sambuc 	}
144*0a6a1f1dSLionel Sambuc 
145*0a6a1f1dSLionel Sambuc 	d->d_flags  |= LIBELF_F_DATA_MALLOCED;
146*0a6a1f1dSLionel Sambuc 
147*0a6a1f1dSLionel Sambuc 	xlate = _libelf_get_translator(elftype, ELF_TOMEMORY, elfclass);
148*0a6a1f1dSLionel Sambuc 	if (!(*xlate)(d->d_data.d_buf, d->d_data.d_size,
149*0a6a1f1dSLionel Sambuc 	    e->e_rawfile + sh_offset, count,
150*0a6a1f1dSLionel Sambuc 	    e->e_byteorder != _libelf_host_byteorder())) {
151*0a6a1f1dSLionel Sambuc 		_libelf_release_data(d);
152*0a6a1f1dSLionel Sambuc 		LIBELF_SET_ERROR(DATA, 0);
153*0a6a1f1dSLionel Sambuc 		return (NULL);
154*0a6a1f1dSLionel Sambuc 	}
155*0a6a1f1dSLionel Sambuc 
156*0a6a1f1dSLionel Sambuc 	STAILQ_INSERT_TAIL(&s->s_data, d, d_next);
157*0a6a1f1dSLionel Sambuc 
158*0a6a1f1dSLionel Sambuc 	return (&d->d_data);
159*0a6a1f1dSLionel Sambuc }
160*0a6a1f1dSLionel Sambuc 
161*0a6a1f1dSLionel Sambuc Elf_Data *
elf_newdata(Elf_Scn * s)162*0a6a1f1dSLionel Sambuc elf_newdata(Elf_Scn *s)
163*0a6a1f1dSLionel Sambuc {
164*0a6a1f1dSLionel Sambuc 	Elf *e;
165*0a6a1f1dSLionel Sambuc 	struct _Libelf_Data *d;
166*0a6a1f1dSLionel Sambuc 
167*0a6a1f1dSLionel Sambuc 	if (s == NULL || (e = s->s_elf) == NULL) {
168*0a6a1f1dSLionel Sambuc 		LIBELF_SET_ERROR(ARGUMENT, 0);
169*0a6a1f1dSLionel Sambuc 		return (NULL);
170*0a6a1f1dSLionel Sambuc 	}
171*0a6a1f1dSLionel Sambuc 
172*0a6a1f1dSLionel Sambuc 	assert(e->e_kind == ELF_K_ELF);
173*0a6a1f1dSLionel Sambuc 
174*0a6a1f1dSLionel Sambuc 	/*
175*0a6a1f1dSLionel Sambuc 	 * elf_newdata() has to append a data descriptor, so
176*0a6a1f1dSLionel Sambuc 	 * bring in existing section data if not already present.
177*0a6a1f1dSLionel Sambuc 	 */
178*0a6a1f1dSLionel Sambuc 	if (e->e_rawfile && s->s_size > 0 && STAILQ_EMPTY(&s->s_data))
179*0a6a1f1dSLionel Sambuc 		if (elf_getdata(s, NULL) == NULL)
180*0a6a1f1dSLionel Sambuc 			return (NULL);
181*0a6a1f1dSLionel Sambuc 
182*0a6a1f1dSLionel Sambuc 	if ((d = _libelf_allocate_data(s)) == NULL)
183*0a6a1f1dSLionel Sambuc 		return (NULL);
184*0a6a1f1dSLionel Sambuc 
185*0a6a1f1dSLionel Sambuc 	STAILQ_INSERT_TAIL(&s->s_data, d, d_next);
186*0a6a1f1dSLionel Sambuc 
187*0a6a1f1dSLionel Sambuc 	d->d_data.d_align = 1;
188*0a6a1f1dSLionel Sambuc 	d->d_data.d_buf = NULL;
189*0a6a1f1dSLionel Sambuc 	d->d_data.d_off = (uint64_t) ~0;
190*0a6a1f1dSLionel Sambuc 	d->d_data.d_size = 0;
191*0a6a1f1dSLionel Sambuc 	d->d_data.d_type = ELF_T_BYTE;
192*0a6a1f1dSLionel Sambuc 	d->d_data.d_version = LIBELF_PRIVATE(version);
193*0a6a1f1dSLionel Sambuc 
194*0a6a1f1dSLionel Sambuc 	(void) elf_flagscn(s, ELF_C_SET, ELF_F_DIRTY);
195*0a6a1f1dSLionel Sambuc 
196*0a6a1f1dSLionel Sambuc 	return (&d->d_data);
197*0a6a1f1dSLionel Sambuc }
198*0a6a1f1dSLionel Sambuc 
199*0a6a1f1dSLionel Sambuc /*
200*0a6a1f1dSLionel Sambuc  * Retrieve a data descriptor for raw (untranslated) data for section
201*0a6a1f1dSLionel Sambuc  * `s'.
202*0a6a1f1dSLionel Sambuc  */
203*0a6a1f1dSLionel Sambuc 
204*0a6a1f1dSLionel Sambuc Elf_Data *
elf_rawdata(Elf_Scn * s,Elf_Data * ed)205*0a6a1f1dSLionel Sambuc elf_rawdata(Elf_Scn *s, Elf_Data *ed)
206*0a6a1f1dSLionel Sambuc {
207*0a6a1f1dSLionel Sambuc 	Elf *e;
208*0a6a1f1dSLionel Sambuc 	int elf_class;
209*0a6a1f1dSLionel Sambuc 	uint32_t sh_type;
210*0a6a1f1dSLionel Sambuc 	struct _Libelf_Data *d;
211*0a6a1f1dSLionel Sambuc 	uint64_t sh_align, sh_offset, sh_size;
212*0a6a1f1dSLionel Sambuc 
213*0a6a1f1dSLionel Sambuc 	if (s == NULL || (e = s->s_elf) == NULL || e->e_rawfile == NULL) {
214*0a6a1f1dSLionel Sambuc 		LIBELF_SET_ERROR(ARGUMENT, 0);
215*0a6a1f1dSLionel Sambuc 		return (NULL);
216*0a6a1f1dSLionel Sambuc 	}
217*0a6a1f1dSLionel Sambuc 
218*0a6a1f1dSLionel Sambuc 	assert(e->e_kind == ELF_K_ELF);
219*0a6a1f1dSLionel Sambuc 
220*0a6a1f1dSLionel Sambuc 	d = (struct _Libelf_Data *) ed;
221*0a6a1f1dSLionel Sambuc 
222*0a6a1f1dSLionel Sambuc 	if (d == NULL && (d = STAILQ_FIRST(&s->s_rawdata)) != NULL)
223*0a6a1f1dSLionel Sambuc 		return (&d->d_data);
224*0a6a1f1dSLionel Sambuc 
225*0a6a1f1dSLionel Sambuc 	if (d != NULL)
226*0a6a1f1dSLionel Sambuc 		return (&STAILQ_NEXT(d, d_next)->d_data);
227*0a6a1f1dSLionel Sambuc 
228*0a6a1f1dSLionel Sambuc 	elf_class = e->e_class;
229*0a6a1f1dSLionel Sambuc 
230*0a6a1f1dSLionel Sambuc 	assert(elf_class == ELFCLASS32 || elf_class == ELFCLASS64);
231*0a6a1f1dSLionel Sambuc 
232*0a6a1f1dSLionel Sambuc 	if (elf_class == ELFCLASS32) {
233*0a6a1f1dSLionel Sambuc 		sh_type   = s->s_shdr.s_shdr32.sh_type;
234*0a6a1f1dSLionel Sambuc 		sh_offset = (uint64_t) s->s_shdr.s_shdr32.sh_offset;
235*0a6a1f1dSLionel Sambuc 		sh_size   = (uint64_t) s->s_shdr.s_shdr32.sh_size;
236*0a6a1f1dSLionel Sambuc 		sh_align  = (uint64_t) s->s_shdr.s_shdr32.sh_addralign;
237*0a6a1f1dSLionel Sambuc 	} else {
238*0a6a1f1dSLionel Sambuc 		sh_type   = s->s_shdr.s_shdr64.sh_type;
239*0a6a1f1dSLionel Sambuc 		sh_offset = s->s_shdr.s_shdr64.sh_offset;
240*0a6a1f1dSLionel Sambuc 		sh_size   = s->s_shdr.s_shdr64.sh_size;
241*0a6a1f1dSLionel Sambuc 		sh_align  = s->s_shdr.s_shdr64.sh_addralign;
242*0a6a1f1dSLionel Sambuc 	}
243*0a6a1f1dSLionel Sambuc 
244*0a6a1f1dSLionel Sambuc 	if (sh_type == SHT_NULL) {
245*0a6a1f1dSLionel Sambuc 		LIBELF_SET_ERROR(SECTION, 0);
246*0a6a1f1dSLionel Sambuc 		return (NULL);
247*0a6a1f1dSLionel Sambuc 	}
248*0a6a1f1dSLionel Sambuc 
249*0a6a1f1dSLionel Sambuc 	if ((d = _libelf_allocate_data(s)) == NULL)
250*0a6a1f1dSLionel Sambuc 		return (NULL);
251*0a6a1f1dSLionel Sambuc 
252*0a6a1f1dSLionel Sambuc 	d->d_data.d_buf = (sh_type == SHT_NOBITS || sh_size == 0) ? NULL :
253*0a6a1f1dSLionel Sambuc 	    e->e_rawfile + sh_offset;
254*0a6a1f1dSLionel Sambuc 	d->d_data.d_off     = 0;
255*0a6a1f1dSLionel Sambuc 	d->d_data.d_align   = sh_align;
256*0a6a1f1dSLionel Sambuc 	d->d_data.d_size    = sh_size;
257*0a6a1f1dSLionel Sambuc 	d->d_data.d_type    = ELF_T_BYTE;
258*0a6a1f1dSLionel Sambuc 	d->d_data.d_version = e->e_version;
259*0a6a1f1dSLionel Sambuc 
260*0a6a1f1dSLionel Sambuc 	STAILQ_INSERT_TAIL(&s->s_rawdata, d, d_next);
261*0a6a1f1dSLionel Sambuc 
262*0a6a1f1dSLionel Sambuc 	return (&d->d_data);
263*0a6a1f1dSLionel Sambuc }
264