elf_data.c revision 164190
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: head/lib/libelf/elf_data.c 164190 2006-11-11 17:16:35Z jkoshy $");
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	char *dst;
43164190Sjkoshy	size_t fsz, msz, count;
44164190Sjkoshy	int elfclass, elftype;
45164190Sjkoshy	unsigned int sh_type;
46164190Sjkoshy	uint64_t sh_align, sh_offset, sh_size;
47164190Sjkoshy	void (*xlate)(char *_d, char *_s, size_t _c, int _swap);
48164190Sjkoshy
49164190Sjkoshy	if (s == NULL || (e = s->s_elf) == NULL || e->e_kind != ELF_K_ELF ||
50164190Sjkoshy	    (d != NULL && s != d->d_scn)) {
51164190Sjkoshy		LIBELF_SET_ERROR(ARGUMENT, 0);
52164190Sjkoshy		return (NULL);
53164190Sjkoshy	}
54164190Sjkoshy
55164190Sjkoshy	if (d == NULL && (d = STAILQ_FIRST(&s->s_data)) != NULL)
56164190Sjkoshy		return (d);
57164190Sjkoshy
58164190Sjkoshy	if (d != NULL)
59164190Sjkoshy		return (STAILQ_NEXT(d, d_next));
60164190Sjkoshy
61164190Sjkoshy	if (e->e_rawfile == NULL) {
62164190Sjkoshy		LIBELF_SET_ERROR(SEQUENCE, 0);
63164190Sjkoshy		return (NULL);
64164190Sjkoshy	}
65164190Sjkoshy
66164190Sjkoshy	elfclass = e->e_class;
67164190Sjkoshy
68164190Sjkoshy	assert(elfclass == ELFCLASS32 || elfclass == ELFCLASS64);
69164190Sjkoshy
70164190Sjkoshy	if (elfclass == ELFCLASS32) {
71164190Sjkoshy		sh_type   = s->s_shdr.s_shdr32.sh_type;
72164190Sjkoshy		sh_offset = (uint64_t) s->s_shdr.s_shdr32.sh_offset;
73164190Sjkoshy		sh_size   = (uint64_t) s->s_shdr.s_shdr32.sh_size;
74164190Sjkoshy		sh_align  = (uint64_t) s->s_shdr.s_shdr32.sh_addralign;
75164190Sjkoshy	} else {
76164190Sjkoshy		sh_type   = s->s_shdr.s_shdr64.sh_type;
77164190Sjkoshy		sh_offset = s->s_shdr.s_shdr64.sh_offset;
78164190Sjkoshy		sh_size   = s->s_shdr.s_shdr64.sh_size;
79164190Sjkoshy		sh_align  = s->s_shdr.s_shdr64.sh_addralign;
80164190Sjkoshy	}
81164190Sjkoshy
82164190Sjkoshy	if ((elftype = _libelf_xlate_shtype(sh_type)) < ELF_T_FIRST ||
83164190Sjkoshy	    elftype > ELF_T_LAST ||
84164190Sjkoshy	    sh_offset + sh_size > (uint64_t) e->e_rawsize) {
85164190Sjkoshy		LIBELF_SET_ERROR(SECTION, 0);
86164190Sjkoshy		return (NULL);
87164190Sjkoshy	}
88164190Sjkoshy
89164190Sjkoshy	if ((fsz = (elfclass == ELFCLASS32 ? elf32_fsize : elf64_fsize)(elftype,
90164190Sjkoshy		 (size_t) 1, e->e_version)) == 0) {
91164190Sjkoshy		LIBELF_SET_ERROR(UNIMPL, 0);
92164190Sjkoshy		return (NULL);
93164190Sjkoshy	}
94164190Sjkoshy
95164190Sjkoshy
96164190Sjkoshy	if (sh_size % fsz) {
97164190Sjkoshy		LIBELF_SET_ERROR(SECTION, 0);
98164190Sjkoshy		return (NULL);
99164190Sjkoshy	}
100164190Sjkoshy
101164190Sjkoshy	count = sh_size / fsz;
102164190Sjkoshy
103164190Sjkoshy	msz = _libelf_msize(elftype, elfclass, e->e_version);
104164190Sjkoshy
105164190Sjkoshy	assert(msz > 0);
106164190Sjkoshy
107164190Sjkoshy	if ((dst = malloc(msz*count)) == NULL) {
108164190Sjkoshy		LIBELF_SET_ERROR(RESOURCE, 0);
109164190Sjkoshy		return (NULL);
110164190Sjkoshy	}
111164190Sjkoshy
112164190Sjkoshy	if ((d = _libelf_allocate_data(s)) == NULL)
113164190Sjkoshy		return (NULL);
114164190Sjkoshy
115164190Sjkoshy	d->d_buf     = dst;
116164190Sjkoshy	d->d_off     = 0;
117164190Sjkoshy	d->d_align   = sh_align;
118164190Sjkoshy	d->d_size    = msz * count;
119164190Sjkoshy	d->d_type    = elftype;
120164190Sjkoshy	d->d_version = e->e_version;
121164190Sjkoshy
122164190Sjkoshy	d->d_flags  |= LIBELF_F_MALLOCED;
123164190Sjkoshy	STAILQ_INSERT_TAIL(&s->s_data, d, d_next);
124164190Sjkoshy
125164190Sjkoshy	xlate = _libelf_get_translator(elftype, ELF_TOMEMORY, elfclass);
126164190Sjkoshy	(*xlate)(d->d_buf, e->e_rawfile + sh_offset, count, e->e_byteorder !=
127164190Sjkoshy	    LIBELF_PRIVATE(byteorder));
128164190Sjkoshy
129164190Sjkoshy	return (d);
130164190Sjkoshy}
131164190Sjkoshy
132164190SjkoshyElf_Data *
133164190Sjkoshyelf_newdata(Elf_Scn *s)
134164190Sjkoshy{
135164190Sjkoshy	Elf *e;
136164190Sjkoshy	Elf_Data *d;
137164190Sjkoshy
138164190Sjkoshy	if (s == NULL || (e = s->s_elf) == NULL ||
139164190Sjkoshy	    e->e_kind != ELF_K_ELF) {
140164190Sjkoshy		LIBELF_SET_ERROR(ARGUMENT, 0);
141164190Sjkoshy		return (NULL);
142164190Sjkoshy	}
143164190Sjkoshy
144164190Sjkoshy	/*
145164190Sjkoshy	 * elf_newdata() has to append a data descriptor, so
146164190Sjkoshy	 * bring in existing section data if not already present.
147164190Sjkoshy	 */
148164190Sjkoshy	if (e->e_rawfile && s->s_size > 0 && STAILQ_EMPTY(&s->s_data))
149164190Sjkoshy		if (elf_getdata(s, NULL) == NULL)
150164190Sjkoshy			return (NULL);
151164190Sjkoshy
152164190Sjkoshy	if ((d = malloc(sizeof(Elf_Data))) == NULL) {
153164190Sjkoshy		LIBELF_SET_ERROR(RESOURCE, errno);
154164190Sjkoshy		return (NULL);
155164190Sjkoshy	}
156164190Sjkoshy
157164190Sjkoshy	STAILQ_INSERT_TAIL(&s->s_data, d, d_next);
158164190Sjkoshy	d->d_flags = 0;
159164190Sjkoshy	d->d_scn = s;
160164190Sjkoshy
161164190Sjkoshy	d->d_align = 1;
162164190Sjkoshy	d->d_buf = NULL;
163164190Sjkoshy	d->d_off = (uint64_t) ~0;
164164190Sjkoshy	d->d_size = 0;
165164190Sjkoshy	d->d_type = ELF_T_BYTE;
166164190Sjkoshy	d->d_version = LIBELF_PRIVATE(version);
167164190Sjkoshy
168164190Sjkoshy	(void) elf_flagscn(s, ELF_C_SET, ELF_F_DIRTY);
169164190Sjkoshy
170164190Sjkoshy	return (d);
171164190Sjkoshy}
172164190Sjkoshy
173164190Sjkoshy/*
174164190Sjkoshy * Retrieve a data descriptor for raw (untranslated) data for section
175164190Sjkoshy * `s'.
176164190Sjkoshy */
177164190Sjkoshy
178164190SjkoshyElf_Data *
179164190Sjkoshyelf_rawdata(Elf_Scn *s, Elf_Data *d)
180164190Sjkoshy{
181164190Sjkoshy	Elf *e;
182164190Sjkoshy	int elf_class;
183164190Sjkoshy	uint64_t sh_align, sh_offset, sh_size;
184164190Sjkoshy
185164190Sjkoshy	if (s == NULL || (e = s->s_elf) == NULL ||
186164190Sjkoshy	    e->e_kind != ELF_K_ELF || e->e_rawfile == NULL) {
187164190Sjkoshy		LIBELF_SET_ERROR(ARGUMENT, 0);
188164190Sjkoshy		return (NULL);
189164190Sjkoshy	}
190164190Sjkoshy
191164190Sjkoshy	if (d == NULL && (d = STAILQ_FIRST(&s->s_rawdata)) != NULL)
192164190Sjkoshy		return (d);
193164190Sjkoshy
194164190Sjkoshy	if (d != NULL)
195164190Sjkoshy		return (STAILQ_NEXT(d, d_next));
196164190Sjkoshy
197164190Sjkoshy	elf_class = e->e_class;
198164190Sjkoshy
199164190Sjkoshy	assert(elf_class == ELFCLASS32 || elf_class == ELFCLASS64);
200164190Sjkoshy
201164190Sjkoshy	if (elf_class == ELFCLASS32) {
202164190Sjkoshy		sh_offset = (uint64_t) s->s_shdr.s_shdr32.sh_offset;
203164190Sjkoshy		sh_size   = (uint64_t) s->s_shdr.s_shdr32.sh_size;
204164190Sjkoshy		sh_align  = (uint64_t) s->s_shdr.s_shdr32.sh_addralign;
205164190Sjkoshy	} else {
206164190Sjkoshy		sh_offset = s->s_shdr.s_shdr32.sh_offset;
207164190Sjkoshy		sh_size   = s->s_shdr.s_shdr32.sh_size;
208164190Sjkoshy		sh_align  = s->s_shdr.s_shdr32.sh_addralign;
209164190Sjkoshy	}
210164190Sjkoshy
211164190Sjkoshy	if ((d = _libelf_allocate_data(s)) == NULL)
212164190Sjkoshy		return (NULL);
213164190Sjkoshy
214164190Sjkoshy	d->d_buf     = e->e_rawfile + sh_offset;
215164190Sjkoshy	d->d_off     = 0;
216164190Sjkoshy	d->d_align   = sh_align;
217164190Sjkoshy	d->d_size    = sh_size;
218164190Sjkoshy	d->d_type    = ELF_T_BYTE;
219164190Sjkoshy	d->d_version = e->e_version;
220164190Sjkoshy
221164190Sjkoshy	STAILQ_INSERT_TAIL(&s->s_rawdata, d, d_next);
222164190Sjkoshy
223164190Sjkoshy	return (d);
224164190Sjkoshy}
225