1130812Smarcel/* Read AIX xcoff symbol tables and convert to internal format, for GDB.
2130812Smarcel   Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
3130812Smarcel   1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
4130812Smarcel   Free Software Foundation, Inc.
5130812Smarcel   Derived from coffread.c, dbxread.c, and a lot of hacking.
6130812Smarcel   Contributed by IBM Corporation.
7130812Smarcel
8130812Smarcel   This file is part of GDB.
9130812Smarcel
10130812Smarcel   This program is free software; you can redistribute it and/or modify
11130812Smarcel   it under the terms of the GNU General Public License as published by
12130812Smarcel   the Free Software Foundation; either version 2 of the License, or
13130812Smarcel   (at your option) any later version.
14130812Smarcel
15130812Smarcel   This program is distributed in the hope that it will be useful,
16130812Smarcel   but WITHOUT ANY WARRANTY; without even the implied warranty of
17130812Smarcel   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18130812Smarcel   GNU General Public License for more details.
19130812Smarcel
20130812Smarcel   You should have received a copy of the GNU General Public License
21130812Smarcel   along with this program; if not, write to the Free Software
22130812Smarcel   Foundation, Inc., 59 Temple Place - Suite 330,
23130812Smarcel   Boston, MA 02111-1307, USA.  */
24130812Smarcel
25130812Smarcel#include "defs.h"
26130812Smarcel#include "bfd.h"
27130812Smarcel
28130812Smarcel#include <sys/types.h>
29130812Smarcel#include <fcntl.h>
30130812Smarcel#include <ctype.h>
31130812Smarcel#include "gdb_string.h"
32130812Smarcel
33130812Smarcel#include <sys/param.h>
34130812Smarcel#ifndef	NO_SYS_FILE
35130812Smarcel#include <sys/file.h>
36130812Smarcel#endif
37130812Smarcel#include "gdb_stat.h"
38130812Smarcel
39130812Smarcel#include "coff/internal.h"
40130812Smarcel#include "libcoff.h"		/* FIXME, internal data from BFD */
41130812Smarcel#include "coff/xcoff.h"
42130812Smarcel#include "libxcoff.h"
43130812Smarcel#include "coff/rs6000.h"
44130812Smarcel
45130812Smarcel#include "symtab.h"
46130812Smarcel#include "gdbtypes.h"
47130812Smarcel/* FIXME: ezannoni/2004-02-13 Verify if the include below is really needed.  */
48130812Smarcel#include "symfile.h"
49130812Smarcel#include "objfiles.h"
50130812Smarcel#include "buildsym.h"
51130812Smarcel#include "stabsread.h"
52130812Smarcel#include "expression.h"
53130812Smarcel#include "complaints.h"
54130812Smarcel
55130812Smarcel#include "gdb-stabs.h"
56130812Smarcel
57130812Smarcel/* For interface with stabsread.c.  */
58130812Smarcel#include "aout/stab_gnu.h"
59130812Smarcel
60130812Smarcel
61130812Smarcel/* We put a pointer to this structure in the read_symtab_private field
62130812Smarcel   of the psymtab.  */
63130812Smarcel
64130812Smarcelstruct symloc
65130812Smarcel  {
66130812Smarcel
67130812Smarcel    /* First symbol number for this file.  */
68130812Smarcel
69130812Smarcel    int first_symnum;
70130812Smarcel
71130812Smarcel    /* Number of symbols in the section of the symbol table devoted to
72130812Smarcel       this file's symbols (actually, the section bracketed may contain
73130812Smarcel       more than just this file's symbols).  If numsyms is 0, the only
74130812Smarcel       reason for this thing's existence is the dependency list.  Nothing
75130812Smarcel       else will happen when it is read in.  */
76130812Smarcel
77130812Smarcel    int numsyms;
78130812Smarcel
79130812Smarcel    /* Position of the start of the line number information for this psymtab.  */
80130812Smarcel    unsigned int lineno_off;
81130812Smarcel  };
82130812Smarcel
83130812Smarcel/* Remember what we deduced to be the source language of this psymtab. */
84130812Smarcel
85130812Smarcelstatic enum language psymtab_language = language_unknown;
86130812Smarcel
87130812Smarcel
88130812Smarcel/* Simplified internal version of coff symbol table information */
89130812Smarcel
90130812Smarcelstruct coff_symbol
91130812Smarcel  {
92130812Smarcel    char *c_name;
93130812Smarcel    int c_symnum;		/* symbol number of this entry */
94130812Smarcel    int c_naux;			/* 0 if syment only, 1 if syment + auxent */
95130812Smarcel    long c_value;
96130812Smarcel    unsigned char c_sclass;
97130812Smarcel    int c_secnum;
98130812Smarcel    unsigned int c_type;
99130812Smarcel  };
100130812Smarcel
101130812Smarcel/* last function's saved coff symbol `cs' */
102130812Smarcel
103130812Smarcelstatic struct coff_symbol fcn_cs_saved;
104130812Smarcel
105130812Smarcelstatic bfd *symfile_bfd;
106130812Smarcel
107130812Smarcel/* Core address of start and end of text of current source file.
108130812Smarcel   This is calculated from the first function seen after a C_FILE
109130812Smarcel   symbol. */
110130812Smarcel
111130812Smarcel
112130812Smarcelstatic CORE_ADDR cur_src_end_addr;
113130812Smarcel
114130812Smarcel/* Core address of the end of the first object file.  */
115130812Smarcel
116130812Smarcelstatic CORE_ADDR first_object_file_end;
117130812Smarcel
118130812Smarcel/* initial symbol-table-debug-string vector length */
119130812Smarcel
120130812Smarcel#define	INITIAL_STABVECTOR_LENGTH	40
121130812Smarcel
122130812Smarcel/* Nonzero if within a function (so symbols should be local,
123130812Smarcel   if nothing says specifically).  */
124130812Smarcel
125130812Smarcelint within_function;
126130812Smarcel
127130812Smarcel/* Size of a COFF symbol.  I think it is always 18, so I'm not sure
128130812Smarcel   there is any reason not to just use a #define, but might as well
129130812Smarcel   ask BFD for the size and store it here, I guess.  */
130130812Smarcel
131130812Smarcelstatic unsigned local_symesz;
132130812Smarcel
133130812Smarcelstruct coff_symfile_info
134130812Smarcel  {
135130812Smarcel    file_ptr min_lineno_offset;	/* Where in file lowest line#s are */
136130812Smarcel    file_ptr max_lineno_offset;	/* 1+last byte of line#s in file */
137130812Smarcel
138130812Smarcel    /* Pointer to the string table.  */
139130812Smarcel    char *strtbl;
140130812Smarcel
141130812Smarcel    /* Pointer to debug section.  */
142130812Smarcel    char *debugsec;
143130812Smarcel
144130812Smarcel    /* Pointer to the a.out symbol table.  */
145130812Smarcel    char *symtbl;
146130812Smarcel
147130812Smarcel    /* Number of symbols in symtbl.  */
148130812Smarcel    int symtbl_num_syms;
149130812Smarcel
150130812Smarcel    /* Offset in data section to TOC anchor.  */
151130812Smarcel    CORE_ADDR toc_offset;
152130812Smarcel  };
153130812Smarcel
154130812Smarcelstatic void
155130812Smarcelbf_notfound_complaint (void)
156130812Smarcel{
157130812Smarcel  complaint (&symfile_complaints, "line numbers off, `.bf' symbol not found");
158130812Smarcel}
159130812Smarcel
160130812Smarcelstatic void
161130812Smarcelef_complaint (int arg1)
162130812Smarcel{
163130812Smarcel  complaint (&symfile_complaints,
164130812Smarcel	     "Mismatched .ef symbol ignored starting at symnum %d", arg1);
165130812Smarcel}
166130812Smarcel
167130812Smarcelstatic void
168130812Smarceleb_complaint (int arg1)
169130812Smarcel{
170130812Smarcel  complaint (&symfile_complaints,
171130812Smarcel	     "Mismatched .eb symbol ignored starting at symnum %d", arg1);
172130812Smarcel}
173130812Smarcel
174130812Smarcelstatic void xcoff_initial_scan (struct objfile *, int);
175130812Smarcel
176130812Smarcelstatic void scan_xcoff_symtab (struct objfile *);
177130812Smarcel
178130812Smarcelstatic char *xcoff_next_symbol_text (struct objfile *);
179130812Smarcel
180130812Smarcelstatic void record_include_begin (struct coff_symbol *);
181130812Smarcel
182130812Smarcelstatic void
183130812Smarcelenter_line_range (struct subfile *, unsigned, unsigned,
184130812Smarcel		  CORE_ADDR, CORE_ADDR, unsigned *);
185130812Smarcel
186130812Smarcelstatic void init_stringtab (bfd *, file_ptr, struct objfile *);
187130812Smarcel
188130812Smarcelstatic void xcoff_symfile_init (struct objfile *);
189130812Smarcel
190130812Smarcelstatic void xcoff_new_init (struct objfile *);
191130812Smarcel
192130812Smarcelstatic void xcoff_symfile_finish (struct objfile *);
193130812Smarcel
194130812Smarcelstatic void xcoff_symfile_offsets (struct objfile *,
195130812Smarcel				   struct section_addr_info *addrs);
196130812Smarcel
197130812Smarcelstatic char *coff_getfilename (union internal_auxent *, struct objfile *);
198130812Smarcel
199130812Smarcelstatic void read_symbol (struct internal_syment *, int);
200130812Smarcel
201130812Smarcelstatic int read_symbol_lineno (int);
202130812Smarcel
203130812Smarcelstatic CORE_ADDR read_symbol_nvalue (int);
204130812Smarcel
205130812Smarcelstatic struct symbol *process_xcoff_symbol (struct coff_symbol *,
206130812Smarcel					    struct objfile *);
207130812Smarcel
208130812Smarcelstatic void read_xcoff_symtab (struct partial_symtab *);
209130812Smarcel
210130812Smarcel#if 0
211130812Smarcelstatic void add_stab_to_list (char *, struct pending_stabs **);
212130812Smarcel#endif
213130812Smarcel
214130812Smarcelstatic int compare_lte (const void *, const void *);
215130812Smarcel
216130812Smarcelstatic struct linetable *arrange_linetable (struct linetable *);
217130812Smarcel
218130812Smarcelstatic void record_include_end (struct coff_symbol *);
219130812Smarcel
220130812Smarcelstatic void process_linenos (CORE_ADDR, CORE_ADDR);
221130812Smarcel
222130812Smarcel
223130812Smarcel/* Translate from a COFF section number (target_index) to a SECT_OFF_*
224130812Smarcel   code.  */
225130812Smarcelstatic int secnum_to_section (int, struct objfile *);
226130812Smarcelstatic asection *secnum_to_bfd_section (int, struct objfile *);
227130812Smarcel
228130812Smarcelstruct find_targ_sec_arg
229130812Smarcel  {
230130812Smarcel    int targ_index;
231130812Smarcel    int *resultp;
232130812Smarcel    asection **bfd_sect;
233130812Smarcel    struct objfile *objfile;
234130812Smarcel  };
235130812Smarcel
236130812Smarcelstatic void find_targ_sec (bfd *, asection *, void *);
237130812Smarcel
238130812Smarcelstatic void
239130812Smarcelfind_targ_sec (bfd *abfd, asection *sect, void *obj)
240130812Smarcel{
241130812Smarcel  struct find_targ_sec_arg *args = (struct find_targ_sec_arg *) obj;
242130812Smarcel  struct objfile *objfile = args->objfile;
243130812Smarcel  if (sect->target_index == args->targ_index)
244130812Smarcel    {
245130812Smarcel      /* This is the section.  Figure out what SECT_OFF_* code it is.  */
246130812Smarcel      if (bfd_get_section_flags (abfd, sect) & SEC_CODE)
247130812Smarcel	*args->resultp = SECT_OFF_TEXT (objfile);
248130812Smarcel      else if (bfd_get_section_flags (abfd, sect) & SEC_LOAD)
249130812Smarcel	*args->resultp = SECT_OFF_DATA (objfile);
250130812Smarcel      else
251130812Smarcel	*args->resultp = sect->index;
252130812Smarcel      *args->bfd_sect = sect;
253130812Smarcel    }
254130812Smarcel}
255130812Smarcel
256130812Smarcel/* Return the section number (SECT_OFF_*) that CS points to.  */
257130812Smarcelstatic int
258130812Smarcelsecnum_to_section (int secnum, struct objfile *objfile)
259130812Smarcel{
260130812Smarcel  int off = SECT_OFF_TEXT (objfile);
261130812Smarcel  asection *sect = NULL;
262130812Smarcel  struct find_targ_sec_arg args;
263130812Smarcel  args.targ_index = secnum;
264130812Smarcel  args.resultp = &off;
265130812Smarcel  args.bfd_sect = &sect;
266130812Smarcel  args.objfile = objfile;
267130812Smarcel  bfd_map_over_sections (objfile->obfd, find_targ_sec, &args);
268130812Smarcel  return off;
269130812Smarcel}
270130812Smarcel
271130812Smarcel/* Return the BFD section that CS points to.  */
272130812Smarcelstatic asection *
273130812Smarcelsecnum_to_bfd_section (int secnum, struct objfile *objfile)
274130812Smarcel{
275130812Smarcel  int off = SECT_OFF_TEXT (objfile);
276130812Smarcel  asection *sect = NULL;
277130812Smarcel  struct find_targ_sec_arg args;
278130812Smarcel  args.targ_index = secnum;
279130812Smarcel  args.resultp = &off;
280130812Smarcel  args.bfd_sect = &sect;
281130812Smarcel  args.objfile = objfile;
282130812Smarcel  bfd_map_over_sections (objfile->obfd, find_targ_sec, &args);
283130812Smarcel  return sect;
284130812Smarcel}
285130812Smarcel
286130812Smarcel/* add a given stab string into given stab vector. */
287130812Smarcel
288130812Smarcel#if 0
289130812Smarcel
290130812Smarcelstatic void
291130812Smarceladd_stab_to_list (char *stabname, struct pending_stabs **stabvector)
292130812Smarcel{
293130812Smarcel  if (*stabvector == NULL)
294130812Smarcel    {
295130812Smarcel      *stabvector = (struct pending_stabs *)
296130812Smarcel	xmalloc (sizeof (struct pending_stabs) +
297130812Smarcel		 INITIAL_STABVECTOR_LENGTH * sizeof (char *));
298130812Smarcel      (*stabvector)->count = 0;
299130812Smarcel      (*stabvector)->length = INITIAL_STABVECTOR_LENGTH;
300130812Smarcel    }
301130812Smarcel  else if ((*stabvector)->count >= (*stabvector)->length)
302130812Smarcel    {
303130812Smarcel      (*stabvector)->length += INITIAL_STABVECTOR_LENGTH;
304130812Smarcel      *stabvector = (struct pending_stabs *)
305130812Smarcel	xrealloc ((char *) *stabvector, sizeof (struct pending_stabs) +
306130812Smarcel		    (*stabvector)->length * sizeof (char *));
307130812Smarcel    }
308130812Smarcel  (*stabvector)->stab[(*stabvector)->count++] = stabname;
309130812Smarcel}
310130812Smarcel
311130812Smarcel#endif
312130812Smarcel/* *INDENT-OFF* */
313130812Smarcel/* Linenos are processed on a file-by-file basis.
314130812Smarcel
315130812Smarcel   Two reasons:
316130812Smarcel
317130812Smarcel   1) xlc (IBM's native c compiler) postpones static function code
318130812Smarcel   emission to the end of a compilation unit. This way it can
319130812Smarcel   determine if those functions (statics) are needed or not, and
320130812Smarcel   can do some garbage collection (I think). This makes line
321130812Smarcel   numbers and corresponding addresses unordered, and we end up
322130812Smarcel   with a line table like:
323130812Smarcel
324130812Smarcel
325130812Smarcel   lineno       addr
326130812Smarcel   foo()          10    0x100
327130812Smarcel   20   0x200
328130812Smarcel   30   0x300
329130812Smarcel
330130812Smarcel   foo3()         70    0x400
331130812Smarcel   80   0x500
332130812Smarcel   90   0x600
333130812Smarcel
334130812Smarcel   static foo2()
335130812Smarcel   40   0x700
336130812Smarcel   50   0x800
337130812Smarcel   60   0x900
338130812Smarcel
339130812Smarcel   and that breaks gdb's binary search on line numbers, if the
340130812Smarcel   above table is not sorted on line numbers. And that sort
341130812Smarcel   should be on function based, since gcc can emit line numbers
342130812Smarcel   like:
343130812Smarcel
344130812Smarcel   10   0x100   - for the init/test part of a for stmt.
345130812Smarcel   20   0x200
346130812Smarcel   30   0x300
347130812Smarcel   10   0x400   - for the increment part of a for stmt.
348130812Smarcel
349130812Smarcel   arrange_linetable() will do this sorting.
350130812Smarcel
351130812Smarcel   2)   aix symbol table might look like:
352130812Smarcel
353130812Smarcel   c_file               // beginning of a new file
354130812Smarcel   .bi          // beginning of include file
355130812Smarcel   .ei          // end of include file
356130812Smarcel   .bi
357130812Smarcel   .ei
358130812Smarcel
359130812Smarcel   basically, .bi/.ei pairs do not necessarily encapsulate
360130812Smarcel   their scope. They need to be recorded, and processed later
361130812Smarcel   on when we come the end of the compilation unit.
362130812Smarcel   Include table (inclTable) and process_linenos() handle
363130812Smarcel   that.  */
364130812Smarcel/* *INDENT-ON* */
365130812Smarcel
366130812Smarcel
367130812Smarcel
368130812Smarcel/* compare line table entry addresses. */
369130812Smarcel
370130812Smarcelstatic int
371130812Smarcelcompare_lte (const void *lte1p, const void *lte2p)
372130812Smarcel{
373130812Smarcel  struct linetable_entry *lte1 = (struct linetable_entry *) lte1p;
374130812Smarcel  struct linetable_entry *lte2 = (struct linetable_entry *) lte2p;
375130812Smarcel  return lte1->pc - lte2->pc;
376130812Smarcel}
377130812Smarcel
378130812Smarcel/* Given a line table with function entries are marked, arrange its functions
379130812Smarcel   in ascending order and strip off function entry markers and return it in
380130812Smarcel   a newly created table. If the old one is good enough, return the old one. */
381130812Smarcel/* FIXME: I think all this stuff can be replaced by just passing
382130812Smarcel   sort_linevec = 1 to end_symtab.  */
383130812Smarcel
384130812Smarcelstatic struct linetable *
385130812Smarcelarrange_linetable (struct linetable *oldLineTb)
386130812Smarcel{
387130812Smarcel  int ii, jj, newline,		/* new line count */
388130812Smarcel    function_count;		/* # of functions */
389130812Smarcel
390130812Smarcel  struct linetable_entry *fentry;	/* function entry vector */
391130812Smarcel  int fentry_size;		/* # of function entries */
392130812Smarcel  struct linetable *newLineTb;	/* new line table */
393130812Smarcel
394130812Smarcel#define NUM_OF_FUNCTIONS 20
395130812Smarcel
396130812Smarcel  fentry_size = NUM_OF_FUNCTIONS;
397130812Smarcel  fentry = (struct linetable_entry *)
398130812Smarcel    xmalloc (fentry_size * sizeof (struct linetable_entry));
399130812Smarcel
400130812Smarcel  for (function_count = 0, ii = 0; ii < oldLineTb->nitems; ++ii)
401130812Smarcel    {
402130812Smarcel
403130812Smarcel      if (oldLineTb->item[ii].line == 0)
404130812Smarcel	{			/* function entry found. */
405130812Smarcel
406130812Smarcel	  if (function_count >= fentry_size)
407130812Smarcel	    {			/* make sure you have room. */
408130812Smarcel	      fentry_size *= 2;
409130812Smarcel	      fentry = (struct linetable_entry *)
410130812Smarcel		xrealloc (fentry, fentry_size * sizeof (struct linetable_entry));
411130812Smarcel	    }
412130812Smarcel	  fentry[function_count].line = ii;
413130812Smarcel	  fentry[function_count].pc = oldLineTb->item[ii].pc;
414130812Smarcel	  ++function_count;
415130812Smarcel	}
416130812Smarcel    }
417130812Smarcel
418130812Smarcel  if (function_count == 0)
419130812Smarcel    {
420130812Smarcel      xfree (fentry);
421130812Smarcel      return oldLineTb;
422130812Smarcel    }
423130812Smarcel  else if (function_count > 1)
424130812Smarcel    qsort (fentry, function_count, sizeof (struct linetable_entry), compare_lte);
425130812Smarcel
426130812Smarcel  /* allocate a new line table. */
427130812Smarcel  newLineTb = (struct linetable *)
428130812Smarcel    xmalloc
429130812Smarcel    (sizeof (struct linetable) +
430130812Smarcel    (oldLineTb->nitems - function_count) * sizeof (struct linetable_entry));
431130812Smarcel
432130812Smarcel  /* if line table does not start with a function beginning, copy up until
433130812Smarcel     a function begin. */
434130812Smarcel
435130812Smarcel  newline = 0;
436130812Smarcel  if (oldLineTb->item[0].line != 0)
437130812Smarcel    for (newline = 0;
438130812Smarcel    newline < oldLineTb->nitems && oldLineTb->item[newline].line; ++newline)
439130812Smarcel      newLineTb->item[newline] = oldLineTb->item[newline];
440130812Smarcel
441130812Smarcel  /* Now copy function lines one by one. */
442130812Smarcel
443130812Smarcel  for (ii = 0; ii < function_count; ++ii)
444130812Smarcel    {
445130812Smarcel      for (jj = fentry[ii].line + 1;
446130812Smarcel	   jj < oldLineTb->nitems && oldLineTb->item[jj].line != 0;
447130812Smarcel	   ++jj, ++newline)
448130812Smarcel	newLineTb->item[newline] = oldLineTb->item[jj];
449130812Smarcel    }
450130812Smarcel  xfree (fentry);
451130812Smarcel  newLineTb->nitems = oldLineTb->nitems - function_count;
452130812Smarcel  return newLineTb;
453130812Smarcel}
454130812Smarcel
455130812Smarcel/* include file support: C_BINCL/C_EINCL pairs will be kept in the
456130812Smarcel   following `IncludeChain'. At the end of each symtab (end_symtab),
457130812Smarcel   we will determine if we should create additional symtab's to
458130812Smarcel   represent if (the include files. */
459130812Smarcel
460130812Smarcel
461130812Smarceltypedef struct _inclTable
462130812Smarcel{
463130812Smarcel  char *name;			/* include filename */
464130812Smarcel
465130812Smarcel  /* Offsets to the line table.  end points to the last entry which is
466130812Smarcel     part of this include file.  */
467130812Smarcel  int begin, end;
468130812Smarcel
469130812Smarcel  struct subfile *subfile;
470130812Smarcel  unsigned funStartLine;	/* start line # of its function */
471130812Smarcel}
472130812SmarcelInclTable;
473130812Smarcel
474130812Smarcel#define	INITIAL_INCLUDE_TABLE_LENGTH	20
475130812Smarcelstatic InclTable *inclTable;	/* global include table */
476130812Smarcelstatic int inclIndx;		/* last entry to table */
477130812Smarcelstatic int inclLength;		/* table length */
478130812Smarcelstatic int inclDepth;		/* nested include depth */
479130812Smarcel
480130812Smarcelstatic void allocate_include_entry (void);
481130812Smarcel
482130812Smarcelstatic void
483130812Smarcelrecord_include_begin (struct coff_symbol *cs)
484130812Smarcel{
485130812Smarcel  if (inclDepth)
486130812Smarcel    {
487130812Smarcel      /* In xcoff, we assume include files cannot be nested (not in .c files
488130812Smarcel         of course, but in corresponding .s files.).  */
489130812Smarcel
490130812Smarcel      /* This can happen with old versions of GCC.
491130812Smarcel         GCC 2.3.3-930426 does not exhibit this on a test case which
492130812Smarcel         a user said produced the message for him.  */
493130812Smarcel      complaint (&symfile_complaints, "Nested C_BINCL symbols");
494130812Smarcel    }
495130812Smarcel  ++inclDepth;
496130812Smarcel
497130812Smarcel  allocate_include_entry ();
498130812Smarcel
499130812Smarcel  inclTable[inclIndx].name = cs->c_name;
500130812Smarcel  inclTable[inclIndx].begin = cs->c_value;
501130812Smarcel}
502130812Smarcel
503130812Smarcelstatic void
504130812Smarcelrecord_include_end (struct coff_symbol *cs)
505130812Smarcel{
506130812Smarcel  InclTable *pTbl;
507130812Smarcel
508130812Smarcel  if (inclDepth == 0)
509130812Smarcel    {
510130812Smarcel      complaint (&symfile_complaints, "Mismatched C_BINCL/C_EINCL pair");
511130812Smarcel    }
512130812Smarcel
513130812Smarcel  allocate_include_entry ();
514130812Smarcel
515130812Smarcel  pTbl = &inclTable[inclIndx];
516130812Smarcel  pTbl->end = cs->c_value;
517130812Smarcel
518130812Smarcel  --inclDepth;
519130812Smarcel  ++inclIndx;
520130812Smarcel}
521130812Smarcel
522130812Smarcelstatic void
523130812Smarcelallocate_include_entry (void)
524130812Smarcel{
525130812Smarcel  if (inclTable == NULL)
526130812Smarcel    {
527130812Smarcel      inclTable = (InclTable *)
528130812Smarcel	xmalloc (sizeof (InclTable) * INITIAL_INCLUDE_TABLE_LENGTH);
529130812Smarcel      memset (inclTable,
530130812Smarcel	      '\0', sizeof (InclTable) * INITIAL_INCLUDE_TABLE_LENGTH);
531130812Smarcel      inclLength = INITIAL_INCLUDE_TABLE_LENGTH;
532130812Smarcel      inclIndx = 0;
533130812Smarcel    }
534130812Smarcel  else if (inclIndx >= inclLength)
535130812Smarcel    {
536130812Smarcel      inclLength += INITIAL_INCLUDE_TABLE_LENGTH;
537130812Smarcel      inclTable = (InclTable *)
538130812Smarcel	xrealloc (inclTable, sizeof (InclTable) * inclLength);
539130812Smarcel      memset (inclTable + inclLength - INITIAL_INCLUDE_TABLE_LENGTH,
540130812Smarcel	      '\0', sizeof (InclTable) * INITIAL_INCLUDE_TABLE_LENGTH);
541130812Smarcel    }
542130812Smarcel}
543130812Smarcel
544130812Smarcel/* Global variable to pass the psymtab down to all the routines involved
545130812Smarcel   in psymtab to symtab processing.  */
546130812Smarcelstatic struct partial_symtab *this_symtab_psymtab;
547130812Smarcel
548130812Smarcel/* given the start and end addresses of a compilation unit (or a csect,
549130812Smarcel   at times) process its lines and create appropriate line vectors. */
550130812Smarcel
551130812Smarcelstatic void
552130812Smarcelprocess_linenos (CORE_ADDR start, CORE_ADDR end)
553130812Smarcel{
554130812Smarcel  int offset, ii;
555130812Smarcel  file_ptr max_offset =
556130812Smarcel  ((struct coff_symfile_info *) this_symtab_psymtab->objfile->sym_private)
557130812Smarcel  ->max_lineno_offset;
558130812Smarcel
559130812Smarcel  /* subfile structure for the main compilation unit.  */
560130812Smarcel  struct subfile main_subfile;
561130812Smarcel
562130812Smarcel  /* In the main source file, any time we see a function entry, we
563130812Smarcel     reset this variable to function's absolute starting line number.
564130812Smarcel     All the following line numbers in the function are relative to
565130812Smarcel     this, and we record absolute line numbers in record_line().  */
566130812Smarcel
567130812Smarcel  unsigned int main_source_baseline = 0;
568130812Smarcel
569130812Smarcel  unsigned *firstLine;
570130812Smarcel
571130812Smarcel  offset =
572130812Smarcel    ((struct symloc *) this_symtab_psymtab->read_symtab_private)->lineno_off;
573130812Smarcel  if (offset == 0)
574130812Smarcel    goto return_after_cleanup;
575130812Smarcel
576130812Smarcel  memset (&main_subfile, '\0', sizeof (main_subfile));
577130812Smarcel
578130812Smarcel  if (inclIndx == 0)
579130812Smarcel    /* All source lines were in the main source file. None in include files. */
580130812Smarcel
581130812Smarcel    enter_line_range (&main_subfile, offset, 0, start, end,
582130812Smarcel		      &main_source_baseline);
583130812Smarcel
584130812Smarcel  else
585130812Smarcel    {
586130812Smarcel      /* There was source with line numbers in include files.  */
587130812Smarcel
588130812Smarcel      int linesz =
589130812Smarcel	coff_data (this_symtab_psymtab->objfile->obfd)->local_linesz;
590130812Smarcel      main_source_baseline = 0;
591130812Smarcel
592130812Smarcel      for (ii = 0; ii < inclIndx; ++ii)
593130812Smarcel	{
594130812Smarcel	  struct subfile *tmpSubfile;
595130812Smarcel
596130812Smarcel	  /* If there is main file source before include file, enter it.  */
597130812Smarcel	  if (offset < inclTable[ii].begin)
598130812Smarcel	    {
599130812Smarcel	      enter_line_range
600130812Smarcel		(&main_subfile, offset, inclTable[ii].begin - linesz,
601130812Smarcel		 start, 0, &main_source_baseline);
602130812Smarcel	    }
603130812Smarcel
604130812Smarcel	  /* Have a new subfile for the include file.  */
605130812Smarcel
606130812Smarcel	  tmpSubfile = inclTable[ii].subfile =
607130812Smarcel	    (struct subfile *) xmalloc (sizeof (struct subfile));
608130812Smarcel
609130812Smarcel	  memset (tmpSubfile, '\0', sizeof (struct subfile));
610130812Smarcel	  firstLine = &(inclTable[ii].funStartLine);
611130812Smarcel
612130812Smarcel	  /* Enter include file's lines now.  */
613130812Smarcel	  enter_line_range (tmpSubfile, inclTable[ii].begin,
614130812Smarcel			    inclTable[ii].end, start, 0, firstLine);
615130812Smarcel
616130812Smarcel	  if (offset <= inclTable[ii].end)
617130812Smarcel	    offset = inclTable[ii].end + linesz;
618130812Smarcel	}
619130812Smarcel
620130812Smarcel      /* All the include files' line have been processed at this point.  Now,
621130812Smarcel         enter remaining lines of the main file, if any left.  */
622130812Smarcel      if (offset < max_offset + 1 - linesz)
623130812Smarcel	{
624130812Smarcel	  enter_line_range (&main_subfile, offset, 0, start, end,
625130812Smarcel			    &main_source_baseline);
626130812Smarcel	}
627130812Smarcel    }
628130812Smarcel
629130812Smarcel  /* Process main file's line numbers.  */
630130812Smarcel  if (main_subfile.line_vector)
631130812Smarcel    {
632130812Smarcel      struct linetable *lineTb, *lv;
633130812Smarcel
634130812Smarcel      lv = main_subfile.line_vector;
635130812Smarcel
636130812Smarcel      /* Line numbers are not necessarily ordered. xlc compilation will
637130812Smarcel         put static function to the end. */
638130812Smarcel
639130812Smarcel      lineTb = arrange_linetable (lv);
640130812Smarcel      if (lv == lineTb)
641130812Smarcel	{
642130812Smarcel	  current_subfile->line_vector = (struct linetable *)
643130812Smarcel	    xrealloc (lv, (sizeof (struct linetable)
644130812Smarcel			   + lv->nitems * sizeof (struct linetable_entry)));
645130812Smarcel	}
646130812Smarcel      else
647130812Smarcel	{
648130812Smarcel	  xfree (lv);
649130812Smarcel	  current_subfile->line_vector = lineTb;
650130812Smarcel	}
651130812Smarcel
652130812Smarcel      current_subfile->line_vector_length =
653130812Smarcel	current_subfile->line_vector->nitems;
654130812Smarcel    }
655130812Smarcel
656130812Smarcel  /* Now, process included files' line numbers.  */
657130812Smarcel
658130812Smarcel  for (ii = 0; ii < inclIndx; ++ii)
659130812Smarcel    {
660130812Smarcel      if ((inclTable[ii].subfile)->line_vector)		/* Useless if!!! FIXMEmgo */
661130812Smarcel	{
662130812Smarcel	  struct linetable *lineTb, *lv;
663130812Smarcel
664130812Smarcel	  lv = (inclTable[ii].subfile)->line_vector;
665130812Smarcel
666130812Smarcel	  /* Line numbers are not necessarily ordered. xlc compilation will
667130812Smarcel	     put static function to the end. */
668130812Smarcel
669130812Smarcel	  lineTb = arrange_linetable (lv);
670130812Smarcel
671130812Smarcel	  push_subfile ();
672130812Smarcel
673130812Smarcel	  /* For the same include file, we might want to have more than one
674130812Smarcel	     subfile.  This happens if we have something like:
675130812Smarcel
676130812Smarcel	     ......
677130812Smarcel	     #include "foo.h"
678130812Smarcel	     ......
679130812Smarcel	     #include "foo.h"
680130812Smarcel	     ......
681130812Smarcel
682130812Smarcel	     while foo.h including code in it. (stupid but possible)
683130812Smarcel	     Since start_subfile() looks at the name and uses an
684130812Smarcel	     existing one if finds, we need to provide a fake name and
685130812Smarcel	     fool it.  */
686130812Smarcel
687130812Smarcel#if 0
688130812Smarcel	  start_subfile (inclTable[ii].name, (char *) 0);
689130812Smarcel#else
690130812Smarcel	  {
691130812Smarcel	    /* Pick a fake name that will produce the same results as this
692130812Smarcel	       one when passed to deduce_language_from_filename.  Kludge on
693130812Smarcel	       top of kludge.  */
694130812Smarcel	    char *fakename = strrchr (inclTable[ii].name, '.');
695130812Smarcel	    if (fakename == NULL)
696130812Smarcel	      fakename = " ?";
697130812Smarcel	    start_subfile (fakename, (char *) 0);
698130812Smarcel	    xfree (current_subfile->name);
699130812Smarcel	  }
700130812Smarcel	  current_subfile->name = xstrdup (inclTable[ii].name);
701130812Smarcel#endif
702130812Smarcel
703130812Smarcel	  if (lv == lineTb)
704130812Smarcel	    {
705130812Smarcel	      current_subfile->line_vector =
706130812Smarcel		(struct linetable *) xrealloc
707130812Smarcel		(lv, (sizeof (struct linetable)
708130812Smarcel		      + lv->nitems * sizeof (struct linetable_entry)));
709130812Smarcel
710130812Smarcel	    }
711130812Smarcel	  else
712130812Smarcel	    {
713130812Smarcel	      xfree (lv);
714130812Smarcel	      current_subfile->line_vector = lineTb;
715130812Smarcel	    }
716130812Smarcel
717130812Smarcel	  current_subfile->line_vector_length =
718130812Smarcel	    current_subfile->line_vector->nitems;
719130812Smarcel	  start_subfile (pop_subfile (), (char *) 0);
720130812Smarcel	}
721130812Smarcel    }
722130812Smarcel
723130812Smarcelreturn_after_cleanup:
724130812Smarcel
725130812Smarcel  /* We don't want to keep alloc/free'ing the global include file table.  */
726130812Smarcel  inclIndx = 0;
727130812Smarcel
728130812Smarcel  /* Start with a fresh subfile structure for the next file.  */
729130812Smarcel  memset (&main_subfile, '\0', sizeof (struct subfile));
730130812Smarcel}
731130812Smarcel
732130812Smarcelvoid
733130812Smarcelaix_process_linenos (void)
734130812Smarcel{
735130812Smarcel  /* process line numbers and enter them into line vector */
736130812Smarcel  process_linenos (last_source_start_addr, cur_src_end_addr);
737130812Smarcel}
738130812Smarcel
739130812Smarcel
740130812Smarcel/* Enter a given range of lines into the line vector.
741130812Smarcel   can be called in the following two ways:
742130812Smarcel   enter_line_range (subfile, beginoffset, endoffset, startaddr, 0, firstLine)  or
743130812Smarcel   enter_line_range (subfile, beginoffset, 0, startaddr, endaddr, firstLine)
744130812Smarcel
745130812Smarcel   endoffset points to the last line table entry that we should pay
746130812Smarcel   attention to.  */
747130812Smarcel
748130812Smarcelstatic void
749130812Smarcelenter_line_range (struct subfile *subfile, unsigned beginoffset, unsigned endoffset,	/* offsets to line table */
750130812Smarcel		  CORE_ADDR startaddr,	/* offsets to line table */
751130812Smarcel		  CORE_ADDR endaddr, unsigned *firstLine)
752130812Smarcel{
753130812Smarcel  unsigned int curoffset;
754130812Smarcel  CORE_ADDR addr;
755130812Smarcel  void *ext_lnno;
756130812Smarcel  struct internal_lineno int_lnno;
757130812Smarcel  unsigned int limit_offset;
758130812Smarcel  bfd *abfd;
759130812Smarcel  int linesz;
760130812Smarcel
761130812Smarcel  if (endoffset == 0 && startaddr == 0 && endaddr == 0)
762130812Smarcel    return;
763130812Smarcel  curoffset = beginoffset;
764130812Smarcel  limit_offset =
765130812Smarcel    ((struct coff_symfile_info *) this_symtab_psymtab->objfile->sym_private)
766130812Smarcel    ->max_lineno_offset;
767130812Smarcel
768130812Smarcel  if (endoffset != 0)
769130812Smarcel    {
770130812Smarcel      if (endoffset >= limit_offset)
771130812Smarcel	{
772130812Smarcel	  complaint (&symfile_complaints,
773130812Smarcel		     "Bad line table offset in C_EINCL directive");
774130812Smarcel	  return;
775130812Smarcel	}
776130812Smarcel      limit_offset = endoffset;
777130812Smarcel    }
778130812Smarcel  else
779130812Smarcel    limit_offset -= 1;
780130812Smarcel
781130812Smarcel  abfd = this_symtab_psymtab->objfile->obfd;
782130812Smarcel  linesz = coff_data (abfd)->local_linesz;
783130812Smarcel  ext_lnno = alloca (linesz);
784130812Smarcel
785130812Smarcel  while (curoffset <= limit_offset)
786130812Smarcel    {
787130812Smarcel      bfd_seek (abfd, curoffset, SEEK_SET);
788130812Smarcel      bfd_bread (ext_lnno, linesz, abfd);
789130812Smarcel      bfd_coff_swap_lineno_in (abfd, ext_lnno, &int_lnno);
790130812Smarcel
791130812Smarcel      /* Find the address this line represents.  */
792130812Smarcel      addr = (int_lnno.l_lnno
793130812Smarcel	      ? int_lnno.l_addr.l_paddr
794130812Smarcel	      : read_symbol_nvalue (int_lnno.l_addr.l_symndx));
795130812Smarcel      addr += ANOFFSET (this_symtab_psymtab->objfile->section_offsets,
796130812Smarcel			SECT_OFF_TEXT (this_symtab_psymtab->objfile));
797130812Smarcel
798130812Smarcel      if (addr < startaddr || (endaddr && addr >= endaddr))
799130812Smarcel	return;
800130812Smarcel
801130812Smarcel      if (int_lnno.l_lnno == 0)
802130812Smarcel	{
803130812Smarcel	  *firstLine = read_symbol_lineno (int_lnno.l_addr.l_symndx);
804130812Smarcel	  record_line (subfile, 0, addr);
805130812Smarcel	  --(*firstLine);
806130812Smarcel	}
807130812Smarcel      else
808130812Smarcel	record_line (subfile, *firstLine + int_lnno.l_lnno, addr);
809130812Smarcel      curoffset += linesz;
810130812Smarcel    }
811130812Smarcel}
812130812Smarcel
813130812Smarcel
814130812Smarcel/* Save the vital information for use when closing off the current file.
815130812Smarcel   NAME is the file name the symbols came from, START_ADDR is the first
816130812Smarcel   text address for the file, and SIZE is the number of bytes of text.  */
817130812Smarcel
818130812Smarcel#define complete_symtab(name, start_addr) {	\
819130812Smarcel  last_source_file = savestring (name, strlen (name));	\
820130812Smarcel  last_source_start_addr = start_addr;			\
821130812Smarcel}
822130812Smarcel
823130812Smarcel
824130812Smarcel/* Refill the symbol table input buffer
825130812Smarcel   and set the variables that control fetching entries from it.
826130812Smarcel   Reports an error if no data available.
827130812Smarcel   This function can read past the end of the symbol table
828130812Smarcel   (into the string table) but this does no harm.  */
829130812Smarcel
830130812Smarcel/* Reading symbol table has to be fast! Keep the followings as macros, rather
831130812Smarcel   than functions. */
832130812Smarcel
833130812Smarcel#define	RECORD_MINIMAL_SYMBOL(NAME, ADDR, TYPE, SECTION, OBJFILE) \
834130812Smarcel{						\
835130812Smarcel  char *namestr;				\
836130812Smarcel  namestr = (NAME); \
837130812Smarcel  if (namestr[0] == '.') ++namestr; \
838130812Smarcel  prim_record_minimal_symbol_and_info (namestr, (ADDR), (TYPE), \
839130812Smarcel				       (char *)NULL, (SECTION), (asection *)NULL, (OBJFILE)); \
840130812Smarcel  misc_func_recorded = 1;					\
841130812Smarcel}
842130812Smarcel
843130812Smarcel
844130812Smarcel/* xcoff has static blocks marked in `.bs', `.es' pairs. They cannot be
845130812Smarcel   nested. At any given time, a symbol can only be in one static block.
846130812Smarcel   This is the base address of current static block, zero if non exists. */
847130812Smarcel
848130812Smarcelstatic int static_block_base = 0;
849130812Smarcel
850130812Smarcel/* Section number for the current static block.  */
851130812Smarcel
852130812Smarcelstatic int static_block_section = -1;
853130812Smarcel
854130812Smarcel/* true if space for symbol name has been allocated. */
855130812Smarcel
856130812Smarcelstatic int symname_alloced = 0;
857130812Smarcel
858130812Smarcel/* Next symbol to read.  Pointer into raw seething symbol table.  */
859130812Smarcel
860130812Smarcelstatic char *raw_symbol;
861130812Smarcel
862130812Smarcel/* This is the function which stabsread.c calls to get symbol
863130812Smarcel   continuations.  */
864130812Smarcel
865130812Smarcelstatic char *
866130812Smarcelxcoff_next_symbol_text (struct objfile *objfile)
867130812Smarcel{
868130812Smarcel  struct internal_syment symbol;
869130812Smarcel  char *retval;
870130812Smarcel  /* FIXME: is this the same as the passed arg? */
871130812Smarcel  objfile = this_symtab_psymtab->objfile;
872130812Smarcel
873130812Smarcel  bfd_coff_swap_sym_in (objfile->obfd, raw_symbol, &symbol);
874130812Smarcel  if (symbol.n_zeroes)
875130812Smarcel    {
876130812Smarcel      complaint (&symfile_complaints, "Unexpected symbol continuation");
877130812Smarcel
878130812Smarcel      /* Return something which points to '\0' and hope the symbol reading
879130812Smarcel         code does something reasonable.  */
880130812Smarcel      retval = "";
881130812Smarcel    }
882130812Smarcel  else if (symbol.n_sclass & 0x80)
883130812Smarcel    {
884130812Smarcel      retval =
885130812Smarcel	((struct coff_symfile_info *) objfile->sym_private)->debugsec
886130812Smarcel	+ symbol.n_offset;
887130812Smarcel      raw_symbol +=
888130812Smarcel	coff_data (objfile->obfd)->local_symesz;
889130812Smarcel      ++symnum;
890130812Smarcel    }
891130812Smarcel  else
892130812Smarcel    {
893130812Smarcel      complaint (&symfile_complaints, "Unexpected symbol continuation");
894130812Smarcel
895130812Smarcel      /* Return something which points to '\0' and hope the symbol reading
896130812Smarcel         code does something reasonable.  */
897130812Smarcel      retval = "";
898130812Smarcel    }
899130812Smarcel  return retval;
900130812Smarcel}
901130812Smarcel
902130812Smarcel/* Read symbols for a given partial symbol table.  */
903130812Smarcel
904130812Smarcelstatic void
905130812Smarcelread_xcoff_symtab (struct partial_symtab *pst)
906130812Smarcel{
907130812Smarcel  struct objfile *objfile = pst->objfile;
908130812Smarcel  bfd *abfd = objfile->obfd;
909130812Smarcel  char *raw_auxptr;		/* Pointer to first raw aux entry for sym */
910130812Smarcel  char *strtbl = ((struct coff_symfile_info *) objfile->sym_private)->strtbl;
911130812Smarcel  char *debugsec =
912130812Smarcel  ((struct coff_symfile_info *) objfile->sym_private)->debugsec;
913130812Smarcel  char *debugfmt = bfd_xcoff_is_xcoff64 (abfd) ? "XCOFF64" : "XCOFF";
914130812Smarcel
915130812Smarcel  struct internal_syment symbol[1];
916130812Smarcel  union internal_auxent main_aux;
917130812Smarcel  struct coff_symbol cs[1];
918130812Smarcel  CORE_ADDR file_start_addr = 0;
919130812Smarcel  CORE_ADDR file_end_addr = 0;
920130812Smarcel
921130812Smarcel  int next_file_symnum = -1;
922130812Smarcel  unsigned int max_symnum;
923130812Smarcel  int just_started = 1;
924130812Smarcel  int depth = 0;
925130812Smarcel  int fcn_start_addr = 0;
926130812Smarcel
927130812Smarcel  struct coff_symbol fcn_stab_saved;
928130812Smarcel
929130812Smarcel  /* fcn_cs_saved is global because process_xcoff_symbol needs it. */
930130812Smarcel  union internal_auxent fcn_aux_saved;
931130812Smarcel  struct context_stack *new;
932130812Smarcel
933130812Smarcel  char *filestring = " _start_ ";	/* Name of the current file. */
934130812Smarcel
935130812Smarcel  char *last_csect_name;	/* last seen csect's name and value */
936130812Smarcel  CORE_ADDR last_csect_val;
937130812Smarcel  int last_csect_sec;
938130812Smarcel
939130812Smarcel  this_symtab_psymtab = pst;
940130812Smarcel
941130812Smarcel  /* Get the appropriate COFF "constants" related to the file we're
942130812Smarcel     handling. */
943130812Smarcel  local_symesz = coff_data (abfd)->local_symesz;
944130812Smarcel
945130812Smarcel  last_source_file = NULL;
946130812Smarcel  last_csect_name = 0;
947130812Smarcel  last_csect_val = 0;
948130812Smarcel
949130812Smarcel  start_stabs ();
950130812Smarcel  start_symtab (filestring, (char *) NULL, file_start_addr);
951130812Smarcel  record_debugformat (debugfmt);
952130812Smarcel  symnum = ((struct symloc *) pst->read_symtab_private)->first_symnum;
953130812Smarcel  max_symnum =
954130812Smarcel    symnum + ((struct symloc *) pst->read_symtab_private)->numsyms;
955130812Smarcel  first_object_file_end = 0;
956130812Smarcel
957130812Smarcel  raw_symbol =
958130812Smarcel    ((struct coff_symfile_info *) objfile->sym_private)->symtbl
959130812Smarcel    + symnum * local_symesz;
960130812Smarcel
961130812Smarcel  while (symnum < max_symnum)
962130812Smarcel    {
963130812Smarcel
964130812Smarcel      QUIT;			/* make this command interruptable.  */
965130812Smarcel
966130812Smarcel      /* READ_ONE_SYMBOL (symbol, cs, symname_alloced); */
967130812Smarcel      /* read one symbol into `cs' structure. After processing the
968130812Smarcel         whole symbol table, only string table will be kept in memory,
969130812Smarcel         symbol table and debug section of xcoff will be freed. Thus
970130812Smarcel         we can mark symbols with names in string table as
971130812Smarcel         `alloced'. */
972130812Smarcel      {
973130812Smarcel	int ii;
974130812Smarcel
975130812Smarcel	/* Swap and align the symbol into a reasonable C structure.  */
976130812Smarcel	bfd_coff_swap_sym_in (abfd, raw_symbol, symbol);
977130812Smarcel
978130812Smarcel	cs->c_symnum = symnum;
979130812Smarcel	cs->c_naux = symbol->n_numaux;
980130812Smarcel	if (symbol->n_zeroes)
981130812Smarcel	  {
982130812Smarcel	    symname_alloced = 0;
983130812Smarcel	    /* We must use the original, unswapped, name here so the name field
984130812Smarcel	       pointed to by cs->c_name will persist throughout xcoffread.  If
985130812Smarcel	       we use the new field, it gets overwritten for each symbol.  */
986130812Smarcel	    cs->c_name = ((struct external_syment *) raw_symbol)->e.e_name;
987130812Smarcel	    /* If it's exactly E_SYMNMLEN characters long it isn't
988130812Smarcel	       '\0'-terminated.  */
989130812Smarcel	    if (cs->c_name[E_SYMNMLEN - 1] != '\0')
990130812Smarcel	      {
991130812Smarcel		char *p;
992130812Smarcel		p = obstack_alloc (&objfile->objfile_obstack, E_SYMNMLEN + 1);
993130812Smarcel		strncpy (p, cs->c_name, E_SYMNMLEN);
994130812Smarcel		p[E_SYMNMLEN] = '\0';
995130812Smarcel		cs->c_name = p;
996130812Smarcel		symname_alloced = 1;
997130812Smarcel	      }
998130812Smarcel	  }
999130812Smarcel	else if (symbol->n_sclass & 0x80)
1000130812Smarcel	  {
1001130812Smarcel	    cs->c_name = debugsec + symbol->n_offset;
1002130812Smarcel	    symname_alloced = 0;
1003130812Smarcel	  }
1004130812Smarcel	else
1005130812Smarcel	  {
1006130812Smarcel	    /* in string table */
1007130812Smarcel	    cs->c_name = strtbl + (int) symbol->n_offset;
1008130812Smarcel	    symname_alloced = 1;
1009130812Smarcel	  }
1010130812Smarcel	cs->c_value = symbol->n_value;
1011130812Smarcel	cs->c_sclass = symbol->n_sclass;
1012130812Smarcel	cs->c_secnum = symbol->n_scnum;
1013130812Smarcel	cs->c_type = (unsigned) symbol->n_type;
1014130812Smarcel
1015130812Smarcel	raw_symbol += local_symesz;
1016130812Smarcel	++symnum;
1017130812Smarcel
1018130812Smarcel	/* Save addr of first aux entry.  */
1019130812Smarcel	raw_auxptr = raw_symbol;
1020130812Smarcel
1021130812Smarcel	/* Skip all the auxents associated with this symbol.  */
1022130812Smarcel	for (ii = symbol->n_numaux; ii; --ii)
1023130812Smarcel	  {
1024130812Smarcel	    raw_symbol += coff_data (abfd)->local_auxesz;
1025130812Smarcel	    ++symnum;
1026130812Smarcel	  }
1027130812Smarcel      }
1028130812Smarcel
1029130812Smarcel      /* if symbol name starts with ".$" or "$", ignore it. */
1030130812Smarcel      if (cs->c_name[0] == '$'
1031130812Smarcel	  || (cs->c_name[1] == '$' && cs->c_name[0] == '.'))
1032130812Smarcel	continue;
1033130812Smarcel
1034130812Smarcel      if (cs->c_symnum == next_file_symnum && cs->c_sclass != C_FILE)
1035130812Smarcel	{
1036130812Smarcel	  if (last_source_file)
1037130812Smarcel	    {
1038130812Smarcel	      pst->symtab =
1039130812Smarcel		end_symtab (cur_src_end_addr, objfile, SECT_OFF_TEXT (objfile));
1040130812Smarcel	      end_stabs ();
1041130812Smarcel	    }
1042130812Smarcel
1043130812Smarcel	  start_stabs ();
1044130812Smarcel	  start_symtab ("_globals_", (char *) NULL, (CORE_ADDR) 0);
1045130812Smarcel	  record_debugformat (debugfmt);
1046130812Smarcel	  cur_src_end_addr = first_object_file_end;
1047130812Smarcel	  /* done with all files, everything from here on is globals */
1048130812Smarcel	}
1049130812Smarcel
1050130812Smarcel      if ((cs->c_sclass == C_EXT || cs->c_sclass == C_HIDEXT)
1051130812Smarcel	  && cs->c_naux == 1)
1052130812Smarcel	{
1053130812Smarcel	  /* Dealing with a symbol with a csect entry.  */
1054130812Smarcel
1055130812Smarcel#define	CSECT(PP) ((PP)->x_csect)
1056130812Smarcel#define	CSECT_LEN(PP) (CSECT(PP).x_scnlen.l)
1057130812Smarcel#define	CSECT_ALIGN(PP) (SMTYP_ALIGN(CSECT(PP).x_smtyp))
1058130812Smarcel#define	CSECT_SMTYP(PP) (SMTYP_SMTYP(CSECT(PP).x_smtyp))
1059130812Smarcel#define	CSECT_SCLAS(PP) (CSECT(PP).x_smclas)
1060130812Smarcel
1061130812Smarcel	  /* Convert the auxent to something we can access.  */
1062130812Smarcel	  bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
1063130812Smarcel				0, cs->c_naux, &main_aux);
1064130812Smarcel
1065130812Smarcel	  switch (CSECT_SMTYP (&main_aux))
1066130812Smarcel	    {
1067130812Smarcel
1068130812Smarcel	    case XTY_ER:
1069130812Smarcel	      /* Ignore all external references.  */
1070130812Smarcel	      continue;
1071130812Smarcel
1072130812Smarcel	    case XTY_SD:
1073130812Smarcel	      /* A section description.  */
1074130812Smarcel	      {
1075130812Smarcel		switch (CSECT_SCLAS (&main_aux))
1076130812Smarcel		  {
1077130812Smarcel
1078130812Smarcel		  case XMC_PR:
1079130812Smarcel		    {
1080130812Smarcel
1081130812Smarcel		      /* A program csect is seen.  We have to allocate one
1082130812Smarcel		         symbol table for each program csect.  Normally gdb
1083130812Smarcel		         prefers one symtab for each source file.  In case
1084130812Smarcel		         of AIX, one source file might include more than one
1085130812Smarcel		         [PR] csect, and they don't have to be adjacent in
1086130812Smarcel		         terms of the space they occupy in memory. Thus, one
1087130812Smarcel		         single source file might get fragmented in the
1088130812Smarcel		         memory and gdb's file start and end address
1089130812Smarcel		         approach does not work!  GCC (and I think xlc) seem
1090130812Smarcel		         to put all the code in the unnamed program csect.  */
1091130812Smarcel
1092130812Smarcel		      if (last_csect_name)
1093130812Smarcel			{
1094130812Smarcel			  complete_symtab (filestring, file_start_addr);
1095130812Smarcel			  cur_src_end_addr = file_end_addr;
1096130812Smarcel			  end_symtab (file_end_addr, objfile, SECT_OFF_TEXT (objfile));
1097130812Smarcel			  end_stabs ();
1098130812Smarcel			  start_stabs ();
1099130812Smarcel			  /* Give all csects for this source file the same
1100130812Smarcel			     name.  */
1101130812Smarcel			  start_symtab (filestring, NULL, (CORE_ADDR) 0);
1102130812Smarcel			  record_debugformat (debugfmt);
1103130812Smarcel			}
1104130812Smarcel
1105130812Smarcel		      /* If this is the very first csect seen,
1106130812Smarcel		         basically `__start'. */
1107130812Smarcel		      if (just_started)
1108130812Smarcel			{
1109130812Smarcel			  first_object_file_end
1110130812Smarcel			    = cs->c_value + CSECT_LEN (&main_aux);
1111130812Smarcel			  just_started = 0;
1112130812Smarcel			}
1113130812Smarcel
1114130812Smarcel		      file_start_addr =
1115130812Smarcel			cs->c_value + ANOFFSET (objfile->section_offsets,
1116130812Smarcel						SECT_OFF_TEXT (objfile));
1117130812Smarcel		      file_end_addr = file_start_addr + CSECT_LEN (&main_aux);
1118130812Smarcel
1119130812Smarcel		      if (cs->c_name && (cs->c_name[0] == '.'
1120130812Smarcel					 || cs->c_name[0] == '@'))
1121130812Smarcel			{
1122130812Smarcel			  last_csect_name = cs->c_name;
1123130812Smarcel			  last_csect_val = cs->c_value;
1124130812Smarcel			  last_csect_sec = secnum_to_section (cs->c_secnum, objfile);
1125130812Smarcel			}
1126130812Smarcel		    }
1127130812Smarcel		    continue;
1128130812Smarcel
1129130812Smarcel		    /* All other symbols are put into the minimal symbol
1130130812Smarcel		       table only.  */
1131130812Smarcel
1132130812Smarcel		  case XMC_RW:
1133130812Smarcel		    continue;
1134130812Smarcel
1135130812Smarcel		  case XMC_TC0:
1136130812Smarcel		    continue;
1137130812Smarcel
1138130812Smarcel		  case XMC_TC:
1139130812Smarcel		    continue;
1140130812Smarcel
1141130812Smarcel		  default:
1142130812Smarcel		    /* Ignore the symbol.  */
1143130812Smarcel		    continue;
1144130812Smarcel		  }
1145130812Smarcel	      }
1146130812Smarcel	      break;
1147130812Smarcel
1148130812Smarcel	    case XTY_LD:
1149130812Smarcel
1150130812Smarcel	      switch (CSECT_SCLAS (&main_aux))
1151130812Smarcel		{
1152130812Smarcel		case XMC_PR:
1153130812Smarcel		  /* a function entry point. */
1154130812Smarcel		function_entry_point:
1155130812Smarcel
1156130812Smarcel		  fcn_start_addr = cs->c_value;
1157130812Smarcel
1158130812Smarcel		  /* save the function header info, which will be used
1159130812Smarcel		     when `.bf' is seen. */
1160130812Smarcel		  fcn_cs_saved = *cs;
1161130812Smarcel		  fcn_aux_saved = main_aux;
1162130812Smarcel		  continue;
1163130812Smarcel
1164130812Smarcel		case XMC_GL:
1165130812Smarcel		  /* shared library function trampoline code entry point. */
1166130812Smarcel		  continue;
1167130812Smarcel
1168130812Smarcel		case XMC_DS:
1169130812Smarcel		  /* The symbols often have the same names as debug symbols for
1170130812Smarcel		     functions, and confuse lookup_symbol.  */
1171130812Smarcel		  continue;
1172130812Smarcel
1173130812Smarcel		default:
1174130812Smarcel		  /* xlc puts each variable in a separate csect, so we get
1175130812Smarcel		     an XTY_SD for each variable.  But gcc puts several
1176130812Smarcel		     variables in a csect, so that each variable only gets
1177130812Smarcel		     an XTY_LD. This will typically be XMC_RW; I suspect
1178130812Smarcel		     XMC_RO and XMC_BS might be possible too.
1179130812Smarcel		     These variables are put in the minimal symbol table
1180130812Smarcel		     only.  */
1181130812Smarcel		  continue;
1182130812Smarcel		}
1183130812Smarcel	      break;
1184130812Smarcel
1185130812Smarcel	    case XTY_CM:
1186130812Smarcel	      /* Common symbols are put into the minimal symbol table only.  */
1187130812Smarcel	      continue;
1188130812Smarcel
1189130812Smarcel	    default:
1190130812Smarcel	      break;
1191130812Smarcel	    }
1192130812Smarcel	}
1193130812Smarcel
1194130812Smarcel      /* If explicitly specified as a function, treat is as one.  This check
1195130812Smarcel	 evaluates to true for @FIX* bigtoc CSECT symbols, so it must occur
1196130812Smarcel	 after the above CSECT check.  */
1197130812Smarcel      if (ISFCN (cs->c_type) && cs->c_sclass != C_TPDEF)
1198130812Smarcel	{
1199130812Smarcel	  bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
1200130812Smarcel				0, cs->c_naux, &main_aux);
1201130812Smarcel	  goto function_entry_point;
1202130812Smarcel	}
1203130812Smarcel
1204130812Smarcel      switch (cs->c_sclass)
1205130812Smarcel	{
1206130812Smarcel
1207130812Smarcel	case C_FILE:
1208130812Smarcel
1209130812Smarcel	  /* c_value field contains symnum of next .file entry in table
1210130812Smarcel	     or symnum of first global after last .file. */
1211130812Smarcel
1212130812Smarcel	  next_file_symnum = cs->c_value;
1213130812Smarcel
1214130812Smarcel	  /* Complete symbol table for last object file containing
1215130812Smarcel	     debugging information. */
1216130812Smarcel
1217130812Smarcel	  /* Whether or not there was a csect in the previous file, we
1218130812Smarcel	     have to call `end_stabs' and `start_stabs' to reset
1219130812Smarcel	     type_vector, line_vector, etc. structures.  */
1220130812Smarcel
1221130812Smarcel	  complete_symtab (filestring, file_start_addr);
1222130812Smarcel	  cur_src_end_addr = file_end_addr;
1223130812Smarcel	  end_symtab (file_end_addr, objfile, SECT_OFF_TEXT (objfile));
1224130812Smarcel	  end_stabs ();
1225130812Smarcel
1226130812Smarcel	  /* XCOFF, according to the AIX 3.2 documentation, puts the filename
1227130812Smarcel	     in cs->c_name.  But xlc 1.3.0.2 has decided to do things the
1228130812Smarcel	     standard COFF way and put it in the auxent.  We use the auxent if
1229130812Smarcel	     the symbol is ".file" and an auxent exists, otherwise use the symbol
1230130812Smarcel	     itself.  Simple enough.  */
1231130812Smarcel	  if (!strcmp (cs->c_name, ".file") && cs->c_naux > 0)
1232130812Smarcel	    {
1233130812Smarcel	      bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
1234130812Smarcel				    0, cs->c_naux, &main_aux);
1235130812Smarcel	      filestring = coff_getfilename (&main_aux, objfile);
1236130812Smarcel	    }
1237130812Smarcel	  else
1238130812Smarcel	    filestring = cs->c_name;
1239130812Smarcel
1240130812Smarcel	  start_stabs ();
1241130812Smarcel	  start_symtab (filestring, (char *) NULL, (CORE_ADDR) 0);
1242130812Smarcel	  record_debugformat (debugfmt);
1243130812Smarcel	  last_csect_name = 0;
1244130812Smarcel
1245130812Smarcel	  /* reset file start and end addresses. A compilation unit with no text
1246130812Smarcel	     (only data) should have zero file boundaries. */
1247130812Smarcel	  file_start_addr = file_end_addr = 0;
1248130812Smarcel	  break;
1249130812Smarcel
1250130812Smarcel	case C_FUN:
1251130812Smarcel	  fcn_stab_saved = *cs;
1252130812Smarcel	  break;
1253130812Smarcel
1254130812Smarcel	case C_FCN:
1255130812Smarcel	  if (DEPRECATED_STREQ (cs->c_name, ".bf"))
1256130812Smarcel	    {
1257130812Smarcel	      CORE_ADDR off = ANOFFSET (objfile->section_offsets,
1258130812Smarcel					SECT_OFF_TEXT (objfile));
1259130812Smarcel	      bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
1260130812Smarcel				    0, cs->c_naux, &main_aux);
1261130812Smarcel
1262130812Smarcel	      within_function = 1;
1263130812Smarcel
1264130812Smarcel	      new = push_context (0, fcn_start_addr + off);
1265130812Smarcel
1266130812Smarcel	      new->name = define_symbol
1267130812Smarcel		(fcn_cs_saved.c_value + off,
1268130812Smarcel		 fcn_stab_saved.c_name, 0, 0, objfile);
1269130812Smarcel	      if (new->name != NULL)
1270130812Smarcel		SYMBOL_SECTION (new->name) = SECT_OFF_TEXT (objfile);
1271130812Smarcel	    }
1272130812Smarcel	  else if (DEPRECATED_STREQ (cs->c_name, ".ef"))
1273130812Smarcel	    {
1274130812Smarcel
1275130812Smarcel	      bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
1276130812Smarcel				    0, cs->c_naux, &main_aux);
1277130812Smarcel
1278130812Smarcel	      /* The value of .ef is the address of epilogue code;
1279130812Smarcel	         not useful for gdb.  */
1280130812Smarcel	      /* { main_aux.x_sym.x_misc.x_lnsz.x_lnno
1281130812Smarcel	         contains number of lines to '}' */
1282130812Smarcel
1283130812Smarcel	      if (context_stack_depth <= 0)
1284130812Smarcel		{		/* We attempted to pop an empty context stack */
1285130812Smarcel		  ef_complaint (cs->c_symnum);
1286130812Smarcel		  within_function = 0;
1287130812Smarcel		  break;
1288130812Smarcel		}
1289130812Smarcel	      new = pop_context ();
1290130812Smarcel	      /* Stack must be empty now.  */
1291130812Smarcel	      if (context_stack_depth > 0 || new == NULL)
1292130812Smarcel		{
1293130812Smarcel		  ef_complaint (cs->c_symnum);
1294130812Smarcel		  within_function = 0;
1295130812Smarcel		  break;
1296130812Smarcel		}
1297130812Smarcel
1298130812Smarcel	      finish_block (new->name, &local_symbols, new->old_blocks,
1299130812Smarcel			    new->start_addr,
1300130812Smarcel			    (fcn_cs_saved.c_value
1301130812Smarcel			     + fcn_aux_saved.x_sym.x_misc.x_fsize
1302130812Smarcel			     + ANOFFSET (objfile->section_offsets,
1303130812Smarcel					 SECT_OFF_TEXT (objfile))),
1304130812Smarcel			    objfile);
1305130812Smarcel	      within_function = 0;
1306130812Smarcel	    }
1307130812Smarcel	  break;
1308130812Smarcel
1309130812Smarcel	case C_BSTAT:
1310130812Smarcel	  /* Begin static block.  */
1311130812Smarcel	  {
1312130812Smarcel	    struct internal_syment symbol;
1313130812Smarcel
1314130812Smarcel	    read_symbol (&symbol, cs->c_value);
1315130812Smarcel	    static_block_base = symbol.n_value;
1316130812Smarcel	    static_block_section =
1317130812Smarcel	      secnum_to_section (symbol.n_scnum, objfile);
1318130812Smarcel	  }
1319130812Smarcel	  break;
1320130812Smarcel
1321130812Smarcel	case C_ESTAT:
1322130812Smarcel	  /* End of static block.  */
1323130812Smarcel	  static_block_base = 0;
1324130812Smarcel	  static_block_section = -1;
1325130812Smarcel	  break;
1326130812Smarcel
1327130812Smarcel	case C_ARG:
1328130812Smarcel	case C_REGPARM:
1329130812Smarcel	case C_REG:
1330130812Smarcel	case C_TPDEF:
1331130812Smarcel	case C_STRTAG:
1332130812Smarcel	case C_UNTAG:
1333130812Smarcel	case C_ENTAG:
1334130812Smarcel	  {
1335130812Smarcel	    complaint (&symfile_complaints, "Unrecognized storage class %d.",
1336130812Smarcel		       cs->c_sclass);
1337130812Smarcel	  }
1338130812Smarcel	  break;
1339130812Smarcel
1340130812Smarcel	case C_LABEL:
1341130812Smarcel	case C_NULL:
1342130812Smarcel	  /* Ignore these.  */
1343130812Smarcel	  break;
1344130812Smarcel
1345130812Smarcel	case C_HIDEXT:
1346130812Smarcel	case C_STAT:
1347130812Smarcel	  break;
1348130812Smarcel
1349130812Smarcel	case C_BINCL:
1350130812Smarcel	  /* beginning of include file */
1351130812Smarcel	  /* In xlc output, C_BINCL/C_EINCL pair doesn't show up in sorted
1352130812Smarcel	     order. Thus, when wee see them, we might not know enough info
1353130812Smarcel	     to process them. Thus, we'll be saving them into a table
1354130812Smarcel	     (inclTable) and postpone their processing. */
1355130812Smarcel
1356130812Smarcel	  record_include_begin (cs);
1357130812Smarcel	  break;
1358130812Smarcel
1359130812Smarcel	case C_EINCL:
1360130812Smarcel	  /* End of include file.  */
1361130812Smarcel	  /* See the comment after case C_BINCL.  */
1362130812Smarcel	  record_include_end (cs);
1363130812Smarcel	  break;
1364130812Smarcel
1365130812Smarcel	case C_BLOCK:
1366130812Smarcel	  if (DEPRECATED_STREQ (cs->c_name, ".bb"))
1367130812Smarcel	    {
1368130812Smarcel	      depth++;
1369130812Smarcel	      new = push_context (depth,
1370130812Smarcel				  (cs->c_value
1371130812Smarcel				   + ANOFFSET (objfile->section_offsets,
1372130812Smarcel					       SECT_OFF_TEXT (objfile))));
1373130812Smarcel	    }
1374130812Smarcel	  else if (DEPRECATED_STREQ (cs->c_name, ".eb"))
1375130812Smarcel	    {
1376130812Smarcel	      if (context_stack_depth <= 0)
1377130812Smarcel		{		/* We attempted to pop an empty context stack */
1378130812Smarcel		  eb_complaint (cs->c_symnum);
1379130812Smarcel		  break;
1380130812Smarcel		}
1381130812Smarcel	      new = pop_context ();
1382130812Smarcel	      if (depth-- != new->depth)
1383130812Smarcel		{
1384130812Smarcel		  eb_complaint (cs->c_symnum);
1385130812Smarcel		  break;
1386130812Smarcel		}
1387130812Smarcel	      if (local_symbols && context_stack_depth > 0)
1388130812Smarcel		{
1389130812Smarcel		  /* Make a block for the local symbols within.  */
1390130812Smarcel		  finish_block (new->name, &local_symbols, new->old_blocks,
1391130812Smarcel				new->start_addr,
1392130812Smarcel				(cs->c_value
1393130812Smarcel				 + ANOFFSET (objfile->section_offsets,
1394130812Smarcel					     SECT_OFF_TEXT (objfile))),
1395130812Smarcel				objfile);
1396130812Smarcel		}
1397130812Smarcel	      local_symbols = new->locals;
1398130812Smarcel	    }
1399130812Smarcel	  break;
1400130812Smarcel
1401130812Smarcel	default:
1402130812Smarcel	  process_xcoff_symbol (cs, objfile);
1403130812Smarcel	  break;
1404130812Smarcel	}
1405130812Smarcel    }
1406130812Smarcel
1407130812Smarcel  if (last_source_file)
1408130812Smarcel    {
1409130812Smarcel      struct symtab *s;
1410130812Smarcel
1411130812Smarcel      complete_symtab (filestring, file_start_addr);
1412130812Smarcel      cur_src_end_addr = file_end_addr;
1413130812Smarcel      s = end_symtab (file_end_addr, objfile, SECT_OFF_TEXT (objfile));
1414130812Smarcel      /* When reading symbols for the last C_FILE of the objfile, try
1415130812Smarcel         to make sure that we set pst->symtab to the symtab for the
1416130812Smarcel         file, not to the _globals_ symtab.  I'm not sure whether this
1417130812Smarcel         actually works right or when/if it comes up.  */
1418130812Smarcel      if (pst->symtab == NULL)
1419130812Smarcel	pst->symtab = s;
1420130812Smarcel      end_stabs ();
1421130812Smarcel    }
1422130812Smarcel}
1423130812Smarcel
1424130812Smarcel#define	SYMBOL_DUP(SYMBOL1, SYMBOL2)	\
1425130812Smarcel  (SYMBOL2) = (struct symbol *)		\
1426130812Smarcel  	obstack_alloc (&objfile->objfile_obstack, sizeof (struct symbol)); \
1427130812Smarcel  *(SYMBOL2) = *(SYMBOL1);
1428130812Smarcel
1429130812Smarcel
1430130812Smarcel#define	SYMNAME_ALLOC(NAME, ALLOCED)	\
1431130812Smarcel  (ALLOCED) ? (NAME) : obsavestring ((NAME), strlen (NAME), &objfile->objfile_obstack);
1432130812Smarcel
1433130812Smarcel
1434130812Smarcelstatic struct type *func_symbol_type;
1435130812Smarcelstatic struct type *var_symbol_type;
1436130812Smarcel
1437130812Smarcel/* process one xcoff symbol. */
1438130812Smarcel
1439130812Smarcelstatic struct symbol *
1440130812Smarcelprocess_xcoff_symbol (struct coff_symbol *cs, struct objfile *objfile)
1441130812Smarcel{
1442130812Smarcel  struct symbol onesymbol;
1443130812Smarcel  struct symbol *sym = &onesymbol;
1444130812Smarcel  struct symbol *sym2 = NULL;
1445130812Smarcel  char *name, *pp;
1446130812Smarcel
1447130812Smarcel  int sec;
1448130812Smarcel  CORE_ADDR off;
1449130812Smarcel
1450130812Smarcel  if (cs->c_secnum < 0)
1451130812Smarcel    {
1452130812Smarcel      /* The value is a register number, offset within a frame, etc.,
1453130812Smarcel         and does not get relocated.  */
1454130812Smarcel      off = 0;
1455130812Smarcel      sec = -1;
1456130812Smarcel    }
1457130812Smarcel  else
1458130812Smarcel    {
1459130812Smarcel      sec = secnum_to_section (cs->c_secnum, objfile);
1460130812Smarcel      off = ANOFFSET (objfile->section_offsets, sec);
1461130812Smarcel    }
1462130812Smarcel
1463130812Smarcel  name = cs->c_name;
1464130812Smarcel  if (name[0] == '.')
1465130812Smarcel    ++name;
1466130812Smarcel
1467130812Smarcel  memset (sym, '\0', sizeof (struct symbol));
1468130812Smarcel
1469130812Smarcel  /* default assumptions */
1470130812Smarcel  SYMBOL_VALUE_ADDRESS (sym) = cs->c_value + off;
1471130812Smarcel  SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
1472130812Smarcel  SYMBOL_SECTION (sym) = secnum_to_section (cs->c_secnum, objfile);
1473130812Smarcel
1474130812Smarcel  if (ISFCN (cs->c_type))
1475130812Smarcel    {
1476130812Smarcel      /* At this point, we don't know the type of the function.  This
1477130812Smarcel         will be patched with the type from its stab entry later on in
1478130812Smarcel         patch_block_stabs (), unless the file was compiled without -g.  */
1479130812Smarcel
1480130812Smarcel      DEPRECATED_SYMBOL_NAME (sym) = SYMNAME_ALLOC (name, symname_alloced);
1481130812Smarcel      SYMBOL_TYPE (sym) = func_symbol_type;
1482130812Smarcel
1483130812Smarcel      SYMBOL_CLASS (sym) = LOC_BLOCK;
1484130812Smarcel      SYMBOL_DUP (sym, sym2);
1485130812Smarcel
1486130812Smarcel      if (cs->c_sclass == C_EXT)
1487130812Smarcel	add_symbol_to_list (sym2, &global_symbols);
1488130812Smarcel      else if (cs->c_sclass == C_HIDEXT || cs->c_sclass == C_STAT)
1489130812Smarcel	add_symbol_to_list (sym2, &file_symbols);
1490130812Smarcel    }
1491130812Smarcel  else
1492130812Smarcel    {
1493130812Smarcel      /* In case we can't figure out the type, provide default. */
1494130812Smarcel      SYMBOL_TYPE (sym) = var_symbol_type;
1495130812Smarcel
1496130812Smarcel      switch (cs->c_sclass)
1497130812Smarcel	{
1498130812Smarcel#if 0
1499130812Smarcel	  /* The values of functions and global symbols are now resolved
1500130812Smarcel	     via the global_sym_chain in stabsread.c.  */
1501130812Smarcel	case C_FUN:
1502130812Smarcel	  if (fcn_cs_saved.c_sclass == C_EXT)
1503130812Smarcel	    add_stab_to_list (name, &global_stabs);
1504130812Smarcel	  else
1505130812Smarcel	    add_stab_to_list (name, &file_stabs);
1506130812Smarcel	  break;
1507130812Smarcel
1508130812Smarcel	case C_GSYM:
1509130812Smarcel	  add_stab_to_list (name, &global_stabs);
1510130812Smarcel	  break;
1511130812Smarcel#endif
1512130812Smarcel
1513130812Smarcel	case C_BCOMM:
1514130812Smarcel	  common_block_start (cs->c_name, objfile);
1515130812Smarcel	  break;
1516130812Smarcel
1517130812Smarcel	case C_ECOMM:
1518130812Smarcel	  common_block_end (objfile);
1519130812Smarcel	  break;
1520130812Smarcel
1521130812Smarcel	default:
1522130812Smarcel	  complaint (&symfile_complaints, "Unexpected storage class: %d",
1523130812Smarcel		     cs->c_sclass);
1524130812Smarcel	  /* FALLTHROUGH */
1525130812Smarcel
1526130812Smarcel	case C_DECL:
1527130812Smarcel	case C_PSYM:
1528130812Smarcel	case C_RPSYM:
1529130812Smarcel	case C_ECOML:
1530130812Smarcel	case C_LSYM:
1531130812Smarcel	case C_RSYM:
1532130812Smarcel	case C_GSYM:
1533130812Smarcel
1534130812Smarcel	  {
1535130812Smarcel	    sym = define_symbol (cs->c_value + off, cs->c_name, 0, 0, objfile);
1536130812Smarcel	    if (sym != NULL)
1537130812Smarcel	      {
1538130812Smarcel		SYMBOL_SECTION (sym) = sec;
1539130812Smarcel	      }
1540130812Smarcel	    return sym;
1541130812Smarcel	  }
1542130812Smarcel
1543130812Smarcel	case C_STSYM:
1544130812Smarcel
1545130812Smarcel	  /* For xlc (not GCC), the 'V' symbol descriptor is used for
1546130812Smarcel	     all statics and we need to distinguish file-scope versus
1547130812Smarcel	     function-scope using within_function.  We do this by
1548130812Smarcel	     changing the string we pass to define_symbol to use 'S'
1549130812Smarcel	     where we need to, which is not necessarily super-clean,
1550130812Smarcel	     but seems workable enough.  */
1551130812Smarcel
1552130812Smarcel	  if (*name == ':' || (pp = (char *) strchr (name, ':')) == NULL)
1553130812Smarcel	    return NULL;
1554130812Smarcel
1555130812Smarcel	  ++pp;
1556130812Smarcel	  if (*pp == 'V' && !within_function)
1557130812Smarcel	    *pp = 'S';
1558130812Smarcel	  sym = define_symbol ((cs->c_value
1559130812Smarcel				+ ANOFFSET (objfile->section_offsets,
1560130812Smarcel					    static_block_section)),
1561130812Smarcel			       cs->c_name, 0, 0, objfile);
1562130812Smarcel	  if (sym != NULL)
1563130812Smarcel	    {
1564130812Smarcel	      SYMBOL_VALUE_ADDRESS (sym) += static_block_base;
1565130812Smarcel	      SYMBOL_SECTION (sym) = static_block_section;
1566130812Smarcel	    }
1567130812Smarcel	  return sym;
1568130812Smarcel
1569130812Smarcel	}
1570130812Smarcel    }
1571130812Smarcel  return sym2;
1572130812Smarcel}
1573130812Smarcel
1574130812Smarcel/* Extract the file name from the aux entry of a C_FILE symbol.
1575130812Smarcel   Result is in static storage and is only good for temporary use.  */
1576130812Smarcel
1577130812Smarcelstatic char *
1578130812Smarcelcoff_getfilename (union internal_auxent *aux_entry, struct objfile *objfile)
1579130812Smarcel{
1580130812Smarcel  static char buffer[BUFSIZ];
1581130812Smarcel
1582130812Smarcel  if (aux_entry->x_file.x_n.x_zeroes == 0)
1583130812Smarcel    strcpy (buffer,
1584130812Smarcel	    ((struct coff_symfile_info *) objfile->sym_private)->strtbl
1585130812Smarcel	    + aux_entry->x_file.x_n.x_offset);
1586130812Smarcel  else
1587130812Smarcel    {
1588130812Smarcel      strncpy (buffer, aux_entry->x_file.x_fname, FILNMLEN);
1589130812Smarcel      buffer[FILNMLEN] = '\0';
1590130812Smarcel    }
1591130812Smarcel  return (buffer);
1592130812Smarcel}
1593130812Smarcel
1594130812Smarcel/* Set *SYMBOL to symbol number symno in symtbl.  */
1595130812Smarcelstatic void
1596130812Smarcelread_symbol (struct internal_syment *symbol, int symno)
1597130812Smarcel{
1598130812Smarcel  int nsyms =
1599130812Smarcel  ((struct coff_symfile_info *) this_symtab_psymtab->objfile->sym_private)
1600130812Smarcel  ->symtbl_num_syms;
1601130812Smarcel  char *stbl =
1602130812Smarcel  ((struct coff_symfile_info *) this_symtab_psymtab->objfile->sym_private)
1603130812Smarcel  ->symtbl;
1604130812Smarcel  if (symno < 0 || symno >= nsyms)
1605130812Smarcel    {
1606130812Smarcel      complaint (&symfile_complaints, "Invalid symbol offset");
1607130812Smarcel      symbol->n_value = 0;
1608130812Smarcel      symbol->n_scnum = -1;
1609130812Smarcel      return;
1610130812Smarcel    }
1611130812Smarcel  bfd_coff_swap_sym_in (this_symtab_psymtab->objfile->obfd,
1612130812Smarcel			stbl + (symno * local_symesz),
1613130812Smarcel			symbol);
1614130812Smarcel}
1615130812Smarcel
1616130812Smarcel/* Get value corresponding to symbol number symno in symtbl.  */
1617130812Smarcel
1618130812Smarcelstatic CORE_ADDR
1619130812Smarcelread_symbol_nvalue (int symno)
1620130812Smarcel{
1621130812Smarcel  struct internal_syment symbol[1];
1622130812Smarcel
1623130812Smarcel  read_symbol (symbol, symno);
1624130812Smarcel  return symbol->n_value;
1625130812Smarcel}
1626130812Smarcel
1627130812Smarcel
1628130812Smarcel/* Find the address of the function corresponding to symno, where
1629130812Smarcel   symno is the symbol pointed to by the linetable.  */
1630130812Smarcel
1631130812Smarcelstatic int
1632130812Smarcelread_symbol_lineno (int symno)
1633130812Smarcel{
1634130812Smarcel  struct objfile *objfile = this_symtab_psymtab->objfile;
1635130812Smarcel  int xcoff64 = bfd_xcoff_is_xcoff64 (objfile->obfd);
1636130812Smarcel
1637130812Smarcel  struct coff_symfile_info *info =
1638130812Smarcel    (struct coff_symfile_info *)objfile->sym_private;
1639130812Smarcel  int nsyms = info->symtbl_num_syms;
1640130812Smarcel  char *stbl = info->symtbl;
1641130812Smarcel  char *strtbl = info->strtbl;
1642130812Smarcel
1643130812Smarcel  struct internal_syment symbol[1];
1644130812Smarcel  union internal_auxent main_aux[1];
1645130812Smarcel
1646130812Smarcel  if (symno < 0)
1647130812Smarcel    {
1648130812Smarcel      bf_notfound_complaint ();
1649130812Smarcel      return 0;
1650130812Smarcel    }
1651130812Smarcel
1652130812Smarcel  /* Note that just searching for a short distance (e.g. 50 symbols)
1653130812Smarcel     is not enough, at least in the following case.
1654130812Smarcel
1655130812Smarcel     .extern foo
1656130812Smarcel     [many .stabx entries]
1657130812Smarcel     [a few functions, referring to foo]
1658130812Smarcel     .globl foo
1659130812Smarcel     .bf
1660130812Smarcel
1661130812Smarcel     What happens here is that the assembler moves the .stabx entries
1662130812Smarcel     to right before the ".bf" for foo, but the symbol for "foo" is before
1663130812Smarcel     all the stabx entries.  See PR gdb/2222.  */
1664130812Smarcel
1665130812Smarcel  /* Maintaining a table of .bf entries might be preferable to this search.
1666130812Smarcel     If I understand things correctly it would need to be done only for
1667130812Smarcel     the duration of a single psymtab to symtab conversion.  */
1668130812Smarcel  while (symno < nsyms)
1669130812Smarcel    {
1670130812Smarcel      bfd_coff_swap_sym_in (symfile_bfd,
1671130812Smarcel			    stbl + (symno * local_symesz), symbol);
1672130812Smarcel      if (symbol->n_sclass == C_FCN)
1673130812Smarcel	{
1674130812Smarcel	  char *name = xcoff64 ? strtbl + symbol->n_offset : symbol->n_name;
1675130812Smarcel	  if (DEPRECATED_STREQ (name, ".bf"))
1676130812Smarcel	    goto gotit;
1677130812Smarcel	}
1678130812Smarcel      symno += symbol->n_numaux + 1;
1679130812Smarcel    }
1680130812Smarcel
1681130812Smarcel  bf_notfound_complaint ();
1682130812Smarcel  return 0;
1683130812Smarcel
1684130812Smarcelgotit:
1685130812Smarcel  /* take aux entry and return its lineno */
1686130812Smarcel  symno++;
1687130812Smarcel  bfd_coff_swap_aux_in (objfile->obfd, stbl + symno * local_symesz,
1688130812Smarcel			symbol->n_type, symbol->n_sclass,
1689130812Smarcel			0, symbol->n_numaux, main_aux);
1690130812Smarcel
1691130812Smarcel  return main_aux->x_sym.x_misc.x_lnsz.x_lnno;
1692130812Smarcel}
1693130812Smarcel
1694130812Smarcel/* Support for line number handling */
1695130812Smarcel
1696130812Smarcel/* This function is called for every section; it finds the outer limits
1697130812Smarcel * of the line table (minimum and maximum file offset) so that the
1698130812Smarcel * mainline code can read the whole thing for efficiency.
1699130812Smarcel */
1700130812Smarcelstatic void
1701130812Smarcelfind_linenos (struct bfd *abfd, struct bfd_section *asect, void *vpinfo)
1702130812Smarcel{
1703130812Smarcel  struct coff_symfile_info *info;
1704130812Smarcel  int size, count;
1705130812Smarcel  file_ptr offset, maxoff;
1706130812Smarcel
1707130812Smarcel  count = asect->lineno_count;
1708130812Smarcel
1709130812Smarcel  if (!DEPRECATED_STREQ (asect->name, ".text") || count == 0)
1710130812Smarcel    return;
1711130812Smarcel
1712130812Smarcel  size = count * coff_data (abfd)->local_linesz;
1713130812Smarcel  info = (struct coff_symfile_info *) vpinfo;
1714130812Smarcel  offset = asect->line_filepos;
1715130812Smarcel  maxoff = offset + size;
1716130812Smarcel
1717130812Smarcel  if (offset < info->min_lineno_offset || info->min_lineno_offset == 0)
1718130812Smarcel    info->min_lineno_offset = offset;
1719130812Smarcel
1720130812Smarcel  if (maxoff > info->max_lineno_offset)
1721130812Smarcel    info->max_lineno_offset = maxoff;
1722130812Smarcel}
1723130812Smarcel
1724130812Smarcelstatic void xcoff_psymtab_to_symtab_1 (struct partial_symtab *);
1725130812Smarcel
1726130812Smarcelstatic void
1727130812Smarcelxcoff_psymtab_to_symtab_1 (struct partial_symtab *pst)
1728130812Smarcel{
1729130812Smarcel  struct cleanup *old_chain;
1730130812Smarcel  int i;
1731130812Smarcel
1732130812Smarcel  if (!pst)
1733130812Smarcel    return;
1734130812Smarcel
1735130812Smarcel  if (pst->readin)
1736130812Smarcel    {
1737130812Smarcel      fprintf_unfiltered
1738130812Smarcel	(gdb_stderr, "Psymtab for %s already read in.  Shouldn't happen.\n",
1739130812Smarcel	 pst->filename);
1740130812Smarcel      return;
1741130812Smarcel    }
1742130812Smarcel
1743130812Smarcel  /* Read in all partial symtabs on which this one is dependent */
1744130812Smarcel  for (i = 0; i < pst->number_of_dependencies; i++)
1745130812Smarcel    if (!pst->dependencies[i]->readin)
1746130812Smarcel      {
1747130812Smarcel	/* Inform about additional files that need to be read in.  */
1748130812Smarcel	if (info_verbose)
1749130812Smarcel	  {
1750130812Smarcel	    fputs_filtered (" ", gdb_stdout);
1751130812Smarcel	    wrap_here ("");
1752130812Smarcel	    fputs_filtered ("and ", gdb_stdout);
1753130812Smarcel	    wrap_here ("");
1754130812Smarcel	    printf_filtered ("%s...", pst->dependencies[i]->filename);
1755130812Smarcel	    wrap_here ("");	/* Flush output */
1756130812Smarcel	    gdb_flush (gdb_stdout);
1757130812Smarcel	  }
1758130812Smarcel	xcoff_psymtab_to_symtab_1 (pst->dependencies[i]);
1759130812Smarcel      }
1760130812Smarcel
1761130812Smarcel  if (((struct symloc *) pst->read_symtab_private)->numsyms != 0)
1762130812Smarcel    {
1763130812Smarcel      /* Init stuff necessary for reading in symbols.  */
1764130812Smarcel      stabsread_init ();
1765130812Smarcel      buildsym_init ();
1766130812Smarcel      old_chain = make_cleanup (really_free_pendings, 0);
1767130812Smarcel
1768130812Smarcel      read_xcoff_symtab (pst);
1769130812Smarcel
1770130812Smarcel      do_cleanups (old_chain);
1771130812Smarcel    }
1772130812Smarcel
1773130812Smarcel  pst->readin = 1;
1774130812Smarcel}
1775130812Smarcel
1776130812Smarcelstatic void xcoff_psymtab_to_symtab (struct partial_symtab *);
1777130812Smarcel
1778130812Smarcel/* Read in all of the symbols for a given psymtab for real.
1779130812Smarcel   Be verbose about it if the user wants that.  */
1780130812Smarcel
1781130812Smarcelstatic void
1782130812Smarcelxcoff_psymtab_to_symtab (struct partial_symtab *pst)
1783130812Smarcel{
1784130812Smarcel  bfd *sym_bfd;
1785130812Smarcel
1786130812Smarcel  if (!pst)
1787130812Smarcel    return;
1788130812Smarcel
1789130812Smarcel  if (pst->readin)
1790130812Smarcel    {
1791130812Smarcel      fprintf_unfiltered
1792130812Smarcel	(gdb_stderr, "Psymtab for %s already read in.  Shouldn't happen.\n",
1793130812Smarcel	 pst->filename);
1794130812Smarcel      return;
1795130812Smarcel    }
1796130812Smarcel
1797130812Smarcel  if (((struct symloc *) pst->read_symtab_private)->numsyms != 0
1798130812Smarcel      || pst->number_of_dependencies)
1799130812Smarcel    {
1800130812Smarcel      /* Print the message now, before reading the string table,
1801130812Smarcel         to avoid disconcerting pauses.  */
1802130812Smarcel      if (info_verbose)
1803130812Smarcel	{
1804130812Smarcel	  printf_filtered ("Reading in symbols for %s...", pst->filename);
1805130812Smarcel	  gdb_flush (gdb_stdout);
1806130812Smarcel	}
1807130812Smarcel
1808130812Smarcel      sym_bfd = pst->objfile->obfd;
1809130812Smarcel
1810130812Smarcel      next_symbol_text_func = xcoff_next_symbol_text;
1811130812Smarcel
1812130812Smarcel      xcoff_psymtab_to_symtab_1 (pst);
1813130812Smarcel
1814130812Smarcel      /* Match with global symbols.  This only needs to be done once,
1815130812Smarcel         after all of the symtabs and dependencies have been read in.   */
1816130812Smarcel      scan_file_globals (pst->objfile);
1817130812Smarcel
1818130812Smarcel      /* Finish up the debug error message.  */
1819130812Smarcel      if (info_verbose)
1820130812Smarcel	printf_filtered ("done.\n");
1821130812Smarcel    }
1822130812Smarcel}
1823130812Smarcel
1824130812Smarcelstatic void
1825130812Smarcelxcoff_new_init (struct objfile *objfile)
1826130812Smarcel{
1827130812Smarcel  stabsread_new_init ();
1828130812Smarcel  buildsym_new_init ();
1829130812Smarcel}
1830130812Smarcel
1831130812Smarcel/* Do initialization in preparation for reading symbols from OBJFILE.
1832130812Smarcel
1833130812Smarcel   We will only be called if this is an XCOFF or XCOFF-like file.
1834130812Smarcel   BFD handles figuring out the format of the file, and code in symfile.c
1835130812Smarcel   uses BFD's determination to vector to us.  */
1836130812Smarcel
1837130812Smarcelstatic void
1838130812Smarcelxcoff_symfile_init (struct objfile *objfile)
1839130812Smarcel{
1840130812Smarcel  /* Allocate struct to keep track of the symfile */
1841130812Smarcel  objfile->sym_private = xmmalloc (objfile->md,
1842130812Smarcel				   sizeof (struct coff_symfile_info));
1843130812Smarcel
1844130812Smarcel  /* XCOFF objects may be reordered, so set OBJF_REORDERED.  If we
1845130812Smarcel     find this causes a significant slowdown in gdb then we could
1846130812Smarcel     set it in the debug symbol readers only when necessary.  */
1847130812Smarcel  objfile->flags |= OBJF_REORDERED;
1848130812Smarcel
1849130812Smarcel  init_entry_point_info (objfile);
1850130812Smarcel}
1851130812Smarcel
1852130812Smarcel/* Perform any local cleanups required when we are done with a particular
1853130812Smarcel   objfile.  I.E, we are in the process of discarding all symbol information
1854130812Smarcel   for an objfile, freeing up all memory held for it, and unlinking the
1855130812Smarcel   objfile struct from the global list of known objfiles. */
1856130812Smarcel
1857130812Smarcelstatic void
1858130812Smarcelxcoff_symfile_finish (struct objfile *objfile)
1859130812Smarcel{
1860130812Smarcel  if (objfile->sym_private != NULL)
1861130812Smarcel    {
1862130812Smarcel      xmfree (objfile->md, objfile->sym_private);
1863130812Smarcel    }
1864130812Smarcel
1865130812Smarcel  /* Start with a fresh include table for the next objfile.  */
1866130812Smarcel  if (inclTable)
1867130812Smarcel    {
1868130812Smarcel      xfree (inclTable);
1869130812Smarcel      inclTable = NULL;
1870130812Smarcel    }
1871130812Smarcel  inclIndx = inclLength = inclDepth = 0;
1872130812Smarcel}
1873130812Smarcel
1874130812Smarcel
1875130812Smarcelstatic void
1876130812Smarcelinit_stringtab (bfd *abfd, file_ptr offset, struct objfile *objfile)
1877130812Smarcel{
1878130812Smarcel  long length;
1879130812Smarcel  int val;
1880130812Smarcel  unsigned char lengthbuf[4];
1881130812Smarcel  char *strtbl;
1882130812Smarcel
1883130812Smarcel  ((struct coff_symfile_info *) objfile->sym_private)->strtbl = NULL;
1884130812Smarcel
1885130812Smarcel  if (bfd_seek (abfd, offset, SEEK_SET) < 0)
1886130812Smarcel    error ("cannot seek to string table in %s: %s",
1887130812Smarcel	   bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
1888130812Smarcel
1889130812Smarcel  val = bfd_bread ((char *) lengthbuf, sizeof lengthbuf, abfd);
1890130812Smarcel  length = bfd_h_get_32 (abfd, lengthbuf);
1891130812Smarcel
1892130812Smarcel  /* If no string table is needed, then the file may end immediately
1893130812Smarcel     after the symbols.  Just return with `strtbl' set to NULL.  */
1894130812Smarcel
1895130812Smarcel  if (val != sizeof lengthbuf || length < sizeof lengthbuf)
1896130812Smarcel    return;
1897130812Smarcel
1898130812Smarcel  /* Allocate string table from objfile_obstack. We will need this table
1899130812Smarcel     as long as we have its symbol table around. */
1900130812Smarcel
1901130812Smarcel  strtbl = (char *) obstack_alloc (&objfile->objfile_obstack, length);
1902130812Smarcel  ((struct coff_symfile_info *) objfile->sym_private)->strtbl = strtbl;
1903130812Smarcel
1904130812Smarcel  /* Copy length buffer, the first byte is usually zero and is
1905130812Smarcel     used for stabs with a name length of zero.  */
1906130812Smarcel  memcpy (strtbl, lengthbuf, sizeof lengthbuf);
1907130812Smarcel  if (length == sizeof lengthbuf)
1908130812Smarcel    return;
1909130812Smarcel
1910130812Smarcel  val = bfd_bread (strtbl + sizeof lengthbuf, length - sizeof lengthbuf, abfd);
1911130812Smarcel
1912130812Smarcel  if (val != length - sizeof lengthbuf)
1913130812Smarcel    error ("cannot read string table from %s: %s",
1914130812Smarcel	   bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
1915130812Smarcel  if (strtbl[length - 1] != '\0')
1916130812Smarcel    error ("bad symbol file: string table does not end with null character");
1917130812Smarcel
1918130812Smarcel  return;
1919130812Smarcel}
1920130812Smarcel
1921130812Smarcel/* If we have not yet seen a function for this psymtab, this is 0.  If we
1922130812Smarcel   have seen one, it is the offset in the line numbers of the line numbers
1923130812Smarcel   for the psymtab.  */
1924130812Smarcelstatic unsigned int first_fun_line_offset;
1925130812Smarcel
1926130812Smarcelstatic struct partial_symtab *xcoff_start_psymtab
1927130812Smarcel  (struct objfile *, char *, int,
1928130812Smarcel   struct partial_symbol **, struct partial_symbol **);
1929130812Smarcel
1930130812Smarcel/* Allocate and partially fill a partial symtab.  It will be
1931130812Smarcel   completely filled at the end of the symbol list.
1932130812Smarcel
1933130812Smarcel   SYMFILE_NAME is the name of the symbol-file we are reading from, and ADDR
1934130812Smarcel   is the address relative to which its symbols are (incremental) or 0
1935130812Smarcel   (normal). */
1936130812Smarcel
1937130812Smarcelstatic struct partial_symtab *
1938130812Smarcelxcoff_start_psymtab (struct objfile *objfile, char *filename, int first_symnum,
1939130812Smarcel		     struct partial_symbol **global_syms,
1940130812Smarcel		     struct partial_symbol **static_syms)
1941130812Smarcel{
1942130812Smarcel  struct partial_symtab *result =
1943130812Smarcel  start_psymtab_common (objfile, objfile->section_offsets,
1944130812Smarcel			filename,
1945130812Smarcel			/* We fill in textlow later.  */
1946130812Smarcel			0,
1947130812Smarcel			global_syms, static_syms);
1948130812Smarcel
1949130812Smarcel  result->read_symtab_private = (char *)
1950130812Smarcel    obstack_alloc (&objfile->objfile_obstack, sizeof (struct symloc));
1951130812Smarcel  ((struct symloc *) result->read_symtab_private)->first_symnum = first_symnum;
1952130812Smarcel  result->read_symtab = xcoff_psymtab_to_symtab;
1953130812Smarcel
1954130812Smarcel  /* Deduce the source language from the filename for this psymtab. */
1955130812Smarcel  psymtab_language = deduce_language_from_filename (filename);
1956130812Smarcel
1957130812Smarcel  return result;
1958130812Smarcel}
1959130812Smarcel
1960130812Smarcelstatic struct partial_symtab *xcoff_end_psymtab
1961130812Smarcel  (struct partial_symtab *, char **, int, int,
1962130812Smarcel   struct partial_symtab **, int, int);
1963130812Smarcel
1964130812Smarcel/* Close off the current usage of PST.
1965130812Smarcel   Returns PST, or NULL if the partial symtab was empty and thrown away.
1966130812Smarcel
1967130812Smarcel   CAPPING_SYMBOL_NUMBER is the end of pst (exclusive).
1968130812Smarcel
1969130812Smarcel   INCLUDE_LIST, NUM_INCLUDES, DEPENDENCY_LIST, and NUMBER_DEPENDENCIES
1970130812Smarcel   are the information for includes and dependencies.  */
1971130812Smarcel
1972130812Smarcelstatic struct partial_symtab *
1973130812Smarcelxcoff_end_psymtab (struct partial_symtab *pst, char **include_list,
1974130812Smarcel		   int num_includes, int capping_symbol_number,
1975130812Smarcel		   struct partial_symtab **dependency_list,
1976130812Smarcel		   int number_dependencies, int textlow_not_set)
1977130812Smarcel{
1978130812Smarcel  int i;
1979130812Smarcel  struct objfile *objfile = pst->objfile;
1980130812Smarcel
1981130812Smarcel  if (capping_symbol_number != -1)
1982130812Smarcel    ((struct symloc *) pst->read_symtab_private)->numsyms =
1983130812Smarcel      capping_symbol_number
1984130812Smarcel      - ((struct symloc *) pst->read_symtab_private)->first_symnum;
1985130812Smarcel  ((struct symloc *) pst->read_symtab_private)->lineno_off =
1986130812Smarcel    first_fun_line_offset;
1987130812Smarcel  first_fun_line_offset = 0;
1988130812Smarcel  pst->n_global_syms =
1989130812Smarcel    objfile->global_psymbols.next - (objfile->global_psymbols.list + pst->globals_offset);
1990130812Smarcel  pst->n_static_syms =
1991130812Smarcel    objfile->static_psymbols.next - (objfile->static_psymbols.list + pst->statics_offset);
1992130812Smarcel
1993130812Smarcel  pst->number_of_dependencies = number_dependencies;
1994130812Smarcel  if (number_dependencies)
1995130812Smarcel    {
1996130812Smarcel      pst->dependencies = (struct partial_symtab **)
1997130812Smarcel	obstack_alloc (&objfile->objfile_obstack,
1998130812Smarcel		    number_dependencies * sizeof (struct partial_symtab *));
1999130812Smarcel      memcpy (pst->dependencies, dependency_list,
2000130812Smarcel	      number_dependencies * sizeof (struct partial_symtab *));
2001130812Smarcel    }
2002130812Smarcel  else
2003130812Smarcel    pst->dependencies = 0;
2004130812Smarcel
2005130812Smarcel  for (i = 0; i < num_includes; i++)
2006130812Smarcel    {
2007130812Smarcel      struct partial_symtab *subpst =
2008130812Smarcel      allocate_psymtab (include_list[i], objfile);
2009130812Smarcel
2010130812Smarcel      subpst->section_offsets = pst->section_offsets;
2011130812Smarcel      subpst->read_symtab_private =
2012130812Smarcel	(char *) obstack_alloc (&objfile->objfile_obstack,
2013130812Smarcel				sizeof (struct symloc));
2014130812Smarcel      ((struct symloc *) subpst->read_symtab_private)->first_symnum = 0;
2015130812Smarcel      ((struct symloc *) subpst->read_symtab_private)->numsyms = 0;
2016130812Smarcel      subpst->textlow = 0;
2017130812Smarcel      subpst->texthigh = 0;
2018130812Smarcel
2019130812Smarcel      /* We could save slight bits of space by only making one of these,
2020130812Smarcel         shared by the entire set of include files.  FIXME-someday.  */
2021130812Smarcel      subpst->dependencies = (struct partial_symtab **)
2022130812Smarcel	obstack_alloc (&objfile->objfile_obstack,
2023130812Smarcel		       sizeof (struct partial_symtab *));
2024130812Smarcel      subpst->dependencies[0] = pst;
2025130812Smarcel      subpst->number_of_dependencies = 1;
2026130812Smarcel
2027130812Smarcel      subpst->globals_offset =
2028130812Smarcel	subpst->n_global_syms =
2029130812Smarcel	subpst->statics_offset =
2030130812Smarcel	subpst->n_static_syms = 0;
2031130812Smarcel
2032130812Smarcel      subpst->readin = 0;
2033130812Smarcel      subpst->symtab = 0;
2034130812Smarcel      subpst->read_symtab = pst->read_symtab;
2035130812Smarcel    }
2036130812Smarcel
2037130812Smarcel  sort_pst_symbols (pst);
2038130812Smarcel
2039130812Smarcel  /* If there is already a psymtab or symtab for a file of this name,
2040130812Smarcel     remove it.  (If there is a symtab, more drastic things also
2041130812Smarcel     happen.)  This happens in VxWorks.  */
2042130812Smarcel  free_named_symtabs (pst->filename);
2043130812Smarcel
2044130812Smarcel  if (num_includes == 0
2045130812Smarcel      && number_dependencies == 0
2046130812Smarcel      && pst->n_global_syms == 0
2047130812Smarcel      && pst->n_static_syms == 0)
2048130812Smarcel    {
2049130812Smarcel      /* Throw away this psymtab, it's empty.  We can't deallocate it, since
2050130812Smarcel         it is on the obstack, but we can forget to chain it on the list.  */
2051130812Smarcel      /* Empty psymtabs happen as a result of header files which don't have
2052130812Smarcel         any symbols in them.  There can be a lot of them.  */
2053130812Smarcel
2054130812Smarcel      discard_psymtab (pst);
2055130812Smarcel
2056130812Smarcel      /* Indicate that psymtab was thrown away.  */
2057130812Smarcel      pst = (struct partial_symtab *) NULL;
2058130812Smarcel    }
2059130812Smarcel  return pst;
2060130812Smarcel}
2061130812Smarcel
2062130812Smarcelstatic void swap_sym (struct internal_syment *,
2063130812Smarcel		      union internal_auxent *, char **, char **,
2064130812Smarcel		      unsigned int *, struct objfile *);
2065130812Smarcel
2066130812Smarcel/* Swap raw symbol at *RAW and put the name in *NAME, the symbol in
2067130812Smarcel   *SYMBOL, the first auxent in *AUX.  Advance *RAW and *SYMNUMP over
2068130812Smarcel   the symbol and its auxents.  */
2069130812Smarcel
2070130812Smarcelstatic void
2071130812Smarcelswap_sym (struct internal_syment *symbol, union internal_auxent *aux,
2072130812Smarcel	  char **name, char **raw, unsigned int *symnump,
2073130812Smarcel	  struct objfile *objfile)
2074130812Smarcel{
2075130812Smarcel  bfd_coff_swap_sym_in (objfile->obfd, *raw, symbol);
2076130812Smarcel  if (symbol->n_zeroes)
2077130812Smarcel    {
2078130812Smarcel      /* If it's exactly E_SYMNMLEN characters long it isn't
2079130812Smarcel         '\0'-terminated.  */
2080130812Smarcel      if (symbol->n_name[E_SYMNMLEN - 1] != '\0')
2081130812Smarcel	{
2082130812Smarcel	  /* FIXME: wastes memory for symbols which we don't end up putting
2083130812Smarcel	     into the minimal symbols.  */
2084130812Smarcel	  char *p;
2085130812Smarcel	  p = obstack_alloc (&objfile->objfile_obstack, E_SYMNMLEN + 1);
2086130812Smarcel	  strncpy (p, symbol->n_name, E_SYMNMLEN);
2087130812Smarcel	  p[E_SYMNMLEN] = '\0';
2088130812Smarcel	  *name = p;
2089130812Smarcel	}
2090130812Smarcel      else
2091130812Smarcel	/* Point to the unswapped name as that persists as long as the
2092130812Smarcel	   objfile does.  */
2093130812Smarcel	*name = ((struct external_syment *) *raw)->e.e_name;
2094130812Smarcel    }
2095130812Smarcel  else if (symbol->n_sclass & 0x80)
2096130812Smarcel    {
2097130812Smarcel      *name = ((struct coff_symfile_info *) objfile->sym_private)->debugsec
2098130812Smarcel	+ symbol->n_offset;
2099130812Smarcel    }
2100130812Smarcel  else
2101130812Smarcel    {
2102130812Smarcel      *name = ((struct coff_symfile_info *) objfile->sym_private)->strtbl
2103130812Smarcel	+ symbol->n_offset;
2104130812Smarcel    }
2105130812Smarcel  ++*symnump;
2106130812Smarcel  *raw += coff_data (objfile->obfd)->local_symesz;
2107130812Smarcel  if (symbol->n_numaux > 0)
2108130812Smarcel    {
2109130812Smarcel      bfd_coff_swap_aux_in (objfile->obfd, *raw, symbol->n_type,
2110130812Smarcel			    symbol->n_sclass, 0, symbol->n_numaux, aux);
2111130812Smarcel
2112130812Smarcel      *symnump += symbol->n_numaux;
2113130812Smarcel      *raw += coff_data (objfile->obfd)->local_symesz * symbol->n_numaux;
2114130812Smarcel    }
2115130812Smarcel}
2116130812Smarcel
2117130812Smarcelstatic void
2118130812Smarcelfunction_outside_compilation_unit_complaint (const char *arg1)
2119130812Smarcel{
2120130812Smarcel  complaint (&symfile_complaints,
2121130812Smarcel	     "function `%s' appears to be defined outside of all compilation units",
2122130812Smarcel	     arg1);
2123130812Smarcel}
2124130812Smarcel
2125130812Smarcelstatic void
2126130812Smarcelscan_xcoff_symtab (struct objfile *objfile)
2127130812Smarcel{
2128130812Smarcel  CORE_ADDR toc_offset = 0;	/* toc offset value in data section. */
2129130812Smarcel  char *filestring = NULL;
2130130812Smarcel
2131130812Smarcel  char *namestring;
2132130812Smarcel  int past_first_source_file = 0;
2133130812Smarcel  bfd *abfd;
2134130812Smarcel  asection *bfd_sect;
2135130812Smarcel  unsigned int nsyms;
2136130812Smarcel
2137130812Smarcel  /* Current partial symtab */
2138130812Smarcel  struct partial_symtab *pst;
2139130812Smarcel
2140130812Smarcel  /* List of current psymtab's include files */
2141130812Smarcel  char **psymtab_include_list;
2142130812Smarcel  int includes_allocated;
2143130812Smarcel  int includes_used;
2144130812Smarcel
2145130812Smarcel  /* Index within current psymtab dependency list */
2146130812Smarcel  struct partial_symtab **dependency_list;
2147130812Smarcel  int dependencies_used, dependencies_allocated;
2148130812Smarcel
2149130812Smarcel  char *sraw_symbol;
2150130812Smarcel  struct internal_syment symbol;
2151130812Smarcel  union internal_auxent main_aux[5];
2152130812Smarcel  unsigned int ssymnum;
2153130812Smarcel
2154130812Smarcel  char *last_csect_name = NULL;	/* last seen csect's name and value */
2155130812Smarcel  CORE_ADDR last_csect_val = 0;
2156130812Smarcel  int last_csect_sec = 0;
2157130812Smarcel  int misc_func_recorded = 0;	/* true if any misc. function */
2158130812Smarcel  int textlow_not_set = 1;
2159130812Smarcel
2160130812Smarcel  pst = (struct partial_symtab *) 0;
2161130812Smarcel
2162130812Smarcel  includes_allocated = 30;
2163130812Smarcel  includes_used = 0;
2164130812Smarcel  psymtab_include_list = (char **) alloca (includes_allocated *
2165130812Smarcel					   sizeof (char *));
2166130812Smarcel
2167130812Smarcel  dependencies_allocated = 30;
2168130812Smarcel  dependencies_used = 0;
2169130812Smarcel  dependency_list =
2170130812Smarcel    (struct partial_symtab **) alloca (dependencies_allocated *
2171130812Smarcel				       sizeof (struct partial_symtab *));
2172130812Smarcel
2173130812Smarcel  last_source_file = NULL;
2174130812Smarcel
2175130812Smarcel  abfd = objfile->obfd;
2176130812Smarcel
2177130812Smarcel  sraw_symbol = ((struct coff_symfile_info *) objfile->sym_private)->symtbl;
2178130812Smarcel  nsyms = ((struct coff_symfile_info *) objfile->sym_private)->symtbl_num_syms;
2179130812Smarcel  ssymnum = 0;
2180130812Smarcel  while (ssymnum < nsyms)
2181130812Smarcel    {
2182130812Smarcel      int sclass;
2183130812Smarcel
2184130812Smarcel      QUIT;
2185130812Smarcel
2186130812Smarcel      bfd_coff_swap_sym_in (abfd, sraw_symbol, &symbol);
2187130812Smarcel      sclass = symbol.n_sclass;
2188130812Smarcel
2189130812Smarcel      switch (sclass)
2190130812Smarcel	{
2191130812Smarcel	case C_EXT:
2192130812Smarcel	case C_HIDEXT:
2193130812Smarcel	  {
2194130812Smarcel	    /* The CSECT auxent--always the last auxent.  */
2195130812Smarcel	    union internal_auxent csect_aux;
2196130812Smarcel	    unsigned int symnum_before = ssymnum;
2197130812Smarcel
2198130812Smarcel	    swap_sym (&symbol, &main_aux[0], &namestring, &sraw_symbol,
2199130812Smarcel		      &ssymnum, objfile);
2200130812Smarcel	    if (symbol.n_numaux > 1)
2201130812Smarcel	      {
2202130812Smarcel		bfd_coff_swap_aux_in
2203130812Smarcel		  (objfile->obfd,
2204130812Smarcel		   sraw_symbol - coff_data (abfd)->local_symesz,
2205130812Smarcel		   symbol.n_type,
2206130812Smarcel		   symbol.n_sclass,
2207130812Smarcel		   symbol.n_numaux - 1,
2208130812Smarcel		   symbol.n_numaux,
2209130812Smarcel		   &csect_aux);
2210130812Smarcel	      }
2211130812Smarcel	    else
2212130812Smarcel	      csect_aux = main_aux[0];
2213130812Smarcel
2214130812Smarcel	    /* If symbol name starts with ".$" or "$", ignore it.  */
2215130812Smarcel	    if (namestring[0] == '$'
2216130812Smarcel		|| (namestring[0] == '.' && namestring[1] == '$'))
2217130812Smarcel	      break;
2218130812Smarcel
2219130812Smarcel	    switch (csect_aux.x_csect.x_smtyp & 0x7)
2220130812Smarcel	      {
2221130812Smarcel	      case XTY_SD:
2222130812Smarcel		switch (csect_aux.x_csect.x_smclas)
2223130812Smarcel		  {
2224130812Smarcel		  case XMC_PR:
2225130812Smarcel		    if (last_csect_name)
2226130812Smarcel		      {
2227130812Smarcel			/* If no misc. function recorded in the last
2228130812Smarcel			   seen csect, enter it as a function. This
2229130812Smarcel			   will take care of functions like strcmp()
2230130812Smarcel			   compiled by xlc.  */
2231130812Smarcel
2232130812Smarcel			if (!misc_func_recorded)
2233130812Smarcel			  {
2234130812Smarcel			    RECORD_MINIMAL_SYMBOL
2235130812Smarcel			      (last_csect_name, last_csect_val,
2236130812Smarcel			       mst_text, last_csect_sec,
2237130812Smarcel			       objfile);
2238130812Smarcel			  }
2239130812Smarcel
2240130812Smarcel			if (pst != NULL)
2241130812Smarcel			  {
2242130812Smarcel			    /* We have to allocate one psymtab for
2243130812Smarcel			       each program csect, because their text
2244130812Smarcel			       sections need not be adjacent.  */
2245130812Smarcel			    xcoff_end_psymtab
2246130812Smarcel			      (pst, psymtab_include_list, includes_used,
2247130812Smarcel			       symnum_before, dependency_list,
2248130812Smarcel			       dependencies_used, textlow_not_set);
2249130812Smarcel			    includes_used = 0;
2250130812Smarcel			    dependencies_used = 0;
2251130812Smarcel			    /* Give all psymtabs for this source file the same
2252130812Smarcel			       name.  */
2253130812Smarcel			    pst = xcoff_start_psymtab
2254130812Smarcel			      (objfile,
2255130812Smarcel			       filestring,
2256130812Smarcel			       symnum_before,
2257130812Smarcel			       objfile->global_psymbols.next,
2258130812Smarcel			       objfile->static_psymbols.next);
2259130812Smarcel			  }
2260130812Smarcel		      }
2261130812Smarcel		    /* Activate the misc_func_recorded mechanism for
2262130812Smarcel		       compiler- and linker-generated CSECTs like ".strcmp"
2263130812Smarcel		       and "@FIX1".  */
2264130812Smarcel		    if (namestring && (namestring[0] == '.'
2265130812Smarcel				       || namestring[0] == '@'))
2266130812Smarcel		      {
2267130812Smarcel			last_csect_name = namestring;
2268130812Smarcel			last_csect_val = symbol.n_value;
2269130812Smarcel			last_csect_sec =
2270130812Smarcel			  secnum_to_section (symbol.n_scnum, objfile);
2271130812Smarcel		      }
2272130812Smarcel		    if (pst != NULL)
2273130812Smarcel		      {
2274130812Smarcel			CORE_ADDR highval =
2275130812Smarcel			symbol.n_value + csect_aux.x_csect.x_scnlen.l;
2276130812Smarcel			if (highval > pst->texthigh)
2277130812Smarcel			  pst->texthigh = highval;
2278130812Smarcel			if (pst->textlow == 0 || symbol.n_value < pst->textlow)
2279130812Smarcel			  pst->textlow = symbol.n_value;
2280130812Smarcel		      }
2281130812Smarcel		    misc_func_recorded = 0;
2282130812Smarcel		    break;
2283130812Smarcel
2284130812Smarcel		  case XMC_RW:
2285130812Smarcel		  case XMC_TD:
2286130812Smarcel		    /* Data variables are recorded in the minimal symbol
2287130812Smarcel		       table, except for section symbols.  */
2288130812Smarcel		    if (*namestring != '.')
2289130812Smarcel		      prim_record_minimal_symbol_and_info
2290130812Smarcel			(namestring, symbol.n_value,
2291130812Smarcel			 sclass == C_HIDEXT ? mst_file_data : mst_data,
2292130812Smarcel			 NULL, secnum_to_section (symbol.n_scnum, objfile),
2293130812Smarcel			 NULL, objfile);
2294130812Smarcel		    break;
2295130812Smarcel
2296130812Smarcel		  case XMC_TC0:
2297130812Smarcel		    if (toc_offset)
2298130812Smarcel		      warning ("More than one XMC_TC0 symbol found.");
2299130812Smarcel		    toc_offset = symbol.n_value;
2300130812Smarcel
2301130812Smarcel		    /* Make TOC offset relative to start address of section.  */
2302130812Smarcel		    bfd_sect = secnum_to_bfd_section (symbol.n_scnum, objfile);
2303130812Smarcel		    if (bfd_sect)
2304130812Smarcel		      toc_offset -= bfd_section_vma (objfile->obfd, bfd_sect);
2305130812Smarcel		    break;
2306130812Smarcel
2307130812Smarcel		  case XMC_TC:
2308130812Smarcel		    /* These symbols tell us where the TOC entry for a
2309130812Smarcel		       variable is, not the variable itself.  */
2310130812Smarcel		    break;
2311130812Smarcel
2312130812Smarcel		  default:
2313130812Smarcel		    break;
2314130812Smarcel		  }
2315130812Smarcel		break;
2316130812Smarcel
2317130812Smarcel	      case XTY_LD:
2318130812Smarcel		switch (csect_aux.x_csect.x_smclas)
2319130812Smarcel		  {
2320130812Smarcel		  case XMC_PR:
2321130812Smarcel		    /* A function entry point.  */
2322130812Smarcel
2323130812Smarcel		    if (first_fun_line_offset == 0 && symbol.n_numaux > 1)
2324130812Smarcel		      first_fun_line_offset =
2325130812Smarcel			main_aux[0].x_sym.x_fcnary.x_fcn.x_lnnoptr;
2326130812Smarcel		    RECORD_MINIMAL_SYMBOL
2327130812Smarcel		      (namestring, symbol.n_value,
2328130812Smarcel		       sclass == C_HIDEXT ? mst_file_text : mst_text,
2329130812Smarcel		       secnum_to_section (symbol.n_scnum, objfile),
2330130812Smarcel		       objfile);
2331130812Smarcel		    break;
2332130812Smarcel
2333130812Smarcel		  case XMC_GL:
2334130812Smarcel		    /* shared library function trampoline code entry
2335130812Smarcel		       point. */
2336130812Smarcel
2337130812Smarcel		    /* record trampoline code entries as
2338130812Smarcel		       mst_solib_trampoline symbol.  When we lookup mst
2339130812Smarcel		       symbols, we will choose mst_text over
2340130812Smarcel		       mst_solib_trampoline. */
2341130812Smarcel		    RECORD_MINIMAL_SYMBOL
2342130812Smarcel		      (namestring, symbol.n_value,
2343130812Smarcel		       mst_solib_trampoline,
2344130812Smarcel		       secnum_to_section (symbol.n_scnum, objfile),
2345130812Smarcel		       objfile);
2346130812Smarcel		    break;
2347130812Smarcel
2348130812Smarcel		  case XMC_DS:
2349130812Smarcel		    /* The symbols often have the same names as
2350130812Smarcel		       debug symbols for functions, and confuse
2351130812Smarcel		       lookup_symbol.  */
2352130812Smarcel		    break;
2353130812Smarcel
2354130812Smarcel		  default:
2355130812Smarcel
2356130812Smarcel		    /* xlc puts each variable in a separate csect,
2357130812Smarcel		       so we get an XTY_SD for each variable.  But
2358130812Smarcel		       gcc puts several variables in a csect, so
2359130812Smarcel		       that each variable only gets an XTY_LD.  We
2360130812Smarcel		       still need to record them.  This will
2361130812Smarcel		       typically be XMC_RW; I suspect XMC_RO and
2362130812Smarcel		       XMC_BS might be possible too.  */
2363130812Smarcel		    if (*namestring != '.')
2364130812Smarcel		      prim_record_minimal_symbol_and_info
2365130812Smarcel			(namestring, symbol.n_value,
2366130812Smarcel			 sclass == C_HIDEXT ? mst_file_data : mst_data,
2367130812Smarcel			 NULL, secnum_to_section (symbol.n_scnum, objfile),
2368130812Smarcel			 NULL, objfile);
2369130812Smarcel		    break;
2370130812Smarcel		  }
2371130812Smarcel		break;
2372130812Smarcel
2373130812Smarcel	      case XTY_CM:
2374130812Smarcel		switch (csect_aux.x_csect.x_smclas)
2375130812Smarcel		  {
2376130812Smarcel		  case XMC_RW:
2377130812Smarcel		  case XMC_BS:
2378130812Smarcel		    /* Common variables are recorded in the minimal symbol
2379130812Smarcel		       table, except for section symbols.  */
2380130812Smarcel		    if (*namestring != '.')
2381130812Smarcel		      prim_record_minimal_symbol_and_info
2382130812Smarcel			(namestring, symbol.n_value,
2383130812Smarcel			 sclass == C_HIDEXT ? mst_file_bss : mst_bss,
2384130812Smarcel			 NULL, secnum_to_section (symbol.n_scnum, objfile),
2385130812Smarcel			 NULL, objfile);
2386130812Smarcel		    break;
2387130812Smarcel		  }
2388130812Smarcel		break;
2389130812Smarcel
2390130812Smarcel	      default:
2391130812Smarcel		break;
2392130812Smarcel	      }
2393130812Smarcel	  }
2394130812Smarcel	  break;
2395130812Smarcel	case C_FILE:
2396130812Smarcel	  {
2397130812Smarcel	    unsigned int symnum_before;
2398130812Smarcel
2399130812Smarcel	    symnum_before = ssymnum;
2400130812Smarcel	    swap_sym (&symbol, &main_aux[0], &namestring, &sraw_symbol,
2401130812Smarcel		      &ssymnum, objfile);
2402130812Smarcel
2403130812Smarcel	    /* See if the last csect needs to be recorded.  */
2404130812Smarcel
2405130812Smarcel	    if (last_csect_name && !misc_func_recorded)
2406130812Smarcel	      {
2407130812Smarcel
2408130812Smarcel		/* If no misc. function recorded in the last seen csect, enter
2409130812Smarcel		   it as a function.  This will take care of functions like
2410130812Smarcel		   strcmp() compiled by xlc.  */
2411130812Smarcel
2412130812Smarcel		RECORD_MINIMAL_SYMBOL
2413130812Smarcel		  (last_csect_name, last_csect_val,
2414130812Smarcel		   mst_text, last_csect_sec, objfile);
2415130812Smarcel	      }
2416130812Smarcel
2417130812Smarcel	    if (pst)
2418130812Smarcel	      {
2419130812Smarcel		xcoff_end_psymtab (pst, psymtab_include_list, includes_used,
2420130812Smarcel				   symnum_before, dependency_list,
2421130812Smarcel				   dependencies_used, textlow_not_set);
2422130812Smarcel		includes_used = 0;
2423130812Smarcel		dependencies_used = 0;
2424130812Smarcel	      }
2425130812Smarcel	    first_fun_line_offset = 0;
2426130812Smarcel
2427130812Smarcel	    /* XCOFF, according to the AIX 3.2 documentation, puts the
2428130812Smarcel	       filename in cs->c_name.  But xlc 1.3.0.2 has decided to
2429130812Smarcel	       do things the standard COFF way and put it in the auxent.
2430130812Smarcel	       We use the auxent if the symbol is ".file" and an auxent
2431130812Smarcel	       exists, otherwise use the symbol itself.  */
2432130812Smarcel	    if (!strcmp (namestring, ".file") && symbol.n_numaux > 0)
2433130812Smarcel	      {
2434130812Smarcel		filestring = coff_getfilename (&main_aux[0], objfile);
2435130812Smarcel	      }
2436130812Smarcel	    else
2437130812Smarcel	      filestring = namestring;
2438130812Smarcel
2439130812Smarcel	    pst = xcoff_start_psymtab (objfile,
2440130812Smarcel				       filestring,
2441130812Smarcel				       symnum_before,
2442130812Smarcel				       objfile->global_psymbols.next,
2443130812Smarcel				       objfile->static_psymbols.next);
2444130812Smarcel	    last_csect_name = NULL;
2445130812Smarcel	  }
2446130812Smarcel	  break;
2447130812Smarcel
2448130812Smarcel	default:
2449130812Smarcel	  {
2450130812Smarcel	    complaint (&symfile_complaints,
2451130812Smarcel		       "Storage class %d not recognized during scan", sclass);
2452130812Smarcel	  }
2453130812Smarcel	  /* FALLTHROUGH */
2454130812Smarcel
2455130812Smarcel	  /* C_FCN is .bf and .ef symbols.  I think it is sufficient
2456130812Smarcel	     to handle only the C_FUN and C_EXT.  */
2457130812Smarcel	case C_FCN:
2458130812Smarcel
2459130812Smarcel	case C_BSTAT:
2460130812Smarcel	case C_ESTAT:
2461130812Smarcel	case C_ARG:
2462130812Smarcel	case C_REGPARM:
2463130812Smarcel	case C_REG:
2464130812Smarcel	case C_TPDEF:
2465130812Smarcel	case C_STRTAG:
2466130812Smarcel	case C_UNTAG:
2467130812Smarcel	case C_ENTAG:
2468130812Smarcel	case C_LABEL:
2469130812Smarcel	case C_NULL:
2470130812Smarcel
2471130812Smarcel	  /* C_EINCL means we are switching back to the main file.  But there
2472130812Smarcel	     is no reason to care; the only thing we want to know about
2473130812Smarcel	     includes is the names of all the included (.h) files.  */
2474130812Smarcel	case C_EINCL:
2475130812Smarcel
2476130812Smarcel	case C_BLOCK:
2477130812Smarcel
2478130812Smarcel	  /* I don't think C_STAT is used in xcoff; C_HIDEXT appears to be
2479130812Smarcel	     used instead.  */
2480130812Smarcel	case C_STAT:
2481130812Smarcel
2482130812Smarcel	  /* I don't think the name of the common block (as opposed to the
2483130812Smarcel	     variables within it) is something which is user visible
2484130812Smarcel	     currently.  */
2485130812Smarcel	case C_BCOMM:
2486130812Smarcel	case C_ECOMM:
2487130812Smarcel
2488130812Smarcel	case C_PSYM:
2489130812Smarcel	case C_RPSYM:
2490130812Smarcel
2491130812Smarcel	  /* I think we can ignore C_LSYM; types on xcoff seem to use C_DECL
2492130812Smarcel	     so C_LSYM would appear to be only for locals.  */
2493130812Smarcel	case C_LSYM:
2494130812Smarcel
2495130812Smarcel	case C_AUTO:
2496130812Smarcel	case C_RSYM:
2497130812Smarcel	  {
2498130812Smarcel	    /* We probably could save a few instructions by assuming that
2499130812Smarcel	       C_LSYM, C_PSYM, etc., never have auxents.  */
2500130812Smarcel	    int naux1 = symbol.n_numaux + 1;
2501130812Smarcel	    ssymnum += naux1;
2502130812Smarcel	    sraw_symbol += bfd_coff_symesz (abfd) * naux1;
2503130812Smarcel	  }
2504130812Smarcel	  break;
2505130812Smarcel
2506130812Smarcel	case C_BINCL:
2507130812Smarcel	  {
2508130812Smarcel	    /* Mark down an include file in the current psymtab */
2509130812Smarcel	    enum language tmp_language;
2510130812Smarcel	    swap_sym (&symbol, &main_aux[0], &namestring, &sraw_symbol,
2511130812Smarcel		      &ssymnum, objfile);
2512130812Smarcel
2513130812Smarcel	    tmp_language = deduce_language_from_filename (namestring);
2514130812Smarcel
2515130812Smarcel	    /* Only change the psymtab's language if we've learned
2516130812Smarcel	       something useful (eg. tmp_language is not language_unknown).
2517130812Smarcel	       In addition, to match what start_subfile does, never change
2518130812Smarcel	       from C++ to C.  */
2519130812Smarcel	    if (tmp_language != language_unknown
2520130812Smarcel		&& (tmp_language != language_c
2521130812Smarcel		    || psymtab_language != language_cplus))
2522130812Smarcel	      psymtab_language = tmp_language;
2523130812Smarcel
2524130812Smarcel	    /* In C++, one may expect the same filename to come round many
2525130812Smarcel	       times, when code is coming alternately from the main file
2526130812Smarcel	       and from inline functions in other files. So I check to see
2527130812Smarcel	       if this is a file we've seen before -- either the main
2528130812Smarcel	       source file, or a previously included file.
2529130812Smarcel
2530130812Smarcel	       This seems to be a lot of time to be spending on N_SOL, but
2531130812Smarcel	       things like "break c-exp.y:435" need to work (I
2532130812Smarcel	       suppose the psymtab_include_list could be hashed or put
2533130812Smarcel	       in a binary tree, if profiling shows this is a major hog).  */
2534130812Smarcel	    if (pst && DEPRECATED_STREQ (namestring, pst->filename))
2535130812Smarcel	      continue;
2536130812Smarcel	    {
2537130812Smarcel	      int i;
2538130812Smarcel	      for (i = 0; i < includes_used; i++)
2539130812Smarcel		if (DEPRECATED_STREQ (namestring, psymtab_include_list[i]))
2540130812Smarcel		  {
2541130812Smarcel		    i = -1;
2542130812Smarcel		    break;
2543130812Smarcel		  }
2544130812Smarcel	      if (i == -1)
2545130812Smarcel		continue;
2546130812Smarcel	    }
2547130812Smarcel	    psymtab_include_list[includes_used++] = namestring;
2548130812Smarcel	    if (includes_used >= includes_allocated)
2549130812Smarcel	      {
2550130812Smarcel		char **orig = psymtab_include_list;
2551130812Smarcel
2552130812Smarcel		psymtab_include_list = (char **)
2553130812Smarcel		  alloca ((includes_allocated *= 2) *
2554130812Smarcel			  sizeof (char *));
2555130812Smarcel		memcpy (psymtab_include_list, orig,
2556130812Smarcel			includes_used * sizeof (char *));
2557130812Smarcel	      }
2558130812Smarcel	    continue;
2559130812Smarcel	  }
2560130812Smarcel	case C_FUN:
2561130812Smarcel	  /* The value of the C_FUN is not the address of the function (it
2562130812Smarcel	     appears to be the address before linking), but as long as it
2563130812Smarcel	     is smaller than the actual address, then find_pc_partial_function
2564130812Smarcel	     will use the minimal symbols instead.  I hope.  */
2565130812Smarcel
2566130812Smarcel	case C_GSYM:
2567130812Smarcel	case C_ECOML:
2568130812Smarcel	case C_DECL:
2569130812Smarcel	case C_STSYM:
2570130812Smarcel	  {
2571130812Smarcel	    char *p;
2572130812Smarcel	    swap_sym (&symbol, &main_aux[0], &namestring, &sraw_symbol,
2573130812Smarcel		      &ssymnum, objfile);
2574130812Smarcel
2575130812Smarcel	    p = (char *) strchr (namestring, ':');
2576130812Smarcel	    if (!p)
2577130812Smarcel	      continue;			/* Not a debugging symbol.   */
2578130812Smarcel
2579130812Smarcel	    /* Main processing section for debugging symbols which
2580130812Smarcel	       the initial read through the symbol tables needs to worry
2581130812Smarcel	       about.  If we reach this point, the symbol which we are
2582130812Smarcel	       considering is definitely one we are interested in.
2583130812Smarcel	       p must also contain the (valid) index into the namestring
2584130812Smarcel	       which indicates the debugging type symbol.  */
2585130812Smarcel
2586130812Smarcel	    switch (p[1])
2587130812Smarcel	      {
2588130812Smarcel	      case 'S':
2589130812Smarcel		symbol.n_value += ANOFFSET (objfile->section_offsets, SECT_OFF_DATA (objfile));
2590130812Smarcel#ifdef STATIC_TRANSFORM_NAME
2591130812Smarcel		namestring = STATIC_TRANSFORM_NAME (namestring);
2592130812Smarcel#endif
2593130812Smarcel		add_psymbol_to_list (namestring, p - namestring,
2594130812Smarcel				     VAR_DOMAIN, LOC_STATIC,
2595130812Smarcel				     &objfile->static_psymbols,
2596130812Smarcel				     0, symbol.n_value,
2597130812Smarcel				     psymtab_language, objfile);
2598130812Smarcel		continue;
2599130812Smarcel
2600130812Smarcel	      case 'G':
2601130812Smarcel		symbol.n_value += ANOFFSET (objfile->section_offsets, SECT_OFF_DATA (objfile));
2602130812Smarcel		/* The addresses in these entries are reported to be
2603130812Smarcel		   wrong.  See the code that reads 'G's for symtabs. */
2604130812Smarcel		add_psymbol_to_list (namestring, p - namestring,
2605130812Smarcel				     VAR_DOMAIN, LOC_STATIC,
2606130812Smarcel				     &objfile->global_psymbols,
2607130812Smarcel				     0, symbol.n_value,
2608130812Smarcel				     psymtab_language, objfile);
2609130812Smarcel		continue;
2610130812Smarcel
2611130812Smarcel	      case 'T':
2612130812Smarcel		/* When a 'T' entry is defining an anonymous enum, it
2613130812Smarcel		   may have a name which is the empty string, or a
2614130812Smarcel		   single space.  Since they're not really defining a
2615130812Smarcel		   symbol, those shouldn't go in the partial symbol
2616130812Smarcel		   table.  We do pick up the elements of such enums at
2617130812Smarcel		   'check_enum:', below.  */
2618130812Smarcel		if (p >= namestring + 2
2619130812Smarcel		    || (p == namestring + 1
2620130812Smarcel			&& namestring[0] != ' '))
2621130812Smarcel		  {
2622130812Smarcel		    add_psymbol_to_list (namestring, p - namestring,
2623130812Smarcel					 STRUCT_DOMAIN, LOC_TYPEDEF,
2624130812Smarcel					 &objfile->static_psymbols,
2625130812Smarcel					 symbol.n_value, 0,
2626130812Smarcel					 psymtab_language, objfile);
2627130812Smarcel		    if (p[2] == 't')
2628130812Smarcel		      {
2629130812Smarcel			/* Also a typedef with the same name.  */
2630130812Smarcel			add_psymbol_to_list (namestring, p - namestring,
2631130812Smarcel					     VAR_DOMAIN, LOC_TYPEDEF,
2632130812Smarcel					     &objfile->static_psymbols,
2633130812Smarcel					     symbol.n_value, 0,
2634130812Smarcel					     psymtab_language, objfile);
2635130812Smarcel			p += 1;
2636130812Smarcel		      }
2637130812Smarcel		  }
2638130812Smarcel		goto check_enum;
2639130812Smarcel
2640130812Smarcel	      case 't':
2641130812Smarcel		if (p != namestring)	/* a name is there, not just :T... */
2642130812Smarcel		  {
2643130812Smarcel		    add_psymbol_to_list (namestring, p - namestring,
2644130812Smarcel					 VAR_DOMAIN, LOC_TYPEDEF,
2645130812Smarcel					 &objfile->static_psymbols,
2646130812Smarcel					 symbol.n_value, 0,
2647130812Smarcel					 psymtab_language, objfile);
2648130812Smarcel		  }
2649130812Smarcel	      check_enum:
2650130812Smarcel		/* If this is an enumerated type, we need to
2651130812Smarcel		   add all the enum constants to the partial symbol
2652130812Smarcel		   table.  This does not cover enums without names, e.g.
2653130812Smarcel		   "enum {a, b} c;" in C, but fortunately those are
2654130812Smarcel		   rare.  There is no way for GDB to find those from the
2655130812Smarcel		   enum type without spending too much time on it.  Thus
2656130812Smarcel		   to solve this problem, the compiler needs to put out the
2657130812Smarcel		   enum in a nameless type.  GCC2 does this.  */
2658130812Smarcel
2659130812Smarcel		/* We are looking for something of the form
2660130812Smarcel		   <name> ":" ("t" | "T") [<number> "="] "e"
2661130812Smarcel		   {<constant> ":" <value> ","} ";".  */
2662130812Smarcel
2663130812Smarcel		/* Skip over the colon and the 't' or 'T'.  */
2664130812Smarcel		p += 2;
2665130812Smarcel		/* This type may be given a number.  Also, numbers can come
2666130812Smarcel		   in pairs like (0,26).  Skip over it.  */
2667130812Smarcel		while ((*p >= '0' && *p <= '9')
2668130812Smarcel		       || *p == '(' || *p == ',' || *p == ')'
2669130812Smarcel		       || *p == '=')
2670130812Smarcel		  p++;
2671130812Smarcel
2672130812Smarcel		if (*p++ == 'e')
2673130812Smarcel		  {
2674130812Smarcel		    /* The aix4 compiler emits extra crud before the members.  */
2675130812Smarcel		    if (*p == '-')
2676130812Smarcel		      {
2677130812Smarcel			/* Skip over the type (?).  */
2678130812Smarcel			while (*p != ':')
2679130812Smarcel			  p++;
2680130812Smarcel
2681130812Smarcel			/* Skip over the colon.  */
2682130812Smarcel			p++;
2683130812Smarcel		      }
2684130812Smarcel
2685130812Smarcel		    /* We have found an enumerated type.  */
2686130812Smarcel		    /* According to comments in read_enum_type
2687130812Smarcel		       a comma could end it instead of a semicolon.
2688130812Smarcel		       I don't know where that happens.
2689130812Smarcel		       Accept either.  */
2690130812Smarcel		    while (*p && *p != ';' && *p != ',')
2691130812Smarcel		      {
2692130812Smarcel			char *q;
2693130812Smarcel
2694130812Smarcel			/* Check for and handle cretinous dbx symbol name
2695130812Smarcel			   continuation!  */
2696130812Smarcel			if (*p == '\\' || (*p == '?' && p[1] == '\0'))
2697130812Smarcel			  p = next_symbol_text (objfile);
2698130812Smarcel
2699130812Smarcel			/* Point to the character after the name
2700130812Smarcel			   of the enum constant.  */
2701130812Smarcel			for (q = p; *q && *q != ':'; q++)
2702130812Smarcel			  ;
2703130812Smarcel			/* Note that the value doesn't matter for
2704130812Smarcel			   enum constants in psymtabs, just in symtabs.  */
2705130812Smarcel			add_psymbol_to_list (p, q - p,
2706130812Smarcel					     VAR_DOMAIN, LOC_CONST,
2707130812Smarcel					     &objfile->static_psymbols, 0,
2708130812Smarcel					     0, psymtab_language, objfile);
2709130812Smarcel			/* Point past the name.  */
2710130812Smarcel			p = q;
2711130812Smarcel			/* Skip over the value.  */
2712130812Smarcel			while (*p && *p != ',')
2713130812Smarcel			  p++;
2714130812Smarcel			/* Advance past the comma.  */
2715130812Smarcel			if (*p)
2716130812Smarcel			  p++;
2717130812Smarcel		      }
2718130812Smarcel		  }
2719130812Smarcel		continue;
2720130812Smarcel
2721130812Smarcel	      case 'c':
2722130812Smarcel		/* Constant, e.g. from "const" in Pascal.  */
2723130812Smarcel		add_psymbol_to_list (namestring, p - namestring,
2724130812Smarcel				     VAR_DOMAIN, LOC_CONST,
2725130812Smarcel				     &objfile->static_psymbols, symbol.n_value,
2726130812Smarcel				     0, psymtab_language, objfile);
2727130812Smarcel		continue;
2728130812Smarcel
2729130812Smarcel	      case 'f':
2730130812Smarcel		if (! pst)
2731130812Smarcel		  {
2732130812Smarcel		    int name_len = p - namestring;
2733130812Smarcel		    char *name = xmalloc (name_len + 1);
2734130812Smarcel		    memcpy (name, namestring, name_len);
2735130812Smarcel		    name[name_len] = '\0';
2736130812Smarcel		    function_outside_compilation_unit_complaint (name);
2737130812Smarcel		    xfree (name);
2738130812Smarcel		  }
2739130812Smarcel		symbol.n_value += ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2740130812Smarcel		add_psymbol_to_list (namestring, p - namestring,
2741130812Smarcel				     VAR_DOMAIN, LOC_BLOCK,
2742130812Smarcel				     &objfile->static_psymbols,
2743130812Smarcel				     0, symbol.n_value,
2744130812Smarcel				     psymtab_language, objfile);
2745130812Smarcel		continue;
2746130812Smarcel
2747130812Smarcel		/* Global functions were ignored here, but now they
2748130812Smarcel		   are put into the global psymtab like one would expect.
2749130812Smarcel		   They're also in the minimal symbol table.  */
2750130812Smarcel	      case 'F':
2751130812Smarcel		if (! pst)
2752130812Smarcel		  {
2753130812Smarcel		    int name_len = p - namestring;
2754130812Smarcel		    char *name = xmalloc (name_len + 1);
2755130812Smarcel		    memcpy (name, namestring, name_len);
2756130812Smarcel		    name[name_len] = '\0';
2757130812Smarcel		    function_outside_compilation_unit_complaint (name);
2758130812Smarcel		    xfree (name);
2759130812Smarcel		  }
2760130812Smarcel		symbol.n_value += ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2761130812Smarcel		add_psymbol_to_list (namestring, p - namestring,
2762130812Smarcel				     VAR_DOMAIN, LOC_BLOCK,
2763130812Smarcel				     &objfile->global_psymbols,
2764130812Smarcel				     0, symbol.n_value,
2765130812Smarcel				     psymtab_language, objfile);
2766130812Smarcel		continue;
2767130812Smarcel
2768130812Smarcel		/* Two things show up here (hopefully); static symbols of
2769130812Smarcel		   local scope (static used inside braces) or extensions
2770130812Smarcel		   of structure symbols.  We can ignore both.  */
2771130812Smarcel	      case 'V':
2772130812Smarcel	      case '(':
2773130812Smarcel	      case '0':
2774130812Smarcel	      case '1':
2775130812Smarcel	      case '2':
2776130812Smarcel	      case '3':
2777130812Smarcel	      case '4':
2778130812Smarcel	      case '5':
2779130812Smarcel	      case '6':
2780130812Smarcel	      case '7':
2781130812Smarcel	      case '8':
2782130812Smarcel	      case '9':
2783130812Smarcel	      case '-':
2784130812Smarcel	      case '#':		/* for symbol identification (used in live ranges) */
2785130812Smarcel		continue;
2786130812Smarcel
2787130812Smarcel	      case ':':
2788130812Smarcel		/* It is a C++ nested symbol.  We don't need to record it
2789130812Smarcel		   (I don't think); if we try to look up foo::bar::baz,
2790130812Smarcel		   then symbols for the symtab containing foo should get
2791130812Smarcel		   read in, I think.  */
2792130812Smarcel		/* Someone says sun cc puts out symbols like
2793130812Smarcel		   /foo/baz/maclib::/usr/local/bin/maclib,
2794130812Smarcel		   which would get here with a symbol type of ':'.  */
2795130812Smarcel		continue;
2796130812Smarcel
2797130812Smarcel	      default:
2798130812Smarcel		/* Unexpected symbol descriptor.  The second and subsequent stabs
2799130812Smarcel		   of a continued stab can show up here.  The question is
2800130812Smarcel		   whether they ever can mimic a normal stab--it would be
2801130812Smarcel		   nice if not, since we certainly don't want to spend the
2802130812Smarcel		   time searching to the end of every string looking for
2803130812Smarcel		   a backslash.  */
2804130812Smarcel
2805130812Smarcel		complaint (&symfile_complaints,
2806130812Smarcel			   "unknown symbol descriptor `%c'", p[1]);
2807130812Smarcel
2808130812Smarcel		/* Ignore it; perhaps it is an extension that we don't
2809130812Smarcel		   know about.  */
2810130812Smarcel		continue;
2811130812Smarcel	      }
2812130812Smarcel	  }
2813130812Smarcel	}
2814130812Smarcel    }
2815130812Smarcel
2816130812Smarcel  if (pst)
2817130812Smarcel    {
2818130812Smarcel      xcoff_end_psymtab (pst, psymtab_include_list, includes_used,
2819130812Smarcel			 ssymnum, dependency_list,
2820130812Smarcel			 dependencies_used, textlow_not_set);
2821130812Smarcel    }
2822130812Smarcel
2823130812Smarcel  /* Record the toc offset value of this symbol table into objfile structure.
2824130812Smarcel     If no XMC_TC0 is found, toc_offset should be zero. Another place to obtain
2825130812Smarcel     this information would be file auxiliary header. */
2826130812Smarcel
2827130812Smarcel  ((struct coff_symfile_info *) objfile->sym_private)->toc_offset = toc_offset;
2828130812Smarcel}
2829130812Smarcel
2830130812Smarcel/* Return the toc offset value for a given objfile.  */
2831130812Smarcel
2832130812SmarcelCORE_ADDR
2833130812Smarcelget_toc_offset (struct objfile *objfile)
2834130812Smarcel{
2835130812Smarcel  if (objfile)
2836130812Smarcel    return ((struct coff_symfile_info *) objfile->sym_private)->toc_offset;
2837130812Smarcel  return 0;
2838130812Smarcel}
2839130812Smarcel
2840130812Smarcel/* Scan and build partial symbols for a symbol file.
2841130812Smarcel   We have been initialized by a call to dbx_symfile_init, which
2842130812Smarcel   put all the relevant info into a "struct dbx_symfile_info",
2843130812Smarcel   hung off the objfile structure.
2844130812Smarcel
2845130812Smarcel   SECTION_OFFSETS contains offsets relative to which the symbols in the
2846130812Smarcel   various sections are (depending where the sections were actually loaded).
2847130812Smarcel   MAINLINE is true if we are reading the main symbol
2848130812Smarcel   table (as opposed to a shared lib or dynamically loaded file).  */
2849130812Smarcel
2850130812Smarcelstatic void
2851130812Smarcelxcoff_initial_scan (struct objfile *objfile, int mainline)
2852130812Smarcel{
2853130812Smarcel  bfd *abfd;
2854130812Smarcel  int val;
2855130812Smarcel  struct cleanup *back_to;
2856130812Smarcel  int num_symbols;		/* # of symbols */
2857130812Smarcel  file_ptr symtab_offset;	/* symbol table and */
2858130812Smarcel  file_ptr stringtab_offset;	/* string table file offsets */
2859130812Smarcel  struct coff_symfile_info *info;
2860130812Smarcel  char *name;
2861130812Smarcel  unsigned int size;
2862130812Smarcel
2863130812Smarcel  info = (struct coff_symfile_info *) objfile->sym_private;
2864130812Smarcel  symfile_bfd = abfd = objfile->obfd;
2865130812Smarcel  name = objfile->name;
2866130812Smarcel
2867130812Smarcel  num_symbols = bfd_get_symcount (abfd);	/* # of symbols */
2868130812Smarcel  symtab_offset = obj_sym_filepos (abfd);	/* symbol table file offset */
2869130812Smarcel  stringtab_offset = symtab_offset +
2870130812Smarcel    num_symbols * coff_data (abfd)->local_symesz;
2871130812Smarcel
2872130812Smarcel  info->min_lineno_offset = 0;
2873130812Smarcel  info->max_lineno_offset = 0;
2874130812Smarcel  bfd_map_over_sections (abfd, find_linenos, info);
2875130812Smarcel
2876130812Smarcel  if (num_symbols > 0)
2877130812Smarcel    {
2878130812Smarcel      /* Read the string table.  */
2879130812Smarcel      init_stringtab (abfd, stringtab_offset, objfile);
2880130812Smarcel
2881130812Smarcel      /* Read the .debug section, if present.  */
2882130812Smarcel      {
2883130812Smarcel	struct bfd_section *secp;
2884130812Smarcel	bfd_size_type length;
2885130812Smarcel	char *debugsec = NULL;
2886130812Smarcel
2887130812Smarcel	secp = bfd_get_section_by_name (abfd, ".debug");
2888130812Smarcel	if (secp)
2889130812Smarcel	  {
2890130812Smarcel	    length = bfd_section_size (abfd, secp);
2891130812Smarcel	    if (length)
2892130812Smarcel	      {
2893130812Smarcel		debugsec =
2894130812Smarcel		  (char *) obstack_alloc (&objfile->objfile_obstack, length);
2895130812Smarcel
2896130812Smarcel		if (!bfd_get_section_contents (abfd, secp, debugsec,
2897130812Smarcel					       (file_ptr) 0, length))
2898130812Smarcel		  {
2899130812Smarcel		    error ("Error reading .debug section of `%s': %s",
2900130812Smarcel			   name, bfd_errmsg (bfd_get_error ()));
2901130812Smarcel		  }
2902130812Smarcel	      }
2903130812Smarcel	  }
2904130812Smarcel	((struct coff_symfile_info *) objfile->sym_private)->debugsec =
2905130812Smarcel	  debugsec;
2906130812Smarcel      }
2907130812Smarcel    }
2908130812Smarcel
2909130812Smarcel  /* Read the symbols.  We keep them in core because we will want to
2910130812Smarcel     access them randomly in read_symbol*.  */
2911130812Smarcel  val = bfd_seek (abfd, symtab_offset, SEEK_SET);
2912130812Smarcel  if (val < 0)
2913130812Smarcel    error ("Error reading symbols from %s: %s",
2914130812Smarcel	   name, bfd_errmsg (bfd_get_error ()));
2915130812Smarcel  size = coff_data (abfd)->local_symesz * num_symbols;
2916130812Smarcel  ((struct coff_symfile_info *) objfile->sym_private)->symtbl =
2917130812Smarcel    obstack_alloc (&objfile->objfile_obstack, size);
2918130812Smarcel  ((struct coff_symfile_info *) objfile->sym_private)->symtbl_num_syms =
2919130812Smarcel    num_symbols;
2920130812Smarcel
2921130812Smarcel  val = bfd_bread (((struct coff_symfile_info *) objfile->sym_private)->symtbl,
2922130812Smarcel		   size, abfd);
2923130812Smarcel  if (val != size)
2924130812Smarcel    perror_with_name ("reading symbol table");
2925130812Smarcel
2926130812Smarcel  /* If we are reinitializing, or if we have never loaded syms yet, init */
2927130812Smarcel  if (mainline
2928130812Smarcel      || (objfile->global_psymbols.size == 0
2929130812Smarcel	  && objfile->static_psymbols.size == 0))
2930130812Smarcel    /* I'm not sure how how good num_symbols is; the rule of thumb in
2931130812Smarcel       init_psymbol_list was developed for a.out.  On the one hand,
2932130812Smarcel       num_symbols includes auxents.  On the other hand, it doesn't
2933130812Smarcel       include N_SLINE.  */
2934130812Smarcel    init_psymbol_list (objfile, num_symbols);
2935130812Smarcel
2936130812Smarcel  free_pending_blocks ();
2937130812Smarcel  back_to = make_cleanup (really_free_pendings, 0);
2938130812Smarcel
2939130812Smarcel  init_minimal_symbol_collection ();
2940130812Smarcel  make_cleanup_discard_minimal_symbols ();
2941130812Smarcel
2942130812Smarcel  /* Now that the symbol table data of the executable file are all in core,
2943130812Smarcel     process them and define symbols accordingly.  */
2944130812Smarcel
2945130812Smarcel  scan_xcoff_symtab (objfile);
2946130812Smarcel
2947130812Smarcel  /* Install any minimal symbols that have been collected as the current
2948130812Smarcel     minimal symbols for this objfile. */
2949130812Smarcel
2950130812Smarcel  install_minimal_symbols (objfile);
2951130812Smarcel
2952130812Smarcel  do_cleanups (back_to);
2953130812Smarcel}
2954130812Smarcel
2955130812Smarcelstatic void
2956130812Smarcelxcoff_symfile_offsets (struct objfile *objfile, struct section_addr_info *addrs)
2957130812Smarcel{
2958130812Smarcel  asection *sect = NULL;
2959130812Smarcel  int i;
2960130812Smarcel
2961130812Smarcel  objfile->num_sections = bfd_count_sections (objfile->obfd);
2962130812Smarcel  objfile->section_offsets = (struct section_offsets *)
2963130812Smarcel    obstack_alloc (&objfile->objfile_obstack,
2964130812Smarcel		   SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));
2965130812Smarcel
2966130812Smarcel  /* Initialize the section indexes for future use. */
2967130812Smarcel  sect = bfd_get_section_by_name (objfile->obfd, ".text");
2968130812Smarcel  if (sect)
2969130812Smarcel    objfile->sect_index_text = sect->index;
2970130812Smarcel
2971130812Smarcel  sect = bfd_get_section_by_name (objfile->obfd, ".data");
2972130812Smarcel  if (sect)
2973130812Smarcel    objfile->sect_index_data = sect->index;
2974130812Smarcel
2975130812Smarcel  sect = bfd_get_section_by_name (objfile->obfd, ".bss");
2976130812Smarcel  if (sect)
2977130812Smarcel    objfile->sect_index_bss = sect->index;
2978130812Smarcel
2979130812Smarcel  sect = bfd_get_section_by_name (objfile->obfd, ".rodata");
2980130812Smarcel  if (sect)
2981130812Smarcel    objfile->sect_index_rodata = sect->index;
2982130812Smarcel
2983130812Smarcel  for (i = 0; i < objfile->num_sections; ++i)
2984130812Smarcel    {
2985130812Smarcel      /* syms_from_objfile kindly subtracts from addr the
2986130812Smarcel	 bfd_section_vma of the .text section.  This strikes me as
2987130812Smarcel	 wrong--whether the offset to be applied to symbol reading is
2988130812Smarcel	 relative to the start address of the section depends on the
2989130812Smarcel	 symbol format.  In any event, this whole "addr" concept is
2990130812Smarcel	 pretty broken (it doesn't handle any section but .text
2991130812Smarcel	 sensibly), so just ignore the addr parameter and use 0.
2992130812Smarcel	 rs6000-nat.c will set the correct section offsets via
2993130812Smarcel	 objfile_relocate.  */
2994130812Smarcel	(objfile->section_offsets)->offsets[i] = 0;
2995130812Smarcel    }
2996130812Smarcel}
2997130812Smarcel
2998130812Smarcel/* Register our ability to parse symbols for xcoff BFD files.  */
2999130812Smarcel
3000130812Smarcelstatic struct sym_fns xcoff_sym_fns =
3001130812Smarcel{
3002130812Smarcel
3003130812Smarcel  /* It is possible that coff and xcoff should be merged as
3004130812Smarcel     they do have fundamental similarities (for example, the extra storage
3005130812Smarcel     classes used for stabs could presumably be recognized in any COFF file).
3006130812Smarcel     However, in addition to obvious things like all the csect hair, there are
3007130812Smarcel     some subtler differences between xcoffread.c and coffread.c, notably
3008130812Smarcel     the fact that coffread.c has no need to read in all the symbols, but
3009130812Smarcel     xcoffread.c reads all the symbols and does in fact randomly access them
3010130812Smarcel     (in C_BSTAT and line number processing).  */
3011130812Smarcel
3012130812Smarcel  bfd_target_xcoff_flavour,
3013130812Smarcel
3014130812Smarcel  xcoff_new_init,		/* sym_new_init: init anything gbl to entire symtab */
3015130812Smarcel  xcoff_symfile_init,		/* sym_init: read initial info, setup for sym_read() */
3016130812Smarcel  xcoff_initial_scan,		/* sym_read: read a symbol file into symtab */
3017130812Smarcel  xcoff_symfile_finish,		/* sym_finish: finished with file, cleanup */
3018130812Smarcel  xcoff_symfile_offsets,	/* sym_offsets: xlate offsets ext->int form */
3019130812Smarcel  NULL				/* next: pointer to next struct sym_fns */
3020130812Smarcel};
3021130812Smarcel
3022130812Smarcelvoid
3023130812Smarcel_initialize_xcoffread (void)
3024130812Smarcel{
3025130812Smarcel  add_symtab_fns (&xcoff_sym_fns);
3026130812Smarcel
3027130812Smarcel  func_symbol_type = init_type (TYPE_CODE_FUNC, 1, 0,
3028130812Smarcel				"<function, no debug info>", NULL);
3029130812Smarcel  TYPE_TARGET_TYPE (func_symbol_type) = builtin_type_int;
3030130812Smarcel  var_symbol_type =
3031130812Smarcel    init_type (TYPE_CODE_INT, TARGET_INT_BIT / HOST_CHAR_BIT, 0,
3032130812Smarcel	       "<variable, no debug info>", NULL);
3033130812Smarcel}
3034