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_strptr.c,v 1.2.2.1.2.1 2009/10/25 01:10:29 kensmith Exp $"); */
32 #include <sys/param.h>
40 * Convert an ELF section#,offset pair to a string pointer.
44 elf_strptr(Elf
*e
, size_t scndx
, size_t offset
)
48 size_t alignment
, count
;
51 if (e
== NULL
|| e
->e_kind
!= ELF_K_ELF
) {
52 LIBELF_SET_ERROR(ARGUMENT
, 0);
56 if ((s
= elf_getscn(e
, scndx
)) == NULL
||
57 gelf_getshdr(s
, &shdr
) == NULL
)
60 if (/*shdr.sh_type != SHT_STRTAB || */
61 offset
>= shdr
.sh_size
) {
62 LIBELF_SET_ERROR(ARGUMENT
, 0);
67 if (e
->e_flags
& ELF_F_LAYOUT
) {
70 * The application is taking responsibility for the
71 * ELF object's layout, so we can directly translate
72 * an offset to a `char *' address using the `d_off'
73 * members of Elf_Data descriptors.
75 while ((d
= elf_getdata(s
, d
)) != NULL
) {
77 if (d
->d_buf
== 0 || d
->d_size
== 0)
80 if (d
->d_type
!= ELF_T_BYTE
) {
81 LIBELF_SET_ERROR(DATA
, 0);
85 if (offset
>= d
->d_off
&&
86 offset
< d
->d_off
+ d
->d_size
)
87 return ((char *) d
->d_buf
+ offset
- d
->d_off
);
91 * Otherwise, the `d_off' members are not useable and
92 * we need to compute offsets ourselves, taking into
93 * account 'holes' in coverage of the section introduced
94 * by alignment requirements.
96 count
= (size_t) 0; /* cumulative count of bytes seen */
97 while ((d
= elf_getdata(s
, d
)) != NULL
&& count
<= offset
) {
99 if (d
->d_buf
== NULL
|| d
->d_size
== 0)
102 if (d
->d_type
!= ELF_T_BYTE
) {
103 LIBELF_SET_ERROR(DATA
, 0);
107 if ((alignment
= d
->d_align
) > 1) {
108 if ((alignment
& (alignment
- 1)) != 0) {
109 LIBELF_SET_ERROR(DATA
, 0);
112 count
= roundup2(count
, alignment
);
115 if (offset
< count
) {
116 /* offset starts in the 'hole' */
117 LIBELF_SET_ERROR(ARGUMENT
, 0);
121 if (offset
< count
+ d
->d_size
) {
122 if (d
->d_buf
!= NULL
)
123 return ((char *) d
->d_buf
+
125 LIBELF_SET_ERROR(DATA
, 0);
133 LIBELF_SET_ERROR(ARGUMENT
, 0);