Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1 | /* Create descriptor for processing file. |
Jakub Jelinek | 720383c | 2014-01-17 19:36:16 +0100 | [diff] [blame] | 2 | Copyright (C) 1998-2010, 2012, 2014 Red Hat, Inc. |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 3 | This file is part of elfutils. |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 4 | Written by Ulrich Drepper <drepper@redhat.com>, 1998. |
| 5 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 6 | This file is free software; you can redistribute it and/or modify |
| 7 | it under the terms of either |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 8 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 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 |
Ulrich Drepper | 361df7d | 2006-04-04 21:38:57 +0000 | [diff] [blame] | 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. |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 25 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 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/>. */ |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 29 | |
| 30 | #ifdef HAVE_CONFIG_H |
| 31 | # include <config.h> |
| 32 | #endif |
| 33 | |
| 34 | #include <assert.h> |
| 35 | #include <ctype.h> |
| 36 | #include <errno.h> |
| 37 | #include <fcntl.h> |
| 38 | #include <stdbool.h> |
| 39 | #include <stddef.h> |
| 40 | #include <string.h> |
| 41 | #include <unistd.h> |
| 42 | #include <sys/mman.h> |
| 43 | #include <sys/param.h> |
| 44 | #include <sys/stat.h> |
| 45 | |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 46 | #include <system.h> |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 47 | #include "libelfP.h" |
| 48 | #include "common.h" |
| 49 | |
| 50 | |
| 51 | /* Create descriptor for archive in memory. */ |
| 52 | static inline Elf * |
| 53 | file_read_ar (int fildes, void *map_address, off_t offset, size_t maxsize, |
| 54 | Elf_Cmd cmd, Elf *parent) |
| 55 | { |
| 56 | Elf *elf; |
| 57 | |
| 58 | /* Create a descriptor. */ |
| 59 | elf = allocate_elf (fildes, map_address, offset, maxsize, cmd, parent, |
| 60 | ELF_K_AR, 0); |
| 61 | if (elf != NULL) |
| 62 | { |
| 63 | /* We don't read all the symbol tables in advance. All this will |
| 64 | happen on demand. */ |
| 65 | elf->state.ar.offset = offset + SARMAG; |
| 66 | |
| 67 | elf->state.ar.elf_ar_hdr.ar_rawname = elf->state.ar.raw_name; |
| 68 | } |
| 69 | |
| 70 | return elf; |
| 71 | } |
| 72 | |
| 73 | |
| 74 | static size_t |
| 75 | get_shnum (void *map_address, unsigned char *e_ident, int fildes, off_t offset, |
| 76 | size_t maxsize) |
| 77 | { |
| 78 | size_t result; |
| 79 | union |
| 80 | { |
| 81 | Elf32_Ehdr *e32; |
| 82 | Elf64_Ehdr *e64; |
| 83 | void *p; |
| 84 | } ehdr; |
Ulrich Drepper | 0f6abbb | 2005-08-13 22:35:15 +0000 | [diff] [blame] | 85 | union |
| 86 | { |
| 87 | Elf32_Ehdr e32; |
| 88 | Elf64_Ehdr e64; |
| 89 | } ehdr_mem; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 90 | bool is32 = e_ident[EI_CLASS] == ELFCLASS32; |
| 91 | |
| 92 | /* Make the ELF header available. */ |
Roland McGrath | 9cf28e4 | 2008-09-30 06:35:35 +0000 | [diff] [blame] | 93 | if (e_ident[EI_DATA] == MY_ELFDATA |
| 94 | && (ALLOW_UNALIGNED |
| 95 | || (((size_t) e_ident |
| 96 | & ((is32 ? __alignof__ (Elf32_Ehdr) : __alignof__ (Elf64_Ehdr)) |
| 97 | - 1)) == 0))) |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 98 | ehdr.p = e_ident; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 99 | else |
| 100 | { |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 101 | /* We already read the ELF header. We have to copy the header |
| 102 | since we possibly modify the data here and the caller |
| 103 | expects the memory it passes in to be preserved. */ |
| 104 | ehdr.p = &ehdr_mem; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 105 | |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 106 | if (is32) |
Ulrich Drepper | 0f6abbb | 2005-08-13 22:35:15 +0000 | [diff] [blame] | 107 | { |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 108 | if (ALLOW_UNALIGNED) |
Ulrich Drepper | 969bda4 | 2005-08-28 08:30:08 +0000 | [diff] [blame] | 109 | { |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 110 | ehdr_mem.e32.e_shnum = ((Elf32_Ehdr *) e_ident)->e_shnum; |
| 111 | ehdr_mem.e32.e_shoff = ((Elf32_Ehdr *) e_ident)->e_shoff; |
Ulrich Drepper | 969bda4 | 2005-08-28 08:30:08 +0000 | [diff] [blame] | 112 | } |
| 113 | else |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 114 | memcpy (&ehdr_mem, e_ident, sizeof (Elf32_Ehdr)); |
| 115 | |
Roland McGrath | 9cf28e4 | 2008-09-30 06:35:35 +0000 | [diff] [blame] | 116 | if (e_ident[EI_DATA] != MY_ELFDATA) |
| 117 | { |
| 118 | CONVERT (ehdr_mem.e32.e_shnum); |
| 119 | CONVERT (ehdr_mem.e32.e_shoff); |
| 120 | } |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 121 | } |
| 122 | else |
| 123 | { |
| 124 | if (ALLOW_UNALIGNED) |
Roland McGrath | 4c305da | 2005-08-25 01:49:35 +0000 | [diff] [blame] | 125 | { |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 126 | ehdr_mem.e64.e_shnum = ((Elf64_Ehdr *) e_ident)->e_shnum; |
| 127 | ehdr_mem.e64.e_shoff = ((Elf64_Ehdr *) e_ident)->e_shoff; |
Roland McGrath | 4c305da | 2005-08-25 01:49:35 +0000 | [diff] [blame] | 128 | } |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 129 | else |
| 130 | memcpy (&ehdr_mem, e_ident, sizeof (Elf64_Ehdr)); |
| 131 | |
Roland McGrath | 9cf28e4 | 2008-09-30 06:35:35 +0000 | [diff] [blame] | 132 | if (e_ident[EI_DATA] != MY_ELFDATA) |
| 133 | { |
| 134 | CONVERT (ehdr_mem.e64.e_shnum); |
| 135 | CONVERT (ehdr_mem.e64.e_shoff); |
| 136 | } |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
| 140 | if (is32) |
| 141 | { |
| 142 | /* Get the number of sections from the ELF header. */ |
| 143 | result = ehdr.e32->e_shnum; |
| 144 | |
| 145 | if (unlikely (result == 0) && ehdr.e32->e_shoff != 0) |
| 146 | { |
Jakub Jelinek | 720383c | 2014-01-17 19:36:16 +0100 | [diff] [blame] | 147 | if (unlikely (ehdr.e32->e_shoff >= maxsize) |
| 148 | || unlikely (maxsize - ehdr.e32->e_shoff < sizeof (Elf32_Shdr))) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 149 | /* Cannot read the first section header. */ |
Ulrich Drepper | c07fbb3 | 2007-03-30 19:14:59 +0000 | [diff] [blame] | 150 | return 0; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 151 | |
| 152 | if (likely (map_address != NULL) && e_ident[EI_DATA] == MY_ELFDATA |
| 153 | && (ALLOW_UNALIGNED |
| 154 | || (((size_t) ((char *) map_address + offset)) |
| 155 | & (__alignof__ (Elf32_Ehdr) - 1)) == 0)) |
| 156 | /* We can directly access the memory. */ |
Ulrich Drepper | 0f6abbb | 2005-08-13 22:35:15 +0000 | [diff] [blame] | 157 | result = ((Elf32_Shdr *) ((char *) map_address + ehdr.e32->e_shoff |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 158 | + offset))->sh_size; |
| 159 | else |
| 160 | { |
| 161 | Elf32_Word size; |
| 162 | |
Ulrich Drepper | 0f6abbb | 2005-08-13 22:35:15 +0000 | [diff] [blame] | 163 | if (likely (map_address != NULL)) |
| 164 | /* gcc will optimize the memcpy to a simple memory |
| 165 | access while taking care of alignment issues. */ |
| 166 | memcpy (&size, &((Elf32_Shdr *) ((char *) map_address |
| 167 | + ehdr.e32->e_shoff |
| 168 | + offset))->sh_size, |
| 169 | sizeof (Elf32_Word)); |
| 170 | else |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 171 | if (unlikely (pread_retry (fildes, &size, sizeof (Elf32_Word), |
| 172 | offset + ehdr.e32->e_shoff |
| 173 | + offsetof (Elf32_Shdr, sh_size)) |
| 174 | != sizeof (Elf32_Word))) |
Ulrich Drepper | 0f6abbb | 2005-08-13 22:35:15 +0000 | [diff] [blame] | 175 | return (size_t) -1l; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 176 | |
| 177 | if (e_ident[EI_DATA] != MY_ELFDATA) |
| 178 | CONVERT (size); |
| 179 | |
| 180 | result = size; |
| 181 | } |
| 182 | } |
Ulrich Drepper | c07fbb3 | 2007-03-30 19:14:59 +0000 | [diff] [blame] | 183 | |
| 184 | /* If the section headers were truncated, pretend none were there. */ |
| 185 | if (ehdr.e32->e_shoff > maxsize |
| 186 | || maxsize - ehdr.e32->e_shoff < sizeof (Elf32_Shdr) * result) |
| 187 | result = 0; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 188 | } |
| 189 | else |
| 190 | { |
| 191 | /* Get the number of sections from the ELF header. */ |
| 192 | result = ehdr.e64->e_shnum; |
| 193 | |
| 194 | if (unlikely (result == 0) && ehdr.e64->e_shoff != 0) |
| 195 | { |
Jakub Jelinek | 720383c | 2014-01-17 19:36:16 +0100 | [diff] [blame] | 196 | if (unlikely (ehdr.e64->e_shoff >= maxsize) |
| 197 | || unlikely (ehdr.e64->e_shoff + sizeof (Elf64_Shdr) > maxsize)) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 198 | /* Cannot read the first section header. */ |
Ulrich Drepper | c07fbb3 | 2007-03-30 19:14:59 +0000 | [diff] [blame] | 199 | return 0; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 200 | |
Ulrich Drepper | 0f6abbb | 2005-08-13 22:35:15 +0000 | [diff] [blame] | 201 | Elf64_Xword size; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 202 | if (likely (map_address != NULL) && e_ident[EI_DATA] == MY_ELFDATA |
| 203 | && (ALLOW_UNALIGNED |
| 204 | || (((size_t) ((char *) map_address + offset)) |
| 205 | & (__alignof__ (Elf64_Ehdr) - 1)) == 0)) |
| 206 | /* We can directly access the memory. */ |
Ulrich Drepper | 0f6abbb | 2005-08-13 22:35:15 +0000 | [diff] [blame] | 207 | size = ((Elf64_Shdr *) ((char *) map_address + ehdr.e64->e_shoff |
| 208 | + offset))->sh_size; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 209 | else |
| 210 | { |
Ulrich Drepper | 0f6abbb | 2005-08-13 22:35:15 +0000 | [diff] [blame] | 211 | if (likely (map_address != NULL)) |
| 212 | /* gcc will optimize the memcpy to a simple memory |
| 213 | access while taking care of alignment issues. */ |
| 214 | memcpy (&size, &((Elf64_Shdr *) ((char *) map_address |
| 215 | + ehdr.e64->e_shoff |
| 216 | + offset))->sh_size, |
| 217 | sizeof (Elf64_Xword)); |
| 218 | else |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 219 | if (unlikely (pread_retry (fildes, &size, sizeof (Elf64_Word), |
| 220 | offset + ehdr.e64->e_shoff |
| 221 | + offsetof (Elf64_Shdr, sh_size)) |
| 222 | != sizeof (Elf64_Xword))) |
Ulrich Drepper | 0f6abbb | 2005-08-13 22:35:15 +0000 | [diff] [blame] | 223 | return (size_t) -1l; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 224 | |
| 225 | if (e_ident[EI_DATA] != MY_ELFDATA) |
| 226 | CONVERT (size); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 227 | } |
Ulrich Drepper | 0f6abbb | 2005-08-13 22:35:15 +0000 | [diff] [blame] | 228 | |
| 229 | if (size > ~((GElf_Word) 0)) |
| 230 | /* Invalid value, it is too large. */ |
| 231 | return (size_t) -1l; |
| 232 | |
| 233 | result = size; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 234 | } |
Ulrich Drepper | c07fbb3 | 2007-03-30 19:14:59 +0000 | [diff] [blame] | 235 | |
| 236 | /* If the section headers were truncated, pretend none were there. */ |
| 237 | if (ehdr.e64->e_shoff > maxsize |
| 238 | || maxsize - ehdr.e64->e_shoff < sizeof (Elf64_Shdr) * result) |
| 239 | result = 0; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | return result; |
| 243 | } |
| 244 | |
| 245 | |
| 246 | /* Create descriptor for ELF file in memory. */ |
| 247 | static Elf * |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 248 | file_read_elf (int fildes, void *map_address, unsigned char *e_ident, |
| 249 | off_t offset, size_t maxsize, Elf_Cmd cmd, Elf *parent) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 250 | { |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 251 | /* Verify the binary is of the class we can handle. */ |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 252 | if (unlikely ((e_ident[EI_CLASS] != ELFCLASS32 |
| 253 | && e_ident[EI_CLASS] != ELFCLASS64) |
| 254 | /* We also can only handle two encodings. */ |
| 255 | || (e_ident[EI_DATA] != ELFDATA2LSB |
| 256 | && e_ident[EI_DATA] != ELFDATA2MSB))) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 257 | { |
| 258 | /* Cannot handle this. */ |
| 259 | __libelf_seterrno (ELF_E_INVALID_FILE); |
| 260 | return NULL; |
| 261 | } |
| 262 | |
| 263 | /* Determine the number of sections. */ |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 264 | size_t scncnt = get_shnum (map_address, e_ident, fildes, offset, maxsize); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 265 | if (scncnt == (size_t) -1l) |
| 266 | /* Could not determine the number of sections. */ |
| 267 | return NULL; |
| 268 | |
Jakub Jelinek | 720383c | 2014-01-17 19:36:16 +0100 | [diff] [blame] | 269 | /* Check for too many sections. */ |
| 270 | if (e_ident[EI_CLASS] == ELFCLASS32) |
| 271 | { |
| 272 | if (scncnt > SIZE_MAX / (sizeof (Elf_Scn) + sizeof (Elf32_Shdr))) |
| 273 | return NULL; |
| 274 | } |
| 275 | else if (scncnt > SIZE_MAX / (sizeof (Elf_Scn) + sizeof (Elf64_Shdr))) |
| 276 | return NULL; |
| 277 | |
Roland McGrath | 6fd3cd1 | 2010-01-07 19:41:04 -0800 | [diff] [blame] | 278 | /* We can now allocate the memory. Even if there are no section headers, |
| 279 | we allocate space for a zeroth section in case we need it later. */ |
| 280 | const size_t scnmax = (scncnt ?: (cmd == ELF_C_RDWR || cmd == ELF_C_RDWR_MMAP) |
| 281 | ? 1 : 0); |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 282 | Elf *elf = allocate_elf (fildes, map_address, offset, maxsize, cmd, parent, |
Roland McGrath | 6fd3cd1 | 2010-01-07 19:41:04 -0800 | [diff] [blame] | 283 | ELF_K_ELF, scnmax * sizeof (Elf_Scn)); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 284 | if (elf == NULL) |
| 285 | /* Not enough memory. */ |
| 286 | return NULL; |
| 287 | |
Roland McGrath | 6fd3cd1 | 2010-01-07 19:41:04 -0800 | [diff] [blame] | 288 | assert ((unsigned int) scncnt == scncnt); |
| 289 | assert (offsetof (struct Elf, state.elf32.scns) |
| 290 | == offsetof (struct Elf, state.elf64.scns)); |
| 291 | elf->state.elf32.scns.cnt = scncnt; |
| 292 | elf->state.elf32.scns.max = scnmax; |
| 293 | |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 294 | /* Some more or less arbitrary value. */ |
| 295 | elf->state.elf.scnincr = 10; |
| 296 | |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 297 | /* Make the class easily available. */ |
| 298 | elf->class = e_ident[EI_CLASS]; |
| 299 | |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 300 | if (e_ident[EI_CLASS] == ELFCLASS32) |
| 301 | { |
| 302 | /* This pointer might not be directly usable if the alignment is |
| 303 | not sufficient for the architecture. */ |
| 304 | Elf32_Ehdr *ehdr = (Elf32_Ehdr *) ((char *) map_address + offset); |
| 305 | |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 306 | /* This is a 32-bit binary. */ |
| 307 | if (map_address != NULL && e_ident[EI_DATA] == MY_ELFDATA |
| 308 | && (ALLOW_UNALIGNED |
| 309 | || ((((uintptr_t) ehdr) & (__alignof__ (Elf32_Ehdr) - 1)) == 0 |
| 310 | && ((uintptr_t) ((char *) ehdr + ehdr->e_shoff) |
| 311 | & (__alignof__ (Elf32_Shdr) - 1)) == 0 |
| 312 | && ((uintptr_t) ((char *) ehdr + ehdr->e_phoff) |
| 313 | & (__alignof__ (Elf32_Phdr) - 1)) == 0))) |
| 314 | { |
| 315 | /* We can use the mmapped memory. */ |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 316 | elf->state.elf32.ehdr = ehdr; |
Jakub Jelinek | 720383c | 2014-01-17 19:36:16 +0100 | [diff] [blame] | 317 | |
| 318 | if (unlikely (ehdr->e_shoff >= maxsize) |
| 319 | || unlikely (maxsize - ehdr->e_shoff |
| 320 | < scncnt * sizeof (Elf32_Shdr))) |
| 321 | { |
| 322 | free_and_out: |
| 323 | free (elf); |
| 324 | __libelf_seterrno (ELF_E_INVALID_FILE); |
| 325 | return NULL; |
| 326 | } |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 327 | elf->state.elf32.shdr |
| 328 | = (Elf32_Shdr *) ((char *) ehdr + ehdr->e_shoff); |
Roland McGrath | 957072c | 2010-04-14 11:24:15 -0700 | [diff] [blame] | 329 | |
| 330 | /* Don't precache the phdr pointer here. |
| 331 | elf32_getphdr will validate it against the size when asked. */ |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 332 | |
| 333 | for (size_t cnt = 0; cnt < scncnt; ++cnt) |
| 334 | { |
| 335 | elf->state.elf32.scns.data[cnt].index = cnt; |
| 336 | elf->state.elf32.scns.data[cnt].elf = elf; |
| 337 | elf->state.elf32.scns.data[cnt].shdr.e32 = |
| 338 | &elf->state.elf32.shdr[cnt]; |
Roland McGrath | 429502f | 2010-02-17 00:49:46 -0800 | [diff] [blame] | 339 | if (likely (elf->state.elf32.shdr[cnt].sh_offset < maxsize) |
Mark Wielaard | b635b8d | 2014-11-07 12:47:16 +0100 | [diff] [blame] | 340 | && likely (elf->state.elf32.shdr[cnt].sh_size |
| 341 | <= maxsize - elf->state.elf32.shdr[cnt].sh_offset)) |
Roland McGrath | 429502f | 2010-02-17 00:49:46 -0800 | [diff] [blame] | 342 | elf->state.elf32.scns.data[cnt].rawdata_base = |
| 343 | elf->state.elf32.scns.data[cnt].data_base = |
| 344 | ((char *) map_address + offset |
| 345 | + elf->state.elf32.shdr[cnt].sh_offset); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 346 | elf->state.elf32.scns.data[cnt].list = &elf->state.elf32.scns; |
Ulrich Drepper | 3cbdd38 | 2008-01-02 17:44:39 +0000 | [diff] [blame] | 347 | |
| 348 | /* If this is a section with an extended index add a |
| 349 | reference in the section which uses the extended |
| 350 | index. */ |
| 351 | if (elf->state.elf32.shdr[cnt].sh_type == SHT_SYMTAB_SHNDX |
| 352 | && elf->state.elf32.shdr[cnt].sh_link < scncnt) |
| 353 | elf->state.elf32.scns.data[elf->state.elf32.shdr[cnt].sh_link].shndx_index |
| 354 | = cnt; |
| 355 | |
| 356 | /* Set the own shndx_index field in case it has not yet |
| 357 | been set. */ |
| 358 | if (elf->state.elf32.scns.data[cnt].shndx_index == 0) |
| 359 | elf->state.elf32.scns.data[cnt].shndx_index = -1; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | else |
| 363 | { |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 364 | /* Copy the ELF header. */ |
| 365 | elf->state.elf32.ehdr = memcpy (&elf->state.elf32.ehdr_mem, e_ident, |
| 366 | sizeof (Elf32_Ehdr)); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 367 | |
| 368 | if (e_ident[EI_DATA] != MY_ELFDATA) |
| 369 | { |
| 370 | CONVERT (elf->state.elf32.ehdr_mem.e_type); |
| 371 | CONVERT (elf->state.elf32.ehdr_mem.e_machine); |
| 372 | CONVERT (elf->state.elf32.ehdr_mem.e_version); |
| 373 | CONVERT (elf->state.elf32.ehdr_mem.e_entry); |
| 374 | CONVERT (elf->state.elf32.ehdr_mem.e_phoff); |
| 375 | CONVERT (elf->state.elf32.ehdr_mem.e_shoff); |
| 376 | CONVERT (elf->state.elf32.ehdr_mem.e_flags); |
| 377 | CONVERT (elf->state.elf32.ehdr_mem.e_ehsize); |
| 378 | CONVERT (elf->state.elf32.ehdr_mem.e_phentsize); |
| 379 | CONVERT (elf->state.elf32.ehdr_mem.e_phnum); |
| 380 | CONVERT (elf->state.elf32.ehdr_mem.e_shentsize); |
| 381 | CONVERT (elf->state.elf32.ehdr_mem.e_shnum); |
| 382 | CONVERT (elf->state.elf32.ehdr_mem.e_shstrndx); |
| 383 | } |
| 384 | |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 385 | for (size_t cnt = 0; cnt < scncnt; ++cnt) |
| 386 | { |
| 387 | elf->state.elf32.scns.data[cnt].index = cnt; |
| 388 | elf->state.elf32.scns.data[cnt].elf = elf; |
| 389 | elf->state.elf32.scns.data[cnt].list = &elf->state.elf32.scns; |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | /* So far only one block with sections. */ |
| 394 | elf->state.elf32.scns_last = &elf->state.elf32.scns; |
| 395 | } |
| 396 | else |
| 397 | { |
| 398 | /* This pointer might not be directly usable if the alignment is |
| 399 | not sufficient for the architecture. */ |
| 400 | Elf64_Ehdr *ehdr = (Elf64_Ehdr *) ((char *) map_address + offset); |
| 401 | |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 402 | /* This is a 64-bit binary. */ |
| 403 | if (map_address != NULL && e_ident[EI_DATA] == MY_ELFDATA |
| 404 | && (ALLOW_UNALIGNED |
| 405 | || ((((uintptr_t) ehdr) & (__alignof__ (Elf64_Ehdr) - 1)) == 0 |
| 406 | && ((uintptr_t) ((char *) ehdr + ehdr->e_shoff) |
| 407 | & (__alignof__ (Elf64_Shdr) - 1)) == 0 |
| 408 | && ((uintptr_t) ((char *) ehdr + ehdr->e_phoff) |
| 409 | & (__alignof__ (Elf64_Phdr) - 1)) == 0))) |
| 410 | { |
| 411 | /* We can use the mmapped memory. */ |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 412 | elf->state.elf64.ehdr = ehdr; |
Jakub Jelinek | 720383c | 2014-01-17 19:36:16 +0100 | [diff] [blame] | 413 | |
| 414 | if (unlikely (ehdr->e_shoff >= maxsize) |
| 415 | || unlikely (ehdr->e_shoff |
| 416 | + scncnt * sizeof (Elf32_Shdr) > maxsize)) |
| 417 | goto free_and_out; |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 418 | elf->state.elf64.shdr |
| 419 | = (Elf64_Shdr *) ((char *) ehdr + ehdr->e_shoff); |
Roland McGrath | 957072c | 2010-04-14 11:24:15 -0700 | [diff] [blame] | 420 | |
| 421 | /* Don't precache the phdr pointer here. |
| 422 | elf64_getphdr will validate it against the size when asked. */ |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 423 | |
| 424 | for (size_t cnt = 0; cnt < scncnt; ++cnt) |
| 425 | { |
| 426 | elf->state.elf64.scns.data[cnt].index = cnt; |
| 427 | elf->state.elf64.scns.data[cnt].elf = elf; |
| 428 | elf->state.elf64.scns.data[cnt].shdr.e64 = |
| 429 | &elf->state.elf64.shdr[cnt]; |
Roland McGrath | 429502f | 2010-02-17 00:49:46 -0800 | [diff] [blame] | 430 | if (likely (elf->state.elf64.shdr[cnt].sh_offset < maxsize) |
Mark Wielaard | b635b8d | 2014-11-07 12:47:16 +0100 | [diff] [blame] | 431 | && likely (elf->state.elf64.shdr[cnt].sh_size |
| 432 | <= maxsize - elf->state.elf64.shdr[cnt].sh_offset)) |
Roland McGrath | 429502f | 2010-02-17 00:49:46 -0800 | [diff] [blame] | 433 | elf->state.elf64.scns.data[cnt].rawdata_base = |
| 434 | elf->state.elf64.scns.data[cnt].data_base = |
| 435 | ((char *) map_address + offset |
| 436 | + elf->state.elf64.shdr[cnt].sh_offset); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 437 | elf->state.elf64.scns.data[cnt].list = &elf->state.elf64.scns; |
Ulrich Drepper | 3cbdd38 | 2008-01-02 17:44:39 +0000 | [diff] [blame] | 438 | |
| 439 | /* If this is a section with an extended index add a |
| 440 | reference in the section which uses the extended |
| 441 | index. */ |
| 442 | if (elf->state.elf64.shdr[cnt].sh_type == SHT_SYMTAB_SHNDX |
| 443 | && elf->state.elf64.shdr[cnt].sh_link < scncnt) |
| 444 | elf->state.elf64.scns.data[elf->state.elf64.shdr[cnt].sh_link].shndx_index |
| 445 | = cnt; |
| 446 | |
| 447 | /* Set the own shndx_index field in case it has not yet |
| 448 | been set. */ |
| 449 | if (elf->state.elf64.scns.data[cnt].shndx_index == 0) |
| 450 | elf->state.elf64.scns.data[cnt].shndx_index = -1; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 451 | } |
| 452 | } |
| 453 | else |
| 454 | { |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 455 | /* Copy the ELF header. */ |
| 456 | elf->state.elf64.ehdr = memcpy (&elf->state.elf64.ehdr_mem, e_ident, |
| 457 | sizeof (Elf64_Ehdr)); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 458 | |
| 459 | if (e_ident[EI_DATA] != MY_ELFDATA) |
| 460 | { |
| 461 | CONVERT (elf->state.elf64.ehdr_mem.e_type); |
| 462 | CONVERT (elf->state.elf64.ehdr_mem.e_machine); |
| 463 | CONVERT (elf->state.elf64.ehdr_mem.e_version); |
| 464 | CONVERT (elf->state.elf64.ehdr_mem.e_entry); |
| 465 | CONVERT (elf->state.elf64.ehdr_mem.e_phoff); |
| 466 | CONVERT (elf->state.elf64.ehdr_mem.e_shoff); |
| 467 | CONVERT (elf->state.elf64.ehdr_mem.e_flags); |
| 468 | CONVERT (elf->state.elf64.ehdr_mem.e_ehsize); |
| 469 | CONVERT (elf->state.elf64.ehdr_mem.e_phentsize); |
| 470 | CONVERT (elf->state.elf64.ehdr_mem.e_phnum); |
| 471 | CONVERT (elf->state.elf64.ehdr_mem.e_shentsize); |
| 472 | CONVERT (elf->state.elf64.ehdr_mem.e_shnum); |
| 473 | CONVERT (elf->state.elf64.ehdr_mem.e_shstrndx); |
| 474 | } |
| 475 | |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 476 | for (size_t cnt = 0; cnt < scncnt; ++cnt) |
| 477 | { |
| 478 | elf->state.elf64.scns.data[cnt].index = cnt; |
| 479 | elf->state.elf64.scns.data[cnt].elf = elf; |
| 480 | elf->state.elf64.scns.data[cnt].list = &elf->state.elf64.scns; |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | /* So far only one block with sections. */ |
| 485 | elf->state.elf64.scns_last = &elf->state.elf64.scns; |
| 486 | } |
| 487 | |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 488 | return elf; |
| 489 | } |
| 490 | |
| 491 | |
| 492 | Elf * |
Ulrich Drepper | 077c65f | 2006-07-12 19:54:51 +0000 | [diff] [blame] | 493 | internal_function |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 494 | __libelf_read_mmaped_file (int fildes, void *map_address, off_t offset, |
| 495 | size_t maxsize, Elf_Cmd cmd, Elf *parent) |
| 496 | { |
| 497 | /* We have to find out what kind of file this is. We handle ELF |
| 498 | files and archives. To find out what we have we must look at the |
| 499 | header. The header for an ELF file is EI_NIDENT bytes in size, |
| 500 | the header for an archive file SARMAG bytes long. */ |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 501 | unsigned char *e_ident = (unsigned char *) map_address + offset; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 502 | |
| 503 | /* See what kind of object we have here. */ |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 504 | Elf_Kind kind = determine_kind (e_ident, maxsize); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 505 | |
| 506 | switch (kind) |
| 507 | { |
| 508 | case ELF_K_ELF: |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 509 | return file_read_elf (fildes, map_address, e_ident, offset, maxsize, |
| 510 | cmd, parent); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 511 | |
| 512 | case ELF_K_AR: |
| 513 | return file_read_ar (fildes, map_address, offset, maxsize, cmd, parent); |
| 514 | |
| 515 | default: |
| 516 | break; |
| 517 | } |
| 518 | |
| 519 | /* This case is easy. Since we cannot do anything with this file |
| 520 | create a dummy descriptor. */ |
| 521 | return allocate_elf (fildes, map_address, offset, maxsize, cmd, parent, |
| 522 | ELF_K_NONE, 0); |
| 523 | } |
| 524 | |
| 525 | |
| 526 | static Elf * |
| 527 | read_unmmaped_file (int fildes, off_t offset, size_t maxsize, Elf_Cmd cmd, |
| 528 | Elf *parent) |
| 529 | { |
| 530 | /* We have to find out what kind of file this is. We handle ELF |
| 531 | files and archives. To find out what we have we must read the |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 532 | header. The identification header for an ELF file is EI_NIDENT |
| 533 | bytes in size, but we read the whole ELF header since we will |
| 534 | need it anyway later. For archives the header in SARMAG bytes |
| 535 | long. Read the maximum of these numbers. |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 536 | |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 537 | XXX We have to change this for the extended `ar' format some day. |
| 538 | |
| 539 | Use a union to ensure alignment. We might later access the |
| 540 | memory as a ElfXX_Ehdr. */ |
| 541 | union |
| 542 | { |
| 543 | Elf64_Ehdr ehdr; |
| 544 | unsigned char header[MAX (sizeof (Elf64_Ehdr), SARMAG)]; |
| 545 | } mem; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 546 | |
| 547 | /* Read the head of the file. */ |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 548 | ssize_t nread = pread_retry (fildes, mem.header, |
| 549 | MIN (MAX (sizeof (Elf64_Ehdr), SARMAG), |
| 550 | maxsize), |
| 551 | offset); |
| 552 | if (unlikely (nread == -1)) |
Petr Machata | 237188e | 2014-09-10 22:25:35 +0200 | [diff] [blame] | 553 | { |
| 554 | /* We cannot even read the head of the file. Maybe FILDES is associated |
| 555 | with an unseekable device. This is nothing we can handle. */ |
| 556 | __libelf_seterrno (ELF_E_INVALID_FILE); |
| 557 | return NULL; |
| 558 | } |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 559 | |
| 560 | /* See what kind of object we have here. */ |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 561 | Elf_Kind kind = determine_kind (mem.header, nread); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 562 | |
| 563 | switch (kind) |
| 564 | { |
| 565 | case ELF_K_AR: |
| 566 | return file_read_ar (fildes, NULL, offset, maxsize, cmd, parent); |
| 567 | |
| 568 | case ELF_K_ELF: |
| 569 | /* Make sure at least the ELF header is contained in the file. */ |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 570 | if ((size_t) nread >= (mem.header[EI_CLASS] == ELFCLASS32 |
| 571 | ? sizeof (Elf32_Ehdr) : sizeof (Elf64_Ehdr))) |
| 572 | return file_read_elf (fildes, NULL, mem.header, offset, maxsize, cmd, |
| 573 | parent); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 574 | /* FALLTHROUGH */ |
| 575 | |
| 576 | default: |
| 577 | break; |
| 578 | } |
| 579 | |
| 580 | /* This case is easy. Since we cannot do anything with this file |
| 581 | create a dummy descriptor. */ |
| 582 | return allocate_elf (fildes, NULL, offset, maxsize, cmd, parent, |
| 583 | ELF_K_NONE, 0); |
| 584 | } |
| 585 | |
| 586 | |
| 587 | /* Open a file for reading. If possible we will try to mmap() the file. */ |
| 588 | static struct Elf * |
| 589 | read_file (int fildes, off_t offset, size_t maxsize, |
| 590 | Elf_Cmd cmd, Elf *parent) |
| 591 | { |
| 592 | void *map_address = NULL; |
| 593 | int use_mmap = (cmd == ELF_C_READ_MMAP || cmd == ELF_C_RDWR_MMAP |
| 594 | || cmd == ELF_C_WRITE_MMAP |
| 595 | || cmd == ELF_C_READ_MMAP_PRIVATE); |
| 596 | |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 597 | if (use_mmap) |
| 598 | { |
| 599 | if (parent == NULL) |
| 600 | { |
| 601 | if (maxsize == ~((size_t) 0)) |
| 602 | { |
| 603 | /* We don't know in the moment how large the file is. |
| 604 | Determine it now. */ |
| 605 | struct stat st; |
| 606 | |
| 607 | if (fstat (fildes, &st) == 0 |
| 608 | && (sizeof (size_t) >= sizeof (st.st_size) |
| 609 | || st.st_size <= ~((size_t) 0))) |
| 610 | maxsize = (size_t) st.st_size; |
| 611 | } |
| 612 | |
| 613 | /* We try to map the file ourself. */ |
| 614 | map_address = mmap (NULL, maxsize, (cmd == ELF_C_READ_MMAP |
| 615 | ? PROT_READ |
| 616 | : PROT_READ|PROT_WRITE), |
| 617 | cmd == ELF_C_READ_MMAP_PRIVATE |
Ulrich Drepper | ce0bdb6 | 2007-02-05 07:13:52 +0000 | [diff] [blame] | 618 | || cmd == ELF_C_READ_MMAP |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 619 | ? MAP_PRIVATE : MAP_SHARED, |
| 620 | fildes, offset); |
| 621 | |
| 622 | if (map_address == MAP_FAILED) |
| 623 | map_address = NULL; |
| 624 | } |
| 625 | else |
| 626 | { |
| 627 | /* The parent is already loaded. Use it. */ |
| 628 | assert (maxsize != ~((size_t) 0)); |
| 629 | |
| 630 | map_address = parent->map_address; |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | /* If we have the file in memory optimize the access. */ |
| 635 | if (map_address != NULL) |
| 636 | { |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 637 | assert (map_address != MAP_FAILED); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 638 | |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 639 | struct Elf *result = __libelf_read_mmaped_file (fildes, map_address, |
| 640 | offset, maxsize, cmd, |
| 641 | parent); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 642 | |
| 643 | /* If something went wrong during the initialization unmap the |
| 644 | memory if we mmaped here. */ |
| 645 | if (result == NULL |
| 646 | && (parent == NULL |
| 647 | || parent->map_address != map_address)) |
| 648 | munmap (map_address, maxsize); |
| 649 | else if (parent == NULL) |
| 650 | /* Remember that we mmap()ed the memory. */ |
| 651 | result->flags |= ELF_F_MMAPPED; |
| 652 | |
| 653 | return result; |
| 654 | } |
| 655 | |
| 656 | /* Otherwise we have to do it the hard way. We read as much as necessary |
| 657 | from the file whenever we need information which is not available. */ |
| 658 | return read_unmmaped_file (fildes, offset, maxsize, cmd, parent); |
| 659 | } |
| 660 | |
| 661 | |
| 662 | /* Find the entry with the long names for the content of this archive. */ |
| 663 | static const char * |
| 664 | read_long_names (Elf *elf) |
| 665 | { |
| 666 | off_t offset = SARMAG; /* This is the first entry. */ |
| 667 | struct ar_hdr hdrm; |
| 668 | struct ar_hdr *hdr; |
| 669 | char *newp; |
| 670 | size_t len; |
| 671 | |
| 672 | while (1) |
| 673 | { |
| 674 | if (elf->map_address != NULL) |
| 675 | { |
| 676 | if (offset + sizeof (struct ar_hdr) > elf->maximum_size) |
| 677 | return NULL; |
| 678 | |
| 679 | /* The data is mapped. */ |
| 680 | hdr = (struct ar_hdr *) (elf->map_address + offset); |
| 681 | } |
| 682 | else |
| 683 | { |
| 684 | /* Read the header from the file. */ |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 685 | if (unlikely (pread_retry (elf->fildes, &hdrm, sizeof (hdrm), |
| 686 | elf->start_offset + offset) |
| 687 | != sizeof (hdrm))) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 688 | return NULL; |
| 689 | |
| 690 | hdr = &hdrm; |
| 691 | } |
| 692 | |
| 693 | len = atol (hdr->ar_size); |
| 694 | |
| 695 | if (memcmp (hdr->ar_name, "// ", 16) == 0) |
| 696 | break; |
| 697 | |
| 698 | offset += sizeof (struct ar_hdr) + ((len + 1) & ~1l); |
| 699 | } |
| 700 | |
| 701 | /* Due to the stupid format of the long name table entry (which are not |
| 702 | NUL terminted) we have to provide an appropriate representation anyhow. |
| 703 | Therefore we always make a copy which has the appropriate form. */ |
| 704 | newp = (char *) malloc (len); |
| 705 | if (newp != NULL) |
| 706 | { |
| 707 | char *runp; |
| 708 | |
| 709 | if (elf->map_address != NULL) |
| 710 | /* Simply copy it over. */ |
| 711 | elf->state.ar.long_names = (char *) memcpy (newp, |
| 712 | elf->map_address + offset |
| 713 | + sizeof (struct ar_hdr), |
| 714 | len); |
| 715 | else |
| 716 | { |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 717 | if (unlikely ((size_t) pread_retry (elf->fildes, newp, len, |
| 718 | elf->start_offset + offset |
| 719 | + sizeof (struct ar_hdr)) |
| 720 | != len)) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 721 | { |
| 722 | /* We were not able to read all data. */ |
| 723 | free (newp); |
| 724 | elf->state.ar.long_names = NULL; |
| 725 | return NULL; |
| 726 | } |
| 727 | elf->state.ar.long_names = newp; |
| 728 | } |
| 729 | |
| 730 | elf->state.ar.long_names_len = len; |
| 731 | |
| 732 | /* Now NUL-terminate the strings. */ |
| 733 | runp = newp; |
| 734 | while (1) |
| 735 | { |
| 736 | runp = (char *) memchr (runp, '/', newp + len - runp); |
| 737 | if (runp == NULL) |
| 738 | /* This was the last entry. */ |
| 739 | break; |
| 740 | |
| 741 | /* NUL-terminate the string. */ |
| 742 | *runp = '\0'; |
| 743 | |
Ulrich Drepper | ce0bdb6 | 2007-02-05 07:13:52 +0000 | [diff] [blame] | 744 | /* Skip the NUL byte and the \012. */ |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 745 | runp += 2; |
| 746 | |
| 747 | /* A sanity check. Somebody might have generated invalid |
| 748 | archive. */ |
| 749 | if (runp >= newp + len) |
| 750 | break; |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | return newp; |
| 755 | } |
| 756 | |
| 757 | |
| 758 | /* Read the next archive header. */ |
| 759 | int |
Ulrich Drepper | 077c65f | 2006-07-12 19:54:51 +0000 | [diff] [blame] | 760 | internal_function |
Ulrich Drepper | 02f6645 | 2008-12-04 05:58:16 +0000 | [diff] [blame] | 761 | __libelf_next_arhdr_wrlock (elf) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 762 | Elf *elf; |
| 763 | { |
| 764 | struct ar_hdr *ar_hdr; |
| 765 | Elf_Arhdr *elf_ar_hdr; |
| 766 | |
| 767 | if (elf->map_address != NULL) |
| 768 | { |
| 769 | /* See whether this entry is in the file. */ |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 770 | if (unlikely (elf->state.ar.offset + sizeof (struct ar_hdr) |
| 771 | > elf->start_offset + elf->maximum_size)) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 772 | { |
| 773 | /* This record is not anymore in the file. */ |
| 774 | __libelf_seterrno (ELF_E_RANGE); |
| 775 | return -1; |
| 776 | } |
| 777 | ar_hdr = (struct ar_hdr *) (elf->map_address + elf->state.ar.offset); |
| 778 | } |
| 779 | else |
| 780 | { |
| 781 | ar_hdr = &elf->state.ar.ar_hdr; |
| 782 | |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 783 | if (unlikely (pread_retry (elf->fildes, ar_hdr, sizeof (struct ar_hdr), |
| 784 | elf->state.ar.offset) |
| 785 | != sizeof (struct ar_hdr))) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 786 | { |
| 787 | /* Something went wrong while reading the file. */ |
| 788 | __libelf_seterrno (ELF_E_RANGE); |
| 789 | return -1; |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | /* One little consistency check. */ |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 794 | if (unlikely (memcmp (ar_hdr->ar_fmag, ARFMAG, 2) != 0)) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 795 | { |
| 796 | /* This is no valid archive. */ |
| 797 | __libelf_seterrno (ELF_E_ARCHIVE_FMAG); |
| 798 | return -1; |
| 799 | } |
| 800 | |
| 801 | /* Copy the raw name over to a NUL terminated buffer. */ |
Mark Wielaard | babd5b6 | 2014-11-08 16:18:34 +0100 | [diff] [blame] | 802 | *((char *) mempcpy (elf->state.ar.raw_name, ar_hdr->ar_name, 16)) = '\0'; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 803 | |
| 804 | elf_ar_hdr = &elf->state.ar.elf_ar_hdr; |
| 805 | |
| 806 | /* Now convert the `struct ar_hdr' into `Elf_Arhdr'. |
| 807 | Determine whether this is a special entry. */ |
| 808 | if (ar_hdr->ar_name[0] == '/') |
| 809 | { |
| 810 | if (ar_hdr->ar_name[1] == ' ' |
| 811 | && memcmp (ar_hdr->ar_name, "/ ", 16) == 0) |
| 812 | /* This is the index. */ |
| 813 | elf_ar_hdr->ar_name = memcpy (elf->state.ar.ar_name, "/", 2); |
Petr Machata | e187314 | 2012-08-01 21:37:52 +0200 | [diff] [blame] | 814 | else if (ar_hdr->ar_name[1] == 'S' |
| 815 | && memcmp (ar_hdr->ar_name, "/SYM64/ ", 16) == 0) |
| 816 | /* 64-bit index. */ |
| 817 | elf_ar_hdr->ar_name = memcpy (elf->state.ar.ar_name, "/SYM64/", 8); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 818 | else if (ar_hdr->ar_name[1] == '/' |
| 819 | && memcmp (ar_hdr->ar_name, "// ", 16) == 0) |
| 820 | /* This is the array with the long names. */ |
| 821 | elf_ar_hdr->ar_name = memcpy (elf->state.ar.ar_name, "//", 3); |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 822 | else if (likely (isdigit (ar_hdr->ar_name[1]))) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 823 | { |
| 824 | size_t offset; |
| 825 | |
| 826 | /* This is a long name. First we have to read the long name |
| 827 | table, if this hasn't happened already. */ |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 828 | if (unlikely (elf->state.ar.long_names == NULL |
| 829 | && read_long_names (elf) == NULL)) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 830 | { |
| 831 | /* No long name table although it is reference. The archive is |
| 832 | broken. */ |
| 833 | __libelf_seterrno (ELF_E_INVALID_ARCHIVE); |
| 834 | return -1; |
| 835 | } |
| 836 | |
| 837 | offset = atol (ar_hdr->ar_name + 1); |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 838 | if (unlikely (offset >= elf->state.ar.long_names_len)) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 839 | { |
| 840 | /* The index in the long name table is larger than the table. */ |
| 841 | __libelf_seterrno (ELF_E_INVALID_ARCHIVE); |
| 842 | return -1; |
| 843 | } |
| 844 | elf_ar_hdr->ar_name = elf->state.ar.long_names + offset; |
| 845 | } |
| 846 | else |
| 847 | { |
| 848 | /* This is none of the known special entries. */ |
| 849 | __libelf_seterrno (ELF_E_INVALID_ARCHIVE); |
| 850 | return -1; |
| 851 | } |
| 852 | } |
| 853 | else |
| 854 | { |
| 855 | char *endp; |
| 856 | |
| 857 | /* It is a normal entry. Copy over the name. */ |
| 858 | endp = (char *) memccpy (elf->state.ar.ar_name, ar_hdr->ar_name, |
| 859 | '/', 16); |
| 860 | if (endp != NULL) |
| 861 | endp[-1] = '\0'; |
| 862 | else |
Ulrich Drepper | b597dfa | 2007-10-16 05:21:27 +0000 | [diff] [blame] | 863 | { |
| 864 | /* In the old BSD style of archive, there is no / terminator. |
| 865 | Instead, there is space padding at the end of the name. */ |
| 866 | size_t i = 15; |
| 867 | do |
| 868 | elf->state.ar.ar_name[i] = '\0'; |
| 869 | while (i > 0 && elf->state.ar.ar_name[--i] == ' '); |
| 870 | } |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 871 | |
| 872 | elf_ar_hdr->ar_name = elf->state.ar.ar_name; |
| 873 | } |
| 874 | |
Roland McGrath | 059c83e | 2008-02-21 06:19:39 +0000 | [diff] [blame] | 875 | if (unlikely (ar_hdr->ar_size[0] == ' ')) |
| 876 | /* Something is really wrong. We cannot live without a size for |
| 877 | the member since it will not be possible to find the next |
| 878 | archive member. */ |
| 879 | { |
| 880 | __libelf_seterrno (ELF_E_INVALID_ARCHIVE); |
| 881 | return -1; |
| 882 | } |
| 883 | |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 884 | /* Since there are no specialized functions to convert ASCII to |
| 885 | time_t, uid_t, gid_t, mode_t, and off_t we use either atol or |
| 886 | atoll depending on the size of the types. We are also prepared |
| 887 | for the case where the whole field in the `struct ar_hdr' is |
| 888 | filled in which case we cannot simply use atol/l but instead have |
| 889 | to create a temporary copy. */ |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 890 | |
Roland McGrath | 059c83e | 2008-02-21 06:19:39 +0000 | [diff] [blame] | 891 | #define INT_FIELD(FIELD) \ |
| 892 | do \ |
| 893 | { \ |
| 894 | char buf[sizeof (ar_hdr->FIELD) + 1]; \ |
| 895 | const char *string = ar_hdr->FIELD; \ |
| 896 | if (ar_hdr->FIELD[sizeof (ar_hdr->FIELD) - 1] != ' ') \ |
| 897 | { \ |
Mark Wielaard | babd5b6 | 2014-11-08 16:18:34 +0100 | [diff] [blame] | 898 | *((char *) mempcpy (buf, ar_hdr->FIELD, sizeof (ar_hdr->FIELD))) \ |
Roland McGrath | 059c83e | 2008-02-21 06:19:39 +0000 | [diff] [blame] | 899 | = '\0'; \ |
| 900 | string = buf; \ |
| 901 | } \ |
| 902 | if (sizeof (elf_ar_hdr->FIELD) <= sizeof (long int)) \ |
| 903 | elf_ar_hdr->FIELD = (__typeof (elf_ar_hdr->FIELD)) atol (string); \ |
| 904 | else \ |
| 905 | elf_ar_hdr->FIELD = (__typeof (elf_ar_hdr->FIELD)) atoll (string); \ |
| 906 | } \ |
| 907 | while (0) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 908 | |
Roland McGrath | 059c83e | 2008-02-21 06:19:39 +0000 | [diff] [blame] | 909 | INT_FIELD (ar_date); |
| 910 | INT_FIELD (ar_uid); |
| 911 | INT_FIELD (ar_gid); |
| 912 | INT_FIELD (ar_mode); |
| 913 | INT_FIELD (ar_size); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 914 | |
| 915 | return 0; |
| 916 | } |
| 917 | |
| 918 | |
| 919 | /* We were asked to return a clone of an existing descriptor. This |
| 920 | function must be called with the lock on the parent descriptor |
| 921 | being held. */ |
| 922 | static Elf * |
| 923 | dup_elf (int fildes, Elf_Cmd cmd, Elf *ref) |
| 924 | { |
| 925 | struct Elf *result; |
| 926 | |
| 927 | if (fildes == -1) |
| 928 | /* Allow the user to pass -1 as the file descriptor for the new file. */ |
| 929 | fildes = ref->fildes; |
| 930 | /* The file descriptor better should be the same. If it was disconnected |
| 931 | already (using `elf_cntl') we do not test it. */ |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 932 | else if (unlikely (ref->fildes != -1 && fildes != ref->fildes)) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 933 | { |
| 934 | __libelf_seterrno (ELF_E_FD_MISMATCH); |
| 935 | return NULL; |
| 936 | } |
| 937 | |
| 938 | /* The mode must allow reading. I.e., a descriptor creating with a |
| 939 | command different then ELF_C_READ, ELF_C_WRITE and ELF_C_RDWR is |
| 940 | not allowed. */ |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 941 | if (unlikely (ref->cmd != ELF_C_READ && ref->cmd != ELF_C_READ_MMAP |
| 942 | && ref->cmd != ELF_C_WRITE && ref->cmd != ELF_C_WRITE_MMAP |
| 943 | && ref->cmd != ELF_C_RDWR && ref->cmd != ELF_C_RDWR_MMAP |
| 944 | && ref->cmd != ELF_C_READ_MMAP_PRIVATE)) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 945 | { |
| 946 | __libelf_seterrno (ELF_E_INVALID_OP); |
| 947 | return NULL; |
| 948 | } |
| 949 | |
| 950 | /* Now it is time to distinguish between reading normal files and |
| 951 | archives. Normal files can easily be handled be incrementing the |
| 952 | reference counter and return the same descriptor. */ |
| 953 | if (ref->kind != ELF_K_AR) |
| 954 | { |
| 955 | ++ref->ref_count; |
| 956 | return ref; |
| 957 | } |
| 958 | |
| 959 | /* This is an archive. We must create a descriptor for the archive |
| 960 | member the internal pointer of the archive file desriptor is |
| 961 | pointing to. First read the header of the next member if this |
| 962 | has not happened already. */ |
| 963 | if (ref->state.ar.elf_ar_hdr.ar_name == NULL |
Ulrich Drepper | 02f6645 | 2008-12-04 05:58:16 +0000 | [diff] [blame] | 964 | && __libelf_next_arhdr_wrlock (ref) != 0) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 965 | /* Something went wrong. Maybe there is no member left. */ |
| 966 | return NULL; |
| 967 | |
| 968 | /* We have all the information we need about the next archive member. |
| 969 | Now create a descriptor for it. */ |
| 970 | result = read_file (fildes, ref->state.ar.offset + sizeof (struct ar_hdr), |
| 971 | ref->state.ar.elf_ar_hdr.ar_size, cmd, ref); |
| 972 | |
| 973 | /* Enlist this new descriptor in the list of children. */ |
| 974 | if (result != NULL) |
| 975 | { |
| 976 | result->next = ref->state.ar.children; |
| 977 | ref->state.ar.children = result; |
| 978 | } |
| 979 | |
| 980 | return result; |
| 981 | } |
| 982 | |
| 983 | |
| 984 | /* Return desriptor for empty file ready for writing. */ |
| 985 | static struct Elf * |
| 986 | write_file (int fd, Elf_Cmd cmd) |
| 987 | { |
| 988 | /* We simply create an empty `Elf' structure. */ |
| 989 | #define NSCNSALLOC 10 |
| 990 | Elf *result = allocate_elf (fd, NULL, 0, 0, cmd, NULL, ELF_K_ELF, |
| 991 | NSCNSALLOC * sizeof (Elf_Scn)); |
| 992 | |
| 993 | if (result != NULL) |
| 994 | { |
| 995 | /* We have to write to the file in any case. */ |
| 996 | result->flags = ELF_F_DIRTY; |
| 997 | |
| 998 | /* Some more or less arbitrary value. */ |
| 999 | result->state.elf.scnincr = NSCNSALLOC; |
| 1000 | |
| 1001 | /* We have allocated room for some sections. */ |
| 1002 | assert (offsetof (struct Elf, state.elf32.scns) |
| 1003 | == offsetof (struct Elf, state.elf64.scns)); |
| 1004 | result->state.elf.scns_last = &result->state.elf32.scns; |
| 1005 | result->state.elf32.scns.max = NSCNSALLOC; |
| 1006 | } |
| 1007 | |
| 1008 | return result; |
| 1009 | } |
| 1010 | |
| 1011 | |
| 1012 | /* Return a descriptor for the file belonging to FILDES. */ |
| 1013 | Elf * |
| 1014 | elf_begin (fildes, cmd, ref) |
| 1015 | int fildes; |
| 1016 | Elf_Cmd cmd; |
| 1017 | Elf *ref; |
| 1018 | { |
| 1019 | Elf *retval; |
| 1020 | |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 1021 | if (unlikely (! __libelf_version_initialized)) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1022 | { |
| 1023 | /* Version wasn't set so far. */ |
| 1024 | __libelf_seterrno (ELF_E_NO_VERSION); |
| 1025 | return NULL; |
| 1026 | } |
| 1027 | |
| 1028 | if (ref != NULL) |
| 1029 | /* Make sure the descriptor is not suddenly going away. */ |
Roland McGrath | b4d6f0f | 2008-08-25 22:55:17 +0000 | [diff] [blame] | 1030 | rwlock_rdlock (ref->lock); |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 1031 | else if (unlikely (fcntl (fildes, F_GETFL) == -1 && errno == EBADF)) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1032 | { |
| 1033 | /* We cannot do anything productive without a file descriptor. */ |
| 1034 | __libelf_seterrno (ELF_E_INVALID_FILE); |
| 1035 | return NULL; |
| 1036 | } |
| 1037 | |
Ulrich Drepper | 02f6645 | 2008-12-04 05:58:16 +0000 | [diff] [blame] | 1038 | Elf *lock_dup_elf () |
| 1039 | { |
| 1040 | /* We need wrlock to dup an archive. */ |
| 1041 | if (ref->kind == ELF_K_AR) |
| 1042 | { |
| 1043 | rwlock_unlock (ref->lock); |
| 1044 | rwlock_wrlock (ref->lock); |
| 1045 | } |
| 1046 | |
| 1047 | /* Duplicate the descriptor. */ |
| 1048 | return dup_elf (fildes, cmd, ref); |
| 1049 | } |
| 1050 | |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1051 | switch (cmd) |
| 1052 | { |
| 1053 | case ELF_C_NULL: |
| 1054 | /* We simply return a NULL pointer. */ |
| 1055 | retval = NULL; |
| 1056 | break; |
| 1057 | |
| 1058 | case ELF_C_READ_MMAP_PRIVATE: |
| 1059 | /* If we have a reference it must also be opened this way. */ |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 1060 | if (unlikely (ref != NULL && ref->cmd != ELF_C_READ_MMAP_PRIVATE)) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1061 | { |
| 1062 | __libelf_seterrno (ELF_E_INVALID_CMD); |
| 1063 | retval = NULL; |
| 1064 | break; |
| 1065 | } |
| 1066 | /* FALLTHROUGH */ |
| 1067 | |
| 1068 | case ELF_C_READ: |
| 1069 | case ELF_C_READ_MMAP: |
| 1070 | if (ref != NULL) |
Ulrich Drepper | 02f6645 | 2008-12-04 05:58:16 +0000 | [diff] [blame] | 1071 | retval = lock_dup_elf (); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1072 | else |
| 1073 | /* Create descriptor for existing file. */ |
| 1074 | retval = read_file (fildes, 0, ~((size_t) 0), cmd, NULL); |
| 1075 | break; |
| 1076 | |
| 1077 | case ELF_C_RDWR: |
| 1078 | case ELF_C_RDWR_MMAP: |
| 1079 | /* If we have a REF object it must also be opened using this |
| 1080 | command. */ |
| 1081 | if (ref != NULL) |
| 1082 | { |
Ulrich Drepper | fbe998a | 2005-08-29 16:27:10 +0000 | [diff] [blame] | 1083 | if (unlikely (ref->cmd != ELF_C_RDWR && ref->cmd != ELF_C_RDWR_MMAP |
| 1084 | && ref->cmd != ELF_C_WRITE |
| 1085 | && ref->cmd != ELF_C_WRITE_MMAP)) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1086 | { |
| 1087 | /* This is not ok. REF must also be opened for writing. */ |
| 1088 | __libelf_seterrno (ELF_E_INVALID_CMD); |
| 1089 | retval = NULL; |
| 1090 | } |
| 1091 | else |
Ulrich Drepper | 02f6645 | 2008-12-04 05:58:16 +0000 | [diff] [blame] | 1092 | retval = lock_dup_elf (); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1093 | } |
| 1094 | else |
| 1095 | /* Create descriptor for existing file. */ |
| 1096 | retval = read_file (fildes, 0, ~((size_t) 0), cmd, NULL); |
| 1097 | break; |
| 1098 | |
| 1099 | case ELF_C_WRITE: |
| 1100 | case ELF_C_WRITE_MMAP: |
| 1101 | /* We ignore REF and prepare a descriptor to write a new file. */ |
| 1102 | retval = write_file (fildes, cmd); |
| 1103 | break; |
| 1104 | |
| 1105 | default: |
| 1106 | __libelf_seterrno (ELF_E_INVALID_CMD); |
| 1107 | retval = NULL; |
| 1108 | break; |
| 1109 | } |
| 1110 | |
| 1111 | /* Release the lock. */ |
| 1112 | if (ref != NULL) |
Roland McGrath | b4d6f0f | 2008-08-25 22:55:17 +0000 | [diff] [blame] | 1113 | rwlock_unlock (ref->lock); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1114 | |
| 1115 | return retval; |
| 1116 | } |
| 1117 | INTDEF(elf_begin) |