2 * Copyright (c) 2006,2008,2010 Joseph Koshy
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
31 #include <sys/cdefs.h>
33 #include <sys/errno.h>
46 _libelf_allocate_elf(void)
50 if ((e
= malloc(sizeof(*e
))) == NULL
) {
51 LIBELF_SET_ERROR(RESOURCE
, errno
);
56 e
->e_hdr
.e_rawhdr
= NULL
;
57 e
->e_byteorder
= ELFDATANONE
;
58 e
->e_class
= ELFCLASSNONE
;
59 e
->e_cmd
= ELF_C_NULL
;
62 e
->e_kind
= ELF_K_NONE
;
66 e
->e_version
= LIBELF_PRIVATE(version
);
68 (void) memset(&e
->e_u
, 0, sizeof(e
->e_u
));
74 _libelf_init_elf(Elf
*e
, Elf_Kind kind
)
77 assert(e
->e_kind
== ELF_K_NONE
);
83 STAILQ_INIT(&e
->e_u
.e_elf
.e_scn
);
90 #define FREE(P) do { \
97 _libelf_release_elf(Elf
*e
)
103 FREE(e
->e_u
.e_ar
.e_symtab
);
107 switch (e
->e_class
) {
109 FREE(e
->e_u
.e_elf
.e_ehdr
.e_ehdr32
);
110 FREE(e
->e_u
.e_elf
.e_phdr
.e_phdr32
);
113 FREE(e
->e_u
.e_elf
.e_ehdr
.e_ehdr64
);
114 FREE(e
->e_u
.e_elf
.e_phdr
.e_phdr64
);
118 assert(STAILQ_EMPTY(&e
->e_u
.e_elf
.e_scn
));
120 if (e
->e_flags
& LIBELF_F_AR_HEADER
) {
121 arh
= e
->e_hdr
.e_arhdr
;
123 FREE(arh
->ar_rawname
);
139 _libelf_allocate_data(Elf_Scn
*s
)
143 if ((d
= calloc((size_t) 1, sizeof(Elf_Data
))) == NULL
) {
144 LIBELF_SET_ERROR(RESOURCE
, 0);
154 _libelf_release_data(Elf_Data
*d
)
157 if (d
->d_flags
& LIBELF_F_DATA_MALLOCED
)
166 _libelf_allocate_scn(Elf
*e
, size_t ndx
)
170 if ((s
= calloc((size_t) 1, sizeof(Elf_Scn
))) == NULL
) {
171 LIBELF_SET_ERROR(RESOURCE
, errno
);
178 STAILQ_INIT(&s
->s_data
);
179 STAILQ_INIT(&s
->s_rawdata
);
181 STAILQ_INSERT_TAIL(&e
->e_u
.e_elf
.e_scn
, s
, s_next
);
187 _libelf_release_scn(Elf_Scn
*s
)
194 STAILQ_FOREACH_SAFE(d
, &s
->s_data
, d_next
, td
) {
195 STAILQ_REMOVE(&s
->s_data
, d
, _Elf_Data
, d_next
);
196 d
= _libelf_release_data(d
);
199 STAILQ_FOREACH_SAFE(d
, &s
->s_rawdata
, d_next
, td
) {
200 assert((d
->d_flags
& LIBELF_F_DATA_MALLOCED
) == 0);
201 STAILQ_REMOVE(&s
->s_rawdata
, d
, _Elf_Data
, d_next
);
202 d
= _libelf_release_data(d
);
209 STAILQ_REMOVE(&e
->e_u
.e_elf
.e_scn
, s
, _Elf_Scn
, s_next
);