1 /* Support routines for building symbol tables in GDB's internal format.
2 Copyright (C) 1986-2022 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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/>. */
20 #include "buildsym-legacy.h"
22 #include "gdbsupport/gdb_obstack.h"
23 #include "gdbsupport/pathstuff.h"
28 #include "complaints.h"
29 #include "expression.h" /* For "enum exp_opcode" used by... */
30 #include "filenames.h" /* For DOSish file names. */
32 #include "demangle.h" /* Needed by SYMBOL_INIT_DEMANGLED_NAME. */
34 #include "cp-support.h"
35 #include "dictionary.h"
39 /* For cleanup_undefined_stabs_types and finish_global_stabs (somewhat
40 questionable--see comment where we call them). */
42 #include "stabsread.h"
44 /* List of blocks already made (lexical contexts already closed).
45 This is used at the end to make the blockvector. */
49 struct pending_block
*next
;
53 buildsym_compunit::buildsym_compunit (struct objfile
*objfile_
,
55 const char *comp_dir_
,
56 enum language language_
,
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
)
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
);
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. */
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
;
80 buildsym_compunit::~buildsym_compunit ()
82 struct subfile
*subfile
, *nextsub
;
84 if (m_pending_macros
!= nullptr)
85 free_macro_table (m_pending_macros
);
87 for (subfile
= m_subfiles
;
91 nextsub
= subfile
->next
;
95 struct pending
*next
, *next1
;
97 for (next
= m_file_symbols
; next
!= NULL
; next
= next1
)
100 xfree ((void *) next
);
103 for (next
= m_global_symbols
; next
!= NULL
; next
= next1
)
106 xfree ((void *) next
);
111 buildsym_compunit::get_macro_table ()
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
,
117 return m_pending_macros
;
120 /* Maintain the lists of symbols and blocks. */
122 /* Add a symbol to one of the lists of symbols. */
125 add_symbol_to_list (struct symbol
*symbol
, struct pending
**listhead
)
127 struct pending
*link
;
129 /* If this is an alias for another symbol, don't add it. */
130 if (symbol
->linkage_name () && symbol
->linkage_name ()[0] == '#')
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
)
137 link
= XNEW (struct pending
);
138 link
->next
= *listhead
;
143 (*listhead
)->symbol
[(*listhead
)->nsyms
++] = symbol
;
146 /* Find a symbol named NAME on a LIST. NAME need not be
147 '\0'-terminated; LENGTH is the length of the name. */
150 find_symbol_in_list (struct pending
*list
, char *name
, int length
)
157 for (j
= list
->nsyms
; --j
>= 0;)
159 pp
= list
->symbol
[j
]->linkage_name ();
160 if (*pp
== *name
&& strncmp (pp
, name
, length
) == 0
161 && pp
[length
] == '\0')
163 return (list
->symbol
[j
]);
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. */
176 buildsym_compunit::record_pending_block (struct block
*block
,
177 struct pending_block
*opblock
)
179 struct pending_block
*pblock
;
181 pblock
= XOBNEW (&m_pending_block_obstack
, struct pending_block
);
182 pblock
->block
= block
;
185 pblock
->next
= opblock
->next
;
186 opblock
->next
= pblock
;
190 pblock
->next
= m_pending_blocks
;
191 m_pending_blocks
= pblock
;
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. */
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
)
208 struct gdbarch
*gdbarch
= m_objfile
->arch ();
209 struct pending
*next
, *next1
;
211 struct pending_block
*pblock
;
212 struct pending_block
*opblock
;
215 ? allocate_global_block (&m_objfile
->objfile_obstack
)
216 : allocate_block (&m_objfile
->objfile_obstack
));
221 (mdict_create_linear (&m_objfile
->objfile_obstack
, *listhead
));
228 (mdict_create_hashed_expandable (m_language
));
229 mdict_add_pending (block
->multidict (), *listhead
);
234 (mdict_create_hashed (&m_objfile
->objfile_obstack
, *listhead
));
238 block
->set_start (start
);
239 block
->set_end (end
);
241 /* Put the block in as the value of the symbol that names it. */
245 struct type
*ftype
= symbol
->type ();
246 struct mdict_iterator miter
;
247 symbol
->set_value_block (block
);
248 block
->set_function (symbol
);
250 if (ftype
->num_fields () <= 0)
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
;
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
)
262 if (sym
->is_argument ())
267 ftype
->set_num_fields (nparams
);
270 TYPE_ALLOC (ftype
, nparams
* sizeof (struct field
)));
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
)
277 if (iparams
== nparams
)
280 if (sym
->is_argument ())
282 ftype
->field (iparams
).set_type (sym
->type ());
283 TYPE_FIELD_ARTIFICIAL (ftype
, iparams
) = 0;
291 block
->set_function (nullptr);
293 if (static_link
!= NULL
)
294 objfile_register_static_link (m_objfile
, block
, static_link
);
296 /* Now free the links of the list, and empty the list. */
298 for (next
= *listhead
; next
; next
= next1
)
305 /* Check to be sure that the blocks have an end address that is
306 greater than starting address. */
308 if (block
->end () < block
->start ())
312 complaint (_("block end address less than block "
313 "start address in %s (patched it)"),
314 symbol
->print_name ());
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 ()));
323 /* Better than nothing. */
324 block
->set_end (block
->start ());
327 /* Install this block as the superblock of all blocks made since the
328 start of this scope that don't have superblocks yet. */
331 for (pblock
= m_pending_blocks
;
332 pblock
&& pblock
!= old_blocks
;
333 pblock
= pblock
->next
)
335 if (pblock
->block
->superblock () == NULL
)
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.
341 Skip blocks which correspond to a function; they're not
342 physically nested inside this other blocks, only
344 if (pblock
->block
->function () == NULL
345 && (pblock
->block
->start () < block
->start ()
346 || pblock
->block
->end () > block
->end ()))
350 complaint (_("inner block not inside outer block in %s"),
351 symbol
->print_name ());
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 ()));
363 if (pblock
->block
->start () < block
->start ())
364 pblock
->block
->set_start (block
->start ());
366 if (pblock
->block
->end () > block
->end ())
367 pblock
->block
->set_end (block
->end ());
369 pblock
->block
->set_superblock (block
);
374 block_set_using (block
,
376 ? m_global_using_directives
377 : m_local_using_directives
),
378 &m_objfile
->objfile_obstack
);
380 m_global_using_directives
= NULL
;
382 m_local_using_directives
= NULL
;
384 record_pending_block (block
, opblock
);
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
)
395 return finish_block_internal (symbol
, &m_local_symbols
,
396 old_blocks
, static_link
, start
, end
, 0, 0);
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.
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. */
408 buildsym_compunit::record_block_range (struct block
*block
,
410 CORE_ADDR end_inclusive
)
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;
421 if (m_pending_addrmap
== nullptr)
422 m_pending_addrmap
= addrmap_create_mutable (&m_pending_addrmap_obstack
);
424 addrmap_set_empty (m_pending_addrmap
, start
, end_inclusive
, block
);
428 buildsym_compunit::make_blockvector ()
430 struct pending_block
*next
;
431 struct blockvector
*blockvector
;
434 /* Count the length of the list of blocks. */
436 for (next
= m_pending_blocks
, i
= 0; next
; next
= next
->next
, i
++)
440 blockvector
= (struct blockvector
*)
441 obstack_alloc (&m_objfile
->objfile_obstack
,
442 (sizeof (struct blockvector
)
443 + (i
- 1) * sizeof (struct block
*)));
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. */
451 blockvector
->set_num_blocks (i
);
452 for (next
= m_pending_blocks
; next
; next
= next
->next
)
453 blockvector
->set_block (--i
, next
->block
);
455 free_pending_blocks ();
457 /* If we needed an address map for this symtab, record it in the
459 if (m_pending_addrmap
!= nullptr && m_pending_addrmap_interesting
)
461 (addrmap_create_fixed (m_pending_addrmap
, &m_objfile
->objfile_obstack
));
463 blockvector
->set_map (nullptr);
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)
473 for (i
= 1; i
< blockvector
->num_blocks (); i
++)
475 if (blockvector
->block (i
- 1)->start ()
476 > blockvector
->block (i
)->start ())
479 = blockvector
->block (i
)->start ();
481 complaint (_("block at %s out of order"),
482 hex_string ((LONGEST
) start
));
487 return (blockvector
);
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). */
495 buildsym_compunit::start_subfile (const char *name
)
497 /* See if this subfile is already registered. */
499 for (subfile
*subfile
= m_subfiles
; subfile
; subfile
= subfile
->next
)
501 std::string subfile_name_holder
;
502 const char *subfile_name
;
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 ())
510 subfile_name_holder
= path_join (m_comp_dir
.c_str (),
511 subfile
->name
.c_str ());
512 subfile_name
= subfile_name_holder
.c_str ();
515 subfile_name
= subfile
->name
.c_str ();
517 if (FILENAME_CMP (subfile_name
, name
) == 0)
519 m_current_subfile
= subfile
;
524 /* This subfile is not known. Add an entry for it. */
526 subfile_up
subfile (new struct subfile
);
527 subfile
->name
= name
;
529 m_current_subfile
= subfile
.get ();
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
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
;
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. */
551 if (!subfile
->name
.empty ())
554 language sublang
= deduce_language_from_filename (subfile
->name
.c_str ());
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
;
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
;
569 /* Link this subfile at the front of the subfile list. */
570 subfile
->next
= m_subfiles
;
571 m_subfiles
= subfile
.release ();
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.
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 '/'). */
587 buildsym_compunit::patch_subfile_names (struct subfile
*subfile
,
591 && m_comp_dir
.empty ()
592 && !subfile
->name
.empty ()
593 && IS_DIR_SEPARATOR (subfile
->name
.back ()))
595 m_comp_dir
= std::move (subfile
->name
);
596 subfile
->name
= name
;
597 set_last_source_file (name
);
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. */
611 = deduce_language_from_filename (subfile
->name
.c_str ());
612 if (subfile
->language
== language_unknown
613 && subfile
->next
!= NULL
)
615 subfile
->language
= subfile
->next
->language
;
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
626 buildsym_compunit::push_subfile ()
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 ());
634 buildsym_compunit::pop_subfile ()
636 gdb_assert (!m_subfile_stack
.empty ());
637 const char *name
= m_subfile_stack
.back ();
638 m_subfile_stack
.pop_back ();
642 /* Add a linetable entry for line number LINE and address PC to the
643 line vector for SUBFILE. */
646 buildsym_compunit::record_line (struct subfile
*subfile
, int line
,
647 CORE_ADDR pc
, linetable_entry_flags flags
)
649 m_have_line_numbers
= true;
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
666 gdb::optional
<int> last_line
;
668 while (!subfile
->line_vector_entries
.empty ())
670 linetable_entry
*last
= &subfile
->line_vector_entries
.back ();
671 last_line
= last
->line
;
676 subfile
->line_vector_entries
.pop_back ();
679 /* Ignore an end-of-sequence marker marking an empty sequence. */
680 if (!last_line
.has_value () || *last_line
== 0)
684 subfile
->line_vector_entries
.emplace_back ();
685 linetable_entry
&e
= subfile
->line_vector_entries
.back ();
687 e
.is_stmt
= (flags
& LEF_IS_STMT
) != 0;
689 e
.prologue_end
= (flags
& LEF_PROLOGUE_END
) != 0;
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. */
704 buildsym_compunit::watch_main_source_file_lossage ()
706 struct subfile
*mainsub
, *subfile
;
708 /* Get the main source file. */
709 mainsub
= m_main_subfile
;
711 /* If the main source file doesn't have any line number or symbol
712 info, look for an alias in another subfile. */
714 if (mainsub
->line_vector_entries
.empty ()
715 && mainsub
->symtab
== NULL
)
717 const char *mainbase
= lbasename (mainsub
->name
.c_str ());
719 struct subfile
*prevsub
;
720 struct subfile
*mainsub_alias
= NULL
;
721 struct subfile
*prev_mainsub_alias
= NULL
;
724 for (subfile
= m_subfiles
;
726 subfile
= subfile
->next
)
728 if (subfile
== mainsub
)
730 if (filename_cmp (lbasename (subfile
->name
.c_str ()), mainbase
) == 0)
733 mainsub_alias
= subfile
;
734 prev_mainsub_alias
= prevsub
;
741 gdb_assert (mainsub_alias
!= NULL
&& mainsub_alias
!= mainsub
);
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. */
747 mainsub
->line_vector_entries
748 = std::move (mainsub_alias
->line_vector_entries
);
749 mainsub
->symtab
= mainsub_alias
->symtab
;
751 if (prev_mainsub_alias
== NULL
)
752 m_subfiles
= mainsub_alias
->next
;
754 prev_mainsub_alias
->next
= mainsub_alias
->next
;
756 delete mainsub_alias
;
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).
767 END_ADDR is the same as for end_compunit_symtab: the address of the end of
770 If EXPANDABLE is non-zero the STATIC_BLOCK dictionary is made
773 If REQUIRED is non-zero, then a symtab is created even if it does
774 not contain any symbols. */
777 buildsym_compunit::end_compunit_symtab_get_static_block (CORE_ADDR end_addr
,
781 /* Finish the lexical context of the last function in the file; pop
782 the context stack. */
784 if (!m_context_stack
.empty ())
786 struct context_stack cstk
= pop_context ();
788 /* Make a block for the local symbols within. */
789 finish_block (cstk
.name
, cstk
.old_blocks
, NULL
,
790 cstk
.start_addr
, end_addr
);
792 if (!m_context_stack
.empty ())
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 ();
804 /* Reordered executables may have out of order pending blocks; if
805 OBJF_REORDERED is true, then sort the pending blocks. */
807 if ((m_objfile
->flags
& OBJF_REORDERED
) && m_pending_blocks
)
809 struct pending_block
*pb
;
811 std::vector
<block
*> barray
;
813 for (pb
= m_pending_blocks
; pb
!= NULL
; pb
= pb
->next
)
814 barray
.push_back (pb
->block
);
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
)
822 return a
->start () > b
->start ();
826 for (pb
= m_pending_blocks
; pb
!= NULL
; pb
= pb
->next
)
827 pb
->block
= barray
[i
++];
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).
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? */
840 cleanup_undefined_stabs_types (m_objfile
);
841 finish_global_stabs (m_objfile
);
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
)
851 /* Ignore symtabs that have no functions with real debugging info. */
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
);
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
868 struct compunit_symtab
*
869 buildsym_compunit::end_compunit_symtab_with_blockvector
870 (struct block
*static_block
, int section
, int expandable
)
872 struct compunit_symtab
*cu
= m_compunit_symtab
;
873 struct blockvector
*blockvector
;
874 struct subfile
*subfile
;
877 gdb_assert (static_block
!= NULL
);
878 gdb_assert (m_subfiles
!= NULL
);
880 end_addr
= static_block
->end ();
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
,
886 blockvector
= make_blockvector ();
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
);
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 ();
898 /* Now create the symtab objects proper, if not already done,
899 one for each subfile. */
901 for (subfile
= m_subfiles
;
903 subfile
= subfile
->next
)
905 if (!subfile
->line_vector_entries
.empty ())
907 const auto lte_is_less_than
908 = [] (const linetable_entry
&ln1
,
909 const linetable_entry
&ln2
) -> bool
912 && ((ln1
.line
== 0) != (ln2
.line
== 0)))
913 return ln1
.line
== 0;
915 return (ln1
.pc
< ln2
.pc
);
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 (),
929 /* Allocate a symbol table if necessary. */
930 if (subfile
->symtab
== NULL
)
931 subfile
->symtab
= allocate_symtab (cu
, subfile
->name
.c_str ());
933 struct symtab
*symtab
= subfile
->symtab
;
935 /* Fill in its components. */
937 if (!subfile
->line_vector_entries
.empty ())
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
;
944 symtab
->set_linetable
945 (XOBNEWVAR (&m_objfile
->objfile_obstack
, struct linetable
,
948 symtab
->linetable ()->nitems
= n_entries
;
949 memcpy (symtab
->linetable ()->item
,
950 subfile
->line_vector_entries
.data (), entry_array_size
);
953 symtab
->set_linetable (nullptr);
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
961 symtab
->set_language (subfile
->language
);
964 /* Make sure the filetab of main_subfile is the primary filetab of the CU. */
965 cu
->set_primary_filetab (m_main_subfile
->symtab
);
967 /* Fill out the compunit symtab. */
969 if (!m_comp_dir
.empty ())
971 /* Reallocate the dirname on the symbol obstack. */
972 cu
->set_dirname (obstack_strdup (&m_objfile
->objfile_obstack
,
973 m_comp_dir
.c_str ()));
976 /* Save the debug format string (if any) in the symtab. */
977 cu
->set_debugformat (m_debugformat
);
979 /* Similarly for the producer. */
980 cu
->set_producer (m_producer
);
982 cu
->set_blockvector (blockvector
);
984 struct block
*b
= blockvector
->global_block ();
986 set_block_compunit_symtab (b
, cu
);
989 cu
->set_block_line_section (section
);
991 cu
->set_macro_table (release_macros ());
993 /* Default any symbols without a specified symtab to the primary symtab. */
997 /* The main source file's symtab. */
998 struct symtab
*symtab
= cu
->primary_filetab ();
1000 for (block_i
= 0; block_i
< blockvector
->num_blocks (); block_i
++)
1002 struct block
*block
= blockvector
->block (block_i
);
1004 struct mdict_iterator miter
;
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
);
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
);
1021 add_compunit_symtab_to_objfile (cu
);
1026 /* Implementation of the second part of end_compunit_symtab. Pass STATIC_BLOCK
1027 as value returned by end_compunit_symtab_get_static_block.
1029 SECTION is the same as for end_compunit_symtab: the section number
1030 (in objfile->section_offsets) of the blockvector and linetable.
1032 If EXPANDABLE is non-zero the GLOBAL_BLOCK dictionary is made
1035 struct compunit_symtab
*
1036 buildsym_compunit::end_compunit_symtab_from_static_block
1037 (struct block
*static_block
, int section
, int expandable
)
1039 struct compunit_symtab
*cu
;
1041 if (static_block
== NULL
)
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.
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
1055 cu
= end_compunit_symtab_with_blockvector (static_block
, section
, expandable
);
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
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
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).
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. */
1081 struct compunit_symtab
*
1082 buildsym_compunit::end_compunit_symtab (CORE_ADDR end_addr
, int section
)
1084 struct block
*static_block
;
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);
1090 /* Same as end_compunit_symtab except create a symtab that can be later added
1093 struct compunit_symtab
*
1094 buildsym_compunit::end_expandable_symtab (CORE_ADDR end_addr
, int section
)
1096 struct block
*static_block
;
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);
1102 /* Subroutine of augment_type_symtab to simplify it.
1103 Attach the main source file's symtab to all symbols in PENDING_LIST that
1107 set_missing_symtab (struct pending
*pending_list
,
1108 struct compunit_symtab
*cu
)
1110 struct pending
*pending
;
1113 for (pending
= pending_list
; pending
!= NULL
; pending
= pending
->next
)
1115 for (i
= 0; i
< pending
->nsyms
; ++i
)
1117 if (pending
->symbol
[i
]->symtab () == NULL
)
1118 pending
->symbol
[i
]->set_symtab (cu
->primary_filetab ());
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. */
1128 buildsym_compunit::augment_type_symtab ()
1130 struct compunit_symtab
*cust
= m_compunit_symtab
;
1131 struct blockvector
*blockvector
= cust
->blockvector ();
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"));
1142 if (m_file_symbols
!= NULL
)
1144 struct block
*block
= blockvector
->static_block ();
1146 /* First mark any symbols without a specified symtab as belonging
1147 to the primary symtab. */
1148 set_missing_symtab (m_file_symbols
, cust
);
1150 mdict_add_pending (block
->multidict (), m_file_symbols
);
1153 if (m_global_symbols
!= NULL
)
1155 struct block
*block
= blockvector
->global_block ();
1157 /* First mark any symbols without a specified symtab as belonging
1158 to the primary symtab. */
1159 set_missing_symtab (m_global_symbols
, cust
);
1161 mdict_add_pending (block
->multidict (), m_global_symbols
);
1165 /* Push a context block. Args are an identifying nesting level
1166 (checkable when you pop it), and the starting PC address of this
1169 struct context_stack
*
1170 buildsym_compunit::push_context (int desc
, CORE_ADDR valu
)
1172 m_context_stack
.emplace_back ();
1173 struct context_stack
*newobj
= &m_context_stack
.back ();
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
;
1182 m_local_symbols
= NULL
;
1183 m_local_using_directives
= NULL
;
1188 /* Pop a context block. Returns the address of the context block just
1191 struct context_stack
1192 buildsym_compunit::pop_context ()
1194 gdb_assert (!m_context_stack
.empty ());
1195 struct context_stack result
= m_context_stack
.back ();
1196 m_context_stack
.pop_back ();