Branch data Line data Source code
1 : : /* Return section index of section header string table.
2 : : Copyright (C) 2002, 2005, 2009, 2014, 2015 Red Hat, Inc.
3 : : This file is part of elfutils.
4 : : Written by Ulrich Drepper <drepper@redhat.com>, 2002.
5 : :
6 : : This file is free software; you can redistribute it and/or modify
7 : : it under the terms of either
8 : :
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
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
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.
25 : :
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/>. */
29 : :
30 : : #ifdef HAVE_CONFIG_H
31 : : # include <config.h>
32 : : #endif
33 : :
34 : : #include <errno.h>
35 : : #include <gelf.h>
36 : : #include <stddef.h>
37 : :
38 : : #include "libelfP.h"
39 : : #include "common.h"
40 : :
41 : :
42 : : int
43 : 168263 : elf_getshdrstrndx (Elf *elf, size_t *dst)
44 : : {
45 : 168263 : int result = 0;
46 : :
47 [ - + ]: 168263 : if (elf == NULL)
48 : : return -1;
49 : :
50 [ - + ]: 168263 : if (unlikely (elf->kind != ELF_K_ELF))
51 : : {
52 : 0 : __libelf_seterrno (ELF_E_INVALID_HANDLE);
53 : 0 : return -1;
54 : : }
55 : :
56 : 168263 : rwlock_rdlock (elf->lock);
57 : :
58 : : /* We rely here on the fact that the `elf' element is a common prefix
59 : : of `elf32' and `elf64'. */
60 : 168263 : eu_static_assert (offsetof (struct Elf, state.elf.ehdr)
61 : : == offsetof (struct Elf, state.elf32.ehdr));
62 : 168263 : eu_static_assert (sizeof (elf->state.elf.ehdr)
63 : : == sizeof (elf->state.elf32.ehdr));
64 : 168263 : eu_static_assert (offsetof (struct Elf, state.elf.ehdr)
65 : : == offsetof (struct Elf, state.elf64.ehdr));
66 : 168263 : eu_static_assert (sizeof (elf->state.elf.ehdr)
67 : : == sizeof (elf->state.elf64.ehdr));
68 : :
69 [ - + ]: 168263 : if (unlikely (elf->state.elf.ehdr == NULL))
70 : : {
71 : 0 : __libelf_seterrno (ELF_E_WRONG_ORDER_EHDR);
72 : 0 : result = -1;
73 : : }
74 : : else
75 : : {
76 : 168263 : Elf32_Word num;
77 : :
78 : 336526 : num = (elf->class == ELFCLASS32
79 : 36864 : ? elf->state.elf32.ehdr->e_shstrndx
80 [ + + ]: 168263 : : elf->state.elf64.ehdr->e_shstrndx);
81 : :
82 : : /* Determine whether the index is too big to fit in the ELF
83 : : header. */
84 [ + + ]: 168263 : if (unlikely (num == SHN_XINDEX))
85 : : {
86 : : /* Yes. Search the zeroth section header. */
87 [ + + ]: 82 : if (elf->class == ELFCLASS32)
88 : : {
89 : 36 : size_t offset;
90 [ - + ]: 36 : if (unlikely (elf->state.elf32.scns.cnt == 0))
91 : : {
92 : : /* Cannot use SHN_XINDEX without section headers. */
93 : 0 : __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
94 : 0 : result = -1;
95 : 0 : goto out;
96 : : }
97 : :
98 [ + + ]: 36 : if (elf->state.elf32.scns.data[0].shdr.e32 != NULL)
99 : : {
100 : 24 : num = elf->state.elf32.scns.data[0].shdr.e32->sh_link;
101 : 24 : goto success;
102 : : }
103 : :
104 : 12 : offset = elf->state.elf32.ehdr->e_shoff;
105 : :
106 [ + + ]: 12 : if (elf->map_address != NULL
107 : 4 : && elf->state.elf32.ehdr->e_ident[EI_DATA] == MY_ELFDATA
108 [ + - ]: 4 : && (ALLOW_UNALIGNED
109 : : || (((size_t) ((char *) elf->map_address
110 : : + elf->start_offset + offset))
111 : : & (__alignof__ (Elf32_Shdr) - 1)) == 0))
112 : : {
113 : : /* First see whether the information in the ELF header is
114 : : valid and it does not ask for too much. */
115 [ - + ]: 4 : if (unlikely (elf->maximum_size - offset
116 : : < sizeof (Elf32_Shdr)))
117 : : {
118 : : /* Something is wrong. */
119 : 0 : __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
120 : 0 : result = -1;
121 : 0 : goto out;
122 : : }
123 : :
124 : : /* We can directly access the memory. */
125 : 4 : num = ((Elf32_Shdr *) (elf->map_address + elf->start_offset
126 : 4 : + offset))->sh_link;
127 : : }
128 : : else
129 : : {
130 : : /* We avoid reading in all the section headers. Just read
131 : : the first one. */
132 : 8 : Elf32_Shdr shdr_mem;
133 : 8 : ssize_t r;
134 : :
135 [ - + ]: 8 : if (unlikely ((r = pread_retry (elf->fildes, &shdr_mem,
136 : : sizeof (Elf32_Shdr), offset))
137 : : != sizeof (Elf32_Shdr)))
138 : : {
139 : : /* We must be able to read this ELF section header. */
140 [ # # ]: 0 : if (r < 0)
141 : 0 : __libelf_seterrno (ELF_E_INVALID_FILE);
142 : : else
143 : 0 : __libelf_seterrno (ELF_E_INVALID_ELF);
144 : 0 : result = -1;
145 : 0 : goto out;
146 : : }
147 : :
148 [ - + ]: 8 : if (elf->state.elf32.ehdr->e_ident[EI_DATA] != MY_ELFDATA)
149 : 0 : CONVERT (shdr_mem.sh_link);
150 : 8 : num = shdr_mem.sh_link;
151 : : }
152 : : }
153 : : else
154 : : {
155 [ - + ]: 46 : if (unlikely (elf->state.elf64.scns.cnt == 0))
156 : : {
157 : : /* Cannot use SHN_XINDEX without section headers. */
158 : 0 : __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
159 : 0 : result = -1;
160 : 0 : goto out;
161 : : }
162 : :
163 [ + + ]: 46 : if (elf->state.elf64.scns.data[0].shdr.e64 != NULL)
164 : : {
165 : 40 : num = elf->state.elf64.scns.data[0].shdr.e64->sh_link;
166 : 40 : goto success;
167 : : }
168 : :
169 : 6 : size_t offset = elf->state.elf64.ehdr->e_shoff;
170 : :
171 [ + + ]: 6 : if (elf->map_address != NULL
172 : 4 : && elf->state.elf64.ehdr->e_ident[EI_DATA] == MY_ELFDATA
173 [ + - ]: 4 : && (ALLOW_UNALIGNED
174 : : || (((size_t) ((char *) elf->map_address
175 : : + elf->start_offset + offset))
176 : : & (__alignof__ (Elf64_Shdr) - 1)) == 0))
177 : : {
178 : : /* First see whether the information in the ELF header is
179 : : valid and it does not ask for too much. */
180 [ - + ]: 4 : if (unlikely (elf->maximum_size - offset
181 : : < sizeof (Elf64_Shdr)))
182 : : {
183 : : /* Something is wrong. */
184 : 0 : __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
185 : 0 : result = -1;
186 : 0 : goto out;
187 : : }
188 : :
189 : : /* We can directly access the memory. */
190 : 4 : num = ((Elf64_Shdr *) (elf->map_address + elf->start_offset
191 : 4 : + offset))->sh_link;
192 : : }
193 : : else
194 : : {
195 : : /* We avoid reading in all the section headers. Just read
196 : : the first one. */
197 : 2 : Elf64_Shdr shdr_mem;
198 : 2 : ssize_t r;
199 : :
200 [ - + ]: 2 : if (unlikely ((r = pread_retry (elf->fildes, &shdr_mem,
201 : : sizeof (Elf64_Shdr), offset))
202 : : != sizeof (Elf64_Shdr)))
203 : : {
204 : : /* We must be able to read this ELF section header. */
205 [ # # ]: 0 : if (r < 0)
206 : 0 : __libelf_seterrno (ELF_E_INVALID_FILE);
207 : : else
208 : 0 : __libelf_seterrno (ELF_E_INVALID_ELF);
209 : 0 : result = -1;
210 : 0 : goto out;
211 : : }
212 : :
213 [ - + ]: 2 : if (elf->state.elf64.ehdr->e_ident[EI_DATA] != MY_ELFDATA)
214 : 0 : CONVERT (shdr_mem.sh_link);
215 : 2 : num = shdr_mem.sh_link;
216 : : }
217 : : }
218 : : }
219 : :
220 : : /* Store the result. */
221 : 168181 : success:
222 : 168263 : *dst = num;
223 : : }
224 : :
225 : : out:
226 : : rwlock_unlock (elf->lock);
227 : :
228 : : return result;
229 : : }
230 : : INTDEF(elf_getshdrstrndx)
231 : : /* Alias for the deprecated name. */
232 : : strong_alias (elf_getshdrstrndx, elf_getshstrndx)
|