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