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