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