1 /* Read coff symbol tables and convert to internal format, for GDB.
2 Contributed by David D. Johnson, Brown University (ddj@cs.brown.edu).
3 Copyright 1987, 1988, 1989, 1990, 1991 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. */
24 #include "breakpoint.h"
33 #include "coff/internal.h" /* Internal format of COFF symbols in BFD */
34 #include "libcoff.h" /* FIXME secret internal data from BFD */
36 /* To be an sdb debug type, type must have at least a basic or primary
37 derived type. Using this rather than checking against T_NULL is
38 said to prevent core dumps if we try to operate on Michael Bloom
41 #define SDB_TYPE(type) (BTYPE(type) | (type & N_TMASK))
44 * Convert from an sdb register number to an internal gdb register number.
45 * This should be defined in tm.h, if REGISTER_NAMES is not set up
46 * to map one to one onto the sdb register numbers.
48 #ifndef SDB_REG_TO_REGNUM
49 # define SDB_REG_TO_REGNUM(value) (value)
52 /* Core address of start and end of text of current source file.
53 This comes from a ".text" symbol where x_nlinno > 0. */
55 static CORE_ADDR cur_src_start_addr
;
56 static CORE_ADDR cur_src_end_addr
;
58 /* Core address of the end of the first object file. */
59 static CORE_ADDR first_object_file_end
;
61 /* The addresses of the symbol table stream and number of symbols
62 of the object file we are reading (as copied into core). */
64 static FILE *nlist_stream_global
;
65 static int nlist_nsyms_global
;
67 /* Vector of line number information. */
69 static struct linetable
*line_vector
;
71 /* Index of next entry to go in line_vector_index. */
73 static int line_vector_index
;
75 /* Last line number recorded in the line vector. */
77 static int prev_line_number
;
79 /* Number of elements allocated for line_vector currently. */
81 static int line_vector_length
;
83 /* Pointers to scratch storage, used for reading raw symbols and auxents. */
85 static char *temp_sym
;
86 static char *temp_aux
;
88 /* Local variables that hold the shift and mask values for the
89 COFF file that we are currently reading. These come back to us
90 from BFD, and are referenced by their macro names, as well as
91 internally to the BTYPE, ISPTR, ISFCN, ISARY, ISTAG, and DECREF
92 macros from ../internalcoff.h . */
94 static unsigned local_n_btmask
;
95 static unsigned local_n_btshft
;
96 static unsigned local_n_tmask
;
97 static unsigned local_n_tshift
;
99 #define N_BTMASK local_n_btmask
100 #define N_BTSHFT local_n_btshft
101 #define N_TMASK local_n_tmask
102 #define N_TSHIFT local_n_tshift
104 /* Local variables that hold the sizes in the file of various COFF structures.
105 (We only need to know this to read them from the file -- BFD will then
106 translate the data in them, into `internal_xxx' structs in the right
107 byte order, alignment, etc.) */
109 static unsigned local_linesz
;
110 static unsigned local_symesz
;
111 static unsigned local_auxesz
;
114 /* Chain of typedefs of pointers to empty struct/union types.
115 They are chained thru the SYMBOL_VALUE_CHAIN. */
117 static struct symbol
*opaque_type_chain
[HASHSIZE
];
119 /* Record the symbols defined for each context in a list.
120 We don't create a struct block for the context until we
121 know how long to make it. */
125 struct coff_pending
*next
;
126 struct symbol
*symbol
;
129 /* Here are the three lists that symbols are put on. */
131 struct coff_pending
*coff_file_symbols
; /* static at top level, and types */
133 struct coff_pending
*coff_global_symbols
; /* global functions and variables */
135 struct coff_pending
*coff_local_symbols
; /* everything local to lexical context */
137 /* List of unclosed lexical contexts
138 (that will become blocks, eventually). */
140 struct coff_context_stack
142 struct coff_context_stack
*next
;
143 struct coff_pending
*locals
;
144 struct pending_block
*old_blocks
;
146 CORE_ADDR start_addr
;
150 struct coff_context_stack
*coff_context_stack
;
152 /* Nonzero if within a function (so symbols should be local,
153 if nothing says specifically). */
158 /* The type of the function we are currently reading in. This is
159 used by define_symbol to record the type of arguments to a function. */
161 struct type
*in_function_type
;
164 struct pending_block
*pending_blocks
;
166 /* Complaints about various problems in the file being read */
168 struct complaint ef_complaint
=
169 {"Unmatched .ef symbol(s) ignored starting at symnum %d", 0, 0};
171 struct complaint bf_no_aux_complaint
=
172 {"`.bf' symbol %d has no aux entry", 0, 0};
174 struct complaint ef_no_aux_complaint
=
175 {"`.ef' symbol %d has no aux entry", 0, 0};
177 struct complaint lineno_complaint
=
178 {"Line number pointer %d lower than start of line numbers", 0, 0};
180 struct complaint unexpected_type_complaint
=
181 {"Unexpected type for symbol %s", 0, 0};
183 struct complaint bad_sclass_complaint
=
184 {"Bad n_sclass for symbol %s", 0, 0};
186 struct complaint misordered_blocks_complaint
=
187 {"Blocks out of order at address %x", 0, 0};
189 struct complaint tagndx_bad_complaint
=
190 {"Symbol table entry for %s has bad tagndx value", 0, 0};
192 /* Simplified internal version of coff symbol table information */
196 int c_symnum
; /* symbol number of this entry */
197 int c_naux
; /* 0 if syment only, 1 if syment + auxent, etc */
205 coff_read_struct_type
PARAMS ((int, int, int));
208 decode_base_type
PARAMS ((struct coff_symbol
*, unsigned int,
209 union internal_auxent
*));
212 decode_type
PARAMS ((struct coff_symbol
*, unsigned int,
213 union internal_auxent
*));
216 decode_function_type
PARAMS ((struct coff_symbol
*, unsigned int,
217 union internal_auxent
*));
220 coff_read_enum_type
PARAMS ((int, int, int));
222 static struct blockvector
*
223 make_blockvector
PARAMS ((struct objfile
*));
225 static struct symbol
*
226 process_coff_symbol
PARAMS ((struct coff_symbol
*, union internal_auxent
*,
230 patch_opaque_types
PARAMS ((struct symtab
*));
233 patch_type
PARAMS ((struct type
*, struct type
*));
236 enter_linenos
PARAMS ((long, int, int));
239 init_lineno
PARAMS ((int, long, int));
242 getfilename
PARAMS ((union internal_auxent
*));
245 getsymname
PARAMS ((struct internal_syment
*));
248 free_stringtab
PARAMS ((void));
251 init_stringtab
PARAMS ((int, long));
254 read_one_sym
PARAMS ((struct coff_symbol
*, struct internal_syment
*,
255 union internal_auxent
*));
258 read_coff_symtab
PARAMS ((long, int, struct objfile
*));
261 find_linenos
PARAMS ((bfd
*, sec_ptr
, PTR
));
264 coff_symfile_init
PARAMS ((struct objfile
*));
267 coff_new_init
PARAMS ((struct objfile
*));
270 coff_symfile_read
PARAMS ((struct objfile
*, CORE_ADDR
, int));
273 coff_symfile_finish
PARAMS ((struct objfile
*));
276 record_minimal_symbol
PARAMS ((char *, CORE_ADDR
, enum minimal_symbol_type
));
279 coff_end_symtab
PARAMS ((struct objfile
*));
282 complete_symtab
PARAMS ((char *, CORE_ADDR
, unsigned int));
285 coff_start_symtab
PARAMS ((void));
288 coff_record_line
PARAMS ((int, CORE_ADDR
));
291 coff_finish_block
PARAMS ((struct symbol
*, struct coff_pending
**,
292 struct pending_block
*, CORE_ADDR
, CORE_ADDR
,
296 coff_add_symbol_to_list
PARAMS ((struct symbol
*, struct coff_pending
**));
299 coff_alloc_type
PARAMS ((int));
301 static struct type
**
302 coff_lookup_type
PARAMS ((int));
305 /* Look up a coff type-number index. Return the address of the slot
306 where the type for that index is stored.
307 The type-number is in INDEX.
309 This can be used for finding the type associated with that index
310 or for associating a new type with the index. */
312 static struct type
**
313 coff_lookup_type (index
)
316 if (index
>= type_vector_length
)
318 int old_vector_length
= type_vector_length
;
320 type_vector_length
*= 2;
321 if (type_vector_length
< index
) {
322 type_vector_length
= index
* 2;
324 type_vector
= (struct type
**)
325 xrealloc ((char *) type_vector
,
326 type_vector_length
* sizeof (struct type
*));
327 bzero (&type_vector
[old_vector_length
],
328 (type_vector_length
- old_vector_length
) * sizeof(struct type
*));
330 return &type_vector
[index
];
333 /* Make sure there is a type allocated for type number index
334 and return the type object.
335 This can create an empty (zeroed) type object. */
338 coff_alloc_type (index
)
341 register struct type
**type_addr
= coff_lookup_type (index
);
342 register struct type
*type
= *type_addr
;
344 /* If we are referring to a type not known at all yet,
345 allocate an empty type for it.
346 We will fill it in later if we find out how. */
349 type
= alloc_type (current_objfile
);
355 /* maintain the lists of symbols and blocks */
357 /* Add a symbol to one of the lists of symbols. */
359 coff_add_symbol_to_list (symbol
, listhead
)
360 struct symbol
*symbol
;
361 struct coff_pending
**listhead
;
363 register struct coff_pending
*link
364 = (struct coff_pending
*) xmalloc (sizeof (struct coff_pending
));
366 link
->next
= *listhead
;
367 link
->symbol
= symbol
;
371 /* Take one of the lists of symbols and make a block from it.
372 Put the block on the list of pending blocks. */
375 coff_finish_block (symbol
, listhead
, old_blocks
, start
, end
, objfile
)
376 struct symbol
*symbol
;
377 struct coff_pending
**listhead
;
378 struct pending_block
*old_blocks
;
379 CORE_ADDR start
, end
;
380 struct objfile
*objfile
;
382 register struct coff_pending
*next
, *next1
;
383 register struct block
*block
;
384 register struct pending_block
*pblock
;
385 struct pending_block
*opblock
;
388 /* Count the length of the list of symbols. */
390 for (next
= *listhead
, i
= 0; next
; next
= next
->next
, i
++);
392 block
= (struct block
*)
393 obstack_alloc (&objfile
->symbol_obstack
, sizeof (struct block
) + (i
- 1) * sizeof (struct symbol
*));
395 /* Copy the symbols into the block. */
397 BLOCK_NSYMS (block
) = i
;
398 for (next
= *listhead
; next
; next
= next
->next
)
399 BLOCK_SYM (block
, --i
) = next
->symbol
;
401 BLOCK_START (block
) = start
;
402 BLOCK_END (block
) = end
;
403 BLOCK_SUPERBLOCK (block
) = 0; /* Filled in when containing block is made */
405 /* Put the block in as the value of the symbol that names it. */
409 SYMBOL_BLOCK_VALUE (symbol
) = block
;
410 BLOCK_FUNCTION (block
) = symbol
;
413 BLOCK_FUNCTION (block
) = 0;
415 /* Now free the links of the list, and empty the list. */
417 for (next
= *listhead
; next
; next
= next1
)
424 /* Install this block as the superblock
425 of all blocks made since the start of this scope
426 that don't have superblocks yet. */
429 for (pblock
= pending_blocks
; pblock
!= old_blocks
; pblock
= pblock
->next
)
431 if (BLOCK_SUPERBLOCK (pblock
->block
) == 0)
432 BLOCK_SUPERBLOCK (pblock
->block
) = block
;
436 /* Record this block on the list of all blocks in the file.
437 Put it after opblock, or at the beginning if opblock is 0.
438 This puts the block in the list after all its subblocks. */
440 pblock
= (struct pending_block
*) xmalloc (sizeof (struct pending_block
));
441 pblock
->block
= block
;
444 pblock
->next
= opblock
->next
;
445 opblock
->next
= pblock
;
449 pblock
->next
= pending_blocks
;
450 pending_blocks
= pblock
;
454 static struct blockvector
*
455 make_blockvector (objfile
)
456 struct objfile
*objfile
;
458 register struct pending_block
*next
, *next1
;
459 register struct blockvector
*blockvector
;
462 /* Count the length of the list of blocks. */
464 for (next
= pending_blocks
, i
= 0; next
; next
= next
->next
, i
++);
466 blockvector
= (struct blockvector
*)
467 obstack_alloc (&objfile
->symbol_obstack
, sizeof (struct blockvector
) + (i
- 1) * sizeof (struct block
*));
469 /* Copy the blocks into the blockvector.
470 This is done in reverse order, which happens to put
471 the blocks into the proper order (ascending starting address).
472 coff_finish_block has hair to insert each block into the list
473 after its subblocks in order to make sure this is true. */
475 BLOCKVECTOR_NBLOCKS (blockvector
) = i
;
476 for (next
= pending_blocks
; next
; next
= next
->next
)
477 BLOCKVECTOR_BLOCK (blockvector
, --i
) = next
->block
;
479 /* Now free the links of the list, and empty the list. */
481 for (next
= pending_blocks
; next
; next
= next1
)
491 /* Manage the vector of line numbers. */
494 coff_record_line (line
, pc
)
498 struct linetable_entry
*e
;
499 /* Make sure line vector is big enough. */
501 if (line_vector_index
+ 2 >= line_vector_length
)
503 line_vector_length
*= 2;
504 line_vector
= (struct linetable
*)
505 xrealloc ((char *) line_vector
, sizeof (struct linetable
)
506 + (line_vector_length
507 * sizeof (struct linetable_entry
)));
510 e
= line_vector
->item
+ line_vector_index
++;
511 e
->line
= line
; e
->pc
= pc
;
514 /* Start a new symtab for a new source file.
515 This is called when a COFF ".file" symbol is seen;
516 it indicates the start of data for one original source file. */
521 coff_file_symbols
= 0;
522 coff_global_symbols
= 0;
523 coff_context_stack
= 0;
525 last_source_file
= 0;
527 /* Initialize the source file line number information for this file. */
529 if (line_vector
) /* Unlikely, but maybe possible? */
530 free ((PTR
)line_vector
);
531 line_vector_index
= 0;
532 line_vector_length
= 1000;
533 prev_line_number
= -2; /* Force first line number to be explicit */
534 line_vector
= (struct linetable
*)
535 xmalloc (sizeof (struct linetable
)
536 + line_vector_length
* sizeof (struct linetable_entry
));
539 /* Save the vital information from when starting to read a file,
540 for use when closing off the current file.
541 NAME is the file name the symbols came from, START_ADDR is the first
542 text address for the file, and SIZE is the number of bytes of text. */
545 complete_symtab (name
, start_addr
, size
)
547 CORE_ADDR start_addr
;
550 last_source_file
= savestring (name
, strlen (name
));
551 cur_src_start_addr
= start_addr
;
552 cur_src_end_addr
= start_addr
+ size
;
554 if (current_objfile
-> ei
.entry_point
>= cur_src_start_addr
&&
555 current_objfile
-> ei
.entry_point
< cur_src_end_addr
)
557 current_objfile
-> ei
.entry_file_lowpc
= cur_src_start_addr
;
558 current_objfile
-> ei
.entry_file_highpc
= cur_src_end_addr
;
562 /* Finish the symbol definitions for one main source file,
563 close off all the lexical contexts for that file
564 (creating struct block's for them), then make the
565 struct symtab for that file and put it in the list of all such. */
568 coff_end_symtab (objfile
)
569 struct objfile
*objfile
;
571 register struct symtab
*symtab
;
572 register struct coff_context_stack
*cstk
;
573 register struct blockvector
*blockvector
;
574 register struct linetable
*lv
;
576 /* Finish the lexical context of the last function in the file. */
578 if (coff_context_stack
)
580 cstk
= coff_context_stack
;
581 coff_context_stack
= 0;
582 /* Make a block for the local symbols within. */
583 coff_finish_block (cstk
->name
, &coff_local_symbols
, cstk
->old_blocks
,
584 cstk
->start_addr
, cur_src_end_addr
, objfile
);
588 /* Ignore a file that has no functions with real debugging info. */
589 if (pending_blocks
== 0 && coff_file_symbols
== 0 && coff_global_symbols
== 0)
591 free ((PTR
)line_vector
);
593 line_vector_length
= -1;
594 last_source_file
= 0;
598 /* It is unfortunate that in amdcoff, pending blocks might not be ordered
599 in this stage. Especially, blocks for static functions will show up at
600 the end. We need to sort them, so tools like `find_pc_function' and
601 `find_pc_block' can work reliably. */
602 if (pending_blocks
) {
603 /* FIXME! Remove this horrid bubble sort and use qsort!!! */
606 struct pending_block
*pb
, *pbnext
;
608 pb
= pending_blocks
, pbnext
= pb
->next
;
613 /* swap blocks if unordered! */
615 if (BLOCK_START(pb
->block
) < BLOCK_START(pbnext
->block
)) {
616 struct block
*tmp
= pb
->block
;
617 complain (&misordered_blocks_complaint
,
618 (char *) BLOCK_START (pb
->block
));
619 pb
->block
= pbnext
->block
;
624 pbnext
= pbnext
->next
;
629 /* Create the two top-level blocks for this file (STATIC_BLOCK and
631 coff_finish_block (0, &coff_file_symbols
, 0, cur_src_start_addr
, cur_src_end_addr
, objfile
);
632 coff_finish_block (0, &coff_global_symbols
, 0, cur_src_start_addr
, cur_src_end_addr
, objfile
);
634 /* Create the blockvector that points to all the file's blocks. */
635 blockvector
= make_blockvector (objfile
);
637 /* Now create the symtab object for this source file. */
638 symtab
= allocate_symtab (last_source_file
, objfile
);
640 /* Fill in its components. */
641 symtab
->blockvector
= blockvector
;
642 symtab
->free_code
= free_linetable
;
643 symtab
->free_ptr
= 0;
644 symtab
->filename
= last_source_file
;
645 symtab
->dirname
= NULL
;
647 lv
->nitems
= line_vector_index
;
648 symtab
->linetable
= (struct linetable
*)
649 xrealloc ((char *) lv
, (sizeof (struct linetable
)
650 + lv
->nitems
* sizeof (struct linetable_entry
)));
652 free_named_symtabs (symtab
->filename
);
654 /* Reinitialize for beginning of new file. */
656 line_vector_length
= -1;
657 last_source_file
= 0;
661 record_minimal_symbol (name
, address
, type
)
664 enum minimal_symbol_type type
;
666 /* We don't want TDESC entry points in the minimal symbol table */
667 if (name
[0] == '@') return;
669 /* mst_text isn't true, but apparently COFF doesn't tell us what it really
670 is, so this guess is more useful than mst_unknown. */
671 prim_record_minimal_symbol (savestring (name
, strlen (name
)),
676 /* coff_symfile_init ()
677 is the coff-specific initialization routine for reading symbols.
678 It is passed a struct objfile which contains, among other things,
679 the BFD for the file whose symbols are being read, and a slot for
680 a pointer to "private data" which we fill with cookies and other
681 treats for coff_symfile_read ().
683 We will only be called if this is a COFF or COFF-like file.
684 BFD handles figuring out the format of the file, and code in symtab.c
685 uses BFD's determination to vector to us.
687 The ultimate result is a new symtab (or, FIXME, eventually a psymtab). */
689 struct coff_symfile_info
{
690 file_ptr min_lineno_offset
; /* Where in file lowest line#s are */
691 file_ptr max_lineno_offset
; /* 1+last byte of line#s in file */
694 static int text_bfd_scnum
;
697 coff_symfile_init (objfile
)
698 struct objfile
*objfile
;
701 bfd
*abfd
= objfile
->obfd
;
703 /* Allocate struct to keep track of the symfile */
704 objfile
-> sym_private
= xmmalloc (objfile
-> md
,
705 sizeof (struct coff_symfile_info
));
707 init_entry_point_info (objfile
);
709 /* Save the section number for the text section */
710 section
= bfd_get_section_by_name(abfd
,".text");
712 text_bfd_scnum
= section
->index
;
717 /* This function is called for every section; it finds the outer limits
718 of the line table (minimum and maximum file offset) so that the
719 mainline code can read the whole thing for efficiency. */
723 find_linenos (abfd
, asect
, vpinfo
)
728 struct coff_symfile_info
*info
;
730 file_ptr offset
, maxoff
;
732 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
733 count
= asect
->lineno_count
;
738 size
= count
* local_linesz
;
740 info
= (struct coff_symfile_info
*)vpinfo
;
741 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
742 offset
= asect
->line_filepos
;
745 if (offset
< info
->min_lineno_offset
|| info
->min_lineno_offset
== 0)
746 info
->min_lineno_offset
= offset
;
748 maxoff
= offset
+ size
;
749 if (maxoff
> info
->max_lineno_offset
)
750 info
->max_lineno_offset
= maxoff
;
754 /* The BFD for this file -- only good while we're actively reading
755 symbols into a psymtab or a symtab. */
757 static bfd
*symfile_bfd
;
759 /* Read a symbol file, after initialization by coff_symfile_init. */
760 /* FIXME! Addr and Mainline are not used yet -- this will not work for
761 shared libraries or add_file! */
765 coff_symfile_read (objfile
, addr
, mainline
)
766 struct objfile
*objfile
;
770 struct coff_symfile_info
*info
;
771 bfd
*abfd
= objfile
->obfd
;
772 coff_data_type
*cdata
= coff_data (abfd
);
773 char *name
= bfd_get_filename (abfd
);
778 int stringtab_offset
;
781 info
= (struct coff_symfile_info
*) objfile
-> sym_private
;
782 symfile_bfd
= abfd
; /* Kludge for swap routines */
784 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
785 desc
= fileno ((FILE *)(abfd
->iostream
)); /* File descriptor */
786 num_symbols
= bfd_get_symcount (abfd
); /* How many syms */
787 symtab_offset
= cdata
->sym_filepos
; /* Symbol table file offset */
788 stringtab_offset
= symtab_offset
+ /* String table file offset */
789 num_symbols
* cdata
->local_symesz
;
791 /* Set a few file-statics that give us specific information about
792 the particular COFF file format we're reading. */
793 local_linesz
= cdata
->local_linesz
;
794 local_n_btmask
= cdata
->local_n_btmask
;
795 local_n_btshft
= cdata
->local_n_btshft
;
796 local_n_tmask
= cdata
->local_n_tmask
;
797 local_n_tshift
= cdata
->local_n_tshift
;
798 local_linesz
= cdata
->local_linesz
;
799 local_symesz
= cdata
->local_symesz
;
800 local_auxesz
= cdata
->local_auxesz
;
802 /* Allocate space for raw symbol and aux entries, based on their
803 space requirements as reported by BFD. */
804 temp_sym
= (char *) xmalloc
805 (cdata
->local_symesz
+ cdata
->local_auxesz
);
806 temp_aux
= temp_sym
+ cdata
->local_symesz
;
807 make_cleanup (free_current_contents
, &temp_sym
);
810 /* Read the line number table, all at once. */
811 info
->min_lineno_offset
= 0;
812 info
->max_lineno_offset
= 0;
813 bfd_map_over_sections (abfd
, find_linenos
, (PTR
)info
);
815 val
= init_lineno (desc
, info
->min_lineno_offset
,
816 info
->max_lineno_offset
- info
->min_lineno_offset
);
818 error ("\"%s\": error reading line numbers\n", name
);
820 /* Now read the string table, all at once. */
822 val
= init_stringtab (desc
, stringtab_offset
);
824 error ("\"%s\": can't get string table", name
);
825 make_cleanup (free_stringtab
, 0);
827 init_minimal_symbol_collection ();
828 make_cleanup (discard_minimal_symbols
, 0);
830 /* Now that the executable file is positioned at symbol table,
831 process it and define symbols accordingly. */
833 read_coff_symtab ((long)symtab_offset
, num_symbols
, objfile
);
835 /* Sort symbols alphabetically within each block. */
837 sort_all_symtab_syms ();
839 /* Install any minimal symbols that have been collected as the current
840 minimal symbols for this objfile. */
842 install_minimal_symbols (objfile
);
846 coff_new_init (ignore
)
847 struct objfile
*ignore
;
852 /* Perform any local cleanups required when we are done with a particular
853 objfile. I.E, we are in the process of discarding all symbol information
854 for an objfile, freeing up all memory held for it, and unlinking the
855 objfile struct from the global list of known objfiles. */
858 coff_symfile_finish (objfile
)
859 struct objfile
*objfile
;
861 if (objfile
-> sym_private
!= NULL
)
863 mfree (objfile
-> md
, objfile
-> sym_private
);
868 /* Given pointers to a symbol table in coff style exec file,
869 analyze them and create struct symtab's describing the symbols.
870 NSYMS is the number of symbols in the symbol table.
871 We read them one at a time using read_one_sym (). */
874 read_coff_symtab (symtab_offset
, nsyms
, objfile
)
877 struct objfile
*objfile
;
880 register struct coff_context_stack
*new;
881 struct coff_symbol coff_symbol
;
882 register struct coff_symbol
*cs
= &coff_symbol
;
883 static struct internal_syment main_sym
;
884 static union internal_auxent main_aux
;
885 struct coff_symbol fcn_cs_saved
;
886 static struct internal_syment fcn_sym_saved
;
887 static union internal_auxent fcn_aux_saved
;
890 /* A .file is open. */
891 int in_source_file
= 0;
892 int num_object_files
= 0;
893 int next_file_symnum
= -1;
895 /* Name of the current file. */
896 char *filestring
= "";
898 int fcn_first_line
= 0;
899 int fcn_last_line
= 0;
900 int fcn_start_addr
= 0;
901 long fcn_line_ptr
= 0;
902 struct cleanup
*old_chain
;
905 stream
= fopen (objfile
->name
, FOPEN_RB
);
907 perror_with_name(objfile
->name
);
909 /* Position to read the symbol table. */
910 val
= fseek (stream
, (long)symtab_offset
, 0);
912 perror_with_name (objfile
->name
);
914 /* These cleanups will be discarded below if we succeed. */
915 old_chain
= make_cleanup (free_objfile
, objfile
);
916 make_cleanup (fclose
, stream
);
918 current_objfile
= objfile
;
919 nlist_stream_global
= stream
;
920 nlist_nsyms_global
= nsyms
;
921 last_source_file
= 0;
922 bzero (opaque_type_chain
, sizeof opaque_type_chain
);
924 if (type_vector
) /* Get rid of previous one */
925 free ((PTR
)type_vector
);
926 type_vector_length
= 160;
927 type_vector
= (struct type
**)
928 xmalloc (type_vector_length
* sizeof (struct type
*));
929 bzero (type_vector
, type_vector_length
* sizeof (struct type
*));
931 coff_start_symtab ();
934 while (symnum
< nsyms
)
936 QUIT
; /* Make this command interruptable. */
937 read_one_sym (cs
, &main_sym
, &main_aux
);
940 temp_sem_val
= cs
->c_name
[0] << 24 | cs
->c_name
[1] << 16 |
941 cs
->c_name
[2] << 8 | cs
->c_name
[3];
942 if (int_sem_val
== temp_sem_val
)
943 last_coffsem
= (int) strtol (cs
->c_name
+4, (char **) NULL
, 10);
946 if (cs
->c_symnum
== next_file_symnum
&& cs
->c_sclass
!= C_FILE
)
948 if (last_source_file
)
949 coff_end_symtab (objfile
);
951 coff_start_symtab ();
952 complete_symtab ("_globals_", 0, first_object_file_end
);
953 /* done with all files, everything from here on out is globals */
956 /* Special case for file with type declarations only, no text. */
957 if (!last_source_file
&& SDB_TYPE (cs
->c_type
)
958 && cs
->c_secnum
== N_DEBUG
)
959 complete_symtab (filestring
, 0, 0);
961 /* Typedefs should not be treated as symbol definitions. */
962 if (ISFCN (cs
->c_type
) && cs
->c_sclass
!= C_TPDEF
)
964 /* record as a minimal symbol. if we get '.bf' next,
965 * then we undo this step
967 record_minimal_symbol (cs
->c_name
, cs
->c_value
, mst_text
);
969 fcn_line_ptr
= main_aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
;
970 fcn_start_addr
= cs
->c_value
;
972 fcn_sym_saved
= main_sym
;
973 fcn_aux_saved
= main_aux
;
977 switch (cs
->c_sclass
)
986 complain (&bad_sclass_complaint
, cs
->c_name
);
991 * c_value field contains symnum of next .file entry in table
992 * or symnum of first global after last .file.
994 next_file_symnum
= cs
->c_value
;
995 filestring
= getfilename (&main_aux
);
997 * Complete symbol table for last object file
998 * containing debugging information.
1000 if (last_source_file
)
1002 coff_end_symtab (objfile
);
1003 coff_start_symtab ();
1009 if (cs
->c_name
[0] == '.') {
1010 if (strcmp (cs
->c_name
, ".text") == 0) {
1011 /* FIXME: don't wire in ".text" as section name
1013 if (++num_object_files
== 1) {
1014 /* last address of startup file */
1015 first_object_file_end
= cs
->c_value
+
1016 main_aux
.x_scn
.x_scnlen
;
1018 /* Check for in_source_file deals with case of
1019 a file with debugging symbols
1020 followed by a later file with no symbols. */
1022 complete_symtab (filestring
, cs
->c_value
,
1023 main_aux
.x_scn
.x_scnlen
);
1026 /* flush rest of '.' symbols */
1029 else if (!SDB_TYPE (cs
->c_type
)
1030 && cs
->c_name
[0] == 'L'
1031 && (strncmp (cs
->c_name
, "LI%", 3) == 0
1032 || strncmp (cs
->c_name
, "LF%", 3) == 0
1033 || strncmp (cs
->c_name
,"LC%",3) == 0
1034 || strncmp (cs
->c_name
,"LP%",3) == 0
1035 || strncmp (cs
->c_name
,"LPB%",4) == 0
1036 || strncmp (cs
->c_name
,"LBB%",4) == 0
1037 || strncmp (cs
->c_name
,"LBE%",4) == 0
1038 || strncmp (cs
->c_name
,"LPBX%",5) == 0))
1039 /* At least on a 3b1, gcc generates swbeg and string labels
1040 that look like this. Ignore them. */
1042 /* fall in for static symbols that don't start with '.' */
1044 if (!SDB_TYPE (cs
->c_type
)) {
1045 /* FIXME: This is BOGUS Will Robinson!
1046 Coff should provide the SEC_CODE flag for executable sections,
1047 then if we could look up sections by section number we
1048 could see if the flags indicate SEC_CODE. If so, then
1049 record this symbol as a function in the minimal symbol table.
1050 But why are absolute syms recorded as functions, anyway? */
1051 if (cs
->c_secnum
<= text_bfd_scnum
+1) {/* text or abs */
1052 record_minimal_symbol (cs
->c_name
, cs
->c_value
,
1056 record_minimal_symbol (cs
->c_name
, cs
->c_value
,
1061 (void) process_coff_symbol (cs
, &main_aux
, objfile
);
1065 if (strcmp (cs
->c_name
, ".bf") == 0)
1067 within_function
= 1;
1069 /* value contains address of first non-init type code */
1070 /* main_aux.x_sym.x_misc.x_lnsz.x_lnno
1071 contains line number of '{' } */
1072 if (cs
->c_naux
!= 1)
1073 complain (&bf_no_aux_complaint
, (char *) cs
->c_symnum
);
1074 fcn_first_line
= main_aux
.x_sym
.x_misc
.x_lnsz
.x_lnno
;
1076 new = (struct coff_context_stack
*)
1077 xmalloc (sizeof (struct coff_context_stack
));
1078 new->depth
= depth
= 0;
1080 coff_context_stack
= new;
1082 new->old_blocks
= pending_blocks
;
1083 new->start_addr
= fcn_start_addr
;
1084 fcn_cs_saved
.c_name
= getsymname (&fcn_sym_saved
);
1085 new->name
= process_coff_symbol (&fcn_cs_saved
,
1086 &fcn_aux_saved
, objfile
);
1088 else if (strcmp (cs
->c_name
, ".ef") == 0)
1090 /* the value of .ef is the address of epilogue code;
1091 * not useful for gdb
1093 /* { main_aux.x_sym.x_misc.x_lnsz.x_lnno
1094 contains number of lines to '}' */
1095 new = coff_context_stack
;
1098 complain (&ef_complaint
, (char *) cs
->c_symnum
);
1099 within_function
= 0;
1102 if (cs
->c_naux
!= 1) {
1103 complain (&ef_no_aux_complaint
, (char *) cs
->c_symnum
);
1104 fcn_last_line
= 0x7FFFFFFF;
1106 fcn_last_line
= main_aux
.x_sym
.x_misc
.x_lnsz
.x_lnno
;
1108 enter_linenos (fcn_line_ptr
, fcn_first_line
, fcn_last_line
);
1110 coff_finish_block (new->name
, &coff_local_symbols
, new->old_blocks
,
1112 #if defined (FUNCTION_EPILOGUE_SIZE)
1113 /* This macro should be defined only on
1115 fcn_aux_saved.x_sym.x_misc.x_fsize
1116 field is always zero.
1117 So use the .bf record information that
1118 points to the epilogue and add the size
1120 cs
->c_value
+ FUNCTION_EPILOGUE_SIZE
,
1122 fcn_cs_saved
.c_value
+
1123 fcn_aux_saved
.x_sym
.x_misc
.x_fsize
,
1127 coff_context_stack
= 0;
1128 within_function
= 0;
1134 if (strcmp (cs
->c_name
, ".bb") == 0)
1136 new = (struct coff_context_stack
*)
1137 xmalloc (sizeof (struct coff_context_stack
));
1140 new->next
= coff_context_stack
;
1141 coff_context_stack
= new;
1142 new->locals
= coff_local_symbols
;
1143 new->old_blocks
= pending_blocks
;
1144 new->start_addr
= cs
->c_value
;
1146 coff_local_symbols
= 0;
1148 else if (strcmp (cs
->c_name
, ".eb") == 0)
1150 new = coff_context_stack
;
1151 if (new == 0 || depth
!= new->depth
)
1152 error ("Invalid symbol data: .bb/.eb symbol mismatch at symbol %d.",
1154 if (coff_local_symbols
&& coff_context_stack
->next
)
1156 /* Make a block for the local symbols within. */
1157 coff_finish_block (0, &coff_local_symbols
, new->old_blocks
,
1158 new->start_addr
, cs
->c_value
, objfile
);
1161 coff_local_symbols
= new->locals
;
1162 coff_context_stack
= new->next
;
1168 (void) process_coff_symbol (cs
, &main_aux
, objfile
);
1173 if (last_source_file
)
1174 coff_end_symtab (objfile
);
1177 /* Patch up any opaque types (references to types that are not defined
1178 in the file where they are referenced, e.g. "struct foo *bar"). */
1179 ALL_OBJFILE_SYMTABS (objfile
, s
)
1180 patch_opaque_types (s
);
1182 discard_cleanups (old_chain
);
1183 current_objfile
= NULL
;
1186 /* Routines for reading headers and symbols from executable. */
1189 /* Move these XXXMAGIC symbol defns into BFD! */
1191 /* Read COFF file header, check magic number,
1192 and return number of symbols. */
1193 read_file_hdr (chan
, file_hdr
)
1197 lseek (chan
, 0L, 0);
1198 if (myread (chan
, (char *)file_hdr
, FILHSZ
) < 0)
1201 switch (file_hdr
->f_magic
)
1216 #if defined (MC68KWRMAGIC) \
1217 && (!defined (MC68MAGIC) || MC68KWRMAGIC != MC68MAGIC)
1231 case I960ROMAGIC
: /* Intel 960 */
1234 case I960RWMAGIC
: /* Intel 960 */
1236 return file_hdr
->f_nsyms
;
1240 if (BADMAG(file_hdr
))
1243 return file_hdr
->f_nsyms
;
1251 /* Read the next symbol, swap it, and return it in both internal_syment
1252 form, and coff_symbol form. Also return its first auxent, if any,
1253 in internal_auxent form, and skip any other auxents. */
1256 read_one_sym (cs
, sym
, aux
)
1257 register struct coff_symbol
*cs
;
1258 register struct internal_syment
*sym
;
1259 register union internal_auxent
*aux
;
1263 cs
->c_symnum
= symnum
;
1264 fread (temp_sym
, local_symesz
, 1, nlist_stream_global
);
1265 bfd_coff_swap_sym_in (symfile_bfd
, temp_sym
, (char *)sym
);
1266 cs
->c_naux
= sym
->n_numaux
& 0xff;
1267 if (cs
->c_naux
>= 1)
1269 fread (temp_aux
, local_auxesz
, 1, nlist_stream_global
);
1270 bfd_coff_swap_aux_in (symfile_bfd
, temp_aux
, sym
->n_type
, sym
->n_sclass
,
1272 /* If more than one aux entry, read past it (only the first aux
1274 for (i
= 1; i
< cs
->c_naux
; i
++)
1275 fread (temp_aux
, local_auxesz
, 1, nlist_stream_global
);
1277 cs
->c_name
= getsymname (sym
);
1278 cs
->c_value
= sym
->n_value
;
1279 cs
->c_sclass
= (sym
->n_sclass
& 0xff);
1280 cs
->c_secnum
= sym
->n_scnum
;
1281 cs
->c_type
= (unsigned) sym
->n_type
;
1282 if (!SDB_TYPE (cs
->c_type
))
1285 symnum
+= 1 + cs
->c_naux
;
1288 /* Support for string table handling */
1290 static char *stringtab
= NULL
;
1293 init_stringtab (chan
, offset
)
1299 unsigned char lengthbuf
[4];
1307 if (lseek (chan
, offset
, 0) < 0)
1310 val
= myread (chan
, (char *)lengthbuf
, sizeof lengthbuf
);
1311 length
= bfd_h_get_32 (symfile_bfd
, lengthbuf
);
1313 /* If no string table is needed, then the file may end immediately
1314 after the symbols. Just return with `stringtab' set to null. */
1315 if (val
!= sizeof length
|| length
< sizeof length
)
1318 stringtab
= (char *) xmalloc (length
);
1319 if (stringtab
== NULL
)
1322 bcopy (&length
, stringtab
, sizeof length
);
1323 if (length
== sizeof length
) /* Empty table -- just the count */
1326 val
= myread (chan
, stringtab
+ sizeof length
, length
- sizeof length
);
1327 if (val
!= length
- sizeof length
|| stringtab
[length
- 1] != '\0')
1342 getsymname (symbol_entry
)
1343 struct internal_syment
*symbol_entry
;
1345 static char buffer
[SYMNMLEN
+1];
1348 if (symbol_entry
->_n
._n_n
._n_zeroes
== 0)
1350 result
= stringtab
+ symbol_entry
->_n
._n_n
._n_offset
;
1354 strncpy (buffer
, symbol_entry
->_n
._n_name
, SYMNMLEN
);
1355 buffer
[SYMNMLEN
] = '\0';
1362 getfilename (aux_entry
)
1363 union internal_auxent
*aux_entry
;
1365 static char buffer
[BUFSIZ
];
1366 register char *temp
;
1369 #ifndef COFF_NO_LONG_FILE_NAMES
1370 #if defined (x_zeroes)
1372 if (aux_entry
->x_zeroes
== 0)
1373 strcpy (buffer
, stringtab
+ aux_entry
->x_offset
);
1374 #else /* no x_zeroes */
1375 if (aux_entry
->x_file
.x_n
.x_zeroes
== 0)
1376 strcpy (buffer
, stringtab
+ aux_entry
->x_file
.x_n
.x_offset
);
1377 #endif /* no x_zeroes */
1379 #endif /* COFF_NO_LONG_FILE_NAMES */
1381 #if defined (x_name)
1383 strncpy (buffer
, aux_entry
->x_name
, FILNMLEN
);
1385 strncpy (buffer
, aux_entry
->x_file
.x_fname
, FILNMLEN
);
1387 buffer
[FILNMLEN
] = '\0';
1390 if ((temp
= strrchr (result
, '/')) != NULL
)
1395 /* Support for line number handling */
1396 static char *linetab
= NULL
;
1397 static long linetab_offset
;
1398 static unsigned long linetab_size
;
1400 /* Read in all the line numbers for fast lookups later. Leave them in
1401 external (unswapped) format in memory; we'll swap them as we enter
1402 them into GDB's data structures. */
1405 init_lineno (chan
, offset
, size
)
1412 linetab_offset
= offset
;
1413 linetab_size
= size
;
1418 if (lseek (chan
, offset
, 0) < 0)
1421 /* Allocate the desired table, plus a sentinel */
1422 linetab
= (char *) xmalloc (size
+ local_linesz
);
1424 val
= myread (chan
, linetab
, size
);
1428 /* Terminate it with an all-zero sentinel record */
1429 bzero (linetab
+ size
, local_linesz
);
1431 make_cleanup (free
, linetab
); /* Be sure it gets de-allocated. */
1435 #if !defined (L_LNNO32)
1436 #define L_LNNO32(lp) ((lp)->l_lnno)
1440 enter_linenos (file_offset
, first_line
, last_line
)
1442 register int first_line
;
1443 register int last_line
;
1445 register char *rawptr
;
1446 struct internal_lineno lptr
;
1448 if (file_offset
< linetab_offset
)
1450 complain (&lineno_complaint
, (char *) file_offset
);
1451 if (file_offset
> linetab_size
) /* Too big to be an offset? */
1453 file_offset
+= linetab_offset
; /* Try reading at that linetab offset */
1456 rawptr
= &linetab
[file_offset
- linetab_offset
];
1458 /* skip first line entry for each function */
1459 rawptr
+= local_linesz
;
1460 /* line numbers start at one for the first line of the function */
1464 bfd_coff_swap_lineno_in (symfile_bfd
, rawptr
, &lptr
);
1465 rawptr
+= local_linesz
;
1466 /* The next function, or the sentinel, will have L_LNNO32 zero; we exit. */
1467 if (L_LNNO32 (&lptr
) && L_LNNO32 (&lptr
) <= last_line
)
1468 coff_record_line (first_line
+ L_LNNO32 (&lptr
), lptr
.l_addr
.l_paddr
);
1475 patch_type (type
, real_type
)
1477 struct type
*real_type
;
1479 register struct type
*target
= TYPE_TARGET_TYPE (type
);
1480 register struct type
*real_target
= TYPE_TARGET_TYPE (real_type
);
1481 int field_size
= TYPE_NFIELDS (real_target
) * sizeof (struct field
);
1483 TYPE_LENGTH (target
) = TYPE_LENGTH (real_target
);
1484 TYPE_NFIELDS (target
) = TYPE_NFIELDS (real_target
);
1485 TYPE_FIELDS (target
) = (struct field
*)
1486 obstack_alloc (¤t_objfile
-> type_obstack
, field_size
);
1488 bcopy (TYPE_FIELDS (real_target
), TYPE_FIELDS (target
), field_size
);
1490 if (TYPE_NAME (real_target
))
1492 if (TYPE_NAME (target
))
1493 free (TYPE_NAME (target
));
1494 TYPE_NAME (target
) = concat (TYPE_NAME (real_target
), NULL
);
1498 /* Patch up all appropriate typedef symbols in the opaque_type_chains
1499 so that they can be used to print out opaque data structures properly. */
1502 patch_opaque_types (s
)
1505 register struct block
*b
;
1507 register struct symbol
*real_sym
;
1509 /* Go through the per-file symbols only */
1510 b
= BLOCKVECTOR_BLOCK (BLOCKVECTOR (s
), STATIC_BLOCK
);
1511 for (i
= BLOCK_NSYMS (b
) - 1; i
>= 0; i
--)
1513 /* Find completed typedefs to use to fix opaque ones.
1514 Remove syms from the chain when their types are stored,
1515 but search the whole chain, as there may be several syms
1516 from different files with the same name. */
1517 real_sym
= BLOCK_SYM (b
, i
);
1518 if (SYMBOL_CLASS (real_sym
) == LOC_TYPEDEF
&&
1519 SYMBOL_NAMESPACE (real_sym
) == VAR_NAMESPACE
&&
1520 TYPE_CODE (SYMBOL_TYPE (real_sym
)) == TYPE_CODE_PTR
&&
1521 TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (real_sym
))) != 0)
1523 register char *name
= SYMBOL_NAME (real_sym
);
1524 register int hash
= hashname (name
);
1525 register struct symbol
*sym
, *prev
;
1528 for (sym
= opaque_type_chain
[hash
]; sym
;)
1530 if (name
[0] == SYMBOL_NAME (sym
)[0] &&
1531 !strcmp (name
+ 1, SYMBOL_NAME (sym
) + 1))
1535 SYMBOL_VALUE_CHAIN (prev
) = SYMBOL_VALUE_CHAIN (sym
);
1539 opaque_type_chain
[hash
] = SYMBOL_VALUE_CHAIN (sym
);
1542 patch_type (SYMBOL_TYPE (sym
), SYMBOL_TYPE (real_sym
));
1546 sym
= SYMBOL_VALUE_CHAIN (prev
);
1550 sym
= opaque_type_chain
[hash
];
1556 sym
= SYMBOL_VALUE_CHAIN (sym
);
1563 static struct symbol
*
1564 process_coff_symbol (cs
, aux
, objfile
)
1565 register struct coff_symbol
*cs
;
1566 register union internal_auxent
*aux
;
1567 struct objfile
*objfile
;
1569 register struct symbol
*sym
1570 = (struct symbol
*) obstack_alloc (&objfile
->symbol_obstack
, sizeof (struct symbol
));
1572 #ifdef NAMES_HAVE_UNDERSCORE
1577 struct type
*temptype
;
1579 bzero (sym
, sizeof (struct symbol
));
1581 name
= (name
[0] == '_' ? name
+ offset
: name
);
1582 SYMBOL_NAME (sym
) = obstack_copy0 (&objfile
->symbol_obstack
, name
, strlen (name
));
1584 /* default assumptions */
1585 SYMBOL_VALUE (sym
) = cs
->c_value
;
1586 SYMBOL_NAMESPACE (sym
) = VAR_NAMESPACE
;
1588 if (ISFCN (cs
->c_type
))
1591 /* FIXME: This has NOT been tested. The DBX version has.. */
1592 /* Generate a template for the type of this function. The
1593 types of the arguments will be added as we read the symbol
1595 struct type
*new = (struct type
*)
1596 obstack_alloc (&objfile
->symbol_obstack
, sizeof (struct type
));
1598 bcopy(lookup_function_type (decode_function_type (cs
, cs
->c_type
, aux
)),
1599 new, sizeof(struct type
));
1600 SYMBOL_TYPE (sym
) = new;
1601 in_function_type
= SYMBOL_TYPE(sym
);
1604 lookup_function_type (decode_function_type (cs
, cs
->c_type
, aux
));
1607 SYMBOL_CLASS (sym
) = LOC_BLOCK
;
1608 if (cs
->c_sclass
== C_STAT
)
1609 coff_add_symbol_to_list (sym
, &coff_file_symbols
);
1610 else if (cs
->c_sclass
== C_EXT
)
1611 coff_add_symbol_to_list (sym
, &coff_global_symbols
);
1615 SYMBOL_TYPE (sym
) = decode_type (cs
, cs
->c_type
, aux
);
1616 switch (cs
->c_sclass
)
1622 SYMBOL_CLASS (sym
) = LOC_LOCAL
;
1623 coff_add_symbol_to_list (sym
, &coff_local_symbols
);
1627 SYMBOL_CLASS (sym
) = LOC_STATIC
;
1628 SYMBOL_VALUE_ADDRESS (sym
) = (CORE_ADDR
) cs
->c_value
;
1629 coff_add_symbol_to_list (sym
, &coff_global_symbols
);
1633 SYMBOL_CLASS (sym
) = LOC_STATIC
;
1634 SYMBOL_VALUE_ADDRESS (sym
) = (CORE_ADDR
) cs
->c_value
;
1635 if (within_function
) {
1636 /* Static symbol of local scope */
1637 coff_add_symbol_to_list (sym
, &coff_local_symbols
);
1640 /* Static symbol at top level of file */
1641 coff_add_symbol_to_list (sym
, &coff_file_symbols
);
1645 #ifdef C_GLBLREG /* AMD coff */
1649 SYMBOL_CLASS (sym
) = LOC_REGISTER
;
1650 SYMBOL_VALUE (sym
) = SDB_REG_TO_REGNUM(cs
->c_value
);
1651 coff_add_symbol_to_list (sym
, &coff_local_symbols
);
1658 SYMBOL_CLASS (sym
) = LOC_ARG
;
1660 /* FIXME: This has not been tested. */
1661 /* Add parameter to function. */
1662 add_param_to_type(&in_function_type
,sym
);
1664 coff_add_symbol_to_list (sym
, &coff_local_symbols
);
1665 #if !defined (BELIEVE_PCC_PROMOTION)
1666 /* If PCC says a parameter is a short or a char,
1667 it is really an int. */
1668 temptype
= lookup_fundamental_type (current_objfile
, FT_INTEGER
);
1669 if (TYPE_LENGTH (SYMBOL_TYPE (sym
)) < TYPE_LENGTH (temptype
)
1670 && TYPE_CODE (SYMBOL_TYPE (sym
)) == TYPE_CODE_INT
)
1672 SYMBOL_TYPE (sym
) = TYPE_UNSIGNED (SYMBOL_TYPE (sym
))
1673 ? lookup_fundamental_type (current_objfile
,
1674 FT_UNSIGNED_INTEGER
)
1681 SYMBOL_CLASS (sym
) = LOC_REGPARM
;
1682 SYMBOL_VALUE (sym
) = SDB_REG_TO_REGNUM(cs
->c_value
);
1683 coff_add_symbol_to_list (sym
, &coff_local_symbols
);
1684 #if !defined (BELIEVE_PCC_PROMOTION)
1685 /* If PCC says a parameter is a short or a char,
1686 it is really an int. */
1687 temptype
= lookup_fundamental_type (current_objfile
, FT_INTEGER
);
1688 if (TYPE_LENGTH (SYMBOL_TYPE (sym
)) < TYPE_LENGTH (temptype
)
1689 && TYPE_CODE (SYMBOL_TYPE (sym
)) == TYPE_CODE_INT
)
1691 SYMBOL_TYPE (sym
) = TYPE_UNSIGNED (SYMBOL_TYPE (sym
))
1692 ? lookup_fundamental_type (current_objfile
,
1693 FT_UNSIGNED_INTEGER
)
1700 SYMBOL_CLASS (sym
) = LOC_TYPEDEF
;
1701 SYMBOL_NAMESPACE (sym
) = VAR_NAMESPACE
;
1703 /* If type has no name, give it one */
1704 if (TYPE_NAME (SYMBOL_TYPE (sym
)) == 0)
1705 TYPE_NAME (SYMBOL_TYPE (sym
)) = concat (SYMBOL_NAME (sym
), NULL
);
1707 /* Keep track of any type which points to empty structured type,
1708 so it can be filled from a definition from another file. A
1709 simple forward reference (TYPE_CODE_UNDEF) is not an
1710 empty structured type, though; the forward references
1711 work themselves out via the magic of coff_lookup_type. */
1712 if (TYPE_CODE (SYMBOL_TYPE (sym
)) == TYPE_CODE_PTR
&&
1713 TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym
))) == 0 &&
1714 TYPE_CODE (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym
))) !=
1717 register int i
= hashname (SYMBOL_NAME (sym
));
1719 SYMBOL_VALUE_CHAIN (sym
) = opaque_type_chain
[i
];
1720 opaque_type_chain
[i
] = sym
;
1722 coff_add_symbol_to_list (sym
, &coff_file_symbols
);
1728 SYMBOL_CLASS (sym
) = LOC_TYPEDEF
;
1729 SYMBOL_NAMESPACE (sym
) = STRUCT_NAMESPACE
;
1730 if (TYPE_NAME (SYMBOL_TYPE (sym
)) == 0)
1731 TYPE_NAME (SYMBOL_TYPE (sym
))
1733 (cs
->c_sclass
== C_ENTAG
1735 : (cs
->c_sclass
== C_STRTAG
1736 ? "struct " : "union ")),
1737 SYMBOL_NAME (sym
), NULL
);
1738 coff_add_symbol_to_list (sym
, &coff_file_symbols
);
1748 /* Decode a coff type specifier;
1749 return the type that is meant. */
1753 decode_type (cs
, c_type
, aux
)
1754 register struct coff_symbol
*cs
;
1755 unsigned int c_type
;
1756 register union internal_auxent
*aux
;
1758 register struct type
*type
= 0;
1759 unsigned int new_c_type
;
1761 if (c_type
& ~N_BTMASK
)
1763 new_c_type
= DECREF (c_type
);
1766 type
= decode_type (cs
, new_c_type
, aux
);
1767 type
= lookup_pointer_type (type
);
1769 else if (ISFCN (c_type
))
1771 type
= decode_type (cs
, new_c_type
, aux
);
1772 type
= lookup_function_type (type
);
1774 else if (ISARY (c_type
))
1777 register unsigned short *dim
;
1778 struct type
*base_type
;
1780 /* Define an array type. */
1781 /* auxent refers to array, not base type */
1782 if (aux
->x_sym
.x_tagndx
.l
== 0)
1785 /* shift the indices down */
1786 dim
= &aux
->x_sym
.x_fcnary
.x_ary
.x_dimen
[0];
1789 for (i
= 0; *dim
&& i
< DIMNUM
- 1; i
++, dim
++)
1793 type
= (struct type
*)
1794 obstack_alloc (¤t_objfile
-> type_obstack
,
1795 sizeof (struct type
));
1796 bzero (type
, sizeof (struct type
));
1797 TYPE_OBJFILE (type
) = current_objfile
;
1799 base_type
= decode_type (cs
, new_c_type
, aux
);
1801 TYPE_CODE (type
) = TYPE_CODE_ARRAY
;
1802 TYPE_TARGET_TYPE (type
) = base_type
;
1803 TYPE_LENGTH (type
) = n
* TYPE_LENGTH (base_type
);
1808 /* Reference to existing type. This only occurs with the
1809 struct, union, and enum types. EPI a29k coff
1810 fakes us out by producing aux entries with a nonzero
1811 x_tagndx for definitions of structs, unions, and enums, so we
1812 have to check the c_sclass field. */
1813 if (cs
->c_naux
> 0 && aux
->x_sym
.x_tagndx
.l
!= 0)
1815 if (cs
->c_sclass
!= C_STRTAG
1816 && cs
->c_sclass
!= C_UNTAG
1817 && cs
->c_sclass
!= C_ENTAG
)
1819 type
= coff_alloc_type (aux
->x_sym
.x_tagndx
.l
);
1822 complain (&tagndx_bad_complaint
, cs
->c_name
);
1823 /* And fall through to decode_base_type... */
1827 return decode_base_type (cs
, BTYPE (c_type
), aux
);
1830 /* Decode a coff type specifier for function definition;
1831 return the type that the function returns. */
1835 decode_function_type (cs
, c_type
, aux
)
1836 register struct coff_symbol
*cs
;
1837 unsigned int c_type
;
1838 register union internal_auxent
*aux
;
1840 if (aux
->x_sym
.x_tagndx
.l
== 0)
1841 cs
->c_naux
= 0; /* auxent refers to function, not base type */
1843 return decode_type (cs
, DECREF (c_type
), aux
);
1850 decode_base_type (cs
, c_type
, aux
)
1851 register struct coff_symbol
*cs
;
1852 unsigned int c_type
;
1853 register union internal_auxent
*aux
;
1860 /* shows up with "void (*foo)();" structure members */
1861 return lookup_fundamental_type (current_objfile
, FT_VOID
);
1864 /* DGUX actually defines both T_ARG and T_VOID to the same value. */
1867 /* Shows up in DGUX, I think. Not sure where. */
1868 return lookup_fundamental_type (current_objfile
, FT_VOID
); /* shouldn't show up here */
1874 /* Intel 960 COFF has this symbol and meaning. */
1875 return lookup_fundamental_type (current_objfile
, FT_VOID
);
1879 return lookup_fundamental_type (current_objfile
, FT_CHAR
);
1882 return lookup_fundamental_type (current_objfile
, FT_SHORT
);
1885 return lookup_fundamental_type (current_objfile
, FT_INTEGER
);
1888 return lookup_fundamental_type (current_objfile
, FT_LONG
);
1891 return lookup_fundamental_type (current_objfile
, FT_FLOAT
);
1894 return lookup_fundamental_type (current_objfile
, FT_DBL_PREC_FLOAT
);
1897 if (cs
->c_naux
!= 1)
1899 /* anonymous structure type */
1900 type
= coff_alloc_type (cs
->c_symnum
);
1901 TYPE_CODE (type
) = TYPE_CODE_STRUCT
;
1902 TYPE_NAME (type
) = concat ("struct ", "<opaque>", NULL
);
1903 INIT_CPLUS_SPECIFIC(type
);
1904 TYPE_LENGTH (type
) = 0;
1905 TYPE_FIELDS (type
) = 0;
1906 TYPE_NFIELDS (type
) = 0;
1910 type
= coff_read_struct_type (cs
->c_symnum
,
1911 aux
->x_sym
.x_misc
.x_lnsz
.x_size
,
1912 aux
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
);
1917 if (cs
->c_naux
!= 1)
1919 /* anonymous union type */
1920 type
= coff_alloc_type (cs
->c_symnum
);
1921 TYPE_NAME (type
) = concat ("union ", "<opaque>", NULL
);
1922 INIT_CPLUS_SPECIFIC(type
);
1923 TYPE_LENGTH (type
) = 0;
1924 TYPE_LENGTH (type
) = 0;
1925 TYPE_FIELDS (type
) = 0;
1926 TYPE_NFIELDS (type
) = 0;
1930 type
= coff_read_struct_type (cs
->c_symnum
,
1931 aux
->x_sym
.x_misc
.x_lnsz
.x_size
,
1932 aux
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
);
1934 TYPE_CODE (type
) = TYPE_CODE_UNION
;
1938 return coff_read_enum_type (cs
->c_symnum
,
1939 aux
->x_sym
.x_misc
.x_lnsz
.x_size
,
1940 aux
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
);
1943 /* shouldn't show up here */
1947 return lookup_fundamental_type (current_objfile
, FT_UNSIGNED_CHAR
);
1950 return lookup_fundamental_type (current_objfile
, FT_UNSIGNED_SHORT
);
1953 return lookup_fundamental_type (current_objfile
, FT_UNSIGNED_INTEGER
);
1956 return lookup_fundamental_type (current_objfile
, FT_UNSIGNED_LONG
);
1958 complain (&unexpected_type_complaint
, cs
->c_name
);
1959 return lookup_fundamental_type (current_objfile
, FT_VOID
);
1962 /* This page contains subroutines of read_type. */
1964 /* Read the description of a structure (or union type)
1965 and return an object describing the type. */
1967 static struct type
*
1968 coff_read_struct_type (index
, length
, lastsym
)
1975 struct nextfield
*next
;
1979 register struct type
*type
;
1980 register struct nextfield
*list
= 0;
1981 struct nextfield
*new;
1985 #ifdef NAMES_HAVE_UNDERSCORE
1990 struct coff_symbol member_sym
;
1991 register struct coff_symbol
*ms
= &member_sym
;
1992 struct internal_syment sub_sym
;
1993 union internal_auxent sub_aux
;
1996 type
= coff_alloc_type (index
);
1997 TYPE_CODE (type
) = TYPE_CODE_STRUCT
;
1998 INIT_CPLUS_SPECIFIC(type
);
1999 TYPE_LENGTH (type
) = length
;
2001 while (!done
&& symnum
< lastsym
&& symnum
< nlist_nsyms_global
)
2003 read_one_sym (ms
, &sub_sym
, &sub_aux
);
2005 name
= (name
[0] == '_' ? name
+ offset
: name
);
2007 switch (ms
->c_sclass
)
2012 /* Get space to record the next field's data. */
2013 new = (struct nextfield
*) alloca (sizeof (struct nextfield
));
2017 /* Save the data. */
2018 list
->field
.name
= savestring (name
, strlen (name
));
2019 list
->field
.type
= decode_type (ms
, ms
->c_type
, &sub_aux
);
2020 list
->field
.bitpos
= 8 * ms
->c_value
;
2021 list
->field
.bitsize
= 0;
2027 /* Get space to record the next field's data. */
2028 new = (struct nextfield
*) alloca (sizeof (struct nextfield
));
2032 /* Save the data. */
2033 list
->field
.name
= savestring (name
, strlen (name
));
2034 list
->field
.type
= decode_type (ms
, ms
->c_type
, &sub_aux
);
2035 list
->field
.bitpos
= ms
->c_value
;
2036 list
->field
.bitsize
= sub_aux
.x_sym
.x_misc
.x_lnsz
.x_size
;
2045 /* Now create the vector of fields, and record how big it is. */
2047 TYPE_NFIELDS (type
) = nfields
;
2048 TYPE_FIELDS (type
) = (struct field
*)
2049 obstack_alloc (¤t_objfile
-> type_obstack
,
2050 sizeof (struct field
) * nfields
);
2052 /* Copy the saved-up fields into the field vector. */
2054 for (n
= nfields
; list
; list
= list
->next
)
2055 TYPE_FIELD (type
, --n
) = list
->field
;
2060 /* Read a definition of an enumeration type,
2061 and create and return a suitable type object.
2062 Also defines the symbols that represent the values of the type. */
2063 /* Currently assumes it's sizeof (int) and doesn't use length. */
2066 static struct type
*
2067 coff_read_enum_type (index
, length
, lastsym
)
2072 register struct symbol
*sym
;
2073 register struct type
*type
;
2076 struct coff_pending
**symlist
;
2077 struct coff_symbol member_sym
;
2078 register struct coff_symbol
*ms
= &member_sym
;
2079 struct internal_syment sub_sym
;
2080 union internal_auxent sub_aux
;
2081 struct coff_pending
*osyms
, *syms
;
2084 #ifdef NAMES_HAVE_UNDERSCORE
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
= (name
[0] == '_' ? name
+ offset
: name
);
2103 switch (ms
->c_sclass
)
2106 sym
= (struct symbol
*) xmalloc (sizeof (struct symbol
));
2107 bzero (sym
, 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. */
2128 TYPE_LENGTH (type
) = TARGET_INT_BIT
/ TARGET_CHAR_BIT
;
2129 TYPE_CODE (type
) = TYPE_CODE_ENUM
;
2130 TYPE_NFIELDS (type
) = nsyms
;
2131 TYPE_FIELDS (type
) = (struct field
*)
2132 obstack_alloc (¤t_objfile
-> type_obstack
,
2133 sizeof (struct field
) * nsyms
);
2135 /* Find the symbols for the values and put them into the type.
2136 The symbols can be found in the symlist that we put them on
2137 to cause them to be defined. osyms contains the old value
2138 of that symlist; everything up to there was defined by us. */
2140 for (syms
= *symlist
, n
= nsyms
; syms
!= osyms
; syms
= syms
->next
)
2142 SYMBOL_TYPE (syms
->symbol
) = type
;
2143 TYPE_FIELD_NAME (type
, --n
) = SYMBOL_NAME (syms
->symbol
);
2144 TYPE_FIELD_VALUE (type
, n
) = 0;
2145 TYPE_FIELD_BITPOS (type
, n
) = SYMBOL_VALUE (syms
->symbol
);
2146 TYPE_FIELD_BITSIZE (type
, n
) = 0;
2148 /* Is this Modula-2's BOOLEAN type? Flag it as such if so. */
2149 if(TYPE_NFIELDS(type
) == 2 &&
2150 ((!strcmp(TYPE_FIELD_NAME(type
,0),"TRUE") &&
2151 !strcmp(TYPE_FIELD_NAME(type
,1),"FALSE")) ||
2152 (!strcmp(TYPE_FIELD_NAME(type
,1),"TRUE") &&
2153 !strcmp(TYPE_FIELD_NAME(type
,0),"FALSE"))))
2154 TYPE_CODE(type
) = TYPE_CODE_BOOL
;
2158 /* Register our ability to parse symbols for coff BFD files */
2160 static struct sym_fns coff_sym_fns
=
2162 "coff", /* sym_name: name or name prefix of BFD target type */
2163 4, /* sym_namelen: number of significant sym_name chars */
2164 coff_new_init
, /* sym_new_init: init anything gbl to entire symtab */
2165 coff_symfile_init
, /* sym_init: read initial info, setup for sym_read() */
2166 coff_symfile_read
, /* sym_read: read a symbol file into symtab */
2167 coff_symfile_finish
, /* sym_finish: finished with file, cleanup */
2168 NULL
/* next: pointer to next struct sym_fns */
2172 _initialize_coffread ()
2174 add_symtab_fns(&coff_sym_fns
);