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