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