1164190Sjkoshy/*-
2164190Sjkoshy * Copyright (c) 2006 Joseph Koshy
3164190Sjkoshy * All rights reserved.
4164190Sjkoshy *
5164190Sjkoshy * Redistribution and use in source and binary forms, with or without
6164190Sjkoshy * modification, are permitted provided that the following conditions
7164190Sjkoshy * are met:
8164190Sjkoshy * 1. Redistributions of source code must retain the above copyright
9164190Sjkoshy *    notice, this list of conditions and the following disclaimer.
10164190Sjkoshy * 2. Redistributions in binary form must reproduce the above copyright
11164190Sjkoshy *    notice, this list of conditions and the following disclaimer in the
12164190Sjkoshy *    documentation and/or other materials provided with the distribution.
13164190Sjkoshy *
14164190Sjkoshy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15164190Sjkoshy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16164190Sjkoshy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17164190Sjkoshy * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18164190Sjkoshy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19164190Sjkoshy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20164190Sjkoshy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21164190Sjkoshy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22164190Sjkoshy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23164190Sjkoshy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24164190Sjkoshy * SUCH DAMAGE.
25164190Sjkoshy */
26164190Sjkoshy
27164190Sjkoshy#include <sys/cdefs.h>
28164190Sjkoshy__FBSDID("$FreeBSD$");
29164190Sjkoshy
30164190Sjkoshy#include <assert.h>
31164190Sjkoshy#include <errno.h>
32164190Sjkoshy#include <gelf.h>
33164190Sjkoshy#include <libelf.h>
34277558Semaste#include <stdint.h>
35164190Sjkoshy#include <stdlib.h>
36164190Sjkoshy
37164190Sjkoshy#include "_libelf.h"
38164190Sjkoshy
39164190Sjkoshy/*
40164190Sjkoshy * Load an ELF section table and create a list of Elf_Scn structures.
41164190Sjkoshy */
42210330Skaiwint
43164190Sjkoshy_libelf_load_scn(Elf *e, void *ehdr)
44164190Sjkoshy{
45164190Sjkoshy	int ec, swapbytes;
46164190Sjkoshy	size_t fsz, i, shnum;
47164190Sjkoshy	uint64_t shoff;
48164190Sjkoshy	char *src;
49164190Sjkoshy	Elf32_Ehdr *eh32;
50164190Sjkoshy	Elf64_Ehdr *eh64;
51164190Sjkoshy	Elf_Scn *scn;
52210338Skaiw	int (*xlator)(char *_d, size_t _dsz, char *_s, size_t _c, int _swap);
53164190Sjkoshy
54164190Sjkoshy	assert(e != NULL);
55164190Sjkoshy	assert(ehdr != NULL);
56165535Sjkoshy	assert((e->e_flags & LIBELF_F_SHDRS_LOADED) == 0);
57164190Sjkoshy
58164190Sjkoshy#define	CHECK_EHDR(E,EH)	do {				\
59277558Semaste		if (shoff > e->e_rawsize ||			\
60277558Semaste		    fsz != (EH)->e_shentsize ||			\
61277558Semaste		    shnum > SIZE_MAX / fsz ||			\
62277558Semaste		    fsz * shnum > e->e_rawsize - shoff) {	\
63164190Sjkoshy			LIBELF_SET_ERROR(HEADER, 0);		\
64164190Sjkoshy			return (0);				\
65164190Sjkoshy		}						\
66164190Sjkoshy	} while (0)
67164190Sjkoshy
68165535Sjkoshy	ec = e->e_class;
69165535Sjkoshy	fsz = _libelf_fsize(ELF_T_SHDR, ec, e->e_version, (size_t) 1);
70164190Sjkoshy	assert(fsz > 0);
71164190Sjkoshy
72165535Sjkoshy	shnum = e->e_u.e_elf.e_nscn;
73165535Sjkoshy
74164190Sjkoshy	if (ec == ELFCLASS32) {
75164190Sjkoshy		eh32 = (Elf32_Ehdr *) ehdr;
76164190Sjkoshy		shoff = (uint64_t) eh32->e_shoff;
77164190Sjkoshy		CHECK_EHDR(e, eh32);
78164190Sjkoshy	} else {
79164190Sjkoshy		eh64 = (Elf64_Ehdr *) ehdr;
80164190Sjkoshy		shoff = eh64->e_shoff;
81164190Sjkoshy		CHECK_EHDR(e, eh64);
82164190Sjkoshy	}
83164190Sjkoshy
84164190Sjkoshy	xlator = _libelf_get_translator(ELF_T_SHDR, ELF_TOMEMORY, ec);
85164190Sjkoshy
86164190Sjkoshy	swapbytes = e->e_byteorder != LIBELF_PRIVATE(byteorder);
87164190Sjkoshy	src = e->e_rawfile + shoff;
88164190Sjkoshy
89165535Sjkoshy	/*
90165535Sjkoshy	 * If the file is using extended numbering then section #0
91165535Sjkoshy	 * would have already been read in.
92165535Sjkoshy	 */
93164190Sjkoshy
94165535Sjkoshy	i = 0;
95165535Sjkoshy	if (!STAILQ_EMPTY(&e->e_u.e_elf.e_scn)) {
96165535Sjkoshy		assert(STAILQ_FIRST(&e->e_u.e_elf.e_scn) ==
97165535Sjkoshy		    STAILQ_LAST(&e->e_u.e_elf.e_scn, _Elf_Scn, s_next));
98164190Sjkoshy
99165535Sjkoshy		i = 1;
100164190Sjkoshy		src += fsz;
101164190Sjkoshy	}
102164190Sjkoshy
103164190Sjkoshy	for (; i < shnum; i++, src += fsz) {
104164190Sjkoshy		if ((scn = _libelf_allocate_scn(e, i)) == NULL)
105164190Sjkoshy			return (0);
106164190Sjkoshy
107210338Skaiw		(*xlator)((char *) &scn->s_shdr, sizeof(scn->s_shdr), src,
108210338Skaiw		    (size_t) 1, swapbytes);
109164190Sjkoshy
110164190Sjkoshy		if (ec == ELFCLASS32) {
111164190Sjkoshy			scn->s_offset = scn->s_rawoff =
112164190Sjkoshy			    scn->s_shdr.s_shdr32.sh_offset;
113164190Sjkoshy			scn->s_size = scn->s_shdr.s_shdr32.sh_size;
114164190Sjkoshy		} else {
115164190Sjkoshy			scn->s_offset = scn->s_rawoff =
116164190Sjkoshy			    scn->s_shdr.s_shdr64.sh_offset;
117164190Sjkoshy			scn->s_size = scn->s_shdr.s_shdr64.sh_size;
118164190Sjkoshy		}
119164190Sjkoshy	}
120165535Sjkoshy
121165535Sjkoshy	e->e_flags |= LIBELF_F_SHDRS_LOADED;
122165535Sjkoshy
123164190Sjkoshy	return (1);
124164190Sjkoshy}
125164190Sjkoshy
126164190Sjkoshy
127164190SjkoshyElf_Scn *
128164190Sjkoshyelf_getscn(Elf *e, size_t index)
129164190Sjkoshy{
130164190Sjkoshy	int ec;
131164190Sjkoshy	void *ehdr;
132164190Sjkoshy	Elf_Scn *s;
133164190Sjkoshy
134164190Sjkoshy	if (e == NULL || e->e_kind != ELF_K_ELF ||
135164190Sjkoshy	    ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
136164190Sjkoshy		LIBELF_SET_ERROR(ARGUMENT, 0);
137164190Sjkoshy		return (NULL);
138164190Sjkoshy	}
139164190Sjkoshy
140164190Sjkoshy	if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL)
141164190Sjkoshy		return (NULL);
142164190Sjkoshy
143165535Sjkoshy	if (e->e_cmd != ELF_C_WRITE &&
144165535Sjkoshy	    (e->e_flags & LIBELF_F_SHDRS_LOADED) == 0 &&
145164190Sjkoshy	    _libelf_load_scn(e, ehdr) == 0)
146164190Sjkoshy		return (NULL);
147164190Sjkoshy
148164190Sjkoshy	STAILQ_FOREACH(s, &e->e_u.e_elf.e_scn, s_next)
149164190Sjkoshy		if (s->s_ndx == index)
150164190Sjkoshy			return (s);
151164190Sjkoshy
152164190Sjkoshy	LIBELF_SET_ERROR(ARGUMENT, 0);
153164190Sjkoshy	return (NULL);
154164190Sjkoshy}
155164190Sjkoshy
156164190Sjkoshysize_t
157164190Sjkoshyelf_ndxscn(Elf_Scn *s)
158164190Sjkoshy{
159164190Sjkoshy	if (s == NULL) {
160164190Sjkoshy		LIBELF_SET_ERROR(ARGUMENT, 0);
161164190Sjkoshy		return (SHN_UNDEF);
162164190Sjkoshy	}
163164190Sjkoshy	return (s->s_ndx);
164164190Sjkoshy}
165164190Sjkoshy
166164190SjkoshyElf_Scn *
167164190Sjkoshyelf_newscn(Elf *e)
168164190Sjkoshy{
169164190Sjkoshy	int ec;
170164190Sjkoshy	void *ehdr;
171164190Sjkoshy	Elf_Scn *scn;
172164190Sjkoshy
173164190Sjkoshy	if (e == NULL || e->e_kind != ELF_K_ELF) {
174164190Sjkoshy		LIBELF_SET_ERROR(ARGUMENT, 0);
175164190Sjkoshy		return (NULL);
176164190Sjkoshy	}
177164190Sjkoshy
178164190Sjkoshy	if ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64) {
179164190Sjkoshy		LIBELF_SET_ERROR(CLASS, 0);
180164190Sjkoshy		return (NULL);
181164190Sjkoshy	}
182164190Sjkoshy
183164190Sjkoshy	if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL)
184164190Sjkoshy		return (NULL);
185164190Sjkoshy
186164190Sjkoshy	/*
187164190Sjkoshy	 * The application may be asking for a new section descriptor
188164190Sjkoshy	 * on an ELF object opened with ELF_C_RDWR or ELF_C_READ.  We
189164190Sjkoshy	 * need to bring in the existing section information before
190164190Sjkoshy	 * appending a new one to the list.
191164190Sjkoshy	 *
192164190Sjkoshy	 * Per the ELF(3) API, an application is allowed to open a
193164190Sjkoshy	 * file using ELF_C_READ, mess with its internal structure and
194164190Sjkoshy	 * use elf_update(...,ELF_C_NULL) to compute its new layout.
195164190Sjkoshy	 */
196165535Sjkoshy	if (e->e_cmd != ELF_C_WRITE &&
197165535Sjkoshy	    (e->e_flags & LIBELF_F_SHDRS_LOADED) == 0 &&
198164190Sjkoshy	    _libelf_load_scn(e, ehdr) == 0)
199164190Sjkoshy		return (NULL);
200164190Sjkoshy
201164190Sjkoshy	if (STAILQ_EMPTY(&e->e_u.e_elf.e_scn)) {
202165535Sjkoshy		assert(e->e_u.e_elf.e_nscn == 0);
203164190Sjkoshy		if ((scn = _libelf_allocate_scn(e, (size_t) SHN_UNDEF)) ==
204164190Sjkoshy		    NULL)
205164190Sjkoshy			return (NULL);
206165535Sjkoshy		e->e_u.e_elf.e_nscn++;
207164190Sjkoshy	}
208164190Sjkoshy
209165535Sjkoshy	assert(e->e_u.e_elf.e_nscn > 0);
210164190Sjkoshy
211165535Sjkoshy	if ((scn = _libelf_allocate_scn(e, e->e_u.e_elf.e_nscn)) == NULL)
212164190Sjkoshy		return (NULL);
213164190Sjkoshy
214165535Sjkoshy	e->e_u.e_elf.e_nscn++;
215164190Sjkoshy
216164190Sjkoshy	(void) elf_flagscn(scn, ELF_C_SET, ELF_F_DIRTY);
217164190Sjkoshy
218164190Sjkoshy	return (scn);
219164190Sjkoshy}
220164190Sjkoshy
221164190SjkoshyElf_Scn *
222164190Sjkoshyelf_nextscn(Elf *e, Elf_Scn *s)
223164190Sjkoshy{
224164190Sjkoshy	if (e == NULL || (e->e_kind != ELF_K_ELF) ||
225164190Sjkoshy	    (s && s->s_elf != e)) {
226164190Sjkoshy		LIBELF_SET_ERROR(ARGUMENT, 0);
227164190Sjkoshy		return (NULL);
228164190Sjkoshy	}
229164190Sjkoshy
230164190Sjkoshy	return (s == NULL ? elf_getscn(e, (size_t) 1) :
231164190Sjkoshy	    STAILQ_NEXT(s, s_next));
232164190Sjkoshy}
233