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