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