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