blob: 80f54fd7bc11e60158ab2271eea67611c8287f15 [file] [log] [blame]
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001/* Get ELF program header table.
Roland McGrath6fd3cd12010-01-07 19:41:04 -08002 Copyright (C) 1998-2010 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
Ulrich Drepperfbe998a2005-08-29 16:27:10 +000055#include <errno.h>
Roland McGrathb4d6f0f2008-08-25 22:55:17 +000056#include <stdbool.h>
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000057#include <stdlib.h>
58#include <unistd.h>
Roland McGrathc373d852006-10-10 00:25:21 +000059#include <assert.h>
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000060
Ulrich Drepperfbe998a2005-08-29 16:27:10 +000061#include <system.h>
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000062#include "libelfP.h"
63#include "common.h"
64
65#ifndef LIBELFBITS
66# define LIBELFBITS 32
67#endif
68
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000069ElfW2(LIBELFBITS,Phdr) *
Roland McGrathb4d6f0f2008-08-25 22:55:17 +000070__elfw2(LIBELFBITS,getphdr_wrlock) (elf)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000071 Elf *elf;
72{
73 ElfW2(LIBELFBITS,Phdr) *result;
74
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000075 /* If the program header entry has already been filled in the code
76 below must already have been run. So the class is set, too. No
77 need to waste any more time here. */
78 result = elf->state.ELFW(elf,LIBELFBITS).phdr;
79 if (likely (result != NULL))
80 return result;
81
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000082 if (elf->class == 0)
83 elf->class = ELFW(ELFCLASS,LIBELFBITS);
84 else if (elf->class != ELFW(ELFCLASS,LIBELFBITS))
85 {
86 __libelf_seterrno (ELF_E_INVALID_CLASS);
87 result = NULL;
88 goto out;
89 }
90
91 if (likely (result == NULL))
92 {
93 /* Read the section header table. */
94 ElfW2(LIBELFBITS,Ehdr) *ehdr = elf->state.ELFW(elf,LIBELFBITS).ehdr;
95
96 /* If no program header exists return NULL. */
Roland McGrath6fd3cd12010-01-07 19:41:04 -080097 size_t phnum;
98 if (__elf_getphdrnum_rdlock (elf, &phnum) != 0)
99 goto out;
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000100 if (phnum == 0)
101 {
102 __libelf_seterrno (ELF_E_NO_PHDR);
103 goto out;
104 }
105
106 size_t size = phnum * sizeof (ElfW2(LIBELFBITS,Phdr));
107
Roland McGrath9d81c7d2010-04-14 11:37:06 -0700108 if (ehdr->e_phoff > elf->maximum_size
Roland McGrath957072c2010-04-14 11:24:15 -0700109 || elf->maximum_size - ehdr->e_phoff < size)
110 {
111 __libelf_seterrno (ELF_E_INVALID_DATA);
112 goto out;
113 }
114
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000115 if (elf->map_address != NULL)
116 {
117 /* All the data is already mapped. Use it. */
Roland McGrathc373d852006-10-10 00:25:21 +0000118 void *file_phdr = ((char *) elf->map_address
119 + elf->start_offset + ehdr->e_phoff);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000120 if (ehdr->e_ident[EI_DATA] == MY_ELFDATA
121 && (ALLOW_UNALIGNED
Roland McGrathc373d852006-10-10 00:25:21 +0000122 || ((uintptr_t) file_phdr
123 & (__alignof__ (ElfW2(LIBELFBITS,Phdr)) - 1)) == 0))
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000124 /* Simply use the mapped data. */
Roland McGrathc373d852006-10-10 00:25:21 +0000125 elf->state.ELFW(elf,LIBELFBITS).phdr = file_phdr;
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000126 else
127 {
128 ElfW2(LIBELFBITS,Phdr) *notcvt;
129 ElfW2(LIBELFBITS,Phdr) *phdr;
130
131 /* Allocate memory for the program headers. We know the number
132 of entries from the ELF header. */
133 phdr = elf->state.ELFW(elf,LIBELFBITS).phdr =
134 (ElfW2(LIBELFBITS,Phdr) *) malloc (size);
135 if (elf->state.ELFW(elf,LIBELFBITS).phdr == NULL)
136 {
137 __libelf_seterrno (ELF_E_NOMEM);
138 goto out;
139 }
140 elf->state.ELFW(elf,LIBELFBITS).phdr_flags |=
141 ELF_F_MALLOCED | ELF_F_DIRTY;
142
143 /* Now copy the data and at the same time convert the
144 byte order. */
Roland McGrathc373d852006-10-10 00:25:21 +0000145
146 if (ehdr->e_ident[EI_DATA] == MY_ELFDATA)
147 {
148 assert (! ALLOW_UNALIGNED);
149 memcpy (phdr, file_phdr, size);
150 }
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000151 else
152 {
Roland McGrathc373d852006-10-10 00:25:21 +0000153 if (ALLOW_UNALIGNED
154 || ((uintptr_t) file_phdr
155 & (__alignof__ (ElfW2(LIBELFBITS,Phdr)) - 1)) == 0)
156 notcvt = file_phdr;
157 else
158 {
159 notcvt = (ElfW2(LIBELFBITS,Phdr) *) alloca (size);
160 memcpy (notcvt, file_phdr, size);
161 }
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000162
Roland McGrathc373d852006-10-10 00:25:21 +0000163 for (size_t cnt = 0; cnt < phnum; ++cnt)
164 {
165 CONVERT_TO (phdr[cnt].p_type, notcvt[cnt].p_type);
166 CONVERT_TO (phdr[cnt].p_offset, notcvt[cnt].p_offset);
167 CONVERT_TO (phdr[cnt].p_vaddr, notcvt[cnt].p_vaddr);
168 CONVERT_TO (phdr[cnt].p_paddr, notcvt[cnt].p_paddr);
169 CONVERT_TO (phdr[cnt].p_filesz, notcvt[cnt].p_filesz);
170 CONVERT_TO (phdr[cnt].p_memsz, notcvt[cnt].p_memsz);
171 CONVERT_TO (phdr[cnt].p_flags, notcvt[cnt].p_flags);
172 CONVERT_TO (phdr[cnt].p_align, notcvt[cnt].p_align);
173 }
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000174 }
175 }
176 }
177 else if (likely (elf->fildes != -1))
178 {
179 /* Allocate memory for the program headers. We know the number
180 of entries from the ELF header. */
181 elf->state.ELFW(elf,LIBELFBITS).phdr =
182 (ElfW2(LIBELFBITS,Phdr) *) malloc (size);
183 if (elf->state.ELFW(elf,LIBELFBITS).phdr == NULL)
184 {
185 __libelf_seterrno (ELF_E_NOMEM);
186 goto out;
187 }
188 elf->state.ELFW(elf,LIBELFBITS).phdr_flags |= ELF_F_MALLOCED;
189
190 /* Read the header. */
Ulrich Drepperfbe998a2005-08-29 16:27:10 +0000191 ssize_t n = pread_retry (elf->fildes,
192 elf->state.ELFW(elf,LIBELFBITS).phdr, size,
193 elf->start_offset + ehdr->e_phoff);
194 if (unlikely ((size_t) n != size))
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000195 {
196 /* Severe problems. We cannot read the data. */
197 __libelf_seterrno (ELF_E_READ_ERROR);
198 free (elf->state.ELFW(elf,LIBELFBITS).phdr);
199 elf->state.ELFW(elf,LIBELFBITS).phdr = NULL;
200 goto out;
201 }
202
203 /* If the byte order of the file is not the same as the one
204 of the host convert the data now. */
205 if (ehdr->e_ident[EI_DATA] != MY_ELFDATA)
206 {
207 ElfW2(LIBELFBITS,Phdr) *phdr
208 = elf->state.ELFW(elf,LIBELFBITS).phdr;
209
210 for (size_t cnt = 0; cnt < phnum; ++cnt)
211 {
212 CONVERT (phdr[cnt].p_type);
213 CONVERT (phdr[cnt].p_offset);
214 CONVERT (phdr[cnt].p_vaddr);
215 CONVERT (phdr[cnt].p_paddr);
216 CONVERT (phdr[cnt].p_filesz);
217 CONVERT (phdr[cnt].p_memsz);
218 CONVERT (phdr[cnt].p_flags);
219 CONVERT (phdr[cnt].p_align);
220 }
221 }
222 }
223 else
224 {
225 /* The file descriptor was already enabled and not all data was
226 read. */
227 __libelf_seterrno (ELF_E_FD_DISABLED);
228 goto out;
229 }
230
231 result = elf->state.ELFW(elf,LIBELFBITS).phdr;
232 }
233
234 out:
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000235 return result;
236}
Ulrich Drepperd56e2322008-08-16 03:09:13 +0000237
238ElfW2(LIBELFBITS,Phdr) *
239elfw2(LIBELFBITS,getphdr) (elf)
240 Elf *elf;
241{
Roland McGrathb4d6f0f2008-08-25 22:55:17 +0000242 ElfW2(LIBELFBITS,Phdr) *result;
Ulrich Drepperd56e2322008-08-16 03:09:13 +0000243
Roland McGrath122f3882008-08-26 07:20:29 +0000244 if (elf == NULL)
245 return NULL;
246
247 if (unlikely (elf->kind != ELF_K_ELF))
248 {
249 __libelf_seterrno (ELF_E_INVALID_HANDLE);
250 return NULL;
251 }
252
253 /* If the program header entry has already been filled in the code
254 * in getphdr_wrlock must already have been run. So the class is
255 * set, too. No need to waste any more time here. */
256 result = elf->state.ELFW(elf,LIBELFBITS).phdr;
257 if (likely (result != NULL))
258 return result;
259
Roland McGrathb4d6f0f2008-08-25 22:55:17 +0000260 rwlock_wrlock (elf->lock);
261 result = __elfw2(LIBELFBITS,getphdr_wrlock) (elf);
262 rwlock_unlock (elf->lock);
263
264 return result;
Ulrich Drepperd56e2322008-08-16 03:09:13 +0000265}
Roland McGrathb4d6f0f2008-08-25 22:55:17 +0000266INTDEF(elfw2(LIBELFBITS,getphdr))