blob: 1f404292744f65d3ce1e4f3a497f7b0c5a11b574 [file] [log] [blame]
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001/* Return string pointer from string section.
Ulrich Drepperd11522f2009-02-01 16:20:40 -08002 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2008, 2009 Red Hat, Inc.
Mark Wielaardde2ed972012-06-05 17:15:16 +02003 This file is part of elfutils.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00004 Contributed by Ulrich Drepper <drepper@redhat.com>, 1998.
5
Mark Wielaardde2ed972012-06-05 17:15:16 +02006 This file is free software; you can redistribute it and/or modify
7 it under the terms of either
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00008
Mark Wielaardde2ed972012-06-05 17:15:16 +02009 * 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
12
13 or
14
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
18
19 or both in parallel, as here.
20
21 elfutils is distributed in the hope that it will be useful, but
Ulrich Drepper361df7d2006-04-04 21:38:57 +000022 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.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000025
Mark Wielaardde2ed972012-06-05 17:15:16 +020026 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/>. */
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000029
30#ifdef HAVE_CONFIG_H
31# include <config.h>
32#endif
33
34#include <libelf.h>
35#include <stddef.h>
36
37#include "libelfP.h"
38
39
40char *
41elf_strptr (elf, idx, offset)
42 Elf *elf;
43 size_t idx;
44 size_t offset;
45{
46 if (elf == NULL)
47 return NULL;
48
49 if (elf->kind != ELF_K_ELF)
50 {
51 __libelf_seterrno (ELF_E_INVALID_HANDLE);
52 return NULL;
53 }
54
Roland McGrathb4d6f0f2008-08-25 22:55:17 +000055 rwlock_rdlock (elf->lock);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000056
57 char *result = NULL;
58 Elf_Scn *strscn;
59
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);
65 while (1)
66 {
67 if (idx < runp->max)
68 {
69 if (idx < runp->cnt)
70 strscn = &runp->data[idx];
71 else
72 {
73 __libelf_seterrno (ELF_E_INVALID_INDEX);
74 goto out;
75 }
76 break;
77 }
78
79 idx -= runp->max;
80
81 runp = runp->next;
82 if (runp == NULL)
83 {
84 __libelf_seterrno (ELF_E_INVALID_INDEX);
85 goto out;
86 }
87 }
88
89 if (elf->class == ELFCLASS32)
90 {
91 if (unlikely (strscn->shdr.e32->sh_type != SHT_STRTAB))
92 {
93 /* This is no string section. */
94 __libelf_seterrno (ELF_E_INVALID_SECTION);
95 goto out;
96 }
97
98 if (unlikely (offset >= strscn->shdr.e32->sh_size))
99 {
100 /* The given offset is too big, it is beyond this section. */
101 __libelf_seterrno (ELF_E_OFFSET_RANGE);
102 goto out;
103 }
104 }
105 else
106 {
107 if (unlikely (strscn->shdr.e64->sh_type != SHT_STRTAB))
108 {
109 /* This is no string section. */
110 __libelf_seterrno (ELF_E_INVALID_SECTION);
111 goto out;
112 }
113
114 if (unlikely (offset >= strscn->shdr.e64->sh_size))
115 {
116 /* The given offset is too big, it is beyond this section. */
117 __libelf_seterrno (ELF_E_OFFSET_RANGE);
118 goto out;
119 }
120 }
121
Roland McGrathb4d6f0f2008-08-25 22:55:17 +0000122 if (strscn->rawdata_base == NULL && ! strscn->data_read)
123 {
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)
129 goto out;
130 }
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000131
Ulrich Drepper89759c72008-01-31 19:44:12 +0000132 if (likely (strscn->rawdata_base != NULL))
Ulrich Drepperd11522f2009-02-01 16:20:40 -0800133 // 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
136 // XXX list.
Ulrich Drepper89759c72008-01-31 19:44:12 +0000137 result = &strscn->rawdata_base[offset];
138 else
139 {
140 /* This is a file which is currently created. Use the list of
141 data blocks. */
142 struct Elf_Data_List *dl = &strscn->data_list;
143 while (dl != NULL)
144 {
145 if (offset >= (size_t) dl->data.d.d_off
146 && offset < dl->data.d.d_off + dl->data.d.d_size)
147 {
148 result = (char *) dl->data.d.d_buf + (offset - dl->data.d.d_off);
149 break;
150 }
151
152 dl = dl->next;
153 }
154 }
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000155
156 out:
Roland McGrathb4d6f0f2008-08-25 22:55:17 +0000157 rwlock_unlock (elf->lock);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000158
159 return result;
160}
161INTDEF(elf_strptr)