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