faf9e963801d22382491ec38a3a03838dd2d30bb
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. */
94 /* Update pointers to functions to *our* copies */
95 obstack_chunkfun (&objfile
-> psymbol_obstack
, xmmalloc
);
96 obstack_freefun (&objfile
-> psymbol_obstack
, mfree
);
97 obstack_chunkfun (&objfile
-> symbol_obstack
, xmmalloc
);
98 obstack_freefun (&objfile
-> symbol_obstack
, mfree
);
99 obstack_chunkfun (&objfile
-> type_obstack
, xmmalloc
);
100 obstack_freefun (&objfile
-> type_obstack
, mfree
);
104 /* Set up to detect internal memory corruption. MUST be done before
105 the first malloc. See comments in init_malloc() and mmcheck(). */
107 objfile
= (struct objfile
*) xmmalloc (md
, sizeof (struct objfile
));
108 (void) memset (objfile
, 0, sizeof (struct objfile
));
110 objfile
-> flags
|= OBJF_MAPPED
;
111 mmalloc_setkey (objfile
-> md
, 0, objfile
);
112 obstack_full_begin (&objfile
-> psymbol_obstack
, 0, 0,
113 xmmalloc
, mfree
, objfile
-> md
,
114 OBSTACK_MMALLOC_LIKE
);
115 obstack_full_begin (&objfile
-> symbol_obstack
, 0, 0,
116 xmmalloc
, mfree
, objfile
-> md
,
117 OBSTACK_MMALLOC_LIKE
);
118 obstack_full_begin (&objfile
-> type_obstack
, 0, 0,
119 xmmalloc
, mfree
, objfile
-> md
,
120 OBSTACK_MMALLOC_LIKE
);
124 if (mapped
&& (objfile
== NULL
))
126 warning ("symbol table for '%s' will not be mapped",
127 bfd_get_filename (abfd
));
130 #else /* defined(NO_MMALLOC) || !defined(HAVE_MMAP) */
134 warning ("this version of gdb does not support mapped symbol tables.");
136 /* Turn off the global flag so we don't try to do mapped symbol tables
137 any more, which shuts up gdb unless the user specifically gives the
138 "mapped" keyword again. */
140 mapped_symbol_files
= 0;
143 #endif /* !defined(NO_MMALLOC) && defined(HAVE_MMAP) */
145 /* If we don't support mapped symbol files, didn't ask for the file to be
146 mapped, or failed to open the mapped file for some reason, then revert
147 back to an unmapped objfile. */
151 objfile
= (struct objfile
*) xmalloc (sizeof (struct objfile
));
152 (void) memset (objfile
, 0, sizeof (struct objfile
));
153 objfile
-> md
= NULL
;
154 obstack_full_begin (&objfile
-> psymbol_obstack
, 0, 0, xmalloc
, free
,
156 obstack_full_begin (&objfile
-> symbol_obstack
, 0, 0, xmalloc
, free
,
158 obstack_full_begin (&objfile
-> type_obstack
, 0, 0, xmalloc
, free
,
163 /* Update the per-objfile information that comes from the bfd, ensuring
164 that any data that is reference is saved in the per-objfile data
167 objfile
-> obfd
= abfd
;
168 objfile
-> name
= mstrsave (objfile
-> md
, bfd_get_filename (abfd
));
169 objfile
-> mtime
= bfd_get_mtime (abfd
);
171 /* Push this file onto the head of the linked list of other such files. */
173 objfile
-> next
= object_files
;
174 object_files
= objfile
;
180 /* Destroy an objfile and all the symtabs and psymtabs under it. Note
181 that as much as possible is allocated on the symbol_obstack and
182 psymbol_obstack, so that the memory can be efficiently freed.
184 Things which we do NOT free because they are not in malloc'd memory
185 or not in memory specific to the objfile include:
192 free_objfile (objfile
)
193 struct objfile
*objfile
;
197 if (objfile
-> sf
!= NULL
)
199 (*objfile
-> sf
-> sym_finish
) (objfile
);
201 if (objfile
-> name
!= NULL
)
203 mfree (objfile
-> md
, objfile
-> name
);
205 if (objfile
-> obfd
!= NULL
)
207 bfd_close (objfile
-> obfd
);
210 /* Remove it from the chain of all objfiles. */
212 if (object_files
== objfile
)
214 object_files
= objfile
-> next
;
218 for (ofp
= object_files
; ofp
; ofp
= ofp
-> next
)
220 if (ofp
-> next
== objfile
)
222 ofp
-> next
= objfile
-> next
;
227 obstack_free (&objfile
-> psymbol_obstack
, 0);
228 obstack_free (&objfile
-> symbol_obstack
, 0);
229 obstack_free (&objfile
-> type_obstack
, 0);
233 /* Before the symbol table code was redone to make it easier to
234 selectively load and remove information particular to a specific
235 linkage unit, gdb used to do these things whenever the monolithic
236 symbol table was blown away. How much still needs to be done
237 is unknown, but we play it safe for now and keep each action until
238 it is shown to be no longer needed. */
240 clear_symtab_users_once ();
241 #if defined (CLEAR_SOLIB)
244 clear_pc_function_cache ();
248 /* The last thing we do is free the objfile struct itself */
250 mfree (objfile
-> md
, objfile
);
254 /* Free all the object files at once. */
259 struct objfile
*objfile
, *temp
;
261 ALL_OBJFILES_SAFE (objfile
, temp
)
263 free_objfile (objfile
);
267 /* Many places in gdb want to test just to see if we have any partial
268 symbols available. This function returns zero if none are currently
269 available, nonzero otherwise. */
272 have_partial_symbols ()
278 if (ofp
-> psymtabs
!= NULL
)
286 /* Many places in gdb want to test just to see if we have any full
287 symbols available. This function returns zero if none are currently
288 available, nonzero otherwise. */
297 if (ofp
-> symtabs
!= NULL
)
305 /* Many places in gdb want to test just to see if we have any minimal
306 symbols available. This function returns zero if none are currently
307 available, nonzero otherwise. */
310 have_minimal_symbols ()
316 if (ofp
-> msymbols
!= NULL
)
324 /* Look for a mapped symbol file that corresponds to FILENAME and is more
325 recent than MTIME. If MAPPED is nonzero, the user has asked that gdb
326 use a mapped symbol file for this file, so create a new one if one does
329 If found, then return an open file descriptor for the file, otherwise
332 This routine is responsible for implementing the policy that generates
333 the name of the mapped symbol file from the name of a file containing
334 symbols that gdb would like to read. */
337 open_mapped_file (filename
, mtime
, mapped
)
346 /* For now, all we do is look in the local directory for a file with
347 the name of the base file and an extension of ".syms" */
349 symfilename
= concat ("./", basename (filename
), ".syms", (char *) NULL
);
351 /* Check to see if the desired file already exists and is more recent than
352 the corresponding base file (specified by the passed MTIME parameter).
353 The open will fail if the file does not already exist. */
355 if ((fd
= open (symfilename
, O_RDWR
)) >= 0)
357 if (fstat (fd
, &sbuf
) != 0)
360 perror_with_name (symfilename
);
362 else if (sbuf
.st_mtime
> mtime
)
373 /* Either the file does not already exist, or the base file has changed
374 since it was created. In either case, if the user has specified use of
375 a mapped file, then create a new mapped file, truncating any existing
378 In the case where there is an existing file, but it is out of date, and
379 the user did not specify mapped, the existing file is just silently
380 ignored. Perhaps we should warn about this case (FIXME?).
382 By default the file is rw for everyone, with the user's umask taking
383 care of turning off the permissions the user wants off. */
387 fd
= open (symfilename
, O_RDWR
| O_CREAT
| O_TRUNC
, 0666);
393 /* Return the base address at which we would like the next objfile's
394 mapped data to start.
396 For now, we use the kludge that the configuration specifies a base
397 address to which it is safe to map the first mmalloc heap, and an
398 increment to add to this address for each successive heap. There are
399 a lot of issues to deal with here to make this work reasonably, including:
401 Avoid memory collisions with existing mapped address spaces
403 Reclaim address spaces when their mmalloc heaps are unmapped
405 When mmalloc heaps are shared between processes they have to be
406 mapped at the same addresses in each
408 Once created, a mmalloc heap that is to be mapped back in must be
409 mapped at the original address. I.E. each objfile will expect to
410 be remapped at it's original address. This becomes a problem if
411 the desired address is already in use.
422 #if defined(MMAP_BASE_ADDRESS) && defined (MMAP_INCREMENT)
424 static CORE_ADDR next
= MMAP_BASE_ADDRESS
;
425 CORE_ADDR mapto
= next
;
427 next
+= MMAP_INCREMENT
;
This page took 0.038753 seconds and 4 git commands to generate.