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