blob: 6f8d66e8065210b027a91fb05ecf2d5a3ce64b33 [file] [log] [blame]
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001/* Return section index of section header string table.
Jakub Jelinek720383c2014-01-17 19:36:16 +01002 Copyright (C) 2002, 2005, 2009, 2014 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 Written by Ulrich Drepper <drepper@redhat.com>, 2002.
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 <assert.h>
Ulrich Drepperfbe998a2005-08-29 16:27:10 +000035#include <errno.h>
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000036#include <gelf.h>
37#include <stddef.h>
38#include <unistd.h>
39
Ulrich Drepperfbe998a2005-08-29 16:27:10 +000040#include <system.h>
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000041#include "libelfP.h"
42#include "common.h"
43
44
45int
Ulrich Drepperf1894932009-06-13 15:55:42 -070046elf_getshdrstrndx (elf, dst)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000047 Elf *elf;
48 size_t *dst;
49{
50 int result = 0;
51
52 if (elf == NULL)
53 return -1;
54
55 if (unlikely (elf->kind != ELF_K_ELF))
56 {
57 __libelf_seterrno (ELF_E_INVALID_HANDLE);
58 return -1;
59 }
60
Roland McGrathb4d6f0f2008-08-25 22:55:17 +000061 rwlock_rdlock (elf->lock);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000062
63 /* We rely here on the fact that the `elf' element is a common prefix
64 of `elf32' and `elf64'. */
65 assert (offsetof (struct Elf, state.elf.ehdr)
66 == offsetof (struct Elf, state.elf32.ehdr));
67 assert (sizeof (elf->state.elf.ehdr)
68 == sizeof (elf->state.elf32.ehdr));
69 assert (offsetof (struct Elf, state.elf.ehdr)
70 == offsetof (struct Elf, state.elf64.ehdr));
71 assert (sizeof (elf->state.elf.ehdr)
72 == sizeof (elf->state.elf64.ehdr));
73
74 if (unlikely (elf->state.elf.ehdr == NULL))
75 {
76 __libelf_seterrno (ELF_E_WRONG_ORDER_EHDR);
77 result = -1;
78 }
79 else
80 {
81 Elf32_Word num;
82
83 num = (elf->class == ELFCLASS32
84 ? elf->state.elf32.ehdr->e_shstrndx
85 : elf->state.elf64.ehdr->e_shstrndx);
86
87 /* Determine whether the index is too big to fit in the ELF
88 header. */
89 if (unlikely (num == SHN_XINDEX))
90 {
91 /* Yes. Search the zeroth section header. */
92 if (elf->class == ELFCLASS32)
93 {
94 size_t offset;
Mark Wielaardbabcdef2014-11-17 00:42:53 +010095 if (unlikely (elf->state.elf32.scns.cnt == 0))
96 {
97 /* Cannot use SHN_XINDEX without section headers. */
98 __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
99 result = -1;
100 goto out;
101 }
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000102
103 if (elf->state.elf32.scns.data[0].shdr.e32 != NULL)
104 {
105 num = elf->state.elf32.scns.data[0].shdr.e32->sh_link;
106 goto success;
107 }
108
109 offset = elf->state.elf32.ehdr->e_shoff;
110
111 if (elf->map_address != NULL
112 && elf->state.elf32.ehdr->e_ident[EI_DATA] == MY_ELFDATA
113 && (ALLOW_UNALIGNED
Jakub Jelinek720383c2014-01-17 19:36:16 +0100114 || (((size_t) ((char *) elf->map_address
115 + elf->start_offset + offset))
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000116 & (__alignof__ (Elf32_Shdr) - 1)) == 0))
Jakub Jelinek720383c2014-01-17 19:36:16 +0100117 {
118 /* First see whether the information in the ELF header is
119 valid and it does not ask for too much. */
120 if (unlikely (elf->maximum_size - offset
121 < sizeof (Elf32_Shdr)))
122 {
123 /* Something is wrong. */
124 __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
125 result = -1;
126 goto out;
127 }
128
129 /* We can directly access the memory. */
130 num = ((Elf32_Shdr *) (elf->map_address + elf->start_offset
131 + offset))->sh_link;
132 }
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000133 else
134 {
135 /* We avoid reading in all the section headers. Just read
136 the first one. */
137 Elf32_Shdr shdr_mem;
138
Ulrich Drepperfbe998a2005-08-29 16:27:10 +0000139 if (unlikely (pread_retry (elf->fildes, &shdr_mem,
140 sizeof (Elf32_Shdr), offset)
141 != sizeof (Elf32_Shdr)))
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000142 {
143 /* We must be able to read this ELF section header. */
144 __libelf_seterrno (ELF_E_INVALID_FILE);
145 result = -1;
146 goto out;
147 }
148
149 if (elf->state.elf32.ehdr->e_ident[EI_DATA] != MY_ELFDATA)
150 CONVERT (shdr_mem.sh_link);
151 num = shdr_mem.sh_link;
152 }
153 }
154 else
155 {
Mark Wielaardbabcdef2014-11-17 00:42:53 +0100156 if (unlikely (elf->state.elf64.scns.cnt == 0))
157 {
158 /* Cannot use SHN_XINDEX without section headers. */
159 __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
160 result = -1;
161 goto out;
162 }
163
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000164 if (elf->state.elf64.scns.data[0].shdr.e64 != NULL)
165 {
166 num = elf->state.elf64.scns.data[0].shdr.e64->sh_link;
167 goto success;
168 }
169
Ulrich Drepperfbe998a2005-08-29 16:27:10 +0000170 size_t offset = elf->state.elf64.ehdr->e_shoff;
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000171
172 if (elf->map_address != NULL
173 && elf->state.elf64.ehdr->e_ident[EI_DATA] == MY_ELFDATA
174 && (ALLOW_UNALIGNED
Jakub Jelinek720383c2014-01-17 19:36:16 +0100175 || (((size_t) ((char *) elf->map_address
176 + elf->start_offset + offset))
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000177 & (__alignof__ (Elf64_Shdr) - 1)) == 0))
Jakub Jelinek720383c2014-01-17 19:36:16 +0100178 {
179 /* First see whether the information in the ELF header is
180 valid and it does not ask for too much. */
181 if (unlikely (elf->maximum_size - offset
182 < sizeof (Elf64_Shdr)))
183 {
184 /* Something is wrong. */
185 __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
186 result = -1;
187 goto out;
188 }
189
190 /* We can directly access the memory. */
191 num = ((Elf64_Shdr *) (elf->map_address + elf->start_offset
192 + offset))->sh_link;
193 }
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000194 else
195 {
196 /* We avoid reading in all the section headers. Just read
197 the first one. */
198 Elf64_Shdr shdr_mem;
199
Ulrich Drepperfbe998a2005-08-29 16:27:10 +0000200 if (unlikely (pread_retry (elf->fildes, &shdr_mem,
201 sizeof (Elf64_Shdr), offset)
202 != sizeof (Elf64_Shdr)))
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000203 {
204 /* We must be able to read this ELF section header. */
205 __libelf_seterrno (ELF_E_INVALID_FILE);
206 result = -1;
207 goto out;
208 }
209
210 if (elf->state.elf64.ehdr->e_ident[EI_DATA] != MY_ELFDATA)
211 CONVERT (shdr_mem.sh_link);
212 num = shdr_mem.sh_link;
213 }
214 }
215 }
216
217 /* Store the result. */
218 success:
219 *dst = num;
220 }
221
222 out:
Roland McGrathb4d6f0f2008-08-25 22:55:17 +0000223 rwlock_unlock (elf->lock);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000224
225 return result;
226}
Ulrich Drepperf1894932009-06-13 15:55:42 -0700227INTDEF(elf_getshdrstrndx)
228/* Alias for the deprecated name. */
229strong_alias (elf_getshdrstrndx, elf_getshstrndx)