]> Git Repo - binutils.git/blob - gdb/xcoffread.c
* gdb.c++/*.exp: Warn if executable does not exist. The original
[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., 675 Mass Ave, Cambridge, MA 02139, 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 <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 <sys/stat.h>
43 #include <sys/debug.h>
44
45 #include "coff/internal.h"      /* FIXME, internal data from BFD */
46 #include "libcoff.h"            /* FIXME, internal data from BFD */
47 #include "coff/rs6000.h"        /* FIXME, raw file-format guts of xcoff */
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 /* Simplified internal version of coff symbol table information */
63
64 struct coff_symbol {
65   char *c_name;
66   int c_symnum;         /* symbol number of this entry */
67   int c_naux;           /* 0 if syment only, 1 if syment + auxent */
68   long c_value;
69   unsigned char c_sclass;
70   int c_secnum;
71   unsigned int c_type;
72 };
73
74 /* The COFF line table, in raw form.  */
75 static char *linetab = NULL;            /* Its actual contents */
76 static long linetab_offset;             /* Its offset in the file */
77 static unsigned long linetab_size;      /* Its size */
78
79 /* last function's saved coff symbol `cs' */
80
81 static struct coff_symbol fcn_cs_saved;
82
83 static bfd *symfile_bfd;
84
85 /* Core address of start and end of text of current source file.
86    This is calculated from the first function seen after a C_FILE
87    symbol. */
88
89
90 static CORE_ADDR cur_src_end_addr;
91
92 /* Core address of the end of the first object file.  */
93
94 static CORE_ADDR first_object_file_end;
95
96 /* pointer to the string table */
97 static char *strtbl;
98
99 /* length of the string table */
100 static int  strtbl_len;
101
102 /* pointer to debug section */
103 static char *debugsec;
104
105 /* pointer to the a.out symbol table */
106 static char *symtbl;
107
108 /* Number of symbols in symtbl.  */
109 static int symtbl_num_syms;
110
111 /* initial symbol-table-debug-string vector length */
112
113 #define INITIAL_STABVECTOR_LENGTH       40
114
115 /* Nonzero if within a function (so symbols should be local,
116    if nothing says specifically).  */
117
118 int within_function;
119
120 /* Local variables that hold the shift and mask values for the
121    COFF file that we are currently reading.  These come back to us
122    from BFD, and are referenced by their macro names, as well as
123    internally to the BTYPE, ISPTR, ISFCN, ISARY, ISTAG, and DECREF
124    macros from ../internalcoff.h .  */
125
126 static unsigned local_n_btshft;
127 static unsigned local_n_tmask;
128
129 #undef  N_BTSHFT
130 #define N_BTSHFT        local_n_btshft
131 #undef  N_TMASK
132 #define N_TMASK         local_n_tmask
133  
134 /* Local variables that hold the sizes in the file of various COFF structures.
135    (We only need to know this to read them from the file -- BFD will then
136    translate the data in them, into `internal_xxx' structs in the right
137    byte order, alignment, etc.)  */
138
139 static unsigned local_symesz;
140
141 struct coff_symfile_info {
142   file_ptr min_lineno_offset;           /* Where in file lowest line#s are */
143   file_ptr max_lineno_offset;           /* 1+last byte of line#s in file */
144 };
145
146 static struct complaint rsym_complaint = 
147   {"Non-stab C_RSYM `%s' needs special handling", 0, 0};
148
149 static struct complaint storclass_complaint =
150   {"Unexpected storage class: %d", 0, 0};
151
152 static struct complaint bf_notfound_complaint =
153   {"line numbers off, `.bf' symbol not found", 0, 0};
154
155 static void
156 enter_line_range PARAMS ((struct subfile *, unsigned, unsigned,
157                           CORE_ADDR, CORE_ADDR, unsigned *));
158
159 static void
160 free_debugsection PARAMS ((void));
161
162 static int
163 init_debugsection PARAMS ((bfd *));
164
165 static int
166 init_stringtab PARAMS ((bfd *, file_ptr, struct objfile *));
167
168 static void
169 xcoff_symfile_init PARAMS ((struct objfile *));
170
171 static void
172 xcoff_new_init PARAMS ((struct objfile *));
173
174 static void
175 xcoff_symfile_read PARAMS ((struct objfile *, struct section_offsets *, int));
176
177 static void
178 xcoff_symfile_finish PARAMS ((struct objfile *));
179
180 static struct section_offsets *
181 xcoff_symfile_offsets PARAMS ((struct objfile *, CORE_ADDR));
182
183 static int
184 init_lineno PARAMS ((bfd *, file_ptr, int));
185
186 static void
187 free_linetab PARAMS ((void));
188
189 static void
190 find_linenos PARAMS ((bfd *, sec_ptr, PTR));
191
192 static char *
193 coff_getfilename PARAMS ((union internal_auxent *));
194
195 static void
196 read_symbol PARAMS ((struct internal_syment *, int));
197
198 static int
199 read_symbol_lineno PARAMS ((int));
200
201 static int
202 read_symbol_nvalue PARAMS ((int));
203
204 static struct symbol *
205 process_xcoff_symbol PARAMS ((struct coff_symbol *, struct objfile *));
206
207 static void
208 read_xcoff_symtab PARAMS ((struct objfile *, int));
209
210 static void
211 add_stab_to_list PARAMS ((char *, struct pending_stabs **));
212
213 \f
214 #ifdef STATIC_NODEBUG_VARS
215 /* Return the section_offsets* that CS points to.  */
216 static int cs_to_section PARAMS ((struct coff_symbol *, struct objfile *));
217
218 struct find_targ_sec_arg {
219   int targ_index;
220   int *resultp;
221 };
222
223 static void find_targ_sec PARAMS ((bfd *, asection *, void *));
224
225 static void find_targ_sec (abfd, sect, obj)
226      bfd *abfd;
227      asection *sect;
228      PTR obj;
229 {
230   struct find_targ_sec_arg *args = (struct find_targ_sec_arg *)obj;
231   if (sect->target_index == args->targ_index)
232     {
233       /* This is the section.  Figure out what SECT_OFF_* code it is.  */
234       if (bfd_get_section_flags (abfd, sect) & SEC_CODE)
235         *args->resultp = SECT_OFF_TEXT;
236       else if (bfd_get_section_flags (abfd, sect) & SEC_LOAD)
237         *args->resultp = SECT_OFF_DATA;
238       else
239         *args->resultp = SECT_OFF_BSS;
240     }
241 }
242
243 /* Return the section number (SECT_OFF_*) that CS points to.  */
244 static int
245 cs_to_section (cs, objfile)
246      struct coff_symbol *cs;
247      struct objfile *objfile;
248 {
249   int off = SECT_OFF_TEXT;
250   struct find_targ_sec_arg args;
251   args.targ_index = cs->c_secnum;
252   args.resultp = &off;
253   bfd_map_over_sections (objfile->obfd, find_targ_sec, &args);
254   return off;
255 }
256 #endif /* STATIC_NODEBUG_VARS */
257 \f
258 /* add a given stab string into given stab vector. */
259
260 static void
261 add_stab_to_list (stabname, stabvector)
262 char *stabname;
263 struct pending_stabs **stabvector;
264 {
265   if ( *stabvector == NULL) {
266     *stabvector = (struct pending_stabs *)
267         xmalloc (sizeof (struct pending_stabs) + 
268                         INITIAL_STABVECTOR_LENGTH * sizeof (char*));
269     (*stabvector)->count = 0;
270     (*stabvector)->length = INITIAL_STABVECTOR_LENGTH;
271   }
272   else if ((*stabvector)->count >= (*stabvector)->length) {
273     (*stabvector)->length += INITIAL_STABVECTOR_LENGTH;
274     *stabvector = (struct pending_stabs *)
275         xrealloc ((char *) *stabvector, sizeof (struct pending_stabs) + 
276         (*stabvector)->length * sizeof (char*));
277   }
278   (*stabvector)->stab [(*stabvector)->count++] = stabname;
279 }
280 \f
281 /* Linenos are processed on a file-by-file basis.
282
283    Two reasons:
284
285     1) xlc (IBM's native c compiler) postpones static function code
286        emission to the end of a compilation unit. This way it can
287        determine if those functions (statics) are needed or not, and
288        can do some garbage collection (I think). This makes line
289        numbers and corresponding addresses unordered, and we end up
290        with a line table like:
291        
292
293                 lineno  addr
294         foo()     10    0x100
295                   20    0x200
296                   30    0x300
297
298         foo3()    70    0x400
299                   80    0x500
300                   90    0x600
301
302         static foo2()
303                   40    0x700
304                   50    0x800
305                   60    0x900           
306
307         and that breaks gdb's binary search on line numbers, if the
308         above table is not sorted on line numbers. And that sort
309         should be on function based, since gcc can emit line numbers
310         like:
311         
312                 10      0x100   - for the init/test part of a for stmt.
313                 20      0x200
314                 30      0x300
315                 10      0x400   - for the increment part of a for stmt.
316
317         arrange_linetable() will do this sorting.               
318
319      2) aix symbol table might look like:
320
321                 c_file          // beginning of a new file
322                 .bi             // beginning of include file
323                 .ei             // end of include file
324                 .bi
325                 .ei
326
327         basically, .bi/.ei pairs do not necessarily encapsulate
328         their scope. They need to be recorded, and processed later
329         on when we come the end of the compilation unit.
330         Include table (inclTable) and process_linenos() handle
331         that.  */
332
333 /* compare line table entry addresses. */
334
335 static int
336 compare_lte (lte1, lte2)
337      struct linetable_entry *lte1, *lte2;
338 {
339   return lte1->pc - lte2->pc;
340 }
341
342 /* Give a line table with function entries are marked, arrange its functions
343    in assending order and strip off function entry markers and return it in
344    a newly created table. If the old one is good enough, return the old one. */
345 /* FIXME: I think all this stuff can be replaced by just passing
346    sort_linevec = 1 to end_symtab.  */
347
348 static struct linetable *
349 arrange_linetable (oldLineTb)
350   struct linetable *oldLineTb;                  /* old linetable */
351 {
352   int ii, jj, 
353       newline,                                  /* new line count */
354       function_count;                           /* # of functions */
355
356   struct linetable_entry *fentry;               /* function entry vector */
357   int fentry_size;                              /* # of function entries */
358   struct linetable *newLineTb;                  /* new line table */
359
360 #define NUM_OF_FUNCTIONS 20
361
362   fentry_size = NUM_OF_FUNCTIONS;
363   fentry = (struct linetable_entry*)
364     xmalloc (fentry_size * sizeof (struct linetable_entry));
365
366   for (function_count=0, ii=0; ii <oldLineTb->nitems; ++ii) {
367
368     if (oldLineTb->item[ii].line == 0) {        /* function entry found. */
369
370       if (function_count >= fentry_size) {      /* make sure you have room. */
371         fentry_size *= 2;
372         fentry = (struct linetable_entry*) 
373           xrealloc (fentry, fentry_size * sizeof (struct linetable_entry));
374       }
375       fentry[function_count].line = ii;
376       fentry[function_count].pc = oldLineTb->item[ii].pc;
377       ++function_count;
378     }
379   }
380
381   if (function_count == 0) {
382     free (fentry);
383     return oldLineTb;
384   }
385   else if (function_count > 1)
386     qsort (fentry, function_count, sizeof(struct linetable_entry), compare_lte);
387
388   /* allocate a new line table. */
389   newLineTb = (struct linetable *)
390     xmalloc
391       (sizeof (struct linetable) + 
392        (oldLineTb->nitems - function_count) * sizeof (struct linetable_entry));
393
394   /* if line table does not start with a function beginning, copy up until
395      a function begin. */
396
397   newline = 0;
398   if (oldLineTb->item[0].line != 0)
399     for (newline=0; 
400         newline < oldLineTb->nitems && oldLineTb->item[newline].line; ++newline)
401       newLineTb->item[newline] = oldLineTb->item[newline];
402
403   /* Now copy function lines one by one. */
404
405   for (ii=0; ii < function_count; ++ii) {
406     for (jj = fentry[ii].line + 1;
407                  jj < oldLineTb->nitems && oldLineTb->item[jj].line != 0; 
408                                                          ++jj, ++newline)
409       newLineTb->item[newline] = oldLineTb->item[jj];
410   }
411   free (fentry);
412   newLineTb->nitems = oldLineTb->nitems - function_count;
413   return newLineTb;  
414 }     
415
416
417
418 /* We try to detect the beginning of a compilation unit. That info will
419    be used as an entry in line number recording routines (enter_line_range) */
420
421 static unsigned first_fun_line_offset;
422 static unsigned first_fun_bf;
423
424 #define mark_first_line(OFFSET, SYMNUM) \
425   if (!first_fun_line_offset) {         \
426     first_fun_line_offset = OFFSET;     \
427     first_fun_bf = SYMNUM;              \
428   }
429   
430
431 /* include file support: C_BINCL/C_EINCL pairs will be kept in the 
432    following `IncludeChain'. At the end of each symtab (end_symtab),
433    we will determine if we should create additional symtab's to
434    represent if (the include files. */
435
436
437 typedef struct _inclTable {
438   char          *name;                          /* include filename */
439
440   /* Offsets to the line table.  end points to the last entry which is
441      part of this include file.  */
442   int           begin, end;
443   
444   struct subfile *subfile;
445   unsigned      funStartLine;                   /* start line # of its function */
446 } InclTable;
447
448 #define INITIAL_INCLUDE_TABLE_LENGTH    20
449 static InclTable  *inclTable;                   /* global include table */
450 static int        inclIndx;                     /* last entry to table */
451 static int        inclLength;                   /* table length */
452 static int        inclDepth;                    /* nested include depth */
453
454 static void allocate_include_entry PARAMS ((void));
455
456 static void
457 record_include_begin (cs)
458 struct coff_symbol *cs;
459 {
460   if (inclDepth)
461     {
462       /* In xcoff, we assume include files cannot be nested (not in .c files
463          of course, but in corresponding .s files.).  */
464
465       /* This can happen with old versions of GCC.
466          GCC 2.3.3-930426 does not exhibit this on a test case which
467          a user said produced the message for him.  */
468       static struct complaint msg = {"Nested C_BINCL symbols", 0, 0};
469       complain (&msg);
470     }
471   ++inclDepth;
472
473   allocate_include_entry ();
474
475   inclTable [inclIndx].name  = cs->c_name;
476   inclTable [inclIndx].begin = cs->c_value;
477 }
478
479 static void
480 record_include_end (cs)
481 struct coff_symbol *cs;
482 {
483   InclTable *pTbl;  
484
485   if (inclDepth == 0)
486     {
487       static struct complaint msg = {"Mismatched C_BINCL/C_EINCL pair", 0, 0};
488       complain (&msg);
489     }
490
491   allocate_include_entry ();
492
493   pTbl = &inclTable [inclIndx];
494   pTbl->end = cs->c_value;
495
496   --inclDepth;
497   ++inclIndx;
498 }
499
500 static void
501 allocate_include_entry ()
502 {
503   if (inclTable == NULL)
504     {
505       inclTable = (InclTable *) 
506         xmalloc (sizeof (InclTable) * INITIAL_INCLUDE_TABLE_LENGTH);
507       memset (inclTable,
508               '\0', sizeof (InclTable) * INITIAL_INCLUDE_TABLE_LENGTH);
509       inclLength = INITIAL_INCLUDE_TABLE_LENGTH;
510       inclIndx = 0;
511     }
512   else if (inclIndx >= inclLength)
513     {
514       inclLength += INITIAL_INCLUDE_TABLE_LENGTH;
515       inclTable = (InclTable *) 
516         xrealloc (inclTable, sizeof (InclTable) * inclLength);
517       memset (inclTable + inclLength - INITIAL_INCLUDE_TABLE_LENGTH, 
518               '\0', sizeof (InclTable)*INITIAL_INCLUDE_TABLE_LENGTH);
519     }
520 }
521
522 /* given the start and end addresses of a compilation unit (or a csect,
523    at times) process its lines and create appropriate line vectors. */
524
525 static void
526 process_linenos (start, end)
527   CORE_ADDR start, end;
528 {
529   char *pp;
530   int offset, ii;
531
532   /* subfile structure for the main compilation unit.  */
533   struct subfile main_subfile;
534
535   /* In the main source file, any time we see a function entry, we
536      reset this variable to function's absolute starting line number.
537      All the following line numbers in the function are relative to
538      this, and we record absolute line numbers in record_line().  */
539
540   int main_source_baseline = 0;
541
542   unsigned *firstLine;
543   CORE_ADDR addr;
544
545   if (!(offset = first_fun_line_offset))
546     goto return_after_cleanup;
547
548   memset (&main_subfile, '\0', sizeof (main_subfile));
549   first_fun_line_offset = 0;
550
551   if (inclIndx == 0)
552     /* All source lines were in the main source file. None in include files. */
553
554     enter_line_range (&main_subfile, offset, 0, start, end, 
555                                                 &main_source_baseline);
556
557   else
558     {
559       /* There was source with line numbers in include files.  */
560       main_source_baseline = 0;
561       for (ii=0; ii < inclIndx; ++ii)
562         {
563           struct subfile *tmpSubfile;
564
565           /* If there is main file source before include file, enter it.  */
566           if (offset < inclTable[ii].begin)
567             {
568               enter_line_range
569                 (&main_subfile, offset, inclTable[ii].begin - LINESZ,
570                  start, 0, &main_source_baseline);
571             }
572
573           /* Have a new subfile for the include file.  */
574
575           tmpSubfile = inclTable[ii].subfile =
576             (struct subfile *) xmalloc (sizeof (struct subfile));
577
578           memset (tmpSubfile, '\0', sizeof (struct subfile));
579           firstLine = &(inclTable[ii].funStartLine);
580
581           /* Enter include file's lines now.  */
582           enter_line_range (tmpSubfile, inclTable[ii].begin, 
583                             inclTable[ii].end, start, 0, firstLine);
584
585           offset = inclTable[ii].end + LINESZ;
586         }
587
588       /* All the include files' line have been processed at this point.  Now,
589          enter remaining lines of the main file, if any left.  */
590       if (offset < (linetab_offset + linetab_size + 1 - LINESZ))
591         {
592           enter_line_range (&main_subfile, offset, 0, start, end, 
593                             &main_source_baseline);
594         }
595     }
596
597   /* Process main file's line numbers.  */
598   if (main_subfile.line_vector)
599     {
600       struct linetable *lineTb, *lv;
601
602       lv = main_subfile.line_vector;
603
604       /* Line numbers are not necessarily ordered. xlc compilation will
605          put static function to the end. */
606
607       lineTb = arrange_linetable (lv);
608       if (lv == lineTb)
609         {
610           current_subfile->line_vector = (struct linetable *)
611             xrealloc (lv, (sizeof (struct linetable)
612                            + lv->nitems * sizeof (struct linetable_entry)));
613         }
614       else
615         {
616           free (lv);
617           current_subfile->line_vector = lineTb;
618         }
619
620       current_subfile->line_vector_length = 
621         current_subfile->line_vector->nitems;
622     }
623
624   /* Now, process included files' line numbers.  */
625
626   for (ii=0; ii < inclIndx; ++ii)
627     {
628       if ((inclTable[ii].subfile)->line_vector) /* Useless if!!! FIXMEmgo */
629         {
630           struct linetable *lineTb, *lv;
631
632           lv = (inclTable[ii].subfile)->line_vector;
633
634           /* Line numbers are not necessarily ordered. xlc compilation will
635              put static function to the end. */
636
637           lineTb = arrange_linetable (lv);
638
639           push_subfile ();
640
641           /* For the same include file, we might want to have more than one
642              subfile.  This happens if we have something like:
643
644                 ......
645                 #include "foo.h"
646                 ......
647                 #include "foo.h"
648                 ......
649
650              while foo.h including code in it. (stupid but possible)
651              Since start_subfile() looks at the name and uses an
652              existing one if finds, we need to provide a fake name and
653              fool it.  */
654
655 #if 0
656           start_subfile (inclTable[ii].name, (char*)0);
657 #else
658           {
659             /* Pick a fake name that will produce the same results as this
660                one when passed to deduce_language_from_filename.  Kludge on
661                top of kludge.  */
662             char *fakename = strrchr (inclTable[ii].name, '.');
663             if (fakename == NULL)
664               fakename = " ?";
665             start_subfile (fakename, (char*)0);
666             free (current_subfile->name);
667           }
668           current_subfile->name = strdup (inclTable[ii].name);
669 #endif
670
671           if (lv == lineTb)
672             {
673               current_subfile->line_vector =
674                 (struct linetable *) xrealloc
675                   (lv, (sizeof (struct linetable)
676                         + lv->nitems * sizeof (struct linetable_entry)));
677
678             }
679           else
680             {
681               free (lv);
682               current_subfile->line_vector = lineTb;
683             }
684
685           current_subfile->line_vector_length = 
686             current_subfile->line_vector->nitems;
687           start_subfile (pop_subfile (), (char*)0);
688         }
689     }
690
691  return_after_cleanup:
692
693   /* We don't want to keep alloc/free'ing the global include file table.  */
694   inclIndx = 0;
695
696   /* Start with a fresh subfile structure for the next file.  */
697   memset (&main_subfile, '\0', sizeof (struct subfile));
698 }
699
700 void
701 aix_process_linenos ()
702 {
703   /* process line numbers and enter them into line vector */
704   process_linenos (last_source_start_addr, cur_src_end_addr);
705 }
706
707
708 /* Enter a given range of lines into the line vector.
709    can be called in the following two ways:
710      enter_line_range (subfile, beginoffset, endoffset, startaddr, 0, firstLine)  or
711      enter_line_range (subfile, beginoffset, 0, startaddr, endaddr, firstLine)
712
713    endoffset points to the last line table entry that we should pay
714    attention to.  */
715
716 static void
717 enter_line_range (subfile, beginoffset, endoffset, startaddr, endaddr, firstLine)
718   struct subfile *subfile;
719   unsigned   beginoffset, endoffset;    /* offsets to line table */
720   CORE_ADDR  startaddr, endaddr;
721   unsigned   *firstLine;
722 {
723   char          *pp, *limit;
724   CORE_ADDR     addr;
725
726 /* Do Byte swapping, if needed. FIXME! */
727 #define P_LINENO(PP)  (*(unsigned short*)((struct external_lineno*)(PP))->l_lnno)
728 #define P_LINEADDR(PP)  (*(long*)((struct external_lineno*)(PP))->l_addr.l_paddr)
729 #define P_LINESYM(PP)       (*(long*)((struct external_lineno*)(PP))->l_addr.l_symndx)
730
731   pp = &linetab [beginoffset - linetab_offset];
732   if (endoffset != 0 && endoffset - linetab_offset >= linetab_size)
733     {
734       static struct complaint msg =
735         {"Bad line table offset in C_EINCL directive", 0, 0};
736       complain (&msg);
737       return;
738     }
739   limit = endoffset ? &linetab [endoffset - linetab_offset]
740                       : &linetab [linetab_size -1];
741
742   while (pp <= limit) {
743
744     /* find the address this line represents */
745     addr = P_LINENO(pp) ? 
746       P_LINEADDR(pp) : read_symbol_nvalue (P_LINESYM(pp)); 
747
748     if (addr < startaddr || (endaddr && addr >= endaddr))
749       return;
750
751     if (P_LINENO(pp) == 0) {
752       *firstLine = read_symbol_lineno (P_LINESYM(pp));
753       record_line (subfile, 0, addr);
754       --(*firstLine);
755     }
756     else
757       record_line (subfile, *firstLine + P_LINENO(pp), addr);
758
759     pp += LINESZ;
760   }
761 }
762
763 typedef struct {
764   int fsize;                            /* file size */
765   int fixedparms;                       /* number of fixed parms */
766   int floatparms;                       /* number of float parms */
767   unsigned int parminfo;                /* parameter info. 
768                                            See /usr/include/sys/debug.h
769                                            tbtable_ext.parminfo */
770   int framesize;                        /* function frame size */
771 } TracebackInfo;
772
773
774 /* Given a function symbol, return its traceback information. */
775
776   TracebackInfo *
777 retrieve_tracebackinfo (abfd, textsec, cs)
778   bfd *abfd;
779   sec_ptr textsec;
780   struct coff_symbol *cs;
781 {
782 #define TBTABLE_BUFSIZ  2000
783
784   static TracebackInfo tbInfo;
785   struct tbtable *ptb;
786
787   static char buffer [TBTABLE_BUFSIZ];
788
789   int  *pinsn;
790   int  bytesread=0;                     /* total # of bytes read so far */
791   int  bufferbytes;                     /* number of bytes in the buffer */
792
793   int functionstart = cs->c_value - textsec->vma;
794
795   memset (&tbInfo, '\0', sizeof (tbInfo));
796
797   /* keep reading blocks of data from the text section, until finding a zero
798      word and a traceback table. */
799
800   /* Note: The logical thing way to write this code would be to assign
801      to bufferbytes within the while condition.  But that triggers a
802      compiler (xlc in AIX 3.2) bug, so simplify it...  */
803   bufferbytes = 
804     (TBTABLE_BUFSIZ < (textsec->_raw_size - functionstart - bytesread) ? 
805      TBTABLE_BUFSIZ : (textsec->_raw_size - functionstart - bytesread));
806   while (bufferbytes 
807          && (bfd_get_section_contents
808              (abfd, textsec, buffer, 
809               (file_ptr)(functionstart + bytesread), bufferbytes)))
810   {
811     bytesread += bufferbytes;
812     pinsn = (int*) buffer;
813
814     /* if this is the first time we filled the buffer, retrieve function
815        framesize info. */
816
817     if (bytesread == bufferbytes) {
818
819       /* skip over unrelated instructions */
820
821       if (*pinsn == 0x7c0802a6)                 /* mflr r0 */
822         ++pinsn;
823       if ((*pinsn & 0xfc00003e) == 0x7c000026)  /* mfcr Rx */
824         ++pinsn;
825       if ((*pinsn & 0xfc000000) == 0x48000000)  /* bl foo, save fprs */
826         ++pinsn;
827       if ((*pinsn  & 0xfc1f0000) == 0xbc010000) /* stm Rx, NUM(r1) */
828         ++pinsn;
829
830       do {
831         int tmp = (*pinsn >> 16) & 0xffff;
832
833         if (tmp ==  0x9421) {                   /* stu  r1, NUM(r1) */
834           tbInfo.framesize = 0x10000 - (*pinsn & 0xffff);
835           break;
836         }
837         else if ((*pinsn == 0x93e1fffc) ||      /* st   r31,-4(r1) */
838                  (tmp == 0x9001))               /* st   r0, NUM(r1) */
839         ;
840         /* else, could not find a frame size. */
841         else
842           return NULL;
843
844       } while (++pinsn && *pinsn);
845
846       if (!tbInfo.framesize)
847         return NULL;      
848
849     }
850
851     /* look for a zero word. */
852
853     while (*pinsn && (pinsn < (int*)(buffer + bufferbytes - sizeof(int))))
854       ++pinsn;
855
856     if (pinsn >= (int*)(buffer + bufferbytes))
857       continue;
858
859     if (*pinsn == 0) {
860
861       /* function size is the amount of bytes we have skipped so far. */
862       tbInfo.fsize = bytesread - (buffer + bufferbytes - (char*)pinsn);
863
864       ++pinsn;
865
866       /* if we don't have the whole traceback table in the buffer, re-read
867          the whole thing. */
868
869       /* This is how much to read to get the traceback table.
870          8 bytes of the traceback table are always present, plus we
871          look at parminfo.  */
872 #define MIN_TBTABSIZ    12
873                                 
874       if ((char*)pinsn > (buffer + bufferbytes - MIN_TBTABSIZ)) {
875
876         /* In case if we are *very* close to the end of the text section
877            and cannot read properly from that point on, abort by returning
878            NULL.
879
880            This could happen if the traceback table is only 8 bytes,
881            but we try to read 12 bytes of it.
882            Handle this case more graciously -- FIXME */
883
884         if (!bfd_get_section_contents (
885                 abfd, textsec, buffer, 
886                 (file_ptr)(functionstart + 
887                  bytesread - (buffer + bufferbytes - (char*)pinsn)),MIN_TBTABSIZ))
888           { printf_unfiltered ("Abnormal return!..\n"); return NULL; }
889
890         ptb = (struct tbtable *)buffer;
891       }
892       else
893         ptb = (struct tbtable *)pinsn;
894
895       tbInfo.fixedparms = ptb->tb.fixedparms;
896       tbInfo.floatparms = ptb->tb.floatparms;
897       tbInfo.parminfo = ptb->tb_ext.parminfo;
898       return &tbInfo;
899     }
900     bufferbytes = 
901       (TBTABLE_BUFSIZ < (textsec->_raw_size - functionstart - bytesread) ? 
902        TBTABLE_BUFSIZ : (textsec->_raw_size - functionstart - bytesread));
903   }
904   return NULL;
905 }
906
907 #if 0
908 /* Given a function symbol, return a pointer to its traceback table. */
909
910   struct tbtable *
911 retrieve_traceback (abfd, textsec, cs, size)
912   bfd *abfd;
913   sec_ptr textsec;
914   struct coff_symbol *cs;
915   int *size;                            /* return function size */
916 {
917 #define TBTABLE_BUFSIZ  2000
918 #define MIN_TBTABSIZ    50              /* minimum buffer size to hold a
919                                            traceback table. */
920
921   static char buffer [TBTABLE_BUFSIZ];
922
923   int  *pinsn;
924   int  bytesread=0;                     /* total # of bytes read so far */
925   int  bufferbytes;                     /* number of bytes in the buffer */
926
927   int functionstart = cs->c_value - textsec->filepos + textsec->vma;
928   *size = 0;
929
930   /* keep reading blocks of data from the text section, until finding a zero
931      word and a traceback table. */
932
933   while (bfd_get_section_contents (abfd, textsec, buffer, 
934         (file_ptr)(functionstart + bytesread), 
935         bufferbytes = (
936                 (TBTABLE_BUFSIZ < (textsec->size - functionstart - bytesread)) ? 
937                  TBTABLE_BUFSIZ : (textsec->size - functionstart - bytesread))))
938   {
939     bytesread += bufferbytes;
940     pinsn = (int*) buffer;
941
942     /* look for a zero word. */
943
944     while (*pinsn && (pinsn < (int*)(buffer + bufferbytes - sizeof(int))))
945       ++pinsn;
946
947     if (pinsn >= (int*)(buffer + bufferbytes))
948       continue;
949
950     if (*pinsn == 0) {
951
952       /* function size is the amount of bytes we have skipped so far. */
953       *size = bytesread - (buffer + bufferbytes - pinsn);
954
955       ++pinsn;
956
957       /* if we don't have the whole traceback table in the buffer, re-read
958          the whole thing. */
959
960       if ((char*)pinsn > (buffer + bufferbytes - MIN_TBTABSIZ)) {
961
962         /* In case if we are *very* close to the end of the text section
963            and cannot read properly from that point on, abort for now.
964            Handle this case more graciously -- FIXME */
965
966         if (!bfd_get_section_contents (
967                 abfd, textsec, buffer, 
968                 (file_ptr)(functionstart + 
969                  bytesread - (buffer + bufferbytes - pinsn)),MIN_TBTABSIZ))
970         /*   abort (); */ { printf_unfiltered ("abort!!!\n"); return NULL; }
971
972         return (struct tbtable *)buffer;
973       }
974       else
975         return (struct tbtable *)pinsn;
976     }
977   }
978   return NULL;
979 }
980 #endif /* 0 */
981
982
983
984
985 /* Save the vital information for use when closing off the current file.
986    NAME is the file name the symbols came from, START_ADDR is the first
987    text address for the file, and SIZE is the number of bytes of text.  */
988
989 #define complete_symtab(name, start_addr) {     \
990   last_source_file = savestring (name, strlen (name));  \
991   last_source_start_addr = start_addr;                  \
992 }
993
994
995 /* Refill the symbol table input buffer
996    and set the variables that control fetching entries from it.
997    Reports an error if no data available.
998    This function can read past the end of the symbol table
999    (into the string table) but this does no harm.  */
1000
1001 /* Reading symbol table has to be fast! Keep the followings as macros, rather
1002    than functions. */
1003
1004 #define RECORD_MINIMAL_SYMBOL(NAME, ADDR, TYPE, ALLOCED, SECTION, OBJFILE) \
1005 {                                               \
1006   char *namestr;                                \
1007   namestr = (NAME); \
1008   if (namestr[0] == '.') ++namestr; \
1009   if (!(ALLOCED)) {                             \
1010     (NAME) = namestr =                          \
1011     obstack_copy0 (&objfile->symbol_obstack, namestr, strlen (namestr)); \
1012     (ALLOCED) = 1;                                              \
1013   }                                                             \
1014   prim_record_minimal_symbol_and_info (namestr, (ADDR), (TYPE), \
1015                                        (char *)NULL, (SECTION), (OBJFILE)); \
1016   misc_func_recorded = 1;                                       \
1017 }
1018
1019
1020 /* A parameter template, used by ADD_PARM_TO_PENDING.  It is initialized
1021    in our initializer function at the bottom of the file, to avoid
1022    dependencies on the exact "struct symbol" format.  */
1023
1024 static struct symbol parmsym;
1025
1026 /* Add a parameter to a given pending symbol list. */ 
1027
1028 #define ADD_PARM_TO_PENDING(PARM, VALUE, PTYPE, PENDING_SYMBOLS)        \
1029 {                                                                       \
1030   PARM = (struct symbol *)                                              \
1031       obstack_alloc (&objfile->symbol_obstack, sizeof (struct symbol)); \
1032   *(PARM) = parmsym;                                                    \
1033   SYMBOL_TYPE (PARM) = PTYPE;                                           \
1034   SYMBOL_VALUE (PARM) = VALUE;                                          \
1035   add_symbol_to_list (PARM, &PENDING_SYMBOLS);                          \
1036 }
1037
1038
1039 /* xcoff has static blocks marked in `.bs', `.es' pairs. They cannot be
1040    nested. At any given time, a symbol can only be in one static block.
1041    This is the base address of current static block, zero if non exists. */
1042    
1043 static int static_block_base = 0;
1044
1045 /* Section number for the current static block.  */
1046
1047 static int static_block_section = -1;
1048
1049 /* true if space for symbol name has been allocated. */
1050
1051 static int symname_alloced = 0;
1052
1053 /* Next symbol to read.  Pointer into raw seething symbol table.  */
1054
1055 static char *raw_symbol;
1056
1057 /* This is the function which stabsread.c calls to get symbol
1058    continuations.  */
1059 static char *
1060 xcoff_next_symbol_text ()
1061 {
1062   struct internal_syment symbol;
1063   static struct complaint msg =
1064     {"Unexpected symbol continuation", 0, 0};
1065   char *retval;
1066
1067   bfd_coff_swap_sym_in (current_objfile->obfd, raw_symbol, &symbol);
1068   if (symbol.n_zeroes)
1069     {
1070       complain (&msg);
1071
1072       /* Return something which points to '\0' and hope the symbol reading
1073          code does something reasonable.  */
1074       retval = "";
1075     }
1076   else if (symbol.n_sclass & 0x80)
1077     {
1078       retval = debugsec + symbol.n_offset;
1079       raw_symbol += coff_data (current_objfile->obfd)->local_symesz;
1080       ++symnum;
1081     }
1082   else
1083     {
1084       complain (&msg);
1085
1086       /* Return something which points to '\0' and hope the symbol reading
1087          code does something reasonable.  */
1088       retval = "";
1089     }
1090   return retval;
1091 }
1092
1093 /* read the whole symbol table of a given bfd. */
1094
1095 static void
1096 read_xcoff_symtab (objfile, nsyms)
1097      struct objfile *objfile;   /* Object file we're reading from */
1098      int nsyms;                 /* # of symbols */
1099 {
1100   bfd *abfd = objfile->obfd;
1101   char *raw_auxptr;             /* Pointer to first raw aux entry for sym */
1102   sec_ptr  textsec;             /* Pointer to text section */
1103   TracebackInfo *ptb;           /* Pointer to traceback table */
1104
1105   struct internal_syment symbol[1];
1106   union internal_auxent main_aux;
1107   struct coff_symbol cs[1];
1108   CORE_ADDR file_start_addr = 0;
1109   CORE_ADDR file_end_addr = 0;
1110
1111   int next_file_symnum = -1;
1112   int just_started = 1;
1113   int depth = 0;
1114   int toc_offset = 0;           /* toc offset value in data section. */
1115   int val;
1116   int fcn_last_line;
1117   int fcn_start_addr;
1118   long fcn_line_offset;
1119   size_t size;
1120
1121   struct coff_symbol fcn_stab_saved;
1122
1123   /* fcn_cs_saved is global because process_xcoff_symbol needs it. */
1124   union internal_auxent fcn_aux_saved;
1125   struct type *fcn_type_saved = NULL;
1126   struct context_stack *new;
1127
1128   char *filestring = " _start_ ";       /* Name of the current file. */
1129
1130   char *last_csect_name;                /* last seen csect's name and value */
1131   CORE_ADDR last_csect_val;
1132   int last_csect_sec;
1133   int  misc_func_recorded;              /* true if any misc. function */
1134
1135   current_objfile = objfile;
1136
1137   /* Get the appropriate COFF "constants" related to the file we're
1138      handling. */
1139   N_TMASK = coff_data (abfd)->local_n_tmask;
1140   N_BTSHFT = coff_data (abfd)->local_n_btshft;
1141   local_symesz = coff_data (abfd)->local_symesz;
1142
1143   last_source_file = NULL;
1144   last_csect_name = 0;
1145   last_csect_val = 0;
1146   misc_func_recorded = 0;
1147
1148   start_stabs ();
1149   start_symtab (filestring, (char *)NULL, file_start_addr);
1150   symnum = 0;
1151   first_object_file_end = 0;
1152
1153   /* Allocate space for the entire symbol table at once, and read it
1154      all in.  The bfd is already positioned at the beginning of
1155      the symbol table.  */
1156
1157   size = coff_data (abfd)->local_symesz * nsyms;
1158   symtbl = xmalloc (size);
1159   symtbl_num_syms = nsyms;
1160
1161   val = bfd_read (symtbl, size, 1, abfd);
1162   if (val != size)
1163     perror_with_name ("reading symbol table");
1164
1165   raw_symbol = symtbl;
1166
1167   textsec = bfd_get_section_by_name (abfd, ".text");
1168   if (!textsec)
1169     {
1170       printf_unfiltered ("Unable to locate text section!\n");
1171     }
1172
1173   next_symbol_text_func = xcoff_next_symbol_text;
1174
1175   while (symnum < nsyms)
1176     {
1177
1178       QUIT;                     /* make this command interruptable.  */
1179
1180       /* READ_ONE_SYMBOL (symbol, cs, symname_alloced); */
1181       /* read one symbol into `cs' structure. After processing the
1182          whole symbol table, only string table will be kept in memory,
1183          symbol table and debug section of xcoff will be freed. Thus
1184          we can mark symbols with names in string table as
1185          `alloced'. */
1186       {
1187         int ii;
1188
1189         /* Swap and align the symbol into a reasonable C structure.  */
1190         bfd_coff_swap_sym_in (abfd, raw_symbol, symbol);
1191
1192         cs->c_symnum = symnum;
1193         cs->c_naux = symbol->n_numaux;
1194         if (symbol->n_zeroes)
1195           {
1196             symname_alloced = 0;
1197             /* We must use the original, unswapped, name here so the name field
1198                pointed to by cs->c_name will persist throughout xcoffread.  If
1199                we use the new field, it gets overwritten for each symbol.  */
1200             cs->c_name = ((struct external_syment *)raw_symbol)->e.e_name;
1201             /* If it's exactly E_SYMNMLEN characters long it isn't
1202                '\0'-terminated.  */
1203             if (cs->c_name[E_SYMNMLEN - 1] != '\0')
1204               {
1205                 char *p;
1206                 p = obstack_alloc (&objfile->symbol_obstack, E_SYMNMLEN + 1);
1207                 strncpy (p, cs->c_name, E_SYMNMLEN);
1208                 p[E_SYMNMLEN] = '\0';
1209                 cs->c_name = p;
1210                 symname_alloced = 1;
1211               }
1212           }
1213         else if (symbol->n_sclass & 0x80)
1214           {
1215             cs->c_name = debugsec + symbol->n_offset;
1216             symname_alloced = 0;
1217           }
1218         else
1219           {
1220             /* in string table */
1221             cs->c_name = strtbl + (int)symbol->n_offset;
1222             symname_alloced = 1;
1223           }
1224         cs->c_value = symbol->n_value;
1225         cs->c_sclass = symbol->n_sclass;
1226         cs->c_secnum = symbol->n_scnum;
1227         cs->c_type = (unsigned)symbol->n_type;
1228
1229         raw_symbol += coff_data (abfd)->local_symesz;
1230         ++symnum;
1231
1232         /* Save addr of first aux entry.  */
1233         raw_auxptr = raw_symbol;
1234
1235         /* Skip all the auxents associated with this symbol.  */
1236         for (ii = symbol->n_numaux; ii; --ii)
1237           {
1238             raw_symbol += coff_data (abfd)->local_auxesz;
1239             ++symnum;
1240           }
1241       }
1242
1243       /* if symbol name starts with ".$" or "$", ignore it. */
1244       if (cs->c_name[0] == '$'
1245           || (cs->c_name[1] == '$' && cs->c_name[0] == '.'))
1246         continue;
1247
1248       if (cs->c_symnum == next_file_symnum && cs->c_sclass != C_FILE)
1249         {
1250           if (last_source_file)
1251             {
1252               end_symtab (cur_src_end_addr, 1, 0, objfile,
1253                           textsec->target_index);
1254               end_stabs ();
1255             }
1256
1257           start_stabs ();
1258           start_symtab ("_globals_", (char *)NULL, (CORE_ADDR)0);
1259           cur_src_end_addr = first_object_file_end;
1260           /* done with all files, everything from here on is globals */
1261         }
1262
1263       /* if explicitly specified as a function, treat is as one. */
1264       if (ISFCN(cs->c_type) && cs->c_sclass != C_TPDEF)
1265         {
1266           bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
1267                                 0, cs->c_naux, &main_aux);
1268           goto function_entry_point;
1269         }
1270
1271       if ((cs->c_sclass == C_EXT || cs->c_sclass == C_HIDEXT)
1272           && cs->c_naux == 1)
1273         {
1274           /* Dealing with a symbol with a csect entry.  */
1275
1276 #define CSECT(PP) ((PP)->x_csect)
1277 #define CSECT_LEN(PP) (CSECT(PP).x_scnlen.l)
1278 #define CSECT_ALIGN(PP) (SMTYP_ALIGN(CSECT(PP).x_smtyp))
1279 #define CSECT_SMTYP(PP) (SMTYP_SMTYP(CSECT(PP).x_smtyp))
1280 #define CSECT_SCLAS(PP) (CSECT(PP).x_smclas)
1281
1282           /* Convert the auxent to something we can access.  */
1283           bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
1284                                 0, cs->c_naux, &main_aux);
1285
1286           switch (CSECT_SMTYP (&main_aux))
1287             {
1288
1289             case XTY_ER:
1290               /* Ignore all external references.  */
1291               continue;
1292
1293             case XTY_SD:
1294               /* A section description.  */
1295               {
1296                 switch (CSECT_SCLAS (&main_aux))
1297                   {
1298
1299                   case XMC_PR:
1300                     {
1301
1302                       /* A program csect is seen.  We have to allocate one
1303                          symbol table for each program csect.  Normally gdb
1304                          prefers one symtab for each source file.  In case
1305                          of AIX, one source file might include more than one
1306                          [PR] csect, and they don't have to be adjacent in
1307                          terms of the space they occupy in memory. Thus, one
1308                          single source file might get fragmented in the
1309                          memory and gdb's file start and end address
1310                          approach does not work!  GCC (and I think xlc) seem
1311                          to put all the code in the unnamed program csect.  */
1312
1313                       if (last_csect_name)
1314                         {
1315
1316                           /* If no misc. function recorded in the last
1317                              seen csect, enter it as a function. This
1318                              will take care of functions like strcmp()
1319                              compiled by xlc.  */
1320
1321                           if (!misc_func_recorded)
1322                             {
1323                               int alloced = 0;
1324                               RECORD_MINIMAL_SYMBOL
1325                                 (last_csect_name, last_csect_val,
1326                                  mst_text, alloced, last_csect_sec,
1327                                  objfile);
1328                             }
1329
1330                           complete_symtab (filestring, file_start_addr);
1331                           cur_src_end_addr = file_end_addr;
1332                           end_symtab (file_end_addr, 1, 0, objfile,
1333                                       textsec->target_index);
1334                           end_stabs ();
1335                           start_stabs ();
1336                           /* Give all csects for this source file the same
1337                              name.  */
1338                           start_symtab (filestring, NULL, (CORE_ADDR)0);
1339                         }
1340
1341                       /* If this is the very first csect seen,
1342                          basically `__start'. */
1343                       if (just_started)
1344                         {
1345                           first_object_file_end
1346                             = cs->c_value + CSECT_LEN (&main_aux);
1347                           just_started = 0;
1348                         }
1349
1350                       file_start_addr = cs->c_value;
1351                       file_end_addr = cs->c_value + CSECT_LEN (&main_aux);
1352
1353                       if (cs->c_name && cs->c_name[0] == '.')
1354                         {
1355                           last_csect_name = cs->c_name;
1356                           last_csect_val = cs->c_value;
1357                           last_csect_sec = cs->c_secnum;
1358                         }
1359                     }
1360                     misc_func_recorded = 0;
1361                     continue;
1362
1363                   case XMC_RW :
1364                     break;
1365
1366                     /* If the section is not a data description,
1367                        ignore it. Note that uninitialized data will
1368                        show up as XTY_CM/XMC_RW pair. */
1369
1370                   case XMC_TC0:
1371                     if (toc_offset)
1372                       warning ("More than one xmc_tc0 symbol found.");
1373                     toc_offset = cs->c_value;
1374                     continue;
1375
1376                   case XMC_TC:
1377 #ifdef STATIC_NODEBUG_VARS
1378                     /* We need to process these symbols if they are C_HIDEXT,
1379                        for static variables in files compiled without -g.  */
1380                     if (cs->c_sclass == C_HIDEXT)
1381                       break;
1382                     else
1383 #endif
1384                       continue;
1385
1386                   default:
1387                     /* Ignore the symbol.  */
1388                     continue;
1389                   }
1390               }
1391               break;
1392
1393             case XTY_LD:
1394
1395               switch (CSECT_SCLAS (&main_aux))
1396                 {
1397                 case XMC_PR:
1398                   /* a function entry point. */
1399                 function_entry_point:
1400                   RECORD_MINIMAL_SYMBOL (cs->c_name, cs->c_value, mst_text, 
1401                                          symname_alloced, cs->c_secnum,
1402                                          objfile);
1403
1404                   fcn_line_offset = main_aux.x_sym.x_fcnary.x_fcn.x_lnnoptr;
1405                   fcn_start_addr = cs->c_value;
1406
1407                   /* save the function header info, which will be used
1408                      when `.bf' is seen. */
1409                   fcn_cs_saved = *cs;
1410                   fcn_aux_saved = main_aux;
1411
1412                   ptb = NULL;
1413
1414                   /* If function has two auxent, then debugging information is
1415                      already available for it. Process traceback table for
1416                      functions with only one auxent. */
1417
1418                   if (cs->c_naux == 1)
1419                     ptb = retrieve_tracebackinfo (abfd, textsec, cs);
1420
1421                   else if (cs->c_naux != 2)
1422                     {
1423                       static struct complaint msg =
1424                         {"Expected one or two auxents for function", 0, 0};
1425                       complain (&msg);
1426                     }
1427
1428                   /* If there is traceback info, create and add parameters
1429                      for it. */
1430
1431                   if (ptb && (ptb->fixedparms || ptb->floatparms))
1432                     {
1433
1434                       int parmcnt = ptb->fixedparms + ptb->floatparms;
1435                       char *parmcode = (char*) &ptb->parminfo;
1436
1437                       /* The link area is 0x18 bytes.  */
1438                       int parmvalue = ptb->framesize + 0x18;
1439                       unsigned int ii, mask;
1440
1441                       for (ii=0, mask = 0x80000000; ii <parmcnt; ++ii)
1442                         {
1443                           struct symbol *parm;
1444
1445                           if (ptb->parminfo & mask)
1446                             {
1447                               /* float or double */
1448                               mask = mask >> 1;
1449                               if (ptb->parminfo & mask)
1450                                 {
1451                                   /* double parm */
1452                                   ADD_PARM_TO_PENDING
1453                                     (parm, parmvalue, builtin_type_double,
1454                                      local_symbols);
1455                                   parmvalue += sizeof (double);
1456                                 }
1457                               else
1458                                 {
1459                                   /* float parm */
1460                                   ADD_PARM_TO_PENDING
1461                                     (parm, parmvalue, builtin_type_float,
1462                                      local_symbols);
1463                                   parmvalue += sizeof (float);
1464                                 }
1465                             }
1466                           else
1467                             {
1468                               static struct type *intparm_type;
1469                               if (intparm_type == NULL)
1470                                 {
1471
1472                                   /* Create a type, which is a pointer
1473                                      type (a kludge to make it print
1474                                      in hex), but which has a name
1475                                      indicating we don't know the real
1476                                      type.  */
1477
1478                                   intparm_type =
1479                                     init_type
1480                                       (TYPE_CODE_PTR,
1481                                        TARGET_PTR_BIT / HOST_CHAR_BIT,
1482                                        0,
1483                                        "<non-float parameter>",
1484                                        NULL);
1485                                   TYPE_TARGET_TYPE (intparm_type) =
1486                                     builtin_type_void;
1487                                 }
1488                               ADD_PARM_TO_PENDING
1489                                 (parm, parmvalue,
1490                                  intparm_type,
1491                                  local_symbols);
1492                               parmvalue += sizeof (int);
1493                             }
1494                           mask = mask >> 1;
1495                         }
1496                 
1497                       /* Fake this as a function.  Needed in
1498                          process_xcoff_symbol().  */
1499                       cs->c_type = 32;
1500
1501                       finish_block
1502                         (process_xcoff_symbol (cs, objfile), &local_symbols, 
1503                          pending_blocks, cs->c_value,
1504                          cs->c_value + ptb->fsize, objfile);
1505                     }
1506                   continue;
1507
1508                 case XMC_GL:
1509                   /* shared library function trampoline code entry point. */
1510
1511                   /* record trampoline code entries as
1512                      mst_solib_trampoline symbol.  When we lookup mst
1513                      symbols, we will choose mst_text over
1514                      mst_solib_trampoline. */
1515                   RECORD_MINIMAL_SYMBOL
1516                     (cs->c_name, cs->c_value,
1517                      mst_solib_trampoline,
1518                      symname_alloced, cs->c_secnum, objfile);
1519                   continue;
1520
1521                 case XMC_DS:
1522                   /* The symbols often have the same names as debug symbols for
1523                      functions, and confuse lookup_symbol.  */
1524                   continue;
1525
1526                 default:
1527                   /* xlc puts each variable in a separate csect, so we get
1528                      an XTY_SD for each variable.  But gcc puts several
1529                      variables in a csect, so that each variable only gets
1530                      an XTY_LD.  We still need to record them.  This will
1531                      typically be XMC_RW; I suspect XMC_RO and XMC_BS might
1532                      be possible too.  */
1533                   break;
1534                 }
1535
1536             default:
1537               break;
1538             }
1539         }
1540
1541       switch (cs->c_sclass)
1542         {
1543
1544         case C_FILE:
1545
1546           /* see if the last csect needs to be recorded. */
1547
1548           if (last_csect_name && !misc_func_recorded)
1549             {
1550
1551               /* If no misc. function recorded in the last seen csect, enter
1552                  it as a function.  This will take care of functions like
1553                  strcmp() compiled by xlc.  */
1554
1555               int alloced = 0;
1556               RECORD_MINIMAL_SYMBOL
1557                 (last_csect_name, last_csect_val,
1558                  mst_text, alloced, last_csect_sec, objfile);
1559             }
1560
1561           /* c_value field contains symnum of next .file entry in table
1562              or symnum of first global after last .file. */
1563
1564           next_file_symnum = cs->c_value;
1565
1566           /* Complete symbol table for last object file containing
1567              debugging information. */
1568
1569           /* Whether or not there was a csect in the previous file, we
1570              have to call `end_stabs' and `start_stabs' to reset
1571              type_vector, line_vector, etc. structures.  */
1572
1573           complete_symtab (filestring, file_start_addr);
1574           cur_src_end_addr = file_end_addr;
1575           end_symtab (file_end_addr, 1, 0, objfile, textsec->target_index);
1576           end_stabs ();
1577
1578           /* XCOFF, according to the AIX 3.2 documentation, puts the filename
1579              in cs->c_name.  But xlc 1.3.0.2 has decided to do things the
1580              standard COFF way and put it in the auxent.  We use the auxent if
1581              the symbol is ".file" and an auxent exists, otherwise use the symbol
1582              itself.  Simple enough.  */
1583           if (!strcmp (cs->c_name, ".file") && cs->c_naux > 0)
1584             {
1585               bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
1586                                     0, cs->c_naux, &main_aux);
1587               filestring = coff_getfilename (&main_aux);
1588             }
1589           else
1590             filestring = cs->c_name;
1591
1592           start_stabs ();
1593           start_symtab (filestring, (char *)NULL, (CORE_ADDR)0);
1594           last_csect_name = 0;
1595
1596           /* reset file start and end addresses. A compilation unit with no text
1597              (only data) should have zero file boundaries. */
1598           file_start_addr = file_end_addr = 0;
1599           break;
1600
1601         case C_FUN:
1602           fcn_stab_saved = *cs;
1603           break;
1604
1605         case C_FCN:
1606           if (STREQ (cs->c_name, ".bf"))
1607             {
1608
1609               bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
1610                                     0, cs->c_naux, &main_aux);
1611
1612               within_function = 1;
1613
1614               mark_first_line (fcn_line_offset, cs->c_symnum);
1615
1616               new = push_context (0, fcn_start_addr);
1617
1618               new->name = define_symbol 
1619                 (fcn_cs_saved.c_value, fcn_stab_saved.c_name, 0, 0, objfile);
1620               if (new->name != NULL)
1621                 SYMBOL_SECTION (new->name) = cs->c_secnum;
1622             }
1623           else if (STREQ (cs->c_name, ".ef"))
1624             {
1625
1626               bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
1627                                     0, cs->c_naux, &main_aux);
1628
1629               /* The value of .ef is the address of epilogue code;
1630                  not useful for gdb.  */
1631               /* { main_aux.x_sym.x_misc.x_lnsz.x_lnno
1632                  contains number of lines to '}' */
1633
1634               fcn_last_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
1635               new = pop_context ();
1636               if (context_stack_depth != 0)
1637                 error ("\
1638   invalid symbol data; .bf/.ef/.bb/.eb symbol mismatch, at symbol %d.",
1639                        symnum);
1640
1641               finish_block (new->name, &local_symbols, new->old_blocks,
1642                             new->start_addr,
1643                             fcn_cs_saved.c_value +
1644                             fcn_aux_saved.x_sym.x_misc.x_fsize, objfile);
1645               within_function = 0;
1646             }
1647           break;
1648
1649         case C_BSTAT:
1650           /* Begin static block.  */
1651           {
1652             struct internal_syment symbol;
1653
1654             read_symbol (&symbol, cs->c_value);
1655             static_block_base = symbol.n_value;
1656             static_block_section = symbol.n_scnum;
1657           }
1658           break;
1659
1660         case C_ESTAT:
1661           /* End of static block.  */
1662           static_block_base = 0;
1663           static_block_section = -1;
1664           break;
1665
1666         case C_ARG:
1667         case C_REGPARM:
1668         case C_TPDEF:
1669         case C_STRTAG:
1670         case C_UNTAG:
1671         case C_ENTAG:
1672           printf_unfiltered
1673             ("ERROR: Unimplemented storage class: %d.\n", cs->c_sclass);
1674           break;
1675
1676         case C_LABEL:
1677         case C_NULL:
1678           /* Ignore these.  */
1679           break;
1680
1681           /* This is wrong.  These symbols are XMC_TC, which means that
1682              the value of the symbol is the address of the TOC entry, not
1683              the address of the variable itself.  */
1684         case C_HIDEXT:
1685 #ifdef STATIC_NODEBUG_VARS
1686           {
1687             /* This is the only place that static variables show up in files
1688                compiled without -g.  External variables also have a C_EXT,
1689                so that is why we record everything as mst_file_* here.  */
1690             enum minimal_symbol_type ms_type;
1691             CORE_ADDR tmpaddr;
1692             int sec;
1693
1694             sec = cs_to_section (cs, objfile);
1695             tmpaddr = cs->c_value;
1696
1697             switch (sec)
1698               {
1699               case SECT_OFF_TEXT:
1700               case SECT_OFF_RODATA:
1701                 ms_type = mst_file_text;
1702                 break;
1703               case SECT_OFF_DATA:
1704                 ms_type = mst_file_data;
1705                 break;
1706               case SECT_OFF_BSS:
1707                 ms_type = mst_file_bss;
1708                 break;
1709               default:
1710                 ms_type = mst_unknown;
1711                 break;
1712               }
1713             RECORD_MINIMAL_SYMBOL (cs->c_name, cs->c_value, ms_type,
1714                                    symname_alloced, cs->c_secnum, objfile);
1715           }
1716 #endif /* STATIC_NODEBUG_VARS */
1717           break;
1718
1719         case C_BINCL:
1720           /* beginning of include file */
1721           /* In xlc output, C_BINCL/C_EINCL pair doesn't show up in sorted
1722              order. Thus, when wee see them, we might not know enough info
1723              to process them. Thus, we'll be saving them into a table 
1724              (inclTable) and postpone their processing. */
1725
1726           record_include_begin (cs);
1727           break;
1728
1729         case C_EINCL:
1730           /* End of include file.  */
1731           /* See the comment after case C_BINCL.  */
1732           record_include_end (cs);
1733           break;
1734
1735         case C_BLOCK:
1736           if (STREQ (cs->c_name, ".bb"))
1737             {
1738               depth++;
1739               new = push_context (depth, cs->c_value);
1740             }
1741           else if (STREQ (cs->c_name, ".eb"))
1742             {
1743               new = pop_context ();
1744               if (depth != new->depth)
1745                 error ("\
1746   Invalid symbol data: .bb/.eb symbol mismatch at symbol %d.",
1747                        symnum);
1748
1749               depth--;
1750               if (local_symbols && context_stack_depth > 0)
1751                 {
1752                   /* Make a block for the local symbols within.  */
1753                   finish_block (new->name, &local_symbols, new->old_blocks,
1754                                 new->start_addr, cs->c_value, objfile);
1755                 }
1756               local_symbols = new->locals;
1757             }
1758           break;
1759
1760         default:
1761           process_xcoff_symbol (cs, objfile);
1762           break;
1763         }
1764     }
1765
1766   if (last_source_file)
1767     {
1768       end_symtab (cur_src_end_addr, 1, 0, objfile, textsec->target_index);
1769       end_stabs ();
1770     }
1771
1772   free (symtbl);
1773   current_objfile = NULL;
1774
1775   /* Record the toc offset value of this symbol table into ldinfo structure.
1776      If no XMC_TC0 is found, toc_offset should be zero. Another place to obtain
1777      this information would be file auxiliary header. */
1778
1779 #ifndef FAKING_RS6000
1780   xcoff_add_toc_to_loadinfo (toc_offset);
1781 #endif
1782 }
1783
1784 #define SYMBOL_DUP(SYMBOL1, SYMBOL2)    \
1785   (SYMBOL2) = (struct symbol *)         \
1786         obstack_alloc (&objfile->symbol_obstack, sizeof (struct symbol)); \
1787   *(SYMBOL2) = *(SYMBOL1);
1788   
1789  
1790 #define SYMNAME_ALLOC(NAME, ALLOCED)    \
1791   (ALLOCED) ? (NAME) : obstack_copy0 (&objfile->symbol_obstack, (NAME), strlen (NAME));
1792
1793
1794 static struct type *func_symbol_type;
1795 static struct type *var_symbol_type;
1796
1797 /* process one xcoff symbol. */
1798
1799 static struct symbol *
1800 process_xcoff_symbol (cs, objfile)
1801   register struct coff_symbol *cs;
1802   struct objfile *objfile;
1803 {
1804   struct symbol onesymbol;
1805   register struct symbol *sym = &onesymbol;
1806   struct symbol *sym2 = NULL;
1807   struct type *ttype;
1808   char *name, *pp, *qq;
1809   int struct_and_type_combined;
1810   int nameless;
1811
1812   name = cs->c_name;
1813   if (name[0] == '.')
1814     ++name;
1815
1816   memset (sym, '\0', sizeof (struct symbol));
1817
1818   /* default assumptions */
1819   SYMBOL_VALUE (sym) = cs->c_value;
1820   SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1821   SYMBOL_SECTION (sym) = cs->c_secnum;
1822
1823   if (ISFCN (cs->c_type))
1824     {
1825       /* At this point, we don't know the type of the function.  This
1826          will be patched with the type from its stab entry later on in
1827          patch_block_stabs (), unless the file was compiled without -g.  */
1828
1829       SYMBOL_NAME (sym) = SYMNAME_ALLOC (name, symname_alloced);
1830       SYMBOL_TYPE (sym) = func_symbol_type;
1831
1832       SYMBOL_CLASS (sym) = LOC_BLOCK;
1833       SYMBOL_DUP (sym, sym2);
1834
1835       if (cs->c_sclass == C_EXT)
1836         add_symbol_to_list (sym2, &global_symbols);
1837       else if (cs->c_sclass == C_HIDEXT || cs->c_sclass == C_STAT)
1838         add_symbol_to_list (sym2, &file_symbols);
1839     }
1840   else
1841     {
1842       /* In case we can't figure out the type, provide default. */
1843       SYMBOL_TYPE (sym) = var_symbol_type;
1844
1845       switch (cs->c_sclass)
1846         {
1847 #if 0
1848         case C_FUN:
1849           if (fcn_cs_saved.c_sclass == C_EXT)
1850             add_stab_to_list (name, &global_stabs);
1851           else
1852             add_stab_to_list (name, &file_stabs);
1853           break;
1854 #endif
1855
1856         case C_GSYM:
1857           add_stab_to_list (name, &global_stabs);
1858           break;
1859
1860         case C_BCOMM:
1861           common_block_start (cs->c_name, objfile);
1862           break;
1863
1864         case C_ECOMM:
1865           common_block_end (objfile);
1866           break;
1867
1868         default:
1869           complain (&storclass_complaint, cs->c_sclass);
1870           /* FALLTHROUGH */
1871
1872         case C_DECL:
1873         case C_PSYM:
1874         case C_RPSYM:
1875         case C_ECOML:
1876
1877           sym = define_symbol (cs->c_value, cs->c_name, 0, 0, objfile);
1878           if (sym != NULL)
1879             {
1880               SYMBOL_SECTION (sym) = cs->c_secnum;
1881             }
1882           return sym;
1883
1884         case C_STSYM:
1885
1886           /* For xlc (not GCC), the 'V' symbol descriptor is used for
1887              all statics and we need to distinguish file-scope versus
1888              function-scope using within_function.  We do this by
1889              changing the string we pass to define_symbol to use 'S'
1890              where we need to, which is not necessarily super-clean,
1891              but seems workable enough.  */
1892
1893           if (*name == ':' || (pp = (char *) strchr(name, ':')) == NULL)
1894             return NULL;
1895
1896           ++pp;
1897           if (*pp == 'V' && !within_function)
1898             *pp = 'S';
1899           sym = define_symbol (cs->c_value, cs->c_name, 0, 0, objfile);
1900           if (sym != NULL)
1901             {
1902               SYMBOL_VALUE (sym) += static_block_base;
1903               SYMBOL_SECTION (sym) = static_block_section;
1904             }
1905           return sym;
1906
1907         case C_LSYM:
1908           sym = define_symbol (cs->c_value, cs->c_name, 0, N_LSYM, objfile);
1909           if (sym != NULL)
1910             {
1911               SYMBOL_SECTION (sym) = cs->c_secnum;
1912             }
1913           return sym;
1914
1915         case C_AUTO:
1916           SYMBOL_CLASS (sym) = LOC_LOCAL;
1917           SYMBOL_NAME (sym) = SYMNAME_ALLOC (name, symname_alloced);
1918           SYMBOL_SECTION (sym) = cs->c_secnum;
1919           SYMBOL_DUP (sym, sym2);
1920           add_symbol_to_list (sym2, &local_symbols);
1921           break;
1922
1923         case C_EXT:
1924           SYMBOL_CLASS (sym) = LOC_STATIC;
1925           SYMBOL_NAME (sym) = SYMNAME_ALLOC (name, symname_alloced);
1926           SYMBOL_SECTION (sym) = cs->c_secnum;
1927           SYMBOL_DUP (sym, sym2);
1928           add_symbol_to_list (sym2, &global_symbols);
1929           break;
1930
1931         case C_STAT:
1932           SYMBOL_CLASS (sym) = LOC_STATIC;
1933           SYMBOL_NAME (sym) = SYMNAME_ALLOC (name, symname_alloced);
1934           SYMBOL_SECTION (sym) = cs->c_secnum;
1935           SYMBOL_DUP (sym, sym2);
1936           add_symbol_to_list 
1937             (sym2, within_function ? &local_symbols : &file_symbols);
1938           break;
1939
1940         case C_REG:
1941           printf_unfiltered ("ERROR! C_REG is not fully implemented!\n");
1942           SYMBOL_CLASS (sym) = LOC_REGISTER;
1943           SYMBOL_NAME (sym) = SYMNAME_ALLOC (name, symname_alloced);
1944           SYMBOL_SECTION (sym) = cs->c_secnum;
1945           SYMBOL_DUP (sym, sym2);
1946           add_symbol_to_list (sym2, &local_symbols);
1947           break;
1948
1949         case C_RSYM:
1950           pp = (char*) strchr (name, ':');
1951           if (pp)
1952             {
1953               sym = define_symbol (cs->c_value, cs->c_name, 0, 0, objfile);
1954               if (sym != NULL)
1955                 SYMBOL_SECTION (sym) = cs->c_secnum;
1956               return sym;
1957             }
1958           else
1959             {
1960               complain (&rsym_complaint, name);
1961               return NULL;
1962             }
1963         }
1964     }
1965   return sym2;
1966 }
1967
1968 /* Extract the file name from the aux entry of a C_FILE symbol.  Return
1969    only the last component of the name.  Result is in static storage and
1970    is only good for temporary use.  */
1971
1972 static char *
1973 coff_getfilename (aux_entry)
1974     union internal_auxent *aux_entry;
1975 {
1976   static char buffer[BUFSIZ];
1977   register char *temp;
1978   char *result;
1979
1980   if (aux_entry->x_file.x_n.x_zeroes == 0)
1981     strcpy (buffer, strtbl + aux_entry->x_file.x_n.x_offset);
1982   else
1983     {
1984       strncpy (buffer, aux_entry->x_file.x_fname, FILNMLEN);
1985       buffer[FILNMLEN] = '\0';
1986     }
1987   result = buffer;
1988
1989   /* FIXME: We should not be throwing away the information about what
1990      directory.  It should go into dirname of the symtab, or some such
1991      place.  */
1992   if ((temp = strrchr (result, '/')) != NULL)
1993     result = temp + 1;
1994   return (result);
1995 }
1996
1997 /* Set *SYMBOL to symbol number symno in symtbl.  */
1998 static void
1999 read_symbol (symbol, symno)
2000      struct internal_syment *symbol;
2001      int symno;
2002 {
2003   if (symno < 0 || symno >= symtbl_num_syms)
2004     {
2005       static struct complaint msg =
2006         {"Invalid symbol offset", 0, 0};
2007       complain (&msg);
2008       symbol->n_value = 0;
2009       symbol->n_scnum = -1;
2010       return;
2011     }
2012   bfd_coff_swap_sym_in (symfile_bfd, symtbl + (symno*local_symesz), symbol);
2013 }
2014   
2015 /* Get value corresponding to symbol number symno in symtbl.  */
2016
2017 static int
2018 read_symbol_nvalue (symno)
2019      int symno;
2020 {
2021   struct internal_syment symbol[1];
2022
2023   read_symbol (symbol, symno);
2024   return symbol->n_value;  
2025 }
2026
2027
2028 /* Find the address of the function corresponding to symno, where
2029    symno is the symbol pointed to by the linetable.  */
2030
2031 static int
2032 read_symbol_lineno (symno)
2033   int symno;
2034 {
2035   struct internal_syment symbol[1];
2036   union internal_auxent main_aux[1];
2037
2038   /* Note that just searching for a short distance (e.g. 50 symbols)
2039      is not enough, at least in the following case.
2040
2041      .extern foo
2042      [many .stabx entries]
2043      [a few functions, referring to foo]
2044      .globl foo
2045      .bf
2046
2047      What happens here is that the assembler moves the .stabx entries
2048      to right before the ".bf" for foo, but the symbol for "foo" is before
2049      all the stabx entries.  See PR gdb/2222.  */
2050   while (symno < symtbl_num_syms) {
2051     bfd_coff_swap_sym_in (symfile_bfd,
2052                           symtbl + (symno*local_symesz), symbol);
2053     if (symbol->n_sclass == C_FCN && STREQ (symbol->n_name, ".bf"))
2054       goto gotit;
2055     symno += symbol->n_numaux+1;
2056   }
2057
2058   complain (&bf_notfound_complaint);
2059   return 0;
2060
2061 gotit:
2062   /* take aux entry and return its lineno */
2063   symno++;
2064   bfd_coff_swap_aux_in (symfile_bfd, symtbl+(symno*local_symesz),
2065                         symbol->n_type, symbol->n_sclass,
2066                         0, symbol->n_numaux, main_aux);
2067
2068   return main_aux->x_sym.x_misc.x_lnsz.x_lnno;
2069 }
2070
2071 /* Support for line number handling */
2072
2073 /* This function is called for every section; it finds the outer limits
2074  * of the line table (minimum and maximum file offset) so that the
2075  * mainline code can read the whole thing for efficiency.
2076  */
2077 static void
2078 find_linenos(abfd, asect, vpinfo)
2079 bfd *abfd;
2080 sec_ptr asect;
2081 PTR vpinfo; 
2082 {
2083   struct coff_symfile_info *info;
2084   int size, count;
2085   file_ptr offset, maxoff;
2086
2087   count = asect->lineno_count;
2088
2089   if (!STREQ (asect->name, ".text") || count == 0)
2090     return;
2091
2092   size   = count * coff_data (symfile_bfd)->local_linesz;
2093   info   = (struct coff_symfile_info *)vpinfo;
2094   offset = asect->line_filepos;
2095   maxoff = offset + size;
2096
2097   if (offset < info->min_lineno_offset || info->min_lineno_offset == 0)
2098     info->min_lineno_offset = offset;
2099
2100   if (maxoff > info->max_lineno_offset)
2101     info->max_lineno_offset = maxoff;
2102 }
2103
2104
2105 /* Read in all the line numbers for fast lookups later.  Leave them in
2106    external (unswapped) format in memory; we'll swap them as we enter
2107    them into GDB's data structures.  */
2108
2109 static int
2110 init_lineno (abfd, offset, size)
2111      bfd *abfd;
2112      file_ptr offset;
2113      int size;
2114 {
2115   int val;
2116
2117   free_linetab ();
2118
2119   if (bfd_seek(abfd, offset, L_SET) < 0)
2120     return -1;
2121
2122   linetab = (char *) xmalloc(size);
2123
2124   val = bfd_read(linetab, 1, size, abfd);
2125   if (val != size)
2126     return -1;
2127
2128   linetab_offset = offset;
2129   linetab_size = size;
2130   return 0;
2131 }
2132
2133 static void
2134 free_linetab ()
2135 {
2136   if (linetab)
2137     free (linetab);
2138   linetab = NULL;
2139 }
2140 \f
2141 static void
2142 xcoff_new_init (objfile)
2143      struct objfile *objfile;
2144 {
2145 }
2146
2147
2148 /* xcoff_symfile_init()
2149    is the xcoff-specific initialization routine for reading symbols.
2150    It is passed an objfile which contains, among other things,
2151    the BFD for the file whose symbols are being read, and a slot for
2152    a pointer to "private data" which we fill with cookies and other
2153    treats for xcoff_symfile_read().
2154  
2155    We will only be called if this is an XCOFF or XCOFF-like file.
2156    BFD handles figuring out the format of the file, and code in symfile.c
2157    uses BFD's determination to vector to us.
2158  
2159    The ultimate result is a new symtab (or, FIXME, eventually a psymtab).  */
2160
2161 static void
2162 xcoff_symfile_init (objfile)
2163   struct objfile *objfile;
2164 {
2165   bfd *abfd = objfile->obfd;
2166
2167   /* Allocate struct to keep track of the symfile */
2168   objfile -> sym_private = xmmalloc (objfile -> md,
2169                                      sizeof (struct coff_symfile_info));
2170   init_entry_point_info (objfile);
2171 }
2172
2173 /* Perform any local cleanups required when we are done with a particular
2174    objfile.  I.E, we are in the process of discarding all symbol information
2175    for an objfile, freeing up all memory held for it, and unlinking the
2176    objfile struct from the global list of known objfiles. */
2177
2178 static void
2179 xcoff_symfile_finish (objfile)
2180      struct objfile *objfile;
2181 {
2182   if (objfile -> sym_private != NULL)
2183     {
2184       mfree (objfile -> md, objfile -> sym_private);
2185     }
2186
2187   /* Start with a fresh include table for the next objfile. */
2188
2189   if (inclTable)
2190     {
2191       free (inclTable);
2192       inclTable = NULL;
2193     }
2194   inclIndx = inclLength = inclDepth = 0;
2195 }
2196
2197
2198 static int
2199 init_stringtab(abfd, offset, objfile)
2200      bfd *abfd;
2201      file_ptr offset;
2202      struct objfile *objfile;
2203 {
2204   long length;
2205   int val;
2206   unsigned char lengthbuf[4];
2207
2208   if (bfd_seek(abfd, offset, L_SET) < 0)
2209     return -1;
2210
2211   val    = bfd_read((char *)lengthbuf, 1, sizeof lengthbuf, abfd);
2212   length = bfd_h_get_32(abfd, lengthbuf);
2213
2214   /* If no string table is needed, then the file may end immediately
2215      after the symbols.  Just return with `strtbl' set to null. */
2216
2217   if (val != sizeof length || length < sizeof length)
2218     return 0;
2219
2220   /* Allocate string table from symbol_obstack. We will need this table
2221      as long as we have its symbol table around. */
2222
2223   strtbl = (char*) obstack_alloc (&objfile->symbol_obstack, length);
2224   if (strtbl == NULL)
2225     return -1;
2226
2227   memcpy(strtbl, &length, sizeof length);
2228   if (length == sizeof length)
2229     return 0;
2230
2231   val = bfd_read(strtbl + sizeof length, 1, length - sizeof length, abfd);
2232
2233   if (val != length - sizeof length || strtbl[length - 1] != '\0')
2234     return -1;
2235
2236   return 0;
2237 }
2238
2239 static int
2240 init_debugsection(abfd)
2241      bfd *abfd;
2242 {
2243   register sec_ptr secp;
2244   bfd_size_type length;
2245
2246   if (debugsec) {
2247     free(debugsec);
2248     debugsec = NULL;
2249   }
2250
2251   secp = bfd_get_section_by_name(abfd, ".debug");
2252   if (!secp)
2253     return 0;
2254
2255   if (!(length = bfd_section_size(abfd, secp)))
2256     return 0;
2257
2258   debugsec = (char *) xmalloc ((unsigned)length);
2259   if (debugsec == NULL)
2260     return -1;
2261
2262   if (!bfd_get_section_contents(abfd, secp, debugsec, (file_ptr) 0, length)) {
2263     printf_unfiltered ("Can't read .debug section from symbol file\n");
2264     return -1;
2265   }
2266   return 0;
2267 }
2268
2269 static void
2270 free_debugsection()
2271 {
2272   if (debugsec)
2273     free(debugsec);
2274   debugsec = NULL;
2275 }
2276
2277
2278 /* xcoff version of symbol file read. */
2279
2280 static void
2281 xcoff_symfile_read (objfile, section_offset, mainline)
2282   struct objfile *objfile;
2283   struct section_offsets *section_offset;
2284   int mainline;
2285 {
2286   int num_symbols;                      /* # of symbols */
2287   file_ptr symtab_offset;               /* symbol table and */
2288   file_ptr stringtab_offset;            /* string table file offsets */
2289   int val;
2290   bfd *abfd;
2291   struct coff_symfile_info *info;
2292   char *name;
2293   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2294
2295   info = (struct coff_symfile_info *) objfile -> sym_private;
2296   symfile_bfd = abfd = objfile->obfd;
2297   name = objfile->name;
2298
2299   num_symbols = bfd_get_symcount (abfd);        /* # of symbols */
2300   symtab_offset = obj_sym_filepos (abfd);       /* symbol table file offset */
2301   stringtab_offset = symtab_offset +
2302     num_symbols * coff_data(abfd)->local_symesz;
2303
2304   info->min_lineno_offset = 0;
2305   info->max_lineno_offset = 0;
2306   bfd_map_over_sections (abfd, find_linenos, info);
2307
2308   /* FIXME!  This stuff should move into symfile_init */
2309   if (info->min_lineno_offset != 0
2310       && info->max_lineno_offset > info->min_lineno_offset) {
2311
2312     /* only read in the line # table if one exists */
2313     make_cleanup (free_linetab, 0);
2314     val = init_lineno(abfd, info->min_lineno_offset,
2315         (int) (info->max_lineno_offset - info->min_lineno_offset));
2316
2317     if (val < 0)
2318       error("\"%s\": error reading line numbers\n", name);
2319   }
2320
2321   if (num_symbols > 0)
2322     {
2323       val = init_stringtab(abfd, stringtab_offset, objfile);
2324       if (val < 0) {
2325         error ("\"%s\": can't get string table", name);
2326       }
2327
2328       if (init_debugsection(abfd) < 0) {
2329         error ("Error reading .debug section of `%s'\n", name);
2330       }
2331     }
2332
2333   /* Position to read the symbol table.  Do not read it all at once. */
2334   val = bfd_seek(abfd, symtab_offset, L_SET);
2335   if (val < 0)
2336     perror_with_name(name);
2337
2338   if (bfd_tell(abfd) != symtab_offset)
2339     fatal("bfd? BFD!");
2340
2341   init_minimal_symbol_collection ();
2342   make_cleanup (discard_minimal_symbols, 0);
2343
2344 #ifndef FAKING_RS6000
2345   /* Initialize load info structure. */
2346   if (mainline)
2347     xcoff_init_loadinfo ();
2348 #endif
2349
2350   /* Now that the executable file is positioned at symbol table,
2351      process it and define symbols accordingly. */
2352
2353   read_xcoff_symtab(objfile, num_symbols);
2354
2355   /* Free debug section. */
2356   free_debugsection ();
2357
2358   /* Sort symbols alphabetically within each block.  */
2359   {
2360     struct symtab *s;
2361     for (s = objfile -> symtabs; s != NULL; s = s -> next)
2362       {
2363         sort_symtab_syms (s);
2364       }
2365   }
2366
2367   /* Install any minimal symbols that have been collected as the current
2368      minimal symbols for this objfile. */
2369
2370   install_minimal_symbols (objfile);
2371
2372   do_cleanups (back_to);
2373 }
2374
2375 /* XCOFF-specific parsing routine for section offsets.  */
2376
2377 static int largest_section;
2378
2379 static void
2380 note_one_section (abfd, asect, ptr)
2381      bfd *abfd;
2382      asection *asect;
2383      PTR ptr;
2384 {
2385   if (asect->target_index > largest_section)
2386     largest_section = asect->target_index;
2387 }
2388
2389 static
2390 struct section_offsets *
2391 xcoff_symfile_offsets (objfile, addr)
2392      struct objfile *objfile;
2393      CORE_ADDR addr;
2394 {
2395   struct section_offsets *section_offsets;
2396   int i;
2397
2398   largest_section = 0;
2399   bfd_map_over_sections (objfile->obfd, note_one_section, NULL);
2400   objfile->num_sections = largest_section + 1;
2401   section_offsets = (struct section_offsets *)
2402     obstack_alloc
2403       (&objfile -> psymbol_obstack,
2404        sizeof (struct section_offsets)
2405        + sizeof (section_offsets->offsets) * (objfile->num_sections));
2406
2407   /* syms_from_objfile kindly subtracts from addr the bfd_section_vma
2408      of the .text section.  This strikes me as wrong--whether the
2409      offset to be applied to symbol reading is relative to the start
2410      address of the section depends on the symbol format.  In any
2411      event, this whole "addr" concept is pretty broken (it doesn't
2412      handle any section but .text sensibly), so just ignore the addr
2413      parameter and use 0.  That matches the fact that xcoff_symfile_read
2414      ignores the section_offsets).  */
2415   for (i = 0; i < objfile->num_sections; i++)
2416     ANOFFSET (section_offsets, i) = 0;
2417   
2418   return section_offsets;
2419 }
2420
2421 /* Register our ability to parse symbols for xcoff BFD files.  */
2422
2423 static struct sym_fns xcoff_sym_fns =
2424 {
2425
2426   /* Because the bfd uses coff_flavour, we need to specially kludge
2427      the flavour.  FIXME: coff and xcoff and fundamentally similar
2428      except for debug format, and we should see if we can merge this
2429      file with coffread.c.  For example, the extra storage classes
2430      used for stabs could presumably be recognized in any COFF file.  */
2431
2432   (enum bfd_flavour)-1,
2433
2434   xcoff_new_init,       /* sym_new_init: init anything gbl to entire symtab */
2435   xcoff_symfile_init,   /* sym_init: read initial info, setup for sym_read() */
2436   xcoff_symfile_read,   /* sym_read: read a symbol file into symtab */
2437   xcoff_symfile_finish, /* sym_finish: finished with file, cleanup */
2438   xcoff_symfile_offsets, /* sym_offsets: xlate offsets ext->int form */
2439   NULL                  /* next: pointer to next struct sym_fns */
2440 };
2441
2442 void
2443 _initialize_xcoffread ()
2444 {
2445   add_symtab_fns(&xcoff_sym_fns);
2446
2447   /* Initialize symbol template later used for arguments.  Its other
2448      fields are zero, or are filled in later.  */
2449   SYMBOL_NAME (&parmsym) = "";
2450   SYMBOL_INIT_LANGUAGE_SPECIFIC (&parmsym, language_c);
2451   SYMBOL_NAMESPACE (&parmsym) = VAR_NAMESPACE;
2452   SYMBOL_CLASS (&parmsym) = LOC_ARG;
2453
2454   func_symbol_type = init_type (TYPE_CODE_FUNC, 1, 0,
2455                                 "<function, no debug info>", NULL);
2456   TYPE_TARGET_TYPE (func_symbol_type) = builtin_type_int;
2457   var_symbol_type =
2458     init_type (TYPE_CODE_INT, TARGET_INT_BIT / HOST_CHAR_BIT, 0,
2459                "<variable, no debug info>", NULL);
2460 }
This page took 0.16969 seconds and 4 git commands to generate.