xref: /netbsd-src/external/bsd/elftoolchain/dist/libelf/elf_scn.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*	$NetBSD: elf_scn.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 #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.3 2016/02/20 02:43:42 christos Exp $");
47 ELFTC_VCSID("Id: elf_scn.c 3177 2015-03-30 18:19:41Z emaste ");
48 
49 /*
50  * Load an ELF section table and create a list of Elf_Scn structures.
51  */
52 int
53 _libelf_load_section_headers(Elf *e, void *ehdr)
54 {
55 	Elf_Scn *scn;
56 	uint64_t shoff;
57 	Elf32_Ehdr *eh32;
58 	Elf64_Ehdr *eh64;
59 	int ec, swapbytes;
60 	unsigned char *src;
61 	size_t fsz, i, shnum;
62 	int (*xlator)(unsigned char *_d, size_t _dsz, unsigned char *_s,
63 	    size_t _c, int _swap);
64 
65 	assert(e != NULL);
66 	assert(ehdr != NULL);
67 	assert((e->e_flags & LIBELF_F_SHDRS_LOADED) == 0);
68 
69 #define	CHECK_EHDR(E,EH)	do {				\
70 		if (shoff > e->e_rawsize ||			\
71 		    fsz != (EH)->e_shentsize ||			\
72 		    shnum > SIZE_MAX / fsz ||			\
73 		    fsz * shnum > e->e_rawsize - shoff) {	\
74 			LIBELF_SET_ERROR(HEADER, 0);		\
75 			return (0);				\
76 		}						\
77 	} while (0)
78 
79 	ec = e->e_class;
80 	fsz = _libelf_fsize(ELF_T_SHDR, ec, e->e_version, (size_t) 1);
81 	assert(fsz > 0);
82 
83 	shnum = e->e_u.e_elf.e_nscn;
84 
85 	if (ec == ELFCLASS32) {
86 		eh32 = (Elf32_Ehdr *) ehdr;
87 		shoff = (uint64_t) eh32->e_shoff;
88 		CHECK_EHDR(e, eh32);
89 	} else {
90 		eh64 = (Elf64_Ehdr *) ehdr;
91 		shoff = eh64->e_shoff;
92 		CHECK_EHDR(e, eh64);
93 	}
94 
95 	xlator = _libelf_get_translator(ELF_T_SHDR, ELF_TOMEMORY, ec);
96 
97 	swapbytes = e->e_byteorder != _libelf_host_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 *
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
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 *
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 *
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