Commit | Line | Data |
---|---|---|
bd5635a1 | 1 | /* Generic symbol file reading for the GNU debugger, GDB. |
2093fe68 | 2 | Copyright 1990, 1991, 1992, 1993 Free Software Foundation, Inc. |
bd5635a1 RP |
3 | Contributed by Cygnus Support, using pieces from other GDB modules. |
4 | ||
5 | This file is part of GDB. | |
6 | ||
61a7292f | 7 | This program is free software; you can redistribute it and/or modify |
bd5635a1 | 8 | it under the terms of the GNU General Public License as published by |
61a7292f SG |
9 | the Free Software Foundation; either version 2 of the License, or |
10 | (at your option) any later version. | |
bd5635a1 | 11 | |
61a7292f | 12 | This program is distributed in the hope that it will be useful, |
bd5635a1 RP |
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 | |
61a7292f SG |
18 | along with this program; if not, write to the Free Software |
19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
bd5635a1 | 20 | |
bd5635a1 RP |
21 | #include "defs.h" |
22 | #include "symtab.h" | |
30875e1c | 23 | #include "gdbtypes.h" |
bd5635a1 RP |
24 | #include "gdbcore.h" |
25 | #include "frame.h" | |
26 | #include "target.h" | |
27 | #include "value.h" | |
28 | #include "symfile.h" | |
bf349b77 | 29 | #include "objfiles.h" |
bd5635a1 RP |
30 | #include "gdbcmd.h" |
31 | #include "breakpoint.h" | |
e58de8a2 | 32 | #include "language.h" |
51b80b00 | 33 | #include "complaints.h" |
2e4964ad | 34 | #include "demangle.h" |
bd5635a1 RP |
35 | |
36 | #include <obstack.h> | |
37 | #include <assert.h> | |
38 | ||
39 | #include <sys/types.h> | |
40 | #include <fcntl.h> | |
41 | #include <string.h> | |
42 | #include <sys/stat.h> | |
9342ecb9 | 43 | #include <ctype.h> |
bd5635a1 | 44 | |
2093fe68 RP |
45 | #ifndef O_BINARY |
46 | #define O_BINARY 0 | |
47 | #endif | |
48 | ||
30875e1c SG |
49 | /* Global variables owned by this file */ |
50 | ||
80d68b1d | 51 | int readnow_symbol_files; /* Read full symbols immediately */ |
d47d5315 | 52 | |
51b80b00 FF |
53 | struct complaint oldsyms_complaint = { |
54 | "Replacing old symbols for `%s'", 0, 0 | |
55 | }; | |
56 | ||
57 | struct complaint empty_symtab_complaint = { | |
58 | "Empty symbol table found for `%s'", 0, 0 | |
59 | }; | |
60 | ||
30875e1c | 61 | /* External variables and functions referenced. */ |
bd5635a1 | 62 | |
30875e1c | 63 | extern int info_verbose; |
bd5635a1 RP |
64 | |
65 | /* Functions this file defines */ | |
7d9884b9 | 66 | |
e58de8a2 FF |
67 | static void |
68 | set_initial_language PARAMS ((void)); | |
69 | ||
30875e1c SG |
70 | static void |
71 | load_command PARAMS ((char *, int)); | |
72 | ||
73 | static void | |
74 | add_symbol_file_command PARAMS ((char *, int)); | |
75 | ||
30875e1c SG |
76 | static void |
77 | cashier_psymtab PARAMS ((struct partial_symtab *)); | |
bd5635a1 | 78 | |
30875e1c SG |
79 | static int |
80 | compare_psymbols PARAMS ((const void *, const void *)); | |
bd5635a1 | 81 | |
30875e1c SG |
82 | static int |
83 | compare_symbols PARAMS ((const void *, const void *)); | |
84 | ||
b0246b3b FF |
85 | static bfd * |
86 | symfile_bfd_open PARAMS ((char *)); | |
30875e1c | 87 | |
80d68b1d FF |
88 | static void |
89 | find_sym_fns PARAMS ((struct objfile *)); | |
30875e1c | 90 | |
80d68b1d FF |
91 | /* List of all available sym_fns. On gdb startup, each object file reader |
92 | calls add_symtab_fns() to register information on each format it is | |
93 | prepared to read. */ | |
bd5635a1 | 94 | |
80d68b1d | 95 | static struct sym_fns *symtab_fns = NULL; |
bd5635a1 | 96 | |
bd5635a1 RP |
97 | /* Structures with which to manage partial symbol allocation. */ |
98 | ||
99 | struct psymbol_allocation_list global_psymbols = {0}, static_psymbols = {0}; | |
100 | ||
61a7292f SG |
101 | /* Flag for whether user will be reloading symbols multiple times. |
102 | Defaults to ON for VxWorks, otherwise OFF. */ | |
103 | ||
104 | #ifdef SYMBOL_RELOADING_DEFAULT | |
105 | int symbol_reloading = SYMBOL_RELOADING_DEFAULT; | |
106 | #else | |
107 | int symbol_reloading = 0; | |
108 | #endif | |
109 | ||
bd5635a1 | 110 | \f |
ade40d31 | 111 | /* Since this function is called from within qsort, in an ANSI environment |
30875e1c SG |
112 | it must conform to the prototype for qsort, which specifies that the |
113 | comparison function takes two "void *" pointers. */ | |
bd5635a1 RP |
114 | |
115 | static int | |
30875e1c SG |
116 | compare_symbols (s1p, s2p) |
117 | const PTR s1p; | |
118 | const PTR s2p; | |
bd5635a1 | 119 | { |
30875e1c | 120 | register struct symbol **s1, **s2; |
bd5635a1 | 121 | |
30875e1c SG |
122 | s1 = (struct symbol **) s1p; |
123 | s2 = (struct symbol **) s2p; | |
124 | ||
ade40d31 | 125 | return (STRCMP (SYMBOL_NAME (*s1), SYMBOL_NAME (*s2))); |
bd5635a1 RP |
126 | } |
127 | ||
30875e1c SG |
128 | /* |
129 | ||
130 | LOCAL FUNCTION | |
131 | ||
132 | compare_psymbols -- compare two partial symbols by name | |
133 | ||
134 | DESCRIPTION | |
135 | ||
136 | Given pointer to two partial symbol table entries, compare | |
137 | them by name and return -N, 0, or +N (ala strcmp). Typically | |
138 | used by sorting routines like qsort(). | |
139 | ||
140 | NOTES | |
141 | ||
142 | Does direct compare of first two characters before punting | |
143 | and passing to strcmp for longer compares. Note that the | |
144 | original version had a bug whereby two null strings or two | |
145 | identically named one character strings would return the | |
146 | comparison of memory following the null byte. | |
147 | ||
148 | */ | |
149 | ||
150 | static int | |
151 | compare_psymbols (s1p, s2p) | |
152 | const PTR s1p; | |
153 | const PTR s2p; | |
154 | { | |
155 | register char *st1 = SYMBOL_NAME ((struct partial_symbol *) s1p); | |
156 | register char *st2 = SYMBOL_NAME ((struct partial_symbol *) s2p); | |
157 | ||
158 | if ((st1[0] - st2[0]) || !st1[0]) | |
159 | { | |
160 | return (st1[0] - st2[0]); | |
161 | } | |
162 | else if ((st1[1] - st2[1]) || !st1[1]) | |
163 | { | |
164 | return (st1[1] - st2[1]); | |
165 | } | |
166 | else | |
167 | { | |
2e4964ad | 168 | return (STRCMP (st1 + 2, st2 + 2)); |
30875e1c SG |
169 | } |
170 | } | |
171 | ||
172 | void | |
173 | sort_pst_symbols (pst) | |
174 | struct partial_symtab *pst; | |
175 | { | |
176 | /* Sort the global list; don't sort the static list */ | |
177 | ||
178 | qsort (pst -> objfile -> global_psymbols.list + pst -> globals_offset, | |
179 | pst -> n_global_syms, sizeof (struct partial_symbol), | |
180 | compare_psymbols); | |
181 | } | |
182 | ||
bd5635a1 RP |
183 | /* Call sort_block_syms to sort alphabetically the symbols of one block. */ |
184 | ||
185 | void | |
186 | sort_block_syms (b) | |
187 | register struct block *b; | |
188 | { | |
189 | qsort (&BLOCK_SYM (b, 0), BLOCK_NSYMS (b), | |
190 | sizeof (struct symbol *), compare_symbols); | |
191 | } | |
192 | ||
193 | /* Call sort_symtab_syms to sort alphabetically | |
194 | the symbols of each block of one symtab. */ | |
195 | ||
196 | void | |
197 | sort_symtab_syms (s) | |
198 | register struct symtab *s; | |
199 | { | |
c9bd6710 JG |
200 | register struct blockvector *bv; |
201 | int nbl; | |
bd5635a1 RP |
202 | int i; |
203 | register struct block *b; | |
204 | ||
c9bd6710 JG |
205 | if (s == 0) |
206 | return; | |
207 | bv = BLOCKVECTOR (s); | |
208 | nbl = BLOCKVECTOR_NBLOCKS (bv); | |
bd5635a1 RP |
209 | for (i = 0; i < nbl; i++) |
210 | { | |
211 | b = BLOCKVECTOR_BLOCK (bv, i); | |
212 | if (BLOCK_SHOULD_SORT (b)) | |
213 | sort_block_syms (b); | |
214 | } | |
215 | } | |
216 | ||
217 | void | |
218 | sort_all_symtab_syms () | |
219 | { | |
220 | register struct symtab *s; | |
30875e1c | 221 | register struct objfile *objfile; |
bd5635a1 | 222 | |
30875e1c | 223 | for (objfile = object_files; objfile != NULL; objfile = objfile -> next) |
bd5635a1 | 224 | { |
30875e1c SG |
225 | for (s = objfile -> symtabs; s != NULL; s = s -> next) |
226 | { | |
227 | sort_symtab_syms (s); | |
228 | } | |
bd5635a1 RP |
229 | } |
230 | } | |
231 | ||
232 | /* Make a copy of the string at PTR with SIZE characters in the symbol obstack | |
233 | (and add a null character at the end in the copy). | |
234 | Returns the address of the copy. */ | |
235 | ||
236 | char * | |
30875e1c | 237 | obsavestring (ptr, size, obstackp) |
bd5635a1 RP |
238 | char *ptr; |
239 | int size; | |
30875e1c | 240 | struct obstack *obstackp; |
bd5635a1 | 241 | { |
30875e1c | 242 | register char *p = (char *) obstack_alloc (obstackp, size + 1); |
ade40d31 | 243 | /* Open-coded memcpy--saves function call time. |
bd5635a1 RP |
244 | These strings are usually short. */ |
245 | { | |
246 | register char *p1 = ptr; | |
247 | register char *p2 = p; | |
248 | char *end = ptr + size; | |
249 | while (p1 != end) | |
250 | *p2++ = *p1++; | |
251 | } | |
252 | p[size] = 0; | |
253 | return p; | |
254 | } | |
255 | ||
256 | /* Concatenate strings S1, S2 and S3; return the new string. | |
257 | Space is found in the symbol_obstack. */ | |
258 | ||
259 | char * | |
30875e1c SG |
260 | obconcat (obstackp, s1, s2, s3) |
261 | struct obstack *obstackp; | |
262 | const char *s1, *s2, *s3; | |
bd5635a1 RP |
263 | { |
264 | register int len = strlen (s1) + strlen (s2) + strlen (s3) + 1; | |
30875e1c | 265 | register char *val = (char *) obstack_alloc (obstackp, len); |
bd5635a1 RP |
266 | strcpy (val, s1); |
267 | strcat (val, s2); | |
268 | strcat (val, s3); | |
269 | return val; | |
270 | } | |
bd5635a1 RP |
271 | |
272 | /* Get the symbol table that corresponds to a partial_symtab. | |
273 | This is fast after the first time you do it. In fact, there | |
274 | is an even faster macro PSYMTAB_TO_SYMTAB that does the fast | |
275 | case inline. */ | |
276 | ||
277 | struct symtab * | |
278 | psymtab_to_symtab (pst) | |
279 | register struct partial_symtab *pst; | |
280 | { | |
bd5635a1 RP |
281 | /* If it's been looked up before, return it. */ |
282 | if (pst->symtab) | |
283 | return pst->symtab; | |
284 | ||
285 | /* If it has not yet been read in, read it. */ | |
286 | if (!pst->readin) | |
287 | { | |
288 | (*pst->read_symtab) (pst); | |
289 | } | |
290 | ||
61a7292f | 291 | return pst->symtab; |
bd5635a1 RP |
292 | } |
293 | ||
bf349b77 FF |
294 | /* Initialize entry point information for this objfile. */ |
295 | ||
296 | void | |
297 | init_entry_point_info (objfile) | |
298 | struct objfile *objfile; | |
299 | { | |
300 | /* Save startup file's range of PC addresses to help blockframe.c | |
301 | decide where the bottom of the stack is. */ | |
302 | ||
303 | if (bfd_get_file_flags (objfile -> obfd) & EXEC_P) | |
304 | { | |
305 | /* Executable file -- record its entry point so we'll recognize | |
306 | the startup file because it contains the entry point. */ | |
307 | objfile -> ei.entry_point = bfd_get_start_address (objfile -> obfd); | |
308 | } | |
309 | else | |
310 | { | |
311 | /* Examination of non-executable.o files. Short-circuit this stuff. */ | |
312 | /* ~0 will not be in any file, we hope. */ | |
313 | objfile -> ei.entry_point = ~0; | |
314 | /* set the startup file to be an empty range. */ | |
315 | objfile -> ei.entry_file_lowpc = 0; | |
316 | objfile -> ei.entry_file_highpc = 0; | |
317 | } | |
318 | } | |
319 | ||
a8e033f2 SG |
320 | /* Remember the lowest-addressed loadable section we've seen. |
321 | This function is called via bfd_map_over_sections. */ | |
322 | ||
323 | #if 0 /* Not used yet */ | |
324 | static void | |
325 | find_lowest_section (abfd, sect, obj) | |
326 | bfd *abfd; | |
327 | asection *sect; | |
328 | PTR obj; | |
329 | { | |
330 | asection **lowest = (asection **)obj; | |
331 | ||
332 | if (0 == (bfd_get_section_flags (abfd, sect) & SEC_LOAD)) | |
333 | return; | |
334 | if (!*lowest) | |
335 | *lowest = sect; /* First loadable section */ | |
336 | else if (bfd_section_vma (abfd, *lowest) >= bfd_section_vma (abfd, sect)) | |
337 | *lowest = sect; /* A lower loadable section */ | |
338 | } | |
339 | #endif | |
340 | ||
bd5635a1 RP |
341 | /* Process a symbol file, as either the main file or as a dynamically |
342 | loaded file. | |
343 | ||
b3fdaf3d JK |
344 | NAME is the file name (which will be tilde-expanded and made |
345 | absolute herein) (but we don't free or modify NAME itself). | |
346 | FROM_TTY says how verbose to be. MAINLINE specifies whether this | |
347 | is the main symbol file, or whether it's an extra symbol file such | |
348 | as dynamically loaded code. If !mainline, ADDR is the address | |
4369a140 JG |
349 | where the text segment was loaded. If VERBO, the caller has printed |
350 | a verbose message about the symbol reading (and complaints can be | |
351 | more terse about it). */ | |
bd5635a1 RP |
352 | |
353 | void | |
4369a140 | 354 | syms_from_objfile (objfile, addr, mainline, verbo) |
7d9884b9 | 355 | struct objfile *objfile; |
bd5635a1 RP |
356 | CORE_ADDR addr; |
357 | int mainline; | |
4369a140 | 358 | int verbo; |
bd5635a1 | 359 | { |
a8e033f2 SG |
360 | struct section_offsets *section_offsets; |
361 | asection *lowest_sect; | |
ade40d31 | 362 | struct cleanup *old_chain; |
bd5635a1 | 363 | |
bf349b77 | 364 | init_entry_point_info (objfile); |
80d68b1d | 365 | find_sym_fns (objfile); |
bd5635a1 | 366 | |
ade40d31 RP |
367 | /* Make sure that partially constructed symbol tables will be cleaned up |
368 | if an error occurs during symbol reading. */ | |
369 | old_chain = make_cleanup (free_objfile, objfile); | |
370 | ||
bd5635a1 RP |
371 | if (mainline) |
372 | { | |
ade40d31 RP |
373 | /* We will modify the main symbol table, make sure that all its users |
374 | will be cleaned up if an error occurs during symbol reading. */ | |
375 | make_cleanup (clear_symtab_users, 0); | |
376 | ||
bd5635a1 RP |
377 | /* Since no error yet, throw away the old symbol table. */ |
378 | ||
80d68b1d FF |
379 | if (symfile_objfile != NULL) |
380 | { | |
381 | free_objfile (symfile_objfile); | |
382 | symfile_objfile = NULL; | |
383 | } | |
bd5635a1 | 384 | |
80d68b1d | 385 | (*objfile -> sf -> sym_new_init) (objfile); |
a8e033f2 | 386 | } |
bd5635a1 | 387 | |
a8e033f2 SG |
388 | /* Convert addr into an offset rather than an absolute address. |
389 | We find the lowest address of a loaded segment in the objfile, | |
390 | and assume that <addr> is where that got loaded. Due to historical | |
391 | precedent, we warn if that doesn't happen to be the ".text" | |
392 | segment. */ | |
80d68b1d | 393 | |
a8e033f2 SG |
394 | if (mainline) |
395 | { | |
396 | addr = 0; /* No offset from objfile addresses. */ | |
397 | } | |
398 | else | |
399 | { | |
400 | lowest_sect = bfd_get_section_by_name (objfile->obfd, ".text"); | |
401 | #if 0 | |
402 | lowest_sect = 0; | |
403 | bfd_map_over_sections (objfile->obfd, find_lowest_section, | |
404 | (PTR) &lowest_sect); | |
405 | #endif | |
406 | ||
407 | if (lowest_sect == 0) | |
408 | warning ("no loadable sections found in added symbol-file %s", | |
409 | objfile->name); | |
410 | else if (0 == bfd_get_section_name (objfile->obfd, lowest_sect) | |
2e4964ad | 411 | || !STREQ (".text", |
a8e033f2 SG |
412 | bfd_get_section_name (objfile->obfd, lowest_sect))) |
413 | warning ("Lowest section in %s is %s at 0x%x", | |
414 | objfile->name, | |
415 | bfd_section_name (objfile->obfd, lowest_sect), | |
416 | bfd_section_vma (objfile->obfd, lowest_sect)); | |
417 | ||
418 | if (lowest_sect) | |
419 | addr -= bfd_section_vma (objfile->obfd, lowest_sect); | |
bd5635a1 RP |
420 | } |
421 | ||
80d68b1d FF |
422 | /* Initialize symbol reading routines for this objfile, allow complaints to |
423 | appear for this new file, and record how verbose to be, then do the | |
424 | initial symbol reading for this file. */ | |
4369a140 | 425 | |
80d68b1d FF |
426 | (*objfile -> sf -> sym_init) (objfile); |
427 | clear_complaints (1, verbo); | |
2093fe68 RP |
428 | |
429 | /* If objfile->sf->sym_offsets doesn't set this, we don't care | |
430 | (currently). */ | |
431 | objfile->num_sections = 0; /* krp-FIXME: why zero? */ | |
a8e033f2 | 432 | section_offsets = (*objfile -> sf -> sym_offsets) (objfile, addr); |
2093fe68 RP |
433 | objfile->section_offsets = section_offsets; |
434 | ||
4365c36c JK |
435 | #ifndef IBM6000_TARGET |
436 | /* This is a SVR4/SunOS specific hack, I think. In any event, it | |
437 | screws RS/6000. sym_offsets should be doing this sort of thing, | |
438 | because it knows the mapping between bfd sections and | |
439 | section_offsets. */ | |
5aefc1ca FF |
440 | /* This is a hack. As far as I can tell, section offsets are not |
441 | target dependent. They are all set to addr with a couple of | |
442 | exceptions. The exceptions are sysvr4 shared libraries, whose | |
443 | offsets are kept in solib structures anyway and rs6000 xcoff | |
444 | which handles shared libraries in a completely unique way. | |
445 | ||
446 | Section offsets are built similarly, except that they are built | |
447 | by adding addr in all cases because there is no clear mapping | |
448 | from section_offsets into actual sections. Note that solib.c | |
449 | has a different algorythm for finding section offsets. | |
450 | ||
451 | These should probably all be collapsed into some target | |
452 | independent form of shared library support. FIXME. */ | |
453 | ||
454 | if (addr) | |
455 | { | |
456 | struct obj_section *s; | |
457 | ||
458 | for (s = objfile->sections; s < objfile->sections_end; ++s) | |
459 | { | |
460 | s->addr -= s->offset; | |
461 | s->addr += addr; | |
462 | s->endaddr -= s->offset; | |
463 | s->endaddr += addr; | |
464 | s->offset += addr; | |
465 | } | |
466 | } | |
4365c36c | 467 | #endif /* not IBM6000_TARGET */ |
5aefc1ca | 468 | |
a8e033f2 | 469 | (*objfile -> sf -> sym_read) (objfile, section_offsets, mainline); |
bd5635a1 RP |
470 | |
471 | /* Don't allow char * to have a typename (else would get caddr_t.) */ | |
472 | /* Ditto void *. FIXME should do this for all the builtin types. */ | |
473 | ||
474 | TYPE_NAME (lookup_pointer_type (builtin_type_char)) = 0; | |
475 | TYPE_NAME (lookup_pointer_type (builtin_type_void)) = 0; | |
476 | ||
9342ecb9 JG |
477 | /* Mark the objfile has having had initial symbol read attempted. Note |
478 | that this does not mean we found any symbols... */ | |
479 | ||
480 | objfile -> flags |= OBJF_SYMS; | |
ade40d31 RP |
481 | |
482 | /* Discard cleanups as symbol reading was successful. */ | |
483 | ||
484 | discard_cleanups (old_chain); | |
9342ecb9 JG |
485 | } |
486 | ||
ade40d31 | 487 | /* Perform required actions after either reading in the initial |
9342ecb9 JG |
488 | symbols for a new objfile, or mapping in the symbols from a reusable |
489 | objfile. */ | |
490 | ||
491 | void | |
492 | new_symfile_objfile (objfile, mainline, verbo) | |
493 | struct objfile *objfile; | |
494 | int mainline; | |
495 | int verbo; | |
496 | { | |
ade40d31 RP |
497 | |
498 | /* If this is the main symbol file we have to clean up all users of the | |
499 | old main symbol file. Otherwise it is sufficient to fixup all the | |
500 | breakpoints that may have been redefined by this symbol file. */ | |
bd5635a1 RP |
501 | if (mainline) |
502 | { | |
503 | /* OK, make it the "real" symbol file. */ | |
7d9884b9 | 504 | symfile_objfile = objfile; |
bd5635a1 | 505 | |
ade40d31 RP |
506 | clear_symtab_users (); |
507 | } | |
508 | else | |
509 | { | |
510 | breakpoint_re_set (); | |
511 | } | |
4369a140 JG |
512 | |
513 | /* We're done reading the symbol file; finish off complaints. */ | |
80d68b1d | 514 | clear_complaints (0, verbo); |
30875e1c | 515 | } |
d47d5315 JG |
516 | |
517 | /* Process a symbol file, as either the main file or as a dynamically | |
518 | loaded file. | |
519 | ||
520 | NAME is the file name (which will be tilde-expanded and made | |
521 | absolute herein) (but we don't free or modify NAME itself). | |
522 | FROM_TTY says how verbose to be. MAINLINE specifies whether this | |
523 | is the main symbol file, or whether it's an extra symbol file such | |
524 | as dynamically loaded code. If !mainline, ADDR is the address | |
30875e1c | 525 | where the text segment was loaded. |
d47d5315 | 526 | |
30875e1c SG |
527 | Upon success, returns a pointer to the objfile that was added. |
528 | Upon failure, jumps back to command level (never returns). */ | |
529 | ||
530 | struct objfile * | |
b0246b3b | 531 | symbol_file_add (name, from_tty, addr, mainline, mapped, readnow) |
d47d5315 JG |
532 | char *name; |
533 | int from_tty; | |
534 | CORE_ADDR addr; | |
535 | int mainline; | |
318bf84f | 536 | int mapped; |
b0246b3b | 537 | int readnow; |
d47d5315 | 538 | { |
7d9884b9 | 539 | struct objfile *objfile; |
b0246b3b | 540 | struct partial_symtab *psymtab; |
80d68b1d | 541 | bfd *abfd; |
d47d5315 | 542 | |
2093fe68 RP |
543 | /* Open a bfd for the file, and give user a chance to burp if we'd be |
544 | interactively wiping out any existing symbols. */ | |
80d68b1d FF |
545 | |
546 | abfd = symfile_bfd_open (name); | |
d47d5315 | 547 | |
80d68b1d FF |
548 | if ((have_full_symbols () || have_partial_symbols ()) |
549 | && mainline | |
550 | && from_tty | |
551 | && !query ("Load new symbol table from \"%s\"? ", name)) | |
552 | error ("Not confirmed."); | |
a8e033f2 | 553 | |
80d68b1d FF |
554 | objfile = allocate_objfile (abfd, mapped); |
555 | ||
318bf84f FF |
556 | /* If the objfile uses a mapped symbol file, and we have a psymtab for |
557 | it, then skip reading any symbols at this time. */ | |
d47d5315 | 558 | |
bf349b77 | 559 | if ((objfile -> flags & OBJF_MAPPED) && (objfile -> flags & OBJF_SYMS)) |
d47d5315 | 560 | { |
80d68b1d | 561 | /* We mapped in an existing symbol table file that already has had |
bf349b77 FF |
562 | initial symbol reading performed, so we can skip that part. Notify |
563 | the user that instead of reading the symbols, they have been mapped. | |
564 | */ | |
318bf84f FF |
565 | if (from_tty || info_verbose) |
566 | { | |
80d68b1d FF |
567 | printf_filtered ("Mapped symbols for %s...", name); |
568 | wrap_here (""); | |
318bf84f FF |
569 | fflush (stdout); |
570 | } | |
9342ecb9 JG |
571 | init_entry_point_info (objfile); |
572 | find_sym_fns (objfile); | |
d47d5315 | 573 | } |
318bf84f | 574 | else |
bd5635a1 | 575 | { |
80d68b1d | 576 | /* We either created a new mapped symbol table, mapped an existing |
bf349b77 FF |
577 | symbol table file which has not had initial symbol reading |
578 | performed, or need to read an unmapped symbol table. */ | |
318bf84f FF |
579 | if (from_tty || info_verbose) |
580 | { | |
581 | printf_filtered ("Reading symbols from %s...", name); | |
582 | wrap_here (""); | |
583 | fflush (stdout); | |
584 | } | |
318bf84f | 585 | syms_from_objfile (objfile, addr, mainline, from_tty); |
80d68b1d FF |
586 | } |
587 | ||
588 | /* We now have at least a partial symbol table. Check to see if the | |
589 | user requested that all symbols be read on initial access via either | |
590 | the gdb startup command line or on a per symbol file basis. Expand | |
591 | all partial symbol tables for this objfile if so. */ | |
b0246b3b | 592 | |
bf349b77 | 593 | if (readnow || readnow_symbol_files) |
80d68b1d | 594 | { |
318bf84f FF |
595 | if (from_tty || info_verbose) |
596 | { | |
80d68b1d FF |
597 | printf_filtered ("expanding to full symbols..."); |
598 | wrap_here (""); | |
318bf84f FF |
599 | fflush (stdout); |
600 | } | |
80d68b1d FF |
601 | |
602 | for (psymtab = objfile -> psymtabs; | |
603 | psymtab != NULL; | |
604 | psymtab = psymtab -> next) | |
605 | { | |
4ed3a9ea | 606 | psymtab_to_symtab (psymtab); |
80d68b1d FF |
607 | } |
608 | } | |
609 | ||
610 | if (from_tty || info_verbose) | |
611 | { | |
612 | printf_filtered ("done.\n"); | |
613 | fflush (stdout); | |
bd5635a1 | 614 | } |
80d68b1d | 615 | |
ade40d31 RP |
616 | new_symfile_objfile (objfile, mainline, from_tty); |
617 | ||
618 | /* Getting new symbols may change our opinion about what is | |
619 | frameless. */ | |
620 | ||
621 | reinit_frame_cache (); | |
622 | ||
30875e1c | 623 | return (objfile); |
bd5635a1 RP |
624 | } |
625 | ||
626 | /* This is the symbol-file command. Read the file, analyze its symbols, | |
30875e1c | 627 | and add a struct symtab to a symtab list. */ |
bd5635a1 RP |
628 | |
629 | void | |
30875e1c SG |
630 | symbol_file_command (args, from_tty) |
631 | char *args; | |
bd5635a1 RP |
632 | int from_tty; |
633 | { | |
30875e1c | 634 | char **argv; |
b0246b3b | 635 | char *name = NULL; |
30875e1c | 636 | struct cleanup *cleanups; |
318bf84f | 637 | int mapped = 0; |
30875e1c | 638 | int readnow = 0; |
bd5635a1 RP |
639 | |
640 | dont_repeat (); | |
641 | ||
30875e1c | 642 | if (args == NULL) |
bd5635a1 | 643 | { |
cba0d141 JG |
644 | if ((have_full_symbols () || have_partial_symbols ()) |
645 | && from_tty | |
646 | && !query ("Discard symbol table from `%s'? ", | |
647 | symfile_objfile -> name)) | |
648 | error ("Not confirmed."); | |
649 | free_all_objfiles (); | |
30875e1c | 650 | symfile_objfile = NULL; |
9342ecb9 JG |
651 | if (from_tty) |
652 | { | |
e58de8a2 | 653 | printf ("No symbol file now.\n"); |
9342ecb9 | 654 | } |
bd5635a1 | 655 | } |
30875e1c SG |
656 | else |
657 | { | |
658 | if ((argv = buildargv (args)) == NULL) | |
659 | { | |
318bf84f | 660 | nomem (0); |
30875e1c SG |
661 | } |
662 | cleanups = make_cleanup (freeargv, (char *) argv); | |
b0246b3b | 663 | while (*argv != NULL) |
30875e1c | 664 | { |
2e4964ad | 665 | if (STREQ (*argv, "-mapped")) |
30875e1c | 666 | { |
318bf84f | 667 | mapped = 1; |
30875e1c | 668 | } |
2e4964ad | 669 | else if (STREQ (*argv, "-readnow")) |
30875e1c SG |
670 | { |
671 | readnow = 1; | |
672 | } | |
b0246b3b FF |
673 | else if (**argv == '-') |
674 | { | |
675 | error ("unknown option `%s'", *argv); | |
676 | } | |
677 | else | |
678 | { | |
679 | name = *argv; | |
680 | } | |
681 | argv++; | |
30875e1c | 682 | } |
2403f49b | 683 | |
b0246b3b FF |
684 | if (name == NULL) |
685 | { | |
686 | error ("no symbol file name was specified"); | |
687 | } | |
688 | else | |
30875e1c | 689 | { |
4ed3a9ea | 690 | symbol_file_add (name, from_tty, (CORE_ADDR)0, 1, mapped, readnow); |
e58de8a2 | 691 | set_initial_language (); |
30875e1c SG |
692 | } |
693 | do_cleanups (cleanups); | |
694 | } | |
bd5635a1 RP |
695 | } |
696 | ||
e58de8a2 FF |
697 | /* Set the initial language. |
698 | ||
699 | A better solution would be to record the language in the psymtab when reading | |
700 | partial symbols, and then use it (if known) to set the language. This would | |
701 | be a win for formats that encode the language in an easily discoverable place, | |
702 | such as DWARF. For stabs, we can jump through hoops looking for specially | |
703 | named symbols or try to intuit the language from the specific type of stabs | |
704 | we find, but we can't do that until later when we read in full symbols. | |
705 | FIXME. */ | |
706 | ||
707 | static void | |
708 | set_initial_language () | |
709 | { | |
710 | struct partial_symtab *pst; | |
711 | enum language lang = language_unknown; | |
712 | ||
713 | pst = find_main_psymtab (); | |
714 | if (pst != NULL) | |
715 | { | |
716 | if (pst -> filename != NULL) | |
717 | { | |
718 | lang = deduce_language_from_filename (pst -> filename); | |
719 | } | |
720 | if (lang == language_unknown) | |
721 | { | |
722 | /* Make C the default language */ | |
723 | lang = language_c; | |
724 | } | |
725 | set_language (lang); | |
726 | expected_language = current_language; /* Don't warn the user */ | |
727 | } | |
728 | } | |
729 | ||
b0246b3b FF |
730 | /* Open file specified by NAME and hand it off to BFD for preliminary |
731 | analysis. Result is a newly initialized bfd *, which includes a newly | |
732 | malloc'd` copy of NAME (tilde-expanded and made absolute). | |
7d9884b9 | 733 | In case of trouble, error() is called. */ |
bd5635a1 | 734 | |
b0246b3b FF |
735 | static bfd * |
736 | symfile_bfd_open (name) | |
bd5635a1 RP |
737 | char *name; |
738 | { | |
739 | bfd *sym_bfd; | |
740 | int desc; | |
741 | char *absolute_name; | |
742 | ||
7d9884b9 | 743 | name = tilde_expand (name); /* Returns 1st new malloc'd copy */ |
bd5635a1 | 744 | |
7d9884b9 | 745 | /* Look down path for it, allocate 2nd new malloc'd copy. */ |
2093fe68 | 746 | desc = openp (getenv ("PATH"), 1, name, O_RDONLY | O_BINARY, 0, &absolute_name); |
b0246b3b FF |
747 | if (desc < 0) |
748 | { | |
749 | make_cleanup (free, name); | |
750 | perror_with_name (name); | |
751 | } | |
7d9884b9 | 752 | free (name); /* Free 1st new malloc'd copy */ |
30875e1c | 753 | name = absolute_name; /* Keep 2nd malloc'd copy in bfd */ |
346168a2 | 754 | /* It'll be freed in free_objfile(). */ |
bd5635a1 | 755 | |
ade40d31 | 756 | sym_bfd = bfd_fdopenr (name, gnutarget, desc); |
bd5635a1 RP |
757 | if (!sym_bfd) |
758 | { | |
759 | close (desc); | |
7d9884b9 | 760 | make_cleanup (free, name); |
b0246b3b FF |
761 | error ("\"%s\": can't open to read symbols: %s.", name, |
762 | bfd_errmsg (bfd_error)); | |
bd5635a1 | 763 | } |
e58de8a2 | 764 | sym_bfd->cacheable = true; |
bd5635a1 | 765 | |
b0246b3b FF |
766 | if (!bfd_check_format (sym_bfd, bfd_object)) |
767 | { | |
768 | bfd_close (sym_bfd); /* This also closes desc */ | |
769 | make_cleanup (free, name); | |
770 | error ("\"%s\": can't read symbols: %s.", name, | |
771 | bfd_errmsg (bfd_error)); | |
772 | } | |
7d9884b9 | 773 | |
b0246b3b | 774 | return (sym_bfd); |
7d9884b9 JG |
775 | } |
776 | ||
80d68b1d FF |
777 | /* Link a new symtab_fns into the global symtab_fns list. Called on gdb |
778 | startup by the _initialize routine in each object file format reader, | |
779 | to register information about each format the the reader is prepared | |
780 | to handle. */ | |
bd5635a1 RP |
781 | |
782 | void | |
783 | add_symtab_fns (sf) | |
784 | struct sym_fns *sf; | |
785 | { | |
786 | sf->next = symtab_fns; | |
787 | symtab_fns = sf; | |
788 | } | |
789 | ||
790 | ||
791 | /* Initialize to read symbols from the symbol file sym_bfd. It either | |
80d68b1d FF |
792 | returns or calls error(). The result is an initialized struct sym_fns |
793 | in the objfile structure, that contains cached information about the | |
794 | symbol file. */ | |
bd5635a1 | 795 | |
80d68b1d FF |
796 | static void |
797 | find_sym_fns (objfile) | |
7d9884b9 | 798 | struct objfile *objfile; |
bd5635a1 | 799 | { |
ac88ca20 | 800 | struct sym_fns *sf; |
bd5635a1 | 801 | |
80d68b1d | 802 | for (sf = symtab_fns; sf != NULL; sf = sf -> next) |
bd5635a1 | 803 | { |
80d68b1d FF |
804 | if (strncmp (bfd_get_target (objfile -> obfd), |
805 | sf -> sym_name, sf -> sym_namelen) == 0) | |
bd5635a1 | 806 | { |
80d68b1d FF |
807 | objfile -> sf = sf; |
808 | return; | |
bd5635a1 RP |
809 | } |
810 | } | |
c9bd6710 | 811 | error ("I'm sorry, Dave, I can't do that. Symbol format `%s' unknown.", |
b0246b3b | 812 | bfd_get_target (objfile -> obfd)); |
bd5635a1 RP |
813 | } |
814 | \f | |
815 | /* This function runs the load command of our current target. */ | |
816 | ||
30875e1c | 817 | static void |
bd5635a1 RP |
818 | load_command (arg, from_tty) |
819 | char *arg; | |
820 | int from_tty; | |
821 | { | |
822 | target_load (arg, from_tty); | |
823 | } | |
824 | ||
ade40d31 RP |
825 | /* This version of "load" should be usable for any target. Currently |
826 | it is just used for remote targets, not inftarg.c or core files, | |
827 | on the theory that only in that case is it useful. | |
828 | ||
829 | Avoiding xmodem and the like seems like a win (a) because we don't have | |
830 | to worry about finding it, and (b) On VMS, fork() is very slow and so | |
831 | we don't want to run a subprocess. On the other hand, I'm not sure how | |
832 | performance compares. */ | |
833 | void | |
834 | generic_load (filename, from_tty) | |
835 | char *filename; | |
836 | int from_tty; | |
837 | { | |
838 | struct cleanup *old_cleanups; | |
839 | asection *s; | |
840 | bfd *loadfile_bfd = bfd_openr (filename, gnutarget); | |
841 | if (loadfile_bfd == NULL) | |
842 | { | |
843 | perror_with_name (filename); | |
844 | return; | |
845 | } | |
846 | old_cleanups = make_cleanup (bfd_close, loadfile_bfd); | |
847 | ||
848 | if (!bfd_check_format (loadfile_bfd, bfd_object)) | |
849 | { | |
850 | error ("\"%s\" is not an object file: %s", filename, | |
851 | bfd_errmsg (bfd_error)); | |
852 | } | |
853 | ||
854 | for (s = loadfile_bfd->sections; s; s = s->next) | |
855 | { | |
856 | if (s->flags & SEC_LOAD) | |
857 | { | |
858 | bfd_size_type size; | |
859 | ||
860 | size = bfd_get_section_size_before_reloc (s); | |
861 | if (size > 0) | |
862 | { | |
863 | char *buffer; | |
864 | struct cleanup *old_chain; | |
865 | bfd_vma vma; | |
866 | ||
867 | buffer = xmalloc (size); | |
868 | old_chain = make_cleanup (free, buffer); | |
869 | ||
870 | vma = bfd_get_section_vma (loadfile_bfd, s); | |
871 | ||
872 | /* Is this really necessary? I guess it gives the user something | |
873 | to look at during a long download. */ | |
874 | printf_filtered ("Loading section %s, size 0x%x vma 0x%x\n", | |
875 | bfd_get_section_name (loadfile_bfd, s), | |
876 | size, vma); | |
877 | ||
878 | bfd_get_section_contents (loadfile_bfd, s, buffer, 0, size); | |
879 | ||
880 | target_write_memory (vma, buffer, size); | |
881 | ||
882 | do_cleanups (old_chain); | |
883 | } | |
884 | } | |
885 | } | |
886 | ||
887 | /* We were doing this in remote-mips.c, I suspect it is right | |
888 | for other targets too. */ | |
889 | write_pc (loadfile_bfd->start_address); | |
890 | ||
891 | /* FIXME: are we supposed to call symbol_file_add or not? According to | |
892 | a comment from remote-mips.c (where a call to symbol_file_add was | |
893 | commented out), making the call confuses GDB if more than one file is | |
894 | loaded in. remote-nindy.c had no call to symbol_file_add, but remote-vx.c | |
895 | does. */ | |
896 | ||
897 | do_cleanups (old_cleanups); | |
898 | } | |
899 | ||
61a7292f SG |
900 | /* This function allows the addition of incrementally linked object files. |
901 | It does not modify any state in the target, only in the debugger. */ | |
bd5635a1 | 902 | |
e1ce8aa5 | 903 | /* ARGSUSED */ |
30875e1c | 904 | static void |
b0246b3b FF |
905 | add_symbol_file_command (args, from_tty) |
906 | char *args; | |
bd5635a1 RP |
907 | int from_tty; |
908 | { | |
b0246b3b | 909 | char *name = NULL; |
bd5635a1 | 910 | CORE_ADDR text_addr; |
b0246b3b | 911 | char *arg; |
ac88ca20 JG |
912 | int readnow = 0; |
913 | int mapped = 0; | |
bd5635a1 | 914 | |
b0246b3b | 915 | dont_repeat (); |
61a7292f | 916 | |
b0246b3b FF |
917 | if (args == NULL) |
918 | { | |
919 | error ("add-symbol-file takes a file name and an address"); | |
920 | } | |
bd5635a1 | 921 | |
b0246b3b | 922 | /* Make a copy of the string that we can safely write into. */ |
bd5635a1 | 923 | |
b0246b3b FF |
924 | args = strdup (args); |
925 | make_cleanup (free, args); | |
926 | ||
927 | /* Pick off any -option args and the file name. */ | |
928 | ||
929 | while ((*args != '\000') && (name == NULL)) | |
930 | { | |
931 | while (isspace (*args)) {args++;} | |
932 | arg = args; | |
933 | while ((*args != '\000') && !isspace (*args)) {args++;} | |
934 | if (*args != '\000') | |
935 | { | |
936 | *args++ = '\000'; | |
937 | } | |
938 | if (*arg != '-') | |
939 | { | |
940 | name = arg; | |
941 | } | |
2e4964ad | 942 | else if (STREQ (arg, "-mapped")) |
b0246b3b FF |
943 | { |
944 | mapped = 1; | |
945 | } | |
2e4964ad | 946 | else if (STREQ (arg, "-readnow")) |
b0246b3b FF |
947 | { |
948 | readnow = 1; | |
949 | } | |
950 | else | |
951 | { | |
952 | error ("unknown option `%s'", arg); | |
953 | } | |
954 | } | |
bd5635a1 | 955 | |
b0246b3b FF |
956 | /* After picking off any options and the file name, args should be |
957 | left pointing at the remainder of the command line, which should | |
958 | be the address expression to evaluate. */ | |
bd5635a1 | 959 | |
b0246b3b FF |
960 | if ((name == NULL) || (*args == '\000') ) |
961 | { | |
962 | error ("add-symbol-file takes a file name and an address"); | |
963 | } | |
964 | name = tilde_expand (name); | |
965 | make_cleanup (free, name); | |
bd5635a1 | 966 | |
b0246b3b | 967 | text_addr = parse_and_eval_address (args); |
bd5635a1 | 968 | |
d8ce1326 JG |
969 | if (!query ("add symbol table from file \"%s\" at text_addr = %s?\n", |
970 | name, local_hex_string (text_addr))) | |
bd5635a1 RP |
971 | error ("Not confirmed."); |
972 | ||
4ed3a9ea | 973 | symbol_file_add (name, 0, text_addr, 0, mapped, readnow); |
bd5635a1 RP |
974 | } |
975 | \f | |
7d9884b9 | 976 | /* Re-read symbols if a symbol-file has changed. */ |
bd5635a1 RP |
977 | void |
978 | reread_symbols () | |
979 | { | |
7d9884b9 JG |
980 | struct objfile *objfile; |
981 | long new_modtime; | |
982 | int reread_one = 0; | |
cba0d141 JG |
983 | struct stat new_statbuf; |
984 | int res; | |
bd5635a1 RP |
985 | |
986 | /* With the addition of shared libraries, this should be modified, | |
987 | the load time should be saved in the partial symbol tables, since | |
988 | different tables may come from different source files. FIXME. | |
989 | This routine should then walk down each partial symbol table | |
30875e1c | 990 | and see if the symbol table that it originates from has been changed */ |
bd5635a1 | 991 | |
30875e1c | 992 | the_big_top: |
7d9884b9 JG |
993 | for (objfile = object_files; objfile; objfile = objfile->next) { |
994 | if (objfile->obfd) { | |
1eeba686 | 995 | #ifdef IBM6000_TARGET |
318bf84f FF |
996 | /* If this object is from a shared library, then you should |
997 | stat on the library name, not member name. */ | |
998 | ||
999 | if (objfile->obfd->my_archive) | |
1000 | res = stat (objfile->obfd->my_archive->filename, &new_statbuf); | |
1001 | else | |
1002 | #endif | |
cba0d141 JG |
1003 | res = stat (objfile->name, &new_statbuf); |
1004 | if (res != 0) { | |
1005 | /* FIXME, should use print_sys_errmsg but it's not filtered. */ | |
1006 | printf_filtered ("`%s' has disappeared; keeping its symbols.\n", | |
1007 | objfile->name); | |
1008 | continue; | |
1009 | } | |
1010 | new_modtime = new_statbuf.st_mtime; | |
7d9884b9 JG |
1011 | if (new_modtime != objfile->mtime) { |
1012 | printf_filtered ("`%s' has changed; re-reading symbols.\n", | |
1013 | objfile->name); | |
1014 | /* FIXME, this should use a different command...that would only | |
30875e1c SG |
1015 | affect this objfile's symbols, and would reset objfile->mtime. |
1016 | (objfile->mtime = new_modtime;) | |
1017 | HOWEVER, that command isn't written yet -- so call symbol_file_ | |
1018 | command, and restart the scan from the top, because it munges | |
1019 | the object_files list. */ | |
7d9884b9 | 1020 | symbol_file_command (objfile->name, 0); |
7d9884b9 | 1021 | reread_one = 1; |
30875e1c | 1022 | goto the_big_top; /* Start over. */ |
7d9884b9 | 1023 | } |
bd5635a1 | 1024 | } |
7d9884b9 JG |
1025 | } |
1026 | ||
1027 | if (reread_one) | |
1028 | breakpoint_re_set (); | |
bd5635a1 | 1029 | } |
bd5635a1 | 1030 | |
bd5635a1 | 1031 | \f |
7d9884b9 JG |
1032 | enum language |
1033 | deduce_language_from_filename (filename) | |
1034 | char *filename; | |
1035 | { | |
2093fe68 | 1036 | char *c; |
7d9884b9 | 1037 | |
2093fe68 RP |
1038 | if (0 == filename) |
1039 | ; /* Get default */ | |
1040 | else if (0 == (c = strrchr (filename, '.'))) | |
1041 | ; /* Get default. */ | |
2e4964ad | 1042 | else if(STREQ(c,".mod")) |
2093fe68 | 1043 | return language_m2; |
2e4964ad | 1044 | else if(STREQ(c,".c")) |
2093fe68 | 1045 | return language_c; |
2e4964ad | 1046 | else if(STREQ(c,".cc") || STREQ(c,".C")) |
2093fe68 | 1047 | return language_cplus; |
2e4964ad | 1048 | else if(STREQ(c,".ch") || STREQ(c,".c186") || STREQ(c,".c286")) |
2093fe68 | 1049 | return language_chill; |
7d9884b9 JG |
1050 | |
1051 | return language_unknown; /* default */ | |
1052 | } | |
1053 | \f | |
d8ce1326 JG |
1054 | /* allocate_symtab: |
1055 | ||
1056 | Allocate and partly initialize a new symbol table. Return a pointer | |
1057 | to it. error() if no space. | |
1058 | ||
1059 | Caller must set these fields: | |
1060 | LINETABLE(symtab) | |
1061 | symtab->blockvector | |
d8ce1326 JG |
1062 | symtab->dirname |
1063 | symtab->free_code | |
1064 | symtab->free_ptr | |
1065 | initialize any EXTRA_SYMTAB_INFO | |
1066 | possibly free_named_symtabs (symtab->filename); | |
d8ce1326 JG |
1067 | */ |
1068 | ||
1069 | struct symtab * | |
30875e1c SG |
1070 | allocate_symtab (filename, objfile) |
1071 | char *filename; | |
1072 | struct objfile *objfile; | |
d8ce1326 JG |
1073 | { |
1074 | register struct symtab *symtab; | |
d8ce1326 | 1075 | |
30875e1c SG |
1076 | symtab = (struct symtab *) |
1077 | obstack_alloc (&objfile -> symbol_obstack, sizeof (struct symtab)); | |
4ed3a9ea | 1078 | memset (symtab, 0, sizeof (*symtab)); |
30875e1c SG |
1079 | symtab -> filename = obsavestring (filename, strlen (filename), |
1080 | &objfile -> symbol_obstack); | |
1081 | symtab -> fullname = NULL; | |
1082 | symtab -> language = deduce_language_from_filename (filename); | |
d8ce1326 | 1083 | |
7d9884b9 | 1084 | /* Hook it to the objfile it comes from */ |
30875e1c SG |
1085 | |
1086 | symtab -> objfile = objfile; | |
1087 | symtab -> next = objfile -> symtabs; | |
1088 | objfile -> symtabs = symtab; | |
7d9884b9 JG |
1089 | |
1090 | #ifdef INIT_EXTRA_SYMTAB_INFO | |
30875e1c | 1091 | INIT_EXTRA_SYMTAB_INFO (symtab); |
7d9884b9 | 1092 | #endif |
d8ce1326 | 1093 | |
30875e1c | 1094 | return (symtab); |
d8ce1326 | 1095 | } |
30875e1c SG |
1096 | |
1097 | struct partial_symtab * | |
1098 | allocate_psymtab (filename, objfile) | |
1099 | char *filename; | |
1100 | struct objfile *objfile; | |
1101 | { | |
1102 | struct partial_symtab *psymtab; | |
1103 | ||
cba0d141 JG |
1104 | if (objfile -> free_psymtabs) |
1105 | { | |
1106 | psymtab = objfile -> free_psymtabs; | |
1107 | objfile -> free_psymtabs = psymtab -> next; | |
1108 | } | |
1109 | else | |
1110 | psymtab = (struct partial_symtab *) | |
1111 | obstack_alloc (&objfile -> psymbol_obstack, | |
1112 | sizeof (struct partial_symtab)); | |
1113 | ||
4ed3a9ea | 1114 | memset (psymtab, 0, sizeof (struct partial_symtab)); |
30875e1c SG |
1115 | psymtab -> filename = obsavestring (filename, strlen (filename), |
1116 | &objfile -> psymbol_obstack); | |
1117 | psymtab -> symtab = NULL; | |
1118 | ||
1119 | /* Hook it to the objfile it comes from */ | |
1120 | ||
1121 | psymtab -> objfile = objfile; | |
1122 | psymtab -> next = objfile -> psymtabs; | |
1123 | objfile -> psymtabs = psymtab; | |
1124 | ||
1125 | return (psymtab); | |
1126 | } | |
1127 | ||
d8ce1326 | 1128 | \f |
ade40d31 RP |
1129 | /* Reset all data structures in gdb which may contain references to symbol |
1130 | table date. */ | |
1131 | ||
1132 | void | |
1133 | clear_symtab_users () | |
1134 | { | |
1135 | /* Someday, we should do better than this, by only blowing away | |
1136 | the things that really need to be blown. */ | |
1137 | clear_value_history (); | |
1138 | clear_displays (); | |
1139 | clear_internalvars (); | |
1140 | breakpoint_re_set (); | |
1141 | set_default_breakpoint (0, 0, 0, 0); | |
1142 | current_source_symtab = 0; | |
1143 | current_source_line = 0; | |
1144 | } | |
1145 | ||
9d199712 JG |
1146 | /* clear_symtab_users_once: |
1147 | ||
1148 | This function is run after symbol reading, or from a cleanup. | |
1149 | If an old symbol table was obsoleted, the old symbol table | |
1150 | has been blown away, but the other GDB data structures that may | |
1151 | reference it have not yet been cleared or re-directed. (The old | |
1152 | symtab was zapped, and the cleanup queued, in free_named_symtab() | |
1153 | below.) | |
1154 | ||
1155 | This function can be queued N times as a cleanup, or called | |
1156 | directly; it will do all the work the first time, and then will be a | |
1157 | no-op until the next time it is queued. This works by bumping a | |
1158 | counter at queueing time. Much later when the cleanup is run, or at | |
1159 | the end of symbol processing (in case the cleanup is discarded), if | |
1160 | the queued count is greater than the "done-count", we do the work | |
1161 | and set the done-count to the queued count. If the queued count is | |
1162 | less than or equal to the done-count, we just ignore the call. This | |
1163 | is needed because reading a single .o file will often replace many | |
1164 | symtabs (one per .h file, for example), and we don't want to reset | |
1165 | the breakpoints N times in the user's face. | |
1166 | ||
1167 | The reason we both queue a cleanup, and call it directly after symbol | |
1168 | reading, is because the cleanup protects us in case of errors, but is | |
1169 | discarded if symbol reading is successful. */ | |
1170 | ||
ade40d31 | 1171 | #if 0 |
996ccb30 JK |
1172 | /* FIXME: As free_named_symtabs is currently a big noop this function |
1173 | is no longer needed. */ | |
ade40d31 RP |
1174 | static void |
1175 | clear_symtab_users_once PARAMS ((void)); | |
1176 | ||
9d199712 JG |
1177 | static int clear_symtab_users_queued; |
1178 | static int clear_symtab_users_done; | |
1179 | ||
ade40d31 | 1180 | static void |
9d199712 JG |
1181 | clear_symtab_users_once () |
1182 | { | |
1183 | /* Enforce once-per-`do_cleanups'-semantics */ | |
1184 | if (clear_symtab_users_queued <= clear_symtab_users_done) | |
1185 | return; | |
1186 | clear_symtab_users_done = clear_symtab_users_queued; | |
1187 | ||
ade40d31 | 1188 | clear_symtab_users (); |
9d199712 | 1189 | } |
ade40d31 | 1190 | #endif |
9d199712 JG |
1191 | |
1192 | /* Delete the specified psymtab, and any others that reference it. */ | |
1193 | ||
e1ce8aa5 | 1194 | static void |
9d199712 JG |
1195 | cashier_psymtab (pst) |
1196 | struct partial_symtab *pst; | |
1197 | { | |
46c28185 | 1198 | struct partial_symtab *ps, *pprev = NULL; |
9d199712 JG |
1199 | int i; |
1200 | ||
1201 | /* Find its previous psymtab in the chain */ | |
30875e1c | 1202 | for (ps = pst->objfile->psymtabs; ps; ps = ps->next) { |
9d199712 JG |
1203 | if (ps == pst) |
1204 | break; | |
1205 | pprev = ps; | |
1206 | } | |
1207 | ||
1208 | if (ps) { | |
1209 | /* Unhook it from the chain. */ | |
30875e1c SG |
1210 | if (ps == pst->objfile->psymtabs) |
1211 | pst->objfile->psymtabs = ps->next; | |
9d199712 JG |
1212 | else |
1213 | pprev->next = ps->next; | |
1214 | ||
1215 | /* FIXME, we can't conveniently deallocate the entries in the | |
1216 | partial_symbol lists (global_psymbols/static_psymbols) that | |
1217 | this psymtab points to. These just take up space until all | |
1218 | the psymtabs are reclaimed. Ditto the dependencies list and | |
1219 | filename, which are all in the psymbol_obstack. */ | |
1220 | ||
1221 | /* We need to cashier any psymtab that has this one as a dependency... */ | |
1222 | again: | |
30875e1c | 1223 | for (ps = pst->objfile->psymtabs; ps; ps = ps->next) { |
9d199712 JG |
1224 | for (i = 0; i < ps->number_of_dependencies; i++) { |
1225 | if (ps->dependencies[i] == pst) { | |
1226 | cashier_psymtab (ps); | |
1227 | goto again; /* Must restart, chain has been munged. */ | |
1228 | } | |
1229 | } | |
1230 | } | |
1231 | } | |
1232 | } | |
1233 | ||
1234 | /* If a symtab or psymtab for filename NAME is found, free it along | |
1235 | with any dependent breakpoints, displays, etc. | |
1236 | Used when loading new versions of object modules with the "add-file" | |
1237 | command. This is only called on the top-level symtab or psymtab's name; | |
1238 | it is not called for subsidiary files such as .h files. | |
1239 | ||
1240 | Return value is 1 if we blew away the environment, 0 if not. | |
30875e1c | 1241 | FIXME. The return valu appears to never be used. |
9d199712 JG |
1242 | |
1243 | FIXME. I think this is not the best way to do this. We should | |
1244 | work on being gentler to the environment while still cleaning up | |
1245 | all stray pointers into the freed symtab. */ | |
1246 | ||
1247 | int | |
1248 | free_named_symtabs (name) | |
1249 | char *name; | |
1250 | { | |
30875e1c SG |
1251 | #if 0 |
1252 | /* FIXME: With the new method of each objfile having it's own | |
1253 | psymtab list, this function needs serious rethinking. In particular, | |
1254 | why was it ever necessary to toss psymtabs with specific compilation | |
1255 | unit filenames, as opposed to all psymtabs from a particular symbol | |
ac88ca20 JG |
1256 | file? -- fnf |
1257 | Well, the answer is that some systems permit reloading of particular | |
1258 | compilation units. We want to blow away any old info about these | |
1259 | compilation units, regardless of which objfiles they arrived in. --gnu. */ | |
1260 | ||
1261 | register struct symtab *s; | |
1262 | register struct symtab *prev; | |
1263 | register struct partial_symtab *ps; | |
1264 | struct blockvector *bv; | |
1265 | int blewit = 0; | |
30875e1c | 1266 | |
61a7292f SG |
1267 | /* We only wack things if the symbol-reload switch is set. */ |
1268 | if (!symbol_reloading) | |
1269 | return 0; | |
1270 | ||
d11c44f1 JG |
1271 | /* Some symbol formats have trouble providing file names... */ |
1272 | if (name == 0 || *name == '\0') | |
1273 | return 0; | |
1274 | ||
9d199712 JG |
1275 | /* Look for a psymtab with the specified name. */ |
1276 | ||
1277 | again2: | |
1278 | for (ps = partial_symtab_list; ps; ps = ps->next) { | |
2e4964ad | 1279 | if (STREQ (name, ps->filename)) { |
9d199712 JG |
1280 | cashier_psymtab (ps); /* Blow it away...and its little dog, too. */ |
1281 | goto again2; /* Must restart, chain has been munged */ | |
1282 | } | |
1283 | } | |
1284 | ||
1285 | /* Look for a symtab with the specified name. */ | |
1286 | ||
1287 | for (s = symtab_list; s; s = s->next) | |
1288 | { | |
2e4964ad | 1289 | if (STREQ (name, s->filename)) |
9d199712 JG |
1290 | break; |
1291 | prev = s; | |
1292 | } | |
1293 | ||
1294 | if (s) | |
1295 | { | |
1296 | if (s == symtab_list) | |
1297 | symtab_list = s->next; | |
1298 | else | |
1299 | prev->next = s->next; | |
1300 | ||
1301 | /* For now, queue a delete for all breakpoints, displays, etc., whether | |
1302 | or not they depend on the symtab being freed. This should be | |
1303 | changed so that only those data structures affected are deleted. */ | |
1304 | ||
1305 | /* But don't delete anything if the symtab is empty. | |
1306 | This test is necessary due to a bug in "dbxread.c" that | |
1307 | causes empty symtabs to be created for N_SO symbols that | |
1308 | contain the pathname of the object file. (This problem | |
1309 | has been fixed in GDB 3.9x). */ | |
1310 | ||
c9bd6710 JG |
1311 | bv = BLOCKVECTOR (s); |
1312 | if (BLOCKVECTOR_NBLOCKS (bv) > 2 | |
9d199712 JG |
1313 | || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK)) |
1314 | || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK))) | |
1315 | { | |
1316 | complain (&oldsyms_complaint, name); | |
1317 | ||
1318 | clear_symtab_users_queued++; | |
1319 | make_cleanup (clear_symtab_users_once, 0); | |
1320 | blewit = 1; | |
1321 | } else { | |
1322 | complain (&empty_symtab_complaint, name); | |
1323 | } | |
1324 | ||
1325 | free_symtab (s); | |
1326 | } | |
1327 | else | |
d8ce1326 JG |
1328 | { |
1329 | /* It is still possible that some breakpoints will be affected | |
1330 | even though no symtab was found, since the file might have | |
1331 | been compiled without debugging, and hence not be associated | |
1332 | with a symtab. In order to handle this correctly, we would need | |
1333 | to keep a list of text address ranges for undebuggable files. | |
1334 | For now, we do nothing, since this is a fairly obscure case. */ | |
1335 | ; | |
1336 | } | |
9d199712 | 1337 | |
30875e1c | 1338 | /* FIXME, what about the minimal symbol table? */ |
9d199712 | 1339 | return blewit; |
30875e1c SG |
1340 | #else |
1341 | return (0); | |
1342 | #endif | |
9d199712 JG |
1343 | } |
1344 | \f | |
d4ea2aba PB |
1345 | /* Allocate and partially fill a partial symtab. It will be |
1346 | completely filled at the end of the symbol list. | |
1347 | ||
1348 | SYMFILE_NAME is the name of the symbol-file we are reading from, and ADDR | |
1349 | is the address relative to which its symbols are (incremental) or 0 | |
1350 | (normal). */ | |
1351 | ||
1352 | ||
1353 | struct partial_symtab * | |
a8e033f2 | 1354 | start_psymtab_common (objfile, section_offsets, |
d4ea2aba PB |
1355 | filename, textlow, global_syms, static_syms) |
1356 | struct objfile *objfile; | |
a8e033f2 | 1357 | struct section_offsets *section_offsets; |
d4ea2aba PB |
1358 | char *filename; |
1359 | CORE_ADDR textlow; | |
1360 | struct partial_symbol *global_syms; | |
1361 | struct partial_symbol *static_syms; | |
1362 | { | |
30875e1c SG |
1363 | struct partial_symtab *psymtab; |
1364 | ||
1365 | psymtab = allocate_psymtab (filename, objfile); | |
a8e033f2 | 1366 | psymtab -> section_offsets = section_offsets; |
30875e1c SG |
1367 | psymtab -> textlow = textlow; |
1368 | psymtab -> texthigh = psymtab -> textlow; /* default */ | |
1369 | psymtab -> globals_offset = global_syms - objfile -> global_psymbols.list; | |
1370 | psymtab -> statics_offset = static_syms - objfile -> static_psymbols.list; | |
1371 | return (psymtab); | |
7d9884b9 | 1372 | } |
9342ecb9 JG |
1373 | \f |
1374 | /* Debugging versions of functions that are usually inline macros | |
1375 | (see symfile.h). */ | |
1376 | ||
2e4964ad | 1377 | #if !INLINE_ADD_PSYMBOL |
9342ecb9 JG |
1378 | |
1379 | /* Add a symbol with a long value to a psymtab. | |
1380 | Since one arg is a struct, we pass in a ptr and deref it (sigh). */ | |
1381 | ||
1382 | void | |
2e4964ad FF |
1383 | add_psymbol_to_list (name, namelength, namespace, class, list, val, language, |
1384 | objfile) | |
9342ecb9 JG |
1385 | char *name; |
1386 | int namelength; | |
1387 | enum namespace namespace; | |
1388 | enum address_class class; | |
1389 | struct psymbol_allocation_list *list; | |
1390 | long val; | |
2e4964ad FF |
1391 | enum language language; |
1392 | struct objfile *objfile; | |
9342ecb9 | 1393 | { |
2e4964ad FF |
1394 | register struct partial_symbol *psym; |
1395 | register char *demangled_name; | |
1396 | ||
1397 | if (list->next >= list->list + list->size) | |
1398 | { | |
1399 | extend_psymbol_list (list,objfile); | |
1400 | } | |
1401 | psym = list->next++; | |
1402 | ||
1403 | SYMBOL_NAME (psym) = | |
1404 | (char *) obstack_alloc (&objfile->psymbol_obstack, namelength + 1); | |
1405 | memcpy (SYMBOL_NAME (psym), name, namelength); | |
1406 | SYMBOL_NAME (psym)[namelength] = '\0'; | |
1407 | SYMBOL_VALUE (psym) = val; | |
1408 | SYMBOL_LANGUAGE (psym) = language; | |
1409 | PSYMBOL_NAMESPACE (psym) = namespace; | |
1410 | PSYMBOL_CLASS (psym) = class; | |
7532cf10 | 1411 | SYMBOL_INIT_DEMANGLED_NAME (psym, &objfile->psymbol_obstack); |
9342ecb9 JG |
1412 | } |
1413 | ||
1414 | /* Add a symbol with a CORE_ADDR value to a psymtab. */ | |
1415 | ||
1416 | void | |
2e4964ad FF |
1417 | add_psymbol_addr_to_list (name, namelength, namespace, class, list, val, |
1418 | language, objfile) | |
9342ecb9 JG |
1419 | char *name; |
1420 | int namelength; | |
1421 | enum namespace namespace; | |
1422 | enum address_class class; | |
1423 | struct psymbol_allocation_list *list; | |
1424 | CORE_ADDR val; | |
2e4964ad FF |
1425 | enum language language; |
1426 | struct objfile *objfile; | |
9342ecb9 | 1427 | { |
2e4964ad FF |
1428 | register struct partial_symbol *psym; |
1429 | register char *demangled_name; | |
1430 | ||
1431 | if (list->next >= list->list + list->size) | |
1432 | { | |
1433 | extend_psymbol_list (list,objfile); | |
1434 | } | |
1435 | psym = list->next++; | |
1436 | ||
1437 | SYMBOL_NAME (psym) = | |
1438 | (char *) obstack_alloc (&objfile->psymbol_obstack, namelength + 1); | |
1439 | memcpy (SYMBOL_NAME (psym), name, namelength); | |
1440 | SYMBOL_NAME (psym)[namelength] = '\0'; | |
1441 | SYMBOL_VALUE_ADDRESS (psym) = val; | |
1442 | SYMBOL_LANGUAGE (psym) = language; | |
1443 | PSYMBOL_NAMESPACE (psym) = namespace; | |
1444 | PSYMBOL_CLASS (psym) = class; | |
7532cf10 | 1445 | SYMBOL_INIT_DEMANGLED_NAME (psym, &objfile->psymbol_obstack); |
9342ecb9 | 1446 | } |
7d9884b9 | 1447 | |
2e4964ad FF |
1448 | #endif /* !INLINE_ADD_PSYMBOL */ |
1449 | ||
7d9884b9 | 1450 | \f |
bd5635a1 RP |
1451 | void |
1452 | _initialize_symfile () | |
1453 | { | |
ade40d31 RP |
1454 | struct cmd_list_element *c; |
1455 | ||
1456 | c = add_cmd ("symbol-file", class_files, symbol_file_command, | |
30875e1c | 1457 | "Load symbol table from executable file FILE.\n\ |
bd5635a1 | 1458 | The `file' command can also load symbol tables, as well as setting the file\n\ |
ade40d31 RP |
1459 | to execute.", &cmdlist); |
1460 | c->completer = filename_completer; | |
bd5635a1 | 1461 | |
ade40d31 | 1462 | c = add_cmd ("add-symbol-file", class_files, add_symbol_file_command, |
bd5635a1 | 1463 | "Load the symbols from FILE, assuming FILE has been dynamically loaded.\n\ |
ade40d31 RP |
1464 | The second argument provides the starting address of the file's text.", |
1465 | &cmdlist); | |
1466 | c->completer = filename_completer; | |
bd5635a1 | 1467 | |
ade40d31 | 1468 | c = add_cmd ("load", class_files, load_command, |
bd5635a1 | 1469 | "Dynamically load FILE into the running program, and record its symbols\n\ |
ade40d31 RP |
1470 | for access from GDB.", &cmdlist); |
1471 | c->completer = filename_completer; | |
bd5635a1 | 1472 | |
61a7292f SG |
1473 | add_show_from_set |
1474 | (add_set_cmd ("symbol-reloading", class_support, var_boolean, | |
1475 | (char *)&symbol_reloading, | |
1476 | "Set dynamic symbol table reloading multiple times in one run.", | |
1477 | &setlist), | |
1478 | &showlist); | |
1479 | ||
bd5635a1 | 1480 | } |