1 /* Create descriptor for processing file.
2 Copyright (C) 1998-2010 Red Hat, Inc.
3 This file is part of Red Hat elfutils.
4 Written by Ulrich Drepper <drepper@redhat.com>, 1998.
6 Red Hat elfutils is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by the
8 Free Software Foundation; version 2 of the License.
10 Red Hat elfutils is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with Red Hat elfutils; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
19 In addition, as a special exception, Red Hat, Inc. gives You the
20 additional right to link the code of Red Hat elfutils with code licensed
21 under any Open Source Initiative certified open source license
22 (http://www.opensource.org/licenses/index.php) which requires the
23 distribution of source code with any binary distribution and to
24 distribute linked combinations of the two. Non-GPL Code permitted under
25 this exception must only link to the code of Red Hat elfutils through
26 those well defined interfaces identified in the file named EXCEPTION
27 found in the source code files (the "Approved Interfaces"). The files
28 of Non-GPL Code may instantiate templates or use macros or inline
29 functions from the Approved Interfaces without causing the resulting
30 work to be covered by the GNU General Public License. Only Red Hat,
31 Inc. may make changes or additions to the list of Approved Interfaces.
32 Red Hat's grant of this exception is conditioned upon your not adding
33 any new exceptions. If you wish to add a new Approved Interface or
34 exception, please contact Red Hat. You must obey the GNU General Public
35 License in all respects for all of the Red Hat elfutils code and other
36 code used in conjunction with Red Hat elfutils except the Non-GPL Code
37 covered by this exception. If you modify this file, you may extend this
38 exception to your version of the file, but you are not obligated to do
39 so. If you do not wish to provide this exception without modification,
40 you must delete this exception statement from your version and license
41 this file solely under the GPL without exception.
43 Red Hat elfutils is an included package of the Open Invention Network.
44 An included package of the Open Invention Network is a package for which
45 Open Invention Network licensees cross-license their patents. No patent
46 license is granted, either expressly or impliedly, by designation as an
47 included package. Should you wish to participate in the Open Invention
48 Network licensing program, please visit www.openinventionnetwork.com
49 <http://www.openinventionnetwork.com>. */
64 #include <sys/param.h>
72 /* Create descriptor for archive in memory. */
74 file_read_ar (int fildes, void *map_address, off_t offset, size_t maxsize,
75 Elf_Cmd cmd, Elf *parent)
79 /* Create a descriptor. */
80 elf = allocate_elf (fildes, map_address, offset, maxsize, cmd, parent,
84 /* We don't read all the symbol tables in advance. All this will
86 elf->state.ar.offset = offset + SARMAG;
88 elf->state.ar.elf_ar_hdr.ar_rawname = elf->state.ar.raw_name;
96 get_shnum (void *map_address, unsigned char *e_ident, int fildes, off_t offset,
111 bool is32 = e_ident[EI_CLASS] == ELFCLASS32;
113 /* Make the ELF header available. */
114 if (e_ident[EI_DATA] == MY_ELFDATA
116 || (((size_t) e_ident
117 & ((is32 ? __alignof__ (Elf32_Ehdr) : __alignof__ (Elf64_Ehdr))
122 /* We already read the ELF header. We have to copy the header
123 since we possibly modify the data here and the caller
124 expects the memory it passes in to be preserved. */
131 ehdr_mem.e32.e_shnum = ((Elf32_Ehdr *) e_ident)->e_shnum;
132 ehdr_mem.e32.e_shoff = ((Elf32_Ehdr *) e_ident)->e_shoff;
135 memcpy (&ehdr_mem, e_ident, sizeof (Elf32_Ehdr));
137 if (e_ident[EI_DATA] != MY_ELFDATA)
139 CONVERT (ehdr_mem.e32.e_shnum);
140 CONVERT (ehdr_mem.e32.e_shoff);
147 ehdr_mem.e64.e_shnum = ((Elf64_Ehdr *) e_ident)->e_shnum;
148 ehdr_mem.e64.e_shoff = ((Elf64_Ehdr *) e_ident)->e_shoff;
151 memcpy (&ehdr_mem, e_ident, sizeof (Elf64_Ehdr));
153 if (e_ident[EI_DATA] != MY_ELFDATA)
155 CONVERT (ehdr_mem.e64.e_shnum);
156 CONVERT (ehdr_mem.e64.e_shoff);
163 /* Get the number of sections from the ELF header. */
164 result = ehdr.e32->e_shnum;
166 if (unlikely (result == 0) && ehdr.e32->e_shoff != 0)
168 if (ehdr.e32->e_shoff + sizeof (Elf32_Shdr) > maxsize)
169 /* Cannot read the first section header. */
172 if (likely (map_address != NULL) && e_ident[EI_DATA] == MY_ELFDATA
174 || (((size_t) ((char *) map_address + offset))
175 & (__alignof__ (Elf32_Ehdr) - 1)) == 0))
176 /* We can directly access the memory. */
177 result = ((Elf32_Shdr *) ((char *) map_address + ehdr.e32->e_shoff
183 if (likely (map_address != NULL))
184 /* gcc will optimize the memcpy to a simple memory
185 access while taking care of alignment issues. */
186 memcpy (&size, &((Elf32_Shdr *) ((char *) map_address
189 sizeof (Elf32_Word));
191 if (unlikely (pread_retry (fildes, &size, sizeof (Elf32_Word),
192 offset + ehdr.e32->e_shoff
193 + offsetof (Elf32_Shdr, sh_size))
194 != sizeof (Elf32_Word)))
197 if (e_ident[EI_DATA] != MY_ELFDATA)
204 /* If the section headers were truncated, pretend none were there. */
205 if (ehdr.e32->e_shoff > maxsize
206 || maxsize - ehdr.e32->e_shoff < sizeof (Elf32_Shdr) * result)
211 /* Get the number of sections from the ELF header. */
212 result = ehdr.e64->e_shnum;
214 if (unlikely (result == 0) && ehdr.e64->e_shoff != 0)
216 if (ehdr.e64->e_shoff + sizeof (Elf64_Shdr) > maxsize)
217 /* Cannot read the first section header. */
221 if (likely (map_address != NULL) && e_ident[EI_DATA] == MY_ELFDATA
223 || (((size_t) ((char *) map_address + offset))
224 & (__alignof__ (Elf64_Ehdr) - 1)) == 0))
225 /* We can directly access the memory. */
226 size = ((Elf64_Shdr *) ((char *) map_address + ehdr.e64->e_shoff
230 if (likely (map_address != NULL))
231 /* gcc will optimize the memcpy to a simple memory
232 access while taking care of alignment issues. */
233 memcpy (&size, &((Elf64_Shdr *) ((char *) map_address
236 sizeof (Elf64_Xword));
238 if (unlikely (pread_retry (fildes, &size, sizeof (Elf64_Word),
239 offset + ehdr.e64->e_shoff
240 + offsetof (Elf64_Shdr, sh_size))
241 != sizeof (Elf64_Xword)))
244 if (e_ident[EI_DATA] != MY_ELFDATA)
248 if (size > ~((GElf_Word) 0))
249 /* Invalid value, it is too large. */
255 /* If the section headers were truncated, pretend none were there. */
256 if (ehdr.e64->e_shoff > maxsize
257 || maxsize - ehdr.e64->e_shoff < sizeof (Elf64_Shdr) * result)
265 /* Create descriptor for ELF file in memory. */
267 file_read_elf (int fildes, void *map_address, unsigned char *e_ident,
268 off_t offset, size_t maxsize, Elf_Cmd cmd, Elf *parent)
270 /* Verify the binary is of the class we can handle. */
271 if (unlikely ((e_ident[EI_CLASS] != ELFCLASS32
272 && e_ident[EI_CLASS] != ELFCLASS64)
273 /* We also can only handle two encodings. */
274 || (e_ident[EI_DATA] != ELFDATA2LSB
275 && e_ident[EI_DATA] != ELFDATA2MSB)))
277 /* Cannot handle this. */
278 __libelf_seterrno (ELF_E_INVALID_FILE);
282 /* Determine the number of sections. */
283 size_t scncnt = get_shnum (map_address, e_ident, fildes, offset, maxsize);
284 if (scncnt == (size_t) -1l)
285 /* Could not determine the number of sections. */
288 /* We can now allocate the memory. Even if there are no section headers,
289 we allocate space for a zeroth section in case we need it later. */
290 const size_t scnmax = (scncnt ?: (cmd == ELF_C_RDWR || cmd == ELF_C_RDWR_MMAP)
292 Elf *elf = allocate_elf (fildes, map_address, offset, maxsize, cmd, parent,
293 ELF_K_ELF, scnmax * sizeof (Elf_Scn));
295 /* Not enough memory. */
298 assert ((unsigned int) scncnt == scncnt);
299 assert (offsetof (struct Elf, state.elf32.scns)
300 == offsetof (struct Elf, state.elf64.scns));
301 elf->state.elf32.scns.cnt = scncnt;
302 elf->state.elf32.scns.max = scnmax;
304 /* Some more or less arbitrary value. */
305 elf->state.elf.scnincr = 10;
307 /* Make the class easily available. */
308 elf->class = e_ident[EI_CLASS];
310 if (e_ident[EI_CLASS] == ELFCLASS32)
312 /* This pointer might not be directly usable if the alignment is
313 not sufficient for the architecture. */
314 Elf32_Ehdr *ehdr = (Elf32_Ehdr *) ((char *) map_address + offset);
316 /* This is a 32-bit binary. */
317 if (map_address != NULL && e_ident[EI_DATA] == MY_ELFDATA
319 || ((((uintptr_t) ehdr) & (__alignof__ (Elf32_Ehdr) - 1)) == 0
320 && ((uintptr_t) ((char *) ehdr + ehdr->e_shoff)
321 & (__alignof__ (Elf32_Shdr) - 1)) == 0
322 && ((uintptr_t) ((char *) ehdr + ehdr->e_phoff)
323 & (__alignof__ (Elf32_Phdr) - 1)) == 0)))
325 /* We can use the mmapped memory. */
326 elf->state.elf32.ehdr = ehdr;
327 elf->state.elf32.shdr
328 = (Elf32_Shdr *) ((char *) ehdr + ehdr->e_shoff);
330 /* Don't precache the phdr pointer here.
331 elf32_getphdr will validate it against the size when asked. */
333 for (size_t cnt = 0; cnt < scncnt; ++cnt)
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];
339 if (likely (elf->state.elf32.shdr[cnt].sh_offset < maxsize)
340 && likely (maxsize - elf->state.elf32.shdr[cnt].sh_offset
341 <= elf->state.elf32.shdr[cnt].sh_size))
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);
346 elf->state.elf32.scns.data[cnt].list = &elf->state.elf32.scns;
348 /* If this is a section with an extended index add a
349 reference in the section which uses the extended
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
356 /* Set the own shndx_index field in case it has not yet
358 if (elf->state.elf32.scns.data[cnt].shndx_index == 0)
359 elf->state.elf32.scns.data[cnt].shndx_index = -1;
364 /* Copy the ELF header. */
365 elf->state.elf32.ehdr = memcpy (&elf->state.elf32.ehdr_mem, e_ident,
366 sizeof (Elf32_Ehdr));
368 if (e_ident[EI_DATA] != MY_ELFDATA)
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);
385 for (size_t cnt = 0; cnt < scncnt; ++cnt)
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;
393 /* So far only one block with sections. */
394 elf->state.elf32.scns_last = &elf->state.elf32.scns;
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);
402 /* This is a 64-bit binary. */
403 if (map_address != NULL && e_ident[EI_DATA] == MY_ELFDATA
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)))
411 /* We can use the mmapped memory. */
412 elf->state.elf64.ehdr = ehdr;
413 elf->state.elf64.shdr
414 = (Elf64_Shdr *) ((char *) ehdr + ehdr->e_shoff);
416 /* Don't precache the phdr pointer here.
417 elf64_getphdr will validate it against the size when asked. */
419 for (size_t cnt = 0; cnt < scncnt; ++cnt)
421 elf->state.elf64.scns.data[cnt].index = cnt;
422 elf->state.elf64.scns.data[cnt].elf = elf;
423 elf->state.elf64.scns.data[cnt].shdr.e64 =
424 &elf->state.elf64.shdr[cnt];
425 if (likely (elf->state.elf64.shdr[cnt].sh_offset < maxsize)
426 && likely (maxsize - elf->state.elf64.shdr[cnt].sh_offset
427 <= elf->state.elf64.shdr[cnt].sh_size))
428 elf->state.elf64.scns.data[cnt].rawdata_base =
429 elf->state.elf64.scns.data[cnt].data_base =
430 ((char *) map_address + offset
431 + elf->state.elf64.shdr[cnt].sh_offset);
432 elf->state.elf64.scns.data[cnt].list = &elf->state.elf64.scns;
434 /* If this is a section with an extended index add a
435 reference in the section which uses the extended
437 if (elf->state.elf64.shdr[cnt].sh_type == SHT_SYMTAB_SHNDX
438 && elf->state.elf64.shdr[cnt].sh_link < scncnt)
439 elf->state.elf64.scns.data[elf->state.elf64.shdr[cnt].sh_link].shndx_index
442 /* Set the own shndx_index field in case it has not yet
444 if (elf->state.elf64.scns.data[cnt].shndx_index == 0)
445 elf->state.elf64.scns.data[cnt].shndx_index = -1;
450 /* Copy the ELF header. */
451 elf->state.elf64.ehdr = memcpy (&elf->state.elf64.ehdr_mem, e_ident,
452 sizeof (Elf64_Ehdr));
454 if (e_ident[EI_DATA] != MY_ELFDATA)
456 CONVERT (elf->state.elf64.ehdr_mem.e_type);
457 CONVERT (elf->state.elf64.ehdr_mem.e_machine);
458 CONVERT (elf->state.elf64.ehdr_mem.e_version);
459 CONVERT (elf->state.elf64.ehdr_mem.e_entry);
460 CONVERT (elf->state.elf64.ehdr_mem.e_phoff);
461 CONVERT (elf->state.elf64.ehdr_mem.e_shoff);
462 CONVERT (elf->state.elf64.ehdr_mem.e_flags);
463 CONVERT (elf->state.elf64.ehdr_mem.e_ehsize);
464 CONVERT (elf->state.elf64.ehdr_mem.e_phentsize);
465 CONVERT (elf->state.elf64.ehdr_mem.e_phnum);
466 CONVERT (elf->state.elf64.ehdr_mem.e_shentsize);
467 CONVERT (elf->state.elf64.ehdr_mem.e_shnum);
468 CONVERT (elf->state.elf64.ehdr_mem.e_shstrndx);
471 for (size_t cnt = 0; cnt < scncnt; ++cnt)
473 elf->state.elf64.scns.data[cnt].index = cnt;
474 elf->state.elf64.scns.data[cnt].elf = elf;
475 elf->state.elf64.scns.data[cnt].list = &elf->state.elf64.scns;
479 /* So far only one block with sections. */
480 elf->state.elf64.scns_last = &elf->state.elf64.scns;
489 __libelf_read_mmaped_file (int fildes, void *map_address, off_t offset,
490 size_t maxsize, Elf_Cmd cmd, Elf *parent)
492 /* We have to find out what kind of file this is. We handle ELF
493 files and archives. To find out what we have we must look at the
494 header. The header for an ELF file is EI_NIDENT bytes in size,
495 the header for an archive file SARMAG bytes long. */
496 unsigned char *e_ident = (unsigned char *) map_address + offset;
498 /* See what kind of object we have here. */
499 Elf_Kind kind = determine_kind (e_ident, maxsize);
504 return file_read_elf (fildes, map_address, e_ident, offset, maxsize,
508 return file_read_ar (fildes, map_address, offset, maxsize, cmd, parent);
514 /* This case is easy. Since we cannot do anything with this file
515 create a dummy descriptor. */
516 return allocate_elf (fildes, map_address, offset, maxsize, cmd, parent,
522 read_unmmaped_file (int fildes, off_t offset, size_t maxsize, Elf_Cmd cmd,
525 /* We have to find out what kind of file this is. We handle ELF
526 files and archives. To find out what we have we must read the
527 header. The identification header for an ELF file is EI_NIDENT
528 bytes in size, but we read the whole ELF header since we will
529 need it anyway later. For archives the header in SARMAG bytes
530 long. Read the maximum of these numbers.
532 XXX We have to change this for the extended `ar' format some day.
534 Use a union to ensure alignment. We might later access the
535 memory as a ElfXX_Ehdr. */
539 unsigned char header[MAX (sizeof (Elf64_Ehdr), SARMAG)];
542 /* Read the head of the file. */
543 ssize_t nread = pread_retry (fildes, mem.header,
544 MIN (MAX (sizeof (Elf64_Ehdr), SARMAG),
547 if (unlikely (nread == -1))
548 /* We cannot even read the head of the file. Maybe FILDES is associated
549 with an unseekable device. This is nothing we can handle. */
552 /* See what kind of object we have here. */
553 Elf_Kind kind = determine_kind (mem.header, nread);
558 return file_read_ar (fildes, NULL, offset, maxsize, cmd, parent);
561 /* Make sure at least the ELF header is contained in the file. */
562 if ((size_t) nread >= (mem.header[EI_CLASS] == ELFCLASS32
563 ? sizeof (Elf32_Ehdr) : sizeof (Elf64_Ehdr)))
564 return file_read_elf (fildes, NULL, mem.header, offset, maxsize, cmd,
572 /* This case is easy. Since we cannot do anything with this file
573 create a dummy descriptor. */
574 return allocate_elf (fildes, NULL, offset, maxsize, cmd, parent,
579 /* Open a file for reading. If possible we will try to mmap() the file. */
581 read_file (int fildes, off_t offset, size_t maxsize,
582 Elf_Cmd cmd, Elf *parent)
584 void *map_address = NULL;
585 int use_mmap = (cmd == ELF_C_READ_MMAP || cmd == ELF_C_RDWR_MMAP
586 || cmd == ELF_C_WRITE_MMAP
587 || cmd == ELF_C_READ_MMAP_PRIVATE);
590 /* Mudflap doesn't grok that our mmap'd data is ok. */
598 if (maxsize == ~((size_t) 0))
600 /* We don't know in the moment how large the file is.
604 if (fstat (fildes, &st) == 0
605 && (sizeof (size_t) >= sizeof (st.st_size)
606 || st.st_size <= ~((size_t) 0)))
607 maxsize = (size_t) st.st_size;
610 /* We try to map the file ourself. */
611 map_address = mmap (NULL, maxsize, (cmd == ELF_C_READ_MMAP
613 : PROT_READ|PROT_WRITE),
614 cmd == ELF_C_READ_MMAP_PRIVATE
615 || cmd == ELF_C_READ_MMAP
616 ? MAP_PRIVATE : MAP_SHARED,
619 if (map_address == MAP_FAILED)
624 /* The parent is already loaded. Use it. */
625 assert (maxsize != ~((size_t) 0));
627 map_address = parent->map_address;
631 /* If we have the file in memory optimize the access. */
632 if (map_address != NULL)
634 assert (map_address != MAP_FAILED);
636 struct Elf *result = __libelf_read_mmaped_file (fildes, map_address,
637 offset, maxsize, cmd,
640 /* If something went wrong during the initialization unmap the
641 memory if we mmaped here. */
644 || parent->map_address != map_address))
645 munmap (map_address, maxsize);
646 else if (parent == NULL)
647 /* Remember that we mmap()ed the memory. */
648 result->flags |= ELF_F_MMAPPED;
653 /* Otherwise we have to do it the hard way. We read as much as necessary
654 from the file whenever we need information which is not available. */
655 return read_unmmaped_file (fildes, offset, maxsize, cmd, parent);
659 /* Find the entry with the long names for the content of this archive. */
661 read_long_names (Elf *elf)
663 off_t offset = SARMAG; /* This is the first entry. */
671 if (elf->map_address != NULL)
673 if (offset + sizeof (struct ar_hdr) > elf->maximum_size)
676 /* The data is mapped. */
677 hdr = (struct ar_hdr *) (elf->map_address + offset);
681 /* Read the header from the file. */
682 if (unlikely (pread_retry (elf->fildes, &hdrm, sizeof (hdrm),
683 elf->start_offset + offset)
690 len = atol (hdr->ar_size);
692 if (memcmp (hdr->ar_name, "// ", 16) == 0)
695 offset += sizeof (struct ar_hdr) + ((len + 1) & ~1l);
698 /* Due to the stupid format of the long name table entry (which are not
699 NUL terminted) we have to provide an appropriate representation anyhow.
700 Therefore we always make a copy which has the appropriate form. */
701 newp = (char *) malloc (len);
706 if (elf->map_address != NULL)
707 /* Simply copy it over. */
708 elf->state.ar.long_names = (char *) memcpy (newp,
709 elf->map_address + offset
710 + sizeof (struct ar_hdr),
714 if (unlikely ((size_t) pread_retry (elf->fildes, newp, len,
715 elf->start_offset + offset
716 + sizeof (struct ar_hdr))
719 /* We were not able to read all data. */
721 elf->state.ar.long_names = NULL;
724 elf->state.ar.long_names = newp;
727 elf->state.ar.long_names_len = len;
729 /* Now NUL-terminate the strings. */
733 runp = (char *) memchr (runp, '/', newp + len - runp);
735 /* This was the last entry. */
738 /* NUL-terminate the string. */
741 /* Skip the NUL byte and the \012. */
744 /* A sanity check. Somebody might have generated invalid
746 if (runp >= newp + len)
755 /* Read the next archive header. */
758 __libelf_next_arhdr_wrlock (elf)
761 struct ar_hdr *ar_hdr;
762 Elf_Arhdr *elf_ar_hdr;
764 if (elf->map_address != NULL)
766 /* See whether this entry is in the file. */
767 if (unlikely (elf->state.ar.offset + sizeof (struct ar_hdr)
768 > elf->start_offset + elf->maximum_size))
770 /* This record is not anymore in the file. */
771 __libelf_seterrno (ELF_E_RANGE);
774 ar_hdr = (struct ar_hdr *) (elf->map_address + elf->state.ar.offset);
778 ar_hdr = &elf->state.ar.ar_hdr;
780 if (unlikely (pread_retry (elf->fildes, ar_hdr, sizeof (struct ar_hdr),
781 elf->state.ar.offset)
782 != sizeof (struct ar_hdr)))
784 /* Something went wrong while reading the file. */
785 __libelf_seterrno (ELF_E_RANGE);
790 /* One little consistency check. */
791 if (unlikely (memcmp (ar_hdr->ar_fmag, ARFMAG, 2) != 0))
793 /* This is no valid archive. */
794 __libelf_seterrno (ELF_E_ARCHIVE_FMAG);
798 /* Copy the raw name over to a NUL terminated buffer. */
799 *((char *) __mempcpy (elf->state.ar.raw_name, ar_hdr->ar_name, 16)) = '\0';
801 elf_ar_hdr = &elf->state.ar.elf_ar_hdr;
803 /* Now convert the `struct ar_hdr' into `Elf_Arhdr'.
804 Determine whether this is a special entry. */
805 if (ar_hdr->ar_name[0] == '/')
807 if (ar_hdr->ar_name[1] == ' '
808 && memcmp (ar_hdr->ar_name, "/ ", 16) == 0)
809 /* This is the index. */
810 elf_ar_hdr->ar_name = memcpy (elf->state.ar.ar_name, "/", 2);
811 else if (ar_hdr->ar_name[1] == '/'
812 && memcmp (ar_hdr->ar_name, "// ", 16) == 0)
813 /* This is the array with the long names. */
814 elf_ar_hdr->ar_name = memcpy (elf->state.ar.ar_name, "//", 3);
815 else if (likely (isdigit (ar_hdr->ar_name[1])))
819 /* This is a long name. First we have to read the long name
820 table, if this hasn't happened already. */
821 if (unlikely (elf->state.ar.long_names == NULL
822 && read_long_names (elf) == NULL))
824 /* No long name table although it is reference. The archive is
826 __libelf_seterrno (ELF_E_INVALID_ARCHIVE);
830 offset = atol (ar_hdr->ar_name + 1);
831 if (unlikely (offset >= elf->state.ar.long_names_len))
833 /* The index in the long name table is larger than the table. */
834 __libelf_seterrno (ELF_E_INVALID_ARCHIVE);
837 elf_ar_hdr->ar_name = elf->state.ar.long_names + offset;
841 /* This is none of the known special entries. */
842 __libelf_seterrno (ELF_E_INVALID_ARCHIVE);
850 /* It is a normal entry. Copy over the name. */
851 endp = (char *) memccpy (elf->state.ar.ar_name, ar_hdr->ar_name,
857 /* In the old BSD style of archive, there is no / terminator.
858 Instead, there is space padding at the end of the name. */
861 elf->state.ar.ar_name[i] = '\0';
862 while (i > 0 && elf->state.ar.ar_name[--i] == ' ');
865 elf_ar_hdr->ar_name = elf->state.ar.ar_name;
868 if (unlikely (ar_hdr->ar_size[0] == ' '))
869 /* Something is really wrong. We cannot live without a size for
870 the member since it will not be possible to find the next
873 __libelf_seterrno (ELF_E_INVALID_ARCHIVE);
877 /* Since there are no specialized functions to convert ASCII to
878 time_t, uid_t, gid_t, mode_t, and off_t we use either atol or
879 atoll depending on the size of the types. We are also prepared
880 for the case where the whole field in the `struct ar_hdr' is
881 filled in which case we cannot simply use atol/l but instead have
882 to create a temporary copy. */
884 #define INT_FIELD(FIELD) \
887 char buf[sizeof (ar_hdr->FIELD) + 1]; \
888 const char *string = ar_hdr->FIELD; \
889 if (ar_hdr->FIELD[sizeof (ar_hdr->FIELD) - 1] != ' ') \
891 *((char *) __mempcpy (buf, ar_hdr->FIELD, sizeof (ar_hdr->FIELD))) \
895 if (sizeof (elf_ar_hdr->FIELD) <= sizeof (long int)) \
896 elf_ar_hdr->FIELD = (__typeof (elf_ar_hdr->FIELD)) atol (string); \
898 elf_ar_hdr->FIELD = (__typeof (elf_ar_hdr->FIELD)) atoll (string); \
912 /* We were asked to return a clone of an existing descriptor. This
913 function must be called with the lock on the parent descriptor
916 dup_elf (int fildes, Elf_Cmd cmd, Elf *ref)
921 /* Allow the user to pass -1 as the file descriptor for the new file. */
922 fildes = ref->fildes;
923 /* The file descriptor better should be the same. If it was disconnected
924 already (using `elf_cntl') we do not test it. */
925 else if (unlikely (ref->fildes != -1 && fildes != ref->fildes))
927 __libelf_seterrno (ELF_E_FD_MISMATCH);
931 /* The mode must allow reading. I.e., a descriptor creating with a
932 command different then ELF_C_READ, ELF_C_WRITE and ELF_C_RDWR is
934 if (unlikely (ref->cmd != ELF_C_READ && ref->cmd != ELF_C_READ_MMAP
935 && ref->cmd != ELF_C_WRITE && ref->cmd != ELF_C_WRITE_MMAP
936 && ref->cmd != ELF_C_RDWR && ref->cmd != ELF_C_RDWR_MMAP
937 && ref->cmd != ELF_C_READ_MMAP_PRIVATE))
939 __libelf_seterrno (ELF_E_INVALID_OP);
943 /* Now it is time to distinguish between reading normal files and
944 archives. Normal files can easily be handled be incrementing the
945 reference counter and return the same descriptor. */
946 if (ref->kind != ELF_K_AR)
952 /* This is an archive. We must create a descriptor for the archive
953 member the internal pointer of the archive file desriptor is
954 pointing to. First read the header of the next member if this
955 has not happened already. */
956 if (ref->state.ar.elf_ar_hdr.ar_name == NULL
957 && __libelf_next_arhdr_wrlock (ref) != 0)
958 /* Something went wrong. Maybe there is no member left. */
961 /* We have all the information we need about the next archive member.
962 Now create a descriptor for it. */
963 result = read_file (fildes, ref->state.ar.offset + sizeof (struct ar_hdr),
964 ref->state.ar.elf_ar_hdr.ar_size, cmd, ref);
966 /* Enlist this new descriptor in the list of children. */
969 result->next = ref->state.ar.children;
970 ref->state.ar.children = result;
977 /* Return desriptor for empty file ready for writing. */
979 write_file (int fd, Elf_Cmd cmd)
981 /* We simply create an empty `Elf' structure. */
982 #define NSCNSALLOC 10
983 Elf *result = allocate_elf (fd, NULL, 0, 0, cmd, NULL, ELF_K_ELF,
984 NSCNSALLOC * sizeof (Elf_Scn));
988 /* We have to write to the file in any case. */
989 result->flags = ELF_F_DIRTY;
991 /* Some more or less arbitrary value. */
992 result->state.elf.scnincr = NSCNSALLOC;
994 /* We have allocated room for some sections. */
995 assert (offsetof (struct Elf, state.elf32.scns)
996 == offsetof (struct Elf, state.elf64.scns));
997 result->state.elf.scns_last = &result->state.elf32.scns;
998 result->state.elf32.scns.max = NSCNSALLOC;
1005 /* Return a descriptor for the file belonging to FILDES. */
1007 elf_begin (fildes, cmd, ref)
1014 if (unlikely (! __libelf_version_initialized))
1016 /* Version wasn't set so far. */
1017 __libelf_seterrno (ELF_E_NO_VERSION);
1022 /* Make sure the descriptor is not suddenly going away. */
1023 rwlock_rdlock (ref->lock);
1024 else if (unlikely (fcntl (fildes, F_GETFL) == -1 && errno == EBADF))
1026 /* We cannot do anything productive without a file descriptor. */
1027 __libelf_seterrno (ELF_E_INVALID_FILE);
1031 Elf *lock_dup_elf ()
1033 /* We need wrlock to dup an archive. */
1034 if (ref->kind == ELF_K_AR)
1036 rwlock_unlock (ref->lock);
1037 rwlock_wrlock (ref->lock);
1040 /* Duplicate the descriptor. */
1041 return dup_elf (fildes, cmd, ref);
1047 /* We simply return a NULL pointer. */
1051 case ELF_C_READ_MMAP_PRIVATE:
1052 /* If we have a reference it must also be opened this way. */
1053 if (unlikely (ref != NULL && ref->cmd != ELF_C_READ_MMAP_PRIVATE))
1055 __libelf_seterrno (ELF_E_INVALID_CMD);
1062 case ELF_C_READ_MMAP:
1064 retval = lock_dup_elf ();
1066 /* Create descriptor for existing file. */
1067 retval = read_file (fildes, 0, ~((size_t) 0), cmd, NULL);
1071 case ELF_C_RDWR_MMAP:
1072 /* If we have a REF object it must also be opened using this
1076 if (unlikely (ref->cmd != ELF_C_RDWR && ref->cmd != ELF_C_RDWR_MMAP
1077 && ref->cmd != ELF_C_WRITE
1078 && ref->cmd != ELF_C_WRITE_MMAP))
1080 /* This is not ok. REF must also be opened for writing. */
1081 __libelf_seterrno (ELF_E_INVALID_CMD);
1085 retval = lock_dup_elf ();
1088 /* Create descriptor for existing file. */
1089 retval = read_file (fildes, 0, ~((size_t) 0), cmd, NULL);
1093 case ELF_C_WRITE_MMAP:
1094 /* We ignore REF and prepare a descriptor to write a new file. */
1095 retval = write_file (fildes, cmd);
1099 __libelf_seterrno (ELF_E_INVALID_CMD);
1104 /* Release the lock. */
1106 rwlock_unlock (ref->lock);