1 /* Read coff symbol tables and convert to internal format, for GDB.
2 Copyright 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994
3 Free Software Foundation, Inc.
4 Contributed by David D. Johnson, Brown University (ddj@cs.brown.edu).
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
25 #include "breakpoint.h"
32 #include "coff/internal.h" /* Internal format of COFF symbols in BFD */
33 #include "libcoff.h" /* FIXME secret internal data from BFD */
38 #include "gdb-stabs.h"
39 #include "stabsread.h"
40 #include "complaints.h"
42 struct coff_symfile_info
{
43 file_ptr min_lineno_offset
; /* Where in file lowest line#s are */
44 file_ptr max_lineno_offset
; /* 1+last byte of line#s in file */
46 asection
*stabsect
; /* Section pointer for .stab section */
47 asection
*stabstrsect
; /* Section pointer for .stab section */
48 asection
*stabindexsect
; /* Section pointer for .stab.index section */
52 /* Translate an external name string into a user-visible name. */
53 #define EXTERNAL_NAME(string, abfd) \
54 (string[0] == bfd_get_symbol_leading_char(abfd)? string+1: string)
56 /* To be an sdb debug type, type must have at least a basic or primary
57 derived type. Using this rather than checking against T_NULL is
58 said to prevent core dumps if we try to operate on Michael Bloom
61 #define SDB_TYPE(type) (BTYPE(type) | (type & N_TMASK))
63 /* Convert from an sdb register number to an internal gdb register number.
64 This should be defined in tm.h, if REGISTER_NAMES is not set up
65 to map one to one onto the sdb register numbers. */
67 #ifndef SDB_REG_TO_REGNUM
68 # define SDB_REG_TO_REGNUM(value) (value)
71 /* Core address of start and end of text of current source file.
72 This comes from a ".text" symbol where x_nlinno > 0. */
74 static CORE_ADDR current_source_start_addr
;
75 static CORE_ADDR current_source_end_addr
;
77 /* The addresses of the symbol table stream and number of symbols
78 of the object file we are reading (as copied into core). */
80 static bfd
*nlist_bfd_global
;
81 static int nlist_nsyms_global
;
83 /* Vector of line number information. */
85 static struct linetable
*line_vector
;
87 /* Index of next entry to go in line_vector_index. */
89 static int line_vector_index
;
91 /* Last line number recorded in the line vector. */
93 static int prev_line_number
;
95 /* Number of elements allocated for line_vector currently. */
97 static int line_vector_length
;
99 /* Pointers to scratch storage, used for reading raw symbols and auxents. */
101 static char *temp_sym
;
102 static char *temp_aux
;
104 /* Local variables that hold the shift and mask values for the
105 COFF file that we are currently reading. These come back to us
106 from BFD, and are referenced by their macro names, as well as
107 internally to the BTYPE, ISPTR, ISFCN, ISARY, ISTAG, and DECREF
108 macros from include/coff/internal.h . */
110 static unsigned local_n_btmask
;
111 static unsigned local_n_btshft
;
112 static unsigned local_n_tmask
;
113 static unsigned local_n_tshift
;
115 #define N_BTMASK local_n_btmask
116 #define N_BTSHFT local_n_btshft
117 #define N_TMASK local_n_tmask
118 #define N_TSHIFT local_n_tshift
120 /* Local variables that hold the sizes in the file of various COFF structures.
121 (We only need to know this to read them from the file -- BFD will then
122 translate the data in them, into `internal_xxx' structs in the right
123 byte order, alignment, etc.) */
125 static unsigned local_linesz
;
126 static unsigned local_symesz
;
127 static unsigned local_auxesz
;
129 /* Chain of typedefs of pointers to empty struct/union types.
130 They are chained thru the SYMBOL_VALUE_CHAIN. */
132 static struct symbol
*opaque_type_chain
[HASHSIZE
];
135 /* The type of the function we are currently reading in. This is
136 used by define_symbol to record the type of arguments to a function. */
138 struct type
*in_function_type
;
141 /* Complaints about various problems in the file being read */
143 struct complaint ef_complaint
=
144 {"Unmatched .ef symbol(s) ignored starting at symnum %d", 0, 0};
146 struct complaint bf_no_aux_complaint
=
147 {"`.bf' symbol %d has no aux entry", 0, 0};
149 struct complaint ef_no_aux_complaint
=
150 {"`.ef' symbol %d has no aux entry", 0, 0};
152 struct complaint lineno_complaint
=
153 {"Line number pointer %d lower than start of line numbers", 0, 0};
155 struct complaint unexpected_type_complaint
=
156 {"Unexpected type for symbol %s", 0, 0};
158 struct complaint bad_sclass_complaint
=
159 {"Bad n_sclass for symbol %s", 0, 0};
161 struct complaint misordered_blocks_complaint
=
162 {"Blocks out of order at address %x", 0, 0};
164 struct complaint tagndx_bad_complaint
=
165 {"Symbol table entry for %s has bad tagndx value", 0, 0};
167 struct complaint eb_complaint
=
168 {"Mismatched .eb symbol ignored starting at symnum %d", 0, 0};
170 /* Simplified internal version of coff symbol table information */
174 int c_symnum
; /* symbol number of this entry */
175 int c_naux
; /* 0 if syment only, 1 if syment + auxent, etc */
182 static struct type
*coff_read_struct_type
PARAMS ((int, int, int));
184 static struct type
*decode_base_type
PARAMS ((struct coff_symbol
*,
186 union internal_auxent
*));
188 static struct type
*decode_type
PARAMS ((struct coff_symbol
*, unsigned int,
189 union internal_auxent
*));
191 static struct type
*decode_function_type
PARAMS ((struct coff_symbol
*,
193 union internal_auxent
*));
195 static struct type
*coff_read_enum_type
PARAMS ((int, int, int));
197 static struct symbol
*process_coff_symbol
PARAMS ((struct coff_symbol
*,
198 union internal_auxent
*,
199 struct section_offsets
*,
202 static void patch_opaque_types
PARAMS ((struct symtab
*));
204 static void patch_type
PARAMS ((struct type
*, struct type
*));
206 static void enter_linenos
PARAMS ((long, int, int, struct section_offsets
*));
208 static void free_linetab
PARAMS ((void));
210 static int init_lineno
PARAMS ((bfd
*, long, int));
212 static char *getsymname
PARAMS ((struct internal_syment
*));
214 static void free_stringtab
PARAMS ((void));
216 static int init_stringtab
PARAMS ((bfd
*, long));
218 static void read_one_sym
PARAMS ((struct coff_symbol
*,
219 struct internal_syment
*,
220 union internal_auxent
*));
222 static void coff_symtab_read
PARAMS ((long, int, struct section_offsets
*,
225 static void find_linenos
PARAMS ((bfd
*, sec_ptr
, PTR
));
227 static void coff_symfile_init
PARAMS ((struct objfile
*));
229 static void coff_new_init
PARAMS ((struct objfile
*));
231 static void coff_symfile_read
PARAMS ((struct objfile
*,
232 struct section_offsets
*, int));
234 static void coff_symfile_finish
PARAMS ((struct objfile
*));
236 static void record_minimal_symbol
PARAMS ((char *, CORE_ADDR
,
237 enum minimal_symbol_type
,
240 static void coff_end_symtab
PARAMS ((struct objfile
*));
242 static void complete_symtab
PARAMS ((char *, CORE_ADDR
, unsigned int));
244 static void coff_start_symtab
PARAMS ((void));
246 static void coff_record_line
PARAMS ((int, CORE_ADDR
));
248 static struct type
*coff_alloc_type
PARAMS ((int));
250 static struct type
**coff_lookup_type
PARAMS ((int));
252 static void coff_locate_sections
PARAMS ((bfd
*, asection
*, PTR
));
254 /* We are called once per section from coff_symfile_read. We
255 need to examine each section we are passed, check to see
256 if it is something we are interested in processing, and
257 if so, stash away some access information for the section.
259 FIXME: The section names should not be hardwired strings (what
260 should they be? I don't think most object file formats have enough
261 section flags to specify what kind of debug section it is
265 coff_locate_sections (ignore_abfd
, sectp
, csip
)
270 register struct coff_symfile_info
*csi
;
272 csi
= (struct coff_symfile_info
*) csip
;
273 if (STREQ (sectp
->name
, ".stab"))
275 csi
->stabsect
= sectp
;
277 else if (STREQ (sectp
->name
, ".stabstr"))
279 csi
->stabstrsect
= sectp
;
281 else if (STREQ (sectp
->name
, ".stab.index"))
283 csi
->stabindexsect
= sectp
;
287 /* Return the section_offsets* that CS points to. */
288 static int cs_to_section
PARAMS ((struct coff_symbol
*, struct objfile
*));
290 struct find_targ_sec_arg
{
295 static void find_targ_sec
PARAMS ((bfd
*, asection
*, void *));
297 static void find_targ_sec (abfd
, sect
, obj
)
302 struct find_targ_sec_arg
*args
= (struct find_targ_sec_arg
*)obj
;
303 if (sect
->target_index
== args
->targ_index
)
305 /* This is the section. Figure out what SECT_OFF_* code it is. */
306 if (bfd_get_section_flags (abfd
, sect
) & SEC_CODE
)
307 *args
->resultp
= SECT_OFF_TEXT
;
308 else if (bfd_get_section_flags (abfd
, sect
) & SEC_LOAD
)
309 *args
->resultp
= SECT_OFF_DATA
;
311 *args
->resultp
= SECT_OFF_BSS
;
315 /* Return the section number (SECT_OFF_*) that CS points to. */
317 cs_to_section (cs
, objfile
)
318 struct coff_symbol
*cs
;
319 struct objfile
*objfile
;
321 int off
= SECT_OFF_TEXT
;
322 struct find_targ_sec_arg args
;
323 args
.targ_index
= cs
->c_secnum
;
325 bfd_map_over_sections (objfile
->obfd
, find_targ_sec
, &args
);
329 /* Look up a coff type-number index. Return the address of the slot
330 where the type for that index is stored.
331 The type-number is in INDEX.
333 This can be used for finding the type associated with that index
334 or for associating a new type with the index. */
336 static struct type
**
337 coff_lookup_type (index
)
340 if (index
>= type_vector_length
)
342 int old_vector_length
= type_vector_length
;
344 type_vector_length
*= 2;
345 if (index
/* is still */ >= type_vector_length
)
346 type_vector_length
= index
* 2;
348 type_vector
= (struct type
**)
349 xrealloc ((char *) type_vector
,
350 type_vector_length
* sizeof (struct type
*));
351 memset (&type_vector
[old_vector_length
], 0,
352 (type_vector_length
- old_vector_length
) * sizeof(struct type
*));
354 return &type_vector
[index
];
357 /* Make sure there is a type allocated for type number index
358 and return the type object.
359 This can create an empty (zeroed) type object. */
362 coff_alloc_type (index
)
365 register struct type
**type_addr
= coff_lookup_type (index
);
366 register struct type
*type
= *type_addr
;
368 /* If we are referring to a type not known at all yet,
369 allocate an empty type for it.
370 We will fill it in later if we find out how. */
373 type
= alloc_type (current_objfile
);
379 /* Record a line number entry for line LINE at address PC.
380 FIXME: Use record_line instead. */
383 coff_record_line (line
, pc
)
387 struct linetable_entry
*e
;
388 /* Make sure line vector is big enough. */
390 if (line_vector_index
+ 2 >= line_vector_length
)
392 line_vector_length
*= 2;
393 line_vector
= (struct linetable
*)
394 xrealloc ((char *) line_vector
, sizeof (struct linetable
)
395 + (line_vector_length
396 * sizeof (struct linetable_entry
)));
399 e
= line_vector
->item
+ line_vector_index
++;
400 e
->line
= line
; e
->pc
= pc
;
403 /* Start a new symtab for a new source file.
404 This is called when a COFF ".file" symbol is seen;
405 it indicates the start of data for one original source file. */
411 /* We fill in the filename later. start_symtab puts
412 this pointer into last_source_file and we put it in
413 subfiles->name, which end_symtab frees; that's why
414 it must be malloc'd. */
416 /* We never know the directory name for COFF. */
418 /* The start address is irrelevant, since we set
419 last_source_start_addr in coff_end_symtab. */
422 /* Initialize the source file line number information for this file. */
424 if (line_vector
) /* Unlikely, but maybe possible? */
425 free ((PTR
)line_vector
);
426 line_vector_index
= 0;
427 line_vector_length
= 1000;
428 prev_line_number
= -2; /* Force first line number to be explicit */
429 line_vector
= (struct linetable
*)
430 xmalloc (sizeof (struct linetable
)
431 + line_vector_length
* sizeof (struct linetable_entry
));
434 /* Save the vital information from when starting to read a file,
435 for use when closing off the current file.
436 NAME is the file name the symbols came from, START_ADDR is the first
437 text address for the file, and SIZE is the number of bytes of text. */
440 complete_symtab (name
, start_addr
, size
)
442 CORE_ADDR start_addr
;
445 if (last_source_file
!= NULL
)
446 free (last_source_file
);
447 last_source_file
= savestring (name
, strlen (name
));
448 current_source_start_addr
= start_addr
;
449 current_source_end_addr
= start_addr
+ size
;
451 if (current_objfile
-> ei
.entry_point
>= current_source_start_addr
&&
452 current_objfile
-> ei
.entry_point
< current_source_end_addr
)
454 current_objfile
-> ei
.entry_file_lowpc
= current_source_start_addr
;
455 current_objfile
-> ei
.entry_file_highpc
= current_source_end_addr
;
459 /* Finish the symbol definitions for one main source file,
460 close off all the lexical contexts for that file
461 (creating struct block's for them), then make the
462 struct symtab for that file and put it in the list of all such. */
465 coff_end_symtab (objfile
)
466 struct objfile
*objfile
;
468 struct symtab
*symtab
;
470 last_source_start_addr
= current_source_start_addr
;
472 /* For no good reason, this file stores the number of entries in a
473 separate variable instead of in line_vector->nitems. Fix it. */
475 line_vector
->nitems
= line_vector_index
;
477 /* For COFF, we only have one subfile, so we can just look at
478 subfiles and not worry about there being other elements in the
479 chain. We fill in various fields now because we didn't know them
480 before (or because doing it now is simply an artifact of how this
481 file used to be written). */
482 subfiles
->line_vector
= line_vector
;
483 subfiles
->name
= last_source_file
;
485 /* sort_pending is needed for amdcoff, at least.
486 sort_linevec is needed for the SCO compiler. */
487 symtab
= end_symtab (current_source_end_addr
, 1, 1, objfile
, 0);
490 free_named_symtabs (symtab
->filename
);
492 /* Reinitialize for beginning of new file. */
494 line_vector_length
= -1;
495 last_source_file
= NULL
;
499 record_minimal_symbol (name
, address
, type
, objfile
)
502 enum minimal_symbol_type type
;
503 struct objfile
*objfile
;
505 /* We don't want TDESC entry points in the minimal symbol table */
506 if (name
[0] == '@') return;
508 prim_record_minimal_symbol
509 (obsavestring (name
, strlen (name
), &objfile
->symbol_obstack
),
514 /* coff_symfile_init ()
515 is the coff-specific initialization routine for reading symbols.
516 It is passed a struct objfile which contains, among other things,
517 the BFD for the file whose symbols are being read, and a slot for
518 a pointer to "private data" which we fill with cookies and other
519 treats for coff_symfile_read ().
521 We will only be called if this is a COFF or COFF-like file.
522 BFD handles figuring out the format of the file, and code in symtab.c
523 uses BFD's determination to vector to us.
525 The ultimate result is a new symtab (or, FIXME, eventually a psymtab). */
528 coff_symfile_init (objfile
)
529 struct objfile
*objfile
;
531 bfd
*abfd
= objfile
->obfd
;
533 /* Allocate struct to keep track of stab reading. */
534 objfile
->sym_stab_info
= (PTR
)
535 xmmalloc (objfile
-> md
, sizeof (struct dbx_symfile_info
));
537 memset ((PTR
) objfile
->sym_stab_info
, 0, sizeof (struct dbx_symfile_info
));
539 /* Allocate struct to keep track of the symfile */
540 objfile
-> sym_private
= xmmalloc (objfile
-> md
,
541 sizeof (struct coff_symfile_info
));
543 memset (objfile
->sym_private
, 0, sizeof (struct coff_symfile_info
));
545 init_entry_point_info (objfile
);
548 /* This function is called for every section; it finds the outer limits
549 of the line table (minimum and maximum file offset) so that the
550 mainline code can read the whole thing for efficiency. */
554 find_linenos (abfd
, asect
, vpinfo
)
559 struct coff_symfile_info
*info
;
561 file_ptr offset
, maxoff
;
563 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
564 count
= asect
->lineno_count
;
569 size
= count
* local_linesz
;
571 info
= (struct coff_symfile_info
*)vpinfo
;
572 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
573 offset
= asect
->line_filepos
;
576 if (offset
< info
->min_lineno_offset
|| info
->min_lineno_offset
== 0)
577 info
->min_lineno_offset
= offset
;
579 maxoff
= offset
+ size
;
580 if (maxoff
> info
->max_lineno_offset
)
581 info
->max_lineno_offset
= maxoff
;
585 /* The BFD for this file -- only good while we're actively reading
586 symbols into a psymtab or a symtab. */
588 static bfd
*symfile_bfd
;
590 /* Read a symbol file, after initialization by coff_symfile_init. */
594 coff_symfile_read (objfile
, section_offsets
, mainline
)
595 struct objfile
*objfile
;
596 struct section_offsets
*section_offsets
;
599 struct coff_symfile_info
*info
;
600 struct dbx_symfile_info
*dbxinfo
;
601 bfd
*abfd
= objfile
->obfd
;
602 coff_data_type
*cdata
= coff_data (abfd
);
603 char *name
= bfd_get_filename (abfd
);
607 int stringtab_offset
;
608 struct cleanup
*back_to
;
609 int stabsize
, stabstrsize
;
611 info
= (struct coff_symfile_info
*) objfile
-> sym_private
;
612 dbxinfo
= (struct dbx_symfile_info
*) objfile
->sym_stab_info
;
613 symfile_bfd
= abfd
; /* Kludge for swap routines */
615 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
616 num_symbols
= bfd_get_symcount (abfd
); /* How many syms */
617 symtab_offset
= cdata
->sym_filepos
; /* Symbol table file offset */
618 stringtab_offset
= symtab_offset
+ /* String table file offset */
619 num_symbols
* cdata
->local_symesz
;
621 /* Set a few file-statics that give us specific information about
622 the particular COFF file format we're reading. */
623 local_linesz
= cdata
->local_linesz
;
624 local_n_btmask
= cdata
->local_n_btmask
;
625 local_n_btshft
= cdata
->local_n_btshft
;
626 local_n_tmask
= cdata
->local_n_tmask
;
627 local_n_tshift
= cdata
->local_n_tshift
;
628 local_linesz
= cdata
->local_linesz
;
629 local_symesz
= cdata
->local_symesz
;
630 local_auxesz
= cdata
->local_auxesz
;
632 /* Allocate space for raw symbol and aux entries, based on their
633 space requirements as reported by BFD. */
634 temp_sym
= (char *) xmalloc
635 (cdata
->local_symesz
+ cdata
->local_auxesz
);
636 temp_aux
= temp_sym
+ cdata
->local_symesz
;
637 back_to
= make_cleanup (free_current_contents
, &temp_sym
);
640 /* Read the line number table, all at once. */
641 info
->min_lineno_offset
= 0;
642 info
->max_lineno_offset
= 0;
643 bfd_map_over_sections (abfd
, find_linenos
, (PTR
) info
);
645 make_cleanup (free_linetab
, 0);
646 val
= init_lineno (abfd
, info
->min_lineno_offset
,
647 info
->max_lineno_offset
- info
->min_lineno_offset
);
649 error ("\"%s\": error reading line numbers\n", name
);
651 /* Now read the string table, all at once. */
653 make_cleanup (free_stringtab
, 0);
654 val
= init_stringtab (abfd
, stringtab_offset
);
656 error ("\"%s\": can't get string table", name
);
658 init_minimal_symbol_collection ();
659 make_cleanup (discard_minimal_symbols
, 0);
661 /* Now that the executable file is positioned at symbol table,
662 process it and define symbols accordingly. */
664 coff_symtab_read ((long) symtab_offset
, num_symbols
, section_offsets
,
667 /* Sort symbols alphabetically within each block. */
672 for (s
= objfile
-> symtabs
; s
!= NULL
; s
= s
-> next
)
673 sort_symtab_syms (s
);
676 /* Install any minimal symbols that have been collected as the current
677 minimal symbols for this objfile. */
679 install_minimal_symbols (objfile
);
681 bfd_map_over_sections (abfd
, coff_locate_sections
, (PTR
) info
);
685 /* FIXME: dubious. Why can't we use something normal like
686 bfd_get_section_contents? */
687 bfd_seek (abfd
, abfd
->where
, 0);
689 stabsize
= bfd_section_size (abfd
, info
->stabsect
);
690 stabstrsize
= bfd_section_size (abfd
, info
->stabstrsect
);
692 coffstab_build_psymtabs (objfile
,
695 info
->stabsect
->filepos
, stabsize
,
696 info
->stabstrsect
->filepos
, stabstrsize
);
699 do_cleanups (back_to
);
703 coff_new_init (ignore
)
704 struct objfile
*ignore
;
708 /* Perform any local cleanups required when we are done with a particular
709 objfile. I.E, we are in the process of discarding all symbol information
710 for an objfile, freeing up all memory held for it, and unlinking the
711 objfile struct from the global list of known objfiles. */
714 coff_symfile_finish (objfile
)
715 struct objfile
*objfile
;
717 if (objfile
-> sym_private
!= NULL
)
719 mfree (objfile
-> md
, objfile
-> sym_private
);
724 /* Given pointers to a symbol table in coff style exec file,
725 analyze them and create struct symtab's describing the symbols.
726 NSYMS is the number of symbols in the symbol table.
727 We read them one at a time using read_one_sym (). */
730 coff_symtab_read (symtab_offset
, nsyms
, section_offsets
, objfile
)
733 struct section_offsets
*section_offsets
;
734 struct objfile
*objfile
;
736 register struct context_stack
*new;
737 struct coff_symbol coff_symbol
;
738 register struct coff_symbol
*cs
= &coff_symbol
;
739 static struct internal_syment main_sym
;
740 static union internal_auxent main_aux
;
741 struct coff_symbol fcn_cs_saved
;
742 static struct internal_syment fcn_sym_saved
;
743 static union internal_auxent fcn_aux_saved
;
745 /* A .file is open. */
746 int in_source_file
= 0;
747 int next_file_symnum
= -1;
748 /* Name of the current file. */
749 char *filestring
= "";
751 int fcn_first_line
= 0;
752 int fcn_last_line
= 0;
753 int fcn_start_addr
= 0;
754 long fcn_line_ptr
= 0;
758 /* Work around a stdio bug in SunOS4.1.1 (this makes me nervous....
759 it's hard to know I've really worked around it. The fix should be
760 harmless, anyway). The symptom of the bug is that the first
761 fread (in read_one_sym), will (in my example) actually get data
762 from file offset 268, when the fseek was to 264 (and ftell shows
763 264). This causes all hell to break loose. I was unable to
764 reproduce this on a short test program which operated on the same
765 file, performing (I think) the same sequence of operations.
767 It stopped happening when I put in this (former) rewind().
769 FIXME: Find out if this has been reported to Sun, whether it has
770 been fixed in a later release, etc. */
772 bfd_seek (objfile
->obfd
, 0, 0);
774 /* Position to read the symbol table. */
775 val
= bfd_seek (objfile
->obfd
, (long) symtab_offset
, 0);
777 perror_with_name (objfile
->name
);
779 current_objfile
= objfile
;
780 nlist_bfd_global
= objfile
->obfd
;
781 nlist_nsyms_global
= nsyms
;
782 last_source_file
= NULL
;
783 memset (opaque_type_chain
, 0, sizeof opaque_type_chain
);
785 if (type_vector
) /* Get rid of previous one */
786 free ((PTR
) type_vector
);
787 type_vector_length
= 160;
788 type_vector
= (struct type
**)
789 xmalloc (type_vector_length
* sizeof (struct type
*));
790 memset (type_vector
, 0, type_vector_length
* sizeof (struct type
*));
792 coff_start_symtab ();
795 while (symnum
< nsyms
)
797 QUIT
; /* Make this command interruptable. */
799 read_one_sym (cs
, &main_sym
, &main_aux
);
802 temp_sem_val
= cs
->c_name
[0] << 24 | cs
->c_name
[1] << 16 |
803 cs
->c_name
[2] << 8 | cs
->c_name
[3];
804 if (int_sem_val
== temp_sem_val
)
805 last_coffsem
= (int) strtol (cs
->c_name
+4, (char **) NULL
, 10);
808 if (cs
->c_symnum
== next_file_symnum
&& cs
->c_sclass
!= C_FILE
)
810 if (last_source_file
)
811 coff_end_symtab (objfile
);
813 coff_start_symtab ();
814 complete_symtab ("_globals_", 0, 0);
815 /* done with all files, everything from here on out is globals */
818 /* Special case for file with type declarations only, no text. */
819 if (!last_source_file
&& SDB_TYPE (cs
->c_type
)
820 && cs
->c_secnum
== N_DEBUG
)
821 complete_symtab (filestring
, 0, 0);
823 /* Typedefs should not be treated as symbol definitions. */
824 if (ISFCN (cs
->c_type
) && cs
->c_sclass
!= C_TPDEF
)
826 /* Record all functions -- external and static -- in minsyms. */
827 tmpaddr
= cs
->c_value
+ ANOFFSET (section_offsets
, SECT_OFF_TEXT
);
828 record_minimal_symbol (cs
->c_name
, tmpaddr
, mst_text
, objfile
);
830 fcn_line_ptr
= main_aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
;
831 fcn_start_addr
= tmpaddr
;
833 fcn_sym_saved
= main_sym
;
834 fcn_aux_saved
= main_aux
;
838 switch (cs
->c_sclass
)
847 complain (&bad_sclass_complaint
, cs
->c_name
);
851 /* c_value field contains symnum of next .file entry in table
852 or symnum of first global after last .file. */
853 next_file_symnum
= cs
->c_value
;
855 filestring
= coff_getfilename (&main_aux
);
859 /* Complete symbol table for last object file
860 containing debugging information. */
861 if (last_source_file
)
863 coff_end_symtab (objfile
);
864 coff_start_symtab ();
870 if (cs
->c_name
[0] == '.')
872 if (STREQ (cs
->c_name
, ".text")) {
873 /* FIXME: don't wire in ".text" as section name
875 /* Check for in_source_file deals with case of
876 a file with debugging symbols
877 followed by a later file with no symbols. */
879 complete_symtab (filestring
,
880 cs
->c_value
+ ANOFFSET (section_offsets
, SECT_OFF_TEXT
),
881 main_aux
.x_scn
.x_scnlen
);
884 /* flush rest of '.' symbols */
887 else if (!SDB_TYPE (cs
->c_type
)
888 && cs
->c_name
[0] == 'L'
889 && (strncmp (cs
->c_name
, "LI%", 3) == 0
890 || strncmp (cs
->c_name
, "LF%", 3) == 0
891 || strncmp (cs
->c_name
,"LC%",3) == 0
892 || strncmp (cs
->c_name
,"LP%",3) == 0
893 || strncmp (cs
->c_name
,"LPB%",4) == 0
894 || strncmp (cs
->c_name
,"LBB%",4) == 0
895 || strncmp (cs
->c_name
,"LBE%",4) == 0
896 || strncmp (cs
->c_name
,"LPBX%",5) == 0))
897 /* At least on a 3b1, gcc generates swbeg and string labels
898 that look like this. Ignore them. */
900 /* fall in for static symbols that don't start with '.' */
903 /* Record it in the minimal symbols regardless of
904 SDB_TYPE. This parallels what we do for other debug
905 formats, and probably is needed to make
906 print_address_symbolic work right without the (now
907 gone) "set fast-symbolic-addr off" kludge. */
909 /* FIXME: should use mst_abs, and not relocate, if absolute. */
910 enum minimal_symbol_type ms_type
;
911 int sec
= cs_to_section (cs
, objfile
);
912 tmpaddr
= cs
->c_value
;
913 if (cs
->c_sclass
!= C_STAT
)
914 tmpaddr
+= ANOFFSET (section_offsets
, sec
);
918 case SECT_OFF_RODATA
:
919 ms_type
= cs
->c_sclass
== C_STAT
? mst_file_text
: mst_text
;
922 ms_type
= cs
->c_sclass
== C_STAT
? mst_file_data
: mst_data
;
925 ms_type
= cs
->c_sclass
== C_STAT
? mst_file_bss
: mst_bss
;
928 ms_type
= mst_unknown
;
932 record_minimal_symbol (cs
->c_name
, tmpaddr
, ms_type
, objfile
);
935 if (SDB_TYPE (cs
->c_type
))
936 process_coff_symbol (cs
, &main_aux
, section_offsets
, objfile
);
940 if (STREQ (cs
->c_name
, ".bf"))
944 /* value contains address of first non-init type code */
945 /* main_aux.x_sym.x_misc.x_lnsz.x_lnno
946 contains line number of '{' } */
948 complain (&bf_no_aux_complaint
, cs
->c_symnum
);
949 fcn_first_line
= main_aux
.x_sym
.x_misc
.x_lnsz
.x_lnno
;
951 /* Might want to check that locals are 0 and
952 context_stack_depth is zero, and complain if not. */
955 new = push_context (depth
, fcn_start_addr
);
956 fcn_cs_saved
.c_name
= getsymname (&fcn_sym_saved
);
958 process_coff_symbol (&fcn_cs_saved
, &fcn_aux_saved
,
959 section_offsets
, objfile
);
961 else if (STREQ (cs
->c_name
, ".ef"))
963 /* the value of .ef is the address of epilogue code;
964 not useful for gdb. */
965 /* { main_aux.x_sym.x_misc.x_lnsz.x_lnno
966 contains number of lines to '}' */
967 new = pop_context ();
968 /* Stack must be empty now. */
969 if (context_stack_depth
> 0 || new == NULL
)
971 complain (&ef_complaint
, cs
->c_symnum
);
977 complain (&ef_no_aux_complaint
, cs
->c_symnum
);
978 fcn_last_line
= 0x7FFFFFFF;
982 fcn_last_line
= main_aux
.x_sym
.x_misc
.x_lnsz
.x_lnno
;
984 enter_linenos (fcn_line_ptr
, fcn_first_line
, fcn_last_line
,
987 finish_block (new->name
, &local_symbols
, new->old_blocks
,
989 #if defined (FUNCTION_EPILOGUE_SIZE)
990 /* This macro should be defined only on
992 fcn_aux_saved.x_sym.x_misc.x_fsize
993 field is always zero.
994 So use the .bf record information that
995 points to the epilogue and add the size
998 + FUNCTION_EPILOGUE_SIZE
999 + ANOFFSET (section_offsets
, SECT_OFF_TEXT
),
1001 fcn_cs_saved
.c_value
1002 + fcn_aux_saved
.x_sym
.x_misc
.x_fsize
1003 + ANOFFSET (section_offsets
, SECT_OFF_TEXT
),
1007 within_function
= 0;
1012 if (STREQ (cs
->c_name
, ".bb"))
1014 tmpaddr
= cs
->c_value
;
1015 tmpaddr
+= ANOFFSET (section_offsets
, SECT_OFF_TEXT
);
1016 push_context (++depth
, tmpaddr
);
1018 else if (STREQ (cs
->c_name
, ".eb"))
1020 new = pop_context ();
1021 if (depth
-- != new->depth
)
1023 complain (&eb_complaint
, symnum
);
1026 if (local_symbols
&& context_stack_depth
> 0)
1029 cs
->c_value
+ ANOFFSET (section_offsets
, SECT_OFF_TEXT
);
1030 /* Make a block for the local symbols within. */
1031 finish_block (0, &local_symbols
, new->old_blocks
,
1032 new->start_addr
, tmpaddr
, objfile
);
1034 /* Now pop locals of block just finished. */
1035 local_symbols
= new->locals
;
1040 process_coff_symbol (cs
, &main_aux
, section_offsets
, objfile
);
1045 if (last_source_file
)
1046 coff_end_symtab (objfile
);
1048 /* Patch up any opaque types (references to types that are not defined
1049 in the file where they are referenced, e.g. "struct foo *bar"). */
1050 ALL_OBJFILE_SYMTABS (objfile
, s
)
1051 patch_opaque_types (s
);
1053 current_objfile
= NULL
;
1056 /* Routines for reading headers and symbols from executable. */
1058 /* Read the next symbol, swap it, and return it in both internal_syment
1059 form, and coff_symbol form. Also return its first auxent, if any,
1060 in internal_auxent form, and skip any other auxents. */
1063 read_one_sym (cs
, sym
, aux
)
1064 register struct coff_symbol
*cs
;
1065 register struct internal_syment
*sym
;
1066 register union internal_auxent
*aux
;
1070 cs
->c_symnum
= symnum
;
1071 bfd_read (temp_sym
, local_symesz
, 1, nlist_bfd_global
);
1072 bfd_coff_swap_sym_in (symfile_bfd
, temp_sym
, (char *)sym
);
1073 cs
->c_naux
= sym
->n_numaux
& 0xff;
1074 if (cs
->c_naux
>= 1)
1076 bfd_read (temp_aux
, local_auxesz
, 1, nlist_bfd_global
);
1077 bfd_coff_swap_aux_in (symfile_bfd
, temp_aux
, sym
->n_type
, sym
->n_sclass
,
1078 0, cs
->c_naux
, (char *)aux
);
1079 /* If more than one aux entry, read past it (only the first aux
1081 for (i
= 1; i
< cs
->c_naux
; i
++)
1082 bfd_read (temp_aux
, local_auxesz
, 1, nlist_bfd_global
);
1084 cs
->c_name
= getsymname (sym
);
1085 cs
->c_value
= sym
->n_value
;
1086 cs
->c_sclass
= (sym
->n_sclass
& 0xff);
1087 cs
->c_secnum
= sym
->n_scnum
;
1088 cs
->c_type
= (unsigned) sym
->n_type
;
1089 if (!SDB_TYPE (cs
->c_type
))
1092 symnum
+= 1 + cs
->c_naux
;
1095 /* Support for string table handling */
1097 static char *stringtab
= NULL
;
1100 init_stringtab (abfd
, offset
)
1106 unsigned char lengthbuf
[4];
1110 /* If the file is stripped, the offset might be zero, indicating no
1111 string table. Just return with `stringtab' set to null. */
1115 if (bfd_seek (abfd
, offset
, 0) < 0)
1118 val
= bfd_read ((char *)lengthbuf
, sizeof lengthbuf
, 1, abfd
);
1119 length
= bfd_h_get_32 (symfile_bfd
, lengthbuf
);
1121 /* If no string table is needed, then the file may end immediately
1122 after the symbols. Just return with `stringtab' set to null. */
1123 if (val
!= sizeof lengthbuf
|| length
< sizeof lengthbuf
)
1126 stringtab
= (char *) xmalloc (length
);
1127 /* This is in target format (probably not very useful, and not currently
1128 used), not host format. */
1129 memcpy (stringtab
, lengthbuf
, sizeof lengthbuf
);
1130 if (length
== sizeof length
) /* Empty table -- just the count */
1133 val
= bfd_read (stringtab
+ sizeof lengthbuf
, length
- sizeof lengthbuf
, 1, abfd
);
1134 if (val
!= length
- sizeof lengthbuf
|| stringtab
[length
- 1] != '\0')
1149 getsymname (symbol_entry
)
1150 struct internal_syment
*symbol_entry
;
1152 static char buffer
[SYMNMLEN
+1];
1155 if (symbol_entry
->_n
._n_n
._n_zeroes
== 0)
1157 /* FIXME: Probably should be detecting corrupt symbol files by
1158 seeing whether offset points to within the stringtab. */
1159 result
= stringtab
+ symbol_entry
->_n
._n_n
._n_offset
;
1163 strncpy (buffer
, symbol_entry
->_n
._n_name
, SYMNMLEN
);
1164 buffer
[SYMNMLEN
] = '\0';
1170 /* Extract the file name from the aux entry of a C_FILE symbol. Return
1171 only the last component of the name. Result is in static storage and
1172 is only good for temporary use. */
1175 coff_getfilename (aux_entry
)
1176 union internal_auxent
*aux_entry
;
1178 static char buffer
[BUFSIZ
];
1179 register char *temp
;
1182 if (aux_entry
->x_file
.x_n
.x_zeroes
== 0)
1183 strcpy (buffer
, stringtab
+ aux_entry
->x_file
.x_n
.x_offset
);
1186 strncpy (buffer
, aux_entry
->x_file
.x_fname
, FILNMLEN
);
1187 buffer
[FILNMLEN
] = '\0';
1191 /* FIXME: We should not be throwing away the information about what
1192 directory. It should go into dirname of the symtab, or some such
1194 if ((temp
= strrchr (result
, '/')) != NULL
)
1199 /* Support for line number handling. */
1201 static char *linetab
= NULL
;
1202 static long linetab_offset
;
1203 static unsigned long linetab_size
;
1205 /* Read in all the line numbers for fast lookups later. Leave them in
1206 external (unswapped) format in memory; we'll swap them as we enter
1207 them into GDB's data structures. */
1210 init_lineno (abfd
, offset
, size
)
1217 linetab_offset
= offset
;
1218 linetab_size
= size
;
1225 if (bfd_seek (abfd
, offset
, 0) < 0)
1228 /* Allocate the desired table, plus a sentinel */
1229 linetab
= (char *) xmalloc (size
+ local_linesz
);
1231 val
= bfd_read (linetab
, size
, 1, abfd
);
1235 /* Terminate it with an all-zero sentinel record */
1236 memset (linetab
+ size
, 0, local_linesz
);
1249 #if !defined (L_LNNO32)
1250 #define L_LNNO32(lp) ((lp)->l_lnno)
1254 enter_linenos (file_offset
, first_line
, last_line
, section_offsets
)
1256 register int first_line
;
1257 register int last_line
;
1258 struct section_offsets
*section_offsets
;
1260 register char *rawptr
;
1261 struct internal_lineno lptr
;
1263 if (file_offset
< linetab_offset
)
1265 complain (&lineno_complaint
, file_offset
);
1266 if (file_offset
> linetab_size
) /* Too big to be an offset? */
1268 file_offset
+= linetab_offset
; /* Try reading at that linetab offset */
1271 rawptr
= &linetab
[file_offset
- linetab_offset
];
1273 /* skip first line entry for each function */
1274 rawptr
+= local_linesz
;
1275 /* line numbers start at one for the first line of the function */
1279 bfd_coff_swap_lineno_in (symfile_bfd
, rawptr
, &lptr
);
1280 rawptr
+= local_linesz
;
1281 /* The next function, or the sentinel, will have L_LNNO32 zero; we exit. */
1282 if (L_LNNO32 (&lptr
) && L_LNNO32 (&lptr
) <= last_line
)
1283 coff_record_line (first_line
+ L_LNNO32 (&lptr
),
1285 + ANOFFSET (section_offsets
, SECT_OFF_TEXT
));
1292 patch_type (type
, real_type
)
1294 struct type
*real_type
;
1296 register struct type
*target
= TYPE_TARGET_TYPE (type
);
1297 register struct type
*real_target
= TYPE_TARGET_TYPE (real_type
);
1298 int field_size
= TYPE_NFIELDS (real_target
) * sizeof (struct field
);
1300 TYPE_LENGTH (target
) = TYPE_LENGTH (real_target
);
1301 TYPE_NFIELDS (target
) = TYPE_NFIELDS (real_target
);
1302 TYPE_FIELDS (target
) = (struct field
*) TYPE_ALLOC (target
, field_size
);
1304 memcpy (TYPE_FIELDS (target
), TYPE_FIELDS (real_target
), field_size
);
1306 if (TYPE_NAME (real_target
))
1308 if (TYPE_NAME (target
))
1309 free (TYPE_NAME (target
));
1310 TYPE_NAME (target
) = concat (TYPE_NAME (real_target
), NULL
);
1314 /* Patch up all appropriate typedef symbols in the opaque_type_chains
1315 so that they can be used to print out opaque data structures properly. */
1318 patch_opaque_types (s
)
1321 register struct block
*b
;
1323 register struct symbol
*real_sym
;
1325 /* Go through the per-file symbols only */
1326 b
= BLOCKVECTOR_BLOCK (BLOCKVECTOR (s
), STATIC_BLOCK
);
1327 for (i
= BLOCK_NSYMS (b
) - 1; i
>= 0; i
--)
1329 /* Find completed typedefs to use to fix opaque ones.
1330 Remove syms from the chain when their types are stored,
1331 but search the whole chain, as there may be several syms
1332 from different files with the same name. */
1333 real_sym
= BLOCK_SYM (b
, i
);
1334 if (SYMBOL_CLASS (real_sym
) == LOC_TYPEDEF
&&
1335 SYMBOL_NAMESPACE (real_sym
) == VAR_NAMESPACE
&&
1336 TYPE_CODE (SYMBOL_TYPE (real_sym
)) == TYPE_CODE_PTR
&&
1337 TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (real_sym
))) != 0)
1339 register char *name
= SYMBOL_NAME (real_sym
);
1340 register int hash
= hashname (name
);
1341 register struct symbol
*sym
, *prev
;
1344 for (sym
= opaque_type_chain
[hash
]; sym
;)
1346 if (name
[0] == SYMBOL_NAME (sym
)[0] &&
1347 STREQ (name
+ 1, SYMBOL_NAME (sym
) + 1))
1351 SYMBOL_VALUE_CHAIN (prev
) = SYMBOL_VALUE_CHAIN (sym
);
1355 opaque_type_chain
[hash
] = SYMBOL_VALUE_CHAIN (sym
);
1358 patch_type (SYMBOL_TYPE (sym
), SYMBOL_TYPE (real_sym
));
1362 sym
= SYMBOL_VALUE_CHAIN (prev
);
1366 sym
= opaque_type_chain
[hash
];
1372 sym
= SYMBOL_VALUE_CHAIN (sym
);
1379 static struct symbol
*
1380 process_coff_symbol (cs
, aux
, section_offsets
, objfile
)
1381 register struct coff_symbol
*cs
;
1382 register union internal_auxent
*aux
;
1383 struct section_offsets
*section_offsets
;
1384 struct objfile
*objfile
;
1386 register struct symbol
*sym
1387 = (struct symbol
*) obstack_alloc (&objfile
->symbol_obstack
,
1388 sizeof (struct symbol
));
1391 memset (sym
, 0, sizeof (struct symbol
));
1393 name
= EXTERNAL_NAME (name
, objfile
->obfd
);
1394 SYMBOL_NAME (sym
) = obstack_copy0 (&objfile
->symbol_obstack
, name
,
1397 /* default assumptions */
1398 SYMBOL_VALUE (sym
) = cs
->c_value
;
1399 SYMBOL_NAMESPACE (sym
) = VAR_NAMESPACE
;
1400 SYMBOL_SECTION (sym
) = cs_to_section (cs
, objfile
);
1402 if (ISFCN (cs
->c_type
))
1404 SYMBOL_VALUE (sym
) += ANOFFSET (section_offsets
, SECT_OFF_TEXT
);
1406 /* FIXME: This has NOT been tested. The DBX version has.. */
1407 /* Generate a template for the type of this function. The
1408 types of the arguments will be added as we read the symbol
1410 struct type
*new = (struct type
*)
1411 obstack_alloc (&objfile
->symbol_obstack
, sizeof (struct type
));
1413 memcpy (new, lookup_function_type (decode_function_type (cs
, cs
->c_type
, aux
)),
1414 sizeof(struct type
));
1415 SYMBOL_TYPE (sym
) = new;
1416 in_function_type
= SYMBOL_TYPE(sym
);
1419 lookup_function_type (decode_function_type (cs
, cs
->c_type
, aux
));
1422 SYMBOL_CLASS (sym
) = LOC_BLOCK
;
1423 if (cs
->c_sclass
== C_STAT
)
1424 add_symbol_to_list (sym
, &file_symbols
);
1425 else if (cs
->c_sclass
== C_EXT
)
1426 add_symbol_to_list (sym
, &global_symbols
);
1430 SYMBOL_TYPE (sym
) = decode_type (cs
, cs
->c_type
, aux
);
1431 switch (cs
->c_sclass
)
1437 SYMBOL_CLASS (sym
) = LOC_LOCAL
;
1438 add_symbol_to_list (sym
, &local_symbols
);
1442 SYMBOL_CLASS (sym
) = LOC_STATIC
;
1443 SYMBOL_VALUE_ADDRESS (sym
) = (CORE_ADDR
) cs
->c_value
;
1444 SYMBOL_VALUE_ADDRESS (sym
) += ANOFFSET (section_offsets
, SECT_OFF_TEXT
);
1445 add_symbol_to_list (sym
, &global_symbols
);
1449 SYMBOL_CLASS (sym
) = LOC_STATIC
;
1450 SYMBOL_VALUE_ADDRESS (sym
) = (CORE_ADDR
) cs
->c_value
;
1451 SYMBOL_VALUE_ADDRESS (sym
) += ANOFFSET (section_offsets
, SECT_OFF_TEXT
);
1452 if (within_function
) {
1453 /* Static symbol of local scope */
1454 add_symbol_to_list (sym
, &local_symbols
);
1457 /* Static symbol at top level of file */
1458 add_symbol_to_list (sym
, &file_symbols
);
1462 #ifdef C_GLBLREG /* AMD coff */
1466 SYMBOL_CLASS (sym
) = LOC_REGISTER
;
1467 SYMBOL_VALUE (sym
) = SDB_REG_TO_REGNUM(cs
->c_value
);
1468 add_symbol_to_list (sym
, &local_symbols
);
1475 SYMBOL_CLASS (sym
) = LOC_ARG
;
1477 /* FIXME: This has not been tested. */
1478 /* Add parameter to function. */
1479 add_param_to_type(&in_function_type
,sym
);
1481 add_symbol_to_list (sym
, &local_symbols
);
1482 #if !defined (BELIEVE_PCC_PROMOTION)
1483 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
1485 /* If PCC says a parameter is a short or a char,
1486 aligned on an int boundary, realign it to the
1487 "little end" of the int. */
1488 struct type
*temptype
;
1489 temptype
= lookup_fundamental_type (current_objfile
,
1491 if (TYPE_LENGTH (SYMBOL_TYPE (sym
)) < TYPE_LENGTH (temptype
)
1492 && TYPE_CODE (SYMBOL_TYPE (sym
)) == TYPE_CODE_INT
1493 && 0 == SYMBOL_VALUE (sym
) % TYPE_LENGTH (temptype
))
1495 SYMBOL_VALUE (sym
) +=
1496 TYPE_LENGTH (temptype
)
1497 - TYPE_LENGTH (SYMBOL_TYPE (sym
));
1504 SYMBOL_CLASS (sym
) = LOC_REGPARM
;
1505 SYMBOL_VALUE (sym
) = SDB_REG_TO_REGNUM(cs
->c_value
);
1506 add_symbol_to_list (sym
, &local_symbols
);
1507 #if !defined (BELIEVE_PCC_PROMOTION)
1508 /* FIXME: This should retain the current type, since it's just
1509 a register value. gnu@adobe, 26Feb93 */
1511 /* If PCC says a parameter is a short or a char,
1512 it is really an int. */
1513 struct type
*temptype
;
1515 lookup_fundamental_type (current_objfile
, FT_INTEGER
);
1516 if (TYPE_LENGTH (SYMBOL_TYPE (sym
)) < TYPE_LENGTH (temptype
)
1517 && TYPE_CODE (SYMBOL_TYPE (sym
)) == TYPE_CODE_INT
)
1520 (TYPE_UNSIGNED (SYMBOL_TYPE (sym
))
1521 ? lookup_fundamental_type (current_objfile
,
1522 FT_UNSIGNED_INTEGER
)
1530 SYMBOL_CLASS (sym
) = LOC_TYPEDEF
;
1531 SYMBOL_NAMESPACE (sym
) = VAR_NAMESPACE
;
1533 /* If type has no name, give it one */
1534 if (TYPE_NAME (SYMBOL_TYPE (sym
)) == 0)
1536 if (TYPE_CODE (SYMBOL_TYPE (sym
)) == TYPE_CODE_PTR
1537 || TYPE_CODE (SYMBOL_TYPE (sym
)) == TYPE_CODE_FUNC
)
1539 /* If we are giving a name to a type such as "pointer to
1540 foo" or "function returning foo", we better not set
1541 the TYPE_NAME. If the program contains "typedef char
1542 *caddr_t;", we don't want all variables of type char
1543 * to print as caddr_t. This is not just a
1544 consequence of GDB's type management; CC and GCC (at
1545 least through version 2.4) both output variables of
1546 either type char * or caddr_t with the type
1547 refering to the C_TPDEF symbol for caddr_t. If a future
1548 compiler cleans this up it GDB is not ready for it
1549 yet, but if it becomes ready we somehow need to
1550 disable this check (without breaking the PCC/GCC2.4
1555 Fortunately, this check seems not to be necessary
1556 for anything except pointers or functions. */
1560 TYPE_NAME (SYMBOL_TYPE (sym
)) =
1561 concat (SYMBOL_NAME (sym
), NULL
);
1564 /* Ignore vendor section for Harris CX/UX targets. */
1565 else if (cs
->c_name
[0] == '$')
1567 #endif /* CXUX_TARGET */
1569 /* Keep track of any type which points to empty structured type,
1570 so it can be filled from a definition from another file. A
1571 simple forward reference (TYPE_CODE_UNDEF) is not an
1572 empty structured type, though; the forward references
1573 work themselves out via the magic of coff_lookup_type. */
1574 if (TYPE_CODE (SYMBOL_TYPE (sym
)) == TYPE_CODE_PTR
&&
1575 TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym
))) == 0 &&
1576 TYPE_CODE (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym
))) !=
1579 register int i
= hashname (SYMBOL_NAME (sym
));
1581 SYMBOL_VALUE_CHAIN (sym
) = opaque_type_chain
[i
];
1582 opaque_type_chain
[i
] = sym
;
1584 add_symbol_to_list (sym
, &file_symbols
);
1590 SYMBOL_CLASS (sym
) = LOC_TYPEDEF
;
1591 SYMBOL_NAMESPACE (sym
) = STRUCT_NAMESPACE
;
1593 /* Some compilers try to be helpful by inventing "fake"
1594 names for anonymous enums, structures, and unions, like
1595 "~0fake" or ".0fake". Thanks, but no thanks... */
1596 if (TYPE_TAG_NAME (SYMBOL_TYPE (sym
)) == 0)
1597 if (SYMBOL_NAME(sym
) != NULL
1598 && *SYMBOL_NAME(sym
) != '~'
1599 && *SYMBOL_NAME(sym
) != '.')
1600 TYPE_TAG_NAME (SYMBOL_TYPE (sym
)) =
1601 concat (SYMBOL_NAME (sym
), NULL
);
1603 add_symbol_to_list (sym
, &file_symbols
);
1613 /* Decode a coff type specifier; return the type that is meant. */
1615 static struct type
*
1616 decode_type (cs
, c_type
, aux
)
1617 register struct coff_symbol
*cs
;
1618 unsigned int c_type
;
1619 register union internal_auxent
*aux
;
1621 register struct type
*type
= 0;
1622 unsigned int new_c_type
;
1624 if (c_type
& ~N_BTMASK
)
1626 new_c_type
= DECREF (c_type
);
1629 type
= decode_type (cs
, new_c_type
, aux
);
1630 type
= lookup_pointer_type (type
);
1632 else if (ISFCN (c_type
))
1634 type
= decode_type (cs
, new_c_type
, aux
);
1635 type
= lookup_function_type (type
);
1637 else if (ISARY (c_type
))
1640 register unsigned short *dim
;
1641 struct type
*base_type
, *index_type
, *range_type
;
1643 /* Define an array type. */
1644 /* auxent refers to array, not base type */
1645 if (aux
->x_sym
.x_tagndx
.l
== 0)
1648 /* shift the indices down */
1649 dim
= &aux
->x_sym
.x_fcnary
.x_ary
.x_dimen
[0];
1652 for (i
= 0; *dim
&& i
< DIMNUM
- 1; i
++, dim
++)
1656 base_type
= decode_type (cs
, new_c_type
, aux
);
1657 index_type
= lookup_fundamental_type (current_objfile
, FT_INTEGER
);
1659 create_range_type ((struct type
*) NULL
, index_type
, 0, n
- 1);
1661 create_array_type ((struct type
*) NULL
, base_type
, range_type
);
1666 /* Reference to existing type. This only occurs with the
1667 struct, union, and enum types. EPI a29k coff
1668 fakes us out by producing aux entries with a nonzero
1669 x_tagndx for definitions of structs, unions, and enums, so we
1670 have to check the c_sclass field. SCO 3.2v4 cc gets confused
1671 with pointers to pointers to defined structs, and generates
1672 negative x_tagndx fields. */
1673 if (cs
->c_naux
> 0 && aux
->x_sym
.x_tagndx
.l
!= 0)
1675 if (cs
->c_sclass
!= C_STRTAG
1676 && cs
->c_sclass
!= C_UNTAG
1677 && cs
->c_sclass
!= C_ENTAG
1678 && aux
->x_sym
.x_tagndx
.l
>= 0)
1680 type
= coff_alloc_type (aux
->x_sym
.x_tagndx
.l
);
1685 complain (&tagndx_bad_complaint
, cs
->c_name
);
1686 /* And fall through to decode_base_type... */
1690 return decode_base_type (cs
, BTYPE (c_type
), aux
);
1693 /* Decode a coff type specifier for function definition;
1694 return the type that the function returns. */
1696 static struct type
*
1697 decode_function_type (cs
, c_type
, aux
)
1698 register struct coff_symbol
*cs
;
1699 unsigned int c_type
;
1700 register union internal_auxent
*aux
;
1702 if (aux
->x_sym
.x_tagndx
.l
== 0)
1703 cs
->c_naux
= 0; /* auxent refers to function, not base type */
1705 return decode_type (cs
, DECREF (c_type
), aux
);
1710 static struct type
*
1711 decode_base_type (cs
, c_type
, aux
)
1712 register struct coff_symbol
*cs
;
1713 unsigned int c_type
;
1714 register union internal_auxent
*aux
;
1721 /* shows up with "void (*foo)();" structure members */
1722 return lookup_fundamental_type (current_objfile
, FT_VOID
);
1725 /* DGUX actually defines both T_ARG and T_VOID to the same value. */
1728 /* Shows up in DGUX, I think. Not sure where. */
1729 return lookup_fundamental_type (current_objfile
, FT_VOID
); /* shouldn't show up here */
1735 /* Intel 960 COFF has this symbol and meaning. */
1736 return lookup_fundamental_type (current_objfile
, FT_VOID
);
1740 return lookup_fundamental_type (current_objfile
, FT_CHAR
);
1743 return lookup_fundamental_type (current_objfile
, FT_SHORT
);
1746 return lookup_fundamental_type (current_objfile
, FT_INTEGER
);
1749 return lookup_fundamental_type (current_objfile
, FT_LONG
);
1752 return lookup_fundamental_type (current_objfile
, FT_FLOAT
);
1755 return lookup_fundamental_type (current_objfile
, FT_DBL_PREC_FLOAT
);
1758 if (cs
->c_naux
!= 1)
1760 /* anonymous structure type */
1761 type
= coff_alloc_type (cs
->c_symnum
);
1762 TYPE_CODE (type
) = TYPE_CODE_STRUCT
;
1763 TYPE_NAME (type
) = NULL
;
1764 /* This used to set the tag to "<opaque>". But I think setting it
1765 to NULL is right, and the printing code can print it as
1767 TYPE_TAG_NAME (type
) = NULL
;
1768 INIT_CPLUS_SPECIFIC(type
);
1769 TYPE_LENGTH (type
) = 0;
1770 TYPE_FIELDS (type
) = 0;
1771 TYPE_NFIELDS (type
) = 0;
1775 type
= coff_read_struct_type (cs
->c_symnum
,
1776 aux
->x_sym
.x_misc
.x_lnsz
.x_size
,
1777 aux
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
);
1782 if (cs
->c_naux
!= 1)
1784 /* anonymous union type */
1785 type
= coff_alloc_type (cs
->c_symnum
);
1786 TYPE_NAME (type
) = NULL
;
1787 /* This used to set the tag to "<opaque>". But I think setting it
1788 to NULL is right, and the printing code can print it as
1790 TYPE_TAG_NAME (type
) = NULL
;
1791 INIT_CPLUS_SPECIFIC(type
);
1792 TYPE_LENGTH (type
) = 0;
1793 TYPE_FIELDS (type
) = 0;
1794 TYPE_NFIELDS (type
) = 0;
1798 type
= coff_read_struct_type (cs
->c_symnum
,
1799 aux
->x_sym
.x_misc
.x_lnsz
.x_size
,
1800 aux
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
);
1802 TYPE_CODE (type
) = TYPE_CODE_UNION
;
1806 if (cs
->c_naux
!= 1)
1808 /* anonymous enum type */
1809 type
= coff_alloc_type (cs
->c_symnum
);
1810 TYPE_CODE (type
) = TYPE_CODE_ENUM
;
1811 TYPE_NAME (type
) = NULL
;
1812 /* This used to set the tag to "<opaque>". But I think setting it
1813 to NULL is right, and the printing code can print it as
1815 TYPE_TAG_NAME (type
) = NULL
;
1816 TYPE_LENGTH (type
) = 0;
1817 TYPE_FIELDS (type
) = 0;
1818 TYPE_NFIELDS(type
) = 0;
1822 type
= coff_read_enum_type (cs
->c_symnum
,
1823 aux
->x_sym
.x_misc
.x_lnsz
.x_size
,
1824 aux
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
);
1829 /* shouldn't show up here */
1833 return lookup_fundamental_type (current_objfile
, FT_UNSIGNED_CHAR
);
1836 return lookup_fundamental_type (current_objfile
, FT_UNSIGNED_SHORT
);
1839 return lookup_fundamental_type (current_objfile
, FT_UNSIGNED_INTEGER
);
1842 return lookup_fundamental_type (current_objfile
, FT_UNSIGNED_LONG
);
1844 complain (&unexpected_type_complaint
, cs
->c_name
);
1845 return lookup_fundamental_type (current_objfile
, FT_VOID
);
1848 /* This page contains subroutines of read_type. */
1850 /* Read the description of a structure (or union type) and return an
1851 object describing the type. */
1853 static struct type
*
1854 coff_read_struct_type (index
, length
, lastsym
)
1861 struct nextfield
*next
;
1865 register struct type
*type
;
1866 register struct nextfield
*list
= 0;
1867 struct nextfield
*new;
1871 struct coff_symbol member_sym
;
1872 register struct coff_symbol
*ms
= &member_sym
;
1873 struct internal_syment sub_sym
;
1874 union internal_auxent sub_aux
;
1877 type
= coff_alloc_type (index
);
1878 TYPE_CODE (type
) = TYPE_CODE_STRUCT
;
1879 INIT_CPLUS_SPECIFIC(type
);
1880 TYPE_LENGTH (type
) = length
;
1882 while (!done
&& symnum
< lastsym
&& symnum
< nlist_nsyms_global
)
1884 read_one_sym (ms
, &sub_sym
, &sub_aux
);
1886 name
= EXTERNAL_NAME (name
, current_objfile
->obfd
);
1888 switch (ms
->c_sclass
)
1893 /* Get space to record the next field's data. */
1894 new = (struct nextfield
*) alloca (sizeof (struct nextfield
));
1898 /* Save the data. */
1902 ¤t_objfile
->symbol_obstack
);
1903 list
->field
.type
= decode_type (ms
, ms
->c_type
, &sub_aux
);
1904 list
->field
.bitpos
= 8 * ms
->c_value
;
1905 list
->field
.bitsize
= 0;
1911 /* Get space to record the next field's data. */
1912 new = (struct nextfield
*) alloca (sizeof (struct nextfield
));
1916 /* Save the data. */
1920 ¤t_objfile
->symbol_obstack
);
1921 list
->field
.type
= decode_type (ms
, ms
->c_type
, &sub_aux
);
1922 list
->field
.bitpos
= ms
->c_value
;
1923 list
->field
.bitsize
= sub_aux
.x_sym
.x_misc
.x_lnsz
.x_size
;
1932 /* Now create the vector of fields, and record how big it is. */
1934 TYPE_NFIELDS (type
) = nfields
;
1935 TYPE_FIELDS (type
) = (struct field
*)
1936 TYPE_ALLOC (type
, sizeof (struct field
) * nfields
);
1938 /* Copy the saved-up fields into the field vector. */
1940 for (n
= nfields
; list
; list
= list
->next
)
1941 TYPE_FIELD (type
, --n
) = list
->field
;
1946 /* Read a definition of an enumeration type,
1947 and create and return a suitable type object.
1948 Also defines the symbols that represent the values of the type. */
1951 static struct type
*
1952 coff_read_enum_type (index
, length
, lastsym
)
1957 register struct symbol
*sym
;
1958 register struct type
*type
;
1961 struct pending
**symlist
;
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
;
1966 struct pending
*osyms
, *syms
;
1971 type
= coff_alloc_type (index
);
1972 if (within_function
)
1973 symlist
= &local_symbols
;
1975 symlist
= &file_symbols
;
1977 o_nsyms
= osyms
? osyms
->nsyms
: 0;
1979 while (!done
&& symnum
< lastsym
&& symnum
< nlist_nsyms_global
)
1981 read_one_sym (ms
, &sub_sym
, &sub_aux
);
1983 name
= EXTERNAL_NAME (name
, current_objfile
->obfd
);
1985 switch (ms
->c_sclass
)
1988 sym
= (struct symbol
*) obstack_alloc
1989 (¤t_objfile
->symbol_obstack
,
1990 sizeof (struct symbol
));
1991 memset (sym
, 0, sizeof (struct symbol
));
1994 obsavestring (name
, strlen (name
),
1995 ¤t_objfile
->symbol_obstack
);
1996 SYMBOL_CLASS (sym
) = LOC_CONST
;
1997 SYMBOL_NAMESPACE (sym
) = VAR_NAMESPACE
;
1998 SYMBOL_VALUE (sym
) = ms
->c_value
;
1999 add_symbol_to_list (sym
, symlist
);
2004 /* Sometimes the linker (on 386/ix 2.0.2 at least) screws
2005 up the count of how many symbols to read. So stop
2012 /* Now fill in the fields of the type-structure. */
2015 TYPE_LENGTH (type
) = length
;
2017 TYPE_LENGTH (type
) = TARGET_INT_BIT
/ TARGET_CHAR_BIT
; /* Assume ints */
2018 TYPE_CODE (type
) = TYPE_CODE_ENUM
;
2019 TYPE_NFIELDS (type
) = nsyms
;
2020 TYPE_FIELDS (type
) = (struct field
*)
2021 TYPE_ALLOC (type
, sizeof (struct field
) * nsyms
);
2023 /* Find the symbols for the values and put them into the type.
2024 The symbols can be found in the symlist that we put them on
2025 to cause them to be defined. osyms contains the old value
2026 of that symlist; everything up to there was defined by us. */
2027 /* Note that we preserve the order of the enum constants, so
2028 that in something like "enum {FOO, LAST_THING=FOO}" we print
2029 FOO, not LAST_THING. */
2031 for (syms
= *symlist
, n
= 0; syms
; syms
= syms
->next
)
2037 for (; j
< syms
->nsyms
; j
++,n
++)
2039 struct symbol
*xsym
= syms
->symbol
[j
];
2040 SYMBOL_TYPE (xsym
) = type
;
2041 TYPE_FIELD_NAME (type
, n
) = SYMBOL_NAME (xsym
);
2042 TYPE_FIELD_VALUE (type
, n
) = 0;
2043 TYPE_FIELD_BITPOS (type
, n
) = SYMBOL_VALUE (xsym
);
2044 TYPE_FIELD_BITSIZE (type
, n
) = 0;
2053 struct section_offsets
*
2054 coff_symfile_offsets (objfile
, addr
)
2055 struct objfile
*objfile
;
2058 struct section_offsets
*section_offsets
;
2061 objfile
->num_sections
= SECT_OFF_MAX
;
2062 section_offsets
= (struct section_offsets
*)
2063 obstack_alloc (&objfile
-> psymbol_obstack
,
2064 sizeof (struct section_offsets
)
2065 + sizeof (section_offsets
->offsets
) * (SECT_OFF_MAX
-1));
2067 for (i
= 0; i
< SECT_OFF_MAX
; i
++)
2068 ANOFFSET (section_offsets
, i
) = addr
;
2070 return section_offsets
;
2073 /* Register our ability to parse symbols for coff BFD files. */
2075 static struct sym_fns coff_sym_fns
=
2077 bfd_target_coff_flavour
,
2078 coff_new_init
, /* sym_new_init: init anything gbl to entire symtab */
2079 coff_symfile_init
, /* sym_init: read initial info, setup for sym_read() */
2080 coff_symfile_read
, /* sym_read: read a symbol file into symtab */
2081 coff_symfile_finish
, /* sym_finish: finished with file, cleanup */
2082 coff_symfile_offsets
, /* sym_offsets: xlate external to internal form */
2083 NULL
/* next: pointer to next struct sym_fns */
2087 _initialize_coffread ()
2089 add_symtab_fns (&coff_sym_fns
);