OSDN Git Service

92db65ef7c061f89f666f803108b6eec74323a2a
[pf3gnuchains/pf3gnuchains4x.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, 2009 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 #include "observer.h"
54
55 /* Prototypes for local functions */
56
57 static void objfile_alloc_data (struct objfile *objfile);
58 static void objfile_free_data (struct objfile *objfile);
59
60 /* Externally visible variables that are owned by this module.
61    See declarations in objfile.h for more info. */
62
63 struct objfile *object_files;   /* Linked list of all objfiles */
64 struct objfile *current_objfile;        /* For symbol file being read in */
65 struct objfile *symfile_objfile;        /* Main symbol table loaded from */
66 struct objfile *rt_common_objfile;      /* For runtime common symbols */
67
68 /* Records whether any objfiles appeared or disappeared since we last updated
69    address to obj section map.  */
70
71 static int objfiles_changed_p;
72
73 /* Locate all mappable sections of a BFD file. 
74    objfile_p_char is a char * to get it through
75    bfd_map_over_sections; we cast it back to its proper type.  */
76
77 /* Called via bfd_map_over_sections to build up the section table that
78    the objfile references.  The objfile contains pointers to the start
79    of the table (objfile->sections) and to the first location after
80    the end of the table (objfile->sections_end). */
81
82 static void
83 add_to_objfile_sections (struct bfd *abfd, struct bfd_section *asect,
84                          void *objfile_p_char)
85 {
86   struct objfile *objfile = (struct objfile *) objfile_p_char;
87   struct obj_section section;
88   flagword aflag;
89
90   aflag = bfd_get_section_flags (abfd, asect);
91
92   if (!(aflag & SEC_ALLOC))
93     return;
94
95   if (0 == bfd_section_size (abfd, asect))
96     return;
97   section.objfile = objfile;
98   section.the_bfd_section = asect;
99   section.ovly_mapped = 0;
100   obstack_grow (&objfile->objfile_obstack, (char *) &section, sizeof (section));
101   objfile->sections_end
102     = (struct obj_section *) (((size_t) objfile->sections_end) + 1);
103 }
104
105 /* Builds a section table for OBJFILE.
106    Returns 0 if OK, 1 on error (in which case bfd_error contains the
107    error).
108
109    Note that while we are building the table, which goes into the
110    psymbol obstack, we hijack the sections_end pointer to instead hold
111    a count of the number of sections.  When bfd_map_over_sections
112    returns, this count is used to compute the pointer to the end of
113    the sections table, which then overwrites the count.
114
115    Also note that the OFFSET and OVLY_MAPPED in each table entry
116    are initialized to zero.
117
118    Also note that if anything else writes to the psymbol obstack while
119    we are building the table, we're pretty much hosed. */
120
121 int
122 build_objfile_section_table (struct objfile *objfile)
123 {
124   /* objfile->sections can be already set when reading a mapped symbol
125      file.  I believe that we do need to rebuild the section table in
126      this case (we rebuild other things derived from the bfd), but we
127      can't free the old one (it's in the objfile_obstack).  So we just
128      waste some memory.  */
129
130   objfile->sections_end = 0;
131   bfd_map_over_sections (objfile->obfd,
132                          add_to_objfile_sections, (void *) objfile);
133   objfile->sections = obstack_finish (&objfile->objfile_obstack);
134   objfile->sections_end = objfile->sections + (size_t) objfile->sections_end;
135   return (0);
136 }
137
138 /* Given a pointer to an initialized bfd (ABFD) and some flag bits
139    allocate a new objfile struct, fill it in as best we can, link it
140    into the list of all known objfiles, and return a pointer to the
141    new objfile struct.
142
143    The FLAGS word contains various bits (OBJF_*) that can be taken as
144    requests for specific operations.  Other bits like OBJF_SHARED are
145    simply copied through to the new objfile flags member. */
146
147 /* NOTE: carlton/2003-02-04: This function is called with args NULL, 0
148    by jv-lang.c, to create an artificial objfile used to hold
149    information about dynamically-loaded Java classes.  Unfortunately,
150    that branch of this function doesn't get tested very frequently, so
151    it's prone to breakage.  (E.g. at one time the name was set to NULL
152    in that situation, which broke a loop over all names in the dynamic
153    library loader.)  If you change this function, please try to leave
154    things in a consistent state even if abfd is NULL.  */
155
156 struct objfile *
157 allocate_objfile (bfd *abfd, int flags)
158 {
159   struct objfile *objfile = NULL;
160   struct objfile *last_one = NULL;
161
162   /* If we don't support mapped symbol files, didn't ask for the file to be
163      mapped, or failed to open the mapped file for some reason, then revert
164      back to an unmapped objfile. */
165
166   if (objfile == NULL)
167     {
168       objfile = (struct objfile *) xmalloc (sizeof (struct objfile));
169       memset (objfile, 0, sizeof (struct objfile));
170       objfile->psymbol_cache = bcache_xmalloc ();
171       objfile->macro_cache = bcache_xmalloc ();
172       /* We could use obstack_specify_allocation here instead, but
173          gdb_obstack.h specifies the alloc/dealloc functions.  */
174       obstack_init (&objfile->objfile_obstack);
175       terminate_minimal_symbol_table (objfile);
176     }
177
178   objfile_alloc_data (objfile);
179
180   /* Update the per-objfile information that comes from the bfd, ensuring
181      that any data that is reference is saved in the per-objfile data
182      region. */
183
184   objfile->obfd = abfd;
185   if (objfile->name != NULL)
186     {
187       xfree (objfile->name);
188     }
189   if (abfd != NULL)
190     {
191       /* Look up the gdbarch associated with the BFD.  */
192       objfile->gdbarch = gdbarch_from_bfd (abfd);
193
194       objfile->name = xstrdup (bfd_get_filename (abfd));
195       objfile->mtime = bfd_get_mtime (abfd);
196
197       /* Build section table.  */
198
199       if (build_objfile_section_table (objfile))
200         {
201           error (_("Can't find the file sections in `%s': %s"),
202                  objfile->name, bfd_errmsg (bfd_get_error ()));
203         }
204     }
205   else
206     {
207       objfile->name = xstrdup ("<<anonymous objfile>>");
208     }
209
210   /* Initialize the section indexes for this objfile, so that we can
211      later detect if they are used w/o being properly assigned to. */
212
213   objfile->sect_index_text = -1;
214   objfile->sect_index_data = -1;
215   objfile->sect_index_bss = -1;
216   objfile->sect_index_rodata = -1;
217
218   /* We don't yet have a C++-specific namespace symtab.  */
219
220   objfile->cp_namespace_symtab = NULL;
221
222   /* Add this file onto the tail of the linked list of other such files. */
223
224   objfile->next = NULL;
225   if (object_files == NULL)
226     object_files = objfile;
227   else
228     {
229       for (last_one = object_files;
230            last_one->next;
231            last_one = last_one->next);
232       last_one->next = objfile;
233     }
234
235   /* Save passed in flag bits. */
236   objfile->flags |= flags;
237
238   objfiles_changed_p = 1;  /* Rebuild section map next time we need it.  */
239
240   return (objfile);
241 }
242
243 /* Retrieve the gdbarch associated with OBJFILE.  */
244 struct gdbarch *
245 get_objfile_arch (struct objfile *objfile)
246 {
247   return objfile->gdbarch;
248 }
249
250 /* Initialize entry point information for this objfile. */
251
252 void
253 init_entry_point_info (struct objfile *objfile)
254 {
255   /* Save startup file's range of PC addresses to help blockframe.c
256      decide where the bottom of the stack is.  */
257
258   if (bfd_get_file_flags (objfile->obfd) & EXEC_P)
259     {
260       /* Executable file -- record its entry point so we'll recognize
261          the startup file because it contains the entry point.  */
262       objfile->ei.entry_point = bfd_get_start_address (objfile->obfd);
263     }
264   else if (bfd_get_file_flags (objfile->obfd) & DYNAMIC
265            && bfd_get_start_address (objfile->obfd) != 0)
266     /* Some shared libraries may have entry points set and be
267        runnable.  There's no clear way to indicate this, so just check
268        for values other than zero.  */
269     objfile->ei.entry_point = bfd_get_start_address (objfile->obfd);    
270   else
271     {
272       /* Examination of non-executable.o files.  Short-circuit this stuff.  */
273       objfile->ei.entry_point = INVALID_ENTRY_POINT;
274     }
275 }
276
277 /* Get current entry point address.  */
278
279 CORE_ADDR
280 entry_point_address (void)
281 {
282   struct gdbarch *gdbarch;
283   CORE_ADDR entry_point;
284
285   if (symfile_objfile == NULL)
286     return 0;
287
288   gdbarch = get_objfile_arch (symfile_objfile);
289
290   entry_point = symfile_objfile->ei.entry_point;
291
292   /* Make certain that the address points at real code, and not a
293      function descriptor.  */
294   entry_point = gdbarch_convert_from_func_ptr_addr (gdbarch, entry_point,
295                                                     &current_target);
296
297   /* Remove any ISA markers, so that this matches entries in the
298      symbol table.  */
299   entry_point = gdbarch_addr_bits_remove (gdbarch, entry_point);
300
301   return entry_point;
302 }
303
304 /* Create the terminating entry of OBJFILE's minimal symbol table.
305    If OBJFILE->msymbols is zero, allocate a single entry from
306    OBJFILE->objfile_obstack; otherwise, just initialize
307    OBJFILE->msymbols[OBJFILE->minimal_symbol_count].  */
308 void
309 terminate_minimal_symbol_table (struct objfile *objfile)
310 {
311   if (! objfile->msymbols)
312     objfile->msymbols = ((struct minimal_symbol *)
313                          obstack_alloc (&objfile->objfile_obstack,
314                                         sizeof (objfile->msymbols[0])));
315
316   {
317     struct minimal_symbol *m
318       = &objfile->msymbols[objfile->minimal_symbol_count];
319
320     memset (m, 0, sizeof (*m));
321     /* Don't rely on these enumeration values being 0's.  */
322     MSYMBOL_TYPE (m) = mst_unknown;
323     SYMBOL_INIT_LANGUAGE_SPECIFIC (m, language_unknown);
324   }
325 }
326
327
328 /* Put one object file before a specified on in the global list.
329    This can be used to make sure an object file is destroyed before
330    another when using ALL_OBJFILES_SAFE to free all objfiles. */
331 void
332 put_objfile_before (struct objfile *objfile, struct objfile *before_this)
333 {
334   struct objfile **objp;
335
336   unlink_objfile (objfile);
337   
338   for (objp = &object_files; *objp != NULL; objp = &((*objp)->next))
339     {
340       if (*objp == before_this)
341         {
342           objfile->next = *objp;
343           *objp = objfile;
344           return;
345         }
346     }
347   
348   internal_error (__FILE__, __LINE__,
349                   _("put_objfile_before: before objfile not in list"));
350 }
351
352 /* Put OBJFILE at the front of the list.  */
353
354 void
355 objfile_to_front (struct objfile *objfile)
356 {
357   struct objfile **objp;
358   for (objp = &object_files; *objp != NULL; objp = &((*objp)->next))
359     {
360       if (*objp == objfile)
361         {
362           /* Unhook it from where it is.  */
363           *objp = objfile->next;
364           /* Put it in the front.  */
365           objfile->next = object_files;
366           object_files = objfile;
367           break;
368         }
369     }
370 }
371
372 /* Unlink OBJFILE from the list of known objfiles, if it is found in the
373    list.
374
375    It is not a bug, or error, to call this function if OBJFILE is not known
376    to be in the current list.  This is done in the case of mapped objfiles,
377    for example, just to ensure that the mapped objfile doesn't appear twice
378    in the list.  Since the list is threaded, linking in a mapped objfile
379    twice would create a circular list.
380
381    If OBJFILE turns out to be in the list, we zap it's NEXT pointer after
382    unlinking it, just to ensure that we have completely severed any linkages
383    between the OBJFILE and the list. */
384
385 void
386 unlink_objfile (struct objfile *objfile)
387 {
388   struct objfile **objpp;
389
390   for (objpp = &object_files; *objpp != NULL; objpp = &((*objpp)->next))
391     {
392       if (*objpp == objfile)
393         {
394           *objpp = (*objpp)->next;
395           objfile->next = NULL;
396           return;
397         }
398     }
399
400   internal_error (__FILE__, __LINE__,
401                   _("unlink_objfile: objfile already unlinked"));
402 }
403
404
405 /* Destroy an objfile and all the symtabs and psymtabs under it.  Note
406    that as much as possible is allocated on the objfile_obstack 
407    so that the memory can be efficiently freed.
408
409    Things which we do NOT free because they are not in malloc'd memory
410    or not in memory specific to the objfile include:
411
412    objfile -> sf
413
414    FIXME:  If the objfile is using reusable symbol information (via mmalloc),
415    then we need to take into account the fact that more than one process
416    may be using the symbol information at the same time (when mmalloc is
417    extended to support cooperative locking).  When more than one process
418    is using the mapped symbol info, we need to be more careful about when
419    we free objects in the reusable area. */
420
421 void
422 free_objfile (struct objfile *objfile)
423 {
424   if (objfile->separate_debug_objfile)
425     {
426       free_objfile (objfile->separate_debug_objfile);
427     }
428   
429   if (objfile->separate_debug_objfile_backlink)
430     {
431       /* We freed the separate debug file, make sure the base objfile
432          doesn't reference it.  */
433       objfile->separate_debug_objfile_backlink->separate_debug_objfile = NULL;
434     }
435   
436   /* Remove any references to this objfile in the global value
437      lists.  */
438   preserve_values (objfile);
439
440   /* First do any symbol file specific actions required when we are
441      finished with a particular symbol file.  Note that if the objfile
442      is using reusable symbol information (via mmalloc) then each of
443      these routines is responsible for doing the correct thing, either
444      freeing things which are valid only during this particular gdb
445      execution, or leaving them to be reused during the next one. */
446
447   if (objfile->sf != NULL)
448     {
449       (*objfile->sf->sym_finish) (objfile);
450     }
451
452   /* Discard any data modules have associated with the objfile.  */
453   objfile_free_data (objfile);
454
455   gdb_bfd_unref (objfile->obfd);
456
457   /* Remove it from the chain of all objfiles. */
458
459   unlink_objfile (objfile);
460
461   /* If we are going to free the runtime common objfile, mark it
462      as unallocated.  */
463
464   if (objfile == rt_common_objfile)
465     rt_common_objfile = NULL;
466
467   /* Before the symbol table code was redone to make it easier to
468      selectively load and remove information particular to a specific
469      linkage unit, gdb used to do these things whenever the monolithic
470      symbol table was blown away.  How much still needs to be done
471      is unknown, but we play it safe for now and keep each action until
472      it is shown to be no longer needed. */
473
474   /* Not all our callers call clear_symtab_users (objfile_purge_solibs,
475      for example), so we need to call this here.  */
476   clear_pc_function_cache ();
477
478   /* Clear globals which might have pointed into a removed objfile.
479      FIXME: It's not clear which of these are supposed to persist
480      between expressions and which ought to be reset each time.  */
481   expression_context_block = NULL;
482   innermost_block = NULL;
483
484   /* Check to see if the current_source_symtab belongs to this objfile,
485      and if so, call clear_current_source_symtab_and_line. */
486
487   {
488     struct symtab_and_line cursal = get_current_source_symtab_and_line ();
489     struct symtab *s;
490
491     ALL_OBJFILE_SYMTABS (objfile, s)
492       {
493         if (s == cursal.symtab)
494           clear_current_source_symtab_and_line ();
495       }
496   }
497
498   /* The last thing we do is free the objfile struct itself. */
499
500   if (objfile->name != NULL)
501     {
502       xfree (objfile->name);
503     }
504   if (objfile->global_psymbols.list)
505     xfree (objfile->global_psymbols.list);
506   if (objfile->static_psymbols.list)
507     xfree (objfile->static_psymbols.list);
508   /* Free the obstacks for non-reusable objfiles */
509   bcache_xfree (objfile->psymbol_cache);
510   bcache_xfree (objfile->macro_cache);
511   if (objfile->demangled_names_hash)
512     htab_delete (objfile->demangled_names_hash);
513   obstack_free (&objfile->objfile_obstack, 0);
514   xfree (objfile);
515   objfile = NULL;
516   objfiles_changed_p = 1;  /* Rebuild section map next time we need it.  */
517 }
518
519 static void
520 do_free_objfile_cleanup (void *obj)
521 {
522   free_objfile (obj);
523 }
524
525 struct cleanup *
526 make_cleanup_free_objfile (struct objfile *obj)
527 {
528   return make_cleanup (do_free_objfile_cleanup, obj);
529 }
530
531 /* Free all the object files at once and clean up their users.  */
532
533 void
534 free_all_objfiles (void)
535 {
536   struct objfile *objfile, *temp;
537
538   ALL_OBJFILES_SAFE (objfile, temp)
539   {
540     free_objfile (objfile);
541   }
542   clear_symtab_users ();
543 }
544 \f
545 /* Relocate OBJFILE to NEW_OFFSETS.  There should be OBJFILE->NUM_SECTIONS
546    entries in new_offsets.  */
547 void
548 objfile_relocate (struct objfile *objfile, struct section_offsets *new_offsets)
549 {
550   struct obj_section *s;
551   struct section_offsets *delta =
552     ((struct section_offsets *) 
553      alloca (SIZEOF_N_SECTION_OFFSETS (objfile->num_sections)));
554
555   {
556     int i;
557     int something_changed = 0;
558     for (i = 0; i < objfile->num_sections; ++i)
559       {
560         delta->offsets[i] =
561           ANOFFSET (new_offsets, i) - ANOFFSET (objfile->section_offsets, i);
562         if (ANOFFSET (delta, i) != 0)
563           something_changed = 1;
564       }
565     if (!something_changed)
566       return;
567   }
568
569   /* OK, get all the symtabs.  */
570   {
571     struct symtab *s;
572
573     ALL_OBJFILE_SYMTABS (objfile, s)
574     {
575       struct linetable *l;
576       struct blockvector *bv;
577       int i;
578
579       /* First the line table.  */
580       l = LINETABLE (s);
581       if (l)
582         {
583           for (i = 0; i < l->nitems; ++i)
584             l->item[i].pc += ANOFFSET (delta, s->block_line_section);
585         }
586
587       /* Don't relocate a shared blockvector more than once.  */
588       if (!s->primary)
589         continue;
590
591       bv = BLOCKVECTOR (s);
592       if (BLOCKVECTOR_MAP (bv))
593         addrmap_relocate (BLOCKVECTOR_MAP (bv),
594                           ANOFFSET (delta, s->block_line_section));
595
596       for (i = 0; i < BLOCKVECTOR_NBLOCKS (bv); ++i)
597         {
598           struct block *b;
599           struct symbol *sym;
600           struct dict_iterator iter;
601
602           b = BLOCKVECTOR_BLOCK (bv, i);
603           BLOCK_START (b) += ANOFFSET (delta, s->block_line_section);
604           BLOCK_END (b) += ANOFFSET (delta, s->block_line_section);
605
606           ALL_BLOCK_SYMBOLS (b, iter, sym)
607             {
608               fixup_symbol_section (sym, objfile);
609
610               /* The RS6000 code from which this was taken skipped
611                  any symbols in STRUCT_DOMAIN or UNDEF_DOMAIN.
612                  But I'm leaving out that test, on the theory that
613                  they can't possibly pass the tests below.  */
614               if ((SYMBOL_CLASS (sym) == LOC_LABEL
615                    || SYMBOL_CLASS (sym) == LOC_STATIC)
616                   && SYMBOL_SECTION (sym) >= 0)
617                 {
618                   SYMBOL_VALUE_ADDRESS (sym) +=
619                     ANOFFSET (delta, SYMBOL_SECTION (sym));
620                 }
621             }
622         }
623     }
624   }
625
626   {
627     struct partial_symtab *p;
628
629     ALL_OBJFILE_PSYMTABS (objfile, p)
630     {
631       p->textlow += ANOFFSET (delta, SECT_OFF_TEXT (objfile));
632       p->texthigh += ANOFFSET (delta, SECT_OFF_TEXT (objfile));
633     }
634   }
635
636   {
637     struct partial_symbol **psym;
638
639     for (psym = objfile->global_psymbols.list;
640          psym < objfile->global_psymbols.next;
641          psym++)
642       {
643         fixup_psymbol_section (*psym, objfile);
644         if (SYMBOL_SECTION (*psym) >= 0)
645           SYMBOL_VALUE_ADDRESS (*psym) += ANOFFSET (delta,
646                                                     SYMBOL_SECTION (*psym));
647       }
648     for (psym = objfile->static_psymbols.list;
649          psym < objfile->static_psymbols.next;
650          psym++)
651       {
652         fixup_psymbol_section (*psym, objfile);
653         if (SYMBOL_SECTION (*psym) >= 0)
654           SYMBOL_VALUE_ADDRESS (*psym) += ANOFFSET (delta,
655                                                     SYMBOL_SECTION (*psym));
656       }
657   }
658
659   {
660     struct minimal_symbol *msym;
661     ALL_OBJFILE_MSYMBOLS (objfile, msym)
662       if (SYMBOL_SECTION (msym) >= 0)
663       SYMBOL_VALUE_ADDRESS (msym) += ANOFFSET (delta, SYMBOL_SECTION (msym));
664   }
665   /* Relocating different sections by different amounts may cause the symbols
666      to be out of order.  */
667   msymbols_sort (objfile);
668
669   if (objfile->ei.entry_point != ~(CORE_ADDR) 0)
670     {
671       /* Relocate ei.entry_point with its section offset, use SECT_OFF_TEXT
672          only as a fallback.  */
673       struct obj_section *s;
674       s = find_pc_section (objfile->ei.entry_point);
675       if (s)
676         objfile->ei.entry_point += ANOFFSET (delta, s->the_bfd_section->index);
677       else
678         objfile->ei.entry_point += ANOFFSET (delta, SECT_OFF_TEXT (objfile));
679     }
680
681   {
682     int i;
683     for (i = 0; i < objfile->num_sections; ++i)
684       (objfile->section_offsets)->offsets[i] = ANOFFSET (new_offsets, i);
685   }
686
687   /* Rebuild section map next time we need it.  */
688   objfiles_changed_p = 1;
689
690   /* Update the table in exec_ops, used to read memory.  */
691   ALL_OBJFILE_OSECTIONS (objfile, s)
692     {
693       int idx = s->the_bfd_section->index;
694
695       exec_set_section_address (bfd_get_filename (objfile->obfd), idx,
696                                 obj_section_addr (s));
697     }
698
699   /* Relocate breakpoints as necessary, after things are relocated. */
700   breakpoint_re_set ();
701 }
702 \f
703 /* Many places in gdb want to test just to see if we have any partial
704    symbols available.  This function returns zero if none are currently
705    available, nonzero otherwise. */
706
707 int
708 have_partial_symbols (void)
709 {
710   struct objfile *ofp;
711
712   ALL_OBJFILES (ofp)
713   {
714     if (ofp->psymtabs != NULL)
715       {
716         return 1;
717       }
718   }
719   return 0;
720 }
721
722 /* Many places in gdb want to test just to see if we have any full
723    symbols available.  This function returns zero if none are currently
724    available, nonzero otherwise. */
725
726 int
727 have_full_symbols (void)
728 {
729   struct objfile *ofp;
730
731   ALL_OBJFILES (ofp)
732   {
733     if (ofp->symtabs != NULL)
734       {
735         return 1;
736       }
737   }
738   return 0;
739 }
740
741
742 /* This operations deletes all objfile entries that represent solibs that
743    weren't explicitly loaded by the user, via e.g., the add-symbol-file
744    command.
745  */
746 void
747 objfile_purge_solibs (void)
748 {
749   struct objfile *objf;
750   struct objfile *temp;
751
752   ALL_OBJFILES_SAFE (objf, temp)
753   {
754     /* We assume that the solib package has been purged already, or will
755        be soon.
756      */
757     if (!(objf->flags & OBJF_USERLOADED) && (objf->flags & OBJF_SHARED))
758       free_objfile (objf);
759   }
760 }
761
762
763 /* Many places in gdb want to test just to see if we have any minimal
764    symbols available.  This function returns zero if none are currently
765    available, nonzero otherwise. */
766
767 int
768 have_minimal_symbols (void)
769 {
770   struct objfile *ofp;
771
772   ALL_OBJFILES (ofp)
773   {
774     if (ofp->minimal_symbol_count > 0)
775       {
776         return 1;
777       }
778   }
779   return 0;
780 }
781
782 /* Qsort comparison function.  */
783
784 static int
785 qsort_cmp (const void *a, const void *b)
786 {
787   const struct obj_section *sect1 = *(const struct obj_section **) a;
788   const struct obj_section *sect2 = *(const struct obj_section **) b;
789   const CORE_ADDR sect1_addr = obj_section_addr (sect1);
790   const CORE_ADDR sect2_addr = obj_section_addr (sect2);
791
792   if (sect1_addr < sect2_addr)
793     {
794       gdb_assert (obj_section_endaddr (sect1) <= sect2_addr);
795       return -1;
796     }
797   else if (sect1_addr > sect2_addr)
798     {
799       gdb_assert (sect1_addr >= obj_section_endaddr (sect2));
800       return 1;
801     }
802
803   return 0;
804 }
805
806 /* Select "better" obj_section to keep.  We prefer the one that came from
807    the real object, rather than the one from separate debuginfo.
808    Most of the time the two sections are exactly identical, but with
809    prelinking the .rel.dyn section in the real object may have different
810    size.  */
811
812 static struct obj_section *
813 preferred_obj_section (struct obj_section *a, struct obj_section *b)
814 {
815   gdb_assert (obj_section_addr (a) == obj_section_addr (b));
816   gdb_assert ((a->objfile->separate_debug_objfile == b->objfile)
817               || (b->objfile->separate_debug_objfile == a->objfile));
818   gdb_assert ((a->objfile->separate_debug_objfile_backlink == b->objfile)
819               || (b->objfile->separate_debug_objfile_backlink == a->objfile));
820
821   if (a->objfile->separate_debug_objfile != NULL)
822     return a;
823   return b;
824 }
825
826 /* Update PMAP, PMAP_SIZE with non-TLS sections from all objfiles.  */
827
828 static void
829 update_section_map (struct obj_section ***pmap, int *pmap_size)
830 {
831   int map_size, i, j;
832   struct obj_section *s, **map;
833   struct objfile *objfile;
834
835   gdb_assert (objfiles_changed_p != 0);
836
837   map = *pmap;
838   xfree (map);
839
840 #define insert_p(objf, sec) \
841   ((bfd_get_section_flags ((objf)->obfd, (sec)->the_bfd_section) \
842     & SEC_THREAD_LOCAL) == 0)
843
844   map_size = 0;
845   ALL_OBJSECTIONS (objfile, s)
846     if (insert_p (objfile, s))
847       map_size += 1;
848
849   map = xmalloc (map_size * sizeof (*map));
850
851   i = 0;
852   ALL_OBJSECTIONS (objfile, s)
853     if (insert_p (objfile, s))
854       map[i++] = s;
855
856 #undef insert_p
857
858   qsort (map, map_size, sizeof (*map), qsort_cmp);
859
860   /* With separate debuginfo files, we may have up to two (almost)
861      identical copies of some obj_sections in the map.
862      Filter out duplicates.  */
863   for (i = 0, j = 0; i < map_size; ++i)
864     {
865       struct obj_section *sect1 = map[i];
866       struct obj_section *sect2 = (i + 1 < map_size) ? map[i + 1] : NULL;
867
868       if (sect2 == NULL
869           || obj_section_addr (sect1) != obj_section_addr (sect2))
870         map[j++] = sect1;
871       else
872         {
873           map[j++] = preferred_obj_section (sect1, sect2);
874           ++i;
875         }
876     }
877
878   if (j < map_size)
879     {
880       /* Some duplicates were eliminated.
881          The new size shouldn't be less than half of the original. */
882       gdb_assert (map_size / 2 <= j);
883       map_size = j;
884
885       map = xrealloc (map, map_size * sizeof (*map));  /* Trim excess space.  */
886     }
887   else
888     gdb_assert (j == map_size);
889
890   *pmap = map;
891   *pmap_size = map_size;
892 }
893
894 /* Bsearch comparison function. */
895
896 static int
897 bsearch_cmp (const void *key, const void *elt)
898 {
899   const CORE_ADDR pc = *(CORE_ADDR *) key;
900   const struct obj_section *section = *(const struct obj_section **) elt;
901
902   if (pc < obj_section_addr (section))
903     return -1;
904   if (pc < obj_section_endaddr (section))
905     return 0;
906   return 1;
907 }
908
909 /* Returns a section whose range includes PC or NULL if none found.   */
910
911 struct obj_section *
912 find_pc_section (CORE_ADDR pc)
913 {
914   static struct obj_section **sections;
915   static int num_sections;
916
917   struct obj_section *s, **sp;
918
919   /* Check for mapped overlay section first.  */
920   s = find_pc_mapped_section (pc);
921   if (s)
922     return s;
923
924   if (objfiles_changed_p != 0)
925     {
926       update_section_map (&sections, &num_sections);
927
928       /* Don't need updates to section map until objfiles are added
929          or removed.  */
930       objfiles_changed_p = 0;
931     }
932
933   sp = (struct obj_section **) bsearch (&pc, sections, num_sections,
934                                         sizeof (*sections), bsearch_cmp);
935   if (sp != NULL)
936     return *sp;
937   return NULL;
938 }
939
940
941 /* In SVR4, we recognize a trampoline by it's section name. 
942    That is, if the pc is in a section named ".plt" then we are in
943    a trampoline.  */
944
945 int
946 in_plt_section (CORE_ADDR pc, char *name)
947 {
948   struct obj_section *s;
949   int retval = 0;
950
951   s = find_pc_section (pc);
952
953   retval = (s != NULL
954             && s->the_bfd_section->name != NULL
955             && strcmp (s->the_bfd_section->name, ".plt") == 0);
956   return (retval);
957 }
958 \f
959
960 /* Keep a registry of per-objfile data-pointers required by other GDB
961    modules.  */
962
963 struct objfile_data
964 {
965   unsigned index;
966   void (*cleanup) (struct objfile *, void *);
967 };
968
969 struct objfile_data_registration
970 {
971   struct objfile_data *data;
972   struct objfile_data_registration *next;
973 };
974   
975 struct objfile_data_registry
976 {
977   struct objfile_data_registration *registrations;
978   unsigned num_registrations;
979 };
980
981 static struct objfile_data_registry objfile_data_registry = { NULL, 0 };
982
983 const struct objfile_data *
984 register_objfile_data_with_cleanup (void (*cleanup) (struct objfile *, void *))
985 {
986   struct objfile_data_registration **curr;
987
988   /* Append new registration.  */
989   for (curr = &objfile_data_registry.registrations;
990        *curr != NULL; curr = &(*curr)->next);
991
992   *curr = XMALLOC (struct objfile_data_registration);
993   (*curr)->next = NULL;
994   (*curr)->data = XMALLOC (struct objfile_data);
995   (*curr)->data->index = objfile_data_registry.num_registrations++;
996   (*curr)->data->cleanup = cleanup;
997
998   return (*curr)->data;
999 }
1000
1001 const struct objfile_data *
1002 register_objfile_data (void)
1003 {
1004   return register_objfile_data_with_cleanup (NULL);
1005 }
1006
1007 static void
1008 objfile_alloc_data (struct objfile *objfile)
1009 {
1010   gdb_assert (objfile->data == NULL);
1011   objfile->num_data = objfile_data_registry.num_registrations;
1012   objfile->data = XCALLOC (objfile->num_data, void *);
1013 }
1014
1015 static void
1016 objfile_free_data (struct objfile *objfile)
1017 {
1018   gdb_assert (objfile->data != NULL);
1019   clear_objfile_data (objfile);
1020   xfree (objfile->data);
1021   objfile->data = NULL;
1022 }
1023
1024 void
1025 clear_objfile_data (struct objfile *objfile)
1026 {
1027   struct objfile_data_registration *registration;
1028   int i;
1029
1030   gdb_assert (objfile->data != NULL);
1031
1032   for (registration = objfile_data_registry.registrations, i = 0;
1033        i < objfile->num_data;
1034        registration = registration->next, i++)
1035     if (objfile->data[i] != NULL && registration->data->cleanup)
1036       registration->data->cleanup (objfile, objfile->data[i]);
1037
1038   memset (objfile->data, 0, objfile->num_data * sizeof (void *));
1039 }
1040
1041 void
1042 set_objfile_data (struct objfile *objfile, const struct objfile_data *data,
1043                   void *value)
1044 {
1045   gdb_assert (data->index < objfile->num_data);
1046   objfile->data[data->index] = value;
1047 }
1048
1049 void *
1050 objfile_data (struct objfile *objfile, const struct objfile_data *data)
1051 {
1052   gdb_assert (data->index < objfile->num_data);
1053   return objfile->data[data->index];
1054 }
1055
1056 /* Set objfiles_changed_p so section map will be rebuilt next time it
1057    is used.  Called by reread_symbols.  */
1058
1059 void
1060 objfiles_changed (void)
1061 {
1062   objfiles_changed_p = 1;  /* Rebuild section map next time we need it.  */
1063 }
1064
1065 /* Unreference and possibly close abfd.  */
1066 void
1067 gdb_bfd_unref (struct bfd *abfd)
1068 {
1069   int *p_refcount;
1070   char *name;
1071
1072   if (abfd == NULL)
1073     return;
1074
1075   p_refcount = bfd_usrdata (abfd);
1076
1077   /* Valid range for p_refcount: NULL (single owner), or a pointer
1078      to int counter, which has a value of 1 (single owner) or 2 (shared).  */
1079   gdb_assert (p_refcount == NULL || *p_refcount == 1 || *p_refcount == 2);
1080
1081   if (p_refcount != NULL)
1082     {
1083       *p_refcount -= 1;
1084       if (*p_refcount > 0)
1085         return;
1086     }
1087   xfree (p_refcount);
1088   bfd_usrdata (abfd) = NULL;  /* Paranoia.  */
1089
1090   name = bfd_get_filename (abfd);
1091   if (!bfd_close (abfd))
1092     warning (_("cannot close \"%s\": %s"),
1093              name, bfd_errmsg (bfd_get_error ()));
1094   xfree (name);
1095 }