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