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