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