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