1
/* Return section header.
2
Copyright (C) 1998, 1999, 2000, 2001, 2002, 2005 Red Hat, Inc.
3
This file is part of Red Hat elfutils.
4
Written by Ulrich Drepper <drepper@redhat.com>, 1998.
6
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.
10
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.
15
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,
17
Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
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.
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>. */
64
# define LIBELFBITS 32
68
ElfW2(LIBELFBITS,Shdr) *
69
elfw2(LIBELFBITS,getshdr) (scn)
72
ElfW2(LIBELFBITS,Shdr) *result;
77
if (unlikely (scn->elf->state.elf.ehdr == NULL))
79
__libelf_seterrno (ELF_E_WRONG_ORDER_EHDR);
83
if (unlikely (scn->elf->class != ELFW(ELFCLASS,LIBELFBITS)))
85
__libelf_seterrno (ELF_E_INVALID_CLASS);
89
result = scn->shdr.ELFW(e,LIBELFBITS);
92
/* Read the section header table. */
94
ElfW2(LIBELFBITS,Ehdr) *ehdr = elf->state.ELFW(elf,LIBELFBITS).ehdr;
96
rwlock_wrlock (elf->lock);
98
/* Try again, maybe the data is there now. */
99
result = scn->shdr.ELFW(e,LIBELFBITS);
104
if (INTUSE (elf_getshnum) (elf, &shnum) != 0)
106
size_t size = shnum * sizeof (ElfW2(LIBELFBITS,Shdr));
108
/* Allocate memory for the program headers. We know the number
109
of entries from the ELF header. */
110
ElfW2(LIBELFBITS,Shdr) *shdr = elf->state.ELFW(elf,LIBELFBITS).shdr =
111
(ElfW2(LIBELFBITS,Shdr) *) malloc (size);
112
if (elf->state.ELFW(elf,LIBELFBITS).shdr == NULL)
114
__libelf_seterrno (ELF_E_NOMEM);
117
elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 1;
119
if (elf->map_address != NULL)
121
ElfW2(LIBELFBITS,Shdr) *notcvt;
123
/* All the data is already mapped. If we could use it
124
directly this would already have happened. */
125
assert (ehdr->e_ident[EI_DATA] != MY_ELFDATA
126
|| (! ALLOW_UNALIGNED
127
&& (((uintptr_t) elf->map_address + elf->start_offset
129
& (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1)) != 0));
131
/* Now copy the data and at the same time convert the byte
134
|| (((uintptr_t) elf->map_address + elf->start_offset
136
& (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1)) == 0)
137
notcvt = (ElfW2(LIBELFBITS,Shdr) *)
138
((char *) elf->map_address
139
+ elf->start_offset + ehdr->e_shoff);
142
notcvt = (ElfW2(LIBELFBITS,Shdr) *) alloca (size);
143
memcpy (notcvt, ((char *) elf->map_address
144
+ elf->start_offset + ehdr->e_shoff),
148
for (size_t cnt = 0; cnt < shnum; ++cnt)
150
CONVERT_TO (shdr[cnt].sh_name, notcvt[cnt].sh_name);
151
CONVERT_TO (shdr[cnt].sh_type, notcvt[cnt].sh_type);
152
CONVERT_TO (shdr[cnt].sh_flags, notcvt[cnt].sh_flags);
153
CONVERT_TO (shdr[cnt].sh_addr, notcvt[cnt].sh_addr);
154
CONVERT_TO (shdr[cnt].sh_offset, notcvt[cnt].sh_offset);
155
CONVERT_TO (shdr[cnt].sh_size, notcvt[cnt].sh_size);
156
CONVERT_TO (shdr[cnt].sh_link, notcvt[cnt].sh_link);
157
CONVERT_TO (shdr[cnt].sh_info, notcvt[cnt].sh_info);
158
CONVERT_TO (shdr[cnt].sh_addralign, notcvt[cnt].sh_addralign);
159
CONVERT_TO (shdr[cnt].sh_entsize, notcvt[cnt].sh_entsize);
162
else if (likely (elf->fildes != -1))
164
/* Read the header. */
165
ssize_t n = pread_retry (elf->fildes,
166
elf->state.ELFW(elf,LIBELFBITS).shdr, size,
167
elf->start_offset + ehdr->e_shoff);
168
if (unlikely ((size_t) n != size))
170
/* Severe problems. We cannot read the data. */
171
__libelf_seterrno (ELF_E_READ_ERROR);
175
/* If the byte order of the file is not the same as the one
176
of the host convert the data now. */
177
if (ehdr->e_ident[EI_DATA] != MY_ELFDATA)
178
for (size_t cnt = 0; cnt < shnum; ++cnt)
180
CONVERT (shdr[cnt].sh_name);
181
CONVERT (shdr[cnt].sh_type);
182
CONVERT (shdr[cnt].sh_flags);
183
CONVERT (shdr[cnt].sh_addr);
184
CONVERT (shdr[cnt].sh_offset);
185
CONVERT (shdr[cnt].sh_size);
186
CONVERT (shdr[cnt].sh_link);
187
CONVERT (shdr[cnt].sh_info);
188
CONVERT (shdr[cnt].sh_addralign);
189
CONVERT (shdr[cnt].sh_entsize);
194
/* The file descriptor was already enabled and not all data was
195
read. Undo the allocation. */
196
__libelf_seterrno (ELF_E_FD_DISABLED);
200
elf->state.ELFW(elf,LIBELFBITS).shdr = NULL;
201
elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 0;
206
/* Set the pointers in the `scn's. */
207
for (size_t cnt = 0; cnt < shnum; ++cnt)
208
elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shdr.ELFW(e,LIBELFBITS)
209
= &elf->state.ELFW(elf,LIBELFBITS).shdr[cnt];
211
result = scn->shdr.ELFW(e,LIBELFBITS);
212
assert (result != NULL);
215
rwlock_unlock (elf->lock);
220
INTDEF(elfw2(LIBELFBITS,getshdr))