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