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