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