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