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