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