1 /* $NetBSD: elf_scn.c,v 1.2 2014/03/09 16:58:04 christos Exp $ */
4 * Copyright (c) 2006,2008-2010 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 #if HAVE_NBTOOL_CONFIG_H
30 # include "nbtool_config.h"
33 #include <sys/cdefs.h>
34 #include <sys/queue.h>
45 __RCSID("$NetBSD: elf_scn.c,v 1.2 2014/03/09 16:58:04 christos Exp $");
46 ELFTC_VCSID("Id: elf_scn.c 2225 2011-11-26 18:55:54Z jkoshy ");
49 * Load an ELF section table and create a list of Elf_Scn structures.
52 _libelf_load_section_headers(Elf
*e
, void *ehdr
)
61 int (*xlator
)(char *_d
, size_t _dsz
, char *_s
, size_t _c
, int _swap
);
65 assert((e
->e_flags
& LIBELF_F_SHDRS_LOADED
) == 0);
67 #define CHECK_EHDR(E,EH) do { \
68 if (fsz != (EH)->e_shentsize || \
69 shoff + fsz * shnum > e->e_rawsize) { \
70 LIBELF_SET_ERROR(HEADER, 0); \
76 fsz
= _libelf_fsize(ELF_T_SHDR
, ec
, e
->e_version
, (size_t) 1);
79 shnum
= e
->e_u
.e_elf
.e_nscn
;
81 if (ec
== ELFCLASS32
) {
82 eh32
= (Elf32_Ehdr
*) ehdr
;
83 shoff
= (uint64_t) eh32
->e_shoff
;
86 eh64
= (Elf64_Ehdr
*) ehdr
;
87 shoff
= eh64
->e_shoff
;
91 xlator
= _libelf_get_translator(ELF_T_SHDR
, ELF_TOMEMORY
, ec
);
93 swapbytes
= e
->e_byteorder
!= _libelf_host_byteorder();
94 if (shoff
> SSIZE_MAX
) {
95 LIBELF_SET_ERROR(HEADER
, 0);
98 src
= e
->e_rawfile
+ (ssize_t
)shoff
;
101 * If the file is using extended numbering then section #0
102 * would have already been read in.
106 if (!STAILQ_EMPTY(&e
->e_u
.e_elf
.e_scn
)) {
107 assert(STAILQ_FIRST(&e
->e_u
.e_elf
.e_scn
) ==
108 STAILQ_LAST(&e
->e_u
.e_elf
.e_scn
, _Elf_Scn
, s_next
));
114 for (; i
< shnum
; i
++, src
+= fsz
) {
115 if ((scn
= _libelf_allocate_scn(e
, i
)) == NULL
)
118 (*xlator
)((void *) &scn
->s_shdr
, sizeof(scn
->s_shdr
), src
,
119 (size_t) 1, swapbytes
);
121 if (ec
== ELFCLASS32
) {
122 scn
->s_offset
= scn
->s_rawoff
=
123 scn
->s_shdr
.s_shdr32
.sh_offset
;
124 scn
->s_size
= scn
->s_shdr
.s_shdr32
.sh_size
;
126 scn
->s_offset
= scn
->s_rawoff
=
127 scn
->s_shdr
.s_shdr64
.sh_offset
;
128 scn
->s_size
= scn
->s_shdr
.s_shdr64
.sh_size
;
132 e
->e_flags
|= LIBELF_F_SHDRS_LOADED
;
139 elf_getscn(Elf
*e
, size_t index
)
145 if (e
== NULL
|| e
->e_kind
!= ELF_K_ELF
||
146 ((ec
= e
->e_class
) != ELFCLASS32
&& ec
!= ELFCLASS64
)) {
147 LIBELF_SET_ERROR(ARGUMENT
, 0);
151 if ((ehdr
= _libelf_ehdr(e
, ec
, 0)) == NULL
)
154 if (e
->e_cmd
!= ELF_C_WRITE
&&
155 (e
->e_flags
& LIBELF_F_SHDRS_LOADED
) == 0 &&
156 _libelf_load_section_headers(e
, ehdr
) == 0)
159 STAILQ_FOREACH(s
, &e
->e_u
.e_elf
.e_scn
, s_next
)
160 if (s
->s_ndx
== index
)
163 LIBELF_SET_ERROR(ARGUMENT
, 0);
168 elf_ndxscn(Elf_Scn
*s
)
171 LIBELF_SET_ERROR(ARGUMENT
, 0);
184 if (e
== NULL
|| e
->e_kind
!= ELF_K_ELF
) {
185 LIBELF_SET_ERROR(ARGUMENT
, 0);
189 if ((ec
= e
->e_class
) != ELFCLASS32
&& ec
!= ELFCLASS64
) {
190 LIBELF_SET_ERROR(CLASS
, 0);
194 if ((ehdr
= _libelf_ehdr(e
, ec
, 0)) == NULL
)
198 * The application may be asking for a new section descriptor
199 * on an ELF object opened with ELF_C_RDWR or ELF_C_READ. We
200 * need to bring in the existing section information before
201 * appending a new one to the list.
203 * Per the ELF(3) API, an application is allowed to open a
204 * file using ELF_C_READ, mess with its internal structure and
205 * use elf_update(...,ELF_C_NULL) to compute its new layout.
207 if (e
->e_cmd
!= ELF_C_WRITE
&&
208 (e
->e_flags
& LIBELF_F_SHDRS_LOADED
) == 0 &&
209 _libelf_load_section_headers(e
, ehdr
) == 0)
212 if (STAILQ_EMPTY(&e
->e_u
.e_elf
.e_scn
)) {
213 assert(e
->e_u
.e_elf
.e_nscn
== 0);
214 if ((scn
= _libelf_allocate_scn(e
, (size_t) SHN_UNDEF
)) ==
217 e
->e_u
.e_elf
.e_nscn
++;
220 assert(e
->e_u
.e_elf
.e_nscn
> 0);
222 if ((scn
= _libelf_allocate_scn(e
, e
->e_u
.e_elf
.e_nscn
)) == NULL
)
225 e
->e_u
.e_elf
.e_nscn
++;
227 (void) elf_flagscn(scn
, ELF_C_SET
, ELF_F_DIRTY
);
233 elf_nextscn(Elf
*e
, Elf_Scn
*s
)
235 if (e
== NULL
|| (e
->e_kind
!= ELF_K_ELF
) ||
236 (s
&& s
->s_elf
!= e
)) {
237 LIBELF_SET_ERROR(ARGUMENT
, 0);
241 return (s
== NULL
? elf_getscn(e
, (size_t) 1) :
242 STAILQ_NEXT(s
, s_next
));