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