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