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