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