xref: /freebsd/contrib/elftoolchain/libelf/elf_data.c (revision 57718be8fa0bd5edc11ab9a72e68cc71982939a6)
1 /*-
2  * Copyright (c) 2006,2008,2011 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 <assert.h>
28 #include <errno.h>
29 #include <libelf.h>
30 #include <stdlib.h>
31 
32 #include "_libelf.h"
33 
34 ELFTC_VCSID("$Id: elf_data.c 2921 2013-03-04 16:19:22Z jkoshy $");
35 
36 Elf_Data *
37 elf_getdata(Elf_Scn *s, Elf_Data *ed)
38 {
39 	Elf *e;
40 	unsigned int sh_type;
41 	int elfclass, elftype;
42 	size_t fsz, msz, count;
43 	struct _Libelf_Data *d;
44 	uint64_t sh_align, sh_offset, sh_size;
45 	int (*xlate)(char *_d, size_t _dsz, char *_s, size_t _c, int _swap);
46 
47 	d = (struct _Libelf_Data *) ed;
48 
49 	if (s == NULL || (e = s->s_elf) == NULL ||
50 	    (d != NULL && s != d->d_scn)) {
51 		LIBELF_SET_ERROR(ARGUMENT, 0);
52 		return (NULL);
53 	}
54 
55 	assert(e->e_kind == ELF_K_ELF);
56 
57 	if (d == NULL && (d = STAILQ_FIRST(&s->s_data)) != NULL)
58 		return (&d->d_data);
59 
60 	if (d != NULL)
61 		return (&STAILQ_NEXT(d, d_next)->d_data);
62 
63 	if (e->e_rawfile == NULL) {
64 		/*
65 		 * In the ELF_C_WRITE case, there is no source that
66 		 * can provide data for the section.
67 		 */
68 		LIBELF_SET_ERROR(ARGUMENT, 0);
69 		return (NULL);
70 	}
71 
72 	elfclass = e->e_class;
73 
74 	assert(elfclass == ELFCLASS32 || elfclass == ELFCLASS64);
75 
76 	if (elfclass == ELFCLASS32) {
77 		sh_type   = s->s_shdr.s_shdr32.sh_type;
78 		sh_offset = (uint64_t) s->s_shdr.s_shdr32.sh_offset;
79 		sh_size   = (uint64_t) s->s_shdr.s_shdr32.sh_size;
80 		sh_align  = (uint64_t) s->s_shdr.s_shdr32.sh_addralign;
81 	} else {
82 		sh_type   = s->s_shdr.s_shdr64.sh_type;
83 		sh_offset = s->s_shdr.s_shdr64.sh_offset;
84 		sh_size   = s->s_shdr.s_shdr64.sh_size;
85 		sh_align  = s->s_shdr.s_shdr64.sh_addralign;
86 	}
87 
88 	if (sh_type == SHT_NULL) {
89 		LIBELF_SET_ERROR(SECTION, 0);
90 		return (NULL);
91 	}
92 
93 	if ((elftype = _libelf_xlate_shtype(sh_type)) < ELF_T_FIRST ||
94 	    elftype > ELF_T_LAST || (sh_type != SHT_NOBITS &&
95 	    sh_offset + sh_size > (uint64_t) e->e_rawsize)) {
96 		LIBELF_SET_ERROR(SECTION, 0);
97 		return (NULL);
98 	}
99 
100 	if ((fsz = (elfclass == ELFCLASS32 ? elf32_fsize : elf64_fsize)
101             (elftype, (size_t) 1, e->e_version)) == 0) {
102 		LIBELF_SET_ERROR(UNIMPL, 0);
103 		return (NULL);
104 	}
105 
106 	if (sh_size % fsz) {
107 		LIBELF_SET_ERROR(SECTION, 0);
108 		return (NULL);
109 	}
110 
111 	count = sh_size / fsz;
112 
113 	msz = _libelf_msize(elftype, elfclass, e->e_version);
114 
115 	assert(msz > 0);
116 
117 	if ((d = _libelf_allocate_data(s)) == NULL)
118 		return (NULL);
119 
120 	d->d_data.d_buf     = NULL;
121 	d->d_data.d_off     = 0;
122 	d->d_data.d_align   = sh_align;
123 	d->d_data.d_size    = msz * count;
124 	d->d_data.d_type    = elftype;
125 	d->d_data.d_version = e->e_version;
126 
127 	if (sh_type == SHT_NOBITS || sh_size == 0) {
128 	        STAILQ_INSERT_TAIL(&s->s_data, d, d_next);
129 		return (&d->d_data);
130         }
131 
132 	if ((d->d_data.d_buf = malloc(msz*count)) == NULL) {
133 		(void) _libelf_release_data(d);
134 		LIBELF_SET_ERROR(RESOURCE, 0);
135 		return (NULL);
136 	}
137 
138 	d->d_flags  |= LIBELF_F_DATA_MALLOCED;
139 
140 	xlate = _libelf_get_translator(elftype, ELF_TOMEMORY, elfclass);
141 	if (!(*xlate)(d->d_data.d_buf, d->d_data.d_size,
142 	    e->e_rawfile + sh_offset, count,
143 	    e->e_byteorder != LIBELF_PRIVATE(byteorder))) {
144 		_libelf_release_data(d);
145 		LIBELF_SET_ERROR(DATA, 0);
146 		return (NULL);
147 	}
148 
149 	STAILQ_INSERT_TAIL(&s->s_data, d, d_next);
150 
151 	return (&d->d_data);
152 }
153 
154 Elf_Data *
155 elf_newdata(Elf_Scn *s)
156 {
157 	Elf *e;
158 	struct _Libelf_Data *d;
159 
160 	if (s == NULL || (e = s->s_elf) == NULL) {
161 		LIBELF_SET_ERROR(ARGUMENT, 0);
162 		return (NULL);
163 	}
164 
165 	assert(e->e_kind == ELF_K_ELF);
166 
167 	/*
168 	 * elf_newdata() has to append a data descriptor, so
169 	 * bring in existing section data if not already present.
170 	 */
171 	if (e->e_rawfile && s->s_size > 0 && STAILQ_EMPTY(&s->s_data))
172 		if (elf_getdata(s, NULL) == NULL)
173 			return (NULL);
174 
175 	if ((d = _libelf_allocate_data(s)) == NULL)
176 		return (NULL);
177 
178 	STAILQ_INSERT_TAIL(&s->s_data, d, d_next);
179 
180 	d->d_data.d_align = 1;
181 	d->d_data.d_buf = NULL;
182 	d->d_data.d_off = (uint64_t) ~0;
183 	d->d_data.d_size = 0;
184 	d->d_data.d_type = ELF_T_BYTE;
185 	d->d_data.d_version = LIBELF_PRIVATE(version);
186 
187 	(void) elf_flagscn(s, ELF_C_SET, ELF_F_DIRTY);
188 
189 	return (&d->d_data);
190 }
191 
192 /*
193  * Retrieve a data descriptor for raw (untranslated) data for section
194  * `s'.
195  */
196 
197 Elf_Data *
198 elf_rawdata(Elf_Scn *s, Elf_Data *ed)
199 {
200 	Elf *e;
201 	int elf_class;
202 	uint32_t sh_type;
203 	struct _Libelf_Data *d;
204 	uint64_t sh_align, sh_offset, sh_size;
205 
206 	if (s == NULL || (e = s->s_elf) == NULL || e->e_rawfile == NULL) {
207 		LIBELF_SET_ERROR(ARGUMENT, 0);
208 		return (NULL);
209 	}
210 
211 	assert(e->e_kind == ELF_K_ELF);
212 
213 	d = (struct _Libelf_Data *) ed;
214 
215 	if (d == NULL && (d = STAILQ_FIRST(&s->s_rawdata)) != NULL)
216 		return (&d->d_data);
217 
218 	if (d != NULL)
219 		return (&STAILQ_NEXT(d, d_next)->d_data);
220 
221 	elf_class = e->e_class;
222 
223 	assert(elf_class == ELFCLASS32 || elf_class == ELFCLASS64);
224 
225 	if (elf_class == ELFCLASS32) {
226 		sh_type   = s->s_shdr.s_shdr32.sh_type;
227 		sh_offset = (uint64_t) s->s_shdr.s_shdr32.sh_offset;
228 		sh_size   = (uint64_t) s->s_shdr.s_shdr32.sh_size;
229 		sh_align  = (uint64_t) s->s_shdr.s_shdr32.sh_addralign;
230 	} else {
231 		sh_type   = s->s_shdr.s_shdr64.sh_type;
232 		sh_offset = s->s_shdr.s_shdr64.sh_offset;
233 		sh_size   = s->s_shdr.s_shdr64.sh_size;
234 		sh_align  = s->s_shdr.s_shdr64.sh_addralign;
235 	}
236 
237 	if (sh_type == SHT_NULL) {
238 		LIBELF_SET_ERROR(SECTION, 0);
239 		return (NULL);
240 	}
241 
242 	if ((d = _libelf_allocate_data(s)) == NULL)
243 		return (NULL);
244 
245 	d->d_data.d_buf = (sh_type == SHT_NOBITS || sh_size == 0) ? NULL :
246 	    e->e_rawfile + sh_offset;
247 	d->d_data.d_off     = 0;
248 	d->d_data.d_align   = sh_align;
249 	d->d_data.d_size    = sh_size;
250 	d->d_data.d_type    = ELF_T_BYTE;
251 	d->d_data.d_version = e->e_version;
252 
253 	STAILQ_INSERT_TAIL(&s->s_rawdata, d, d_next);
254 
255 	return (&d->d_data);
256 }
257