1 /* Read coff symbol tables and convert to internal format, for GDB.
3 Copyright 1987, 1988, 1989, 1990, 1991, 1992, 1993
4 Free Software Foundation, Inc.
6 This file is part of GDB.
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.
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.
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. */
25 #include "breakpoint.h"
30 #include "complaints.h"
35 #include "libbfd.h" /* FIXME secret internal data from BFD */
36 #include "coff/internal.h" /* Internal format of COFF symbols in BFD */
37 #include "libcoff.h" /* FIXME secret internal data from BFD */
39 /* Translate an external name string into a user-visible name. */
40 #define EXTERNAL_NAME(string, abfd) \
41 (string[0] == bfd_get_symbol_leading_char(abfd)? string+1: string)
43 /* To be an sdb debug type, type must have at least a basic or primary
44 derived type. Using this rather than checking against T_NULL is
45 said to prevent core dumps if we try to operate on Michael Bloom
48 #define SDB_TYPE(type) (BTYPE(type) | (type & N_TMASK))
51 * Convert from an sdb register number to an internal gdb register number.
52 * This should be defined in tm.h, if REGISTER_NAMES is not set up
53 * to map one to one onto the sdb register numbers.
55 #ifndef SDB_REG_TO_REGNUM
56 # define SDB_REG_TO_REGNUM(value) (value)
59 /* Core address of start and end of text of current source file.
60 This comes from a ".text" symbol where x_nlinno > 0. */
62 static CORE_ADDR cur_src_start_addr;
63 static CORE_ADDR cur_src_end_addr;
65 /* Core address of the end of the first object file. */
66 static CORE_ADDR first_object_file_end;
68 /* The addresses of the symbol table stream and number of symbols
69 of the object file we are reading (as copied into core). */
71 static FILE *nlist_stream_global;
72 static int nlist_nsyms_global;
74 /* Vector of line number information. */
76 static struct linetable *line_vector;
78 /* Index of next entry to go in line_vector_index. */
80 static int line_vector_index;
82 /* Last line number recorded in the line vector. */
84 static int prev_line_number;
86 /* Number of elements allocated for line_vector currently. */
88 static int line_vector_length;
90 /* Pointers to scratch storage, used for reading raw symbols and auxents. */
92 static char *temp_sym;
93 static char *temp_aux;
95 /* Local variables that hold the shift and mask values for the
96 COFF file that we are currently reading. These come back to us
97 from BFD, and are referenced by their macro names, as well as
98 internally to the BTYPE, ISPTR, ISFCN, ISARY, ISTAG, and DECREF
99 macros from ../internalcoff.h . */
101 static unsigned local_n_btmask;
102 static unsigned local_n_btshft;
103 static unsigned local_n_tmask;
104 static unsigned local_n_tshift;
106 #define N_BTMASK local_n_btmask
107 #define N_BTSHFT local_n_btshft
108 #define N_TMASK local_n_tmask
109 #define N_TSHIFT local_n_tshift
111 /* Local variables that hold the sizes in the file of various COFF structures.
112 (We only need to know this to read them from the file -- BFD will then
113 translate the data in them, into `internal_xxx' structs in the right
114 byte order, alignment, etc.) */
116 static unsigned local_linesz;
117 static unsigned local_symesz;
118 static unsigned local_auxesz;
121 /* Chain of typedefs of pointers to empty struct/union types.
122 They are chained thru the SYMBOL_VALUE_CHAIN. */
124 static struct symbol *opaque_type_chain[HASHSIZE];
126 /* Record the symbols defined for each context in a list.
127 We don't create a struct block for the context until we
128 know how long to make it. */
132 struct coff_pending *next;
133 struct symbol *symbol;
136 /* Here are the three lists that symbols are put on. */
138 struct coff_pending *coff_file_symbols; /* static at top level, and types */
140 struct coff_pending *coff_global_symbols; /* global functions and variables */
142 struct coff_pending *coff_local_symbols; /* everything local to lexical context */
144 /* List of unclosed lexical contexts
145 (that will become blocks, eventually). */
147 struct coff_context_stack
149 struct coff_context_stack *next;
150 struct coff_pending *locals;
151 struct pending_block *old_blocks;
153 CORE_ADDR start_addr;
157 struct coff_context_stack *coff_context_stack;
159 /* Nonzero if within a function (so symbols should be local,
160 if nothing says specifically). */
165 /* The type of the function we are currently reading in. This is
166 used by define_symbol to record the type of arguments to a function. */
168 struct type *in_function_type;
171 struct pending_block *pending_blocks;
173 /* Complaints about various problems in the file being read */
175 struct complaint ef_complaint =
176 {"Unmatched .ef symbol(s) ignored starting at symnum %d", 0, 0};
178 struct complaint bf_no_aux_complaint =
179 {"`.bf' symbol %d has no aux entry", 0, 0};
181 struct complaint ef_no_aux_complaint =
182 {"`.ef' symbol %d has no aux entry", 0, 0};
184 struct complaint lineno_complaint =
185 {"Line number pointer %d lower than start of line numbers", 0, 0};
187 struct complaint unexpected_type_complaint =
188 {"Unexpected type for symbol %s", 0, 0};
190 struct complaint bad_sclass_complaint =
191 {"Bad n_sclass for symbol %s", 0, 0};
193 struct complaint misordered_blocks_complaint =
194 {"Blocks out of order at address %x", 0, 0};
196 struct complaint tagndx_bad_complaint =
197 {"Symbol table entry for %s has bad tagndx value", 0, 0};
199 struct complaint eb_complaint =
200 {"Mismatched .eb symbol ignored starting at symnum %d", 0, 0};
202 /* Simplified internal version of coff symbol table information */
206 int c_symnum; /* symbol number of this entry */
207 int c_naux; /* 0 if syment only, 1 if syment + auxent, etc */
215 coff_read_struct_type PARAMS ((int, int, int));
218 decode_base_type PARAMS ((struct coff_symbol *, unsigned int,
219 union internal_auxent *));
222 decode_type PARAMS ((struct coff_symbol *, unsigned int,
223 union internal_auxent *));
226 decode_function_type PARAMS ((struct coff_symbol *, unsigned int,
227 union internal_auxent *));
230 coff_read_enum_type PARAMS ((int, int, int));
232 static struct blockvector *
233 make_blockvector PARAMS ((struct objfile *));
235 static struct symbol *
236 process_coff_symbol PARAMS ((struct coff_symbol *, union internal_auxent *,
240 patch_opaque_types PARAMS ((struct symtab *));
243 patch_type PARAMS ((struct type *, struct type *));
246 enter_linenos PARAMS ((long, int, int));
249 init_lineno PARAMS ((int, long, int));
252 getfilename PARAMS ((union internal_auxent *));
255 getsymname PARAMS ((struct internal_syment *));
258 free_stringtab PARAMS ((void));
261 init_stringtab PARAMS ((int, long));
264 read_one_sym PARAMS ((struct coff_symbol *, struct internal_syment *,
265 union internal_auxent *));
268 read_coff_symtab PARAMS ((long, int, struct objfile *));
271 find_linenos PARAMS ((bfd *, sec_ptr, PTR));
274 coff_symfile_init PARAMS ((struct objfile *));
277 coff_new_init PARAMS ((struct objfile *));
280 coff_symfile_read PARAMS ((struct objfile *, struct section_offsets *, int));
283 coff_symfile_finish PARAMS ((struct objfile *));
286 record_minimal_symbol PARAMS ((char *, CORE_ADDR, enum minimal_symbol_type));
289 coff_end_symtab PARAMS ((struct objfile *));
292 complete_symtab PARAMS ((char *, CORE_ADDR, unsigned int));
295 coff_start_symtab PARAMS ((void));
298 coff_record_line PARAMS ((int, CORE_ADDR));
301 coff_finish_block PARAMS ((struct symbol *, struct coff_pending **,
302 struct pending_block *, CORE_ADDR, CORE_ADDR,
306 coff_add_symbol_to_list PARAMS ((struct symbol *, struct coff_pending **));
309 coff_alloc_type PARAMS ((int));
311 static struct type **
312 coff_lookup_type PARAMS ((int));
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.
319 This can be used for finding the type associated with that index
320 or for associating a new type with the index. */
322 static struct type **
323 coff_lookup_type (index)
326 if (index >= type_vector_length)
328 int old_vector_length = type_vector_length;
330 type_vector_length *= 2;
331 if (index /* is still */ >= type_vector_length) {
332 type_vector_length = index * 2;
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 *));
340 return &type_vector[index];
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. */
348 coff_alloc_type (index)
351 register struct type **type_addr = coff_lookup_type (index);
352 register struct type *type = *type_addr;
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. */
359 type = alloc_type (current_objfile);
365 /* maintain the lists of symbols and blocks */
367 /* Add a symbol to one of the lists of symbols. */
369 coff_add_symbol_to_list (symbol, listhead)
370 struct symbol *symbol;
371 struct coff_pending **listhead;
373 register struct coff_pending *link
374 = (struct coff_pending *) xmalloc (sizeof (struct coff_pending));
376 link->next = *listhead;
377 link->symbol = symbol;
381 /* Take one of the lists of symbols and make a block from it.
382 Put the block on the list of pending blocks. */
385 coff_finish_block (symbol, listhead, old_blocks, start, end, objfile)
386 struct symbol *symbol;
387 struct coff_pending **listhead;
388 struct pending_block *old_blocks;
389 CORE_ADDR start, end;
390 struct objfile *objfile;
392 register struct coff_pending *next, *next1;
393 register struct block *block;
394 register struct pending_block *pblock;
395 struct pending_block *opblock;
398 /* Count the length of the list of symbols. */
400 for (next = *listhead, i = 0; next; next = next->next, i++);
402 block = (struct block *)
403 obstack_alloc (&objfile->symbol_obstack, sizeof (struct block) + (i - 1) * sizeof (struct symbol *));
405 /* Copy the symbols into the block. */
407 BLOCK_NSYMS (block) = i;
408 for (next = *listhead; next; next = next->next)
409 BLOCK_SYM (block, --i) = next->symbol;
411 BLOCK_START (block) = start;
412 BLOCK_END (block) = end;
413 BLOCK_SUPERBLOCK (block) = 0; /* Filled in when containing block is made */
415 /* Put the block in as the value of the symbol that names it. */
419 SYMBOL_BLOCK_VALUE (symbol) = block;
420 BLOCK_FUNCTION (block) = symbol;
423 BLOCK_FUNCTION (block) = 0;
425 /* Now free the links of the list, and empty the list. */
427 for (next = *listhead; next; next = next1)
434 /* Install this block as the superblock
435 of all blocks made since the start of this scope
436 that don't have superblocks yet. */
439 for (pblock = pending_blocks; pblock != old_blocks; pblock = pblock->next)
441 if (BLOCK_SUPERBLOCK (pblock->block) == 0)
442 BLOCK_SUPERBLOCK (pblock->block) = block;
446 /* Record this block on the list of all blocks in the file.
447 Put it after opblock, or at the beginning if opblock is 0.
448 This puts the block in the list after all its subblocks. */
450 pblock = (struct pending_block *) xmalloc (sizeof (struct pending_block));
451 pblock->block = block;
454 pblock->next = opblock->next;
455 opblock->next = pblock;
459 pblock->next = pending_blocks;
460 pending_blocks = pblock;
464 static struct blockvector *
465 make_blockvector (objfile)
466 struct objfile *objfile;
468 register struct pending_block *next, *next1;
469 register struct blockvector *blockvector;
472 /* Count the length of the list of blocks. */
474 for (next = pending_blocks, i = 0; next; next = next->next, i++);
476 blockvector = (struct blockvector *)
477 obstack_alloc (&objfile->symbol_obstack, sizeof (struct blockvector) + (i - 1) * sizeof (struct block *));
479 /* Copy the blocks into the blockvector.
480 This is done in reverse order, which happens to put
481 the blocks into the proper order (ascending starting address).
482 coff_finish_block has hair to insert each block into the list
483 after its subblocks in order to make sure this is true. */
485 BLOCKVECTOR_NBLOCKS (blockvector) = i;
486 for (next = pending_blocks; next; next = next->next)
487 BLOCKVECTOR_BLOCK (blockvector, --i) = next->block;
489 /* Now free the links of the list, and empty the list. */
491 for (next = pending_blocks; next; next = next1)
501 /* Manage the vector of line numbers. */
504 coff_record_line (line, pc)
508 struct linetable_entry *e;
509 /* Make sure line vector is big enough. */
511 if (line_vector_index + 2 >= line_vector_length)
513 line_vector_length *= 2;
514 line_vector = (struct linetable *)
515 xrealloc ((char *) line_vector, sizeof (struct linetable)
516 + (line_vector_length
517 * sizeof (struct linetable_entry)));
520 e = line_vector->item + line_vector_index++;
521 e->line = line; e->pc = pc;
524 /* Start a new symtab for a new source file.
525 This is called when a COFF ".file" symbol is seen;
526 it indicates the start of data for one original source file. */
531 coff_file_symbols = 0;
532 coff_global_symbols = 0;
533 coff_context_stack = 0;
535 last_source_file = NULL;
537 /* Initialize the source file line number information for this file. */
539 if (line_vector) /* Unlikely, but maybe possible? */
540 free ((PTR)line_vector);
541 line_vector_index = 0;
542 line_vector_length = 1000;
543 prev_line_number = -2; /* Force first line number to be explicit */
544 line_vector = (struct linetable *)
545 xmalloc (sizeof (struct linetable)
546 + line_vector_length * sizeof (struct linetable_entry));
549 /* Save the vital information from when starting to read a file,
550 for use when closing off the current file.
551 NAME is the file name the symbols came from, START_ADDR is the first
552 text address for the file, and SIZE is the number of bytes of text. */
555 complete_symtab (name, start_addr, size)
557 CORE_ADDR start_addr;
560 last_source_file = savestring (name, strlen (name));
561 cur_src_start_addr = start_addr;
562 cur_src_end_addr = start_addr + size;
564 if (current_objfile -> ei.entry_point >= cur_src_start_addr &&
565 current_objfile -> ei.entry_point < cur_src_end_addr)
567 current_objfile -> ei.entry_file_lowpc = cur_src_start_addr;
568 current_objfile -> ei.entry_file_highpc = cur_src_end_addr;
572 /* Finish the symbol definitions for one main source file,
573 close off all the lexical contexts for that file
574 (creating struct block's for them), then make the
575 struct symtab for that file and put it in the list of all such. */
578 coff_end_symtab (objfile)
579 struct objfile *objfile;
581 register struct symtab *symtab;
582 register struct coff_context_stack *cstk;
583 register struct blockvector *blockvector;
584 register struct linetable *lv;
586 /* Finish the lexical context of the last function in the file. */
588 if (coff_context_stack)
590 cstk = coff_context_stack;
591 coff_context_stack = 0;
592 /* Make a block for the local symbols within. */
593 coff_finish_block (cstk->name, &coff_local_symbols, cstk->old_blocks,
594 cstk->start_addr, cur_src_end_addr, objfile);
598 /* Ignore a file that has no functions with real debugging info. */
599 if (pending_blocks == 0 && coff_file_symbols == 0 && coff_global_symbols == 0)
601 free ((PTR)line_vector);
603 line_vector_length = -1;
604 last_source_file = NULL;
608 /* It is unfortunate that in amdcoff, pending blocks might not be ordered
609 in this stage. Especially, blocks for static functions will show up at
610 the end. We need to sort them, so tools like `find_pc_function' and
611 `find_pc_block' can work reliably. */
612 if (pending_blocks) {
613 /* FIXME! Remove this horrid bubble sort and use qsort!!! */
616 struct pending_block *pb, *pbnext;
618 pb = pending_blocks, pbnext = pb->next;
623 /* swap blocks if unordered! */
625 if (BLOCK_START(pb->block) < BLOCK_START(pbnext->block)) {
626 struct block *tmp = pb->block;
627 complain (&misordered_blocks_complaint, BLOCK_START (pb->block));
628 pb->block = pbnext->block;
633 pbnext = pbnext->next;
638 /* Create the two top-level blocks for this file (STATIC_BLOCK and
640 coff_finish_block (0, &coff_file_symbols, 0, cur_src_start_addr, cur_src_end_addr, objfile);
641 coff_finish_block (0, &coff_global_symbols, 0, cur_src_start_addr, cur_src_end_addr, objfile);
643 /* Create the blockvector that points to all the file's blocks. */
644 blockvector = make_blockvector (objfile);
646 /* Now create the symtab object for this source file. */
647 symtab = allocate_symtab (last_source_file, objfile);
649 /* Fill in its components. */
650 symtab->blockvector = blockvector;
651 symtab->free_code = free_linetable;
652 symtab->free_ptr = 0;
653 symtab->filename = last_source_file;
654 symtab->dirname = NULL;
656 lv->nitems = line_vector_index;
657 symtab->linetable = (struct linetable *)
658 xrealloc ((char *) lv, (sizeof (struct linetable)
659 + lv->nitems * sizeof (struct linetable_entry)));
661 free_named_symtabs (symtab->filename);
663 /* Reinitialize for beginning of new file. */
665 line_vector_length = -1;
666 last_source_file = NULL;
670 record_minimal_symbol (name, address, type)
673 enum minimal_symbol_type type;
675 /* We don't want TDESC entry points in the minimal symbol table */
676 if (name[0] == '@') return;
678 /* mst_text isn't true, but apparently COFF doesn't tell us what it really
679 is, so this guess is more useful than mst_unknown. */
680 prim_record_minimal_symbol (savestring (name, strlen (name)),
685 /* coff_symfile_init ()
686 is the coff-specific initialization routine for reading symbols.
687 It is passed a struct objfile which contains, among other things,
688 the BFD for the file whose symbols are being read, and a slot for
689 a pointer to "private data" which we fill with cookies and other
690 treats for coff_symfile_read ().
692 We will only be called if this is a COFF or COFF-like file.
693 BFD handles figuring out the format of the file, and code in symtab.c
694 uses BFD's determination to vector to us.
696 The ultimate result is a new symtab (or, FIXME, eventually a psymtab). */
698 struct coff_symfile_info {
699 file_ptr min_lineno_offset; /* Where in file lowest line#s are */
700 file_ptr max_lineno_offset; /* 1+last byte of line#s in file */
703 static int text_bfd_scnum;
706 coff_symfile_init (objfile)
707 struct objfile *objfile;
710 bfd *abfd = objfile->obfd;
712 /* Allocate struct to keep track of the symfile */
713 objfile -> sym_private = xmmalloc (objfile -> md,
714 sizeof (struct coff_symfile_info));
716 init_entry_point_info (objfile);
718 /* Save the section number for the text section */
719 section = bfd_get_section_by_name(abfd,".text");
721 text_bfd_scnum = section->index;
726 /* This function is called for every section; it finds the outer limits
727 of the line table (minimum and maximum file offset) so that the
728 mainline code can read the whole thing for efficiency. */
732 find_linenos (abfd, asect, vpinfo)
737 struct coff_symfile_info *info;
739 file_ptr offset, maxoff;
741 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
742 count = asect->lineno_count;
747 size = count * local_linesz;
749 info = (struct coff_symfile_info *)vpinfo;
750 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
751 offset = asect->line_filepos;
754 if (offset < info->min_lineno_offset || info->min_lineno_offset == 0)
755 info->min_lineno_offset = offset;
757 maxoff = offset + size;
758 if (maxoff > info->max_lineno_offset)
759 info->max_lineno_offset = maxoff;
763 /* The BFD for this file -- only good while we're actively reading
764 symbols into a psymtab or a symtab. */
766 static bfd *symfile_bfd;
768 /* Read a symbol file, after initialization by coff_symfile_init. */
769 /* FIXME! Addr and Mainline are not used yet -- this will not work for
770 shared libraries or add_file! */
774 coff_symfile_read (objfile, section_offsets, mainline)
775 struct objfile *objfile;
776 struct section_offsets *section_offsets;
779 struct coff_symfile_info *info;
780 bfd *abfd = objfile->obfd;
781 coff_data_type *cdata = coff_data (abfd);
782 char *name = bfd_get_filename (abfd);
787 int stringtab_offset;
789 info = (struct coff_symfile_info *) objfile -> sym_private;
790 symfile_bfd = abfd; /* Kludge for swap routines */
792 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
793 desc = fileno ((FILE *)(abfd->iostream)); /* File descriptor */
794 num_symbols = bfd_get_symcount (abfd); /* How many syms */
795 symtab_offset = cdata->sym_filepos; /* Symbol table file offset */
796 stringtab_offset = symtab_offset + /* String table file offset */
797 num_symbols * cdata->local_symesz;
799 /* Set a few file-statics that give us specific information about
800 the particular COFF file format we're reading. */
801 local_linesz = cdata->local_linesz;
802 local_n_btmask = cdata->local_n_btmask;
803 local_n_btshft = cdata->local_n_btshft;
804 local_n_tmask = cdata->local_n_tmask;
805 local_n_tshift = cdata->local_n_tshift;
806 local_linesz = cdata->local_linesz;
807 local_symesz = cdata->local_symesz;
808 local_auxesz = cdata->local_auxesz;
810 /* Allocate space for raw symbol and aux entries, based on their
811 space requirements as reported by BFD. */
812 temp_sym = (char *) xmalloc
813 (cdata->local_symesz + cdata->local_auxesz);
814 temp_aux = temp_sym + cdata->local_symesz;
815 make_cleanup (free_current_contents, &temp_sym);
818 /* Read the line number table, all at once. */
819 info->min_lineno_offset = 0;
820 info->max_lineno_offset = 0;
821 bfd_map_over_sections (abfd, find_linenos, (PTR)info);
823 val = init_lineno (desc, info->min_lineno_offset,
824 info->max_lineno_offset - info->min_lineno_offset);
826 error ("\"%s\": error reading line numbers\n", name);
828 /* Now read the string table, all at once. */
830 val = init_stringtab (desc, stringtab_offset);
832 error ("\"%s\": can't get string table", name);
833 make_cleanup (free_stringtab, 0);
835 init_minimal_symbol_collection ();
836 make_cleanup (discard_minimal_symbols, 0);
838 /* Now that the executable file is positioned at symbol table,
839 process it and define symbols accordingly. */
841 read_coff_symtab ((long)symtab_offset, num_symbols, objfile);
843 /* Sort symbols alphabetically within each block. */
845 sort_all_symtab_syms ();
847 /* Install any minimal symbols that have been collected as the current
848 minimal symbols for this objfile. */
850 install_minimal_symbols (objfile);
854 coff_new_init (ignore)
855 struct objfile *ignore;
860 /* Perform any local cleanups required when we are done with a particular
861 objfile. I.E, we are in the process of discarding all symbol information
862 for an objfile, freeing up all memory held for it, and unlinking the
863 objfile struct from the global list of known objfiles. */
866 coff_symfile_finish (objfile)
867 struct objfile *objfile;
869 if (objfile -> sym_private != NULL)
871 mfree (objfile -> md, objfile -> sym_private);
876 /* Given pointers to a symbol table in coff style exec file,
877 analyze them and create struct symtab's describing the symbols.
878 NSYMS is the number of symbols in the symbol table.
879 We read them one at a time using read_one_sym (). */
882 read_coff_symtab (symtab_offset, nsyms, objfile)
885 struct objfile *objfile;
888 register struct coff_context_stack *new;
889 struct coff_symbol coff_symbol;
890 register struct coff_symbol *cs = &coff_symbol;
891 static struct internal_syment main_sym;
892 static union internal_auxent main_aux;
893 struct coff_symbol fcn_cs_saved;
894 static struct internal_syment fcn_sym_saved;
895 static union internal_auxent fcn_aux_saved;
898 /* A .file is open. */
899 int in_source_file = 0;
900 int num_object_files = 0;
901 int next_file_symnum = -1;
903 /* Name of the current file. */
904 char *filestring = "";
906 int fcn_first_line = 0;
907 int fcn_last_line = 0;
908 int fcn_start_addr = 0;
909 long fcn_line_ptr = 0;
910 struct cleanup *old_chain;
913 stream = bfd_cache_lookup(objfile->obfd);
915 perror_with_name(objfile->name);
917 /* Work around a stdio bug in SunOS4.1.1 (this makes me nervous....
918 it's hard to know I've really worked around it. The fix should be
919 harmless, anyway). The symptom of the bug is that the first
920 fread (in read_one_sym), will (in my example) actually get data
921 from file offset 268, when the fseek was to 264 (and ftell shows
922 264). This causes all hell to break loose. I was unable to
923 reproduce this on a short test program which operated on the same
924 file, performing (I think) the same sequence of operations.
926 It stopped happening when I put in this rewind().
928 FIXME: Find out if this has been reported to Sun, whether it has
929 been fixed in a later release, etc. */
933 /* Position to read the symbol table. */
934 val = fseek (stream, (long)symtab_offset, 0);
936 perror_with_name (objfile->name);
938 /* This cleanup will be discarded below if we succeed. */
939 old_chain = make_cleanup (free_objfile, objfile);
941 current_objfile = objfile;
942 nlist_stream_global = stream;
943 nlist_nsyms_global = nsyms;
944 last_source_file = NULL;
945 memset (opaque_type_chain, 0, sizeof opaque_type_chain);
947 if (type_vector) /* Get rid of previous one */
948 free ((PTR)type_vector);
949 type_vector_length = 160;
950 type_vector = (struct type **)
951 xmalloc (type_vector_length * sizeof (struct type *));
952 memset (type_vector, 0, type_vector_length * sizeof (struct type *));
954 coff_start_symtab ();
957 while (symnum < nsyms)
959 QUIT; /* Make this command interruptable. */
960 read_one_sym (cs, &main_sym, &main_aux);
963 temp_sem_val = cs->c_name[0] << 24 | cs->c_name[1] << 16 |
964 cs->c_name[2] << 8 | cs->c_name[3];
965 if (int_sem_val == temp_sem_val)
966 last_coffsem = (int) strtol (cs->c_name+4, (char **) NULL, 10);
969 if (cs->c_symnum == next_file_symnum && cs->c_sclass != C_FILE)
971 if (last_source_file)
972 coff_end_symtab (objfile);
974 coff_start_symtab ();
975 complete_symtab ("_globals_", 0, first_object_file_end);
976 /* done with all files, everything from here on out is globals */
979 /* Special case for file with type declarations only, no text. */
980 if (!last_source_file && SDB_TYPE (cs->c_type)
981 && cs->c_secnum == N_DEBUG)
982 complete_symtab (filestring, 0, 0);
984 /* Typedefs should not be treated as symbol definitions. */
985 if (ISFCN (cs->c_type) && cs->c_sclass != C_TPDEF)
987 /* record as a minimal symbol. if we get '.bf' next,
988 * then we undo this step
990 record_minimal_symbol (cs->c_name, cs->c_value, mst_text);
992 fcn_line_ptr = main_aux.x_sym.x_fcnary.x_fcn.x_lnnoptr;
993 fcn_start_addr = cs->c_value;
995 fcn_sym_saved = main_sym;
996 fcn_aux_saved = main_aux;
1000 switch (cs->c_sclass)
1009 complain (&bad_sclass_complaint, cs->c_name);
1014 * c_value field contains symnum of next .file entry in table
1015 * or symnum of first global after last .file.
1017 next_file_symnum = cs->c_value;
1018 filestring = getfilename (&main_aux);
1020 * Complete symbol table for last object file
1021 * containing debugging information.
1023 if (last_source_file)
1025 coff_end_symtab (objfile);
1026 coff_start_symtab ();
1032 if (cs->c_name[0] == '.') {
1033 if (STREQ (cs->c_name, ".text")) {
1034 /* FIXME: don't wire in ".text" as section name
1036 if (++num_object_files == 1) {
1037 /* last address of startup file */
1038 first_object_file_end = cs->c_value +
1039 main_aux.x_scn.x_scnlen;
1041 /* Check for in_source_file deals with case of
1042 a file with debugging symbols
1043 followed by a later file with no symbols. */
1045 complete_symtab (filestring, cs->c_value,
1046 main_aux.x_scn.x_scnlen);
1049 /* flush rest of '.' symbols */
1052 else if (!SDB_TYPE (cs->c_type)
1053 && cs->c_name[0] == 'L'
1054 && (strncmp (cs->c_name, "LI%", 3) == 0
1055 || strncmp (cs->c_name, "LF%", 3) == 0
1056 || strncmp (cs->c_name,"LC%",3) == 0
1057 || strncmp (cs->c_name,"LP%",3) == 0
1058 || strncmp (cs->c_name,"LPB%",4) == 0
1059 || strncmp (cs->c_name,"LBB%",4) == 0
1060 || strncmp (cs->c_name,"LBE%",4) == 0
1061 || strncmp (cs->c_name,"LPBX%",5) == 0))
1062 /* At least on a 3b1, gcc generates swbeg and string labels
1063 that look like this. Ignore them. */
1065 /* fall in for static symbols that don't start with '.' */
1067 if (!SDB_TYPE (cs->c_type)) {
1068 /* FIXME: This is BOGUS Will Robinson!
1069 Coff should provide the SEC_CODE flag for executable sections,
1070 then if we could look up sections by section number we
1071 could see if the flags indicate SEC_CODE. If so, then
1072 record this symbol as a function in the minimal symbol table.
1073 But why are absolute syms recorded as functions, anyway? */
1074 if (cs->c_secnum <= text_bfd_scnum+1) {/* text or abs */
1075 record_minimal_symbol (cs->c_name, cs->c_value,
1079 record_minimal_symbol (cs->c_name, cs->c_value,
1084 process_coff_symbol (cs, &main_aux, objfile);
1088 if (STREQ (cs->c_name, ".bf"))
1090 within_function = 1;
1092 /* value contains address of first non-init type code */
1093 /* main_aux.x_sym.x_misc.x_lnsz.x_lnno
1094 contains line number of '{' } */
1095 if (cs->c_naux != 1)
1096 complain (&bf_no_aux_complaint, cs->c_symnum);
1097 fcn_first_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
1099 new = (struct coff_context_stack *)
1100 xmalloc (sizeof (struct coff_context_stack));
1101 new->depth = depth = 0;
1103 coff_context_stack = new;
1105 new->old_blocks = pending_blocks;
1106 new->start_addr = fcn_start_addr;
1107 fcn_cs_saved.c_name = getsymname (&fcn_sym_saved);
1108 new->name = process_coff_symbol (&fcn_cs_saved,
1109 &fcn_aux_saved, objfile);
1111 else if (STREQ (cs->c_name, ".ef"))
1113 /* the value of .ef is the address of epilogue code;
1114 * not useful for gdb
1116 /* { main_aux.x_sym.x_misc.x_lnsz.x_lnno
1117 contains number of lines to '}' */
1118 new = coff_context_stack;
1121 complain (&ef_complaint, cs->c_symnum);
1122 within_function = 0;
1125 if (cs->c_naux != 1) {
1126 complain (&ef_no_aux_complaint, cs->c_symnum);
1127 fcn_last_line = 0x7FFFFFFF;
1129 fcn_last_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
1131 enter_linenos (fcn_line_ptr, fcn_first_line, fcn_last_line);
1133 coff_finish_block (new->name, &coff_local_symbols, new->old_blocks,
1135 #if defined (FUNCTION_EPILOGUE_SIZE)
1136 /* This macro should be defined only on
1138 fcn_aux_saved.x_sym.x_misc.x_fsize
1139 field is always zero.
1140 So use the .bf record information that
1141 points to the epilogue and add the size
1143 cs->c_value + FUNCTION_EPILOGUE_SIZE,
1145 fcn_cs_saved.c_value +
1146 fcn_aux_saved.x_sym.x_misc.x_fsize,
1150 coff_context_stack = 0;
1151 within_function = 0;
1157 if (STREQ (cs->c_name, ".bb"))
1159 new = (struct coff_context_stack *)
1160 xmalloc (sizeof (struct coff_context_stack));
1163 new->next = coff_context_stack;
1164 coff_context_stack = new;
1165 new->locals = coff_local_symbols;
1166 new->old_blocks = pending_blocks;
1167 new->start_addr = cs->c_value;
1169 coff_local_symbols = 0;
1171 else if (STREQ (cs->c_name, ".eb"))
1173 new = coff_context_stack;
1174 if (new == 0 || depth != new->depth)
1176 complain (&eb_complaint, (char *)symnum);
1179 if (coff_local_symbols && coff_context_stack->next)
1181 /* Make a block for the local symbols within. */
1182 coff_finish_block (0, &coff_local_symbols, new->old_blocks,
1183 new->start_addr, cs->c_value, objfile);
1186 coff_local_symbols = new->locals;
1187 coff_context_stack = new->next;
1193 process_coff_symbol (cs, &main_aux, objfile);
1198 if (last_source_file)
1199 coff_end_symtab (objfile);
1202 /* Patch up any opaque types (references to types that are not defined
1203 in the file where they are referenced, e.g. "struct foo *bar"). */
1204 ALL_OBJFILE_SYMTABS (objfile, s)
1205 patch_opaque_types (s);
1207 discard_cleanups (old_chain);
1208 current_objfile = NULL;
1211 /* Routines for reading headers and symbols from executable. */
1214 /* Move these XXXMAGIC symbol defns into BFD! */
1216 /* Read COFF file header, check magic number,
1217 and return number of symbols. */
1218 read_file_hdr (chan, file_hdr)
1222 lseek (chan, 0L, 0);
1223 if (myread (chan, (char *)file_hdr, FILHSZ) < 0)
1226 switch (file_hdr->f_magic)
1241 #if defined (MC68KWRMAGIC) \
1242 && (!defined (MC68MAGIC) || MC68KWRMAGIC != MC68MAGIC)
1256 case I960ROMAGIC: /* Intel 960 */
1259 case I960RWMAGIC: /* Intel 960 */
1261 return file_hdr->f_nsyms;
1265 if (BADMAG(file_hdr))
1268 return file_hdr->f_nsyms;
1276 /* Read the next symbol, swap it, and return it in both internal_syment
1277 form, and coff_symbol form. Also return its first auxent, if any,
1278 in internal_auxent form, and skip any other auxents. */
1281 read_one_sym (cs, sym, aux)
1282 register struct coff_symbol *cs;
1283 register struct internal_syment *sym;
1284 register union internal_auxent *aux;
1288 cs->c_symnum = symnum;
1289 fread (temp_sym, local_symesz, 1, nlist_stream_global);
1290 bfd_coff_swap_sym_in (symfile_bfd, temp_sym, (char *)sym);
1291 cs->c_naux = sym->n_numaux & 0xff;
1292 if (cs->c_naux >= 1)
1294 fread (temp_aux, local_auxesz, 1, nlist_stream_global);
1295 bfd_coff_swap_aux_in (symfile_bfd, temp_aux, sym->n_type, sym->n_sclass,
1297 /* If more than one aux entry, read past it (only the first aux
1299 for (i = 1; i < cs->c_naux; i++)
1300 fread (temp_aux, local_auxesz, 1, nlist_stream_global);
1302 cs->c_name = getsymname (sym);
1303 cs->c_value = sym->n_value;
1304 cs->c_sclass = (sym->n_sclass & 0xff);
1305 cs->c_secnum = sym->n_scnum;
1306 cs->c_type = (unsigned) sym->n_type;
1307 if (!SDB_TYPE (cs->c_type))
1310 symnum += 1 + cs->c_naux;
1313 /* Support for string table handling */
1315 static char *stringtab = NULL;
1318 init_stringtab (chan, offset)
1324 unsigned char lengthbuf[4];
1332 if (lseek (chan, offset, 0) < 0)
1335 val = myread (chan, (char *)lengthbuf, sizeof lengthbuf);
1336 length = bfd_h_get_32 (symfile_bfd, lengthbuf);
1338 /* If no string table is needed, then the file may end immediately
1339 after the symbols. Just return with `stringtab' set to null. */
1340 if (val != sizeof length || length < sizeof length)
1343 stringtab = (char *) xmalloc (length);
1344 if (stringtab == NULL)
1347 memcpy (stringtab, &length, sizeof length);
1348 if (length == sizeof length) /* Empty table -- just the count */
1351 val = myread (chan, stringtab + sizeof length, length - sizeof length);
1352 if (val != length - sizeof length || stringtab[length - 1] != '\0')
1367 getsymname (symbol_entry)
1368 struct internal_syment *symbol_entry;
1370 static char buffer[SYMNMLEN+1];
1373 if (symbol_entry->_n._n_n._n_zeroes == 0)
1375 result = stringtab + symbol_entry->_n._n_n._n_offset;
1379 strncpy (buffer, symbol_entry->_n._n_name, SYMNMLEN);
1380 buffer[SYMNMLEN] = '\0';
1386 /* Extract the file name from the aux entry of a C_FILE symbol. Return
1387 only the last component of the name. Result is in static storage and
1388 is only good for temporary use. */
1391 getfilename (aux_entry)
1392 union internal_auxent *aux_entry;
1394 static char buffer[BUFSIZ];
1395 register char *temp;
1398 if (aux_entry->x_file.x_n.x_zeroes == 0)
1399 strcpy (buffer, stringtab + aux_entry->x_file.x_n.x_offset);
1402 strncpy (buffer, aux_entry->x_file.x_fname, FILNMLEN);
1403 buffer[FILNMLEN] = '\0';
1406 if ((temp = strrchr (result, '/')) != NULL)
1411 /* Support for line number handling */
1412 static char *linetab = NULL;
1413 static long linetab_offset;
1414 static unsigned long linetab_size;
1416 /* Read in all the line numbers for fast lookups later. Leave them in
1417 external (unswapped) format in memory; we'll swap them as we enter
1418 them into GDB's data structures. */
1421 init_lineno (chan, offset, size)
1428 linetab_offset = offset;
1429 linetab_size = size;
1434 if (lseek (chan, offset, 0) < 0)
1437 /* Allocate the desired table, plus a sentinel */
1438 linetab = (char *) xmalloc (size + local_linesz);
1440 val = myread (chan, linetab, size);
1444 /* Terminate it with an all-zero sentinel record */
1445 memset (linetab + size, 0, local_linesz);
1447 make_cleanup (free, linetab); /* Be sure it gets de-allocated. */
1451 #if !defined (L_LNNO32)
1452 #define L_LNNO32(lp) ((lp)->l_lnno)
1456 enter_linenos (file_offset, first_line, last_line)
1458 register int first_line;
1459 register int last_line;
1461 register char *rawptr;
1462 struct internal_lineno lptr;
1464 if (file_offset < linetab_offset)
1466 complain (&lineno_complaint, file_offset);
1467 if (file_offset > linetab_size) /* Too big to be an offset? */
1469 file_offset += linetab_offset; /* Try reading at that linetab offset */
1472 rawptr = &linetab[file_offset - linetab_offset];
1474 /* skip first line entry for each function */
1475 rawptr += local_linesz;
1476 /* line numbers start at one for the first line of the function */
1480 bfd_coff_swap_lineno_in (symfile_bfd, rawptr, &lptr);
1481 rawptr += local_linesz;
1482 /* The next function, or the sentinel, will have L_LNNO32 zero; we exit. */
1483 if (L_LNNO32 (&lptr) && L_LNNO32 (&lptr) <= last_line)
1484 coff_record_line (first_line + L_LNNO32 (&lptr), lptr.l_addr.l_paddr);
1491 patch_type (type, real_type)
1493 struct type *real_type;
1495 register struct type *target = TYPE_TARGET_TYPE (type);
1496 register struct type *real_target = TYPE_TARGET_TYPE (real_type);
1497 int field_size = TYPE_NFIELDS (real_target) * sizeof (struct field);
1499 TYPE_LENGTH (target) = TYPE_LENGTH (real_target);
1500 TYPE_NFIELDS (target) = TYPE_NFIELDS (real_target);
1501 TYPE_FIELDS (target) = (struct field *) TYPE_ALLOC (target, field_size);
1503 memcpy (TYPE_FIELDS (target), TYPE_FIELDS (real_target), field_size);
1505 if (TYPE_NAME (real_target))
1507 if (TYPE_NAME (target))
1508 free (TYPE_NAME (target));
1509 TYPE_NAME (target) = concat (TYPE_NAME (real_target), NULL);
1513 /* Patch up all appropriate typedef symbols in the opaque_type_chains
1514 so that they can be used to print out opaque data structures properly. */
1517 patch_opaque_types (s)
1520 register struct block *b;
1522 register struct symbol *real_sym;
1524 /* Go through the per-file symbols only */
1525 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
1526 for (i = BLOCK_NSYMS (b) - 1; i >= 0; i--)
1528 /* Find completed typedefs to use to fix opaque ones.
1529 Remove syms from the chain when their types are stored,
1530 but search the whole chain, as there may be several syms
1531 from different files with the same name. */
1532 real_sym = BLOCK_SYM (b, i);
1533 if (SYMBOL_CLASS (real_sym) == LOC_TYPEDEF &&
1534 SYMBOL_NAMESPACE (real_sym) == VAR_NAMESPACE &&
1535 TYPE_CODE (SYMBOL_TYPE (real_sym)) == TYPE_CODE_PTR &&
1536 TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (real_sym))) != 0)
1538 register char *name = SYMBOL_NAME (real_sym);
1539 register int hash = hashname (name);
1540 register struct symbol *sym, *prev;
1543 for (sym = opaque_type_chain[hash]; sym;)
1545 if (name[0] == SYMBOL_NAME (sym)[0] &&
1546 STREQ (name + 1, SYMBOL_NAME (sym) + 1))
1550 SYMBOL_VALUE_CHAIN (prev) = SYMBOL_VALUE_CHAIN (sym);
1554 opaque_type_chain[hash] = SYMBOL_VALUE_CHAIN (sym);
1557 patch_type (SYMBOL_TYPE (sym), SYMBOL_TYPE (real_sym));
1561 sym = SYMBOL_VALUE_CHAIN (prev);
1565 sym = opaque_type_chain[hash];
1571 sym = SYMBOL_VALUE_CHAIN (sym);
1578 static struct symbol *
1579 process_coff_symbol (cs, aux, objfile)
1580 register struct coff_symbol *cs;
1581 register union internal_auxent *aux;
1582 struct objfile *objfile;
1584 register struct symbol *sym
1585 = (struct symbol *) obstack_alloc (&objfile->symbol_obstack,
1586 sizeof (struct symbol));
1588 struct type *temptype;
1590 memset (sym, 0, sizeof (struct symbol));
1592 name = EXTERNAL_NAME (name, objfile->obfd);
1593 SYMBOL_NAME (sym) = obstack_copy0 (&objfile->symbol_obstack, name,
1596 /* default assumptions */
1597 SYMBOL_VALUE (sym) = cs->c_value;
1598 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1600 if (ISFCN (cs->c_type))
1603 /* FIXME: This has NOT been tested. The DBX version has.. */
1604 /* Generate a template for the type of this function. The
1605 types of the arguments will be added as we read the symbol
1607 struct type *new = (struct type *)
1608 obstack_alloc (&objfile->symbol_obstack, sizeof (struct type));
1610 memcpy (new, lookup_function_type (decode_function_type (cs, cs->c_type, aux)),
1611 sizeof(struct type));
1612 SYMBOL_TYPE (sym) = new;
1613 in_function_type = SYMBOL_TYPE(sym);
1616 lookup_function_type (decode_function_type (cs, cs->c_type, aux));
1619 SYMBOL_CLASS (sym) = LOC_BLOCK;
1620 if (cs->c_sclass == C_STAT)
1621 coff_add_symbol_to_list (sym, &coff_file_symbols);
1622 else if (cs->c_sclass == C_EXT)
1623 coff_add_symbol_to_list (sym, &coff_global_symbols);
1627 SYMBOL_TYPE (sym) = decode_type (cs, cs->c_type, aux);
1628 switch (cs->c_sclass)
1634 SYMBOL_CLASS (sym) = LOC_LOCAL;
1635 coff_add_symbol_to_list (sym, &coff_local_symbols);
1639 SYMBOL_CLASS (sym) = LOC_STATIC;
1640 SYMBOL_VALUE_ADDRESS (sym) = (CORE_ADDR) cs->c_value;
1641 coff_add_symbol_to_list (sym, &coff_global_symbols);
1645 SYMBOL_CLASS (sym) = LOC_STATIC;
1646 SYMBOL_VALUE_ADDRESS (sym) = (CORE_ADDR) cs->c_value;
1647 if (within_function) {
1648 /* Static symbol of local scope */
1649 coff_add_symbol_to_list (sym, &coff_local_symbols);
1652 /* Static symbol at top level of file */
1653 coff_add_symbol_to_list (sym, &coff_file_symbols);
1657 #ifdef C_GLBLREG /* AMD coff */
1661 SYMBOL_CLASS (sym) = LOC_REGISTER;
1662 SYMBOL_VALUE (sym) = SDB_REG_TO_REGNUM(cs->c_value);
1663 coff_add_symbol_to_list (sym, &coff_local_symbols);
1670 SYMBOL_CLASS (sym) = LOC_ARG;
1672 /* FIXME: This has not been tested. */
1673 /* Add parameter to function. */
1674 add_param_to_type(&in_function_type,sym);
1676 coff_add_symbol_to_list (sym, &coff_local_symbols);
1677 #if !defined (BELIEVE_PCC_PROMOTION) && (TARGET_BYTE_ORDER == BIG_ENDIAN)
1678 /* If PCC says a parameter is a short or a char,
1679 aligned on an int boundary, realign it to the "little end"
1681 temptype = lookup_fundamental_type (current_objfile, FT_INTEGER);
1682 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) < TYPE_LENGTH (temptype)
1683 && TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_INT
1684 && 0 == SYMBOL_VALUE (sym) % TYPE_LENGTH (temptype))
1686 SYMBOL_VALUE (sym) += TYPE_LENGTH (temptype)
1687 - TYPE_LENGTH (SYMBOL_TYPE (sym));
1693 SYMBOL_CLASS (sym) = LOC_REGPARM;
1694 SYMBOL_VALUE (sym) = SDB_REG_TO_REGNUM(cs->c_value);
1695 coff_add_symbol_to_list (sym, &coff_local_symbols);
1696 #if !defined (BELIEVE_PCC_PROMOTION)
1697 /* FIXME: This should retain the current type, since it's just
1698 a register value. gnu@adobe, 26Feb93 */
1699 /* If PCC says a parameter is a short or a char,
1700 it is really an int. */
1701 temptype = lookup_fundamental_type (current_objfile, FT_INTEGER);
1702 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) < TYPE_LENGTH (temptype)
1703 && TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_INT)
1705 SYMBOL_TYPE (sym) = TYPE_UNSIGNED (SYMBOL_TYPE (sym))
1706 ? lookup_fundamental_type (current_objfile,
1707 FT_UNSIGNED_INTEGER)
1714 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
1715 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1717 /* If type has no name, give it one */
1718 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
1719 TYPE_NAME (SYMBOL_TYPE (sym)) = concat (SYMBOL_NAME (sym), NULL);
1721 /* Keep track of any type which points to empty structured type,
1722 so it can be filled from a definition from another file. A
1723 simple forward reference (TYPE_CODE_UNDEF) is not an
1724 empty structured type, though; the forward references
1725 work themselves out via the magic of coff_lookup_type. */
1726 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_PTR &&
1727 TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym))) == 0 &&
1728 TYPE_CODE (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym))) !=
1731 register int i = hashname (SYMBOL_NAME (sym));
1733 SYMBOL_VALUE_CHAIN (sym) = opaque_type_chain[i];
1734 opaque_type_chain[i] = sym;
1736 coff_add_symbol_to_list (sym, &coff_file_symbols);
1742 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
1743 SYMBOL_NAMESPACE (sym) = STRUCT_NAMESPACE;
1744 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
1745 TYPE_NAME (SYMBOL_TYPE (sym))
1747 (cs->c_sclass == C_ENTAG
1749 : (cs->c_sclass == C_STRTAG
1750 ? "struct " : "union ")),
1751 SYMBOL_NAME (sym), NULL);
1752 coff_add_symbol_to_list (sym, &coff_file_symbols);
1762 /* Decode a coff type specifier;
1763 return the type that is meant. */
1767 decode_type (cs, c_type, aux)
1768 register struct coff_symbol *cs;
1769 unsigned int c_type;
1770 register union internal_auxent *aux;
1772 register struct type *type = 0;
1773 unsigned int new_c_type;
1775 if (c_type & ~N_BTMASK)
1777 new_c_type = DECREF (c_type);
1780 type = decode_type (cs, new_c_type, aux);
1781 type = lookup_pointer_type (type);
1783 else if (ISFCN (c_type))
1785 type = decode_type (cs, new_c_type, aux);
1786 type = lookup_function_type (type);
1788 else if (ISARY (c_type))
1791 register unsigned short *dim;
1792 struct type *base_type, *index_type, *range_type;
1794 /* Define an array type. */
1795 /* auxent refers to array, not base type */
1796 if (aux->x_sym.x_tagndx.l == 0)
1799 /* shift the indices down */
1800 dim = &aux->x_sym.x_fcnary.x_ary.x_dimen[0];
1803 for (i = 0; *dim && i < DIMNUM - 1; i++, dim++)
1807 base_type = decode_type (cs, new_c_type, aux);
1808 index_type = lookup_fundamental_type (current_objfile, FT_INTEGER);
1810 create_range_type ((struct type *) NULL, index_type, 0, n - 1);
1812 create_array_type ((struct type *) NULL, base_type, range_type);
1817 /* Reference to existing type. This only occurs with the
1818 struct, union, and enum types. EPI a29k coff
1819 fakes us out by producing aux entries with a nonzero
1820 x_tagndx for definitions of structs, unions, and enums, so we
1821 have to check the c_sclass field. SCO 3.2v4 cc gets confused
1822 with pointers to pointers to defined structs, and generates
1823 negative x_tagndx fields. */
1824 if (cs->c_naux > 0 && aux->x_sym.x_tagndx.l != 0)
1826 if (cs->c_sclass != C_STRTAG
1827 && cs->c_sclass != C_UNTAG
1828 && cs->c_sclass != C_ENTAG
1829 && aux->x_sym.x_tagndx.l >= 0)
1831 type = coff_alloc_type (aux->x_sym.x_tagndx.l);
1834 complain (&tagndx_bad_complaint, cs->c_name);
1835 /* And fall through to decode_base_type... */
1839 return decode_base_type (cs, BTYPE (c_type), aux);
1842 /* Decode a coff type specifier for function definition;
1843 return the type that the function returns. */
1847 decode_function_type (cs, c_type, aux)
1848 register struct coff_symbol *cs;
1849 unsigned int c_type;
1850 register union internal_auxent *aux;
1852 if (aux->x_sym.x_tagndx.l == 0)
1853 cs->c_naux = 0; /* auxent refers to function, not base type */
1855 return decode_type (cs, DECREF (c_type), aux);
1862 decode_base_type (cs, c_type, aux)
1863 register struct coff_symbol *cs;
1864 unsigned int c_type;
1865 register union internal_auxent *aux;
1872 /* shows up with "void (*foo)();" structure members */
1873 return lookup_fundamental_type (current_objfile, FT_VOID);
1876 /* DGUX actually defines both T_ARG and T_VOID to the same value. */
1879 /* Shows up in DGUX, I think. Not sure where. */
1880 return lookup_fundamental_type (current_objfile, FT_VOID); /* shouldn't show up here */
1886 /* Intel 960 COFF has this symbol and meaning. */
1887 return lookup_fundamental_type (current_objfile, FT_VOID);
1891 return lookup_fundamental_type (current_objfile, FT_CHAR);
1894 return lookup_fundamental_type (current_objfile, FT_SHORT);
1897 return lookup_fundamental_type (current_objfile, FT_INTEGER);
1900 return lookup_fundamental_type (current_objfile, FT_LONG);
1903 return lookup_fundamental_type (current_objfile, FT_FLOAT);
1906 return lookup_fundamental_type (current_objfile, FT_DBL_PREC_FLOAT);
1909 if (cs->c_naux != 1)
1911 /* anonymous structure type */
1912 type = coff_alloc_type (cs->c_symnum);
1913 TYPE_CODE (type) = TYPE_CODE_STRUCT;
1914 TYPE_NAME (type) = concat ("struct ", "<opaque>", NULL);
1915 INIT_CPLUS_SPECIFIC(type);
1916 TYPE_LENGTH (type) = 0;
1917 TYPE_FIELDS (type) = 0;
1918 TYPE_NFIELDS (type) = 0;
1922 type = coff_read_struct_type (cs->c_symnum,
1923 aux->x_sym.x_misc.x_lnsz.x_size,
1924 aux->x_sym.x_fcnary.x_fcn.x_endndx.l);
1929 if (cs->c_naux != 1)
1931 /* anonymous union type */
1932 type = coff_alloc_type (cs->c_symnum);
1933 TYPE_NAME (type) = concat ("union ", "<opaque>", NULL);
1934 INIT_CPLUS_SPECIFIC(type);
1935 TYPE_LENGTH (type) = 0;
1936 TYPE_LENGTH (type) = 0;
1937 TYPE_FIELDS (type) = 0;
1938 TYPE_NFIELDS (type) = 0;
1942 type = coff_read_struct_type (cs->c_symnum,
1943 aux->x_sym.x_misc.x_lnsz.x_size,
1944 aux->x_sym.x_fcnary.x_fcn.x_endndx.l);
1946 TYPE_CODE (type) = TYPE_CODE_UNION;
1950 return coff_read_enum_type (cs->c_symnum,
1951 aux->x_sym.x_misc.x_lnsz.x_size,
1952 aux->x_sym.x_fcnary.x_fcn.x_endndx.l);
1955 /* shouldn't show up here */
1959 return lookup_fundamental_type (current_objfile, FT_UNSIGNED_CHAR);
1962 return lookup_fundamental_type (current_objfile, FT_UNSIGNED_SHORT);
1965 return lookup_fundamental_type (current_objfile, FT_UNSIGNED_INTEGER);
1968 return lookup_fundamental_type (current_objfile, FT_UNSIGNED_LONG);
1970 complain (&unexpected_type_complaint, cs->c_name);
1971 return lookup_fundamental_type (current_objfile, FT_VOID);
1974 /* This page contains subroutines of read_type. */
1976 /* Read the description of a structure (or union type)
1977 and return an object describing the type. */
1979 static struct type *
1980 coff_read_struct_type (index, length, lastsym)
1987 struct nextfield *next;
1991 register struct type *type;
1992 register struct nextfield *list = 0;
1993 struct nextfield *new;
1997 struct coff_symbol member_sym;
1998 register struct coff_symbol *ms = &member_sym;
1999 struct internal_syment sub_sym;
2000 union internal_auxent sub_aux;
2003 type = coff_alloc_type (index);
2004 TYPE_CODE (type) = TYPE_CODE_STRUCT;
2005 INIT_CPLUS_SPECIFIC(type);
2006 TYPE_LENGTH (type) = length;
2008 while (!done && symnum < lastsym && symnum < nlist_nsyms_global)
2010 read_one_sym (ms, &sub_sym, &sub_aux);
2012 name = EXTERNAL_NAME (name, current_objfile->obfd);
2014 switch (ms->c_sclass)
2019 /* Get space to record the next field's data. */
2020 new = (struct nextfield *) alloca (sizeof (struct nextfield));
2024 /* Save the data. */
2025 list->field.name = savestring (name, strlen (name));
2026 list->field.type = decode_type (ms, ms->c_type, &sub_aux);
2027 list->field.bitpos = 8 * ms->c_value;
2028 list->field.bitsize = 0;
2034 /* Get space to record the next field's data. */
2035 new = (struct nextfield *) alloca (sizeof (struct nextfield));
2039 /* Save the data. */
2040 list->field.name = savestring (name, strlen (name));
2041 list->field.type = decode_type (ms, ms->c_type, &sub_aux);
2042 list->field.bitpos = ms->c_value;
2043 list->field.bitsize = sub_aux.x_sym.x_misc.x_lnsz.x_size;
2052 /* Now create the vector of fields, and record how big it is. */
2054 TYPE_NFIELDS (type) = nfields;
2055 TYPE_FIELDS (type) = (struct field *)
2056 TYPE_ALLOC (type, sizeof (struct field) * nfields);
2058 /* Copy the saved-up fields into the field vector. */
2060 for (n = nfields; list; list = list->next)
2061 TYPE_FIELD (type, --n) = list->field;
2066 /* Read a definition of an enumeration type,
2067 and create and return a suitable type object.
2068 Also defines the symbols that represent the values of the type. */
2071 static struct type *
2072 coff_read_enum_type (index, length, lastsym)
2077 register struct symbol *sym;
2078 register struct type *type;
2081 struct coff_pending **symlist;
2082 struct coff_symbol member_sym;
2083 register struct coff_symbol *ms = &member_sym;
2084 struct internal_syment sub_sym;
2085 union internal_auxent sub_aux;
2086 struct coff_pending *osyms, *syms;
2090 type = coff_alloc_type (index);
2091 if (within_function)
2092 symlist = &coff_local_symbols;
2094 symlist = &coff_file_symbols;
2097 while (!done && symnum < lastsym && symnum < nlist_nsyms_global)
2099 read_one_sym (ms, &sub_sym, &sub_aux);
2101 name = EXTERNAL_NAME (name, current_objfile->obfd);
2103 switch (ms->c_sclass)
2106 sym = (struct symbol *) xmalloc (sizeof (struct symbol));
2107 memset (sym, 0, sizeof (struct symbol));
2109 SYMBOL_NAME (sym) = savestring (name, strlen (name));
2110 SYMBOL_CLASS (sym) = LOC_CONST;
2111 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
2112 SYMBOL_VALUE (sym) = ms->c_value;
2113 coff_add_symbol_to_list (sym, symlist);
2118 /* Sometimes the linker (on 386/ix 2.0.2 at least) screws
2119 up the count of how many symbols to read. So stop
2126 /* Now fill in the fields of the type-structure. */
2129 TYPE_LENGTH (type) = length;
2131 TYPE_LENGTH (type) = TARGET_INT_BIT / TARGET_CHAR_BIT; /* Assume ints */
2132 TYPE_CODE (type) = TYPE_CODE_ENUM;
2133 TYPE_NFIELDS (type) = nsyms;
2134 TYPE_FIELDS (type) = (struct field *)
2135 TYPE_ALLOC (type, sizeof (struct field) * nsyms);
2137 /* Find the symbols for the values and put them into the type.
2138 The symbols can be found in the symlist that we put them on
2139 to cause them to be defined. osyms contains the old value
2140 of that symlist; everything up to there was defined by us. */
2142 for (syms = *symlist, n = nsyms; syms != osyms; syms = syms->next)
2144 SYMBOL_TYPE (syms->symbol) = type;
2145 TYPE_FIELD_NAME (type, --n) = SYMBOL_NAME (syms->symbol);
2146 TYPE_FIELD_VALUE (type, n) = 0;
2147 TYPE_FIELD_BITPOS (type, n) = SYMBOL_VALUE (syms->symbol);
2148 TYPE_FIELD_BITSIZE (type, n) = 0;
2150 /* Is this Modula-2's BOOLEAN type? Flag it as such if so. */
2151 if(TYPE_NFIELDS(type) == 2 &&
2152 ((STREQ(TYPE_FIELD_NAME(type,0),"TRUE") &&
2153 STREQ(TYPE_FIELD_NAME(type,1),"FALSE")) ||
2154 (STREQ(TYPE_FIELD_NAME(type,1),"TRUE") &&
2155 STREQ(TYPE_FIELD_NAME(type,0),"FALSE"))))
2156 TYPE_CODE(type) = TYPE_CODE_BOOL;
2160 /* Fake up support for relocating symbol addresses. FIXME. */
2162 struct section_offsets coff_symfile_faker = {0};
2164 struct section_offsets *
2165 coff_symfile_offsets (objfile, addr)
2166 struct objfile *objfile;
2169 return &coff_symfile_faker;
2172 /* Register our ability to parse symbols for coff BFD files */
2174 static struct sym_fns coff_sym_fns =
2176 "coff", /* sym_name: name or name prefix of BFD target type */
2177 4, /* sym_namelen: number of significant sym_name chars */
2178 coff_new_init, /* sym_new_init: init anything gbl to entire symtab */
2179 coff_symfile_init, /* sym_init: read initial info, setup for sym_read() */
2180 coff_symfile_read, /* sym_read: read a symbol file into symtab */
2181 coff_symfile_finish, /* sym_finish: finished with file, cleanup */
2182 coff_symfile_offsets, /* sym_offsets: xlate external to internal form */
2183 NULL /* next: pointer to next struct sym_fns */
2187 _initialize_coffread ()
2189 add_symtab_fns(&coff_sym_fns);