1/* Read AIX xcoff symbol tables and convert to internal format, for GDB.
2   Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
3   1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
4   Free Software Foundation, Inc.
5   Derived from coffread.c, dbxread.c, and a lot of hacking.
6   Contributed by IBM Corporation.
7
8   This file is part of GDB.
9
10   This program is free software; you can redistribute it and/or modify
11   it under the terms of the GNU General Public License as published by
12   the Free Software Foundation; either version 2 of the License, or
13   (at your option) any later version.
14
15   This program is distributed in the hope that it will be useful,
16   but WITHOUT ANY WARRANTY; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   GNU General Public License for more details.
19
20   You should have received a copy of the GNU General Public License
21   along with this program; if not, write to the Free Software
22   Foundation, Inc., 59 Temple Place - Suite 330,
23   Boston, MA 02111-1307, USA.  */
24
25#include "defs.h"
26#include "bfd.h"
27
28#include <sys/types.h>
29#include <fcntl.h>
30#include <ctype.h>
31#include "gdb_string.h"
32
33#include <sys/param.h>
34#ifndef	NO_SYS_FILE
35#include <sys/file.h>
36#endif
37#include "gdb_stat.h"
38
39#include "coff/internal.h"
40#include "libcoff.h"		/* FIXME, internal data from BFD */
41#include "coff/xcoff.h"
42#include "libxcoff.h"
43#include "coff/rs6000.h"
44
45#include "symtab.h"
46#include "gdbtypes.h"
47/* FIXME: ezannoni/2004-02-13 Verify if the include below is really needed.  */
48#include "symfile.h"
49#include "objfiles.h"
50#include "buildsym.h"
51#include "stabsread.h"
52#include "expression.h"
53#include "complaints.h"
54
55#include "gdb-stabs.h"
56
57/* For interface with stabsread.c.  */
58#include "aout/stab_gnu.h"
59
60
61/* We put a pointer to this structure in the read_symtab_private field
62   of the psymtab.  */
63
64struct symloc
65  {
66
67    /* First symbol number for this file.  */
68
69    int first_symnum;
70
71    /* Number of symbols in the section of the symbol table devoted to
72       this file's symbols (actually, the section bracketed may contain
73       more than just this file's symbols).  If numsyms is 0, the only
74       reason for this thing's existence is the dependency list.  Nothing
75       else will happen when it is read in.  */
76
77    int numsyms;
78
79    /* Position of the start of the line number information for this psymtab.  */
80    unsigned int lineno_off;
81  };
82
83/* Remember what we deduced to be the source language of this psymtab. */
84
85static enum language psymtab_language = language_unknown;
86
87
88/* Simplified internal version of coff symbol table information */
89
90struct coff_symbol
91  {
92    char *c_name;
93    int c_symnum;		/* symbol number of this entry */
94    int c_naux;			/* 0 if syment only, 1 if syment + auxent */
95    long c_value;
96    unsigned char c_sclass;
97    int c_secnum;
98    unsigned int c_type;
99  };
100
101/* last function's saved coff symbol `cs' */
102
103static struct coff_symbol fcn_cs_saved;
104
105static bfd *symfile_bfd;
106
107/* Core address of start and end of text of current source file.
108   This is calculated from the first function seen after a C_FILE
109   symbol. */
110
111
112static CORE_ADDR cur_src_end_addr;
113
114/* Core address of the end of the first object file.  */
115
116static CORE_ADDR first_object_file_end;
117
118/* initial symbol-table-debug-string vector length */
119
120#define	INITIAL_STABVECTOR_LENGTH	40
121
122/* Nonzero if within a function (so symbols should be local,
123   if nothing says specifically).  */
124
125int within_function;
126
127/* Size of a COFF symbol.  I think it is always 18, so I'm not sure
128   there is any reason not to just use a #define, but might as well
129   ask BFD for the size and store it here, I guess.  */
130
131static unsigned local_symesz;
132
133struct coff_symfile_info
134  {
135    file_ptr min_lineno_offset;	/* Where in file lowest line#s are */
136    file_ptr max_lineno_offset;	/* 1+last byte of line#s in file */
137
138    /* Pointer to the string table.  */
139    char *strtbl;
140
141    /* Pointer to debug section.  */
142    char *debugsec;
143
144    /* Pointer to the a.out symbol table.  */
145    char *symtbl;
146
147    /* Number of symbols in symtbl.  */
148    int symtbl_num_syms;
149
150    /* Offset in data section to TOC anchor.  */
151    CORE_ADDR toc_offset;
152  };
153
154static void
155bf_notfound_complaint (void)
156{
157  complaint (&symfile_complaints, "line numbers off, `.bf' symbol not found");
158}
159
160static void
161ef_complaint (int arg1)
162{
163  complaint (&symfile_complaints,
164	     "Mismatched .ef symbol ignored starting at symnum %d", arg1);
165}
166
167static void
168eb_complaint (int arg1)
169{
170  complaint (&symfile_complaints,
171	     "Mismatched .eb symbol ignored starting at symnum %d", arg1);
172}
173
174static void xcoff_initial_scan (struct objfile *, int);
175
176static void scan_xcoff_symtab (struct objfile *);
177
178static char *xcoff_next_symbol_text (struct objfile *);
179
180static void record_include_begin (struct coff_symbol *);
181
182static void
183enter_line_range (struct subfile *, unsigned, unsigned,
184		  CORE_ADDR, CORE_ADDR, unsigned *);
185
186static void init_stringtab (bfd *, file_ptr, struct objfile *);
187
188static void xcoff_symfile_init (struct objfile *);
189
190static void xcoff_new_init (struct objfile *);
191
192static void xcoff_symfile_finish (struct objfile *);
193
194static void xcoff_symfile_offsets (struct objfile *,
195				   struct section_addr_info *addrs);
196
197static char *coff_getfilename (union internal_auxent *, struct objfile *);
198
199static void read_symbol (struct internal_syment *, int);
200
201static int read_symbol_lineno (int);
202
203static CORE_ADDR read_symbol_nvalue (int);
204
205static struct symbol *process_xcoff_symbol (struct coff_symbol *,
206					    struct objfile *);
207
208static void read_xcoff_symtab (struct partial_symtab *);
209
210#if 0
211static void add_stab_to_list (char *, struct pending_stabs **);
212#endif
213
214static int compare_lte (const void *, const void *);
215
216static struct linetable *arrange_linetable (struct linetable *);
217
218static void record_include_end (struct coff_symbol *);
219
220static void process_linenos (CORE_ADDR, CORE_ADDR);
221
222
223/* Translate from a COFF section number (target_index) to a SECT_OFF_*
224   code.  */
225static int secnum_to_section (int, struct objfile *);
226static asection *secnum_to_bfd_section (int, struct objfile *);
227
228struct find_targ_sec_arg
229  {
230    int targ_index;
231    int *resultp;
232    asection **bfd_sect;
233    struct objfile *objfile;
234  };
235
236static void find_targ_sec (bfd *, asection *, void *);
237
238static void
239find_targ_sec (bfd *abfd, asection *sect, void *obj)
240{
241  struct find_targ_sec_arg *args = (struct find_targ_sec_arg *) obj;
242  struct objfile *objfile = args->objfile;
243  if (sect->target_index == args->targ_index)
244    {
245      /* This is the section.  Figure out what SECT_OFF_* code it is.  */
246      if (bfd_get_section_flags (abfd, sect) & SEC_CODE)
247	*args->resultp = SECT_OFF_TEXT (objfile);
248      else if (bfd_get_section_flags (abfd, sect) & SEC_LOAD)
249	*args->resultp = SECT_OFF_DATA (objfile);
250      else
251	*args->resultp = sect->index;
252      *args->bfd_sect = sect;
253    }
254}
255
256/* Return the section number (SECT_OFF_*) that CS points to.  */
257static int
258secnum_to_section (int secnum, struct objfile *objfile)
259{
260  int off = SECT_OFF_TEXT (objfile);
261  asection *sect = NULL;
262  struct find_targ_sec_arg args;
263  args.targ_index = secnum;
264  args.resultp = &off;
265  args.bfd_sect = &sect;
266  args.objfile = objfile;
267  bfd_map_over_sections (objfile->obfd, find_targ_sec, &args);
268  return off;
269}
270
271/* Return the BFD section that CS points to.  */
272static asection *
273secnum_to_bfd_section (int secnum, struct objfile *objfile)
274{
275  int off = SECT_OFF_TEXT (objfile);
276  asection *sect = NULL;
277  struct find_targ_sec_arg args;
278  args.targ_index = secnum;
279  args.resultp = &off;
280  args.bfd_sect = &sect;
281  args.objfile = objfile;
282  bfd_map_over_sections (objfile->obfd, find_targ_sec, &args);
283  return sect;
284}
285
286/* add a given stab string into given stab vector. */
287
288#if 0
289
290static void
291add_stab_to_list (char *stabname, struct pending_stabs **stabvector)
292{
293  if (*stabvector == NULL)
294    {
295      *stabvector = (struct pending_stabs *)
296	xmalloc (sizeof (struct pending_stabs) +
297		 INITIAL_STABVECTOR_LENGTH * sizeof (char *));
298      (*stabvector)->count = 0;
299      (*stabvector)->length = INITIAL_STABVECTOR_LENGTH;
300    }
301  else if ((*stabvector)->count >= (*stabvector)->length)
302    {
303      (*stabvector)->length += INITIAL_STABVECTOR_LENGTH;
304      *stabvector = (struct pending_stabs *)
305	xrealloc ((char *) *stabvector, sizeof (struct pending_stabs) +
306		    (*stabvector)->length * sizeof (char *));
307    }
308  (*stabvector)->stab[(*stabvector)->count++] = stabname;
309}
310
311#endif
312/* *INDENT-OFF* */
313/* Linenos are processed on a file-by-file basis.
314
315   Two reasons:
316
317   1) xlc (IBM's native c compiler) postpones static function code
318   emission to the end of a compilation unit. This way it can
319   determine if those functions (statics) are needed or not, and
320   can do some garbage collection (I think). This makes line
321   numbers and corresponding addresses unordered, and we end up
322   with a line table like:
323
324
325   lineno       addr
326   foo()          10    0x100
327   20   0x200
328   30   0x300
329
330   foo3()         70    0x400
331   80   0x500
332   90   0x600
333
334   static foo2()
335   40   0x700
336   50   0x800
337   60   0x900
338
339   and that breaks gdb's binary search on line numbers, if the
340   above table is not sorted on line numbers. And that sort
341   should be on function based, since gcc can emit line numbers
342   like:
343
344   10   0x100   - for the init/test part of a for stmt.
345   20   0x200
346   30   0x300
347   10   0x400   - for the increment part of a for stmt.
348
349   arrange_linetable() will do this sorting.
350
351   2)   aix symbol table might look like:
352
353   c_file               // beginning of a new file
354   .bi          // beginning of include file
355   .ei          // end of include file
356   .bi
357   .ei
358
359   basically, .bi/.ei pairs do not necessarily encapsulate
360   their scope. They need to be recorded, and processed later
361   on when we come the end of the compilation unit.
362   Include table (inclTable) and process_linenos() handle
363   that.  */
364/* *INDENT-ON* */
365
366
367
368/* compare line table entry addresses. */
369
370static int
371compare_lte (const void *lte1p, const void *lte2p)
372{
373  struct linetable_entry *lte1 = (struct linetable_entry *) lte1p;
374  struct linetable_entry *lte2 = (struct linetable_entry *) lte2p;
375  return lte1->pc - lte2->pc;
376}
377
378/* Given a line table with function entries are marked, arrange its functions
379   in ascending order and strip off function entry markers and return it in
380   a newly created table. If the old one is good enough, return the old one. */
381/* FIXME: I think all this stuff can be replaced by just passing
382   sort_linevec = 1 to end_symtab.  */
383
384static struct linetable *
385arrange_linetable (struct linetable *oldLineTb)
386{
387  int ii, jj, newline,		/* new line count */
388    function_count;		/* # of functions */
389
390  struct linetable_entry *fentry;	/* function entry vector */
391  int fentry_size;		/* # of function entries */
392  struct linetable *newLineTb;	/* new line table */
393
394#define NUM_OF_FUNCTIONS 20
395
396  fentry_size = NUM_OF_FUNCTIONS;
397  fentry = (struct linetable_entry *)
398    xmalloc (fentry_size * sizeof (struct linetable_entry));
399
400  for (function_count = 0, ii = 0; ii < oldLineTb->nitems; ++ii)
401    {
402
403      if (oldLineTb->item[ii].line == 0)
404	{			/* function entry found. */
405
406	  if (function_count >= fentry_size)
407	    {			/* make sure you have room. */
408	      fentry_size *= 2;
409	      fentry = (struct linetable_entry *)
410		xrealloc (fentry, fentry_size * sizeof (struct linetable_entry));
411	    }
412	  fentry[function_count].line = ii;
413	  fentry[function_count].pc = oldLineTb->item[ii].pc;
414	  ++function_count;
415	}
416    }
417
418  if (function_count == 0)
419    {
420      xfree (fentry);
421      return oldLineTb;
422    }
423  else if (function_count > 1)
424    qsort (fentry, function_count, sizeof (struct linetable_entry), compare_lte);
425
426  /* allocate a new line table. */
427  newLineTb = (struct linetable *)
428    xmalloc
429    (sizeof (struct linetable) +
430    (oldLineTb->nitems - function_count) * sizeof (struct linetable_entry));
431
432  /* if line table does not start with a function beginning, copy up until
433     a function begin. */
434
435  newline = 0;
436  if (oldLineTb->item[0].line != 0)
437    for (newline = 0;
438    newline < oldLineTb->nitems && oldLineTb->item[newline].line; ++newline)
439      newLineTb->item[newline] = oldLineTb->item[newline];
440
441  /* Now copy function lines one by one. */
442
443  for (ii = 0; ii < function_count; ++ii)
444    {
445      for (jj = fentry[ii].line + 1;
446	   jj < oldLineTb->nitems && oldLineTb->item[jj].line != 0;
447	   ++jj, ++newline)
448	newLineTb->item[newline] = oldLineTb->item[jj];
449    }
450  xfree (fentry);
451  newLineTb->nitems = oldLineTb->nitems - function_count;
452  return newLineTb;
453}
454
455/* include file support: C_BINCL/C_EINCL pairs will be kept in the
456   following `IncludeChain'. At the end of each symtab (end_symtab),
457   we will determine if we should create additional symtab's to
458   represent if (the include files. */
459
460
461typedef struct _inclTable
462{
463  char *name;			/* include filename */
464
465  /* Offsets to the line table.  end points to the last entry which is
466     part of this include file.  */
467  int begin, end;
468
469  struct subfile *subfile;
470  unsigned funStartLine;	/* start line # of its function */
471}
472InclTable;
473
474#define	INITIAL_INCLUDE_TABLE_LENGTH	20
475static InclTable *inclTable;	/* global include table */
476static int inclIndx;		/* last entry to table */
477static int inclLength;		/* table length */
478static int inclDepth;		/* nested include depth */
479
480static void allocate_include_entry (void);
481
482static void
483record_include_begin (struct coff_symbol *cs)
484{
485  if (inclDepth)
486    {
487      /* In xcoff, we assume include files cannot be nested (not in .c files
488         of course, but in corresponding .s files.).  */
489
490      /* This can happen with old versions of GCC.
491         GCC 2.3.3-930426 does not exhibit this on a test case which
492         a user said produced the message for him.  */
493      complaint (&symfile_complaints, "Nested C_BINCL symbols");
494    }
495  ++inclDepth;
496
497  allocate_include_entry ();
498
499  inclTable[inclIndx].name = cs->c_name;
500  inclTable[inclIndx].begin = cs->c_value;
501}
502
503static void
504record_include_end (struct coff_symbol *cs)
505{
506  InclTable *pTbl;
507
508  if (inclDepth == 0)
509    {
510      complaint (&symfile_complaints, "Mismatched C_BINCL/C_EINCL pair");
511    }
512
513  allocate_include_entry ();
514
515  pTbl = &inclTable[inclIndx];
516  pTbl->end = cs->c_value;
517
518  --inclDepth;
519  ++inclIndx;
520}
521
522static void
523allocate_include_entry (void)
524{
525  if (inclTable == NULL)
526    {
527      inclTable = (InclTable *)
528	xmalloc (sizeof (InclTable) * INITIAL_INCLUDE_TABLE_LENGTH);
529      memset (inclTable,
530	      '\0', sizeof (InclTable) * INITIAL_INCLUDE_TABLE_LENGTH);
531      inclLength = INITIAL_INCLUDE_TABLE_LENGTH;
532      inclIndx = 0;
533    }
534  else if (inclIndx >= inclLength)
535    {
536      inclLength += INITIAL_INCLUDE_TABLE_LENGTH;
537      inclTable = (InclTable *)
538	xrealloc (inclTable, sizeof (InclTable) * inclLength);
539      memset (inclTable + inclLength - INITIAL_INCLUDE_TABLE_LENGTH,
540	      '\0', sizeof (InclTable) * INITIAL_INCLUDE_TABLE_LENGTH);
541    }
542}
543
544/* Global variable to pass the psymtab down to all the routines involved
545   in psymtab to symtab processing.  */
546static struct partial_symtab *this_symtab_psymtab;
547
548/* given the start and end addresses of a compilation unit (or a csect,
549   at times) process its lines and create appropriate line vectors. */
550
551static void
552process_linenos (CORE_ADDR start, CORE_ADDR end)
553{
554  int offset, ii;
555  file_ptr max_offset =
556  ((struct coff_symfile_info *) this_symtab_psymtab->objfile->sym_private)
557  ->max_lineno_offset;
558
559  /* subfile structure for the main compilation unit.  */
560  struct subfile main_subfile;
561
562  /* In the main source file, any time we see a function entry, we
563     reset this variable to function's absolute starting line number.
564     All the following line numbers in the function are relative to
565     this, and we record absolute line numbers in record_line().  */
566
567  unsigned int main_source_baseline = 0;
568
569  unsigned *firstLine;
570
571  offset =
572    ((struct symloc *) this_symtab_psymtab->read_symtab_private)->lineno_off;
573  if (offset == 0)
574    goto return_after_cleanup;
575
576  memset (&main_subfile, '\0', sizeof (main_subfile));
577
578  if (inclIndx == 0)
579    /* All source lines were in the main source file. None in include files. */
580
581    enter_line_range (&main_subfile, offset, 0, start, end,
582		      &main_source_baseline);
583
584  else
585    {
586      /* There was source with line numbers in include files.  */
587
588      int linesz =
589	coff_data (this_symtab_psymtab->objfile->obfd)->local_linesz;
590      main_source_baseline = 0;
591
592      for (ii = 0; ii < inclIndx; ++ii)
593	{
594	  struct subfile *tmpSubfile;
595
596	  /* If there is main file source before include file, enter it.  */
597	  if (offset < inclTable[ii].begin)
598	    {
599	      enter_line_range
600		(&main_subfile, offset, inclTable[ii].begin - linesz,
601		 start, 0, &main_source_baseline);
602	    }
603
604	  /* Have a new subfile for the include file.  */
605
606	  tmpSubfile = inclTable[ii].subfile =
607	    (struct subfile *) xmalloc (sizeof (struct subfile));
608
609	  memset (tmpSubfile, '\0', sizeof (struct subfile));
610	  firstLine = &(inclTable[ii].funStartLine);
611
612	  /* Enter include file's lines now.  */
613	  enter_line_range (tmpSubfile, inclTable[ii].begin,
614			    inclTable[ii].end, start, 0, firstLine);
615
616	  if (offset <= inclTable[ii].end)
617	    offset = inclTable[ii].end + linesz;
618	}
619
620      /* All the include files' line have been processed at this point.  Now,
621         enter remaining lines of the main file, if any left.  */
622      if (offset < max_offset + 1 - linesz)
623	{
624	  enter_line_range (&main_subfile, offset, 0, start, end,
625			    &main_source_baseline);
626	}
627    }
628
629  /* Process main file's line numbers.  */
630  if (main_subfile.line_vector)
631    {
632      struct linetable *lineTb, *lv;
633
634      lv = main_subfile.line_vector;
635
636      /* Line numbers are not necessarily ordered. xlc compilation will
637         put static function to the end. */
638
639      lineTb = arrange_linetable (lv);
640      if (lv == lineTb)
641	{
642	  current_subfile->line_vector = (struct linetable *)
643	    xrealloc (lv, (sizeof (struct linetable)
644			   + lv->nitems * sizeof (struct linetable_entry)));
645	}
646      else
647	{
648	  xfree (lv);
649	  current_subfile->line_vector = lineTb;
650	}
651
652      current_subfile->line_vector_length =
653	current_subfile->line_vector->nitems;
654    }
655
656  /* Now, process included files' line numbers.  */
657
658  for (ii = 0; ii < inclIndx; ++ii)
659    {
660      if ((inclTable[ii].subfile)->line_vector)		/* Useless if!!! FIXMEmgo */
661	{
662	  struct linetable *lineTb, *lv;
663
664	  lv = (inclTable[ii].subfile)->line_vector;
665
666	  /* Line numbers are not necessarily ordered. xlc compilation will
667	     put static function to the end. */
668
669	  lineTb = arrange_linetable (lv);
670
671	  push_subfile ();
672
673	  /* For the same include file, we might want to have more than one
674	     subfile.  This happens if we have something like:
675
676	     ......
677	     #include "foo.h"
678	     ......
679	     #include "foo.h"
680	     ......
681
682	     while foo.h including code in it. (stupid but possible)
683	     Since start_subfile() looks at the name and uses an
684	     existing one if finds, we need to provide a fake name and
685	     fool it.  */
686
687#if 0
688	  start_subfile (inclTable[ii].name, (char *) 0);
689#else
690	  {
691	    /* Pick a fake name that will produce the same results as this
692	       one when passed to deduce_language_from_filename.  Kludge on
693	       top of kludge.  */
694	    char *fakename = strrchr (inclTable[ii].name, '.');
695	    if (fakename == NULL)
696	      fakename = " ?";
697	    start_subfile (fakename, (char *) 0);
698	    xfree (current_subfile->name);
699	  }
700	  current_subfile->name = xstrdup (inclTable[ii].name);
701#endif
702
703	  if (lv == lineTb)
704	    {
705	      current_subfile->line_vector =
706		(struct linetable *) xrealloc
707		(lv, (sizeof (struct linetable)
708		      + lv->nitems * sizeof (struct linetable_entry)));
709
710	    }
711	  else
712	    {
713	      xfree (lv);
714	      current_subfile->line_vector = lineTb;
715	    }
716
717	  current_subfile->line_vector_length =
718	    current_subfile->line_vector->nitems;
719	  start_subfile (pop_subfile (), (char *) 0);
720	}
721    }
722
723return_after_cleanup:
724
725  /* We don't want to keep alloc/free'ing the global include file table.  */
726  inclIndx = 0;
727
728  /* Start with a fresh subfile structure for the next file.  */
729  memset (&main_subfile, '\0', sizeof (struct subfile));
730}
731
732void
733aix_process_linenos (void)
734{
735  /* process line numbers and enter them into line vector */
736  process_linenos (last_source_start_addr, cur_src_end_addr);
737}
738
739
740/* Enter a given range of lines into the line vector.
741   can be called in the following two ways:
742   enter_line_range (subfile, beginoffset, endoffset, startaddr, 0, firstLine)  or
743   enter_line_range (subfile, beginoffset, 0, startaddr, endaddr, firstLine)
744
745   endoffset points to the last line table entry that we should pay
746   attention to.  */
747
748static void
749enter_line_range (struct subfile *subfile, unsigned beginoffset, unsigned endoffset,	/* offsets to line table */
750		  CORE_ADDR startaddr,	/* offsets to line table */
751		  CORE_ADDR endaddr, unsigned *firstLine)
752{
753  unsigned int curoffset;
754  CORE_ADDR addr;
755  void *ext_lnno;
756  struct internal_lineno int_lnno;
757  unsigned int limit_offset;
758  bfd *abfd;
759  int linesz;
760
761  if (endoffset == 0 && startaddr == 0 && endaddr == 0)
762    return;
763  curoffset = beginoffset;
764  limit_offset =
765    ((struct coff_symfile_info *) this_symtab_psymtab->objfile->sym_private)
766    ->max_lineno_offset;
767
768  if (endoffset != 0)
769    {
770      if (endoffset >= limit_offset)
771	{
772	  complaint (&symfile_complaints,
773		     "Bad line table offset in C_EINCL directive");
774	  return;
775	}
776      limit_offset = endoffset;
777    }
778  else
779    limit_offset -= 1;
780
781  abfd = this_symtab_psymtab->objfile->obfd;
782  linesz = coff_data (abfd)->local_linesz;
783  ext_lnno = alloca (linesz);
784
785  while (curoffset <= limit_offset)
786    {
787      bfd_seek (abfd, curoffset, SEEK_SET);
788      bfd_bread (ext_lnno, linesz, abfd);
789      bfd_coff_swap_lineno_in (abfd, ext_lnno, &int_lnno);
790
791      /* Find the address this line represents.  */
792      addr = (int_lnno.l_lnno
793	      ? int_lnno.l_addr.l_paddr
794	      : read_symbol_nvalue (int_lnno.l_addr.l_symndx));
795      addr += ANOFFSET (this_symtab_psymtab->objfile->section_offsets,
796			SECT_OFF_TEXT (this_symtab_psymtab->objfile));
797
798      if (addr < startaddr || (endaddr && addr >= endaddr))
799	return;
800
801      if (int_lnno.l_lnno == 0)
802	{
803	  *firstLine = read_symbol_lineno (int_lnno.l_addr.l_symndx);
804	  record_line (subfile, 0, addr);
805	  --(*firstLine);
806	}
807      else
808	record_line (subfile, *firstLine + int_lnno.l_lnno, addr);
809      curoffset += linesz;
810    }
811}
812
813
814/* Save the vital information for use when closing off the current file.
815   NAME is the file name the symbols came from, START_ADDR is the first
816   text address for the file, and SIZE is the number of bytes of text.  */
817
818#define complete_symtab(name, start_addr) {	\
819  last_source_file = savestring (name, strlen (name));	\
820  last_source_start_addr = start_addr;			\
821}
822
823
824/* Refill the symbol table input buffer
825   and set the variables that control fetching entries from it.
826   Reports an error if no data available.
827   This function can read past the end of the symbol table
828   (into the string table) but this does no harm.  */
829
830/* Reading symbol table has to be fast! Keep the followings as macros, rather
831   than functions. */
832
833#define	RECORD_MINIMAL_SYMBOL(NAME, ADDR, TYPE, SECTION, OBJFILE) \
834{						\
835  char *namestr;				\
836  namestr = (NAME); \
837  if (namestr[0] == '.') ++namestr; \
838  prim_record_minimal_symbol_and_info (namestr, (ADDR), (TYPE), \
839				       (char *)NULL, (SECTION), (asection *)NULL, (OBJFILE)); \
840  misc_func_recorded = 1;					\
841}
842
843
844/* xcoff has static blocks marked in `.bs', `.es' pairs. They cannot be
845   nested. At any given time, a symbol can only be in one static block.
846   This is the base address of current static block, zero if non exists. */
847
848static int static_block_base = 0;
849
850/* Section number for the current static block.  */
851
852static int static_block_section = -1;
853
854/* true if space for symbol name has been allocated. */
855
856static int symname_alloced = 0;
857
858/* Next symbol to read.  Pointer into raw seething symbol table.  */
859
860static char *raw_symbol;
861
862/* This is the function which stabsread.c calls to get symbol
863   continuations.  */
864
865static char *
866xcoff_next_symbol_text (struct objfile *objfile)
867{
868  struct internal_syment symbol;
869  char *retval;
870  /* FIXME: is this the same as the passed arg? */
871  objfile = this_symtab_psymtab->objfile;
872
873  bfd_coff_swap_sym_in (objfile->obfd, raw_symbol, &symbol);
874  if (symbol.n_zeroes)
875    {
876      complaint (&symfile_complaints, "Unexpected symbol continuation");
877
878      /* Return something which points to '\0' and hope the symbol reading
879         code does something reasonable.  */
880      retval = "";
881    }
882  else if (symbol.n_sclass & 0x80)
883    {
884      retval =
885	((struct coff_symfile_info *) objfile->sym_private)->debugsec
886	+ symbol.n_offset;
887      raw_symbol +=
888	coff_data (objfile->obfd)->local_symesz;
889      ++symnum;
890    }
891  else
892    {
893      complaint (&symfile_complaints, "Unexpected symbol continuation");
894
895      /* Return something which points to '\0' and hope the symbol reading
896         code does something reasonable.  */
897      retval = "";
898    }
899  return retval;
900}
901
902/* Read symbols for a given partial symbol table.  */
903
904static void
905read_xcoff_symtab (struct partial_symtab *pst)
906{
907  struct objfile *objfile = pst->objfile;
908  bfd *abfd = objfile->obfd;
909  char *raw_auxptr;		/* Pointer to first raw aux entry for sym */
910  char *strtbl = ((struct coff_symfile_info *) objfile->sym_private)->strtbl;
911  char *debugsec =
912  ((struct coff_symfile_info *) objfile->sym_private)->debugsec;
913  char *debugfmt = bfd_xcoff_is_xcoff64 (abfd) ? "XCOFF64" : "XCOFF";
914
915  struct internal_syment symbol[1];
916  union internal_auxent main_aux;
917  struct coff_symbol cs[1];
918  CORE_ADDR file_start_addr = 0;
919  CORE_ADDR file_end_addr = 0;
920
921  int next_file_symnum = -1;
922  unsigned int max_symnum;
923  int just_started = 1;
924  int depth = 0;
925  int fcn_start_addr = 0;
926
927  struct coff_symbol fcn_stab_saved;
928
929  /* fcn_cs_saved is global because process_xcoff_symbol needs it. */
930  union internal_auxent fcn_aux_saved;
931  struct context_stack *new;
932
933  char *filestring = " _start_ ";	/* Name of the current file. */
934
935  char *last_csect_name;	/* last seen csect's name and value */
936  CORE_ADDR last_csect_val;
937  int last_csect_sec;
938
939  this_symtab_psymtab = pst;
940
941  /* Get the appropriate COFF "constants" related to the file we're
942     handling. */
943  local_symesz = coff_data (abfd)->local_symesz;
944
945  last_source_file = NULL;
946  last_csect_name = 0;
947  last_csect_val = 0;
948
949  start_stabs ();
950  start_symtab (filestring, (char *) NULL, file_start_addr);
951  record_debugformat (debugfmt);
952  symnum = ((struct symloc *) pst->read_symtab_private)->first_symnum;
953  max_symnum =
954    symnum + ((struct symloc *) pst->read_symtab_private)->numsyms;
955  first_object_file_end = 0;
956
957  raw_symbol =
958    ((struct coff_symfile_info *) objfile->sym_private)->symtbl
959    + symnum * local_symesz;
960
961  while (symnum < max_symnum)
962    {
963
964      QUIT;			/* make this command interruptable.  */
965
966      /* READ_ONE_SYMBOL (symbol, cs, symname_alloced); */
967      /* read one symbol into `cs' structure. After processing the
968         whole symbol table, only string table will be kept in memory,
969         symbol table and debug section of xcoff will be freed. Thus
970         we can mark symbols with names in string table as
971         `alloced'. */
972      {
973	int ii;
974
975	/* Swap and align the symbol into a reasonable C structure.  */
976	bfd_coff_swap_sym_in (abfd, raw_symbol, symbol);
977
978	cs->c_symnum = symnum;
979	cs->c_naux = symbol->n_numaux;
980	if (symbol->n_zeroes)
981	  {
982	    symname_alloced = 0;
983	    /* We must use the original, unswapped, name here so the name field
984	       pointed to by cs->c_name will persist throughout xcoffread.  If
985	       we use the new field, it gets overwritten for each symbol.  */
986	    cs->c_name = ((struct external_syment *) raw_symbol)->e.e_name;
987	    /* If it's exactly E_SYMNMLEN characters long it isn't
988	       '\0'-terminated.  */
989	    if (cs->c_name[E_SYMNMLEN - 1] != '\0')
990	      {
991		char *p;
992		p = obstack_alloc (&objfile->objfile_obstack, E_SYMNMLEN + 1);
993		strncpy (p, cs->c_name, E_SYMNMLEN);
994		p[E_SYMNMLEN] = '\0';
995		cs->c_name = p;
996		symname_alloced = 1;
997	      }
998	  }
999	else if (symbol->n_sclass & 0x80)
1000	  {
1001	    cs->c_name = debugsec + symbol->n_offset;
1002	    symname_alloced = 0;
1003	  }
1004	else
1005	  {
1006	    /* in string table */
1007	    cs->c_name = strtbl + (int) symbol->n_offset;
1008	    symname_alloced = 1;
1009	  }
1010	cs->c_value = symbol->n_value;
1011	cs->c_sclass = symbol->n_sclass;
1012	cs->c_secnum = symbol->n_scnum;
1013	cs->c_type = (unsigned) symbol->n_type;
1014
1015	raw_symbol += local_symesz;
1016	++symnum;
1017
1018	/* Save addr of first aux entry.  */
1019	raw_auxptr = raw_symbol;
1020
1021	/* Skip all the auxents associated with this symbol.  */
1022	for (ii = symbol->n_numaux; ii; --ii)
1023	  {
1024	    raw_symbol += coff_data (abfd)->local_auxesz;
1025	    ++symnum;
1026	  }
1027      }
1028
1029      /* if symbol name starts with ".$" or "$", ignore it. */
1030      if (cs->c_name[0] == '$'
1031	  || (cs->c_name[1] == '$' && cs->c_name[0] == '.'))
1032	continue;
1033
1034      if (cs->c_symnum == next_file_symnum && cs->c_sclass != C_FILE)
1035	{
1036	  if (last_source_file)
1037	    {
1038	      pst->symtab =
1039		end_symtab (cur_src_end_addr, objfile, SECT_OFF_TEXT (objfile));
1040	      end_stabs ();
1041	    }
1042
1043	  start_stabs ();
1044	  start_symtab ("_globals_", (char *) NULL, (CORE_ADDR) 0);
1045	  record_debugformat (debugfmt);
1046	  cur_src_end_addr = first_object_file_end;
1047	  /* done with all files, everything from here on is globals */
1048	}
1049
1050      if ((cs->c_sclass == C_EXT || cs->c_sclass == C_HIDEXT)
1051	  && cs->c_naux == 1)
1052	{
1053	  /* Dealing with a symbol with a csect entry.  */
1054
1055#define	CSECT(PP) ((PP)->x_csect)
1056#define	CSECT_LEN(PP) (CSECT(PP).x_scnlen.l)
1057#define	CSECT_ALIGN(PP) (SMTYP_ALIGN(CSECT(PP).x_smtyp))
1058#define	CSECT_SMTYP(PP) (SMTYP_SMTYP(CSECT(PP).x_smtyp))
1059#define	CSECT_SCLAS(PP) (CSECT(PP).x_smclas)
1060
1061	  /* Convert the auxent to something we can access.  */
1062	  bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
1063				0, cs->c_naux, &main_aux);
1064
1065	  switch (CSECT_SMTYP (&main_aux))
1066	    {
1067
1068	    case XTY_ER:
1069	      /* Ignore all external references.  */
1070	      continue;
1071
1072	    case XTY_SD:
1073	      /* A section description.  */
1074	      {
1075		switch (CSECT_SCLAS (&main_aux))
1076		  {
1077
1078		  case XMC_PR:
1079		    {
1080
1081		      /* A program csect is seen.  We have to allocate one
1082		         symbol table for each program csect.  Normally gdb
1083		         prefers one symtab for each source file.  In case
1084		         of AIX, one source file might include more than one
1085		         [PR] csect, and they don't have to be adjacent in
1086		         terms of the space they occupy in memory. Thus, one
1087		         single source file might get fragmented in the
1088		         memory and gdb's file start and end address
1089		         approach does not work!  GCC (and I think xlc) seem
1090		         to put all the code in the unnamed program csect.  */
1091
1092		      if (last_csect_name)
1093			{
1094			  complete_symtab (filestring, file_start_addr);
1095			  cur_src_end_addr = file_end_addr;
1096			  end_symtab (file_end_addr, objfile, SECT_OFF_TEXT (objfile));
1097			  end_stabs ();
1098			  start_stabs ();
1099			  /* Give all csects for this source file the same
1100			     name.  */
1101			  start_symtab (filestring, NULL, (CORE_ADDR) 0);
1102			  record_debugformat (debugfmt);
1103			}
1104
1105		      /* If this is the very first csect seen,
1106		         basically `__start'. */
1107		      if (just_started)
1108			{
1109			  first_object_file_end
1110			    = cs->c_value + CSECT_LEN (&main_aux);
1111			  just_started = 0;
1112			}
1113
1114		      file_start_addr =
1115			cs->c_value + ANOFFSET (objfile->section_offsets,
1116						SECT_OFF_TEXT (objfile));
1117		      file_end_addr = file_start_addr + CSECT_LEN (&main_aux);
1118
1119		      if (cs->c_name && (cs->c_name[0] == '.'
1120					 || cs->c_name[0] == '@'))
1121			{
1122			  last_csect_name = cs->c_name;
1123			  last_csect_val = cs->c_value;
1124			  last_csect_sec = secnum_to_section (cs->c_secnum, objfile);
1125			}
1126		    }
1127		    continue;
1128
1129		    /* All other symbols are put into the minimal symbol
1130		       table only.  */
1131
1132		  case XMC_RW:
1133		    continue;
1134
1135		  case XMC_TC0:
1136		    continue;
1137
1138		  case XMC_TC:
1139		    continue;
1140
1141		  default:
1142		    /* Ignore the symbol.  */
1143		    continue;
1144		  }
1145	      }
1146	      break;
1147
1148	    case XTY_LD:
1149
1150	      switch (CSECT_SCLAS (&main_aux))
1151		{
1152		case XMC_PR:
1153		  /* a function entry point. */
1154		function_entry_point:
1155
1156		  fcn_start_addr = cs->c_value;
1157
1158		  /* save the function header info, which will be used
1159		     when `.bf' is seen. */
1160		  fcn_cs_saved = *cs;
1161		  fcn_aux_saved = main_aux;
1162		  continue;
1163
1164		case XMC_GL:
1165		  /* shared library function trampoline code entry point. */
1166		  continue;
1167
1168		case XMC_DS:
1169		  /* The symbols often have the same names as debug symbols for
1170		     functions, and confuse lookup_symbol.  */
1171		  continue;
1172
1173		default:
1174		  /* xlc puts each variable in a separate csect, so we get
1175		     an XTY_SD for each variable.  But gcc puts several
1176		     variables in a csect, so that each variable only gets
1177		     an XTY_LD. This will typically be XMC_RW; I suspect
1178		     XMC_RO and XMC_BS might be possible too.
1179		     These variables are put in the minimal symbol table
1180		     only.  */
1181		  continue;
1182		}
1183	      break;
1184
1185	    case XTY_CM:
1186	      /* Common symbols are put into the minimal symbol table only.  */
1187	      continue;
1188
1189	    default:
1190	      break;
1191	    }
1192	}
1193
1194      /* If explicitly specified as a function, treat is as one.  This check
1195	 evaluates to true for @FIX* bigtoc CSECT symbols, so it must occur
1196	 after the above CSECT check.  */
1197      if (ISFCN (cs->c_type) && cs->c_sclass != C_TPDEF)
1198	{
1199	  bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
1200				0, cs->c_naux, &main_aux);
1201	  goto function_entry_point;
1202	}
1203
1204      switch (cs->c_sclass)
1205	{
1206
1207	case C_FILE:
1208
1209	  /* c_value field contains symnum of next .file entry in table
1210	     or symnum of first global after last .file. */
1211
1212	  next_file_symnum = cs->c_value;
1213
1214	  /* Complete symbol table for last object file containing
1215	     debugging information. */
1216
1217	  /* Whether or not there was a csect in the previous file, we
1218	     have to call `end_stabs' and `start_stabs' to reset
1219	     type_vector, line_vector, etc. structures.  */
1220
1221	  complete_symtab (filestring, file_start_addr);
1222	  cur_src_end_addr = file_end_addr;
1223	  end_symtab (file_end_addr, objfile, SECT_OFF_TEXT (objfile));
1224	  end_stabs ();
1225
1226	  /* XCOFF, according to the AIX 3.2 documentation, puts the filename
1227	     in cs->c_name.  But xlc 1.3.0.2 has decided to do things the
1228	     standard COFF way and put it in the auxent.  We use the auxent if
1229	     the symbol is ".file" and an auxent exists, otherwise use the symbol
1230	     itself.  Simple enough.  */
1231	  if (!strcmp (cs->c_name, ".file") && cs->c_naux > 0)
1232	    {
1233	      bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
1234				    0, cs->c_naux, &main_aux);
1235	      filestring = coff_getfilename (&main_aux, objfile);
1236	    }
1237	  else
1238	    filestring = cs->c_name;
1239
1240	  start_stabs ();
1241	  start_symtab (filestring, (char *) NULL, (CORE_ADDR) 0);
1242	  record_debugformat (debugfmt);
1243	  last_csect_name = 0;
1244
1245	  /* reset file start and end addresses. A compilation unit with no text
1246	     (only data) should have zero file boundaries. */
1247	  file_start_addr = file_end_addr = 0;
1248	  break;
1249
1250	case C_FUN:
1251	  fcn_stab_saved = *cs;
1252	  break;
1253
1254	case C_FCN:
1255	  if (DEPRECATED_STREQ (cs->c_name, ".bf"))
1256	    {
1257	      CORE_ADDR off = ANOFFSET (objfile->section_offsets,
1258					SECT_OFF_TEXT (objfile));
1259	      bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
1260				    0, cs->c_naux, &main_aux);
1261
1262	      within_function = 1;
1263
1264	      new = push_context (0, fcn_start_addr + off);
1265
1266	      new->name = define_symbol
1267		(fcn_cs_saved.c_value + off,
1268		 fcn_stab_saved.c_name, 0, 0, objfile);
1269	      if (new->name != NULL)
1270		SYMBOL_SECTION (new->name) = SECT_OFF_TEXT (objfile);
1271	    }
1272	  else if (DEPRECATED_STREQ (cs->c_name, ".ef"))
1273	    {
1274
1275	      bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
1276				    0, cs->c_naux, &main_aux);
1277
1278	      /* The value of .ef is the address of epilogue code;
1279	         not useful for gdb.  */
1280	      /* { main_aux.x_sym.x_misc.x_lnsz.x_lnno
1281	         contains number of lines to '}' */
1282
1283	      if (context_stack_depth <= 0)
1284		{		/* We attempted to pop an empty context stack */
1285		  ef_complaint (cs->c_symnum);
1286		  within_function = 0;
1287		  break;
1288		}
1289	      new = pop_context ();
1290	      /* Stack must be empty now.  */
1291	      if (context_stack_depth > 0 || new == NULL)
1292		{
1293		  ef_complaint (cs->c_symnum);
1294		  within_function = 0;
1295		  break;
1296		}
1297
1298	      finish_block (new->name, &local_symbols, new->old_blocks,
1299			    new->start_addr,
1300			    (fcn_cs_saved.c_value
1301			     + fcn_aux_saved.x_sym.x_misc.x_fsize
1302			     + ANOFFSET (objfile->section_offsets,
1303					 SECT_OFF_TEXT (objfile))),
1304			    objfile);
1305	      within_function = 0;
1306	    }
1307	  break;
1308
1309	case C_BSTAT:
1310	  /* Begin static block.  */
1311	  {
1312	    struct internal_syment symbol;
1313
1314	    read_symbol (&symbol, cs->c_value);
1315	    static_block_base = symbol.n_value;
1316	    static_block_section =
1317	      secnum_to_section (symbol.n_scnum, objfile);
1318	  }
1319	  break;
1320
1321	case C_ESTAT:
1322	  /* End of static block.  */
1323	  static_block_base = 0;
1324	  static_block_section = -1;
1325	  break;
1326
1327	case C_ARG:
1328	case C_REGPARM:
1329	case C_REG:
1330	case C_TPDEF:
1331	case C_STRTAG:
1332	case C_UNTAG:
1333	case C_ENTAG:
1334	  {
1335	    complaint (&symfile_complaints, "Unrecognized storage class %d.",
1336		       cs->c_sclass);
1337	  }
1338	  break;
1339
1340	case C_LABEL:
1341	case C_NULL:
1342	  /* Ignore these.  */
1343	  break;
1344
1345	case C_HIDEXT:
1346	case C_STAT:
1347	  break;
1348
1349	case C_BINCL:
1350	  /* beginning of include file */
1351	  /* In xlc output, C_BINCL/C_EINCL pair doesn't show up in sorted
1352	     order. Thus, when wee see them, we might not know enough info
1353	     to process them. Thus, we'll be saving them into a table
1354	     (inclTable) and postpone their processing. */
1355
1356	  record_include_begin (cs);
1357	  break;
1358
1359	case C_EINCL:
1360	  /* End of include file.  */
1361	  /* See the comment after case C_BINCL.  */
1362	  record_include_end (cs);
1363	  break;
1364
1365	case C_BLOCK:
1366	  if (DEPRECATED_STREQ (cs->c_name, ".bb"))
1367	    {
1368	      depth++;
1369	      new = push_context (depth,
1370				  (cs->c_value
1371				   + ANOFFSET (objfile->section_offsets,
1372					       SECT_OFF_TEXT (objfile))));
1373	    }
1374	  else if (DEPRECATED_STREQ (cs->c_name, ".eb"))
1375	    {
1376	      if (context_stack_depth <= 0)
1377		{		/* We attempted to pop an empty context stack */
1378		  eb_complaint (cs->c_symnum);
1379		  break;
1380		}
1381	      new = pop_context ();
1382	      if (depth-- != new->depth)
1383		{
1384		  eb_complaint (cs->c_symnum);
1385		  break;
1386		}
1387	      if (local_symbols && context_stack_depth > 0)
1388		{
1389		  /* Make a block for the local symbols within.  */
1390		  finish_block (new->name, &local_symbols, new->old_blocks,
1391				new->start_addr,
1392				(cs->c_value
1393				 + ANOFFSET (objfile->section_offsets,
1394					     SECT_OFF_TEXT (objfile))),
1395				objfile);
1396		}
1397	      local_symbols = new->locals;
1398	    }
1399	  break;
1400
1401	default:
1402	  process_xcoff_symbol (cs, objfile);
1403	  break;
1404	}
1405    }
1406
1407  if (last_source_file)
1408    {
1409      struct symtab *s;
1410
1411      complete_symtab (filestring, file_start_addr);
1412      cur_src_end_addr = file_end_addr;
1413      s = end_symtab (file_end_addr, objfile, SECT_OFF_TEXT (objfile));
1414      /* When reading symbols for the last C_FILE of the objfile, try
1415         to make sure that we set pst->symtab to the symtab for the
1416         file, not to the _globals_ symtab.  I'm not sure whether this
1417         actually works right or when/if it comes up.  */
1418      if (pst->symtab == NULL)
1419	pst->symtab = s;
1420      end_stabs ();
1421    }
1422}
1423
1424#define	SYMBOL_DUP(SYMBOL1, SYMBOL2)	\
1425  (SYMBOL2) = (struct symbol *)		\
1426  	obstack_alloc (&objfile->objfile_obstack, sizeof (struct symbol)); \
1427  *(SYMBOL2) = *(SYMBOL1);
1428
1429
1430#define	SYMNAME_ALLOC(NAME, ALLOCED)	\
1431  (ALLOCED) ? (NAME) : obsavestring ((NAME), strlen (NAME), &objfile->objfile_obstack);
1432
1433
1434static struct type *func_symbol_type;
1435static struct type *var_symbol_type;
1436
1437/* process one xcoff symbol. */
1438
1439static struct symbol *
1440process_xcoff_symbol (struct coff_symbol *cs, struct objfile *objfile)
1441{
1442  struct symbol onesymbol;
1443  struct symbol *sym = &onesymbol;
1444  struct symbol *sym2 = NULL;
1445  char *name, *pp;
1446
1447  int sec;
1448  CORE_ADDR off;
1449
1450  if (cs->c_secnum < 0)
1451    {
1452      /* The value is a register number, offset within a frame, etc.,
1453         and does not get relocated.  */
1454      off = 0;
1455      sec = -1;
1456    }
1457  else
1458    {
1459      sec = secnum_to_section (cs->c_secnum, objfile);
1460      off = ANOFFSET (objfile->section_offsets, sec);
1461    }
1462
1463  name = cs->c_name;
1464  if (name[0] == '.')
1465    ++name;
1466
1467  memset (sym, '\0', sizeof (struct symbol));
1468
1469  /* default assumptions */
1470  SYMBOL_VALUE_ADDRESS (sym) = cs->c_value + off;
1471  SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
1472  SYMBOL_SECTION (sym) = secnum_to_section (cs->c_secnum, objfile);
1473
1474  if (ISFCN (cs->c_type))
1475    {
1476      /* At this point, we don't know the type of the function.  This
1477         will be patched with the type from its stab entry later on in
1478         patch_block_stabs (), unless the file was compiled without -g.  */
1479
1480      DEPRECATED_SYMBOL_NAME (sym) = SYMNAME_ALLOC (name, symname_alloced);
1481      SYMBOL_TYPE (sym) = func_symbol_type;
1482
1483      SYMBOL_CLASS (sym) = LOC_BLOCK;
1484      SYMBOL_DUP (sym, sym2);
1485
1486      if (cs->c_sclass == C_EXT)
1487	add_symbol_to_list (sym2, &global_symbols);
1488      else if (cs->c_sclass == C_HIDEXT || cs->c_sclass == C_STAT)
1489	add_symbol_to_list (sym2, &file_symbols);
1490    }
1491  else
1492    {
1493      /* In case we can't figure out the type, provide default. */
1494      SYMBOL_TYPE (sym) = var_symbol_type;
1495
1496      switch (cs->c_sclass)
1497	{
1498#if 0
1499	  /* The values of functions and global symbols are now resolved
1500	     via the global_sym_chain in stabsread.c.  */
1501	case C_FUN:
1502	  if (fcn_cs_saved.c_sclass == C_EXT)
1503	    add_stab_to_list (name, &global_stabs);
1504	  else
1505	    add_stab_to_list (name, &file_stabs);
1506	  break;
1507
1508	case C_GSYM:
1509	  add_stab_to_list (name, &global_stabs);
1510	  break;
1511#endif
1512
1513	case C_BCOMM:
1514	  common_block_start (cs->c_name, objfile);
1515	  break;
1516
1517	case C_ECOMM:
1518	  common_block_end (objfile);
1519	  break;
1520
1521	default:
1522	  complaint (&symfile_complaints, "Unexpected storage class: %d",
1523		     cs->c_sclass);
1524	  /* FALLTHROUGH */
1525
1526	case C_DECL:
1527	case C_PSYM:
1528	case C_RPSYM:
1529	case C_ECOML:
1530	case C_LSYM:
1531	case C_RSYM:
1532	case C_GSYM:
1533
1534	  {
1535	    sym = define_symbol (cs->c_value + off, cs->c_name, 0, 0, objfile);
1536	    if (sym != NULL)
1537	      {
1538		SYMBOL_SECTION (sym) = sec;
1539	      }
1540	    return sym;
1541	  }
1542
1543	case C_STSYM:
1544
1545	  /* For xlc (not GCC), the 'V' symbol descriptor is used for
1546	     all statics and we need to distinguish file-scope versus
1547	     function-scope using within_function.  We do this by
1548	     changing the string we pass to define_symbol to use 'S'
1549	     where we need to, which is not necessarily super-clean,
1550	     but seems workable enough.  */
1551
1552	  if (*name == ':' || (pp = (char *) strchr (name, ':')) == NULL)
1553	    return NULL;
1554
1555	  ++pp;
1556	  if (*pp == 'V' && !within_function)
1557	    *pp = 'S';
1558	  sym = define_symbol ((cs->c_value
1559				+ ANOFFSET (objfile->section_offsets,
1560					    static_block_section)),
1561			       cs->c_name, 0, 0, objfile);
1562	  if (sym != NULL)
1563	    {
1564	      SYMBOL_VALUE_ADDRESS (sym) += static_block_base;
1565	      SYMBOL_SECTION (sym) = static_block_section;
1566	    }
1567	  return sym;
1568
1569	}
1570    }
1571  return sym2;
1572}
1573
1574/* Extract the file name from the aux entry of a C_FILE symbol.
1575   Result is in static storage and is only good for temporary use.  */
1576
1577static char *
1578coff_getfilename (union internal_auxent *aux_entry, struct objfile *objfile)
1579{
1580  static char buffer[BUFSIZ];
1581
1582  if (aux_entry->x_file.x_n.x_zeroes == 0)
1583    strcpy (buffer,
1584	    ((struct coff_symfile_info *) objfile->sym_private)->strtbl
1585	    + aux_entry->x_file.x_n.x_offset);
1586  else
1587    {
1588      strncpy (buffer, aux_entry->x_file.x_fname, FILNMLEN);
1589      buffer[FILNMLEN] = '\0';
1590    }
1591  return (buffer);
1592}
1593
1594/* Set *SYMBOL to symbol number symno in symtbl.  */
1595static void
1596read_symbol (struct internal_syment *symbol, int symno)
1597{
1598  int nsyms =
1599  ((struct coff_symfile_info *) this_symtab_psymtab->objfile->sym_private)
1600  ->symtbl_num_syms;
1601  char *stbl =
1602  ((struct coff_symfile_info *) this_symtab_psymtab->objfile->sym_private)
1603  ->symtbl;
1604  if (symno < 0 || symno >= nsyms)
1605    {
1606      complaint (&symfile_complaints, "Invalid symbol offset");
1607      symbol->n_value = 0;
1608      symbol->n_scnum = -1;
1609      return;
1610    }
1611  bfd_coff_swap_sym_in (this_symtab_psymtab->objfile->obfd,
1612			stbl + (symno * local_symesz),
1613			symbol);
1614}
1615
1616/* Get value corresponding to symbol number symno in symtbl.  */
1617
1618static CORE_ADDR
1619read_symbol_nvalue (int symno)
1620{
1621  struct internal_syment symbol[1];
1622
1623  read_symbol (symbol, symno);
1624  return symbol->n_value;
1625}
1626
1627
1628/* Find the address of the function corresponding to symno, where
1629   symno is the symbol pointed to by the linetable.  */
1630
1631static int
1632read_symbol_lineno (int symno)
1633{
1634  struct objfile *objfile = this_symtab_psymtab->objfile;
1635  int xcoff64 = bfd_xcoff_is_xcoff64 (objfile->obfd);
1636
1637  struct coff_symfile_info *info =
1638    (struct coff_symfile_info *)objfile->sym_private;
1639  int nsyms = info->symtbl_num_syms;
1640  char *stbl = info->symtbl;
1641  char *strtbl = info->strtbl;
1642
1643  struct internal_syment symbol[1];
1644  union internal_auxent main_aux[1];
1645
1646  if (symno < 0)
1647    {
1648      bf_notfound_complaint ();
1649      return 0;
1650    }
1651
1652  /* Note that just searching for a short distance (e.g. 50 symbols)
1653     is not enough, at least in the following case.
1654
1655     .extern foo
1656     [many .stabx entries]
1657     [a few functions, referring to foo]
1658     .globl foo
1659     .bf
1660
1661     What happens here is that the assembler moves the .stabx entries
1662     to right before the ".bf" for foo, but the symbol for "foo" is before
1663     all the stabx entries.  See PR gdb/2222.  */
1664
1665  /* Maintaining a table of .bf entries might be preferable to this search.
1666     If I understand things correctly it would need to be done only for
1667     the duration of a single psymtab to symtab conversion.  */
1668  while (symno < nsyms)
1669    {
1670      bfd_coff_swap_sym_in (symfile_bfd,
1671			    stbl + (symno * local_symesz), symbol);
1672      if (symbol->n_sclass == C_FCN)
1673	{
1674	  char *name = xcoff64 ? strtbl + symbol->n_offset : symbol->n_name;
1675	  if (DEPRECATED_STREQ (name, ".bf"))
1676	    goto gotit;
1677	}
1678      symno += symbol->n_numaux + 1;
1679    }
1680
1681  bf_notfound_complaint ();
1682  return 0;
1683
1684gotit:
1685  /* take aux entry and return its lineno */
1686  symno++;
1687  bfd_coff_swap_aux_in (objfile->obfd, stbl + symno * local_symesz,
1688			symbol->n_type, symbol->n_sclass,
1689			0, symbol->n_numaux, main_aux);
1690
1691  return main_aux->x_sym.x_misc.x_lnsz.x_lnno;
1692}
1693
1694/* Support for line number handling */
1695
1696/* This function is called for every section; it finds the outer limits
1697 * of the line table (minimum and maximum file offset) so that the
1698 * mainline code can read the whole thing for efficiency.
1699 */
1700static void
1701find_linenos (struct bfd *abfd, struct bfd_section *asect, void *vpinfo)
1702{
1703  struct coff_symfile_info *info;
1704  int size, count;
1705  file_ptr offset, maxoff;
1706
1707  count = asect->lineno_count;
1708
1709  if (!DEPRECATED_STREQ (asect->name, ".text") || count == 0)
1710    return;
1711
1712  size = count * coff_data (abfd)->local_linesz;
1713  info = (struct coff_symfile_info *) vpinfo;
1714  offset = asect->line_filepos;
1715  maxoff = offset + size;
1716
1717  if (offset < info->min_lineno_offset || info->min_lineno_offset == 0)
1718    info->min_lineno_offset = offset;
1719
1720  if (maxoff > info->max_lineno_offset)
1721    info->max_lineno_offset = maxoff;
1722}
1723
1724static void xcoff_psymtab_to_symtab_1 (struct partial_symtab *);
1725
1726static void
1727xcoff_psymtab_to_symtab_1 (struct partial_symtab *pst)
1728{
1729  struct cleanup *old_chain;
1730  int i;
1731
1732  if (!pst)
1733    return;
1734
1735  if (pst->readin)
1736    {
1737      fprintf_unfiltered
1738	(gdb_stderr, "Psymtab for %s already read in.  Shouldn't happen.\n",
1739	 pst->filename);
1740      return;
1741    }
1742
1743  /* Read in all partial symtabs on which this one is dependent */
1744  for (i = 0; i < pst->number_of_dependencies; i++)
1745    if (!pst->dependencies[i]->readin)
1746      {
1747	/* Inform about additional files that need to be read in.  */
1748	if (info_verbose)
1749	  {
1750	    fputs_filtered (" ", gdb_stdout);
1751	    wrap_here ("");
1752	    fputs_filtered ("and ", gdb_stdout);
1753	    wrap_here ("");
1754	    printf_filtered ("%s...", pst->dependencies[i]->filename);
1755	    wrap_here ("");	/* Flush output */
1756	    gdb_flush (gdb_stdout);
1757	  }
1758	xcoff_psymtab_to_symtab_1 (pst->dependencies[i]);
1759      }
1760
1761  if (((struct symloc *) pst->read_symtab_private)->numsyms != 0)
1762    {
1763      /* Init stuff necessary for reading in symbols.  */
1764      stabsread_init ();
1765      buildsym_init ();
1766      old_chain = make_cleanup (really_free_pendings, 0);
1767
1768      read_xcoff_symtab (pst);
1769
1770      do_cleanups (old_chain);
1771    }
1772
1773  pst->readin = 1;
1774}
1775
1776static void xcoff_psymtab_to_symtab (struct partial_symtab *);
1777
1778/* Read in all of the symbols for a given psymtab for real.
1779   Be verbose about it if the user wants that.  */
1780
1781static void
1782xcoff_psymtab_to_symtab (struct partial_symtab *pst)
1783{
1784  bfd *sym_bfd;
1785
1786  if (!pst)
1787    return;
1788
1789  if (pst->readin)
1790    {
1791      fprintf_unfiltered
1792	(gdb_stderr, "Psymtab for %s already read in.  Shouldn't happen.\n",
1793	 pst->filename);
1794      return;
1795    }
1796
1797  if (((struct symloc *) pst->read_symtab_private)->numsyms != 0
1798      || pst->number_of_dependencies)
1799    {
1800      /* Print the message now, before reading the string table,
1801         to avoid disconcerting pauses.  */
1802      if (info_verbose)
1803	{
1804	  printf_filtered ("Reading in symbols for %s...", pst->filename);
1805	  gdb_flush (gdb_stdout);
1806	}
1807
1808      sym_bfd = pst->objfile->obfd;
1809
1810      next_symbol_text_func = xcoff_next_symbol_text;
1811
1812      xcoff_psymtab_to_symtab_1 (pst);
1813
1814      /* Match with global symbols.  This only needs to be done once,
1815         after all of the symtabs and dependencies have been read in.   */
1816      scan_file_globals (pst->objfile);
1817
1818      /* Finish up the debug error message.  */
1819      if (info_verbose)
1820	printf_filtered ("done.\n");
1821    }
1822}
1823
1824static void
1825xcoff_new_init (struct objfile *objfile)
1826{
1827  stabsread_new_init ();
1828  buildsym_new_init ();
1829}
1830
1831/* Do initialization in preparation for reading symbols from OBJFILE.
1832
1833   We will only be called if this is an XCOFF or XCOFF-like file.
1834   BFD handles figuring out the format of the file, and code in symfile.c
1835   uses BFD's determination to vector to us.  */
1836
1837static void
1838xcoff_symfile_init (struct objfile *objfile)
1839{
1840  /* Allocate struct to keep track of the symfile */
1841  objfile->sym_private = xmmalloc (objfile->md,
1842				   sizeof (struct coff_symfile_info));
1843
1844  /* XCOFF objects may be reordered, so set OBJF_REORDERED.  If we
1845     find this causes a significant slowdown in gdb then we could
1846     set it in the debug symbol readers only when necessary.  */
1847  objfile->flags |= OBJF_REORDERED;
1848
1849  init_entry_point_info (objfile);
1850}
1851
1852/* Perform any local cleanups required when we are done with a particular
1853   objfile.  I.E, we are in the process of discarding all symbol information
1854   for an objfile, freeing up all memory held for it, and unlinking the
1855   objfile struct from the global list of known objfiles. */
1856
1857static void
1858xcoff_symfile_finish (struct objfile *objfile)
1859{
1860  if (objfile->sym_private != NULL)
1861    {
1862      xmfree (objfile->md, objfile->sym_private);
1863    }
1864
1865  /* Start with a fresh include table for the next objfile.  */
1866  if (inclTable)
1867    {
1868      xfree (inclTable);
1869      inclTable = NULL;
1870    }
1871  inclIndx = inclLength = inclDepth = 0;
1872}
1873
1874
1875static void
1876init_stringtab (bfd *abfd, file_ptr offset, struct objfile *objfile)
1877{
1878  long length;
1879  int val;
1880  unsigned char lengthbuf[4];
1881  char *strtbl;
1882
1883  ((struct coff_symfile_info *) objfile->sym_private)->strtbl = NULL;
1884
1885  if (bfd_seek (abfd, offset, SEEK_SET) < 0)
1886    error ("cannot seek to string table in %s: %s",
1887	   bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
1888
1889  val = bfd_bread ((char *) lengthbuf, sizeof lengthbuf, abfd);
1890  length = bfd_h_get_32 (abfd, lengthbuf);
1891
1892  /* If no string table is needed, then the file may end immediately
1893     after the symbols.  Just return with `strtbl' set to NULL.  */
1894
1895  if (val != sizeof lengthbuf || length < sizeof lengthbuf)
1896    return;
1897
1898  /* Allocate string table from objfile_obstack. We will need this table
1899     as long as we have its symbol table around. */
1900
1901  strtbl = (char *) obstack_alloc (&objfile->objfile_obstack, length);
1902  ((struct coff_symfile_info *) objfile->sym_private)->strtbl = strtbl;
1903
1904  /* Copy length buffer, the first byte is usually zero and is
1905     used for stabs with a name length of zero.  */
1906  memcpy (strtbl, lengthbuf, sizeof lengthbuf);
1907  if (length == sizeof lengthbuf)
1908    return;
1909
1910  val = bfd_bread (strtbl + sizeof lengthbuf, length - sizeof lengthbuf, abfd);
1911
1912  if (val != length - sizeof lengthbuf)
1913    error ("cannot read string table from %s: %s",
1914	   bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
1915  if (strtbl[length - 1] != '\0')
1916    error ("bad symbol file: string table does not end with null character");
1917
1918  return;
1919}
1920
1921/* If we have not yet seen a function for this psymtab, this is 0.  If we
1922   have seen one, it is the offset in the line numbers of the line numbers
1923   for the psymtab.  */
1924static unsigned int first_fun_line_offset;
1925
1926static struct partial_symtab *xcoff_start_psymtab
1927  (struct objfile *, char *, int,
1928   struct partial_symbol **, struct partial_symbol **);
1929
1930/* Allocate and partially fill a partial symtab.  It will be
1931   completely filled at the end of the symbol list.
1932
1933   SYMFILE_NAME is the name of the symbol-file we are reading from, and ADDR
1934   is the address relative to which its symbols are (incremental) or 0
1935   (normal). */
1936
1937static struct partial_symtab *
1938xcoff_start_psymtab (struct objfile *objfile, char *filename, int first_symnum,
1939		     struct partial_symbol **global_syms,
1940		     struct partial_symbol **static_syms)
1941{
1942  struct partial_symtab *result =
1943  start_psymtab_common (objfile, objfile->section_offsets,
1944			filename,
1945			/* We fill in textlow later.  */
1946			0,
1947			global_syms, static_syms);
1948
1949  result->read_symtab_private = (char *)
1950    obstack_alloc (&objfile->objfile_obstack, sizeof (struct symloc));
1951  ((struct symloc *) result->read_symtab_private)->first_symnum = first_symnum;
1952  result->read_symtab = xcoff_psymtab_to_symtab;
1953
1954  /* Deduce the source language from the filename for this psymtab. */
1955  psymtab_language = deduce_language_from_filename (filename);
1956
1957  return result;
1958}
1959
1960static struct partial_symtab *xcoff_end_psymtab
1961  (struct partial_symtab *, char **, int, int,
1962   struct partial_symtab **, int, int);
1963
1964/* Close off the current usage of PST.
1965   Returns PST, or NULL if the partial symtab was empty and thrown away.
1966
1967   CAPPING_SYMBOL_NUMBER is the end of pst (exclusive).
1968
1969   INCLUDE_LIST, NUM_INCLUDES, DEPENDENCY_LIST, and NUMBER_DEPENDENCIES
1970   are the information for includes and dependencies.  */
1971
1972static struct partial_symtab *
1973xcoff_end_psymtab (struct partial_symtab *pst, char **include_list,
1974		   int num_includes, int capping_symbol_number,
1975		   struct partial_symtab **dependency_list,
1976		   int number_dependencies, int textlow_not_set)
1977{
1978  int i;
1979  struct objfile *objfile = pst->objfile;
1980
1981  if (capping_symbol_number != -1)
1982    ((struct symloc *) pst->read_symtab_private)->numsyms =
1983      capping_symbol_number
1984      - ((struct symloc *) pst->read_symtab_private)->first_symnum;
1985  ((struct symloc *) pst->read_symtab_private)->lineno_off =
1986    first_fun_line_offset;
1987  first_fun_line_offset = 0;
1988  pst->n_global_syms =
1989    objfile->global_psymbols.next - (objfile->global_psymbols.list + pst->globals_offset);
1990  pst->n_static_syms =
1991    objfile->static_psymbols.next - (objfile->static_psymbols.list + pst->statics_offset);
1992
1993  pst->number_of_dependencies = number_dependencies;
1994  if (number_dependencies)
1995    {
1996      pst->dependencies = (struct partial_symtab **)
1997	obstack_alloc (&objfile->objfile_obstack,
1998		    number_dependencies * sizeof (struct partial_symtab *));
1999      memcpy (pst->dependencies, dependency_list,
2000	      number_dependencies * sizeof (struct partial_symtab *));
2001    }
2002  else
2003    pst->dependencies = 0;
2004
2005  for (i = 0; i < num_includes; i++)
2006    {
2007      struct partial_symtab *subpst =
2008      allocate_psymtab (include_list[i], objfile);
2009
2010      subpst->section_offsets = pst->section_offsets;
2011      subpst->read_symtab_private =
2012	(char *) obstack_alloc (&objfile->objfile_obstack,
2013				sizeof (struct symloc));
2014      ((struct symloc *) subpst->read_symtab_private)->first_symnum = 0;
2015      ((struct symloc *) subpst->read_symtab_private)->numsyms = 0;
2016      subpst->textlow = 0;
2017      subpst->texthigh = 0;
2018
2019      /* We could save slight bits of space by only making one of these,
2020         shared by the entire set of include files.  FIXME-someday.  */
2021      subpst->dependencies = (struct partial_symtab **)
2022	obstack_alloc (&objfile->objfile_obstack,
2023		       sizeof (struct partial_symtab *));
2024      subpst->dependencies[0] = pst;
2025      subpst->number_of_dependencies = 1;
2026
2027      subpst->globals_offset =
2028	subpst->n_global_syms =
2029	subpst->statics_offset =
2030	subpst->n_static_syms = 0;
2031
2032      subpst->readin = 0;
2033      subpst->symtab = 0;
2034      subpst->read_symtab = pst->read_symtab;
2035    }
2036
2037  sort_pst_symbols (pst);
2038
2039  /* If there is already a psymtab or symtab for a file of this name,
2040     remove it.  (If there is a symtab, more drastic things also
2041     happen.)  This happens in VxWorks.  */
2042  free_named_symtabs (pst->filename);
2043
2044  if (num_includes == 0
2045      && number_dependencies == 0
2046      && pst->n_global_syms == 0
2047      && pst->n_static_syms == 0)
2048    {
2049      /* Throw away this psymtab, it's empty.  We can't deallocate it, since
2050         it is on the obstack, but we can forget to chain it on the list.  */
2051      /* Empty psymtabs happen as a result of header files which don't have
2052         any symbols in them.  There can be a lot of them.  */
2053
2054      discard_psymtab (pst);
2055
2056      /* Indicate that psymtab was thrown away.  */
2057      pst = (struct partial_symtab *) NULL;
2058    }
2059  return pst;
2060}
2061
2062static void swap_sym (struct internal_syment *,
2063		      union internal_auxent *, char **, char **,
2064		      unsigned int *, struct objfile *);
2065
2066/* Swap raw symbol at *RAW and put the name in *NAME, the symbol in
2067   *SYMBOL, the first auxent in *AUX.  Advance *RAW and *SYMNUMP over
2068   the symbol and its auxents.  */
2069
2070static void
2071swap_sym (struct internal_syment *symbol, union internal_auxent *aux,
2072	  char **name, char **raw, unsigned int *symnump,
2073	  struct objfile *objfile)
2074{
2075  bfd_coff_swap_sym_in (objfile->obfd, *raw, symbol);
2076  if (symbol->n_zeroes)
2077    {
2078      /* If it's exactly E_SYMNMLEN characters long it isn't
2079         '\0'-terminated.  */
2080      if (symbol->n_name[E_SYMNMLEN - 1] != '\0')
2081	{
2082	  /* FIXME: wastes memory for symbols which we don't end up putting
2083	     into the minimal symbols.  */
2084	  char *p;
2085	  p = obstack_alloc (&objfile->objfile_obstack, E_SYMNMLEN + 1);
2086	  strncpy (p, symbol->n_name, E_SYMNMLEN);
2087	  p[E_SYMNMLEN] = '\0';
2088	  *name = p;
2089	}
2090      else
2091	/* Point to the unswapped name as that persists as long as the
2092	   objfile does.  */
2093	*name = ((struct external_syment *) *raw)->e.e_name;
2094    }
2095  else if (symbol->n_sclass & 0x80)
2096    {
2097      *name = ((struct coff_symfile_info *) objfile->sym_private)->debugsec
2098	+ symbol->n_offset;
2099    }
2100  else
2101    {
2102      *name = ((struct coff_symfile_info *) objfile->sym_private)->strtbl
2103	+ symbol->n_offset;
2104    }
2105  ++*symnump;
2106  *raw += coff_data (objfile->obfd)->local_symesz;
2107  if (symbol->n_numaux > 0)
2108    {
2109      bfd_coff_swap_aux_in (objfile->obfd, *raw, symbol->n_type,
2110			    symbol->n_sclass, 0, symbol->n_numaux, aux);
2111
2112      *symnump += symbol->n_numaux;
2113      *raw += coff_data (objfile->obfd)->local_symesz * symbol->n_numaux;
2114    }
2115}
2116
2117static void
2118function_outside_compilation_unit_complaint (const char *arg1)
2119{
2120  complaint (&symfile_complaints,
2121	     "function `%s' appears to be defined outside of all compilation units",
2122	     arg1);
2123}
2124
2125static void
2126scan_xcoff_symtab (struct objfile *objfile)
2127{
2128  CORE_ADDR toc_offset = 0;	/* toc offset value in data section. */
2129  char *filestring = NULL;
2130
2131  char *namestring;
2132  int past_first_source_file = 0;
2133  bfd *abfd;
2134  asection *bfd_sect;
2135  unsigned int nsyms;
2136
2137  /* Current partial symtab */
2138  struct partial_symtab *pst;
2139
2140  /* List of current psymtab's include files */
2141  char **psymtab_include_list;
2142  int includes_allocated;
2143  int includes_used;
2144
2145  /* Index within current psymtab dependency list */
2146  struct partial_symtab **dependency_list;
2147  int dependencies_used, dependencies_allocated;
2148
2149  char *sraw_symbol;
2150  struct internal_syment symbol;
2151  union internal_auxent main_aux[5];
2152  unsigned int ssymnum;
2153
2154  char *last_csect_name = NULL;	/* last seen csect's name and value */
2155  CORE_ADDR last_csect_val = 0;
2156  int last_csect_sec = 0;
2157  int misc_func_recorded = 0;	/* true if any misc. function */
2158  int textlow_not_set = 1;
2159
2160  pst = (struct partial_symtab *) 0;
2161
2162  includes_allocated = 30;
2163  includes_used = 0;
2164  psymtab_include_list = (char **) alloca (includes_allocated *
2165					   sizeof (char *));
2166
2167  dependencies_allocated = 30;
2168  dependencies_used = 0;
2169  dependency_list =
2170    (struct partial_symtab **) alloca (dependencies_allocated *
2171				       sizeof (struct partial_symtab *));
2172
2173  last_source_file = NULL;
2174
2175  abfd = objfile->obfd;
2176
2177  sraw_symbol = ((struct coff_symfile_info *) objfile->sym_private)->symtbl;
2178  nsyms = ((struct coff_symfile_info *) objfile->sym_private)->symtbl_num_syms;
2179  ssymnum = 0;
2180  while (ssymnum < nsyms)
2181    {
2182      int sclass;
2183
2184      QUIT;
2185
2186      bfd_coff_swap_sym_in (abfd, sraw_symbol, &symbol);
2187      sclass = symbol.n_sclass;
2188
2189      switch (sclass)
2190	{
2191	case C_EXT:
2192	case C_HIDEXT:
2193	  {
2194	    /* The CSECT auxent--always the last auxent.  */
2195	    union internal_auxent csect_aux;
2196	    unsigned int symnum_before = ssymnum;
2197
2198	    swap_sym (&symbol, &main_aux[0], &namestring, &sraw_symbol,
2199		      &ssymnum, objfile);
2200	    if (symbol.n_numaux > 1)
2201	      {
2202		bfd_coff_swap_aux_in
2203		  (objfile->obfd,
2204		   sraw_symbol - coff_data (abfd)->local_symesz,
2205		   symbol.n_type,
2206		   symbol.n_sclass,
2207		   symbol.n_numaux - 1,
2208		   symbol.n_numaux,
2209		   &csect_aux);
2210	      }
2211	    else
2212	      csect_aux = main_aux[0];
2213
2214	    /* If symbol name starts with ".$" or "$", ignore it.  */
2215	    if (namestring[0] == '$'
2216		|| (namestring[0] == '.' && namestring[1] == '$'))
2217	      break;
2218
2219	    switch (csect_aux.x_csect.x_smtyp & 0x7)
2220	      {
2221	      case XTY_SD:
2222		switch (csect_aux.x_csect.x_smclas)
2223		  {
2224		  case XMC_PR:
2225		    if (last_csect_name)
2226		      {
2227			/* If no misc. function recorded in the last
2228			   seen csect, enter it as a function. This
2229			   will take care of functions like strcmp()
2230			   compiled by xlc.  */
2231
2232			if (!misc_func_recorded)
2233			  {
2234			    RECORD_MINIMAL_SYMBOL
2235			      (last_csect_name, last_csect_val,
2236			       mst_text, last_csect_sec,
2237			       objfile);
2238			  }
2239
2240			if (pst != NULL)
2241			  {
2242			    /* We have to allocate one psymtab for
2243			       each program csect, because their text
2244			       sections need not be adjacent.  */
2245			    xcoff_end_psymtab
2246			      (pst, psymtab_include_list, includes_used,
2247			       symnum_before, dependency_list,
2248			       dependencies_used, textlow_not_set);
2249			    includes_used = 0;
2250			    dependencies_used = 0;
2251			    /* Give all psymtabs for this source file the same
2252			       name.  */
2253			    pst = xcoff_start_psymtab
2254			      (objfile,
2255			       filestring,
2256			       symnum_before,
2257			       objfile->global_psymbols.next,
2258			       objfile->static_psymbols.next);
2259			  }
2260		      }
2261		    /* Activate the misc_func_recorded mechanism for
2262		       compiler- and linker-generated CSECTs like ".strcmp"
2263		       and "@FIX1".  */
2264		    if (namestring && (namestring[0] == '.'
2265				       || namestring[0] == '@'))
2266		      {
2267			last_csect_name = namestring;
2268			last_csect_val = symbol.n_value;
2269			last_csect_sec =
2270			  secnum_to_section (symbol.n_scnum, objfile);
2271		      }
2272		    if (pst != NULL)
2273		      {
2274			CORE_ADDR highval =
2275			symbol.n_value + csect_aux.x_csect.x_scnlen.l;
2276			if (highval > pst->texthigh)
2277			  pst->texthigh = highval;
2278			if (pst->textlow == 0 || symbol.n_value < pst->textlow)
2279			  pst->textlow = symbol.n_value;
2280		      }
2281		    misc_func_recorded = 0;
2282		    break;
2283
2284		  case XMC_RW:
2285		  case XMC_TD:
2286		    /* Data variables are recorded in the minimal symbol
2287		       table, except for section symbols.  */
2288		    if (*namestring != '.')
2289		      prim_record_minimal_symbol_and_info
2290			(namestring, symbol.n_value,
2291			 sclass == C_HIDEXT ? mst_file_data : mst_data,
2292			 NULL, secnum_to_section (symbol.n_scnum, objfile),
2293			 NULL, objfile);
2294		    break;
2295
2296		  case XMC_TC0:
2297		    if (toc_offset)
2298		      warning ("More than one XMC_TC0 symbol found.");
2299		    toc_offset = symbol.n_value;
2300
2301		    /* Make TOC offset relative to start address of section.  */
2302		    bfd_sect = secnum_to_bfd_section (symbol.n_scnum, objfile);
2303		    if (bfd_sect)
2304		      toc_offset -= bfd_section_vma (objfile->obfd, bfd_sect);
2305		    break;
2306
2307		  case XMC_TC:
2308		    /* These symbols tell us where the TOC entry for a
2309		       variable is, not the variable itself.  */
2310		    break;
2311
2312		  default:
2313		    break;
2314		  }
2315		break;
2316
2317	      case XTY_LD:
2318		switch (csect_aux.x_csect.x_smclas)
2319		  {
2320		  case XMC_PR:
2321		    /* A function entry point.  */
2322
2323		    if (first_fun_line_offset == 0 && symbol.n_numaux > 1)
2324		      first_fun_line_offset =
2325			main_aux[0].x_sym.x_fcnary.x_fcn.x_lnnoptr;
2326		    RECORD_MINIMAL_SYMBOL
2327		      (namestring, symbol.n_value,
2328		       sclass == C_HIDEXT ? mst_file_text : mst_text,
2329		       secnum_to_section (symbol.n_scnum, objfile),
2330		       objfile);
2331		    break;
2332
2333		  case XMC_GL:
2334		    /* shared library function trampoline code entry
2335		       point. */
2336
2337		    /* record trampoline code entries as
2338		       mst_solib_trampoline symbol.  When we lookup mst
2339		       symbols, we will choose mst_text over
2340		       mst_solib_trampoline. */
2341		    RECORD_MINIMAL_SYMBOL
2342		      (namestring, symbol.n_value,
2343		       mst_solib_trampoline,
2344		       secnum_to_section (symbol.n_scnum, objfile),
2345		       objfile);
2346		    break;
2347
2348		  case XMC_DS:
2349		    /* The symbols often have the same names as
2350		       debug symbols for functions, and confuse
2351		       lookup_symbol.  */
2352		    break;
2353
2354		  default:
2355
2356		    /* xlc puts each variable in a separate csect,
2357		       so we get an XTY_SD for each variable.  But
2358		       gcc puts several variables in a csect, so
2359		       that each variable only gets an XTY_LD.  We
2360		       still need to record them.  This will
2361		       typically be XMC_RW; I suspect XMC_RO and
2362		       XMC_BS might be possible too.  */
2363		    if (*namestring != '.')
2364		      prim_record_minimal_symbol_and_info
2365			(namestring, symbol.n_value,
2366			 sclass == C_HIDEXT ? mst_file_data : mst_data,
2367			 NULL, secnum_to_section (symbol.n_scnum, objfile),
2368			 NULL, objfile);
2369		    break;
2370		  }
2371		break;
2372
2373	      case XTY_CM:
2374		switch (csect_aux.x_csect.x_smclas)
2375		  {
2376		  case XMC_RW:
2377		  case XMC_BS:
2378		    /* Common variables are recorded in the minimal symbol
2379		       table, except for section symbols.  */
2380		    if (*namestring != '.')
2381		      prim_record_minimal_symbol_and_info
2382			(namestring, symbol.n_value,
2383			 sclass == C_HIDEXT ? mst_file_bss : mst_bss,
2384			 NULL, secnum_to_section (symbol.n_scnum, objfile),
2385			 NULL, objfile);
2386		    break;
2387		  }
2388		break;
2389
2390	      default:
2391		break;
2392	      }
2393	  }
2394	  break;
2395	case C_FILE:
2396	  {
2397	    unsigned int symnum_before;
2398
2399	    symnum_before = ssymnum;
2400	    swap_sym (&symbol, &main_aux[0], &namestring, &sraw_symbol,
2401		      &ssymnum, objfile);
2402
2403	    /* See if the last csect needs to be recorded.  */
2404
2405	    if (last_csect_name && !misc_func_recorded)
2406	      {
2407
2408		/* If no misc. function recorded in the last seen csect, enter
2409		   it as a function.  This will take care of functions like
2410		   strcmp() compiled by xlc.  */
2411
2412		RECORD_MINIMAL_SYMBOL
2413		  (last_csect_name, last_csect_val,
2414		   mst_text, last_csect_sec, objfile);
2415	      }
2416
2417	    if (pst)
2418	      {
2419		xcoff_end_psymtab (pst, psymtab_include_list, includes_used,
2420				   symnum_before, dependency_list,
2421				   dependencies_used, textlow_not_set);
2422		includes_used = 0;
2423		dependencies_used = 0;
2424	      }
2425	    first_fun_line_offset = 0;
2426
2427	    /* XCOFF, according to the AIX 3.2 documentation, puts the
2428	       filename in cs->c_name.  But xlc 1.3.0.2 has decided to
2429	       do things the standard COFF way and put it in the auxent.
2430	       We use the auxent if the symbol is ".file" and an auxent
2431	       exists, otherwise use the symbol itself.  */
2432	    if (!strcmp (namestring, ".file") && symbol.n_numaux > 0)
2433	      {
2434		filestring = coff_getfilename (&main_aux[0], objfile);
2435	      }
2436	    else
2437	      filestring = namestring;
2438
2439	    pst = xcoff_start_psymtab (objfile,
2440				       filestring,
2441				       symnum_before,
2442				       objfile->global_psymbols.next,
2443				       objfile->static_psymbols.next);
2444	    last_csect_name = NULL;
2445	  }
2446	  break;
2447
2448	default:
2449	  {
2450	    complaint (&symfile_complaints,
2451		       "Storage class %d not recognized during scan", sclass);
2452	  }
2453	  /* FALLTHROUGH */
2454
2455	  /* C_FCN is .bf and .ef symbols.  I think it is sufficient
2456	     to handle only the C_FUN and C_EXT.  */
2457	case C_FCN:
2458
2459	case C_BSTAT:
2460	case C_ESTAT:
2461	case C_ARG:
2462	case C_REGPARM:
2463	case C_REG:
2464	case C_TPDEF:
2465	case C_STRTAG:
2466	case C_UNTAG:
2467	case C_ENTAG:
2468	case C_LABEL:
2469	case C_NULL:
2470
2471	  /* C_EINCL means we are switching back to the main file.  But there
2472	     is no reason to care; the only thing we want to know about
2473	     includes is the names of all the included (.h) files.  */
2474	case C_EINCL:
2475
2476	case C_BLOCK:
2477
2478	  /* I don't think C_STAT is used in xcoff; C_HIDEXT appears to be
2479	     used instead.  */
2480	case C_STAT:
2481
2482	  /* I don't think the name of the common block (as opposed to the
2483	     variables within it) is something which is user visible
2484	     currently.  */
2485	case C_BCOMM:
2486	case C_ECOMM:
2487
2488	case C_PSYM:
2489	case C_RPSYM:
2490
2491	  /* I think we can ignore C_LSYM; types on xcoff seem to use C_DECL
2492	     so C_LSYM would appear to be only for locals.  */
2493	case C_LSYM:
2494
2495	case C_AUTO:
2496	case C_RSYM:
2497	  {
2498	    /* We probably could save a few instructions by assuming that
2499	       C_LSYM, C_PSYM, etc., never have auxents.  */
2500	    int naux1 = symbol.n_numaux + 1;
2501	    ssymnum += naux1;
2502	    sraw_symbol += bfd_coff_symesz (abfd) * naux1;
2503	  }
2504	  break;
2505
2506	case C_BINCL:
2507	  {
2508	    /* Mark down an include file in the current psymtab */
2509	    enum language tmp_language;
2510	    swap_sym (&symbol, &main_aux[0], &namestring, &sraw_symbol,
2511		      &ssymnum, objfile);
2512
2513	    tmp_language = deduce_language_from_filename (namestring);
2514
2515	    /* Only change the psymtab's language if we've learned
2516	       something useful (eg. tmp_language is not language_unknown).
2517	       In addition, to match what start_subfile does, never change
2518	       from C++ to C.  */
2519	    if (tmp_language != language_unknown
2520		&& (tmp_language != language_c
2521		    || psymtab_language != language_cplus))
2522	      psymtab_language = tmp_language;
2523
2524	    /* In C++, one may expect the same filename to come round many
2525	       times, when code is coming alternately from the main file
2526	       and from inline functions in other files. So I check to see
2527	       if this is a file we've seen before -- either the main
2528	       source file, or a previously included file.
2529
2530	       This seems to be a lot of time to be spending on N_SOL, but
2531	       things like "break c-exp.y:435" need to work (I
2532	       suppose the psymtab_include_list could be hashed or put
2533	       in a binary tree, if profiling shows this is a major hog).  */
2534	    if (pst && DEPRECATED_STREQ (namestring, pst->filename))
2535	      continue;
2536	    {
2537	      int i;
2538	      for (i = 0; i < includes_used; i++)
2539		if (DEPRECATED_STREQ (namestring, psymtab_include_list[i]))
2540		  {
2541		    i = -1;
2542		    break;
2543		  }
2544	      if (i == -1)
2545		continue;
2546	    }
2547	    psymtab_include_list[includes_used++] = namestring;
2548	    if (includes_used >= includes_allocated)
2549	      {
2550		char **orig = psymtab_include_list;
2551
2552		psymtab_include_list = (char **)
2553		  alloca ((includes_allocated *= 2) *
2554			  sizeof (char *));
2555		memcpy (psymtab_include_list, orig,
2556			includes_used * sizeof (char *));
2557	      }
2558	    continue;
2559	  }
2560	case C_FUN:
2561	  /* The value of the C_FUN is not the address of the function (it
2562	     appears to be the address before linking), but as long as it
2563	     is smaller than the actual address, then find_pc_partial_function
2564	     will use the minimal symbols instead.  I hope.  */
2565
2566	case C_GSYM:
2567	case C_ECOML:
2568	case C_DECL:
2569	case C_STSYM:
2570	  {
2571	    char *p;
2572	    swap_sym (&symbol, &main_aux[0], &namestring, &sraw_symbol,
2573		      &ssymnum, objfile);
2574
2575	    p = (char *) strchr (namestring, ':');
2576	    if (!p)
2577	      continue;			/* Not a debugging symbol.   */
2578
2579	    /* Main processing section for debugging symbols which
2580	       the initial read through the symbol tables needs to worry
2581	       about.  If we reach this point, the symbol which we are
2582	       considering is definitely one we are interested in.
2583	       p must also contain the (valid) index into the namestring
2584	       which indicates the debugging type symbol.  */
2585
2586	    switch (p[1])
2587	      {
2588	      case 'S':
2589		symbol.n_value += ANOFFSET (objfile->section_offsets, SECT_OFF_DATA (objfile));
2590#ifdef STATIC_TRANSFORM_NAME
2591		namestring = STATIC_TRANSFORM_NAME (namestring);
2592#endif
2593		add_psymbol_to_list (namestring, p - namestring,
2594				     VAR_DOMAIN, LOC_STATIC,
2595				     &objfile->static_psymbols,
2596				     0, symbol.n_value,
2597				     psymtab_language, objfile);
2598		continue;
2599
2600	      case 'G':
2601		symbol.n_value += ANOFFSET (objfile->section_offsets, SECT_OFF_DATA (objfile));
2602		/* The addresses in these entries are reported to be
2603		   wrong.  See the code that reads 'G's for symtabs. */
2604		add_psymbol_to_list (namestring, p - namestring,
2605				     VAR_DOMAIN, LOC_STATIC,
2606				     &objfile->global_psymbols,
2607				     0, symbol.n_value,
2608				     psymtab_language, objfile);
2609		continue;
2610
2611	      case 'T':
2612		/* When a 'T' entry is defining an anonymous enum, it
2613		   may have a name which is the empty string, or a
2614		   single space.  Since they're not really defining a
2615		   symbol, those shouldn't go in the partial symbol
2616		   table.  We do pick up the elements of such enums at
2617		   'check_enum:', below.  */
2618		if (p >= namestring + 2
2619		    || (p == namestring + 1
2620			&& namestring[0] != ' '))
2621		  {
2622		    add_psymbol_to_list (namestring, p - namestring,
2623					 STRUCT_DOMAIN, LOC_TYPEDEF,
2624					 &objfile->static_psymbols,
2625					 symbol.n_value, 0,
2626					 psymtab_language, objfile);
2627		    if (p[2] == 't')
2628		      {
2629			/* Also a typedef with the same name.  */
2630			add_psymbol_to_list (namestring, p - namestring,
2631					     VAR_DOMAIN, LOC_TYPEDEF,
2632					     &objfile->static_psymbols,
2633					     symbol.n_value, 0,
2634					     psymtab_language, objfile);
2635			p += 1;
2636		      }
2637		  }
2638		goto check_enum;
2639
2640	      case 't':
2641		if (p != namestring)	/* a name is there, not just :T... */
2642		  {
2643		    add_psymbol_to_list (namestring, p - namestring,
2644					 VAR_DOMAIN, LOC_TYPEDEF,
2645					 &objfile->static_psymbols,
2646					 symbol.n_value, 0,
2647					 psymtab_language, objfile);
2648		  }
2649	      check_enum:
2650		/* If this is an enumerated type, we need to
2651		   add all the enum constants to the partial symbol
2652		   table.  This does not cover enums without names, e.g.
2653		   "enum {a, b} c;" in C, but fortunately those are
2654		   rare.  There is no way for GDB to find those from the
2655		   enum type without spending too much time on it.  Thus
2656		   to solve this problem, the compiler needs to put out the
2657		   enum in a nameless type.  GCC2 does this.  */
2658
2659		/* We are looking for something of the form
2660		   <name> ":" ("t" | "T") [<number> "="] "e"
2661		   {<constant> ":" <value> ","} ";".  */
2662
2663		/* Skip over the colon and the 't' or 'T'.  */
2664		p += 2;
2665		/* This type may be given a number.  Also, numbers can come
2666		   in pairs like (0,26).  Skip over it.  */
2667		while ((*p >= '0' && *p <= '9')
2668		       || *p == '(' || *p == ',' || *p == ')'
2669		       || *p == '=')
2670		  p++;
2671
2672		if (*p++ == 'e')
2673		  {
2674		    /* The aix4 compiler emits extra crud before the members.  */
2675		    if (*p == '-')
2676		      {
2677			/* Skip over the type (?).  */
2678			while (*p != ':')
2679			  p++;
2680
2681			/* Skip over the colon.  */
2682			p++;
2683		      }
2684
2685		    /* We have found an enumerated type.  */
2686		    /* According to comments in read_enum_type
2687		       a comma could end it instead of a semicolon.
2688		       I don't know where that happens.
2689		       Accept either.  */
2690		    while (*p && *p != ';' && *p != ',')
2691		      {
2692			char *q;
2693
2694			/* Check for and handle cretinous dbx symbol name
2695			   continuation!  */
2696			if (*p == '\\' || (*p == '?' && p[1] == '\0'))
2697			  p = next_symbol_text (objfile);
2698
2699			/* Point to the character after the name
2700			   of the enum constant.  */
2701			for (q = p; *q && *q != ':'; q++)
2702			  ;
2703			/* Note that the value doesn't matter for
2704			   enum constants in psymtabs, just in symtabs.  */
2705			add_psymbol_to_list (p, q - p,
2706					     VAR_DOMAIN, LOC_CONST,
2707					     &objfile->static_psymbols, 0,
2708					     0, psymtab_language, objfile);
2709			/* Point past the name.  */
2710			p = q;
2711			/* Skip over the value.  */
2712			while (*p && *p != ',')
2713			  p++;
2714			/* Advance past the comma.  */
2715			if (*p)
2716			  p++;
2717		      }
2718		  }
2719		continue;
2720
2721	      case 'c':
2722		/* Constant, e.g. from "const" in Pascal.  */
2723		add_psymbol_to_list (namestring, p - namestring,
2724				     VAR_DOMAIN, LOC_CONST,
2725				     &objfile->static_psymbols, symbol.n_value,
2726				     0, psymtab_language, objfile);
2727		continue;
2728
2729	      case 'f':
2730		if (! pst)
2731		  {
2732		    int name_len = p - namestring;
2733		    char *name = xmalloc (name_len + 1);
2734		    memcpy (name, namestring, name_len);
2735		    name[name_len] = '\0';
2736		    function_outside_compilation_unit_complaint (name);
2737		    xfree (name);
2738		  }
2739		symbol.n_value += ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2740		add_psymbol_to_list (namestring, p - namestring,
2741				     VAR_DOMAIN, LOC_BLOCK,
2742				     &objfile->static_psymbols,
2743				     0, symbol.n_value,
2744				     psymtab_language, objfile);
2745		continue;
2746
2747		/* Global functions were ignored here, but now they
2748		   are put into the global psymtab like one would expect.
2749		   They're also in the minimal symbol table.  */
2750	      case 'F':
2751		if (! pst)
2752		  {
2753		    int name_len = p - namestring;
2754		    char *name = xmalloc (name_len + 1);
2755		    memcpy (name, namestring, name_len);
2756		    name[name_len] = '\0';
2757		    function_outside_compilation_unit_complaint (name);
2758		    xfree (name);
2759		  }
2760		symbol.n_value += ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2761		add_psymbol_to_list (namestring, p - namestring,
2762				     VAR_DOMAIN, LOC_BLOCK,
2763				     &objfile->global_psymbols,
2764				     0, symbol.n_value,
2765				     psymtab_language, objfile);
2766		continue;
2767
2768		/* Two things show up here (hopefully); static symbols of
2769		   local scope (static used inside braces) or extensions
2770		   of structure symbols.  We can ignore both.  */
2771	      case 'V':
2772	      case '(':
2773	      case '0':
2774	      case '1':
2775	      case '2':
2776	      case '3':
2777	      case '4':
2778	      case '5':
2779	      case '6':
2780	      case '7':
2781	      case '8':
2782	      case '9':
2783	      case '-':
2784	      case '#':		/* for symbol identification (used in live ranges) */
2785		continue;
2786
2787	      case ':':
2788		/* It is a C++ nested symbol.  We don't need to record it
2789		   (I don't think); if we try to look up foo::bar::baz,
2790		   then symbols for the symtab containing foo should get
2791		   read in, I think.  */
2792		/* Someone says sun cc puts out symbols like
2793		   /foo/baz/maclib::/usr/local/bin/maclib,
2794		   which would get here with a symbol type of ':'.  */
2795		continue;
2796
2797	      default:
2798		/* Unexpected symbol descriptor.  The second and subsequent stabs
2799		   of a continued stab can show up here.  The question is
2800		   whether they ever can mimic a normal stab--it would be
2801		   nice if not, since we certainly don't want to spend the
2802		   time searching to the end of every string looking for
2803		   a backslash.  */
2804
2805		complaint (&symfile_complaints,
2806			   "unknown symbol descriptor `%c'", p[1]);
2807
2808		/* Ignore it; perhaps it is an extension that we don't
2809		   know about.  */
2810		continue;
2811	      }
2812	  }
2813	}
2814    }
2815
2816  if (pst)
2817    {
2818      xcoff_end_psymtab (pst, psymtab_include_list, includes_used,
2819			 ssymnum, dependency_list,
2820			 dependencies_used, textlow_not_set);
2821    }
2822
2823  /* Record the toc offset value of this symbol table into objfile structure.
2824     If no XMC_TC0 is found, toc_offset should be zero. Another place to obtain
2825     this information would be file auxiliary header. */
2826
2827  ((struct coff_symfile_info *) objfile->sym_private)->toc_offset = toc_offset;
2828}
2829
2830/* Return the toc offset value for a given objfile.  */
2831
2832CORE_ADDR
2833get_toc_offset (struct objfile *objfile)
2834{
2835  if (objfile)
2836    return ((struct coff_symfile_info *) objfile->sym_private)->toc_offset;
2837  return 0;
2838}
2839
2840/* Scan and build partial symbols for a symbol file.
2841   We have been initialized by a call to dbx_symfile_init, which
2842   put all the relevant info into a "struct dbx_symfile_info",
2843   hung off the objfile structure.
2844
2845   SECTION_OFFSETS contains offsets relative to which the symbols in the
2846   various sections are (depending where the sections were actually loaded).
2847   MAINLINE is true if we are reading the main symbol
2848   table (as opposed to a shared lib or dynamically loaded file).  */
2849
2850static void
2851xcoff_initial_scan (struct objfile *objfile, int mainline)
2852{
2853  bfd *abfd;
2854  int val;
2855  struct cleanup *back_to;
2856  int num_symbols;		/* # of symbols */
2857  file_ptr symtab_offset;	/* symbol table and */
2858  file_ptr stringtab_offset;	/* string table file offsets */
2859  struct coff_symfile_info *info;
2860  char *name;
2861  unsigned int size;
2862
2863  info = (struct coff_symfile_info *) objfile->sym_private;
2864  symfile_bfd = abfd = objfile->obfd;
2865  name = objfile->name;
2866
2867  num_symbols = bfd_get_symcount (abfd);	/* # of symbols */
2868  symtab_offset = obj_sym_filepos (abfd);	/* symbol table file offset */
2869  stringtab_offset = symtab_offset +
2870    num_symbols * coff_data (abfd)->local_symesz;
2871
2872  info->min_lineno_offset = 0;
2873  info->max_lineno_offset = 0;
2874  bfd_map_over_sections (abfd, find_linenos, info);
2875
2876  if (num_symbols > 0)
2877    {
2878      /* Read the string table.  */
2879      init_stringtab (abfd, stringtab_offset, objfile);
2880
2881      /* Read the .debug section, if present.  */
2882      {
2883	struct bfd_section *secp;
2884	bfd_size_type length;
2885	char *debugsec = NULL;
2886
2887	secp = bfd_get_section_by_name (abfd, ".debug");
2888	if (secp)
2889	  {
2890	    length = bfd_section_size (abfd, secp);
2891	    if (length)
2892	      {
2893		debugsec =
2894		  (char *) obstack_alloc (&objfile->objfile_obstack, length);
2895
2896		if (!bfd_get_section_contents (abfd, secp, debugsec,
2897					       (file_ptr) 0, length))
2898		  {
2899		    error ("Error reading .debug section of `%s': %s",
2900			   name, bfd_errmsg (bfd_get_error ()));
2901		  }
2902	      }
2903	  }
2904	((struct coff_symfile_info *) objfile->sym_private)->debugsec =
2905	  debugsec;
2906      }
2907    }
2908
2909  /* Read the symbols.  We keep them in core because we will want to
2910     access them randomly in read_symbol*.  */
2911  val = bfd_seek (abfd, symtab_offset, SEEK_SET);
2912  if (val < 0)
2913    error ("Error reading symbols from %s: %s",
2914	   name, bfd_errmsg (bfd_get_error ()));
2915  size = coff_data (abfd)->local_symesz * num_symbols;
2916  ((struct coff_symfile_info *) objfile->sym_private)->symtbl =
2917    obstack_alloc (&objfile->objfile_obstack, size);
2918  ((struct coff_symfile_info *) objfile->sym_private)->symtbl_num_syms =
2919    num_symbols;
2920
2921  val = bfd_bread (((struct coff_symfile_info *) objfile->sym_private)->symtbl,
2922		   size, abfd);
2923  if (val != size)
2924    perror_with_name ("reading symbol table");
2925
2926  /* If we are reinitializing, or if we have never loaded syms yet, init */
2927  if (mainline
2928      || (objfile->global_psymbols.size == 0
2929	  && objfile->static_psymbols.size == 0))
2930    /* I'm not sure how how good num_symbols is; the rule of thumb in
2931       init_psymbol_list was developed for a.out.  On the one hand,
2932       num_symbols includes auxents.  On the other hand, it doesn't
2933       include N_SLINE.  */
2934    init_psymbol_list (objfile, num_symbols);
2935
2936  free_pending_blocks ();
2937  back_to = make_cleanup (really_free_pendings, 0);
2938
2939  init_minimal_symbol_collection ();
2940  make_cleanup_discard_minimal_symbols ();
2941
2942  /* Now that the symbol table data of the executable file are all in core,
2943     process them and define symbols accordingly.  */
2944
2945  scan_xcoff_symtab (objfile);
2946
2947  /* Install any minimal symbols that have been collected as the current
2948     minimal symbols for this objfile. */
2949
2950  install_minimal_symbols (objfile);
2951
2952  do_cleanups (back_to);
2953}
2954
2955static void
2956xcoff_symfile_offsets (struct objfile *objfile, struct section_addr_info *addrs)
2957{
2958  asection *sect = NULL;
2959  int i;
2960
2961  objfile->num_sections = bfd_count_sections (objfile->obfd);
2962  objfile->section_offsets = (struct section_offsets *)
2963    obstack_alloc (&objfile->objfile_obstack,
2964		   SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));
2965
2966  /* Initialize the section indexes for future use. */
2967  sect = bfd_get_section_by_name (objfile->obfd, ".text");
2968  if (sect)
2969    objfile->sect_index_text = sect->index;
2970
2971  sect = bfd_get_section_by_name (objfile->obfd, ".data");
2972  if (sect)
2973    objfile->sect_index_data = sect->index;
2974
2975  sect = bfd_get_section_by_name (objfile->obfd, ".bss");
2976  if (sect)
2977    objfile->sect_index_bss = sect->index;
2978
2979  sect = bfd_get_section_by_name (objfile->obfd, ".rodata");
2980  if (sect)
2981    objfile->sect_index_rodata = sect->index;
2982
2983  for (i = 0; i < objfile->num_sections; ++i)
2984    {
2985      /* syms_from_objfile kindly subtracts from addr the
2986	 bfd_section_vma of the .text section.  This strikes me as
2987	 wrong--whether the offset to be applied to symbol reading is
2988	 relative to the start address of the section depends on the
2989	 symbol format.  In any event, this whole "addr" concept is
2990	 pretty broken (it doesn't handle any section but .text
2991	 sensibly), so just ignore the addr parameter and use 0.
2992	 rs6000-nat.c will set the correct section offsets via
2993	 objfile_relocate.  */
2994	(objfile->section_offsets)->offsets[i] = 0;
2995    }
2996}
2997
2998/* Register our ability to parse symbols for xcoff BFD files.  */
2999
3000static struct sym_fns xcoff_sym_fns =
3001{
3002
3003  /* It is possible that coff and xcoff should be merged as
3004     they do have fundamental similarities (for example, the extra storage
3005     classes used for stabs could presumably be recognized in any COFF file).
3006     However, in addition to obvious things like all the csect hair, there are
3007     some subtler differences between xcoffread.c and coffread.c, notably
3008     the fact that coffread.c has no need to read in all the symbols, but
3009     xcoffread.c reads all the symbols and does in fact randomly access them
3010     (in C_BSTAT and line number processing).  */
3011
3012  bfd_target_xcoff_flavour,
3013
3014  xcoff_new_init,		/* sym_new_init: init anything gbl to entire symtab */
3015  xcoff_symfile_init,		/* sym_init: read initial info, setup for sym_read() */
3016  xcoff_initial_scan,		/* sym_read: read a symbol file into symtab */
3017  xcoff_symfile_finish,		/* sym_finish: finished with file, cleanup */
3018  xcoff_symfile_offsets,	/* sym_offsets: xlate offsets ext->int form */
3019  NULL				/* next: pointer to next struct sym_fns */
3020};
3021
3022void
3023_initialize_xcoffread (void)
3024{
3025  add_symtab_fns (&xcoff_sym_fns);
3026
3027  func_symbol_type = init_type (TYPE_CODE_FUNC, 1, 0,
3028				"<function, no debug info>", NULL);
3029  TYPE_TARGET_TYPE (func_symbol_type) = builtin_type_int;
3030  var_symbol_type =
3031    init_type (TYPE_CODE_INT, TARGET_INT_BIT / HOST_CHAR_BIT, 0,
3032	       "<variable, no debug info>", NULL);
3033}
3034