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