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 */
30 #include <sys/types.h>
35 /* Prototypes for local functions */
38 open_mapped_file PARAMS ((char *filename, long mtime, int mapped));
41 map_to_address PARAMS ((void));
43 /* Externally visible variables that are owned by this module.
44 See declarations in objfile.h for more info. */
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 */
50 int mapped_symbol_files; /* Try to use mapped symbol files */
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. */
58 allocate_objfile (abfd, mapped)
62 struct objfile *objfile = NULL;
67 mapped |= mapped_symbol_files;
69 #if !defined(NO_MMALLOC) && defined(HAVE_MMAP)
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. */
80 fd = open_mapped_file (bfd_get_filename (abfd), bfd_get_mtime (abfd),
84 if (((mapto = map_to_address ()) == 0) ||
85 ((md = mmalloc_attach (fd, (void *) mapto)) == NULL))
89 else if ((objfile = (struct objfile *) mmalloc_getkey (md, 0)) != NULL)
91 /* Update memory corruption handler function addresses. */
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);
105 /* Set up to detect internal memory corruption. MUST be done before
106 the first malloc. See comments in init_malloc() and mmcheck(). */
108 objfile = (struct objfile *) xmmalloc (md, sizeof (struct objfile));
109 (void) memset (objfile, 0, sizeof (struct objfile));
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);
126 if (mapped && (objfile == NULL))
128 warning ("symbol table for '%s' will not be mapped",
129 bfd_get_filename (abfd));
132 #else /* defined(NO_MMALLOC) || !defined(HAVE_MMAP) */
136 warning ("this version of gdb does not support mapped symbol tables.");
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. */
142 mapped_symbol_files = 0;
145 #endif /* !defined(NO_MMALLOC) && defined(HAVE_MMAP) */
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. */
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,
158 obstack_full_begin (&objfile -> symbol_obstack, 0, 0, xmalloc, free,
160 obstack_full_begin (&objfile -> type_obstack, 0, 0, xmalloc, free,
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
169 objfile -> obfd = abfd;
170 if (objfile -> name != NULL)
172 mfree (objfile -> md, objfile -> name);
174 objfile -> name = mstrsave (objfile -> md, bfd_get_filename (abfd));
175 objfile -> mtime = bfd_get_mtime (abfd);
177 /* Push this file onto the head of the linked list of other such files. */
179 objfile -> next = object_files;
180 object_files = objfile;
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.
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:
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. */
203 free_objfile (objfile)
204 struct objfile *objfile;
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. */
216 if (objfile -> sf != NULL)
218 (*objfile -> sf -> sym_finish) (objfile);
221 /* We always close the bfd. */
223 if (objfile -> obfd != NULL)
225 bfd_close (objfile -> obfd);
228 /* Remove it from the chain of all objfiles. */
230 if (object_files == objfile)
232 object_files = objfile -> next;
236 for (ofp = object_files; ofp; ofp = ofp -> next)
238 if (ofp -> next == objfile)
240 ofp -> next = objfile -> next;
245 objfile -> next = NULL;
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. */
256 clear_symtab_users_once ();
257 #if defined (CLEAR_SOLIB)
260 clear_pc_function_cache ();
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. */
269 if (objfile -> flags & OBJF_MAPPED)
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);
279 if (objfile -> name != NULL)
281 mfree (objfile -> md, objfile -> name);
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);
292 /* Free all the object files at once. */
297 struct objfile *objfile, *temp;
299 ALL_OBJFILES_SAFE (objfile, temp)
301 free_objfile (objfile);
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. */
310 have_partial_symbols ()
316 if (ofp -> psymtabs != NULL)
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. */
335 if (ofp -> symtabs != NULL)
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. */
348 have_minimal_symbols ()
354 if (ofp -> msymbols != NULL)
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
367 If found, then return an open file descriptor for the file, otherwise
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. */
375 open_mapped_file (filename, mtime, mapped)
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" */
387 symfilename = concat ("./", basename (filename), ".syms", (char *) NULL);
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. */
393 if ((fd = open (symfilename, O_RDWR)) >= 0)
395 if (fstat (fd, &sbuf) != 0)
398 perror_with_name (symfilename);
400 else if (sbuf.st_mtime > mtime)
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
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?).
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. */
425 fd = open (symfilename, O_RDWR | O_CREAT | O_TRUNC, 0666);
431 /* Return the base address at which we would like the next objfile's
432 mapped data to start.
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:
439 Avoid memory collisions with existing mapped address spaces
441 Reclaim address spaces when their mmalloc heaps are unmapped
443 When mmalloc heaps are shared between processes they have to be
444 mapped at the same addresses in each
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.
460 #if defined(MMAP_BASE_ADDRESS) && defined (MMAP_INCREMENT)
462 static CORE_ADDR next = MMAP_BASE_ADDRESS;
463 CORE_ADDR mapto = next;
465 next += MMAP_INCREMENT;