1 /* Read coff symbol tables and convert to internal format, for GDB.
2 Copyright 1987, 88, 89, 90, 91, 92, 93, 94, 96, 97, 1998
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., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
27 #include "breakpoint.h"
32 #include "gdb_string.h"
35 #include "coff/internal.h" /* Internal format of COFF symbols in BFD */
36 #include "libcoff.h" /* FIXME secret internal data from BFD */
41 #include "gdb-stabs.h"
42 #include "stabsread.h"
43 #include "complaints.h"
46 extern void _initialize_coffread PARAMS ((void));
48 struct coff_symfile_info
50 file_ptr min_lineno_offset; /* Where in file lowest line#s are */
51 file_ptr max_lineno_offset; /* 1+last byte of line#s in file */
53 CORE_ADDR textaddr; /* Addr of .text section. */
54 unsigned int textsize; /* Size of .text section. */
55 struct stab_section_list *stabsects; /* .stab sections. */
56 asection *stabstrsect; /* Section pointer for .stab section */
60 /* Translate an external name string into a user-visible name. */
61 #define EXTERNAL_NAME(string, abfd) \
62 (string[0] == bfd_get_symbol_leading_char(abfd)? string+1: string)
64 /* To be an sdb debug type, type must have at least a basic or primary
65 derived type. Using this rather than checking against T_NULL is
66 said to prevent core dumps if we try to operate on Michael Bloom
69 #define SDB_TYPE(type) (BTYPE(type) | (type & N_TMASK))
71 /* Convert from an sdb register number to an internal gdb register number.
72 This should be defined in tm.h, if REGISTER_NAMES is not set up
73 to map one to one onto the sdb register numbers. */
75 #ifndef SDB_REG_TO_REGNUM
76 #define SDB_REG_TO_REGNUM(value) (value)
79 /* Core address of start and end of text of current source file.
80 This comes from a ".text" symbol where x_nlinno > 0. */
82 static CORE_ADDR current_source_start_addr;
83 static CORE_ADDR current_source_end_addr;
85 /* The addresses of the symbol table stream and number of symbols
86 of the object file we are reading (as copied into core). */
88 static bfd *nlist_bfd_global;
89 static int nlist_nsyms_global;
92 /* Pointers to scratch storage, used for reading raw symbols and auxents. */
94 static char *temp_sym;
95 static char *temp_aux;
97 /* Local variables that hold the shift and mask values for the
98 COFF file that we are currently reading. These come back to us
99 from BFD, and are referenced by their macro names, as well as
100 internally to the BTYPE, ISPTR, ISFCN, ISARY, ISTAG, and DECREF
101 macros from include/coff/internal.h . */
103 static unsigned local_n_btmask;
104 static unsigned local_n_btshft;
105 static unsigned local_n_tmask;
106 static unsigned local_n_tshift;
108 #define N_BTMASK local_n_btmask
109 #define N_BTSHFT local_n_btshft
110 #define N_TMASK local_n_tmask
111 #define N_TSHIFT local_n_tshift
113 /* Local variables that hold the sizes in the file of various COFF structures.
114 (We only need to know this to read them from the file -- BFD will then
115 translate the data in them, into `internal_xxx' structs in the right
116 byte order, alignment, etc.) */
118 static unsigned local_linesz;
119 static unsigned local_symesz;
120 static unsigned local_auxesz;
122 /* This is set if this is a PE format file. */
126 /* Chain of typedefs of pointers to empty struct/union types.
127 They are chained thru the SYMBOL_VALUE_CHAIN. */
129 static struct symbol *opaque_type_chain[HASHSIZE];
131 /* Complaints about various problems in the file being read */
133 struct complaint ef_complaint =
134 {"Unmatched .ef symbol(s) ignored starting at symnum %d", 0, 0};
136 struct complaint ef_stack_complaint =
137 {"`.ef' symbol without matching `.bf' symbol ignored starting at symnum %d", 0, 0};
139 struct complaint eb_stack_complaint =
140 {"`.eb' symbol without matching `.bb' symbol ignored starting at symnum %d", 0, 0};
142 struct complaint bf_no_aux_complaint =
143 {"`.bf' symbol %d has no aux entry", 0, 0};
145 struct complaint ef_no_aux_complaint =
146 {"`.ef' symbol %d has no aux entry", 0, 0};
148 struct complaint lineno_complaint =
149 {"Line number pointer %d lower than start of line numbers", 0, 0};
151 struct complaint unexpected_type_complaint =
152 {"Unexpected type for symbol %s", 0, 0};
154 struct complaint bad_sclass_complaint =
155 {"Bad n_sclass for symbol %s", 0, 0};
157 struct complaint misordered_blocks_complaint =
158 {"Blocks out of order at address %x", 0, 0};
160 struct complaint tagndx_bad_complaint =
161 {"Symbol table entry for %s has bad tagndx value", 0, 0};
163 struct complaint eb_complaint =
164 {"Mismatched .eb symbol ignored starting at symnum %d", 0, 0};
166 /* Simplified internal version of coff symbol table information */
171 int c_symnum; /* symbol number of this entry */
172 int c_naux; /* 0 if syment only, 1 if syment + auxent, etc */
179 static struct type *coff_read_struct_type PARAMS ((int, int, int));
181 static struct type *decode_base_type PARAMS ((struct coff_symbol *,
183 union internal_auxent *));
185 static struct type *decode_type PARAMS ((struct coff_symbol *, unsigned int,
186 union internal_auxent *));
188 static struct type *decode_function_type PARAMS ((struct coff_symbol *,
190 union internal_auxent *));
192 static struct type *coff_read_enum_type PARAMS ((int, int, int));
194 static struct symbol *process_coff_symbol PARAMS ((struct coff_symbol *,
195 union internal_auxent *,
198 static void patch_opaque_types PARAMS ((struct symtab *));
200 static void patch_type PARAMS ((struct type *, struct type *));
202 static void enter_linenos PARAMS ((long, int, int, struct section_offsets *));
204 static void free_linetab PARAMS ((void));
206 static int init_lineno PARAMS ((bfd *, long, int));
208 static char *getsymname PARAMS ((struct internal_syment *));
210 static char *coff_getfilename PARAMS ((union internal_auxent *));
212 static void free_stringtab PARAMS ((void));
214 static int init_stringtab PARAMS ((bfd *, long));
216 static void read_one_sym PARAMS ((struct coff_symbol *,
217 struct internal_syment *,
218 union internal_auxent *));
220 static void coff_symtab_read PARAMS ((long, int, struct objfile *));
222 static void find_linenos PARAMS ((bfd *, sec_ptr, PTR));
224 static void coff_symfile_init PARAMS ((struct objfile *));
226 static void coff_new_init PARAMS ((struct objfile *));
228 static void coff_symfile_read PARAMS ((struct objfile *, int));
230 static void coff_symfile_finish PARAMS ((struct objfile *));
232 static void record_minimal_symbol PARAMS ((char *, CORE_ADDR,
233 enum minimal_symbol_type,
236 static void coff_end_symtab PARAMS ((struct objfile *));
238 static void complete_symtab PARAMS ((char *, CORE_ADDR, unsigned int));
240 static void coff_start_symtab PARAMS ((char *));
242 static struct type *coff_alloc_type PARAMS ((int));
244 static struct type **coff_lookup_type PARAMS ((int));
246 static void coff_locate_sections PARAMS ((bfd *, asection *, PTR));
248 /* We are called once per section from coff_symfile_read. We
249 need to examine each section we are passed, check to see
250 if it is something we are interested in processing, and
251 if so, stash away some access information for the section.
253 FIXME: The section names should not be hardwired strings (what
254 should they be? I don't think most object file formats have enough
255 section flags to specify what kind of debug section it is
259 coff_locate_sections (abfd, sectp, csip)
264 register struct coff_symfile_info *csi;
267 csi = (struct coff_symfile_info *) csip;
268 name = bfd_get_section_name (abfd, sectp);
269 if (STREQ (name, ".text"))
271 csi->textaddr = bfd_section_vma (abfd, sectp);
272 csi->textsize += bfd_section_size (abfd, sectp);
274 else if (strncmp (name, ".text", sizeof ".text" - 1) == 0)
276 csi->textsize += bfd_section_size (abfd, sectp);
278 else if (STREQ (name, ".stabstr"))
280 csi->stabstrsect = sectp;
282 else if (strncmp (name, ".stab", sizeof ".stab" - 1) == 0)
286 /* We can have multiple .stab sections if linked with
288 for (s = name + sizeof ".stab" - 1; *s != '\0'; s++)
293 struct stab_section_list *n, **pn;
295 n = ((struct stab_section_list *)
296 xmalloc (sizeof (struct stab_section_list)));
299 for (pn = &csi->stabsects; *pn != NULL; pn = &(*pn)->next)
303 /* This will be run after coffstab_build_psymtabs is called
304 in coff_symfile_read, at which point we no longer need
306 make_cleanup (free, n);
311 /* Return the section_offsets* that CS points to. */
312 static int cs_to_section PARAMS ((struct coff_symbol *, struct objfile *));
314 struct find_targ_sec_arg
320 static void find_targ_sec PARAMS ((bfd *, asection *, void *));
323 find_targ_sec (abfd, sect, obj)
328 struct find_targ_sec_arg *args = (struct find_targ_sec_arg *) obj;
329 if (sect->target_index == args->targ_index)
330 *args->resultp = sect;
333 /* Return the section number (SECT_OFF_*) that CS points to. */
335 cs_to_section (cs, objfile)
336 struct coff_symbol *cs;
337 struct objfile *objfile;
339 asection *sect = NULL;
340 struct find_targ_sec_arg args;
341 int off = SECT_OFF_TEXT;
343 args.targ_index = cs->c_secnum;
344 args.resultp = §
345 bfd_map_over_sections (objfile->obfd, find_targ_sec, &args);
348 /* This is the section. Figure out what SECT_OFF_* code it is. */
349 if (bfd_get_section_flags (abfd, sect) & SEC_CODE)
351 else if (bfd_get_section_flags (abfd, sect) & SEC_LOAD)
359 /* Return the address of the section of a COFF symbol. */
361 static CORE_ADDR cs_section_address PARAMS ((struct coff_symbol *, bfd *));
364 cs_section_address (cs, abfd)
365 struct coff_symbol *cs;
368 asection *sect = NULL;
369 struct find_targ_sec_arg args;
372 args.targ_index = cs->c_secnum;
373 args.resultp = §
374 bfd_map_over_sections (abfd, find_targ_sec, &args);
376 addr = bfd_get_section_vma (objfile->obfd, sect);
380 /* Look up a coff type-number index. Return the address of the slot
381 where the type for that index is stored.
382 The type-number is in INDEX.
384 This can be used for finding the type associated with that index
385 or for associating a new type with the index. */
387 static struct type **
388 coff_lookup_type (index)
391 if (index >= type_vector_length)
393 int old_vector_length = type_vector_length;
395 type_vector_length *= 2;
396 if (index /* is still */ >= type_vector_length)
397 type_vector_length = index * 2;
399 type_vector = (struct type **)
400 xrealloc ((char *) type_vector,
401 type_vector_length * sizeof (struct type *));
402 memset (&type_vector[old_vector_length], 0,
403 (type_vector_length - old_vector_length) * sizeof (struct type *));
405 return &type_vector[index];
408 /* Make sure there is a type allocated for type number index
409 and return the type object.
410 This can create an empty (zeroed) type object. */
413 coff_alloc_type (index)
416 register struct type **type_addr = coff_lookup_type (index);
417 register struct type *type = *type_addr;
419 /* If we are referring to a type not known at all yet,
420 allocate an empty type for it.
421 We will fill it in later if we find out how. */
424 type = alloc_type (current_objfile);
430 /* Start a new symtab for a new source file.
431 This is called when a COFF ".file" symbol is seen;
432 it indicates the start of data for one original source file. */
435 coff_start_symtab (name)
439 /* We fill in the filename later. start_symtab puts
440 this pointer into last_source_file and we put it in
441 subfiles->name, which end_symtab frees; that's why
442 it must be malloc'd. */
443 savestring (name, strlen (name)),
444 /* We never know the directory name for COFF. */
446 /* The start address is irrelevant, since we set
447 last_source_start_addr in coff_end_symtab. */
449 record_debugformat ("COFF");
452 /* Save the vital information from when starting to read a file,
453 for use when closing off the current file.
454 NAME is the file name the symbols came from, START_ADDR is the first
455 text address for the file, and SIZE is the number of bytes of text. */
458 complete_symtab (name, start_addr, size)
460 CORE_ADDR start_addr;
463 if (last_source_file != NULL)
464 free (last_source_file);
465 last_source_file = savestring (name, strlen (name));
466 current_source_start_addr = start_addr;
467 current_source_end_addr = start_addr + size;
469 if (current_objfile->ei.entry_point >= current_source_start_addr &&
470 current_objfile->ei.entry_point < current_source_end_addr)
472 current_objfile->ei.entry_file_lowpc = current_source_start_addr;
473 current_objfile->ei.entry_file_highpc = current_source_end_addr;
477 /* Finish the symbol definitions for one main source file,
478 close off all the lexical contexts for that file
479 (creating struct block's for them), then make the
480 struct symtab for that file and put it in the list of all such. */
483 coff_end_symtab (objfile)
484 struct objfile *objfile;
486 struct symtab *symtab;
488 last_source_start_addr = current_source_start_addr;
490 symtab = end_symtab (current_source_end_addr, objfile, 0);
493 free_named_symtabs (symtab->filename);
495 /* Reinitialize for beginning of new file. */
496 last_source_file = NULL;
500 record_minimal_symbol (name, address, type, objfile)
503 enum minimal_symbol_type type;
504 struct objfile *objfile;
506 /* We don't want TDESC entry points in the minimal symbol table */
510 prim_record_minimal_symbol (name, address, type, objfile);
513 /* coff_symfile_init ()
514 is the coff-specific initialization routine for reading symbols.
515 It is passed a struct objfile which contains, among other things,
516 the BFD for the file whose symbols are being read, and a slot for
517 a pointer to "private data" which we fill with cookies and other
518 treats for coff_symfile_read ().
520 We will only be called if this is a COFF or COFF-like file.
521 BFD handles figuring out the format of the file, and code in symtab.c
522 uses BFD's determination to vector to us.
524 The ultimate result is a new symtab (or, FIXME, eventually a psymtab). */
527 coff_symfile_init (objfile)
528 struct objfile *objfile;
530 /* Allocate struct to keep track of stab reading. */
531 objfile->sym_stab_info = (struct dbx_symfile_info *)
532 xmmalloc (objfile->md, sizeof (struct dbx_symfile_info));
534 memset ((PTR) objfile->sym_stab_info, 0, sizeof (struct dbx_symfile_info));
536 /* Allocate struct to keep track of the symfile */
537 objfile->sym_private = xmmalloc (objfile->md,
538 sizeof (struct coff_symfile_info));
540 memset (objfile->sym_private, 0, sizeof (struct coff_symfile_info));
542 /* COFF objects may be reordered, so set OBJF_REORDERED. If we
543 find this causes a significant slowdown in gdb then we could
544 set it in the debug symbol readers only when necessary. */
545 objfile->flags |= OBJF_REORDERED;
547 init_entry_point_info (objfile);
550 /* This function is called for every section; it finds the outer limits
551 of the line table (minimum and maximum file offset) so that the
552 mainline code can read the whole thing for efficiency. */
556 find_linenos (abfd, asect, vpinfo)
561 struct coff_symfile_info *info;
563 file_ptr offset, maxoff;
565 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
566 count = asect->lineno_count;
571 size = count * local_linesz;
573 info = (struct coff_symfile_info *) vpinfo;
574 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
575 offset = asect->line_filepos;
578 if (offset < info->min_lineno_offset || info->min_lineno_offset == 0)
579 info->min_lineno_offset = offset;
581 maxoff = offset + size;
582 if (maxoff > info->max_lineno_offset)
583 info->max_lineno_offset = maxoff;
587 /* The BFD for this file -- only good while we're actively reading
588 symbols into a psymtab or a symtab. */
590 static bfd *symfile_bfd;
592 /* Read a symbol file, after initialization by coff_symfile_init. */
596 coff_symfile_read (objfile, mainline)
597 struct objfile *objfile;
600 struct coff_symfile_info *info;
601 struct dbx_symfile_info *dbxinfo;
602 bfd *abfd = objfile->obfd;
603 coff_data_type *cdata = coff_data (abfd);
604 char *name = bfd_get_filename (abfd);
608 int stringtab_offset;
609 struct cleanup *back_to;
612 info = (struct coff_symfile_info *) objfile->sym_private;
613 dbxinfo = objfile->sym_stab_info;
614 symfile_bfd = abfd; /* Kludge for swap routines */
616 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
617 num_symbols = bfd_get_symcount (abfd); /* How many syms */
618 symtab_offset = cdata->sym_filepos; /* Symbol table file offset */
619 stringtab_offset = symtab_offset + /* String table file offset */
620 num_symbols * cdata->local_symesz;
622 /* Set a few file-statics that give us specific information about
623 the particular COFF file format we're reading. */
624 local_n_btmask = cdata->local_n_btmask;
625 local_n_btshft = cdata->local_n_btshft;
626 local_n_tmask = cdata->local_n_tmask;
627 local_n_tshift = cdata->local_n_tshift;
628 local_linesz = cdata->local_linesz;
629 local_symesz = cdata->local_symesz;
630 local_auxesz = cdata->local_auxesz;
632 /* Allocate space for raw symbol and aux entries, based on their
633 space requirements as reported by BFD. */
634 temp_sym = (char *) xmalloc
635 (cdata->local_symesz + cdata->local_auxesz);
636 temp_aux = temp_sym + cdata->local_symesz;
637 back_to = make_cleanup ((make_cleanup_func) free_current_contents, &temp_sym);
639 /* We need to know whether this is a PE file, because in PE files,
640 unlike standard COFF files, symbol values are stored as offsets
641 from the section address, rather than as absolute addresses.
642 FIXME: We should use BFD to read the symbol table, and thus avoid
644 pe_file = strncmp (bfd_get_target (objfile->obfd), "pe", 2) == 0;
648 /* Read the line number table, all at once. */
649 info->min_lineno_offset = 0;
650 info->max_lineno_offset = 0;
651 bfd_map_over_sections (abfd, find_linenos, (PTR) info);
653 make_cleanup ((make_cleanup_func) free_linetab, 0);
654 val = init_lineno (abfd, info->min_lineno_offset,
655 info->max_lineno_offset - info->min_lineno_offset);
657 error ("\"%s\": error reading line numbers\n", name);
659 /* Now read the string table, all at once. */
661 make_cleanup ((make_cleanup_func) free_stringtab, 0);
662 val = init_stringtab (abfd, stringtab_offset);
664 error ("\"%s\": can't get string table", name);
666 init_minimal_symbol_collection ();
667 make_cleanup ((make_cleanup_func) discard_minimal_symbols, 0);
669 /* Now that the executable file is positioned at symbol table,
670 process it and define symbols accordingly. */
672 coff_symtab_read ((long) symtab_offset, num_symbols, objfile);
674 /* Sort symbols alphabetically within each block. */
679 for (s = objfile->symtabs; s != NULL; s = s->next)
680 sort_symtab_syms (s);
683 /* Install any minimal symbols that have been collected as the current
684 minimal symbols for this objfile. */
686 install_minimal_symbols (objfile);
688 bfd_map_over_sections (abfd, coff_locate_sections, (PTR) info);
692 if (!info->stabstrsect)
697 ("The debugging information in `%s' is corrupted.\n"
698 "The file has a `.stabs' section, but no `.stabstr' section.\n"),
700 return_to_top_level (RETURN_ERROR);
703 /* FIXME: dubious. Why can't we use something normal like
704 bfd_get_section_contents? */
705 bfd_seek (abfd, abfd->where, 0);
707 stabstrsize = bfd_section_size (abfd, info->stabstrsect);
709 coffstab_build_psymtabs (objfile,
711 info->textaddr, info->textsize,
713 info->stabstrsect->filepos, stabstrsize);
716 do_cleanups (back_to);
720 coff_new_init (ignore)
721 struct objfile *ignore;
725 /* Perform any local cleanups required when we are done with a particular
726 objfile. I.E, we are in the process of discarding all symbol information
727 for an objfile, freeing up all memory held for it, and unlinking the
728 objfile struct from the global list of known objfiles. */
731 coff_symfile_finish (objfile)
732 struct objfile *objfile;
734 if (objfile->sym_private != NULL)
736 mfree (objfile->md, objfile->sym_private);
741 /* Given pointers to a symbol table in coff style exec file,
742 analyze them and create struct symtab's describing the symbols.
743 NSYMS is the number of symbols in the symbol table.
744 We read them one at a time using read_one_sym (). */
747 coff_symtab_read (symtab_offset, nsyms, objfile)
750 struct objfile *objfile;
752 register struct context_stack *new;
753 struct coff_symbol coff_symbol;
754 register struct coff_symbol *cs = &coff_symbol;
755 static struct internal_syment main_sym;
756 static union internal_auxent main_aux;
757 struct coff_symbol fcn_cs_saved;
758 static struct internal_syment fcn_sym_saved;
759 static union internal_auxent fcn_aux_saved;
761 /* A .file is open. */
762 int in_source_file = 0;
763 int next_file_symnum = -1;
764 /* Name of the current file. */
765 char *filestring = "";
767 int fcn_first_line = 0;
768 CORE_ADDR fcn_first_line_addr;
769 int fcn_last_line = 0;
770 int fcn_start_addr = 0;
771 long fcn_line_ptr = 0;
775 /* Work around a stdio bug in SunOS4.1.1 (this makes me nervous....
776 it's hard to know I've really worked around it. The fix should be
777 harmless, anyway). The symptom of the bug is that the first
778 fread (in read_one_sym), will (in my example) actually get data
779 from file offset 268, when the fseek was to 264 (and ftell shows
780 264). This causes all hell to break loose. I was unable to
781 reproduce this on a short test program which operated on the same
782 file, performing (I think) the same sequence of operations.
784 It stopped happening when I put in this (former) rewind().
786 FIXME: Find out if this has been reported to Sun, whether it has
787 been fixed in a later release, etc. */
789 bfd_seek (objfile->obfd, 0, 0);
791 /* Position to read the symbol table. */
792 val = bfd_seek (objfile->obfd, (long) symtab_offset, 0);
794 perror_with_name (objfile->name);
796 current_objfile = objfile;
797 nlist_bfd_global = objfile->obfd;
798 nlist_nsyms_global = nsyms;
799 last_source_file = NULL;
800 memset (opaque_type_chain, 0, sizeof opaque_type_chain);
802 if (type_vector) /* Get rid of previous one */
803 free ((PTR) type_vector);
804 type_vector_length = 160;
805 type_vector = (struct type **)
806 xmalloc (type_vector_length * sizeof (struct type *));
807 memset (type_vector, 0, type_vector_length * sizeof (struct type *));
809 coff_start_symtab ("");
812 while (symnum < nsyms)
814 QUIT; /* Make this command interruptable. */
816 read_one_sym (cs, &main_sym, &main_aux);
818 if (cs->c_symnum == next_file_symnum && cs->c_sclass != C_FILE)
820 if (last_source_file)
821 coff_end_symtab (objfile);
823 coff_start_symtab ("_globals_");
824 complete_symtab ("_globals_", 0, 0);
825 /* done with all files, everything from here on out is globals */
828 /* Special case for file with type declarations only, no text. */
829 if (!last_source_file && SDB_TYPE (cs->c_type)
830 && cs->c_secnum == N_DEBUG)
831 complete_symtab (filestring, 0, 0);
833 /* Typedefs should not be treated as symbol definitions. */
834 if (ISFCN (cs->c_type) && cs->c_sclass != C_TPDEF)
836 /* Record all functions -- external and static -- in minsyms. */
837 tmpaddr = cs->c_value + ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT);
838 record_minimal_symbol (cs->c_name, tmpaddr, mst_text, objfile);
840 fcn_line_ptr = main_aux.x_sym.x_fcnary.x_fcn.x_lnnoptr;
841 fcn_start_addr = tmpaddr;
843 fcn_sym_saved = main_sym;
844 fcn_aux_saved = main_aux;
848 switch (cs->c_sclass)
857 complain (&bad_sclass_complaint, cs->c_name);
861 /* c_value field contains symnum of next .file entry in table
862 or symnum of first global after last .file. */
863 next_file_symnum = cs->c_value;
865 filestring = coff_getfilename (&main_aux);
869 /* Complete symbol table for last object file
870 containing debugging information. */
871 if (last_source_file)
873 coff_end_symtab (objfile);
874 coff_start_symtab (filestring);
879 /* C_LABEL is used for labels and static functions. Including
880 it here allows gdb to see static functions when no debug
881 info is available. */
883 /* However, labels within a function can make weird backtraces,
884 so filter them out (from phdm@macqel.be). */
890 case C_THUMBSTATFUNC:
891 if (cs->c_name[0] == '.')
893 if (STREQ (cs->c_name, ".text"))
895 /* FIXME: don't wire in ".text" as section name
897 /* Check for in_source_file deals with case of
898 a file with debugging symbols
899 followed by a later file with no symbols. */
901 complete_symtab (filestring,
902 cs->c_value + ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT),
903 main_aux.x_scn.x_scnlen);
906 /* flush rest of '.' symbols */
909 else if (!SDB_TYPE (cs->c_type)
910 && cs->c_name[0] == 'L'
911 && (strncmp (cs->c_name, "LI%", 3) == 0
912 || strncmp (cs->c_name, "LF%", 3) == 0
913 || strncmp (cs->c_name, "LC%", 3) == 0
914 || strncmp (cs->c_name, "LP%", 3) == 0
915 || strncmp (cs->c_name, "LPB%", 4) == 0
916 || strncmp (cs->c_name, "LBB%", 4) == 0
917 || strncmp (cs->c_name, "LBE%", 4) == 0
918 || strncmp (cs->c_name, "LPBX%", 5) == 0))
919 /* At least on a 3b1, gcc generates swbeg and string labels
920 that look like this. Ignore them. */
922 /* fall in for static symbols that don't start with '.' */
927 /* Record it in the minimal symbols regardless of
928 SDB_TYPE. This parallels what we do for other debug
929 formats, and probably is needed to make
930 print_address_symbolic work right without the (now
931 gone) "set fast-symbolic-addr off" kludge. */
933 /* FIXME: should use mst_abs, and not relocate, if absolute. */
934 enum minimal_symbol_type ms_type;
937 if (cs->c_secnum == N_UNDEF)
939 /* This is a common symbol. See if the target
940 environment knows where it has been relocated to. */
942 if (target_lookup_symbol (cs->c_name, &reladdr))
944 /* Error in lookup; ignore symbol. */
948 /* The address has already been relocated; make sure that
949 objfile_relocate doesn't relocate it again. */
951 ms_type = cs->c_sclass == C_EXT
952 || cs->c_sclass == C_THUMBEXT ?
953 mst_bss : mst_file_bss;
957 sec = cs_to_section (cs, objfile);
958 tmpaddr = cs->c_value;
959 if (cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXTFUNC
960 || cs->c_sclass == C_THUMBEXT)
961 tmpaddr += ANOFFSET (objfile->section_offsets, sec);
966 case SECT_OFF_RODATA:
968 cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXTFUNC
969 || cs->c_sclass == C_THUMBEXT ?
970 mst_text : mst_file_text;
971 #ifdef SMASH_TEXT_ADDRESS
972 if (tmpaddr & 1) /* FIXME: delete this line */
973 SMASH_TEXT_ADDRESS (tmpaddr);
978 cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXT ?
979 mst_data : mst_file_data;
983 cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXT ?
984 mst_data : mst_file_data;
987 ms_type = mst_unknown;
992 if (cs->c_name[0] != '@' /* Skip tdesc symbols */ )
994 struct minimal_symbol *msym;
996 msym = prim_record_minimal_symbol_and_info
997 (cs->c_name, tmpaddr, ms_type, (char *) cs->c_sclass, sec,
999 #ifdef COFF_MAKE_MSYMBOL_SPECIAL
1001 COFF_MAKE_MSYMBOL_SPECIAL (cs->c_sclass, msym);
1004 if (SDB_TYPE (cs->c_type))
1007 sym = process_coff_symbol
1008 (cs, &main_aux, objfile);
1009 SYMBOL_VALUE (sym) = tmpaddr;
1010 SYMBOL_SECTION (sym) = sec;
1016 if (STREQ (cs->c_name, ".bf"))
1018 within_function = 1;
1020 /* value contains address of first non-init type code */
1021 /* main_aux.x_sym.x_misc.x_lnsz.x_lnno
1022 contains line number of '{' } */
1023 if (cs->c_naux != 1)
1024 complain (&bf_no_aux_complaint, cs->c_symnum);
1025 fcn_first_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
1026 fcn_first_line_addr = cs->c_value;
1028 /* Might want to check that locals are 0 and
1029 context_stack_depth is zero, and complain if not. */
1032 new = push_context (depth, fcn_start_addr);
1033 fcn_cs_saved.c_name = getsymname (&fcn_sym_saved);
1035 process_coff_symbol (&fcn_cs_saved, &fcn_aux_saved, objfile);
1037 else if (STREQ (cs->c_name, ".ef"))
1039 /* the value of .ef is the address of epilogue code;
1040 not useful for gdb. */
1041 /* { main_aux.x_sym.x_misc.x_lnsz.x_lnno
1042 contains number of lines to '}' */
1044 if (context_stack_depth <= 0)
1045 { /* We attempted to pop an empty context stack */
1046 complain (&ef_stack_complaint, cs->c_symnum);
1047 within_function = 0;
1051 new = pop_context ();
1052 /* Stack must be empty now. */
1053 if (context_stack_depth > 0 || new == NULL)
1055 complain (&ef_complaint, cs->c_symnum);
1056 within_function = 0;
1059 if (cs->c_naux != 1)
1061 complain (&ef_no_aux_complaint, cs->c_symnum);
1062 fcn_last_line = 0x7FFFFFFF;
1066 fcn_last_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
1068 /* fcn_first_line is the line number of the opening '{'.
1069 Do not record it - because it would affect gdb's idea
1070 of the line number of the first statement of the function -
1071 except for one-line functions, for which it is also the line
1072 number of all the statements and of the closing '}', and
1073 for which we do not have any other statement-line-number. */
1074 if (fcn_last_line == 1)
1075 record_line (current_subfile, fcn_first_line,
1076 fcn_first_line_addr);
1078 enter_linenos (fcn_line_ptr, fcn_first_line, fcn_last_line,
1079 objfile->section_offsets);
1081 finish_block (new->name, &local_symbols, new->old_blocks,
1083 #if defined (FUNCTION_EPILOGUE_SIZE)
1084 /* This macro should be defined only on
1086 fcn_aux_saved.x_sym.x_misc.x_fsize
1087 field is always zero.
1088 So use the .bf record information that
1089 points to the epilogue and add the size
1092 + FUNCTION_EPILOGUE_SIZE
1093 + ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT),
1095 fcn_cs_saved.c_value
1096 + fcn_aux_saved.x_sym.x_misc.x_fsize
1097 + ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT),
1101 within_function = 0;
1106 if (STREQ (cs->c_name, ".bb"))
1108 tmpaddr = cs->c_value;
1109 tmpaddr += ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT);
1110 push_context (++depth, tmpaddr);
1112 else if (STREQ (cs->c_name, ".eb"))
1114 if (context_stack_depth <= 0)
1115 { /* We attempted to pop an empty context stack */
1116 complain (&eb_stack_complaint, cs->c_symnum);
1120 new = pop_context ();
1121 if (depth-- != new->depth)
1123 complain (&eb_complaint, symnum);
1126 if (local_symbols && context_stack_depth > 0)
1129 cs->c_value + ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT);
1130 /* Make a block for the local symbols within. */
1131 finish_block (0, &local_symbols, new->old_blocks,
1132 new->start_addr, tmpaddr, objfile);
1134 /* Now pop locals of block just finished. */
1135 local_symbols = new->locals;
1140 process_coff_symbol (cs, &main_aux, objfile);
1145 if (last_source_file)
1146 coff_end_symtab (objfile);
1148 /* Patch up any opaque types (references to types that are not defined
1149 in the file where they are referenced, e.g. "struct foo *bar"). */
1150 ALL_OBJFILE_SYMTABS (objfile, s)
1151 patch_opaque_types (s);
1153 current_objfile = NULL;
1156 /* Routines for reading headers and symbols from executable. */
1158 /* Read the next symbol, swap it, and return it in both internal_syment
1159 form, and coff_symbol form. Also return its first auxent, if any,
1160 in internal_auxent form, and skip any other auxents. */
1163 read_one_sym (cs, sym, aux)
1164 register struct coff_symbol *cs;
1165 register struct internal_syment *sym;
1166 register union internal_auxent *aux;
1170 cs->c_symnum = symnum;
1171 bfd_read (temp_sym, local_symesz, 1, nlist_bfd_global);
1172 bfd_coff_swap_sym_in (symfile_bfd, temp_sym, (char *) sym);
1173 cs->c_naux = sym->n_numaux & 0xff;
1174 if (cs->c_naux >= 1)
1176 bfd_read (temp_aux, local_auxesz, 1, nlist_bfd_global);
1177 bfd_coff_swap_aux_in (symfile_bfd, temp_aux, sym->n_type, sym->n_sclass,
1178 0, cs->c_naux, (char *) aux);
1179 /* If more than one aux entry, read past it (only the first aux
1181 for (i = 1; i < cs->c_naux; i++)
1182 bfd_read (temp_aux, local_auxesz, 1, nlist_bfd_global);
1184 cs->c_name = getsymname (sym);
1185 cs->c_value = sym->n_value;
1186 cs->c_sclass = (sym->n_sclass & 0xff);
1187 cs->c_secnum = sym->n_scnum;
1188 cs->c_type = (unsigned) sym->n_type;
1189 if (!SDB_TYPE (cs->c_type))
1193 if (cs->c_sclass & 128)
1194 printf ("thumb symbol %s, class 0x%x\n", cs->c_name, cs->c_sclass);
1197 symnum += 1 + cs->c_naux;
1199 /* The PE file format stores symbol values as offsets within the
1200 section, rather than as absolute addresses. We correct that
1201 here, if the symbol has an appropriate storage class. FIXME: We
1202 should use BFD to read the symbols, rather than duplicating the
1206 switch (cs->c_sclass)
1210 case C_THUMBEXTFUNC:
1215 case C_THUMBSTATFUNC:
1221 if (cs->c_secnum != 0)
1222 cs->c_value += cs_section_address (cs, symfile_bfd);
1228 /* Support for string table handling */
1230 static char *stringtab = NULL;
1233 init_stringtab (abfd, offset)
1239 unsigned char lengthbuf[4];
1243 /* If the file is stripped, the offset might be zero, indicating no
1244 string table. Just return with `stringtab' set to null. */
1248 if (bfd_seek (abfd, offset, 0) < 0)
1251 val = bfd_read ((char *) lengthbuf, sizeof lengthbuf, 1, abfd);
1252 length = bfd_h_get_32 (symfile_bfd, lengthbuf);
1254 /* If no string table is needed, then the file may end immediately
1255 after the symbols. Just return with `stringtab' set to null. */
1256 if (val != sizeof lengthbuf || length < sizeof lengthbuf)
1259 stringtab = (char *) xmalloc (length);
1260 /* This is in target format (probably not very useful, and not currently
1261 used), not host format. */
1262 memcpy (stringtab, lengthbuf, sizeof lengthbuf);
1263 if (length == sizeof length) /* Empty table -- just the count */
1266 val = bfd_read (stringtab + sizeof lengthbuf, length - sizeof lengthbuf, 1, abfd);
1267 if (val != length - sizeof lengthbuf || stringtab[length - 1] != '\0')
1282 getsymname (symbol_entry)
1283 struct internal_syment *symbol_entry;
1285 static char buffer[SYMNMLEN + 1];
1288 if (symbol_entry->_n._n_n._n_zeroes == 0)
1290 /* FIXME: Probably should be detecting corrupt symbol files by
1291 seeing whether offset points to within the stringtab. */
1292 result = stringtab + symbol_entry->_n._n_n._n_offset;
1296 strncpy (buffer, symbol_entry->_n._n_name, SYMNMLEN);
1297 buffer[SYMNMLEN] = '\0';
1303 /* Extract the file name from the aux entry of a C_FILE symbol. Return
1304 only the last component of the name. Result is in static storage and
1305 is only good for temporary use. */
1308 coff_getfilename (aux_entry)
1309 union internal_auxent *aux_entry;
1311 static char buffer[BUFSIZ];
1312 register char *temp;
1315 if (aux_entry->x_file.x_n.x_zeroes == 0)
1316 strcpy (buffer, stringtab + aux_entry->x_file.x_n.x_offset);
1319 strncpy (buffer, aux_entry->x_file.x_fname, FILNMLEN);
1320 buffer[FILNMLEN] = '\0';
1324 /* FIXME: We should not be throwing away the information about what
1325 directory. It should go into dirname of the symtab, or some such
1327 if ((temp = strrchr (result, '/')) != NULL)
1332 /* Support for line number handling. */
1334 static char *linetab = NULL;
1335 static long linetab_offset;
1336 static unsigned long linetab_size;
1338 /* Read in all the line numbers for fast lookups later. Leave them in
1339 external (unswapped) format in memory; we'll swap them as we enter
1340 them into GDB's data structures. */
1343 init_lineno (abfd, offset, size)
1350 linetab_offset = offset;
1351 linetab_size = size;
1358 if (bfd_seek (abfd, offset, 0) < 0)
1361 /* Allocate the desired table, plus a sentinel */
1362 linetab = (char *) xmalloc (size + local_linesz);
1364 val = bfd_read (linetab, size, 1, abfd);
1368 /* Terminate it with an all-zero sentinel record */
1369 memset (linetab + size, 0, local_linesz);
1382 #if !defined (L_LNNO32)
1383 #define L_LNNO32(lp) ((lp)->l_lnno)
1387 enter_linenos (file_offset, first_line, last_line, section_offsets)
1389 register int first_line;
1390 register int last_line;
1391 struct section_offsets *section_offsets;
1393 register char *rawptr;
1394 struct internal_lineno lptr;
1398 if (file_offset < linetab_offset)
1400 complain (&lineno_complaint, file_offset);
1401 if (file_offset > linetab_size) /* Too big to be an offset? */
1403 file_offset += linetab_offset; /* Try reading at that linetab offset */
1406 rawptr = &linetab[file_offset - linetab_offset];
1408 /* skip first line entry for each function */
1409 rawptr += local_linesz;
1410 /* line numbers start at one for the first line of the function */
1415 bfd_coff_swap_lineno_in (symfile_bfd, rawptr, &lptr);
1416 rawptr += local_linesz;
1417 /* The next function, or the sentinel, will have L_LNNO32 zero; we exit. */
1418 if (L_LNNO32 (&lptr) && L_LNNO32 (&lptr) <= last_line)
1419 record_line (current_subfile, first_line + L_LNNO32 (&lptr),
1421 + ANOFFSET (section_offsets, SECT_OFF_TEXT));
1428 patch_type (type, real_type)
1430 struct type *real_type;
1432 register struct type *target = TYPE_TARGET_TYPE (type);
1433 register struct type *real_target = TYPE_TARGET_TYPE (real_type);
1434 int field_size = TYPE_NFIELDS (real_target) * sizeof (struct field);
1436 TYPE_LENGTH (target) = TYPE_LENGTH (real_target);
1437 TYPE_NFIELDS (target) = TYPE_NFIELDS (real_target);
1438 TYPE_FIELDS (target) = (struct field *) TYPE_ALLOC (target, field_size);
1440 memcpy (TYPE_FIELDS (target), TYPE_FIELDS (real_target), field_size);
1442 if (TYPE_NAME (real_target))
1444 if (TYPE_NAME (target))
1445 free (TYPE_NAME (target));
1446 TYPE_NAME (target) = concat (TYPE_NAME (real_target), NULL);
1450 /* Patch up all appropriate typedef symbols in the opaque_type_chains
1451 so that they can be used to print out opaque data structures properly. */
1454 patch_opaque_types (s)
1457 register struct block *b;
1459 register struct symbol *real_sym;
1461 /* Go through the per-file symbols only */
1462 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
1463 for (i = BLOCK_NSYMS (b) - 1; i >= 0; i--)
1465 /* Find completed typedefs to use to fix opaque ones.
1466 Remove syms from the chain when their types are stored,
1467 but search the whole chain, as there may be several syms
1468 from different files with the same name. */
1469 real_sym = BLOCK_SYM (b, i);
1470 if (SYMBOL_CLASS (real_sym) == LOC_TYPEDEF &&
1471 SYMBOL_NAMESPACE (real_sym) == VAR_NAMESPACE &&
1472 TYPE_CODE (SYMBOL_TYPE (real_sym)) == TYPE_CODE_PTR &&
1473 TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (real_sym))) != 0)
1475 register char *name = SYMBOL_NAME (real_sym);
1476 register int hash = hashname (name);
1477 register struct symbol *sym, *prev;
1480 for (sym = opaque_type_chain[hash]; sym;)
1482 if (name[0] == SYMBOL_NAME (sym)[0] &&
1483 STREQ (name + 1, SYMBOL_NAME (sym) + 1))
1487 SYMBOL_VALUE_CHAIN (prev) = SYMBOL_VALUE_CHAIN (sym);
1491 opaque_type_chain[hash] = SYMBOL_VALUE_CHAIN (sym);
1494 patch_type (SYMBOL_TYPE (sym), SYMBOL_TYPE (real_sym));
1498 sym = SYMBOL_VALUE_CHAIN (prev);
1502 sym = opaque_type_chain[hash];
1508 sym = SYMBOL_VALUE_CHAIN (sym);
1515 static struct symbol *
1516 process_coff_symbol (cs, aux, objfile)
1517 register struct coff_symbol *cs;
1518 register union internal_auxent *aux;
1519 struct objfile *objfile;
1521 register struct symbol *sym
1522 = (struct symbol *) obstack_alloc (&objfile->symbol_obstack,
1523 sizeof (struct symbol));
1526 memset (sym, 0, sizeof (struct symbol));
1528 name = EXTERNAL_NAME (name, objfile->obfd);
1529 SYMBOL_NAME (sym) = obsavestring (name, strlen (name),
1530 &objfile->symbol_obstack);
1531 SYMBOL_LANGUAGE (sym) = language_auto;
1532 SYMBOL_INIT_DEMANGLED_NAME (sym, &objfile->symbol_obstack);
1534 /* default assumptions */
1535 SYMBOL_VALUE (sym) = cs->c_value;
1536 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1537 SYMBOL_SECTION (sym) = cs_to_section (cs, objfile);
1539 if (ISFCN (cs->c_type))
1541 SYMBOL_VALUE (sym) += ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT);
1543 lookup_function_type (decode_function_type (cs, cs->c_type, aux));
1545 SYMBOL_CLASS (sym) = LOC_BLOCK;
1546 if (cs->c_sclass == C_STAT || cs->c_sclass == C_THUMBSTAT
1547 || cs->c_sclass == C_THUMBSTATFUNC)
1548 add_symbol_to_list (sym, &file_symbols);
1549 else if (cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXT
1550 || cs->c_sclass == C_THUMBEXTFUNC)
1551 add_symbol_to_list (sym, &global_symbols);
1555 SYMBOL_TYPE (sym) = decode_type (cs, cs->c_type, aux);
1556 switch (cs->c_sclass)
1562 SYMBOL_CLASS (sym) = LOC_LOCAL;
1563 add_symbol_to_list (sym, &local_symbols);
1567 case C_THUMBEXTFUNC:
1569 SYMBOL_CLASS (sym) = LOC_STATIC;
1570 SYMBOL_VALUE_ADDRESS (sym) = (CORE_ADDR) cs->c_value;
1571 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT);
1572 add_symbol_to_list (sym, &global_symbols);
1576 case C_THUMBSTATFUNC:
1578 SYMBOL_CLASS (sym) = LOC_STATIC;
1579 SYMBOL_VALUE_ADDRESS (sym) = (CORE_ADDR) cs->c_value;
1580 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT);
1581 if (within_function)
1583 /* Static symbol of local scope */
1584 add_symbol_to_list (sym, &local_symbols);
1588 /* Static symbol at top level of file */
1589 add_symbol_to_list (sym, &file_symbols);
1593 #ifdef C_GLBLREG /* AMD coff */
1597 SYMBOL_CLASS (sym) = LOC_REGISTER;
1598 SYMBOL_VALUE (sym) = SDB_REG_TO_REGNUM (cs->c_value);
1599 add_symbol_to_list (sym, &local_symbols);
1607 SYMBOL_CLASS (sym) = LOC_ARG;
1608 add_symbol_to_list (sym, &local_symbols);
1609 #if !defined (BELIEVE_PCC_PROMOTION)
1610 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
1612 /* If PCC says a parameter is a short or a char,
1613 aligned on an int boundary, realign it to the
1614 "little end" of the int. */
1615 struct type *temptype;
1616 temptype = lookup_fundamental_type (current_objfile,
1618 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) < TYPE_LENGTH (temptype)
1619 && TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_INT
1620 && 0 == SYMBOL_VALUE (sym) % TYPE_LENGTH (temptype))
1622 SYMBOL_VALUE (sym) +=
1623 TYPE_LENGTH (temptype)
1624 - TYPE_LENGTH (SYMBOL_TYPE (sym));
1631 SYMBOL_CLASS (sym) = LOC_REGPARM;
1632 SYMBOL_VALUE (sym) = SDB_REG_TO_REGNUM (cs->c_value);
1633 add_symbol_to_list (sym, &local_symbols);
1634 #if !defined (BELIEVE_PCC_PROMOTION)
1635 /* FIXME: This should retain the current type, since it's just
1636 a register value. gnu@adobe, 26Feb93 */
1638 /* If PCC says a parameter is a short or a char,
1639 it is really an int. */
1640 struct type *temptype;
1642 lookup_fundamental_type (current_objfile, FT_INTEGER);
1643 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) < TYPE_LENGTH (temptype)
1644 && TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_INT)
1647 (TYPE_UNSIGNED (SYMBOL_TYPE (sym))
1648 ? lookup_fundamental_type (current_objfile,
1649 FT_UNSIGNED_INTEGER)
1657 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
1658 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1660 /* If type has no name, give it one */
1661 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
1663 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_PTR
1664 || TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_FUNC)
1666 /* If we are giving a name to a type such as "pointer to
1667 foo" or "function returning foo", we better not set
1668 the TYPE_NAME. If the program contains "typedef char
1669 *caddr_t;", we don't want all variables of type char
1670 * to print as caddr_t. This is not just a
1671 consequence of GDB's type management; CC and GCC (at
1672 least through version 2.4) both output variables of
1673 either type char * or caddr_t with the type
1674 refering to the C_TPDEF symbol for caddr_t. If a future
1675 compiler cleans this up it GDB is not ready for it
1676 yet, but if it becomes ready we somehow need to
1677 disable this check (without breaking the PCC/GCC2.4
1682 Fortunately, this check seems not to be necessary
1683 for anything except pointers or functions. */
1687 TYPE_NAME (SYMBOL_TYPE (sym)) =
1688 concat (SYMBOL_NAME (sym), NULL);
1691 /* Ignore vendor section for Harris CX/UX targets. */
1692 else if (cs->c_name[0] == '$')
1694 #endif /* CXUX_TARGET */
1696 /* Keep track of any type which points to empty structured type,
1697 so it can be filled from a definition from another file. A
1698 simple forward reference (TYPE_CODE_UNDEF) is not an
1699 empty structured type, though; the forward references
1700 work themselves out via the magic of coff_lookup_type. */
1701 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_PTR &&
1702 TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym))) == 0 &&
1703 TYPE_CODE (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym))) !=
1706 register int i = hashname (SYMBOL_NAME (sym));
1708 SYMBOL_VALUE_CHAIN (sym) = opaque_type_chain[i];
1709 opaque_type_chain[i] = sym;
1711 add_symbol_to_list (sym, &file_symbols);
1717 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
1718 SYMBOL_NAMESPACE (sym) = STRUCT_NAMESPACE;
1720 /* Some compilers try to be helpful by inventing "fake"
1721 names for anonymous enums, structures, and unions, like
1722 "~0fake" or ".0fake". Thanks, but no thanks... */
1723 if (TYPE_TAG_NAME (SYMBOL_TYPE (sym)) == 0)
1724 if (SYMBOL_NAME (sym) != NULL
1725 && *SYMBOL_NAME (sym) != '~'
1726 && *SYMBOL_NAME (sym) != '.')
1727 TYPE_TAG_NAME (SYMBOL_TYPE (sym)) =
1728 concat (SYMBOL_NAME (sym), NULL);
1730 add_symbol_to_list (sym, &file_symbols);
1740 /* Decode a coff type specifier; return the type that is meant. */
1742 static struct type *
1743 decode_type (cs, c_type, aux)
1744 register struct coff_symbol *cs;
1745 unsigned int c_type;
1746 register union internal_auxent *aux;
1748 register struct type *type = 0;
1749 unsigned int new_c_type;
1751 if (c_type & ~N_BTMASK)
1753 new_c_type = DECREF (c_type);
1756 type = decode_type (cs, new_c_type, aux);
1757 type = lookup_pointer_type (type);
1759 else if (ISFCN (c_type))
1761 type = decode_type (cs, new_c_type, aux);
1762 type = lookup_function_type (type);
1764 else if (ISARY (c_type))
1767 register unsigned short *dim;
1768 struct type *base_type, *index_type, *range_type;
1770 /* Define an array type. */
1771 /* auxent refers to array, not base type */
1772 if (aux->x_sym.x_tagndx.l == 0)
1775 /* shift the indices down */
1776 dim = &aux->x_sym.x_fcnary.x_ary.x_dimen[0];
1779 for (i = 0; *dim && i < DIMNUM - 1; i++, dim++)
1783 base_type = decode_type (cs, new_c_type, aux);
1784 index_type = lookup_fundamental_type (current_objfile, FT_INTEGER);
1786 create_range_type ((struct type *) NULL, index_type, 0, n - 1);
1788 create_array_type ((struct type *) NULL, base_type, range_type);
1793 /* Reference to existing type. This only occurs with the
1794 struct, union, and enum types. EPI a29k coff
1795 fakes us out by producing aux entries with a nonzero
1796 x_tagndx for definitions of structs, unions, and enums, so we
1797 have to check the c_sclass field. SCO 3.2v4 cc gets confused
1798 with pointers to pointers to defined structs, and generates
1799 negative x_tagndx fields. */
1800 if (cs->c_naux > 0 && aux->x_sym.x_tagndx.l != 0)
1802 if (cs->c_sclass != C_STRTAG
1803 && cs->c_sclass != C_UNTAG
1804 && cs->c_sclass != C_ENTAG
1805 && aux->x_sym.x_tagndx.l >= 0)
1807 type = coff_alloc_type (aux->x_sym.x_tagndx.l);
1812 complain (&tagndx_bad_complaint, cs->c_name);
1813 /* And fall through to decode_base_type... */
1817 return decode_base_type (cs, BTYPE (c_type), aux);
1820 /* Decode a coff type specifier for function definition;
1821 return the type that the function returns. */
1823 static struct type *
1824 decode_function_type (cs, c_type, aux)
1825 register struct coff_symbol *cs;
1826 unsigned int c_type;
1827 register union internal_auxent *aux;
1829 if (aux->x_sym.x_tagndx.l == 0)
1830 cs->c_naux = 0; /* auxent refers to function, not base type */
1832 return decode_type (cs, DECREF (c_type), aux);
1837 static struct type *
1838 decode_base_type (cs, c_type, aux)
1839 register struct coff_symbol *cs;
1840 unsigned int c_type;
1841 register union internal_auxent *aux;
1848 /* shows up with "void (*foo)();" structure members */
1849 return lookup_fundamental_type (current_objfile, FT_VOID);
1852 /* DGUX actually defines both T_ARG and T_VOID to the same value. */
1855 /* Shows up in DGUX, I think. Not sure where. */
1856 return lookup_fundamental_type (current_objfile, FT_VOID); /* shouldn't show up here */
1862 /* Intel 960 COFF has this symbol and meaning. */
1863 return lookup_fundamental_type (current_objfile, FT_VOID);
1867 return lookup_fundamental_type (current_objfile, FT_CHAR);
1870 return lookup_fundamental_type (current_objfile, FT_SHORT);
1873 return lookup_fundamental_type (current_objfile, FT_INTEGER);
1876 if (cs->c_sclass == C_FIELD
1877 && aux->x_sym.x_misc.x_lnsz.x_size > TARGET_LONG_BIT)
1878 return lookup_fundamental_type (current_objfile, FT_LONG_LONG);
1880 return lookup_fundamental_type (current_objfile, FT_LONG);
1883 return lookup_fundamental_type (current_objfile, FT_FLOAT);
1886 return lookup_fundamental_type (current_objfile, FT_DBL_PREC_FLOAT);
1889 return lookup_fundamental_type (current_objfile, FT_EXT_PREC_FLOAT);
1892 if (cs->c_naux != 1)
1894 /* anonymous structure type */
1895 type = coff_alloc_type (cs->c_symnum);
1896 TYPE_CODE (type) = TYPE_CODE_STRUCT;
1897 TYPE_NAME (type) = NULL;
1898 /* This used to set the tag to "<opaque>". But I think setting it
1899 to NULL is right, and the printing code can print it as
1901 TYPE_TAG_NAME (type) = NULL;
1902 INIT_CPLUS_SPECIFIC (type);
1903 TYPE_LENGTH (type) = 0;
1904 TYPE_FIELDS (type) = 0;
1905 TYPE_NFIELDS (type) = 0;
1909 type = coff_read_struct_type (cs->c_symnum,
1910 aux->x_sym.x_misc.x_lnsz.x_size,
1911 aux->x_sym.x_fcnary.x_fcn.x_endndx.l);
1916 if (cs->c_naux != 1)
1918 /* anonymous union type */
1919 type = coff_alloc_type (cs->c_symnum);
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);
1936 TYPE_CODE (type) = TYPE_CODE_UNION;
1940 if (cs->c_naux != 1)
1942 /* anonymous enum type */
1943 type = coff_alloc_type (cs->c_symnum);
1944 TYPE_CODE (type) = TYPE_CODE_ENUM;
1945 TYPE_NAME (type) = NULL;
1946 /* This used to set the tag to "<opaque>". But I think setting it
1947 to NULL is right, and the printing code can print it as
1949 TYPE_TAG_NAME (type) = NULL;
1950 TYPE_LENGTH (type) = 0;
1951 TYPE_FIELDS (type) = 0;
1952 TYPE_NFIELDS (type) = 0;
1956 type = coff_read_enum_type (cs->c_symnum,
1957 aux->x_sym.x_misc.x_lnsz.x_size,
1958 aux->x_sym.x_fcnary.x_fcn.x_endndx.l);
1963 /* shouldn't show up here */
1967 return lookup_fundamental_type (current_objfile, FT_UNSIGNED_CHAR);
1970 return lookup_fundamental_type (current_objfile, FT_UNSIGNED_SHORT);
1973 return lookup_fundamental_type (current_objfile, FT_UNSIGNED_INTEGER);
1976 if (cs->c_sclass == C_FIELD
1977 && aux->x_sym.x_misc.x_lnsz.x_size > TARGET_LONG_BIT)
1978 return lookup_fundamental_type (current_objfile, FT_UNSIGNED_LONG_LONG);
1980 return lookup_fundamental_type (current_objfile, FT_UNSIGNED_LONG);
1982 complain (&unexpected_type_complaint, cs->c_name);
1983 return lookup_fundamental_type (current_objfile, FT_VOID);
1986 /* This page contains subroutines of read_type. */
1988 /* Read the description of a structure (or union type) and return an
1989 object describing the type. */
1991 static struct type *
1992 coff_read_struct_type (index, length, lastsym)
1999 struct nextfield *next;
2003 register struct type *type;
2004 register struct nextfield *list = 0;
2005 struct nextfield *new;
2009 struct coff_symbol member_sym;
2010 register struct coff_symbol *ms = &member_sym;
2011 struct internal_syment sub_sym;
2012 union internal_auxent sub_aux;
2015 type = coff_alloc_type (index);
2016 TYPE_CODE (type) = TYPE_CODE_STRUCT;
2017 INIT_CPLUS_SPECIFIC (type);
2018 TYPE_LENGTH (type) = length;
2020 while (!done && symnum < lastsym && symnum < nlist_nsyms_global)
2022 read_one_sym (ms, &sub_sym, &sub_aux);
2024 name = EXTERNAL_NAME (name, current_objfile->obfd);
2026 switch (ms->c_sclass)
2031 /* Get space to record the next field's data. */
2032 new = (struct nextfield *) alloca (sizeof (struct nextfield));
2036 /* Save the data. */
2040 ¤t_objfile->symbol_obstack);
2041 FIELD_TYPE (list->field) = decode_type (ms, ms->c_type, &sub_aux);
2042 FIELD_BITPOS (list->field) = 8 * ms->c_value;
2043 FIELD_BITSIZE (list->field) = 0;
2049 /* Get space to record the next field's data. */
2050 new = (struct nextfield *) alloca (sizeof (struct nextfield));
2054 /* Save the data. */
2058 ¤t_objfile->symbol_obstack);
2059 FIELD_TYPE (list->field) = decode_type (ms, ms->c_type, &sub_aux);
2060 FIELD_BITPOS (list->field) = ms->c_value;
2061 FIELD_BITSIZE (list->field) = sub_aux.x_sym.x_misc.x_lnsz.x_size;
2070 /* Now create the vector of fields, and record how big it is. */
2072 TYPE_NFIELDS (type) = nfields;
2073 TYPE_FIELDS (type) = (struct field *)
2074 TYPE_ALLOC (type, sizeof (struct field) * nfields);
2076 /* Copy the saved-up fields into the field vector. */
2078 for (n = nfields; list; list = list->next)
2079 TYPE_FIELD (type, --n) = list->field;
2084 /* Read a definition of an enumeration type,
2085 and create and return a suitable type object.
2086 Also defines the symbols that represent the values of the type. */
2089 static struct type *
2090 coff_read_enum_type (index, length, lastsym)
2095 register struct symbol *sym;
2096 register struct type *type;
2099 struct pending **symlist;
2100 struct coff_symbol member_sym;
2101 register struct coff_symbol *ms = &member_sym;
2102 struct internal_syment sub_sym;
2103 union internal_auxent sub_aux;
2104 struct pending *osyms, *syms;
2108 int unsigned_enum = 1;
2110 type = coff_alloc_type (index);
2111 if (within_function)
2112 symlist = &local_symbols;
2114 symlist = &file_symbols;
2116 o_nsyms = osyms ? osyms->nsyms : 0;
2118 while (!done && symnum < lastsym && symnum < nlist_nsyms_global)
2120 read_one_sym (ms, &sub_sym, &sub_aux);
2122 name = EXTERNAL_NAME (name, current_objfile->obfd);
2124 switch (ms->c_sclass)
2127 sym = (struct symbol *) obstack_alloc
2128 (¤t_objfile->symbol_obstack,
2129 sizeof (struct symbol));
2130 memset (sym, 0, sizeof (struct symbol));
2133 obsavestring (name, strlen (name),
2134 ¤t_objfile->symbol_obstack);
2135 SYMBOL_CLASS (sym) = LOC_CONST;
2136 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
2137 SYMBOL_VALUE (sym) = ms->c_value;
2138 add_symbol_to_list (sym, symlist);
2143 /* Sometimes the linker (on 386/ix 2.0.2 at least) screws
2144 up the count of how many symbols to read. So stop
2151 /* Now fill in the fields of the type-structure. */
2154 TYPE_LENGTH (type) = length;
2156 TYPE_LENGTH (type) = TARGET_INT_BIT / TARGET_CHAR_BIT; /* Assume ints */
2157 TYPE_CODE (type) = TYPE_CODE_ENUM;
2158 TYPE_NFIELDS (type) = nsyms;
2159 TYPE_FIELDS (type) = (struct field *)
2160 TYPE_ALLOC (type, sizeof (struct field) * nsyms);
2162 /* Find the symbols for the values and put them into the type.
2163 The symbols can be found in the symlist that we put them on
2164 to cause them to be defined. osyms contains the old value
2165 of that symlist; everything up to there was defined by us. */
2166 /* Note that we preserve the order of the enum constants, so
2167 that in something like "enum {FOO, LAST_THING=FOO}" we print
2168 FOO, not LAST_THING. */
2170 for (syms = *symlist, n = 0; syms; syms = syms->next)
2176 for (; j < syms->nsyms; j++, n++)
2178 struct symbol *xsym = syms->symbol[j];
2179 SYMBOL_TYPE (xsym) = type;
2180 TYPE_FIELD_NAME (type, n) = SYMBOL_NAME (xsym);
2181 TYPE_FIELD_BITPOS (type, n) = SYMBOL_VALUE (xsym);
2182 if (SYMBOL_VALUE (xsym) < 0)
2184 TYPE_FIELD_BITSIZE (type, n) = 0;
2191 TYPE_FLAGS (type) |= TYPE_FLAG_UNSIGNED;
2196 /* Register our ability to parse symbols for coff BFD files. */
2198 static struct sym_fns coff_sym_fns =
2200 bfd_target_coff_flavour,
2201 coff_new_init, /* sym_new_init: init anything gbl to entire symtab */
2202 coff_symfile_init, /* sym_init: read initial info, setup for sym_read() */
2203 coff_symfile_read, /* sym_read: read a symbol file into symtab */
2204 coff_symfile_finish, /* sym_finish: finished with file, cleanup */
2205 default_symfile_offsets, /* sym_offsets: xlate external to internal form */
2206 NULL /* next: pointer to next struct sym_fns */
2210 _initialize_coffread ()
2212 add_symtab_fns (&coff_sym_fns);