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