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