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