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