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