xref: /freebsd/contrib/elftoolchain/libelf/elf_strptr.c (revision 02e9120893770924227138ba49df1edb3896112a)
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 <sys/param.h>
28 
29 #include <assert.h>
30 #include <gelf.h>
31 
32 #include "_libelf.h"
33 
34 ELFTC_VCSID("$Id: elf_strptr.c 2990 2014-03-17 09:56:58Z jkoshy $");
35 
36 /*
37  * Convert an ELF section#,offset pair to a string pointer.
38  */
39 
40 char *
41 elf_strptr(Elf *e, size_t scndx, size_t offset)
42 {
43 	Elf_Scn *s;
44 	Elf_Data *d;
45 	GElf_Shdr shdr;
46 	uint64_t alignment, count;
47 
48 	if (e == NULL || e->e_kind != ELF_K_ELF) {
49 		LIBELF_SET_ERROR(ARGUMENT, 0);
50 		return (NULL);
51 	}
52 
53 	if ((s = elf_getscn(e, scndx)) == NULL ||
54 	    gelf_getshdr(s, &shdr) == NULL)
55 		return (NULL);
56 
57 	if (shdr.sh_type != SHT_STRTAB ||
58 	    offset >= shdr.sh_size) {
59 		LIBELF_SET_ERROR(ARGUMENT, 0);
60 		return (NULL);
61 	}
62 
63 	d = NULL;
64 	if (e->e_flags & ELF_F_LAYOUT) {
65 
66 		/*
67 		 * The application is taking responsibility for the
68 		 * ELF object's layout, so we can directly translate
69 		 * an offset to a `char *' address using the `d_off'
70 		 * members of Elf_Data descriptors.
71 		 */
72 		while ((d = elf_getdata(s, d)) != NULL) {
73 
74 			if (d->d_buf == 0 || d->d_size == 0)
75 				continue;
76 
77 			if (d->d_type != ELF_T_BYTE) {
78 				LIBELF_SET_ERROR(DATA, 0);
79 				return (NULL);
80 			}
81 
82 			if (offset >= d->d_off &&
83 			    offset < d->d_off + d->d_size)
84 				return ((char *) d->d_buf + offset - d->d_off);
85 		}
86 	} else {
87 		/*
88 		 * Otherwise, the `d_off' members are not useable and
89 		 * we need to compute offsets ourselves, taking into
90 		 * account 'holes' in coverage of the section introduced
91 		 * by alignment requirements.
92 		 */
93 		count = (uint64_t) 0;	/* cumulative count of bytes seen */
94 		while ((d = elf_getdata(s, d)) != NULL && count <= offset) {
95 
96 			if (d->d_buf == NULL || d->d_size == 0)
97 				continue;
98 
99 			if (d->d_type != ELF_T_BYTE) {
100 				LIBELF_SET_ERROR(DATA, 0);
101 				return (NULL);
102 			}
103 
104 			if ((alignment = d->d_align) > 1) {
105 				if ((alignment & (alignment - 1)) != 0) {
106 					LIBELF_SET_ERROR(DATA, 0);
107 					return (NULL);
108 				}
109 				count = roundup2(count, alignment);
110 			}
111 
112 			if (offset < count) {
113 				/* offset starts in the 'hole' */
114 				LIBELF_SET_ERROR(ARGUMENT, 0);
115 				return (NULL);
116 			}
117 
118 			if (offset < count + d->d_size) {
119 				if (d->d_buf != NULL)
120 					return ((char *) d->d_buf +
121 					    offset - count);
122 				LIBELF_SET_ERROR(DATA, 0);
123 				return (NULL);
124 			}
125 
126 			count += d->d_size;
127 		}
128 	}
129 
130 	LIBELF_SET_ERROR(ARGUMENT, 0);
131 	return (NULL);
132 }
133