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