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