1 /* $NetBSD: elf_strptr.c,v 1.2 2014/03/09 16:58:04 christos Exp $ */
2
3 /*-
4 * Copyright (c) 2006,2008 Joseph Koshy
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
15 *
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
26 * SUCH DAMAGE.
27 */
28
29 #if HAVE_NBTOOL_CONFIG_H
30 # include "nbtool_config.h"
31 #endif
32
33 #include <sys/param.h>
34
35 #include <assert.h>
36 #include <gelf.h>
37
38 #include "_libelf.h"
39
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 ");
42
43 /*
44 * Convert an ELF section#,offset pair to a string pointer.
45 */
46
47 char *
elf_strptr(Elf * e,size_t scndx,size_t offset)48 elf_strptr(Elf *e, size_t scndx, size_t offset)
49 {
50 Elf_Scn *s;
51 Elf_Data *d;
52 size_t alignment, count;
53 GElf_Shdr shdr;
54
55 if (e == NULL || e->e_kind != ELF_K_ELF) {
56 LIBELF_SET_ERROR(ARGUMENT, 0);
57 return (NULL);
58 }
59
60 if ((s = elf_getscn(e, scndx)) == NULL ||
61 gelf_getshdr(s, &shdr) == NULL)
62 return (NULL);
63
64 if (shdr.sh_type != SHT_STRTAB ||
65 offset >= shdr.sh_size) {
66 LIBELF_SET_ERROR(ARGUMENT, 0);
67 return (NULL);
68 }
69
70 d = NULL;
71 if (e->e_flags & ELF_F_LAYOUT) {
72
73 /*
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.
78 */
79 while ((d = elf_getdata(s, d)) != NULL) {
80
81 if (d->d_buf == 0 || d->d_size == 0)
82 continue;
83
84 if (d->d_type != ELF_T_BYTE) {
85 LIBELF_SET_ERROR(DATA, 0);
86 return (NULL);
87 }
88
89 if (offset >= d->d_off &&
90 offset < d->d_off + d->d_size)
91 return ((char *) d->d_buf + offset - d->d_off);
92 }
93 } else {
94 /*
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.
99 */
100 count = (size_t) 0; /* cumulative count of bytes seen */
101 while ((d = elf_getdata(s, d)) != NULL && count <= offset) {
102
103 if (d->d_buf == NULL || d->d_size == 0)
104 continue;
105
106 if (d->d_type != ELF_T_BYTE) {
107 LIBELF_SET_ERROR(DATA, 0);
108 return (NULL);
109 }
110
111 if ((alignment = d->d_align) > 1) {
112 if ((alignment & (alignment - 1)) != 0) {
113 LIBELF_SET_ERROR(DATA, 0);
114 return (NULL);
115 }
116 count = roundup2(count, alignment);
117 }
118
119 if (offset < count) {
120 /* offset starts in the 'hole' */
121 LIBELF_SET_ERROR(ARGUMENT, 0);
122 return (NULL);
123 }
124
125 if (offset < count + d->d_size) {
126 if (d->d_buf != NULL)
127 return ((char *) d->d_buf +
128 offset - count);
129 LIBELF_SET_ERROR(DATA, 0);
130 return (NULL);
131 }
132
133 count += d->d_size;
134 }
135 }
136
137 LIBELF_SET_ERROR(ARGUMENT, 0);
138 return (NULL);
139 }
140