blob: 0101618826d0df7a6adf9a6e00d86e61fa49427e [file] [log] [blame]
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001/* Read all of the file associated with the descriptor.
Roland McGrath0c6db7e2009-11-10 13:34:09 -08002 Copyright (C) 1998-2009 Red Hat, Inc.
Mark Wielaardde2ed972012-06-05 17:15:16 +02003 This file is part of elfutils.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00004 Contributed by Ulrich Drepper <drepper@redhat.com>, 1998.
5
Mark Wielaardde2ed972012-06-05 17:15:16 +02006 This file is free software; you can redistribute it and/or modify
7 it under the terms of either
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00008
Mark Wielaardde2ed972012-06-05 17:15:16 +02009 * 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 Drepper361df7d2006-04-04 21:38:57 +000022 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 Drepperb08d5a82005-07-26 05:00:05 +000025
Mark Wielaardde2ed972012-06-05 17:15:16 +020026 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 Drepperb08d5a82005-07-26 05:00:05 +000029
30#ifdef HAVE_CONFIG_H
31# include <config.h>
32#endif
33
Ulrich Drepperfbe998a2005-08-29 16:27:10 +000034#include <errno.h>
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000035#include <unistd.h>
Roland McGrath0c6db7e2009-11-10 13:34:09 -080036#include <sys/stat.h>
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000037
Ulrich Drepperfbe998a2005-08-29 16:27:10 +000038#include <system.h>
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000039#include "libelfP.h"
40#include "common.h"
41
42
43static void
44set_address (Elf *elf, size_t offset)
45{
46 if (elf->kind == ELF_K_AR)
47 {
48 Elf *child = elf->state.ar.children;
49
50 while (child != NULL)
51 {
52 if (child->map_address == NULL)
53 {
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;
58
59 set_address (child, offset);
60 }
61
62 child = child->next;
63 }
64 }
65}
66
67
68char *
69__libelf_readall (elf)
70 Elf *elf;
71{
72 /* Get the file. */
Roland McGrathb4d6f0f2008-08-25 22:55:17 +000073 rwlock_wrlock (elf->lock);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000074
75 if (elf->map_address == NULL && unlikely (elf->fildes == -1))
76 {
77 __libelf_seterrno (ELF_E_INVALID_HANDLE);
Roland McGrathb4d6f0f2008-08-25 22:55:17 +000078 rwlock_unlock (elf->lock);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000079 return NULL;
80 }
81
82 /* If the file is not mmap'ed and not previously loaded, do it now. */
83 if (elf->map_address == NULL)
84 {
Roland McGrath0c6db7e2009-11-10 13:34:09 -080085 char *mem = NULL;
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000086
87 /* If this is an archive and we have derived descriptors get the
88 locks for all of them. */
89 libelf_acquire_all (elf);
90
Roland McGrath0c6db7e2009-11-10 13:34:09 -080091 if (elf->maximum_size == ~((size_t) 0))
92 {
93 /* We don't yet know how large the file is. Determine that now. */
94 struct stat st;
95
96 if (fstat (elf->fildes, &st) < 0)
97 goto read_error;
98
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;
102 else
103 {
104 errno = EOVERFLOW;
105 goto read_error;
106 }
107 }
108
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000109 /* Allocate all the memory we need. */
110 mem = (char *) malloc (elf->maximum_size);
111 if (mem != NULL)
112 {
113 /* Read the file content. */
Ulrich Drepperfbe998a2005-08-29 16:27:10 +0000114 if (unlikely ((size_t) pread_retry (elf->fildes, mem,
115 elf->maximum_size,
116 elf->start_offset)
117 != elf->maximum_size))
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000118 {
119 /* Something went wrong. */
Roland McGrath0c6db7e2009-11-10 13:34:09 -0800120 read_error:
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000121 __libelf_seterrno (ELF_E_READ_ERROR);
122 free (mem);
123 }
124 else
125 {
126 /* Remember the address. */
127 elf->map_address = mem;
128
129 /* Also remember that we allocated the memory. */
130 elf->flags |= ELF_F_MALLOCED;
131
132 /* Propagate the information down to all children and
133 their children. */
134 set_address (elf, elf->start_offset);
135
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;
140 }
141 }
142 else
143 __libelf_seterrno (ELF_E_NOMEM);
144
145 /* Free the locks on the children. */
146 libelf_release_all (elf);
147 }
148
Roland McGrathb4d6f0f2008-08-25 22:55:17 +0000149 rwlock_unlock (elf->lock);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000150
151 return (char *) elf->map_address;
152}