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