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>
34164190Sjkoshy#include <stdlib.h>
35164190Sjkoshy
36164190Sjkoshy#include "_libelf.h"
37164190Sjkoshy
38164190Sjkoshy/*
39164190Sjkoshy * Load an ELF section table and create a list of Elf_Scn structures.
40164190Sjkoshy */
41210330Skaiwint
42164190Sjkoshy_libelf_load_scn(Elf *e, void *ehdr)
43164190Sjkoshy{
44164190Sjkoshy	int ec, swapbytes;
45164190Sjkoshy	size_t fsz, i, shnum;
46164190Sjkoshy	uint64_t shoff;
47164190Sjkoshy	char *src;
48164190Sjkoshy	Elf32_Ehdr *eh32;
49164190Sjkoshy	Elf64_Ehdr *eh64;
50164190Sjkoshy	Elf_Scn *scn;
51210338Skaiw	int (*xlator)(char *_d, size_t _dsz, char *_s, size_t _c, int _swap);
52164190Sjkoshy
53164190Sjkoshy	assert(e != NULL);
54164190Sjkoshy	assert(ehdr != NULL);
55165535Sjkoshy	assert((e->e_flags & LIBELF_F_SHDRS_LOADED) == 0);
56164190Sjkoshy
57164190Sjkoshy#define	CHECK_EHDR(E,EH)	do {				\
58164190Sjkoshy		if (fsz != (EH)->e_shentsize ||			\
59164190Sjkoshy		    shoff + fsz * shnum > e->e_rawsize) {	\
60164190Sjkoshy			LIBELF_SET_ERROR(HEADER, 0);		\
61164190Sjkoshy			return (0);				\
62164190Sjkoshy		}						\
63164190Sjkoshy	} while (0)
64164190Sjkoshy
65165535Sjkoshy	ec = e->e_class;
66165535Sjkoshy	fsz = _libelf_fsize(ELF_T_SHDR, ec, e->e_version, (size_t) 1);
67164190Sjkoshy	assert(fsz > 0);
68164190Sjkoshy
69165535Sjkoshy	shnum = e->e_u.e_elf.e_nscn;
70165535Sjkoshy
71164190Sjkoshy	if (ec == ELFCLASS32) {
72164190Sjkoshy		eh32 = (Elf32_Ehdr *) ehdr;
73164190Sjkoshy		shoff = (uint64_t) eh32->e_shoff;
74164190Sjkoshy		CHECK_EHDR(e, eh32);
75164190Sjkoshy	} else {
76164190Sjkoshy		eh64 = (Elf64_Ehdr *) ehdr;
77164190Sjkoshy		shoff = eh64->e_shoff;
78164190Sjkoshy		CHECK_EHDR(e, eh64);
79164190Sjkoshy	}
80164190Sjkoshy
81164190Sjkoshy	xlator = _libelf_get_translator(ELF_T_SHDR, ELF_TOMEMORY, ec);
82164190Sjkoshy
83164190Sjkoshy	swapbytes = e->e_byteorder != LIBELF_PRIVATE(byteorder);
84164190Sjkoshy	src = e->e_rawfile + shoff;
85164190Sjkoshy
86165535Sjkoshy	/*
87165535Sjkoshy	 * If the file is using extended numbering then section #0
88165535Sjkoshy	 * would have already been read in.
89165535Sjkoshy	 */
90164190Sjkoshy
91165535Sjkoshy	i = 0;
92165535Sjkoshy	if (!STAILQ_EMPTY(&e->e_u.e_elf.e_scn)) {
93165535Sjkoshy		assert(STAILQ_FIRST(&e->e_u.e_elf.e_scn) ==
94165535Sjkoshy		    STAILQ_LAST(&e->e_u.e_elf.e_scn, _Elf_Scn, s_next));
95164190Sjkoshy
96165535Sjkoshy		i = 1;
97164190Sjkoshy		src += fsz;
98164190Sjkoshy	}
99164190Sjkoshy
100164190Sjkoshy	for (; i < shnum; i++, src += fsz) {
101164190Sjkoshy		if ((scn = _libelf_allocate_scn(e, i)) == NULL)
102164190Sjkoshy			return (0);
103164190Sjkoshy
104210338Skaiw		(*xlator)((char *) &scn->s_shdr, sizeof(scn->s_shdr), src,
105210338Skaiw		    (size_t) 1, swapbytes);
106164190Sjkoshy
107164190Sjkoshy		if (ec == ELFCLASS32) {
108164190Sjkoshy			scn->s_offset = scn->s_rawoff =
109164190Sjkoshy			    scn->s_shdr.s_shdr32.sh_offset;
110164190Sjkoshy			scn->s_size = scn->s_shdr.s_shdr32.sh_size;
111164190Sjkoshy		} else {
112164190Sjkoshy			scn->s_offset = scn->s_rawoff =
113164190Sjkoshy			    scn->s_shdr.s_shdr64.sh_offset;
114164190Sjkoshy			scn->s_size = scn->s_shdr.s_shdr64.sh_size;
115164190Sjkoshy		}
116164190Sjkoshy	}
117165535Sjkoshy
118165535Sjkoshy	e->e_flags |= LIBELF_F_SHDRS_LOADED;
119165535Sjkoshy
120164190Sjkoshy	return (1);
121164190Sjkoshy}
122164190Sjkoshy
123164190Sjkoshy
124164190SjkoshyElf_Scn *
125164190Sjkoshyelf_getscn(Elf *e, size_t index)
126164190Sjkoshy{
127164190Sjkoshy	int ec;
128164190Sjkoshy	void *ehdr;
129164190Sjkoshy	Elf_Scn *s;
130164190Sjkoshy
131164190Sjkoshy	if (e == NULL || e->e_kind != ELF_K_ELF ||
132164190Sjkoshy	    ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
133164190Sjkoshy		LIBELF_SET_ERROR(ARGUMENT, 0);
134164190Sjkoshy		return (NULL);
135164190Sjkoshy	}
136164190Sjkoshy
137164190Sjkoshy	if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL)
138164190Sjkoshy		return (NULL);
139164190Sjkoshy
140165535Sjkoshy	if (e->e_cmd != ELF_C_WRITE &&
141165535Sjkoshy	    (e->e_flags & LIBELF_F_SHDRS_LOADED) == 0 &&
142164190Sjkoshy	    _libelf_load_scn(e, ehdr) == 0)
143164190Sjkoshy		return (NULL);
144164190Sjkoshy
145164190Sjkoshy	STAILQ_FOREACH(s, &e->e_u.e_elf.e_scn, s_next)
146164190Sjkoshy		if (s->s_ndx == index)
147164190Sjkoshy			return (s);
148164190Sjkoshy
149164190Sjkoshy	LIBELF_SET_ERROR(ARGUMENT, 0);
150164190Sjkoshy	return (NULL);
151164190Sjkoshy}
152164190Sjkoshy
153164190Sjkoshysize_t
154164190Sjkoshyelf_ndxscn(Elf_Scn *s)
155164190Sjkoshy{
156164190Sjkoshy	if (s == NULL) {
157164190Sjkoshy		LIBELF_SET_ERROR(ARGUMENT, 0);
158164190Sjkoshy		return (SHN_UNDEF);
159164190Sjkoshy	}
160164190Sjkoshy	return (s->s_ndx);
161164190Sjkoshy}
162164190Sjkoshy
163164190SjkoshyElf_Scn *
164164190Sjkoshyelf_newscn(Elf *e)
165164190Sjkoshy{
166164190Sjkoshy	int ec;
167164190Sjkoshy	void *ehdr;
168164190Sjkoshy	Elf_Scn *scn;
169164190Sjkoshy
170164190Sjkoshy	if (e == NULL || e->e_kind != ELF_K_ELF) {
171164190Sjkoshy		LIBELF_SET_ERROR(ARGUMENT, 0);
172164190Sjkoshy		return (NULL);
173164190Sjkoshy	}
174164190Sjkoshy
175164190Sjkoshy	if ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64) {
176164190Sjkoshy		LIBELF_SET_ERROR(CLASS, 0);
177164190Sjkoshy		return (NULL);
178164190Sjkoshy	}
179164190Sjkoshy
180164190Sjkoshy	if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL)
181164190Sjkoshy		return (NULL);
182164190Sjkoshy
183164190Sjkoshy	/*
184164190Sjkoshy	 * The application may be asking for a new section descriptor
185164190Sjkoshy	 * on an ELF object opened with ELF_C_RDWR or ELF_C_READ.  We
186164190Sjkoshy	 * need to bring in the existing section information before
187164190Sjkoshy	 * appending a new one to the list.
188164190Sjkoshy	 *
189164190Sjkoshy	 * Per the ELF(3) API, an application is allowed to open a
190164190Sjkoshy	 * file using ELF_C_READ, mess with its internal structure and
191164190Sjkoshy	 * use elf_update(...,ELF_C_NULL) to compute its new layout.
192164190Sjkoshy	 */
193165535Sjkoshy	if (e->e_cmd != ELF_C_WRITE &&
194165535Sjkoshy	    (e->e_flags & LIBELF_F_SHDRS_LOADED) == 0 &&
195164190Sjkoshy	    _libelf_load_scn(e, ehdr) == 0)
196164190Sjkoshy		return (NULL);
197164190Sjkoshy
198164190Sjkoshy	if (STAILQ_EMPTY(&e->e_u.e_elf.e_scn)) {
199165535Sjkoshy		assert(e->e_u.e_elf.e_nscn == 0);
200164190Sjkoshy		if ((scn = _libelf_allocate_scn(e, (size_t) SHN_UNDEF)) ==
201164190Sjkoshy		    NULL)
202164190Sjkoshy			return (NULL);
203165535Sjkoshy		e->e_u.e_elf.e_nscn++;
204164190Sjkoshy	}
205164190Sjkoshy
206165535Sjkoshy	assert(e->e_u.e_elf.e_nscn > 0);
207164190Sjkoshy
208165535Sjkoshy	if ((scn = _libelf_allocate_scn(e, e->e_u.e_elf.e_nscn)) == NULL)
209164190Sjkoshy		return (NULL);
210164190Sjkoshy
211165535Sjkoshy	e->e_u.e_elf.e_nscn++;
212164190Sjkoshy
213164190Sjkoshy	(void) elf_flagscn(scn, ELF_C_SET, ELF_F_DIRTY);
214164190Sjkoshy
215164190Sjkoshy	return (scn);
216164190Sjkoshy}
217164190Sjkoshy
218164190SjkoshyElf_Scn *
219164190Sjkoshyelf_nextscn(Elf *e, Elf_Scn *s)
220164190Sjkoshy{
221164190Sjkoshy	if (e == NULL || (e->e_kind != ELF_K_ELF) ||
222164190Sjkoshy	    (s && s->s_elf != e)) {
223164190Sjkoshy		LIBELF_SET_ERROR(ARGUMENT, 0);
224164190Sjkoshy		return (NULL);
225164190Sjkoshy	}
226164190Sjkoshy
227164190Sjkoshy	return (s == NULL ? elf_getscn(e, (size_t) 1) :
228164190Sjkoshy	    STAILQ_NEXT(s, s_next));
229164190Sjkoshy}
230