1*7304104dSAndroid Build Coastguard Worker /* Return string pointer from string section.
2*7304104dSAndroid Build Coastguard Worker Copyright (C) 1998-2002, 2004, 2008, 2009, 2015 Red Hat, Inc.
3*7304104dSAndroid Build Coastguard Worker This file is part of elfutils.
4*7304104dSAndroid Build Coastguard Worker Contributed by Ulrich Drepper <[email protected]>, 1998.
5*7304104dSAndroid Build Coastguard Worker
6*7304104dSAndroid Build Coastguard Worker This file is free software; you can redistribute it and/or modify
7*7304104dSAndroid Build Coastguard Worker it under the terms of either
8*7304104dSAndroid Build Coastguard Worker
9*7304104dSAndroid Build Coastguard Worker * the GNU Lesser General Public License as published by the Free
10*7304104dSAndroid Build Coastguard Worker Software Foundation; either version 3 of the License, or (at
11*7304104dSAndroid Build Coastguard Worker your option) any later version
12*7304104dSAndroid Build Coastguard Worker
13*7304104dSAndroid Build Coastguard Worker or
14*7304104dSAndroid Build Coastguard Worker
15*7304104dSAndroid Build Coastguard Worker * the GNU General Public License as published by the Free
16*7304104dSAndroid Build Coastguard Worker Software Foundation; either version 2 of the License, or (at
17*7304104dSAndroid Build Coastguard Worker your option) any later version
18*7304104dSAndroid Build Coastguard Worker
19*7304104dSAndroid Build Coastguard Worker or both in parallel, as here.
20*7304104dSAndroid Build Coastguard Worker
21*7304104dSAndroid Build Coastguard Worker elfutils is distributed in the hope that it will be useful, but
22*7304104dSAndroid Build Coastguard Worker WITHOUT ANY WARRANTY; without even the implied warranty of
23*7304104dSAndroid Build Coastguard Worker MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24*7304104dSAndroid Build Coastguard Worker General Public License for more details.
25*7304104dSAndroid Build Coastguard Worker
26*7304104dSAndroid Build Coastguard Worker You should have received copies of the GNU General Public License and
27*7304104dSAndroid Build Coastguard Worker the GNU Lesser General Public License along with this program. If
28*7304104dSAndroid Build Coastguard Worker not, see <http://www.gnu.org/licenses/>. */
29*7304104dSAndroid Build Coastguard Worker
30*7304104dSAndroid Build Coastguard Worker #ifdef HAVE_CONFIG_H
31*7304104dSAndroid Build Coastguard Worker # include <config.h>
32*7304104dSAndroid Build Coastguard Worker #endif
33*7304104dSAndroid Build Coastguard Worker
34*7304104dSAndroid Build Coastguard Worker #include <libelf.h>
35*7304104dSAndroid Build Coastguard Worker #include <stdbool.h>
36*7304104dSAndroid Build Coastguard Worker #include <stddef.h>
37*7304104dSAndroid Build Coastguard Worker
38*7304104dSAndroid Build Coastguard Worker #include "libelfP.h"
39*7304104dSAndroid Build Coastguard Worker
40*7304104dSAndroid Build Coastguard Worker
41*7304104dSAndroid Build Coastguard Worker static void *
get_zdata(Elf_Scn * strscn)42*7304104dSAndroid Build Coastguard Worker get_zdata (Elf_Scn *strscn)
43*7304104dSAndroid Build Coastguard Worker {
44*7304104dSAndroid Build Coastguard Worker size_t zsize, zalign;
45*7304104dSAndroid Build Coastguard Worker void *zdata = __libelf_decompress_elf (strscn, &zsize, &zalign);
46*7304104dSAndroid Build Coastguard Worker if (zdata == NULL)
47*7304104dSAndroid Build Coastguard Worker return NULL;
48*7304104dSAndroid Build Coastguard Worker
49*7304104dSAndroid Build Coastguard Worker strscn->zdata_base = zdata;
50*7304104dSAndroid Build Coastguard Worker strscn->zdata_size = zsize;
51*7304104dSAndroid Build Coastguard Worker strscn->zdata_align = zalign;
52*7304104dSAndroid Build Coastguard Worker
53*7304104dSAndroid Build Coastguard Worker return zdata;
54*7304104dSAndroid Build Coastguard Worker }
55*7304104dSAndroid Build Coastguard Worker
validate_str(const char * str,size_t from,size_t to)56*7304104dSAndroid Build Coastguard Worker static bool validate_str (const char *str, size_t from, size_t to)
57*7304104dSAndroid Build Coastguard Worker {
58*7304104dSAndroid Build Coastguard Worker #if HAVE_DECL_MEMRCHR
59*7304104dSAndroid Build Coastguard Worker // Check end first, which is likely a zero terminator, to prevent function call
60*7304104dSAndroid Build Coastguard Worker return ((to > 0 && str[to - 1] == '\0')
61*7304104dSAndroid Build Coastguard Worker || (to - from > 0 && memrchr (&str[from], '\0', to - from - 1) != NULL));
62*7304104dSAndroid Build Coastguard Worker #else
63*7304104dSAndroid Build Coastguard Worker do {
64*7304104dSAndroid Build Coastguard Worker if (to <= from)
65*7304104dSAndroid Build Coastguard Worker return false;
66*7304104dSAndroid Build Coastguard Worker
67*7304104dSAndroid Build Coastguard Worker to--;
68*7304104dSAndroid Build Coastguard Worker } while (str[to]);
69*7304104dSAndroid Build Coastguard Worker
70*7304104dSAndroid Build Coastguard Worker return true;
71*7304104dSAndroid Build Coastguard Worker #endif
72*7304104dSAndroid Build Coastguard Worker }
73*7304104dSAndroid Build Coastguard Worker
74*7304104dSAndroid Build Coastguard Worker char *
elf_strptr(Elf * elf,size_t idx,size_t offset)75*7304104dSAndroid Build Coastguard Worker elf_strptr (Elf *elf, size_t idx, size_t offset)
76*7304104dSAndroid Build Coastguard Worker {
77*7304104dSAndroid Build Coastguard Worker if (elf == NULL)
78*7304104dSAndroid Build Coastguard Worker return NULL;
79*7304104dSAndroid Build Coastguard Worker
80*7304104dSAndroid Build Coastguard Worker if (elf->kind != ELF_K_ELF)
81*7304104dSAndroid Build Coastguard Worker {
82*7304104dSAndroid Build Coastguard Worker __libelf_seterrno (ELF_E_INVALID_HANDLE);
83*7304104dSAndroid Build Coastguard Worker return NULL;
84*7304104dSAndroid Build Coastguard Worker }
85*7304104dSAndroid Build Coastguard Worker
86*7304104dSAndroid Build Coastguard Worker rwlock_rdlock (elf->lock);
87*7304104dSAndroid Build Coastguard Worker
88*7304104dSAndroid Build Coastguard Worker char *result = NULL;
89*7304104dSAndroid Build Coastguard Worker Elf_Scn *strscn;
90*7304104dSAndroid Build Coastguard Worker
91*7304104dSAndroid Build Coastguard Worker /* Find the section in the list. */
92*7304104dSAndroid Build Coastguard Worker Elf_ScnList *runp = (elf->class == ELFCLASS32
93*7304104dSAndroid Build Coastguard Worker || (offsetof (struct Elf, state.elf32.scns)
94*7304104dSAndroid Build Coastguard Worker == offsetof (struct Elf, state.elf64.scns))
95*7304104dSAndroid Build Coastguard Worker ? &elf->state.elf32.scns : &elf->state.elf64.scns);
96*7304104dSAndroid Build Coastguard Worker while (1)
97*7304104dSAndroid Build Coastguard Worker {
98*7304104dSAndroid Build Coastguard Worker if (idx < runp->max)
99*7304104dSAndroid Build Coastguard Worker {
100*7304104dSAndroid Build Coastguard Worker if (idx < runp->cnt)
101*7304104dSAndroid Build Coastguard Worker strscn = &runp->data[idx];
102*7304104dSAndroid Build Coastguard Worker else
103*7304104dSAndroid Build Coastguard Worker {
104*7304104dSAndroid Build Coastguard Worker __libelf_seterrno (ELF_E_INVALID_INDEX);
105*7304104dSAndroid Build Coastguard Worker goto out;
106*7304104dSAndroid Build Coastguard Worker }
107*7304104dSAndroid Build Coastguard Worker break;
108*7304104dSAndroid Build Coastguard Worker }
109*7304104dSAndroid Build Coastguard Worker
110*7304104dSAndroid Build Coastguard Worker idx -= runp->max;
111*7304104dSAndroid Build Coastguard Worker
112*7304104dSAndroid Build Coastguard Worker runp = runp->next;
113*7304104dSAndroid Build Coastguard Worker if (runp == NULL)
114*7304104dSAndroid Build Coastguard Worker {
115*7304104dSAndroid Build Coastguard Worker __libelf_seterrno (ELF_E_INVALID_INDEX);
116*7304104dSAndroid Build Coastguard Worker goto out;
117*7304104dSAndroid Build Coastguard Worker }
118*7304104dSAndroid Build Coastguard Worker }
119*7304104dSAndroid Build Coastguard Worker
120*7304104dSAndroid Build Coastguard Worker size_t sh_size = 0;
121*7304104dSAndroid Build Coastguard Worker if (elf->class == ELFCLASS32)
122*7304104dSAndroid Build Coastguard Worker {
123*7304104dSAndroid Build Coastguard Worker Elf32_Shdr *shdr = strscn->shdr.e32 ?: __elf32_getshdr_rdlock (strscn);
124*7304104dSAndroid Build Coastguard Worker if (unlikely (shdr == NULL || shdr->sh_type != SHT_STRTAB))
125*7304104dSAndroid Build Coastguard Worker {
126*7304104dSAndroid Build Coastguard Worker /* This is no string section. */
127*7304104dSAndroid Build Coastguard Worker __libelf_seterrno (ELF_E_INVALID_SECTION);
128*7304104dSAndroid Build Coastguard Worker goto out;
129*7304104dSAndroid Build Coastguard Worker }
130*7304104dSAndroid Build Coastguard Worker
131*7304104dSAndroid Build Coastguard Worker if ((shdr->sh_flags & SHF_COMPRESSED) == 0)
132*7304104dSAndroid Build Coastguard Worker sh_size = shdr->sh_size;
133*7304104dSAndroid Build Coastguard Worker else
134*7304104dSAndroid Build Coastguard Worker {
135*7304104dSAndroid Build Coastguard Worker if (strscn->zdata_base == NULL && get_zdata (strscn) == NULL)
136*7304104dSAndroid Build Coastguard Worker goto out;
137*7304104dSAndroid Build Coastguard Worker sh_size = strscn->zdata_size;
138*7304104dSAndroid Build Coastguard Worker }
139*7304104dSAndroid Build Coastguard Worker
140*7304104dSAndroid Build Coastguard Worker if (unlikely (offset >= sh_size))
141*7304104dSAndroid Build Coastguard Worker {
142*7304104dSAndroid Build Coastguard Worker /* The given offset is too big, it is beyond this section. */
143*7304104dSAndroid Build Coastguard Worker __libelf_seterrno (ELF_E_OFFSET_RANGE);
144*7304104dSAndroid Build Coastguard Worker goto out;
145*7304104dSAndroid Build Coastguard Worker }
146*7304104dSAndroid Build Coastguard Worker }
147*7304104dSAndroid Build Coastguard Worker else
148*7304104dSAndroid Build Coastguard Worker {
149*7304104dSAndroid Build Coastguard Worker Elf64_Shdr *shdr = strscn->shdr.e64 ?: __elf64_getshdr_rdlock (strscn);
150*7304104dSAndroid Build Coastguard Worker if (unlikely (shdr == NULL || shdr->sh_type != SHT_STRTAB))
151*7304104dSAndroid Build Coastguard Worker {
152*7304104dSAndroid Build Coastguard Worker /* This is no string section. */
153*7304104dSAndroid Build Coastguard Worker __libelf_seterrno (ELF_E_INVALID_SECTION);
154*7304104dSAndroid Build Coastguard Worker goto out;
155*7304104dSAndroid Build Coastguard Worker }
156*7304104dSAndroid Build Coastguard Worker
157*7304104dSAndroid Build Coastguard Worker if ((shdr->sh_flags & SHF_COMPRESSED) == 0)
158*7304104dSAndroid Build Coastguard Worker sh_size = shdr->sh_size;
159*7304104dSAndroid Build Coastguard Worker else
160*7304104dSAndroid Build Coastguard Worker {
161*7304104dSAndroid Build Coastguard Worker if (strscn->zdata_base == NULL && get_zdata (strscn) == NULL)
162*7304104dSAndroid Build Coastguard Worker goto out;
163*7304104dSAndroid Build Coastguard Worker sh_size = strscn->zdata_size;
164*7304104dSAndroid Build Coastguard Worker }
165*7304104dSAndroid Build Coastguard Worker
166*7304104dSAndroid Build Coastguard Worker if (unlikely (offset >= sh_size))
167*7304104dSAndroid Build Coastguard Worker {
168*7304104dSAndroid Build Coastguard Worker /* The given offset is too big, it is beyond this section. */
169*7304104dSAndroid Build Coastguard Worker __libelf_seterrno (ELF_E_OFFSET_RANGE);
170*7304104dSAndroid Build Coastguard Worker goto out;
171*7304104dSAndroid Build Coastguard Worker }
172*7304104dSAndroid Build Coastguard Worker }
173*7304104dSAndroid Build Coastguard Worker
174*7304104dSAndroid Build Coastguard Worker if (strscn->rawdata_base == NULL && ! strscn->data_read)
175*7304104dSAndroid Build Coastguard Worker {
176*7304104dSAndroid Build Coastguard Worker rwlock_unlock (elf->lock);
177*7304104dSAndroid Build Coastguard Worker rwlock_wrlock (elf->lock);
178*7304104dSAndroid Build Coastguard Worker if (strscn->rawdata_base == NULL && ! strscn->data_read
179*7304104dSAndroid Build Coastguard Worker /* Read the section data. */
180*7304104dSAndroid Build Coastguard Worker && __libelf_set_rawdata_wrlock (strscn) != 0)
181*7304104dSAndroid Build Coastguard Worker goto out;
182*7304104dSAndroid Build Coastguard Worker }
183*7304104dSAndroid Build Coastguard Worker
184*7304104dSAndroid Build Coastguard Worker if (unlikely (strscn->zdata_base != NULL))
185*7304104dSAndroid Build Coastguard Worker {
186*7304104dSAndroid Build Coastguard Worker /* Make sure the string is NUL terminated. Start from the end,
187*7304104dSAndroid Build Coastguard Worker which very likely is a NUL char. */
188*7304104dSAndroid Build Coastguard Worker if (likely (validate_str (strscn->zdata_base, offset, sh_size)))
189*7304104dSAndroid Build Coastguard Worker result = &strscn->zdata_base[offset];
190*7304104dSAndroid Build Coastguard Worker else
191*7304104dSAndroid Build Coastguard Worker __libelf_seterrno (ELF_E_INVALID_INDEX);
192*7304104dSAndroid Build Coastguard Worker }
193*7304104dSAndroid Build Coastguard Worker else if (likely (strscn->data_list_rear == NULL))
194*7304104dSAndroid Build Coastguard Worker {
195*7304104dSAndroid Build Coastguard Worker // XXX The above is currently correct since elf_newdata will
196*7304104dSAndroid Build Coastguard Worker // make sure to convert the rawdata into the datalist if
197*7304104dSAndroid Build Coastguard Worker // necessary. But it would be more efficient to keep the rawdata
198*7304104dSAndroid Build Coastguard Worker // unconverted and only then iterate over the rest of the (newly
199*7304104dSAndroid Build Coastguard Worker // added data) list. Note that when the ELF file is mmapped
200*7304104dSAndroid Build Coastguard Worker // rawdata_base can be set while rawdata.d hasn't been
201*7304104dSAndroid Build Coastguard Worker // initialized yet (when data_read is zero). So we cannot just
202*7304104dSAndroid Build Coastguard Worker // look at the rawdata.d.d_size.
203*7304104dSAndroid Build Coastguard Worker
204*7304104dSAndroid Build Coastguard Worker /* Make sure the string is NUL terminated. Start from the end,
205*7304104dSAndroid Build Coastguard Worker which very likely is a NUL char. */
206*7304104dSAndroid Build Coastguard Worker if (likely (validate_str (strscn->rawdata_base, offset, sh_size)))
207*7304104dSAndroid Build Coastguard Worker result = &strscn->rawdata_base[offset];
208*7304104dSAndroid Build Coastguard Worker else
209*7304104dSAndroid Build Coastguard Worker __libelf_seterrno (ELF_E_INVALID_INDEX);
210*7304104dSAndroid Build Coastguard Worker }
211*7304104dSAndroid Build Coastguard Worker else
212*7304104dSAndroid Build Coastguard Worker {
213*7304104dSAndroid Build Coastguard Worker /* This is a file which is currently created. Use the list of
214*7304104dSAndroid Build Coastguard Worker data blocks. */
215*7304104dSAndroid Build Coastguard Worker struct Elf_Data_List *dl = &strscn->data_list;
216*7304104dSAndroid Build Coastguard Worker while (dl != NULL)
217*7304104dSAndroid Build Coastguard Worker {
218*7304104dSAndroid Build Coastguard Worker if (offset >= (size_t) dl->data.d.d_off
219*7304104dSAndroid Build Coastguard Worker && offset < dl->data.d.d_off + dl->data.d.d_size)
220*7304104dSAndroid Build Coastguard Worker {
221*7304104dSAndroid Build Coastguard Worker /* Make sure the string is NUL terminated. Start from
222*7304104dSAndroid Build Coastguard Worker the end, which very likely is a NUL char. */
223*7304104dSAndroid Build Coastguard Worker if (likely (validate_str ((char *) dl->data.d.d_buf,
224*7304104dSAndroid Build Coastguard Worker offset - dl->data.d.d_off,
225*7304104dSAndroid Build Coastguard Worker dl->data.d.d_size)))
226*7304104dSAndroid Build Coastguard Worker result = ((char *) dl->data.d.d_buf
227*7304104dSAndroid Build Coastguard Worker + (offset - dl->data.d.d_off));
228*7304104dSAndroid Build Coastguard Worker else
229*7304104dSAndroid Build Coastguard Worker __libelf_seterrno (ELF_E_INVALID_INDEX);
230*7304104dSAndroid Build Coastguard Worker break;
231*7304104dSAndroid Build Coastguard Worker }
232*7304104dSAndroid Build Coastguard Worker
233*7304104dSAndroid Build Coastguard Worker dl = dl->next;
234*7304104dSAndroid Build Coastguard Worker }
235*7304104dSAndroid Build Coastguard Worker }
236*7304104dSAndroid Build Coastguard Worker
237*7304104dSAndroid Build Coastguard Worker out:
238*7304104dSAndroid Build Coastguard Worker rwlock_unlock (elf->lock);
239*7304104dSAndroid Build Coastguard Worker
240*7304104dSAndroid Build Coastguard Worker return result;
241*7304104dSAndroid Build Coastguard Worker }
242*7304104dSAndroid Build Coastguard Worker INTDEF(elf_strptr)
243