* ld-discard/static.d: Accept both original sym and section sym.
[deliverable/binutils-gdb.git] / mmalloc / attach.c
1 /* Initialization for access to a mmap'd malloc managed region.
2 Copyright 1992, 2000 Free Software Foundation, Inc.
3
4 Contributed by Fred Fish at Cygnus Support. fnf@cygnus.com
5
6 This file is part of the GNU C Library.
7
8 The GNU C Library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version.
12
13 The GNU C Library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Library General Public License for more details.
17
18 You should have received a copy of the GNU Library General Public
19 License along with the GNU C Library; see the file COPYING.LIB. If
20 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
22
23 #include <sys/types.h>
24 #include <fcntl.h> /* After sys/types.h, at least for dpx/2. */
25 #include <sys/stat.h>
26 #include <string.h>
27 #ifdef HAVE_UNISTD_H
28 #include <unistd.h> /* Prototypes for lseek */
29 #endif
30 #include "mmprivate.h"
31
32 #ifndef SEEK_SET
33 #define SEEK_SET 0
34 #endif
35
36
37 #if defined(HAVE_MMAP)
38
39 /* Forward declarations/prototypes for local functions */
40
41 static struct mdesc *reuse PARAMS ((int));
42
43 /* Initialize access to a mmalloc managed region.
44
45 If FD is a valid file descriptor for an open file then data for the
46 mmalloc managed region is mapped to that file, otherwise "/dev/zero"
47 is used and the data will not exist in any filesystem object.
48
49 If the open file corresponding to FD is from a previous use of
50 mmalloc and passes some basic sanity checks to ensure that it is
51 compatible with the current mmalloc package, then it's data is
52 mapped in and is immediately accessible at the same addresses in
53 the current process as the process that created the file.
54
55 If BASEADDR is not NULL, the mapping is established starting at the
56 specified address in the process address space. If BASEADDR is NULL,
57 the mmalloc package chooses a suitable address at which to start the
58 mapped region, which will be the value of the previous mapping if
59 opening an existing file which was previously built by mmalloc, or
60 for new files will be a value chosen by mmap.
61
62 Specifying BASEADDR provides more control over where the regions
63 start and how big they can be before bumping into existing mapped
64 regions or future mapped regions.
65
66 On success, returns a "malloc descriptor" which is used in subsequent
67 calls to other mmalloc package functions. It is explicitly "void *"
68 ("char *" for systems that don't fully support void) so that users
69 of the package don't have to worry about the actual implementation
70 details.
71
72 On failure returns NULL. */
73
74 PTR
75 mmalloc_attach (fd, baseaddr)
76 int fd;
77 PTR baseaddr;
78 {
79 struct mdesc mtemp;
80 struct mdesc *mdp;
81 PTR mbase;
82 struct stat sbuf;
83
84 /* First check to see if FD is a valid file descriptor, and if so, see
85 if the file has any current contents (size > 0). If it does, then
86 attempt to reuse the file. If we can't reuse the file, either
87 because it isn't a valid mmalloc produced file, was produced by an
88 obsolete version, or any other reason, then we fail to attach to
89 this file. */
90
91 if (fd >= 0)
92 {
93 if (fstat (fd, &sbuf) < 0)
94 {
95 return (NULL);
96 }
97 else if (sbuf.st_size > 0)
98 {
99 return ((PTR) reuse (fd));
100 }
101 }
102
103 /* We start off with the malloc descriptor allocated on the stack, until
104 we build it up enough to call _mmalloc_mmap_morecore() to allocate the
105 first page of the region and copy it there. Ensure that it is zero'd and
106 then initialize the fields that we know values for. */
107
108 mdp = &mtemp;
109 memset ((char *) mdp, 0, sizeof (mtemp));
110 strncpy (mdp -> magic, MMALLOC_MAGIC, MMALLOC_MAGIC_SIZE);
111 mdp -> headersize = sizeof (mtemp);
112 mdp -> version = MMALLOC_VERSION;
113 mdp -> morecore = __mmalloc_mmap_morecore;
114 mdp -> fd = fd;
115 mdp -> base = mdp -> breakval = mdp -> top = baseaddr;
116
117 /* If we have not been passed a valid open file descriptor for the file
118 to map to, then open /dev/zero and use that to map to. */
119
120 if (mdp -> fd < 0)
121 {
122 if ((mdp -> fd = open ("/dev/zero", O_RDWR)) < 0)
123 {
124 return (NULL);
125 }
126 else
127 {
128 mdp -> flags |= MMALLOC_DEVZERO;
129 }
130 }
131
132 /* Now try to map in the first page, copy the malloc descriptor structure
133 there, and arrange to return a pointer to this new copy. If the mapping
134 fails, then close the file descriptor if it was opened by us, and arrange
135 to return a NULL. */
136
137 if ((mbase = mdp -> morecore (mdp, sizeof (mtemp))) != NULL)
138 {
139 memcpy (mbase, mdp, sizeof (mtemp));
140 mdp = (struct mdesc *) mbase;
141 }
142 else
143 {
144 if (mdp -> flags & MMALLOC_DEVZERO)
145 {
146 close (mdp -> fd);
147 }
148 mdp = NULL;
149 }
150
151 return ((PTR) mdp);
152 }
153
154 /* Given an valid file descriptor on an open file, test to see if that file
155 is a valid mmalloc produced file, and if so, attempt to remap it into the
156 current process at the same address to which it was previously mapped.
157
158 Note that we have to update the file descriptor number in the malloc-
159 descriptor read from the file to match the current valid one, before
160 trying to map the file in, and again after a successful mapping and
161 after we've switched over to using the mapped in malloc descriptor
162 rather than the temporary one on the stack.
163
164 Once we've switched over to using the mapped in malloc descriptor, we
165 have to update the pointer to the morecore function, since it almost
166 certainly will be at a different address if the process reusing the
167 mapped region is from a different executable.
168
169 Also note that if the heap being remapped previously used the mmcheckf()
170 routines, we need to update the hooks since their target functions
171 will have certainly moved if the executable has changed in any way.
172 We do this by calling mmcheckf() internally.
173
174 Returns a pointer to the malloc descriptor if successful, or NULL if
175 unsuccessful for some reason. */
176
177 static struct mdesc *
178 reuse (fd)
179 int fd;
180 {
181 struct mdesc mtemp;
182 struct mdesc *mdp = NULL;
183
184 if ((lseek (fd, 0L, SEEK_SET) == 0) &&
185 (read (fd, (char *) &mtemp, sizeof (mtemp)) == sizeof (mtemp)) &&
186 (mtemp.headersize == sizeof (mtemp)) &&
187 (strcmp (mtemp.magic, MMALLOC_MAGIC) == 0) &&
188 (mtemp.version <= MMALLOC_VERSION))
189 {
190 mtemp.fd = fd;
191 if (__mmalloc_remap_core (&mtemp) == mtemp.base)
192 {
193 mdp = (struct mdesc *) mtemp.base;
194 mdp -> fd = fd;
195 mdp -> morecore = __mmalloc_mmap_morecore;
196 if (mdp -> mfree_hook != NULL)
197 {
198 mmcheckf ((PTR) mdp, (void (*) PARAMS ((void))) NULL, 1);
199 }
200 }
201 }
202 return (mdp);
203 }
204
205 #else /* !defined (HAVE_MMAP) */
206
207 /* For systems without mmap, the library still supplies an entry point
208 to link to, but trying to initialize access to an mmap'd managed region
209 always fails. */
210
211 /* ARGSUSED */
212 PTR
213 mmalloc_attach (fd, baseaddr)
214 int fd;
215 PTR baseaddr;
216 {
217 return (NULL);
218 }
219
220 #endif /* defined (HAVE_MMAP) */
221
This page took 0.034653 seconds and 4 git commands to generate.