Add missed log entry for som.c.
[deliverable/binutils-gdb.git] / gdb / xcoffread.c
CommitLineData
9b280a7f 1/* Read AIX xcoff symbol tables and convert to internal format, for GDB.
07aa9fdc 2 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994
9b280a7f 3 Free Software Foundation, Inc.
e38e0312
JG
4 Derived from coffread.c, dbxread.c, and a lot of hacking.
5 Contributed by IBM Corporation.
6
7This file is part of GDB.
8
9This program is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2 of the License, or
12(at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
22
9b280a7f 23/* Native only: Need struct tbtable in <sys/debug.h> from host, and
e5eeaaf8
JG
24 need xcoff_add_toc_to_loadinfo in rs6000-tdep.c from target.
25 need xcoff_init_loadinfo ditto.
26 However, if you grab <sys/debug.h> and make it available on your
27 host, and define FAKING_RS6000, then this code will compile. */
818de002 28
dd469789
JG
29#include "defs.h"
30#include "bfd.h"
31
e38e0312
JG
32#include <sys/types.h>
33#include <fcntl.h>
34#include <ctype.h>
e8abe489 35#include <string.h>
e38e0312
JG
36
37#include "obstack.h"
38#include <sys/param.h>
5e4d4b0f 39#ifndef NO_SYS_FILE
e38e0312 40#include <sys/file.h>
5e4d4b0f 41#endif
e38e0312 42#include <sys/stat.h>
818de002 43#include <sys/debug.h>
e38e0312 44
27ad511f
JK
45#include "coff/internal.h" /* FIXME, internal data from BFD */
46#include "libcoff.h" /* FIXME, internal data from BFD */
47#include "coff/rs6000.h" /* FIXME, raw file-format guts of xcoff */
48
e38e0312 49#include "symtab.h"
1ab3bf1b 50#include "gdbtypes.h"
e38e0312 51#include "symfile.h"
5e2e79f8 52#include "objfiles.h"
e38e0312 53#include "buildsym.h"
d07734e3 54#include "stabsread.h"
dd469789 55#include "complaints.h"
e38e0312 56
a81ce07d
JK
57#include "gdb-stabs.h"
58
73262420 59/* For interface with stabsread.c. */
c41e08c4 60#include "aout/stab_gnu.h"
1eeba686 61
e38e0312
JG
62/* Simplified internal version of coff symbol table information */
63
64struct coff_symbol {
65 char *c_name;
66 int c_symnum; /* symbol number of this entry */
d4a0983a 67 int c_naux; /* 0 if syment only, 1 if syment + auxent */
e38e0312 68 long c_value;
ef5b809c 69 unsigned char c_sclass;
e38e0312
JG
70 int c_secnum;
71 unsigned int c_type;
72};
73
74/* The COFF line table, in raw form. */
75static char *linetab = NULL; /* Its actual contents */
76static long linetab_offset; /* Its offset in the file */
77static unsigned long linetab_size; /* Its size */
78
79/* last function's saved coff symbol `cs' */
80
81static struct coff_symbol fcn_cs_saved;
82
83static bfd *symfile_bfd;
84
85/* Core address of start and end of text of current source file.
86 This is calculated from the first function seen after a C_FILE
87 symbol. */
88
818de002 89
e38e0312
JG
90static CORE_ADDR cur_src_end_addr;
91
92/* Core address of the end of the first object file. */
93
94static CORE_ADDR first_object_file_end;
95
96/* pointer to the string table */
97static char *strtbl;
98
99/* length of the string table */
100static int strtbl_len;
101
102/* pointer to debug section */
103static char *debugsec;
104
105/* pointer to the a.out symbol table */
106static char *symtbl;
107
c84a96d7
JK
108/* Number of symbols in symtbl. */
109static int symtbl_num_syms;
110
e38e0312
JG
111/* initial symbol-table-debug-string vector length */
112
113#define INITIAL_STABVECTOR_LENGTH 40
114
e38e0312
JG
115/* Nonzero if within a function (so symbols should be local,
116 if nothing says specifically). */
117
118int within_function;
119
120/* Local variables that hold the shift and mask values for the
121 COFF file that we are currently reading. These come back to us
122 from BFD, and are referenced by their macro names, as well as
123 internally to the BTYPE, ISPTR, ISFCN, ISARY, ISTAG, and DECREF
124 macros from ../internalcoff.h . */
125
126static unsigned local_n_btshft;
127static unsigned local_n_tmask;
128
129#undef N_BTSHFT
130#define N_BTSHFT local_n_btshft
131#undef N_TMASK
132#define N_TMASK local_n_tmask
133
134/* Local variables that hold the sizes in the file of various COFF structures.
135 (We only need to know this to read them from the file -- BFD will then
136 translate the data in them, into `internal_xxx' structs in the right
137 byte order, alignment, etc.) */
138
139static unsigned local_symesz;
140
e38e0312
JG
141struct coff_symfile_info {
142 file_ptr min_lineno_offset; /* Where in file lowest line#s are */
143 file_ptr max_lineno_offset; /* 1+last byte of line#s in file */
144};
145
dd469789
JG
146static struct complaint rsym_complaint =
147 {"Non-stab C_RSYM `%s' needs special handling", 0, 0};
148
149static struct complaint storclass_complaint =
150 {"Unexpected storage class: %d", 0, 0};
151
152static struct complaint bf_notfound_complaint =
153 {"line numbers off, `.bf' symbol not found", 0, 0};
e38e0312 154
1ab3bf1b 155static void
818de002
PB
156enter_line_range PARAMS ((struct subfile *, unsigned, unsigned,
157 CORE_ADDR, CORE_ADDR, unsigned *));
1ab3bf1b 158
1ab3bf1b
JG
159static void
160free_debugsection PARAMS ((void));
161
162static int
163init_debugsection PARAMS ((bfd *));
164
165static int
d5931d79 166init_stringtab PARAMS ((bfd *, file_ptr, struct objfile *));
1ab3bf1b
JG
167
168static void
9b280a7f 169xcoff_symfile_init PARAMS ((struct objfile *));
80d68b1d
FF
170
171static void
9b280a7f 172xcoff_new_init PARAMS ((struct objfile *));
80d68b1d
FF
173
174static void
1b880e74 175xcoff_symfile_read PARAMS ((struct objfile *, struct section_offsets *, int));
1ab3bf1b
JG
176
177static void
9b280a7f 178xcoff_symfile_finish PARAMS ((struct objfile *));
1ab3bf1b 179
fe0b60b2 180static struct section_offsets *
9b280a7f 181xcoff_symfile_offsets PARAMS ((struct objfile *, CORE_ADDR));
2670f34d 182
1ab3bf1b 183static int
d5931d79 184init_lineno PARAMS ((bfd *, file_ptr, int));
1ab3bf1b 185
0eb22669
PS
186static void
187free_linetab PARAMS ((void));
188
1ab3bf1b
JG
189static void
190find_linenos PARAMS ((bfd *, sec_ptr, PTR));
191
e8abe489
PS
192static char *
193coff_getfilename PARAMS ((union internal_auxent *));
194
3c02636b
JK
195static void
196read_symbol PARAMS ((struct internal_syment *, int));
197
1ab3bf1b 198static int
c84a96d7 199read_symbol_lineno PARAMS ((int));
1ab3bf1b
JG
200
201static int
c84a96d7 202read_symbol_nvalue PARAMS ((int));
1ab3bf1b
JG
203
204static struct symbol *
205process_xcoff_symbol PARAMS ((struct coff_symbol *, struct objfile *));
206
207static void
208read_xcoff_symtab PARAMS ((struct objfile *, int));
209
1ab3bf1b
JG
210static void
211add_stab_to_list PARAMS ((char *, struct pending_stabs **));
212
a81ce07d
JK
213\f
214#ifdef STATIC_NODEBUG_VARS
215/* Return the section_offsets* that CS points to. */
216static int cs_to_section PARAMS ((struct coff_symbol *, struct objfile *));
217
218struct find_targ_sec_arg {
219 int targ_index;
220 int *resultp;
221};
222
223static void find_targ_sec PARAMS ((bfd *, asection *, void *));
224
225static void find_targ_sec (abfd, sect, obj)
226 bfd *abfd;
227 asection *sect;
228 PTR obj;
229{
230 struct find_targ_sec_arg *args = (struct find_targ_sec_arg *)obj;
231 if (sect->target_index == args->targ_index)
232 {
233 /* This is the section. Figure out what SECT_OFF_* code it is. */
234 if (bfd_get_section_flags (abfd, sect) & SEC_CODE)
235 *args->resultp = SECT_OFF_TEXT;
236 else if (bfd_get_section_flags (abfd, sect) & SEC_LOAD)
237 *args->resultp = SECT_OFF_DATA;
238 else
239 *args->resultp = SECT_OFF_BSS;
240 }
241}
242
243/* Return the section number (SECT_OFF_*) that CS points to. */
244static int
245cs_to_section (cs, objfile)
246 struct coff_symbol *cs;
247 struct objfile *objfile;
248{
249 int off = SECT_OFF_TEXT;
250 struct find_targ_sec_arg args;
251 args.targ_index = cs->c_secnum;
252 args.resultp = &off;
253 bfd_map_over_sections (objfile->obfd, find_targ_sec, &args);
254 return off;
255}
256#endif /* STATIC_NODEBUG_VARS */
257\f
e38e0312
JG
258/* add a given stab string into given stab vector. */
259
260static void
261add_stab_to_list (stabname, stabvector)
262char *stabname;
263struct pending_stabs **stabvector;
264{
265 if ( *stabvector == NULL) {
266 *stabvector = (struct pending_stabs *)
267 xmalloc (sizeof (struct pending_stabs) +
268 INITIAL_STABVECTOR_LENGTH * sizeof (char*));
269 (*stabvector)->count = 0;
270 (*stabvector)->length = INITIAL_STABVECTOR_LENGTH;
271 }
272 else if ((*stabvector)->count >= (*stabvector)->length) {
273 (*stabvector)->length += INITIAL_STABVECTOR_LENGTH;
274 *stabvector = (struct pending_stabs *)
1ab3bf1b 275 xrealloc ((char *) *stabvector, sizeof (struct pending_stabs) +
e38e0312
JG
276 (*stabvector)->length * sizeof (char*));
277 }
278 (*stabvector)->stab [(*stabvector)->count++] = stabname;
279}
194d98c6
JK
280\f
281/* Linenos are processed on a file-by-file basis.
282
283 Two reasons:
284
285 1) xlc (IBM's native c compiler) postpones static function code
286 emission to the end of a compilation unit. This way it can
287 determine if those functions (statics) are needed or not, and
288 can do some garbage collection (I think). This makes line
289 numbers and corresponding addresses unordered, and we end up
290 with a line table like:
291
292
293 lineno addr
294 foo() 10 0x100
295 20 0x200
296 30 0x300
297
298 foo3() 70 0x400
299 80 0x500
300 90 0x600
301
302 static foo2()
303 40 0x700
304 50 0x800
305 60 0x900
306
307 and that breaks gdb's binary search on line numbers, if the
308 above table is not sorted on line numbers. And that sort
309 should be on function based, since gcc can emit line numbers
310 like:
311
312 10 0x100 - for the init/test part of a for stmt.
313 20 0x200
314 30 0x300
315 10 0x400 - for the increment part of a for stmt.
e38e0312 316
194d98c6 317 arrange_linetable() will do this sorting.
e38e0312 318
194d98c6 319 2) aix symbol table might look like:
818de002 320
194d98c6
JK
321 c_file // beginning of a new file
322 .bi // beginning of include file
323 .ei // end of include file
324 .bi
325 .ei
818de002 326
194d98c6
JK
327 basically, .bi/.ei pairs do not necessarily encapsulate
328 their scope. They need to be recorded, and processed later
329 on when we come the end of the compilation unit.
330 Include table (inclTable) and process_linenos() handle
331 that. */
818de002
PB
332
333/* compare line table entry addresses. */
334
194d98c6 335static int
818de002 336compare_lte (lte1, lte2)
194d98c6 337 struct linetable_entry *lte1, *lte2;
818de002
PB
338{
339 return lte1->pc - lte2->pc;
340}
341
342/* Give a line table with function entries are marked, arrange its functions
343 in assending order and strip off function entry markers and return it in
344 a newly created table. If the old one is good enough, return the old one. */
c438b3af
JK
345/* FIXME: I think all this stuff can be replaced by just passing
346 sort_linevec = 1 to end_symtab. */
818de002
PB
347
348static struct linetable *
349arrange_linetable (oldLineTb)
350 struct linetable *oldLineTb; /* old linetable */
351{
352 int ii, jj,
353 newline, /* new line count */
354 function_count; /* # of functions */
355
356 struct linetable_entry *fentry; /* function entry vector */
357 int fentry_size; /* # of function entries */
358 struct linetable *newLineTb; /* new line table */
359
360#define NUM_OF_FUNCTIONS 20
361
362 fentry_size = NUM_OF_FUNCTIONS;
363 fentry = (struct linetable_entry*)
ecfd2b60 364 xmalloc (fentry_size * sizeof (struct linetable_entry));
818de002
PB
365
366 for (function_count=0, ii=0; ii <oldLineTb->nitems; ++ii) {
367
368 if (oldLineTb->item[ii].line == 0) { /* function entry found. */
369
370 if (function_count >= fentry_size) { /* make sure you have room. */
371 fentry_size *= 2;
372 fentry = (struct linetable_entry*)
ecfd2b60 373 xrealloc (fentry, fentry_size * sizeof (struct linetable_entry));
818de002
PB
374 }
375 fentry[function_count].line = ii;
376 fentry[function_count].pc = oldLineTb->item[ii].pc;
377 ++function_count;
378 }
379 }
380
381 if (function_count == 0) {
382 free (fentry);
383 return oldLineTb;
384 }
385 else if (function_count > 1)
386 qsort (fentry, function_count, sizeof(struct linetable_entry), compare_lte);
387
388 /* allocate a new line table. */
ecfd2b60
JK
389 newLineTb = (struct linetable *)
390 xmalloc
391 (sizeof (struct linetable) +
392 (oldLineTb->nitems - function_count) * sizeof (struct linetable_entry));
818de002
PB
393
394 /* if line table does not start with a function beginning, copy up until
395 a function begin. */
396
397 newline = 0;
398 if (oldLineTb->item[0].line != 0)
399 for (newline=0;
400 newline < oldLineTb->nitems && oldLineTb->item[newline].line; ++newline)
401 newLineTb->item[newline] = oldLineTb->item[newline];
402
403 /* Now copy function lines one by one. */
404
405 for (ii=0; ii < function_count; ++ii) {
406 for (jj = fentry[ii].line + 1;
407 jj < oldLineTb->nitems && oldLineTb->item[jj].line != 0;
408 ++jj, ++newline)
409 newLineTb->item[newline] = oldLineTb->item[jj];
410 }
411 free (fentry);
412 newLineTb->nitems = oldLineTb->nitems - function_count;
413 return newLineTb;
414}
415
416
417
418/* We try to detect the beginning of a compilation unit. That info will
419 be used as an entry in line number recording routines (enter_line_range) */
420
421static unsigned first_fun_line_offset;
422static unsigned first_fun_bf;
423
424#define mark_first_line(OFFSET, SYMNUM) \
425 if (!first_fun_line_offset) { \
426 first_fun_line_offset = OFFSET; \
427 first_fun_bf = SYMNUM; \
428 }
429
430
431/* include file support: C_BINCL/C_EINCL pairs will be kept in the
432 following `IncludeChain'. At the end of each symtab (end_symtab),
433 we will determine if we should create additional symtab's to
434 represent if (the include files. */
435
436
437typedef struct _inclTable {
438 char *name; /* include filename */
2aefe6e4
JK
439
440 /* Offsets to the line table. end points to the last entry which is
441 part of this include file. */
442 int begin, end;
443
818de002
PB
444 struct subfile *subfile;
445 unsigned funStartLine; /* start line # of its function */
446} InclTable;
447
448#define INITIAL_INCLUDE_TABLE_LENGTH 20
449static InclTable *inclTable; /* global include table */
450static int inclIndx; /* last entry to table */
451static int inclLength; /* table length */
452static int inclDepth; /* nested include depth */
453
7f4c8595 454static void allocate_include_entry PARAMS ((void));
818de002
PB
455
456static void
457record_include_begin (cs)
458struct coff_symbol *cs;
459{
818de002 460 if (inclDepth)
486b440e
JK
461 {
462 /* In xcoff, we assume include files cannot be nested (not in .c files
463 of course, but in corresponding .s files.). */
464
67240bb8
JK
465 /* This can happen with old versions of GCC.
466 GCC 2.3.3-930426 does not exhibit this on a test case which
467 a user said produced the message for him. */
b646b438 468 static struct complaint msg = {"Nested C_BINCL symbols", 0, 0};
486b440e
JK
469 complain (&msg);
470 }
818de002
PB
471 ++inclDepth;
472
7f4c8595 473 allocate_include_entry ();
818de002
PB
474
475 inclTable [inclIndx].name = cs->c_name;
476 inclTable [inclIndx].begin = cs->c_value;
477}
478
818de002
PB
479static void
480record_include_end (cs)
481struct coff_symbol *cs;
482{
483 InclTable *pTbl;
484
485 if (inclDepth == 0)
486b440e 486 {
b646b438 487 static struct complaint msg = {"Mismatched C_BINCL/C_EINCL pair", 0, 0};
486b440e
JK
488 complain (&msg);
489 }
818de002 490
7f4c8595
SS
491 allocate_include_entry ();
492
818de002
PB
493 pTbl = &inclTable [inclIndx];
494 pTbl->end = cs->c_value;
495
496 --inclDepth;
497 ++inclIndx;
498}
499
7f4c8595
SS
500static void
501allocate_include_entry ()
502{
503 if (inclTable == NULL)
504 {
505 inclTable = (InclTable *)
506 xmalloc (sizeof (InclTable) * INITIAL_INCLUDE_TABLE_LENGTH);
507 memset (inclTable,
508 '\0', sizeof (InclTable) * INITIAL_INCLUDE_TABLE_LENGTH);
509 inclLength = INITIAL_INCLUDE_TABLE_LENGTH;
510 inclIndx = 0;
511 }
512 else if (inclIndx >= inclLength)
513 {
514 inclLength += INITIAL_INCLUDE_TABLE_LENGTH;
515 inclTable = (InclTable *)
516 xrealloc (inclTable, sizeof (InclTable) * inclLength);
517 memset (inclTable + inclLength - INITIAL_INCLUDE_TABLE_LENGTH,
518 '\0', sizeof (InclTable)*INITIAL_INCLUDE_TABLE_LENGTH);
519 }
520}
818de002 521
7f4c8595
SS
522/* given the start and end addresses of a compilation unit (or a csect,
523 at times) process its lines and create appropriate line vectors. */
818de002
PB
524
525static void
526process_linenos (start, end)
527 CORE_ADDR start, end;
528{
529 char *pp;
530 int offset, ii;
531
532 struct subfile main_subfile; /* subfile structure for the main
533 compilation unit. */
534
535 /* in the main source file, any time we see a function entry, we reset
536 this variable to function's absolute starting line number. All the
537 following line numbers in the function are relative to this, and
538 we record absolute line numbers in record_line(). */
539
540 int main_source_baseline = 0;
541
542
543 unsigned *firstLine;
544 CORE_ADDR addr;
545
546 if (!(offset = first_fun_line_offset))
547 goto return_after_cleanup;
548
4ed97c9a 549 memset (&main_subfile, '\0', sizeof (main_subfile));
818de002
PB
550 first_fun_line_offset = 0;
551
552 if (inclIndx == 0)
556f3d90
PB
553 /* All source lines were in the main source file. None in include files. */
554
818de002
PB
555 enter_line_range (&main_subfile, offset, 0, start, end,
556 &main_source_baseline);
557
558 /* else, there was source with line numbers in include files */
559 else {
560
561 main_source_baseline = 0;
562 for (ii=0; ii < inclIndx; ++ii) {
563
564 struct subfile *tmpSubfile;
565
566 /* if there is main file source before include file, enter it. */
567 if (offset < inclTable[ii].begin) {
568 enter_line_range
569 (&main_subfile, offset, inclTable[ii].begin - LINESZ, start, 0,
570 &main_source_baseline);
571 }
572
573 /* Have a new subfile for the include file */
574
575 tmpSubfile = inclTable[ii].subfile = (struct subfile*)
576 xmalloc (sizeof (struct subfile));
577
4ed97c9a 578 memset (tmpSubfile, '\0', sizeof (struct subfile));
818de002
PB
579 firstLine = &(inclTable[ii].funStartLine);
580
581 /* enter include file's lines now. */
582 enter_line_range (tmpSubfile, inclTable[ii].begin,
583 inclTable[ii].end, start, 0, firstLine);
584
585 offset = inclTable[ii].end + LINESZ;
586 }
587
588 /* all the include files' line have been processed at this point. Now,
589 enter remaining lines of the main file, if any left. */
590 if (offset < (linetab_offset + linetab_size + 1 - LINESZ)) {
591 enter_line_range (&main_subfile, offset, 0, start, end,
592 &main_source_baseline);
593 }
594 }
595
596 /* Process main file's line numbers. */
597 if (main_subfile.line_vector) {
598 struct linetable *lineTb, *lv;
599
600 lv = main_subfile.line_vector;
601
602 /* Line numbers are not necessarily ordered. xlc compilation will
603 put static function to the end. */
604
605 lineTb = arrange_linetable (lv);
606 if (lv == lineTb) {
607 current_subfile->line_vector = (struct linetable *)
608 xrealloc (lv, (sizeof (struct linetable)
609 + lv->nitems * sizeof (struct linetable_entry)));
610
611 }
612 else {
613 free (lv);
614 current_subfile->line_vector = lineTb;
615 }
616
617 current_subfile->line_vector_length =
618 current_subfile->line_vector->nitems;
556f3d90 619 }
818de002
PB
620
621 /* Now, process included files' line numbers. */
622
623 for (ii=0; ii < inclIndx; ++ii) {
624
625 if ( (inclTable[ii].subfile)->line_vector) { /* Useless if!!! FIXMEmgo */
626 struct linetable *lineTb, *lv;
627
628 lv = (inclTable[ii].subfile)->line_vector;
629
630 /* Line numbers are not necessarily ordered. xlc compilation will
631 put static function to the end. */
632
633 lineTb = arrange_linetable (lv);
634
635 push_subfile ();
636
637 /* For the same include file, we might want to have more than one subfile.
638 This happens if we have something like:
639
640 ......
641 #include "foo.h"
642 ......
643 #include "foo.h"
644 ......
645
646 while foo.h including code in it. (stupid but possible)
647 Since start_subfile() looks at the name and uses an existing one if finds,
648 we need to provide a fake name and fool it. */
649
650/* start_subfile (inclTable[ii].name, (char*)0); */
651 start_subfile (" ?", (char*)0);
6d4ea3a5
RP
652 free (current_subfile->name);
653 current_subfile->name = strdup (inclTable[ii].name);
818de002
PB
654
655 if (lv == lineTb) {
656 current_subfile->line_vector = (struct linetable *)
657 xrealloc (lv, (sizeof (struct linetable)
658 + lv->nitems * sizeof (struct linetable_entry)));
659
660 }
661 else {
662 free (lv);
663 current_subfile->line_vector = lineTb;
664 }
665
666 current_subfile->line_vector_length =
667 current_subfile->line_vector->nitems;
668 start_subfile (pop_subfile (), (char*)0);
669 }
670 }
818de002
PB
671
672return_after_cleanup:
673
674 /* We don't want to keep alloc/free'ing the global include file table. */
675 inclIndx = 0;
676
677 /* start with a fresh subfile structure for the next file. */
4ed97c9a 678 memset (&main_subfile, '\0', sizeof (struct subfile));
818de002
PB
679}
680
681void
682aix_process_linenos ()
683{
684 /* process line numbers and enter them into line vector */
685 process_linenos (last_source_start_addr, cur_src_end_addr);
686}
687
688
e38e0312
JG
689/* Enter a given range of lines into the line vector.
690 can be called in the following two ways:
818de002 691 enter_line_range (subfile, beginoffset, endoffset, startaddr, 0, firstLine) or
2aefe6e4
JK
692 enter_line_range (subfile, beginoffset, 0, startaddr, endaddr, firstLine)
693
694 endoffset points to the last line table entry that we should pay
695 attention to. */
e38e0312
JG
696
697static void
818de002
PB
698enter_line_range (subfile, beginoffset, endoffset, startaddr, endaddr, firstLine)
699 struct subfile *subfile;
e38e0312 700 unsigned beginoffset, endoffset; /* offsets to line table */
818de002 701 CORE_ADDR startaddr, endaddr;
e38e0312
JG
702 unsigned *firstLine;
703{
704 char *pp, *limit;
705 CORE_ADDR addr;
818de002
PB
706
707/* Do Byte swapping, if needed. FIXME! */
708#define P_LINENO(PP) (*(unsigned short*)((struct external_lineno*)(PP))->l_lnno)
709#define P_LINEADDR(PP) (*(long*)((struct external_lineno*)(PP))->l_addr.l_paddr)
710#define P_LINESYM(PP) (*(long*)((struct external_lineno*)(PP))->l_addr.l_symndx)
e38e0312
JG
711
712 pp = &linetab [beginoffset - linetab_offset];
2aefe6e4
JK
713 if (endoffset != 0 && endoffset - linetab_offset >= linetab_size)
714 {
715 static struct complaint msg =
716 {"Bad line table offset in C_EINCL directive", 0, 0};
717 complain (&msg);
718 return;
719 }
e38e0312
JG
720 limit = endoffset ? &linetab [endoffset - linetab_offset]
721 : &linetab [linetab_size -1];
722
723 while (pp <= limit) {
724
e38e0312 725 /* find the address this line represents */
818de002 726 addr = P_LINENO(pp) ?
c84a96d7 727 P_LINEADDR(pp) : read_symbol_nvalue (P_LINESYM(pp));
e38e0312 728
b60b2e3e 729 if (addr < startaddr || (endaddr && addr >= endaddr))
e38e0312
JG
730 return;
731
818de002 732 if (P_LINENO(pp) == 0) {
c84a96d7 733 *firstLine = read_symbol_lineno (P_LINESYM(pp));
818de002 734 record_line (subfile, 0, addr);
e38e0312
JG
735 --(*firstLine);
736 }
737 else
818de002
PB
738 record_line (subfile, *firstLine + P_LINENO(pp), addr);
739
740 pp += LINESZ;
741 }
742}
743
744typedef struct {
745 int fsize; /* file size */
746 int fixedparms; /* number of fixed parms */
747 int floatparms; /* number of float parms */
748 unsigned int parminfo; /* parameter info.
749 See /usr/include/sys/debug.h
750 tbtable_ext.parminfo */
751 int framesize; /* function frame size */
752} TracebackInfo;
753
754
755/* Given a function symbol, return its traceback information. */
756
757 TracebackInfo *
758retrieve_tracebackinfo (abfd, textsec, cs)
759 bfd *abfd;
760 sec_ptr textsec;
761 struct coff_symbol *cs;
762{
763#define TBTABLE_BUFSIZ 2000
818de002
PB
764
765 static TracebackInfo tbInfo;
766 struct tbtable *ptb;
767
768 static char buffer [TBTABLE_BUFSIZ];
769
770 int *pinsn;
771 int bytesread=0; /* total # of bytes read so far */
772 int bufferbytes; /* number of bytes in the buffer */
773
774 int functionstart = cs->c_value - textsec->vma;
775
4ed97c9a 776 memset (&tbInfo, '\0', sizeof (tbInfo));
818de002
PB
777
778 /* keep reading blocks of data from the text section, until finding a zero
779 word and a traceback table. */
780
2aefe6e4
JK
781 /* Note: The logical thing way to write this code would be to assign
782 to bufferbytes within the while condition. But that triggers a
783 compiler (xlc in AIX 3.2) bug, so simplify it... */
784 bufferbytes =
785 (TBTABLE_BUFSIZ < (textsec->_raw_size - functionstart - bytesread) ?
786 TBTABLE_BUFSIZ : (textsec->_raw_size - functionstart - bytesread));
787 while (bufferbytes
788 && (bfd_get_section_contents
789 (abfd, textsec, buffer,
790 (file_ptr)(functionstart + bytesread), bufferbytes)))
818de002
PB
791 {
792 bytesread += bufferbytes;
793 pinsn = (int*) buffer;
794
795 /* if this is the first time we filled the buffer, retrieve function
796 framesize info. */
797
798 if (bytesread == bufferbytes) {
799
800 /* skip over unrelated instructions */
801
802 if (*pinsn == 0x7c0802a6) /* mflr r0 */
803 ++pinsn;
804 if ((*pinsn & 0xfc00003e) == 0x7c000026) /* mfcr Rx */
805 ++pinsn;
806 if ((*pinsn & 0xfc000000) == 0x48000000) /* bl foo, save fprs */
807 ++pinsn;
808 if ((*pinsn & 0xfc1f0000) == 0xbc010000) /* stm Rx, NUM(r1) */
809 ++pinsn;
810
811 do {
812 int tmp = (*pinsn >> 16) & 0xffff;
813
814 if (tmp == 0x9421) { /* stu r1, NUM(r1) */
815 tbInfo.framesize = 0x10000 - (*pinsn & 0xffff);
816 break;
817 }
818 else if ((*pinsn == 0x93e1fffc) || /* st r31,-4(r1) */
819 (tmp == 0x9001)) /* st r0, NUM(r1) */
820 ;
821 /* else, could not find a frame size. */
822 else
823 return NULL;
824
825 } while (++pinsn && *pinsn);
826
827 if (!tbInfo.framesize)
828 return NULL;
2aefe6e4 829
818de002
PB
830 }
831
832 /* look for a zero word. */
833
834 while (*pinsn && (pinsn < (int*)(buffer + bufferbytes - sizeof(int))))
835 ++pinsn;
836
837 if (pinsn >= (int*)(buffer + bufferbytes))
838 continue;
e38e0312 839
818de002
PB
840 if (*pinsn == 0) {
841
842 /* function size is the amount of bytes we have skipped so far. */
843 tbInfo.fsize = bytesread - (buffer + bufferbytes - (char*)pinsn);
844
845 ++pinsn;
846
847 /* if we don't have the whole traceback table in the buffer, re-read
848 the whole thing. */
849
3e57da38
JK
850 /* This is how much to read to get the traceback table.
851 8 bytes of the traceback table are always present, plus we
852 look at parminfo. */
853#define MIN_TBTABSIZ 12
854
818de002
PB
855 if ((char*)pinsn > (buffer + bufferbytes - MIN_TBTABSIZ)) {
856
857 /* In case if we are *very* close to the end of the text section
858 and cannot read properly from that point on, abort by returning
859 NULL.
3e57da38
JK
860
861 This could happen if the traceback table is only 8 bytes,
862 but we try to read 12 bytes of it.
818de002
PB
863 Handle this case more graciously -- FIXME */
864
865 if (!bfd_get_section_contents (
866 abfd, textsec, buffer,
867 (file_ptr)(functionstart +
868 bytesread - (buffer + bufferbytes - (char*)pinsn)),MIN_TBTABSIZ))
199b2450 869 { printf_unfiltered ("Abnormal return!..\n"); return NULL; }
818de002
PB
870
871 ptb = (struct tbtable *)buffer;
872 }
873 else
874 ptb = (struct tbtable *)pinsn;
875
876 tbInfo.fixedparms = ptb->tb.fixedparms;
877 tbInfo.floatparms = ptb->tb.floatparms;
878 tbInfo.parminfo = ptb->tb_ext.parminfo;
879 return &tbInfo;
880 }
2aefe6e4
JK
881 bufferbytes =
882 (TBTABLE_BUFSIZ < (textsec->_raw_size - functionstart - bytesread) ?
883 TBTABLE_BUFSIZ : (textsec->_raw_size - functionstart - bytesread));
818de002
PB
884 }
885 return NULL;
886}
887
888#if 0
889/* Given a function symbol, return a pointer to its traceback table. */
890
891 struct tbtable *
892retrieve_traceback (abfd, textsec, cs, size)
893 bfd *abfd;
894 sec_ptr textsec;
895 struct coff_symbol *cs;
896 int *size; /* return function size */
897{
898#define TBTABLE_BUFSIZ 2000
899#define MIN_TBTABSIZ 50 /* minimum buffer size to hold a
900 traceback table. */
901
902 static char buffer [TBTABLE_BUFSIZ];
903
904 int *pinsn;
905 int bytesread=0; /* total # of bytes read so far */
906 int bufferbytes; /* number of bytes in the buffer */
907
908 int functionstart = cs->c_value - textsec->filepos + textsec->vma;
909 *size = 0;
910
911 /* keep reading blocks of data from the text section, until finding a zero
912 word and a traceback table. */
913
914 while (bfd_get_section_contents (abfd, textsec, buffer,
915 (file_ptr)(functionstart + bytesread),
916 bufferbytes = (
917 (TBTABLE_BUFSIZ < (textsec->size - functionstart - bytesread)) ?
918 TBTABLE_BUFSIZ : (textsec->size - functionstart - bytesread))))
919 {
920 bytesread += bufferbytes;
921 pinsn = (int*) buffer;
922
923 /* look for a zero word. */
924
925 while (*pinsn && (pinsn < (int*)(buffer + bufferbytes - sizeof(int))))
926 ++pinsn;
927
928 if (pinsn >= (int*)(buffer + bufferbytes))
929 continue;
930
931 if (*pinsn == 0) {
932
933 /* function size is the amount of bytes we have skipped so far. */
934 *size = bytesread - (buffer + bufferbytes - pinsn);
935
936 ++pinsn;
937
938 /* if we don't have the whole traceback table in the buffer, re-read
939 the whole thing. */
940
941 if ((char*)pinsn > (buffer + bufferbytes - MIN_TBTABSIZ)) {
942
943 /* In case if we are *very* close to the end of the text section
944 and cannot read properly from that point on, abort for now.
945 Handle this case more graciously -- FIXME */
946
947 if (!bfd_get_section_contents (
948 abfd, textsec, buffer,
949 (file_ptr)(functionstart +
950 bytesread - (buffer + bufferbytes - pinsn)),MIN_TBTABSIZ))
199b2450 951 /* abort (); */ { printf_unfiltered ("abort!!!\n"); return NULL; }
818de002
PB
952
953 return (struct tbtable *)buffer;
954 }
955 else
956 return (struct tbtable *)pinsn;
957 }
e38e0312 958 }
818de002 959 return NULL;
e38e0312 960}
818de002
PB
961#endif /* 0 */
962
963
e38e0312
JG
964
965
966/* Save the vital information for use when closing off the current file.
967 NAME is the file name the symbols came from, START_ADDR is the first
968 text address for the file, and SIZE is the number of bytes of text. */
969
970#define complete_symtab(name, start_addr) { \
971 last_source_file = savestring (name, strlen (name)); \
818de002 972 last_source_start_addr = start_addr; \
e38e0312
JG
973}
974
975
976/* Refill the symbol table input buffer
977 and set the variables that control fetching entries from it.
978 Reports an error if no data available.
979 This function can read past the end of the symbol table
980 (into the string table) but this does no harm. */
981
982/* Reading symbol table has to be fast! Keep the followings as macros, rather
983 than functions. */
984
8d60affd 985#define RECORD_MINIMAL_SYMBOL(NAME, ADDR, TYPE, ALLOCED, SECTION, OBJFILE) \
e38e0312
JG
986{ \
987 char *namestr; \
a81ce07d
JK
988 namestr = (NAME); \
989 if (namestr[0] == '.') ++namestr; \
990 if (!(ALLOCED)) { \
1eeba686 991 (NAME) = namestr = \
a81ce07d 992 obstack_copy0 (&objfile->symbol_obstack, namestr, strlen (namestr)); \
e38e0312
JG
993 (ALLOCED) = 1; \
994 } \
3c02636b 995 prim_record_minimal_symbol_and_info (namestr, (ADDR), (TYPE), \
8d60affd 996 (char *)NULL, (SECTION), (OBJFILE)); \
818de002 997 misc_func_recorded = 1; \
e38e0312
JG
998}
999
1000
e5eeaaf8
JG
1001/* A parameter template, used by ADD_PARM_TO_PENDING. It is initialized
1002 in our initializer function at the bottom of the file, to avoid
1003 dependencies on the exact "struct symbol" format. */
818de002 1004
e5eeaaf8 1005static struct symbol parmsym;
818de002
PB
1006
1007/* Add a parameter to a given pending symbol list. */
1008
1009#define ADD_PARM_TO_PENDING(PARM, VALUE, PTYPE, PENDING_SYMBOLS) \
1010{ \
1011 PARM = (struct symbol *) \
1012 obstack_alloc (&objfile->symbol_obstack, sizeof (struct symbol)); \
1013 *(PARM) = parmsym; \
1014 SYMBOL_TYPE (PARM) = PTYPE; \
1015 SYMBOL_VALUE (PARM) = VALUE; \
1016 add_symbol_to_list (PARM, &PENDING_SYMBOLS); \
1017}
1018
e38e0312 1019
9b280a7f 1020/* xcoff has static blocks marked in `.bs', `.es' pairs. They cannot be
e38e0312
JG
1021 nested. At any given time, a symbol can only be in one static block.
1022 This is the base address of current static block, zero if non exists. */
1023
1024static int static_block_base = 0;
1025
3c02636b
JK
1026/* Section number for the current static block. */
1027
1028static int static_block_section = -1;
1029
e38e0312
JG
1030/* true if space for symbol name has been allocated. */
1031
1032static int symname_alloced = 0;
1033
34d265dc
JK
1034/* Next symbol to read. Pointer into raw seething symbol table. */
1035
1036static char *raw_symbol;
1037
1038/* This is the function which stabsread.c calls to get symbol
1039 continuations. */
1040static char *
1041xcoff_next_symbol_text ()
1042{
1043 struct internal_syment symbol;
1044 static struct complaint msg =
1045 {"Unexpected symbol continuation", 0, 0};
963dbabe 1046 char *retval;
34d265dc
JK
1047
1048 bfd_coff_swap_sym_in (current_objfile->obfd, raw_symbol, &symbol);
afbdbbd7 1049 if (symbol.n_zeroes)
963dbabe
JK
1050 {
1051 complain (&msg);
1052
1053 /* Return something which points to '\0' and hope the symbol reading
1054 code does something reasonable. */
1055 retval = "";
1056 }
afbdbbd7 1057 else if (symbol.n_sclass & 0x80)
963dbabe
JK
1058 {
1059 retval = debugsec + symbol.n_offset;
1060 raw_symbol += coff_data (current_objfile->obfd)->local_symesz;
1061 ++symnum;
1062 }
34d265dc 1063 else
963dbabe
JK
1064 {
1065 complain (&msg);
1066
1067 /* Return something which points to '\0' and hope the symbol reading
1068 code does something reasonable. */
1069 retval = "";
1070 }
1071 return retval;
34d265dc
JK
1072}
1073
e38e0312
JG
1074/* read the whole symbol table of a given bfd. */
1075
1ab3bf1b 1076static void
e38e0312
JG
1077read_xcoff_symtab (objfile, nsyms)
1078 struct objfile *objfile; /* Object file we're reading from */
1079 int nsyms; /* # of symbols */
1080{
1081 bfd *abfd = objfile->obfd;
e38e0312 1082 char *raw_auxptr; /* Pointer to first raw aux entry for sym */
818de002
PB
1083 sec_ptr textsec; /* Pointer to text section */
1084 TracebackInfo *ptb; /* Pointer to traceback table */
1085
e38e0312 1086 struct internal_syment symbol[1];
6657a0c7 1087 union internal_auxent main_aux;
e38e0312
JG
1088 struct coff_symbol cs[1];
1089 CORE_ADDR file_start_addr = 0;
1090 CORE_ADDR file_end_addr = 0;
1091
1092 int next_file_symnum = -1;
1093 int just_started = 1;
1094 int depth = 0;
556f3d90 1095 int toc_offset = 0; /* toc offset value in data section. */
e38e0312 1096 int val;
e38e0312
JG
1097 int fcn_last_line;
1098 int fcn_start_addr;
1099 long fcn_line_offset;
1100 size_t size;
1101
1eeba686
PB
1102 struct coff_symbol fcn_stab_saved;
1103
e38e0312
JG
1104 /* fcn_cs_saved is global because process_xcoff_symbol needs it. */
1105 union internal_auxent fcn_aux_saved;
818de002 1106 struct type *fcn_type_saved = NULL;
e38e0312
JG
1107 struct context_stack *new;
1108
1109 char *filestring = " _start_ "; /* Name of the current file. */
818de002
PB
1110
1111 char *last_csect_name; /* last seen csect's name and value */
1112 CORE_ADDR last_csect_val;
3c02636b 1113 int last_csect_sec;
818de002 1114 int misc_func_recorded; /* true if any misc. function */
e38e0312 1115
1ab3bf1b
JG
1116 current_objfile = objfile;
1117
a81ce07d
JK
1118 /* Get the appropriate COFF "constants" related to the file we're
1119 handling. */
e38e0312
JG
1120 N_TMASK = coff_data (abfd)->local_n_tmask;
1121 N_BTSHFT = coff_data (abfd)->local_n_btshft;
1122 local_symesz = coff_data (abfd)->local_symesz;
1123
d07734e3 1124 last_source_file = NULL;
818de002
PB
1125 last_csect_name = 0;
1126 last_csect_val = 0;
1127 misc_func_recorded = 0;
e38e0312 1128
d07734e3 1129 start_stabs ();
e38e0312
JG
1130 start_symtab (filestring, (char *)NULL, file_start_addr);
1131 symnum = 0;
1132 first_object_file_end = 0;
1133
1134 /* Allocate space for the entire symbol table at once, and read it
1135 all in. The bfd is already positioned at the beginning of
1136 the symbol table. */
1137
1138 size = coff_data (abfd)->local_symesz * nsyms;
1139 symtbl = xmalloc (size);
c84a96d7 1140 symtbl_num_syms = nsyms;
e38e0312
JG
1141
1142 val = bfd_read (symtbl, size, 1, abfd);
1143 if (val != size)
1144 perror_with_name ("reading symbol table");
1145
1146 raw_symbol = symtbl;
1147
818de002 1148 textsec = bfd_get_section_by_name (abfd, ".text");
a81ce07d
JK
1149 if (!textsec)
1150 {
1151 printf_unfiltered ("Unable to locate text section!\n");
1152 }
818de002 1153
34d265dc
JK
1154 next_symbol_text_func = xcoff_next_symbol_text;
1155
a81ce07d
JK
1156 while (symnum < nsyms)
1157 {
e38e0312 1158
a81ce07d 1159 QUIT; /* make this command interruptable. */
e38e0312 1160
a81ce07d
JK
1161 /* READ_ONE_SYMBOL (symbol, cs, symname_alloced); */
1162 /* read one symbol into `cs' structure. After processing the
1163 whole symbol table, only string table will be kept in memory,
1164 symbol table and debug section of xcoff will be freed. Thus
1165 we can mark symbols with names in string table as
1166 `alloced'. */
1167 {
1168 int ii;
1169
1170 /* Swap and align the symbol into a reasonable C structure. */
1171 bfd_coff_swap_sym_in (abfd, raw_symbol, symbol);
1172
1173 cs->c_symnum = symnum;
1174 cs->c_naux = symbol->n_numaux;
1175 if (symbol->n_zeroes)
786757a9 1176 {
a81ce07d
JK
1177 symname_alloced = 0;
1178 /* We must use the original, unswapped, name here so the name field
1179 pointed to by cs->c_name will persist throughout xcoffread. If
1180 we use the new field, it gets overwritten for each symbol. */
1181 cs->c_name = ((struct external_syment *)raw_symbol)->e.e_name;
1182 /* If it's exactly E_SYMNMLEN characters long it isn't
1183 '\0'-terminated. */
1184 if (cs->c_name[E_SYMNMLEN - 1] != '\0')
1185 {
1186 char *p;
1187 p = obstack_alloc (&objfile->symbol_obstack, E_SYMNMLEN + 1);
1188 strncpy (p, cs->c_name, E_SYMNMLEN);
1189 p[E_SYMNMLEN] = '\0';
1190 cs->c_name = p;
1191 symname_alloced = 1;
1192 }
1193 }
1194 else if (symbol->n_sclass & 0x80)
1195 {
1196 cs->c_name = debugsec + symbol->n_offset;
1197 symname_alloced = 0;
1198 }
1199 else
1200 {
1201 /* in string table */
1202 cs->c_name = strtbl + (int)symbol->n_offset;
786757a9
JK
1203 symname_alloced = 1;
1204 }
a81ce07d
JK
1205 cs->c_value = symbol->n_value;
1206 cs->c_sclass = symbol->n_sclass;
1207 cs->c_secnum = symbol->n_scnum;
1208 cs->c_type = (unsigned)symbol->n_type;
e38e0312 1209
a81ce07d
JK
1210 raw_symbol += coff_data (abfd)->local_symesz;
1211 ++symnum;
e38e0312 1212
a81ce07d
JK
1213 /* Save addr of first aux entry. */
1214 raw_auxptr = raw_symbol;
e38e0312 1215
a81ce07d
JK
1216 /* Skip all the auxents associated with this symbol. */
1217 for (ii = symbol->n_numaux; ii; --ii)
1218 {
1219 raw_symbol += coff_data (abfd)->local_auxesz;
1220 ++symnum;
1221 }
e38e0312 1222 }
e38e0312 1223
a81ce07d
JK
1224 /* if symbol name starts with ".$" or "$", ignore it. */
1225 if (cs->c_name[0] == '$'
1226 || (cs->c_name[1] == '$' && cs->c_name[0] == '.'))
1227 continue;
e38e0312 1228
a81ce07d 1229 if (cs->c_symnum == next_file_symnum && cs->c_sclass != C_FILE)
d07734e3 1230 {
a81ce07d
JK
1231 if (last_source_file)
1232 {
1233 end_symtab (cur_src_end_addr, 1, 0, objfile,
1234 textsec->target_index);
1235 end_stabs ();
1236 }
e38e0312 1237
a81ce07d
JK
1238 start_stabs ();
1239 start_symtab ("_globals_", (char *)NULL, (CORE_ADDR)0);
1240 cur_src_end_addr = first_object_file_end;
1241 /* done with all files, everything from here on is globals */
1242 }
e38e0312 1243
a81ce07d
JK
1244 /* if explicitly specified as a function, treat is as one. */
1245 if (ISFCN(cs->c_type) && cs->c_sclass != C_TPDEF)
1246 {
1247 bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
1248 0, cs->c_naux, &main_aux);
1249 goto function_entry_point;
1250 }
e38e0312 1251
a81ce07d
JK
1252 if ((cs->c_sclass == C_EXT || cs->c_sclass == C_HIDEXT)
1253 && cs->c_naux == 1)
1254 {
1255 /* Dealing with a symbol with a csect entry. */
e38e0312 1256
a81ce07d
JK
1257#define CSECT(PP) ((PP)->x_csect)
1258#define CSECT_LEN(PP) (CSECT(PP).x_scnlen.l)
1259#define CSECT_ALIGN(PP) (SMTYP_ALIGN(CSECT(PP).x_smtyp))
1260#define CSECT_SMTYP(PP) (SMTYP_SMTYP(CSECT(PP).x_smtyp))
1261#define CSECT_SCLAS(PP) (CSECT(PP).x_smclas)
e38e0312 1262
a81ce07d
JK
1263 /* Convert the auxent to something we can access. */
1264 bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
1265 0, cs->c_naux, &main_aux);
e38e0312 1266
a81ce07d
JK
1267 switch (CSECT_SMTYP (&main_aux))
1268 {
e38e0312 1269
a81ce07d
JK
1270 case XTY_ER:
1271 /* Ignore all external references. */
1272 continue;
e38e0312 1273
a81ce07d
JK
1274 case XTY_SD:
1275 /* A section description. */
e38e0312 1276 {
a81ce07d
JK
1277 switch (CSECT_SCLAS (&main_aux))
1278 {
1279
1280 case XMC_PR:
1281 {
1282
1283 /* A program csect is seen. We have to allocate one
1284 symbol table for each program csect. Normally gdb
1285 prefers one symtab for each source file. In case
1286 of AIX, one source file might include more than one
1287 [PR] csect, and they don't have to be adjacent in
1288 terms of the space they occupy in memory. Thus, one
1289 single source file might get fragmented in the
1290 memory and gdb's file start and end address
1291 approach does not work! GCC (and I think xlc) seem
1292 to put all the code in the unnamed program csect. */
1293
1294 if (last_csect_name)
1295 {
1296
1297 /* If no misc. function recorded in the last
1298 seen csect, enter it as a function. This
1299 will take care of functions like strcmp()
1300 compiled by xlc. */
1301
1302 if (!misc_func_recorded)
1303 {
1304 int alloced = 0;
1305 RECORD_MINIMAL_SYMBOL
1306 (last_csect_name, last_csect_val,
1307 mst_text, alloced, last_csect_sec,
1308 objfile);
1309 }
1310
1311 complete_symtab (filestring, file_start_addr);
1312 cur_src_end_addr = file_end_addr;
1313 end_symtab (file_end_addr, 1, 0, objfile,
1314 textsec->target_index);
1315 end_stabs ();
1316 start_stabs ();
1317 /* Give all csects for this source file the same
1318 name. */
1319 start_symtab (filestring, NULL, (CORE_ADDR)0);
1320 }
1321
1322 /* If this is the very first csect seen,
1323 basically `__start'. */
1324 if (just_started)
1325 {
1326 first_object_file_end
1327 = cs->c_value + CSECT_LEN (&main_aux);
1328 just_started = 0;
1329 }
1330
1331 file_start_addr = cs->c_value;
1332 file_end_addr = cs->c_value + CSECT_LEN (&main_aux);
1333
1334 if (cs->c_name && cs->c_name[0] == '.')
1335 {
1336 last_csect_name = cs->c_name;
1337 last_csect_val = cs->c_value;
1338 last_csect_sec = cs->c_secnum;
1339 }
1340 }
1341 misc_func_recorded = 0;
1342 continue;
1343
1344 case XMC_RW :
1345 break;
1346
1347 /* If the section is not a data description,
1348 ignore it. Note that uninitialized data will
1349 show up as XTY_CM/XMC_RW pair. */
1350
1351 case XMC_TC0:
1352 if (toc_offset)
1353 warning ("More than one xmc_tc0 symbol found.");
1354 toc_offset = cs->c_value;
1355 continue;
1356
1357 case XMC_TC:
1358#ifdef STATIC_NODEBUG_VARS
1359 /* We need to process these symbols if they are C_HIDEXT,
1360 for static variables in files compiled without -g. */
1361 if (cs->c_sclass == C_HIDEXT)
1362 break;
1363 else
1364#endif
1365 continue;
e38e0312 1366
a81ce07d
JK
1367 default:
1368 /* Ignore the symbol. */
1369 continue;
818de002 1370 }
e38e0312 1371 }
e38e0312
JG
1372 break;
1373
a81ce07d
JK
1374 case XTY_LD:
1375
1376 switch (CSECT_SCLAS (&main_aux))
1377 {
1378 case XMC_PR:
1379 /* a function entry point. */
1380 function_entry_point:
1381 RECORD_MINIMAL_SYMBOL (cs->c_name, cs->c_value, mst_text,
1382 symname_alloced, cs->c_secnum,
1383 objfile);
1384
1385 fcn_line_offset = main_aux.x_sym.x_fcnary.x_fcn.x_lnnoptr;
1386 fcn_start_addr = cs->c_value;
1387
1388 /* save the function header info, which will be used
1389 when `.bf' is seen. */
1390 fcn_cs_saved = *cs;
1391 fcn_aux_saved = main_aux;
1392
1393 ptb = NULL;
1394
1395 /* If function has two auxent, then debugging information is
1396 already available for it. Process traceback table for
1397 functions with only one auxent. */
1398
1399 if (cs->c_naux == 1)
1400 ptb = retrieve_tracebackinfo (abfd, textsec, cs);
1401
1402 else if (cs->c_naux != 2)
1403 {
1404 static struct complaint msg =
1405 {"Expected one or two auxents for function", 0, 0};
1406 complain (&msg);
1407 }
1408
1409 /* If there is traceback info, create and add parameters
1410 for it. */
1411
1412 if (ptb && (ptb->fixedparms || ptb->floatparms))
1413 {
1414
1415 int parmcnt = ptb->fixedparms + ptb->floatparms;
1416 char *parmcode = (char*) &ptb->parminfo;
1417
1418 /* The link area is 0x18 bytes. */
1419 int parmvalue = ptb->framesize + 0x18;
1420 unsigned int ii, mask;
1421
1422 for (ii=0, mask = 0x80000000; ii <parmcnt; ++ii)
1423 {
1424 struct symbol *parm;
1425
1426 if (ptb->parminfo & mask)
1427 {
1428 /* float or double */
1429 mask = mask >> 1;
1430 if (ptb->parminfo & mask)
1431 {
1432 /* double parm */
1433 ADD_PARM_TO_PENDING
1434 (parm, parmvalue, builtin_type_double,
1435 local_symbols);
1436 parmvalue += sizeof (double);
1437 }
1438 else
1439 {
1440 /* float parm */
1441 ADD_PARM_TO_PENDING
1442 (parm, parmvalue, builtin_type_float,
1443 local_symbols);
1444 parmvalue += sizeof (float);
1445 }
1446 }
1447 else
1448 {
1449 /* fixed parm, use (int*) for hex rep. */
1450 ADD_PARM_TO_PENDING
1451 (parm, parmvalue,
1452 lookup_pointer_type (builtin_type_int),
1453 local_symbols);
1454 parmvalue += sizeof (int);
1455 }
1456 mask = mask >> 1;
1457 }
1458
1459 /* Fake this as a function. Needed in
1460 process_xcoff_symbol(). */
1461 cs->c_type = 32;
1462
1463 finish_block
1464 (process_xcoff_symbol (cs, objfile), &local_symbols,
1465 pending_blocks, cs->c_value,
1466 cs->c_value + ptb->fsize, objfile);
1467 }
1468 continue;
1469
1470 case XMC_GL:
1471 /* shared library function trampoline code entry point. */
1472
1473 /* record trampoline code entries as
1474 mst_solib_trampoline symbol. When we lookup mst
1475 symbols, we will choose mst_text over
1476 mst_solib_trampoline. */
1477 RECORD_MINIMAL_SYMBOL
1478 (cs->c_name, cs->c_value,
1479 mst_solib_trampoline,
1480 symname_alloced, cs->c_secnum, objfile);
1481 continue;
1482
1483 case XMC_DS:
1484 /* The symbols often have the same names as debug symbols for
1485 functions, and confuse lookup_symbol. */
1486 continue;
1487
1488 default:
1489 /* xlc puts each variable in a separate csect, so we get
1490 an XTY_SD for each variable. But gcc puts several
1491 variables in a csect, so that each variable only gets
1492 an XTY_LD. We still need to record them. This will
1493 typically be XMC_RW; I suspect XMC_RO and XMC_BS might
1494 be possible too. */
1495 break;
1496 }
e38e0312 1497
a81ce07d
JK
1498 default:
1499 break;
e38e0312 1500 }
a81ce07d 1501 }
e38e0312 1502
a81ce07d
JK
1503 switch (cs->c_sclass)
1504 {
e38e0312 1505
a81ce07d 1506 case C_FILE:
818de002 1507
a81ce07d 1508 /* see if the last csect needs to be recorded. */
818de002 1509
a81ce07d
JK
1510 if (last_csect_name && !misc_func_recorded)
1511 {
818de002 1512
a81ce07d
JK
1513 /* If no misc. function recorded in the last seen csect, enter
1514 it as a function. This will take care of functions like
1515 strcmp() compiled by xlc. */
818de002 1516
a81ce07d
JK
1517 int alloced = 0;
1518 RECORD_MINIMAL_SYMBOL
1519 (last_csect_name, last_csect_val,
1520 mst_text, alloced, last_csect_sec, objfile);
1521 }
818de002 1522
a81ce07d
JK
1523 /* c_value field contains symnum of next .file entry in table
1524 or symnum of first global after last .file. */
818de002 1525
a81ce07d 1526 next_file_symnum = cs->c_value;
818de002 1527
a81ce07d
JK
1528 /* Complete symbol table for last object file containing
1529 debugging information. */
818de002 1530
a81ce07d
JK
1531 /* Whether or not there was a csect in the previous file, we
1532 have to call `end_stabs' and `start_stabs' to reset
1533 type_vector, line_vector, etc. structures. */
818de002 1534
a81ce07d
JK
1535 complete_symtab (filestring, file_start_addr);
1536 cur_src_end_addr = file_end_addr;
1537 end_symtab (file_end_addr, 1, 0, objfile, textsec->target_index);
1538 end_stabs ();
818de002 1539
a81ce07d
JK
1540 /* XCOFF, according to the AIX 3.2 documentation, puts the filename
1541 in cs->c_name. But xlc 1.3.0.2 has decided to do things the
1542 standard COFF way and put it in the auxent. We use the auxent if
1543 the symbol is ".file" and an auxent exists, otherwise use the symbol
1544 itself. Simple enough. */
1545 if (!strcmp (cs->c_name, ".file") && cs->c_naux > 0)
1546 {
1547 bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
1548 0, cs->c_naux, &main_aux);
1549 filestring = coff_getfilename (&main_aux);
818de002 1550 }
a81ce07d
JK
1551 else
1552 filestring = cs->c_name;
d4cedfe4 1553
a81ce07d
JK
1554 start_stabs ();
1555 start_symtab (filestring, (char *)NULL, (CORE_ADDR)0);
1556 last_csect_name = 0;
507e4004 1557
a81ce07d
JK
1558 /* reset file start and end addresses. A compilation unit with no text
1559 (only data) should have zero file boundaries. */
1560 file_start_addr = file_end_addr = 0;
e38e0312 1561 break;
e38e0312 1562
a81ce07d
JK
1563 case C_FUN:
1564 fcn_stab_saved = *cs;
1565 break;
e38e0312 1566
a81ce07d
JK
1567 case C_FCN:
1568 if (STREQ (cs->c_name, ".bf"))
1569 {
818de002 1570
a81ce07d
JK
1571 bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
1572 0, cs->c_naux, &main_aux);
818de002 1573
a81ce07d 1574 within_function = 1;
818de002 1575
a81ce07d 1576 mark_first_line (fcn_line_offset, cs->c_symnum);
818de002 1577
a81ce07d 1578 new = push_context (0, fcn_start_addr);
e38e0312 1579
a81ce07d
JK
1580 new->name = define_symbol
1581 (fcn_cs_saved.c_value, fcn_stab_saved.c_name, 0, 0, objfile);
1582 if (new->name != NULL)
1583 SYMBOL_SECTION (new->name) = cs->c_secnum;
1584 }
1585 else if (STREQ (cs->c_name, ".ef"))
1586 {
e38e0312 1587
a81ce07d
JK
1588 bfd_coff_swap_aux_in (abfd, raw_auxptr, cs->c_type, cs->c_sclass,
1589 0, cs->c_naux, &main_aux);
1590
1591 /* The value of .ef is the address of epilogue code;
1592 not useful for gdb. */
1593 /* { main_aux.x_sym.x_misc.x_lnsz.x_lnno
1594 contains number of lines to '}' */
1595
1596 fcn_last_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
1597 new = pop_context ();
1598 if (context_stack_depth != 0)
1599 error ("\
1600 invalid symbol data; .bf/.ef/.bb/.eb symbol mismatch, at symbol %d.",
1601 symnum);
1602
1603 finish_block (new->name, &local_symbols, new->old_blocks,
1604 new->start_addr,
1605 fcn_cs_saved.c_value +
1606 fcn_aux_saved.x_sym.x_misc.x_fsize, objfile);
1607 within_function = 0;
1608 }
1609 break;
818de002 1610
a81ce07d
JK
1611 case C_BSTAT:
1612 /* Begin static block. */
1613 {
1614 struct internal_syment symbol;
af8b7906 1615
a81ce07d
JK
1616 read_symbol (&symbol, cs->c_value);
1617 static_block_base = symbol.n_value;
1618 static_block_section = symbol.n_scnum;
1619 }
1620 break;
af8b7906 1621
a81ce07d
JK
1622 case C_ESTAT:
1623 /* End of static block. */
1624 static_block_base = 0;
1625 static_block_section = -1;
1626 break;
e38e0312 1627
a81ce07d
JK
1628 case C_ARG:
1629 case C_REGPARM:
1630 case C_TPDEF:
1631 case C_STRTAG:
1632 case C_UNTAG:
1633 case C_ENTAG:
1634 printf_unfiltered
1635 ("ERROR: Unimplemented storage class: %d.\n", cs->c_sclass);
1636 break;
e38e0312 1637
a81ce07d
JK
1638 case C_LABEL:
1639 case C_NULL:
1640 /* Ignore these. */
1641 break;
e38e0312 1642
a81ce07d
JK
1643 /* This is wrong. These symbols are XMC_TC, which means that
1644 the value of the symbol is the address of the TOC entry, not
1645 the address of the variable itself. */
1646 case C_HIDEXT:
76a457c0 1647#ifdef STATIC_NODEBUG_VARS
a81ce07d
JK
1648 {
1649 /* This is the only place that static variables show up in files
1650 compiled without -g. External variables also have a C_EXT,
1651 so that is why we record everything as mst_file_* here. */
1652 enum minimal_symbol_type ms_type;
1653 CORE_ADDR tmpaddr;
1654 int sec;
818de002 1655
a81ce07d
JK
1656 sec = cs_to_section (cs, objfile);
1657 tmpaddr = cs->c_value;
e38e0312 1658
a81ce07d
JK
1659 switch (sec)
1660 {
1661 case SECT_OFF_TEXT:
1662 case SECT_OFF_RODATA:
1663 ms_type = mst_file_text;
1664 break;
1665 case SECT_OFF_DATA:
1666 ms_type = mst_file_data;
1667 break;
1668 case SECT_OFF_BSS:
1669 ms_type = mst_file_bss;
1670 break;
1671 default:
1672 ms_type = mst_unknown;
1673 break;
1674 }
1675 RECORD_MINIMAL_SYMBOL (cs->c_name, cs->c_value, ms_type,
1676 symname_alloced, cs->c_secnum, objfile);
1677 }
1678#endif /* STATIC_NODEBUG_VARS */
1679 break;
e38e0312 1680
a81ce07d
JK
1681 case C_BINCL:
1682 /* beginning of include file */
1683 /* In xlc output, C_BINCL/C_EINCL pair doesn't show up in sorted
1684 order. Thus, when wee see them, we might not know enough info
1685 to process them. Thus, we'll be saving them into a table
1686 (inclTable) and postpone their processing. */
e38e0312 1687
a81ce07d
JK
1688 record_include_begin (cs);
1689 break;
e38e0312 1690
a81ce07d
JK
1691 case C_EINCL:
1692 /* End of include file. */
1693 /* See the comment after case C_BINCL. */
1694 record_include_end (cs);
1695 break;
1eeba686 1696
a81ce07d
JK
1697 case C_BLOCK:
1698 if (STREQ (cs->c_name, ".bb"))
1699 {
1700 depth++;
1701 new = push_context (depth, cs->c_value);
1702 }
1703 else if (STREQ (cs->c_name, ".eb"))
1704 {
1705 new = pop_context ();
1706 if (depth != new->depth)
1707 error ("\
1708 Invalid symbol data: .bb/.eb symbol mismatch at symbol %d.",
1709 symnum);
1710
1711 depth--;
1712 if (local_symbols && context_stack_depth > 0)
1713 {
1714 /* Make a block for the local symbols within. */
1715 finish_block (new->name, &local_symbols, new->old_blocks,
1716 new->start_addr, cs->c_value, objfile);
1717 }
1718 local_symbols = new->locals;
1719 }
1720 break;
e38e0312 1721
a81ce07d
JK
1722 default:
1723 process_xcoff_symbol (cs, objfile);
1724 break;
e38e0312 1725 }
e38e0312
JG
1726 }
1727
e38e0312 1728 if (last_source_file)
d07734e3 1729 {
3c02636b 1730 end_symtab (cur_src_end_addr, 1, 0, objfile, textsec->target_index);
d07734e3
FF
1731 end_stabs ();
1732 }
e38e0312
JG
1733
1734 free (symtbl);
1ab3bf1b 1735 current_objfile = NULL;
556f3d90
PB
1736
1737 /* Record the toc offset value of this symbol table into ldinfo structure.
1738 If no XMC_TC0 is found, toc_offset should be zero. Another place to obtain
1739 this information would be file auxiliary header. */
1740
e5eeaaf8 1741#ifndef FAKING_RS6000
556f3d90 1742 xcoff_add_toc_to_loadinfo (toc_offset);
e5eeaaf8 1743#endif
e38e0312
JG
1744}
1745
1746#define SYMBOL_DUP(SYMBOL1, SYMBOL2) \
1747 (SYMBOL2) = (struct symbol *) \
1ab3bf1b 1748 obstack_alloc (&objfile->symbol_obstack, sizeof (struct symbol)); \
e38e0312
JG
1749 *(SYMBOL2) = *(SYMBOL1);
1750
1751
1752#define SYMNAME_ALLOC(NAME, ALLOCED) \
1ab3bf1b 1753 (ALLOCED) ? (NAME) : obstack_copy0 (&objfile->symbol_obstack, (NAME), strlen (NAME));
e38e0312
JG
1754
1755
a81ce07d
JK
1756static struct type *func_symbol_type;
1757static struct type *var_symbol_type;
1758
e38e0312
JG
1759/* process one xcoff symbol. */
1760
1761static struct symbol *
1ab3bf1b 1762process_xcoff_symbol (cs, objfile)
e38e0312 1763 register struct coff_symbol *cs;
1ab3bf1b 1764 struct objfile *objfile;
e38e0312
JG
1765{
1766 struct symbol onesymbol;
1767 register struct symbol *sym = &onesymbol;
1768 struct symbol *sym2 = NULL;
1769 struct type *ttype;
1770 char *name, *pp, *qq;
818de002 1771 int struct_and_type_combined;
6c6afbb9 1772 int nameless;
e38e0312
JG
1773
1774 name = cs->c_name;
1775 if (name[0] == '.')
1776 ++name;
1777
4ed97c9a 1778 memset (sym, '\0', sizeof (struct symbol));
e38e0312
JG
1779
1780 /* default assumptions */
1781 SYMBOL_VALUE (sym) = cs->c_value;
1782 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
3c02636b 1783 SYMBOL_SECTION (sym) = cs->c_secnum;
e38e0312 1784
a81ce07d
JK
1785 if (ISFCN (cs->c_type))
1786 {
1787 /* At this point, we don't know the type of the function. This
1788 will be patched with the type from its stab entry later on in
1789 patch_block_stabs (), unless the file was compiled without -g. */
e38e0312 1790
a81ce07d
JK
1791 SYMBOL_NAME (sym) = SYMNAME_ALLOC (name, symname_alloced);
1792 SYMBOL_TYPE (sym) = func_symbol_type;
e38e0312 1793
a81ce07d
JK
1794 SYMBOL_CLASS (sym) = LOC_BLOCK;
1795 SYMBOL_DUP (sym, sym2);
e38e0312 1796
a81ce07d
JK
1797 if (cs->c_sclass == C_EXT)
1798 add_symbol_to_list (sym2, &global_symbols);
1799 else if (cs->c_sclass == C_HIDEXT || cs->c_sclass == C_STAT)
1800 add_symbol_to_list (sym2, &file_symbols);
1801 }
1802 else
e38e0312 1803 {
a81ce07d
JK
1804 /* In case we can't figure out the type, provide default. */
1805 SYMBOL_TYPE (sym) = var_symbol_type;
1806
1807 switch (cs->c_sclass)
1808 {
818de002 1809#if 0
a81ce07d
JK
1810 case C_FUN:
1811 if (fcn_cs_saved.c_sclass == C_EXT)
1812 add_stab_to_list (name, &global_stabs);
1813 else
1814 add_stab_to_list (name, &file_stabs);
1815 break;
818de002 1816#endif
e38e0312 1817
a81ce07d
JK
1818 case C_GSYM:
1819 add_stab_to_list (name, &global_stabs);
1820 break;
e38e0312 1821
a81ce07d
JK
1822 case C_BCOMM:
1823 common_block_start (cs->c_name, objfile);
1824 break;
9438d642 1825
a81ce07d
JK
1826 case C_ECOMM:
1827 common_block_end (objfile);
1828 break;
9438d642 1829
a81ce07d
JK
1830 default:
1831 complain (&storclass_complaint, cs->c_sclass);
1832 /* FALLTHROUGH */
9438d642 1833
a81ce07d
JK
1834 case C_DECL:
1835 case C_PSYM:
1836 case C_RPSYM:
1837 case C_ECOML:
1eeba686 1838
a81ce07d
JK
1839 sym = define_symbol (cs->c_value, cs->c_name, 0, 0, objfile);
1840 if (sym != NULL)
1841 {
1842 SYMBOL_SECTION (sym) = cs->c_secnum;
1843 }
1844 return sym;
e38e0312 1845
a81ce07d 1846 case C_STSYM:
1eeba686 1847
a81ce07d
JK
1848 /* For xlc (not GCC), the 'V' symbol descriptor is used for
1849 all statics and we need to distinguish file-scope versus
1850 function-scope using within_function. We do this by
1851 changing the string we pass to define_symbol to use 'S'
1852 where we need to, which is not necessarily super-clean,
1853 but seems workable enough. */
91cc45da 1854
a81ce07d
JK
1855 if (*name == ':' || (pp = (char *) strchr(name, ':')) == NULL)
1856 return NULL;
91cc45da 1857
a81ce07d
JK
1858 ++pp;
1859 if (*pp == 'V' && !within_function)
1860 *pp = 'S';
1861 sym = define_symbol (cs->c_value, cs->c_name, 0, 0, objfile);
1862 if (sym != NULL)
1863 {
1864 SYMBOL_VALUE (sym) += static_block_base;
1865 SYMBOL_SECTION (sym) = static_block_section;
1866 }
1867 return sym;
e38e0312 1868
a81ce07d
JK
1869 case C_LSYM:
1870 sym = define_symbol (cs->c_value, cs->c_name, 0, N_LSYM, objfile);
1871 if (sym != NULL)
1872 {
1873 SYMBOL_SECTION (sym) = cs->c_secnum;
1874 }
1875 return sym;
e38e0312 1876
a81ce07d
JK
1877 case C_AUTO:
1878 SYMBOL_CLASS (sym) = LOC_LOCAL;
1879 SYMBOL_NAME (sym) = SYMNAME_ALLOC (name, symname_alloced);
1880 SYMBOL_SECTION (sym) = cs->c_secnum;
1881 SYMBOL_DUP (sym, sym2);
1882 add_symbol_to_list (sym2, &local_symbols);
1883 break;
e38e0312 1884
a81ce07d
JK
1885 case C_EXT:
1886 SYMBOL_CLASS (sym) = LOC_STATIC;
1887 SYMBOL_NAME (sym) = SYMNAME_ALLOC (name, symname_alloced);
1888 SYMBOL_SECTION (sym) = cs->c_secnum;
1889 SYMBOL_DUP (sym, sym2);
1890 add_symbol_to_list (sym2, &global_symbols);
1891 break;
e38e0312 1892
a81ce07d
JK
1893 case C_STAT:
1894 SYMBOL_CLASS (sym) = LOC_STATIC;
1895 SYMBOL_NAME (sym) = SYMNAME_ALLOC (name, symname_alloced);
1896 SYMBOL_SECTION (sym) = cs->c_secnum;
1897 SYMBOL_DUP (sym, sym2);
1898 add_symbol_to_list
1899 (sym2, within_function ? &local_symbols : &file_symbols);
1900 break;
e38e0312 1901
a81ce07d
JK
1902 case C_REG:
1903 printf_unfiltered ("ERROR! C_REG is not fully implemented!\n");
1904 SYMBOL_CLASS (sym) = LOC_REGISTER;
1905 SYMBOL_NAME (sym) = SYMNAME_ALLOC (name, symname_alloced);
1906 SYMBOL_SECTION (sym) = cs->c_secnum;
1907 SYMBOL_DUP (sym, sym2);
1908 add_symbol_to_list (sym2, &local_symbols);
1909 break;
e38e0312 1910
a81ce07d
JK
1911 case C_RSYM:
1912 pp = (char*) strchr (name, ':');
1913 if (pp)
1914 {
1915 sym = define_symbol (cs->c_value, cs->c_name, 0, 0, objfile);
1916 if (sym != NULL)
1917 SYMBOL_SECTION (sym) = cs->c_secnum;
1918 return sym;
1919 }
1920 else
1921 {
1922 complain (&rsym_complaint, name);
1923 return NULL;
1924 }
1eeba686 1925 }
e38e0312 1926 }
e38e0312
JG
1927 return sym2;
1928}
1929
e8abe489
PS
1930/* Extract the file name from the aux entry of a C_FILE symbol. Return
1931 only the last component of the name. Result is in static storage and
1932 is only good for temporary use. */
1933
1934static char *
1935coff_getfilename (aux_entry)
1936 union internal_auxent *aux_entry;
1937{
1938 static char buffer[BUFSIZ];
1939 register char *temp;
1940 char *result;
1941
1942 if (aux_entry->x_file.x_n.x_zeroes == 0)
1943 strcpy (buffer, strtbl + aux_entry->x_file.x_n.x_offset);
1944 else
1945 {
1946 strncpy (buffer, aux_entry->x_file.x_fname, FILNMLEN);
1947 buffer[FILNMLEN] = '\0';
1948 }
1949 result = buffer;
1950
1951 /* FIXME: We should not be throwing away the information about what
1952 directory. It should go into dirname of the symtab, or some such
1953 place. */
1954 if ((temp = strrchr (result, '/')) != NULL)
1955 result = temp + 1;
1956 return (result);
1957}
1958
3c02636b
JK
1959/* Set *SYMBOL to symbol number symno in symtbl. */
1960static void
1961read_symbol (symbol, symno)
1962 struct internal_syment *symbol;
e38e0312
JG
1963 int symno;
1964{
c84a96d7
JK
1965 if (symno < 0 || symno >= symtbl_num_syms)
1966 {
b646b438 1967 static struct complaint msg =
c84a96d7
JK
1968 {"Invalid symbol offset", 0, 0};
1969 complain (&msg);
3c02636b
JK
1970 symbol->n_value = 0;
1971 symbol->n_scnum = -1;
1972 return;
c84a96d7
JK
1973 }
1974 bfd_coff_swap_sym_in (symfile_bfd, symtbl + (symno*local_symesz), symbol);
3c02636b
JK
1975}
1976
1977/* Get value corresponding to symbol number symno in symtbl. */
1978
1979static int
1980read_symbol_nvalue (symno)
1981 int symno;
1982{
1983 struct internal_syment symbol[1];
1984
1985 read_symbol (symbol, symno);
e38e0312
JG
1986 return symbol->n_value;
1987}
1988
1989
c84a96d7
JK
1990/* Find the address of the function corresponding to symno, where
1991 symno is the symbol pointed to by the linetable. */
1992
e38e0312 1993static int
c84a96d7 1994read_symbol_lineno (symno)
e38e0312
JG
1995 int symno;
1996{
1997 struct internal_syment symbol[1];
1998 union internal_auxent main_aux[1];
1999
c84a96d7
JK
2000 /* Note that just searching for a short distance (e.g. 50 symbols)
2001 is not enough, at least in the following case.
2002
2003 .extern foo
2004 [many .stabx entries]
2005 [a few functions, referring to foo]
2006 .globl foo
2007 .bf
e38e0312 2008
c84a96d7
JK
2009 What happens here is that the assembler moves the .stabx entries
2010 to right before the ".bf" for foo, but the symbol for "foo" is before
2011 all the stabx entries. See PR gdb/2222. */
2012 while (symno < symtbl_num_syms) {
e38e0312 2013 bfd_coff_swap_sym_in (symfile_bfd,
c84a96d7 2014 symtbl + (symno*local_symesz), symbol);
2e4964ad 2015 if (symbol->n_sclass == C_FCN && STREQ (symbol->n_name, ".bf"))
e38e0312 2016 goto gotit;
6b5b330b 2017 symno += symbol->n_numaux+1;
e38e0312
JG
2018 }
2019
e9916390 2020 complain (&bf_notfound_complaint);
e38e0312
JG
2021 return 0;
2022
2023gotit:
2024 /* take aux entry and return its lineno */
2025 symno++;
c84a96d7 2026 bfd_coff_swap_aux_in (symfile_bfd, symtbl+(symno*local_symesz),
bf8d9d28
ILT
2027 symbol->n_type, symbol->n_sclass,
2028 0, symbol->n_numaux, main_aux);
e38e0312
JG
2029
2030 return main_aux->x_sym.x_misc.x_lnsz.x_lnno;
2031}
2032
2033/* Support for line number handling */
2034
2035/* This function is called for every section; it finds the outer limits
2036 * of the line table (minimum and maximum file offset) so that the
2037 * mainline code can read the whole thing for efficiency.
2038 */
2039static void
2040find_linenos(abfd, asect, vpinfo)
2041bfd *abfd;
2042sec_ptr asect;
1ab3bf1b 2043PTR vpinfo;
e38e0312
JG
2044{
2045 struct coff_symfile_info *info;
2046 int size, count;
2047 file_ptr offset, maxoff;
2048
2049 count = asect->lineno_count;
2050
2e4964ad 2051 if (!STREQ (asect->name, ".text") || count == 0)
e38e0312
JG
2052 return;
2053
2054 size = count * coff_data (symfile_bfd)->local_linesz;
2055 info = (struct coff_symfile_info *)vpinfo;
2056 offset = asect->line_filepos;
2057 maxoff = offset + size;
2058
2059 if (offset < info->min_lineno_offset || info->min_lineno_offset == 0)
2060 info->min_lineno_offset = offset;
2061
2062 if (maxoff > info->max_lineno_offset)
2063 info->max_lineno_offset = maxoff;
2064}
2065
2066
2067/* Read in all the line numbers for fast lookups later. Leave them in
2068 external (unswapped) format in memory; we'll swap them as we enter
2069 them into GDB's data structures. */
2070
2071static int
2072init_lineno (abfd, offset, size)
2073 bfd *abfd;
d5931d79 2074 file_ptr offset;
e38e0312
JG
2075 int size;
2076{
2077 int val;
2078
0eb22669
PS
2079 free_linetab ();
2080
d5931d79 2081 if (bfd_seek(abfd, offset, L_SET) < 0)
e38e0312
JG
2082 return -1;
2083
2084 linetab = (char *) xmalloc(size);
2085
2086 val = bfd_read(linetab, 1, size, abfd);
2087 if (val != size)
2088 return -1;
2089
2090 linetab_offset = offset;
2091 linetab_size = size;
e38e0312
JG
2092 return 0;
2093}
0eb22669
PS
2094
2095static void
2096free_linetab ()
2097{
2098 if (linetab)
2099 free (linetab);
2100 linetab = NULL;
2101}
93fe4e33 2102\f
1ab3bf1b 2103static void
9b280a7f 2104xcoff_new_init (objfile)
80d68b1d 2105 struct objfile *objfile;
e38e0312 2106{
e38e0312
JG
2107}
2108
dd469789
JG
2109
2110/* xcoff_symfile_init()
2111 is the xcoff-specific initialization routine for reading symbols.
2112 It is passed an objfile which contains, among other things,
2113 the BFD for the file whose symbols are being read, and a slot for
2114 a pointer to "private data" which we fill with cookies and other
2115 treats for xcoff_symfile_read().
2116
2117 We will only be called if this is an XCOFF or XCOFF-like file.
2118 BFD handles figuring out the format of the file, and code in symfile.c
2119 uses BFD's determination to vector to us.
2120
2121 The ultimate result is a new symtab (or, FIXME, eventually a psymtab). */
2122
1ab3bf1b 2123static void
9b280a7f 2124xcoff_symfile_init (objfile)
80d68b1d 2125 struct objfile *objfile;
e38e0312 2126{
80d68b1d 2127 bfd *abfd = objfile->obfd;
e38e0312
JG
2128
2129 /* Allocate struct to keep track of the symfile */
80d68b1d
FF
2130 objfile -> sym_private = xmmalloc (objfile -> md,
2131 sizeof (struct coff_symfile_info));
5e2e79f8 2132 init_entry_point_info (objfile);
e38e0312
JG
2133}
2134
80d68b1d
FF
2135/* Perform any local cleanups required when we are done with a particular
2136 objfile. I.E, we are in the process of discarding all symbol information
2137 for an objfile, freeing up all memory held for it, and unlinking the
2138 objfile struct from the global list of known objfiles. */
2139
2140static void
9b280a7f 2141xcoff_symfile_finish (objfile)
80d68b1d
FF
2142 struct objfile *objfile;
2143{
2144 if (objfile -> sym_private != NULL)
2145 {
2146 mfree (objfile -> md, objfile -> sym_private);
2147 }
2148
2149 /* Start with a fresh include table for the next objfile. */
2150
2151 if (inclTable)
2152 {
2153 free (inclTable);
2154 inclTable = NULL;
2155 }
2156 inclIndx = inclLength = inclDepth = 0;
2157}
2158
e38e0312
JG
2159
2160static int
1ab3bf1b 2161init_stringtab(abfd, offset, objfile)
e38e0312 2162 bfd *abfd;
d5931d79 2163 file_ptr offset;
1ab3bf1b 2164 struct objfile *objfile;
e38e0312
JG
2165{
2166 long length;
2167 int val;
2168 unsigned char lengthbuf[4];
2169
d5931d79 2170 if (bfd_seek(abfd, offset, L_SET) < 0)
e38e0312
JG
2171 return -1;
2172
2173 val = bfd_read((char *)lengthbuf, 1, sizeof lengthbuf, abfd);
2174 length = bfd_h_get_32(abfd, lengthbuf);
2175
2176 /* If no string table is needed, then the file may end immediately
2177 after the symbols. Just return with `strtbl' set to null. */
2178
2179 if (val != sizeof length || length < sizeof length)
2180 return 0;
2181
2182 /* Allocate string table from symbol_obstack. We will need this table
2183 as long as we have its symbol table around. */
2184
1ab3bf1b 2185 strtbl = (char*) obstack_alloc (&objfile->symbol_obstack, length);
e38e0312
JG
2186 if (strtbl == NULL)
2187 return -1;
2188
ade40d31 2189 memcpy(strtbl, &length, sizeof length);
e38e0312
JG
2190 if (length == sizeof length)
2191 return 0;
2192
2193 val = bfd_read(strtbl + sizeof length, 1, length - sizeof length, abfd);
2194
2195 if (val != length - sizeof length || strtbl[length - 1] != '\0')
2196 return -1;
2197
2198 return 0;
2199}
2200
2201static int
2202init_debugsection(abfd)
2203 bfd *abfd;
2204{
2205 register sec_ptr secp;
2206 bfd_size_type length;
2207
2208 if (debugsec) {
2209 free(debugsec);
2210 debugsec = NULL;
2211 }
2212
2213 secp = bfd_get_section_by_name(abfd, ".debug");
2214 if (!secp)
2215 return 0;
2216
2217 if (!(length = bfd_section_size(abfd, secp)))
2218 return 0;
2219
1ab3bf1b 2220 debugsec = (char *) xmalloc ((unsigned)length);
e38e0312
JG
2221 if (debugsec == NULL)
2222 return -1;
2223
2224 if (!bfd_get_section_contents(abfd, secp, debugsec, (file_ptr) 0, length)) {
199b2450 2225 printf_unfiltered ("Can't read .debug section from symbol file\n");
e38e0312
JG
2226 return -1;
2227 }
2228 return 0;
2229}
2230
2231static void
2232free_debugsection()
2233{
2234 if (debugsec)
2235 free(debugsec);
2236 debugsec = NULL;
2237}
2238
2239
9b280a7f 2240/* xcoff version of symbol file read. */
e38e0312 2241
1ab3bf1b 2242static void
9b280a7f 2243xcoff_symfile_read (objfile, section_offset, mainline)
80d68b1d 2244 struct objfile *objfile;
1b880e74 2245 struct section_offsets *section_offset;
e38e0312
JG
2246 int mainline;
2247{
d5931d79
JG
2248 int num_symbols; /* # of symbols */
2249 file_ptr symtab_offset; /* symbol table and */
2250 file_ptr stringtab_offset; /* string table file offsets */
e38e0312
JG
2251 int val;
2252 bfd *abfd;
80d68b1d 2253 struct coff_symfile_info *info;
e38e0312 2254 char *name;
0eb22669 2255 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
e38e0312 2256
80d68b1d
FF
2257 info = (struct coff_symfile_info *) objfile -> sym_private;
2258 symfile_bfd = abfd = objfile->obfd;
2259 name = objfile->name;
e38e0312
JG
2260
2261 num_symbols = bfd_get_symcount (abfd); /* # of symbols */
2262 symtab_offset = obj_sym_filepos (abfd); /* symbol table file offset */
2263 stringtab_offset = symtab_offset +
2264 num_symbols * coff_data(abfd)->local_symesz;
2265
2266 info->min_lineno_offset = 0;
2267 info->max_lineno_offset = 0;
2268 bfd_map_over_sections (abfd, find_linenos, info);
2269
2270 /* FIXME! This stuff should move into symfile_init */
2271 if (info->min_lineno_offset != 0
2272 && info->max_lineno_offset > info->min_lineno_offset) {
2273
2274 /* only read in the line # table if one exists */
0eb22669 2275 make_cleanup (free_linetab, 0);
e38e0312 2276 val = init_lineno(abfd, info->min_lineno_offset,
d5931d79 2277 (int) (info->max_lineno_offset - info->min_lineno_offset));
e38e0312
JG
2278
2279 if (val < 0)
2280 error("\"%s\": error reading line numbers\n", name);
2281 }
2282
9d61147e
JK
2283 if (num_symbols > 0)
2284 {
2285 val = init_stringtab(abfd, stringtab_offset, objfile);
2286 if (val < 0) {
2287 error ("\"%s\": can't get string table", name);
2288 }
e38e0312 2289
9d61147e
JK
2290 if (init_debugsection(abfd) < 0) {
2291 error ("Error reading .debug section of `%s'\n", name);
2292 }
2293 }
e38e0312
JG
2294
2295 /* Position to read the symbol table. Do not read it all at once. */
d5931d79 2296 val = bfd_seek(abfd, symtab_offset, L_SET);
e38e0312
JG
2297 if (val < 0)
2298 perror_with_name(name);
2299
2300 if (bfd_tell(abfd) != symtab_offset)
2301 fatal("bfd? BFD!");
2302
1ab3bf1b
JG
2303 init_minimal_symbol_collection ();
2304 make_cleanup (discard_minimal_symbols, 0);
e38e0312 2305
e5eeaaf8 2306#ifndef FAKING_RS6000
556f3d90 2307 /* Initialize load info structure. */
e38e0312 2308 if (mainline)
556f3d90 2309 xcoff_init_loadinfo ();
e5eeaaf8 2310#endif
e38e0312
JG
2311
2312 /* Now that the executable file is positioned at symbol table,
2313 process it and define symbols accordingly. */
2314
80d68b1d 2315 read_xcoff_symtab(objfile, num_symbols);
e38e0312 2316
1eeba686
PB
2317 /* Free debug section. */
2318 free_debugsection ();
e38e0312
JG
2319
2320 /* Sort symbols alphabetically within each block. */
577e6a8d
JK
2321 {
2322 struct symtab *s;
2323 for (s = objfile -> symtabs; s != NULL; s = s -> next)
2324 {
2325 sort_symtab_syms (s);
2326 }
2327 }
e38e0312 2328
1ab3bf1b
JG
2329 /* Install any minimal symbols that have been collected as the current
2330 minimal symbols for this objfile. */
2331
80d68b1d 2332 install_minimal_symbols (objfile);
0eb22669
PS
2333
2334 do_cleanups (back_to);
e38e0312
JG
2335}
2336
3c02636b
JK
2337/* XCOFF-specific parsing routine for section offsets. */
2338
2339static int largest_section;
2340
2341static void
2342note_one_section (abfd, asect, ptr)
2343 bfd *abfd;
2344 asection *asect;
2345 PTR ptr;
2346{
2347 if (asect->target_index > largest_section)
2348 largest_section = asect->target_index;
2349}
2670f34d
JG
2350
2351static
2352struct section_offsets *
9b280a7f 2353xcoff_symfile_offsets (objfile, addr)
2670f34d
JG
2354 struct objfile *objfile;
2355 CORE_ADDR addr;
2356{
2357 struct section_offsets *section_offsets;
2358 int i;
3c02636b
JK
2359
2360 largest_section = 0;
2361 bfd_map_over_sections (objfile->obfd, note_one_section, NULL);
2362 objfile->num_sections = largest_section + 1;
2670f34d 2363 section_offsets = (struct section_offsets *)
3c02636b
JK
2364 obstack_alloc
2365 (&objfile -> psymbol_obstack,
2366 sizeof (struct section_offsets)
2367 + sizeof (section_offsets->offsets) * (objfile->num_sections));
2670f34d 2368
1d7e34e1
JK
2369 /* syms_from_objfile kindly subtracts from addr the bfd_section_vma
2370 of the .text section. This strikes me as wrong--whether the
2371 offset to be applied to symbol reading is relative to the start
2372 address of the section depends on the symbol format. In any
2373 event, this whole "addr" concept is pretty broken (it doesn't
2374 handle any section but .text sensibly), so just ignore the addr
2375 parameter and use 0. That matches the fact that xcoff_symfile_read
2376 ignores the section_offsets). */
3c02636b 2377 for (i = 0; i < objfile->num_sections; i++)
1d7e34e1 2378 ANOFFSET (section_offsets, i) = 0;
2670f34d
JG
2379
2380 return section_offsets;
2381}
0eed42de
JK
2382
2383/* Register our ability to parse symbols for xcoff BFD files. */
e38e0312 2384
9b280a7f 2385static struct sym_fns xcoff_sym_fns =
e38e0312 2386{
0eed42de
JK
2387
2388 /* Because the bfd uses coff_flavour, we need to specially kludge
2389 the flavour. FIXME: coff and xcoff and fundamentally similar
2390 except for debug format, and we should see if we can merge this
2391 file with coffread.c. For example, the extra storage classes
2392 used for stabs could presumably be recognized in any COFF file. */
2393
52912aac 2394 (enum bfd_flavour)-1,
0eed42de 2395
9b280a7f
JG
2396 xcoff_new_init, /* sym_new_init: init anything gbl to entire symtab */
2397 xcoff_symfile_init, /* sym_init: read initial info, setup for sym_read() */
2398 xcoff_symfile_read, /* sym_read: read a symbol file into symtab */
2399 xcoff_symfile_finish, /* sym_finish: finished with file, cleanup */
2400 xcoff_symfile_offsets, /* sym_offsets: xlate offsets ext->int form */
80d68b1d 2401 NULL /* next: pointer to next struct sym_fns */
e38e0312
JG
2402};
2403
2404void
2405_initialize_xcoffread ()
2406{
9b280a7f 2407 add_symtab_fns(&xcoff_sym_fns);
e5eeaaf8 2408
a81ce07d
JK
2409 /* Initialize symbol template later used for arguments. Its other
2410 fields are zero, or are filled in later. */
e5eeaaf8
JG
2411 SYMBOL_NAME (&parmsym) = "";
2412 SYMBOL_INIT_LANGUAGE_SPECIFIC (&parmsym, language_c);
2413 SYMBOL_NAMESPACE (&parmsym) = VAR_NAMESPACE;
2414 SYMBOL_CLASS (&parmsym) = LOC_ARG;
a81ce07d
JK
2415
2416 func_symbol_type = init_type (TYPE_CODE_FUNC, 1, 0,
2417 "<function, no debug info>", NULL);
2418 TYPE_TARGET_TYPE (func_symbol_type) = builtin_type_int;
2419 var_symbol_type =
2420 init_type (TYPE_CODE_INT, TARGET_INT_BIT / HOST_CHAR_BIT, 0,
2421 "<variable, no debug info>", NULL);
e38e0312 2422}
This page took 0.338245 seconds and 4 git commands to generate.