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