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