1130812Smarcel/* Read HP PA/Risc object files for GDB.
2130812Smarcel   Copyright 1991, 1992, 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002,
3130812Smarcel   2004 Free Software Foundation, Inc.
4130812Smarcel   Written by Fred Fish at Cygnus Support.
5130812Smarcel
6130812Smarcel   This file is part of GDB.
7130812Smarcel
8130812Smarcel   This program is free software; you can redistribute it and/or modify
9130812Smarcel   it under the terms of the GNU General Public License as published by
10130812Smarcel   the Free Software Foundation; either version 2 of the License, or
11130812Smarcel   (at your option) any later version.
12130812Smarcel
13130812Smarcel   This program is distributed in the hope that it will be useful,
14130812Smarcel   but WITHOUT ANY WARRANTY; without even the implied warranty of
15130812Smarcel   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16130812Smarcel   GNU General Public License for more details.
17130812Smarcel
18130812Smarcel   You should have received a copy of the GNU General Public License
19130812Smarcel   along with this program; if not, write to the Free Software
20130812Smarcel   Foundation, Inc., 59 Temple Place - Suite 330,
21130812Smarcel   Boston, MA 02111-1307, USA.  */
22130812Smarcel
23130812Smarcel#include "defs.h"
24130812Smarcel#include "bfd.h"
25130812Smarcel#include <syms.h>
26130812Smarcel#include "symtab.h"
27130812Smarcel#include "symfile.h"
28130812Smarcel#include "objfiles.h"
29130812Smarcel#include "buildsym.h"
30130812Smarcel#include "stabsread.h"
31130812Smarcel#include "gdb-stabs.h"
32130812Smarcel#include "complaints.h"
33130812Smarcel#include "gdb_string.h"
34130812Smarcel#include "demangle.h"
35130812Smarcel#include "som.h"
36130812Smarcel#include "libhppa.h"
37130812Smarcel
38130812Smarcel/* Various things we might complain about... */
39130812Smarcel
40130812Smarcelstatic int init_import_symbols (struct objfile *objfile);
41130812Smarcel
42130812Smarcelstatic void som_symfile_init (struct objfile *);
43130812Smarcel
44130812Smarcelstatic void som_new_init (struct objfile *);
45130812Smarcel
46130812Smarcelstatic void som_symfile_read (struct objfile *, int);
47130812Smarcel
48130812Smarcelstatic void som_symfile_finish (struct objfile *);
49130812Smarcel
50130812Smarcelstatic void som_symtab_read (bfd *, struct objfile *,
51130812Smarcel			     struct section_offsets *);
52130812Smarcel
53130812Smarcelstatic void som_symfile_offsets (struct objfile *, struct section_addr_info *);
54130812Smarcel
55130812Smarcel/* FIXME: These should really be in a common header somewhere */
56130812Smarcel
57130812Smarcelextern void hpread_build_psymtabs (struct objfile *, int);
58130812Smarcel
59130812Smarcelextern void hpread_symfile_finish (struct objfile *);
60130812Smarcel
61130812Smarcelextern void hpread_symfile_init (struct objfile *);
62130812Smarcel
63130812Smarcelextern void do_pxdb (bfd *);
64130812Smarcel
65130812Smarcel/*
66130812Smarcel
67130812Smarcel   LOCAL FUNCTION
68130812Smarcel
69130812Smarcel   som_symtab_read -- read the symbol table of a SOM file
70130812Smarcel
71130812Smarcel   SYNOPSIS
72130812Smarcel
73130812Smarcel   void som_symtab_read (bfd *abfd, struct objfile *objfile,
74130812Smarcel   struct section_offsets *section_offsets)
75130812Smarcel
76130812Smarcel   DESCRIPTION
77130812Smarcel
78130812Smarcel   Given an open bfd, a base address to relocate symbols to, and a
79130812Smarcel   flag that specifies whether or not this bfd is for an executable
80130812Smarcel   or not (may be shared library for example), add all the global
81130812Smarcel   function and data symbols to the minimal symbol table.
82130812Smarcel */
83130812Smarcel
84130812Smarcelstatic void
85130812Smarcelsom_symtab_read (bfd *abfd, struct objfile *objfile,
86130812Smarcel		 struct section_offsets *section_offsets)
87130812Smarcel{
88130812Smarcel  unsigned int number_of_symbols;
89130812Smarcel  int val, dynamic;
90130812Smarcel  char *stringtab;
91130812Smarcel  asection *shlib_info;
92130812Smarcel  struct symbol_dictionary_record *buf, *bufp, *endbufp;
93130812Smarcel  char *symname;
94130812Smarcel  CONST int symsize = sizeof (struct symbol_dictionary_record);
95130812Smarcel  CORE_ADDR text_offset, data_offset;
96130812Smarcel
97130812Smarcel
98130812Smarcel  text_offset = ANOFFSET (section_offsets, 0);
99130812Smarcel  data_offset = ANOFFSET (section_offsets, 1);
100130812Smarcel
101130812Smarcel  number_of_symbols = bfd_get_symcount (abfd);
102130812Smarcel
103130812Smarcel  /* FIXME (alloca): could be quite large. */
104130812Smarcel  buf = alloca (symsize * number_of_symbols);
105130812Smarcel  bfd_seek (abfd, obj_som_sym_filepos (abfd), SEEK_SET);
106130812Smarcel  val = bfd_bread (buf, symsize * number_of_symbols, abfd);
107130812Smarcel  if (val != symsize * number_of_symbols)
108130812Smarcel    error ("Couldn't read symbol dictionary!");
109130812Smarcel
110130812Smarcel  /* FIXME (alloca): could be quite large. */
111130812Smarcel  stringtab = alloca (obj_som_stringtab_size (abfd));
112130812Smarcel  bfd_seek (abfd, obj_som_str_filepos (abfd), SEEK_SET);
113130812Smarcel  val = bfd_bread (stringtab, obj_som_stringtab_size (abfd), abfd);
114130812Smarcel  if (val != obj_som_stringtab_size (abfd))
115130812Smarcel    error ("Can't read in HP string table.");
116130812Smarcel
117130812Smarcel  /* We need to determine if objfile is a dynamic executable (so we
118130812Smarcel     can do the right thing for ST_ENTRY vs ST_CODE symbols).
119130812Smarcel
120130812Smarcel     There's nothing in the header which easily allows us to do
121130812Smarcel     this.
122130812Smarcel
123130812Smarcel     This code used to rely upon the existence of a $SHLIB_INFO$
124130812Smarcel     section to make this determination.  HP claims that it is
125130812Smarcel     more accurate to check for a nonzero text offset, but they
126130812Smarcel     have not provided any information about why that test is
127130812Smarcel     more accurate.  */
128130812Smarcel  dynamic = (text_offset != 0);
129130812Smarcel
130130812Smarcel  endbufp = buf + number_of_symbols;
131130812Smarcel  for (bufp = buf; bufp < endbufp; ++bufp)
132130812Smarcel    {
133130812Smarcel      enum minimal_symbol_type ms_type;
134130812Smarcel
135130812Smarcel      QUIT;
136130812Smarcel
137130812Smarcel      switch (bufp->symbol_scope)
138130812Smarcel	{
139130812Smarcel	case SS_UNIVERSAL:
140130812Smarcel	case SS_EXTERNAL:
141130812Smarcel	  switch (bufp->symbol_type)
142130812Smarcel	    {
143130812Smarcel	    case ST_SYM_EXT:
144130812Smarcel	    case ST_ARG_EXT:
145130812Smarcel	      continue;
146130812Smarcel
147130812Smarcel	    case ST_CODE:
148130812Smarcel	    case ST_PRI_PROG:
149130812Smarcel	    case ST_SEC_PROG:
150130812Smarcel	    case ST_MILLICODE:
151130812Smarcel	      symname = bufp->name.n_strx + stringtab;
152130812Smarcel	      ms_type = mst_text;
153130812Smarcel	      bufp->symbol_value += text_offset;
154130812Smarcel	      bufp->symbol_value = SMASH_TEXT_ADDRESS (bufp->symbol_value);
155130812Smarcel	      break;
156130812Smarcel
157130812Smarcel	    case ST_ENTRY:
158130812Smarcel	      symname = bufp->name.n_strx + stringtab;
159130812Smarcel	      /* For a dynamic executable, ST_ENTRY symbols are
160130812Smarcel	         the stubs, while the ST_CODE symbol is the real
161130812Smarcel	         function.  */
162130812Smarcel	      if (dynamic)
163130812Smarcel		ms_type = mst_solib_trampoline;
164130812Smarcel	      else
165130812Smarcel		ms_type = mst_text;
166130812Smarcel	      bufp->symbol_value += text_offset;
167130812Smarcel	      bufp->symbol_value = SMASH_TEXT_ADDRESS (bufp->symbol_value);
168130812Smarcel	      break;
169130812Smarcel
170130812Smarcel	    case ST_STUB:
171130812Smarcel	      symname = bufp->name.n_strx + stringtab;
172130812Smarcel	      ms_type = mst_solib_trampoline;
173130812Smarcel	      bufp->symbol_value += text_offset;
174130812Smarcel	      bufp->symbol_value = SMASH_TEXT_ADDRESS (bufp->symbol_value);
175130812Smarcel	      break;
176130812Smarcel
177130812Smarcel	    case ST_DATA:
178130812Smarcel	      symname = bufp->name.n_strx + stringtab;
179130812Smarcel	      bufp->symbol_value += data_offset;
180130812Smarcel	      ms_type = mst_data;
181130812Smarcel	      break;
182130812Smarcel	    default:
183130812Smarcel	      continue;
184130812Smarcel	    }
185130812Smarcel	  break;
186130812Smarcel
187130812Smarcel#if 0
188130812Smarcel	  /* SS_GLOBAL and SS_LOCAL are two names for the same thing (!).  */
189130812Smarcel	case SS_GLOBAL:
190130812Smarcel#endif
191130812Smarcel	case SS_LOCAL:
192130812Smarcel	  switch (bufp->symbol_type)
193130812Smarcel	    {
194130812Smarcel	    case ST_SYM_EXT:
195130812Smarcel	    case ST_ARG_EXT:
196130812Smarcel	      continue;
197130812Smarcel
198130812Smarcel	    case ST_CODE:
199130812Smarcel	      symname = bufp->name.n_strx + stringtab;
200130812Smarcel	      ms_type = mst_file_text;
201130812Smarcel	      bufp->symbol_value += text_offset;
202130812Smarcel	      bufp->symbol_value = SMASH_TEXT_ADDRESS (bufp->symbol_value);
203130812Smarcel
204130812Smarcel	    check_strange_names:
205130812Smarcel	      /* Utah GCC 2.5, FSF GCC 2.6 and later generate correct local
206130812Smarcel	         label prefixes for stabs, constant data, etc.  So we need
207130812Smarcel	         only filter out L$ symbols which are left in due to
208130812Smarcel	         limitations in how GAS generates SOM relocations.
209130812Smarcel
210130812Smarcel	         When linking in the HPUX C-library the HP linker has
211130812Smarcel	         the nasty habit of placing section symbols from the literal
212130812Smarcel	         subspaces in the middle of the program's text.  Filter
213130812Smarcel	         those out as best we can.  Check for first and last character
214130812Smarcel	         being '$'.
215130812Smarcel
216130812Smarcel	         And finally, the newer HP compilers emit crud like $PIC_foo$N
217130812Smarcel	         in some circumstance (PIC code I guess).  It's also claimed
218130812Smarcel	         that they emit D$ symbols too.  What stupidity.  */
219130812Smarcel	      if ((symname[0] == 'L' && symname[1] == '$')
220130812Smarcel	      || (symname[0] == '$' && symname[strlen (symname) - 1] == '$')
221130812Smarcel		  || (symname[0] == 'D' && symname[1] == '$')
222130812Smarcel		  || (strncmp (symname, "$PIC", 4) == 0))
223130812Smarcel		continue;
224130812Smarcel	      break;
225130812Smarcel
226130812Smarcel	    case ST_PRI_PROG:
227130812Smarcel	    case ST_SEC_PROG:
228130812Smarcel	    case ST_MILLICODE:
229130812Smarcel	      symname = bufp->name.n_strx + stringtab;
230130812Smarcel	      ms_type = mst_file_text;
231130812Smarcel	      bufp->symbol_value += text_offset;
232130812Smarcel	      bufp->symbol_value = SMASH_TEXT_ADDRESS (bufp->symbol_value);
233130812Smarcel	      break;
234130812Smarcel
235130812Smarcel	    case ST_ENTRY:
236130812Smarcel	      symname = bufp->name.n_strx + stringtab;
237130812Smarcel	      /* SS_LOCAL symbols in a shared library do not have
238130812Smarcel		 export stubs, so we do not have to worry about
239130812Smarcel		 using mst_file_text vs mst_solib_trampoline here like
240130812Smarcel		 we do for SS_UNIVERSAL and SS_EXTERNAL symbols above.  */
241130812Smarcel	      ms_type = mst_file_text;
242130812Smarcel	      bufp->symbol_value += text_offset;
243130812Smarcel	      bufp->symbol_value = SMASH_TEXT_ADDRESS (bufp->symbol_value);
244130812Smarcel	      break;
245130812Smarcel
246130812Smarcel	    case ST_STUB:
247130812Smarcel	      symname = bufp->name.n_strx + stringtab;
248130812Smarcel	      ms_type = mst_solib_trampoline;
249130812Smarcel	      bufp->symbol_value += text_offset;
250130812Smarcel	      bufp->symbol_value = SMASH_TEXT_ADDRESS (bufp->symbol_value);
251130812Smarcel	      break;
252130812Smarcel
253130812Smarcel
254130812Smarcel	    case ST_DATA:
255130812Smarcel	      symname = bufp->name.n_strx + stringtab;
256130812Smarcel	      bufp->symbol_value += data_offset;
257130812Smarcel	      ms_type = mst_file_data;
258130812Smarcel	      goto check_strange_names;
259130812Smarcel
260130812Smarcel	    default:
261130812Smarcel	      continue;
262130812Smarcel	    }
263130812Smarcel	  break;
264130812Smarcel
265130812Smarcel	  /* This can happen for common symbols when -E is passed to the
266130812Smarcel	     final link.  No idea _why_ that would make the linker force
267130812Smarcel	     common symbols to have an SS_UNSAT scope, but it does.
268130812Smarcel
269130812Smarcel	     This also happens for weak symbols, but their type is
270130812Smarcel	     ST_DATA.  */
271130812Smarcel	case SS_UNSAT:
272130812Smarcel	  switch (bufp->symbol_type)
273130812Smarcel	    {
274130812Smarcel	    case ST_STORAGE:
275130812Smarcel	    case ST_DATA:
276130812Smarcel	      symname = bufp->name.n_strx + stringtab;
277130812Smarcel	      bufp->symbol_value += data_offset;
278130812Smarcel	      ms_type = mst_data;
279130812Smarcel	      break;
280130812Smarcel
281130812Smarcel	    default:
282130812Smarcel	      continue;
283130812Smarcel	    }
284130812Smarcel	  break;
285130812Smarcel
286130812Smarcel	default:
287130812Smarcel	  continue;
288130812Smarcel	}
289130812Smarcel
290130812Smarcel      if (bufp->name.n_strx > obj_som_stringtab_size (abfd))
291130812Smarcel	error ("Invalid symbol data; bad HP string table offset: %d",
292130812Smarcel	       bufp->name.n_strx);
293130812Smarcel
294130812Smarcel      prim_record_minimal_symbol (symname, bufp->symbol_value, ms_type,
295130812Smarcel				  objfile);
296130812Smarcel    }
297130812Smarcel}
298130812Smarcel
299130812Smarcel/* Scan and build partial symbols for a symbol file.
300130812Smarcel   We have been initialized by a call to som_symfile_init, which
301130812Smarcel   currently does nothing.
302130812Smarcel
303130812Smarcel   SECTION_OFFSETS is a set of offsets to apply to relocate the symbols
304130812Smarcel   in each section.  This is ignored, as it isn't needed for SOM.
305130812Smarcel
306130812Smarcel   MAINLINE is true if we are reading the main symbol
307130812Smarcel   table (as opposed to a shared lib or dynamically loaded file).
308130812Smarcel
309130812Smarcel   This function only does the minimum work necessary for letting the
310130812Smarcel   user "name" things symbolically; it does not read the entire symtab.
311130812Smarcel   Instead, it reads the external and static symbols and puts them in partial
312130812Smarcel   symbol tables.  When more extensive information is requested of a
313130812Smarcel   file, the corresponding partial symbol table is mutated into a full
314130812Smarcel   fledged symbol table by going back and reading the symbols
315130812Smarcel   for real.
316130812Smarcel
317130812Smarcel   We look for sections with specific names, to tell us what debug
318130812Smarcel   format to look for:  FIXME!!!
319130812Smarcel
320130812Smarcel   somstab_build_psymtabs() handles STABS symbols.
321130812Smarcel
322130812Smarcel   Note that SOM files have a "minimal" symbol table, which is vaguely
323130812Smarcel   reminiscent of a COFF symbol table, but has only the minimal information
324130812Smarcel   necessary for linking.  We process this also, and use the information to
325130812Smarcel   build gdb's minimal symbol table.  This gives us some minimal debugging
326130812Smarcel   capability even for files compiled without -g.  */
327130812Smarcel
328130812Smarcelstatic void
329130812Smarcelsom_symfile_read (struct objfile *objfile, int mainline)
330130812Smarcel{
331130812Smarcel  bfd *abfd = objfile->obfd;
332130812Smarcel  struct cleanup *back_to;
333130812Smarcel
334130812Smarcel  do_pxdb (symfile_bfd_open (objfile->name));
335130812Smarcel
336130812Smarcel  init_minimal_symbol_collection ();
337130812Smarcel  back_to = make_cleanup_discard_minimal_symbols ();
338130812Smarcel
339130812Smarcel  /* Read in the import list and the export list.  Currently
340130812Smarcel     the export list isn't used; the import list is used in
341130812Smarcel     hp-symtab-read.c to handle static vars declared in other
342130812Smarcel     shared libraries. */
343130812Smarcel  init_import_symbols (objfile);
344130812Smarcel#if 0				/* Export symbols not used today 1997-08-05 */
345130812Smarcel  init_export_symbols (objfile);
346130812Smarcel#else
347130812Smarcel  objfile->export_list = NULL;
348130812Smarcel  objfile->export_list_size = 0;
349130812Smarcel#endif
350130812Smarcel
351130812Smarcel  /* Process the normal SOM symbol table first.
352130812Smarcel     This reads in the DNTT and string table, but doesn't
353130812Smarcel     actually scan the DNTT. It does scan the linker symbol
354130812Smarcel     table and thus build up a "minimal symbol table". */
355130812Smarcel
356130812Smarcel  som_symtab_read (abfd, objfile, objfile->section_offsets);
357130812Smarcel
358130812Smarcel  /* Install any minimal symbols that have been collected as the current
359130812Smarcel     minimal symbols for this objfile.
360130812Smarcel     Further symbol-reading is done incrementally, file-by-file,
361130812Smarcel     in a step known as "psymtab-to-symtab" expansion. hp-symtab-read.c
362130812Smarcel     contains the code to do the actual DNTT scanning and symtab building. */
363130812Smarcel  install_minimal_symbols (objfile);
364130812Smarcel  do_cleanups (back_to);
365130812Smarcel
366130812Smarcel  /* Now read information from the stabs debug sections.
367130812Smarcel     This is a no-op for SOM.
368130812Smarcel     Perhaps it is intended for some kind of mixed STABS/SOM
369130812Smarcel     situation? */
370130812Smarcel  stabsect_build_psymtabs (objfile, mainline,
371130812Smarcel			   "$GDB_SYMBOLS$", "$GDB_STRINGS$", "$TEXT$");
372130812Smarcel
373130812Smarcel  /* Now read the native debug information.
374130812Smarcel     This builds the psymtab. This used to be done via a scan of
375130812Smarcel     the DNTT, but is now done via the PXDB-built quick-lookup tables
376130812Smarcel     together with a scan of the GNTT. See hp-psymtab-read.c. */
377130812Smarcel  hpread_build_psymtabs (objfile, mainline);
378130812Smarcel
379130812Smarcel  /* Force hppa-tdep.c to re-read the unwind descriptors.  */
380130812Smarcel  objfile->obj_private = NULL;
381130812Smarcel}
382130812Smarcel
383130812Smarcel/* Initialize anything that needs initializing when a completely new symbol
384130812Smarcel   file is specified (not just adding some symbols from another file, e.g. a
385130812Smarcel   shared library).
386130812Smarcel
387130812Smarcel   We reinitialize buildsym, since we may be reading stabs from a SOM file.  */
388130812Smarcel
389130812Smarcelstatic void
390130812Smarcelsom_new_init (struct objfile *ignore)
391130812Smarcel{
392130812Smarcel  stabsread_new_init ();
393130812Smarcel  buildsym_new_init ();
394130812Smarcel}
395130812Smarcel
396130812Smarcel/* Perform any local cleanups required when we are done with a particular
397130812Smarcel   objfile.  I.E, we are in the process of discarding all symbol information
398130812Smarcel   for an objfile, freeing up all memory held for it, and unlinking the
399130812Smarcel   objfile struct from the global list of known objfiles. */
400130812Smarcel
401130812Smarcelstatic void
402130812Smarcelsom_symfile_finish (struct objfile *objfile)
403130812Smarcel{
404130812Smarcel  if (objfile->sym_stab_info != NULL)
405130812Smarcel    {
406130812Smarcel      xmfree (objfile->md, objfile->sym_stab_info);
407130812Smarcel    }
408130812Smarcel  hpread_symfile_finish (objfile);
409130812Smarcel}
410130812Smarcel
411130812Smarcel/* SOM specific initialization routine for reading symbols.  */
412130812Smarcel
413130812Smarcelstatic void
414130812Smarcelsom_symfile_init (struct objfile *objfile)
415130812Smarcel{
416130812Smarcel  /* SOM objects may be reordered, so set OBJF_REORDERED.  If we
417130812Smarcel     find this causes a significant slowdown in gdb then we could
418130812Smarcel     set it in the debug symbol readers only when necessary.  */
419130812Smarcel  objfile->flags |= OBJF_REORDERED;
420130812Smarcel  hpread_symfile_init (objfile);
421130812Smarcel}
422130812Smarcel
423130812Smarcel/* SOM specific parsing routine for section offsets.
424130812Smarcel
425130812Smarcel   Plain and simple for now.  */
426130812Smarcel
427130812Smarcelstatic void
428130812Smarcelsom_symfile_offsets (struct objfile *objfile, struct section_addr_info *addrs)
429130812Smarcel{
430130812Smarcel  int i;
431130812Smarcel  CORE_ADDR text_addr;
432130812Smarcel
433130812Smarcel  objfile->num_sections = bfd_count_sections (objfile->obfd);
434130812Smarcel  objfile->section_offsets = (struct section_offsets *)
435130812Smarcel    obstack_alloc (&objfile->objfile_obstack,
436130812Smarcel		   SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));
437130812Smarcel
438130812Smarcel  /* FIXME: ezannoni 2000-04-20 The section names in SOM are not
439130812Smarcel     .text, .data, etc, but $TEXT$, $DATA$,... We should initialize
440130812Smarcel     SET_OFF_* from bfd. (See default_symfile_offsets()). But I don't
441130812Smarcel     know the correspondence between SOM sections and GDB's idea of
442130812Smarcel     section names. So for now we default to what is was before these
443130812Smarcel     changes.*/
444130812Smarcel  objfile->sect_index_text = 0;
445130812Smarcel  objfile->sect_index_data = 1;
446130812Smarcel  objfile->sect_index_bss = 2;
447130812Smarcel  objfile->sect_index_rodata = 3;
448130812Smarcel
449130812Smarcel  /* First see if we're a shared library.  If so, get the section
450130812Smarcel     offsets from the library, else get them from addrs.  */
451130812Smarcel  if (!som_solib_section_offsets (objfile, objfile->section_offsets))
452130812Smarcel    {
453130812Smarcel      /* Note: Here is OK to compare with ".text" because this is the
454130812Smarcel         name that gdb itself gives to that section, not the SOM
455130812Smarcel         name. */
456130812Smarcel      for (i = 0; i < objfile->num_sections && addrs->other[i].name; i++)
457130812Smarcel	if (strcmp (addrs->other[i].name, ".text") == 0)
458130812Smarcel	  break;
459130812Smarcel      text_addr = addrs->other[i].addr;
460130812Smarcel
461130812Smarcel      for (i = 0; i < objfile->num_sections; i++)
462130812Smarcel	(objfile->section_offsets)->offsets[i] = text_addr;
463130812Smarcel    }
464130812Smarcel}
465130812Smarcel
466130812Smarcel/* Read in and initialize the SOM import list which is present
467130812Smarcel   for all executables and shared libraries.  The import list
468130812Smarcel   consists of the symbols that are referenced in OBJFILE but
469130812Smarcel   not defined there.  (Variables that are imported are dealt
470130812Smarcel   with as "loc_indirect" vars.)
471130812Smarcel   Return value = number of import symbols read in. */
472130812Smarcelstatic int
473130812Smarcelinit_import_symbols (struct objfile *objfile)
474130812Smarcel{
475130812Smarcel  unsigned int import_list;
476130812Smarcel  unsigned int import_list_size;
477130812Smarcel  unsigned int string_table;
478130812Smarcel  unsigned int string_table_size;
479130812Smarcel  char *string_buffer;
480130812Smarcel  int i;
481130812Smarcel  int j;
482130812Smarcel  int k;
483130812Smarcel  asection *text_section;	/* section handle */
484130812Smarcel  unsigned int dl_header[12];	/* SOM executable header */
485130812Smarcel
486130812Smarcel  /* A struct for an entry in the SOM import list */
487130812Smarcel  typedef struct
488130812Smarcel    {
489130812Smarcel      int name;			/* index into the string table */
490130812Smarcel      short dont_care1;		/* we don't use this */
491130812Smarcel      unsigned char type;	/* 0 = NULL, 2 = Data, 3 = Code, 7 = Storage, 13 = Plabel */
492130812Smarcel      unsigned int reserved2:8;	/* not used */
493130812Smarcel    }
494130812Smarcel  SomImportEntry;
495130812Smarcel
496130812Smarcel  /* We read 100 entries in at a time from the disk file. */
497130812Smarcel#define SOM_READ_IMPORTS_NUM         100
498130812Smarcel#define SOM_READ_IMPORTS_CHUNK_SIZE  (sizeof (SomImportEntry) * SOM_READ_IMPORTS_NUM)
499130812Smarcel  SomImportEntry buffer[SOM_READ_IMPORTS_NUM];
500130812Smarcel
501130812Smarcel  /* Initialize in case we error out */
502130812Smarcel  objfile->import_list = NULL;
503130812Smarcel  objfile->import_list_size = 0;
504130812Smarcel
505130812Smarcel  /* It doesn't work, for some reason, to read in space $TEXT$;
506130812Smarcel     the subspace $SHLIB_INFO$ has to be used.  Some BFD quirk? pai/1997-08-05 */
507130812Smarcel  text_section = bfd_get_section_by_name (objfile->obfd, "$SHLIB_INFO$");
508130812Smarcel  if (!text_section)
509130812Smarcel    return 0;
510130812Smarcel  /* Get the SOM executable header */
511130812Smarcel  bfd_get_section_contents (objfile->obfd, text_section, dl_header, 0, 12 * sizeof (int));
512130812Smarcel
513130812Smarcel  /* Check header version number for 10.x HP-UX */
514130812Smarcel  /* Currently we deal only with 10.x systems; on 9.x the version # is 89060912.
515130812Smarcel     FIXME: Change for future HP-UX releases and mods to the SOM executable format */
516130812Smarcel  if (dl_header[0] != 93092112)
517130812Smarcel    return 0;
518130812Smarcel
519130812Smarcel  import_list = dl_header[4];
520130812Smarcel  import_list_size = dl_header[5];
521130812Smarcel  if (!import_list_size)
522130812Smarcel    return 0;
523130812Smarcel  string_table = dl_header[10];
524130812Smarcel  string_table_size = dl_header[11];
525130812Smarcel  if (!string_table_size)
526130812Smarcel    return 0;
527130812Smarcel
528130812Smarcel  /* Suck in SOM string table */
529130812Smarcel  string_buffer = (char *) xmalloc (string_table_size);
530130812Smarcel  bfd_get_section_contents (objfile->obfd, text_section, string_buffer,
531130812Smarcel			    string_table, string_table_size);
532130812Smarcel
533130812Smarcel  /* Allocate import list in the psymbol obstack; this has nothing
534130812Smarcel     to do with psymbols, just a matter of convenience.  We want the
535130812Smarcel     import list to be freed when the objfile is deallocated */
536130812Smarcel  objfile->import_list
537130812Smarcel    = (ImportEntry *) obstack_alloc (&objfile->objfile_obstack,
538130812Smarcel				   import_list_size * sizeof (ImportEntry));
539130812Smarcel
540130812Smarcel  /* Read in the import entries, a bunch at a time */
541130812Smarcel  for (j = 0, k = 0;
542130812Smarcel       j < (import_list_size / SOM_READ_IMPORTS_NUM);
543130812Smarcel       j++)
544130812Smarcel    {
545130812Smarcel      bfd_get_section_contents (objfile->obfd, text_section, buffer,
546130812Smarcel			      import_list + j * SOM_READ_IMPORTS_CHUNK_SIZE,
547130812Smarcel				SOM_READ_IMPORTS_CHUNK_SIZE);
548130812Smarcel      for (i = 0; i < SOM_READ_IMPORTS_NUM; i++, k++)
549130812Smarcel	{
550130812Smarcel	  if (buffer[i].type != (unsigned char) 0)
551130812Smarcel	    {
552130812Smarcel	      objfile->import_list[k]
553130812Smarcel		= (char *) obstack_alloc (&objfile->objfile_obstack, strlen (string_buffer + buffer[i].name) + 1);
554130812Smarcel	      strcpy (objfile->import_list[k], string_buffer + buffer[i].name);
555130812Smarcel	      /* Some day we might want to record the type and other information too */
556130812Smarcel	    }
557130812Smarcel	  else			/* null type */
558130812Smarcel	    objfile->import_list[k] = NULL;
559130812Smarcel
560130812Smarcel	}
561130812Smarcel    }
562130812Smarcel
563130812Smarcel  /* Get the leftovers */
564130812Smarcel  if (k < import_list_size)
565130812Smarcel    bfd_get_section_contents (objfile->obfd, text_section, buffer,
566130812Smarcel			      import_list + k * sizeof (SomImportEntry),
567130812Smarcel			  (import_list_size - k) * sizeof (SomImportEntry));
568130812Smarcel  for (i = 0; k < import_list_size; i++, k++)
569130812Smarcel    {
570130812Smarcel      if (buffer[i].type != (unsigned char) 0)
571130812Smarcel	{
572130812Smarcel	  objfile->import_list[k]
573130812Smarcel	    = (char *) obstack_alloc (&objfile->objfile_obstack, strlen (string_buffer + buffer[i].name) + 1);
574130812Smarcel	  strcpy (objfile->import_list[k], string_buffer + buffer[i].name);
575130812Smarcel	  /* Some day we might want to record the type and other information too */
576130812Smarcel	}
577130812Smarcel      else
578130812Smarcel	objfile->import_list[k] = NULL;
579130812Smarcel    }
580130812Smarcel
581130812Smarcel  objfile->import_list_size = import_list_size;
582130812Smarcel  xfree (string_buffer);
583130812Smarcel  return import_list_size;
584130812Smarcel}
585130812Smarcel
586130812Smarcel/* Read in and initialize the SOM export list which is present
587130812Smarcel   for all executables and shared libraries.  The import list
588130812Smarcel   consists of the symbols that are referenced in OBJFILE but
589130812Smarcel   not defined there.  (Variables that are imported are dealt
590130812Smarcel   with as "loc_indirect" vars.)
591130812Smarcel   Return value = number of import symbols read in. */
592130812Smarcelint
593130812Smarcelinit_export_symbols (struct objfile *objfile)
594130812Smarcel{
595130812Smarcel  unsigned int export_list;
596130812Smarcel  unsigned int export_list_size;
597130812Smarcel  unsigned int string_table;
598130812Smarcel  unsigned int string_table_size;
599130812Smarcel  char *string_buffer;
600130812Smarcel  int i;
601130812Smarcel  int j;
602130812Smarcel  int k;
603130812Smarcel  asection *text_section;	/* section handle */
604130812Smarcel  unsigned int dl_header[12];	/* SOM executable header */
605130812Smarcel
606130812Smarcel  /* A struct for an entry in the SOM export list */
607130812Smarcel  typedef struct
608130812Smarcel    {
609130812Smarcel      int next;			/* for hash table use -- we don't use this */
610130812Smarcel      int name;			/* index into string table */
611130812Smarcel      int value;		/* offset or plabel */
612130812Smarcel      int dont_care1;		/* not used */
613130812Smarcel      unsigned char type;	/* 0 = NULL, 2 = Data, 3 = Code, 7 = Storage, 13 = Plabel */
614130812Smarcel      char dont_care2;		/* not used */
615130812Smarcel      short dont_care3;		/* not used */
616130812Smarcel    }
617130812Smarcel  SomExportEntry;
618130812Smarcel
619130812Smarcel  /* We read 100 entries in at a time from the disk file. */
620130812Smarcel#define SOM_READ_EXPORTS_NUM         100
621130812Smarcel#define SOM_READ_EXPORTS_CHUNK_SIZE  (sizeof (SomExportEntry) * SOM_READ_EXPORTS_NUM)
622130812Smarcel  SomExportEntry buffer[SOM_READ_EXPORTS_NUM];
623130812Smarcel
624130812Smarcel  /* Initialize in case we error out */
625130812Smarcel  objfile->export_list = NULL;
626130812Smarcel  objfile->export_list_size = 0;
627130812Smarcel
628130812Smarcel  /* It doesn't work, for some reason, to read in space $TEXT$;
629130812Smarcel     the subspace $SHLIB_INFO$ has to be used.  Some BFD quirk? pai/1997-08-05 */
630130812Smarcel  text_section = bfd_get_section_by_name (objfile->obfd, "$SHLIB_INFO$");
631130812Smarcel  if (!text_section)
632130812Smarcel    return 0;
633130812Smarcel  /* Get the SOM executable header */
634130812Smarcel  bfd_get_section_contents (objfile->obfd, text_section, dl_header, 0, 12 * sizeof (int));
635130812Smarcel
636130812Smarcel  /* Check header version number for 10.x HP-UX */
637130812Smarcel  /* Currently we deal only with 10.x systems; on 9.x the version # is 89060912.
638130812Smarcel     FIXME: Change for future HP-UX releases and mods to the SOM executable format */
639130812Smarcel  if (dl_header[0] != 93092112)
640130812Smarcel    return 0;
641130812Smarcel
642130812Smarcel  export_list = dl_header[8];
643130812Smarcel  export_list_size = dl_header[9];
644130812Smarcel  if (!export_list_size)
645130812Smarcel    return 0;
646130812Smarcel  string_table = dl_header[10];
647130812Smarcel  string_table_size = dl_header[11];
648130812Smarcel  if (!string_table_size)
649130812Smarcel    return 0;
650130812Smarcel
651130812Smarcel  /* Suck in SOM string table */
652130812Smarcel  string_buffer = (char *) xmalloc (string_table_size);
653130812Smarcel  bfd_get_section_contents (objfile->obfd, text_section, string_buffer,
654130812Smarcel			    string_table, string_table_size);
655130812Smarcel
656130812Smarcel  /* Allocate export list in the psymbol obstack; this has nothing
657130812Smarcel     to do with psymbols, just a matter of convenience.  We want the
658130812Smarcel     export list to be freed when the objfile is deallocated */
659130812Smarcel  objfile->export_list
660130812Smarcel    = (ExportEntry *) obstack_alloc (&objfile->objfile_obstack,
661130812Smarcel				   export_list_size * sizeof (ExportEntry));
662130812Smarcel
663130812Smarcel  /* Read in the export entries, a bunch at a time */
664130812Smarcel  for (j = 0, k = 0;
665130812Smarcel       j < (export_list_size / SOM_READ_EXPORTS_NUM);
666130812Smarcel       j++)
667130812Smarcel    {
668130812Smarcel      bfd_get_section_contents (objfile->obfd, text_section, buffer,
669130812Smarcel			      export_list + j * SOM_READ_EXPORTS_CHUNK_SIZE,
670130812Smarcel				SOM_READ_EXPORTS_CHUNK_SIZE);
671130812Smarcel      for (i = 0; i < SOM_READ_EXPORTS_NUM; i++, k++)
672130812Smarcel	{
673130812Smarcel	  if (buffer[i].type != (unsigned char) 0)
674130812Smarcel	    {
675130812Smarcel	      objfile->export_list[k].name
676130812Smarcel		= (char *) obstack_alloc (&objfile->objfile_obstack, strlen (string_buffer + buffer[i].name) + 1);
677130812Smarcel	      strcpy (objfile->export_list[k].name, string_buffer + buffer[i].name);
678130812Smarcel	      objfile->export_list[k].address = buffer[i].value;
679130812Smarcel	      /* Some day we might want to record the type and other information too */
680130812Smarcel	    }
681130812Smarcel	  else
682130812Smarcel	    /* null type */
683130812Smarcel	    {
684130812Smarcel	      objfile->export_list[k].name = NULL;
685130812Smarcel	      objfile->export_list[k].address = 0;
686130812Smarcel	    }
687130812Smarcel	}
688130812Smarcel    }
689130812Smarcel
690130812Smarcel  /* Get the leftovers */
691130812Smarcel  if (k < export_list_size)
692130812Smarcel    bfd_get_section_contents (objfile->obfd, text_section, buffer,
693130812Smarcel			      export_list + k * sizeof (SomExportEntry),
694130812Smarcel			  (export_list_size - k) * sizeof (SomExportEntry));
695130812Smarcel  for (i = 0; k < export_list_size; i++, k++)
696130812Smarcel    {
697130812Smarcel      if (buffer[i].type != (unsigned char) 0)
698130812Smarcel	{
699130812Smarcel	  objfile->export_list[k].name
700130812Smarcel	    = (char *) obstack_alloc (&objfile->objfile_obstack, strlen (string_buffer + buffer[i].name) + 1);
701130812Smarcel	  strcpy (objfile->export_list[k].name, string_buffer + buffer[i].name);
702130812Smarcel	  /* Some day we might want to record the type and other information too */
703130812Smarcel	  objfile->export_list[k].address = buffer[i].value;
704130812Smarcel	}
705130812Smarcel      else
706130812Smarcel	{
707130812Smarcel	  objfile->export_list[k].name = NULL;
708130812Smarcel	  objfile->export_list[k].address = 0;
709130812Smarcel	}
710130812Smarcel    }
711130812Smarcel
712130812Smarcel  objfile->export_list_size = export_list_size;
713130812Smarcel  xfree (string_buffer);
714130812Smarcel  return export_list_size;
715130812Smarcel}
716130812Smarcel
717130812Smarcel
718130812Smarcel
719130812Smarcel/* Register that we are able to handle SOM object file formats.  */
720130812Smarcel
721130812Smarcelstatic struct sym_fns som_sym_fns =
722130812Smarcel{
723130812Smarcel  bfd_target_som_flavour,
724130812Smarcel  som_new_init,			/* sym_new_init: init anything gbl to entire symtab */
725130812Smarcel  som_symfile_init,		/* sym_init: read initial info, setup for sym_read() */
726130812Smarcel  som_symfile_read,		/* sym_read: read a symbol file into symtab */
727130812Smarcel  som_symfile_finish,		/* sym_finish: finished with file, cleanup */
728130812Smarcel  som_symfile_offsets,		/* sym_offsets:  Translate ext. to int. relocation */
729130812Smarcel  NULL				/* next: pointer to next struct sym_fns */
730130812Smarcel};
731130812Smarcel
732130812Smarcelvoid
733130812Smarcel_initialize_somread (void)
734130812Smarcel{
735130812Smarcel  add_symtab_fns (&som_sym_fns);
736130812Smarcel}
737