35121a96768d7d8b61469ee45e04b2a81fe54ac4
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
)
92 /* Update pointers to functions to *our* copies */
93 obstack_chunkfun (&objfile
-> psymbol_obstack
, xmmalloc
);
94 obstack_freefun (&objfile
-> psymbol_obstack
, mfree
);
95 obstack_chunkfun (&objfile
-> symbol_obstack
, xmmalloc
);
96 obstack_freefun (&objfile
-> symbol_obstack
, mfree
);
97 obstack_chunkfun (&objfile
-> type_obstack
, xmmalloc
);
98 obstack_freefun (&objfile
-> type_obstack
, mfree
);
99 /* Update memory corruption handler function addresses */
100 init_malloc (objfile
-> md
);
104 objfile
= (struct objfile
*) xmmalloc (md
, sizeof (struct objfile
));
105 (void) memset (objfile
, 0, sizeof (struct objfile
));
107 objfile
-> flags
|= OBJF_MAPPED
;
108 mmalloc_setkey (objfile
-> md
, 0, objfile
);
109 obstack_full_begin (&objfile
-> psymbol_obstack
, 0, 0,
110 xmmalloc
, mfree
, objfile
-> md
,
111 OBSTACK_MMALLOC_LIKE
);
112 obstack_full_begin (&objfile
-> symbol_obstack
, 0, 0,
113 xmmalloc
, mfree
, objfile
-> md
,
114 OBSTACK_MMALLOC_LIKE
);
115 obstack_full_begin (&objfile
-> type_obstack
, 0, 0,
116 xmmalloc
, mfree
, objfile
-> md
,
117 OBSTACK_MMALLOC_LIKE
);
118 /* Set up to detect internal memory corruption */
119 init_malloc (objfile
-> md
);
123 if (mapped
&& (objfile
== NULL
))
125 warning ("symbol table for '%s' will not be mapped",
126 bfd_get_filename (abfd
));
129 #else /* defined(NO_MMALLOC) || !defined(HAVE_MMAP) */
133 warning ("this version of gdb does not support mapped symbol tables.");
135 /* Turn off the global flag so we don't try to do mapped symbol tables
136 any more, which shuts up gdb unless the user specifically gives the
137 "mapped" keyword again. */
139 mapped_symbol_files
= 0;
142 #endif /* !defined(NO_MMALLOC) && defined(HAVE_MMAP) */
144 /* If we don't support mapped symbol files, didn't ask for the file to be
145 mapped, or failed to open the mapped file for some reason, then revert
146 back to an unmapped objfile. */
150 objfile
= (struct objfile
*) xmalloc (sizeof (struct objfile
));
151 (void) memset (objfile
, 0, sizeof (struct objfile
));
152 objfile
-> md
= NULL
;
153 obstack_full_begin (&objfile
-> psymbol_obstack
, 0, 0, xmalloc
, free
,
155 obstack_full_begin (&objfile
-> symbol_obstack
, 0, 0, xmalloc
, free
,
157 obstack_full_begin (&objfile
-> type_obstack
, 0, 0, xmalloc
, free
,
162 /* Update the per-objfile information that comes from the bfd, ensuring
163 that any data that is reference is saved in the per-objfile data
166 objfile
-> obfd
= abfd
;
167 objfile
-> name
= mstrsave (objfile
-> md
, bfd_get_filename (abfd
));
168 objfile
-> mtime
= bfd_get_mtime (abfd
);
170 /* Push this file onto the head of the linked list of other such files. */
172 objfile
-> next
= object_files
;
173 object_files
= objfile
;
179 /* Destroy an objfile and all the symtabs and psymtabs under it. Note
180 that as much as possible is allocated on the symbol_obstack and
181 psymbol_obstack, so that the memory can be efficiently freed.
183 Things which we do NOT free because they are not in malloc'd memory
184 or not in memory specific to the objfile include:
191 free_objfile (objfile
)
192 struct objfile
*objfile
;
196 if (objfile
-> sf
!= NULL
)
198 (*objfile
-> sf
-> sym_finish
) (objfile
);
200 if (objfile
-> name
!= NULL
)
202 mfree (objfile
-> md
, objfile
-> name
);
204 if (objfile
-> obfd
!= NULL
)
206 bfd_close (objfile
-> obfd
);
209 /* Remove it from the chain of all objfiles. */
211 if (object_files
== objfile
)
213 object_files
= objfile
-> next
;
217 for (ofp
= object_files
; ofp
; ofp
= ofp
-> next
)
219 if (ofp
-> next
== objfile
)
221 ofp
-> next
= objfile
-> next
;
226 obstack_free (&objfile
-> psymbol_obstack
, 0);
227 obstack_free (&objfile
-> symbol_obstack
, 0);
228 obstack_free (&objfile
-> type_obstack
, 0);
232 /* Before the symbol table code was redone to make it easier to
233 selectively load and remove information particular to a specific
234 linkage unit, gdb used to do these things whenever the monolithic
235 symbol table was blown away. How much still needs to be done
236 is unknown, but we play it safe for now and keep each action until
237 it is shown to be no longer needed. */
239 clear_symtab_users_once ();
240 #if defined (CLEAR_SOLIB)
243 clear_pc_function_cache ();
247 /* The last thing we do is free the objfile struct itself */
249 mfree (objfile
-> md
, objfile
);
253 /* Free all the object files at once. */
258 struct objfile
*objfile
, *temp
;
260 ALL_OBJFILES_SAFE (objfile
, temp
)
262 free_objfile (objfile
);
266 /* Many places in gdb want to test just to see if we have any partial
267 symbols available. This function returns zero if none are currently
268 available, nonzero otherwise. */
271 have_partial_symbols ()
276 for (ofp
= object_files
; ofp
; ofp
= ofp
-> next
)
278 if (ofp
-> psymtabs
!= NULL
)
287 /* Many places in gdb want to test just to see if we have any full
288 symbols available. This function returns zero if none are currently
289 available, nonzero otherwise. */
297 for (ofp
= object_files
; ofp
; ofp
= ofp
-> next
)
299 if (ofp
-> symtabs
!= NULL
)
308 /* Many places in gdb want to test just to see if we have any minimal
309 symbols available. This function returns zero if none are currently
310 available, nonzero otherwise. */
313 have_minimal_symbols ()
318 for (ofp
= object_files
; ofp
; ofp
= ofp
-> next
)
320 if (ofp
-> msymbols
!= NULL
)
329 /* Call the function specified by FUNC for each currently available objfile,
330 for as long as this function continues to return NULL. If the function
331 ever returns non-NULL, then the iteration over the objfiles is terminated,
332 and the result is returned to the caller. The function called has full
333 control over the form and content of the information returned via the
334 non-NULL result, which may be as simple as a pointer to the objfile that
335 the iteration terminated on, or as complex as a pointer to a private
336 structure containing multiple results. */
339 iterate_over_objfiles (func
, arg1
, arg2
, arg3
)
340 PTR (*func
) PARAMS ((struct objfile
*, PTR
, PTR
, PTR
));
345 register struct objfile
*objfile
;
348 for (objfile
= object_files
;
349 objfile
!= NULL
&& result
== NULL
;
350 objfile
= objfile
-> next
)
352 result
= (*func
)(objfile
, arg1
, arg2
, arg3
);
357 /* Call the function specified by FUNC for each currently available symbol
358 table, for as long as this function continues to return NULL. If the
359 function ever returns non-NULL, then the iteration over the symbol tables
360 is terminated, and the result is returned to the caller. The function
361 called has full control over the form and content of the information
362 returned via the non-NULL result, which may be as simple as a pointer
363 to the symtab that the iteration terminated on, or as complex as a
364 pointer to a private structure containing multiple results. */
367 iterate_over_symtabs (func
, arg1
, arg2
, arg3
)
368 PTR (*func
) PARAMS ((struct objfile
*, struct symtab
*, PTR
, PTR
, PTR
));
373 register struct objfile
*objfile
;
374 register struct symtab
*symtab
;
377 for (objfile
= object_files
;
378 objfile
!= NULL
&& result
== NULL
;
379 objfile
= objfile
-> next
)
381 for (symtab
= objfile
-> symtabs
;
382 symtab
!= NULL
&& result
== NULL
;
383 symtab
= symtab
-> next
)
385 result
= (*func
)(objfile
, symtab
, arg1
, arg2
, arg3
);
391 /* Call the function specified by FUNC for each currently available partial
392 symbol table, for as long as this function continues to return NULL. If
393 the function ever returns non-NULL, then the iteration over the partial
394 symbol tables is terminated, and the result is returned to the caller.
396 The function called has full control over the form and content of the
397 information returned via the non-NULL result, which may be as simple as a
398 pointer to the partial symbol table that the iteration terminated on, or
399 as complex as a pointer to a private structure containing multiple
403 iterate_over_psymtabs (func
, arg1
, arg2
, arg3
)
404 PTR (*func
) PARAMS ((struct objfile
*, struct partial_symtab
*,
410 register struct objfile
*objfile
;
411 register struct partial_symtab
*psymtab
;
414 for (objfile
= object_files
;
415 objfile
!= NULL
&& result
== NULL
;
416 objfile
= objfile
-> next
)
418 for (psymtab
= objfile
-> psymtabs
;
419 psymtab
!= NULL
&& result
== NULL
;
420 psymtab
= psymtab
-> next
)
422 result
= (*func
)(objfile
, psymtab
, arg1
, arg2
, arg3
);
429 /* Look for a mapped symbol file that corresponds to FILENAME and is more
430 recent than MTIME. If MAPPED is nonzero, the user has asked that gdb
431 use a mapped symbol file for this file, so create a new one if one does
434 If found, then return an open file descriptor for the file, otherwise
437 This routine is responsible for implementing the policy that generates
438 the name of the mapped symbol file from the name of a file containing
439 symbols that gdb would like to read. */
442 open_mapped_file (filename
, mtime
, mapped
)
451 /* For now, all we do is look in the local directory for a file with
452 the name of the base file and an extension of ".syms" */
454 symfilename
= concat ("./", basename (filename
), ".syms", (char *) NULL
);
456 /* Check to see if the desired file already exists and is more recent than
457 the corresponding base file (specified by the passed MTIME parameter).
458 The open will fail if the file does not already exist. */
460 if ((fd
= open (symfilename
, O_RDWR
)) >= 0)
462 if (fstat (fd
, &sbuf
) != 0)
465 perror_with_name (symfilename
);
467 else if (sbuf
.st_mtime
> mtime
)
478 /* Either the file does not already exist, or the base file has changed
479 since it was created. In either case, if the user has specified use of
480 a mapped file, then create a new mapped file, truncating any existing
483 In the case where there is an existing file, but it is out of date, and
484 the user did not specify mapped, the existing file is just silently
485 ignored. Perhaps we should warn about this case (FIXME?).
487 By default the file is rw for everyone, with the user's umask taking
488 care of turning off the permissions the user wants off. */
492 fd
= open (symfilename
, O_RDWR
| O_CREAT
| O_TRUNC
, 0666);
498 /* Return the base address at which we would like the next objfile's
499 mapped data to start.
501 For now, we use the kludge that the configuration specifies a base
502 address to which it is safe to map the first mmalloc heap, and an
503 increment to add to this address for each successive heap. There are
504 a lot of issues to deal with here to make this work reasonably, including:
506 Avoid memory collisions with existing mapped address spaces
508 Reclaim address spaces when their mmalloc heaps are unmapped
510 When mmalloc heaps are shared between processes they have to be
511 mapped at the same addresses in each
513 Once created, a mmalloc heap that is to be mapped back in must be
514 mapped at the original address. I.E. each objfile will expect to
515 be remapped at it's original address. This becomes a problem if
516 the desired address is already in use.
527 #if defined(MMAP_BASE_ADDRESS) && defined (MMAP_INCREMENT)
529 static CORE_ADDR next
= MMAP_BASE_ADDRESS
;
530 CORE_ADDR mapto
= next
;
532 next
+= MMAP_INCREMENT
;
This page took 0.04006 seconds and 4 git commands to generate.