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