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