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