1 /* GDB routines for manipulating objfiles.
2 Copyright 1992 Free Software Foundation, Inc.
3 Contributed by Cygnus Support, using pieces from other GDB modules.
5 This file is part of GDB.
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.
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.
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. */
21 /* This file contains support routines for creating, manipulating, and
22 destroying objfile structures. */
25 #include "bfd.h" /* Binary File Description */
29 #include <sys/types.h>
34 /* Prototypes for local functions */
37 open_mapped_file PARAMS ((char *filename, long mtime, int mapped));
40 map_to_address PARAMS ((void));
42 /* Externally visible variables that are owned by this module. */
44 struct objfile *object_files; /* Linked list of all objfiles */
45 int mapped_symbol_files; /* Try to use mapped symbol files */
47 /* Given a pointer to an initialized bfd (ABFD) and a flag that indicates
48 whether or not an objfile is to be mapped (MAPPED), allocate a new objfile
49 struct, fill it in as best we can, link it into the list of all known
50 objfiles, and return a pointer to the new objfile struct. */
53 allocate_objfile (abfd, mapped)
57 struct objfile *objfile = NULL;
62 mapped |= mapped_symbol_files;
64 #if !defined(NO_MMALLOC) && defined(HAVE_MMAP)
66 /* If we can support mapped symbol files, try to open/reopen the mapped file
67 that corresponds to the file from which we wish to read symbols. If the
68 objfile is to be mapped, we must malloc the structure itself using the
69 mmap version, and arrange that all memory allocation for the objfile uses
70 the mmap routines. If we are reusing an existing mapped file, from which
71 we get our objfile pointer, we have to make sure that we update the
72 pointers to the alloc/free functions in the obstack, in case these
73 functions have moved within the current gdb. */
75 fd = open_mapped_file (bfd_get_filename (abfd), bfd_get_mtime (abfd),
79 if (((mapto = map_to_address ()) == 0) ||
80 ((md = mmalloc_attach (fd, (void *) mapto)) == NULL))
84 else if ((objfile = (struct objfile *) mmalloc_getkey (md, 0)) != NULL)
87 /* Update pointers to functions to *our* copies */
88 obstack_chunkfun (&objfile -> psymbol_obstack, xmmalloc);
89 obstack_freefun (&objfile -> psymbol_obstack, mfree);
90 obstack_chunkfun (&objfile -> symbol_obstack, xmmalloc);
91 obstack_freefun (&objfile -> symbol_obstack, mfree);
92 obstack_chunkfun (&objfile -> type_obstack, xmmalloc);
93 obstack_freefun (&objfile -> type_obstack, mfree);
94 /* Update memory corruption handler function addresses */
95 init_malloc (objfile -> md);
99 objfile = (struct objfile *) xmmalloc (md, sizeof (struct objfile));
100 (void) memset (objfile, 0, sizeof (struct objfile));
102 objfile -> flags |= OBJF_MAPPED;
103 mmalloc_setkey (objfile -> md, 0, objfile);
104 obstack_full_begin (&objfile -> psymbol_obstack, 0, 0,
105 xmmalloc, mfree, objfile -> md,
106 OBSTACK_MMALLOC_LIKE);
107 obstack_full_begin (&objfile -> symbol_obstack, 0, 0,
108 xmmalloc, mfree, objfile -> md,
109 OBSTACK_MMALLOC_LIKE);
110 obstack_full_begin (&objfile -> type_obstack, 0, 0,
111 xmmalloc, mfree, objfile -> md,
112 OBSTACK_MMALLOC_LIKE);
113 /* Set up to detect internal memory corruption */
114 init_malloc (objfile -> md);
118 if (mapped && (objfile == NULL))
120 warning ("symbol table for '%s' will not be mapped",
121 bfd_get_filename (abfd));
124 #else /* defined(NO_MMALLOC) || !defined(HAVE_MMAP) */
128 warning ("this version of gdb does not support mapped symbol tables.");
130 /* Turn off the global flag so we don't try to do mapped symbol tables
131 any more, which shuts up gdb unless the user specifically gives the
132 "mapped" keyword again. */
134 mapped_symbol_files = 0;
137 #endif /* !defined(NO_MMALLOC) && defined(HAVE_MMAP) */
139 /* If we don't support mapped symbol files, didn't ask for the file to be
140 mapped, or failed to open the mapped file for some reason, then revert
141 back to an unmapped objfile. */
145 objfile = (struct objfile *) xmalloc (sizeof (struct objfile));
146 (void) memset (objfile, 0, sizeof (struct objfile));
147 objfile -> md = NULL;
148 obstack_full_begin (&objfile -> psymbol_obstack, 0, 0, xmalloc, free,
150 obstack_full_begin (&objfile -> symbol_obstack, 0, 0, xmalloc, free,
152 obstack_full_begin (&objfile -> type_obstack, 0, 0, xmalloc, free,
157 /* Update the per-objfile information that comes from the bfd, ensuring
158 that any data that is reference is saved in the per-objfile data
161 objfile -> obfd = abfd;
162 objfile -> name = mstrsave (objfile -> md, bfd_get_filename (abfd));
163 objfile -> mtime = bfd_get_mtime (abfd);
165 /* Push this file onto the head of the linked list of other such files. */
167 objfile -> next = object_files;
168 object_files = objfile;
174 /* Destroy an objfile and all the symtabs and psymtabs under it. Note
175 that as much as possible is allocated on the symbol_obstack and
176 psymbol_obstack, so that the memory can be efficiently freed. */
179 free_objfile (objfile)
180 struct objfile *objfile;
186 mfree (objfile -> md, objfile -> name);
190 bfd_close (objfile -> obfd);
193 /* Remove it from the chain of all objfiles. */
195 if (object_files == objfile)
197 object_files = objfile -> next;
201 for (ofp = object_files; ofp; ofp = ofp -> next)
203 if (ofp -> next == objfile)
205 ofp -> next = objfile -> next;
210 obstack_free (&objfile -> psymbol_obstack, 0);
211 obstack_free (&objfile -> symbol_obstack, 0);
212 obstack_free (&objfile -> type_obstack, 0);
216 /* Before the symbol table code was redone to make it easier to
217 selectively load and remove information particular to a specific
218 linkage unit, gdb used to do these things whenever the monolithic
219 symbol table was blown away. How much still needs to be done
220 is unknown, but we play it safe for now and keep each action until
221 it is shown to be no longer needed. */
223 clear_symtab_users_once ();
224 #if defined (CLEAR_SOLIB)
227 clear_pc_function_cache ();
231 /* The last thing we do is free the objfile struct itself */
233 mfree (objfile -> md, objfile);
237 /* Free all the object files at once. */
242 struct objfile *objfile, *temp;
244 ALL_OBJFILES_SAFE (objfile, temp)
246 free_objfile (objfile);
250 /* Many places in gdb want to test just to see if we have any partial
251 symbols available. This function returns zero if none are currently
252 available, nonzero otherwise. */
255 have_partial_symbols ()
260 for (ofp = object_files; ofp; ofp = ofp -> next)
262 if (ofp -> psymtabs != NULL)
271 /* Many places in gdb want to test just to see if we have any full
272 symbols available. This function returns zero if none are currently
273 available, nonzero otherwise. */
281 for (ofp = object_files; ofp; ofp = ofp -> next)
283 if (ofp -> symtabs != NULL)
292 /* Many places in gdb want to test just to see if we have any minimal
293 symbols available. This function returns zero if none are currently
294 available, nonzero otherwise. */
297 have_minimal_symbols ()
302 for (ofp = object_files; ofp; ofp = ofp -> next)
304 if (ofp -> msymbols != NULL)
313 /* Call the function specified by FUNC for each currently available objfile,
314 for as long as this function continues to return NULL. If the function
315 ever returns non-NULL, then the iteration over the objfiles is terminated,
316 and the result is returned to the caller. The function called has full
317 control over the form and content of the information returned via the
318 non-NULL result, which may be as simple as a pointer to the objfile that
319 the iteration terminated on, or as complex as a pointer to a private
320 structure containing multiple results. */
323 iterate_over_objfiles (func, arg1, arg2, arg3)
324 PTR (*func) PARAMS ((struct objfile *, PTR, PTR, PTR));
329 register struct objfile *objfile;
332 for (objfile = object_files;
333 objfile != NULL && result == NULL;
334 objfile = objfile -> next)
336 result = (*func)(objfile, arg1, arg2, arg3);
341 /* Call the function specified by FUNC for each currently available symbol
342 table, for as long as this function continues to return NULL. If the
343 function ever returns non-NULL, then the iteration over the symbol tables
344 is terminated, and the result is returned to the caller. The function
345 called has full control over the form and content of the information
346 returned via the non-NULL result, which may be as simple as a pointer
347 to the symtab that the iteration terminated on, or as complex as a
348 pointer to a private structure containing multiple results. */
351 iterate_over_symtabs (func, arg1, arg2, arg3)
352 PTR (*func) PARAMS ((struct objfile *, struct symtab *, PTR, PTR, PTR));
357 register struct objfile *objfile;
358 register struct symtab *symtab;
361 for (objfile = object_files;
362 objfile != NULL && result == NULL;
363 objfile = objfile -> next)
365 for (symtab = objfile -> symtabs;
366 symtab != NULL && result == NULL;
367 symtab = symtab -> next)
369 result = (*func)(objfile, symtab, arg1, arg2, arg3);
375 /* Call the function specified by FUNC for each currently available partial
376 symbol table, for as long as this function continues to return NULL. If
377 the function ever returns non-NULL, then the iteration over the partial
378 symbol tables is terminated, and the result is returned to the caller.
380 The function called has full control over the form and content of the
381 information returned via the non-NULL result, which may be as simple as a
382 pointer to the partial symbol table that the iteration terminated on, or
383 as complex as a pointer to a private structure containing multiple
387 iterate_over_psymtabs (func, arg1, arg2, arg3)
388 PTR (*func) PARAMS ((struct objfile *, struct partial_symtab *,
394 register struct objfile *objfile;
395 register struct partial_symtab *psymtab;
398 for (objfile = object_files;
399 objfile != NULL && result == NULL;
400 objfile = objfile -> next)
402 for (psymtab = objfile -> psymtabs;
403 psymtab != NULL && result == NULL;
404 psymtab = psymtab -> next)
406 result = (*func)(objfile, psymtab, arg1, arg2, arg3);
413 /* Look for a mapped symbol file that corresponds to FILENAME and is more
414 recent than MTIME. If MAPPED is nonzero, the user has asked that gdb
415 use a mapped symbol file for this file, so create a new one if one does
418 If found, then return an open file descriptor for the file, otherwise
421 This routine is responsible for implementing the policy that generates
422 the name of the mapped symbol file from the name of a file containing
423 symbols that gdb would like to read. */
426 open_mapped_file (filename, mtime, mapped)
435 /* For now, all we do is look in the local directory for a file with
436 the name of the base file and an extension of ".syms" */
438 symfilename = concat ("./", basename (filename), ".syms", (char *) NULL);
440 /* Check to see if the desired file already exists and is more recent than
441 the corresponding base file (specified by the passed MTIME parameter).
442 The open will fail if the file does not already exist. */
444 if ((fd = open (symfilename, O_RDWR)) >= 0)
446 if (fstat (fd, &sbuf) != 0)
449 perror_with_name (symfilename);
451 else if (sbuf.st_mtime > mtime)
462 /* Either the file does not already exist, or the base file has changed
463 since it was created. In either case, if the user has specified use of
464 a mapped file, then create a new mapped file, truncating any existing
467 In the case where there is an existing file, but it is out of date, and
468 the user did not specify mapped, the existing file is just silently
469 ignored. Perhaps we should warn about this case (FIXME?).
471 By default the file is rw for everyone, with the user's umask taking
472 care of turning off the permissions the user wants off. */
476 fd = open (symfilename, O_RDWR | O_CREAT | O_TRUNC, 0666);
482 /* Return the base address at which we would like the next objfile's
483 mapped data to start.
485 For now, we use the kludge that the configuration specifies a base
486 address to which it is safe to map the first mmalloc heap, and an
487 increment to add to this address for each successive heap. There are
488 a lot of issues to deal with here to make this work reasonably, including:
490 Avoid memory collisions with existing mapped address spaces
492 Reclaim address spaces when their mmalloc heaps are unmapped
494 When mmalloc heaps are shared between processes they have to be
495 mapped at the same addresses in each
497 Once created, a mmalloc heap that is to be mapped back in must be
498 mapped at the original address. I.E. each objfile will expect to
499 be remapped at it's original address. This becomes a problem if
500 the desired address is already in use.
511 #if defined(MMAP_BASE_ADDRESS) && defined (MMAP_INCREMENT)
513 static CORE_ADDR next = MMAP_BASE_ADDRESS;
514 CORE_ADDR mapto = next;
516 next += MMAP_INCREMENT;