Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / libelf / dist / libelf_allocate.c
blob26e5a11d6d04569090c8310a0ff1ba8fd85c5409
1 /* $NetBSD: libelf_allocate.c,v 1.3 2009/12/19 06:22:25 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.
30 * Internal APIs
33 #include <sys/cdefs.h>
34 /* __FBSDID("$FreeBSD: src/lib/libelf/libelf_allocate.c,v 1.2.10.1.2.1 2009/10/25 01:10:29 kensmith Exp $"); */
36 #include <assert.h>
37 #include <errno.h>
38 #include <libelf.h>
39 #include <stdlib.h>
40 #include <string.h>
42 #include "_libelf.h"
44 Elf *
45 _libelf_allocate_elf(void)
47 Elf *e;
49 if ((e = malloc(sizeof(*e))) == NULL) {
50 LIBELF_SET_ERROR(RESOURCE, errno);
51 return NULL;
54 e->e_activations = 1;
55 e->e_arhdr = NULL;
56 e->e_byteorder = ELFDATANONE;
57 e->e_class = ELFCLASSNONE;
58 e->e_cmd = ELF_C_NULL;
59 e->e_fd = -1;
60 e->e_flags = 0;
61 e->e_kind = ELF_K_NONE;
62 e->e_parent = NULL;
63 e->e_rawfile = NULL;
64 e->e_rawsize = 0;
65 e->e_version = LIBELF_PRIVATE(version);
67 (void) memset(&e->e_u, 0, sizeof(e->e_u));
69 return (e);
72 void
73 _libelf_init_elf(Elf *e, Elf_Kind kind)
75 assert(e != NULL);
76 assert(e->e_kind == ELF_K_NONE);
78 e->e_kind = kind;
80 switch (kind) {
81 case ELF_K_ELF:
82 STAILQ_INIT(&e->e_u.e_elf.e_scn);
83 break;
84 default:
85 break;
89 #define FREE(P) do { \
90 if (P) \
91 free(P); \
92 } while (/*CONSTCOND*/0)
95 Elf *
96 _libelf_release_elf(Elf *e)
98 switch (e->e_kind) {
99 case ELF_K_AR:
100 FREE(e->e_u.e_ar.e_symtab);
101 break;
103 case ELF_K_ELF:
104 switch (e->e_class) {
105 case ELFCLASS32:
106 FREE(e->e_u.e_elf.e_ehdr.e_ehdr32);
107 FREE(e->e_u.e_elf.e_phdr.e_phdr32);
108 break;
109 case ELFCLASS64:
110 FREE(e->e_u.e_elf.e_ehdr.e_ehdr64);
111 FREE(e->e_u.e_elf.e_phdr.e_phdr64);
112 break;
115 assert(STAILQ_EMPTY(&e->e_u.e_elf.e_scn));
117 if (e->e_arhdr) {
118 FREE(e->e_arhdr->ar_name);
119 FREE(e->e_arhdr->ar_rawname);
120 free(e->e_arhdr);
123 break;
125 default:
126 break;
129 free(e);
131 return (NULL);
134 Elf_Data *
135 _libelf_allocate_data(Elf_Scn *s)
137 Elf_Data *d;
139 if ((d = calloc((size_t) 1, sizeof(Elf_Data))) == NULL) {
140 LIBELF_SET_ERROR(RESOURCE, 0);
141 return (NULL);
144 d->d_scn = s;
146 return (d);
149 Elf_Data *
150 _libelf_release_data(Elf_Data *d)
153 if (d->d_flags & LIBELF_F_MALLOCED)
154 free(d->d_buf);
156 free(d);
158 return (NULL);
161 Elf_Scn *
162 _libelf_allocate_scn(Elf *e, size_t ndx)
164 Elf_Scn *s;
166 if ((s = calloc((size_t) 1, sizeof(Elf_Scn))) == NULL) {
167 LIBELF_SET_ERROR(RESOURCE, errno);
168 return (NULL);
171 s->s_elf = e;
172 s->s_ndx = ndx;
174 STAILQ_INIT(&s->s_data);
175 STAILQ_INIT(&s->s_rawdata);
177 STAILQ_INSERT_TAIL(&e->e_u.e_elf.e_scn, s, s_next);
179 return (s);
182 Elf_Scn *
183 _libelf_release_scn(Elf_Scn *s)
185 Elf *e;
186 Elf_Data *d, *td;
188 assert(s != NULL);
190 STAILQ_FOREACH_SAFE(d, &s->s_data, d_next, td) {
191 STAILQ_REMOVE(&s->s_data, d, _Elf_Data, d_next);
192 d = _libelf_release_data(d);
195 STAILQ_FOREACH_SAFE(d, &s->s_rawdata, d_next, td) {
196 assert((d->d_flags & LIBELF_F_MALLOCED) == 0);
197 STAILQ_REMOVE(&s->s_rawdata, d, _Elf_Data, d_next);
198 d = _libelf_release_data(d);
201 e = s->s_elf;
203 assert(e != NULL);
205 STAILQ_REMOVE(&e->e_u.e_elf.e_scn, s, _Elf_Scn, s_next);
207 free(s);
209 return (NULL);