98426153de51f366cd330e1da9db6dc2660517a6
[deliverable/binutils-gdb.git] / gdb / objfiles.c
1 /* GDB routines for manipulating objfiles.
2 Copyright 1992 Free Software Foundation, Inc.
3 Contributed by Cygnus Support, using pieces from other GDB modules.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21 /* This file contains support routines for creating, manipulating, and
22 destroying objfile structures. */
23
24 #include "defs.h"
25 #include "bfd.h" /* Binary File Description */
26 #include "symtab.h"
27 #include "symfile.h"
28 #include "objfiles.h"
29
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <fcntl.h>
33 #include <obstack.h>
34
35 /* Prototypes for local functions */
36
37 static int
38 open_mapped_file PARAMS ((char *filename, long mtime, int mapped));
39
40 static CORE_ADDR
41 map_to_address PARAMS ((void));
42
43 /* Externally visible variables that are owned by this module.
44 See declarations in objfile.h for more info. */
45
46 struct objfile *object_files; /* Linked list of all objfiles */
47 struct objfile *current_objfile; /* For symbol file being read in */
48 struct objfile *symfile_objfile; /* Main symbol table loaded from */
49
50 int mapped_symbol_files; /* Try to use mapped symbol files */
51
52 /* Given a pointer to an initialized bfd (ABFD) and a flag that indicates
53 whether or not an objfile is to be mapped (MAPPED), allocate a new objfile
54 struct, fill it in as best we can, link it into the list of all known
55 objfiles, and return a pointer to the new objfile struct. */
56
57 struct objfile *
58 allocate_objfile (abfd, mapped)
59 bfd *abfd;
60 int mapped;
61 {
62 struct objfile *objfile = NULL;
63 int fd;
64 void *md;
65 CORE_ADDR mapto;
66
67 mapped |= mapped_symbol_files;
68
69 #if !defined(NO_MMALLOC) && defined(HAVE_MMAP)
70
71 /* If we can support mapped symbol files, try to open/reopen the mapped file
72 that corresponds to the file from which we wish to read symbols. If the
73 objfile is to be mapped, we must malloc the structure itself using the
74 mmap version, and arrange that all memory allocation for the objfile uses
75 the mmap routines. If we are reusing an existing mapped file, from which
76 we get our objfile pointer, we have to make sure that we update the
77 pointers to the alloc/free functions in the obstack, in case these
78 functions have moved within the current gdb. */
79
80 fd = open_mapped_file (bfd_get_filename (abfd), bfd_get_mtime (abfd),
81 mapped);
82 if (fd >= 0)
83 {
84 if (((mapto = map_to_address ()) == 0) ||
85 ((md = mmalloc_attach (fd, (void *) mapto)) == NULL))
86 {
87 (void) close (fd);
88 }
89 else if ((objfile = (struct objfile *) mmalloc_getkey (md, 0)) != NULL)
90 {
91 /* Update memory corruption handler function addresses. */
92 init_malloc (md);
93 objfile -> md = md;
94 objfile -> mmfd = fd;
95 /* Update pointers to functions to *our* copies */
96 obstack_chunkfun (&objfile -> psymbol_obstack, xmmalloc);
97 obstack_freefun (&objfile -> psymbol_obstack, mfree);
98 obstack_chunkfun (&objfile -> symbol_obstack, xmmalloc);
99 obstack_freefun (&objfile -> symbol_obstack, mfree);
100 obstack_chunkfun (&objfile -> type_obstack, xmmalloc);
101 obstack_freefun (&objfile -> type_obstack, mfree);
102 }
103 else
104 {
105 /* Set up to detect internal memory corruption. MUST be done before
106 the first malloc. See comments in init_malloc() and mmcheck(). */
107 init_malloc (md);
108 objfile = (struct objfile *) xmmalloc (md, sizeof (struct objfile));
109 (void) memset (objfile, 0, sizeof (struct objfile));
110 objfile -> md = md;
111 objfile -> mmfd = fd;
112 objfile -> flags |= OBJF_MAPPED;
113 mmalloc_setkey (objfile -> md, 0, objfile);
114 obstack_full_begin (&objfile -> psymbol_obstack, 0, 0,
115 xmmalloc, mfree, objfile -> md,
116 OBSTACK_MMALLOC_LIKE);
117 obstack_full_begin (&objfile -> symbol_obstack, 0, 0,
118 xmmalloc, mfree, objfile -> md,
119 OBSTACK_MMALLOC_LIKE);
120 obstack_full_begin (&objfile -> type_obstack, 0, 0,
121 xmmalloc, mfree, objfile -> md,
122 OBSTACK_MMALLOC_LIKE);
123 }
124 }
125
126 if (mapped && (objfile == NULL))
127 {
128 warning ("symbol table for '%s' will not be mapped",
129 bfd_get_filename (abfd));
130 }
131
132 #else /* defined(NO_MMALLOC) || !defined(HAVE_MMAP) */
133
134 if (mapped)
135 {
136 warning ("this version of gdb does not support mapped symbol tables.");
137
138 /* Turn off the global flag so we don't try to do mapped symbol tables
139 any more, which shuts up gdb unless the user specifically gives the
140 "mapped" keyword again. */
141
142 mapped_symbol_files = 0;
143 }
144
145 #endif /* !defined(NO_MMALLOC) && defined(HAVE_MMAP) */
146
147 /* If we don't support mapped symbol files, didn't ask for the file to be
148 mapped, or failed to open the mapped file for some reason, then revert
149 back to an unmapped objfile. */
150
151 if (objfile == NULL)
152 {
153 objfile = (struct objfile *) xmalloc (sizeof (struct objfile));
154 (void) memset (objfile, 0, sizeof (struct objfile));
155 objfile -> md = NULL;
156 obstack_full_begin (&objfile -> psymbol_obstack, 0, 0, xmalloc, free,
157 (void *) 0, 0);
158 obstack_full_begin (&objfile -> symbol_obstack, 0, 0, xmalloc, free,
159 (void *) 0, 0);
160 obstack_full_begin (&objfile -> type_obstack, 0, 0, xmalloc, free,
161 (void *) 0, 0);
162
163 }
164
165 /* Update the per-objfile information that comes from the bfd, ensuring
166 that any data that is reference is saved in the per-objfile data
167 region. */
168
169 objfile -> obfd = abfd;
170 if (objfile -> name != NULL)
171 {
172 mfree (objfile -> md, objfile -> name);
173 }
174 objfile -> name = mstrsave (objfile -> md, bfd_get_filename (abfd));
175 objfile -> mtime = bfd_get_mtime (abfd);
176
177 /* Push this file onto the head of the linked list of other such files. */
178
179 objfile -> next = object_files;
180 object_files = objfile;
181
182 return (objfile);
183 }
184
185
186 /* Destroy an objfile and all the symtabs and psymtabs under it. Note
187 that as much as possible is allocated on the symbol_obstack and
188 psymbol_obstack, so that the memory can be efficiently freed.
189
190 Things which we do NOT free because they are not in malloc'd memory
191 or not in memory specific to the objfile include:
192
193 objfile -> sf
194
195 FIXME: If the objfile is using reusable symbol information (via mmalloc),
196 then we need to take into account the fact that more than one process
197 may be using the symbol information at the same time (when mmalloc is
198 extended to support cooperative locking). When more than one process
199 is using the mapped symbol info, we need to be more careful about when
200 we free objects in the reusable area. */
201
202 void
203 free_objfile (objfile)
204 struct objfile *objfile;
205 {
206 struct objfile *ofp;
207 int mmfd;
208
209 /* First do any symbol file specific actions required when we are
210 finished with a particular symbol file. Note that if the objfile
211 is using reusable symbol information (via mmalloc) then each of
212 these routines is responsible for doing the correct thing, either
213 freeing things which are valid only during this particular gdb
214 execution, or leaving them to be reused during the next one. */
215
216 if (objfile -> sf != NULL)
217 {
218 (*objfile -> sf -> sym_finish) (objfile);
219 }
220
221 /* We always close the bfd. */
222
223 if (objfile -> obfd != NULL)
224 {
225 bfd_close (objfile -> obfd);
226 }
227
228 /* Remove it from the chain of all objfiles. */
229
230 if (object_files == objfile)
231 {
232 object_files = objfile -> next;
233 }
234 else
235 {
236 for (ofp = object_files; ofp; ofp = ofp -> next)
237 {
238 if (ofp -> next == objfile)
239 {
240 ofp -> next = objfile -> next;
241 break;
242 }
243 }
244 }
245 objfile -> next = NULL;
246
247 #if 0 /* FIXME!! */
248
249 /* Before the symbol table code was redone to make it easier to
250 selectively load and remove information particular to a specific
251 linkage unit, gdb used to do these things whenever the monolithic
252 symbol table was blown away. How much still needs to be done
253 is unknown, but we play it safe for now and keep each action until
254 it is shown to be no longer needed. */
255
256 clear_symtab_users_once ();
257 #if defined (CLEAR_SOLIB)
258 CLEAR_SOLIB ();
259 #endif
260 clear_pc_function_cache ();
261
262 #endif
263
264 /* The last thing we do is free the objfile struct itself for the
265 non-reusable case, or detach from the mapped file for the reusable
266 case. Note that the mmalloc_detach or the mfree is the last thing
267 we can do with this objfile. */
268
269 if (objfile -> flags & OBJF_MAPPED)
270 {
271 /* Remember the fd so we can close it. We can't close it before
272 doing the detach, and after the detach the objfile is gone. */
273 mmfd = objfile -> mmfd;
274 mmalloc_detach (objfile -> md);
275 (void) close (mmfd);
276 }
277 else
278 {
279 if (objfile -> name != NULL)
280 {
281 mfree (objfile -> md, objfile -> name);
282 }
283 /* Free the obstacks for non-reusable objfiles */
284 obstack_free (&objfile -> psymbol_obstack, 0);
285 obstack_free (&objfile -> symbol_obstack, 0);
286 obstack_free (&objfile -> type_obstack, 0);
287 mfree (objfile -> md, objfile);
288 }
289 }
290
291
292 /* Free all the object files at once. */
293
294 void
295 free_all_objfiles ()
296 {
297 struct objfile *objfile, *temp;
298
299 ALL_OBJFILES_SAFE (objfile, temp)
300 {
301 free_objfile (objfile);
302 }
303 }
304
305 /* Many places in gdb want to test just to see if we have any partial
306 symbols available. This function returns zero if none are currently
307 available, nonzero otherwise. */
308
309 int
310 have_partial_symbols ()
311 {
312 struct objfile *ofp;
313
314 ALL_OBJFILES (ofp)
315 {
316 if (ofp -> psymtabs != NULL)
317 {
318 return 1;
319 }
320 }
321 return 0;
322 }
323
324 /* Many places in gdb want to test just to see if we have any full
325 symbols available. This function returns zero if none are currently
326 available, nonzero otherwise. */
327
328 int
329 have_full_symbols ()
330 {
331 struct objfile *ofp;
332
333 ALL_OBJFILES (ofp)
334 {
335 if (ofp -> symtabs != NULL)
336 {
337 return 1;
338 }
339 }
340 return 0;
341 }
342
343 /* Many places in gdb want to test just to see if we have any minimal
344 symbols available. This function returns zero if none are currently
345 available, nonzero otherwise. */
346
347 int
348 have_minimal_symbols ()
349 {
350 struct objfile *ofp;
351
352 ALL_OBJFILES (ofp)
353 {
354 if (ofp -> msymbols != NULL)
355 {
356 return 1;
357 }
358 }
359 return 0;
360 }
361
362 /* Look for a mapped symbol file that corresponds to FILENAME and is more
363 recent than MTIME. If MAPPED is nonzero, the user has asked that gdb
364 use a mapped symbol file for this file, so create a new one if one does
365 not currently exist.
366
367 If found, then return an open file descriptor for the file, otherwise
368 return -1.
369
370 This routine is responsible for implementing the policy that generates
371 the name of the mapped symbol file from the name of a file containing
372 symbols that gdb would like to read. */
373
374 static int
375 open_mapped_file (filename, mtime, mapped)
376 char *filename;
377 long mtime;
378 int mapped;
379 {
380 int fd;
381 char *symfilename;
382 struct stat sbuf;
383
384 /* For now, all we do is look in the local directory for a file with
385 the name of the base file and an extension of ".syms" */
386
387 symfilename = concat ("./", basename (filename), ".syms", (char *) NULL);
388
389 /* Check to see if the desired file already exists and is more recent than
390 the corresponding base file (specified by the passed MTIME parameter).
391 The open will fail if the file does not already exist. */
392
393 if ((fd = open (symfilename, O_RDWR)) >= 0)
394 {
395 if (fstat (fd, &sbuf) != 0)
396 {
397 (void) close (fd);
398 perror_with_name (symfilename);
399 }
400 else if (sbuf.st_mtime > mtime)
401 {
402 return (fd);
403 }
404 else
405 {
406 (void) close (fd);
407 fd = -1;
408 }
409 }
410
411 /* Either the file does not already exist, or the base file has changed
412 since it was created. In either case, if the user has specified use of
413 a mapped file, then create a new mapped file, truncating any existing
414 one.
415
416 In the case where there is an existing file, but it is out of date, and
417 the user did not specify mapped, the existing file is just silently
418 ignored. Perhaps we should warn about this case (FIXME?).
419
420 By default the file is rw for everyone, with the user's umask taking
421 care of turning off the permissions the user wants off. */
422
423 if (mapped)
424 {
425 fd = open (symfilename, O_RDWR | O_CREAT | O_TRUNC, 0666);
426 }
427
428 return (fd);
429 }
430
431 /* Return the base address at which we would like the next objfile's
432 mapped data to start.
433
434 For now, we use the kludge that the configuration specifies a base
435 address to which it is safe to map the first mmalloc heap, and an
436 increment to add to this address for each successive heap. There are
437 a lot of issues to deal with here to make this work reasonably, including:
438
439 Avoid memory collisions with existing mapped address spaces
440
441 Reclaim address spaces when their mmalloc heaps are unmapped
442
443 When mmalloc heaps are shared between processes they have to be
444 mapped at the same addresses in each
445
446 Once created, a mmalloc heap that is to be mapped back in must be
447 mapped at the original address. I.E. each objfile will expect to
448 be remapped at it's original address. This becomes a problem if
449 the desired address is already in use.
450
451 etc, etc, etc.
452
453 */
454
455
456 static CORE_ADDR
457 map_to_address ()
458 {
459
460 #if defined(MMAP_BASE_ADDRESS) && defined (MMAP_INCREMENT)
461
462 static CORE_ADDR next = MMAP_BASE_ADDRESS;
463 CORE_ADDR mapto = next;
464
465 next += MMAP_INCREMENT;
466 return (mapto);
467
468 #else
469
470 return (0);
471
472 #endif
473
474 }
This page took 0.037881 seconds and 4 git commands to generate.