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