1 /* Read coff symbol tables and convert to internal format, for GDB.
2 Copyright (C) 1987-2019 Free Software Foundation, Inc.
3 Contributed by David D. Johnson, Brown University (ddj@cs.brown.edu).
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "breakpoint.h"
27 #include "gdb_obstack.h"
30 #include "coff/internal.h" /* Internal format of COFF symbols in BFD */
31 #include "libcoff.h" /* FIXME secret internal data from BFD */
33 #include "buildsym-legacy.h"
34 #include "gdb-stabs.h"
35 #include "stabsread.h"
36 #include "complaints.h"
39 #include "dictionary.h"
41 #include "coff-pe-read.h"
46 /* Key for COFF-associated data. */
48 static const struct objfile_data *coff_objfile_data_key;
50 /* The objfile we are currently reading. */
52 static struct objfile *coffread_objfile;
54 struct coff_symfile_info
56 file_ptr min_lineno_offset; /* Where in file lowest line#s are. */
57 file_ptr max_lineno_offset; /* 1+last byte of line#s in file. */
59 CORE_ADDR textaddr; /* Addr of .text section. */
60 unsigned int textsize; /* Size of .text section. */
61 std::vector<asection *> *stabsects; /* .stab sections. */
62 asection *stabstrsect; /* Section pointer for .stab section. */
66 /* Translate an external name string into a user-visible name. */
67 #define EXTERNAL_NAME(string, abfd) \
68 (string[0] == bfd_get_symbol_leading_char (abfd) \
69 ? string + 1 : string)
71 /* To be an sdb debug type, type must have at least a basic or primary
72 derived type. Using this rather than checking against T_NULL is
73 said to prevent core dumps if we try to operate on Michael Bloom
76 #define SDB_TYPE(type) (BTYPE(type) | (type & N_TMASK))
78 /* Core address of start and end of text of current source file.
79 This comes from a ".text" symbol where x_nlinno > 0. */
81 static CORE_ADDR current_source_start_addr;
82 static CORE_ADDR current_source_end_addr;
84 /* The addresses of the symbol table stream and number of symbols
85 of the object file we are reading (as copied into core). */
87 static bfd *nlist_bfd_global;
88 static int nlist_nsyms_global;
91 /* Pointers to scratch storage, used for reading raw symbols and
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
114 structures. (We only need to know this to read them from the file
115 -- BFD will then translate the data in them, into `internal_xxx'
116 structs in the right 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 /* Simplified internal version of coff symbol table information. */
136 int c_symnum; /* Symbol number of this entry. */
137 int c_naux; /* 0 if syment only, 1 if syment +
145 /* Vector of types defined so far, indexed by their type numbers. */
147 static struct type **type_vector;
149 /* Number of elements allocated for type_vector currently. */
151 static int type_vector_length;
153 /* Initial size of type vector. Is realloc'd larger if needed, and
154 realloc'd down to the size actually used, when completed. */
156 #define INITIAL_TYPE_VECTOR_LENGTH 160
158 static char *linetab = NULL;
159 static long linetab_offset;
160 static unsigned long linetab_size;
162 static char *stringtab = NULL;
164 extern void stabsread_clear_cache (void);
166 static struct type *coff_read_struct_type (int, int, int,
169 static struct type *decode_base_type (struct coff_symbol *,
171 union internal_auxent *,
174 static struct type *decode_type (struct coff_symbol *, unsigned int,
175 union internal_auxent *,
178 static struct type *decode_function_type (struct coff_symbol *,
180 union internal_auxent *,
183 static struct type *coff_read_enum_type (int, int, int,
186 static struct symbol *process_coff_symbol (struct coff_symbol *,
187 union internal_auxent *,
190 static void patch_opaque_types (struct symtab *);
192 static void enter_linenos (long, int, int, struct objfile *);
194 static int init_lineno (bfd *, long, int, gdb::unique_xmalloc_ptr<char> *);
196 static char *getsymname (struct internal_syment *);
198 static const char *coff_getfilename (union internal_auxent *);
200 static int init_stringtab (bfd *, long, gdb::unique_xmalloc_ptr<char> *);
202 static void read_one_sym (struct coff_symbol *,
203 struct internal_syment *,
204 union internal_auxent *);
206 static void coff_symtab_read (minimal_symbol_reader &,
207 long, unsigned int, struct objfile *);
209 /* We are called once per section from coff_symfile_read. We
210 need to examine each section we are passed, check to see
211 if it is something we are interested in processing, and
212 if so, stash away some access information for the section.
214 FIXME: The section names should not be hardwired strings (what
215 should they be? I don't think most object file formats have enough
216 section flags to specify what kind of debug section it is
220 coff_locate_sections (bfd *abfd, asection *sectp, void *csip)
222 struct coff_symfile_info *csi;
225 csi = (struct coff_symfile_info *) csip;
226 name = bfd_get_section_name (abfd, sectp);
227 if (strcmp (name, ".text") == 0)
229 csi->textaddr = bfd_section_vma (abfd, sectp);
230 csi->textsize += bfd_section_size (abfd, sectp);
232 else if (startswith (name, ".text"))
234 csi->textsize += bfd_section_size (abfd, sectp);
236 else if (strcmp (name, ".stabstr") == 0)
238 csi->stabstrsect = sectp;
240 else if (startswith (name, ".stab"))
244 /* We can have multiple .stab sections if linked with
246 for (s = name + sizeof ".stab" - 1; *s != '\0'; s++)
250 csi->stabsects->push_back (sectp);
254 /* Return the section_offsets* that CS points to. */
255 static int cs_to_section (struct coff_symbol *, struct objfile *);
257 struct find_targ_sec_arg
264 find_targ_sec (bfd *abfd, asection *sect, void *obj)
266 struct find_targ_sec_arg *args = (struct find_targ_sec_arg *) obj;
268 if (sect->target_index == args->targ_index)
269 *args->resultp = sect;
272 /* Return the bfd_section that CS points to. */
273 static struct bfd_section*
274 cs_to_bfd_section (struct coff_symbol *cs, struct objfile *objfile)
276 asection *sect = NULL;
277 struct find_targ_sec_arg args;
279 args.targ_index = cs->c_secnum;
280 args.resultp = §
281 bfd_map_over_sections (objfile->obfd, find_targ_sec, &args);
285 /* Return the section number (SECT_OFF_*) that CS points to. */
287 cs_to_section (struct coff_symbol *cs, struct objfile *objfile)
289 asection *sect = cs_to_bfd_section (cs, objfile);
292 return SECT_OFF_TEXT (objfile);
293 return gdb_bfd_section_index (objfile->obfd, sect);
296 /* Return the address of the section of a COFF symbol. */
298 static CORE_ADDR cs_section_address (struct coff_symbol *, bfd *);
301 cs_section_address (struct coff_symbol *cs, bfd *abfd)
303 asection *sect = NULL;
304 struct find_targ_sec_arg args;
307 args.targ_index = cs->c_secnum;
308 args.resultp = §
309 bfd_map_over_sections (abfd, find_targ_sec, &args);
311 addr = bfd_get_section_vma (abfd, sect);
315 /* Look up a coff type-number index. Return the address of the slot
316 where the type for that index is stored.
317 The type-number is in INDEX.
319 This can be used for finding the type associated with that index
320 or for associating a new type with the index. */
322 static struct type **
323 coff_lookup_type (int index)
325 if (index >= type_vector_length)
327 int old_vector_length = type_vector_length;
329 type_vector_length *= 2;
330 if (index /* is still */ >= type_vector_length)
331 type_vector_length = index * 2;
333 type_vector = (struct type **)
334 xrealloc ((char *) type_vector,
335 type_vector_length * sizeof (struct type *));
336 memset (&type_vector[old_vector_length], 0,
337 (type_vector_length - old_vector_length) * sizeof (struct type *));
339 return &type_vector[index];
342 /* Make sure there is a type allocated for type number index
343 and return the type object.
344 This can create an empty (zeroed) type object. */
347 coff_alloc_type (int index)
349 struct type **type_addr = coff_lookup_type (index);
350 struct type *type = *type_addr;
352 /* If we are referring to a type not known at all yet,
353 allocate an empty type for it.
354 We will fill it in later if we find out how. */
357 type = alloc_type (coffread_objfile);
363 /* Start a new symtab for a new source file.
364 This is called when a COFF ".file" symbol is seen;
365 it indicates the start of data for one original source file. */
368 coff_start_symtab (struct objfile *objfile, const char *name)
371 start_symtab (objfile,
373 /* We never know the directory name for COFF. */
375 /* The start address is irrelevant, since we call
376 set_last_source_start_addr in coff_end_symtab. */
378 /* Let buildsym.c deduce the language for this symtab. */
380 record_debugformat ("COFF");
383 /* Save the vital information from when starting to read a file,
384 for use when closing off the current file.
385 NAME is the file name the symbols came from, START_ADDR is the
386 first text address for the file, and SIZE is the number of bytes of
390 complete_symtab (const char *name, CORE_ADDR start_addr, unsigned int size)
392 set_last_source_file (name);
393 current_source_start_addr = start_addr;
394 current_source_end_addr = start_addr + size;
397 /* Finish the symbol definitions for one main source file, close off
398 all the lexical contexts for that file (creating struct block's for
399 them), then make the struct symtab for that file and put it in the
403 coff_end_symtab (struct objfile *objfile)
405 set_last_source_start_addr (current_source_start_addr);
407 end_symtab (current_source_end_addr, SECT_OFF_TEXT (objfile));
409 /* Reinitialize for beginning of new file. */
410 set_last_source_file (NULL);
413 /* The linker sometimes generates some non-function symbols inside
414 functions referencing variables imported from another DLL.
415 Return nonzero if the given symbol corresponds to one of them. */
418 is_import_fixup_symbol (struct coff_symbol *cs,
419 enum minimal_symbol_type type)
421 /* The following is a bit of a heuristic using the characterictics
422 of these fixup symbols, but should work well in practice... */
425 /* Must be a non-static text symbol. */
426 if (type != mst_text)
429 /* Must be a non-function symbol. */
430 if (ISFCN (cs->c_type))
433 /* The name must start with "__fu<digits>__". */
434 if (!startswith (cs->c_name, "__fu"))
436 if (! isdigit (cs->c_name[4]))
438 for (i = 5; cs->c_name[i] != '\0' && isdigit (cs->c_name[i]); i++)
439 /* Nothing, just incrementing index past all digits. */;
440 if (cs->c_name[i] != '_' || cs->c_name[i + 1] != '_')
446 static struct minimal_symbol *
447 record_minimal_symbol (minimal_symbol_reader &reader,
448 struct coff_symbol *cs, CORE_ADDR address,
449 enum minimal_symbol_type type, int section,
450 struct objfile *objfile)
452 /* We don't want TDESC entry points in the minimal symbol table. */
453 if (cs->c_name[0] == '@')
456 if (is_import_fixup_symbol (cs, type))
458 /* Because the value of these symbols is within a function code
459 range, these symbols interfere with the symbol-from-address
460 reverse lookup; this manifests itselfs in backtraces, or any
461 other commands that prints symbolic addresses. Just pretend
462 these symbols do not exist. */
466 return reader.record_full (cs->c_name, strlen (cs->c_name), true, address,
470 /* coff_symfile_init ()
471 is the coff-specific initialization routine for reading symbols.
472 It is passed a struct objfile which contains, among other things,
473 the BFD for the file whose symbols are being read, and a slot for
474 a pointer to "private data" which we fill with cookies and other
475 treats for coff_symfile_read ().
477 We will only be called if this is a COFF or COFF-like file. BFD
478 handles figuring out the format of the file, and code in symtab.c
479 uses BFD's determination to vector to us.
481 The ultimate result is a new symtab (or, FIXME, eventually a
485 coff_symfile_init (struct objfile *objfile)
487 struct dbx_symfile_info *dbx;
488 struct coff_symfile_info *coff;
490 /* Allocate struct to keep track of stab reading. */
491 dbx = XCNEW (struct dbx_symfile_info);
492 set_objfile_data (objfile, dbx_objfile_data_key, dbx);
494 /* Allocate struct to keep track of the symfile. */
495 coff = XCNEW (struct coff_symfile_info);
496 set_objfile_data (objfile, coff_objfile_data_key, coff);
498 /* COFF objects may be reordered, so set OBJF_REORDERED. If we
499 find this causes a significant slowdown in gdb then we could
500 set it in the debug symbol readers only when necessary. */
501 objfile->flags |= OBJF_REORDERED;
504 /* This function is called for every section; it finds the outer
505 limits of the line table (minimum and maximum file offset) so that
506 the mainline code can read the whole thing for efficiency. */
509 find_linenos (bfd *abfd, struct bfd_section *asect, void *vpinfo)
511 struct coff_symfile_info *info;
513 file_ptr offset, maxoff;
515 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
516 count = asect->lineno_count;
517 /* End of warning. */
521 size = count * local_linesz;
523 info = (struct coff_symfile_info *) vpinfo;
524 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
525 offset = asect->line_filepos;
526 /* End of warning. */
528 if (offset < info->min_lineno_offset || info->min_lineno_offset == 0)
529 info->min_lineno_offset = offset;
531 maxoff = offset + size;
532 if (maxoff > info->max_lineno_offset)
533 info->max_lineno_offset = maxoff;
537 /* The BFD for this file -- only good while we're actively reading
538 symbols into a psymtab or a symtab. */
540 static bfd *symfile_bfd;
542 /* Read a symbol file, after initialization by coff_symfile_init. */
545 coff_symfile_read (struct objfile *objfile, symfile_add_flags symfile_flags)
547 struct coff_symfile_info *info;
548 bfd *abfd = objfile->obfd;
549 coff_data_type *cdata = coff_data (abfd);
550 char *filename = bfd_get_filename (abfd);
552 unsigned int num_symbols;
554 int stringtab_offset;
557 info = (struct coff_symfile_info *) objfile_data (objfile,
558 coff_objfile_data_key);
559 symfile_bfd = abfd; /* Kludge for swap routines. */
561 std::vector<asection *> stabsects;
562 scoped_restore restore_stabsects
563 = make_scoped_restore (&info->stabsects, &stabsects);
565 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
566 num_symbols = bfd_get_symcount (abfd); /* How many syms */
567 symtab_offset = cdata->sym_filepos; /* Symbol table file offset */
568 stringtab_offset = symtab_offset + /* String table file offset */
569 num_symbols * cdata->local_symesz;
571 /* Set a few file-statics that give us specific information about
572 the particular COFF file format we're reading. */
573 local_n_btmask = cdata->local_n_btmask;
574 local_n_btshft = cdata->local_n_btshft;
575 local_n_tmask = cdata->local_n_tmask;
576 local_n_tshift = cdata->local_n_tshift;
577 local_linesz = cdata->local_linesz;
578 local_symesz = cdata->local_symesz;
579 local_auxesz = cdata->local_auxesz;
581 /* Allocate space for raw symbol and aux entries, based on their
582 space requirements as reported by BFD. */
583 gdb::def_vector<char> temp_storage (cdata->local_symesz
584 + cdata->local_auxesz);
585 temp_sym = temp_storage.data ();
586 temp_aux = temp_sym + cdata->local_symesz;
588 /* We need to know whether this is a PE file, because in PE files,
589 unlike standard COFF files, symbol values are stored as offsets
590 from the section address, rather than as absolute addresses.
591 FIXME: We should use BFD to read the symbol table, and thus avoid
594 startswith (bfd_get_target (objfile->obfd), "pe")
595 || startswith (bfd_get_target (objfile->obfd), "epoc-pe");
597 /* End of warning. */
599 info->min_lineno_offset = 0;
600 info->max_lineno_offset = 0;
602 /* Only read line number information if we have symbols.
604 On Windows NT, some of the system's DLL's have sections with
605 PointerToLinenumbers fields that are non-zero, but point at
606 random places within the image file. (In the case I found,
607 KERNEL32.DLL's .text section has a line number info pointer that
608 points into the middle of the string `lib\\i386\kernel32.dll'.)
610 However, these DLL's also have no symbols. The line number
611 tables are meaningless without symbols. And in fact, GDB never
612 uses the line number information unless there are symbols. So we
613 can avoid spurious error messages (and maybe run a little
614 faster!) by not even reading the line number table unless we have
616 scoped_restore restore_linetab = make_scoped_restore (&linetab);
617 gdb::unique_xmalloc_ptr<char> linetab_storage;
620 /* Read the line number table, all at once. */
621 bfd_map_over_sections (abfd, find_linenos, (void *) info);
623 val = init_lineno (abfd, info->min_lineno_offset,
624 info->max_lineno_offset - info->min_lineno_offset,
627 error (_("\"%s\": error reading line numbers."), filename);
630 /* Now read the string table, all at once. */
632 scoped_restore restore_stringtab = make_scoped_restore (&stringtab);
633 gdb::unique_xmalloc_ptr<char> stringtab_storage;
634 val = init_stringtab (abfd, stringtab_offset, &stringtab_storage);
636 error (_("\"%s\": can't get string table"), filename);
638 minimal_symbol_reader reader (objfile);
640 /* Now that the executable file is positioned at symbol table,
641 process it and define symbols accordingly. */
643 coff_symtab_read (reader, (long) symtab_offset, num_symbols, objfile);
645 /* Install any minimal symbols that have been collected as the
646 current minimal symbols for this objfile. */
652 for (minimal_symbol *msym : objfile->msymbols ())
654 const char *name = MSYMBOL_LINKAGE_NAME (msym);
656 /* If the minimal symbols whose name are prefixed by "__imp_"
657 or "_imp_", get rid of the prefix, and search the minimal
658 symbol in OBJFILE. Note that 'maintenance print msymbols'
659 shows that type of these "_imp_XXXX" symbols is mst_data. */
660 if (MSYMBOL_TYPE (msym) == mst_data)
662 const char *name1 = NULL;
664 if (startswith (name, "_imp_"))
666 else if (startswith (name, "__imp_"))
670 int lead = bfd_get_symbol_leading_char (objfile->obfd);
671 struct bound_minimal_symbol found;
673 if (lead != '\0' && *name1 == lead)
676 found = lookup_minimal_symbol (name1, NULL, objfile);
678 /* If found, there are symbols named "_imp_foo" and "foo"
679 respectively in OBJFILE. Set the type of symbol "foo"
680 as 'mst_solib_trampoline'. */
681 if (found.minsym != NULL
682 && MSYMBOL_TYPE (found.minsym) == mst_text)
683 MSYMBOL_TYPE (found.minsym) = mst_solib_trampoline;
689 if (!(objfile->flags & OBJF_READNEVER))
690 bfd_map_over_sections (abfd, coff_locate_sections, (void *) info);
692 if (!info->stabsects->empty())
694 if (!info->stabstrsect)
696 error (_("The debugging information in `%s' is corrupted.\nThe "
697 "file has a `.stabs' section, but no `.stabstr' section."),
701 /* FIXME: dubious. Why can't we use something normal like
702 bfd_get_section_contents? */
703 bfd_seek (abfd, abfd->where, 0);
705 stabstrsize = bfd_section_size (abfd, info->stabstrsect);
707 coffstab_build_psymtabs (objfile,
708 info->textaddr, info->textsize,
710 info->stabstrsect->filepos, stabstrsize);
712 if (dwarf2_has_info (objfile, NULL))
714 /* DWARF2 sections. */
715 dwarf2_build_psymtabs (objfile);
718 dwarf2_build_frame_info (objfile);
720 /* Try to add separate debug file if no symbols table found. */
721 if (!objfile_has_partial_symbols (objfile))
723 std::string debugfile = find_separate_debug_file_by_buildid (objfile);
725 if (debugfile.empty ())
726 debugfile = find_separate_debug_file_by_debuglink (objfile);
728 if (!debugfile.empty ())
730 gdb_bfd_ref_ptr debug_bfd (symfile_bfd_open (debugfile.c_str ()));
732 symbol_file_add_separate (debug_bfd.get (), debugfile.c_str (),
733 symfile_flags, objfile);
739 coff_new_init (struct objfile *ignore)
743 /* Perform any local cleanups required when we are done with a
744 particular objfile. I.E, we are in the process of discarding all
745 symbol information for an objfile, freeing up all memory held for
746 it, and unlinking the objfile struct from the global list of known
750 coff_symfile_finish (struct objfile *objfile)
752 /* Let stabs reader clean up. */
753 stabsread_clear_cache ();
757 /* Given pointers to a symbol table in coff style exec file,
758 analyze them and create struct symtab's describing the symbols.
759 NSYMS is the number of symbols in the symbol table.
760 We read them one at a time using read_one_sym (). */
763 coff_symtab_read (minimal_symbol_reader &reader,
764 long symtab_offset, unsigned int nsyms,
765 struct objfile *objfile)
767 struct gdbarch *gdbarch = get_objfile_arch (objfile);
768 struct context_stack *newobj = nullptr;
769 struct coff_symbol coff_symbol;
770 struct coff_symbol *cs = &coff_symbol;
771 static struct internal_syment main_sym;
772 static union internal_auxent main_aux;
773 struct coff_symbol fcn_cs_saved;
774 static struct internal_syment fcn_sym_saved;
775 static union internal_auxent fcn_aux_saved;
776 /* A .file is open. */
777 int in_source_file = 0;
778 int next_file_symnum = -1;
779 /* Name of the current file. */
780 const char *filestring = "";
782 int fcn_first_line = 0;
783 CORE_ADDR fcn_first_line_addr = 0;
784 int fcn_last_line = 0;
785 int fcn_start_addr = 0;
786 long fcn_line_ptr = 0;
789 struct minimal_symbol *msym;
791 scoped_free_pendings free_pending;
793 /* Work around a stdio bug in SunOS4.1.1 (this makes me nervous....
794 it's hard to know I've really worked around it. The fix should
795 be harmless, anyway). The symptom of the bug is that the first
796 fread (in read_one_sym), will (in my example) actually get data
797 from file offset 268, when the fseek was to 264 (and ftell shows
798 264). This causes all hell to break loose. I was unable to
799 reproduce this on a short test program which operated on the same
800 file, performing (I think) the same sequence of operations.
802 It stopped happening when I put in this (former) rewind().
804 FIXME: Find out if this has been reported to Sun, whether it has
805 been fixed in a later release, etc. */
807 bfd_seek (objfile->obfd, 0, 0);
809 /* Position to read the symbol table. */
810 val = bfd_seek (objfile->obfd, (long) symtab_offset, 0);
812 perror_with_name (objfile_name (objfile));
814 coffread_objfile = objfile;
815 nlist_bfd_global = objfile->obfd;
816 nlist_nsyms_global = nsyms;
817 set_last_source_file (NULL);
818 memset (opaque_type_chain, 0, sizeof opaque_type_chain);
820 if (type_vector) /* Get rid of previous one. */
822 type_vector_length = INITIAL_TYPE_VECTOR_LENGTH;
823 type_vector = XCNEWVEC (struct type *, type_vector_length);
825 coff_start_symtab (objfile, "");
828 while (symnum < nsyms)
830 QUIT; /* Make this command interruptable. */
832 read_one_sym (cs, &main_sym, &main_aux);
834 if (cs->c_symnum == next_file_symnum && cs->c_sclass != C_FILE)
836 if (get_last_source_file ())
837 coff_end_symtab (objfile);
839 coff_start_symtab (objfile, "_globals_");
840 /* coff_start_symtab will set the language of this symtab to
841 language_unknown, since such a ``file name'' is not
842 recognized. Override that with the minimal language to
843 allow printing values in this symtab. */
844 get_current_subfile ()->language = language_minimal;
845 complete_symtab ("_globals_", 0, 0);
846 /* Done with all files, everything from here on out is
850 /* Special case for file with type declarations only, no
852 if (!get_last_source_file () && SDB_TYPE (cs->c_type)
853 && cs->c_secnum == N_DEBUG)
854 complete_symtab (filestring, 0, 0);
856 /* Typedefs should not be treated as symbol definitions. */
857 if (ISFCN (cs->c_type) && cs->c_sclass != C_TPDEF)
859 /* Record all functions -- external and static -- in
861 int section = cs_to_section (cs, objfile);
863 tmpaddr = cs->c_value;
864 /* Don't record unresolved symbols. */
865 if (!(cs->c_secnum <= 0 && cs->c_value == 0))
866 record_minimal_symbol (reader, cs, tmpaddr, mst_text,
869 fcn_line_ptr = main_aux.x_sym.x_fcnary.x_fcn.x_lnnoptr;
870 fcn_start_addr = tmpaddr;
872 fcn_sym_saved = main_sym;
873 fcn_aux_saved = main_aux;
877 switch (cs->c_sclass)
886 complaint (_("Bad n_sclass for symbol %s"),
891 /* c_value field contains symnum of next .file entry in
892 table or symnum of first global after last .file. */
893 next_file_symnum = cs->c_value;
895 filestring = coff_getfilename (&main_aux);
899 /* Complete symbol table for last object file
900 containing debugging information. */
901 if (get_last_source_file ())
903 coff_end_symtab (objfile);
904 coff_start_symtab (objfile, filestring);
909 /* C_LABEL is used for labels and static functions.
910 Including it here allows gdb to see static functions when
911 no debug info is available. */
913 /* However, labels within a function can make weird
914 backtraces, so filter them out (from phdm@macqel.be). */
921 case C_THUMBSTATFUNC:
922 if (cs->c_name[0] == '.')
924 if (strcmp (cs->c_name, ".text") == 0)
926 /* FIXME: don't wire in ".text" as section name or
928 /* Check for in_source_file deals with case of a
929 file with debugging symbols followed by a later
930 file with no symbols. */
932 complete_symtab (filestring,
933 cs->c_value + ANOFFSET (objfile->section_offsets,
934 SECT_OFF_TEXT (objfile)),
935 main_aux.x_scn.x_scnlen);
938 /* Flush rest of '.' symbols. */
941 else if (!SDB_TYPE (cs->c_type)
942 && cs->c_name[0] == 'L'
943 && (startswith (cs->c_name, "LI%")
944 || startswith (cs->c_name, "LF%")
945 || startswith (cs->c_name, "LC%")
946 || startswith (cs->c_name, "LP%")
947 || startswith (cs->c_name, "LPB%")
948 || startswith (cs->c_name, "LBB%")
949 || startswith (cs->c_name, "LBE%")
950 || startswith (cs->c_name, "LPBX%")))
951 /* At least on a 3b1, gcc generates swbeg and string labels
952 that look like this. Ignore them. */
954 /* For static symbols that don't start with '.'... */
960 /* Record it in the minimal symbols regardless of
961 SDB_TYPE. This parallels what we do for other debug
962 formats, and probably is needed to make
963 print_address_symbolic work right without the (now
964 gone) "set fast-symbolic-addr off" kludge. */
966 enum minimal_symbol_type ms_type;
968 CORE_ADDR offset = 0;
970 if (cs->c_secnum == N_UNDEF)
972 /* This is a common symbol. We used to rely on
973 the target to tell us whether it knows where
974 the symbol has been relocated to, but none of
975 the target implementations actually provided
976 that operation. So we just ignore the symbol,
977 the same way we would do if we had a target-side
978 symbol lookup which returned no match. */
981 else if (cs->c_secnum == N_ABS)
983 /* Use the correct minimal symbol type (and don't
984 relocate) for absolute values. */
986 sec = cs_to_section (cs, objfile);
987 tmpaddr = cs->c_value;
991 asection *bfd_section = cs_to_bfd_section (cs, objfile);
993 sec = cs_to_section (cs, objfile);
994 tmpaddr = cs->c_value;
995 /* Statics in a PE file also get relocated. */
996 if (cs->c_sclass == C_EXT
997 || cs->c_sclass == C_THUMBEXTFUNC
998 || cs->c_sclass == C_THUMBEXT
999 || (pe_file && (cs->c_sclass == C_STAT)))
1000 offset = ANOFFSET (objfile->section_offsets, sec);
1002 if (bfd_section->flags & SEC_CODE)
1005 cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXTFUNC
1006 || cs->c_sclass == C_THUMBEXT ?
1007 mst_text : mst_file_text;
1008 tmpaddr = gdbarch_addr_bits_remove (gdbarch, tmpaddr);
1010 else if (bfd_section->flags & SEC_ALLOC
1011 && bfd_section->flags & SEC_LOAD)
1014 cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXT
1015 ? mst_data : mst_file_data;
1017 else if (bfd_section->flags & SEC_ALLOC)
1020 cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXT
1021 ? mst_bss : mst_file_bss;
1024 ms_type = mst_unknown;
1027 msym = record_minimal_symbol (reader, cs, tmpaddr, ms_type,
1030 gdbarch_coff_make_msymbol_special (gdbarch,
1031 cs->c_sclass, msym);
1033 if (SDB_TYPE (cs->c_type))
1037 sym = process_coff_symbol
1038 (cs, &main_aux, objfile);
1039 SYMBOL_VALUE (sym) = tmpaddr + offset;
1040 SYMBOL_SECTION (sym) = sec;
1046 if (strcmp (cs->c_name, ".bf") == 0)
1048 within_function = 1;
1050 /* Value contains address of first non-init type
1052 /* main_aux.x_sym.x_misc.x_lnsz.x_lnno
1053 contains line number of '{' }. */
1054 if (cs->c_naux != 1)
1055 complaint (_("`.bf' symbol %d has no aux entry"),
1057 fcn_first_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
1058 fcn_first_line_addr = cs->c_value;
1060 /* Might want to check that locals are 0 and
1061 context_stack_depth is zero, and complain if not. */
1064 newobj = push_context (depth, fcn_start_addr);
1065 fcn_cs_saved.c_name = getsymname (&fcn_sym_saved);
1067 process_coff_symbol (&fcn_cs_saved,
1068 &fcn_aux_saved, objfile);
1070 else if (strcmp (cs->c_name, ".ef") == 0)
1072 if (!within_function)
1073 error (_("Bad coff function information."));
1074 /* The value of .ef is the address of epilogue code;
1075 not useful for gdb. */
1076 /* { main_aux.x_sym.x_misc.x_lnsz.x_lnno
1077 contains number of lines to '}' */
1079 if (outermost_context_p ())
1080 { /* We attempted to pop an empty context stack. */
1081 complaint (_("`.ef' symbol without matching `.bf' "
1082 "symbol ignored starting at symnum %d"),
1084 within_function = 0;
1088 struct context_stack cstk = pop_context ();
1089 /* Stack must be empty now. */
1090 if (!outermost_context_p () || newobj == NULL)
1092 complaint (_("Unmatched .ef symbol(s) ignored "
1093 "starting at symnum %d"),
1095 within_function = 0;
1098 if (cs->c_naux != 1)
1100 complaint (_("`.ef' symbol %d has no aux entry"),
1102 fcn_last_line = 0x7FFFFFFF;
1106 fcn_last_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
1108 /* fcn_first_line is the line number of the opening '{'.
1109 Do not record it - because it would affect gdb's idea
1110 of the line number of the first statement of the
1111 function - except for one-line functions, for which
1112 it is also the line number of all the statements and
1113 of the closing '}', and for which we do not have any
1114 other statement-line-number. */
1115 if (fcn_last_line == 1)
1116 record_line (get_current_subfile (), fcn_first_line,
1117 gdbarch_addr_bits_remove (gdbarch,
1118 fcn_first_line_addr));
1120 enter_linenos (fcn_line_ptr, fcn_first_line,
1121 fcn_last_line, objfile);
1123 finish_block (cstk.name, cstk.old_blocks,
1124 NULL, cstk.start_addr,
1125 fcn_cs_saved.c_value
1126 + fcn_aux_saved.x_sym.x_misc.x_fsize
1127 + ANOFFSET (objfile->section_offsets,
1128 SECT_OFF_TEXT (objfile)));
1129 within_function = 0;
1134 if (strcmp (cs->c_name, ".bb") == 0)
1136 tmpaddr = cs->c_value;
1137 tmpaddr += ANOFFSET (objfile->section_offsets,
1138 SECT_OFF_TEXT (objfile));
1139 push_context (++depth, tmpaddr);
1141 else if (strcmp (cs->c_name, ".eb") == 0)
1143 if (outermost_context_p ())
1144 { /* We attempted to pop an empty context stack. */
1145 complaint (_("`.eb' symbol without matching `.bb' "
1146 "symbol ignored starting at symnum %d"),
1151 struct context_stack cstk = pop_context ();
1152 if (depth-- != cstk.depth)
1154 complaint (_("Mismatched .eb symbol ignored "
1155 "starting at symnum %d"),
1159 if (*get_local_symbols () && !outermost_context_p ())
1162 cs->c_value + ANOFFSET (objfile->section_offsets,
1163 SECT_OFF_TEXT (objfile));
1164 /* Make a block for the local symbols within. */
1165 finish_block (0, cstk.old_blocks, NULL,
1166 cstk.start_addr, tmpaddr);
1168 /* Now pop locals of block just finished. */
1169 *get_local_symbols () = cstk.locals;
1174 process_coff_symbol (cs, &main_aux, objfile);
1179 if ((nsyms == 0) && (pe_file))
1181 /* We've got no debugging symbols, but it's a portable
1182 executable, so try to read the export table. */
1183 read_pe_exported_syms (reader, objfile);
1186 if (get_last_source_file ())
1187 coff_end_symtab (objfile);
1189 /* Patch up any opaque types (references to types that are not defined
1190 in the file where they are referenced, e.g. "struct foo *bar"). */
1192 for (compunit_symtab *cu : objfile->compunits ())
1194 for (symtab *s : compunit_filetabs (cu))
1195 patch_opaque_types (s);
1199 coffread_objfile = NULL;
1202 /* Routines for reading headers and symbols from executable. */
1204 /* Read the next symbol, swap it, and return it in both
1205 internal_syment form, and coff_symbol form. Also return its first
1206 auxent, if any, in internal_auxent form, and skip any other
1210 read_one_sym (struct coff_symbol *cs,
1211 struct internal_syment *sym,
1212 union internal_auxent *aux)
1215 bfd_size_type bytes;
1217 cs->c_symnum = symnum;
1218 bytes = bfd_bread (temp_sym, local_symesz, nlist_bfd_global);
1219 if (bytes != local_symesz)
1220 error (_("%s: error reading symbols"), objfile_name (coffread_objfile));
1221 bfd_coff_swap_sym_in (symfile_bfd, temp_sym, (char *) sym);
1222 cs->c_naux = sym->n_numaux & 0xff;
1223 if (cs->c_naux >= 1)
1225 bytes = bfd_bread (temp_aux, local_auxesz, nlist_bfd_global);
1226 if (bytes != local_auxesz)
1227 error (_("%s: error reading symbols"), objfile_name (coffread_objfile));
1228 bfd_coff_swap_aux_in (symfile_bfd, temp_aux,
1229 sym->n_type, sym->n_sclass,
1230 0, cs->c_naux, (char *) aux);
1231 /* If more than one aux entry, read past it (only the first aux
1233 for (i = 1; i < cs->c_naux; i++)
1235 bytes = bfd_bread (temp_aux, local_auxesz, nlist_bfd_global);
1236 if (bytes != local_auxesz)
1237 error (_("%s: error reading symbols"),
1238 objfile_name (coffread_objfile));
1241 cs->c_name = getsymname (sym);
1242 cs->c_value = sym->n_value;
1243 cs->c_sclass = (sym->n_sclass & 0xff);
1244 cs->c_secnum = sym->n_scnum;
1245 cs->c_type = (unsigned) sym->n_type;
1246 if (!SDB_TYPE (cs->c_type))
1250 if (cs->c_sclass & 128)
1251 printf (_("thumb symbol %s, class 0x%x\n"), cs->c_name, cs->c_sclass);
1254 symnum += 1 + cs->c_naux;
1256 /* The PE file format stores symbol values as offsets within the
1257 section, rather than as absolute addresses. We correct that
1258 here, if the symbol has an appropriate storage class. FIXME: We
1259 should use BFD to read the symbols, rather than duplicating the
1263 switch (cs->c_sclass)
1267 case C_THUMBEXTFUNC:
1272 case C_THUMBSTATFUNC:
1278 if (cs->c_secnum != 0)
1279 cs->c_value += cs_section_address (cs, symfile_bfd);
1285 /* Support for string table handling. */
1288 init_stringtab (bfd *abfd, long offset, gdb::unique_xmalloc_ptr<char> *storage)
1292 unsigned char lengthbuf[4];
1294 /* If the file is stripped, the offset might be zero, indicating no
1295 string table. Just return with `stringtab' set to null. */
1299 if (bfd_seek (abfd, offset, 0) < 0)
1302 val = bfd_bread ((char *) lengthbuf, sizeof lengthbuf, abfd);
1303 length = bfd_h_get_32 (symfile_bfd, lengthbuf);
1305 /* If no string table is needed, then the file may end immediately
1306 after the symbols. Just return with `stringtab' set to null. */
1307 if (val != sizeof lengthbuf || length < sizeof lengthbuf)
1310 storage->reset ((char *) xmalloc (length));
1311 stringtab = storage->get ();
1312 /* This is in target format (probably not very useful, and not
1313 currently used), not host format. */
1314 memcpy (stringtab, lengthbuf, sizeof lengthbuf);
1315 if (length == sizeof length) /* Empty table -- just the count. */
1318 val = bfd_bread (stringtab + sizeof lengthbuf,
1319 length - sizeof lengthbuf, abfd);
1320 if (val != length - sizeof lengthbuf || stringtab[length - 1] != '\0')
1327 getsymname (struct internal_syment *symbol_entry)
1329 static char buffer[SYMNMLEN + 1];
1332 if (symbol_entry->_n._n_n._n_zeroes == 0)
1334 /* FIXME: Probably should be detecting corrupt symbol files by
1335 seeing whether offset points to within the stringtab. */
1336 result = stringtab + symbol_entry->_n._n_n._n_offset;
1340 strncpy (buffer, symbol_entry->_n._n_name, SYMNMLEN);
1341 buffer[SYMNMLEN] = '\0';
1347 /* Extract the file name from the aux entry of a C_FILE symbol.
1348 Return only the last component of the name. Result is in static
1349 storage and is only good for temporary use. */
1352 coff_getfilename (union internal_auxent *aux_entry)
1354 static char buffer[BUFSIZ];
1357 if (aux_entry->x_file.x_n.x_zeroes == 0)
1359 if (strlen (stringtab + aux_entry->x_file.x_n.x_offset) >= BUFSIZ)
1360 internal_error (__FILE__, __LINE__, _("coff file name too long"));
1361 strcpy (buffer, stringtab + aux_entry->x_file.x_n.x_offset);
1365 strncpy (buffer, aux_entry->x_file.x_fname, FILNMLEN);
1366 buffer[FILNMLEN] = '\0';
1370 /* FIXME: We should not be throwing away the information about what
1371 directory. It should go into dirname of the symtab, or some such
1373 result = lbasename (result);
1377 /* Support for line number handling. */
1379 /* Read in all the line numbers for fast lookups later. Leave them in
1380 external (unswapped) format in memory; we'll swap them as we enter
1381 them into GDB's data structures. */
1384 init_lineno (bfd *abfd, long offset, int size,
1385 gdb::unique_xmalloc_ptr<char> *storage)
1389 linetab_offset = offset;
1390 linetab_size = size;
1395 if (bfd_seek (abfd, offset, 0) < 0)
1398 /* Allocate the desired table, plus a sentinel. */
1399 storage->reset ((char *) xmalloc (size + local_linesz));
1400 linetab = storage->get ();
1402 val = bfd_bread (storage->get (), size, abfd);
1406 /* Terminate it with an all-zero sentinel record. */
1407 memset (linetab + size, 0, local_linesz);
1412 #if !defined (L_LNNO32)
1413 #define L_LNNO32(lp) ((lp)->l_lnno)
1417 enter_linenos (long file_offset, int first_line,
1418 int last_line, struct objfile *objfile)
1420 struct gdbarch *gdbarch = get_objfile_arch (objfile);
1422 struct internal_lineno lptr;
1426 if (file_offset < linetab_offset)
1428 complaint (_("Line number pointer %ld lower than start of line numbers"),
1430 if (file_offset > linetab_size) /* Too big to be an offset? */
1432 file_offset += linetab_offset; /* Try reading at that linetab
1436 rawptr = &linetab[file_offset - linetab_offset];
1438 /* Skip first line entry for each function. */
1439 rawptr += local_linesz;
1440 /* Line numbers start at one for the first line of the function. */
1443 /* If the line number table is full (e.g. 64K lines in COFF debug
1444 info), the next function's L_LNNO32 might not be zero, so don't
1445 overstep the table's end in any case. */
1446 while (rawptr <= &linetab[0] + linetab_size)
1448 bfd_coff_swap_lineno_in (symfile_bfd, rawptr, &lptr);
1449 rawptr += local_linesz;
1450 /* The next function, or the sentinel, will have L_LNNO32 zero;
1452 if (L_LNNO32 (&lptr) && L_LNNO32 (&lptr) <= last_line)
1454 CORE_ADDR addr = lptr.l_addr.l_paddr;
1455 addr += ANOFFSET (objfile->section_offsets,
1456 SECT_OFF_TEXT (objfile));
1457 record_line (get_current_subfile (),
1458 first_line + L_LNNO32 (&lptr),
1459 gdbarch_addr_bits_remove (gdbarch, addr));
1467 patch_type (struct type *type, struct type *real_type)
1469 struct type *target = TYPE_TARGET_TYPE (type);
1470 struct type *real_target = TYPE_TARGET_TYPE (real_type);
1471 int field_size = TYPE_NFIELDS (real_target) * sizeof (struct field);
1473 TYPE_LENGTH (target) = TYPE_LENGTH (real_target);
1474 TYPE_NFIELDS (target) = TYPE_NFIELDS (real_target);
1475 TYPE_FIELDS (target) = (struct field *) TYPE_ALLOC (target,
1478 memcpy (TYPE_FIELDS (target),
1479 TYPE_FIELDS (real_target),
1482 if (TYPE_NAME (real_target))
1484 /* The previous copy of TYPE_NAME is allocated by
1485 process_coff_symbol. */
1486 if (TYPE_NAME (target))
1487 xfree ((char*) TYPE_NAME (target));
1488 TYPE_NAME (target) = xstrdup (TYPE_NAME (real_target));
1492 /* Patch up all appropriate typedef symbols in the opaque_type_chains
1493 so that they can be used to print out opaque data structures
1497 patch_opaque_types (struct symtab *s)
1500 struct block_iterator iter;
1501 struct symbol *real_sym;
1503 /* Go through the per-file symbols only. */
1504 b = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (s), STATIC_BLOCK);
1505 ALL_BLOCK_SYMBOLS (b, iter, real_sym)
1507 /* Find completed typedefs to use to fix opaque ones.
1508 Remove syms from the chain when their types are stored,
1509 but search the whole chain, as there may be several syms
1510 from different files with the same name. */
1511 if (SYMBOL_CLASS (real_sym) == LOC_TYPEDEF
1512 && SYMBOL_DOMAIN (real_sym) == VAR_DOMAIN
1513 && TYPE_CODE (SYMBOL_TYPE (real_sym)) == TYPE_CODE_PTR
1514 && TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (real_sym))) != 0)
1516 const char *name = SYMBOL_LINKAGE_NAME (real_sym);
1517 int hash = hashname (name);
1518 struct symbol *sym, *prev;
1521 for (sym = opaque_type_chain[hash]; sym;)
1523 if (name[0] == SYMBOL_LINKAGE_NAME (sym)[0]
1524 && strcmp (name + 1, SYMBOL_LINKAGE_NAME (sym) + 1) == 0)
1528 SYMBOL_VALUE_CHAIN (prev) = SYMBOL_VALUE_CHAIN (sym);
1532 opaque_type_chain[hash] = SYMBOL_VALUE_CHAIN (sym);
1535 patch_type (SYMBOL_TYPE (sym), SYMBOL_TYPE (real_sym));
1539 sym = SYMBOL_VALUE_CHAIN (prev);
1543 sym = opaque_type_chain[hash];
1549 sym = SYMBOL_VALUE_CHAIN (sym);
1557 coff_reg_to_regnum (struct symbol *sym, struct gdbarch *gdbarch)
1559 return gdbarch_sdb_reg_to_regnum (gdbarch, SYMBOL_VALUE (sym));
1562 static const struct symbol_register_ops coff_register_funcs = {
1566 /* The "aclass" index for computed COFF symbols. */
1568 static int coff_register_index;
1570 static struct symbol *
1571 process_coff_symbol (struct coff_symbol *cs,
1572 union internal_auxent *aux,
1573 struct objfile *objfile)
1575 struct symbol *sym = allocate_symbol (objfile);
1579 name = EXTERNAL_NAME (name, objfile->obfd);
1580 SYMBOL_SET_LANGUAGE (sym, get_current_subfile ()->language,
1581 &objfile->objfile_obstack);
1582 SYMBOL_SET_NAMES (sym, name, strlen (name), 1, objfile);
1584 /* default assumptions */
1585 SYMBOL_VALUE (sym) = cs->c_value;
1586 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
1587 SYMBOL_SECTION (sym) = cs_to_section (cs, objfile);
1589 if (ISFCN (cs->c_type))
1591 SYMBOL_VALUE (sym) += ANOFFSET (objfile->section_offsets,
1592 SECT_OFF_TEXT (objfile));
1594 lookup_function_type (decode_function_type (cs, cs->c_type,
1597 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
1598 if (cs->c_sclass == C_STAT || cs->c_sclass == C_THUMBSTAT
1599 || cs->c_sclass == C_THUMBSTATFUNC)
1600 add_symbol_to_list (sym, get_file_symbols ());
1601 else if (cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXT
1602 || cs->c_sclass == C_THUMBEXTFUNC)
1603 add_symbol_to_list (sym, get_global_symbols ());
1607 SYMBOL_TYPE (sym) = decode_type (cs, cs->c_type, aux, objfile);
1608 switch (cs->c_sclass)
1614 SYMBOL_ACLASS_INDEX (sym) = LOC_LOCAL;
1615 add_symbol_to_list (sym, get_local_symbols ());
1619 case C_THUMBEXTFUNC:
1621 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
1622 SYMBOL_VALUE_ADDRESS (sym) = (CORE_ADDR) cs->c_value;
1623 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
1624 SECT_OFF_TEXT (objfile));
1625 add_symbol_to_list (sym, get_global_symbols ());
1629 case C_THUMBSTATFUNC:
1631 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
1632 SYMBOL_VALUE_ADDRESS (sym) = (CORE_ADDR) cs->c_value;
1633 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
1634 SECT_OFF_TEXT (objfile));
1635 if (within_function)
1637 /* Static symbol of local scope. */
1638 add_symbol_to_list (sym, get_local_symbols ());
1642 /* Static symbol at top level of file. */
1643 add_symbol_to_list (sym, get_file_symbols ());
1647 #ifdef C_GLBLREG /* AMD coff */
1651 SYMBOL_ACLASS_INDEX (sym) = coff_register_index;
1652 SYMBOL_VALUE (sym) = cs->c_value;
1653 add_symbol_to_list (sym, get_local_symbols ());
1661 SYMBOL_ACLASS_INDEX (sym) = LOC_ARG;
1662 SYMBOL_IS_ARGUMENT (sym) = 1;
1663 add_symbol_to_list (sym, get_local_symbols ());
1667 SYMBOL_ACLASS_INDEX (sym) = coff_register_index;
1668 SYMBOL_IS_ARGUMENT (sym) = 1;
1669 SYMBOL_VALUE (sym) = cs->c_value;
1670 add_symbol_to_list (sym, get_local_symbols ());
1674 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
1675 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
1677 /* If type has no name, give it one. */
1678 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
1680 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_PTR
1681 || TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_FUNC)
1683 /* If we are giving a name to a type such as
1684 "pointer to foo" or "function returning foo", we
1685 better not set the TYPE_NAME. If the program
1686 contains "typedef char *caddr_t;", we don't want
1687 all variables of type char * to print as caddr_t.
1688 This is not just a consequence of GDB's type
1689 management; CC and GCC (at least through version
1690 2.4) both output variables of either type char *
1691 or caddr_t with the type refering to the C_TPDEF
1692 symbol for caddr_t. If a future compiler cleans
1693 this up it GDB is not ready for it yet, but if it
1694 becomes ready we somehow need to disable this
1695 check (without breaking the PCC/GCC2.4 case).
1699 Fortunately, this check seems not to be necessary
1700 for anything except pointers or functions. */
1704 TYPE_NAME (SYMBOL_TYPE (sym)) =
1705 xstrdup (SYMBOL_LINKAGE_NAME (sym));
1708 /* Keep track of any type which points to empty structured
1709 type, so it can be filled from a definition from another
1710 file. A simple forward reference (TYPE_CODE_UNDEF) is
1711 not an empty structured type, though; the forward
1712 references work themselves out via the magic of
1713 coff_lookup_type. */
1714 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_PTR
1715 && TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym))) == 0
1716 && TYPE_CODE (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym)))
1719 int i = hashname (SYMBOL_LINKAGE_NAME (sym));
1721 SYMBOL_VALUE_CHAIN (sym) = opaque_type_chain[i];
1722 opaque_type_chain[i] = sym;
1724 add_symbol_to_list (sym, get_file_symbols ());
1730 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
1731 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
1733 /* Some compilers try to be helpful by inventing "fake"
1734 names for anonymous enums, structures, and unions, like
1735 "~0fake" or ".0fake". Thanks, but no thanks... */
1736 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
1737 if (SYMBOL_LINKAGE_NAME (sym) != NULL
1738 && *SYMBOL_LINKAGE_NAME (sym) != '~'
1739 && *SYMBOL_LINKAGE_NAME (sym) != '.')
1740 TYPE_NAME (SYMBOL_TYPE (sym)) =
1741 concat (SYMBOL_LINKAGE_NAME (sym), (char *)NULL);
1743 add_symbol_to_list (sym, get_file_symbols ());
1753 /* Decode a coff type specifier; return the type that is meant. */
1755 static struct type *
1756 decode_type (struct coff_symbol *cs, unsigned int c_type,
1757 union internal_auxent *aux, struct objfile *objfile)
1759 struct type *type = 0;
1760 unsigned int new_c_type;
1762 if (c_type & ~N_BTMASK)
1764 new_c_type = DECREF (c_type);
1767 type = decode_type (cs, new_c_type, aux, objfile);
1768 type = lookup_pointer_type (type);
1770 else if (ISFCN (c_type))
1772 type = decode_type (cs, new_c_type, aux, objfile);
1773 type = lookup_function_type (type);
1775 else if (ISARY (c_type))
1778 unsigned short *dim;
1779 struct type *base_type, *index_type, *range_type;
1781 /* Define an array type. */
1782 /* auxent refers to array, not base type. */
1783 if (aux->x_sym.x_tagndx.l == 0)
1786 /* Shift the indices down. */
1787 dim = &aux->x_sym.x_fcnary.x_ary.x_dimen[0];
1790 for (i = 0; *dim && i < DIMNUM - 1; i++, dim++)
1794 base_type = decode_type (cs, new_c_type, aux, objfile);
1795 index_type = objfile_type (objfile)->builtin_int;
1797 = create_static_range_type ((struct type *) NULL,
1798 index_type, 0, n - 1);
1800 create_array_type ((struct type *) NULL,
1801 base_type, range_type);
1806 /* Reference to existing type. This only occurs with the struct,
1807 union, and enum types. EPI a29k coff fakes us out by producing
1808 aux entries with a nonzero x_tagndx for definitions of structs,
1809 unions, and enums, so we have to check the c_sclass field. SCO
1810 3.2v4 cc gets confused with pointers to pointers to defined
1811 structs, and generates negative x_tagndx fields. */
1812 if (cs->c_naux > 0 && aux->x_sym.x_tagndx.l != 0)
1814 if (cs->c_sclass != C_STRTAG
1815 && cs->c_sclass != C_UNTAG
1816 && cs->c_sclass != C_ENTAG
1817 && aux->x_sym.x_tagndx.l >= 0)
1819 type = coff_alloc_type (aux->x_sym.x_tagndx.l);
1824 complaint (_("Symbol table entry for %s has bad tagndx value"),
1826 /* And fall through to decode_base_type... */
1830 return decode_base_type (cs, BTYPE (c_type), aux, objfile);
1833 /* Decode a coff type specifier for function definition;
1834 return the type that the function returns. */
1836 static struct type *
1837 decode_function_type (struct coff_symbol *cs,
1838 unsigned int c_type,
1839 union internal_auxent *aux,
1840 struct objfile *objfile)
1842 if (aux->x_sym.x_tagndx.l == 0)
1843 cs->c_naux = 0; /* auxent refers to function, not base
1846 return decode_type (cs, DECREF (c_type), aux, objfile);
1849 /* Basic C types. */
1851 static struct type *
1852 decode_base_type (struct coff_symbol *cs,
1853 unsigned int c_type,
1854 union internal_auxent *aux,
1855 struct objfile *objfile)
1857 struct gdbarch *gdbarch = get_objfile_arch (objfile);
1863 /* Shows up with "void (*foo)();" structure members. */
1864 return objfile_type (objfile)->builtin_void;
1868 /* Intel 960 COFF has this symbol and meaning. */
1869 return objfile_type (objfile)->builtin_void;
1873 return objfile_type (objfile)->builtin_char;
1876 return objfile_type (objfile)->builtin_short;
1879 return objfile_type (objfile)->builtin_int;
1882 if (cs->c_sclass == C_FIELD
1883 && aux->x_sym.x_misc.x_lnsz.x_size
1884 > gdbarch_long_bit (gdbarch))
1885 return objfile_type (objfile)->builtin_long_long;
1887 return objfile_type (objfile)->builtin_long;
1890 return objfile_type (objfile)->builtin_float;
1893 return objfile_type (objfile)->builtin_double;
1896 return objfile_type (objfile)->builtin_long_double;
1899 if (cs->c_naux != 1)
1901 /* Anonymous structure type. */
1902 type = coff_alloc_type (cs->c_symnum);
1903 TYPE_CODE (type) = TYPE_CODE_STRUCT;
1904 TYPE_NAME (type) = NULL;
1905 INIT_CPLUS_SPECIFIC (type);
1906 TYPE_LENGTH (type) = 0;
1907 TYPE_FIELDS (type) = 0;
1908 TYPE_NFIELDS (type) = 0;
1912 type = coff_read_struct_type (cs->c_symnum,
1913 aux->x_sym.x_misc.x_lnsz.x_size,
1914 aux->x_sym.x_fcnary.x_fcn.x_endndx.l,
1920 if (cs->c_naux != 1)
1922 /* Anonymous union type. */
1923 type = coff_alloc_type (cs->c_symnum);
1924 TYPE_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,
1937 TYPE_CODE (type) = TYPE_CODE_UNION;
1941 if (cs->c_naux != 1)
1943 /* Anonymous enum type. */
1944 type = coff_alloc_type (cs->c_symnum);
1945 TYPE_CODE (type) = TYPE_CODE_ENUM;
1946 TYPE_NAME (type) = NULL;
1947 TYPE_LENGTH (type) = 0;
1948 TYPE_FIELDS (type) = 0;
1949 TYPE_NFIELDS (type) = 0;
1953 type = coff_read_enum_type (cs->c_symnum,
1954 aux->x_sym.x_misc.x_lnsz.x_size,
1955 aux->x_sym.x_fcnary.x_fcn.x_endndx.l,
1961 /* Shouldn't show up here. */
1965 return objfile_type (objfile)->builtin_unsigned_char;
1968 return objfile_type (objfile)->builtin_unsigned_short;
1971 return objfile_type (objfile)->builtin_unsigned_int;
1974 if (cs->c_sclass == C_FIELD
1975 && aux->x_sym.x_misc.x_lnsz.x_size
1976 > gdbarch_long_bit (gdbarch))
1977 return objfile_type (objfile)->builtin_unsigned_long_long;
1979 return objfile_type (objfile)->builtin_unsigned_long;
1981 complaint (_("Unexpected type for symbol %s"), cs->c_name);
1982 return objfile_type (objfile)->builtin_void;
1985 /* This page contains subroutines of read_type. */
1987 /* Read the description of a structure (or union type) and return an
1988 object describing the type. */
1990 static struct type *
1991 coff_read_struct_type (int index, int length, int lastsym,
1992 struct objfile *objfile)
1996 struct nextfield *next;
2001 struct nextfield *list = 0;
2002 struct nextfield *newobj;
2006 struct coff_symbol member_sym;
2007 struct coff_symbol *ms = &member_sym;
2008 struct internal_syment sub_sym;
2009 union internal_auxent sub_aux;
2012 type = coff_alloc_type (index);
2013 TYPE_CODE (type) = TYPE_CODE_STRUCT;
2014 INIT_CPLUS_SPECIFIC (type);
2015 TYPE_LENGTH (type) = length;
2017 while (!done && symnum < lastsym && symnum < nlist_nsyms_global)
2019 read_one_sym (ms, &sub_sym, &sub_aux);
2021 name = EXTERNAL_NAME (name, objfile->obfd);
2023 switch (ms->c_sclass)
2028 /* Get space to record the next field's data. */
2029 newobj = XALLOCA (struct nextfield);
2030 newobj->next = list;
2033 /* Save the data. */
2035 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
2036 name, strlen (name));
2037 FIELD_TYPE (list->field) = decode_type (ms, ms->c_type,
2039 SET_FIELD_BITPOS (list->field, 8 * ms->c_value);
2040 FIELD_BITSIZE (list->field) = 0;
2046 /* Get space to record the next field's data. */
2047 newobj = XALLOCA (struct nextfield);
2048 newobj->next = list;
2051 /* Save the data. */
2053 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
2054 name, strlen (name));
2055 FIELD_TYPE (list->field) = decode_type (ms, ms->c_type,
2057 SET_FIELD_BITPOS (list->field, ms->c_value);
2058 FIELD_BITSIZE (list->field) = sub_aux.x_sym.x_misc.x_lnsz.x_size;
2067 /* Now create the vector of fields, and record how big it is. */
2069 TYPE_NFIELDS (type) = nfields;
2070 TYPE_FIELDS (type) = (struct field *)
2071 TYPE_ALLOC (type, sizeof (struct field) * nfields);
2073 /* Copy the saved-up fields into the field vector. */
2075 for (n = nfields; list; list = list->next)
2076 TYPE_FIELD (type, --n) = list->field;
2081 /* Read a definition of an enumeration type,
2082 and create and return a suitable type object.
2083 Also defines the symbols that represent the values of the type. */
2085 static struct type *
2086 coff_read_enum_type (int index, int length, int lastsym,
2087 struct objfile *objfile)
2089 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2094 struct pending **symlist;
2095 struct coff_symbol member_sym;
2096 struct coff_symbol *ms = &member_sym;
2097 struct internal_syment sub_sym;
2098 union internal_auxent sub_aux;
2099 struct pending *osyms, *syms;
2103 int unsigned_enum = 1;
2105 type = coff_alloc_type (index);
2106 if (within_function)
2107 symlist = get_local_symbols ();
2109 symlist = get_file_symbols ();
2111 o_nsyms = osyms ? osyms->nsyms : 0;
2113 while (!done && symnum < lastsym && symnum < nlist_nsyms_global)
2115 read_one_sym (ms, &sub_sym, &sub_aux);
2117 name = EXTERNAL_NAME (name, objfile->obfd);
2119 switch (ms->c_sclass)
2122 sym = allocate_symbol (objfile);
2124 name = (char *) obstack_copy0 (&objfile->objfile_obstack, name,
2126 SYMBOL_SET_LINKAGE_NAME (sym, name);
2127 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
2128 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
2129 SYMBOL_VALUE (sym) = ms->c_value;
2130 add_symbol_to_list (sym, symlist);
2135 /* Sometimes the linker (on 386/ix 2.0.2 at least) screws
2136 up the count of how many symbols to read. So stop
2143 /* Now fill in the fields of the type-structure. */
2146 TYPE_LENGTH (type) = length;
2147 else /* Assume ints. */
2148 TYPE_LENGTH (type) = gdbarch_int_bit (gdbarch) / TARGET_CHAR_BIT;
2149 TYPE_CODE (type) = TYPE_CODE_ENUM;
2150 TYPE_NFIELDS (type) = nsyms;
2151 TYPE_FIELDS (type) = (struct field *)
2152 TYPE_ALLOC (type, sizeof (struct field) * nsyms);
2154 /* Find the symbols for the values and put them into the type.
2155 The symbols can be found in the symlist that we put them on
2156 to cause them to be defined. osyms contains the old value
2157 of that symlist; everything up to there was defined by us. */
2158 /* Note that we preserve the order of the enum constants, so
2159 that in something like "enum {FOO, LAST_THING=FOO}" we print
2160 FOO, not LAST_THING. */
2162 for (syms = *symlist, n = 0; syms; syms = syms->next)
2168 for (; j < syms->nsyms; j++, n++)
2170 struct symbol *xsym = syms->symbol[j];
2172 SYMBOL_TYPE (xsym) = type;
2173 TYPE_FIELD_NAME (type, n) = SYMBOL_LINKAGE_NAME (xsym);
2174 SET_FIELD_ENUMVAL (TYPE_FIELD (type, n), SYMBOL_VALUE (xsym));
2175 if (SYMBOL_VALUE (xsym) < 0)
2177 TYPE_FIELD_BITSIZE (type, n) = 0;
2184 TYPE_UNSIGNED (type) = 1;
2189 /* Register our ability to parse symbols for coff BFD files. */
2191 static const struct sym_fns coff_sym_fns =
2193 coff_new_init, /* sym_new_init: init anything gbl to
2195 coff_symfile_init, /* sym_init: read initial info, setup
2197 coff_symfile_read, /* sym_read: read a symbol file into
2199 NULL, /* sym_read_psymbols */
2200 coff_symfile_finish, /* sym_finish: finished with file,
2202 default_symfile_offsets, /* sym_offsets: xlate external to
2204 default_symfile_segments, /* sym_segments: Get segment
2205 information from a file */
2206 NULL, /* sym_read_linetable */
2208 default_symfile_relocate, /* sym_relocate: Relocate a debug
2210 NULL, /* sym_probe_fns */
2214 /* Free the per-objfile COFF data. */
2217 coff_free_info (struct objfile *objfile, void *arg)
2223 _initialize_coffread (void)
2225 add_symtab_fns (bfd_target_coff_flavour, &coff_sym_fns);
2227 coff_objfile_data_key = register_objfile_data_with_cleanup (NULL,
2231 = register_symbol_register_impl (LOC_REGISTER, &coff_register_funcs);