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