| 1 | /* Generic symbol file reading for the GNU debugger, GDB. |
| 2 | Copyright 1990, 1991, 1992, 1993 Free Software Foundation, Inc. |
| 3 | Contributed by Cygnus Support, using pieces from other GDB modules. |
| 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., 675 Mass Ave, Cambridge, MA 02139, USA. */ |
| 20 | |
| 21 | #include "defs.h" |
| 22 | #include "symtab.h" |
| 23 | #include "gdbtypes.h" |
| 24 | #include "gdbcore.h" |
| 25 | #include "frame.h" |
| 26 | #include "target.h" |
| 27 | #include "value.h" |
| 28 | #include "symfile.h" |
| 29 | #include "objfiles.h" |
| 30 | #include "gdbcmd.h" |
| 31 | #include "breakpoint.h" |
| 32 | #include "language.h" |
| 33 | #include "complaints.h" |
| 34 | #include "demangle.h" |
| 35 | #include "inferior.h" /* for write_pc */ |
| 36 | |
| 37 | #include <obstack.h> |
| 38 | #include <assert.h> |
| 39 | |
| 40 | #include <sys/types.h> |
| 41 | #include <fcntl.h> |
| 42 | #include <string.h> |
| 43 | #include <sys/stat.h> |
| 44 | #include <ctype.h> |
| 45 | |
| 46 | #ifndef O_BINARY |
| 47 | #define O_BINARY 0 |
| 48 | #endif |
| 49 | |
| 50 | /* Global variables owned by this file */ |
| 51 | |
| 52 | int readnow_symbol_files; /* Read full symbols immediately */ |
| 53 | |
| 54 | struct complaint oldsyms_complaint = { |
| 55 | "Replacing old symbols for `%s'", 0, 0 |
| 56 | }; |
| 57 | |
| 58 | struct complaint empty_symtab_complaint = { |
| 59 | "Empty symbol table found for `%s'", 0, 0 |
| 60 | }; |
| 61 | |
| 62 | /* External variables and functions referenced. */ |
| 63 | |
| 64 | extern int info_verbose; |
| 65 | |
| 66 | /* Functions this file defines */ |
| 67 | |
| 68 | static void |
| 69 | set_initial_language PARAMS ((void)); |
| 70 | |
| 71 | static void |
| 72 | load_command PARAMS ((char *, int)); |
| 73 | |
| 74 | static void |
| 75 | add_symbol_file_command PARAMS ((char *, int)); |
| 76 | |
| 77 | static void |
| 78 | cashier_psymtab PARAMS ((struct partial_symtab *)); |
| 79 | |
| 80 | static int |
| 81 | compare_psymbols PARAMS ((const void *, const void *)); |
| 82 | |
| 83 | static int |
| 84 | compare_symbols PARAMS ((const void *, const void *)); |
| 85 | |
| 86 | static bfd * |
| 87 | symfile_bfd_open PARAMS ((char *)); |
| 88 | |
| 89 | static void |
| 90 | find_sym_fns PARAMS ((struct objfile *)); |
| 91 | |
| 92 | /* List of all available sym_fns. On gdb startup, each object file reader |
| 93 | calls add_symtab_fns() to register information on each format it is |
| 94 | prepared to read. */ |
| 95 | |
| 96 | static struct sym_fns *symtab_fns = NULL; |
| 97 | |
| 98 | /* Structures with which to manage partial symbol allocation. */ |
| 99 | |
| 100 | struct psymbol_allocation_list global_psymbols = {0}, static_psymbols = {0}; |
| 101 | |
| 102 | /* Flag for whether user will be reloading symbols multiple times. |
| 103 | Defaults to ON for VxWorks, otherwise OFF. */ |
| 104 | |
| 105 | #ifdef SYMBOL_RELOADING_DEFAULT |
| 106 | int symbol_reloading = SYMBOL_RELOADING_DEFAULT; |
| 107 | #else |
| 108 | int symbol_reloading = 0; |
| 109 | #endif |
| 110 | |
| 111 | \f |
| 112 | /* Since this function is called from within qsort, in an ANSI environment |
| 113 | it must conform to the prototype for qsort, which specifies that the |
| 114 | comparison function takes two "void *" pointers. */ |
| 115 | |
| 116 | static int |
| 117 | compare_symbols (s1p, s2p) |
| 118 | const PTR s1p; |
| 119 | const PTR s2p; |
| 120 | { |
| 121 | register struct symbol **s1, **s2; |
| 122 | |
| 123 | s1 = (struct symbol **) s1p; |
| 124 | s2 = (struct symbol **) s2p; |
| 125 | |
| 126 | return (STRCMP (SYMBOL_NAME (*s1), SYMBOL_NAME (*s2))); |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | |
| 131 | LOCAL FUNCTION |
| 132 | |
| 133 | compare_psymbols -- compare two partial symbols by name |
| 134 | |
| 135 | DESCRIPTION |
| 136 | |
| 137 | Given pointer to two partial symbol table entries, compare |
| 138 | them by name and return -N, 0, or +N (ala strcmp). Typically |
| 139 | used by sorting routines like qsort(). |
| 140 | |
| 141 | NOTES |
| 142 | |
| 143 | Does direct compare of first two characters before punting |
| 144 | and passing to strcmp for longer compares. Note that the |
| 145 | original version had a bug whereby two null strings or two |
| 146 | identically named one character strings would return the |
| 147 | comparison of memory following the null byte. |
| 148 | |
| 149 | */ |
| 150 | |
| 151 | static int |
| 152 | compare_psymbols (s1p, s2p) |
| 153 | const PTR s1p; |
| 154 | const PTR s2p; |
| 155 | { |
| 156 | register char *st1 = SYMBOL_NAME ((struct partial_symbol *) s1p); |
| 157 | register char *st2 = SYMBOL_NAME ((struct partial_symbol *) s2p); |
| 158 | |
| 159 | if ((st1[0] - st2[0]) || !st1[0]) |
| 160 | { |
| 161 | return (st1[0] - st2[0]); |
| 162 | } |
| 163 | else if ((st1[1] - st2[1]) || !st1[1]) |
| 164 | { |
| 165 | return (st1[1] - st2[1]); |
| 166 | } |
| 167 | else |
| 168 | { |
| 169 | return (STRCMP (st1 + 2, st2 + 2)); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | void |
| 174 | sort_pst_symbols (pst) |
| 175 | struct partial_symtab *pst; |
| 176 | { |
| 177 | /* Sort the global list; don't sort the static list */ |
| 178 | |
| 179 | qsort (pst -> objfile -> global_psymbols.list + pst -> globals_offset, |
| 180 | pst -> n_global_syms, sizeof (struct partial_symbol), |
| 181 | compare_psymbols); |
| 182 | } |
| 183 | |
| 184 | /* Call sort_block_syms to sort alphabetically the symbols of one block. */ |
| 185 | |
| 186 | void |
| 187 | sort_block_syms (b) |
| 188 | register struct block *b; |
| 189 | { |
| 190 | qsort (&BLOCK_SYM (b, 0), BLOCK_NSYMS (b), |
| 191 | sizeof (struct symbol *), compare_symbols); |
| 192 | } |
| 193 | |
| 194 | /* Call sort_symtab_syms to sort alphabetically |
| 195 | the symbols of each block of one symtab. */ |
| 196 | |
| 197 | void |
| 198 | sort_symtab_syms (s) |
| 199 | register struct symtab *s; |
| 200 | { |
| 201 | register struct blockvector *bv; |
| 202 | int nbl; |
| 203 | int i; |
| 204 | register struct block *b; |
| 205 | |
| 206 | if (s == 0) |
| 207 | return; |
| 208 | bv = BLOCKVECTOR (s); |
| 209 | nbl = BLOCKVECTOR_NBLOCKS (bv); |
| 210 | for (i = 0; i < nbl; i++) |
| 211 | { |
| 212 | b = BLOCKVECTOR_BLOCK (bv, i); |
| 213 | if (BLOCK_SHOULD_SORT (b)) |
| 214 | sort_block_syms (b); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | /* Make a copy of the string at PTR with SIZE characters in the symbol obstack |
| 219 | (and add a null character at the end in the copy). |
| 220 | Returns the address of the copy. */ |
| 221 | |
| 222 | char * |
| 223 | obsavestring (ptr, size, obstackp) |
| 224 | char *ptr; |
| 225 | int size; |
| 226 | struct obstack *obstackp; |
| 227 | { |
| 228 | register char *p = (char *) obstack_alloc (obstackp, size + 1); |
| 229 | /* Open-coded memcpy--saves function call time. |
| 230 | These strings are usually short. */ |
| 231 | { |
| 232 | register char *p1 = ptr; |
| 233 | register char *p2 = p; |
| 234 | char *end = ptr + size; |
| 235 | while (p1 != end) |
| 236 | *p2++ = *p1++; |
| 237 | } |
| 238 | p[size] = 0; |
| 239 | return p; |
| 240 | } |
| 241 | |
| 242 | /* Concatenate strings S1, S2 and S3; return the new string. |
| 243 | Space is found in the symbol_obstack. */ |
| 244 | |
| 245 | char * |
| 246 | obconcat (obstackp, s1, s2, s3) |
| 247 | struct obstack *obstackp; |
| 248 | const char *s1, *s2, *s3; |
| 249 | { |
| 250 | register int len = strlen (s1) + strlen (s2) + strlen (s3) + 1; |
| 251 | register char *val = (char *) obstack_alloc (obstackp, len); |
| 252 | strcpy (val, s1); |
| 253 | strcat (val, s2); |
| 254 | strcat (val, s3); |
| 255 | return val; |
| 256 | } |
| 257 | |
| 258 | /* Get the symbol table that corresponds to a partial_symtab. |
| 259 | This is fast after the first time you do it. In fact, there |
| 260 | is an even faster macro PSYMTAB_TO_SYMTAB that does the fast |
| 261 | case inline. */ |
| 262 | |
| 263 | struct symtab * |
| 264 | psymtab_to_symtab (pst) |
| 265 | register struct partial_symtab *pst; |
| 266 | { |
| 267 | /* If it's been looked up before, return it. */ |
| 268 | if (pst->symtab) |
| 269 | return pst->symtab; |
| 270 | |
| 271 | /* If it has not yet been read in, read it. */ |
| 272 | if (!pst->readin) |
| 273 | { |
| 274 | (*pst->read_symtab) (pst); |
| 275 | } |
| 276 | |
| 277 | return pst->symtab; |
| 278 | } |
| 279 | |
| 280 | /* Initialize entry point information for this objfile. */ |
| 281 | |
| 282 | void |
| 283 | init_entry_point_info (objfile) |
| 284 | struct objfile *objfile; |
| 285 | { |
| 286 | /* Save startup file's range of PC addresses to help blockframe.c |
| 287 | decide where the bottom of the stack is. */ |
| 288 | |
| 289 | if (bfd_get_file_flags (objfile -> obfd) & EXEC_P) |
| 290 | { |
| 291 | /* Executable file -- record its entry point so we'll recognize |
| 292 | the startup file because it contains the entry point. */ |
| 293 | objfile -> ei.entry_point = bfd_get_start_address (objfile -> obfd); |
| 294 | } |
| 295 | else |
| 296 | { |
| 297 | /* Examination of non-executable.o files. Short-circuit this stuff. */ |
| 298 | /* ~0 will not be in any file, we hope. */ |
| 299 | objfile -> ei.entry_point = ~0; |
| 300 | /* set the startup file to be an empty range. */ |
| 301 | objfile -> ei.entry_file_lowpc = 0; |
| 302 | objfile -> ei.entry_file_highpc = 0; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | /* Get current entry point address. */ |
| 307 | |
| 308 | CORE_ADDR |
| 309 | entry_point_address() |
| 310 | { |
| 311 | return symfile_objfile ? symfile_objfile->ei.entry_point : 0; |
| 312 | } |
| 313 | |
| 314 | /* Remember the lowest-addressed loadable section we've seen. |
| 315 | This function is called via bfd_map_over_sections. */ |
| 316 | |
| 317 | #if 0 /* Not used yet */ |
| 318 | static void |
| 319 | find_lowest_section (abfd, sect, obj) |
| 320 | bfd *abfd; |
| 321 | asection *sect; |
| 322 | PTR obj; |
| 323 | { |
| 324 | asection **lowest = (asection **)obj; |
| 325 | |
| 326 | if (0 == (bfd_get_section_flags (abfd, sect) & SEC_LOAD)) |
| 327 | return; |
| 328 | if (!*lowest) |
| 329 | *lowest = sect; /* First loadable section */ |
| 330 | else if (bfd_section_vma (abfd, *lowest) >= bfd_section_vma (abfd, sect)) |
| 331 | *lowest = sect; /* A lower loadable section */ |
| 332 | } |
| 333 | #endif |
| 334 | |
| 335 | /* Process a symbol file, as either the main file or as a dynamically |
| 336 | loaded file. |
| 337 | |
| 338 | NAME is the file name (which will be tilde-expanded and made |
| 339 | absolute herein) (but we don't free or modify NAME itself). |
| 340 | FROM_TTY says how verbose to be. MAINLINE specifies whether this |
| 341 | is the main symbol file, or whether it's an extra symbol file such |
| 342 | as dynamically loaded code. If !mainline, ADDR is the address |
| 343 | where the text segment was loaded. If VERBO, the caller has printed |
| 344 | a verbose message about the symbol reading (and complaints can be |
| 345 | more terse about it). */ |
| 346 | |
| 347 | void |
| 348 | syms_from_objfile (objfile, addr, mainline, verbo) |
| 349 | struct objfile *objfile; |
| 350 | CORE_ADDR addr; |
| 351 | int mainline; |
| 352 | int verbo; |
| 353 | { |
| 354 | struct section_offsets *section_offsets; |
| 355 | asection *lowest_sect; |
| 356 | struct cleanup *old_chain; |
| 357 | |
| 358 | init_entry_point_info (objfile); |
| 359 | find_sym_fns (objfile); |
| 360 | |
| 361 | /* Make sure that partially constructed symbol tables will be cleaned up |
| 362 | if an error occurs during symbol reading. */ |
| 363 | old_chain = make_cleanup (free_objfile, objfile); |
| 364 | |
| 365 | if (mainline) |
| 366 | { |
| 367 | /* We will modify the main symbol table, make sure that all its users |
| 368 | will be cleaned up if an error occurs during symbol reading. */ |
| 369 | make_cleanup (clear_symtab_users, 0); |
| 370 | |
| 371 | /* Since no error yet, throw away the old symbol table. */ |
| 372 | |
| 373 | if (symfile_objfile != NULL) |
| 374 | { |
| 375 | free_objfile (symfile_objfile); |
| 376 | symfile_objfile = NULL; |
| 377 | } |
| 378 | |
| 379 | /* Currently we keep symbols from the add-symbol-file command. |
| 380 | If the user wants to get rid of them, they should do "symbol-file" |
| 381 | without arguments first. Not sure this is the best behavior |
| 382 | (PR 2207). */ |
| 383 | |
| 384 | (*objfile -> sf -> sym_new_init) (objfile); |
| 385 | } |
| 386 | |
| 387 | /* Convert addr into an offset rather than an absolute address. |
| 388 | We find the lowest address of a loaded segment in the objfile, |
| 389 | and assume that <addr> is where that got loaded. Due to historical |
| 390 | precedent, we warn if that doesn't happen to be the ".text" |
| 391 | segment. */ |
| 392 | |
| 393 | if (mainline) |
| 394 | { |
| 395 | addr = 0; /* No offset from objfile addresses. */ |
| 396 | } |
| 397 | else |
| 398 | { |
| 399 | lowest_sect = bfd_get_section_by_name (objfile->obfd, ".text"); |
| 400 | #if 0 |
| 401 | lowest_sect = 0; |
| 402 | bfd_map_over_sections (objfile->obfd, find_lowest_section, |
| 403 | (PTR) &lowest_sect); |
| 404 | #endif |
| 405 | |
| 406 | if (lowest_sect == 0) |
| 407 | warning ("no loadable sections found in added symbol-file %s", |
| 408 | objfile->name); |
| 409 | else if (0 == bfd_get_section_name (objfile->obfd, lowest_sect) |
| 410 | || !STREQ (".text", |
| 411 | bfd_get_section_name (objfile->obfd, lowest_sect))) |
| 412 | warning ("Lowest section in %s is %s at 0x%lx", |
| 413 | objfile->name, |
| 414 | bfd_section_name (objfile->obfd, lowest_sect), |
| 415 | (unsigned long) bfd_section_vma (objfile->obfd, lowest_sect)); |
| 416 | |
| 417 | if (lowest_sect) |
| 418 | addr -= bfd_section_vma (objfile->obfd, lowest_sect); |
| 419 | } |
| 420 | |
| 421 | /* Initialize symbol reading routines for this objfile, allow complaints to |
| 422 | appear for this new file, and record how verbose to be, then do the |
| 423 | initial symbol reading for this file. */ |
| 424 | |
| 425 | (*objfile -> sf -> sym_init) (objfile); |
| 426 | clear_complaints (1, verbo); |
| 427 | |
| 428 | section_offsets = (*objfile -> sf -> sym_offsets) (objfile, addr); |
| 429 | objfile->section_offsets = section_offsets; |
| 430 | |
| 431 | #ifndef IBM6000_TARGET |
| 432 | /* This is a SVR4/SunOS specific hack, I think. In any event, it |
| 433 | screws RS/6000. sym_offsets should be doing this sort of thing, |
| 434 | because it knows the mapping between bfd sections and |
| 435 | section_offsets. */ |
| 436 | /* This is a hack. As far as I can tell, section offsets are not |
| 437 | target dependent. They are all set to addr with a couple of |
| 438 | exceptions. The exceptions are sysvr4 shared libraries, whose |
| 439 | offsets are kept in solib structures anyway and rs6000 xcoff |
| 440 | which handles shared libraries in a completely unique way. |
| 441 | |
| 442 | Section offsets are built similarly, except that they are built |
| 443 | by adding addr in all cases because there is no clear mapping |
| 444 | from section_offsets into actual sections. Note that solib.c |
| 445 | has a different algorythm for finding section offsets. |
| 446 | |
| 447 | These should probably all be collapsed into some target |
| 448 | independent form of shared library support. FIXME. */ |
| 449 | |
| 450 | if (addr) |
| 451 | { |
| 452 | struct obj_section *s; |
| 453 | |
| 454 | for (s = objfile->sections; s < objfile->sections_end; ++s) |
| 455 | { |
| 456 | s->addr -= s->offset; |
| 457 | s->addr += addr; |
| 458 | s->endaddr -= s->offset; |
| 459 | s->endaddr += addr; |
| 460 | s->offset += addr; |
| 461 | } |
| 462 | } |
| 463 | #endif /* not IBM6000_TARGET */ |
| 464 | |
| 465 | (*objfile -> sf -> sym_read) (objfile, section_offsets, mainline); |
| 466 | |
| 467 | /* Don't allow char * to have a typename (else would get caddr_t). |
| 468 | Ditto void *. FIXME: Check whether this is now done by all the |
| 469 | symbol readers themselves (many of them now do), and if so remove |
| 470 | it from here. */ |
| 471 | |
| 472 | TYPE_NAME (lookup_pointer_type (builtin_type_char)) = 0; |
| 473 | TYPE_NAME (lookup_pointer_type (builtin_type_void)) = 0; |
| 474 | |
| 475 | /* Mark the objfile has having had initial symbol read attempted. Note |
| 476 | that this does not mean we found any symbols... */ |
| 477 | |
| 478 | objfile -> flags |= OBJF_SYMS; |
| 479 | |
| 480 | /* Discard cleanups as symbol reading was successful. */ |
| 481 | |
| 482 | discard_cleanups (old_chain); |
| 483 | } |
| 484 | |
| 485 | /* Perform required actions after either reading in the initial |
| 486 | symbols for a new objfile, or mapping in the symbols from a reusable |
| 487 | objfile. */ |
| 488 | |
| 489 | void |
| 490 | new_symfile_objfile (objfile, mainline, verbo) |
| 491 | struct objfile *objfile; |
| 492 | int mainline; |
| 493 | int verbo; |
| 494 | { |
| 495 | |
| 496 | /* If this is the main symbol file we have to clean up all users of the |
| 497 | old main symbol file. Otherwise it is sufficient to fixup all the |
| 498 | breakpoints that may have been redefined by this symbol file. */ |
| 499 | if (mainline) |
| 500 | { |
| 501 | /* OK, make it the "real" symbol file. */ |
| 502 | symfile_objfile = objfile; |
| 503 | |
| 504 | clear_symtab_users (); |
| 505 | } |
| 506 | else |
| 507 | { |
| 508 | breakpoint_re_set (); |
| 509 | } |
| 510 | |
| 511 | /* We're done reading the symbol file; finish off complaints. */ |
| 512 | clear_complaints (0, verbo); |
| 513 | } |
| 514 | |
| 515 | /* Process a symbol file, as either the main file or as a dynamically |
| 516 | loaded file. |
| 517 | |
| 518 | NAME is the file name (which will be tilde-expanded and made |
| 519 | absolute herein) (but we don't free or modify NAME itself). |
| 520 | FROM_TTY says how verbose to be. MAINLINE specifies whether this |
| 521 | is the main symbol file, or whether it's an extra symbol file such |
| 522 | as dynamically loaded code. If !mainline, ADDR is the address |
| 523 | where the text segment was loaded. |
| 524 | |
| 525 | Upon success, returns a pointer to the objfile that was added. |
| 526 | Upon failure, jumps back to command level (never returns). */ |
| 527 | |
| 528 | struct objfile * |
| 529 | symbol_file_add (name, from_tty, addr, mainline, mapped, readnow) |
| 530 | char *name; |
| 531 | int from_tty; |
| 532 | CORE_ADDR addr; |
| 533 | int mainline; |
| 534 | int mapped; |
| 535 | int readnow; |
| 536 | { |
| 537 | struct objfile *objfile; |
| 538 | struct partial_symtab *psymtab; |
| 539 | bfd *abfd; |
| 540 | |
| 541 | /* Open a bfd for the file, and give user a chance to burp if we'd be |
| 542 | interactively wiping out any existing symbols. */ |
| 543 | |
| 544 | abfd = symfile_bfd_open (name); |
| 545 | |
| 546 | if ((have_full_symbols () || have_partial_symbols ()) |
| 547 | && mainline |
| 548 | && from_tty |
| 549 | && !query ("Load new symbol table from \"%s\"? ", name)) |
| 550 | error ("Not confirmed."); |
| 551 | |
| 552 | objfile = allocate_objfile (abfd, mapped); |
| 553 | |
| 554 | /* If the objfile uses a mapped symbol file, and we have a psymtab for |
| 555 | it, then skip reading any symbols at this time. */ |
| 556 | |
| 557 | if ((objfile -> flags & OBJF_MAPPED) && (objfile -> flags & OBJF_SYMS)) |
| 558 | { |
| 559 | /* We mapped in an existing symbol table file that already has had |
| 560 | initial symbol reading performed, so we can skip that part. Notify |
| 561 | the user that instead of reading the symbols, they have been mapped. |
| 562 | */ |
| 563 | if (from_tty || info_verbose) |
| 564 | { |
| 565 | printf_filtered ("Mapped symbols for %s...", name); |
| 566 | wrap_here (""); |
| 567 | gdb_flush (gdb_stdout); |
| 568 | } |
| 569 | init_entry_point_info (objfile); |
| 570 | find_sym_fns (objfile); |
| 571 | } |
| 572 | else |
| 573 | { |
| 574 | /* We either created a new mapped symbol table, mapped an existing |
| 575 | symbol table file which has not had initial symbol reading |
| 576 | performed, or need to read an unmapped symbol table. */ |
| 577 | if (from_tty || info_verbose) |
| 578 | { |
| 579 | printf_filtered ("Reading symbols from %s...", name); |
| 580 | wrap_here (""); |
| 581 | gdb_flush (gdb_stdout); |
| 582 | } |
| 583 | syms_from_objfile (objfile, addr, mainline, from_tty); |
| 584 | } |
| 585 | |
| 586 | /* We now have at least a partial symbol table. Check to see if the |
| 587 | user requested that all symbols be read on initial access via either |
| 588 | the gdb startup command line or on a per symbol file basis. Expand |
| 589 | all partial symbol tables for this objfile if so. */ |
| 590 | |
| 591 | if (readnow || readnow_symbol_files) |
| 592 | { |
| 593 | if (from_tty || info_verbose) |
| 594 | { |
| 595 | printf_filtered ("expanding to full symbols..."); |
| 596 | wrap_here (""); |
| 597 | gdb_flush (gdb_stdout); |
| 598 | } |
| 599 | |
| 600 | for (psymtab = objfile -> psymtabs; |
| 601 | psymtab != NULL; |
| 602 | psymtab = psymtab -> next) |
| 603 | { |
| 604 | psymtab_to_symtab (psymtab); |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | if (from_tty || info_verbose) |
| 609 | { |
| 610 | printf_filtered ("done.\n"); |
| 611 | gdb_flush (gdb_stdout); |
| 612 | } |
| 613 | |
| 614 | new_symfile_objfile (objfile, mainline, from_tty); |
| 615 | |
| 616 | /* Getting new symbols may change our opinion about what is |
| 617 | frameless. */ |
| 618 | |
| 619 | reinit_frame_cache (); |
| 620 | |
| 621 | return (objfile); |
| 622 | } |
| 623 | |
| 624 | /* This is the symbol-file command. Read the file, analyze its symbols, |
| 625 | and add a struct symtab to a symtab list. */ |
| 626 | |
| 627 | void |
| 628 | symbol_file_command (args, from_tty) |
| 629 | char *args; |
| 630 | int from_tty; |
| 631 | { |
| 632 | char **argv; |
| 633 | char *name = NULL; |
| 634 | struct cleanup *cleanups; |
| 635 | int mapped = 0; |
| 636 | int readnow = 0; |
| 637 | |
| 638 | dont_repeat (); |
| 639 | |
| 640 | if (args == NULL) |
| 641 | { |
| 642 | if ((have_full_symbols () || have_partial_symbols ()) |
| 643 | && from_tty |
| 644 | && !query ("Discard symbol table from `%s'? ", |
| 645 | symfile_objfile -> name)) |
| 646 | error ("Not confirmed."); |
| 647 | free_all_objfiles (); |
| 648 | symfile_objfile = NULL; |
| 649 | if (from_tty) |
| 650 | { |
| 651 | printf_unfiltered ("No symbol file now.\n"); |
| 652 | } |
| 653 | } |
| 654 | else |
| 655 | { |
| 656 | if ((argv = buildargv (args)) == NULL) |
| 657 | { |
| 658 | nomem (0); |
| 659 | } |
| 660 | cleanups = make_cleanup (freeargv, (char *) argv); |
| 661 | while (*argv != NULL) |
| 662 | { |
| 663 | if (STREQ (*argv, "-mapped")) |
| 664 | { |
| 665 | mapped = 1; |
| 666 | } |
| 667 | else if (STREQ (*argv, "-readnow")) |
| 668 | { |
| 669 | readnow = 1; |
| 670 | } |
| 671 | else if (**argv == '-') |
| 672 | { |
| 673 | error ("unknown option `%s'", *argv); |
| 674 | } |
| 675 | else |
| 676 | { |
| 677 | name = *argv; |
| 678 | } |
| 679 | argv++; |
| 680 | } |
| 681 | |
| 682 | if (name == NULL) |
| 683 | { |
| 684 | error ("no symbol file name was specified"); |
| 685 | } |
| 686 | else |
| 687 | { |
| 688 | symbol_file_add (name, from_tty, (CORE_ADDR)0, 1, mapped, readnow); |
| 689 | set_initial_language (); |
| 690 | } |
| 691 | do_cleanups (cleanups); |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | /* Set the initial language. |
| 696 | |
| 697 | A better solution would be to record the language in the psymtab when reading |
| 698 | partial symbols, and then use it (if known) to set the language. This would |
| 699 | be a win for formats that encode the language in an easily discoverable place, |
| 700 | such as DWARF. For stabs, we can jump through hoops looking for specially |
| 701 | named symbols or try to intuit the language from the specific type of stabs |
| 702 | we find, but we can't do that until later when we read in full symbols. |
| 703 | FIXME. */ |
| 704 | |
| 705 | static void |
| 706 | set_initial_language () |
| 707 | { |
| 708 | struct partial_symtab *pst; |
| 709 | enum language lang = language_unknown; |
| 710 | |
| 711 | pst = find_main_psymtab (); |
| 712 | if (pst != NULL) |
| 713 | { |
| 714 | if (pst -> filename != NULL) |
| 715 | { |
| 716 | lang = deduce_language_from_filename (pst -> filename); |
| 717 | } |
| 718 | if (lang == language_unknown) |
| 719 | { |
| 720 | /* Make C the default language */ |
| 721 | lang = language_c; |
| 722 | } |
| 723 | set_language (lang); |
| 724 | expected_language = current_language; /* Don't warn the user */ |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | /* Open file specified by NAME and hand it off to BFD for preliminary |
| 729 | analysis. Result is a newly initialized bfd *, which includes a newly |
| 730 | malloc'd` copy of NAME (tilde-expanded and made absolute). |
| 731 | In case of trouble, error() is called. */ |
| 732 | |
| 733 | static bfd * |
| 734 | symfile_bfd_open (name) |
| 735 | char *name; |
| 736 | { |
| 737 | bfd *sym_bfd; |
| 738 | int desc; |
| 739 | char *absolute_name; |
| 740 | |
| 741 | name = tilde_expand (name); /* Returns 1st new malloc'd copy */ |
| 742 | |
| 743 | /* Look down path for it, allocate 2nd new malloc'd copy. */ |
| 744 | desc = openp (getenv ("PATH"), 1, name, O_RDONLY | O_BINARY, 0, &absolute_name); |
| 745 | if (desc < 0) |
| 746 | { |
| 747 | make_cleanup (free, name); |
| 748 | perror_with_name (name); |
| 749 | } |
| 750 | free (name); /* Free 1st new malloc'd copy */ |
| 751 | name = absolute_name; /* Keep 2nd malloc'd copy in bfd */ |
| 752 | /* It'll be freed in free_objfile(). */ |
| 753 | |
| 754 | sym_bfd = bfd_fdopenr (name, gnutarget, desc); |
| 755 | if (!sym_bfd) |
| 756 | { |
| 757 | close (desc); |
| 758 | make_cleanup (free, name); |
| 759 | error ("\"%s\": can't open to read symbols: %s.", name, |
| 760 | bfd_errmsg (bfd_error)); |
| 761 | } |
| 762 | sym_bfd->cacheable = true; |
| 763 | |
| 764 | if (!bfd_check_format (sym_bfd, bfd_object)) |
| 765 | { |
| 766 | bfd_close (sym_bfd); /* This also closes desc */ |
| 767 | make_cleanup (free, name); |
| 768 | error ("\"%s\": can't read symbols: %s.", name, |
| 769 | bfd_errmsg (bfd_error)); |
| 770 | } |
| 771 | |
| 772 | return (sym_bfd); |
| 773 | } |
| 774 | |
| 775 | /* Link a new symtab_fns into the global symtab_fns list. Called on gdb |
| 776 | startup by the _initialize routine in each object file format reader, |
| 777 | to register information about each format the the reader is prepared |
| 778 | to handle. */ |
| 779 | |
| 780 | void |
| 781 | add_symtab_fns (sf) |
| 782 | struct sym_fns *sf; |
| 783 | { |
| 784 | sf->next = symtab_fns; |
| 785 | symtab_fns = sf; |
| 786 | } |
| 787 | |
| 788 | |
| 789 | /* Initialize to read symbols from the symbol file sym_bfd. It either |
| 790 | returns or calls error(). The result is an initialized struct sym_fns |
| 791 | in the objfile structure, that contains cached information about the |
| 792 | symbol file. */ |
| 793 | |
| 794 | static void |
| 795 | find_sym_fns (objfile) |
| 796 | struct objfile *objfile; |
| 797 | { |
| 798 | struct sym_fns *sf; |
| 799 | enum bfd_flavour our_flavour = bfd_get_flavour (objfile -> obfd); |
| 800 | |
| 801 | /* Special kludge for RS/6000. See xcoffread.c. */ |
| 802 | if (STREQ (bfd_get_target (objfile -> obfd), "aixcoff-rs6000")) |
| 803 | our_flavour = (enum bfd_flavour)-1; |
| 804 | |
| 805 | for (sf = symtab_fns; sf != NULL; sf = sf -> next) |
| 806 | { |
| 807 | if (our_flavour == sf -> sym_flavour) |
| 808 | { |
| 809 | objfile -> sf = sf; |
| 810 | return; |
| 811 | } |
| 812 | } |
| 813 | error ("I'm sorry, Dave, I can't do that. Symbol format `%s' unknown.", |
| 814 | bfd_get_target (objfile -> obfd)); |
| 815 | } |
| 816 | \f |
| 817 | /* This function runs the load command of our current target. */ |
| 818 | |
| 819 | static void |
| 820 | load_command (arg, from_tty) |
| 821 | char *arg; |
| 822 | int from_tty; |
| 823 | { |
| 824 | target_load (arg, from_tty); |
| 825 | } |
| 826 | |
| 827 | /* This version of "load" should be usable for any target. Currently |
| 828 | it is just used for remote targets, not inftarg.c or core files, |
| 829 | on the theory that only in that case is it useful. |
| 830 | |
| 831 | Avoiding xmodem and the like seems like a win (a) because we don't have |
| 832 | to worry about finding it, and (b) On VMS, fork() is very slow and so |
| 833 | we don't want to run a subprocess. On the other hand, I'm not sure how |
| 834 | performance compares. */ |
| 835 | void |
| 836 | generic_load (filename, from_tty) |
| 837 | char *filename; |
| 838 | int from_tty; |
| 839 | { |
| 840 | struct cleanup *old_cleanups; |
| 841 | asection *s; |
| 842 | bfd *loadfile_bfd = bfd_openr (filename, gnutarget); |
| 843 | if (loadfile_bfd == NULL) |
| 844 | { |
| 845 | perror_with_name (filename); |
| 846 | return; |
| 847 | } |
| 848 | old_cleanups = make_cleanup (bfd_close, loadfile_bfd); |
| 849 | |
| 850 | if (!bfd_check_format (loadfile_bfd, bfd_object)) |
| 851 | { |
| 852 | error ("\"%s\" is not an object file: %s", filename, |
| 853 | bfd_errmsg (bfd_error)); |
| 854 | } |
| 855 | |
| 856 | for (s = loadfile_bfd->sections; s; s = s->next) |
| 857 | { |
| 858 | if (s->flags & SEC_LOAD) |
| 859 | { |
| 860 | bfd_size_type size; |
| 861 | |
| 862 | size = bfd_get_section_size_before_reloc (s); |
| 863 | if (size > 0) |
| 864 | { |
| 865 | char *buffer; |
| 866 | struct cleanup *old_chain; |
| 867 | bfd_vma vma; |
| 868 | |
| 869 | buffer = xmalloc (size); |
| 870 | old_chain = make_cleanup (free, buffer); |
| 871 | |
| 872 | vma = bfd_get_section_vma (loadfile_bfd, s); |
| 873 | |
| 874 | /* Is this really necessary? I guess it gives the user something |
| 875 | to look at during a long download. */ |
| 876 | printf_filtered ("Loading section %s, size 0x%lx vma 0x%lx\n", |
| 877 | bfd_get_section_name (loadfile_bfd, s), |
| 878 | (unsigned long) size, (unsigned long) vma); |
| 879 | |
| 880 | bfd_get_section_contents (loadfile_bfd, s, buffer, 0, size); |
| 881 | |
| 882 | target_write_memory (vma, buffer, size); |
| 883 | |
| 884 | do_cleanups (old_chain); |
| 885 | } |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | /* We were doing this in remote-mips.c, I suspect it is right |
| 890 | for other targets too. */ |
| 891 | write_pc (loadfile_bfd->start_address); |
| 892 | |
| 893 | /* FIXME: are we supposed to call symbol_file_add or not? According to |
| 894 | a comment from remote-mips.c (where a call to symbol_file_add was |
| 895 | commented out), making the call confuses GDB if more than one file is |
| 896 | loaded in. remote-nindy.c had no call to symbol_file_add, but remote-vx.c |
| 897 | does. */ |
| 898 | |
| 899 | do_cleanups (old_cleanups); |
| 900 | } |
| 901 | |
| 902 | /* This function allows the addition of incrementally linked object files. |
| 903 | It does not modify any state in the target, only in the debugger. */ |
| 904 | |
| 905 | /* ARGSUSED */ |
| 906 | static void |
| 907 | add_symbol_file_command (args, from_tty) |
| 908 | char *args; |
| 909 | int from_tty; |
| 910 | { |
| 911 | char *name = NULL; |
| 912 | CORE_ADDR text_addr; |
| 913 | char *arg; |
| 914 | int readnow = 0; |
| 915 | int mapped = 0; |
| 916 | |
| 917 | dont_repeat (); |
| 918 | |
| 919 | if (args == NULL) |
| 920 | { |
| 921 | error ("add-symbol-file takes a file name and an address"); |
| 922 | } |
| 923 | |
| 924 | /* Make a copy of the string that we can safely write into. */ |
| 925 | |
| 926 | args = strdup (args); |
| 927 | make_cleanup (free, args); |
| 928 | |
| 929 | /* Pick off any -option args and the file name. */ |
| 930 | |
| 931 | while ((*args != '\000') && (name == NULL)) |
| 932 | { |
| 933 | while (isspace (*args)) {args++;} |
| 934 | arg = args; |
| 935 | while ((*args != '\000') && !isspace (*args)) {args++;} |
| 936 | if (*args != '\000') |
| 937 | { |
| 938 | *args++ = '\000'; |
| 939 | } |
| 940 | if (*arg != '-') |
| 941 | { |
| 942 | name = arg; |
| 943 | } |
| 944 | else if (STREQ (arg, "-mapped")) |
| 945 | { |
| 946 | mapped = 1; |
| 947 | } |
| 948 | else if (STREQ (arg, "-readnow")) |
| 949 | { |
| 950 | readnow = 1; |
| 951 | } |
| 952 | else |
| 953 | { |
| 954 | error ("unknown option `%s'", arg); |
| 955 | } |
| 956 | } |
| 957 | |
| 958 | /* After picking off any options and the file name, args should be |
| 959 | left pointing at the remainder of the command line, which should |
| 960 | be the address expression to evaluate. */ |
| 961 | |
| 962 | if ((name == NULL) || (*args == '\000') ) |
| 963 | { |
| 964 | error ("add-symbol-file takes a file name and an address"); |
| 965 | } |
| 966 | name = tilde_expand (name); |
| 967 | make_cleanup (free, name); |
| 968 | |
| 969 | text_addr = parse_and_eval_address (args); |
| 970 | |
| 971 | if (!query ("add symbol table from file \"%s\" at text_addr = %s?\n", |
| 972 | name, local_hex_string ((unsigned long)text_addr))) |
| 973 | error ("Not confirmed."); |
| 974 | |
| 975 | symbol_file_add (name, 0, text_addr, 0, mapped, readnow); |
| 976 | } |
| 977 | \f |
| 978 | /* Re-read symbols if a symbol-file has changed. */ |
| 979 | void |
| 980 | reread_symbols () |
| 981 | { |
| 982 | struct objfile *objfile; |
| 983 | long new_modtime; |
| 984 | int reread_one = 0; |
| 985 | struct stat new_statbuf; |
| 986 | int res; |
| 987 | |
| 988 | /* With the addition of shared libraries, this should be modified, |
| 989 | the load time should be saved in the partial symbol tables, since |
| 990 | different tables may come from different source files. FIXME. |
| 991 | This routine should then walk down each partial symbol table |
| 992 | and see if the symbol table that it originates from has been changed */ |
| 993 | |
| 994 | for (objfile = object_files; objfile; objfile = objfile->next) { |
| 995 | if (objfile->obfd) { |
| 996 | #ifdef IBM6000_TARGET |
| 997 | /* If this object is from a shared library, then you should |
| 998 | stat on the library name, not member name. */ |
| 999 | |
| 1000 | if (objfile->obfd->my_archive) |
| 1001 | res = stat (objfile->obfd->my_archive->filename, &new_statbuf); |
| 1002 | else |
| 1003 | #endif |
| 1004 | res = stat (objfile->name, &new_statbuf); |
| 1005 | if (res != 0) { |
| 1006 | /* FIXME, should use print_sys_errmsg but it's not filtered. */ |
| 1007 | printf_filtered ("`%s' has disappeared; keeping its symbols.\n", |
| 1008 | objfile->name); |
| 1009 | continue; |
| 1010 | } |
| 1011 | new_modtime = new_statbuf.st_mtime; |
| 1012 | if (new_modtime != objfile->mtime) |
| 1013 | { |
| 1014 | struct cleanup *old_cleanups; |
| 1015 | struct section_offsets *offsets; |
| 1016 | int num_offsets; |
| 1017 | int section_offsets_size; |
| 1018 | |
| 1019 | printf_filtered ("`%s' has changed; re-reading symbols.\n", |
| 1020 | objfile->name); |
| 1021 | |
| 1022 | /* There are various functions like symbol_file_add, |
| 1023 | symfile_bfd_open, syms_from_objfile, etc., which might |
| 1024 | appear to do what we want. But they have various other |
| 1025 | effects which we *don't* want. So we just do stuff |
| 1026 | ourselves. We don't worry about mapped files (for one thing, |
| 1027 | any mapped file will be out of date). */ |
| 1028 | |
| 1029 | /* If we get an error, blow away this objfile (not sure if |
| 1030 | that is the correct response for things like shared |
| 1031 | libraries). */ |
| 1032 | old_cleanups = make_cleanup (free_objfile, objfile); |
| 1033 | /* We need to do this whenever any symbols go away. */ |
| 1034 | make_cleanup (clear_symtab_users, 0); |
| 1035 | |
| 1036 | /* Clean up any state BFD has sitting around. We don't need |
| 1037 | to close the descriptor but BFD lacks a way of closing the |
| 1038 | BFD without closing the descriptor. */ |
| 1039 | if (!bfd_close (objfile->obfd)) |
| 1040 | error ("Can't close BFD for %s.", objfile->name); |
| 1041 | objfile->obfd = bfd_openr (objfile->name, gnutarget); |
| 1042 | if (objfile->obfd == NULL) |
| 1043 | error ("Can't open %s to read symbols.", objfile->name); |
| 1044 | /* bfd_openr sets cacheable to true, which is what we want. */ |
| 1045 | if (!bfd_check_format (objfile->obfd, bfd_object)) |
| 1046 | error ("Can't read symbols from %s: %s.", objfile->name, |
| 1047 | bfd_errmsg (bfd_error)); |
| 1048 | |
| 1049 | /* Save the offsets, we will nuke them with the rest of the |
| 1050 | psymbol_obstack. */ |
| 1051 | num_offsets = objfile->num_sections; |
| 1052 | section_offsets_size = |
| 1053 | sizeof (struct section_offsets) |
| 1054 | + sizeof (objfile->section_offsets->offsets) * num_offsets; |
| 1055 | offsets = (struct section_offsets *) alloca (section_offsets_size); |
| 1056 | memcpy (offsets, objfile->section_offsets, section_offsets_size); |
| 1057 | |
| 1058 | /* Nuke all the state that we will re-read. Much of the following |
| 1059 | code which sets things to NULL really is necessary to tell |
| 1060 | other parts of GDB that there is nothing currently there. */ |
| 1061 | |
| 1062 | /* FIXME: Do we have to free a whole linked list, or is this |
| 1063 | enough? */ |
| 1064 | if (objfile->global_psymbols.list) |
| 1065 | mfree (objfile->md, objfile->global_psymbols.list); |
| 1066 | objfile->global_psymbols.list = NULL; |
| 1067 | objfile->global_psymbols.size = 0; |
| 1068 | if (objfile->static_psymbols.list) |
| 1069 | mfree (objfile->md, objfile->static_psymbols.list); |
| 1070 | objfile->static_psymbols.list = NULL; |
| 1071 | objfile->static_psymbols.size = 0; |
| 1072 | |
| 1073 | /* Free the obstacks for non-reusable objfiles */ |
| 1074 | obstack_free (&objfile -> psymbol_obstack, 0); |
| 1075 | obstack_free (&objfile -> symbol_obstack, 0); |
| 1076 | obstack_free (&objfile -> type_obstack, 0); |
| 1077 | objfile->sections = NULL; |
| 1078 | objfile->symtabs = NULL; |
| 1079 | objfile->psymtabs = NULL; |
| 1080 | objfile->free_psymtabs = NULL; |
| 1081 | objfile->msymbols = NULL; |
| 1082 | objfile->minimal_symbol_count= 0; |
| 1083 | objfile->fundamental_types = NULL; |
| 1084 | if (objfile -> sf != NULL) |
| 1085 | { |
| 1086 | (*objfile -> sf -> sym_finish) (objfile); |
| 1087 | } |
| 1088 | |
| 1089 | /* We never make this a mapped file. */ |
| 1090 | objfile -> md = NULL; |
| 1091 | /* obstack_specify_allocation also initializes the obstack so |
| 1092 | it is empty. */ |
| 1093 | obstack_specify_allocation (&objfile -> psymbol_obstack, 0, 0, |
| 1094 | xmalloc, free); |
| 1095 | obstack_specify_allocation (&objfile -> symbol_obstack, 0, 0, |
| 1096 | xmalloc, free); |
| 1097 | obstack_specify_allocation (&objfile -> type_obstack, 0, 0, |
| 1098 | xmalloc, free); |
| 1099 | if (build_objfile_section_table (objfile)) |
| 1100 | { |
| 1101 | error ("Can't find the file sections in `%s': %s", |
| 1102 | objfile -> name, bfd_errmsg (bfd_error)); |
| 1103 | } |
| 1104 | |
| 1105 | /* We use the same section offsets as from last time. I'm not |
| 1106 | sure whether that is always correct for shared libraries. */ |
| 1107 | objfile->section_offsets = (struct section_offsets *) |
| 1108 | obstack_alloc (&objfile -> psymbol_obstack, section_offsets_size); |
| 1109 | memcpy (objfile->section_offsets, offsets, section_offsets_size); |
| 1110 | objfile->num_sections = num_offsets; |
| 1111 | |
| 1112 | /* What the hell is sym_new_init for, anyway? The concept of |
| 1113 | distinguishing between the main file and additional files |
| 1114 | in this way seems rather dubious. */ |
| 1115 | if (objfile == symfile_objfile) |
| 1116 | (*objfile->sf->sym_new_init) (objfile); |
| 1117 | |
| 1118 | (*objfile->sf->sym_init) (objfile); |
| 1119 | clear_complaints (1, 1); |
| 1120 | /* The "mainline" parameter is a hideous hack; I think leaving it |
| 1121 | zero is OK since dbxread.c also does what it needs to do if |
| 1122 | objfile->global_psymbols.size is 0. */ |
| 1123 | (*objfile->sf->sym_read) (objfile, objfile->section_offsets, 0); |
| 1124 | objfile -> flags |= OBJF_SYMS; |
| 1125 | |
| 1126 | /* We're done reading the symbol file; finish off complaints. */ |
| 1127 | clear_complaints (0, 1); |
| 1128 | |
| 1129 | /* Getting new symbols may change our opinion about what is |
| 1130 | frameless. */ |
| 1131 | |
| 1132 | reinit_frame_cache (); |
| 1133 | |
| 1134 | /* Discard cleanups as symbol reading was successful. */ |
| 1135 | discard_cleanups (old_cleanups); |
| 1136 | |
| 1137 | /* If the mtime has changed between the time we set new_modtime |
| 1138 | and now, we *want* this to be out of date, so don't call stat |
| 1139 | again now. */ |
| 1140 | objfile->mtime = new_modtime; |
| 1141 | reread_one = 1; |
| 1142 | } |
| 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | if (reread_one) |
| 1147 | clear_symtab_users (); |
| 1148 | } |
| 1149 | |
| 1150 | \f |
| 1151 | enum language |
| 1152 | deduce_language_from_filename (filename) |
| 1153 | char *filename; |
| 1154 | { |
| 1155 | char *c; |
| 1156 | |
| 1157 | if (0 == filename) |
| 1158 | ; /* Get default */ |
| 1159 | else if (0 == (c = strrchr (filename, '.'))) |
| 1160 | ; /* Get default. */ |
| 1161 | else if (STREQ(c,".mod")) |
| 1162 | return language_m2; |
| 1163 | else if (STREQ(c,".c")) |
| 1164 | return language_c; |
| 1165 | else if (STREQ (c,".cc") || STREQ (c,".C") || STREQ (c, ".cxx")) |
| 1166 | return language_cplus; |
| 1167 | else if (STREQ (c,".ch") || STREQ (c,".c186") || STREQ (c,".c286")) |
| 1168 | return language_chill; |
| 1169 | |
| 1170 | return language_unknown; /* default */ |
| 1171 | } |
| 1172 | \f |
| 1173 | /* allocate_symtab: |
| 1174 | |
| 1175 | Allocate and partly initialize a new symbol table. Return a pointer |
| 1176 | to it. error() if no space. |
| 1177 | |
| 1178 | Caller must set these fields: |
| 1179 | LINETABLE(symtab) |
| 1180 | symtab->blockvector |
| 1181 | symtab->dirname |
| 1182 | symtab->free_code |
| 1183 | symtab->free_ptr |
| 1184 | initialize any EXTRA_SYMTAB_INFO |
| 1185 | possibly free_named_symtabs (symtab->filename); |
| 1186 | */ |
| 1187 | |
| 1188 | struct symtab * |
| 1189 | allocate_symtab (filename, objfile) |
| 1190 | char *filename; |
| 1191 | struct objfile *objfile; |
| 1192 | { |
| 1193 | register struct symtab *symtab; |
| 1194 | |
| 1195 | symtab = (struct symtab *) |
| 1196 | obstack_alloc (&objfile -> symbol_obstack, sizeof (struct symtab)); |
| 1197 | memset (symtab, 0, sizeof (*symtab)); |
| 1198 | symtab -> filename = obsavestring (filename, strlen (filename), |
| 1199 | &objfile -> symbol_obstack); |
| 1200 | symtab -> fullname = NULL; |
| 1201 | symtab -> language = deduce_language_from_filename (filename); |
| 1202 | |
| 1203 | /* Hook it to the objfile it comes from */ |
| 1204 | |
| 1205 | symtab -> objfile = objfile; |
| 1206 | symtab -> next = objfile -> symtabs; |
| 1207 | objfile -> symtabs = symtab; |
| 1208 | |
| 1209 | #ifdef INIT_EXTRA_SYMTAB_INFO |
| 1210 | INIT_EXTRA_SYMTAB_INFO (symtab); |
| 1211 | #endif |
| 1212 | |
| 1213 | return (symtab); |
| 1214 | } |
| 1215 | |
| 1216 | struct partial_symtab * |
| 1217 | allocate_psymtab (filename, objfile) |
| 1218 | char *filename; |
| 1219 | struct objfile *objfile; |
| 1220 | { |
| 1221 | struct partial_symtab *psymtab; |
| 1222 | |
| 1223 | if (objfile -> free_psymtabs) |
| 1224 | { |
| 1225 | psymtab = objfile -> free_psymtabs; |
| 1226 | objfile -> free_psymtabs = psymtab -> next; |
| 1227 | } |
| 1228 | else |
| 1229 | psymtab = (struct partial_symtab *) |
| 1230 | obstack_alloc (&objfile -> psymbol_obstack, |
| 1231 | sizeof (struct partial_symtab)); |
| 1232 | |
| 1233 | memset (psymtab, 0, sizeof (struct partial_symtab)); |
| 1234 | psymtab -> filename = obsavestring (filename, strlen (filename), |
| 1235 | &objfile -> psymbol_obstack); |
| 1236 | psymtab -> symtab = NULL; |
| 1237 | |
| 1238 | /* Hook it to the objfile it comes from */ |
| 1239 | |
| 1240 | psymtab -> objfile = objfile; |
| 1241 | psymtab -> next = objfile -> psymtabs; |
| 1242 | objfile -> psymtabs = psymtab; |
| 1243 | |
| 1244 | return (psymtab); |
| 1245 | } |
| 1246 | |
| 1247 | \f |
| 1248 | /* Reset all data structures in gdb which may contain references to symbol |
| 1249 | table date. */ |
| 1250 | |
| 1251 | void |
| 1252 | clear_symtab_users () |
| 1253 | { |
| 1254 | /* Someday, we should do better than this, by only blowing away |
| 1255 | the things that really need to be blown. */ |
| 1256 | clear_value_history (); |
| 1257 | clear_displays (); |
| 1258 | clear_internalvars (); |
| 1259 | breakpoint_re_set (); |
| 1260 | set_default_breakpoint (0, 0, 0, 0); |
| 1261 | current_source_symtab = 0; |
| 1262 | current_source_line = 0; |
| 1263 | clear_pc_function_cache (); |
| 1264 | } |
| 1265 | |
| 1266 | /* clear_symtab_users_once: |
| 1267 | |
| 1268 | This function is run after symbol reading, or from a cleanup. |
| 1269 | If an old symbol table was obsoleted, the old symbol table |
| 1270 | has been blown away, but the other GDB data structures that may |
| 1271 | reference it have not yet been cleared or re-directed. (The old |
| 1272 | symtab was zapped, and the cleanup queued, in free_named_symtab() |
| 1273 | below.) |
| 1274 | |
| 1275 | This function can be queued N times as a cleanup, or called |
| 1276 | directly; it will do all the work the first time, and then will be a |
| 1277 | no-op until the next time it is queued. This works by bumping a |
| 1278 | counter at queueing time. Much later when the cleanup is run, or at |
| 1279 | the end of symbol processing (in case the cleanup is discarded), if |
| 1280 | the queued count is greater than the "done-count", we do the work |
| 1281 | and set the done-count to the queued count. If the queued count is |
| 1282 | less than or equal to the done-count, we just ignore the call. This |
| 1283 | is needed because reading a single .o file will often replace many |
| 1284 | symtabs (one per .h file, for example), and we don't want to reset |
| 1285 | the breakpoints N times in the user's face. |
| 1286 | |
| 1287 | The reason we both queue a cleanup, and call it directly after symbol |
| 1288 | reading, is because the cleanup protects us in case of errors, but is |
| 1289 | discarded if symbol reading is successful. */ |
| 1290 | |
| 1291 | #if 0 |
| 1292 | /* FIXME: As free_named_symtabs is currently a big noop this function |
| 1293 | is no longer needed. */ |
| 1294 | static void |
| 1295 | clear_symtab_users_once PARAMS ((void)); |
| 1296 | |
| 1297 | static int clear_symtab_users_queued; |
| 1298 | static int clear_symtab_users_done; |
| 1299 | |
| 1300 | static void |
| 1301 | clear_symtab_users_once () |
| 1302 | { |
| 1303 | /* Enforce once-per-`do_cleanups'-semantics */ |
| 1304 | if (clear_symtab_users_queued <= clear_symtab_users_done) |
| 1305 | return; |
| 1306 | clear_symtab_users_done = clear_symtab_users_queued; |
| 1307 | |
| 1308 | clear_symtab_users (); |
| 1309 | } |
| 1310 | #endif |
| 1311 | |
| 1312 | /* Delete the specified psymtab, and any others that reference it. */ |
| 1313 | |
| 1314 | static void |
| 1315 | cashier_psymtab (pst) |
| 1316 | struct partial_symtab *pst; |
| 1317 | { |
| 1318 | struct partial_symtab *ps, *pprev = NULL; |
| 1319 | int i; |
| 1320 | |
| 1321 | /* Find its previous psymtab in the chain */ |
| 1322 | for (ps = pst->objfile->psymtabs; ps; ps = ps->next) { |
| 1323 | if (ps == pst) |
| 1324 | break; |
| 1325 | pprev = ps; |
| 1326 | } |
| 1327 | |
| 1328 | if (ps) { |
| 1329 | /* Unhook it from the chain. */ |
| 1330 | if (ps == pst->objfile->psymtabs) |
| 1331 | pst->objfile->psymtabs = ps->next; |
| 1332 | else |
| 1333 | pprev->next = ps->next; |
| 1334 | |
| 1335 | /* FIXME, we can't conveniently deallocate the entries in the |
| 1336 | partial_symbol lists (global_psymbols/static_psymbols) that |
| 1337 | this psymtab points to. These just take up space until all |
| 1338 | the psymtabs are reclaimed. Ditto the dependencies list and |
| 1339 | filename, which are all in the psymbol_obstack. */ |
| 1340 | |
| 1341 | /* We need to cashier any psymtab that has this one as a dependency... */ |
| 1342 | again: |
| 1343 | for (ps = pst->objfile->psymtabs; ps; ps = ps->next) { |
| 1344 | for (i = 0; i < ps->number_of_dependencies; i++) { |
| 1345 | if (ps->dependencies[i] == pst) { |
| 1346 | cashier_psymtab (ps); |
| 1347 | goto again; /* Must restart, chain has been munged. */ |
| 1348 | } |
| 1349 | } |
| 1350 | } |
| 1351 | } |
| 1352 | } |
| 1353 | |
| 1354 | /* If a symtab or psymtab for filename NAME is found, free it along |
| 1355 | with any dependent breakpoints, displays, etc. |
| 1356 | Used when loading new versions of object modules with the "add-file" |
| 1357 | command. This is only called on the top-level symtab or psymtab's name; |
| 1358 | it is not called for subsidiary files such as .h files. |
| 1359 | |
| 1360 | Return value is 1 if we blew away the environment, 0 if not. |
| 1361 | FIXME. The return valu appears to never be used. |
| 1362 | |
| 1363 | FIXME. I think this is not the best way to do this. We should |
| 1364 | work on being gentler to the environment while still cleaning up |
| 1365 | all stray pointers into the freed symtab. */ |
| 1366 | |
| 1367 | int |
| 1368 | free_named_symtabs (name) |
| 1369 | char *name; |
| 1370 | { |
| 1371 | #if 0 |
| 1372 | /* FIXME: With the new method of each objfile having it's own |
| 1373 | psymtab list, this function needs serious rethinking. In particular, |
| 1374 | why was it ever necessary to toss psymtabs with specific compilation |
| 1375 | unit filenames, as opposed to all psymtabs from a particular symbol |
| 1376 | file? -- fnf |
| 1377 | Well, the answer is that some systems permit reloading of particular |
| 1378 | compilation units. We want to blow away any old info about these |
| 1379 | compilation units, regardless of which objfiles they arrived in. --gnu. */ |
| 1380 | |
| 1381 | register struct symtab *s; |
| 1382 | register struct symtab *prev; |
| 1383 | register struct partial_symtab *ps; |
| 1384 | struct blockvector *bv; |
| 1385 | int blewit = 0; |
| 1386 | |
| 1387 | /* We only wack things if the symbol-reload switch is set. */ |
| 1388 | if (!symbol_reloading) |
| 1389 | return 0; |
| 1390 | |
| 1391 | /* Some symbol formats have trouble providing file names... */ |
| 1392 | if (name == 0 || *name == '\0') |
| 1393 | return 0; |
| 1394 | |
| 1395 | /* Look for a psymtab with the specified name. */ |
| 1396 | |
| 1397 | again2: |
| 1398 | for (ps = partial_symtab_list; ps; ps = ps->next) { |
| 1399 | if (STREQ (name, ps->filename)) { |
| 1400 | cashier_psymtab (ps); /* Blow it away...and its little dog, too. */ |
| 1401 | goto again2; /* Must restart, chain has been munged */ |
| 1402 | } |
| 1403 | } |
| 1404 | |
| 1405 | /* Look for a symtab with the specified name. */ |
| 1406 | |
| 1407 | for (s = symtab_list; s; s = s->next) |
| 1408 | { |
| 1409 | if (STREQ (name, s->filename)) |
| 1410 | break; |
| 1411 | prev = s; |
| 1412 | } |
| 1413 | |
| 1414 | if (s) |
| 1415 | { |
| 1416 | if (s == symtab_list) |
| 1417 | symtab_list = s->next; |
| 1418 | else |
| 1419 | prev->next = s->next; |
| 1420 | |
| 1421 | /* For now, queue a delete for all breakpoints, displays, etc., whether |
| 1422 | or not they depend on the symtab being freed. This should be |
| 1423 | changed so that only those data structures affected are deleted. */ |
| 1424 | |
| 1425 | /* But don't delete anything if the symtab is empty. |
| 1426 | This test is necessary due to a bug in "dbxread.c" that |
| 1427 | causes empty symtabs to be created for N_SO symbols that |
| 1428 | contain the pathname of the object file. (This problem |
| 1429 | has been fixed in GDB 3.9x). */ |
| 1430 | |
| 1431 | bv = BLOCKVECTOR (s); |
| 1432 | if (BLOCKVECTOR_NBLOCKS (bv) > 2 |
| 1433 | || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK)) |
| 1434 | || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK))) |
| 1435 | { |
| 1436 | complain (&oldsyms_complaint, name); |
| 1437 | |
| 1438 | clear_symtab_users_queued++; |
| 1439 | make_cleanup (clear_symtab_users_once, 0); |
| 1440 | blewit = 1; |
| 1441 | } else { |
| 1442 | complain (&empty_symtab_complaint, name); |
| 1443 | } |
| 1444 | |
| 1445 | free_symtab (s); |
| 1446 | } |
| 1447 | else |
| 1448 | { |
| 1449 | /* It is still possible that some breakpoints will be affected |
| 1450 | even though no symtab was found, since the file might have |
| 1451 | been compiled without debugging, and hence not be associated |
| 1452 | with a symtab. In order to handle this correctly, we would need |
| 1453 | to keep a list of text address ranges for undebuggable files. |
| 1454 | For now, we do nothing, since this is a fairly obscure case. */ |
| 1455 | ; |
| 1456 | } |
| 1457 | |
| 1458 | /* FIXME, what about the minimal symbol table? */ |
| 1459 | return blewit; |
| 1460 | #else |
| 1461 | return (0); |
| 1462 | #endif |
| 1463 | } |
| 1464 | \f |
| 1465 | /* Allocate and partially fill a partial symtab. It will be |
| 1466 | completely filled at the end of the symbol list. |
| 1467 | |
| 1468 | SYMFILE_NAME is the name of the symbol-file we are reading from, and ADDR |
| 1469 | is the address relative to which its symbols are (incremental) or 0 |
| 1470 | (normal). */ |
| 1471 | |
| 1472 | |
| 1473 | struct partial_symtab * |
| 1474 | start_psymtab_common (objfile, section_offsets, |
| 1475 | filename, textlow, global_syms, static_syms) |
| 1476 | struct objfile *objfile; |
| 1477 | struct section_offsets *section_offsets; |
| 1478 | char *filename; |
| 1479 | CORE_ADDR textlow; |
| 1480 | struct partial_symbol *global_syms; |
| 1481 | struct partial_symbol *static_syms; |
| 1482 | { |
| 1483 | struct partial_symtab *psymtab; |
| 1484 | |
| 1485 | psymtab = allocate_psymtab (filename, objfile); |
| 1486 | psymtab -> section_offsets = section_offsets; |
| 1487 | psymtab -> textlow = textlow; |
| 1488 | psymtab -> texthigh = psymtab -> textlow; /* default */ |
| 1489 | psymtab -> globals_offset = global_syms - objfile -> global_psymbols.list; |
| 1490 | psymtab -> statics_offset = static_syms - objfile -> static_psymbols.list; |
| 1491 | return (psymtab); |
| 1492 | } |
| 1493 | \f |
| 1494 | /* Debugging versions of functions that are usually inline macros |
| 1495 | (see symfile.h). */ |
| 1496 | |
| 1497 | #if !INLINE_ADD_PSYMBOL |
| 1498 | |
| 1499 | /* Add a symbol with a long value to a psymtab. |
| 1500 | Since one arg is a struct, we pass in a ptr and deref it (sigh). */ |
| 1501 | |
| 1502 | void |
| 1503 | add_psymbol_to_list (name, namelength, namespace, class, list, val, language, |
| 1504 | objfile) |
| 1505 | char *name; |
| 1506 | int namelength; |
| 1507 | enum namespace namespace; |
| 1508 | enum address_class class; |
| 1509 | struct psymbol_allocation_list *list; |
| 1510 | long val; |
| 1511 | enum language language; |
| 1512 | struct objfile *objfile; |
| 1513 | { |
| 1514 | register struct partial_symbol *psym; |
| 1515 | register char *demangled_name; |
| 1516 | |
| 1517 | if (list->next >= list->list + list->size) |
| 1518 | { |
| 1519 | extend_psymbol_list (list,objfile); |
| 1520 | } |
| 1521 | psym = list->next++; |
| 1522 | |
| 1523 | SYMBOL_NAME (psym) = |
| 1524 | (char *) obstack_alloc (&objfile->psymbol_obstack, namelength + 1); |
| 1525 | memcpy (SYMBOL_NAME (psym), name, namelength); |
| 1526 | SYMBOL_NAME (psym)[namelength] = '\0'; |
| 1527 | SYMBOL_VALUE (psym) = val; |
| 1528 | SYMBOL_LANGUAGE (psym) = language; |
| 1529 | PSYMBOL_NAMESPACE (psym) = namespace; |
| 1530 | PSYMBOL_CLASS (psym) = class; |
| 1531 | SYMBOL_INIT_DEMANGLED_NAME (psym, &objfile->psymbol_obstack); |
| 1532 | } |
| 1533 | |
| 1534 | /* Add a symbol with a CORE_ADDR value to a psymtab. */ |
| 1535 | |
| 1536 | void |
| 1537 | add_psymbol_addr_to_list (name, namelength, namespace, class, list, val, |
| 1538 | language, objfile) |
| 1539 | char *name; |
| 1540 | int namelength; |
| 1541 | enum namespace namespace; |
| 1542 | enum address_class class; |
| 1543 | struct psymbol_allocation_list *list; |
| 1544 | CORE_ADDR val; |
| 1545 | enum language language; |
| 1546 | struct objfile *objfile; |
| 1547 | { |
| 1548 | register struct partial_symbol *psym; |
| 1549 | register char *demangled_name; |
| 1550 | |
| 1551 | if (list->next >= list->list + list->size) |
| 1552 | { |
| 1553 | extend_psymbol_list (list,objfile); |
| 1554 | } |
| 1555 | psym = list->next++; |
| 1556 | |
| 1557 | SYMBOL_NAME (psym) = |
| 1558 | (char *) obstack_alloc (&objfile->psymbol_obstack, namelength + 1); |
| 1559 | memcpy (SYMBOL_NAME (psym), name, namelength); |
| 1560 | SYMBOL_NAME (psym)[namelength] = '\0'; |
| 1561 | SYMBOL_VALUE_ADDRESS (psym) = val; |
| 1562 | SYMBOL_LANGUAGE (psym) = language; |
| 1563 | PSYMBOL_NAMESPACE (psym) = namespace; |
| 1564 | PSYMBOL_CLASS (psym) = class; |
| 1565 | SYMBOL_INIT_DEMANGLED_NAME (psym, &objfile->psymbol_obstack); |
| 1566 | } |
| 1567 | |
| 1568 | #endif /* !INLINE_ADD_PSYMBOL */ |
| 1569 | |
| 1570 | \f |
| 1571 | void |
| 1572 | _initialize_symfile () |
| 1573 | { |
| 1574 | struct cmd_list_element *c; |
| 1575 | |
| 1576 | c = add_cmd ("symbol-file", class_files, symbol_file_command, |
| 1577 | "Load symbol table from executable file FILE.\n\ |
| 1578 | The `file' command can also load symbol tables, as well as setting the file\n\ |
| 1579 | to execute.", &cmdlist); |
| 1580 | c->completer = filename_completer; |
| 1581 | |
| 1582 | c = add_cmd ("add-symbol-file", class_files, add_symbol_file_command, |
| 1583 | "Load the symbols from FILE, assuming FILE has been dynamically loaded.\n\ |
| 1584 | The second argument provides the starting address of the file's text.", |
| 1585 | &cmdlist); |
| 1586 | c->completer = filename_completer; |
| 1587 | |
| 1588 | c = add_cmd ("load", class_files, load_command, |
| 1589 | "Dynamically load FILE into the running program, and record its symbols\n\ |
| 1590 | for access from GDB.", &cmdlist); |
| 1591 | c->completer = filename_completer; |
| 1592 | |
| 1593 | add_show_from_set |
| 1594 | (add_set_cmd ("symbol-reloading", class_support, var_boolean, |
| 1595 | (char *)&symbol_reloading, |
| 1596 | "Set dynamic symbol table reloading multiple times in one run.", |
| 1597 | &setlist), |
| 1598 | &showlist); |
| 1599 | |
| 1600 | } |