xref: /openbsd-src/gnu/usr.bin/binutils/gdb/somread.c (revision 63addd46c1e40ca0f49488ddcdc4ab598023b0c1)
1e93f7393Sniklas /* Read HP PA/Risc object files for GDB.
2b725ae77Skettenis    Copyright 1991, 1992, 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002,
3b725ae77Skettenis    2004 Free Software Foundation, Inc.
4e93f7393Sniklas    Written by Fred Fish at Cygnus Support.
5e93f7393Sniklas 
6e93f7393Sniklas    This file is part of GDB.
7e93f7393Sniklas 
8e93f7393Sniklas    This program is free software; you can redistribute it and/or modify
9e93f7393Sniklas    it under the terms of the GNU General Public License as published by
10e93f7393Sniklas    the Free Software Foundation; either version 2 of the License, or
11e93f7393Sniklas    (at your option) any later version.
12e93f7393Sniklas 
13e93f7393Sniklas    This program is distributed in the hope that it will be useful,
14e93f7393Sniklas    but WITHOUT ANY WARRANTY; without even the implied warranty of
15e93f7393Sniklas    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16e93f7393Sniklas    GNU General Public License for more details.
17e93f7393Sniklas 
18e93f7393Sniklas    You should have received a copy of the GNU General Public License
19e93f7393Sniklas    along with this program; if not, write to the Free Software
20b725ae77Skettenis    Foundation, Inc., 59 Temple Place - Suite 330,
21b725ae77Skettenis    Boston, MA 02111-1307, USA.  */
22e93f7393Sniklas 
23e93f7393Sniklas #include "defs.h"
24e93f7393Sniklas #include "bfd.h"
25e93f7393Sniklas #include <syms.h>
26e93f7393Sniklas #include "symtab.h"
27e93f7393Sniklas #include "symfile.h"
28e93f7393Sniklas #include "objfiles.h"
29e93f7393Sniklas #include "buildsym.h"
30e93f7393Sniklas #include "stabsread.h"
31e93f7393Sniklas #include "gdb-stabs.h"
32e93f7393Sniklas #include "complaints.h"
33e93f7393Sniklas #include "gdb_string.h"
34e93f7393Sniklas #include "demangle.h"
35e93f7393Sniklas #include "som.h"
36e93f7393Sniklas #include "libhppa.h"
37e93f7393Sniklas 
38e93f7393Sniklas /* Various things we might complain about... */
39e93f7393Sniklas 
40b725ae77Skettenis static int init_import_symbols (struct objfile *objfile);
41e93f7393Sniklas 
42b725ae77Skettenis static void som_symfile_init (struct objfile *);
43e93f7393Sniklas 
44b725ae77Skettenis static void som_new_init (struct objfile *);
45e93f7393Sniklas 
46b725ae77Skettenis static void som_symfile_read (struct objfile *, int);
47e93f7393Sniklas 
48b725ae77Skettenis static void som_symfile_finish (struct objfile *);
49e93f7393Sniklas 
50b725ae77Skettenis static void som_symtab_read (bfd *, struct objfile *,
51b725ae77Skettenis 			     struct section_offsets *);
52b725ae77Skettenis 
53b725ae77Skettenis static void som_symfile_offsets (struct objfile *, struct section_addr_info *);
54e93f7393Sniklas 
55e93f7393Sniklas /* FIXME: These should really be in a common header somewhere */
56e93f7393Sniklas 
57b725ae77Skettenis extern void hpread_build_psymtabs (struct objfile *, int);
58e93f7393Sniklas 
59b725ae77Skettenis extern void hpread_symfile_finish (struct objfile *);
60e93f7393Sniklas 
61b725ae77Skettenis extern void hpread_symfile_init (struct objfile *);
62b725ae77Skettenis 
63b725ae77Skettenis extern void do_pxdb (bfd *);
64e93f7393Sniklas 
65e93f7393Sniklas /*
66e93f7393Sniklas 
67e93f7393Sniklas    LOCAL FUNCTION
68e93f7393Sniklas 
69e93f7393Sniklas    som_symtab_read -- read the symbol table of a SOM file
70e93f7393Sniklas 
71e93f7393Sniklas    SYNOPSIS
72e93f7393Sniklas 
73e93f7393Sniklas    void som_symtab_read (bfd *abfd, struct objfile *objfile,
74e93f7393Sniklas    struct section_offsets *section_offsets)
75e93f7393Sniklas 
76e93f7393Sniklas    DESCRIPTION
77e93f7393Sniklas 
78e93f7393Sniklas    Given an open bfd, a base address to relocate symbols to, and a
79e93f7393Sniklas    flag that specifies whether or not this bfd is for an executable
80e93f7393Sniklas    or not (may be shared library for example), add all the global
81e93f7393Sniklas    function and data symbols to the minimal symbol table.
82e93f7393Sniklas  */
83e93f7393Sniklas 
84e93f7393Sniklas static void
som_symtab_read(bfd * abfd,struct objfile * objfile,struct section_offsets * section_offsets)85b725ae77Skettenis som_symtab_read (bfd *abfd, struct objfile *objfile,
86b725ae77Skettenis 		 struct section_offsets *section_offsets)
87e93f7393Sniklas {
88e93f7393Sniklas   unsigned int number_of_symbols;
89e93f7393Sniklas   int val, dynamic;
90e93f7393Sniklas   char *stringtab;
91e93f7393Sniklas   asection *shlib_info;
92e93f7393Sniklas   struct symbol_dictionary_record *buf, *bufp, *endbufp;
93e93f7393Sniklas   char *symname;
94e93f7393Sniklas   CONST int symsize = sizeof (struct symbol_dictionary_record);
95e93f7393Sniklas   CORE_ADDR text_offset, data_offset;
96e93f7393Sniklas 
97e93f7393Sniklas 
98e93f7393Sniklas   text_offset = ANOFFSET (section_offsets, 0);
99e93f7393Sniklas   data_offset = ANOFFSET (section_offsets, 1);
100e93f7393Sniklas 
101e93f7393Sniklas   number_of_symbols = bfd_get_symcount (abfd);
102e93f7393Sniklas 
103b725ae77Skettenis   /* FIXME (alloca): could be quite large. */
104e93f7393Sniklas   buf = alloca (symsize * number_of_symbols);
105e93f7393Sniklas   bfd_seek (abfd, obj_som_sym_filepos (abfd), SEEK_SET);
106b725ae77Skettenis   val = bfd_bread (buf, symsize * number_of_symbols, abfd);
107e93f7393Sniklas   if (val != symsize * number_of_symbols)
108e93f7393Sniklas     error ("Couldn't read symbol dictionary!");
109e93f7393Sniklas 
110b725ae77Skettenis   /* FIXME (alloca): could be quite large. */
111e93f7393Sniklas   stringtab = alloca (obj_som_stringtab_size (abfd));
112e93f7393Sniklas   bfd_seek (abfd, obj_som_str_filepos (abfd), SEEK_SET);
113b725ae77Skettenis   val = bfd_bread (stringtab, obj_som_stringtab_size (abfd), abfd);
114e93f7393Sniklas   if (val != obj_som_stringtab_size (abfd))
115e93f7393Sniklas     error ("Can't read in HP string table.");
116e93f7393Sniklas 
117e93f7393Sniklas   /* We need to determine if objfile is a dynamic executable (so we
118e93f7393Sniklas      can do the right thing for ST_ENTRY vs ST_CODE symbols).
119e93f7393Sniklas 
120e93f7393Sniklas      There's nothing in the header which easily allows us to do
121b725ae77Skettenis      this.
122b725ae77Skettenis 
123b725ae77Skettenis      This code used to rely upon the existence of a $SHLIB_INFO$
124b725ae77Skettenis      section to make this determination.  HP claims that it is
125b725ae77Skettenis      more accurate to check for a nonzero text offset, but they
126b725ae77Skettenis      have not provided any information about why that test is
127b725ae77Skettenis      more accurate.  */
128b725ae77Skettenis   dynamic = (text_offset != 0);
129e93f7393Sniklas 
130e93f7393Sniklas   endbufp = buf + number_of_symbols;
131e93f7393Sniklas   for (bufp = buf; bufp < endbufp; ++bufp)
132e93f7393Sniklas     {
133e93f7393Sniklas       enum minimal_symbol_type ms_type;
134e93f7393Sniklas 
135e93f7393Sniklas       QUIT;
136e93f7393Sniklas 
137e93f7393Sniklas       switch (bufp->symbol_scope)
138e93f7393Sniklas 	{
139e93f7393Sniklas 	case SS_UNIVERSAL:
140e93f7393Sniklas 	case SS_EXTERNAL:
141e93f7393Sniklas 	  switch (bufp->symbol_type)
142e93f7393Sniklas 	    {
143e93f7393Sniklas 	    case ST_SYM_EXT:
144e93f7393Sniklas 	    case ST_ARG_EXT:
145e93f7393Sniklas 	      continue;
146e93f7393Sniklas 
147e93f7393Sniklas 	    case ST_CODE:
148e93f7393Sniklas 	    case ST_PRI_PROG:
149e93f7393Sniklas 	    case ST_SEC_PROG:
150e93f7393Sniklas 	    case ST_MILLICODE:
151e93f7393Sniklas 	      symname = bufp->name.n_strx + stringtab;
152e93f7393Sniklas 	      ms_type = mst_text;
153e93f7393Sniklas 	      bufp->symbol_value += text_offset;
154b725ae77Skettenis 	      bufp->symbol_value = SMASH_TEXT_ADDRESS (bufp->symbol_value);
155e93f7393Sniklas 	      break;
156e93f7393Sniklas 
157e93f7393Sniklas 	    case ST_ENTRY:
158e93f7393Sniklas 	      symname = bufp->name.n_strx + stringtab;
159e93f7393Sniklas 	      /* For a dynamic executable, ST_ENTRY symbols are
160e93f7393Sniklas 	         the stubs, while the ST_CODE symbol is the real
161e93f7393Sniklas 	         function.  */
162e93f7393Sniklas 	      if (dynamic)
163e93f7393Sniklas 		ms_type = mst_solib_trampoline;
164e93f7393Sniklas 	      else
165e93f7393Sniklas 		ms_type = mst_text;
166e93f7393Sniklas 	      bufp->symbol_value += text_offset;
167b725ae77Skettenis 	      bufp->symbol_value = SMASH_TEXT_ADDRESS (bufp->symbol_value);
168e93f7393Sniklas 	      break;
169e93f7393Sniklas 
170e93f7393Sniklas 	    case ST_STUB:
171e93f7393Sniklas 	      symname = bufp->name.n_strx + stringtab;
172e93f7393Sniklas 	      ms_type = mst_solib_trampoline;
173e93f7393Sniklas 	      bufp->symbol_value += text_offset;
174b725ae77Skettenis 	      bufp->symbol_value = SMASH_TEXT_ADDRESS (bufp->symbol_value);
175e93f7393Sniklas 	      break;
176e93f7393Sniklas 
177e93f7393Sniklas 	    case ST_DATA:
178e93f7393Sniklas 	      symname = bufp->name.n_strx + stringtab;
179e93f7393Sniklas 	      bufp->symbol_value += data_offset;
180e93f7393Sniklas 	      ms_type = mst_data;
181e93f7393Sniklas 	      break;
182e93f7393Sniklas 	    default:
183e93f7393Sniklas 	      continue;
184e93f7393Sniklas 	    }
185e93f7393Sniklas 	  break;
186e93f7393Sniklas 
187e93f7393Sniklas #if 0
188e93f7393Sniklas 	  /* SS_GLOBAL and SS_LOCAL are two names for the same thing (!).  */
189e93f7393Sniklas 	case SS_GLOBAL:
190e93f7393Sniklas #endif
191e93f7393Sniklas 	case SS_LOCAL:
192e93f7393Sniklas 	  switch (bufp->symbol_type)
193e93f7393Sniklas 	    {
194e93f7393Sniklas 	    case ST_SYM_EXT:
195e93f7393Sniklas 	    case ST_ARG_EXT:
196e93f7393Sniklas 	      continue;
197e93f7393Sniklas 
198e93f7393Sniklas 	    case ST_CODE:
199e93f7393Sniklas 	      symname = bufp->name.n_strx + stringtab;
200e93f7393Sniklas 	      ms_type = mst_file_text;
201e93f7393Sniklas 	      bufp->symbol_value += text_offset;
202b725ae77Skettenis 	      bufp->symbol_value = SMASH_TEXT_ADDRESS (bufp->symbol_value);
203e93f7393Sniklas 
204e93f7393Sniklas 	    check_strange_names:
205e93f7393Sniklas 	      /* Utah GCC 2.5, FSF GCC 2.6 and later generate correct local
206e93f7393Sniklas 	         label prefixes for stabs, constant data, etc.  So we need
207e93f7393Sniklas 	         only filter out L$ symbols which are left in due to
208e93f7393Sniklas 	         limitations in how GAS generates SOM relocations.
209e93f7393Sniklas 
210e93f7393Sniklas 	         When linking in the HPUX C-library the HP linker has
211e93f7393Sniklas 	         the nasty habit of placing section symbols from the literal
212e93f7393Sniklas 	         subspaces in the middle of the program's text.  Filter
213e93f7393Sniklas 	         those out as best we can.  Check for first and last character
214e93f7393Sniklas 	         being '$'.
215e93f7393Sniklas 
216e93f7393Sniklas 	         And finally, the newer HP compilers emit crud like $PIC_foo$N
217e93f7393Sniklas 	         in some circumstance (PIC code I guess).  It's also claimed
218e93f7393Sniklas 	         that they emit D$ symbols too.  What stupidity.  */
219e93f7393Sniklas 	      if ((symname[0] == 'L' && symname[1] == '$')
220e93f7393Sniklas 	      || (symname[0] == '$' && symname[strlen (symname) - 1] == '$')
221e93f7393Sniklas 		  || (symname[0] == 'D' && symname[1] == '$')
222*63addd46Skettenis 		  || (strncmp (symname, "L0\001", 3) == 0)
223e93f7393Sniklas 		  || (strncmp (symname, "$PIC", 4) == 0))
224e93f7393Sniklas 		continue;
225e93f7393Sniklas 	      break;
226e93f7393Sniklas 
227e93f7393Sniklas 	    case ST_PRI_PROG:
228e93f7393Sniklas 	    case ST_SEC_PROG:
229e93f7393Sniklas 	    case ST_MILLICODE:
230e93f7393Sniklas 	      symname = bufp->name.n_strx + stringtab;
231e93f7393Sniklas 	      ms_type = mst_file_text;
232e93f7393Sniklas 	      bufp->symbol_value += text_offset;
233b725ae77Skettenis 	      bufp->symbol_value = SMASH_TEXT_ADDRESS (bufp->symbol_value);
234e93f7393Sniklas 	      break;
235e93f7393Sniklas 
236e93f7393Sniklas 	    case ST_ENTRY:
237e93f7393Sniklas 	      symname = bufp->name.n_strx + stringtab;
238b725ae77Skettenis 	      /* SS_LOCAL symbols in a shared library do not have
239b725ae77Skettenis 		 export stubs, so we do not have to worry about
240b725ae77Skettenis 		 using mst_file_text vs mst_solib_trampoline here like
241b725ae77Skettenis 		 we do for SS_UNIVERSAL and SS_EXTERNAL symbols above.  */
242e93f7393Sniklas 	      ms_type = mst_file_text;
243e93f7393Sniklas 	      bufp->symbol_value += text_offset;
244b725ae77Skettenis 	      bufp->symbol_value = SMASH_TEXT_ADDRESS (bufp->symbol_value);
245e93f7393Sniklas 	      break;
246e93f7393Sniklas 
247e93f7393Sniklas 	    case ST_STUB:
248e93f7393Sniklas 	      symname = bufp->name.n_strx + stringtab;
249e93f7393Sniklas 	      ms_type = mst_solib_trampoline;
250e93f7393Sniklas 	      bufp->symbol_value += text_offset;
251b725ae77Skettenis 	      bufp->symbol_value = SMASH_TEXT_ADDRESS (bufp->symbol_value);
252e93f7393Sniklas 	      break;
253e93f7393Sniklas 
254e93f7393Sniklas 
255e93f7393Sniklas 	    case ST_DATA:
256e93f7393Sniklas 	      symname = bufp->name.n_strx + stringtab;
257e93f7393Sniklas 	      bufp->symbol_value += data_offset;
258e93f7393Sniklas 	      ms_type = mst_file_data;
259e93f7393Sniklas 	      goto check_strange_names;
260e93f7393Sniklas 
261e93f7393Sniklas 	    default:
262e93f7393Sniklas 	      continue;
263e93f7393Sniklas 	    }
264e93f7393Sniklas 	  break;
265e93f7393Sniklas 
266e93f7393Sniklas 	  /* This can happen for common symbols when -E is passed to the
267e93f7393Sniklas 	     final link.  No idea _why_ that would make the linker force
268e93f7393Sniklas 	     common symbols to have an SS_UNSAT scope, but it does.
269e93f7393Sniklas 
270e93f7393Sniklas 	     This also happens for weak symbols, but their type is
271e93f7393Sniklas 	     ST_DATA.  */
272e93f7393Sniklas 	case SS_UNSAT:
273e93f7393Sniklas 	  switch (bufp->symbol_type)
274e93f7393Sniklas 	    {
275e93f7393Sniklas 	    case ST_STORAGE:
276e93f7393Sniklas 	    case ST_DATA:
277e93f7393Sniklas 	      symname = bufp->name.n_strx + stringtab;
278e93f7393Sniklas 	      bufp->symbol_value += data_offset;
279e93f7393Sniklas 	      ms_type = mst_data;
280e93f7393Sniklas 	      break;
281e93f7393Sniklas 
282e93f7393Sniklas 	    default:
283e93f7393Sniklas 	      continue;
284e93f7393Sniklas 	    }
285e93f7393Sniklas 	  break;
286e93f7393Sniklas 
287e93f7393Sniklas 	default:
288e93f7393Sniklas 	  continue;
289e93f7393Sniklas 	}
290e93f7393Sniklas 
291e93f7393Sniklas       if (bufp->name.n_strx > obj_som_stringtab_size (abfd))
292e93f7393Sniklas 	error ("Invalid symbol data; bad HP string table offset: %d",
293e93f7393Sniklas 	       bufp->name.n_strx);
294e93f7393Sniklas 
295e93f7393Sniklas       prim_record_minimal_symbol (symname, bufp->symbol_value, ms_type,
296e93f7393Sniklas 				  objfile);
297e93f7393Sniklas     }
298e93f7393Sniklas }
299e93f7393Sniklas 
300e93f7393Sniklas /* Scan and build partial symbols for a symbol file.
301e93f7393Sniklas    We have been initialized by a call to som_symfile_init, which
302e93f7393Sniklas    currently does nothing.
303e93f7393Sniklas 
304e93f7393Sniklas    SECTION_OFFSETS is a set of offsets to apply to relocate the symbols
305e93f7393Sniklas    in each section.  This is ignored, as it isn't needed for SOM.
306e93f7393Sniklas 
307e93f7393Sniklas    MAINLINE is true if we are reading the main symbol
308e93f7393Sniklas    table (as opposed to a shared lib or dynamically loaded file).
309e93f7393Sniklas 
310e93f7393Sniklas    This function only does the minimum work necessary for letting the
311e93f7393Sniklas    user "name" things symbolically; it does not read the entire symtab.
312e93f7393Sniklas    Instead, it reads the external and static symbols and puts them in partial
313e93f7393Sniklas    symbol tables.  When more extensive information is requested of a
314e93f7393Sniklas    file, the corresponding partial symbol table is mutated into a full
315e93f7393Sniklas    fledged symbol table by going back and reading the symbols
316e93f7393Sniklas    for real.
317e93f7393Sniklas 
318e93f7393Sniklas    We look for sections with specific names, to tell us what debug
319e93f7393Sniklas    format to look for:  FIXME!!!
320e93f7393Sniklas 
321e93f7393Sniklas    somstab_build_psymtabs() handles STABS symbols.
322e93f7393Sniklas 
323e93f7393Sniklas    Note that SOM files have a "minimal" symbol table, which is vaguely
324e93f7393Sniklas    reminiscent of a COFF symbol table, but has only the minimal information
325e93f7393Sniklas    necessary for linking.  We process this also, and use the information to
326e93f7393Sniklas    build gdb's minimal symbol table.  This gives us some minimal debugging
327e93f7393Sniklas    capability even for files compiled without -g.  */
328e93f7393Sniklas 
329e93f7393Sniklas static void
som_symfile_read(struct objfile * objfile,int mainline)330b725ae77Skettenis som_symfile_read (struct objfile *objfile, int mainline)
331e93f7393Sniklas {
332e93f7393Sniklas   bfd *abfd = objfile->obfd;
333e93f7393Sniklas   struct cleanup *back_to;
334e93f7393Sniklas 
335b725ae77Skettenis   do_pxdb (symfile_bfd_open (objfile->name));
336b725ae77Skettenis 
337e93f7393Sniklas   init_minimal_symbol_collection ();
338b725ae77Skettenis   back_to = make_cleanup_discard_minimal_symbols ();
339e93f7393Sniklas 
340b725ae77Skettenis   /* Read in the import list and the export list.  Currently
341b725ae77Skettenis      the export list isn't used; the import list is used in
342b725ae77Skettenis      hp-symtab-read.c to handle static vars declared in other
343b725ae77Skettenis      shared libraries. */
344b725ae77Skettenis   init_import_symbols (objfile);
345b725ae77Skettenis #if 0				/* Export symbols not used today 1997-08-05 */
346b725ae77Skettenis   init_export_symbols (objfile);
347b725ae77Skettenis #else
348b725ae77Skettenis   objfile->export_list = NULL;
349b725ae77Skettenis   objfile->export_list_size = 0;
350b725ae77Skettenis #endif
351e93f7393Sniklas 
352b725ae77Skettenis   /* Process the normal SOM symbol table first.
353b725ae77Skettenis      This reads in the DNTT and string table, but doesn't
354b725ae77Skettenis      actually scan the DNTT. It does scan the linker symbol
355b725ae77Skettenis      table and thus build up a "minimal symbol table". */
356e93f7393Sniklas 
357b725ae77Skettenis   som_symtab_read (abfd, objfile, objfile->section_offsets);
358e93f7393Sniklas 
359e93f7393Sniklas   /* Install any minimal symbols that have been collected as the current
360b725ae77Skettenis      minimal symbols for this objfile.
361b725ae77Skettenis      Further symbol-reading is done incrementally, file-by-file,
362b725ae77Skettenis      in a step known as "psymtab-to-symtab" expansion. hp-symtab-read.c
363b725ae77Skettenis      contains the code to do the actual DNTT scanning and symtab building. */
364e93f7393Sniklas   install_minimal_symbols (objfile);
365b725ae77Skettenis   do_cleanups (back_to);
366b725ae77Skettenis 
367b725ae77Skettenis   /* Now read information from the stabs debug sections.
368b725ae77Skettenis      This is a no-op for SOM.
369b725ae77Skettenis      Perhaps it is intended for some kind of mixed STABS/SOM
370b725ae77Skettenis      situation? */
371b725ae77Skettenis   stabsect_build_psymtabs (objfile, mainline,
372b725ae77Skettenis 			   "$GDB_SYMBOLS$", "$GDB_STRINGS$", "$TEXT$");
373b725ae77Skettenis 
374b725ae77Skettenis   /* Now read the native debug information.
375b725ae77Skettenis      This builds the psymtab. This used to be done via a scan of
376b725ae77Skettenis      the DNTT, but is now done via the PXDB-built quick-lookup tables
377b725ae77Skettenis      together with a scan of the GNTT. See hp-psymtab-read.c. */
378b725ae77Skettenis   hpread_build_psymtabs (objfile, mainline);
379e93f7393Sniklas 
380e93f7393Sniklas   /* Force hppa-tdep.c to re-read the unwind descriptors.  */
381e93f7393Sniklas   objfile->obj_private = NULL;
382e93f7393Sniklas }
383e93f7393Sniklas 
384e93f7393Sniklas /* Initialize anything that needs initializing when a completely new symbol
385e93f7393Sniklas    file is specified (not just adding some symbols from another file, e.g. a
386e93f7393Sniklas    shared library).
387e93f7393Sniklas 
388e93f7393Sniklas    We reinitialize buildsym, since we may be reading stabs from a SOM file.  */
389e93f7393Sniklas 
390e93f7393Sniklas static void
som_new_init(struct objfile * ignore)391b725ae77Skettenis som_new_init (struct objfile *ignore)
392e93f7393Sniklas {
393e93f7393Sniklas   stabsread_new_init ();
394e93f7393Sniklas   buildsym_new_init ();
395e93f7393Sniklas }
396e93f7393Sniklas 
397e93f7393Sniklas /* Perform any local cleanups required when we are done with a particular
398e93f7393Sniklas    objfile.  I.E, we are in the process of discarding all symbol information
399e93f7393Sniklas    for an objfile, freeing up all memory held for it, and unlinking the
400e93f7393Sniklas    objfile struct from the global list of known objfiles. */
401e93f7393Sniklas 
402e93f7393Sniklas static void
som_symfile_finish(struct objfile * objfile)403b725ae77Skettenis som_symfile_finish (struct objfile *objfile)
404e93f7393Sniklas {
405e93f7393Sniklas   if (objfile->sym_stab_info != NULL)
406e93f7393Sniklas     {
407*63addd46Skettenis       xfree (objfile->sym_stab_info);
408e93f7393Sniklas     }
409e93f7393Sniklas   hpread_symfile_finish (objfile);
410e93f7393Sniklas }
411e93f7393Sniklas 
412e93f7393Sniklas /* SOM specific initialization routine for reading symbols.  */
413e93f7393Sniklas 
414e93f7393Sniklas static void
som_symfile_init(struct objfile * objfile)415b725ae77Skettenis som_symfile_init (struct objfile *objfile)
416e93f7393Sniklas {
417e93f7393Sniklas   /* SOM objects may be reordered, so set OBJF_REORDERED.  If we
418e93f7393Sniklas      find this causes a significant slowdown in gdb then we could
419e93f7393Sniklas      set it in the debug symbol readers only when necessary.  */
420e93f7393Sniklas   objfile->flags |= OBJF_REORDERED;
421e93f7393Sniklas   hpread_symfile_init (objfile);
422e93f7393Sniklas }
423e93f7393Sniklas 
424e93f7393Sniklas /* SOM specific parsing routine for section offsets.
425e93f7393Sniklas 
426e93f7393Sniklas    Plain and simple for now.  */
427e93f7393Sniklas 
428b725ae77Skettenis static void
som_symfile_offsets(struct objfile * objfile,struct section_addr_info * addrs)429b725ae77Skettenis som_symfile_offsets (struct objfile *objfile, struct section_addr_info *addrs)
430e93f7393Sniklas {
431e93f7393Sniklas   int i;
432b725ae77Skettenis   CORE_ADDR text_addr;
433e93f7393Sniklas 
434b725ae77Skettenis   objfile->num_sections = bfd_count_sections (objfile->obfd);
435b725ae77Skettenis   objfile->section_offsets = (struct section_offsets *)
436b725ae77Skettenis     obstack_alloc (&objfile->objfile_obstack,
437b725ae77Skettenis 		   SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));
438b725ae77Skettenis 
439b725ae77Skettenis   /* FIXME: ezannoni 2000-04-20 The section names in SOM are not
440b725ae77Skettenis      .text, .data, etc, but $TEXT$, $DATA$,... We should initialize
441b725ae77Skettenis      SET_OFF_* from bfd. (See default_symfile_offsets()). But I don't
442b725ae77Skettenis      know the correspondence between SOM sections and GDB's idea of
443b725ae77Skettenis      section names. So for now we default to what is was before these
444b725ae77Skettenis      changes.*/
445b725ae77Skettenis   objfile->sect_index_text = 0;
446b725ae77Skettenis   objfile->sect_index_data = 1;
447b725ae77Skettenis   objfile->sect_index_bss = 2;
448b725ae77Skettenis   objfile->sect_index_rodata = 3;
449e93f7393Sniklas 
450e93f7393Sniklas   /* First see if we're a shared library.  If so, get the section
451b725ae77Skettenis      offsets from the library, else get them from addrs.  */
452b725ae77Skettenis   if (!som_solib_section_offsets (objfile, objfile->section_offsets))
453e93f7393Sniklas     {
454b725ae77Skettenis       /* Note: Here is OK to compare with ".text" because this is the
455b725ae77Skettenis          name that gdb itself gives to that section, not the SOM
456b725ae77Skettenis          name. */
457b725ae77Skettenis       for (i = 0; i < objfile->num_sections && addrs->other[i].name; i++)
458b725ae77Skettenis 	if (strcmp (addrs->other[i].name, ".text") == 0)
459b725ae77Skettenis 	  break;
460b725ae77Skettenis       text_addr = addrs->other[i].addr;
461b725ae77Skettenis 
462b725ae77Skettenis       for (i = 0; i < objfile->num_sections; i++)
463b725ae77Skettenis 	(objfile->section_offsets)->offsets[i] = text_addr;
464b725ae77Skettenis     }
465e93f7393Sniklas }
466e93f7393Sniklas 
467b725ae77Skettenis /* Read in and initialize the SOM import list which is present
468b725ae77Skettenis    for all executables and shared libraries.  The import list
469b725ae77Skettenis    consists of the symbols that are referenced in OBJFILE but
470b725ae77Skettenis    not defined there.  (Variables that are imported are dealt
471b725ae77Skettenis    with as "loc_indirect" vars.)
472b725ae77Skettenis    Return value = number of import symbols read in. */
473b725ae77Skettenis static int
init_import_symbols(struct objfile * objfile)474b725ae77Skettenis init_import_symbols (struct objfile *objfile)
475b725ae77Skettenis {
476b725ae77Skettenis   unsigned int import_list;
477b725ae77Skettenis   unsigned int import_list_size;
478b725ae77Skettenis   unsigned int string_table;
479b725ae77Skettenis   unsigned int string_table_size;
480b725ae77Skettenis   char *string_buffer;
481b725ae77Skettenis   int i;
482b725ae77Skettenis   int j;
483b725ae77Skettenis   int k;
484b725ae77Skettenis   asection *text_section;	/* section handle */
485b725ae77Skettenis   unsigned int dl_header[12];	/* SOM executable header */
486b725ae77Skettenis 
487b725ae77Skettenis   /* A struct for an entry in the SOM import list */
488b725ae77Skettenis   typedef struct
489b725ae77Skettenis     {
490b725ae77Skettenis       int name;			/* index into the string table */
491b725ae77Skettenis       short dont_care1;		/* we don't use this */
492b725ae77Skettenis       unsigned char type;	/* 0 = NULL, 2 = Data, 3 = Code, 7 = Storage, 13 = Plabel */
493b725ae77Skettenis       unsigned int reserved2:8;	/* not used */
494b725ae77Skettenis     }
495b725ae77Skettenis   SomImportEntry;
496b725ae77Skettenis 
497b725ae77Skettenis   /* We read 100 entries in at a time from the disk file. */
498b725ae77Skettenis #define SOM_READ_IMPORTS_NUM         100
499b725ae77Skettenis #define SOM_READ_IMPORTS_CHUNK_SIZE  (sizeof (SomImportEntry) * SOM_READ_IMPORTS_NUM)
500b725ae77Skettenis   SomImportEntry buffer[SOM_READ_IMPORTS_NUM];
501b725ae77Skettenis 
502b725ae77Skettenis   /* Initialize in case we error out */
503b725ae77Skettenis   objfile->import_list = NULL;
504b725ae77Skettenis   objfile->import_list_size = 0;
505b725ae77Skettenis 
506b725ae77Skettenis   /* It doesn't work, for some reason, to read in space $TEXT$;
507b725ae77Skettenis      the subspace $SHLIB_INFO$ has to be used.  Some BFD quirk? pai/1997-08-05 */
508b725ae77Skettenis   text_section = bfd_get_section_by_name (objfile->obfd, "$SHLIB_INFO$");
509b725ae77Skettenis   if (!text_section)
510b725ae77Skettenis     return 0;
511b725ae77Skettenis   /* Get the SOM executable header */
512b725ae77Skettenis   bfd_get_section_contents (objfile->obfd, text_section, dl_header, 0, 12 * sizeof (int));
513b725ae77Skettenis 
514b725ae77Skettenis   /* Check header version number for 10.x HP-UX */
515b725ae77Skettenis   /* Currently we deal only with 10.x systems; on 9.x the version # is 89060912.
516b725ae77Skettenis      FIXME: Change for future HP-UX releases and mods to the SOM executable format */
517b725ae77Skettenis   if (dl_header[0] != 93092112)
518b725ae77Skettenis     return 0;
519b725ae77Skettenis 
520b725ae77Skettenis   import_list = dl_header[4];
521b725ae77Skettenis   import_list_size = dl_header[5];
522b725ae77Skettenis   if (!import_list_size)
523b725ae77Skettenis     return 0;
524b725ae77Skettenis   string_table = dl_header[10];
525b725ae77Skettenis   string_table_size = dl_header[11];
526b725ae77Skettenis   if (!string_table_size)
527b725ae77Skettenis     return 0;
528b725ae77Skettenis 
529b725ae77Skettenis   /* Suck in SOM string table */
530b725ae77Skettenis   string_buffer = (char *) xmalloc (string_table_size);
531b725ae77Skettenis   bfd_get_section_contents (objfile->obfd, text_section, string_buffer,
532b725ae77Skettenis 			    string_table, string_table_size);
533b725ae77Skettenis 
534b725ae77Skettenis   /* Allocate import list in the psymbol obstack; this has nothing
535b725ae77Skettenis      to do with psymbols, just a matter of convenience.  We want the
536b725ae77Skettenis      import list to be freed when the objfile is deallocated */
537b725ae77Skettenis   objfile->import_list
538b725ae77Skettenis     = (ImportEntry *) obstack_alloc (&objfile->objfile_obstack,
539b725ae77Skettenis 				   import_list_size * sizeof (ImportEntry));
540b725ae77Skettenis 
541b725ae77Skettenis   /* Read in the import entries, a bunch at a time */
542b725ae77Skettenis   for (j = 0, k = 0;
543b725ae77Skettenis        j < (import_list_size / SOM_READ_IMPORTS_NUM);
544b725ae77Skettenis        j++)
545b725ae77Skettenis     {
546b725ae77Skettenis       bfd_get_section_contents (objfile->obfd, text_section, buffer,
547b725ae77Skettenis 			      import_list + j * SOM_READ_IMPORTS_CHUNK_SIZE,
548b725ae77Skettenis 				SOM_READ_IMPORTS_CHUNK_SIZE);
549b725ae77Skettenis       for (i = 0; i < SOM_READ_IMPORTS_NUM; i++, k++)
550b725ae77Skettenis 	{
551b725ae77Skettenis 	  if (buffer[i].type != (unsigned char) 0)
552b725ae77Skettenis 	    {
553b725ae77Skettenis 	      objfile->import_list[k]
554b725ae77Skettenis 		= (char *) obstack_alloc (&objfile->objfile_obstack, strlen (string_buffer + buffer[i].name) + 1);
555b725ae77Skettenis 	      strcpy (objfile->import_list[k], string_buffer + buffer[i].name);
556b725ae77Skettenis 	      /* Some day we might want to record the type and other information too */
557b725ae77Skettenis 	    }
558b725ae77Skettenis 	  else			/* null type */
559b725ae77Skettenis 	    objfile->import_list[k] = NULL;
560b725ae77Skettenis 
561b725ae77Skettenis 	}
562b725ae77Skettenis     }
563b725ae77Skettenis 
564b725ae77Skettenis   /* Get the leftovers */
565b725ae77Skettenis   if (k < import_list_size)
566b725ae77Skettenis     bfd_get_section_contents (objfile->obfd, text_section, buffer,
567b725ae77Skettenis 			      import_list + k * sizeof (SomImportEntry),
568b725ae77Skettenis 			  (import_list_size - k) * sizeof (SomImportEntry));
569b725ae77Skettenis   for (i = 0; k < import_list_size; i++, k++)
570b725ae77Skettenis     {
571b725ae77Skettenis       if (buffer[i].type != (unsigned char) 0)
572b725ae77Skettenis 	{
573b725ae77Skettenis 	  objfile->import_list[k]
574b725ae77Skettenis 	    = (char *) obstack_alloc (&objfile->objfile_obstack, strlen (string_buffer + buffer[i].name) + 1);
575b725ae77Skettenis 	  strcpy (objfile->import_list[k], string_buffer + buffer[i].name);
576b725ae77Skettenis 	  /* Some day we might want to record the type and other information too */
577b725ae77Skettenis 	}
578b725ae77Skettenis       else
579b725ae77Skettenis 	objfile->import_list[k] = NULL;
580b725ae77Skettenis     }
581b725ae77Skettenis 
582b725ae77Skettenis   objfile->import_list_size = import_list_size;
583b725ae77Skettenis   xfree (string_buffer);
584b725ae77Skettenis   return import_list_size;
585b725ae77Skettenis }
586b725ae77Skettenis 
587b725ae77Skettenis /* Read in and initialize the SOM export list which is present
588b725ae77Skettenis    for all executables and shared libraries.  The import list
589b725ae77Skettenis    consists of the symbols that are referenced in OBJFILE but
590b725ae77Skettenis    not defined there.  (Variables that are imported are dealt
591b725ae77Skettenis    with as "loc_indirect" vars.)
592b725ae77Skettenis    Return value = number of import symbols read in. */
593b725ae77Skettenis int
init_export_symbols(struct objfile * objfile)594b725ae77Skettenis init_export_symbols (struct objfile *objfile)
595b725ae77Skettenis {
596b725ae77Skettenis   unsigned int export_list;
597b725ae77Skettenis   unsigned int export_list_size;
598b725ae77Skettenis   unsigned int string_table;
599b725ae77Skettenis   unsigned int string_table_size;
600b725ae77Skettenis   char *string_buffer;
601b725ae77Skettenis   int i;
602b725ae77Skettenis   int j;
603b725ae77Skettenis   int k;
604b725ae77Skettenis   asection *text_section;	/* section handle */
605b725ae77Skettenis   unsigned int dl_header[12];	/* SOM executable header */
606b725ae77Skettenis 
607b725ae77Skettenis   /* A struct for an entry in the SOM export list */
608b725ae77Skettenis   typedef struct
609b725ae77Skettenis     {
610b725ae77Skettenis       int next;			/* for hash table use -- we don't use this */
611b725ae77Skettenis       int name;			/* index into string table */
612b725ae77Skettenis       int value;		/* offset or plabel */
613b725ae77Skettenis       int dont_care1;		/* not used */
614b725ae77Skettenis       unsigned char type;	/* 0 = NULL, 2 = Data, 3 = Code, 7 = Storage, 13 = Plabel */
615b725ae77Skettenis       char dont_care2;		/* not used */
616b725ae77Skettenis       short dont_care3;		/* not used */
617b725ae77Skettenis     }
618b725ae77Skettenis   SomExportEntry;
619b725ae77Skettenis 
620b725ae77Skettenis   /* We read 100 entries in at a time from the disk file. */
621b725ae77Skettenis #define SOM_READ_EXPORTS_NUM         100
622b725ae77Skettenis #define SOM_READ_EXPORTS_CHUNK_SIZE  (sizeof (SomExportEntry) * SOM_READ_EXPORTS_NUM)
623b725ae77Skettenis   SomExportEntry buffer[SOM_READ_EXPORTS_NUM];
624b725ae77Skettenis 
625b725ae77Skettenis   /* Initialize in case we error out */
626b725ae77Skettenis   objfile->export_list = NULL;
627b725ae77Skettenis   objfile->export_list_size = 0;
628b725ae77Skettenis 
629b725ae77Skettenis   /* It doesn't work, for some reason, to read in space $TEXT$;
630b725ae77Skettenis      the subspace $SHLIB_INFO$ has to be used.  Some BFD quirk? pai/1997-08-05 */
631b725ae77Skettenis   text_section = bfd_get_section_by_name (objfile->obfd, "$SHLIB_INFO$");
632b725ae77Skettenis   if (!text_section)
633b725ae77Skettenis     return 0;
634b725ae77Skettenis   /* Get the SOM executable header */
635b725ae77Skettenis   bfd_get_section_contents (objfile->obfd, text_section, dl_header, 0, 12 * sizeof (int));
636b725ae77Skettenis 
637b725ae77Skettenis   /* Check header version number for 10.x HP-UX */
638b725ae77Skettenis   /* Currently we deal only with 10.x systems; on 9.x the version # is 89060912.
639b725ae77Skettenis      FIXME: Change for future HP-UX releases and mods to the SOM executable format */
640b725ae77Skettenis   if (dl_header[0] != 93092112)
641b725ae77Skettenis     return 0;
642b725ae77Skettenis 
643b725ae77Skettenis   export_list = dl_header[8];
644b725ae77Skettenis   export_list_size = dl_header[9];
645b725ae77Skettenis   if (!export_list_size)
646b725ae77Skettenis     return 0;
647b725ae77Skettenis   string_table = dl_header[10];
648b725ae77Skettenis   string_table_size = dl_header[11];
649b725ae77Skettenis   if (!string_table_size)
650b725ae77Skettenis     return 0;
651b725ae77Skettenis 
652b725ae77Skettenis   /* Suck in SOM string table */
653b725ae77Skettenis   string_buffer = (char *) xmalloc (string_table_size);
654b725ae77Skettenis   bfd_get_section_contents (objfile->obfd, text_section, string_buffer,
655b725ae77Skettenis 			    string_table, string_table_size);
656b725ae77Skettenis 
657b725ae77Skettenis   /* Allocate export list in the psymbol obstack; this has nothing
658b725ae77Skettenis      to do with psymbols, just a matter of convenience.  We want the
659b725ae77Skettenis      export list to be freed when the objfile is deallocated */
660b725ae77Skettenis   objfile->export_list
661b725ae77Skettenis     = (ExportEntry *) obstack_alloc (&objfile->objfile_obstack,
662b725ae77Skettenis 				   export_list_size * sizeof (ExportEntry));
663b725ae77Skettenis 
664b725ae77Skettenis   /* Read in the export entries, a bunch at a time */
665b725ae77Skettenis   for (j = 0, k = 0;
666b725ae77Skettenis        j < (export_list_size / SOM_READ_EXPORTS_NUM);
667b725ae77Skettenis        j++)
668b725ae77Skettenis     {
669b725ae77Skettenis       bfd_get_section_contents (objfile->obfd, text_section, buffer,
670b725ae77Skettenis 			      export_list + j * SOM_READ_EXPORTS_CHUNK_SIZE,
671b725ae77Skettenis 				SOM_READ_EXPORTS_CHUNK_SIZE);
672b725ae77Skettenis       for (i = 0; i < SOM_READ_EXPORTS_NUM; i++, k++)
673b725ae77Skettenis 	{
674b725ae77Skettenis 	  if (buffer[i].type != (unsigned char) 0)
675b725ae77Skettenis 	    {
676b725ae77Skettenis 	      objfile->export_list[k].name
677b725ae77Skettenis 		= (char *) obstack_alloc (&objfile->objfile_obstack, strlen (string_buffer + buffer[i].name) + 1);
678b725ae77Skettenis 	      strcpy (objfile->export_list[k].name, string_buffer + buffer[i].name);
679b725ae77Skettenis 	      objfile->export_list[k].address = buffer[i].value;
680b725ae77Skettenis 	      /* Some day we might want to record the type and other information too */
681b725ae77Skettenis 	    }
682b725ae77Skettenis 	  else
683b725ae77Skettenis 	    /* null type */
684b725ae77Skettenis 	    {
685b725ae77Skettenis 	      objfile->export_list[k].name = NULL;
686b725ae77Skettenis 	      objfile->export_list[k].address = 0;
687b725ae77Skettenis 	    }
688b725ae77Skettenis 	}
689b725ae77Skettenis     }
690b725ae77Skettenis 
691b725ae77Skettenis   /* Get the leftovers */
692b725ae77Skettenis   if (k < export_list_size)
693b725ae77Skettenis     bfd_get_section_contents (objfile->obfd, text_section, buffer,
694b725ae77Skettenis 			      export_list + k * sizeof (SomExportEntry),
695b725ae77Skettenis 			  (export_list_size - k) * sizeof (SomExportEntry));
696b725ae77Skettenis   for (i = 0; k < export_list_size; i++, k++)
697b725ae77Skettenis     {
698b725ae77Skettenis       if (buffer[i].type != (unsigned char) 0)
699b725ae77Skettenis 	{
700b725ae77Skettenis 	  objfile->export_list[k].name
701b725ae77Skettenis 	    = (char *) obstack_alloc (&objfile->objfile_obstack, strlen (string_buffer + buffer[i].name) + 1);
702b725ae77Skettenis 	  strcpy (objfile->export_list[k].name, string_buffer + buffer[i].name);
703b725ae77Skettenis 	  /* Some day we might want to record the type and other information too */
704b725ae77Skettenis 	  objfile->export_list[k].address = buffer[i].value;
705b725ae77Skettenis 	}
706b725ae77Skettenis       else
707b725ae77Skettenis 	{
708b725ae77Skettenis 	  objfile->export_list[k].name = NULL;
709b725ae77Skettenis 	  objfile->export_list[k].address = 0;
710b725ae77Skettenis 	}
711b725ae77Skettenis     }
712b725ae77Skettenis 
713b725ae77Skettenis   objfile->export_list_size = export_list_size;
714b725ae77Skettenis   xfree (string_buffer);
715b725ae77Skettenis   return export_list_size;
716e93f7393Sniklas }
717e93f7393Sniklas 
718b725ae77Skettenis 
719b725ae77Skettenis 
720e93f7393Sniklas /* Register that we are able to handle SOM object file formats.  */
721e93f7393Sniklas 
722e93f7393Sniklas static struct sym_fns som_sym_fns =
723e93f7393Sniklas {
724e93f7393Sniklas   bfd_target_som_flavour,
725e93f7393Sniklas   som_new_init,			/* sym_new_init: init anything gbl to entire symtab */
726e93f7393Sniklas   som_symfile_init,		/* sym_init: read initial info, setup for sym_read() */
727e93f7393Sniklas   som_symfile_read,		/* sym_read: read a symbol file into symtab */
728e93f7393Sniklas   som_symfile_finish,		/* sym_finish: finished with file, cleanup */
729e93f7393Sniklas   som_symfile_offsets,		/* sym_offsets:  Translate ext. to int. relocation */
730e93f7393Sniklas   NULL				/* next: pointer to next struct sym_fns */
731e93f7393Sniklas };
732e93f7393Sniklas 
733e93f7393Sniklas void
_initialize_somread(void)734b725ae77Skettenis _initialize_somread (void)
735e93f7393Sniklas {
736e93f7393Sniklas   add_symtab_fns (&som_sym_fns);
737e93f7393Sniklas }
738