implement support for "enum class"
[platform/upstream/binutils.git] / gdb / somread.c
1 /* Read HP PA/Risc object files for GDB.
2    Copyright (C) 1991-2014 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 3 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, see <http://www.gnu.org/licenses/>.  */
19
20 #include "defs.h"
21 #include "bfd.h"
22 #include "som/aout.h"
23 #include "symtab.h"
24 #include "symfile.h"
25 #include "objfiles.h"
26 #include "buildsym.h"
27 #include "stabsread.h"
28 #include "gdb-stabs.h"
29 #include "complaints.h"
30 #include <string.h>
31 #include "demangle.h"
32 #include "som.h"
33 #include "libhppa.h"
34 #include "psymtab.h"
35
36 #include "solib-som.h"
37
38 /* Read the symbol table of a SOM file.
39
40    Given an open bfd, a base address to relocate symbols to, and a
41    flag that specifies whether or not this bfd is for an executable
42    or not (may be shared library for example), add all the global
43    function and data symbols to the minimal symbol table.  */
44
45 static void
46 som_symtab_read (bfd *abfd, struct objfile *objfile,
47                  struct section_offsets *section_offsets)
48 {
49   struct cleanup *cleanup;
50   struct gdbarch *gdbarch = get_objfile_arch (objfile);
51   unsigned int number_of_symbols;
52   int val, dynamic;
53   char *stringtab;
54   asection *shlib_info;
55   struct som_external_symbol_dictionary_record *buf, *bufp, *endbufp;
56   char *symname;
57   const int symsize = sizeof (struct som_external_symbol_dictionary_record);
58
59
60   number_of_symbols = bfd_get_symcount (abfd);
61
62   /* Allocate a buffer to read in the debug info.
63      We avoid using alloca because the memory size could be so large
64      that we could hit the stack size limit.  */
65   buf = xmalloc (symsize * number_of_symbols);
66   cleanup = make_cleanup (xfree, buf);
67   bfd_seek (abfd, obj_som_sym_filepos (abfd), SEEK_SET);
68   val = bfd_bread (buf, symsize * number_of_symbols, abfd);
69   if (val != symsize * number_of_symbols)
70     error (_("Couldn't read symbol dictionary!"));
71
72   /* Allocate a buffer to read in the som stringtab section of
73      the debugging info.  Again, we avoid using alloca because
74      the data could be so large that we could potentially hit
75      the stack size limitat.  */
76   stringtab = xmalloc (obj_som_stringtab_size (abfd));
77   make_cleanup (xfree, stringtab);
78   bfd_seek (abfd, obj_som_str_filepos (abfd), SEEK_SET);
79   val = bfd_bread (stringtab, obj_som_stringtab_size (abfd), abfd);
80   if (val != obj_som_stringtab_size (abfd))
81     error (_("Can't read in HP string table."));
82
83   /* We need to determine if objfile is a dynamic executable (so we
84      can do the right thing for ST_ENTRY vs ST_CODE symbols).
85
86      There's nothing in the header which easily allows us to do
87      this.
88
89      This code used to rely upon the existence of a $SHLIB_INFO$
90      section to make this determination.  HP claims that it is
91      more accurate to check for a nonzero text offset, but they
92      have not provided any information about why that test is
93      more accurate.  */
94   dynamic = (ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile)) != 0);
95
96   endbufp = buf + number_of_symbols;
97   for (bufp = buf; bufp < endbufp; ++bufp)
98     {
99       enum minimal_symbol_type ms_type;
100       unsigned int flags = bfd_getb32 (bufp->flags);
101       unsigned int symbol_type
102         = (flags >> SOM_SYMBOL_TYPE_SH) & SOM_SYMBOL_TYPE_MASK;
103       unsigned int symbol_scope
104         = (flags >> SOM_SYMBOL_SCOPE_SH) & SOM_SYMBOL_SCOPE_MASK;
105       CORE_ADDR symbol_value = bfd_getb32 (bufp->symbol_value);
106       asection *section = NULL;
107
108       QUIT;
109
110       /* Compute the section.  */
111       switch (symbol_scope)
112         {
113         case SS_EXTERNAL:
114           if (symbol_type != ST_STORAGE)
115             section = bfd_und_section_ptr;
116           else
117             section = bfd_com_section_ptr;
118           break;
119
120         case SS_UNSAT:
121           if (symbol_type != ST_STORAGE)
122             section = bfd_und_section_ptr;
123           else
124             section = bfd_com_section_ptr;
125           break;
126
127         case SS_UNIVERSAL:
128           section = bfd_section_from_som_symbol (abfd, bufp);
129           break;
130
131         case SS_LOCAL:
132           section = bfd_section_from_som_symbol (abfd, bufp);
133           break;
134         }
135
136       switch (symbol_scope)
137         {
138         case SS_UNIVERSAL:
139         case SS_EXTERNAL:
140           switch (symbol_type)
141             {
142             case ST_SYM_EXT:
143             case ST_ARG_EXT:
144               continue;
145
146             case ST_CODE:
147             case ST_PRI_PROG:
148             case ST_SEC_PROG:
149             case ST_MILLICODE:
150               symname = bfd_getb32 (bufp->name) + stringtab;
151               ms_type = mst_text;
152               symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
153               break;
154
155             case ST_ENTRY:
156               symname = bfd_getb32 (bufp->name) + stringtab;
157               /* For a dynamic executable, ST_ENTRY symbols are
158                  the stubs, while the ST_CODE symbol is the real
159                  function.  */
160               if (dynamic)
161                 ms_type = mst_solib_trampoline;
162               else
163                 ms_type = mst_text;
164               symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
165               break;
166
167             case ST_STUB:
168               symname = bfd_getb32 (bufp->name) + stringtab;
169               ms_type = mst_solib_trampoline;
170               symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
171               break;
172
173             case ST_DATA:
174               symname = bfd_getb32 (bufp->name) + stringtab;
175               ms_type = mst_data;
176               break;
177             default:
178               continue;
179             }
180           break;
181
182 #if 0
183           /* SS_GLOBAL and SS_LOCAL are two names for the same thing (!).  */
184         case SS_GLOBAL:
185 #endif
186         case SS_LOCAL:
187           switch (symbol_type)
188             {
189             case ST_SYM_EXT:
190             case ST_ARG_EXT:
191               continue;
192
193             case ST_CODE:
194               symname = bfd_getb32 (bufp->name) + stringtab;
195               ms_type = mst_file_text;
196               symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
197
198             check_strange_names:
199               /* Utah GCC 2.5, FSF GCC 2.6 and later generate correct local
200                  label prefixes for stabs, constant data, etc.  So we need
201                  only filter out L$ symbols which are left in due to
202                  limitations in how GAS generates SOM relocations.
203
204                  When linking in the HPUX C-library the HP linker has
205                  the nasty habit of placing section symbols from the literal
206                  subspaces in the middle of the program's text.  Filter
207                  those out as best we can.  Check for first and last character
208                  being '$'.
209
210                  And finally, the newer HP compilers emit crud like $PIC_foo$N
211                  in some circumstance (PIC code I guess).  It's also claimed
212                  that they emit D$ symbols too.  What stupidity.  */
213               if ((symname[0] == 'L' && symname[1] == '$')
214               || (symname[0] == '$' && symname[strlen (symname) - 1] == '$')
215                   || (symname[0] == 'D' && symname[1] == '$')
216                   || (strncmp (symname, "L0\001", 3) == 0)
217                   || (strncmp (symname, "$PIC", 4) == 0))
218                 continue;
219               break;
220
221             case ST_PRI_PROG:
222             case ST_SEC_PROG:
223             case ST_MILLICODE:
224               symname = bfd_getb32 (bufp->name) + stringtab;
225               ms_type = mst_file_text;
226               symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
227               break;
228
229             case ST_ENTRY:
230               symname = bfd_getb32 (bufp->name) + stringtab;
231               /* SS_LOCAL symbols in a shared library do not have
232                  export stubs, so we do not have to worry about
233                  using mst_file_text vs mst_solib_trampoline here like
234                  we do for SS_UNIVERSAL and SS_EXTERNAL symbols above.  */
235               ms_type = mst_file_text;
236               symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
237               break;
238
239             case ST_STUB:
240               symname = bfd_getb32 (bufp->name) + stringtab;
241               ms_type = mst_solib_trampoline;
242               symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
243               break;
244
245
246             case ST_DATA:
247               symname = bfd_getb32 (bufp->name) + stringtab;
248               ms_type = mst_file_data;
249               goto check_strange_names;
250
251             default:
252               continue;
253             }
254           break;
255
256           /* This can happen for common symbols when -E is passed to the
257              final link.  No idea _why_ that would make the linker force
258              common symbols to have an SS_UNSAT scope, but it does.
259
260              This also happens for weak symbols, but their type is
261              ST_DATA.  */
262         case SS_UNSAT:
263           switch (symbol_type)
264             {
265             case ST_STORAGE:
266             case ST_DATA:
267               symname = bfd_getb32 (bufp->name) + stringtab;
268               ms_type = mst_data;
269               break;
270
271             default:
272               continue;
273             }
274           break;
275
276         default:
277           continue;
278         }
279
280       if (bfd_getb32 (bufp->name) > obj_som_stringtab_size (abfd))
281         error (_("Invalid symbol data; bad HP string table offset: %s"),
282                plongest (bfd_getb32 (bufp->name)));
283
284       if (bfd_is_const_section (section))
285         {
286           struct obj_section *iter;
287
288           ALL_OBJFILE_OSECTIONS (objfile, iter)
289             {
290               CORE_ADDR start;
291               CORE_ADDR len;
292
293               if (bfd_is_const_section (iter->the_bfd_section))
294                 continue;
295
296               start = bfd_get_section_vma (iter->objfile->obfd,
297                                            iter->the_bfd_section);
298               len = bfd_get_section_size (iter->the_bfd_section);
299               if (start <= symbol_value && symbol_value < start + len)
300                 {
301                   section = iter->the_bfd_section;
302                   break;
303                 }
304             }
305         }
306
307       prim_record_minimal_symbol_and_info (symname, symbol_value, ms_type,
308                                            gdb_bfd_section_index (objfile->obfd,
309                                                                   section),
310                                            objfile);
311     }
312
313   do_cleanups (cleanup);
314 }
315
316 /* Scan and build partial symbols for a symbol file.
317    We have been initialized by a call to som_symfile_init, which 
318    currently does nothing.
319
320    SECTION_OFFSETS is a set of offsets to apply to relocate the symbols
321    in each section.  This is ignored, as it isn't needed for SOM.
322
323    This function only does the minimum work necessary for letting the
324    user "name" things symbolically; it does not read the entire symtab.
325    Instead, it reads the external and static symbols and puts them in partial
326    symbol tables.  When more extensive information is requested of a
327    file, the corresponding partial symbol table is mutated into a full
328    fledged symbol table by going back and reading the symbols
329    for real.
330
331    We look for sections with specific names, to tell us what debug
332    format to look for.
333
334    somstab_build_psymtabs() handles STABS symbols.
335
336    Note that SOM files have a "minimal" symbol table, which is vaguely
337    reminiscent of a COFF symbol table, but has only the minimal information
338    necessary for linking.  We process this also, and use the information to
339    build gdb's minimal symbol table.  This gives us some minimal debugging
340    capability even for files compiled without -g.  */
341
342 static void
343 som_symfile_read (struct objfile *objfile, int symfile_flags)
344 {
345   bfd *abfd = objfile->obfd;
346   struct cleanup *back_to;
347
348   init_minimal_symbol_collection ();
349   back_to = make_cleanup_discard_minimal_symbols ();
350
351   /* Process the normal SOM symbol table first.
352      This reads in the DNTT and string table, but doesn't
353      actually scan the DNTT.  It does scan the linker symbol
354      table and thus build up a "minimal symbol table".  */
355
356   som_symtab_read (abfd, objfile, objfile->section_offsets);
357
358   /* Install any minimal symbols that have been collected as the current
359      minimal symbols for this objfile.
360      Further symbol-reading is done incrementally, file-by-file,
361      in a step known as "psymtab-to-symtab" expansion.  hp-symtab-read.c
362      contains the code to do the actual DNTT scanning and symtab building.  */
363   install_minimal_symbols (objfile);
364   do_cleanups (back_to);
365
366   /* Now read information from the stabs debug sections.
367      This is emitted by gcc.  */
368   stabsect_build_psymtabs (objfile,
369                            "$GDB_SYMBOLS$", "$GDB_STRINGS$", "$TEXT$");
370 }
371
372 /* Initialize anything that needs initializing when a completely new symbol
373    file is specified (not just adding some symbols from another file, e.g. a
374    shared library).
375
376    We reinitialize buildsym, since we may be reading stabs from a SOM file.  */
377
378 static void
379 som_new_init (struct objfile *ignore)
380 {
381   stabsread_new_init ();
382   buildsym_new_init ();
383 }
384
385 /* Perform any local cleanups required when we are done with a particular
386    objfile.  I.e, we are in the process of discarding all symbol information
387    for an objfile, freeing up all memory held for it, and unlinking the
388    objfile struct from the global list of known objfiles.  */
389
390 static void
391 som_symfile_finish (struct objfile *objfile)
392 {
393 }
394
395 /* SOM specific initialization routine for reading symbols.  */
396
397 static void
398 som_symfile_init (struct objfile *objfile)
399 {
400   /* SOM objects may be reordered, so set OBJF_REORDERED.  If we
401      find this causes a significant slowdown in gdb then we could
402      set it in the debug symbol readers only when necessary.  */
403   objfile->flags |= OBJF_REORDERED;
404 }
405
406 /* An object of this type is passed to find_section_offset.  */
407
408 struct find_section_offset_arg
409 {
410   /* The objfile.  */
411
412   struct objfile *objfile;
413
414   /* Flags to invert.  */
415
416   flagword invert;
417
418   /* Flags to look for.  */
419
420   flagword flags;
421
422   /* A text section with non-zero size, if any.  */
423
424   asection *best_section;
425
426   /* An empty text section, if any.  */
427
428   asection *empty_section;
429 };
430
431 /* A callback for bfd_map_over_sections that tries to find a section
432    with particular flags in an objfile.  */
433
434 static void
435 find_section_offset (bfd *abfd, asection *sect, void *arg)
436 {
437   struct find_section_offset_arg *info = arg;
438   flagword aflag;
439
440   aflag = bfd_get_section_flags (abfd, sect);
441
442   aflag ^= info->invert;
443
444   if ((aflag & info->flags) == info->flags)
445     {
446       if (bfd_section_size (abfd, sect) > 0)
447         {
448           if (info->best_section == NULL)
449             info->best_section = sect;
450         }
451       else
452         {
453           if (info->empty_section == NULL)
454             info->empty_section = sect;
455         }
456     }
457 }
458
459 /* Set a section index from a BFD.  */
460
461 static void
462 set_section_index (struct objfile *objfile, flagword invert, flagword flags,
463                    int *index_ptr)
464 {
465   struct find_section_offset_arg info;
466
467   info.objfile = objfile;
468   info.best_section = NULL;
469   info.empty_section = NULL;
470   info.invert = invert;
471   info.flags = flags;
472   bfd_map_over_sections (objfile->obfd, find_section_offset, &info);
473
474   if (info.best_section)
475     *index_ptr = info.best_section->index;
476   else if (info.empty_section)
477     *index_ptr = info.empty_section->index;
478 }
479
480 /* SOM specific parsing routine for section offsets.
481
482    Plain and simple for now.  */
483
484 static void
485 som_symfile_offsets (struct objfile *objfile,
486                      const struct section_addr_info *addrs)
487 {
488   int i;
489   CORE_ADDR text_addr;
490   asection *sect;
491
492   objfile->num_sections = bfd_count_sections (objfile->obfd);
493   objfile->section_offsets = (struct section_offsets *)
494     obstack_alloc (&objfile->objfile_obstack, 
495                    SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));
496
497   set_section_index (objfile, 0, SEC_ALLOC | SEC_CODE,
498                      &objfile->sect_index_text);
499   set_section_index (objfile, 0, SEC_ALLOC | SEC_DATA,
500                      &objfile->sect_index_data);
501   set_section_index (objfile, SEC_LOAD, SEC_ALLOC | SEC_LOAD,
502                      &objfile->sect_index_bss);
503   set_section_index (objfile, 0, SEC_ALLOC | SEC_READONLY,
504                      &objfile->sect_index_rodata);
505
506   /* First see if we're a shared library.  If so, get the section
507      offsets from the library, else get them from addrs.  */
508   if (!som_solib_section_offsets (objfile, objfile->section_offsets))
509     {
510       /* Note: Here is OK to compare with ".text" because this is the
511          name that gdb itself gives to that section, not the SOM
512          name.  */
513       for (i = 0; i < addrs->num_sections; i++)
514         if (strcmp (addrs->other[i].name, ".text") == 0)
515           break;
516       text_addr = addrs->other[i].addr;
517
518       for (i = 0; i < objfile->num_sections; i++)
519         (objfile->section_offsets)->offsets[i] = text_addr;
520     }
521 }
522 \f
523
524
525 /* Register that we are able to handle SOM object file formats.  */
526
527 static const struct sym_fns som_sym_fns =
528 {
529   som_new_init,                 /* init anything gbl to entire symtab */
530   som_symfile_init,             /* read initial info, setup for sym_read() */
531   som_symfile_read,             /* read a symbol file into symtab */
532   NULL,                         /* sym_read_psymbols */
533   som_symfile_finish,           /* finished with file, cleanup */
534   som_symfile_offsets,          /* Translate ext. to int. relocation */
535   default_symfile_segments,     /* Get segment information from a file.  */
536   NULL,
537   default_symfile_relocate,     /* Relocate a debug section.  */
538   NULL,                         /* sym_get_probes */
539   &psym_functions
540 };
541
542 initialize_file_ftype _initialize_somread;
543
544 void
545 _initialize_somread (void)
546 {
547   add_symtab_fns (bfd_target_som_flavour, &som_sym_fns);
548 }