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