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.
5 This file is part of GDB.
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.
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.
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. */
23 #include <time.h> /* For time_t in libbfd.h. */
24 #include <sys/types.h> /* For time_t, if not in time.h. */
33 #include "stabsread.h"
34 #include "gdb-stabs.h"
35 #include "complaints.h"
40 /* Size of n_value and n_strx fields in a stab symbol. */
41 #define BYTES_IN_WORD 4
43 #include "aout/aout64.h"
45 /* Various things we might complain about... */
48 pa_symfile_init PARAMS ((struct objfile *));
51 compare_unwind_entries PARAMS ((struct unwind_table_entry *,
52 struct unwind_table_entry *));
55 pa_new_init PARAMS ((struct objfile *));
58 read_unwind_info PARAMS ((struct objfile *));
61 pa_symfile_read PARAMS ((struct objfile *, struct section_offsets *, int));
64 pa_symfile_finish PARAMS ((struct objfile *));
67 pa_symtab_read PARAMS ((bfd *, CORE_ADDR, struct objfile *));
70 free_painfo PARAMS ((PTR));
72 static struct section_offsets *
73 pa_symfile_offsets PARAMS ((struct objfile *, CORE_ADDR));
76 record_minimal_symbol PARAMS ((char *, CORE_ADDR,
77 enum minimal_symbol_type,
81 record_minimal_symbol (name, address, ms_type, objfile)
84 enum minimal_symbol_type ms_type;
85 struct objfile *objfile;
87 name = obsavestring (name, strlen (name), &objfile -> symbol_obstack);
88 prim_record_minimal_symbol (name, address, ms_type, objfile);
95 pa_symtab_read -- read the symbol table of a PA file
99 void pa_symtab_read (bfd *abfd, CORE_ADDR addr,
100 struct objfile *objfile)
104 Given an open bfd, a base address to relocate symbols to, and a
105 flag that specifies whether or not this bfd is for an executable
106 or not (may be shared library for example), add all the global
107 function and data symbols to the minimal symbol table.
111 pa_symtab_read (abfd, addr, objfile)
114 struct objfile *objfile;
116 unsigned int number_of_symbols;
120 struct symbol_dictionary_record *buf, *bufp, *endbufp;
122 CONST int symsize = sizeof (struct symbol_dictionary_record);
124 number_of_symbols = bfd_get_symcount (abfd);
126 buf = alloca (symsize * number_of_symbols);
127 bfd_seek (abfd, obj_som_sym_filepos (abfd), L_SET);
128 val = bfd_read (buf, symsize * number_of_symbols, 1, abfd);
129 if (val != symsize * number_of_symbols)
130 error ("Couldn't read symbol dictionary!");
132 stringtab = alloca (obj_som_stringtab_size (abfd));
133 bfd_seek (abfd, obj_som_str_filepos (abfd), L_SET);
134 val = bfd_read (stringtab, obj_som_stringtab_size (abfd), 1, abfd);
135 if (val != obj_som_stringtab_size (abfd))
136 error ("Can't read in HP string table.");
138 endbufp = buf + number_of_symbols;
139 for (bufp = buf; bufp < endbufp; ++bufp)
141 enum minimal_symbol_type ms_type;
145 switch (bufp->symbol_scope)
148 switch (bufp->symbol_type)
159 symname = bufp->name.n_strx + stringtab;
161 bufp->symbol_value &= ~0x3; /* clear out permission bits */
164 symname = bufp->name.n_strx + stringtab;
173 /* SS_GLOBAL and SS_LOCAL are two names for the same thing (!). */
177 switch (bufp->symbol_type)
184 symname = bufp->name.n_strx + stringtab;
185 ms_type = mst_file_text;
186 bufp->symbol_value &= ~0x3; /* clear out permission bits */
189 /* GAS leaves labels in .o files after assembling. At
190 least labels starting with "LS$", "LBB$", "LBE$",
191 "LC$", and "L$" can happen. This should be fixed in
192 the assembler and/or compiler, to save space in the
193 executable (and because having GDB make gross
194 distinctions based on the name is kind of ugly), but
195 until then, just ignore them. ("L$" at least, has something
196 to do with getting relocation correct, so that one might
199 && (symname[1] == '$' || symname[2] == '$'
200 || symname[3] == '$'))
208 symname = bufp->name.n_strx + stringtab;
209 ms_type = mst_file_text;
210 bufp->symbol_value &= ~0x3; /* clear out permission bits */
214 symname = bufp->name.n_strx + stringtab;
215 ms_type = mst_file_data;
216 goto check_strange_names;
227 if (bufp->name.n_strx > obj_som_stringtab_size (abfd))
228 error ("Invalid symbol data; bad HP string table offset: %d",
231 record_minimal_symbol (symname,
232 bufp->symbol_value, ms_type,
236 install_minimal_symbols (objfile);
239 /* Compare the start address for two unwind entries returning 1 if
240 the first address is larger than the second, -1 if the second is
241 larger than the first, and zero if they are equal. */
244 compare_unwind_entries (a, b)
245 struct unwind_table_entry *a;
246 struct unwind_table_entry *b;
248 if (a->region_start > b->region_start)
250 else if (a->region_start < b->region_start)
256 /* Read in the backtrace information stored in the `$UNWIND_START$' section of
257 the object file. This info is used mainly by find_unwind_entry() to find
258 out the stack frame size and frame pointer used by procedures. We put
259 everything on the psymbol obstack in the objfile so that it automatically
260 gets freed when the objfile is destroyed. */
263 read_unwind_info (objfile)
264 struct objfile *objfile;
266 asection *unwind_sec, *stub_unwind_sec;
267 unsigned unwind_size, stub_unwind_size, total_size;
268 unsigned index, unwind_entries, stub_entries, total_entries;
269 struct obj_unwind_info *ui;
271 ui = obstack_alloc (&objfile->psymbol_obstack,
272 sizeof (struct obj_unwind_info));
278 /* Get hooks to both unwind sections. */
279 unwind_sec = bfd_get_section_by_name (objfile->obfd, "$UNWIND_START$");
280 stub_unwind_sec = bfd_get_section_by_name (objfile->obfd, "$UNWIND_END$");
282 /* Get sizes and unwind counts for both sections. */
285 unwind_size = bfd_section_size (objfile->obfd, unwind_sec);
286 unwind_entries = unwind_size / UNWIND_ENTRY_SIZE;
296 stub_unwind_size = bfd_section_size (objfile->obfd, stub_unwind_sec);
297 stub_entries = stub_unwind_size / STUB_UNWIND_ENTRY_SIZE;
301 stub_unwind_size = 0;
305 /* Compute total number of stubs. */
306 total_entries = unwind_entries + stub_entries;
307 total_size = total_entries * sizeof (struct unwind_table_entry);
309 /* Allocate memory for the unwind table. */
310 ui->table = obstack_alloc (&objfile->psymbol_obstack, total_size);
311 ui->last = total_entries + 1;
313 /* We will read the unwind entries into temporary memory, then
314 fill in the actual unwind table. */
319 char *buf = alloca (unwind_size);
321 bfd_get_section_contents (objfile->obfd, unwind_sec, buf, 0, unwind_size);
323 /* Now internalize the information being careful to handle host/target
325 for (i = 0; i < unwind_entries; i++)
327 ui->table[i].region_start = bfd_get_32 (objfile->obfd,
330 ui->table[i].region_end = bfd_get_32 (objfile->obfd, (bfd_byte *)buf);
332 tmp = bfd_get_32 (objfile->obfd, (bfd_byte *)buf);
334 ui->table[i].Cannot_unwind = (tmp >> 31) & 0x1;;
335 ui->table[i].Millicode = (tmp >> 30) & 0x1;
336 ui->table[i].Millicode_save_sr0 = (tmp >> 29) & 0x1;
337 ui->table[i].Region_description = (tmp >> 27) & 0x3;
338 ui->table[i].reserved1 = (tmp >> 26) & 0x1;
339 ui->table[i].Entry_SR = (tmp >> 25) & 0x1;
340 ui->table[i].Entry_FR = (tmp >> 21) & 0xf;
341 ui->table[i].Entry_GR = (tmp >> 16) & 0x1f;
342 ui->table[i].Args_stored = (tmp >> 15) & 0x1;
343 ui->table[i].Variable_Frame = (tmp >> 14) & 0x1;
344 ui->table[i].Separate_Package_Body = (tmp >> 13) & 0x1;
345 ui->table[i].Frame_Extension_Millicode = (tmp >> 12 ) & 0x1;
346 ui->table[i].Stack_Overflow_Check = (tmp >> 11) & 0x1;
347 ui->table[i].Two_Instruction_SP_Increment = (tmp >> 10) & 0x1;
348 ui->table[i].Ada_Region = (tmp >> 9) & 0x1;
349 ui->table[i].reserved2 = (tmp >> 5) & 0xf;
350 ui->table[i].Save_SP = (tmp >> 4) & 0x1;
351 ui->table[i].Save_RP = (tmp >> 3) & 0x1;
352 ui->table[i].Save_MRP_in_frame = (tmp >> 2) & 0x1;
353 ui->table[i].extn_ptr_defined = (tmp >> 1) & 0x1;
354 ui->table[i].Cleanup_defined = tmp & 0x1;
355 tmp = bfd_get_32 (objfile->obfd, (bfd_byte *)buf);
357 ui->table[i].MPE_XL_interrupt_marker = (tmp >> 31) & 0x1;
358 ui->table[i].HP_UX_interrupt_marker = (tmp >> 30) & 0x1;
359 ui->table[i].Large_frame = (tmp >> 29) & 0x1;
360 ui->table[i].reserved4 = (tmp >> 27) & 0x3;
361 ui->table[i].Total_frame_size = tmp & 0x7ffffff;
366 if (stub_unwind_size > 0)
369 char *buf = alloca (stub_unwind_size);
371 /* Read in the stub unwind entries. */
372 bfd_get_section_contents (objfile->obfd, stub_unwind_sec, buf,
373 0, stub_unwind_size);
375 /* Now convert them into regular unwind entries. */
376 index = unwind_entries;
377 for (i = 0; i < stub_entries; i++, index++)
379 /* Clear out the next unwind entry. */
380 memset (&ui->table[index], 0, sizeof (struct unwind_table_entry));
382 /* Convert offset & size into region_start and region_end.
383 Stuff away the stub type into "reserved" fields. */
384 ui->table[index].region_start = bfd_get_32 (objfile->obfd,
387 ui->table[index].stub_type = bfd_get_8 (objfile->obfd,
390 ui->table[index].region_end
391 = ui->table[index].region_start + 4 * bfd_get_16 (objfile->obfd,
398 /* Unwind table needs to be kept sorted. */
399 qsort (ui->table, total_entries, sizeof (struct unwind_table_entry),
400 compare_unwind_entries);
402 /* Keep a pointer to the unwind information. */
403 OBJ_UNWIND_INFO (objfile) = ui;
406 /* Scan and build partial symbols for a symbol file.
407 We have been initialized by a call to pa_symfile_init, which
408 currently does nothing.
410 SECTION_OFFSETS is a set of offsets to apply to relocate the symbols
411 in each section. This is ignored, as it isn't needed for the PA.
413 MAINLINE is true if we are reading the main symbol
414 table (as opposed to a shared lib or dynamically loaded file).
416 This function only does the minimum work necessary for letting the
417 user "name" things symbolically; it does not read the entire symtab.
418 Instead, it reads the external and static symbols and puts them in partial
419 symbol tables. When more extensive information is requested of a
420 file, the corresponding partial symbol table is mutated into a full
421 fledged symbol table by going back and reading the symbols
424 We look for sections with specific names, to tell us what debug
425 format to look for: FIXME!!!
427 pastab_build_psymtabs() handles STABS symbols.
429 Note that PA files have a "minimal" symbol table, which is vaguely
430 reminiscent of a COFF symbol table, but has only the minimal information
431 necessary for linking. We process this also, and use the information to
432 build gdb's minimal symbol table. This gives us some minimal debugging
433 capability even for files compiled without -g. */
436 pa_symfile_read (objfile, section_offsets, mainline)
437 struct objfile *objfile;
438 struct section_offsets *section_offsets;
441 bfd *abfd = objfile->obfd;
442 struct cleanup *back_to;
445 init_minimal_symbol_collection ();
446 back_to = make_cleanup (discard_minimal_symbols, 0);
448 make_cleanup (free_painfo, (PTR) objfile);
450 /* Process the normal PA symbol table first. */
452 /* FIXME, should take a section_offsets param, not just an offset. */
454 offset = ANOFFSET (section_offsets, 0);
455 pa_symtab_read (abfd, offset, objfile);
457 /* Now process debugging information, which is contained in
458 special PA sections. */
460 pastab_build_psymtabs (objfile, section_offsets, mainline);
462 read_unwind_info(objfile);
464 do_cleanups (back_to);
467 /* This cleans up the objfile's sym_stab_info pointer, and the chain of
468 stab_section_info's, that might be dangling from it. */
474 struct objfile *objfile = (struct objfile *)objp;
475 struct dbx_symfile_info *dbxinfo = (struct dbx_symfile_info *)
476 objfile->sym_stab_info;
477 struct stab_section_info *ssi, *nssi;
479 ssi = dbxinfo->stab_section_info;
483 mfree (objfile->md, ssi);
487 dbxinfo->stab_section_info = 0; /* Just say No mo info about this. */
490 /* Initialize anything that needs initializing when a completely new symbol
491 file is specified (not just adding some symbols from another file, e.g. a
494 We reinitialize buildsym, since we may be reading stabs from a PA file. */
498 struct objfile *ignore;
500 stabsread_new_init ();
501 buildsym_new_init ();
504 /* Perform any local cleanups required when we are done with a particular
505 objfile. I.E, we are in the process of discarding all symbol information
506 for an objfile, freeing up all memory held for it, and unlinking the
507 objfile struct from the global list of known objfiles. */
510 pa_symfile_finish (objfile)
511 struct objfile *objfile;
513 if (objfile -> sym_stab_info != NULL)
515 mfree (objfile -> md, objfile -> sym_stab_info);
519 /* PA specific initialization routine for reading symbols.
521 It is passed a pointer to a struct sym_fns which contains, among other
522 things, the BFD for the file whose symbols are being read, and a slot for
523 a pointer to "private data" which we can fill with goodies.
525 This routine is almost a complete ripoff of dbx_symfile_init. The
526 common parts of these routines should be extracted and used instead of
527 duplicating this code. FIXME. */
530 pa_symfile_init (objfile)
531 struct objfile *objfile;
534 bfd *sym_bfd = objfile->obfd;
535 char *name = bfd_get_filename (sym_bfd);
536 asection *stabsect; /* Section containing symbol table entries */
537 asection *stringsect; /* Section containing symbol name strings */
539 stabsect = bfd_get_section_by_name (sym_bfd, "$GDB_SYMBOLS$");
540 stringsect = bfd_get_section_by_name (sym_bfd, "$GDB_STRINGS$");
542 /* Allocate struct to keep track of the symfile */
543 objfile->sym_stab_info = (PTR)
544 xmmalloc (objfile -> md, sizeof (struct dbx_symfile_info));
546 memset ((PTR) objfile->sym_stab_info, 0, sizeof (struct dbx_symfile_info));
549 /* FIXME POKING INSIDE BFD DATA STRUCTURES */
550 #define STRING_TABLE_OFFSET (stringsect->filepos)
551 #define SYMBOL_TABLE_OFFSET (stabsect->filepos)
553 /* FIXME POKING INSIDE BFD DATA STRUCTURES */
555 DBX_SYMFILE_INFO (objfile)->stab_section_info = NULL;
556 DBX_TEXT_SECT (objfile) = bfd_get_section_by_name (sym_bfd, "$TEXT$");
557 if (!DBX_TEXT_SECT (objfile))
558 error ("Can't find $TEXT$ section in symbol file");
564 error ("Found stabs, but not string section");
566 /* FIXME: I suspect this should be external_nlist. The size of host
567 types like long and bfd_vma should not affect how we read the
569 DBX_SYMBOL_SIZE (objfile) = sizeof (struct internal_nlist);
570 DBX_SYMCOUNT (objfile) = bfd_section_size (sym_bfd, stabsect)
571 / DBX_SYMBOL_SIZE (objfile);
572 DBX_SYMTAB_OFFSET (objfile) = SYMBOL_TABLE_OFFSET;
574 /* Read the string table and stash it away in the psymbol_obstack. It is
575 only needed as long as we need to expand psymbols into full symbols,
576 so when we blow away the psymbol the string table goes away as well.
577 Note that gdb used to use the results of attempting to malloc the
578 string table, based on the size it read, as a form of sanity check
579 for botched byte swapping, on the theory that a byte swapped string
580 table size would be so totally bogus that the malloc would fail. Now
581 that we put in on the psymbol_obstack, we can't do this since gdb gets
582 a fatal error (out of virtual memory) if the size is bogus. We can
583 however at least check to see if the size is zero or some negative
586 DBX_STRINGTAB_SIZE (objfile) = bfd_section_size (sym_bfd, stringsect);
588 if (DBX_SYMCOUNT (objfile) == 0
589 || DBX_STRINGTAB_SIZE (objfile) == 0)
592 if (DBX_STRINGTAB_SIZE (objfile) <= 0
593 || DBX_STRINGTAB_SIZE (objfile) > bfd_get_size (sym_bfd))
594 error ("ridiculous string table size (%d bytes).",
595 DBX_STRINGTAB_SIZE (objfile));
597 DBX_STRINGTAB (objfile) =
598 (char *) obstack_alloc (&objfile -> psymbol_obstack,
599 DBX_STRINGTAB_SIZE (objfile));
601 /* Now read in the string table in one big gulp. */
603 val = bfd_seek (sym_bfd, STRING_TABLE_OFFSET, L_SET);
605 perror_with_name (name);
606 val = bfd_read (DBX_STRINGTAB (objfile), DBX_STRINGTAB_SIZE (objfile), 1,
609 error ("End of file reading string table");
611 /* It's possible bfd_read should be setting bfd_error, and we should be
612 checking that. But currently it doesn't set bfd_error. */
613 perror_with_name (name);
614 else if (val != DBX_STRINGTAB_SIZE (objfile))
615 error ("Short read reading string table");
618 /* PA specific parsing routine for section offsets.
620 Plain and simple for now. */
622 static struct section_offsets *
623 pa_symfile_offsets (objfile, addr)
624 struct objfile *objfile;
627 struct section_offsets *section_offsets;
630 objfile->num_sections = SECT_OFF_MAX;
631 section_offsets = (struct section_offsets *)
632 obstack_alloc (&objfile -> psymbol_obstack,
633 sizeof (struct section_offsets)
634 + sizeof (section_offsets->offsets) * (SECT_OFF_MAX-1));
636 for (i = 0; i < SECT_OFF_MAX; i++)
637 ANOFFSET (section_offsets, i) = addr;
639 return section_offsets;
642 /* Register that we are able to handle SOM object file formats. */
644 static struct sym_fns pa_sym_fns =
646 bfd_target_som_flavour,
647 pa_new_init, /* sym_new_init: init anything gbl to entire symtab */
648 pa_symfile_init, /* sym_init: read initial info, setup for sym_read() */
649 pa_symfile_read, /* sym_read: read a symbol file into symtab */
650 pa_symfile_finish, /* sym_finish: finished with file, cleanup */
651 pa_symfile_offsets, /* sym_offsets: Translate ext. to int. relocation */
652 NULL /* next: pointer to next struct sym_fns */
656 _initialize_paread ()
658 add_symtab_fns (&pa_sym_fns);