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"
43 struct coff_symfile_info
{
44 file_ptr min_lineno_offset
; /* Where in file lowest line#s are */
45 file_ptr max_lineno_offset
; /* 1+last byte of line#s in file */
47 asection
*stabsect
; /* Section pointer for .stab section */
48 asection
*stabstrsect
; /* Section pointer for .stab section */
49 asection
*stabindexsect
; /* Section pointer for .stab.index section */
53 /* Translate an external name string into a user-visible name. */
54 #define EXTERNAL_NAME(string, abfd) \
55 (string[0] == bfd_get_symbol_leading_char(abfd)? string+1: string)
57 /* To be an sdb debug type, type must have at least a basic or primary
58 derived type. Using this rather than checking against T_NULL is
59 said to prevent core dumps if we try to operate on Michael Bloom
62 #define SDB_TYPE(type) (BTYPE(type) | (type & N_TMASK))
64 /* Convert from an sdb register number to an internal gdb register number.
65 This should be defined in tm.h, if REGISTER_NAMES is not set up
66 to map one to one onto the sdb register numbers. */
68 #ifndef SDB_REG_TO_REGNUM
69 # define SDB_REG_TO_REGNUM(value) (value)
72 /* Core address of start and end of text of current source file.
73 This comes from a ".text" symbol where x_nlinno > 0. */
75 static CORE_ADDR current_source_start_addr
;
76 static CORE_ADDR current_source_end_addr
;
78 /* The addresses of the symbol table stream and number of symbols
79 of the object file we are reading (as copied into core). */
81 static bfd
*nlist_bfd_global
;
82 static int nlist_nsyms_global
;
84 /* Vector of line number information. */
86 static struct linetable
*line_vector
;
88 /* Index of next entry to go in line_vector_index. */
90 static int line_vector_index
;
92 /* Last line number recorded in the line vector. */
94 static int prev_line_number
;
96 /* Number of elements allocated for line_vector currently. */
98 static int line_vector_length
;
100 /* Pointers to scratch storage, used for reading raw symbols and auxents. */
102 static char *temp_sym
;
103 static char *temp_aux
;
105 /* Local variables that hold the shift and mask values for the
106 COFF file that we are currently reading. These come back to us
107 from BFD, and are referenced by their macro names, as well as
108 internally to the BTYPE, ISPTR, ISFCN, ISARY, ISTAG, and DECREF
109 macros from include/coff/internal.h . */
111 static unsigned local_n_btmask
;
112 static unsigned local_n_btshft
;
113 static unsigned local_n_tmask
;
114 static unsigned local_n_tshift
;
116 #define N_BTMASK local_n_btmask
117 #define N_BTSHFT local_n_btshft
118 #define N_TMASK local_n_tmask
119 #define N_TSHIFT local_n_tshift
121 /* Local variables that hold the sizes in the file of various COFF structures.
122 (We only need to know this to read them from the file -- BFD will then
123 translate the data in them, into `internal_xxx' structs in the right
124 byte order, alignment, etc.) */
126 static unsigned local_linesz
;
127 static unsigned local_symesz
;
128 static unsigned local_auxesz
;
130 /* Chain of typedefs of pointers to empty struct/union types.
131 They are chained thru the SYMBOL_VALUE_CHAIN. */
133 static struct symbol
*opaque_type_chain
[HASHSIZE
];
136 /* The type of the function we are currently reading in. This is
137 used by define_symbol to record the type of arguments to a function. */
139 struct type
*in_function_type
;
142 /* Complaints about various problems in the file being read */
144 struct complaint ef_complaint
=
145 {"Unmatched .ef symbol(s) ignored starting at symnum %d", 0, 0};
147 struct complaint bf_no_aux_complaint
=
148 {"`.bf' symbol %d has no aux entry", 0, 0};
150 struct complaint ef_no_aux_complaint
=
151 {"`.ef' symbol %d has no aux entry", 0, 0};
153 struct complaint lineno_complaint
=
154 {"Line number pointer %d lower than start of line numbers", 0, 0};
156 struct complaint unexpected_type_complaint
=
157 {"Unexpected type for symbol %s", 0, 0};
159 struct complaint bad_sclass_complaint
=
160 {"Bad n_sclass for symbol %s", 0, 0};
162 struct complaint misordered_blocks_complaint
=
163 {"Blocks out of order at address %x", 0, 0};
165 struct complaint tagndx_bad_complaint
=
166 {"Symbol table entry for %s has bad tagndx value", 0, 0};
168 struct complaint eb_complaint
=
169 {"Mismatched .eb symbol ignored starting at symnum %d", 0, 0};
171 /* Simplified internal version of coff symbol table information */
175 int c_symnum
; /* symbol number of this entry */
176 int c_naux
; /* 0 if syment only, 1 if syment + auxent, etc */
183 static struct type
*coff_read_struct_type
PARAMS ((int, int, int));
185 static struct type
*decode_base_type
PARAMS ((struct coff_symbol
*,
187 union internal_auxent
*));
189 static struct type
*decode_type
PARAMS ((struct coff_symbol
*, unsigned int,
190 union internal_auxent
*));
192 static struct type
*decode_function_type
PARAMS ((struct coff_symbol
*,
194 union internal_auxent
*));
196 static struct type
*coff_read_enum_type
PARAMS ((int, int, int));
198 static struct symbol
*process_coff_symbol
PARAMS ((struct coff_symbol
*,
199 union internal_auxent
*,
200 struct section_offsets
*,
203 static void patch_opaque_types
PARAMS ((struct symtab
*));
205 static void patch_type
PARAMS ((struct type
*, struct type
*));
207 static void enter_linenos
PARAMS ((long, int, int, struct section_offsets
*));
209 static void free_linetab
PARAMS ((void));
211 static int init_lineno
PARAMS ((bfd
*, long, int));
213 static char *getsymname
PARAMS ((struct internal_syment
*));
215 static void free_stringtab
PARAMS ((void));
217 static int init_stringtab
PARAMS ((bfd
*, long));
219 static void read_one_sym
PARAMS ((struct coff_symbol
*,
220 struct internal_syment
*,
221 union internal_auxent
*));
223 static void coff_symtab_read
PARAMS ((long, int, struct section_offsets
*,
226 static void find_linenos
PARAMS ((bfd
*, sec_ptr
, PTR
));
228 static void coff_symfile_init
PARAMS ((struct objfile
*));
230 static void coff_new_init
PARAMS ((struct objfile
*));
232 static void coff_symfile_read
PARAMS ((struct objfile
*,
233 struct section_offsets
*, int));
235 static void coff_symfile_finish
PARAMS ((struct objfile
*));
237 static void record_minimal_symbol
PARAMS ((char *, CORE_ADDR
,
238 enum minimal_symbol_type
,
241 static void coff_end_symtab
PARAMS ((struct objfile
*));
243 static void complete_symtab
PARAMS ((char *, CORE_ADDR
, unsigned int));
245 static void coff_start_symtab
PARAMS ((void));
247 static void coff_record_line
PARAMS ((int, CORE_ADDR
));
249 static struct type
*coff_alloc_type
PARAMS ((int));
251 static struct type
**coff_lookup_type
PARAMS ((int));
253 static void coff_locate_sections
PARAMS ((bfd
*, asection
*, PTR
));
255 /* We are called once per section from coff_symfile_read. We
256 need to examine each section we are passed, check to see
257 if it is something we are interested in processing, and
258 if so, stash away some access information for the section.
260 FIXME: The section names should not be hardwired strings (what
261 should they be? I don't think most object file formats have enough
262 section flags to specify what kind of debug section it is
266 coff_locate_sections (ignore_abfd
, sectp
, csip
)
271 register struct coff_symfile_info
*csi
;
273 csi
= (struct coff_symfile_info
*) csip
;
274 if (STREQ (sectp
->name
, ".stab"))
276 csi
->stabsect
= sectp
;
278 else if (STREQ (sectp
->name
, ".stabstr"))
280 csi
->stabstrsect
= sectp
;
282 else if (STREQ (sectp
->name
, ".stab.index"))
284 csi
->stabindexsect
= sectp
;
288 /* Return the section_offsets* that CS points to. */
289 static int cs_to_section
PARAMS ((struct coff_symbol
*, struct objfile
*));
291 struct find_targ_sec_arg
{
296 static void find_targ_sec
PARAMS ((bfd
*, asection
*, void *));
298 static void find_targ_sec (abfd
, sect
, obj
)
303 struct find_targ_sec_arg
*args
= (struct find_targ_sec_arg
*)obj
;
304 if (sect
->target_index
== args
->targ_index
)
306 /* This is the section. Figure out what SECT_OFF_* code it is. */
307 if (bfd_get_section_flags (abfd
, sect
) & SEC_CODE
)
308 *args
->resultp
= SECT_OFF_TEXT
;
309 else if (bfd_get_section_flags (abfd
, sect
) & SEC_LOAD
)
310 *args
->resultp
= SECT_OFF_DATA
;
312 *args
->resultp
= SECT_OFF_BSS
;
316 /* Return the section number (SECT_OFF_*) that CS points to. */
318 cs_to_section (cs
, objfile
)
319 struct coff_symbol
*cs
;
320 struct objfile
*objfile
;
322 int off
= SECT_OFF_TEXT
;
323 struct find_targ_sec_arg args
;
324 args
.targ_index
= cs
->c_secnum
;
326 bfd_map_over_sections (objfile
->obfd
, find_targ_sec
, &args
);
330 /* Look up a coff type-number index. Return the address of the slot
331 where the type for that index is stored.
332 The type-number is in INDEX.
334 This can be used for finding the type associated with that index
335 or for associating a new type with the index. */
337 static struct type
**
338 coff_lookup_type (index
)
341 if (index
>= type_vector_length
)
343 int old_vector_length
= type_vector_length
;
345 type_vector_length
*= 2;
346 if (index
/* is still */ >= type_vector_length
)
347 type_vector_length
= index
* 2;
349 type_vector
= (struct type
**)
350 xrealloc ((char *) type_vector
,
351 type_vector_length
* sizeof (struct type
*));
352 memset (&type_vector
[old_vector_length
], 0,
353 (type_vector_length
- old_vector_length
) * sizeof(struct type
*));
355 return &type_vector
[index
];
358 /* Make sure there is a type allocated for type number index
359 and return the type object.
360 This can create an empty (zeroed) type object. */
363 coff_alloc_type (index
)
366 register struct type
**type_addr
= coff_lookup_type (index
);
367 register struct type
*type
= *type_addr
;
369 /* If we are referring to a type not known at all yet,
370 allocate an empty type for it.
371 We will fill it in later if we find out how. */
374 type
= alloc_type (current_objfile
);
380 /* Record a line number entry for line LINE at address PC.
381 FIXME: Use record_line instead. */
384 coff_record_line (line
, pc
)
388 struct linetable_entry
*e
;
389 /* Make sure line vector is big enough. */
391 if (line_vector_index
+ 2 >= line_vector_length
)
393 line_vector_length
*= 2;
394 line_vector
= (struct linetable
*)
395 xrealloc ((char *) line_vector
, sizeof (struct linetable
)
396 + (line_vector_length
397 * sizeof (struct linetable_entry
)));
400 e
= line_vector
->item
+ line_vector_index
++;
401 e
->line
= line
; e
->pc
= pc
;
404 /* Start a new symtab for a new source file.
405 This is called when a COFF ".file" symbol is seen;
406 it indicates the start of data for one original source file. */
412 /* We fill in the filename later. start_symtab puts
413 this pointer into last_source_file and we put it in
414 subfiles->name, which end_symtab frees; that's why
415 it must be malloc'd. */
417 /* We never know the directory name for COFF. */
419 /* The start address is irrelevant, since we set
420 last_source_start_addr in coff_end_symtab. */
423 /* Initialize the source file line number information for this file. */
425 if (line_vector
) /* Unlikely, but maybe possible? */
426 free ((PTR
)line_vector
);
427 line_vector_index
= 0;
428 line_vector_length
= 1000;
429 prev_line_number
= -2; /* Force first line number to be explicit */
430 line_vector
= (struct linetable
*)
431 xmalloc (sizeof (struct linetable
)
432 + line_vector_length
* sizeof (struct linetable_entry
));
435 /* Save the vital information from when starting to read a file,
436 for use when closing off the current file.
437 NAME is the file name the symbols came from, START_ADDR is the first
438 text address for the file, and SIZE is the number of bytes of text. */
441 complete_symtab (name
, start_addr
, size
)
443 CORE_ADDR start_addr
;
446 if (last_source_file
!= NULL
)
447 free (last_source_file
);
448 last_source_file
= savestring (name
, strlen (name
));
449 current_source_start_addr
= start_addr
;
450 current_source_end_addr
= start_addr
+ size
;
452 if (current_objfile
-> ei
.entry_point
>= current_source_start_addr
&&
453 current_objfile
-> ei
.entry_point
< current_source_end_addr
)
455 current_objfile
-> ei
.entry_file_lowpc
= current_source_start_addr
;
456 current_objfile
-> ei
.entry_file_highpc
= current_source_end_addr
;
460 /* Finish the symbol definitions for one main source file,
461 close off all the lexical contexts for that file
462 (creating struct block's for them), then make the
463 struct symtab for that file and put it in the list of all such. */
466 coff_end_symtab (objfile
)
467 struct objfile
*objfile
;
469 struct symtab
*symtab
;
471 last_source_start_addr
= current_source_start_addr
;
473 /* For no good reason, this file stores the number of entries in a
474 separate variable instead of in line_vector->nitems. Fix it. */
476 line_vector
->nitems
= line_vector_index
;
478 /* For COFF, we only have one subfile, so we can just look at
479 subfiles and not worry about there being other elements in the
480 chain. We fill in various fields now because we didn't know them
481 before (or because doing it now is simply an artifact of how this
482 file used to be written). */
483 subfiles
->line_vector
= line_vector
;
484 subfiles
->name
= last_source_file
;
486 /* sort_pending is needed for amdcoff, at least.
487 sort_linevec is needed for the SCO compiler. */
488 symtab
= end_symtab (current_source_end_addr
, 1, 1, objfile
, 0);
491 free_named_symtabs (symtab
->filename
);
493 /* Reinitialize for beginning of new file. */
495 line_vector_length
= -1;
496 last_source_file
= NULL
;
500 record_minimal_symbol (name
, address
, type
, objfile
)
503 enum minimal_symbol_type type
;
504 struct objfile
*objfile
;
506 /* We don't want TDESC entry points in the minimal symbol table */
507 if (name
[0] == '@') return;
509 prim_record_minimal_symbol
510 (obsavestring (name
, strlen (name
), &objfile
->symbol_obstack
),
515 /* coff_symfile_init ()
516 is the coff-specific initialization routine for reading symbols.
517 It is passed a struct objfile which contains, among other things,
518 the BFD for the file whose symbols are being read, and a slot for
519 a pointer to "private data" which we fill with cookies and other
520 treats for coff_symfile_read ().
522 We will only be called if this is a COFF or COFF-like file.
523 BFD handles figuring out the format of the file, and code in symtab.c
524 uses BFD's determination to vector to us.
526 The ultimate result is a new symtab (or, FIXME, eventually a psymtab). */
529 coff_symfile_init (objfile
)
530 struct objfile
*objfile
;
532 bfd
*abfd
= objfile
->obfd
;
534 /* Allocate struct to keep track of stab reading. */
535 objfile
->sym_stab_info
= (PTR
)
536 xmmalloc (objfile
-> md
, sizeof (struct dbx_symfile_info
));
538 memset ((PTR
) objfile
->sym_stab_info
, 0, sizeof (struct dbx_symfile_info
));
540 /* Allocate struct to keep track of the symfile */
541 objfile
-> sym_private
= xmmalloc (objfile
-> md
,
542 sizeof (struct coff_symfile_info
));
544 memset (objfile
->sym_private
, 0, sizeof (struct coff_symfile_info
));
546 init_entry_point_info (objfile
);
549 /* This function is called for every section; it finds the outer limits
550 of the line table (minimum and maximum file offset) so that the
551 mainline code can read the whole thing for efficiency. */
555 find_linenos (abfd
, asect
, vpinfo
)
560 struct coff_symfile_info
*info
;
562 file_ptr offset
, maxoff
;
564 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
565 count
= asect
->lineno_count
;
570 size
= count
* local_linesz
;
572 info
= (struct coff_symfile_info
*)vpinfo
;
573 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
574 offset
= asect
->line_filepos
;
577 if (offset
< info
->min_lineno_offset
|| info
->min_lineno_offset
== 0)
578 info
->min_lineno_offset
= offset
;
580 maxoff
= offset
+ size
;
581 if (maxoff
> info
->max_lineno_offset
)
582 info
->max_lineno_offset
= maxoff
;
586 /* The BFD for this file -- only good while we're actively reading
587 symbols into a psymtab or a symtab. */
589 static bfd
*symfile_bfd
;
591 /* Read a symbol file, after initialization by coff_symfile_init. */
595 coff_symfile_read (objfile
, section_offsets
, mainline
)
596 struct objfile
*objfile
;
597 struct section_offsets
*section_offsets
;
600 struct coff_symfile_info
*info
;
601 struct dbx_symfile_info
*dbxinfo
;
602 bfd
*abfd
= objfile
->obfd
;
603 coff_data_type
*cdata
= coff_data (abfd
);
604 char *name
= bfd_get_filename (abfd
);
608 int stringtab_offset
;
609 struct cleanup
*back_to
;
610 int stabsize
, stabstrsize
;
612 info
= (struct coff_symfile_info
*) objfile
-> sym_private
;
613 dbxinfo
= (struct dbx_symfile_info
*) objfile
->sym_stab_info
;
614 symfile_bfd
= abfd
; /* Kludge for swap routines */
616 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
617 num_symbols
= bfd_get_symcount (abfd
); /* How many syms */
618 symtab_offset
= cdata
->sym_filepos
; /* Symbol table file offset */
619 stringtab_offset
= symtab_offset
+ /* String table file offset */
620 num_symbols
* cdata
->local_symesz
;
622 /* Set a few file-statics that give us specific information about
623 the particular COFF file format we're reading. */
624 local_linesz
= cdata
->local_linesz
;
625 local_n_btmask
= cdata
->local_n_btmask
;
626 local_n_btshft
= cdata
->local_n_btshft
;
627 local_n_tmask
= cdata
->local_n_tmask
;
628 local_n_tshift
= cdata
->local_n_tshift
;
629 local_linesz
= cdata
->local_linesz
;
630 local_symesz
= cdata
->local_symesz
;
631 local_auxesz
= cdata
->local_auxesz
;
633 /* Allocate space for raw symbol and aux entries, based on their
634 space requirements as reported by BFD. */
635 temp_sym
= (char *) xmalloc
636 (cdata
->local_symesz
+ cdata
->local_auxesz
);
637 temp_aux
= temp_sym
+ cdata
->local_symesz
;
638 back_to
= make_cleanup (free_current_contents
, &temp_sym
);
641 /* Read the line number table, all at once. */
642 info
->min_lineno_offset
= 0;
643 info
->max_lineno_offset
= 0;
644 bfd_map_over_sections (abfd
, find_linenos
, (PTR
) info
);
646 make_cleanup (free_linetab
, 0);
647 val
= init_lineno (abfd
, info
->min_lineno_offset
,
648 info
->max_lineno_offset
- info
->min_lineno_offset
);
650 error ("\"%s\": error reading line numbers\n", name
);
652 /* Now read the string table, all at once. */
654 make_cleanup (free_stringtab
, 0);
655 val
= init_stringtab (abfd
, stringtab_offset
);
657 error ("\"%s\": can't get string table", name
);
659 init_minimal_symbol_collection ();
660 make_cleanup (discard_minimal_symbols
, 0);
662 /* Now that the executable file is positioned at symbol table,
663 process it and define symbols accordingly. */
665 coff_symtab_read ((long) symtab_offset
, num_symbols
, section_offsets
,
668 /* Sort symbols alphabetically within each block. */
673 for (s
= objfile
-> symtabs
; s
!= NULL
; s
= s
-> next
)
674 sort_symtab_syms (s
);
677 /* Install any minimal symbols that have been collected as the current
678 minimal symbols for this objfile. */
680 install_minimal_symbols (objfile
);
682 bfd_map_over_sections (abfd
, coff_locate_sections
, (PTR
) info
);
686 /* FIXME: dubious. Why can't we use something normal like
687 bfd_get_section_contents? */
688 bfd_seek (abfd
, abfd
->where
, 0);
690 stabsize
= bfd_section_size (abfd
, info
->stabsect
);
691 stabstrsize
= bfd_section_size (abfd
, info
->stabstrsect
);
693 coffstab_build_psymtabs (objfile
,
696 info
->stabsect
->filepos
, stabsize
,
697 info
->stabstrsect
->filepos
, stabstrsize
);
700 do_cleanups (back_to
);
704 coff_new_init (ignore
)
705 struct objfile
*ignore
;
709 /* Perform any local cleanups required when we are done with a particular
710 objfile. I.E, we are in the process of discarding all symbol information
711 for an objfile, freeing up all memory held for it, and unlinking the
712 objfile struct from the global list of known objfiles. */
715 coff_symfile_finish (objfile
)
716 struct objfile
*objfile
;
718 if (objfile
-> sym_private
!= NULL
)
720 mfree (objfile
-> md
, objfile
-> sym_private
);
725 /* Given pointers to a symbol table in coff style exec file,
726 analyze them and create struct symtab's describing the symbols.
727 NSYMS is the number of symbols in the symbol table.
728 We read them one at a time using read_one_sym (). */
731 coff_symtab_read (symtab_offset
, nsyms
, section_offsets
, objfile
)
734 struct section_offsets
*section_offsets
;
735 struct objfile
*objfile
;
737 register struct context_stack
*new;
738 struct coff_symbol coff_symbol
;
739 register struct coff_symbol
*cs
= &coff_symbol
;
740 static struct internal_syment main_sym
;
741 static union internal_auxent main_aux
;
742 struct coff_symbol fcn_cs_saved
;
743 static struct internal_syment fcn_sym_saved
;
744 static union internal_auxent fcn_aux_saved
;
746 /* A .file is open. */
747 int in_source_file
= 0;
748 int next_file_symnum
= -1;
749 /* Name of the current file. */
750 char *filestring
= "";
752 int fcn_first_line
= 0;
753 int fcn_last_line
= 0;
754 int fcn_start_addr
= 0;
755 long fcn_line_ptr
= 0;
759 /* Work around a stdio bug in SunOS4.1.1 (this makes me nervous....
760 it's hard to know I've really worked around it. The fix should be
761 harmless, anyway). The symptom of the bug is that the first
762 fread (in read_one_sym), will (in my example) actually get data
763 from file offset 268, when the fseek was to 264 (and ftell shows
764 264). This causes all hell to break loose. I was unable to
765 reproduce this on a short test program which operated on the same
766 file, performing (I think) the same sequence of operations.
768 It stopped happening when I put in this (former) rewind().
770 FIXME: Find out if this has been reported to Sun, whether it has
771 been fixed in a later release, etc. */
773 bfd_seek (objfile
->obfd
, 0, 0);
775 /* Position to read the symbol table. */
776 val
= bfd_seek (objfile
->obfd
, (long) symtab_offset
, 0);
778 perror_with_name (objfile
->name
);
780 current_objfile
= objfile
;
781 nlist_bfd_global
= objfile
->obfd
;
782 nlist_nsyms_global
= nsyms
;
783 last_source_file
= NULL
;
784 memset (opaque_type_chain
, 0, sizeof opaque_type_chain
);
786 if (type_vector
) /* Get rid of previous one */
787 free ((PTR
) type_vector
);
788 type_vector_length
= 160;
789 type_vector
= (struct type
**)
790 xmalloc (type_vector_length
* sizeof (struct type
*));
791 memset (type_vector
, 0, type_vector_length
* sizeof (struct type
*));
793 coff_start_symtab ();
796 while (symnum
< nsyms
)
798 QUIT
; /* Make this command interruptable. */
800 read_one_sym (cs
, &main_sym
, &main_aux
);
803 temp_sem_val
= cs
->c_name
[0] << 24 | cs
->c_name
[1] << 16 |
804 cs
->c_name
[2] << 8 | cs
->c_name
[3];
805 if (int_sem_val
== temp_sem_val
)
806 last_coffsem
= (int) strtol (cs
->c_name
+4, (char **) NULL
, 10);
809 if (cs
->c_symnum
== next_file_symnum
&& cs
->c_sclass
!= C_FILE
)
811 if (last_source_file
)
812 coff_end_symtab (objfile
);
814 coff_start_symtab ();
815 complete_symtab ("_globals_", 0, 0);
816 /* done with all files, everything from here on out is globals */
819 /* Special case for file with type declarations only, no text. */
820 if (!last_source_file
&& SDB_TYPE (cs
->c_type
)
821 && cs
->c_secnum
== N_DEBUG
)
822 complete_symtab (filestring
, 0, 0);
824 /* Typedefs should not be treated as symbol definitions. */
825 if (ISFCN (cs
->c_type
) && cs
->c_sclass
!= C_TPDEF
)
827 /* Record all functions -- external and static -- in minsyms. */
828 tmpaddr
= cs
->c_value
+ ANOFFSET (section_offsets
, SECT_OFF_TEXT
);
829 record_minimal_symbol (cs
->c_name
, tmpaddr
, mst_text
, objfile
);
831 fcn_line_ptr
= main_aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
;
832 fcn_start_addr
= tmpaddr
;
834 fcn_sym_saved
= main_sym
;
835 fcn_aux_saved
= main_aux
;
839 switch (cs
->c_sclass
)
848 complain (&bad_sclass_complaint
, cs
->c_name
);
852 /* c_value field contains symnum of next .file entry in table
853 or symnum of first global after last .file. */
854 next_file_symnum
= cs
->c_value
;
856 filestring
= coff_getfilename (&main_aux
);
860 /* Complete symbol table for last object file
861 containing debugging information. */
862 if (last_source_file
)
864 coff_end_symtab (objfile
);
865 coff_start_symtab ();
871 if (cs
->c_name
[0] == '.')
873 if (STREQ (cs
->c_name
, ".text")) {
874 /* FIXME: don't wire in ".text" as section name
876 /* Check for in_source_file deals with case of
877 a file with debugging symbols
878 followed by a later file with no symbols. */
880 complete_symtab (filestring
,
881 cs
->c_value
+ ANOFFSET (section_offsets
, SECT_OFF_TEXT
),
882 main_aux
.x_scn
.x_scnlen
);
885 /* flush rest of '.' symbols */
888 else if (!SDB_TYPE (cs
->c_type
)
889 && cs
->c_name
[0] == 'L'
890 && (strncmp (cs
->c_name
, "LI%", 3) == 0
891 || strncmp (cs
->c_name
, "LF%", 3) == 0
892 || strncmp (cs
->c_name
,"LC%",3) == 0
893 || strncmp (cs
->c_name
,"LP%",3) == 0
894 || strncmp (cs
->c_name
,"LPB%",4) == 0
895 || strncmp (cs
->c_name
,"LBB%",4) == 0
896 || strncmp (cs
->c_name
,"LBE%",4) == 0
897 || strncmp (cs
->c_name
,"LPBX%",5) == 0))
898 /* At least on a 3b1, gcc generates swbeg and string labels
899 that look like this. Ignore them. */
901 /* fall in for static symbols that don't start with '.' */
904 /* Record it in the minimal symbols regardless of
905 SDB_TYPE. This parallels what we do for other debug
906 formats, and probably is needed to make
907 print_address_symbolic work right without the (now
908 gone) "set fast-symbolic-addr off" kludge. */
910 /* FIXME: should use mst_abs, and not relocate, if absolute. */
911 enum minimal_symbol_type ms_type
;
914 if (cs
->c_secnum
== N_UNDEF
)
916 /* This is a common symbol. See if the target
917 environment knows where it has been relocated to. */
919 if (target_lookup_symbol (cs
->c_name
, &reladdr
))
921 /* Error in lookup; ignore symbol. */
929 sec
= cs_to_section (cs
, objfile
);
930 tmpaddr
= cs
->c_value
;
931 if (cs
->c_sclass
!= C_STAT
)
932 tmpaddr
+= ANOFFSET (section_offsets
, sec
);
938 case SECT_OFF_RODATA
:
939 ms_type
= cs
->c_sclass
== C_STAT
? mst_file_text
: mst_text
;
942 ms_type
= cs
->c_sclass
== C_STAT
? mst_file_data
: mst_data
;
945 ms_type
= cs
->c_sclass
== C_STAT
? mst_file_bss
: mst_bss
;
948 ms_type
= mst_unknown
;
952 record_minimal_symbol (cs
->c_name
, tmpaddr
, ms_type
, objfile
);
955 if (SDB_TYPE (cs
->c_type
))
956 process_coff_symbol (cs
, &main_aux
, section_offsets
, objfile
);
960 if (STREQ (cs
->c_name
, ".bf"))
964 /* value contains address of first non-init type code */
965 /* main_aux.x_sym.x_misc.x_lnsz.x_lnno
966 contains line number of '{' } */
968 complain (&bf_no_aux_complaint
, cs
->c_symnum
);
969 fcn_first_line
= main_aux
.x_sym
.x_misc
.x_lnsz
.x_lnno
;
971 /* Might want to check that locals are 0 and
972 context_stack_depth is zero, and complain if not. */
975 new = push_context (depth
, fcn_start_addr
);
976 fcn_cs_saved
.c_name
= getsymname (&fcn_sym_saved
);
978 process_coff_symbol (&fcn_cs_saved
, &fcn_aux_saved
,
979 section_offsets
, objfile
);
981 else if (STREQ (cs
->c_name
, ".ef"))
983 /* the value of .ef is the address of epilogue code;
984 not useful for gdb. */
985 /* { main_aux.x_sym.x_misc.x_lnsz.x_lnno
986 contains number of lines to '}' */
987 new = pop_context ();
988 /* Stack must be empty now. */
989 if (context_stack_depth
> 0 || new == NULL
)
991 complain (&ef_complaint
, cs
->c_symnum
);
997 complain (&ef_no_aux_complaint
, cs
->c_symnum
);
998 fcn_last_line
= 0x7FFFFFFF;
1002 fcn_last_line
= main_aux
.x_sym
.x_misc
.x_lnsz
.x_lnno
;
1004 enter_linenos (fcn_line_ptr
, fcn_first_line
, fcn_last_line
,
1007 finish_block (new->name
, &local_symbols
, new->old_blocks
,
1009 #if defined (FUNCTION_EPILOGUE_SIZE)
1010 /* This macro should be defined only on
1012 fcn_aux_saved.x_sym.x_misc.x_fsize
1013 field is always zero.
1014 So use the .bf record information that
1015 points to the epilogue and add the size
1018 + FUNCTION_EPILOGUE_SIZE
1019 + ANOFFSET (section_offsets
, SECT_OFF_TEXT
),
1021 fcn_cs_saved
.c_value
1022 + fcn_aux_saved
.x_sym
.x_misc
.x_fsize
1023 + ANOFFSET (section_offsets
, SECT_OFF_TEXT
),
1027 within_function
= 0;
1032 if (STREQ (cs
->c_name
, ".bb"))
1034 tmpaddr
= cs
->c_value
;
1035 tmpaddr
+= ANOFFSET (section_offsets
, SECT_OFF_TEXT
);
1036 push_context (++depth
, tmpaddr
);
1038 else if (STREQ (cs
->c_name
, ".eb"))
1040 new = pop_context ();
1041 if (depth
-- != new->depth
)
1043 complain (&eb_complaint
, symnum
);
1046 if (local_symbols
&& context_stack_depth
> 0)
1049 cs
->c_value
+ ANOFFSET (section_offsets
, SECT_OFF_TEXT
);
1050 /* Make a block for the local symbols within. */
1051 finish_block (0, &local_symbols
, new->old_blocks
,
1052 new->start_addr
, tmpaddr
, objfile
);
1054 /* Now pop locals of block just finished. */
1055 local_symbols
= new->locals
;
1060 process_coff_symbol (cs
, &main_aux
, section_offsets
, objfile
);
1065 if (last_source_file
)
1066 coff_end_symtab (objfile
);
1068 /* Patch up any opaque types (references to types that are not defined
1069 in the file where they are referenced, e.g. "struct foo *bar"). */
1070 ALL_OBJFILE_SYMTABS (objfile
, s
)
1071 patch_opaque_types (s
);
1073 current_objfile
= NULL
;
1076 /* Routines for reading headers and symbols from executable. */
1078 /* Read the next symbol, swap it, and return it in both internal_syment
1079 form, and coff_symbol form. Also return its first auxent, if any,
1080 in internal_auxent form, and skip any other auxents. */
1083 read_one_sym (cs
, sym
, aux
)
1084 register struct coff_symbol
*cs
;
1085 register struct internal_syment
*sym
;
1086 register union internal_auxent
*aux
;
1090 cs
->c_symnum
= symnum
;
1091 bfd_read (temp_sym
, local_symesz
, 1, nlist_bfd_global
);
1092 bfd_coff_swap_sym_in (symfile_bfd
, temp_sym
, (char *)sym
);
1093 cs
->c_naux
= sym
->n_numaux
& 0xff;
1094 if (cs
->c_naux
>= 1)
1096 bfd_read (temp_aux
, local_auxesz
, 1, nlist_bfd_global
);
1097 bfd_coff_swap_aux_in (symfile_bfd
, temp_aux
, sym
->n_type
, sym
->n_sclass
,
1098 0, cs
->c_naux
, (char *)aux
);
1099 /* If more than one aux entry, read past it (only the first aux
1101 for (i
= 1; i
< cs
->c_naux
; i
++)
1102 bfd_read (temp_aux
, local_auxesz
, 1, nlist_bfd_global
);
1104 cs
->c_name
= getsymname (sym
);
1105 cs
->c_value
= sym
->n_value
;
1106 cs
->c_sclass
= (sym
->n_sclass
& 0xff);
1107 cs
->c_secnum
= sym
->n_scnum
;
1108 cs
->c_type
= (unsigned) sym
->n_type
;
1109 if (!SDB_TYPE (cs
->c_type
))
1112 symnum
+= 1 + cs
->c_naux
;
1115 /* Support for string table handling */
1117 static char *stringtab
= NULL
;
1120 init_stringtab (abfd
, offset
)
1126 unsigned char lengthbuf
[4];
1130 /* If the file is stripped, the offset might be zero, indicating no
1131 string table. Just return with `stringtab' set to null. */
1135 if (bfd_seek (abfd
, offset
, 0) < 0)
1138 val
= bfd_read ((char *)lengthbuf
, sizeof lengthbuf
, 1, abfd
);
1139 length
= bfd_h_get_32 (symfile_bfd
, lengthbuf
);
1141 /* If no string table is needed, then the file may end immediately
1142 after the symbols. Just return with `stringtab' set to null. */
1143 if (val
!= sizeof lengthbuf
|| length
< sizeof lengthbuf
)
1146 stringtab
= (char *) xmalloc (length
);
1147 /* This is in target format (probably not very useful, and not currently
1148 used), not host format. */
1149 memcpy (stringtab
, lengthbuf
, sizeof lengthbuf
);
1150 if (length
== sizeof length
) /* Empty table -- just the count */
1153 val
= bfd_read (stringtab
+ sizeof lengthbuf
, length
- sizeof lengthbuf
, 1, abfd
);
1154 if (val
!= length
- sizeof lengthbuf
|| stringtab
[length
- 1] != '\0')
1169 getsymname (symbol_entry
)
1170 struct internal_syment
*symbol_entry
;
1172 static char buffer
[SYMNMLEN
+1];
1175 if (symbol_entry
->_n
._n_n
._n_zeroes
== 0)
1177 /* FIXME: Probably should be detecting corrupt symbol files by
1178 seeing whether offset points to within the stringtab. */
1179 result
= stringtab
+ symbol_entry
->_n
._n_n
._n_offset
;
1183 strncpy (buffer
, symbol_entry
->_n
._n_name
, SYMNMLEN
);
1184 buffer
[SYMNMLEN
] = '\0';
1190 /* Extract the file name from the aux entry of a C_FILE symbol. Return
1191 only the last component of the name. Result is in static storage and
1192 is only good for temporary use. */
1195 coff_getfilename (aux_entry
)
1196 union internal_auxent
*aux_entry
;
1198 static char buffer
[BUFSIZ
];
1199 register char *temp
;
1202 if (aux_entry
->x_file
.x_n
.x_zeroes
== 0)
1203 strcpy (buffer
, stringtab
+ aux_entry
->x_file
.x_n
.x_offset
);
1206 strncpy (buffer
, aux_entry
->x_file
.x_fname
, FILNMLEN
);
1207 buffer
[FILNMLEN
] = '\0';
1211 /* FIXME: We should not be throwing away the information about what
1212 directory. It should go into dirname of the symtab, or some such
1214 if ((temp
= strrchr (result
, '/')) != NULL
)
1219 /* Support for line number handling. */
1221 static char *linetab
= NULL
;
1222 static long linetab_offset
;
1223 static unsigned long linetab_size
;
1225 /* Read in all the line numbers for fast lookups later. Leave them in
1226 external (unswapped) format in memory; we'll swap them as we enter
1227 them into GDB's data structures. */
1230 init_lineno (abfd
, offset
, size
)
1237 linetab_offset
= offset
;
1238 linetab_size
= size
;
1245 if (bfd_seek (abfd
, offset
, 0) < 0)
1248 /* Allocate the desired table, plus a sentinel */
1249 linetab
= (char *) xmalloc (size
+ local_linesz
);
1251 val
= bfd_read (linetab
, size
, 1, abfd
);
1255 /* Terminate it with an all-zero sentinel record */
1256 memset (linetab
+ size
, 0, local_linesz
);
1269 #if !defined (L_LNNO32)
1270 #define L_LNNO32(lp) ((lp)->l_lnno)
1274 enter_linenos (file_offset
, first_line
, last_line
, section_offsets
)
1276 register int first_line
;
1277 register int last_line
;
1278 struct section_offsets
*section_offsets
;
1280 register char *rawptr
;
1281 struct internal_lineno lptr
;
1283 if (file_offset
< linetab_offset
)
1285 complain (&lineno_complaint
, file_offset
);
1286 if (file_offset
> linetab_size
) /* Too big to be an offset? */
1288 file_offset
+= linetab_offset
; /* Try reading at that linetab offset */
1291 rawptr
= &linetab
[file_offset
- linetab_offset
];
1293 /* skip first line entry for each function */
1294 rawptr
+= local_linesz
;
1295 /* line numbers start at one for the first line of the function */
1299 bfd_coff_swap_lineno_in (symfile_bfd
, rawptr
, &lptr
);
1300 rawptr
+= local_linesz
;
1301 /* The next function, or the sentinel, will have L_LNNO32 zero; we exit. */
1302 if (L_LNNO32 (&lptr
) && L_LNNO32 (&lptr
) <= last_line
)
1303 coff_record_line (first_line
+ L_LNNO32 (&lptr
),
1305 + ANOFFSET (section_offsets
, SECT_OFF_TEXT
));
1312 patch_type (type
, real_type
)
1314 struct type
*real_type
;
1316 register struct type
*target
= TYPE_TARGET_TYPE (type
);
1317 register struct type
*real_target
= TYPE_TARGET_TYPE (real_type
);
1318 int field_size
= TYPE_NFIELDS (real_target
) * sizeof (struct field
);
1320 TYPE_LENGTH (target
) = TYPE_LENGTH (real_target
);
1321 TYPE_NFIELDS (target
) = TYPE_NFIELDS (real_target
);
1322 TYPE_FIELDS (target
) = (struct field
*) TYPE_ALLOC (target
, field_size
);
1324 memcpy (TYPE_FIELDS (target
), TYPE_FIELDS (real_target
), field_size
);
1326 if (TYPE_NAME (real_target
))
1328 if (TYPE_NAME (target
))
1329 free (TYPE_NAME (target
));
1330 TYPE_NAME (target
) = concat (TYPE_NAME (real_target
), NULL
);
1334 /* Patch up all appropriate typedef symbols in the opaque_type_chains
1335 so that they can be used to print out opaque data structures properly. */
1338 patch_opaque_types (s
)
1341 register struct block
*b
;
1343 register struct symbol
*real_sym
;
1345 /* Go through the per-file symbols only */
1346 b
= BLOCKVECTOR_BLOCK (BLOCKVECTOR (s
), STATIC_BLOCK
);
1347 for (i
= BLOCK_NSYMS (b
) - 1; i
>= 0; i
--)
1349 /* Find completed typedefs to use to fix opaque ones.
1350 Remove syms from the chain when their types are stored,
1351 but search the whole chain, as there may be several syms
1352 from different files with the same name. */
1353 real_sym
= BLOCK_SYM (b
, i
);
1354 if (SYMBOL_CLASS (real_sym
) == LOC_TYPEDEF
&&
1355 SYMBOL_NAMESPACE (real_sym
) == VAR_NAMESPACE
&&
1356 TYPE_CODE (SYMBOL_TYPE (real_sym
)) == TYPE_CODE_PTR
&&
1357 TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (real_sym
))) != 0)
1359 register char *name
= SYMBOL_NAME (real_sym
);
1360 register int hash
= hashname (name
);
1361 register struct symbol
*sym
, *prev
;
1364 for (sym
= opaque_type_chain
[hash
]; sym
;)
1366 if (name
[0] == SYMBOL_NAME (sym
)[0] &&
1367 STREQ (name
+ 1, SYMBOL_NAME (sym
) + 1))
1371 SYMBOL_VALUE_CHAIN (prev
) = SYMBOL_VALUE_CHAIN (sym
);
1375 opaque_type_chain
[hash
] = SYMBOL_VALUE_CHAIN (sym
);
1378 patch_type (SYMBOL_TYPE (sym
), SYMBOL_TYPE (real_sym
));
1382 sym
= SYMBOL_VALUE_CHAIN (prev
);
1386 sym
= opaque_type_chain
[hash
];
1392 sym
= SYMBOL_VALUE_CHAIN (sym
);
1399 static struct symbol
*
1400 process_coff_symbol (cs
, aux
, section_offsets
, objfile
)
1401 register struct coff_symbol
*cs
;
1402 register union internal_auxent
*aux
;
1403 struct section_offsets
*section_offsets
;
1404 struct objfile
*objfile
;
1406 register struct symbol
*sym
1407 = (struct symbol
*) obstack_alloc (&objfile
->symbol_obstack
,
1408 sizeof (struct symbol
));
1411 memset (sym
, 0, sizeof (struct symbol
));
1413 name
= EXTERNAL_NAME (name
, objfile
->obfd
);
1414 SYMBOL_NAME (sym
) = obstack_copy0 (&objfile
->symbol_obstack
, name
,
1417 /* default assumptions */
1418 SYMBOL_VALUE (sym
) = cs
->c_value
;
1419 SYMBOL_NAMESPACE (sym
) = VAR_NAMESPACE
;
1420 SYMBOL_SECTION (sym
) = cs_to_section (cs
, objfile
);
1422 if (ISFCN (cs
->c_type
))
1424 SYMBOL_VALUE (sym
) += ANOFFSET (section_offsets
, SECT_OFF_TEXT
);
1426 /* FIXME: This has NOT been tested. The DBX version has.. */
1427 /* Generate a template for the type of this function. The
1428 types of the arguments will be added as we read the symbol
1430 struct type
*new = (struct type
*)
1431 obstack_alloc (&objfile
->symbol_obstack
, sizeof (struct type
));
1433 memcpy (new, lookup_function_type (decode_function_type (cs
, cs
->c_type
, aux
)),
1434 sizeof(struct type
));
1435 SYMBOL_TYPE (sym
) = new;
1436 in_function_type
= SYMBOL_TYPE(sym
);
1439 lookup_function_type (decode_function_type (cs
, cs
->c_type
, aux
));
1442 SYMBOL_CLASS (sym
) = LOC_BLOCK
;
1443 if (cs
->c_sclass
== C_STAT
)
1444 add_symbol_to_list (sym
, &file_symbols
);
1445 else if (cs
->c_sclass
== C_EXT
)
1446 add_symbol_to_list (sym
, &global_symbols
);
1450 SYMBOL_TYPE (sym
) = decode_type (cs
, cs
->c_type
, aux
);
1451 switch (cs
->c_sclass
)
1457 SYMBOL_CLASS (sym
) = LOC_LOCAL
;
1458 add_symbol_to_list (sym
, &local_symbols
);
1462 SYMBOL_CLASS (sym
) = LOC_STATIC
;
1463 SYMBOL_VALUE_ADDRESS (sym
) = (CORE_ADDR
) cs
->c_value
;
1464 SYMBOL_VALUE_ADDRESS (sym
) += ANOFFSET (section_offsets
, SECT_OFF_TEXT
);
1465 add_symbol_to_list (sym
, &global_symbols
);
1469 SYMBOL_CLASS (sym
) = LOC_STATIC
;
1470 SYMBOL_VALUE_ADDRESS (sym
) = (CORE_ADDR
) cs
->c_value
;
1471 SYMBOL_VALUE_ADDRESS (sym
) += ANOFFSET (section_offsets
, SECT_OFF_TEXT
);
1472 if (within_function
) {
1473 /* Static symbol of local scope */
1474 add_symbol_to_list (sym
, &local_symbols
);
1477 /* Static symbol at top level of file */
1478 add_symbol_to_list (sym
, &file_symbols
);
1482 #ifdef C_GLBLREG /* AMD coff */
1486 SYMBOL_CLASS (sym
) = LOC_REGISTER
;
1487 SYMBOL_VALUE (sym
) = SDB_REG_TO_REGNUM(cs
->c_value
);
1488 add_symbol_to_list (sym
, &local_symbols
);
1495 SYMBOL_CLASS (sym
) = LOC_ARG
;
1497 /* FIXME: This has not been tested. */
1498 /* Add parameter to function. */
1499 add_param_to_type(&in_function_type
,sym
);
1501 add_symbol_to_list (sym
, &local_symbols
);
1502 #if !defined (BELIEVE_PCC_PROMOTION)
1503 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
1505 /* If PCC says a parameter is a short or a char,
1506 aligned on an int boundary, realign it to the
1507 "little end" of the int. */
1508 struct type
*temptype
;
1509 temptype
= lookup_fundamental_type (current_objfile
,
1511 if (TYPE_LENGTH (SYMBOL_TYPE (sym
)) < TYPE_LENGTH (temptype
)
1512 && TYPE_CODE (SYMBOL_TYPE (sym
)) == TYPE_CODE_INT
1513 && 0 == SYMBOL_VALUE (sym
) % TYPE_LENGTH (temptype
))
1515 SYMBOL_VALUE (sym
) +=
1516 TYPE_LENGTH (temptype
)
1517 - TYPE_LENGTH (SYMBOL_TYPE (sym
));
1524 SYMBOL_CLASS (sym
) = LOC_REGPARM
;
1525 SYMBOL_VALUE (sym
) = SDB_REG_TO_REGNUM(cs
->c_value
);
1526 add_symbol_to_list (sym
, &local_symbols
);
1527 #if !defined (BELIEVE_PCC_PROMOTION)
1528 /* FIXME: This should retain the current type, since it's just
1529 a register value. gnu@adobe, 26Feb93 */
1531 /* If PCC says a parameter is a short or a char,
1532 it is really an int. */
1533 struct type
*temptype
;
1535 lookup_fundamental_type (current_objfile
, FT_INTEGER
);
1536 if (TYPE_LENGTH (SYMBOL_TYPE (sym
)) < TYPE_LENGTH (temptype
)
1537 && TYPE_CODE (SYMBOL_TYPE (sym
)) == TYPE_CODE_INT
)
1540 (TYPE_UNSIGNED (SYMBOL_TYPE (sym
))
1541 ? lookup_fundamental_type (current_objfile
,
1542 FT_UNSIGNED_INTEGER
)
1550 SYMBOL_CLASS (sym
) = LOC_TYPEDEF
;
1551 SYMBOL_NAMESPACE (sym
) = VAR_NAMESPACE
;
1553 /* If type has no name, give it one */
1554 if (TYPE_NAME (SYMBOL_TYPE (sym
)) == 0)
1556 if (TYPE_CODE (SYMBOL_TYPE (sym
)) == TYPE_CODE_PTR
1557 || TYPE_CODE (SYMBOL_TYPE (sym
)) == TYPE_CODE_FUNC
)
1559 /* If we are giving a name to a type such as "pointer to
1560 foo" or "function returning foo", we better not set
1561 the TYPE_NAME. If the program contains "typedef char
1562 *caddr_t;", we don't want all variables of type char
1563 * to print as caddr_t. This is not just a
1564 consequence of GDB's type management; CC and GCC (at
1565 least through version 2.4) both output variables of
1566 either type char * or caddr_t with the type
1567 refering to the C_TPDEF symbol for caddr_t. If a future
1568 compiler cleans this up it GDB is not ready for it
1569 yet, but if it becomes ready we somehow need to
1570 disable this check (without breaking the PCC/GCC2.4
1575 Fortunately, this check seems not to be necessary
1576 for anything except pointers or functions. */
1580 TYPE_NAME (SYMBOL_TYPE (sym
)) =
1581 concat (SYMBOL_NAME (sym
), NULL
);
1584 /* Ignore vendor section for Harris CX/UX targets. */
1585 else if (cs
->c_name
[0] == '$')
1587 #endif /* CXUX_TARGET */
1589 /* Keep track of any type which points to empty structured type,
1590 so it can be filled from a definition from another file. A
1591 simple forward reference (TYPE_CODE_UNDEF) is not an
1592 empty structured type, though; the forward references
1593 work themselves out via the magic of coff_lookup_type. */
1594 if (TYPE_CODE (SYMBOL_TYPE (sym
)) == TYPE_CODE_PTR
&&
1595 TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym
))) == 0 &&
1596 TYPE_CODE (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym
))) !=
1599 register int i
= hashname (SYMBOL_NAME (sym
));
1601 SYMBOL_VALUE_CHAIN (sym
) = opaque_type_chain
[i
];
1602 opaque_type_chain
[i
] = sym
;
1604 add_symbol_to_list (sym
, &file_symbols
);
1610 SYMBOL_CLASS (sym
) = LOC_TYPEDEF
;
1611 SYMBOL_NAMESPACE (sym
) = STRUCT_NAMESPACE
;
1613 /* Some compilers try to be helpful by inventing "fake"
1614 names for anonymous enums, structures, and unions, like
1615 "~0fake" or ".0fake". Thanks, but no thanks... */
1616 if (TYPE_TAG_NAME (SYMBOL_TYPE (sym
)) == 0)
1617 if (SYMBOL_NAME(sym
) != NULL
1618 && *SYMBOL_NAME(sym
) != '~'
1619 && *SYMBOL_NAME(sym
) != '.')
1620 TYPE_TAG_NAME (SYMBOL_TYPE (sym
)) =
1621 concat (SYMBOL_NAME (sym
), NULL
);
1623 add_symbol_to_list (sym
, &file_symbols
);
1633 /* Decode a coff type specifier; return the type that is meant. */
1635 static struct type
*
1636 decode_type (cs
, c_type
, aux
)
1637 register struct coff_symbol
*cs
;
1638 unsigned int c_type
;
1639 register union internal_auxent
*aux
;
1641 register struct type
*type
= 0;
1642 unsigned int new_c_type
;
1644 if (c_type
& ~N_BTMASK
)
1646 new_c_type
= DECREF (c_type
);
1649 type
= decode_type (cs
, new_c_type
, aux
);
1650 type
= lookup_pointer_type (type
);
1652 else if (ISFCN (c_type
))
1654 type
= decode_type (cs
, new_c_type
, aux
);
1655 type
= lookup_function_type (type
);
1657 else if (ISARY (c_type
))
1660 register unsigned short *dim
;
1661 struct type
*base_type
, *index_type
, *range_type
;
1663 /* Define an array type. */
1664 /* auxent refers to array, not base type */
1665 if (aux
->x_sym
.x_tagndx
.l
== 0)
1668 /* shift the indices down */
1669 dim
= &aux
->x_sym
.x_fcnary
.x_ary
.x_dimen
[0];
1672 for (i
= 0; *dim
&& i
< DIMNUM
- 1; i
++, dim
++)
1676 base_type
= decode_type (cs
, new_c_type
, aux
);
1677 index_type
= lookup_fundamental_type (current_objfile
, FT_INTEGER
);
1679 create_range_type ((struct type
*) NULL
, index_type
, 0, n
- 1);
1681 create_array_type ((struct type
*) NULL
, base_type
, range_type
);
1686 /* Reference to existing type. This only occurs with the
1687 struct, union, and enum types. EPI a29k coff
1688 fakes us out by producing aux entries with a nonzero
1689 x_tagndx for definitions of structs, unions, and enums, so we
1690 have to check the c_sclass field. SCO 3.2v4 cc gets confused
1691 with pointers to pointers to defined structs, and generates
1692 negative x_tagndx fields. */
1693 if (cs
->c_naux
> 0 && aux
->x_sym
.x_tagndx
.l
!= 0)
1695 if (cs
->c_sclass
!= C_STRTAG
1696 && cs
->c_sclass
!= C_UNTAG
1697 && cs
->c_sclass
!= C_ENTAG
1698 && aux
->x_sym
.x_tagndx
.l
>= 0)
1700 type
= coff_alloc_type (aux
->x_sym
.x_tagndx
.l
);
1705 complain (&tagndx_bad_complaint
, cs
->c_name
);
1706 /* And fall through to decode_base_type... */
1710 return decode_base_type (cs
, BTYPE (c_type
), aux
);
1713 /* Decode a coff type specifier for function definition;
1714 return the type that the function returns. */
1716 static struct type
*
1717 decode_function_type (cs
, c_type
, aux
)
1718 register struct coff_symbol
*cs
;
1719 unsigned int c_type
;
1720 register union internal_auxent
*aux
;
1722 if (aux
->x_sym
.x_tagndx
.l
== 0)
1723 cs
->c_naux
= 0; /* auxent refers to function, not base type */
1725 return decode_type (cs
, DECREF (c_type
), aux
);
1730 static struct type
*
1731 decode_base_type (cs
, c_type
, aux
)
1732 register struct coff_symbol
*cs
;
1733 unsigned int c_type
;
1734 register union internal_auxent
*aux
;
1741 /* shows up with "void (*foo)();" structure members */
1742 return lookup_fundamental_type (current_objfile
, FT_VOID
);
1745 /* DGUX actually defines both T_ARG and T_VOID to the same value. */
1748 /* Shows up in DGUX, I think. Not sure where. */
1749 return lookup_fundamental_type (current_objfile
, FT_VOID
); /* shouldn't show up here */
1755 /* Intel 960 COFF has this symbol and meaning. */
1756 return lookup_fundamental_type (current_objfile
, FT_VOID
);
1760 return lookup_fundamental_type (current_objfile
, FT_CHAR
);
1763 return lookup_fundamental_type (current_objfile
, FT_SHORT
);
1766 return lookup_fundamental_type (current_objfile
, FT_INTEGER
);
1769 return lookup_fundamental_type (current_objfile
, FT_LONG
);
1772 return lookup_fundamental_type (current_objfile
, FT_FLOAT
);
1775 return lookup_fundamental_type (current_objfile
, FT_DBL_PREC_FLOAT
);
1778 if (cs
->c_naux
!= 1)
1780 /* anonymous structure type */
1781 type
= coff_alloc_type (cs
->c_symnum
);
1782 TYPE_CODE (type
) = TYPE_CODE_STRUCT
;
1783 TYPE_NAME (type
) = NULL
;
1784 /* This used to set the tag to "<opaque>". But I think setting it
1785 to NULL is right, and the printing code can print it as
1787 TYPE_TAG_NAME (type
) = NULL
;
1788 INIT_CPLUS_SPECIFIC(type
);
1789 TYPE_LENGTH (type
) = 0;
1790 TYPE_FIELDS (type
) = 0;
1791 TYPE_NFIELDS (type
) = 0;
1795 type
= coff_read_struct_type (cs
->c_symnum
,
1796 aux
->x_sym
.x_misc
.x_lnsz
.x_size
,
1797 aux
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
);
1802 if (cs
->c_naux
!= 1)
1804 /* anonymous union type */
1805 type
= coff_alloc_type (cs
->c_symnum
);
1806 TYPE_NAME (type
) = NULL
;
1807 /* This used to set the tag to "<opaque>". But I think setting it
1808 to NULL is right, and the printing code can print it as
1810 TYPE_TAG_NAME (type
) = NULL
;
1811 INIT_CPLUS_SPECIFIC(type
);
1812 TYPE_LENGTH (type
) = 0;
1813 TYPE_FIELDS (type
) = 0;
1814 TYPE_NFIELDS (type
) = 0;
1818 type
= coff_read_struct_type (cs
->c_symnum
,
1819 aux
->x_sym
.x_misc
.x_lnsz
.x_size
,
1820 aux
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
);
1822 TYPE_CODE (type
) = TYPE_CODE_UNION
;
1826 if (cs
->c_naux
!= 1)
1828 /* anonymous enum type */
1829 type
= coff_alloc_type (cs
->c_symnum
);
1830 TYPE_CODE (type
) = TYPE_CODE_ENUM
;
1831 TYPE_NAME (type
) = NULL
;
1832 /* This used to set the tag to "<opaque>". But I think setting it
1833 to NULL is right, and the printing code can print it as
1835 TYPE_TAG_NAME (type
) = NULL
;
1836 TYPE_LENGTH (type
) = 0;
1837 TYPE_FIELDS (type
) = 0;
1838 TYPE_NFIELDS(type
) = 0;
1842 type
= coff_read_enum_type (cs
->c_symnum
,
1843 aux
->x_sym
.x_misc
.x_lnsz
.x_size
,
1844 aux
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
);
1849 /* shouldn't show up here */
1853 return lookup_fundamental_type (current_objfile
, FT_UNSIGNED_CHAR
);
1856 return lookup_fundamental_type (current_objfile
, FT_UNSIGNED_SHORT
);
1859 return lookup_fundamental_type (current_objfile
, FT_UNSIGNED_INTEGER
);
1862 return lookup_fundamental_type (current_objfile
, FT_UNSIGNED_LONG
);
1864 complain (&unexpected_type_complaint
, cs
->c_name
);
1865 return lookup_fundamental_type (current_objfile
, FT_VOID
);
1868 /* This page contains subroutines of read_type. */
1870 /* Read the description of a structure (or union type) and return an
1871 object describing the type. */
1873 static struct type
*
1874 coff_read_struct_type (index
, length
, lastsym
)
1881 struct nextfield
*next
;
1885 register struct type
*type
;
1886 register struct nextfield
*list
= 0;
1887 struct nextfield
*new;
1891 struct coff_symbol member_sym
;
1892 register struct coff_symbol
*ms
= &member_sym
;
1893 struct internal_syment sub_sym
;
1894 union internal_auxent sub_aux
;
1897 type
= coff_alloc_type (index
);
1898 TYPE_CODE (type
) = TYPE_CODE_STRUCT
;
1899 INIT_CPLUS_SPECIFIC(type
);
1900 TYPE_LENGTH (type
) = length
;
1902 while (!done
&& symnum
< lastsym
&& symnum
< nlist_nsyms_global
)
1904 read_one_sym (ms
, &sub_sym
, &sub_aux
);
1906 name
= EXTERNAL_NAME (name
, current_objfile
->obfd
);
1908 switch (ms
->c_sclass
)
1913 /* Get space to record the next field's data. */
1914 new = (struct nextfield
*) alloca (sizeof (struct nextfield
));
1918 /* Save the data. */
1922 ¤t_objfile
->symbol_obstack
);
1923 list
->field
.type
= decode_type (ms
, ms
->c_type
, &sub_aux
);
1924 list
->field
.bitpos
= 8 * ms
->c_value
;
1925 list
->field
.bitsize
= 0;
1931 /* Get space to record the next field's data. */
1932 new = (struct nextfield
*) alloca (sizeof (struct nextfield
));
1936 /* Save the data. */
1940 ¤t_objfile
->symbol_obstack
);
1941 list
->field
.type
= decode_type (ms
, ms
->c_type
, &sub_aux
);
1942 list
->field
.bitpos
= ms
->c_value
;
1943 list
->field
.bitsize
= sub_aux
.x_sym
.x_misc
.x_lnsz
.x_size
;
1952 /* Now create the vector of fields, and record how big it is. */
1954 TYPE_NFIELDS (type
) = nfields
;
1955 TYPE_FIELDS (type
) = (struct field
*)
1956 TYPE_ALLOC (type
, sizeof (struct field
) * nfields
);
1958 /* Copy the saved-up fields into the field vector. */
1960 for (n
= nfields
; list
; list
= list
->next
)
1961 TYPE_FIELD (type
, --n
) = list
->field
;
1966 /* Read a definition of an enumeration type,
1967 and create and return a suitable type object.
1968 Also defines the symbols that represent the values of the type. */
1971 static struct type
*
1972 coff_read_enum_type (index
, length
, lastsym
)
1977 register struct symbol
*sym
;
1978 register struct type
*type
;
1981 struct pending
**symlist
;
1982 struct coff_symbol member_sym
;
1983 register struct coff_symbol
*ms
= &member_sym
;
1984 struct internal_syment sub_sym
;
1985 union internal_auxent sub_aux
;
1986 struct pending
*osyms
, *syms
;
1991 type
= coff_alloc_type (index
);
1992 if (within_function
)
1993 symlist
= &local_symbols
;
1995 symlist
= &file_symbols
;
1997 o_nsyms
= osyms
? osyms
->nsyms
: 0;
1999 while (!done
&& symnum
< lastsym
&& symnum
< nlist_nsyms_global
)
2001 read_one_sym (ms
, &sub_sym
, &sub_aux
);
2003 name
= EXTERNAL_NAME (name
, current_objfile
->obfd
);
2005 switch (ms
->c_sclass
)
2008 sym
= (struct symbol
*) obstack_alloc
2009 (¤t_objfile
->symbol_obstack
,
2010 sizeof (struct symbol
));
2011 memset (sym
, 0, sizeof (struct symbol
));
2014 obsavestring (name
, strlen (name
),
2015 ¤t_objfile
->symbol_obstack
);
2016 SYMBOL_CLASS (sym
) = LOC_CONST
;
2017 SYMBOL_NAMESPACE (sym
) = VAR_NAMESPACE
;
2018 SYMBOL_VALUE (sym
) = ms
->c_value
;
2019 add_symbol_to_list (sym
, symlist
);
2024 /* Sometimes the linker (on 386/ix 2.0.2 at least) screws
2025 up the count of how many symbols to read. So stop
2032 /* Now fill in the fields of the type-structure. */
2035 TYPE_LENGTH (type
) = length
;
2037 TYPE_LENGTH (type
) = TARGET_INT_BIT
/ TARGET_CHAR_BIT
; /* Assume ints */
2038 TYPE_CODE (type
) = TYPE_CODE_ENUM
;
2039 TYPE_NFIELDS (type
) = nsyms
;
2040 TYPE_FIELDS (type
) = (struct field
*)
2041 TYPE_ALLOC (type
, sizeof (struct field
) * nsyms
);
2043 /* Find the symbols for the values and put them into the type.
2044 The symbols can be found in the symlist that we put them on
2045 to cause them to be defined. osyms contains the old value
2046 of that symlist; everything up to there was defined by us. */
2047 /* Note that we preserve the order of the enum constants, so
2048 that in something like "enum {FOO, LAST_THING=FOO}" we print
2049 FOO, not LAST_THING. */
2051 for (syms
= *symlist
, n
= 0; syms
; syms
= syms
->next
)
2057 for (; j
< syms
->nsyms
; j
++,n
++)
2059 struct symbol
*xsym
= syms
->symbol
[j
];
2060 SYMBOL_TYPE (xsym
) = type
;
2061 TYPE_FIELD_NAME (type
, n
) = SYMBOL_NAME (xsym
);
2062 TYPE_FIELD_VALUE (type
, n
) = 0;
2063 TYPE_FIELD_BITPOS (type
, n
) = SYMBOL_VALUE (xsym
);
2064 TYPE_FIELD_BITSIZE (type
, n
) = 0;
2073 struct section_offsets
*
2074 coff_symfile_offsets (objfile
, addr
)
2075 struct objfile
*objfile
;
2078 struct section_offsets
*section_offsets
;
2081 objfile
->num_sections
= SECT_OFF_MAX
;
2082 section_offsets
= (struct section_offsets
*)
2083 obstack_alloc (&objfile
-> psymbol_obstack
,
2084 sizeof (struct section_offsets
)
2085 + sizeof (section_offsets
->offsets
) * (SECT_OFF_MAX
-1));
2087 for (i
= 0; i
< SECT_OFF_MAX
; i
++)
2088 ANOFFSET (section_offsets
, i
) = addr
;
2090 return section_offsets
;
2093 /* Register our ability to parse symbols for coff BFD files. */
2095 static struct sym_fns coff_sym_fns
=
2097 bfd_target_coff_flavour
,
2098 coff_new_init
, /* sym_new_init: init anything gbl to entire symtab */
2099 coff_symfile_init
, /* sym_init: read initial info, setup for sym_read() */
2100 coff_symfile_read
, /* sym_read: read a symbol file into symtab */
2101 coff_symfile_finish
, /* sym_finish: finished with file, cleanup */
2102 coff_symfile_offsets
, /* sym_offsets: xlate external to internal form */
2103 NULL
/* next: pointer to next struct sym_fns */
2107 _initialize_coffread ()
2109 add_symtab_fns (&coff_sym_fns
);