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