2012-07-18 Sergio Durigan Junior <sergiodj@redhat.com>
[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   int secnum;
916   asection *bfd_sect;
917
918   if (name[0] == '.')
919     ++name;
920
921   xcoff_secnum_to_sections (n_scnum, objfile, &bfd_sect, &secnum);
922   prim_record_minimal_symbol_and_info (name, address, ms_type,
923                                        secnum, bfd_sect, objfile);
924 }
925
926 /* xcoff has static blocks marked in `.bs', `.es' pairs.  They cannot be
927    nested.  At any given time, a symbol can only be in one static block.
928    This is the base address of current static block, zero if non exists.  */
929
930 static int static_block_base = 0;
931
932 /* Section number for the current static block.  */
933
934 static int static_block_section = -1;
935
936 /* true if space for symbol name has been allocated.  */
937
938 static int symname_alloced = 0;
939
940 /* Next symbol to read.  Pointer into raw seething symbol table.  */
941
942 static char *raw_symbol;
943
944 /* This is the function which stabsread.c calls to get symbol
945    continuations.  */
946
947 static char *
948 xcoff_next_symbol_text (struct objfile *objfile)
949 {
950   struct internal_syment symbol;
951   char *retval;
952
953   /* FIXME: is this the same as the passed arg?  */
954   if (this_symtab_psymtab)
955     objfile = this_symtab_psymtab->objfile;
956
957   bfd_coff_swap_sym_in (objfile->obfd, raw_symbol, &symbol);
958   if (symbol.n_zeroes)
959     {
960       complaint (&symfile_complaints, _("Unexpected symbol continuation"));
961
962       /* Return something which points to '\0' and hope the symbol reading
963          code does something reasonable.  */
964       retval = "";
965     }
966   else if (symbol.n_sclass & 0x80)
967     {
968       retval = ((struct coff_symfile_info *)
969                 objfile->deprecated_sym_private)->debugsec
970         + symbol.n_offset;
971       raw_symbol += coff_data (objfile->obfd)->local_symesz;
972       ++symnum;
973     }
974   else
975     {
976       complaint (&symfile_complaints, _("Unexpected symbol continuation"));
977
978       /* Return something which points to '\0' and hope the symbol reading
979          code does something reasonable.  */
980       retval = "";
981     }
982   return retval;
983 }
984
985 /* Read symbols for a given partial symbol table.  */
986
987 static void
988 read_xcoff_symtab (struct partial_symtab *pst)
989 {
990   struct objfile *objfile = pst->objfile;
991   bfd *abfd = objfile->obfd;
992   char *raw_auxptr;             /* Pointer to first raw aux entry for sym.  */
993   char *strtbl = 
994     ((struct coff_symfile_info *) objfile->deprecated_sym_private)->strtbl;
995   char *debugsec =
996     ((struct coff_symfile_info *) objfile->deprecated_sym_private)->debugsec;
997   const char *debugfmt = bfd_xcoff_is_xcoff64 (abfd) ? "XCOFF64" : "XCOFF";
998
999   struct internal_syment symbol[1];
1000   union internal_auxent main_aux;
1001   struct coff_symbol cs[1];
1002   CORE_ADDR file_start_addr = 0;
1003   CORE_ADDR file_end_addr = 0;
1004
1005   int next_file_symnum = -1;
1006   unsigned int max_symnum;
1007   int just_started = 1;
1008   int depth = 0;
1009   int fcn_start_addr = 0;
1010
1011   struct coff_symbol fcn_stab_saved = { 0 };
1012
1013   /* fcn_cs_saved is global because process_xcoff_symbol needs it.  */
1014   union internal_auxent fcn_aux_saved = main_aux;
1015   struct context_stack *new;
1016
1017   char *filestring = " _start_ ";       /* Name of the current file.  */
1018
1019   const char *last_csect_name;  /* Last seen csect's name.  */
1020
1021   this_symtab_psymtab = pst;
1022
1023   /* Get the appropriate COFF "constants" related to the file we're
1024      handling.  */
1025   local_symesz = coff_data (abfd)->local_symesz;
1026
1027   last_source_file = NULL;
1028   last_csect_name = 0;
1029
1030   start_stabs ();
1031   start_symtab (filestring, (char *) NULL, file_start_addr);
1032   record_debugformat (debugfmt);
1033   symnum = ((struct symloc *) pst->read_symtab_private)->first_symnum;
1034   max_symnum =
1035     symnum + ((struct symloc *) pst->read_symtab_private)->numsyms;
1036   first_object_file_end = 0;
1037
1038   raw_symbol =
1039     ((struct coff_symfile_info *) objfile->deprecated_sym_private)->symtbl
1040     + symnum * local_symesz;
1041
1042   while (symnum < max_symnum)
1043     {
1044       QUIT;                     /* make this command interruptable.  */
1045
1046       /* READ_ONE_SYMBOL (symbol, cs, symname_alloced); */
1047       /* read one symbol into `cs' structure.  After processing the
1048          whole symbol table, only string table will be kept in memory,
1049          symbol table and debug section of xcoff will be freed.  Thus
1050          we can mark symbols with names in string table as
1051          `alloced'.  */
1052       {
1053         int ii;
1054
1055         /* Swap and align the symbol into a reasonable C structure.  */
1056         bfd_coff_swap_sym_in (abfd, raw_symbol, symbol);
1057
1058         cs->c_symnum = symnum;
1059         cs->c_naux = symbol->n_numaux;
1060         if (symbol->n_zeroes)
1061           {
1062             symname_alloced = 0;
1063             /* We must use the original, unswapped, name here so the name field
1064                pointed to by cs->c_name will persist throughout xcoffread.  If
1065                we use the new field, it gets overwritten for each symbol.  */
1066             cs->c_name = ((struct external_syment *) raw_symbol)->e.e_name;
1067             /* If it's exactly E_SYMNMLEN characters long it isn't
1068                '\0'-terminated.  */
1069             if (cs->c_name[E_SYMNMLEN - 1] != '\0')
1070               {
1071                 char *p;
1072
1073                 p = obstack_alloc (&objfile->objfile_obstack, E_SYMNMLEN + 1);
1074                 strncpy (p, cs->c_name, E_SYMNMLEN);
1075                 p[E_SYMNMLEN] = '\0';
1076                 cs->c_name = p;
1077                 symname_alloced = 1;
1078               }
1079           }
1080         else if (symbol->n_sclass & 0x80)
1081           {
1082             cs->c_name = debugsec + symbol->n_offset;
1083             symname_alloced = 0;
1084           }
1085         else
1086           {
1087             /* in string table */
1088             cs->c_name = strtbl + (int) symbol->n_offset;
1089             symname_alloced = 1;
1090           }
1091         cs->c_value = symbol->n_value;
1092         cs->c_sclass = symbol->n_sclass;
1093         cs->c_secnum = symbol->n_scnum;
1094         cs->c_type = (unsigned) symbol->n_type;
1095
1096         raw_symbol += local_symesz;
1097         ++symnum;
1098
1099         /* Save addr of first aux entry.  */
1100         raw_auxptr = raw_symbol;
1101
1102         /* Skip all the auxents associated with this symbol.  */
1103         for (ii = symbol->n_numaux; ii; --ii)
1104           {
1105             raw_symbol += coff_data (abfd)->local_auxesz;
1106             ++symnum;
1107           }
1108       }
1109
1110       /* if symbol name starts with ".$" or "$", ignore it.  */
1111       if (cs->c_name[0] == '$'
1112           || (cs->c_name[1] == '$' && cs->c_name[0] == '.'))
1113         continue;
1114
1115       if (cs->c_symnum == next_file_symnum && cs->c_sclass != C_FILE)
1116         {
1117           if (last_source_file)
1118             {
1119               pst->symtab = end_symtab (cur_src_end_addr, objfile,
1120                                         SECT_OFF_TEXT (objfile));
1121               end_stabs ();
1122             }
1123
1124           start_stabs ();
1125           start_symtab ("_globals_", (char *) NULL, (CORE_ADDR) 0);
1126           record_debugformat (debugfmt);
1127           cur_src_end_addr = first_object_file_end;
1128           /* Done with all files, everything from here on is globals.  */
1129         }
1130
1131       if ((cs->c_sclass == C_EXT || cs->c_sclass == C_HIDEXT)
1132           && cs->c_naux == 1)
1133         {
1134           /* Dealing with a symbol with a csect entry.  */
1135
1136 #define CSECT(PP) ((PP)->x_csect)
1137 #define CSECT_LEN(PP) (CSECT(PP).x_scnlen.l)
1138 #define CSECT_ALIGN(PP) (SMTYP_ALIGN(CSECT(PP).x_smtyp))
1139 #define CSECT_SMTYP(PP) (SMTYP_SMTYP(CSECT(PP).x_smtyp))
1140 #define CSECT_SCLAS(PP) (CSECT(PP).x_smclas)
1141
1142           /* Convert the auxent to something we can access.  */
1143           bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
1144                                 0, cs->c_naux, &main_aux);
1145
1146           switch (CSECT_SMTYP (&main_aux))
1147             {
1148
1149             case XTY_ER:
1150               /* Ignore all external references.  */
1151               continue;
1152
1153             case XTY_SD:
1154               /* A section description.  */
1155               {
1156                 switch (CSECT_SCLAS (&main_aux))
1157                   {
1158
1159                   case XMC_PR:
1160                     {
1161
1162                       /* A program csect is seen.  We have to allocate one
1163                          symbol table for each program csect.  Normally gdb
1164                          prefers one symtab for each source file.  In case
1165                          of AIX, one source file might include more than one
1166                          [PR] csect, and they don't have to be adjacent in
1167                          terms of the space they occupy in memory.  Thus, one
1168                          single source file might get fragmented in the
1169                          memory and gdb's file start and end address
1170                          approach does not work!  GCC (and I think xlc) seem
1171                          to put all the code in the unnamed program csect.  */
1172
1173                       if (last_csect_name)
1174                         {
1175                           complete_symtab (filestring, file_start_addr);
1176                           cur_src_end_addr = file_end_addr;
1177                           end_symtab (file_end_addr, objfile,
1178                                       SECT_OFF_TEXT (objfile));
1179                           end_stabs ();
1180                           start_stabs ();
1181                           /* Give all csects for this source file the same
1182                              name.  */
1183                           start_symtab (filestring, NULL, (CORE_ADDR) 0);
1184                           record_debugformat (debugfmt);
1185                         }
1186
1187                       /* If this is the very first csect seen,
1188                          basically `__start'.  */
1189                       if (just_started)
1190                         {
1191                           first_object_file_end
1192                             = cs->c_value + CSECT_LEN (&main_aux);
1193                           just_started = 0;
1194                         }
1195
1196                       file_start_addr =
1197                         cs->c_value + ANOFFSET (objfile->section_offsets,
1198                                                 SECT_OFF_TEXT (objfile));
1199                       file_end_addr = file_start_addr + CSECT_LEN (&main_aux);
1200
1201                       if (cs->c_name && (cs->c_name[0] == '.' || cs->c_name[0] == '@'))
1202                         last_csect_name = cs->c_name;
1203                     }
1204                     continue;
1205
1206                     /* All other symbols are put into the minimal symbol
1207                        table only.  */
1208
1209                   case XMC_RW:
1210                     continue;
1211
1212                   case XMC_TC0:
1213                     continue;
1214
1215                   case XMC_TC:
1216                     continue;
1217
1218                   default:
1219                     /* Ignore the symbol.  */
1220                     continue;
1221                   }
1222               }
1223               break;
1224
1225             case XTY_LD:
1226
1227               switch (CSECT_SCLAS (&main_aux))
1228                 {
1229                 case XMC_PR:
1230                   /* a function entry point.  */
1231                 function_entry_point:
1232
1233                   fcn_start_addr = cs->c_value;
1234
1235                   /* save the function header info, which will be used
1236                      when `.bf' is seen.  */
1237                   fcn_cs_saved = *cs;
1238                   fcn_aux_saved = main_aux;
1239                   continue;
1240
1241                 case XMC_GL:
1242                   /* shared library function trampoline code entry point.  */
1243                   continue;
1244
1245                 case XMC_DS:
1246                   /* The symbols often have the same names as debug symbols for
1247                      functions, and confuse lookup_symbol.  */
1248                   continue;
1249
1250                 default:
1251                   /* xlc puts each variable in a separate csect, so we get
1252                      an XTY_SD for each variable.  But gcc puts several
1253                      variables in a csect, so that each variable only gets
1254                      an XTY_LD.  This will typically be XMC_RW; I suspect
1255                      XMC_RO and XMC_BS might be possible too.
1256                      These variables are put in the minimal symbol table
1257                      only.  */
1258                   continue;
1259                 }
1260               break;
1261
1262             case XTY_CM:
1263               /* Common symbols are put into the minimal symbol table only.  */
1264               continue;
1265
1266             default:
1267               break;
1268             }
1269         }
1270
1271       /* If explicitly specified as a function, treat is as one.  This check
1272          evaluates to true for @FIX* bigtoc CSECT symbols, so it must occur
1273          after the above CSECT check.  */
1274       if (ISFCN (cs->c_type) && cs->c_sclass != C_TPDEF)
1275         {
1276           bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
1277                                 0, cs->c_naux, &main_aux);
1278           goto function_entry_point;
1279         }
1280
1281       switch (cs->c_sclass)
1282         {
1283         case C_FILE:
1284
1285           /* c_value field contains symnum of next .file entry in table
1286              or symnum of first global after last .file.  */
1287
1288           next_file_symnum = cs->c_value;
1289
1290           /* Complete symbol table for last object file containing
1291              debugging information.  */
1292
1293           /* Whether or not there was a csect in the previous file, we
1294              have to call `end_stabs' and `start_stabs' to reset
1295              type_vector, line_vector, etc. structures.  */
1296
1297           complete_symtab (filestring, file_start_addr);
1298           cur_src_end_addr = file_end_addr;
1299           end_symtab (file_end_addr, objfile, SECT_OFF_TEXT (objfile));
1300           end_stabs ();
1301
1302           /* XCOFF, according to the AIX 3.2 documentation, puts the
1303              filename in cs->c_name.  But xlc 1.3.0.2 has decided to
1304              do things the standard COFF way and put it in the auxent.
1305              We use the auxent if the symbol is ".file" and an auxent
1306              exists, otherwise use the symbol itself.  Simple
1307              enough.  */
1308           if (!strcmp (cs->c_name, ".file") && cs->c_naux > 0)
1309             {
1310               bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
1311                                     0, cs->c_naux, &main_aux);
1312               filestring = coff_getfilename (&main_aux, objfile);
1313             }
1314           else
1315             filestring = cs->c_name;
1316
1317           start_stabs ();
1318           start_symtab (filestring, (char *) NULL, (CORE_ADDR) 0);
1319           record_debugformat (debugfmt);
1320           last_csect_name = 0;
1321
1322           /* reset file start and end addresses.  A compilation unit
1323              with no text (only data) should have zero file
1324              boundaries.  */
1325           file_start_addr = file_end_addr = 0;
1326           break;
1327
1328         case C_FUN:
1329           fcn_stab_saved = *cs;
1330           break;
1331
1332         case C_FCN:
1333           if (strcmp (cs->c_name, ".bf") == 0)
1334             {
1335               CORE_ADDR off = ANOFFSET (objfile->section_offsets,
1336                                         SECT_OFF_TEXT (objfile));
1337
1338               bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
1339                                     0, cs->c_naux, &main_aux);
1340
1341               within_function = 1;
1342
1343               new = push_context (0, fcn_start_addr + off);
1344
1345               new->name = define_symbol
1346                 (fcn_cs_saved.c_value + off,
1347                  fcn_stab_saved.c_name, 0, 0, objfile);
1348               if (new->name != NULL)
1349                 SYMBOL_SECTION (new->name) = SECT_OFF_TEXT (objfile);
1350             }
1351           else if (strcmp (cs->c_name, ".ef") == 0)
1352             {
1353               bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
1354                                     0, cs->c_naux, &main_aux);
1355
1356               /* The value of .ef is the address of epilogue code;
1357                  not useful for gdb.  */
1358               /* { main_aux.x_sym.x_misc.x_lnsz.x_lnno
1359                  contains number of lines to '}' */
1360
1361               if (context_stack_depth <= 0)
1362                 {       /* We attempted to pop an empty context stack.  */
1363                   ef_complaint (cs->c_symnum);
1364                   within_function = 0;
1365                   break;
1366                 }
1367               new = pop_context ();
1368               /* Stack must be empty now.  */
1369               if (context_stack_depth > 0 || new == NULL)
1370                 {
1371                   ef_complaint (cs->c_symnum);
1372                   within_function = 0;
1373                   break;
1374                 }
1375
1376               finish_block (new->name, &local_symbols, new->old_blocks,
1377                             new->start_addr,
1378                             (fcn_cs_saved.c_value
1379                              + fcn_aux_saved.x_sym.x_misc.x_fsize
1380                              + ANOFFSET (objfile->section_offsets,
1381                                          SECT_OFF_TEXT (objfile))),
1382                             objfile);
1383               within_function = 0;
1384             }
1385           break;
1386
1387         case C_BSTAT:
1388           /* Begin static block.  */
1389           {
1390             struct internal_syment symbol;
1391
1392             read_symbol (&symbol, cs->c_value);
1393             static_block_base = symbol.n_value;
1394             static_block_section =
1395               secnum_to_section (symbol.n_scnum, objfile);
1396           }
1397           break;
1398
1399         case C_ESTAT:
1400           /* End of static block.  */
1401           static_block_base = 0;
1402           static_block_section = -1;
1403           break;
1404
1405         case C_ARG:
1406         case C_REGPARM:
1407         case C_REG:
1408         case C_TPDEF:
1409         case C_STRTAG:
1410         case C_UNTAG:
1411         case C_ENTAG:
1412           {
1413             complaint (&symfile_complaints,
1414                        _("Unrecognized storage class %d."),
1415                        cs->c_sclass);
1416           }
1417           break;
1418
1419         case C_LABEL:
1420         case C_NULL:
1421           /* Ignore these.  */
1422           break;
1423
1424         case C_HIDEXT:
1425         case C_STAT:
1426           break;
1427
1428         case C_BINCL:
1429           /* beginning of include file */
1430           /* In xlc output, C_BINCL/C_EINCL pair doesn't show up in sorted
1431              order.  Thus, when wee see them, we might not know enough info
1432              to process them.  Thus, we'll be saving them into a table 
1433              (inclTable) and postpone their processing.  */
1434
1435           record_include_begin (cs);
1436           break;
1437
1438         case C_EINCL:
1439           /* End of include file.  */
1440           /* See the comment after case C_BINCL.  */
1441           record_include_end (cs);
1442           break;
1443
1444         case C_BLOCK:
1445           if (strcmp (cs->c_name, ".bb") == 0)
1446             {
1447               depth++;
1448               new = push_context (depth,
1449                                   (cs->c_value
1450                                    + ANOFFSET (objfile->section_offsets,
1451                                                SECT_OFF_TEXT (objfile))));
1452             }
1453           else if (strcmp (cs->c_name, ".eb") == 0)
1454             {
1455               if (context_stack_depth <= 0)
1456                 {       /* We attempted to pop an empty context stack.  */
1457                   eb_complaint (cs->c_symnum);
1458                   break;
1459                 }
1460               new = pop_context ();
1461               if (depth-- != new->depth)
1462                 {
1463                   eb_complaint (cs->c_symnum);
1464                   break;
1465                 }
1466               if (local_symbols && context_stack_depth > 0)
1467                 {
1468                   /* Make a block for the local symbols within.  */
1469                   finish_block (new->name, &local_symbols, new->old_blocks,
1470                                 new->start_addr,
1471                                 (cs->c_value
1472                                  + ANOFFSET (objfile->section_offsets,
1473                                              SECT_OFF_TEXT (objfile))),
1474                                 objfile);
1475                 }
1476               local_symbols = new->locals;
1477             }
1478           break;
1479
1480         default:
1481           process_xcoff_symbol (cs, objfile);
1482           break;
1483         }
1484     }
1485
1486   if (last_source_file)
1487     {
1488       struct symtab *s;
1489
1490       complete_symtab (filestring, file_start_addr);
1491       cur_src_end_addr = file_end_addr;
1492       s = end_symtab (file_end_addr, objfile, SECT_OFF_TEXT (objfile));
1493       /* When reading symbols for the last C_FILE of the objfile, try
1494          to make sure that we set pst->symtab to the symtab for the
1495          file, not to the _globals_ symtab.  I'm not sure whether this
1496          actually works right or when/if it comes up.  */
1497       if (pst->symtab == NULL)
1498         pst->symtab = s;
1499       end_stabs ();
1500     }
1501 }
1502
1503 #define SYMBOL_DUP(SYMBOL1, SYMBOL2)    \
1504   (SYMBOL2) = (struct symbol *)         \
1505         obstack_alloc (&objfile->objfile_obstack, sizeof (struct symbol)); \
1506   *(SYMBOL2) = *(SYMBOL1);
1507
1508
1509 #define SYMNAME_ALLOC(NAME, ALLOCED)    \
1510   ((ALLOCED) ? (NAME) : obsavestring ((NAME), strlen (NAME), \
1511                                       &objfile->objfile_obstack))
1512
1513
1514 /* process one xcoff symbol.  */
1515
1516 static struct symbol *
1517 process_xcoff_symbol (struct coff_symbol *cs, struct objfile *objfile)
1518 {
1519   struct symbol onesymbol;
1520   struct symbol *sym = &onesymbol;
1521   struct symbol *sym2 = NULL;
1522   char *name, *pp;
1523
1524   int sec;
1525   CORE_ADDR off;
1526
1527   if (cs->c_secnum < 0)
1528     {
1529       /* The value is a register number, offset within a frame, etc.,
1530          and does not get relocated.  */
1531       off = 0;
1532       sec = -1;
1533     }
1534   else
1535     {
1536       sec = secnum_to_section (cs->c_secnum, objfile);
1537       off = ANOFFSET (objfile->section_offsets, sec);
1538     }
1539
1540   name = cs->c_name;
1541   if (name[0] == '.')
1542     ++name;
1543
1544   memset (sym, '\0', sizeof (struct symbol));
1545
1546   /* default assumptions */
1547   SYMBOL_VALUE_ADDRESS (sym) = cs->c_value + off;
1548   SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
1549   SYMBOL_SECTION (sym) = secnum_to_section (cs->c_secnum, objfile);
1550
1551   if (ISFCN (cs->c_type))
1552     {
1553       /* At this point, we don't know the type of the function.  This
1554          will be patched with the type from its stab entry later on in
1555          patch_block_stabs (), unless the file was compiled without -g.  */
1556
1557       SYMBOL_SET_LINKAGE_NAME (sym, SYMNAME_ALLOC (name, symname_alloced));
1558       SYMBOL_TYPE (sym) = objfile_type (objfile)->nodebug_text_symbol;
1559
1560       SYMBOL_CLASS (sym) = LOC_BLOCK;
1561       SYMBOL_DUP (sym, sym2);
1562
1563       if (cs->c_sclass == C_EXT)
1564         add_symbol_to_list (sym2, &global_symbols);
1565       else if (cs->c_sclass == C_HIDEXT || cs->c_sclass == C_STAT)
1566         add_symbol_to_list (sym2, &file_symbols);
1567     }
1568   else
1569     {
1570       /* In case we can't figure out the type, provide default.  */
1571       SYMBOL_TYPE (sym) = objfile_type (objfile)->nodebug_data_symbol;
1572
1573       switch (cs->c_sclass)
1574         {
1575 #if 0
1576           /* The values of functions and global symbols are now resolved
1577              via the global_sym_chain in stabsread.c.  */
1578         case C_FUN:
1579           if (fcn_cs_saved.c_sclass == C_EXT)
1580             add_stab_to_list (name, &global_stabs);
1581           else
1582             add_stab_to_list (name, &file_stabs);
1583           break;
1584
1585         case C_GSYM:
1586           add_stab_to_list (name, &global_stabs);
1587           break;
1588 #endif
1589
1590         case C_BCOMM:
1591           common_block_start (cs->c_name, objfile);
1592           break;
1593
1594         case C_ECOMM:
1595           common_block_end (objfile);
1596           break;
1597
1598         default:
1599           complaint (&symfile_complaints, _("Unexpected storage class: %d"),
1600                      cs->c_sclass);
1601           /* FALLTHROUGH */
1602
1603         case C_DECL:
1604         case C_PSYM:
1605         case C_RPSYM:
1606         case C_ECOML:
1607         case C_LSYM:
1608         case C_RSYM:
1609         case C_GSYM:
1610
1611           {
1612             sym = define_symbol (cs->c_value + off, cs->c_name, 0, 0, objfile);
1613             if (sym != NULL)
1614               {
1615                 SYMBOL_SECTION (sym) = sec;
1616               }
1617             return sym;
1618           }
1619
1620         case C_STSYM:
1621
1622           /* For xlc (not GCC), the 'V' symbol descriptor is used for
1623              all statics and we need to distinguish file-scope versus
1624              function-scope using within_function.  We do this by
1625              changing the string we pass to define_symbol to use 'S'
1626              where we need to, which is not necessarily super-clean,
1627              but seems workable enough.  */
1628
1629           if (*name == ':')
1630             return NULL;
1631
1632           pp = strchr (name, ':');
1633           if (pp == NULL)
1634             return NULL;
1635
1636           ++pp;
1637           if (*pp == 'V' && !within_function)
1638             *pp = 'S';
1639           sym = define_symbol ((cs->c_value
1640                                 + ANOFFSET (objfile->section_offsets,
1641                                             static_block_section)),
1642                                cs->c_name, 0, 0, objfile);
1643           if (sym != NULL)
1644             {
1645               SYMBOL_VALUE_ADDRESS (sym) += static_block_base;
1646               SYMBOL_SECTION (sym) = static_block_section;
1647             }
1648           return sym;
1649
1650         }
1651     }
1652   return sym2;
1653 }
1654
1655 /* Extract the file name from the aux entry of a C_FILE symbol.
1656    Result is in static storage and is only good for temporary use.  */
1657
1658 static char *
1659 coff_getfilename (union internal_auxent *aux_entry, struct objfile *objfile)
1660 {
1661   static char buffer[BUFSIZ];
1662
1663   if (aux_entry->x_file.x_n.x_zeroes == 0)
1664     strcpy (buffer, ((struct coff_symfile_info *)
1665                      objfile->deprecated_sym_private)->strtbl
1666             + aux_entry->x_file.x_n.x_offset);
1667   else
1668     {
1669       strncpy (buffer, aux_entry->x_file.x_fname, FILNMLEN);
1670       buffer[FILNMLEN] = '\0';
1671     }
1672   return (buffer);
1673 }
1674
1675 /* Set *SYMBOL to symbol number symno in symtbl.  */
1676 static void
1677 read_symbol (struct internal_syment *symbol, int symno)
1678 {
1679   int nsyms
1680     = ((struct coff_symfile_info *)
1681        this_symtab_psymtab->objfile->deprecated_sym_private)->symtbl_num_syms;
1682   char *stbl = ((struct coff_symfile_info *)
1683                 this_symtab_psymtab->objfile->deprecated_sym_private)->symtbl;
1684
1685   if (symno < 0 || symno >= nsyms)
1686     {
1687       complaint (&symfile_complaints, _("Invalid symbol offset"));
1688       symbol->n_value = 0;
1689       symbol->n_scnum = -1;
1690       return;
1691     }
1692   bfd_coff_swap_sym_in (this_symtab_psymtab->objfile->obfd,
1693                         stbl + (symno * local_symesz),
1694                         symbol);
1695 }
1696
1697 /* Get value corresponding to symbol number symno in symtbl.  */
1698
1699 static CORE_ADDR
1700 read_symbol_nvalue (int symno)
1701 {
1702   struct internal_syment symbol[1];
1703
1704   read_symbol (symbol, symno);
1705   return symbol->n_value;
1706 }
1707
1708
1709 /* Find the address of the function corresponding to symno, where
1710    symno is the symbol pointed to by the linetable.  */
1711
1712 static int
1713 read_symbol_lineno (int symno)
1714 {
1715   struct objfile *objfile = this_symtab_psymtab->objfile;
1716   int xcoff64 = bfd_xcoff_is_xcoff64 (objfile->obfd);
1717
1718   struct coff_symfile_info *info =
1719     (struct coff_symfile_info *)objfile->deprecated_sym_private;
1720   int nsyms = info->symtbl_num_syms;
1721   char *stbl = info->symtbl;
1722   char *strtbl = info->strtbl;
1723
1724   struct internal_syment symbol[1];
1725   union internal_auxent main_aux[1];
1726
1727   if (symno < 0)
1728     {
1729       bf_notfound_complaint ();
1730       return 0;
1731     }
1732
1733   /* Note that just searching for a short distance (e.g. 50 symbols)
1734      is not enough, at least in the following case.
1735
1736      .extern foo
1737      [many .stabx entries]
1738      [a few functions, referring to foo]
1739      .globl foo
1740      .bf
1741
1742      What happens here is that the assembler moves the .stabx entries
1743      to right before the ".bf" for foo, but the symbol for "foo" is before
1744      all the stabx entries.  See PR gdb/2222.  */
1745
1746   /* Maintaining a table of .bf entries might be preferable to this search.
1747      If I understand things correctly it would need to be done only for
1748      the duration of a single psymtab to symtab conversion.  */
1749   while (symno < nsyms)
1750     {
1751       bfd_coff_swap_sym_in (symfile_bfd,
1752                             stbl + (symno * local_symesz), symbol);
1753       if (symbol->n_sclass == C_FCN)
1754         {
1755           char *name = xcoff64 ? strtbl + symbol->n_offset : symbol->n_name;
1756
1757           if (strcmp (name, ".bf") == 0)
1758             goto gotit;
1759         }
1760       symno += symbol->n_numaux + 1;
1761     }
1762
1763   bf_notfound_complaint ();
1764   return 0;
1765
1766 gotit:
1767   /* Take aux entry and return its lineno.  */
1768   symno++;
1769   bfd_coff_swap_aux_in (objfile->obfd, stbl + symno * local_symesz,
1770                         symbol->n_type, symbol->n_sclass,
1771                         0, symbol->n_numaux, main_aux);
1772
1773   return main_aux->x_sym.x_misc.x_lnsz.x_lnno;
1774 }
1775
1776 /* Support for line number handling.  */
1777
1778 /* This function is called for every section; it finds the outer limits
1779  * of the line table (minimum and maximum file offset) so that the
1780  * mainline code can read the whole thing for efficiency.
1781  */
1782 static void
1783 find_linenos (struct bfd *abfd, struct bfd_section *asect, void *vpinfo)
1784 {
1785   struct coff_symfile_info *info;
1786   int size, count;
1787   file_ptr offset, maxoff;
1788
1789   count = asect->lineno_count;
1790
1791   if (strcmp (asect->name, ".text") != 0 || count == 0)
1792     return;
1793
1794   size = count * coff_data (abfd)->local_linesz;
1795   info = (struct coff_symfile_info *) vpinfo;
1796   offset = asect->line_filepos;
1797   maxoff = offset + size;
1798
1799   if (offset < info->min_lineno_offset || info->min_lineno_offset == 0)
1800     info->min_lineno_offset = offset;
1801
1802   if (maxoff > info->max_lineno_offset)
1803     info->max_lineno_offset = maxoff;
1804 }
1805 \f
1806 static void xcoff_psymtab_to_symtab_1 (struct partial_symtab *);
1807
1808 static void
1809 xcoff_psymtab_to_symtab_1 (struct partial_symtab *pst)
1810 {
1811   struct cleanup *old_chain;
1812   int i;
1813
1814   if (!pst)
1815     return;
1816
1817   if (pst->readin)
1818     {
1819       fprintf_unfiltered
1820         (gdb_stderr, "Psymtab for %s already read in.  Shouldn't happen.\n",
1821          pst->filename);
1822       return;
1823     }
1824
1825   /* Read in all partial symtabs on which this one is dependent.  */
1826   for (i = 0; i < pst->number_of_dependencies; i++)
1827     if (!pst->dependencies[i]->readin)
1828       {
1829         /* Inform about additional files that need to be read in.  */
1830         if (info_verbose)
1831           {
1832             fputs_filtered (" ", gdb_stdout);
1833             wrap_here ("");
1834             fputs_filtered ("and ", gdb_stdout);
1835             wrap_here ("");
1836             printf_filtered ("%s...", pst->dependencies[i]->filename);
1837             wrap_here ("");     /* Flush output */
1838             gdb_flush (gdb_stdout);
1839           }
1840         xcoff_psymtab_to_symtab_1 (pst->dependencies[i]);
1841       }
1842
1843   if (((struct symloc *) pst->read_symtab_private)->numsyms != 0)
1844     {
1845       /* Init stuff necessary for reading in symbols.  */
1846       stabsread_init ();
1847       buildsym_init ();
1848       old_chain = make_cleanup (really_free_pendings, 0);
1849
1850       read_xcoff_symtab (pst);
1851
1852       do_cleanups (old_chain);
1853     }
1854
1855   pst->readin = 1;
1856 }
1857
1858 static void xcoff_psymtab_to_symtab (struct partial_symtab *);
1859
1860 /* Read in all of the symbols for a given psymtab for real.
1861    Be verbose about it if the user wants that.  */
1862
1863 static void
1864 xcoff_psymtab_to_symtab (struct partial_symtab *pst)
1865 {
1866   bfd *sym_bfd;
1867
1868   if (!pst)
1869     return;
1870
1871   if (pst->readin)
1872     {
1873       fprintf_unfiltered
1874         (gdb_stderr, "Psymtab for %s already read in.  Shouldn't happen.\n",
1875          pst->filename);
1876       return;
1877     }
1878
1879   if (((struct symloc *) pst->read_symtab_private)->numsyms != 0
1880       || pst->number_of_dependencies)
1881     {
1882       /* Print the message now, before reading the string table,
1883          to avoid disconcerting pauses.  */
1884       if (info_verbose)
1885         {
1886           printf_filtered ("Reading in symbols for %s...", pst->filename);
1887           gdb_flush (gdb_stdout);
1888         }
1889
1890       sym_bfd = pst->objfile->obfd;
1891
1892       next_symbol_text_func = xcoff_next_symbol_text;
1893
1894       xcoff_psymtab_to_symtab_1 (pst);
1895
1896       /* Match with global symbols.  This only needs to be done once,
1897          after all of the symtabs and dependencies have been read in.   */
1898       scan_file_globals (pst->objfile);
1899
1900       /* Finish up the debug error message.  */
1901       if (info_verbose)
1902         printf_filtered ("done.\n");
1903     }
1904 }
1905 \f
1906 static void
1907 xcoff_new_init (struct objfile *objfile)
1908 {
1909   stabsread_new_init ();
1910   buildsym_new_init ();
1911 }
1912
1913 /* Do initialization in preparation for reading symbols from OBJFILE.
1914
1915    We will only be called if this is an XCOFF or XCOFF-like file.
1916    BFD handles figuring out the format of the file, and code in symfile.c
1917    uses BFD's determination to vector to us.  */
1918
1919 static void
1920 xcoff_symfile_init (struct objfile *objfile)
1921 {
1922   /* Allocate struct to keep track of the symfile.  */
1923   objfile->deprecated_sym_private
1924     = xmalloc (sizeof (struct coff_symfile_info));
1925
1926   /* XCOFF objects may be reordered, so set OBJF_REORDERED.  If we
1927      find this causes a significant slowdown in gdb then we could
1928      set it in the debug symbol readers only when necessary.  */
1929   objfile->flags |= OBJF_REORDERED;
1930
1931   init_entry_point_info (objfile);
1932 }
1933
1934 /* Perform any local cleanups required when we are done with a particular
1935    objfile.  I.E, we are in the process of discarding all symbol information
1936    for an objfile, freeing up all memory held for it, and unlinking the
1937    objfile struct from the global list of known objfiles.  */
1938
1939 static void
1940 xcoff_symfile_finish (struct objfile *objfile)
1941 {
1942   if (objfile->deprecated_sym_private != NULL)
1943     {
1944       xfree (objfile->deprecated_sym_private);
1945     }
1946
1947   /* Start with a fresh include table for the next objfile.  */
1948   if (inclTable)
1949     {
1950       xfree (inclTable);
1951       inclTable = NULL;
1952     }
1953   inclIndx = inclLength = inclDepth = 0;
1954
1955   dwarf2_free_objfile (objfile);
1956 }
1957
1958
1959 static void
1960 init_stringtab (bfd *abfd, file_ptr offset, struct objfile *objfile)
1961 {
1962   long length;
1963   int val;
1964   unsigned char lengthbuf[4];
1965   char *strtbl;
1966
1967   ((struct coff_symfile_info *) objfile->deprecated_sym_private)->strtbl
1968     = NULL;
1969
1970   if (bfd_seek (abfd, offset, SEEK_SET) < 0)
1971     error (_("cannot seek to string table in %s: %s"),
1972            bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
1973
1974   val = bfd_bread ((char *) lengthbuf, sizeof lengthbuf, abfd);
1975   length = bfd_h_get_32 (abfd, lengthbuf);
1976
1977   /* If no string table is needed, then the file may end immediately
1978      after the symbols.  Just return with `strtbl' set to NULL.  */
1979
1980   if (val != sizeof lengthbuf || length < sizeof lengthbuf)
1981     return;
1982
1983   /* Allocate string table from objfile_obstack.  We will need this table
1984      as long as we have its symbol table around.  */
1985
1986   strtbl = (char *) obstack_alloc (&objfile->objfile_obstack, length);
1987   ((struct coff_symfile_info *) objfile->deprecated_sym_private)->strtbl
1988     = strtbl;
1989
1990   /* Copy length buffer, the first byte is usually zero and is
1991      used for stabs with a name length of zero.  */
1992   memcpy (strtbl, lengthbuf, sizeof lengthbuf);
1993   if (length == sizeof lengthbuf)
1994     return;
1995
1996   val = bfd_bread (strtbl + sizeof lengthbuf, length - sizeof lengthbuf, abfd);
1997
1998   if (val != length - sizeof lengthbuf)
1999     error (_("cannot read string table from %s: %s"),
2000            bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
2001   if (strtbl[length - 1] != '\0')
2002     error (_("bad symbol file: string table "
2003              "does not end with null character"));
2004
2005   return;
2006 }
2007 \f
2008 /* If we have not yet seen a function for this psymtab, this is 0.  If we
2009    have seen one, it is the offset in the line numbers of the line numbers
2010    for the psymtab.  */
2011 static unsigned int first_fun_line_offset;
2012
2013 /* Allocate and partially fill a partial symtab.  It will be
2014    completely filled at the end of the symbol list.
2015
2016    SYMFILE_NAME is the name of the symbol-file we are reading from, and ADDR
2017    is the address relative to which its symbols are (incremental) or 0
2018    (normal).  */
2019
2020 static struct partial_symtab *
2021 xcoff_start_psymtab (struct objfile *objfile,
2022                      const char *filename, int first_symnum,
2023                      struct partial_symbol **global_syms,
2024                      struct partial_symbol **static_syms)
2025 {
2026   struct partial_symtab *result =
2027     start_psymtab_common (objfile, objfile->section_offsets,
2028                           filename,
2029                           /* We fill in textlow later.  */
2030                           0,
2031                           global_syms, static_syms);
2032
2033   result->read_symtab_private = obstack_alloc (&objfile->objfile_obstack,
2034                                                sizeof (struct symloc));
2035   ((struct symloc *) result->read_symtab_private)->first_symnum = first_symnum;
2036   result->read_symtab = xcoff_psymtab_to_symtab;
2037
2038   /* Deduce the source language from the filename for this psymtab.  */
2039   psymtab_language = deduce_language_from_filename (filename);
2040
2041   return result;
2042 }
2043
2044 /* Close off the current usage of PST.
2045    Returns PST, or NULL if the partial symtab was empty and thrown away.
2046
2047    CAPPING_SYMBOL_NUMBER is the end of pst (exclusive).
2048
2049    INCLUDE_LIST, NUM_INCLUDES, DEPENDENCY_LIST, and NUMBER_DEPENDENCIES
2050    are the information for includes and dependencies.  */
2051
2052 static struct partial_symtab *
2053 xcoff_end_psymtab (struct partial_symtab *pst, const char **include_list,
2054                    int num_includes, int capping_symbol_number,
2055                    struct partial_symtab **dependency_list,
2056                    int number_dependencies, int textlow_not_set)
2057 {
2058   int i;
2059   struct objfile *objfile = pst->objfile;
2060
2061   if (capping_symbol_number != -1)
2062     ((struct symloc *) pst->read_symtab_private)->numsyms =
2063       capping_symbol_number
2064       - ((struct symloc *) pst->read_symtab_private)->first_symnum;
2065   ((struct symloc *) pst->read_symtab_private)->lineno_off =
2066     first_fun_line_offset;
2067   first_fun_line_offset = 0;
2068   pst->n_global_syms = objfile->global_psymbols.next
2069     - (objfile->global_psymbols.list + pst->globals_offset);
2070   pst->n_static_syms = objfile->static_psymbols.next
2071     - (objfile->static_psymbols.list + pst->statics_offset);
2072
2073   pst->number_of_dependencies = number_dependencies;
2074   if (number_dependencies)
2075     {
2076       pst->dependencies = (struct partial_symtab **)
2077         obstack_alloc (&objfile->objfile_obstack,
2078                     number_dependencies * sizeof (struct partial_symtab *));
2079       memcpy (pst->dependencies, dependency_list,
2080               number_dependencies * sizeof (struct partial_symtab *));
2081     }
2082   else
2083     pst->dependencies = 0;
2084
2085   for (i = 0; i < num_includes; i++)
2086     {
2087       struct partial_symtab *subpst =
2088         allocate_psymtab (include_list[i], objfile);
2089
2090       subpst->section_offsets = pst->section_offsets;
2091       subpst->read_symtab_private = obstack_alloc (&objfile->objfile_obstack,
2092                                                    sizeof (struct symloc));
2093       ((struct symloc *) subpst->read_symtab_private)->first_symnum = 0;
2094       ((struct symloc *) subpst->read_symtab_private)->numsyms = 0;
2095       subpst->textlow = 0;
2096       subpst->texthigh = 0;
2097
2098       /* We could save slight bits of space by only making one of these,
2099          shared by the entire set of include files.  FIXME-someday.  */
2100       subpst->dependencies = (struct partial_symtab **)
2101         obstack_alloc (&objfile->objfile_obstack,
2102                        sizeof (struct partial_symtab *));
2103       subpst->dependencies[0] = pst;
2104       subpst->number_of_dependencies = 1;
2105
2106       subpst->globals_offset =
2107         subpst->n_global_syms =
2108         subpst->statics_offset =
2109         subpst->n_static_syms = 0;
2110
2111       subpst->readin = 0;
2112       subpst->symtab = 0;
2113       subpst->read_symtab = pst->read_symtab;
2114     }
2115
2116   sort_pst_symbols (pst);
2117
2118   if (num_includes == 0
2119       && number_dependencies == 0
2120       && pst->n_global_syms == 0
2121       && pst->n_static_syms == 0)
2122     {
2123       /* Throw away this psymtab, it's empty.  We can't deallocate it, since
2124          it is on the obstack, but we can forget to chain it on the list.  */
2125       /* Empty psymtabs happen as a result of header files which don't have
2126          any symbols in them.  There can be a lot of them.  */
2127
2128       discard_psymtab (pst);
2129
2130       /* Indicate that psymtab was thrown away.  */
2131       pst = (struct partial_symtab *) NULL;
2132     }
2133   return pst;
2134 }
2135
2136 /* Swap raw symbol at *RAW and put the name in *NAME, the symbol in
2137    *SYMBOL, the first auxent in *AUX.  Advance *RAW and *SYMNUMP over
2138    the symbol and its auxents.  */
2139
2140 static void
2141 swap_sym (struct internal_syment *symbol, union internal_auxent *aux,
2142           const char **name, char **raw, unsigned int *symnump,
2143           struct objfile *objfile)
2144 {
2145   bfd_coff_swap_sym_in (objfile->obfd, *raw, symbol);
2146   if (symbol->n_zeroes)
2147     {
2148       /* If it's exactly E_SYMNMLEN characters long it isn't
2149          '\0'-terminated.  */
2150       if (symbol->n_name[E_SYMNMLEN - 1] != '\0')
2151         {
2152           /* FIXME: wastes memory for symbols which we don't end up putting
2153              into the minimal symbols.  */
2154           char *p;
2155
2156           p = obstack_alloc (&objfile->objfile_obstack, E_SYMNMLEN + 1);
2157           strncpy (p, symbol->n_name, E_SYMNMLEN);
2158           p[E_SYMNMLEN] = '\0';
2159           *name = p;
2160         }
2161       else
2162         /* Point to the unswapped name as that persists as long as the
2163            objfile does.  */
2164         *name = ((struct external_syment *) *raw)->e.e_name;
2165     }
2166   else if (symbol->n_sclass & 0x80)
2167     {
2168       *name = ((struct coff_symfile_info *)
2169                objfile->deprecated_sym_private)->debugsec + symbol->n_offset;
2170     }
2171   else
2172     {
2173       *name = ((struct coff_symfile_info *)
2174                objfile->deprecated_sym_private)->strtbl + symbol->n_offset;
2175     }
2176   ++*symnump;
2177   *raw += coff_data (objfile->obfd)->local_symesz;
2178   if (symbol->n_numaux > 0)
2179     {
2180       bfd_coff_swap_aux_in (objfile->obfd, *raw, symbol->n_type,
2181                             symbol->n_sclass, 0, symbol->n_numaux, aux);
2182
2183       *symnump += symbol->n_numaux;
2184       *raw += coff_data (objfile->obfd)->local_symesz * symbol->n_numaux;
2185     }
2186 }
2187
2188 static void
2189 function_outside_compilation_unit_complaint (const char *arg1)
2190 {
2191   complaint (&symfile_complaints,
2192              _("function `%s' appears to be defined "
2193                "outside of all compilation units"),
2194              arg1);
2195 }
2196
2197 static void
2198 scan_xcoff_symtab (struct objfile *objfile)
2199 {
2200   struct gdbarch *gdbarch = get_objfile_arch (objfile);
2201   CORE_ADDR toc_offset = 0;     /* toc offset value in data section.  */
2202   const char *filestring = NULL;
2203
2204   const char *namestring;
2205   int past_first_source_file = 0;
2206   bfd *abfd;
2207   asection *bfd_sect;
2208   unsigned int nsyms;
2209
2210   /* Current partial symtab */
2211   struct partial_symtab *pst;
2212
2213   /* List of current psymtab's include files.  */
2214   const char **psymtab_include_list;
2215   int includes_allocated;
2216   int includes_used;
2217
2218   /* Index within current psymtab dependency list.  */
2219   struct partial_symtab **dependency_list;
2220   int dependencies_used, dependencies_allocated;
2221
2222   char *sraw_symbol;
2223   struct internal_syment symbol;
2224   union internal_auxent main_aux[5];
2225   unsigned int ssymnum;
2226
2227   const char *last_csect_name = NULL; /* Last seen csect's name and value.  */
2228   CORE_ADDR last_csect_val = 0;
2229   int last_csect_sec = 0;
2230   int misc_func_recorded = 0;   /* true if any misc. function.  */
2231   int textlow_not_set = 1;
2232
2233   pst = (struct partial_symtab *) 0;
2234
2235   includes_allocated = 30;
2236   includes_used = 0;
2237   psymtab_include_list = (const char **) alloca (includes_allocated *
2238                                                  sizeof (const char *));
2239
2240   dependencies_allocated = 30;
2241   dependencies_used = 0;
2242   dependency_list =
2243     (struct partial_symtab **) alloca (dependencies_allocated *
2244                                        sizeof (struct partial_symtab *));
2245
2246   last_source_file = NULL;
2247
2248   abfd = objfile->obfd;
2249   next_symbol_text_func = xcoff_next_symbol_text;
2250
2251   sraw_symbol = ((struct coff_symfile_info *)
2252                  objfile->deprecated_sym_private)->symtbl;
2253   nsyms = ((struct coff_symfile_info *)
2254            objfile->deprecated_sym_private)->symtbl_num_syms;
2255   ssymnum = 0;
2256   while (ssymnum < nsyms)
2257     {
2258       int sclass;
2259
2260       QUIT;
2261
2262       bfd_coff_swap_sym_in (abfd, sraw_symbol, &symbol);
2263       sclass = symbol.n_sclass;
2264
2265       switch (sclass)
2266         {
2267         case C_EXT:
2268         case C_HIDEXT:
2269           {
2270             /* The CSECT auxent--always the last auxent.  */
2271             union internal_auxent csect_aux;
2272             unsigned int symnum_before = ssymnum;
2273
2274             swap_sym (&symbol, &main_aux[0], &namestring, &sraw_symbol,
2275                       &ssymnum, objfile);
2276             if (symbol.n_numaux > 1)
2277               {
2278                 bfd_coff_swap_aux_in
2279                   (objfile->obfd,
2280                    sraw_symbol - coff_data (abfd)->local_symesz,
2281                    symbol.n_type,
2282                    symbol.n_sclass,
2283                    symbol.n_numaux - 1,
2284                    symbol.n_numaux,
2285                    &csect_aux);
2286               }
2287             else
2288               csect_aux = main_aux[0];
2289
2290             /* If symbol name starts with ".$" or "$", ignore it.  */
2291             if (namestring[0] == '$'
2292                 || (namestring[0] == '.' && namestring[1] == '$'))
2293               break;
2294
2295             switch (csect_aux.x_csect.x_smtyp & 0x7)
2296               {
2297               case XTY_SD:
2298                 switch (csect_aux.x_csect.x_smclas)
2299                   {
2300                   case XMC_PR:
2301                     if (last_csect_name)
2302                       {
2303                         /* If no misc. function recorded in the last
2304                            seen csect, enter it as a function.  This
2305                            will take care of functions like strcmp()
2306                            compiled by xlc.  */
2307
2308                         if (!misc_func_recorded)
2309                           {
2310                             record_minimal_symbol
2311                               (last_csect_name, last_csect_val,
2312                                mst_text, last_csect_sec, objfile);
2313                             misc_func_recorded = 1;
2314                           }
2315
2316                         if (pst != NULL)
2317                           {
2318                             /* We have to allocate one psymtab for
2319                                each program csect, because their text
2320                                sections need not be adjacent.  */
2321                             xcoff_end_psymtab
2322                               (pst, psymtab_include_list, includes_used,
2323                                symnum_before, dependency_list,
2324                                dependencies_used, textlow_not_set);
2325                             includes_used = 0;
2326                             dependencies_used = 0;
2327                             /* Give all psymtabs for this source file the same
2328                                name.  */
2329                             pst = xcoff_start_psymtab
2330                               (objfile,
2331                                filestring,
2332                                symnum_before,
2333                                objfile->global_psymbols.next,
2334                                objfile->static_psymbols.next);
2335                           }
2336                       }
2337                     /* Activate the misc_func_recorded mechanism for
2338                        compiler- and linker-generated CSECTs like ".strcmp"
2339                        and "@FIX1".  */ 
2340                     if (namestring && (namestring[0] == '.'
2341                                        || namestring[0] == '@'))
2342                       {
2343                         last_csect_name = namestring;
2344                         last_csect_val = symbol.n_value;
2345                         last_csect_sec = symbol.n_scnum;
2346                       }
2347                     if (pst != NULL)
2348                       {
2349                         CORE_ADDR highval =
2350                           symbol.n_value + csect_aux.x_csect.x_scnlen.l;
2351
2352                         if (highval > pst->texthigh)
2353                           pst->texthigh = highval;
2354                         if (pst->textlow == 0 || symbol.n_value < pst->textlow)
2355                           pst->textlow = symbol.n_value;
2356                       }
2357                     misc_func_recorded = 0;
2358                     break;
2359
2360                   case XMC_RW:
2361                   case XMC_TD:
2362                     /* Data variables are recorded in the minimal symbol
2363                        table, except for section symbols.  */
2364                     if (*namestring != '.')
2365                       prim_record_minimal_symbol_and_info
2366                         (namestring, symbol.n_value,
2367                          sclass == C_HIDEXT ? mst_file_data : mst_data,
2368                          secnum_to_section (symbol.n_scnum, objfile),
2369                          secnum_to_bfd_section (symbol.n_scnum, objfile),
2370                          objfile);
2371                     break;
2372
2373                   case XMC_TC0:
2374                     if (toc_offset)
2375                       warning (_("More than one XMC_TC0 symbol found."));
2376                     toc_offset = symbol.n_value;
2377
2378                     /* Make TOC offset relative to start address of
2379                        section.  */
2380                     bfd_sect = secnum_to_bfd_section (symbol.n_scnum, objfile);
2381                     if (bfd_sect)
2382                       toc_offset -= bfd_section_vma (objfile->obfd, bfd_sect);
2383                     break;
2384
2385                   case XMC_TC:
2386                     /* These symbols tell us where the TOC entry for a
2387                        variable is, not the variable itself.  */
2388                     break;
2389
2390                   default:
2391                     break;
2392                   }
2393                 break;
2394
2395               case XTY_LD:
2396                 switch (csect_aux.x_csect.x_smclas)
2397                   {
2398                   case XMC_PR:
2399                     /* A function entry point.  */
2400
2401                     if (first_fun_line_offset == 0 && symbol.n_numaux > 1)
2402                       first_fun_line_offset =
2403                         main_aux[0].x_sym.x_fcnary.x_fcn.x_lnnoptr;
2404                       {
2405                         record_minimal_symbol
2406                           (namestring, symbol.n_value,
2407                            sclass == C_HIDEXT ? mst_file_text : mst_text,
2408                            symbol.n_scnum, objfile);
2409                         misc_func_recorded = 1;
2410                       }
2411                     break;
2412
2413                   case XMC_GL:
2414                     /* shared library function trampoline code entry
2415                        point.  */
2416
2417                     /* record trampoline code entries as
2418                        mst_solib_trampoline symbol.  When we lookup mst
2419                        symbols, we will choose mst_text over
2420                        mst_solib_trampoline.  */
2421                     record_minimal_symbol
2422                       (namestring, symbol.n_value,
2423                        mst_solib_trampoline, symbol.n_scnum, objfile);
2424                     misc_func_recorded = 1;
2425                     break;
2426
2427                   case XMC_DS:
2428                     /* The symbols often have the same names as
2429                        debug symbols for functions, and confuse
2430                        lookup_symbol.  */
2431                     break;
2432
2433                   default:
2434
2435                     /* xlc puts each variable in a separate csect,
2436                        so we get an XTY_SD for each variable.  But
2437                        gcc puts several variables in a csect, so
2438                        that each variable only gets an XTY_LD.  We
2439                        still need to record them.  This will
2440                        typically be XMC_RW; I suspect XMC_RO and
2441                        XMC_BS might be possible too.  */
2442                     if (*namestring != '.')
2443                       prim_record_minimal_symbol_and_info
2444                         (namestring, symbol.n_value,
2445                          sclass == C_HIDEXT ? mst_file_data : mst_data,
2446                          secnum_to_section (symbol.n_scnum, objfile),
2447                          secnum_to_bfd_section (symbol.n_scnum, objfile),
2448                          objfile);
2449                     break;
2450                   }
2451                 break;
2452
2453               case XTY_CM:
2454                 switch (csect_aux.x_csect.x_smclas)
2455                   {
2456                   case XMC_RW:
2457                   case XMC_BS:
2458                     /* Common variables are recorded in the minimal symbol
2459                        table, except for section symbols.  */
2460                     if (*namestring != '.')
2461                       prim_record_minimal_symbol_and_info
2462                         (namestring, symbol.n_value,
2463                          sclass == C_HIDEXT ? mst_file_bss : mst_bss,
2464                          secnum_to_section (symbol.n_scnum, objfile),
2465                          secnum_to_bfd_section (symbol.n_scnum, objfile),
2466                          objfile);
2467                     break;
2468                   }
2469                 break;
2470
2471               default:
2472                 break;
2473               }
2474           }
2475           break;
2476         case C_FILE:
2477           {
2478             unsigned int symnum_before;
2479
2480             symnum_before = ssymnum;
2481             swap_sym (&symbol, &main_aux[0], &namestring, &sraw_symbol,
2482                       &ssymnum, objfile);
2483
2484             /* See if the last csect needs to be recorded.  */
2485
2486             if (last_csect_name && !misc_func_recorded)
2487               {
2488                 /* If no misc. function recorded in the last seen csect, enter
2489                    it as a function.  This will take care of functions like
2490                    strcmp() compiled by xlc.  */
2491
2492                 record_minimal_symbol (last_csect_name, last_csect_val,
2493                                        mst_text, last_csect_sec, objfile);
2494                 misc_func_recorded = 1;
2495               }
2496
2497             if (pst)
2498               {
2499                 xcoff_end_psymtab (pst, psymtab_include_list, includes_used,
2500                                    symnum_before, dependency_list,
2501                                    dependencies_used, textlow_not_set);
2502                 includes_used = 0;
2503                 dependencies_used = 0;
2504               }
2505             first_fun_line_offset = 0;
2506
2507             /* XCOFF, according to the AIX 3.2 documentation, puts the
2508                filename in cs->c_name.  But xlc 1.3.0.2 has decided to
2509                do things the standard COFF way and put it in the auxent.
2510                We use the auxent if the symbol is ".file" and an auxent
2511                exists, otherwise use the symbol itself.  */
2512             if (!strcmp (namestring, ".file") && symbol.n_numaux > 0)
2513               {
2514                 filestring = coff_getfilename (&main_aux[0], objfile);
2515               }
2516             else
2517               filestring = namestring;
2518
2519             pst = xcoff_start_psymtab (objfile,
2520                                        filestring,
2521                                        symnum_before,
2522                                        objfile->global_psymbols.next,
2523                                        objfile->static_psymbols.next);
2524             last_csect_name = NULL;
2525           }
2526           break;
2527
2528         default:
2529           {
2530             complaint (&symfile_complaints,
2531                        _("Storage class %d not recognized during scan"),
2532                        sclass);
2533           }
2534           /* FALLTHROUGH */
2535
2536           /* C_FCN is .bf and .ef symbols.  I think it is sufficient
2537              to handle only the C_FUN and C_EXT.  */
2538         case C_FCN:
2539
2540         case C_BSTAT:
2541         case C_ESTAT:
2542         case C_ARG:
2543         case C_REGPARM:
2544         case C_REG:
2545         case C_TPDEF:
2546         case C_STRTAG:
2547         case C_UNTAG:
2548         case C_ENTAG:
2549         case C_LABEL:
2550         case C_NULL:
2551
2552           /* C_EINCL means we are switching back to the main file.  But there
2553              is no reason to care; the only thing we want to know about
2554              includes is the names of all the included (.h) files.  */
2555         case C_EINCL:
2556
2557         case C_BLOCK:
2558
2559           /* I don't think C_STAT is used in xcoff; C_HIDEXT appears to be
2560              used instead.  */
2561         case C_STAT:
2562
2563           /* I don't think the name of the common block (as opposed to the
2564              variables within it) is something which is user visible
2565              currently.  */
2566         case C_BCOMM:
2567         case C_ECOMM:
2568
2569         case C_PSYM:
2570         case C_RPSYM:
2571
2572           /* I think we can ignore C_LSYM; types on xcoff seem to use C_DECL
2573              so C_LSYM would appear to be only for locals.  */
2574         case C_LSYM:
2575
2576         case C_AUTO:
2577         case C_RSYM:
2578           {
2579             /* We probably could save a few instructions by assuming that
2580                C_LSYM, C_PSYM, etc., never have auxents.  */
2581             int naux1 = symbol.n_numaux + 1;
2582
2583             ssymnum += naux1;
2584             sraw_symbol += bfd_coff_symesz (abfd) * naux1;
2585           }
2586           break;
2587
2588         case C_BINCL:
2589           {
2590             /* Mark down an include file in the current psymtab.  */
2591             enum language tmp_language;
2592
2593             swap_sym (&symbol, &main_aux[0], &namestring, &sraw_symbol,
2594                       &ssymnum, objfile);
2595
2596             tmp_language = deduce_language_from_filename (namestring);
2597
2598             /* Only change the psymtab's language if we've learned
2599                something useful (eg. tmp_language is not language_unknown).
2600                In addition, to match what start_subfile does, never change
2601                from C++ to C.  */
2602             if (tmp_language != language_unknown
2603                 && (tmp_language != language_c
2604                     || psymtab_language != language_cplus))
2605               psymtab_language = tmp_language;
2606
2607             /* In C++, one may expect the same filename to come round many
2608                times, when code is coming alternately from the main file
2609                and from inline functions in other files.  So I check to see
2610                if this is a file we've seen before -- either the main
2611                source file, or a previously included file.
2612
2613                This seems to be a lot of time to be spending on N_SOL, but
2614                things like "break c-exp.y:435" need to work (I
2615                suppose the psymtab_include_list could be hashed or put
2616                in a binary tree, if profiling shows this is a major hog).  */
2617             if (pst && strcmp (namestring, pst->filename) == 0)
2618               continue;
2619
2620             {
2621               int i;
2622
2623               for (i = 0; i < includes_used; i++)
2624                 if (strcmp (namestring, psymtab_include_list[i]) == 0)
2625                   {
2626                     i = -1;
2627                     break;
2628                   }
2629               if (i == -1)
2630                 continue;
2631             }
2632             psymtab_include_list[includes_used++] = namestring;
2633             if (includes_used >= includes_allocated)
2634               {
2635                 const char **orig = psymtab_include_list;
2636
2637                 psymtab_include_list = (const char **)
2638                   alloca ((includes_allocated *= 2) *
2639                           sizeof (const char *));
2640                 memcpy (psymtab_include_list, orig,
2641                         includes_used * sizeof (const char *));
2642               }
2643             continue;
2644           }
2645         case C_FUN:
2646           /* The value of the C_FUN is not the address of the function (it
2647              appears to be the address before linking), but as long as it
2648              is smaller than the actual address, then find_pc_partial_function
2649              will use the minimal symbols instead.  I hope.  */
2650
2651         case C_GSYM:
2652         case C_ECOML:
2653         case C_DECL:
2654         case C_STSYM:
2655           {
2656             char *p;
2657
2658             swap_sym (&symbol, &main_aux[0], &namestring, &sraw_symbol,
2659                       &ssymnum, objfile);
2660
2661             p = strchr (namestring, ':');
2662             if (!p)
2663               continue;                 /* Not a debugging symbol.   */
2664
2665             /* Main processing section for debugging symbols which
2666                the initial read through the symbol tables needs to worry
2667                about.  If we reach this point, the symbol which we are
2668                considering is definitely one we are interested in.
2669                p must also contain the (valid) index into the namestring
2670                which indicates the debugging type symbol.  */
2671
2672             switch (p[1])
2673               {
2674               case 'S':
2675                 symbol.n_value += ANOFFSET (objfile->section_offsets,
2676                                             SECT_OFF_DATA (objfile));
2677
2678                 if (gdbarch_static_transform_name_p (gdbarch))
2679                   namestring = gdbarch_static_transform_name
2680                                  (gdbarch, namestring);
2681
2682                 add_psymbol_to_list (namestring, p - namestring, 1,
2683                                      VAR_DOMAIN, LOC_STATIC,
2684                                      &objfile->static_psymbols,
2685                                      0, symbol.n_value,
2686                                      psymtab_language, objfile);
2687                 continue;
2688
2689               case 'G':
2690                 symbol.n_value += ANOFFSET (objfile->section_offsets,
2691                                             SECT_OFF_DATA (objfile));
2692                 /* The addresses in these entries are reported to be
2693                    wrong.  See the code that reads 'G's for symtabs.  */
2694                 add_psymbol_to_list (namestring, p - namestring, 1,
2695                                      VAR_DOMAIN, LOC_STATIC,
2696                                      &objfile->global_psymbols,
2697                                      0, symbol.n_value,
2698                                      psymtab_language, objfile);
2699                 continue;
2700
2701               case 'T':
2702                 /* When a 'T' entry is defining an anonymous enum, it
2703                    may have a name which is the empty string, or a
2704                    single space.  Since they're not really defining a
2705                    symbol, those shouldn't go in the partial symbol
2706                    table.  We do pick up the elements of such enums at
2707                    'check_enum:', below.  */
2708                 if (p >= namestring + 2
2709                     || (p == namestring + 1
2710                         && namestring[0] != ' '))
2711                   {
2712                     add_psymbol_to_list (namestring, p - namestring, 1,
2713                                          STRUCT_DOMAIN, LOC_TYPEDEF,
2714                                          &objfile->static_psymbols,
2715                                          symbol.n_value, 0,
2716                                          psymtab_language, objfile);
2717                     if (p[2] == 't')
2718                       {
2719                         /* Also a typedef with the same name.  */
2720                         add_psymbol_to_list (namestring, p - namestring, 1,
2721                                              VAR_DOMAIN, LOC_TYPEDEF,
2722                                              &objfile->static_psymbols,
2723                                              symbol.n_value, 0,
2724                                              psymtab_language, objfile);
2725                         p += 1;
2726                       }
2727                   }
2728                 goto check_enum;
2729
2730               case 't':
2731                 if (p != namestring)    /* a name is there, not just :T...  */
2732                   {
2733                     add_psymbol_to_list (namestring, p - namestring, 1,
2734                                          VAR_DOMAIN, LOC_TYPEDEF,
2735                                          &objfile->static_psymbols,
2736                                          symbol.n_value, 0,
2737                                          psymtab_language, objfile);
2738                   }
2739               check_enum:
2740                 /* If this is an enumerated type, we need to
2741                    add all the enum constants to the partial symbol
2742                    table.  This does not cover enums without names, e.g.
2743                    "enum {a, b} c;" in C, but fortunately those are
2744                    rare.  There is no way for GDB to find those from the
2745                    enum type without spending too much time on it.  Thus
2746                    to solve this problem, the compiler needs to put out the
2747                    enum in a nameless type.  GCC2 does this.  */
2748
2749                 /* We are looking for something of the form
2750                    <name> ":" ("t" | "T") [<number> "="] "e"
2751                    {<constant> ":" <value> ","} ";".  */
2752
2753                 /* Skip over the colon and the 't' or 'T'.  */
2754                 p += 2;
2755                 /* This type may be given a number.  Also, numbers can come
2756                    in pairs like (0,26).  Skip over it.  */
2757                 while ((*p >= '0' && *p <= '9')
2758                        || *p == '(' || *p == ',' || *p == ')'
2759                        || *p == '=')
2760                   p++;
2761
2762                 if (*p++ == 'e')
2763                   {
2764                     /* The aix4 compiler emits extra crud before the
2765                        members.  */
2766                     if (*p == '-')
2767                       {
2768                         /* Skip over the type (?).  */
2769                         while (*p != ':')
2770                           p++;
2771
2772                         /* Skip over the colon.  */
2773                         p++;
2774                       }
2775
2776                     /* We have found an enumerated type.  */
2777                     /* According to comments in read_enum_type
2778                        a comma could end it instead of a semicolon.
2779                        I don't know where that happens.
2780                        Accept either.  */
2781                     while (*p && *p != ';' && *p != ',')
2782                       {
2783                         char *q;
2784
2785                         /* Check for and handle cretinous dbx symbol name
2786                            continuation!  */
2787                         if (*p == '\\' || (*p == '?' && p[1] == '\0'))
2788                           p = next_symbol_text (objfile);
2789
2790                         /* Point to the character after the name
2791                            of the enum constant.  */
2792                         for (q = p; *q && *q != ':'; q++)
2793                           ;
2794                         /* Note that the value doesn't matter for
2795                            enum constants in psymtabs, just in symtabs.  */
2796                         add_psymbol_to_list (p, q - p, 1,
2797                                              VAR_DOMAIN, LOC_CONST,
2798                                              &objfile->static_psymbols, 0,
2799                                              0, psymtab_language, objfile);
2800                         /* Point past the name.  */
2801                         p = q;
2802                         /* Skip over the value.  */
2803                         while (*p && *p != ',')
2804                           p++;
2805                         /* Advance past the comma.  */
2806                         if (*p)
2807                           p++;
2808                       }
2809                   }
2810                 continue;
2811
2812               case 'c':
2813                 /* Constant, e.g. from "const" in Pascal.  */
2814                 add_psymbol_to_list (namestring, p - namestring, 1,
2815                                      VAR_DOMAIN, LOC_CONST,
2816                                      &objfile->static_psymbols, symbol.n_value,
2817                                      0, psymtab_language, objfile);
2818                 continue;
2819
2820               case 'f':
2821                 if (! pst)
2822                   {
2823                     int name_len = p - namestring;
2824                     char *name = xmalloc (name_len + 1);
2825
2826                     memcpy (name, namestring, name_len);
2827                     name[name_len] = '\0';
2828                     function_outside_compilation_unit_complaint (name);
2829                     xfree (name);
2830                   }
2831                 symbol.n_value += ANOFFSET (objfile->section_offsets,
2832                                             SECT_OFF_TEXT (objfile));
2833                 add_psymbol_to_list (namestring, p - namestring, 1,
2834                                      VAR_DOMAIN, LOC_BLOCK,
2835                                      &objfile->static_psymbols,
2836                                      0, symbol.n_value,
2837                                      psymtab_language, objfile);
2838                 continue;
2839
2840                 /* Global functions were ignored here, but now they
2841                    are put into the global psymtab like one would expect.
2842                    They're also in the minimal symbol table.  */
2843               case 'F':
2844                 if (! pst)
2845                   {
2846                     int name_len = p - namestring;
2847                     char *name = xmalloc (name_len + 1);
2848
2849                     memcpy (name, namestring, name_len);
2850                     name[name_len] = '\0';
2851                     function_outside_compilation_unit_complaint (name);
2852                     xfree (name);
2853                   }
2854
2855                 /* We need only the minimal symbols for these
2856                    loader-generated definitions.  Keeping the global
2857                    symbols leads to "in psymbols but not in symbols"
2858                    errors.  */
2859                 if (strncmp (namestring, "@FIX", 4) == 0)
2860                   continue;
2861
2862                 symbol.n_value += ANOFFSET (objfile->section_offsets,
2863                                             SECT_OFF_TEXT (objfile));
2864                 add_psymbol_to_list (namestring, p - namestring, 1,
2865                                      VAR_DOMAIN, LOC_BLOCK,
2866                                      &objfile->global_psymbols,
2867                                      0, symbol.n_value,
2868                                      psymtab_language, objfile);
2869                 continue;
2870
2871                 /* Two things show up here (hopefully); static symbols of
2872                    local scope (static used inside braces) or extensions
2873                    of structure symbols.  We can ignore both.  */
2874               case 'V':
2875               case '(':
2876               case '0':
2877               case '1':
2878               case '2':
2879               case '3':
2880               case '4':
2881               case '5':
2882               case '6':
2883               case '7':
2884               case '8':
2885               case '9':
2886               case '-':
2887               case '#':         /* For symbol identification (used in
2888                                    live ranges).  */
2889                 continue;
2890
2891               case ':':
2892                 /* It is a C++ nested symbol.  We don't need to record it
2893                    (I don't think); if we try to look up foo::bar::baz,
2894                    then symbols for the symtab containing foo should get
2895                    read in, I think.  */
2896                 /* Someone says sun cc puts out symbols like
2897                    /foo/baz/maclib::/usr/local/bin/maclib,
2898                    which would get here with a symbol type of ':'.  */
2899                 continue;
2900
2901               default:
2902                 /* Unexpected symbol descriptor.  The second and
2903                    subsequent stabs of a continued stab can show up
2904                    here.  The question is whether they ever can mimic
2905                    a normal stab--it would be nice if not, since we
2906                    certainly don't want to spend the time searching to
2907                    the end of every string looking for a
2908                    backslash.  */
2909
2910                 complaint (&symfile_complaints,
2911                            _("unknown symbol descriptor `%c'"), p[1]);
2912
2913                 /* Ignore it; perhaps it is an extension that we don't
2914                    know about.  */
2915                 continue;
2916               }
2917           }
2918         }
2919     }
2920
2921   if (pst)
2922     {
2923       xcoff_end_psymtab (pst, psymtab_include_list, includes_used,
2924                          ssymnum, dependency_list,
2925                          dependencies_used, textlow_not_set);
2926     }
2927
2928   /* Record the toc offset value of this symbol table into objfile
2929      structure.  If no XMC_TC0 is found, toc_offset should be zero.
2930      Another place to obtain this information would be file auxiliary
2931      header.  */
2932
2933   ((struct coff_symfile_info *) objfile->deprecated_sym_private)->toc_offset
2934     = toc_offset;
2935 }
2936
2937 /* Return the toc offset value for a given objfile.  */
2938
2939 CORE_ADDR
2940 xcoff_get_toc_offset (struct objfile *objfile)
2941 {
2942   if (objfile)
2943     return ((struct coff_symfile_info *)
2944             objfile->deprecated_sym_private)->toc_offset;
2945   return 0;
2946 }
2947
2948 /* Scan and build partial symbols for a symbol file.
2949    We have been initialized by a call to dbx_symfile_init, which 
2950    put all the relevant info into a "struct dbx_symfile_info",
2951    hung off the objfile structure.
2952
2953    SECTION_OFFSETS contains offsets relative to which the symbols in the
2954    various sections are (depending where the sections were actually
2955    loaded).  */
2956
2957 static void
2958 xcoff_initial_scan (struct objfile *objfile, int symfile_flags)
2959 {
2960   bfd *abfd;
2961   int val;
2962   struct cleanup *back_to;
2963   int num_symbols;              /* # of symbols */
2964   file_ptr symtab_offset;       /* symbol table and */
2965   file_ptr stringtab_offset;    /* string table file offsets */
2966   struct coff_symfile_info *info;
2967   char *name;
2968   unsigned int size;
2969
2970   info = (struct coff_symfile_info *) objfile->deprecated_sym_private;
2971   symfile_bfd = abfd = objfile->obfd;
2972   name = objfile->name;
2973
2974   num_symbols = bfd_get_symcount (abfd);        /* # of symbols */
2975   symtab_offset = obj_sym_filepos (abfd);       /* symbol table file offset */
2976   stringtab_offset = symtab_offset +
2977     num_symbols * coff_data (abfd)->local_symesz;
2978
2979   info->min_lineno_offset = 0;
2980   info->max_lineno_offset = 0;
2981   bfd_map_over_sections (abfd, find_linenos, info);
2982
2983   if (num_symbols > 0)
2984     {
2985       /* Read the string table.  */
2986       init_stringtab (abfd, stringtab_offset, objfile);
2987
2988       /* Read the .debug section, if present.  */
2989       {
2990         struct bfd_section *secp;
2991         bfd_size_type length;
2992         char *debugsec = NULL;
2993
2994         secp = bfd_get_section_by_name (abfd, ".debug");
2995         if (secp)
2996           {
2997             length = bfd_section_size (abfd, secp);
2998             if (length)
2999               {
3000                 debugsec =
3001                   (char *) obstack_alloc (&objfile->objfile_obstack, length);
3002
3003                 if (!bfd_get_section_contents (abfd, secp, debugsec,
3004                                                (file_ptr) 0, length))
3005                   {
3006                     error (_("Error reading .debug section of `%s': %s"),
3007                            name, bfd_errmsg (bfd_get_error ()));
3008                   }
3009               }
3010           }
3011         ((struct coff_symfile_info *)
3012          objfile->deprecated_sym_private)->debugsec
3013           = debugsec;
3014       }
3015     }
3016
3017   /* Read the symbols.  We keep them in core because we will want to
3018      access them randomly in read_symbol*.  */
3019   val = bfd_seek (abfd, symtab_offset, SEEK_SET);
3020   if (val < 0)
3021     error (_("Error reading symbols from %s: %s"),
3022            name, bfd_errmsg (bfd_get_error ()));
3023   size = coff_data (abfd)->local_symesz * num_symbols;
3024   ((struct coff_symfile_info *) objfile->deprecated_sym_private)->symtbl =
3025     obstack_alloc (&objfile->objfile_obstack, size);
3026   ((struct coff_symfile_info *)
3027    objfile->deprecated_sym_private)->symtbl_num_syms
3028     = num_symbols;
3029
3030   val = bfd_bread (((struct coff_symfile_info *)
3031                     objfile->deprecated_sym_private)->symtbl,
3032                    size, abfd);
3033   if (val != size)
3034     perror_with_name (_("reading symbol table"));
3035
3036   /* If we are reinitializing, or if we have never loaded syms yet, init.  */
3037   if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
3038     /* I'm not sure how how good num_symbols is; the rule of thumb in
3039        init_psymbol_list was developed for a.out.  On the one hand,
3040        num_symbols includes auxents.  On the other hand, it doesn't
3041        include N_SLINE.  */
3042     init_psymbol_list (objfile, num_symbols);
3043
3044   free_pending_blocks ();
3045   back_to = make_cleanup (really_free_pendings, 0);
3046
3047   init_minimal_symbol_collection ();
3048   make_cleanup_discard_minimal_symbols ();
3049
3050   /* Now that the symbol table data of the executable file are all in core,
3051      process them and define symbols accordingly.  */
3052
3053   scan_xcoff_symtab (objfile);
3054
3055   /* Install any minimal symbols that have been collected as the current
3056      minimal symbols for this objfile.  */
3057
3058   install_minimal_symbols (objfile);
3059
3060   /* DWARF2 sections.  */
3061
3062   if (dwarf2_has_info (objfile, &dwarf2_xcoff_names))
3063     dwarf2_build_psymtabs (objfile);
3064
3065   dwarf2_build_frame_info (objfile);
3066
3067   do_cleanups (back_to);
3068 }
3069 \f
3070 static void
3071 xcoff_symfile_offsets (struct objfile *objfile,
3072                        struct section_addr_info *addrs)
3073 {
3074   asection *sect = NULL;
3075   int i;
3076
3077   objfile->num_sections = bfd_count_sections (objfile->obfd);
3078   objfile->section_offsets = (struct section_offsets *)
3079     obstack_alloc (&objfile->objfile_obstack, 
3080                    SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));
3081
3082   /* Initialize the section indexes for future use.  */
3083   sect = bfd_get_section_by_name (objfile->obfd, ".text");
3084   if (sect) 
3085     objfile->sect_index_text = sect->index;
3086
3087   sect = bfd_get_section_by_name (objfile->obfd, ".data");
3088   if (sect) 
3089     objfile->sect_index_data = sect->index;
3090
3091   sect = bfd_get_section_by_name (objfile->obfd, ".bss");
3092   if (sect) 
3093     objfile->sect_index_bss = sect->index;
3094
3095   sect = bfd_get_section_by_name (objfile->obfd, ".rodata");
3096   if (sect) 
3097     objfile->sect_index_rodata = sect->index;
3098
3099   for (i = 0; i < objfile->num_sections; ++i)
3100     {
3101       /* syms_from_objfile kindly subtracts from addr the
3102          bfd_section_vma of the .text section.  This strikes me as
3103          wrong--whether the offset to be applied to symbol reading is
3104          relative to the start address of the section depends on the
3105          symbol format.  In any event, this whole "addr" concept is
3106          pretty broken (it doesn't handle any section but .text
3107          sensibly), so just ignore the addr parameter and use 0.
3108          rs6000-nat.c will set the correct section offsets via
3109          objfile_relocate.  */
3110         (objfile->section_offsets)->offsets[i] = 0;
3111     }
3112 }
3113
3114 /* Register our ability to parse symbols for xcoff BFD files.  */
3115
3116 static const struct sym_fns xcoff_sym_fns =
3117 {
3118
3119   /* It is possible that coff and xcoff should be merged as
3120      they do have fundamental similarities (for example, the extra storage
3121      classes used for stabs could presumably be recognized in any COFF file).
3122      However, in addition to obvious things like all the csect hair, there are
3123      some subtler differences between xcoffread.c and coffread.c, notably
3124      the fact that coffread.c has no need to read in all the symbols, but
3125      xcoffread.c reads all the symbols and does in fact randomly access them
3126      (in C_BSTAT and line number processing).  */
3127
3128   bfd_target_xcoff_flavour,
3129
3130   xcoff_new_init,               /* init anything gbl to entire symtab */
3131   xcoff_symfile_init,           /* read initial info, setup for sym_read() */
3132   xcoff_initial_scan,           /* read a symbol file into symtab */
3133   NULL,                         /* sym_read_psymbols */
3134   xcoff_symfile_finish,         /* finished with file, cleanup */
3135   xcoff_symfile_offsets,        /* xlate offsets ext->int form */
3136   default_symfile_segments,     /* Get segment information from a file.  */
3137   aix_process_linenos,
3138   default_symfile_relocate,     /* Relocate a debug section.  */
3139   NULL,                         /* sym_probe_fns */
3140   &psym_functions
3141 };
3142
3143 /* Provide a prototype to silence -Wmissing-prototypes.  */
3144 extern initialize_file_ftype _initialize_xcoffread;
3145
3146 void
3147 _initialize_xcoffread (void)
3148 {
3149   add_symtab_fns (&xcoff_sym_fns);
3150 }