1 /* Read coff symbol tables and convert to internal format, for GDB.
2 Copyright 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1996, 1997
3 Free Software Foundation, Inc.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 #include "breakpoint.h"
30 #include "gdb_string.h"
33 #include "coff/internal.h" /* Internal format of COFF symbols in BFD */
34 #include "libcoff.h" /* FIXME secret internal data from BFD */
39 #include "gdb-stabs.h"
40 #include "stabsread.h"
41 #include "complaints.h"
44 struct coff_symfile_info {
45 file_ptr min_lineno_offset; /* Where in file lowest line#s are */
46 file_ptr max_lineno_offset; /* 1+last byte of line#s in file */
48 CORE_ADDR textaddr; /* Addr of .text section. */
49 unsigned int textsize; /* Size of .text section. */
50 struct stab_section_list *stabsects; /* .stab sections. */
51 asection *stabstrsect; /* Section pointer for .stab section */
55 /* Translate an external name string into a user-visible name. */
56 #define EXTERNAL_NAME(string, abfd) \
57 (string[0] == bfd_get_symbol_leading_char(abfd)? string+1: string)
59 /* To be an sdb debug type, type must have at least a basic or primary
60 derived type. Using this rather than checking against T_NULL is
61 said to prevent core dumps if we try to operate on Michael Bloom
64 #define SDB_TYPE(type) (BTYPE(type) | (type & N_TMASK))
66 /* Convert from an sdb register number to an internal gdb register number.
67 This should be defined in tm.h, if REGISTER_NAMES is not set up
68 to map one to one onto the sdb register numbers. */
70 #ifndef SDB_REG_TO_REGNUM
71 # define SDB_REG_TO_REGNUM(value) (value)
74 /* Core address of start and end of text of current source file.
75 This comes from a ".text" symbol where x_nlinno > 0. */
77 static CORE_ADDR current_source_start_addr;
78 static CORE_ADDR current_source_end_addr;
80 /* The addresses of the symbol table stream and number of symbols
81 of the object file we are reading (as copied into core). */
83 static bfd *nlist_bfd_global;
84 static int nlist_nsyms_global;
86 /* Vector of line number information. */
88 static struct linetable *line_vector;
90 /* Index of next entry to go in line_vector_index. */
92 static int line_vector_index;
94 /* Last line number recorded in the line vector. */
96 static int prev_line_number;
98 /* Number of elements allocated for line_vector currently. */
100 static int line_vector_length;
102 /* Pointers to scratch storage, used for reading raw symbols and auxents. */
104 static char *temp_sym;
105 static char *temp_aux;
107 /* Local variables that hold the shift and mask values for the
108 COFF file that we are currently reading. These come back to us
109 from BFD, and are referenced by their macro names, as well as
110 internally to the BTYPE, ISPTR, ISFCN, ISARY, ISTAG, and DECREF
111 macros from include/coff/internal.h . */
113 static unsigned local_n_btmask;
114 static unsigned local_n_btshft;
115 static unsigned local_n_tmask;
116 static unsigned local_n_tshift;
118 #define N_BTMASK local_n_btmask
119 #define N_BTSHFT local_n_btshft
120 #define N_TMASK local_n_tmask
121 #define N_TSHIFT local_n_tshift
123 /* Local variables that hold the sizes in the file of various COFF structures.
124 (We only need to know this to read them from the file -- BFD will then
125 translate the data in them, into `internal_xxx' structs in the right
126 byte order, alignment, etc.) */
128 static unsigned local_linesz;
129 static unsigned local_symesz;
130 static unsigned local_auxesz;
132 /* This is set if this is a PE format file. */
136 /* Chain of typedefs of pointers to empty struct/union types.
137 They are chained thru the SYMBOL_VALUE_CHAIN. */
139 static struct symbol *opaque_type_chain[HASHSIZE];
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 ef_stack_complaint =
147 {"`.ef' symbol without matching `.bf' symbol ignored starting at symnum %d", 0, 0};
149 struct complaint eb_stack_complaint =
150 {"`.eb' symbol without matching `.bb' symbol ignored starting at symnum %d", 0, 0};
152 struct complaint bf_no_aux_complaint =
153 {"`.bf' symbol %d has no aux entry", 0, 0};
155 struct complaint ef_no_aux_complaint =
156 {"`.ef' symbol %d has no aux entry", 0, 0};
158 struct complaint lineno_complaint =
159 {"Line number pointer %d lower than start of line numbers", 0, 0};
161 struct complaint unexpected_type_complaint =
162 {"Unexpected type for symbol %s", 0, 0};
164 struct complaint bad_sclass_complaint =
165 {"Bad n_sclass for symbol %s", 0, 0};
167 struct complaint misordered_blocks_complaint =
168 {"Blocks out of order at address %x", 0, 0};
170 struct complaint tagndx_bad_complaint =
171 {"Symbol table entry for %s has bad tagndx value", 0, 0};
173 struct complaint eb_complaint =
174 {"Mismatched .eb symbol ignored starting at symnum %d", 0, 0};
176 /* Simplified internal version of coff symbol table information */
180 int c_symnum; /* symbol number of this entry */
181 int c_naux; /* 0 if syment only, 1 if syment + auxent, etc */
188 static struct type *coff_read_struct_type PARAMS ((int, int, int));
190 static struct type *decode_base_type PARAMS ((struct coff_symbol *,
192 union internal_auxent *));
194 static struct type *decode_type PARAMS ((struct coff_symbol *, unsigned int,
195 union internal_auxent *));
197 static struct type *decode_function_type PARAMS ((struct coff_symbol *,
199 union internal_auxent *));
201 static struct type *coff_read_enum_type PARAMS ((int, int, int));
203 static struct symbol *process_coff_symbol PARAMS ((struct coff_symbol *,
204 union internal_auxent *,
205 struct section_offsets *,
208 static void patch_opaque_types PARAMS ((struct symtab *));
210 static void patch_type PARAMS ((struct type *, struct type *));
212 static void enter_linenos PARAMS ((long, int, int, struct section_offsets *));
214 static void free_linetab PARAMS ((void));
216 static int init_lineno PARAMS ((bfd *, long, int));
218 static char *getsymname PARAMS ((struct internal_syment *));
220 static char *coff_getfilename PARAMS ((union internal_auxent *));
222 static void free_stringtab PARAMS ((void));
224 static int init_stringtab PARAMS ((bfd *, long));
226 static void read_one_sym PARAMS ((struct coff_symbol *,
227 struct internal_syment *,
228 union internal_auxent *));
230 static void coff_symtab_read PARAMS ((long, int, struct section_offsets *,
233 static void find_linenos PARAMS ((bfd *, sec_ptr, PTR));
235 static void coff_symfile_init PARAMS ((struct objfile *));
237 static void coff_new_init PARAMS ((struct objfile *));
239 static void coff_symfile_read PARAMS ((struct objfile *,
240 struct section_offsets *, int));
242 static void coff_symfile_finish PARAMS ((struct objfile *));
244 static void record_minimal_symbol PARAMS ((char *, CORE_ADDR,
245 enum minimal_symbol_type,
248 static void coff_end_symtab PARAMS ((struct objfile *));
250 static void complete_symtab PARAMS ((char *, CORE_ADDR, unsigned int));
252 static void coff_start_symtab PARAMS ((void));
254 static void coff_record_line PARAMS ((int, CORE_ADDR));
256 static struct type *coff_alloc_type PARAMS ((int));
258 static struct type **coff_lookup_type PARAMS ((int));
260 static void coff_locate_sections PARAMS ((bfd *, asection *, PTR));
262 /* We are called once per section from coff_symfile_read. We
263 need to examine each section we are passed, check to see
264 if it is something we are interested in processing, and
265 if so, stash away some access information for the section.
267 FIXME: The section names should not be hardwired strings (what
268 should they be? I don't think most object file formats have enough
269 section flags to specify what kind of debug section it is
273 coff_locate_sections (abfd, sectp, csip)
278 register struct coff_symfile_info *csi;
281 csi = (struct coff_symfile_info *) csip;
282 name = bfd_get_section_name (abfd, sectp);
283 if (STREQ (name, ".text"))
285 csi->textaddr = bfd_section_vma (abfd, sectp);
286 csi->textsize += bfd_section_size (abfd, sectp);
288 else if (strncmp (name, ".text", sizeof ".text" - 1) == 0)
290 csi->textsize += bfd_section_size (abfd, sectp);
292 else if (STREQ (name, ".stabstr"))
294 csi->stabstrsect = sectp;
296 else if (strncmp (name, ".stab", sizeof ".stab" - 1) == 0)
300 /* We can have multiple .stab sections if linked with
302 for (s = name + sizeof ".stab" - 1; *s != '\0'; s++)
307 struct stab_section_list *n, **pn;
309 n = ((struct stab_section_list *)
310 xmalloc (sizeof (struct stab_section_list)));
313 for (pn = &csi->stabsects; *pn != NULL; pn = &(*pn)->next)
317 /* This will be run after coffstab_build_psymtabs is called
318 in coff_symfile_read, at which point we no longer need
320 make_cleanup (free, n);
325 /* Return the section_offsets* that CS points to. */
326 static int cs_to_section PARAMS ((struct coff_symbol *, struct objfile *));
328 struct find_targ_sec_arg {
333 static void find_targ_sec PARAMS ((bfd *, asection *, void *));
335 static void find_targ_sec (abfd, sect, obj)
340 struct find_targ_sec_arg *args = (struct find_targ_sec_arg *)obj;
341 if (sect->target_index == args->targ_index)
342 *args->resultp = sect;
345 /* Return the section number (SECT_OFF_*) that CS points to. */
347 cs_to_section (cs, objfile)
348 struct coff_symbol *cs;
349 struct objfile *objfile;
351 asection *sect = NULL;
352 struct find_targ_sec_arg args;
353 int off = SECT_OFF_TEXT;
355 args.targ_index = cs->c_secnum;
356 args.resultp = §
357 bfd_map_over_sections (objfile->obfd, find_targ_sec, &args);
360 /* This is the section. Figure out what SECT_OFF_* code it is. */
361 if (bfd_get_section_flags (abfd, sect) & SEC_CODE)
363 else if (bfd_get_section_flags (abfd, sect) & SEC_LOAD)
371 /* Return the address of the section of a COFF symbol. */
373 static CORE_ADDR cs_section_address PARAMS ((struct coff_symbol *, bfd *));
376 cs_section_address (cs, abfd)
377 struct coff_symbol *cs;
380 asection *sect = NULL;
381 struct find_targ_sec_arg args;
384 args.targ_index = cs->c_secnum;
385 args.resultp = §
386 bfd_map_over_sections (abfd, find_targ_sec, &args);
388 addr = bfd_get_section_vma (objfile->obfd, sect);
392 /* Look up a coff type-number index. Return the address of the slot
393 where the type for that index is stored.
394 The type-number is in INDEX.
396 This can be used for finding the type associated with that index
397 or for associating a new type with the index. */
399 static struct type **
400 coff_lookup_type (index)
403 if (index >= type_vector_length)
405 int old_vector_length = type_vector_length;
407 type_vector_length *= 2;
408 if (index /* is still */ >= type_vector_length)
409 type_vector_length = index * 2;
411 type_vector = (struct type **)
412 xrealloc ((char *) type_vector,
413 type_vector_length * sizeof (struct type *));
414 memset (&type_vector[old_vector_length], 0,
415 (type_vector_length - old_vector_length) * sizeof(struct type *));
417 return &type_vector[index];
420 /* Make sure there is a type allocated for type number index
421 and return the type object.
422 This can create an empty (zeroed) type object. */
425 coff_alloc_type (index)
428 register struct type **type_addr = coff_lookup_type (index);
429 register struct type *type = *type_addr;
431 /* If we are referring to a type not known at all yet,
432 allocate an empty type for it.
433 We will fill it in later if we find out how. */
436 type = alloc_type (current_objfile);
442 /* Record a line number entry for line LINE at address PC.
443 FIXME: Use record_line instead. */
446 coff_record_line (line, pc)
450 struct linetable_entry *e;
451 /* Make sure line vector is big enough. */
453 if (line_vector_index + 2 >= line_vector_length)
455 line_vector_length *= 2;
456 line_vector = (struct linetable *)
457 xrealloc ((char *) line_vector, sizeof (struct linetable)
458 + (line_vector_length
459 * sizeof (struct linetable_entry)));
462 e = line_vector->item + line_vector_index++;
463 e->line = line; e->pc = pc;
466 /* Start a new symtab for a new source file.
467 This is called when a COFF ".file" symbol is seen;
468 it indicates the start of data for one original source file. */
474 /* We fill in the filename later. start_symtab puts
475 this pointer into last_source_file and we put it in
476 subfiles->name, which end_symtab frees; that's why
477 it must be malloc'd. */
479 /* We never know the directory name for COFF. */
481 /* The start address is irrelevant, since we set
482 last_source_start_addr in coff_end_symtab. */
484 record_debugformat ("COFF");
486 /* Initialize the source file line number information for this file. */
488 if (line_vector) /* Unlikely, but maybe possible? */
489 free ((PTR)line_vector);
490 line_vector_index = 0;
491 line_vector_length = 1000;
492 prev_line_number = -2; /* Force first line number to be explicit */
493 line_vector = (struct linetable *)
494 xmalloc (sizeof (struct linetable)
495 + line_vector_length * sizeof (struct linetable_entry));
498 /* Save the vital information from when starting to read a file,
499 for use when closing off the current file.
500 NAME is the file name the symbols came from, START_ADDR is the first
501 text address for the file, and SIZE is the number of bytes of text. */
504 complete_symtab (name, start_addr, size)
506 CORE_ADDR start_addr;
509 if (last_source_file != NULL)
510 free (last_source_file);
511 last_source_file = savestring (name, strlen (name));
512 current_source_start_addr = start_addr;
513 current_source_end_addr = start_addr + size;
515 if (current_objfile -> ei.entry_point >= current_source_start_addr &&
516 current_objfile -> ei.entry_point < current_source_end_addr)
518 current_objfile -> ei.entry_file_lowpc = current_source_start_addr;
519 current_objfile -> ei.entry_file_highpc = current_source_end_addr;
523 /* Finish the symbol definitions for one main source file,
524 close off all the lexical contexts for that file
525 (creating struct block's for them), then make the
526 struct symtab for that file and put it in the list of all such. */
529 coff_end_symtab (objfile)
530 struct objfile *objfile;
532 struct symtab *symtab;
534 last_source_start_addr = current_source_start_addr;
536 /* For no good reason, this file stores the number of entries in a
537 separate variable instead of in line_vector->nitems. Fix it. */
539 line_vector->nitems = line_vector_index;
541 /* For COFF, we only have one subfile, so we can just look at
542 subfiles and not worry about there being other elements in the
543 chain. We fill in various fields now because we didn't know them
544 before (or because doing it now is simply an artifact of how this
545 file used to be written). */
546 subfiles->line_vector = line_vector;
547 subfiles->name = last_source_file;
549 symtab = end_symtab (current_source_end_addr, objfile, 0);
552 free_named_symtabs (symtab->filename);
554 /* Reinitialize for beginning of new file. */
556 line_vector_length = -1;
557 last_source_file = NULL;
561 record_minimal_symbol (name, address, type, objfile)
564 enum minimal_symbol_type type;
565 struct objfile *objfile;
567 /* We don't want TDESC entry points in the minimal symbol table */
568 if (name[0] == '@') return;
570 prim_record_minimal_symbol (name, address, type, objfile);
573 /* coff_symfile_init ()
574 is the coff-specific initialization routine for reading symbols.
575 It is passed a struct objfile which contains, among other things,
576 the BFD for the file whose symbols are being read, and a slot for
577 a pointer to "private data" which we fill with cookies and other
578 treats for coff_symfile_read ().
580 We will only be called if this is a COFF or COFF-like file.
581 BFD handles figuring out the format of the file, and code in symtab.c
582 uses BFD's determination to vector to us.
584 The ultimate result is a new symtab (or, FIXME, eventually a psymtab). */
587 coff_symfile_init (objfile)
588 struct objfile *objfile;
590 /* Allocate struct to keep track of stab reading. */
591 objfile->sym_stab_info = (PTR)
592 xmmalloc (objfile->md, sizeof (struct dbx_symfile_info));
594 memset ((PTR) objfile->sym_stab_info, 0, sizeof (struct dbx_symfile_info));
596 /* Allocate struct to keep track of the symfile */
597 objfile->sym_private = xmmalloc (objfile->md,
598 sizeof (struct coff_symfile_info));
600 memset (objfile->sym_private, 0, sizeof (struct coff_symfile_info));
602 /* COFF objects may be reordered, so set OBJF_REORDERED. If we
603 find this causes a significant slowdown in gdb then we could
604 set it in the debug symbol readers only when necessary. */
605 objfile->flags |= OBJF_REORDERED;
607 init_entry_point_info (objfile);
610 /* This function is called for every section; it finds the outer limits
611 of the line table (minimum and maximum file offset) so that the
612 mainline code can read the whole thing for efficiency. */
616 find_linenos (abfd, asect, vpinfo)
621 struct coff_symfile_info *info;
623 file_ptr offset, maxoff;
625 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
626 count = asect->lineno_count;
631 size = count * local_linesz;
633 info = (struct coff_symfile_info *)vpinfo;
634 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
635 offset = asect->line_filepos;
638 if (offset < info->min_lineno_offset || info->min_lineno_offset == 0)
639 info->min_lineno_offset = offset;
641 maxoff = offset + size;
642 if (maxoff > info->max_lineno_offset)
643 info->max_lineno_offset = maxoff;
647 /* The BFD for this file -- only good while we're actively reading
648 symbols into a psymtab or a symtab. */
650 static bfd *symfile_bfd;
652 /* Read a symbol file, after initialization by coff_symfile_init. */
656 coff_symfile_read (objfile, section_offsets, mainline)
657 struct objfile *objfile;
658 struct section_offsets *section_offsets;
661 struct coff_symfile_info *info;
662 struct dbx_symfile_info *dbxinfo;
663 bfd *abfd = objfile->obfd;
664 coff_data_type *cdata = coff_data (abfd);
665 char *name = bfd_get_filename (abfd);
669 int stringtab_offset;
670 struct cleanup *back_to;
673 info = (struct coff_symfile_info *) objfile -> sym_private;
674 dbxinfo = objfile->sym_stab_info;
675 symfile_bfd = abfd; /* Kludge for swap routines */
677 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
678 num_symbols = bfd_get_symcount (abfd); /* How many syms */
679 symtab_offset = cdata->sym_filepos; /* Symbol table file offset */
680 stringtab_offset = symtab_offset + /* String table file offset */
681 num_symbols * cdata->local_symesz;
683 /* Set a few file-statics that give us specific information about
684 the particular COFF file format we're reading. */
685 local_linesz = cdata->local_linesz;
686 local_n_btmask = cdata->local_n_btmask;
687 local_n_btshft = cdata->local_n_btshft;
688 local_n_tmask = cdata->local_n_tmask;
689 local_n_tshift = cdata->local_n_tshift;
690 local_linesz = cdata->local_linesz;
691 local_symesz = cdata->local_symesz;
692 local_auxesz = cdata->local_auxesz;
694 /* Allocate space for raw symbol and aux entries, based on their
695 space requirements as reported by BFD. */
696 temp_sym = (char *) xmalloc
697 (cdata->local_symesz + cdata->local_auxesz);
698 temp_aux = temp_sym + cdata->local_symesz;
699 back_to = make_cleanup (free_current_contents, &temp_sym);
701 /* We need to know whether this is a PE file, because in PE files,
702 unlike standard COFF files, symbol values are stored as offsets
703 from the section address, rather than as absolute addresses.
704 FIXME: We should use BFD to read the symbol table, and thus avoid
706 pe_file = strncmp (bfd_get_target (objfile->obfd), "pe", 2) == 0;
710 /* Read the line number table, all at once. */
711 info->min_lineno_offset = 0;
712 info->max_lineno_offset = 0;
713 bfd_map_over_sections (abfd, find_linenos, (PTR) info);
715 make_cleanup (free_linetab, 0);
716 val = init_lineno (abfd, info->min_lineno_offset,
717 info->max_lineno_offset - info->min_lineno_offset);
719 error ("\"%s\": error reading line numbers\n", name);
721 /* Now read the string table, all at once. */
723 make_cleanup (free_stringtab, 0);
724 val = init_stringtab (abfd, stringtab_offset);
726 error ("\"%s\": can't get string table", name);
728 init_minimal_symbol_collection ();
729 make_cleanup (discard_minimal_symbols, 0);
731 /* Now that the executable file is positioned at symbol table,
732 process it and define symbols accordingly. */
734 coff_symtab_read ((long) symtab_offset, num_symbols, section_offsets,
737 /* Sort symbols alphabetically within each block. */
742 for (s = objfile -> symtabs; s != NULL; s = s -> next)
743 sort_symtab_syms (s);
746 /* Install any minimal symbols that have been collected as the current
747 minimal symbols for this objfile. */
749 install_minimal_symbols (objfile);
751 bfd_map_over_sections (abfd, coff_locate_sections, (PTR) info);
755 /* FIXME: dubious. Why can't we use something normal like
756 bfd_get_section_contents? */
757 bfd_seek (abfd, abfd->where, 0);
759 stabstrsize = bfd_section_size (abfd, info->stabstrsect);
761 coffstab_build_psymtabs (objfile,
764 info->textaddr, info->textsize,
766 info->stabstrsect->filepos, stabstrsize);
769 do_cleanups (back_to);
773 coff_new_init (ignore)
774 struct objfile *ignore;
778 /* Perform any local cleanups required when we are done with a particular
779 objfile. I.E, we are in the process of discarding all symbol information
780 for an objfile, freeing up all memory held for it, and unlinking the
781 objfile struct from the global list of known objfiles. */
784 coff_symfile_finish (objfile)
785 struct objfile *objfile;
787 if (objfile -> sym_private != NULL)
789 mfree (objfile -> md, objfile -> sym_private);
794 /* Given pointers to a symbol table in coff style exec file,
795 analyze them and create struct symtab's describing the symbols.
796 NSYMS is the number of symbols in the symbol table.
797 We read them one at a time using read_one_sym (). */
800 coff_symtab_read (symtab_offset, nsyms, section_offsets, objfile)
803 struct section_offsets *section_offsets;
804 struct objfile *objfile;
806 register struct context_stack *new;
807 struct coff_symbol coff_symbol;
808 register struct coff_symbol *cs = &coff_symbol;
809 static struct internal_syment main_sym;
810 static union internal_auxent main_aux;
811 struct coff_symbol fcn_cs_saved;
812 static struct internal_syment fcn_sym_saved;
813 static union internal_auxent fcn_aux_saved;
815 /* A .file is open. */
816 int in_source_file = 0;
817 int next_file_symnum = -1;
818 /* Name of the current file. */
819 char *filestring = "";
821 int fcn_first_line = 0;
822 int fcn_last_line = 0;
823 int fcn_start_addr = 0;
824 long fcn_line_ptr = 0;
828 /* Work around a stdio bug in SunOS4.1.1 (this makes me nervous....
829 it's hard to know I've really worked around it. The fix should be
830 harmless, anyway). The symptom of the bug is that the first
831 fread (in read_one_sym), will (in my example) actually get data
832 from file offset 268, when the fseek was to 264 (and ftell shows
833 264). This causes all hell to break loose. I was unable to
834 reproduce this on a short test program which operated on the same
835 file, performing (I think) the same sequence of operations.
837 It stopped happening when I put in this (former) rewind().
839 FIXME: Find out if this has been reported to Sun, whether it has
840 been fixed in a later release, etc. */
842 bfd_seek (objfile->obfd, 0, 0);
844 /* Position to read the symbol table. */
845 val = bfd_seek (objfile->obfd, (long) symtab_offset, 0);
847 perror_with_name (objfile->name);
849 current_objfile = objfile;
850 nlist_bfd_global = objfile->obfd;
851 nlist_nsyms_global = nsyms;
852 last_source_file = NULL;
853 memset (opaque_type_chain, 0, sizeof opaque_type_chain);
855 if (type_vector) /* Get rid of previous one */
856 free ((PTR) type_vector);
857 type_vector_length = 160;
858 type_vector = (struct type **)
859 xmalloc (type_vector_length * sizeof (struct type *));
860 memset (type_vector, 0, type_vector_length * sizeof (struct type *));
862 coff_start_symtab ();
865 while (symnum < nsyms)
867 QUIT; /* Make this command interruptable. */
869 read_one_sym (cs, &main_sym, &main_aux);
871 if (cs->c_symnum == next_file_symnum && cs->c_sclass != C_FILE)
873 if (last_source_file)
874 coff_end_symtab (objfile);
876 coff_start_symtab ();
877 complete_symtab ("_globals_", 0, 0);
878 /* done with all files, everything from here on out is globals */
881 /* Special case for file with type declarations only, no text. */
882 if (!last_source_file && SDB_TYPE (cs->c_type)
883 && cs->c_secnum == N_DEBUG)
884 complete_symtab (filestring, 0, 0);
886 /* Typedefs should not be treated as symbol definitions. */
887 if (ISFCN (cs->c_type) && cs->c_sclass != C_TPDEF)
889 /* Record all functions -- external and static -- in minsyms. */
890 tmpaddr = cs->c_value + ANOFFSET (section_offsets, SECT_OFF_TEXT);
891 record_minimal_symbol (cs->c_name, tmpaddr, mst_text, objfile);
893 fcn_line_ptr = main_aux.x_sym.x_fcnary.x_fcn.x_lnnoptr;
894 fcn_start_addr = tmpaddr;
896 fcn_sym_saved = main_sym;
897 fcn_aux_saved = main_aux;
901 switch (cs->c_sclass)
910 complain (&bad_sclass_complaint, cs->c_name);
914 /* c_value field contains symnum of next .file entry in table
915 or symnum of first global after last .file. */
916 next_file_symnum = cs->c_value;
918 filestring = coff_getfilename (&main_aux);
922 /* Complete symbol table for last object file
923 containing debugging information. */
924 if (last_source_file)
926 coff_end_symtab (objfile);
927 coff_start_symtab ();
932 /* C_LABEL is used for labels and static functions. Including
933 it here allows gdb to see static functions when no debug
934 info is available. */
939 case C_THUMBSTATFUNC:
940 if (cs->c_name[0] == '.')
942 if (STREQ (cs->c_name, ".text")) {
943 /* FIXME: don't wire in ".text" as section name
945 /* Check for in_source_file deals with case of
946 a file with debugging symbols
947 followed by a later file with no symbols. */
949 complete_symtab (filestring,
950 cs->c_value + ANOFFSET (section_offsets, SECT_OFF_TEXT),
951 main_aux.x_scn.x_scnlen);
954 /* flush rest of '.' symbols */
957 else if (!SDB_TYPE (cs->c_type)
958 && cs->c_name[0] == 'L'
959 && (strncmp (cs->c_name, "LI%", 3) == 0
960 || strncmp (cs->c_name, "LF%", 3) == 0
961 || strncmp (cs->c_name,"LC%",3) == 0
962 || strncmp (cs->c_name,"LP%",3) == 0
963 || strncmp (cs->c_name,"LPB%",4) == 0
964 || strncmp (cs->c_name,"LBB%",4) == 0
965 || strncmp (cs->c_name,"LBE%",4) == 0
966 || strncmp (cs->c_name,"LPBX%",5) == 0))
967 /* At least on a 3b1, gcc generates swbeg and string labels
968 that look like this. Ignore them. */
970 /* fall in for static symbols that don't start with '.' */
975 /* Record it in the minimal symbols regardless of
976 SDB_TYPE. This parallels what we do for other debug
977 formats, and probably is needed to make
978 print_address_symbolic work right without the (now
979 gone) "set fast-symbolic-addr off" kludge. */
981 /* FIXME: should use mst_abs, and not relocate, if absolute. */
982 enum minimal_symbol_type ms_type;
985 if (cs->c_secnum == N_UNDEF)
987 /* This is a common symbol. See if the target
988 environment knows where it has been relocated to. */
990 if (target_lookup_symbol (cs->c_name, &reladdr))
992 /* Error in lookup; ignore symbol. */
996 /* The address has already been relocated; make sure that
997 objfile_relocate doesn't relocate it again. */
999 ms_type = cs->c_sclass == C_EXT
1000 || cs->c_sclass == C_THUMBEXT ?
1001 mst_bss : mst_file_bss;
1005 sec = cs_to_section (cs, objfile);
1006 tmpaddr = cs->c_value;
1007 if (cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXTFUNC
1008 || cs->c_sclass == C_THUMBEXT)
1009 tmpaddr += ANOFFSET (section_offsets, sec);
1014 case SECT_OFF_RODATA:
1016 cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXTFUNC
1017 || cs->c_sclass == C_THUMBEXT ?
1018 mst_text : mst_file_text;
1019 #ifdef SMASH_TEXT_ADDRESS
1020 if (tmpaddr & 1) /* FIXME: delete this line */
1021 SMASH_TEXT_ADDRESS (tmpaddr);
1026 cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXT ?
1027 mst_data : mst_file_data;
1031 cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXT ?
1032 mst_data : mst_file_data;
1035 ms_type = mst_unknown;
1040 if (cs->c_name[0] != '@' /* Skip tdesc symbols */)
1041 prim_record_minimal_symbol_and_info
1042 (cs->c_name, tmpaddr, ms_type, (char *)cs->c_sclass, sec,
1045 if (SDB_TYPE (cs->c_type))
1048 sym = process_coff_symbol
1049 (cs, &main_aux, section_offsets, objfile);
1050 SYMBOL_VALUE (sym) = tmpaddr;
1051 SYMBOL_SECTION (sym) = sec;
1057 if (STREQ (cs->c_name, ".bf"))
1059 within_function = 1;
1061 /* value contains address of first non-init type code */
1062 /* main_aux.x_sym.x_misc.x_lnsz.x_lnno
1063 contains line number of '{' } */
1064 if (cs->c_naux != 1)
1065 complain (&bf_no_aux_complaint, cs->c_symnum);
1066 fcn_first_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
1068 /* Might want to check that locals are 0 and
1069 context_stack_depth is zero, and complain if not. */
1072 new = push_context (depth, fcn_start_addr);
1073 fcn_cs_saved.c_name = getsymname (&fcn_sym_saved);
1075 process_coff_symbol (&fcn_cs_saved, &fcn_aux_saved,
1076 section_offsets, objfile);
1078 else if (STREQ (cs->c_name, ".ef"))
1080 /* the value of .ef is the address of epilogue code;
1081 not useful for gdb. */
1082 /* { main_aux.x_sym.x_misc.x_lnsz.x_lnno
1083 contains number of lines to '}' */
1085 if (context_stack_depth <= 0)
1086 { /* We attempted to pop an empty context stack */
1087 complain (&ef_stack_complaint, cs->c_symnum);
1088 within_function = 0;
1092 new = pop_context ();
1093 /* Stack must be empty now. */
1094 if (context_stack_depth > 0 || new == NULL)
1096 complain (&ef_complaint, cs->c_symnum);
1097 within_function = 0;
1100 if (cs->c_naux != 1)
1102 complain (&ef_no_aux_complaint, cs->c_symnum);
1103 fcn_last_line = 0x7FFFFFFF;
1107 fcn_last_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
1109 enter_linenos (fcn_line_ptr, fcn_first_line, fcn_last_line,
1112 finish_block (new->name, &local_symbols, new->old_blocks,
1114 #if defined (FUNCTION_EPILOGUE_SIZE)
1115 /* This macro should be defined only on
1117 fcn_aux_saved.x_sym.x_misc.x_fsize
1118 field is always zero.
1119 So use the .bf record information that
1120 points to the epilogue and add the size
1123 + FUNCTION_EPILOGUE_SIZE
1124 + ANOFFSET (section_offsets, SECT_OFF_TEXT),
1126 fcn_cs_saved.c_value
1127 + fcn_aux_saved.x_sym.x_misc.x_fsize
1128 + ANOFFSET (section_offsets, SECT_OFF_TEXT),
1132 within_function = 0;
1137 if (STREQ (cs->c_name, ".bb"))
1139 tmpaddr = cs->c_value;
1140 tmpaddr += ANOFFSET (section_offsets, SECT_OFF_TEXT);
1141 push_context (++depth, tmpaddr);
1143 else if (STREQ (cs->c_name, ".eb"))
1145 if (context_stack_depth <= 0)
1146 { /* We attempted to pop an empty context stack */
1147 complain (&eb_stack_complaint, cs->c_symnum);
1151 new = pop_context ();
1152 if (depth-- != new->depth)
1154 complain (&eb_complaint, symnum);
1157 if (local_symbols && context_stack_depth > 0)
1160 cs->c_value + ANOFFSET (section_offsets, SECT_OFF_TEXT);
1161 /* Make a block for the local symbols within. */
1162 finish_block (0, &local_symbols, new->old_blocks,
1163 new->start_addr, tmpaddr, objfile);
1165 /* Now pop locals of block just finished. */
1166 local_symbols = new->locals;
1171 process_coff_symbol (cs, &main_aux, section_offsets, objfile);
1176 if (last_source_file)
1177 coff_end_symtab (objfile);
1179 /* Patch up any opaque types (references to types that are not defined
1180 in the file where they are referenced, e.g. "struct foo *bar"). */
1181 ALL_OBJFILE_SYMTABS (objfile, s)
1182 patch_opaque_types (s);
1184 current_objfile = NULL;
1187 /* Routines for reading headers and symbols from executable. */
1189 /* Read the next symbol, swap it, and return it in both internal_syment
1190 form, and coff_symbol form. Also return its first auxent, if any,
1191 in internal_auxent form, and skip any other auxents. */
1194 read_one_sym (cs, sym, aux)
1195 register struct coff_symbol *cs;
1196 register struct internal_syment *sym;
1197 register union internal_auxent *aux;
1201 cs->c_symnum = symnum;
1202 bfd_read (temp_sym, local_symesz, 1, nlist_bfd_global);
1203 bfd_coff_swap_sym_in (symfile_bfd, temp_sym, (char *)sym);
1204 cs->c_naux = sym->n_numaux & 0xff;
1205 if (cs->c_naux >= 1)
1207 bfd_read (temp_aux, local_auxesz, 1, nlist_bfd_global);
1208 bfd_coff_swap_aux_in (symfile_bfd, temp_aux, sym->n_type, sym->n_sclass,
1209 0, cs->c_naux, (char *)aux);
1210 /* If more than one aux entry, read past it (only the first aux
1212 for (i = 1; i < cs->c_naux; i++)
1213 bfd_read (temp_aux, local_auxesz, 1, nlist_bfd_global);
1215 cs->c_name = getsymname (sym);
1216 cs->c_value = sym->n_value;
1217 cs->c_sclass = (sym->n_sclass & 0xff);
1218 cs->c_secnum = sym->n_scnum;
1219 cs->c_type = (unsigned) sym->n_type;
1220 if (!SDB_TYPE (cs->c_type))
1224 if (cs->c_sclass & 128)
1225 printf("thumb symbol %s, class 0x%x\n", cs->c_name, cs->c_sclass);
1228 symnum += 1 + cs->c_naux;
1230 /* The PE file format stores symbol values as offsets within the
1231 section, rather than as absolute addresses. We correct that
1232 here, if the symbol has an appropriate storage class. FIXME: We
1233 should use BFD to read the symbols, rather than duplicating the
1237 switch (cs->c_sclass)
1241 case C_THUMBEXTFUNC:
1246 case C_THUMBSTATFUNC:
1252 if (cs->c_secnum != 0)
1253 cs->c_value += cs_section_address (cs, symfile_bfd);
1259 /* Support for string table handling */
1261 static char *stringtab = NULL;
1264 init_stringtab (abfd, offset)
1270 unsigned char lengthbuf[4];
1274 /* If the file is stripped, the offset might be zero, indicating no
1275 string table. Just return with `stringtab' set to null. */
1279 if (bfd_seek (abfd, offset, 0) < 0)
1282 val = bfd_read ((char *)lengthbuf, sizeof lengthbuf, 1, abfd);
1283 length = bfd_h_get_32 (symfile_bfd, lengthbuf);
1285 /* If no string table is needed, then the file may end immediately
1286 after the symbols. Just return with `stringtab' set to null. */
1287 if (val != sizeof lengthbuf || length < sizeof lengthbuf)
1290 stringtab = (char *) xmalloc (length);
1291 /* This is in target format (probably not very useful, and not currently
1292 used), not host format. */
1293 memcpy (stringtab, lengthbuf, sizeof lengthbuf);
1294 if (length == sizeof length) /* Empty table -- just the count */
1297 val = bfd_read (stringtab + sizeof lengthbuf, length - sizeof lengthbuf, 1, abfd);
1298 if (val != length - sizeof lengthbuf || stringtab[length - 1] != '\0')
1313 getsymname (symbol_entry)
1314 struct internal_syment *symbol_entry;
1316 static char buffer[SYMNMLEN+1];
1319 if (symbol_entry->_n._n_n._n_zeroes == 0)
1321 /* FIXME: Probably should be detecting corrupt symbol files by
1322 seeing whether offset points to within the stringtab. */
1323 result = stringtab + symbol_entry->_n._n_n._n_offset;
1327 strncpy (buffer, symbol_entry->_n._n_name, SYMNMLEN);
1328 buffer[SYMNMLEN] = '\0';
1334 /* Extract the file name from the aux entry of a C_FILE symbol. Return
1335 only the last component of the name. Result is in static storage and
1336 is only good for temporary use. */
1339 coff_getfilename (aux_entry)
1340 union internal_auxent *aux_entry;
1342 static char buffer[BUFSIZ];
1343 register char *temp;
1346 if (aux_entry->x_file.x_n.x_zeroes == 0)
1347 strcpy (buffer, stringtab + aux_entry->x_file.x_n.x_offset);
1350 strncpy (buffer, aux_entry->x_file.x_fname, FILNMLEN);
1351 buffer[FILNMLEN] = '\0';
1355 /* FIXME: We should not be throwing away the information about what
1356 directory. It should go into dirname of the symtab, or some such
1358 if ((temp = strrchr (result, '/')) != NULL)
1363 /* Support for line number handling. */
1365 static char *linetab = NULL;
1366 static long linetab_offset;
1367 static unsigned long linetab_size;
1369 /* Read in all the line numbers for fast lookups later. Leave them in
1370 external (unswapped) format in memory; we'll swap them as we enter
1371 them into GDB's data structures. */
1374 init_lineno (abfd, offset, size)
1381 linetab_offset = offset;
1382 linetab_size = size;
1389 if (bfd_seek (abfd, offset, 0) < 0)
1392 /* Allocate the desired table, plus a sentinel */
1393 linetab = (char *) xmalloc (size + local_linesz);
1395 val = bfd_read (linetab, size, 1, abfd);
1399 /* Terminate it with an all-zero sentinel record */
1400 memset (linetab + size, 0, local_linesz);
1413 #if !defined (L_LNNO32)
1414 #define L_LNNO32(lp) ((lp)->l_lnno)
1418 enter_linenos (file_offset, first_line, last_line, section_offsets)
1420 register int first_line;
1421 register int last_line;
1422 struct section_offsets *section_offsets;
1424 register char *rawptr;
1425 struct internal_lineno lptr;
1429 if (file_offset < linetab_offset)
1431 complain (&lineno_complaint, file_offset);
1432 if (file_offset > linetab_size) /* Too big to be an offset? */
1434 file_offset += linetab_offset; /* Try reading at that linetab offset */
1437 rawptr = &linetab[file_offset - linetab_offset];
1439 /* skip first line entry for each function */
1440 rawptr += local_linesz;
1441 /* line numbers start at one for the first line of the function */
1445 bfd_coff_swap_lineno_in (symfile_bfd, rawptr, &lptr);
1446 rawptr += local_linesz;
1447 /* The next function, or the sentinel, will have L_LNNO32 zero; we exit. */
1448 if (L_LNNO32 (&lptr) && L_LNNO32 (&lptr) <= last_line)
1449 coff_record_line (first_line + L_LNNO32 (&lptr),
1451 + ANOFFSET (section_offsets, SECT_OFF_TEXT));
1458 patch_type (type, real_type)
1460 struct type *real_type;
1462 register struct type *target = TYPE_TARGET_TYPE (type);
1463 register struct type *real_target = TYPE_TARGET_TYPE (real_type);
1464 int field_size = TYPE_NFIELDS (real_target) * sizeof (struct field);
1466 TYPE_LENGTH (target) = TYPE_LENGTH (real_target);
1467 TYPE_NFIELDS (target) = TYPE_NFIELDS (real_target);
1468 TYPE_FIELDS (target) = (struct field *) TYPE_ALLOC (target, field_size);
1470 memcpy (TYPE_FIELDS (target), TYPE_FIELDS (real_target), field_size);
1472 if (TYPE_NAME (real_target))
1474 if (TYPE_NAME (target))
1475 free (TYPE_NAME (target));
1476 TYPE_NAME (target) = concat (TYPE_NAME (real_target), NULL);
1480 /* Patch up all appropriate typedef symbols in the opaque_type_chains
1481 so that they can be used to print out opaque data structures properly. */
1484 patch_opaque_types (s)
1487 register struct block *b;
1489 register struct symbol *real_sym;
1491 /* Go through the per-file symbols only */
1492 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
1493 for (i = BLOCK_NSYMS (b) - 1; i >= 0; i--)
1495 /* Find completed typedefs to use to fix opaque ones.
1496 Remove syms from the chain when their types are stored,
1497 but search the whole chain, as there may be several syms
1498 from different files with the same name. */
1499 real_sym = BLOCK_SYM (b, i);
1500 if (SYMBOL_CLASS (real_sym) == LOC_TYPEDEF &&
1501 SYMBOL_NAMESPACE (real_sym) == VAR_NAMESPACE &&
1502 TYPE_CODE (SYMBOL_TYPE (real_sym)) == TYPE_CODE_PTR &&
1503 TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (real_sym))) != 0)
1505 register char *name = SYMBOL_NAME (real_sym);
1506 register int hash = hashname (name);
1507 register struct symbol *sym, *prev;
1510 for (sym = opaque_type_chain[hash]; sym;)
1512 if (name[0] == SYMBOL_NAME (sym)[0] &&
1513 STREQ (name + 1, SYMBOL_NAME (sym) + 1))
1517 SYMBOL_VALUE_CHAIN (prev) = SYMBOL_VALUE_CHAIN (sym);
1521 opaque_type_chain[hash] = SYMBOL_VALUE_CHAIN (sym);
1524 patch_type (SYMBOL_TYPE (sym), SYMBOL_TYPE (real_sym));
1528 sym = SYMBOL_VALUE_CHAIN (prev);
1532 sym = opaque_type_chain[hash];
1538 sym = SYMBOL_VALUE_CHAIN (sym);
1545 static struct symbol *
1546 process_coff_symbol (cs, aux, section_offsets, objfile)
1547 register struct coff_symbol *cs;
1548 register union internal_auxent *aux;
1549 struct section_offsets *section_offsets;
1550 struct objfile *objfile;
1552 register struct symbol *sym
1553 = (struct symbol *) obstack_alloc (&objfile->symbol_obstack,
1554 sizeof (struct symbol));
1557 memset (sym, 0, sizeof (struct symbol));
1559 name = EXTERNAL_NAME (name, objfile->obfd);
1560 SYMBOL_NAME (sym) = obsavestring (name, strlen (name),
1561 &objfile->symbol_obstack);
1563 /* default assumptions */
1564 SYMBOL_VALUE (sym) = cs->c_value;
1565 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1566 SYMBOL_SECTION (sym) = cs_to_section (cs, objfile);
1568 if (ISFCN (cs->c_type))
1570 SYMBOL_VALUE (sym) += ANOFFSET (section_offsets, SECT_OFF_TEXT);
1572 lookup_function_type (decode_function_type (cs, cs->c_type, aux));
1574 SYMBOL_CLASS (sym) = LOC_BLOCK;
1575 if (cs->c_sclass == C_STAT || cs->c_sclass == C_THUMBSTAT
1576 || cs->c_sclass == C_THUMBSTATFUNC)
1577 add_symbol_to_list (sym, &file_symbols);
1578 else if (cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXT
1579 || cs->c_sclass == C_THUMBEXTFUNC)
1580 add_symbol_to_list (sym, &global_symbols);
1584 SYMBOL_TYPE (sym) = decode_type (cs, cs->c_type, aux);
1585 switch (cs->c_sclass)
1591 SYMBOL_CLASS (sym) = LOC_LOCAL;
1592 add_symbol_to_list (sym, &local_symbols);
1596 case C_THUMBEXTFUNC:
1598 SYMBOL_CLASS (sym) = LOC_STATIC;
1599 SYMBOL_VALUE_ADDRESS (sym) = (CORE_ADDR) cs->c_value;
1600 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (section_offsets, SECT_OFF_TEXT);
1601 add_symbol_to_list (sym, &global_symbols);
1605 case C_THUMBSTATFUNC:
1607 SYMBOL_CLASS (sym) = LOC_STATIC;
1608 SYMBOL_VALUE_ADDRESS (sym) = (CORE_ADDR) cs->c_value;
1609 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (section_offsets, SECT_OFF_TEXT);
1610 if (within_function) {
1611 /* Static symbol of local scope */
1612 add_symbol_to_list (sym, &local_symbols);
1615 /* Static symbol at top level of file */
1616 add_symbol_to_list (sym, &file_symbols);
1620 #ifdef C_GLBLREG /* AMD coff */
1624 SYMBOL_CLASS (sym) = LOC_REGISTER;
1625 SYMBOL_VALUE (sym) = SDB_REG_TO_REGNUM(cs->c_value);
1626 add_symbol_to_list (sym, &local_symbols);
1634 SYMBOL_CLASS (sym) = LOC_ARG;
1635 add_symbol_to_list (sym, &local_symbols);
1636 #if !defined (BELIEVE_PCC_PROMOTION)
1637 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
1639 /* If PCC says a parameter is a short or a char,
1640 aligned on an int boundary, realign it to the
1641 "little end" of the int. */
1642 struct type *temptype;
1643 temptype = lookup_fundamental_type (current_objfile,
1645 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) < TYPE_LENGTH (temptype)
1646 && TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_INT
1647 && 0 == SYMBOL_VALUE (sym) % TYPE_LENGTH (temptype))
1649 SYMBOL_VALUE (sym) +=
1650 TYPE_LENGTH (temptype)
1651 - TYPE_LENGTH (SYMBOL_TYPE (sym));
1658 SYMBOL_CLASS (sym) = LOC_REGPARM;
1659 SYMBOL_VALUE (sym) = SDB_REG_TO_REGNUM(cs->c_value);
1660 add_symbol_to_list (sym, &local_symbols);
1661 #if !defined (BELIEVE_PCC_PROMOTION)
1662 /* FIXME: This should retain the current type, since it's just
1663 a register value. gnu@adobe, 26Feb93 */
1665 /* If PCC says a parameter is a short or a char,
1666 it is really an int. */
1667 struct type *temptype;
1669 lookup_fundamental_type (current_objfile, FT_INTEGER);
1670 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) < TYPE_LENGTH (temptype)
1671 && TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_INT)
1674 (TYPE_UNSIGNED (SYMBOL_TYPE (sym))
1675 ? lookup_fundamental_type (current_objfile,
1676 FT_UNSIGNED_INTEGER)
1684 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
1685 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1687 /* If type has no name, give it one */
1688 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
1690 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_PTR
1691 || TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_FUNC)
1693 /* If we are giving a name to a type such as "pointer to
1694 foo" or "function returning foo", we better not set
1695 the TYPE_NAME. If the program contains "typedef char
1696 *caddr_t;", we don't want all variables of type char
1697 * to print as caddr_t. This is not just a
1698 consequence of GDB's type management; CC and GCC (at
1699 least through version 2.4) both output variables of
1700 either type char * or caddr_t with the type
1701 refering to the C_TPDEF symbol for caddr_t. If a future
1702 compiler cleans this up it GDB is not ready for it
1703 yet, but if it becomes ready we somehow need to
1704 disable this check (without breaking the PCC/GCC2.4
1709 Fortunately, this check seems not to be necessary
1710 for anything except pointers or functions. */
1714 TYPE_NAME (SYMBOL_TYPE (sym)) =
1715 concat (SYMBOL_NAME (sym), NULL);
1718 /* Ignore vendor section for Harris CX/UX targets. */
1719 else if (cs->c_name[0] == '$')
1721 #endif /* CXUX_TARGET */
1723 /* Keep track of any type which points to empty structured type,
1724 so it can be filled from a definition from another file. A
1725 simple forward reference (TYPE_CODE_UNDEF) is not an
1726 empty structured type, though; the forward references
1727 work themselves out via the magic of coff_lookup_type. */
1728 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_PTR &&
1729 TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym))) == 0 &&
1730 TYPE_CODE (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym))) !=
1733 register int i = hashname (SYMBOL_NAME (sym));
1735 SYMBOL_VALUE_CHAIN (sym) = opaque_type_chain[i];
1736 opaque_type_chain[i] = sym;
1738 add_symbol_to_list (sym, &file_symbols);
1744 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
1745 SYMBOL_NAMESPACE (sym) = STRUCT_NAMESPACE;
1747 /* Some compilers try to be helpful by inventing "fake"
1748 names for anonymous enums, structures, and unions, like
1749 "~0fake" or ".0fake". Thanks, but no thanks... */
1750 if (TYPE_TAG_NAME (SYMBOL_TYPE (sym)) == 0)
1751 if (SYMBOL_NAME(sym) != NULL
1752 && *SYMBOL_NAME(sym) != '~'
1753 && *SYMBOL_NAME(sym) != '.')
1754 TYPE_TAG_NAME (SYMBOL_TYPE (sym)) =
1755 concat (SYMBOL_NAME (sym), NULL);
1757 add_symbol_to_list (sym, &file_symbols);
1767 /* Decode a coff type specifier; return the type that is meant. */
1769 static struct type *
1770 decode_type (cs, c_type, aux)
1771 register struct coff_symbol *cs;
1772 unsigned int c_type;
1773 register union internal_auxent *aux;
1775 register struct type *type = 0;
1776 unsigned int new_c_type;
1778 if (c_type & ~N_BTMASK)
1780 new_c_type = DECREF (c_type);
1783 type = decode_type (cs, new_c_type, aux);
1784 type = lookup_pointer_type (type);
1786 else if (ISFCN (c_type))
1788 type = decode_type (cs, new_c_type, aux);
1789 type = lookup_function_type (type);
1791 else if (ISARY (c_type))
1794 register unsigned short *dim;
1795 struct type *base_type, *index_type, *range_type;
1797 /* Define an array type. */
1798 /* auxent refers to array, not base type */
1799 if (aux->x_sym.x_tagndx.l == 0)
1802 /* shift the indices down */
1803 dim = &aux->x_sym.x_fcnary.x_ary.x_dimen[0];
1806 for (i = 0; *dim && i < DIMNUM - 1; i++, dim++)
1810 base_type = decode_type (cs, new_c_type, aux);
1811 index_type = lookup_fundamental_type (current_objfile, FT_INTEGER);
1813 create_range_type ((struct type *) NULL, index_type, 0, n - 1);
1815 create_array_type ((struct type *) NULL, base_type, range_type);
1820 /* Reference to existing type. This only occurs with the
1821 struct, union, and enum types. EPI a29k coff
1822 fakes us out by producing aux entries with a nonzero
1823 x_tagndx for definitions of structs, unions, and enums, so we
1824 have to check the c_sclass field. SCO 3.2v4 cc gets confused
1825 with pointers to pointers to defined structs, and generates
1826 negative x_tagndx fields. */
1827 if (cs->c_naux > 0 && aux->x_sym.x_tagndx.l != 0)
1829 if (cs->c_sclass != C_STRTAG
1830 && cs->c_sclass != C_UNTAG
1831 && cs->c_sclass != C_ENTAG
1832 && aux->x_sym.x_tagndx.l >= 0)
1834 type = coff_alloc_type (aux->x_sym.x_tagndx.l);
1839 complain (&tagndx_bad_complaint, cs->c_name);
1840 /* And fall through to decode_base_type... */
1844 return decode_base_type (cs, BTYPE (c_type), aux);
1847 /* Decode a coff type specifier for function definition;
1848 return the type that the function returns. */
1850 static struct type *
1851 decode_function_type (cs, c_type, aux)
1852 register struct coff_symbol *cs;
1853 unsigned int c_type;
1854 register union internal_auxent *aux;
1856 if (aux->x_sym.x_tagndx.l == 0)
1857 cs->c_naux = 0; /* auxent refers to function, not base type */
1859 return decode_type (cs, DECREF (c_type), aux);
1864 static struct type *
1865 decode_base_type (cs, c_type, aux)
1866 register struct coff_symbol *cs;
1867 unsigned int c_type;
1868 register union internal_auxent *aux;
1875 /* shows up with "void (*foo)();" structure members */
1876 return lookup_fundamental_type (current_objfile, FT_VOID);
1879 /* DGUX actually defines both T_ARG and T_VOID to the same value. */
1882 /* Shows up in DGUX, I think. Not sure where. */
1883 return lookup_fundamental_type (current_objfile, FT_VOID); /* shouldn't show up here */
1889 /* Intel 960 COFF has this symbol and meaning. */
1890 return lookup_fundamental_type (current_objfile, FT_VOID);
1894 return lookup_fundamental_type (current_objfile, FT_CHAR);
1897 return lookup_fundamental_type (current_objfile, FT_SHORT);
1900 return lookup_fundamental_type (current_objfile, FT_INTEGER);
1903 return lookup_fundamental_type (current_objfile, FT_LONG);
1906 return lookup_fundamental_type (current_objfile, FT_FLOAT);
1909 return lookup_fundamental_type (current_objfile, FT_DBL_PREC_FLOAT);
1912 return lookup_fundamental_type (current_objfile, FT_EXT_PREC_FLOAT);
1915 if (cs->c_naux != 1)
1917 /* anonymous structure type */
1918 type = coff_alloc_type (cs->c_symnum);
1919 TYPE_CODE (type) = TYPE_CODE_STRUCT;
1920 TYPE_NAME (type) = NULL;
1921 /* This used to set the tag to "<opaque>". But I think setting it
1922 to NULL is right, and the printing code can print it as
1924 TYPE_TAG_NAME (type) = NULL;
1925 INIT_CPLUS_SPECIFIC(type);
1926 TYPE_LENGTH (type) = 0;
1927 TYPE_FIELDS (type) = 0;
1928 TYPE_NFIELDS (type) = 0;
1932 type = coff_read_struct_type (cs->c_symnum,
1933 aux->x_sym.x_misc.x_lnsz.x_size,
1934 aux->x_sym.x_fcnary.x_fcn.x_endndx.l);
1939 if (cs->c_naux != 1)
1941 /* anonymous union type */
1942 type = coff_alloc_type (cs->c_symnum);
1943 TYPE_NAME (type) = NULL;
1944 /* This used to set the tag to "<opaque>". But I think setting it
1945 to NULL is right, and the printing code can print it as
1947 TYPE_TAG_NAME (type) = NULL;
1948 INIT_CPLUS_SPECIFIC(type);
1949 TYPE_LENGTH (type) = 0;
1950 TYPE_FIELDS (type) = 0;
1951 TYPE_NFIELDS (type) = 0;
1955 type = coff_read_struct_type (cs->c_symnum,
1956 aux->x_sym.x_misc.x_lnsz.x_size,
1957 aux->x_sym.x_fcnary.x_fcn.x_endndx.l);
1959 TYPE_CODE (type) = TYPE_CODE_UNION;
1963 if (cs->c_naux != 1)
1965 /* anonymous enum type */
1966 type = coff_alloc_type (cs->c_symnum);
1967 TYPE_CODE (type) = TYPE_CODE_ENUM;
1968 TYPE_NAME (type) = NULL;
1969 /* This used to set the tag to "<opaque>". But I think setting it
1970 to NULL is right, and the printing code can print it as
1972 TYPE_TAG_NAME (type) = NULL;
1973 TYPE_LENGTH (type) = 0;
1974 TYPE_FIELDS (type) = 0;
1975 TYPE_NFIELDS(type) = 0;
1979 type = coff_read_enum_type (cs->c_symnum,
1980 aux->x_sym.x_misc.x_lnsz.x_size,
1981 aux->x_sym.x_fcnary.x_fcn.x_endndx.l);
1986 /* shouldn't show up here */
1990 return lookup_fundamental_type (current_objfile, FT_UNSIGNED_CHAR);
1993 return lookup_fundamental_type (current_objfile, FT_UNSIGNED_SHORT);
1996 return lookup_fundamental_type (current_objfile, FT_UNSIGNED_INTEGER);
1999 return lookup_fundamental_type (current_objfile, FT_UNSIGNED_LONG);
2001 complain (&unexpected_type_complaint, cs->c_name);
2002 return lookup_fundamental_type (current_objfile, FT_VOID);
2005 /* This page contains subroutines of read_type. */
2007 /* Read the description of a structure (or union type) and return an
2008 object describing the type. */
2010 static struct type *
2011 coff_read_struct_type (index, length, lastsym)
2018 struct nextfield *next;
2022 register struct type *type;
2023 register struct nextfield *list = 0;
2024 struct nextfield *new;
2028 struct coff_symbol member_sym;
2029 register struct coff_symbol *ms = &member_sym;
2030 struct internal_syment sub_sym;
2031 union internal_auxent sub_aux;
2034 type = coff_alloc_type (index);
2035 TYPE_CODE (type) = TYPE_CODE_STRUCT;
2036 INIT_CPLUS_SPECIFIC(type);
2037 TYPE_LENGTH (type) = length;
2039 while (!done && symnum < lastsym && symnum < nlist_nsyms_global)
2041 read_one_sym (ms, &sub_sym, &sub_aux);
2043 name = EXTERNAL_NAME (name, current_objfile->obfd);
2045 switch (ms->c_sclass)
2050 /* Get space to record the next field's data. */
2051 new = (struct nextfield *) alloca (sizeof (struct nextfield));
2055 /* Save the data. */
2059 ¤t_objfile->symbol_obstack);
2060 FIELD_TYPE (list->field) = decode_type (ms, ms->c_type, &sub_aux);
2061 FIELD_BITPOS (list->field) = 8 * ms->c_value;
2062 FIELD_BITSIZE (list->field) = 0;
2068 /* Get space to record the next field's data. */
2069 new = (struct nextfield *) alloca (sizeof (struct nextfield));
2073 /* Save the data. */
2077 ¤t_objfile->symbol_obstack);
2078 FIELD_TYPE (list->field) = decode_type (ms, ms->c_type, &sub_aux);
2079 FIELD_BITPOS (list->field) = ms->c_value;
2080 FIELD_BITSIZE (list->field) = sub_aux.x_sym.x_misc.x_lnsz.x_size;
2089 /* Now create the vector of fields, and record how big it is. */
2091 TYPE_NFIELDS (type) = nfields;
2092 TYPE_FIELDS (type) = (struct field *)
2093 TYPE_ALLOC (type, sizeof (struct field) * nfields);
2095 /* Copy the saved-up fields into the field vector. */
2097 for (n = nfields; list; list = list->next)
2098 TYPE_FIELD (type, --n) = list->field;
2103 /* Read a definition of an enumeration type,
2104 and create and return a suitable type object.
2105 Also defines the symbols that represent the values of the type. */
2108 static struct type *
2109 coff_read_enum_type (index, length, lastsym)
2114 register struct symbol *sym;
2115 register struct type *type;
2118 struct pending **symlist;
2119 struct coff_symbol member_sym;
2120 register struct coff_symbol *ms = &member_sym;
2121 struct internal_syment sub_sym;
2122 union internal_auxent sub_aux;
2123 struct pending *osyms, *syms;
2127 int unsigned_enum = 1;
2129 type = coff_alloc_type (index);
2130 if (within_function)
2131 symlist = &local_symbols;
2133 symlist = &file_symbols;
2135 o_nsyms = osyms ? osyms->nsyms : 0;
2137 while (!done && symnum < lastsym && symnum < nlist_nsyms_global)
2139 read_one_sym (ms, &sub_sym, &sub_aux);
2141 name = EXTERNAL_NAME (name, current_objfile->obfd);
2143 switch (ms->c_sclass)
2146 sym = (struct symbol *) obstack_alloc
2147 (¤t_objfile->symbol_obstack,
2148 sizeof (struct symbol));
2149 memset (sym, 0, sizeof (struct symbol));
2152 obsavestring (name, strlen (name),
2153 ¤t_objfile->symbol_obstack);
2154 SYMBOL_CLASS (sym) = LOC_CONST;
2155 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
2156 SYMBOL_VALUE (sym) = ms->c_value;
2157 add_symbol_to_list (sym, symlist);
2162 /* Sometimes the linker (on 386/ix 2.0.2 at least) screws
2163 up the count of how many symbols to read. So stop
2170 /* Now fill in the fields of the type-structure. */
2173 TYPE_LENGTH (type) = length;
2175 TYPE_LENGTH (type) = TARGET_INT_BIT / TARGET_CHAR_BIT; /* Assume ints */
2176 TYPE_CODE (type) = TYPE_CODE_ENUM;
2177 TYPE_NFIELDS (type) = nsyms;
2178 TYPE_FIELDS (type) = (struct field *)
2179 TYPE_ALLOC (type, sizeof (struct field) * nsyms);
2181 /* Find the symbols for the values and put them into the type.
2182 The symbols can be found in the symlist that we put them on
2183 to cause them to be defined. osyms contains the old value
2184 of that symlist; everything up to there was defined by us. */
2185 /* Note that we preserve the order of the enum constants, so
2186 that in something like "enum {FOO, LAST_THING=FOO}" we print
2187 FOO, not LAST_THING. */
2189 for (syms = *symlist, n = 0; syms; syms = syms->next)
2195 for (; j < syms->nsyms; j++,n++)
2197 struct symbol *xsym = syms->symbol[j];
2198 SYMBOL_TYPE (xsym) = type;
2199 TYPE_FIELD_NAME (type, n) = SYMBOL_NAME (xsym);
2200 TYPE_FIELD_BITPOS (type, n) = SYMBOL_VALUE (xsym);
2201 if (SYMBOL_VALUE (xsym) < 0)
2203 TYPE_FIELD_BITSIZE (type, n) = 0;
2210 TYPE_FLAGS (type) |= TYPE_FLAG_UNSIGNED;
2215 /* Register our ability to parse symbols for coff BFD files. */
2217 static struct sym_fns coff_sym_fns =
2219 bfd_target_coff_flavour,
2220 coff_new_init, /* sym_new_init: init anything gbl to entire symtab */
2221 coff_symfile_init, /* sym_init: read initial info, setup for sym_read() */
2222 coff_symfile_read, /* sym_read: read a symbol file into symtab */
2223 coff_symfile_finish, /* sym_finish: finished with file, cleanup */
2224 default_symfile_offsets,
2225 /* sym_offsets: xlate external to internal form */
2226 NULL /* next: pointer to next struct sym_fns */
2230 _initialize_coffread ()
2232 add_symtab_fns (&coff_sym_fns);