* gregset.h: Protect against multiple inclusion. Remove some
[deliverable/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
470d5666 229static CORE_ADDR 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 264static void
fba45db2 265find_targ_sec (bfd *abfd, asection *sect, PTR obj)
c906108c 266{
c5aa993b 267 struct find_targ_sec_arg *args = (struct find_targ_sec_arg *) obj;
b8fbeb18 268 struct objfile *objfile = args->objfile;
c906108c
SS
269 if (sect->target_index == args->targ_index)
270 {
271 /* This is the section. Figure out what SECT_OFF_* code it is. */
272 if (bfd_get_section_flags (abfd, sect) & SEC_CODE)
b8fbeb18 273 *args->resultp = SECT_OFF_TEXT (objfile);
c906108c 274 else if (bfd_get_section_flags (abfd, sect) & SEC_LOAD)
b8fbeb18 275 *args->resultp = SECT_OFF_DATA (objfile);
c906108c 276 else
44af9391 277 *args->resultp = sect->index;
c906108c
SS
278 *args->bfd_sect = sect;
279 }
280}
281
282/* Return the section number (SECT_OFF_*) that CS points to. */
283static int
fba45db2 284secnum_to_section (int secnum, struct objfile *objfile)
c906108c 285{
b8fbeb18 286 int off = SECT_OFF_TEXT (objfile);
c906108c
SS
287 asection *sect = NULL;
288 struct find_targ_sec_arg args;
289 args.targ_index = secnum;
290 args.resultp = &off;
291 args.bfd_sect = &sect;
b8fbeb18 292 args.objfile = objfile;
c906108c
SS
293 bfd_map_over_sections (objfile->obfd, find_targ_sec, &args);
294 return off;
295}
296
297/* Return the BFD section that CS points to. */
298static asection *
fba45db2 299secnum_to_bfd_section (int secnum, struct objfile *objfile)
c906108c 300{
b8fbeb18 301 int off = SECT_OFF_TEXT (objfile);
c906108c
SS
302 asection *sect = NULL;
303 struct find_targ_sec_arg args;
304 args.targ_index = secnum;
305 args.resultp = &off;
306 args.bfd_sect = &sect;
7a78ae4e 307 args.objfile = objfile;
c906108c
SS
308 bfd_map_over_sections (objfile->obfd, find_targ_sec, &args);
309 return sect;
310}
311\f
312/* add a given stab string into given stab vector. */
313
314#if 0
315
316static void
fba45db2 317add_stab_to_list (char *stabname, struct pending_stabs **stabvector)
c906108c 318{
c5aa993b
JM
319 if (*stabvector == NULL)
320 {
321 *stabvector = (struct pending_stabs *)
322 xmalloc (sizeof (struct pending_stabs) +
323 INITIAL_STABVECTOR_LENGTH * sizeof (char *));
324 (*stabvector)->count = 0;
325 (*stabvector)->length = INITIAL_STABVECTOR_LENGTH;
326 }
327 else if ((*stabvector)->count >= (*stabvector)->length)
328 {
329 (*stabvector)->length += INITIAL_STABVECTOR_LENGTH;
330 *stabvector = (struct pending_stabs *)
331 xrealloc ((char *) *stabvector, sizeof (struct pending_stabs) +
332 (*stabvector)->length * sizeof (char *));
333 }
334 (*stabvector)->stab[(*stabvector)->count++] = stabname;
c906108c
SS
335}
336
337#endif
c5aa993b 338\f/* *INDENT-OFF* */
c906108c
SS
339/* Linenos are processed on a file-by-file basis.
340
341 Two reasons:
342
c5aa993b
JM
343 1) xlc (IBM's native c compiler) postpones static function code
344 emission to the end of a compilation unit. This way it can
345 determine if those functions (statics) are needed or not, and
346 can do some garbage collection (I think). This makes line
347 numbers and corresponding addresses unordered, and we end up
348 with a line table like:
349
350
351 lineno addr
352 foo() 10 0x100
353 20 0x200
354 30 0x300
355
356 foo3() 70 0x400
357 80 0x500
358 90 0x600
359
360 static foo2()
361 40 0x700
362 50 0x800
363 60 0x900
364
365 and that breaks gdb's binary search on line numbers, if the
366 above table is not sorted on line numbers. And that sort
367 should be on function based, since gcc can emit line numbers
368 like:
369
370 10 0x100 - for the init/test part of a for stmt.
371 20 0x200
372 30 0x300
373 10 0x400 - for the increment part of a for stmt.
374
375 arrange_linetable() will do this sorting.
376
377 2) aix symbol table might look like:
378
379 c_file // beginning of a new file
380 .bi // beginning of include file
381 .ei // end of include file
382 .bi
383 .ei
384
385 basically, .bi/.ei pairs do not necessarily encapsulate
386 their scope. They need to be recorded, and processed later
387 on when we come the end of the compilation unit.
388 Include table (inclTable) and process_linenos() handle
389 that. */
9846de1b 390/* *INDENT-ON* */
c906108c 391
c5aa993b
JM
392
393
c906108c
SS
394/* compare line table entry addresses. */
395
396static int
fba45db2 397compare_lte (const void *lte1p, const void *lte2p)
c906108c
SS
398{
399 struct linetable_entry *lte1 = (struct linetable_entry *) lte1p;
400 struct linetable_entry *lte2 = (struct linetable_entry *) lte2p;
401 return lte1->pc - lte2->pc;
402}
403
404/* Given a line table with function entries are marked, arrange its functions
405 in ascending order and strip off function entry markers and return it in
406 a newly created table. If the old one is good enough, return the old one. */
407/* FIXME: I think all this stuff can be replaced by just passing
408 sort_linevec = 1 to end_symtab. */
409
410static struct linetable *
411arrange_linetable (oldLineTb)
c5aa993b 412 struct linetable *oldLineTb; /* old linetable */
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 {
447 free (fentry);
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 }
c906108c
SS
477 free (fentry);
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 {
679 free (lv);
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);
c906108c
SS
729 free (current_subfile->name);
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 {
744 free (lv);
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);
7a78ae4e
ND
820 bfd_read (ext_lnno, linesz, 1, abfd);
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
1084 /* if explicitly specified as a function, treat is as one. */
c5aa993b 1085 if (ISFCN (cs->c_type) && cs->c_sclass != C_TPDEF)
c906108c
SS
1086 {
1087 bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
1088 0, cs->c_naux, &main_aux);
1089 goto function_entry_point;
1090 }
1091
1092 if ((cs->c_sclass == C_EXT || cs->c_sclass == C_HIDEXT)
1093 && cs->c_naux == 1)
1094 {
1095 /* Dealing with a symbol with a csect entry. */
1096
1097#define CSECT(PP) ((PP)->x_csect)
1098#define CSECT_LEN(PP) (CSECT(PP).x_scnlen.l)
1099#define CSECT_ALIGN(PP) (SMTYP_ALIGN(CSECT(PP).x_smtyp))
1100#define CSECT_SMTYP(PP) (SMTYP_SMTYP(CSECT(PP).x_smtyp))
1101#define CSECT_SCLAS(PP) (CSECT(PP).x_smclas)
1102
1103 /* Convert the auxent to something we can access. */
1104 bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
1105 0, cs->c_naux, &main_aux);
1106
1107 switch (CSECT_SMTYP (&main_aux))
1108 {
1109
1110 case XTY_ER:
1111 /* Ignore all external references. */
1112 continue;
1113
1114 case XTY_SD:
1115 /* A section description. */
1116 {
1117 switch (CSECT_SCLAS (&main_aux))
1118 {
1119
1120 case XMC_PR:
1121 {
1122
1123 /* A program csect is seen. We have to allocate one
c5aa993b
JM
1124 symbol table for each program csect. Normally gdb
1125 prefers one symtab for each source file. In case
1126 of AIX, one source file might include more than one
1127 [PR] csect, and they don't have to be adjacent in
1128 terms of the space they occupy in memory. Thus, one
1129 single source file might get fragmented in the
1130 memory and gdb's file start and end address
1131 approach does not work! GCC (and I think xlc) seem
1132 to put all the code in the unnamed program csect. */
c906108c
SS
1133
1134 if (last_csect_name)
1135 {
1136 complete_symtab (filestring, file_start_addr);
1137 cur_src_end_addr = file_end_addr;
b8fbeb18 1138 end_symtab (file_end_addr, objfile, SECT_OFF_TEXT (objfile));
c906108c
SS
1139 end_stabs ();
1140 start_stabs ();
1141 /* Give all csects for this source file the same
1142 name. */
c5aa993b 1143 start_symtab (filestring, NULL, (CORE_ADDR) 0);
7a78ae4e 1144 record_debugformat (debugfmt);
c906108c
SS
1145 }
1146
1147 /* If this is the very first csect seen,
c5aa993b 1148 basically `__start'. */
c906108c
SS
1149 if (just_started)
1150 {
1151 first_object_file_end
1152 = cs->c_value + CSECT_LEN (&main_aux);
1153 just_started = 0;
1154 }
1155
1156 file_start_addr =
1157 cs->c_value + ANOFFSET (objfile->section_offsets,
b8fbeb18 1158 SECT_OFF_TEXT (objfile));
c906108c
SS
1159 file_end_addr = file_start_addr + CSECT_LEN (&main_aux);
1160
1161 if (cs->c_name && cs->c_name[0] == '.')
1162 {
1163 last_csect_name = cs->c_name;
1164 last_csect_val = cs->c_value;
1165 last_csect_sec = secnum_to_section (cs->c_secnum, objfile);
1166 }
1167 }
1168 continue;
1169
1170 /* All other symbols are put into the minimal symbol
1171 table only. */
1172
1173 case XMC_RW:
1174 continue;
1175
1176 case XMC_TC0:
1177 continue;
1178
1179 case XMC_TC:
1180 continue;
1181
1182 default:
1183 /* Ignore the symbol. */
1184 continue;
1185 }
1186 }
1187 break;
1188
1189 case XTY_LD:
1190
1191 switch (CSECT_SCLAS (&main_aux))
1192 {
1193 case XMC_PR:
1194 /* a function entry point. */
1195 function_entry_point:
1196
1197 fcn_start_addr = cs->c_value;
1198
1199 /* save the function header info, which will be used
1200 when `.bf' is seen. */
1201 fcn_cs_saved = *cs;
1202 fcn_aux_saved = main_aux;
1203 continue;
1204
1205 case XMC_GL:
1206 /* shared library function trampoline code entry point. */
1207 continue;
1208
1209 case XMC_DS:
1210 /* The symbols often have the same names as debug symbols for
1211 functions, and confuse lookup_symbol. */
1212 continue;
1213
1214 default:
1215 /* xlc puts each variable in a separate csect, so we get
1216 an XTY_SD for each variable. But gcc puts several
1217 variables in a csect, so that each variable only gets
1218 an XTY_LD. This will typically be XMC_RW; I suspect
1219 XMC_RO and XMC_BS might be possible too.
1220 These variables are put in the minimal symbol table
1221 only. */
1222 continue;
1223 }
1224 break;
1225
1226 case XTY_CM:
1227 /* Common symbols are put into the minimal symbol table only. */
1228 continue;
1229
1230 default:
1231 break;
1232 }
1233 }
1234
1235 switch (cs->c_sclass)
1236 {
1237
1238 case C_FILE:
1239
1240 /* c_value field contains symnum of next .file entry in table
1241 or symnum of first global after last .file. */
1242
1243 next_file_symnum = cs->c_value;
1244
1245 /* Complete symbol table for last object file containing
1246 debugging information. */
1247
1248 /* Whether or not there was a csect in the previous file, we
1249 have to call `end_stabs' and `start_stabs' to reset
1250 type_vector, line_vector, etc. structures. */
1251
1252 complete_symtab (filestring, file_start_addr);
1253 cur_src_end_addr = file_end_addr;
b8fbeb18 1254 end_symtab (file_end_addr, objfile, SECT_OFF_TEXT (objfile));
c906108c
SS
1255 end_stabs ();
1256
1257 /* XCOFF, according to the AIX 3.2 documentation, puts the filename
1258 in cs->c_name. But xlc 1.3.0.2 has decided to do things the
1259 standard COFF way and put it in the auxent. We use the auxent if
1260 the symbol is ".file" and an auxent exists, otherwise use the symbol
1261 itself. Simple enough. */
1262 if (!strcmp (cs->c_name, ".file") && cs->c_naux > 0)
1263 {
1264 bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
1265 0, cs->c_naux, &main_aux);
1266 filestring = coff_getfilename (&main_aux, objfile);
1267 }
1268 else
1269 filestring = cs->c_name;
1270
1271 start_stabs ();
c5aa993b 1272 start_symtab (filestring, (char *) NULL, (CORE_ADDR) 0);
7a78ae4e 1273 record_debugformat (debugfmt);
c906108c
SS
1274 last_csect_name = 0;
1275
1276 /* reset file start and end addresses. A compilation unit with no text
1277 (only data) should have zero file boundaries. */
1278 file_start_addr = file_end_addr = 0;
1279 break;
1280
1281 case C_FUN:
1282 fcn_stab_saved = *cs;
1283 break;
1284
1285 case C_FCN:
1286 if (STREQ (cs->c_name, ".bf"))
1287 {
1288 CORE_ADDR off = ANOFFSET (objfile->section_offsets,
b8fbeb18 1289 SECT_OFF_TEXT (objfile));
c906108c
SS
1290 bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
1291 0, cs->c_naux, &main_aux);
1292
1293 within_function = 1;
1294
1295 new = push_context (0, fcn_start_addr + off);
1296
c5aa993b 1297 new->name = define_symbol
c906108c
SS
1298 (fcn_cs_saved.c_value + off,
1299 fcn_stab_saved.c_name, 0, 0, objfile);
1300 if (new->name != NULL)
b8fbeb18 1301 SYMBOL_SECTION (new->name) = SECT_OFF_TEXT (objfile);
c906108c
SS
1302 }
1303 else if (STREQ (cs->c_name, ".ef"))
1304 {
1305
1306 bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
1307 0, cs->c_naux, &main_aux);
1308
1309 /* The value of .ef is the address of epilogue code;
c5aa993b 1310 not useful for gdb. */
c906108c 1311 /* { main_aux.x_sym.x_misc.x_lnsz.x_lnno
c5aa993b 1312 contains number of lines to '}' */
c906108c
SS
1313
1314 if (context_stack_depth <= 0)
1315 { /* We attempted to pop an empty context stack */
1316 complain (&ef_complaint, cs->c_symnum);
1317 within_function = 0;
1318 break;
1319 }
1320 new = pop_context ();
1321 /* Stack must be empty now. */
1322 if (context_stack_depth > 0 || new == NULL)
1323 {
1324 complain (&ef_complaint, cs->c_symnum);
1325 within_function = 0;
1326 break;
1327 }
1328
1329 finish_block (new->name, &local_symbols, new->old_blocks,
1330 new->start_addr,
1331 (fcn_cs_saved.c_value
1332 + fcn_aux_saved.x_sym.x_misc.x_fsize
1333 + ANOFFSET (objfile->section_offsets,
b8fbeb18 1334 SECT_OFF_TEXT (objfile))),
c906108c
SS
1335 objfile);
1336 within_function = 0;
1337 }
1338 break;
1339
1340 case C_BSTAT:
1341 /* Begin static block. */
1342 {
1343 struct internal_syment symbol;
1344
1345 read_symbol (&symbol, cs->c_value);
1346 static_block_base = symbol.n_value;
1347 static_block_section =
1348 secnum_to_section (symbol.n_scnum, objfile);
1349 }
1350 break;
1351
1352 case C_ESTAT:
1353 /* End of static block. */
1354 static_block_base = 0;
1355 static_block_section = -1;
1356 break;
1357
1358 case C_ARG:
1359 case C_REGPARM:
1360 case C_REG:
1361 case C_TPDEF:
1362 case C_STRTAG:
1363 case C_UNTAG:
1364 case C_ENTAG:
1365 {
1366 static struct complaint msg =
c5aa993b 1367 {"Unrecognized storage class %d.", 0, 0};
c906108c
SS
1368 complain (&msg, cs->c_sclass);
1369 }
1370 break;
1371
1372 case C_LABEL:
1373 case C_NULL:
1374 /* Ignore these. */
1375 break;
1376
1377 case C_HIDEXT:
1378 case C_STAT:
1379 break;
1380
1381 case C_BINCL:
1382 /* beginning of include file */
1383 /* In xlc output, C_BINCL/C_EINCL pair doesn't show up in sorted
1384 order. Thus, when wee see them, we might not know enough info
1385 to process them. Thus, we'll be saving them into a table
1386 (inclTable) and postpone their processing. */
1387
1388 record_include_begin (cs);
1389 break;
1390
1391 case C_EINCL:
1392 /* End of include file. */
1393 /* See the comment after case C_BINCL. */
1394 record_include_end (cs);
1395 break;
1396
1397 case C_BLOCK:
1398 if (STREQ (cs->c_name, ".bb"))
1399 {
1400 depth++;
1401 new = push_context (depth,
1402 (cs->c_value
1403 + ANOFFSET (objfile->section_offsets,
b8fbeb18 1404 SECT_OFF_TEXT (objfile))));
c906108c
SS
1405 }
1406 else if (STREQ (cs->c_name, ".eb"))
1407 {
1408 if (context_stack_depth <= 0)
1409 { /* We attempted to pop an empty context stack */
1410 complain (&eb_complaint, cs->c_symnum);
1411 break;
1412 }
1413 new = pop_context ();
1414 if (depth-- != new->depth)
1415 {
1416 complain (&eb_complaint, cs->c_symnum);
1417 break;
1418 }
1419 if (local_symbols && context_stack_depth > 0)
1420 {
1421 /* Make a block for the local symbols within. */
1422 finish_block (new->name, &local_symbols, new->old_blocks,
1423 new->start_addr,
1424 (cs->c_value
1425 + ANOFFSET (objfile->section_offsets,
b8fbeb18 1426 SECT_OFF_TEXT (objfile))),
c906108c
SS
1427 objfile);
1428 }
1429 local_symbols = new->locals;
1430 }
1431 break;
1432
1433 default:
1434 process_xcoff_symbol (cs, objfile);
1435 break;
1436 }
1437 }
1438
1439 if (last_source_file)
1440 {
1441 struct symtab *s;
1442
1443 complete_symtab (filestring, file_start_addr);
1444 cur_src_end_addr = file_end_addr;
b8fbeb18 1445 s = end_symtab (file_end_addr, objfile, SECT_OFF_TEXT (objfile));
c906108c
SS
1446 /* When reading symbols for the last C_FILE of the objfile, try
1447 to make sure that we set pst->symtab to the symtab for the
1448 file, not to the _globals_ symtab. I'm not sure whether this
1449 actually works right or when/if it comes up. */
1450 if (pst->symtab == NULL)
1451 pst->symtab = s;
1452 end_stabs ();
1453 }
1454}
1455
1456#define SYMBOL_DUP(SYMBOL1, SYMBOL2) \
1457 (SYMBOL2) = (struct symbol *) \
1458 obstack_alloc (&objfile->symbol_obstack, sizeof (struct symbol)); \
1459 *(SYMBOL2) = *(SYMBOL1);
c5aa993b
JM
1460
1461
c906108c
SS
1462#define SYMNAME_ALLOC(NAME, ALLOCED) \
1463 (ALLOCED) ? (NAME) : obsavestring ((NAME), strlen (NAME), &objfile->symbol_obstack);
1464
1465
1466static struct type *func_symbol_type;
1467static struct type *var_symbol_type;
1468
1469/* process one xcoff symbol. */
1470
1471static struct symbol *
fba45db2 1472process_xcoff_symbol (register struct coff_symbol *cs, struct objfile *objfile)
c906108c
SS
1473{
1474 struct symbol onesymbol;
1475 register struct symbol *sym = &onesymbol;
1476 struct symbol *sym2 = NULL;
1477 char *name, *pp;
1478
1479 int sec;
1480 CORE_ADDR off;
1481
1482 if (cs->c_secnum < 0)
1483 {
1484 /* The value is a register number, offset within a frame, etc.,
c5aa993b 1485 and does not get relocated. */
c906108c
SS
1486 off = 0;
1487 sec = -1;
1488 }
1489 else
1490 {
1491 sec = secnum_to_section (cs->c_secnum, objfile);
1492 off = ANOFFSET (objfile->section_offsets, sec);
1493 }
1494
1495 name = cs->c_name;
1496 if (name[0] == '.')
1497 ++name;
1498
1499 memset (sym, '\0', sizeof (struct symbol));
1500
1501 /* default assumptions */
7a78ae4e 1502 SYMBOL_VALUE_ADDRESS (sym) = cs->c_value + off;
c906108c
SS
1503 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1504 SYMBOL_SECTION (sym) = secnum_to_section (cs->c_secnum, objfile);
1505
1506 if (ISFCN (cs->c_type))
1507 {
1508 /* At this point, we don't know the type of the function. This
c5aa993b
JM
1509 will be patched with the type from its stab entry later on in
1510 patch_block_stabs (), unless the file was compiled without -g. */
c906108c
SS
1511
1512 SYMBOL_NAME (sym) = SYMNAME_ALLOC (name, symname_alloced);
1513 SYMBOL_TYPE (sym) = func_symbol_type;
1514
1515 SYMBOL_CLASS (sym) = LOC_BLOCK;
1516 SYMBOL_DUP (sym, sym2);
1517
1518 if (cs->c_sclass == C_EXT)
1519 add_symbol_to_list (sym2, &global_symbols);
1520 else if (cs->c_sclass == C_HIDEXT || cs->c_sclass == C_STAT)
1521 add_symbol_to_list (sym2, &file_symbols);
1522 }
1523 else
1524 {
1525 /* In case we can't figure out the type, provide default. */
1526 SYMBOL_TYPE (sym) = var_symbol_type;
1527
1528 switch (cs->c_sclass)
1529 {
1530#if 0
c5aa993b
JM
1531 /* The values of functions and global symbols are now resolved
1532 via the global_sym_chain in stabsread.c. */
c906108c
SS
1533 case C_FUN:
1534 if (fcn_cs_saved.c_sclass == C_EXT)
1535 add_stab_to_list (name, &global_stabs);
1536 else
1537 add_stab_to_list (name, &file_stabs);
1538 break;
1539
1540 case C_GSYM:
1541 add_stab_to_list (name, &global_stabs);
1542 break;
1543#endif
1544
1545 case C_BCOMM:
1546 common_block_start (cs->c_name, objfile);
1547 break;
1548
1549 case C_ECOMM:
1550 common_block_end (objfile);
1551 break;
1552
1553 default:
1554 complain (&storclass_complaint, cs->c_sclass);
1555 /* FALLTHROUGH */
1556
1557 case C_DECL:
1558 case C_PSYM:
1559 case C_RPSYM:
1560 case C_ECOML:
1561 case C_LSYM:
1562 case C_RSYM:
1563 case C_GSYM:
1564
1565 {
1566 sym = define_symbol (cs->c_value + off, cs->c_name, 0, 0, objfile);
1567 if (sym != NULL)
1568 {
1569 SYMBOL_SECTION (sym) = sec;
1570 }
1571 return sym;
1572 }
1573
1574 case C_STSYM:
1575
1576 /* For xlc (not GCC), the 'V' symbol descriptor is used for
1577 all statics and we need to distinguish file-scope versus
1578 function-scope using within_function. We do this by
1579 changing the string we pass to define_symbol to use 'S'
1580 where we need to, which is not necessarily super-clean,
1581 but seems workable enough. */
1582
c5aa993b 1583 if (*name == ':' || (pp = (char *) strchr (name, ':')) == NULL)
c906108c
SS
1584 return NULL;
1585
1586 ++pp;
1587 if (*pp == 'V' && !within_function)
1588 *pp = 'S';
1589 sym = define_symbol ((cs->c_value
1590 + ANOFFSET (objfile->section_offsets,
1591 static_block_section)),
1592 cs->c_name, 0, 0, objfile);
1593 if (sym != NULL)
1594 {
7a78ae4e 1595 SYMBOL_VALUE_ADDRESS (sym) += static_block_base;
c906108c
SS
1596 SYMBOL_SECTION (sym) = static_block_section;
1597 }
1598 return sym;
1599
1600 }
1601 }
1602 return sym2;
1603}
1604
1605/* Extract the file name from the aux entry of a C_FILE symbol.
1606 Result is in static storage and is only good for temporary use. */
1607
1608static char *
fba45db2 1609coff_getfilename (union internal_auxent *aux_entry, struct objfile *objfile)
c906108c
SS
1610{
1611 static char buffer[BUFSIZ];
1612
1613 if (aux_entry->x_file.x_n.x_zeroes == 0)
1614 strcpy (buffer,
c5aa993b 1615 ((struct coff_symfile_info *) objfile->sym_private)->strtbl
c906108c
SS
1616 + aux_entry->x_file.x_n.x_offset);
1617 else
1618 {
1619 strncpy (buffer, aux_entry->x_file.x_fname, FILNMLEN);
1620 buffer[FILNMLEN] = '\0';
1621 }
1622 return (buffer);
1623}
1624
1625/* Set *SYMBOL to symbol number symno in symtbl. */
1626static void
fba45db2 1627read_symbol (struct internal_syment *symbol, int symno)
c906108c
SS
1628{
1629 int nsyms =
c5aa993b
JM
1630 ((struct coff_symfile_info *) this_symtab_psymtab->objfile->sym_private)
1631 ->symtbl_num_syms;
1632 char *stbl =
1633 ((struct coff_symfile_info *) this_symtab_psymtab->objfile->sym_private)
1634 ->symtbl;
c906108c
SS
1635 if (symno < 0 || symno >= nsyms)
1636 {
1637 static struct complaint msg =
c5aa993b 1638 {"Invalid symbol offset", 0, 0};
c906108c
SS
1639 complain (&msg);
1640 symbol->n_value = 0;
1641 symbol->n_scnum = -1;
1642 return;
1643 }
1644 bfd_coff_swap_sym_in (this_symtab_psymtab->objfile->obfd,
c5aa993b 1645 stbl + (symno * local_symesz),
c906108c
SS
1646 symbol);
1647}
c5aa993b 1648
c906108c
SS
1649/* Get value corresponding to symbol number symno in symtbl. */
1650
470d5666 1651static CORE_ADDR
fba45db2 1652read_symbol_nvalue (int symno)
c906108c
SS
1653{
1654 struct internal_syment symbol[1];
1655
1656 read_symbol (symbol, symno);
c5aa993b 1657 return symbol->n_value;
c906108c
SS
1658}
1659
1660
1661/* Find the address of the function corresponding to symno, where
1662 symno is the symbol pointed to by the linetable. */
1663
1664static int
fba45db2 1665read_symbol_lineno (int symno)
c906108c 1666{
7a78ae4e
ND
1667 struct objfile *objfile = this_symtab_psymtab->objfile;
1668 boolean xcoff64 = xcoff_data (objfile->obfd)->xcoff64;
1669
1670 struct coff_symfile_info *info =
1671 (struct coff_symfile_info *)objfile->sym_private;
1672 int nsyms = info->symtbl_num_syms;
1673 char *stbl = info->symtbl;
1674 char *strtbl = info->strtbl;
1675
c906108c
SS
1676 struct internal_syment symbol[1];
1677 union internal_auxent main_aux[1];
1678
1679 if (symno < 0)
1680 {
1681 complain (&bf_notfound_complaint);
1682 return 0;
1683 }
1684
1685 /* Note that just searching for a short distance (e.g. 50 symbols)
1686 is not enough, at least in the following case.
1687
1688 .extern foo
1689 [many .stabx entries]
1690 [a few functions, referring to foo]
1691 .globl foo
1692 .bf
1693
1694 What happens here is that the assembler moves the .stabx entries
1695 to right before the ".bf" for foo, but the symbol for "foo" is before
1696 all the stabx entries. See PR gdb/2222. */
1697
1698 /* Maintaining a table of .bf entries might be preferable to this search.
1699 If I understand things correctly it would need to be done only for
1700 the duration of a single psymtab to symtab conversion. */
1701 while (symno < nsyms)
1702 {
1703 bfd_coff_swap_sym_in (symfile_bfd,
1704 stbl + (symno * local_symesz), symbol);
7a78ae4e
ND
1705 if (symbol->n_sclass == C_FCN)
1706 {
1707 char *name = xcoff64 ? strtbl + symbol->n_offset : symbol->n_name;
1708 if (STREQ (name, ".bf"))
1709 goto gotit;
1710 }
c906108c
SS
1711 symno += symbol->n_numaux + 1;
1712 }
1713
1714 complain (&bf_notfound_complaint);
1715 return 0;
1716
1717gotit:
1718 /* take aux entry and return its lineno */
1719 symno++;
7a78ae4e 1720 bfd_coff_swap_aux_in (objfile->obfd, stbl + symno * local_symesz,
c906108c
SS
1721 symbol->n_type, symbol->n_sclass,
1722 0, symbol->n_numaux, main_aux);
1723
1724 return main_aux->x_sym.x_misc.x_lnsz.x_lnno;
1725}
1726
1727/* Support for line number handling */
1728
1729/* This function is called for every section; it finds the outer limits
1730 * of the line table (minimum and maximum file offset) so that the
1731 * mainline code can read the whole thing for efficiency.
1732 */
1733static void
fba45db2 1734find_linenos (bfd *abfd, sec_ptr asect, PTR vpinfo)
c906108c
SS
1735{
1736 struct coff_symfile_info *info;
1737 int size, count;
1738 file_ptr offset, maxoff;
1739
1740 count = asect->lineno_count;
1741
1742 if (!STREQ (asect->name, ".text") || count == 0)
1743 return;
1744
1745 size = count * coff_data (abfd)->local_linesz;
c5aa993b 1746 info = (struct coff_symfile_info *) vpinfo;
c906108c
SS
1747 offset = asect->line_filepos;
1748 maxoff = offset + size;
1749
1750 if (offset < info->min_lineno_offset || info->min_lineno_offset == 0)
1751 info->min_lineno_offset = offset;
1752
1753 if (maxoff > info->max_lineno_offset)
1754 info->max_lineno_offset = maxoff;
1755}
1756\f
a14ed312 1757static void xcoff_psymtab_to_symtab_1 (struct partial_symtab *);
c906108c
SS
1758
1759static void
fba45db2 1760xcoff_psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c
SS
1761{
1762 struct cleanup *old_chain;
1763 int i;
c5aa993b 1764
c906108c
SS
1765 if (!pst)
1766 return;
1767
1768 if (pst->readin)
1769 {
1770 fprintf_unfiltered
1771 (gdb_stderr, "Psymtab for %s already read in. Shouldn't happen.\n",
1772 pst->filename);
1773 return;
1774 }
1775
1776 /* Read in all partial symtabs on which this one is dependent */
1777 for (i = 0; i < pst->number_of_dependencies; i++)
1778 if (!pst->dependencies[i]->readin)
1779 {
1780 /* Inform about additional files that need to be read in. */
1781 if (info_verbose)
1782 {
1783 fputs_filtered (" ", gdb_stdout);
1784 wrap_here ("");
1785 fputs_filtered ("and ", gdb_stdout);
1786 wrap_here ("");
1787 printf_filtered ("%s...", pst->dependencies[i]->filename);
c5aa993b 1788 wrap_here (""); /* Flush output */
c906108c
SS
1789 gdb_flush (gdb_stdout);
1790 }
1791 xcoff_psymtab_to_symtab_1 (pst->dependencies[i]);
1792 }
1793
c5aa993b 1794 if (((struct symloc *) pst->read_symtab_private)->numsyms != 0)
c906108c
SS
1795 {
1796 /* Init stuff necessary for reading in symbols. */
1797 stabsread_init ();
1798 buildsym_init ();
a0b3c4fd 1799 old_chain = make_cleanup (really_free_pendings, 0);
c906108c
SS
1800
1801 read_xcoff_symtab (pst);
1802 sort_symtab_syms (pst->symtab);
1803
1804 do_cleanups (old_chain);
1805 }
1806
1807 pst->readin = 1;
1808}
1809
a14ed312 1810static void xcoff_psymtab_to_symtab (struct partial_symtab *);
c906108c
SS
1811
1812/* Read in all of the symbols for a given psymtab for real.
1813 Be verbose about it if the user wants that. */
1814
1815static void
fba45db2 1816xcoff_psymtab_to_symtab (struct partial_symtab *pst)
c906108c
SS
1817{
1818 bfd *sym_bfd;
1819
1820 if (!pst)
1821 return;
1822
1823 if (pst->readin)
1824 {
1825 fprintf_unfiltered
1826 (gdb_stderr, "Psymtab for %s already read in. Shouldn't happen.\n",
1827 pst->filename);
1828 return;
1829 }
1830
c5aa993b 1831 if (((struct symloc *) pst->read_symtab_private)->numsyms != 0
c906108c
SS
1832 || pst->number_of_dependencies)
1833 {
1834 /* Print the message now, before reading the string table,
c5aa993b 1835 to avoid disconcerting pauses. */
c906108c
SS
1836 if (info_verbose)
1837 {
1838 printf_filtered ("Reading in symbols for %s...", pst->filename);
1839 gdb_flush (gdb_stdout);
1840 }
1841
1842 sym_bfd = pst->objfile->obfd;
1843
1844 next_symbol_text_func = xcoff_next_symbol_text;
1845
1846 xcoff_psymtab_to_symtab_1 (pst);
1847
1848 /* Match with global symbols. This only needs to be done once,
1849 after all of the symtabs and dependencies have been read in. */
1850 scan_file_globals (pst->objfile);
1851
1852 /* Finish up the debug error message. */
1853 if (info_verbose)
1854 printf_filtered ("done.\n");
1855 }
1856}
1857\f
1858static void
fba45db2 1859xcoff_new_init (struct objfile *objfile)
c906108c
SS
1860{
1861 stabsread_new_init ();
1862 buildsym_new_init ();
1863}
1864
1865/* Do initialization in preparation for reading symbols from OBJFILE.
c5aa993b 1866
c906108c
SS
1867 We will only be called if this is an XCOFF or XCOFF-like file.
1868 BFD handles figuring out the format of the file, and code in symfile.c
1869 uses BFD's determination to vector to us. */
1870
1871static void
fba45db2 1872xcoff_symfile_init (struct objfile *objfile)
c906108c
SS
1873{
1874 /* Allocate struct to keep track of the symfile */
c5aa993b
JM
1875 objfile->sym_private = xmmalloc (objfile->md,
1876 sizeof (struct coff_symfile_info));
c906108c
SS
1877
1878 /* XCOFF objects may be reordered, so set OBJF_REORDERED. If we
1879 find this causes a significant slowdown in gdb then we could
1880 set it in the debug symbol readers only when necessary. */
1881 objfile->flags |= OBJF_REORDERED;
1882
1883 init_entry_point_info (objfile);
1884}
1885
1886/* Perform any local cleanups required when we are done with a particular
1887 objfile. I.E, we are in the process of discarding all symbol information
1888 for an objfile, freeing up all memory held for it, and unlinking the
1889 objfile struct from the global list of known objfiles. */
1890
1891static void
fba45db2 1892xcoff_symfile_finish (struct objfile *objfile)
c906108c 1893{
c5aa993b 1894 if (objfile->sym_private != NULL)
c906108c 1895 {
c5aa993b 1896 mfree (objfile->md, objfile->sym_private);
c906108c
SS
1897 }
1898
1899 /* Start with a fresh include table for the next objfile. */
1900 if (inclTable)
1901 {
1902 free (inclTable);
1903 inclTable = NULL;
1904 }
1905 inclIndx = inclLength = inclDepth = 0;
1906}
1907
1908
1909static void
fba45db2 1910init_stringtab (bfd *abfd, file_ptr offset, struct objfile *objfile)
c906108c
SS
1911{
1912 long length;
1913 int val;
1914 unsigned char lengthbuf[4];
1915 char *strtbl;
1916
c5aa993b 1917 ((struct coff_symfile_info *) objfile->sym_private)->strtbl = NULL;
c906108c
SS
1918
1919 if (bfd_seek (abfd, offset, SEEK_SET) < 0)
1920 error ("cannot seek to string table in %s: %s",
1921 bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
1922
c5aa993b 1923 val = bfd_read ((char *) lengthbuf, 1, sizeof lengthbuf, abfd);
c906108c
SS
1924 length = bfd_h_get_32 (abfd, lengthbuf);
1925
1926 /* If no string table is needed, then the file may end immediately
1927 after the symbols. Just return with `strtbl' set to NULL. */
1928
1929 if (val != sizeof lengthbuf || length < sizeof lengthbuf)
1930 return;
1931
1932 /* Allocate string table from symbol_obstack. We will need this table
1933 as long as we have its symbol table around. */
1934
1935 strtbl = (char *) obstack_alloc (&objfile->symbol_obstack, length);
c5aa993b 1936 ((struct coff_symfile_info *) objfile->sym_private)->strtbl = strtbl;
c906108c
SS
1937
1938 /* Copy length buffer, the first byte is usually zero and is
1939 used for stabs with a name length of zero. */
1940 memcpy (strtbl, lengthbuf, sizeof lengthbuf);
1941 if (length == sizeof lengthbuf)
1942 return;
1943
1944 val = bfd_read (strtbl + sizeof lengthbuf, 1, length - sizeof lengthbuf,
1945 abfd);
1946
1947 if (val != length - sizeof lengthbuf)
1948 error ("cannot read string table from %s: %s",
1949 bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
1950 if (strtbl[length - 1] != '\0')
1951 error ("bad symbol file: string table does not end with null character");
1952
1953 return;
1954}
1955\f
1956/* If we have not yet seen a function for this psymtab, this is 0. If we
1957 have seen one, it is the offset in the line numbers of the line numbers
1958 for the psymtab. */
1959static unsigned int first_fun_line_offset;
1960
1961static struct partial_symtab *xcoff_start_psymtab
a14ed312
KB
1962 (struct objfile *, char *, int,
1963 struct partial_symbol **, struct partial_symbol **);
c906108c
SS
1964
1965/* Allocate and partially fill a partial symtab. It will be
1966 completely filled at the end of the symbol list.
1967
1968 SYMFILE_NAME is the name of the symbol-file we are reading from, and ADDR
1969 is the address relative to which its symbols are (incremental) or 0
1970 (normal). */
1971
1972static struct partial_symtab *
fba45db2
KB
1973xcoff_start_psymtab (struct objfile *objfile, char *filename, int first_symnum,
1974 struct partial_symbol **global_syms,
1975 struct partial_symbol **static_syms)
c906108c
SS
1976{
1977 struct partial_symtab *result =
d4f3574e 1978 start_psymtab_common (objfile, objfile->section_offsets,
c5aa993b 1979 filename,
d4f3574e 1980 /* We fill in textlow later. */
c5aa993b
JM
1981 0,
1982 global_syms, static_syms);
c906108c
SS
1983
1984 result->read_symtab_private = (char *)
c5aa993b
JM
1985 obstack_alloc (&objfile->psymbol_obstack, sizeof (struct symloc));
1986 ((struct symloc *) result->read_symtab_private)->first_symnum = first_symnum;
c906108c
SS
1987 result->read_symtab = xcoff_psymtab_to_symtab;
1988
1989 /* Deduce the source language from the filename for this psymtab. */
1990 psymtab_language = deduce_language_from_filename (filename);
1991
1992 return result;
1993}
1994
1995static struct partial_symtab *xcoff_end_psymtab
a14ed312
KB
1996 (struct partial_symtab *, char **, int, int,
1997 struct partial_symtab **, int, int);
c906108c
SS
1998
1999/* Close off the current usage of PST.
2000 Returns PST, or NULL if the partial symtab was empty and thrown away.
2001
2002 CAPPING_SYMBOL_NUMBER is the end of pst (exclusive).
2003
2004 INCLUDE_LIST, NUM_INCLUDES, DEPENDENCY_LIST, and NUMBER_DEPENDENCIES
2005 are the information for includes and dependencies. */
2006
2007static struct partial_symtab *
fba45db2
KB
2008xcoff_end_psymtab (struct partial_symtab *pst, char **include_list,
2009 int num_includes, int capping_symbol_number,
2010 struct partial_symtab **dependency_list,
2011 int number_dependencies, int textlow_not_set)
c906108c
SS
2012{
2013 int i;
c5aa993b 2014 struct objfile *objfile = pst->objfile;
c906108c
SS
2015
2016 if (capping_symbol_number != -1)
c5aa993b 2017 ((struct symloc *) pst->read_symtab_private)->numsyms =
c906108c 2018 capping_symbol_number
c5aa993b
JM
2019 - ((struct symloc *) pst->read_symtab_private)->first_symnum;
2020 ((struct symloc *) pst->read_symtab_private)->lineno_off =
c906108c
SS
2021 first_fun_line_offset;
2022 first_fun_line_offset = 0;
2023 pst->n_global_syms =
2024 objfile->global_psymbols.next - (objfile->global_psymbols.list + pst->globals_offset);
2025 pst->n_static_syms =
2026 objfile->static_psymbols.next - (objfile->static_psymbols.list + pst->statics_offset);
2027
2028 pst->number_of_dependencies = number_dependencies;
2029 if (number_dependencies)
2030 {
2031 pst->dependencies = (struct partial_symtab **)
2032 obstack_alloc (&objfile->psymbol_obstack,
c5aa993b 2033 number_dependencies * sizeof (struct partial_symtab *));
c906108c 2034 memcpy (pst->dependencies, dependency_list,
c5aa993b 2035 number_dependencies * sizeof (struct partial_symtab *));
c906108c
SS
2036 }
2037 else
2038 pst->dependencies = 0;
2039
2040 for (i = 0; i < num_includes; i++)
2041 {
2042 struct partial_symtab *subpst =
c5aa993b 2043 allocate_psymtab (include_list[i], objfile);
c906108c
SS
2044
2045 subpst->section_offsets = pst->section_offsets;
2046 subpst->read_symtab_private =
c5aa993b
JM
2047 (char *) obstack_alloc (&objfile->psymbol_obstack,
2048 sizeof (struct symloc));
2049 ((struct symloc *) subpst->read_symtab_private)->first_symnum = 0;
2050 ((struct symloc *) subpst->read_symtab_private)->numsyms = 0;
c906108c
SS
2051 subpst->textlow = 0;
2052 subpst->texthigh = 0;
2053
2054 /* We could save slight bits of space by only making one of these,
c5aa993b 2055 shared by the entire set of include files. FIXME-someday. */
c906108c
SS
2056 subpst->dependencies = (struct partial_symtab **)
2057 obstack_alloc (&objfile->psymbol_obstack,
2058 sizeof (struct partial_symtab *));
2059 subpst->dependencies[0] = pst;
2060 subpst->number_of_dependencies = 1;
2061
2062 subpst->globals_offset =
2063 subpst->n_global_syms =
c5aa993b
JM
2064 subpst->statics_offset =
2065 subpst->n_static_syms = 0;
c906108c
SS
2066
2067 subpst->readin = 0;
2068 subpst->symtab = 0;
2069 subpst->read_symtab = pst->read_symtab;
2070 }
2071
2072 sort_pst_symbols (pst);
2073
2074 /* If there is already a psymtab or symtab for a file of this name,
2075 remove it. (If there is a symtab, more drastic things also
2076 happen.) This happens in VxWorks. */
2077 free_named_symtabs (pst->filename);
2078
2079 if (num_includes == 0
2080 && number_dependencies == 0
2081 && pst->n_global_syms == 0
2082 && pst->n_static_syms == 0)
2083 {
2084 /* Throw away this psymtab, it's empty. We can't deallocate it, since
c5aa993b 2085 it is on the obstack, but we can forget to chain it on the list. */
c906108c 2086 /* Empty psymtabs happen as a result of header files which don't have
c5aa993b 2087 any symbols in them. There can be a lot of them. */
c906108c
SS
2088
2089 discard_psymtab (pst);
2090
2091 /* Indicate that psymtab was thrown away. */
c5aa993b 2092 pst = (struct partial_symtab *) NULL;
c906108c
SS
2093 }
2094 return pst;
2095}
2096
a14ed312
KB
2097static void swap_sym (struct internal_syment *,
2098 union internal_auxent *, char **, char **,
2099 unsigned int *, struct objfile *);
c906108c
SS
2100
2101/* Swap raw symbol at *RAW and put the name in *NAME, the symbol in
2102 *SYMBOL, the first auxent in *AUX. Advance *RAW and *SYMNUMP over
2103 the symbol and its auxents. */
2104
2105static void
fba45db2
KB
2106swap_sym (struct internal_syment *symbol, union internal_auxent *aux,
2107 char **name, char **raw, unsigned int *symnump,
2108 struct objfile *objfile)
c906108c
SS
2109{
2110 bfd_coff_swap_sym_in (objfile->obfd, *raw, symbol);
2111 if (symbol->n_zeroes)
2112 {
2113 /* If it's exactly E_SYMNMLEN characters long it isn't
c5aa993b 2114 '\0'-terminated. */
c906108c
SS
2115 if (symbol->n_name[E_SYMNMLEN - 1] != '\0')
2116 {
2117 /* FIXME: wastes memory for symbols which we don't end up putting
2118 into the minimal symbols. */
2119 char *p;
2120 p = obstack_alloc (&objfile->psymbol_obstack, E_SYMNMLEN + 1);
2121 strncpy (p, symbol->n_name, E_SYMNMLEN);
2122 p[E_SYMNMLEN] = '\0';
2123 *name = p;
2124 }
2125 else
2126 /* Point to the unswapped name as that persists as long as the
2127 objfile does. */
c5aa993b 2128 *name = ((struct external_syment *) *raw)->e.e_name;
c906108c
SS
2129 }
2130 else if (symbol->n_sclass & 0x80)
2131 {
c5aa993b 2132 *name = ((struct coff_symfile_info *) objfile->sym_private)->debugsec
c906108c
SS
2133 + symbol->n_offset;
2134 }
2135 else
2136 {
c5aa993b 2137 *name = ((struct coff_symfile_info *) objfile->sym_private)->strtbl
c906108c
SS
2138 + symbol->n_offset;
2139 }
2140 ++*symnump;
2141 *raw += coff_data (objfile->obfd)->local_symesz;
2142 if (symbol->n_numaux > 0)
2143 {
2144 bfd_coff_swap_aux_in (objfile->obfd, *raw, symbol->n_type,
2145 symbol->n_sclass, 0, symbol->n_numaux, aux);
2146
2147 *symnump += symbol->n_numaux;
2148 *raw += coff_data (objfile->obfd)->local_symesz * symbol->n_numaux;
2149 }
2150}
2151
2152static void
fba45db2 2153scan_xcoff_symtab (struct objfile *objfile)
c906108c 2154{
c5aa993b 2155 CORE_ADDR toc_offset = 0; /* toc offset value in data section. */
c906108c
SS
2156 char *filestring = NULL;
2157
2158 char *namestring;
2159 int past_first_source_file = 0;
2160 bfd *abfd;
2161 asection *bfd_sect;
2162 unsigned int nsyms;
2163
2164 /* Current partial symtab */
2165 struct partial_symtab *pst;
2166
2167 /* List of current psymtab's include files */
2168 char **psymtab_include_list;
2169 int includes_allocated;
2170 int includes_used;
2171
2172 /* Index within current psymtab dependency list */
2173 struct partial_symtab **dependency_list;
2174 int dependencies_used, dependencies_allocated;
2175
2176 char *sraw_symbol;
2177 struct internal_syment symbol;
96baa820 2178 union internal_auxent main_aux[5];
c906108c
SS
2179 unsigned int ssymnum;
2180
c5aa993b 2181 char *last_csect_name = NULL; /* last seen csect's name and value */
c906108c
SS
2182 CORE_ADDR last_csect_val = 0;
2183 int last_csect_sec = 0;
c5aa993b 2184 int misc_func_recorded = 0; /* true if any misc. function */
c906108c
SS
2185 int textlow_not_set = 1;
2186
2187 pst = (struct partial_symtab *) 0;
2188
2189 includes_allocated = 30;
2190 includes_used = 0;
2191 psymtab_include_list = (char **) alloca (includes_allocated *
2192 sizeof (char *));
2193
2194 dependencies_allocated = 30;
2195 dependencies_used = 0;
2196 dependency_list =
2197 (struct partial_symtab **) alloca (dependencies_allocated *
2198 sizeof (struct partial_symtab *));
2199
2200 last_source_file = NULL;
2201
2202 abfd = objfile->obfd;
2203
c5aa993b
JM
2204 sraw_symbol = ((struct coff_symfile_info *) objfile->sym_private)->symtbl;
2205 nsyms = ((struct coff_symfile_info *) objfile->sym_private)->symtbl_num_syms;
c906108c
SS
2206 ssymnum = 0;
2207 while (ssymnum < nsyms)
2208 {
7a78ae4e 2209 int sclass;
c906108c 2210 /* This is the type we pass to partial-stab.h. A less kludgy solution
c5aa993b
JM
2211 would be to break out partial-stab.h into its various parts--shuffle
2212 off the DBXREAD_ONLY stuff to dbxread.c, and make separate
2213 pstab-norm.h (for most types), pstab-sol.h (for N_SOL), etc. */
c906108c
SS
2214 int stype;
2215
2216 QUIT;
2217
7a78ae4e
ND
2218 bfd_coff_swap_sym_in (abfd, sraw_symbol, &symbol);
2219 sclass = symbol.n_sclass;
2220
c906108c
SS
2221 switch (sclass)
2222 {
2223 case C_EXT:
2224 case C_HIDEXT:
2225 {
2226 /* The CSECT auxent--always the last auxent. */
2227 union internal_auxent csect_aux;
2228 unsigned int symnum_before = ssymnum;
2229
96baa820 2230 swap_sym (&symbol, &main_aux[0], &namestring, &sraw_symbol,
c906108c
SS
2231 &ssymnum, objfile);
2232 if (symbol.n_numaux > 1)
2233 {
2234 bfd_coff_swap_aux_in
2235 (objfile->obfd,
c5aa993b 2236 sraw_symbol - coff_data (abfd)->local_symesz,
c906108c
SS
2237 symbol.n_type,
2238 symbol.n_sclass,
2239 symbol.n_numaux - 1,
2240 symbol.n_numaux,
2241 &csect_aux);
2242 }
2243 else
96baa820 2244 csect_aux = main_aux[0];
c906108c
SS
2245
2246 /* If symbol name starts with ".$" or "$", ignore it. */
2247 if (namestring[0] == '$'
2248 || (namestring[0] == '.' && namestring[1] == '$'))
2249 break;
2250
2251 switch (csect_aux.x_csect.x_smtyp & 0x7)
2252 {
2253 case XTY_SD:
2254 switch (csect_aux.x_csect.x_smclas)
2255 {
2256 case XMC_PR:
2257 if (last_csect_name)
2258 {
2259 /* If no misc. function recorded in the last
2260 seen csect, enter it as a function. This
2261 will take care of functions like strcmp()
2262 compiled by xlc. */
2263
2264 if (!misc_func_recorded)
2265 {
2266 RECORD_MINIMAL_SYMBOL
2267 (last_csect_name, last_csect_val,
2268 mst_text, last_csect_sec,
2269 objfile);
2270 }
2271
2272 if (pst != NULL)
2273 {
2274 /* We have to allocate one psymtab for
2275 each program csect, because their text
2276 sections need not be adjacent. */
2277 xcoff_end_psymtab
2278 (pst, psymtab_include_list, includes_used,
2279 symnum_before, dependency_list,
2280 dependencies_used, textlow_not_set);
2281 includes_used = 0;
2282 dependencies_used = 0;
2283 /* Give all psymtabs for this source file the same
2284 name. */
2285 pst = xcoff_start_psymtab
d4f3574e 2286 (objfile,
c906108c
SS
2287 filestring,
2288 symnum_before,
2289 objfile->global_psymbols.next,
2290 objfile->static_psymbols.next);
2291 }
2292 }
2293 if (namestring && namestring[0] == '.')
2294 {
2295 last_csect_name = namestring;
2296 last_csect_val = symbol.n_value;
2297 last_csect_sec =
2298 secnum_to_section (symbol.n_scnum, objfile);
2299 }
2300 if (pst != NULL)
2301 {
2302 CORE_ADDR highval =
c5aa993b 2303 symbol.n_value + csect_aux.x_csect.x_scnlen.l;
c906108c
SS
2304 if (highval > pst->texthigh)
2305 pst->texthigh = highval;
2306 if (pst->textlow == 0 || symbol.n_value < pst->textlow)
2307 pst->textlow = symbol.n_value;
2308 }
2309 misc_func_recorded = 0;
2310 break;
2311
2312 case XMC_RW:
2313 /* Data variables are recorded in the minimal symbol
2314 table, except for section symbols. */
2315 if (*namestring != '.')
2316 prim_record_minimal_symbol_and_info
2317 (namestring, symbol.n_value,
2318 sclass == C_HIDEXT ? mst_file_data : mst_data,
2319 NULL, secnum_to_section (symbol.n_scnum, objfile),
2320 NULL, objfile);
2321 break;
2322
2323 case XMC_TC0:
2324 if (toc_offset)
2325 warning ("More than one XMC_TC0 symbol found.");
2326 toc_offset = symbol.n_value;
2327
2328 /* Make TOC offset relative to start address of section. */
2329 bfd_sect = secnum_to_bfd_section (symbol.n_scnum, objfile);
2330 if (bfd_sect)
2331 toc_offset -= bfd_section_vma (objfile->obfd, bfd_sect);
2332 break;
2333
2334 case XMC_TC:
2335 /* These symbols tell us where the TOC entry for a
2336 variable is, not the variable itself. */
2337 break;
2338
2339 default:
2340 break;
2341 }
2342 break;
2343
2344 case XTY_LD:
2345 switch (csect_aux.x_csect.x_smclas)
2346 {
2347 case XMC_PR:
2348 /* A function entry point. */
2349
2350 if (first_fun_line_offset == 0 && symbol.n_numaux > 1)
2351 first_fun_line_offset =
96baa820 2352 main_aux[0].x_sym.x_fcnary.x_fcn.x_lnnoptr;
c906108c
SS
2353 RECORD_MINIMAL_SYMBOL
2354 (namestring, symbol.n_value,
2355 sclass == C_HIDEXT ? mst_file_text : mst_text,
2356 secnum_to_section (symbol.n_scnum, objfile),
2357 objfile);
2358 break;
2359
2360 case XMC_GL:
2361 /* shared library function trampoline code entry
2362 point. */
2363
2364 /* record trampoline code entries as
2365 mst_solib_trampoline symbol. When we lookup mst
2366 symbols, we will choose mst_text over
2367 mst_solib_trampoline. */
2368 RECORD_MINIMAL_SYMBOL
2369 (namestring, symbol.n_value,
2370 mst_solib_trampoline,
2371 secnum_to_section (symbol.n_scnum, objfile),
2372 objfile);
2373 break;
2374
2375 case XMC_DS:
2376 /* The symbols often have the same names as
2377 debug symbols for functions, and confuse
2378 lookup_symbol. */
2379 break;
2380
2381 default:
2382
2383 /* xlc puts each variable in a separate csect,
2384 so we get an XTY_SD for each variable. But
2385 gcc puts several variables in a csect, so
2386 that each variable only gets an XTY_LD. We
2387 still need to record them. This will
2388 typically be XMC_RW; I suspect XMC_RO and
2389 XMC_BS might be possible too. */
2390 if (*namestring != '.')
2391 prim_record_minimal_symbol_and_info
2392 (namestring, symbol.n_value,
2393 sclass == C_HIDEXT ? mst_file_data : mst_data,
2394 NULL, secnum_to_section (symbol.n_scnum, objfile),
2395 NULL, objfile);
2396 break;
2397 }
2398 break;
2399
2400 case XTY_CM:
2401 switch (csect_aux.x_csect.x_smclas)
2402 {
2403 case XMC_RW:
2404 case XMC_BS:
2405 /* Common variables are recorded in the minimal symbol
2406 table, except for section symbols. */
2407 if (*namestring != '.')
2408 prim_record_minimal_symbol_and_info
2409 (namestring, symbol.n_value,
2410 sclass == C_HIDEXT ? mst_file_bss : mst_bss,
2411 NULL, secnum_to_section (symbol.n_scnum, objfile),
2412 NULL, objfile);
2413 break;
2414 }
2415 break;
2416
2417 default:
2418 break;
2419 }
2420 }
2421 break;
2422 case C_FILE:
2423 {
2424 unsigned int symnum_before;
2425
2426 symnum_before = ssymnum;
96baa820 2427 swap_sym (&symbol, &main_aux[0], &namestring, &sraw_symbol,
c906108c
SS
2428 &ssymnum, objfile);
2429
2430 /* See if the last csect needs to be recorded. */
2431
2432 if (last_csect_name && !misc_func_recorded)
2433 {
2434
2435 /* If no misc. function recorded in the last seen csect, enter
2436 it as a function. This will take care of functions like
2437 strcmp() compiled by xlc. */
2438
2439 RECORD_MINIMAL_SYMBOL
2440 (last_csect_name, last_csect_val,
2441 mst_text, last_csect_sec, objfile);
2442 }
2443
2444 if (pst)
2445 {
2446 xcoff_end_psymtab (pst, psymtab_include_list, includes_used,
2447 symnum_before, dependency_list,
2448 dependencies_used, textlow_not_set);
2449 includes_used = 0;
2450 dependencies_used = 0;
2451 }
2452 first_fun_line_offset = 0;
2453
2454 /* XCOFF, according to the AIX 3.2 documentation, puts the
2455 filename in cs->c_name. But xlc 1.3.0.2 has decided to
2456 do things the standard COFF way and put it in the auxent.
2457 We use the auxent if the symbol is ".file" and an auxent
2458 exists, otherwise use the symbol itself. */
2459 if (!strcmp (namestring, ".file") && symbol.n_numaux > 0)
2460 {
96baa820 2461 filestring = coff_getfilename (&main_aux[0], objfile);
c906108c
SS
2462 }
2463 else
2464 filestring = namestring;
2465
d4f3574e 2466 pst = xcoff_start_psymtab (objfile,
c906108c
SS
2467 filestring,
2468 symnum_before,
2469 objfile->global_psymbols.next,
2470 objfile->static_psymbols.next);
2471 last_csect_name = NULL;
2472 }
2473 break;
2474
2475 default:
2476 {
2477 static struct complaint msg =
c5aa993b 2478 {"Storage class %d not recognized during scan", 0, 0};
c906108c
SS
2479 complain (&msg, sclass);
2480 }
2481 /* FALLTHROUGH */
2482
2483 /* C_FCN is .bf and .ef symbols. I think it is sufficient
2484 to handle only the C_FUN and C_EXT. */
2485 case C_FCN:
2486
2487 case C_BSTAT:
2488 case C_ESTAT:
2489 case C_ARG:
2490 case C_REGPARM:
2491 case C_REG:
2492 case C_TPDEF:
2493 case C_STRTAG:
2494 case C_UNTAG:
2495 case C_ENTAG:
2496 case C_LABEL:
2497 case C_NULL:
2498
2499 /* C_EINCL means we are switching back to the main file. But there
2500 is no reason to care; the only thing we want to know about
2501 includes is the names of all the included (.h) files. */
2502 case C_EINCL:
2503
2504 case C_BLOCK:
2505
2506 /* I don't think C_STAT is used in xcoff; C_HIDEXT appears to be
2507 used instead. */
2508 case C_STAT:
2509
2510 /* I don't think the name of the common block (as opposed to the
2511 variables within it) is something which is user visible
2512 currently. */
2513 case C_BCOMM:
2514 case C_ECOMM:
2515
2516 case C_PSYM:
2517 case C_RPSYM:
2518
2519 /* I think we can ignore C_LSYM; types on xcoff seem to use C_DECL
2520 so C_LSYM would appear to be only for locals. */
2521 case C_LSYM:
2522
2523 case C_AUTO:
2524 case C_RSYM:
2525 {
2526 /* We probably could save a few instructions by assuming that
2527 C_LSYM, C_PSYM, etc., never have auxents. */
7a78ae4e 2528 int naux1 = symbol.n_numaux + 1;
c906108c 2529 ssymnum += naux1;
7a78ae4e 2530 sraw_symbol += bfd_coff_symesz (abfd) * naux1;
c906108c
SS
2531 }
2532 break;
2533
2534 case C_BINCL:
2535 stype = N_SOL;
2536 goto pstab;
2537
2538 case C_FUN:
2539 /* The value of the C_FUN is not the address of the function (it
2540 appears to be the address before linking), but as long as it
2541 is smaller than the actual address, then find_pc_partial_function
2542 will use the minimal symbols instead. I hope. */
2543
2544 case C_GSYM:
2545 case C_ECOML:
2546 case C_DECL:
2547 case C_STSYM:
2548 stype = N_LSYM;
7a78ae4e 2549 pstab:
96baa820 2550 swap_sym (&symbol, &main_aux[0], &namestring, &sraw_symbol,
c906108c
SS
2551 &ssymnum, objfile);
2552#define CUR_SYMBOL_TYPE stype
2553#define CUR_SYMBOL_VALUE symbol.n_value
2554
2555/* START_PSYMTAB and END_PSYMTAB are never used, because they are only
2556 called from DBXREAD_ONLY or N_SO code. Likewise for the symnum
2557 variable. */
d4f3574e 2558#define START_PSYMTAB(ofile,fname,low,symoff,global_syms,static_syms) 0
c906108c
SS
2559#define END_PSYMTAB(pst,ilist,ninc,c_off,c_text,dep_list,n_deps,textlow_not_set)\
2560 do {} while (0)
2561/* We have already set the namestring. */
c5aa993b 2562#define SET_NAMESTRING() /* */
c906108c
SS
2563
2564#include "partial-stab.h"
2565 }
2566 }
2567
2568 if (pst)
2569 {
2570 xcoff_end_psymtab (pst, psymtab_include_list, includes_used,
2571 ssymnum, dependency_list,
2572 dependencies_used, textlow_not_set);
2573 }
2574
2575 /* Record the toc offset value of this symbol table into objfile structure.
2576 If no XMC_TC0 is found, toc_offset should be zero. Another place to obtain
2577 this information would be file auxiliary header. */
2578
2579 ((struct coff_symfile_info *) objfile->sym_private)->toc_offset = toc_offset;
2580}
2581
2582/* Return the toc offset value for a given objfile. */
2583
2584CORE_ADDR
fba45db2 2585get_toc_offset (struct objfile *objfile)
c906108c
SS
2586{
2587 if (objfile)
2588 return ((struct coff_symfile_info *) objfile->sym_private)->toc_offset;
2589 return 0;
2590}
2591
2592/* Scan and build partial symbols for a symbol file.
2593 We have been initialized by a call to dbx_symfile_init, which
2594 put all the relevant info into a "struct dbx_symfile_info",
2595 hung off the objfile structure.
2596
2597 SECTION_OFFSETS contains offsets relative to which the symbols in the
2598 various sections are (depending where the sections were actually loaded).
2599 MAINLINE is true if we are reading the main symbol
2600 table (as opposed to a shared lib or dynamically loaded file). */
2601
2602static void
96baa820 2603xcoff_initial_scan (objfile, mainline)
c906108c 2604 struct objfile *objfile;
c5aa993b 2605 int mainline; /* FIXME comments above */
c906108c
SS
2606{
2607 bfd *abfd;
2608 int val;
2609 struct cleanup *back_to;
c5aa993b
JM
2610 int num_symbols; /* # of symbols */
2611 file_ptr symtab_offset; /* symbol table and */
2612 file_ptr stringtab_offset; /* string table file offsets */
c906108c
SS
2613 struct coff_symfile_info *info;
2614 char *name;
2615 unsigned int size;
2616
c5aa993b 2617 info = (struct coff_symfile_info *) objfile->sym_private;
c906108c
SS
2618 symfile_bfd = abfd = objfile->obfd;
2619 name = objfile->name;
2620
2621 num_symbols = bfd_get_symcount (abfd); /* # of symbols */
2622 symtab_offset = obj_sym_filepos (abfd); /* symbol table file offset */
2623 stringtab_offset = symtab_offset +
c5aa993b 2624 num_symbols * coff_data (abfd)->local_symesz;
c906108c
SS
2625
2626 info->min_lineno_offset = 0;
2627 info->max_lineno_offset = 0;
2628 bfd_map_over_sections (abfd, find_linenos, info);
2629
2630 if (num_symbols > 0)
2631 {
2632 /* Read the string table. */
2633 init_stringtab (abfd, stringtab_offset, objfile);
2634
2635 /* Read the .debug section, if present. */
2636 {
2637 sec_ptr secp;
2638 bfd_size_type length;
2639 char *debugsec = NULL;
2640
2641 secp = bfd_get_section_by_name (abfd, ".debug");
2642 if (secp)
2643 {
2644 length = bfd_section_size (abfd, secp);
2645 if (length)
2646 {
2647 debugsec =
2648 (char *) obstack_alloc (&objfile->symbol_obstack, length);
2649
2650 if (!bfd_get_section_contents (abfd, secp, debugsec,
2651 (file_ptr) 0, length))
2652 {
2653 error ("Error reading .debug section of `%s': %s",
2654 name, bfd_errmsg (bfd_get_error ()));
2655 }
2656 }
2657 }
c5aa993b 2658 ((struct coff_symfile_info *) objfile->sym_private)->debugsec =
c906108c
SS
2659 debugsec;
2660 }
2661 }
2662
2663 /* Read the symbols. We keep them in core because we will want to
2664 access them randomly in read_symbol*. */
2665 val = bfd_seek (abfd, symtab_offset, SEEK_SET);
2666 if (val < 0)
2667 error ("Error reading symbols from %s: %s",
2668 name, bfd_errmsg (bfd_get_error ()));
2669 size = coff_data (abfd)->local_symesz * num_symbols;
c5aa993b 2670 ((struct coff_symfile_info *) objfile->sym_private)->symtbl =
c906108c 2671 obstack_alloc (&objfile->symbol_obstack, size);
c5aa993b 2672 ((struct coff_symfile_info *) objfile->sym_private)->symtbl_num_syms =
c906108c
SS
2673 num_symbols;
2674
c5aa993b 2675 val = bfd_read (((struct coff_symfile_info *) objfile->sym_private)->symtbl,
c906108c
SS
2676 size, 1, abfd);
2677 if (val != size)
2678 perror_with_name ("reading symbol table");
2679
2680 /* If we are reinitializing, or if we have never loaded syms yet, init */
2681 if (mainline
2682 || objfile->global_psymbols.size == 0
2683 || objfile->static_psymbols.size == 0)
2684 /* I'm not sure how how good num_symbols is; the rule of thumb in
2685 init_psymbol_list was developed for a.out. On the one hand,
2686 num_symbols includes auxents. On the other hand, it doesn't
2687 include N_SLINE. */
2688 init_psymbol_list (objfile, num_symbols);
2689
2690 free_pending_blocks ();
a0b3c4fd 2691 back_to = make_cleanup (really_free_pendings, 0);
c906108c
SS
2692
2693 init_minimal_symbol_collection ();
56e290f4 2694 make_cleanup_discard_minimal_symbols ();
c906108c
SS
2695
2696 /* Now that the symbol table data of the executable file are all in core,
2697 process them and define symbols accordingly. */
2698
d4f3574e 2699 scan_xcoff_symtab (objfile);
c906108c
SS
2700
2701 /* Install any minimal symbols that have been collected as the current
2702 minimal symbols for this objfile. */
2703
2704 install_minimal_symbols (objfile);
2705
2706 do_cleanups (back_to);
2707}
2708\f
d4f3574e 2709static void
fba45db2 2710xcoff_symfile_offsets (struct objfile *objfile, struct section_addr_info *addrs)
c906108c 2711{
b8fbeb18 2712 asection *sect = NULL;
c906108c
SS
2713 int i;
2714
2715 objfile->num_sections = SECT_OFF_MAX;
d4f3574e 2716 objfile->section_offsets = (struct section_offsets *)
96baa820 2717 obstack_alloc (&objfile->psymbol_obstack, SIZEOF_SECTION_OFFSETS);
c906108c 2718
b8fbeb18
EZ
2719 /* Initialize the section indexes for future use. */
2720 sect = bfd_get_section_by_name (objfile->obfd, ".text");
2721 if (sect)
2722 objfile->sect_index_text = sect->index;
2723
2724 sect = bfd_get_section_by_name (objfile->obfd, ".data");
2725 if (sect)
2726 objfile->sect_index_data = sect->index;
2727
2728 sect = bfd_get_section_by_name (objfile->obfd, ".bss");
2729 if (sect)
2730 objfile->sect_index_bss = sect->index;
2731
2732 sect = bfd_get_section_by_name (objfile->obfd, ".rodata");
2733 if (sect)
2734 objfile->sect_index_rodata = sect->index;
2735
c906108c 2736 for (i = 0; i < objfile->num_sections; ++i)
b8fbeb18
EZ
2737 {
2738 /* syms_from_objfile kindly subtracts from addr the
2739 bfd_section_vma of the .text section. This strikes me as
2740 wrong--whether the offset to be applied to symbol reading is
2741 relative to the start address of the section depends on the
2742 symbol format. In any event, this whole "addr" concept is
2743 pretty broken (it doesn't handle any section but .text
2744 sensibly), so just ignore the addr parameter and use 0.
2745 rs6000-nat.c will set the correct section offsets via
2746 objfile_relocate. */
2747 ANOFFSET (objfile->section_offsets, i) = 0;
2748 }
c906108c
SS
2749}
2750
2751/* Register our ability to parse symbols for xcoff BFD files. */
2752
2753static struct sym_fns xcoff_sym_fns =
2754{
2755
7a78ae4e 2756 /* It is possible that coff and xcoff should be merged as
c906108c
SS
2757 they do have fundamental similarities (for example, the extra storage
2758 classes used for stabs could presumably be recognized in any COFF file).
2759 However, in addition to obvious things like all the csect hair, there are
2760 some subtler differences between xcoffread.c and coffread.c, notably
2761 the fact that coffread.c has no need to read in all the symbols, but
2762 xcoffread.c reads all the symbols and does in fact randomly access them
2763 (in C_BSTAT and line number processing). */
2764
7a78ae4e 2765 bfd_target_xcoff_flavour,
c906108c 2766
c5aa993b
JM
2767 xcoff_new_init, /* sym_new_init: init anything gbl to entire symtab */
2768 xcoff_symfile_init, /* sym_init: read initial info, setup for sym_read() */
2769 xcoff_initial_scan, /* sym_read: read a symbol file into symtab */
2770 xcoff_symfile_finish, /* sym_finish: finished with file, cleanup */
2771 xcoff_symfile_offsets, /* sym_offsets: xlate offsets ext->int form */
2772 NULL /* next: pointer to next struct sym_fns */
c906108c
SS
2773};
2774
2775void
fba45db2 2776_initialize_xcoffread (void)
c906108c 2777{
c5aa993b 2778 add_symtab_fns (&xcoff_sym_fns);
c906108c
SS
2779
2780 func_symbol_type = init_type (TYPE_CODE_FUNC, 1, 0,
2781 "<function, no debug info>", NULL);
2782 TYPE_TARGET_TYPE (func_symbol_type) = builtin_type_int;
2783 var_symbol_type =
2784 init_type (TYPE_CODE_INT, TARGET_INT_BIT / HOST_CHAR_BIT, 0,
2785 "<variable, no debug info>", NULL);
2786}
This page took 0.292123 seconds and 4 git commands to generate.