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