| 1 | /* Support routines for building symbol tables in GDB's internal format. |
| 2 | Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1995, 1996 |
| 3 | Free Software Foundation, Inc. |
| 4 | |
| 5 | This file is part of GDB. |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 2 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program; if not, write to the Free Software |
| 19 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
| 20 | |
| 21 | /* This module provides subroutines used for creating and adding to |
| 22 | the symbol table. These routines are called from various symbol- |
| 23 | file-reading routines. |
| 24 | |
| 25 | Routines to support specific debugging information formats (stabs, |
| 26 | DWARF, etc) belong somewhere else. */ |
| 27 | |
| 28 | #include "defs.h" |
| 29 | #include "bfd.h" |
| 30 | #include "obstack.h" |
| 31 | #include "symtab.h" |
| 32 | #include "symfile.h" /* Needed for "struct complaint" */ |
| 33 | #include "objfiles.h" |
| 34 | #include "gdbtypes.h" |
| 35 | #include "complaints.h" |
| 36 | #include "gdb_string.h" |
| 37 | |
| 38 | /* Ask buildsym.h to define the vars it normally declares `extern'. */ |
| 39 | #define EXTERN /**/ |
| 40 | #include "buildsym.h" /* Our own declarations */ |
| 41 | #undef EXTERN |
| 42 | |
| 43 | /* For cleanup_undefined_types and finish_global_stabs (somewhat |
| 44 | questionable--see comment where we call them). */ |
| 45 | #include "stabsread.h" |
| 46 | |
| 47 | /* Pointer to the head of a linked list of symbol blocks which have |
| 48 | already been finalized (lexical contexts already closed) and which are |
| 49 | just waiting to be built into a blockvector when finalizing the |
| 50 | associated symtab. */ |
| 51 | |
| 52 | static struct pending_block *pending_blocks = NULL; |
| 53 | |
| 54 | /* List of free `struct pending' structures for reuse. */ |
| 55 | |
| 56 | static struct pending *free_pendings; |
| 57 | |
| 58 | /* Non-zero if symtab has line number info. This prevents an otherwise empty |
| 59 | symtab from being tossed. */ |
| 60 | |
| 61 | static int have_line_numbers; |
| 62 | \f |
| 63 | static int |
| 64 | compare_line_numbers PARAMS ((const void *, const void *)); |
| 65 | |
| 66 | \f |
| 67 | /* Initial sizes of data structures. These are realloc'd larger if needed, |
| 68 | and realloc'd down to the size actually used, when completed. */ |
| 69 | |
| 70 | #define INITIAL_CONTEXT_STACK_SIZE 10 |
| 71 | #define INITIAL_LINE_VECTOR_LENGTH 1000 |
| 72 | |
| 73 | \f |
| 74 | /* Complaints about the symbols we have encountered. */ |
| 75 | |
| 76 | struct complaint block_end_complaint = |
| 77 | {"block end address less than block start address in %s (patched it)", 0, 0}; |
| 78 | |
| 79 | struct complaint anon_block_end_complaint = |
| 80 | {"block end address 0x%lx less than block start address 0x%lx (patched it)", 0, 0}; |
| 81 | |
| 82 | struct complaint innerblock_complaint = |
| 83 | {"inner block not inside outer block in %s", 0, 0}; |
| 84 | |
| 85 | struct complaint innerblock_anon_complaint = |
| 86 | {"inner block (0x%lx-0x%lx) not inside outer block (0x%lx-0x%lx)", 0, 0}; |
| 87 | |
| 88 | struct complaint blockvector_complaint = |
| 89 | {"block at 0x%lx out of order", 0, 0}; |
| 90 | |
| 91 | \f |
| 92 | /* maintain the lists of symbols and blocks */ |
| 93 | |
| 94 | /* Add a symbol to one of the lists of symbols. */ |
| 95 | |
| 96 | void |
| 97 | add_symbol_to_list (symbol, listhead) |
| 98 | struct symbol *symbol; |
| 99 | struct pending **listhead; |
| 100 | { |
| 101 | register struct pending *link; |
| 102 | |
| 103 | /* If this is an alias for another symbol, don't add it. */ |
| 104 | if (symbol->ginfo.name && symbol->ginfo.name[0] == '#') |
| 105 | return; |
| 106 | |
| 107 | /* We keep PENDINGSIZE symbols in each link of the list. |
| 108 | If we don't have a link with room in it, add a new link. */ |
| 109 | if (*listhead == NULL || (*listhead)->nsyms == PENDINGSIZE) |
| 110 | { |
| 111 | if (free_pendings) |
| 112 | { |
| 113 | link = free_pendings; |
| 114 | free_pendings = link->next; |
| 115 | } |
| 116 | else |
| 117 | { |
| 118 | link = (struct pending *) xmalloc (sizeof (struct pending)); |
| 119 | } |
| 120 | |
| 121 | link->next = *listhead; |
| 122 | *listhead = link; |
| 123 | link->nsyms = 0; |
| 124 | } |
| 125 | |
| 126 | (*listhead)->symbol[(*listhead)->nsyms++] = symbol; |
| 127 | } |
| 128 | |
| 129 | /* Find a symbol named NAME on a LIST. NAME need not be '\0'-terminated; |
| 130 | LENGTH is the length of the name. */ |
| 131 | |
| 132 | struct symbol * |
| 133 | find_symbol_in_list (list, name, length) |
| 134 | struct pending *list; |
| 135 | char *name; |
| 136 | int length; |
| 137 | { |
| 138 | int j; |
| 139 | char *pp; |
| 140 | |
| 141 | while (list != NULL) |
| 142 | { |
| 143 | for (j = list->nsyms; --j >= 0; ) |
| 144 | { |
| 145 | pp = SYMBOL_NAME (list->symbol[j]); |
| 146 | if (*pp == *name && strncmp (pp, name, length) == 0 && |
| 147 | pp[length] == '\0') |
| 148 | { |
| 149 | return (list->symbol[j]); |
| 150 | } |
| 151 | } |
| 152 | list = list->next; |
| 153 | } |
| 154 | return (NULL); |
| 155 | } |
| 156 | |
| 157 | /* At end of reading syms, or in case of quit, |
| 158 | really free as many `struct pending's as we can easily find. */ |
| 159 | |
| 160 | /* ARGSUSED */ |
| 161 | void |
| 162 | really_free_pendings (foo) |
| 163 | int foo; |
| 164 | { |
| 165 | struct pending *next, *next1; |
| 166 | |
| 167 | for (next = free_pendings; next; next = next1) |
| 168 | { |
| 169 | next1 = next->next; |
| 170 | free ((PTR)next); |
| 171 | } |
| 172 | free_pendings = NULL; |
| 173 | |
| 174 | free_pending_blocks (); |
| 175 | |
| 176 | for (next = file_symbols; next != NULL; next = next1) |
| 177 | { |
| 178 | next1 = next->next; |
| 179 | free ((PTR)next); |
| 180 | } |
| 181 | file_symbols = NULL; |
| 182 | |
| 183 | for (next = global_symbols; next != NULL; next = next1) |
| 184 | { |
| 185 | next1 = next->next; |
| 186 | free ((PTR)next); |
| 187 | } |
| 188 | global_symbols = NULL; |
| 189 | } |
| 190 | |
| 191 | /* This function is called to discard any pending blocks. */ |
| 192 | |
| 193 | void |
| 194 | free_pending_blocks () |
| 195 | { |
| 196 | #if 0 /* Now we make the links in the symbol_obstack, so don't free them. */ |
| 197 | struct pending_block *bnext, *bnext1; |
| 198 | |
| 199 | for (bnext = pending_blocks; bnext; bnext = bnext1) |
| 200 | { |
| 201 | bnext1 = bnext->next; |
| 202 | free ((PTR)bnext); |
| 203 | } |
| 204 | #endif |
| 205 | pending_blocks = NULL; |
| 206 | } |
| 207 | |
| 208 | /* Take one of the lists of symbols and make a block from it. |
| 209 | Keep the order the symbols have in the list (reversed from the input file). |
| 210 | Put the block on the list of pending blocks. */ |
| 211 | |
| 212 | void |
| 213 | finish_block (symbol, listhead, old_blocks, start, end, objfile) |
| 214 | struct symbol *symbol; |
| 215 | struct pending **listhead; |
| 216 | struct pending_block *old_blocks; |
| 217 | CORE_ADDR start, end; |
| 218 | struct objfile *objfile; |
| 219 | { |
| 220 | register struct pending *next, *next1; |
| 221 | register struct block *block; |
| 222 | register struct pending_block *pblock; |
| 223 | struct pending_block *opblock; |
| 224 | register int i; |
| 225 | register int j; |
| 226 | |
| 227 | /* Count the length of the list of symbols. */ |
| 228 | |
| 229 | for (next = *listhead, i = 0; |
| 230 | next; |
| 231 | i += next->nsyms, next = next->next) |
| 232 | { |
| 233 | /*EMPTY*/; |
| 234 | } |
| 235 | |
| 236 | block = (struct block *) obstack_alloc (&objfile -> symbol_obstack, |
| 237 | (sizeof (struct block) + ((i - 1) * sizeof (struct symbol *)))); |
| 238 | |
| 239 | /* Copy the symbols into the block. */ |
| 240 | |
| 241 | BLOCK_NSYMS (block) = i; |
| 242 | for (next = *listhead; next; next = next->next) |
| 243 | { |
| 244 | for (j = next->nsyms - 1; j >= 0; j--) |
| 245 | { |
| 246 | BLOCK_SYM (block, --i) = next->symbol[j]; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | BLOCK_START (block) = start; |
| 251 | BLOCK_END (block) = end; |
| 252 | /* Superblock filled in when containing block is made */ |
| 253 | BLOCK_SUPERBLOCK (block) = NULL; |
| 254 | BLOCK_GCC_COMPILED (block) = processing_gcc_compilation; |
| 255 | |
| 256 | /* Put the block in as the value of the symbol that names it. */ |
| 257 | |
| 258 | if (symbol) |
| 259 | { |
| 260 | struct type *ftype = SYMBOL_TYPE (symbol); |
| 261 | SYMBOL_BLOCK_VALUE (symbol) = block; |
| 262 | BLOCK_FUNCTION (block) = symbol; |
| 263 | |
| 264 | if (TYPE_NFIELDS (ftype) <= 0) |
| 265 | { |
| 266 | /* No parameter type information is recorded with the function's |
| 267 | type. Set that from the type of the parameter symbols. */ |
| 268 | int nparams = 0, iparams; |
| 269 | struct symbol *sym; |
| 270 | for (i = 0; i < BLOCK_NSYMS (block); i++) |
| 271 | { |
| 272 | sym = BLOCK_SYM (block, i); |
| 273 | switch (SYMBOL_CLASS (sym)) |
| 274 | { |
| 275 | case LOC_ARG: |
| 276 | case LOC_REF_ARG: |
| 277 | case LOC_REGPARM: |
| 278 | case LOC_REGPARM_ADDR: |
| 279 | case LOC_BASEREG_ARG: |
| 280 | case LOC_LOCAL_ARG: |
| 281 | nparams++; |
| 282 | break; |
| 283 | case LOC_UNDEF: |
| 284 | case LOC_CONST: |
| 285 | case LOC_STATIC: |
| 286 | case LOC_REGISTER: |
| 287 | case LOC_LOCAL: |
| 288 | case LOC_TYPEDEF: |
| 289 | case LOC_LABEL: |
| 290 | case LOC_BLOCK: |
| 291 | case LOC_CONST_BYTES: |
| 292 | case LOC_BASEREG: |
| 293 | case LOC_UNRESOLVED: |
| 294 | case LOC_OPTIMIZED_OUT: |
| 295 | default: |
| 296 | break; |
| 297 | } |
| 298 | } |
| 299 | if (nparams > 0) |
| 300 | { |
| 301 | TYPE_NFIELDS (ftype) = nparams; |
| 302 | TYPE_FIELDS (ftype) = (struct field *) |
| 303 | TYPE_ALLOC (ftype, nparams * sizeof (struct field)); |
| 304 | |
| 305 | for (i = iparams = 0; iparams < nparams; i++) |
| 306 | { |
| 307 | sym = BLOCK_SYM (block, i); |
| 308 | switch (SYMBOL_CLASS (sym)) |
| 309 | { |
| 310 | case LOC_ARG: |
| 311 | case LOC_REF_ARG: |
| 312 | case LOC_REGPARM: |
| 313 | case LOC_REGPARM_ADDR: |
| 314 | case LOC_BASEREG_ARG: |
| 315 | case LOC_LOCAL_ARG: |
| 316 | TYPE_FIELD_TYPE (ftype, iparams) = SYMBOL_TYPE (sym); |
| 317 | iparams++; |
| 318 | break; |
| 319 | case LOC_UNDEF: |
| 320 | case LOC_CONST: |
| 321 | case LOC_STATIC: |
| 322 | case LOC_REGISTER: |
| 323 | case LOC_LOCAL: |
| 324 | case LOC_TYPEDEF: |
| 325 | case LOC_LABEL: |
| 326 | case LOC_BLOCK: |
| 327 | case LOC_CONST_BYTES: |
| 328 | case LOC_BASEREG: |
| 329 | case LOC_UNRESOLVED: |
| 330 | case LOC_OPTIMIZED_OUT: |
| 331 | default: |
| 332 | break; |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | else |
| 339 | { |
| 340 | BLOCK_FUNCTION (block) = NULL; |
| 341 | } |
| 342 | |
| 343 | /* Now "free" the links of the list, and empty the list. */ |
| 344 | |
| 345 | for (next = *listhead; next; next = next1) |
| 346 | { |
| 347 | next1 = next->next; |
| 348 | next->next = free_pendings; |
| 349 | free_pendings = next; |
| 350 | } |
| 351 | *listhead = NULL; |
| 352 | |
| 353 | #if 1 |
| 354 | /* Check to be sure that the blocks have an end address that is |
| 355 | greater than starting address */ |
| 356 | |
| 357 | if (BLOCK_END (block) < BLOCK_START (block)) |
| 358 | { |
| 359 | if (symbol) |
| 360 | { |
| 361 | complain (&block_end_complaint, SYMBOL_SOURCE_NAME (symbol)); |
| 362 | } |
| 363 | else |
| 364 | { |
| 365 | complain (&anon_block_end_complaint, BLOCK_END (block), BLOCK_START (block)); |
| 366 | } |
| 367 | /* Better than nothing */ |
| 368 | BLOCK_END (block) = BLOCK_START (block); |
| 369 | } |
| 370 | #endif |
| 371 | |
| 372 | /* Install this block as the superblock |
| 373 | of all blocks made since the start of this scope |
| 374 | that don't have superblocks yet. */ |
| 375 | |
| 376 | opblock = NULL; |
| 377 | for (pblock = pending_blocks; pblock != old_blocks; pblock = pblock->next) |
| 378 | { |
| 379 | if (BLOCK_SUPERBLOCK (pblock->block) == NULL) |
| 380 | { |
| 381 | #if 1 |
| 382 | /* Check to be sure the blocks are nested as we receive them. |
| 383 | If the compiler/assembler/linker work, this just burns a small |
| 384 | amount of time. */ |
| 385 | if (BLOCK_START (pblock->block) < BLOCK_START (block) || |
| 386 | BLOCK_END (pblock->block) > BLOCK_END (block)) |
| 387 | { |
| 388 | if (symbol) |
| 389 | { |
| 390 | complain (&innerblock_complaint, |
| 391 | SYMBOL_SOURCE_NAME (symbol)); |
| 392 | } |
| 393 | else |
| 394 | { |
| 395 | complain (&innerblock_anon_complaint, BLOCK_START (pblock->block), |
| 396 | BLOCK_END (pblock->block), BLOCK_START (block), |
| 397 | BLOCK_END (block)); |
| 398 | } |
| 399 | if (BLOCK_START (pblock->block) < BLOCK_START (block)) |
| 400 | BLOCK_START (pblock->block) = BLOCK_START (block); |
| 401 | if (BLOCK_END (pblock->block) > BLOCK_END (block)) |
| 402 | BLOCK_END (pblock->block) = BLOCK_END (block); |
| 403 | } |
| 404 | #endif |
| 405 | BLOCK_SUPERBLOCK (pblock->block) = block; |
| 406 | } |
| 407 | opblock = pblock; |
| 408 | } |
| 409 | |
| 410 | record_pending_block (objfile, block, opblock); |
| 411 | } |
| 412 | |
| 413 | /* Record BLOCK on the list of all blocks in the file. Put it after |
| 414 | OPBLOCK, or at the beginning if opblock is NULL. This puts the block |
| 415 | in the list after all its subblocks. |
| 416 | |
| 417 | Allocate the pending block struct in the symbol_obstack to save |
| 418 | time. This wastes a little space. FIXME: Is it worth it? */ |
| 419 | |
| 420 | void |
| 421 | record_pending_block (objfile, block, opblock) |
| 422 | struct objfile* objfile; |
| 423 | struct block *block; |
| 424 | struct pending_block *opblock; |
| 425 | { |
| 426 | register struct pending_block *pblock; |
| 427 | |
| 428 | pblock = (struct pending_block *) |
| 429 | obstack_alloc (&objfile -> symbol_obstack, sizeof (struct pending_block)); |
| 430 | pblock -> block = block; |
| 431 | if (opblock) |
| 432 | { |
| 433 | pblock -> next = opblock -> next; |
| 434 | opblock -> next = pblock; |
| 435 | } |
| 436 | else |
| 437 | { |
| 438 | pblock -> next = pending_blocks; |
| 439 | pending_blocks = pblock; |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | /* Note that this is only used in this file and in dstread.c, which should be |
| 444 | fixed to not need direct access to this function. When that is done, it can |
| 445 | be made static again. */ |
| 446 | |
| 447 | struct blockvector * |
| 448 | make_blockvector (objfile) |
| 449 | struct objfile *objfile; |
| 450 | { |
| 451 | register struct pending_block *next; |
| 452 | register struct blockvector *blockvector; |
| 453 | register int i; |
| 454 | |
| 455 | /* Count the length of the list of blocks. */ |
| 456 | |
| 457 | for (next = pending_blocks, i = 0; next; next = next->next, i++) {;} |
| 458 | |
| 459 | blockvector = (struct blockvector *) |
| 460 | obstack_alloc (&objfile -> symbol_obstack, |
| 461 | (sizeof (struct blockvector) |
| 462 | + (i - 1) * sizeof (struct block *))); |
| 463 | |
| 464 | /* Copy the blocks into the blockvector. |
| 465 | This is done in reverse order, which happens to put |
| 466 | the blocks into the proper order (ascending starting address). |
| 467 | finish_block has hair to insert each block into the list |
| 468 | after its subblocks in order to make sure this is true. */ |
| 469 | |
| 470 | BLOCKVECTOR_NBLOCKS (blockvector) = i; |
| 471 | for (next = pending_blocks; next; next = next->next) |
| 472 | { |
| 473 | BLOCKVECTOR_BLOCK (blockvector, --i) = next->block; |
| 474 | } |
| 475 | |
| 476 | #if 0 /* Now we make the links in the obstack, so don't free them. */ |
| 477 | /* Now free the links of the list, and empty the list. */ |
| 478 | |
| 479 | for (next = pending_blocks; next; next = next1) |
| 480 | { |
| 481 | next1 = next->next; |
| 482 | free (next); |
| 483 | } |
| 484 | #endif |
| 485 | pending_blocks = NULL; |
| 486 | |
| 487 | #if 1 /* FIXME, shut this off after a while to speed up symbol reading. */ |
| 488 | /* Some compilers output blocks in the wrong order, but we depend |
| 489 | on their being in the right order so we can binary search. |
| 490 | Check the order and moan about it. FIXME. */ |
| 491 | if (BLOCKVECTOR_NBLOCKS (blockvector) > 1) |
| 492 | { |
| 493 | for (i = 1; i < BLOCKVECTOR_NBLOCKS (blockvector); i++) |
| 494 | { |
| 495 | if (BLOCK_START(BLOCKVECTOR_BLOCK (blockvector, i-1)) |
| 496 | > BLOCK_START(BLOCKVECTOR_BLOCK (blockvector, i))) |
| 497 | { |
| 498 | |
| 499 | /* FIXME-32x64: loses if CORE_ADDR doesn't fit in a |
| 500 | long. Possible solutions include a version of |
| 501 | complain which takes a callback, a |
| 502 | sprintf_address_numeric to match |
| 503 | print_address_numeric, or a way to set up a GDB_FILE |
| 504 | * which causes sprintf rather than fprintf to be |
| 505 | called. */ |
| 506 | |
| 507 | complain (&blockvector_complaint, |
| 508 | (unsigned long) BLOCK_START(BLOCKVECTOR_BLOCK (blockvector, i))); |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | #endif |
| 513 | |
| 514 | return (blockvector); |
| 515 | } |
| 516 | |
| 517 | \f |
| 518 | /* Start recording information about source code that came from an included |
| 519 | (or otherwise merged-in) source file with a different name. NAME is |
| 520 | the name of the file (cannot be NULL), DIRNAME is the directory in which |
| 521 | it resides (or NULL if not known). */ |
| 522 | |
| 523 | void |
| 524 | start_subfile (name, dirname) |
| 525 | char *name; |
| 526 | char *dirname; |
| 527 | { |
| 528 | register struct subfile *subfile; |
| 529 | |
| 530 | /* See if this subfile is already known as a subfile of the |
| 531 | current main source file. */ |
| 532 | |
| 533 | for (subfile = subfiles; subfile; subfile = subfile->next) |
| 534 | { |
| 535 | if (STREQ (subfile->name, name)) |
| 536 | { |
| 537 | current_subfile = subfile; |
| 538 | return; |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | /* This subfile is not known. Add an entry for it. |
| 543 | Make an entry for this subfile in the list of all subfiles |
| 544 | of the current main source file. */ |
| 545 | |
| 546 | subfile = (struct subfile *) xmalloc (sizeof (struct subfile)); |
| 547 | subfile->next = subfiles; |
| 548 | subfiles = subfile; |
| 549 | current_subfile = subfile; |
| 550 | |
| 551 | /* Save its name and compilation directory name */ |
| 552 | subfile->name = (name == NULL) ? NULL : savestring (name, strlen (name)); |
| 553 | subfile->dirname = |
| 554 | (dirname == NULL) ? NULL : savestring (dirname, strlen (dirname)); |
| 555 | |
| 556 | /* Initialize line-number recording for this subfile. */ |
| 557 | subfile->line_vector = NULL; |
| 558 | |
| 559 | /* Default the source language to whatever can be deduced from |
| 560 | the filename. If nothing can be deduced (such as for a C/C++ |
| 561 | include file with a ".h" extension), then inherit whatever |
| 562 | language the previous subfile had. This kludgery is necessary |
| 563 | because there is no standard way in some object formats to |
| 564 | record the source language. Also, when symtabs are allocated |
| 565 | we try to deduce a language then as well, but it is too late |
| 566 | for us to use that information while reading symbols, since |
| 567 | symtabs aren't allocated until after all the symbols have |
| 568 | been processed for a given source file. */ |
| 569 | |
| 570 | subfile->language = deduce_language_from_filename (subfile->name); |
| 571 | if (subfile->language == language_unknown && |
| 572 | subfile->next != NULL) |
| 573 | { |
| 574 | subfile->language = subfile->next->language; |
| 575 | } |
| 576 | |
| 577 | /* Initialize the debug format string to NULL. We may supply it |
| 578 | later via a call to record_debugformat. */ |
| 579 | subfile->debugformat = NULL; |
| 580 | |
| 581 | /* cfront output is a C program, so in most ways it looks like a C |
| 582 | program. But to demangle we need to set the language to C++. We |
| 583 | can distinguish cfront code by the fact that it has #line |
| 584 | directives which specify a file name ending in .C. |
| 585 | |
| 586 | So if the filename of this subfile ends in .C, then change the language |
| 587 | of any pending subfiles from C to C++. We also accept any other C++ |
| 588 | suffixes accepted by deduce_language_from_filename (in particular, |
| 589 | some people use .cxx with cfront). */ |
| 590 | /* Likewise for f2c. */ |
| 591 | |
| 592 | if (subfile->name) |
| 593 | { |
| 594 | struct subfile *s; |
| 595 | enum language sublang = deduce_language_from_filename (subfile->name); |
| 596 | |
| 597 | if (sublang == language_cplus || sublang == language_fortran) |
| 598 | for (s = subfiles; s != NULL; s = s->next) |
| 599 | if (s->language == language_c) |
| 600 | s->language = sublang; |
| 601 | } |
| 602 | |
| 603 | /* And patch up this file if necessary. */ |
| 604 | if (subfile->language == language_c |
| 605 | && subfile->next != NULL |
| 606 | && (subfile->next->language == language_cplus |
| 607 | || subfile->next->language == language_fortran)) |
| 608 | { |
| 609 | subfile->language = subfile->next->language; |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | /* For stabs readers, the first N_SO symbol is assumed to be the source |
| 614 | file name, and the subfile struct is initialized using that assumption. |
| 615 | If another N_SO symbol is later seen, immediately following the first |
| 616 | one, then the first one is assumed to be the directory name and the |
| 617 | second one is really the source file name. |
| 618 | |
| 619 | So we have to patch up the subfile struct by moving the old name value to |
| 620 | dirname and remembering the new name. Some sanity checking is performed |
| 621 | to ensure that the state of the subfile struct is reasonable and that the |
| 622 | old name we are assuming to be a directory name actually is (by checking |
| 623 | for a trailing '/'). */ |
| 624 | |
| 625 | void |
| 626 | patch_subfile_names (subfile, name) |
| 627 | struct subfile *subfile; |
| 628 | char *name; |
| 629 | { |
| 630 | if (subfile != NULL && subfile->dirname == NULL && subfile->name != NULL |
| 631 | && subfile->name[strlen(subfile->name)-1] == '/') |
| 632 | { |
| 633 | subfile->dirname = subfile->name; |
| 634 | subfile->name = savestring (name, strlen (name)); |
| 635 | last_source_file = name; |
| 636 | |
| 637 | /* Default the source language to whatever can be deduced from |
| 638 | the filename. If nothing can be deduced (such as for a C/C++ |
| 639 | include file with a ".h" extension), then inherit whatever |
| 640 | language the previous subfile had. This kludgery is necessary |
| 641 | because there is no standard way in some object formats to |
| 642 | record the source language. Also, when symtabs are allocated |
| 643 | we try to deduce a language then as well, but it is too late |
| 644 | for us to use that information while reading symbols, since |
| 645 | symtabs aren't allocated until after all the symbols have |
| 646 | been processed for a given source file. */ |
| 647 | |
| 648 | subfile->language = deduce_language_from_filename (subfile->name); |
| 649 | if (subfile->language == language_unknown && |
| 650 | subfile->next != NULL) |
| 651 | { |
| 652 | subfile->language = subfile->next->language; |
| 653 | } |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | \f |
| 658 | /* Handle the N_BINCL and N_EINCL symbol types |
| 659 | that act like N_SOL for switching source files |
| 660 | (different subfiles, as we call them) within one object file, |
| 661 | but using a stack rather than in an arbitrary order. */ |
| 662 | |
| 663 | void |
| 664 | push_subfile () |
| 665 | { |
| 666 | register struct subfile_stack *tem |
| 667 | = (struct subfile_stack *) xmalloc (sizeof (struct subfile_stack)); |
| 668 | |
| 669 | tem->next = subfile_stack; |
| 670 | subfile_stack = tem; |
| 671 | if (current_subfile == NULL || current_subfile->name == NULL) |
| 672 | { |
| 673 | abort (); |
| 674 | } |
| 675 | tem->name = current_subfile->name; |
| 676 | } |
| 677 | |
| 678 | char * |
| 679 | pop_subfile () |
| 680 | { |
| 681 | register char *name; |
| 682 | register struct subfile_stack *link = subfile_stack; |
| 683 | |
| 684 | if (link == NULL) |
| 685 | { |
| 686 | abort (); |
| 687 | } |
| 688 | name = link->name; |
| 689 | subfile_stack = link->next; |
| 690 | free ((PTR)link); |
| 691 | return (name); |
| 692 | } |
| 693 | |
| 694 | \f |
| 695 | /* Add a linetable entry for line number LINE and address PC to the line |
| 696 | vector for SUBFILE. */ |
| 697 | |
| 698 | void |
| 699 | record_line (subfile, line, pc) |
| 700 | register struct subfile *subfile; |
| 701 | int line; |
| 702 | CORE_ADDR pc; |
| 703 | { |
| 704 | struct linetable_entry *e; |
| 705 | /* Ignore the dummy line number in libg.o */ |
| 706 | |
| 707 | if (line == 0xffff) |
| 708 | { |
| 709 | return; |
| 710 | } |
| 711 | |
| 712 | /* Make sure line vector exists and is big enough. */ |
| 713 | if (!subfile->line_vector) |
| 714 | { |
| 715 | subfile->line_vector_length = INITIAL_LINE_VECTOR_LENGTH; |
| 716 | subfile->line_vector = (struct linetable *) |
| 717 | xmalloc (sizeof (struct linetable) |
| 718 | + subfile->line_vector_length * sizeof (struct linetable_entry)); |
| 719 | subfile->line_vector->nitems = 0; |
| 720 | have_line_numbers = 1; |
| 721 | } |
| 722 | |
| 723 | if (subfile->line_vector->nitems + 1 >= subfile->line_vector_length) |
| 724 | { |
| 725 | subfile->line_vector_length *= 2; |
| 726 | subfile->line_vector = (struct linetable *) |
| 727 | xrealloc ((char *) subfile->line_vector, (sizeof (struct linetable) |
| 728 | + subfile->line_vector_length * sizeof (struct linetable_entry))); |
| 729 | } |
| 730 | |
| 731 | e = subfile->line_vector->item + subfile->line_vector->nitems++; |
| 732 | e->line = line; e->pc = pc; |
| 733 | } |
| 734 | |
| 735 | |
| 736 | /* Needed in order to sort line tables from IBM xcoff files. Sigh! */ |
| 737 | |
| 738 | static int |
| 739 | compare_line_numbers (ln1p, ln2p) |
| 740 | const void *ln1p; |
| 741 | const void *ln2p; |
| 742 | { |
| 743 | struct linetable_entry *ln1 = (struct linetable_entry *) ln1p; |
| 744 | struct linetable_entry *ln2 = (struct linetable_entry *) ln2p; |
| 745 | |
| 746 | /* Note: this code does not assume that CORE_ADDRs can fit in ints. |
| 747 | Please keep it that way. */ |
| 748 | if (ln1->pc < ln2->pc) |
| 749 | return -1; |
| 750 | |
| 751 | if (ln1->pc > ln2->pc) |
| 752 | return 1; |
| 753 | |
| 754 | /* If pc equal, sort by line. I'm not sure whether this is optimum |
| 755 | behavior (see comment at struct linetable in symtab.h). */ |
| 756 | return ln1->line - ln2->line; |
| 757 | } |
| 758 | |
| 759 | \f |
| 760 | /* Start a new symtab for a new source file. |
| 761 | Called, for example, when a stabs symbol of type N_SO is seen, or when |
| 762 | a DWARF TAG_compile_unit DIE is seen. |
| 763 | It indicates the start of data for one original source file. */ |
| 764 | |
| 765 | void |
| 766 | start_symtab (name, dirname, start_addr) |
| 767 | char *name; |
| 768 | char *dirname; |
| 769 | CORE_ADDR start_addr; |
| 770 | { |
| 771 | |
| 772 | last_source_file = name; |
| 773 | last_source_start_addr = start_addr; |
| 774 | file_symbols = NULL; |
| 775 | global_symbols = NULL; |
| 776 | within_function = 0; |
| 777 | have_line_numbers = 0; |
| 778 | |
| 779 | /* Context stack is initially empty. Allocate first one with room for |
| 780 | 10 levels; reuse it forever afterward. */ |
| 781 | if (context_stack == NULL) |
| 782 | { |
| 783 | context_stack_size = INITIAL_CONTEXT_STACK_SIZE; |
| 784 | context_stack = (struct context_stack *) |
| 785 | xmalloc (context_stack_size * sizeof (struct context_stack)); |
| 786 | } |
| 787 | context_stack_depth = 0; |
| 788 | |
| 789 | /* Initialize the list of sub source files with one entry |
| 790 | for this file (the top-level source file). */ |
| 791 | |
| 792 | subfiles = NULL; |
| 793 | current_subfile = NULL; |
| 794 | start_subfile (name, dirname); |
| 795 | } |
| 796 | |
| 797 | /* Finish the symbol definitions for one main source file, |
| 798 | close off all the lexical contexts for that file |
| 799 | (creating struct block's for them), then make the struct symtab |
| 800 | for that file and put it in the list of all such. |
| 801 | |
| 802 | END_ADDR is the address of the end of the file's text. |
| 803 | SECTION is the section number (in objfile->section_offsets) of |
| 804 | the blockvector and linetable. |
| 805 | |
| 806 | Note that it is possible for end_symtab() to return NULL. In particular, |
| 807 | for the DWARF case at least, it will return NULL when it finds a |
| 808 | compilation unit that has exactly one DIE, a TAG_compile_unit DIE. This |
| 809 | can happen when we link in an object file that was compiled from an empty |
| 810 | source file. Returning NULL is probably not the correct thing to do, |
| 811 | because then gdb will never know about this empty file (FIXME). */ |
| 812 | |
| 813 | struct symtab * |
| 814 | end_symtab (end_addr, objfile, section) |
| 815 | CORE_ADDR end_addr; |
| 816 | struct objfile *objfile; |
| 817 | int section; |
| 818 | { |
| 819 | register struct symtab *symtab = NULL; |
| 820 | register struct blockvector *blockvector; |
| 821 | register struct subfile *subfile; |
| 822 | register struct context_stack *cstk; |
| 823 | struct subfile *nextsub; |
| 824 | |
| 825 | /* Finish the lexical context of the last function in the file; |
| 826 | pop the context stack. */ |
| 827 | |
| 828 | if (context_stack_depth > 0) |
| 829 | { |
| 830 | cstk = pop_context(); |
| 831 | /* Make a block for the local symbols within. */ |
| 832 | finish_block (cstk->name, &local_symbols, cstk->old_blocks, |
| 833 | cstk->start_addr, end_addr, objfile); |
| 834 | |
| 835 | if (context_stack_depth > 0) |
| 836 | { |
| 837 | /* This is said to happen with SCO. The old coffread.c code |
| 838 | simply emptied the context stack, so we do the same. FIXME: |
| 839 | Find out why it is happening. This is not believed to happen |
| 840 | in most cases (even for coffread.c); it used to be an abort(). */ |
| 841 | static struct complaint msg = |
| 842 | {"Context stack not empty in end_symtab", 0, 0}; |
| 843 | complain (&msg); |
| 844 | context_stack_depth = 0; |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | /* Reordered executables may have out of order pending blocks; if |
| 849 | OBJF_REORDERED is true, then sort the pending blocks. */ |
| 850 | if ((objfile->flags & OBJF_REORDERED) && pending_blocks) |
| 851 | { |
| 852 | /* FIXME! Remove this horrid bubble sort and use merge sort!!! */ |
| 853 | int swapped; |
| 854 | do |
| 855 | { |
| 856 | struct pending_block *pb, *pbnext; |
| 857 | |
| 858 | pb = pending_blocks; |
| 859 | pbnext = pb->next; |
| 860 | swapped = 0; |
| 861 | |
| 862 | while (pbnext) |
| 863 | { |
| 864 | /* swap blocks if unordered! */ |
| 865 | |
| 866 | if (BLOCK_START(pb->block) < BLOCK_START(pbnext->block)) |
| 867 | { |
| 868 | struct block *tmp = pb->block; |
| 869 | pb->block = pbnext->block; |
| 870 | pbnext->block = tmp; |
| 871 | swapped = 1; |
| 872 | } |
| 873 | pb = pbnext; |
| 874 | pbnext = pbnext->next; |
| 875 | } |
| 876 | } while (swapped); |
| 877 | } |
| 878 | |
| 879 | /* Cleanup any undefined types that have been left hanging around |
| 880 | (this needs to be done before the finish_blocks so that |
| 881 | file_symbols is still good). |
| 882 | |
| 883 | Both cleanup_undefined_types and finish_global_stabs are stabs |
| 884 | specific, but harmless for other symbol readers, since on gdb |
| 885 | startup or when finished reading stabs, the state is set so these |
| 886 | are no-ops. FIXME: Is this handled right in case of QUIT? Can |
| 887 | we make this cleaner? */ |
| 888 | |
| 889 | cleanup_undefined_types (); |
| 890 | finish_global_stabs (objfile); |
| 891 | |
| 892 | if (pending_blocks == NULL |
| 893 | && file_symbols == NULL |
| 894 | && global_symbols == NULL |
| 895 | && have_line_numbers == 0) |
| 896 | { |
| 897 | /* Ignore symtabs that have no functions with real debugging info */ |
| 898 | blockvector = NULL; |
| 899 | } |
| 900 | else |
| 901 | { |
| 902 | /* Define the STATIC_BLOCK & GLOBAL_BLOCK, and build the blockvector. */ |
| 903 | finish_block (0, &file_symbols, 0, last_source_start_addr, end_addr, |
| 904 | objfile); |
| 905 | finish_block (0, &global_symbols, 0, last_source_start_addr, end_addr, |
| 906 | objfile); |
| 907 | blockvector = make_blockvector (objfile); |
| 908 | } |
| 909 | |
| 910 | #ifdef PROCESS_LINENUMBER_HOOK |
| 911 | PROCESS_LINENUMBER_HOOK (); /* Needed for xcoff. */ |
| 912 | #endif |
| 913 | |
| 914 | /* Now create the symtab objects proper, one for each subfile. */ |
| 915 | /* (The main file is the last one on the chain.) */ |
| 916 | |
| 917 | for (subfile = subfiles; subfile; subfile = nextsub) |
| 918 | { |
| 919 | int linetablesize = 0; |
| 920 | /* If we have blocks of symbols, make a symtab. |
| 921 | Otherwise, just ignore this file and any line number info in it. */ |
| 922 | symtab = NULL; |
| 923 | if (blockvector) |
| 924 | { |
| 925 | if (subfile->line_vector) |
| 926 | { |
| 927 | linetablesize = sizeof (struct linetable) + |
| 928 | subfile->line_vector->nitems * sizeof (struct linetable_entry); |
| 929 | #if 0 |
| 930 | /* I think this is artifact from before it went on the obstack. |
| 931 | I doubt we'll need the memory between now and when we |
| 932 | free it later in this function. */ |
| 933 | /* First, shrink the linetable to make more memory. */ |
| 934 | subfile->line_vector = (struct linetable *) |
| 935 | xrealloc ((char *) subfile->line_vector, linetablesize); |
| 936 | #endif |
| 937 | |
| 938 | /* Like the pending blocks, the line table may be scrambled |
| 939 | in reordered executables. Sort it if OBJF_REORDERED is |
| 940 | true. */ |
| 941 | if (objfile->flags & OBJF_REORDERED) |
| 942 | qsort (subfile->line_vector->item, |
| 943 | subfile->line_vector->nitems, |
| 944 | sizeof (struct linetable_entry), compare_line_numbers); |
| 945 | } |
| 946 | |
| 947 | /* Now, allocate a symbol table. */ |
| 948 | symtab = allocate_symtab (subfile->name, objfile); |
| 949 | |
| 950 | /* Fill in its components. */ |
| 951 | symtab->blockvector = blockvector; |
| 952 | if (subfile->line_vector) |
| 953 | { |
| 954 | /* Reallocate the line table on the symbol obstack */ |
| 955 | symtab->linetable = (struct linetable *) |
| 956 | obstack_alloc (&objfile -> symbol_obstack, linetablesize); |
| 957 | memcpy (symtab->linetable, subfile->line_vector, linetablesize); |
| 958 | } |
| 959 | else |
| 960 | { |
| 961 | symtab->linetable = NULL; |
| 962 | } |
| 963 | symtab->block_line_section = section; |
| 964 | if (subfile->dirname) |
| 965 | { |
| 966 | /* Reallocate the dirname on the symbol obstack */ |
| 967 | symtab->dirname = (char *) |
| 968 | obstack_alloc (&objfile -> symbol_obstack, |
| 969 | strlen (subfile -> dirname) + 1); |
| 970 | strcpy (symtab->dirname, subfile->dirname); |
| 971 | } |
| 972 | else |
| 973 | { |
| 974 | symtab->dirname = NULL; |
| 975 | } |
| 976 | symtab->free_code = free_linetable; |
| 977 | symtab->free_ptr = NULL; |
| 978 | |
| 979 | /* Use whatever language we have been using for this subfile, |
| 980 | not the one that was deduced in allocate_symtab from the |
| 981 | filename. We already did our own deducing when we created |
| 982 | the subfile, and we may have altered our opinion of what |
| 983 | language it is from things we found in the symbols. */ |
| 984 | symtab->language = subfile->language; |
| 985 | |
| 986 | /* Save the debug format string (if any) in the symtab */ |
| 987 | if (subfile -> debugformat != NULL) |
| 988 | { |
| 989 | symtab->debugformat = obsavestring (subfile->debugformat, |
| 990 | strlen (subfile->debugformat), |
| 991 | &objfile -> symbol_obstack); |
| 992 | } |
| 993 | |
| 994 | /* All symtabs for the main file and the subfiles share a |
| 995 | blockvector, so we need to clear primary for everything but |
| 996 | the main file. */ |
| 997 | |
| 998 | symtab->primary = 0; |
| 999 | } |
| 1000 | if (subfile->name != NULL) |
| 1001 | { |
| 1002 | free ((PTR) subfile->name); |
| 1003 | } |
| 1004 | if (subfile->dirname != NULL) |
| 1005 | { |
| 1006 | free ((PTR) subfile->dirname); |
| 1007 | } |
| 1008 | if (subfile->line_vector != NULL) |
| 1009 | { |
| 1010 | free ((PTR) subfile->line_vector); |
| 1011 | } |
| 1012 | if (subfile->debugformat != NULL) |
| 1013 | { |
| 1014 | free ((PTR) subfile->debugformat); |
| 1015 | } |
| 1016 | |
| 1017 | nextsub = subfile->next; |
| 1018 | free ((PTR)subfile); |
| 1019 | } |
| 1020 | |
| 1021 | /* Set this for the main source file. */ |
| 1022 | if (symtab) |
| 1023 | { |
| 1024 | symtab->primary = 1; |
| 1025 | } |
| 1026 | |
| 1027 | last_source_file = NULL; |
| 1028 | current_subfile = NULL; |
| 1029 | |
| 1030 | return (symtab); |
| 1031 | } |
| 1032 | |
| 1033 | |
| 1034 | /* Push a context block. Args are an identifying nesting level (checkable |
| 1035 | when you pop it), and the starting PC address of this context. */ |
| 1036 | |
| 1037 | struct context_stack * |
| 1038 | push_context (desc, valu) |
| 1039 | int desc; |
| 1040 | CORE_ADDR valu; |
| 1041 | { |
| 1042 | register struct context_stack *new; |
| 1043 | |
| 1044 | if (context_stack_depth == context_stack_size) |
| 1045 | { |
| 1046 | context_stack_size *= 2; |
| 1047 | context_stack = (struct context_stack *) |
| 1048 | xrealloc ((char *) context_stack, |
| 1049 | (context_stack_size * sizeof (struct context_stack))); |
| 1050 | } |
| 1051 | |
| 1052 | new = &context_stack[context_stack_depth++]; |
| 1053 | new->depth = desc; |
| 1054 | new->locals = local_symbols; |
| 1055 | new->old_blocks = pending_blocks; |
| 1056 | new->start_addr = valu; |
| 1057 | new->name = NULL; |
| 1058 | |
| 1059 | local_symbols = NULL; |
| 1060 | |
| 1061 | return (new); |
| 1062 | } |
| 1063 | |
| 1064 | \f |
| 1065 | /* Compute a small integer hash code for the given name. */ |
| 1066 | |
| 1067 | int |
| 1068 | hashname (name) |
| 1069 | char *name; |
| 1070 | { |
| 1071 | register char *p = name; |
| 1072 | register int total = p[0]; |
| 1073 | register int c; |
| 1074 | |
| 1075 | c = p[1]; |
| 1076 | total += c << 2; |
| 1077 | if (c) |
| 1078 | { |
| 1079 | c = p[2]; |
| 1080 | total += c << 4; |
| 1081 | if (c) |
| 1082 | { |
| 1083 | total += p[3] << 6; |
| 1084 | } |
| 1085 | } |
| 1086 | |
| 1087 | /* Ensure result is positive. */ |
| 1088 | if (total < 0) |
| 1089 | { |
| 1090 | total += (1000 << 6); |
| 1091 | } |
| 1092 | return (total % HASHSIZE); |
| 1093 | } |
| 1094 | |
| 1095 | \f |
| 1096 | void |
| 1097 | record_debugformat (format) |
| 1098 | char *format; |
| 1099 | { |
| 1100 | current_subfile -> debugformat = savestring (format, strlen (format)); |
| 1101 | } |
| 1102 | |
| 1103 | \f |
| 1104 | /* Initialize anything that needs initializing when starting to read |
| 1105 | a fresh piece of a symbol file, e.g. reading in the stuff corresponding |
| 1106 | to a psymtab. */ |
| 1107 | |
| 1108 | void |
| 1109 | buildsym_init () |
| 1110 | { |
| 1111 | free_pendings = NULL; |
| 1112 | file_symbols = NULL; |
| 1113 | global_symbols = NULL; |
| 1114 | pending_blocks = NULL; |
| 1115 | } |
| 1116 | |
| 1117 | /* Initialize anything that needs initializing when a completely new |
| 1118 | symbol file is specified (not just adding some symbols from another |
| 1119 | file, e.g. a shared library). */ |
| 1120 | |
| 1121 | void |
| 1122 | buildsym_new_init () |
| 1123 | { |
| 1124 | buildsym_init (); |
| 1125 | } |
| 1126 | |
| 1127 | /* Initializer for this module */ |
| 1128 | |
| 1129 | void |
| 1130 | _initialize_buildsym () |
| 1131 | { |
| 1132 | } |