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