1 /* Return string pointer from string section.
2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2008, 2009 Red Hat, Inc.
3 This file is part of elfutils.
4 Contributed by Ulrich Drepper <drepper@redhat.com>, 1998.
6 This file is free software; you can redistribute it and/or modify
7 it under the terms of either
9 * the GNU Lesser General Public License as published by the Free
10 Software Foundation; either version 3 of the License, or (at
11 your option) any later version
15 * the GNU General Public License as published by the Free
16 Software Foundation; either version 2 of the License, or (at
17 your option) any later version
19 or both in parallel, as here.
21 elfutils is distributed in the hope that it will be useful, but
22 WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 General Public License for more details.
26 You should have received copies of the GNU General Public License and
27 the GNU Lesser General Public License along with this program. If
28 not, see <http://www.gnu.org/licenses/>. */
41 elf_strptr (elf, idx, offset)
49 if (elf->kind != ELF_K_ELF)
51 __libelf_seterrno (ELF_E_INVALID_HANDLE);
55 rwlock_rdlock (elf->lock);
60 /* Find the section in the list. */
61 Elf_ScnList *runp = (elf->class == ELFCLASS32
62 || (offsetof (struct Elf, state.elf32.scns)
63 == offsetof (struct Elf, state.elf64.scns))
64 ? &elf->state.elf32.scns : &elf->state.elf64.scns);
70 strscn = &runp->data[idx];
73 __libelf_seterrno (ELF_E_INVALID_INDEX);
84 __libelf_seterrno (ELF_E_INVALID_INDEX);
89 if (elf->class == ELFCLASS32)
91 if (unlikely (strscn->shdr.e32->sh_type != SHT_STRTAB))
93 /* This is no string section. */
94 __libelf_seterrno (ELF_E_INVALID_SECTION);
98 if (unlikely (offset >= strscn->shdr.e32->sh_size))
100 /* The given offset is too big, it is beyond this section. */
101 __libelf_seterrno (ELF_E_OFFSET_RANGE);
107 if (unlikely (strscn->shdr.e64->sh_type != SHT_STRTAB))
109 /* This is no string section. */
110 __libelf_seterrno (ELF_E_INVALID_SECTION);
114 if (unlikely (offset >= strscn->shdr.e64->sh_size))
116 /* The given offset is too big, it is beyond this section. */
117 __libelf_seterrno (ELF_E_OFFSET_RANGE);
122 if (strscn->rawdata_base == NULL && ! strscn->data_read)
124 rwlock_unlock (elf->lock);
125 rwlock_wrlock (elf->lock);
126 if (strscn->rawdata_base == NULL && ! strscn->data_read
127 /* Read the section data. */
128 && __libelf_set_rawdata_wrlock (strscn) != 0)
132 if (likely (strscn->rawdata_base != NULL))
133 // XXX Is this correct if a file is read and then new data is added
134 // XXX to the string section? Likely needs to check offset against
135 // XXX size of rawdata_base buffer and then iterate over rest of the
137 result = &strscn->rawdata_base[offset];
140 /* This is a file which is currently created. Use the list of
142 struct Elf_Data_List *dl = &strscn->data_list;
145 if (offset >= (size_t) dl->data.d.d_off
146 && offset < dl->data.d.d_off + dl->data.d.d_size)
148 result = (char *) dl->data.d.d_buf + (offset - dl->data.d.d_off);
157 rwlock_unlock (elf->lock);