* breakpoint.h (ALL_BREAKPOINTS_SAFE): Add.
[external/binutils.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 <stdio.h>
25 #include "defs.h"
26 #include "bfd.h"                /* Binary File Description */
27 #include "symtab.h"
28 #include "symfile.h"
29
30 #include <obstack.h>
31
32 /* Externally visible variables that are owned by this module. */
33
34 struct objfile *object_files;           /* Linked list of all objfiles */
35
36 /* Allocate a new objfile struct, fill it in as best we can, and return it.
37    It is also linked into the list of all known object files. */
38
39 struct objfile *
40 allocate_objfile (abfd, filename, dumpable)
41      bfd *abfd;
42      char *filename;
43     int dumpable;
44 {
45   struct objfile *objfile;
46
47   /* First, if the objfile is to be dumpable, we must malloc the structure
48      itself using the mmap version, and arrange that all memory allocation
49      for the objfile uses the mmap routines.  Otherwise, just use the
50      old sbrk'd malloc routines. */
51
52   if (dumpable)
53     {
54       objfile = (struct objfile *) mmap_xmalloc (sizeof (struct objfile));
55       (void) memset (objfile, 0, sizeof (struct objfile));
56       objfile -> malloc = mmap_malloc;
57       objfile -> realloc = mmap_realloc;
58       objfile -> xmalloc = mmap_xmalloc;
59       objfile -> xrealloc = mmap_xrealloc;
60       objfile -> free = mmap_free;
61       objfile -> flags |= OBJF_DUMPABLE;
62     }
63   else
64     {
65       objfile = (struct objfile *) xmalloc (sizeof (struct objfile));
66       (void) memset (objfile, 0, sizeof (struct objfile));
67       objfile -> malloc = malloc;
68       objfile -> realloc = realloc;
69       objfile -> xmalloc = xmalloc;
70       objfile -> xrealloc = xrealloc;
71       objfile -> free = free;
72     }
73
74   /* Now, malloc a fresh copy of the filename string using the malloc
75      specified as appropriate for the objfile. */
76
77   objfile -> name = (*objfile -> xmalloc) (strlen (filename) + 1);
78   strcpy (objfile -> name, filename);
79
80   objfile -> obfd = abfd;
81
82   objfile -> mtime = bfd_get_mtime (abfd);
83
84   /* Set up the various obstacks to use the memory allocation/free
85      functions that are appropriate for this objfile. */
86
87   obstack_full_begin (&objfile -> psymbol_obstack, 0, 0,
88                       objfile -> xmalloc, objfile -> free);
89   obstack_full_begin (&objfile -> symbol_obstack, 0, 0,
90                       objfile -> xmalloc, objfile -> free);
91   obstack_full_begin (&objfile -> type_obstack, 0, 0,
92                       objfile -> xmalloc, objfile -> free);
93
94   /* Push this file onto the head of the linked list of other such files. */
95
96   objfile -> next = object_files;
97   object_files = objfile;
98
99   return (objfile);
100 }
101
102
103 /* Destroy an objfile and all the symtabs and psymtabs under it.  Note
104    that as much as possible is allocated on the symbol_obstack and
105    psymbol_obstack, so that the memory can be efficiently freed. */
106
107 void
108 free_objfile (objfile)
109      struct objfile *objfile;
110 {
111   struct objfile *ofp;
112
113   if (objfile -> name)
114     {
115       (*objfile -> free) (objfile -> name);
116     }
117   if (objfile -> obfd)
118     {
119       bfd_close (objfile -> obfd);
120     }
121
122   /* Remove it from the chain of all objfiles.  */
123
124   if (object_files == objfile)
125     {
126       object_files = objfile -> next;
127     }
128   else
129     {
130       for (ofp = object_files; ofp; ofp = ofp -> next)
131         {
132           if (ofp -> next == objfile)
133             {
134               ofp -> next = objfile -> next;
135             }
136         }
137     }
138
139   obstack_free (&objfile -> psymbol_obstack, 0);
140   obstack_free (&objfile -> symbol_obstack, 0);
141   obstack_free (&objfile -> type_obstack, 0);
142
143 #if 0   /* FIXME!! */
144
145   /* Before the symbol table code was redone to make it easier to
146      selectively load and remove information particular to a specific
147      linkage unit, gdb used to do these things whenever the monolithic
148      symbol table was blown away.  How much still needs to be done
149      is unknown, but we play it safe for now and keep each action until
150      it is shown to be no longer needed. */
151      
152   clear_symtab_users_once ();
153 #if defined (CLEAR_SOLIB)
154   CLEAR_SOLIB ();
155 #endif
156   clear_pc_function_cache ();
157
158 #endif
159
160   /* The last thing we do is free the objfile struct itself, using the
161      free() that is appropriate for the objfile. */
162
163   (*objfile -> free) (objfile);
164 }
165
166
167 /* Free all the object files at once.  */
168
169 void
170 free_all_objfiles ()
171 {
172   struct objfile *objfile, *temp;
173
174   ALL_OBJFILES_SAFE (objfile, temp)
175     {
176       free_objfile (objfile);
177     }
178 }
179
180 /* Many places in gdb want to test just to see if we have any partial
181    symbols available.  This function returns zero if none are currently
182    available, nonzero otherwise. */
183
184 int
185 have_partial_symbols ()
186 {
187   struct objfile *ofp;
188   int havethem = 0;
189
190   for (ofp = object_files; ofp; ofp = ofp -> next)
191     {
192       if (ofp -> psymtabs != NULL)
193         {
194           havethem++;
195           break;
196         }
197     }
198   return (havethem);
199 }
200
201 /* Many places in gdb want to test just to see if we have any full
202    symbols available.  This function returns zero if none are currently
203    available, nonzero otherwise. */
204
205 int
206 have_full_symbols ()
207 {
208   struct objfile *ofp;
209   int havethem = 0;
210
211   for (ofp = object_files; ofp; ofp = ofp -> next)
212     {
213       if (ofp -> symtabs != NULL)
214         {
215           havethem++;
216           break;
217         }
218     }
219   return (havethem);
220 }
221
222 /* Many places in gdb want to test just to see if we have any minimal
223    symbols available.  This function returns zero if none are currently
224    available, nonzero otherwise. */
225
226 int
227 have_minimal_symbols ()
228 {
229   struct objfile *ofp;
230   int havethem = 0;
231
232   for (ofp = object_files; ofp; ofp = ofp -> next)
233     {
234       if (ofp -> msymbols != NULL)
235         {
236           havethem++;
237           break;
238         }
239     }
240   return (havethem);
241 }
242
243 /* Call the function specified by FUNC for each currently available objfile,
244    for as long as this function continues to return NULL.  If the function
245    ever returns non-NULL, then the iteration over the objfiles is terminated,
246    and the result is returned to the caller.  The function called has full
247    control over the form and content of the information returned via the
248    non-NULL result, which may be as simple as a pointer to the objfile that
249    the iteration terminated on, or as complex as a pointer to a private
250    structure containing multiple results. */
251
252 PTR
253 iterate_over_objfiles (func, arg1, arg2, arg3)
254      PTR (*func) PARAMS ((struct objfile *, PTR, PTR, PTR));
255      PTR arg1;
256      PTR arg2;
257      PTR arg3;
258 {
259   register struct objfile *objfile;
260   PTR result = NULL;
261
262   for (objfile = object_files;
263        objfile != NULL && result == NULL;
264        objfile = objfile -> next)
265     {
266       result = (*func)(objfile, arg1, arg2, arg3);
267     }
268   return (result);
269 }
270
271 /* Call the function specified by FUNC for each currently available symbol
272    table, for as long as this function continues to return NULL.  If the
273    function ever returns non-NULL, then the iteration over the symbol tables
274    is terminated, and the result is returned to the caller.  The function
275    called has full control over the form and content of the information
276    returned via the non-NULL result, which may be as simple as a pointer
277    to the symtab that the iteration terminated on, or as complex as a
278    pointer to a private structure containing multiple results. */
279
280 PTR 
281 iterate_over_symtabs (func, arg1, arg2, arg3)
282      PTR (*func) PARAMS ((struct objfile *, struct symtab *, PTR, PTR, PTR));
283      PTR arg1;
284      PTR arg2;
285      PTR arg3;
286 {
287   register struct objfile *objfile;
288   register struct symtab *symtab;
289   PTR result = NULL;
290
291   for (objfile = object_files;
292        objfile != NULL && result == NULL;
293        objfile = objfile -> next)
294     {
295       for (symtab = objfile -> symtabs;
296            symtab != NULL && result == NULL;
297            symtab = symtab -> next)
298         {
299           result = (*func)(objfile, symtab, arg1, arg2, arg3);
300         }
301     }
302   return (result);
303 }
304
305 /* Call the function specified by FUNC for each currently available partial
306    symbol table, for as long as this function continues to return NULL.  If
307    the function ever returns non-NULL, then the iteration over the partial
308    symbol tables is terminated, and the result is returned to the caller.
309
310    The function called has full control over the form and content of the
311    information returned via the non-NULL result, which may be as simple as a
312    pointer to the partial symbol table that the iteration terminated on, or
313    as complex as a pointer to a private structure containing multiple
314    results. */
315
316 PTR 
317 iterate_over_psymtabs (func, arg1, arg2, arg3)
318      PTR (*func) PARAMS ((struct objfile *, struct partial_symtab *,
319                           PTR, PTR, PTR));
320      PTR arg1;
321      PTR arg2;
322      PTR arg3;
323 {
324   register struct objfile *objfile;
325   register struct partial_symtab *psymtab;
326   PTR result = NULL;
327
328   for (objfile = object_files;
329        objfile != NULL && result == NULL;
330        objfile = objfile -> next)
331     {
332       for (psymtab = objfile -> psymtabs;
333            psymtab != NULL && result == NULL;
334            psymtab = psymtab -> next)
335         {
336           result = (*func)(objfile, psymtab, arg1, arg2, arg3);
337         }
338     }
339   return (result);
340 }