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 #ifdef SMASH_TEXT_ADDRESS
162 SMASH_TEXT_ADDRESS (bufp
->symbol_value
);
166 symname
= bufp
->name
.n_strx
+ stringtab
;
175 /* SS_GLOBAL and SS_LOCAL are two names for the same thing (!). */
179 switch (bufp
->symbol_type
)
186 symname
= bufp
->name
.n_strx
+ stringtab
;
187 ms_type
= mst_file_text
;
188 #ifdef SMASH_TEXT_ADDRESS
189 SMASH_TEXT_ADDRESS (bufp
->symbol_value
);
193 /* Utah GCC 2.5, FSF GCC 2.6 and later generate correct local
194 label prefixes for stabs, constant data, etc. So we need
195 only filter out L$ symbols which are left in due to
196 limitations in how GAS generates SOM relocations.
198 When linking in the HPUX C-library the HP linker has
199 the nasty habit of placing section symbols from the literal
200 subspaces in the middle of the program's text. Filter
201 those out as best we can. Check for first and last character
203 if ((symname
[0] == 'L' && symname
[1] == '$')
204 || (symname
[0] == '$' && symname
[strlen(symname
) - 1] == '$'))
212 symname
= bufp
->name
.n_strx
+ stringtab
;
213 ms_type
= mst_file_text
;
214 #ifdef SMASH_TEXT_ADDRESS
215 SMASH_TEXT_ADDRESS (bufp
->symbol_value
);
220 symname
= bufp
->name
.n_strx
+ stringtab
;
221 ms_type
= mst_file_data
;
222 goto check_strange_names
;
233 if (bufp
->name
.n_strx
> obj_som_stringtab_size (abfd
))
234 error ("Invalid symbol data; bad HP string table offset: %d",
237 record_minimal_symbol (symname
,
238 bufp
->symbol_value
, ms_type
,
242 install_minimal_symbols (objfile
);
245 /* Compare the start address for two unwind entries returning 1 if
246 the first address is larger than the second, -1 if the second is
247 larger than the first, and zero if they are equal. */
250 compare_unwind_entries (a
, b
)
251 struct unwind_table_entry
*a
;
252 struct unwind_table_entry
*b
;
254 if (a
->region_start
> b
->region_start
)
256 else if (a
->region_start
< b
->region_start
)
262 /* Read in the backtrace information stored in the `$UNWIND_START$' section of
263 the object file. This info is used mainly by find_unwind_entry() to find
264 out the stack frame size and frame pointer used by procedures. We put
265 everything on the psymbol obstack in the objfile so that it automatically
266 gets freed when the objfile is destroyed. */
269 read_unwind_info (objfile
)
270 struct objfile
*objfile
;
272 asection
*unwind_sec
, *stub_unwind_sec
;
273 unsigned unwind_size
, stub_unwind_size
, total_size
;
274 unsigned index
, unwind_entries
, stub_entries
, total_entries
;
275 struct obj_unwind_info
*ui
;
277 ui
= obstack_alloc (&objfile
->psymbol_obstack
,
278 sizeof (struct obj_unwind_info
));
284 /* Get hooks to both unwind sections. */
285 unwind_sec
= bfd_get_section_by_name (objfile
->obfd
, "$UNWIND_START$");
286 stub_unwind_sec
= bfd_get_section_by_name (objfile
->obfd
, "$UNWIND_END$");
288 /* Get sizes and unwind counts for both sections. */
291 unwind_size
= bfd_section_size (objfile
->obfd
, unwind_sec
);
292 unwind_entries
= unwind_size
/ UNWIND_ENTRY_SIZE
;
302 stub_unwind_size
= bfd_section_size (objfile
->obfd
, stub_unwind_sec
);
303 stub_entries
= stub_unwind_size
/ STUB_UNWIND_ENTRY_SIZE
;
307 stub_unwind_size
= 0;
311 /* Compute total number of stubs. */
312 total_entries
= unwind_entries
+ stub_entries
;
313 total_size
= total_entries
* sizeof (struct unwind_table_entry
);
315 /* Allocate memory for the unwind table. */
316 ui
->table
= obstack_alloc (&objfile
->psymbol_obstack
, total_size
);
317 ui
->last
= total_entries
- 1;
319 /* We will read the unwind entries into temporary memory, then
320 fill in the actual unwind table. */
325 char *buf
= alloca (unwind_size
);
327 bfd_get_section_contents (objfile
->obfd
, unwind_sec
, buf
, 0, unwind_size
);
329 /* Now internalize the information being careful to handle host/target
331 for (i
= 0; i
< unwind_entries
; i
++)
333 ui
->table
[i
].region_start
= bfd_get_32 (objfile
->obfd
,
336 ui
->table
[i
].region_end
= bfd_get_32 (objfile
->obfd
, (bfd_byte
*)buf
);
338 tmp
= bfd_get_32 (objfile
->obfd
, (bfd_byte
*)buf
);
340 ui
->table
[i
].Cannot_unwind
= (tmp
>> 31) & 0x1;;
341 ui
->table
[i
].Millicode
= (tmp
>> 30) & 0x1;
342 ui
->table
[i
].Millicode_save_sr0
= (tmp
>> 29) & 0x1;
343 ui
->table
[i
].Region_description
= (tmp
>> 27) & 0x3;
344 ui
->table
[i
].reserved1
= (tmp
>> 26) & 0x1;
345 ui
->table
[i
].Entry_SR
= (tmp
>> 25) & 0x1;
346 ui
->table
[i
].Entry_FR
= (tmp
>> 21) & 0xf;
347 ui
->table
[i
].Entry_GR
= (tmp
>> 16) & 0x1f;
348 ui
->table
[i
].Args_stored
= (tmp
>> 15) & 0x1;
349 ui
->table
[i
].Variable_Frame
= (tmp
>> 14) & 0x1;
350 ui
->table
[i
].Separate_Package_Body
= (tmp
>> 13) & 0x1;
351 ui
->table
[i
].Frame_Extension_Millicode
= (tmp
>> 12 ) & 0x1;
352 ui
->table
[i
].Stack_Overflow_Check
= (tmp
>> 11) & 0x1;
353 ui
->table
[i
].Two_Instruction_SP_Increment
= (tmp
>> 10) & 0x1;
354 ui
->table
[i
].Ada_Region
= (tmp
>> 9) & 0x1;
355 ui
->table
[i
].reserved2
= (tmp
>> 5) & 0xf;
356 ui
->table
[i
].Save_SP
= (tmp
>> 4) & 0x1;
357 ui
->table
[i
].Save_RP
= (tmp
>> 3) & 0x1;
358 ui
->table
[i
].Save_MRP_in_frame
= (tmp
>> 2) & 0x1;
359 ui
->table
[i
].extn_ptr_defined
= (tmp
>> 1) & 0x1;
360 ui
->table
[i
].Cleanup_defined
= tmp
& 0x1;
361 tmp
= bfd_get_32 (objfile
->obfd
, (bfd_byte
*)buf
);
363 ui
->table
[i
].MPE_XL_interrupt_marker
= (tmp
>> 31) & 0x1;
364 ui
->table
[i
].HP_UX_interrupt_marker
= (tmp
>> 30) & 0x1;
365 ui
->table
[i
].Large_frame
= (tmp
>> 29) & 0x1;
366 ui
->table
[i
].reserved4
= (tmp
>> 27) & 0x3;
367 ui
->table
[i
].Total_frame_size
= tmp
& 0x7ffffff;
372 if (stub_unwind_size
> 0)
375 char *buf
= alloca (stub_unwind_size
);
377 /* Read in the stub unwind entries. */
378 bfd_get_section_contents (objfile
->obfd
, stub_unwind_sec
, buf
,
379 0, stub_unwind_size
);
381 /* Now convert them into regular unwind entries. */
382 index
= unwind_entries
;
383 for (i
= 0; i
< stub_entries
; i
++, index
++)
385 /* Clear out the next unwind entry. */
386 memset (&ui
->table
[index
], 0, sizeof (struct unwind_table_entry
));
388 /* Convert offset & size into region_start and region_end.
389 Stuff away the stub type into "reserved" fields. */
390 ui
->table
[index
].region_start
= bfd_get_32 (objfile
->obfd
,
393 ui
->table
[index
].stub_type
= bfd_get_8 (objfile
->obfd
,
396 ui
->table
[index
].region_end
397 = ui
->table
[index
].region_start
+ 4 * bfd_get_16 (objfile
->obfd
,
404 /* Unwind table needs to be kept sorted. */
405 qsort (ui
->table
, total_entries
, sizeof (struct unwind_table_entry
),
406 compare_unwind_entries
);
408 /* Keep a pointer to the unwind information. */
409 objfile
->obj_private
= (PTR
) ui
;
412 /* Scan and build partial symbols for a symbol file.
413 We have been initialized by a call to pa_symfile_init, which
414 currently does nothing.
416 SECTION_OFFSETS is a set of offsets to apply to relocate the symbols
417 in each section. This is ignored, as it isn't needed for the PA.
419 MAINLINE is true if we are reading the main symbol
420 table (as opposed to a shared lib or dynamically loaded file).
422 This function only does the minimum work necessary for letting the
423 user "name" things symbolically; it does not read the entire symtab.
424 Instead, it reads the external and static symbols and puts them in partial
425 symbol tables. When more extensive information is requested of a
426 file, the corresponding partial symbol table is mutated into a full
427 fledged symbol table by going back and reading the symbols
430 We look for sections with specific names, to tell us what debug
431 format to look for: FIXME!!!
433 pastab_build_psymtabs() handles STABS symbols.
435 Note that PA files have a "minimal" symbol table, which is vaguely
436 reminiscent of a COFF symbol table, but has only the minimal information
437 necessary for linking. We process this also, and use the information to
438 build gdb's minimal symbol table. This gives us some minimal debugging
439 capability even for files compiled without -g. */
442 pa_symfile_read (objfile
, section_offsets
, mainline
)
443 struct objfile
*objfile
;
444 struct section_offsets
*section_offsets
;
447 bfd
*abfd
= objfile
->obfd
;
448 struct cleanup
*back_to
;
451 init_minimal_symbol_collection ();
452 back_to
= make_cleanup (discard_minimal_symbols
, 0);
454 make_cleanup (free_painfo
, (PTR
) objfile
);
456 /* Process the normal PA symbol table first. */
458 /* FIXME, should take a section_offsets param, not just an offset. */
460 offset
= ANOFFSET (section_offsets
, 0);
461 pa_symtab_read (abfd
, offset
, objfile
);
463 /* Now process debugging information, which is contained in
464 special PA sections. */
466 pastab_build_psymtabs (objfile
, section_offsets
, mainline
);
468 read_unwind_info(objfile
);
470 do_cleanups (back_to
);
473 /* This cleans up the objfile's sym_stab_info pointer, and the chain of
474 stab_section_info's, that might be dangling from it. */
480 struct objfile
*objfile
= (struct objfile
*)objp
;
481 struct dbx_symfile_info
*dbxinfo
= (struct dbx_symfile_info
*)
482 objfile
->sym_stab_info
;
483 struct stab_section_info
*ssi
, *nssi
;
485 ssi
= dbxinfo
->stab_section_info
;
489 mfree (objfile
->md
, ssi
);
493 dbxinfo
->stab_section_info
= 0; /* Just say No mo info about this. */
496 /* Initialize anything that needs initializing when a completely new symbol
497 file is specified (not just adding some symbols from another file, e.g. a
500 We reinitialize buildsym, since we may be reading stabs from a PA file. */
504 struct objfile
*ignore
;
506 stabsread_new_init ();
507 buildsym_new_init ();
510 /* Perform any local cleanups required when we are done with a particular
511 objfile. I.E, we are in the process of discarding all symbol information
512 for an objfile, freeing up all memory held for it, and unlinking the
513 objfile struct from the global list of known objfiles. */
516 pa_symfile_finish (objfile
)
517 struct objfile
*objfile
;
519 if (objfile
-> sym_stab_info
!= NULL
)
521 mfree (objfile
-> md
, objfile
-> sym_stab_info
);
525 /* PA specific initialization routine for reading symbols.
527 It is passed a pointer to a struct sym_fns which contains, among other
528 things, the BFD for the file whose symbols are being read, and a slot for
529 a pointer to "private data" which we can fill with goodies.
531 This routine is almost a complete ripoff of dbx_symfile_init. The
532 common parts of these routines should be extracted and used instead of
533 duplicating this code. FIXME. */
536 pa_symfile_init (objfile
)
537 struct objfile
*objfile
;
540 bfd
*sym_bfd
= objfile
->obfd
;
541 char *name
= bfd_get_filename (sym_bfd
);
542 asection
*stabsect
; /* Section containing symbol table entries */
543 asection
*stringsect
; /* Section containing symbol name strings */
545 stabsect
= bfd_get_section_by_name (sym_bfd
, "$GDB_SYMBOLS$");
546 stringsect
= bfd_get_section_by_name (sym_bfd
, "$GDB_STRINGS$");
548 /* Allocate struct to keep track of the symfile */
549 objfile
->sym_stab_info
= (PTR
)
550 xmmalloc (objfile
-> md
, sizeof (struct dbx_symfile_info
));
552 memset ((PTR
) objfile
->sym_stab_info
, 0, sizeof (struct dbx_symfile_info
));
555 /* FIXME POKING INSIDE BFD DATA STRUCTURES */
556 #define STRING_TABLE_OFFSET (stringsect->filepos)
557 #define SYMBOL_TABLE_OFFSET (stabsect->filepos)
559 /* FIXME POKING INSIDE BFD DATA STRUCTURES */
561 DBX_SYMFILE_INFO (objfile
)->stab_section_info
= NULL
;
562 DBX_TEXT_SECT (objfile
) = bfd_get_section_by_name (sym_bfd
, "$TEXT$");
563 if (!DBX_TEXT_SECT (objfile
))
564 error ("Can't find $TEXT$ section in symbol file");
570 error ("Found stabs, but not string section");
572 /* FIXME: I suspect this should be external_nlist. The size of host
573 types like long and bfd_vma should not affect how we read the
575 DBX_SYMBOL_SIZE (objfile
) = sizeof (struct internal_nlist
);
576 DBX_SYMCOUNT (objfile
) = bfd_section_size (sym_bfd
, stabsect
)
577 / DBX_SYMBOL_SIZE (objfile
);
578 DBX_SYMTAB_OFFSET (objfile
) = SYMBOL_TABLE_OFFSET
;
580 /* Read the string table and stash it away in the psymbol_obstack. It is
581 only needed as long as we need to expand psymbols into full symbols,
582 so when we blow away the psymbol the string table goes away as well.
583 Note that gdb used to use the results of attempting to malloc the
584 string table, based on the size it read, as a form of sanity check
585 for botched byte swapping, on the theory that a byte swapped string
586 table size would be so totally bogus that the malloc would fail. Now
587 that we put in on the psymbol_obstack, we can't do this since gdb gets
588 a fatal error (out of virtual memory) if the size is bogus. We can
589 however at least check to see if the size is zero or some negative
592 DBX_STRINGTAB_SIZE (objfile
) = bfd_section_size (sym_bfd
, stringsect
);
594 if (DBX_SYMCOUNT (objfile
) == 0
595 || DBX_STRINGTAB_SIZE (objfile
) == 0)
598 if (DBX_STRINGTAB_SIZE (objfile
) <= 0
599 || DBX_STRINGTAB_SIZE (objfile
) > bfd_get_size (sym_bfd
))
600 error ("ridiculous string table size (%d bytes).",
601 DBX_STRINGTAB_SIZE (objfile
));
603 DBX_STRINGTAB (objfile
) =
604 (char *) obstack_alloc (&objfile
-> psymbol_obstack
,
605 DBX_STRINGTAB_SIZE (objfile
));
607 /* Now read in the string table in one big gulp. */
609 val
= bfd_seek (sym_bfd
, STRING_TABLE_OFFSET
, L_SET
);
611 perror_with_name (name
);
612 val
= bfd_read (DBX_STRINGTAB (objfile
), DBX_STRINGTAB_SIZE (objfile
), 1,
615 error ("End of file reading string table");
617 /* It's possible bfd_read should be setting bfd_error, and we should be
618 checking that. But currently it doesn't set bfd_error. */
619 perror_with_name (name
);
620 else if (val
!= DBX_STRINGTAB_SIZE (objfile
))
621 error ("Short read reading string table");
624 /* PA specific parsing routine for section offsets.
626 Plain and simple for now. */
628 static struct section_offsets
*
629 pa_symfile_offsets (objfile
, addr
)
630 struct objfile
*objfile
;
633 struct section_offsets
*section_offsets
;
636 objfile
->num_sections
= SECT_OFF_MAX
;
637 section_offsets
= (struct section_offsets
*)
638 obstack_alloc (&objfile
-> psymbol_obstack
,
639 sizeof (struct section_offsets
)
640 + sizeof (section_offsets
->offsets
) * (SECT_OFF_MAX
-1));
642 for (i
= 0; i
< SECT_OFF_MAX
; i
++)
643 ANOFFSET (section_offsets
, i
) = addr
;
645 return section_offsets
;
648 /* Register that we are able to handle SOM object file formats. */
650 static struct sym_fns pa_sym_fns
=
652 bfd_target_som_flavour
,
653 pa_new_init
, /* sym_new_init: init anything gbl to entire symtab */
654 pa_symfile_init
, /* sym_init: read initial info, setup for sym_read() */
655 pa_symfile_read
, /* sym_read: read a symbol file into symtab */
656 pa_symfile_finish
, /* sym_finish: finished with file, cleanup */
657 pa_symfile_offsets
, /* sym_offsets: Translate ext. to int. relocation */
658 NULL
/* next: pointer to next struct sym_fns */
662 _initialize_paread ()
664 add_symtab_fns (&pa_sym_fns
);