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