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