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 pa_new_init
PARAMS ((struct objfile
*));
54 read_unwind_info
PARAMS ((struct objfile
*));
57 pa_symfile_read
PARAMS ((struct objfile
*, struct section_offsets
*, int));
60 pa_symfile_finish
PARAMS ((struct objfile
*));
63 pa_symtab_read
PARAMS ((bfd
*, CORE_ADDR
, struct objfile
*));
66 free_painfo
PARAMS ((PTR
));
68 static struct section_offsets
*
69 pa_symfile_offsets
PARAMS ((struct objfile
*, CORE_ADDR
));
72 record_minimal_symbol
PARAMS ((char *, CORE_ADDR
,
73 enum minimal_symbol_type
,
77 record_minimal_symbol (name
, address
, ms_type
, objfile
)
80 enum minimal_symbol_type ms_type
;
81 struct objfile
*objfile
;
83 name
= obsavestring (name
, strlen (name
), &objfile
-> symbol_obstack
);
84 prim_record_minimal_symbol (name
, address
, ms_type
);
91 pa_symtab_read -- read the symbol table of a PA file
95 void pa_symtab_read (bfd *abfd, CORE_ADDR addr,
96 struct objfile *objfile)
100 Given an open bfd, a base address to relocate symbols to, and a
101 flag that specifies whether or not this bfd is for an executable
102 or not (may be shared library for example), add all the global
103 function and data symbols to the minimal symbol table.
107 pa_symtab_read (abfd
, addr
, objfile
)
110 struct objfile
*objfile
;
112 unsigned int number_of_symbols
;
116 struct symbol_dictionary_record
*buf
, *bufp
, *endbufp
;
118 CONST
int symsize
= sizeof (struct symbol_dictionary_record
);
120 number_of_symbols
= bfd_get_symcount (abfd
);
122 buf
= alloca (symsize
* number_of_symbols
);
123 bfd_seek (abfd
, obj_som_sym_filepos (abfd
), L_SET
);
124 val
= bfd_read (buf
, symsize
* number_of_symbols
, 1, abfd
);
125 if (val
!= symsize
* number_of_symbols
)
126 error ("Couldn't read symbol dictionary!");
128 stringtab
= alloca (obj_som_stringtab_size (abfd
));
129 bfd_seek (abfd
, obj_som_str_filepos (abfd
), L_SET
);
130 val
= bfd_read (stringtab
, obj_som_stringtab_size (abfd
), 1, abfd
);
131 if (val
!= obj_som_stringtab_size (abfd
))
132 error ("Can't read in HP string table.");
134 endbufp
= buf
+ number_of_symbols
;
135 for (bufp
= buf
; bufp
< endbufp
; ++bufp
)
137 enum minimal_symbol_type ms_type
;
141 switch (bufp
->symbol_scope
)
144 switch (bufp
->symbol_type
)
155 symname
= bufp
->name
.n_strx
+ stringtab
;
157 bufp
->symbol_value
&= ~0x3; /* clear out permission bits */
160 symname
= bufp
->name
.n_strx
+ stringtab
;
169 /* SS_GLOBAL and SS_LOCAL are two names for the same thing (!). */
173 switch (bufp
->symbol_type
)
180 symname
= bufp
->name
.n_strx
+ stringtab
;
181 ms_type
= mst_file_text
;
182 bufp
->symbol_value
&= ~0x3; /* clear out permission bits */
185 /* GAS leaves labels in .o files after assembling. At
186 least labels starting with "LS$", "LBB$", "LBE$",
187 "LC$", and "L$" can happen. This should be fixed in
188 the assembler and/or compiler, to save space in the
189 executable (and because having GDB make gross
190 distinctions based on the name is kind of ugly), but
191 until then, just ignore them. */
193 && (symname
[1] == '$' || symname
[2] == '$'
194 || symname
[3] == '$'))
202 symname
= bufp
->name
.n_strx
+ stringtab
;
203 ms_type
= mst_file_text
;
204 bufp
->symbol_value
&= ~0x3; /* clear out permission bits */
208 symname
= bufp
->name
.n_strx
+ stringtab
;
209 ms_type
= mst_file_data
;
210 goto check_strange_names
;
221 if (bufp
->name
.n_strx
> obj_som_stringtab_size (abfd
))
222 error ("Invalid symbol data; bad HP string table offset: %d",
225 record_minimal_symbol (symname
,
226 bufp
->symbol_value
, ms_type
,
230 install_minimal_symbols (objfile
);
233 /* Read in the backtrace information stored in the `$UNWIND_START$' section of
234 the object file. This info is used mainly by find_unwind_entry() to find
235 out the stack frame size and frame pointer used by procedures. We put
236 everything on the psymbol obstack in the objfile so that it automatically
237 gets freed when the objfile is destroyed. */
240 read_unwind_info (objfile
)
241 struct objfile
*objfile
;
243 asection
*unwind_sec
;
244 struct obj_unwind_info
*ui
;
246 ui
= obstack_alloc (&objfile
->psymbol_obstack
,
247 sizeof (struct obj_unwind_info
));
253 unwind_sec
= bfd_get_section_by_name (objfile
->obfd
,
260 size
= bfd_section_size (objfile
->obfd
, unwind_sec
);
261 ui
->table
= obstack_alloc (&objfile
->psymbol_obstack
, size
);
262 ui
->last
= size
/ sizeof (struct unwind_table_entry
) - 1;
264 bfd_get_section_contents (objfile
->obfd
, unwind_sec
, ui
->table
,
267 OBJ_UNWIND_INFO (objfile
) = ui
;
271 /* Scan and build partial symbols for a symbol file.
272 We have been initialized by a call to pa_symfile_init, which
273 currently does nothing.
275 SECTION_OFFSETS is a set of offsets to apply to relocate the symbols
276 in each section. This is ignored, as it isn't needed for the PA.
278 MAINLINE is true if we are reading the main symbol
279 table (as opposed to a shared lib or dynamically loaded file).
281 This function only does the minimum work necessary for letting the
282 user "name" things symbolically; it does not read the entire symtab.
283 Instead, it reads the external and static symbols and puts them in partial
284 symbol tables. When more extensive information is requested of a
285 file, the corresponding partial symbol table is mutated into a full
286 fledged symbol table by going back and reading the symbols
289 We look for sections with specific names, to tell us what debug
290 format to look for: FIXME!!!
292 pastab_build_psymtabs() handles STABS symbols.
294 Note that PA files have a "minimal" symbol table, which is vaguely
295 reminiscent of a COFF symbol table, but has only the minimal information
296 necessary for linking. We process this also, and use the information to
297 build gdb's minimal symbol table. This gives us some minimal debugging
298 capability even for files compiled without -g. */
301 pa_symfile_read (objfile
, section_offsets
, mainline
)
302 struct objfile
*objfile
;
303 struct section_offsets
*section_offsets
;
306 bfd
*abfd
= objfile
->obfd
;
307 struct cleanup
*back_to
;
310 init_minimal_symbol_collection ();
311 back_to
= make_cleanup (discard_minimal_symbols
, 0);
313 make_cleanup (free_painfo
, (PTR
) objfile
);
315 /* Process the normal PA symbol table first. */
317 /* FIXME, should take a section_offsets param, not just an offset. */
319 offset
= ANOFFSET (section_offsets
, 0);
320 pa_symtab_read (abfd
, offset
, objfile
);
322 /* Now process debugging information, which is contained in
323 special PA sections. */
325 pastab_build_psymtabs (objfile
, section_offsets
, mainline
);
327 read_unwind_info(objfile
);
329 do_cleanups (back_to
);
332 /* This cleans up the objfile's sym_stab_info pointer, and the chain of
333 stab_section_info's, that might be dangling from it. */
339 struct objfile
*objfile
= (struct objfile
*)objp
;
340 struct dbx_symfile_info
*dbxinfo
= (struct dbx_symfile_info
*)
341 objfile
->sym_stab_info
;
342 struct stab_section_info
*ssi
, *nssi
;
344 ssi
= dbxinfo
->stab_section_info
;
348 mfree (objfile
->md
, ssi
);
352 dbxinfo
->stab_section_info
= 0; /* Just say No mo info about this. */
355 /* Initialize anything that needs initializing when a completely new symbol
356 file is specified (not just adding some symbols from another file, e.g. a
359 We reinitialize buildsym, since we may be reading stabs from a PA file. */
363 struct objfile
*ignore
;
365 stabsread_new_init ();
366 buildsym_new_init ();
369 /* Perform any local cleanups required when we are done with a particular
370 objfile. I.E, we are in the process of discarding all symbol information
371 for an objfile, freeing up all memory held for it, and unlinking the
372 objfile struct from the global list of known objfiles. */
375 pa_symfile_finish (objfile
)
376 struct objfile
*objfile
;
378 if (objfile
-> sym_stab_info
!= NULL
)
380 mfree (objfile
-> md
, objfile
-> sym_stab_info
);
384 /* PA specific initialization routine for reading symbols.
386 It is passed a pointer to a struct sym_fns which contains, among other
387 things, the BFD for the file whose symbols are being read, and a slot for
388 a pointer to "private data" which we can fill with goodies.
390 This routine is almost a complete ripoff of dbx_symfile_init. The
391 common parts of these routines should be extracted and used instead of
392 duplicating this code. FIXME. */
395 pa_symfile_init (objfile
)
396 struct objfile
*objfile
;
399 bfd
*sym_bfd
= objfile
->obfd
;
400 char *name
= bfd_get_filename (sym_bfd
);
401 asection
*stabsect
; /* Section containing symbol table entries */
402 asection
*stringsect
; /* Section containing symbol name strings */
404 stabsect
= bfd_get_section_by_name (sym_bfd
, "$GDB_SYMBOLS$");
405 stringsect
= bfd_get_section_by_name (sym_bfd
, "$GDB_STRINGS$");
407 /* Allocate struct to keep track of the symfile */
408 objfile
->sym_stab_info
= (PTR
)
409 xmmalloc (objfile
-> md
, sizeof (struct dbx_symfile_info
));
411 memset ((PTR
) objfile
->sym_stab_info
, 0, sizeof (struct dbx_symfile_info
));
417 error ("Found stabs, but not string section");
419 /* FIXME POKING INSIDE BFD DATA STRUCTURES */
420 #define STRING_TABLE_OFFSET (stringsect->filepos)
421 #define SYMBOL_TABLE_OFFSET (stabsect->filepos)
423 /* FIXME POKING INSIDE BFD DATA STRUCTURES */
425 DBX_SYMFILE_INFO (objfile
)->stab_section_info
= NULL
;
426 DBX_TEXT_SECT (objfile
) = bfd_get_section_by_name (sym_bfd
, ".text");
427 if (!DBX_TEXT_SECT (objfile
))
428 error ("Can't find .text section in symbol file");
430 /* FIXME: I suspect this should be external_nlist. The size of host
431 types like long and bfd_vma should not affect how we read the
433 DBX_SYMBOL_SIZE (objfile
) = sizeof (struct internal_nlist
);
434 DBX_SYMCOUNT (objfile
) = bfd_section_size (sym_bfd
, stabsect
)
435 / DBX_SYMBOL_SIZE (objfile
);
436 DBX_SYMTAB_OFFSET (objfile
) = SYMBOL_TABLE_OFFSET
;
438 /* Read the string table and stash it away in the psymbol_obstack. It is
439 only needed as long as we need to expand psymbols into full symbols,
440 so when we blow away the psymbol the string table goes away as well.
441 Note that gdb used to use the results of attempting to malloc the
442 string table, based on the size it read, as a form of sanity check
443 for botched byte swapping, on the theory that a byte swapped string
444 table size would be so totally bogus that the malloc would fail. Now
445 that we put in on the psymbol_obstack, we can't do this since gdb gets
446 a fatal error (out of virtual memory) if the size is bogus. We can
447 however at least check to see if the size is zero or some negative
450 DBX_STRINGTAB_SIZE (objfile
) = bfd_section_size (sym_bfd
, stringsect
);
452 if (DBX_SYMCOUNT (objfile
) == 0
453 || DBX_STRINGTAB_SIZE (objfile
) == 0)
456 if (DBX_STRINGTAB_SIZE (objfile
) <= 0
457 || DBX_STRINGTAB_SIZE (objfile
) > bfd_get_size (sym_bfd
))
458 error ("ridiculous string table size (%d bytes).",
459 DBX_STRINGTAB_SIZE (objfile
));
461 DBX_STRINGTAB (objfile
) =
462 (char *) obstack_alloc (&objfile
-> psymbol_obstack
,
463 DBX_STRINGTAB_SIZE (objfile
));
465 /* Now read in the string table in one big gulp. */
467 val
= bfd_seek (sym_bfd
, STRING_TABLE_OFFSET
, L_SET
);
469 perror_with_name (name
);
470 val
= bfd_read (DBX_STRINGTAB (objfile
), DBX_STRINGTAB_SIZE (objfile
), 1,
473 error ("End of file reading string table");
475 /* It's possible bfd_read should be setting bfd_error, and we should be
476 checking that. But currently it doesn't set bfd_error. */
477 perror_with_name (name
);
478 else if (val
!= DBX_STRINGTAB_SIZE (objfile
))
479 error ("Short read reading string table");
482 /* PA specific parsing routine for section offsets.
484 Plain and simple for now. */
486 static struct section_offsets
*
487 pa_symfile_offsets (objfile
, addr
)
488 struct objfile
*objfile
;
491 struct section_offsets
*section_offsets
;
494 objfile
->num_sections
= SECT_OFF_MAX
;
495 section_offsets
= (struct section_offsets
*)
496 obstack_alloc (&objfile
-> psymbol_obstack
,
497 sizeof (struct section_offsets
)
498 + sizeof (section_offsets
->offsets
) * (SECT_OFF_MAX
-1));
500 for (i
= 0; i
< SECT_OFF_MAX
; i
++)
501 ANOFFSET (section_offsets
, i
) = addr
;
503 return section_offsets
;
506 /* Register that we are able to handle SOM object file formats. */
508 static struct sym_fns pa_sym_fns
=
510 bfd_target_som_flavour
,
511 pa_new_init
, /* sym_new_init: init anything gbl to entire symtab */
512 pa_symfile_init
, /* sym_init: read initial info, setup for sym_read() */
513 pa_symfile_read
, /* sym_read: read a symbol file into symtab */
514 pa_symfile_finish
, /* sym_finish: finished with file, cleanup */
515 pa_symfile_offsets
, /* sym_offsets: Translate ext. to int. relocation */
516 NULL
/* next: pointer to next struct sym_fns */
520 _initialize_paread ()
522 add_symtab_fns (&pa_sym_fns
);