blob: 8d650d9f29bd12b3ad75dc65b68d56aa68a87371 [file] [log] [blame]
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001/* Return section header.
Ulrich Drepperf1894932009-06-13 15:55:42 -07002 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2005, 2007, 2009 Red Hat, Inc.
Ulrich Drepper361df7d2006-04-04 21:38:57 +00003 This file is part of Red Hat elfutils.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00004 Written by Ulrich Drepper <drepper@redhat.com>, 1998.
5
Ulrich Drepper361df7d2006-04-04 21:38:57 +00006 Red Hat elfutils is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by the
8 Free Software Foundation; version 2 of the License.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00009
Ulrich Drepper361df7d2006-04-04 21:38:57 +000010 Red Hat elfutils is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000014
Ulrich Drepper361df7d2006-04-04 21:38:57 +000015 You should have received a copy of the GNU General Public License along
16 with Red Hat elfutils; if not, write to the Free Software Foundation,
Ulrich Drepper1e9ef502006-04-04 22:29:06 +000017 Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
Ulrich Drepper361df7d2006-04-04 21:38:57 +000018
19 In addition, as a special exception, Red Hat, Inc. gives You the
20 additional right to link the code of Red Hat elfutils with code licensed
21 under any Open Source Initiative certified open source license
22 (http://www.opensource.org/licenses/index.php) which requires the
23 distribution of source code with any binary distribution and to
24 distribute linked combinations of the two. Non-GPL Code permitted under
25 this exception must only link to the code of Red Hat elfutils through
26 those well defined interfaces identified in the file named EXCEPTION
27 found in the source code files (the "Approved Interfaces"). The files
28 of Non-GPL Code may instantiate templates or use macros or inline
29 functions from the Approved Interfaces without causing the resulting
30 work to be covered by the GNU General Public License. Only Red Hat,
31 Inc. may make changes or additions to the list of Approved Interfaces.
32 Red Hat's grant of this exception is conditioned upon your not adding
33 any new exceptions. If you wish to add a new Approved Interface or
34 exception, please contact Red Hat. You must obey the GNU General Public
35 License in all respects for all of the Red Hat elfutils code and other
36 code used in conjunction with Red Hat elfutils except the Non-GPL Code
37 covered by this exception. If you modify this file, you may extend this
38 exception to your version of the file, but you are not obligated to do
39 so. If you do not wish to provide this exception without modification,
40 you must delete this exception statement from your version and license
41 this file solely under the GPL without exception.
42
43 Red Hat elfutils is an included package of the Open Invention Network.
44 An included package of the Open Invention Network is a package for which
45 Open Invention Network licensees cross-license their patents. No patent
46 license is granted, either expressly or impliedly, by designation as an
47 included package. Should you wish to participate in the Open Invention
48 Network licensing program, please visit www.openinventionnetwork.com
49 <http://www.openinventionnetwork.com>. */
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000050
51#ifdef HAVE_CONFIG_H
52# include <config.h>
53#endif
54
55#include <assert.h>
Ulrich Drepperfbe998a2005-08-29 16:27:10 +000056#include <errno.h>
Roland McGrathb4d6f0f2008-08-25 22:55:17 +000057#include <stdbool.h>
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000058#include <unistd.h>
59
Ulrich Drepperfbe998a2005-08-29 16:27:10 +000060#include <system.h>
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000061#include "libelfP.h"
62#include "common.h"
63
64#ifndef LIBELFBITS
65# define LIBELFBITS 32
66#endif
67
68
Roland McGrathb4d6f0f2008-08-25 22:55:17 +000069static ElfW2(LIBELFBITS,Shdr) *
Ulrich Drepper02f66452008-12-04 05:58:16 +000070load_shdr_wrlock (Elf_Scn *scn)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000071{
72 ElfW2(LIBELFBITS,Shdr) *result;
73
Roland McGrathb4d6f0f2008-08-25 22:55:17 +000074 /* Read the section header table. */
75 Elf *elf = scn->elf;
76 ElfW2(LIBELFBITS,Ehdr) *ehdr = elf->state.ELFW(elf,LIBELFBITS).ehdr;
77
78 /* Try again, maybe the data is there now. */
79 result = scn->shdr.ELFW(e,LIBELFBITS);
80 if (result != NULL)
81 goto out;
82
83 size_t shnum;
Ulrich Drepperf1894932009-06-13 15:55:42 -070084 if (__elf_getshdrnum_rdlock (elf, &shnum) != 0)
Roland McGrathb4d6f0f2008-08-25 22:55:17 +000085 goto out;
86 size_t size = shnum * sizeof (ElfW2(LIBELFBITS,Shdr));
87
88 /* Allocate memory for the section headers. We know the number
89 of entries from the ELF header. */
90 ElfW2(LIBELFBITS,Shdr) *shdr = elf->state.ELFW(elf,LIBELFBITS).shdr =
91 (ElfW2(LIBELFBITS,Shdr) *) malloc (size);
92 if (elf->state.ELFW(elf,LIBELFBITS).shdr == NULL)
93 {
94 __libelf_seterrno (ELF_E_NOMEM);
95 goto out;
96 }
97 elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 1;
98
99 if (elf->map_address != NULL)
100 {
101 ElfW2(LIBELFBITS,Shdr) *notcvt;
102
103 /* All the data is already mapped. If we could use it
104 directly this would already have happened. */
105 void *file_shdr = ((char *) elf->map_address
106 + elf->start_offset + ehdr->e_shoff);
107
108 assert (ehdr->e_ident[EI_DATA] != MY_ELFDATA
109 || (! ALLOW_UNALIGNED
110 && ((uintptr_t) file_shdr
111 & (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1)) != 0));
112
113 /* Now copy the data and at the same time convert the byte order. */
114 if (ehdr->e_ident[EI_DATA] == MY_ELFDATA)
115 {
116 assert (! ALLOW_UNALIGNED);
117 memcpy (shdr, file_shdr, size);
118 }
119 else
120 {
121 if (ALLOW_UNALIGNED
122 || ((uintptr_t) file_shdr
123 & (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1)) == 0)
124 notcvt = (ElfW2(LIBELFBITS,Shdr) *)
125 ((char *) elf->map_address
126 + elf->start_offset + ehdr->e_shoff);
127 else
128 {
129 notcvt = (ElfW2(LIBELFBITS,Shdr) *) alloca (size);
130 memcpy (notcvt, ((char *) elf->map_address
131 + elf->start_offset + ehdr->e_shoff),
132 size);
133 }
134
135 for (size_t cnt = 0; cnt < shnum; ++cnt)
136 {
137 CONVERT_TO (shdr[cnt].sh_name, notcvt[cnt].sh_name);
138 CONVERT_TO (shdr[cnt].sh_type, notcvt[cnt].sh_type);
139 CONVERT_TO (shdr[cnt].sh_flags, notcvt[cnt].sh_flags);
140 CONVERT_TO (shdr[cnt].sh_addr, notcvt[cnt].sh_addr);
141 CONVERT_TO (shdr[cnt].sh_offset, notcvt[cnt].sh_offset);
142 CONVERT_TO (shdr[cnt].sh_size, notcvt[cnt].sh_size);
143 CONVERT_TO (shdr[cnt].sh_link, notcvt[cnt].sh_link);
144 CONVERT_TO (shdr[cnt].sh_info, notcvt[cnt].sh_info);
145 CONVERT_TO (shdr[cnt].sh_addralign,
146 notcvt[cnt].sh_addralign);
147 CONVERT_TO (shdr[cnt].sh_entsize, notcvt[cnt].sh_entsize);
148
149 /* If this is a section with an extended index add a
150 reference in the section which uses the extended
151 index. */
152 if (shdr[cnt].sh_type == SHT_SYMTAB_SHNDX
153 && shdr[cnt].sh_link < shnum)
154 elf->state.ELFW(elf,LIBELFBITS).scns.data[shdr[cnt].sh_link].shndx_index
155 = cnt;
156
157 /* Set the own shndx_index field in case it has not yet
158 been set. */
159 if (elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shndx_index == 0)
160 elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shndx_index
161 = -1;
162 }
163 }
164 }
165 else if (likely (elf->fildes != -1))
166 {
167 /* Read the header. */
168 ssize_t n = pread_retry (elf->fildes,
169 elf->state.ELFW(elf,LIBELFBITS).shdr, size,
170 elf->start_offset + ehdr->e_shoff);
171 if (unlikely ((size_t) n != size))
172 {
173 /* Severe problems. We cannot read the data. */
174 __libelf_seterrno (ELF_E_READ_ERROR);
175 goto free_and_out;
176 }
177
178 /* If the byte order of the file is not the same as the one
179 of the host convert the data now. */
180 if (ehdr->e_ident[EI_DATA] != MY_ELFDATA)
181 for (size_t cnt = 0; cnt < shnum; ++cnt)
182 {
183 CONVERT (shdr[cnt].sh_name);
184 CONVERT (shdr[cnt].sh_type);
185 CONVERT (shdr[cnt].sh_flags);
186 CONVERT (shdr[cnt].sh_addr);
187 CONVERT (shdr[cnt].sh_offset);
188 CONVERT (shdr[cnt].sh_size);
189 CONVERT (shdr[cnt].sh_link);
190 CONVERT (shdr[cnt].sh_info);
191 CONVERT (shdr[cnt].sh_addralign);
192 CONVERT (shdr[cnt].sh_entsize);
193 }
194 }
195 else
196 {
197 /* The file descriptor was already enabled and not all data was
198 read. Undo the allocation. */
199 __libelf_seterrno (ELF_E_FD_DISABLED);
200
201 free_and_out:
202 free (shdr);
203 elf->state.ELFW(elf,LIBELFBITS).shdr = NULL;
204 elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 0;
205
206 goto out;
207 }
208
209 /* Set the pointers in the `scn's. */
210 for (size_t cnt = 0; cnt < shnum; ++cnt)
211 elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shdr.ELFW(e,LIBELFBITS)
212 = &elf->state.ELFW(elf,LIBELFBITS).shdr[cnt];
213
214 result = scn->shdr.ELFW(e,LIBELFBITS);
215 assert (result != NULL);
216
217out:
218 return result;
219}
220
221static bool
222scn_valid (Elf_Scn *scn)
223{
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000224 if (scn == NULL)
Roland McGrathb4d6f0f2008-08-25 22:55:17 +0000225 return false;
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000226
227 if (unlikely (scn->elf->state.elf.ehdr == NULL))
228 {
229 __libelf_seterrno (ELF_E_WRONG_ORDER_EHDR);
Roland McGrathb4d6f0f2008-08-25 22:55:17 +0000230 return false;
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000231 }
232
233 if (unlikely (scn->elf->class != ELFW(ELFCLASS,LIBELFBITS)))
234 {
235 __libelf_seterrno (ELF_E_INVALID_CLASS);
Roland McGrathb4d6f0f2008-08-25 22:55:17 +0000236 return false;
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000237 }
238
Roland McGrathb4d6f0f2008-08-25 22:55:17 +0000239 return true;
240}
241
242ElfW2(LIBELFBITS,Shdr) *
243__elfw2(LIBELFBITS,getshdr_rdlock) (scn)
244 Elf_Scn *scn;
245{
Roland McGrathb4d6f0f2008-08-25 22:55:17 +0000246 ElfW2(LIBELFBITS,Shdr) *result;
247
248 if (!scn_valid (scn))
249 return NULL;
250
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000251 result = scn->shdr.ELFW(e,LIBELFBITS);
252 if (result == NULL)
253 {
Roland McGrathb4d6f0f2008-08-25 22:55:17 +0000254 rwlock_unlock (scn->elf->lock);
255 rwlock_wrlock (scn->elf->lock);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000256 result = scn->shdr.ELFW(e,LIBELFBITS);
Roland McGrathb4d6f0f2008-08-25 22:55:17 +0000257 if (result == NULL)
Ulrich Drepper02f66452008-12-04 05:58:16 +0000258 result = load_shdr_wrlock (scn);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000259 }
260
261 return result;
262}
Ulrich Drepperd56e2322008-08-16 03:09:13 +0000263
264ElfW2(LIBELFBITS,Shdr) *
Roland McGrathb4d6f0f2008-08-25 22:55:17 +0000265__elfw2(LIBELFBITS,getshdr_wrlock) (scn)
266 Elf_Scn *scn;
267{
268 ElfW2(LIBELFBITS,Shdr) *result;
269
270 if (!scn_valid (scn))
271 return NULL;
272
273 result = scn->shdr.ELFW(e,LIBELFBITS);
274 if (result == NULL)
Ulrich Drepper02f66452008-12-04 05:58:16 +0000275 result = load_shdr_wrlock (scn);
Roland McGrathb4d6f0f2008-08-25 22:55:17 +0000276
277 return result;
278}
279
280ElfW2(LIBELFBITS,Shdr) *
Ulrich Drepperd56e2322008-08-16 03:09:13 +0000281elfw2(LIBELFBITS,getshdr) (scn)
282 Elf_Scn *scn;
283{
Roland McGrathb4d6f0f2008-08-25 22:55:17 +0000284 ElfW2(LIBELFBITS,Shdr) *result;
285
286 if (!scn_valid (scn))
287 return NULL;
288
289 rwlock_rdlock (scn->elf->lock);
290 result = __elfw2(LIBELFBITS,getshdr_rdlock) (scn);
291 rwlock_unlock (scn->elf->lock);
292
293 return result;
Ulrich Drepperd56e2322008-08-16 03:09:13 +0000294}