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