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