1164190Sjkoshy/*-
2164190Sjkoshy * Copyright (c) 2006 Joseph Koshy
3164190Sjkoshy * All rights reserved.
4164190Sjkoshy *
5164190Sjkoshy * Redistribution and use in source and binary forms, with or without
6164190Sjkoshy * modification, are permitted provided that the following conditions
7164190Sjkoshy * are met:
8164190Sjkoshy * 1. Redistributions of source code must retain the above copyright
9164190Sjkoshy *    notice, this list of conditions and the following disclaimer.
10164190Sjkoshy * 2. Redistributions in binary form must reproduce the above copyright
11164190Sjkoshy *    notice, this list of conditions and the following disclaimer in the
12164190Sjkoshy *    documentation and/or other materials provided with the distribution.
13164190Sjkoshy *
14164190Sjkoshy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15164190Sjkoshy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16164190Sjkoshy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17164190Sjkoshy * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18164190Sjkoshy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19164190Sjkoshy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20164190Sjkoshy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21164190Sjkoshy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22164190Sjkoshy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23164190Sjkoshy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24164190Sjkoshy * SUCH DAMAGE.
25164190Sjkoshy */
26164190Sjkoshy
27164190Sjkoshy#include <sys/cdefs.h>
28164190Sjkoshy__FBSDID("$FreeBSD$");
29164190Sjkoshy
30164190Sjkoshy#include <assert.h>
31164190Sjkoshy#include <errno.h>
32164190Sjkoshy#include <libelf.h>
33164190Sjkoshy#include <stdlib.h>
34164190Sjkoshy
35164190Sjkoshy#include "_libelf.h"
36164190Sjkoshy
37164190Sjkoshy
38164190SjkoshyElf_Data *
39164190Sjkoshyelf_getdata(Elf_Scn *s, Elf_Data *d)
40164190Sjkoshy{
41164190Sjkoshy	Elf *e;
42164190Sjkoshy	size_t fsz, msz, count;
43164190Sjkoshy	int elfclass, elftype;
44164190Sjkoshy	unsigned int sh_type;
45164190Sjkoshy	uint64_t sh_align, sh_offset, sh_size;
46210338Skaiw	int (*xlate)(char *_d, size_t _dsz, char *_s, size_t _c, int _swap);
47164190Sjkoshy
48164190Sjkoshy	if (s == NULL || (e = s->s_elf) == NULL || e->e_kind != ELF_K_ELF ||
49164190Sjkoshy	    (d != NULL && s != d->d_scn)) {
50164190Sjkoshy		LIBELF_SET_ERROR(ARGUMENT, 0);
51164190Sjkoshy		return (NULL);
52164190Sjkoshy	}
53164190Sjkoshy
54164190Sjkoshy	if (d == NULL && (d = STAILQ_FIRST(&s->s_data)) != NULL)
55164190Sjkoshy		return (d);
56164190Sjkoshy
57164190Sjkoshy	if (d != NULL)
58164190Sjkoshy		return (STAILQ_NEXT(d, d_next));
59164190Sjkoshy
60164190Sjkoshy	if (e->e_rawfile == NULL) {
61164190Sjkoshy		LIBELF_SET_ERROR(SEQUENCE, 0);
62164190Sjkoshy		return (NULL);
63164190Sjkoshy	}
64164190Sjkoshy
65164190Sjkoshy	elfclass = e->e_class;
66164190Sjkoshy
67164190Sjkoshy	assert(elfclass == ELFCLASS32 || elfclass == ELFCLASS64);
68164190Sjkoshy
69164190Sjkoshy	if (elfclass == ELFCLASS32) {
70164190Sjkoshy		sh_type   = s->s_shdr.s_shdr32.sh_type;
71164190Sjkoshy		sh_offset = (uint64_t) s->s_shdr.s_shdr32.sh_offset;
72164190Sjkoshy		sh_size   = (uint64_t) s->s_shdr.s_shdr32.sh_size;
73164190Sjkoshy		sh_align  = (uint64_t) s->s_shdr.s_shdr32.sh_addralign;
74164190Sjkoshy	} else {
75164190Sjkoshy		sh_type   = s->s_shdr.s_shdr64.sh_type;
76164190Sjkoshy		sh_offset = s->s_shdr.s_shdr64.sh_offset;
77164190Sjkoshy		sh_size   = s->s_shdr.s_shdr64.sh_size;
78164190Sjkoshy		sh_align  = s->s_shdr.s_shdr64.sh_addralign;
79164190Sjkoshy	}
80164190Sjkoshy
81246978Smarkj	if (sh_type == SHT_NULL) {
82246978Smarkj		LIBELF_SET_ERROR(SECTION, 0);
83210324Skaiw		return (NULL);
84246978Smarkj	}
85210324Skaiw
86164190Sjkoshy	if ((elftype = _libelf_xlate_shtype(sh_type)) < ELF_T_FIRST ||
87210324Skaiw	    elftype > ELF_T_LAST || (sh_type != SHT_NOBITS &&
88210324Skaiw	    sh_offset + sh_size > (uint64_t) e->e_rawsize)) {
89164190Sjkoshy		LIBELF_SET_ERROR(SECTION, 0);
90164190Sjkoshy		return (NULL);
91164190Sjkoshy	}
92164190Sjkoshy
93210324Skaiw	if ((fsz = (elfclass == ELFCLASS32 ? elf32_fsize : elf64_fsize)
94210324Skaiw            (elftype, (size_t) 1, e->e_version)) == 0) {
95164190Sjkoshy		LIBELF_SET_ERROR(UNIMPL, 0);
96164190Sjkoshy		return (NULL);
97164190Sjkoshy	}
98164190Sjkoshy
99164190Sjkoshy	if (sh_size % fsz) {
100164190Sjkoshy		LIBELF_SET_ERROR(SECTION, 0);
101164190Sjkoshy		return (NULL);
102164190Sjkoshy	}
103164190Sjkoshy
104164190Sjkoshy	count = sh_size / fsz;
105164190Sjkoshy
106164190Sjkoshy	msz = _libelf_msize(elftype, elfclass, e->e_version);
107164190Sjkoshy
108164190Sjkoshy	assert(msz > 0);
109164190Sjkoshy
110164190Sjkoshy	if ((d = _libelf_allocate_data(s)) == NULL)
111164190Sjkoshy		return (NULL);
112164190Sjkoshy
113210324Skaiw	d->d_buf     = NULL;
114164190Sjkoshy	d->d_off     = 0;
115164190Sjkoshy	d->d_align   = sh_align;
116164190Sjkoshy	d->d_size    = msz * count;
117164190Sjkoshy	d->d_type    = elftype;
118164190Sjkoshy	d->d_version = e->e_version;
119164190Sjkoshy
120217833Skan	if (sh_type == SHT_NOBITS || sh_size == 0) {
121217833Skan	        STAILQ_INSERT_TAIL(&s->s_data, d, d_next);
122210324Skaiw		return (d);
123217833Skan        }
124210324Skaiw
125210324Skaiw	if ((d->d_buf = malloc(msz*count)) == NULL) {
126210324Skaiw		(void) _libelf_release_data(d);
127210324Skaiw		LIBELF_SET_ERROR(RESOURCE, 0);
128210324Skaiw		return (NULL);
129210324Skaiw	}
130210324Skaiw
131164190Sjkoshy	d->d_flags  |= LIBELF_F_MALLOCED;
132164190Sjkoshy
133164190Sjkoshy	xlate = _libelf_get_translator(elftype, ELF_TOMEMORY, elfclass);
134210338Skaiw	if (!(*xlate)(d->d_buf, d->d_size, e->e_rawfile + sh_offset, count,
135210338Skaiw	    e->e_byteorder != LIBELF_PRIVATE(byteorder))) {
136210338Skaiw		_libelf_release_data(d);
137210338Skaiw		LIBELF_SET_ERROR(DATA, 0);
138210338Skaiw		return (NULL);
139210338Skaiw	}
140164190Sjkoshy
141210338Skaiw	STAILQ_INSERT_TAIL(&s->s_data, d, d_next);
142210338Skaiw
143164190Sjkoshy	return (d);
144164190Sjkoshy}
145164190Sjkoshy
146164190SjkoshyElf_Data *
147164190Sjkoshyelf_newdata(Elf_Scn *s)
148164190Sjkoshy{
149164190Sjkoshy	Elf *e;
150164190Sjkoshy	Elf_Data *d;
151164190Sjkoshy
152164190Sjkoshy	if (s == NULL || (e = s->s_elf) == NULL ||
153164190Sjkoshy	    e->e_kind != ELF_K_ELF) {
154164190Sjkoshy		LIBELF_SET_ERROR(ARGUMENT, 0);
155164190Sjkoshy		return (NULL);
156164190Sjkoshy	}
157164190Sjkoshy
158164190Sjkoshy	/*
159164190Sjkoshy	 * elf_newdata() has to append a data descriptor, so
160164190Sjkoshy	 * bring in existing section data if not already present.
161164190Sjkoshy	 */
162164190Sjkoshy	if (e->e_rawfile && s->s_size > 0 && STAILQ_EMPTY(&s->s_data))
163164190Sjkoshy		if (elf_getdata(s, NULL) == NULL)
164164190Sjkoshy			return (NULL);
165164190Sjkoshy
166210324Skaiw	if ((d = _libelf_allocate_data(s)) == NULL)
167164190Sjkoshy		return (NULL);
168164190Sjkoshy
169164190Sjkoshy	STAILQ_INSERT_TAIL(&s->s_data, d, d_next);
170164190Sjkoshy
171164190Sjkoshy	d->d_align = 1;
172164190Sjkoshy	d->d_buf = NULL;
173164190Sjkoshy	d->d_off = (uint64_t) ~0;
174164190Sjkoshy	d->d_size = 0;
175164190Sjkoshy	d->d_type = ELF_T_BYTE;
176164190Sjkoshy	d->d_version = LIBELF_PRIVATE(version);
177164190Sjkoshy
178164190Sjkoshy	(void) elf_flagscn(s, ELF_C_SET, ELF_F_DIRTY);
179164190Sjkoshy
180164190Sjkoshy	return (d);
181164190Sjkoshy}
182164190Sjkoshy
183164190Sjkoshy/*
184164190Sjkoshy * Retrieve a data descriptor for raw (untranslated) data for section
185164190Sjkoshy * `s'.
186164190Sjkoshy */
187164190Sjkoshy
188164190SjkoshyElf_Data *
189164190Sjkoshyelf_rawdata(Elf_Scn *s, Elf_Data *d)
190164190Sjkoshy{
191164190Sjkoshy	Elf *e;
192164190Sjkoshy	int elf_class;
193210324Skaiw	uint32_t sh_type;
194164190Sjkoshy	uint64_t sh_align, sh_offset, sh_size;
195164190Sjkoshy
196164190Sjkoshy	if (s == NULL || (e = s->s_elf) == NULL ||
197164190Sjkoshy	    e->e_kind != ELF_K_ELF || e->e_rawfile == NULL) {
198164190Sjkoshy		LIBELF_SET_ERROR(ARGUMENT, 0);
199164190Sjkoshy		return (NULL);
200164190Sjkoshy	}
201164190Sjkoshy
202164190Sjkoshy	if (d == NULL && (d = STAILQ_FIRST(&s->s_rawdata)) != NULL)
203164190Sjkoshy		return (d);
204164190Sjkoshy
205164190Sjkoshy	if (d != NULL)
206164190Sjkoshy		return (STAILQ_NEXT(d, d_next));
207164190Sjkoshy
208164190Sjkoshy	elf_class = e->e_class;
209164190Sjkoshy
210164190Sjkoshy	assert(elf_class == ELFCLASS32 || elf_class == ELFCLASS64);
211164190Sjkoshy
212164190Sjkoshy	if (elf_class == ELFCLASS32) {
213210324Skaiw		sh_type   = s->s_shdr.s_shdr32.sh_type;
214164190Sjkoshy		sh_offset = (uint64_t) s->s_shdr.s_shdr32.sh_offset;
215164190Sjkoshy		sh_size   = (uint64_t) s->s_shdr.s_shdr32.sh_size;
216164190Sjkoshy		sh_align  = (uint64_t) s->s_shdr.s_shdr32.sh_addralign;
217164190Sjkoshy	} else {
218210324Skaiw		sh_type   = s->s_shdr.s_shdr64.sh_type;
219167687Sjkoshy		sh_offset = s->s_shdr.s_shdr64.sh_offset;
220167687Sjkoshy		sh_size   = s->s_shdr.s_shdr64.sh_size;
221167687Sjkoshy		sh_align  = s->s_shdr.s_shdr64.sh_addralign;
222164190Sjkoshy	}
223164190Sjkoshy
224246978Smarkj	if (sh_type == SHT_NULL) {
225246978Smarkj		LIBELF_SET_ERROR(SECTION, 0);
226210324Skaiw		return (NULL);
227246978Smarkj	}
228210324Skaiw
229164190Sjkoshy	if ((d = _libelf_allocate_data(s)) == NULL)
230164190Sjkoshy		return (NULL);
231164190Sjkoshy
232221595Skaiw	d->d_buf     = (sh_type == SHT_NOBITS || sh_size == 0) ? NULL :
233221595Skaiw	    e->e_rawfile + sh_offset;
234164190Sjkoshy	d->d_off     = 0;
235164190Sjkoshy	d->d_align   = sh_align;
236164190Sjkoshy	d->d_size    = sh_size;
237164190Sjkoshy	d->d_type    = ELF_T_BYTE;
238164190Sjkoshy	d->d_version = e->e_version;
239164190Sjkoshy
240164190Sjkoshy	STAILQ_INSERT_TAIL(&s->s_rawdata, d, d_next);
241164190Sjkoshy
242164190Sjkoshy	return (d);
243164190Sjkoshy}
244