1 /* $NetBSD: libelf_allocate.c,v 1.3 2009/12/19 06:22:25 thorpej Exp $ */
4 * Copyright (c) 2006 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
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 $"); */
45 _libelf_allocate_elf(void)
49 if ((e
= malloc(sizeof(*e
))) == NULL
) {
50 LIBELF_SET_ERROR(RESOURCE
, errno
);
56 e
->e_byteorder
= ELFDATANONE
;
57 e
->e_class
= ELFCLASSNONE
;
58 e
->e_cmd
= ELF_C_NULL
;
61 e
->e_kind
= ELF_K_NONE
;
65 e
->e_version
= LIBELF_PRIVATE(version
);
67 (void) memset(&e
->e_u
, 0, sizeof(e
->e_u
));
73 _libelf_init_elf(Elf
*e
, Elf_Kind kind
)
76 assert(e
->e_kind
== ELF_K_NONE
);
82 STAILQ_INIT(&e
->e_u
.e_elf
.e_scn
);
89 #define FREE(P) do { \
92 } while (/*CONSTCOND*/0)
96 _libelf_release_elf(Elf
*e
)
100 FREE(e
->e_u
.e_ar
.e_symtab
);
104 switch (e
->e_class
) {
106 FREE(e
->e_u
.e_elf
.e_ehdr
.e_ehdr32
);
107 FREE(e
->e_u
.e_elf
.e_phdr
.e_phdr32
);
110 FREE(e
->e_u
.e_elf
.e_ehdr
.e_ehdr64
);
111 FREE(e
->e_u
.e_elf
.e_phdr
.e_phdr64
);
115 assert(STAILQ_EMPTY(&e
->e_u
.e_elf
.e_scn
));
118 FREE(e
->e_arhdr
->ar_name
);
119 FREE(e
->e_arhdr
->ar_rawname
);
135 _libelf_allocate_data(Elf_Scn
*s
)
139 if ((d
= calloc((size_t) 1, sizeof(Elf_Data
))) == NULL
) {
140 LIBELF_SET_ERROR(RESOURCE
, 0);
150 _libelf_release_data(Elf_Data
*d
)
153 if (d
->d_flags
& LIBELF_F_MALLOCED
)
162 _libelf_allocate_scn(Elf
*e
, size_t ndx
)
166 if ((s
= calloc((size_t) 1, sizeof(Elf_Scn
))) == NULL
) {
167 LIBELF_SET_ERROR(RESOURCE
, errno
);
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
);
183 _libelf_release_scn(Elf_Scn
*s
)
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
);
205 STAILQ_REMOVE(&e
->e_u
.e_elf
.e_scn
, s
, _Elf_Scn
, s_next
);