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