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