]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/objfiles.c
* coffread.c, mipsread.c, xcoffread.c, coffread.c, dbxread.c,
[thirdparty/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
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <obstack.h>
33
34 /* Prototypes for local functions */
35
36 static int
37 open_mapped_file PARAMS ((char *filename, long mtime, int mapped));
38
39 static CORE_ADDR
40 map_to_address PARAMS ((void));
41
42 /* Externally visible variables that are owned by this module. */
43
44 struct objfile *object_files; /* Linked list of all objfiles */
45 int mapped_symbol_files; /* Try to use mapped symbol files */
46
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. */
51
52 struct objfile *
53 allocate_objfile (abfd, mapped)
54 bfd *abfd;
55 int mapped;
56 {
57 struct objfile *objfile = NULL;
58 int fd;
59 void *md;
60 CORE_ADDR mapto;
61
62 mapped |= mapped_symbol_files;
63
64 #if !defined(NO_MMALLOC) && defined(HAVE_MMAP)
65
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. */
74
75 fd = open_mapped_file (bfd_get_filename (abfd), bfd_get_mtime (abfd),
76 mapped);
77 if (fd >= 0)
78 {
79 if (((mapto = map_to_address ()) == 0) ||
80 ((md = mmalloc_attach (fd, (void *) mapto)) == NULL))
81 {
82 close (fd);
83 }
84 else if ((objfile = (struct objfile *) mmalloc_getkey (md, 0)) != NULL)
85 {
86 objfile -> md = md;
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);
96 }
97 else
98 {
99 objfile = (struct objfile *) xmmalloc (md, sizeof (struct objfile));
100 (void) memset (objfile, 0, sizeof (struct objfile));
101 objfile -> md = md;
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);
115 }
116 }
117
118 if (mapped && (objfile == NULL))
119 {
120 warning ("symbol table for '%s' will not be mapped",
121 bfd_get_filename (abfd));
122 }
123
124 #else /* defined(NO_MMALLOC) || !defined(HAVE_MMAP) */
125
126 if (mapped)
127 {
128 warning ("this version of gdb does not support mapped symbol tables.");
129
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. */
133
134 mapped_symbol_files = 0;
135 }
136
137 #endif /* !defined(NO_MMALLOC) && defined(HAVE_MMAP) */
138
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. */
142
143 if (objfile == NULL)
144 {
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,
149 (void *) 0, 0);
150 obstack_full_begin (&objfile -> symbol_obstack, 0, 0, xmalloc, free,
151 (void *) 0, 0);
152 obstack_full_begin (&objfile -> type_obstack, 0, 0, xmalloc, free,
153 (void *) 0, 0);
154
155 }
156
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
159 region. */
160
161 objfile -> obfd = abfd;
162 objfile -> name = mstrsave (objfile -> md, bfd_get_filename (abfd));
163 objfile -> mtime = bfd_get_mtime (abfd);
164
165 /* Push this file onto the head of the linked list of other such files. */
166
167 objfile -> next = object_files;
168 object_files = objfile;
169
170 return (objfile);
171 }
172
173
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.
177
178 Things which we do NOT free because they are not in malloc'd memory
179 or not in memory specific to the objfile include:
180
181 objfile -> sf
182
183 */
184
185 void
186 free_objfile (objfile)
187 struct objfile *objfile;
188 {
189 struct objfile *ofp;
190
191 if (objfile -> sf != NULL)
192 {
193 (*objfile -> sf -> sym_finish) (objfile);
194 }
195 if (objfile -> name != NULL)
196 {
197 mfree (objfile -> md, objfile -> name);
198 }
199 if (objfile -> obfd != NULL)
200 {
201 bfd_close (objfile -> obfd);
202 }
203
204 /* Remove it from the chain of all objfiles. */
205
206 if (object_files == objfile)
207 {
208 object_files = objfile -> next;
209 }
210 else
211 {
212 for (ofp = object_files; ofp; ofp = ofp -> next)
213 {
214 if (ofp -> next == objfile)
215 {
216 ofp -> next = objfile -> next;
217 }
218 }
219 }
220
221 obstack_free (&objfile -> psymbol_obstack, 0);
222 obstack_free (&objfile -> symbol_obstack, 0);
223 obstack_free (&objfile -> type_obstack, 0);
224
225 #if 0 /* FIXME!! */
226
227 /* Before the symbol table code was redone to make it easier to
228 selectively load and remove information particular to a specific
229 linkage unit, gdb used to do these things whenever the monolithic
230 symbol table was blown away. How much still needs to be done
231 is unknown, but we play it safe for now and keep each action until
232 it is shown to be no longer needed. */
233
234 clear_symtab_users_once ();
235 #if defined (CLEAR_SOLIB)
236 CLEAR_SOLIB ();
237 #endif
238 clear_pc_function_cache ();
239
240 #endif
241
242 /* The last thing we do is free the objfile struct itself */
243
244 mfree (objfile -> md, objfile);
245 }
246
247
248 /* Free all the object files at once. */
249
250 void
251 free_all_objfiles ()
252 {
253 struct objfile *objfile, *temp;
254
255 ALL_OBJFILES_SAFE (objfile, temp)
256 {
257 free_objfile (objfile);
258 }
259 }
260
261 /* Many places in gdb want to test just to see if we have any partial
262 symbols available. This function returns zero if none are currently
263 available, nonzero otherwise. */
264
265 int
266 have_partial_symbols ()
267 {
268 struct objfile *ofp;
269 int havethem = 0;
270
271 for (ofp = object_files; ofp; ofp = ofp -> next)
272 {
273 if (ofp -> psymtabs != NULL)
274 {
275 havethem++;
276 break;
277 }
278 }
279 return (havethem);
280 }
281
282 /* Many places in gdb want to test just to see if we have any full
283 symbols available. This function returns zero if none are currently
284 available, nonzero otherwise. */
285
286 int
287 have_full_symbols ()
288 {
289 struct objfile *ofp;
290 int havethem = 0;
291
292 for (ofp = object_files; ofp; ofp = ofp -> next)
293 {
294 if (ofp -> symtabs != NULL)
295 {
296 havethem++;
297 break;
298 }
299 }
300 return (havethem);
301 }
302
303 /* Many places in gdb want to test just to see if we have any minimal
304 symbols available. This function returns zero if none are currently
305 available, nonzero otherwise. */
306
307 int
308 have_minimal_symbols ()
309 {
310 struct objfile *ofp;
311 int havethem = 0;
312
313 for (ofp = object_files; ofp; ofp = ofp -> next)
314 {
315 if (ofp -> msymbols != NULL)
316 {
317 havethem++;
318 break;
319 }
320 }
321 return (havethem);
322 }
323
324 /* Call the function specified by FUNC for each currently available objfile,
325 for as long as this function continues to return NULL. If the function
326 ever returns non-NULL, then the iteration over the objfiles is terminated,
327 and the result is returned to the caller. The function called has full
328 control over the form and content of the information returned via the
329 non-NULL result, which may be as simple as a pointer to the objfile that
330 the iteration terminated on, or as complex as a pointer to a private
331 structure containing multiple results. */
332
333 PTR
334 iterate_over_objfiles (func, arg1, arg2, arg3)
335 PTR (*func) PARAMS ((struct objfile *, PTR, PTR, PTR));
336 PTR arg1;
337 PTR arg2;
338 PTR arg3;
339 {
340 register struct objfile *objfile;
341 PTR result = NULL;
342
343 for (objfile = object_files;
344 objfile != NULL && result == NULL;
345 objfile = objfile -> next)
346 {
347 result = (*func)(objfile, arg1, arg2, arg3);
348 }
349 return (result);
350 }
351
352 /* Call the function specified by FUNC for each currently available symbol
353 table, for as long as this function continues to return NULL. If the
354 function ever returns non-NULL, then the iteration over the symbol tables
355 is terminated, and the result is returned to the caller. The function
356 called has full control over the form and content of the information
357 returned via the non-NULL result, which may be as simple as a pointer
358 to the symtab that the iteration terminated on, or as complex as a
359 pointer to a private structure containing multiple results. */
360
361 PTR
362 iterate_over_symtabs (func, arg1, arg2, arg3)
363 PTR (*func) PARAMS ((struct objfile *, struct symtab *, PTR, PTR, PTR));
364 PTR arg1;
365 PTR arg2;
366 PTR arg3;
367 {
368 register struct objfile *objfile;
369 register struct symtab *symtab;
370 PTR result = NULL;
371
372 for (objfile = object_files;
373 objfile != NULL && result == NULL;
374 objfile = objfile -> next)
375 {
376 for (symtab = objfile -> symtabs;
377 symtab != NULL && result == NULL;
378 symtab = symtab -> next)
379 {
380 result = (*func)(objfile, symtab, arg1, arg2, arg3);
381 }
382 }
383 return (result);
384 }
385
386 /* Call the function specified by FUNC for each currently available partial
387 symbol table, for as long as this function continues to return NULL. If
388 the function ever returns non-NULL, then the iteration over the partial
389 symbol tables is terminated, and the result is returned to the caller.
390
391 The function called has full control over the form and content of the
392 information returned via the non-NULL result, which may be as simple as a
393 pointer to the partial symbol table that the iteration terminated on, or
394 as complex as a pointer to a private structure containing multiple
395 results. */
396
397 PTR
398 iterate_over_psymtabs (func, arg1, arg2, arg3)
399 PTR (*func) PARAMS ((struct objfile *, struct partial_symtab *,
400 PTR, PTR, PTR));
401 PTR arg1;
402 PTR arg2;
403 PTR arg3;
404 {
405 register struct objfile *objfile;
406 register struct partial_symtab *psymtab;
407 PTR result = NULL;
408
409 for (objfile = object_files;
410 objfile != NULL && result == NULL;
411 objfile = objfile -> next)
412 {
413 for (psymtab = objfile -> psymtabs;
414 psymtab != NULL && result == NULL;
415 psymtab = psymtab -> next)
416 {
417 result = (*func)(objfile, psymtab, arg1, arg2, arg3);
418 }
419 }
420 return (result);
421 }
422
423
424 /* Look for a mapped symbol file that corresponds to FILENAME and is more
425 recent than MTIME. If MAPPED is nonzero, the user has asked that gdb
426 use a mapped symbol file for this file, so create a new one if one does
427 not currently exist.
428
429 If found, then return an open file descriptor for the file, otherwise
430 return -1.
431
432 This routine is responsible for implementing the policy that generates
433 the name of the mapped symbol file from the name of a file containing
434 symbols that gdb would like to read. */
435
436 static int
437 open_mapped_file (filename, mtime, mapped)
438 char *filename;
439 long mtime;
440 int mapped;
441 {
442 int fd;
443 char *symfilename;
444 struct stat sbuf;
445
446 /* For now, all we do is look in the local directory for a file with
447 the name of the base file and an extension of ".syms" */
448
449 symfilename = concat ("./", basename (filename), ".syms", (char *) NULL);
450
451 /* Check to see if the desired file already exists and is more recent than
452 the corresponding base file (specified by the passed MTIME parameter).
453 The open will fail if the file does not already exist. */
454
455 if ((fd = open (symfilename, O_RDWR)) >= 0)
456 {
457 if (fstat (fd, &sbuf) != 0)
458 {
459 close (fd);
460 perror_with_name (symfilename);
461 }
462 else if (sbuf.st_mtime > mtime)
463 {
464 return (fd);
465 }
466 else
467 {
468 close (fd);
469 fd = -1;
470 }
471 }
472
473 /* Either the file does not already exist, or the base file has changed
474 since it was created. In either case, if the user has specified use of
475 a mapped file, then create a new mapped file, truncating any existing
476 one.
477
478 In the case where there is an existing file, but it is out of date, and
479 the user did not specify mapped, the existing file is just silently
480 ignored. Perhaps we should warn about this case (FIXME?).
481
482 By default the file is rw for everyone, with the user's umask taking
483 care of turning off the permissions the user wants off. */
484
485 if (mapped)
486 {
487 fd = open (symfilename, O_RDWR | O_CREAT | O_TRUNC, 0666);
488 }
489
490 return (fd);
491 }
492
493 /* Return the base address at which we would like the next objfile's
494 mapped data to start.
495
496 For now, we use the kludge that the configuration specifies a base
497 address to which it is safe to map the first mmalloc heap, and an
498 increment to add to this address for each successive heap. There are
499 a lot of issues to deal with here to make this work reasonably, including:
500
501 Avoid memory collisions with existing mapped address spaces
502
503 Reclaim address spaces when their mmalloc heaps are unmapped
504
505 When mmalloc heaps are shared between processes they have to be
506 mapped at the same addresses in each
507
508 Once created, a mmalloc heap that is to be mapped back in must be
509 mapped at the original address. I.E. each objfile will expect to
510 be remapped at it's original address. This becomes a problem if
511 the desired address is already in use.
512
513 etc, etc, etc.
514
515 */
516
517
518 static CORE_ADDR
519 map_to_address ()
520 {
521
522 #if defined(MMAP_BASE_ADDRESS) && defined (MMAP_INCREMENT)
523
524 static CORE_ADDR next = MMAP_BASE_ADDRESS;
525 CORE_ADDR mapto = next;
526
527 next += MMAP_INCREMENT;
528 return (mapto);
529
530 #else
531
532 return (0);
533
534 #endif
535
536 }