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