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