/src/elfutils/libelf/elf32_getshdr.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Return section header. |
2 | | Copyright (C) 1998-2002, 2005, 2007, 2009, 2012, 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 <assert.h> |
35 | | #include <errno.h> |
36 | | #include <stdbool.h> |
37 | | |
38 | | #include "libelfP.h" |
39 | | #include "common.h" |
40 | | |
41 | | #ifndef LIBELFBITS |
42 | | # define LIBELFBITS 32 |
43 | | #endif |
44 | | |
45 | | |
46 | | static ElfW2(LIBELFBITS,Shdr) * |
47 | | load_shdr_wrlock (Elf_Scn *scn) |
48 | 1.95k | { |
49 | 1.95k | ElfW2(LIBELFBITS,Shdr) *result; |
50 | | |
51 | | /* Read the section header table. */ |
52 | 1.95k | Elf *elf = scn->elf; |
53 | 1.95k | ElfW2(LIBELFBITS,Ehdr) *ehdr = elf->state.ELFW(elf,LIBELFBITS).ehdr; |
54 | | |
55 | | /* Try again, maybe the data is there now. */ |
56 | 1.95k | result = scn->shdr.ELFW(e,LIBELFBITS); |
57 | 1.95k | if (result != NULL) |
58 | 0 | goto out; |
59 | | |
60 | 1.95k | size_t shnum; |
61 | 1.95k | if (__elf_getshdrnum_rdlock (elf, &shnum) != 0 |
62 | 1.95k | || shnum > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Shdr))) |
63 | 0 | goto out; |
64 | 1.95k | size_t size = shnum * sizeof (ElfW2(LIBELFBITS,Shdr)); |
65 | | |
66 | | /* Allocate memory for the section headers. We know the number |
67 | | of entries from the ELF header. */ |
68 | 1.95k | ElfW2(LIBELFBITS,Shdr) *shdr = elf->state.ELFW(elf,LIBELFBITS).shdr = |
69 | 1.95k | (ElfW2(LIBELFBITS,Shdr) *) malloc (size); |
70 | 1.95k | if (elf->state.ELFW(elf,LIBELFBITS).shdr == NULL) |
71 | 0 | { |
72 | 0 | __libelf_seterrno (ELF_E_NOMEM); |
73 | 0 | goto out; |
74 | 0 | } |
75 | 1.95k | elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 1; |
76 | | |
77 | 1.95k | if (elf->map_address != NULL) |
78 | 1.95k | { |
79 | | /* First see whether the information in the ELF header is |
80 | | valid and it does not ask for too much. */ |
81 | 1.95k | if (unlikely (ehdr->e_shoff >= elf->maximum_size) |
82 | 1.95k | || unlikely (elf->maximum_size - ehdr->e_shoff < size)) |
83 | 0 | { |
84 | | /* Something is wrong. */ |
85 | 0 | __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER); |
86 | 0 | goto free_and_out; |
87 | 0 | } |
88 | | |
89 | 1.95k | ElfW2(LIBELFBITS,Shdr) *notcvt; |
90 | | |
91 | | /* All the data is already mapped. If we could use it |
92 | | directly this would already have happened. Unless |
93 | | we allocated the memory ourselves and the ELF_F_MALLOCED |
94 | | flag is set. */ |
95 | 1.95k | void *file_shdr = ((char *) elf->map_address |
96 | 1.95k | + elf->start_offset + ehdr->e_shoff); |
97 | | |
98 | 1.95k | assert ((elf->flags & ELF_F_MALLOCED) |
99 | 1.95k | || ehdr->e_ident[EI_DATA] != MY_ELFDATA |
100 | 1.95k | || elf->cmd == ELF_C_READ_MMAP |
101 | 1.95k | || (! ALLOW_UNALIGNED |
102 | 1.95k | && ((uintptr_t) file_shdr |
103 | 1.95k | & (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1)) != 0)); |
104 | | |
105 | | /* Now copy the data and at the same time convert the byte order. */ |
106 | 1.95k | if (ehdr->e_ident[EI_DATA] == MY_ELFDATA) |
107 | 0 | { |
108 | 0 | assert ((elf->flags & ELF_F_MALLOCED) |
109 | 0 | || elf->cmd == ELF_C_READ_MMAP |
110 | 0 | || ! ALLOW_UNALIGNED); |
111 | 0 | memcpy (shdr, file_shdr, size); |
112 | 0 | } |
113 | 1.95k | else |
114 | 1.95k | { |
115 | 1.95k | bool copy = ! (ALLOW_UNALIGNED |
116 | 1.95k | || ((uintptr_t) file_shdr |
117 | 0 | & (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1)) |
118 | 0 | == 0); |
119 | 1.95k | if (! copy) |
120 | 1.95k | notcvt = (ElfW2(LIBELFBITS,Shdr) *) |
121 | 1.95k | ((char *) elf->map_address |
122 | 1.95k | + elf->start_offset + ehdr->e_shoff); |
123 | 0 | else |
124 | 0 | { |
125 | 0 | notcvt = (ElfW2(LIBELFBITS,Shdr) *) malloc (size); |
126 | 0 | if (unlikely (notcvt == NULL)) |
127 | 0 | { |
128 | 0 | __libelf_seterrno (ELF_E_NOMEM); |
129 | 0 | goto free_and_out; |
130 | 0 | } |
131 | 0 | memcpy (notcvt, ((char *) elf->map_address |
132 | 0 | + elf->start_offset + ehdr->e_shoff), |
133 | 0 | size); |
134 | 0 | } |
135 | | |
136 | 1.78M | for (size_t cnt = 0; cnt < shnum; ++cnt) |
137 | 1.78M | { |
138 | 1.78M | CONVERT_TO (shdr[cnt].sh_name, notcvt[cnt].sh_name); |
139 | 1.78M | CONVERT_TO (shdr[cnt].sh_type, notcvt[cnt].sh_type); |
140 | 1.78M | CONVERT_TO (shdr[cnt].sh_flags, notcvt[cnt].sh_flags); |
141 | 1.78M | CONVERT_TO (shdr[cnt].sh_addr, notcvt[cnt].sh_addr); |
142 | 1.78M | CONVERT_TO (shdr[cnt].sh_offset, notcvt[cnt].sh_offset); |
143 | 1.78M | CONVERT_TO (shdr[cnt].sh_size, notcvt[cnt].sh_size); |
144 | 1.78M | CONVERT_TO (shdr[cnt].sh_link, notcvt[cnt].sh_link); |
145 | 1.78M | CONVERT_TO (shdr[cnt].sh_info, notcvt[cnt].sh_info); |
146 | 1.78M | CONVERT_TO (shdr[cnt].sh_addralign, |
147 | 1.78M | notcvt[cnt].sh_addralign); |
148 | 1.78M | CONVERT_TO (shdr[cnt].sh_entsize, notcvt[cnt].sh_entsize); |
149 | 1.78M | } |
150 | | |
151 | 1.95k | if (copy) |
152 | 0 | free (notcvt); |
153 | 1.95k | } |
154 | 1.95k | } |
155 | 0 | else if (likely (elf->fildes != -1)) |
156 | 0 | { |
157 | | /* Read the header. */ |
158 | 0 | ssize_t n = pread_retry (elf->fildes, |
159 | 0 | elf->state.ELFW(elf,LIBELFBITS).shdr, size, |
160 | 0 | elf->start_offset + ehdr->e_shoff); |
161 | 0 | if (unlikely ((size_t) n != size)) |
162 | 0 | { |
163 | | /* Severe problems. We cannot read the data. */ |
164 | 0 | __libelf_seterrno (ELF_E_READ_ERROR); |
165 | 0 | goto free_and_out; |
166 | 0 | } |
167 | | |
168 | | /* If the byte order of the file is not the same as the one |
169 | | of the host convert the data now. */ |
170 | 0 | if (ehdr->e_ident[EI_DATA] != MY_ELFDATA) |
171 | 0 | for (size_t cnt = 0; cnt < shnum; ++cnt) |
172 | 0 | { |
173 | 0 | CONVERT (shdr[cnt].sh_name); |
174 | 0 | CONVERT (shdr[cnt].sh_type); |
175 | 0 | CONVERT (shdr[cnt].sh_flags); |
176 | 0 | CONVERT (shdr[cnt].sh_addr); |
177 | 0 | CONVERT (shdr[cnt].sh_offset); |
178 | 0 | CONVERT (shdr[cnt].sh_size); |
179 | 0 | CONVERT (shdr[cnt].sh_link); |
180 | 0 | CONVERT (shdr[cnt].sh_info); |
181 | 0 | CONVERT (shdr[cnt].sh_addralign); |
182 | 0 | CONVERT (shdr[cnt].sh_entsize); |
183 | 0 | } |
184 | 0 | } |
185 | 0 | else |
186 | 0 | { |
187 | | /* The file descriptor was already enabled and not all data was |
188 | | read. Undo the allocation. */ |
189 | 0 | __libelf_seterrno (ELF_E_FD_DISABLED); |
190 | |
|
191 | 0 | free_and_out: |
192 | 0 | free (shdr); |
193 | 0 | elf->state.ELFW(elf,LIBELFBITS).shdr = NULL; |
194 | 0 | elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 0; |
195 | |
|
196 | 0 | goto out; |
197 | 0 | } |
198 | | |
199 | | /* Set the pointers in the `scn's. */ |
200 | 1.78M | for (size_t cnt = 0; cnt < shnum; ++cnt) |
201 | 1.78M | elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shdr.ELFW(e,LIBELFBITS) |
202 | 1.78M | = &elf->state.ELFW(elf,LIBELFBITS).shdr[cnt]; |
203 | | |
204 | 1.95k | result = scn->shdr.ELFW(e,LIBELFBITS); |
205 | 1.95k | assert (result != NULL); |
206 | | |
207 | 1.95k | out: |
208 | 1.95k | return result; |
209 | 1.95k | } elf32_getshdr.c:load_shdr_wrlock Line | Count | Source | 48 | 948 | { | 49 | 948 | ElfW2(LIBELFBITS,Shdr) *result; | 50 | | | 51 | | /* Read the section header table. */ | 52 | 948 | Elf *elf = scn->elf; | 53 | 948 | ElfW2(LIBELFBITS,Ehdr) *ehdr = elf->state.ELFW(elf,LIBELFBITS).ehdr; | 54 | | | 55 | | /* Try again, maybe the data is there now. */ | 56 | 948 | result = scn->shdr.ELFW(e,LIBELFBITS); | 57 | 948 | if (result != NULL) | 58 | 0 | goto out; | 59 | | | 60 | 948 | size_t shnum; | 61 | 948 | if (__elf_getshdrnum_rdlock (elf, &shnum) != 0 | 62 | 948 | || shnum > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Shdr))) | 63 | 0 | goto out; | 64 | 948 | size_t size = shnum * sizeof (ElfW2(LIBELFBITS,Shdr)); | 65 | | | 66 | | /* Allocate memory for the section headers. We know the number | 67 | | of entries from the ELF header. */ | 68 | 948 | ElfW2(LIBELFBITS,Shdr) *shdr = elf->state.ELFW(elf,LIBELFBITS).shdr = | 69 | 948 | (ElfW2(LIBELFBITS,Shdr) *) malloc (size); | 70 | 948 | if (elf->state.ELFW(elf,LIBELFBITS).shdr == NULL) | 71 | 0 | { | 72 | 0 | __libelf_seterrno (ELF_E_NOMEM); | 73 | 0 | goto out; | 74 | 0 | } | 75 | 948 | elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 1; | 76 | | | 77 | 948 | if (elf->map_address != NULL) | 78 | 948 | { | 79 | | /* First see whether the information in the ELF header is | 80 | | valid and it does not ask for too much. */ | 81 | 948 | if (unlikely (ehdr->e_shoff >= elf->maximum_size) | 82 | 948 | || unlikely (elf->maximum_size - ehdr->e_shoff < size)) | 83 | 0 | { | 84 | | /* Something is wrong. */ | 85 | 0 | __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER); | 86 | 0 | goto free_and_out; | 87 | 0 | } | 88 | | | 89 | 948 | ElfW2(LIBELFBITS,Shdr) *notcvt; | 90 | | | 91 | | /* All the data is already mapped. If we could use it | 92 | | directly this would already have happened. Unless | 93 | | we allocated the memory ourselves and the ELF_F_MALLOCED | 94 | | flag is set. */ | 95 | 948 | void *file_shdr = ((char *) elf->map_address | 96 | 948 | + elf->start_offset + ehdr->e_shoff); | 97 | | | 98 | 948 | assert ((elf->flags & ELF_F_MALLOCED) | 99 | 948 | || ehdr->e_ident[EI_DATA] != MY_ELFDATA | 100 | 948 | || elf->cmd == ELF_C_READ_MMAP | 101 | 948 | || (! ALLOW_UNALIGNED | 102 | 948 | && ((uintptr_t) file_shdr | 103 | 948 | & (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1)) != 0)); | 104 | | | 105 | | /* Now copy the data and at the same time convert the byte order. */ | 106 | 948 | if (ehdr->e_ident[EI_DATA] == MY_ELFDATA) | 107 | 0 | { | 108 | 0 | assert ((elf->flags & ELF_F_MALLOCED) | 109 | 0 | || elf->cmd == ELF_C_READ_MMAP | 110 | 0 | || ! ALLOW_UNALIGNED); | 111 | 0 | memcpy (shdr, file_shdr, size); | 112 | 0 | } | 113 | 948 | else | 114 | 948 | { | 115 | 948 | bool copy = ! (ALLOW_UNALIGNED | 116 | 948 | || ((uintptr_t) file_shdr | 117 | 0 | & (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1)) | 118 | 0 | == 0); | 119 | 948 | if (! copy) | 120 | 948 | notcvt = (ElfW2(LIBELFBITS,Shdr) *) | 121 | 948 | ((char *) elf->map_address | 122 | 948 | + elf->start_offset + ehdr->e_shoff); | 123 | 0 | else | 124 | 0 | { | 125 | 0 | notcvt = (ElfW2(LIBELFBITS,Shdr) *) malloc (size); | 126 | 0 | if (unlikely (notcvt == NULL)) | 127 | 0 | { | 128 | 0 | __libelf_seterrno (ELF_E_NOMEM); | 129 | 0 | goto free_and_out; | 130 | 0 | } | 131 | 0 | memcpy (notcvt, ((char *) elf->map_address | 132 | 0 | + elf->start_offset + ehdr->e_shoff), | 133 | 0 | size); | 134 | 0 | } | 135 | | | 136 | 692k | for (size_t cnt = 0; cnt < shnum; ++cnt) | 137 | 691k | { | 138 | 691k | CONVERT_TO (shdr[cnt].sh_name, notcvt[cnt].sh_name); | 139 | 691k | CONVERT_TO (shdr[cnt].sh_type, notcvt[cnt].sh_type); | 140 | 691k | CONVERT_TO (shdr[cnt].sh_flags, notcvt[cnt].sh_flags); | 141 | 691k | CONVERT_TO (shdr[cnt].sh_addr, notcvt[cnt].sh_addr); | 142 | 691k | CONVERT_TO (shdr[cnt].sh_offset, notcvt[cnt].sh_offset); | 143 | 691k | CONVERT_TO (shdr[cnt].sh_size, notcvt[cnt].sh_size); | 144 | 691k | CONVERT_TO (shdr[cnt].sh_link, notcvt[cnt].sh_link); | 145 | 691k | CONVERT_TO (shdr[cnt].sh_info, notcvt[cnt].sh_info); | 146 | 691k | CONVERT_TO (shdr[cnt].sh_addralign, | 147 | 691k | notcvt[cnt].sh_addralign); | 148 | 691k | CONVERT_TO (shdr[cnt].sh_entsize, notcvt[cnt].sh_entsize); | 149 | 691k | } | 150 | | | 151 | 948 | if (copy) | 152 | 0 | free (notcvt); | 153 | 948 | } | 154 | 948 | } | 155 | 0 | else if (likely (elf->fildes != -1)) | 156 | 0 | { | 157 | | /* Read the header. */ | 158 | 0 | ssize_t n = pread_retry (elf->fildes, | 159 | 0 | elf->state.ELFW(elf,LIBELFBITS).shdr, size, | 160 | 0 | elf->start_offset + ehdr->e_shoff); | 161 | 0 | if (unlikely ((size_t) n != size)) | 162 | 0 | { | 163 | | /* Severe problems. We cannot read the data. */ | 164 | 0 | __libelf_seterrno (ELF_E_READ_ERROR); | 165 | 0 | goto free_and_out; | 166 | 0 | } | 167 | | | 168 | | /* If the byte order of the file is not the same as the one | 169 | | of the host convert the data now. */ | 170 | 0 | if (ehdr->e_ident[EI_DATA] != MY_ELFDATA) | 171 | 0 | for (size_t cnt = 0; cnt < shnum; ++cnt) | 172 | 0 | { | 173 | 0 | CONVERT (shdr[cnt].sh_name); | 174 | 0 | CONVERT (shdr[cnt].sh_type); | 175 | 0 | CONVERT (shdr[cnt].sh_flags); | 176 | 0 | CONVERT (shdr[cnt].sh_addr); | 177 | 0 | CONVERT (shdr[cnt].sh_offset); | 178 | 0 | CONVERT (shdr[cnt].sh_size); | 179 | 0 | CONVERT (shdr[cnt].sh_link); | 180 | 0 | CONVERT (shdr[cnt].sh_info); | 181 | 0 | CONVERT (shdr[cnt].sh_addralign); | 182 | 0 | CONVERT (shdr[cnt].sh_entsize); | 183 | 0 | } | 184 | 0 | } | 185 | 0 | else | 186 | 0 | { | 187 | | /* The file descriptor was already enabled and not all data was | 188 | | read. Undo the allocation. */ | 189 | 0 | __libelf_seterrno (ELF_E_FD_DISABLED); | 190 | |
| 191 | 0 | free_and_out: | 192 | 0 | free (shdr); | 193 | 0 | elf->state.ELFW(elf,LIBELFBITS).shdr = NULL; | 194 | 0 | elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 0; | 195 | |
| 196 | 0 | goto out; | 197 | 0 | } | 198 | | | 199 | | /* Set the pointers in the `scn's. */ | 200 | 692k | for (size_t cnt = 0; cnt < shnum; ++cnt) | 201 | 691k | elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shdr.ELFW(e,LIBELFBITS) | 202 | 691k | = &elf->state.ELFW(elf,LIBELFBITS).shdr[cnt]; | 203 | | | 204 | 948 | result = scn->shdr.ELFW(e,LIBELFBITS); | 205 | 948 | assert (result != NULL); | 206 | | | 207 | 948 | out: | 208 | 948 | return result; | 209 | 948 | } |
elf64_getshdr.c:load_shdr_wrlock Line | Count | Source | 48 | 1.00k | { | 49 | 1.00k | ElfW2(LIBELFBITS,Shdr) *result; | 50 | | | 51 | | /* Read the section header table. */ | 52 | 1.00k | Elf *elf = scn->elf; | 53 | 1.00k | ElfW2(LIBELFBITS,Ehdr) *ehdr = elf->state.ELFW(elf,LIBELFBITS).ehdr; | 54 | | | 55 | | /* Try again, maybe the data is there now. */ | 56 | 1.00k | result = scn->shdr.ELFW(e,LIBELFBITS); | 57 | 1.00k | if (result != NULL) | 58 | 0 | goto out; | 59 | | | 60 | 1.00k | size_t shnum; | 61 | 1.00k | if (__elf_getshdrnum_rdlock (elf, &shnum) != 0 | 62 | 1.00k | || shnum > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Shdr))) | 63 | 0 | goto out; | 64 | 1.00k | size_t size = shnum * sizeof (ElfW2(LIBELFBITS,Shdr)); | 65 | | | 66 | | /* Allocate memory for the section headers. We know the number | 67 | | of entries from the ELF header. */ | 68 | 1.00k | ElfW2(LIBELFBITS,Shdr) *shdr = elf->state.ELFW(elf,LIBELFBITS).shdr = | 69 | 1.00k | (ElfW2(LIBELFBITS,Shdr) *) malloc (size); | 70 | 1.00k | if (elf->state.ELFW(elf,LIBELFBITS).shdr == NULL) | 71 | 0 | { | 72 | 0 | __libelf_seterrno (ELF_E_NOMEM); | 73 | 0 | goto out; | 74 | 0 | } | 75 | 1.00k | elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 1; | 76 | | | 77 | 1.00k | if (elf->map_address != NULL) | 78 | 1.00k | { | 79 | | /* First see whether the information in the ELF header is | 80 | | valid and it does not ask for too much. */ | 81 | 1.00k | if (unlikely (ehdr->e_shoff >= elf->maximum_size) | 82 | 1.00k | || unlikely (elf->maximum_size - ehdr->e_shoff < size)) | 83 | 0 | { | 84 | | /* Something is wrong. */ | 85 | 0 | __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER); | 86 | 0 | goto free_and_out; | 87 | 0 | } | 88 | | | 89 | 1.00k | ElfW2(LIBELFBITS,Shdr) *notcvt; | 90 | | | 91 | | /* All the data is already mapped. If we could use it | 92 | | directly this would already have happened. Unless | 93 | | we allocated the memory ourselves and the ELF_F_MALLOCED | 94 | | flag is set. */ | 95 | 1.00k | void *file_shdr = ((char *) elf->map_address | 96 | 1.00k | + elf->start_offset + ehdr->e_shoff); | 97 | | | 98 | 1.00k | assert ((elf->flags & ELF_F_MALLOCED) | 99 | 1.00k | || ehdr->e_ident[EI_DATA] != MY_ELFDATA | 100 | 1.00k | || elf->cmd == ELF_C_READ_MMAP | 101 | 1.00k | || (! ALLOW_UNALIGNED | 102 | 1.00k | && ((uintptr_t) file_shdr | 103 | 1.00k | & (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1)) != 0)); | 104 | | | 105 | | /* Now copy the data and at the same time convert the byte order. */ | 106 | 1.00k | if (ehdr->e_ident[EI_DATA] == MY_ELFDATA) | 107 | 0 | { | 108 | 0 | assert ((elf->flags & ELF_F_MALLOCED) | 109 | 0 | || elf->cmd == ELF_C_READ_MMAP | 110 | 0 | || ! ALLOW_UNALIGNED); | 111 | 0 | memcpy (shdr, file_shdr, size); | 112 | 0 | } | 113 | 1.00k | else | 114 | 1.00k | { | 115 | 1.00k | bool copy = ! (ALLOW_UNALIGNED | 116 | 1.00k | || ((uintptr_t) file_shdr | 117 | 0 | & (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1)) | 118 | 0 | == 0); | 119 | 1.00k | if (! copy) | 120 | 1.00k | notcvt = (ElfW2(LIBELFBITS,Shdr) *) | 121 | 1.00k | ((char *) elf->map_address | 122 | 1.00k | + elf->start_offset + ehdr->e_shoff); | 123 | 0 | else | 124 | 0 | { | 125 | 0 | notcvt = (ElfW2(LIBELFBITS,Shdr) *) malloc (size); | 126 | 0 | if (unlikely (notcvt == NULL)) | 127 | 0 | { | 128 | 0 | __libelf_seterrno (ELF_E_NOMEM); | 129 | 0 | goto free_and_out; | 130 | 0 | } | 131 | 0 | memcpy (notcvt, ((char *) elf->map_address | 132 | 0 | + elf->start_offset + ehdr->e_shoff), | 133 | 0 | size); | 134 | 0 | } | 135 | | | 136 | 1.09M | for (size_t cnt = 0; cnt < shnum; ++cnt) | 137 | 1.09M | { | 138 | 1.09M | CONVERT_TO (shdr[cnt].sh_name, notcvt[cnt].sh_name); | 139 | 1.09M | CONVERT_TO (shdr[cnt].sh_type, notcvt[cnt].sh_type); | 140 | 1.09M | CONVERT_TO (shdr[cnt].sh_flags, notcvt[cnt].sh_flags); | 141 | 1.09M | CONVERT_TO (shdr[cnt].sh_addr, notcvt[cnt].sh_addr); | 142 | 1.09M | CONVERT_TO (shdr[cnt].sh_offset, notcvt[cnt].sh_offset); | 143 | 1.09M | CONVERT_TO (shdr[cnt].sh_size, notcvt[cnt].sh_size); | 144 | 1.09M | CONVERT_TO (shdr[cnt].sh_link, notcvt[cnt].sh_link); | 145 | 1.09M | CONVERT_TO (shdr[cnt].sh_info, notcvt[cnt].sh_info); | 146 | 1.09M | CONVERT_TO (shdr[cnt].sh_addralign, | 147 | 1.09M | notcvt[cnt].sh_addralign); | 148 | 1.09M | CONVERT_TO (shdr[cnt].sh_entsize, notcvt[cnt].sh_entsize); | 149 | 1.09M | } | 150 | | | 151 | 1.00k | if (copy) | 152 | 0 | free (notcvt); | 153 | 1.00k | } | 154 | 1.00k | } | 155 | 0 | else if (likely (elf->fildes != -1)) | 156 | 0 | { | 157 | | /* Read the header. */ | 158 | 0 | ssize_t n = pread_retry (elf->fildes, | 159 | 0 | elf->state.ELFW(elf,LIBELFBITS).shdr, size, | 160 | 0 | elf->start_offset + ehdr->e_shoff); | 161 | 0 | if (unlikely ((size_t) n != size)) | 162 | 0 | { | 163 | | /* Severe problems. We cannot read the data. */ | 164 | 0 | __libelf_seterrno (ELF_E_READ_ERROR); | 165 | 0 | goto free_and_out; | 166 | 0 | } | 167 | | | 168 | | /* If the byte order of the file is not the same as the one | 169 | | of the host convert the data now. */ | 170 | 0 | if (ehdr->e_ident[EI_DATA] != MY_ELFDATA) | 171 | 0 | for (size_t cnt = 0; cnt < shnum; ++cnt) | 172 | 0 | { | 173 | 0 | CONVERT (shdr[cnt].sh_name); | 174 | 0 | CONVERT (shdr[cnt].sh_type); | 175 | 0 | CONVERT (shdr[cnt].sh_flags); | 176 | 0 | CONVERT (shdr[cnt].sh_addr); | 177 | 0 | CONVERT (shdr[cnt].sh_offset); | 178 | 0 | CONVERT (shdr[cnt].sh_size); | 179 | 0 | CONVERT (shdr[cnt].sh_link); | 180 | 0 | CONVERT (shdr[cnt].sh_info); | 181 | 0 | CONVERT (shdr[cnt].sh_addralign); | 182 | 0 | CONVERT (shdr[cnt].sh_entsize); | 183 | 0 | } | 184 | 0 | } | 185 | 0 | else | 186 | 0 | { | 187 | | /* The file descriptor was already enabled and not all data was | 188 | | read. Undo the allocation. */ | 189 | 0 | __libelf_seterrno (ELF_E_FD_DISABLED); | 190 | |
| 191 | 0 | free_and_out: | 192 | 0 | free (shdr); | 193 | 0 | elf->state.ELFW(elf,LIBELFBITS).shdr = NULL; | 194 | 0 | elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 0; | 195 | |
| 196 | 0 | goto out; | 197 | 0 | } | 198 | | | 199 | | /* Set the pointers in the `scn's. */ | 200 | 1.09M | for (size_t cnt = 0; cnt < shnum; ++cnt) | 201 | 1.09M | elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shdr.ELFW(e,LIBELFBITS) | 202 | 1.09M | = &elf->state.ELFW(elf,LIBELFBITS).shdr[cnt]; | 203 | | | 204 | 1.00k | result = scn->shdr.ELFW(e,LIBELFBITS); | 205 | 1.00k | assert (result != NULL); | 206 | | | 207 | 1.00k | out: | 208 | 1.00k | return result; | 209 | 1.00k | } |
|
210 | | |
211 | | static bool |
212 | | scn_valid (Elf_Scn *scn) |
213 | 982k | { |
214 | 982k | if (scn == NULL) |
215 | 0 | return false; |
216 | | |
217 | 982k | if (unlikely (scn->elf->state.elf.ehdr == NULL)) |
218 | 0 | { |
219 | 0 | __libelf_seterrno (ELF_E_WRONG_ORDER_EHDR); |
220 | 0 | return false; |
221 | 0 | } |
222 | | |
223 | 982k | if (unlikely (scn->elf->class != ELFW(ELFCLASS,LIBELFBITS))) |
224 | 0 | { |
225 | 0 | __libelf_seterrno (ELF_E_INVALID_CLASS); |
226 | 0 | return false; |
227 | 0 | } |
228 | | |
229 | 982k | return true; |
230 | 982k | } elf32_getshdr.c:scn_valid Line | Count | Source | 213 | 969k | { | 214 | 969k | if (scn == NULL) | 215 | 0 | return false; | 216 | | | 217 | 969k | if (unlikely (scn->elf->state.elf.ehdr == NULL)) | 218 | 0 | { | 219 | 0 | __libelf_seterrno (ELF_E_WRONG_ORDER_EHDR); | 220 | 0 | return false; | 221 | 0 | } | 222 | | | 223 | 969k | if (unlikely (scn->elf->class != ELFW(ELFCLASS,LIBELFBITS))) | 224 | 0 | { | 225 | 0 | __libelf_seterrno (ELF_E_INVALID_CLASS); | 226 | 0 | return false; | 227 | 0 | } | 228 | | | 229 | 969k | return true; | 230 | 969k | } |
elf64_getshdr.c:scn_valid Line | Count | Source | 213 | 12.6k | { | 214 | 12.6k | if (scn == NULL) | 215 | 0 | return false; | 216 | | | 217 | 12.6k | if (unlikely (scn->elf->state.elf.ehdr == NULL)) | 218 | 0 | { | 219 | 0 | __libelf_seterrno (ELF_E_WRONG_ORDER_EHDR); | 220 | 0 | return false; | 221 | 0 | } | 222 | | | 223 | 12.6k | if (unlikely (scn->elf->class != ELFW(ELFCLASS,LIBELFBITS))) | 224 | 0 | { | 225 | 0 | __libelf_seterrno (ELF_E_INVALID_CLASS); | 226 | 0 | return false; | 227 | 0 | } | 228 | | | 229 | 12.6k | return true; | 230 | 12.6k | } |
|
231 | | |
232 | | ElfW2(LIBELFBITS,Shdr) * |
233 | | internal_function |
234 | | __elfw2(LIBELFBITS,getshdr_rdlock) (Elf_Scn *scn) |
235 | 492k | { |
236 | 492k | ElfW2(LIBELFBITS,Shdr) *result; |
237 | | |
238 | 492k | if (!scn_valid (scn)) |
239 | 0 | return NULL; |
240 | | |
241 | 492k | result = scn->shdr.ELFW(e,LIBELFBITS); |
242 | 492k | if (result == NULL) |
243 | 1.95k | { |
244 | 1.95k | rwlock_unlock (scn->elf->lock); |
245 | 1.95k | rwlock_wrlock (scn->elf->lock); |
246 | 1.95k | result = scn->shdr.ELFW(e,LIBELFBITS); |
247 | 1.95k | if (result == NULL) |
248 | 1.95k | result = load_shdr_wrlock (scn); |
249 | 1.95k | } |
250 | | |
251 | 492k | return result; |
252 | 492k | } Line | Count | Source | 235 | 485k | { | 236 | 485k | ElfW2(LIBELFBITS,Shdr) *result; | 237 | | | 238 | 485k | if (!scn_valid (scn)) | 239 | 0 | return NULL; | 240 | | | 241 | 485k | result = scn->shdr.ELFW(e,LIBELFBITS); | 242 | 485k | if (result == NULL) | 243 | 948 | { | 244 | 948 | rwlock_unlock (scn->elf->lock); | 245 | 948 | rwlock_wrlock (scn->elf->lock); | 246 | 948 | result = scn->shdr.ELFW(e,LIBELFBITS); | 247 | 948 | if (result == NULL) | 248 | 948 | result = load_shdr_wrlock (scn); | 249 | 948 | } | 250 | | | 251 | 485k | return result; | 252 | 485k | } |
Line | Count | Source | 235 | 6.82k | { | 236 | 6.82k | ElfW2(LIBELFBITS,Shdr) *result; | 237 | | | 238 | 6.82k | if (!scn_valid (scn)) | 239 | 0 | return NULL; | 240 | | | 241 | 6.82k | result = scn->shdr.ELFW(e,LIBELFBITS); | 242 | 6.82k | if (result == NULL) | 243 | 1.00k | { | 244 | 1.00k | rwlock_unlock (scn->elf->lock); | 245 | 1.00k | rwlock_wrlock (scn->elf->lock); | 246 | 1.00k | result = scn->shdr.ELFW(e,LIBELFBITS); | 247 | 1.00k | if (result == NULL) | 248 | 1.00k | result = load_shdr_wrlock (scn); | 249 | 1.00k | } | 250 | | | 251 | 6.82k | return result; | 252 | 6.82k | } |
|
253 | | |
254 | | ElfW2(LIBELFBITS,Shdr) * |
255 | | internal_function |
256 | | __elfw2(LIBELFBITS,getshdr_wrlock) (Elf_Scn *scn) |
257 | 0 | { |
258 | 0 | ElfW2(LIBELFBITS,Shdr) *result; |
259 | |
|
260 | 0 | if (!scn_valid (scn)) |
261 | 0 | return NULL; |
262 | | |
263 | 0 | result = scn->shdr.ELFW(e,LIBELFBITS); |
264 | 0 | if (result == NULL) |
265 | 0 | result = load_shdr_wrlock (scn); |
266 | |
|
267 | 0 | return result; |
268 | 0 | } Unexecuted instantiation: __elf32_getshdr_wrlock Unexecuted instantiation: __elf64_getshdr_wrlock |
269 | | |
270 | | ElfW2(LIBELFBITS,Shdr) * |
271 | | elfw2(LIBELFBITS,getshdr) (Elf_Scn *scn) |
272 | 490k | { |
273 | 490k | ElfW2(LIBELFBITS,Shdr) *result; |
274 | | |
275 | 490k | if (!scn_valid (scn)) |
276 | 0 | return NULL; |
277 | | |
278 | 490k | rwlock_rdlock (scn->elf->lock); |
279 | 490k | result = __elfw2(LIBELFBITS,getshdr_rdlock) (scn); |
280 | 490k | rwlock_unlock (scn->elf->lock); |
281 | | |
282 | 490k | return result; |
283 | 490k | } Line | Count | Source | 272 | 484k | { | 273 | 484k | ElfW2(LIBELFBITS,Shdr) *result; | 274 | | | 275 | 484k | if (!scn_valid (scn)) | 276 | 0 | return NULL; | 277 | | | 278 | 484k | rwlock_rdlock (scn->elf->lock); | 279 | 484k | result = __elfw2(LIBELFBITS,getshdr_rdlock) (scn); | 280 | 484k | rwlock_unlock (scn->elf->lock); | 281 | | | 282 | 484k | return result; | 283 | 484k | } |
Line | Count | Source | 272 | 5.81k | { | 273 | 5.81k | ElfW2(LIBELFBITS,Shdr) *result; | 274 | | | 275 | 5.81k | if (!scn_valid (scn)) | 276 | 0 | return NULL; | 277 | | | 278 | 5.81k | rwlock_rdlock (scn->elf->lock); | 279 | 5.81k | result = __elfw2(LIBELFBITS,getshdr_rdlock) (scn); | 280 | 5.81k | rwlock_unlock (scn->elf->lock); | 281 | | | 282 | 5.81k | return result; | 283 | 5.81k | } |
|