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