35121a96768d7d8b61469ee45e04b2a81fe54ac4
[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 close (fd);
88 }
89 else if ((objfile = (struct objfile *) mmalloc_getkey (md, 0)) != NULL)
90 {
91 objfile -> md = md;
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);
101 }
102 else
103 {
104 objfile = (struct objfile *) xmmalloc (md, sizeof (struct objfile));
105 (void) memset (objfile, 0, sizeof (struct objfile));
106 objfile -> md = md;
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);
120 }
121 }
122
123 if (mapped && (objfile == NULL))
124 {
125 warning ("symbol table for '%s' will not be mapped",
126 bfd_get_filename (abfd));
127 }
128
129 #else /* defined(NO_MMALLOC) || !defined(HAVE_MMAP) */
130
131 if (mapped)
132 {
133 warning ("this version of gdb does not support mapped symbol tables.");
134
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. */
138
139 mapped_symbol_files = 0;
140 }
141
142 #endif /* !defined(NO_MMALLOC) && defined(HAVE_MMAP) */
143
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. */
147
148 if (objfile == NULL)
149 {
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,
154 (void *) 0, 0);
155 obstack_full_begin (&objfile -> symbol_obstack, 0, 0, xmalloc, free,
156 (void *) 0, 0);
157 obstack_full_begin (&objfile -> type_obstack, 0, 0, xmalloc, free,
158 (void *) 0, 0);
159
160 }
161
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
164 region. */
165
166 objfile -> obfd = abfd;
167 objfile -> name = mstrsave (objfile -> md, bfd_get_filename (abfd));
168 objfile -> mtime = bfd_get_mtime (abfd);
169
170 /* Push this file onto the head of the linked list of other such files. */
171
172 objfile -> next = object_files;
173 object_files = objfile;
174
175 return (objfile);
176 }
177
178
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.
182
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:
185
186 objfile -> sf
187
188 */
189
190 void
191 free_objfile (objfile)
192 struct objfile *objfile;
193 {
194 struct objfile *ofp;
195
196 if (objfile -> sf != NULL)
197 {
198 (*objfile -> sf -> sym_finish) (objfile);
199 }
200 if (objfile -> name != NULL)
201 {
202 mfree (objfile -> md, objfile -> name);
203 }
204 if (objfile -> obfd != NULL)
205 {
206 bfd_close (objfile -> obfd);
207 }
208
209 /* Remove it from the chain of all objfiles. */
210
211 if (object_files == objfile)
212 {
213 object_files = objfile -> next;
214 }
215 else
216 {
217 for (ofp = object_files; ofp; ofp = ofp -> next)
218 {
219 if (ofp -> next == objfile)
220 {
221 ofp -> next = objfile -> next;
222 }
223 }
224 }
225
226 obstack_free (&objfile -> psymbol_obstack, 0);
227 obstack_free (&objfile -> symbol_obstack, 0);
228 obstack_free (&objfile -> type_obstack, 0);
229
230 #if 0 /* FIXME!! */
231
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. */
238
239 clear_symtab_users_once ();
240 #if defined (CLEAR_SOLIB)
241 CLEAR_SOLIB ();
242 #endif
243 clear_pc_function_cache ();
244
245 #endif
246
247 /* The last thing we do is free the objfile struct itself */
248
249 mfree (objfile -> md, objfile);
250 }
251
252
253 /* Free all the object files at once. */
254
255 void
256 free_all_objfiles ()
257 {
258 struct objfile *objfile, *temp;
259
260 ALL_OBJFILES_SAFE (objfile, temp)
261 {
262 free_objfile (objfile);
263 }
264 }
265
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. */
269
270 int
271 have_partial_symbols ()
272 {
273 struct objfile *ofp;
274 int havethem = 0;
275
276 for (ofp = object_files; ofp; ofp = ofp -> next)
277 {
278 if (ofp -> psymtabs != NULL)
279 {
280 havethem++;
281 break;
282 }
283 }
284 return (havethem);
285 }
286
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. */
290
291 int
292 have_full_symbols ()
293 {
294 struct objfile *ofp;
295 int havethem = 0;
296
297 for (ofp = object_files; ofp; ofp = ofp -> next)
298 {
299 if (ofp -> symtabs != NULL)
300 {
301 havethem++;
302 break;
303 }
304 }
305 return (havethem);
306 }
307
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. */
311
312 int
313 have_minimal_symbols ()
314 {
315 struct objfile *ofp;
316 int havethem = 0;
317
318 for (ofp = object_files; ofp; ofp = ofp -> next)
319 {
320 if (ofp -> msymbols != NULL)
321 {
322 havethem++;
323 break;
324 }
325 }
326 return (havethem);
327 }
328
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. */
337
338 PTR
339 iterate_over_objfiles (func, arg1, arg2, arg3)
340 PTR (*func) PARAMS ((struct objfile *, PTR, PTR, PTR));
341 PTR arg1;
342 PTR arg2;
343 PTR arg3;
344 {
345 register struct objfile *objfile;
346 PTR result = NULL;
347
348 for (objfile = object_files;
349 objfile != NULL && result == NULL;
350 objfile = objfile -> next)
351 {
352 result = (*func)(objfile, arg1, arg2, arg3);
353 }
354 return (result);
355 }
356
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. */
365
366 PTR
367 iterate_over_symtabs (func, arg1, arg2, arg3)
368 PTR (*func) PARAMS ((struct objfile *, struct symtab *, PTR, PTR, PTR));
369 PTR arg1;
370 PTR arg2;
371 PTR arg3;
372 {
373 register struct objfile *objfile;
374 register struct symtab *symtab;
375 PTR result = NULL;
376
377 for (objfile = object_files;
378 objfile != NULL && result == NULL;
379 objfile = objfile -> next)
380 {
381 for (symtab = objfile -> symtabs;
382 symtab != NULL && result == NULL;
383 symtab = symtab -> next)
384 {
385 result = (*func)(objfile, symtab, arg1, arg2, arg3);
386 }
387 }
388 return (result);
389 }
390
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.
395
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
400 results. */
401
402 PTR
403 iterate_over_psymtabs (func, arg1, arg2, arg3)
404 PTR (*func) PARAMS ((struct objfile *, struct partial_symtab *,
405 PTR, PTR, PTR));
406 PTR arg1;
407 PTR arg2;
408 PTR arg3;
409 {
410 register struct objfile *objfile;
411 register struct partial_symtab *psymtab;
412 PTR result = NULL;
413
414 for (objfile = object_files;
415 objfile != NULL && result == NULL;
416 objfile = objfile -> next)
417 {
418 for (psymtab = objfile -> psymtabs;
419 psymtab != NULL && result == NULL;
420 psymtab = psymtab -> next)
421 {
422 result = (*func)(objfile, psymtab, arg1, arg2, arg3);
423 }
424 }
425 return (result);
426 }
427
428
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
432 not currently exist.
433
434 If found, then return an open file descriptor for the file, otherwise
435 return -1.
436
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. */
440
441 static int
442 open_mapped_file (filename, mtime, mapped)
443 char *filename;
444 long mtime;
445 int mapped;
446 {
447 int fd;
448 char *symfilename;
449 struct stat sbuf;
450
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" */
453
454 symfilename = concat ("./", basename (filename), ".syms", (char *) NULL);
455
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. */
459
460 if ((fd = open (symfilename, O_RDWR)) >= 0)
461 {
462 if (fstat (fd, &sbuf) != 0)
463 {
464 close (fd);
465 perror_with_name (symfilename);
466 }
467 else if (sbuf.st_mtime > mtime)
468 {
469 return (fd);
470 }
471 else
472 {
473 close (fd);
474 fd = -1;
475 }
476 }
477
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
481 one.
482
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?).
486
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. */
489
490 if (mapped)
491 {
492 fd = open (symfilename, O_RDWR | O_CREAT | O_TRUNC, 0666);
493 }
494
495 return (fd);
496 }
497
498 /* Return the base address at which we would like the next objfile's
499 mapped data to start.
500
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:
505
506 Avoid memory collisions with existing mapped address spaces
507
508 Reclaim address spaces when their mmalloc heaps are unmapped
509
510 When mmalloc heaps are shared between processes they have to be
511 mapped at the same addresses in each
512
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.
517
518 etc, etc, etc.
519
520 */
521
522
523 static CORE_ADDR
524 map_to_address ()
525 {
526
527 #if defined(MMAP_BASE_ADDRESS) && defined (MMAP_INCREMENT)
528
529 static CORE_ADDR next = MMAP_BASE_ADDRESS;
530 CORE_ADDR mapto = next;
531
532 next += MMAP_INCREMENT;
533 return (mapto);
534
535 #else
536
537 return (0);
538
539 #endif
540
541 }
This page took 0.04006 seconds and 4 git commands to generate.