a1fb22f742c7716310bae78b1129618cd9a37d2f
[platform/upstream/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 /* Many places in gdb want to test just to see if we have any partial
167    symbols available.  This function returns zero if none are currently
168    available, nonzero otherwise. */
169
170 int
171 have_partial_symbols ()
172 {
173   struct objfile *ofp;
174   int havethem = 0;
175
176   for (ofp = object_files; ofp; ofp = ofp -> next)
177     {
178       if (ofp -> psymtabs != NULL)
179         {
180           havethem++;
181           break;
182         }
183     }
184   return (havethem);
185 }
186
187 /* Many places in gdb want to test just to see if we have any full
188    symbols available.  This function returns zero if none are currently
189    available, nonzero otherwise. */
190
191 int
192 have_full_symbols ()
193 {
194   struct objfile *ofp;
195   int havethem = 0;
196
197   for (ofp = object_files; ofp; ofp = ofp -> next)
198     {
199       if (ofp -> symtabs != NULL)
200         {
201           havethem++;
202           break;
203         }
204     }
205   return (havethem);
206 }
207
208 /* Many places in gdb want to test just to see if we have any minimal
209    symbols available.  This function returns zero if none are currently
210    available, nonzero otherwise. */
211
212 int
213 have_minimal_symbols ()
214 {
215   struct objfile *ofp;
216   int havethem = 0;
217
218   for (ofp = object_files; ofp; ofp = ofp -> next)
219     {
220       if (ofp -> msymbols != NULL)
221         {
222           havethem++;
223           break;
224         }
225     }
226   return (havethem);
227 }
228
229 /* Call the function specified by FUNC for each currently available objfile,
230    for as long as this function continues to return NULL.  If the function
231    ever returns non-NULL, then the iteration over the objfiles is terminated,
232    and the result is returned to the caller.  The function called has full
233    control over the form and content of the information returned via the
234    non-NULL result, which may be as simple as a pointer to the objfile that
235    the iteration terminated on, or as complex as a pointer to a private
236    structure containing multiple results. */
237
238 PTR
239 iterate_over_objfiles (func, arg1, arg2, arg3)
240      PTR (*func) PARAMS ((struct objfile *, PTR, PTR, PTR));
241      PTR arg1;
242      PTR arg2;
243      PTR arg3;
244 {
245   register struct objfile *objfile;
246   PTR result = NULL;
247
248   for (objfile = object_files;
249        objfile != NULL && result == NULL;
250        objfile = objfile -> next)
251     {
252       result = (*func)(objfile, arg1, arg2, arg3);
253     }
254   return (result);
255 }
256
257 /* Call the function specified by FUNC for each currently available symbol
258    table, for as long as this function continues to return NULL.  If the
259    function ever returns non-NULL, then the iteration over the symbol tables
260    is terminated, and the result is returned to the caller.  The function
261    called has full control over the form and content of the information
262    returned via the non-NULL result, which may be as simple as a pointer
263    to the symtab that the iteration terminated on, or as complex as a
264    pointer to a private structure containing multiple results. */
265
266 PTR 
267 iterate_over_symtabs (func, arg1, arg2, arg3)
268      PTR (*func) PARAMS ((struct objfile *, struct symtab *, PTR, PTR, PTR));
269      PTR arg1;
270      PTR arg2;
271      PTR arg3;
272 {
273   register struct objfile *objfile;
274   register struct symtab *symtab;
275   PTR result = NULL;
276
277   for (objfile = object_files;
278        objfile != NULL && result == NULL;
279        objfile = objfile -> next)
280     {
281       for (symtab = objfile -> symtabs;
282            symtab != NULL && result == NULL;
283            symtab = symtab -> next)
284         {
285           result = (*func)(objfile, symtab, arg1, arg2, arg3);
286         }
287     }
288   return (result);
289 }
290
291 /* Call the function specified by FUNC for each currently available partial
292    symbol table, for as long as this function continues to return NULL.  If
293    the function ever returns non-NULL, then the iteration over the partial
294    symbol tables is terminated, and the result is returned to the caller.
295
296    The function called has full control over the form and content of the
297    information returned via the non-NULL result, which may be as simple as a
298    pointer to the partial symbol table that the iteration terminated on, or
299    as complex as a pointer to a private structure containing multiple
300    results. */
301
302 PTR 
303 iterate_over_psymtabs (func, arg1, arg2, arg3)
304      PTR (*func) PARAMS ((struct objfile *, struct partial_symtab *,
305                           PTR, PTR, PTR));
306      PTR arg1;
307      PTR arg2;
308      PTR arg3;
309 {
310   register struct objfile *objfile;
311   register struct partial_symtab *psymtab;
312   PTR result = NULL;
313
314   for (objfile = object_files;
315        objfile != NULL && result == NULL;
316        objfile = objfile -> next)
317     {
318       for (psymtab = objfile -> psymtabs;
319            psymtab != NULL && result == NULL;
320            psymtab = psymtab -> next)
321         {
322           result = (*func)(objfile, psymtab, arg1, arg2, arg3);
323         }
324     }
325   return (result);
326 }