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