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