Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Support routines for building symbol tables in GDB's internal format. |
b6ba6518 | 2 | Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, |
d7f0b9ce | 3 | 1996, 1997, 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc. |
c906108c | 4 | |
c5aa993b | 5 | This file is part of GDB. |
c906108c | 6 | |
c5aa993b JM |
7 | This program is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
c906108c | 11 | |
c5aa993b JM |
12 | This program is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
c906108c | 16 | |
c5aa993b JM |
17 | You should have received a copy of the GNU General Public License |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 59 Temple Place - Suite 330, | |
20 | Boston, MA 02111-1307, USA. */ | |
c906108c SS |
21 | |
22 | /* This module provides subroutines used for creating and adding to | |
23 | the symbol table. These routines are called from various symbol- | |
24 | file-reading routines. | |
25 | ||
26 | Routines to support specific debugging information formats (stabs, | |
27 | DWARF, etc) belong somewhere else. */ | |
28 | ||
29 | #include "defs.h" | |
30 | #include "bfd.h" | |
04ea0df1 | 31 | #include "gdb_obstack.h" |
c906108c SS |
32 | #include "symtab.h" |
33 | #include "symfile.h" /* Needed for "struct complaint" */ | |
34 | #include "objfiles.h" | |
35 | #include "gdbtypes.h" | |
36 | #include "complaints.h" | |
37 | #include "gdb_string.h" | |
91b9ff21 | 38 | #include "expression.h" /* For "enum exp_opcode" used by... */ |
14a5e767 | 39 | #include "language.h" /* For "local_hex_string" */ |
357e46e7 | 40 | #include "bcache.h" |
d5166ae1 | 41 | #include "filenames.h" /* For DOSish file names */ |
99d9066e | 42 | #include "macrotab.h" |
261397f8 | 43 | #include "demangle.h" /* Needed by SYMBOL_INIT_DEMANGLED_NAME. */ |
c906108c | 44 | /* Ask buildsym.h to define the vars it normally declares `extern'. */ |
c5aa993b JM |
45 | #define EXTERN |
46 | /**/ | |
c906108c SS |
47 | #include "buildsym.h" /* Our own declarations */ |
48 | #undef EXTERN | |
49 | ||
50 | /* For cleanup_undefined_types and finish_global_stabs (somewhat | |
51 | questionable--see comment where we call them). */ | |
52 | ||
53 | #include "stabsread.h" | |
54 | ||
55 | /* List of free `struct pending' structures for reuse. */ | |
56 | ||
57 | static struct pending *free_pendings; | |
58 | ||
59 | /* Non-zero if symtab has line number info. This prevents an | |
60 | otherwise empty symtab from being tossed. */ | |
61 | ||
62 | static int have_line_numbers; | |
63 | \f | |
64 | static int compare_line_numbers (const void *ln1p, const void *ln2p); | |
65 | \f | |
66 | ||
67 | /* Initial sizes of data structures. These are realloc'd larger if | |
68 | needed, and realloc'd down to the size actually used, when | |
69 | completed. */ | |
70 | ||
71 | #define INITIAL_CONTEXT_STACK_SIZE 10 | |
72 | #define INITIAL_LINE_VECTOR_LENGTH 1000 | |
73 | \f | |
74 | ||
75 | /* Complaints about the symbols we have encountered. */ | |
76 | ||
77 | struct complaint block_end_complaint = | |
78 | {"block end address less than block start address in %s (patched it)", 0, 0}; | |
79 | ||
80 | struct complaint anon_block_end_complaint = | |
81 | {"block end address 0x%lx less than block start address 0x%lx (patched it)", 0, 0}; | |
82 | ||
83 | struct complaint innerblock_complaint = | |
84 | {"inner block not inside outer block in %s", 0, 0}; | |
85 | ||
86 | struct complaint innerblock_anon_complaint = | |
87 | {"inner block (0x%lx-0x%lx) not inside outer block (0x%lx-0x%lx)", 0, 0}; | |
88 | ||
89 | struct complaint blockvector_complaint = | |
59527da0 | 90 | {"block at %s out of order", 0, 0}; |
c906108c SS |
91 | \f |
92 | /* maintain the lists of symbols and blocks */ | |
93 | ||
59527da0 JB |
94 | /* Add a pending list to free_pendings. */ |
95 | void | |
96 | add_free_pendings (struct pending *list) | |
97 | { | |
98 | register struct pending *link = list; | |
99 | ||
100 | if (list) | |
101 | { | |
102 | while (link->next) link = link->next; | |
103 | link->next = free_pendings; | |
104 | free_pendings = list; | |
105 | } | |
106 | } | |
107 | ||
c906108c SS |
108 | /* Add a symbol to one of the lists of symbols. */ |
109 | ||
110 | void | |
111 | add_symbol_to_list (struct symbol *symbol, struct pending **listhead) | |
112 | { | |
113 | register struct pending *link; | |
114 | ||
115 | /* If this is an alias for another symbol, don't add it. */ | |
116 | if (symbol->ginfo.name && symbol->ginfo.name[0] == '#') | |
117 | return; | |
118 | ||
119 | /* We keep PENDINGSIZE symbols in each link of the list. If we | |
120 | don't have a link with room in it, add a new link. */ | |
121 | if (*listhead == NULL || (*listhead)->nsyms == PENDINGSIZE) | |
122 | { | |
123 | if (free_pendings) | |
124 | { | |
125 | link = free_pendings; | |
126 | free_pendings = link->next; | |
127 | } | |
128 | else | |
129 | { | |
130 | link = (struct pending *) xmalloc (sizeof (struct pending)); | |
131 | } | |
132 | ||
133 | link->next = *listhead; | |
134 | *listhead = link; | |
135 | link->nsyms = 0; | |
136 | } | |
137 | ||
138 | (*listhead)->symbol[(*listhead)->nsyms++] = symbol; | |
139 | } | |
140 | ||
141 | /* Find a symbol named NAME on a LIST. NAME need not be | |
142 | '\0'-terminated; LENGTH is the length of the name. */ | |
143 | ||
144 | struct symbol * | |
145 | find_symbol_in_list (struct pending *list, char *name, int length) | |
146 | { | |
147 | int j; | |
148 | char *pp; | |
149 | ||
150 | while (list != NULL) | |
151 | { | |
152 | for (j = list->nsyms; --j >= 0;) | |
153 | { | |
154 | pp = SYMBOL_NAME (list->symbol[j]); | |
155 | if (*pp == *name && strncmp (pp, name, length) == 0 && | |
156 | pp[length] == '\0') | |
157 | { | |
158 | return (list->symbol[j]); | |
159 | } | |
160 | } | |
161 | list = list->next; | |
162 | } | |
163 | return (NULL); | |
164 | } | |
165 | ||
166 | /* At end of reading syms, or in case of quit, really free as many | |
167 | `struct pending's as we can easily find. */ | |
168 | ||
169 | /* ARGSUSED */ | |
170 | void | |
a0b3c4fd | 171 | really_free_pendings (PTR dummy) |
c906108c SS |
172 | { |
173 | struct pending *next, *next1; | |
174 | ||
175 | for (next = free_pendings; next; next = next1) | |
176 | { | |
177 | next1 = next->next; | |
b8c9b27d | 178 | xfree ((void *) next); |
c906108c SS |
179 | } |
180 | free_pendings = NULL; | |
181 | ||
182 | free_pending_blocks (); | |
183 | ||
184 | for (next = file_symbols; next != NULL; next = next1) | |
185 | { | |
186 | next1 = next->next; | |
b8c9b27d | 187 | xfree ((void *) next); |
c906108c SS |
188 | } |
189 | file_symbols = NULL; | |
190 | ||
191 | for (next = global_symbols; next != NULL; next = next1) | |
192 | { | |
193 | next1 = next->next; | |
b8c9b27d | 194 | xfree ((void *) next); |
c906108c SS |
195 | } |
196 | global_symbols = NULL; | |
99d9066e JB |
197 | |
198 | if (pending_macros) | |
199 | free_macro_table (pending_macros); | |
c906108c SS |
200 | } |
201 | ||
202 | /* This function is called to discard any pending blocks. */ | |
203 | ||
204 | void | |
205 | free_pending_blocks (void) | |
206 | { | |
207 | #if 0 /* Now we make the links in the | |
208 | symbol_obstack, so don't free | |
209 | them. */ | |
210 | struct pending_block *bnext, *bnext1; | |
211 | ||
212 | for (bnext = pending_blocks; bnext; bnext = bnext1) | |
213 | { | |
214 | bnext1 = bnext->next; | |
b8c9b27d | 215 | xfree ((void *) bnext); |
c906108c SS |
216 | } |
217 | #endif | |
218 | pending_blocks = NULL; | |
219 | } | |
220 | ||
221 | /* Take one of the lists of symbols and make a block from it. Keep | |
222 | the order the symbols have in the list (reversed from the input | |
223 | file). Put the block on the list of pending blocks. */ | |
224 | ||
225 | void | |
226 | finish_block (struct symbol *symbol, struct pending **listhead, | |
227 | struct pending_block *old_blocks, | |
228 | CORE_ADDR start, CORE_ADDR end, | |
229 | struct objfile *objfile) | |
230 | { | |
231 | register struct pending *next, *next1; | |
232 | register struct block *block; | |
233 | register struct pending_block *pblock; | |
234 | struct pending_block *opblock; | |
235 | register int i; | |
236 | register int j; | |
237 | ||
238 | /* Count the length of the list of symbols. */ | |
239 | ||
240 | for (next = *listhead, i = 0; | |
241 | next; | |
242 | i += next->nsyms, next = next->next) | |
243 | { | |
244 | /* EMPTY */ ; | |
245 | } | |
246 | ||
c906108c SS |
247 | /* Copy the symbols into the block. */ |
248 | ||
261397f8 DJ |
249 | if (symbol) |
250 | { | |
251 | block = (struct block *) | |
252 | obstack_alloc (&objfile->symbol_obstack, | |
253 | (sizeof (struct block) + | |
254 | ((i - 1) * sizeof (struct symbol *)))); | |
255 | BLOCK_NSYMS (block) = i; | |
256 | for (next = *listhead; next; next = next->next) | |
257 | for (j = next->nsyms - 1; j >= 0; j--) | |
258 | { | |
259 | BLOCK_SYM (block, --i) = next->symbol[j]; | |
260 | } | |
261 | } | |
262 | else | |
c906108c | 263 | { |
261397f8 DJ |
264 | int htab_size = BLOCK_HASHTABLE_SIZE (i); |
265 | ||
266 | block = (struct block *) | |
267 | obstack_alloc (&objfile->symbol_obstack, | |
268 | (sizeof (struct block) + | |
269 | ((htab_size - 1) * sizeof (struct symbol *)))); | |
270 | for (j = 0; j < htab_size; j++) | |
271 | { | |
272 | BLOCK_BUCKET (block, j) = 0; | |
273 | } | |
274 | BLOCK_BUCKETS (block) = htab_size; | |
275 | for (next = *listhead; next; next = next->next) | |
c906108c | 276 | { |
261397f8 DJ |
277 | for (j = next->nsyms - 1; j >= 0; j--) |
278 | { | |
279 | struct symbol *sym; | |
280 | unsigned int hash_index; | |
281 | const char *name = SYMBOL_DEMANGLED_NAME (next->symbol[j]); | |
282 | if (name == NULL) | |
283 | name = SYMBOL_NAME (next->symbol[j]); | |
284 | hash_index = msymbol_hash_iw (name); | |
285 | hash_index = hash_index % BLOCK_BUCKETS (block); | |
286 | sym = BLOCK_BUCKET (block, hash_index); | |
287 | BLOCK_BUCKET (block, hash_index) = next->symbol[j]; | |
288 | next->symbol[j]->hash_next = sym; | |
289 | } | |
c906108c SS |
290 | } |
291 | } | |
292 | ||
293 | BLOCK_START (block) = start; | |
294 | BLOCK_END (block) = end; | |
295 | /* Superblock filled in when containing block is made */ | |
296 | BLOCK_SUPERBLOCK (block) = NULL; | |
297 | ||
298 | BLOCK_GCC_COMPILED (block) = processing_gcc_compilation; | |
299 | ||
300 | /* Put the block in as the value of the symbol that names it. */ | |
301 | ||
302 | if (symbol) | |
303 | { | |
304 | struct type *ftype = SYMBOL_TYPE (symbol); | |
305 | SYMBOL_BLOCK_VALUE (symbol) = block; | |
306 | BLOCK_FUNCTION (block) = symbol; | |
261397f8 | 307 | BLOCK_HASHTABLE (block) = 0; |
c906108c SS |
308 | |
309 | if (TYPE_NFIELDS (ftype) <= 0) | |
310 | { | |
311 | /* No parameter type information is recorded with the | |
312 | function's type. Set that from the type of the | |
313 | parameter symbols. */ | |
314 | int nparams = 0, iparams; | |
315 | struct symbol *sym; | |
e88c90f2 | 316 | ALL_BLOCK_SYMBOLS (block, i, sym) |
c906108c | 317 | { |
c906108c SS |
318 | switch (SYMBOL_CLASS (sym)) |
319 | { | |
320 | case LOC_ARG: | |
321 | case LOC_REF_ARG: | |
322 | case LOC_REGPARM: | |
323 | case LOC_REGPARM_ADDR: | |
324 | case LOC_BASEREG_ARG: | |
325 | case LOC_LOCAL_ARG: | |
326 | nparams++; | |
327 | break; | |
328 | case LOC_UNDEF: | |
329 | case LOC_CONST: | |
330 | case LOC_STATIC: | |
331 | case LOC_INDIRECT: | |
332 | case LOC_REGISTER: | |
333 | case LOC_LOCAL: | |
334 | case LOC_TYPEDEF: | |
335 | case LOC_LABEL: | |
336 | case LOC_BLOCK: | |
337 | case LOC_CONST_BYTES: | |
338 | case LOC_BASEREG: | |
339 | case LOC_UNRESOLVED: | |
340 | case LOC_OPTIMIZED_OUT: | |
341 | default: | |
342 | break; | |
343 | } | |
344 | } | |
345 | if (nparams > 0) | |
346 | { | |
347 | TYPE_NFIELDS (ftype) = nparams; | |
348 | TYPE_FIELDS (ftype) = (struct field *) | |
349 | TYPE_ALLOC (ftype, nparams * sizeof (struct field)); | |
350 | ||
351 | for (i = iparams = 0; iparams < nparams; i++) | |
352 | { | |
353 | sym = BLOCK_SYM (block, i); | |
354 | switch (SYMBOL_CLASS (sym)) | |
355 | { | |
356 | case LOC_ARG: | |
357 | case LOC_REF_ARG: | |
358 | case LOC_REGPARM: | |
359 | case LOC_REGPARM_ADDR: | |
360 | case LOC_BASEREG_ARG: | |
361 | case LOC_LOCAL_ARG: | |
362 | TYPE_FIELD_TYPE (ftype, iparams) = SYMBOL_TYPE (sym); | |
8176bb6d | 363 | TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0; |
c906108c SS |
364 | iparams++; |
365 | break; | |
366 | case LOC_UNDEF: | |
367 | case LOC_CONST: | |
368 | case LOC_STATIC: | |
369 | case LOC_INDIRECT: | |
370 | case LOC_REGISTER: | |
371 | case LOC_LOCAL: | |
372 | case LOC_TYPEDEF: | |
373 | case LOC_LABEL: | |
374 | case LOC_BLOCK: | |
375 | case LOC_CONST_BYTES: | |
376 | case LOC_BASEREG: | |
377 | case LOC_UNRESOLVED: | |
378 | case LOC_OPTIMIZED_OUT: | |
379 | default: | |
380 | break; | |
381 | } | |
382 | } | |
383 | } | |
384 | } | |
385 | } | |
386 | else | |
387 | { | |
388 | BLOCK_FUNCTION (block) = NULL; | |
261397f8 | 389 | BLOCK_HASHTABLE (block) = 1; |
c906108c SS |
390 | } |
391 | ||
392 | /* Now "free" the links of the list, and empty the list. */ | |
393 | ||
394 | for (next = *listhead; next; next = next1) | |
395 | { | |
396 | next1 = next->next; | |
397 | next->next = free_pendings; | |
398 | free_pendings = next; | |
399 | } | |
400 | *listhead = NULL; | |
401 | ||
402 | #if 1 | |
403 | /* Check to be sure that the blocks have an end address that is | |
404 | greater than starting address */ | |
405 | ||
406 | if (BLOCK_END (block) < BLOCK_START (block)) | |
407 | { | |
408 | if (symbol) | |
409 | { | |
410 | complain (&block_end_complaint, SYMBOL_SOURCE_NAME (symbol)); | |
411 | } | |
412 | else | |
413 | { | |
414 | complain (&anon_block_end_complaint, BLOCK_END (block), BLOCK_START (block)); | |
415 | } | |
416 | /* Better than nothing */ | |
417 | BLOCK_END (block) = BLOCK_START (block); | |
418 | } | |
419 | #endif | |
420 | ||
421 | /* Install this block as the superblock of all blocks made since the | |
422 | start of this scope that don't have superblocks yet. */ | |
423 | ||
424 | opblock = NULL; | |
425 | for (pblock = pending_blocks; pblock != old_blocks; pblock = pblock->next) | |
426 | { | |
427 | if (BLOCK_SUPERBLOCK (pblock->block) == NULL) | |
428 | { | |
429 | #if 1 | |
430 | /* Check to be sure the blocks are nested as we receive | |
431 | them. If the compiler/assembler/linker work, this just | |
432 | burns a small amount of time. */ | |
433 | if (BLOCK_START (pblock->block) < BLOCK_START (block) || | |
434 | BLOCK_END (pblock->block) > BLOCK_END (block)) | |
435 | { | |
436 | if (symbol) | |
437 | { | |
438 | complain (&innerblock_complaint, | |
439 | SYMBOL_SOURCE_NAME (symbol)); | |
440 | } | |
441 | else | |
442 | { | |
443 | complain (&innerblock_anon_complaint, BLOCK_START (pblock->block), | |
c5aa993b | 444 | BLOCK_END (pblock->block), BLOCK_START (block), |
c906108c SS |
445 | BLOCK_END (block)); |
446 | } | |
447 | if (BLOCK_START (pblock->block) < BLOCK_START (block)) | |
448 | BLOCK_START (pblock->block) = BLOCK_START (block); | |
449 | if (BLOCK_END (pblock->block) > BLOCK_END (block)) | |
450 | BLOCK_END (pblock->block) = BLOCK_END (block); | |
451 | } | |
452 | #endif | |
453 | BLOCK_SUPERBLOCK (pblock->block) = block; | |
454 | } | |
455 | opblock = pblock; | |
456 | } | |
457 | ||
458 | record_pending_block (objfile, block, opblock); | |
459 | } | |
460 | ||
461 | /* Record BLOCK on the list of all blocks in the file. Put it after | |
462 | OPBLOCK, or at the beginning if opblock is NULL. This puts the | |
463 | block in the list after all its subblocks. | |
464 | ||
465 | Allocate the pending block struct in the symbol_obstack to save | |
466 | time. This wastes a little space. FIXME: Is it worth it? */ | |
467 | ||
468 | void | |
469 | record_pending_block (struct objfile *objfile, struct block *block, | |
470 | struct pending_block *opblock) | |
471 | { | |
472 | register struct pending_block *pblock; | |
473 | ||
474 | pblock = (struct pending_block *) | |
475 | obstack_alloc (&objfile->symbol_obstack, sizeof (struct pending_block)); | |
476 | pblock->block = block; | |
477 | if (opblock) | |
478 | { | |
479 | pblock->next = opblock->next; | |
480 | opblock->next = pblock; | |
481 | } | |
482 | else | |
483 | { | |
484 | pblock->next = pending_blocks; | |
485 | pending_blocks = pblock; | |
486 | } | |
487 | } | |
488 | ||
822e978b AC |
489 | /* OBSOLETE Note that this is only used in this file and in dstread.c, which */ |
490 | /* OBSOLETE should be fixed to not need direct access to this function. When */ | |
491 | /* OBSOLETE that is done, it can be made static again. */ | |
c906108c | 492 | |
822e978b | 493 | static struct blockvector * |
c906108c SS |
494 | make_blockvector (struct objfile *objfile) |
495 | { | |
496 | register struct pending_block *next; | |
497 | register struct blockvector *blockvector; | |
498 | register int i; | |
499 | ||
500 | /* Count the length of the list of blocks. */ | |
501 | ||
502 | for (next = pending_blocks, i = 0; next; next = next->next, i++) | |
503 | {; | |
504 | } | |
505 | ||
506 | blockvector = (struct blockvector *) | |
507 | obstack_alloc (&objfile->symbol_obstack, | |
508 | (sizeof (struct blockvector) | |
509 | + (i - 1) * sizeof (struct block *))); | |
510 | ||
511 | /* Copy the blocks into the blockvector. This is done in reverse | |
512 | order, which happens to put the blocks into the proper order | |
513 | (ascending starting address). finish_block has hair to insert | |
514 | each block into the list after its subblocks in order to make | |
515 | sure this is true. */ | |
516 | ||
517 | BLOCKVECTOR_NBLOCKS (blockvector) = i; | |
518 | for (next = pending_blocks; next; next = next->next) | |
519 | { | |
520 | BLOCKVECTOR_BLOCK (blockvector, --i) = next->block; | |
521 | } | |
522 | ||
523 | #if 0 /* Now we make the links in the | |
524 | obstack, so don't free them. */ | |
525 | /* Now free the links of the list, and empty the list. */ | |
526 | ||
527 | for (next = pending_blocks; next; next = next1) | |
528 | { | |
529 | next1 = next->next; | |
b8c9b27d | 530 | xfree (next); |
c906108c SS |
531 | } |
532 | #endif | |
533 | pending_blocks = NULL; | |
534 | ||
535 | #if 1 /* FIXME, shut this off after a while | |
536 | to speed up symbol reading. */ | |
537 | /* Some compilers output blocks in the wrong order, but we depend on | |
538 | their being in the right order so we can binary search. Check the | |
539 | order and moan about it. FIXME. */ | |
540 | if (BLOCKVECTOR_NBLOCKS (blockvector) > 1) | |
541 | { | |
542 | for (i = 1; i < BLOCKVECTOR_NBLOCKS (blockvector); i++) | |
543 | { | |
544 | if (BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i - 1)) | |
545 | > BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i))) | |
546 | { | |
59527da0 JB |
547 | CORE_ADDR start |
548 | = BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i)); | |
c906108c SS |
549 | |
550 | complain (&blockvector_complaint, | |
14a5e767 | 551 | local_hex_string ((LONGEST) start)); |
c906108c SS |
552 | } |
553 | } | |
554 | } | |
555 | #endif | |
556 | ||
557 | return (blockvector); | |
558 | } | |
559 | \f | |
560 | /* Start recording information about source code that came from an | |
561 | included (or otherwise merged-in) source file with a different | |
562 | name. NAME is the name of the file (cannot be NULL), DIRNAME is | |
563 | the directory in which it resides (or NULL if not known). */ | |
564 | ||
565 | void | |
566 | start_subfile (char *name, char *dirname) | |
567 | { | |
568 | register struct subfile *subfile; | |
569 | ||
570 | /* See if this subfile is already known as a subfile of the current | |
571 | main source file. */ | |
572 | ||
573 | for (subfile = subfiles; subfile; subfile = subfile->next) | |
574 | { | |
d5166ae1 | 575 | if (FILENAME_CMP (subfile->name, name) == 0) |
c906108c SS |
576 | { |
577 | current_subfile = subfile; | |
578 | return; | |
579 | } | |
580 | } | |
581 | ||
582 | /* This subfile is not known. Add an entry for it. Make an entry | |
583 | for this subfile in the list of all subfiles of the current main | |
584 | source file. */ | |
585 | ||
586 | subfile = (struct subfile *) xmalloc (sizeof (struct subfile)); | |
59527da0 | 587 | memset ((char *) subfile, 0, sizeof (struct subfile)); |
c906108c SS |
588 | subfile->next = subfiles; |
589 | subfiles = subfile; | |
590 | current_subfile = subfile; | |
591 | ||
592 | /* Save its name and compilation directory name */ | |
593 | subfile->name = (name == NULL) ? NULL : savestring (name, strlen (name)); | |
594 | subfile->dirname = | |
595 | (dirname == NULL) ? NULL : savestring (dirname, strlen (dirname)); | |
596 | ||
597 | /* Initialize line-number recording for this subfile. */ | |
598 | subfile->line_vector = NULL; | |
599 | ||
600 | /* Default the source language to whatever can be deduced from the | |
601 | filename. If nothing can be deduced (such as for a C/C++ include | |
602 | file with a ".h" extension), then inherit whatever language the | |
603 | previous subfile had. This kludgery is necessary because there | |
604 | is no standard way in some object formats to record the source | |
605 | language. Also, when symtabs are allocated we try to deduce a | |
606 | language then as well, but it is too late for us to use that | |
607 | information while reading symbols, since symtabs aren't allocated | |
608 | until after all the symbols have been processed for a given | |
609 | source file. */ | |
610 | ||
611 | subfile->language = deduce_language_from_filename (subfile->name); | |
612 | if (subfile->language == language_unknown && | |
613 | subfile->next != NULL) | |
614 | { | |
615 | subfile->language = subfile->next->language; | |
616 | } | |
617 | ||
618 | /* Initialize the debug format string to NULL. We may supply it | |
619 | later via a call to record_debugformat. */ | |
620 | subfile->debugformat = NULL; | |
621 | ||
622 | /* cfront output is a C program, so in most ways it looks like a C | |
623 | program. But to demangle we need to set the language to C++. We | |
624 | can distinguish cfront code by the fact that it has #line | |
625 | directives which specify a file name ending in .C. | |
c5aa993b | 626 | |
c906108c SS |
627 | So if the filename of this subfile ends in .C, then change the |
628 | language of any pending subfiles from C to C++. We also accept | |
629 | any other C++ suffixes accepted by deduce_language_from_filename | |
630 | (in particular, some people use .cxx with cfront). */ | |
631 | /* Likewise for f2c. */ | |
632 | ||
633 | if (subfile->name) | |
634 | { | |
635 | struct subfile *s; | |
636 | enum language sublang = deduce_language_from_filename (subfile->name); | |
637 | ||
638 | if (sublang == language_cplus || sublang == language_fortran) | |
639 | for (s = subfiles; s != NULL; s = s->next) | |
640 | if (s->language == language_c) | |
641 | s->language = sublang; | |
642 | } | |
643 | ||
644 | /* And patch up this file if necessary. */ | |
645 | if (subfile->language == language_c | |
646 | && subfile->next != NULL | |
647 | && (subfile->next->language == language_cplus | |
648 | || subfile->next->language == language_fortran)) | |
649 | { | |
650 | subfile->language = subfile->next->language; | |
651 | } | |
652 | } | |
653 | ||
654 | /* For stabs readers, the first N_SO symbol is assumed to be the | |
655 | source file name, and the subfile struct is initialized using that | |
656 | assumption. If another N_SO symbol is later seen, immediately | |
657 | following the first one, then the first one is assumed to be the | |
658 | directory name and the second one is really the source file name. | |
659 | ||
660 | So we have to patch up the subfile struct by moving the old name | |
661 | value to dirname and remembering the new name. Some sanity | |
662 | checking is performed to ensure that the state of the subfile | |
663 | struct is reasonable and that the old name we are assuming to be a | |
664 | directory name actually is (by checking for a trailing '/'). */ | |
665 | ||
666 | void | |
667 | patch_subfile_names (struct subfile *subfile, char *name) | |
668 | { | |
669 | if (subfile != NULL && subfile->dirname == NULL && subfile->name != NULL | |
670 | && subfile->name[strlen (subfile->name) - 1] == '/') | |
671 | { | |
672 | subfile->dirname = subfile->name; | |
673 | subfile->name = savestring (name, strlen (name)); | |
674 | last_source_file = name; | |
675 | ||
676 | /* Default the source language to whatever can be deduced from | |
677 | the filename. If nothing can be deduced (such as for a C/C++ | |
678 | include file with a ".h" extension), then inherit whatever | |
679 | language the previous subfile had. This kludgery is | |
680 | necessary because there is no standard way in some object | |
681 | formats to record the source language. Also, when symtabs | |
682 | are allocated we try to deduce a language then as well, but | |
683 | it is too late for us to use that information while reading | |
684 | symbols, since symtabs aren't allocated until after all the | |
685 | symbols have been processed for a given source file. */ | |
686 | ||
687 | subfile->language = deduce_language_from_filename (subfile->name); | |
688 | if (subfile->language == language_unknown && | |
689 | subfile->next != NULL) | |
690 | { | |
691 | subfile->language = subfile->next->language; | |
692 | } | |
693 | } | |
694 | } | |
695 | \f | |
696 | /* Handle the N_BINCL and N_EINCL symbol types that act like N_SOL for | |
697 | switching source files (different subfiles, as we call them) within | |
698 | one object file, but using a stack rather than in an arbitrary | |
699 | order. */ | |
700 | ||
701 | void | |
702 | push_subfile (void) | |
703 | { | |
704 | register struct subfile_stack *tem | |
705 | = (struct subfile_stack *) xmalloc (sizeof (struct subfile_stack)); | |
706 | ||
707 | tem->next = subfile_stack; | |
708 | subfile_stack = tem; | |
709 | if (current_subfile == NULL || current_subfile->name == NULL) | |
710 | { | |
e1e9e218 | 711 | internal_error (__FILE__, __LINE__, "failed internal consistency check"); |
c906108c SS |
712 | } |
713 | tem->name = current_subfile->name; | |
714 | } | |
715 | ||
716 | char * | |
717 | pop_subfile (void) | |
718 | { | |
719 | register char *name; | |
720 | register struct subfile_stack *link = subfile_stack; | |
721 | ||
722 | if (link == NULL) | |
723 | { | |
e1e9e218 | 724 | internal_error (__FILE__, __LINE__, "failed internal consistency check"); |
c906108c SS |
725 | } |
726 | name = link->name; | |
727 | subfile_stack = link->next; | |
b8c9b27d | 728 | xfree ((void *) link); |
c906108c SS |
729 | return (name); |
730 | } | |
731 | \f | |
732 | /* Add a linetable entry for line number LINE and address PC to the | |
733 | line vector for SUBFILE. */ | |
734 | ||
735 | void | |
736 | record_line (register struct subfile *subfile, int line, CORE_ADDR pc) | |
737 | { | |
738 | struct linetable_entry *e; | |
739 | /* Ignore the dummy line number in libg.o */ | |
740 | ||
741 | if (line == 0xffff) | |
742 | { | |
743 | return; | |
744 | } | |
745 | ||
746 | /* Make sure line vector exists and is big enough. */ | |
747 | if (!subfile->line_vector) | |
748 | { | |
749 | subfile->line_vector_length = INITIAL_LINE_VECTOR_LENGTH; | |
750 | subfile->line_vector = (struct linetable *) | |
751 | xmalloc (sizeof (struct linetable) | |
c5aa993b | 752 | + subfile->line_vector_length * sizeof (struct linetable_entry)); |
c906108c SS |
753 | subfile->line_vector->nitems = 0; |
754 | have_line_numbers = 1; | |
755 | } | |
756 | ||
757 | if (subfile->line_vector->nitems + 1 >= subfile->line_vector_length) | |
758 | { | |
759 | subfile->line_vector_length *= 2; | |
760 | subfile->line_vector = (struct linetable *) | |
761 | xrealloc ((char *) subfile->line_vector, | |
762 | (sizeof (struct linetable) | |
763 | + (subfile->line_vector_length | |
764 | * sizeof (struct linetable_entry)))); | |
765 | } | |
766 | ||
767 | e = subfile->line_vector->item + subfile->line_vector->nitems++; | |
768 | e->line = line; | |
063fd668 | 769 | e->pc = ADDR_BITS_REMOVE(pc); |
c906108c SS |
770 | } |
771 | ||
772 | /* Needed in order to sort line tables from IBM xcoff files. Sigh! */ | |
773 | ||
774 | static int | |
775 | compare_line_numbers (const void *ln1p, const void *ln2p) | |
776 | { | |
777 | struct linetable_entry *ln1 = (struct linetable_entry *) ln1p; | |
778 | struct linetable_entry *ln2 = (struct linetable_entry *) ln2p; | |
779 | ||
780 | /* Note: this code does not assume that CORE_ADDRs can fit in ints. | |
781 | Please keep it that way. */ | |
782 | if (ln1->pc < ln2->pc) | |
783 | return -1; | |
784 | ||
785 | if (ln1->pc > ln2->pc) | |
786 | return 1; | |
787 | ||
788 | /* If pc equal, sort by line. I'm not sure whether this is optimum | |
789 | behavior (see comment at struct linetable in symtab.h). */ | |
790 | return ln1->line - ln2->line; | |
791 | } | |
792 | \f | |
793 | /* Start a new symtab for a new source file. Called, for example, | |
794 | when a stabs symbol of type N_SO is seen, or when a DWARF | |
795 | TAG_compile_unit DIE is seen. It indicates the start of data for | |
796 | one original source file. */ | |
797 | ||
798 | void | |
799 | start_symtab (char *name, char *dirname, CORE_ADDR start_addr) | |
800 | { | |
801 | ||
802 | last_source_file = name; | |
803 | last_source_start_addr = start_addr; | |
804 | file_symbols = NULL; | |
805 | global_symbols = NULL; | |
806 | within_function = 0; | |
807 | have_line_numbers = 0; | |
808 | ||
809 | /* Context stack is initially empty. Allocate first one with room | |
810 | for 10 levels; reuse it forever afterward. */ | |
811 | if (context_stack == NULL) | |
812 | { | |
813 | context_stack_size = INITIAL_CONTEXT_STACK_SIZE; | |
814 | context_stack = (struct context_stack *) | |
815 | xmalloc (context_stack_size * sizeof (struct context_stack)); | |
816 | } | |
817 | context_stack_depth = 0; | |
818 | ||
819 | /* Initialize the list of sub source files with one entry for this | |
820 | file (the top-level source file). */ | |
821 | ||
822 | subfiles = NULL; | |
823 | current_subfile = NULL; | |
824 | start_subfile (name, dirname); | |
825 | } | |
826 | ||
827 | /* Finish the symbol definitions for one main source file, close off | |
828 | all the lexical contexts for that file (creating struct block's for | |
829 | them), then make the struct symtab for that file and put it in the | |
830 | list of all such. | |
831 | ||
832 | END_ADDR is the address of the end of the file's text. SECTION is | |
833 | the section number (in objfile->section_offsets) of the blockvector | |
834 | and linetable. | |
835 | ||
836 | Note that it is possible for end_symtab() to return NULL. In | |
837 | particular, for the DWARF case at least, it will return NULL when | |
838 | it finds a compilation unit that has exactly one DIE, a | |
839 | TAG_compile_unit DIE. This can happen when we link in an object | |
840 | file that was compiled from an empty source file. Returning NULL | |
841 | is probably not the correct thing to do, because then gdb will | |
842 | never know about this empty file (FIXME). */ | |
843 | ||
844 | struct symtab * | |
845 | end_symtab (CORE_ADDR end_addr, struct objfile *objfile, int section) | |
846 | { | |
847 | register struct symtab *symtab = NULL; | |
848 | register struct blockvector *blockvector; | |
849 | register struct subfile *subfile; | |
850 | register struct context_stack *cstk; | |
851 | struct subfile *nextsub; | |
852 | ||
853 | /* Finish the lexical context of the last function in the file; pop | |
854 | the context stack. */ | |
855 | ||
856 | if (context_stack_depth > 0) | |
857 | { | |
858 | cstk = pop_context (); | |
859 | /* Make a block for the local symbols within. */ | |
860 | finish_block (cstk->name, &local_symbols, cstk->old_blocks, | |
861 | cstk->start_addr, end_addr, objfile); | |
862 | ||
863 | if (context_stack_depth > 0) | |
864 | { | |
865 | /* This is said to happen with SCO. The old coffread.c | |
866 | code simply emptied the context stack, so we do the | |
867 | same. FIXME: Find out why it is happening. This is not | |
868 | believed to happen in most cases (even for coffread.c); | |
869 | it used to be an abort(). */ | |
870 | static struct complaint msg = | |
871 | {"Context stack not empty in end_symtab", 0, 0}; | |
872 | complain (&msg); | |
873 | context_stack_depth = 0; | |
874 | } | |
875 | } | |
876 | ||
877 | /* Reordered executables may have out of order pending blocks; if | |
878 | OBJF_REORDERED is true, then sort the pending blocks. */ | |
879 | if ((objfile->flags & OBJF_REORDERED) && pending_blocks) | |
880 | { | |
881 | /* FIXME! Remove this horrid bubble sort and use merge sort!!! */ | |
882 | int swapped; | |
883 | do | |
884 | { | |
885 | struct pending_block *pb, *pbnext; | |
886 | ||
887 | pb = pending_blocks; | |
888 | pbnext = pb->next; | |
889 | swapped = 0; | |
890 | ||
891 | while (pbnext) | |
892 | { | |
893 | /* swap blocks if unordered! */ | |
894 | ||
895 | if (BLOCK_START (pb->block) < BLOCK_START (pbnext->block)) | |
896 | { | |
897 | struct block *tmp = pb->block; | |
898 | pb->block = pbnext->block; | |
899 | pbnext->block = tmp; | |
900 | swapped = 1; | |
901 | } | |
902 | pb = pbnext; | |
903 | pbnext = pbnext->next; | |
904 | } | |
905 | } | |
906 | while (swapped); | |
907 | } | |
908 | ||
909 | /* Cleanup any undefined types that have been left hanging around | |
910 | (this needs to be done before the finish_blocks so that | |
911 | file_symbols is still good). | |
c5aa993b | 912 | |
c906108c SS |
913 | Both cleanup_undefined_types and finish_global_stabs are stabs |
914 | specific, but harmless for other symbol readers, since on gdb | |
915 | startup or when finished reading stabs, the state is set so these | |
916 | are no-ops. FIXME: Is this handled right in case of QUIT? Can | |
917 | we make this cleaner? */ | |
918 | ||
919 | cleanup_undefined_types (); | |
920 | finish_global_stabs (objfile); | |
921 | ||
922 | if (pending_blocks == NULL | |
923 | && file_symbols == NULL | |
924 | && global_symbols == NULL | |
99d9066e JB |
925 | && have_line_numbers == 0 |
926 | && pending_macros == NULL) | |
c906108c SS |
927 | { |
928 | /* Ignore symtabs that have no functions with real debugging | |
929 | info. */ | |
930 | blockvector = NULL; | |
931 | } | |
932 | else | |
933 | { | |
934 | /* Define the STATIC_BLOCK & GLOBAL_BLOCK, and build the | |
935 | blockvector. */ | |
936 | finish_block (0, &file_symbols, 0, last_source_start_addr, end_addr, | |
937 | objfile); | |
938 | finish_block (0, &global_symbols, 0, last_source_start_addr, end_addr, | |
939 | objfile); | |
940 | blockvector = make_blockvector (objfile); | |
941 | } | |
942 | ||
943 | #ifndef PROCESS_LINENUMBER_HOOK | |
944 | #define PROCESS_LINENUMBER_HOOK() | |
945 | #endif | |
946 | PROCESS_LINENUMBER_HOOK (); /* Needed for xcoff. */ | |
947 | ||
948 | /* Now create the symtab objects proper, one for each subfile. */ | |
949 | /* (The main file is the last one on the chain.) */ | |
950 | ||
951 | for (subfile = subfiles; subfile; subfile = nextsub) | |
952 | { | |
953 | int linetablesize = 0; | |
954 | symtab = NULL; | |
955 | ||
956 | /* If we have blocks of symbols, make a symtab. Otherwise, just | |
957 | ignore this file and any line number info in it. */ | |
958 | if (blockvector) | |
959 | { | |
960 | if (subfile->line_vector) | |
961 | { | |
962 | linetablesize = sizeof (struct linetable) + | |
963 | subfile->line_vector->nitems * sizeof (struct linetable_entry); | |
964 | #if 0 | |
965 | /* I think this is artifact from before it went on the | |
966 | obstack. I doubt we'll need the memory between now | |
967 | and when we free it later in this function. */ | |
968 | /* First, shrink the linetable to make more memory. */ | |
969 | subfile->line_vector = (struct linetable *) | |
970 | xrealloc ((char *) subfile->line_vector, linetablesize); | |
971 | #endif | |
972 | ||
973 | /* Like the pending blocks, the line table may be | |
974 | scrambled in reordered executables. Sort it if | |
975 | OBJF_REORDERED is true. */ | |
976 | if (objfile->flags & OBJF_REORDERED) | |
977 | qsort (subfile->line_vector->item, | |
978 | subfile->line_vector->nitems, | |
c5aa993b | 979 | sizeof (struct linetable_entry), compare_line_numbers); |
c906108c SS |
980 | } |
981 | ||
982 | /* Now, allocate a symbol table. */ | |
983 | symtab = allocate_symtab (subfile->name, objfile); | |
984 | ||
985 | /* Fill in its components. */ | |
986 | symtab->blockvector = blockvector; | |
99d9066e | 987 | symtab->macro_table = pending_macros; |
c906108c SS |
988 | if (subfile->line_vector) |
989 | { | |
990 | /* Reallocate the line table on the symbol obstack */ | |
991 | symtab->linetable = (struct linetable *) | |
992 | obstack_alloc (&objfile->symbol_obstack, linetablesize); | |
993 | memcpy (symtab->linetable, subfile->line_vector, linetablesize); | |
994 | } | |
995 | else | |
996 | { | |
997 | symtab->linetable = NULL; | |
998 | } | |
999 | symtab->block_line_section = section; | |
1000 | if (subfile->dirname) | |
1001 | { | |
1002 | /* Reallocate the dirname on the symbol obstack */ | |
1003 | symtab->dirname = (char *) | |
1004 | obstack_alloc (&objfile->symbol_obstack, | |
1005 | strlen (subfile->dirname) + 1); | |
1006 | strcpy (symtab->dirname, subfile->dirname); | |
1007 | } | |
1008 | else | |
1009 | { | |
1010 | symtab->dirname = NULL; | |
1011 | } | |
1012 | symtab->free_code = free_linetable; | |
1013 | symtab->free_ptr = NULL; | |
1014 | ||
1015 | /* Use whatever language we have been using for this | |
1016 | subfile, not the one that was deduced in allocate_symtab | |
1017 | from the filename. We already did our own deducing when | |
1018 | we created the subfile, and we may have altered our | |
1019 | opinion of what language it is from things we found in | |
1020 | the symbols. */ | |
1021 | symtab->language = subfile->language; | |
1022 | ||
1023 | /* Save the debug format string (if any) in the symtab */ | |
1024 | if (subfile->debugformat != NULL) | |
1025 | { | |
1026 | symtab->debugformat = obsavestring (subfile->debugformat, | |
c5aa993b JM |
1027 | strlen (subfile->debugformat), |
1028 | &objfile->symbol_obstack); | |
c906108c SS |
1029 | } |
1030 | ||
1031 | /* All symtabs for the main file and the subfiles share a | |
1032 | blockvector, so we need to clear primary for everything | |
1033 | but the main file. */ | |
1034 | ||
1035 | symtab->primary = 0; | |
1036 | } | |
1037 | if (subfile->name != NULL) | |
1038 | { | |
b8c9b27d | 1039 | xfree ((void *) subfile->name); |
c906108c SS |
1040 | } |
1041 | if (subfile->dirname != NULL) | |
1042 | { | |
b8c9b27d | 1043 | xfree ((void *) subfile->dirname); |
c906108c SS |
1044 | } |
1045 | if (subfile->line_vector != NULL) | |
1046 | { | |
b8c9b27d | 1047 | xfree ((void *) subfile->line_vector); |
c906108c SS |
1048 | } |
1049 | if (subfile->debugformat != NULL) | |
1050 | { | |
b8c9b27d | 1051 | xfree ((void *) subfile->debugformat); |
c906108c SS |
1052 | } |
1053 | ||
1054 | nextsub = subfile->next; | |
b8c9b27d | 1055 | xfree ((void *) subfile); |
c906108c SS |
1056 | } |
1057 | ||
1058 | /* Set this for the main source file. */ | |
1059 | if (symtab) | |
1060 | { | |
1061 | symtab->primary = 1; | |
1062 | } | |
1063 | ||
1064 | last_source_file = NULL; | |
1065 | current_subfile = NULL; | |
99d9066e | 1066 | pending_macros = NULL; |
c906108c SS |
1067 | |
1068 | return symtab; | |
1069 | } | |
1070 | ||
1071 | /* Push a context block. Args are an identifying nesting level | |
1072 | (checkable when you pop it), and the starting PC address of this | |
1073 | context. */ | |
1074 | ||
1075 | struct context_stack * | |
1076 | push_context (int desc, CORE_ADDR valu) | |
1077 | { | |
1078 | register struct context_stack *new; | |
1079 | ||
1080 | if (context_stack_depth == context_stack_size) | |
1081 | { | |
1082 | context_stack_size *= 2; | |
1083 | context_stack = (struct context_stack *) | |
1084 | xrealloc ((char *) context_stack, | |
c5aa993b | 1085 | (context_stack_size * sizeof (struct context_stack))); |
c906108c SS |
1086 | } |
1087 | ||
1088 | new = &context_stack[context_stack_depth++]; | |
1089 | new->depth = desc; | |
1090 | new->locals = local_symbols; | |
1091 | new->params = param_symbols; | |
1092 | new->old_blocks = pending_blocks; | |
1093 | new->start_addr = valu; | |
1094 | new->name = NULL; | |
1095 | ||
1096 | local_symbols = NULL; | |
1097 | param_symbols = NULL; | |
1098 | ||
1099 | return new; | |
1100 | } | |
1101 | \f | |
357e46e7 | 1102 | |
c906108c SS |
1103 | /* Compute a small integer hash code for the given name. */ |
1104 | ||
1105 | int | |
1106 | hashname (char *name) | |
1107 | { | |
357e46e7 | 1108 | return (hash(name,strlen(name)) % HASHSIZE); |
c906108c SS |
1109 | } |
1110 | \f | |
1111 | ||
1112 | void | |
1113 | record_debugformat (char *format) | |
1114 | { | |
1115 | current_subfile->debugformat = savestring (format, strlen (format)); | |
1116 | } | |
1117 | ||
1118 | /* Merge the first symbol list SRCLIST into the second symbol list | |
1119 | TARGETLIST by repeated calls to add_symbol_to_list(). This | |
1120 | procedure "frees" each link of SRCLIST by adding it to the | |
1121 | free_pendings list. Caller must set SRCLIST to a null list after | |
1122 | calling this function. | |
1123 | ||
1124 | Void return. */ | |
1125 | ||
1126 | void | |
1127 | merge_symbol_lists (struct pending **srclist, struct pending **targetlist) | |
1128 | { | |
1129 | register int i; | |
1130 | ||
1131 | if (!srclist || !*srclist) | |
1132 | return; | |
1133 | ||
1134 | /* Merge in elements from current link. */ | |
1135 | for (i = 0; i < (*srclist)->nsyms; i++) | |
1136 | add_symbol_to_list ((*srclist)->symbol[i], targetlist); | |
1137 | ||
1138 | /* Recurse on next. */ | |
1139 | merge_symbol_lists (&(*srclist)->next, targetlist); | |
1140 | ||
1141 | /* "Free" the current link. */ | |
1142 | (*srclist)->next = free_pendings; | |
1143 | free_pendings = (*srclist); | |
1144 | } | |
1145 | \f | |
1146 | /* Initialize anything that needs initializing when starting to read a | |
1147 | fresh piece of a symbol file, e.g. reading in the stuff | |
1148 | corresponding to a psymtab. */ | |
1149 | ||
1150 | void | |
fba45db2 | 1151 | buildsym_init (void) |
c906108c SS |
1152 | { |
1153 | free_pendings = NULL; | |
1154 | file_symbols = NULL; | |
1155 | global_symbols = NULL; | |
1156 | pending_blocks = NULL; | |
99d9066e | 1157 | pending_macros = NULL; |
c906108c SS |
1158 | } |
1159 | ||
1160 | /* Initialize anything that needs initializing when a completely new | |
1161 | symbol file is specified (not just adding some symbols from another | |
1162 | file, e.g. a shared library). */ | |
1163 | ||
1164 | void | |
fba45db2 | 1165 | buildsym_new_init (void) |
c906108c SS |
1166 | { |
1167 | buildsym_init (); | |
1168 | } |