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