1 /*-
2 * Copyright (c) 2006,2008,2010 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 /*
28 * Internal APIs
29 */
30
31 #include <sys/cdefs.h>
32
33 #include <assert.h>
34 #include <errno.h>
35 #include <libelf.h>
36 #include <stdlib.h>
37 #include <string.h>
38
39 #include "_libelf.h"
40
41 ELFTC_VCSID("$Id$");
42
43 Elf *
_libelf_allocate_elf(void)44 _libelf_allocate_elf(void)
45 {
46 Elf *e;
47
48 if ((e = malloc(sizeof(*e))) == NULL) {
49 LIBELF_SET_ERROR(RESOURCE, errno);
50 return NULL;
51 }
52
53 e->e_activations = 1;
54 e->e_hdr.e_rawhdr = NULL;
55 e->e_byteorder = ELFDATANONE;
56 e->e_class = ELFCLASSNONE;
57 e->e_cmd = ELF_C_NULL;
58 e->e_fd = -1;
59 e->e_flags = 0;
60 e->e_kind = ELF_K_NONE;
61 e->e_parent = NULL;
62 e->e_rawfile = NULL;
63 e->e_rawsize = 0;
64 e->e_version = LIBELF_PRIVATE(version);
65
66 (void) memset(&e->e_u, 0, sizeof(e->e_u));
67
68 return (e);
69 }
70
71 void
_libelf_init_elf(Elf * e,Elf_Kind kind)72 _libelf_init_elf(Elf *e, Elf_Kind kind)
73 {
74 assert(e != NULL);
75 assert(e->e_kind == ELF_K_NONE);
76
77 e->e_kind = kind;
78
79 switch (kind) {
80 case ELF_K_ELF:
81 STAILQ_INIT(&e->e_u.e_elf.e_scn);
82 break;
83 default:
84 break;
85 }
86 }
87
88 #define FREE(P) do { \
89 if (P) \
90 free(P); \
91 } while (0)
92
93
94 Elf *
_libelf_release_elf(Elf * e)95 _libelf_release_elf(Elf *e)
96 {
97 Elf_Arhdr *arh;
98
99 switch (e->e_kind) {
100 case ELF_K_AR:
101 FREE(e->e_u.e_ar.e_symtab);
102 break;
103
104 case ELF_K_ELF:
105 switch (e->e_class) {
106 case ELFCLASS32:
107 FREE(e->e_u.e_elf.e_ehdr.e_ehdr32);
108 FREE(e->e_u.e_elf.e_phdr.e_phdr32);
109 break;
110 case ELFCLASS64:
111 FREE(e->e_u.e_elf.e_ehdr.e_ehdr64);
112 FREE(e->e_u.e_elf.e_phdr.e_phdr64);
113 break;
114 }
115
116 assert(STAILQ_EMPTY(&e->e_u.e_elf.e_scn));
117
118 if (e->e_flags & LIBELF_F_AR_HEADER) {
119 arh = e->e_hdr.e_arhdr;
120 FREE(arh->ar_name);
121 FREE(arh->ar_rawname);
122 free(arh);
123 }
124
125 break;
126
127 default:
128 break;
129 }
130
131 free(e);
132
133 return (NULL);
134 }
135
136 struct _Libelf_Data *
_libelf_allocate_data(Elf_Scn * s)137 _libelf_allocate_data(Elf_Scn *s)
138 {
139 struct _Libelf_Data *d;
140
141 if ((d = calloc((size_t) 1, sizeof(*d))) == NULL) {
142 LIBELF_SET_ERROR(RESOURCE, 0);
143 return (NULL);
144 }
145
146 d->d_scn = s;
147
148 return (d);
149 }
150
151 struct _Libelf_Data *
_libelf_release_data(struct _Libelf_Data * d)152 _libelf_release_data(struct _Libelf_Data *d)
153 {
154
155 if (d->d_flags & LIBELF_F_DATA_MALLOCED)
156 free(d->d_data.d_buf);
157
158 free(d);
159
160 return (NULL);
161 }
162
163 Elf_Scn *
_libelf_allocate_scn(Elf * e,size_t ndx)164 _libelf_allocate_scn(Elf *e, size_t ndx)
165 {
166 Elf_Scn *s;
167
168 if ((s = calloc((size_t) 1, sizeof(Elf_Scn))) == NULL) {
169 LIBELF_SET_ERROR(RESOURCE, errno);
170 return (NULL);
171 }
172
173 s->s_elf = e;
174 s->s_ndx = ndx;
175
176 STAILQ_INIT(&s->s_data);
177 STAILQ_INIT(&s->s_rawdata);
178
179 STAILQ_INSERT_TAIL(&e->e_u.e_elf.e_scn, s, s_next);
180
181 return (s);
182 }
183
184 Elf_Scn *
_libelf_release_scn(Elf_Scn * s)185 _libelf_release_scn(Elf_Scn *s)
186 {
187 Elf *e;
188 struct _Libelf_Data *d, *td;
189
190 assert(s != NULL);
191
192 STAILQ_FOREACH_SAFE(d, &s->s_data, d_next, td) {
193 STAILQ_REMOVE(&s->s_data, d, _Libelf_Data, d_next);
194 d = _libelf_release_data(d);
195 }
196
197 STAILQ_FOREACH_SAFE(d, &s->s_rawdata, d_next, td) {
198 assert((d->d_flags & LIBELF_F_DATA_MALLOCED) == 0);
199 STAILQ_REMOVE(&s->s_rawdata, d, _Libelf_Data, d_next);
200 d = _libelf_release_data(d);
201 }
202
203 e = s->s_elf;
204
205 assert(e != NULL);
206
207 STAILQ_REMOVE(&e->e_u.e_elf.e_scn, s, _Elf_Scn, s_next);
208
209 free(s);
210
211 return (NULL);
212 }
213