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