1 /* Create descriptor for processing file.
2 Copyright (C) 1998-2010, 2012 Red Hat, Inc.
3 This file is part of elfutils.
4 Written by Ulrich Drepper <drepper@redhat.com>, 1998.
6 This file is free software; you can redistribute it and/or modify
7 it under the terms of either
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
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
19 or both in parallel, as here.
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.
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/>. */
43 #include <sys/param.h>
51 /* Create descriptor for archive in memory. */
53 file_read_ar (int fildes, void *map_address, off_t offset, size_t maxsize,
54 Elf_Cmd cmd, Elf *parent)
58 /* Create a descriptor. */
59 elf = allocate_elf (fildes, map_address, offset, maxsize, cmd, parent,
63 /* We don't read all the symbol tables in advance. All this will
65 elf->state.ar.offset = offset + SARMAG;
67 elf->state.ar.elf_ar_hdr.ar_rawname = elf->state.ar.raw_name;
75 get_shnum (void *map_address, unsigned char *e_ident, int fildes, off_t offset,
90 bool is32 = e_ident[EI_CLASS] == ELFCLASS32;
92 /* Make the ELF header available. */
93 if (e_ident[EI_DATA] == MY_ELFDATA
96 & ((is32 ? __alignof__ (Elf32_Ehdr) : __alignof__ (Elf64_Ehdr))
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. */
110 ehdr_mem.e32.e_shnum = ((Elf32_Ehdr *) e_ident)->e_shnum;
111 ehdr_mem.e32.e_shoff = ((Elf32_Ehdr *) e_ident)->e_shoff;
114 memcpy (&ehdr_mem, e_ident, sizeof (Elf32_Ehdr));
116 if (e_ident[EI_DATA] != MY_ELFDATA)
118 CONVERT (ehdr_mem.e32.e_shnum);
119 CONVERT (ehdr_mem.e32.e_shoff);
126 ehdr_mem.e64.e_shnum = ((Elf64_Ehdr *) e_ident)->e_shnum;
127 ehdr_mem.e64.e_shoff = ((Elf64_Ehdr *) e_ident)->e_shoff;
130 memcpy (&ehdr_mem, e_ident, sizeof (Elf64_Ehdr));
132 if (e_ident[EI_DATA] != MY_ELFDATA)
134 CONVERT (ehdr_mem.e64.e_shnum);
135 CONVERT (ehdr_mem.e64.e_shoff);
142 /* Get the number of sections from the ELF header. */
143 result = ehdr.e32->e_shnum;
145 if (unlikely (result == 0) && ehdr.e32->e_shoff != 0)
147 if (ehdr.e32->e_shoff + sizeof (Elf32_Shdr) > maxsize)
148 /* Cannot read the first section header. */
151 if (likely (map_address != NULL) && e_ident[EI_DATA] == MY_ELFDATA
153 || (((size_t) ((char *) map_address + offset))
154 & (__alignof__ (Elf32_Ehdr) - 1)) == 0))
155 /* We can directly access the memory. */
156 result = ((Elf32_Shdr *) ((char *) map_address + ehdr.e32->e_shoff
162 if (likely (map_address != NULL))
163 /* gcc will optimize the memcpy to a simple memory
164 access while taking care of alignment issues. */
165 memcpy (&size, &((Elf32_Shdr *) ((char *) map_address
168 sizeof (Elf32_Word));
170 if (unlikely (pread_retry (fildes, &size, sizeof (Elf32_Word),
171 offset + ehdr.e32->e_shoff
172 + offsetof (Elf32_Shdr, sh_size))
173 != sizeof (Elf32_Word)))
176 if (e_ident[EI_DATA] != MY_ELFDATA)
183 /* If the section headers were truncated, pretend none were there. */
184 if (ehdr.e32->e_shoff > maxsize
185 || maxsize - ehdr.e32->e_shoff < sizeof (Elf32_Shdr) * result)
190 /* Get the number of sections from the ELF header. */
191 result = ehdr.e64->e_shnum;
193 if (unlikely (result == 0) && ehdr.e64->e_shoff != 0)
195 if (ehdr.e64->e_shoff + sizeof (Elf64_Shdr) > maxsize)
196 /* Cannot read the first section header. */
200 if (likely (map_address != NULL) && e_ident[EI_DATA] == MY_ELFDATA
202 || (((size_t) ((char *) map_address + offset))
203 & (__alignof__ (Elf64_Ehdr) - 1)) == 0))
204 /* We can directly access the memory. */
205 size = ((Elf64_Shdr *) ((char *) map_address + ehdr.e64->e_shoff
209 if (likely (map_address != NULL))
210 /* gcc will optimize the memcpy to a simple memory
211 access while taking care of alignment issues. */
212 memcpy (&size, &((Elf64_Shdr *) ((char *) map_address
215 sizeof (Elf64_Xword));
217 if (unlikely (pread_retry (fildes, &size, sizeof (Elf64_Word),
218 offset + ehdr.e64->e_shoff
219 + offsetof (Elf64_Shdr, sh_size))
220 != sizeof (Elf64_Xword)))
223 if (e_ident[EI_DATA] != MY_ELFDATA)
227 if (size > ~((GElf_Word) 0))
228 /* Invalid value, it is too large. */
234 /* If the section headers were truncated, pretend none were there. */
235 if (ehdr.e64->e_shoff > maxsize
236 || maxsize - ehdr.e64->e_shoff < sizeof (Elf64_Shdr) * result)
244 /* Create descriptor for ELF file in memory. */
246 file_read_elf (int fildes, void *map_address, unsigned char *e_ident,
247 off_t offset, size_t maxsize, Elf_Cmd cmd, Elf *parent)
249 /* Verify the binary is of the class we can handle. */
250 if (unlikely ((e_ident[EI_CLASS] != ELFCLASS32
251 && e_ident[EI_CLASS] != ELFCLASS64)
252 /* We also can only handle two encodings. */
253 || (e_ident[EI_DATA] != ELFDATA2LSB
254 && e_ident[EI_DATA] != ELFDATA2MSB)))
256 /* Cannot handle this. */
257 __libelf_seterrno (ELF_E_INVALID_FILE);
261 /* Determine the number of sections. */
262 size_t scncnt = get_shnum (map_address, e_ident, fildes, offset, maxsize);
263 if (scncnt == (size_t) -1l)
264 /* Could not determine the number of sections. */
267 /* We can now allocate the memory. Even if there are no section headers,
268 we allocate space for a zeroth section in case we need it later. */
269 const size_t scnmax = (scncnt ?: (cmd == ELF_C_RDWR || cmd == ELF_C_RDWR_MMAP)
271 Elf *elf = allocate_elf (fildes, map_address, offset, maxsize, cmd, parent,
272 ELF_K_ELF, scnmax * sizeof (Elf_Scn));
274 /* Not enough memory. */
277 assert ((unsigned int) scncnt == scncnt);
278 assert (offsetof (struct Elf, state.elf32.scns)
279 == offsetof (struct Elf, state.elf64.scns));
280 elf->state.elf32.scns.cnt = scncnt;
281 elf->state.elf32.scns.max = scnmax;
283 /* Some more or less arbitrary value. */
284 elf->state.elf.scnincr = 10;
286 /* Make the class easily available. */
287 elf->class = e_ident[EI_CLASS];
289 if (e_ident[EI_CLASS] == ELFCLASS32)
291 /* This pointer might not be directly usable if the alignment is
292 not sufficient for the architecture. */
293 Elf32_Ehdr *ehdr = (Elf32_Ehdr *) ((char *) map_address + offset);
295 /* This is a 32-bit binary. */
296 if (map_address != NULL && e_ident[EI_DATA] == MY_ELFDATA
298 || ((((uintptr_t) ehdr) & (__alignof__ (Elf32_Ehdr) - 1)) == 0
299 && ((uintptr_t) ((char *) ehdr + ehdr->e_shoff)
300 & (__alignof__ (Elf32_Shdr) - 1)) == 0
301 && ((uintptr_t) ((char *) ehdr + ehdr->e_phoff)
302 & (__alignof__ (Elf32_Phdr) - 1)) == 0)))
304 /* We can use the mmapped memory. */
305 elf->state.elf32.ehdr = ehdr;
306 elf->state.elf32.shdr
307 = (Elf32_Shdr *) ((char *) ehdr + ehdr->e_shoff);
309 /* Don't precache the phdr pointer here.
310 elf32_getphdr will validate it against the size when asked. */
312 for (size_t cnt = 0; cnt < scncnt; ++cnt)
314 elf->state.elf32.scns.data[cnt].index = cnt;
315 elf->state.elf32.scns.data[cnt].elf = elf;
316 elf->state.elf32.scns.data[cnt].shdr.e32 =
317 &elf->state.elf32.shdr[cnt];
318 if (likely (elf->state.elf32.shdr[cnt].sh_offset < maxsize)
319 && likely (maxsize - elf->state.elf32.shdr[cnt].sh_offset
320 <= elf->state.elf32.shdr[cnt].sh_size))
321 elf->state.elf32.scns.data[cnt].rawdata_base =
322 elf->state.elf32.scns.data[cnt].data_base =
323 ((char *) map_address + offset
324 + elf->state.elf32.shdr[cnt].sh_offset);
325 elf->state.elf32.scns.data[cnt].list = &elf->state.elf32.scns;
327 /* If this is a section with an extended index add a
328 reference in the section which uses the extended
330 if (elf->state.elf32.shdr[cnt].sh_type == SHT_SYMTAB_SHNDX
331 && elf->state.elf32.shdr[cnt].sh_link < scncnt)
332 elf->state.elf32.scns.data[elf->state.elf32.shdr[cnt].sh_link].shndx_index
335 /* Set the own shndx_index field in case it has not yet
337 if (elf->state.elf32.scns.data[cnt].shndx_index == 0)
338 elf->state.elf32.scns.data[cnt].shndx_index = -1;
343 /* Copy the ELF header. */
344 elf->state.elf32.ehdr = memcpy (&elf->state.elf32.ehdr_mem, e_ident,
345 sizeof (Elf32_Ehdr));
347 if (e_ident[EI_DATA] != MY_ELFDATA)
349 CONVERT (elf->state.elf32.ehdr_mem.e_type);
350 CONVERT (elf->state.elf32.ehdr_mem.e_machine);
351 CONVERT (elf->state.elf32.ehdr_mem.e_version);
352 CONVERT (elf->state.elf32.ehdr_mem.e_entry);
353 CONVERT (elf->state.elf32.ehdr_mem.e_phoff);
354 CONVERT (elf->state.elf32.ehdr_mem.e_shoff);
355 CONVERT (elf->state.elf32.ehdr_mem.e_flags);
356 CONVERT (elf->state.elf32.ehdr_mem.e_ehsize);
357 CONVERT (elf->state.elf32.ehdr_mem.e_phentsize);
358 CONVERT (elf->state.elf32.ehdr_mem.e_phnum);
359 CONVERT (elf->state.elf32.ehdr_mem.e_shentsize);
360 CONVERT (elf->state.elf32.ehdr_mem.e_shnum);
361 CONVERT (elf->state.elf32.ehdr_mem.e_shstrndx);
364 for (size_t cnt = 0; cnt < scncnt; ++cnt)
366 elf->state.elf32.scns.data[cnt].index = cnt;
367 elf->state.elf32.scns.data[cnt].elf = elf;
368 elf->state.elf32.scns.data[cnt].list = &elf->state.elf32.scns;
372 /* So far only one block with sections. */
373 elf->state.elf32.scns_last = &elf->state.elf32.scns;
377 /* This pointer might not be directly usable if the alignment is
378 not sufficient for the architecture. */
379 Elf64_Ehdr *ehdr = (Elf64_Ehdr *) ((char *) map_address + offset);
381 /* This is a 64-bit binary. */
382 if (map_address != NULL && e_ident[EI_DATA] == MY_ELFDATA
384 || ((((uintptr_t) ehdr) & (__alignof__ (Elf64_Ehdr) - 1)) == 0
385 && ((uintptr_t) ((char *) ehdr + ehdr->e_shoff)
386 & (__alignof__ (Elf64_Shdr) - 1)) == 0
387 && ((uintptr_t) ((char *) ehdr + ehdr->e_phoff)
388 & (__alignof__ (Elf64_Phdr) - 1)) == 0)))
390 /* We can use the mmapped memory. */
391 elf->state.elf64.ehdr = ehdr;
392 elf->state.elf64.shdr
393 = (Elf64_Shdr *) ((char *) ehdr + ehdr->e_shoff);
395 /* Don't precache the phdr pointer here.
396 elf64_getphdr will validate it against the size when asked. */
398 for (size_t cnt = 0; cnt < scncnt; ++cnt)
400 elf->state.elf64.scns.data[cnt].index = cnt;
401 elf->state.elf64.scns.data[cnt].elf = elf;
402 elf->state.elf64.scns.data[cnt].shdr.e64 =
403 &elf->state.elf64.shdr[cnt];
404 if (likely (elf->state.elf64.shdr[cnt].sh_offset < maxsize)
405 && likely (maxsize - elf->state.elf64.shdr[cnt].sh_offset
406 <= elf->state.elf64.shdr[cnt].sh_size))
407 elf->state.elf64.scns.data[cnt].rawdata_base =
408 elf->state.elf64.scns.data[cnt].data_base =
409 ((char *) map_address + offset
410 + elf->state.elf64.shdr[cnt].sh_offset);
411 elf->state.elf64.scns.data[cnt].list = &elf->state.elf64.scns;
413 /* If this is a section with an extended index add a
414 reference in the section which uses the extended
416 if (elf->state.elf64.shdr[cnt].sh_type == SHT_SYMTAB_SHNDX
417 && elf->state.elf64.shdr[cnt].sh_link < scncnt)
418 elf->state.elf64.scns.data[elf->state.elf64.shdr[cnt].sh_link].shndx_index
421 /* Set the own shndx_index field in case it has not yet
423 if (elf->state.elf64.scns.data[cnt].shndx_index == 0)
424 elf->state.elf64.scns.data[cnt].shndx_index = -1;
429 /* Copy the ELF header. */
430 elf->state.elf64.ehdr = memcpy (&elf->state.elf64.ehdr_mem, e_ident,
431 sizeof (Elf64_Ehdr));
433 if (e_ident[EI_DATA] != MY_ELFDATA)
435 CONVERT (elf->state.elf64.ehdr_mem.e_type);
436 CONVERT (elf->state.elf64.ehdr_mem.e_machine);
437 CONVERT (elf->state.elf64.ehdr_mem.e_version);
438 CONVERT (elf->state.elf64.ehdr_mem.e_entry);
439 CONVERT (elf->state.elf64.ehdr_mem.e_phoff);
440 CONVERT (elf->state.elf64.ehdr_mem.e_shoff);
441 CONVERT (elf->state.elf64.ehdr_mem.e_flags);
442 CONVERT (elf->state.elf64.ehdr_mem.e_ehsize);
443 CONVERT (elf->state.elf64.ehdr_mem.e_phentsize);
444 CONVERT (elf->state.elf64.ehdr_mem.e_phnum);
445 CONVERT (elf->state.elf64.ehdr_mem.e_shentsize);
446 CONVERT (elf->state.elf64.ehdr_mem.e_shnum);
447 CONVERT (elf->state.elf64.ehdr_mem.e_shstrndx);
450 for (size_t cnt = 0; cnt < scncnt; ++cnt)
452 elf->state.elf64.scns.data[cnt].index = cnt;
453 elf->state.elf64.scns.data[cnt].elf = elf;
454 elf->state.elf64.scns.data[cnt].list = &elf->state.elf64.scns;
458 /* So far only one block with sections. */
459 elf->state.elf64.scns_last = &elf->state.elf64.scns;
468 __libelf_read_mmaped_file (int fildes, void *map_address, off_t offset,
469 size_t maxsize, Elf_Cmd cmd, Elf *parent)
471 /* We have to find out what kind of file this is. We handle ELF
472 files and archives. To find out what we have we must look at the
473 header. The header for an ELF file is EI_NIDENT bytes in size,
474 the header for an archive file SARMAG bytes long. */
475 unsigned char *e_ident = (unsigned char *) map_address + offset;
477 /* See what kind of object we have here. */
478 Elf_Kind kind = determine_kind (e_ident, maxsize);
483 return file_read_elf (fildes, map_address, e_ident, offset, maxsize,
487 return file_read_ar (fildes, map_address, offset, maxsize, cmd, parent);
493 /* This case is easy. Since we cannot do anything with this file
494 create a dummy descriptor. */
495 return allocate_elf (fildes, map_address, offset, maxsize, cmd, parent,
501 read_unmmaped_file (int fildes, off_t offset, size_t maxsize, Elf_Cmd cmd,
504 /* We have to find out what kind of file this is. We handle ELF
505 files and archives. To find out what we have we must read the
506 header. The identification header for an ELF file is EI_NIDENT
507 bytes in size, but we read the whole ELF header since we will
508 need it anyway later. For archives the header in SARMAG bytes
509 long. Read the maximum of these numbers.
511 XXX We have to change this for the extended `ar' format some day.
513 Use a union to ensure alignment. We might later access the
514 memory as a ElfXX_Ehdr. */
518 unsigned char header[MAX (sizeof (Elf64_Ehdr), SARMAG)];
521 /* Read the head of the file. */
522 ssize_t nread = pread_retry (fildes, mem.header,
523 MIN (MAX (sizeof (Elf64_Ehdr), SARMAG),
526 if (unlikely (nread == -1))
527 /* We cannot even read the head of the file. Maybe FILDES is associated
528 with an unseekable device. This is nothing we can handle. */
531 /* See what kind of object we have here. */
532 Elf_Kind kind = determine_kind (mem.header, nread);
537 return file_read_ar (fildes, NULL, offset, maxsize, cmd, parent);
540 /* Make sure at least the ELF header is contained in the file. */
541 if ((size_t) nread >= (mem.header[EI_CLASS] == ELFCLASS32
542 ? sizeof (Elf32_Ehdr) : sizeof (Elf64_Ehdr)))
543 return file_read_elf (fildes, NULL, mem.header, offset, maxsize, cmd,
551 /* This case is easy. Since we cannot do anything with this file
552 create a dummy descriptor. */
553 return allocate_elf (fildes, NULL, offset, maxsize, cmd, parent,
558 /* Open a file for reading. If possible we will try to mmap() the file. */
560 read_file (int fildes, off_t offset, size_t maxsize,
561 Elf_Cmd cmd, Elf *parent)
563 void *map_address = NULL;
564 int use_mmap = (cmd == ELF_C_READ_MMAP || cmd == ELF_C_RDWR_MMAP
565 || cmd == ELF_C_WRITE_MMAP
566 || cmd == ELF_C_READ_MMAP_PRIVATE);
569 /* Mudflap doesn't grok that our mmap'd data is ok. */
577 if (maxsize == ~((size_t) 0))
579 /* We don't know in the moment how large the file is.
583 if (fstat (fildes, &st) == 0
584 && (sizeof (size_t) >= sizeof (st.st_size)
585 || st.st_size <= ~((size_t) 0)))
586 maxsize = (size_t) st.st_size;
589 /* We try to map the file ourself. */
590 map_address = mmap (NULL, maxsize, (cmd == ELF_C_READ_MMAP
592 : PROT_READ|PROT_WRITE),
593 cmd == ELF_C_READ_MMAP_PRIVATE
594 || cmd == ELF_C_READ_MMAP
595 ? MAP_PRIVATE : MAP_SHARED,
598 if (map_address == MAP_FAILED)
603 /* The parent is already loaded. Use it. */
604 assert (maxsize != ~((size_t) 0));
606 map_address = parent->map_address;
610 /* If we have the file in memory optimize the access. */
611 if (map_address != NULL)
613 assert (map_address != MAP_FAILED);
615 struct Elf *result = __libelf_read_mmaped_file (fildes, map_address,
616 offset, maxsize, cmd,
619 /* If something went wrong during the initialization unmap the
620 memory if we mmaped here. */
623 || parent->map_address != map_address))
624 munmap (map_address, maxsize);
625 else if (parent == NULL)
626 /* Remember that we mmap()ed the memory. */
627 result->flags |= ELF_F_MMAPPED;
632 /* Otherwise we have to do it the hard way. We read as much as necessary
633 from the file whenever we need information which is not available. */
634 return read_unmmaped_file (fildes, offset, maxsize, cmd, parent);
638 /* Find the entry with the long names for the content of this archive. */
640 read_long_names (Elf *elf)
642 off_t offset = SARMAG; /* This is the first entry. */
650 if (elf->map_address != NULL)
652 if (offset + sizeof (struct ar_hdr) > elf->maximum_size)
655 /* The data is mapped. */
656 hdr = (struct ar_hdr *) (elf->map_address + offset);
660 /* Read the header from the file. */
661 if (unlikely (pread_retry (elf->fildes, &hdrm, sizeof (hdrm),
662 elf->start_offset + offset)
669 len = atol (hdr->ar_size);
671 if (memcmp (hdr->ar_name, "// ", 16) == 0)
674 offset += sizeof (struct ar_hdr) + ((len + 1) & ~1l);
677 /* Due to the stupid format of the long name table entry (which are not
678 NUL terminted) we have to provide an appropriate representation anyhow.
679 Therefore we always make a copy which has the appropriate form. */
680 newp = (char *) malloc (len);
685 if (elf->map_address != NULL)
686 /* Simply copy it over. */
687 elf->state.ar.long_names = (char *) memcpy (newp,
688 elf->map_address + offset
689 + sizeof (struct ar_hdr),
693 if (unlikely ((size_t) pread_retry (elf->fildes, newp, len,
694 elf->start_offset + offset
695 + sizeof (struct ar_hdr))
698 /* We were not able to read all data. */
700 elf->state.ar.long_names = NULL;
703 elf->state.ar.long_names = newp;
706 elf->state.ar.long_names_len = len;
708 /* Now NUL-terminate the strings. */
712 runp = (char *) memchr (runp, '/', newp + len - runp);
714 /* This was the last entry. */
717 /* NUL-terminate the string. */
720 /* Skip the NUL byte and the \012. */
723 /* A sanity check. Somebody might have generated invalid
725 if (runp >= newp + len)
734 /* Read the next archive header. */
737 __libelf_next_arhdr_wrlock (elf)
740 struct ar_hdr *ar_hdr;
741 Elf_Arhdr *elf_ar_hdr;
743 if (elf->map_address != NULL)
745 /* See whether this entry is in the file. */
746 if (unlikely (elf->state.ar.offset + sizeof (struct ar_hdr)
747 > elf->start_offset + elf->maximum_size))
749 /* This record is not anymore in the file. */
750 __libelf_seterrno (ELF_E_RANGE);
753 ar_hdr = (struct ar_hdr *) (elf->map_address + elf->state.ar.offset);
757 ar_hdr = &elf->state.ar.ar_hdr;
759 if (unlikely (pread_retry (elf->fildes, ar_hdr, sizeof (struct ar_hdr),
760 elf->state.ar.offset)
761 != sizeof (struct ar_hdr)))
763 /* Something went wrong while reading the file. */
764 __libelf_seterrno (ELF_E_RANGE);
769 /* One little consistency check. */
770 if (unlikely (memcmp (ar_hdr->ar_fmag, ARFMAG, 2) != 0))
772 /* This is no valid archive. */
773 __libelf_seterrno (ELF_E_ARCHIVE_FMAG);
777 /* Copy the raw name over to a NUL terminated buffer. */
778 *((char *) __mempcpy (elf->state.ar.raw_name, ar_hdr->ar_name, 16)) = '\0';
780 elf_ar_hdr = &elf->state.ar.elf_ar_hdr;
782 /* Now convert the `struct ar_hdr' into `Elf_Arhdr'.
783 Determine whether this is a special entry. */
784 if (ar_hdr->ar_name[0] == '/')
786 if (ar_hdr->ar_name[1] == ' '
787 && memcmp (ar_hdr->ar_name, "/ ", 16) == 0)
788 /* This is the index. */
789 elf_ar_hdr->ar_name = memcpy (elf->state.ar.ar_name, "/", 2);
790 else if (ar_hdr->ar_name[1] == 'S'
791 && memcmp (ar_hdr->ar_name, "/SYM64/ ", 16) == 0)
793 elf_ar_hdr->ar_name = memcpy (elf->state.ar.ar_name, "/SYM64/", 8);
794 else if (ar_hdr->ar_name[1] == '/'
795 && memcmp (ar_hdr->ar_name, "// ", 16) == 0)
796 /* This is the array with the long names. */
797 elf_ar_hdr->ar_name = memcpy (elf->state.ar.ar_name, "//", 3);
798 else if (likely (isdigit (ar_hdr->ar_name[1])))
802 /* This is a long name. First we have to read the long name
803 table, if this hasn't happened already. */
804 if (unlikely (elf->state.ar.long_names == NULL
805 && read_long_names (elf) == NULL))
807 /* No long name table although it is reference. The archive is
809 __libelf_seterrno (ELF_E_INVALID_ARCHIVE);
813 offset = atol (ar_hdr->ar_name + 1);
814 if (unlikely (offset >= elf->state.ar.long_names_len))
816 /* The index in the long name table is larger than the table. */
817 __libelf_seterrno (ELF_E_INVALID_ARCHIVE);
820 elf_ar_hdr->ar_name = elf->state.ar.long_names + offset;
824 /* This is none of the known special entries. */
825 __libelf_seterrno (ELF_E_INVALID_ARCHIVE);
833 /* It is a normal entry. Copy over the name. */
834 endp = (char *) memccpy (elf->state.ar.ar_name, ar_hdr->ar_name,
840 /* In the old BSD style of archive, there is no / terminator.
841 Instead, there is space padding at the end of the name. */
844 elf->state.ar.ar_name[i] = '\0';
845 while (i > 0 && elf->state.ar.ar_name[--i] == ' ');
848 elf_ar_hdr->ar_name = elf->state.ar.ar_name;
851 if (unlikely (ar_hdr->ar_size[0] == ' '))
852 /* Something is really wrong. We cannot live without a size for
853 the member since it will not be possible to find the next
856 __libelf_seterrno (ELF_E_INVALID_ARCHIVE);
860 /* Since there are no specialized functions to convert ASCII to
861 time_t, uid_t, gid_t, mode_t, and off_t we use either atol or
862 atoll depending on the size of the types. We are also prepared
863 for the case where the whole field in the `struct ar_hdr' is
864 filled in which case we cannot simply use atol/l but instead have
865 to create a temporary copy. */
867 #define INT_FIELD(FIELD) \
870 char buf[sizeof (ar_hdr->FIELD) + 1]; \
871 const char *string = ar_hdr->FIELD; \
872 if (ar_hdr->FIELD[sizeof (ar_hdr->FIELD) - 1] != ' ') \
874 *((char *) __mempcpy (buf, ar_hdr->FIELD, sizeof (ar_hdr->FIELD))) \
878 if (sizeof (elf_ar_hdr->FIELD) <= sizeof (long int)) \
879 elf_ar_hdr->FIELD = (__typeof (elf_ar_hdr->FIELD)) atol (string); \
881 elf_ar_hdr->FIELD = (__typeof (elf_ar_hdr->FIELD)) atoll (string); \
895 /* We were asked to return a clone of an existing descriptor. This
896 function must be called with the lock on the parent descriptor
899 dup_elf (int fildes, Elf_Cmd cmd, Elf *ref)
904 /* Allow the user to pass -1 as the file descriptor for the new file. */
905 fildes = ref->fildes;
906 /* The file descriptor better should be the same. If it was disconnected
907 already (using `elf_cntl') we do not test it. */
908 else if (unlikely (ref->fildes != -1 && fildes != ref->fildes))
910 __libelf_seterrno (ELF_E_FD_MISMATCH);
914 /* The mode must allow reading. I.e., a descriptor creating with a
915 command different then ELF_C_READ, ELF_C_WRITE and ELF_C_RDWR is
917 if (unlikely (ref->cmd != ELF_C_READ && ref->cmd != ELF_C_READ_MMAP
918 && ref->cmd != ELF_C_WRITE && ref->cmd != ELF_C_WRITE_MMAP
919 && ref->cmd != ELF_C_RDWR && ref->cmd != ELF_C_RDWR_MMAP
920 && ref->cmd != ELF_C_READ_MMAP_PRIVATE))
922 __libelf_seterrno (ELF_E_INVALID_OP);
926 /* Now it is time to distinguish between reading normal files and
927 archives. Normal files can easily be handled be incrementing the
928 reference counter and return the same descriptor. */
929 if (ref->kind != ELF_K_AR)
935 /* This is an archive. We must create a descriptor for the archive
936 member the internal pointer of the archive file desriptor is
937 pointing to. First read the header of the next member if this
938 has not happened already. */
939 if (ref->state.ar.elf_ar_hdr.ar_name == NULL
940 && __libelf_next_arhdr_wrlock (ref) != 0)
941 /* Something went wrong. Maybe there is no member left. */
944 /* We have all the information we need about the next archive member.
945 Now create a descriptor for it. */
946 result = read_file (fildes, ref->state.ar.offset + sizeof (struct ar_hdr),
947 ref->state.ar.elf_ar_hdr.ar_size, cmd, ref);
949 /* Enlist this new descriptor in the list of children. */
952 result->next = ref->state.ar.children;
953 ref->state.ar.children = result;
960 /* Return desriptor for empty file ready for writing. */
962 write_file (int fd, Elf_Cmd cmd)
964 /* We simply create an empty `Elf' structure. */
965 #define NSCNSALLOC 10
966 Elf *result = allocate_elf (fd, NULL, 0, 0, cmd, NULL, ELF_K_ELF,
967 NSCNSALLOC * sizeof (Elf_Scn));
971 /* We have to write to the file in any case. */
972 result->flags = ELF_F_DIRTY;
974 /* Some more or less arbitrary value. */
975 result->state.elf.scnincr = NSCNSALLOC;
977 /* We have allocated room for some sections. */
978 assert (offsetof (struct Elf, state.elf32.scns)
979 == offsetof (struct Elf, state.elf64.scns));
980 result->state.elf.scns_last = &result->state.elf32.scns;
981 result->state.elf32.scns.max = NSCNSALLOC;
988 /* Return a descriptor for the file belonging to FILDES. */
990 elf_begin (fildes, cmd, ref)
997 if (unlikely (! __libelf_version_initialized))
999 /* Version wasn't set so far. */
1000 __libelf_seterrno (ELF_E_NO_VERSION);
1005 /* Make sure the descriptor is not suddenly going away. */
1006 rwlock_rdlock (ref->lock);
1007 else if (unlikely (fcntl (fildes, F_GETFL) == -1 && errno == EBADF))
1009 /* We cannot do anything productive without a file descriptor. */
1010 __libelf_seterrno (ELF_E_INVALID_FILE);
1014 Elf *lock_dup_elf ()
1016 /* We need wrlock to dup an archive. */
1017 if (ref->kind == ELF_K_AR)
1019 rwlock_unlock (ref->lock);
1020 rwlock_wrlock (ref->lock);
1023 /* Duplicate the descriptor. */
1024 return dup_elf (fildes, cmd, ref);
1030 /* We simply return a NULL pointer. */
1034 case ELF_C_READ_MMAP_PRIVATE:
1035 /* If we have a reference it must also be opened this way. */
1036 if (unlikely (ref != NULL && ref->cmd != ELF_C_READ_MMAP_PRIVATE))
1038 __libelf_seterrno (ELF_E_INVALID_CMD);
1045 case ELF_C_READ_MMAP:
1047 retval = lock_dup_elf ();
1049 /* Create descriptor for existing file. */
1050 retval = read_file (fildes, 0, ~((size_t) 0), cmd, NULL);
1054 case ELF_C_RDWR_MMAP:
1055 /* If we have a REF object it must also be opened using this
1059 if (unlikely (ref->cmd != ELF_C_RDWR && ref->cmd != ELF_C_RDWR_MMAP
1060 && ref->cmd != ELF_C_WRITE
1061 && ref->cmd != ELF_C_WRITE_MMAP))
1063 /* This is not ok. REF must also be opened for writing. */
1064 __libelf_seterrno (ELF_E_INVALID_CMD);
1068 retval = lock_dup_elf ();
1071 /* Create descriptor for existing file. */
1072 retval = read_file (fildes, 0, ~((size_t) 0), cmd, NULL);
1076 case ELF_C_WRITE_MMAP:
1077 /* We ignore REF and prepare a descriptor to write a new file. */
1078 retval = write_file (fildes, cmd);
1082 __libelf_seterrno (ELF_E_INVALID_CMD);
1087 /* Release the lock. */
1089 rwlock_unlock (ref->lock);