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 (C) 1987-1991 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
25 #include "breakpoint.h"
30 /* Need to get C_VERSION and friends. FIXME, should be in internalcoff.h */
32 #endif /* not TDESC */
37 #include "internalcoff.h" /* Internal format of COFF symbols in BFD */
38 #include "libcoff.h" /* FIXME secret internal data from BFD */
40 static void add_symbol_to_list ();
41 static void read_coff_symtab ();
42 static void patch_opaque_types ();
43 static struct type
*decode_function_type ();
44 static struct type
*decode_type ();
45 static struct type
*decode_base_type ();
46 static struct type
*read_enum_type ();
47 static struct type
*read_struct_type ();
48 static void finish_block ();
49 static struct blockvector
*make_blockvector ();
50 static struct symbol
*process_coff_symbol ();
51 static int init_stringtab ();
52 static void free_stringtab ();
53 static char *getfilename ();
54 static char *getsymname ();
55 static int init_lineno ();
56 static void enter_linenos ();
57 static void read_one_sym ();
60 extern void free_all_symtabs ();
61 extern void free_all_psymtabs ();
63 /* To be an sdb debug type, type must have at least a basic or primary
64 derived type. Using this rather than checking against T_NULL is
65 said to prevent core dumps if we try to operate on Michael Bloom
68 #define SDB_TYPE(type) (BTYPE(type) | (type & N_TMASK))
71 * Convert from an sdb register number to an internal gdb register number.
72 * This should be defined in tm.h, if REGISTER_NAMES is not set up
73 * to map one to one onto the sdb register numbers.
75 #ifndef SDB_REG_TO_REGNUM
76 # define SDB_REG_TO_REGNUM(value) (value)
79 /* Name of source file whose symbol data we are now processing.
80 This comes from a symbol named ".file". */
82 static char *last_source_file
;
84 /* Core address of start and end of text of current source file.
85 This comes from a ".text" symbol where x_nlinno > 0. */
87 static CORE_ADDR cur_src_start_addr
;
88 static CORE_ADDR cur_src_end_addr
;
90 /* Core address of the end of the first object file. */
91 static CORE_ADDR first_object_file_end
;
93 /* The addresses of the symbol table stream and number of symbols
94 of the object file we are reading (as copied into core). */
96 static FILE *nlist_stream_global
;
97 static int nlist_nsyms_global
;
99 /* The entry point (starting address) of the file, if it is an executable. */
101 static CORE_ADDR entry_point
;
103 /* The index in the symbol table of the last coff symbol that was processed. */
107 /* Vector of types defined so far, indexed by their coff symnum. */
109 static struct typevector
*type_vector
;
111 /* Number of elements allocated for type_vector currently. */
113 static int type_vector_length
;
115 /* Vector of line number information. */
117 static struct linetable
*line_vector
;
119 /* Index of next entry to go in line_vector_index. */
121 static int line_vector_index
;
123 /* Last line number recorded in the line vector. */
125 static int prev_line_number
;
127 /* Number of elements allocated for line_vector currently. */
129 static int line_vector_length
;
131 /* Pointers to scratch storage, used for reading raw symbols and auxents. */
133 static char *temp_sym
;
134 static char *temp_aux
;
136 /* Local variables that hold the shift and mask values for the
137 COFF file that we are currently reading. These come back to us
138 from BFD, and are referenced by their macro names, as well as
139 internally to the BTYPE, ISPTR, ISFCN, ISARY, ISTAG, and DECREF
140 macros from ../internalcoff.h . */
142 static unsigned local_n_btmask
;
143 static unsigned local_n_btshft
;
144 static unsigned local_n_tmask
;
145 static unsigned local_n_tshift
;
147 #define N_BTMASK local_n_btmask
148 #define N_BTSHFT local_n_btshft
149 #define N_TMASK local_n_tmask
150 #define N_TSHIFT local_n_tshift
152 /* Local variables that hold the sizes in the file of various COFF structures.
153 (We only need to know this to read them from the file -- BFD will then
154 translate the data in them, into `internal_xxx' structs in the right
155 byte order, alignment, etc.) */
157 static unsigned local_linesz
;
158 static unsigned local_symesz
;
159 static unsigned local_auxesz
;
165 int int_sem_val
= 's' << 24 | 'e' << 16 | 'm' << 8 | '.';
167 int last_coffsem
= 2;
169 /* This isn't used currently. */
170 int last_coffsyn
= 0;
172 int debug_info
= 0; /*used by tdesc */
173 extern dc_dcontext_t tdesc_handle
;
174 extern int safe_to_init_tdesc_context
;
177 /* Chain of typedefs of pointers to empty struct/union types.
178 They are chained thru the SYMBOL_VALUE_CHAIN. */
181 static struct symbol
*opaque_type_chain
[HASHSIZE
];
183 /* Record the symbols defined for each context in a list.
184 We don't create a struct block for the context until we
185 know how long to make it. */
189 struct pending
*next
;
190 struct symbol
*symbol
;
193 /* Here are the three lists that symbols are put on. */
195 struct pending
*file_symbols
; /* static at top level, and types */
197 struct pending
*global_symbols
; /* global functions and variables */
199 struct pending
*local_symbols
; /* everything local to lexical context */
201 /* List of unclosed lexical contexts
202 (that will become blocks, eventually). */
206 struct context_stack
*next
;
207 struct pending
*locals
;
208 struct pending_block
*old_blocks
;
210 CORE_ADDR start_addr
;
214 struct context_stack
*context_stack
;
216 /* Nonzero if within a function (so symbols should be local,
217 if nothing says specifically). */
222 /* The type of the function we are currently reading in. This is
223 used by define_symbol to record the type of arguments to a function. */
225 struct type
*in_function_type
;
228 /* List of blocks already made (lexical contexts already closed).
229 This is used at the end to make the blockvector. */
233 struct pending_block
*next
;
237 struct pending_block
*pending_blocks
;
239 extern CORE_ADDR startup_file_start
; /* From blockframe.c */
240 extern CORE_ADDR startup_file_end
; /* From blockframe.c */
242 /* Complaints about various problems in the file being read */
244 struct complaint ef_complaint
=
245 {"Unmatched .ef symbol(s) ignored starting at symnum %d", 0, 0};
247 struct complaint lineno_complaint
=
248 {"Line number pointer %d lower than start of line numbers", 0, 0};
251 /* Look up a coff type-number index. Return the address of the slot
252 where the type for that index is stored.
253 The type-number is in INDEX.
255 This can be used for finding the type associated with that index
256 or for associating a new type with the index. */
258 static struct type
**
259 coff_lookup_type (index
)
262 if (index
>= type_vector_length
)
264 int old_vector_length
= type_vector_length
;
266 type_vector_length
*= 2;
267 if (type_vector_length
< index
) {
268 type_vector_length
= index
* 2;
270 type_vector
= (struct typevector
*)
271 xrealloc (type_vector
, sizeof (struct typevector
)
272 + type_vector_length
* sizeof (struct type
*));
273 bzero (&type_vector
->type
[ old_vector_length
],
274 (type_vector_length
- old_vector_length
) * sizeof(struct type
*));
276 return &type_vector
->type
[index
];
279 /* Make sure there is a type allocated for type number index
280 and return the type object.
281 This can create an empty (zeroed) type object. */
284 coff_alloc_type (index
)
287 register struct type
**type_addr
= coff_lookup_type (index
);
288 register struct type
*type
= *type_addr
;
290 /* If we are referring to a type not known at all yet,
291 allocate an empty type for it.
292 We will fill it in later if we find out how. */
295 type
= (struct type
*) obstack_alloc (symbol_obstack
,
296 sizeof (struct type
));
297 bzero (type
, sizeof (struct type
));
303 /* maintain the lists of symbols and blocks */
305 /* Add a symbol to one of the lists of symbols. */
307 add_symbol_to_list (symbol
, listhead
)
308 struct symbol
*symbol
;
309 struct pending
**listhead
;
311 register struct pending
*link
312 = (struct pending
*) xmalloc (sizeof (struct pending
));
314 link
->next
= *listhead
;
315 link
->symbol
= symbol
;
319 /* Take one of the lists of symbols and make a block from it.
320 Put the block on the list of pending blocks. */
323 finish_block (symbol
, listhead
, old_blocks
, start
, end
)
324 struct symbol
*symbol
;
325 struct pending
**listhead
;
326 struct pending_block
*old_blocks
;
327 CORE_ADDR start
, end
;
329 register struct pending
*next
, *next1
;
330 register struct block
*block
;
331 register struct pending_block
*pblock
;
332 struct pending_block
*opblock
;
335 /* Count the length of the list of symbols. */
337 for (next
= *listhead
, i
= 0; next
; next
= next
->next
, i
++);
339 block
= (struct block
*)
340 obstack_alloc (symbol_obstack
, sizeof (struct block
) + (i
- 1) * sizeof (struct symbol
*));
342 /* Copy the symbols into the block. */
344 BLOCK_NSYMS (block
) = i
;
345 for (next
= *listhead
; next
; next
= next
->next
)
346 BLOCK_SYM (block
, --i
) = next
->symbol
;
348 BLOCK_START (block
) = start
;
349 BLOCK_END (block
) = end
;
350 BLOCK_SUPERBLOCK (block
) = 0; /* Filled in when containing block is made */
352 /* Put the block in as the value of the symbol that names it. */
356 SYMBOL_BLOCK_VALUE (symbol
) = block
;
357 BLOCK_FUNCTION (block
) = symbol
;
360 BLOCK_FUNCTION (block
) = 0;
362 /* Now free the links of the list, and empty the list. */
364 for (next
= *listhead
; next
; next
= next1
)
371 /* Install this block as the superblock
372 of all blocks made since the start of this scope
373 that don't have superblocks yet. */
376 for (pblock
= pending_blocks
; pblock
!= old_blocks
; pblock
= pblock
->next
)
378 if (BLOCK_SUPERBLOCK (pblock
->block
) == 0)
379 BLOCK_SUPERBLOCK (pblock
->block
) = block
;
383 /* Record this block on the list of all blocks in the file.
384 Put it after opblock, or at the beginning if opblock is 0.
385 This puts the block in the list after all its subblocks. */
387 pblock
= (struct pending_block
*) xmalloc (sizeof (struct pending_block
));
388 pblock
->block
= block
;
391 pblock
->next
= opblock
->next
;
392 opblock
->next
= pblock
;
396 pblock
->next
= pending_blocks
;
397 pending_blocks
= pblock
;
401 static struct blockvector
*
404 register struct pending_block
*next
, *next1
;
405 register struct blockvector
*blockvector
;
408 /* Count the length of the list of blocks. */
410 for (next
= pending_blocks
, i
= 0; next
; next
= next
->next
, i
++);
412 blockvector
= (struct blockvector
*)
413 obstack_alloc (symbol_obstack
, sizeof (struct blockvector
) + (i
- 1) * sizeof (struct block
*));
415 /* Copy the blocks into the blockvector.
416 This is done in reverse order, which happens to put
417 the blocks into the proper order (ascending starting address).
418 finish_block has hair to insert each block into the list
419 after its subblocks in order to make sure this is true. */
421 BLOCKVECTOR_NBLOCKS (blockvector
) = i
;
422 for (next
= pending_blocks
; next
; next
= next
->next
)
423 BLOCKVECTOR_BLOCK (blockvector
, --i
) = next
->block
;
425 /* Now free the links of the list, and empty the list. */
427 for (next
= pending_blocks
; next
; next
= next1
)
437 /* Manage the vector of line numbers. */
440 record_line (line
, pc
)
444 struct linetable_entry
*e
;
445 /* Make sure line vector is big enough. */
447 if (line_vector_index
+ 2 >= line_vector_length
)
449 line_vector_length
*= 2;
450 line_vector
= (struct linetable
*)
451 xrealloc (line_vector
, sizeof (struct linetable
)
452 + (line_vector_length
453 * sizeof (struct linetable_entry
)));
456 e
= line_vector
->item
+ line_vector_index
++;
457 e
->line
= line
; e
->pc
= pc
;
460 /* Start a new symtab for a new source file.
461 This is called when a COFF ".file" symbol is seen;
462 it indicates the start of data for one original source file. */
471 last_source_file
= 0;
475 /* This isn't used currently. */
480 /* Initialize the source file line number information for this file. */
482 if (line_vector
) /* Unlikely, but maybe possible? */
484 line_vector_index
= 0;
485 line_vector_length
= 1000;
486 prev_line_number
= -2; /* Force first line number to be explicit */
487 line_vector
= (struct linetable
*)
488 xmalloc (sizeof (struct linetable
)
489 + line_vector_length
* sizeof (struct linetable_entry
));
492 /* Save the vital information from when starting to read a file,
493 for use when closing off the current file.
494 NAME is the file name the symbols came from, START_ADDR is the first
495 text address for the file, and SIZE is the number of bytes of text. */
498 complete_symtab (name
, start_addr
, size
)
500 CORE_ADDR start_addr
;
503 last_source_file
= savestring (name
, strlen (name
));
504 cur_src_start_addr
= start_addr
;
505 cur_src_end_addr
= start_addr
+ size
;
507 if (entry_point
< cur_src_end_addr
508 && entry_point
>= cur_src_start_addr
)
510 startup_file_start
= cur_src_start_addr
;
511 startup_file_end
= cur_src_end_addr
;
515 /* Finish the symbol definitions for one main source file,
516 close off all the lexical contexts for that file
517 (creating struct block's for them), then make the
518 struct symtab for that file and put it in the list of all such. */
523 register struct symtab
*symtab
;
524 register struct context_stack
*cstk
;
525 register struct blockvector
*blockvector
;
526 register struct linetable
*lv
;
528 /* Finish the lexical context of the last function in the file. */
532 cstk
= context_stack
;
534 /* Make a block for the local symbols within. */
535 finish_block (cstk
->name
, &local_symbols
, cstk
->old_blocks
,
536 cstk
->start_addr
, cur_src_end_addr
);
540 /* Ignore a file that has no functions with real debugging info. */
541 if (pending_blocks
== 0 && file_symbols
== 0 && global_symbols
== 0)
545 line_vector_length
= -1;
546 last_source_file
= 0;
550 /* Create the two top-level blocks for this file (STATIC_BLOCK and
552 finish_block (0, &file_symbols
, 0, cur_src_start_addr
, cur_src_end_addr
);
553 finish_block (0, &global_symbols
, 0, cur_src_start_addr
, cur_src_end_addr
);
555 /* Create the blockvector that points to all the file's blocks. */
556 blockvector
= make_blockvector ();
558 /* Now create the symtab object for this source file. */
559 symtab
= allocate_symtab (last_source_file
);
561 /* Fill in its components. */
562 symtab
->blockvector
= blockvector
;
563 symtab
->free_code
= free_linetable
;
564 symtab
->free_ptr
= 0;
565 symtab
->filename
= last_source_file
;
566 symtab
->dirname
= NULL
;
568 lv
->nitems
= line_vector_index
;
569 symtab
->linetable
= (struct linetable
*)
570 xrealloc (lv
, (sizeof (struct linetable
)
571 + lv
->nitems
* sizeof (struct linetable_entry
)));
574 symtab
->coffsem
= last_coffsem
;
576 /* This isn't used currently. Besides, if this is really about "syntax",
577 it shouldn't need to stick around past symbol read-in time. */
578 symtab
->coffsyn
= last_coffsyn
;
582 free_named_symtabs (symtab
->filename
);
584 /* Link the new symtab into the list of such. */
585 symtab
->next
= symtab_list
;
586 symtab_list
= symtab
;
588 /* Reinitialize for beginning of new file. */
590 line_vector_length
= -1;
591 last_source_file
= 0;
595 record_misc_function (name
, address
)
600 /* We don't want TDESC entry points on the misc_function_vector */
601 if (name
[0] == '@') return;
603 /* mf_text isn't true, but apparently COFF doesn't tell us what it really
604 is, so this guess is more useful than mf_unknown. */
605 prim_record_misc_function (savestring (name
, strlen (name
)),
610 /* coff_symfile_init ()
611 is the coff-specific initialization routine for reading symbols.
612 It is passed a struct sym_fns which contains, among other things,
613 the BFD for the file whose symbols are being read, and a slot for
614 a pointer to "private data" which we fill with cookies and other
615 treats for coff_symfile_read ().
617 We will only be called if this is a COFF or COFF-like file.
618 BFD handles figuring out the format of the file, and code in symtab.c
619 uses BFD's determination to vector to us.
621 The ultimate result is a new symtab (or, FIXME, eventually a psymtab). */
623 struct coff_symfile_info
{
624 file_ptr min_lineno_offset
; /* Where in file lowest line#s are */
625 file_ptr max_lineno_offset
; /* 1+last byte of line#s in file */
628 static int text_bfd_scnum
;
631 coff_symfile_init (sf
)
635 bfd
*abfd
= sf
->sym_bfd
;
637 /* Allocate struct to keep track of the symfile */
638 /* FIXME memory leak */
639 sf
->sym_private
= xmalloc (sizeof (struct coff_symfile_info
));
642 safe_to_init_tdesc_context
= 0;
645 /* Save startup file's range of PC addresses to help blockframe.c
646 decide where the bottom of the stack is. */
647 if (bfd_get_file_flags (abfd
) & EXEC_P
)
649 /* Executable file -- record its entry point so we'll recognize
650 the startup file because it contains the entry point. */
651 entry_point
= bfd_get_start_address (abfd
);
655 /* Examination of non-executable.o files. Short-circuit this stuff. */
656 /* ~0 will not be in any file, we hope. */
658 /* set the startup file to be an empty range. */
659 startup_file_start
= 0;
660 startup_file_end
= 0;
662 /* Save the section number for the text section */
663 if (section
= bfd_get_section_by_name(abfd
,".text"))
664 text_bfd_scnum
= section
->index
;
669 /* This function is called for every section; it finds the outer limits
670 of the line table (minimum and maximum file offset) so that the
671 mainline code can read the whole thing for efficiency. */
675 find_linenos (abfd
, asect
, vpinfo
)
680 struct coff_symfile_info
*info
;
682 file_ptr offset
, maxoff
;
684 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
685 count
= asect
->lineno_count
;
690 size
= count
* local_linesz
;
692 info
= (struct coff_symfile_info
*)vpinfo
;
693 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
694 offset
= asect
->line_filepos
;
697 if (offset
< info
->min_lineno_offset
|| info
->min_lineno_offset
== 0)
698 info
->min_lineno_offset
= offset
;
700 maxoff
= offset
+ size
;
701 if (maxoff
> info
->max_lineno_offset
)
702 info
->max_lineno_offset
= maxoff
;
704 /* While we're at it, find the debug_info. It's in the s_relptr
705 (or, in BFD-speak, rel_filepos) of the text segment section header. */
706 if (strcmp (bfd_section_name (abfd
, asect
), ".text") == 0)
708 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
709 debug_info
= asect
->rel_filepos
;
713 dc_terminate (tdesc_handle
);
721 /* The BFD for this file -- only good while we're actively reading
722 symbols into a psymtab or a symtab. */
724 static bfd
*symfile_bfd
;
726 /* Read a symbol file, after initialization by coff_symfile_init. */
727 /* FIXME! Addr and Mainline are not used yet -- this will not work for
728 shared libraries or add_file! */
732 coff_symfile_read (sf
, addr
, mainline
)
737 struct coff_symfile_info
*info
= (struct coff_symfile_info
*)sf
->sym_private
;
738 bfd
*abfd
= sf
->sym_bfd
;
739 coff_data_type
*cdata
= coff_data (abfd
);
740 char *name
= bfd_get_filename (abfd
);
745 int stringtab_offset
;
747 symfile_bfd
= abfd
; /* Kludge for swap routines */
749 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
750 desc
= fileno ((FILE *)(abfd
->iostream
)); /* File descriptor */
751 num_symbols
= bfd_get_symcount (abfd
); /* How many syms */
752 symtab_offset
= cdata
->sym_filepos
; /* Symbol table file offset */
753 stringtab_offset
= symtab_offset
+ /* String table file offset */
754 num_symbols
* cdata
->local_symesz
;
756 /* Set a few file-statics that give us specific information about
757 the particular COFF file format we're reading. */
758 local_linesz
= cdata
->local_linesz
;
759 local_n_btmask
= cdata
->local_n_btmask
;
760 local_n_btshft
= cdata
->local_n_btshft
;
761 local_n_tmask
= cdata
->local_n_tmask
;
762 local_n_tshift
= cdata
->local_n_tshift
;
763 local_linesz
= cdata
->local_linesz
;
764 local_symesz
= cdata
->local_symesz
;
765 local_auxesz
= cdata
->local_auxesz
;
767 /* Allocate space for raw symbol and aux entries, based on their
768 space requirements as reported by BFD. */
769 temp_sym
= (char *) xmalloc
770 (cdata
->local_symesz
+ cdata
->local_auxesz
);
771 temp_aux
= temp_sym
+ cdata
->local_symesz
;
772 make_cleanup (free_current_contents
, &temp_sym
);
775 /* Read the line number table, all at once. */
776 info
->min_lineno_offset
= 0;
777 info
->max_lineno_offset
= 0;
778 bfd_map_over_sections (abfd
, find_linenos
, info
);
780 val
= init_lineno (desc
, info
->min_lineno_offset
,
781 info
->max_lineno_offset
- info
->min_lineno_offset
);
783 error ("\"%s\": error reading line numbers\n", name
);
785 /* Now read the string table, all at once. */
787 val
= init_stringtab (desc
, stringtab_offset
);
790 free_all_symtabs (); /* FIXME blows whole symtab */
791 printf ("\"%s\": can't get string table", name
);
795 make_cleanup (free_stringtab
, 0);
797 /* Position to read the symbol table. Do not read it all at once. */
798 val
= lseek (desc
, (long)symtab_offset
, 0);
800 perror_with_name (name
);
802 init_misc_bunches ();
803 make_cleanup (discard_misc_bunches
, 0);
805 /* Now that the executable file is positioned at symbol table,
806 process it and define symbols accordingly. */
808 read_coff_symtab (desc
, num_symbols
);
810 patch_opaque_types ();
812 /* Sort symbols alphabetically within each block. */
814 sort_all_symtab_syms ();
816 /* Go over the misc symbol bunches and install them in vector. */
818 condense_misc_bunches (!mainline
);
820 /* Make a default for file to list. */
822 select_source_symtab (0); /* FIXME, this might be too slow, see dbxread */
828 /* There seems to be nothing to do except free_all_symtabs and set
829 symfile to zero, which is done by our caller. */
832 /* Simplified internal version of coff symbol table information */
836 int c_symnum
; /* symbol number of this entry */
837 int c_nsyms
; /* 1 if syment only, 2 if syment + auxent, etc */
844 /* Given pointers to a symbol table in coff style exec file,
845 analyze them and create struct symtab's describing the symbols.
846 NSYMS is the number of symbols in the symbol table.
847 We read them one at a time using read_one_sym (). */
850 read_coff_symtab (desc
, nsyms
)
854 int newfd
; /* Avoid multiple closes on same desc */
856 register struct context_stack
*new;
857 struct coff_symbol coff_symbol
;
858 register struct coff_symbol
*cs
= &coff_symbol
;
859 static struct internal_syment main_sym
;
860 static union internal_auxent main_aux
;
861 struct coff_symbol fcn_cs_saved
;
862 static struct internal_syment fcn_sym_saved
;
863 static union internal_auxent fcn_aux_saved
;
865 /* A .file is open. */
866 int in_source_file
= 0;
867 int num_object_files
= 0;
868 int next_file_symnum
= -1;
870 /* Name of the current file. */
871 char *filestring
= "";
877 struct cleanup
*old_chain
;
882 fatal ("Too many open files");
883 stream
= fdopen (newfd
, "r");
885 old_chain
= make_cleanup (free_all_symtabs
, 0);
886 make_cleanup (fclose
, stream
);
887 nlist_stream_global
= stream
;
888 nlist_nsyms_global
= nsyms
;
889 last_source_file
= 0;
890 bzero (opaque_type_chain
, sizeof opaque_type_chain
);
892 if (type_vector
) /* Get rid of previous one */
894 type_vector_length
= 160;
895 type_vector
= (struct typevector
*)
896 xmalloc (sizeof (struct typevector
)
897 + type_vector_length
* sizeof (struct type
*));
898 bzero (type_vector
->type
, type_vector_length
* sizeof (struct type
*));
903 while (symnum
< nsyms
)
905 QUIT
; /* Make this command interruptable. */
906 read_one_sym (cs
, &main_sym
, &main_aux
);
909 temp_sem_val
= cs
->c_name
[0] << 24 | cs
->c_name
[1] << 16 |
910 cs
->c_name
[2] << 8 | cs
->c_name
[3];
911 if (int_sem_val
== temp_sem_val
)
912 last_coffsem
= (int) strtol (cs
->c_name
+4, (char **) NULL
, 10);
915 if (cs
->c_symnum
== next_file_symnum
&& cs
->c_sclass
!= C_FILE
)
917 if (last_source_file
)
921 complete_symtab ("_globals_", 0, first_object_file_end
);
922 /* done with all files, everything from here on out is globals */
925 /* Special case for file with type declarations only, no text. */
926 if (!last_source_file
&& SDB_TYPE (cs
->c_type
)
927 && cs
->c_secnum
== N_DEBUG
)
928 complete_symtab (filestring
, 0, 0);
930 /* Typedefs should not be treated as symbol definitions. */
931 if (ISFCN (cs
->c_type
) && cs
->c_sclass
!= C_TPDEF
)
933 /* record as misc function. if we get '.bf' next,
934 * then we undo this step
936 record_misc_function (cs
->c_name
, cs
->c_value
);
938 fcn_line_ptr
= main_aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
;
939 fcn_start_addr
= cs
->c_value
;
941 fcn_sym_saved
= main_sym
;
942 fcn_aux_saved
= main_aux
;
946 switch (cs
->c_sclass
)
955 printf ("Bad n_sclass = %d\n", cs
->c_sclass
);
960 * c_value field contains symnum of next .file entry in table
961 * or symnum of first global after last .file.
963 next_file_symnum
= cs
->c_value
;
964 filestring
= getfilename (&main_aux
);
966 * Complete symbol table for last object file
967 * containing debugging information.
969 if (last_source_file
)
978 if (cs
->c_name
[0] == '.') {
979 if (strcmp (cs
->c_name
, ".text") == 0) {
980 /* FIXME: don't wire in ".text" as section name
982 if (++num_object_files
== 1) {
983 /* last address of startup file */
984 first_object_file_end
= cs
->c_value
+
985 main_aux
.x_scn
.x_scnlen
;
987 /* Check for in_source_file deals with case of
988 a file with debugging symbols
989 followed by a later file with no symbols. */
991 complete_symtab (filestring
, cs
->c_value
,
992 main_aux
.x_scn
.x_scnlen
);
995 /* flush rest of '.' symbols */
998 else if (!SDB_TYPE (cs
->c_type
)
999 && cs
->c_name
[0] == 'L'
1000 && (strncmp (cs
->c_name
, "LI%", 3) == 0
1001 || strncmp (cs
->c_name
, "LF%", 3) == 0
1002 || strncmp (cs
->c_name
,"LC%",3) == 0
1003 || strncmp (cs
->c_name
,"LP%",3) == 0
1004 || strncmp (cs
->c_name
,"LPB%",4) == 0
1005 || strncmp (cs
->c_name
,"LBB%",4) == 0
1006 || strncmp (cs
->c_name
,"LBE%",4) == 0
1007 || strncmp (cs
->c_name
,"LPBX%",5) == 0))
1008 /* At least on a 3b1, gcc generates swbeg and string labels
1009 that look like this. Ignore them. */
1011 /* fall in for static symbols that don't start with '.' */
1013 if (!SDB_TYPE (cs
->c_type
)) {
1014 /* FIXME: This is BOGUS Will Robinson!
1015 Coff should provide the SEC_CODE flag for executable sections,
1016 then if we could look up sections by section number we
1017 could see if the flags indicate SEC_CODE. If so, then
1018 record this symbol as a miscellaneous function. But why
1019 are absolute syms recorded as functions, anyway? */
1020 if (cs
->c_secnum
<= text_bfd_scnum
+1) {/* text or abs */
1021 record_misc_function (cs
->c_name
, cs
->c_value
);
1027 (void) process_coff_symbol (cs
, &main_aux
);
1031 if (strcmp (cs
->c_name
, ".bf") == 0)
1033 within_function
= 1;
1035 /* value contains address of first non-init type code */
1036 /* main_aux.x_sym.x_misc.x_lnsz.x_lnno
1037 contains line number of '{' } */
1038 fcn_first_line
= main_aux
.x_sym
.x_misc
.x_lnsz
.x_lnno
;
1040 new = (struct context_stack
*)
1041 xmalloc (sizeof (struct context_stack
));
1042 new->depth
= depth
= 0;
1044 context_stack
= new;
1046 new->old_blocks
= pending_blocks
;
1047 new->start_addr
= fcn_start_addr
;
1048 fcn_cs_saved
.c_name
= getsymname (&fcn_sym_saved
);
1049 new->name
= process_coff_symbol (&fcn_cs_saved
,
1052 else if (strcmp (cs
->c_name
, ".ef") == 0)
1054 /* the value of .ef is the address of epilogue code;
1055 * not useful for gdb
1057 /* { main_aux.x_sym.x_misc.x_lnsz.x_lnno
1058 contains number of lines to '}' */
1059 new = context_stack
;
1062 complain (&ef_complaint
, cs
->c_symnum
);
1063 within_function
= 0;
1066 fcn_last_line
= main_aux
.x_sym
.x_misc
.x_lnsz
.x_lnno
;
1067 enter_linenos (fcn_line_ptr
, fcn_first_line
, fcn_last_line
);
1069 finish_block (new->name
, &local_symbols
, new->old_blocks
,
1071 #if defined (FUNCTION_EPILOGUE_SIZE)
1072 /* This macro should be defined only on
1074 fcn_aux_saved.x_sym.x_misc.x_fsize
1075 field is always zero.
1076 So use the .bf record information that
1077 points to the epilogue and add the size
1079 cs
->c_value
+ FUNCTION_EPILOGUE_SIZE
1081 fcn_cs_saved
.c_value
+
1082 fcn_aux_saved
.x_sym
.x_misc
.x_fsize
1086 within_function
= 0;
1092 if (strcmp (cs
->c_name
, ".bb") == 0)
1094 new = (struct context_stack
*)
1095 xmalloc (sizeof (struct context_stack
));
1098 new->next
= context_stack
;
1099 context_stack
= new;
1100 new->locals
= local_symbols
;
1101 new->old_blocks
= pending_blocks
;
1102 new->start_addr
= cs
->c_value
;
1106 else if (strcmp (cs
->c_name
, ".eb") == 0)
1108 new = context_stack
;
1109 if (new == 0 || depth
!= new->depth
)
1110 error ("Invalid symbol data: .bb/.eb symbol mismatch at symbol %d.",
1112 if (local_symbols
&& context_stack
->next
)
1114 /* Make a block for the local symbols within. */
1115 finish_block (0, &local_symbols
, new->old_blocks
,
1116 new->start_addr
, cs
->c_value
);
1119 local_symbols
= new->locals
;
1120 context_stack
= new->next
;
1127 /* This isn't used currently. */
1128 if (strcmp (cs
->c_name
, ".coffsyn") == 0)
1129 last_coffsyn
= cs
->c_value
;
1132 if ((strcmp (cs
->c_name
, ".coffsem") == 0) &&
1134 last_coffsem
= cs
->c_value
;
1140 if ((strcmp (cs
->c_name
, ".coffsem") == 0) &&
1142 last_coffsem
= cs
->c_value
;
1145 (void) process_coff_symbol (cs
, &main_aux
);
1150 if (last_source_file
)
1153 discard_cleanups (old_chain
);
1156 /* Routines for reading headers and symbols from executable. */
1159 /* Move these XXXMAGIC symbol defns into BFD! */
1161 /* Read COFF file header, check magic number,
1162 and return number of symbols. */
1163 read_file_hdr (chan
, file_hdr
)
1167 lseek (chan
, 0L, 0);
1168 if (myread (chan
, (char *)file_hdr
, FILHSZ
) < 0)
1171 switch (file_hdr
->f_magic
)
1186 #if defined (MC68KWRMAGIC) \
1187 && (!defined (MC68MAGIC) || MC68KWRMAGIC != MC68MAGIC)
1201 case I960ROMAGIC
: /* Intel 960 */
1204 case I960RWMAGIC
: /* Intel 960 */
1206 return file_hdr
->f_nsyms
;
1210 if (BADMAG(file_hdr
))
1213 return file_hdr
->f_nsyms
;
1221 /* Read the next symbol, swap it, and return it in both internal_syment
1222 form, and coff_symbol form. Also return its first auxent, if any,
1223 in internal_auxent form, and skip any other auxents. */
1226 read_one_sym (cs
, sym
, aux
)
1227 register struct coff_symbol
*cs
;
1228 register struct internal_syment
*sym
;
1229 register union internal_auxent
*aux
;
1233 cs
->c_symnum
= symnum
;
1234 fread (temp_sym
, local_symesz
, 1, nlist_stream_global
);
1235 bfd_coff_swap_sym_in (symfile_bfd
, temp_sym
, (char *)sym
);
1236 cs
->c_nsyms
= (sym
->n_numaux
& 0xff) + 1;
1237 if (cs
->c_nsyms
>= 2)
1239 fread (temp_aux
, local_auxesz
, 1, nlist_stream_global
);
1240 bfd_coff_swap_aux_in (symfile_bfd
, temp_aux
, sym
->n_type
, sym
->n_sclass
,
1242 /* If more than one aux entry, read past it (only the first aux
1244 for (i
= 2; i
< cs
->c_nsyms
; i
++)
1245 fread (temp_aux
, local_auxesz
, 1, nlist_stream_global
);
1247 cs
->c_name
= getsymname (sym
);
1248 cs
->c_value
= sym
->n_value
;
1249 cs
->c_sclass
= (sym
->n_sclass
& 0xff);
1250 cs
->c_secnum
= sym
->n_scnum
;
1251 cs
->c_type
= (unsigned) sym
->n_type
;
1252 if (!SDB_TYPE (cs
->c_type
))
1255 symnum
+= cs
->c_nsyms
;
1258 /* Support for string table handling */
1260 static char *stringtab
= NULL
;
1263 init_stringtab (chan
, offset
)
1269 unsigned char lengthbuf
[4];
1277 if (lseek (chan
, offset
, 0) < 0)
1280 val
= myread (chan
, (char *)lengthbuf
, sizeof lengthbuf
);
1281 length
= bfd_h_get_32 (symfile_bfd
, lengthbuf
);
1283 /* If no string table is needed, then the file may end immediately
1284 after the symbols. Just return with `stringtab' set to null. */
1285 if (val
!= sizeof length
|| length
< sizeof length
)
1288 stringtab
= (char *) xmalloc (length
);
1289 if (stringtab
== NULL
)
1292 bcopy (&length
, stringtab
, sizeof length
);
1293 if (length
== sizeof length
) /* Empty table -- just the count */
1296 val
= myread (chan
, stringtab
+ sizeof length
, length
- sizeof length
);
1297 if (val
!= length
- sizeof length
|| stringtab
[length
- 1] != '\0')
1312 getsymname (symbol_entry
)
1313 struct internal_syment
*symbol_entry
;
1315 static char buffer
[SYMNMLEN
+1];
1318 if (symbol_entry
->_n
._n_n
._n_zeroes
== 0)
1320 result
= stringtab
+ symbol_entry
->_n
._n_n
._n_offset
;
1324 strncpy (buffer
, symbol_entry
->_n
._n_name
, SYMNMLEN
);
1325 buffer
[SYMNMLEN
] = '\0';
1332 getfilename (aux_entry
)
1333 union internal_auxent
*aux_entry
;
1335 static char buffer
[BUFSIZ
];
1336 register char *temp
;
1338 extern char *rindex ();
1340 #ifndef COFF_NO_LONG_FILE_NAMES
1341 #if defined (x_zeroes)
1343 if (aux_entry
->x_zeroes
== 0)
1344 strcpy (buffer
, stringtab
+ aux_entry
->x_offset
);
1345 #else /* no x_zeroes */
1346 if (aux_entry
->x_file
.x_n
.x_zeroes
== 0)
1347 strcpy (buffer
, stringtab
+ aux_entry
->x_file
.x_n
.x_offset
);
1348 #endif /* no x_zeroes */
1350 #endif /* COFF_NO_LONG_FILE_NAMES */
1352 #if defined (x_name)
1354 strncpy (buffer
, aux_entry
->x_name
, FILNMLEN
);
1356 strncpy (buffer
, aux_entry
->x_file
.x_fname
, FILNMLEN
);
1358 buffer
[FILNMLEN
] = '\0';
1361 if ((temp
= rindex (result
, '/')) != NULL
)
1366 /* Support for line number handling */
1367 static char *linetab
= NULL
;
1368 static long linetab_offset
;
1369 static unsigned long linetab_size
;
1371 /* Read in all the line numbers for fast lookups later. Leave them in
1372 external (unswapped) format in memory; we'll swap them as we enter
1373 them into GDB's data structures. */
1376 init_lineno (chan
, offset
, size
)
1383 linetab_offset
= offset
;
1384 linetab_size
= size
;
1389 if (lseek (chan
, offset
, 0) < 0)
1392 linetab
= (char *) xmalloc (size
);
1394 val
= myread (chan
, linetab
, size
);
1398 make_cleanup (free
, linetab
); /* Be sure it gets de-allocated. */
1402 #if !defined (L_LNNO32)
1403 #define L_LNNO32(lp) ((lp)->l_lnno)
1407 enter_linenos (file_offset
, first_line
, last_line
)
1409 register int first_line
;
1410 register int last_line
;
1412 register char *rawptr
;
1413 struct internal_lineno lptr
;
1415 if (file_offset
< linetab_offset
)
1417 complain (&lineno_complaint
, file_offset
);
1418 if (file_offset
> linetab_size
) /* Too big to be an offset? */
1420 file_offset
+= linetab_offset
; /* Try reading at that linetab offset */
1423 rawptr
= &linetab
[file_offset
- linetab_offset
];
1425 /* skip first line entry for each function */
1426 rawptr
+= local_linesz
;
1427 /* line numbers start at one for the first line of the function */
1431 bfd_coff_swap_lineno_in (symfile_bfd
, rawptr
, &lptr
);
1432 rawptr
+= local_linesz
;
1433 if (L_LNNO32 (&lptr
) && L_LNNO32 (&lptr
) <= last_line
)
1434 record_line (first_line
+ L_LNNO32 (&lptr
), lptr
.l_addr
.l_paddr
);
1444 register char *p
= name
;
1445 register int total
= p
[0];
1458 return total
% HASHSIZE
;
1462 patch_type (type
, real_type
)
1464 struct type
*real_type
;
1466 register struct type
*target
= TYPE_TARGET_TYPE (type
);
1467 register struct type
*real_target
= TYPE_TARGET_TYPE (real_type
);
1468 int field_size
= TYPE_NFIELDS (real_target
) * sizeof (struct field
);
1470 TYPE_LENGTH (target
) = TYPE_LENGTH (real_target
);
1471 TYPE_NFIELDS (target
) = TYPE_NFIELDS (real_target
);
1472 TYPE_FIELDS (target
) = (struct field
*)
1473 obstack_alloc (symbol_obstack
, field_size
);
1475 bcopy (TYPE_FIELDS (real_target
), TYPE_FIELDS (target
), field_size
);
1477 if (TYPE_NAME (real_target
))
1479 if (TYPE_NAME (target
))
1480 free (TYPE_NAME (target
));
1481 TYPE_NAME (target
) = concat (TYPE_NAME (real_target
), "", "");
1485 /* Patch up all appropriate typdef symbols in the opaque_type_chains
1486 so that they can be used to print out opaque data structures properly */
1489 patch_opaque_types ()
1493 /* Look at each symbol in the per-file block of each symtab. */
1494 for (s
= symtab_list
; s
; s
= s
->next
)
1496 register struct block
*b
;
1499 /* Go through the per-file symbols only */
1500 b
= BLOCKVECTOR_BLOCK (BLOCKVECTOR (s
), STATIC_BLOCK
);
1501 for (i
= BLOCK_NSYMS (b
) - 1; i
>= 0; i
--)
1503 register struct symbol
*real_sym
;
1505 /* Find completed typedefs to use to fix opaque ones.
1506 Remove syms from the chain when their types are stored,
1507 but search the whole chain, as there may be several syms
1508 from different files with the same name. */
1509 real_sym
= BLOCK_SYM (b
, i
);
1510 if (SYMBOL_CLASS (real_sym
) == LOC_TYPEDEF
&&
1511 SYMBOL_NAMESPACE (real_sym
) == VAR_NAMESPACE
&&
1512 TYPE_CODE (SYMBOL_TYPE (real_sym
)) == TYPE_CODE_PTR
&&
1513 TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (real_sym
))) != 0)
1515 register char *name
= SYMBOL_NAME (real_sym
);
1516 register int hash
= hashname (name
);
1517 register struct symbol
*sym
, *prev
;
1520 for (sym
= opaque_type_chain
[hash
]; sym
;)
1522 if (name
[0] == SYMBOL_NAME (sym
)[0] &&
1523 !strcmp (name
+ 1, SYMBOL_NAME (sym
) + 1))
1526 SYMBOL_VALUE_CHAIN (prev
) = SYMBOL_VALUE_CHAIN (sym
);
1528 opaque_type_chain
[hash
] = SYMBOL_VALUE_CHAIN (sym
);
1530 patch_type (SYMBOL_TYPE (sym
), SYMBOL_TYPE (real_sym
));
1533 sym
= SYMBOL_VALUE_CHAIN (prev
);
1535 sym
= opaque_type_chain
[hash
];
1540 sym
= SYMBOL_VALUE_CHAIN (sym
);
1548 #if defined (clipper)
1549 #define BELIEVE_PCC_PROMOTION 1
1552 static struct symbol
*
1553 process_coff_symbol (cs
, aux
)
1554 register struct coff_symbol
*cs
;
1555 register union internal_auxent
*aux
;
1557 register struct symbol
*sym
1558 = (struct symbol
*) obstack_alloc (symbol_obstack
, sizeof (struct symbol
));
1560 #ifdef NAMES_HAVE_UNDERSCORE
1566 bzero (sym
, sizeof (struct symbol
));
1568 name
= (name
[0] == '_' ? name
+ offset
: name
);
1569 SYMBOL_NAME (sym
) = obstack_copy0 (symbol_obstack
, name
, strlen (name
));
1571 /* default assumptions */
1572 SYMBOL_VALUE (sym
) = cs
->c_value
;
1573 SYMBOL_NAMESPACE (sym
) = VAR_NAMESPACE
;
1575 if (ISFCN (cs
->c_type
))
1578 /* FIXME: This has NOT been tested. The DBX version has.. */
1579 /* Generate a template for the type of this function. The
1580 types of the arguments will be added as we read the symbol
1582 struct type
*new = (struct type
*)
1583 obstack_alloc (symbol_obstack
, sizeof (struct type
));
1585 bcopy(lookup_function_type (decode_function_type (cs
, cs
->c_type
, aux
)),
1586 new, sizeof(struct type
));
1587 SYMBOL_TYPE (sym
) = new;
1588 in_function_type
= SYMBOL_TYPE(sym
);
1591 lookup_function_type (decode_function_type (cs
, cs
->c_type
, aux
));
1594 SYMBOL_CLASS (sym
) = LOC_BLOCK
;
1595 if (cs
->c_sclass
== C_STAT
)
1596 add_symbol_to_list (sym
, &file_symbols
);
1597 else if (cs
->c_sclass
== C_EXT
)
1598 add_symbol_to_list (sym
, &global_symbols
);
1602 SYMBOL_TYPE (sym
) = decode_type (cs
, cs
->c_type
, aux
);
1603 switch (cs
->c_sclass
)
1609 SYMBOL_CLASS (sym
) = LOC_LOCAL
;
1610 add_symbol_to_list (sym
, &local_symbols
);
1614 SYMBOL_CLASS (sym
) = LOC_STATIC
;
1615 SYMBOL_VALUE_ADDRESS (sym
) = (CORE_ADDR
) cs
->c_value
;
1616 add_symbol_to_list (sym
, &global_symbols
);
1620 SYMBOL_CLASS (sym
) = LOC_STATIC
;
1621 SYMBOL_VALUE_ADDRESS (sym
) = (CORE_ADDR
) cs
->c_value
;
1622 if (within_function
) {
1623 /* Static symbol of local scope */
1624 add_symbol_to_list (sym
, &local_symbols
);
1627 /* Static symbol at top level of file */
1628 add_symbol_to_list (sym
, &file_symbols
);
1632 #ifdef C_GLBLREG /* AMD coff */
1636 SYMBOL_CLASS (sym
) = LOC_REGISTER
;
1637 SYMBOL_VALUE (sym
) = SDB_REG_TO_REGNUM(cs
->c_value
);
1638 add_symbol_to_list (sym
, &local_symbols
);
1645 SYMBOL_CLASS (sym
) = LOC_ARG
;
1647 /* FIXME: This has not bee tested. */
1648 /* Add parameter to function. */
1649 add_param_to_type(&in_function_type
,sym
);
1651 add_symbol_to_list (sym
, &local_symbols
);
1652 #if !defined (BELIEVE_PCC_PROMOTION)
1653 /* If PCC says a parameter is a short or a char,
1654 it is really an int. */
1655 if (SYMBOL_TYPE (sym
) == builtin_type_char
1656 || SYMBOL_TYPE (sym
) == builtin_type_short
)
1657 SYMBOL_TYPE (sym
) = builtin_type_int
;
1658 else if (SYMBOL_TYPE (sym
) == builtin_type_unsigned_char
1659 || SYMBOL_TYPE (sym
) == builtin_type_unsigned_short
)
1660 SYMBOL_TYPE (sym
) = builtin_type_unsigned_int
;
1665 SYMBOL_CLASS (sym
) = LOC_REGPARM
;
1666 SYMBOL_VALUE (sym
) = SDB_REG_TO_REGNUM(cs
->c_value
);
1667 add_symbol_to_list (sym
, &local_symbols
);
1668 #if !defined (BELIEVE_PCC_PROMOTION)
1669 /* If PCC says a parameter is a short or a char,
1670 it is really an int. */
1671 if (SYMBOL_TYPE (sym
) == builtin_type_char
1672 || SYMBOL_TYPE (sym
) == builtin_type_short
)
1673 SYMBOL_TYPE (sym
) = builtin_type_int
;
1674 else if (SYMBOL_TYPE (sym
) == builtin_type_unsigned_char
1675 || SYMBOL_TYPE (sym
) == builtin_type_unsigned_short
)
1676 SYMBOL_TYPE (sym
) = builtin_type_unsigned_int
;
1681 SYMBOL_CLASS (sym
) = LOC_TYPEDEF
;
1682 SYMBOL_NAMESPACE (sym
) = VAR_NAMESPACE
;
1684 /* If type has no name, give it one */
1685 if (TYPE_NAME (SYMBOL_TYPE (sym
)) == 0
1686 && (TYPE_FLAGS (SYMBOL_TYPE (sym
)) & TYPE_FLAG_PERM
) == 0)
1687 TYPE_NAME (SYMBOL_TYPE (sym
))
1688 = concat (SYMBOL_NAME (sym
), "", "");
1690 /* Keep track of any type which points to empty structured type,
1691 so it can be filled from a definition from another file */
1692 if (TYPE_CODE (SYMBOL_TYPE (sym
)) == TYPE_CODE_PTR
&&
1693 TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym
))) == 0)
1695 register int i
= hashname (SYMBOL_NAME (sym
));
1697 SYMBOL_VALUE_CHAIN (sym
) = opaque_type_chain
[i
];
1698 opaque_type_chain
[i
] = sym
;
1700 add_symbol_to_list (sym
, &file_symbols
);
1706 SYMBOL_CLASS (sym
) = LOC_TYPEDEF
;
1707 SYMBOL_NAMESPACE (sym
) = STRUCT_NAMESPACE
;
1708 if (TYPE_NAME (SYMBOL_TYPE (sym
)) == 0
1709 && (TYPE_FLAGS (SYMBOL_TYPE (sym
)) & TYPE_FLAG_PERM
) == 0)
1710 TYPE_NAME (SYMBOL_TYPE (sym
))
1712 (cs
->c_sclass
== C_ENTAG
1714 : (cs
->c_sclass
== C_STRTAG
1715 ? "struct " : "union ")),
1717 add_symbol_to_list (sym
, &file_symbols
);
1727 /* Decode a coff type specifier;
1728 return the type that is meant. */
1732 decode_type (cs
, c_type
, aux
)
1733 register struct coff_symbol
*cs
;
1734 unsigned int c_type
;
1735 register union internal_auxent
*aux
;
1737 register struct type
*type
= 0;
1738 unsigned int new_c_type
;
1740 if (c_type
& ~N_BTMASK
)
1742 new_c_type
= DECREF (c_type
);
1745 type
= decode_type (cs
, new_c_type
, aux
);
1746 type
= lookup_pointer_type (type
);
1748 else if (ISFCN (c_type
))
1750 type
= decode_type (cs
, new_c_type
, aux
);
1751 type
= lookup_function_type (type
);
1753 else if (ISARY (c_type
))
1756 register unsigned short *dim
;
1757 struct type
*base_type
;
1759 /* Define an array type. */
1760 /* auxent refers to array, not base type */
1761 if (aux
->x_sym
.x_tagndx
.l
== 0)
1764 /* shift the indices down */
1765 dim
= &aux
->x_sym
.x_fcnary
.x_ary
.x_dimen
[0];
1768 for (i
= 0; *dim
&& i
< DIMNUM
- 1; i
++, dim
++)
1772 type
= (struct type
*)
1773 obstack_alloc (symbol_obstack
, sizeof (struct type
));
1774 bzero (type
, sizeof (struct type
));
1776 base_type
= decode_type (cs
, new_c_type
, aux
);
1778 TYPE_CODE (type
) = TYPE_CODE_ARRAY
;
1779 TYPE_TARGET_TYPE (type
) = base_type
;
1780 TYPE_LENGTH (type
) = n
* TYPE_LENGTH (base_type
);
1785 /* Reference to existing type */
1786 if (cs
->c_nsyms
> 1 && aux
->x_sym
.x_tagndx
.l
!= 0)
1788 type
= coff_alloc_type (aux
->x_sym
.x_tagndx
.l
);
1792 return decode_base_type (cs
, BTYPE (c_type
), aux
);
1795 /* Decode a coff type specifier for function definition;
1796 return the type that the function returns. */
1800 decode_function_type (cs
, c_type
, aux
)
1801 register struct coff_symbol
*cs
;
1802 unsigned int c_type
;
1803 register union internal_auxent
*aux
;
1805 if (aux
->x_sym
.x_tagndx
.l
== 0)
1806 cs
->c_nsyms
= 1; /* auxent refers to function, not base type */
1808 return decode_type (cs
, DECREF (c_type
), aux
);
1815 decode_base_type (cs
, c_type
, aux
)
1816 register struct coff_symbol
*cs
;
1817 unsigned int c_type
;
1818 register union internal_auxent
*aux
;
1825 /* shows up with "void (*foo)();" structure members */
1826 return builtin_type_void
;
1829 /* DGUX actually defines both T_ARG and T_VOID to the same value. */
1832 /* Shows up in DGUX, I think. Not sure where. */
1833 return builtin_type_void
; /* shouldn't show up here */
1839 /* Intel 960 COFF has this symbol and meaning. */
1840 return builtin_type_void
;
1844 return builtin_type_char
;
1847 return builtin_type_short
;
1850 return builtin_type_int
;
1853 return builtin_type_long
;
1856 return builtin_type_float
;
1859 return builtin_type_double
;
1862 if (cs
->c_nsyms
!= 2)
1864 /* anonymous structure type */
1865 type
= coff_alloc_type (cs
->c_symnum
);
1866 TYPE_CODE (type
) = TYPE_CODE_STRUCT
;
1867 TYPE_NAME (type
) = concat ("struct ", "<opaque>", "");
1868 TYPE_LENGTH (type
) = 0;
1869 TYPE_FIELDS (type
) = 0;
1870 TYPE_NFIELDS (type
) = 0;
1874 type
= read_struct_type (cs
->c_symnum
,
1875 aux
->x_sym
.x_misc
.x_lnsz
.x_size
,
1876 aux
->x_sym
.x_fcnary
.x_fcn
.x_endndx
);
1881 if (cs
->c_nsyms
!= 2)
1883 /* anonymous union type */
1884 type
= coff_alloc_type (cs
->c_symnum
);
1885 TYPE_NAME (type
) = concat ("union ", "<opaque>", "");
1886 TYPE_LENGTH (type
) = 0;
1887 TYPE_FIELDS (type
) = 0;
1888 TYPE_NFIELDS (type
) = 0;
1892 type
= read_struct_type (cs
->c_symnum
,
1893 aux
->x_sym
.x_misc
.x_lnsz
.x_size
,
1894 aux
->x_sym
.x_fcnary
.x_fcn
.x_endndx
);
1896 TYPE_CODE (type
) = TYPE_CODE_UNION
;
1900 return read_enum_type (cs
->c_symnum
,
1901 aux
->x_sym
.x_misc
.x_lnsz
.x_size
,
1902 aux
->x_sym
.x_fcnary
.x_fcn
.x_endndx
);
1905 /* shouldn't show up here */
1909 return builtin_type_unsigned_char
;
1912 return builtin_type_unsigned_short
;
1915 return builtin_type_unsigned_int
;
1918 return builtin_type_unsigned_long
;
1920 printf ("unexpected type %d at symnum %d\n", c_type
, cs
->c_symnum
);
1921 return builtin_type_void
;
1924 /* This page contains subroutines of read_type. */
1926 /* Read the description of a structure (or union type)
1927 and return an object describing the type. */
1929 static struct type
*
1930 read_struct_type (index
, length
, lastsym
)
1937 struct nextfield
*next
;
1941 register struct type
*type
;
1942 register struct nextfield
*list
= 0;
1943 struct nextfield
*new;
1947 #ifdef NAMES_HAVE_UNDERSCORE
1952 struct coff_symbol member_sym
;
1953 register struct coff_symbol
*ms
= &member_sym
;
1954 struct internal_syment sub_sym
;
1955 union internal_auxent sub_aux
;
1958 type
= coff_alloc_type (index
);
1959 TYPE_CODE (type
) = TYPE_CODE_STRUCT
;
1960 TYPE_LENGTH (type
) = length
;
1962 while (!done
&& symnum
< lastsym
&& symnum
< nlist_nsyms_global
)
1964 read_one_sym (ms
, &sub_sym
, &sub_aux
);
1966 name
= (name
[0] == '_' ? name
+ offset
: name
);
1968 switch (ms
->c_sclass
)
1973 /* Get space to record the next field's data. */
1974 new = (struct nextfield
*) alloca (sizeof (struct nextfield
));
1978 /* Save the data. */
1979 list
->field
.name
= savestring (name
, strlen (name
));
1980 list
->field
.type
= decode_type (ms
, ms
->c_type
, &sub_aux
);
1981 list
->field
.bitpos
= 8 * ms
->c_value
;
1982 list
->field
.bitsize
= 0;
1988 /* Get space to record the next field's data. */
1989 new = (struct nextfield
*) alloca (sizeof (struct nextfield
));
1993 /* Save the data. */
1994 list
->field
.name
= savestring (name
, strlen (name
));
1995 list
->field
.type
= decode_type (ms
, ms
->c_type
, &sub_aux
);
1996 list
->field
.bitpos
= ms
->c_value
;
1997 list
->field
.bitsize
= sub_aux
.x_sym
.x_misc
.x_lnsz
.x_size
;
2006 /* Now create the vector of fields, and record how big it is. */
2008 TYPE_NFIELDS (type
) = nfields
;
2009 TYPE_FIELDS (type
) = (struct field
*)
2010 obstack_alloc (symbol_obstack
, sizeof (struct field
) * nfields
);
2012 /* Copy the saved-up fields into the field vector. */
2014 for (n
= nfields
; list
; list
= list
->next
)
2015 TYPE_FIELD (type
, --n
) = list
->field
;
2020 /* Read a definition of an enumeration type,
2021 and create and return a suitable type object.
2022 Also defines the symbols that represent the values of the type. */
2023 /* Currently assumes it's sizeof (int) and doesn't use length. */
2026 static struct type
*
2027 read_enum_type (index
, length
, lastsym
)
2032 register struct symbol
*sym
;
2033 register struct type
*type
;
2036 struct pending
**symlist
;
2037 struct coff_symbol member_sym
;
2038 register struct coff_symbol
*ms
= &member_sym
;
2039 struct internal_syment sub_sym
;
2040 union internal_auxent sub_aux
;
2041 struct pending
*osyms
, *syms
;
2044 #ifdef NAMES_HAVE_UNDERSCORE
2050 type
= coff_alloc_type (index
);
2051 if (within_function
)
2052 symlist
= &local_symbols
;
2054 symlist
= &file_symbols
;
2057 while (!done
&& symnum
< lastsym
&& symnum
< nlist_nsyms_global
)
2059 read_one_sym (ms
, &sub_sym
, &sub_aux
);
2061 name
= (name
[0] == '_' ? name
+ offset
: name
);
2063 switch (ms
->c_sclass
)
2066 sym
= (struct symbol
*) xmalloc (sizeof (struct symbol
));
2067 bzero (sym
, sizeof (struct symbol
));
2069 SYMBOL_NAME (sym
) = savestring (name
, strlen (name
));
2070 SYMBOL_CLASS (sym
) = LOC_CONST
;
2071 SYMBOL_NAMESPACE (sym
) = VAR_NAMESPACE
;
2072 SYMBOL_VALUE (sym
) = ms
->c_value
;
2073 add_symbol_to_list (sym
, symlist
);
2078 /* Sometimes the linker (on 386/ix 2.0.2 at least) screws
2079 up the count of how many symbols to read. So stop
2086 /* Now fill in the fields of the type-structure. */
2088 /* FIXME: Should be sizeof (int) on target, not host. */
2089 TYPE_LENGTH (type
) = sizeof (int);
2090 TYPE_CODE (type
) = TYPE_CODE_ENUM
;
2091 TYPE_NFIELDS (type
) = nsyms
;
2092 TYPE_FIELDS (type
) = (struct field
*)
2093 obstack_alloc (symbol_obstack
, sizeof (struct field
) * nsyms
);
2095 /* Find the symbols for the values and put them into the type.
2096 The symbols can be found in the symlist that we put them on
2097 to cause them to be defined. osyms contains the old value
2098 of that symlist; everything up to there was defined by us. */
2100 for (syms
= *symlist
, n
= nsyms
; syms
!= osyms
; syms
= syms
->next
)
2102 SYMBOL_TYPE (syms
->symbol
) = type
;
2103 TYPE_FIELD_NAME (type
, --n
) = SYMBOL_NAME (syms
->symbol
);
2104 TYPE_FIELD_VALUE (type
, n
) = 0;
2105 TYPE_FIELD_BITPOS (type
, n
) = SYMBOL_VALUE (syms
->symbol
);
2106 TYPE_FIELD_BITSIZE (type
, n
) = 0;
2108 /* Is this Modula-2's BOOLEAN type? Flag it as such if so. */
2109 if(TYPE_NFIELDS(type
) == 2 &&
2110 ((!strcmp(TYPE_FIELD_NAME(type
,0),"TRUE") &&
2111 !strcmp(TYPE_FIELD_NAME(type
,1),"FALSE")) ||
2112 (!strcmp(TYPE_FIELD_NAME(type
,1),"TRUE") &&
2113 !strcmp(TYPE_FIELD_NAME(type
,0),"FALSE"))))
2114 TYPE_CODE(type
) = TYPE_CODE_BOOL
;
2118 /* Register our ability to parse symbols for coff BFD files */
2120 static struct sym_fns coff_sym_fns
=
2122 /* This assumes that 88kbcs implies TDESC and TDESC implies 88kbcs.
2123 If that's not true, this can be relaxed, but if it is true,
2124 it will just cause users grief if we try to read the wrong kind
2128 #else /* not TDESC */
2130 #endif /* not TDESC */
2131 coff_new_init
, coff_symfile_init
, coff_symfile_read
,
2135 _initialize_coffread ()
2137 add_symtab_fns(&coff_sym_fns
);