Add generated source files and fix thinko in aarch64-asm.c
[binutils-gdb.git] / gdb / coffread.c
blob30a38035c52b0a967f0a0c628632207a3bb455dd
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 "defs.h"
21 #include "symtab.h"
22 #include "gdbtypes.h"
23 #include "demangle.h"
24 #include "breakpoint.h"
26 #include "bfd.h"
27 #include "gdbsupport/gdb_obstack.h"
28 #include <ctype.h>
30 #include "coff/internal.h"
31 #include "libcoff.h"
32 #include "objfiles.h"
33 #include "buildsym-legacy.h"
34 #include "stabsread.h"
35 #include "complaints.h"
36 #include "target.h"
37 #include "block.h"
38 #include "dictionary.h"
39 #include "dwarf2/public.h"
41 #include "coff-pe-read.h"
43 /* The objfile we are currently reading. */
45 static struct objfile *coffread_objfile;
47 struct coff_symfile_info
49 file_ptr min_lineno_offset = 0; /* Where in file lowest line#s are. */
50 file_ptr max_lineno_offset = 0; /* 1+last byte of line#s in file. */
52 CORE_ADDR textaddr = 0; /* Addr of .text section. */
53 unsigned int textsize = 0; /* Size of .text section. */
54 std::vector<asection *> *stabsects; /* .stab sections. */
55 asection *stabstrsect = nullptr; /* Section pointer for .stab section. */
56 char *stabstrdata = nullptr;
59 /* Key for COFF-associated data. */
61 static const registry<objfile>::key<coff_symfile_info> coff_objfile_data_key;
63 /* Translate an external name string into a user-visible name. */
64 #define EXTERNAL_NAME(string, abfd) \
65 (*string != '\0' && *string == bfd_get_symbol_leading_char (abfd) \
66 ? string + 1 : string)
68 /* To be an sdb debug type, type must have at least a basic or primary
69 derived type. Using this rather than checking against T_NULL is
70 said to prevent core dumps if we try to operate on Michael Bloom
71 dbx-in-coff file. */
73 #define SDB_TYPE(type) (BTYPE(type) | (type & N_TMASK))
75 /* Core address of start and end of text of current source file.
76 This comes from a ".text" symbol where x_nlinno > 0. */
78 static CORE_ADDR current_source_start_addr;
79 static CORE_ADDR current_source_end_addr;
81 /* The addresses of the symbol table stream and number of symbols
82 of the object file we are reading (as copied into core). */
84 static bfd *nlist_bfd_global;
85 static int nlist_nsyms_global;
88 /* Pointers to scratch storage, used for reading raw symbols and
89 auxents. */
91 static char *temp_sym;
92 static char *temp_aux;
94 /* Local variables that hold the shift and mask values for the
95 COFF file that we are currently reading. These come back to us
96 from BFD, and are referenced by their macro names, as well as
97 internally to the BTYPE, ISPTR, ISFCN, ISARY, ISTAG, and DECREF
98 macros from include/coff/internal.h . */
100 static unsigned local_n_btmask;
101 static unsigned local_n_btshft;
102 static unsigned local_n_tmask;
103 static unsigned local_n_tshift;
105 #define N_BTMASK local_n_btmask
106 #define N_BTSHFT local_n_btshft
107 #define N_TMASK local_n_tmask
108 #define N_TSHIFT local_n_tshift
110 /* Local variables that hold the sizes in the file of various COFF
111 structures. (We only need to know this to read them from the file
112 -- BFD will then translate the data in them, into `internal_xxx'
113 structs in the right byte order, alignment, etc.) */
115 static unsigned local_linesz;
116 static unsigned local_symesz;
117 static unsigned local_auxesz;
119 /* This is set if this is a PE format file. */
121 static int pe_file;
123 /* Chain of typedefs of pointers to empty struct/union types.
124 They are chained thru the SYMBOL_VALUE_CHAIN. */
126 static struct symbol *opaque_type_chain[HASHSIZE];
128 /* Simplified internal version of coff symbol table information. */
130 struct coff_symbol
132 char *c_name;
133 int c_symnum; /* Symbol number of this entry. */
134 int c_naux; /* 0 if syment only, 1 if syment +
135 auxent, etc. */
136 CORE_ADDR c_value;
137 int c_sclass;
138 int c_secnum;
139 unsigned int c_type;
142 /* Vector of types defined so far, indexed by their type numbers. */
144 static struct type **type_vector;
146 /* Number of elements allocated for type_vector currently. */
148 static int type_vector_length;
150 /* Initial size of type vector. Is realloc'd larger if needed, and
151 realloc'd down to the size actually used, when completed. */
153 #define INITIAL_TYPE_VECTOR_LENGTH 160
155 static char *linetab = NULL;
156 static file_ptr linetab_offset;
157 static file_ptr linetab_size;
159 static char *stringtab = NULL;
160 static long stringtab_length = 0;
162 extern void stabsread_clear_cache (void);
164 static struct type *coff_read_struct_type (int, int, int,
165 struct objfile *);
167 static struct type *decode_base_type (struct coff_symbol *,
168 unsigned int,
169 union internal_auxent *,
170 struct objfile *);
172 static struct type *decode_type (struct coff_symbol *, unsigned int,
173 union internal_auxent *,
174 struct objfile *);
176 static struct type *decode_function_type (struct coff_symbol *,
177 unsigned int,
178 union internal_auxent *,
179 struct objfile *);
181 static struct type *coff_read_enum_type (int, int, int,
182 struct objfile *);
184 static struct symbol *process_coff_symbol (struct coff_symbol *,
185 union internal_auxent *,
186 struct objfile *);
188 static void patch_opaque_types (struct symtab *);
190 static void enter_linenos (file_ptr, int, int, struct objfile *);
192 static int init_lineno (bfd *, file_ptr, file_ptr, gdb::unique_xmalloc_ptr<char> *);
194 static char *getsymname (struct internal_syment *);
196 static const char *coff_getfilename (union internal_auxent *);
198 static int init_stringtab (bfd *, file_ptr, gdb::unique_xmalloc_ptr<char> *);
200 static void read_one_sym (struct coff_symbol *,
201 struct internal_syment *,
202 union internal_auxent *);
204 static void coff_symtab_read (minimal_symbol_reader &,
205 file_ptr, unsigned int, struct objfile *);
207 /* We are called once per section from coff_symfile_read. We
208 need to examine each section we are passed, check to see
209 if it is something we are interested in processing, and
210 if so, stash away some access information for the section.
212 FIXME: The section names should not be hardwired strings (what
213 should they be? I don't think most object file formats have enough
214 section flags to specify what kind of debug section it is
215 -kingdon). */
217 static void
218 coff_locate_sections (bfd *abfd, asection *sectp, void *csip)
220 struct coff_symfile_info *csi;
221 const char *name;
223 csi = (struct coff_symfile_info *) csip;
224 name = bfd_section_name (sectp);
225 if (strcmp (name, ".text") == 0)
227 csi->textaddr = bfd_section_vma (sectp);
228 csi->textsize += bfd_section_size (sectp);
230 else if (startswith (name, ".text"))
232 csi->textsize += bfd_section_size (sectp);
234 else if (strcmp (name, ".stabstr") == 0)
236 csi->stabstrsect = sectp;
238 else if (startswith (name, ".stab"))
240 const char *s;
242 /* We can have multiple .stab sections if linked with
243 --split-by-reloc. */
244 for (s = name + sizeof ".stab" - 1; *s != '\0'; s++)
245 if (!isdigit (*s))
246 break;
247 if (*s == '\0')
248 csi->stabsects->push_back (sectp);
252 /* Return the section_offsets* that CS points to. */
253 static int cs_to_section (struct coff_symbol *, struct objfile *);
255 struct coff_find_targ_sec_arg
257 int targ_index;
258 asection **resultp;
261 static void
262 find_targ_sec (bfd *abfd, asection *sect, void *obj)
264 struct coff_find_targ_sec_arg *args = (struct coff_find_targ_sec_arg *) obj;
266 if (sect->target_index == args->targ_index)
267 *args->resultp = sect;
270 /* Return the bfd_section that CS points to. */
271 static struct bfd_section*
272 cs_to_bfd_section (struct coff_symbol *cs, struct objfile *objfile)
274 asection *sect = NULL;
275 struct coff_find_targ_sec_arg args;
277 args.targ_index = cs->c_secnum;
278 args.resultp = &sect;
279 bfd_map_over_sections (objfile->obfd.get (), find_targ_sec, &args);
280 return sect;
283 /* Return the section number (SECT_OFF_*) that CS points to. */
284 static int
285 cs_to_section (struct coff_symbol *cs, struct objfile *objfile)
287 asection *sect = cs_to_bfd_section (cs, objfile);
289 if (sect == NULL)
290 return SECT_OFF_TEXT (objfile);
291 return gdb_bfd_section_index (objfile->obfd.get (), sect);
294 /* Return the address of the section of a COFF symbol. */
296 static CORE_ADDR cs_section_address (struct coff_symbol *, bfd *);
298 static CORE_ADDR
299 cs_section_address (struct coff_symbol *cs, bfd *abfd)
301 asection *sect = NULL;
302 struct coff_find_targ_sec_arg args;
303 CORE_ADDR addr = 0;
305 args.targ_index = cs->c_secnum;
306 args.resultp = &sect;
307 bfd_map_over_sections (abfd, find_targ_sec, &args);
308 if (sect != NULL)
309 addr = bfd_section_vma (sect);
310 return addr;
313 /* Look up a coff type-number index. Return the address of the slot
314 where the type for that index is stored.
315 The type-number is in INDEX.
317 This can be used for finding the type associated with that index
318 or for associating a new type with the index. */
320 static struct type **
321 coff_lookup_type (int index)
323 if (index >= type_vector_length)
325 int old_vector_length = type_vector_length;
327 type_vector_length *= 2;
328 if (index /* is still */ >= type_vector_length)
329 type_vector_length = index * 2;
331 type_vector = (struct type **)
332 xrealloc ((char *) type_vector,
333 type_vector_length * sizeof (struct type *));
334 memset (&type_vector[old_vector_length], 0,
335 (type_vector_length - old_vector_length) * sizeof (struct type *));
337 return &type_vector[index];
340 /* Make sure there is a type allocated for type number index
341 and return the type object.
342 This can create an empty (zeroed) type object. */
344 static struct type *
345 coff_alloc_type (int index)
347 struct type **type_addr = coff_lookup_type (index);
348 struct type *type = *type_addr;
350 /* If we are referring to a type not known at all yet,
351 allocate an empty type for it.
352 We will fill it in later if we find out how. */
353 if (type == NULL)
355 type = type_allocator (coffread_objfile, language_c).new_type ();
356 *type_addr = type;
358 return type;
361 /* Start a new symtab for a new source file.
362 This is called when a COFF ".file" symbol is seen;
363 it indicates the start of data for one original source file. */
365 static void
366 coff_start_compunit_symtab (struct objfile *objfile, const char *name)
368 within_function = 0;
369 start_compunit_symtab (objfile,
370 name,
371 /* We never know the directory name for COFF. */
372 NULL,
373 /* The start address is irrelevant, since we call
374 set_last_source_start_addr in coff_end_compunit_symtab. */
376 /* Let buildsym.c deduce the language for this symtab. */
377 language_unknown);
378 record_debugformat ("COFF");
381 /* Save the vital information from when starting to read a file,
382 for use when closing off the current file.
383 NAME is the file name the symbols came from, START_ADDR is the
384 first text address for the file, and SIZE is the number of bytes of
385 text. */
387 static void
388 complete_symtab (const char *name, CORE_ADDR start_addr, unsigned int size)
390 set_last_source_file (name);
391 current_source_start_addr = start_addr;
392 current_source_end_addr = start_addr + size;
395 /* Finish the symbol definitions for one main source file, close off
396 all the lexical contexts for that file (creating struct block's for
397 them), then make the struct symtab for that file and put it in the
398 list of all such. */
400 static void
401 coff_end_compunit_symtab (struct objfile *objfile)
403 set_last_source_start_addr (current_source_start_addr);
405 end_compunit_symtab (current_source_end_addr);
407 /* Reinitialize for beginning of new file. */
408 set_last_source_file (NULL);
411 /* The linker sometimes generates some non-function symbols inside
412 functions referencing variables imported from another DLL.
413 Return nonzero if the given symbol corresponds to one of them. */
415 static int
416 is_import_fixup_symbol (struct coff_symbol *cs,
417 enum minimal_symbol_type type)
419 /* The following is a bit of a heuristic using the characteristics
420 of these fixup symbols, but should work well in practice... */
421 int i;
423 /* Must be a non-static text symbol. */
424 if (type != mst_text)
425 return 0;
427 /* Must be a non-function symbol. */
428 if (ISFCN (cs->c_type))
429 return 0;
431 /* The name must start with "__fu<digits>__". */
432 if (!startswith (cs->c_name, "__fu"))
433 return 0;
434 if (! isdigit (cs->c_name[4]))
435 return 0;
436 for (i = 5; cs->c_name[i] != '\0' && isdigit (cs->c_name[i]); i++)
437 /* Nothing, just incrementing index past all digits. */;
438 if (cs->c_name[i] != '_' || cs->c_name[i + 1] != '_')
439 return 0;
441 return 1;
444 static struct minimal_symbol *
445 record_minimal_symbol (minimal_symbol_reader &reader,
446 struct coff_symbol *cs, unrelocated_addr address,
447 enum minimal_symbol_type type, int section,
448 struct objfile *objfile)
450 /* We don't want TDESC entry points in the minimal symbol table. */
451 if (cs->c_name[0] == '@')
452 return NULL;
454 if (is_import_fixup_symbol (cs, type))
456 /* Because the value of these symbols is within a function code
457 range, these symbols interfere with the symbol-from-address
458 reverse lookup; this manifests itself in backtraces, or any
459 other commands that prints symbolic addresses. Just pretend
460 these symbols do not exist. */
461 return NULL;
464 return reader.record_full (cs->c_name, true, address, type, section);
467 /* coff_symfile_init ()
468 is the coff-specific initialization routine for reading symbols.
469 It is passed a struct objfile which contains, among other things,
470 the BFD for the file whose symbols are being read, and a slot for
471 a pointer to "private data" which we fill with cookies and other
472 treats for coff_symfile_read ().
474 We will only be called if this is a COFF or COFF-like file. BFD
475 handles figuring out the format of the file, and code in symtab.c
476 uses BFD's determination to vector to us.
478 The ultimate result is a new symtab (or, FIXME, eventually a
479 psymtab). */
481 static void
482 coff_symfile_init (struct objfile *objfile)
484 /* Allocate struct to keep track of the symfile. */
485 coff_objfile_data_key.emplace (objfile);
488 /* This function is called for every section; it finds the outer
489 limits of the line table (minimum and maximum file offset) so that
490 the mainline code can read the whole thing for efficiency. */
492 static void
493 find_linenos (bfd *abfd, struct bfd_section *asect, void *vpinfo)
495 struct coff_symfile_info *info;
496 int size, count;
497 file_ptr offset, maxoff;
499 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
500 count = asect->lineno_count;
501 /* End of warning. */
503 if (count == 0)
504 return;
505 size = count * local_linesz;
507 info = (struct coff_symfile_info *) vpinfo;
508 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
509 offset = asect->line_filepos;
510 /* End of warning. */
512 if (offset < info->min_lineno_offset || info->min_lineno_offset == 0)
513 info->min_lineno_offset = offset;
515 maxoff = offset + size;
516 if (maxoff > info->max_lineno_offset)
517 info->max_lineno_offset = maxoff;
521 /* A helper function for coff_symfile_read that reads minimal
522 symbols. It may also read other forms of symbol as well. */
524 static void
525 coff_read_minsyms (file_ptr symtab_offset, unsigned int nsyms,
526 struct objfile *objfile)
529 /* If minimal symbols were already read, and if we know we aren't
530 going to read any other kind of symbol here, then we can just
531 return. */
532 if (objfile->per_bfd->minsyms_read && pe_file && nsyms == 0)
533 return;
535 minimal_symbol_reader reader (objfile);
537 if (pe_file && nsyms == 0)
539 /* We've got no debugging symbols, but it's a portable
540 executable, so try to read the export table. */
541 read_pe_exported_syms (reader, objfile);
543 else
545 /* Now that the executable file is positioned at symbol table,
546 process it and define symbols accordingly. */
547 coff_symtab_read (reader, symtab_offset, nsyms, objfile);
550 /* Install any minimal symbols that have been collected as the
551 current minimal symbols for this objfile. */
553 reader.install ();
555 if (pe_file)
557 for (minimal_symbol *msym : objfile->msymbols ())
559 const char *name = msym->linkage_name ();
561 /* If the minimal symbols whose name are prefixed by "__imp_"
562 or "_imp_", get rid of the prefix, and search the minimal
563 symbol in OBJFILE. Note that 'maintenance print msymbols'
564 shows that type of these "_imp_XXXX" symbols is mst_data. */
565 if (msym->type () == mst_data)
567 const char *name1 = NULL;
569 if (startswith (name, "_imp_"))
570 name1 = name + 5;
571 else if (startswith (name, "__imp_"))
572 name1 = name + 6;
573 if (name1 != NULL)
575 int lead
576 = bfd_get_symbol_leading_char (objfile->obfd.get ());
577 struct bound_minimal_symbol found;
579 if (lead != '\0' && *name1 == lead)
580 name1 += 1;
582 found = lookup_minimal_symbol (name1, NULL, objfile);
584 /* If found, there are symbols named "_imp_foo" and "foo"
585 respectively in OBJFILE. Set the type of symbol "foo"
586 as 'mst_solib_trampoline'. */
587 if (found.minsym != NULL
588 && found.minsym->type () == mst_text)
589 found.minsym->set_type (mst_solib_trampoline);
596 /* The BFD for this file -- only good while we're actively reading
597 symbols into a psymtab or a symtab. */
599 static bfd *symfile_bfd;
601 /* Read a symbol file, after initialization by coff_symfile_init. */
603 static void
604 coff_symfile_read (struct objfile *objfile, symfile_add_flags symfile_flags)
606 struct coff_symfile_info *info;
607 bfd *abfd = objfile->obfd.get ();
608 coff_data_type *cdata = coff_data (abfd);
609 const char *filename = bfd_get_filename (abfd);
610 int val;
611 unsigned int num_symbols;
612 file_ptr symtab_offset;
613 file_ptr stringtab_offset;
614 unsigned int stabstrsize;
616 info = coff_objfile_data_key.get (objfile);
617 symfile_bfd = abfd; /* Kludge for swap routines. */
619 std::vector<asection *> stabsects;
620 scoped_restore restore_stabsects
621 = make_scoped_restore (&info->stabsects, &stabsects);
623 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
624 num_symbols = bfd_get_symcount (abfd); /* How many syms */
625 symtab_offset = cdata->sym_filepos; /* Symbol table file offset */
626 stringtab_offset = symtab_offset + /* String table file offset */
627 num_symbols * cdata->local_symesz;
629 /* Set a few file-statics that give us specific information about
630 the particular COFF file format we're reading. */
631 local_n_btmask = cdata->local_n_btmask;
632 local_n_btshft = cdata->local_n_btshft;
633 local_n_tmask = cdata->local_n_tmask;
634 local_n_tshift = cdata->local_n_tshift;
635 local_linesz = cdata->local_linesz;
636 local_symesz = cdata->local_symesz;
637 local_auxesz = cdata->local_auxesz;
639 /* Allocate space for raw symbol and aux entries, based on their
640 space requirements as reported by BFD. */
641 gdb::def_vector<char> temp_storage (cdata->local_symesz
642 + cdata->local_auxesz);
643 temp_sym = temp_storage.data ();
644 temp_aux = temp_sym + cdata->local_symesz;
646 /* We need to know whether this is a PE file, because in PE files,
647 unlike standard COFF files, symbol values are stored as offsets
648 from the section address, rather than as absolute addresses.
649 FIXME: We should use BFD to read the symbol table, and thus avoid
650 this problem. */
651 pe_file =
652 startswith (bfd_get_target (objfile->obfd.get ()), "pe")
653 || startswith (bfd_get_target (objfile->obfd.get ()), "epoc-pe");
655 /* End of warning. */
657 info->min_lineno_offset = 0;
658 info->max_lineno_offset = 0;
660 /* Only read line number information if we have symbols.
662 On Windows NT, some of the system's DLL's have sections with
663 PointerToLinenumbers fields that are non-zero, but point at
664 random places within the image file. (In the case I found,
665 KERNEL32.DLL's .text section has a line number info pointer that
666 points into the middle of the string `lib\\i386\kernel32.dll'.)
668 However, these DLL's also have no symbols. The line number
669 tables are meaningless without symbols. And in fact, GDB never
670 uses the line number information unless there are symbols. So we
671 can avoid spurious error messages (and maybe run a little
672 faster!) by not even reading the line number table unless we have
673 symbols. */
674 scoped_restore restore_linetab = make_scoped_restore (&linetab);
675 gdb::unique_xmalloc_ptr<char> linetab_storage;
676 if (num_symbols > 0)
678 /* Read the line number table, all at once. */
679 bfd_map_over_sections (abfd, find_linenos, (void *) info);
681 val = init_lineno (abfd, info->min_lineno_offset,
682 info->max_lineno_offset - info->min_lineno_offset,
683 &linetab_storage);
684 if (val < 0)
685 error (_("\"%s\": error reading line numbers."), filename);
688 /* Now read the string table, all at once. */
690 scoped_restore restore_stringtab = make_scoped_restore (&stringtab);
691 gdb::unique_xmalloc_ptr<char> stringtab_storage;
692 val = init_stringtab (abfd, stringtab_offset, &stringtab_storage);
693 if (val < 0)
694 error (_("\"%s\": can't get string table"), filename);
696 coff_read_minsyms (symtab_offset, num_symbols, objfile);
698 if (!(objfile->flags & OBJF_READNEVER))
699 bfd_map_over_sections (abfd, coff_locate_sections, (void *) info);
701 if (!info->stabsects->empty())
703 if (!info->stabstrsect)
705 error (_("The debugging information in `%s' is corrupted.\nThe "
706 "file has a `.stabs' section, but no `.stabstr' section."),
707 filename);
710 /* FIXME: dubious. Why can't we use something normal like
711 bfd_get_section_contents? */
712 stabstrsize = bfd_section_size (info->stabstrsect);
714 coffstab_build_psymtabs (objfile,
715 info->textaddr, info->textsize,
716 *info->stabsects,
717 info->stabstrsect->filepos, stabstrsize);
720 if (dwarf2_initialize_objfile (objfile))
722 /* Nothing. */
725 /* Try to add separate debug file if no symbols table found. */
726 else if (!objfile->has_partial_symbols ()
727 && objfile->separate_debug_objfile == NULL
728 && objfile->separate_debug_objfile_backlink == NULL)
730 if (objfile->find_and_add_separate_symbol_file (symfile_flags))
731 gdb_assert (objfile->separate_debug_objfile != nullptr);
735 static void
736 coff_new_init (struct objfile *ignore)
740 /* Perform any local cleanups required when we are done with a
741 particular objfile. I.E, we are in the process of discarding all
742 symbol information for an objfile, freeing up all memory held for
743 it, and unlinking the objfile struct from the global list of known
744 objfiles. */
746 static void
747 coff_symfile_finish (struct objfile *objfile)
749 /* Let stabs reader clean up. */
750 stabsread_clear_cache ();
754 /* Given pointers to a symbol table in coff style exec file,
755 analyze them and create struct symtab's describing the symbols.
756 NSYMS is the number of symbols in the symbol table.
757 We read them one at a time using read_one_sym (). */
759 static void
760 coff_symtab_read (minimal_symbol_reader &reader,
761 file_ptr symtab_offset, unsigned int nsyms,
762 struct objfile *objfile)
764 struct gdbarch *gdbarch = objfile->arch ();
765 struct context_stack *newobj = nullptr;
766 struct coff_symbol coff_symbol;
767 struct coff_symbol *cs = &coff_symbol;
768 static struct internal_syment main_sym;
769 static union internal_auxent main_aux;
770 struct coff_symbol fcn_cs_saved;
771 static struct internal_syment fcn_sym_saved;
772 static union internal_auxent fcn_aux_saved;
773 /* A .file is open. */
774 int in_source_file = 0;
775 int next_file_symnum = -1;
776 /* Name of the current file. */
777 const char *filestring = "";
778 int depth = 0;
779 int fcn_first_line = 0;
780 CORE_ADDR fcn_first_line_addr = 0;
781 int fcn_last_line = 0;
782 int fcn_start_addr = 0;
783 long fcn_line_ptr = 0;
784 int val;
785 CORE_ADDR tmpaddr;
786 struct minimal_symbol *msym;
788 scoped_free_pendings free_pending;
790 /* Position to read the symbol table. */
791 val = bfd_seek (objfile->obfd.get (), symtab_offset, 0);
792 if (val < 0)
793 perror_with_name (objfile_name (objfile));
795 coffread_objfile = objfile;
796 nlist_bfd_global = objfile->obfd.get ();
797 nlist_nsyms_global = nsyms;
798 set_last_source_file (NULL);
799 memset (opaque_type_chain, 0, sizeof opaque_type_chain);
801 if (type_vector) /* Get rid of previous one. */
802 xfree (type_vector);
803 type_vector_length = INITIAL_TYPE_VECTOR_LENGTH;
804 type_vector = XCNEWVEC (struct type *, type_vector_length);
806 coff_start_compunit_symtab (objfile, "");
808 symnum = 0;
809 while (symnum < nsyms)
811 QUIT; /* Make this command interruptable. */
813 read_one_sym (cs, &main_sym, &main_aux);
815 if (cs->c_symnum == next_file_symnum && cs->c_sclass != C_FILE)
817 if (get_last_source_file ())
818 coff_end_compunit_symtab (objfile);
820 coff_start_compunit_symtab (objfile, "_globals_");
821 /* coff_start_compunit_symtab will set the language of this symtab to
822 language_unknown, since such a ``file name'' is not
823 recognized. Override that with the minimal language to
824 allow printing values in this symtab. */
825 get_current_subfile ()->language = language_minimal;
826 complete_symtab ("_globals_", 0, 0);
827 /* Done with all files, everything from here on out is
828 globals. */
831 /* Special case for file with type declarations only, no
832 text. */
833 if (!get_last_source_file () && SDB_TYPE (cs->c_type)
834 && cs->c_secnum == N_DEBUG)
835 complete_symtab (filestring, 0, 0);
837 /* Typedefs should not be treated as symbol definitions. */
838 if (ISFCN (cs->c_type) && cs->c_sclass != C_TPDEF)
840 /* Record all functions -- external and static -- in
841 minsyms. */
842 int section = cs_to_section (cs, objfile);
844 tmpaddr = cs->c_value;
845 /* Don't record unresolved symbols. */
846 if (!(cs->c_secnum <= 0 && cs->c_value == 0))
847 record_minimal_symbol (reader, cs,
848 unrelocated_addr (tmpaddr),
849 mst_text, section, objfile);
851 fcn_line_ptr = main_aux.x_sym.x_fcnary.x_fcn.x_lnnoptr;
852 fcn_start_addr = tmpaddr;
853 fcn_cs_saved = *cs;
854 fcn_sym_saved = main_sym;
855 fcn_aux_saved = main_aux;
856 continue;
859 switch (cs->c_sclass)
861 case C_EFCN:
862 case C_EXTDEF:
863 case C_ULABEL:
864 case C_USTATIC:
865 case C_LINE:
866 case C_ALIAS:
867 case C_HIDDEN:
868 complaint (_("Bad n_sclass for symbol %s"),
869 cs->c_name);
870 break;
872 case C_FILE:
873 /* c_value field contains symnum of next .file entry in
874 table or symnum of first global after last .file. */
875 next_file_symnum = cs->c_value;
876 if (cs->c_naux > 0)
877 filestring = coff_getfilename (&main_aux);
878 else
879 filestring = "";
881 /* Complete symbol table for last object file
882 containing debugging information. */
883 if (get_last_source_file ())
885 coff_end_compunit_symtab (objfile);
886 coff_start_compunit_symtab (objfile, filestring);
888 in_source_file = 1;
889 break;
891 /* C_LABEL is used for labels and static functions.
892 Including it here allows gdb to see static functions when
893 no debug info is available. */
894 case C_LABEL:
895 /* However, labels within a function can make weird
896 backtraces, so filter them out (from phdm@macqel.be). */
897 if (within_function)
898 break;
899 [[fallthrough]];
900 case C_STAT:
901 case C_THUMBLABEL:
902 case C_THUMBSTAT:
903 case C_THUMBSTATFUNC:
904 if (cs->c_name[0] == '.')
906 if (strcmp (cs->c_name, ".text") == 0)
908 /* FIXME: don't wire in ".text" as section name or
909 symbol name! */
910 /* Check for in_source_file deals with case of a
911 file with debugging symbols followed by a later
912 file with no symbols. */
913 if (in_source_file)
914 complete_symtab (filestring,
915 (cs->c_value
916 + objfile->text_section_offset ()),
917 main_aux.x_scn.x_scnlen);
918 in_source_file = 0;
920 /* Flush rest of '.' symbols. */
921 break;
923 else if (!SDB_TYPE (cs->c_type)
924 && cs->c_name[0] == 'L'
925 && (startswith (cs->c_name, "LI%")
926 || startswith (cs->c_name, "LF%")
927 || startswith (cs->c_name, "LC%")
928 || startswith (cs->c_name, "LP%")
929 || startswith (cs->c_name, "LPB%")
930 || startswith (cs->c_name, "LBB%")
931 || startswith (cs->c_name, "LBE%")
932 || startswith (cs->c_name, "LPBX%")))
933 /* At least on a 3b1, gcc generates swbeg and string labels
934 that look like this. Ignore them. */
935 break;
936 /* For static symbols that don't start with '.'... */
937 [[fallthrough]];
938 case C_THUMBEXT:
939 case C_THUMBEXTFUNC:
940 case C_EXT:
942 /* Record it in the minimal symbols regardless of
943 SDB_TYPE. This parallels what we do for other debug
944 formats, and probably is needed to make
945 print_address_symbolic work right without the (now
946 gone) "set fast-symbolic-addr off" kludge. */
948 enum minimal_symbol_type ms_type;
949 int sec;
950 CORE_ADDR offset = 0;
952 if (cs->c_secnum == N_UNDEF)
954 /* This is a common symbol. We used to rely on
955 the target to tell us whether it knows where
956 the symbol has been relocated to, but none of
957 the target implementations actually provided
958 that operation. So we just ignore the symbol,
959 the same way we would do if we had a target-side
960 symbol lookup which returned no match. */
961 break;
963 else if (cs->c_secnum == N_ABS)
965 /* Use the correct minimal symbol type (and don't
966 relocate) for absolute values. */
967 ms_type = mst_abs;
968 sec = cs_to_section (cs, objfile);
969 tmpaddr = cs->c_value;
971 else
973 asection *bfd_section = cs_to_bfd_section (cs, objfile);
975 sec = cs_to_section (cs, objfile);
976 tmpaddr = cs->c_value;
977 /* Statics in a PE file also get relocated. */
978 if (cs->c_sclass == C_EXT
979 || cs->c_sclass == C_THUMBEXTFUNC
980 || cs->c_sclass == C_THUMBEXT
981 || (pe_file && (cs->c_sclass == C_STAT)))
982 offset = objfile->section_offsets[sec];
984 if (bfd_section->flags & SEC_CODE)
986 ms_type =
987 cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXTFUNC
988 || cs->c_sclass == C_THUMBEXT ?
989 mst_text : mst_file_text;
990 tmpaddr = gdbarch_addr_bits_remove (gdbarch, tmpaddr);
992 else if (bfd_section->flags & SEC_ALLOC
993 && bfd_section->flags & SEC_LOAD)
995 ms_type =
996 cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXT
997 ? mst_data : mst_file_data;
999 else if (bfd_section->flags & SEC_ALLOC)
1001 ms_type =
1002 cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXT
1003 ? mst_bss : mst_file_bss;
1005 else
1006 ms_type = mst_unknown;
1009 msym = record_minimal_symbol (reader, cs,
1010 unrelocated_addr (tmpaddr),
1011 ms_type, sec, objfile);
1012 if (msym)
1013 gdbarch_coff_make_msymbol_special (gdbarch,
1014 cs->c_sclass, msym);
1016 if (SDB_TYPE (cs->c_type))
1018 struct symbol *sym;
1020 sym = process_coff_symbol
1021 (cs, &main_aux, objfile);
1022 sym->set_value_longest (tmpaddr + offset);
1023 sym->set_section_index (sec);
1026 break;
1028 case C_FCN:
1029 if (strcmp (cs->c_name, ".bf") == 0)
1031 within_function = 1;
1033 /* Value contains address of first non-init type
1034 code. */
1035 /* main_aux.x_sym.x_misc.x_lnsz.x_lnno
1036 contains line number of '{' }. */
1037 if (cs->c_naux != 1)
1038 complaint (_("`.bf' symbol %d has no aux entry"),
1039 cs->c_symnum);
1040 fcn_first_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
1041 fcn_first_line_addr = cs->c_value;
1043 /* Might want to check that locals are 0 and
1044 context_stack_depth is zero, and complain if not. */
1046 depth = 0;
1047 newobj = push_context (depth, fcn_start_addr);
1048 fcn_cs_saved.c_name = getsymname (&fcn_sym_saved);
1049 newobj->name =
1050 process_coff_symbol (&fcn_cs_saved,
1051 &fcn_aux_saved, objfile);
1053 else if (strcmp (cs->c_name, ".ef") == 0)
1055 if (!within_function)
1056 error (_("Bad coff function information."));
1057 /* The value of .ef is the address of epilogue code;
1058 not useful for gdb. */
1059 /* { main_aux.x_sym.x_misc.x_lnsz.x_lnno
1060 contains number of lines to '}' */
1062 if (outermost_context_p ())
1063 { /* We attempted to pop an empty context stack. */
1064 complaint (_("`.ef' symbol without matching `.bf' "
1065 "symbol ignored starting at symnum %d"),
1066 cs->c_symnum);
1067 within_function = 0;
1068 break;
1071 struct context_stack cstk = pop_context ();
1072 /* Stack must be empty now. */
1073 if (!outermost_context_p () || newobj == NULL)
1075 complaint (_("Unmatched .ef symbol(s) ignored "
1076 "starting at symnum %d"),
1077 cs->c_symnum);
1078 within_function = 0;
1079 break;
1081 if (cs->c_naux != 1)
1083 complaint (_("`.ef' symbol %d has no aux entry"),
1084 cs->c_symnum);
1085 fcn_last_line = 0x7FFFFFFF;
1087 else
1089 fcn_last_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
1091 /* fcn_first_line is the line number of the opening '{'.
1092 Do not record it - because it would affect gdb's idea
1093 of the line number of the first statement of the
1094 function - except for one-line functions, for which
1095 it is also the line number of all the statements and
1096 of the closing '}', and for which we do not have any
1097 other statement-line-number. */
1098 if (fcn_last_line == 1)
1099 record_line
1100 (get_current_subfile (), fcn_first_line,
1101 unrelocated_addr (gdbarch_addr_bits_remove (gdbarch,
1102 fcn_first_line_addr)));
1103 else
1104 enter_linenos (fcn_line_ptr, fcn_first_line,
1105 fcn_last_line, objfile);
1107 finish_block (cstk.name, cstk.old_blocks,
1108 NULL, cstk.start_addr,
1109 fcn_cs_saved.c_value
1110 + fcn_aux_saved.x_sym.x_misc.x_fsize
1111 + objfile->text_section_offset ());
1112 within_function = 0;
1114 break;
1116 case C_BLOCK:
1117 if (strcmp (cs->c_name, ".bb") == 0)
1119 tmpaddr = cs->c_value;
1120 tmpaddr += objfile->text_section_offset ();
1121 push_context (++depth, tmpaddr);
1123 else if (strcmp (cs->c_name, ".eb") == 0)
1125 if (outermost_context_p ())
1126 { /* We attempted to pop an empty context stack. */
1127 complaint (_("`.eb' symbol without matching `.bb' "
1128 "symbol ignored starting at symnum %d"),
1129 cs->c_symnum);
1130 break;
1133 struct context_stack cstk = pop_context ();
1134 if (depth-- != cstk.depth)
1136 complaint (_("Mismatched .eb symbol ignored "
1137 "starting at symnum %d"),
1138 symnum);
1139 break;
1141 if (*get_local_symbols () && !outermost_context_p ())
1143 tmpaddr = cs->c_value + objfile->text_section_offset ();
1144 /* Make a block for the local symbols within. */
1145 finish_block (0, cstk.old_blocks, NULL,
1146 cstk.start_addr, tmpaddr);
1148 /* Now pop locals of block just finished. */
1149 *get_local_symbols () = cstk.locals;
1151 break;
1153 default:
1154 process_coff_symbol (cs, &main_aux, objfile);
1155 break;
1159 if (get_last_source_file ())
1160 coff_end_compunit_symtab (objfile);
1162 /* Patch up any opaque types (references to types that are not defined
1163 in the file where they are referenced, e.g. "struct foo *bar"). */
1165 for (compunit_symtab *cu : objfile->compunits ())
1167 for (symtab *s : cu->filetabs ())
1168 patch_opaque_types (s);
1172 coffread_objfile = NULL;
1175 /* Routines for reading headers and symbols from executable. */
1177 /* Read the next symbol, swap it, and return it in both
1178 internal_syment form, and coff_symbol form. Also return its first
1179 auxent, if any, in internal_auxent form, and skip any other
1180 auxents. */
1182 static void
1183 read_one_sym (struct coff_symbol *cs,
1184 struct internal_syment *sym,
1185 union internal_auxent *aux)
1187 int i;
1188 bfd_size_type bytes;
1190 cs->c_symnum = symnum;
1191 bytes = bfd_read (temp_sym, local_symesz, nlist_bfd_global);
1192 if (bytes != local_symesz)
1193 error (_("%s: error reading symbols"), objfile_name (coffread_objfile));
1194 bfd_coff_swap_sym_in (symfile_bfd, temp_sym, (char *) sym);
1195 cs->c_naux = sym->n_numaux & 0xff;
1196 if (cs->c_naux >= 1)
1198 bytes = bfd_read (temp_aux, local_auxesz, nlist_bfd_global);
1199 if (bytes != local_auxesz)
1200 error (_("%s: error reading symbols"), objfile_name (coffread_objfile));
1201 bfd_coff_swap_aux_in (symfile_bfd, temp_aux,
1202 sym->n_type, sym->n_sclass,
1203 0, cs->c_naux, (char *) aux);
1204 /* If more than one aux entry, read past it (only the first aux
1205 is important). */
1206 for (i = 1; i < cs->c_naux; i++)
1208 bytes = bfd_read (temp_aux, local_auxesz, nlist_bfd_global);
1209 if (bytes != local_auxesz)
1210 error (_("%s: error reading symbols"),
1211 objfile_name (coffread_objfile));
1214 cs->c_name = getsymname (sym);
1215 cs->c_value = sym->n_value;
1216 cs->c_sclass = (sym->n_sclass & 0xff);
1217 cs->c_secnum = sym->n_scnum;
1218 cs->c_type = (unsigned) sym->n_type;
1219 if (!SDB_TYPE (cs->c_type))
1220 cs->c_type = 0;
1222 #if 0
1223 if (cs->c_sclass & 128)
1224 printf (_("thumb symbol %s, class 0x%x\n"), cs->c_name, cs->c_sclass);
1225 #endif
1227 symnum += 1 + cs->c_naux;
1229 /* The PE file format stores symbol values as offsets within the
1230 section, rather than as absolute addresses. We correct that
1231 here, if the symbol has an appropriate storage class. FIXME: We
1232 should use BFD to read the symbols, rather than duplicating the
1233 work here. */
1234 if (pe_file)
1236 switch (cs->c_sclass)
1238 case C_EXT:
1239 case C_THUMBEXT:
1240 case C_THUMBEXTFUNC:
1241 case C_SECTION:
1242 case C_NT_WEAK:
1243 case C_STAT:
1244 case C_THUMBSTAT:
1245 case C_THUMBSTATFUNC:
1246 case C_LABEL:
1247 case C_THUMBLABEL:
1248 case C_BLOCK:
1249 case C_FCN:
1250 case C_EFCN:
1251 if (cs->c_secnum != 0)
1252 cs->c_value += cs_section_address (cs, symfile_bfd);
1253 break;
1258 /* Support for string table handling. */
1260 static int
1261 init_stringtab (bfd *abfd, file_ptr offset, gdb::unique_xmalloc_ptr<char> *storage)
1263 long length;
1264 int val;
1265 unsigned char lengthbuf[4];
1267 /* If the file is stripped, the offset might be zero, indicating no
1268 string table. Just return with `stringtab' set to null. */
1269 if (offset == 0)
1270 return 0;
1272 if (bfd_seek (abfd, offset, 0) < 0)
1273 return -1;
1275 val = bfd_read (lengthbuf, sizeof lengthbuf, abfd);
1276 /* If no string table is needed, then the file may end immediately
1277 after the symbols. Just return with `stringtab' set to null. */
1278 if (val != sizeof lengthbuf)
1279 return 0;
1280 length = bfd_h_get_32 (symfile_bfd, lengthbuf);
1281 if (length < sizeof lengthbuf)
1282 return 0;
1284 storage->reset ((char *) xmalloc (length));
1285 stringtab = storage->get ();
1286 /* This is in target format (probably not very useful, and not
1287 currently used), not host format. */
1288 memcpy (stringtab, lengthbuf, sizeof lengthbuf);
1289 stringtab_length = length;
1290 if (length == sizeof length) /* Empty table -- just the count. */
1291 return 0;
1293 val = bfd_read (stringtab + sizeof lengthbuf,
1294 length - sizeof lengthbuf, abfd);
1295 if (val != length - sizeof lengthbuf || stringtab[length - 1] != '\0')
1296 return -1;
1298 return 0;
1301 static char *
1302 getsymname (struct internal_syment *symbol_entry)
1304 static char buffer[SYMNMLEN + 1];
1305 char *result;
1307 if (symbol_entry->_n._n_n._n_zeroes == 0)
1309 if (symbol_entry->_n._n_n._n_offset > stringtab_length)
1310 error (_("COFF Error: string table offset (%s) outside string table (length %ld)"),
1311 hex_string (symbol_entry->_n._n_n._n_offset), stringtab_length);
1312 result = stringtab + symbol_entry->_n._n_n._n_offset;
1314 else
1316 strncpy (buffer, symbol_entry->_n._n_name, SYMNMLEN);
1317 buffer[SYMNMLEN] = '\0';
1318 result = buffer;
1320 return result;
1323 /* Extract the file name from the aux entry of a C_FILE symbol.
1324 Return only the last component of the name. Result is in static
1325 storage and is only good for temporary use. */
1327 static const char *
1328 coff_getfilename (union internal_auxent *aux_entry)
1330 static char buffer[BUFSIZ];
1331 const char *result;
1333 if (aux_entry->x_file.x_n.x_n.x_zeroes == 0)
1335 if (strlen (stringtab + aux_entry->x_file.x_n.x_n.x_offset) >= BUFSIZ)
1336 internal_error (_("coff file name too long"));
1337 strcpy (buffer, stringtab + aux_entry->x_file.x_n.x_n.x_offset);
1339 else
1341 size_t x_fname_len = sizeof (aux_entry->x_file.x_n.x_fname);
1342 strncpy (buffer, aux_entry->x_file.x_n.x_fname, x_fname_len);
1343 buffer[x_fname_len] = '\0';
1345 result = buffer;
1347 /* FIXME: We should not be throwing away the information about what
1348 directory. It should go into dirname of the symtab, or some such
1349 place. */
1350 result = lbasename (result);
1351 return (result);
1354 /* Support for line number handling. */
1356 /* Read in all the line numbers for fast lookups later. Leave them in
1357 external (unswapped) format in memory; we'll swap them as we enter
1358 them into GDB's data structures. */
1360 static int
1361 init_lineno (bfd *abfd, file_ptr offset, file_ptr size,
1362 gdb::unique_xmalloc_ptr<char> *storage)
1364 int val;
1366 linetab_offset = offset;
1367 linetab_size = size;
1369 if (size == 0)
1370 return 0;
1372 if (bfd_seek (abfd, offset, 0) < 0)
1373 return -1;
1375 /* Allocate the desired table, plus a sentinel. */
1376 storage->reset ((char *) xmalloc (size + local_linesz));
1377 linetab = storage->get ();
1379 val = bfd_read (storage->get (), size, abfd);
1380 if (val != size)
1381 return -1;
1383 /* Terminate it with an all-zero sentinel record. */
1384 memset (linetab + size, 0, local_linesz);
1386 return 0;
1389 #if !defined (L_LNNO32)
1390 #define L_LNNO32(lp) ((lp)->l_lnno)
1391 #endif
1393 static void
1394 enter_linenos (file_ptr file_offset, int first_line,
1395 int last_line, struct objfile *objfile)
1397 struct gdbarch *gdbarch = objfile->arch ();
1398 char *rawptr;
1399 struct internal_lineno lptr;
1401 if (!linetab)
1402 return;
1403 if (file_offset < linetab_offset)
1405 complaint (_("Line number pointer %s lower than start of line numbers"),
1406 plongest (file_offset));
1407 if (file_offset > linetab_size) /* Too big to be an offset? */
1408 return;
1409 file_offset += linetab_offset; /* Try reading at that linetab
1410 offset. */
1413 rawptr = &linetab[file_offset - linetab_offset];
1415 /* Skip first line entry for each function. */
1416 rawptr += local_linesz;
1417 /* Line numbers start at one for the first line of the function. */
1418 first_line--;
1420 /* If the line number table is full (e.g. 64K lines in COFF debug
1421 info), the next function's L_LNNO32 might not be zero, so don't
1422 overstep the table's end in any case. */
1423 while (rawptr <= &linetab[0] + linetab_size)
1425 bfd_coff_swap_lineno_in (symfile_bfd, rawptr, &lptr);
1426 rawptr += local_linesz;
1427 /* The next function, or the sentinel, will have L_LNNO32 zero;
1428 we exit. */
1429 if (L_LNNO32 (&lptr) && L_LNNO32 (&lptr) <= last_line)
1431 CORE_ADDR addr = lptr.l_addr.l_paddr;
1432 record_line (get_current_subfile (),
1433 first_line + L_LNNO32 (&lptr),
1434 unrelocated_addr (gdbarch_addr_bits_remove (gdbarch,
1435 addr)));
1437 else
1438 break;
1442 static void
1443 patch_type (struct type *type, struct type *real_type)
1445 struct type *target = type->target_type ();
1446 struct type *real_target = real_type->target_type ();
1448 target->set_length (real_target->length ());
1449 target->copy_fields (real_target);
1451 if (real_target->name ())
1453 /* The previous copy of TYPE_NAME is allocated by
1454 process_coff_symbol. */
1455 xfree ((char *) target->name ());
1456 target->set_name (xstrdup (real_target->name ()));
1460 /* Patch up all appropriate typedef symbols in the opaque_type_chains
1461 so that they can be used to print out opaque data structures
1462 properly. */
1464 static void
1465 patch_opaque_types (struct symtab *s)
1467 /* Go through the per-file symbols only. */
1468 const struct block *b = s->compunit ()->blockvector ()->static_block ();
1469 for (struct symbol *real_sym : block_iterator_range (b))
1471 /* Find completed typedefs to use to fix opaque ones.
1472 Remove syms from the chain when their types are stored,
1473 but search the whole chain, as there may be several syms
1474 from different files with the same name. */
1475 if (real_sym->aclass () == LOC_TYPEDEF
1476 && real_sym->domain () == VAR_DOMAIN
1477 && real_sym->type ()->code () == TYPE_CODE_PTR
1478 && real_sym->type ()->target_type ()->length () != 0)
1480 const char *name = real_sym->linkage_name ();
1481 int hash = hashname (name);
1482 struct symbol *sym, *prev;
1484 prev = 0;
1485 for (sym = opaque_type_chain[hash]; sym;)
1487 if (name[0] == sym->linkage_name ()[0]
1488 && strcmp (name + 1, sym->linkage_name () + 1) == 0)
1490 if (prev)
1491 prev->set_value_chain (sym->value_chain ());
1492 else
1493 opaque_type_chain[hash] = sym->value_chain ();
1495 patch_type (sym->type (), real_sym->type ());
1497 if (prev)
1498 sym = prev->value_chain ();
1499 else
1500 sym = opaque_type_chain[hash];
1502 else
1504 prev = sym;
1505 sym->set_value_chain (sym);
1512 static int
1513 coff_reg_to_regnum (struct symbol *sym, struct gdbarch *gdbarch)
1515 return gdbarch_sdb_reg_to_regnum (gdbarch, sym->value_longest ());
1518 static const struct symbol_register_ops coff_register_funcs = {
1519 coff_reg_to_regnum
1522 /* The "aclass" index for computed COFF symbols. */
1524 static int coff_register_index;
1526 static struct symbol *
1527 process_coff_symbol (struct coff_symbol *cs,
1528 union internal_auxent *aux,
1529 struct objfile *objfile)
1531 struct symbol *sym = new (&objfile->objfile_obstack) symbol;
1532 char *name;
1534 name = cs->c_name;
1535 name = EXTERNAL_NAME (name, objfile->obfd.get ());
1536 sym->set_language (get_current_subfile ()->language,
1537 &objfile->objfile_obstack);
1538 sym->compute_and_set_names (name, true, objfile->per_bfd);
1540 /* default assumptions */
1541 sym->set_value_longest (cs->c_value);
1542 sym->set_domain (VAR_DOMAIN);
1543 sym->set_section_index (cs_to_section (cs, objfile));
1545 if (ISFCN (cs->c_type))
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 (VAR_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);