1*0a6a1f1dSLionel Sambuc /* $NetBSD: libelf_allocate.c,v 1.2 2014/03/09 16:58:04 christos Exp $ */
2*0a6a1f1dSLionel Sambuc
3*0a6a1f1dSLionel Sambuc /*-
4*0a6a1f1dSLionel Sambuc * Copyright (c) 2006,2008,2010 Joseph Koshy
5*0a6a1f1dSLionel Sambuc * All rights reserved.
6*0a6a1f1dSLionel Sambuc *
7*0a6a1f1dSLionel Sambuc * Redistribution and use in source and binary forms, with or without
8*0a6a1f1dSLionel Sambuc * modification, are permitted provided that the following conditions
9*0a6a1f1dSLionel Sambuc * are met:
10*0a6a1f1dSLionel Sambuc * 1. Redistributions of source code must retain the above copyright
11*0a6a1f1dSLionel Sambuc * notice, this list of conditions and the following disclaimer.
12*0a6a1f1dSLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
13*0a6a1f1dSLionel Sambuc * notice, this list of conditions and the following disclaimer in the
14*0a6a1f1dSLionel Sambuc * documentation and/or other materials provided with the distribution.
15*0a6a1f1dSLionel Sambuc *
16*0a6a1f1dSLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17*0a6a1f1dSLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*0a6a1f1dSLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*0a6a1f1dSLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20*0a6a1f1dSLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*0a6a1f1dSLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*0a6a1f1dSLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*0a6a1f1dSLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*0a6a1f1dSLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*0a6a1f1dSLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*0a6a1f1dSLionel Sambuc * SUCH DAMAGE.
27*0a6a1f1dSLionel Sambuc */
28*0a6a1f1dSLionel Sambuc
29*0a6a1f1dSLionel Sambuc #if HAVE_NBTOOL_CONFIG_H
30*0a6a1f1dSLionel Sambuc # include "nbtool_config.h"
31*0a6a1f1dSLionel Sambuc #endif
32*0a6a1f1dSLionel Sambuc
33*0a6a1f1dSLionel Sambuc /*
34*0a6a1f1dSLionel Sambuc * Internal APIs
35*0a6a1f1dSLionel Sambuc */
36*0a6a1f1dSLionel Sambuc
37*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
38*0a6a1f1dSLionel Sambuc
39*0a6a1f1dSLionel Sambuc #include <assert.h>
40*0a6a1f1dSLionel Sambuc #include <errno.h>
41*0a6a1f1dSLionel Sambuc #include <libelf.h>
42*0a6a1f1dSLionel Sambuc #include <stdlib.h>
43*0a6a1f1dSLionel Sambuc #include <string.h>
44*0a6a1f1dSLionel Sambuc
45*0a6a1f1dSLionel Sambuc #include "_libelf.h"
46*0a6a1f1dSLionel Sambuc
47*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: libelf_allocate.c,v 1.2 2014/03/09 16:58:04 christos Exp $");
48*0a6a1f1dSLionel Sambuc ELFTC_VCSID("Id: libelf_allocate.c 2272 2011-12-03 17:07:31Z jkoshy ");
49*0a6a1f1dSLionel Sambuc
50*0a6a1f1dSLionel Sambuc Elf *
_libelf_allocate_elf(void)51*0a6a1f1dSLionel Sambuc _libelf_allocate_elf(void)
52*0a6a1f1dSLionel Sambuc {
53*0a6a1f1dSLionel Sambuc Elf *e;
54*0a6a1f1dSLionel Sambuc
55*0a6a1f1dSLionel Sambuc if ((e = malloc(sizeof(*e))) == NULL) {
56*0a6a1f1dSLionel Sambuc LIBELF_SET_ERROR(RESOURCE, errno);
57*0a6a1f1dSLionel Sambuc return NULL;
58*0a6a1f1dSLionel Sambuc }
59*0a6a1f1dSLionel Sambuc
60*0a6a1f1dSLionel Sambuc e->e_activations = 1;
61*0a6a1f1dSLionel Sambuc e->e_hdr.e_rawhdr = NULL;
62*0a6a1f1dSLionel Sambuc e->e_byteorder = ELFDATANONE;
63*0a6a1f1dSLionel Sambuc e->e_class = ELFCLASSNONE;
64*0a6a1f1dSLionel Sambuc e->e_cmd = ELF_C_NULL;
65*0a6a1f1dSLionel Sambuc e->e_fd = -1;
66*0a6a1f1dSLionel Sambuc e->e_flags = 0;
67*0a6a1f1dSLionel Sambuc e->e_kind = ELF_K_NONE;
68*0a6a1f1dSLionel Sambuc e->e_parent = NULL;
69*0a6a1f1dSLionel Sambuc e->e_rawfile = NULL;
70*0a6a1f1dSLionel Sambuc e->e_rawsize = 0;
71*0a6a1f1dSLionel Sambuc e->e_version = LIBELF_PRIVATE(version);
72*0a6a1f1dSLionel Sambuc
73*0a6a1f1dSLionel Sambuc (void) memset(&e->e_u, 0, sizeof(e->e_u));
74*0a6a1f1dSLionel Sambuc
75*0a6a1f1dSLionel Sambuc return (e);
76*0a6a1f1dSLionel Sambuc }
77*0a6a1f1dSLionel Sambuc
78*0a6a1f1dSLionel Sambuc void
_libelf_init_elf(Elf * e,Elf_Kind kind)79*0a6a1f1dSLionel Sambuc _libelf_init_elf(Elf *e, Elf_Kind kind)
80*0a6a1f1dSLionel Sambuc {
81*0a6a1f1dSLionel Sambuc assert(e != NULL);
82*0a6a1f1dSLionel Sambuc assert(e->e_kind == ELF_K_NONE);
83*0a6a1f1dSLionel Sambuc
84*0a6a1f1dSLionel Sambuc e->e_kind = kind;
85*0a6a1f1dSLionel Sambuc
86*0a6a1f1dSLionel Sambuc switch (kind) {
87*0a6a1f1dSLionel Sambuc case ELF_K_ELF:
88*0a6a1f1dSLionel Sambuc STAILQ_INIT(&e->e_u.e_elf.e_scn);
89*0a6a1f1dSLionel Sambuc break;
90*0a6a1f1dSLionel Sambuc default:
91*0a6a1f1dSLionel Sambuc break;
92*0a6a1f1dSLionel Sambuc }
93*0a6a1f1dSLionel Sambuc }
94*0a6a1f1dSLionel Sambuc
95*0a6a1f1dSLionel Sambuc #define FREE(P) do { \
96*0a6a1f1dSLionel Sambuc if (P) \
97*0a6a1f1dSLionel Sambuc free(P); \
98*0a6a1f1dSLionel Sambuc } while (/*CONSTCOND*/0)
99*0a6a1f1dSLionel Sambuc
100*0a6a1f1dSLionel Sambuc
101*0a6a1f1dSLionel Sambuc Elf *
_libelf_release_elf(Elf * e)102*0a6a1f1dSLionel Sambuc _libelf_release_elf(Elf *e)
103*0a6a1f1dSLionel Sambuc {
104*0a6a1f1dSLionel Sambuc Elf_Arhdr *arh;
105*0a6a1f1dSLionel Sambuc
106*0a6a1f1dSLionel Sambuc switch (e->e_kind) {
107*0a6a1f1dSLionel Sambuc case ELF_K_AR:
108*0a6a1f1dSLionel Sambuc FREE(e->e_u.e_ar.e_symtab);
109*0a6a1f1dSLionel Sambuc break;
110*0a6a1f1dSLionel Sambuc
111*0a6a1f1dSLionel Sambuc case ELF_K_ELF:
112*0a6a1f1dSLionel Sambuc switch (e->e_class) {
113*0a6a1f1dSLionel Sambuc case ELFCLASS32:
114*0a6a1f1dSLionel Sambuc FREE(e->e_u.e_elf.e_ehdr.e_ehdr32);
115*0a6a1f1dSLionel Sambuc FREE(e->e_u.e_elf.e_phdr.e_phdr32);
116*0a6a1f1dSLionel Sambuc break;
117*0a6a1f1dSLionel Sambuc case ELFCLASS64:
118*0a6a1f1dSLionel Sambuc FREE(e->e_u.e_elf.e_ehdr.e_ehdr64);
119*0a6a1f1dSLionel Sambuc FREE(e->e_u.e_elf.e_phdr.e_phdr64);
120*0a6a1f1dSLionel Sambuc break;
121*0a6a1f1dSLionel Sambuc }
122*0a6a1f1dSLionel Sambuc
123*0a6a1f1dSLionel Sambuc assert(STAILQ_EMPTY(&e->e_u.e_elf.e_scn));
124*0a6a1f1dSLionel Sambuc
125*0a6a1f1dSLionel Sambuc if (e->e_flags & LIBELF_F_AR_HEADER) {
126*0a6a1f1dSLionel Sambuc arh = e->e_hdr.e_arhdr;
127*0a6a1f1dSLionel Sambuc FREE(arh->ar_name);
128*0a6a1f1dSLionel Sambuc FREE(arh->ar_rawname);
129*0a6a1f1dSLionel Sambuc free(arh);
130*0a6a1f1dSLionel Sambuc }
131*0a6a1f1dSLionel Sambuc
132*0a6a1f1dSLionel Sambuc break;
133*0a6a1f1dSLionel Sambuc
134*0a6a1f1dSLionel Sambuc default:
135*0a6a1f1dSLionel Sambuc break;
136*0a6a1f1dSLionel Sambuc }
137*0a6a1f1dSLionel Sambuc
138*0a6a1f1dSLionel Sambuc free(e);
139*0a6a1f1dSLionel Sambuc
140*0a6a1f1dSLionel Sambuc return (NULL);
141*0a6a1f1dSLionel Sambuc }
142*0a6a1f1dSLionel Sambuc
143*0a6a1f1dSLionel Sambuc struct _Libelf_Data *
_libelf_allocate_data(Elf_Scn * s)144*0a6a1f1dSLionel Sambuc _libelf_allocate_data(Elf_Scn *s)
145*0a6a1f1dSLionel Sambuc {
146*0a6a1f1dSLionel Sambuc struct _Libelf_Data *d;
147*0a6a1f1dSLionel Sambuc
148*0a6a1f1dSLionel Sambuc if ((d = calloc((size_t) 1, sizeof(*d))) == NULL) {
149*0a6a1f1dSLionel Sambuc LIBELF_SET_ERROR(RESOURCE, 0);
150*0a6a1f1dSLionel Sambuc return (NULL);
151*0a6a1f1dSLionel Sambuc }
152*0a6a1f1dSLionel Sambuc
153*0a6a1f1dSLionel Sambuc d->d_scn = s;
154*0a6a1f1dSLionel Sambuc
155*0a6a1f1dSLionel Sambuc return (d);
156*0a6a1f1dSLionel Sambuc }
157*0a6a1f1dSLionel Sambuc
158*0a6a1f1dSLionel Sambuc struct _Libelf_Data *
_libelf_release_data(struct _Libelf_Data * d)159*0a6a1f1dSLionel Sambuc _libelf_release_data(struct _Libelf_Data *d)
160*0a6a1f1dSLionel Sambuc {
161*0a6a1f1dSLionel Sambuc
162*0a6a1f1dSLionel Sambuc if (d->d_flags & LIBELF_F_DATA_MALLOCED)
163*0a6a1f1dSLionel Sambuc free(d->d_data.d_buf);
164*0a6a1f1dSLionel Sambuc
165*0a6a1f1dSLionel Sambuc free(d);
166*0a6a1f1dSLionel Sambuc
167*0a6a1f1dSLionel Sambuc return (NULL);
168*0a6a1f1dSLionel Sambuc }
169*0a6a1f1dSLionel Sambuc
170*0a6a1f1dSLionel Sambuc Elf_Scn *
_libelf_allocate_scn(Elf * e,size_t ndx)171*0a6a1f1dSLionel Sambuc _libelf_allocate_scn(Elf *e, size_t ndx)
172*0a6a1f1dSLionel Sambuc {
173*0a6a1f1dSLionel Sambuc Elf_Scn *s;
174*0a6a1f1dSLionel Sambuc
175*0a6a1f1dSLionel Sambuc if ((s = calloc((size_t) 1, sizeof(Elf_Scn))) == NULL) {
176*0a6a1f1dSLionel Sambuc LIBELF_SET_ERROR(RESOURCE, errno);
177*0a6a1f1dSLionel Sambuc return (NULL);
178*0a6a1f1dSLionel Sambuc }
179*0a6a1f1dSLionel Sambuc
180*0a6a1f1dSLionel Sambuc s->s_elf = e;
181*0a6a1f1dSLionel Sambuc s->s_ndx = ndx;
182*0a6a1f1dSLionel Sambuc
183*0a6a1f1dSLionel Sambuc STAILQ_INIT(&s->s_data);
184*0a6a1f1dSLionel Sambuc STAILQ_INIT(&s->s_rawdata);
185*0a6a1f1dSLionel Sambuc
186*0a6a1f1dSLionel Sambuc STAILQ_INSERT_TAIL(&e->e_u.e_elf.e_scn, s, s_next);
187*0a6a1f1dSLionel Sambuc
188*0a6a1f1dSLionel Sambuc return (s);
189*0a6a1f1dSLionel Sambuc }
190*0a6a1f1dSLionel Sambuc
191*0a6a1f1dSLionel Sambuc Elf_Scn *
_libelf_release_scn(Elf_Scn * s)192*0a6a1f1dSLionel Sambuc _libelf_release_scn(Elf_Scn *s)
193*0a6a1f1dSLionel Sambuc {
194*0a6a1f1dSLionel Sambuc Elf *e;
195*0a6a1f1dSLionel Sambuc struct _Libelf_Data *d, *td;
196*0a6a1f1dSLionel Sambuc
197*0a6a1f1dSLionel Sambuc assert(s != NULL);
198*0a6a1f1dSLionel Sambuc
199*0a6a1f1dSLionel Sambuc STAILQ_FOREACH_SAFE(d, &s->s_data, d_next, td) {
200*0a6a1f1dSLionel Sambuc STAILQ_REMOVE(&s->s_data, d, _Libelf_Data, d_next);
201*0a6a1f1dSLionel Sambuc d = _libelf_release_data(d);
202*0a6a1f1dSLionel Sambuc }
203*0a6a1f1dSLionel Sambuc
204*0a6a1f1dSLionel Sambuc STAILQ_FOREACH_SAFE(d, &s->s_rawdata, d_next, td) {
205*0a6a1f1dSLionel Sambuc assert((d->d_flags & LIBELF_F_DATA_MALLOCED) == 0);
206*0a6a1f1dSLionel Sambuc STAILQ_REMOVE(&s->s_rawdata, d, _Libelf_Data, d_next);
207*0a6a1f1dSLionel Sambuc d = _libelf_release_data(d);
208*0a6a1f1dSLionel Sambuc }
209*0a6a1f1dSLionel Sambuc
210*0a6a1f1dSLionel Sambuc e = s->s_elf;
211*0a6a1f1dSLionel Sambuc
212*0a6a1f1dSLionel Sambuc assert(e != NULL);
213*0a6a1f1dSLionel Sambuc
214*0a6a1f1dSLionel Sambuc STAILQ_REMOVE(&e->e_u.e_elf.e_scn, s, _Elf_Scn, s_next);
215*0a6a1f1dSLionel Sambuc
216*0a6a1f1dSLionel Sambuc free(s);
217*0a6a1f1dSLionel Sambuc
218*0a6a1f1dSLionel Sambuc return (NULL);
219*0a6a1f1dSLionel Sambuc }
220