* somread.c (som_symfile_read): Force unwinds to be re-read after
[external/binutils.git] / gdb / somread.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 "som.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 "stabsread.h"
31 #include "gdb-stabs.h"
32 #include "complaints.h"
33 #include <string.h>
34 #include "demangle.h"
35 #include <sys/file.h>
36
37 /* Various things we might complain about... */
38
39 static void
40 som_symfile_init PARAMS ((struct objfile *));
41
42 static void
43 som_new_init PARAMS ((struct objfile *));
44
45 static void
46 som_symfile_read PARAMS ((struct objfile *, struct section_offsets *, int));
47
48 static void
49 som_symfile_finish PARAMS ((struct objfile *));
50
51 static void
52 som_symtab_read PARAMS ((bfd *,  CORE_ADDR, struct objfile *));
53
54 static struct section_offsets *
55 som_symfile_offsets PARAMS ((struct objfile *, CORE_ADDR));
56
57 static void
58 record_minimal_symbol PARAMS ((char *, CORE_ADDR,
59                                enum minimal_symbol_type,
60                                struct objfile *));
61
62 static void
63 record_minimal_symbol (name, address, ms_type, objfile)
64      char *name;
65      CORE_ADDR address;
66      enum minimal_symbol_type ms_type;
67      struct objfile *objfile;
68 {
69   name = obsavestring (name, strlen (name), &objfile -> symbol_obstack);
70   prim_record_minimal_symbol (name, address, ms_type, objfile);
71 }
72
73 /*
74
75 LOCAL FUNCTION
76
77         som_symtab_read -- read the symbol table of a SOM file
78
79 SYNOPSIS
80
81         void som_symtab_read (bfd *abfd, CORE_ADDR addr,
82                               struct objfile *objfile)
83
84 DESCRIPTION
85
86         Given an open bfd, a base address to relocate symbols to, and a
87         flag that specifies whether or not this bfd is for an executable
88         or not (may be shared library for example), add all the global
89         function and data symbols to the minimal symbol table.
90 */
91
92 static void
93 som_symtab_read (abfd, addr, objfile)
94      bfd *abfd;
95      CORE_ADDR addr;
96      struct objfile *objfile;
97 {
98   unsigned int number_of_symbols;
99   int val, dynamic;
100   char *stringtab;
101   asection *shlib_info;
102   struct symbol_dictionary_record *buf, *bufp, *endbufp;
103   char *symname;
104   CONST int symsize = sizeof (struct symbol_dictionary_record);
105
106   number_of_symbols = bfd_get_symcount (abfd);
107
108   buf = alloca (symsize * number_of_symbols);
109   bfd_seek (abfd, obj_som_sym_filepos (abfd), L_SET);
110   val = bfd_read (buf, symsize * number_of_symbols, 1, abfd);
111   if (val != symsize * number_of_symbols)
112     error ("Couldn't read symbol dictionary!");
113
114   stringtab = alloca (obj_som_stringtab_size (abfd));
115   bfd_seek (abfd, obj_som_str_filepos (abfd), L_SET);
116   val = bfd_read (stringtab, obj_som_stringtab_size (abfd), 1, abfd);
117   if (val != obj_som_stringtab_size (abfd))
118     error ("Can't read in HP string table.");
119
120   /* We need to determine if objfile is a dynamic executable (so we
121      can do the right thing for ST_ENTRY vs ST_CODE symbols).
122
123      There's nothing in the header which easily allows us to do
124      this.  The only reliable way I know of is to check for the
125      existance of a $SHLIB_INFO$ section with a non-zero size.  */
126   shlib_info = bfd_get_section_by_name (objfile->obfd, "$SHLIB_INFO$");
127   if (shlib_info)
128     dynamic = (bfd_section_size (objfile->obfd, shlib_info) != 0);
129   else
130     dynamic = 0;
131
132   endbufp = buf + number_of_symbols;
133   for (bufp = buf; bufp < endbufp; ++bufp)
134     {
135       enum minimal_symbol_type ms_type;
136
137       QUIT;
138
139       switch (bufp->symbol_scope)
140         {
141         case SS_UNIVERSAL:
142         case SS_EXTERNAL:
143           switch (bufp->symbol_type)
144             {
145             case ST_SYM_EXT:
146             case ST_ARG_EXT:
147               continue;
148
149             case ST_CODE:
150             case ST_PRI_PROG:
151             case ST_SEC_PROG:
152             case ST_MILLICODE:
153               symname = bufp->name.n_strx + stringtab;
154               ms_type = mst_text;
155 #ifdef SMASH_TEXT_ADDRESS
156               SMASH_TEXT_ADDRESS (bufp->symbol_value);
157 #endif
158               break;
159
160             case ST_ENTRY:
161               symname = bufp->name.n_strx + stringtab;
162               /* For a dynamic executable, ST_ENTRY symbols are
163                  the stubs, while the ST_CODE symbol is the real
164                  function.  */
165               if (dynamic)
166                 ms_type = mst_solib_trampoline;
167               else
168                 ms_type = mst_text;
169 #ifdef SMASH_TEXT_ADDRESS
170               SMASH_TEXT_ADDRESS (bufp->symbol_value);
171 #endif
172               break;
173
174             case ST_STUB:
175               symname = bufp->name.n_strx + stringtab;
176               ms_type = mst_solib_trampoline;
177 #ifdef SMASH_TEXT_ADDRESS
178               SMASH_TEXT_ADDRESS (bufp->symbol_value);
179 #endif
180               break;
181
182             case ST_DATA:
183               symname = bufp->name.n_strx + stringtab;
184               ms_type = mst_data;
185               break;
186             default:
187               continue;
188             }
189           break;
190
191 #if 0
192           /* SS_GLOBAL and SS_LOCAL are two names for the same thing (!).  */
193         case SS_GLOBAL:
194 #endif
195         case SS_LOCAL:
196           switch (bufp->symbol_type)
197             {
198             case ST_SYM_EXT:
199             case ST_ARG_EXT:
200               continue;
201
202             case ST_CODE:
203               symname = bufp->name.n_strx + stringtab;
204               ms_type = mst_file_text;
205 #ifdef SMASH_TEXT_ADDRESS
206               SMASH_TEXT_ADDRESS (bufp->symbol_value);
207 #endif
208
209             check_strange_names:
210               /* Utah GCC 2.5, FSF GCC 2.6 and later generate correct local
211                  label prefixes for stabs, constant data, etc.  So we need
212                  only filter out L$ symbols which are left in due to
213                  limitations in how GAS generates SOM relocations.
214
215                  When linking in the HPUX C-library the HP linker has
216                  the nasty habit of placing section symbols from the literal
217                  subspaces in the middle of the program's text.  Filter
218                  those out as best we can.  Check for first and last character
219                  being '$'.  */
220               if ((symname[0] == 'L' && symname[1] == '$')
221                   || (symname[0] == '$' && symname[strlen(symname) - 1] == '$'))
222                 continue;
223               break;
224
225             case ST_PRI_PROG:
226             case ST_SEC_PROG:
227             case ST_MILLICODE:
228               symname = bufp->name.n_strx + stringtab;
229               ms_type = mst_file_text;
230 #ifdef SMASH_TEXT_ADDRESS
231               SMASH_TEXT_ADDRESS (bufp->symbol_value);
232 #endif
233               break;
234
235             case ST_ENTRY:
236               symname = bufp->name.n_strx + stringtab;
237               /* For a dynamic executable, ST_ENTRY symbols are
238                  the stubs, while the ST_CODE symbol is the real
239                  function.  */
240               if (dynamic)
241                 ms_type = mst_solib_trampoline;
242               else
243                 ms_type = mst_file_text;
244 #ifdef SMASH_TEXT_ADDRESS
245               SMASH_TEXT_ADDRESS (bufp->symbol_value);
246 #endif
247               break;
248
249             case ST_STUB:
250               symname = bufp->name.n_strx + stringtab;
251               ms_type = mst_solib_trampoline;
252 #ifdef SMASH_TEXT_ADDRESS
253               SMASH_TEXT_ADDRESS (bufp->symbol_value);
254 #endif
255               break;
256
257
258             case ST_DATA:
259               symname = bufp->name.n_strx + stringtab;
260               ms_type = mst_file_data;
261               goto check_strange_names;
262
263             default:
264               continue;
265             }
266           break;
267
268         default:
269           continue;
270         }
271
272       if (bufp->name.n_strx > obj_som_stringtab_size (abfd))
273         error ("Invalid symbol data; bad HP string table offset: %d",
274                bufp->name.n_strx);
275
276       record_minimal_symbol (symname,
277                              bufp->symbol_value, ms_type, 
278                              objfile);
279     }
280 }
281
282 /* Scan and build partial symbols for a symbol file.
283    We have been initialized by a call to som_symfile_init, which 
284    currently does nothing.
285
286    SECTION_OFFSETS is a set of offsets to apply to relocate the symbols
287    in each section.  This is ignored, as it isn't needed for SOM.
288
289    MAINLINE is true if we are reading the main symbol
290    table (as opposed to a shared lib or dynamically loaded file).
291
292    This function only does the minimum work necessary for letting the
293    user "name" things symbolically; it does not read the entire symtab.
294    Instead, it reads the external and static symbols and puts them in partial
295    symbol tables.  When more extensive information is requested of a
296    file, the corresponding partial symbol table is mutated into a full
297    fledged symbol table by going back and reading the symbols
298    for real.
299
300    We look for sections with specific names, to tell us what debug
301    format to look for:  FIXME!!!
302
303    somstab_build_psymtabs() handles STABS symbols.
304
305    Note that SOM files have a "minimal" symbol table, which is vaguely
306    reminiscent of a COFF symbol table, but has only the minimal information
307    necessary for linking.  We process this also, and use the information to
308    build gdb's minimal symbol table.  This gives us some minimal debugging
309    capability even for files compiled without -g.  */
310
311 static void
312 som_symfile_read (objfile, section_offsets, mainline)
313      struct objfile *objfile;
314      struct section_offsets *section_offsets;
315      int mainline;
316 {
317   bfd *abfd = objfile->obfd;
318   struct cleanup *back_to;
319   CORE_ADDR offset;
320
321   init_minimal_symbol_collection ();
322   back_to = make_cleanup (discard_minimal_symbols, 0);
323
324   /* FIXME, should take a section_offsets param, not just an offset.  */
325
326   offset = ANOFFSET (section_offsets, 0);
327
328   /* Process the normal SOM symbol table first. */
329
330   som_symtab_read (abfd, offset, objfile);
331
332   /* Now read information from the stabs debug sections.  */
333   stabsect_build_psymtabs (objfile, section_offsets, mainline,
334                            "$GDB_SYMBOLS$", "$GDB_STRINGS$", "$TEXT$");
335
336 /* start-sanitize-hpread */
337   /* Now read the native debug information.  */
338   hpread_build_psymtabs (objfile, section_offsets, mainline);
339 /* end-sanitize-hpread */
340
341   /* Install any minimal symbols that have been collected as the current
342      minimal symbols for this objfile.  */
343   install_minimal_symbols (objfile);
344
345   /* Force hppa-tdep.c to re-read the unwind descriptors.  */
346   OBJ_UNWIND_INFO (objfile) = NULL;
347   do_cleanups (back_to);
348 }
349
350 /* Initialize anything that needs initializing when a completely new symbol
351    file is specified (not just adding some symbols from another file, e.g. a
352    shared library).
353
354    We reinitialize buildsym, since we may be reading stabs from a SOM file.  */
355
356 static void
357 som_new_init (ignore)
358      struct objfile *ignore;
359 {
360   stabsread_new_init ();
361   buildsym_new_init ();
362 }
363
364 /* Perform any local cleanups required when we are done with a particular
365    objfile.  I.E, we are in the process of discarding all symbol information
366    for an objfile, freeing up all memory held for it, and unlinking the
367    objfile struct from the global list of known objfiles. */
368
369 static void
370 som_symfile_finish (objfile)
371      struct objfile *objfile;
372 {
373   if (objfile -> sym_stab_info != NULL)
374     {
375       mfree (objfile -> md, objfile -> sym_stab_info);
376     }
377 /* start-sanitize-hpread */
378   hpread_symfile_finish (objfile);
379 /* end-sanitize-hpread */
380 }
381
382 /* SOM specific initialization routine for reading symbols.
383
384    Nothing SOM specific left to do anymore.  */
385 static void
386 som_symfile_init (objfile)
387      struct objfile *objfile;
388 {
389 /* start-sanitize-hpread */
390   hpread_symfile_init (objfile);
391 /* end-sanitize-hpread */
392 }
393
394 /* SOM specific parsing routine for section offsets.
395
396    Plain and simple for now.  */
397
398 static struct section_offsets *
399 som_symfile_offsets (objfile, addr)
400      struct objfile *objfile;
401      CORE_ADDR addr;
402 {
403   struct section_offsets *section_offsets;
404   int i;
405
406   objfile->num_sections = SECT_OFF_MAX;
407   section_offsets = (struct section_offsets *)
408     obstack_alloc (&objfile -> psymbol_obstack,
409                    sizeof (struct section_offsets)
410                    + sizeof (section_offsets->offsets) * (SECT_OFF_MAX-1));
411
412   for (i = 0; i < SECT_OFF_MAX; i++)
413     ANOFFSET (section_offsets, i) = addr;
414
415   return section_offsets;
416 }
417 \f
418 /* Register that we are able to handle SOM object file formats.  */
419
420 static struct sym_fns som_sym_fns =
421 {
422   bfd_target_som_flavour,
423   som_new_init,         /* sym_new_init: init anything gbl to entire symtab */
424   som_symfile_init,     /* sym_init: read initial info, setup for sym_read() */
425   som_symfile_read,     /* sym_read: read a symbol file into symtab */
426   som_symfile_finish,   /* sym_finish: finished with file, cleanup */
427   som_symfile_offsets,  /* sym_offsets:  Translate ext. to int. relocation */
428   NULL                  /* next: pointer to next struct sym_fns */
429 };
430
431 void
432 _initialize_somread ()
433 {
434   add_symtab_fns (&som_sym_fns);
435 }