Add tekhex
[deliverable/binutils-gdb.git] / gdb / objfiles.c
CommitLineData
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
5This file is part of GDB.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, 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
37static int
b0246b3b 38open_mapped_file PARAMS ((char *filename, long mtime, int mapped));
318bf84f
FF
39
40static CORE_ADDR
41map_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
46struct objfile *object_files; /* Linked list of all objfiles */
5e2e79f8
FF
47struct objfile *current_objfile; /* For symbol file being read in */
48struct objfile *symfile_objfile; /* Main symbol table loaded from */
49
318bf84f 50int 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
57struct objfile *
b0246b3b 58allocate_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 {
2d6d969c 87 (void) close (fd);
318bf84f
FF
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 93 objfile -> md = md;
2d6d969c 94 objfile -> mmfd = fd;
318bf84f
FF
95 /* Update pointers to functions to *our* copies */
96 obstack_chunkfun (&objfile -> psymbol_obstack, xmmalloc);
97 obstack_freefun (&objfile -> psymbol_obstack, mfree);
98 obstack_chunkfun (&objfile -> symbol_obstack, xmmalloc);
99 obstack_freefun (&objfile -> symbol_obstack, mfree);
100 obstack_chunkfun (&objfile -> type_obstack, xmmalloc);
101 obstack_freefun (&objfile -> type_obstack, mfree);
318bf84f
FF
102 }
103 else
104 {
3624c875
FF
105 /* Set up to detect internal memory corruption. MUST be done before
106 the first malloc. See comments in init_malloc() and mmcheck(). */
107 init_malloc (md);
318bf84f
FF
108 objfile = (struct objfile *) xmmalloc (md, sizeof (struct objfile));
109 (void) memset (objfile, 0, sizeof (struct objfile));
110 objfile -> md = md;
2d6d969c 111 objfile -> mmfd = fd;
318bf84f
FF
112 objfile -> flags |= OBJF_MAPPED;
113 mmalloc_setkey (objfile -> md, 0, objfile);
114 obstack_full_begin (&objfile -> psymbol_obstack, 0, 0,
115 xmmalloc, mfree, objfile -> md,
116 OBSTACK_MMALLOC_LIKE);
117 obstack_full_begin (&objfile -> symbol_obstack, 0, 0,
118 xmmalloc, mfree, objfile -> md,
119 OBSTACK_MMALLOC_LIKE);
120 obstack_full_begin (&objfile -> type_obstack, 0, 0,
121 xmmalloc, mfree, objfile -> md,
122 OBSTACK_MMALLOC_LIKE);
318bf84f
FF
123 }
124 }
125
126 if (mapped && (objfile == NULL))
127 {
b0246b3b
FF
128 warning ("symbol table for '%s' will not be mapped",
129 bfd_get_filename (abfd));
318bf84f 130 }
1ab3bf1b 131
318bf84f 132#else /* defined(NO_MMALLOC) || !defined(HAVE_MMAP) */
1ab3bf1b 133
318bf84f 134 if (mapped)
1ab3bf1b 135 {
318bf84f
FF
136 warning ("this version of gdb does not support mapped symbol tables.");
137
138 /* Turn off the global flag so we don't try to do mapped symbol tables
139 any more, which shuts up gdb unless the user specifically gives the
140 "mapped" keyword again. */
141
142 mapped_symbol_files = 0;
1ab3bf1b 143 }
318bf84f
FF
144
145#endif /* !defined(NO_MMALLOC) && defined(HAVE_MMAP) */
146
147 /* If we don't support mapped symbol files, didn't ask for the file to be
148 mapped, or failed to open the mapped file for some reason, then revert
149 back to an unmapped objfile. */
150
151 if (objfile == NULL)
1ab3bf1b
JG
152 {
153 objfile = (struct objfile *) xmalloc (sizeof (struct objfile));
154 (void) memset (objfile, 0, sizeof (struct objfile));
318bf84f
FF
155 objfile -> md = NULL;
156 obstack_full_begin (&objfile -> psymbol_obstack, 0, 0, xmalloc, free,
157 (void *) 0, 0);
158 obstack_full_begin (&objfile -> symbol_obstack, 0, 0, xmalloc, free,
159 (void *) 0, 0);
160 obstack_full_begin (&objfile -> type_obstack, 0, 0, xmalloc, free,
161 (void *) 0, 0);
162
1ab3bf1b
JG
163 }
164
b0246b3b
FF
165 /* Update the per-objfile information that comes from the bfd, ensuring
166 that any data that is reference is saved in the per-objfile data
167 region. */
1ab3bf1b
JG
168
169 objfile -> obfd = abfd;
2d6d969c
FF
170 if (objfile -> name != NULL)
171 {
172 mfree (objfile -> md, objfile -> name);
173 }
b0246b3b 174 objfile -> name = mstrsave (objfile -> md, bfd_get_filename (abfd));
1ab3bf1b
JG
175 objfile -> mtime = bfd_get_mtime (abfd);
176
1ab3bf1b
JG
177 /* Push this file onto the head of the linked list of other such files. */
178
179 objfile -> next = object_files;
180 object_files = objfile;
181
182 return (objfile);
183}
184
185
186/* Destroy an objfile and all the symtabs and psymtabs under it. Note
187 that as much as possible is allocated on the symbol_obstack and
80d68b1d
FF
188 psymbol_obstack, so that the memory can be efficiently freed.
189
190 Things which we do NOT free because they are not in malloc'd memory
191 or not in memory specific to the objfile include:
192
193 objfile -> sf
194
2d6d969c
FF
195 FIXME: If the objfile is using reusable symbol information (via mmalloc),
196 then we need to take into account the fact that more than one process
197 may be using the symbol information at the same time (when mmalloc is
198 extended to support cooperative locking). When more than one process
199 is using the mapped symbol info, we need to be more careful about when
200 we free objects in the reusable area. */
1ab3bf1b
JG
201
202void
203free_objfile (objfile)
204 struct objfile *objfile;
205{
206 struct objfile *ofp;
2d6d969c
FF
207 int mmfd;
208
209 /* First do any symbol file specific actions required when we are
210 finished with a particular symbol file. Note that if the objfile
211 is using reusable symbol information (via mmalloc) then each of
212 these routines is responsible for doing the correct thing, either
213 freeing things which are valid only during this particular gdb
214 execution, or leaving them to be reused during the next one. */
1ab3bf1b 215
80d68b1d
FF
216 if (objfile -> sf != NULL)
217 {
218 (*objfile -> sf -> sym_finish) (objfile);
219 }
2d6d969c
FF
220
221 /* We always close the bfd. */
222
80d68b1d 223 if (objfile -> obfd != NULL)
1ab3bf1b
JG
224 {
225 bfd_close (objfile -> obfd);
226 }
227
2d6d969c 228 /* Remove it from the chain of all objfiles. */
1ab3bf1b
JG
229
230 if (object_files == objfile)
231 {
232 object_files = objfile -> next;
233 }
234 else
235 {
236 for (ofp = object_files; ofp; ofp = ofp -> next)
237 {
238 if (ofp -> next == objfile)
239 {
240 ofp -> next = objfile -> next;
2d6d969c 241 break;
1ab3bf1b
JG
242 }
243 }
244 }
2d6d969c 245 objfile -> next = NULL;
1ab3bf1b
JG
246
247#if 0 /* FIXME!! */
248
249 /* Before the symbol table code was redone to make it easier to
250 selectively load and remove information particular to a specific
251 linkage unit, gdb used to do these things whenever the monolithic
252 symbol table was blown away. How much still needs to be done
253 is unknown, but we play it safe for now and keep each action until
254 it is shown to be no longer needed. */
255
256 clear_symtab_users_once ();
257#if defined (CLEAR_SOLIB)
258 CLEAR_SOLIB ();
259#endif
260 clear_pc_function_cache ();
261
262#endif
263
2d6d969c
FF
264 /* The last thing we do is free the objfile struct itself for the
265 non-reusable case, or detach from the mapped file for the reusable
266 case. Note that the mmalloc_detach or the mfree is the last thing
267 we can do with this objfile. */
1ab3bf1b 268
2d6d969c
FF
269 if (objfile -> flags & OBJF_MAPPED)
270 {
271 /* Remember the fd so we can close it. We can't close it before
272 doing the detach, and after the detach the objfile is gone. */
273 mmfd = objfile -> mmfd;
274 mmalloc_detach (objfile -> md);
275 (void) close (mmfd);
276 }
277 else
278 {
279 if (objfile -> name != NULL)
280 {
281 mfree (objfile -> md, objfile -> name);
282 }
283 /* Free the obstacks for non-reusable objfiles */
284 obstack_free (&objfile -> psymbol_obstack, 0);
285 obstack_free (&objfile -> symbol_obstack, 0);
286 obstack_free (&objfile -> type_obstack, 0);
287 mfree (objfile -> md, objfile);
288 }
1ab3bf1b
JG
289}
290
cba0d141
JG
291
292/* Free all the object files at once. */
293
294void
295free_all_objfiles ()
296{
297 struct objfile *objfile, *temp;
298
299 ALL_OBJFILES_SAFE (objfile, temp)
300 {
301 free_objfile (objfile);
302 }
303}
304
1ab3bf1b
JG
305/* Many places in gdb want to test just to see if we have any partial
306 symbols available. This function returns zero if none are currently
307 available, nonzero otherwise. */
308
309int
310have_partial_symbols ()
311{
312 struct objfile *ofp;
1ab3bf1b 313
84ffdec2 314 ALL_OBJFILES (ofp)
1ab3bf1b
JG
315 {
316 if (ofp -> psymtabs != NULL)
317 {
84ffdec2 318 return 1;
1ab3bf1b
JG
319 }
320 }
84ffdec2 321 return 0;
1ab3bf1b
JG
322}
323
324/* Many places in gdb want to test just to see if we have any full
325 symbols available. This function returns zero if none are currently
326 available, nonzero otherwise. */
327
328int
329have_full_symbols ()
330{
331 struct objfile *ofp;
1ab3bf1b 332
84ffdec2 333 ALL_OBJFILES (ofp)
1ab3bf1b
JG
334 {
335 if (ofp -> symtabs != NULL)
336 {
84ffdec2 337 return 1;
1ab3bf1b
JG
338 }
339 }
84ffdec2 340 return 0;
1ab3bf1b
JG
341}
342
343/* Many places in gdb want to test just to see if we have any minimal
344 symbols available. This function returns zero if none are currently
345 available, nonzero otherwise. */
346
347int
348have_minimal_symbols ()
349{
350 struct objfile *ofp;
1ab3bf1b 351
84ffdec2 352 ALL_OBJFILES (ofp)
1ab3bf1b
JG
353 {
354 if (ofp -> msymbols != NULL)
355 {
84ffdec2 356 return 1;
1ab3bf1b
JG
357 }
358 }
84ffdec2 359 return 0;
1ab3bf1b
JG
360}
361
b0246b3b 362/* Look for a mapped symbol file that corresponds to FILENAME and is more
318bf84f 363 recent than MTIME. If MAPPED is nonzero, the user has asked that gdb
b0246b3b
FF
364 use a mapped symbol file for this file, so create a new one if one does
365 not currently exist.
318bf84f
FF
366
367 If found, then return an open file descriptor for the file, otherwise
368 return -1.
369
370 This routine is responsible for implementing the policy that generates
371 the name of the mapped symbol file from the name of a file containing
372 symbols that gdb would like to read. */
373
374static int
b0246b3b
FF
375open_mapped_file (filename, mtime, mapped)
376 char *filename;
318bf84f
FF
377 long mtime;
378 int mapped;
379{
380 int fd;
381 char *symfilename;
382 struct stat sbuf;
383
384 /* For now, all we do is look in the local directory for a file with
385 the name of the base file and an extension of ".syms" */
386
b0246b3b 387 symfilename = concat ("./", basename (filename), ".syms", (char *) NULL);
318bf84f
FF
388
389 /* Check to see if the desired file already exists and is more recent than
390 the corresponding base file (specified by the passed MTIME parameter).
391 The open will fail if the file does not already exist. */
392
393 if ((fd = open (symfilename, O_RDWR)) >= 0)
394 {
395 if (fstat (fd, &sbuf) != 0)
396 {
2d6d969c 397 (void) close (fd);
318bf84f
FF
398 perror_with_name (symfilename);
399 }
400 else if (sbuf.st_mtime > mtime)
401 {
402 return (fd);
403 }
404 else
405 {
2d6d969c 406 (void) close (fd);
318bf84f
FF
407 fd = -1;
408 }
409 }
410
411 /* Either the file does not already exist, or the base file has changed
412 since it was created. In either case, if the user has specified use of
413 a mapped file, then create a new mapped file, truncating any existing
414 one.
415
416 In the case where there is an existing file, but it is out of date, and
417 the user did not specify mapped, the existing file is just silently
418 ignored. Perhaps we should warn about this case (FIXME?).
419
420 By default the file is rw for everyone, with the user's umask taking
421 care of turning off the permissions the user wants off. */
422
423 if (mapped)
424 {
425 fd = open (symfilename, O_RDWR | O_CREAT | O_TRUNC, 0666);
426 }
427
428 return (fd);
429}
430
431/* Return the base address at which we would like the next objfile's
432 mapped data to start.
433
434 For now, we use the kludge that the configuration specifies a base
435 address to which it is safe to map the first mmalloc heap, and an
436 increment to add to this address for each successive heap. There are
437 a lot of issues to deal with here to make this work reasonably, including:
438
439 Avoid memory collisions with existing mapped address spaces
440
441 Reclaim address spaces when their mmalloc heaps are unmapped
442
443 When mmalloc heaps are shared between processes they have to be
444 mapped at the same addresses in each
445
446 Once created, a mmalloc heap that is to be mapped back in must be
447 mapped at the original address. I.E. each objfile will expect to
448 be remapped at it's original address. This becomes a problem if
449 the desired address is already in use.
450
451 etc, etc, etc.
452
453 */
454
455
456static CORE_ADDR
457map_to_address ()
458{
459
460#if defined(MMAP_BASE_ADDRESS) && defined (MMAP_INCREMENT)
461
462 static CORE_ADDR next = MMAP_BASE_ADDRESS;
463 CORE_ADDR mapto = next;
464
465 next += MMAP_INCREMENT;
466 return (mapto);
467
468#else
469
470 return (0);
471
472#endif
473
474}
This page took 0.05995 seconds and 4 git commands to generate.