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