1 /* Return section header.
2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2005, 2007, 2009 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>. */
65 # define LIBELFBITS 32
69 static ElfW2(LIBELFBITS,Shdr) *
70 load_shdr_wrlock (Elf_Scn *scn)
72 ElfW2(LIBELFBITS,Shdr) *result;
74 /* Read the section header table. */
76 ElfW2(LIBELFBITS,Ehdr) *ehdr = elf->state.ELFW(elf,LIBELFBITS).ehdr;
78 /* Try again, maybe the data is there now. */
79 result = scn->shdr.ELFW(e,LIBELFBITS);
84 if (__elf_getshdrnum_rdlock (elf, &shnum) != 0)
86 size_t size = shnum * sizeof (ElfW2(LIBELFBITS,Shdr));
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)
94 __libelf_seterrno (ELF_E_NOMEM);
97 elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 1;
99 if (elf->map_address != NULL)
101 ElfW2(LIBELFBITS,Shdr) *notcvt;
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);
108 assert (ehdr->e_ident[EI_DATA] != MY_ELFDATA
109 || (! ALLOW_UNALIGNED
110 && ((uintptr_t) file_shdr
111 & (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1)) != 0));
113 /* Now copy the data and at the same time convert the byte order. */
114 if (ehdr->e_ident[EI_DATA] == MY_ELFDATA)
116 assert (! ALLOW_UNALIGNED);
117 memcpy (shdr, file_shdr, size);
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);
129 notcvt = (ElfW2(LIBELFBITS,Shdr) *) alloca (size);
130 memcpy (notcvt, ((char *) elf->map_address
131 + elf->start_offset + ehdr->e_shoff),
135 for (size_t cnt = 0; cnt < shnum; ++cnt)
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);
149 /* If this is a section with an extended index add a
150 reference in the section which uses the extended
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
157 /* Set the own shndx_index field in case it has not yet
159 if (elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shndx_index == 0)
160 elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shndx_index
165 else if (likely (elf->fildes != -1))
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))
173 /* Severe problems. We cannot read the data. */
174 __libelf_seterrno (ELF_E_READ_ERROR);
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)
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);
197 /* The file descriptor was already enabled and not all data was
198 read. Undo the allocation. */
199 __libelf_seterrno (ELF_E_FD_DISABLED);
203 elf->state.ELFW(elf,LIBELFBITS).shdr = NULL;
204 elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 0;
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];
214 result = scn->shdr.ELFW(e,LIBELFBITS);
215 assert (result != NULL);
222 scn_valid (Elf_Scn *scn)
227 if (unlikely (scn->elf->state.elf.ehdr == NULL))
229 __libelf_seterrno (ELF_E_WRONG_ORDER_EHDR);
233 if (unlikely (scn->elf->class != ELFW(ELFCLASS,LIBELFBITS)))
235 __libelf_seterrno (ELF_E_INVALID_CLASS);
242 ElfW2(LIBELFBITS,Shdr) *
243 __elfw2(LIBELFBITS,getshdr_rdlock) (scn)
246 ElfW2(LIBELFBITS,Shdr) *result;
248 if (!scn_valid (scn))
251 result = scn->shdr.ELFW(e,LIBELFBITS);
254 rwlock_unlock (scn->elf->lock);
255 rwlock_wrlock (scn->elf->lock);
256 result = scn->shdr.ELFW(e,LIBELFBITS);
258 result = load_shdr_wrlock (scn);
264 ElfW2(LIBELFBITS,Shdr) *
265 __elfw2(LIBELFBITS,getshdr_wrlock) (scn)
268 ElfW2(LIBELFBITS,Shdr) *result;
270 if (!scn_valid (scn))
273 result = scn->shdr.ELFW(e,LIBELFBITS);
275 result = load_shdr_wrlock (scn);
280 ElfW2(LIBELFBITS,Shdr) *
281 elfw2(LIBELFBITS,getshdr) (scn)
284 ElfW2(LIBELFBITS,Shdr) *result;
286 if (!scn_valid (scn))
289 rwlock_rdlock (scn->elf->lock);
290 result = __elfw2(LIBELFBITS,getshdr_rdlock) (scn);
291 rwlock_unlock (scn->elf->lock);