| 1 | /* Read HP PA/Risc object files for GDB. |
| 2 | Copyright 1991, 1992 Free Software Foundation, Inc. |
| 3 | Written by Fred Fish at Cygnus Support. |
| 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 "bfd.h" |
| 23 | #include <time.h> /* For time_t in libbfd.h. */ |
| 24 | #include <sys/types.h> /* For time_t, if not in time.h. */ |
| 25 | #include "libbfd.h" |
| 26 | #include "som.h" |
| 27 | #include <syms.h> |
| 28 | #include "symtab.h" |
| 29 | #include "symfile.h" |
| 30 | #include "objfiles.h" |
| 31 | #include "buildsym.h" |
| 32 | #include "stabsread.h" |
| 33 | #include "gdb-stabs.h" |
| 34 | #include "complaints.h" |
| 35 | #include <string.h> |
| 36 | #include "demangle.h" |
| 37 | #include <sys/file.h> |
| 38 | |
| 39 | /* Size of n_value and n_strx fields in a stab symbol. */ |
| 40 | #define BYTES_IN_WORD 4 |
| 41 | |
| 42 | #include "aout/aout64.h" |
| 43 | |
| 44 | /* Various things we might complain about... */ |
| 45 | |
| 46 | static void |
| 47 | pa_symfile_init PARAMS ((struct objfile *)); |
| 48 | |
| 49 | static void |
| 50 | pa_new_init PARAMS ((struct objfile *)); |
| 51 | |
| 52 | static void |
| 53 | read_unwind_info PARAMS ((struct objfile *)); |
| 54 | |
| 55 | static void |
| 56 | pa_symfile_read PARAMS ((struct objfile *, struct section_offsets *, int)); |
| 57 | |
| 58 | static void |
| 59 | pa_symfile_finish PARAMS ((struct objfile *)); |
| 60 | |
| 61 | static void |
| 62 | pa_symtab_read PARAMS ((bfd *, CORE_ADDR, struct objfile *)); |
| 63 | |
| 64 | static void |
| 65 | free_painfo PARAMS ((PTR)); |
| 66 | |
| 67 | static struct section_offsets * |
| 68 | pa_symfile_offsets PARAMS ((struct objfile *, CORE_ADDR)); |
| 69 | |
| 70 | static void |
| 71 | record_minimal_symbol PARAMS ((char *, CORE_ADDR, |
| 72 | enum minimal_symbol_type, |
| 73 | struct objfile *)); |
| 74 | |
| 75 | static void |
| 76 | record_minimal_symbol (name, address, ms_type, objfile) |
| 77 | char *name; |
| 78 | CORE_ADDR address; |
| 79 | enum minimal_symbol_type ms_type; |
| 80 | struct objfile *objfile; |
| 81 | { |
| 82 | name = obsavestring (name, strlen (name), &objfile -> symbol_obstack); |
| 83 | prim_record_minimal_symbol (name, address, ms_type); |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | |
| 88 | LOCAL FUNCTION |
| 89 | |
| 90 | pa_symtab_read -- read the symbol table of a PA file |
| 91 | |
| 92 | SYNOPSIS |
| 93 | |
| 94 | void pa_symtab_read (bfd *abfd, CORE_ADDR addr, |
| 95 | struct objfile *objfile) |
| 96 | |
| 97 | DESCRIPTION |
| 98 | |
| 99 | Given an open bfd, a base address to relocate symbols to, and a |
| 100 | flag that specifies whether or not this bfd is for an executable |
| 101 | or not (may be shared library for example), add all the global |
| 102 | function and data symbols to the minimal symbol table. |
| 103 | */ |
| 104 | |
| 105 | static void |
| 106 | pa_symtab_read (abfd, addr, objfile) |
| 107 | bfd *abfd; |
| 108 | CORE_ADDR addr; |
| 109 | struct objfile *objfile; |
| 110 | { |
| 111 | unsigned int number_of_symbols; |
| 112 | unsigned int i; |
| 113 | int val; |
| 114 | char *stringtab; |
| 115 | struct symbol_dictionary_record *buf, *bufp, *endbufp; |
| 116 | char *symname; |
| 117 | CONST int symsize = sizeof (struct symbol_dictionary_record); |
| 118 | |
| 119 | number_of_symbols = bfd_get_symcount (abfd); |
| 120 | |
| 121 | buf = alloca (symsize * number_of_symbols); |
| 122 | bfd_seek (abfd, obj_sym_filepos (abfd), L_SET); |
| 123 | val = bfd_read (buf, symsize * number_of_symbols, 1, abfd); |
| 124 | if (val != symsize * number_of_symbols) |
| 125 | error ("Couldn't read symbol dictionary!"); |
| 126 | |
| 127 | stringtab = alloca (obj_stringtab_size (abfd)); |
| 128 | bfd_seek (abfd, obj_str_filepos (abfd), L_SET); |
| 129 | val = bfd_read (stringtab, obj_stringtab_size (abfd), 1, abfd); |
| 130 | if (val != obj_stringtab_size (abfd)) |
| 131 | error ("Can't read in HP string table."); |
| 132 | |
| 133 | endbufp = buf + number_of_symbols; |
| 134 | for (bufp = buf; bufp < endbufp; ++bufp) |
| 135 | { |
| 136 | enum minimal_symbol_type ms_type; |
| 137 | |
| 138 | QUIT; |
| 139 | |
| 140 | switch (bufp->symbol_scope) |
| 141 | { |
| 142 | case SS_UNIVERSAL: |
| 143 | switch (bufp->symbol_type) |
| 144 | { |
| 145 | case ST_SYM_EXT: |
| 146 | case ST_ARG_EXT: |
| 147 | continue; |
| 148 | |
| 149 | case ST_CODE: |
| 150 | case ST_PRI_PROG: |
| 151 | case ST_SEC_PROG: |
| 152 | case ST_ENTRY: |
| 153 | case ST_MILLICODE: |
| 154 | symname = bufp->name.n_strx + stringtab; |
| 155 | ms_type = mst_text; |
| 156 | bufp->symbol_value &= ~0x3; /* clear out permission bits */ |
| 157 | break; |
| 158 | case ST_DATA: |
| 159 | symname = bufp->name.n_strx + stringtab; |
| 160 | ms_type = mst_data; |
| 161 | break; |
| 162 | default: |
| 163 | continue; |
| 164 | } |
| 165 | break; |
| 166 | |
| 167 | #if 0 |
| 168 | /* SS_GLOBAL and SS_LOCAL are two names for the same thing (!). */ |
| 169 | case SS_GLOBAL: |
| 170 | #endif |
| 171 | case SS_LOCAL: |
| 172 | switch (bufp->symbol_type) |
| 173 | { |
| 174 | case ST_SYM_EXT: |
| 175 | case ST_ARG_EXT: |
| 176 | continue; |
| 177 | |
| 178 | case ST_CODE: |
| 179 | symname = bufp->name.n_strx + stringtab; |
| 180 | ms_type = mst_file_text; |
| 181 | bufp->symbol_value &= ~0x3; /* clear out permission bits */ |
| 182 | |
| 183 | check_strange_names: |
| 184 | /* GAS leaves symbols with the prefixes "LS$", "LBB$", |
| 185 | and "LBE$" in .o files after assembling. And thus |
| 186 | they appear in the final executable. This can |
| 187 | cause problems if these special symbols have the |
| 188 | same value as real symbols. So ignore them. Also "LC$". */ |
| 189 | if (*symname == 'L' |
| 190 | && (symname[2] == '$' || symname[3] == '$')) |
| 191 | continue; |
| 192 | break; |
| 193 | |
| 194 | case ST_PRI_PROG: |
| 195 | case ST_SEC_PROG: |
| 196 | case ST_ENTRY: |
| 197 | case ST_MILLICODE: |
| 198 | symname = bufp->name.n_strx + stringtab; |
| 199 | ms_type = mst_file_text; |
| 200 | bufp->symbol_value &= ~0x3; /* clear out permission bits */ |
| 201 | break; |
| 202 | |
| 203 | case ST_DATA: |
| 204 | symname = bufp->name.n_strx + stringtab; |
| 205 | ms_type = mst_file_data; |
| 206 | goto check_strange_names; |
| 207 | |
| 208 | default: |
| 209 | continue; |
| 210 | } |
| 211 | break; |
| 212 | |
| 213 | default: |
| 214 | continue; |
| 215 | } |
| 216 | |
| 217 | if (bufp->name.n_strx > obj_stringtab_size (abfd)) |
| 218 | error ("Invalid symbol data; bad HP string table offset: %d", |
| 219 | bufp->name.n_strx); |
| 220 | |
| 221 | record_minimal_symbol (symname, |
| 222 | bufp->symbol_value, ms_type, |
| 223 | objfile); |
| 224 | } |
| 225 | |
| 226 | install_minimal_symbols (objfile); |
| 227 | } |
| 228 | |
| 229 | /* Read in the backtrace information stored in the `$UNWIND_START$' section of |
| 230 | the object file. This info is used mainly by find_unwind_entry() to find |
| 231 | out the stack frame size and frame pointer used by procedures. We put |
| 232 | everything on the psymbol obstack in the objfile so that it automatically |
| 233 | gets freed when the objfile is destroyed. */ |
| 234 | |
| 235 | static void |
| 236 | read_unwind_info (objfile) |
| 237 | struct objfile *objfile; |
| 238 | { |
| 239 | asection *unwind_sec; |
| 240 | struct obj_unwind_info *ui; |
| 241 | |
| 242 | ui = obstack_alloc (&objfile->psymbol_obstack, |
| 243 | sizeof (struct obj_unwind_info)); |
| 244 | |
| 245 | ui->table = NULL; |
| 246 | ui->cache = NULL; |
| 247 | ui->last = -1; |
| 248 | |
| 249 | unwind_sec = bfd_get_section_by_name (objfile->obfd, |
| 250 | "$UNWIND_START$"); |
| 251 | if (unwind_sec) |
| 252 | { |
| 253 | int size; |
| 254 | int i, *ip; |
| 255 | |
| 256 | size = bfd_section_size (objfile->obfd, unwind_sec); |
| 257 | ui->table = obstack_alloc (&objfile->psymbol_obstack, size); |
| 258 | ui->last = size / sizeof (struct unwind_table_entry) - 1; |
| 259 | |
| 260 | bfd_get_section_contents (objfile->obfd, unwind_sec, ui->table, |
| 261 | 0, size); |
| 262 | |
| 263 | OBJ_UNWIND_INFO (objfile) = ui; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | /* Scan and build partial symbols for a symbol file. |
| 268 | We have been initialized by a call to pa_symfile_init, which |
| 269 | currently does nothing. |
| 270 | |
| 271 | SECTION_OFFSETS is a set of offsets to apply to relocate the symbols |
| 272 | in each section. This is ignored, as it isn't needed for the PA. |
| 273 | |
| 274 | MAINLINE is true if we are reading the main symbol |
| 275 | table (as opposed to a shared lib or dynamically loaded file). |
| 276 | |
| 277 | This function only does the minimum work necessary for letting the |
| 278 | user "name" things symbolically; it does not read the entire symtab. |
| 279 | Instead, it reads the external and static symbols and puts them in partial |
| 280 | symbol tables. When more extensive information is requested of a |
| 281 | file, the corresponding partial symbol table is mutated into a full |
| 282 | fledged symbol table by going back and reading the symbols |
| 283 | for real. |
| 284 | |
| 285 | We look for sections with specific names, to tell us what debug |
| 286 | format to look for: FIXME!!! |
| 287 | |
| 288 | pastab_build_psymtabs() handles STABS symbols. |
| 289 | |
| 290 | Note that PA files have a "minimal" symbol table, which is vaguely |
| 291 | reminiscent of a COFF symbol table, but has only the minimal information |
| 292 | necessary for linking. We process this also, and use the information to |
| 293 | build gdb's minimal symbol table. This gives us some minimal debugging |
| 294 | capability even for files compiled without -g. */ |
| 295 | |
| 296 | static void |
| 297 | pa_symfile_read (objfile, section_offsets, mainline) |
| 298 | struct objfile *objfile; |
| 299 | struct section_offsets *section_offsets; |
| 300 | int mainline; |
| 301 | { |
| 302 | bfd *abfd = objfile->obfd; |
| 303 | struct cleanup *back_to; |
| 304 | CORE_ADDR offset; |
| 305 | |
| 306 | init_minimal_symbol_collection (); |
| 307 | back_to = make_cleanup (discard_minimal_symbols, 0); |
| 308 | |
| 309 | make_cleanup (free_painfo, (PTR) objfile); |
| 310 | |
| 311 | /* Process the normal PA symbol table first. */ |
| 312 | |
| 313 | /* FIXME, should take a section_offsets param, not just an offset. */ |
| 314 | |
| 315 | offset = ANOFFSET (section_offsets, 0); |
| 316 | pa_symtab_read (abfd, offset, objfile); |
| 317 | |
| 318 | /* Now process debugging information, which is contained in |
| 319 | special PA sections. */ |
| 320 | |
| 321 | pastab_build_psymtabs (objfile, section_offsets, mainline); |
| 322 | |
| 323 | read_unwind_info(objfile); |
| 324 | |
| 325 | do_cleanups (back_to); |
| 326 | } |
| 327 | |
| 328 | /* This cleans up the objfile's sym_stab_info pointer, and the chain of |
| 329 | stab_section_info's, that might be dangling from it. */ |
| 330 | |
| 331 | static void |
| 332 | free_painfo (objp) |
| 333 | PTR objp; |
| 334 | { |
| 335 | struct objfile *objfile = (struct objfile *)objp; |
| 336 | struct dbx_symfile_info *dbxinfo = (struct dbx_symfile_info *) |
| 337 | objfile->sym_stab_info; |
| 338 | struct stab_section_info *ssi, *nssi; |
| 339 | |
| 340 | ssi = dbxinfo->stab_section_info; |
| 341 | while (ssi) |
| 342 | { |
| 343 | nssi = ssi->next; |
| 344 | mfree (objfile->md, ssi); |
| 345 | ssi = nssi; |
| 346 | } |
| 347 | |
| 348 | dbxinfo->stab_section_info = 0; /* Just say No mo info about this. */ |
| 349 | } |
| 350 | |
| 351 | /* Initialize anything that needs initializing when a completely new symbol |
| 352 | file is specified (not just adding some symbols from another file, e.g. a |
| 353 | shared library). |
| 354 | |
| 355 | We reinitialize buildsym, since we may be reading stabs from a PA file. */ |
| 356 | |
| 357 | static void |
| 358 | pa_new_init (ignore) |
| 359 | struct objfile *ignore; |
| 360 | { |
| 361 | stabsread_new_init (); |
| 362 | buildsym_new_init (); |
| 363 | } |
| 364 | |
| 365 | /* Perform any local cleanups required when we are done with a particular |
| 366 | objfile. I.E, we are in the process of discarding all symbol information |
| 367 | for an objfile, freeing up all memory held for it, and unlinking the |
| 368 | objfile struct from the global list of known objfiles. */ |
| 369 | |
| 370 | static void |
| 371 | pa_symfile_finish (objfile) |
| 372 | struct objfile *objfile; |
| 373 | { |
| 374 | if (objfile -> sym_stab_info != NULL) |
| 375 | { |
| 376 | mfree (objfile -> md, objfile -> sym_stab_info); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | /* PA specific initialization routine for reading symbols. |
| 381 | |
| 382 | It is passed a pointer to a struct sym_fns which contains, among other |
| 383 | things, the BFD for the file whose symbols are being read, and a slot for |
| 384 | a pointer to "private data" which we can fill with goodies. |
| 385 | |
| 386 | This routine is almost a complete ripoff of dbx_symfile_init. The |
| 387 | common parts of these routines should be extracted and used instead of |
| 388 | duplicating this code. FIXME. */ |
| 389 | |
| 390 | static void |
| 391 | pa_symfile_init (objfile) |
| 392 | struct objfile *objfile; |
| 393 | { |
| 394 | int val; |
| 395 | bfd *sym_bfd = objfile->obfd; |
| 396 | char *name = bfd_get_filename (sym_bfd); |
| 397 | asection *stabsect; /* Section containing symbol table entries */ |
| 398 | asection *stringsect; /* Section containing symbol name strings */ |
| 399 | |
| 400 | stabsect = bfd_get_section_by_name (sym_bfd, "$GDB_SYMBOLS$"); |
| 401 | stringsect = bfd_get_section_by_name (sym_bfd, "$GDB_STRINGS$"); |
| 402 | |
| 403 | /* Allocate struct to keep track of the symfile */ |
| 404 | objfile->sym_stab_info = (PTR) |
| 405 | xmmalloc (objfile -> md, sizeof (struct dbx_symfile_info)); |
| 406 | |
| 407 | memset ((PTR) objfile->sym_stab_info, 0, sizeof (struct dbx_symfile_info)); |
| 408 | |
| 409 | if (!stabsect) |
| 410 | return; |
| 411 | |
| 412 | if (!stringsect) |
| 413 | error ("Found stabs, but not string section"); |
| 414 | |
| 415 | /* FIXME POKING INSIDE BFD DATA STRUCTURES */ |
| 416 | #define STRING_TABLE_OFFSET (stringsect->filepos) |
| 417 | #define SYMBOL_TABLE_OFFSET (stabsect->filepos) |
| 418 | |
| 419 | /* FIXME POKING INSIDE BFD DATA STRUCTURES */ |
| 420 | |
| 421 | DBX_SYMFILE_INFO (objfile)->stab_section_info = NULL; |
| 422 | DBX_TEXT_SECT (objfile) = bfd_get_section_by_name (sym_bfd, ".text"); |
| 423 | if (!DBX_TEXT_SECT (objfile)) |
| 424 | error ("Can't find .text section in symbol file"); |
| 425 | |
| 426 | /* FIXME: I suspect this should be external_nlist. The size of host |
| 427 | types like long and bfd_vma should not affect how we read the |
| 428 | file. */ |
| 429 | DBX_SYMBOL_SIZE (objfile) = sizeof (struct internal_nlist); |
| 430 | DBX_SYMCOUNT (objfile) = bfd_section_size (sym_bfd, stabsect) |
| 431 | / DBX_SYMBOL_SIZE (objfile); |
| 432 | DBX_SYMTAB_OFFSET (objfile) = SYMBOL_TABLE_OFFSET; |
| 433 | |
| 434 | /* Read the string table and stash it away in the psymbol_obstack. It is |
| 435 | only needed as long as we need to expand psymbols into full symbols, |
| 436 | so when we blow away the psymbol the string table goes away as well. |
| 437 | Note that gdb used to use the results of attempting to malloc the |
| 438 | string table, based on the size it read, as a form of sanity check |
| 439 | for botched byte swapping, on the theory that a byte swapped string |
| 440 | table size would be so totally bogus that the malloc would fail. Now |
| 441 | that we put in on the psymbol_obstack, we can't do this since gdb gets |
| 442 | a fatal error (out of virtual memory) if the size is bogus. We can |
| 443 | however at least check to see if the size is zero or some negative |
| 444 | value. */ |
| 445 | |
| 446 | DBX_STRINGTAB_SIZE (objfile) = bfd_section_size (sym_bfd, stringsect); |
| 447 | |
| 448 | if (DBX_SYMCOUNT (objfile) == 0 |
| 449 | || DBX_STRINGTAB_SIZE (objfile) == 0) |
| 450 | return; |
| 451 | |
| 452 | if (DBX_STRINGTAB_SIZE (objfile) <= 0 |
| 453 | || DBX_STRINGTAB_SIZE (objfile) > bfd_get_size (sym_bfd)) |
| 454 | error ("ridiculous string table size (%d bytes).", |
| 455 | DBX_STRINGTAB_SIZE (objfile)); |
| 456 | |
| 457 | DBX_STRINGTAB (objfile) = |
| 458 | (char *) obstack_alloc (&objfile -> psymbol_obstack, |
| 459 | DBX_STRINGTAB_SIZE (objfile)); |
| 460 | |
| 461 | /* Now read in the string table in one big gulp. */ |
| 462 | |
| 463 | val = bfd_seek (sym_bfd, STRING_TABLE_OFFSET, L_SET); |
| 464 | if (val < 0) |
| 465 | perror_with_name (name); |
| 466 | val = bfd_read (DBX_STRINGTAB (objfile), DBX_STRINGTAB_SIZE (objfile), 1, |
| 467 | sym_bfd); |
| 468 | if (val == 0) |
| 469 | error ("End of file reading string table"); |
| 470 | else if (val < 0) |
| 471 | /* It's possible bfd_read should be setting bfd_error, and we should be |
| 472 | checking that. But currently it doesn't set bfd_error. */ |
| 473 | perror_with_name (name); |
| 474 | else if (val != DBX_STRINGTAB_SIZE (objfile)) |
| 475 | error ("Short read reading string table"); |
| 476 | } |
| 477 | |
| 478 | /* PA specific parsing routine for section offsets. |
| 479 | |
| 480 | Plain and simple for now. */ |
| 481 | |
| 482 | static struct section_offsets * |
| 483 | pa_symfile_offsets (objfile, addr) |
| 484 | struct objfile *objfile; |
| 485 | CORE_ADDR addr; |
| 486 | { |
| 487 | struct section_offsets *section_offsets; |
| 488 | int i; |
| 489 | |
| 490 | section_offsets = (struct section_offsets *) |
| 491 | obstack_alloc (&objfile -> psymbol_obstack, |
| 492 | sizeof (struct section_offsets) + |
| 493 | sizeof (section_offsets->offsets) * (SECT_OFF_MAX-1)); |
| 494 | |
| 495 | for (i = 0; i < SECT_OFF_MAX; i++) |
| 496 | ANOFFSET (section_offsets, i) = addr; |
| 497 | |
| 498 | return section_offsets; |
| 499 | } |
| 500 | \f |
| 501 | /* Register that we are able to handle PA object file formats. */ |
| 502 | |
| 503 | /* This is probably a mistake. FIXME. Why can't the HP's use an ordinary |
| 504 | file format name with an -hppa suffix? */ |
| 505 | static struct sym_fns pa_sym_fns = |
| 506 | { |
| 507 | "hppa", /* sym_name: name or name prefix of BFD target type */ |
| 508 | 4, /* sym_namelen: number of significant sym_name chars */ |
| 509 | pa_new_init, /* sym_new_init: init anything gbl to entire symtab */ |
| 510 | pa_symfile_init, /* sym_init: read initial info, setup for sym_read() */ |
| 511 | pa_symfile_read, /* sym_read: read a symbol file into symtab */ |
| 512 | pa_symfile_finish, /* sym_finish: finished with file, cleanup */ |
| 513 | pa_symfile_offsets, /* sym_offsets: Translate ext. to int. relocation */ |
| 514 | NULL /* next: pointer to next struct sym_fns */ |
| 515 | }; |
| 516 | |
| 517 | void |
| 518 | _initialize_paread () |
| 519 | { |
| 520 | add_symtab_fns (&pa_sym_fns); |
| 521 | } |