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