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