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