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