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