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 #include <sys/queue.h>
28
29 #include <assert.h>
30 #include <errno.h>
31 #include <gelf.h>
32 #include <libelf.h>
33 #include <stddef.h>
34 #include <stdint.h>
35 #include <stdlib.h>
36
37 #include "_libelf.h"
38
39 ELFTC_VCSID("$Id: elf_scn.c 3712 2019-03-16 22:23:34Z jkoshy $");
40
41 static int
elfscn_cmp(struct _Elf_Scn * s1,struct _Elf_Scn * s2)42 elfscn_cmp(struct _Elf_Scn *s1, struct _Elf_Scn *s2)
43 {
44
45 if (s1->s_ndx < s2->s_ndx)
46 return (-1);
47 if (s1->s_ndx > s2->s_ndx)
48 return (1);
49 return (0);
50 }
51
52 RB_GENERATE(scntree, _Elf_Scn, s_tree, elfscn_cmp);
53
54 /*
55 * Load an ELF section table and create a list of Elf_Scn structures.
56 */
57 int
_libelf_load_section_headers(Elf * e,void * ehdr)58 _libelf_load_section_headers(Elf *e, void *ehdr)
59 {
60 Elf_Scn *scn;
61 uint64_t shoff;
62 Elf32_Ehdr *eh32;
63 Elf64_Ehdr *eh64;
64 int ec, swapbytes;
65 unsigned char *src;
66 size_t fsz, i, shnum;
67 _libelf_translator_function *xlator;
68
69 assert(e != NULL);
70 assert(ehdr != NULL);
71 assert((e->e_flags & LIBELF_F_SHDRS_LOADED) == 0);
72
73 #define CHECK_EHDR(E,EH) do { \
74 uintmax_t rawsize = (uintmax_t) e->e_rawsize; \
75 if (shoff > (uintmax_t) e->e_rawsize || \
76 fsz != (EH)->e_shentsize || \
77 shnum > SIZE_MAX / fsz || \
78 fsz * shnum > rawsize - shoff) { \
79 LIBELF_SET_ERROR(HEADER, 0); \
80 return (0); \
81 } \
82 } while (0)
83
84 ec = e->e_class;
85 fsz = _libelf_fsize(ELF_T_SHDR, ec, e->e_version, (size_t) 1);
86 assert(fsz > 0);
87
88 shnum = e->e_u.e_elf.e_nscn;
89
90 if (ec == ELFCLASS32) {
91 eh32 = (Elf32_Ehdr *) ehdr;
92 shoff = (uint64_t) eh32->e_shoff;
93 CHECK_EHDR(e, eh32);
94 } else {
95 eh64 = (Elf64_Ehdr *) ehdr;
96 shoff = eh64->e_shoff;
97 CHECK_EHDR(e, eh64);
98 }
99
100 xlator = _libelf_get_translator(ELF_T_SHDR, ELF_TOMEMORY, ec,
101 _libelf_elfmachine(e));
102
103 swapbytes = e->e_byteorder != LIBELF_PRIVATE(byteorder);
104 src = e->e_rawfile + shoff;
105
106 /*
107 * If the file is using extended numbering then section #0
108 * would have already been read in.
109 */
110
111 i = 0;
112 if (!RB_EMPTY(&e->e_u.e_elf.e_scn)) {
113 assert(RB_MIN(scntree, &e->e_u.e_elf.e_scn) ==
114 RB_MAX(scntree, &e->e_u.e_elf.e_scn));
115
116 i = 1;
117 src += fsz;
118 }
119
120 for (; i < shnum; i++, src += fsz) {
121 if ((scn = _libelf_allocate_scn(e, i)) == NULL)
122 return (0);
123
124 (*xlator)((unsigned char *) &scn->s_shdr, sizeof(scn->s_shdr),
125 src, (size_t) 1, swapbytes);
126
127 if (ec == ELFCLASS32) {
128 scn->s_offset = scn->s_rawoff =
129 scn->s_shdr.s_shdr32.sh_offset;
130 scn->s_size = scn->s_shdr.s_shdr32.sh_size;
131 } else {
132 scn->s_offset = scn->s_rawoff =
133 scn->s_shdr.s_shdr64.sh_offset;
134 scn->s_size = scn->s_shdr.s_shdr64.sh_size;
135 }
136 }
137
138 e->e_flags |= LIBELF_F_SHDRS_LOADED;
139
140 return (1);
141 }
142
143
144 Elf_Scn *
elf_getscn(Elf * e,size_t index)145 elf_getscn(Elf *e, size_t index)
146 {
147 int ec;
148 void *ehdr;
149 Elf_Scn *s;
150
151 if (e == NULL || e->e_kind != ELF_K_ELF ||
152 ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
153 LIBELF_SET_ERROR(ARGUMENT, 0);
154 return (NULL);
155 }
156
157 if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL)
158 return (NULL);
159
160 if (e->e_cmd != ELF_C_WRITE &&
161 (e->e_flags & LIBELF_F_SHDRS_LOADED) == 0 &&
162 _libelf_load_section_headers(e, ehdr) == 0)
163 return (NULL);
164
165 for (s = RB_ROOT(&e->e_u.e_elf.e_scn); s != NULL;) {
166 if (s->s_ndx == index)
167 return (s);
168
169 if (s->s_ndx < index)
170 s = RB_RIGHT(s, s_tree);
171 else
172 s = RB_LEFT(s, s_tree);
173 }
174
175 LIBELF_SET_ERROR(ARGUMENT, 0);
176 return (NULL);
177 }
178
179 size_t
elf_ndxscn(Elf_Scn * s)180 elf_ndxscn(Elf_Scn *s)
181 {
182 if (s == NULL) {
183 LIBELF_SET_ERROR(ARGUMENT, 0);
184 return (SHN_UNDEF);
185 }
186 return (s->s_ndx);
187 }
188
189 Elf_Scn *
elf_newscn(Elf * e)190 elf_newscn(Elf *e)
191 {
192 int ec;
193 void *ehdr;
194 Elf_Scn *scn;
195
196 if (e == NULL || e->e_kind != ELF_K_ELF) {
197 LIBELF_SET_ERROR(ARGUMENT, 0);
198 return (NULL);
199 }
200
201 if ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64) {
202 LIBELF_SET_ERROR(CLASS, 0);
203 return (NULL);
204 }
205
206 if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL)
207 return (NULL);
208
209 /*
210 * The application may be asking for a new section descriptor
211 * on an ELF object opened with ELF_C_RDWR or ELF_C_READ. We
212 * need to bring in the existing section information before
213 * appending a new one to the list.
214 *
215 * Per the ELF(3) API, an application is allowed to open a
216 * file using ELF_C_READ, mess with its internal structure and
217 * use elf_update(...,ELF_C_NULL) to compute its new layout.
218 */
219 if (e->e_cmd != ELF_C_WRITE &&
220 (e->e_flags & LIBELF_F_SHDRS_LOADED) == 0 &&
221 _libelf_load_section_headers(e, ehdr) == 0)
222 return (NULL);
223
224 if (RB_EMPTY(&e->e_u.e_elf.e_scn)) {
225 assert(e->e_u.e_elf.e_nscn == 0);
226 if ((scn = _libelf_allocate_scn(e, (size_t) SHN_UNDEF)) ==
227 NULL)
228 return (NULL);
229 e->e_u.e_elf.e_nscn++;
230 }
231
232 assert(e->e_u.e_elf.e_nscn > 0);
233
234 if ((scn = _libelf_allocate_scn(e, e->e_u.e_elf.e_nscn)) == NULL)
235 return (NULL);
236
237 e->e_u.e_elf.e_nscn++;
238
239 (void) elf_flagscn(scn, ELF_C_SET, ELF_F_DIRTY);
240
241 return (scn);
242 }
243
244 Elf_Scn *
elf_nextscn(Elf * e,Elf_Scn * s)245 elf_nextscn(Elf *e, Elf_Scn *s)
246 {
247 if (e == NULL || (e->e_kind != ELF_K_ELF) ||
248 (s && s->s_elf != e)) {
249 LIBELF_SET_ERROR(ARGUMENT, 0);
250 return (NULL);
251 }
252
253 return (s == NULL ? elf_getscn(e, (size_t) 1) :
254 RB_NEXT(scntree, &e->e_u.e_elf.e_scn, s));
255 }
256