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