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