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