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