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