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