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