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