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