1 /* $NetBSD: elf_strptr.c,v 1.2 2014/03/09 16:58:04 christos Exp $ */
4 * Copyright (c) 2006,2008 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/param.h>
40 __RCSID("$NetBSD: elf_strptr.c,v 1.2 2014/03/09 16:58:04 christos Exp $");
41 ELFTC_VCSID("Id: elf_strptr.c 2271 2011-12-03 17:06:35Z jkoshy ");
44 * Convert an ELF section#,offset pair to a string pointer.
48 elf_strptr(Elf
*e
, size_t scndx
, size_t offset
)
52 size_t alignment
, count
;
55 if (e
== NULL
|| e
->e_kind
!= ELF_K_ELF
) {
56 LIBELF_SET_ERROR(ARGUMENT
, 0);
60 if ((s
= elf_getscn(e
, scndx
)) == NULL
||
61 gelf_getshdr(s
, &shdr
) == NULL
)
64 if (shdr
.sh_type
!= SHT_STRTAB
||
65 offset
>= shdr
.sh_size
) {
66 LIBELF_SET_ERROR(ARGUMENT
, 0);
71 if (e
->e_flags
& ELF_F_LAYOUT
) {
74 * The application is taking responsibility for the
75 * ELF object's layout, so we can directly translate
76 * an offset to a `char *' address using the `d_off'
77 * members of Elf_Data descriptors.
79 while ((d
= elf_getdata(s
, d
)) != NULL
) {
81 if (d
->d_buf
== 0 || d
->d_size
== 0)
84 if (d
->d_type
!= ELF_T_BYTE
) {
85 LIBELF_SET_ERROR(DATA
, 0);
89 if (offset
>= d
->d_off
&&
90 offset
< d
->d_off
+ d
->d_size
)
91 return ((char *) d
->d_buf
+ offset
- d
->d_off
);
95 * Otherwise, the `d_off' members are not useable and
96 * we need to compute offsets ourselves, taking into
97 * account 'holes' in coverage of the section introduced
98 * by alignment requirements.
100 count
= (size_t) 0; /* cumulative count of bytes seen */
101 while ((d
= elf_getdata(s
, d
)) != NULL
&& count
<= offset
) {
103 if (d
->d_buf
== NULL
|| d
->d_size
== 0)
106 if (d
->d_type
!= ELF_T_BYTE
) {
107 LIBELF_SET_ERROR(DATA
, 0);
111 if ((alignment
= d
->d_align
) > 1) {
112 if ((alignment
& (alignment
- 1)) != 0) {
113 LIBELF_SET_ERROR(DATA
, 0);
116 count
= roundup2(count
, alignment
);
119 if (offset
< count
) {
120 /* offset starts in the 'hole' */
121 LIBELF_SET_ERROR(ARGUMENT
, 0);
125 if (offset
< count
+ d
->d_size
) {
126 if (d
->d_buf
!= NULL
)
127 return ((char *) d
->d_buf
+
129 LIBELF_SET_ERROR(DATA
, 0);
137 LIBELF_SET_ERROR(ARGUMENT
, 0);