Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1 | /* Get ELF program header table. |
Roland McGrath | 6fd3cd1 | 2010-01-07 19:41:04 -0800 | [diff] [blame] | 2 | Copyright (C) 1998-2010 Red Hat, Inc. |
Ulrich Drepper | 361df7d | 2006-04-04 21:38:57 +0000 | [diff] [blame] | 3 | This file is part of Red Hat elfutils. |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 4 | Written by Ulrich Drepper <drepper@redhat.com>, 1998. |
| 5 | |
Ulrich Drepper | 361df7d | 2006-04-04 21:38:57 +0000 | [diff] [blame] | 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. |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 9 | |
Ulrich Drepper | 361df7d | 2006-04-04 21:38:57 +0000 | [diff] [blame] | 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. |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 14 | |
Ulrich Drepper | 361df7d | 2006-04-04 21:38:57 +0000 | [diff] [blame] | 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, |
Ulrich Drepper | 1e9ef50 | 2006-04-04 22:29:06 +0000 | [diff] [blame] | 17 | Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA. |
Ulrich Drepper | 361df7d | 2006-04-04 21:38:57 +0000 | [diff] [blame] | 18 | |
| 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 Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 50 | |
| 51 | #ifdef HAVE_CONFIG_H |
| 52 | # include <config.h> |
| 53 | #endif |
| 54 | |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 55 | #include <errno.h> |
Roland McGrath | b4d6f0f | 2008-08-25 22:55:17 +0000 | [diff] [blame] | 56 | #include <stdbool.h> |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 57 | #include <stdlib.h> |
| 58 | #include <unistd.h> |
Roland McGrath | c373d85 | 2006-10-10 00:25:21 +0000 | [diff] [blame] | 59 | #include <assert.h> |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 60 | |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 61 | #include <system.h> |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 62 | #include "libelfP.h" |
| 63 | #include "common.h" |
| 64 | |
| 65 | #ifndef LIBELFBITS |
| 66 | # define LIBELFBITS 32 |
| 67 | #endif |
| 68 | |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 69 | ElfW2(LIBELFBITS,Phdr) * |
Roland McGrath | b4d6f0f | 2008-08-25 22:55:17 +0000 | [diff] [blame] | 70 | __elfw2(LIBELFBITS,getphdr_wrlock) (elf) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 71 | Elf *elf; |
| 72 | { |
| 73 | ElfW2(LIBELFBITS,Phdr) *result; |
| 74 | |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 75 | /* 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 Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 82 | 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 McGrath | 6fd3cd1 | 2010-01-07 19:41:04 -0800 | [diff] [blame] | 97 | size_t phnum; |
| 98 | if (__elf_getphdrnum_rdlock (elf, &phnum) != 0) |
| 99 | goto out; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 100 | 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 McGrath | 9d81c7d | 2010-04-14 11:37:06 -0700 | [diff] [blame] | 108 | if (ehdr->e_phoff > elf->maximum_size |
Roland McGrath | 957072c | 2010-04-14 11:24:15 -0700 | [diff] [blame] | 109 | || elf->maximum_size - ehdr->e_phoff < size) |
| 110 | { |
| 111 | __libelf_seterrno (ELF_E_INVALID_DATA); |
| 112 | goto out; |
| 113 | } |
| 114 | |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 115 | if (elf->map_address != NULL) |
| 116 | { |
| 117 | /* All the data is already mapped. Use it. */ |
Roland McGrath | c373d85 | 2006-10-10 00:25:21 +0000 | [diff] [blame] | 118 | void *file_phdr = ((char *) elf->map_address |
| 119 | + elf->start_offset + ehdr->e_phoff); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 120 | if (ehdr->e_ident[EI_DATA] == MY_ELFDATA |
| 121 | && (ALLOW_UNALIGNED |
Roland McGrath | c373d85 | 2006-10-10 00:25:21 +0000 | [diff] [blame] | 122 | || ((uintptr_t) file_phdr |
| 123 | & (__alignof__ (ElfW2(LIBELFBITS,Phdr)) - 1)) == 0)) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 124 | /* Simply use the mapped data. */ |
Roland McGrath | c373d85 | 2006-10-10 00:25:21 +0000 | [diff] [blame] | 125 | elf->state.ELFW(elf,LIBELFBITS).phdr = file_phdr; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 126 | 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 McGrath | c373d85 | 2006-10-10 00:25:21 +0000 | [diff] [blame] | 145 | |
| 146 | if (ehdr->e_ident[EI_DATA] == MY_ELFDATA) |
| 147 | { |
| 148 | assert (! ALLOW_UNALIGNED); |
| 149 | memcpy (phdr, file_phdr, size); |
| 150 | } |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 151 | else |
| 152 | { |
Roland McGrath | c373d85 | 2006-10-10 00:25:21 +0000 | [diff] [blame] | 153 | 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 Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 162 | |
Roland McGrath | c373d85 | 2006-10-10 00:25:21 +0000 | [diff] [blame] | 163 | 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 Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 174 | } |
| 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 Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 191 | 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 Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 195 | { |
| 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 Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 235 | return result; |
| 236 | } |
Ulrich Drepper | d56e232 | 2008-08-16 03:09:13 +0000 | [diff] [blame] | 237 | |
| 238 | ElfW2(LIBELFBITS,Phdr) * |
| 239 | elfw2(LIBELFBITS,getphdr) (elf) |
| 240 | Elf *elf; |
| 241 | { |
Roland McGrath | b4d6f0f | 2008-08-25 22:55:17 +0000 | [diff] [blame] | 242 | ElfW2(LIBELFBITS,Phdr) *result; |
Ulrich Drepper | d56e232 | 2008-08-16 03:09:13 +0000 | [diff] [blame] | 243 | |
Roland McGrath | 122f388 | 2008-08-26 07:20:29 +0000 | [diff] [blame] | 244 | 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 McGrath | b4d6f0f | 2008-08-25 22:55:17 +0000 | [diff] [blame] | 260 | rwlock_wrlock (elf->lock); |
| 261 | result = __elfw2(LIBELFBITS,getphdr_wrlock) (elf); |
| 262 | rwlock_unlock (elf->lock); |
| 263 | |
| 264 | return result; |
Ulrich Drepper | d56e232 | 2008-08-16 03:09:13 +0000 | [diff] [blame] | 265 | } |
Roland McGrath | b4d6f0f | 2008-08-25 22:55:17 +0000 | [diff] [blame] | 266 | INTDEF(elfw2(LIBELFBITS,getphdr)) |