* paread.c (pa_symtab_read): Put file-local symbols in minimal symbols.
[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 #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, *endbufp;
109   char *symname;
110   CONST int symsize = sizeof (struct symbol_dictionary_record);
111
112   number_of_symbols = bfd_get_symcount (abfd);
113
114   buf = alloca (symsize * number_of_symbols);
115   bfd_seek (abfd, obj_sym_filepos (abfd), L_SET);
116   val = bfd_read (buf, symsize * number_of_symbols, 1, abfd);
117   if (val != symsize * number_of_symbols)
118     error ("Couldn't read symbol dictionary!");
119
120   stringtab = alloca (obj_stringtab_size (abfd));
121   bfd_seek (abfd, obj_str_filepos (abfd), L_SET);
122   val = bfd_read (stringtab, obj_stringtab_size (abfd), 1, abfd);
123   if (val != obj_stringtab_size (abfd))
124     error ("Can't read in HP string table.");
125
126   endbufp = buf + number_of_symbols;
127   for (bufp = buf; bufp < endbufp; ++bufp)
128     {
129       enum minimal_symbol_type ms_type;
130
131       QUIT;
132
133       switch (bufp->symbol_scope)
134         {
135         case SS_UNIVERSAL:
136           switch (bufp->symbol_type)
137             {
138             case ST_SYM_EXT:
139             case ST_ARG_EXT:
140               continue;
141
142             case ST_CODE:
143             case ST_PRI_PROG:
144             case ST_SEC_PROG:
145             case ST_ENTRY:
146             case ST_MILLICODE:
147               symname = bufp->name.n_strx + stringtab;
148               ms_type = mst_text;
149               bufp->symbol_value &= ~0x3; /* clear out permission bits */
150               break;
151             case ST_DATA:
152               symname = bufp->name.n_strx + stringtab;
153               ms_type = mst_data;
154               break;
155             default:
156               continue;
157             }
158           break;
159
160         case SS_GLOBAL:
161         case SS_LOCAL:
162           switch (bufp->symbol_type)
163             {
164             case ST_SYM_EXT:
165             case ST_ARG_EXT:
166               continue;
167
168             case ST_CODE:
169               symname = bufp->name.n_strx + stringtab;
170               /* GAS leaves symbols with the prefixes "LS$", "LBB$",
171                  and "LBE$" in .o files after assembling.  And thus
172                  they appear in the final executable.  This can
173                  cause problems if these special symbols have the
174                  same value as real symbols.  So ignore them.  Is this
175                  meant as a feature, or is it just a GAS bug?  */
176               if (*symname == 'L'
177                   && (symname[2] == '$' && symname[1] == 'S'
178                       || (symname[3] == '$' && symname[1] == 'B'
179                           && (symname[2] == 'B' || symname[2] == 'E'))))
180                 continue;
181               ms_type = mst_file_text;
182               bufp->symbol_value &= ~0x3; /* clear out permission bits */
183               break;
184
185             case ST_PRI_PROG:
186             case ST_SEC_PROG:
187             case ST_ENTRY:
188             case ST_MILLICODE:
189               symname = bufp->name.n_strx + stringtab;
190               ms_type = mst_file_text;
191               bufp->symbol_value &= ~0x3; /* clear out permission bits */
192               break;
193             case ST_DATA:
194               symname = bufp->name.n_strx + stringtab;
195               ms_type = mst_file_data;
196               break;
197             default:
198               continue;
199             }
200         default:
201           continue;
202         }
203
204       if (bufp->name.n_strx > obj_stringtab_size (abfd))
205         error ("Invalid symbol data; bad HP string table offset: %d",
206                bufp->name.n_strx);
207
208       record_minimal_symbol (symname,
209                              bufp->symbol_value, ms_type, 
210                              objfile);
211     }
212
213   install_minimal_symbols (objfile);
214 }
215
216 /* Read in the backtrace information stored in the `$UNWIND_START$' section of
217    the object file.  This info is used mainly by find_unwind_entry() to find
218    out the stack frame size and frame pointer used by procedures.  We put
219    everything on the psymbol obstack in the objfile so that it automatically
220    gets freed when the objfile is destroyed.  */
221
222 static void
223 read_unwind_info (objfile)
224      struct objfile *objfile;
225 {
226   asection *unwind_sec;
227   struct obj_unwind_info *ui;
228
229   ui = obstack_alloc (&objfile->psymbol_obstack,
230                       sizeof (struct obj_unwind_info));
231
232   ui->table = NULL;
233   ui->cache = NULL;
234   ui->last = -1;
235
236   unwind_sec = bfd_get_section_by_name (objfile->obfd,
237                                         "$UNWIND_START$");
238   if (unwind_sec)
239     {
240       int size;
241       int i, *ip;
242
243       size = bfd_section_size (objfile->obfd, unwind_sec);
244       ui->table = obstack_alloc (&objfile->psymbol_obstack, size);
245       ui->last = size / sizeof (struct unwind_table_entry) - 1;
246
247       bfd_get_section_contents (objfile->obfd, unwind_sec, ui->table,
248                                 0, size);
249
250       OBJ_UNWIND_INFO (objfile) = ui;
251     }
252 }
253
254 /* Scan and build partial symbols for a symbol file.
255    We have been initialized by a call to pa_symfile_init, which 
256    currently does nothing.
257
258    SECTION_OFFSETS is a set of offsets to apply to relocate the symbols
259    in each section.  This is ignored, as it isn't needed for the PA.
260
261    MAINLINE is true if we are reading the main symbol
262    table (as opposed to a shared lib or dynamically loaded file).
263
264    This function only does the minimum work necessary for letting the
265    user "name" things symbolically; it does not read the entire symtab.
266    Instead, it reads the external and static symbols and puts them in partial
267    symbol tables.  When more extensive information is requested of a
268    file, the corresponding partial symbol table is mutated into a full
269    fledged symbol table by going back and reading the symbols
270    for real.
271
272    We look for sections with specific names, to tell us what debug
273    format to look for:  FIXME!!!
274
275    pastab_build_psymtabs() handles STABS symbols.
276
277    Note that PA files have a "minimal" symbol table, which is vaguely
278    reminiscent of a COFF symbol table, but has only the minimal information
279    necessary for linking.  We process this also, and use the information to
280    build gdb's minimal symbol table.  This gives us some minimal debugging
281    capability even for files compiled without -g.  */
282
283 static void
284 pa_symfile_read (objfile, section_offsets, mainline)
285      struct objfile *objfile;
286      struct section_offsets *section_offsets;
287      int mainline;
288 {
289   bfd *abfd = objfile->obfd;
290   struct cleanup *back_to;
291   CORE_ADDR offset;
292
293   init_minimal_symbol_collection ();
294   back_to = make_cleanup (discard_minimal_symbols, 0);
295
296   make_cleanup (free_painfo, (PTR) objfile);
297
298   /* Process the normal PA symbol table first. */
299
300   /* FIXME, should take a section_offsets param, not just an offset.  */
301
302   offset = ANOFFSET (section_offsets, 0);
303   pa_symtab_read (abfd, offset, objfile);
304
305   /* Now process debugging information, which is contained in
306      special PA sections.  */
307
308   pastab_build_psymtabs (objfile, section_offsets, mainline);
309
310   read_unwind_info(objfile);
311
312   do_cleanups (back_to);
313 }
314
315 /* This cleans up the objfile's sym_private pointer, and the chain of
316    stab_section_info's, that might be dangling from it.  */
317
318 static void
319 free_painfo (objp)
320      PTR objp;
321 {
322   struct objfile *objfile = (struct objfile *)objp;
323   struct dbx_symfile_info *dbxinfo = (struct dbx_symfile_info *)
324                                      objfile->sym_private;
325   struct stab_section_info *ssi, *nssi;
326
327   ssi = dbxinfo->stab_section_info;
328   while (ssi)
329     {
330       nssi = ssi->next;
331       mfree (objfile->md, ssi);
332       ssi = nssi;
333     }
334
335   dbxinfo->stab_section_info = 0;       /* Just say No mo info about this.  */
336 }
337
338 /* Initialize anything that needs initializing when a completely new symbol
339    file is specified (not just adding some symbols from another file, e.g. a
340    shared library).
341
342    We reinitialize buildsym, since we may be reading stabs from a PA file.  */
343
344 static void
345 pa_new_init (ignore)
346      struct objfile *ignore;
347 {
348   stabsread_new_init ();
349   buildsym_new_init ();
350 }
351
352 /* Perform any local cleanups required when we are done with a particular
353    objfile.  I.E, we are in the process of discarding all symbol information
354    for an objfile, freeing up all memory held for it, and unlinking the
355    objfile struct from the global list of known objfiles. */
356
357 static void
358 pa_symfile_finish (objfile)
359      struct objfile *objfile;
360 {
361   if (objfile -> sym_private != NULL)
362     {
363       mfree (objfile -> md, objfile -> sym_private);
364     }
365 }
366
367 /* PA specific initialization routine for reading symbols.
368
369    It is passed a pointer to a struct sym_fns which contains, among other
370    things, the BFD for the file whose symbols are being read, and a slot for
371    a pointer to "private data" which we can fill with goodies.
372
373    This routine is almost a complete ripoff of dbx_symfile_init.  The
374    common parts of these routines should be extracted and used instead of
375    duplicating this code.  FIXME. */
376
377 static void
378 pa_symfile_init (objfile)
379      struct objfile *objfile;
380 {
381   int val;
382   bfd *sym_bfd = objfile->obfd;
383   char *name = bfd_get_filename (sym_bfd);
384   asection *stabsect;           /* Section containing symbol table entries */
385   asection *stringsect;         /* Section containing symbol name strings */
386
387   stabsect = bfd_get_section_by_name (sym_bfd, "$GDB_SYMBOLS$");
388   stringsect = bfd_get_section_by_name (sym_bfd, "$GDB_STRINGS$");
389
390   /* Allocate struct to keep track of the symfile */
391   objfile->sym_private = (PTR)
392     xmmalloc (objfile -> md, sizeof (struct dbx_symfile_info));
393
394   memset ((PTR) objfile->sym_private, 0, sizeof (struct dbx_symfile_info));
395
396   if (!stabsect)
397     return;
398
399   if (!stringsect)
400     error ("Found stabs, but not string section");
401
402   /* FIXME POKING INSIDE BFD DATA STRUCTURES */
403 #define STRING_TABLE_OFFSET     (stringsect->filepos)
404 #define SYMBOL_TABLE_OFFSET     (stabsect->filepos)
405
406   /* FIXME POKING INSIDE BFD DATA STRUCTURES */
407
408   DBX_SYMFILE_INFO (objfile)->stab_section_info = NULL;
409   DBX_TEXT_SECT (objfile) = bfd_get_section_by_name (sym_bfd, ".text");
410   if (!DBX_TEXT_SECT (objfile))
411     error ("Can't find .text section in symbol file");
412
413   DBX_SYMBOL_SIZE (objfile) = sizeof (struct internal_nlist);
414   DBX_SYMCOUNT (objfile) = bfd_section_size (sym_bfd, stabsect)
415     / DBX_SYMBOL_SIZE (objfile);
416   DBX_SYMTAB_OFFSET (objfile) = SYMBOL_TABLE_OFFSET;
417
418   /* Read the string table and stash it away in the psymbol_obstack.  It is
419      only needed as long as we need to expand psymbols into full symbols,
420      so when we blow away the psymbol the string table goes away as well.
421      Note that gdb used to use the results of attempting to malloc the
422      string table, based on the size it read, as a form of sanity check
423      for botched byte swapping, on the theory that a byte swapped string
424      table size would be so totally bogus that the malloc would fail.  Now
425      that we put in on the psymbol_obstack, we can't do this since gdb gets
426      a fatal error (out of virtual memory) if the size is bogus.  We can
427      however at least check to see if the size is zero or some negative
428      value. */
429
430   DBX_STRINGTAB_SIZE (objfile) = bfd_section_size (sym_bfd, stringsect);
431
432   if (DBX_SYMCOUNT (objfile) == 0
433       || DBX_STRINGTAB_SIZE (objfile) == 0)
434     return;
435
436   if (DBX_STRINGTAB_SIZE (objfile) <= 0
437       || DBX_STRINGTAB_SIZE (objfile) > bfd_get_size (sym_bfd))
438     error ("ridiculous string table size (%d bytes).",
439            DBX_STRINGTAB_SIZE (objfile));
440
441   DBX_STRINGTAB (objfile) =
442     (char *) obstack_alloc (&objfile -> psymbol_obstack,
443                             DBX_STRINGTAB_SIZE (objfile));
444
445   /* Now read in the string table in one big gulp.  */
446
447   val = bfd_seek (sym_bfd, STRING_TABLE_OFFSET, L_SET);
448   if (val < 0)
449     perror_with_name (name);
450   val = bfd_read (DBX_STRINGTAB (objfile), DBX_STRINGTAB_SIZE (objfile), 1,
451                   sym_bfd);
452   if (val == 0)
453     error ("End of file reading string table");
454   else if (val < 0)
455     /* It's possible bfd_read should be setting bfd_error, and we should be
456        checking that.  But currently it doesn't set bfd_error.  */
457     perror_with_name (name);
458   else if (val != DBX_STRINGTAB_SIZE (objfile))
459     error ("Short read reading string table");
460 }
461
462 /* PA specific parsing routine for section offsets.
463
464    Plain and simple for now.  */
465
466 static struct section_offsets *
467 pa_symfile_offsets (objfile, addr)
468      struct objfile *objfile;
469      CORE_ADDR addr;
470 {
471   struct section_offsets *section_offsets;
472   int i;
473  
474   section_offsets = (struct section_offsets *)
475     obstack_alloc (&objfile -> psymbol_obstack,
476                    sizeof (struct section_offsets) +
477                           sizeof (section_offsets->offsets) * (SECT_OFF_MAX-1));
478
479   for (i = 0; i < SECT_OFF_MAX; i++)
480     ANOFFSET (section_offsets, i) = addr;
481   
482   return section_offsets;
483 }
484 \f
485 /*  Register that we are able to handle PA object file formats. */
486
487 /* This is probably a mistake.  FIXME.  Why can't the HP's use an ordinary
488    file format name with an -hppa suffix?  */
489 static struct sym_fns pa_sym_fns =
490 {
491   "hppa",               /* sym_name: name or name prefix of BFD target type */
492   4,                    /* sym_namelen: number of significant sym_name chars */
493   pa_new_init,          /* sym_new_init: init anything gbl to entire symtab */
494   pa_symfile_init,      /* sym_init: read initial info, setup for sym_read() */
495   pa_symfile_read,      /* sym_read: read a symbol file into symtab */
496   pa_symfile_finish,    /* sym_finish: finished with file, cleanup */
497   pa_symfile_offsets,   /* sym_offsets:  Translate ext. to int. relocation */
498   NULL                  /* next: pointer to next struct sym_fns */
499 };
500
501 void
502 _initialize_paread ()
503 {
504   add_symtab_fns (&pa_sym_fns);
505 }