1 /* $NetBSD: libelf_allocate.c,v 1.2 2014/03/09 16:58:04 christos Exp $ */
4 * Copyright (c) 2006,2008,2010 Joseph Koshy
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
29 #if HAVE_NBTOOL_CONFIG_H
30 # include "nbtool_config.h"
37 #include <sys/cdefs.h>
47 __RCSID("$NetBSD: libelf_allocate.c,v 1.2 2014/03/09 16:58:04 christos Exp $");
48 ELFTC_VCSID("Id: libelf_allocate.c 2272 2011-12-03 17:07:31Z jkoshy ");
51 _libelf_allocate_elf(void)
55 if ((e
= malloc(sizeof(*e
))) == NULL
) {
56 LIBELF_SET_ERROR(RESOURCE
, errno
);
61 e
->e_hdr
.e_rawhdr
= NULL
;
62 e
->e_byteorder
= ELFDATANONE
;
63 e
->e_class
= ELFCLASSNONE
;
64 e
->e_cmd
= ELF_C_NULL
;
67 e
->e_kind
= ELF_K_NONE
;
71 e
->e_version
= LIBELF_PRIVATE(version
);
73 (void) memset(&e
->e_u
, 0, sizeof(e
->e_u
));
79 _libelf_init_elf(Elf
*e
, Elf_Kind kind
)
82 assert(e
->e_kind
== ELF_K_NONE
);
88 STAILQ_INIT(&e
->e_u
.e_elf
.e_scn
);
95 #define FREE(P) do { \
98 } while (/*CONSTCOND*/0)
102 _libelf_release_elf(Elf
*e
)
108 FREE(e
->e_u
.e_ar
.e_symtab
);
112 switch (e
->e_class
) {
114 FREE(e
->e_u
.e_elf
.e_ehdr
.e_ehdr32
);
115 FREE(e
->e_u
.e_elf
.e_phdr
.e_phdr32
);
118 FREE(e
->e_u
.e_elf
.e_ehdr
.e_ehdr64
);
119 FREE(e
->e_u
.e_elf
.e_phdr
.e_phdr64
);
123 assert(STAILQ_EMPTY(&e
->e_u
.e_elf
.e_scn
));
125 if (e
->e_flags
& LIBELF_F_AR_HEADER
) {
126 arh
= e
->e_hdr
.e_arhdr
;
128 FREE(arh
->ar_rawname
);
143 struct _Libelf_Data
*
144 _libelf_allocate_data(Elf_Scn
*s
)
146 struct _Libelf_Data
*d
;
148 if ((d
= calloc((size_t) 1, sizeof(*d
))) == NULL
) {
149 LIBELF_SET_ERROR(RESOURCE
, 0);
158 struct _Libelf_Data
*
159 _libelf_release_data(struct _Libelf_Data
*d
)
162 if (d
->d_flags
& LIBELF_F_DATA_MALLOCED
)
163 free(d
->d_data
.d_buf
);
171 _libelf_allocate_scn(Elf
*e
, size_t ndx
)
175 if ((s
= calloc((size_t) 1, sizeof(Elf_Scn
))) == NULL
) {
176 LIBELF_SET_ERROR(RESOURCE
, errno
);
183 STAILQ_INIT(&s
->s_data
);
184 STAILQ_INIT(&s
->s_rawdata
);
186 STAILQ_INSERT_TAIL(&e
->e_u
.e_elf
.e_scn
, s
, s_next
);
192 _libelf_release_scn(Elf_Scn
*s
)
195 struct _Libelf_Data
*d
, *td
;
199 STAILQ_FOREACH_SAFE(d
, &s
->s_data
, d_next
, td
) {
200 STAILQ_REMOVE(&s
->s_data
, d
, _Libelf_Data
, d_next
);
201 d
= _libelf_release_data(d
);
204 STAILQ_FOREACH_SAFE(d
, &s
->s_rawdata
, d_next
, td
) {
205 assert((d
->d_flags
& LIBELF_F_DATA_MALLOCED
) == 0);
206 STAILQ_REMOVE(&s
->s_rawdata
, d
, _Libelf_Data
, d_next
);
207 d
= _libelf_release_data(d
);
214 STAILQ_REMOVE(&e
->e_u
.e_elf
.e_scn
, s
, _Elf_Scn
, s_next
);