mrealloc() -> xmrealloc().
[deliverable/binutils-gdb.git] / gdb / xcoffread.c
CommitLineData
c906108c 1/* Read AIX xcoff symbol tables and convert to internal format, for GDB.
b6ba6518 2 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
3a42e9d0 3 1996, 1997, 1998, 1999, 2000, 2001
c5aa993b 4 Free Software Foundation, Inc.
c906108c
SS
5 Derived from coffread.c, dbxread.c, and a lot of hacking.
6 Contributed by IBM Corporation.
7
c5aa993b 8 This file is part of GDB.
c906108c 9
c5aa993b
JM
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
c906108c 14
c5aa993b
JM
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
c906108c 19
c5aa993b
JM
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA. */
c906108c
SS
24
25#include "defs.h"
26#include "bfd.h"
27
28#include <sys/types.h>
29#include <fcntl.h>
30#include <ctype.h>
31#include "gdb_string.h"
32
33#include <sys/param.h>
34#ifndef NO_SYS_FILE
35#include <sys/file.h>
36#endif
37#include "gdb_stat.h"
38
39#include "coff/internal.h"
40#include "libcoff.h" /* FIXME, internal data from BFD */
41#include "coff/rs6000.h"
42
43#include "symtab.h"
44#include "gdbtypes.h"
45#include "symfile.h"
46#include "objfiles.h"
47#include "buildsym.h"
48#include "stabsread.h"
49#include "expression.h"
c906108c
SS
50#include "complaints.h"
51
52#include "gdb-stabs.h"
53
54/* For interface with stabsread.c. */
55#include "aout/stab_gnu.h"
56
c906108c
SS
57\f
58/* We put a pointer to this structure in the read_symtab_private field
59 of the psymtab. */
60
c5aa993b
JM
61struct symloc
62 {
c906108c 63
c5aa993b 64 /* First symbol number for this file. */
c906108c 65
c5aa993b 66 int first_symnum;
c906108c 67
c5aa993b
JM
68 /* Number of symbols in the section of the symbol table devoted to
69 this file's symbols (actually, the section bracketed may contain
70 more than just this file's symbols). If numsyms is 0, the only
71 reason for this thing's existence is the dependency list. Nothing
72 else will happen when it is read in. */
c906108c 73
c5aa993b 74 int numsyms;
c906108c 75
c5aa993b
JM
76 /* Position of the start of the line number information for this psymtab. */
77 unsigned int lineno_off;
78 };
c906108c
SS
79
80/* Remember what we deduced to be the source language of this psymtab. */
81
82static enum language psymtab_language = language_unknown;
c906108c 83\f
c5aa993b 84
c906108c
SS
85/* Simplified internal version of coff symbol table information */
86
c5aa993b
JM
87struct coff_symbol
88 {
89 char *c_name;
90 int c_symnum; /* symbol number of this entry */
91 int c_naux; /* 0 if syment only, 1 if syment + auxent */
92 long c_value;
93 unsigned char c_sclass;
94 int c_secnum;
95 unsigned int c_type;
96 };
c906108c
SS
97
98/* last function's saved coff symbol `cs' */
99
100static struct coff_symbol fcn_cs_saved;
101
102static bfd *symfile_bfd;
103
104/* Core address of start and end of text of current source file.
105 This is calculated from the first function seen after a C_FILE
106 symbol. */
107
108
109static CORE_ADDR cur_src_end_addr;
110
111/* Core address of the end of the first object file. */
112
113static CORE_ADDR first_object_file_end;
114
115/* initial symbol-table-debug-string vector length */
116
117#define INITIAL_STABVECTOR_LENGTH 40
118
119/* Nonzero if within a function (so symbols should be local,
120 if nothing says specifically). */
121
122int within_function;
123
124/* Size of a COFF symbol. I think it is always 18, so I'm not sure
125 there is any reason not to just use a #define, but might as well
126 ask BFD for the size and store it here, I guess. */
127
c5aa993b 128static unsigned local_symesz;
c906108c 129
c5aa993b
JM
130struct coff_symfile_info
131 {
132 file_ptr min_lineno_offset; /* Where in file lowest line#s are */
133 file_ptr max_lineno_offset; /* 1+last byte of line#s in file */
c906108c 134
c5aa993b
JM
135 /* Pointer to the string table. */
136 char *strtbl;
c906108c 137
c5aa993b
JM
138 /* Pointer to debug section. */
139 char *debugsec;
c906108c 140
c5aa993b
JM
141 /* Pointer to the a.out symbol table. */
142 char *symtbl;
c906108c 143
c5aa993b
JM
144 /* Number of symbols in symtbl. */
145 int symtbl_num_syms;
c906108c 146
c5aa993b
JM
147 /* Offset in data section to TOC anchor. */
148 CORE_ADDR toc_offset;
149 };
c906108c
SS
150
151static struct complaint storclass_complaint =
c5aa993b 152{"Unexpected storage class: %d", 0, 0};
c906108c
SS
153
154static struct complaint bf_notfound_complaint =
c5aa993b 155{"line numbers off, `.bf' symbol not found", 0, 0};
c906108c 156
c5aa993b
JM
157static struct complaint ef_complaint =
158{"Mismatched .ef symbol ignored starting at symnum %d", 0, 0};
c906108c 159
c5aa993b
JM
160static struct complaint eb_complaint =
161{"Mismatched .eb symbol ignored starting at symnum %d", 0, 0};
c906108c 162
a14ed312 163static void xcoff_initial_scan (struct objfile *, int);
c906108c 164
a14ed312 165static void scan_xcoff_symtab (struct objfile *);
c906108c 166
a14ed312 167static char *xcoff_next_symbol_text (struct objfile *);
c906108c 168
a14ed312 169static void record_include_begin (struct coff_symbol *);
c906108c
SS
170
171static void
a14ed312
KB
172enter_line_range (struct subfile *, unsigned, unsigned,
173 CORE_ADDR, CORE_ADDR, unsigned *);
c906108c 174
a14ed312 175static void init_stringtab (bfd *, file_ptr, struct objfile *);
c906108c 176
a14ed312 177static void xcoff_symfile_init (struct objfile *);
c906108c 178
a14ed312 179static void xcoff_new_init (struct objfile *);
c906108c 180
a14ed312 181static void xcoff_symfile_finish (struct objfile *);
c906108c 182
d4f3574e 183static void
a14ed312 184xcoff_symfile_offsets (struct objfile *, struct section_addr_info *addrs);
c906108c 185
a14ed312 186static void find_linenos (bfd *, sec_ptr, PTR);
c906108c 187
a14ed312 188static char *coff_getfilename (union internal_auxent *, struct objfile *);
c906108c 189
a14ed312 190static void read_symbol (struct internal_syment *, int);
c906108c 191
a14ed312 192static int read_symbol_lineno (int);
c906108c 193
470d5666 194static CORE_ADDR read_symbol_nvalue (int);
c906108c 195
a14ed312
KB
196static struct symbol *process_xcoff_symbol (struct coff_symbol *,
197 struct objfile *);
c906108c 198
a14ed312 199static void read_xcoff_symtab (struct partial_symtab *);
c906108c
SS
200
201#if 0
a14ed312 202static void add_stab_to_list (char *, struct pending_stabs **);
c906108c
SS
203#endif
204
a14ed312 205static int compare_lte (const void *, const void *);
c906108c 206
a14ed312 207static struct linetable *arrange_linetable (struct linetable *);
c906108c 208
a14ed312 209static void record_include_end (struct coff_symbol *);
c906108c 210
a14ed312 211static void process_linenos (CORE_ADDR, CORE_ADDR);
c906108c 212\f
c5aa993b 213
c906108c
SS
214/* Translate from a COFF section number (target_index) to a SECT_OFF_*
215 code. */
a14ed312
KB
216static int secnum_to_section (int, struct objfile *);
217static asection *secnum_to_bfd_section (int, struct objfile *);
c906108c 218
c5aa993b
JM
219struct find_targ_sec_arg
220 {
221 int targ_index;
222 int *resultp;
223 asection **bfd_sect;
b8fbeb18 224 struct objfile *objfile;
c5aa993b 225 };
c906108c 226
a14ed312 227static void find_targ_sec (bfd *, asection *, void *);
c906108c 228
c5aa993b 229static void
fba45db2 230find_targ_sec (bfd *abfd, asection *sect, PTR obj)
c906108c 231{
c5aa993b 232 struct find_targ_sec_arg *args = (struct find_targ_sec_arg *) obj;
b8fbeb18 233 struct objfile *objfile = args->objfile;
c906108c
SS
234 if (sect->target_index == args->targ_index)
235 {
236 /* This is the section. Figure out what SECT_OFF_* code it is. */
237 if (bfd_get_section_flags (abfd, sect) & SEC_CODE)
b8fbeb18 238 *args->resultp = SECT_OFF_TEXT (objfile);
c906108c 239 else if (bfd_get_section_flags (abfd, sect) & SEC_LOAD)
b8fbeb18 240 *args->resultp = SECT_OFF_DATA (objfile);
c906108c 241 else
44af9391 242 *args->resultp = sect->index;
c906108c
SS
243 *args->bfd_sect = sect;
244 }
245}
246
247/* Return the section number (SECT_OFF_*) that CS points to. */
248static int
fba45db2 249secnum_to_section (int secnum, struct objfile *objfile)
c906108c 250{
b8fbeb18 251 int off = SECT_OFF_TEXT (objfile);
c906108c
SS
252 asection *sect = NULL;
253 struct find_targ_sec_arg args;
254 args.targ_index = secnum;
255 args.resultp = &off;
256 args.bfd_sect = &sect;
b8fbeb18 257 args.objfile = objfile;
c906108c
SS
258 bfd_map_over_sections (objfile->obfd, find_targ_sec, &args);
259 return off;
260}
261
262/* Return the BFD section that CS points to. */
263static asection *
fba45db2 264secnum_to_bfd_section (int secnum, struct objfile *objfile)
c906108c 265{
b8fbeb18 266 int off = SECT_OFF_TEXT (objfile);
c906108c
SS
267 asection *sect = NULL;
268 struct find_targ_sec_arg args;
269 args.targ_index = secnum;
270 args.resultp = &off;
271 args.bfd_sect = &sect;
7a78ae4e 272 args.objfile = objfile;
c906108c
SS
273 bfd_map_over_sections (objfile->obfd, find_targ_sec, &args);
274 return sect;
275}
276\f
277/* add a given stab string into given stab vector. */
278
279#if 0
280
281static void
fba45db2 282add_stab_to_list (char *stabname, struct pending_stabs **stabvector)
c906108c 283{
c5aa993b
JM
284 if (*stabvector == NULL)
285 {
286 *stabvector = (struct pending_stabs *)
287 xmalloc (sizeof (struct pending_stabs) +
288 INITIAL_STABVECTOR_LENGTH * sizeof (char *));
289 (*stabvector)->count = 0;
290 (*stabvector)->length = INITIAL_STABVECTOR_LENGTH;
291 }
292 else if ((*stabvector)->count >= (*stabvector)->length)
293 {
294 (*stabvector)->length += INITIAL_STABVECTOR_LENGTH;
295 *stabvector = (struct pending_stabs *)
296 xrealloc ((char *) *stabvector, sizeof (struct pending_stabs) +
297 (*stabvector)->length * sizeof (char *));
298 }
299 (*stabvector)->stab[(*stabvector)->count++] = stabname;
c906108c
SS
300}
301
302#endif
c5aa993b 303\f/* *INDENT-OFF* */
c906108c
SS
304/* Linenos are processed on a file-by-file basis.
305
306 Two reasons:
307
c5aa993b
JM
308 1) xlc (IBM's native c compiler) postpones static function code
309 emission to the end of a compilation unit. This way it can
310 determine if those functions (statics) are needed or not, and
311 can do some garbage collection (I think). This makes line
312 numbers and corresponding addresses unordered, and we end up
313 with a line table like:
314
315
316 lineno addr
317 foo() 10 0x100
318 20 0x200
319 30 0x300
320
321 foo3() 70 0x400
322 80 0x500
323 90 0x600
324
325 static foo2()
326 40 0x700
327 50 0x800
328 60 0x900
329
330 and that breaks gdb's binary search on line numbers, if the
331 above table is not sorted on line numbers. And that sort
332 should be on function based, since gcc can emit line numbers
333 like:
334
335 10 0x100 - for the init/test part of a for stmt.
336 20 0x200
337 30 0x300
338 10 0x400 - for the increment part of a for stmt.
339
340 arrange_linetable() will do this sorting.
341
342 2) aix symbol table might look like:
343
344 c_file // beginning of a new file
345 .bi // beginning of include file
346 .ei // end of include file
347 .bi
348 .ei
349
350 basically, .bi/.ei pairs do not necessarily encapsulate
351 their scope. They need to be recorded, and processed later
352 on when we come the end of the compilation unit.
353 Include table (inclTable) and process_linenos() handle
354 that. */
9846de1b 355/* *INDENT-ON* */
c906108c 356
c5aa993b
JM
357
358
c906108c
SS
359/* compare line table entry addresses. */
360
361static int
fba45db2 362compare_lte (const void *lte1p, const void *lte2p)
c906108c
SS
363{
364 struct linetable_entry *lte1 = (struct linetable_entry *) lte1p;
365 struct linetable_entry *lte2 = (struct linetable_entry *) lte2p;
366 return lte1->pc - lte2->pc;
367}
368
369/* Given a line table with function entries are marked, arrange its functions
370 in ascending order and strip off function entry markers and return it in
371 a newly created table. If the old one is good enough, return the old one. */
372/* FIXME: I think all this stuff can be replaced by just passing
373 sort_linevec = 1 to end_symtab. */
374
375static struct linetable *
b095261a 376arrange_linetable (struct linetable *oldLineTb)
c906108c 377{
c5aa993b
JM
378 int ii, jj, newline, /* new line count */
379 function_count; /* # of functions */
c906108c 380
c5aa993b
JM
381 struct linetable_entry *fentry; /* function entry vector */
382 int fentry_size; /* # of function entries */
383 struct linetable *newLineTb; /* new line table */
c906108c
SS
384
385#define NUM_OF_FUNCTIONS 20
386
387 fentry_size = NUM_OF_FUNCTIONS;
c5aa993b 388 fentry = (struct linetable_entry *)
c906108c
SS
389 xmalloc (fentry_size * sizeof (struct linetable_entry));
390
c5aa993b
JM
391 for (function_count = 0, ii = 0; ii < oldLineTb->nitems; ++ii)
392 {
c906108c 393
c5aa993b
JM
394 if (oldLineTb->item[ii].line == 0)
395 { /* function entry found. */
c906108c 396
c5aa993b
JM
397 if (function_count >= fentry_size)
398 { /* make sure you have room. */
399 fentry_size *= 2;
400 fentry = (struct linetable_entry *)
401 xrealloc (fentry, fentry_size * sizeof (struct linetable_entry));
402 }
403 fentry[function_count].line = ii;
404 fentry[function_count].pc = oldLineTb->item[ii].pc;
405 ++function_count;
406 }
c906108c 407 }
c906108c 408
c5aa993b
JM
409 if (function_count == 0)
410 {
b8c9b27d 411 xfree (fentry);
c5aa993b
JM
412 return oldLineTb;
413 }
c906108c 414 else if (function_count > 1)
c5aa993b 415 qsort (fentry, function_count, sizeof (struct linetable_entry), compare_lte);
c906108c
SS
416
417 /* allocate a new line table. */
418 newLineTb = (struct linetable *)
419 xmalloc
c5aa993b
JM
420 (sizeof (struct linetable) +
421 (oldLineTb->nitems - function_count) * sizeof (struct linetable_entry));
c906108c
SS
422
423 /* if line table does not start with a function beginning, copy up until
424 a function begin. */
425
426 newline = 0;
427 if (oldLineTb->item[0].line != 0)
c5aa993b
JM
428 for (newline = 0;
429 newline < oldLineTb->nitems && oldLineTb->item[newline].line; ++newline)
c906108c
SS
430 newLineTb->item[newline] = oldLineTb->item[newline];
431
432 /* Now copy function lines one by one. */
433
c5aa993b
JM
434 for (ii = 0; ii < function_count; ++ii)
435 {
436 for (jj = fentry[ii].line + 1;
437 jj < oldLineTb->nitems && oldLineTb->item[jj].line != 0;
438 ++jj, ++newline)
439 newLineTb->item[newline] = oldLineTb->item[jj];
440 }
b8c9b27d 441 xfree (fentry);
c906108c 442 newLineTb->nitems = oldLineTb->nitems - function_count;
c5aa993b
JM
443 return newLineTb;
444}
c906108c
SS
445
446/* include file support: C_BINCL/C_EINCL pairs will be kept in the
447 following `IncludeChain'. At the end of each symtab (end_symtab),
448 we will determine if we should create additional symtab's to
449 represent if (the include files. */
450
451
c5aa993b
JM
452typedef struct _inclTable
453{
454 char *name; /* include filename */
c906108c
SS
455
456 /* Offsets to the line table. end points to the last entry which is
457 part of this include file. */
c5aa993b
JM
458 int begin, end;
459
c906108c 460 struct subfile *subfile;
c5aa993b
JM
461 unsigned funStartLine; /* start line # of its function */
462}
463InclTable;
c906108c
SS
464
465#define INITIAL_INCLUDE_TABLE_LENGTH 20
c5aa993b
JM
466static InclTable *inclTable; /* global include table */
467static int inclIndx; /* last entry to table */
468static int inclLength; /* table length */
469static int inclDepth; /* nested include depth */
c906108c 470
a14ed312 471static void allocate_include_entry (void);
c906108c
SS
472
473static void
fba45db2 474record_include_begin (struct coff_symbol *cs)
c906108c
SS
475{
476 if (inclDepth)
477 {
478 /* In xcoff, we assume include files cannot be nested (not in .c files
c5aa993b 479 of course, but in corresponding .s files.). */
c906108c
SS
480
481 /* This can happen with old versions of GCC.
c5aa993b
JM
482 GCC 2.3.3-930426 does not exhibit this on a test case which
483 a user said produced the message for him. */
484 static struct complaint msg =
485 {"Nested C_BINCL symbols", 0, 0};
c906108c
SS
486 complain (&msg);
487 }
488 ++inclDepth;
489
490 allocate_include_entry ();
491
c5aa993b
JM
492 inclTable[inclIndx].name = cs->c_name;
493 inclTable[inclIndx].begin = cs->c_value;
c906108c
SS
494}
495
496static void
fba45db2 497record_include_end (struct coff_symbol *cs)
c906108c 498{
c5aa993b 499 InclTable *pTbl;
c906108c
SS
500
501 if (inclDepth == 0)
502 {
c5aa993b
JM
503 static struct complaint msg =
504 {"Mismatched C_BINCL/C_EINCL pair", 0, 0};
c906108c
SS
505 complain (&msg);
506 }
507
508 allocate_include_entry ();
509
c5aa993b 510 pTbl = &inclTable[inclIndx];
c906108c
SS
511 pTbl->end = cs->c_value;
512
513 --inclDepth;
514 ++inclIndx;
515}
516
517static void
fba45db2 518allocate_include_entry (void)
c906108c
SS
519{
520 if (inclTable == NULL)
521 {
c5aa993b 522 inclTable = (InclTable *)
c906108c
SS
523 xmalloc (sizeof (InclTable) * INITIAL_INCLUDE_TABLE_LENGTH);
524 memset (inclTable,
525 '\0', sizeof (InclTable) * INITIAL_INCLUDE_TABLE_LENGTH);
526 inclLength = INITIAL_INCLUDE_TABLE_LENGTH;
527 inclIndx = 0;
528 }
529 else if (inclIndx >= inclLength)
530 {
531 inclLength += INITIAL_INCLUDE_TABLE_LENGTH;
c5aa993b 532 inclTable = (InclTable *)
c906108c 533 xrealloc (inclTable, sizeof (InclTable) * inclLength);
c5aa993b
JM
534 memset (inclTable + inclLength - INITIAL_INCLUDE_TABLE_LENGTH,
535 '\0', sizeof (InclTable) * INITIAL_INCLUDE_TABLE_LENGTH);
c906108c
SS
536 }
537}
538
539/* Global variable to pass the psymtab down to all the routines involved
540 in psymtab to symtab processing. */
541static struct partial_symtab *this_symtab_psymtab;
542
543/* given the start and end addresses of a compilation unit (or a csect,
544 at times) process its lines and create appropriate line vectors. */
545
546static void
fba45db2 547process_linenos (CORE_ADDR start, CORE_ADDR end)
c906108c
SS
548{
549 int offset, ii;
550 file_ptr max_offset =
c5aa993b
JM
551 ((struct coff_symfile_info *) this_symtab_psymtab->objfile->sym_private)
552 ->max_lineno_offset;
c906108c
SS
553
554 /* subfile structure for the main compilation unit. */
555 struct subfile main_subfile;
556
557 /* In the main source file, any time we see a function entry, we
558 reset this variable to function's absolute starting line number.
559 All the following line numbers in the function are relative to
560 this, and we record absolute line numbers in record_line(). */
561
562 unsigned int main_source_baseline = 0;
563
564 unsigned *firstLine;
565
566 offset =
c5aa993b 567 ((struct symloc *) this_symtab_psymtab->read_symtab_private)->lineno_off;
c906108c
SS
568 if (offset == 0)
569 goto return_after_cleanup;
570
571 memset (&main_subfile, '\0', sizeof (main_subfile));
572
573 if (inclIndx == 0)
574 /* All source lines were in the main source file. None in include files. */
575
c5aa993b
JM
576 enter_line_range (&main_subfile, offset, 0, start, end,
577 &main_source_baseline);
c906108c
SS
578
579 else
580 {
581 /* There was source with line numbers in include files. */
7a78ae4e
ND
582
583 int linesz =
584 coff_data (this_symtab_psymtab->objfile->obfd)->local_linesz;
c906108c 585 main_source_baseline = 0;
7a78ae4e 586
c5aa993b 587 for (ii = 0; ii < inclIndx; ++ii)
c906108c
SS
588 {
589 struct subfile *tmpSubfile;
590
591 /* If there is main file source before include file, enter it. */
592 if (offset < inclTable[ii].begin)
593 {
594 enter_line_range
7a78ae4e 595 (&main_subfile, offset, inclTable[ii].begin - linesz,
c906108c
SS
596 start, 0, &main_source_baseline);
597 }
598
599 /* Have a new subfile for the include file. */
600
601 tmpSubfile = inclTable[ii].subfile =
602 (struct subfile *) xmalloc (sizeof (struct subfile));
603
604 memset (tmpSubfile, '\0', sizeof (struct subfile));
605 firstLine = &(inclTable[ii].funStartLine);
606
607 /* Enter include file's lines now. */
c5aa993b 608 enter_line_range (tmpSubfile, inclTable[ii].begin,
c906108c
SS
609 inclTable[ii].end, start, 0, firstLine);
610
611 if (offset <= inclTable[ii].end)
7a78ae4e 612 offset = inclTable[ii].end + linesz;
c906108c
SS
613 }
614
615 /* All the include files' line have been processed at this point. Now,
c5aa993b 616 enter remaining lines of the main file, if any left. */
7a78ae4e 617 if (offset < max_offset + 1 - linesz)
c906108c 618 {
c5aa993b 619 enter_line_range (&main_subfile, offset, 0, start, end,
c906108c
SS
620 &main_source_baseline);
621 }
622 }
623
624 /* Process main file's line numbers. */
625 if (main_subfile.line_vector)
626 {
627 struct linetable *lineTb, *lv;
628
629 lv = main_subfile.line_vector;
630
631 /* Line numbers are not necessarily ordered. xlc compilation will
c5aa993b 632 put static function to the end. */
c906108c
SS
633
634 lineTb = arrange_linetable (lv);
635 if (lv == lineTb)
636 {
637 current_subfile->line_vector = (struct linetable *)
638 xrealloc (lv, (sizeof (struct linetable)
639 + lv->nitems * sizeof (struct linetable_entry)));
640 }
641 else
642 {
b8c9b27d 643 xfree (lv);
c906108c
SS
644 current_subfile->line_vector = lineTb;
645 }
646
c5aa993b 647 current_subfile->line_vector_length =
c906108c
SS
648 current_subfile->line_vector->nitems;
649 }
650
651 /* Now, process included files' line numbers. */
652
c5aa993b 653 for (ii = 0; ii < inclIndx; ++ii)
c906108c 654 {
c5aa993b 655 if ((inclTable[ii].subfile)->line_vector) /* Useless if!!! FIXMEmgo */
c906108c
SS
656 {
657 struct linetable *lineTb, *lv;
658
659 lv = (inclTable[ii].subfile)->line_vector;
660
661 /* Line numbers are not necessarily ordered. xlc compilation will
662 put static function to the end. */
663
664 lineTb = arrange_linetable (lv);
665
666 push_subfile ();
667
668 /* For the same include file, we might want to have more than one
669 subfile. This happens if we have something like:
670
c5aa993b
JM
671 ......
672 #include "foo.h"
673 ......
674 #include "foo.h"
675 ......
c906108c
SS
676
677 while foo.h including code in it. (stupid but possible)
678 Since start_subfile() looks at the name and uses an
679 existing one if finds, we need to provide a fake name and
680 fool it. */
681
682#if 0
c5aa993b 683 start_subfile (inclTable[ii].name, (char *) 0);
c906108c
SS
684#else
685 {
686 /* Pick a fake name that will produce the same results as this
687 one when passed to deduce_language_from_filename. Kludge on
688 top of kludge. */
689 char *fakename = strrchr (inclTable[ii].name, '.');
690 if (fakename == NULL)
691 fakename = " ?";
c5aa993b 692 start_subfile (fakename, (char *) 0);
b8c9b27d 693 xfree (current_subfile->name);
c906108c 694 }
c2d11a7d 695 current_subfile->name = xstrdup (inclTable[ii].name);
c906108c
SS
696#endif
697
698 if (lv == lineTb)
699 {
700 current_subfile->line_vector =
701 (struct linetable *) xrealloc
c5aa993b
JM
702 (lv, (sizeof (struct linetable)
703 + lv->nitems * sizeof (struct linetable_entry)));
c906108c
SS
704
705 }
706 else
707 {
b8c9b27d 708 xfree (lv);
c906108c
SS
709 current_subfile->line_vector = lineTb;
710 }
711
c5aa993b 712 current_subfile->line_vector_length =
c906108c 713 current_subfile->line_vector->nitems;
c5aa993b 714 start_subfile (pop_subfile (), (char *) 0);
c906108c
SS
715 }
716 }
717
c5aa993b 718return_after_cleanup:
c906108c
SS
719
720 /* We don't want to keep alloc/free'ing the global include file table. */
721 inclIndx = 0;
722
723 /* Start with a fresh subfile structure for the next file. */
724 memset (&main_subfile, '\0', sizeof (struct subfile));
725}
726
727void
fba45db2 728aix_process_linenos (void)
c906108c
SS
729{
730 /* process line numbers and enter them into line vector */
731 process_linenos (last_source_start_addr, cur_src_end_addr);
732}
733
734
735/* Enter a given range of lines into the line vector.
736 can be called in the following two ways:
c5aa993b
JM
737 enter_line_range (subfile, beginoffset, endoffset, startaddr, 0, firstLine) or
738 enter_line_range (subfile, beginoffset, 0, startaddr, endaddr, firstLine)
c906108c
SS
739
740 endoffset points to the last line table entry that we should pay
741 attention to. */
742
743static void
fba45db2
KB
744enter_line_range (struct subfile *subfile, unsigned beginoffset, unsigned endoffset, /* offsets to line table */
745 CORE_ADDR startaddr, /* offsets to line table */
746 CORE_ADDR endaddr, unsigned *firstLine)
c906108c
SS
747{
748 unsigned int curoffset;
749 CORE_ADDR addr;
7a78ae4e 750 void *ext_lnno;
c906108c
SS
751 struct internal_lineno int_lnno;
752 unsigned int limit_offset;
753 bfd *abfd;
7a78ae4e 754 int linesz;
c906108c
SS
755
756 if (endoffset == 0 && startaddr == 0 && endaddr == 0)
757 return;
758 curoffset = beginoffset;
759 limit_offset =
c5aa993b
JM
760 ((struct coff_symfile_info *) this_symtab_psymtab->objfile->sym_private)
761 ->max_lineno_offset;
c906108c
SS
762
763 if (endoffset != 0)
764 {
765 if (endoffset >= limit_offset)
766 {
767 static struct complaint msg =
c5aa993b 768 {"Bad line table offset in C_EINCL directive", 0, 0};
c906108c
SS
769 complain (&msg);
770 return;
771 }
772 limit_offset = endoffset;
773 }
774 else
775 limit_offset -= 1;
7a78ae4e 776
c906108c 777 abfd = this_symtab_psymtab->objfile->obfd;
7a78ae4e
ND
778 linesz = coff_data (abfd)->local_linesz;
779 ext_lnno = alloca (linesz);
c906108c
SS
780
781 while (curoffset <= limit_offset)
782 {
783 bfd_seek (abfd, curoffset, SEEK_SET);
3a42e9d0 784 bfd_bread (ext_lnno, linesz, abfd);
7a78ae4e 785 bfd_coff_swap_lineno_in (abfd, ext_lnno, &int_lnno);
c906108c
SS
786
787 /* Find the address this line represents. */
788 addr = (int_lnno.l_lnno
789 ? int_lnno.l_addr.l_paddr
790 : read_symbol_nvalue (int_lnno.l_addr.l_symndx));
791 addr += ANOFFSET (this_symtab_psymtab->objfile->section_offsets,
b8fbeb18 792 SECT_OFF_TEXT (this_symtab_psymtab->objfile));
c906108c
SS
793
794 if (addr < startaddr || (endaddr && addr >= endaddr))
795 return;
796
797 if (int_lnno.l_lnno == 0)
798 {
799 *firstLine = read_symbol_lineno (int_lnno.l_addr.l_symndx);
800 record_line (subfile, 0, addr);
801 --(*firstLine);
802 }
803 else
804 record_line (subfile, *firstLine + int_lnno.l_lnno, addr);
7a78ae4e 805 curoffset += linesz;
c906108c
SS
806 }
807}
808
809
810/* Save the vital information for use when closing off the current file.
811 NAME is the file name the symbols came from, START_ADDR is the first
812 text address for the file, and SIZE is the number of bytes of text. */
813
814#define complete_symtab(name, start_addr) { \
815 last_source_file = savestring (name, strlen (name)); \
816 last_source_start_addr = start_addr; \
817}
818
819
820/* Refill the symbol table input buffer
821 and set the variables that control fetching entries from it.
822 Reports an error if no data available.
823 This function can read past the end of the symbol table
824 (into the string table) but this does no harm. */
825
826/* Reading symbol table has to be fast! Keep the followings as macros, rather
827 than functions. */
828
829#define RECORD_MINIMAL_SYMBOL(NAME, ADDR, TYPE, SECTION, OBJFILE) \
830{ \
831 char *namestr; \
832 namestr = (NAME); \
833 if (namestr[0] == '.') ++namestr; \
834 prim_record_minimal_symbol_and_info (namestr, (ADDR), (TYPE), \
835 (char *)NULL, (SECTION), (asection *)NULL, (OBJFILE)); \
836 misc_func_recorded = 1; \
837}
838
839
840/* xcoff has static blocks marked in `.bs', `.es' pairs. They cannot be
841 nested. At any given time, a symbol can only be in one static block.
842 This is the base address of current static block, zero if non exists. */
c5aa993b 843
c906108c
SS
844static int static_block_base = 0;
845
846/* Section number for the current static block. */
847
848static int static_block_section = -1;
849
850/* true if space for symbol name has been allocated. */
851
852static int symname_alloced = 0;
853
854/* Next symbol to read. Pointer into raw seething symbol table. */
855
856static char *raw_symbol;
857
858/* This is the function which stabsread.c calls to get symbol
859 continuations. */
860
861static char *
fba45db2 862xcoff_next_symbol_text (struct objfile *objfile)
c906108c
SS
863{
864 struct internal_syment symbol;
865 static struct complaint msg =
c5aa993b 866 {"Unexpected symbol continuation", 0, 0};
c906108c
SS
867 char *retval;
868 /* FIXME: is this the same as the passed arg? */
869 objfile = this_symtab_psymtab->objfile;
870
871 bfd_coff_swap_sym_in (objfile->obfd, raw_symbol, &symbol);
872 if (symbol.n_zeroes)
873 {
874 complain (&msg);
875
876 /* Return something which points to '\0' and hope the symbol reading
c5aa993b 877 code does something reasonable. */
c906108c
SS
878 retval = "";
879 }
880 else if (symbol.n_sclass & 0x80)
881 {
882 retval =
c5aa993b
JM
883 ((struct coff_symfile_info *) objfile->sym_private)->debugsec
884 + symbol.n_offset;
c906108c
SS
885 raw_symbol +=
886 coff_data (objfile->obfd)->local_symesz;
887 ++symnum;
888 }
889 else
890 {
891 complain (&msg);
892
893 /* Return something which points to '\0' and hope the symbol reading
c5aa993b 894 code does something reasonable. */
c906108c
SS
895 retval = "";
896 }
897 return retval;
898}
899
900/* Read symbols for a given partial symbol table. */
901
902static void
fba45db2 903read_xcoff_symtab (struct partial_symtab *pst)
c906108c
SS
904{
905 struct objfile *objfile = pst->objfile;
906 bfd *abfd = objfile->obfd;
907 char *raw_auxptr; /* Pointer to first raw aux entry for sym */
c5aa993b 908 char *strtbl = ((struct coff_symfile_info *) objfile->sym_private)->strtbl;
c906108c 909 char *debugsec =
c5aa993b 910 ((struct coff_symfile_info *) objfile->sym_private)->debugsec;
7a78ae4e 911 char *debugfmt = xcoff_data (abfd)->xcoff64 ? "XCOFF64" : "XCOFF";
c906108c
SS
912
913 struct internal_syment symbol[1];
914 union internal_auxent main_aux;
915 struct coff_symbol cs[1];
916 CORE_ADDR file_start_addr = 0;
917 CORE_ADDR file_end_addr = 0;
918
919 int next_file_symnum = -1;
920 unsigned int max_symnum;
921 int just_started = 1;
922 int depth = 0;
923 int fcn_start_addr = 0;
924
925 struct coff_symbol fcn_stab_saved;
926
927 /* fcn_cs_saved is global because process_xcoff_symbol needs it. */
928 union internal_auxent fcn_aux_saved;
929 struct context_stack *new;
930
931 char *filestring = " _start_ "; /* Name of the current file. */
932
c5aa993b 933 char *last_csect_name; /* last seen csect's name and value */
c906108c
SS
934 CORE_ADDR last_csect_val;
935 int last_csect_sec;
936
937 this_symtab_psymtab = pst;
938
939 /* Get the appropriate COFF "constants" related to the file we're
940 handling. */
941 local_symesz = coff_data (abfd)->local_symesz;
942
943 last_source_file = NULL;
944 last_csect_name = 0;
945 last_csect_val = 0;
946
947 start_stabs ();
c5aa993b 948 start_symtab (filestring, (char *) NULL, file_start_addr);
7a78ae4e 949 record_debugformat (debugfmt);
c5aa993b 950 symnum = ((struct symloc *) pst->read_symtab_private)->first_symnum;
c906108c 951 max_symnum =
c5aa993b 952 symnum + ((struct symloc *) pst->read_symtab_private)->numsyms;
c906108c
SS
953 first_object_file_end = 0;
954
955 raw_symbol =
956 ((struct coff_symfile_info *) objfile->sym_private)->symtbl
c5aa993b 957 + symnum * local_symesz;
c906108c
SS
958
959 while (symnum < max_symnum)
960 {
961
962 QUIT; /* make this command interruptable. */
963
964 /* READ_ONE_SYMBOL (symbol, cs, symname_alloced); */
965 /* read one symbol into `cs' structure. After processing the
c5aa993b
JM
966 whole symbol table, only string table will be kept in memory,
967 symbol table and debug section of xcoff will be freed. Thus
968 we can mark symbols with names in string table as
969 `alloced'. */
c906108c
SS
970 {
971 int ii;
972
973 /* Swap and align the symbol into a reasonable C structure. */
974 bfd_coff_swap_sym_in (abfd, raw_symbol, symbol);
975
976 cs->c_symnum = symnum;
977 cs->c_naux = symbol->n_numaux;
978 if (symbol->n_zeroes)
979 {
980 symname_alloced = 0;
981 /* We must use the original, unswapped, name here so the name field
982 pointed to by cs->c_name will persist throughout xcoffread. If
983 we use the new field, it gets overwritten for each symbol. */
c5aa993b 984 cs->c_name = ((struct external_syment *) raw_symbol)->e.e_name;
c906108c
SS
985 /* If it's exactly E_SYMNMLEN characters long it isn't
986 '\0'-terminated. */
987 if (cs->c_name[E_SYMNMLEN - 1] != '\0')
988 {
989 char *p;
990 p = obstack_alloc (&objfile->symbol_obstack, E_SYMNMLEN + 1);
991 strncpy (p, cs->c_name, E_SYMNMLEN);
992 p[E_SYMNMLEN] = '\0';
993 cs->c_name = p;
994 symname_alloced = 1;
995 }
996 }
997 else if (symbol->n_sclass & 0x80)
998 {
999 cs->c_name = debugsec + symbol->n_offset;
1000 symname_alloced = 0;
1001 }
1002 else
1003 {
1004 /* in string table */
c5aa993b 1005 cs->c_name = strtbl + (int) symbol->n_offset;
c906108c
SS
1006 symname_alloced = 1;
1007 }
1008 cs->c_value = symbol->n_value;
1009 cs->c_sclass = symbol->n_sclass;
1010 cs->c_secnum = symbol->n_scnum;
c5aa993b 1011 cs->c_type = (unsigned) symbol->n_type;
c906108c 1012
7a78ae4e 1013 raw_symbol += local_symesz;
c906108c
SS
1014 ++symnum;
1015
1016 /* Save addr of first aux entry. */
1017 raw_auxptr = raw_symbol;
1018
1019 /* Skip all the auxents associated with this symbol. */
1020 for (ii = symbol->n_numaux; ii; --ii)
1021 {
1022 raw_symbol += coff_data (abfd)->local_auxesz;
1023 ++symnum;
1024 }
1025 }
1026
1027 /* if symbol name starts with ".$" or "$", ignore it. */
1028 if (cs->c_name[0] == '$'
1029 || (cs->c_name[1] == '$' && cs->c_name[0] == '.'))
1030 continue;
1031
1032 if (cs->c_symnum == next_file_symnum && cs->c_sclass != C_FILE)
1033 {
1034 if (last_source_file)
1035 {
1036 pst->symtab =
b8fbeb18 1037 end_symtab (cur_src_end_addr, objfile, SECT_OFF_TEXT (objfile));
c906108c
SS
1038 end_stabs ();
1039 }
1040
1041 start_stabs ();
c5aa993b 1042 start_symtab ("_globals_", (char *) NULL, (CORE_ADDR) 0);
7a78ae4e 1043 record_debugformat (debugfmt);
c906108c
SS
1044 cur_src_end_addr = first_object_file_end;
1045 /* done with all files, everything from here on is globals */
1046 }
1047
c906108c
SS
1048 if ((cs->c_sclass == C_EXT || cs->c_sclass == C_HIDEXT)
1049 && cs->c_naux == 1)
1050 {
1051 /* Dealing with a symbol with a csect entry. */
1052
1053#define CSECT(PP) ((PP)->x_csect)
1054#define CSECT_LEN(PP) (CSECT(PP).x_scnlen.l)
1055#define CSECT_ALIGN(PP) (SMTYP_ALIGN(CSECT(PP).x_smtyp))
1056#define CSECT_SMTYP(PP) (SMTYP_SMTYP(CSECT(PP).x_smtyp))
1057#define CSECT_SCLAS(PP) (CSECT(PP).x_smclas)
1058
1059 /* Convert the auxent to something we can access. */
1060 bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
1061 0, cs->c_naux, &main_aux);
1062
1063 switch (CSECT_SMTYP (&main_aux))
1064 {
1065
1066 case XTY_ER:
1067 /* Ignore all external references. */
1068 continue;
1069
1070 case XTY_SD:
1071 /* A section description. */
1072 {
1073 switch (CSECT_SCLAS (&main_aux))
1074 {
1075
1076 case XMC_PR:
1077 {
1078
1079 /* A program csect is seen. We have to allocate one
c5aa993b
JM
1080 symbol table for each program csect. Normally gdb
1081 prefers one symtab for each source file. In case
1082 of AIX, one source file might include more than one
1083 [PR] csect, and they don't have to be adjacent in
1084 terms of the space they occupy in memory. Thus, one
1085 single source file might get fragmented in the
1086 memory and gdb's file start and end address
1087 approach does not work! GCC (and I think xlc) seem
1088 to put all the code in the unnamed program csect. */
c906108c
SS
1089
1090 if (last_csect_name)
1091 {
1092 complete_symtab (filestring, file_start_addr);
1093 cur_src_end_addr = file_end_addr;
b8fbeb18 1094 end_symtab (file_end_addr, objfile, SECT_OFF_TEXT (objfile));
c906108c
SS
1095 end_stabs ();
1096 start_stabs ();
1097 /* Give all csects for this source file the same
1098 name. */
c5aa993b 1099 start_symtab (filestring, NULL, (CORE_ADDR) 0);
7a78ae4e 1100 record_debugformat (debugfmt);
c906108c
SS
1101 }
1102
1103 /* If this is the very first csect seen,
c5aa993b 1104 basically `__start'. */
c906108c
SS
1105 if (just_started)
1106 {
1107 first_object_file_end
1108 = cs->c_value + CSECT_LEN (&main_aux);
1109 just_started = 0;
1110 }
1111
1112 file_start_addr =
1113 cs->c_value + ANOFFSET (objfile->section_offsets,
b8fbeb18 1114 SECT_OFF_TEXT (objfile));
c906108c
SS
1115 file_end_addr = file_start_addr + CSECT_LEN (&main_aux);
1116
977adac5
ND
1117 if (cs->c_name && (cs->c_name[0] == '.'
1118 || cs->c_name[0] == '@'))
c906108c
SS
1119 {
1120 last_csect_name = cs->c_name;
1121 last_csect_val = cs->c_value;
1122 last_csect_sec = secnum_to_section (cs->c_secnum, objfile);
1123 }
1124 }
1125 continue;
1126
1127 /* All other symbols are put into the minimal symbol
1128 table only. */
1129
1130 case XMC_RW:
1131 continue;
1132
1133 case XMC_TC0:
1134 continue;
1135
1136 case XMC_TC:
1137 continue;
1138
1139 default:
1140 /* Ignore the symbol. */
1141 continue;
1142 }
1143 }
1144 break;
1145
1146 case XTY_LD:
1147
1148 switch (CSECT_SCLAS (&main_aux))
1149 {
1150 case XMC_PR:
1151 /* a function entry point. */
1152 function_entry_point:
1153
1154 fcn_start_addr = cs->c_value;
1155
1156 /* save the function header info, which will be used
1157 when `.bf' is seen. */
1158 fcn_cs_saved = *cs;
1159 fcn_aux_saved = main_aux;
1160 continue;
1161
1162 case XMC_GL:
1163 /* shared library function trampoline code entry point. */
1164 continue;
1165
1166 case XMC_DS:
1167 /* The symbols often have the same names as debug symbols for
1168 functions, and confuse lookup_symbol. */
1169 continue;
1170
1171 default:
1172 /* xlc puts each variable in a separate csect, so we get
1173 an XTY_SD for each variable. But gcc puts several
1174 variables in a csect, so that each variable only gets
1175 an XTY_LD. This will typically be XMC_RW; I suspect
1176 XMC_RO and XMC_BS might be possible too.
1177 These variables are put in the minimal symbol table
1178 only. */
1179 continue;
1180 }
1181 break;
1182
1183 case XTY_CM:
1184 /* Common symbols are put into the minimal symbol table only. */
1185 continue;
1186
1187 default:
1188 break;
1189 }
1190 }
1191
977adac5
ND
1192 /* If explicitly specified as a function, treat is as one. This check
1193 evaluates to true for @FIX* bigtoc CSECT symbols, so it must occur
1194 after the above CSECT check. */
1195 if (ISFCN (cs->c_type) && cs->c_sclass != C_TPDEF)
1196 {
1197 bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
1198 0, cs->c_naux, &main_aux);
1199 goto function_entry_point;
1200 }
1201
c906108c
SS
1202 switch (cs->c_sclass)
1203 {
1204
1205 case C_FILE:
1206
1207 /* c_value field contains symnum of next .file entry in table
1208 or symnum of first global after last .file. */
1209
1210 next_file_symnum = cs->c_value;
1211
1212 /* Complete symbol table for last object file containing
1213 debugging information. */
1214
1215 /* Whether or not there was a csect in the previous file, we
1216 have to call `end_stabs' and `start_stabs' to reset
1217 type_vector, line_vector, etc. structures. */
1218
1219 complete_symtab (filestring, file_start_addr);
1220 cur_src_end_addr = file_end_addr;
b8fbeb18 1221 end_symtab (file_end_addr, objfile, SECT_OFF_TEXT (objfile));
c906108c
SS
1222 end_stabs ();
1223
1224 /* XCOFF, according to the AIX 3.2 documentation, puts the filename
1225 in cs->c_name. But xlc 1.3.0.2 has decided to do things the
1226 standard COFF way and put it in the auxent. We use the auxent if
1227 the symbol is ".file" and an auxent exists, otherwise use the symbol
1228 itself. Simple enough. */
1229 if (!strcmp (cs->c_name, ".file") && cs->c_naux > 0)
1230 {
1231 bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
1232 0, cs->c_naux, &main_aux);
1233 filestring = coff_getfilename (&main_aux, objfile);
1234 }
1235 else
1236 filestring = cs->c_name;
1237
1238 start_stabs ();
c5aa993b 1239 start_symtab (filestring, (char *) NULL, (CORE_ADDR) 0);
7a78ae4e 1240 record_debugformat (debugfmt);
c906108c
SS
1241 last_csect_name = 0;
1242
1243 /* reset file start and end addresses. A compilation unit with no text
1244 (only data) should have zero file boundaries. */
1245 file_start_addr = file_end_addr = 0;
1246 break;
1247
1248 case C_FUN:
1249 fcn_stab_saved = *cs;
1250 break;
1251
1252 case C_FCN:
1253 if (STREQ (cs->c_name, ".bf"))
1254 {
1255 CORE_ADDR off = ANOFFSET (objfile->section_offsets,
b8fbeb18 1256 SECT_OFF_TEXT (objfile));
c906108c
SS
1257 bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
1258 0, cs->c_naux, &main_aux);
1259
1260 within_function = 1;
1261
1262 new = push_context (0, fcn_start_addr + off);
1263
c5aa993b 1264 new->name = define_symbol
c906108c
SS
1265 (fcn_cs_saved.c_value + off,
1266 fcn_stab_saved.c_name, 0, 0, objfile);
1267 if (new->name != NULL)
b8fbeb18 1268 SYMBOL_SECTION (new->name) = SECT_OFF_TEXT (objfile);
c906108c
SS
1269 }
1270 else if (STREQ (cs->c_name, ".ef"))
1271 {
1272
1273 bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
1274 0, cs->c_naux, &main_aux);
1275
1276 /* The value of .ef is the address of epilogue code;
c5aa993b 1277 not useful for gdb. */
c906108c 1278 /* { main_aux.x_sym.x_misc.x_lnsz.x_lnno
c5aa993b 1279 contains number of lines to '}' */
c906108c
SS
1280
1281 if (context_stack_depth <= 0)
1282 { /* We attempted to pop an empty context stack */
1283 complain (&ef_complaint, cs->c_symnum);
1284 within_function = 0;
1285 break;
1286 }
1287 new = pop_context ();
1288 /* Stack must be empty now. */
1289 if (context_stack_depth > 0 || new == NULL)
1290 {
1291 complain (&ef_complaint, cs->c_symnum);
1292 within_function = 0;
1293 break;
1294 }
1295
1296 finish_block (new->name, &local_symbols, new->old_blocks,
1297 new->start_addr,
1298 (fcn_cs_saved.c_value
1299 + fcn_aux_saved.x_sym.x_misc.x_fsize
1300 + ANOFFSET (objfile->section_offsets,
b8fbeb18 1301 SECT_OFF_TEXT (objfile))),
c906108c
SS
1302 objfile);
1303 within_function = 0;
1304 }
1305 break;
1306
1307 case C_BSTAT:
1308 /* Begin static block. */
1309 {
1310 struct internal_syment symbol;
1311
1312 read_symbol (&symbol, cs->c_value);
1313 static_block_base = symbol.n_value;
1314 static_block_section =
1315 secnum_to_section (symbol.n_scnum, objfile);
1316 }
1317 break;
1318
1319 case C_ESTAT:
1320 /* End of static block. */
1321 static_block_base = 0;
1322 static_block_section = -1;
1323 break;
1324
1325 case C_ARG:
1326 case C_REGPARM:
1327 case C_REG:
1328 case C_TPDEF:
1329 case C_STRTAG:
1330 case C_UNTAG:
1331 case C_ENTAG:
1332 {
1333 static struct complaint msg =
c5aa993b 1334 {"Unrecognized storage class %d.", 0, 0};
c906108c
SS
1335 complain (&msg, cs->c_sclass);
1336 }
1337 break;
1338
1339 case C_LABEL:
1340 case C_NULL:
1341 /* Ignore these. */
1342 break;
1343
1344 case C_HIDEXT:
1345 case C_STAT:
1346 break;
1347
1348 case C_BINCL:
1349 /* beginning of include file */
1350 /* In xlc output, C_BINCL/C_EINCL pair doesn't show up in sorted
1351 order. Thus, when wee see them, we might not know enough info
1352 to process them. Thus, we'll be saving them into a table
1353 (inclTable) and postpone their processing. */
1354
1355 record_include_begin (cs);
1356 break;
1357
1358 case C_EINCL:
1359 /* End of include file. */
1360 /* See the comment after case C_BINCL. */
1361 record_include_end (cs);
1362 break;
1363
1364 case C_BLOCK:
1365 if (STREQ (cs->c_name, ".bb"))
1366 {
1367 depth++;
1368 new = push_context (depth,
1369 (cs->c_value
1370 + ANOFFSET (objfile->section_offsets,
b8fbeb18 1371 SECT_OFF_TEXT (objfile))));
c906108c
SS
1372 }
1373 else if (STREQ (cs->c_name, ".eb"))
1374 {
1375 if (context_stack_depth <= 0)
1376 { /* We attempted to pop an empty context stack */
1377 complain (&eb_complaint, cs->c_symnum);
1378 break;
1379 }
1380 new = pop_context ();
1381 if (depth-- != new->depth)
1382 {
1383 complain (&eb_complaint, cs->c_symnum);
1384 break;
1385 }
1386 if (local_symbols && context_stack_depth > 0)
1387 {
1388 /* Make a block for the local symbols within. */
1389 finish_block (new->name, &local_symbols, new->old_blocks,
1390 new->start_addr,
1391 (cs->c_value
1392 + ANOFFSET (objfile->section_offsets,
b8fbeb18 1393 SECT_OFF_TEXT (objfile))),
c906108c
SS
1394 objfile);
1395 }
1396 local_symbols = new->locals;
1397 }
1398 break;
1399
1400 default:
1401 process_xcoff_symbol (cs, objfile);
1402 break;
1403 }
1404 }
1405
1406 if (last_source_file)
1407 {
1408 struct symtab *s;
1409
1410 complete_symtab (filestring, file_start_addr);
1411 cur_src_end_addr = file_end_addr;
b8fbeb18 1412 s = end_symtab (file_end_addr, objfile, SECT_OFF_TEXT (objfile));
c906108c
SS
1413 /* When reading symbols for the last C_FILE of the objfile, try
1414 to make sure that we set pst->symtab to the symtab for the
1415 file, not to the _globals_ symtab. I'm not sure whether this
1416 actually works right or when/if it comes up. */
1417 if (pst->symtab == NULL)
1418 pst->symtab = s;
1419 end_stabs ();
1420 }
1421}
1422
1423#define SYMBOL_DUP(SYMBOL1, SYMBOL2) \
1424 (SYMBOL2) = (struct symbol *) \
1425 obstack_alloc (&objfile->symbol_obstack, sizeof (struct symbol)); \
1426 *(SYMBOL2) = *(SYMBOL1);
c5aa993b
JM
1427
1428
c906108c
SS
1429#define SYMNAME_ALLOC(NAME, ALLOCED) \
1430 (ALLOCED) ? (NAME) : obsavestring ((NAME), strlen (NAME), &objfile->symbol_obstack);
1431
1432
1433static struct type *func_symbol_type;
1434static struct type *var_symbol_type;
1435
1436/* process one xcoff symbol. */
1437
1438static struct symbol *
fba45db2 1439process_xcoff_symbol (register struct coff_symbol *cs, struct objfile *objfile)
c906108c
SS
1440{
1441 struct symbol onesymbol;
1442 register struct symbol *sym = &onesymbol;
1443 struct symbol *sym2 = NULL;
1444 char *name, *pp;
1445
1446 int sec;
1447 CORE_ADDR off;
1448
1449 if (cs->c_secnum < 0)
1450 {
1451 /* The value is a register number, offset within a frame, etc.,
c5aa993b 1452 and does not get relocated. */
c906108c
SS
1453 off = 0;
1454 sec = -1;
1455 }
1456 else
1457 {
1458 sec = secnum_to_section (cs->c_secnum, objfile);
1459 off = ANOFFSET (objfile->section_offsets, sec);
1460 }
1461
1462 name = cs->c_name;
1463 if (name[0] == '.')
1464 ++name;
1465
1466 memset (sym, '\0', sizeof (struct symbol));
1467
1468 /* default assumptions */
7a78ae4e 1469 SYMBOL_VALUE_ADDRESS (sym) = cs->c_value + off;
c906108c
SS
1470 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1471 SYMBOL_SECTION (sym) = secnum_to_section (cs->c_secnum, objfile);
1472
1473 if (ISFCN (cs->c_type))
1474 {
1475 /* At this point, we don't know the type of the function. This
c5aa993b
JM
1476 will be patched with the type from its stab entry later on in
1477 patch_block_stabs (), unless the file was compiled without -g. */
c906108c
SS
1478
1479 SYMBOL_NAME (sym) = SYMNAME_ALLOC (name, symname_alloced);
1480 SYMBOL_TYPE (sym) = func_symbol_type;
1481
1482 SYMBOL_CLASS (sym) = LOC_BLOCK;
1483 SYMBOL_DUP (sym, sym2);
1484
1485 if (cs->c_sclass == C_EXT)
1486 add_symbol_to_list (sym2, &global_symbols);
1487 else if (cs->c_sclass == C_HIDEXT || cs->c_sclass == C_STAT)
1488 add_symbol_to_list (sym2, &file_symbols);
1489 }
1490 else
1491 {
1492 /* In case we can't figure out the type, provide default. */
1493 SYMBOL_TYPE (sym) = var_symbol_type;
1494
1495 switch (cs->c_sclass)
1496 {
1497#if 0
c5aa993b
JM
1498 /* The values of functions and global symbols are now resolved
1499 via the global_sym_chain in stabsread.c. */
c906108c
SS
1500 case C_FUN:
1501 if (fcn_cs_saved.c_sclass == C_EXT)
1502 add_stab_to_list (name, &global_stabs);
1503 else
1504 add_stab_to_list (name, &file_stabs);
1505 break;
1506
1507 case C_GSYM:
1508 add_stab_to_list (name, &global_stabs);
1509 break;
1510#endif
1511
1512 case C_BCOMM:
1513 common_block_start (cs->c_name, objfile);
1514 break;
1515
1516 case C_ECOMM:
1517 common_block_end (objfile);
1518 break;
1519
1520 default:
1521 complain (&storclass_complaint, cs->c_sclass);
1522 /* FALLTHROUGH */
1523
1524 case C_DECL:
1525 case C_PSYM:
1526 case C_RPSYM:
1527 case C_ECOML:
1528 case C_LSYM:
1529 case C_RSYM:
1530 case C_GSYM:
1531
1532 {
1533 sym = define_symbol (cs->c_value + off, cs->c_name, 0, 0, objfile);
1534 if (sym != NULL)
1535 {
1536 SYMBOL_SECTION (sym) = sec;
1537 }
1538 return sym;
1539 }
1540
1541 case C_STSYM:
1542
1543 /* For xlc (not GCC), the 'V' symbol descriptor is used for
1544 all statics and we need to distinguish file-scope versus
1545 function-scope using within_function. We do this by
1546 changing the string we pass to define_symbol to use 'S'
1547 where we need to, which is not necessarily super-clean,
1548 but seems workable enough. */
1549
c5aa993b 1550 if (*name == ':' || (pp = (char *) strchr (name, ':')) == NULL)
c906108c
SS
1551 return NULL;
1552
1553 ++pp;
1554 if (*pp == 'V' && !within_function)
1555 *pp = 'S';
1556 sym = define_symbol ((cs->c_value
1557 + ANOFFSET (objfile->section_offsets,
1558 static_block_section)),
1559 cs->c_name, 0, 0, objfile);
1560 if (sym != NULL)
1561 {
7a78ae4e 1562 SYMBOL_VALUE_ADDRESS (sym) += static_block_base;
c906108c
SS
1563 SYMBOL_SECTION (sym) = static_block_section;
1564 }
1565 return sym;
1566
1567 }
1568 }
1569 return sym2;
1570}
1571
1572/* Extract the file name from the aux entry of a C_FILE symbol.
1573 Result is in static storage and is only good for temporary use. */
1574
1575static char *
fba45db2 1576coff_getfilename (union internal_auxent *aux_entry, struct objfile *objfile)
c906108c
SS
1577{
1578 static char buffer[BUFSIZ];
1579
1580 if (aux_entry->x_file.x_n.x_zeroes == 0)
1581 strcpy (buffer,
c5aa993b 1582 ((struct coff_symfile_info *) objfile->sym_private)->strtbl
c906108c
SS
1583 + aux_entry->x_file.x_n.x_offset);
1584 else
1585 {
1586 strncpy (buffer, aux_entry->x_file.x_fname, FILNMLEN);
1587 buffer[FILNMLEN] = '\0';
1588 }
1589 return (buffer);
1590}
1591
1592/* Set *SYMBOL to symbol number symno in symtbl. */
1593static void
fba45db2 1594read_symbol (struct internal_syment *symbol, int symno)
c906108c
SS
1595{
1596 int nsyms =
c5aa993b
JM
1597 ((struct coff_symfile_info *) this_symtab_psymtab->objfile->sym_private)
1598 ->symtbl_num_syms;
1599 char *stbl =
1600 ((struct coff_symfile_info *) this_symtab_psymtab->objfile->sym_private)
1601 ->symtbl;
c906108c
SS
1602 if (symno < 0 || symno >= nsyms)
1603 {
1604 static struct complaint msg =
c5aa993b 1605 {"Invalid symbol offset", 0, 0};
c906108c
SS
1606 complain (&msg);
1607 symbol->n_value = 0;
1608 symbol->n_scnum = -1;
1609 return;
1610 }
1611 bfd_coff_swap_sym_in (this_symtab_psymtab->objfile->obfd,
c5aa993b 1612 stbl + (symno * local_symesz),
c906108c
SS
1613 symbol);
1614}
c5aa993b 1615
c906108c
SS
1616/* Get value corresponding to symbol number symno in symtbl. */
1617
470d5666 1618static CORE_ADDR
fba45db2 1619read_symbol_nvalue (int symno)
c906108c
SS
1620{
1621 struct internal_syment symbol[1];
1622
1623 read_symbol (symbol, symno);
c5aa993b 1624 return symbol->n_value;
c906108c
SS
1625}
1626
1627
1628/* Find the address of the function corresponding to symno, where
1629 symno is the symbol pointed to by the linetable. */
1630
1631static int
fba45db2 1632read_symbol_lineno (int symno)
c906108c 1633{
7a78ae4e
ND
1634 struct objfile *objfile = this_symtab_psymtab->objfile;
1635 boolean xcoff64 = xcoff_data (objfile->obfd)->xcoff64;
1636
1637 struct coff_symfile_info *info =
1638 (struct coff_symfile_info *)objfile->sym_private;
1639 int nsyms = info->symtbl_num_syms;
1640 char *stbl = info->symtbl;
1641 char *strtbl = info->strtbl;
1642
c906108c
SS
1643 struct internal_syment symbol[1];
1644 union internal_auxent main_aux[1];
1645
1646 if (symno < 0)
1647 {
1648 complain (&bf_notfound_complaint);
1649 return 0;
1650 }
1651
1652 /* Note that just searching for a short distance (e.g. 50 symbols)
1653 is not enough, at least in the following case.
1654
1655 .extern foo
1656 [many .stabx entries]
1657 [a few functions, referring to foo]
1658 .globl foo
1659 .bf
1660
1661 What happens here is that the assembler moves the .stabx entries
1662 to right before the ".bf" for foo, but the symbol for "foo" is before
1663 all the stabx entries. See PR gdb/2222. */
1664
1665 /* Maintaining a table of .bf entries might be preferable to this search.
1666 If I understand things correctly it would need to be done only for
1667 the duration of a single psymtab to symtab conversion. */
1668 while (symno < nsyms)
1669 {
1670 bfd_coff_swap_sym_in (symfile_bfd,
1671 stbl + (symno * local_symesz), symbol);
7a78ae4e
ND
1672 if (symbol->n_sclass == C_FCN)
1673 {
1674 char *name = xcoff64 ? strtbl + symbol->n_offset : symbol->n_name;
1675 if (STREQ (name, ".bf"))
1676 goto gotit;
1677 }
c906108c
SS
1678 symno += symbol->n_numaux + 1;
1679 }
1680
1681 complain (&bf_notfound_complaint);
1682 return 0;
1683
1684gotit:
1685 /* take aux entry and return its lineno */
1686 symno++;
7a78ae4e 1687 bfd_coff_swap_aux_in (objfile->obfd, stbl + symno * local_symesz,
c906108c
SS
1688 symbol->n_type, symbol->n_sclass,
1689 0, symbol->n_numaux, main_aux);
1690
1691 return main_aux->x_sym.x_misc.x_lnsz.x_lnno;
1692}
1693
1694/* Support for line number handling */
1695
1696/* This function is called for every section; it finds the outer limits
1697 * of the line table (minimum and maximum file offset) so that the
1698 * mainline code can read the whole thing for efficiency.
1699 */
1700static void
fba45db2 1701find_linenos (bfd *abfd, sec_ptr asect, PTR vpinfo)
c906108c
SS
1702{
1703 struct coff_symfile_info *info;
1704 int size, count;
1705 file_ptr offset, maxoff;
1706
1707 count = asect->lineno_count;
1708
1709 if (!STREQ (asect->name, ".text") || count == 0)
1710 return;
1711
1712 size = count * coff_data (abfd)->local_linesz;
c5aa993b 1713 info = (struct coff_symfile_info *) vpinfo;
c906108c
SS
1714 offset = asect->line_filepos;
1715 maxoff = offset + size;
1716
1717 if (offset < info->min_lineno_offset || info->min_lineno_offset == 0)
1718 info->min_lineno_offset = offset;
1719
1720 if (maxoff > info->max_lineno_offset)
1721 info->max_lineno_offset = maxoff;
1722}
1723\f
a14ed312 1724static void xcoff_psymtab_to_symtab_1 (struct partial_symtab *);
c906108c
SS
1725
1726static void
fba45db2 1727xcoff_psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c
SS
1728{
1729 struct cleanup *old_chain;
1730 int i;
c5aa993b 1731
c906108c
SS
1732 if (!pst)
1733 return;
1734
1735 if (pst->readin)
1736 {
1737 fprintf_unfiltered
1738 (gdb_stderr, "Psymtab for %s already read in. Shouldn't happen.\n",
1739 pst->filename);
1740 return;
1741 }
1742
1743 /* Read in all partial symtabs on which this one is dependent */
1744 for (i = 0; i < pst->number_of_dependencies; i++)
1745 if (!pst->dependencies[i]->readin)
1746 {
1747 /* Inform about additional files that need to be read in. */
1748 if (info_verbose)
1749 {
1750 fputs_filtered (" ", gdb_stdout);
1751 wrap_here ("");
1752 fputs_filtered ("and ", gdb_stdout);
1753 wrap_here ("");
1754 printf_filtered ("%s...", pst->dependencies[i]->filename);
c5aa993b 1755 wrap_here (""); /* Flush output */
c906108c
SS
1756 gdb_flush (gdb_stdout);
1757 }
1758 xcoff_psymtab_to_symtab_1 (pst->dependencies[i]);
1759 }
1760
c5aa993b 1761 if (((struct symloc *) pst->read_symtab_private)->numsyms != 0)
c906108c
SS
1762 {
1763 /* Init stuff necessary for reading in symbols. */
1764 stabsread_init ();
1765 buildsym_init ();
a0b3c4fd 1766 old_chain = make_cleanup (really_free_pendings, 0);
c906108c
SS
1767
1768 read_xcoff_symtab (pst);
1769 sort_symtab_syms (pst->symtab);
1770
1771 do_cleanups (old_chain);
1772 }
1773
1774 pst->readin = 1;
1775}
1776
a14ed312 1777static void xcoff_psymtab_to_symtab (struct partial_symtab *);
c906108c
SS
1778
1779/* Read in all of the symbols for a given psymtab for real.
1780 Be verbose about it if the user wants that. */
1781
1782static void
fba45db2 1783xcoff_psymtab_to_symtab (struct partial_symtab *pst)
c906108c
SS
1784{
1785 bfd *sym_bfd;
1786
1787 if (!pst)
1788 return;
1789
1790 if (pst->readin)
1791 {
1792 fprintf_unfiltered
1793 (gdb_stderr, "Psymtab for %s already read in. Shouldn't happen.\n",
1794 pst->filename);
1795 return;
1796 }
1797
c5aa993b 1798 if (((struct symloc *) pst->read_symtab_private)->numsyms != 0
c906108c
SS
1799 || pst->number_of_dependencies)
1800 {
1801 /* Print the message now, before reading the string table,
c5aa993b 1802 to avoid disconcerting pauses. */
c906108c
SS
1803 if (info_verbose)
1804 {
1805 printf_filtered ("Reading in symbols for %s...", pst->filename);
1806 gdb_flush (gdb_stdout);
1807 }
1808
1809 sym_bfd = pst->objfile->obfd;
1810
1811 next_symbol_text_func = xcoff_next_symbol_text;
1812
1813 xcoff_psymtab_to_symtab_1 (pst);
1814
1815 /* Match with global symbols. This only needs to be done once,
1816 after all of the symtabs and dependencies have been read in. */
1817 scan_file_globals (pst->objfile);
1818
1819 /* Finish up the debug error message. */
1820 if (info_verbose)
1821 printf_filtered ("done.\n");
1822 }
1823}
1824\f
1825static void
fba45db2 1826xcoff_new_init (struct objfile *objfile)
c906108c
SS
1827{
1828 stabsread_new_init ();
1829 buildsym_new_init ();
1830}
1831
1832/* Do initialization in preparation for reading symbols from OBJFILE.
c5aa993b 1833
c906108c
SS
1834 We will only be called if this is an XCOFF or XCOFF-like file.
1835 BFD handles figuring out the format of the file, and code in symfile.c
1836 uses BFD's determination to vector to us. */
1837
1838static void
fba45db2 1839xcoff_symfile_init (struct objfile *objfile)
c906108c
SS
1840{
1841 /* Allocate struct to keep track of the symfile */
c5aa993b
JM
1842 objfile->sym_private = xmmalloc (objfile->md,
1843 sizeof (struct coff_symfile_info));
c906108c
SS
1844
1845 /* XCOFF objects may be reordered, so set OBJF_REORDERED. If we
1846 find this causes a significant slowdown in gdb then we could
1847 set it in the debug symbol readers only when necessary. */
1848 objfile->flags |= OBJF_REORDERED;
1849
1850 init_entry_point_info (objfile);
1851}
1852
1853/* Perform any local cleanups required when we are done with a particular
1854 objfile. I.E, we are in the process of discarding all symbol information
1855 for an objfile, freeing up all memory held for it, and unlinking the
1856 objfile struct from the global list of known objfiles. */
1857
1858static void
fba45db2 1859xcoff_symfile_finish (struct objfile *objfile)
c906108c 1860{
c5aa993b 1861 if (objfile->sym_private != NULL)
c906108c 1862 {
c5aa993b 1863 mfree (objfile->md, objfile->sym_private);
c906108c
SS
1864 }
1865
1866 /* Start with a fresh include table for the next objfile. */
1867 if (inclTable)
1868 {
b8c9b27d 1869 xfree (inclTable);
c906108c
SS
1870 inclTable = NULL;
1871 }
1872 inclIndx = inclLength = inclDepth = 0;
1873}
1874
1875
1876static void
fba45db2 1877init_stringtab (bfd *abfd, file_ptr offset, struct objfile *objfile)
c906108c
SS
1878{
1879 long length;
1880 int val;
1881 unsigned char lengthbuf[4];
1882 char *strtbl;
1883
c5aa993b 1884 ((struct coff_symfile_info *) objfile->sym_private)->strtbl = NULL;
c906108c
SS
1885
1886 if (bfd_seek (abfd, offset, SEEK_SET) < 0)
1887 error ("cannot seek to string table in %s: %s",
1888 bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
1889
3a42e9d0 1890 val = bfd_bread ((char *) lengthbuf, sizeof lengthbuf, abfd);
c906108c
SS
1891 length = bfd_h_get_32 (abfd, lengthbuf);
1892
1893 /* If no string table is needed, then the file may end immediately
1894 after the symbols. Just return with `strtbl' set to NULL. */
1895
1896 if (val != sizeof lengthbuf || length < sizeof lengthbuf)
1897 return;
1898
1899 /* Allocate string table from symbol_obstack. We will need this table
1900 as long as we have its symbol table around. */
1901
1902 strtbl = (char *) obstack_alloc (&objfile->symbol_obstack, length);
c5aa993b 1903 ((struct coff_symfile_info *) objfile->sym_private)->strtbl = strtbl;
c906108c
SS
1904
1905 /* Copy length buffer, the first byte is usually zero and is
1906 used for stabs with a name length of zero. */
1907 memcpy (strtbl, lengthbuf, sizeof lengthbuf);
1908 if (length == sizeof lengthbuf)
1909 return;
1910
3a42e9d0 1911 val = bfd_bread (strtbl + sizeof lengthbuf, length - sizeof lengthbuf, abfd);
c906108c
SS
1912
1913 if (val != length - sizeof lengthbuf)
1914 error ("cannot read string table from %s: %s",
1915 bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
1916 if (strtbl[length - 1] != '\0')
1917 error ("bad symbol file: string table does not end with null character");
1918
1919 return;
1920}
1921\f
1922/* If we have not yet seen a function for this psymtab, this is 0. If we
1923 have seen one, it is the offset in the line numbers of the line numbers
1924 for the psymtab. */
1925static unsigned int first_fun_line_offset;
1926
1927static struct partial_symtab *xcoff_start_psymtab
a14ed312
KB
1928 (struct objfile *, char *, int,
1929 struct partial_symbol **, struct partial_symbol **);
c906108c
SS
1930
1931/* Allocate and partially fill a partial symtab. It will be
1932 completely filled at the end of the symbol list.
1933
1934 SYMFILE_NAME is the name of the symbol-file we are reading from, and ADDR
1935 is the address relative to which its symbols are (incremental) or 0
1936 (normal). */
1937
1938static struct partial_symtab *
fba45db2
KB
1939xcoff_start_psymtab (struct objfile *objfile, char *filename, int first_symnum,
1940 struct partial_symbol **global_syms,
1941 struct partial_symbol **static_syms)
c906108c
SS
1942{
1943 struct partial_symtab *result =
d4f3574e 1944 start_psymtab_common (objfile, objfile->section_offsets,
c5aa993b 1945 filename,
d4f3574e 1946 /* We fill in textlow later. */
c5aa993b
JM
1947 0,
1948 global_syms, static_syms);
c906108c
SS
1949
1950 result->read_symtab_private = (char *)
c5aa993b
JM
1951 obstack_alloc (&objfile->psymbol_obstack, sizeof (struct symloc));
1952 ((struct symloc *) result->read_symtab_private)->first_symnum = first_symnum;
c906108c
SS
1953 result->read_symtab = xcoff_psymtab_to_symtab;
1954
1955 /* Deduce the source language from the filename for this psymtab. */
1956 psymtab_language = deduce_language_from_filename (filename);
1957
1958 return result;
1959}
1960
1961static struct partial_symtab *xcoff_end_psymtab
a14ed312
KB
1962 (struct partial_symtab *, char **, int, int,
1963 struct partial_symtab **, int, int);
c906108c
SS
1964
1965/* Close off the current usage of PST.
1966 Returns PST, or NULL if the partial symtab was empty and thrown away.
1967
1968 CAPPING_SYMBOL_NUMBER is the end of pst (exclusive).
1969
1970 INCLUDE_LIST, NUM_INCLUDES, DEPENDENCY_LIST, and NUMBER_DEPENDENCIES
1971 are the information for includes and dependencies. */
1972
1973static struct partial_symtab *
fba45db2
KB
1974xcoff_end_psymtab (struct partial_symtab *pst, char **include_list,
1975 int num_includes, int capping_symbol_number,
1976 struct partial_symtab **dependency_list,
1977 int number_dependencies, int textlow_not_set)
c906108c
SS
1978{
1979 int i;
c5aa993b 1980 struct objfile *objfile = pst->objfile;
c906108c
SS
1981
1982 if (capping_symbol_number != -1)
c5aa993b 1983 ((struct symloc *) pst->read_symtab_private)->numsyms =
c906108c 1984 capping_symbol_number
c5aa993b
JM
1985 - ((struct symloc *) pst->read_symtab_private)->first_symnum;
1986 ((struct symloc *) pst->read_symtab_private)->lineno_off =
c906108c
SS
1987 first_fun_line_offset;
1988 first_fun_line_offset = 0;
1989 pst->n_global_syms =
1990 objfile->global_psymbols.next - (objfile->global_psymbols.list + pst->globals_offset);
1991 pst->n_static_syms =
1992 objfile->static_psymbols.next - (objfile->static_psymbols.list + pst->statics_offset);
1993
1994 pst->number_of_dependencies = number_dependencies;
1995 if (number_dependencies)
1996 {
1997 pst->dependencies = (struct partial_symtab **)
1998 obstack_alloc (&objfile->psymbol_obstack,
c5aa993b 1999 number_dependencies * sizeof (struct partial_symtab *));
c906108c 2000 memcpy (pst->dependencies, dependency_list,
c5aa993b 2001 number_dependencies * sizeof (struct partial_symtab *));
c906108c
SS
2002 }
2003 else
2004 pst->dependencies = 0;
2005
2006 for (i = 0; i < num_includes; i++)
2007 {
2008 struct partial_symtab *subpst =
c5aa993b 2009 allocate_psymtab (include_list[i], objfile);
c906108c
SS
2010
2011 subpst->section_offsets = pst->section_offsets;
2012 subpst->read_symtab_private =
c5aa993b
JM
2013 (char *) obstack_alloc (&objfile->psymbol_obstack,
2014 sizeof (struct symloc));
2015 ((struct symloc *) subpst->read_symtab_private)->first_symnum = 0;
2016 ((struct symloc *) subpst->read_symtab_private)->numsyms = 0;
c906108c
SS
2017 subpst->textlow = 0;
2018 subpst->texthigh = 0;
2019
2020 /* We could save slight bits of space by only making one of these,
c5aa993b 2021 shared by the entire set of include files. FIXME-someday. */
c906108c
SS
2022 subpst->dependencies = (struct partial_symtab **)
2023 obstack_alloc (&objfile->psymbol_obstack,
2024 sizeof (struct partial_symtab *));
2025 subpst->dependencies[0] = pst;
2026 subpst->number_of_dependencies = 1;
2027
2028 subpst->globals_offset =
2029 subpst->n_global_syms =
c5aa993b
JM
2030 subpst->statics_offset =
2031 subpst->n_static_syms = 0;
c906108c
SS
2032
2033 subpst->readin = 0;
2034 subpst->symtab = 0;
2035 subpst->read_symtab = pst->read_symtab;
2036 }
2037
2038 sort_pst_symbols (pst);
2039
2040 /* If there is already a psymtab or symtab for a file of this name,
2041 remove it. (If there is a symtab, more drastic things also
2042 happen.) This happens in VxWorks. */
2043 free_named_symtabs (pst->filename);
2044
2045 if (num_includes == 0
2046 && number_dependencies == 0
2047 && pst->n_global_syms == 0
2048 && pst->n_static_syms == 0)
2049 {
2050 /* Throw away this psymtab, it's empty. We can't deallocate it, since
c5aa993b 2051 it is on the obstack, but we can forget to chain it on the list. */
c906108c 2052 /* Empty psymtabs happen as a result of header files which don't have
c5aa993b 2053 any symbols in them. There can be a lot of them. */
c906108c
SS
2054
2055 discard_psymtab (pst);
2056
2057 /* Indicate that psymtab was thrown away. */
c5aa993b 2058 pst = (struct partial_symtab *) NULL;
c906108c
SS
2059 }
2060 return pst;
2061}
2062
a14ed312
KB
2063static void swap_sym (struct internal_syment *,
2064 union internal_auxent *, char **, char **,
2065 unsigned int *, struct objfile *);
c906108c
SS
2066
2067/* Swap raw symbol at *RAW and put the name in *NAME, the symbol in
2068 *SYMBOL, the first auxent in *AUX. Advance *RAW and *SYMNUMP over
2069 the symbol and its auxents. */
2070
2071static void
fba45db2
KB
2072swap_sym (struct internal_syment *symbol, union internal_auxent *aux,
2073 char **name, char **raw, unsigned int *symnump,
2074 struct objfile *objfile)
c906108c
SS
2075{
2076 bfd_coff_swap_sym_in (objfile->obfd, *raw, symbol);
2077 if (symbol->n_zeroes)
2078 {
2079 /* If it's exactly E_SYMNMLEN characters long it isn't
c5aa993b 2080 '\0'-terminated. */
c906108c
SS
2081 if (symbol->n_name[E_SYMNMLEN - 1] != '\0')
2082 {
2083 /* FIXME: wastes memory for symbols which we don't end up putting
2084 into the minimal symbols. */
2085 char *p;
2086 p = obstack_alloc (&objfile->psymbol_obstack, E_SYMNMLEN + 1);
2087 strncpy (p, symbol->n_name, E_SYMNMLEN);
2088 p[E_SYMNMLEN] = '\0';
2089 *name = p;
2090 }
2091 else
2092 /* Point to the unswapped name as that persists as long as the
2093 objfile does. */
c5aa993b 2094 *name = ((struct external_syment *) *raw)->e.e_name;
c906108c
SS
2095 }
2096 else if (symbol->n_sclass & 0x80)
2097 {
c5aa993b 2098 *name = ((struct coff_symfile_info *) objfile->sym_private)->debugsec
c906108c
SS
2099 + symbol->n_offset;
2100 }
2101 else
2102 {
c5aa993b 2103 *name = ((struct coff_symfile_info *) objfile->sym_private)->strtbl
c906108c
SS
2104 + symbol->n_offset;
2105 }
2106 ++*symnump;
2107 *raw += coff_data (objfile->obfd)->local_symesz;
2108 if (symbol->n_numaux > 0)
2109 {
2110 bfd_coff_swap_aux_in (objfile->obfd, *raw, symbol->n_type,
2111 symbol->n_sclass, 0, symbol->n_numaux, aux);
2112
2113 *symnump += symbol->n_numaux;
2114 *raw += coff_data (objfile->obfd)->local_symesz * symbol->n_numaux;
2115 }
2116}
2117
2118static void
fba45db2 2119scan_xcoff_symtab (struct objfile *objfile)
c906108c 2120{
c5aa993b 2121 CORE_ADDR toc_offset = 0; /* toc offset value in data section. */
c906108c
SS
2122 char *filestring = NULL;
2123
2124 char *namestring;
2125 int past_first_source_file = 0;
2126 bfd *abfd;
2127 asection *bfd_sect;
2128 unsigned int nsyms;
2129
2130 /* Current partial symtab */
2131 struct partial_symtab *pst;
2132
2133 /* List of current psymtab's include files */
2134 char **psymtab_include_list;
2135 int includes_allocated;
2136 int includes_used;
2137
2138 /* Index within current psymtab dependency list */
2139 struct partial_symtab **dependency_list;
2140 int dependencies_used, dependencies_allocated;
2141
2142 char *sraw_symbol;
2143 struct internal_syment symbol;
96baa820 2144 union internal_auxent main_aux[5];
c906108c
SS
2145 unsigned int ssymnum;
2146
c5aa993b 2147 char *last_csect_name = NULL; /* last seen csect's name and value */
c906108c
SS
2148 CORE_ADDR last_csect_val = 0;
2149 int last_csect_sec = 0;
c5aa993b 2150 int misc_func_recorded = 0; /* true if any misc. function */
c906108c
SS
2151 int textlow_not_set = 1;
2152
2153 pst = (struct partial_symtab *) 0;
2154
2155 includes_allocated = 30;
2156 includes_used = 0;
2157 psymtab_include_list = (char **) alloca (includes_allocated *
2158 sizeof (char *));
2159
2160 dependencies_allocated = 30;
2161 dependencies_used = 0;
2162 dependency_list =
2163 (struct partial_symtab **) alloca (dependencies_allocated *
2164 sizeof (struct partial_symtab *));
2165
2166 last_source_file = NULL;
2167
2168 abfd = objfile->obfd;
2169
c5aa993b
JM
2170 sraw_symbol = ((struct coff_symfile_info *) objfile->sym_private)->symtbl;
2171 nsyms = ((struct coff_symfile_info *) objfile->sym_private)->symtbl_num_syms;
c906108c
SS
2172 ssymnum = 0;
2173 while (ssymnum < nsyms)
2174 {
7a78ae4e 2175 int sclass;
c906108c
SS
2176
2177 QUIT;
2178
7a78ae4e
ND
2179 bfd_coff_swap_sym_in (abfd, sraw_symbol, &symbol);
2180 sclass = symbol.n_sclass;
2181
c906108c
SS
2182 switch (sclass)
2183 {
2184 case C_EXT:
2185 case C_HIDEXT:
2186 {
2187 /* The CSECT auxent--always the last auxent. */
2188 union internal_auxent csect_aux;
2189 unsigned int symnum_before = ssymnum;
2190
96baa820 2191 swap_sym (&symbol, &main_aux[0], &namestring, &sraw_symbol,
c906108c
SS
2192 &ssymnum, objfile);
2193 if (symbol.n_numaux > 1)
2194 {
2195 bfd_coff_swap_aux_in
2196 (objfile->obfd,
c5aa993b 2197 sraw_symbol - coff_data (abfd)->local_symesz,
c906108c
SS
2198 symbol.n_type,
2199 symbol.n_sclass,
2200 symbol.n_numaux - 1,
2201 symbol.n_numaux,
2202 &csect_aux);
2203 }
2204 else
96baa820 2205 csect_aux = main_aux[0];
c906108c 2206
977adac5
ND
2207 /* If symbol name starts with ".$" or "$", ignore it. */
2208 if (namestring[0] == '$'
c906108c
SS
2209 || (namestring[0] == '.' && namestring[1] == '$'))
2210 break;
2211
2212 switch (csect_aux.x_csect.x_smtyp & 0x7)
2213 {
2214 case XTY_SD:
2215 switch (csect_aux.x_csect.x_smclas)
2216 {
2217 case XMC_PR:
2218 if (last_csect_name)
2219 {
2220 /* If no misc. function recorded in the last
2221 seen csect, enter it as a function. This
2222 will take care of functions like strcmp()
2223 compiled by xlc. */
2224
2225 if (!misc_func_recorded)
2226 {
2227 RECORD_MINIMAL_SYMBOL
2228 (last_csect_name, last_csect_val,
2229 mst_text, last_csect_sec,
2230 objfile);
2231 }
2232
2233 if (pst != NULL)
2234 {
2235 /* We have to allocate one psymtab for
2236 each program csect, because their text
2237 sections need not be adjacent. */
2238 xcoff_end_psymtab
2239 (pst, psymtab_include_list, includes_used,
2240 symnum_before, dependency_list,
2241 dependencies_used, textlow_not_set);
2242 includes_used = 0;
2243 dependencies_used = 0;
2244 /* Give all psymtabs for this source file the same
2245 name. */
2246 pst = xcoff_start_psymtab
d4f3574e 2247 (objfile,
c906108c
SS
2248 filestring,
2249 symnum_before,
2250 objfile->global_psymbols.next,
2251 objfile->static_psymbols.next);
2252 }
2253 }
977adac5
ND
2254 /* Activate the misc_func_recorded mechanism for
2255 compiler- and linker-generated CSECTs like ".strcmp"
2256 and "@FIX1". */
2257 if (namestring && (namestring[0] == '.'
2258 || namestring[0] == '@'))
c906108c
SS
2259 {
2260 last_csect_name = namestring;
2261 last_csect_val = symbol.n_value;
2262 last_csect_sec =
2263 secnum_to_section (symbol.n_scnum, objfile);
2264 }
2265 if (pst != NULL)
2266 {
2267 CORE_ADDR highval =
c5aa993b 2268 symbol.n_value + csect_aux.x_csect.x_scnlen.l;
c906108c
SS
2269 if (highval > pst->texthigh)
2270 pst->texthigh = highval;
2271 if (pst->textlow == 0 || symbol.n_value < pst->textlow)
2272 pst->textlow = symbol.n_value;
2273 }
2274 misc_func_recorded = 0;
2275 break;
2276
2277 case XMC_RW:
2278 /* Data variables are recorded in the minimal symbol
2279 table, except for section symbols. */
2280 if (*namestring != '.')
2281 prim_record_minimal_symbol_and_info
2282 (namestring, symbol.n_value,
2283 sclass == C_HIDEXT ? mst_file_data : mst_data,
2284 NULL, secnum_to_section (symbol.n_scnum, objfile),
2285 NULL, objfile);
2286 break;
2287
2288 case XMC_TC0:
2289 if (toc_offset)
2290 warning ("More than one XMC_TC0 symbol found.");
2291 toc_offset = symbol.n_value;
2292
2293 /* Make TOC offset relative to start address of section. */
2294 bfd_sect = secnum_to_bfd_section (symbol.n_scnum, objfile);
2295 if (bfd_sect)
2296 toc_offset -= bfd_section_vma (objfile->obfd, bfd_sect);
2297 break;
2298
2299 case XMC_TC:
2300 /* These symbols tell us where the TOC entry for a
2301 variable is, not the variable itself. */
2302 break;
2303
2304 default:
2305 break;
2306 }
2307 break;
2308
2309 case XTY_LD:
2310 switch (csect_aux.x_csect.x_smclas)
2311 {
2312 case XMC_PR:
2313 /* A function entry point. */
2314
2315 if (first_fun_line_offset == 0 && symbol.n_numaux > 1)
2316 first_fun_line_offset =
96baa820 2317 main_aux[0].x_sym.x_fcnary.x_fcn.x_lnnoptr;
c906108c
SS
2318 RECORD_MINIMAL_SYMBOL
2319 (namestring, symbol.n_value,
2320 sclass == C_HIDEXT ? mst_file_text : mst_text,
2321 secnum_to_section (symbol.n_scnum, objfile),
2322 objfile);
2323 break;
2324
2325 case XMC_GL:
2326 /* shared library function trampoline code entry
2327 point. */
2328
2329 /* record trampoline code entries as
2330 mst_solib_trampoline symbol. When we lookup mst
2331 symbols, we will choose mst_text over
2332 mst_solib_trampoline. */
2333 RECORD_MINIMAL_SYMBOL
2334 (namestring, symbol.n_value,
2335 mst_solib_trampoline,
2336 secnum_to_section (symbol.n_scnum, objfile),
2337 objfile);
2338 break;
2339
2340 case XMC_DS:
2341 /* The symbols often have the same names as
2342 debug symbols for functions, and confuse
2343 lookup_symbol. */
2344 break;
2345
2346 default:
2347
2348 /* xlc puts each variable in a separate csect,
2349 so we get an XTY_SD for each variable. But
2350 gcc puts several variables in a csect, so
2351 that each variable only gets an XTY_LD. We
2352 still need to record them. This will
2353 typically be XMC_RW; I suspect XMC_RO and
2354 XMC_BS might be possible too. */
2355 if (*namestring != '.')
2356 prim_record_minimal_symbol_and_info
2357 (namestring, symbol.n_value,
2358 sclass == C_HIDEXT ? mst_file_data : mst_data,
2359 NULL, secnum_to_section (symbol.n_scnum, objfile),
2360 NULL, objfile);
2361 break;
2362 }
2363 break;
2364
2365 case XTY_CM:
2366 switch (csect_aux.x_csect.x_smclas)
2367 {
2368 case XMC_RW:
2369 case XMC_BS:
2370 /* Common variables are recorded in the minimal symbol
2371 table, except for section symbols. */
2372 if (*namestring != '.')
2373 prim_record_minimal_symbol_and_info
2374 (namestring, symbol.n_value,
2375 sclass == C_HIDEXT ? mst_file_bss : mst_bss,
2376 NULL, secnum_to_section (symbol.n_scnum, objfile),
2377 NULL, objfile);
2378 break;
2379 }
2380 break;
2381
2382 default:
2383 break;
2384 }
2385 }
2386 break;
2387 case C_FILE:
2388 {
2389 unsigned int symnum_before;
2390
2391 symnum_before = ssymnum;
96baa820 2392 swap_sym (&symbol, &main_aux[0], &namestring, &sraw_symbol,
c906108c
SS
2393 &ssymnum, objfile);
2394
2395 /* See if the last csect needs to be recorded. */
2396
2397 if (last_csect_name && !misc_func_recorded)
2398 {
2399
2400 /* If no misc. function recorded in the last seen csect, enter
2401 it as a function. This will take care of functions like
2402 strcmp() compiled by xlc. */
2403
2404 RECORD_MINIMAL_SYMBOL
2405 (last_csect_name, last_csect_val,
2406 mst_text, last_csect_sec, objfile);
2407 }
2408
2409 if (pst)
2410 {
2411 xcoff_end_psymtab (pst, psymtab_include_list, includes_used,
2412 symnum_before, dependency_list,
2413 dependencies_used, textlow_not_set);
2414 includes_used = 0;
2415 dependencies_used = 0;
2416 }
2417 first_fun_line_offset = 0;
2418
2419 /* XCOFF, according to the AIX 3.2 documentation, puts the
2420 filename in cs->c_name. But xlc 1.3.0.2 has decided to
2421 do things the standard COFF way and put it in the auxent.
2422 We use the auxent if the symbol is ".file" and an auxent
2423 exists, otherwise use the symbol itself. */
2424 if (!strcmp (namestring, ".file") && symbol.n_numaux > 0)
2425 {
96baa820 2426 filestring = coff_getfilename (&main_aux[0], objfile);
c906108c
SS
2427 }
2428 else
2429 filestring = namestring;
2430
d4f3574e 2431 pst = xcoff_start_psymtab (objfile,
c906108c
SS
2432 filestring,
2433 symnum_before,
2434 objfile->global_psymbols.next,
2435 objfile->static_psymbols.next);
2436 last_csect_name = NULL;
2437 }
2438 break;
2439
2440 default:
2441 {
2442 static struct complaint msg =
c5aa993b 2443 {"Storage class %d not recognized during scan", 0, 0};
c906108c
SS
2444 complain (&msg, sclass);
2445 }
2446 /* FALLTHROUGH */
2447
2448 /* C_FCN is .bf and .ef symbols. I think it is sufficient
2449 to handle only the C_FUN and C_EXT. */
2450 case C_FCN:
2451
2452 case C_BSTAT:
2453 case C_ESTAT:
2454 case C_ARG:
2455 case C_REGPARM:
2456 case C_REG:
2457 case C_TPDEF:
2458 case C_STRTAG:
2459 case C_UNTAG:
2460 case C_ENTAG:
2461 case C_LABEL:
2462 case C_NULL:
2463
2464 /* C_EINCL means we are switching back to the main file. But there
2465 is no reason to care; the only thing we want to know about
2466 includes is the names of all the included (.h) files. */
2467 case C_EINCL:
2468
2469 case C_BLOCK:
2470
2471 /* I don't think C_STAT is used in xcoff; C_HIDEXT appears to be
2472 used instead. */
2473 case C_STAT:
2474
2475 /* I don't think the name of the common block (as opposed to the
2476 variables within it) is something which is user visible
2477 currently. */
2478 case C_BCOMM:
2479 case C_ECOMM:
2480
2481 case C_PSYM:
2482 case C_RPSYM:
2483
2484 /* I think we can ignore C_LSYM; types on xcoff seem to use C_DECL
2485 so C_LSYM would appear to be only for locals. */
2486 case C_LSYM:
2487
2488 case C_AUTO:
2489 case C_RSYM:
2490 {
2491 /* We probably could save a few instructions by assuming that
2492 C_LSYM, C_PSYM, etc., never have auxents. */
7a78ae4e 2493 int naux1 = symbol.n_numaux + 1;
c906108c 2494 ssymnum += naux1;
7a78ae4e 2495 sraw_symbol += bfd_coff_symesz (abfd) * naux1;
c906108c
SS
2496 }
2497 break;
2498
2499 case C_BINCL:
d5d0a62f
EZ
2500 {
2501 /* Mark down an include file in the current psymtab */
2502 enum language tmp_language;
2503 swap_sym (&symbol, &main_aux[0], &namestring, &sraw_symbol,
2504 &ssymnum, objfile);
2505
2506 tmp_language = deduce_language_from_filename (namestring);
2507
2508 /* Only change the psymtab's language if we've learned
2509 something useful (eg. tmp_language is not language_unknown).
2510 In addition, to match what start_subfile does, never change
2511 from C++ to C. */
2512 if (tmp_language != language_unknown
2513 && (tmp_language != language_c
2514 || psymtab_language != language_cplus))
2515 psymtab_language = tmp_language;
2516
2517 /* In C++, one may expect the same filename to come round many
2518 times, when code is coming alternately from the main file
2519 and from inline functions in other files. So I check to see
2520 if this is a file we've seen before -- either the main
2521 source file, or a previously included file.
2522
2523 This seems to be a lot of time to be spending on N_SOL, but
2524 things like "break c-exp.y:435" need to work (I
2525 suppose the psymtab_include_list could be hashed or put
2526 in a binary tree, if profiling shows this is a major hog). */
2527 if (pst && STREQ (namestring, pst->filename))
2528 continue;
2529 {
2530 register int i;
2531 for (i = 0; i < includes_used; i++)
2532 if (STREQ (namestring, psymtab_include_list[i]))
2533 {
2534 i = -1;
2535 break;
2536 }
2537 if (i == -1)
2538 continue;
2539 }
2540 psymtab_include_list[includes_used++] = namestring;
2541 if (includes_used >= includes_allocated)
2542 {
2543 char **orig = psymtab_include_list;
c906108c 2544
d5d0a62f
EZ
2545 psymtab_include_list = (char **)
2546 alloca ((includes_allocated *= 2) *
2547 sizeof (char *));
2548 memcpy ((PTR) psymtab_include_list, (PTR) orig,
2549 includes_used * sizeof (char *));
2550 }
2551 continue;
2552 }
c906108c
SS
2553 case C_FUN:
2554 /* The value of the C_FUN is not the address of the function (it
2555 appears to be the address before linking), but as long as it
2556 is smaller than the actual address, then find_pc_partial_function
2557 will use the minimal symbols instead. I hope. */
2558
2559 case C_GSYM:
2560 case C_ECOML:
2561 case C_DECL:
2562 case C_STSYM:
d5d0a62f
EZ
2563 {
2564
2565 static struct complaint function_outside_compilation_unit = {
2566 "function `%s' appears to be defined outside of all compilation units", 0, 0
2567 };
2568
2569 char *p;
2570 swap_sym (&symbol, &main_aux[0], &namestring, &sraw_symbol,
2571 &ssymnum, objfile);
2572
2573 p = (char *) strchr (namestring, ':');
2574 if (!p)
2575 continue; /* Not a debugging symbol. */
2576
2577 /* Main processing section for debugging symbols which
2578 the initial read through the symbol tables needs to worry
2579 about. If we reach this point, the symbol which we are
2580 considering is definitely one we are interested in.
2581 p must also contain the (valid) index into the namestring
2582 which indicates the debugging type symbol. */
2583
2584 switch (p[1])
2585 {
2586 case 'S':
2587 symbol.n_value += ANOFFSET (objfile->section_offsets, SECT_OFF_DATA (objfile));
2588#ifdef STATIC_TRANSFORM_NAME
2589 namestring = STATIC_TRANSFORM_NAME (namestring);
2590#endif
2591 add_psymbol_to_list (namestring, p - namestring,
2592 VAR_NAMESPACE, LOC_STATIC,
2593 &objfile->static_psymbols,
2594 0, symbol.n_value,
2595 psymtab_language, objfile);
2596 continue;
2597
2598 case 'G':
2599 symbol.n_value += ANOFFSET (objfile->section_offsets, SECT_OFF_DATA (objfile));
2600 /* The addresses in these entries are reported to be
2601 wrong. See the code that reads 'G's for symtabs. */
2602 add_psymbol_to_list (namestring, p - namestring,
2603 VAR_NAMESPACE, LOC_STATIC,
2604 &objfile->global_psymbols,
2605 0, symbol.n_value,
2606 psymtab_language, objfile);
2607 continue;
2608
2609 case 'T':
2610 /* When a 'T' entry is defining an anonymous enum, it
2611 may have a name which is the empty string, or a
2612 single space. Since they're not really defining a
2613 symbol, those shouldn't go in the partial symbol
2614 table. We do pick up the elements of such enums at
2615 'check_enum:', below. */
2616 if (p >= namestring + 2
2617 || (p == namestring + 1
2618 && namestring[0] != ' '))
2619 {
2620 add_psymbol_to_list (namestring, p - namestring,
2621 STRUCT_NAMESPACE, LOC_TYPEDEF,
2622 &objfile->static_psymbols,
2623 symbol.n_value, 0,
2624 psymtab_language, objfile);
2625 if (p[2] == 't')
2626 {
2627 /* Also a typedef with the same name. */
2628 add_psymbol_to_list (namestring, p - namestring,
2629 VAR_NAMESPACE, LOC_TYPEDEF,
2630 &objfile->static_psymbols,
2631 symbol.n_value, 0,
2632 psymtab_language, objfile);
2633 p += 1;
2634 }
2635 /* The semantics of C++ state that "struct foo { ... }"
2636 also defines a typedef for "foo". Unfortuantely, cfront
2637 never makes the typedef when translating from C++ to C.
2638 We make the typedef here so that "ptype foo" works as
2639 expected for cfront translated code. */
2640 else if (psymtab_language == language_cplus)
2641 {
2642 /* Also a typedef with the same name. */
2643 add_psymbol_to_list (namestring, p - namestring,
2644 VAR_NAMESPACE, LOC_TYPEDEF,
2645 &objfile->static_psymbols,
2646 symbol.n_value, 0,
2647 psymtab_language, objfile);
2648 }
2649 }
2650 goto check_enum;
2651
2652 case 't':
2653 if (p != namestring) /* a name is there, not just :T... */
2654 {
2655 add_psymbol_to_list (namestring, p - namestring,
2656 VAR_NAMESPACE, LOC_TYPEDEF,
2657 &objfile->static_psymbols,
2658 symbol.n_value, 0,
2659 psymtab_language, objfile);
2660 }
2661 check_enum:
2662 /* If this is an enumerated type, we need to
2663 add all the enum constants to the partial symbol
2664 table. This does not cover enums without names, e.g.
2665 "enum {a, b} c;" in C, but fortunately those are
2666 rare. There is no way for GDB to find those from the
2667 enum type without spending too much time on it. Thus
2668 to solve this problem, the compiler needs to put out the
2669 enum in a nameless type. GCC2 does this. */
2670
2671 /* We are looking for something of the form
2672 <name> ":" ("t" | "T") [<number> "="] "e"
2673 {<constant> ":" <value> ","} ";". */
2674
2675 /* Skip over the colon and the 't' or 'T'. */
2676 p += 2;
2677 /* This type may be given a number. Also, numbers can come
2678 in pairs like (0,26). Skip over it. */
2679 while ((*p >= '0' && *p <= '9')
2680 || *p == '(' || *p == ',' || *p == ')'
2681 || *p == '=')
2682 p++;
2683
2684 if (*p++ == 'e')
2685 {
2686 /* The aix4 compiler emits extra crud before the members. */
2687 if (*p == '-')
2688 {
2689 /* Skip over the type (?). */
2690 while (*p != ':')
2691 p++;
2692
2693 /* Skip over the colon. */
2694 p++;
2695 }
2696
2697 /* We have found an enumerated type. */
2698 /* According to comments in read_enum_type
2699 a comma could end it instead of a semicolon.
2700 I don't know where that happens.
2701 Accept either. */
2702 while (*p && *p != ';' && *p != ',')
2703 {
2704 char *q;
2705
2706 /* Check for and handle cretinous dbx symbol name
2707 continuation! */
2708 if (*p == '\\' || (*p == '?' && p[1] == '\0'))
2709 p = next_symbol_text (objfile);
2710
2711 /* Point to the character after the name
2712 of the enum constant. */
2713 for (q = p; *q && *q != ':'; q++)
2714 ;
2715 /* Note that the value doesn't matter for
2716 enum constants in psymtabs, just in symtabs. */
2717 add_psymbol_to_list (p, q - p,
2718 VAR_NAMESPACE, LOC_CONST,
2719 &objfile->static_psymbols, 0,
2720 0, psymtab_language, objfile);
2721 /* Point past the name. */
2722 p = q;
2723 /* Skip over the value. */
2724 while (*p && *p != ',')
2725 p++;
2726 /* Advance past the comma. */
2727 if (*p)
2728 p++;
2729 }
2730 }
2731 continue;
2732
2733 case 'c':
2734 /* Constant, e.g. from "const" in Pascal. */
2735 add_psymbol_to_list (namestring, p - namestring,
2736 VAR_NAMESPACE, LOC_CONST,
2737 &objfile->static_psymbols, symbol.n_value,
2738 0, psymtab_language, objfile);
2739 continue;
2740
2741 case 'f':
2742 if (! pst)
2743 {
2744 int name_len = p - namestring;
2745 char *name = xmalloc (name_len + 1);
2746 memcpy (name, namestring, name_len);
2747 name[name_len] = '\0';
2748 complain (&function_outside_compilation_unit, name);
2749 xfree (name);
2750 }
2751 symbol.n_value += ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2752 add_psymbol_to_list (namestring, p - namestring,
2753 VAR_NAMESPACE, LOC_BLOCK,
2754 &objfile->static_psymbols,
2755 0, symbol.n_value,
2756 psymtab_language, objfile);
2757 continue;
2758
2759 /* Global functions were ignored here, but now they
2760 are put into the global psymtab like one would expect.
2761 They're also in the minimal symbol table. */
2762 case 'F':
2763 if (! pst)
2764 {
2765 int name_len = p - namestring;
2766 char *name = xmalloc (name_len + 1);
2767 memcpy (name, namestring, name_len);
2768 name[name_len] = '\0';
2769 complain (&function_outside_compilation_unit, name);
2770 xfree (name);
2771 }
2772 symbol.n_value += ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2773 add_psymbol_to_list (namestring, p - namestring,
2774 VAR_NAMESPACE, LOC_BLOCK,
2775 &objfile->global_psymbols,
2776 0, symbol.n_value,
2777 psymtab_language, objfile);
2778 continue;
2779
2780 /* Two things show up here (hopefully); static symbols of
2781 local scope (static used inside braces) or extensions
2782 of structure symbols. We can ignore both. */
2783 case 'V':
2784 case '(':
2785 case '0':
2786 case '1':
2787 case '2':
2788 case '3':
2789 case '4':
2790 case '5':
2791 case '6':
2792 case '7':
2793 case '8':
2794 case '9':
2795 case '-':
2796 case '#': /* for symbol identification (used in live ranges) */
2797 /* added to support cfront stabs strings */
2798 case 'Z': /* for definition continuations */
2799 case 'P': /* for prototypes */
2800 continue;
2801
2802 case ':':
2803 /* It is a C++ nested symbol. We don't need to record it
2804 (I don't think); if we try to look up foo::bar::baz,
2805 then symbols for the symtab containing foo should get
2806 read in, I think. */
2807 /* Someone says sun cc puts out symbols like
2808 /foo/baz/maclib::/usr/local/bin/maclib,
2809 which would get here with a symbol type of ':'. */
2810 continue;
2811
2812 default:
2813 /* Unexpected symbol descriptor. The second and subsequent stabs
2814 of a continued stab can show up here. The question is
2815 whether they ever can mimic a normal stab--it would be
2816 nice if not, since we certainly don't want to spend the
2817 time searching to the end of every string looking for
2818 a backslash. */
2819
2820 complain (&unknown_symchar_complaint, p[1]);
2821
2822 /* Ignore it; perhaps it is an extension that we don't
2823 know about. */
2824 continue;
2825 }
2826 }
c906108c
SS
2827 }
2828 }
2829
2830 if (pst)
2831 {
2832 xcoff_end_psymtab (pst, psymtab_include_list, includes_used,
2833 ssymnum, dependency_list,
2834 dependencies_used, textlow_not_set);
2835 }
2836
2837 /* Record the toc offset value of this symbol table into objfile structure.
2838 If no XMC_TC0 is found, toc_offset should be zero. Another place to obtain
2839 this information would be file auxiliary header. */
2840
2841 ((struct coff_symfile_info *) objfile->sym_private)->toc_offset = toc_offset;
2842}
2843
2844/* Return the toc offset value for a given objfile. */
2845
2846CORE_ADDR
fba45db2 2847get_toc_offset (struct objfile *objfile)
c906108c
SS
2848{
2849 if (objfile)
2850 return ((struct coff_symfile_info *) objfile->sym_private)->toc_offset;
2851 return 0;
2852}
2853
2854/* Scan and build partial symbols for a symbol file.
2855 We have been initialized by a call to dbx_symfile_init, which
2856 put all the relevant info into a "struct dbx_symfile_info",
2857 hung off the objfile structure.
2858
2859 SECTION_OFFSETS contains offsets relative to which the symbols in the
2860 various sections are (depending where the sections were actually loaded).
2861 MAINLINE is true if we are reading the main symbol
2862 table (as opposed to a shared lib or dynamically loaded file). */
2863
2864static void
b095261a 2865xcoff_initial_scan (struct objfile *objfile, int mainline)
c906108c
SS
2866{
2867 bfd *abfd;
2868 int val;
2869 struct cleanup *back_to;
c5aa993b
JM
2870 int num_symbols; /* # of symbols */
2871 file_ptr symtab_offset; /* symbol table and */
2872 file_ptr stringtab_offset; /* string table file offsets */
c906108c
SS
2873 struct coff_symfile_info *info;
2874 char *name;
2875 unsigned int size;
2876
c5aa993b 2877 info = (struct coff_symfile_info *) objfile->sym_private;
c906108c
SS
2878 symfile_bfd = abfd = objfile->obfd;
2879 name = objfile->name;
2880
2881 num_symbols = bfd_get_symcount (abfd); /* # of symbols */
2882 symtab_offset = obj_sym_filepos (abfd); /* symbol table file offset */
2883 stringtab_offset = symtab_offset +
c5aa993b 2884 num_symbols * coff_data (abfd)->local_symesz;
c906108c
SS
2885
2886 info->min_lineno_offset = 0;
2887 info->max_lineno_offset = 0;
2888 bfd_map_over_sections (abfd, find_linenos, info);
2889
2890 if (num_symbols > 0)
2891 {
2892 /* Read the string table. */
2893 init_stringtab (abfd, stringtab_offset, objfile);
2894
2895 /* Read the .debug section, if present. */
2896 {
2897 sec_ptr secp;
2898 bfd_size_type length;
2899 char *debugsec = NULL;
2900
2901 secp = bfd_get_section_by_name (abfd, ".debug");
2902 if (secp)
2903 {
2904 length = bfd_section_size (abfd, secp);
2905 if (length)
2906 {
2907 debugsec =
2908 (char *) obstack_alloc (&objfile->symbol_obstack, length);
2909
2910 if (!bfd_get_section_contents (abfd, secp, debugsec,
2911 (file_ptr) 0, length))
2912 {
2913 error ("Error reading .debug section of `%s': %s",
2914 name, bfd_errmsg (bfd_get_error ()));
2915 }
2916 }
2917 }
c5aa993b 2918 ((struct coff_symfile_info *) objfile->sym_private)->debugsec =
c906108c
SS
2919 debugsec;
2920 }
2921 }
2922
2923 /* Read the symbols. We keep them in core because we will want to
2924 access them randomly in read_symbol*. */
2925 val = bfd_seek (abfd, symtab_offset, SEEK_SET);
2926 if (val < 0)
2927 error ("Error reading symbols from %s: %s",
2928 name, bfd_errmsg (bfd_get_error ()));
2929 size = coff_data (abfd)->local_symesz * num_symbols;
c5aa993b 2930 ((struct coff_symfile_info *) objfile->sym_private)->symtbl =
c906108c 2931 obstack_alloc (&objfile->symbol_obstack, size);
c5aa993b 2932 ((struct coff_symfile_info *) objfile->sym_private)->symtbl_num_syms =
c906108c
SS
2933 num_symbols;
2934
3a42e9d0
AM
2935 val = bfd_bread (((struct coff_symfile_info *) objfile->sym_private)->symtbl,
2936 size, abfd);
c906108c
SS
2937 if (val != size)
2938 perror_with_name ("reading symbol table");
2939
2940 /* If we are reinitializing, or if we have never loaded syms yet, init */
2941 if (mainline
ef96bde8
EZ
2942 || (objfile->global_psymbols.size == 0
2943 && objfile->static_psymbols.size == 0))
c906108c
SS
2944 /* I'm not sure how how good num_symbols is; the rule of thumb in
2945 init_psymbol_list was developed for a.out. On the one hand,
2946 num_symbols includes auxents. On the other hand, it doesn't
2947 include N_SLINE. */
2948 init_psymbol_list (objfile, num_symbols);
2949
2950 free_pending_blocks ();
a0b3c4fd 2951 back_to = make_cleanup (really_free_pendings, 0);
c906108c
SS
2952
2953 init_minimal_symbol_collection ();
56e290f4 2954 make_cleanup_discard_minimal_symbols ();
c906108c
SS
2955
2956 /* Now that the symbol table data of the executable file are all in core,
2957 process them and define symbols accordingly. */
2958
d4f3574e 2959 scan_xcoff_symtab (objfile);
c906108c
SS
2960
2961 /* Install any minimal symbols that have been collected as the current
2962 minimal symbols for this objfile. */
2963
2964 install_minimal_symbols (objfile);
2965
2966 do_cleanups (back_to);
2967}
2968\f
d4f3574e 2969static void
fba45db2 2970xcoff_symfile_offsets (struct objfile *objfile, struct section_addr_info *addrs)
c906108c 2971{
b8fbeb18 2972 asection *sect = NULL;
c906108c
SS
2973 int i;
2974
2975 objfile->num_sections = SECT_OFF_MAX;
d4f3574e 2976 objfile->section_offsets = (struct section_offsets *)
96baa820 2977 obstack_alloc (&objfile->psymbol_obstack, SIZEOF_SECTION_OFFSETS);
c906108c 2978
b8fbeb18
EZ
2979 /* Initialize the section indexes for future use. */
2980 sect = bfd_get_section_by_name (objfile->obfd, ".text");
2981 if (sect)
2982 objfile->sect_index_text = sect->index;
2983
2984 sect = bfd_get_section_by_name (objfile->obfd, ".data");
2985 if (sect)
2986 objfile->sect_index_data = sect->index;
2987
2988 sect = bfd_get_section_by_name (objfile->obfd, ".bss");
2989 if (sect)
2990 objfile->sect_index_bss = sect->index;
2991
2992 sect = bfd_get_section_by_name (objfile->obfd, ".rodata");
2993 if (sect)
2994 objfile->sect_index_rodata = sect->index;
2995
c906108c 2996 for (i = 0; i < objfile->num_sections; ++i)
b8fbeb18
EZ
2997 {
2998 /* syms_from_objfile kindly subtracts from addr the
2999 bfd_section_vma of the .text section. This strikes me as
3000 wrong--whether the offset to be applied to symbol reading is
3001 relative to the start address of the section depends on the
3002 symbol format. In any event, this whole "addr" concept is
3003 pretty broken (it doesn't handle any section but .text
3004 sensibly), so just ignore the addr parameter and use 0.
3005 rs6000-nat.c will set the correct section offsets via
3006 objfile_relocate. */
f0a58b0b 3007 (objfile->section_offsets)->offsets[i] = 0;
b8fbeb18 3008 }
c906108c
SS
3009}
3010
3011/* Register our ability to parse symbols for xcoff BFD files. */
3012
3013static struct sym_fns xcoff_sym_fns =
3014{
3015
7a78ae4e 3016 /* It is possible that coff and xcoff should be merged as
c906108c
SS
3017 they do have fundamental similarities (for example, the extra storage
3018 classes used for stabs could presumably be recognized in any COFF file).
3019 However, in addition to obvious things like all the csect hair, there are
3020 some subtler differences between xcoffread.c and coffread.c, notably
3021 the fact that coffread.c has no need to read in all the symbols, but
3022 xcoffread.c reads all the symbols and does in fact randomly access them
3023 (in C_BSTAT and line number processing). */
3024
7a78ae4e 3025 bfd_target_xcoff_flavour,
c906108c 3026
c5aa993b
JM
3027 xcoff_new_init, /* sym_new_init: init anything gbl to entire symtab */
3028 xcoff_symfile_init, /* sym_init: read initial info, setup for sym_read() */
3029 xcoff_initial_scan, /* sym_read: read a symbol file into symtab */
3030 xcoff_symfile_finish, /* sym_finish: finished with file, cleanup */
3031 xcoff_symfile_offsets, /* sym_offsets: xlate offsets ext->int form */
3032 NULL /* next: pointer to next struct sym_fns */
c906108c
SS
3033};
3034
3035void
fba45db2 3036_initialize_xcoffread (void)
c906108c 3037{
c5aa993b 3038 add_symtab_fns (&xcoff_sym_fns);
c906108c
SS
3039
3040 func_symbol_type = init_type (TYPE_CODE_FUNC, 1, 0,
3041 "<function, no debug info>", NULL);
3042 TYPE_TARGET_TYPE (func_symbol_type) = builtin_type_int;
3043 var_symbol_type =
3044 init_type (TYPE_CODE_INT, TARGET_INT_BIT / HOST_CHAR_BIT, 0,
3045 "<variable, no debug info>", NULL);
3046}
This page took 0.425839 seconds and 4 git commands to generate.