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