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