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