Commit | Line | Data |
---|---|---|
bd5635a1 RP |
1 | /* Generic symbol file reading for the GNU debugger, GDB. |
2 | Copyright 1990, 1991 Free Software Foundation, Inc. | |
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 RP |
20 | |
21 | #include <stdio.h> | |
22 | #include "defs.h" | |
23 | #include "symtab.h" | |
24 | #include "param.h" | |
25 | #include "gdbcore.h" | |
26 | #include "frame.h" | |
27 | #include "target.h" | |
28 | #include "value.h" | |
29 | #include "symfile.h" | |
30 | #include "gdbcmd.h" | |
31 | #include "breakpoint.h" | |
32 | ||
33 | #include <obstack.h> | |
34 | #include <assert.h> | |
35 | ||
36 | #include <sys/types.h> | |
37 | #include <fcntl.h> | |
38 | #include <string.h> | |
39 | #include <sys/stat.h> | |
40 | ||
d47d5315 JG |
41 | CORE_ADDR entry_point; /* Where execution starts in symfile */ |
42 | ||
bd5635a1 RP |
43 | extern int info_verbose; |
44 | ||
bd5635a1 RP |
45 | extern void qsort (); |
46 | extern char *getenv (); | |
d8ce1326 | 47 | extern char *rindex (); |
bd5635a1 | 48 | |
d47d5315 JG |
49 | extern CORE_ADDR startup_file_start; /* From blockframe.c */ |
50 | extern CORE_ADDR startup_file_end; /* From blockframe.c */ | |
51 | ||
bd5635a1 RP |
52 | /* Functions this file defines */ |
53 | static bfd *symfile_open(); | |
54 | static struct sym_fns *symfile_init(); | |
9d199712 | 55 | static void clear_symtab_users_once(); |
bd5635a1 RP |
56 | |
57 | /* List of all available sym_fns. */ | |
58 | ||
59 | struct sym_fns *symtab_fns = NULL; | |
60 | ||
61 | /* Saves the sym_fns of the current symbol table, so we can call | |
61a7292f SG |
62 | the right XXX_new_init function when we free it. FIXME. This |
63 | should be extended to calling the new_init function for each | |
64 | existing symtab or psymtab, since the main symbol file and | |
65 | subsequent added symbol files can have different types. */ | |
bd5635a1 RP |
66 | |
67 | static struct sym_fns *symfile_fns; | |
68 | ||
69 | /* Allocate an obstack to hold objects that should be freed | |
70 | when we load a new symbol table. | |
71 | This includes the symbols made by dbxread | |
72 | and the types that are not permanent. */ | |
73 | ||
74 | struct obstack obstack1; | |
75 | ||
76 | struct obstack *symbol_obstack = &obstack1; | |
77 | ||
78 | /* This obstack will be used for partial_symbol objects. It can | |
79 | probably actually be the same as the symbol_obstack above, but I'd | |
80 | like to keep them seperate for now. If I want to later, I'll | |
81 | replace one with the other. */ | |
82 | ||
83 | struct obstack obstack2; | |
84 | ||
85 | struct obstack *psymbol_obstack = &obstack2; | |
86 | ||
87 | /* File name symbols were loaded from. */ | |
88 | ||
89 | char *symfile = 0; | |
90 | ||
91 | /* The modification date of the file when they were loaded. */ | |
92 | ||
e1ce8aa5 | 93 | long /* really time_t */ symfile_mtime = 0; |
bd5635a1 RP |
94 | |
95 | /* Structures with which to manage partial symbol allocation. */ | |
96 | ||
d47d5315 | 97 | #include "param.h" |
bd5635a1 RP |
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 | /* Structure to manage complaints about symbol file contents. */ |
110 | ||
111 | struct complaint complaint_root[1] = { | |
112 | {(char *)0, 0, complaint_root}, | |
113 | }; | |
114 | ||
9d199712 JG |
115 | /* Some actual complaints. */ |
116 | ||
117 | struct complaint oldsyms_complaint = { | |
118 | "Replacing old symbols for `%s'", 0, 0 }; | |
119 | ||
120 | struct complaint empty_symtab_complaint = { | |
121 | "Empty symbol table found for `%s'", 0, 0 }; | |
122 | ||
bd5635a1 RP |
123 | \f |
124 | /* In the following sort, we always make sure that | |
125 | register debug symbol declarations always come before regular | |
126 | debug symbol declarations (as might happen when parameters are | |
127 | then put into registers by the compiler). */ | |
128 | ||
129 | static int | |
130 | compare_symbols (s1, s2) | |
131 | struct symbol **s1, **s2; | |
132 | { | |
133 | register int namediff; | |
134 | ||
135 | /* Compare the initial characters. */ | |
136 | namediff = SYMBOL_NAME (*s1)[0] - SYMBOL_NAME (*s2)[0]; | |
137 | if (namediff != 0) return namediff; | |
138 | ||
139 | /* If they match, compare the rest of the names. */ | |
140 | namediff = strcmp (SYMBOL_NAME (*s1), SYMBOL_NAME (*s2)); | |
141 | if (namediff != 0) return namediff; | |
142 | ||
143 | /* For symbols of the same name, registers should come first. */ | |
144 | return ((SYMBOL_CLASS (*s2) == LOC_REGISTER) | |
145 | - (SYMBOL_CLASS (*s1) == LOC_REGISTER)); | |
146 | } | |
147 | ||
148 | /* Call sort_block_syms to sort alphabetically the symbols of one block. */ | |
149 | ||
150 | void | |
151 | sort_block_syms (b) | |
152 | register struct block *b; | |
153 | { | |
154 | qsort (&BLOCK_SYM (b, 0), BLOCK_NSYMS (b), | |
155 | sizeof (struct symbol *), compare_symbols); | |
156 | } | |
157 | ||
158 | /* Call sort_symtab_syms to sort alphabetically | |
159 | the symbols of each block of one symtab. */ | |
160 | ||
161 | void | |
162 | sort_symtab_syms (s) | |
163 | register struct symtab *s; | |
164 | { | |
165 | register struct blockvector *bv = BLOCKVECTOR (s); | |
166 | int nbl = BLOCKVECTOR_NBLOCKS (bv); | |
167 | int i; | |
168 | register struct block *b; | |
169 | ||
170 | for (i = 0; i < nbl; i++) | |
171 | { | |
172 | b = BLOCKVECTOR_BLOCK (bv, i); | |
173 | if (BLOCK_SHOULD_SORT (b)) | |
174 | sort_block_syms (b); | |
175 | } | |
176 | } | |
177 | ||
178 | void | |
179 | sort_all_symtab_syms () | |
180 | { | |
181 | register struct symtab *s; | |
182 | ||
183 | for (s = symtab_list; s; s = s->next) | |
184 | { | |
185 | sort_symtab_syms (s); | |
186 | } | |
187 | } | |
188 | ||
189 | /* Make a copy of the string at PTR with SIZE characters in the symbol obstack | |
190 | (and add a null character at the end in the copy). | |
191 | Returns the address of the copy. */ | |
192 | ||
193 | char * | |
194 | obsavestring (ptr, size) | |
195 | char *ptr; | |
196 | int size; | |
197 | { | |
198 | register char *p = (char *) obstack_alloc (symbol_obstack, size + 1); | |
199 | /* Open-coded bcopy--saves function call time. | |
200 | These strings are usually short. */ | |
201 | { | |
202 | register char *p1 = ptr; | |
203 | register char *p2 = p; | |
204 | char *end = ptr + size; | |
205 | while (p1 != end) | |
206 | *p2++ = *p1++; | |
207 | } | |
208 | p[size] = 0; | |
209 | return p; | |
210 | } | |
211 | ||
212 | /* Concatenate strings S1, S2 and S3; return the new string. | |
213 | Space is found in the symbol_obstack. */ | |
214 | ||
215 | char * | |
216 | obconcat (s1, s2, s3) | |
217 | char *s1, *s2, *s3; | |
218 | { | |
219 | register int len = strlen (s1) + strlen (s2) + strlen (s3) + 1; | |
220 | register char *val = (char *) obstack_alloc (symbol_obstack, len); | |
221 | strcpy (val, s1); | |
222 | strcat (val, s2); | |
223 | strcat (val, s3); | |
224 | return val; | |
225 | } | |
226 | \f | |
227 | /* Accumulate the misc functions in bunches of 127. | |
228 | At the end, copy them all into one newly allocated structure. */ | |
229 | ||
230 | #define MISC_BUNCH_SIZE 127 | |
231 | ||
232 | struct misc_bunch | |
233 | { | |
234 | struct misc_bunch *next; | |
235 | struct misc_function contents[MISC_BUNCH_SIZE]; | |
236 | }; | |
237 | ||
238 | /* Bunch currently being filled up. | |
239 | The next field points to chain of filled bunches. */ | |
240 | ||
241 | static struct misc_bunch *misc_bunch; | |
242 | ||
243 | /* Number of slots filled in current bunch. */ | |
244 | ||
245 | static int misc_bunch_index; | |
246 | ||
247 | /* Total number of misc functions recorded so far. */ | |
248 | ||
249 | static int misc_count; | |
250 | ||
251 | void | |
252 | init_misc_bunches () | |
253 | { | |
254 | misc_count = 0; | |
255 | misc_bunch = 0; | |
256 | misc_bunch_index = MISC_BUNCH_SIZE; | |
257 | } | |
258 | ||
259 | void | |
260 | prim_record_misc_function (name, address, misc_type) | |
261 | char *name; | |
262 | CORE_ADDR address; | |
263 | enum misc_function_type misc_type; | |
264 | { | |
265 | register struct misc_bunch *new; | |
266 | ||
267 | if (misc_bunch_index == MISC_BUNCH_SIZE) | |
268 | { | |
269 | new = (struct misc_bunch *) xmalloc (sizeof (struct misc_bunch)); | |
270 | misc_bunch_index = 0; | |
271 | new->next = misc_bunch; | |
272 | misc_bunch = new; | |
273 | } | |
274 | misc_bunch->contents[misc_bunch_index].name = name; | |
275 | misc_bunch->contents[misc_bunch_index].address = address; | |
276 | misc_bunch->contents[misc_bunch_index].type = misc_type; | |
277 | misc_bunch->contents[misc_bunch_index].misc_info = 0; | |
278 | misc_bunch_index++; | |
279 | misc_count++; | |
280 | } | |
281 | ||
282 | static int | |
283 | compare_misc_functions (fn1, fn2) | |
284 | struct misc_function *fn1, *fn2; | |
285 | { | |
286 | /* Return a signed result based on unsigned comparisons | |
287 | so that we sort into unsigned numeric order. */ | |
288 | if (fn1->address < fn2->address) | |
289 | return -1; | |
290 | if (fn1->address > fn2->address) | |
291 | return 1; | |
292 | return 0; | |
293 | } | |
294 | ||
295 | /* ARGSUSED */ | |
296 | void | |
297 | discard_misc_bunches (foo) | |
298 | int foo; | |
299 | { | |
300 | register struct misc_bunch *next; | |
301 | ||
302 | while (misc_bunch) | |
303 | { | |
304 | next = misc_bunch->next; | |
305 | free (misc_bunch); | |
306 | misc_bunch = next; | |
307 | } | |
308 | } | |
309 | ||
310 | /* INCLINK nonzero means bunches are from an incrementally-linked file. | |
311 | Add them to the existing bunches. | |
312 | Otherwise INCLINK is zero, and we start from scratch. */ | |
313 | void | |
314 | condense_misc_bunches (inclink) | |
315 | int inclink; | |
316 | { | |
317 | register int i, j; | |
318 | register struct misc_bunch *bunch; | |
319 | ||
320 | if (inclink) | |
321 | { | |
322 | misc_function_vector | |
323 | = (struct misc_function *) | |
324 | xrealloc (misc_function_vector, (misc_count + misc_function_count) | |
325 | * sizeof (struct misc_function)); | |
326 | j = misc_function_count; | |
327 | } | |
328 | else | |
329 | { | |
330 | misc_function_vector | |
331 | = (struct misc_function *) | |
332 | xmalloc (misc_count * sizeof (struct misc_function)); | |
333 | j = 0; | |
334 | } | |
335 | ||
336 | bunch = misc_bunch; | |
337 | while (bunch) | |
338 | { | |
339 | for (i = 0; i < misc_bunch_index; i++, j++) | |
340 | { | |
341 | misc_function_vector[j] = bunch->contents[i]; | |
342 | #ifdef NAMES_HAVE_UNDERSCORE | |
343 | if (misc_function_vector[j].name[0] == '_') | |
344 | misc_function_vector[j].name++; | |
345 | #endif | |
d47d5315 JG |
346 | #ifdef SOME_NAMES_HAVE_DOT |
347 | if (misc_function_vector[j].name[0] == '.') | |
348 | misc_function_vector[j].name++; | |
349 | #endif | |
350 | ||
bd5635a1 RP |
351 | } |
352 | bunch = bunch->next; | |
353 | misc_bunch_index = MISC_BUNCH_SIZE; | |
354 | } | |
355 | ||
356 | if (misc_function_count + misc_count != j) /* DEBUG */ | |
357 | printf_filtered ("Function counts are off! %d + %d != %d\n", | |
358 | misc_function_count, misc_count, j); | |
359 | ||
360 | misc_function_count = j; | |
361 | ||
362 | /* Sort the misc functions by address. */ | |
363 | ||
364 | qsort (misc_function_vector, misc_function_count, | |
365 | sizeof (struct misc_function), | |
366 | compare_misc_functions); | |
367 | } | |
368 | ||
369 | ||
370 | /* Get the symbol table that corresponds to a partial_symtab. | |
371 | This is fast after the first time you do it. In fact, there | |
372 | is an even faster macro PSYMTAB_TO_SYMTAB that does the fast | |
373 | case inline. */ | |
374 | ||
375 | struct symtab * | |
376 | psymtab_to_symtab (pst) | |
377 | register struct partial_symtab *pst; | |
378 | { | |
bd5635a1 RP |
379 | /* If it's been looked up before, return it. */ |
380 | if (pst->symtab) | |
381 | return pst->symtab; | |
382 | ||
383 | /* If it has not yet been read in, read it. */ | |
384 | if (!pst->readin) | |
385 | { | |
386 | (*pst->read_symtab) (pst); | |
387 | } | |
388 | ||
61a7292f | 389 | return pst->symtab; |
bd5635a1 RP |
390 | } |
391 | ||
392 | /* Process a symbol file, as either the main file or as a dynamically | |
393 | loaded file. | |
394 | ||
b3fdaf3d JK |
395 | NAME is the file name (which will be tilde-expanded and made |
396 | absolute herein) (but we don't free or modify NAME itself). | |
397 | FROM_TTY says how verbose to be. MAINLINE specifies whether this | |
398 | is the main symbol file, or whether it's an extra symbol file such | |
399 | as dynamically loaded code. If !mainline, ADDR is the address | |
bd5635a1 RP |
400 | where the text segment was loaded. */ |
401 | ||
402 | void | |
d47d5315 JG |
403 | syms_from_bfd (sym_bfd, addr, mainline) |
404 | bfd *sym_bfd; | |
bd5635a1 RP |
405 | CORE_ADDR addr; |
406 | int mainline; | |
407 | { | |
bd5635a1 RP |
408 | asection *text_sect; |
409 | struct sym_fns *sf; | |
410 | char *realname; | |
411 | ||
bd5635a1 RP |
412 | /* There is a distinction between having no symbol table |
413 | (we refuse to read the file, leaving the old set of symbols around) | |
414 | and having no debugging symbols in your symbol table (we read | |
415 | the file and end up with a mostly empty symbol table). */ | |
416 | ||
417 | if (!(bfd_get_file_flags (sym_bfd) & HAS_SYMS)) | |
d47d5315 JG |
418 | return; |
419 | ||
420 | /* Save startup file's range of PC addresses to help blockframe.c | |
421 | decide where the bottom of the stack is. */ | |
422 | if (bfd_get_file_flags (sym_bfd) & EXEC_P) | |
bd5635a1 | 423 | { |
d47d5315 JG |
424 | /* Executable file -- record its entry point so we'll recognize |
425 | the startup file because it contains the entry point. */ | |
426 | entry_point = bfd_get_start_address (sym_bfd); | |
bd5635a1 | 427 | } |
d47d5315 | 428 | else |
bd5635a1 | 429 | { |
d47d5315 JG |
430 | /* Examination of non-executable.o files. Short-circuit this stuff. */ |
431 | /* ~0 will not be in any file, we hope. */ | |
432 | entry_point = ~0; | |
433 | /* set the startup file to be an empty range. */ | |
434 | startup_file_start = 0; | |
435 | startup_file_end = 0; | |
bd5635a1 RP |
436 | } |
437 | ||
438 | sf = symfile_init (sym_bfd); | |
439 | realname = bfd_get_filename (sym_bfd); | |
440 | realname = savestring (realname, strlen (realname)); | |
441 | /* FIXME, this probably creates a storage leak... */ | |
442 | ||
443 | if (mainline) | |
444 | { | |
445 | /* Since no error yet, throw away the old symbol table. */ | |
446 | ||
447 | if (symfile) | |
448 | free (symfile); | |
449 | symfile = 0; | |
450 | free_all_symtabs (); | |
451 | free_all_psymtabs (); | |
452 | ||
453 | (*sf->sym_new_init) (); | |
454 | ||
455 | /* For mainline, caller didn't know the specified address of the | |
456 | text section. We fix that here. */ | |
457 | text_sect = bfd_get_section_by_name (sym_bfd, ".text"); | |
458 | addr = bfd_section_vma (sym_bfd, text_sect); | |
459 | } | |
460 | ||
461 | clear_complaints(); /* Allow complaints to appear for this new file. */ | |
462 | ||
463 | (*sf->sym_read) (sf, addr, mainline); | |
464 | ||
465 | /* Don't allow char * to have a typename (else would get caddr_t.) */ | |
466 | /* Ditto void *. FIXME should do this for all the builtin types. */ | |
467 | ||
468 | TYPE_NAME (lookup_pointer_type (builtin_type_char)) = 0; | |
469 | TYPE_NAME (lookup_pointer_type (builtin_type_void)) = 0; | |
470 | ||
471 | if (mainline) | |
472 | { | |
473 | /* OK, make it the "real" symbol file. */ | |
474 | symfile = realname; | |
475 | symfile_fns = sf; | |
476 | } | |
477 | ||
0ef6f019 JG |
478 | /* If we have wiped out any old symbol tables, clean up. */ |
479 | clear_symtab_users_once (); | |
d47d5315 JG |
480 | } |
481 | ||
482 | ||
483 | /* Process a symbol file, as either the main file or as a dynamically | |
484 | loaded file. | |
485 | ||
486 | NAME is the file name (which will be tilde-expanded and made | |
487 | absolute herein) (but we don't free or modify NAME itself). | |
488 | FROM_TTY says how verbose to be. MAINLINE specifies whether this | |
489 | is the main symbol file, or whether it's an extra symbol file such | |
490 | as dynamically loaded code. If !mainline, ADDR is the address | |
491 | where the text segment was loaded. */ | |
492 | ||
493 | void | |
494 | symbol_file_add (name, from_tty, addr, mainline) | |
495 | char *name; | |
496 | int from_tty; | |
497 | CORE_ADDR addr; | |
498 | int mainline; | |
499 | { | |
500 | bfd *sym_bfd; | |
501 | ||
502 | sym_bfd = symfile_open (name); | |
503 | ||
504 | if (mainline) | |
505 | symfile_mtime = bfd_get_mtime (sym_bfd); | |
506 | ||
507 | /* There is a distinction between having no symbol table | |
508 | (we refuse to read the file, leaving the old set of symbols around) | |
509 | and having no debugging symbols in your symbol table (we read | |
510 | the file and end up with a mostly empty symbol table). */ | |
511 | ||
512 | if (!(bfd_get_file_flags (sym_bfd) & HAS_SYMS)) | |
513 | { | |
514 | error ("%s has no symbol-table", name); | |
515 | } | |
516 | ||
517 | if ((symtab_list || partial_symtab_list) | |
518 | && mainline | |
519 | && from_tty | |
520 | && !query ("Load new symbol table from \"%s\"? ", name)) | |
521 | error ("Not confirmed."); | |
522 | ||
523 | if (from_tty) | |
524 | { | |
525 | printf_filtered ("Reading symbols from %s...", name); | |
526 | wrap_here (""); | |
527 | fflush (stdout); | |
528 | } | |
529 | ||
530 | syms_from_bfd (sym_bfd, addr, mainline); | |
0ef6f019 | 531 | |
bd5635a1 RP |
532 | if (from_tty) |
533 | { | |
0ef6f019 | 534 | printf_filtered ("done.\n"); |
bd5635a1 RP |
535 | fflush (stdout); |
536 | } | |
537 | } | |
538 | ||
539 | /* This is the symbol-file command. Read the file, analyze its symbols, | |
540 | and add a struct symtab to symtab_list. */ | |
541 | ||
542 | void | |
543 | symbol_file_command (name, from_tty) | |
544 | char *name; | |
545 | int from_tty; | |
546 | { | |
547 | ||
548 | dont_repeat (); | |
549 | ||
550 | if (name == 0) | |
551 | { | |
552 | if ((symtab_list || partial_symtab_list) | |
553 | && from_tty | |
554 | && !query ("Discard symbol table from `%s'? ", symfile)) | |
555 | error ("Not confirmed."); | |
556 | if (symfile) | |
557 | free (symfile); | |
558 | symfile = 0; | |
559 | free_all_symtabs (); | |
560 | free_all_psymtabs (); | |
561 | /* FIXME, this does not account for the main file and subsequent | |
562 | files (shared libs, dynloads, etc) having different formats. | |
563 | It only calls the cleanup routine for the main file's format. */ | |
0ef6f019 JG |
564 | if (symfile_fns) { |
565 | (*symfile_fns->sym_new_init) (); | |
566 | free (symfile_fns); | |
567 | symfile_fns = 0; | |
568 | } | |
bd5635a1 RP |
569 | return; |
570 | } | |
571 | ||
2403f49b JK |
572 | /* Getting new symbols may change our opinion about what is |
573 | frameless. */ | |
574 | reinit_frame_cache (); | |
575 | ||
bd5635a1 RP |
576 | symbol_file_add (name, from_tty, (CORE_ADDR)0, 1); |
577 | } | |
578 | ||
579 | /* Open NAME and hand it off to BFD for preliminary analysis. Result | |
580 | is a BFD *, which includes a new copy of NAME dynamically allocated | |
581 | (which will be freed by the cleanup chain). In case of trouble, | |
582 | error() is called. */ | |
583 | ||
584 | static bfd * | |
585 | symfile_open (name) | |
586 | char *name; | |
587 | { | |
588 | bfd *sym_bfd; | |
589 | int desc; | |
590 | char *absolute_name; | |
591 | ||
592 | name = tilde_expand (name); | |
593 | make_cleanup (free, name); | |
594 | ||
595 | desc = openp (getenv ("PATH"), 1, name, O_RDONLY, 0, &absolute_name); | |
596 | if (desc < 0) | |
597 | perror_with_name (name); | |
598 | else | |
599 | { | |
600 | make_cleanup (free, absolute_name); | |
601 | name = absolute_name; | |
602 | } | |
603 | ||
604 | sym_bfd = bfd_fdopenr (name, NULL, desc); | |
605 | if (!sym_bfd) | |
606 | { | |
607 | close (desc); | |
608 | error ("Could not open `%s' to read symbols: %s", | |
609 | name, bfd_errmsg (bfd_error)); | |
610 | } | |
611 | make_cleanup (bfd_close, sym_bfd); | |
612 | ||
613 | if (!bfd_check_format (sym_bfd, bfd_object)) | |
614 | error ("\"%s\": can't read symbols: %s.", | |
615 | name, bfd_errmsg (bfd_error)); | |
616 | ||
617 | return sym_bfd; | |
618 | } | |
619 | ||
620 | /* Link a new symtab_fns into the global symtab_fns list. | |
621 | Called by various _initialize routines. */ | |
622 | ||
623 | void | |
624 | add_symtab_fns (sf) | |
625 | struct sym_fns *sf; | |
626 | { | |
627 | sf->next = symtab_fns; | |
628 | symtab_fns = sf; | |
629 | } | |
630 | ||
631 | ||
632 | /* Initialize to read symbols from the symbol file sym_bfd. It either | |
633 | returns or calls error(). The result is a malloc'd struct sym_fns | |
634 | that contains cached information about the symbol file. */ | |
635 | ||
636 | static struct sym_fns * | |
637 | symfile_init (sym_bfd) | |
638 | bfd *sym_bfd; | |
639 | { | |
640 | struct sym_fns *sf, *sf2; | |
641 | ||
642 | for (sf = symtab_fns; sf != NULL; sf = sf->next) | |
643 | { | |
644 | if (!strncmp (bfd_get_target (sym_bfd), sf->sym_name, sf->sym_namelen)) | |
645 | { | |
646 | sf2 = (struct sym_fns *)xmalloc (sizeof (*sf2)); | |
647 | /* FIXME, who frees this? */ | |
648 | *sf2 = *sf; | |
649 | sf2->sym_bfd = sym_bfd; | |
650 | sf2->sym_private = 0; /* Not alloc'd yet */ | |
651 | (*sf2->sym_init) (sf2); | |
652 | return sf2; | |
653 | } | |
654 | } | |
655 | error ("I'm sorry, Dave, I can't do that. Symbol format unknown."); | |
e1ce8aa5 | 656 | return 0; /* Appease lint. */ |
bd5635a1 RP |
657 | } |
658 | \f | |
659 | /* This function runs the load command of our current target. */ | |
660 | ||
661 | void | |
662 | load_command (arg, from_tty) | |
663 | char *arg; | |
664 | int from_tty; | |
665 | { | |
666 | target_load (arg, from_tty); | |
667 | } | |
668 | ||
61a7292f SG |
669 | /* This function allows the addition of incrementally linked object files. |
670 | It does not modify any state in the target, only in the debugger. */ | |
bd5635a1 | 671 | |
e1ce8aa5 | 672 | /* ARGSUSED */ |
bd5635a1 | 673 | void |
61a7292f SG |
674 | add_symbol_file_command (arg_string, from_tty) |
675 | char *arg_string; | |
bd5635a1 RP |
676 | int from_tty; |
677 | { | |
678 | char *name; | |
679 | CORE_ADDR text_addr; | |
680 | ||
61a7292f SG |
681 | /* Getting new symbols may change our opinion about what is |
682 | frameless. */ | |
683 | reinit_frame_cache (); | |
684 | ||
bd5635a1 | 685 | if (arg_string == 0) |
e74d7b43 | 686 | error ("add-symbol-file takes a file name and an address"); |
bd5635a1 RP |
687 | |
688 | arg_string = tilde_expand (arg_string); | |
689 | make_cleanup (free, arg_string); | |
690 | ||
691 | for( ; *arg_string == ' '; arg_string++ ); | |
692 | name = arg_string; | |
693 | for( ; *arg_string && *arg_string != ' ' ; arg_string++ ); | |
694 | *arg_string++ = (char) 0; | |
695 | ||
696 | if (name[0] == 0) | |
e74d7b43 | 697 | error ("add-symbol-file takes a file name and an address"); |
bd5635a1 RP |
698 | |
699 | text_addr = parse_and_eval_address (arg_string); | |
700 | ||
701 | dont_repeat (); | |
702 | ||
d8ce1326 JG |
703 | if (!query ("add symbol table from file \"%s\" at text_addr = %s?\n", |
704 | name, local_hex_string (text_addr))) | |
bd5635a1 RP |
705 | error ("Not confirmed."); |
706 | ||
707 | symbol_file_add (name, 0, text_addr, 0); | |
708 | } | |
709 | \f | |
710 | /* Re-read symbols if the symbol-file has changed. */ | |
711 | void | |
712 | reread_symbols () | |
713 | { | |
714 | struct stat symstat; | |
715 | ||
716 | /* With the addition of shared libraries, this should be modified, | |
717 | the load time should be saved in the partial symbol tables, since | |
718 | different tables may come from different source files. FIXME. | |
719 | This routine should then walk down each partial symbol table | |
720 | and see if the symbol table that it originates from has been changed | |
721 | */ | |
722 | ||
723 | if (stat (symfile, &symstat) < 0) | |
724 | /* Can't read symbol-file. Assume it is up to date. */ | |
725 | return; | |
726 | ||
727 | if (symstat.st_mtime > symfile_mtime) | |
728 | { | |
729 | printf_filtered ("Symbol file has changed; re-reading symbols.\n"); | |
730 | symbol_file_command (symfile, 0); | |
731 | breakpoint_re_set (); | |
732 | } | |
733 | } | |
734 | ||
bd5635a1 RP |
735 | /* This function is really horrible, but to avoid it, there would need |
736 | to be more filling in of forward references. */ | |
7cc43879 | 737 | void |
bd5635a1 RP |
738 | fill_in_vptr_fieldno (type) |
739 | struct type *type; | |
740 | { | |
bd5635a1 | 741 | if (TYPE_VPTR_FIELDNO (type) < 0) |
7cc43879 JK |
742 | { |
743 | int i; | |
744 | for (i = 1; i < TYPE_N_BASECLASSES (type); i++) | |
745 | { | |
746 | fill_in_vptr_fieldno (TYPE_BASECLASS (type, i)); | |
747 | if (TYPE_VPTR_FIELDNO (TYPE_BASECLASS (type, i)) >= 0) | |
748 | { | |
749 | TYPE_VPTR_FIELDNO (type) | |
750 | = TYPE_VPTR_FIELDNO (TYPE_BASECLASS (type, i)); | |
751 | TYPE_VPTR_BASETYPE (type) | |
752 | = TYPE_VPTR_BASETYPE (TYPE_BASECLASS (type, i)); | |
753 | break; | |
754 | } | |
755 | } | |
756 | } | |
bd5635a1 RP |
757 | } |
758 | \f | |
759 | /* Functions to handle complaints during symbol reading. */ | |
760 | ||
761 | /* How many complaints about a particular thing should be printed before | |
61a7292f SG |
762 | we stop whining about it? Default is no whining at all, since so many |
763 | systems have ill-constructed symbol files. */ | |
bd5635a1 | 764 | |
61a7292f | 765 | static unsigned stop_whining = 0; |
bd5635a1 RP |
766 | |
767 | /* Print a complaint about the input symbols, and link the complaint block | |
768 | into a chain for later handling. Result is 1 if the complaint was | |
769 | printed, 0 if it was suppressed. */ | |
770 | ||
771 | int | |
772 | complain (complaint, val) | |
773 | struct complaint *complaint; | |
774 | char *val; | |
775 | { | |
776 | complaint->counter++; | |
777 | if (complaint->next == 0) { | |
778 | complaint->next = complaint_root->next; | |
779 | complaint_root->next = complaint; | |
780 | } | |
781 | if (complaint->counter > stop_whining) | |
782 | return 0; | |
783 | wrap_here (""); | |
784 | if (!info_verbose) { | |
785 | puts_filtered ("During symbol reading..."); | |
786 | } | |
787 | printf_filtered (complaint->message, val); | |
788 | puts_filtered ("..."); | |
789 | wrap_here(""); | |
790 | if (!info_verbose) | |
791 | puts_filtered ("\n"); | |
792 | return 1; | |
793 | } | |
794 | ||
795 | /* Clear out all complaint counters that have ever been incremented. */ | |
796 | ||
797 | void | |
798 | clear_complaints () | |
799 | { | |
800 | struct complaint *p; | |
801 | ||
802 | for (p = complaint_root->next; p != complaint_root; p = p->next) | |
803 | p->counter = 0; | |
804 | } | |
805 | \f | |
d8ce1326 JG |
806 | /* allocate_symtab: |
807 | ||
808 | Allocate and partly initialize a new symbol table. Return a pointer | |
809 | to it. error() if no space. | |
810 | ||
811 | Caller must set these fields: | |
812 | LINETABLE(symtab) | |
813 | symtab->blockvector | |
d8ce1326 JG |
814 | symtab->dirname |
815 | symtab->free_code | |
816 | symtab->free_ptr | |
817 | initialize any EXTRA_SYMTAB_INFO | |
818 | possibly free_named_symtabs (symtab->filename); | |
819 | symtab->next = symtab_list; | |
820 | symtab_list = symtab; | |
821 | */ | |
822 | ||
823 | struct symtab * | |
824 | allocate_symtab(name) | |
825 | char *name; | |
826 | { | |
827 | register struct symtab *symtab; | |
828 | char *c; | |
829 | ||
830 | symtab = (struct symtab *) xmalloc (sizeof (struct symtab)); | |
831 | bzero (symtab, sizeof (*symtab)); | |
832 | symtab->filename = name; | |
833 | symtab->fullname = NULL; | |
834 | symtab->nlines = 0; | |
835 | symtab->line_charpos = 0; | |
836 | symtab->version = 0; | |
837 | symtab->language = language_unknown; /* default */ | |
838 | ||
839 | c = rindex (name, '.'); | |
840 | ||
841 | if (!c) { | |
842 | ; /* Don't know language of file. */ | |
843 | } else if(!strcmp(c,".mod")) { | |
844 | symtab->language = language_m2; | |
845 | } else if(!strcmp(c,".c") || !strcmp(c,".cc")) { | |
846 | symtab->language = language_c; | |
847 | } | |
848 | ||
849 | return symtab; | |
850 | } | |
851 | \f | |
9d199712 JG |
852 | /* clear_symtab_users_once: |
853 | ||
854 | This function is run after symbol reading, or from a cleanup. | |
855 | If an old symbol table was obsoleted, the old symbol table | |
856 | has been blown away, but the other GDB data structures that may | |
857 | reference it have not yet been cleared or re-directed. (The old | |
858 | symtab was zapped, and the cleanup queued, in free_named_symtab() | |
859 | below.) | |
860 | ||
861 | This function can be queued N times as a cleanup, or called | |
862 | directly; it will do all the work the first time, and then will be a | |
863 | no-op until the next time it is queued. This works by bumping a | |
864 | counter at queueing time. Much later when the cleanup is run, or at | |
865 | the end of symbol processing (in case the cleanup is discarded), if | |
866 | the queued count is greater than the "done-count", we do the work | |
867 | and set the done-count to the queued count. If the queued count is | |
868 | less than or equal to the done-count, we just ignore the call. This | |
869 | is needed because reading a single .o file will often replace many | |
870 | symtabs (one per .h file, for example), and we don't want to reset | |
871 | the breakpoints N times in the user's face. | |
872 | ||
873 | The reason we both queue a cleanup, and call it directly after symbol | |
874 | reading, is because the cleanup protects us in case of errors, but is | |
875 | discarded if symbol reading is successful. */ | |
876 | ||
877 | static int clear_symtab_users_queued; | |
878 | static int clear_symtab_users_done; | |
879 | ||
880 | static void | |
881 | clear_symtab_users_once () | |
882 | { | |
883 | /* Enforce once-per-`do_cleanups'-semantics */ | |
884 | if (clear_symtab_users_queued <= clear_symtab_users_done) | |
885 | return; | |
886 | clear_symtab_users_done = clear_symtab_users_queued; | |
887 | ||
888 | printf ("Resetting debugger state after updating old symbol tables\n"); | |
889 | ||
890 | /* Someday, we should do better than this, by only blowing away | |
891 | the things that really need to be blown. */ | |
892 | clear_value_history (); | |
893 | clear_displays (); | |
894 | clear_internalvars (); | |
895 | breakpoint_re_set (); | |
896 | set_default_breakpoint (0, 0, 0, 0); | |
897 | current_source_symtab = 0; | |
898 | } | |
899 | ||
900 | /* Delete the specified psymtab, and any others that reference it. */ | |
901 | ||
e1ce8aa5 | 902 | static void |
9d199712 JG |
903 | cashier_psymtab (pst) |
904 | struct partial_symtab *pst; | |
905 | { | |
906 | struct partial_symtab *ps, *pprev; | |
907 | int i; | |
908 | ||
909 | /* Find its previous psymtab in the chain */ | |
910 | for (ps = partial_symtab_list; ps; ps = ps->next) { | |
911 | if (ps == pst) | |
912 | break; | |
913 | pprev = ps; | |
914 | } | |
915 | ||
916 | if (ps) { | |
917 | /* Unhook it from the chain. */ | |
918 | if (ps == partial_symtab_list) | |
919 | partial_symtab_list = ps->next; | |
920 | else | |
921 | pprev->next = ps->next; | |
922 | ||
923 | /* FIXME, we can't conveniently deallocate the entries in the | |
924 | partial_symbol lists (global_psymbols/static_psymbols) that | |
925 | this psymtab points to. These just take up space until all | |
926 | the psymtabs are reclaimed. Ditto the dependencies list and | |
927 | filename, which are all in the psymbol_obstack. */ | |
928 | ||
929 | /* We need to cashier any psymtab that has this one as a dependency... */ | |
930 | again: | |
931 | for (ps = partial_symtab_list; ps; ps = ps->next) { | |
932 | for (i = 0; i < ps->number_of_dependencies; i++) { | |
933 | if (ps->dependencies[i] == pst) { | |
934 | cashier_psymtab (ps); | |
935 | goto again; /* Must restart, chain has been munged. */ | |
936 | } | |
937 | } | |
938 | } | |
939 | } | |
940 | } | |
941 | ||
942 | /* If a symtab or psymtab for filename NAME is found, free it along | |
943 | with any dependent breakpoints, displays, etc. | |
944 | Used when loading new versions of object modules with the "add-file" | |
945 | command. This is only called on the top-level symtab or psymtab's name; | |
946 | it is not called for subsidiary files such as .h files. | |
947 | ||
948 | Return value is 1 if we blew away the environment, 0 if not. | |
949 | ||
950 | FIXME. I think this is not the best way to do this. We should | |
951 | work on being gentler to the environment while still cleaning up | |
952 | all stray pointers into the freed symtab. */ | |
953 | ||
954 | int | |
955 | free_named_symtabs (name) | |
956 | char *name; | |
957 | { | |
958 | register struct symtab *s; | |
959 | register struct symtab *prev; | |
960 | register struct partial_symtab *ps; | |
9d199712 JG |
961 | struct blockvector *bv; |
962 | int blewit = 0; | |
963 | ||
61a7292f SG |
964 | /* We only wack things if the symbol-reload switch is set. */ |
965 | if (!symbol_reloading) | |
966 | return 0; | |
967 | ||
d11c44f1 JG |
968 | /* Some symbol formats have trouble providing file names... */ |
969 | if (name == 0 || *name == '\0') | |
970 | return 0; | |
971 | ||
9d199712 JG |
972 | /* Look for a psymtab with the specified name. */ |
973 | ||
974 | again2: | |
975 | for (ps = partial_symtab_list; ps; ps = ps->next) { | |
976 | if (!strcmp (name, ps->filename)) { | |
977 | cashier_psymtab (ps); /* Blow it away...and its little dog, too. */ | |
978 | goto again2; /* Must restart, chain has been munged */ | |
979 | } | |
980 | } | |
981 | ||
982 | /* Look for a symtab with the specified name. */ | |
983 | ||
984 | for (s = symtab_list; s; s = s->next) | |
985 | { | |
986 | if (!strcmp (name, s->filename)) | |
987 | break; | |
988 | prev = s; | |
989 | } | |
990 | ||
991 | if (s) | |
992 | { | |
993 | if (s == symtab_list) | |
994 | symtab_list = s->next; | |
995 | else | |
996 | prev->next = s->next; | |
997 | ||
998 | /* For now, queue a delete for all breakpoints, displays, etc., whether | |
999 | or not they depend on the symtab being freed. This should be | |
1000 | changed so that only those data structures affected are deleted. */ | |
1001 | ||
1002 | /* But don't delete anything if the symtab is empty. | |
1003 | This test is necessary due to a bug in "dbxread.c" that | |
1004 | causes empty symtabs to be created for N_SO symbols that | |
1005 | contain the pathname of the object file. (This problem | |
1006 | has been fixed in GDB 3.9x). */ | |
1007 | ||
1008 | bv = BLOCKLIST (s); | |
1009 | if (BLOCKLIST_NBLOCKS (bv) > 2 | |
1010 | || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK)) | |
1011 | || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK))) | |
1012 | { | |
1013 | complain (&oldsyms_complaint, name); | |
1014 | ||
1015 | clear_symtab_users_queued++; | |
1016 | make_cleanup (clear_symtab_users_once, 0); | |
1017 | blewit = 1; | |
1018 | } else { | |
1019 | complain (&empty_symtab_complaint, name); | |
1020 | } | |
1021 | ||
1022 | free_symtab (s); | |
1023 | } | |
1024 | else | |
d8ce1326 JG |
1025 | { |
1026 | /* It is still possible that some breakpoints will be affected | |
1027 | even though no symtab was found, since the file might have | |
1028 | been compiled without debugging, and hence not be associated | |
1029 | with a symtab. In order to handle this correctly, we would need | |
1030 | to keep a list of text address ranges for undebuggable files. | |
1031 | For now, we do nothing, since this is a fairly obscure case. */ | |
1032 | ; | |
1033 | } | |
9d199712 JG |
1034 | |
1035 | /* FIXME, what about the misc function vector? */ | |
1036 | return blewit; | |
1037 | } | |
1038 | \f | |
bd5635a1 RP |
1039 | void |
1040 | _initialize_symfile () | |
1041 | { | |
1042 | ||
1043 | add_com ("symbol-file", class_files, symbol_file_command, | |
1044 | "Load symbol table from executable file FILE.\n\ | |
1045 | The `file' command can also load symbol tables, as well as setting the file\n\ | |
1046 | to execute."); | |
1047 | ||
e74d7b43 | 1048 | add_com ("add-symbol-file", class_files, add_symbol_file_command, |
bd5635a1 RP |
1049 | "Load the symbols from FILE, assuming FILE has been dynamically loaded.\n\ |
1050 | The second argument provides the starting address of the file's text."); | |
1051 | ||
1052 | add_com ("load", class_files, load_command, | |
1053 | "Dynamically load FILE into the running program, and record its symbols\n\ | |
1054 | for access from GDB."); | |
1055 | ||
1056 | add_show_from_set | |
1057 | (add_set_cmd ("complaints", class_support, var_uinteger, | |
1058 | (char *)&stop_whining, | |
1059 | "Set max number of complaints about incorrect symbols.", | |
1060 | &setlist), | |
1061 | &showlist); | |
1062 | ||
61a7292f SG |
1063 | add_show_from_set |
1064 | (add_set_cmd ("symbol-reloading", class_support, var_boolean, | |
1065 | (char *)&symbol_reloading, | |
1066 | "Set dynamic symbol table reloading multiple times in one run.", | |
1067 | &setlist), | |
1068 | &showlist); | |
1069 | ||
bd5635a1 RP |
1070 | obstack_init (symbol_obstack); |
1071 | obstack_init (psymbol_obstack); | |
1072 | } |