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