PR symtab/17559
[deliverable/binutils-gdb.git] / gdb / buildsym.c
CommitLineData
c906108c 1/* Support routines for building symbol tables in GDB's internal format.
ecd75fc8 2 Copyright (C) 1986-2014 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
a9762ec7 8 the Free Software Foundation; either version 3 of the License, or
c5aa993b 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 16 You should have received a copy of the GNU General Public License
a9762ec7 17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
18
19/* This module provides subroutines used for creating and adding to
20 the symbol table. These routines are called from various symbol-
21 file-reading routines.
22
23 Routines to support specific debugging information formats (stabs,
4a64f543 24 DWARF, etc) belong somewhere else. */
c906108c
SS
25
26#include "defs.h"
27#include "bfd.h"
04ea0df1 28#include "gdb_obstack.h"
c906108c 29#include "symtab.h"
72367fb4 30#include "symfile.h"
c906108c
SS
31#include "objfiles.h"
32#include "gdbtypes.h"
33#include "complaints.h"
4a64f543 34#include "expression.h" /* For "enum exp_opcode" used by... */
357e46e7 35#include "bcache.h"
4a64f543 36#include "filenames.h" /* For DOSish file names. */
99d9066e 37#include "macrotab.h"
261397f8 38#include "demangle.h" /* Needed by SYMBOL_INIT_DEMANGLED_NAME. */
fe898f56 39#include "block.h"
9219021c 40#include "cp-support.h"
de4f826b 41#include "dictionary.h"
801e3a5b 42#include "addrmap.h"
9219021c 43
c906108c 44/* Ask buildsym.h to define the vars it normally declares `extern'. */
c5aa993b
JM
45#define EXTERN
46/**/
4a64f543 47#include "buildsym.h" /* Our own declarations. */
c906108c
SS
48#undef EXTERN
49
0a0edcd5 50/* For cleanup_undefined_stabs_types and finish_global_stabs (somewhat
c906108c
SS
51 questionable--see comment where we call them). */
52
53#include "stabsread.h"
54
94d09e04
DE
55/* List of subfiles. */
56
57static struct subfile *subfiles;
58
c906108c
SS
59/* List of free `struct pending' structures for reuse. */
60
61static struct pending *free_pendings;
62
63/* Non-zero if symtab has line number info. This prevents an
64 otherwise empty symtab from being tossed. */
65
66static int have_line_numbers;
801e3a5b
JB
67
68/* The mutable address map for the compilation unit whose symbols
69 we're currently reading. The symtabs' shared blockvector will
70 point to a fixed copy of this. */
71static struct addrmap *pending_addrmap;
72
73/* The obstack on which we allocate pending_addrmap.
74 If pending_addrmap is NULL, this is uninitialized; otherwise, it is
75 initialized (and holds pending_addrmap). */
76static struct obstack pending_addrmap_obstack;
77
78/* Non-zero if we recorded any ranges in the addrmap that are
79 different from those in the blockvector already. We set this to
80 zero when we start processing a symfile, and if it's still zero at
81 the end, then we just toss the addrmap. */
82static int pending_addrmap_interesting;
83
93eed41f
TT
84/* An obstack used for allocating pending blocks. */
85
86static struct obstack pending_block_obstack;
87
88/* List of blocks already made (lexical contexts already closed).
89 This is used at the end to make the blockvector. */
90
91struct pending_block
92 {
93 struct pending_block *next;
94 struct block *block;
95 };
96
97/* Pointer to the head of a linked list of symbol blocks which have
98 already been finalized (lexical contexts already closed) and which
99 are just waiting to be built into a blockvector when finalizing the
100 associated symtab. */
101
102static struct pending_block *pending_blocks;
fc474241
DE
103
104struct subfile_stack
105 {
106 struct subfile_stack *next;
107 char *name;
108 };
109
110static struct subfile_stack *subfile_stack;
111
112/* The macro table for the compilation unit whose symbols we're
113 currently reading. All the symtabs for the CU will point to this. */
114static struct macro_table *pending_macros;
115
c906108c 116static int compare_line_numbers (const void *ln1p, const void *ln2p);
0b49e518
TT
117
118static void record_pending_block (struct objfile *objfile,
119 struct block *block,
120 struct pending_block *opblock);
c906108c
SS
121
122/* Initial sizes of data structures. These are realloc'd larger if
123 needed, and realloc'd down to the size actually used, when
124 completed. */
125
126#define INITIAL_CONTEXT_STACK_SIZE 10
127#define INITIAL_LINE_VECTOR_LENGTH 1000
128\f
129
4a64f543 130/* Maintain the lists of symbols and blocks. */
c906108c 131
93bf33fd 132/* Add a symbol to one of the lists of symbols. */
c906108c
SS
133
134void
135add_symbol_to_list (struct symbol *symbol, struct pending **listhead)
136{
52f0bd74 137 struct pending *link;
c906108c
SS
138
139 /* If this is an alias for another symbol, don't add it. */
140 if (symbol->ginfo.name && symbol->ginfo.name[0] == '#')
141 return;
142
4a64f543 143 /* We keep PENDINGSIZE symbols in each link of the list. If we
c906108c
SS
144 don't have a link with room in it, add a new link. */
145 if (*listhead == NULL || (*listhead)->nsyms == PENDINGSIZE)
146 {
147 if (free_pendings)
148 {
149 link = free_pendings;
150 free_pendings = link->next;
151 }
152 else
153 {
154 link = (struct pending *) xmalloc (sizeof (struct pending));
155 }
156
157 link->next = *listhead;
158 *listhead = link;
159 link->nsyms = 0;
160 }
161
162 (*listhead)->symbol[(*listhead)->nsyms++] = symbol;
163}
164
165/* Find a symbol named NAME on a LIST. NAME need not be
166 '\0'-terminated; LENGTH is the length of the name. */
167
168struct symbol *
169find_symbol_in_list (struct pending *list, char *name, int length)
170{
171 int j;
0d5cff50 172 const char *pp;
c906108c
SS
173
174 while (list != NULL)
175 {
176 for (j = list->nsyms; --j >= 0;)
177 {
3567439c 178 pp = SYMBOL_LINKAGE_NAME (list->symbol[j]);
5aafa1cc
PM
179 if (*pp == *name && strncmp (pp, name, length) == 0
180 && pp[length] == '\0')
c906108c
SS
181 {
182 return (list->symbol[j]);
183 }
184 }
185 list = list->next;
186 }
187 return (NULL);
188}
189
190/* At end of reading syms, or in case of quit, really free as many
4a64f543 191 `struct pending's as we can easily find. */
c906108c 192
c906108c 193void
bde58177 194really_free_pendings (void *dummy)
c906108c
SS
195{
196 struct pending *next, *next1;
197
198 for (next = free_pendings; next; next = next1)
199 {
200 next1 = next->next;
b8c9b27d 201 xfree ((void *) next);
c906108c
SS
202 }
203 free_pendings = NULL;
204
205 free_pending_blocks ();
206
207 for (next = file_symbols; next != NULL; next = next1)
208 {
209 next1 = next->next;
b8c9b27d 210 xfree ((void *) next);
c906108c
SS
211 }
212 file_symbols = NULL;
213
214 for (next = global_symbols; next != NULL; next = next1)
215 {
216 next1 = next->next;
b8c9b27d 217 xfree ((void *) next);
c906108c
SS
218 }
219 global_symbols = NULL;
99d9066e
JB
220
221 if (pending_macros)
222 free_macro_table (pending_macros);
801e3a5b
JB
223
224 if (pending_addrmap)
225 {
226 obstack_free (&pending_addrmap_obstack, NULL);
227 pending_addrmap = NULL;
228 }
c906108c
SS
229}
230
4a64f543 231/* This function is called to discard any pending blocks. */
c906108c
SS
232
233void
234free_pending_blocks (void)
235{
93eed41f
TT
236 if (pending_blocks != NULL)
237 {
238 obstack_free (&pending_block_obstack, NULL);
239 pending_blocks = NULL;
240 }
c906108c
SS
241}
242
243/* Take one of the lists of symbols and make a block from it. Keep
244 the order the symbols have in the list (reversed from the input
245 file). Put the block on the list of pending blocks. */
246
84a146c9
TT
247static struct block *
248finish_block_internal (struct symbol *symbol, struct pending **listhead,
249 struct pending_block *old_blocks,
250 CORE_ADDR start, CORE_ADDR end,
251 struct objfile *objfile,
6d30eef8 252 int is_global, int expandable)
c906108c 253{
5af949e3 254 struct gdbarch *gdbarch = get_objfile_arch (objfile);
52f0bd74
AC
255 struct pending *next, *next1;
256 struct block *block;
257 struct pending_block *pblock;
c906108c 258 struct pending_block *opblock;
c906108c 259
84a146c9
TT
260 block = (is_global
261 ? allocate_global_block (&objfile->objfile_obstack)
262 : allocate_block (&objfile->objfile_obstack));
c906108c 263
261397f8
DJ
264 if (symbol)
265 {
4a146b47 266 BLOCK_DICT (block) = dict_create_linear (&objfile->objfile_obstack,
de4f826b 267 *listhead);
261397f8
DJ
268 }
269 else
c906108c 270 {
6d30eef8
DE
271 if (expandable)
272 {
273 BLOCK_DICT (block) = dict_create_hashed_expandable ();
274 dict_add_pending (BLOCK_DICT (block), *listhead);
275 }
276 else
277 {
278 BLOCK_DICT (block) =
279 dict_create_hashed (&objfile->objfile_obstack, *listhead);
280 }
c906108c
SS
281 }
282
283 BLOCK_START (block) = start;
284 BLOCK_END (block) = end;
c906108c 285
c906108c
SS
286 /* Put the block in as the value of the symbol that names it. */
287
288 if (symbol)
289 {
290 struct type *ftype = SYMBOL_TYPE (symbol);
de4f826b 291 struct dict_iterator iter;
c906108c
SS
292 SYMBOL_BLOCK_VALUE (symbol) = block;
293 BLOCK_FUNCTION (block) = symbol;
294
295 if (TYPE_NFIELDS (ftype) <= 0)
296 {
297 /* No parameter type information is recorded with the
298 function's type. Set that from the type of the
4a64f543 299 parameter symbols. */
c906108c
SS
300 int nparams = 0, iparams;
301 struct symbol *sym;
8157b174
TT
302
303 /* Here we want to directly access the dictionary, because
304 we haven't fully initialized the block yet. */
305 ALL_DICT_SYMBOLS (BLOCK_DICT (block), iter, sym)
c906108c 306 {
2a2d4dc3
AS
307 if (SYMBOL_IS_ARGUMENT (sym))
308 nparams++;
c906108c
SS
309 }
310 if (nparams > 0)
311 {
312 TYPE_NFIELDS (ftype) = nparams;
313 TYPE_FIELDS (ftype) = (struct field *)
314 TYPE_ALLOC (ftype, nparams * sizeof (struct field));
315
de4f826b 316 iparams = 0;
8157b174
TT
317 /* Here we want to directly access the dictionary, because
318 we haven't fully initialized the block yet. */
319 ALL_DICT_SYMBOLS (BLOCK_DICT (block), iter, sym)
c906108c 320 {
de4f826b
DC
321 if (iparams == nparams)
322 break;
323
2a2d4dc3 324 if (SYMBOL_IS_ARGUMENT (sym))
c906108c 325 {
c906108c 326 TYPE_FIELD_TYPE (ftype, iparams) = SYMBOL_TYPE (sym);
8176bb6d 327 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
c906108c 328 iparams++;
c906108c
SS
329 }
330 }
331 }
332 }
333 }
334 else
335 {
336 BLOCK_FUNCTION (block) = NULL;
337 }
338
339 /* Now "free" the links of the list, and empty the list. */
340
341 for (next = *listhead; next; next = next1)
342 {
343 next1 = next->next;
344 next->next = free_pendings;
345 free_pendings = next;
346 }
347 *listhead = NULL;
348
c906108c 349 /* Check to be sure that the blocks have an end address that is
4a64f543 350 greater than starting address. */
c906108c
SS
351
352 if (BLOCK_END (block) < BLOCK_START (block))
353 {
354 if (symbol)
355 {
23136709 356 complaint (&symfile_complaints,
3e43a32a
MS
357 _("block end address less than block "
358 "start address in %s (patched it)"),
de5ad195 359 SYMBOL_PRINT_NAME (symbol));
c906108c
SS
360 }
361 else
362 {
23136709 363 complaint (&symfile_complaints,
3e43a32a
MS
364 _("block end address %s less than block "
365 "start address %s (patched it)"),
5af949e3
UW
366 paddress (gdbarch, BLOCK_END (block)),
367 paddress (gdbarch, BLOCK_START (block)));
c906108c 368 }
4a64f543 369 /* Better than nothing. */
c906108c
SS
370 BLOCK_END (block) = BLOCK_START (block);
371 }
c906108c
SS
372
373 /* Install this block as the superblock of all blocks made since the
374 start of this scope that don't have superblocks yet. */
375
376 opblock = NULL;
c0219d42
MS
377 for (pblock = pending_blocks;
378 pblock && pblock != old_blocks;
379 pblock = pblock->next)
c906108c
SS
380 {
381 if (BLOCK_SUPERBLOCK (pblock->block) == NULL)
382 {
c906108c 383 /* Check to be sure the blocks are nested as we receive
4a64f543 384 them. If the compiler/assembler/linker work, this just
14711c82
DJ
385 burns a small amount of time.
386
387 Skip blocks which correspond to a function; they're not
388 physically nested inside this other blocks, only
389 lexically nested. */
390 if (BLOCK_FUNCTION (pblock->block) == NULL
391 && (BLOCK_START (pblock->block) < BLOCK_START (block)
392 || BLOCK_END (pblock->block) > BLOCK_END (block)))
c906108c
SS
393 {
394 if (symbol)
395 {
23136709 396 complaint (&symfile_complaints,
3d263c1d 397 _("inner block not inside outer block in %s"),
de5ad195 398 SYMBOL_PRINT_NAME (symbol));
c906108c
SS
399 }
400 else
401 {
23136709 402 complaint (&symfile_complaints,
3e43a32a
MS
403 _("inner block (%s-%s) not "
404 "inside outer block (%s-%s)"),
5af949e3
UW
405 paddress (gdbarch, BLOCK_START (pblock->block)),
406 paddress (gdbarch, BLOCK_END (pblock->block)),
407 paddress (gdbarch, BLOCK_START (block)),
408 paddress (gdbarch, BLOCK_END (block)));
c906108c
SS
409 }
410 if (BLOCK_START (pblock->block) < BLOCK_START (block))
411 BLOCK_START (pblock->block) = BLOCK_START (block);
412 if (BLOCK_END (pblock->block) > BLOCK_END (block))
413 BLOCK_END (pblock->block) = BLOCK_END (block);
414 }
c906108c
SS
415 BLOCK_SUPERBLOCK (pblock->block) = block;
416 }
417 opblock = pblock;
418 }
419
27aa8d6a 420 block_set_using (block, using_directives, &objfile->objfile_obstack);
00ae8fef 421 using_directives = NULL;
27aa8d6a 422
c906108c 423 record_pending_block (objfile, block, opblock);
801e3a5b
JB
424
425 return block;
c906108c
SS
426}
427
84a146c9
TT
428struct block *
429finish_block (struct symbol *symbol, struct pending **listhead,
430 struct pending_block *old_blocks,
431 CORE_ADDR start, CORE_ADDR end,
432 struct objfile *objfile)
433{
434 return finish_block_internal (symbol, listhead, old_blocks,
6d30eef8 435 start, end, objfile, 0, 0);
84a146c9 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
0b49e518 445static void
c906108c
SS
446record_pending_block (struct objfile *objfile, struct block *block,
447 struct pending_block *opblock)
448{
52f0bd74 449 struct pending_block *pblock;
c906108c 450
93eed41f
TT
451 if (pending_blocks == NULL)
452 obstack_init (&pending_block_obstack);
453
c906108c 454 pblock = (struct pending_block *)
93eed41f 455 obstack_alloc (&pending_block_obstack, sizeof (struct pending_block));
c906108c
SS
456 pblock->block = block;
457 if (opblock)
458 {
459 pblock->next = opblock->next;
460 opblock->next = pblock;
461 }
462 else
463 {
464 pblock->next = pending_blocks;
465 pending_blocks = pblock;
466 }
467}
468
801e3a5b
JB
469
470/* Record that the range of addresses from START to END_INCLUSIVE
471 (inclusive, like it says) belongs to BLOCK. BLOCK's start and end
472 addresses must be set already. You must apply this function to all
473 BLOCK's children before applying it to BLOCK.
474
475 If a call to this function complicates the picture beyond that
476 already provided by BLOCK_START and BLOCK_END, then we create an
477 address map for the block. */
478void
479record_block_range (struct block *block,
480 CORE_ADDR start, CORE_ADDR end_inclusive)
481{
482 /* If this is any different from the range recorded in the block's
483 own BLOCK_START and BLOCK_END, then note that the address map has
484 become interesting. Note that even if this block doesn't have
485 any "interesting" ranges, some later block might, so we still
486 need to record this block in the addrmap. */
487 if (start != BLOCK_START (block)
488 || end_inclusive + 1 != BLOCK_END (block))
489 pending_addrmap_interesting = 1;
490
491 if (! pending_addrmap)
492 {
493 obstack_init (&pending_addrmap_obstack);
494 pending_addrmap = addrmap_create_mutable (&pending_addrmap_obstack);
495 }
496
497 addrmap_set_empty (pending_addrmap, start, end_inclusive, block);
498}
499
500
822e978b 501static struct blockvector *
c906108c
SS
502make_blockvector (struct objfile *objfile)
503{
52f0bd74
AC
504 struct pending_block *next;
505 struct blockvector *blockvector;
506 int i;
c906108c
SS
507
508 /* Count the length of the list of blocks. */
509
510 for (next = pending_blocks, i = 0; next; next = next->next, i++)
511 {;
512 }
513
514 blockvector = (struct blockvector *)
4a146b47 515 obstack_alloc (&objfile->objfile_obstack,
c906108c
SS
516 (sizeof (struct blockvector)
517 + (i - 1) * sizeof (struct block *)));
518
4a64f543 519 /* Copy the blocks into the blockvector. This is done in reverse
c906108c 520 order, which happens to put the blocks into the proper order
4a64f543 521 (ascending starting address). finish_block has hair to insert
c906108c
SS
522 each block into the list after its subblocks in order to make
523 sure this is true. */
524
525 BLOCKVECTOR_NBLOCKS (blockvector) = i;
526 for (next = pending_blocks; next; next = next->next)
527 {
528 BLOCKVECTOR_BLOCK (blockvector, --i) = next->block;
529 }
530
89ba75b1 531 free_pending_blocks ();
c906108c 532
801e3a5b
JB
533 /* If we needed an address map for this symtab, record it in the
534 blockvector. */
535 if (pending_addrmap && pending_addrmap_interesting)
536 BLOCKVECTOR_MAP (blockvector)
537 = addrmap_create_fixed (pending_addrmap, &objfile->objfile_obstack);
538 else
539 BLOCKVECTOR_MAP (blockvector) = 0;
4aad0dfc 540
c906108c 541 /* Some compilers output blocks in the wrong order, but we depend on
4a64f543 542 their being in the right order so we can binary search. Check the
4aad0dfc
DE
543 order and moan about it.
544 Note: Remember that the first two blocks are the global and static
545 blocks. We could special case that fact and begin checking at block 2.
546 To avoid making that assumption we do not. */
c906108c
SS
547 if (BLOCKVECTOR_NBLOCKS (blockvector) > 1)
548 {
549 for (i = 1; i < BLOCKVECTOR_NBLOCKS (blockvector); i++)
550 {
551 if (BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i - 1))
552 > BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i)))
553 {
59527da0
JB
554 CORE_ADDR start
555 = BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i));
c906108c 556
3d263c1d 557 complaint (&symfile_complaints, _("block at %s out of order"),
bb599908 558 hex_string ((LONGEST) start));
c906108c
SS
559 }
560 }
561 }
c906108c
SS
562
563 return (blockvector);
564}
565\f
566/* Start recording information about source code that came from an
567 included (or otherwise merged-in) source file with a different
568 name. NAME is the name of the file (cannot be NULL), DIRNAME is
4a64f543
MS
569 the directory in which the file was compiled (or NULL if not
570 known). */
c906108c
SS
571
572void
72b9f47f 573start_subfile (const char *name, const char *dirname)
c906108c 574{
52f0bd74 575 struct subfile *subfile;
c906108c
SS
576
577 /* See if this subfile is already known as a subfile of the current
578 main source file. */
579
580 for (subfile = subfiles; subfile; subfile = subfile->next)
581 {
84ba0adf
DJ
582 char *subfile_name;
583
584 /* If NAME is an absolute path, and this subfile is not, then
585 attempt to create an absolute path to compare. */
586 if (IS_ABSOLUTE_PATH (name)
587 && !IS_ABSOLUTE_PATH (subfile->name)
588 && subfile->dirname != NULL)
589 subfile_name = concat (subfile->dirname, SLASH_STRING,
6eb7ee03 590 subfile->name, (char *) NULL);
84ba0adf
DJ
591 else
592 subfile_name = subfile->name;
593
594 if (FILENAME_CMP (subfile_name, name) == 0)
c906108c
SS
595 {
596 current_subfile = subfile;
84ba0adf
DJ
597 if (subfile_name != subfile->name)
598 xfree (subfile_name);
c906108c
SS
599 return;
600 }
84ba0adf
DJ
601 if (subfile_name != subfile->name)
602 xfree (subfile_name);
c906108c
SS
603 }
604
4a64f543 605 /* This subfile is not known. Add an entry for it. Make an entry
c906108c
SS
606 for this subfile in the list of all subfiles of the current main
607 source file. */
608
609 subfile = (struct subfile *) xmalloc (sizeof (struct subfile));
59527da0 610 memset ((char *) subfile, 0, sizeof (struct subfile));
c906108c
SS
611 subfile->next = subfiles;
612 subfiles = subfile;
613 current_subfile = subfile;
614
4a64f543 615 /* Save its name and compilation directory name. */
b74db436 616 subfile->name = xstrdup (name);
1b36a34b 617 subfile->dirname = (dirname == NULL) ? NULL : xstrdup (dirname);
c906108c
SS
618
619 /* Initialize line-number recording for this subfile. */
620 subfile->line_vector = NULL;
621
622 /* Default the source language to whatever can be deduced from the
623 filename. If nothing can be deduced (such as for a C/C++ include
624 file with a ".h" extension), then inherit whatever language the
625 previous subfile had. This kludgery is necessary because there
626 is no standard way in some object formats to record the source
627 language. Also, when symtabs are allocated we try to deduce a
628 language then as well, but it is too late for us to use that
629 information while reading symbols, since symtabs aren't allocated
630 until after all the symbols have been processed for a given
4a64f543 631 source file. */
c906108c
SS
632
633 subfile->language = deduce_language_from_filename (subfile->name);
5aafa1cc
PM
634 if (subfile->language == language_unknown
635 && subfile->next != NULL)
c906108c
SS
636 {
637 subfile->language = subfile->next->language;
638 }
639
640 /* Initialize the debug format string to NULL. We may supply it
4a64f543 641 later via a call to record_debugformat. */
c906108c
SS
642 subfile->debugformat = NULL;
643
303b6f5d
DJ
644 /* Similarly for the producer. */
645 subfile->producer = NULL;
646
25caa7a8 647 /* If the filename of this subfile ends in .C, then change the
c906108c 648 language of any pending subfiles from C to C++. We also accept
25caa7a8 649 any other C++ suffixes accepted by deduce_language_from_filename. */
c906108c
SS
650 /* Likewise for f2c. */
651
652 if (subfile->name)
653 {
654 struct subfile *s;
655 enum language sublang = deduce_language_from_filename (subfile->name);
656
657 if (sublang == language_cplus || sublang == language_fortran)
658 for (s = subfiles; s != NULL; s = s->next)
659 if (s->language == language_c)
660 s->language = sublang;
661 }
662
663 /* And patch up this file if necessary. */
664 if (subfile->language == language_c
665 && subfile->next != NULL
666 && (subfile->next->language == language_cplus
667 || subfile->next->language == language_fortran))
668 {
669 subfile->language = subfile->next->language;
670 }
671}
672
673/* For stabs readers, the first N_SO symbol is assumed to be the
674 source file name, and the subfile struct is initialized using that
675 assumption. If another N_SO symbol is later seen, immediately
676 following the first one, then the first one is assumed to be the
677 directory name and the second one is really the source file name.
678
679 So we have to patch up the subfile struct by moving the old name
680 value to dirname and remembering the new name. Some sanity
681 checking is performed to ensure that the state of the subfile
682 struct is reasonable and that the old name we are assuming to be a
4a64f543 683 directory name actually is (by checking for a trailing '/'). */
c906108c
SS
684
685void
686patch_subfile_names (struct subfile *subfile, char *name)
687{
688 if (subfile != NULL && subfile->dirname == NULL && subfile->name != NULL
0ba1096a 689 && IS_DIR_SEPARATOR (subfile->name[strlen (subfile->name) - 1]))
c906108c
SS
690 {
691 subfile->dirname = subfile->name;
1b36a34b 692 subfile->name = xstrdup (name);
46212e0b 693 set_last_source_file (name);
c906108c
SS
694
695 /* Default the source language to whatever can be deduced from
696 the filename. If nothing can be deduced (such as for a C/C++
697 include file with a ".h" extension), then inherit whatever
698 language the previous subfile had. This kludgery is
699 necessary because there is no standard way in some object
700 formats to record the source language. Also, when symtabs
701 are allocated we try to deduce a language then as well, but
702 it is too late for us to use that information while reading
703 symbols, since symtabs aren't allocated until after all the
4a64f543 704 symbols have been processed for a given source file. */
c906108c
SS
705
706 subfile->language = deduce_language_from_filename (subfile->name);
5aafa1cc
PM
707 if (subfile->language == language_unknown
708 && subfile->next != NULL)
c906108c
SS
709 {
710 subfile->language = subfile->next->language;
711 }
712 }
713}
714\f
715/* Handle the N_BINCL and N_EINCL symbol types that act like N_SOL for
716 switching source files (different subfiles, as we call them) within
717 one object file, but using a stack rather than in an arbitrary
718 order. */
719
720void
721push_subfile (void)
722{
52f0bd74 723 struct subfile_stack *tem
cc59ec59 724 = (struct subfile_stack *) xmalloc (sizeof (struct subfile_stack));
c906108c
SS
725
726 tem->next = subfile_stack;
727 subfile_stack = tem;
728 if (current_subfile == NULL || current_subfile->name == NULL)
729 {
4a64f543
MS
730 internal_error (__FILE__, __LINE__,
731 _("failed internal consistency check"));
c906108c
SS
732 }
733 tem->name = current_subfile->name;
734}
735
736char *
737pop_subfile (void)
738{
52f0bd74
AC
739 char *name;
740 struct subfile_stack *link = subfile_stack;
c906108c
SS
741
742 if (link == NULL)
743 {
3e43a32a
MS
744 internal_error (__FILE__, __LINE__,
745 _("failed internal consistency check"));
c906108c
SS
746 }
747 name = link->name;
748 subfile_stack = link->next;
b8c9b27d 749 xfree ((void *) link);
c906108c
SS
750 return (name);
751}
752\f
753/* Add a linetable entry for line number LINE and address PC to the
754 line vector for SUBFILE. */
755
756void
aa1ee363 757record_line (struct subfile *subfile, int line, CORE_ADDR pc)
c906108c
SS
758{
759 struct linetable_entry *e;
c906108c 760
cc59ec59 761 /* Ignore the dummy line number in libg.o */
c906108c
SS
762 if (line == 0xffff)
763 {
764 return;
765 }
766
767 /* Make sure line vector exists and is big enough. */
768 if (!subfile->line_vector)
769 {
770 subfile->line_vector_length = INITIAL_LINE_VECTOR_LENGTH;
771 subfile->line_vector = (struct linetable *)
772 xmalloc (sizeof (struct linetable)
c5aa993b 773 + subfile->line_vector_length * sizeof (struct linetable_entry));
c906108c
SS
774 subfile->line_vector->nitems = 0;
775 have_line_numbers = 1;
776 }
777
778 if (subfile->line_vector->nitems + 1 >= subfile->line_vector_length)
779 {
780 subfile->line_vector_length *= 2;
781 subfile->line_vector = (struct linetable *)
782 xrealloc ((char *) subfile->line_vector,
783 (sizeof (struct linetable)
784 + (subfile->line_vector_length
785 * sizeof (struct linetable_entry))));
786 }
787
607ae575
DJ
788 /* Normally, we treat lines as unsorted. But the end of sequence
789 marker is special. We sort line markers at the same PC by line
790 number, so end of sequence markers (which have line == 0) appear
791 first. This is right if the marker ends the previous function,
792 and there is no padding before the next function. But it is
793 wrong if the previous line was empty and we are now marking a
794 switch to a different subfile. We must leave the end of sequence
795 marker at the end of this group of lines, not sort the empty line
796 to after the marker. The easiest way to accomplish this is to
797 delete any empty lines from our table, if they are followed by
798 end of sequence markers. All we lose is the ability to set
799 breakpoints at some lines which contain no instructions
800 anyway. */
801 if (line == 0 && subfile->line_vector->nitems > 0)
802 {
803 e = subfile->line_vector->item + subfile->line_vector->nitems - 1;
804 while (subfile->line_vector->nitems > 0 && e->pc == pc)
805 {
806 e--;
807 subfile->line_vector->nitems--;
808 }
809 }
810
c906108c
SS
811 e = subfile->line_vector->item + subfile->line_vector->nitems++;
812 e->line = line;
607ae575 813 e->pc = pc;
c906108c
SS
814}
815
816/* Needed in order to sort line tables from IBM xcoff files. Sigh! */
817
818static int
819compare_line_numbers (const void *ln1p, const void *ln2p)
820{
821 struct linetable_entry *ln1 = (struct linetable_entry *) ln1p;
822 struct linetable_entry *ln2 = (struct linetable_entry *) ln2p;
823
824 /* Note: this code does not assume that CORE_ADDRs can fit in ints.
825 Please keep it that way. */
826 if (ln1->pc < ln2->pc)
827 return -1;
828
829 if (ln1->pc > ln2->pc)
830 return 1;
831
832 /* If pc equal, sort by line. I'm not sure whether this is optimum
833 behavior (see comment at struct linetable in symtab.h). */
834 return ln1->line - ln2->line;
835}
836\f
fc474241
DE
837/* Return the macro table.
838 Initialize it if this is the first use. */
839
840struct macro_table *
841get_macro_table (struct objfile *objfile, const char *comp_dir)
842{
843 if (! pending_macros)
844 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
845 objfile->per_bfd->macro_cache,
846 comp_dir);
847 return pending_macros;
848}
849\f
c906108c
SS
850/* Start a new symtab for a new source file. Called, for example,
851 when a stabs symbol of type N_SO is seen, or when a DWARF
852 TAG_compile_unit DIE is seen. It indicates the start of data for
0b0287a1
DE
853 one original source file.
854
855 NAME is the name of the file (cannot be NULL). DIRNAME is the directory in
856 which the file was compiled (or NULL if not known). START_ADDR is the
857 lowest address of objects in the file (or 0 if not known). */
c906108c
SS
858
859void
46212e0b 860start_symtab (const char *name, const char *dirname, CORE_ADDR start_addr)
c906108c 861{
6d30eef8 862 restart_symtab (start_addr);
46212e0b 863 set_last_source_file (name);
6d30eef8
DE
864 start_subfile (name, dirname);
865}
866
867/* Restart compilation for a symtab.
868 This is used when a symtab is built from multiple sources.
869 The symtab is first built with start_symtab and then for each additional
870 piece call restart_symtab. */
871
872void
873restart_symtab (CORE_ADDR start_addr)
874{
46212e0b 875 set_last_source_file (NULL);
c906108c
SS
876 last_source_start_addr = start_addr;
877 file_symbols = NULL;
878 global_symbols = NULL;
879 within_function = 0;
880 have_line_numbers = 0;
881
882 /* Context stack is initially empty. Allocate first one with room
883 for 10 levels; reuse it forever afterward. */
884 if (context_stack == NULL)
885 {
886 context_stack_size = INITIAL_CONTEXT_STACK_SIZE;
887 context_stack = (struct context_stack *)
888 xmalloc (context_stack_size * sizeof (struct context_stack));
889 }
890 context_stack_depth = 0;
891
801e3a5b
JB
892 /* We shouldn't have any address map at this point. */
893 gdb_assert (! pending_addrmap);
894
c906108c
SS
895 /* Initialize the list of sub source files with one entry for this
896 file (the top-level source file). */
c906108c
SS
897 subfiles = NULL;
898 current_subfile = NULL;
c906108c
SS
899}
900
4a64f543
MS
901/* Subroutine of end_symtab to simplify it. Look for a subfile that
902 matches the main source file's basename. If there is only one, and
903 if the main source file doesn't have any symbol or line number
904 information, then copy this file's symtab and line_vector to the
905 main source file's subfile and discard the other subfile. This can
906 happen because of a compiler bug or from the user playing games
907 with #line or from things like a distributed build system that
908 manipulates the debug info. */
4584e32e
DE
909
910static void
911watch_main_source_file_lossage (void)
912{
913 struct subfile *mainsub, *subfile;
914
915 /* Find the main source file.
916 This loop could be eliminated if start_symtab saved it for us. */
917 mainsub = NULL;
918 for (subfile = subfiles; subfile; subfile = subfile->next)
919 {
920 /* The main subfile is guaranteed to be the last one. */
921 if (subfile->next == NULL)
922 mainsub = subfile;
923 }
924
4a64f543
MS
925 /* If the main source file doesn't have any line number or symbol
926 info, look for an alias in another subfile.
927
928 We have to watch for mainsub == NULL here. It's a quirk of
929 end_symtab, it can return NULL so there may not be a main
930 subfile. */
4584e32e
DE
931
932 if (mainsub
933 && mainsub->line_vector == NULL
934 && mainsub->symtab == NULL)
935 {
936 const char *mainbase = lbasename (mainsub->name);
937 int nr_matches = 0;
938 struct subfile *prevsub;
939 struct subfile *mainsub_alias = NULL;
940 struct subfile *prev_mainsub_alias = NULL;
941
942 prevsub = NULL;
943 for (subfile = subfiles;
944 /* Stop before we get to the last one. */
945 subfile->next;
946 subfile = subfile->next)
947 {
0ba1096a 948 if (filename_cmp (lbasename (subfile->name), mainbase) == 0)
4584e32e
DE
949 {
950 ++nr_matches;
951 mainsub_alias = subfile;
952 prev_mainsub_alias = prevsub;
953 }
954 prevsub = subfile;
955 }
956
957 if (nr_matches == 1)
958 {
959 gdb_assert (mainsub_alias != NULL && mainsub_alias != mainsub);
960
961 /* Found a match for the main source file.
962 Copy its line_vector and symtab to the main subfile
963 and then discard it. */
964
965 mainsub->line_vector = mainsub_alias->line_vector;
966 mainsub->line_vector_length = mainsub_alias->line_vector_length;
967 mainsub->symtab = mainsub_alias->symtab;
968
969 if (prev_mainsub_alias == NULL)
970 subfiles = mainsub_alias->next;
971 else
972 prev_mainsub_alias->next = mainsub_alias->next;
973 xfree (mainsub_alias);
974 }
975 }
976}
977
98cc87bd 978/* Helper function for qsort. Parameters are `struct block *' pointers,
07e7f39f
JK
979 function sorts them in descending order by their BLOCK_START. */
980
981static int
982block_compar (const void *ap, const void *bp)
983{
984 const struct block *a = *(const struct block **) ap;
985 const struct block *b = *(const struct block **) bp;
986
987 return ((BLOCK_START (b) > BLOCK_START (a))
988 - (BLOCK_START (b) < BLOCK_START (a)));
989}
990
6d30eef8
DE
991/* Reset globals used to build symtabs. */
992
993static void
994reset_symtab_globals (void)
995{
46212e0b 996 set_last_source_file (NULL);
6d30eef8
DE
997 current_subfile = NULL;
998 pending_macros = NULL;
999 if (pending_addrmap)
1000 {
1001 obstack_free (&pending_addrmap_obstack, NULL);
1002 pending_addrmap = NULL;
1003 }
1004}
1005
4359dff1
JK
1006/* Implementation of the first part of end_symtab. It allows modifying
1007 STATIC_BLOCK before it gets finalized by end_symtab_from_static_block.
1008 If the returned value is NULL there is no blockvector created for
1009 this symtab (you still must call end_symtab_from_static_block).
c906108c 1010
4359dff1
JK
1011 END_ADDR is the same as for end_symtab: the address of the end of the
1012 file's text.
c906108c 1013
4359dff1 1014 If EXPANDABLE is non-zero the STATIC_BLOCK dictionary is made
36586728
TT
1015 expandable.
1016
1017 If REQUIRED is non-zero, then a symtab is created even if it does
1018 not contain any symbols. */
6d30eef8 1019
4359dff1
JK
1020struct block *
1021end_symtab_get_static_block (CORE_ADDR end_addr, struct objfile *objfile,
36586728 1022 int expandable, int required)
c906108c 1023{
c906108c
SS
1024 /* Finish the lexical context of the last function in the file; pop
1025 the context stack. */
1026
1027 if (context_stack_depth > 0)
1028 {
4359dff1
JK
1029 struct context_stack *cstk = pop_context ();
1030
c906108c
SS
1031 /* Make a block for the local symbols within. */
1032 finish_block (cstk->name, &local_symbols, cstk->old_blocks,
1033 cstk->start_addr, end_addr, objfile);
1034
1035 if (context_stack_depth > 0)
1036 {
1037 /* This is said to happen with SCO. The old coffread.c
1038 code simply emptied the context stack, so we do the
1039 same. FIXME: Find out why it is happening. This is not
1040 believed to happen in most cases (even for coffread.c);
1041 it used to be an abort(). */
23136709 1042 complaint (&symfile_complaints,
3d263c1d 1043 _("Context stack not empty in end_symtab"));
c906108c
SS
1044 context_stack_depth = 0;
1045 }
1046 }
1047
1048 /* Reordered executables may have out of order pending blocks; if
1049 OBJF_REORDERED is true, then sort the pending blocks. */
6d30eef8 1050
c906108c
SS
1051 if ((objfile->flags & OBJF_REORDERED) && pending_blocks)
1052 {
07e7f39f
JK
1053 unsigned count = 0;
1054 struct pending_block *pb;
1055 struct block **barray, **bp;
1056 struct cleanup *back_to;
c906108c 1057
07e7f39f
JK
1058 for (pb = pending_blocks; pb != NULL; pb = pb->next)
1059 count++;
c906108c 1060
07e7f39f
JK
1061 barray = xmalloc (sizeof (*barray) * count);
1062 back_to = make_cleanup (xfree, barray);
c906108c 1063
07e7f39f
JK
1064 bp = barray;
1065 for (pb = pending_blocks; pb != NULL; pb = pb->next)
1066 *bp++ = pb->block;
1067
1068 qsort (barray, count, sizeof (*barray), block_compar);
1069
1070 bp = barray;
1071 for (pb = pending_blocks; pb != NULL; pb = pb->next)
1072 pb->block = *bp++;
1073
1074 do_cleanups (back_to);
c906108c
SS
1075 }
1076
1077 /* Cleanup any undefined types that have been left hanging around
1078 (this needs to be done before the finish_blocks so that
1079 file_symbols is still good).
c5aa993b 1080
0a0edcd5 1081 Both cleanup_undefined_stabs_types and finish_global_stabs are stabs
c906108c
SS
1082 specific, but harmless for other symbol readers, since on gdb
1083 startup or when finished reading stabs, the state is set so these
1084 are no-ops. FIXME: Is this handled right in case of QUIT? Can
1085 we make this cleaner? */
1086
0a0edcd5 1087 cleanup_undefined_stabs_types (objfile);
c906108c
SS
1088 finish_global_stabs (objfile);
1089
36586728
TT
1090 if (!required
1091 && pending_blocks == NULL
c906108c
SS
1092 && file_symbols == NULL
1093 && global_symbols == NULL
99d9066e
JB
1094 && have_line_numbers == 0
1095 && pending_macros == NULL)
c906108c 1096 {
4359dff1
JK
1097 /* Ignore symtabs that have no functions with real debugging info. */
1098 return NULL;
1099 }
1100 else
1101 {
1102 /* Define the STATIC_BLOCK. */
1103 return finish_block_internal (NULL, &file_symbols, NULL,
1104 last_source_start_addr, end_addr, objfile,
1105 0, expandable);
1106 }
1107}
1108
1109/* Implementation of the second part of end_symtab. Pass STATIC_BLOCK
1110 as value returned by end_symtab_get_static_block.
1111
1112 SECTION is the same as for end_symtab: the section number
1113 (in objfile->section_offsets) of the blockvector and linetable.
1114
1115 If EXPANDABLE is non-zero the GLOBAL_BLOCK dictionary is made
1116 expandable. */
1117
1118struct symtab *
1119end_symtab_from_static_block (struct block *static_block,
1120 struct objfile *objfile, int section,
1121 int expandable)
1122{
1123 struct symtab *symtab = NULL;
1124 struct blockvector *blockvector;
1125 struct subfile *subfile;
1126 struct subfile *nextsub;
1127
1128 if (static_block == NULL)
1129 {
1130 /* Ignore symtabs that have no functions with real debugging info. */
c906108c
SS
1131 blockvector = NULL;
1132 }
1133 else
1134 {
4359dff1
JK
1135 CORE_ADDR end_addr = BLOCK_END (static_block);
1136
1137 /* Define after STATIC_BLOCK also GLOBAL_BLOCK, and build the
c906108c 1138 blockvector. */
4359dff1
JK
1139 finish_block_internal (NULL, &global_symbols, NULL,
1140 last_source_start_addr, end_addr, objfile,
1141 1, expandable);
c906108c
SS
1142 blockvector = make_blockvector (objfile);
1143 }
1144
f56ce883
DE
1145 /* Read the line table if it has to be read separately.
1146 This is only used by xcoffread.c. */
c295b2e5 1147 if (objfile->sf->sym_read_linetable != NULL)
f56ce883 1148 objfile->sf->sym_read_linetable (objfile);
c906108c 1149
4584e32e
DE
1150 /* Handle the case where the debug info specifies a different path
1151 for the main source file. It can cause us to lose track of its
1152 line number information. */
1153 watch_main_source_file_lossage ();
1154
c906108c
SS
1155 /* Now create the symtab objects proper, one for each subfile. */
1156 /* (The main file is the last one on the chain.) */
1157
1158 for (subfile = subfiles; subfile; subfile = nextsub)
1159 {
1160 int linetablesize = 0;
1161 symtab = NULL;
1162
4a64f543 1163 /* If we have blocks of symbols, make a symtab. Otherwise, just
c906108c
SS
1164 ignore this file and any line number info in it. */
1165 if (blockvector)
1166 {
1167 if (subfile->line_vector)
1168 {
1169 linetablesize = sizeof (struct linetable) +
1170 subfile->line_vector->nitems * sizeof (struct linetable_entry);
c906108c
SS
1171
1172 /* Like the pending blocks, the line table may be
1173 scrambled in reordered executables. Sort it if
1174 OBJF_REORDERED is true. */
1175 if (objfile->flags & OBJF_REORDERED)
1176 qsort (subfile->line_vector->item,
1177 subfile->line_vector->nitems,
c5aa993b 1178 sizeof (struct linetable_entry), compare_line_numbers);
c906108c
SS
1179 }
1180
1181 /* Now, allocate a symbol table. */
cb1df416
DJ
1182 if (subfile->symtab == NULL)
1183 symtab = allocate_symtab (subfile->name, objfile);
1184 else
1185 symtab = subfile->symtab;
c906108c
SS
1186
1187 /* Fill in its components. */
1188 symtab->blockvector = blockvector;
99d9066e 1189 symtab->macro_table = pending_macros;
c906108c
SS
1190 if (subfile->line_vector)
1191 {
4a64f543 1192 /* Reallocate the line table on the symbol obstack. */
c906108c 1193 symtab->linetable = (struct linetable *)
4a146b47 1194 obstack_alloc (&objfile->objfile_obstack, linetablesize);
c906108c
SS
1195 memcpy (symtab->linetable, subfile->line_vector, linetablesize);
1196 }
1197 else
1198 {
1199 symtab->linetable = NULL;
1200 }
1201 symtab->block_line_section = section;
1202 if (subfile->dirname)
1203 {
4a64f543 1204 /* Reallocate the dirname on the symbol obstack. */
8e96694e
TT
1205 symtab->dirname =
1206 obstack_copy0 (&objfile->objfile_obstack,
1207 subfile->dirname,
1208 strlen (subfile->dirname));
c906108c
SS
1209 }
1210 else
1211 {
1212 symtab->dirname = NULL;
1213 }
c906108c
SS
1214
1215 /* Use whatever language we have been using for this
1216 subfile, not the one that was deduced in allocate_symtab
1217 from the filename. We already did our own deducing when
1218 we created the subfile, and we may have altered our
1219 opinion of what language it is from things we found in
4a64f543 1220 the symbols. */
c906108c
SS
1221 symtab->language = subfile->language;
1222
9182c5bc
JK
1223 /* Save the debug format string (if any) in the symtab. */
1224 symtab->debugformat = subfile->debugformat;
1225
1226 /* Similarly for the producer. */
1227 symtab->producer = subfile->producer;
1228
c906108c
SS
1229 /* All symtabs for the main file and the subfiles share a
1230 blockvector, so we need to clear primary for everything
1231 but the main file. */
db0fec5c 1232 set_symtab_primary (symtab, 0);
c906108c 1233 }
24be086d
JB
1234 else
1235 {
1236 if (subfile->symtab)
1237 {
1238 /* Since we are ignoring that subfile, we also need
1239 to unlink the associated empty symtab that we created.
98cc87bd 1240 Otherwise, we can run into trouble because various parts
24be086d
JB
1241 such as the block-vector are uninitialized whereas
1242 the rest of the code assumes that they are.
1243
1244 We can only unlink the symtab because it was allocated
1245 on the objfile obstack. */
1246 struct symtab *s;
1247
1248 if (objfile->symtabs == subfile->symtab)
1249 objfile->symtabs = objfile->symtabs->next;
1250 else
1251 ALL_OBJFILE_SYMTABS (objfile, s)
1252 if (s->next == subfile->symtab)
1253 {
1254 s->next = s->next->next;
1255 break;
1256 }
1257 subfile->symtab = NULL;
1258 }
1259 }
c906108c
SS
1260 if (subfile->name != NULL)
1261 {
b8c9b27d 1262 xfree ((void *) subfile->name);
c906108c
SS
1263 }
1264 if (subfile->dirname != NULL)
1265 {
b8c9b27d 1266 xfree ((void *) subfile->dirname);
c906108c
SS
1267 }
1268 if (subfile->line_vector != NULL)
1269 {
b8c9b27d 1270 xfree ((void *) subfile->line_vector);
c906108c 1271 }
c906108c
SS
1272
1273 nextsub = subfile->next;
b8c9b27d 1274 xfree ((void *) subfile);
c906108c
SS
1275 }
1276
1277 /* Set this for the main source file. */
1278 if (symtab)
1279 {
db0fec5c 1280 set_symtab_primary (symtab, 1);
84a146c9
TT
1281
1282 if (symtab->blockvector)
1283 {
1284 struct block *b = BLOCKVECTOR_BLOCK (symtab->blockvector,
1285 GLOBAL_BLOCK);
1286
1287 set_block_symtab (b, symtab);
1288 }
c906108c
SS
1289 }
1290
cb1df416
DJ
1291 /* Default any symbols without a specified symtab to the primary
1292 symtab. */
1293 if (blockvector)
1294 {
1295 int block_i;
1296
1297 for (block_i = 0; block_i < BLOCKVECTOR_NBLOCKS (blockvector); block_i++)
1298 {
1299 struct block *block = BLOCKVECTOR_BLOCK (blockvector, block_i);
1300 struct symbol *sym;
1301 struct dict_iterator iter;
1302
4a64f543
MS
1303 /* Inlined functions may have symbols not in the global or
1304 static symbol lists. */
edb3359d
DJ
1305 if (BLOCK_FUNCTION (block) != NULL)
1306 if (SYMBOL_SYMTAB (BLOCK_FUNCTION (block)) == NULL)
1307 SYMBOL_SYMTAB (BLOCK_FUNCTION (block)) = symtab;
1308
8157b174 1309 /* Note that we only want to fix up symbols from the local
98cc87bd
DE
1310 blocks, not blocks coming from included symtabs. That is why
1311 we use ALL_DICT_SYMBOLS here and not ALL_BLOCK_SYMBOLS. */
8157b174 1312 ALL_DICT_SYMBOLS (BLOCK_DICT (block), iter, sym)
cb1df416
DJ
1313 if (SYMBOL_SYMTAB (sym) == NULL)
1314 SYMBOL_SYMTAB (sym) = symtab;
1315 }
1316 }
1317
6d30eef8
DE
1318 reset_symtab_globals ();
1319
1320 return symtab;
1321}
1322
4359dff1
JK
1323/* Finish the symbol definitions for one main source file, close off
1324 all the lexical contexts for that file (creating struct block's for
1325 them), then make the struct symtab for that file and put it in the
1326 list of all such.
1327
1328 END_ADDR is the address of the end of the file's text. SECTION is
1329 the section number (in objfile->section_offsets) of the blockvector
1330 and linetable.
1331
1332 Note that it is possible for end_symtab() to return NULL. In
1333 particular, for the DWARF case at least, it will return NULL when
1334 it finds a compilation unit that has exactly one DIE, a
1335 TAG_compile_unit DIE. This can happen when we link in an object
1336 file that was compiled from an empty source file. Returning NULL
1337 is probably not the correct thing to do, because then gdb will
1338 never know about this empty file (FIXME).
1339
1340 If you need to modify STATIC_BLOCK before it is finalized you should
1341 call end_symtab_get_static_block and end_symtab_from_static_block
1342 yourself. */
6d30eef8
DE
1343
1344struct symtab *
1345end_symtab (CORE_ADDR end_addr, struct objfile *objfile, int section)
1346{
4359dff1
JK
1347 struct block *static_block;
1348
36586728 1349 static_block = end_symtab_get_static_block (end_addr, objfile, 0, 0);
4359dff1 1350 return end_symtab_from_static_block (static_block, objfile, section, 0);
6d30eef8
DE
1351}
1352
4359dff1 1353/* Same as end_symtab except create a symtab that can be later added to. */
6d30eef8
DE
1354
1355struct symtab *
1356end_expandable_symtab (CORE_ADDR end_addr, struct objfile *objfile,
1357 int section)
1358{
4359dff1
JK
1359 struct block *static_block;
1360
36586728 1361 static_block = end_symtab_get_static_block (end_addr, objfile, 1, 0);
4359dff1 1362 return end_symtab_from_static_block (static_block, objfile, section, 1);
6d30eef8
DE
1363}
1364
1365/* Subroutine of augment_type_symtab to simplify it.
1366 Attach SYMTAB to all symbols in PENDING_LIST that don't have one. */
1367
1368static void
1369set_missing_symtab (struct pending *pending_list, struct symtab *symtab)
1370{
1371 struct pending *pending;
1372 int i;
1373
1374 for (pending = pending_list; pending != NULL; pending = pending->next)
801e3a5b 1375 {
6d30eef8
DE
1376 for (i = 0; i < pending->nsyms; ++i)
1377 {
1378 if (SYMBOL_SYMTAB (pending->symbol[i]) == NULL)
1379 SYMBOL_SYMTAB (pending->symbol[i]) = symtab;
1380 }
801e3a5b 1381 }
6d30eef8 1382}
c906108c 1383
6d30eef8
DE
1384/* Same as end_symtab, but for the case where we're adding more symbols
1385 to an existing symtab that is known to contain only type information.
1386 This is the case for DWARF4 Type Units. */
1387
1388void
1389augment_type_symtab (struct objfile *objfile, struct symtab *primary_symtab)
1390{
346d1dfe 1391 const struct blockvector *blockvector = primary_symtab->blockvector;
6d30eef8
DE
1392
1393 if (context_stack_depth > 0)
1394 {
1395 complaint (&symfile_complaints,
1396 _("Context stack not empty in augment_type_symtab"));
1397 context_stack_depth = 0;
1398 }
1399 if (pending_blocks != NULL)
1400 complaint (&symfile_complaints, _("Blocks in a type symtab"));
1401 if (pending_macros != NULL)
1402 complaint (&symfile_complaints, _("Macro in a type symtab"));
1403 if (have_line_numbers)
1404 complaint (&symfile_complaints,
1405 _("Line numbers recorded in a type symtab"));
1406
1407 if (file_symbols != NULL)
1408 {
1409 struct block *block = BLOCKVECTOR_BLOCK (blockvector, STATIC_BLOCK);
1410
1411 /* First mark any symbols without a specified symtab as belonging
1412 to the primary symtab. */
1413 set_missing_symtab (file_symbols, primary_symtab);
1414
1415 dict_add_pending (BLOCK_DICT (block), file_symbols);
1416 }
1417
1418 if (global_symbols != NULL)
1419 {
1420 struct block *block = BLOCKVECTOR_BLOCK (blockvector, GLOBAL_BLOCK);
1421
1422 /* First mark any symbols without a specified symtab as belonging
1423 to the primary symtab. */
1424 set_missing_symtab (global_symbols, primary_symtab);
1425
1426 dict_add_pending (BLOCK_DICT (block), global_symbols);
1427 }
1428
1429 reset_symtab_globals ();
c906108c
SS
1430}
1431
1432/* Push a context block. Args are an identifying nesting level
1433 (checkable when you pop it), and the starting PC address of this
1434 context. */
1435
1436struct context_stack *
1437push_context (int desc, CORE_ADDR valu)
1438{
52f0bd74 1439 struct context_stack *new;
c906108c
SS
1440
1441 if (context_stack_depth == context_stack_size)
1442 {
1443 context_stack_size *= 2;
1444 context_stack = (struct context_stack *)
1445 xrealloc ((char *) context_stack,
c5aa993b 1446 (context_stack_size * sizeof (struct context_stack)));
c906108c
SS
1447 }
1448
1449 new = &context_stack[context_stack_depth++];
1450 new->depth = desc;
1451 new->locals = local_symbols;
c906108c
SS
1452 new->old_blocks = pending_blocks;
1453 new->start_addr = valu;
27aa8d6a 1454 new->using_directives = using_directives;
c906108c
SS
1455 new->name = NULL;
1456
1457 local_symbols = NULL;
27aa8d6a 1458 using_directives = NULL;
c906108c
SS
1459
1460 return new;
1461}
0c5e171a 1462
a672ef13 1463/* Pop a context block. Returns the address of the context block just
4a64f543 1464 popped. */
a672ef13 1465
0c5e171a
KD
1466struct context_stack *
1467pop_context (void)
1468{
1469 gdb_assert (context_stack_depth > 0);
1470 return (&context_stack[--context_stack_depth]);
1471}
1472
c906108c 1473\f
357e46e7 1474
4a64f543 1475/* Compute a small integer hash code for the given name. */
c906108c
SS
1476
1477int
0d5cff50 1478hashname (const char *name)
c906108c 1479{
357e46e7 1480 return (hash(name,strlen(name)) % HASHSIZE);
c906108c
SS
1481}
1482\f
1483
1484void
554d387d 1485record_debugformat (const char *format)
c906108c 1486{
554d387d 1487 current_subfile->debugformat = format;
c906108c
SS
1488}
1489
303b6f5d
DJ
1490void
1491record_producer (const char *producer)
1492{
554d387d 1493 current_subfile->producer = producer;
303b6f5d
DJ
1494}
1495
c906108c
SS
1496/* Merge the first symbol list SRCLIST into the second symbol list
1497 TARGETLIST by repeated calls to add_symbol_to_list(). This
1498 procedure "frees" each link of SRCLIST by adding it to the
1499 free_pendings list. Caller must set SRCLIST to a null list after
1500 calling this function.
1501
4a64f543 1502 Void return. */
c906108c
SS
1503
1504void
1505merge_symbol_lists (struct pending **srclist, struct pending **targetlist)
1506{
52f0bd74 1507 int i;
c906108c
SS
1508
1509 if (!srclist || !*srclist)
1510 return;
1511
1512 /* Merge in elements from current link. */
1513 for (i = 0; i < (*srclist)->nsyms; i++)
1514 add_symbol_to_list ((*srclist)->symbol[i], targetlist);
1515
1516 /* Recurse on next. */
1517 merge_symbol_lists (&(*srclist)->next, targetlist);
1518
1519 /* "Free" the current link. */
1520 (*srclist)->next = free_pendings;
1521 free_pendings = (*srclist);
1522}
1523\f
46212e0b
TT
1524
1525/* Name of source file whose symbol data we are now processing. This
1526 comes from a symbol of type N_SO for stabs. For Dwarf it comes
1527 from the DW_AT_name attribute of a DW_TAG_compile_unit DIE. */
1528
1529static char *last_source_file;
1530
1531/* See buildsym.h. */
1532
1533void
1534set_last_source_file (const char *name)
1535{
1536 xfree (last_source_file);
1537 last_source_file = name == NULL ? NULL : xstrdup (name);
1538}
1539
1540/* See buildsym.h. */
1541
1542const char *
1543get_last_source_file (void)
1544{
1545 return last_source_file;
1546}
1547
1548\f
1549
c906108c
SS
1550/* Initialize anything that needs initializing when starting to read a
1551 fresh piece of a symbol file, e.g. reading in the stuff
1552 corresponding to a psymtab. */
1553
1554void
fba45db2 1555buildsym_init (void)
c906108c
SS
1556{
1557 free_pendings = NULL;
1558 file_symbols = NULL;
1559 global_symbols = NULL;
1560 pending_blocks = NULL;
99d9066e 1561 pending_macros = NULL;
aa14df25 1562 using_directives = NULL;
fc474241 1563 subfile_stack = NULL;
801e3a5b
JB
1564
1565 /* We shouldn't have any address map at this point. */
1566 gdb_assert (! pending_addrmap);
1567 pending_addrmap_interesting = 0;
c906108c
SS
1568}
1569
1570/* Initialize anything that needs initializing when a completely new
1571 symbol file is specified (not just adding some symbols from another
1572 file, e.g. a shared library). */
1573
1574void
fba45db2 1575buildsym_new_init (void)
c906108c
SS
1576{
1577 buildsym_init ();
1578}
This page took 1.183832 seconds and 4 git commands to generate.