1 /*-
2 * Copyright (c) 2006,2008 Joseph Koshy
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #include <assert.h>
28 #include <gelf.h>
29
30 #include "_libelf.h"
31
32 ELFTC_VCSID("$Id: elf_strptr.c,v 1.3 2022/12/27 17:10:06 jmc Exp $");
33
34 /*
35 * Convert an ELF section#,offset pair to a string pointer.
36 */
37
38 char *
elf_strptr(Elf * e,size_t scndx,size_t offset)39 elf_strptr(Elf *e, size_t scndx, size_t offset)
40 {
41 Elf_Scn *s;
42 Elf_Data *d;
43 GElf_Shdr shdr;
44 uint64_t alignment, count;
45
46 if (e == NULL || e->e_kind != ELF_K_ELF) {
47 LIBELF_SET_ERROR(ARGUMENT, 0);
48 return (NULL);
49 }
50
51 if ((s = elf_getscn(e, scndx)) == NULL ||
52 gelf_getshdr(s, &shdr) == NULL)
53 return (NULL);
54
55 if (shdr.sh_type != SHT_STRTAB ||
56 offset >= shdr.sh_size) {
57 LIBELF_SET_ERROR(ARGUMENT, 0);
58 return (NULL);
59 }
60
61 d = NULL;
62 if (e->e_flags & ELF_F_LAYOUT) {
63
64 /*
65 * The application is taking responsibility for the
66 * ELF object's layout, so we can directly translate
67 * an offset to a `char *' address using the `d_off'
68 * members of Elf_Data descriptors.
69 */
70 while ((d = elf_getdata(s, d)) != NULL) {
71
72 if (d->d_buf == 0 || d->d_size == 0)
73 continue;
74
75 if (d->d_type != ELF_T_BYTE) {
76 LIBELF_SET_ERROR(DATA, 0);
77 return (NULL);
78 }
79
80 if (offset >= d->d_off &&
81 offset < d->d_off + d->d_size)
82 return ((char *) d->d_buf + offset - d->d_off);
83 }
84 } else {
85 /*
86 * Otherwise, the `d_off' members are not usable and
87 * we need to compute offsets ourselves, taking into
88 * account 'holes' in coverage of the section introduced
89 * by alignment requirements.
90 */
91 count = (uint64_t) 0; /* cumulative count of bytes seen */
92 while ((d = elf_getdata(s, d)) != NULL && count <= offset) {
93
94 if (d->d_buf == NULL || d->d_size == 0)
95 continue;
96
97 if (d->d_type != ELF_T_BYTE) {
98 LIBELF_SET_ERROR(DATA, 0);
99 return (NULL);
100 }
101
102 if ((alignment = d->d_align) > 1) {
103 if ((alignment & (alignment - 1)) != 0) {
104 LIBELF_SET_ERROR(DATA, 0);
105 return (NULL);
106 }
107 count = roundup2(count, alignment);
108 }
109
110 if (offset < count) {
111 /* offset starts in the 'hole' */
112 LIBELF_SET_ERROR(ARGUMENT, 0);
113 return (NULL);
114 }
115
116 if (offset < count + d->d_size) {
117 if (d->d_buf != NULL)
118 return ((char *) d->d_buf +
119 offset - count);
120 LIBELF_SET_ERROR(DATA, 0);
121 return (NULL);
122 }
123
124 count += d->d_size;
125 }
126 }
127
128 LIBELF_SET_ERROR(ARGUMENT, 0);
129 return (NULL);
130 }
131