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