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