Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / libelf / dist / elf_data.c
blobcaa1025805e449c93c9de11474778f5609a889a3
1 /* $NetBSD: elf_data.c,v 1.2 2009/12/20 23:23:46 thorpej Exp $ */
3 /*-
4 * Copyright (c) 2006 Joseph Koshy
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 /* __FBSDID("$FreeBSD: src/lib/libelf/elf_data.c,v 1.2.10.1.2.1 2009/10/25 01:10:29 kensmith Exp $"); */
32 #include <assert.h>
33 #include <errno.h>
34 #include <libelf.h>
35 #include <stdlib.h>
37 #include "_libelf.h"
40 Elf_Data *
41 elf_getdata(Elf_Scn *s, Elf_Data *d)
43 Elf *e;
44 char *dst;
45 size_t fsz, msz, count;
46 int elfclass, elftype;
47 unsigned int sh_type;
48 uint64_t sh_align, sh_offset, sh_size;
49 void (*xlate)(char *_d, char *_s, size_t _c, int _swap);
51 if (s == NULL || (e = s->s_elf) == NULL || e->e_kind != ELF_K_ELF ||
52 (d != NULL && s != d->d_scn)) {
53 LIBELF_SET_ERROR(ARGUMENT, 0);
54 return (NULL);
57 if (d == NULL && (d = STAILQ_FIRST(&s->s_data)) != NULL)
58 return (d);
60 if (d != NULL)
61 return (STAILQ_NEXT(d, d_next));
63 if (e->e_rawfile == NULL) {
64 LIBELF_SET_ERROR(SEQUENCE, 0);
65 return (NULL);
68 elfclass = e->e_class;
70 assert(elfclass == ELFCLASS32 || elfclass == ELFCLASS64);
72 if (elfclass == ELFCLASS32) {
73 sh_type = s->s_shdr.s_shdr32.sh_type;
74 sh_offset = (uint64_t) s->s_shdr.s_shdr32.sh_offset;
75 sh_size = (uint64_t) s->s_shdr.s_shdr32.sh_size;
76 sh_align = (uint64_t) s->s_shdr.s_shdr32.sh_addralign;
77 } else {
78 sh_type = s->s_shdr.s_shdr64.sh_type;
79 sh_offset = s->s_shdr.s_shdr64.sh_offset;
80 sh_size = s->s_shdr.s_shdr64.sh_size;
81 sh_align = s->s_shdr.s_shdr64.sh_addralign;
84 if ((elftype = _libelf_xlate_shtype(sh_type)) < ELF_T_FIRST ||
85 elftype > ELF_T_LAST ||
86 sh_offset + sh_size > (uint64_t) e->e_rawsize) {
87 LIBELF_SET_ERROR(SECTION, 0);
88 return (NULL);
91 if ((fsz = (elfclass == ELFCLASS32 ? elf32_fsize : elf64_fsize)(elftype,
92 (size_t) 1, e->e_version)) == 0) {
93 LIBELF_SET_ERROR(UNIMPL, 0);
94 return (NULL);
98 if (sh_size % fsz) {
99 LIBELF_SET_ERROR(SECTION, 0);
100 return (NULL);
103 count = sh_size / fsz;
105 msz = _libelf_msize(elftype, elfclass, e->e_version);
107 assert(msz > 0);
109 if ((dst = malloc(msz*count)) == NULL) {
110 LIBELF_SET_ERROR(RESOURCE, 0);
111 return (NULL);
114 if ((d = _libelf_allocate_data(s)) == NULL)
115 return (NULL);
117 d->d_buf = dst;
118 d->d_off = 0;
119 d->d_align = sh_align;
120 d->d_size = msz * count;
121 d->d_type = elftype;
122 d->d_version = e->e_version;
124 d->d_flags |= LIBELF_F_MALLOCED;
125 STAILQ_INSERT_TAIL(&s->s_data, d, d_next);
127 xlate = _libelf_get_translator(elftype, ELF_TOMEMORY, elfclass);
128 (*xlate)(d->d_buf, e->e_rawfile + sh_offset, count, e->e_byteorder !=
129 _libelf_host_byteorder());
131 return (d);
134 Elf_Data *
135 elf_newdata(Elf_Scn *s)
137 Elf *e;
138 Elf_Data *d;
140 if (s == NULL || (e = s->s_elf) == NULL ||
141 e->e_kind != ELF_K_ELF) {
142 LIBELF_SET_ERROR(ARGUMENT, 0);
143 return (NULL);
147 * elf_newdata() has to append a data descriptor, so
148 * bring in existing section data if not already present.
150 if (e->e_rawfile && s->s_size > 0 && STAILQ_EMPTY(&s->s_data))
151 if (elf_getdata(s, NULL) == NULL)
152 return (NULL);
154 if ((d = malloc(sizeof(Elf_Data))) == NULL) {
155 LIBELF_SET_ERROR(RESOURCE, errno);
156 return (NULL);
159 STAILQ_INSERT_TAIL(&s->s_data, d, d_next);
160 d->d_flags = 0;
161 d->d_scn = s;
163 d->d_align = 1;
164 d->d_buf = NULL;
165 d->d_off = (uint64_t) ~0;
166 d->d_size = 0;
167 d->d_type = ELF_T_BYTE;
168 d->d_version = LIBELF_PRIVATE(version);
170 (void) elf_flagscn(s, ELF_C_SET, ELF_F_DIRTY);
172 return (d);
176 * Retrieve a data descriptor for raw (untranslated) data for section
177 * `s'.
180 Elf_Data *
181 elf_rawdata(Elf_Scn *s, Elf_Data *d)
183 Elf *e;
184 int elf_class;
185 uint64_t sh_align, sh_offset, sh_size;
187 if (s == NULL || (e = s->s_elf) == NULL ||
188 e->e_kind != ELF_K_ELF || e->e_rawfile == NULL) {
189 LIBELF_SET_ERROR(ARGUMENT, 0);
190 return (NULL);
193 if (d == NULL && (d = STAILQ_FIRST(&s->s_rawdata)) != NULL)
194 return (d);
196 if (d != NULL)
197 return (STAILQ_NEXT(d, d_next));
199 elf_class = e->e_class;
201 assert(elf_class == ELFCLASS32 || elf_class == ELFCLASS64);
203 if (elf_class == ELFCLASS32) {
204 sh_offset = (uint64_t) s->s_shdr.s_shdr32.sh_offset;
205 sh_size = (uint64_t) s->s_shdr.s_shdr32.sh_size;
206 sh_align = (uint64_t) s->s_shdr.s_shdr32.sh_addralign;
207 } else {
208 sh_offset = s->s_shdr.s_shdr64.sh_offset;
209 sh_size = s->s_shdr.s_shdr64.sh_size;
210 sh_align = s->s_shdr.s_shdr64.sh_addralign;
213 if ((d = _libelf_allocate_data(s)) == NULL)
214 return (NULL);
216 d->d_buf = e->e_rawfile + sh_offset;
217 d->d_off = 0;
218 d->d_align = sh_align;
219 d->d_size = sh_size;
220 d->d_type = ELF_T_BYTE;
221 d->d_version = e->e_version;
223 STAILQ_INSERT_TAIL(&s->s_rawdata, d, d_next);
225 return (d);