/src/elfutils/libelf/elf32_getphdr.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Get ELF program header table. |
2 | | Copyright (C) 1998-2010, 2014, 2015 Red Hat, Inc. |
3 | | This file is part of elfutils. |
4 | | Written by Ulrich Drepper <drepper@redhat.com>, 1998. |
5 | | |
6 | | This file is free software; you can redistribute it and/or modify |
7 | | it under the terms of either |
8 | | |
9 | | * the GNU Lesser General Public License as published by the Free |
10 | | Software Foundation; either version 3 of the License, or (at |
11 | | your option) any later version |
12 | | |
13 | | or |
14 | | |
15 | | * the GNU General Public License as published by the Free |
16 | | Software Foundation; either version 2 of the License, or (at |
17 | | your option) any later version |
18 | | |
19 | | or both in parallel, as here. |
20 | | |
21 | | elfutils is distributed in the hope that it will be useful, but |
22 | | WITHOUT ANY WARRANTY; without even the implied warranty of |
23 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
24 | | General Public License for more details. |
25 | | |
26 | | You should have received copies of the GNU General Public License and |
27 | | the GNU Lesser General Public License along with this program. If |
28 | | not, see <http://www.gnu.org/licenses/>. */ |
29 | | |
30 | | #ifdef HAVE_CONFIG_H |
31 | | # include <config.h> |
32 | | #endif |
33 | | |
34 | | #include <errno.h> |
35 | | #include <stdbool.h> |
36 | | #include <stdlib.h> |
37 | | #include <assert.h> |
38 | | |
39 | | #include "libelfP.h" |
40 | | #include "common.h" |
41 | | |
42 | | #ifndef LIBELFBITS |
43 | | # define LIBELFBITS 32 |
44 | | #endif |
45 | | |
46 | | ElfW2(LIBELFBITS,Phdr) * |
47 | | __elfw2(LIBELFBITS,getphdr_wrlock) (Elf *elf) |
48 | 4 | { |
49 | 4 | ElfW2(LIBELFBITS,Phdr) *result; |
50 | | |
51 | | /* If the program header entry has already been filled in the code |
52 | | below must already have been run. So the class is set, too. No |
53 | | need to waste any more time here. */ |
54 | 4 | result = elf->state.ELFW(elf,LIBELFBITS).phdr; |
55 | 4 | if (likely (result != NULL)) |
56 | 0 | return result; |
57 | | |
58 | 4 | if (elf->class == 0) |
59 | 0 | elf->class = ELFW(ELFCLASS,LIBELFBITS); |
60 | 4 | else if (elf->class != ELFW(ELFCLASS,LIBELFBITS)) |
61 | 0 | { |
62 | 0 | __libelf_seterrno (ELF_E_INVALID_CLASS); |
63 | 0 | result = NULL; |
64 | 0 | goto out; |
65 | 0 | } |
66 | | |
67 | 4 | if (likely (result == NULL)) |
68 | 4 | { |
69 | | /* Read the section header table. */ |
70 | 4 | ElfW2(LIBELFBITS,Ehdr) *ehdr = elf->state.ELFW(elf,LIBELFBITS).ehdr; |
71 | | |
72 | | /* If no program header exists return NULL. */ |
73 | 4 | size_t phnum; |
74 | 4 | if (__elf_getphdrnum_rdlock (elf, &phnum) != 0) |
75 | 0 | goto out; |
76 | 4 | if (phnum == 0 || ehdr->e_phoff == 0) |
77 | 0 | { |
78 | 0 | __libelf_seterrno (ELF_E_NO_PHDR); |
79 | 0 | goto out; |
80 | 0 | } |
81 | | |
82 | | /* Check this doesn't overflow. */ |
83 | 4 | size_t size = phnum * sizeof (ElfW2(LIBELFBITS,Phdr)); |
84 | | |
85 | 4 | if (phnum > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Phdr)) |
86 | 4 | || ehdr->e_phoff > elf->maximum_size |
87 | 4 | || elf->maximum_size - ehdr->e_phoff < size) |
88 | 0 | { |
89 | 0 | __libelf_seterrno (ELF_E_INVALID_DATA); |
90 | 0 | goto out; |
91 | 0 | } |
92 | | |
93 | 4 | if (elf->map_address != NULL) |
94 | 4 | { |
95 | | /* First see whether the information in the ELF header is |
96 | | valid and it does not ask for too much. */ |
97 | 4 | if (unlikely (ehdr->e_phoff >= elf->maximum_size) |
98 | 4 | || unlikely (elf->maximum_size - ehdr->e_phoff < size)) |
99 | 0 | { |
100 | | /* Something is wrong. */ |
101 | 0 | __libelf_seterrno (ELF_E_INVALID_PHDR); |
102 | 0 | goto out; |
103 | 0 | } |
104 | | |
105 | | /* All the data is already mapped. Use it. */ |
106 | 4 | void *file_phdr = ((char *) elf->map_address |
107 | 4 | + elf->start_offset + ehdr->e_phoff); |
108 | 4 | if (ehdr->e_ident[EI_DATA] == MY_ELFDATA |
109 | 4 | && (ALLOW_UNALIGNED |
110 | 0 | || ((uintptr_t) file_phdr |
111 | 0 | & (__alignof__ (ElfW2(LIBELFBITS,Phdr)) - 1)) == 0)) |
112 | | /* Simply use the mapped data. */ |
113 | 0 | elf->state.ELFW(elf,LIBELFBITS).phdr = file_phdr; |
114 | 4 | else |
115 | 4 | { |
116 | 4 | ElfW2(LIBELFBITS,Phdr) *notcvt; |
117 | 4 | ElfW2(LIBELFBITS,Phdr) *phdr; |
118 | | |
119 | | /* Allocate memory for the program headers. We know the number |
120 | | of entries from the ELF header. */ |
121 | 4 | phdr = elf->state.ELFW(elf,LIBELFBITS).phdr = |
122 | 4 | (ElfW2(LIBELFBITS,Phdr) *) malloc (size); |
123 | 4 | if (elf->state.ELFW(elf,LIBELFBITS).phdr == NULL) |
124 | 0 | { |
125 | 0 | __libelf_seterrno (ELF_E_NOMEM); |
126 | 0 | goto out; |
127 | 0 | } |
128 | 4 | elf->state.ELFW(elf,LIBELFBITS).phdr_flags |= |
129 | 4 | ELF_F_MALLOCED | ELF_F_DIRTY; |
130 | | |
131 | | /* Now copy the data and at the same time convert the |
132 | | byte order. */ |
133 | | |
134 | 4 | if (ehdr->e_ident[EI_DATA] == MY_ELFDATA) |
135 | 0 | { |
136 | 0 | assert (! ALLOW_UNALIGNED); |
137 | 0 | memcpy (phdr, file_phdr, size); |
138 | 0 | } |
139 | 4 | else |
140 | 4 | { |
141 | 4 | bool copy = ! (ALLOW_UNALIGNED |
142 | 4 | || ((uintptr_t) file_phdr |
143 | 0 | & (__alignof__ (ElfW2(LIBELFBITS,Phdr)) |
144 | 0 | - 1)) == 0); |
145 | 4 | if (! copy) |
146 | 4 | notcvt = file_phdr; |
147 | 0 | else |
148 | 0 | { |
149 | 0 | notcvt = (ElfW2(LIBELFBITS,Phdr) *) malloc (size); |
150 | 0 | if (unlikely (notcvt == NULL)) |
151 | 0 | { |
152 | 0 | __libelf_seterrno (ELF_E_NOMEM); |
153 | 0 | goto out; |
154 | 0 | } |
155 | 0 | memcpy (notcvt, file_phdr, size); |
156 | 0 | } |
157 | | |
158 | 59.6k | for (size_t cnt = 0; cnt < phnum; ++cnt) |
159 | 59.6k | { |
160 | 59.6k | CONVERT_TO (phdr[cnt].p_type, notcvt[cnt].p_type); |
161 | 59.6k | CONVERT_TO (phdr[cnt].p_offset, notcvt[cnt].p_offset); |
162 | 59.6k | CONVERT_TO (phdr[cnt].p_vaddr, notcvt[cnt].p_vaddr); |
163 | 59.6k | CONVERT_TO (phdr[cnt].p_paddr, notcvt[cnt].p_paddr); |
164 | 59.6k | CONVERT_TO (phdr[cnt].p_filesz, notcvt[cnt].p_filesz); |
165 | 59.6k | CONVERT_TO (phdr[cnt].p_memsz, notcvt[cnt].p_memsz); |
166 | 59.6k | CONVERT_TO (phdr[cnt].p_flags, notcvt[cnt].p_flags); |
167 | 59.6k | CONVERT_TO (phdr[cnt].p_align, notcvt[cnt].p_align); |
168 | 59.6k | } |
169 | | |
170 | 4 | if (copy) |
171 | 0 | free (notcvt); |
172 | 4 | } |
173 | 4 | } |
174 | 4 | } |
175 | 0 | else if (likely (elf->fildes != -1)) |
176 | 0 | { |
177 | | /* Allocate memory for the program headers. We know the number |
178 | | of entries from the ELF header. */ |
179 | 0 | elf->state.ELFW(elf,LIBELFBITS).phdr = |
180 | 0 | (ElfW2(LIBELFBITS,Phdr) *) malloc (size); |
181 | 0 | if (elf->state.ELFW(elf,LIBELFBITS).phdr == NULL) |
182 | 0 | { |
183 | 0 | __libelf_seterrno (ELF_E_NOMEM); |
184 | 0 | goto out; |
185 | 0 | } |
186 | 0 | elf->state.ELFW(elf,LIBELFBITS).phdr_flags |= ELF_F_MALLOCED; |
187 | | |
188 | | /* Read the header. */ |
189 | 0 | ssize_t n = pread_retry (elf->fildes, |
190 | 0 | elf->state.ELFW(elf,LIBELFBITS).phdr, size, |
191 | 0 | elf->start_offset + ehdr->e_phoff); |
192 | 0 | if (unlikely ((size_t) n != size)) |
193 | 0 | { |
194 | | /* Severe problems. We cannot read the data. */ |
195 | 0 | __libelf_seterrno (ELF_E_READ_ERROR); |
196 | 0 | free (elf->state.ELFW(elf,LIBELFBITS).phdr); |
197 | 0 | elf->state.ELFW(elf,LIBELFBITS).phdr = NULL; |
198 | 0 | goto out; |
199 | 0 | } |
200 | | |
201 | | /* If the byte order of the file is not the same as the one |
202 | | of the host convert the data now. */ |
203 | 0 | if (ehdr->e_ident[EI_DATA] != MY_ELFDATA) |
204 | 0 | { |
205 | 0 | ElfW2(LIBELFBITS,Phdr) *phdr |
206 | 0 | = elf->state.ELFW(elf,LIBELFBITS).phdr; |
207 | |
|
208 | 0 | for (size_t cnt = 0; cnt < phnum; ++cnt) |
209 | 0 | { |
210 | 0 | CONVERT (phdr[cnt].p_type); |
211 | 0 | CONVERT (phdr[cnt].p_offset); |
212 | 0 | CONVERT (phdr[cnt].p_vaddr); |
213 | 0 | CONVERT (phdr[cnt].p_paddr); |
214 | 0 | CONVERT (phdr[cnt].p_filesz); |
215 | 0 | CONVERT (phdr[cnt].p_memsz); |
216 | 0 | CONVERT (phdr[cnt].p_flags); |
217 | 0 | CONVERT (phdr[cnt].p_align); |
218 | 0 | } |
219 | 0 | } |
220 | 0 | } |
221 | 0 | else |
222 | 0 | { |
223 | | /* The file descriptor was already enabled and not all data was |
224 | | read. */ |
225 | 0 | __libelf_seterrno (ELF_E_FD_DISABLED); |
226 | 0 | goto out; |
227 | 0 | } |
228 | | |
229 | 4 | result = elf->state.ELFW(elf,LIBELFBITS).phdr; |
230 | 4 | } |
231 | | |
232 | 4 | out: |
233 | 4 | return result; |
234 | 4 | } Line | Count | Source | 48 | 3 | { | 49 | 3 | ElfW2(LIBELFBITS,Phdr) *result; | 50 | | | 51 | | /* If the program header entry has already been filled in the code | 52 | | below must already have been run. So the class is set, too. No | 53 | | need to waste any more time here. */ | 54 | 3 | result = elf->state.ELFW(elf,LIBELFBITS).phdr; | 55 | 3 | if (likely (result != NULL)) | 56 | 0 | return result; | 57 | | | 58 | 3 | if (elf->class == 0) | 59 | 0 | elf->class = ELFW(ELFCLASS,LIBELFBITS); | 60 | 3 | else if (elf->class != ELFW(ELFCLASS,LIBELFBITS)) | 61 | 0 | { | 62 | 0 | __libelf_seterrno (ELF_E_INVALID_CLASS); | 63 | 0 | result = NULL; | 64 | 0 | goto out; | 65 | 0 | } | 66 | | | 67 | 3 | if (likely (result == NULL)) | 68 | 3 | { | 69 | | /* Read the section header table. */ | 70 | 3 | ElfW2(LIBELFBITS,Ehdr) *ehdr = elf->state.ELFW(elf,LIBELFBITS).ehdr; | 71 | | | 72 | | /* If no program header exists return NULL. */ | 73 | 3 | size_t phnum; | 74 | 3 | if (__elf_getphdrnum_rdlock (elf, &phnum) != 0) | 75 | 0 | goto out; | 76 | 3 | if (phnum == 0 || ehdr->e_phoff == 0) | 77 | 0 | { | 78 | 0 | __libelf_seterrno (ELF_E_NO_PHDR); | 79 | 0 | goto out; | 80 | 0 | } | 81 | | | 82 | | /* Check this doesn't overflow. */ | 83 | 3 | size_t size = phnum * sizeof (ElfW2(LIBELFBITS,Phdr)); | 84 | | | 85 | 3 | if (phnum > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Phdr)) | 86 | 3 | || ehdr->e_phoff > elf->maximum_size | 87 | 3 | || elf->maximum_size - ehdr->e_phoff < size) | 88 | 0 | { | 89 | 0 | __libelf_seterrno (ELF_E_INVALID_DATA); | 90 | 0 | goto out; | 91 | 0 | } | 92 | | | 93 | 3 | if (elf->map_address != NULL) | 94 | 3 | { | 95 | | /* First see whether the information in the ELF header is | 96 | | valid and it does not ask for too much. */ | 97 | 3 | if (unlikely (ehdr->e_phoff >= elf->maximum_size) | 98 | 3 | || unlikely (elf->maximum_size - ehdr->e_phoff < size)) | 99 | 0 | { | 100 | | /* Something is wrong. */ | 101 | 0 | __libelf_seterrno (ELF_E_INVALID_PHDR); | 102 | 0 | goto out; | 103 | 0 | } | 104 | | | 105 | | /* All the data is already mapped. Use it. */ | 106 | 3 | void *file_phdr = ((char *) elf->map_address | 107 | 3 | + elf->start_offset + ehdr->e_phoff); | 108 | 3 | if (ehdr->e_ident[EI_DATA] == MY_ELFDATA | 109 | 3 | && (ALLOW_UNALIGNED | 110 | 0 | || ((uintptr_t) file_phdr | 111 | 0 | & (__alignof__ (ElfW2(LIBELFBITS,Phdr)) - 1)) == 0)) | 112 | | /* Simply use the mapped data. */ | 113 | 0 | elf->state.ELFW(elf,LIBELFBITS).phdr = file_phdr; | 114 | 3 | else | 115 | 3 | { | 116 | 3 | ElfW2(LIBELFBITS,Phdr) *notcvt; | 117 | 3 | ElfW2(LIBELFBITS,Phdr) *phdr; | 118 | | | 119 | | /* Allocate memory for the program headers. We know the number | 120 | | of entries from the ELF header. */ | 121 | 3 | phdr = elf->state.ELFW(elf,LIBELFBITS).phdr = | 122 | 3 | (ElfW2(LIBELFBITS,Phdr) *) malloc (size); | 123 | 3 | if (elf->state.ELFW(elf,LIBELFBITS).phdr == NULL) | 124 | 0 | { | 125 | 0 | __libelf_seterrno (ELF_E_NOMEM); | 126 | 0 | goto out; | 127 | 0 | } | 128 | 3 | elf->state.ELFW(elf,LIBELFBITS).phdr_flags |= | 129 | 3 | ELF_F_MALLOCED | ELF_F_DIRTY; | 130 | | | 131 | | /* Now copy the data and at the same time convert the | 132 | | byte order. */ | 133 | | | 134 | 3 | if (ehdr->e_ident[EI_DATA] == MY_ELFDATA) | 135 | 0 | { | 136 | 0 | assert (! ALLOW_UNALIGNED); | 137 | 0 | memcpy (phdr, file_phdr, size); | 138 | 0 | } | 139 | 3 | else | 140 | 3 | { | 141 | 3 | bool copy = ! (ALLOW_UNALIGNED | 142 | 3 | || ((uintptr_t) file_phdr | 143 | 0 | & (__alignof__ (ElfW2(LIBELFBITS,Phdr)) | 144 | 0 | - 1)) == 0); | 145 | 3 | if (! copy) | 146 | 3 | notcvt = file_phdr; | 147 | 0 | else | 148 | 0 | { | 149 | 0 | notcvt = (ElfW2(LIBELFBITS,Phdr) *) malloc (size); | 150 | 0 | if (unlikely (notcvt == NULL)) | 151 | 0 | { | 152 | 0 | __libelf_seterrno (ELF_E_NOMEM); | 153 | 0 | goto out; | 154 | 0 | } | 155 | 0 | memcpy (notcvt, file_phdr, size); | 156 | 0 | } | 157 | | | 158 | 53.2k | for (size_t cnt = 0; cnt < phnum; ++cnt) | 159 | 53.2k | { | 160 | 53.2k | CONVERT_TO (phdr[cnt].p_type, notcvt[cnt].p_type); | 161 | 53.2k | CONVERT_TO (phdr[cnt].p_offset, notcvt[cnt].p_offset); | 162 | 53.2k | CONVERT_TO (phdr[cnt].p_vaddr, notcvt[cnt].p_vaddr); | 163 | 53.2k | CONVERT_TO (phdr[cnt].p_paddr, notcvt[cnt].p_paddr); | 164 | 53.2k | CONVERT_TO (phdr[cnt].p_filesz, notcvt[cnt].p_filesz); | 165 | 53.2k | CONVERT_TO (phdr[cnt].p_memsz, notcvt[cnt].p_memsz); | 166 | 53.2k | CONVERT_TO (phdr[cnt].p_flags, notcvt[cnt].p_flags); | 167 | 53.2k | CONVERT_TO (phdr[cnt].p_align, notcvt[cnt].p_align); | 168 | 53.2k | } | 169 | | | 170 | 3 | if (copy) | 171 | 0 | free (notcvt); | 172 | 3 | } | 173 | 3 | } | 174 | 3 | } | 175 | 0 | else if (likely (elf->fildes != -1)) | 176 | 0 | { | 177 | | /* Allocate memory for the program headers. We know the number | 178 | | of entries from the ELF header. */ | 179 | 0 | elf->state.ELFW(elf,LIBELFBITS).phdr = | 180 | 0 | (ElfW2(LIBELFBITS,Phdr) *) malloc (size); | 181 | 0 | if (elf->state.ELFW(elf,LIBELFBITS).phdr == NULL) | 182 | 0 | { | 183 | 0 | __libelf_seterrno (ELF_E_NOMEM); | 184 | 0 | goto out; | 185 | 0 | } | 186 | 0 | elf->state.ELFW(elf,LIBELFBITS).phdr_flags |= ELF_F_MALLOCED; | 187 | | | 188 | | /* Read the header. */ | 189 | 0 | ssize_t n = pread_retry (elf->fildes, | 190 | 0 | elf->state.ELFW(elf,LIBELFBITS).phdr, size, | 191 | 0 | elf->start_offset + ehdr->e_phoff); | 192 | 0 | if (unlikely ((size_t) n != size)) | 193 | 0 | { | 194 | | /* Severe problems. We cannot read the data. */ | 195 | 0 | __libelf_seterrno (ELF_E_READ_ERROR); | 196 | 0 | free (elf->state.ELFW(elf,LIBELFBITS).phdr); | 197 | 0 | elf->state.ELFW(elf,LIBELFBITS).phdr = NULL; | 198 | 0 | goto out; | 199 | 0 | } | 200 | | | 201 | | /* If the byte order of the file is not the same as the one | 202 | | of the host convert the data now. */ | 203 | 0 | if (ehdr->e_ident[EI_DATA] != MY_ELFDATA) | 204 | 0 | { | 205 | 0 | ElfW2(LIBELFBITS,Phdr) *phdr | 206 | 0 | = elf->state.ELFW(elf,LIBELFBITS).phdr; | 207 | |
| 208 | 0 | for (size_t cnt = 0; cnt < phnum; ++cnt) | 209 | 0 | { | 210 | 0 | CONVERT (phdr[cnt].p_type); | 211 | 0 | CONVERT (phdr[cnt].p_offset); | 212 | 0 | CONVERT (phdr[cnt].p_vaddr); | 213 | 0 | CONVERT (phdr[cnt].p_paddr); | 214 | 0 | CONVERT (phdr[cnt].p_filesz); | 215 | 0 | CONVERT (phdr[cnt].p_memsz); | 216 | 0 | CONVERT (phdr[cnt].p_flags); | 217 | 0 | CONVERT (phdr[cnt].p_align); | 218 | 0 | } | 219 | 0 | } | 220 | 0 | } | 221 | 0 | else | 222 | 0 | { | 223 | | /* The file descriptor was already enabled and not all data was | 224 | | read. */ | 225 | 0 | __libelf_seterrno (ELF_E_FD_DISABLED); | 226 | 0 | goto out; | 227 | 0 | } | 228 | | | 229 | 3 | result = elf->state.ELFW(elf,LIBELFBITS).phdr; | 230 | 3 | } | 231 | | | 232 | 3 | out: | 233 | 3 | return result; | 234 | 3 | } |
Line | Count | Source | 48 | 1 | { | 49 | 1 | ElfW2(LIBELFBITS,Phdr) *result; | 50 | | | 51 | | /* If the program header entry has already been filled in the code | 52 | | below must already have been run. So the class is set, too. No | 53 | | need to waste any more time here. */ | 54 | 1 | result = elf->state.ELFW(elf,LIBELFBITS).phdr; | 55 | 1 | if (likely (result != NULL)) | 56 | 0 | return result; | 57 | | | 58 | 1 | if (elf->class == 0) | 59 | 0 | elf->class = ELFW(ELFCLASS,LIBELFBITS); | 60 | 1 | else if (elf->class != ELFW(ELFCLASS,LIBELFBITS)) | 61 | 0 | { | 62 | 0 | __libelf_seterrno (ELF_E_INVALID_CLASS); | 63 | 0 | result = NULL; | 64 | 0 | goto out; | 65 | 0 | } | 66 | | | 67 | 1 | if (likely (result == NULL)) | 68 | 1 | { | 69 | | /* Read the section header table. */ | 70 | 1 | ElfW2(LIBELFBITS,Ehdr) *ehdr = elf->state.ELFW(elf,LIBELFBITS).ehdr; | 71 | | | 72 | | /* If no program header exists return NULL. */ | 73 | 1 | size_t phnum; | 74 | 1 | if (__elf_getphdrnum_rdlock (elf, &phnum) != 0) | 75 | 0 | goto out; | 76 | 1 | if (phnum == 0 || ehdr->e_phoff == 0) | 77 | 0 | { | 78 | 0 | __libelf_seterrno (ELF_E_NO_PHDR); | 79 | 0 | goto out; | 80 | 0 | } | 81 | | | 82 | | /* Check this doesn't overflow. */ | 83 | 1 | size_t size = phnum * sizeof (ElfW2(LIBELFBITS,Phdr)); | 84 | | | 85 | 1 | if (phnum > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Phdr)) | 86 | 1 | || ehdr->e_phoff > elf->maximum_size | 87 | 1 | || elf->maximum_size - ehdr->e_phoff < size) | 88 | 0 | { | 89 | 0 | __libelf_seterrno (ELF_E_INVALID_DATA); | 90 | 0 | goto out; | 91 | 0 | } | 92 | | | 93 | 1 | if (elf->map_address != NULL) | 94 | 1 | { | 95 | | /* First see whether the information in the ELF header is | 96 | | valid and it does not ask for too much. */ | 97 | 1 | if (unlikely (ehdr->e_phoff >= elf->maximum_size) | 98 | 1 | || unlikely (elf->maximum_size - ehdr->e_phoff < size)) | 99 | 0 | { | 100 | | /* Something is wrong. */ | 101 | 0 | __libelf_seterrno (ELF_E_INVALID_PHDR); | 102 | 0 | goto out; | 103 | 0 | } | 104 | | | 105 | | /* All the data is already mapped. Use it. */ | 106 | 1 | void *file_phdr = ((char *) elf->map_address | 107 | 1 | + elf->start_offset + ehdr->e_phoff); | 108 | 1 | if (ehdr->e_ident[EI_DATA] == MY_ELFDATA | 109 | 1 | && (ALLOW_UNALIGNED | 110 | 0 | || ((uintptr_t) file_phdr | 111 | 0 | & (__alignof__ (ElfW2(LIBELFBITS,Phdr)) - 1)) == 0)) | 112 | | /* Simply use the mapped data. */ | 113 | 0 | elf->state.ELFW(elf,LIBELFBITS).phdr = file_phdr; | 114 | 1 | else | 115 | 1 | { | 116 | 1 | ElfW2(LIBELFBITS,Phdr) *notcvt; | 117 | 1 | ElfW2(LIBELFBITS,Phdr) *phdr; | 118 | | | 119 | | /* Allocate memory for the program headers. We know the number | 120 | | of entries from the ELF header. */ | 121 | 1 | phdr = elf->state.ELFW(elf,LIBELFBITS).phdr = | 122 | 1 | (ElfW2(LIBELFBITS,Phdr) *) malloc (size); | 123 | 1 | if (elf->state.ELFW(elf,LIBELFBITS).phdr == NULL) | 124 | 0 | { | 125 | 0 | __libelf_seterrno (ELF_E_NOMEM); | 126 | 0 | goto out; | 127 | 0 | } | 128 | 1 | elf->state.ELFW(elf,LIBELFBITS).phdr_flags |= | 129 | 1 | ELF_F_MALLOCED | ELF_F_DIRTY; | 130 | | | 131 | | /* Now copy the data and at the same time convert the | 132 | | byte order. */ | 133 | | | 134 | 1 | if (ehdr->e_ident[EI_DATA] == MY_ELFDATA) | 135 | 0 | { | 136 | 0 | assert (! ALLOW_UNALIGNED); | 137 | 0 | memcpy (phdr, file_phdr, size); | 138 | 0 | } | 139 | 1 | else | 140 | 1 | { | 141 | 1 | bool copy = ! (ALLOW_UNALIGNED | 142 | 1 | || ((uintptr_t) file_phdr | 143 | 0 | & (__alignof__ (ElfW2(LIBELFBITS,Phdr)) | 144 | 0 | - 1)) == 0); | 145 | 1 | if (! copy) | 146 | 1 | notcvt = file_phdr; | 147 | 0 | else | 148 | 0 | { | 149 | 0 | notcvt = (ElfW2(LIBELFBITS,Phdr) *) malloc (size); | 150 | 0 | if (unlikely (notcvt == NULL)) | 151 | 0 | { | 152 | 0 | __libelf_seterrno (ELF_E_NOMEM); | 153 | 0 | goto out; | 154 | 0 | } | 155 | 0 | memcpy (notcvt, file_phdr, size); | 156 | 0 | } | 157 | | | 158 | 6.41k | for (size_t cnt = 0; cnt < phnum; ++cnt) | 159 | 6.40k | { | 160 | 6.40k | CONVERT_TO (phdr[cnt].p_type, notcvt[cnt].p_type); | 161 | 6.40k | CONVERT_TO (phdr[cnt].p_offset, notcvt[cnt].p_offset); | 162 | 6.40k | CONVERT_TO (phdr[cnt].p_vaddr, notcvt[cnt].p_vaddr); | 163 | 6.40k | CONVERT_TO (phdr[cnt].p_paddr, notcvt[cnt].p_paddr); | 164 | 6.40k | CONVERT_TO (phdr[cnt].p_filesz, notcvt[cnt].p_filesz); | 165 | 6.40k | CONVERT_TO (phdr[cnt].p_memsz, notcvt[cnt].p_memsz); | 166 | 6.40k | CONVERT_TO (phdr[cnt].p_flags, notcvt[cnt].p_flags); | 167 | 6.40k | CONVERT_TO (phdr[cnt].p_align, notcvt[cnt].p_align); | 168 | 6.40k | } | 169 | | | 170 | 1 | if (copy) | 171 | 0 | free (notcvt); | 172 | 1 | } | 173 | 1 | } | 174 | 1 | } | 175 | 0 | else if (likely (elf->fildes != -1)) | 176 | 0 | { | 177 | | /* Allocate memory for the program headers. We know the number | 178 | | of entries from the ELF header. */ | 179 | 0 | elf->state.ELFW(elf,LIBELFBITS).phdr = | 180 | 0 | (ElfW2(LIBELFBITS,Phdr) *) malloc (size); | 181 | 0 | if (elf->state.ELFW(elf,LIBELFBITS).phdr == NULL) | 182 | 0 | { | 183 | 0 | __libelf_seterrno (ELF_E_NOMEM); | 184 | 0 | goto out; | 185 | 0 | } | 186 | 0 | elf->state.ELFW(elf,LIBELFBITS).phdr_flags |= ELF_F_MALLOCED; | 187 | | | 188 | | /* Read the header. */ | 189 | 0 | ssize_t n = pread_retry (elf->fildes, | 190 | 0 | elf->state.ELFW(elf,LIBELFBITS).phdr, size, | 191 | 0 | elf->start_offset + ehdr->e_phoff); | 192 | 0 | if (unlikely ((size_t) n != size)) | 193 | 0 | { | 194 | | /* Severe problems. We cannot read the data. */ | 195 | 0 | __libelf_seterrno (ELF_E_READ_ERROR); | 196 | 0 | free (elf->state.ELFW(elf,LIBELFBITS).phdr); | 197 | 0 | elf->state.ELFW(elf,LIBELFBITS).phdr = NULL; | 198 | 0 | goto out; | 199 | 0 | } | 200 | | | 201 | | /* If the byte order of the file is not the same as the one | 202 | | of the host convert the data now. */ | 203 | 0 | if (ehdr->e_ident[EI_DATA] != MY_ELFDATA) | 204 | 0 | { | 205 | 0 | ElfW2(LIBELFBITS,Phdr) *phdr | 206 | 0 | = elf->state.ELFW(elf,LIBELFBITS).phdr; | 207 | |
| 208 | 0 | for (size_t cnt = 0; cnt < phnum; ++cnt) | 209 | 0 | { | 210 | 0 | CONVERT (phdr[cnt].p_type); | 211 | 0 | CONVERT (phdr[cnt].p_offset); | 212 | 0 | CONVERT (phdr[cnt].p_vaddr); | 213 | 0 | CONVERT (phdr[cnt].p_paddr); | 214 | 0 | CONVERT (phdr[cnt].p_filesz); | 215 | 0 | CONVERT (phdr[cnt].p_memsz); | 216 | 0 | CONVERT (phdr[cnt].p_flags); | 217 | 0 | CONVERT (phdr[cnt].p_align); | 218 | 0 | } | 219 | 0 | } | 220 | 0 | } | 221 | 0 | else | 222 | 0 | { | 223 | | /* The file descriptor was already enabled and not all data was | 224 | | read. */ | 225 | 0 | __libelf_seterrno (ELF_E_FD_DISABLED); | 226 | 0 | goto out; | 227 | 0 | } | 228 | | | 229 | 1 | result = elf->state.ELFW(elf,LIBELFBITS).phdr; | 230 | 1 | } | 231 | | | 232 | 1 | out: | 233 | 1 | return result; | 234 | 1 | } |
|
235 | | |
236 | | ElfW2(LIBELFBITS,Phdr) * |
237 | | elfw2(LIBELFBITS,getphdr) (Elf *elf) |
238 | 4 | { |
239 | 4 | ElfW2(LIBELFBITS,Phdr) *result; |
240 | | |
241 | 4 | if (elf == NULL) |
242 | 0 | return NULL; |
243 | | |
244 | 4 | if (unlikely (elf->kind != ELF_K_ELF)) |
245 | 0 | { |
246 | 0 | __libelf_seterrno (ELF_E_INVALID_HANDLE); |
247 | 0 | return NULL; |
248 | 0 | } |
249 | | |
250 | | /* If the program header entry has already been filled in the code |
251 | | * in getphdr_wrlock must already have been run. So the class is |
252 | | * set, too. No need to waste any more time here. */ |
253 | 4 | result = elf->state.ELFW(elf,LIBELFBITS).phdr; |
254 | 4 | if (likely (result != NULL)) |
255 | 0 | return result; |
256 | | |
257 | 4 | rwlock_wrlock (elf->lock); |
258 | 4 | result = __elfw2(LIBELFBITS,getphdr_wrlock) (elf); |
259 | 4 | rwlock_unlock (elf->lock); |
260 | | |
261 | 4 | return result; |
262 | 4 | } Line | Count | Source | 238 | 3 | { | 239 | 3 | ElfW2(LIBELFBITS,Phdr) *result; | 240 | | | 241 | 3 | if (elf == NULL) | 242 | 0 | return NULL; | 243 | | | 244 | 3 | if (unlikely (elf->kind != ELF_K_ELF)) | 245 | 0 | { | 246 | 0 | __libelf_seterrno (ELF_E_INVALID_HANDLE); | 247 | 0 | return NULL; | 248 | 0 | } | 249 | | | 250 | | /* If the program header entry has already been filled in the code | 251 | | * in getphdr_wrlock must already have been run. So the class is | 252 | | * set, too. No need to waste any more time here. */ | 253 | 3 | result = elf->state.ELFW(elf,LIBELFBITS).phdr; | 254 | 3 | if (likely (result != NULL)) | 255 | 0 | return result; | 256 | | | 257 | 3 | rwlock_wrlock (elf->lock); | 258 | 3 | result = __elfw2(LIBELFBITS,getphdr_wrlock) (elf); | 259 | 3 | rwlock_unlock (elf->lock); | 260 | | | 261 | 3 | return result; | 262 | 3 | } |
Line | Count | Source | 238 | 1 | { | 239 | 1 | ElfW2(LIBELFBITS,Phdr) *result; | 240 | | | 241 | 1 | if (elf == NULL) | 242 | 0 | return NULL; | 243 | | | 244 | 1 | if (unlikely (elf->kind != ELF_K_ELF)) | 245 | 0 | { | 246 | 0 | __libelf_seterrno (ELF_E_INVALID_HANDLE); | 247 | 0 | return NULL; | 248 | 0 | } | 249 | | | 250 | | /* If the program header entry has already been filled in the code | 251 | | * in getphdr_wrlock must already have been run. So the class is | 252 | | * set, too. No need to waste any more time here. */ | 253 | 1 | result = elf->state.ELFW(elf,LIBELFBITS).phdr; | 254 | 1 | if (likely (result != NULL)) | 255 | 0 | return result; | 256 | | | 257 | 1 | rwlock_wrlock (elf->lock); | 258 | 1 | result = __elfw2(LIBELFBITS,getphdr_wrlock) (elf); | 259 | 1 | rwlock_unlock (elf->lock); | 260 | | | 261 | 1 | return result; | 262 | 1 | } |
|
263 | | INTDEF(elfw2(LIBELFBITS,getphdr)) |