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