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