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