OSDN Git Service

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