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