Fix building Loongarch BFD with a 32-bit compiler
[binutils-gdb.git] / gdb / coffread.c
blobdaae3ffe989c04ecdaa5e891ed2ab6a380e4487a
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 "event-top.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 () == TYPE_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_domain (FUNCTION_DOMAIN);
1548 sym->set_value_longest
1549 (sym->value_longest () + objfile->text_section_offset ());
1550 sym->set_type
1551 (lookup_function_type (decode_function_type (cs, cs->c_type,
1552 aux, objfile)));
1554 sym->set_aclass_index (LOC_BLOCK);
1555 if (cs->c_sclass == C_STAT || cs->c_sclass == C_THUMBSTAT
1556 || cs->c_sclass == C_THUMBSTATFUNC)
1557 add_symbol_to_list (sym, get_file_symbols ());
1558 else if (cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXT
1559 || cs->c_sclass == C_THUMBEXTFUNC)
1560 add_symbol_to_list (sym, get_global_symbols ());
1562 else
1564 sym->set_type (decode_type (cs, cs->c_type, aux, objfile));
1565 switch (cs->c_sclass)
1567 case C_NULL:
1568 break;
1570 case C_AUTO:
1571 sym->set_aclass_index (LOC_LOCAL);
1572 add_symbol_to_list (sym, get_local_symbols ());
1573 break;
1575 case C_THUMBEXT:
1576 case C_THUMBEXTFUNC:
1577 case C_EXT:
1578 sym->set_aclass_index (LOC_STATIC);
1579 sym->set_value_address ((CORE_ADDR) cs->c_value
1580 + objfile->section_offsets[SECT_OFF_TEXT (objfile)]);
1581 add_symbol_to_list (sym, get_global_symbols ());
1582 break;
1584 case C_THUMBSTAT:
1585 case C_THUMBSTATFUNC:
1586 case C_STAT:
1587 sym->set_aclass_index (LOC_STATIC);
1588 sym->set_value_address ((CORE_ADDR) cs->c_value
1589 + objfile->section_offsets[SECT_OFF_TEXT (objfile)]);
1590 if (within_function)
1592 /* Static symbol of local scope. */
1593 add_symbol_to_list (sym, get_local_symbols ());
1595 else
1597 /* Static symbol at top level of file. */
1598 add_symbol_to_list (sym, get_file_symbols ());
1600 break;
1602 #ifdef C_GLBLREG /* AMD coff */
1603 case C_GLBLREG:
1604 #endif
1605 case C_REG:
1606 sym->set_aclass_index (coff_register_index);
1607 sym->set_value_longest (cs->c_value);
1608 add_symbol_to_list (sym, get_local_symbols ());
1609 break;
1611 case C_THUMBLABEL:
1612 case C_LABEL:
1613 break;
1615 case C_ARG:
1616 sym->set_aclass_index (LOC_ARG);
1617 sym->set_is_argument (1);
1618 add_symbol_to_list (sym, get_local_symbols ());
1619 break;
1621 case C_REGPARM:
1622 sym->set_aclass_index (coff_register_index);
1623 sym->set_is_argument (1);
1624 sym->set_value_longest (cs->c_value);
1625 add_symbol_to_list (sym, get_local_symbols ());
1626 break;
1628 case C_TPDEF:
1629 sym->set_aclass_index (LOC_TYPEDEF);
1630 sym->set_domain (TYPE_DOMAIN);
1632 /* If type has no name, give it one. */
1633 if (sym->type ()->name () == 0)
1635 if (sym->type ()->code () == TYPE_CODE_PTR
1636 || sym->type ()->code () == TYPE_CODE_FUNC)
1638 /* If we are giving a name to a type such as
1639 "pointer to foo" or "function returning foo", we
1640 better not set the TYPE_NAME. If the program
1641 contains "typedef char *caddr_t;", we don't want
1642 all variables of type char * to print as caddr_t.
1643 This is not just a consequence of GDB's type
1644 management; CC and GCC (at least through version
1645 2.4) both output variables of either type char *
1646 or caddr_t with the type refering to the C_TPDEF
1647 symbol for caddr_t. If a future compiler cleans
1648 this up it GDB is not ready for it yet, but if it
1649 becomes ready we somehow need to disable this
1650 check (without breaking the PCC/GCC2.4 case).
1652 Sigh.
1654 Fortunately, this check seems not to be necessary
1655 for anything except pointers or functions. */
1658 else
1659 sym->type ()->set_name (xstrdup (sym->linkage_name ()));
1662 /* Keep track of any type which points to empty structured
1663 type, so it can be filled from a definition from another
1664 file. A simple forward reference (TYPE_CODE_UNDEF) is
1665 not an empty structured type, though; the forward
1666 references work themselves out via the magic of
1667 coff_lookup_type. */
1668 if (sym->type ()->code () == TYPE_CODE_PTR
1669 && sym->type ()->target_type ()->length () == 0
1670 && sym->type ()->target_type ()->code ()
1671 != TYPE_CODE_UNDEF)
1673 int i = hashname (sym->linkage_name ());
1675 sym->set_value_chain (opaque_type_chain[i]);
1676 opaque_type_chain[i] = sym;
1678 add_symbol_to_list (sym, get_file_symbols ());
1679 break;
1681 case C_STRTAG:
1682 case C_UNTAG:
1683 case C_ENTAG:
1684 sym->set_aclass_index (LOC_TYPEDEF);
1685 sym->set_domain (STRUCT_DOMAIN);
1687 /* Some compilers try to be helpful by inventing "fake"
1688 names for anonymous enums, structures, and unions, like
1689 "~0fake" or ".0fake". Thanks, but no thanks... */
1690 if (sym->type ()->name () == 0)
1691 if (sym->linkage_name () != NULL
1692 && *sym->linkage_name () != '~'
1693 && *sym->linkage_name () != '.')
1694 sym->type ()->set_name (xstrdup (sym->linkage_name ()));
1696 add_symbol_to_list (sym, get_file_symbols ());
1697 break;
1699 default:
1700 break;
1703 return sym;
1706 /* Decode a coff type specifier; return the type that is meant. */
1708 static struct type *
1709 decode_type (struct coff_symbol *cs, unsigned int c_type,
1710 union internal_auxent *aux, struct objfile *objfile)
1712 struct type *type = 0;
1713 unsigned int new_c_type;
1715 if (c_type & ~N_BTMASK)
1717 new_c_type = DECREF (c_type);
1718 if (ISPTR (c_type))
1720 type = decode_type (cs, new_c_type, aux, objfile);
1721 type = lookup_pointer_type (type);
1723 else if (ISFCN (c_type))
1725 type = decode_type (cs, new_c_type, aux, objfile);
1726 type = lookup_function_type (type);
1728 else if (ISARY (c_type))
1730 int i, n;
1731 unsigned short *dim;
1732 struct type *base_type, *index_type, *range_type;
1734 /* Define an array type. */
1735 /* auxent refers to array, not base type. */
1736 if (aux->x_sym.x_tagndx.u32 == 0)
1737 cs->c_naux = 0;
1739 /* Shift the indices down. */
1740 dim = &aux->x_sym.x_fcnary.x_ary.x_dimen[0];
1741 i = 1;
1742 n = dim[0];
1743 for (i = 0; *dim && i < DIMNUM - 1; i++, dim++)
1744 *dim = *(dim + 1);
1745 *dim = 0;
1747 base_type = decode_type (cs, new_c_type, aux, objfile);
1748 index_type = builtin_type (objfile)->builtin_int;
1749 type_allocator alloc (objfile, language_c);
1750 range_type
1751 = create_static_range_type (alloc, index_type, 0, n - 1);
1752 type = create_array_type (alloc, base_type, range_type);
1754 return type;
1757 /* Reference to existing type. This only occurs with the struct,
1758 union, and enum types. EPI a29k coff fakes us out by producing
1759 aux entries with a nonzero x_tagndx for definitions of structs,
1760 unions, and enums, so we have to check the c_sclass field. SCO
1761 3.2v4 cc gets confused with pointers to pointers to defined
1762 structs, and generates negative x_tagndx fields. */
1763 if (cs->c_naux > 0 && aux->x_sym.x_tagndx.u32 != 0)
1765 if (cs->c_sclass != C_STRTAG
1766 && cs->c_sclass != C_UNTAG
1767 && cs->c_sclass != C_ENTAG
1768 && (int32_t) aux->x_sym.x_tagndx.u32 >= 0)
1770 type = coff_alloc_type (aux->x_sym.x_tagndx.u32);
1771 return type;
1773 else
1775 complaint (_("Symbol table entry for %s has bad tagndx value"),
1776 cs->c_name);
1777 /* And fall through to decode_base_type... */
1781 return decode_base_type (cs, BTYPE (c_type), aux, objfile);
1784 /* Decode a coff type specifier for function definition;
1785 return the type that the function returns. */
1787 static struct type *
1788 decode_function_type (struct coff_symbol *cs,
1789 unsigned int c_type,
1790 union internal_auxent *aux,
1791 struct objfile *objfile)
1793 if (aux->x_sym.x_tagndx.u32 == 0)
1794 cs->c_naux = 0; /* auxent refers to function, not base
1795 type. */
1797 return decode_type (cs, DECREF (c_type), aux, objfile);
1800 /* Basic C types. */
1802 static struct type *
1803 decode_base_type (struct coff_symbol *cs,
1804 unsigned int c_type,
1805 union internal_auxent *aux,
1806 struct objfile *objfile)
1808 struct gdbarch *gdbarch = objfile->arch ();
1809 struct type *type;
1811 switch (c_type)
1813 case T_NULL:
1814 /* Shows up with "void (*foo)();" structure members. */
1815 return builtin_type (objfile)->builtin_void;
1817 #ifdef T_VOID
1818 case T_VOID:
1819 /* Intel 960 COFF has this symbol and meaning. */
1820 return builtin_type (objfile)->builtin_void;
1821 #endif
1823 case T_CHAR:
1824 return builtin_type (objfile)->builtin_char;
1826 case T_SHORT:
1827 return builtin_type (objfile)->builtin_short;
1829 case T_INT:
1830 return builtin_type (objfile)->builtin_int;
1832 case T_LONG:
1833 if (cs->c_sclass == C_FIELD
1834 && aux->x_sym.x_misc.x_lnsz.x_size
1835 > gdbarch_long_bit (gdbarch))
1836 return builtin_type (objfile)->builtin_long_long;
1837 else
1838 return builtin_type (objfile)->builtin_long;
1840 case T_FLOAT:
1841 return builtin_type (objfile)->builtin_float;
1843 case T_DOUBLE:
1844 return builtin_type (objfile)->builtin_double;
1846 case T_LNGDBL:
1847 return builtin_type (objfile)->builtin_long_double;
1849 case T_STRUCT:
1850 if (cs->c_naux != 1)
1852 /* Anonymous structure type. */
1853 type = coff_alloc_type (cs->c_symnum);
1854 type->set_code (TYPE_CODE_STRUCT);
1855 type->set_name (NULL);
1856 INIT_CPLUS_SPECIFIC (type);
1857 type->set_length (0);
1858 type->set_fields (nullptr);
1859 type->set_num_fields (0);
1861 else
1863 type = coff_read_struct_type (cs->c_symnum,
1864 aux->x_sym.x_misc.x_lnsz.x_size,
1865 aux->x_sym.x_fcnary.x_fcn.x_endndx.u32,
1866 objfile);
1868 return type;
1870 case T_UNION:
1871 if (cs->c_naux != 1)
1873 /* Anonymous union type. */
1874 type = coff_alloc_type (cs->c_symnum);
1875 type->set_name (NULL);
1876 INIT_CPLUS_SPECIFIC (type);
1877 type->set_length (0);
1878 type->set_fields (nullptr);
1879 type->set_num_fields (0);
1881 else
1883 type = coff_read_struct_type (cs->c_symnum,
1884 aux->x_sym.x_misc.x_lnsz.x_size,
1885 aux->x_sym.x_fcnary.x_fcn.x_endndx.u32,
1886 objfile);
1888 type->set_code (TYPE_CODE_UNION);
1889 return type;
1891 case T_ENUM:
1892 if (cs->c_naux != 1)
1894 /* Anonymous enum type. */
1895 type = coff_alloc_type (cs->c_symnum);
1896 type->set_code (TYPE_CODE_ENUM);
1897 type->set_name (NULL);
1898 type->set_length (0);
1899 type->set_fields (nullptr);
1900 type->set_num_fields (0);
1902 else
1904 type = coff_read_enum_type (cs->c_symnum,
1905 aux->x_sym.x_misc.x_lnsz.x_size,
1906 aux->x_sym.x_fcnary.x_fcn.x_endndx.u32,
1907 objfile);
1909 return type;
1911 case T_MOE:
1912 /* Shouldn't show up here. */
1913 break;
1915 case T_UCHAR:
1916 return builtin_type (objfile)->builtin_unsigned_char;
1918 case T_USHORT:
1919 return builtin_type (objfile)->builtin_unsigned_short;
1921 case T_UINT:
1922 return builtin_type (objfile)->builtin_unsigned_int;
1924 case T_ULONG:
1925 if (cs->c_sclass == C_FIELD
1926 && aux->x_sym.x_misc.x_lnsz.x_size
1927 > gdbarch_long_bit (gdbarch))
1928 return builtin_type (objfile)->builtin_unsigned_long_long;
1929 else
1930 return builtin_type (objfile)->builtin_unsigned_long;
1932 complaint (_("Unexpected type for symbol %s"), cs->c_name);
1933 return builtin_type (objfile)->builtin_void;
1936 /* This page contains subroutines of read_type. */
1938 /* Read the description of a structure (or union type) and return an
1939 object describing the type. */
1941 static struct type *
1942 coff_read_struct_type (int index, int length, int lastsym,
1943 struct objfile *objfile)
1945 struct nextfield
1947 struct nextfield *next;
1948 struct field field;
1951 struct type *type;
1952 struct nextfield *list = 0;
1953 struct nextfield *newobj;
1954 int nfields = 0;
1955 int n;
1956 char *name;
1957 struct coff_symbol member_sym;
1958 struct coff_symbol *ms = &member_sym;
1959 struct internal_syment sub_sym;
1960 union internal_auxent sub_aux;
1961 int done = 0;
1963 type = coff_alloc_type (index);
1964 type->set_code (TYPE_CODE_STRUCT);
1965 INIT_CPLUS_SPECIFIC (type);
1966 type->set_length (length);
1968 while (!done && symnum < lastsym && symnum < nlist_nsyms_global)
1970 read_one_sym (ms, &sub_sym, &sub_aux);
1971 name = ms->c_name;
1972 name = EXTERNAL_NAME (name, objfile->obfd.get ());
1974 switch (ms->c_sclass)
1976 case C_MOS:
1977 case C_MOU:
1979 /* Get space to record the next field's data. */
1980 newobj = XALLOCA (struct nextfield);
1981 newobj->next = list;
1982 list = newobj;
1984 /* Save the data. */
1985 list->field.set_name (obstack_strdup (&objfile->objfile_obstack,
1986 name));
1987 list->field.set_type (decode_type (ms, ms->c_type, &sub_aux,
1988 objfile));
1989 list->field.set_loc_bitpos (8 * ms->c_value);
1990 list->field.set_bitsize (0);
1991 nfields++;
1992 break;
1994 case C_FIELD:
1996 /* Get space to record the next field's data. */
1997 newobj = XALLOCA (struct nextfield);
1998 newobj->next = list;
1999 list = newobj;
2001 /* Save the data. */
2002 list->field.set_name (obstack_strdup (&objfile->objfile_obstack,
2003 name));
2004 list->field.set_type (decode_type (ms, ms->c_type, &sub_aux,
2005 objfile));
2006 list->field.set_loc_bitpos (ms->c_value);
2007 list->field.set_bitsize (sub_aux.x_sym.x_misc.x_lnsz.x_size);
2008 nfields++;
2009 break;
2011 case C_EOS:
2012 done = 1;
2013 break;
2016 /* Now create the vector of fields, and record how big it is. */
2018 type->alloc_fields (nfields);
2020 /* Copy the saved-up fields into the field vector. */
2022 for (n = nfields; list; list = list->next)
2023 type->field (--n) = list->field;
2025 return type;
2028 /* Read a definition of an enumeration type,
2029 and create and return a suitable type object.
2030 Also defines the symbols that represent the values of the type. */
2032 static struct type *
2033 coff_read_enum_type (int index, int length, int lastsym,
2034 struct objfile *objfile)
2036 struct gdbarch *gdbarch = objfile->arch ();
2037 struct symbol *sym;
2038 struct type *type;
2039 int nsyms = 0;
2040 int done = 0;
2041 struct pending **symlist;
2042 struct coff_symbol member_sym;
2043 struct coff_symbol *ms = &member_sym;
2044 struct internal_syment sub_sym;
2045 union internal_auxent sub_aux;
2046 struct pending *osyms, *syms;
2047 int o_nsyms;
2048 int n;
2049 char *name;
2050 int unsigned_enum = 1;
2052 type = coff_alloc_type (index);
2053 if (within_function)
2054 symlist = get_local_symbols ();
2055 else
2056 symlist = get_file_symbols ();
2057 osyms = *symlist;
2058 o_nsyms = osyms ? osyms->nsyms : 0;
2060 while (!done && symnum < lastsym && symnum < nlist_nsyms_global)
2062 read_one_sym (ms, &sub_sym, &sub_aux);
2063 name = ms->c_name;
2064 name = EXTERNAL_NAME (name, objfile->obfd.get ());
2066 switch (ms->c_sclass)
2068 case C_MOE:
2069 sym = new (&objfile->objfile_obstack) symbol;
2071 name = obstack_strdup (&objfile->objfile_obstack, name);
2072 sym->set_linkage_name (name);
2073 sym->set_aclass_index (LOC_CONST);
2074 sym->set_domain (VAR_DOMAIN);
2075 sym->set_value_longest (ms->c_value);
2076 add_symbol_to_list (sym, symlist);
2077 nsyms++;
2078 break;
2080 case C_EOS:
2081 /* Sometimes the linker (on 386/ix 2.0.2 at least) screws
2082 up the count of how many symbols to read. So stop
2083 on .eos. */
2084 done = 1;
2085 break;
2089 /* Now fill in the fields of the type-structure. */
2091 if (length > 0)
2092 type->set_length (length);
2093 else /* Assume ints. */
2094 type->set_length (gdbarch_int_bit (gdbarch) / TARGET_CHAR_BIT);
2095 type->set_code (TYPE_CODE_ENUM);
2096 type->alloc_fields (nsyms);
2098 /* Find the symbols for the values and put them into the type.
2099 The symbols can be found in the symlist that we put them on
2100 to cause them to be defined. osyms contains the old value
2101 of that symlist; everything up to there was defined by us. */
2102 /* Note that we preserve the order of the enum constants, so
2103 that in something like "enum {FOO, LAST_THING=FOO}" we print
2104 FOO, not LAST_THING. */
2106 for (syms = *symlist, n = 0; syms; syms = syms->next)
2108 int j = 0;
2110 if (syms == osyms)
2111 j = o_nsyms;
2112 for (; j < syms->nsyms; j++, n++)
2114 struct symbol *xsym = syms->symbol[j];
2116 xsym->set_type (type);
2117 type->field (n).set_name (xsym->linkage_name ());
2118 type->field (n).set_loc_enumval (xsym->value_longest ());
2119 if (xsym->value_longest () < 0)
2120 unsigned_enum = 0;
2121 type->field (n).set_bitsize (0);
2123 if (syms == osyms)
2124 break;
2127 if (unsigned_enum)
2128 type->set_is_unsigned (true);
2130 return type;
2133 /* Register our ability to parse symbols for coff BFD files. */
2135 static const struct sym_fns coff_sym_fns =
2137 coff_new_init, /* sym_new_init: init anything gbl to
2138 entire symtab */
2139 coff_symfile_init, /* sym_init: read initial info, setup
2140 for sym_read() */
2141 coff_symfile_read, /* sym_read: read a symbol file into
2142 symtab */
2143 coff_symfile_finish, /* sym_finish: finished with file,
2144 cleanup */
2145 default_symfile_offsets, /* sym_offsets: xlate external to
2146 internal form */
2147 default_symfile_segments, /* sym_segments: Get segment
2148 information from a file */
2149 NULL, /* sym_read_linetable */
2151 default_symfile_relocate, /* sym_relocate: Relocate a debug
2152 section. */
2153 NULL, /* sym_probe_fns */
2156 void _initialize_coffread ();
2157 void
2158 _initialize_coffread ()
2160 add_symtab_fns (bfd_target_coff_flavour, &coff_sym_fns);
2162 coff_register_index
2163 = register_symbol_register_impl (LOC_REGISTER, &coff_register_funcs);