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