* elf32-ppc.c (allocate_dynrelocs): Ignore dyn_relocs when
[deliverable/binutils-gdb.git] / gdb / objfiles.c
1 /* GDB routines for manipulating objfiles.
2
3 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
4 2002, 2003, 2004, 2007, 2008 Free Software Foundation, Inc.
5
6 Contributed by Cygnus Support, using pieces from other GDB modules.
7
8 This file is part of GDB.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22
23 /* This file contains support routines for creating, manipulating, and
24 destroying objfile structures. */
25
26 #include "defs.h"
27 #include "bfd.h" /* Binary File Description */
28 #include "symtab.h"
29 #include "symfile.h"
30 #include "objfiles.h"
31 #include "gdb-stabs.h"
32 #include "target.h"
33 #include "bcache.h"
34 #include "mdebugread.h"
35 #include "expression.h"
36 #include "parser-defs.h"
37
38 #include "gdb_assert.h"
39 #include <sys/types.h>
40 #include "gdb_stat.h"
41 #include <fcntl.h>
42 #include "gdb_obstack.h"
43 #include "gdb_string.h"
44 #include "hashtab.h"
45
46 #include "breakpoint.h"
47 #include "block.h"
48 #include "dictionary.h"
49 #include "source.h"
50 #include "addrmap.h"
51 #include "arch-utils.h"
52 #include "exec.h"
53
54 /* Prototypes for local functions */
55
56 static void objfile_alloc_data (struct objfile *objfile);
57 static void objfile_free_data (struct objfile *objfile);
58
59 /* Externally visible variables that are owned by this module.
60 See declarations in objfile.h for more info. */
61
62 struct objfile *object_files; /* Linked list of all objfiles */
63 struct objfile *current_objfile; /* For symbol file being read in */
64 struct objfile *symfile_objfile; /* Main symbol table loaded from */
65 struct objfile *rt_common_objfile; /* For runtime common symbols */
66
67 /* Locate all mappable sections of a BFD file.
68 objfile_p_char is a char * to get it through
69 bfd_map_over_sections; we cast it back to its proper type. */
70
71 /* Called via bfd_map_over_sections to build up the section table that
72 the objfile references. The objfile contains pointers to the start
73 of the table (objfile->sections) and to the first location after
74 the end of the table (objfile->sections_end). */
75
76 static void
77 add_to_objfile_sections (struct bfd *abfd, struct bfd_section *asect,
78 void *objfile_p_char)
79 {
80 struct objfile *objfile = (struct objfile *) objfile_p_char;
81 struct obj_section section;
82 flagword aflag;
83
84 aflag = bfd_get_section_flags (abfd, asect);
85
86 if (!(aflag & SEC_ALLOC))
87 return;
88
89 if (0 == bfd_section_size (abfd, asect))
90 return;
91 section.offset = 0;
92 section.objfile = objfile;
93 section.the_bfd_section = asect;
94 section.ovly_mapped = 0;
95 section.addr = bfd_section_vma (abfd, asect);
96 section.endaddr = section.addr + bfd_section_size (abfd, asect);
97 obstack_grow (&objfile->objfile_obstack, (char *) &section, sizeof (section));
98 objfile->sections_end = (struct obj_section *) (((unsigned long) objfile->sections_end) + 1);
99 }
100
101 /* Builds a section table for OBJFILE.
102 Returns 0 if OK, 1 on error (in which case bfd_error contains the
103 error).
104
105 Note that while we are building the table, which goes into the
106 psymbol obstack, we hijack the sections_end pointer to instead hold
107 a count of the number of sections. When bfd_map_over_sections
108 returns, this count is used to compute the pointer to the end of
109 the sections table, which then overwrites the count.
110
111 Also note that the OFFSET and OVLY_MAPPED in each table entry
112 are initialized to zero.
113
114 Also note that if anything else writes to the psymbol obstack while
115 we are building the table, we're pretty much hosed. */
116
117 int
118 build_objfile_section_table (struct objfile *objfile)
119 {
120 /* objfile->sections can be already set when reading a mapped symbol
121 file. I believe that we do need to rebuild the section table in
122 this case (we rebuild other things derived from the bfd), but we
123 can't free the old one (it's in the objfile_obstack). So we just
124 waste some memory. */
125
126 objfile->sections_end = 0;
127 bfd_map_over_sections (objfile->obfd, add_to_objfile_sections, (char *) objfile);
128 objfile->sections = (struct obj_section *)
129 obstack_finish (&objfile->objfile_obstack);
130 objfile->sections_end = objfile->sections + (unsigned long) objfile->sections_end;
131 return (0);
132 }
133
134 /* Given a pointer to an initialized bfd (ABFD) and some flag bits
135 allocate a new objfile struct, fill it in as best we can, link it
136 into the list of all known objfiles, and return a pointer to the
137 new objfile struct.
138
139 The FLAGS word contains various bits (OBJF_*) that can be taken as
140 requests for specific operations. Other bits like OBJF_SHARED are
141 simply copied through to the new objfile flags member. */
142
143 /* NOTE: carlton/2003-02-04: This function is called with args NULL, 0
144 by jv-lang.c, to create an artificial objfile used to hold
145 information about dynamically-loaded Java classes. Unfortunately,
146 that branch of this function doesn't get tested very frequently, so
147 it's prone to breakage. (E.g. at one time the name was set to NULL
148 in that situation, which broke a loop over all names in the dynamic
149 library loader.) If you change this function, please try to leave
150 things in a consistent state even if abfd is NULL. */
151
152 struct objfile *
153 allocate_objfile (bfd *abfd, int flags)
154 {
155 struct objfile *objfile = NULL;
156 struct objfile *last_one = NULL;
157
158 /* If we don't support mapped symbol files, didn't ask for the file to be
159 mapped, or failed to open the mapped file for some reason, then revert
160 back to an unmapped objfile. */
161
162 if (objfile == NULL)
163 {
164 objfile = (struct objfile *) xmalloc (sizeof (struct objfile));
165 memset (objfile, 0, sizeof (struct objfile));
166 objfile->md = NULL;
167 objfile->psymbol_cache = bcache_xmalloc ();
168 objfile->macro_cache = bcache_xmalloc ();
169 /* We could use obstack_specify_allocation here instead, but
170 gdb_obstack.h specifies the alloc/dealloc functions. */
171 obstack_init (&objfile->objfile_obstack);
172 terminate_minimal_symbol_table (objfile);
173 }
174
175 objfile_alloc_data (objfile);
176
177 /* Update the per-objfile information that comes from the bfd, ensuring
178 that any data that is reference is saved in the per-objfile data
179 region. */
180
181 objfile->obfd = abfd;
182 if (objfile->name != NULL)
183 {
184 xfree (objfile->name);
185 }
186 if (abfd != NULL)
187 {
188 /* Look up the gdbarch associated with the BFD. */
189 objfile->gdbarch = gdbarch_from_bfd (abfd);
190
191 objfile->name = xstrdup (bfd_get_filename (abfd));
192 objfile->mtime = bfd_get_mtime (abfd);
193
194 /* Build section table. */
195
196 if (build_objfile_section_table (objfile))
197 {
198 error (_("Can't find the file sections in `%s': %s"),
199 objfile->name, bfd_errmsg (bfd_get_error ()));
200 }
201 }
202 else
203 {
204 objfile->name = xstrdup ("<<anonymous objfile>>");
205 }
206
207 /* Initialize the section indexes for this objfile, so that we can
208 later detect if they are used w/o being properly assigned to. */
209
210 objfile->sect_index_text = -1;
211 objfile->sect_index_data = -1;
212 objfile->sect_index_bss = -1;
213 objfile->sect_index_rodata = -1;
214
215 /* We don't yet have a C++-specific namespace symtab. */
216
217 objfile->cp_namespace_symtab = NULL;
218
219 /* Add this file onto the tail of the linked list of other such files. */
220
221 objfile->next = NULL;
222 if (object_files == NULL)
223 object_files = objfile;
224 else
225 {
226 for (last_one = object_files;
227 last_one->next;
228 last_one = last_one->next);
229 last_one->next = objfile;
230 }
231
232 /* Save passed in flag bits. */
233 objfile->flags |= flags;
234
235 return (objfile);
236 }
237
238 /* Retrieve the gdbarch associated with OBJFILE. */
239 struct gdbarch *
240 get_objfile_arch (struct objfile *objfile)
241 {
242 return objfile->gdbarch;
243 }
244
245 /* Initialize entry point information for this objfile. */
246
247 void
248 init_entry_point_info (struct objfile *objfile)
249 {
250 /* Save startup file's range of PC addresses to help blockframe.c
251 decide where the bottom of the stack is. */
252
253 if (bfd_get_file_flags (objfile->obfd) & EXEC_P)
254 {
255 /* Executable file -- record its entry point so we'll recognize
256 the startup file because it contains the entry point. */
257 objfile->ei.entry_point = bfd_get_start_address (objfile->obfd);
258 }
259 else if (bfd_get_file_flags (objfile->obfd) & DYNAMIC
260 && bfd_get_start_address (objfile->obfd) != 0)
261 /* Some shared libraries may have entry points set and be
262 runnable. There's no clear way to indicate this, so just check
263 for values other than zero. */
264 objfile->ei.entry_point = bfd_get_start_address (objfile->obfd);
265 else
266 {
267 /* Examination of non-executable.o files. Short-circuit this stuff. */
268 objfile->ei.entry_point = INVALID_ENTRY_POINT;
269 }
270 }
271
272 /* Get current entry point address. */
273
274 CORE_ADDR
275 entry_point_address (void)
276 {
277 return symfile_objfile ? symfile_objfile->ei.entry_point : 0;
278 }
279
280 /* Create the terminating entry of OBJFILE's minimal symbol table.
281 If OBJFILE->msymbols is zero, allocate a single entry from
282 OBJFILE->objfile_obstack; otherwise, just initialize
283 OBJFILE->msymbols[OBJFILE->minimal_symbol_count]. */
284 void
285 terminate_minimal_symbol_table (struct objfile *objfile)
286 {
287 if (! objfile->msymbols)
288 objfile->msymbols = ((struct minimal_symbol *)
289 obstack_alloc (&objfile->objfile_obstack,
290 sizeof (objfile->msymbols[0])));
291
292 {
293 struct minimal_symbol *m
294 = &objfile->msymbols[objfile->minimal_symbol_count];
295
296 memset (m, 0, sizeof (*m));
297 /* Don't rely on these enumeration values being 0's. */
298 MSYMBOL_TYPE (m) = mst_unknown;
299 SYMBOL_INIT_LANGUAGE_SPECIFIC (m, language_unknown);
300 }
301 }
302
303
304 /* Put one object file before a specified on in the global list.
305 This can be used to make sure an object file is destroyed before
306 another when using ALL_OBJFILES_SAFE to free all objfiles. */
307 void
308 put_objfile_before (struct objfile *objfile, struct objfile *before_this)
309 {
310 struct objfile **objp;
311
312 unlink_objfile (objfile);
313
314 for (objp = &object_files; *objp != NULL; objp = &((*objp)->next))
315 {
316 if (*objp == before_this)
317 {
318 objfile->next = *objp;
319 *objp = objfile;
320 return;
321 }
322 }
323
324 internal_error (__FILE__, __LINE__,
325 _("put_objfile_before: before objfile not in list"));
326 }
327
328 /* Put OBJFILE at the front of the list. */
329
330 void
331 objfile_to_front (struct objfile *objfile)
332 {
333 struct objfile **objp;
334 for (objp = &object_files; *objp != NULL; objp = &((*objp)->next))
335 {
336 if (*objp == objfile)
337 {
338 /* Unhook it from where it is. */
339 *objp = objfile->next;
340 /* Put it in the front. */
341 objfile->next = object_files;
342 object_files = objfile;
343 break;
344 }
345 }
346 }
347
348 /* Unlink OBJFILE from the list of known objfiles, if it is found in the
349 list.
350
351 It is not a bug, or error, to call this function if OBJFILE is not known
352 to be in the current list. This is done in the case of mapped objfiles,
353 for example, just to ensure that the mapped objfile doesn't appear twice
354 in the list. Since the list is threaded, linking in a mapped objfile
355 twice would create a circular list.
356
357 If OBJFILE turns out to be in the list, we zap it's NEXT pointer after
358 unlinking it, just to ensure that we have completely severed any linkages
359 between the OBJFILE and the list. */
360
361 void
362 unlink_objfile (struct objfile *objfile)
363 {
364 struct objfile **objpp;
365
366 for (objpp = &object_files; *objpp != NULL; objpp = &((*objpp)->next))
367 {
368 if (*objpp == objfile)
369 {
370 *objpp = (*objpp)->next;
371 objfile->next = NULL;
372 return;
373 }
374 }
375
376 internal_error (__FILE__, __LINE__,
377 _("unlink_objfile: objfile already unlinked"));
378 }
379
380
381 /* Destroy an objfile and all the symtabs and psymtabs under it. Note
382 that as much as possible is allocated on the objfile_obstack
383 so that the memory can be efficiently freed.
384
385 Things which we do NOT free because they are not in malloc'd memory
386 or not in memory specific to the objfile include:
387
388 objfile -> sf
389
390 FIXME: If the objfile is using reusable symbol information (via mmalloc),
391 then we need to take into account the fact that more than one process
392 may be using the symbol information at the same time (when mmalloc is
393 extended to support cooperative locking). When more than one process
394 is using the mapped symbol info, we need to be more careful about when
395 we free objects in the reusable area. */
396
397 void
398 free_objfile (struct objfile *objfile)
399 {
400 if (objfile->separate_debug_objfile)
401 {
402 free_objfile (objfile->separate_debug_objfile);
403 }
404
405 if (objfile->separate_debug_objfile_backlink)
406 {
407 /* We freed the separate debug file, make sure the base objfile
408 doesn't reference it. */
409 objfile->separate_debug_objfile_backlink->separate_debug_objfile = NULL;
410 }
411
412 /* Remove any references to this objfile in the global value
413 lists. */
414 preserve_values (objfile);
415
416 /* First do any symbol file specific actions required when we are
417 finished with a particular symbol file. Note that if the objfile
418 is using reusable symbol information (via mmalloc) then each of
419 these routines is responsible for doing the correct thing, either
420 freeing things which are valid only during this particular gdb
421 execution, or leaving them to be reused during the next one. */
422
423 if (objfile->sf != NULL)
424 {
425 (*objfile->sf->sym_finish) (objfile);
426 }
427
428 /* We always close the bfd. */
429
430 if (objfile->obfd != NULL)
431 {
432 char *name = bfd_get_filename (objfile->obfd);
433 if (!bfd_close (objfile->obfd))
434 warning (_("cannot close \"%s\": %s"),
435 name, bfd_errmsg (bfd_get_error ()));
436 xfree (name);
437 }
438
439 /* Remove it from the chain of all objfiles. */
440
441 unlink_objfile (objfile);
442
443 /* If we are going to free the runtime common objfile, mark it
444 as unallocated. */
445
446 if (objfile == rt_common_objfile)
447 rt_common_objfile = NULL;
448
449 /* Before the symbol table code was redone to make it easier to
450 selectively load and remove information particular to a specific
451 linkage unit, gdb used to do these things whenever the monolithic
452 symbol table was blown away. How much still needs to be done
453 is unknown, but we play it safe for now and keep each action until
454 it is shown to be no longer needed. */
455
456 /* Not all our callers call clear_symtab_users (objfile_purge_solibs,
457 for example), so we need to call this here. */
458 clear_pc_function_cache ();
459
460 /* Clear globals which might have pointed into a removed objfile.
461 FIXME: It's not clear which of these are supposed to persist
462 between expressions and which ought to be reset each time. */
463 expression_context_block = NULL;
464 innermost_block = NULL;
465
466 /* Check to see if the current_source_symtab belongs to this objfile,
467 and if so, call clear_current_source_symtab_and_line. */
468
469 {
470 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
471 struct symtab *s;
472
473 ALL_OBJFILE_SYMTABS (objfile, s)
474 {
475 if (s == cursal.symtab)
476 clear_current_source_symtab_and_line ();
477 }
478 }
479
480 /* The last thing we do is free the objfile struct itself. */
481
482 objfile_free_data (objfile);
483 if (objfile->name != NULL)
484 {
485 xfree (objfile->name);
486 }
487 if (objfile->global_psymbols.list)
488 xfree (objfile->global_psymbols.list);
489 if (objfile->static_psymbols.list)
490 xfree (objfile->static_psymbols.list);
491 /* Free the obstacks for non-reusable objfiles */
492 bcache_xfree (objfile->psymbol_cache);
493 bcache_xfree (objfile->macro_cache);
494 if (objfile->demangled_names_hash)
495 htab_delete (objfile->demangled_names_hash);
496 obstack_free (&objfile->objfile_obstack, 0);
497 xfree (objfile);
498 objfile = NULL;
499 }
500
501 static void
502 do_free_objfile_cleanup (void *obj)
503 {
504 free_objfile (obj);
505 }
506
507 struct cleanup *
508 make_cleanup_free_objfile (struct objfile *obj)
509 {
510 return make_cleanup (do_free_objfile_cleanup, obj);
511 }
512
513 /* Free all the object files at once and clean up their users. */
514
515 void
516 free_all_objfiles (void)
517 {
518 struct objfile *objfile, *temp;
519
520 ALL_OBJFILES_SAFE (objfile, temp)
521 {
522 free_objfile (objfile);
523 }
524 clear_symtab_users ();
525 }
526 \f
527 /* Relocate OBJFILE to NEW_OFFSETS. There should be OBJFILE->NUM_SECTIONS
528 entries in new_offsets. */
529 void
530 objfile_relocate (struct objfile *objfile, struct section_offsets *new_offsets)
531 {
532 struct obj_section *s;
533 struct section_offsets *delta =
534 ((struct section_offsets *)
535 alloca (SIZEOF_N_SECTION_OFFSETS (objfile->num_sections)));
536
537 {
538 int i;
539 int something_changed = 0;
540 for (i = 0; i < objfile->num_sections; ++i)
541 {
542 delta->offsets[i] =
543 ANOFFSET (new_offsets, i) - ANOFFSET (objfile->section_offsets, i);
544 if (ANOFFSET (delta, i) != 0)
545 something_changed = 1;
546 }
547 if (!something_changed)
548 return;
549 }
550
551 /* OK, get all the symtabs. */
552 {
553 struct symtab *s;
554
555 ALL_OBJFILE_SYMTABS (objfile, s)
556 {
557 struct linetable *l;
558 struct blockvector *bv;
559 int i;
560
561 /* First the line table. */
562 l = LINETABLE (s);
563 if (l)
564 {
565 for (i = 0; i < l->nitems; ++i)
566 l->item[i].pc += ANOFFSET (delta, s->block_line_section);
567 }
568
569 /* Don't relocate a shared blockvector more than once. */
570 if (!s->primary)
571 continue;
572
573 bv = BLOCKVECTOR (s);
574 for (i = 0; i < BLOCKVECTOR_NBLOCKS (bv); ++i)
575 {
576 struct block *b;
577 struct symbol *sym;
578 struct dict_iterator iter;
579
580 b = BLOCKVECTOR_BLOCK (bv, i);
581 BLOCK_START (b) += ANOFFSET (delta, s->block_line_section);
582 BLOCK_END (b) += ANOFFSET (delta, s->block_line_section);
583 if (BLOCKVECTOR_MAP (bv))
584 addrmap_relocate (BLOCKVECTOR_MAP (bv),
585 ANOFFSET (delta, s->block_line_section));
586
587 ALL_BLOCK_SYMBOLS (b, iter, sym)
588 {
589 fixup_symbol_section (sym, objfile);
590
591 /* The RS6000 code from which this was taken skipped
592 any symbols in STRUCT_DOMAIN or UNDEF_DOMAIN.
593 But I'm leaving out that test, on the theory that
594 they can't possibly pass the tests below. */
595 if ((SYMBOL_CLASS (sym) == LOC_LABEL
596 || SYMBOL_CLASS (sym) == LOC_STATIC)
597 && SYMBOL_SECTION (sym) >= 0)
598 {
599 SYMBOL_VALUE_ADDRESS (sym) +=
600 ANOFFSET (delta, SYMBOL_SECTION (sym));
601 }
602 }
603 }
604 }
605 }
606
607 {
608 struct partial_symtab *p;
609
610 ALL_OBJFILE_PSYMTABS (objfile, p)
611 {
612 p->textlow += ANOFFSET (delta, SECT_OFF_TEXT (objfile));
613 p->texthigh += ANOFFSET (delta, SECT_OFF_TEXT (objfile));
614 }
615 }
616
617 {
618 struct partial_symbol **psym;
619
620 for (psym = objfile->global_psymbols.list;
621 psym < objfile->global_psymbols.next;
622 psym++)
623 {
624 fixup_psymbol_section (*psym, objfile);
625 if (SYMBOL_SECTION (*psym) >= 0)
626 SYMBOL_VALUE_ADDRESS (*psym) += ANOFFSET (delta,
627 SYMBOL_SECTION (*psym));
628 }
629 for (psym = objfile->static_psymbols.list;
630 psym < objfile->static_psymbols.next;
631 psym++)
632 {
633 fixup_psymbol_section (*psym, objfile);
634 if (SYMBOL_SECTION (*psym) >= 0)
635 SYMBOL_VALUE_ADDRESS (*psym) += ANOFFSET (delta,
636 SYMBOL_SECTION (*psym));
637 }
638 }
639
640 {
641 struct minimal_symbol *msym;
642 ALL_OBJFILE_MSYMBOLS (objfile, msym)
643 if (SYMBOL_SECTION (msym) >= 0)
644 SYMBOL_VALUE_ADDRESS (msym) += ANOFFSET (delta, SYMBOL_SECTION (msym));
645 }
646 /* Relocating different sections by different amounts may cause the symbols
647 to be out of order. */
648 msymbols_sort (objfile);
649
650 {
651 int i;
652 for (i = 0; i < objfile->num_sections; ++i)
653 (objfile->section_offsets)->offsets[i] = ANOFFSET (new_offsets, i);
654 }
655
656 if (objfile->ei.entry_point != ~(CORE_ADDR) 0)
657 {
658 /* Relocate ei.entry_point with its section offset, use SECT_OFF_TEXT
659 only as a fallback. */
660 struct obj_section *s;
661 s = find_pc_section (objfile->ei.entry_point);
662 if (s)
663 objfile->ei.entry_point += ANOFFSET (delta, s->the_bfd_section->index);
664 else
665 objfile->ei.entry_point += ANOFFSET (delta, SECT_OFF_TEXT (objfile));
666 }
667
668 {
669 struct obj_section *s;
670 bfd *abfd;
671
672 abfd = objfile->obfd;
673
674 ALL_OBJFILE_OSECTIONS (objfile, s)
675 {
676 int idx = s->the_bfd_section->index;
677
678 s->addr += ANOFFSET (delta, idx);
679 s->endaddr += ANOFFSET (delta, idx);
680 }
681 }
682
683 /* Update the table in exec_ops, used to read memory. */
684 ALL_OBJFILE_OSECTIONS (objfile, s)
685 {
686 int idx = s->the_bfd_section->index;
687
688 exec_set_section_address (bfd_get_filename (objfile->obfd), idx,
689 s->addr);
690 }
691
692 /* Relocate breakpoints as necessary, after things are relocated. */
693 breakpoint_re_set ();
694 }
695 \f
696 /* Many places in gdb want to test just to see if we have any partial
697 symbols available. This function returns zero if none are currently
698 available, nonzero otherwise. */
699
700 int
701 have_partial_symbols (void)
702 {
703 struct objfile *ofp;
704
705 ALL_OBJFILES (ofp)
706 {
707 if (ofp->psymtabs != NULL)
708 {
709 return 1;
710 }
711 }
712 return 0;
713 }
714
715 /* Many places in gdb want to test just to see if we have any full
716 symbols available. This function returns zero if none are currently
717 available, nonzero otherwise. */
718
719 int
720 have_full_symbols (void)
721 {
722 struct objfile *ofp;
723
724 ALL_OBJFILES (ofp)
725 {
726 if (ofp->symtabs != NULL)
727 {
728 return 1;
729 }
730 }
731 return 0;
732 }
733
734
735 /* This operations deletes all objfile entries that represent solibs that
736 weren't explicitly loaded by the user, via e.g., the add-symbol-file
737 command.
738 */
739 void
740 objfile_purge_solibs (void)
741 {
742 struct objfile *objf;
743 struct objfile *temp;
744
745 ALL_OBJFILES_SAFE (objf, temp)
746 {
747 /* We assume that the solib package has been purged already, or will
748 be soon.
749 */
750 if (!(objf->flags & OBJF_USERLOADED) && (objf->flags & OBJF_SHARED))
751 free_objfile (objf);
752 }
753 }
754
755
756 /* Many places in gdb want to test just to see if we have any minimal
757 symbols available. This function returns zero if none are currently
758 available, nonzero otherwise. */
759
760 int
761 have_minimal_symbols (void)
762 {
763 struct objfile *ofp;
764
765 ALL_OBJFILES (ofp)
766 {
767 if (ofp->minimal_symbol_count > 0)
768 {
769 return 1;
770 }
771 }
772 return 0;
773 }
774
775 /* Returns a section whose range includes PC and SECTION, or NULL if
776 none found. Note the distinction between the return type, struct
777 obj_section (which is defined in gdb), and the input type "struct
778 bfd_section" (which is a bfd-defined data type). The obj_section
779 contains a pointer to the "struct bfd_section". */
780
781 struct obj_section *
782 find_pc_sect_section (CORE_ADDR pc, struct bfd_section *section)
783 {
784 struct obj_section *s;
785 struct objfile *objfile;
786
787 ALL_OBJSECTIONS (objfile, s)
788 if ((section == 0 || section == s->the_bfd_section) &&
789 s->addr <= pc && pc < s->endaddr)
790 return (s);
791
792 return (NULL);
793 }
794
795 /* Returns a section whose range includes PC or NULL if none found.
796 Backward compatibility, no section. */
797
798 struct obj_section *
799 find_pc_section (CORE_ADDR pc)
800 {
801 return find_pc_sect_section (pc, find_pc_mapped_section (pc));
802 }
803
804
805 /* In SVR4, we recognize a trampoline by it's section name.
806 That is, if the pc is in a section named ".plt" then we are in
807 a trampoline. */
808
809 int
810 in_plt_section (CORE_ADDR pc, char *name)
811 {
812 struct obj_section *s;
813 int retval = 0;
814
815 s = find_pc_section (pc);
816
817 retval = (s != NULL
818 && s->the_bfd_section->name != NULL
819 && strcmp (s->the_bfd_section->name, ".plt") == 0);
820 return (retval);
821 }
822 \f
823
824 /* Keep a registry of per-objfile data-pointers required by other GDB
825 modules. */
826
827 struct objfile_data
828 {
829 unsigned index;
830 void (*cleanup) (struct objfile *, void *);
831 };
832
833 struct objfile_data_registration
834 {
835 struct objfile_data *data;
836 struct objfile_data_registration *next;
837 };
838
839 struct objfile_data_registry
840 {
841 struct objfile_data_registration *registrations;
842 unsigned num_registrations;
843 };
844
845 static struct objfile_data_registry objfile_data_registry = { NULL, 0 };
846
847 const struct objfile_data *
848 register_objfile_data_with_cleanup (void (*cleanup) (struct objfile *, void *))
849 {
850 struct objfile_data_registration **curr;
851
852 /* Append new registration. */
853 for (curr = &objfile_data_registry.registrations;
854 *curr != NULL; curr = &(*curr)->next);
855
856 *curr = XMALLOC (struct objfile_data_registration);
857 (*curr)->next = NULL;
858 (*curr)->data = XMALLOC (struct objfile_data);
859 (*curr)->data->index = objfile_data_registry.num_registrations++;
860 (*curr)->data->cleanup = cleanup;
861
862 return (*curr)->data;
863 }
864
865 const struct objfile_data *
866 register_objfile_data (void)
867 {
868 return register_objfile_data_with_cleanup (NULL);
869 }
870
871 static void
872 objfile_alloc_data (struct objfile *objfile)
873 {
874 gdb_assert (objfile->data == NULL);
875 objfile->num_data = objfile_data_registry.num_registrations;
876 objfile->data = XCALLOC (objfile->num_data, void *);
877 }
878
879 static void
880 objfile_free_data (struct objfile *objfile)
881 {
882 gdb_assert (objfile->data != NULL);
883 clear_objfile_data (objfile);
884 xfree (objfile->data);
885 objfile->data = NULL;
886 }
887
888 void
889 clear_objfile_data (struct objfile *objfile)
890 {
891 struct objfile_data_registration *registration;
892 int i;
893
894 gdb_assert (objfile->data != NULL);
895
896 for (registration = objfile_data_registry.registrations, i = 0;
897 i < objfile->num_data;
898 registration = registration->next, i++)
899 if (objfile->data[i] != NULL && registration->data->cleanup)
900 registration->data->cleanup (objfile, objfile->data[i]);
901
902 memset (objfile->data, 0, objfile->num_data * sizeof (void *));
903 }
904
905 void
906 set_objfile_data (struct objfile *objfile, const struct objfile_data *data,
907 void *value)
908 {
909 gdb_assert (data->index < objfile->num_data);
910 objfile->data[data->index] = value;
911 }
912
913 void *
914 objfile_data (struct objfile *objfile, const struct objfile_data *data)
915 {
916 gdb_assert (data->index < objfile->num_data);
917 return objfile->data[data->index];
918 }
This page took 0.048454 seconds and 4 git commands to generate.