1 /* $NetBSD: elf_scn.c,v 1.4 2009/12/20 23:23:46 thorpej Exp $ */
4 * Copyright (c) 2006 Joseph Koshy
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 /* __FBSDID("$FreeBSD: src/lib/libelf/elf_scn.c,v 1.2.10.1.2.1 2009/10/25 01:10:29 kensmith Exp $"); */
43 * Load an ELF section table and create a list of Elf_Scn structures.
46 _libelf_load_scn(Elf
*e
, void *ehdr
)
55 void (*xlator
)(char *_d
, char *_s
, size_t _c
, int _swap
);
59 assert((e
->e_flags
& LIBELF_F_SHDRS_LOADED
) == 0);
61 #define CHECK_EHDR(E,EH) do { \
62 if (fsz != (EH)->e_shentsize || \
63 shoff + fsz * shnum > e->e_rawsize) { \
64 LIBELF_SET_ERROR(HEADER, 0); \
67 } while (/*CONSTCOND*/0)
70 fsz
= _libelf_fsize(ELF_T_SHDR
, ec
, e
->e_version
, (size_t) 1);
73 shnum
= e
->e_u
.e_elf
.e_nscn
;
75 if (ec
== ELFCLASS32
) {
76 eh32
= (Elf32_Ehdr
*) ehdr
;
77 shoff
= (uint64_t) eh32
->e_shoff
;
80 eh64
= (Elf64_Ehdr
*) ehdr
;
81 shoff
= eh64
->e_shoff
;
85 xlator
= _libelf_get_translator(ELF_T_SHDR
, ELF_TOMEMORY
, ec
);
87 swapbytes
= e
->e_byteorder
!= _libelf_host_byteorder();
88 if (shoff
> SSIZE_MAX
) {
89 LIBELF_SET_ERROR(HEADER
, 0);
92 src
= e
->e_rawfile
+ (ssize_t
)shoff
;
95 * If the file is using extended numbering then section #0
96 * would have already been read in.
100 if (!STAILQ_EMPTY(&e
->e_u
.e_elf
.e_scn
)) {
101 assert(STAILQ_FIRST(&e
->e_u
.e_elf
.e_scn
) ==
102 STAILQ_LAST(&e
->e_u
.e_elf
.e_scn
, _Elf_Scn
, s_next
));
108 for (; i
< shnum
; i
++, src
+= fsz
) {
109 if ((scn
= _libelf_allocate_scn(e
, i
)) == NULL
)
112 (*xlator
)((void *) &scn
->s_shdr
, src
, (size_t) 1, swapbytes
);
114 if (ec
== ELFCLASS32
) {
115 scn
->s_offset
= scn
->s_rawoff
=
116 scn
->s_shdr
.s_shdr32
.sh_offset
;
117 scn
->s_size
= scn
->s_shdr
.s_shdr32
.sh_size
;
119 scn
->s_offset
= scn
->s_rawoff
=
120 scn
->s_shdr
.s_shdr64
.sh_offset
;
121 scn
->s_size
= scn
->s_shdr
.s_shdr64
.sh_size
;
125 e
->e_flags
|= LIBELF_F_SHDRS_LOADED
;
132 elf_getscn(Elf
*e
, size_t index
)
138 if (e
== NULL
|| e
->e_kind
!= ELF_K_ELF
||
139 ((ec
= e
->e_class
) != ELFCLASS32
&& ec
!= ELFCLASS64
)) {
140 LIBELF_SET_ERROR(ARGUMENT
, 0);
144 if ((ehdr
= _libelf_ehdr(e
, ec
, 0)) == NULL
)
147 if (e
->e_cmd
!= ELF_C_WRITE
&&
148 (e
->e_flags
& LIBELF_F_SHDRS_LOADED
) == 0 &&
149 _libelf_load_scn(e
, ehdr
) == 0)
152 STAILQ_FOREACH(s
, &e
->e_u
.e_elf
.e_scn
, s_next
)
153 if (s
->s_ndx
== index
)
156 LIBELF_SET_ERROR(ARGUMENT
, 0);
161 elf_ndxscn(Elf_Scn
*s
)
164 LIBELF_SET_ERROR(ARGUMENT
, 0);
177 if (e
== NULL
|| e
->e_kind
!= ELF_K_ELF
) {
178 LIBELF_SET_ERROR(ARGUMENT
, 0);
182 if ((ec
= e
->e_class
) != ELFCLASS32
&& ec
!= ELFCLASS64
) {
183 LIBELF_SET_ERROR(CLASS
, 0);
187 if ((ehdr
= _libelf_ehdr(e
, ec
, 0)) == NULL
)
191 * The application may be asking for a new section descriptor
192 * on an ELF object opened with ELF_C_RDWR or ELF_C_READ. We
193 * need to bring in the existing section information before
194 * appending a new one to the list.
196 * Per the ELF(3) API, an application is allowed to open a
197 * file using ELF_C_READ, mess with its internal structure and
198 * use elf_update(...,ELF_C_NULL) to compute its new layout.
200 if (e
->e_cmd
!= ELF_C_WRITE
&&
201 (e
->e_flags
& LIBELF_F_SHDRS_LOADED
) == 0 &&
202 _libelf_load_scn(e
, ehdr
) == 0)
205 if (STAILQ_EMPTY(&e
->e_u
.e_elf
.e_scn
)) {
206 assert(e
->e_u
.e_elf
.e_nscn
== 0);
207 if ((scn
= _libelf_allocate_scn(e
, (size_t) SHN_UNDEF
)) ==
210 e
->e_u
.e_elf
.e_nscn
++;
213 assert(e
->e_u
.e_elf
.e_nscn
> 0);
215 if ((scn
= _libelf_allocate_scn(e
, e
->e_u
.e_elf
.e_nscn
)) == NULL
)
218 e
->e_u
.e_elf
.e_nscn
++;
220 (void) elf_flagscn(scn
, ELF_C_SET
, ELF_F_DIRTY
);
226 elf_nextscn(Elf
*e
, Elf_Scn
*s
)
228 if (e
== NULL
|| (e
->e_kind
!= ELF_K_ELF
) ||
229 (s
&& s
->s_elf
!= e
)) {
230 LIBELF_SET_ERROR(ARGUMENT
, 0);
234 return (s
== NULL
? elf_getscn(e
, (size_t) 1) :
235 STAILQ_NEXT(s
, s_next
));