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 */
31 #include "coff-m88k.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 index in the symbol table of the last coff symbol that was processed. */
103 /* Vector of types defined so far, indexed by their coff symnum. */
105 static struct type
**type_vector
;
107 /* Number of elements allocated for type_vector currently. */
109 static int type_vector_length
;
111 /* Vector of line number information. */
113 static struct linetable
*line_vector
;
115 /* Index of next entry to go in line_vector_index. */
117 static int line_vector_index
;
119 /* Last line number recorded in the line vector. */
121 static int prev_line_number
;
123 /* Number of elements allocated for line_vector currently. */
125 static int line_vector_length
;
127 /* Pointers to scratch storage, used for reading raw symbols and auxents. */
129 static char *temp_sym
;
130 static char *temp_aux
;
132 /* Local variables that hold the shift and mask values for the
133 COFF file that we are currently reading. These come back to us
134 from BFD, and are referenced by their macro names, as well as
135 internally to the BTYPE, ISPTR, ISFCN, ISARY, ISTAG, and DECREF
136 macros from ../internalcoff.h . */
138 static unsigned local_n_btmask
;
139 static unsigned local_n_btshft
;
140 static unsigned local_n_tmask
;
141 static unsigned local_n_tshift
;
143 #define N_BTMASK local_n_btmask
144 #define N_BTSHFT local_n_btshft
145 #define N_TMASK local_n_tmask
146 #define N_TSHIFT local_n_tshift
148 /* Local variables that hold the sizes in the file of various COFF structures.
149 (We only need to know this to read them from the file -- BFD will then
150 translate the data in them, into `internal_xxx' structs in the right
151 byte order, alignment, etc.) */
153 static unsigned local_linesz
;
154 static unsigned local_symesz
;
155 static unsigned local_auxesz
;
161 int int_sem_val
= 's' << 24 | 'e' << 16 | 'm' << 8 | '.';
163 int last_coffsem
= 2;
165 /* This isn't used currently. */
166 int last_coffsyn
= 0;
168 int debug_info
= 0; /*used by tdesc */
169 extern dc_dcontext_t tdesc_handle
;
170 extern int safe_to_init_tdesc_context
;
173 /* Chain of typedefs of pointers to empty struct/union types.
174 They are chained thru the SYMBOL_VALUE_CHAIN. */
177 static struct symbol
*opaque_type_chain
[HASHSIZE
];
179 /* Record the symbols defined for each context in a list.
180 We don't create a struct block for the context until we
181 know how long to make it. */
185 struct pending
*next
;
186 struct symbol
*symbol
;
189 /* Here are the three lists that symbols are put on. */
191 struct pending
*file_symbols
; /* static at top level, and types */
193 struct pending
*global_symbols
; /* global functions and variables */
195 struct pending
*local_symbols
; /* everything local to lexical context */
197 /* List of unclosed lexical contexts
198 (that will become blocks, eventually). */
202 struct context_stack
*next
;
203 struct pending
*locals
;
204 struct pending_block
*old_blocks
;
206 CORE_ADDR start_addr
;
210 struct context_stack
*context_stack
;
212 /* Nonzero if within a function (so symbols should be local,
213 if nothing says specifically). */
218 /* The type of the function we are currently reading in. This is
219 used by define_symbol to record the type of arguments to a function. */
221 struct type
*in_function_type
;
224 /* List of blocks already made (lexical contexts already closed).
225 This is used at the end to make the blockvector. */
229 struct pending_block
*next
;
233 struct pending_block
*pending_blocks
;
235 extern CORE_ADDR startup_file_start
; /* From blockframe.c */
236 extern CORE_ADDR startup_file_end
; /* From blockframe.c */
238 /* Complaints about various problems in the file being read */
240 struct complaint ef_complaint
=
241 {"Unmatched .ef symbol(s) ignored starting at symnum %d", 0, 0};
243 struct complaint no_aux_complaint
=
244 {"symbol %d without one aux entry", 0, 0};
246 struct complaint lineno_complaint
=
247 {"Line number pointer %d lower than start of line numbers", 0, 0};
250 /* Look up a coff type-number index. Return the address of the slot
251 where the type for that index is stored.
252 The type-number is in INDEX.
254 This can be used for finding the type associated with that index
255 or for associating a new type with the index. */
257 static struct type
**
258 coff_lookup_type (index
)
261 if (index
>= type_vector_length
)
263 int old_vector_length
= type_vector_length
;
265 type_vector_length
*= 2;
266 if (type_vector_length
< index
) {
267 type_vector_length
= index
* 2;
269 type_vector
= (struct type
**)
270 xrealloc (type_vector
, type_vector_length
* sizeof (struct type
*));
271 bzero (&type_vector
[old_vector_length
],
272 (type_vector_length
- old_vector_length
) * sizeof(struct type
*));
274 return &type_vector
[index
];
277 /* Make sure there is a type allocated for type number index
278 and return the type object.
279 This can create an empty (zeroed) type object. */
282 coff_alloc_type (index
)
285 register struct type
**type_addr
= coff_lookup_type (index
);
286 register struct type
*type
= *type_addr
;
288 /* If we are referring to a type not known at all yet,
289 allocate an empty type for it.
290 We will fill it in later if we find out how. */
293 type
= (struct type
*) obstack_alloc (symbol_obstack
,
294 sizeof (struct type
));
295 bzero (type
, sizeof (struct type
));
296 TYPE_VPTR_FIELDNO (type
) = -1;
302 /* maintain the lists of symbols and blocks */
304 /* Add a symbol to one of the lists of symbols. */
306 add_symbol_to_list (symbol
, listhead
)
307 struct symbol
*symbol
;
308 struct pending
**listhead
;
310 register struct pending
*link
311 = (struct pending
*) xmalloc (sizeof (struct pending
));
313 link
->next
= *listhead
;
314 link
->symbol
= symbol
;
318 /* Take one of the lists of symbols and make a block from it.
319 Put the block on the list of pending blocks. */
322 finish_block (symbol
, listhead
, old_blocks
, start
, end
)
323 struct symbol
*symbol
;
324 struct pending
**listhead
;
325 struct pending_block
*old_blocks
;
326 CORE_ADDR start
, end
;
328 register struct pending
*next
, *next1
;
329 register struct block
*block
;
330 register struct pending_block
*pblock
;
331 struct pending_block
*opblock
;
334 /* Count the length of the list of symbols. */
336 for (next
= *listhead
, i
= 0; next
; next
= next
->next
, i
++);
338 block
= (struct block
*)
339 obstack_alloc (symbol_obstack
, sizeof (struct block
) + (i
- 1) * sizeof (struct symbol
*));
341 /* Copy the symbols into the block. */
343 BLOCK_NSYMS (block
) = i
;
344 for (next
= *listhead
; next
; next
= next
->next
)
345 BLOCK_SYM (block
, --i
) = next
->symbol
;
347 BLOCK_START (block
) = start
;
348 BLOCK_END (block
) = end
;
349 BLOCK_SUPERBLOCK (block
) = 0; /* Filled in when containing block is made */
351 /* Put the block in as the value of the symbol that names it. */
355 SYMBOL_BLOCK_VALUE (symbol
) = block
;
356 BLOCK_FUNCTION (block
) = symbol
;
359 BLOCK_FUNCTION (block
) = 0;
361 /* Now free the links of the list, and empty the list. */
363 for (next
= *listhead
; next
; next
= next1
)
370 /* Install this block as the superblock
371 of all blocks made since the start of this scope
372 that don't have superblocks yet. */
375 for (pblock
= pending_blocks
; pblock
!= old_blocks
; pblock
= pblock
->next
)
377 if (BLOCK_SUPERBLOCK (pblock
->block
) == 0)
378 BLOCK_SUPERBLOCK (pblock
->block
) = block
;
382 /* Record this block on the list of all blocks in the file.
383 Put it after opblock, or at the beginning if opblock is 0.
384 This puts the block in the list after all its subblocks. */
386 pblock
= (struct pending_block
*) xmalloc (sizeof (struct pending_block
));
387 pblock
->block
= block
;
390 pblock
->next
= opblock
->next
;
391 opblock
->next
= pblock
;
395 pblock
->next
= pending_blocks
;
396 pending_blocks
= pblock
;
400 static struct blockvector
*
403 register struct pending_block
*next
, *next1
;
404 register struct blockvector
*blockvector
;
407 /* Count the length of the list of blocks. */
409 for (next
= pending_blocks
, i
= 0; next
; next
= next
->next
, i
++);
411 blockvector
= (struct blockvector
*)
412 obstack_alloc (symbol_obstack
, sizeof (struct blockvector
) + (i
- 1) * sizeof (struct block
*));
414 /* Copy the blocks into the blockvector.
415 This is done in reverse order, which happens to put
416 the blocks into the proper order (ascending starting address).
417 finish_block has hair to insert each block into the list
418 after its subblocks in order to make sure this is true. */
420 BLOCKVECTOR_NBLOCKS (blockvector
) = i
;
421 for (next
= pending_blocks
; next
; next
= next
->next
)
422 BLOCKVECTOR_BLOCK (blockvector
, --i
) = next
->block
;
424 /* Now free the links of the list, and empty the list. */
426 for (next
= pending_blocks
; next
; next
= next1
)
436 /* Manage the vector of line numbers. */
439 record_line (line
, pc
)
443 struct linetable_entry
*e
;
444 /* Make sure line vector is big enough. */
446 if (line_vector_index
+ 2 >= line_vector_length
)
448 line_vector_length
*= 2;
449 line_vector
= (struct linetable
*)
450 xrealloc (line_vector
, sizeof (struct linetable
)
451 + (line_vector_length
452 * sizeof (struct linetable_entry
)));
455 e
= line_vector
->item
+ line_vector_index
++;
456 e
->line
= line
; e
->pc
= pc
;
459 /* Start a new symtab for a new source file.
460 This is called when a COFF ".file" symbol is seen;
461 it indicates the start of data for one original source file. */
470 last_source_file
= 0;
474 /* This isn't used currently. */
479 /* Initialize the source file line number information for this file. */
481 if (line_vector
) /* Unlikely, but maybe possible? */
483 line_vector_index
= 0;
484 line_vector_length
= 1000;
485 prev_line_number
= -2; /* Force first line number to be explicit */
486 line_vector
= (struct linetable
*)
487 xmalloc (sizeof (struct linetable
)
488 + line_vector_length
* sizeof (struct linetable_entry
));
491 /* Save the vital information from when starting to read a file,
492 for use when closing off the current file.
493 NAME is the file name the symbols came from, START_ADDR is the first
494 text address for the file, and SIZE is the number of bytes of text. */
497 complete_symtab (name
, start_addr
, size
)
499 CORE_ADDR start_addr
;
502 last_source_file
= savestring (name
, strlen (name
));
503 cur_src_start_addr
= start_addr
;
504 cur_src_end_addr
= start_addr
+ size
;
506 if (entry_point
< cur_src_end_addr
507 && entry_point
>= cur_src_start_addr
)
509 startup_file_start
= cur_src_start_addr
;
510 startup_file_end
= cur_src_end_addr
;
514 /* Finish the symbol definitions for one main source file,
515 close off all the lexical contexts for that file
516 (creating struct block's for them), then make the
517 struct symtab for that file and put it in the list of all such. */
522 register struct symtab
*symtab
;
523 register struct context_stack
*cstk
;
524 register struct blockvector
*blockvector
;
525 register struct linetable
*lv
;
527 /* Finish the lexical context of the last function in the file. */
531 cstk
= context_stack
;
533 /* Make a block for the local symbols within. */
534 finish_block (cstk
->name
, &local_symbols
, cstk
->old_blocks
,
535 cstk
->start_addr
, cur_src_end_addr
);
539 /* Ignore a file that has no functions with real debugging info. */
540 if (pending_blocks
== 0 && file_symbols
== 0 && global_symbols
== 0)
544 line_vector_length
= -1;
545 last_source_file
= 0;
549 /* Create the two top-level blocks for this file (STATIC_BLOCK and
551 finish_block (0, &file_symbols
, 0, cur_src_start_addr
, cur_src_end_addr
);
552 finish_block (0, &global_symbols
, 0, cur_src_start_addr
, cur_src_end_addr
);
554 /* Create the blockvector that points to all the file's blocks. */
555 blockvector
= make_blockvector ();
557 /* Now create the symtab object for this source file. */
558 symtab
= allocate_symtab (last_source_file
);
560 /* Fill in its components. */
561 symtab
->blockvector
= blockvector
;
562 symtab
->free_code
= free_linetable
;
563 symtab
->free_ptr
= 0;
564 symtab
->filename
= last_source_file
;
565 symtab
->dirname
= NULL
;
567 lv
->nitems
= line_vector_index
;
568 symtab
->linetable
= (struct linetable
*)
569 xrealloc (lv
, (sizeof (struct linetable
)
570 + lv
->nitems
* sizeof (struct linetable_entry
)));
573 symtab
->coffsem
= last_coffsem
;
575 /* This isn't used currently. Besides, if this is really about "syntax",
576 it shouldn't need to stick around past symbol read-in time. */
577 symtab
->coffsyn
= last_coffsyn
;
581 free_named_symtabs (symtab
->filename
);
583 /* Link the new symtab into the list of such. */
584 symtab
->next
= symtab_list
;
585 symtab_list
= symtab
;
587 /* Reinitialize for beginning of new file. */
589 line_vector_length
= -1;
590 last_source_file
= 0;
594 record_misc_function (name
, address
)
599 /* We don't want TDESC entry points on the misc_function_vector */
600 if (name
[0] == '@') return;
602 /* mf_text isn't true, but apparently COFF doesn't tell us what it really
603 is, so this guess is more useful than mf_unknown. */
604 prim_record_misc_function (savestring (name
, strlen (name
)),
609 /* coff_symfile_init ()
610 is the coff-specific initialization routine for reading symbols.
611 It is passed a struct sym_fns which contains, among other things,
612 the BFD for the file whose symbols are being read, and a slot for
613 a pointer to "private data" which we fill with cookies and other
614 treats for coff_symfile_read ().
616 We will only be called if this is a COFF or COFF-like file.
617 BFD handles figuring out the format of the file, and code in symtab.c
618 uses BFD's determination to vector to us.
620 The ultimate result is a new symtab (or, FIXME, eventually a psymtab). */
622 struct coff_symfile_info
{
623 file_ptr min_lineno_offset
; /* Where in file lowest line#s are */
624 file_ptr max_lineno_offset
; /* 1+last byte of line#s in file */
627 static int text_bfd_scnum
;
630 coff_symfile_init (sf
)
634 bfd
*abfd
= sf
->sym_bfd
;
636 /* Allocate struct to keep track of the symfile */
637 /* FIXME memory leak */
638 sf
->sym_private
= xmalloc (sizeof (struct coff_symfile_info
));
641 safe_to_init_tdesc_context
= 0;
644 /* Save startup file's range of PC addresses to help blockframe.c
645 decide where the bottom of the stack is. */
646 if (bfd_get_file_flags (abfd
) & EXEC_P
)
648 /* Executable file -- record its entry point so we'll recognize
649 the startup file because it contains the entry point. */
650 entry_point
= bfd_get_start_address (abfd
);
654 /* Examination of non-executable.o files. Short-circuit this stuff. */
655 /* ~0 will not be in any file, we hope. */
657 /* set the startup file to be an empty range. */
658 startup_file_start
= 0;
659 startup_file_end
= 0;
661 /* Save the section number for the text section */
662 if (section
= bfd_get_section_by_name(abfd
,".text"))
663 text_bfd_scnum
= section
->index
;
668 /* This function is called for every section; it finds the outer limits
669 of the line table (minimum and maximum file offset) so that the
670 mainline code can read the whole thing for efficiency. */
674 find_linenos (abfd
, asect
, vpinfo
)
679 struct coff_symfile_info
*info
;
681 file_ptr offset
, maxoff
;
683 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
684 count
= asect
->lineno_count
;
689 size
= count
* local_linesz
;
691 info
= (struct coff_symfile_info
*)vpinfo
;
692 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
693 offset
= asect
->line_filepos
;
696 if (offset
< info
->min_lineno_offset
|| info
->min_lineno_offset
== 0)
697 info
->min_lineno_offset
= offset
;
699 maxoff
= offset
+ size
;
700 if (maxoff
> info
->max_lineno_offset
)
701 info
->max_lineno_offset
= maxoff
;
703 /* While we're at it, find the debug_info. It's in the s_relptr
704 (or, in BFD-speak, rel_filepos) of the text segment section header. */
705 if (strcmp (bfd_section_name (abfd
, asect
), ".text") == 0)
707 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
708 debug_info
= asect
->rel_filepos
;
712 dc_terminate (tdesc_handle
);
720 /* The BFD for this file -- only good while we're actively reading
721 symbols into a psymtab or a symtab. */
723 static bfd
*symfile_bfd
;
725 /* Read a symbol file, after initialization by coff_symfile_init. */
726 /* FIXME! Addr and Mainline are not used yet -- this will not work for
727 shared libraries or add_file! */
731 coff_symfile_read (sf
, addr
, mainline
)
736 struct coff_symfile_info
*info
= (struct coff_symfile_info
*)sf
->sym_private
;
737 bfd
*abfd
= sf
->sym_bfd
;
738 coff_data_type
*cdata
= coff_data (abfd
);
739 char *name
= bfd_get_filename (abfd
);
744 int stringtab_offset
;
746 symfile_bfd
= abfd
; /* Kludge for swap routines */
748 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
749 desc
= fileno ((FILE *)(abfd
->iostream
)); /* File descriptor */
750 num_symbols
= bfd_get_symcount (abfd
); /* How many syms */
751 symtab_offset
= cdata
->sym_filepos
; /* Symbol table file offset */
752 stringtab_offset
= symtab_offset
+ /* String table file offset */
753 num_symbols
* cdata
->local_symesz
;
755 /* Set a few file-statics that give us specific information about
756 the particular COFF file format we're reading. */
757 local_linesz
= cdata
->local_linesz
;
758 local_n_btmask
= cdata
->local_n_btmask
;
759 local_n_btshft
= cdata
->local_n_btshft
;
760 local_n_tmask
= cdata
->local_n_tmask
;
761 local_n_tshift
= cdata
->local_n_tshift
;
762 local_linesz
= cdata
->local_linesz
;
763 local_symesz
= cdata
->local_symesz
;
764 local_auxesz
= cdata
->local_auxesz
;
766 /* Allocate space for raw symbol and aux entries, based on their
767 space requirements as reported by BFD. */
768 temp_sym
= (char *) xmalloc
769 (cdata
->local_symesz
+ cdata
->local_auxesz
);
770 temp_aux
= temp_sym
+ cdata
->local_symesz
;
771 make_cleanup (free_current_contents
, &temp_sym
);
774 /* Read the line number table, all at once. */
775 info
->min_lineno_offset
= 0;
776 info
->max_lineno_offset
= 0;
777 bfd_map_over_sections (abfd
, find_linenos
, info
);
779 val
= init_lineno (desc
, info
->min_lineno_offset
,
780 info
->max_lineno_offset
- info
->min_lineno_offset
);
782 error ("\"%s\": error reading line numbers\n", name
);
784 /* Now read the string table, all at once. */
786 val
= init_stringtab (desc
, stringtab_offset
);
789 free_all_symtabs (); /* FIXME blows whole symtab */
790 printf ("\"%s\": can't get string table", name
);
794 make_cleanup (free_stringtab
, 0);
796 /* Position to read the symbol table. Do not read it all at once. */
797 val
= lseek (desc
, (long)symtab_offset
, 0);
799 perror_with_name (name
);
801 init_misc_bunches ();
802 make_cleanup (discard_misc_bunches
, 0);
804 /* Now that the executable file is positioned at symbol table,
805 process it and define symbols accordingly. */
807 read_coff_symtab (desc
, num_symbols
);
809 patch_opaque_types ();
811 /* Sort symbols alphabetically within each block. */
813 sort_all_symtab_syms ();
815 /* Go over the misc symbol bunches and install them in vector. */
817 condense_misc_bunches (!mainline
);
819 /* Make a default for file to list. */
821 select_source_symtab (0); /* FIXME, this might be too slow, see dbxread */
827 /* There seems to be nothing to do except free_all_symtabs and set
828 symfile to zero, which is done by our caller. */
831 /* Simplified internal version of coff symbol table information */
835 int c_symnum
; /* symbol number of this entry */
836 int c_naux
; /* 0 if syment only, 1 if syment + auxent, etc */
843 /* Given pointers to a symbol table in coff style exec file,
844 analyze them and create struct symtab's describing the symbols.
845 NSYMS is the number of symbols in the symbol table.
846 We read them one at a time using read_one_sym (). */
849 read_coff_symtab (desc
, nsyms
)
853 int newfd
; /* Avoid multiple closes on same desc */
855 register struct context_stack
*new;
856 struct coff_symbol coff_symbol
;
857 register struct coff_symbol
*cs
= &coff_symbol
;
858 static struct internal_syment main_sym
;
859 static union internal_auxent main_aux
;
860 struct coff_symbol fcn_cs_saved
;
861 static struct internal_syment fcn_sym_saved
;
862 static union internal_auxent fcn_aux_saved
;
864 /* A .file is open. */
865 int in_source_file
= 0;
866 int num_object_files
= 0;
867 int next_file_symnum
= -1;
869 /* Name of the current file. */
870 char *filestring
= "";
876 struct cleanup
*old_chain
;
881 fatal ("Too many open files");
882 stream
= fdopen (newfd
, "r");
884 old_chain
= make_cleanup (free_all_symtabs
, 0);
885 make_cleanup (fclose
, stream
);
886 nlist_stream_global
= stream
;
887 nlist_nsyms_global
= nsyms
;
888 last_source_file
= 0;
889 bzero (opaque_type_chain
, sizeof opaque_type_chain
);
891 if (type_vector
) /* Get rid of previous one */
893 type_vector_length
= 160;
894 type_vector
= (struct type
**)
895 xmalloc (type_vector_length
* sizeof (struct type
*));
896 bzero (type_vector
, type_vector_length
* sizeof (struct type
*));
901 while (symnum
< nsyms
)
903 QUIT
; /* Make this command interruptable. */
904 read_one_sym (cs
, &main_sym
, &main_aux
);
907 temp_sem_val
= cs
->c_name
[0] << 24 | cs
->c_name
[1] << 16 |
908 cs
->c_name
[2] << 8 | cs
->c_name
[3];
909 if (int_sem_val
== temp_sem_val
)
910 last_coffsem
= (int) strtol (cs
->c_name
+4, (char **) NULL
, 10);
913 if (cs
->c_symnum
== next_file_symnum
&& cs
->c_sclass
!= C_FILE
)
915 if (last_source_file
)
919 complete_symtab ("_globals_", 0, first_object_file_end
);
920 /* done with all files, everything from here on out is globals */
923 /* Special case for file with type declarations only, no text. */
924 if (!last_source_file
&& SDB_TYPE (cs
->c_type
)
925 && cs
->c_secnum
== N_DEBUG
)
926 complete_symtab (filestring
, 0, 0);
928 /* Typedefs should not be treated as symbol definitions. */
929 if (ISFCN (cs
->c_type
) && cs
->c_sclass
!= C_TPDEF
)
931 /* record as misc function. if we get '.bf' next,
932 * then we undo this step
934 record_misc_function (cs
->c_name
, cs
->c_value
);
936 fcn_line_ptr
= main_aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
;
937 fcn_start_addr
= cs
->c_value
;
939 fcn_sym_saved
= main_sym
;
940 fcn_aux_saved
= main_aux
;
944 switch (cs
->c_sclass
)
953 printf ("Bad n_sclass = %d\n", cs
->c_sclass
);
958 * c_value field contains symnum of next .file entry in table
959 * or symnum of first global after last .file.
961 next_file_symnum
= cs
->c_value
;
962 filestring
= getfilename (&main_aux
);
964 * Complete symbol table for last object file
965 * containing debugging information.
967 if (last_source_file
)
976 if (cs
->c_name
[0] == '.') {
977 if (strcmp (cs
->c_name
, ".text") == 0) {
978 /* FIXME: don't wire in ".text" as section name
980 if (++num_object_files
== 1) {
981 /* last address of startup file */
982 first_object_file_end
= cs
->c_value
+
983 main_aux
.x_scn
.x_scnlen
;
985 /* Check for in_source_file deals with case of
986 a file with debugging symbols
987 followed by a later file with no symbols. */
989 complete_symtab (filestring
, cs
->c_value
,
990 main_aux
.x_scn
.x_scnlen
);
993 /* flush rest of '.' symbols */
996 else if (!SDB_TYPE (cs
->c_type
)
997 && cs
->c_name
[0] == 'L'
998 && (strncmp (cs
->c_name
, "LI%", 3) == 0
999 || strncmp (cs
->c_name
, "LF%", 3) == 0
1000 || strncmp (cs
->c_name
,"LC%",3) == 0
1001 || strncmp (cs
->c_name
,"LP%",3) == 0
1002 || strncmp (cs
->c_name
,"LPB%",4) == 0
1003 || strncmp (cs
->c_name
,"LBB%",4) == 0
1004 || strncmp (cs
->c_name
,"LBE%",4) == 0
1005 || strncmp (cs
->c_name
,"LPBX%",5) == 0))
1006 /* At least on a 3b1, gcc generates swbeg and string labels
1007 that look like this. Ignore them. */
1009 /* fall in for static symbols that don't start with '.' */
1011 if (!SDB_TYPE (cs
->c_type
)) {
1012 /* FIXME: This is BOGUS Will Robinson!
1013 Coff should provide the SEC_CODE flag for executable sections,
1014 then if we could look up sections by section number we
1015 could see if the flags indicate SEC_CODE. If so, then
1016 record this symbol as a miscellaneous function. But why
1017 are absolute syms recorded as functions, anyway? */
1018 if (cs
->c_secnum
<= text_bfd_scnum
+1) {/* text or abs */
1019 record_misc_function (cs
->c_name
, cs
->c_value
);
1025 (void) process_coff_symbol (cs
, &main_aux
);
1029 if (strcmp (cs
->c_name
, ".bf") == 0)
1031 within_function
= 1;
1033 /* value contains address of first non-init type code */
1034 /* main_aux.x_sym.x_misc.x_lnsz.x_lnno
1035 contains line number of '{' } */
1036 if (cs
->c_naux
!= 1)
1037 complain (no_aux_complaint
, cs
->c_symnum
);
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 if (cs
->c_naux
!= 1) {
1067 complain (no_aux_complaint
, cs
->c_symnum
);
1068 fcn_last_line
= 0x7FFFFFFF;
1070 fcn_last_line
= main_aux
.x_sym
.x_misc
.x_lnsz
.x_lnno
;
1072 enter_linenos (fcn_line_ptr
, fcn_first_line
, fcn_last_line
);
1074 finish_block (new->name
, &local_symbols
, new->old_blocks
,
1076 #if defined (FUNCTION_EPILOGUE_SIZE)
1077 /* This macro should be defined only on
1079 fcn_aux_saved.x_sym.x_misc.x_fsize
1080 field is always zero.
1081 So use the .bf record information that
1082 points to the epilogue and add the size
1084 cs
->c_value
+ FUNCTION_EPILOGUE_SIZE
1086 fcn_cs_saved
.c_value
+
1087 fcn_aux_saved
.x_sym
.x_misc
.x_fsize
1091 within_function
= 0;
1097 if (strcmp (cs
->c_name
, ".bb") == 0)
1099 new = (struct context_stack
*)
1100 xmalloc (sizeof (struct context_stack
));
1103 new->next
= context_stack
;
1104 context_stack
= new;
1105 new->locals
= local_symbols
;
1106 new->old_blocks
= pending_blocks
;
1107 new->start_addr
= cs
->c_value
;
1111 else if (strcmp (cs
->c_name
, ".eb") == 0)
1113 new = context_stack
;
1114 if (new == 0 || depth
!= new->depth
)
1115 error ("Invalid symbol data: .bb/.eb symbol mismatch at symbol %d.",
1117 if (local_symbols
&& context_stack
->next
)
1119 /* Make a block for the local symbols within. */
1120 finish_block (0, &local_symbols
, new->old_blocks
,
1121 new->start_addr
, cs
->c_value
);
1124 local_symbols
= new->locals
;
1125 context_stack
= new->next
;
1132 /* This isn't used currently. */
1133 if (strcmp (cs
->c_name
, ".coffsyn") == 0)
1134 last_coffsyn
= cs
->c_value
;
1137 if ((strcmp (cs
->c_name
, ".coffsem") == 0) &&
1139 last_coffsem
= cs
->c_value
;
1145 if ((strcmp (cs
->c_name
, ".coffsem") == 0) &&
1147 last_coffsem
= cs
->c_value
;
1150 (void) process_coff_symbol (cs
, &main_aux
);
1155 if (last_source_file
)
1158 discard_cleanups (old_chain
);
1161 /* Routines for reading headers and symbols from executable. */
1164 /* Move these XXXMAGIC symbol defns into BFD! */
1166 /* Read COFF file header, check magic number,
1167 and return number of symbols. */
1168 read_file_hdr (chan
, file_hdr
)
1172 lseek (chan
, 0L, 0);
1173 if (myread (chan
, (char *)file_hdr
, FILHSZ
) < 0)
1176 switch (file_hdr
->f_magic
)
1191 #if defined (MC68KWRMAGIC) \
1192 && (!defined (MC68MAGIC) || MC68KWRMAGIC != MC68MAGIC)
1206 case I960ROMAGIC
: /* Intel 960 */
1209 case I960RWMAGIC
: /* Intel 960 */
1211 return file_hdr
->f_nsyms
;
1215 if (BADMAG(file_hdr
))
1218 return file_hdr
->f_nsyms
;
1226 /* Read the next symbol, swap it, and return it in both internal_syment
1227 form, and coff_symbol form. Also return its first auxent, if any,
1228 in internal_auxent form, and skip any other auxents. */
1231 read_one_sym (cs
, sym
, aux
)
1232 register struct coff_symbol
*cs
;
1233 register struct internal_syment
*sym
;
1234 register union internal_auxent
*aux
;
1238 cs
->c_symnum
= symnum
;
1239 fread (temp_sym
, local_symesz
, 1, nlist_stream_global
);
1240 bfd_coff_swap_sym_in (symfile_bfd
, temp_sym
, (char *)sym
);
1241 cs
->c_naux
= sym
->n_numaux
& 0xff;
1242 if (cs
->c_naux
>= 1)
1244 fread (temp_aux
, local_auxesz
, 1, nlist_stream_global
);
1245 bfd_coff_swap_aux_in (symfile_bfd
, temp_aux
, sym
->n_type
, sym
->n_sclass
,
1247 /* If more than one aux entry, read past it (only the first aux
1249 for (i
= 1; i
< cs
->c_naux
; i
++)
1250 fread (temp_aux
, local_auxesz
, 1, nlist_stream_global
);
1252 cs
->c_name
= getsymname (sym
);
1253 cs
->c_value
= sym
->n_value
;
1254 cs
->c_sclass
= (sym
->n_sclass
& 0xff);
1255 cs
->c_secnum
= sym
->n_scnum
;
1256 cs
->c_type
= (unsigned) sym
->n_type
;
1257 if (!SDB_TYPE (cs
->c_type
))
1260 symnum
+= 1 + cs
->c_naux
;
1263 /* Support for string table handling */
1265 static char *stringtab
= NULL
;
1268 init_stringtab (chan
, offset
)
1274 unsigned char lengthbuf
[4];
1282 if (lseek (chan
, offset
, 0) < 0)
1285 val
= myread (chan
, (char *)lengthbuf
, sizeof lengthbuf
);
1286 length
= bfd_h_get_32 (symfile_bfd
, lengthbuf
);
1288 /* If no string table is needed, then the file may end immediately
1289 after the symbols. Just return with `stringtab' set to null. */
1290 if (val
!= sizeof length
|| length
< sizeof length
)
1293 stringtab
= (char *) xmalloc (length
);
1294 if (stringtab
== NULL
)
1297 bcopy (&length
, stringtab
, sizeof length
);
1298 if (length
== sizeof length
) /* Empty table -- just the count */
1301 val
= myread (chan
, stringtab
+ sizeof length
, length
- sizeof length
);
1302 if (val
!= length
- sizeof length
|| stringtab
[length
- 1] != '\0')
1317 getsymname (symbol_entry
)
1318 struct internal_syment
*symbol_entry
;
1320 static char buffer
[SYMNMLEN
+1];
1323 if (symbol_entry
->_n
._n_n
._n_zeroes
== 0)
1325 result
= stringtab
+ symbol_entry
->_n
._n_n
._n_offset
;
1329 strncpy (buffer
, symbol_entry
->_n
._n_name
, SYMNMLEN
);
1330 buffer
[SYMNMLEN
] = '\0';
1337 getfilename (aux_entry
)
1338 union internal_auxent
*aux_entry
;
1340 static char buffer
[BUFSIZ
];
1341 register char *temp
;
1343 extern char *rindex ();
1345 #ifndef COFF_NO_LONG_FILE_NAMES
1346 #if defined (x_zeroes)
1348 if (aux_entry
->x_zeroes
== 0)
1349 strcpy (buffer
, stringtab
+ aux_entry
->x_offset
);
1350 #else /* no x_zeroes */
1351 if (aux_entry
->x_file
.x_n
.x_zeroes
== 0)
1352 strcpy (buffer
, stringtab
+ aux_entry
->x_file
.x_n
.x_offset
);
1353 #endif /* no x_zeroes */
1355 #endif /* COFF_NO_LONG_FILE_NAMES */
1357 #if defined (x_name)
1359 strncpy (buffer
, aux_entry
->x_name
, FILNMLEN
);
1361 strncpy (buffer
, aux_entry
->x_file
.x_fname
, FILNMLEN
);
1363 buffer
[FILNMLEN
] = '\0';
1366 if ((temp
= rindex (result
, '/')) != NULL
)
1371 /* Support for line number handling */
1372 static char *linetab
= NULL
;
1373 static long linetab_offset
;
1374 static unsigned long linetab_size
;
1376 /* Read in all the line numbers for fast lookups later. Leave them in
1377 external (unswapped) format in memory; we'll swap them as we enter
1378 them into GDB's data structures. */
1381 init_lineno (chan
, offset
, size
)
1388 linetab_offset
= offset
;
1389 linetab_size
= size
;
1394 if (lseek (chan
, offset
, 0) < 0)
1397 /* Allocate the desired table, plus a sentinel */
1398 linetab
= (char *) xmalloc (size
+ local_linesz
);
1400 val
= myread (chan
, linetab
, size
);
1404 /* Terminate it with an all-zero sentinel record */
1405 bzero (linetab
+ size
, local_linesz
);
1407 make_cleanup (free
, linetab
); /* Be sure it gets de-allocated. */
1411 #if !defined (L_LNNO32)
1412 #define L_LNNO32(lp) ((lp)->l_lnno)
1416 enter_linenos (file_offset
, first_line
, last_line
)
1418 register int first_line
;
1419 register int last_line
;
1421 register char *rawptr
;
1422 struct internal_lineno lptr
;
1424 if (file_offset
< linetab_offset
)
1426 complain (&lineno_complaint
, file_offset
);
1427 if (file_offset
> linetab_size
) /* Too big to be an offset? */
1429 file_offset
+= linetab_offset
; /* Try reading at that linetab offset */
1432 rawptr
= &linetab
[file_offset
- linetab_offset
];
1434 /* skip first line entry for each function */
1435 rawptr
+= local_linesz
;
1436 /* line numbers start at one for the first line of the function */
1440 bfd_coff_swap_lineno_in (symfile_bfd
, rawptr
, &lptr
);
1441 rawptr
+= local_linesz
;
1442 /* The next function, or the sentinel, will have L_LNNO32 zero; we exit. */
1443 if (L_LNNO32 (&lptr
) && L_LNNO32 (&lptr
) <= last_line
)
1444 record_line (first_line
+ L_LNNO32 (&lptr
), lptr
.l_addr
.l_paddr
);
1454 register char *p
= name
;
1455 register int total
= p
[0];
1468 return total
% HASHSIZE
;
1472 patch_type (type
, real_type
)
1474 struct type
*real_type
;
1476 register struct type
*target
= TYPE_TARGET_TYPE (type
);
1477 register struct type
*real_target
= TYPE_TARGET_TYPE (real_type
);
1478 int field_size
= TYPE_NFIELDS (real_target
) * sizeof (struct field
);
1480 TYPE_LENGTH (target
) = TYPE_LENGTH (real_target
);
1481 TYPE_NFIELDS (target
) = TYPE_NFIELDS (real_target
);
1482 TYPE_FIELDS (target
) = (struct field
*)
1483 obstack_alloc (symbol_obstack
, field_size
);
1485 bcopy (TYPE_FIELDS (real_target
), TYPE_FIELDS (target
), field_size
);
1487 if (TYPE_NAME (real_target
))
1489 if (TYPE_NAME (target
))
1490 free (TYPE_NAME (target
));
1491 TYPE_NAME (target
) = concat (TYPE_NAME (real_target
), "", "");
1495 /* Patch up all appropriate typdef symbols in the opaque_type_chains
1496 so that they can be used to print out opaque data structures properly */
1499 patch_opaque_types ()
1503 /* Look at each symbol in the per-file block of each symtab. */
1504 for (s
= symtab_list
; s
; s
= s
->next
)
1506 register struct block
*b
;
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 register struct symbol
*real_sym
;
1515 /* Find completed typedefs to use to fix opaque ones.
1516 Remove syms from the chain when their types are stored,
1517 but search the whole chain, as there may be several syms
1518 from different files with the same name. */
1519 real_sym
= BLOCK_SYM (b
, i
);
1520 if (SYMBOL_CLASS (real_sym
) == LOC_TYPEDEF
&&
1521 SYMBOL_NAMESPACE (real_sym
) == VAR_NAMESPACE
&&
1522 TYPE_CODE (SYMBOL_TYPE (real_sym
)) == TYPE_CODE_PTR
&&
1523 TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (real_sym
))) != 0)
1525 register char *name
= SYMBOL_NAME (real_sym
);
1526 register int hash
= hashname (name
);
1527 register struct symbol
*sym
, *prev
;
1530 for (sym
= opaque_type_chain
[hash
]; sym
;)
1532 if (name
[0] == SYMBOL_NAME (sym
)[0] &&
1533 !strcmp (name
+ 1, SYMBOL_NAME (sym
) + 1))
1536 SYMBOL_VALUE_CHAIN (prev
) = SYMBOL_VALUE_CHAIN (sym
);
1538 opaque_type_chain
[hash
] = SYMBOL_VALUE_CHAIN (sym
);
1540 patch_type (SYMBOL_TYPE (sym
), SYMBOL_TYPE (real_sym
));
1543 sym
= SYMBOL_VALUE_CHAIN (prev
);
1545 sym
= opaque_type_chain
[hash
];
1550 sym
= SYMBOL_VALUE_CHAIN (sym
);
1558 #if defined (clipper)
1559 #define BELIEVE_PCC_PROMOTION 1
1562 static struct symbol
*
1563 process_coff_symbol (cs
, aux
)
1564 register struct coff_symbol
*cs
;
1565 register union internal_auxent
*aux
;
1567 register struct symbol
*sym
1568 = (struct symbol
*) obstack_alloc (symbol_obstack
, sizeof (struct symbol
));
1570 #ifdef NAMES_HAVE_UNDERSCORE
1576 bzero (sym
, sizeof (struct symbol
));
1578 name
= (name
[0] == '_' ? name
+ offset
: name
);
1579 SYMBOL_NAME (sym
) = obstack_copy0 (symbol_obstack
, name
, strlen (name
));
1581 /* default assumptions */
1582 SYMBOL_VALUE (sym
) = cs
->c_value
;
1583 SYMBOL_NAMESPACE (sym
) = VAR_NAMESPACE
;
1585 if (ISFCN (cs
->c_type
))
1588 /* FIXME: This has NOT been tested. The DBX version has.. */
1589 /* Generate a template for the type of this function. The
1590 types of the arguments will be added as we read the symbol
1592 struct type
*new = (struct type
*)
1593 obstack_alloc (symbol_obstack
, sizeof (struct type
));
1595 bcopy(lookup_function_type (decode_function_type (cs
, cs
->c_type
, aux
)),
1596 new, sizeof(struct type
));
1597 SYMBOL_TYPE (sym
) = new;
1598 in_function_type
= SYMBOL_TYPE(sym
);
1601 lookup_function_type (decode_function_type (cs
, cs
->c_type
, aux
));
1604 SYMBOL_CLASS (sym
) = LOC_BLOCK
;
1605 if (cs
->c_sclass
== C_STAT
)
1606 add_symbol_to_list (sym
, &file_symbols
);
1607 else if (cs
->c_sclass
== C_EXT
)
1608 add_symbol_to_list (sym
, &global_symbols
);
1612 SYMBOL_TYPE (sym
) = decode_type (cs
, cs
->c_type
, aux
);
1613 switch (cs
->c_sclass
)
1619 SYMBOL_CLASS (sym
) = LOC_LOCAL
;
1620 add_symbol_to_list (sym
, &local_symbols
);
1624 SYMBOL_CLASS (sym
) = LOC_STATIC
;
1625 SYMBOL_VALUE_ADDRESS (sym
) = (CORE_ADDR
) cs
->c_value
;
1626 add_symbol_to_list (sym
, &global_symbols
);
1630 SYMBOL_CLASS (sym
) = LOC_STATIC
;
1631 SYMBOL_VALUE_ADDRESS (sym
) = (CORE_ADDR
) cs
->c_value
;
1632 if (within_function
) {
1633 /* Static symbol of local scope */
1634 add_symbol_to_list (sym
, &local_symbols
);
1637 /* Static symbol at top level of file */
1638 add_symbol_to_list (sym
, &file_symbols
);
1642 #ifdef C_GLBLREG /* AMD coff */
1646 SYMBOL_CLASS (sym
) = LOC_REGISTER
;
1647 SYMBOL_VALUE (sym
) = SDB_REG_TO_REGNUM(cs
->c_value
);
1648 add_symbol_to_list (sym
, &local_symbols
);
1655 SYMBOL_CLASS (sym
) = LOC_ARG
;
1657 /* FIXME: This has not bee tested. */
1658 /* Add parameter to function. */
1659 add_param_to_type(&in_function_type
,sym
);
1661 add_symbol_to_list (sym
, &local_symbols
);
1662 #if !defined (BELIEVE_PCC_PROMOTION)
1663 /* If PCC says a parameter is a short or a char,
1664 it is really an int. */
1665 if (SYMBOL_TYPE (sym
) == builtin_type_char
1666 || SYMBOL_TYPE (sym
) == builtin_type_short
)
1667 SYMBOL_TYPE (sym
) = builtin_type_int
;
1668 else if (SYMBOL_TYPE (sym
) == builtin_type_unsigned_char
1669 || SYMBOL_TYPE (sym
) == builtin_type_unsigned_short
)
1670 SYMBOL_TYPE (sym
) = builtin_type_unsigned_int
;
1675 SYMBOL_CLASS (sym
) = LOC_REGPARM
;
1676 SYMBOL_VALUE (sym
) = SDB_REG_TO_REGNUM(cs
->c_value
);
1677 add_symbol_to_list (sym
, &local_symbols
);
1678 #if !defined (BELIEVE_PCC_PROMOTION)
1679 /* If PCC says a parameter is a short or a char,
1680 it is really an int. */
1681 if (SYMBOL_TYPE (sym
) == builtin_type_char
1682 || SYMBOL_TYPE (sym
) == builtin_type_short
)
1683 SYMBOL_TYPE (sym
) = builtin_type_int
;
1684 else if (SYMBOL_TYPE (sym
) == builtin_type_unsigned_char
1685 || SYMBOL_TYPE (sym
) == builtin_type_unsigned_short
)
1686 SYMBOL_TYPE (sym
) = builtin_type_unsigned_int
;
1691 SYMBOL_CLASS (sym
) = LOC_TYPEDEF
;
1692 SYMBOL_NAMESPACE (sym
) = VAR_NAMESPACE
;
1694 /* If type has no name, give it one */
1695 if (TYPE_NAME (SYMBOL_TYPE (sym
)) == 0
1696 && (TYPE_FLAGS (SYMBOL_TYPE (sym
)) & TYPE_FLAG_PERM
) == 0)
1697 TYPE_NAME (SYMBOL_TYPE (sym
))
1698 = concat (SYMBOL_NAME (sym
), "", "");
1700 /* Keep track of any type which points to empty structured type,
1701 so it can be filled from a definition from another file */
1702 if (TYPE_CODE (SYMBOL_TYPE (sym
)) == TYPE_CODE_PTR
&&
1703 TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym
))) == 0)
1705 register int i
= hashname (SYMBOL_NAME (sym
));
1707 SYMBOL_VALUE_CHAIN (sym
) = opaque_type_chain
[i
];
1708 opaque_type_chain
[i
] = sym
;
1710 add_symbol_to_list (sym
, &file_symbols
);
1716 SYMBOL_CLASS (sym
) = LOC_TYPEDEF
;
1717 SYMBOL_NAMESPACE (sym
) = STRUCT_NAMESPACE
;
1718 if (TYPE_NAME (SYMBOL_TYPE (sym
)) == 0
1719 && (TYPE_FLAGS (SYMBOL_TYPE (sym
)) & TYPE_FLAG_PERM
) == 0)
1720 TYPE_NAME (SYMBOL_TYPE (sym
))
1722 (cs
->c_sclass
== C_ENTAG
1724 : (cs
->c_sclass
== C_STRTAG
1725 ? "struct " : "union ")),
1727 add_symbol_to_list (sym
, &file_symbols
);
1737 /* Decode a coff type specifier;
1738 return the type that is meant. */
1742 decode_type (cs
, c_type
, aux
)
1743 register struct coff_symbol
*cs
;
1744 unsigned int c_type
;
1745 register union internal_auxent
*aux
;
1747 register struct type
*type
= 0;
1748 unsigned int new_c_type
;
1750 if (c_type
& ~N_BTMASK
)
1752 new_c_type
= DECREF (c_type
);
1755 type
= decode_type (cs
, new_c_type
, aux
);
1756 type
= lookup_pointer_type (type
);
1758 else if (ISFCN (c_type
))
1760 type
= decode_type (cs
, new_c_type
, aux
);
1761 type
= lookup_function_type (type
);
1763 else if (ISARY (c_type
))
1766 register unsigned short *dim
;
1767 struct type
*base_type
;
1769 /* Define an array type. */
1770 /* auxent refers to array, not base type */
1771 if (aux
->x_sym
.x_tagndx
.l
== 0)
1774 /* shift the indices down */
1775 dim
= &aux
->x_sym
.x_fcnary
.x_ary
.x_dimen
[0];
1778 for (i
= 0; *dim
&& i
< DIMNUM
- 1; i
++, dim
++)
1782 type
= (struct type
*)
1783 obstack_alloc (symbol_obstack
, sizeof (struct type
));
1784 bzero (type
, sizeof (struct type
));
1786 base_type
= decode_type (cs
, new_c_type
, aux
);
1788 TYPE_CODE (type
) = TYPE_CODE_ARRAY
;
1789 TYPE_TARGET_TYPE (type
) = base_type
;
1790 TYPE_LENGTH (type
) = n
* TYPE_LENGTH (base_type
);
1795 /* Reference to existing type */
1796 if (cs
->c_naux
> 0 && aux
->x_sym
.x_tagndx
.l
!= 0)
1798 type
= coff_alloc_type (aux
->x_sym
.x_tagndx
.l
);
1802 return decode_base_type (cs
, BTYPE (c_type
), aux
);
1805 /* Decode a coff type specifier for function definition;
1806 return the type that the function returns. */
1810 decode_function_type (cs
, c_type
, aux
)
1811 register struct coff_symbol
*cs
;
1812 unsigned int c_type
;
1813 register union internal_auxent
*aux
;
1815 if (aux
->x_sym
.x_tagndx
.l
== 0)
1816 cs
->c_naux
= 0; /* auxent refers to function, not base type */
1818 return decode_type (cs
, DECREF (c_type
), aux
);
1825 decode_base_type (cs
, c_type
, aux
)
1826 register struct coff_symbol
*cs
;
1827 unsigned int c_type
;
1828 register union internal_auxent
*aux
;
1835 /* shows up with "void (*foo)();" structure members */
1836 return builtin_type_void
;
1839 /* DGUX actually defines both T_ARG and T_VOID to the same value. */
1842 /* Shows up in DGUX, I think. Not sure where. */
1843 return builtin_type_void
; /* shouldn't show up here */
1849 /* Intel 960 COFF has this symbol and meaning. */
1850 return builtin_type_void
;
1854 return builtin_type_char
;
1857 return builtin_type_short
;
1860 return builtin_type_int
;
1863 return builtin_type_long
;
1866 return builtin_type_float
;
1869 return builtin_type_double
;
1872 if (cs
->c_naux
!= 1)
1874 /* anonymous structure type */
1875 type
= coff_alloc_type (cs
->c_symnum
);
1876 TYPE_CODE (type
) = TYPE_CODE_STRUCT
;
1877 TYPE_NAME (type
) = concat ("struct ", "<opaque>", "");
1878 TYPE_LENGTH (type
) = 0;
1879 TYPE_FIELDS (type
) = 0;
1880 TYPE_NFIELDS (type
) = 0;
1884 type
= read_struct_type (cs
->c_symnum
,
1885 aux
->x_sym
.x_misc
.x_lnsz
.x_size
,
1886 aux
->x_sym
.x_fcnary
.x_fcn
.x_endndx
);
1891 if (cs
->c_naux
!= 1)
1893 /* anonymous union type */
1894 type
= coff_alloc_type (cs
->c_symnum
);
1895 TYPE_NAME (type
) = concat ("union ", "<opaque>", "");
1896 TYPE_LENGTH (type
) = 0;
1897 TYPE_FIELDS (type
) = 0;
1898 TYPE_NFIELDS (type
) = 0;
1902 type
= read_struct_type (cs
->c_symnum
,
1903 aux
->x_sym
.x_misc
.x_lnsz
.x_size
,
1904 aux
->x_sym
.x_fcnary
.x_fcn
.x_endndx
);
1906 TYPE_CODE (type
) = TYPE_CODE_UNION
;
1910 return read_enum_type (cs
->c_symnum
,
1911 aux
->x_sym
.x_misc
.x_lnsz
.x_size
,
1912 aux
->x_sym
.x_fcnary
.x_fcn
.x_endndx
);
1915 /* shouldn't show up here */
1919 return builtin_type_unsigned_char
;
1922 return builtin_type_unsigned_short
;
1925 return builtin_type_unsigned_int
;
1928 return builtin_type_unsigned_long
;
1930 printf ("unexpected type %d at symnum %d\n", c_type
, cs
->c_symnum
);
1931 return builtin_type_void
;
1934 /* This page contains subroutines of read_type. */
1936 /* Read the description of a structure (or union type)
1937 and return an object describing the type. */
1939 static struct type
*
1940 read_struct_type (index
, length
, lastsym
)
1947 struct nextfield
*next
;
1951 register struct type
*type
;
1952 register struct nextfield
*list
= 0;
1953 struct nextfield
*new;
1957 #ifdef NAMES_HAVE_UNDERSCORE
1962 struct coff_symbol member_sym
;
1963 register struct coff_symbol
*ms
= &member_sym
;
1964 struct internal_syment sub_sym
;
1965 union internal_auxent sub_aux
;
1968 type
= coff_alloc_type (index
);
1969 TYPE_CODE (type
) = TYPE_CODE_STRUCT
;
1970 TYPE_LENGTH (type
) = length
;
1972 while (!done
&& symnum
< lastsym
&& symnum
< nlist_nsyms_global
)
1974 read_one_sym (ms
, &sub_sym
, &sub_aux
);
1976 name
= (name
[0] == '_' ? name
+ offset
: name
);
1978 switch (ms
->c_sclass
)
1983 /* Get space to record the next field's data. */
1984 new = (struct nextfield
*) alloca (sizeof (struct nextfield
));
1988 /* Save the data. */
1989 list
->field
.name
= savestring (name
, strlen (name
));
1990 list
->field
.type
= decode_type (ms
, ms
->c_type
, &sub_aux
);
1991 list
->field
.bitpos
= 8 * ms
->c_value
;
1992 list
->field
.bitsize
= 0;
1998 /* Get space to record the next field's data. */
1999 new = (struct nextfield
*) alloca (sizeof (struct nextfield
));
2003 /* Save the data. */
2004 list
->field
.name
= savestring (name
, strlen (name
));
2005 list
->field
.type
= decode_type (ms
, ms
->c_type
, &sub_aux
);
2006 list
->field
.bitpos
= ms
->c_value
;
2007 list
->field
.bitsize
= sub_aux
.x_sym
.x_misc
.x_lnsz
.x_size
;
2016 /* Now create the vector of fields, and record how big it is. */
2018 TYPE_NFIELDS (type
) = nfields
;
2019 TYPE_FIELDS (type
) = (struct field
*)
2020 obstack_alloc (symbol_obstack
, sizeof (struct field
) * nfields
);
2022 /* Copy the saved-up fields into the field vector. */
2024 for (n
= nfields
; list
; list
= list
->next
)
2025 TYPE_FIELD (type
, --n
) = list
->field
;
2030 /* Read a definition of an enumeration type,
2031 and create and return a suitable type object.
2032 Also defines the symbols that represent the values of the type. */
2033 /* Currently assumes it's sizeof (int) and doesn't use length. */
2036 static struct type
*
2037 read_enum_type (index
, length
, lastsym
)
2042 register struct symbol
*sym
;
2043 register struct type
*type
;
2046 struct pending
**symlist
;
2047 struct coff_symbol member_sym
;
2048 register struct coff_symbol
*ms
= &member_sym
;
2049 struct internal_syment sub_sym
;
2050 union internal_auxent sub_aux
;
2051 struct pending
*osyms
, *syms
;
2054 #ifdef NAMES_HAVE_UNDERSCORE
2060 type
= coff_alloc_type (index
);
2061 if (within_function
)
2062 symlist
= &local_symbols
;
2064 symlist
= &file_symbols
;
2067 while (!done
&& symnum
< lastsym
&& symnum
< nlist_nsyms_global
)
2069 read_one_sym (ms
, &sub_sym
, &sub_aux
);
2071 name
= (name
[0] == '_' ? name
+ offset
: name
);
2073 switch (ms
->c_sclass
)
2076 sym
= (struct symbol
*) xmalloc (sizeof (struct symbol
));
2077 bzero (sym
, sizeof (struct symbol
));
2079 SYMBOL_NAME (sym
) = savestring (name
, strlen (name
));
2080 SYMBOL_CLASS (sym
) = LOC_CONST
;
2081 SYMBOL_NAMESPACE (sym
) = VAR_NAMESPACE
;
2082 SYMBOL_VALUE (sym
) = ms
->c_value
;
2083 add_symbol_to_list (sym
, symlist
);
2088 /* Sometimes the linker (on 386/ix 2.0.2 at least) screws
2089 up the count of how many symbols to read. So stop
2096 /* Now fill in the fields of the type-structure. */
2098 /* FIXME: Should be sizeof (int) on target, not host. */
2099 TYPE_LENGTH (type
) = sizeof (int);
2100 TYPE_CODE (type
) = TYPE_CODE_ENUM
;
2101 TYPE_NFIELDS (type
) = nsyms
;
2102 TYPE_FIELDS (type
) = (struct field
*)
2103 obstack_alloc (symbol_obstack
, sizeof (struct field
) * nsyms
);
2105 /* Find the symbols for the values and put them into the type.
2106 The symbols can be found in the symlist that we put them on
2107 to cause them to be defined. osyms contains the old value
2108 of that symlist; everything up to there was defined by us. */
2110 for (syms
= *symlist
, n
= nsyms
; syms
!= osyms
; syms
= syms
->next
)
2112 SYMBOL_TYPE (syms
->symbol
) = type
;
2113 TYPE_FIELD_NAME (type
, --n
) = SYMBOL_NAME (syms
->symbol
);
2114 TYPE_FIELD_VALUE (type
, n
) = 0;
2115 TYPE_FIELD_BITPOS (type
, n
) = SYMBOL_VALUE (syms
->symbol
);
2116 TYPE_FIELD_BITSIZE (type
, n
) = 0;
2118 /* Is this Modula-2's BOOLEAN type? Flag it as such if so. */
2119 if(TYPE_NFIELDS(type
) == 2 &&
2120 ((!strcmp(TYPE_FIELD_NAME(type
,0),"TRUE") &&
2121 !strcmp(TYPE_FIELD_NAME(type
,1),"FALSE")) ||
2122 (!strcmp(TYPE_FIELD_NAME(type
,1),"TRUE") &&
2123 !strcmp(TYPE_FIELD_NAME(type
,0),"FALSE"))))
2124 TYPE_CODE(type
) = TYPE_CODE_BOOL
;
2128 /* Register our ability to parse symbols for coff BFD files */
2130 static struct sym_fns coff_sym_fns
=
2132 /* This assumes that 88kbcs implies TDESC and TDESC implies 88kbcs.
2133 If that's not true, this can be relaxed, but if it is true,
2134 it will just cause users grief if we try to read the wrong kind
2138 #else /* not TDESC */
2143 # endif /* not i386 */
2144 #endif /* not TDESC */
2145 coff_new_init
, coff_symfile_init
, coff_symfile_read
,
2149 _initialize_coffread ()
2151 add_symtab_fns(&coff_sym_fns
);