Automatic date update in version.in
[binutils-gdb.git] / gdb / coffread.c
blob327c014a5a4c9a1c91d7bd85e916601a2aa1f841
1 /* Read coff symbol tables and convert to internal format, for GDB.
2 Copyright (C) 1987-2024 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/>. */
20 #include "symtab.h"
21 #include "gdbtypes.h"
22 #include "demangle.h"
23 #include "breakpoint.h"
25 #include "bfd.h"
26 #include "gdbsupport/gdb_obstack.h"
27 #include <ctype.h>
29 #include "coff/internal.h"
30 #include "libcoff.h"
31 #include "objfiles.h"
32 #include "buildsym-legacy.h"
33 #include "stabsread.h"
34 #include "complaints.h"
35 #include "target.h"
36 #include "block.h"
37 #include "dictionary.h"
38 #include "dwarf2/public.h"
40 #include "coff-pe-read.h"
42 /* The objfile we are currently reading. */
44 static struct objfile *coffread_objfile;
46 struct coff_symfile_info
48 file_ptr min_lineno_offset = 0; /* Where in file lowest line#s are. */
49 file_ptr max_lineno_offset = 0; /* 1+last byte of line#s in file. */
51 CORE_ADDR textaddr = 0; /* Addr of .text section. */
52 unsigned int textsize = 0; /* Size of .text section. */
53 std::vector<asection *> *stabsects; /* .stab sections. */
54 asection *stabstrsect = nullptr; /* Section pointer for .stab section. */
55 char *stabstrdata = nullptr;
58 /* Key for COFF-associated data. */
60 static const registry<objfile>::key<coff_symfile_info> coff_objfile_data_key;
62 /* Translate an external name string into a user-visible name. */
63 #define EXTERNAL_NAME(string, abfd) \
64 (*string != '\0' && *string == bfd_get_symbol_leading_char (abfd) \
65 ? string + 1 : string)
67 /* To be an sdb debug type, type must have at least a basic or primary
68 derived type. Using this rather than checking against T_NULL is
69 said to prevent core dumps if we try to operate on Michael Bloom
70 dbx-in-coff file. */
72 #define SDB_TYPE(type) (BTYPE(type) | (type & N_TMASK))
74 /* Core address of start and end of text of current source file.
75 This comes from a ".text" symbol where x_nlinno > 0. */
77 static CORE_ADDR current_source_start_addr;
78 static CORE_ADDR current_source_end_addr;
80 /* The addresses of the symbol table stream and number of symbols
81 of the object file we are reading (as copied into core). */
83 static bfd *nlist_bfd_global;
84 static int nlist_nsyms_global;
87 /* Pointers to scratch storage, used for reading raw symbols and
88 auxents. */
90 static char *temp_sym;
91 static char *temp_aux;
93 /* Local variables that hold the shift and mask values for the
94 COFF file that we are currently reading. These come back to us
95 from BFD, and are referenced by their macro names, as well as
96 internally to the BTYPE, ISPTR, ISFCN, ISARY, ISTAG, and DECREF
97 macros from include/coff/internal.h . */
99 static unsigned local_n_btmask;
100 static unsigned local_n_btshft;
101 static unsigned local_n_tmask;
102 static unsigned local_n_tshift;
104 #define N_BTMASK local_n_btmask
105 #define N_BTSHFT local_n_btshft
106 #define N_TMASK local_n_tmask
107 #define N_TSHIFT local_n_tshift
109 /* Local variables that hold the sizes in the file of various COFF
110 structures. (We only need to know this to read them from the file
111 -- BFD will then translate the data in them, into `internal_xxx'
112 structs in the right byte order, alignment, etc.) */
114 static unsigned local_linesz;
115 static unsigned local_symesz;
116 static unsigned local_auxesz;
118 /* This is set if this is a PE format file. */
120 static int pe_file;
122 /* Chain of typedefs of pointers to empty struct/union types.
123 They are chained thru the SYMBOL_VALUE_CHAIN. */
125 static struct symbol *opaque_type_chain[HASHSIZE];
127 /* Simplified internal version of coff symbol table information. */
129 struct coff_symbol
131 char *c_name;
132 int c_symnum; /* Symbol number of this entry. */
133 int c_naux; /* 0 if syment only, 1 if syment +
134 auxent, etc. */
135 CORE_ADDR c_value;
136 int c_sclass;
137 int c_secnum;
138 unsigned int c_type;
141 /* Vector of types defined so far, indexed by their type numbers. */
143 static struct type **type_vector;
145 /* Number of elements allocated for type_vector currently. */
147 static int type_vector_length;
149 /* Initial size of type vector. Is realloc'd larger if needed, and
150 realloc'd down to the size actually used, when completed. */
152 #define INITIAL_TYPE_VECTOR_LENGTH 160
154 static char *linetab = NULL;
155 static file_ptr linetab_offset;
156 static file_ptr linetab_size;
158 static char *stringtab = NULL;
159 static long stringtab_length = 0;
161 extern void stabsread_clear_cache (void);
163 static struct type *coff_read_struct_type (int, int, int,
164 struct objfile *);
166 static struct type *decode_base_type (struct coff_symbol *,
167 unsigned int,
168 union internal_auxent *,
169 struct objfile *);
171 static struct type *decode_type (struct coff_symbol *, unsigned int,
172 union internal_auxent *,
173 struct objfile *);
175 static struct type *decode_function_type (struct coff_symbol *,
176 unsigned int,
177 union internal_auxent *,
178 struct objfile *);
180 static struct type *coff_read_enum_type (int, int, int,
181 struct objfile *);
183 static struct symbol *process_coff_symbol (struct coff_symbol *,
184 union internal_auxent *,
185 struct objfile *);
187 static void patch_opaque_types (struct symtab *);
189 static void enter_linenos (file_ptr, int, int, struct objfile *);
191 static int init_lineno (bfd *, file_ptr, file_ptr, gdb::unique_xmalloc_ptr<char> *);
193 static char *getsymname (struct internal_syment *);
195 static const char *coff_getfilename (union internal_auxent *);
197 static int init_stringtab (bfd *, file_ptr, gdb::unique_xmalloc_ptr<char> *);
199 static void read_one_sym (struct coff_symbol *,
200 struct internal_syment *,
201 union internal_auxent *);
203 static void coff_symtab_read (minimal_symbol_reader &,
204 file_ptr, unsigned int, struct objfile *);
206 /* We are called once per section from coff_symfile_read. We
207 need to examine each section we are passed, check to see
208 if it is something we are interested in processing, and
209 if so, stash away some access information for the section.
211 FIXME: The section names should not be hardwired strings (what
212 should they be? I don't think most object file formats have enough
213 section flags to specify what kind of debug section it is
214 -kingdon). */
216 static void
217 coff_locate_sections (bfd *abfd, asection *sectp, void *csip)
219 struct coff_symfile_info *csi;
220 const char *name;
222 csi = (struct coff_symfile_info *) csip;
223 name = bfd_section_name (sectp);
224 if (strcmp (name, ".text") == 0)
226 csi->textaddr = bfd_section_vma (sectp);
227 csi->textsize += bfd_section_size (sectp);
229 else if (startswith (name, ".text"))
231 csi->textsize += bfd_section_size (sectp);
233 else if (strcmp (name, ".stabstr") == 0)
235 csi->stabstrsect = sectp;
237 else if (startswith (name, ".stab"))
239 const char *s;
241 /* We can have multiple .stab sections if linked with
242 --split-by-reloc. */
243 for (s = name + sizeof ".stab" - 1; *s != '\0'; s++)
244 if (!isdigit (*s))
245 break;
246 if (*s == '\0')
247 csi->stabsects->push_back (sectp);
251 /* Return the section_offsets* that CS points to. */
252 static int cs_to_section (struct coff_symbol *, struct objfile *);
254 struct coff_find_targ_sec_arg
256 int targ_index;
257 asection **resultp;
260 static void
261 find_targ_sec (bfd *abfd, asection *sect, void *obj)
263 struct coff_find_targ_sec_arg *args = (struct coff_find_targ_sec_arg *) obj;
265 if (sect->target_index == args->targ_index)
266 *args->resultp = sect;
269 /* Return the bfd_section that CS points to. */
270 static struct bfd_section*
271 cs_to_bfd_section (struct coff_symbol *cs, struct objfile *objfile)
273 asection *sect = NULL;
274 struct coff_find_targ_sec_arg args;
276 args.targ_index = cs->c_secnum;
277 args.resultp = &sect;
278 bfd_map_over_sections (objfile->obfd.get (), find_targ_sec, &args);
279 return sect;
282 /* Return the section number (SECT_OFF_*) that CS points to. */
283 static int
284 cs_to_section (struct coff_symbol *cs, struct objfile *objfile)
286 asection *sect = cs_to_bfd_section (cs, objfile);
288 if (sect == NULL)
289 return SECT_OFF_TEXT (objfile);
290 return gdb_bfd_section_index (objfile->obfd.get (), sect);
293 /* Return the address of the section of a COFF symbol. */
295 static CORE_ADDR cs_section_address (struct coff_symbol *, bfd *);
297 static CORE_ADDR
298 cs_section_address (struct coff_symbol *cs, bfd *abfd)
300 asection *sect = NULL;
301 struct coff_find_targ_sec_arg args;
302 CORE_ADDR addr = 0;
304 args.targ_index = cs->c_secnum;
305 args.resultp = &sect;
306 bfd_map_over_sections (abfd, find_targ_sec, &args);
307 if (sect != NULL)
308 addr = bfd_section_vma (sect);
309 return addr;
312 /* Look up a coff type-number index. Return the address of the slot
313 where the type for that index is stored.
314 The type-number is in INDEX.
316 This can be used for finding the type associated with that index
317 or for associating a new type with the index. */
319 static struct type **
320 coff_lookup_type (int index)
322 if (index >= type_vector_length)
324 int old_vector_length = type_vector_length;
326 type_vector_length *= 2;
327 if (index /* is still */ >= type_vector_length)
328 type_vector_length = index * 2;
330 type_vector = (struct type **)
331 xrealloc ((char *) type_vector,
332 type_vector_length * sizeof (struct type *));
333 memset (&type_vector[old_vector_length], 0,
334 (type_vector_length - old_vector_length) * sizeof (struct type *));
336 return &type_vector[index];
339 /* Make sure there is a type allocated for type number index
340 and return the type object.
341 This can create an empty (zeroed) type object. */
343 static struct type *
344 coff_alloc_type (int index)
346 struct type **type_addr = coff_lookup_type (index);
347 struct type *type = *type_addr;
349 /* If we are referring to a type not known at all yet,
350 allocate an empty type for it.
351 We will fill it in later if we find out how. */
352 if (type == NULL)
354 type = type_allocator (coffread_objfile, language_c).new_type ();
355 *type_addr = type;
357 return type;
360 /* Start a new symtab for a new source file.
361 This is called when a COFF ".file" symbol is seen;
362 it indicates the start of data for one original source file. */
364 static void
365 coff_start_compunit_symtab (struct objfile *objfile, const char *name)
367 within_function = 0;
368 start_compunit_symtab (objfile,
369 name,
370 /* We never know the directory name for COFF. */
371 NULL,
372 /* The start address is irrelevant, since we call
373 set_last_source_start_addr in coff_end_compunit_symtab. */
375 /* Let buildsym.c deduce the language for this symtab. */
376 language_unknown);
377 record_debugformat ("COFF");
380 /* Save the vital information from when starting to read a file,
381 for use when closing off the current file.
382 NAME is the file name the symbols came from, START_ADDR is the
383 first text address for the file, and SIZE is the number of bytes of
384 text. */
386 static void
387 complete_symtab (const char *name, CORE_ADDR start_addr, unsigned int size)
389 set_last_source_file (name);
390 current_source_start_addr = start_addr;
391 current_source_end_addr = start_addr + size;
394 /* Finish the symbol definitions for one main source file, close off
395 all the lexical contexts for that file (creating struct block's for
396 them), then make the struct symtab for that file and put it in the
397 list of all such. */
399 static void
400 coff_end_compunit_symtab (struct objfile *objfile)
402 set_last_source_start_addr (current_source_start_addr);
404 end_compunit_symtab (current_source_end_addr);
406 /* Reinitialize for beginning of new file. */
407 set_last_source_file (NULL);
410 /* The linker sometimes generates some non-function symbols inside
411 functions referencing variables imported from another DLL.
412 Return nonzero if the given symbol corresponds to one of them. */
414 static int
415 is_import_fixup_symbol (struct coff_symbol *cs,
416 enum minimal_symbol_type type)
418 /* The following is a bit of a heuristic using the characteristics
419 of these fixup symbols, but should work well in practice... */
420 int i;
422 /* Must be a non-static text symbol. */
423 if (type != mst_text)
424 return 0;
426 /* Must be a non-function symbol. */
427 if (ISFCN (cs->c_type))
428 return 0;
430 /* The name must start with "__fu<digits>__". */
431 if (!startswith (cs->c_name, "__fu"))
432 return 0;
433 if (! isdigit (cs->c_name[4]))
434 return 0;
435 for (i = 5; cs->c_name[i] != '\0' && isdigit (cs->c_name[i]); i++)
436 /* Nothing, just incrementing index past all digits. */;
437 if (cs->c_name[i] != '_' || cs->c_name[i + 1] != '_')
438 return 0;
440 return 1;
443 static struct minimal_symbol *
444 record_minimal_symbol (minimal_symbol_reader &reader,
445 struct coff_symbol *cs, unrelocated_addr address,
446 enum minimal_symbol_type type, int section,
447 struct objfile *objfile)
449 /* We don't want TDESC entry points in the minimal symbol table. */
450 if (cs->c_name[0] == '@')
451 return NULL;
453 if (is_import_fixup_symbol (cs, type))
455 /* Because the value of these symbols is within a function code
456 range, these symbols interfere with the symbol-from-address
457 reverse lookup; this manifests itself in backtraces, or any
458 other commands that prints symbolic addresses. Just pretend
459 these symbols do not exist. */
460 return NULL;
463 return reader.record_full (cs->c_name, true, address, type, section);
466 /* coff_symfile_init ()
467 is the coff-specific initialization routine for reading symbols.
468 It is passed a struct objfile which contains, among other things,
469 the BFD for the file whose symbols are being read, and a slot for
470 a pointer to "private data" which we fill with cookies and other
471 treats for coff_symfile_read ().
473 We will only be called if this is a COFF or COFF-like file. BFD
474 handles figuring out the format of the file, and code in symtab.c
475 uses BFD's determination to vector to us.
477 The ultimate result is a new symtab (or, FIXME, eventually a
478 psymtab). */
480 static void
481 coff_symfile_init (struct objfile *objfile)
483 /* Allocate struct to keep track of the symfile. */
484 coff_objfile_data_key.emplace (objfile);
487 /* This function is called for every section; it finds the outer
488 limits of the line table (minimum and maximum file offset) so that
489 the mainline code can read the whole thing for efficiency. */
491 static void
492 find_linenos (bfd *abfd, struct bfd_section *asect, void *vpinfo)
494 struct coff_symfile_info *info;
495 int size, count;
496 file_ptr offset, maxoff;
498 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
499 count = asect->lineno_count;
500 /* End of warning. */
502 if (count == 0)
503 return;
504 size = count * local_linesz;
506 info = (struct coff_symfile_info *) vpinfo;
507 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
508 offset = asect->line_filepos;
509 /* End of warning. */
511 if (offset < info->min_lineno_offset || info->min_lineno_offset == 0)
512 info->min_lineno_offset = offset;
514 maxoff = offset + size;
515 if (maxoff > info->max_lineno_offset)
516 info->max_lineno_offset = maxoff;
520 /* A helper function for coff_symfile_read that reads minimal
521 symbols. It may also read other forms of symbol as well. */
523 static void
524 coff_read_minsyms (file_ptr symtab_offset, unsigned int nsyms,
525 struct objfile *objfile)
528 /* If minimal symbols were already read, and if we know we aren't
529 going to read any other kind of symbol here, then we can just
530 return. */
531 if (objfile->per_bfd->minsyms_read && pe_file && nsyms == 0)
532 return;
534 minimal_symbol_reader reader (objfile);
536 if (pe_file && nsyms == 0)
538 /* We've got no debugging symbols, but it's a portable
539 executable, so try to read the export table. */
540 read_pe_exported_syms (reader, objfile);
542 else
544 /* Now that the executable file is positioned at symbol table,
545 process it and define symbols accordingly. */
546 coff_symtab_read (reader, symtab_offset, nsyms, objfile);
549 /* Install any minimal symbols that have been collected as the
550 current minimal symbols for this objfile. */
552 reader.install ();
554 if (pe_file)
556 for (minimal_symbol *msym : objfile->msymbols ())
558 const char *name = msym->linkage_name ();
560 /* If the minimal symbols whose name are prefixed by "__imp_"
561 or "_imp_", get rid of the prefix, and search the minimal
562 symbol in OBJFILE. Note that 'maintenance print msymbols'
563 shows that type of these "_imp_XXXX" symbols is mst_data. */
564 if (msym->type () == mst_data)
566 const char *name1 = NULL;
568 if (startswith (name, "_imp_"))
569 name1 = name + 5;
570 else if (startswith (name, "__imp_"))
571 name1 = name + 6;
572 if (name1 != NULL)
574 int lead
575 = bfd_get_symbol_leading_char (objfile->obfd.get ());
576 struct bound_minimal_symbol found;
578 if (lead != '\0' && *name1 == lead)
579 name1 += 1;
581 found = lookup_minimal_symbol (name1, NULL, objfile);
583 /* If found, there are symbols named "_imp_foo" and "foo"
584 respectively in OBJFILE. Set the type of symbol "foo"
585 as 'mst_solib_trampoline'. */
586 if (found.minsym != NULL
587 && found.minsym->type () == mst_text)
588 found.minsym->set_type (mst_solib_trampoline);
595 /* The BFD for this file -- only good while we're actively reading
596 symbols into a psymtab or a symtab. */
598 static bfd *symfile_bfd;
600 /* Read a symbol file, after initialization by coff_symfile_init. */
602 static void
603 coff_symfile_read (struct objfile *objfile, symfile_add_flags symfile_flags)
605 struct coff_symfile_info *info;
606 bfd *abfd = objfile->obfd.get ();
607 coff_data_type *cdata = coff_data (abfd);
608 const char *filename = bfd_get_filename (abfd);
609 int val;
610 unsigned int num_symbols;
611 file_ptr symtab_offset;
612 file_ptr stringtab_offset;
613 unsigned int stabstrsize;
615 info = coff_objfile_data_key.get (objfile);
616 symfile_bfd = abfd; /* Kludge for swap routines. */
618 std::vector<asection *> stabsects;
619 scoped_restore restore_stabsects
620 = make_scoped_restore (&info->stabsects, &stabsects);
622 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
623 num_symbols = bfd_get_symcount (abfd); /* How many syms */
624 symtab_offset = cdata->sym_filepos; /* Symbol table file offset */
625 stringtab_offset = symtab_offset + /* String table file offset */
626 num_symbols * cdata->local_symesz;
628 /* Set a few file-statics that give us specific information about
629 the particular COFF file format we're reading. */
630 local_n_btmask = cdata->local_n_btmask;
631 local_n_btshft = cdata->local_n_btshft;
632 local_n_tmask = cdata->local_n_tmask;
633 local_n_tshift = cdata->local_n_tshift;
634 local_linesz = cdata->local_linesz;
635 local_symesz = cdata->local_symesz;
636 local_auxesz = cdata->local_auxesz;
638 /* Allocate space for raw symbol and aux entries, based on their
639 space requirements as reported by BFD. */
640 gdb::def_vector<char> temp_storage (cdata->local_symesz
641 + cdata->local_auxesz);
642 temp_sym = temp_storage.data ();
643 temp_aux = temp_sym + cdata->local_symesz;
645 /* We need to know whether this is a PE file, because in PE files,
646 unlike standard COFF files, symbol values are stored as offsets
647 from the section address, rather than as absolute addresses.
648 FIXME: We should use BFD to read the symbol table, and thus avoid
649 this problem. */
650 pe_file =
651 startswith (bfd_get_target (objfile->obfd.get ()), "pe")
652 || startswith (bfd_get_target (objfile->obfd.get ()), "epoc-pe");
654 /* End of warning. */
656 info->min_lineno_offset = 0;
657 info->max_lineno_offset = 0;
659 /* Only read line number information if we have symbols.
661 On Windows NT, some of the system's DLL's have sections with
662 PointerToLinenumbers fields that are non-zero, but point at
663 random places within the image file. (In the case I found,
664 KERNEL32.DLL's .text section has a line number info pointer that
665 points into the middle of the string `lib\\i386\kernel32.dll'.)
667 However, these DLL's also have no symbols. The line number
668 tables are meaningless without symbols. And in fact, GDB never
669 uses the line number information unless there are symbols. So we
670 can avoid spurious error messages (and maybe run a little
671 faster!) by not even reading the line number table unless we have
672 symbols. */
673 scoped_restore restore_linetab = make_scoped_restore (&linetab);
674 gdb::unique_xmalloc_ptr<char> linetab_storage;
675 if (num_symbols > 0)
677 /* Read the line number table, all at once. */
678 bfd_map_over_sections (abfd, find_linenos, (void *) info);
680 val = init_lineno (abfd, info->min_lineno_offset,
681 info->max_lineno_offset - info->min_lineno_offset,
682 &linetab_storage);
683 if (val < 0)
684 error (_("\"%s\": error reading line numbers."), filename);
687 /* Now read the string table, all at once. */
689 scoped_restore restore_stringtab = make_scoped_restore (&stringtab);
690 gdb::unique_xmalloc_ptr<char> stringtab_storage;
691 val = init_stringtab (abfd, stringtab_offset, &stringtab_storage);
692 if (val < 0)
693 error (_("\"%s\": can't get string table"), filename);
695 coff_read_minsyms (symtab_offset, num_symbols, objfile);
697 if (!(objfile->flags & OBJF_READNEVER))
698 bfd_map_over_sections (abfd, coff_locate_sections, (void *) info);
700 if (!info->stabsects->empty())
702 if (!info->stabstrsect)
704 error (_("The debugging information in `%s' is corrupted.\nThe "
705 "file has a `.stabs' section, but no `.stabstr' section."),
706 filename);
709 /* FIXME: dubious. Why can't we use something normal like
710 bfd_get_section_contents? */
711 stabstrsize = bfd_section_size (info->stabstrsect);
713 coffstab_build_psymtabs (objfile,
714 info->textaddr, info->textsize,
715 *info->stabsects,
716 info->stabstrsect->filepos, stabstrsize);
719 if (dwarf2_initialize_objfile (objfile))
721 /* Nothing. */
724 /* Try to add separate debug file if no symbols table found. */
725 else if (!objfile->has_partial_symbols ()
726 && objfile->separate_debug_objfile == NULL
727 && objfile->separate_debug_objfile_backlink == NULL)
729 if (objfile->find_and_add_separate_symbol_file (symfile_flags))
730 gdb_assert (objfile->separate_debug_objfile != nullptr);
734 static void
735 coff_new_init (struct objfile *ignore)
739 /* Perform any local cleanups required when we are done with a
740 particular objfile. I.E, we are in the process of discarding all
741 symbol information for an objfile, freeing up all memory held for
742 it, and unlinking the objfile struct from the global list of known
743 objfiles. */
745 static void
746 coff_symfile_finish (struct objfile *objfile)
748 /* Let stabs reader clean up. */
749 stabsread_clear_cache ();
753 /* Given pointers to a symbol table in coff style exec file,
754 analyze them and create struct symtab's describing the symbols.
755 NSYMS is the number of symbols in the symbol table.
756 We read them one at a time using read_one_sym (). */
758 static void
759 coff_symtab_read (minimal_symbol_reader &reader,
760 file_ptr symtab_offset, unsigned int nsyms,
761 struct objfile *objfile)
763 struct gdbarch *gdbarch = objfile->arch ();
764 struct context_stack *newobj = nullptr;
765 struct coff_symbol coff_symbol;
766 struct coff_symbol *cs = &coff_symbol;
767 static struct internal_syment main_sym;
768 static union internal_auxent main_aux;
769 struct coff_symbol fcn_cs_saved;
770 static struct internal_syment fcn_sym_saved;
771 static union internal_auxent fcn_aux_saved;
772 /* A .file is open. */
773 int in_source_file = 0;
774 int next_file_symnum = -1;
775 /* Name of the current file. */
776 const char *filestring = "";
777 int depth = 0;
778 int fcn_first_line = 0;
779 CORE_ADDR fcn_first_line_addr = 0;
780 int fcn_last_line = 0;
781 int fcn_start_addr = 0;
782 long fcn_line_ptr = 0;
783 int val;
784 CORE_ADDR tmpaddr;
785 struct minimal_symbol *msym;
787 scoped_free_pendings free_pending;
789 /* Position to read the symbol table. */
790 val = bfd_seek (objfile->obfd.get (), symtab_offset, 0);
791 if (val < 0)
792 perror_with_name (objfile_name (objfile));
794 coffread_objfile = objfile;
795 nlist_bfd_global = objfile->obfd.get ();
796 nlist_nsyms_global = nsyms;
797 set_last_source_file (NULL);
798 memset (opaque_type_chain, 0, sizeof opaque_type_chain);
800 if (type_vector) /* Get rid of previous one. */
801 xfree (type_vector);
802 type_vector_length = INITIAL_TYPE_VECTOR_LENGTH;
803 type_vector = XCNEWVEC (struct type *, type_vector_length);
805 coff_start_compunit_symtab (objfile, "");
807 symnum = 0;
808 while (symnum < nsyms)
810 QUIT; /* Make this command interruptable. */
812 read_one_sym (cs, &main_sym, &main_aux);
814 if (cs->c_symnum == next_file_symnum && cs->c_sclass != C_FILE)
816 if (get_last_source_file ())
817 coff_end_compunit_symtab (objfile);
819 coff_start_compunit_symtab (objfile, "_globals_");
820 /* coff_start_compunit_symtab will set the language of this symtab to
821 language_unknown, since such a ``file name'' is not
822 recognized. Override that with the minimal language to
823 allow printing values in this symtab. */
824 get_current_subfile ()->language = language_minimal;
825 complete_symtab ("_globals_", 0, 0);
826 /* Done with all files, everything from here on out is
827 globals. */
830 /* Special case for file with type declarations only, no
831 text. */
832 if (!get_last_source_file () && SDB_TYPE (cs->c_type)
833 && cs->c_secnum == N_DEBUG)
834 complete_symtab (filestring, 0, 0);
836 /* Typedefs should not be treated as symbol definitions. */
837 if (ISFCN (cs->c_type) && cs->c_sclass != C_TPDEF)
839 /* Record all functions -- external and static -- in
840 minsyms. */
841 int section = cs_to_section (cs, objfile);
843 tmpaddr = cs->c_value;
844 /* Don't record unresolved symbols. */
845 if (!(cs->c_secnum <= 0 && cs->c_value == 0))
846 record_minimal_symbol (reader, cs,
847 unrelocated_addr (tmpaddr),
848 mst_text, section, objfile);
850 fcn_line_ptr = main_aux.x_sym.x_fcnary.x_fcn.x_lnnoptr;
851 fcn_start_addr = tmpaddr;
852 fcn_cs_saved = *cs;
853 fcn_sym_saved = main_sym;
854 fcn_aux_saved = main_aux;
855 continue;
858 switch (cs->c_sclass)
860 case C_EFCN:
861 case C_EXTDEF:
862 case C_ULABEL:
863 case C_USTATIC:
864 case C_LINE:
865 case C_ALIAS:
866 case C_HIDDEN:
867 complaint (_("Bad n_sclass for symbol %s"),
868 cs->c_name);
869 break;
871 case C_FILE:
872 /* c_value field contains symnum of next .file entry in
873 table or symnum of first global after last .file. */
874 next_file_symnum = cs->c_value;
875 if (cs->c_naux > 0)
876 filestring = coff_getfilename (&main_aux);
877 else
878 filestring = "";
880 /* Complete symbol table for last object file
881 containing debugging information. */
882 if (get_last_source_file ())
884 coff_end_compunit_symtab (objfile);
885 coff_start_compunit_symtab (objfile, filestring);
887 in_source_file = 1;
888 break;
890 /* C_LABEL is used for labels and static functions.
891 Including it here allows gdb to see static functions when
892 no debug info is available. */
893 case C_LABEL:
894 /* However, labels within a function can make weird
895 backtraces, so filter them out (from phdm@macqel.be). */
896 if (within_function)
897 break;
898 [[fallthrough]];
899 case C_STAT:
900 case C_THUMBLABEL:
901 case C_THUMBSTAT:
902 case C_THUMBSTATFUNC:
903 if (cs->c_name[0] == '.')
905 if (strcmp (cs->c_name, ".text") == 0)
907 /* FIXME: don't wire in ".text" as section name or
908 symbol name! */
909 /* Check for in_source_file deals with case of a
910 file with debugging symbols followed by a later
911 file with no symbols. */
912 if (in_source_file)
913 complete_symtab (filestring,
914 (cs->c_value
915 + objfile->text_section_offset ()),
916 main_aux.x_scn.x_scnlen);
917 in_source_file = 0;
919 /* Flush rest of '.' symbols. */
920 break;
922 else if (!SDB_TYPE (cs->c_type)
923 && cs->c_name[0] == 'L'
924 && (startswith (cs->c_name, "LI%")
925 || startswith (cs->c_name, "LF%")
926 || startswith (cs->c_name, "LC%")
927 || startswith (cs->c_name, "LP%")
928 || startswith (cs->c_name, "LPB%")
929 || startswith (cs->c_name, "LBB%")
930 || startswith (cs->c_name, "LBE%")
931 || startswith (cs->c_name, "LPBX%")))
932 /* At least on a 3b1, gcc generates swbeg and string labels
933 that look like this. Ignore them. */
934 break;
935 /* For static symbols that don't start with '.'... */
936 [[fallthrough]];
937 case C_THUMBEXT:
938 case C_THUMBEXTFUNC:
939 case C_EXT:
941 /* Record it in the minimal symbols regardless of
942 SDB_TYPE. This parallels what we do for other debug
943 formats, and probably is needed to make
944 print_address_symbolic work right without the (now
945 gone) "set fast-symbolic-addr off" kludge. */
947 enum minimal_symbol_type ms_type;
948 int sec;
949 CORE_ADDR offset = 0;
951 if (cs->c_secnum == N_UNDEF)
953 /* This is a common symbol. We used to rely on
954 the target to tell us whether it knows where
955 the symbol has been relocated to, but none of
956 the target implementations actually provided
957 that operation. So we just ignore the symbol,
958 the same way we would do if we had a target-side
959 symbol lookup which returned no match. */
960 break;
962 else if (cs->c_secnum == N_ABS)
964 /* Use the correct minimal symbol type (and don't
965 relocate) for absolute values. */
966 ms_type = mst_abs;
967 sec = cs_to_section (cs, objfile);
968 tmpaddr = cs->c_value;
970 else
972 asection *bfd_section = cs_to_bfd_section (cs, objfile);
974 sec = cs_to_section (cs, objfile);
975 tmpaddr = cs->c_value;
976 /* Statics in a PE file also get relocated. */
977 if (cs->c_sclass == C_EXT
978 || cs->c_sclass == C_THUMBEXTFUNC
979 || cs->c_sclass == C_THUMBEXT
980 || (pe_file && (cs->c_sclass == C_STAT)))
981 offset = objfile->section_offsets[sec];
983 if (bfd_section->flags & SEC_CODE)
985 ms_type =
986 cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXTFUNC
987 || cs->c_sclass == C_THUMBEXT ?
988 mst_text : mst_file_text;
989 tmpaddr = gdbarch_addr_bits_remove (gdbarch, tmpaddr);
991 else if (bfd_section->flags & SEC_ALLOC
992 && bfd_section->flags & SEC_LOAD)
994 ms_type =
995 cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXT
996 ? mst_data : mst_file_data;
998 else if (bfd_section->flags & SEC_ALLOC)
1000 ms_type =
1001 cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXT
1002 ? mst_bss : mst_file_bss;
1004 else
1005 ms_type = mst_unknown;
1008 msym = record_minimal_symbol (reader, cs,
1009 unrelocated_addr (tmpaddr),
1010 ms_type, sec, objfile);
1011 if (msym)
1012 gdbarch_coff_make_msymbol_special (gdbarch,
1013 cs->c_sclass, msym);
1015 if (SDB_TYPE (cs->c_type))
1017 struct symbol *sym;
1019 sym = process_coff_symbol
1020 (cs, &main_aux, objfile);
1021 sym->set_value_longest (tmpaddr + offset);
1022 sym->set_section_index (sec);
1025 break;
1027 case C_FCN:
1028 if (strcmp (cs->c_name, ".bf") == 0)
1030 within_function = 1;
1032 /* Value contains address of first non-init type
1033 code. */
1034 /* main_aux.x_sym.x_misc.x_lnsz.x_lnno
1035 contains line number of '{' }. */
1036 if (cs->c_naux != 1)
1037 complaint (_("`.bf' symbol %d has no aux entry"),
1038 cs->c_symnum);
1039 fcn_first_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
1040 fcn_first_line_addr = cs->c_value;
1042 /* Might want to check that locals are 0 and
1043 context_stack_depth is zero, and complain if not. */
1045 depth = 0;
1046 newobj = push_context (depth, fcn_start_addr);
1047 fcn_cs_saved.c_name = getsymname (&fcn_sym_saved);
1048 newobj->name =
1049 process_coff_symbol (&fcn_cs_saved,
1050 &fcn_aux_saved, objfile);
1052 else if (strcmp (cs->c_name, ".ef") == 0)
1054 if (!within_function)
1055 error (_("Bad coff function information."));
1056 /* The value of .ef is the address of epilogue code;
1057 not useful for gdb. */
1058 /* { main_aux.x_sym.x_misc.x_lnsz.x_lnno
1059 contains number of lines to '}' */
1061 if (outermost_context_p ())
1062 { /* We attempted to pop an empty context stack. */
1063 complaint (_("`.ef' symbol without matching `.bf' "
1064 "symbol ignored starting at symnum %d"),
1065 cs->c_symnum);
1066 within_function = 0;
1067 break;
1070 struct context_stack cstk = pop_context ();
1071 /* Stack must be empty now. */
1072 if (!outermost_context_p () || newobj == NULL)
1074 complaint (_("Unmatched .ef symbol(s) ignored "
1075 "starting at symnum %d"),
1076 cs->c_symnum);
1077 within_function = 0;
1078 break;
1080 if (cs->c_naux != 1)
1082 complaint (_("`.ef' symbol %d has no aux entry"),
1083 cs->c_symnum);
1084 fcn_last_line = 0x7FFFFFFF;
1086 else
1088 fcn_last_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
1090 /* fcn_first_line is the line number of the opening '{'.
1091 Do not record it - because it would affect gdb's idea
1092 of the line number of the first statement of the
1093 function - except for one-line functions, for which
1094 it is also the line number of all the statements and
1095 of the closing '}', and for which we do not have any
1096 other statement-line-number. */
1097 if (fcn_last_line == 1)
1098 record_line
1099 (get_current_subfile (), fcn_first_line,
1100 unrelocated_addr (gdbarch_addr_bits_remove (gdbarch,
1101 fcn_first_line_addr)));
1102 else
1103 enter_linenos (fcn_line_ptr, fcn_first_line,
1104 fcn_last_line, objfile);
1106 finish_block (cstk.name, cstk.old_blocks,
1107 NULL, cstk.start_addr,
1108 fcn_cs_saved.c_value
1109 + fcn_aux_saved.x_sym.x_misc.x_fsize
1110 + objfile->text_section_offset ());
1111 within_function = 0;
1113 break;
1115 case C_BLOCK:
1116 if (strcmp (cs->c_name, ".bb") == 0)
1118 tmpaddr = cs->c_value;
1119 tmpaddr += objfile->text_section_offset ();
1120 push_context (++depth, tmpaddr);
1122 else if (strcmp (cs->c_name, ".eb") == 0)
1124 if (outermost_context_p ())
1125 { /* We attempted to pop an empty context stack. */
1126 complaint (_("`.eb' symbol without matching `.bb' "
1127 "symbol ignored starting at symnum %d"),
1128 cs->c_symnum);
1129 break;
1132 struct context_stack cstk = pop_context ();
1133 if (depth-- != cstk.depth)
1135 complaint (_("Mismatched .eb symbol ignored "
1136 "starting at symnum %d"),
1137 symnum);
1138 break;
1140 if (*get_local_symbols () && !outermost_context_p ())
1142 tmpaddr = cs->c_value + objfile->text_section_offset ();
1143 /* Make a block for the local symbols within. */
1144 finish_block (0, cstk.old_blocks, NULL,
1145 cstk.start_addr, tmpaddr);
1147 /* Now pop locals of block just finished. */
1148 *get_local_symbols () = cstk.locals;
1150 break;
1152 default:
1153 process_coff_symbol (cs, &main_aux, objfile);
1154 break;
1158 if (get_last_source_file ())
1159 coff_end_compunit_symtab (objfile);
1161 /* Patch up any opaque types (references to types that are not defined
1162 in the file where they are referenced, e.g. "struct foo *bar"). */
1164 for (compunit_symtab *cu : objfile->compunits ())
1166 for (symtab *s : cu->filetabs ())
1167 patch_opaque_types (s);
1171 coffread_objfile = NULL;
1174 /* Routines for reading headers and symbols from executable. */
1176 /* Read the next symbol, swap it, and return it in both
1177 internal_syment form, and coff_symbol form. Also return its first
1178 auxent, if any, in internal_auxent form, and skip any other
1179 auxents. */
1181 static void
1182 read_one_sym (struct coff_symbol *cs,
1183 struct internal_syment *sym,
1184 union internal_auxent *aux)
1186 int i;
1187 bfd_size_type bytes;
1189 cs->c_symnum = symnum;
1190 bytes = bfd_read (temp_sym, local_symesz, nlist_bfd_global);
1191 if (bytes != local_symesz)
1192 error (_("%s: error reading symbols"), objfile_name (coffread_objfile));
1193 bfd_coff_swap_sym_in (symfile_bfd, temp_sym, (char *) sym);
1194 cs->c_naux = sym->n_numaux & 0xff;
1195 if (cs->c_naux >= 1)
1197 bytes = bfd_read (temp_aux, local_auxesz, nlist_bfd_global);
1198 if (bytes != local_auxesz)
1199 error (_("%s: error reading symbols"), objfile_name (coffread_objfile));
1200 bfd_coff_swap_aux_in (symfile_bfd, temp_aux,
1201 sym->n_type, sym->n_sclass,
1202 0, cs->c_naux, (char *) aux);
1203 /* If more than one aux entry, read past it (only the first aux
1204 is important). */
1205 for (i = 1; i < cs->c_naux; i++)
1207 bytes = bfd_read (temp_aux, local_auxesz, nlist_bfd_global);
1208 if (bytes != local_auxesz)
1209 error (_("%s: error reading symbols"),
1210 objfile_name (coffread_objfile));
1213 cs->c_name = getsymname (sym);
1214 cs->c_value = sym->n_value;
1215 cs->c_sclass = (sym->n_sclass & 0xff);
1216 cs->c_secnum = sym->n_scnum;
1217 cs->c_type = (unsigned) sym->n_type;
1218 if (!SDB_TYPE (cs->c_type))
1219 cs->c_type = 0;
1221 #if 0
1222 if (cs->c_sclass & 128)
1223 printf (_("thumb symbol %s, class 0x%x\n"), cs->c_name, cs->c_sclass);
1224 #endif
1226 symnum += 1 + cs->c_naux;
1228 /* The PE file format stores symbol values as offsets within the
1229 section, rather than as absolute addresses. We correct that
1230 here, if the symbol has an appropriate storage class. FIXME: We
1231 should use BFD to read the symbols, rather than duplicating the
1232 work here. */
1233 if (pe_file)
1235 switch (cs->c_sclass)
1237 case C_EXT:
1238 case C_THUMBEXT:
1239 case C_THUMBEXTFUNC:
1240 case C_SECTION:
1241 case C_NT_WEAK:
1242 case C_STAT:
1243 case C_THUMBSTAT:
1244 case C_THUMBSTATFUNC:
1245 case C_LABEL:
1246 case C_THUMBLABEL:
1247 case C_BLOCK:
1248 case C_FCN:
1249 case C_EFCN:
1250 if (cs->c_secnum != 0)
1251 cs->c_value += cs_section_address (cs, symfile_bfd);
1252 break;
1257 /* Support for string table handling. */
1259 static int
1260 init_stringtab (bfd *abfd, file_ptr offset, gdb::unique_xmalloc_ptr<char> *storage)
1262 long length;
1263 int val;
1264 unsigned char lengthbuf[4];
1266 /* If the file is stripped, the offset might be zero, indicating no
1267 string table. Just return with `stringtab' set to null. */
1268 if (offset == 0)
1269 return 0;
1271 if (bfd_seek (abfd, offset, 0) < 0)
1272 return -1;
1274 val = bfd_read (lengthbuf, sizeof lengthbuf, abfd);
1275 /* If no string table is needed, then the file may end immediately
1276 after the symbols. Just return with `stringtab' set to null. */
1277 if (val != sizeof lengthbuf)
1278 return 0;
1279 length = bfd_h_get_32 (symfile_bfd, lengthbuf);
1280 if (length < sizeof lengthbuf)
1281 return 0;
1283 storage->reset ((char *) xmalloc (length));
1284 stringtab = storage->get ();
1285 /* This is in target format (probably not very useful, and not
1286 currently used), not host format. */
1287 memcpy (stringtab, lengthbuf, sizeof lengthbuf);
1288 stringtab_length = length;
1289 if (length == sizeof length) /* Empty table -- just the count. */
1290 return 0;
1292 val = bfd_read (stringtab + sizeof lengthbuf,
1293 length - sizeof lengthbuf, abfd);
1294 if (val != length - sizeof lengthbuf || stringtab[length - 1] != '\0')
1295 return -1;
1297 return 0;
1300 static char *
1301 getsymname (struct internal_syment *symbol_entry)
1303 static char buffer[SYMNMLEN + 1];
1304 char *result;
1306 if (symbol_entry->_n._n_n._n_zeroes == 0)
1308 if (symbol_entry->_n._n_n._n_offset > stringtab_length)
1309 error (_("COFF Error: string table offset (%s) outside string table (length %ld)"),
1310 hex_string (symbol_entry->_n._n_n._n_offset), stringtab_length);
1311 result = stringtab + symbol_entry->_n._n_n._n_offset;
1313 else
1315 strncpy (buffer, symbol_entry->_n._n_name, SYMNMLEN);
1316 buffer[SYMNMLEN] = '\0';
1317 result = buffer;
1319 return result;
1322 /* Extract the file name from the aux entry of a C_FILE symbol.
1323 Return only the last component of the name. Result is in static
1324 storage and is only good for temporary use. */
1326 static const char *
1327 coff_getfilename (union internal_auxent *aux_entry)
1329 static char buffer[BUFSIZ];
1330 const char *result;
1332 if (aux_entry->x_file.x_n.x_n.x_zeroes == 0)
1334 if (strlen (stringtab + aux_entry->x_file.x_n.x_n.x_offset) >= BUFSIZ)
1335 internal_error (_("coff file name too long"));
1336 strcpy (buffer, stringtab + aux_entry->x_file.x_n.x_n.x_offset);
1338 else
1340 size_t x_fname_len = sizeof (aux_entry->x_file.x_n.x_fname);
1341 strncpy (buffer, aux_entry->x_file.x_n.x_fname, x_fname_len);
1342 buffer[x_fname_len] = '\0';
1344 result = buffer;
1346 /* FIXME: We should not be throwing away the information about what
1347 directory. It should go into dirname of the symtab, or some such
1348 place. */
1349 result = lbasename (result);
1350 return (result);
1353 /* Support for line number handling. */
1355 /* Read in all the line numbers for fast lookups later. Leave them in
1356 external (unswapped) format in memory; we'll swap them as we enter
1357 them into GDB's data structures. */
1359 static int
1360 init_lineno (bfd *abfd, file_ptr offset, file_ptr size,
1361 gdb::unique_xmalloc_ptr<char> *storage)
1363 int val;
1365 linetab_offset = offset;
1366 linetab_size = size;
1368 if (size == 0)
1369 return 0;
1371 if (bfd_seek (abfd, offset, 0) < 0)
1372 return -1;
1374 /* Allocate the desired table, plus a sentinel. */
1375 storage->reset ((char *) xmalloc (size + local_linesz));
1376 linetab = storage->get ();
1378 val = bfd_read (storage->get (), size, abfd);
1379 if (val != size)
1380 return -1;
1382 /* Terminate it with an all-zero sentinel record. */
1383 memset (linetab + size, 0, local_linesz);
1385 return 0;
1388 #if !defined (L_LNNO32)
1389 #define L_LNNO32(lp) ((lp)->l_lnno)
1390 #endif
1392 static void
1393 enter_linenos (file_ptr file_offset, int first_line,
1394 int last_line, struct objfile *objfile)
1396 struct gdbarch *gdbarch = objfile->arch ();
1397 char *rawptr;
1398 struct internal_lineno lptr;
1400 if (!linetab)
1401 return;
1402 if (file_offset < linetab_offset)
1404 complaint (_("Line number pointer %s lower than start of line numbers"),
1405 plongest (file_offset));
1406 if (file_offset > linetab_size) /* Too big to be an offset? */
1407 return;
1408 file_offset += linetab_offset; /* Try reading at that linetab
1409 offset. */
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. */
1417 first_line--;
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;
1427 we exit. */
1428 if (L_LNNO32 (&lptr) && L_LNNO32 (&lptr) <= last_line)
1430 CORE_ADDR addr = lptr.l_addr.l_paddr;
1431 record_line (get_current_subfile (),
1432 first_line + L_LNNO32 (&lptr),
1433 unrelocated_addr (gdbarch_addr_bits_remove (gdbarch,
1434 addr)));
1436 else
1437 break;
1441 static void
1442 patch_type (struct type *type, struct type *real_type)
1444 struct type *target = type->target_type ();
1445 struct type *real_target = real_type->target_type ();
1447 target->set_length (real_target->length ());
1448 target->copy_fields (real_target);
1450 if (real_target->name ())
1452 /* The previous copy of TYPE_NAME is allocated by
1453 process_coff_symbol. */
1454 xfree ((char *) target->name ());
1455 target->set_name (xstrdup (real_target->name ()));
1459 /* Patch up all appropriate typedef symbols in the opaque_type_chains
1460 so that they can be used to print out opaque data structures
1461 properly. */
1463 static void
1464 patch_opaque_types (struct symtab *s)
1466 /* Go through the per-file symbols only. */
1467 const struct block *b = s->compunit ()->blockvector ()->static_block ();
1468 for (struct symbol *real_sym : block_iterator_range (b))
1470 /* Find completed typedefs to use to fix opaque ones.
1471 Remove syms from the chain when their types are stored,
1472 but search the whole chain, as there may be several syms
1473 from different files with the same name. */
1474 if (real_sym->aclass () == LOC_TYPEDEF
1475 && real_sym->domain () == TYPE_DOMAIN
1476 && real_sym->type ()->code () == TYPE_CODE_PTR
1477 && real_sym->type ()->target_type ()->length () != 0)
1479 const char *name = real_sym->linkage_name ();
1480 int hash = hashname (name);
1481 struct symbol *sym, *prev;
1483 prev = 0;
1484 for (sym = opaque_type_chain[hash]; sym;)
1486 if (name[0] == sym->linkage_name ()[0]
1487 && strcmp (name + 1, sym->linkage_name () + 1) == 0)
1489 if (prev)
1490 prev->set_value_chain (sym->value_chain ());
1491 else
1492 opaque_type_chain[hash] = sym->value_chain ();
1494 patch_type (sym->type (), real_sym->type ());
1496 if (prev)
1497 sym = prev->value_chain ();
1498 else
1499 sym = opaque_type_chain[hash];
1501 else
1503 prev = sym;
1504 sym->set_value_chain (sym);
1511 static int
1512 coff_reg_to_regnum (struct symbol *sym, struct gdbarch *gdbarch)
1514 return gdbarch_sdb_reg_to_regnum (gdbarch, sym->value_longest ());
1517 static const struct symbol_register_ops coff_register_funcs = {
1518 coff_reg_to_regnum
1521 /* The "aclass" index for computed COFF symbols. */
1523 static int coff_register_index;
1525 static struct symbol *
1526 process_coff_symbol (struct coff_symbol *cs,
1527 union internal_auxent *aux,
1528 struct objfile *objfile)
1530 struct symbol *sym = new (&objfile->objfile_obstack) symbol;
1531 char *name;
1533 name = cs->c_name;
1534 name = EXTERNAL_NAME (name, objfile->obfd.get ());
1535 sym->set_language (get_current_subfile ()->language,
1536 &objfile->objfile_obstack);
1537 sym->compute_and_set_names (name, true, objfile->per_bfd);
1539 /* default assumptions */
1540 sym->set_value_longest (cs->c_value);
1541 sym->set_domain (VAR_DOMAIN);
1542 sym->set_section_index (cs_to_section (cs, objfile));
1544 if (ISFCN (cs->c_type))
1546 sym->set_domain (FUNCTION_DOMAIN);
1547 sym->set_value_longest
1548 (sym->value_longest () + objfile->text_section_offset ());
1549 sym->set_type
1550 (lookup_function_type (decode_function_type (cs, cs->c_type,
1551 aux, objfile)));
1553 sym->set_aclass_index (LOC_BLOCK);
1554 if (cs->c_sclass == C_STAT || cs->c_sclass == C_THUMBSTAT
1555 || cs->c_sclass == C_THUMBSTATFUNC)
1556 add_symbol_to_list (sym, get_file_symbols ());
1557 else if (cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXT
1558 || cs->c_sclass == C_THUMBEXTFUNC)
1559 add_symbol_to_list (sym, get_global_symbols ());
1561 else
1563 sym->set_type (decode_type (cs, cs->c_type, aux, objfile));
1564 switch (cs->c_sclass)
1566 case C_NULL:
1567 break;
1569 case C_AUTO:
1570 sym->set_aclass_index (LOC_LOCAL);
1571 add_symbol_to_list (sym, get_local_symbols ());
1572 break;
1574 case C_THUMBEXT:
1575 case C_THUMBEXTFUNC:
1576 case C_EXT:
1577 sym->set_aclass_index (LOC_STATIC);
1578 sym->set_value_address ((CORE_ADDR) cs->c_value
1579 + objfile->section_offsets[SECT_OFF_TEXT (objfile)]);
1580 add_symbol_to_list (sym, get_global_symbols ());
1581 break;
1583 case C_THUMBSTAT:
1584 case C_THUMBSTATFUNC:
1585 case C_STAT:
1586 sym->set_aclass_index (LOC_STATIC);
1587 sym->set_value_address ((CORE_ADDR) cs->c_value
1588 + objfile->section_offsets[SECT_OFF_TEXT (objfile)]);
1589 if (within_function)
1591 /* Static symbol of local scope. */
1592 add_symbol_to_list (sym, get_local_symbols ());
1594 else
1596 /* Static symbol at top level of file. */
1597 add_symbol_to_list (sym, get_file_symbols ());
1599 break;
1601 #ifdef C_GLBLREG /* AMD coff */
1602 case C_GLBLREG:
1603 #endif
1604 case C_REG:
1605 sym->set_aclass_index (coff_register_index);
1606 sym->set_value_longest (cs->c_value);
1607 add_symbol_to_list (sym, get_local_symbols ());
1608 break;
1610 case C_THUMBLABEL:
1611 case C_LABEL:
1612 break;
1614 case C_ARG:
1615 sym->set_aclass_index (LOC_ARG);
1616 sym->set_is_argument (1);
1617 add_symbol_to_list (sym, get_local_symbols ());
1618 break;
1620 case C_REGPARM:
1621 sym->set_aclass_index (coff_register_index);
1622 sym->set_is_argument (1);
1623 sym->set_value_longest (cs->c_value);
1624 add_symbol_to_list (sym, get_local_symbols ());
1625 break;
1627 case C_TPDEF:
1628 sym->set_aclass_index (LOC_TYPEDEF);
1629 sym->set_domain (TYPE_DOMAIN);
1631 /* If type has no name, give it one. */
1632 if (sym->type ()->name () == 0)
1634 if (sym->type ()->code () == TYPE_CODE_PTR
1635 || sym->type ()->code () == TYPE_CODE_FUNC)
1637 /* If we are giving a name to a type such as
1638 "pointer to foo" or "function returning foo", we
1639 better not set the TYPE_NAME. If the program
1640 contains "typedef char *caddr_t;", we don't want
1641 all variables of type char * to print as caddr_t.
1642 This is not just a consequence of GDB's type
1643 management; CC and GCC (at least through version
1644 2.4) both output variables of either type char *
1645 or caddr_t with the type refering to the C_TPDEF
1646 symbol for caddr_t. If a future compiler cleans
1647 this up it GDB is not ready for it yet, but if it
1648 becomes ready we somehow need to disable this
1649 check (without breaking the PCC/GCC2.4 case).
1651 Sigh.
1653 Fortunately, this check seems not to be necessary
1654 for anything except pointers or functions. */
1657 else
1658 sym->type ()->set_name (xstrdup (sym->linkage_name ()));
1661 /* Keep track of any type which points to empty structured
1662 type, so it can be filled from a definition from another
1663 file. A simple forward reference (TYPE_CODE_UNDEF) is
1664 not an empty structured type, though; the forward
1665 references work themselves out via the magic of
1666 coff_lookup_type. */
1667 if (sym->type ()->code () == TYPE_CODE_PTR
1668 && sym->type ()->target_type ()->length () == 0
1669 && sym->type ()->target_type ()->code ()
1670 != TYPE_CODE_UNDEF)
1672 int i = hashname (sym->linkage_name ());
1674 sym->set_value_chain (opaque_type_chain[i]);
1675 opaque_type_chain[i] = sym;
1677 add_symbol_to_list (sym, get_file_symbols ());
1678 break;
1680 case C_STRTAG:
1681 case C_UNTAG:
1682 case C_ENTAG:
1683 sym->set_aclass_index (LOC_TYPEDEF);
1684 sym->set_domain (STRUCT_DOMAIN);
1686 /* Some compilers try to be helpful by inventing "fake"
1687 names for anonymous enums, structures, and unions, like
1688 "~0fake" or ".0fake". Thanks, but no thanks... */
1689 if (sym->type ()->name () == 0)
1690 if (sym->linkage_name () != NULL
1691 && *sym->linkage_name () != '~'
1692 && *sym->linkage_name () != '.')
1693 sym->type ()->set_name (xstrdup (sym->linkage_name ()));
1695 add_symbol_to_list (sym, get_file_symbols ());
1696 break;
1698 default:
1699 break;
1702 return sym;
1705 /* Decode a coff type specifier; return the type that is meant. */
1707 static struct type *
1708 decode_type (struct coff_symbol *cs, unsigned int c_type,
1709 union internal_auxent *aux, struct objfile *objfile)
1711 struct type *type = 0;
1712 unsigned int new_c_type;
1714 if (c_type & ~N_BTMASK)
1716 new_c_type = DECREF (c_type);
1717 if (ISPTR (c_type))
1719 type = decode_type (cs, new_c_type, aux, objfile);
1720 type = lookup_pointer_type (type);
1722 else if (ISFCN (c_type))
1724 type = decode_type (cs, new_c_type, aux, objfile);
1725 type = lookup_function_type (type);
1727 else if (ISARY (c_type))
1729 int i, n;
1730 unsigned short *dim;
1731 struct type *base_type, *index_type, *range_type;
1733 /* Define an array type. */
1734 /* auxent refers to array, not base type. */
1735 if (aux->x_sym.x_tagndx.u32 == 0)
1736 cs->c_naux = 0;
1738 /* Shift the indices down. */
1739 dim = &aux->x_sym.x_fcnary.x_ary.x_dimen[0];
1740 i = 1;
1741 n = dim[0];
1742 for (i = 0; *dim && i < DIMNUM - 1; i++, dim++)
1743 *dim = *(dim + 1);
1744 *dim = 0;
1746 base_type = decode_type (cs, new_c_type, aux, objfile);
1747 index_type = builtin_type (objfile)->builtin_int;
1748 type_allocator alloc (objfile, language_c);
1749 range_type
1750 = create_static_range_type (alloc, index_type, 0, n - 1);
1751 type = create_array_type (alloc, base_type, range_type);
1753 return type;
1756 /* Reference to existing type. This only occurs with the struct,
1757 union, and enum types. EPI a29k coff fakes us out by producing
1758 aux entries with a nonzero x_tagndx for definitions of structs,
1759 unions, and enums, so we have to check the c_sclass field. SCO
1760 3.2v4 cc gets confused with pointers to pointers to defined
1761 structs, and generates negative x_tagndx fields. */
1762 if (cs->c_naux > 0 && aux->x_sym.x_tagndx.u32 != 0)
1764 if (cs->c_sclass != C_STRTAG
1765 && cs->c_sclass != C_UNTAG
1766 && cs->c_sclass != C_ENTAG
1767 && (int32_t) aux->x_sym.x_tagndx.u32 >= 0)
1769 type = coff_alloc_type (aux->x_sym.x_tagndx.u32);
1770 return type;
1772 else
1774 complaint (_("Symbol table entry for %s has bad tagndx value"),
1775 cs->c_name);
1776 /* And fall through to decode_base_type... */
1780 return decode_base_type (cs, BTYPE (c_type), aux, objfile);
1783 /* Decode a coff type specifier for function definition;
1784 return the type that the function returns. */
1786 static struct type *
1787 decode_function_type (struct coff_symbol *cs,
1788 unsigned int c_type,
1789 union internal_auxent *aux,
1790 struct objfile *objfile)
1792 if (aux->x_sym.x_tagndx.u32 == 0)
1793 cs->c_naux = 0; /* auxent refers to function, not base
1794 type. */
1796 return decode_type (cs, DECREF (c_type), aux, objfile);
1799 /* Basic C types. */
1801 static struct type *
1802 decode_base_type (struct coff_symbol *cs,
1803 unsigned int c_type,
1804 union internal_auxent *aux,
1805 struct objfile *objfile)
1807 struct gdbarch *gdbarch = objfile->arch ();
1808 struct type *type;
1810 switch (c_type)
1812 case T_NULL:
1813 /* Shows up with "void (*foo)();" structure members. */
1814 return builtin_type (objfile)->builtin_void;
1816 #ifdef T_VOID
1817 case T_VOID:
1818 /* Intel 960 COFF has this symbol and meaning. */
1819 return builtin_type (objfile)->builtin_void;
1820 #endif
1822 case T_CHAR:
1823 return builtin_type (objfile)->builtin_char;
1825 case T_SHORT:
1826 return builtin_type (objfile)->builtin_short;
1828 case T_INT:
1829 return builtin_type (objfile)->builtin_int;
1831 case T_LONG:
1832 if (cs->c_sclass == C_FIELD
1833 && aux->x_sym.x_misc.x_lnsz.x_size
1834 > gdbarch_long_bit (gdbarch))
1835 return builtin_type (objfile)->builtin_long_long;
1836 else
1837 return builtin_type (objfile)->builtin_long;
1839 case T_FLOAT:
1840 return builtin_type (objfile)->builtin_float;
1842 case T_DOUBLE:
1843 return builtin_type (objfile)->builtin_double;
1845 case T_LNGDBL:
1846 return builtin_type (objfile)->builtin_long_double;
1848 case T_STRUCT:
1849 if (cs->c_naux != 1)
1851 /* Anonymous structure type. */
1852 type = coff_alloc_type (cs->c_symnum);
1853 type->set_code (TYPE_CODE_STRUCT);
1854 type->set_name (NULL);
1855 INIT_CPLUS_SPECIFIC (type);
1856 type->set_length (0);
1857 type->set_fields (nullptr);
1858 type->set_num_fields (0);
1860 else
1862 type = coff_read_struct_type (cs->c_symnum,
1863 aux->x_sym.x_misc.x_lnsz.x_size,
1864 aux->x_sym.x_fcnary.x_fcn.x_endndx.u32,
1865 objfile);
1867 return type;
1869 case T_UNION:
1870 if (cs->c_naux != 1)
1872 /* Anonymous union type. */
1873 type = coff_alloc_type (cs->c_symnum);
1874 type->set_name (NULL);
1875 INIT_CPLUS_SPECIFIC (type);
1876 type->set_length (0);
1877 type->set_fields (nullptr);
1878 type->set_num_fields (0);
1880 else
1882 type = coff_read_struct_type (cs->c_symnum,
1883 aux->x_sym.x_misc.x_lnsz.x_size,
1884 aux->x_sym.x_fcnary.x_fcn.x_endndx.u32,
1885 objfile);
1887 type->set_code (TYPE_CODE_UNION);
1888 return type;
1890 case T_ENUM:
1891 if (cs->c_naux != 1)
1893 /* Anonymous enum type. */
1894 type = coff_alloc_type (cs->c_symnum);
1895 type->set_code (TYPE_CODE_ENUM);
1896 type->set_name (NULL);
1897 type->set_length (0);
1898 type->set_fields (nullptr);
1899 type->set_num_fields (0);
1901 else
1903 type = coff_read_enum_type (cs->c_symnum,
1904 aux->x_sym.x_misc.x_lnsz.x_size,
1905 aux->x_sym.x_fcnary.x_fcn.x_endndx.u32,
1906 objfile);
1908 return type;
1910 case T_MOE:
1911 /* Shouldn't show up here. */
1912 break;
1914 case T_UCHAR:
1915 return builtin_type (objfile)->builtin_unsigned_char;
1917 case T_USHORT:
1918 return builtin_type (objfile)->builtin_unsigned_short;
1920 case T_UINT:
1921 return builtin_type (objfile)->builtin_unsigned_int;
1923 case T_ULONG:
1924 if (cs->c_sclass == C_FIELD
1925 && aux->x_sym.x_misc.x_lnsz.x_size
1926 > gdbarch_long_bit (gdbarch))
1927 return builtin_type (objfile)->builtin_unsigned_long_long;
1928 else
1929 return builtin_type (objfile)->builtin_unsigned_long;
1931 complaint (_("Unexpected type for symbol %s"), cs->c_name);
1932 return builtin_type (objfile)->builtin_void;
1935 /* This page contains subroutines of read_type. */
1937 /* Read the description of a structure (or union type) and return an
1938 object describing the type. */
1940 static struct type *
1941 coff_read_struct_type (int index, int length, int lastsym,
1942 struct objfile *objfile)
1944 struct nextfield
1946 struct nextfield *next;
1947 struct field field;
1950 struct type *type;
1951 struct nextfield *list = 0;
1952 struct nextfield *newobj;
1953 int nfields = 0;
1954 int n;
1955 char *name;
1956 struct coff_symbol member_sym;
1957 struct coff_symbol *ms = &member_sym;
1958 struct internal_syment sub_sym;
1959 union internal_auxent sub_aux;
1960 int done = 0;
1962 type = coff_alloc_type (index);
1963 type->set_code (TYPE_CODE_STRUCT);
1964 INIT_CPLUS_SPECIFIC (type);
1965 type->set_length (length);
1967 while (!done && symnum < lastsym && symnum < nlist_nsyms_global)
1969 read_one_sym (ms, &sub_sym, &sub_aux);
1970 name = ms->c_name;
1971 name = EXTERNAL_NAME (name, objfile->obfd.get ());
1973 switch (ms->c_sclass)
1975 case C_MOS:
1976 case C_MOU:
1978 /* Get space to record the next field's data. */
1979 newobj = XALLOCA (struct nextfield);
1980 newobj->next = list;
1981 list = newobj;
1983 /* Save the data. */
1984 list->field.set_name (obstack_strdup (&objfile->objfile_obstack,
1985 name));
1986 list->field.set_type (decode_type (ms, ms->c_type, &sub_aux,
1987 objfile));
1988 list->field.set_loc_bitpos (8 * ms->c_value);
1989 list->field.set_bitsize (0);
1990 nfields++;
1991 break;
1993 case C_FIELD:
1995 /* Get space to record the next field's data. */
1996 newobj = XALLOCA (struct nextfield);
1997 newobj->next = list;
1998 list = newobj;
2000 /* Save the data. */
2001 list->field.set_name (obstack_strdup (&objfile->objfile_obstack,
2002 name));
2003 list->field.set_type (decode_type (ms, ms->c_type, &sub_aux,
2004 objfile));
2005 list->field.set_loc_bitpos (ms->c_value);
2006 list->field.set_bitsize (sub_aux.x_sym.x_misc.x_lnsz.x_size);
2007 nfields++;
2008 break;
2010 case C_EOS:
2011 done = 1;
2012 break;
2015 /* Now create the vector of fields, and record how big it is. */
2017 type->alloc_fields (nfields);
2019 /* Copy the saved-up fields into the field vector. */
2021 for (n = nfields; list; list = list->next)
2022 type->field (--n) = list->field;
2024 return type;
2027 /* Read a definition of an enumeration type,
2028 and create and return a suitable type object.
2029 Also defines the symbols that represent the values of the type. */
2031 static struct type *
2032 coff_read_enum_type (int index, int length, int lastsym,
2033 struct objfile *objfile)
2035 struct gdbarch *gdbarch = objfile->arch ();
2036 struct symbol *sym;
2037 struct type *type;
2038 int nsyms = 0;
2039 int done = 0;
2040 struct pending **symlist;
2041 struct coff_symbol member_sym;
2042 struct coff_symbol *ms = &member_sym;
2043 struct internal_syment sub_sym;
2044 union internal_auxent sub_aux;
2045 struct pending *osyms, *syms;
2046 int o_nsyms;
2047 int n;
2048 char *name;
2049 int unsigned_enum = 1;
2051 type = coff_alloc_type (index);
2052 if (within_function)
2053 symlist = get_local_symbols ();
2054 else
2055 symlist = get_file_symbols ();
2056 osyms = *symlist;
2057 o_nsyms = osyms ? osyms->nsyms : 0;
2059 while (!done && symnum < lastsym && symnum < nlist_nsyms_global)
2061 read_one_sym (ms, &sub_sym, &sub_aux);
2062 name = ms->c_name;
2063 name = EXTERNAL_NAME (name, objfile->obfd.get ());
2065 switch (ms->c_sclass)
2067 case C_MOE:
2068 sym = new (&objfile->objfile_obstack) symbol;
2070 name = obstack_strdup (&objfile->objfile_obstack, name);
2071 sym->set_linkage_name (name);
2072 sym->set_aclass_index (LOC_CONST);
2073 sym->set_domain (VAR_DOMAIN);
2074 sym->set_value_longest (ms->c_value);
2075 add_symbol_to_list (sym, symlist);
2076 nsyms++;
2077 break;
2079 case C_EOS:
2080 /* Sometimes the linker (on 386/ix 2.0.2 at least) screws
2081 up the count of how many symbols to read. So stop
2082 on .eos. */
2083 done = 1;
2084 break;
2088 /* Now fill in the fields of the type-structure. */
2090 if (length > 0)
2091 type->set_length (length);
2092 else /* Assume ints. */
2093 type->set_length (gdbarch_int_bit (gdbarch) / TARGET_CHAR_BIT);
2094 type->set_code (TYPE_CODE_ENUM);
2095 type->alloc_fields (nsyms);
2097 /* Find the symbols for the values and put them into the type.
2098 The symbols can be found in the symlist that we put them on
2099 to cause them to be defined. osyms contains the old value
2100 of that symlist; everything up to there was defined by us. */
2101 /* Note that we preserve the order of the enum constants, so
2102 that in something like "enum {FOO, LAST_THING=FOO}" we print
2103 FOO, not LAST_THING. */
2105 for (syms = *symlist, n = 0; syms; syms = syms->next)
2107 int j = 0;
2109 if (syms == osyms)
2110 j = o_nsyms;
2111 for (; j < syms->nsyms; j++, n++)
2113 struct symbol *xsym = syms->symbol[j];
2115 xsym->set_type (type);
2116 type->field (n).set_name (xsym->linkage_name ());
2117 type->field (n).set_loc_enumval (xsym->value_longest ());
2118 if (xsym->value_longest () < 0)
2119 unsigned_enum = 0;
2120 type->field (n).set_bitsize (0);
2122 if (syms == osyms)
2123 break;
2126 if (unsigned_enum)
2127 type->set_is_unsigned (true);
2129 return type;
2132 /* Register our ability to parse symbols for coff BFD files. */
2134 static const struct sym_fns coff_sym_fns =
2136 coff_new_init, /* sym_new_init: init anything gbl to
2137 entire symtab */
2138 coff_symfile_init, /* sym_init: read initial info, setup
2139 for sym_read() */
2140 coff_symfile_read, /* sym_read: read a symbol file into
2141 symtab */
2142 coff_symfile_finish, /* sym_finish: finished with file,
2143 cleanup */
2144 default_symfile_offsets, /* sym_offsets: xlate external to
2145 internal form */
2146 default_symfile_segments, /* sym_segments: Get segment
2147 information from a file */
2148 NULL, /* sym_read_linetable */
2150 default_symfile_relocate, /* sym_relocate: Relocate a debug
2151 section. */
2152 NULL, /* sym_probe_fns */
2155 void _initialize_coffread ();
2156 void
2157 _initialize_coffread ()
2159 add_symtab_fns (bfd_target_coff_flavour, &coff_sym_fns);
2161 coff_register_index
2162 = register_symbol_register_impl (LOC_REGISTER, &coff_register_funcs);