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