gdb: change subfile::name and buildsym_compunit::m_comp_dir to strings
[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 "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 ? "" : 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->line_vector);
99 delete subfile;
100 }
101
102 struct pending *next, *next1;
103
104 for (next = m_file_symbols; next != NULL; next = next1)
105 {
106 next1 = next->next;
107 xfree ((void *) next);
108 }
109
110 for (next = m_global_symbols; next != NULL; next = next1)
111 {
112 next1 = next->next;
113 xfree ((void *) next);
114 }
115 }
116
117 struct macro_table *
118 buildsym_compunit::get_macro_table ()
119 {
120 if (m_pending_macros == nullptr)
121 m_pending_macros = new_macro_table (&m_objfile->per_bfd->storage_obstack,
122 &m_objfile->per_bfd->string_cache,
123 m_compunit_symtab);
124 return m_pending_macros;
125 }
126
127 /* Maintain the lists of symbols and blocks. */
128
129 /* Add a symbol to one of the lists of symbols. */
130
131 void
132 add_symbol_to_list (struct symbol *symbol, struct pending **listhead)
133 {
134 struct pending *link;
135
136 /* If this is an alias for another symbol, don't add it. */
137 if (symbol->linkage_name () && symbol->linkage_name ()[0] == '#')
138 return;
139
140 /* We keep PENDINGSIZE symbols in each link of the list. If we
141 don't have a link with room in it, add a new link. */
142 if (*listhead == NULL || (*listhead)->nsyms == PENDINGSIZE)
143 {
144 link = XNEW (struct pending);
145 link->next = *listhead;
146 *listhead = link;
147 link->nsyms = 0;
148 }
149
150 (*listhead)->symbol[(*listhead)->nsyms++] = symbol;
151 }
152
153 /* Find a symbol named NAME on a LIST. NAME need not be
154 '\0'-terminated; LENGTH is the length of the name. */
155
156 struct symbol *
157 find_symbol_in_list (struct pending *list, char *name, int length)
158 {
159 int j;
160 const char *pp;
161
162 while (list != NULL)
163 {
164 for (j = list->nsyms; --j >= 0;)
165 {
166 pp = list->symbol[j]->linkage_name ();
167 if (*pp == *name && strncmp (pp, name, length) == 0
168 && pp[length] == '\0')
169 {
170 return (list->symbol[j]);
171 }
172 }
173 list = list->next;
174 }
175 return (NULL);
176 }
177
178 /* Record BLOCK on the list of all blocks in the file. Put it after
179 OPBLOCK, or at the beginning if opblock is NULL. This puts the
180 block in the list after all its subblocks. */
181
182 void
183 buildsym_compunit::record_pending_block (struct block *block,
184 struct pending_block *opblock)
185 {
186 struct pending_block *pblock;
187
188 pblock = XOBNEW (&m_pending_block_obstack, struct pending_block);
189 pblock->block = block;
190 if (opblock)
191 {
192 pblock->next = opblock->next;
193 opblock->next = pblock;
194 }
195 else
196 {
197 pblock->next = m_pending_blocks;
198 m_pending_blocks = pblock;
199 }
200 }
201
202 /* Take one of the lists of symbols and make a block from it. Keep
203 the order the symbols have in the list (reversed from the input
204 file). Put the block on the list of pending blocks. */
205
206 struct block *
207 buildsym_compunit::finish_block_internal
208 (struct symbol *symbol,
209 struct pending **listhead,
210 struct pending_block *old_blocks,
211 const struct dynamic_prop *static_link,
212 CORE_ADDR start, CORE_ADDR end,
213 int is_global, int expandable)
214 {
215 struct gdbarch *gdbarch = m_objfile->arch ();
216 struct pending *next, *next1;
217 struct block *block;
218 struct pending_block *pblock;
219 struct pending_block *opblock;
220
221 block = (is_global
222 ? allocate_global_block (&m_objfile->objfile_obstack)
223 : allocate_block (&m_objfile->objfile_obstack));
224
225 if (symbol)
226 {
227 BLOCK_MULTIDICT (block)
228 = mdict_create_linear (&m_objfile->objfile_obstack, *listhead);
229 }
230 else
231 {
232 if (expandable)
233 {
234 BLOCK_MULTIDICT (block) = mdict_create_hashed_expandable (m_language);
235 mdict_add_pending (BLOCK_MULTIDICT (block), *listhead);
236 }
237 else
238 {
239 BLOCK_MULTIDICT (block) =
240 mdict_create_hashed (&m_objfile->objfile_obstack, *listhead);
241 }
242 }
243
244 BLOCK_START (block) = start;
245 BLOCK_END (block) = end;
246
247 /* Put the block in as the value of the symbol that names it. */
248
249 if (symbol)
250 {
251 struct type *ftype = symbol->type ();
252 struct mdict_iterator miter;
253 symbol->set_value_block (block);
254 BLOCK_FUNCTION (block) = symbol;
255
256 if (ftype->num_fields () <= 0)
257 {
258 /* No parameter type information is recorded with the
259 function's type. Set that from the type of the
260 parameter symbols. */
261 int nparams = 0, iparams;
262 struct symbol *sym;
263
264 /* Here we want to directly access the dictionary, because
265 we haven't fully initialized the block yet. */
266 ALL_DICT_SYMBOLS (BLOCK_MULTIDICT (block), miter, sym)
267 {
268 if (sym->is_argument ())
269 nparams++;
270 }
271 if (nparams > 0)
272 {
273 ftype->set_num_fields (nparams);
274 ftype->set_fields
275 ((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 (sym->is_argument ())
287 {
288 ftype->field (iparams).set_type (sym->type ());
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 /* See if this subfile is already registered. */
506
507 for (subfile *subfile = m_subfiles; subfile; subfile = subfile->next)
508 {
509 std::string subfile_name_holder;
510 const char *subfile_name;
511
512 /* If NAME is an absolute path, and this subfile is not, then
513 attempt to create an absolute path to compare. */
514 if (IS_ABSOLUTE_PATH (name)
515 && !IS_ABSOLUTE_PATH (subfile->name)
516 && !m_comp_dir.empty ())
517 {
518 subfile_name_holder = string_printf ("%s/%s", m_comp_dir.c_str (),
519 subfile->name.c_str ());
520 subfile_name = subfile_name_holder.c_str ();
521 }
522 else
523 subfile_name = subfile->name.c_str ();
524
525 if (FILENAME_CMP (subfile_name, name) == 0)
526 {
527 m_current_subfile = subfile;
528 return;
529 }
530 }
531
532 /* This subfile is not known. Add an entry for it. */
533
534 subfile_up subfile (new struct subfile);
535 subfile->name = name;
536
537 m_current_subfile = subfile.get ();
538
539 /* Initialize line-number recording for this subfile. */
540 subfile->line_vector = NULL;
541
542 /* Default the source language to whatever can be deduced from the
543 filename. If nothing can be deduced (such as for a C/C++ include
544 file with a ".h" extension), then inherit whatever language the
545 previous subfile had. This kludgery is necessary because there
546 is no standard way in some object formats to record the source
547 language. Also, when symtabs are allocated we try to deduce a
548 language then as well, but it is too late for us to use that
549 information while reading symbols, since symtabs aren't allocated
550 until after all the symbols have been processed for a given
551 source file. */
552
553 subfile->language = deduce_language_from_filename (subfile->name.c_str ());
554 if (subfile->language == language_unknown && m_subfiles != nullptr)
555 subfile->language = m_subfiles->language;
556
557 /* If the filename of this subfile ends in .C, then change the
558 language of any pending subfiles from C to C++. We also accept
559 any other C++ suffixes accepted by deduce_language_from_filename. */
560 /* Likewise for f2c. */
561
562 if (!subfile->name.empty ())
563 {
564 struct subfile *s;
565 language sublang = deduce_language_from_filename (subfile->name.c_str ());
566
567 if (sublang == language_cplus || sublang == language_fortran)
568 for (s = m_subfiles; s != NULL; s = s->next)
569 if (s->language == language_c)
570 s->language = sublang;
571 }
572
573 /* And patch up this file if necessary. */
574 if (subfile->language == language_c
575 && m_subfiles != nullptr
576 && (m_subfiles->language == language_cplus
577 || m_subfiles->language == language_fortran))
578 subfile->language = m_subfiles->language;
579
580 /* Link this subfile at the front of the subfile list. */
581 subfile->next = m_subfiles;
582 m_subfiles = subfile.release ();
583 }
584
585 /* For stabs readers, the first N_SO symbol is assumed to be the
586 source file name, and the subfile struct is initialized using that
587 assumption. If another N_SO symbol is later seen, immediately
588 following the first one, then the first one is assumed to be the
589 directory name and the second one is really the source file name.
590
591 So we have to patch up the subfile struct by moving the old name
592 value to dirname and remembering the new name. Some sanity
593 checking is performed to ensure that the state of the subfile
594 struct is reasonable and that the old name we are assuming to be a
595 directory name actually is (by checking for a trailing '/'). */
596
597 void
598 buildsym_compunit::patch_subfile_names (struct subfile *subfile,
599 const char *name)
600 {
601 if (subfile != NULL
602 && m_comp_dir.empty ()
603 && !subfile->name.empty ()
604 && IS_DIR_SEPARATOR (subfile->name.back ()))
605 {
606 m_comp_dir = std::move (subfile->name);
607 subfile->name = name;
608 set_last_source_file (name);
609
610 /* Default the source language to whatever can be deduced from
611 the filename. If nothing can be deduced (such as for a C/C++
612 include file with a ".h" extension), then inherit whatever
613 language the previous subfile had. This kludgery is
614 necessary because there is no standard way in some object
615 formats to record the source language. Also, when symtabs
616 are allocated we try to deduce a language then as well, but
617 it is too late for us to use that information while reading
618 symbols, since symtabs aren't allocated until after all the
619 symbols have been processed for a given source file. */
620
621 subfile->language
622 = deduce_language_from_filename (subfile->name.c_str ());
623 if (subfile->language == language_unknown
624 && subfile->next != NULL)
625 {
626 subfile->language = subfile->next->language;
627 }
628 }
629 }
630 \f
631 /* Handle the N_BINCL and N_EINCL symbol types that act like N_SOL for
632 switching source files (different subfiles, as we call them) within
633 one object file, but using a stack rather than in an arbitrary
634 order. */
635
636 void
637 buildsym_compunit::push_subfile ()
638 {
639 gdb_assert (m_current_subfile != NULL);
640 gdb_assert (!m_current_subfile->name.empty ());
641 m_subfile_stack.push_back (m_current_subfile->name.c_str ());
642 }
643
644 const char *
645 buildsym_compunit::pop_subfile ()
646 {
647 gdb_assert (!m_subfile_stack.empty ());
648 const char *name = m_subfile_stack.back ();
649 m_subfile_stack.pop_back ();
650 return name;
651 }
652 \f
653 /* Add a linetable entry for line number LINE and address PC to the
654 line vector for SUBFILE. */
655
656 void
657 buildsym_compunit::record_line (struct subfile *subfile, int line,
658 CORE_ADDR pc, linetable_entry_flags flags)
659 {
660 struct linetable_entry *e;
661
662 /* Make sure line vector exists and is big enough. */
663 if (!subfile->line_vector)
664 {
665 subfile->line_vector_length = INITIAL_LINE_VECTOR_LENGTH;
666 subfile->line_vector = (struct linetable *)
667 xmalloc (sizeof (struct linetable)
668 + subfile->line_vector_length * sizeof (struct linetable_entry));
669 subfile->line_vector->nitems = 0;
670 m_have_line_numbers = true;
671 }
672
673 if (subfile->line_vector->nitems >= subfile->line_vector_length)
674 {
675 subfile->line_vector_length *= 2;
676 subfile->line_vector = (struct linetable *)
677 xrealloc ((char *) subfile->line_vector,
678 (sizeof (struct linetable)
679 + (subfile->line_vector_length
680 * sizeof (struct linetable_entry))));
681 }
682
683 /* Normally, we treat lines as unsorted. But the end of sequence
684 marker is special. We sort line markers at the same PC by line
685 number, so end of sequence markers (which have line == 0) appear
686 first. This is right if the marker ends the previous function,
687 and there is no padding before the next function. But it is
688 wrong if the previous line was empty and we are now marking a
689 switch to a different subfile. We must leave the end of sequence
690 marker at the end of this group of lines, not sort the empty line
691 to after the marker. The easiest way to accomplish this is to
692 delete any empty lines from our table, if they are followed by
693 end of sequence markers. All we lose is the ability to set
694 breakpoints at some lines which contain no instructions
695 anyway. */
696 if (line == 0)
697 {
698 struct linetable_entry *last = nullptr;
699 while (subfile->line_vector->nitems > 0)
700 {
701 last = subfile->line_vector->item + subfile->line_vector->nitems - 1;
702 if (last->pc != pc)
703 break;
704 subfile->line_vector->nitems--;
705 }
706
707 /* Ignore an end-of-sequence marker marking an empty sequence. */
708 if (last == nullptr || last->line == 0)
709 return;
710 }
711
712 e = subfile->line_vector->item + subfile->line_vector->nitems++;
713 e->line = line;
714 e->is_stmt = (flags & LEF_IS_STMT) != 0;
715 e->pc = pc;
716 e->prologue_end = (flags & LEF_PROLOGUE_END) != 0;
717 }
718
719 \f
720 /* Subroutine of end_compunit_symtab to simplify it. Look for a subfile that
721 matches the main source file's basename. If there is only one, and
722 if the main source file doesn't have any symbol or line number
723 information, then copy this file's symtab and line_vector to the
724 main source file's subfile and discard the other subfile. This can
725 happen because of a compiler bug or from the user playing games
726 with #line or from things like a distributed build system that
727 manipulates the debug info. This can also happen from an innocent
728 symlink in the paths, we don't canonicalize paths here. */
729
730 void
731 buildsym_compunit::watch_main_source_file_lossage ()
732 {
733 struct subfile *mainsub, *subfile;
734
735 /* Get the main source file. */
736 mainsub = m_main_subfile;
737
738 /* If the main source file doesn't have any line number or symbol
739 info, look for an alias in another subfile. */
740
741 if (mainsub->line_vector == NULL
742 && mainsub->symtab == NULL)
743 {
744 const char *mainbase = lbasename (mainsub->name.c_str ());
745 int nr_matches = 0;
746 struct subfile *prevsub;
747 struct subfile *mainsub_alias = NULL;
748 struct subfile *prev_mainsub_alias = NULL;
749
750 prevsub = NULL;
751 for (subfile = m_subfiles;
752 subfile != NULL;
753 subfile = subfile->next)
754 {
755 if (subfile == mainsub)
756 continue;
757 if (filename_cmp (lbasename (subfile->name.c_str ()), mainbase) == 0)
758 {
759 ++nr_matches;
760 mainsub_alias = subfile;
761 prev_mainsub_alias = prevsub;
762 }
763 prevsub = subfile;
764 }
765
766 if (nr_matches == 1)
767 {
768 gdb_assert (mainsub_alias != NULL && mainsub_alias != mainsub);
769
770 /* Found a match for the main source file.
771 Copy its line_vector and symtab to the main subfile
772 and then discard it. */
773
774 mainsub->line_vector = mainsub_alias->line_vector;
775 mainsub->line_vector_length = mainsub_alias->line_vector_length;
776 mainsub->symtab = mainsub_alias->symtab;
777
778 if (prev_mainsub_alias == NULL)
779 m_subfiles = mainsub_alias->next;
780 else
781 prev_mainsub_alias->next = mainsub_alias->next;
782
783 delete mainsub_alias;
784 }
785 }
786 }
787
788 /* Implementation of the first part of end_compunit_symtab. It allows modifying
789 STATIC_BLOCK before it gets finalized by
790 end_compunit_symtab_from_static_block. If the returned value is NULL there
791 is no blockvector created for this symtab (you still must call
792 end_compunit_symtab_from_static_block).
793
794 END_ADDR is the same as for end_compunit_symtab: the address of the end of
795 the file's text.
796
797 If EXPANDABLE is non-zero the STATIC_BLOCK dictionary is made
798 expandable.
799
800 If REQUIRED is non-zero, then a symtab is created even if it does
801 not contain any symbols. */
802
803 struct block *
804 buildsym_compunit::end_compunit_symtab_get_static_block (CORE_ADDR end_addr,
805 int expandable,
806 int required)
807 {
808 /* Finish the lexical context of the last function in the file; pop
809 the context stack. */
810
811 if (!m_context_stack.empty ())
812 {
813 struct context_stack cstk = pop_context ();
814
815 /* Make a block for the local symbols within. */
816 finish_block (cstk.name, cstk.old_blocks, NULL,
817 cstk.start_addr, end_addr);
818
819 if (!m_context_stack.empty ())
820 {
821 /* This is said to happen with SCO. The old coffread.c
822 code simply emptied the context stack, so we do the
823 same. FIXME: Find out why it is happening. This is not
824 believed to happen in most cases (even for coffread.c);
825 it used to be an abort(). */
826 complaint (_("Context stack not empty in end_compunit_symtab"));
827 m_context_stack.clear ();
828 }
829 }
830
831 /* Reordered executables may have out of order pending blocks; if
832 OBJF_REORDERED is true, then sort the pending blocks. */
833
834 if ((m_objfile->flags & OBJF_REORDERED) && m_pending_blocks)
835 {
836 struct pending_block *pb;
837
838 std::vector<block *> barray;
839
840 for (pb = m_pending_blocks; pb != NULL; pb = pb->next)
841 barray.push_back (pb->block);
842
843 /* Sort blocks by start address in descending order. Blocks with the
844 same start address must remain in the original order to preserve
845 inline function caller/callee relationships. */
846 std::stable_sort (barray.begin (), barray.end (),
847 [] (const block *a, const block *b)
848 {
849 return BLOCK_START (a) > BLOCK_START (b);
850 });
851
852 int i = 0;
853 for (pb = m_pending_blocks; pb != NULL; pb = pb->next)
854 pb->block = barray[i++];
855 }
856
857 /* Cleanup any undefined types that have been left hanging around
858 (this needs to be done before the finish_blocks so that
859 file_symbols is still good).
860
861 Both cleanup_undefined_stabs_types and finish_global_stabs are stabs
862 specific, but harmless for other symbol readers, since on gdb
863 startup or when finished reading stabs, the state is set so these
864 are no-ops. FIXME: Is this handled right in case of QUIT? Can
865 we make this cleaner? */
866
867 cleanup_undefined_stabs_types (m_objfile);
868 finish_global_stabs (m_objfile);
869
870 if (!required
871 && m_pending_blocks == NULL
872 && m_file_symbols == NULL
873 && m_global_symbols == NULL
874 && !m_have_line_numbers
875 && m_pending_macros == NULL
876 && m_global_using_directives == NULL)
877 {
878 /* Ignore symtabs that have no functions with real debugging info. */
879 return NULL;
880 }
881 else
882 {
883 /* Define the STATIC_BLOCK. */
884 return finish_block_internal (NULL, get_file_symbols (), NULL, NULL,
885 m_last_source_start_addr,
886 end_addr, 0, expandable);
887 }
888 }
889
890 /* Subroutine of end_compunit_symtab_from_static_block to simplify it.
891 Handle the "have blockvector" case.
892 See end_compunit_symtab_from_static_block for a description of the
893 arguments. */
894
895 struct compunit_symtab *
896 buildsym_compunit::end_compunit_symtab_with_blockvector
897 (struct block *static_block, int section, int expandable)
898 {
899 struct compunit_symtab *cu = m_compunit_symtab;
900 struct blockvector *blockvector;
901 struct subfile *subfile;
902 CORE_ADDR end_addr;
903
904 gdb_assert (static_block != NULL);
905 gdb_assert (m_subfiles != NULL);
906
907 end_addr = BLOCK_END (static_block);
908
909 /* Create the GLOBAL_BLOCK and build the blockvector. */
910 finish_block_internal (NULL, get_global_symbols (), NULL, NULL,
911 m_last_source_start_addr, end_addr,
912 1, expandable);
913 blockvector = make_blockvector ();
914
915 /* Read the line table if it has to be read separately.
916 This is only used by xcoffread.c. */
917 if (m_objfile->sf->sym_read_linetable != NULL)
918 m_objfile->sf->sym_read_linetable (m_objfile);
919
920 /* Handle the case where the debug info specifies a different path
921 for the main source file. It can cause us to lose track of its
922 line number information. */
923 watch_main_source_file_lossage ();
924
925 /* Now create the symtab objects proper, if not already done,
926 one for each subfile. */
927
928 for (subfile = m_subfiles;
929 subfile != NULL;
930 subfile = subfile->next)
931 {
932 int linetablesize = 0;
933
934 if (subfile->line_vector)
935 {
936 linetablesize = sizeof (struct linetable) +
937 subfile->line_vector->nitems * sizeof (struct linetable_entry);
938
939 const auto lte_is_less_than
940 = [] (const linetable_entry &ln1,
941 const linetable_entry &ln2) -> bool
942 {
943 if (ln1.pc == ln2.pc
944 && ((ln1.line == 0) != (ln2.line == 0)))
945 return ln1.line == 0;
946
947 return (ln1.pc < ln2.pc);
948 };
949
950 /* Like the pending blocks, the line table may be scrambled in
951 reordered executables. Sort it if OBJF_REORDERED is true. It
952 is important to preserve the order of lines at the same
953 address, as this maintains the inline function caller/callee
954 relationships, this is why std::stable_sort is used. */
955 if (m_objfile->flags & OBJF_REORDERED)
956 std::stable_sort (subfile->line_vector->item,
957 subfile->line_vector->item
958 + subfile->line_vector->nitems,
959 lte_is_less_than);
960 }
961
962 /* Allocate a symbol table if necessary. */
963 if (subfile->symtab == NULL)
964 subfile->symtab = allocate_symtab (cu, subfile->name.c_str ());
965
966 struct symtab *symtab = subfile->symtab;
967
968 /* Fill in its components. */
969
970 if (subfile->line_vector)
971 {
972 /* Reallocate the line table on the symbol obstack. */
973 symtab->set_linetable
974 ((struct linetable *)
975 obstack_alloc (&m_objfile->objfile_obstack, linetablesize));
976 memcpy (symtab->linetable (), subfile->line_vector, linetablesize);
977 }
978 else
979 symtab->set_linetable (nullptr);
980
981 /* Use whatever language we have been using for this
982 subfile, not the one that was deduced in allocate_symtab
983 from the filename. We already did our own deducing when
984 we created the subfile, and we may have altered our
985 opinion of what language it is from things we found in
986 the symbols. */
987 symtab->set_language (subfile->language);
988 }
989
990 /* Make sure the filetab of main_subfile is the primary filetab of the CU. */
991 cu->set_primary_filetab (m_main_subfile->symtab);
992
993 /* Fill out the compunit symtab. */
994
995 if (!m_comp_dir.empty ())
996 {
997 /* Reallocate the dirname on the symbol obstack. */
998 cu->set_dirname (obstack_strdup (&m_objfile->objfile_obstack,
999 m_comp_dir.c_str ()));
1000 }
1001
1002 /* Save the debug format string (if any) in the symtab. */
1003 cu->set_debugformat (m_debugformat);
1004
1005 /* Similarly for the producer. */
1006 cu->set_producer (m_producer);
1007
1008 cu->set_blockvector (blockvector);
1009 {
1010 struct block *b = BLOCKVECTOR_BLOCK (blockvector, GLOBAL_BLOCK);
1011
1012 set_block_compunit_symtab (b, cu);
1013 }
1014
1015 cu->set_block_line_section (section);
1016
1017 cu->set_macro_table (release_macros ());
1018
1019 /* Default any symbols without a specified symtab to the primary symtab. */
1020 {
1021 int block_i;
1022
1023 /* The main source file's symtab. */
1024 struct symtab *symtab = cu->primary_filetab ();
1025
1026 for (block_i = 0; block_i < BLOCKVECTOR_NBLOCKS (blockvector); block_i++)
1027 {
1028 struct block *block = BLOCKVECTOR_BLOCK (blockvector, block_i);
1029 struct symbol *sym;
1030 struct mdict_iterator miter;
1031
1032 /* Inlined functions may have symbols not in the global or
1033 static symbol lists. */
1034 if (BLOCK_FUNCTION (block) != NULL)
1035 if (symbol_symtab (BLOCK_FUNCTION (block)) == NULL)
1036 symbol_set_symtab (BLOCK_FUNCTION (block), symtab);
1037
1038 /* Note that we only want to fix up symbols from the local
1039 blocks, not blocks coming from included symtabs. That is why
1040 we use ALL_DICT_SYMBOLS here and not ALL_BLOCK_SYMBOLS. */
1041 ALL_DICT_SYMBOLS (BLOCK_MULTIDICT (block), miter, sym)
1042 if (symbol_symtab (sym) == NULL)
1043 symbol_set_symtab (sym, symtab);
1044 }
1045 }
1046
1047 add_compunit_symtab_to_objfile (cu);
1048
1049 return cu;
1050 }
1051
1052 /* Implementation of the second part of end_compunit_symtab. Pass STATIC_BLOCK
1053 as value returned by end_compunit_symtab_get_static_block.
1054
1055 SECTION is the same as for end_compunit_symtab: the section number
1056 (in objfile->section_offsets) of the blockvector and linetable.
1057
1058 If EXPANDABLE is non-zero the GLOBAL_BLOCK dictionary is made
1059 expandable. */
1060
1061 struct compunit_symtab *
1062 buildsym_compunit::end_compunit_symtab_from_static_block
1063 (struct block *static_block, int section, int expandable)
1064 {
1065 struct compunit_symtab *cu;
1066
1067 if (static_block == NULL)
1068 {
1069 /* Handle the "no blockvector" case.
1070 When this happens there is nothing to record, so there's nothing
1071 to do: memory will be freed up later.
1072
1073 Note: We won't be adding a compunit to the objfile's list of
1074 compunits, so there's nothing to unchain. However, since each symtab
1075 is added to the objfile's obstack we can't free that space.
1076 We could do better, but this is believed to be a sufficiently rare
1077 event. */
1078 cu = NULL;
1079 }
1080 else
1081 cu = end_compunit_symtab_with_blockvector (static_block, section, expandable);
1082
1083 return cu;
1084 }
1085
1086 /* Finish the symbol definitions for one main source file, close off
1087 all the lexical contexts for that file (creating struct block's for
1088 them), then make the struct symtab for that file and put it in the
1089 list of all such.
1090
1091 END_ADDR is the address of the end of the file's text. SECTION is
1092 the section number (in objfile->section_offsets) of the blockvector
1093 and linetable.
1094
1095 Note that it is possible for end_compunit_symtab() to return NULL. In
1096 particular, for the DWARF case at least, it will return NULL when
1097 it finds a compilation unit that has exactly one DIE, a
1098 TAG_compile_unit DIE. This can happen when we link in an object
1099 file that was compiled from an empty source file. Returning NULL
1100 is probably not the correct thing to do, because then gdb will
1101 never know about this empty file (FIXME).
1102
1103 If you need to modify STATIC_BLOCK before it is finalized you should
1104 call end_compunit_symtab_get_static_block and
1105 end_compunit_symtab_from_static_block yourself. */
1106
1107 struct compunit_symtab *
1108 buildsym_compunit::end_compunit_symtab (CORE_ADDR end_addr, int section)
1109 {
1110 struct block *static_block;
1111
1112 static_block = end_compunit_symtab_get_static_block (end_addr, 0, 0);
1113 return end_compunit_symtab_from_static_block (static_block, section, 0);
1114 }
1115
1116 /* Same as end_compunit_symtab except create a symtab that can be later added
1117 to. */
1118
1119 struct compunit_symtab *
1120 buildsym_compunit::end_expandable_symtab (CORE_ADDR end_addr, int section)
1121 {
1122 struct block *static_block;
1123
1124 static_block = end_compunit_symtab_get_static_block (end_addr, 1, 0);
1125 return end_compunit_symtab_from_static_block (static_block, section, 1);
1126 }
1127
1128 /* Subroutine of augment_type_symtab to simplify it.
1129 Attach the main source file's symtab to all symbols in PENDING_LIST that
1130 don't have one. */
1131
1132 static void
1133 set_missing_symtab (struct pending *pending_list,
1134 struct compunit_symtab *cu)
1135 {
1136 struct pending *pending;
1137 int i;
1138
1139 for (pending = pending_list; pending != NULL; pending = pending->next)
1140 {
1141 for (i = 0; i < pending->nsyms; ++i)
1142 {
1143 if (symbol_symtab (pending->symbol[i]) == NULL)
1144 symbol_set_symtab (pending->symbol[i], cu->primary_filetab ());
1145 }
1146 }
1147 }
1148
1149 /* Same as end_compunit_symtab, but for the case where we're adding more symbols
1150 to an existing symtab that is known to contain only type information.
1151 This is the case for DWARF4 Type Units. */
1152
1153 void
1154 buildsym_compunit::augment_type_symtab ()
1155 {
1156 struct compunit_symtab *cust = m_compunit_symtab;
1157 const struct blockvector *blockvector = cust->blockvector ();
1158
1159 if (!m_context_stack.empty ())
1160 complaint (_("Context stack not empty in augment_type_symtab"));
1161 if (m_pending_blocks != NULL)
1162 complaint (_("Blocks in a type symtab"));
1163 if (m_pending_macros != NULL)
1164 complaint (_("Macro in a type symtab"));
1165 if (m_have_line_numbers)
1166 complaint (_("Line numbers recorded in a type symtab"));
1167
1168 if (m_file_symbols != NULL)
1169 {
1170 struct block *block = BLOCKVECTOR_BLOCK (blockvector, STATIC_BLOCK);
1171
1172 /* First mark any symbols without a specified symtab as belonging
1173 to the primary symtab. */
1174 set_missing_symtab (m_file_symbols, cust);
1175
1176 mdict_add_pending (BLOCK_MULTIDICT (block), m_file_symbols);
1177 }
1178
1179 if (m_global_symbols != NULL)
1180 {
1181 struct block *block = BLOCKVECTOR_BLOCK (blockvector, GLOBAL_BLOCK);
1182
1183 /* First mark any symbols without a specified symtab as belonging
1184 to the primary symtab. */
1185 set_missing_symtab (m_global_symbols, cust);
1186
1187 mdict_add_pending (BLOCK_MULTIDICT (block),
1188 m_global_symbols);
1189 }
1190 }
1191
1192 /* Push a context block. Args are an identifying nesting level
1193 (checkable when you pop it), and the starting PC address of this
1194 context. */
1195
1196 struct context_stack *
1197 buildsym_compunit::push_context (int desc, CORE_ADDR valu)
1198 {
1199 m_context_stack.emplace_back ();
1200 struct context_stack *newobj = &m_context_stack.back ();
1201
1202 newobj->depth = desc;
1203 newobj->locals = m_local_symbols;
1204 newobj->old_blocks = m_pending_blocks;
1205 newobj->start_addr = valu;
1206 newobj->local_using_directives = m_local_using_directives;
1207 newobj->name = NULL;
1208
1209 m_local_symbols = NULL;
1210 m_local_using_directives = NULL;
1211
1212 return newobj;
1213 }
1214
1215 /* Pop a context block. Returns the address of the context block just
1216 popped. */
1217
1218 struct context_stack
1219 buildsym_compunit::pop_context ()
1220 {
1221 gdb_assert (!m_context_stack.empty ());
1222 struct context_stack result = m_context_stack.back ();
1223 m_context_stack.pop_back ();
1224 return result;
1225 }