]> Git Repo - binutils.git/blob - gdb/xcoffread.c
Wed Apr 12 16:06:01 1995 Jason Merrill <[email protected]>
[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 extern struct complaint ef_complaint;
156 extern struct complaint eb_complaint;
157
158 static void
159 enter_line_range PARAMS ((struct subfile *, unsigned, unsigned,
160                           CORE_ADDR, CORE_ADDR, unsigned *));
161
162 static void
163 free_debugsection PARAMS ((void));
164
165 static int
166 init_debugsection PARAMS ((bfd *));
167
168 static int
169 init_stringtab PARAMS ((bfd *, file_ptr, struct objfile *));
170
171 static void
172 xcoff_symfile_init PARAMS ((struct objfile *));
173
174 static void
175 xcoff_new_init PARAMS ((struct objfile *));
176
177 static void
178 xcoff_symfile_read PARAMS ((struct objfile *, struct section_offsets *, int));
179
180 static void
181 xcoff_symfile_finish PARAMS ((struct objfile *));
182
183 static struct section_offsets *
184 xcoff_symfile_offsets PARAMS ((struct objfile *, CORE_ADDR));
185
186 static int
187 init_lineno PARAMS ((bfd *, file_ptr, int));
188
189 static void
190 free_linetab PARAMS ((void));
191
192 static void
193 find_linenos PARAMS ((bfd *, sec_ptr, PTR));
194
195 static char *
196 coff_getfilename PARAMS ((union internal_auxent *));
197
198 static void
199 read_symbol PARAMS ((struct internal_syment *, int));
200
201 static int
202 read_symbol_lineno PARAMS ((int));
203
204 static int
205 read_symbol_nvalue PARAMS ((int));
206
207 static struct symbol *
208 process_xcoff_symbol PARAMS ((struct coff_symbol *, struct objfile *));
209
210 static void
211 read_xcoff_symtab PARAMS ((struct objfile *, int));
212
213 static void
214 add_stab_to_list PARAMS ((char *, struct pending_stabs **));
215
216 \f
217 #ifdef STATIC_NODEBUG_VARS
218 /* Return the section_offsets* that CS points to.  */
219 static int cs_to_section PARAMS ((struct coff_symbol *, struct objfile *));
220
221 struct find_targ_sec_arg {
222   int targ_index;
223   int *resultp;
224 };
225
226 static void find_targ_sec PARAMS ((bfd *, asection *, void *));
227
228 static void find_targ_sec (abfd, sect, obj)
229      bfd *abfd;
230      asection *sect;
231      PTR obj;
232 {
233   struct find_targ_sec_arg *args = (struct find_targ_sec_arg *)obj;
234   if (sect->target_index == args->targ_index)
235     {
236       /* This is the section.  Figure out what SECT_OFF_* code it is.  */
237       if (bfd_get_section_flags (abfd, sect) & SEC_CODE)
238         *args->resultp = SECT_OFF_TEXT;
239       else if (bfd_get_section_flags (abfd, sect) & SEC_LOAD)
240         *args->resultp = SECT_OFF_DATA;
241       else
242         *args->resultp = SECT_OFF_BSS;
243     }
244 }
245
246 /* Return the section number (SECT_OFF_*) that CS points to.  */
247 static int
248 cs_to_section (cs, objfile)
249      struct coff_symbol *cs;
250      struct objfile *objfile;
251 {
252   int off = SECT_OFF_TEXT;
253   struct find_targ_sec_arg args;
254   args.targ_index = cs->c_secnum;
255   args.resultp = &off;
256   bfd_map_over_sections (objfile->obfd, find_targ_sec, &args);
257   return off;
258 }
259 #endif /* STATIC_NODEBUG_VARS */
260 \f
261 /* add a given stab string into given stab vector. */
262
263 static void
264 add_stab_to_list (stabname, stabvector)
265 char *stabname;
266 struct pending_stabs **stabvector;
267 {
268   if ( *stabvector == NULL) {
269     *stabvector = (struct pending_stabs *)
270         xmalloc (sizeof (struct pending_stabs) + 
271                         INITIAL_STABVECTOR_LENGTH * sizeof (char*));
272     (*stabvector)->count = 0;
273     (*stabvector)->length = INITIAL_STABVECTOR_LENGTH;
274   }
275   else if ((*stabvector)->count >= (*stabvector)->length) {
276     (*stabvector)->length += INITIAL_STABVECTOR_LENGTH;
277     *stabvector = (struct pending_stabs *)
278         xrealloc ((char *) *stabvector, sizeof (struct pending_stabs) + 
279         (*stabvector)->length * sizeof (char*));
280   }
281   (*stabvector)->stab [(*stabvector)->count++] = stabname;
282 }
283 \f
284 /* Linenos are processed on a file-by-file basis.
285
286    Two reasons:
287
288     1) xlc (IBM's native c compiler) postpones static function code
289        emission to the end of a compilation unit. This way it can
290        determine if those functions (statics) are needed or not, and
291        can do some garbage collection (I think). This makes line
292        numbers and corresponding addresses unordered, and we end up
293        with a line table like:
294        
295
296                 lineno  addr
297         foo()     10    0x100
298                   20    0x200
299                   30    0x300
300
301         foo3()    70    0x400
302                   80    0x500
303                   90    0x600
304
305         static foo2()
306                   40    0x700
307                   50    0x800
308                   60    0x900           
309
310         and that breaks gdb's binary search on line numbers, if the
311         above table is not sorted on line numbers. And that sort
312         should be on function based, since gcc can emit line numbers
313         like:
314         
315                 10      0x100   - for the init/test part of a for stmt.
316                 20      0x200
317                 30      0x300
318                 10      0x400   - for the increment part of a for stmt.
319
320         arrange_linetable() will do this sorting.               
321
322      2) aix symbol table might look like:
323
324                 c_file          // beginning of a new file
325                 .bi             // beginning of include file
326                 .ei             // end of include file
327                 .bi
328                 .ei
329
330         basically, .bi/.ei pairs do not necessarily encapsulate
331         their scope. They need to be recorded, and processed later
332         on when we come the end of the compilation unit.
333         Include table (inclTable) and process_linenos() handle
334         that.  */
335
336 /* compare line table entry addresses. */
337
338 static int
339 compare_lte (lte1, lte2)
340      struct linetable_entry *lte1, *lte2;
341 {
342   return lte1->pc - lte2->pc;
343 }
344
345 /* Give a line table with function entries are marked, arrange its functions
346    in assending order and strip off function entry markers and return it in
347    a newly created table. If the old one is good enough, return the old one. */
348 /* FIXME: I think all this stuff can be replaced by just passing
349    sort_linevec = 1 to end_symtab.  */
350
351 static struct linetable *
352 arrange_linetable (oldLineTb)
353   struct linetable *oldLineTb;                  /* old linetable */
354 {
355   int ii, jj, 
356       newline,                                  /* new line count */
357       function_count;                           /* # of functions */
358
359   struct linetable_entry *fentry;               /* function entry vector */
360   int fentry_size;                              /* # of function entries */
361   struct linetable *newLineTb;                  /* new line table */
362
363 #define NUM_OF_FUNCTIONS 20
364
365   fentry_size = NUM_OF_FUNCTIONS;
366   fentry = (struct linetable_entry*)
367     xmalloc (fentry_size * sizeof (struct linetable_entry));
368
369   for (function_count=0, ii=0; ii <oldLineTb->nitems; ++ii) {
370
371     if (oldLineTb->item[ii].line == 0) {        /* function entry found. */
372
373       if (function_count >= fentry_size) {      /* make sure you have room. */
374         fentry_size *= 2;
375         fentry = (struct linetable_entry*) 
376           xrealloc (fentry, fentry_size * sizeof (struct linetable_entry));
377       }
378       fentry[function_count].line = ii;
379       fentry[function_count].pc = oldLineTb->item[ii].pc;
380       ++function_count;
381     }
382   }
383
384   if (function_count == 0) {
385     free (fentry);
386     return oldLineTb;
387   }
388   else if (function_count > 1)
389     qsort (fentry, function_count, sizeof(struct linetable_entry), compare_lte);
390
391   /* allocate a new line table. */
392   newLineTb = (struct linetable *)
393     xmalloc
394       (sizeof (struct linetable) + 
395        (oldLineTb->nitems - function_count) * sizeof (struct linetable_entry));
396
397   /* if line table does not start with a function beginning, copy up until
398      a function begin. */
399
400   newline = 0;
401   if (oldLineTb->item[0].line != 0)
402     for (newline=0; 
403         newline < oldLineTb->nitems && oldLineTb->item[newline].line; ++newline)
404       newLineTb->item[newline] = oldLineTb->item[newline];
405
406   /* Now copy function lines one by one. */
407
408   for (ii=0; ii < function_count; ++ii) {
409     for (jj = fentry[ii].line + 1;
410                  jj < oldLineTb->nitems && oldLineTb->item[jj].line != 0; 
411                                                          ++jj, ++newline)
412       newLineTb->item[newline] = oldLineTb->item[jj];
413   }
414   free (fentry);
415   newLineTb->nitems = oldLineTb->nitems - function_count;
416   return newLineTb;  
417 }     
418
419
420
421 /* We try to detect the beginning of a compilation unit. That info will
422    be used as an entry in line number recording routines (enter_line_range) */
423
424 static unsigned first_fun_line_offset;
425 static unsigned first_fun_bf;
426
427 #define mark_first_line(OFFSET, SYMNUM) \
428   if (!first_fun_line_offset) {         \
429     first_fun_line_offset = OFFSET;     \
430     first_fun_bf = SYMNUM;              \
431   }
432   
433
434 /* include file support: C_BINCL/C_EINCL pairs will be kept in the 
435    following `IncludeChain'. At the end of each symtab (end_symtab),
436    we will determine if we should create additional symtab's to
437    represent if (the include files. */
438
439
440 typedef struct _inclTable {
441   char          *name;                          /* include filename */
442
443   /* Offsets to the line table.  end points to the last entry which is
444      part of this include file.  */
445   int           begin, end;
446   
447   struct subfile *subfile;
448   unsigned      funStartLine;                   /* start line # of its function */
449 } InclTable;
450
451 #define INITIAL_INCLUDE_TABLE_LENGTH    20
452 static InclTable  *inclTable;                   /* global include table */
453 static int        inclIndx;                     /* last entry to table */
454 static int        inclLength;                   /* table length */
455 static int        inclDepth;                    /* nested include depth */
456
457 static void allocate_include_entry PARAMS ((void));
458
459 static void
460 record_include_begin (cs)
461 struct coff_symbol *cs;
462 {
463   if (inclDepth)
464     {
465       /* In xcoff, we assume include files cannot be nested (not in .c files
466          of course, but in corresponding .s files.).  */
467
468       /* This can happen with old versions of GCC.
469          GCC 2.3.3-930426 does not exhibit this on a test case which
470          a user said produced the message for him.  */
471       static struct complaint msg = {"Nested C_BINCL symbols", 0, 0};
472       complain (&msg);
473     }
474   ++inclDepth;
475
476   allocate_include_entry ();
477
478   inclTable [inclIndx].name  = cs->c_name;
479   inclTable [inclIndx].begin = cs->c_value;
480 }
481
482 static void
483 record_include_end (cs)
484 struct coff_symbol *cs;
485 {
486   InclTable *pTbl;  
487
488   if (inclDepth == 0)
489     {
490       static struct complaint msg = {"Mismatched C_BINCL/C_EINCL pair", 0, 0};
491       complain (&msg);
492     }
493
494   allocate_include_entry ();
495
496   pTbl = &inclTable [inclIndx];
497   pTbl->end = cs->c_value;
498
499   --inclDepth;
500   ++inclIndx;
501 }
502
503 static void
504 allocate_include_entry ()
505 {
506   if (inclTable == NULL)
507     {
508       inclTable = (InclTable *) 
509         xmalloc (sizeof (InclTable) * INITIAL_INCLUDE_TABLE_LENGTH);
510       memset (inclTable,
511               '\0', sizeof (InclTable) * INITIAL_INCLUDE_TABLE_LENGTH);
512       inclLength = INITIAL_INCLUDE_TABLE_LENGTH;
513       inclIndx = 0;
514     }
515   else if (inclIndx >= inclLength)
516     {
517       inclLength += INITIAL_INCLUDE_TABLE_LENGTH;
518       inclTable = (InclTable *) 
519         xrealloc (inclTable, sizeof (InclTable) * inclLength);
520       memset (inclTable + inclLength - INITIAL_INCLUDE_TABLE_LENGTH, 
521               '\0', sizeof (InclTable)*INITIAL_INCLUDE_TABLE_LENGTH);
522     }
523 }
524
525 /* given the start and end addresses of a compilation unit (or a csect,
526    at times) process its lines and create appropriate line vectors. */
527
528 static void
529 process_linenos (start, end)
530   CORE_ADDR start, end;
531 {
532   char *pp;
533   int offset, ii;
534
535   /* subfile structure for the main compilation unit.  */
536   struct subfile main_subfile;
537
538   /* In the main source file, any time we see a function entry, we
539      reset this variable to function's absolute starting line number.
540      All the following line numbers in the function are relative to
541      this, and we record absolute line numbers in record_line().  */
542
543   int main_source_baseline = 0;
544
545   unsigned *firstLine;
546   CORE_ADDR addr;
547
548   if (!(offset = first_fun_line_offset))
549     goto return_after_cleanup;
550
551   memset (&main_subfile, '\0', sizeof (main_subfile));
552   first_fun_line_offset = 0;
553
554   if (inclIndx == 0)
555     /* All source lines were in the main source file. None in include files. */
556
557     enter_line_range (&main_subfile, offset, 0, start, end, 
558                                                 &main_source_baseline);
559
560   else
561     {
562       /* There was source with line numbers in include files.  */
563       main_source_baseline = 0;
564       for (ii=0; ii < inclIndx; ++ii)
565         {
566           struct subfile *tmpSubfile;
567
568           /* If there is main file source before include file, enter it.  */
569           if (offset < inclTable[ii].begin)
570             {
571               enter_line_range
572                 (&main_subfile, offset, inclTable[ii].begin - LINESZ,
573                  start, 0, &main_source_baseline);
574             }
575
576           /* Have a new subfile for the include file.  */
577
578           tmpSubfile = inclTable[ii].subfile =
579             (struct subfile *) xmalloc (sizeof (struct subfile));
580
581           memset (tmpSubfile, '\0', sizeof (struct subfile));
582           firstLine = &(inclTable[ii].funStartLine);
583
584           /* Enter include file's lines now.  */
585           enter_line_range (tmpSubfile, inclTable[ii].begin, 
586                             inclTable[ii].end, start, 0, firstLine);
587
588           offset = inclTable[ii].end + LINESZ;
589         }
590
591       /* All the include files' line have been processed at this point.  Now,
592          enter remaining lines of the main file, if any left.  */
593       if (offset < (linetab_offset + linetab_size + 1 - LINESZ))
594         {
595           enter_line_range (&main_subfile, offset, 0, start, end, 
596                             &main_source_baseline);
597         }
598     }
599
600   /* Process main file's line numbers.  */
601   if (main_subfile.line_vector)
602     {
603       struct linetable *lineTb, *lv;
604
605       lv = main_subfile.line_vector;
606
607       /* Line numbers are not necessarily ordered. xlc compilation will
608          put static function to the end. */
609
610       lineTb = arrange_linetable (lv);
611       if (lv == lineTb)
612         {
613           current_subfile->line_vector = (struct linetable *)
614             xrealloc (lv, (sizeof (struct linetable)
615                            + lv->nitems * sizeof (struct linetable_entry)));
616         }
617       else
618         {
619           free (lv);
620           current_subfile->line_vector = lineTb;
621         }
622
623       current_subfile->line_vector_length = 
624         current_subfile->line_vector->nitems;
625     }
626
627   /* Now, process included files' line numbers.  */
628
629   for (ii=0; ii < inclIndx; ++ii)
630     {
631       if ((inclTable[ii].subfile)->line_vector) /* Useless if!!! FIXMEmgo */
632         {
633           struct linetable *lineTb, *lv;
634
635           lv = (inclTable[ii].subfile)->line_vector;
636
637           /* Line numbers are not necessarily ordered. xlc compilation will
638              put static function to the end. */
639
640           lineTb = arrange_linetable (lv);
641
642           push_subfile ();
643
644           /* For the same include file, we might want to have more than one
645              subfile.  This happens if we have something like:
646
647                 ......
648                 #include "foo.h"
649                 ......
650                 #include "foo.h"
651                 ......
652
653              while foo.h including code in it. (stupid but possible)
654              Since start_subfile() looks at the name and uses an
655              existing one if finds, we need to provide a fake name and
656              fool it.  */
657
658 #if 0
659           start_subfile (inclTable[ii].name, (char*)0);
660 #else
661           {
662             /* Pick a fake name that will produce the same results as this
663                one when passed to deduce_language_from_filename.  Kludge on
664                top of kludge.  */
665             char *fakename = strrchr (inclTable[ii].name, '.');
666             if (fakename == NULL)
667               fakename = " ?";
668             start_subfile (fakename, (char*)0);
669             free (current_subfile->name);
670           }
671           current_subfile->name = strdup (inclTable[ii].name);
672 #endif
673
674           if (lv == lineTb)
675             {
676               current_subfile->line_vector =
677                 (struct linetable *) xrealloc
678                   (lv, (sizeof (struct linetable)
679                         + lv->nitems * sizeof (struct linetable_entry)));
680
681             }
682           else
683             {
684               free (lv);
685               current_subfile->line_vector = lineTb;
686             }
687
688           current_subfile->line_vector_length = 
689             current_subfile->line_vector->nitems;
690           start_subfile (pop_subfile (), (char*)0);
691         }
692     }
693
694  return_after_cleanup:
695
696   /* We don't want to keep alloc/free'ing the global include file table.  */
697   inclIndx = 0;
698
699   /* Start with a fresh subfile structure for the next file.  */
700   memset (&main_subfile, '\0', sizeof (struct subfile));
701 }
702
703 void
704 aix_process_linenos ()
705 {
706   /* process line numbers and enter them into line vector */
707   process_linenos (last_source_start_addr, cur_src_end_addr);
708 }
709
710
711 /* Enter a given range of lines into the line vector.
712    can be called in the following two ways:
713      enter_line_range (subfile, beginoffset, endoffset, startaddr, 0, firstLine)  or
714      enter_line_range (subfile, beginoffset, 0, startaddr, endaddr, firstLine)
715
716    endoffset points to the last line table entry that we should pay
717    attention to.  */
718
719 static void
720 enter_line_range (subfile, beginoffset, endoffset, startaddr, endaddr, firstLine)
721   struct subfile *subfile;
722   unsigned   beginoffset, endoffset;    /* offsets to line table */
723   CORE_ADDR  startaddr, endaddr;
724   unsigned   *firstLine;
725 {
726   char          *pp, *limit;
727   CORE_ADDR     addr;
728
729 /* Do Byte swapping, if needed. FIXME! */
730 #define P_LINENO(PP)  (*(unsigned short*)((struct external_lineno*)(PP))->l_lnno)
731 #define P_LINEADDR(PP)  (*(long*)((struct external_lineno*)(PP))->l_addr.l_paddr)
732 #define P_LINESYM(PP)       (*(long*)((struct external_lineno*)(PP))->l_addr.l_symndx)
733
734   pp = &linetab [beginoffset - linetab_offset];
735   if (endoffset != 0 && endoffset - linetab_offset >= linetab_size)
736     {
737       static struct complaint msg =
738         {"Bad line table offset in C_EINCL directive", 0, 0};
739       complain (&msg);
740       return;
741     }
742   limit = endoffset ? &linetab [endoffset - linetab_offset]
743                       : &linetab [linetab_size -1];
744
745   while (pp <= limit) {
746
747     /* find the address this line represents */
748     addr = P_LINENO(pp) ? 
749       P_LINEADDR(pp) : read_symbol_nvalue (P_LINESYM(pp)); 
750
751     if (addr < startaddr || (endaddr && addr >= endaddr))
752       return;
753
754     if (P_LINENO(pp) == 0) {
755       *firstLine = read_symbol_lineno (P_LINESYM(pp));
756       record_line (subfile, 0, addr);
757       --(*firstLine);
758     }
759     else
760       record_line (subfile, *firstLine + P_LINENO(pp), addr);
761
762     pp += LINESZ;
763   }
764 }
765
766 typedef struct {
767   int fsize;                            /* file size */
768   int fixedparms;                       /* number of fixed parms */
769   int floatparms;                       /* number of float parms */
770   unsigned int parminfo;                /* parameter info. 
771                                            See /usr/include/sys/debug.h
772                                            tbtable_ext.parminfo */
773   int framesize;                        /* function frame size */
774 } TracebackInfo;
775
776
777 /* Given a function symbol, return its traceback information. */
778
779   TracebackInfo *
780 retrieve_tracebackinfo (abfd, textsec, cs)
781   bfd *abfd;
782   sec_ptr textsec;
783   struct coff_symbol *cs;
784 {
785 #define TBTABLE_BUFSIZ  2000
786
787   static TracebackInfo tbInfo;
788   struct tbtable *ptb;
789
790   static char buffer [TBTABLE_BUFSIZ];
791
792   int  *pinsn;
793   int  bytesread=0;                     /* total # of bytes read so far */
794   int  bufferbytes;                     /* number of bytes in the buffer */
795
796   int functionstart = cs->c_value - textsec->vma;
797
798   memset (&tbInfo, '\0', sizeof (tbInfo));
799
800   /* keep reading blocks of data from the text section, until finding a zero
801      word and a traceback table. */
802
803   /* Note: The logical thing way to write this code would be to assign
804      to bufferbytes within the while condition.  But that triggers a
805      compiler (xlc in AIX 3.2) bug, so simplify it...  */
806   bufferbytes = 
807     (TBTABLE_BUFSIZ < (textsec->_raw_size - functionstart - bytesread) ? 
808      TBTABLE_BUFSIZ : (textsec->_raw_size - functionstart - bytesread));
809   while (bufferbytes 
810          && (bfd_get_section_contents
811              (abfd, textsec, buffer, 
812               (file_ptr)(functionstart + bytesread), bufferbytes)))
813   {
814     bytesread += bufferbytes;
815     pinsn = (int*) buffer;
816
817     /* if this is the first time we filled the buffer, retrieve function
818        framesize info. */
819
820     if (bytesread == bufferbytes) {
821
822       /* skip over unrelated instructions */
823
824       if (*pinsn == 0x7c0802a6)                 /* mflr r0 */
825         ++pinsn;
826       if ((*pinsn & 0xfc00003e) == 0x7c000026)  /* mfcr Rx */
827         ++pinsn;
828       if ((*pinsn & 0xfc000000) == 0x48000000)  /* bl foo, save fprs */
829         ++pinsn;
830       if ((*pinsn  & 0xfc1f0000) == 0xbc010000) /* stm Rx, NUM(r1) */
831         ++pinsn;
832
833       do {
834         int tmp = (*pinsn >> 16) & 0xffff;
835
836         if (tmp ==  0x9421) {                   /* stu  r1, NUM(r1) */
837           tbInfo.framesize = 0x10000 - (*pinsn & 0xffff);
838           break;
839         }
840         else if ((*pinsn == 0x93e1fffc) ||      /* st   r31,-4(r1) */
841                  (tmp == 0x9001))               /* st   r0, NUM(r1) */
842         ;
843         /* else, could not find a frame size. */
844         else
845           return NULL;
846
847       } while (++pinsn && *pinsn);
848
849       if (!tbInfo.framesize)
850         return NULL;      
851
852     }
853
854     /* look for a zero word. */
855
856     while (*pinsn && (pinsn < (int*)(buffer + bufferbytes - sizeof(int))))
857       ++pinsn;
858
859     if (pinsn >= (int*)(buffer + bufferbytes))
860       continue;
861
862     if (*pinsn == 0) {
863
864       /* function size is the amount of bytes we have skipped so far. */
865       tbInfo.fsize = bytesread - (buffer + bufferbytes - (char*)pinsn);
866
867       ++pinsn;
868
869       /* if we don't have the whole traceback table in the buffer, re-read
870          the whole thing. */
871
872       /* This is how much to read to get the traceback table.
873          8 bytes of the traceback table are always present, plus we
874          look at parminfo.  */
875 #define MIN_TBTABSIZ    12
876                                 
877       if ((char*)pinsn > (buffer + bufferbytes - MIN_TBTABSIZ)) {
878
879         /* In case if we are *very* close to the end of the text section
880            and cannot read properly from that point on, abort by returning
881            NULL.
882
883            This could happen if the traceback table is only 8 bytes,
884            but we try to read 12 bytes of it.
885            Handle this case more graciously -- FIXME */
886
887         if (!bfd_get_section_contents (
888                 abfd, textsec, buffer, 
889                 (file_ptr)(functionstart + 
890                  bytesread - (buffer + bufferbytes - (char*)pinsn)),MIN_TBTABSIZ))
891           { printf_unfiltered ("Abnormal return!..\n"); return NULL; }
892
893         ptb = (struct tbtable *)buffer;
894       }
895       else
896         ptb = (struct tbtable *)pinsn;
897
898       tbInfo.fixedparms = ptb->tb.fixedparms;
899       tbInfo.floatparms = ptb->tb.floatparms;
900       tbInfo.parminfo = ptb->tb_ext.parminfo;
901       return &tbInfo;
902     }
903     bufferbytes = 
904       (TBTABLE_BUFSIZ < (textsec->_raw_size - functionstart - bytesread) ? 
905        TBTABLE_BUFSIZ : (textsec->_raw_size - functionstart - bytesread));
906   }
907   return NULL;
908 }
909
910 #if 0
911 /* Given a function symbol, return a pointer to its traceback table. */
912
913   struct tbtable *
914 retrieve_traceback (abfd, textsec, cs, size)
915   bfd *abfd;
916   sec_ptr textsec;
917   struct coff_symbol *cs;
918   int *size;                            /* return function size */
919 {
920 #define TBTABLE_BUFSIZ  2000
921 #define MIN_TBTABSIZ    50              /* minimum buffer size to hold a
922                                            traceback table. */
923
924   static char buffer [TBTABLE_BUFSIZ];
925
926   int  *pinsn;
927   int  bytesread=0;                     /* total # of bytes read so far */
928   int  bufferbytes;                     /* number of bytes in the buffer */
929
930   int functionstart = cs->c_value - textsec->filepos + textsec->vma;
931   *size = 0;
932
933   /* keep reading blocks of data from the text section, until finding a zero
934      word and a traceback table. */
935
936   while (bfd_get_section_contents (abfd, textsec, buffer, 
937         (file_ptr)(functionstart + bytesread), 
938         bufferbytes = (
939                 (TBTABLE_BUFSIZ < (textsec->size - functionstart - bytesread)) ? 
940                  TBTABLE_BUFSIZ : (textsec->size - functionstart - bytesread))))
941   {
942     bytesread += bufferbytes;
943     pinsn = (int*) buffer;
944
945     /* look for a zero word. */
946
947     while (*pinsn && (pinsn < (int*)(buffer + bufferbytes - sizeof(int))))
948       ++pinsn;
949
950     if (pinsn >= (int*)(buffer + bufferbytes))
951       continue;
952
953     if (*pinsn == 0) {
954
955       /* function size is the amount of bytes we have skipped so far. */
956       *size = bytesread - (buffer + bufferbytes - pinsn);
957
958       ++pinsn;
959
960       /* if we don't have the whole traceback table in the buffer, re-read
961          the whole thing. */
962
963       if ((char*)pinsn > (buffer + bufferbytes - MIN_TBTABSIZ)) {
964
965         /* In case if we are *very* close to the end of the text section
966            and cannot read properly from that point on, abort for now.
967            Handle this case more graciously -- FIXME */
968
969         if (!bfd_get_section_contents (
970                 abfd, textsec, buffer, 
971                 (file_ptr)(functionstart + 
972                  bytesread - (buffer + bufferbytes - pinsn)),MIN_TBTABSIZ))
973         /*   abort (); */ { printf_unfiltered ("abort!!!\n"); return NULL; }
974
975         return (struct tbtable *)buffer;
976       }
977       else
978         return (struct tbtable *)pinsn;
979     }
980   }
981   return NULL;
982 }
983 #endif /* 0 */
984
985
986
987
988 /* Save the vital information for use when closing off the current file.
989    NAME is the file name the symbols came from, START_ADDR is the first
990    text address for the file, and SIZE is the number of bytes of text.  */
991
992 #define complete_symtab(name, start_addr) {     \
993   last_source_file = savestring (name, strlen (name));  \
994   last_source_start_addr = start_addr;                  \
995 }
996
997
998 /* Refill the symbol table input buffer
999    and set the variables that control fetching entries from it.
1000    Reports an error if no data available.
1001    This function can read past the end of the symbol table
1002    (into the string table) but this does no harm.  */
1003
1004 /* Reading symbol table has to be fast! Keep the followings as macros, rather
1005    than functions. */
1006
1007 #define RECORD_MINIMAL_SYMBOL(NAME, ADDR, TYPE, ALLOCED, SECTION, OBJFILE) \
1008 {                                               \
1009   char *namestr;                                \
1010   namestr = (NAME); \
1011   if (namestr[0] == '.') ++namestr; \
1012   if (!(ALLOCED)) {                             \
1013     (NAME) = namestr =                          \
1014     obstack_copy0 (&objfile->symbol_obstack, namestr, strlen (namestr)); \
1015     (ALLOCED) = 1;                                              \
1016   }                                                             \
1017   prim_record_minimal_symbol_and_info (namestr, (ADDR), (TYPE), \
1018                                        (char *)NULL, (SECTION), (OBJFILE)); \
1019   misc_func_recorded = 1;                                       \
1020 }
1021
1022
1023 /* A parameter template, used by ADD_PARM_TO_PENDING.  It is initialized
1024    in our initializer function at the bottom of the file, to avoid
1025    dependencies on the exact "struct symbol" format.  */
1026
1027 static struct symbol parmsym;
1028
1029 /* Add a parameter to a given pending symbol list. */ 
1030
1031 #define ADD_PARM_TO_PENDING(PARM, VALUE, PTYPE, PENDING_SYMBOLS)        \
1032 {                                                                       \
1033   PARM = (struct symbol *)                                              \
1034       obstack_alloc (&objfile->symbol_obstack, sizeof (struct symbol)); \
1035   *(PARM) = parmsym;                                                    \
1036   SYMBOL_TYPE (PARM) = PTYPE;                                           \
1037   SYMBOL_VALUE (PARM) = VALUE;                                          \
1038   add_symbol_to_list (PARM, &PENDING_SYMBOLS);                          \
1039 }
1040
1041
1042 /* xcoff has static blocks marked in `.bs', `.es' pairs. They cannot be
1043    nested. At any given time, a symbol can only be in one static block.
1044    This is the base address of current static block, zero if non exists. */
1045    
1046 static int static_block_base = 0;
1047
1048 /* Section number for the current static block.  */
1049
1050 static int static_block_section = -1;
1051
1052 /* true if space for symbol name has been allocated. */
1053
1054 static int symname_alloced = 0;
1055
1056 /* Next symbol to read.  Pointer into raw seething symbol table.  */
1057
1058 static char *raw_symbol;
1059
1060 /* This is the function which stabsread.c calls to get symbol
1061    continuations.  */
1062 static char *
1063 xcoff_next_symbol_text ()
1064 {
1065   struct internal_syment symbol;
1066   static struct complaint msg =
1067     {"Unexpected symbol continuation", 0, 0};
1068   char *retval;
1069
1070   bfd_coff_swap_sym_in (current_objfile->obfd, raw_symbol, &symbol);
1071   if (symbol.n_zeroes)
1072     {
1073       complain (&msg);
1074
1075       /* Return something which points to '\0' and hope the symbol reading
1076          code does something reasonable.  */
1077       retval = "";
1078     }
1079   else if (symbol.n_sclass & 0x80)
1080     {
1081       retval = debugsec + symbol.n_offset;
1082       raw_symbol += coff_data (current_objfile->obfd)->local_symesz;
1083       ++symnum;
1084     }
1085   else
1086     {
1087       complain (&msg);
1088
1089       /* Return something which points to '\0' and hope the symbol reading
1090          code does something reasonable.  */
1091       retval = "";
1092     }
1093   return retval;
1094 }
1095
1096 /* read the whole symbol table of a given bfd. */
1097
1098 static void
1099 read_xcoff_symtab (objfile, nsyms)
1100      struct objfile *objfile;   /* Object file we're reading from */
1101      int nsyms;                 /* # of symbols */
1102 {
1103   bfd *abfd = objfile->obfd;
1104   char *raw_auxptr;             /* Pointer to first raw aux entry for sym */
1105   sec_ptr  textsec;             /* Pointer to text section */
1106   TracebackInfo *ptb;           /* Pointer to traceback table */
1107
1108   struct internal_syment symbol[1];
1109   union internal_auxent main_aux;
1110   struct coff_symbol cs[1];
1111   CORE_ADDR file_start_addr = 0;
1112   CORE_ADDR file_end_addr = 0;
1113
1114   int next_file_symnum = -1;
1115   int just_started = 1;
1116   int depth = 0;
1117   int toc_offset = 0;           /* toc offset value in data section. */
1118   int val;
1119   int fcn_last_line;
1120   int fcn_start_addr;
1121   long fcn_line_offset;
1122   size_t size;
1123
1124   struct coff_symbol fcn_stab_saved;
1125
1126   /* fcn_cs_saved is global because process_xcoff_symbol needs it. */
1127   union internal_auxent fcn_aux_saved;
1128   struct type *fcn_type_saved = NULL;
1129   struct context_stack *new;
1130
1131   char *filestring = " _start_ ";       /* Name of the current file. */
1132
1133   char *last_csect_name;                /* last seen csect's name and value */
1134   CORE_ADDR last_csect_val;
1135   int last_csect_sec;
1136   int  misc_func_recorded;              /* true if any misc. function */
1137
1138   current_objfile = objfile;
1139
1140   /* Get the appropriate COFF "constants" related to the file we're
1141      handling. */
1142   N_TMASK = coff_data (abfd)->local_n_tmask;
1143   N_BTSHFT = coff_data (abfd)->local_n_btshft;
1144   local_symesz = coff_data (abfd)->local_symesz;
1145
1146   last_source_file = NULL;
1147   last_csect_name = 0;
1148   last_csect_val = 0;
1149   misc_func_recorded = 0;
1150
1151   start_stabs ();
1152   start_symtab (filestring, (char *)NULL, file_start_addr);
1153   symnum = 0;
1154   first_object_file_end = 0;
1155
1156   /* Allocate space for the entire symbol table at once, and read it
1157      all in.  The bfd is already positioned at the beginning of
1158      the symbol table.  */
1159
1160   size = coff_data (abfd)->local_symesz * nsyms;
1161   symtbl = xmalloc (size);
1162   symtbl_num_syms = nsyms;
1163
1164   val = bfd_read (symtbl, size, 1, abfd);
1165   if (val != size)
1166     perror_with_name ("reading symbol table");
1167
1168   raw_symbol = symtbl;
1169
1170   textsec = bfd_get_section_by_name (abfd, ".text");
1171   if (!textsec)
1172     {
1173       printf_unfiltered ("Unable to locate text section!\n");
1174     }
1175
1176   next_symbol_text_func = xcoff_next_symbol_text;
1177
1178   while (symnum < nsyms)
1179     {
1180
1181       QUIT;                     /* make this command interruptable.  */
1182
1183       /* READ_ONE_SYMBOL (symbol, cs, symname_alloced); */
1184       /* read one symbol into `cs' structure. After processing the
1185          whole symbol table, only string table will be kept in memory,
1186          symbol table and debug section of xcoff will be freed. Thus
1187          we can mark symbols with names in string table as
1188          `alloced'. */
1189       {
1190         int ii;
1191
1192         /* Swap and align the symbol into a reasonable C structure.  */
1193         bfd_coff_swap_sym_in (abfd, raw_symbol, symbol);
1194
1195         cs->c_symnum = symnum;
1196         cs->c_naux = symbol->n_numaux;
1197         if (symbol->n_zeroes)
1198           {
1199             symname_alloced = 0;
1200             /* We must use the original, unswapped, name here so the name field
1201                pointed to by cs->c_name will persist throughout xcoffread.  If
1202                we use the new field, it gets overwritten for each symbol.  */
1203             cs->c_name = ((struct external_syment *)raw_symbol)->e.e_name;
1204             /* If it's exactly E_SYMNMLEN characters long it isn't
1205                '\0'-terminated.  */
1206             if (cs->c_name[E_SYMNMLEN - 1] != '\0')
1207               {
1208                 char *p;
1209                 p = obstack_alloc (&objfile->symbol_obstack, E_SYMNMLEN + 1);
1210                 strncpy (p, cs->c_name, E_SYMNMLEN);
1211                 p[E_SYMNMLEN] = '\0';
1212                 cs->c_name = p;
1213                 symname_alloced = 1;
1214               }
1215           }
1216         else if (symbol->n_sclass & 0x80)
1217           {
1218             cs->c_name = debugsec + symbol->n_offset;
1219             symname_alloced = 0;
1220           }
1221         else
1222           {
1223             /* in string table */
1224             cs->c_name = strtbl + (int)symbol->n_offset;
1225             symname_alloced = 1;
1226           }
1227         cs->c_value = symbol->n_value;
1228         cs->c_sclass = symbol->n_sclass;
1229         cs->c_secnum = symbol->n_scnum;
1230         cs->c_type = (unsigned)symbol->n_type;
1231
1232         raw_symbol += coff_data (abfd)->local_symesz;
1233         ++symnum;
1234
1235         /* Save addr of first aux entry.  */
1236         raw_auxptr = raw_symbol;
1237
1238         /* Skip all the auxents associated with this symbol.  */
1239         for (ii = symbol->n_numaux; ii; --ii)
1240           {
1241             raw_symbol += coff_data (abfd)->local_auxesz;
1242             ++symnum;
1243           }
1244       }
1245
1246       /* if symbol name starts with ".$" or "$", ignore it. */
1247       if (cs->c_name[0] == '$'
1248           || (cs->c_name[1] == '$' && cs->c_name[0] == '.'))
1249         continue;
1250
1251       if (cs->c_symnum == next_file_symnum && cs->c_sclass != C_FILE)
1252         {
1253           if (last_source_file)
1254             {
1255               end_symtab (cur_src_end_addr, 1, 0, objfile,
1256                           textsec->target_index);
1257               end_stabs ();
1258             }
1259
1260           start_stabs ();
1261           start_symtab ("_globals_", (char *)NULL, (CORE_ADDR)0);
1262           cur_src_end_addr = first_object_file_end;
1263           /* done with all files, everything from here on is globals */
1264         }
1265
1266       /* if explicitly specified as a function, treat is as one. */
1267       if (ISFCN(cs->c_type) && cs->c_sclass != C_TPDEF)
1268         {
1269           bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
1270                                 0, cs->c_naux, &main_aux);
1271           goto function_entry_point;
1272         }
1273
1274       if ((cs->c_sclass == C_EXT || cs->c_sclass == C_HIDEXT)
1275           && cs->c_naux == 1)
1276         {
1277           /* Dealing with a symbol with a csect entry.  */
1278
1279 #define CSECT(PP) ((PP)->x_csect)
1280 #define CSECT_LEN(PP) (CSECT(PP).x_scnlen.l)
1281 #define CSECT_ALIGN(PP) (SMTYP_ALIGN(CSECT(PP).x_smtyp))
1282 #define CSECT_SMTYP(PP) (SMTYP_SMTYP(CSECT(PP).x_smtyp))
1283 #define CSECT_SCLAS(PP) (CSECT(PP).x_smclas)
1284
1285           /* Convert the auxent to something we can access.  */
1286           bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
1287                                 0, cs->c_naux, &main_aux);
1288
1289           switch (CSECT_SMTYP (&main_aux))
1290             {
1291
1292             case XTY_ER:
1293               /* Ignore all external references.  */
1294               continue;
1295
1296             case XTY_SD:
1297               /* A section description.  */
1298               {
1299                 switch (CSECT_SCLAS (&main_aux))
1300                   {
1301
1302                   case XMC_PR:
1303                     {
1304
1305                       /* A program csect is seen.  We have to allocate one
1306                          symbol table for each program csect.  Normally gdb
1307                          prefers one symtab for each source file.  In case
1308                          of AIX, one source file might include more than one
1309                          [PR] csect, and they don't have to be adjacent in
1310                          terms of the space they occupy in memory. Thus, one
1311                          single source file might get fragmented in the
1312                          memory and gdb's file start and end address
1313                          approach does not work!  GCC (and I think xlc) seem
1314                          to put all the code in the unnamed program csect.  */
1315
1316                       if (last_csect_name)
1317                         {
1318
1319                           /* If no misc. function recorded in the last
1320                              seen csect, enter it as a function. This
1321                              will take care of functions like strcmp()
1322                              compiled by xlc.  */
1323
1324                           if (!misc_func_recorded)
1325                             {
1326                               int alloced = 0;
1327                               RECORD_MINIMAL_SYMBOL
1328                                 (last_csect_name, last_csect_val,
1329                                  mst_text, alloced, last_csect_sec,
1330                                  objfile);
1331                             }
1332
1333                           complete_symtab (filestring, file_start_addr);
1334                           cur_src_end_addr = file_end_addr;
1335                           end_symtab (file_end_addr, 1, 0, objfile,
1336                                       textsec->target_index);
1337                           end_stabs ();
1338                           start_stabs ();
1339                           /* Give all csects for this source file the same
1340                              name.  */
1341                           start_symtab (filestring, NULL, (CORE_ADDR)0);
1342                         }
1343
1344                       /* If this is the very first csect seen,
1345                          basically `__start'. */
1346                       if (just_started)
1347                         {
1348                           first_object_file_end
1349                             = cs->c_value + CSECT_LEN (&main_aux);
1350                           just_started = 0;
1351                         }
1352
1353                       file_start_addr = cs->c_value;
1354                       file_end_addr = cs->c_value + CSECT_LEN (&main_aux);
1355
1356                       if (cs->c_name && cs->c_name[0] == '.')
1357                         {
1358                           last_csect_name = cs->c_name;
1359                           last_csect_val = cs->c_value;
1360                           last_csect_sec = cs->c_secnum;
1361                         }
1362                     }
1363                     misc_func_recorded = 0;
1364                     continue;
1365
1366                   case XMC_RW :
1367                     break;
1368
1369                     /* If the section is not a data description,
1370                        ignore it. Note that uninitialized data will
1371                        show up as XTY_CM/XMC_RW pair. */
1372
1373                   case XMC_TC0:
1374                     if (toc_offset)
1375                       warning ("More than one xmc_tc0 symbol found.");
1376                     toc_offset = cs->c_value;
1377                     continue;
1378
1379                   case XMC_TC:
1380 #ifdef STATIC_NODEBUG_VARS
1381                     /* We need to process these symbols if they are C_HIDEXT,
1382                        for static variables in files compiled without -g.  */
1383                     if (cs->c_sclass == C_HIDEXT)
1384                       break;
1385                     else
1386 #endif
1387                       continue;
1388
1389                   default:
1390                     /* Ignore the symbol.  */
1391                     continue;
1392                   }
1393               }
1394               break;
1395
1396             case XTY_LD:
1397
1398               switch (CSECT_SCLAS (&main_aux))
1399                 {
1400                 case XMC_PR:
1401                   /* a function entry point. */
1402                 function_entry_point:
1403                   RECORD_MINIMAL_SYMBOL (cs->c_name, cs->c_value, mst_text, 
1404                                          symname_alloced, cs->c_secnum,
1405                                          objfile);
1406
1407                   fcn_line_offset = main_aux.x_sym.x_fcnary.x_fcn.x_lnnoptr;
1408                   fcn_start_addr = cs->c_value;
1409
1410                   /* save the function header info, which will be used
1411                      when `.bf' is seen. */
1412                   fcn_cs_saved = *cs;
1413                   fcn_aux_saved = main_aux;
1414
1415                   ptb = NULL;
1416
1417                   /* If function has two auxent, then debugging information is
1418                      already available for it. Process traceback table for
1419                      functions with only one auxent. */
1420
1421                   if (cs->c_naux == 1)
1422                     ptb = retrieve_tracebackinfo (abfd, textsec, cs);
1423
1424                   else if (cs->c_naux != 2)
1425                     {
1426                       static struct complaint msg =
1427                         {"Expected one or two auxents for function", 0, 0};
1428                       complain (&msg);
1429                     }
1430
1431                   /* If there is traceback info, create and add parameters
1432                      for it. */
1433
1434                   if (ptb && (ptb->fixedparms || ptb->floatparms))
1435                     {
1436
1437                       int parmcnt = ptb->fixedparms + ptb->floatparms;
1438                       char *parmcode = (char*) &ptb->parminfo;
1439
1440                       /* The link area is 0x18 bytes.  */
1441                       int parmvalue = ptb->framesize + 0x18;
1442                       unsigned int ii, mask;
1443
1444                       for (ii=0, mask = 0x80000000; ii <parmcnt; ++ii)
1445                         {
1446                           struct symbol *parm;
1447
1448                           if (ptb->parminfo & mask)
1449                             {
1450                               /* float or double */
1451                               mask = mask >> 1;
1452                               if (ptb->parminfo & mask)
1453                                 {
1454                                   /* double parm */
1455                                   ADD_PARM_TO_PENDING
1456                                     (parm, parmvalue, builtin_type_double,
1457                                      local_symbols);
1458                                   parmvalue += sizeof (double);
1459                                 }
1460                               else
1461                                 {
1462                                   /* float parm */
1463                                   ADD_PARM_TO_PENDING
1464                                     (parm, parmvalue, builtin_type_float,
1465                                      local_symbols);
1466                                   parmvalue += sizeof (float);
1467                                 }
1468                             }
1469                           else
1470                             {
1471                               static struct type *intparm_type;
1472                               if (intparm_type == NULL)
1473                                 {
1474
1475                                   /* Create a type, which is a pointer
1476                                      type (a kludge to make it print
1477                                      in hex), but which has a name
1478                                      indicating we don't know the real
1479                                      type.  */
1480
1481                                   intparm_type =
1482                                     init_type
1483                                       (TYPE_CODE_PTR,
1484                                        TARGET_PTR_BIT / HOST_CHAR_BIT,
1485                                        0,
1486                                        "<non-float parameter>",
1487                                        NULL);
1488                                   TYPE_TARGET_TYPE (intparm_type) =
1489                                     builtin_type_void;
1490                                 }
1491                               ADD_PARM_TO_PENDING
1492                                 (parm, parmvalue,
1493                                  intparm_type,
1494                                  local_symbols);
1495                               parmvalue += sizeof (int);
1496                             }
1497                           mask = mask >> 1;
1498                         }
1499                 
1500                       /* Fake this as a function.  Needed in
1501                          process_xcoff_symbol().  */
1502                       cs->c_type = 32;
1503
1504                       finish_block
1505                         (process_xcoff_symbol (cs, objfile), &local_symbols, 
1506                          pending_blocks, cs->c_value,
1507                          cs->c_value + ptb->fsize, objfile);
1508                     }
1509                   continue;
1510
1511                 case XMC_GL:
1512                   /* shared library function trampoline code entry point. */
1513
1514                   /* record trampoline code entries as
1515                      mst_solib_trampoline symbol.  When we lookup mst
1516                      symbols, we will choose mst_text over
1517                      mst_solib_trampoline. */
1518                   RECORD_MINIMAL_SYMBOL
1519                     (cs->c_name, cs->c_value,
1520                      mst_solib_trampoline,
1521                      symname_alloced, cs->c_secnum, objfile);
1522                   continue;
1523
1524                 case XMC_DS:
1525                   /* The symbols often have the same names as debug symbols for
1526                      functions, and confuse lookup_symbol.  */
1527                   continue;
1528
1529                 default:
1530                   /* xlc puts each variable in a separate csect, so we get
1531                      an XTY_SD for each variable.  But gcc puts several
1532                      variables in a csect, so that each variable only gets
1533                      an XTY_LD.  We still need to record them.  This will
1534                      typically be XMC_RW; I suspect XMC_RO and XMC_BS might
1535                      be possible too.  */
1536                   break;
1537                 }
1538
1539             default:
1540               break;
1541             }
1542         }
1543
1544       switch (cs->c_sclass)
1545         {
1546
1547         case C_FILE:
1548
1549           /* see if the last csect needs to be recorded. */
1550
1551           if (last_csect_name && !misc_func_recorded)
1552             {
1553
1554               /* If no misc. function recorded in the last seen csect, enter
1555                  it as a function.  This will take care of functions like
1556                  strcmp() compiled by xlc.  */
1557
1558               int alloced = 0;
1559               RECORD_MINIMAL_SYMBOL
1560                 (last_csect_name, last_csect_val,
1561                  mst_text, alloced, last_csect_sec, objfile);
1562             }
1563
1564           /* c_value field contains symnum of next .file entry in table
1565              or symnum of first global after last .file. */
1566
1567           next_file_symnum = cs->c_value;
1568
1569           /* Complete symbol table for last object file containing
1570              debugging information. */
1571
1572           /* Whether or not there was a csect in the previous file, we
1573              have to call `end_stabs' and `start_stabs' to reset
1574              type_vector, line_vector, etc. structures.  */
1575
1576           complete_symtab (filestring, file_start_addr);
1577           cur_src_end_addr = file_end_addr;
1578           end_symtab (file_end_addr, 1, 0, objfile, textsec->target_index);
1579           end_stabs ();
1580
1581           /* XCOFF, according to the AIX 3.2 documentation, puts the filename
1582              in cs->c_name.  But xlc 1.3.0.2 has decided to do things the
1583              standard COFF way and put it in the auxent.  We use the auxent if
1584              the symbol is ".file" and an auxent exists, otherwise use the symbol
1585              itself.  Simple enough.  */
1586           if (!strcmp (cs->c_name, ".file") && cs->c_naux > 0)
1587             {
1588               bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
1589                                     0, cs->c_naux, &main_aux);
1590               filestring = coff_getfilename (&main_aux);
1591             }
1592           else
1593             filestring = cs->c_name;
1594
1595           start_stabs ();
1596           start_symtab (filestring, (char *)NULL, (CORE_ADDR)0);
1597           last_csect_name = 0;
1598
1599           /* reset file start and end addresses. A compilation unit with no text
1600              (only data) should have zero file boundaries. */
1601           file_start_addr = file_end_addr = 0;
1602           break;
1603
1604         case C_FUN:
1605           fcn_stab_saved = *cs;
1606           break;
1607
1608         case C_FCN:
1609           if (STREQ (cs->c_name, ".bf"))
1610             {
1611
1612               bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
1613                                     0, cs->c_naux, &main_aux);
1614
1615               within_function = 1;
1616
1617               mark_first_line (fcn_line_offset, cs->c_symnum);
1618
1619               new = push_context (0, fcn_start_addr);
1620
1621               new->name = define_symbol 
1622                 (fcn_cs_saved.c_value, fcn_stab_saved.c_name, 0, 0, objfile);
1623               if (new->name != NULL)
1624                 SYMBOL_SECTION (new->name) = cs->c_secnum;
1625             }
1626           else if (STREQ (cs->c_name, ".ef"))
1627             {
1628
1629               bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
1630                                     0, cs->c_naux, &main_aux);
1631
1632               /* The value of .ef is the address of epilogue code;
1633                  not useful for gdb.  */
1634               /* { main_aux.x_sym.x_misc.x_lnsz.x_lnno
1635                  contains number of lines to '}' */
1636
1637               fcn_last_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
1638               new = pop_context ();
1639               /* Stack must be empty now.  */
1640               if (context_stack_depth > 0 || new == NULL)
1641                 {
1642                   complain (&ef_complaint, cs->c_symnum);
1643                   within_function = 0;
1644                   break;
1645                 }
1646
1647               finish_block (new->name, &local_symbols, new->old_blocks,
1648                             new->start_addr,
1649                             fcn_cs_saved.c_value +
1650                             fcn_aux_saved.x_sym.x_misc.x_fsize, objfile);
1651               within_function = 0;
1652             }
1653           break;
1654
1655         case C_BSTAT:
1656           /* Begin static block.  */
1657           {
1658             struct internal_syment symbol;
1659
1660             read_symbol (&symbol, cs->c_value);
1661             static_block_base = symbol.n_value;
1662             static_block_section = symbol.n_scnum;
1663           }
1664           break;
1665
1666         case C_ESTAT:
1667           /* End of static block.  */
1668           static_block_base = 0;
1669           static_block_section = -1;
1670           break;
1671
1672         case C_ARG:
1673         case C_REGPARM:
1674         case C_REG:
1675         case C_TPDEF:
1676         case C_STRTAG:
1677         case C_UNTAG:
1678         case C_ENTAG:
1679           {
1680             static struct complaint msg =
1681               {"Unrecognized storage class %d.", 0, 0};
1682             complain (&msg, cs->c_sclass);
1683           }
1684           break;
1685
1686         case C_LABEL:
1687         case C_NULL:
1688           /* Ignore these.  */
1689           break;
1690
1691           /* This is wrong.  These symbols are XMC_TC, which means that
1692              the value of the symbol is the address of the TOC entry, not
1693              the address of the variable itself.  */
1694         case C_HIDEXT:
1695 #ifdef STATIC_NODEBUG_VARS
1696           {
1697             /* This is the only place that static variables show up in files
1698                compiled without -g.  External variables also have a C_EXT,
1699                so that is why we record everything as mst_file_* here.  */
1700             enum minimal_symbol_type ms_type;
1701             CORE_ADDR tmpaddr;
1702             int sec;
1703
1704             sec = cs_to_section (cs, objfile);
1705             tmpaddr = cs->c_value;
1706
1707             switch (sec)
1708               {
1709               case SECT_OFF_TEXT:
1710               case SECT_OFF_RODATA:
1711                 ms_type = mst_file_text;
1712                 break;
1713               case SECT_OFF_DATA:
1714                 ms_type = mst_file_data;
1715                 break;
1716               case SECT_OFF_BSS:
1717                 ms_type = mst_file_bss;
1718                 break;
1719               default:
1720                 ms_type = mst_unknown;
1721                 break;
1722               }
1723             RECORD_MINIMAL_SYMBOL (cs->c_name, cs->c_value, ms_type,
1724                                    symname_alloced, cs->c_secnum, objfile);
1725           }
1726 #endif /* STATIC_NODEBUG_VARS */
1727           break;
1728
1729         case C_BINCL:
1730           /* beginning of include file */
1731           /* In xlc output, C_BINCL/C_EINCL pair doesn't show up in sorted
1732              order. Thus, when wee see them, we might not know enough info
1733              to process them. Thus, we'll be saving them into a table 
1734              (inclTable) and postpone their processing. */
1735
1736           record_include_begin (cs);
1737           break;
1738
1739         case C_EINCL:
1740           /* End of include file.  */
1741           /* See the comment after case C_BINCL.  */
1742           record_include_end (cs);
1743           break;
1744
1745         case C_BLOCK:
1746           if (STREQ (cs->c_name, ".bb"))
1747             {
1748               depth++;
1749               new = push_context (depth, cs->c_value);
1750             }
1751           else if (STREQ (cs->c_name, ".eb"))
1752             {
1753               new = pop_context ();
1754               if (depth-- != new->depth)
1755                 {
1756                   complain (&eb_complaint, symnum);
1757                   break;
1758                 }
1759               if (local_symbols && context_stack_depth > 0)
1760                 {
1761                   /* Make a block for the local symbols within.  */
1762                   finish_block (new->name, &local_symbols, new->old_blocks,
1763                                 new->start_addr, cs->c_value, objfile);
1764                 }
1765               local_symbols = new->locals;
1766             }
1767           break;
1768
1769         default:
1770           process_xcoff_symbol (cs, objfile);
1771           break;
1772         }
1773     }
1774
1775   if (last_source_file)
1776     {
1777       end_symtab (cur_src_end_addr, 1, 0, objfile, textsec->target_index);
1778       end_stabs ();
1779     }
1780
1781   free (symtbl);
1782   current_objfile = NULL;
1783
1784   /* Record the toc offset value of this symbol table into ldinfo structure.
1785      If no XMC_TC0 is found, toc_offset should be zero. Another place to obtain
1786      this information would be file auxiliary header. */
1787
1788 #ifndef FAKING_RS6000
1789   xcoff_add_toc_to_loadinfo (toc_offset);
1790 #endif
1791 }
1792
1793 #define SYMBOL_DUP(SYMBOL1, SYMBOL2)    \
1794   (SYMBOL2) = (struct symbol *)         \
1795         obstack_alloc (&objfile->symbol_obstack, sizeof (struct symbol)); \
1796   *(SYMBOL2) = *(SYMBOL1);
1797   
1798  
1799 #define SYMNAME_ALLOC(NAME, ALLOCED)    \
1800   (ALLOCED) ? (NAME) : obstack_copy0 (&objfile->symbol_obstack, (NAME), strlen (NAME));
1801
1802
1803 static struct type *func_symbol_type;
1804 static struct type *var_symbol_type;
1805
1806 /* process one xcoff symbol. */
1807
1808 static struct symbol *
1809 process_xcoff_symbol (cs, objfile)
1810   register struct coff_symbol *cs;
1811   struct objfile *objfile;
1812 {
1813   struct symbol onesymbol;
1814   register struct symbol *sym = &onesymbol;
1815   struct symbol *sym2 = NULL;
1816   struct type *ttype;
1817   char *name, *pp, *qq;
1818   int struct_and_type_combined;
1819   int nameless;
1820
1821   name = cs->c_name;
1822   if (name[0] == '.')
1823     ++name;
1824
1825   memset (sym, '\0', sizeof (struct symbol));
1826
1827   /* default assumptions */
1828   SYMBOL_VALUE (sym) = cs->c_value;
1829   SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1830   SYMBOL_SECTION (sym) = cs->c_secnum;
1831
1832   if (ISFCN (cs->c_type))
1833     {
1834       /* At this point, we don't know the type of the function.  This
1835          will be patched with the type from its stab entry later on in
1836          patch_block_stabs (), unless the file was compiled without -g.  */
1837
1838       SYMBOL_NAME (sym) = SYMNAME_ALLOC (name, symname_alloced);
1839       SYMBOL_TYPE (sym) = func_symbol_type;
1840
1841       SYMBOL_CLASS (sym) = LOC_BLOCK;
1842       SYMBOL_DUP (sym, sym2);
1843
1844       if (cs->c_sclass == C_EXT)
1845         add_symbol_to_list (sym2, &global_symbols);
1846       else if (cs->c_sclass == C_HIDEXT || cs->c_sclass == C_STAT)
1847         add_symbol_to_list (sym2, &file_symbols);
1848     }
1849   else
1850     {
1851       /* In case we can't figure out the type, provide default. */
1852       SYMBOL_TYPE (sym) = var_symbol_type;
1853
1854       switch (cs->c_sclass)
1855         {
1856 #if 0
1857         case C_FUN:
1858           if (fcn_cs_saved.c_sclass == C_EXT)
1859             add_stab_to_list (name, &global_stabs);
1860           else
1861             add_stab_to_list (name, &file_stabs);
1862           break;
1863 #endif
1864
1865         case C_GSYM:
1866           add_stab_to_list (name, &global_stabs);
1867           break;
1868
1869         case C_BCOMM:
1870           common_block_start (cs->c_name, objfile);
1871           break;
1872
1873         case C_ECOMM:
1874           common_block_end (objfile);
1875           break;
1876
1877         default:
1878           complain (&storclass_complaint, cs->c_sclass);
1879           /* FALLTHROUGH */
1880
1881         case C_DECL:
1882         case C_PSYM:
1883         case C_RPSYM:
1884         case C_ECOML:
1885
1886           sym = define_symbol (cs->c_value, cs->c_name, 0, 0, objfile);
1887           if (sym != NULL)
1888             {
1889               SYMBOL_SECTION (sym) = cs->c_secnum;
1890             }
1891           return sym;
1892
1893         case C_STSYM:
1894
1895           /* For xlc (not GCC), the 'V' symbol descriptor is used for
1896              all statics and we need to distinguish file-scope versus
1897              function-scope using within_function.  We do this by
1898              changing the string we pass to define_symbol to use 'S'
1899              where we need to, which is not necessarily super-clean,
1900              but seems workable enough.  */
1901
1902           if (*name == ':' || (pp = (char *) strchr(name, ':')) == NULL)
1903             return NULL;
1904
1905           ++pp;
1906           if (*pp == 'V' && !within_function)
1907             *pp = 'S';
1908           sym = define_symbol (cs->c_value, cs->c_name, 0, 0, objfile);
1909           if (sym != NULL)
1910             {
1911               SYMBOL_VALUE (sym) += static_block_base;
1912               SYMBOL_SECTION (sym) = static_block_section;
1913             }
1914           return sym;
1915
1916         case C_LSYM:
1917           sym = define_symbol (cs->c_value, cs->c_name, 0, N_LSYM, objfile);
1918           if (sym != NULL)
1919             {
1920               SYMBOL_SECTION (sym) = cs->c_secnum;
1921             }
1922           return sym;
1923
1924         case C_AUTO:
1925           SYMBOL_CLASS (sym) = LOC_LOCAL;
1926           SYMBOL_NAME (sym) = SYMNAME_ALLOC (name, symname_alloced);
1927           SYMBOL_SECTION (sym) = cs->c_secnum;
1928           SYMBOL_DUP (sym, sym2);
1929           add_symbol_to_list (sym2, &local_symbols);
1930           break;
1931
1932         case C_EXT:
1933           SYMBOL_CLASS (sym) = LOC_STATIC;
1934           SYMBOL_NAME (sym) = SYMNAME_ALLOC (name, symname_alloced);
1935           SYMBOL_SECTION (sym) = cs->c_secnum;
1936           SYMBOL_DUP (sym, sym2);
1937           add_symbol_to_list (sym2, &global_symbols);
1938           break;
1939
1940         case C_STAT:
1941           SYMBOL_CLASS (sym) = LOC_STATIC;
1942           SYMBOL_NAME (sym) = SYMNAME_ALLOC (name, symname_alloced);
1943           SYMBOL_SECTION (sym) = cs->c_secnum;
1944           SYMBOL_DUP (sym, sym2);
1945           add_symbol_to_list 
1946             (sym2, within_function ? &local_symbols : &file_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.166435 seconds and 4 git commands to generate.