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