* coffread.c, xcoffread.c: Include <coff/internal.h>
[binutils-gdb.git] / gdb / coffread.c
1 /* Read coff symbol tables and convert to internal format, for GDB.
2 Contributed by David D. Johnson, Brown University (ddj@cs.brown.edu).
3 Copyright 1987, 1988, 1989, 1990, 1991, 1992, 1993
4 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
21
22 #include "defs.h"
23 #include "symtab.h"
24 #include "gdbtypes.h"
25 #include "breakpoint.h"
26
27 #include "bfd.h"
28 #include <obstack.h>
29
30 #include <string.h>
31
32 #include <time.h> /* For time_t in libbfd.h. */
33 #include <sys/types.h> /* For time_t, if not in time.h. */
34 #include "libbfd.h" /* FIXME secret internal data from BFD */
35 #include "coff/internal.h" /* Internal format of COFF symbols in BFD */
36 #include "libcoff.h" /* FIXME secret internal data from BFD */
37
38 #include "symfile.h"
39 #include "objfiles.h"
40 #include "buildsym.h"
41 #include "gdb-stabs.h"
42 #include "stabsread.h"
43 #include "complaints.h"
44
45 struct coff_symfile_info {
46 file_ptr min_lineno_offset; /* Where in file lowest line#s are */
47 file_ptr max_lineno_offset; /* 1+last byte of line#s in file */
48
49 asection *stabsect; /* Section pointer for .stab section */
50 asection *stabstrsect; /* Section pointer for .stab section */
51 asection *stabindexsect; /* Section pointer for .stab.index section */
52 char *stabstrdata;
53 };
54
55 /* Translate an external name string into a user-visible name. */
56 #define EXTERNAL_NAME(string, abfd) \
57 (string[0] == bfd_get_symbol_leading_char(abfd)? string+1: string)
58
59 /* To be an sdb debug type, type must have at least a basic or primary
60 derived type. Using this rather than checking against T_NULL is
61 said to prevent core dumps if we try to operate on Michael Bloom
62 dbx-in-coff file. */
63
64 #define SDB_TYPE(type) (BTYPE(type) | (type & N_TMASK))
65
66 /*
67 * Convert from an sdb register number to an internal gdb register number.
68 * This should be defined in tm.h, if REGISTER_NAMES is not set up
69 * to map one to one onto the sdb register numbers.
70 */
71 #ifndef SDB_REG_TO_REGNUM
72 # define SDB_REG_TO_REGNUM(value) (value)
73 #endif
74
75 /* Core address of start and end of text of current source file.
76 This comes from a ".text" symbol where x_nlinno > 0. */
77
78 static CORE_ADDR cur_src_start_addr;
79 static CORE_ADDR cur_src_end_addr;
80
81 /* The addresses of the symbol table stream and number of symbols
82 of the object file we are reading (as copied into core). */
83
84 static GDB_FILE *nlist_stream_global;
85 static int nlist_nsyms_global;
86
87 /* Vector of line number information. */
88
89 static struct linetable *line_vector;
90
91 /* Index of next entry to go in line_vector_index. */
92
93 static int line_vector_index;
94
95 /* Last line number recorded in the line vector. */
96
97 static int prev_line_number;
98
99 /* Number of elements allocated for line_vector currently. */
100
101 static int line_vector_length;
102
103 /* Pointers to scratch storage, used for reading raw symbols and auxents. */
104
105 static char *temp_sym;
106 static char *temp_aux;
107
108 /* Local variables that hold the shift and mask values for the
109 COFF file that we are currently reading. These come back to us
110 from BFD, and are referenced by their macro names, as well as
111 internally to the BTYPE, ISPTR, ISFCN, ISARY, ISTAG, and DECREF
112 macros from ../internalcoff.h . */
113
114 static unsigned local_n_btmask;
115 static unsigned local_n_btshft;
116 static unsigned local_n_tmask;
117 static unsigned local_n_tshift;
118
119 #define N_BTMASK local_n_btmask
120 #define N_BTSHFT local_n_btshft
121 #define N_TMASK local_n_tmask
122 #define N_TSHIFT local_n_tshift
123
124 /* Local variables that hold the sizes in the file of various COFF structures.
125 (We only need to know this to read them from the file -- BFD will then
126 translate the data in them, into `internal_xxx' structs in the right
127 byte order, alignment, etc.) */
128
129 static unsigned local_linesz;
130 static unsigned local_symesz;
131 static unsigned local_auxesz;
132
133
134 /* Chain of typedefs of pointers to empty struct/union types.
135 They are chained thru the SYMBOL_VALUE_CHAIN. */
136
137 static struct symbol *opaque_type_chain[HASHSIZE];
138
139 #if 0
140 /* The type of the function we are currently reading in. This is
141 used by define_symbol to record the type of arguments to a function. */
142
143 struct type *in_function_type;
144 #endif
145
146 struct pending_block *pending_blocks;
147
148 /* Complaints about various problems in the file being read */
149
150 struct complaint ef_complaint =
151 {"Unmatched .ef symbol(s) ignored starting at symnum %d", 0, 0};
152
153 struct complaint bf_no_aux_complaint =
154 {"`.bf' symbol %d has no aux entry", 0, 0};
155
156 struct complaint ef_no_aux_complaint =
157 {"`.ef' symbol %d has no aux entry", 0, 0};
158
159 struct complaint lineno_complaint =
160 {"Line number pointer %d lower than start of line numbers", 0, 0};
161
162 struct complaint unexpected_type_complaint =
163 {"Unexpected type for symbol %s", 0, 0};
164
165 struct complaint bad_sclass_complaint =
166 {"Bad n_sclass for symbol %s", 0, 0};
167
168 struct complaint misordered_blocks_complaint =
169 {"Blocks out of order at address %x", 0, 0};
170
171 struct complaint tagndx_bad_complaint =
172 {"Symbol table entry for %s has bad tagndx value", 0, 0};
173
174 struct complaint eb_complaint =
175 {"Mismatched .eb symbol ignored starting at symnum %d", 0, 0};
176
177 /* Simplified internal version of coff symbol table information */
178
179 struct coff_symbol {
180 char *c_name;
181 int c_symnum; /* symbol number of this entry */
182 int c_naux; /* 0 if syment only, 1 if syment + auxent, etc */
183 long c_value;
184 int c_sclass;
185 int c_secnum;
186 unsigned int c_type;
187 };
188
189 static struct type *
190 coff_read_struct_type PARAMS ((int, int, int));
191
192 static struct type *
193 decode_base_type PARAMS ((struct coff_symbol *, unsigned int,
194 union internal_auxent *));
195
196 static struct type *
197 decode_type PARAMS ((struct coff_symbol *, unsigned int,
198 union internal_auxent *));
199
200 static struct type *
201 decode_function_type PARAMS ((struct coff_symbol *, unsigned int,
202 union internal_auxent *));
203
204 static struct type *
205 coff_read_enum_type PARAMS ((int, int, int));
206
207 static struct symbol *
208 process_coff_symbol PARAMS ((struct coff_symbol *, union internal_auxent *,
209 struct objfile *));
210
211 static void
212 patch_opaque_types PARAMS ((struct symtab *));
213
214 static void
215 patch_type PARAMS ((struct type *, struct type *));
216
217 static void
218 enter_linenos PARAMS ((long, int, int));
219
220 static void
221 free_linetab PARAMS ((void));
222
223 static int
224 init_lineno PARAMS ((int, long, int));
225
226 static char *
227 getsymname PARAMS ((struct internal_syment *));
228
229 static void
230 free_stringtab PARAMS ((void));
231
232 static int
233 init_stringtab PARAMS ((int, long));
234
235 static void
236 read_one_sym PARAMS ((struct coff_symbol *, struct internal_syment *,
237 union internal_auxent *));
238
239 static void
240 read_coff_symtab PARAMS ((long, int, struct objfile *));
241
242 static void
243 find_linenos PARAMS ((bfd *, sec_ptr, PTR));
244
245 static void
246 coff_symfile_init PARAMS ((struct objfile *));
247
248 static void
249 coff_new_init PARAMS ((struct objfile *));
250
251 static void
252 coff_symfile_read PARAMS ((struct objfile *, struct section_offsets *, int));
253
254 static void
255 coff_symfile_finish PARAMS ((struct objfile *));
256
257 static void record_minimal_symbol
258 PARAMS ((char *, CORE_ADDR, enum minimal_symbol_type, struct objfile *));
259
260 static void
261 coff_end_symtab PARAMS ((struct objfile *));
262
263 static void
264 complete_symtab PARAMS ((char *, CORE_ADDR, unsigned int));
265
266 static void
267 coff_start_symtab PARAMS ((void));
268
269 static void
270 coff_record_line PARAMS ((int, CORE_ADDR));
271
272 static struct type *
273 coff_alloc_type PARAMS ((int));
274
275 static struct type **
276 coff_lookup_type PARAMS ((int));
277
278 \f
279 static void
280 coff_locate_sections PARAMS ((bfd *, asection *, PTR));
281
282 /* We are called once per section from coff_symfile_read. We
283 need to examine each section we are passed, check to see
284 if it is something we are interested in processing, and
285 if so, stash away some access information for the section.
286
287 FIXME: The section names should not be hardwired strings (what
288 should they be? I don't think most object file formats have enough
289 section flags to specify what kind of debug section it is
290 -kingdon). */
291
292 static void
293 coff_locate_sections (ignore_abfd, sectp, csip)
294 bfd *ignore_abfd;
295 asection *sectp;
296 PTR csip;
297 {
298 register struct coff_symfile_info *csi;
299
300 csi = (struct coff_symfile_info *) csip;
301 if (STREQ (sectp->name, ".stab"))
302 {
303 csi->stabsect = sectp;
304 }
305 else if (STREQ (sectp->name, ".stabstr"))
306 {
307 csi->stabstrsect = sectp;
308 }
309 else if (STREQ (sectp->name, ".stab.index"))
310 {
311 csi->stabindexsect = sectp;
312 }
313 }
314
315 /* Look up a coff type-number index. Return the address of the slot
316 where the type for that index is stored.
317 The type-number is in INDEX.
318
319 This can be used for finding the type associated with that index
320 or for associating a new type with the index. */
321
322 static struct type **
323 coff_lookup_type (index)
324 register int index;
325 {
326 if (index >= type_vector_length)
327 {
328 int old_vector_length = type_vector_length;
329
330 type_vector_length *= 2;
331 if (index /* is still */ >= type_vector_length) {
332 type_vector_length = index * 2;
333 }
334 type_vector = (struct type **)
335 xrealloc ((char *) type_vector,
336 type_vector_length * sizeof (struct type *));
337 memset (&type_vector[old_vector_length], 0,
338 (type_vector_length - old_vector_length) * sizeof(struct type *));
339 }
340 return &type_vector[index];
341 }
342
343 /* Make sure there is a type allocated for type number index
344 and return the type object.
345 This can create an empty (zeroed) type object. */
346
347 static struct type *
348 coff_alloc_type (index)
349 int index;
350 {
351 register struct type **type_addr = coff_lookup_type (index);
352 register struct type *type = *type_addr;
353
354 /* If we are referring to a type not known at all yet,
355 allocate an empty type for it.
356 We will fill it in later if we find out how. */
357 if (type == NULL)
358 {
359 type = alloc_type (current_objfile);
360 *type_addr = type;
361 }
362 return type;
363 }
364 \f
365 /* Record a line number entry for line LINE at address PC.
366 FIXME: Use record_line instead. */
367
368 static void
369 coff_record_line (line, pc)
370 int line;
371 CORE_ADDR pc;
372 {
373 struct linetable_entry *e;
374 /* Make sure line vector is big enough. */
375
376 if (line_vector_index + 2 >= line_vector_length)
377 {
378 line_vector_length *= 2;
379 line_vector = (struct linetable *)
380 xrealloc ((char *) line_vector, sizeof (struct linetable)
381 + (line_vector_length
382 * sizeof (struct linetable_entry)));
383 }
384
385 e = line_vector->item + line_vector_index++;
386 e->line = line; e->pc = pc;
387 }
388 \f
389 /* Start a new symtab for a new source file.
390 This is called when a COFF ".file" symbol is seen;
391 it indicates the start of data for one original source file. */
392
393 static void
394 coff_start_symtab ()
395 {
396 start_symtab (
397 /* We fill in the filename later. start_symtab
398 puts this pointer into last_source file and in
399 coff_end_symtab we assume we can free() it.
400 FIXME: leaks memory. */
401 savestring ("", 0),
402 /* We never know the directory name for COFF. */
403 NULL,
404 /* The start address is irrelevant, since we set
405 last_source_start_addr in coff_end_symtab. */
406 0);
407
408 /* Initialize the source file line number information for this file. */
409
410 if (line_vector) /* Unlikely, but maybe possible? */
411 free ((PTR)line_vector);
412 line_vector_index = 0;
413 line_vector_length = 1000;
414 prev_line_number = -2; /* Force first line number to be explicit */
415 line_vector = (struct linetable *)
416 xmalloc (sizeof (struct linetable)
417 + line_vector_length * sizeof (struct linetable_entry));
418 }
419
420 /* Save the vital information from when starting to read a file,
421 for use when closing off the current file.
422 NAME is the file name the symbols came from, START_ADDR is the first
423 text address for the file, and SIZE is the number of bytes of text. */
424
425 static void
426 complete_symtab (name, start_addr, size)
427 char *name;
428 CORE_ADDR start_addr;
429 unsigned int size;
430 {
431 last_source_file = savestring (name, strlen (name));
432 cur_src_start_addr = start_addr;
433 cur_src_end_addr = start_addr + size;
434
435 if (current_objfile -> ei.entry_point >= cur_src_start_addr &&
436 current_objfile -> ei.entry_point < cur_src_end_addr)
437 {
438 current_objfile -> ei.entry_file_lowpc = cur_src_start_addr;
439 current_objfile -> ei.entry_file_highpc = cur_src_end_addr;
440 }
441 }
442
443 /* Finish the symbol definitions for one main source file,
444 close off all the lexical contexts for that file
445 (creating struct block's for them), then make the
446 struct symtab for that file and put it in the list of all such. */
447
448 static void
449 coff_end_symtab (objfile)
450 struct objfile *objfile;
451 {
452 struct symtab *symtab;
453
454 last_source_start_addr = cur_src_start_addr;
455
456 /* For no good reason, this file stores the number of entries in a
457 separate variable instead of in line_vector->nitems. Fix it. */
458 if (line_vector)
459 line_vector->nitems = line_vector_index;
460
461 /* For COFF, we only have one subfile, so we can just look at
462 subfiles and not worry about there being other elements in the
463 chain. We fill in various fields now because we didn't know them
464 before (or because doing it now is simply an artifact of how this
465 file used to be written). */
466 subfiles->line_vector = line_vector;
467 subfiles->name = last_source_file;
468
469 /* sort_pending is needed for amdcoff, at least.
470 sort_linevec is needed for the SCO compiler. */
471 symtab = end_symtab (cur_src_end_addr, 1, 1, objfile, 0);
472
473 if (symtab != NULL)
474 free_named_symtabs (symtab->filename);
475
476 /* Reinitialize for beginning of new file. */
477 line_vector = 0;
478 line_vector_length = -1;
479 last_source_file = NULL;
480 }
481 \f
482 static void
483 record_minimal_symbol (name, address, type, objfile)
484 char *name;
485 CORE_ADDR address;
486 enum minimal_symbol_type type;
487 struct objfile *objfile;
488 {
489 /* We don't want TDESC entry points in the minimal symbol table */
490 if (name[0] == '@') return;
491
492 prim_record_minimal_symbol (savestring (name, strlen (name)), address, type,
493 objfile);
494 }
495 \f
496 /* coff_symfile_init ()
497 is the coff-specific initialization routine for reading symbols.
498 It is passed a struct objfile which contains, among other things,
499 the BFD for the file whose symbols are being read, and a slot for
500 a pointer to "private data" which we fill with cookies and other
501 treats for coff_symfile_read ().
502
503 We will only be called if this is a COFF or COFF-like file.
504 BFD handles figuring out the format of the file, and code in symtab.c
505 uses BFD's determination to vector to us.
506
507 The ultimate result is a new symtab (or, FIXME, eventually a psymtab). */
508
509 static int text_bfd_scnum;
510
511 static void
512 coff_symfile_init (objfile)
513 struct objfile *objfile;
514 {
515 asection *section;
516 bfd *abfd = objfile->obfd;
517
518 /* Allocate struct to keep track of stab reading. */
519 objfile->sym_stab_info = (PTR)
520 xmmalloc (objfile -> md, sizeof (struct dbx_symfile_info));
521
522 memset ((PTR) objfile->sym_stab_info, 0, sizeof (struct dbx_symfile_info));
523
524 /* Allocate struct to keep track of the symfile */
525 objfile -> sym_private = xmmalloc (objfile -> md,
526 sizeof (struct coff_symfile_info));
527
528 memset (objfile->sym_private, 0, sizeof (struct coff_symfile_info));
529
530 init_entry_point_info (objfile);
531
532 /* Save the section number for the text section */
533 section = bfd_get_section_by_name (abfd, ".text");
534 if (section)
535 text_bfd_scnum = section->index;
536 else
537 text_bfd_scnum = -1;
538 }
539
540 /* This function is called for every section; it finds the outer limits
541 of the line table (minimum and maximum file offset) so that the
542 mainline code can read the whole thing for efficiency. */
543
544 /* ARGSUSED */
545 static void
546 find_linenos (abfd, asect, vpinfo)
547 bfd *abfd;
548 sec_ptr asect;
549 PTR vpinfo;
550 {
551 struct coff_symfile_info *info;
552 int size, count;
553 file_ptr offset, maxoff;
554
555 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
556 count = asect->lineno_count;
557 /* End of warning */
558
559 if (count == 0)
560 return;
561 size = count * local_linesz;
562
563 info = (struct coff_symfile_info *)vpinfo;
564 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
565 offset = asect->line_filepos;
566 /* End of warning */
567
568 if (offset < info->min_lineno_offset || info->min_lineno_offset == 0)
569 info->min_lineno_offset = offset;
570
571 maxoff = offset + size;
572 if (maxoff > info->max_lineno_offset)
573 info->max_lineno_offset = maxoff;
574 }
575
576
577 /* The BFD for this file -- only good while we're actively reading
578 symbols into a psymtab or a symtab. */
579
580 static bfd *symfile_bfd;
581
582 /* Read a symbol file, after initialization by coff_symfile_init. */
583 /* FIXME! Addr and Mainline are not used yet -- this will not work for
584 shared libraries or add_file! */
585
586 /* ARGSUSED */
587 static void
588 coff_symfile_read (objfile, section_offsets, mainline)
589 struct objfile *objfile;
590 struct section_offsets *section_offsets;
591 int mainline;
592 {
593 struct coff_symfile_info *info;
594 struct dbx_symfile_info *dbxinfo;
595 bfd *abfd = objfile->obfd;
596 coff_data_type *cdata = coff_data (abfd);
597 char *name = bfd_get_filename (abfd);
598 int desc;
599 register int val;
600 int num_symbols;
601 int symtab_offset;
602 int stringtab_offset;
603 struct cleanup *back_to;
604 int stabsize, stabstrsize;
605
606 info = (struct coff_symfile_info *) objfile -> sym_private;
607 dbxinfo = (struct dbx_symfile_info *) objfile->sym_stab_info;
608 symfile_bfd = abfd; /* Kludge for swap routines */
609
610 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
611 desc = fileno ((GDB_FILE *)(abfd->iostream)); /* File descriptor */
612 num_symbols = bfd_get_symcount (abfd); /* How many syms */
613 symtab_offset = cdata->sym_filepos; /* Symbol table file offset */
614 stringtab_offset = symtab_offset + /* String table file offset */
615 num_symbols * cdata->local_symesz;
616
617 /* Set a few file-statics that give us specific information about
618 the particular COFF file format we're reading. */
619 local_linesz = cdata->local_linesz;
620 local_n_btmask = cdata->local_n_btmask;
621 local_n_btshft = cdata->local_n_btshft;
622 local_n_tmask = cdata->local_n_tmask;
623 local_n_tshift = cdata->local_n_tshift;
624 local_linesz = cdata->local_linesz;
625 local_symesz = cdata->local_symesz;
626 local_auxesz = cdata->local_auxesz;
627
628 /* Allocate space for raw symbol and aux entries, based on their
629 space requirements as reported by BFD. */
630 temp_sym = (char *) xmalloc
631 (cdata->local_symesz + cdata->local_auxesz);
632 temp_aux = temp_sym + cdata->local_symesz;
633 back_to = make_cleanup (free_current_contents, &temp_sym);
634 /* End of warning */
635
636 /* Read the line number table, all at once. */
637 info->min_lineno_offset = 0;
638 info->max_lineno_offset = 0;
639 bfd_map_over_sections (abfd, find_linenos, (PTR) info);
640
641 make_cleanup (free_linetab, 0);
642 val = init_lineno (desc, info->min_lineno_offset,
643 info->max_lineno_offset - info->min_lineno_offset);
644 if (val < 0)
645 error ("\"%s\": error reading line numbers\n", name);
646
647 /* Now read the string table, all at once. */
648
649 make_cleanup (free_stringtab, 0);
650 val = init_stringtab (desc, stringtab_offset);
651 if (val < 0)
652 error ("\"%s\": can't get string table", name);
653
654 init_minimal_symbol_collection ();
655 make_cleanup (discard_minimal_symbols, 0);
656
657 /* Now that the executable file is positioned at symbol table,
658 process it and define symbols accordingly. */
659
660 read_coff_symtab ((long)symtab_offset, num_symbols, objfile);
661
662 /* Sort symbols alphabetically within each block. */
663
664 {
665 struct symtab *s;
666 for (s = objfile -> symtabs; s != NULL; s = s -> next)
667 {
668 sort_symtab_syms (s);
669 }
670 }
671
672 /* Install any minimal symbols that have been collected as the current
673 minimal symbols for this objfile. */
674
675 install_minimal_symbols (objfile);
676
677 bfd_map_over_sections (abfd, coff_locate_sections, (PTR) info);
678
679 if (info->stabsect)
680 {
681 /* FIXME: dubious. Why can't we use something normal like
682 bfd_get_section_contents? */
683 fseek ((GDB_FILE *) abfd->iostream, abfd->where, 0);
684
685 stabsize = bfd_section_size (abfd, info->stabsect);
686 stabstrsize = bfd_section_size (abfd, info->stabstrsect);
687
688 coffstab_build_psymtabs (objfile,
689 section_offsets,
690 mainline,
691 info->stabsect->filepos, stabsize,
692 info->stabstrsect->filepos, stabstrsize);
693 }
694
695 do_cleanups (back_to);
696 }
697
698 static void
699 coff_new_init (ignore)
700 struct objfile *ignore;
701 {
702 }
703
704 /* Perform any local cleanups required when we are done with a particular
705 objfile. I.E, we are in the process of discarding all symbol information
706 for an objfile, freeing up all memory held for it, and unlinking the
707 objfile struct from the global list of known objfiles. */
708
709 static void
710 coff_symfile_finish (objfile)
711 struct objfile *objfile;
712 {
713 if (objfile -> sym_private != NULL)
714 {
715 mfree (objfile -> md, objfile -> sym_private);
716 }
717 }
718
719 \f
720 /* Given pointers to a symbol table in coff style exec file,
721 analyze them and create struct symtab's describing the symbols.
722 NSYMS is the number of symbols in the symbol table.
723 We read them one at a time using read_one_sym (). */
724
725 static void
726 read_coff_symtab (symtab_offset, nsyms, objfile)
727 long symtab_offset;
728 int nsyms;
729 struct objfile *objfile;
730 {
731 GDB_FILE *stream;
732 register struct context_stack *new;
733 struct coff_symbol coff_symbol;
734 register struct coff_symbol *cs = &coff_symbol;
735 static struct internal_syment main_sym;
736 static union internal_auxent main_aux;
737 struct coff_symbol fcn_cs_saved;
738 static struct internal_syment fcn_sym_saved;
739 static union internal_auxent fcn_aux_saved;
740 struct symtab *s;
741
742 /* A .file is open. */
743 int in_source_file = 0;
744 int next_file_symnum = -1;
745
746 /* Name of the current file. */
747 char *filestring = "";
748 int depth = 0;
749 int fcn_first_line = 0;
750 int fcn_last_line = 0;
751 int fcn_start_addr = 0;
752 long fcn_line_ptr = 0;
753 int val;
754
755 stream = bfd_cache_lookup(objfile->obfd);
756 if (!stream)
757 perror_with_name(objfile->name);
758
759 /* Work around a stdio bug in SunOS4.1.1 (this makes me nervous....
760 it's hard to know I've really worked around it. The fix should be
761 harmless, anyway). The symptom of the bug is that the first
762 fread (in read_one_sym), will (in my example) actually get data
763 from file offset 268, when the fseek was to 264 (and ftell shows
764 264). This causes all hell to break loose. I was unable to
765 reproduce this on a short test program which operated on the same
766 file, performing (I think) the same sequence of operations.
767
768 It stopped happening when I put in this rewind().
769
770 FIXME: Find out if this has been reported to Sun, whether it has
771 been fixed in a later release, etc. */
772
773 rewind (stream);
774
775 /* Position to read the symbol table. */
776 val = fseek (stream, (long)symtab_offset, 0);
777 if (val < 0)
778 perror_with_name (objfile->name);
779
780 current_objfile = objfile;
781 nlist_stream_global = stream;
782 nlist_nsyms_global = nsyms;
783 last_source_file = NULL;
784 memset (opaque_type_chain, 0, sizeof opaque_type_chain);
785
786 if (type_vector) /* Get rid of previous one */
787 free ((PTR)type_vector);
788 type_vector_length = 160;
789 type_vector = (struct type **)
790 xmalloc (type_vector_length * sizeof (struct type *));
791 memset (type_vector, 0, type_vector_length * sizeof (struct type *));
792
793 coff_start_symtab ();
794
795 symnum = 0;
796 while (symnum < nsyms)
797 {
798 QUIT; /* Make this command interruptable. */
799 read_one_sym (cs, &main_sym, &main_aux);
800
801 #ifdef SEM
802 temp_sem_val = cs->c_name[0] << 24 | cs->c_name[1] << 16 |
803 cs->c_name[2] << 8 | cs->c_name[3];
804 if (int_sem_val == temp_sem_val)
805 last_coffsem = (int) strtol (cs->c_name+4, (char **) NULL, 10);
806 #endif
807
808 if (cs->c_symnum == next_file_symnum && cs->c_sclass != C_FILE)
809 {
810 if (last_source_file)
811 coff_end_symtab (objfile);
812
813 coff_start_symtab ();
814 complete_symtab ("_globals_", 0, 0);
815 /* done with all files, everything from here on out is globals */
816 }
817
818 /* Special case for file with type declarations only, no text. */
819 if (!last_source_file && SDB_TYPE (cs->c_type)
820 && cs->c_secnum == N_DEBUG)
821 complete_symtab (filestring, 0, 0);
822
823 /* Typedefs should not be treated as symbol definitions. */
824 if (ISFCN (cs->c_type) && cs->c_sclass != C_TPDEF)
825 {
826 /* Record all functions -- external and static -- in minsyms. */
827 record_minimal_symbol (cs->c_name, cs->c_value, mst_text, objfile);
828
829 fcn_line_ptr = main_aux.x_sym.x_fcnary.x_fcn.x_lnnoptr;
830 fcn_start_addr = cs->c_value;
831 fcn_cs_saved = *cs;
832 fcn_sym_saved = main_sym;
833 fcn_aux_saved = main_aux;
834 continue;
835 }
836
837 switch (cs->c_sclass)
838 {
839 case C_EFCN:
840 case C_EXTDEF:
841 case C_ULABEL:
842 case C_USTATIC:
843 case C_LINE:
844 case C_ALIAS:
845 case C_HIDDEN:
846 complain (&bad_sclass_complaint, cs->c_name);
847 break;
848
849 case C_FILE:
850 /*
851 * c_value field contains symnum of next .file entry in table
852 * or symnum of first global after last .file.
853 */
854 next_file_symnum = cs->c_value;
855 if (cs->c_naux > 0)
856 filestring = coff_getfilename (&main_aux);
857 else
858 filestring = "";
859
860 /*
861 * Complete symbol table for last object file
862 * containing debugging information.
863 */
864 if (last_source_file)
865 {
866 coff_end_symtab (objfile);
867 coff_start_symtab ();
868 }
869 in_source_file = 1;
870 break;
871
872 case C_STAT:
873 if (cs->c_name[0] == '.') {
874 if (STREQ (cs->c_name, ".text")) {
875 /* FIXME: don't wire in ".text" as section name
876 or symbol name! */
877 /* Check for in_source_file deals with case of
878 a file with debugging symbols
879 followed by a later file with no symbols. */
880 if (in_source_file)
881 complete_symtab (filestring, cs->c_value,
882 main_aux.x_scn.x_scnlen);
883 in_source_file = 0;
884 }
885 /* flush rest of '.' symbols */
886 break;
887 }
888 else if (!SDB_TYPE (cs->c_type)
889 && cs->c_name[0] == 'L'
890 && (strncmp (cs->c_name, "LI%", 3) == 0
891 || strncmp (cs->c_name, "LF%", 3) == 0
892 || strncmp (cs->c_name,"LC%",3) == 0
893 || strncmp (cs->c_name,"LP%",3) == 0
894 || strncmp (cs->c_name,"LPB%",4) == 0
895 || strncmp (cs->c_name,"LBB%",4) == 0
896 || strncmp (cs->c_name,"LBE%",4) == 0
897 || strncmp (cs->c_name,"LPBX%",5) == 0))
898 /* At least on a 3b1, gcc generates swbeg and string labels
899 that look like this. Ignore them. */
900 break;
901 /* fall in for static symbols that don't start with '.' */
902 case C_EXT:
903 /* Record external symbols in minsyms if we don't have debug
904 info for them. FIXME, this is probably the wrong thing
905 to do. Why don't we record them even if we do have
906 debug symbol info? What really belongs in the minsyms
907 anyway? Fred!?? */
908 if (!SDB_TYPE (cs->c_type)) {
909 /* FIXME: This is BOGUS Will Robinson!
910 Coff should provide the SEC_CODE flag for executable sections,
911 then if we could look up sections by section number we
912 could see if the flags indicate SEC_CODE. If so, then
913 record this symbol as a function in the minimal symbol table.
914 But why are absolute syms recorded as functions, anyway? */
915 if (cs->c_secnum <= text_bfd_scnum+1) {/* text or abs */
916 record_minimal_symbol (cs->c_name, cs->c_value,
917 mst_text, objfile);
918 break;
919 } else {
920 record_minimal_symbol (cs->c_name, cs->c_value,
921 mst_data, objfile);
922 break;
923 }
924 }
925 process_coff_symbol (cs, &main_aux, objfile);
926 break;
927
928 case C_FCN:
929 if (STREQ (cs->c_name, ".bf"))
930 {
931 within_function = 1;
932
933 /* value contains address of first non-init type code */
934 /* main_aux.x_sym.x_misc.x_lnsz.x_lnno
935 contains line number of '{' } */
936 if (cs->c_naux != 1)
937 complain (&bf_no_aux_complaint, cs->c_symnum);
938 fcn_first_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
939
940 /* Might want to check that locals are 0 and
941 context_stack_depth is zero, and complain if not. */
942
943 depth = 0;
944 new = push_context (depth, fcn_start_addr);
945 fcn_cs_saved.c_name = getsymname (&fcn_sym_saved);
946 new->name = process_coff_symbol (&fcn_cs_saved,
947 &fcn_aux_saved, objfile);
948 }
949 else if (STREQ (cs->c_name, ".ef"))
950 {
951 /* the value of .ef is the address of epilogue code;
952 not useful for gdb. */
953 /* { main_aux.x_sym.x_misc.x_lnsz.x_lnno
954 contains number of lines to '}' */
955 new = pop_context ();
956 /* Stack must be empty now. */
957 if (context_stack_depth > 0 || new == NULL)
958 {
959 complain (&ef_complaint, cs->c_symnum);
960 within_function = 0;
961 break;
962 }
963 if (cs->c_naux != 1)
964 {
965 complain (&ef_no_aux_complaint, cs->c_symnum);
966 fcn_last_line = 0x7FFFFFFF;
967 }
968 else
969 {
970 fcn_last_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
971 }
972 enter_linenos (fcn_line_ptr, fcn_first_line, fcn_last_line);
973
974 finish_block (new->name, &local_symbols, new->old_blocks,
975 new->start_addr,
976 #if defined (FUNCTION_EPILOGUE_SIZE)
977 /* This macro should be defined only on
978 machines where the
979 fcn_aux_saved.x_sym.x_misc.x_fsize
980 field is always zero.
981 So use the .bf record information that
982 points to the epilogue and add the size
983 of the epilogue. */
984 cs->c_value + FUNCTION_EPILOGUE_SIZE,
985 #else
986 fcn_cs_saved.c_value +
987 fcn_aux_saved.x_sym.x_misc.x_fsize,
988 #endif
989 objfile
990 );
991 within_function = 0;
992 }
993 break;
994
995 case C_BLOCK:
996 if (STREQ (cs->c_name, ".bb"))
997 {
998 push_context (++depth, cs->c_value);
999 }
1000 else if (STREQ (cs->c_name, ".eb"))
1001 {
1002 new = pop_context ();
1003 if (depth-- != new->depth)
1004 {
1005 complain (&eb_complaint, symnum);
1006 break;
1007 }
1008 if (local_symbols && context_stack_depth > 0)
1009 {
1010 /* Make a block for the local symbols within. */
1011 finish_block (0, &local_symbols, new->old_blocks,
1012 new->start_addr, cs->c_value, objfile);
1013 }
1014 /* Now pop locals of block just finished. */
1015 local_symbols = new->locals;
1016 }
1017 break;
1018
1019 default:
1020 process_coff_symbol (cs, &main_aux, objfile);
1021 break;
1022 }
1023 }
1024
1025 if (last_source_file)
1026 coff_end_symtab (objfile);
1027
1028 /* Patch up any opaque types (references to types that are not defined
1029 in the file where they are referenced, e.g. "struct foo *bar"). */
1030 ALL_OBJFILE_SYMTABS (objfile, s)
1031 patch_opaque_types (s);
1032
1033 current_objfile = NULL;
1034 }
1035 \f
1036 /* Routines for reading headers and symbols from executable. */
1037
1038 #ifdef FIXME
1039 /* Move these XXXMAGIC symbol defns into BFD! */
1040
1041 /* Read COFF file header, check magic number,
1042 and return number of symbols. */
1043 read_file_hdr (chan, file_hdr)
1044 int chan;
1045 FILHDR *file_hdr;
1046 {
1047 lseek (chan, 0L, 0);
1048 if (myread (chan, (char *)file_hdr, FILHSZ) < 0)
1049 return -1;
1050
1051 switch (file_hdr->f_magic)
1052 {
1053 #ifdef MC68MAGIC
1054 case MC68MAGIC:
1055 #endif
1056 #ifdef NS32GMAGIC
1057 case NS32GMAGIC:
1058 case NS32SMAGIC:
1059 #endif
1060 #ifdef I386MAGIC
1061 case I386MAGIC:
1062 #endif
1063 #ifdef CLIPPERMAGIC
1064 case CLIPPERMAGIC:
1065 #endif
1066 #if defined (MC68KWRMAGIC) \
1067 && (!defined (MC68MAGIC) || MC68KWRMAGIC != MC68MAGIC)
1068 case MC68KWRMAGIC:
1069 #endif
1070 #ifdef MC68KROMAGIC
1071 case MC68KROMAGIC:
1072 case MC68KPGMAGIC:
1073 #endif
1074 #ifdef MC88DGMAGIC
1075 case MC88DGMAGIC:
1076 #endif
1077 #ifdef MC88MAGIC
1078 case MC88MAGIC:
1079 #endif
1080 #ifdef I960ROMAGIC
1081 case I960ROMAGIC: /* Intel 960 */
1082 #endif
1083 #ifdef I960RWMAGIC
1084 case I960RWMAGIC: /* Intel 960 */
1085 #endif
1086 return file_hdr->f_nsyms;
1087
1088 default:
1089 #ifdef BADMAG
1090 if (BADMAG(file_hdr))
1091 return -1;
1092 else
1093 return file_hdr->f_nsyms;
1094 #else
1095 return -1;
1096 #endif
1097 }
1098 }
1099 #endif
1100
1101 /* Read the next symbol, swap it, and return it in both internal_syment
1102 form, and coff_symbol form. Also return its first auxent, if any,
1103 in internal_auxent form, and skip any other auxents. */
1104
1105 static void
1106 read_one_sym (cs, sym, aux)
1107 register struct coff_symbol *cs;
1108 register struct internal_syment *sym;
1109 register union internal_auxent *aux;
1110 {
1111 int i;
1112
1113 cs->c_symnum = symnum;
1114 fread (temp_sym, local_symesz, 1, nlist_stream_global);
1115 bfd_coff_swap_sym_in (symfile_bfd, temp_sym, (char *)sym);
1116 cs->c_naux = sym->n_numaux & 0xff;
1117 if (cs->c_naux >= 1)
1118 {
1119 fread (temp_aux, local_auxesz, 1, nlist_stream_global);
1120 bfd_coff_swap_aux_in (symfile_bfd, temp_aux, sym->n_type, sym->n_sclass,
1121 (char *)aux);
1122 /* If more than one aux entry, read past it (only the first aux
1123 is important). */
1124 for (i = 1; i < cs->c_naux; i++)
1125 fread (temp_aux, local_auxesz, 1, nlist_stream_global);
1126 }
1127 cs->c_name = getsymname (sym);
1128 cs->c_value = sym->n_value;
1129 cs->c_sclass = (sym->n_sclass & 0xff);
1130 cs->c_secnum = sym->n_scnum;
1131 cs->c_type = (unsigned) sym->n_type;
1132 if (!SDB_TYPE (cs->c_type))
1133 cs->c_type = 0;
1134
1135 symnum += 1 + cs->c_naux;
1136 }
1137 \f
1138 /* Support for string table handling */
1139
1140 static char *stringtab = NULL;
1141
1142 static int
1143 init_stringtab (chan, offset)
1144 int chan;
1145 long offset;
1146 {
1147 long length;
1148 int val;
1149 unsigned char lengthbuf[4];
1150
1151 free_stringtab ();
1152
1153 if (lseek (chan, offset, 0) < 0)
1154 return -1;
1155
1156 val = myread (chan, (char *)lengthbuf, sizeof lengthbuf);
1157 length = bfd_h_get_32 (symfile_bfd, lengthbuf);
1158
1159 /* If no string table is needed, then the file may end immediately
1160 after the symbols. Just return with `stringtab' set to null. */
1161 if (val != sizeof lengthbuf || length < sizeof lengthbuf)
1162 return 0;
1163
1164 stringtab = (char *) xmalloc (length);
1165 memcpy (stringtab, &length, sizeof length);
1166 if (length == sizeof length) /* Empty table -- just the count */
1167 return 0;
1168
1169 val = myread (chan, stringtab + sizeof lengthbuf, length - sizeof lengthbuf);
1170 if (val != length - sizeof lengthbuf || stringtab[length - 1] != '\0')
1171 return -1;
1172
1173 return 0;
1174 }
1175
1176 static void
1177 free_stringtab ()
1178 {
1179 if (stringtab)
1180 free (stringtab);
1181 stringtab = NULL;
1182 }
1183
1184 static char *
1185 getsymname (symbol_entry)
1186 struct internal_syment *symbol_entry;
1187 {
1188 static char buffer[SYMNMLEN+1];
1189 char *result;
1190
1191 if (symbol_entry->_n._n_n._n_zeroes == 0)
1192 {
1193 result = stringtab + symbol_entry->_n._n_n._n_offset;
1194 }
1195 else
1196 {
1197 strncpy (buffer, symbol_entry->_n._n_name, SYMNMLEN);
1198 buffer[SYMNMLEN] = '\0';
1199 result = buffer;
1200 }
1201 return result;
1202 }
1203
1204 /* Extract the file name from the aux entry of a C_FILE symbol. Return
1205 only the last component of the name. Result is in static storage and
1206 is only good for temporary use. */
1207
1208 char *
1209 coff_getfilename (aux_entry)
1210 union internal_auxent *aux_entry;
1211 {
1212 static char buffer[BUFSIZ];
1213 register char *temp;
1214 char *result;
1215
1216 if (aux_entry->x_file.x_n.x_zeroes == 0)
1217 strcpy (buffer, stringtab + aux_entry->x_file.x_n.x_offset);
1218 else
1219 {
1220 strncpy (buffer, aux_entry->x_file.x_fname, FILNMLEN);
1221 buffer[FILNMLEN] = '\0';
1222 }
1223 result = buffer;
1224 if ((temp = strrchr (result, '/')) != NULL)
1225 result = temp + 1;
1226 return (result);
1227 }
1228 \f
1229 /* Support for line number handling */
1230 static char *linetab = NULL;
1231 static long linetab_offset;
1232 static unsigned long linetab_size;
1233
1234 /* Read in all the line numbers for fast lookups later. Leave them in
1235 external (unswapped) format in memory; we'll swap them as we enter
1236 them into GDB's data structures. */
1237
1238 static int
1239 init_lineno (chan, offset, size)
1240 int chan;
1241 long offset;
1242 int size;
1243 {
1244 int val;
1245
1246 linetab_offset = offset;
1247 linetab_size = size;
1248
1249 free_linetab();
1250
1251 if (size == 0)
1252 return 0;
1253
1254 if (lseek (chan, offset, 0) < 0)
1255 return -1;
1256
1257 /* Allocate the desired table, plus a sentinel */
1258 linetab = (char *) xmalloc (size + local_linesz);
1259
1260 val = myread (chan, linetab, size);
1261 if (val != size)
1262 return -1;
1263
1264 /* Terminate it with an all-zero sentinel record */
1265 memset (linetab + size, 0, local_linesz);
1266
1267 return 0;
1268 }
1269
1270 static void
1271 free_linetab ()
1272 {
1273 if (linetab)
1274 free (linetab);
1275 linetab = NULL;
1276 }
1277
1278 #if !defined (L_LNNO32)
1279 #define L_LNNO32(lp) ((lp)->l_lnno)
1280 #endif
1281
1282 static void
1283 enter_linenos (file_offset, first_line, last_line)
1284 long file_offset;
1285 register int first_line;
1286 register int last_line;
1287 {
1288 register char *rawptr;
1289 struct internal_lineno lptr;
1290
1291 if (file_offset < linetab_offset)
1292 {
1293 complain (&lineno_complaint, file_offset);
1294 if (file_offset > linetab_size) /* Too big to be an offset? */
1295 return;
1296 file_offset += linetab_offset; /* Try reading at that linetab offset */
1297 }
1298
1299 rawptr = &linetab[file_offset - linetab_offset];
1300
1301 /* skip first line entry for each function */
1302 rawptr += local_linesz;
1303 /* line numbers start at one for the first line of the function */
1304 first_line--;
1305
1306 for (;;) {
1307 bfd_coff_swap_lineno_in (symfile_bfd, rawptr, &lptr);
1308 rawptr += local_linesz;
1309 /* The next function, or the sentinel, will have L_LNNO32 zero; we exit. */
1310 if (L_LNNO32 (&lptr) && L_LNNO32 (&lptr) <= last_line)
1311 coff_record_line (first_line + L_LNNO32 (&lptr), lptr.l_addr.l_paddr);
1312 else
1313 break;
1314 }
1315 }
1316 \f
1317 static void
1318 patch_type (type, real_type)
1319 struct type *type;
1320 struct type *real_type;
1321 {
1322 register struct type *target = TYPE_TARGET_TYPE (type);
1323 register struct type *real_target = TYPE_TARGET_TYPE (real_type);
1324 int field_size = TYPE_NFIELDS (real_target) * sizeof (struct field);
1325
1326 TYPE_LENGTH (target) = TYPE_LENGTH (real_target);
1327 TYPE_NFIELDS (target) = TYPE_NFIELDS (real_target);
1328 TYPE_FIELDS (target) = (struct field *) TYPE_ALLOC (target, field_size);
1329
1330 memcpy (TYPE_FIELDS (target), TYPE_FIELDS (real_target), field_size);
1331
1332 if (TYPE_NAME (real_target))
1333 {
1334 if (TYPE_NAME (target))
1335 free (TYPE_NAME (target));
1336 TYPE_NAME (target) = concat (TYPE_NAME (real_target), NULL);
1337 }
1338 }
1339
1340 /* Patch up all appropriate typedef symbols in the opaque_type_chains
1341 so that they can be used to print out opaque data structures properly. */
1342
1343 static void
1344 patch_opaque_types (s)
1345 struct symtab *s;
1346 {
1347 register struct block *b;
1348 register int i;
1349 register struct symbol *real_sym;
1350
1351 /* Go through the per-file symbols only */
1352 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
1353 for (i = BLOCK_NSYMS (b) - 1; i >= 0; i--)
1354 {
1355 /* Find completed typedefs to use to fix opaque ones.
1356 Remove syms from the chain when their types are stored,
1357 but search the whole chain, as there may be several syms
1358 from different files with the same name. */
1359 real_sym = BLOCK_SYM (b, i);
1360 if (SYMBOL_CLASS (real_sym) == LOC_TYPEDEF &&
1361 SYMBOL_NAMESPACE (real_sym) == VAR_NAMESPACE &&
1362 TYPE_CODE (SYMBOL_TYPE (real_sym)) == TYPE_CODE_PTR &&
1363 TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (real_sym))) != 0)
1364 {
1365 register char *name = SYMBOL_NAME (real_sym);
1366 register int hash = hashname (name);
1367 register struct symbol *sym, *prev;
1368
1369 prev = 0;
1370 for (sym = opaque_type_chain[hash]; sym;)
1371 {
1372 if (name[0] == SYMBOL_NAME (sym)[0] &&
1373 STREQ (name + 1, SYMBOL_NAME (sym) + 1))
1374 {
1375 if (prev)
1376 {
1377 SYMBOL_VALUE_CHAIN (prev) = SYMBOL_VALUE_CHAIN (sym);
1378 }
1379 else
1380 {
1381 opaque_type_chain[hash] = SYMBOL_VALUE_CHAIN (sym);
1382 }
1383
1384 patch_type (SYMBOL_TYPE (sym), SYMBOL_TYPE (real_sym));
1385
1386 if (prev)
1387 {
1388 sym = SYMBOL_VALUE_CHAIN (prev);
1389 }
1390 else
1391 {
1392 sym = opaque_type_chain[hash];
1393 }
1394 }
1395 else
1396 {
1397 prev = sym;
1398 sym = SYMBOL_VALUE_CHAIN (sym);
1399 }
1400 }
1401 }
1402 }
1403 }
1404 \f
1405 static struct symbol *
1406 process_coff_symbol (cs, aux, objfile)
1407 register struct coff_symbol *cs;
1408 register union internal_auxent *aux;
1409 struct objfile *objfile;
1410 {
1411 register struct symbol *sym
1412 = (struct symbol *) obstack_alloc (&objfile->symbol_obstack,
1413 sizeof (struct symbol));
1414 char *name;
1415 struct type *temptype;
1416
1417 memset (sym, 0, sizeof (struct symbol));
1418 name = cs->c_name;
1419 name = EXTERNAL_NAME (name, objfile->obfd);
1420 SYMBOL_NAME (sym) = obstack_copy0 (&objfile->symbol_obstack, name,
1421 strlen (name));
1422
1423 /* default assumptions */
1424 SYMBOL_VALUE (sym) = cs->c_value;
1425 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1426
1427 if (ISFCN (cs->c_type))
1428 {
1429 #if 0
1430 /* FIXME: This has NOT been tested. The DBX version has.. */
1431 /* Generate a template for the type of this function. The
1432 types of the arguments will be added as we read the symbol
1433 table. */
1434 struct type *new = (struct type *)
1435 obstack_alloc (&objfile->symbol_obstack, sizeof (struct type));
1436
1437 memcpy (new, lookup_function_type (decode_function_type (cs, cs->c_type, aux)),
1438 sizeof(struct type));
1439 SYMBOL_TYPE (sym) = new;
1440 in_function_type = SYMBOL_TYPE(sym);
1441 #else
1442 SYMBOL_TYPE(sym) =
1443 lookup_function_type (decode_function_type (cs, cs->c_type, aux));
1444 #endif
1445
1446 SYMBOL_CLASS (sym) = LOC_BLOCK;
1447 if (cs->c_sclass == C_STAT)
1448 add_symbol_to_list (sym, &file_symbols);
1449 else if (cs->c_sclass == C_EXT)
1450 add_symbol_to_list (sym, &global_symbols);
1451 }
1452 else
1453 {
1454 SYMBOL_TYPE (sym) = decode_type (cs, cs->c_type, aux);
1455 switch (cs->c_sclass)
1456 {
1457 case C_NULL:
1458 break;
1459
1460 case C_AUTO:
1461 SYMBOL_CLASS (sym) = LOC_LOCAL;
1462 add_symbol_to_list (sym, &local_symbols);
1463 break;
1464
1465 case C_EXT:
1466 SYMBOL_CLASS (sym) = LOC_STATIC;
1467 SYMBOL_VALUE_ADDRESS (sym) = (CORE_ADDR) cs->c_value;
1468 add_symbol_to_list (sym, &global_symbols);
1469 break;
1470
1471 case C_STAT:
1472 SYMBOL_CLASS (sym) = LOC_STATIC;
1473 SYMBOL_VALUE_ADDRESS (sym) = (CORE_ADDR) cs->c_value;
1474 if (within_function) {
1475 /* Static symbol of local scope */
1476 add_symbol_to_list (sym, &local_symbols);
1477 }
1478 else {
1479 /* Static symbol at top level of file */
1480 add_symbol_to_list (sym, &file_symbols);
1481 }
1482 break;
1483
1484 #ifdef C_GLBLREG /* AMD coff */
1485 case C_GLBLREG:
1486 #endif
1487 case C_REG:
1488 SYMBOL_CLASS (sym) = LOC_REGISTER;
1489 SYMBOL_VALUE (sym) = SDB_REG_TO_REGNUM(cs->c_value);
1490 add_symbol_to_list (sym, &local_symbols);
1491 break;
1492
1493 case C_LABEL:
1494 break;
1495
1496 case C_ARG:
1497 SYMBOL_CLASS (sym) = LOC_ARG;
1498 #if 0
1499 /* FIXME: This has not been tested. */
1500 /* Add parameter to function. */
1501 add_param_to_type(&in_function_type,sym);
1502 #endif
1503 add_symbol_to_list (sym, &local_symbols);
1504 #if !defined (BELIEVE_PCC_PROMOTION) && (TARGET_BYTE_ORDER == BIG_ENDIAN)
1505 /* If PCC says a parameter is a short or a char,
1506 aligned on an int boundary, realign it to the "little end"
1507 of the int. */
1508 temptype = lookup_fundamental_type (current_objfile, FT_INTEGER);
1509 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) < TYPE_LENGTH (temptype)
1510 && TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_INT
1511 && 0 == SYMBOL_VALUE (sym) % TYPE_LENGTH (temptype))
1512 {
1513 SYMBOL_VALUE (sym) += TYPE_LENGTH (temptype)
1514 - TYPE_LENGTH (SYMBOL_TYPE (sym));
1515 }
1516 #endif
1517 break;
1518
1519 case C_REGPARM:
1520 SYMBOL_CLASS (sym) = LOC_REGPARM;
1521 SYMBOL_VALUE (sym) = SDB_REG_TO_REGNUM(cs->c_value);
1522 add_symbol_to_list (sym, &local_symbols);
1523 #if !defined (BELIEVE_PCC_PROMOTION)
1524 /* FIXME: This should retain the current type, since it's just
1525 a register value. gnu@adobe, 26Feb93 */
1526 /* If PCC says a parameter is a short or a char,
1527 it is really an int. */
1528 temptype = lookup_fundamental_type (current_objfile, FT_INTEGER);
1529 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) < TYPE_LENGTH (temptype)
1530 && TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_INT)
1531 {
1532 SYMBOL_TYPE (sym) = TYPE_UNSIGNED (SYMBOL_TYPE (sym))
1533 ? lookup_fundamental_type (current_objfile,
1534 FT_UNSIGNED_INTEGER)
1535 : temptype;
1536 }
1537 #endif
1538 break;
1539
1540 case C_TPDEF:
1541 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
1542 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1543
1544 /* If type has no name, give it one */
1545 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
1546 TYPE_NAME (SYMBOL_TYPE (sym)) = concat (SYMBOL_NAME (sym), NULL);
1547
1548 /* Keep track of any type which points to empty structured type,
1549 so it can be filled from a definition from another file. A
1550 simple forward reference (TYPE_CODE_UNDEF) is not an
1551 empty structured type, though; the forward references
1552 work themselves out via the magic of coff_lookup_type. */
1553 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_PTR &&
1554 TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym))) == 0 &&
1555 TYPE_CODE (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym))) !=
1556 TYPE_CODE_UNDEF)
1557 {
1558 register int i = hashname (SYMBOL_NAME (sym));
1559
1560 SYMBOL_VALUE_CHAIN (sym) = opaque_type_chain[i];
1561 opaque_type_chain[i] = sym;
1562 }
1563 add_symbol_to_list (sym, &file_symbols);
1564 break;
1565
1566 case C_STRTAG:
1567 case C_UNTAG:
1568 case C_ENTAG:
1569 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
1570 SYMBOL_NAMESPACE (sym) = STRUCT_NAMESPACE;
1571
1572 /* Some compilers try to be helpful by inventing "fake"
1573 names for anonymous enums, structures, and unions, like
1574 "~0fake" or ".0fake". Thanks, but no thanks... */
1575 if (TYPE_TAG_NAME (SYMBOL_TYPE (sym)) == 0)
1576 if (SYMBOL_NAME(sym) != NULL
1577 && *SYMBOL_NAME(sym) != '~'
1578 && *SYMBOL_NAME(sym) != '.')
1579 TYPE_TAG_NAME (SYMBOL_TYPE (sym)) =
1580 concat (SYMBOL_NAME (sym), NULL);
1581
1582 add_symbol_to_list (sym, &file_symbols);
1583 break;
1584
1585 default:
1586 break;
1587 }
1588 }
1589 return sym;
1590 }
1591 \f
1592 /* Decode a coff type specifier;
1593 return the type that is meant. */
1594
1595 static
1596 struct type *
1597 decode_type (cs, c_type, aux)
1598 register struct coff_symbol *cs;
1599 unsigned int c_type;
1600 register union internal_auxent *aux;
1601 {
1602 register struct type *type = 0;
1603 unsigned int new_c_type;
1604
1605 if (c_type & ~N_BTMASK)
1606 {
1607 new_c_type = DECREF (c_type);
1608 if (ISPTR (c_type))
1609 {
1610 type = decode_type (cs, new_c_type, aux);
1611 type = lookup_pointer_type (type);
1612 }
1613 else if (ISFCN (c_type))
1614 {
1615 type = decode_type (cs, new_c_type, aux);
1616 type = lookup_function_type (type);
1617 }
1618 else if (ISARY (c_type))
1619 {
1620 int i, n;
1621 register unsigned short *dim;
1622 struct type *base_type, *index_type, *range_type;
1623
1624 /* Define an array type. */
1625 /* auxent refers to array, not base type */
1626 if (aux->x_sym.x_tagndx.l == 0)
1627 cs->c_naux = 0;
1628
1629 /* shift the indices down */
1630 dim = &aux->x_sym.x_fcnary.x_ary.x_dimen[0];
1631 i = 1;
1632 n = dim[0];
1633 for (i = 0; *dim && i < DIMNUM - 1; i++, dim++)
1634 *dim = *(dim + 1);
1635 *dim = 0;
1636
1637 base_type = decode_type (cs, new_c_type, aux);
1638 index_type = lookup_fundamental_type (current_objfile, FT_INTEGER);
1639 range_type =
1640 create_range_type ((struct type *) NULL, index_type, 0, n - 1);
1641 type =
1642 create_array_type ((struct type *) NULL, base_type, range_type);
1643 }
1644 return type;
1645 }
1646
1647 /* Reference to existing type. This only occurs with the
1648 struct, union, and enum types. EPI a29k coff
1649 fakes us out by producing aux entries with a nonzero
1650 x_tagndx for definitions of structs, unions, and enums, so we
1651 have to check the c_sclass field. SCO 3.2v4 cc gets confused
1652 with pointers to pointers to defined structs, and generates
1653 negative x_tagndx fields. */
1654 if (cs->c_naux > 0 && aux->x_sym.x_tagndx.l != 0)
1655 {
1656 if (cs->c_sclass != C_STRTAG
1657 && cs->c_sclass != C_UNTAG
1658 && cs->c_sclass != C_ENTAG
1659 && aux->x_sym.x_tagndx.l >= 0)
1660 {
1661 type = coff_alloc_type (aux->x_sym.x_tagndx.l);
1662 return type;
1663 } else {
1664 complain (&tagndx_bad_complaint, cs->c_name);
1665 /* And fall through to decode_base_type... */
1666 }
1667 }
1668
1669 return decode_base_type (cs, BTYPE (c_type), aux);
1670 }
1671
1672 /* Decode a coff type specifier for function definition;
1673 return the type that the function returns. */
1674
1675 static
1676 struct type *
1677 decode_function_type (cs, c_type, aux)
1678 register struct coff_symbol *cs;
1679 unsigned int c_type;
1680 register union internal_auxent *aux;
1681 {
1682 if (aux->x_sym.x_tagndx.l == 0)
1683 cs->c_naux = 0; /* auxent refers to function, not base type */
1684
1685 return decode_type (cs, DECREF (c_type), aux);
1686 }
1687 \f
1688 /* basic C types */
1689
1690 static
1691 struct type *
1692 decode_base_type (cs, c_type, aux)
1693 register struct coff_symbol *cs;
1694 unsigned int c_type;
1695 register union internal_auxent *aux;
1696 {
1697 struct type *type;
1698
1699 switch (c_type)
1700 {
1701 case T_NULL:
1702 /* shows up with "void (*foo)();" structure members */
1703 return lookup_fundamental_type (current_objfile, FT_VOID);
1704
1705 #if 0
1706 /* DGUX actually defines both T_ARG and T_VOID to the same value. */
1707 #ifdef T_ARG
1708 case T_ARG:
1709 /* Shows up in DGUX, I think. Not sure where. */
1710 return lookup_fundamental_type (current_objfile, FT_VOID); /* shouldn't show up here */
1711 #endif
1712 #endif /* 0 */
1713
1714 #ifdef T_VOID
1715 case T_VOID:
1716 /* Intel 960 COFF has this symbol and meaning. */
1717 return lookup_fundamental_type (current_objfile, FT_VOID);
1718 #endif
1719
1720 case T_CHAR:
1721 return lookup_fundamental_type (current_objfile, FT_CHAR);
1722
1723 case T_SHORT:
1724 return lookup_fundamental_type (current_objfile, FT_SHORT);
1725
1726 case T_INT:
1727 return lookup_fundamental_type (current_objfile, FT_INTEGER);
1728
1729 case T_LONG:
1730 return lookup_fundamental_type (current_objfile, FT_LONG);
1731
1732 case T_FLOAT:
1733 return lookup_fundamental_type (current_objfile, FT_FLOAT);
1734
1735 case T_DOUBLE:
1736 return lookup_fundamental_type (current_objfile, FT_DBL_PREC_FLOAT);
1737
1738 case T_STRUCT:
1739 if (cs->c_naux != 1)
1740 {
1741 /* anonymous structure type */
1742 type = coff_alloc_type (cs->c_symnum);
1743 TYPE_CODE (type) = TYPE_CODE_STRUCT;
1744 TYPE_NAME (type) = NULL;
1745 /* This used to set the tag to "<opaque>". But I think setting it
1746 to NULL is right, and the printing code can print it as
1747 "struct {...}". */
1748 TYPE_TAG_NAME (type) = NULL;
1749 INIT_CPLUS_SPECIFIC(type);
1750 TYPE_LENGTH (type) = 0;
1751 TYPE_FIELDS (type) = 0;
1752 TYPE_NFIELDS (type) = 0;
1753 }
1754 else
1755 {
1756 type = coff_read_struct_type (cs->c_symnum,
1757 aux->x_sym.x_misc.x_lnsz.x_size,
1758 aux->x_sym.x_fcnary.x_fcn.x_endndx.l);
1759 }
1760 return type;
1761
1762 case T_UNION:
1763 if (cs->c_naux != 1)
1764 {
1765 /* anonymous union type */
1766 type = coff_alloc_type (cs->c_symnum);
1767 TYPE_NAME (type) = NULL;
1768 /* This used to set the tag to "<opaque>". But I think setting it
1769 to NULL is right, and the printing code can print it as
1770 "union {...}". */
1771 TYPE_TAG_NAME (type) = NULL;
1772 INIT_CPLUS_SPECIFIC(type);
1773 TYPE_LENGTH (type) = 0;
1774 TYPE_FIELDS (type) = 0;
1775 TYPE_NFIELDS (type) = 0;
1776 }
1777 else
1778 {
1779 type = coff_read_struct_type (cs->c_symnum,
1780 aux->x_sym.x_misc.x_lnsz.x_size,
1781 aux->x_sym.x_fcnary.x_fcn.x_endndx.l);
1782 }
1783 TYPE_CODE (type) = TYPE_CODE_UNION;
1784 return type;
1785
1786 case T_ENUM:
1787 if (cs->c_naux != 1)
1788 {
1789 /* anonymous enum type */
1790 type = coff_alloc_type (cs->c_symnum);
1791 TYPE_CODE (type) = TYPE_CODE_ENUM;
1792 TYPE_NAME (type) = NULL;
1793 /* This used to set the tag to "<opaque>". But I think setting it
1794 to NULL is right, and the printing code can print it as
1795 "enum {...}". */
1796 TYPE_TAG_NAME (type) = NULL;
1797 TYPE_LENGTH (type) = 0;
1798 TYPE_FIELDS (type) = 0;
1799 TYPE_NFIELDS(type) = 0;
1800 }
1801 else
1802 {
1803 type = coff_read_enum_type (cs->c_symnum,
1804 aux->x_sym.x_misc.x_lnsz.x_size,
1805 aux->x_sym.x_fcnary.x_fcn.x_endndx.l);
1806 }
1807 return type;
1808
1809 case T_MOE:
1810 /* shouldn't show up here */
1811 break;
1812
1813 case T_UCHAR:
1814 return lookup_fundamental_type (current_objfile, FT_UNSIGNED_CHAR);
1815
1816 case T_USHORT:
1817 return lookup_fundamental_type (current_objfile, FT_UNSIGNED_SHORT);
1818
1819 case T_UINT:
1820 return lookup_fundamental_type (current_objfile, FT_UNSIGNED_INTEGER);
1821
1822 case T_ULONG:
1823 return lookup_fundamental_type (current_objfile, FT_UNSIGNED_LONG);
1824 }
1825 complain (&unexpected_type_complaint, cs->c_name);
1826 return lookup_fundamental_type (current_objfile, FT_VOID);
1827 }
1828 \f
1829 /* This page contains subroutines of read_type. */
1830
1831 /* Read the description of a structure (or union type)
1832 and return an object describing the type. */
1833
1834 static struct type *
1835 coff_read_struct_type (index, length, lastsym)
1836 int index;
1837 int length;
1838 int lastsym;
1839 {
1840 struct nextfield
1841 {
1842 struct nextfield *next;
1843 struct field field;
1844 };
1845
1846 register struct type *type;
1847 register struct nextfield *list = 0;
1848 struct nextfield *new;
1849 int nfields = 0;
1850 register int n;
1851 char *name;
1852 struct coff_symbol member_sym;
1853 register struct coff_symbol *ms = &member_sym;
1854 struct internal_syment sub_sym;
1855 union internal_auxent sub_aux;
1856 int done = 0;
1857
1858 type = coff_alloc_type (index);
1859 TYPE_CODE (type) = TYPE_CODE_STRUCT;
1860 INIT_CPLUS_SPECIFIC(type);
1861 TYPE_LENGTH (type) = length;
1862
1863 while (!done && symnum < lastsym && symnum < nlist_nsyms_global)
1864 {
1865 read_one_sym (ms, &sub_sym, &sub_aux);
1866 name = ms->c_name;
1867 name = EXTERNAL_NAME (name, current_objfile->obfd);
1868
1869 switch (ms->c_sclass)
1870 {
1871 case C_MOS:
1872 case C_MOU:
1873
1874 /* Get space to record the next field's data. */
1875 new = (struct nextfield *) alloca (sizeof (struct nextfield));
1876 new->next = list;
1877 list = new;
1878
1879 /* Save the data. */
1880 list->field.name = savestring (name, strlen (name));
1881 list->field.type = decode_type (ms, ms->c_type, &sub_aux);
1882 list->field.bitpos = 8 * ms->c_value;
1883 list->field.bitsize = 0;
1884 nfields++;
1885 break;
1886
1887 case C_FIELD:
1888
1889 /* Get space to record the next field's data. */
1890 new = (struct nextfield *) alloca (sizeof (struct nextfield));
1891 new->next = list;
1892 list = new;
1893
1894 /* Save the data. */
1895 list->field.name = savestring (name, strlen (name));
1896 list->field.type = decode_type (ms, ms->c_type, &sub_aux);
1897 list->field.bitpos = ms->c_value;
1898 list->field.bitsize = sub_aux.x_sym.x_misc.x_lnsz.x_size;
1899 nfields++;
1900 break;
1901
1902 case C_EOS:
1903 done = 1;
1904 break;
1905 }
1906 }
1907 /* Now create the vector of fields, and record how big it is. */
1908
1909 TYPE_NFIELDS (type) = nfields;
1910 TYPE_FIELDS (type) = (struct field *)
1911 TYPE_ALLOC (type, sizeof (struct field) * nfields);
1912
1913 /* Copy the saved-up fields into the field vector. */
1914
1915 for (n = nfields; list; list = list->next)
1916 TYPE_FIELD (type, --n) = list->field;
1917
1918 return type;
1919 }
1920 \f
1921 /* Read a definition of an enumeration type,
1922 and create and return a suitable type object.
1923 Also defines the symbols that represent the values of the type. */
1924
1925 /* ARGSUSED */
1926 static struct type *
1927 coff_read_enum_type (index, length, lastsym)
1928 int index;
1929 int length;
1930 int lastsym;
1931 {
1932 register struct symbol *sym;
1933 register struct type *type;
1934 int nsyms = 0;
1935 int done = 0;
1936 struct pending **symlist;
1937 struct coff_symbol member_sym;
1938 register struct coff_symbol *ms = &member_sym;
1939 struct internal_syment sub_sym;
1940 union internal_auxent sub_aux;
1941 struct pending *osyms, *syms;
1942 int o_nsyms;
1943 register int n;
1944 char *name;
1945
1946 type = coff_alloc_type (index);
1947 if (within_function)
1948 symlist = &local_symbols;
1949 else
1950 symlist = &file_symbols;
1951 osyms = *symlist;
1952 o_nsyms = osyms ? osyms->nsyms : 0;
1953
1954 while (!done && symnum < lastsym && symnum < nlist_nsyms_global)
1955 {
1956 read_one_sym (ms, &sub_sym, &sub_aux);
1957 name = ms->c_name;
1958 name = EXTERNAL_NAME (name, current_objfile->obfd);
1959
1960 switch (ms->c_sclass)
1961 {
1962 case C_MOE:
1963 sym = (struct symbol *) xmalloc (sizeof (struct symbol));
1964 memset (sym, 0, sizeof (struct symbol));
1965
1966 SYMBOL_NAME (sym) = savestring (name, strlen (name));
1967 SYMBOL_CLASS (sym) = LOC_CONST;
1968 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1969 SYMBOL_VALUE (sym) = ms->c_value;
1970 add_symbol_to_list (sym, symlist);
1971 nsyms++;
1972 break;
1973
1974 case C_EOS:
1975 /* Sometimes the linker (on 386/ix 2.0.2 at least) screws
1976 up the count of how many symbols to read. So stop
1977 on .eos. */
1978 done = 1;
1979 break;
1980 }
1981 }
1982
1983 /* Now fill in the fields of the type-structure. */
1984
1985 if (length > 0)
1986 TYPE_LENGTH (type) = length;
1987 else
1988 TYPE_LENGTH (type) = TARGET_INT_BIT / TARGET_CHAR_BIT; /* Assume ints */
1989 TYPE_CODE (type) = TYPE_CODE_ENUM;
1990 TYPE_NFIELDS (type) = nsyms;
1991 TYPE_FIELDS (type) = (struct field *)
1992 TYPE_ALLOC (type, sizeof (struct field) * nsyms);
1993
1994 /* Find the symbols for the values and put them into the type.
1995 The symbols can be found in the symlist that we put them on
1996 to cause them to be defined. osyms contains the old value
1997 of that symlist; everything up to there was defined by us. */
1998 /* Note that we preserve the order of the enum constants, so
1999 that in something like "enum {FOO, LAST_THING=FOO}" we print
2000 FOO, not LAST_THING. */
2001
2002 for (syms = *symlist, n = 0; syms; syms = syms->next)
2003 {
2004 int j = 0;
2005 if (syms == osyms)
2006 j = o_nsyms;
2007 for (; j < syms->nsyms; j++,n++)
2008 {
2009 struct symbol *xsym = syms->symbol[j];
2010 SYMBOL_TYPE (xsym) = type;
2011 TYPE_FIELD_NAME (type, n) = SYMBOL_NAME (xsym);
2012 TYPE_FIELD_VALUE (type, n) = 0;
2013 TYPE_FIELD_BITPOS (type, n) = SYMBOL_VALUE (xsym);
2014 TYPE_FIELD_BITSIZE (type, n) = 0;
2015 }
2016 if (syms == osyms)
2017 break;
2018 }
2019
2020 #if 0
2021 /* This screws up perfectly good C programs with enums. FIXME. */
2022 /* Is this Modula-2's BOOLEAN type? Flag it as such if so. */
2023 if(TYPE_NFIELDS(type) == 2 &&
2024 ((STREQ(TYPE_FIELD_NAME(type,0),"TRUE") &&
2025 STREQ(TYPE_FIELD_NAME(type,1),"FALSE")) ||
2026 (STREQ(TYPE_FIELD_NAME(type,1),"TRUE") &&
2027 STREQ(TYPE_FIELD_NAME(type,0),"FALSE"))))
2028 TYPE_CODE(type) = TYPE_CODE_BOOL;
2029 #endif
2030 return type;
2031 }
2032
2033 struct section_offsets *
2034 coff_symfile_offsets (objfile, addr)
2035 struct objfile *objfile;
2036 CORE_ADDR addr;
2037 {
2038 struct section_offsets *section_offsets;
2039 int i;
2040
2041 objfile->num_sections = SECT_OFF_MAX;
2042 section_offsets = (struct section_offsets *)
2043 obstack_alloc (&objfile -> psymbol_obstack,
2044 sizeof (struct section_offsets)
2045 + sizeof (section_offsets->offsets) * (SECT_OFF_MAX-1));
2046
2047 for (i = 0; i < SECT_OFF_MAX; i++)
2048 ANOFFSET (section_offsets, i) = addr;
2049
2050 return section_offsets;
2051 }
2052
2053 /* Register our ability to parse symbols for coff BFD files */
2054
2055 static struct sym_fns coff_sym_fns =
2056 {
2057 bfd_target_coff_flavour,
2058 coff_new_init, /* sym_new_init: init anything gbl to entire symtab */
2059 coff_symfile_init, /* sym_init: read initial info, setup for sym_read() */
2060 coff_symfile_read, /* sym_read: read a symbol file into symtab */
2061 coff_symfile_finish, /* sym_finish: finished with file, cleanup */
2062 coff_symfile_offsets, /* sym_offsets: xlate external to internal form */
2063 NULL /* next: pointer to next struct sym_fns */
2064 };
2065
2066 void
2067 _initialize_coffread ()
2068 {
2069 add_symtab_fns(&coff_sym_fns);
2070 }