1 /* Read coff symbol tables and convert to internal format, for GDB.
2 Copyright (C) 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
3 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009,
4 2010, 2011 Free Software Foundation, Inc.
5 Contributed by David D. Johnson, Brown University (ddj@cs.brown.edu).
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26 #include "breakpoint.h"
29 #include "gdb_obstack.h"
31 #include "gdb_string.h"
34 #include "coff/internal.h" /* Internal format of COFF symbols in BFD */
35 #include "libcoff.h" /* FIXME secret internal data from BFD */
38 #include "gdb-stabs.h"
39 #include "stabsread.h"
40 #include "complaints.h"
42 #include "gdb_assert.h"
44 #include "dictionary.h"
46 #include "coff-pe-read.h"
50 extern void _initialize_coffread (void);
52 /* The objfile we are currently reading. */
54 static struct objfile *coffread_objfile;
56 struct coff_symfile_info
58 file_ptr min_lineno_offset; /* Where in file lowest line#s are. */
59 file_ptr max_lineno_offset; /* 1+last byte of line#s in file. */
61 CORE_ADDR textaddr; /* Addr of .text section. */
62 unsigned int textsize; /* Size of .text section. */
63 struct stab_section_list *stabsects; /* .stab sections. */
64 asection *stabstrsect; /* Section pointer for .stab section. */
68 /* Translate an external name string into a user-visible name. */
69 #define EXTERNAL_NAME(string, abfd) \
70 (string[0] == bfd_get_symbol_leading_char (abfd) \
71 ? string + 1 : string)
73 /* To be an sdb debug type, type must have at least a basic or primary
74 derived type. Using this rather than checking against T_NULL is
75 said to prevent core dumps if we try to operate on Michael Bloom
78 #define SDB_TYPE(type) (BTYPE(type) | (type & N_TMASK))
80 /* Core address of start and end of text of current source file.
81 This comes from a ".text" symbol where x_nlinno > 0. */
83 static CORE_ADDR current_source_start_addr;
84 static CORE_ADDR current_source_end_addr;
86 /* The addresses of the symbol table stream and number of symbols
87 of the object file we are reading (as copied into core). */
89 static bfd *nlist_bfd_global;
90 static int nlist_nsyms_global;
93 /* Pointers to scratch storage, used for reading raw symbols and
96 static char *temp_sym;
97 static char *temp_aux;
99 /* Local variables that hold the shift and mask values for the
100 COFF file that we are currently reading. These come back to us
101 from BFD, and are referenced by their macro names, as well as
102 internally to the BTYPE, ISPTR, ISFCN, ISARY, ISTAG, and DECREF
103 macros from include/coff/internal.h . */
105 static unsigned local_n_btmask;
106 static unsigned local_n_btshft;
107 static unsigned local_n_tmask;
108 static unsigned local_n_tshift;
110 #define N_BTMASK local_n_btmask
111 #define N_BTSHFT local_n_btshft
112 #define N_TMASK local_n_tmask
113 #define N_TSHIFT local_n_tshift
115 /* Local variables that hold the sizes in the file of various COFF
116 structures. (We only need to know this to read them from the file
117 -- BFD will then translate the data in them, into `internal_xxx'
118 structs in the right byte order, alignment, etc.) */
120 static unsigned local_linesz;
121 static unsigned local_symesz;
122 static unsigned local_auxesz;
124 /* This is set if this is a PE format file. */
128 /* Chain of typedefs of pointers to empty struct/union types.
129 They are chained thru the SYMBOL_VALUE_CHAIN. */
131 static struct symbol *opaque_type_chain[HASHSIZE];
133 /* Simplified internal version of coff symbol table information. */
138 int c_symnum; /* Symbol number of this entry. */
139 int c_naux; /* 0 if syment only, 1 if syment +
147 extern void stabsread_clear_cache (void);
149 static struct type *coff_read_struct_type (int, int, int,
152 static struct type *decode_base_type (struct coff_symbol *,
154 union internal_auxent *,
157 static struct type *decode_type (struct coff_symbol *, unsigned int,
158 union internal_auxent *,
161 static struct type *decode_function_type (struct coff_symbol *,
163 union internal_auxent *,
166 static struct type *coff_read_enum_type (int, int, int,
169 static struct symbol *process_coff_symbol (struct coff_symbol *,
170 union internal_auxent *,
173 static void patch_opaque_types (struct symtab *);
175 static void enter_linenos (long, int, int, struct objfile *);
177 static void free_linetab (void);
179 static void free_linetab_cleanup (void *ignore);
181 static int init_lineno (bfd *, long, int);
183 static char *getsymname (struct internal_syment *);
185 static const char *coff_getfilename (union internal_auxent *);
187 static void free_stringtab (void);
189 static void free_stringtab_cleanup (void *ignore);
191 static int init_stringtab (bfd *, long);
193 static void read_one_sym (struct coff_symbol *,
194 struct internal_syment *,
195 union internal_auxent *);
197 static void coff_symtab_read (long, unsigned int, struct objfile *);
199 /* We are called once per section from coff_symfile_read. We
200 need to examine each section we are passed, check to see
201 if it is something we are interested in processing, and
202 if so, stash away some access information for the section.
204 FIXME: The section names should not be hardwired strings (what
205 should they be? I don't think most object file formats have enough
206 section flags to specify what kind of debug section it is
210 coff_locate_sections (bfd *abfd, asection *sectp, void *csip)
212 struct coff_symfile_info *csi;
215 csi = (struct coff_symfile_info *) csip;
216 name = bfd_get_section_name (abfd, sectp);
217 if (strcmp (name, ".text") == 0)
219 csi->textaddr = bfd_section_vma (abfd, sectp);
220 csi->textsize += bfd_section_size (abfd, sectp);
222 else if (strncmp (name, ".text", sizeof ".text" - 1) == 0)
224 csi->textsize += bfd_section_size (abfd, sectp);
226 else if (strcmp (name, ".stabstr") == 0)
228 csi->stabstrsect = sectp;
230 else if (strncmp (name, ".stab", sizeof ".stab" - 1) == 0)
234 /* We can have multiple .stab sections if linked with
236 for (s = name + sizeof ".stab" - 1; *s != '\0'; s++)
241 struct stab_section_list *n, **pn;
243 n = ((struct stab_section_list *)
244 xmalloc (sizeof (struct stab_section_list)));
247 for (pn = &csi->stabsects; *pn != NULL; pn = &(*pn)->next)
251 /* This will be run after coffstab_build_psymtabs is called
252 in coff_symfile_read, at which point we no longer need
254 make_cleanup (xfree, n);
259 /* Return the section_offsets* that CS points to. */
260 static int cs_to_section (struct coff_symbol *, struct objfile *);
262 struct find_targ_sec_arg
269 find_targ_sec (bfd *abfd, asection *sect, void *obj)
271 struct find_targ_sec_arg *args = (struct find_targ_sec_arg *) obj;
273 if (sect->target_index == args->targ_index)
274 *args->resultp = sect;
277 /* Return the bfd_section that CS points to. */
278 static struct bfd_section*
279 cs_to_bfd_section (struct coff_symbol *cs, struct objfile *objfile)
281 asection *sect = NULL;
282 struct find_targ_sec_arg args;
284 args.targ_index = cs->c_secnum;
285 args.resultp = §
286 bfd_map_over_sections (objfile->obfd, find_targ_sec, &args);
290 /* Return the section number (SECT_OFF_*) that CS points to. */
292 cs_to_section (struct coff_symbol *cs, struct objfile *objfile)
294 asection *sect = cs_to_bfd_section (cs, objfile);
297 return SECT_OFF_TEXT (objfile);
301 /* Return the address of the section of a COFF symbol. */
303 static CORE_ADDR cs_section_address (struct coff_symbol *, bfd *);
306 cs_section_address (struct coff_symbol *cs, bfd *abfd)
308 asection *sect = NULL;
309 struct find_targ_sec_arg args;
312 args.targ_index = cs->c_secnum;
313 args.resultp = §
314 bfd_map_over_sections (abfd, find_targ_sec, &args);
316 addr = bfd_get_section_vma (objfile->obfd, sect);
320 /* Look up a coff type-number index. Return the address of the slot
321 where the type for that index is stored.
322 The type-number is in INDEX.
324 This can be used for finding the type associated with that index
325 or for associating a new type with the index. */
327 static struct type **
328 coff_lookup_type (int index)
330 if (index >= type_vector_length)
332 int old_vector_length = type_vector_length;
334 type_vector_length *= 2;
335 if (index /* is still */ >= type_vector_length)
336 type_vector_length = index * 2;
338 type_vector = (struct type **)
339 xrealloc ((char *) type_vector,
340 type_vector_length * sizeof (struct type *));
341 memset (&type_vector[old_vector_length], 0,
342 (type_vector_length - old_vector_length) * sizeof (struct type *));
344 return &type_vector[index];
347 /* Make sure there is a type allocated for type number index
348 and return the type object.
349 This can create an empty (zeroed) type object. */
352 coff_alloc_type (int index)
354 struct type **type_addr = coff_lookup_type (index);
355 struct type *type = *type_addr;
357 /* If we are referring to a type not known at all yet,
358 allocate an empty type for it.
359 We will fill it in later if we find out how. */
362 type = alloc_type (coffread_objfile);
368 /* Start a new symtab for a new source file.
369 This is called when a COFF ".file" symbol is seen;
370 it indicates the start of data for one original source file. */
373 coff_start_symtab (const char *name)
376 /* We fill in the filename later. start_symtab puts this pointer
377 into last_source_file and we put it in subfiles->name, which
378 end_symtab frees; that's why it must be malloc'd. */
380 /* We never know the directory name for COFF. */
382 /* The start address is irrelevant, since we set
383 last_source_start_addr in coff_end_symtab. */
385 record_debugformat ("COFF");
388 /* Save the vital information from when starting to read a file,
389 for use when closing off the current file.
390 NAME is the file name the symbols came from, START_ADDR is the
391 first text address for the file, and SIZE is the number of bytes of
395 complete_symtab (const char *name, CORE_ADDR start_addr, unsigned int size)
397 if (last_source_file != NULL)
398 xfree (last_source_file);
399 last_source_file = xstrdup (name);
400 current_source_start_addr = start_addr;
401 current_source_end_addr = start_addr + size;
404 /* Finish the symbol definitions for one main source file, close off
405 all the lexical contexts for that file (creating struct block's for
406 them), then make the struct symtab for that file and put it in the
410 coff_end_symtab (struct objfile *objfile)
412 last_source_start_addr = current_source_start_addr;
414 end_symtab (current_source_end_addr, objfile,
415 SECT_OFF_TEXT (objfile));
417 /* Reinitialize for beginning of new file. */
418 last_source_file = NULL;
421 static struct minimal_symbol *
422 record_minimal_symbol (struct coff_symbol *cs, CORE_ADDR address,
423 enum minimal_symbol_type type, int section,
424 struct objfile *objfile)
426 struct bfd_section *bfd_section;
428 /* We don't want TDESC entry points in the minimal symbol table. */
429 if (cs->c_name[0] == '@')
432 bfd_section = cs_to_bfd_section (cs, objfile);
433 return prim_record_minimal_symbol_and_info (cs->c_name, address,
435 bfd_section, objfile);
438 /* coff_symfile_init ()
439 is the coff-specific initialization routine for reading symbols.
440 It is passed a struct objfile which contains, among other things,
441 the BFD for the file whose symbols are being read, and a slot for
442 a pointer to "private data" which we fill with cookies and other
443 treats for coff_symfile_read ().
445 We will only be called if this is a COFF or COFF-like file. BFD
446 handles figuring out the format of the file, and code in symtab.c
447 uses BFD's determination to vector to us.
449 The ultimate result is a new symtab (or, FIXME, eventually a
453 coff_symfile_init (struct objfile *objfile)
455 /* Allocate struct to keep track of stab reading. */
456 objfile->deprecated_sym_stab_info = (struct dbx_symfile_info *)
457 xmalloc (sizeof (struct dbx_symfile_info));
459 memset (objfile->deprecated_sym_stab_info, 0,
460 sizeof (struct dbx_symfile_info));
462 /* Allocate struct to keep track of the symfile. */
463 objfile->deprecated_sym_private
464 = xmalloc (sizeof (struct coff_symfile_info));
466 memset (objfile->deprecated_sym_private, 0,
467 sizeof (struct coff_symfile_info));
469 /* COFF objects may be reordered, so set OBJF_REORDERED. If we
470 find this causes a significant slowdown in gdb then we could
471 set it in the debug symbol readers only when necessary. */
472 objfile->flags |= OBJF_REORDERED;
474 init_entry_point_info (objfile);
477 /* This function is called for every section; it finds the outer
478 limits of the line table (minimum and maximum file offset) so that
479 the mainline code can read the whole thing for efficiency. */
482 find_linenos (bfd *abfd, struct bfd_section *asect, void *vpinfo)
484 struct coff_symfile_info *info;
486 file_ptr offset, maxoff;
488 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
489 count = asect->lineno_count;
490 /* End of warning. */
494 size = count * local_linesz;
496 info = (struct coff_symfile_info *) vpinfo;
497 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
498 offset = asect->line_filepos;
499 /* End of warning. */
501 if (offset < info->min_lineno_offset || info->min_lineno_offset == 0)
502 info->min_lineno_offset = offset;
504 maxoff = offset + size;
505 if (maxoff > info->max_lineno_offset)
506 info->max_lineno_offset = maxoff;
510 /* The BFD for this file -- only good while we're actively reading
511 symbols into a psymtab or a symtab. */
513 static bfd *symfile_bfd;
515 /* Read a symbol file, after initialization by coff_symfile_init. */
518 coff_symfile_read (struct objfile *objfile, int symfile_flags)
520 struct coff_symfile_info *info;
521 struct dbx_symfile_info *dbxinfo;
522 bfd *abfd = objfile->obfd;
523 coff_data_type *cdata = coff_data (abfd);
524 char *name = bfd_get_filename (abfd);
526 unsigned int num_symbols;
528 int stringtab_offset;
529 struct cleanup *back_to, *cleanup_minimal_symbols;
532 info = (struct coff_symfile_info *) objfile->deprecated_sym_private;
533 dbxinfo = objfile->deprecated_sym_stab_info;
534 symfile_bfd = abfd; /* Kludge for swap routines. */
536 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
537 num_symbols = bfd_get_symcount (abfd); /* How many syms */
538 symtab_offset = cdata->sym_filepos; /* Symbol table file offset */
539 stringtab_offset = symtab_offset + /* String table file offset */
540 num_symbols * cdata->local_symesz;
542 /* Set a few file-statics that give us specific information about
543 the particular COFF file format we're reading. */
544 local_n_btmask = cdata->local_n_btmask;
545 local_n_btshft = cdata->local_n_btshft;
546 local_n_tmask = cdata->local_n_tmask;
547 local_n_tshift = cdata->local_n_tshift;
548 local_linesz = cdata->local_linesz;
549 local_symesz = cdata->local_symesz;
550 local_auxesz = cdata->local_auxesz;
552 /* Allocate space for raw symbol and aux entries, based on their
553 space requirements as reported by BFD. */
554 temp_sym = (char *) xmalloc
555 (cdata->local_symesz + cdata->local_auxesz);
556 temp_aux = temp_sym + cdata->local_symesz;
557 back_to = make_cleanup (free_current_contents, &temp_sym);
559 /* We need to know whether this is a PE file, because in PE files,
560 unlike standard COFF files, symbol values are stored as offsets
561 from the section address, rather than as absolute addresses.
562 FIXME: We should use BFD to read the symbol table, and thus avoid
565 strncmp (bfd_get_target (objfile->obfd), "pe", 2) == 0
566 || strncmp (bfd_get_target (objfile->obfd), "epoc-pe", 7) == 0;
568 /* End of warning. */
570 info->min_lineno_offset = 0;
571 info->max_lineno_offset = 0;
573 /* Only read line number information if we have symbols.
575 On Windows NT, some of the system's DLL's have sections with
576 PointerToLinenumbers fields that are non-zero, but point at
577 random places within the image file. (In the case I found,
578 KERNEL32.DLL's .text section has a line number info pointer that
579 points into the middle of the string `lib\\i386\kernel32.dll'.)
581 However, these DLL's also have no symbols. The line number
582 tables are meaningless without symbols. And in fact, GDB never
583 uses the line number information unless there are symbols. So we
584 can avoid spurious error messages (and maybe run a little
585 faster!) by not even reading the line number table unless we have
589 /* Read the line number table, all at once. */
590 bfd_map_over_sections (abfd, find_linenos, (void *) info);
592 make_cleanup (free_linetab_cleanup, 0 /*ignore*/);
593 val = init_lineno (abfd, info->min_lineno_offset,
594 info->max_lineno_offset - info->min_lineno_offset);
596 error (_("\"%s\": error reading line numbers."), name);
599 /* Now read the string table, all at once. */
601 make_cleanup (free_stringtab_cleanup, 0 /*ignore*/);
602 val = init_stringtab (abfd, stringtab_offset);
604 error (_("\"%s\": can't get string table"), name);
606 init_minimal_symbol_collection ();
607 cleanup_minimal_symbols = make_cleanup_discard_minimal_symbols ();
609 /* Now that the executable file is positioned at symbol table,
610 process it and define symbols accordingly. */
612 coff_symtab_read ((long) symtab_offset, num_symbols, objfile);
614 /* Install any minimal symbols that have been collected as the
615 current minimal symbols for this objfile. */
617 install_minimal_symbols (objfile);
619 /* Free the installed minimal symbol data. */
620 do_cleanups (cleanup_minimal_symbols);
622 bfd_map_over_sections (abfd, coff_locate_sections, (void *) info);
626 if (!info->stabstrsect)
628 error (_("The debugging information in `%s' is corrupted.\nThe "
629 "file has a `.stabs' section, but no `.stabstr' section."),
633 /* FIXME: dubious. Why can't we use something normal like
634 bfd_get_section_contents? */
635 bfd_seek (abfd, abfd->where, 0);
637 stabstrsize = bfd_section_size (abfd, info->stabstrsect);
639 coffstab_build_psymtabs (objfile,
640 info->textaddr, info->textsize,
642 info->stabstrsect->filepos, stabstrsize);
644 if (dwarf2_has_info (objfile, NULL))
646 /* DWARF2 sections. */
647 dwarf2_build_psymtabs (objfile);
650 dwarf2_build_frame_info (objfile);
652 /* Try to add separate debug file if no symbols table found. */
653 if (!objfile_has_partial_symbols (objfile))
657 debugfile = find_separate_debug_file_by_debuglink (objfile);
661 bfd *abfd = symfile_bfd_open (debugfile);
663 symbol_file_add_separate (abfd, symfile_flags, objfile);
668 do_cleanups (back_to);
672 coff_new_init (struct objfile *ignore)
676 /* Perform any local cleanups required when we are done with a
677 particular objfile. I.E, we are in the process of discarding all
678 symbol information for an objfile, freeing up all memory held for
679 it, and unlinking the objfile struct from the global list of known
683 coff_symfile_finish (struct objfile *objfile)
685 if (objfile->deprecated_sym_private != NULL)
687 xfree (objfile->deprecated_sym_private);
690 /* Let stabs reader clean up. */
691 stabsread_clear_cache ();
693 dwarf2_free_objfile (objfile);
697 /* Given pointers to a symbol table in coff style exec file,
698 analyze them and create struct symtab's describing the symbols.
699 NSYMS is the number of symbols in the symbol table.
700 We read them one at a time using read_one_sym (). */
703 coff_symtab_read (long symtab_offset, unsigned int nsyms,
704 struct objfile *objfile)
706 struct gdbarch *gdbarch = get_objfile_arch (objfile);
707 struct context_stack *new;
708 struct coff_symbol coff_symbol;
709 struct coff_symbol *cs = &coff_symbol;
710 static struct internal_syment main_sym;
711 static union internal_auxent main_aux;
712 struct coff_symbol fcn_cs_saved;
713 static struct internal_syment fcn_sym_saved;
714 static union internal_auxent fcn_aux_saved;
716 /* A .file is open. */
717 int in_source_file = 0;
718 int next_file_symnum = -1;
719 /* Name of the current file. */
720 const char *filestring = "";
722 int fcn_first_line = 0;
723 CORE_ADDR fcn_first_line_addr = 0;
724 int fcn_last_line = 0;
725 int fcn_start_addr = 0;
726 long fcn_line_ptr = 0;
729 struct minimal_symbol *msym;
731 /* Work around a stdio bug in SunOS4.1.1 (this makes me nervous....
732 it's hard to know I've really worked around it. The fix should
733 be harmless, anyway). The symptom of the bug is that the first
734 fread (in read_one_sym), will (in my example) actually get data
735 from file offset 268, when the fseek was to 264 (and ftell shows
736 264). This causes all hell to break loose. I was unable to
737 reproduce this on a short test program which operated on the same
738 file, performing (I think) the same sequence of operations.
740 It stopped happening when I put in this (former) rewind().
742 FIXME: Find out if this has been reported to Sun, whether it has
743 been fixed in a later release, etc. */
745 bfd_seek (objfile->obfd, 0, 0);
747 /* Position to read the symbol table. */
748 val = bfd_seek (objfile->obfd, (long) symtab_offset, 0);
750 perror_with_name (objfile->name);
752 coffread_objfile = objfile;
753 nlist_bfd_global = objfile->obfd;
754 nlist_nsyms_global = nsyms;
755 last_source_file = NULL;
756 memset (opaque_type_chain, 0, sizeof opaque_type_chain);
758 if (type_vector) /* Get rid of previous one. */
760 type_vector_length = 160;
761 type_vector = (struct type **)
762 xmalloc (type_vector_length * sizeof (struct type *));
763 memset (type_vector, 0, type_vector_length * sizeof (struct type *));
765 coff_start_symtab ("");
768 while (symnum < nsyms)
770 QUIT; /* Make this command interruptable. */
772 read_one_sym (cs, &main_sym, &main_aux);
774 if (cs->c_symnum == next_file_symnum && cs->c_sclass != C_FILE)
776 if (last_source_file)
777 coff_end_symtab (objfile);
779 coff_start_symtab ("_globals_");
780 /* coff_start_symtab will set the language of this symtab to
781 language_unknown, since such a ``file name'' is not
782 recognized. Override that with the minimal language to
783 allow printing values in this symtab. */
784 current_subfile->language = language_minimal;
785 complete_symtab ("_globals_", 0, 0);
786 /* Done with all files, everything from here on out is
790 /* Special case for file with type declarations only, no
792 if (!last_source_file && SDB_TYPE (cs->c_type)
793 && cs->c_secnum == N_DEBUG)
794 complete_symtab (filestring, 0, 0);
796 /* Typedefs should not be treated as symbol definitions. */
797 if (ISFCN (cs->c_type) && cs->c_sclass != C_TPDEF)
799 /* Record all functions -- external and static -- in
801 int section = cs_to_section (cs, objfile);
803 tmpaddr = cs->c_value + ANOFFSET (objfile->section_offsets,
804 SECT_OFF_TEXT (objfile));
805 record_minimal_symbol (cs, tmpaddr, mst_text,
808 fcn_line_ptr = main_aux.x_sym.x_fcnary.x_fcn.x_lnnoptr;
809 fcn_start_addr = tmpaddr;
811 fcn_sym_saved = main_sym;
812 fcn_aux_saved = main_aux;
816 switch (cs->c_sclass)
825 complaint (&symfile_complaints,
826 _("Bad n_sclass for symbol %s"),
831 /* c_value field contains symnum of next .file entry in
832 table or symnum of first global after last .file. */
833 next_file_symnum = cs->c_value;
835 filestring = coff_getfilename (&main_aux);
839 /* Complete symbol table for last object file
840 containing debugging information. */
841 if (last_source_file)
843 coff_end_symtab (objfile);
844 coff_start_symtab (filestring);
849 /* C_LABEL is used for labels and static functions.
850 Including it here allows gdb to see static functions when
851 no debug info is available. */
853 /* However, labels within a function can make weird
854 backtraces, so filter them out (from phdm@macqel.be). */
860 case C_THUMBSTATFUNC:
861 if (cs->c_name[0] == '.')
863 if (strcmp (cs->c_name, ".text") == 0)
865 /* FIXME: don't wire in ".text" as section name or
867 /* Check for in_source_file deals with case of a
868 file with debugging symbols followed by a later
869 file with no symbols. */
871 complete_symtab (filestring,
872 cs->c_value + ANOFFSET (objfile->section_offsets,
873 SECT_OFF_TEXT (objfile)),
874 main_aux.x_scn.x_scnlen);
877 /* Flush rest of '.' symbols. */
880 else if (!SDB_TYPE (cs->c_type)
881 && cs->c_name[0] == 'L'
882 && (strncmp (cs->c_name, "LI%", 3) == 0
883 || strncmp (cs->c_name, "LF%", 3) == 0
884 || strncmp (cs->c_name, "LC%", 3) == 0
885 || strncmp (cs->c_name, "LP%", 3) == 0
886 || strncmp (cs->c_name, "LPB%", 4) == 0
887 || strncmp (cs->c_name, "LBB%", 4) == 0
888 || strncmp (cs->c_name, "LBE%", 4) == 0
889 || strncmp (cs->c_name, "LPBX%", 5) == 0))
890 /* At least on a 3b1, gcc generates swbeg and string labels
891 that look like this. Ignore them. */
893 /* Fall in for static symbols that don't start with '.' */
898 /* Record it in the minimal symbols regardless of
899 SDB_TYPE. This parallels what we do for other debug
900 formats, and probably is needed to make
901 print_address_symbolic work right without the (now
902 gone) "set fast-symbolic-addr off" kludge. */
904 enum minimal_symbol_type ms_type;
907 if (cs->c_secnum == N_UNDEF)
909 /* This is a common symbol. We used to rely on
910 the target to tell us whether it knows where
911 the symbol has been relocated to, but none of
912 the target implementations actually provided
913 that operation. So we just ignore the symbol,
914 the same way we would do if we had a target-side
915 symbol lookup which returned no match. */
918 else if (cs->c_secnum == N_ABS)
920 /* Use the correct minimal symbol type (and don't
921 relocate) for absolute values. */
923 sec = cs_to_section (cs, objfile);
924 tmpaddr = cs->c_value;
928 asection *bfd_section = cs_to_bfd_section (cs, objfile);
930 sec = cs_to_section (cs, objfile);
931 tmpaddr = cs->c_value;
932 /* Statics in a PE file also get relocated. */
933 if (cs->c_sclass == C_EXT
934 || cs->c_sclass == C_THUMBEXTFUNC
935 || cs->c_sclass == C_THUMBEXT
936 || (pe_file && (cs->c_sclass == C_STAT)))
937 tmpaddr += ANOFFSET (objfile->section_offsets, sec);
939 if (bfd_section->flags & SEC_CODE)
942 cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXTFUNC
943 || cs->c_sclass == C_THUMBEXT ?
944 mst_text : mst_file_text;
945 tmpaddr = gdbarch_smash_text_address (gdbarch, tmpaddr);
947 else if (bfd_section->flags & SEC_ALLOC
948 && bfd_section->flags & SEC_LOAD)
951 cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXT
952 ? mst_data : mst_file_data;
954 else if (bfd_section->flags & SEC_ALLOC)
957 cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXT
958 ? mst_bss : mst_file_bss;
961 ms_type = mst_unknown;
964 msym = record_minimal_symbol (cs, tmpaddr, ms_type,
967 gdbarch_coff_make_msymbol_special (gdbarch,
970 if (SDB_TYPE (cs->c_type))
974 sym = process_coff_symbol
975 (cs, &main_aux, objfile);
976 SYMBOL_VALUE (sym) = tmpaddr;
977 SYMBOL_SECTION (sym) = sec;
983 if (strcmp (cs->c_name, ".bf") == 0)
987 /* Value contains address of first non-init type
989 /* main_aux.x_sym.x_misc.x_lnsz.x_lnno
990 contains line number of '{' }. */
992 complaint (&symfile_complaints,
993 _("`.bf' symbol %d has no aux entry"),
995 fcn_first_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
996 fcn_first_line_addr = cs->c_value;
998 /* Might want to check that locals are 0 and
999 context_stack_depth is zero, and complain if not. */
1002 new = push_context (depth, fcn_start_addr);
1003 fcn_cs_saved.c_name = getsymname (&fcn_sym_saved);
1005 process_coff_symbol (&fcn_cs_saved,
1006 &fcn_aux_saved, objfile);
1008 else if (strcmp (cs->c_name, ".ef") == 0)
1010 if (!within_function)
1011 error (_("Bad coff function information."));
1012 /* The value of .ef is the address of epilogue code;
1013 not useful for gdb. */
1014 /* { main_aux.x_sym.x_misc.x_lnsz.x_lnno
1015 contains number of lines to '}' */
1017 if (context_stack_depth <= 0)
1018 { /* We attempted to pop an empty context stack. */
1019 complaint (&symfile_complaints,
1020 _("`.ef' symbol without matching `.bf' "
1021 "symbol ignored starting at symnum %d"),
1023 within_function = 0;
1027 new = pop_context ();
1028 /* Stack must be empty now. */
1029 if (context_stack_depth > 0 || new == NULL)
1031 complaint (&symfile_complaints,
1032 _("Unmatched .ef symbol(s) ignored "
1033 "starting at symnum %d"),
1035 within_function = 0;
1038 if (cs->c_naux != 1)
1040 complaint (&symfile_complaints,
1041 _("`.ef' symbol %d has no aux entry"),
1043 fcn_last_line = 0x7FFFFFFF;
1047 fcn_last_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
1049 /* fcn_first_line is the line number of the opening '{'.
1050 Do not record it - because it would affect gdb's idea
1051 of the line number of the first statement of the
1052 function - except for one-line functions, for which
1053 it is also the line number of all the statements and
1054 of the closing '}', and for which we do not have any
1055 other statement-line-number. */
1056 if (fcn_last_line == 1)
1057 record_line (current_subfile, fcn_first_line,
1058 gdbarch_addr_bits_remove (gdbarch,
1059 fcn_first_line_addr));
1061 enter_linenos (fcn_line_ptr, fcn_first_line,
1062 fcn_last_line, objfile);
1064 finish_block (new->name, &local_symbols,
1065 new->old_blocks, new->start_addr,
1066 fcn_cs_saved.c_value
1067 + fcn_aux_saved.x_sym.x_misc.x_fsize
1068 + ANOFFSET (objfile->section_offsets,
1069 SECT_OFF_TEXT (objfile)),
1072 within_function = 0;
1077 if (strcmp (cs->c_name, ".bb") == 0)
1079 tmpaddr = cs->c_value;
1080 tmpaddr += ANOFFSET (objfile->section_offsets,
1081 SECT_OFF_TEXT (objfile));
1082 push_context (++depth, tmpaddr);
1084 else if (strcmp (cs->c_name, ".eb") == 0)
1086 if (context_stack_depth <= 0)
1087 { /* We attempted to pop an empty context stack. */
1088 complaint (&symfile_complaints,
1089 _("`.eb' symbol without matching `.bb' "
1090 "symbol ignored starting at symnum %d"),
1095 new = pop_context ();
1096 if (depth-- != new->depth)
1098 complaint (&symfile_complaints,
1099 _("Mismatched .eb symbol ignored "
1100 "starting at symnum %d"),
1104 if (local_symbols && context_stack_depth > 0)
1107 cs->c_value + ANOFFSET (objfile->section_offsets,
1108 SECT_OFF_TEXT (objfile));
1109 /* Make a block for the local symbols within. */
1110 finish_block (0, &local_symbols, new->old_blocks,
1111 new->start_addr, tmpaddr, objfile);
1113 /* Now pop locals of block just finished. */
1114 local_symbols = new->locals;
1119 process_coff_symbol (cs, &main_aux, objfile);
1124 if ((nsyms == 0) && (pe_file))
1126 /* We've got no debugging symbols, but it's a portable
1127 executable, so try to read the export table. */
1128 read_pe_exported_syms (objfile);
1131 if (last_source_file)
1132 coff_end_symtab (objfile);
1134 /* Patch up any opaque types (references to types that are not defined
1135 in the file where they are referenced, e.g. "struct foo *bar"). */
1136 ALL_OBJFILE_SYMTABS (objfile, s)
1137 patch_opaque_types (s);
1139 coffread_objfile = NULL;
1142 /* Routines for reading headers and symbols from executable. */
1144 /* Read the next symbol, swap it, and return it in both
1145 internal_syment form, and coff_symbol form. Also return its first
1146 auxent, if any, in internal_auxent form, and skip any other
1150 read_one_sym (struct coff_symbol *cs,
1151 struct internal_syment *sym,
1152 union internal_auxent *aux)
1155 bfd_size_type bytes;
1157 cs->c_symnum = symnum;
1158 bytes = bfd_bread (temp_sym, local_symesz, nlist_bfd_global);
1159 if (bytes != local_symesz)
1160 error (_("%s: error reading symbols"), coffread_objfile->name);
1161 bfd_coff_swap_sym_in (symfile_bfd, temp_sym, (char *) sym);
1162 cs->c_naux = sym->n_numaux & 0xff;
1163 if (cs->c_naux >= 1)
1165 bytes = bfd_bread (temp_aux, local_auxesz, nlist_bfd_global);
1166 if (bytes != local_auxesz)
1167 error (_("%s: error reading symbols"), coffread_objfile->name);
1168 bfd_coff_swap_aux_in (symfile_bfd, temp_aux,
1169 sym->n_type, sym->n_sclass,
1170 0, cs->c_naux, (char *) aux);
1171 /* If more than one aux entry, read past it (only the first aux
1173 for (i = 1; i < cs->c_naux; i++)
1175 bytes = bfd_bread (temp_aux, local_auxesz, nlist_bfd_global);
1176 if (bytes != local_auxesz)
1177 error (_("%s: error reading symbols"), coffread_objfile->name);
1180 cs->c_name = getsymname (sym);
1181 cs->c_value = sym->n_value;
1182 cs->c_sclass = (sym->n_sclass & 0xff);
1183 cs->c_secnum = sym->n_scnum;
1184 cs->c_type = (unsigned) sym->n_type;
1185 if (!SDB_TYPE (cs->c_type))
1189 if (cs->c_sclass & 128)
1190 printf (_("thumb symbol %s, class 0x%x\n"), cs->c_name, cs->c_sclass);
1193 symnum += 1 + cs->c_naux;
1195 /* The PE file format stores symbol values as offsets within the
1196 section, rather than as absolute addresses. We correct that
1197 here, if the symbol has an appropriate storage class. FIXME: We
1198 should use BFD to read the symbols, rather than duplicating the
1202 switch (cs->c_sclass)
1206 case C_THUMBEXTFUNC:
1211 case C_THUMBSTATFUNC:
1217 if (cs->c_secnum != 0)
1218 cs->c_value += cs_section_address (cs, symfile_bfd);
1224 /* Support for string table handling. */
1226 static char *stringtab = NULL;
1229 init_stringtab (bfd *abfd, long offset)
1233 unsigned char lengthbuf[4];
1237 /* If the file is stripped, the offset might be zero, indicating no
1238 string table. Just return with `stringtab' set to null. */
1242 if (bfd_seek (abfd, offset, 0) < 0)
1245 val = bfd_bread ((char *) lengthbuf, sizeof lengthbuf, abfd);
1246 length = bfd_h_get_32 (symfile_bfd, lengthbuf);
1248 /* If no string table is needed, then the file may end immediately
1249 after the symbols. Just return with `stringtab' set to null. */
1250 if (val != sizeof lengthbuf || length < sizeof lengthbuf)
1253 stringtab = (char *) xmalloc (length);
1254 /* This is in target format (probably not very useful, and not
1255 currently used), not host format. */
1256 memcpy (stringtab, lengthbuf, sizeof lengthbuf);
1257 if (length == sizeof length) /* Empty table -- just the count. */
1260 val = bfd_bread (stringtab + sizeof lengthbuf,
1261 length - sizeof lengthbuf, abfd);
1262 if (val != length - sizeof lengthbuf || stringtab[length - 1] != '\0')
1269 free_stringtab (void)
1277 free_stringtab_cleanup (void *ignore)
1283 getsymname (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.
1304 Return only the last component of the name. Result is in static
1305 storage and is only good for temporary use. */
1308 coff_getfilename (union internal_auxent *aux_entry)
1310 static char buffer[BUFSIZ];
1314 if (aux_entry->x_file.x_n.x_zeroes == 0)
1316 if (strlen (stringtab + aux_entry->x_file.x_n.x_offset) >= BUFSIZ)
1317 internal_error (__FILE__, __LINE__, _("coff file name too long"));
1318 strcpy (buffer, stringtab + aux_entry->x_file.x_n.x_offset);
1322 strncpy (buffer, aux_entry->x_file.x_fname, FILNMLEN);
1323 buffer[FILNMLEN] = '\0';
1327 /* FIXME: We should not be throwing away the information about what
1328 directory. It should go into dirname of the symtab, or some such
1330 result = lbasename (result);
1334 /* Support for line number handling. */
1336 static char *linetab = NULL;
1337 static long linetab_offset;
1338 static unsigned long linetab_size;
1340 /* Read in all the line numbers for fast lookups later. Leave them in
1341 external (unswapped) format in memory; we'll swap them as we enter
1342 them into GDB's data structures. */
1345 init_lineno (bfd *abfd, long offset, int size)
1349 linetab_offset = offset;
1350 linetab_size = size;
1357 if (bfd_seek (abfd, offset, 0) < 0)
1360 /* Allocate the desired table, plus a sentinel. */
1361 linetab = (char *) xmalloc (size + local_linesz);
1363 val = bfd_bread (linetab, size, abfd);
1367 /* Terminate it with an all-zero sentinel record. */
1368 memset (linetab + size, 0, local_linesz);
1382 free_linetab_cleanup (void *ignore)
1387 #if !defined (L_LNNO32)
1388 #define L_LNNO32(lp) ((lp)->l_lnno)
1392 enter_linenos (long file_offset, int first_line,
1393 int last_line, struct objfile *objfile)
1395 struct gdbarch *gdbarch = get_objfile_arch (objfile);
1397 struct internal_lineno lptr;
1401 if (file_offset < linetab_offset)
1403 complaint (&symfile_complaints,
1404 _("Line number pointer %ld lower than start of line numbers"),
1406 if (file_offset > linetab_size) /* Too big to be an offset? */
1408 file_offset += linetab_offset; /* Try reading at that linetab
1412 rawptr = &linetab[file_offset - linetab_offset];
1414 /* Skip first line entry for each function. */
1415 rawptr += local_linesz;
1416 /* Line numbers start at one for the first line of the function. */
1419 /* If the line number table is full (e.g. 64K lines in COFF debug
1420 info), the next function's L_LNNO32 might not be zero, so don't
1421 overstep the table's end in any case. */
1422 while (rawptr <= &linetab[0] + linetab_size)
1424 bfd_coff_swap_lineno_in (symfile_bfd, rawptr, &lptr);
1425 rawptr += local_linesz;
1426 /* The next function, or the sentinel, will have L_LNNO32 zero;
1428 if (L_LNNO32 (&lptr) && L_LNNO32 (&lptr) <= last_line)
1430 CORE_ADDR addr = lptr.l_addr.l_paddr;
1431 addr += ANOFFSET (objfile->section_offsets,
1432 SECT_OFF_TEXT (objfile));
1433 record_line (current_subfile,
1434 first_line + L_LNNO32 (&lptr),
1435 gdbarch_addr_bits_remove (gdbarch, addr));
1443 patch_type (struct type *type, struct type *real_type)
1445 struct type *target = TYPE_TARGET_TYPE (type);
1446 struct type *real_target = TYPE_TARGET_TYPE (real_type);
1447 int field_size = TYPE_NFIELDS (real_target) * sizeof (struct field);
1449 TYPE_LENGTH (target) = TYPE_LENGTH (real_target);
1450 TYPE_NFIELDS (target) = TYPE_NFIELDS (real_target);
1451 TYPE_FIELDS (target) = (struct field *) TYPE_ALLOC (target,
1454 memcpy (TYPE_FIELDS (target),
1455 TYPE_FIELDS (real_target),
1458 if (TYPE_NAME (real_target))
1460 if (TYPE_NAME (target))
1461 xfree (TYPE_NAME (target));
1462 TYPE_NAME (target) = concat (TYPE_NAME (real_target),
1467 /* Patch up all appropriate typedef symbols in the opaque_type_chains
1468 so that they can be used to print out opaque data structures
1472 patch_opaque_types (struct symtab *s)
1475 struct dict_iterator iter;
1476 struct symbol *real_sym;
1478 /* Go through the per-file symbols only. */
1479 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
1480 ALL_BLOCK_SYMBOLS (b, iter, real_sym)
1482 /* Find completed typedefs to use to fix opaque ones.
1483 Remove syms from the chain when their types are stored,
1484 but search the whole chain, as there may be several syms
1485 from different files with the same name. */
1486 if (SYMBOL_CLASS (real_sym) == LOC_TYPEDEF
1487 && SYMBOL_DOMAIN (real_sym) == VAR_DOMAIN
1488 && TYPE_CODE (SYMBOL_TYPE (real_sym)) == TYPE_CODE_PTR
1489 && TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (real_sym))) != 0)
1491 char *name = SYMBOL_LINKAGE_NAME (real_sym);
1492 int hash = hashname (name);
1493 struct symbol *sym, *prev;
1496 for (sym = opaque_type_chain[hash]; sym;)
1498 if (name[0] == SYMBOL_LINKAGE_NAME (sym)[0]
1499 && strcmp (name + 1, SYMBOL_LINKAGE_NAME (sym) + 1) == 0)
1503 SYMBOL_VALUE_CHAIN (prev) = SYMBOL_VALUE_CHAIN (sym);
1507 opaque_type_chain[hash] = SYMBOL_VALUE_CHAIN (sym);
1510 patch_type (SYMBOL_TYPE (sym), SYMBOL_TYPE (real_sym));
1514 sym = SYMBOL_VALUE_CHAIN (prev);
1518 sym = opaque_type_chain[hash];
1524 sym = SYMBOL_VALUE_CHAIN (sym);
1532 coff_reg_to_regnum (struct symbol *sym, struct gdbarch *gdbarch)
1534 return gdbarch_sdb_reg_to_regnum (gdbarch, SYMBOL_VALUE (sym));
1537 static const struct symbol_register_ops coff_register_funcs = {
1541 static struct symbol *
1542 process_coff_symbol (struct coff_symbol *cs,
1543 union internal_auxent *aux,
1544 struct objfile *objfile)
1547 = (struct symbol *) obstack_alloc (&objfile->objfile_obstack,
1548 sizeof (struct symbol));
1551 memset (sym, 0, sizeof (struct symbol));
1553 name = EXTERNAL_NAME (name, objfile->obfd);
1554 SYMBOL_SET_LANGUAGE (sym, current_subfile->language);
1555 SYMBOL_SET_NAMES (sym, name, strlen (name), 1, objfile);
1557 /* default assumptions */
1558 SYMBOL_VALUE (sym) = cs->c_value;
1559 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
1560 SYMBOL_SECTION (sym) = cs_to_section (cs, objfile);
1562 if (ISFCN (cs->c_type))
1564 SYMBOL_VALUE (sym) += ANOFFSET (objfile->section_offsets,
1565 SECT_OFF_TEXT (objfile));
1567 lookup_function_type (decode_function_type (cs, cs->c_type,
1570 SYMBOL_CLASS (sym) = LOC_BLOCK;
1571 if (cs->c_sclass == C_STAT || cs->c_sclass == C_THUMBSTAT
1572 || cs->c_sclass == C_THUMBSTATFUNC)
1573 add_symbol_to_list (sym, &file_symbols);
1574 else if (cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXT
1575 || cs->c_sclass == C_THUMBEXTFUNC)
1576 add_symbol_to_list (sym, &global_symbols);
1580 SYMBOL_TYPE (sym) = decode_type (cs, cs->c_type, aux, objfile);
1581 switch (cs->c_sclass)
1587 SYMBOL_CLASS (sym) = LOC_LOCAL;
1588 add_symbol_to_list (sym, &local_symbols);
1592 case C_THUMBEXTFUNC:
1594 SYMBOL_CLASS (sym) = LOC_STATIC;
1595 SYMBOL_VALUE_ADDRESS (sym) = (CORE_ADDR) cs->c_value;
1596 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
1597 SECT_OFF_TEXT (objfile));
1598 add_symbol_to_list (sym, &global_symbols);
1602 case C_THUMBSTATFUNC:
1604 SYMBOL_CLASS (sym) = LOC_STATIC;
1605 SYMBOL_VALUE_ADDRESS (sym) = (CORE_ADDR) cs->c_value;
1606 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
1607 SECT_OFF_TEXT (objfile));
1608 if (within_function)
1610 /* Static symbol of local scope. */
1611 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_REGISTER_OPS (sym) = &coff_register_funcs;
1626 SYMBOL_VALUE (sym) = cs->c_value;
1627 add_symbol_to_list (sym, &local_symbols);
1635 SYMBOL_CLASS (sym) = LOC_ARG;
1636 SYMBOL_IS_ARGUMENT (sym) = 1;
1637 add_symbol_to_list (sym, &local_symbols);
1641 SYMBOL_CLASS (sym) = LOC_REGISTER;
1642 SYMBOL_REGISTER_OPS (sym) = &coff_register_funcs;
1643 SYMBOL_IS_ARGUMENT (sym) = 1;
1644 SYMBOL_VALUE (sym) = cs->c_value;
1645 add_symbol_to_list (sym, &local_symbols);
1649 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
1650 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
1652 /* If type has no name, give it one. */
1653 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
1655 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_PTR
1656 || TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_FUNC)
1658 /* If we are giving a name to a type such as
1659 "pointer to foo" or "function returning foo", we
1660 better not set the TYPE_NAME. If the program
1661 contains "typedef char *caddr_t;", we don't want
1662 all variables of type char * to print as caddr_t.
1663 This is not just a consequence of GDB's type
1664 management; CC and GCC (at least through version
1665 2.4) both output variables of either type char *
1666 or caddr_t with the type refering to the C_TPDEF
1667 symbol for caddr_t. If a future compiler cleans
1668 this up it GDB is not ready for it yet, but if it
1669 becomes ready we somehow need to disable this
1670 check (without breaking the PCC/GCC2.4 case).
1674 Fortunately, this check seems not to be necessary
1675 for anything except pointers or functions. */
1679 TYPE_NAME (SYMBOL_TYPE (sym)) =
1680 concat (SYMBOL_LINKAGE_NAME (sym), (char *) NULL);
1683 /* Keep track of any type which points to empty structured
1684 type, so it can be filled from a definition from another
1685 file. A simple forward reference (TYPE_CODE_UNDEF) is
1686 not an empty structured type, though; the forward
1687 references work themselves out via the magic of
1688 coff_lookup_type. */
1689 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_PTR
1690 && TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym))) == 0
1691 && TYPE_CODE (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym)))
1694 int i = hashname (SYMBOL_LINKAGE_NAME (sym));
1696 SYMBOL_VALUE_CHAIN (sym) = opaque_type_chain[i];
1697 opaque_type_chain[i] = sym;
1699 add_symbol_to_list (sym, &file_symbols);
1705 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
1706 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
1708 /* Some compilers try to be helpful by inventing "fake"
1709 names for anonymous enums, structures, and unions, like
1710 "~0fake" or ".0fake". Thanks, but no thanks... */
1711 if (TYPE_TAG_NAME (SYMBOL_TYPE (sym)) == 0)
1712 if (SYMBOL_LINKAGE_NAME (sym) != NULL
1713 && *SYMBOL_LINKAGE_NAME (sym) != '~'
1714 && *SYMBOL_LINKAGE_NAME (sym) != '.')
1715 TYPE_TAG_NAME (SYMBOL_TYPE (sym)) =
1716 concat (SYMBOL_LINKAGE_NAME (sym), (char *)NULL);
1718 add_symbol_to_list (sym, &file_symbols);
1728 /* Decode a coff type specifier; return the type that is meant. */
1730 static struct type *
1731 decode_type (struct coff_symbol *cs, unsigned int c_type,
1732 union internal_auxent *aux, struct objfile *objfile)
1734 struct type *type = 0;
1735 unsigned int new_c_type;
1737 if (c_type & ~N_BTMASK)
1739 new_c_type = DECREF (c_type);
1742 type = decode_type (cs, new_c_type, aux, objfile);
1743 type = lookup_pointer_type (type);
1745 else if (ISFCN (c_type))
1747 type = decode_type (cs, new_c_type, aux, objfile);
1748 type = lookup_function_type (type);
1750 else if (ISARY (c_type))
1753 unsigned short *dim;
1754 struct type *base_type, *index_type, *range_type;
1756 /* Define an array type. */
1757 /* auxent refers to array, not base type. */
1758 if (aux->x_sym.x_tagndx.l == 0)
1761 /* Shift the indices down. */
1762 dim = &aux->x_sym.x_fcnary.x_ary.x_dimen[0];
1765 for (i = 0; *dim && i < DIMNUM - 1; i++, dim++)
1769 base_type = decode_type (cs, new_c_type, aux, objfile);
1770 index_type = objfile_type (objfile)->builtin_int;
1772 create_range_type ((struct type *) NULL,
1773 index_type, 0, n - 1);
1775 create_array_type ((struct type *) NULL,
1776 base_type, range_type);
1781 /* Reference to existing type. This only occurs with the struct,
1782 union, and enum types. EPI a29k coff fakes us out by producing
1783 aux entries with a nonzero x_tagndx for definitions of structs,
1784 unions, and enums, so we have to check the c_sclass field. SCO
1785 3.2v4 cc gets confused with pointers to pointers to defined
1786 structs, and generates negative x_tagndx fields. */
1787 if (cs->c_naux > 0 && aux->x_sym.x_tagndx.l != 0)
1789 if (cs->c_sclass != C_STRTAG
1790 && cs->c_sclass != C_UNTAG
1791 && cs->c_sclass != C_ENTAG
1792 && aux->x_sym.x_tagndx.l >= 0)
1794 type = coff_alloc_type (aux->x_sym.x_tagndx.l);
1799 complaint (&symfile_complaints,
1800 _("Symbol table entry for %s has bad tagndx value"),
1802 /* And fall through to decode_base_type... */
1806 return decode_base_type (cs, BTYPE (c_type), aux, objfile);
1809 /* Decode a coff type specifier for function definition;
1810 return the type that the function returns. */
1812 static struct type *
1813 decode_function_type (struct coff_symbol *cs,
1814 unsigned int c_type,
1815 union internal_auxent *aux,
1816 struct objfile *objfile)
1818 if (aux->x_sym.x_tagndx.l == 0)
1819 cs->c_naux = 0; /* auxent refers to function, not base
1822 return decode_type (cs, DECREF (c_type), aux, objfile);
1825 /* Basic C types. */
1827 static struct type *
1828 decode_base_type (struct coff_symbol *cs,
1829 unsigned int c_type,
1830 union internal_auxent *aux,
1831 struct objfile *objfile)
1833 struct gdbarch *gdbarch = get_objfile_arch (objfile);
1839 /* Shows up with "void (*foo)();" structure members. */
1840 return objfile_type (objfile)->builtin_void;
1844 /* Intel 960 COFF has this symbol and meaning. */
1845 return objfile_type (objfile)->builtin_void;
1849 return objfile_type (objfile)->builtin_char;
1852 return objfile_type (objfile)->builtin_short;
1855 return objfile_type (objfile)->builtin_int;
1858 if (cs->c_sclass == C_FIELD
1859 && aux->x_sym.x_misc.x_lnsz.x_size
1860 > gdbarch_long_bit (gdbarch))
1861 return objfile_type (objfile)->builtin_long_long;
1863 return objfile_type (objfile)->builtin_long;
1866 return objfile_type (objfile)->builtin_float;
1869 return objfile_type (objfile)->builtin_double;
1872 return objfile_type (objfile)->builtin_long_double;
1875 if (cs->c_naux != 1)
1877 /* Anonymous structure type. */
1878 type = coff_alloc_type (cs->c_symnum);
1879 TYPE_CODE (type) = TYPE_CODE_STRUCT;
1880 TYPE_NAME (type) = NULL;
1881 /* This used to set the tag to "<opaque>". But I think
1882 setting it to NULL is right, and the printing code can
1883 print it as "struct {...}". */
1884 TYPE_TAG_NAME (type) = NULL;
1885 INIT_CPLUS_SPECIFIC (type);
1886 TYPE_LENGTH (type) = 0;
1887 TYPE_FIELDS (type) = 0;
1888 TYPE_NFIELDS (type) = 0;
1892 type = coff_read_struct_type (cs->c_symnum,
1893 aux->x_sym.x_misc.x_lnsz.x_size,
1894 aux->x_sym.x_fcnary.x_fcn.x_endndx.l,
1900 if (cs->c_naux != 1)
1902 /* Anonymous union type. */
1903 type = coff_alloc_type (cs->c_symnum);
1904 TYPE_NAME (type) = NULL;
1905 /* This used to set the tag to "<opaque>". But I think
1906 setting it to NULL is right, and the printing code can
1907 print it as "union {...}". */
1908 TYPE_TAG_NAME (type) = NULL;
1909 INIT_CPLUS_SPECIFIC (type);
1910 TYPE_LENGTH (type) = 0;
1911 TYPE_FIELDS (type) = 0;
1912 TYPE_NFIELDS (type) = 0;
1916 type = coff_read_struct_type (cs->c_symnum,
1917 aux->x_sym.x_misc.x_lnsz.x_size,
1918 aux->x_sym.x_fcnary.x_fcn.x_endndx.l,
1921 TYPE_CODE (type) = TYPE_CODE_UNION;
1925 if (cs->c_naux != 1)
1927 /* Anonymous enum type. */
1928 type = coff_alloc_type (cs->c_symnum);
1929 TYPE_CODE (type) = TYPE_CODE_ENUM;
1930 TYPE_NAME (type) = NULL;
1931 /* This used to set the tag to "<opaque>". But I think
1932 setting it to NULL is right, and the printing code can
1933 print it as "enum {...}". */
1934 TYPE_TAG_NAME (type) = NULL;
1935 TYPE_LENGTH (type) = 0;
1936 TYPE_FIELDS (type) = 0;
1937 TYPE_NFIELDS (type) = 0;
1941 type = coff_read_enum_type (cs->c_symnum,
1942 aux->x_sym.x_misc.x_lnsz.x_size,
1943 aux->x_sym.x_fcnary.x_fcn.x_endndx.l,
1949 /* Shouldn't show up here. */
1953 return objfile_type (objfile)->builtin_unsigned_char;
1956 return objfile_type (objfile)->builtin_unsigned_short;
1959 return objfile_type (objfile)->builtin_unsigned_int;
1962 if (cs->c_sclass == C_FIELD
1963 && aux->x_sym.x_misc.x_lnsz.x_size
1964 > gdbarch_long_bit (gdbarch))
1965 return objfile_type (objfile)->builtin_unsigned_long_long;
1967 return objfile_type (objfile)->builtin_unsigned_long;
1969 complaint (&symfile_complaints,
1970 _("Unexpected type for symbol %s"), cs->c_name);
1971 return objfile_type (objfile)->builtin_void;
1974 /* This page contains subroutines of read_type. */
1976 /* Read the description of a structure (or union type) and return an
1977 object describing the type. */
1979 static struct type *
1980 coff_read_struct_type (int index, int length, int lastsym,
1981 struct objfile *objfile)
1985 struct nextfield *next;
1990 struct nextfield *list = 0;
1991 struct nextfield *new;
1995 struct coff_symbol member_sym;
1996 struct coff_symbol *ms = &member_sym;
1997 struct internal_syment sub_sym;
1998 union internal_auxent sub_aux;
2001 type = coff_alloc_type (index);
2002 TYPE_CODE (type) = TYPE_CODE_STRUCT;
2003 INIT_CPLUS_SPECIFIC (type);
2004 TYPE_LENGTH (type) = length;
2006 while (!done && symnum < lastsym && symnum < nlist_nsyms_global)
2008 read_one_sym (ms, &sub_sym, &sub_aux);
2010 name = EXTERNAL_NAME (name, objfile->obfd);
2012 switch (ms->c_sclass)
2017 /* Get space to record the next field's data. */
2018 new = (struct nextfield *) alloca (sizeof (struct nextfield));
2022 /* Save the data. */
2023 list->field.name = obsavestring (name, strlen (name),
2024 &objfile->objfile_obstack);
2025 FIELD_TYPE (list->field) = decode_type (ms, ms->c_type,
2027 SET_FIELD_BITPOS (list->field, 8 * ms->c_value);
2028 FIELD_BITSIZE (list->field) = 0;
2034 /* Get space to record the next field's data. */
2035 new = (struct nextfield *) alloca (sizeof (struct nextfield));
2039 /* Save the data. */
2040 list->field.name = obsavestring (name, strlen (name),
2041 &objfile->objfile_obstack);
2042 FIELD_TYPE (list->field) = decode_type (ms, ms->c_type,
2044 SET_FIELD_BITPOS (list->field, ms->c_value);
2045 FIELD_BITSIZE (list->field) = sub_aux.x_sym.x_misc.x_lnsz.x_size;
2054 /* Now create the vector of fields, and record how big it is. */
2056 TYPE_NFIELDS (type) = nfields;
2057 TYPE_FIELDS (type) = (struct field *)
2058 TYPE_ALLOC (type, sizeof (struct field) * nfields);
2060 /* Copy the saved-up fields into the field vector. */
2062 for (n = nfields; list; list = list->next)
2063 TYPE_FIELD (type, --n) = list->field;
2068 /* Read a definition of an enumeration type,
2069 and create and return a suitable type object.
2070 Also defines the symbols that represent the values of the type. */
2072 static struct type *
2073 coff_read_enum_type (int index, int length, int lastsym,
2074 struct objfile *objfile)
2076 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2081 struct pending **symlist;
2082 struct coff_symbol member_sym;
2083 struct coff_symbol *ms = &member_sym;
2084 struct internal_syment sub_sym;
2085 union internal_auxent sub_aux;
2086 struct pending *osyms, *syms;
2090 int unsigned_enum = 1;
2092 type = coff_alloc_type (index);
2093 if (within_function)
2094 symlist = &local_symbols;
2096 symlist = &file_symbols;
2098 o_nsyms = osyms ? osyms->nsyms : 0;
2100 while (!done && symnum < lastsym && symnum < nlist_nsyms_global)
2102 read_one_sym (ms, &sub_sym, &sub_aux);
2104 name = EXTERNAL_NAME (name, objfile->obfd);
2106 switch (ms->c_sclass)
2109 sym = (struct symbol *) obstack_alloc
2110 (&objfile->objfile_obstack, sizeof (struct symbol));
2111 memset (sym, 0, sizeof (struct symbol));
2113 SYMBOL_SET_LINKAGE_NAME (sym,
2114 obsavestring (name, strlen (name),
2115 &objfile->objfile_obstack));
2116 SYMBOL_CLASS (sym) = LOC_CONST;
2117 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
2118 SYMBOL_VALUE (sym) = ms->c_value;
2119 add_symbol_to_list (sym, symlist);
2124 /* Sometimes the linker (on 386/ix 2.0.2 at least) screws
2125 up the count of how many symbols to read. So stop
2132 /* Now fill in the fields of the type-structure. */
2135 TYPE_LENGTH (type) = length;
2136 else /* Assume ints. */
2137 TYPE_LENGTH (type) = gdbarch_int_bit (gdbarch) / TARGET_CHAR_BIT;
2138 TYPE_CODE (type) = TYPE_CODE_ENUM;
2139 TYPE_NFIELDS (type) = nsyms;
2140 TYPE_FIELDS (type) = (struct field *)
2141 TYPE_ALLOC (type, sizeof (struct field) * nsyms);
2143 /* Find the symbols for the values and put them into the type.
2144 The symbols can be found in the symlist that we put them on
2145 to cause them to be defined. osyms contains the old value
2146 of that symlist; everything up to there was defined by us. */
2147 /* Note that we preserve the order of the enum constants, so
2148 that in something like "enum {FOO, LAST_THING=FOO}" we print
2149 FOO, not LAST_THING. */
2151 for (syms = *symlist, n = 0; syms; syms = syms->next)
2157 for (; j < syms->nsyms; j++, n++)
2159 struct symbol *xsym = syms->symbol[j];
2161 SYMBOL_TYPE (xsym) = type;
2162 TYPE_FIELD_NAME (type, n) = SYMBOL_LINKAGE_NAME (xsym);
2163 SET_FIELD_BITPOS (TYPE_FIELD (type, n), SYMBOL_VALUE (xsym));
2164 if (SYMBOL_VALUE (xsym) < 0)
2166 TYPE_FIELD_BITSIZE (type, n) = 0;
2173 TYPE_UNSIGNED (type) = 1;
2178 /* Register our ability to parse symbols for coff BFD files. */
2180 static const struct sym_fns coff_sym_fns =
2182 bfd_target_coff_flavour,
2183 coff_new_init, /* sym_new_init: init anything gbl to
2185 coff_symfile_init, /* sym_init: read initial info, setup
2187 coff_symfile_read, /* sym_read: read a symbol file into
2189 NULL, /* sym_read_psymbols */
2190 coff_symfile_finish, /* sym_finish: finished with file,
2192 default_symfile_offsets, /* sym_offsets: xlate external to
2194 default_symfile_segments, /* sym_segments: Get segment
2195 information from a file */
2196 NULL, /* sym_read_linetable */
2198 default_symfile_relocate, /* sym_relocate: Relocate a debug
2204 _initialize_coffread (void)
2206 add_symtab_fns (&coff_sym_fns);