2 * Copyright (c) 2006,2008-2010 Joseph Koshy
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/queue.h>
39 ELFTC_VCSID("$Id: elf_scn.c 3632 2018-10-10 21:12:43Z jkoshy $");
42 elfscn_cmp(struct _Elf_Scn *s1, struct _Elf_Scn *s2)
45 if (s1->s_ndx < s2->s_ndx)
47 if (s1->s_ndx > s2->s_ndx)
52 RB_GENERATE(scntree, _Elf_Scn, s_tree, elfscn_cmp);
55 * Load an ELF section table and create a list of Elf_Scn structures.
58 _libelf_load_section_headers(Elf *e, void *ehdr)
67 _libelf_translator_function *xlator;
71 assert((e->e_flags & LIBELF_F_SHDRS_LOADED) == 0);
73 #define CHECK_EHDR(E,EH) do { \
74 if (shoff > e->e_rawsize || \
75 fsz != (EH)->e_shentsize || \
76 shnum > SIZE_MAX / fsz || \
77 fsz * shnum > e->e_rawsize - shoff) { \
78 LIBELF_SET_ERROR(HEADER, 0); \
84 fsz = _libelf_fsize(ELF_T_SHDR, ec, e->e_version, (size_t) 1);
87 shnum = e->e_u.e_elf.e_nscn;
89 if (ec == ELFCLASS32) {
90 eh32 = (Elf32_Ehdr *) ehdr;
91 shoff = (uint64_t) eh32->e_shoff;
94 eh64 = (Elf64_Ehdr *) ehdr;
95 shoff = eh64->e_shoff;
99 xlator = _libelf_get_translator(ELF_T_SHDR, ELF_TOMEMORY, ec,
100 _libelf_elfmachine(e));
102 swapbytes = e->e_byteorder != LIBELF_PRIVATE(byteorder);
103 src = e->e_rawfile + shoff;
106 * If the file is using extended numbering then section #0
107 * would have already been read in.
111 if (!RB_EMPTY(&e->e_u.e_elf.e_scn)) {
112 assert(RB_MIN(scntree, &e->e_u.e_elf.e_scn) ==
113 RB_MAX(scntree, &e->e_u.e_elf.e_scn));
119 for (; i < shnum; i++, src += fsz) {
120 if ((scn = _libelf_allocate_scn(e, i)) == NULL)
123 (*xlator)((unsigned char *) &scn->s_shdr, sizeof(scn->s_shdr),
124 src, (size_t) 1, swapbytes);
126 if (ec == ELFCLASS32) {
127 scn->s_offset = scn->s_rawoff =
128 scn->s_shdr.s_shdr32.sh_offset;
129 scn->s_size = scn->s_shdr.s_shdr32.sh_size;
131 scn->s_offset = scn->s_rawoff =
132 scn->s_shdr.s_shdr64.sh_offset;
133 scn->s_size = scn->s_shdr.s_shdr64.sh_size;
137 e->e_flags |= LIBELF_F_SHDRS_LOADED;
144 elf_getscn(Elf *e, size_t index)
150 if (e == NULL || e->e_kind != ELF_K_ELF ||
151 ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
152 LIBELF_SET_ERROR(ARGUMENT, 0);
156 if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL)
159 if (e->e_cmd != ELF_C_WRITE &&
160 (e->e_flags & LIBELF_F_SHDRS_LOADED) == 0 &&
161 _libelf_load_section_headers(e, ehdr) == 0)
164 for (s = RB_ROOT(&e->e_u.e_elf.e_scn); s != NULL;) {
165 if (s->s_ndx == index)
168 if (s->s_ndx < index)
169 s = RB_RIGHT(s, s_tree);
171 s = RB_LEFT(s, s_tree);
174 LIBELF_SET_ERROR(ARGUMENT, 0);
179 elf_ndxscn(Elf_Scn *s)
182 LIBELF_SET_ERROR(ARGUMENT, 0);
195 if (e == NULL || e->e_kind != ELF_K_ELF) {
196 LIBELF_SET_ERROR(ARGUMENT, 0);
200 if ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64) {
201 LIBELF_SET_ERROR(CLASS, 0);
205 if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL)
209 * The application may be asking for a new section descriptor
210 * on an ELF object opened with ELF_C_RDWR or ELF_C_READ. We
211 * need to bring in the existing section information before
212 * appending a new one to the list.
214 * Per the ELF(3) API, an application is allowed to open a
215 * file using ELF_C_READ, mess with its internal structure and
216 * use elf_update(...,ELF_C_NULL) to compute its new layout.
218 if (e->e_cmd != ELF_C_WRITE &&
219 (e->e_flags & LIBELF_F_SHDRS_LOADED) == 0 &&
220 _libelf_load_section_headers(e, ehdr) == 0)
223 if (RB_EMPTY(&e->e_u.e_elf.e_scn)) {
224 assert(e->e_u.e_elf.e_nscn == 0);
225 if ((scn = _libelf_allocate_scn(e, (size_t) SHN_UNDEF)) ==
228 e->e_u.e_elf.e_nscn++;
231 assert(e->e_u.e_elf.e_nscn > 0);
233 if ((scn = _libelf_allocate_scn(e, e->e_u.e_elf.e_nscn)) == NULL)
236 e->e_u.e_elf.e_nscn++;
238 (void) elf_flagscn(scn, ELF_C_SET, ELF_F_DIRTY);
244 elf_nextscn(Elf *e, Elf_Scn *s)
246 if (e == NULL || (e->e_kind != ELF_K_ELF) ||
247 (s && s->s_elf != e)) {
248 LIBELF_SET_ERROR(ARGUMENT, 0);
252 return (s == NULL ? elf_getscn(e, (size_t) 1) :
253 RB_NEXT(scntree, &e->e_u.e_elf.e_scn, s));