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