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 /* Look up a coff type-number index. Return the address of the slot
288 where the type for that index is stored.
289 The type-number is in INDEX.
291 This can be used for finding the type associated with that index
292 or for associating a new type with the index. */
294 static struct type
**
295 coff_lookup_type (index
)
298 if (index
>= type_vector_length
)
300 int old_vector_length
= type_vector_length
;
302 type_vector_length
*= 2;
303 if (index
/* is still */ >= type_vector_length
)
304 type_vector_length
= index
* 2;
306 type_vector
= (struct type
**)
307 xrealloc ((char *) type_vector
,
308 type_vector_length
* sizeof (struct type
*));
309 memset (&type_vector
[old_vector_length
], 0,
310 (type_vector_length
- old_vector_length
) * sizeof(struct type
*));
312 return &type_vector
[index
];
315 /* Make sure there is a type allocated for type number index
316 and return the type object.
317 This can create an empty (zeroed) type object. */
320 coff_alloc_type (index
)
323 register struct type
**type_addr
= coff_lookup_type (index
);
324 register struct type
*type
= *type_addr
;
326 /* If we are referring to a type not known at all yet,
327 allocate an empty type for it.
328 We will fill it in later if we find out how. */
331 type
= alloc_type (current_objfile
);
337 /* Record a line number entry for line LINE at address PC.
338 FIXME: Use record_line instead. */
341 coff_record_line (line
, pc
)
345 struct linetable_entry
*e
;
346 /* Make sure line vector is big enough. */
348 if (line_vector_index
+ 2 >= line_vector_length
)
350 line_vector_length
*= 2;
351 line_vector
= (struct linetable
*)
352 xrealloc ((char *) line_vector
, sizeof (struct linetable
)
353 + (line_vector_length
354 * sizeof (struct linetable_entry
)));
357 e
= line_vector
->item
+ line_vector_index
++;
358 e
->line
= line
; e
->pc
= pc
;
361 /* Start a new symtab for a new source file.
362 This is called when a COFF ".file" symbol is seen;
363 it indicates the start of data for one original source file. */
369 /* We fill in the filename later. start_symtab puts
370 this pointer into last_source_file and we put it in
371 subfiles->name, which end_symtab frees; that's why
372 it must be malloc'd. */
374 /* We never know the directory name for COFF. */
376 /* The start address is irrelevant, since we set
377 last_source_start_addr in coff_end_symtab. */
380 /* Initialize the source file line number information for this file. */
382 if (line_vector
) /* Unlikely, but maybe possible? */
383 free ((PTR
)line_vector
);
384 line_vector_index
= 0;
385 line_vector_length
= 1000;
386 prev_line_number
= -2; /* Force first line number to be explicit */
387 line_vector
= (struct linetable
*)
388 xmalloc (sizeof (struct linetable
)
389 + line_vector_length
* sizeof (struct linetable_entry
));
392 /* Save the vital information from when starting to read a file,
393 for use when closing off the current file.
394 NAME is the file name the symbols came from, START_ADDR is the first
395 text address for the file, and SIZE is the number of bytes of text. */
398 complete_symtab (name
, start_addr
, size
)
400 CORE_ADDR start_addr
;
403 if (last_source_file
!= NULL
)
404 free (last_source_file
);
405 last_source_file
= savestring (name
, strlen (name
));
406 current_source_start_addr
= start_addr
;
407 current_source_end_addr
= start_addr
+ size
;
409 if (current_objfile
-> ei
.entry_point
>= current_source_start_addr
&&
410 current_objfile
-> ei
.entry_point
< current_source_end_addr
)
412 current_objfile
-> ei
.entry_file_lowpc
= current_source_start_addr
;
413 current_objfile
-> ei
.entry_file_highpc
= current_source_end_addr
;
417 /* Finish the symbol definitions for one main source file,
418 close off all the lexical contexts for that file
419 (creating struct block's for them), then make the
420 struct symtab for that file and put it in the list of all such. */
423 coff_end_symtab (objfile
)
424 struct objfile
*objfile
;
426 struct symtab
*symtab
;
428 last_source_start_addr
= current_source_start_addr
;
430 /* For no good reason, this file stores the number of entries in a
431 separate variable instead of in line_vector->nitems. Fix it. */
433 line_vector
->nitems
= line_vector_index
;
435 /* For COFF, we only have one subfile, so we can just look at
436 subfiles and not worry about there being other elements in the
437 chain. We fill in various fields now because we didn't know them
438 before (or because doing it now is simply an artifact of how this
439 file used to be written). */
440 subfiles
->line_vector
= line_vector
;
441 subfiles
->name
= last_source_file
;
443 /* sort_pending is needed for amdcoff, at least.
444 sort_linevec is needed for the SCO compiler. */
445 symtab
= end_symtab (current_source_end_addr
, 1, 1, objfile
, 0);
448 free_named_symtabs (symtab
->filename
);
450 /* Reinitialize for beginning of new file. */
452 line_vector_length
= -1;
453 last_source_file
= NULL
;
457 record_minimal_symbol (name
, address
, type
, objfile
)
460 enum minimal_symbol_type type
;
461 struct objfile
*objfile
;
463 /* We don't want TDESC entry points in the minimal symbol table */
464 if (name
[0] == '@') return;
466 prim_record_minimal_symbol
467 (obsavestring (name
, strlen (name
), &objfile
->symbol_obstack
),
472 /* coff_symfile_init ()
473 is the coff-specific initialization routine for reading symbols.
474 It is passed a struct objfile which contains, among other things,
475 the BFD for the file whose symbols are being read, and a slot for
476 a pointer to "private data" which we fill with cookies and other
477 treats for coff_symfile_read ().
479 We will only be called if this is a COFF or COFF-like file.
480 BFD handles figuring out the format of the file, and code in symtab.c
481 uses BFD's determination to vector to us.
483 The ultimate result is a new symtab (or, FIXME, eventually a psymtab). */
485 static int text_bfd_scnum
;
488 coff_symfile_init (objfile
)
489 struct objfile
*objfile
;
492 bfd
*abfd
= objfile
->obfd
;
494 /* Allocate struct to keep track of stab reading. */
495 objfile
->sym_stab_info
= (PTR
)
496 xmmalloc (objfile
-> md
, sizeof (struct dbx_symfile_info
));
498 memset ((PTR
) objfile
->sym_stab_info
, 0, sizeof (struct dbx_symfile_info
));
500 /* Allocate struct to keep track of the symfile */
501 objfile
-> sym_private
= xmmalloc (objfile
-> md
,
502 sizeof (struct coff_symfile_info
));
504 memset (objfile
->sym_private
, 0, sizeof (struct coff_symfile_info
));
506 init_entry_point_info (objfile
);
508 /* Save the section number for the text section */
509 section
= bfd_get_section_by_name (abfd
, ".text");
511 text_bfd_scnum
= section
->index
;
516 /* This function is called for every section; it finds the outer limits
517 of the line table (minimum and maximum file offset) so that the
518 mainline code can read the whole thing for efficiency. */
522 find_linenos (abfd
, asect
, vpinfo
)
527 struct coff_symfile_info
*info
;
529 file_ptr offset
, maxoff
;
531 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
532 count
= asect
->lineno_count
;
537 size
= count
* local_linesz
;
539 info
= (struct coff_symfile_info
*)vpinfo
;
540 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
541 offset
= asect
->line_filepos
;
544 if (offset
< info
->min_lineno_offset
|| info
->min_lineno_offset
== 0)
545 info
->min_lineno_offset
= offset
;
547 maxoff
= offset
+ size
;
548 if (maxoff
> info
->max_lineno_offset
)
549 info
->max_lineno_offset
= maxoff
;
553 /* The BFD for this file -- only good while we're actively reading
554 symbols into a psymtab or a symtab. */
556 static bfd
*symfile_bfd
;
558 /* Read a symbol file, after initialization by coff_symfile_init. */
562 coff_symfile_read (objfile
, section_offsets
, mainline
)
563 struct objfile
*objfile
;
564 struct section_offsets
*section_offsets
;
567 struct coff_symfile_info
*info
;
568 struct dbx_symfile_info
*dbxinfo
;
569 bfd
*abfd
= objfile
->obfd
;
570 coff_data_type
*cdata
= coff_data (abfd
);
571 char *name
= bfd_get_filename (abfd
);
575 int stringtab_offset
;
576 struct cleanup
*back_to
;
577 int stabsize
, stabstrsize
;
579 info
= (struct coff_symfile_info
*) objfile
-> sym_private
;
580 dbxinfo
= (struct dbx_symfile_info
*) objfile
->sym_stab_info
;
581 symfile_bfd
= abfd
; /* Kludge for swap routines */
583 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
584 num_symbols
= bfd_get_symcount (abfd
); /* How many syms */
585 symtab_offset
= cdata
->sym_filepos
; /* Symbol table file offset */
586 stringtab_offset
= symtab_offset
+ /* String table file offset */
587 num_symbols
* cdata
->local_symesz
;
589 /* Set a few file-statics that give us specific information about
590 the particular COFF file format we're reading. */
591 local_linesz
= cdata
->local_linesz
;
592 local_n_btmask
= cdata
->local_n_btmask
;
593 local_n_btshft
= cdata
->local_n_btshft
;
594 local_n_tmask
= cdata
->local_n_tmask
;
595 local_n_tshift
= cdata
->local_n_tshift
;
596 local_linesz
= cdata
->local_linesz
;
597 local_symesz
= cdata
->local_symesz
;
598 local_auxesz
= cdata
->local_auxesz
;
600 /* Allocate space for raw symbol and aux entries, based on their
601 space requirements as reported by BFD. */
602 temp_sym
= (char *) xmalloc
603 (cdata
->local_symesz
+ cdata
->local_auxesz
);
604 temp_aux
= temp_sym
+ cdata
->local_symesz
;
605 back_to
= make_cleanup (free_current_contents
, &temp_sym
);
608 /* Read the line number table, all at once. */
609 info
->min_lineno_offset
= 0;
610 info
->max_lineno_offset
= 0;
611 bfd_map_over_sections (abfd
, find_linenos
, (PTR
) info
);
613 make_cleanup (free_linetab
, 0);
614 val
= init_lineno (abfd
, info
->min_lineno_offset
,
615 info
->max_lineno_offset
- info
->min_lineno_offset
);
617 error ("\"%s\": error reading line numbers\n", name
);
619 /* Now read the string table, all at once. */
621 make_cleanup (free_stringtab
, 0);
622 val
= init_stringtab (abfd
, stringtab_offset
);
624 error ("\"%s\": can't get string table", name
);
626 init_minimal_symbol_collection ();
627 make_cleanup (discard_minimal_symbols
, 0);
629 /* Now that the executable file is positioned at symbol table,
630 process it and define symbols accordingly. */
632 coff_symtab_read ((long) symtab_offset
, num_symbols
, section_offsets
,
635 /* Sort symbols alphabetically within each block. */
640 for (s
= objfile
-> symtabs
; s
!= NULL
; s
= s
-> next
)
641 sort_symtab_syms (s
);
644 /* Install any minimal symbols that have been collected as the current
645 minimal symbols for this objfile. */
647 install_minimal_symbols (objfile
);
649 bfd_map_over_sections (abfd
, coff_locate_sections
, (PTR
) info
);
653 /* FIXME: dubious. Why can't we use something normal like
654 bfd_get_section_contents? */
655 bfd_seek (abfd
, abfd
->where
, 0);
657 stabsize
= bfd_section_size (abfd
, info
->stabsect
);
658 stabstrsize
= bfd_section_size (abfd
, info
->stabstrsect
);
660 coffstab_build_psymtabs (objfile
,
663 info
->stabsect
->filepos
, stabsize
,
664 info
->stabstrsect
->filepos
, stabstrsize
);
667 do_cleanups (back_to
);
671 coff_new_init (ignore
)
672 struct objfile
*ignore
;
676 /* Perform any local cleanups required when we are done with a particular
677 objfile. I.E, we are in the process of discarding all symbol information
678 for an objfile, freeing up all memory held for it, and unlinking the
679 objfile struct from the global list of known objfiles. */
682 coff_symfile_finish (objfile
)
683 struct objfile
*objfile
;
685 if (objfile
-> sym_private
!= NULL
)
687 mfree (objfile
-> md
, objfile
-> sym_private
);
692 /* Given pointers to a symbol table in coff style exec file,
693 analyze them and create struct symtab's describing the symbols.
694 NSYMS is the number of symbols in the symbol table.
695 We read them one at a time using read_one_sym (). */
698 coff_symtab_read (symtab_offset
, nsyms
, section_offsets
, objfile
)
701 struct section_offsets
*section_offsets
;
702 struct objfile
*objfile
;
704 register struct context_stack
*new;
705 struct coff_symbol coff_symbol
;
706 register struct coff_symbol
*cs
= &coff_symbol
;
707 static struct internal_syment main_sym
;
708 static union internal_auxent main_aux
;
709 struct coff_symbol fcn_cs_saved
;
710 static struct internal_syment fcn_sym_saved
;
711 static union internal_auxent fcn_aux_saved
;
713 /* A .file is open. */
714 int in_source_file
= 0;
715 int next_file_symnum
= -1;
716 /* Name of the current file. */
717 char *filestring
= "";
719 int fcn_first_line
= 0;
720 int fcn_last_line
= 0;
721 int fcn_start_addr
= 0;
722 long fcn_line_ptr
= 0;
726 /* Work around a stdio bug in SunOS4.1.1 (this makes me nervous....
727 it's hard to know I've really worked around it. The fix should be
728 harmless, anyway). The symptom of the bug is that the first
729 fread (in read_one_sym), will (in my example) actually get data
730 from file offset 268, when the fseek was to 264 (and ftell shows
731 264). This causes all hell to break loose. I was unable to
732 reproduce this on a short test program which operated on the same
733 file, performing (I think) the same sequence of operations.
735 It stopped happening when I put in this (former) rewind().
737 FIXME: Find out if this has been reported to Sun, whether it has
738 been fixed in a later release, etc. */
740 bfd_seek (objfile
->obfd
, 0, 0);
742 /* Position to read the symbol table. */
743 val
= bfd_seek (objfile
->obfd
, (long) symtab_offset
, 0);
745 perror_with_name (objfile
->name
);
747 current_objfile
= objfile
;
748 nlist_bfd_global
= objfile
->obfd
;
749 nlist_nsyms_global
= nsyms
;
750 last_source_file
= NULL
;
751 memset (opaque_type_chain
, 0, sizeof opaque_type_chain
);
753 if (type_vector
) /* Get rid of previous one */
754 free ((PTR
) type_vector
);
755 type_vector_length
= 160;
756 type_vector
= (struct type
**)
757 xmalloc (type_vector_length
* sizeof (struct type
*));
758 memset (type_vector
, 0, type_vector_length
* sizeof (struct type
*));
760 coff_start_symtab ();
763 while (symnum
< nsyms
)
765 QUIT
; /* Make this command interruptable. */
767 read_one_sym (cs
, &main_sym
, &main_aux
);
770 temp_sem_val
= cs
->c_name
[0] << 24 | cs
->c_name
[1] << 16 |
771 cs
->c_name
[2] << 8 | cs
->c_name
[3];
772 if (int_sem_val
== temp_sem_val
)
773 last_coffsem
= (int) strtol (cs
->c_name
+4, (char **) NULL
, 10);
776 if (cs
->c_symnum
== next_file_symnum
&& cs
->c_sclass
!= C_FILE
)
778 if (last_source_file
)
779 coff_end_symtab (objfile
);
781 coff_start_symtab ();
782 complete_symtab ("_globals_", 0, 0);
783 /* done with all files, everything from here on out is globals */
786 /* Special case for file with type declarations only, no text. */
787 if (!last_source_file
&& SDB_TYPE (cs
->c_type
)
788 && cs
->c_secnum
== N_DEBUG
)
789 complete_symtab (filestring
, 0, 0);
791 /* Typedefs should not be treated as symbol definitions. */
792 if (ISFCN (cs
->c_type
) && cs
->c_sclass
!= C_TPDEF
)
794 /* Record all functions -- external and static -- in minsyms. */
795 tmpaddr
= cs
->c_value
+ ANOFFSET (section_offsets
, SECT_OFF_TEXT
);
796 record_minimal_symbol (cs
->c_name
, tmpaddr
, mst_text
, objfile
);
798 fcn_line_ptr
= main_aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
;
799 fcn_start_addr
= tmpaddr
;
801 fcn_sym_saved
= main_sym
;
802 fcn_aux_saved
= main_aux
;
806 switch (cs
->c_sclass
)
815 complain (&bad_sclass_complaint
, cs
->c_name
);
819 /* c_value field contains symnum of next .file entry in table
820 or symnum of first global after last .file. */
821 next_file_symnum
= cs
->c_value
;
823 filestring
= coff_getfilename (&main_aux
);
827 /* Complete symbol table for last object file
828 containing debugging information. */
829 if (last_source_file
)
831 coff_end_symtab (objfile
);
832 coff_start_symtab ();
838 if (cs
->c_name
[0] == '.')
840 if (STREQ (cs
->c_name
, ".text")) {
841 /* FIXME: don't wire in ".text" as section name
843 /* Check for in_source_file deals with case of
844 a file with debugging symbols
845 followed by a later file with no symbols. */
847 complete_symtab (filestring
,
848 cs
->c_value
+ ANOFFSET (section_offsets
, SECT_OFF_TEXT
),
849 main_aux
.x_scn
.x_scnlen
);
852 /* flush rest of '.' symbols */
855 else if (!SDB_TYPE (cs
->c_type
)
856 && cs
->c_name
[0] == 'L'
857 && (strncmp (cs
->c_name
, "LI%", 3) == 0
858 || strncmp (cs
->c_name
, "LF%", 3) == 0
859 || strncmp (cs
->c_name
,"LC%",3) == 0
860 || strncmp (cs
->c_name
,"LP%",3) == 0
861 || strncmp (cs
->c_name
,"LPB%",4) == 0
862 || strncmp (cs
->c_name
,"LBB%",4) == 0
863 || strncmp (cs
->c_name
,"LBE%",4) == 0
864 || strncmp (cs
->c_name
,"LPBX%",5) == 0))
865 /* At least on a 3b1, gcc generates swbeg and string labels
866 that look like this. Ignore them. */
868 /* fall in for static symbols that don't start with '.' */
870 /* Record it in the minimal symbols regardless of SDB_TYPE.
871 This parallels what we do for other debug formats, and
872 probably is needed to make print_address_symbolic work right
873 without the (now gone) "set fast-symbolic-addr off" kludge. */
875 /* FIXME: This bogusly assumes the sections are in a certain
876 order, text (SEC_CODE) sections are before data sections,
878 if (cs
->c_secnum
<= text_bfd_scnum
+1)
880 /* text or absolute. (FIXME, should use mst_abs if
882 tmpaddr
= cs
->c_value
;
883 if (cs
->c_sclass
!= C_STAT
)
884 tmpaddr
+= ANOFFSET (section_offsets
, SECT_OFF_TEXT
);
885 record_minimal_symbol
886 (cs
->c_name
, tmpaddr
,
887 cs
->c_sclass
== C_STAT
? mst_file_text
: mst_text
,
892 tmpaddr
= cs
->c_value
;
893 if (cs
->c_sclass
!= C_STAT
)
894 tmpaddr
+= ANOFFSET (section_offsets
, SECT_OFF_DATA
);
895 record_minimal_symbol
896 (cs
->c_name
, tmpaddr
,
897 cs
->c_sclass
== C_STAT
? mst_file_data
: mst_data
,
900 if (SDB_TYPE (cs
->c_type
))
901 process_coff_symbol (cs
, &main_aux
, section_offsets
, objfile
);
905 if (STREQ (cs
->c_name
, ".bf"))
909 /* value contains address of first non-init type code */
910 /* main_aux.x_sym.x_misc.x_lnsz.x_lnno
911 contains line number of '{' } */
913 complain (&bf_no_aux_complaint
, cs
->c_symnum
);
914 fcn_first_line
= main_aux
.x_sym
.x_misc
.x_lnsz
.x_lnno
;
916 /* Might want to check that locals are 0 and
917 context_stack_depth is zero, and complain if not. */
920 new = push_context (depth
, fcn_start_addr
);
921 fcn_cs_saved
.c_name
= getsymname (&fcn_sym_saved
);
923 process_coff_symbol (&fcn_cs_saved
, &fcn_aux_saved
,
924 section_offsets
, objfile
);
926 else if (STREQ (cs
->c_name
, ".ef"))
928 /* the value of .ef is the address of epilogue code;
929 not useful for gdb. */
930 /* { main_aux.x_sym.x_misc.x_lnsz.x_lnno
931 contains number of lines to '}' */
932 new = pop_context ();
933 /* Stack must be empty now. */
934 if (context_stack_depth
> 0 || new == NULL
)
936 complain (&ef_complaint
, cs
->c_symnum
);
942 complain (&ef_no_aux_complaint
, cs
->c_symnum
);
943 fcn_last_line
= 0x7FFFFFFF;
947 fcn_last_line
= main_aux
.x_sym
.x_misc
.x_lnsz
.x_lnno
;
949 enter_linenos (fcn_line_ptr
, fcn_first_line
, fcn_last_line
,
952 finish_block (new->name
, &local_symbols
, new->old_blocks
,
954 #if defined (FUNCTION_EPILOGUE_SIZE)
955 /* This macro should be defined only on
957 fcn_aux_saved.x_sym.x_misc.x_fsize
958 field is always zero.
959 So use the .bf record information that
960 points to the epilogue and add the size
963 + FUNCTION_EPILOGUE_SIZE
964 + ANOFFSET (section_offsets
, SECT_OFF_TEXT
),
967 + fcn_aux_saved
.x_sym
.x_misc
.x_fsize
968 + ANOFFSET (section_offsets
, SECT_OFF_TEXT
),
977 if (STREQ (cs
->c_name
, ".bb"))
979 tmpaddr
= cs
->c_value
;
980 tmpaddr
+= ANOFFSET (section_offsets
, SECT_OFF_TEXT
);
981 push_context (++depth
, tmpaddr
);
983 else if (STREQ (cs
->c_name
, ".eb"))
985 new = pop_context ();
986 if (depth
-- != new->depth
)
988 complain (&eb_complaint
, symnum
);
991 if (local_symbols
&& context_stack_depth
> 0)
994 cs
->c_value
+ ANOFFSET (section_offsets
, SECT_OFF_TEXT
);
995 /* Make a block for the local symbols within. */
996 finish_block (0, &local_symbols
, new->old_blocks
,
997 new->start_addr
, tmpaddr
, objfile
);
999 /* Now pop locals of block just finished. */
1000 local_symbols
= new->locals
;
1005 process_coff_symbol (cs
, &main_aux
, section_offsets
, objfile
);
1010 if (last_source_file
)
1011 coff_end_symtab (objfile
);
1013 /* Patch up any opaque types (references to types that are not defined
1014 in the file where they are referenced, e.g. "struct foo *bar"). */
1015 ALL_OBJFILE_SYMTABS (objfile
, s
)
1016 patch_opaque_types (s
);
1018 current_objfile
= NULL
;
1021 /* Routines for reading headers and symbols from executable. */
1023 /* Read the next symbol, swap it, and return it in both internal_syment
1024 form, and coff_symbol form. Also return its first auxent, if any,
1025 in internal_auxent form, and skip any other auxents. */
1028 read_one_sym (cs
, sym
, aux
)
1029 register struct coff_symbol
*cs
;
1030 register struct internal_syment
*sym
;
1031 register union internal_auxent
*aux
;
1035 cs
->c_symnum
= symnum
;
1036 bfd_read (temp_sym
, local_symesz
, 1, nlist_bfd_global
);
1037 bfd_coff_swap_sym_in (symfile_bfd
, temp_sym
, (char *)sym
);
1038 cs
->c_naux
= sym
->n_numaux
& 0xff;
1039 if (cs
->c_naux
>= 1)
1041 bfd_read (temp_aux
, local_auxesz
, 1, nlist_bfd_global
);
1042 bfd_coff_swap_aux_in (symfile_bfd
, temp_aux
, sym
->n_type
, sym
->n_sclass
,
1043 0, cs
->c_naux
, (char *)aux
);
1044 /* If more than one aux entry, read past it (only the first aux
1046 for (i
= 1; i
< cs
->c_naux
; i
++)
1047 bfd_read (temp_aux
, local_auxesz
, 1, nlist_bfd_global
);
1049 cs
->c_name
= getsymname (sym
);
1050 cs
->c_value
= sym
->n_value
;
1051 cs
->c_sclass
= (sym
->n_sclass
& 0xff);
1052 cs
->c_secnum
= sym
->n_scnum
;
1053 cs
->c_type
= (unsigned) sym
->n_type
;
1054 if (!SDB_TYPE (cs
->c_type
))
1057 symnum
+= 1 + cs
->c_naux
;
1060 /* Support for string table handling */
1062 static char *stringtab
= NULL
;
1065 init_stringtab (abfd
, offset
)
1071 unsigned char lengthbuf
[4];
1075 /* If the file is stripped, the offset might be zero, indicating no
1076 string table. Just return with `stringtab' set to null. */
1080 if (bfd_seek (abfd
, offset
, 0) < 0)
1083 val
= bfd_read ((char *)lengthbuf
, sizeof lengthbuf
, 1, abfd
);
1084 length
= bfd_h_get_32 (symfile_bfd
, lengthbuf
);
1086 /* If no string table is needed, then the file may end immediately
1087 after the symbols. Just return with `stringtab' set to null. */
1088 if (val
!= sizeof lengthbuf
|| length
< sizeof lengthbuf
)
1091 stringtab
= (char *) xmalloc (length
);
1092 /* This is in target format (probably not very useful, and not currently
1093 used), not host format. */
1094 memcpy (stringtab
, lengthbuf
, sizeof lengthbuf
);
1095 if (length
== sizeof length
) /* Empty table -- just the count */
1098 val
= bfd_read (stringtab
+ sizeof lengthbuf
, length
- sizeof lengthbuf
, 1, abfd
);
1099 if (val
!= length
- sizeof lengthbuf
|| stringtab
[length
- 1] != '\0')
1114 getsymname (symbol_entry
)
1115 struct internal_syment
*symbol_entry
;
1117 static char buffer
[SYMNMLEN
+1];
1120 if (symbol_entry
->_n
._n_n
._n_zeroes
== 0)
1122 /* FIXME: Probably should be detecting corrupt symbol files by
1123 seeing whether offset points to within the stringtab. */
1124 result
= stringtab
+ symbol_entry
->_n
._n_n
._n_offset
;
1128 strncpy (buffer
, symbol_entry
->_n
._n_name
, SYMNMLEN
);
1129 buffer
[SYMNMLEN
] = '\0';
1135 /* Extract the file name from the aux entry of a C_FILE symbol. Return
1136 only the last component of the name. Result is in static storage and
1137 is only good for temporary use. */
1140 coff_getfilename (aux_entry
)
1141 union internal_auxent
*aux_entry
;
1143 static char buffer
[BUFSIZ
];
1144 register char *temp
;
1147 if (aux_entry
->x_file
.x_n
.x_zeroes
== 0)
1148 strcpy (buffer
, stringtab
+ aux_entry
->x_file
.x_n
.x_offset
);
1151 strncpy (buffer
, aux_entry
->x_file
.x_fname
, FILNMLEN
);
1152 buffer
[FILNMLEN
] = '\0';
1156 /* FIXME: We should not be throwing away the information about what
1157 directory. It should go into dirname of the symtab, or some such
1159 if ((temp
= strrchr (result
, '/')) != NULL
)
1164 /* Support for line number handling. */
1166 static char *linetab
= NULL
;
1167 static long linetab_offset
;
1168 static unsigned long linetab_size
;
1170 /* Read in all the line numbers for fast lookups later. Leave them in
1171 external (unswapped) format in memory; we'll swap them as we enter
1172 them into GDB's data structures. */
1175 init_lineno (abfd
, offset
, size
)
1182 linetab_offset
= offset
;
1183 linetab_size
= size
;
1190 if (bfd_seek (abfd
, offset
, 0) < 0)
1193 /* Allocate the desired table, plus a sentinel */
1194 linetab
= (char *) xmalloc (size
+ local_linesz
);
1196 val
= bfd_read (linetab
, size
, 1, abfd
);
1200 /* Terminate it with an all-zero sentinel record */
1201 memset (linetab
+ size
, 0, local_linesz
);
1214 #if !defined (L_LNNO32)
1215 #define L_LNNO32(lp) ((lp)->l_lnno)
1219 enter_linenos (file_offset
, first_line
, last_line
, section_offsets
)
1221 register int first_line
;
1222 register int last_line
;
1223 struct section_offsets
*section_offsets
;
1225 register char *rawptr
;
1226 struct internal_lineno lptr
;
1228 if (file_offset
< linetab_offset
)
1230 complain (&lineno_complaint
, file_offset
);
1231 if (file_offset
> linetab_size
) /* Too big to be an offset? */
1233 file_offset
+= linetab_offset
; /* Try reading at that linetab offset */
1236 rawptr
= &linetab
[file_offset
- linetab_offset
];
1238 /* skip first line entry for each function */
1239 rawptr
+= local_linesz
;
1240 /* line numbers start at one for the first line of the function */
1244 bfd_coff_swap_lineno_in (symfile_bfd
, rawptr
, &lptr
);
1245 rawptr
+= local_linesz
;
1246 /* The next function, or the sentinel, will have L_LNNO32 zero; we exit. */
1247 if (L_LNNO32 (&lptr
) && L_LNNO32 (&lptr
) <= last_line
)
1248 coff_record_line (first_line
+ L_LNNO32 (&lptr
),
1250 + ANOFFSET (section_offsets
, SECT_OFF_TEXT
));
1257 patch_type (type
, real_type
)
1259 struct type
*real_type
;
1261 register struct type
*target
= TYPE_TARGET_TYPE (type
);
1262 register struct type
*real_target
= TYPE_TARGET_TYPE (real_type
);
1263 int field_size
= TYPE_NFIELDS (real_target
) * sizeof (struct field
);
1265 TYPE_LENGTH (target
) = TYPE_LENGTH (real_target
);
1266 TYPE_NFIELDS (target
) = TYPE_NFIELDS (real_target
);
1267 TYPE_FIELDS (target
) = (struct field
*) TYPE_ALLOC (target
, field_size
);
1269 memcpy (TYPE_FIELDS (target
), TYPE_FIELDS (real_target
), field_size
);
1271 if (TYPE_NAME (real_target
))
1273 if (TYPE_NAME (target
))
1274 free (TYPE_NAME (target
));
1275 TYPE_NAME (target
) = concat (TYPE_NAME (real_target
), NULL
);
1279 /* Patch up all appropriate typedef symbols in the opaque_type_chains
1280 so that they can be used to print out opaque data structures properly. */
1283 patch_opaque_types (s
)
1286 register struct block
*b
;
1288 register struct symbol
*real_sym
;
1290 /* Go through the per-file symbols only */
1291 b
= BLOCKVECTOR_BLOCK (BLOCKVECTOR (s
), STATIC_BLOCK
);
1292 for (i
= BLOCK_NSYMS (b
) - 1; i
>= 0; i
--)
1294 /* Find completed typedefs to use to fix opaque ones.
1295 Remove syms from the chain when their types are stored,
1296 but search the whole chain, as there may be several syms
1297 from different files with the same name. */
1298 real_sym
= BLOCK_SYM (b
, i
);
1299 if (SYMBOL_CLASS (real_sym
) == LOC_TYPEDEF
&&
1300 SYMBOL_NAMESPACE (real_sym
) == VAR_NAMESPACE
&&
1301 TYPE_CODE (SYMBOL_TYPE (real_sym
)) == TYPE_CODE_PTR
&&
1302 TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (real_sym
))) != 0)
1304 register char *name
= SYMBOL_NAME (real_sym
);
1305 register int hash
= hashname (name
);
1306 register struct symbol
*sym
, *prev
;
1309 for (sym
= opaque_type_chain
[hash
]; sym
;)
1311 if (name
[0] == SYMBOL_NAME (sym
)[0] &&
1312 STREQ (name
+ 1, SYMBOL_NAME (sym
) + 1))
1316 SYMBOL_VALUE_CHAIN (prev
) = SYMBOL_VALUE_CHAIN (sym
);
1320 opaque_type_chain
[hash
] = SYMBOL_VALUE_CHAIN (sym
);
1323 patch_type (SYMBOL_TYPE (sym
), SYMBOL_TYPE (real_sym
));
1327 sym
= SYMBOL_VALUE_CHAIN (prev
);
1331 sym
= opaque_type_chain
[hash
];
1337 sym
= SYMBOL_VALUE_CHAIN (sym
);
1344 static struct symbol
*
1345 process_coff_symbol (cs
, aux
, section_offsets
, objfile
)
1346 register struct coff_symbol
*cs
;
1347 register union internal_auxent
*aux
;
1348 struct section_offsets
*section_offsets
;
1349 struct objfile
*objfile
;
1351 register struct symbol
*sym
1352 = (struct symbol
*) obstack_alloc (&objfile
->symbol_obstack
,
1353 sizeof (struct symbol
));
1356 memset (sym
, 0, sizeof (struct symbol
));
1358 name
= EXTERNAL_NAME (name
, objfile
->obfd
);
1359 SYMBOL_NAME (sym
) = obstack_copy0 (&objfile
->symbol_obstack
, name
,
1362 /* default assumptions */
1363 SYMBOL_VALUE (sym
) = cs
->c_value
;
1364 SYMBOL_NAMESPACE (sym
) = VAR_NAMESPACE
;
1366 if (ISFCN (cs
->c_type
))
1368 SYMBOL_VALUE (sym
) += ANOFFSET (section_offsets
, SECT_OFF_TEXT
);
1370 /* FIXME: This has NOT been tested. The DBX version has.. */
1371 /* Generate a template for the type of this function. The
1372 types of the arguments will be added as we read the symbol
1374 struct type
*new = (struct type
*)
1375 obstack_alloc (&objfile
->symbol_obstack
, sizeof (struct type
));
1377 memcpy (new, lookup_function_type (decode_function_type (cs
, cs
->c_type
, aux
)),
1378 sizeof(struct type
));
1379 SYMBOL_TYPE (sym
) = new;
1380 in_function_type
= SYMBOL_TYPE(sym
);
1383 lookup_function_type (decode_function_type (cs
, cs
->c_type
, aux
));
1386 SYMBOL_CLASS (sym
) = LOC_BLOCK
;
1387 if (cs
->c_sclass
== C_STAT
)
1388 add_symbol_to_list (sym
, &file_symbols
);
1389 else if (cs
->c_sclass
== C_EXT
)
1390 add_symbol_to_list (sym
, &global_symbols
);
1394 SYMBOL_TYPE (sym
) = decode_type (cs
, cs
->c_type
, aux
);
1395 switch (cs
->c_sclass
)
1401 SYMBOL_CLASS (sym
) = LOC_LOCAL
;
1402 add_symbol_to_list (sym
, &local_symbols
);
1406 SYMBOL_CLASS (sym
) = LOC_STATIC
;
1407 SYMBOL_VALUE_ADDRESS (sym
) = (CORE_ADDR
) cs
->c_value
;
1408 SYMBOL_VALUE_ADDRESS (sym
) += ANOFFSET (section_offsets
, SECT_OFF_TEXT
);
1409 add_symbol_to_list (sym
, &global_symbols
);
1413 SYMBOL_CLASS (sym
) = LOC_STATIC
;
1414 SYMBOL_VALUE_ADDRESS (sym
) = (CORE_ADDR
) cs
->c_value
;
1415 SYMBOL_VALUE_ADDRESS (sym
) += ANOFFSET (section_offsets
, SECT_OFF_TEXT
);
1416 if (within_function
) {
1417 /* Static symbol of local scope */
1418 add_symbol_to_list (sym
, &local_symbols
);
1421 /* Static symbol at top level of file */
1422 add_symbol_to_list (sym
, &file_symbols
);
1426 #ifdef C_GLBLREG /* AMD coff */
1430 SYMBOL_CLASS (sym
) = LOC_REGISTER
;
1431 SYMBOL_VALUE (sym
) = SDB_REG_TO_REGNUM(cs
->c_value
);
1432 add_symbol_to_list (sym
, &local_symbols
);
1439 SYMBOL_CLASS (sym
) = LOC_ARG
;
1441 /* FIXME: This has not been tested. */
1442 /* Add parameter to function. */
1443 add_param_to_type(&in_function_type
,sym
);
1445 add_symbol_to_list (sym
, &local_symbols
);
1446 #if !defined (BELIEVE_PCC_PROMOTION) && (TARGET_BYTE_ORDER == BIG_ENDIAN)
1448 /* If PCC says a parameter is a short or a char,
1449 aligned on an int boundary, realign it to the "little end"
1451 struct type
*temptype
;
1452 temptype
= lookup_fundamental_type (current_objfile
, FT_INTEGER
);
1453 if (TYPE_LENGTH (SYMBOL_TYPE (sym
)) < TYPE_LENGTH (temptype
)
1454 && TYPE_CODE (SYMBOL_TYPE (sym
)) == TYPE_CODE_INT
1455 && 0 == SYMBOL_VALUE (sym
) % TYPE_LENGTH (temptype
))
1457 SYMBOL_VALUE (sym
) +=
1458 TYPE_LENGTH (temptype
)
1459 - TYPE_LENGTH (SYMBOL_TYPE (sym
));
1466 SYMBOL_CLASS (sym
) = LOC_REGPARM
;
1467 SYMBOL_VALUE (sym
) = SDB_REG_TO_REGNUM(cs
->c_value
);
1468 add_symbol_to_list (sym
, &local_symbols
);
1469 #if !defined (BELIEVE_PCC_PROMOTION)
1470 /* FIXME: This should retain the current type, since it's just
1471 a register value. gnu@adobe, 26Feb93 */
1473 /* If PCC says a parameter is a short or a char,
1474 it is really an int. */
1475 struct type
*temptype
;
1477 lookup_fundamental_type (current_objfile
, FT_INTEGER
);
1478 if (TYPE_LENGTH (SYMBOL_TYPE (sym
)) < TYPE_LENGTH (temptype
)
1479 && TYPE_CODE (SYMBOL_TYPE (sym
)) == TYPE_CODE_INT
)
1482 (TYPE_UNSIGNED (SYMBOL_TYPE (sym
))
1483 ? lookup_fundamental_type (current_objfile
,
1484 FT_UNSIGNED_INTEGER
)
1492 SYMBOL_CLASS (sym
) = LOC_TYPEDEF
;
1493 SYMBOL_NAMESPACE (sym
) = VAR_NAMESPACE
;
1495 /* If type has no name, give it one */
1496 if (TYPE_NAME (SYMBOL_TYPE (sym
)) == 0)
1498 if (TYPE_CODE (SYMBOL_TYPE (sym
)) == TYPE_CODE_PTR
1499 || TYPE_CODE (SYMBOL_TYPE (sym
)) == TYPE_CODE_FUNC
)
1501 /* If we are giving a name to a type such as "pointer to
1502 foo" or "function returning foo", we better not set
1503 the TYPE_NAME. If the program contains "typedef char
1504 *caddr_t;", we don't want all variables of type char
1505 * to print as caddr_t. This is not just a
1506 consequence of GDB's type management; CC and GCC (at
1507 least through version 2.4) both output variables of
1508 either type char * or caddr_t with the type
1509 refering to the C_TPDEF symbol for caddr_t. If a future
1510 compiler cleans this up it GDB is not ready for it
1511 yet, but if it becomes ready we somehow need to
1512 disable this check (without breaking the PCC/GCC2.4
1517 Fortunately, this check seems not to be necessary
1518 for anything except pointers or functions. */
1522 TYPE_NAME (SYMBOL_TYPE (sym
)) =
1523 concat (SYMBOL_NAME (sym
), NULL
);
1526 /* Ignore vendor section for Harris CX/UX targets. */
1527 else if (cs
->c_name
[0] == '$')
1529 #endif /* CXUX_TARGET */
1531 /* Keep track of any type which points to empty structured type,
1532 so it can be filled from a definition from another file. A
1533 simple forward reference (TYPE_CODE_UNDEF) is not an
1534 empty structured type, though; the forward references
1535 work themselves out via the magic of coff_lookup_type. */
1536 if (TYPE_CODE (SYMBOL_TYPE (sym
)) == TYPE_CODE_PTR
&&
1537 TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym
))) == 0 &&
1538 TYPE_CODE (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym
))) !=
1541 register int i
= hashname (SYMBOL_NAME (sym
));
1543 SYMBOL_VALUE_CHAIN (sym
) = opaque_type_chain
[i
];
1544 opaque_type_chain
[i
] = sym
;
1546 add_symbol_to_list (sym
, &file_symbols
);
1552 SYMBOL_CLASS (sym
) = LOC_TYPEDEF
;
1553 SYMBOL_NAMESPACE (sym
) = STRUCT_NAMESPACE
;
1555 /* Some compilers try to be helpful by inventing "fake"
1556 names for anonymous enums, structures, and unions, like
1557 "~0fake" or ".0fake". Thanks, but no thanks... */
1558 if (TYPE_TAG_NAME (SYMBOL_TYPE (sym
)) == 0)
1559 if (SYMBOL_NAME(sym
) != NULL
1560 && *SYMBOL_NAME(sym
) != '~'
1561 && *SYMBOL_NAME(sym
) != '.')
1562 TYPE_TAG_NAME (SYMBOL_TYPE (sym
)) =
1563 concat (SYMBOL_NAME (sym
), NULL
);
1565 add_symbol_to_list (sym
, &file_symbols
);
1575 /* Decode a coff type specifier; return the type that is meant. */
1577 static struct type
*
1578 decode_type (cs
, c_type
, aux
)
1579 register struct coff_symbol
*cs
;
1580 unsigned int c_type
;
1581 register union internal_auxent
*aux
;
1583 register struct type
*type
= 0;
1584 unsigned int new_c_type
;
1586 if (c_type
& ~N_BTMASK
)
1588 new_c_type
= DECREF (c_type
);
1591 type
= decode_type (cs
, new_c_type
, aux
);
1592 type
= lookup_pointer_type (type
);
1594 else if (ISFCN (c_type
))
1596 type
= decode_type (cs
, new_c_type
, aux
);
1597 type
= lookup_function_type (type
);
1599 else if (ISARY (c_type
))
1602 register unsigned short *dim
;
1603 struct type
*base_type
, *index_type
, *range_type
;
1605 /* Define an array type. */
1606 /* auxent refers to array, not base type */
1607 if (aux
->x_sym
.x_tagndx
.l
== 0)
1610 /* shift the indices down */
1611 dim
= &aux
->x_sym
.x_fcnary
.x_ary
.x_dimen
[0];
1614 for (i
= 0; *dim
&& i
< DIMNUM
- 1; i
++, dim
++)
1618 base_type
= decode_type (cs
, new_c_type
, aux
);
1619 index_type
= lookup_fundamental_type (current_objfile
, FT_INTEGER
);
1621 create_range_type ((struct type
*) NULL
, index_type
, 0, n
- 1);
1623 create_array_type ((struct type
*) NULL
, base_type
, range_type
);
1628 /* Reference to existing type. This only occurs with the
1629 struct, union, and enum types. EPI a29k coff
1630 fakes us out by producing aux entries with a nonzero
1631 x_tagndx for definitions of structs, unions, and enums, so we
1632 have to check the c_sclass field. SCO 3.2v4 cc gets confused
1633 with pointers to pointers to defined structs, and generates
1634 negative x_tagndx fields. */
1635 if (cs
->c_naux
> 0 && aux
->x_sym
.x_tagndx
.l
!= 0)
1637 if (cs
->c_sclass
!= C_STRTAG
1638 && cs
->c_sclass
!= C_UNTAG
1639 && cs
->c_sclass
!= C_ENTAG
1640 && aux
->x_sym
.x_tagndx
.l
>= 0)
1642 type
= coff_alloc_type (aux
->x_sym
.x_tagndx
.l
);
1647 complain (&tagndx_bad_complaint
, cs
->c_name
);
1648 /* And fall through to decode_base_type... */
1652 return decode_base_type (cs
, BTYPE (c_type
), aux
);
1655 /* Decode a coff type specifier for function definition;
1656 return the type that the function returns. */
1658 static struct type
*
1659 decode_function_type (cs
, c_type
, aux
)
1660 register struct coff_symbol
*cs
;
1661 unsigned int c_type
;
1662 register union internal_auxent
*aux
;
1664 if (aux
->x_sym
.x_tagndx
.l
== 0)
1665 cs
->c_naux
= 0; /* auxent refers to function, not base type */
1667 return decode_type (cs
, DECREF (c_type
), aux
);
1672 static struct type
*
1673 decode_base_type (cs
, c_type
, aux
)
1674 register struct coff_symbol
*cs
;
1675 unsigned int c_type
;
1676 register union internal_auxent
*aux
;
1683 /* shows up with "void (*foo)();" structure members */
1684 return lookup_fundamental_type (current_objfile
, FT_VOID
);
1687 /* DGUX actually defines both T_ARG and T_VOID to the same value. */
1690 /* Shows up in DGUX, I think. Not sure where. */
1691 return lookup_fundamental_type (current_objfile
, FT_VOID
); /* shouldn't show up here */
1697 /* Intel 960 COFF has this symbol and meaning. */
1698 return lookup_fundamental_type (current_objfile
, FT_VOID
);
1702 return lookup_fundamental_type (current_objfile
, FT_CHAR
);
1705 return lookup_fundamental_type (current_objfile
, FT_SHORT
);
1708 return lookup_fundamental_type (current_objfile
, FT_INTEGER
);
1711 return lookup_fundamental_type (current_objfile
, FT_LONG
);
1714 return lookup_fundamental_type (current_objfile
, FT_FLOAT
);
1717 return lookup_fundamental_type (current_objfile
, FT_DBL_PREC_FLOAT
);
1720 if (cs
->c_naux
!= 1)
1722 /* anonymous structure type */
1723 type
= coff_alloc_type (cs
->c_symnum
);
1724 TYPE_CODE (type
) = TYPE_CODE_STRUCT
;
1725 TYPE_NAME (type
) = NULL
;
1726 /* This used to set the tag to "<opaque>". But I think setting it
1727 to NULL is right, and the printing code can print it as
1729 TYPE_TAG_NAME (type
) = NULL
;
1730 INIT_CPLUS_SPECIFIC(type
);
1731 TYPE_LENGTH (type
) = 0;
1732 TYPE_FIELDS (type
) = 0;
1733 TYPE_NFIELDS (type
) = 0;
1737 type
= coff_read_struct_type (cs
->c_symnum
,
1738 aux
->x_sym
.x_misc
.x_lnsz
.x_size
,
1739 aux
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
);
1744 if (cs
->c_naux
!= 1)
1746 /* anonymous union type */
1747 type
= coff_alloc_type (cs
->c_symnum
);
1748 TYPE_NAME (type
) = NULL
;
1749 /* This used to set the tag to "<opaque>". But I think setting it
1750 to NULL is right, and the printing code can print it as
1752 TYPE_TAG_NAME (type
) = NULL
;
1753 INIT_CPLUS_SPECIFIC(type
);
1754 TYPE_LENGTH (type
) = 0;
1755 TYPE_FIELDS (type
) = 0;
1756 TYPE_NFIELDS (type
) = 0;
1760 type
= coff_read_struct_type (cs
->c_symnum
,
1761 aux
->x_sym
.x_misc
.x_lnsz
.x_size
,
1762 aux
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
);
1764 TYPE_CODE (type
) = TYPE_CODE_UNION
;
1768 if (cs
->c_naux
!= 1)
1770 /* anonymous enum type */
1771 type
= coff_alloc_type (cs
->c_symnum
);
1772 TYPE_CODE (type
) = TYPE_CODE_ENUM
;
1773 TYPE_NAME (type
) = NULL
;
1774 /* This used to set the tag to "<opaque>". But I think setting it
1775 to NULL is right, and the printing code can print it as
1777 TYPE_TAG_NAME (type
) = NULL
;
1778 TYPE_LENGTH (type
) = 0;
1779 TYPE_FIELDS (type
) = 0;
1780 TYPE_NFIELDS(type
) = 0;
1784 type
= coff_read_enum_type (cs
->c_symnum
,
1785 aux
->x_sym
.x_misc
.x_lnsz
.x_size
,
1786 aux
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
);
1791 /* shouldn't show up here */
1795 return lookup_fundamental_type (current_objfile
, FT_UNSIGNED_CHAR
);
1798 return lookup_fundamental_type (current_objfile
, FT_UNSIGNED_SHORT
);
1801 return lookup_fundamental_type (current_objfile
, FT_UNSIGNED_INTEGER
);
1804 return lookup_fundamental_type (current_objfile
, FT_UNSIGNED_LONG
);
1806 complain (&unexpected_type_complaint
, cs
->c_name
);
1807 return lookup_fundamental_type (current_objfile
, FT_VOID
);
1810 /* This page contains subroutines of read_type. */
1812 /* Read the description of a structure (or union type) and return an
1813 object describing the type. */
1815 static struct type
*
1816 coff_read_struct_type (index
, length
, lastsym
)
1823 struct nextfield
*next
;
1827 register struct type
*type
;
1828 register struct nextfield
*list
= 0;
1829 struct nextfield
*new;
1833 struct coff_symbol member_sym
;
1834 register struct coff_symbol
*ms
= &member_sym
;
1835 struct internal_syment sub_sym
;
1836 union internal_auxent sub_aux
;
1839 type
= coff_alloc_type (index
);
1840 TYPE_CODE (type
) = TYPE_CODE_STRUCT
;
1841 INIT_CPLUS_SPECIFIC(type
);
1842 TYPE_LENGTH (type
) = length
;
1844 while (!done
&& symnum
< lastsym
&& symnum
< nlist_nsyms_global
)
1846 read_one_sym (ms
, &sub_sym
, &sub_aux
);
1848 name
= EXTERNAL_NAME (name
, current_objfile
->obfd
);
1850 switch (ms
->c_sclass
)
1855 /* Get space to record the next field's data. */
1856 new = (struct nextfield
*) alloca (sizeof (struct nextfield
));
1860 /* Save the data. */
1864 ¤t_objfile
->symbol_obstack
);
1865 list
->field
.type
= decode_type (ms
, ms
->c_type
, &sub_aux
);
1866 list
->field
.bitpos
= 8 * ms
->c_value
;
1867 list
->field
.bitsize
= 0;
1873 /* Get space to record the next field's data. */
1874 new = (struct nextfield
*) alloca (sizeof (struct nextfield
));
1878 /* Save the data. */
1882 ¤t_objfile
->symbol_obstack
);
1883 list
->field
.type
= decode_type (ms
, ms
->c_type
, &sub_aux
);
1884 list
->field
.bitpos
= ms
->c_value
;
1885 list
->field
.bitsize
= sub_aux
.x_sym
.x_misc
.x_lnsz
.x_size
;
1894 /* Now create the vector of fields, and record how big it is. */
1896 TYPE_NFIELDS (type
) = nfields
;
1897 TYPE_FIELDS (type
) = (struct field
*)
1898 TYPE_ALLOC (type
, sizeof (struct field
) * nfields
);
1900 /* Copy the saved-up fields into the field vector. */
1902 for (n
= nfields
; list
; list
= list
->next
)
1903 TYPE_FIELD (type
, --n
) = list
->field
;
1908 /* Read a definition of an enumeration type,
1909 and create and return a suitable type object.
1910 Also defines the symbols that represent the values of the type. */
1913 static struct type
*
1914 coff_read_enum_type (index
, length
, lastsym
)
1919 register struct symbol
*sym
;
1920 register struct type
*type
;
1923 struct pending
**symlist
;
1924 struct coff_symbol member_sym
;
1925 register struct coff_symbol
*ms
= &member_sym
;
1926 struct internal_syment sub_sym
;
1927 union internal_auxent sub_aux
;
1928 struct pending
*osyms
, *syms
;
1933 type
= coff_alloc_type (index
);
1934 if (within_function
)
1935 symlist
= &local_symbols
;
1937 symlist
= &file_symbols
;
1939 o_nsyms
= osyms
? osyms
->nsyms
: 0;
1941 while (!done
&& symnum
< lastsym
&& symnum
< nlist_nsyms_global
)
1943 read_one_sym (ms
, &sub_sym
, &sub_aux
);
1945 name
= EXTERNAL_NAME (name
, current_objfile
->obfd
);
1947 switch (ms
->c_sclass
)
1950 sym
= (struct symbol
*) obstack_alloc
1951 (¤t_objfile
->symbol_obstack
,
1952 sizeof (struct symbol
));
1953 memset (sym
, 0, sizeof (struct symbol
));
1956 obsavestring (name
, strlen (name
),
1957 ¤t_objfile
->symbol_obstack
);
1958 SYMBOL_CLASS (sym
) = LOC_CONST
;
1959 SYMBOL_NAMESPACE (sym
) = VAR_NAMESPACE
;
1960 SYMBOL_VALUE (sym
) = ms
->c_value
;
1961 add_symbol_to_list (sym
, symlist
);
1966 /* Sometimes the linker (on 386/ix 2.0.2 at least) screws
1967 up the count of how many symbols to read. So stop
1974 /* Now fill in the fields of the type-structure. */
1977 TYPE_LENGTH (type
) = length
;
1979 TYPE_LENGTH (type
) = TARGET_INT_BIT
/ TARGET_CHAR_BIT
; /* Assume ints */
1980 TYPE_CODE (type
) = TYPE_CODE_ENUM
;
1981 TYPE_NFIELDS (type
) = nsyms
;
1982 TYPE_FIELDS (type
) = (struct field
*)
1983 TYPE_ALLOC (type
, sizeof (struct field
) * nsyms
);
1985 /* Find the symbols for the values and put them into the type.
1986 The symbols can be found in the symlist that we put them on
1987 to cause them to be defined. osyms contains the old value
1988 of that symlist; everything up to there was defined by us. */
1989 /* Note that we preserve the order of the enum constants, so
1990 that in something like "enum {FOO, LAST_THING=FOO}" we print
1991 FOO, not LAST_THING. */
1993 for (syms
= *symlist
, n
= 0; syms
; syms
= syms
->next
)
1999 for (; j
< syms
->nsyms
; j
++,n
++)
2001 struct symbol
*xsym
= syms
->symbol
[j
];
2002 SYMBOL_TYPE (xsym
) = type
;
2003 TYPE_FIELD_NAME (type
, n
) = SYMBOL_NAME (xsym
);
2004 TYPE_FIELD_VALUE (type
, n
) = 0;
2005 TYPE_FIELD_BITPOS (type
, n
) = SYMBOL_VALUE (xsym
);
2006 TYPE_FIELD_BITSIZE (type
, n
) = 0;
2015 struct section_offsets
*
2016 coff_symfile_offsets (objfile
, addr
)
2017 struct objfile
*objfile
;
2020 struct section_offsets
*section_offsets
;
2023 objfile
->num_sections
= SECT_OFF_MAX
;
2024 section_offsets
= (struct section_offsets
*)
2025 obstack_alloc (&objfile
-> psymbol_obstack
,
2026 sizeof (struct section_offsets
)
2027 + sizeof (section_offsets
->offsets
) * (SECT_OFF_MAX
-1));
2029 for (i
= 0; i
< SECT_OFF_MAX
; i
++)
2030 ANOFFSET (section_offsets
, i
) = addr
;
2032 return section_offsets
;
2035 /* Register our ability to parse symbols for coff BFD files. */
2037 static struct sym_fns coff_sym_fns
=
2039 bfd_target_coff_flavour
,
2040 coff_new_init
, /* sym_new_init: init anything gbl to entire symtab */
2041 coff_symfile_init
, /* sym_init: read initial info, setup for sym_read() */
2042 coff_symfile_read
, /* sym_read: read a symbol file into symtab */
2043 coff_symfile_finish
, /* sym_finish: finished with file, cleanup */
2044 coff_symfile_offsets
, /* sym_offsets: xlate external to internal form */
2045 NULL
/* next: pointer to next struct sym_fns */
2049 _initialize_coffread ()
2051 add_symtab_fns (&coff_sym_fns
);