OSDN Git Service

* objfiles.h (ImportEntry, ExportEntry): Remove types.
[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 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 2 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, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor,
21    Boston, MA 02110-1301, USA.  */
22
23 #include "defs.h"
24 #include "bfd.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 "gdb_string.h"
34 #include "demangle.h"
35 #include "som.h"
36 #include "libhppa.h"
37
38 #include "solib-som.h"
39
40 /*
41
42    LOCAL FUNCTION
43
44    som_symtab_read -- read the symbol table of a SOM file
45
46    SYNOPSIS
47
48    void som_symtab_read (bfd *abfd, struct objfile *objfile,
49    struct section_offsets *section_offsets)
50
51    DESCRIPTION
52
53    Given an open bfd, a base address to relocate symbols to, and a
54    flag that specifies whether or not this bfd is for an executable
55    or not (may be shared library for example), add all the global
56    function and data symbols to the minimal symbol table.
57  */
58
59 static void
60 som_symtab_read (bfd *abfd, struct objfile *objfile,
61                  struct section_offsets *section_offsets)
62 {
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 = SMASH_TEXT_ADDRESS (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 = SMASH_TEXT_ADDRESS (bufp->symbol_value);
150               break;
151
152             case ST_STUB:
153               symname = bufp->name.n_strx + stringtab;
154               ms_type = mst_solib_trampoline;
155               bufp->symbol_value += text_offset;
156               bufp->symbol_value = SMASH_TEXT_ADDRESS (bufp->symbol_value);
157               break;
158
159             case ST_DATA:
160               symname = bufp->name.n_strx + stringtab;
161               bufp->symbol_value += data_offset;
162               ms_type = mst_data;
163               break;
164             default:
165               continue;
166             }
167           break;
168
169 #if 0
170           /* SS_GLOBAL and SS_LOCAL are two names for the same thing (!).  */
171         case SS_GLOBAL:
172 #endif
173         case SS_LOCAL:
174           switch (bufp->symbol_type)
175             {
176             case ST_SYM_EXT:
177             case ST_ARG_EXT:
178               continue;
179
180             case ST_CODE:
181               symname = bufp->name.n_strx + stringtab;
182               ms_type = mst_file_text;
183               bufp->symbol_value += text_offset;
184               bufp->symbol_value = SMASH_TEXT_ADDRESS (bufp->symbol_value);
185
186             check_strange_names:
187               /* Utah GCC 2.5, FSF GCC 2.6 and later generate correct local
188                  label prefixes for stabs, constant data, etc.  So we need
189                  only filter out L$ symbols which are left in due to
190                  limitations in how GAS generates SOM relocations.
191
192                  When linking in the HPUX C-library the HP linker has
193                  the nasty habit of placing section symbols from the literal
194                  subspaces in the middle of the program's text.  Filter
195                  those out as best we can.  Check for first and last character
196                  being '$'. 
197
198                  And finally, the newer HP compilers emit crud like $PIC_foo$N
199                  in some circumstance (PIC code I guess).  It's also claimed
200                  that they emit D$ symbols too.  What stupidity.  */
201               if ((symname[0] == 'L' && symname[1] == '$')
202               || (symname[0] == '$' && symname[strlen (symname) - 1] == '$')
203                   || (symname[0] == 'D' && symname[1] == '$')
204                   || (strncmp (symname, "L0\001", 3) == 0)
205                   || (strncmp (symname, "$PIC", 4) == 0))
206                 continue;
207               break;
208
209             case ST_PRI_PROG:
210             case ST_SEC_PROG:
211             case ST_MILLICODE:
212               symname = bufp->name.n_strx + stringtab;
213               ms_type = mst_file_text;
214               bufp->symbol_value += text_offset;
215               bufp->symbol_value = SMASH_TEXT_ADDRESS (bufp->symbol_value);
216               break;
217
218             case ST_ENTRY:
219               symname = bufp->name.n_strx + stringtab;
220               /* SS_LOCAL symbols in a shared library do not have
221                  export stubs, so we do not have to worry about
222                  using mst_file_text vs mst_solib_trampoline here like
223                  we do for SS_UNIVERSAL and SS_EXTERNAL symbols above.  */
224               ms_type = mst_file_text;
225               bufp->symbol_value += text_offset;
226               bufp->symbol_value = SMASH_TEXT_ADDRESS (bufp->symbol_value);
227               break;
228
229             case ST_STUB:
230               symname = bufp->name.n_strx + stringtab;
231               ms_type = mst_solib_trampoline;
232               bufp->symbol_value += text_offset;
233               bufp->symbol_value = SMASH_TEXT_ADDRESS (bufp->symbol_value);
234               break;
235
236
237             case ST_DATA:
238               symname = bufp->name.n_strx + stringtab;
239               bufp->symbol_value += data_offset;
240               ms_type = mst_file_data;
241               goto check_strange_names;
242
243             default:
244               continue;
245             }
246           break;
247
248           /* This can happen for common symbols when -E is passed to the
249              final link.  No idea _why_ that would make the linker force
250              common symbols to have an SS_UNSAT scope, but it does.
251
252              This also happens for weak symbols, but their type is
253              ST_DATA.  */
254         case SS_UNSAT:
255           switch (bufp->symbol_type)
256             {
257             case ST_STORAGE:
258             case ST_DATA:
259               symname = bufp->name.n_strx + stringtab;
260               bufp->symbol_value += data_offset;
261               ms_type = mst_data;
262               break;
263
264             default:
265               continue;
266             }
267           break;
268
269         default:
270           continue;
271         }
272
273       if (bufp->name.n_strx > obj_som_stringtab_size (abfd))
274         error (_("Invalid symbol data; bad HP string table offset: %d"),
275                bufp->name.n_strx);
276
277       prim_record_minimal_symbol (symname, 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 (struct objfile *objfile, int mainline)
313 {
314   bfd *abfd = objfile->obfd;
315   struct cleanup *back_to;
316
317   init_minimal_symbol_collection ();
318   back_to = make_cleanup_discard_minimal_symbols ();
319
320   /* Process the normal SOM symbol table first. 
321      This reads in the DNTT and string table, but doesn't
322      actually scan the DNTT. It does scan the linker symbol
323      table and thus build up a "minimal symbol table". */
324
325   som_symtab_read (abfd, objfile, objfile->section_offsets);
326
327   /* Install any minimal symbols that have been collected as the current
328      minimal symbols for this objfile. 
329      Further symbol-reading is done incrementally, file-by-file,
330      in a step known as "psymtab-to-symtab" expansion. hp-symtab-read.c
331      contains the code to do the actual DNTT scanning and symtab building. */
332   install_minimal_symbols (objfile);
333   do_cleanups (back_to);
334
335   /* Now read information from the stabs debug sections.
336      This is emitted by gcc.  */
337   stabsect_build_psymtabs (objfile, mainline,
338                            "$GDB_SYMBOLS$", "$GDB_STRINGS$", "$TEXT$");
339 }
340
341 /* Initialize anything that needs initializing when a completely new symbol
342    file is specified (not just adding some symbols from another file, e.g. a
343    shared library).
344
345    We reinitialize buildsym, since we may be reading stabs from a SOM file.  */
346
347 static void
348 som_new_init (struct objfile *ignore)
349 {
350   stabsread_new_init ();
351   buildsym_new_init ();
352 }
353
354 /* Perform any local cleanups required when we are done with a particular
355    objfile.  I.E, we are in the process of discarding all symbol information
356    for an objfile, freeing up all memory held for it, and unlinking the
357    objfile struct from the global list of known objfiles. */
358
359 static void
360 som_symfile_finish (struct objfile *objfile)
361 {
362   if (objfile->deprecated_sym_stab_info != NULL)
363     {
364       xfree (objfile->deprecated_sym_stab_info);
365     }
366 }
367
368 /* SOM specific initialization routine for reading symbols.  */
369
370 static void
371 som_symfile_init (struct objfile *objfile)
372 {
373   /* SOM objects may be reordered, so set OBJF_REORDERED.  If we
374      find this causes a significant slowdown in gdb then we could
375      set it in the debug symbol readers only when necessary.  */
376   objfile->flags |= OBJF_REORDERED;
377 }
378
379 /* SOM specific parsing routine for section offsets.
380
381    Plain and simple for now.  */
382
383 static void
384 som_symfile_offsets (struct objfile *objfile, struct section_addr_info *addrs)
385 {
386   int i;
387   CORE_ADDR text_addr;
388
389   objfile->num_sections = bfd_count_sections (objfile->obfd);
390   objfile->section_offsets = (struct section_offsets *)
391     obstack_alloc (&objfile->objfile_obstack, 
392                    SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));
393
394   /* FIXME: ezannoni 2000-04-20 The section names in SOM are not
395      .text, .data, etc, but $TEXT$, $DATA$,... We should initialize
396      SET_OFF_* from bfd. (See default_symfile_offsets()). But I don't
397      know the correspondence between SOM sections and GDB's idea of
398      section names. So for now we default to what is was before these
399      changes.*/
400   objfile->sect_index_text = 0;
401   objfile->sect_index_data = 1;
402   objfile->sect_index_bss = 2;
403   objfile->sect_index_rodata = 3;
404
405   /* First see if we're a shared library.  If so, get the section
406      offsets from the library, else get them from addrs.  */
407   if (!som_solib_section_offsets (objfile, objfile->section_offsets))
408     {
409       /* Note: Here is OK to compare with ".text" because this is the
410          name that gdb itself gives to that section, not the SOM
411          name. */
412       for (i = 0; i < addrs->num_sections && addrs->other[i].name; i++)
413         if (strcmp (addrs->other[i].name, ".text") == 0)
414           break;
415       text_addr = addrs->other[i].addr;
416
417       for (i = 0; i < objfile->num_sections; i++)
418         (objfile->section_offsets)->offsets[i] = text_addr;
419     }
420 }
421 \f
422
423
424 /* Register that we are able to handle SOM object file formats.  */
425
426 static struct sym_fns som_sym_fns =
427 {
428   bfd_target_som_flavour,
429   som_new_init,                 /* sym_new_init: init anything gbl to entire symtab */
430   som_symfile_init,             /* sym_init: read initial info, setup for sym_read() */
431   som_symfile_read,             /* sym_read: read a symbol file into symtab */
432   som_symfile_finish,           /* sym_finish: finished with file, cleanup */
433   som_symfile_offsets,          /* sym_offsets:  Translate ext. to int. relocation */
434   NULL                          /* next: pointer to next struct sym_fns */
435 };
436
437 void
438 _initialize_somread (void)
439 {
440   add_symtab_fns (&som_sym_fns);
441 }