[gdb/testsuite] Accept new complex print style in mixed-lang-stack.exp
[deliverable/binutils-gdb.git] / gdb / buildsym.c
1 /* Support routines for building symbol tables in GDB's internal format.
2 Copyright (C) 1986-2020 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
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
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
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.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18
19 #include "defs.h"
20 #include "buildsym-legacy.h"
21 #include "bfd.h"
22 #include "gdb_obstack.h"
23 #include "symtab.h"
24 #include "symfile.h"
25 #include "objfiles.h"
26 #include "gdbtypes.h"
27 #include "complaints.h"
28 #include "expression.h" /* For "enum exp_opcode" used by... */
29 #include "filenames.h" /* For DOSish file names. */
30 #include "macrotab.h"
31 #include "demangle.h" /* Needed by SYMBOL_INIT_DEMANGLED_NAME. */
32 #include "block.h"
33 #include "cp-support.h"
34 #include "dictionary.h"
35 #include "addrmap.h"
36 #include <algorithm>
37
38 /* For cleanup_undefined_stabs_types and finish_global_stabs (somewhat
39 questionable--see comment where we call them). */
40
41 #include "stabsread.h"
42
43 /* List of blocks already made (lexical contexts already closed).
44 This is used at the end to make the blockvector. */
45
46 struct pending_block
47 {
48 struct pending_block *next;
49 struct block *block;
50 };
51
52 /* Initial sizes of data structures. These are realloc'd larger if
53 needed, and realloc'd down to the size actually used, when
54 completed. */
55
56 #define INITIAL_LINE_VECTOR_LENGTH 1000
57 \f
58
59 buildsym_compunit::buildsym_compunit (struct objfile *objfile_,
60 const char *name,
61 const char *comp_dir_,
62 enum language language_,
63 CORE_ADDR last_addr)
64 : m_objfile (objfile_),
65 m_last_source_file (name == nullptr ? nullptr : xstrdup (name)),
66 m_comp_dir (comp_dir_ == nullptr ? nullptr : xstrdup (comp_dir_)),
67 m_language (language_),
68 m_last_source_start_addr (last_addr)
69 {
70 /* Allocate the compunit symtab now. The caller needs it to allocate
71 non-primary symtabs. It is also needed by get_macro_table. */
72 m_compunit_symtab = allocate_compunit_symtab (m_objfile, name);
73
74 /* Build the subfile for NAME (the main source file) so that we can record
75 a pointer to it for later.
76 IMPORTANT: Do not allocate a struct symtab for NAME here.
77 It can happen that the debug info provides a different path to NAME than
78 DIRNAME,NAME. We cope with this in watch_main_source_file_lossage but
79 that only works if the main_subfile doesn't have a symtab yet. */
80 start_subfile (name);
81 /* Save this so that we don't have to go looking for it at the end
82 of the subfiles list. */
83 m_main_subfile = m_current_subfile;
84 }
85
86 buildsym_compunit::~buildsym_compunit ()
87 {
88 struct subfile *subfile, *nextsub;
89
90 if (m_pending_macros != nullptr)
91 free_macro_table (m_pending_macros);
92
93 for (subfile = m_subfiles;
94 subfile != NULL;
95 subfile = nextsub)
96 {
97 nextsub = subfile->next;
98 xfree (subfile->name);
99 xfree (subfile->line_vector);
100 xfree (subfile);
101 }
102
103 struct pending *next, *next1;
104
105 for (next = m_file_symbols; next != NULL; next = next1)
106 {
107 next1 = next->next;
108 xfree ((void *) next);
109 }
110
111 for (next = m_global_symbols; next != NULL; next = next1)
112 {
113 next1 = next->next;
114 xfree ((void *) next);
115 }
116 }
117
118 struct macro_table *
119 buildsym_compunit::get_macro_table ()
120 {
121 if (m_pending_macros == nullptr)
122 m_pending_macros = new_macro_table (&m_objfile->per_bfd->storage_obstack,
123 &m_objfile->per_bfd->string_cache,
124 m_compunit_symtab);
125 return m_pending_macros;
126 }
127
128 /* Maintain the lists of symbols and blocks. */
129
130 /* Add a symbol to one of the lists of symbols. */
131
132 void
133 add_symbol_to_list (struct symbol *symbol, struct pending **listhead)
134 {
135 struct pending *link;
136
137 /* If this is an alias for another symbol, don't add it. */
138 if (symbol->linkage_name () && symbol->linkage_name ()[0] == '#')
139 return;
140
141 /* We keep PENDINGSIZE symbols in each link of the list. If we
142 don't have a link with room in it, add a new link. */
143 if (*listhead == NULL || (*listhead)->nsyms == PENDINGSIZE)
144 {
145 link = XNEW (struct pending);
146 link->next = *listhead;
147 *listhead = link;
148 link->nsyms = 0;
149 }
150
151 (*listhead)->symbol[(*listhead)->nsyms++] = symbol;
152 }
153
154 /* Find a symbol named NAME on a LIST. NAME need not be
155 '\0'-terminated; LENGTH is the length of the name. */
156
157 struct symbol *
158 find_symbol_in_list (struct pending *list, char *name, int length)
159 {
160 int j;
161 const char *pp;
162
163 while (list != NULL)
164 {
165 for (j = list->nsyms; --j >= 0;)
166 {
167 pp = list->symbol[j]->linkage_name ();
168 if (*pp == *name && strncmp (pp, name, length) == 0
169 && pp[length] == '\0')
170 {
171 return (list->symbol[j]);
172 }
173 }
174 list = list->next;
175 }
176 return (NULL);
177 }
178
179 /* Record BLOCK on the list of all blocks in the file. Put it after
180 OPBLOCK, or at the beginning if opblock is NULL. This puts the
181 block in the list after all its subblocks. */
182
183 void
184 buildsym_compunit::record_pending_block (struct block *block,
185 struct pending_block *opblock)
186 {
187 struct pending_block *pblock;
188
189 pblock = XOBNEW (&m_pending_block_obstack, struct pending_block);
190 pblock->block = block;
191 if (opblock)
192 {
193 pblock->next = opblock->next;
194 opblock->next = pblock;
195 }
196 else
197 {
198 pblock->next = m_pending_blocks;
199 m_pending_blocks = pblock;
200 }
201 }
202
203 /* Take one of the lists of symbols and make a block from it. Keep
204 the order the symbols have in the list (reversed from the input
205 file). Put the block on the list of pending blocks. */
206
207 struct block *
208 buildsym_compunit::finish_block_internal
209 (struct symbol *symbol,
210 struct pending **listhead,
211 struct pending_block *old_blocks,
212 const struct dynamic_prop *static_link,
213 CORE_ADDR start, CORE_ADDR end,
214 int is_global, int expandable)
215 {
216 struct gdbarch *gdbarch = get_objfile_arch (m_objfile);
217 struct pending *next, *next1;
218 struct block *block;
219 struct pending_block *pblock;
220 struct pending_block *opblock;
221
222 block = (is_global
223 ? allocate_global_block (&m_objfile->objfile_obstack)
224 : allocate_block (&m_objfile->objfile_obstack));
225
226 if (symbol)
227 {
228 BLOCK_MULTIDICT (block)
229 = mdict_create_linear (&m_objfile->objfile_obstack, *listhead);
230 }
231 else
232 {
233 if (expandable)
234 {
235 BLOCK_MULTIDICT (block) = mdict_create_hashed_expandable (m_language);
236 mdict_add_pending (BLOCK_MULTIDICT (block), *listhead);
237 }
238 else
239 {
240 BLOCK_MULTIDICT (block) =
241 mdict_create_hashed (&m_objfile->objfile_obstack, *listhead);
242 }
243 }
244
245 BLOCK_START (block) = start;
246 BLOCK_END (block) = end;
247
248 /* Put the block in as the value of the symbol that names it. */
249
250 if (symbol)
251 {
252 struct type *ftype = SYMBOL_TYPE (symbol);
253 struct mdict_iterator miter;
254 SYMBOL_BLOCK_VALUE (symbol) = block;
255 BLOCK_FUNCTION (block) = symbol;
256
257 if (TYPE_NFIELDS (ftype) <= 0)
258 {
259 /* No parameter type information is recorded with the
260 function's type. Set that from the type of the
261 parameter symbols. */
262 int nparams = 0, iparams;
263 struct symbol *sym;
264
265 /* Here we want to directly access the dictionary, because
266 we haven't fully initialized the block yet. */
267 ALL_DICT_SYMBOLS (BLOCK_MULTIDICT (block), miter, sym)
268 {
269 if (SYMBOL_IS_ARGUMENT (sym))
270 nparams++;
271 }
272 if (nparams > 0)
273 {
274 TYPE_NFIELDS (ftype) = nparams;
275 TYPE_FIELDS (ftype) = (struct field *)
276 TYPE_ALLOC (ftype, nparams * sizeof (struct field));
277
278 iparams = 0;
279 /* Here we want to directly access the dictionary, because
280 we haven't fully initialized the block yet. */
281 ALL_DICT_SYMBOLS (BLOCK_MULTIDICT (block), miter, sym)
282 {
283 if (iparams == nparams)
284 break;
285
286 if (SYMBOL_IS_ARGUMENT (sym))
287 {
288 TYPE_FIELD_TYPE (ftype, iparams) = SYMBOL_TYPE (sym);
289 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
290 iparams++;
291 }
292 }
293 }
294 }
295 }
296 else
297 {
298 BLOCK_FUNCTION (block) = NULL;
299 }
300
301 if (static_link != NULL)
302 objfile_register_static_link (m_objfile, block, static_link);
303
304 /* Now free the links of the list, and empty the list. */
305
306 for (next = *listhead; next; next = next1)
307 {
308 next1 = next->next;
309 xfree (next);
310 }
311 *listhead = NULL;
312
313 /* Check to be sure that the blocks have an end address that is
314 greater than starting address. */
315
316 if (BLOCK_END (block) < BLOCK_START (block))
317 {
318 if (symbol)
319 {
320 complaint (_("block end address less than block "
321 "start address in %s (patched it)"),
322 symbol->print_name ());
323 }
324 else
325 {
326 complaint (_("block end address %s less than block "
327 "start address %s (patched it)"),
328 paddress (gdbarch, BLOCK_END (block)),
329 paddress (gdbarch, BLOCK_START (block)));
330 }
331 /* Better than nothing. */
332 BLOCK_END (block) = BLOCK_START (block);
333 }
334
335 /* Install this block as the superblock of all blocks made since the
336 start of this scope that don't have superblocks yet. */
337
338 opblock = NULL;
339 for (pblock = m_pending_blocks;
340 pblock && pblock != old_blocks;
341 pblock = pblock->next)
342 {
343 if (BLOCK_SUPERBLOCK (pblock->block) == NULL)
344 {
345 /* Check to be sure the blocks are nested as we receive
346 them. If the compiler/assembler/linker work, this just
347 burns a small amount of time.
348
349 Skip blocks which correspond to a function; they're not
350 physically nested inside this other blocks, only
351 lexically nested. */
352 if (BLOCK_FUNCTION (pblock->block) == NULL
353 && (BLOCK_START (pblock->block) < BLOCK_START (block)
354 || BLOCK_END (pblock->block) > BLOCK_END (block)))
355 {
356 if (symbol)
357 {
358 complaint (_("inner block not inside outer block in %s"),
359 symbol->print_name ());
360 }
361 else
362 {
363 complaint (_("inner block (%s-%s) not "
364 "inside outer block (%s-%s)"),
365 paddress (gdbarch, BLOCK_START (pblock->block)),
366 paddress (gdbarch, BLOCK_END (pblock->block)),
367 paddress (gdbarch, BLOCK_START (block)),
368 paddress (gdbarch, BLOCK_END (block)));
369 }
370 if (BLOCK_START (pblock->block) < BLOCK_START (block))
371 BLOCK_START (pblock->block) = BLOCK_START (block);
372 if (BLOCK_END (pblock->block) > BLOCK_END (block))
373 BLOCK_END (pblock->block) = BLOCK_END (block);
374 }
375 BLOCK_SUPERBLOCK (pblock->block) = block;
376 }
377 opblock = pblock;
378 }
379
380 block_set_using (block,
381 (is_global
382 ? m_global_using_directives
383 : m_local_using_directives),
384 &m_objfile->objfile_obstack);
385 if (is_global)
386 m_global_using_directives = NULL;
387 else
388 m_local_using_directives = NULL;
389
390 record_pending_block (block, opblock);
391
392 return block;
393 }
394
395 struct block *
396 buildsym_compunit::finish_block (struct symbol *symbol,
397 struct pending_block *old_blocks,
398 const struct dynamic_prop *static_link,
399 CORE_ADDR start, CORE_ADDR end)
400 {
401 return finish_block_internal (symbol, &m_local_symbols,
402 old_blocks, static_link, start, end, 0, 0);
403 }
404
405 /* Record that the range of addresses from START to END_INCLUSIVE
406 (inclusive, like it says) belongs to BLOCK. BLOCK's start and end
407 addresses must be set already. You must apply this function to all
408 BLOCK's children before applying it to BLOCK.
409
410 If a call to this function complicates the picture beyond that
411 already provided by BLOCK_START and BLOCK_END, then we create an
412 address map for the block. */
413 void
414 buildsym_compunit::record_block_range (struct block *block,
415 CORE_ADDR start,
416 CORE_ADDR end_inclusive)
417 {
418 /* If this is any different from the range recorded in the block's
419 own BLOCK_START and BLOCK_END, then note that the address map has
420 become interesting. Note that even if this block doesn't have
421 any "interesting" ranges, some later block might, so we still
422 need to record this block in the addrmap. */
423 if (start != BLOCK_START (block)
424 || end_inclusive + 1 != BLOCK_END (block))
425 m_pending_addrmap_interesting = true;
426
427 if (m_pending_addrmap == nullptr)
428 m_pending_addrmap = addrmap_create_mutable (&m_pending_addrmap_obstack);
429
430 addrmap_set_empty (m_pending_addrmap, start, end_inclusive, block);
431 }
432
433 struct blockvector *
434 buildsym_compunit::make_blockvector ()
435 {
436 struct pending_block *next;
437 struct blockvector *blockvector;
438 int i;
439
440 /* Count the length of the list of blocks. */
441
442 for (next = m_pending_blocks, i = 0; next; next = next->next, i++)
443 {
444 }
445
446 blockvector = (struct blockvector *)
447 obstack_alloc (&m_objfile->objfile_obstack,
448 (sizeof (struct blockvector)
449 + (i - 1) * sizeof (struct block *)));
450
451 /* Copy the blocks into the blockvector. This is done in reverse
452 order, which happens to put the blocks into the proper order
453 (ascending starting address). finish_block has hair to insert
454 each block into the list after its subblocks in order to make
455 sure this is true. */
456
457 BLOCKVECTOR_NBLOCKS (blockvector) = i;
458 for (next = m_pending_blocks; next; next = next->next)
459 {
460 BLOCKVECTOR_BLOCK (blockvector, --i) = next->block;
461 }
462
463 free_pending_blocks ();
464
465 /* If we needed an address map for this symtab, record it in the
466 blockvector. */
467 if (m_pending_addrmap != nullptr && m_pending_addrmap_interesting)
468 BLOCKVECTOR_MAP (blockvector)
469 = addrmap_create_fixed (m_pending_addrmap, &m_objfile->objfile_obstack);
470 else
471 BLOCKVECTOR_MAP (blockvector) = 0;
472
473 /* Some compilers output blocks in the wrong order, but we depend on
474 their being in the right order so we can binary search. Check the
475 order and moan about it.
476 Note: Remember that the first two blocks are the global and static
477 blocks. We could special case that fact and begin checking at block 2.
478 To avoid making that assumption we do not. */
479 if (BLOCKVECTOR_NBLOCKS (blockvector) > 1)
480 {
481 for (i = 1; i < BLOCKVECTOR_NBLOCKS (blockvector); i++)
482 {
483 if (BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i - 1))
484 > BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i)))
485 {
486 CORE_ADDR start
487 = BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i));
488
489 complaint (_("block at %s out of order"),
490 hex_string ((LONGEST) start));
491 }
492 }
493 }
494
495 return (blockvector);
496 }
497 \f
498 /* Start recording information about source code that came from an
499 included (or otherwise merged-in) source file with a different
500 name. NAME is the name of the file (cannot be NULL). */
501
502 void
503 buildsym_compunit::start_subfile (const char *name)
504 {
505 const char *subfile_dirname;
506 struct subfile *subfile;
507
508 subfile_dirname = m_comp_dir.get ();
509
510 /* See if this subfile is already registered. */
511
512 for (subfile = m_subfiles; subfile; subfile = subfile->next)
513 {
514 char *subfile_name;
515
516 /* If NAME is an absolute path, and this subfile is not, then
517 attempt to create an absolute path to compare. */
518 if (IS_ABSOLUTE_PATH (name)
519 && !IS_ABSOLUTE_PATH (subfile->name)
520 && subfile_dirname != NULL)
521 subfile_name = concat (subfile_dirname, SLASH_STRING,
522 subfile->name, (char *) NULL);
523 else
524 subfile_name = subfile->name;
525
526 if (FILENAME_CMP (subfile_name, name) == 0)
527 {
528 m_current_subfile = subfile;
529 if (subfile_name != subfile->name)
530 xfree (subfile_name);
531 return;
532 }
533 if (subfile_name != subfile->name)
534 xfree (subfile_name);
535 }
536
537 /* This subfile is not known. Add an entry for it. */
538
539 subfile = XNEW (struct subfile);
540 memset (subfile, 0, sizeof (struct subfile));
541 subfile->buildsym_compunit = this;
542
543 subfile->next = m_subfiles;
544 m_subfiles = subfile;
545
546 m_current_subfile = subfile;
547
548 subfile->name = xstrdup (name);
549
550 /* Initialize line-number recording for this subfile. */
551 subfile->line_vector = NULL;
552
553 /* Default the source language to whatever can be deduced from the
554 filename. If nothing can be deduced (such as for a C/C++ include
555 file with a ".h" extension), then inherit whatever language the
556 previous subfile had. This kludgery is necessary because there
557 is no standard way in some object formats to record the source
558 language. Also, when symtabs are allocated we try to deduce a
559 language then as well, but it is too late for us to use that
560 information while reading symbols, since symtabs aren't allocated
561 until after all the symbols have been processed for a given
562 source file. */
563
564 subfile->language = deduce_language_from_filename (subfile->name);
565 if (subfile->language == language_unknown
566 && subfile->next != NULL)
567 {
568 subfile->language = subfile->next->language;
569 }
570
571 /* If the filename of this subfile ends in .C, then change the
572 language of any pending subfiles from C to C++. We also accept
573 any other C++ suffixes accepted by deduce_language_from_filename. */
574 /* Likewise for f2c. */
575
576 if (subfile->name)
577 {
578 struct subfile *s;
579 enum language sublang = deduce_language_from_filename (subfile->name);
580
581 if (sublang == language_cplus || sublang == language_fortran)
582 for (s = m_subfiles; s != NULL; s = s->next)
583 if (s->language == language_c)
584 s->language = sublang;
585 }
586
587 /* And patch up this file if necessary. */
588 if (subfile->language == language_c
589 && subfile->next != NULL
590 && (subfile->next->language == language_cplus
591 || subfile->next->language == language_fortran))
592 {
593 subfile->language = subfile->next->language;
594 }
595 }
596
597 /* For stabs readers, the first N_SO symbol is assumed to be the
598 source file name, and the subfile struct is initialized using that
599 assumption. If another N_SO symbol is later seen, immediately
600 following the first one, then the first one is assumed to be the
601 directory name and the second one is really the source file name.
602
603 So we have to patch up the subfile struct by moving the old name
604 value to dirname and remembering the new name. Some sanity
605 checking is performed to ensure that the state of the subfile
606 struct is reasonable and that the old name we are assuming to be a
607 directory name actually is (by checking for a trailing '/'). */
608
609 void
610 buildsym_compunit::patch_subfile_names (struct subfile *subfile,
611 const char *name)
612 {
613 if (subfile != NULL
614 && m_comp_dir == NULL
615 && subfile->name != NULL
616 && IS_DIR_SEPARATOR (subfile->name[strlen (subfile->name) - 1]))
617 {
618 m_comp_dir.reset (subfile->name);
619 subfile->name = xstrdup (name);
620 set_last_source_file (name);
621
622 /* Default the source language to whatever can be deduced from
623 the filename. If nothing can be deduced (such as for a C/C++
624 include file with a ".h" extension), then inherit whatever
625 language the previous subfile had. This kludgery is
626 necessary because there is no standard way in some object
627 formats to record the source language. Also, when symtabs
628 are allocated we try to deduce a language then as well, but
629 it is too late for us to use that information while reading
630 symbols, since symtabs aren't allocated until after all the
631 symbols have been processed for a given source file. */
632
633 subfile->language = deduce_language_from_filename (subfile->name);
634 if (subfile->language == language_unknown
635 && subfile->next != NULL)
636 {
637 subfile->language = subfile->next->language;
638 }
639 }
640 }
641 \f
642 /* Handle the N_BINCL and N_EINCL symbol types that act like N_SOL for
643 switching source files (different subfiles, as we call them) within
644 one object file, but using a stack rather than in an arbitrary
645 order. */
646
647 void
648 buildsym_compunit::push_subfile ()
649 {
650 gdb_assert (m_current_subfile != NULL);
651 gdb_assert (m_current_subfile->name != NULL);
652 m_subfile_stack.push_back (m_current_subfile->name);
653 }
654
655 const char *
656 buildsym_compunit::pop_subfile ()
657 {
658 gdb_assert (!m_subfile_stack.empty ());
659 const char *name = m_subfile_stack.back ();
660 m_subfile_stack.pop_back ();
661 return name;
662 }
663 \f
664 /* Add a linetable entry for line number LINE and address PC to the
665 line vector for SUBFILE. */
666
667 void
668 buildsym_compunit::record_line (struct subfile *subfile, int line,
669 CORE_ADDR pc, bool is_stmt)
670 {
671 struct linetable_entry *e;
672
673 /* Make sure line vector exists and is big enough. */
674 if (!subfile->line_vector)
675 {
676 subfile->line_vector_length = INITIAL_LINE_VECTOR_LENGTH;
677 subfile->line_vector = (struct linetable *)
678 xmalloc (sizeof (struct linetable)
679 + subfile->line_vector_length * sizeof (struct linetable_entry));
680 subfile->line_vector->nitems = 0;
681 m_have_line_numbers = true;
682 }
683
684 if (subfile->line_vector->nitems > 0)
685 {
686 /* If we have a duplicate for the previous entry then ignore the new
687 entry, except, if the new entry is setting the is_stmt flag, then
688 ensure the previous entry respects the new setting. */
689 e = subfile->line_vector->item + subfile->line_vector->nitems - 1;
690 if (e->line == line && e->pc == pc)
691 {
692 if (is_stmt && !e->is_stmt)
693 e->is_stmt = 1;
694 return;
695 }
696 }
697
698 if (subfile->line_vector->nitems >= subfile->line_vector_length)
699 {
700 subfile->line_vector_length *= 2;
701 subfile->line_vector = (struct linetable *)
702 xrealloc ((char *) subfile->line_vector,
703 (sizeof (struct linetable)
704 + (subfile->line_vector_length
705 * sizeof (struct linetable_entry))));
706 }
707
708 /* The end of sequence marker is special. We need to reset the
709 is_stmt flag on previous lines at the same PC, otherwise these
710 lines may cause problems since they might be at the same address
711 as the following function. For instance suppose a function calls
712 abort there is no reason to emit a ret after that point (no joke).
713 So the label may be at the same address where the following
714 function begins. A similar problem appears if a label is at the
715 same address where an inline function ends we cannot reliably tell
716 if this is considered part of the inline function or the calling
717 program or even the next inline function, so stack traces may
718 give surprising results. Expect gdb.cp/step-and-next-inline.exp
719 to fail if these lines are not modified here. */
720 if (line == 0 && subfile->line_vector->nitems > 0)
721 {
722 e = subfile->line_vector->item + subfile->line_vector->nitems;
723 do
724 {
725 e--;
726 if (e->pc != pc || e->line == 0)
727 break;
728 e->is_stmt = 0;
729 }
730 while (e > subfile->line_vector->item);
731 }
732
733 e = subfile->line_vector->item + subfile->line_vector->nitems++;
734 e->line = line;
735 e->is_stmt = is_stmt ? 1 : 0;
736 e->pc = pc;
737 }
738
739 \f
740 /* Subroutine of end_symtab to simplify it. Look for a subfile that
741 matches the main source file's basename. If there is only one, and
742 if the main source file doesn't have any symbol or line number
743 information, then copy this file's symtab and line_vector to the
744 main source file's subfile and discard the other subfile. This can
745 happen because of a compiler bug or from the user playing games
746 with #line or from things like a distributed build system that
747 manipulates the debug info. This can also happen from an innocent
748 symlink in the paths, we don't canonicalize paths here. */
749
750 void
751 buildsym_compunit::watch_main_source_file_lossage ()
752 {
753 struct subfile *mainsub, *subfile;
754
755 /* Get the main source file. */
756 mainsub = m_main_subfile;
757
758 /* If the main source file doesn't have any line number or symbol
759 info, look for an alias in another subfile. */
760
761 if (mainsub->line_vector == NULL
762 && mainsub->symtab == NULL)
763 {
764 const char *mainbase = lbasename (mainsub->name);
765 int nr_matches = 0;
766 struct subfile *prevsub;
767 struct subfile *mainsub_alias = NULL;
768 struct subfile *prev_mainsub_alias = NULL;
769
770 prevsub = NULL;
771 for (subfile = m_subfiles;
772 subfile != NULL;
773 subfile = subfile->next)
774 {
775 if (subfile == mainsub)
776 continue;
777 if (filename_cmp (lbasename (subfile->name), mainbase) == 0)
778 {
779 ++nr_matches;
780 mainsub_alias = subfile;
781 prev_mainsub_alias = prevsub;
782 }
783 prevsub = subfile;
784 }
785
786 if (nr_matches == 1)
787 {
788 gdb_assert (mainsub_alias != NULL && mainsub_alias != mainsub);
789
790 /* Found a match for the main source file.
791 Copy its line_vector and symtab to the main subfile
792 and then discard it. */
793
794 mainsub->line_vector = mainsub_alias->line_vector;
795 mainsub->line_vector_length = mainsub_alias->line_vector_length;
796 mainsub->symtab = mainsub_alias->symtab;
797
798 if (prev_mainsub_alias == NULL)
799 m_subfiles = mainsub_alias->next;
800 else
801 prev_mainsub_alias->next = mainsub_alias->next;
802 xfree (mainsub_alias->name);
803 xfree (mainsub_alias);
804 }
805 }
806 }
807
808 /* Implementation of the first part of end_symtab. It allows modifying
809 STATIC_BLOCK before it gets finalized by end_symtab_from_static_block.
810 If the returned value is NULL there is no blockvector created for
811 this symtab (you still must call end_symtab_from_static_block).
812
813 END_ADDR is the same as for end_symtab: the address of the end of the
814 file's text.
815
816 If EXPANDABLE is non-zero the STATIC_BLOCK dictionary is made
817 expandable.
818
819 If REQUIRED is non-zero, then a symtab is created even if it does
820 not contain any symbols. */
821
822 struct block *
823 buildsym_compunit::end_symtab_get_static_block (CORE_ADDR end_addr,
824 int expandable, int required)
825 {
826 /* Finish the lexical context of the last function in the file; pop
827 the context stack. */
828
829 if (!m_context_stack.empty ())
830 {
831 struct context_stack cstk = pop_context ();
832
833 /* Make a block for the local symbols within. */
834 finish_block (cstk.name, cstk.old_blocks, NULL,
835 cstk.start_addr, end_addr);
836
837 if (!m_context_stack.empty ())
838 {
839 /* This is said to happen with SCO. The old coffread.c
840 code simply emptied the context stack, so we do the
841 same. FIXME: Find out why it is happening. This is not
842 believed to happen in most cases (even for coffread.c);
843 it used to be an abort(). */
844 complaint (_("Context stack not empty in end_symtab"));
845 m_context_stack.clear ();
846 }
847 }
848
849 /* Reordered executables may have out of order pending blocks; if
850 OBJF_REORDERED is true, then sort the pending blocks. */
851
852 if ((m_objfile->flags & OBJF_REORDERED) && m_pending_blocks)
853 {
854 struct pending_block *pb;
855
856 std::vector<block *> barray;
857
858 for (pb = m_pending_blocks; pb != NULL; pb = pb->next)
859 barray.push_back (pb->block);
860
861 /* Sort blocks by start address in descending order. Blocks with the
862 same start address must remain in the original order to preserve
863 inline function caller/callee relationships. */
864 std::stable_sort (barray.begin (), barray.end (),
865 [] (const block *a, const block *b)
866 {
867 return BLOCK_START (a) > BLOCK_START (b);
868 });
869
870 int i = 0;
871 for (pb = m_pending_blocks; pb != NULL; pb = pb->next)
872 pb->block = barray[i++];
873 }
874
875 /* Cleanup any undefined types that have been left hanging around
876 (this needs to be done before the finish_blocks so that
877 file_symbols is still good).
878
879 Both cleanup_undefined_stabs_types and finish_global_stabs are stabs
880 specific, but harmless for other symbol readers, since on gdb
881 startup or when finished reading stabs, the state is set so these
882 are no-ops. FIXME: Is this handled right in case of QUIT? Can
883 we make this cleaner? */
884
885 cleanup_undefined_stabs_types (m_objfile);
886 finish_global_stabs (m_objfile);
887
888 if (!required
889 && m_pending_blocks == NULL
890 && m_file_symbols == NULL
891 && m_global_symbols == NULL
892 && !m_have_line_numbers
893 && m_pending_macros == NULL
894 && m_global_using_directives == NULL)
895 {
896 /* Ignore symtabs that have no functions with real debugging info. */
897 return NULL;
898 }
899 else
900 {
901 /* Define the STATIC_BLOCK. */
902 return finish_block_internal (NULL, get_file_symbols (), NULL, NULL,
903 m_last_source_start_addr,
904 end_addr, 0, expandable);
905 }
906 }
907
908 /* Subroutine of end_symtab_from_static_block to simplify it.
909 Handle the "have blockvector" case.
910 See end_symtab_from_static_block for a description of the arguments. */
911
912 struct compunit_symtab *
913 buildsym_compunit::end_symtab_with_blockvector (struct block *static_block,
914 int section, int expandable)
915 {
916 struct compunit_symtab *cu = m_compunit_symtab;
917 struct blockvector *blockvector;
918 struct subfile *subfile;
919 CORE_ADDR end_addr;
920
921 gdb_assert (static_block != NULL);
922 gdb_assert (m_subfiles != NULL);
923
924 end_addr = BLOCK_END (static_block);
925
926 /* Create the GLOBAL_BLOCK and build the blockvector. */
927 finish_block_internal (NULL, get_global_symbols (), NULL, NULL,
928 m_last_source_start_addr, end_addr,
929 1, expandable);
930 blockvector = make_blockvector ();
931
932 /* Read the line table if it has to be read separately.
933 This is only used by xcoffread.c. */
934 if (m_objfile->sf->sym_read_linetable != NULL)
935 m_objfile->sf->sym_read_linetable (m_objfile);
936
937 /* Handle the case where the debug info specifies a different path
938 for the main source file. It can cause us to lose track of its
939 line number information. */
940 watch_main_source_file_lossage ();
941
942 /* Now create the symtab objects proper, if not already done,
943 one for each subfile. */
944
945 for (subfile = m_subfiles;
946 subfile != NULL;
947 subfile = subfile->next)
948 {
949 int linetablesize = 0;
950
951 if (subfile->line_vector)
952 {
953 linetablesize = sizeof (struct linetable) +
954 subfile->line_vector->nitems * sizeof (struct linetable_entry);
955
956 const auto lte_is_less_than
957 = [] (const linetable_entry &ln1,
958 const linetable_entry &ln2) -> bool
959 {
960 return (ln1.pc < ln2.pc);
961 };
962
963 /* Like the pending blocks, the line table may be scrambled in
964 reordered executables. Sort it if OBJF_REORDERED is true. It
965 is important to preserve the order of lines at the same
966 address, as this maintains the inline function caller/callee
967 relationships, this is why std::stable_sort is used. */
968 if (m_objfile->flags & OBJF_REORDERED)
969 std::stable_sort (subfile->line_vector->item,
970 subfile->line_vector->item
971 + subfile->line_vector->nitems,
972 lte_is_less_than);
973 }
974
975 /* Allocate a symbol table if necessary. */
976 if (subfile->symtab == NULL)
977 subfile->symtab = allocate_symtab (cu, subfile->name);
978 struct symtab *symtab = subfile->symtab;
979
980 /* Fill in its components. */
981
982 if (subfile->line_vector)
983 {
984 /* Reallocate the line table on the symbol obstack. */
985 SYMTAB_LINETABLE (symtab) = (struct linetable *)
986 obstack_alloc (&m_objfile->objfile_obstack, linetablesize);
987 memcpy (SYMTAB_LINETABLE (symtab), subfile->line_vector,
988 linetablesize);
989 }
990 else
991 {
992 SYMTAB_LINETABLE (symtab) = NULL;
993 }
994
995 /* Use whatever language we have been using for this
996 subfile, not the one that was deduced in allocate_symtab
997 from the filename. We already did our own deducing when
998 we created the subfile, and we may have altered our
999 opinion of what language it is from things we found in
1000 the symbols. */
1001 symtab->language = subfile->language;
1002 }
1003
1004 /* Make sure the symtab of main_subfile is the first in its list. */
1005 {
1006 struct symtab *main_symtab, *prev_symtab;
1007
1008 main_symtab = m_main_subfile->symtab;
1009 prev_symtab = NULL;
1010 for (symtab *symtab : compunit_filetabs (cu))
1011 {
1012 if (symtab == main_symtab)
1013 {
1014 if (prev_symtab != NULL)
1015 {
1016 prev_symtab->next = main_symtab->next;
1017 main_symtab->next = COMPUNIT_FILETABS (cu);
1018 COMPUNIT_FILETABS (cu) = main_symtab;
1019 }
1020 break;
1021 }
1022 prev_symtab = symtab;
1023 }
1024 gdb_assert (main_symtab == COMPUNIT_FILETABS (cu));
1025 }
1026
1027 /* Fill out the compunit symtab. */
1028
1029 if (m_comp_dir != NULL)
1030 {
1031 /* Reallocate the dirname on the symbol obstack. */
1032 const char *comp_dir = m_comp_dir.get ();
1033 COMPUNIT_DIRNAME (cu) = obstack_strdup (&m_objfile->objfile_obstack,
1034 comp_dir);
1035 }
1036
1037 /* Save the debug format string (if any) in the symtab. */
1038 COMPUNIT_DEBUGFORMAT (cu) = m_debugformat;
1039
1040 /* Similarly for the producer. */
1041 COMPUNIT_PRODUCER (cu) = m_producer;
1042
1043 COMPUNIT_BLOCKVECTOR (cu) = blockvector;
1044 {
1045 struct block *b = BLOCKVECTOR_BLOCK (blockvector, GLOBAL_BLOCK);
1046
1047 set_block_compunit_symtab (b, cu);
1048 }
1049
1050 COMPUNIT_BLOCK_LINE_SECTION (cu) = section;
1051
1052 COMPUNIT_MACRO_TABLE (cu) = release_macros ();
1053
1054 /* Default any symbols without a specified symtab to the primary symtab. */
1055 {
1056 int block_i;
1057
1058 /* The main source file's symtab. */
1059 struct symtab *symtab = COMPUNIT_FILETABS (cu);
1060
1061 for (block_i = 0; block_i < BLOCKVECTOR_NBLOCKS (blockvector); block_i++)
1062 {
1063 struct block *block = BLOCKVECTOR_BLOCK (blockvector, block_i);
1064 struct symbol *sym;
1065 struct mdict_iterator miter;
1066
1067 /* Inlined functions may have symbols not in the global or
1068 static symbol lists. */
1069 if (BLOCK_FUNCTION (block) != NULL)
1070 if (symbol_symtab (BLOCK_FUNCTION (block)) == NULL)
1071 symbol_set_symtab (BLOCK_FUNCTION (block), symtab);
1072
1073 /* Note that we only want to fix up symbols from the local
1074 blocks, not blocks coming from included symtabs. That is why
1075 we use ALL_DICT_SYMBOLS here and not ALL_BLOCK_SYMBOLS. */
1076 ALL_DICT_SYMBOLS (BLOCK_MULTIDICT (block), miter, sym)
1077 if (symbol_symtab (sym) == NULL)
1078 symbol_set_symtab (sym, symtab);
1079 }
1080 }
1081
1082 add_compunit_symtab_to_objfile (cu);
1083
1084 return cu;
1085 }
1086
1087 /* Implementation of the second part of end_symtab. Pass STATIC_BLOCK
1088 as value returned by end_symtab_get_static_block.
1089
1090 SECTION is the same as for end_symtab: the section number
1091 (in objfile->section_offsets) of the blockvector and linetable.
1092
1093 If EXPANDABLE is non-zero the GLOBAL_BLOCK dictionary is made
1094 expandable. */
1095
1096 struct compunit_symtab *
1097 buildsym_compunit::end_symtab_from_static_block (struct block *static_block,
1098 int section, int expandable)
1099 {
1100 struct compunit_symtab *cu;
1101
1102 if (static_block == NULL)
1103 {
1104 /* Handle the "no blockvector" case.
1105 When this happens there is nothing to record, so there's nothing
1106 to do: memory will be freed up later.
1107
1108 Note: We won't be adding a compunit to the objfile's list of
1109 compunits, so there's nothing to unchain. However, since each symtab
1110 is added to the objfile's obstack we can't free that space.
1111 We could do better, but this is believed to be a sufficiently rare
1112 event. */
1113 cu = NULL;
1114 }
1115 else
1116 cu = end_symtab_with_blockvector (static_block, section, expandable);
1117
1118 return cu;
1119 }
1120
1121 /* Finish the symbol definitions for one main source file, close off
1122 all the lexical contexts for that file (creating struct block's for
1123 them), then make the struct symtab for that file and put it in the
1124 list of all such.
1125
1126 END_ADDR is the address of the end of the file's text. SECTION is
1127 the section number (in objfile->section_offsets) of the blockvector
1128 and linetable.
1129
1130 Note that it is possible for end_symtab() to return NULL. In
1131 particular, for the DWARF case at least, it will return NULL when
1132 it finds a compilation unit that has exactly one DIE, a
1133 TAG_compile_unit DIE. This can happen when we link in an object
1134 file that was compiled from an empty source file. Returning NULL
1135 is probably not the correct thing to do, because then gdb will
1136 never know about this empty file (FIXME).
1137
1138 If you need to modify STATIC_BLOCK before it is finalized you should
1139 call end_symtab_get_static_block and end_symtab_from_static_block
1140 yourself. */
1141
1142 struct compunit_symtab *
1143 buildsym_compunit::end_symtab (CORE_ADDR end_addr, int section)
1144 {
1145 struct block *static_block;
1146
1147 static_block = end_symtab_get_static_block (end_addr, 0, 0);
1148 return end_symtab_from_static_block (static_block, section, 0);
1149 }
1150
1151 /* Same as end_symtab except create a symtab that can be later added to. */
1152
1153 struct compunit_symtab *
1154 buildsym_compunit::end_expandable_symtab (CORE_ADDR end_addr, int section)
1155 {
1156 struct block *static_block;
1157
1158 static_block = end_symtab_get_static_block (end_addr, 1, 0);
1159 return end_symtab_from_static_block (static_block, section, 1);
1160 }
1161
1162 /* Subroutine of augment_type_symtab to simplify it.
1163 Attach the main source file's symtab to all symbols in PENDING_LIST that
1164 don't have one. */
1165
1166 static void
1167 set_missing_symtab (struct pending *pending_list,
1168 struct compunit_symtab *cu)
1169 {
1170 struct pending *pending;
1171 int i;
1172
1173 for (pending = pending_list; pending != NULL; pending = pending->next)
1174 {
1175 for (i = 0; i < pending->nsyms; ++i)
1176 {
1177 if (symbol_symtab (pending->symbol[i]) == NULL)
1178 symbol_set_symtab (pending->symbol[i], COMPUNIT_FILETABS (cu));
1179 }
1180 }
1181 }
1182
1183 /* Same as end_symtab, but for the case where we're adding more symbols
1184 to an existing symtab that is known to contain only type information.
1185 This is the case for DWARF4 Type Units. */
1186
1187 void
1188 buildsym_compunit::augment_type_symtab ()
1189 {
1190 struct compunit_symtab *cust = m_compunit_symtab;
1191 const struct blockvector *blockvector = COMPUNIT_BLOCKVECTOR (cust);
1192
1193 if (!m_context_stack.empty ())
1194 complaint (_("Context stack not empty in augment_type_symtab"));
1195 if (m_pending_blocks != NULL)
1196 complaint (_("Blocks in a type symtab"));
1197 if (m_pending_macros != NULL)
1198 complaint (_("Macro in a type symtab"));
1199 if (m_have_line_numbers)
1200 complaint (_("Line numbers recorded in a type symtab"));
1201
1202 if (m_file_symbols != NULL)
1203 {
1204 struct block *block = BLOCKVECTOR_BLOCK (blockvector, STATIC_BLOCK);
1205
1206 /* First mark any symbols without a specified symtab as belonging
1207 to the primary symtab. */
1208 set_missing_symtab (m_file_symbols, cust);
1209
1210 mdict_add_pending (BLOCK_MULTIDICT (block), m_file_symbols);
1211 }
1212
1213 if (m_global_symbols != NULL)
1214 {
1215 struct block *block = BLOCKVECTOR_BLOCK (blockvector, GLOBAL_BLOCK);
1216
1217 /* First mark any symbols without a specified symtab as belonging
1218 to the primary symtab. */
1219 set_missing_symtab (m_global_symbols, cust);
1220
1221 mdict_add_pending (BLOCK_MULTIDICT (block),
1222 m_global_symbols);
1223 }
1224 }
1225
1226 /* Push a context block. Args are an identifying nesting level
1227 (checkable when you pop it), and the starting PC address of this
1228 context. */
1229
1230 struct context_stack *
1231 buildsym_compunit::push_context (int desc, CORE_ADDR valu)
1232 {
1233 m_context_stack.emplace_back ();
1234 struct context_stack *newobj = &m_context_stack.back ();
1235
1236 newobj->depth = desc;
1237 newobj->locals = m_local_symbols;
1238 newobj->old_blocks = m_pending_blocks;
1239 newobj->start_addr = valu;
1240 newobj->local_using_directives = m_local_using_directives;
1241 newobj->name = NULL;
1242
1243 m_local_symbols = NULL;
1244 m_local_using_directives = NULL;
1245
1246 return newobj;
1247 }
1248
1249 /* Pop a context block. Returns the address of the context block just
1250 popped. */
1251
1252 struct context_stack
1253 buildsym_compunit::pop_context ()
1254 {
1255 gdb_assert (!m_context_stack.empty ());
1256 struct context_stack result = m_context_stack.back ();
1257 m_context_stack.pop_back ();
1258 return result;
1259 }
This page took 0.11123 seconds and 4 git commands to generate.