OSDN Git Service

Updated copyright notices for most files.
[pf3gnuchains/pf3gnuchains3x.git] / gdb / somread.c
1 /* Read HP PA/Risc object files for GDB.
2    Copyright (C) 1991, 1992, 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002,
3    2004, 2007, 2008, 2009 Free Software Foundation, Inc.
4    Written by Fred Fish at Cygnus Support.
5
6    This file is part of GDB.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20
21 #include "defs.h"
22 #include "bfd.h"
23 #include <syms.h>
24 #include "symtab.h"
25 #include "symfile.h"
26 #include "objfiles.h"
27 #include "buildsym.h"
28 #include "stabsread.h"
29 #include "gdb-stabs.h"
30 #include "complaints.h"
31 #include "gdb_string.h"
32 #include "demangle.h"
33 #include "som.h"
34 #include "libhppa.h"
35
36 #include "solib-som.h"
37
38 /*
39
40    LOCAL FUNCTION
41
42    som_symtab_read -- read the symbol table of a SOM file
43
44    SYNOPSIS
45
46    void som_symtab_read (bfd *abfd, struct objfile *objfile,
47    struct section_offsets *section_offsets)
48
49    DESCRIPTION
50
51    Given an open bfd, a base address to relocate symbols to, and a
52    flag that specifies whether or not this bfd is for an executable
53    or not (may be shared library for example), add all the global
54    function and data symbols to the minimal symbol table.
55  */
56
57 static void
58 som_symtab_read (bfd *abfd, struct objfile *objfile,
59                  struct section_offsets *section_offsets)
60 {
61   struct gdbarch *gdbarch = get_objfile_arch (objfile);
62   unsigned int number_of_symbols;
63   int val, dynamic;
64   char *stringtab;
65   asection *shlib_info;
66   struct symbol_dictionary_record *buf, *bufp, *endbufp;
67   char *symname;
68   CONST int symsize = sizeof (struct symbol_dictionary_record);
69   CORE_ADDR text_offset, data_offset;
70
71
72   text_offset = ANOFFSET (section_offsets, 0);
73   data_offset = ANOFFSET (section_offsets, 1);
74
75   number_of_symbols = bfd_get_symcount (abfd);
76
77   /* Allocate a buffer to read in the debug info.
78      We avoid using alloca because the memory size could be so large
79      that we could hit the stack size limit.  */
80   buf = xmalloc (symsize * number_of_symbols);
81   make_cleanup (xfree, buf);
82   bfd_seek (abfd, obj_som_sym_filepos (abfd), SEEK_SET);
83   val = bfd_bread (buf, symsize * number_of_symbols, abfd);
84   if (val != symsize * number_of_symbols)
85     error (_("Couldn't read symbol dictionary!"));
86
87   /* Allocate a buffer to read in the som stringtab section of
88      the debugging info.  Again, we avoid using alloca because
89      the data could be so large that we could potentially hit
90      the stack size limitat.  */
91   stringtab = xmalloc (obj_som_stringtab_size (abfd));
92   make_cleanup (xfree, stringtab);
93   bfd_seek (abfd, obj_som_str_filepos (abfd), SEEK_SET);
94   val = bfd_bread (stringtab, obj_som_stringtab_size (abfd), abfd);
95   if (val != obj_som_stringtab_size (abfd))
96     error (_("Can't read in HP string table."));
97
98   /* We need to determine if objfile is a dynamic executable (so we
99      can do the right thing for ST_ENTRY vs ST_CODE symbols).
100
101      There's nothing in the header which easily allows us to do
102      this.
103
104      This code used to rely upon the existence of a $SHLIB_INFO$
105      section to make this determination.  HP claims that it is
106      more accurate to check for a nonzero text offset, but they
107      have not provided any information about why that test is
108      more accurate.  */
109   dynamic = (text_offset != 0);
110
111   endbufp = buf + number_of_symbols;
112   for (bufp = buf; bufp < endbufp; ++bufp)
113     {
114       enum minimal_symbol_type ms_type;
115
116       QUIT;
117
118       switch (bufp->symbol_scope)
119         {
120         case SS_UNIVERSAL:
121         case SS_EXTERNAL:
122           switch (bufp->symbol_type)
123             {
124             case ST_SYM_EXT:
125             case ST_ARG_EXT:
126               continue;
127
128             case ST_CODE:
129             case ST_PRI_PROG:
130             case ST_SEC_PROG:
131             case ST_MILLICODE:
132               symname = bufp->name.n_strx + stringtab;
133               ms_type = mst_text;
134               bufp->symbol_value += text_offset;
135               bufp->symbol_value = gdbarch_smash_text_address
136                                      (gdbarch, bufp->symbol_value);
137               break;
138
139             case ST_ENTRY:
140               symname = bufp->name.n_strx + stringtab;
141               /* For a dynamic executable, ST_ENTRY symbols are
142                  the stubs, while the ST_CODE symbol is the real
143                  function.  */
144               if (dynamic)
145                 ms_type = mst_solib_trampoline;
146               else
147                 ms_type = mst_text;
148               bufp->symbol_value += text_offset;
149               bufp->symbol_value = gdbarch_smash_text_address
150                                      (gdbarch, bufp->symbol_value);
151               break;
152
153             case ST_STUB:
154               symname = bufp->name.n_strx + stringtab;
155               ms_type = mst_solib_trampoline;
156               bufp->symbol_value += text_offset;
157               bufp->symbol_value = gdbarch_smash_text_address
158                                      (gdbarch, bufp->symbol_value);
159               break;
160
161             case ST_DATA:
162               symname = bufp->name.n_strx + stringtab;
163               bufp->symbol_value += data_offset;
164               ms_type = mst_data;
165               break;
166             default:
167               continue;
168             }
169           break;
170
171 #if 0
172           /* SS_GLOBAL and SS_LOCAL are two names for the same thing (!).  */
173         case SS_GLOBAL:
174 #endif
175         case SS_LOCAL:
176           switch (bufp->symbol_type)
177             {
178             case ST_SYM_EXT:
179             case ST_ARG_EXT:
180               continue;
181
182             case ST_CODE:
183               symname = bufp->name.n_strx + stringtab;
184               ms_type = mst_file_text;
185               bufp->symbol_value += text_offset;
186               bufp->symbol_value = gdbarch_smash_text_address
187                                      (gdbarch, bufp->symbol_value);
188
189             check_strange_names:
190               /* Utah GCC 2.5, FSF GCC 2.6 and later generate correct local
191                  label prefixes for stabs, constant data, etc.  So we need
192                  only filter out L$ symbols which are left in due to
193                  limitations in how GAS generates SOM relocations.
194
195                  When linking in the HPUX C-library the HP linker has
196                  the nasty habit of placing section symbols from the literal
197                  subspaces in the middle of the program's text.  Filter
198                  those out as best we can.  Check for first and last character
199                  being '$'. 
200
201                  And finally, the newer HP compilers emit crud like $PIC_foo$N
202                  in some circumstance (PIC code I guess).  It's also claimed
203                  that they emit D$ symbols too.  What stupidity.  */
204               if ((symname[0] == 'L' && symname[1] == '$')
205               || (symname[0] == '$' && symname[strlen (symname) - 1] == '$')
206                   || (symname[0] == 'D' && symname[1] == '$')
207                   || (strncmp (symname, "L0\001", 3) == 0)
208                   || (strncmp (symname, "$PIC", 4) == 0))
209                 continue;
210               break;
211
212             case ST_PRI_PROG:
213             case ST_SEC_PROG:
214             case ST_MILLICODE:
215               symname = bufp->name.n_strx + stringtab;
216               ms_type = mst_file_text;
217               bufp->symbol_value += text_offset;
218               bufp->symbol_value = gdbarch_smash_text_address
219                                      (gdbarch, bufp->symbol_value);
220               break;
221
222             case ST_ENTRY:
223               symname = bufp->name.n_strx + stringtab;
224               /* SS_LOCAL symbols in a shared library do not have
225                  export stubs, so we do not have to worry about
226                  using mst_file_text vs mst_solib_trampoline here like
227                  we do for SS_UNIVERSAL and SS_EXTERNAL symbols above.  */
228               ms_type = mst_file_text;
229               bufp->symbol_value += text_offset;
230               bufp->symbol_value = gdbarch_smash_text_address
231                                      (gdbarch, bufp->symbol_value);
232               break;
233
234             case ST_STUB:
235               symname = bufp->name.n_strx + stringtab;
236               ms_type = mst_solib_trampoline;
237               bufp->symbol_value += text_offset;
238               bufp->symbol_value = gdbarch_smash_text_address
239                                      (gdbarch, bufp->symbol_value);
240               break;
241
242
243             case ST_DATA:
244               symname = bufp->name.n_strx + stringtab;
245               bufp->symbol_value += data_offset;
246               ms_type = mst_file_data;
247               goto check_strange_names;
248
249             default:
250               continue;
251             }
252           break;
253
254           /* This can happen for common symbols when -E is passed to the
255              final link.  No idea _why_ that would make the linker force
256              common symbols to have an SS_UNSAT scope, but it does.
257
258              This also happens for weak symbols, but their type is
259              ST_DATA.  */
260         case SS_UNSAT:
261           switch (bufp->symbol_type)
262             {
263             case ST_STORAGE:
264             case ST_DATA:
265               symname = bufp->name.n_strx + stringtab;
266               bufp->symbol_value += data_offset;
267               ms_type = mst_data;
268               break;
269
270             default:
271               continue;
272             }
273           break;
274
275         default:
276           continue;
277         }
278
279       if (bufp->name.n_strx > obj_som_stringtab_size (abfd))
280         error (_("Invalid symbol data; bad HP string table offset: %d"),
281                bufp->name.n_strx);
282
283       prim_record_minimal_symbol (symname, bufp->symbol_value, ms_type,
284                                   objfile);
285     }
286 }
287
288 /* Scan and build partial symbols for a symbol file.
289    We have been initialized by a call to som_symfile_init, which 
290    currently does nothing.
291
292    SECTION_OFFSETS is a set of offsets to apply to relocate the symbols
293    in each section.  This is ignored, as it isn't needed for SOM.
294
295    MAINLINE is true if we are reading the main symbol
296    table (as opposed to a shared lib or dynamically loaded file).
297
298    This function only does the minimum work necessary for letting the
299    user "name" things symbolically; it does not read the entire symtab.
300    Instead, it reads the external and static symbols and puts them in partial
301    symbol tables.  When more extensive information is requested of a
302    file, the corresponding partial symbol table is mutated into a full
303    fledged symbol table by going back and reading the symbols
304    for real.
305
306    We look for sections with specific names, to tell us what debug
307    format to look for:  FIXME!!!
308
309    somstab_build_psymtabs() handles STABS symbols.
310
311    Note that SOM files have a "minimal" symbol table, which is vaguely
312    reminiscent of a COFF symbol table, but has only the minimal information
313    necessary for linking.  We process this also, and use the information to
314    build gdb's minimal symbol table.  This gives us some minimal debugging
315    capability even for files compiled without -g.  */
316
317 static void
318 som_symfile_read (struct objfile *objfile, int mainline)
319 {
320   bfd *abfd = objfile->obfd;
321   struct cleanup *back_to;
322
323   init_minimal_symbol_collection ();
324   back_to = make_cleanup_discard_minimal_symbols ();
325
326   /* Process the normal SOM symbol table first. 
327      This reads in the DNTT and string table, but doesn't
328      actually scan the DNTT. It does scan the linker symbol
329      table and thus build up a "minimal symbol table". */
330
331   som_symtab_read (abfd, objfile, objfile->section_offsets);
332
333   /* Install any minimal symbols that have been collected as the current
334      minimal symbols for this objfile. 
335      Further symbol-reading is done incrementally, file-by-file,
336      in a step known as "psymtab-to-symtab" expansion. hp-symtab-read.c
337      contains the code to do the actual DNTT scanning and symtab building. */
338   install_minimal_symbols (objfile);
339   do_cleanups (back_to);
340
341   /* Now read information from the stabs debug sections.
342      This is emitted by gcc.  */
343   stabsect_build_psymtabs (objfile, mainline,
344                            "$GDB_SYMBOLS$", "$GDB_STRINGS$", "$TEXT$");
345 }
346
347 /* Initialize anything that needs initializing when a completely new symbol
348    file is specified (not just adding some symbols from another file, e.g. a
349    shared library).
350
351    We reinitialize buildsym, since we may be reading stabs from a SOM file.  */
352
353 static void
354 som_new_init (struct objfile *ignore)
355 {
356   stabsread_new_init ();
357   buildsym_new_init ();
358 }
359
360 /* Perform any local cleanups required when we are done with a particular
361    objfile.  I.E, we are in the process of discarding all symbol information
362    for an objfile, freeing up all memory held for it, and unlinking the
363    objfile struct from the global list of known objfiles. */
364
365 static void
366 som_symfile_finish (struct objfile *objfile)
367 {
368   if (objfile->deprecated_sym_stab_info != NULL)
369     {
370       xfree (objfile->deprecated_sym_stab_info);
371     }
372 }
373
374 /* SOM specific initialization routine for reading symbols.  */
375
376 static void
377 som_symfile_init (struct objfile *objfile)
378 {
379   /* SOM objects may be reordered, so set OBJF_REORDERED.  If we
380      find this causes a significant slowdown in gdb then we could
381      set it in the debug symbol readers only when necessary.  */
382   objfile->flags |= OBJF_REORDERED;
383 }
384
385 /* SOM specific parsing routine for section offsets.
386
387    Plain and simple for now.  */
388
389 static void
390 som_symfile_offsets (struct objfile *objfile, struct section_addr_info *addrs)
391 {
392   int i;
393   CORE_ADDR text_addr;
394
395   objfile->num_sections = bfd_count_sections (objfile->obfd);
396   objfile->section_offsets = (struct section_offsets *)
397     obstack_alloc (&objfile->objfile_obstack, 
398                    SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));
399
400   /* FIXME: ezannoni 2000-04-20 The section names in SOM are not
401      .text, .data, etc, but $TEXT$, $DATA$,... We should initialize
402      SET_OFF_* from bfd. (See default_symfile_offsets()). But I don't
403      know the correspondence between SOM sections and GDB's idea of
404      section names. So for now we default to what is was before these
405      changes.*/
406   objfile->sect_index_text = 0;
407   objfile->sect_index_data = 1;
408   objfile->sect_index_bss = 2;
409   objfile->sect_index_rodata = 3;
410
411   /* First see if we're a shared library.  If so, get the section
412      offsets from the library, else get them from addrs.  */
413   if (!som_solib_section_offsets (objfile, objfile->section_offsets))
414     {
415       /* Note: Here is OK to compare with ".text" because this is the
416          name that gdb itself gives to that section, not the SOM
417          name. */
418       for (i = 0; i < addrs->num_sections && addrs->other[i].name; i++)
419         if (strcmp (addrs->other[i].name, ".text") == 0)
420           break;
421       text_addr = addrs->other[i].addr;
422
423       for (i = 0; i < objfile->num_sections; i++)
424         (objfile->section_offsets)->offsets[i] = text_addr;
425     }
426 }
427 \f
428
429
430 /* Register that we are able to handle SOM object file formats.  */
431
432 static struct sym_fns som_sym_fns =
433 {
434   bfd_target_som_flavour,
435   som_new_init,                 /* sym_new_init: init anything gbl to entire symtab */
436   som_symfile_init,             /* sym_init: read initial info, setup for sym_read() */
437   som_symfile_read,             /* sym_read: read a symbol file into symtab */
438   som_symfile_finish,           /* sym_finish: finished with file, cleanup */
439   som_symfile_offsets,          /* sym_offsets:  Translate ext. to int. relocation */
440   default_symfile_segments,     /* sym_segments: Get segment information from
441                                    a file.  */
442   NULL,                         /* sym_read_linetable */
443   NULL                          /* next: pointer to next struct sym_fns */
444 };
445
446 void
447 _initialize_somread (void)
448 {
449   add_symtab_fns (&som_sym_fns);
450 }