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