1 /* Read all of the file associated with the descriptor.
2 Copyright (C) 1998-2009 Red Hat, Inc.
3 This file is part of elfutils.
4 Contributed 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/>. */
44 set_address (Elf *elf, size_t offset)
46 if (elf->kind == ELF_K_AR)
48 Elf *child = elf->state.ar.children;
52 if (child->map_address == NULL)
54 child->map_address = elf->map_address;
55 child->start_offset -= offset;
56 if (child->kind == ELF_K_AR)
57 child->state.ar.offset -= offset;
59 set_address (child, offset);
69 __libelf_readall (elf)
73 rwlock_wrlock (elf->lock);
75 if (elf->map_address == NULL && unlikely (elf->fildes == -1))
77 __libelf_seterrno (ELF_E_INVALID_HANDLE);
78 rwlock_unlock (elf->lock);
82 /* If the file is not mmap'ed and not previously loaded, do it now. */
83 if (elf->map_address == NULL)
87 /* If this is an archive and we have derived descriptors get the
88 locks for all of them. */
89 libelf_acquire_all (elf);
91 if (elf->maximum_size == ~((size_t) 0))
93 /* We don't yet know how large the file is. Determine that now. */
96 if (fstat (elf->fildes, &st) < 0)
99 if (sizeof (size_t) >= sizeof (st.st_size)
100 || st.st_size <= ~((size_t) 0))
101 elf->maximum_size = (size_t) st.st_size;
109 /* Allocate all the memory we need. */
110 mem = (char *) malloc (elf->maximum_size);
113 /* Read the file content. */
114 if (unlikely ((size_t) pread_retry (elf->fildes, mem,
117 != elf->maximum_size))
119 /* Something went wrong. */
121 __libelf_seterrno (ELF_E_READ_ERROR);
126 /* Remember the address. */
127 elf->map_address = mem;
129 /* Also remember that we allocated the memory. */
130 elf->flags |= ELF_F_MALLOCED;
132 /* Propagate the information down to all children and
134 set_address (elf, elf->start_offset);
136 /* Correct the own offsets. */
137 if (elf->kind == ELF_K_AR)
138 elf->state.ar.offset -= elf->start_offset;
139 elf->start_offset = 0;
143 __libelf_seterrno (ELF_E_NOMEM);
145 /* Free the locks on the children. */
146 libelf_release_all (elf);
149 rwlock_unlock (elf->lock);
151 return (char *) elf->map_address;