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