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