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"
24 #include "breakpoint.h"
27 #include "gdbsupport/gdb_obstack.h"
30 #include "coff/internal.h"
33 #include "buildsym-legacy.h"
34 #include "stabsread.h"
35 #include "complaints.h"
38 #include "dictionary.h"
39 #include "dwarf2/public.h"
40 #include "gdb-stabs.h"
42 #include "coff-pe-read.h"
44 /* The objfile we are currently reading. */
46 static struct objfile
*coffread_objfile
;
48 struct coff_symfile_info
50 file_ptr min_lineno_offset
= 0; /* Where in file lowest line#s are. */
51 file_ptr max_lineno_offset
= 0; /* 1+last byte of line#s in file. */
53 CORE_ADDR textaddr
= 0; /* Addr of .text section. */
54 unsigned int textsize
= 0; /* Size of .text section. */
55 std::vector
<asection
*> *stabsects
; /* .stab sections. */
56 asection
*stabstrsect
= nullptr; /* Section pointer for .stab section. */
57 char *stabstrdata
= nullptr;
60 /* Key for COFF-associated data. */
62 static const registry
<objfile
>::key
<coff_symfile_info
> coff_objfile_data_key
;
64 /* Translate an external name string into a user-visible name. */
65 #define EXTERNAL_NAME(string, abfd) \
66 (*string != '\0' && *string == bfd_get_symbol_leading_char (abfd) \
67 ? string + 1 : string)
69 /* To be an sdb debug type, type must have at least a basic or primary
70 derived type. Using this rather than checking against T_NULL is
71 said to prevent core dumps if we try to operate on Michael Bloom
74 #define SDB_TYPE(type) (BTYPE(type) | (type & N_TMASK))
76 /* Core address of start and end of text of current source file.
77 This comes from a ".text" symbol where x_nlinno > 0. */
79 static CORE_ADDR current_source_start_addr
;
80 static CORE_ADDR current_source_end_addr
;
82 /* The addresses of the symbol table stream and number of symbols
83 of the object file we are reading (as copied into core). */
85 static bfd
*nlist_bfd_global
;
86 static int nlist_nsyms_global
;
89 /* Pointers to scratch storage, used for reading raw symbols and
92 static char *temp_sym
;
93 static char *temp_aux
;
95 /* Local variables that hold the shift and mask values for the
96 COFF file that we are currently reading. These come back to us
97 from BFD, and are referenced by their macro names, as well as
98 internally to the BTYPE, ISPTR, ISFCN, ISARY, ISTAG, and DECREF
99 macros from include/coff/internal.h . */
101 static unsigned local_n_btmask
;
102 static unsigned local_n_btshft
;
103 static unsigned local_n_tmask
;
104 static unsigned local_n_tshift
;
106 #define N_BTMASK local_n_btmask
107 #define N_BTSHFT local_n_btshft
108 #define N_TMASK local_n_tmask
109 #define N_TSHIFT local_n_tshift
111 /* Local variables that hold the sizes in the file of various COFF
112 structures. (We only need to know this to read them from the file
113 -- BFD will then translate the data in them, into `internal_xxx'
114 structs in the right byte order, alignment, etc.) */
116 static unsigned local_linesz
;
117 static unsigned local_symesz
;
118 static unsigned local_auxesz
;
120 /* This is set if this is a PE format file. */
124 /* Chain of typedefs of pointers to empty struct/union types.
125 They are chained through the SYMBOL_VALUE_CHAIN. */
127 static struct symbol
*opaque_type_chain
[HASHSIZE
];
129 /* Simplified internal version of coff symbol table information. */
134 int c_symnum
; /* Symbol number of this entry. */
135 int c_naux
; /* 0 if syment only, 1 if syment +
143 /* Vector of types defined so far, indexed by their type numbers. */
145 static struct type
**type_vector
;
147 /* Number of elements allocated for type_vector currently. */
149 static int type_vector_length
;
151 /* Initial size of type vector. Is realloc'd larger if needed, and
152 realloc'd down to the size actually used, when completed. */
154 #define INITIAL_TYPE_VECTOR_LENGTH 160
156 static char *linetab
= NULL
;
157 static file_ptr linetab_offset
;
158 static file_ptr linetab_size
;
160 static char *stringtab
= NULL
;
161 static long stringtab_length
= 0;
163 extern void stabsread_clear_cache (void);
165 static struct type
*coff_read_struct_type (int, int, int,
168 static struct type
*decode_base_type (struct coff_symbol
*,
170 union internal_auxent
*,
173 static struct type
*decode_type (struct coff_symbol
*, unsigned int,
174 union internal_auxent
*,
177 static struct type
*decode_function_type (struct coff_symbol
*,
179 union internal_auxent
*,
182 static struct type
*coff_read_enum_type (int, int, int,
185 static struct symbol
*process_coff_symbol (struct coff_symbol
*,
186 union internal_auxent
*,
189 static void patch_opaque_types (struct symtab
*);
191 static void enter_linenos (file_ptr
, int, int, struct objfile
*);
193 static int init_lineno (bfd
*, file_ptr
, file_ptr
, gdb::unique_xmalloc_ptr
<char> *);
195 static char *getsymname (struct internal_syment
*);
197 static const char *coff_getfilename (union internal_auxent
*);
199 static int init_stringtab (bfd
*, file_ptr
, gdb::unique_xmalloc_ptr
<char> *);
201 static void read_one_sym (struct coff_symbol
*,
202 struct internal_syment
*,
203 union internal_auxent
*);
205 static void coff_symtab_read (minimal_symbol_reader
&,
206 file_ptr
, unsigned int, struct objfile
*);
208 /* Scan and build partial symbols for an coff symbol file.
209 The coff file has already been processed to get its minimal symbols.
211 This routine is the equivalent of dbx_symfile_init and dbx_symfile_read
214 OBJFILE is the object file we are reading symbols from.
215 ADDR is the address relative to which the symbols are (e.g.
216 the base address of the text segment).
217 TEXTADDR is the address of the text section.
218 TEXTSIZE is the size of the text section.
219 STABSECTS is the list of .stab sections in OBJFILE.
220 STABSTROFFSET and STABSTRSIZE define the location in OBJFILE where the
221 .stabstr section exists.
223 This routine is mostly copied from dbx_symfile_init and dbx_symfile_read,
224 adjusted for coff details. */
227 coffstab_build_psymtabs (struct objfile
*objfile
,
228 CORE_ADDR textaddr
, unsigned int textsize
,
229 const std::vector
<asection
*> &stabsects
,
230 file_ptr stabstroffset
, unsigned int stabstrsize
)
233 bfd
*sym_bfd
= objfile
->obfd
.get ();
234 const char *name
= bfd_get_filename (sym_bfd
);
235 unsigned int stabsize
;
237 /* Allocate struct to keep track of stab reading. */
238 dbx_objfile_data_key
.emplace (objfile
);
239 dbx_symfile_info
*key
= dbx_objfile_data_key
.get (objfile
);
241 DBX_TEXT_ADDR (objfile
) = textaddr
;
242 DBX_TEXT_SIZE (objfile
) = textsize
;
244 #define COFF_STABS_SYMBOL_SIZE 12 /* XXX FIXME XXX */
245 DBX_SYMBOL_SIZE (objfile
) = COFF_STABS_SYMBOL_SIZE
;
246 DBX_STRINGTAB_SIZE (objfile
) = stabstrsize
;
248 if (stabstrsize
> bfd_get_size (sym_bfd
))
249 error (_("ridiculous string table size: %d bytes"), stabstrsize
);
250 DBX_STRINGTAB (objfile
) = (char *)
251 obstack_alloc (&objfile
->objfile_obstack
, stabstrsize
+ 1);
252 OBJSTAT (objfile
, sz_strtab
+= stabstrsize
+ 1);
254 /* Now read in the string table in one big gulp. */
256 val
= bfd_seek (sym_bfd
, stabstroffset
, SEEK_SET
);
258 perror_with_name (name
);
259 val
= bfd_read (DBX_STRINGTAB (objfile
), stabstrsize
, sym_bfd
);
260 if (val
!= stabstrsize
)
261 perror_with_name (name
);
263 stabsread_new_init ();
264 free_header_files ();
265 init_header_files ();
267 key
->ctx
.processing_acc_compilation
= 1;
269 /* In a coff file, we've already installed the minimal symbols that came
270 from the coff (non-stab) symbol table, so always act like an
271 incremental load here. */
272 scoped_restore save_symbuf_sections
273 = make_scoped_restore (&key
->ctx
.symbuf_sections
);
274 if (stabsects
.size () == 1)
276 stabsize
= bfd_section_size (stabsects
[0]);
277 DBX_SYMCOUNT (objfile
) = stabsize
/ DBX_SYMBOL_SIZE (objfile
);
278 DBX_SYMTAB_OFFSET (objfile
) = stabsects
[0]->filepos
;
282 DBX_SYMCOUNT (objfile
) = 0;
283 for (asection
*section
: stabsects
)
285 stabsize
= bfd_section_size (section
);
286 DBX_SYMCOUNT (objfile
) += stabsize
/ DBX_SYMBOL_SIZE (objfile
);
289 DBX_SYMTAB_OFFSET (objfile
) = stabsects
[0]->filepos
;
291 key
->ctx
.sect_idx
= 1;
292 key
->ctx
.symbuf_sections
= &stabsects
;
293 key
->ctx
.symbuf_left
= bfd_section_size (stabsects
[0]);
294 key
->ctx
.symbuf_read
= 0;
297 read_stabs_symtab (objfile
, 0);
300 /* We are called once per section from coff_symfile_read. We
301 need to examine each section we are passed, check to see
302 if it is something we are interested in processing, and
303 if so, stash away some access information for the section.
305 FIXME: The section names should not be hardwired strings (what
306 should they be? I don't think most object file formats have enough
307 section flags to specify what kind of debug section it is
311 coff_locate_sections (bfd
*abfd
, asection
*sectp
, void *csip
)
313 struct coff_symfile_info
*csi
;
316 csi
= (struct coff_symfile_info
*) csip
;
317 name
= bfd_section_name (sectp
);
318 if (strcmp (name
, ".text") == 0)
320 csi
->textaddr
= bfd_section_vma (sectp
);
321 csi
->textsize
+= bfd_section_size (sectp
);
323 else if (startswith (name
, ".text"))
325 csi
->textsize
+= bfd_section_size (sectp
);
327 else if (strcmp (name
, ".stabstr") == 0)
329 csi
->stabstrsect
= sectp
;
331 else if (startswith (name
, ".stab"))
335 /* We can have multiple .stab sections if linked with
337 for (s
= name
+ sizeof ".stab" - 1; *s
!= '\0'; s
++)
341 csi
->stabsects
->push_back (sectp
);
345 /* Return the section_offsets* that CS points to. */
346 static int cs_to_section (struct coff_symbol
*, struct objfile
*);
348 struct coff_find_targ_sec_arg
355 find_targ_sec (bfd
*abfd
, asection
*sect
, void *obj
)
357 struct coff_find_targ_sec_arg
*args
= (struct coff_find_targ_sec_arg
*) obj
;
359 if (sect
->target_index
== args
->targ_index
)
360 *args
->resultp
= sect
;
363 /* Return the bfd_section that CS points to. */
364 static struct bfd_section
*
365 cs_to_bfd_section (struct coff_symbol
*cs
, struct objfile
*objfile
)
367 asection
*sect
= NULL
;
368 struct coff_find_targ_sec_arg args
;
370 args
.targ_index
= cs
->c_secnum
;
371 args
.resultp
= §
;
372 bfd_map_over_sections (objfile
->obfd
.get (), find_targ_sec
, &args
);
376 /* Return the section number (SECT_OFF_*) that CS points to. */
378 cs_to_section (struct coff_symbol
*cs
, struct objfile
*objfile
)
380 asection
*sect
= cs_to_bfd_section (cs
, objfile
);
383 return SECT_OFF_TEXT (objfile
);
384 return gdb_bfd_section_index (objfile
->obfd
.get (), sect
);
387 /* Return the address of the section of a COFF symbol. */
389 static CORE_ADDR
cs_section_address (struct coff_symbol
*, bfd
*);
392 cs_section_address (struct coff_symbol
*cs
, bfd
*abfd
)
394 asection
*sect
= NULL
;
395 struct coff_find_targ_sec_arg args
;
398 args
.targ_index
= cs
->c_secnum
;
399 args
.resultp
= §
;
400 bfd_map_over_sections (abfd
, find_targ_sec
, &args
);
402 addr
= bfd_section_vma (sect
);
406 /* Look up a coff type-number index. Return the address of the slot
407 where the type for that index is stored.
408 The type-number is in INDEX.
410 This can be used for finding the type associated with that index
411 or for associating a new type with the index. */
413 static struct type
**
414 coff_lookup_type (int index
)
416 if (index
>= type_vector_length
)
418 int old_vector_length
= type_vector_length
;
420 type_vector_length
*= 2;
421 if (index
/* is still */ >= type_vector_length
)
422 type_vector_length
= index
* 2;
424 type_vector
= (struct type
**)
425 xrealloc ((char *) type_vector
,
426 type_vector_length
* sizeof (struct type
*));
427 memset (&type_vector
[old_vector_length
], 0,
428 (type_vector_length
- old_vector_length
) * sizeof (struct type
*));
430 return &type_vector
[index
];
433 /* Make sure there is a type allocated for type number index
434 and return the type object.
435 This can create an empty (zeroed) type object. */
438 coff_alloc_type (int index
)
440 struct type
**type_addr
= coff_lookup_type (index
);
441 struct type
*type
= *type_addr
;
443 /* If we are referring to a type not known at all yet,
444 allocate an empty type for it.
445 We will fill it in later if we find out how. */
448 type
= type_allocator (coffread_objfile
, language_c
).new_type ();
454 /* Start a new symtab for a new source file.
455 This is called when a COFF ".file" symbol is seen;
456 it indicates the start of data for one original source file. */
459 coff_start_compunit_symtab (struct objfile
*objfile
, const char *name
)
462 start_compunit_symtab (objfile
,
464 /* We never know the directory name for COFF. */
466 /* The start address is irrelevant, since we call
467 set_last_source_start_addr in coff_end_compunit_symtab. */
469 /* Let buildsym.c deduce the language for this symtab. */
471 record_debugformat ("COFF");
474 /* Save the vital information from when starting to read a file,
475 for use when closing off the current file.
476 NAME is the file name the symbols came from, START_ADDR is the
477 first text address for the file, and SIZE is the number of bytes of
481 complete_symtab (const char *name
, CORE_ADDR start_addr
, unsigned int size
)
483 set_last_source_file (name
);
484 current_source_start_addr
= start_addr
;
485 current_source_end_addr
= start_addr
+ size
;
488 /* Finish the symbol definitions for one main source file, close off
489 all the lexical contexts for that file (creating struct block's for
490 them), then make the struct symtab for that file and put it in the
494 coff_end_compunit_symtab (struct objfile
*objfile
)
496 set_last_source_start_addr (current_source_start_addr
);
498 end_compunit_symtab (current_source_end_addr
);
500 /* Reinitialize for beginning of new file. */
501 set_last_source_file (NULL
);
504 /* The linker sometimes generates some non-function symbols inside
505 functions referencing variables imported from another DLL.
506 Return nonzero if the given symbol corresponds to one of them. */
509 is_import_fixup_symbol (struct coff_symbol
*cs
,
510 enum minimal_symbol_type type
)
512 /* The following is a bit of a heuristic using the characteristics
513 of these fixup symbols, but should work well in practice... */
516 /* Must be a non-static text symbol. */
517 if (type
!= mst_text
)
520 /* Must be a non-function symbol. */
521 if (ISFCN (cs
->c_type
))
524 /* The name must start with "__fu<digits>__". */
525 if (!startswith (cs
->c_name
, "__fu"))
527 if (! isdigit (cs
->c_name
[4]))
529 for (i
= 5; cs
->c_name
[i
] != '\0' && isdigit (cs
->c_name
[i
]); i
++)
530 /* Nothing, just incrementing index past all digits. */;
531 if (cs
->c_name
[i
] != '_' || cs
->c_name
[i
+ 1] != '_')
537 static struct minimal_symbol
*
538 record_minimal_symbol (minimal_symbol_reader
&reader
,
539 struct coff_symbol
*cs
, unrelocated_addr address
,
540 enum minimal_symbol_type type
, int section
,
541 struct objfile
*objfile
)
543 /* We don't want TDESC entry points in the minimal symbol table. */
544 if (cs
->c_name
[0] == '@')
547 if (is_import_fixup_symbol (cs
, type
))
549 /* Because the value of these symbols is within a function code
550 range, these symbols interfere with the symbol-from-address
551 reverse lookup; this manifests itself in backtraces, or any
552 other commands that prints symbolic addresses. Just pretend
553 these symbols do not exist. */
557 return reader
.record_full (cs
->c_name
, true, address
, type
, section
);
560 /* coff_symfile_init ()
561 is the coff-specific initialization routine for reading symbols.
562 It is passed a struct objfile which contains, among other things,
563 the BFD for the file whose symbols are being read, and a slot for
564 a pointer to "private data" which we fill with cookies and other
565 treats for coff_symfile_read ().
567 We will only be called if this is a COFF or COFF-like file. BFD
568 handles figuring out the format of the file, and code in symtab.c
569 uses BFD's determination to vector to us.
571 The ultimate result is a new symtab (or, FIXME, eventually a
575 coff_symfile_init (struct objfile
*objfile
)
577 /* Allocate struct to keep track of the symfile. */
578 coff_objfile_data_key
.emplace (objfile
);
581 /* This function is called for every section; it finds the outer
582 limits of the line table (minimum and maximum file offset) so that
583 the mainline code can read the whole thing for efficiency. */
586 find_linenos (bfd
*abfd
, struct bfd_section
*asect
, void *vpinfo
)
588 struct coff_symfile_info
*info
;
590 file_ptr offset
, maxoff
;
592 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
593 count
= asect
->lineno_count
;
594 /* End of warning. */
598 size
= count
* local_linesz
;
600 info
= (struct coff_symfile_info
*) vpinfo
;
601 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
602 offset
= asect
->line_filepos
;
603 /* End of warning. */
605 if (offset
< info
->min_lineno_offset
|| info
->min_lineno_offset
== 0)
606 info
->min_lineno_offset
= offset
;
608 maxoff
= offset
+ size
;
609 if (maxoff
> info
->max_lineno_offset
)
610 info
->max_lineno_offset
= maxoff
;
614 /* A helper function for coff_symfile_read that reads minimal
615 symbols. It may also read other forms of symbol as well. */
618 coff_read_minsyms (file_ptr symtab_offset
, unsigned int nsyms
,
619 struct objfile
*objfile
)
622 /* If minimal symbols were already read, and if we know we aren't
623 going to read any other kind of symbol here, then we can just
625 if (objfile
->per_bfd
->minsyms_read
&& pe_file
&& nsyms
== 0)
628 minimal_symbol_reader
reader (objfile
);
630 if (pe_file
&& nsyms
== 0)
632 /* We've got no debugging symbols, but it's a portable
633 executable, so try to read the export table. */
634 read_pe_exported_syms (reader
, objfile
);
638 /* Now that the executable file is positioned at symbol table,
639 process it and define symbols accordingly. */
640 coff_symtab_read (reader
, symtab_offset
, nsyms
, objfile
);
643 /* Install any minimal symbols that have been collected as the
644 current minimal symbols for this objfile. */
650 for (minimal_symbol
*msym
: objfile
->msymbols ())
652 const char *name
= msym
->linkage_name ();
654 /* If the minimal symbols whose name are prefixed by "__imp_"
655 or "_imp_", get rid of the prefix, and search the minimal
656 symbol in OBJFILE. Note that 'maintenance print msymbols'
657 shows that type of these "_imp_XXXX" symbols is mst_data. */
658 if (msym
->type () == mst_data
)
660 const char *name1
= NULL
;
662 if (startswith (name
, "_imp_"))
664 else if (startswith (name
, "__imp_"))
669 = bfd_get_symbol_leading_char (objfile
->obfd
.get ());
671 if (lead
!= '\0' && *name1
== lead
)
674 bound_minimal_symbol found
675 = lookup_minimal_symbol (current_program_space
, name1
,
678 /* If found, there are symbols named "_imp_foo" and "foo"
679 respectively in OBJFILE. Set the type of symbol "foo"
680 as 'mst_solib_trampoline'. */
681 if (found
.minsym
!= NULL
682 && found
.minsym
->type () == mst_text
)
683 found
.minsym
->set_type (mst_solib_trampoline
);
690 /* The BFD for this file -- only good while we're actively reading
691 symbols into a psymtab or a symtab. */
693 static bfd
*symfile_bfd
;
695 /* Read a symbol file, after initialization by coff_symfile_init. */
698 coff_symfile_read (struct objfile
*objfile
, symfile_add_flags symfile_flags
)
700 struct coff_symfile_info
*info
;
701 bfd
*abfd
= objfile
->obfd
.get ();
702 coff_data_type
*cdata
= coff_data (abfd
);
703 const char *filename
= bfd_get_filename (abfd
);
705 unsigned int num_symbols
;
706 file_ptr symtab_offset
;
707 file_ptr stringtab_offset
;
708 unsigned int stabstrsize
;
710 info
= coff_objfile_data_key
.get (objfile
);
711 symfile_bfd
= abfd
; /* Kludge for swap routines. */
713 std::vector
<asection
*> stabsects
;
714 scoped_restore restore_stabsects
715 = make_scoped_restore (&info
->stabsects
, &stabsects
);
717 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
718 num_symbols
= bfd_get_symcount (abfd
); /* How many syms */
719 symtab_offset
= cdata
->sym_filepos
; /* Symbol table file offset */
720 stringtab_offset
= symtab_offset
+ /* String table file offset */
721 num_symbols
* cdata
->local_symesz
;
723 /* Set a few file-statics that give us specific information about
724 the particular COFF file format we're reading. */
725 local_n_btmask
= cdata
->local_n_btmask
;
726 local_n_btshft
= cdata
->local_n_btshft
;
727 local_n_tmask
= cdata
->local_n_tmask
;
728 local_n_tshift
= cdata
->local_n_tshift
;
729 local_linesz
= cdata
->local_linesz
;
730 local_symesz
= cdata
->local_symesz
;
731 local_auxesz
= cdata
->local_auxesz
;
733 /* Allocate space for raw symbol and aux entries, based on their
734 space requirements as reported by BFD. */
735 gdb::def_vector
<char> temp_storage (cdata
->local_symesz
736 + cdata
->local_auxesz
);
737 temp_sym
= temp_storage
.data ();
738 temp_aux
= temp_sym
+ cdata
->local_symesz
;
740 /* We need to know whether this is a PE file, because in PE files,
741 unlike standard COFF files, symbol values are stored as offsets
742 from the section address, rather than as absolute addresses.
743 FIXME: We should use BFD to read the symbol table, and thus avoid
746 startswith (bfd_get_target (objfile
->obfd
.get ()), "pe")
747 || startswith (bfd_get_target (objfile
->obfd
.get ()), "epoc-pe");
749 /* End of warning. */
751 info
->min_lineno_offset
= 0;
752 info
->max_lineno_offset
= 0;
754 /* Only read line number information if we have symbols.
756 On Windows NT, some of the system's DLL's have sections with
757 PointerToLinenumbers fields that are non-zero, but point at
758 random places within the image file. (In the case I found,
759 KERNEL32.DLL's .text section has a line number info pointer that
760 points into the middle of the string `lib\\i386\kernel32.dll'.)
762 However, these DLL's also have no symbols. The line number
763 tables are meaningless without symbols. And in fact, GDB never
764 uses the line number information unless there are symbols. So we
765 can avoid spurious error messages (and maybe run a little
766 faster!) by not even reading the line number table unless we have
768 scoped_restore restore_linetab
= make_scoped_restore (&linetab
);
769 gdb::unique_xmalloc_ptr
<char> linetab_storage
;
772 /* Read the line number table, all at once. */
773 bfd_map_over_sections (abfd
, find_linenos
, (void *) info
);
775 val
= init_lineno (abfd
, info
->min_lineno_offset
,
776 info
->max_lineno_offset
- info
->min_lineno_offset
,
779 error (_("\"%s\": error reading line numbers."), filename
);
782 /* Now read the string table, all at once. */
784 scoped_restore restore_stringtab
= make_scoped_restore (&stringtab
);
785 gdb::unique_xmalloc_ptr
<char> stringtab_storage
;
786 val
= init_stringtab (abfd
, stringtab_offset
, &stringtab_storage
);
788 error (_("\"%s\": can't get string table"), filename
);
790 coff_read_minsyms (symtab_offset
, num_symbols
, objfile
);
792 if (!(objfile
->flags
& OBJF_READNEVER
))
793 bfd_map_over_sections (abfd
, coff_locate_sections
, (void *) info
);
795 if (!info
->stabsects
->empty())
797 if (!info
->stabstrsect
)
799 error (_("The debugging information in `%s' is corrupted.\nThe "
800 "file has a `.stabs' section, but no `.stabstr' section."),
804 /* FIXME: dubious. Why can't we use something normal like
805 bfd_get_section_contents? */
806 stabstrsize
= bfd_section_size (info
->stabstrsect
);
808 coffstab_build_psymtabs (objfile
,
809 info
->textaddr
, info
->textsize
,
811 info
->stabstrsect
->filepos
, stabstrsize
);
814 if (dwarf2_initialize_objfile (objfile
))
819 /* Try to add separate debug file if no symbols table found. */
820 else if (!objfile
->has_partial_symbols ()
821 && objfile
->separate_debug_objfile
== NULL
822 && objfile
->separate_debug_objfile_backlink
== NULL
)
824 if (objfile
->find_and_add_separate_symbol_file (symfile_flags
))
825 gdb_assert (objfile
->separate_debug_objfile
!= nullptr);
830 coff_new_init (struct objfile
*ignore
)
834 /* Perform any local cleanups required when we are done with a
835 particular objfile. I.E, we are in the process of discarding all
836 symbol information for an objfile, freeing up all memory held for
837 it, and unlinking the objfile struct from the global list of known
841 coff_symfile_finish (struct objfile
*objfile
)
843 /* Let stabs reader clean up. */
844 stabsread_clear_cache ();
848 /* Given pointers to a symbol table in coff style exec file,
849 analyze them and create struct symtab's describing the symbols.
850 NSYMS is the number of symbols in the symbol table.
851 We read them one at a time using read_one_sym (). */
854 coff_symtab_read (minimal_symbol_reader
&reader
,
855 file_ptr symtab_offset
, unsigned int nsyms
,
856 struct objfile
*objfile
)
858 struct gdbarch
*gdbarch
= objfile
->arch ();
859 struct context_stack
*newobj
= nullptr;
860 struct coff_symbol coff_symbol
;
861 struct coff_symbol
*cs
= &coff_symbol
;
862 static struct internal_syment main_sym
;
863 static union internal_auxent main_aux
;
864 struct coff_symbol fcn_cs_saved
;
865 static struct internal_syment fcn_sym_saved
;
866 static union internal_auxent fcn_aux_saved
;
867 /* A .file is open. */
868 int in_source_file
= 0;
869 int next_file_symnum
= -1;
870 /* Name of the current file. */
871 const char *filestring
= "";
873 int fcn_first_line
= 0;
874 CORE_ADDR fcn_first_line_addr
= 0;
875 int fcn_last_line
= 0;
876 int fcn_start_addr
= 0;
877 long fcn_line_ptr
= 0;
880 struct minimal_symbol
*msym
;
882 scoped_free_pendings free_pending
;
884 /* Position to read the symbol table. */
885 val
= bfd_seek (objfile
->obfd
.get (), symtab_offset
, 0);
887 perror_with_name (objfile_name (objfile
));
889 coffread_objfile
= objfile
;
890 nlist_bfd_global
= objfile
->obfd
.get ();
891 nlist_nsyms_global
= nsyms
;
892 set_last_source_file (NULL
);
893 memset (opaque_type_chain
, 0, sizeof opaque_type_chain
);
895 if (type_vector
) /* Get rid of previous one. */
897 type_vector_length
= INITIAL_TYPE_VECTOR_LENGTH
;
898 type_vector
= XCNEWVEC (struct type
*, type_vector_length
);
900 coff_start_compunit_symtab (objfile
, "");
903 while (symnum
< nsyms
)
905 QUIT
; /* Make this command interruptable. */
907 read_one_sym (cs
, &main_sym
, &main_aux
);
909 if (cs
->c_symnum
== next_file_symnum
&& cs
->c_sclass
!= C_FILE
)
911 if (get_last_source_file ())
912 coff_end_compunit_symtab (objfile
);
914 coff_start_compunit_symtab (objfile
, "_globals_");
915 /* coff_start_compunit_symtab will set the language of this symtab to
916 language_unknown, since such a ``file name'' is not
917 recognized. Override that with the minimal language to
918 allow printing values in this symtab. */
919 get_current_subfile ()->language
= language_minimal
;
920 complete_symtab ("_globals_", 0, 0);
921 /* Done with all files, everything from here on out is
925 /* Special case for file with type declarations only, no
927 if (!get_last_source_file () && SDB_TYPE (cs
->c_type
)
928 && cs
->c_secnum
== N_DEBUG
)
929 complete_symtab (filestring
, 0, 0);
931 /* Typedefs should not be treated as symbol definitions. */
932 if (ISFCN (cs
->c_type
) && cs
->c_sclass
!= C_TPDEF
)
934 /* Record all functions -- external and static -- in
936 int section
= cs_to_section (cs
, objfile
);
938 tmpaddr
= cs
->c_value
;
939 /* Don't record unresolved symbols. */
940 if (!(cs
->c_secnum
<= 0 && cs
->c_value
== 0))
941 record_minimal_symbol (reader
, cs
,
942 unrelocated_addr (tmpaddr
),
943 mst_text
, section
, objfile
);
945 fcn_line_ptr
= main_aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
;
946 fcn_start_addr
= tmpaddr
;
948 fcn_sym_saved
= main_sym
;
949 fcn_aux_saved
= main_aux
;
953 switch (cs
->c_sclass
)
962 complaint (_("Bad n_sclass for symbol %s"),
967 /* c_value field contains symnum of next .file entry in
968 table or symnum of first global after last .file. */
969 next_file_symnum
= cs
->c_value
;
971 filestring
= coff_getfilename (&main_aux
);
975 /* Complete symbol table for last object file
976 containing debugging information. */
977 if (get_last_source_file ())
979 coff_end_compunit_symtab (objfile
);
980 coff_start_compunit_symtab (objfile
, filestring
);
985 /* C_LABEL is used for labels and static functions.
986 Including it here allows gdb to see static functions when
987 no debug info is available. */
989 /* However, labels within a function can make weird
990 backtraces, so filter them out (from phdm@macqel.be). */
997 case C_THUMBSTATFUNC
:
998 if (cs
->c_name
[0] == '.')
1000 if (strcmp (cs
->c_name
, ".text") == 0)
1002 /* FIXME: don't wire in ".text" as section name or
1004 /* Check for in_source_file deals with case of a
1005 file with debugging symbols followed by a later
1006 file with no symbols. */
1008 complete_symtab (filestring
,
1010 + objfile
->text_section_offset ()),
1011 main_aux
.x_scn
.x_scnlen
);
1014 /* Flush rest of '.' symbols. */
1017 else if (!SDB_TYPE (cs
->c_type
)
1018 && cs
->c_name
[0] == 'L'
1019 && (startswith (cs
->c_name
, "LI%")
1020 || startswith (cs
->c_name
, "LF%")
1021 || startswith (cs
->c_name
, "LC%")
1022 || startswith (cs
->c_name
, "LP%")
1023 || startswith (cs
->c_name
, "LPB%")
1024 || startswith (cs
->c_name
, "LBB%")
1025 || startswith (cs
->c_name
, "LBE%")
1026 || startswith (cs
->c_name
, "LPBX%")))
1027 /* At least on a 3b1, gcc generates swbeg and string labels
1028 that look like this. Ignore them. */
1030 /* For static symbols that don't start with '.'... */
1033 case C_THUMBEXTFUNC
:
1036 /* Record it in the minimal symbols regardless of
1037 SDB_TYPE. This parallels what we do for other debug
1038 formats, and probably is needed to make
1039 print_address_symbolic work right without the (now
1040 gone) "set fast-symbolic-addr off" kludge. */
1042 enum minimal_symbol_type ms_type
;
1044 CORE_ADDR offset
= 0;
1046 if (cs
->c_secnum
== N_UNDEF
)
1048 /* This is a common symbol. We used to rely on
1049 the target to tell us whether it knows where
1050 the symbol has been relocated to, but none of
1051 the target implementations actually provided
1052 that operation. So we just ignore the symbol,
1053 the same way we would do if we had a target-side
1054 symbol lookup which returned no match. */
1057 else if (cs
->c_secnum
== N_ABS
)
1059 /* Use the correct minimal symbol type (and don't
1060 relocate) for absolute values. */
1062 sec
= cs_to_section (cs
, objfile
);
1063 tmpaddr
= cs
->c_value
;
1067 asection
*bfd_section
= cs_to_bfd_section (cs
, objfile
);
1069 sec
= cs_to_section (cs
, objfile
);
1070 tmpaddr
= cs
->c_value
;
1071 /* Statics in a PE file also get relocated. */
1072 if (cs
->c_sclass
== C_EXT
1073 || cs
->c_sclass
== C_THUMBEXTFUNC
1074 || cs
->c_sclass
== C_THUMBEXT
1075 || (pe_file
&& (cs
->c_sclass
== C_STAT
)))
1076 offset
= objfile
->section_offsets
[sec
];
1078 if (bfd_section
->flags
& SEC_CODE
)
1081 cs
->c_sclass
== C_EXT
|| cs
->c_sclass
== C_THUMBEXTFUNC
1082 || cs
->c_sclass
== C_THUMBEXT
?
1083 mst_text
: mst_file_text
;
1084 tmpaddr
= gdbarch_addr_bits_remove (gdbarch
, tmpaddr
);
1086 else if (bfd_section
->flags
& SEC_ALLOC
1087 && bfd_section
->flags
& SEC_LOAD
)
1090 cs
->c_sclass
== C_EXT
|| cs
->c_sclass
== C_THUMBEXT
1091 ? mst_data
: mst_file_data
;
1093 else if (bfd_section
->flags
& SEC_ALLOC
)
1096 cs
->c_sclass
== C_EXT
|| cs
->c_sclass
== C_THUMBEXT
1097 ? mst_bss
: mst_file_bss
;
1100 ms_type
= mst_unknown
;
1103 msym
= record_minimal_symbol (reader
, cs
,
1104 unrelocated_addr (tmpaddr
),
1105 ms_type
, sec
, objfile
);
1107 gdbarch_coff_make_msymbol_special (gdbarch
,
1108 cs
->c_sclass
, msym
);
1110 if (SDB_TYPE (cs
->c_type
))
1114 sym
= process_coff_symbol
1115 (cs
, &main_aux
, objfile
);
1116 sym
->set_value_longest (tmpaddr
+ offset
);
1117 sym
->set_section_index (sec
);
1123 if (strcmp (cs
->c_name
, ".bf") == 0)
1125 within_function
= 1;
1127 /* Value contains address of first non-init type
1129 /* main_aux.x_sym.x_misc.x_lnsz.x_lnno
1130 contains line number of '{' }. */
1131 if (cs
->c_naux
!= 1)
1132 complaint (_("`.bf' symbol %d has no aux entry"),
1134 fcn_first_line
= main_aux
.x_sym
.x_misc
.x_lnsz
.x_lnno
;
1135 fcn_first_line_addr
= cs
->c_value
;
1137 /* Might want to check that locals are 0 and
1138 context_stack_depth is zero, and complain if not. */
1141 newobj
= push_context (depth
, fcn_start_addr
);
1142 fcn_cs_saved
.c_name
= getsymname (&fcn_sym_saved
);
1144 process_coff_symbol (&fcn_cs_saved
,
1145 &fcn_aux_saved
, objfile
);
1147 else if (strcmp (cs
->c_name
, ".ef") == 0)
1149 if (!within_function
)
1150 error (_("Bad coff function information."));
1151 /* The value of .ef is the address of epilogue code;
1152 not useful for gdb. */
1153 /* { main_aux.x_sym.x_misc.x_lnsz.x_lnno
1154 contains number of lines to '}' */
1156 if (outermost_context_p ())
1157 { /* We attempted to pop an empty context stack. */
1158 complaint (_("`.ef' symbol without matching `.bf' "
1159 "symbol ignored starting at symnum %d"),
1161 within_function
= 0;
1165 struct context_stack cstk
= pop_context ();
1166 /* Stack must be empty now. */
1167 if (!outermost_context_p () || newobj
== NULL
)
1169 complaint (_("Unmatched .ef symbol(s) ignored "
1170 "starting at symnum %d"),
1172 within_function
= 0;
1175 if (cs
->c_naux
!= 1)
1177 complaint (_("`.ef' symbol %d has no aux entry"),
1179 fcn_last_line
= 0x7FFFFFFF;
1183 fcn_last_line
= main_aux
.x_sym
.x_misc
.x_lnsz
.x_lnno
;
1185 /* fcn_first_line is the line number of the opening '{'.
1186 Do not record it - because it would affect gdb's idea
1187 of the line number of the first statement of the
1188 function - except for one-line functions, for which
1189 it is also the line number of all the statements and
1190 of the closing '}', and for which we do not have any
1191 other statement-line-number. */
1192 if (fcn_last_line
== 1)
1194 (get_current_subfile (), fcn_first_line
,
1195 unrelocated_addr (gdbarch_addr_bits_remove (gdbarch
,
1196 fcn_first_line_addr
)));
1198 enter_linenos (fcn_line_ptr
, fcn_first_line
,
1199 fcn_last_line
, objfile
);
1201 finish_block (cstk
.name
, cstk
.old_blocks
,
1202 NULL
, cstk
.start_addr
,
1203 fcn_cs_saved
.c_value
1204 + fcn_aux_saved
.x_sym
.x_misc
.x_fsize
1205 + objfile
->text_section_offset ());
1206 within_function
= 0;
1211 if (strcmp (cs
->c_name
, ".bb") == 0)
1213 tmpaddr
= cs
->c_value
;
1214 tmpaddr
+= objfile
->text_section_offset ();
1215 push_context (++depth
, tmpaddr
);
1217 else if (strcmp (cs
->c_name
, ".eb") == 0)
1219 if (outermost_context_p ())
1220 { /* We attempted to pop an empty context stack. */
1221 complaint (_("`.eb' symbol without matching `.bb' "
1222 "symbol ignored starting at symnum %d"),
1227 struct context_stack cstk
= pop_context ();
1228 if (depth
-- != cstk
.depth
)
1230 complaint (_("Mismatched .eb symbol ignored "
1231 "starting at symnum %d"),
1235 if (*get_local_symbols () && !outermost_context_p ())
1237 tmpaddr
= cs
->c_value
+ objfile
->text_section_offset ();
1238 /* Make a block for the local symbols within. */
1239 finish_block (0, cstk
.old_blocks
, NULL
,
1240 cstk
.start_addr
, tmpaddr
);
1242 /* Now pop locals of block just finished. */
1243 *get_local_symbols () = cstk
.locals
;
1248 process_coff_symbol (cs
, &main_aux
, objfile
);
1253 if (get_last_source_file ())
1254 coff_end_compunit_symtab (objfile
);
1256 /* Patch up any opaque types (references to types that are not defined
1257 in the file where they are referenced, e.g. "struct foo *bar"). */
1259 for (compunit_symtab
*cu
: objfile
->compunits ())
1261 for (symtab
*s
: cu
->filetabs ())
1262 patch_opaque_types (s
);
1266 coffread_objfile
= NULL
;
1269 /* Routines for reading headers and symbols from executable. */
1271 /* Read the next symbol, swap it, and return it in both
1272 internal_syment form, and coff_symbol form. Also return its first
1273 auxent, if any, in internal_auxent form, and skip any other
1277 read_one_sym (struct coff_symbol
*cs
,
1278 struct internal_syment
*sym
,
1279 union internal_auxent
*aux
)
1282 bfd_size_type bytes
;
1284 cs
->c_symnum
= symnum
;
1285 bytes
= bfd_read (temp_sym
, local_symesz
, nlist_bfd_global
);
1286 if (bytes
!= local_symesz
)
1287 error (_("%s: error reading symbols"), objfile_name (coffread_objfile
));
1288 bfd_coff_swap_sym_in (symfile_bfd
, temp_sym
, (char *) sym
);
1289 cs
->c_naux
= sym
->n_numaux
& 0xff;
1290 if (cs
->c_naux
>= 1)
1292 bytes
= bfd_read (temp_aux
, local_auxesz
, nlist_bfd_global
);
1293 if (bytes
!= local_auxesz
)
1294 error (_("%s: error reading symbols"), objfile_name (coffread_objfile
));
1295 bfd_coff_swap_aux_in (symfile_bfd
, temp_aux
,
1296 sym
->n_type
, sym
->n_sclass
,
1297 0, cs
->c_naux
, (char *) aux
);
1298 /* If more than one aux entry, read past it (only the first aux
1300 for (i
= 1; i
< cs
->c_naux
; i
++)
1302 bytes
= bfd_read (temp_aux
, local_auxesz
, nlist_bfd_global
);
1303 if (bytes
!= local_auxesz
)
1304 error (_("%s: error reading symbols"),
1305 objfile_name (coffread_objfile
));
1308 cs
->c_name
= getsymname (sym
);
1309 cs
->c_value
= sym
->n_value
;
1310 cs
->c_sclass
= (sym
->n_sclass
& 0xff);
1311 cs
->c_secnum
= sym
->n_scnum
;
1312 cs
->c_type
= (unsigned) sym
->n_type
;
1313 if (!SDB_TYPE (cs
->c_type
))
1317 if (cs
->c_sclass
& 128)
1318 printf (_("thumb symbol %s, class 0x%x\n"), cs
->c_name
, cs
->c_sclass
);
1321 symnum
+= 1 + cs
->c_naux
;
1323 /* The PE file format stores symbol values as offsets within the
1324 section, rather than as absolute addresses. We correct that
1325 here, if the symbol has an appropriate storage class. FIXME: We
1326 should use BFD to read the symbols, rather than duplicating the
1330 switch (cs
->c_sclass
)
1334 case C_THUMBEXTFUNC
:
1339 case C_THUMBSTATFUNC
:
1345 if (cs
->c_secnum
!= 0)
1346 cs
->c_value
+= cs_section_address (cs
, symfile_bfd
);
1352 /* Support for string table handling. */
1355 init_stringtab (bfd
*abfd
, file_ptr offset
, gdb::unique_xmalloc_ptr
<char> *storage
)
1359 unsigned char lengthbuf
[4];
1361 /* If the file is stripped, the offset might be zero, indicating no
1362 string table. Just return with `stringtab' set to null. */
1366 if (bfd_seek (abfd
, offset
, 0) < 0)
1369 val
= bfd_read (lengthbuf
, sizeof lengthbuf
, abfd
);
1370 /* If no string table is needed, then the file may end immediately
1371 after the symbols. Just return with `stringtab' set to null. */
1372 if (val
!= sizeof lengthbuf
)
1374 length
= bfd_h_get_32 (symfile_bfd
, lengthbuf
);
1375 if (length
< sizeof lengthbuf
)
1378 storage
->reset ((char *) xmalloc (length
));
1379 stringtab
= storage
->get ();
1380 /* This is in target format (probably not very useful, and not
1381 currently used), not host format. */
1382 memcpy (stringtab
, lengthbuf
, sizeof lengthbuf
);
1383 stringtab_length
= length
;
1384 if (length
== sizeof length
) /* Empty table -- just the count. */
1387 val
= bfd_read (stringtab
+ sizeof lengthbuf
,
1388 length
- sizeof lengthbuf
, abfd
);
1389 if (val
!= length
- sizeof lengthbuf
|| stringtab
[length
- 1] != '\0')
1396 getsymname (struct internal_syment
*symbol_entry
)
1398 static char buffer
[SYMNMLEN
+ 1];
1401 if (symbol_entry
->_n
._n_n
._n_zeroes
== 0)
1403 if (symbol_entry
->_n
._n_n
._n_offset
> stringtab_length
)
1404 error (_("COFF Error: string table offset (%s) outside string table (length %ld)"),
1405 hex_string (symbol_entry
->_n
._n_n
._n_offset
), stringtab_length
);
1406 result
= stringtab
+ symbol_entry
->_n
._n_n
._n_offset
;
1410 strncpy (buffer
, symbol_entry
->_n
._n_name
, SYMNMLEN
);
1411 buffer
[SYMNMLEN
] = '\0';
1417 /* Extract the file name from the aux entry of a C_FILE symbol.
1418 Return only the last component of the name. Result is in static
1419 storage and is only good for temporary use. */
1422 coff_getfilename (union internal_auxent
*aux_entry
)
1424 static char buffer
[BUFSIZ
];
1427 if (aux_entry
->x_file
.x_n
.x_n
.x_zeroes
== 0)
1429 if (strlen (stringtab
+ aux_entry
->x_file
.x_n
.x_n
.x_offset
) >= BUFSIZ
)
1430 internal_error (_("coff file name too long"));
1431 strcpy (buffer
, stringtab
+ aux_entry
->x_file
.x_n
.x_n
.x_offset
);
1435 size_t x_fname_len
= sizeof (aux_entry
->x_file
.x_n
.x_fname
);
1436 strncpy (buffer
, aux_entry
->x_file
.x_n
.x_fname
, x_fname_len
);
1437 buffer
[x_fname_len
] = '\0';
1441 /* FIXME: We should not be throwing away the information about what
1442 directory. It should go into dirname of the symtab, or some such
1444 result
= lbasename (result
);
1448 /* Support for line number handling. */
1450 /* Read in all the line numbers for fast lookups later. Leave them in
1451 external (unswapped) format in memory; we'll swap them as we enter
1452 them into GDB's data structures. */
1455 init_lineno (bfd
*abfd
, file_ptr offset
, file_ptr size
,
1456 gdb::unique_xmalloc_ptr
<char> *storage
)
1460 linetab_offset
= offset
;
1461 linetab_size
= size
;
1466 if (bfd_seek (abfd
, offset
, 0) < 0)
1469 /* Allocate the desired table, plus a sentinel. */
1470 storage
->reset ((char *) xmalloc (size
+ local_linesz
));
1471 linetab
= storage
->get ();
1473 val
= bfd_read (storage
->get (), size
, abfd
);
1477 /* Terminate it with an all-zero sentinel record. */
1478 memset (linetab
+ size
, 0, local_linesz
);
1483 #if !defined (L_LNNO32)
1484 #define L_LNNO32(lp) ((lp)->l_lnno)
1488 enter_linenos (file_ptr file_offset
, int first_line
,
1489 int last_line
, struct objfile
*objfile
)
1491 struct gdbarch
*gdbarch
= objfile
->arch ();
1493 struct internal_lineno lptr
;
1497 if (file_offset
< linetab_offset
)
1499 complaint (_("Line number pointer %s lower than start of line numbers"),
1500 plongest (file_offset
));
1501 if (file_offset
> linetab_size
) /* Too big to be an offset? */
1503 file_offset
+= linetab_offset
; /* Try reading at that linetab
1507 rawptr
= &linetab
[file_offset
- linetab_offset
];
1509 /* Skip first line entry for each function. */
1510 rawptr
+= local_linesz
;
1511 /* Line numbers start at one for the first line of the function. */
1514 /* If the line number table is full (e.g. 64K lines in COFF debug
1515 info), the next function's L_LNNO32 might not be zero, so don't
1516 overstep the table's end in any case. */
1517 while (rawptr
<= &linetab
[0] + linetab_size
)
1519 bfd_coff_swap_lineno_in (symfile_bfd
, rawptr
, &lptr
);
1520 rawptr
+= local_linesz
;
1521 /* The next function, or the sentinel, will have L_LNNO32 zero;
1523 if (L_LNNO32 (&lptr
) && L_LNNO32 (&lptr
) <= last_line
)
1525 CORE_ADDR addr
= lptr
.l_addr
.l_paddr
;
1526 record_line (get_current_subfile (),
1527 first_line
+ L_LNNO32 (&lptr
),
1528 unrelocated_addr (gdbarch_addr_bits_remove (gdbarch
,
1537 patch_type (struct type
*type
, struct type
*real_type
)
1539 struct type
*target
= type
->target_type ();
1540 struct type
*real_target
= real_type
->target_type ();
1542 target
->set_length (real_target
->length ());
1543 target
->copy_fields (real_target
);
1545 if (real_target
->name ())
1547 /* The previous copy of TYPE_NAME is allocated by
1548 process_coff_symbol. */
1549 xfree ((char *) target
->name ());
1550 target
->set_name (xstrdup (real_target
->name ()));
1554 /* Patch up all appropriate typedef symbols in the opaque_type_chains
1555 so that they can be used to print out opaque data structures
1559 patch_opaque_types (struct symtab
*s
)
1561 /* Go through the per-file symbols only. */
1562 const struct block
*b
= s
->compunit ()->blockvector ()->static_block ();
1563 for (struct symbol
*real_sym
: block_iterator_range (b
))
1565 /* Find completed typedefs to use to fix opaque ones.
1566 Remove syms from the chain when their types are stored,
1567 but search the whole chain, as there may be several syms
1568 from different files with the same name. */
1569 if (real_sym
->aclass () == LOC_TYPEDEF
1570 && real_sym
->domain () == TYPE_DOMAIN
1571 && real_sym
->type ()->code () == TYPE_CODE_PTR
1572 && real_sym
->type ()->target_type ()->length () != 0)
1574 const char *name
= real_sym
->linkage_name ();
1575 int hash
= hashname (name
);
1576 struct symbol
*sym
, *prev
;
1579 for (sym
= opaque_type_chain
[hash
]; sym
;)
1581 if (name
[0] == sym
->linkage_name ()[0]
1582 && strcmp (name
+ 1, sym
->linkage_name () + 1) == 0)
1585 prev
->set_value_chain (sym
->value_chain ());
1587 opaque_type_chain
[hash
] = sym
->value_chain ();
1589 patch_type (sym
->type (), real_sym
->type ());
1592 sym
= prev
->value_chain ();
1594 sym
= opaque_type_chain
[hash
];
1599 sym
->set_value_chain (sym
);
1607 coff_reg_to_regnum (struct symbol
*sym
, struct gdbarch
*gdbarch
)
1609 return gdbarch_sdb_reg_to_regnum (gdbarch
, sym
->value_longest ());
1612 static const struct symbol_register_ops coff_register_funcs
= {
1616 /* The "aclass" index for computed COFF symbols. */
1618 static int coff_register_index
;
1620 static struct symbol
*
1621 process_coff_symbol (struct coff_symbol
*cs
,
1622 union internal_auxent
*aux
,
1623 struct objfile
*objfile
)
1625 struct symbol
*sym
= new (&objfile
->objfile_obstack
) symbol
;
1629 name
= EXTERNAL_NAME (name
, objfile
->obfd
.get ());
1630 sym
->set_language (get_current_subfile ()->language
,
1631 &objfile
->objfile_obstack
);
1632 sym
->compute_and_set_names (name
, true, objfile
->per_bfd
);
1634 /* default assumptions */
1635 sym
->set_value_longest (cs
->c_value
);
1636 sym
->set_domain (VAR_DOMAIN
);
1637 sym
->set_section_index (cs_to_section (cs
, objfile
));
1639 if (ISFCN (cs
->c_type
))
1641 sym
->set_domain (FUNCTION_DOMAIN
);
1642 sym
->set_value_longest
1643 (sym
->value_longest () + objfile
->text_section_offset ());
1645 (lookup_function_type (decode_function_type (cs
, cs
->c_type
,
1648 sym
->set_aclass_index (LOC_BLOCK
);
1649 if (cs
->c_sclass
== C_STAT
|| cs
->c_sclass
== C_THUMBSTAT
1650 || cs
->c_sclass
== C_THUMBSTATFUNC
)
1651 add_symbol_to_list (sym
, get_file_symbols ());
1652 else if (cs
->c_sclass
== C_EXT
|| cs
->c_sclass
== C_THUMBEXT
1653 || cs
->c_sclass
== C_THUMBEXTFUNC
)
1654 add_symbol_to_list (sym
, get_global_symbols ());
1658 sym
->set_type (decode_type (cs
, cs
->c_type
, aux
, objfile
));
1659 switch (cs
->c_sclass
)
1665 sym
->set_aclass_index (LOC_LOCAL
);
1666 add_symbol_to_list (sym
, get_local_symbols ());
1670 case C_THUMBEXTFUNC
:
1672 sym
->set_aclass_index (LOC_STATIC
);
1673 sym
->set_value_address ((CORE_ADDR
) cs
->c_value
1674 + objfile
->section_offsets
[SECT_OFF_TEXT (objfile
)]);
1675 add_symbol_to_list (sym
, get_global_symbols ());
1679 case C_THUMBSTATFUNC
:
1681 sym
->set_aclass_index (LOC_STATIC
);
1682 sym
->set_value_address ((CORE_ADDR
) cs
->c_value
1683 + objfile
->section_offsets
[SECT_OFF_TEXT (objfile
)]);
1684 if (within_function
)
1686 /* Static symbol of local scope. */
1687 add_symbol_to_list (sym
, get_local_symbols ());
1691 /* Static symbol at top level of file. */
1692 add_symbol_to_list (sym
, get_file_symbols ());
1696 #ifdef C_GLBLREG /* AMD coff */
1700 sym
->set_aclass_index (coff_register_index
);
1701 sym
->set_value_longest (cs
->c_value
);
1702 add_symbol_to_list (sym
, get_local_symbols ());
1710 sym
->set_aclass_index (LOC_ARG
);
1711 sym
->set_is_argument (1);
1712 add_symbol_to_list (sym
, get_local_symbols ());
1716 sym
->set_aclass_index (coff_register_index
);
1717 sym
->set_is_argument (1);
1718 sym
->set_value_longest (cs
->c_value
);
1719 add_symbol_to_list (sym
, get_local_symbols ());
1723 sym
->set_aclass_index (LOC_TYPEDEF
);
1724 sym
->set_domain (TYPE_DOMAIN
);
1726 /* If type has no name, give it one. */
1727 if (sym
->type ()->name () == 0)
1729 if (sym
->type ()->code () == TYPE_CODE_PTR
1730 || sym
->type ()->code () == TYPE_CODE_FUNC
)
1732 /* If we are giving a name to a type such as
1733 "pointer to foo" or "function returning foo", we
1734 better not set the TYPE_NAME. If the program
1735 contains "typedef char *caddr_t;", we don't want
1736 all variables of type char * to print as caddr_t.
1737 This is not just a consequence of GDB's type
1738 management; CC and GCC (at least through version
1739 2.4) both output variables of either type char *
1740 or caddr_t with the type referring to the C_TPDEF
1741 symbol for caddr_t. If a future compiler cleans
1742 this up it GDB is not ready for it yet, but if it
1743 becomes ready we somehow need to disable this
1744 check (without breaking the PCC/GCC2.4 case).
1748 Fortunately, this check seems not to be necessary
1749 for anything except pointers or functions. */
1753 sym
->type ()->set_name (xstrdup (sym
->linkage_name ()));
1756 /* Keep track of any type which points to empty structured
1757 type, so it can be filled from a definition from another
1758 file. A simple forward reference (TYPE_CODE_UNDEF) is
1759 not an empty structured type, though; the forward
1760 references work themselves out via the magic of
1761 coff_lookup_type. */
1762 if (sym
->type ()->code () == TYPE_CODE_PTR
1763 && sym
->type ()->target_type ()->length () == 0
1764 && sym
->type ()->target_type ()->code ()
1767 int i
= hashname (sym
->linkage_name ());
1769 sym
->set_value_chain (opaque_type_chain
[i
]);
1770 opaque_type_chain
[i
] = sym
;
1772 add_symbol_to_list (sym
, get_file_symbols ());
1778 sym
->set_aclass_index (LOC_TYPEDEF
);
1779 sym
->set_domain (STRUCT_DOMAIN
);
1781 /* Some compilers try to be helpful by inventing "fake"
1782 names for anonymous enums, structures, and unions, like
1783 "~0fake" or ".0fake". Thanks, but no thanks... */
1784 if (sym
->type ()->name () == 0)
1785 if (sym
->linkage_name () != NULL
1786 && *sym
->linkage_name () != '~'
1787 && *sym
->linkage_name () != '.')
1788 sym
->type ()->set_name (xstrdup (sym
->linkage_name ()));
1790 add_symbol_to_list (sym
, get_file_symbols ());
1800 /* Decode a coff type specifier; return the type that is meant. */
1802 static struct type
*
1803 decode_type (struct coff_symbol
*cs
, unsigned int c_type
,
1804 union internal_auxent
*aux
, struct objfile
*objfile
)
1806 struct type
*type
= 0;
1807 unsigned int new_c_type
;
1809 if (c_type
& ~N_BTMASK
)
1811 new_c_type
= DECREF (c_type
);
1814 type
= decode_type (cs
, new_c_type
, aux
, objfile
);
1815 type
= lookup_pointer_type (type
);
1817 else if (ISFCN (c_type
))
1819 type
= decode_type (cs
, new_c_type
, aux
, objfile
);
1820 type
= lookup_function_type (type
);
1822 else if (ISARY (c_type
))
1825 unsigned short *dim
;
1826 struct type
*base_type
, *index_type
, *range_type
;
1828 /* Define an array type. */
1829 /* auxent refers to array, not base type. */
1830 if (aux
->x_sym
.x_tagndx
.u32
== 0)
1833 /* Shift the indices down. */
1834 dim
= &aux
->x_sym
.x_fcnary
.x_ary
.x_dimen
[0];
1837 for (i
= 0; *dim
&& i
< DIMNUM
- 1; i
++, dim
++)
1841 base_type
= decode_type (cs
, new_c_type
, aux
, objfile
);
1842 index_type
= builtin_type (objfile
)->builtin_int
;
1843 type_allocator
alloc (objfile
, language_c
);
1845 = create_static_range_type (alloc
, index_type
, 0, n
- 1);
1846 type
= create_array_type (alloc
, base_type
, range_type
);
1851 /* Reference to existing type. This only occurs with the struct,
1852 union, and enum types. EPI a29k coff fakes us out by producing
1853 aux entries with a nonzero x_tagndx for definitions of structs,
1854 unions, and enums, so we have to check the c_sclass field. SCO
1855 3.2v4 cc gets confused with pointers to pointers to defined
1856 structs, and generates negative x_tagndx fields. */
1857 if (cs
->c_naux
> 0 && aux
->x_sym
.x_tagndx
.u32
!= 0)
1859 if (cs
->c_sclass
!= C_STRTAG
1860 && cs
->c_sclass
!= C_UNTAG
1861 && cs
->c_sclass
!= C_ENTAG
1862 && (int32_t) aux
->x_sym
.x_tagndx
.u32
>= 0)
1864 type
= coff_alloc_type (aux
->x_sym
.x_tagndx
.u32
);
1869 complaint (_("Symbol table entry for %s has bad tagndx value"),
1871 /* And fall through to decode_base_type... */
1875 return decode_base_type (cs
, BTYPE (c_type
), aux
, objfile
);
1878 /* Decode a coff type specifier for function definition;
1879 return the type that the function returns. */
1881 static struct type
*
1882 decode_function_type (struct coff_symbol
*cs
,
1883 unsigned int c_type
,
1884 union internal_auxent
*aux
,
1885 struct objfile
*objfile
)
1887 if (aux
->x_sym
.x_tagndx
.u32
== 0)
1888 cs
->c_naux
= 0; /* auxent refers to function, not base
1891 return decode_type (cs
, DECREF (c_type
), aux
, objfile
);
1894 /* Basic C types. */
1896 static struct type
*
1897 decode_base_type (struct coff_symbol
*cs
,
1898 unsigned int c_type
,
1899 union internal_auxent
*aux
,
1900 struct objfile
*objfile
)
1902 struct gdbarch
*gdbarch
= objfile
->arch ();
1908 /* Shows up with "void (*foo)();" structure members. */
1909 return builtin_type (objfile
)->builtin_void
;
1913 /* Intel 960 COFF has this symbol and meaning. */
1914 return builtin_type (objfile
)->builtin_void
;
1918 return builtin_type (objfile
)->builtin_char
;
1921 return builtin_type (objfile
)->builtin_short
;
1924 return builtin_type (objfile
)->builtin_int
;
1927 if (cs
->c_sclass
== C_FIELD
1928 && aux
->x_sym
.x_misc
.x_lnsz
.x_size
1929 > gdbarch_long_bit (gdbarch
))
1930 return builtin_type (objfile
)->builtin_long_long
;
1932 return builtin_type (objfile
)->builtin_long
;
1935 return builtin_type (objfile
)->builtin_float
;
1938 return builtin_type (objfile
)->builtin_double
;
1941 return builtin_type (objfile
)->builtin_long_double
;
1944 if (cs
->c_naux
!= 1)
1946 /* Anonymous structure type. */
1947 type
= coff_alloc_type (cs
->c_symnum
);
1948 type
->set_code (TYPE_CODE_STRUCT
);
1949 type
->set_name (NULL
);
1950 INIT_CPLUS_SPECIFIC (type
);
1951 type
->set_length (0);
1952 type
->set_fields (nullptr);
1953 type
->set_num_fields (0);
1957 type
= coff_read_struct_type (cs
->c_symnum
,
1958 aux
->x_sym
.x_misc
.x_lnsz
.x_size
,
1959 aux
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.u32
,
1965 if (cs
->c_naux
!= 1)
1967 /* Anonymous union type. */
1968 type
= coff_alloc_type (cs
->c_symnum
);
1969 type
->set_name (NULL
);
1970 INIT_CPLUS_SPECIFIC (type
);
1971 type
->set_length (0);
1972 type
->set_fields (nullptr);
1973 type
->set_num_fields (0);
1977 type
= coff_read_struct_type (cs
->c_symnum
,
1978 aux
->x_sym
.x_misc
.x_lnsz
.x_size
,
1979 aux
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.u32
,
1982 type
->set_code (TYPE_CODE_UNION
);
1986 if (cs
->c_naux
!= 1)
1988 /* Anonymous enum type. */
1989 type
= coff_alloc_type (cs
->c_symnum
);
1990 type
->set_code (TYPE_CODE_ENUM
);
1991 type
->set_name (NULL
);
1992 type
->set_length (0);
1993 type
->set_fields (nullptr);
1994 type
->set_num_fields (0);
1998 type
= coff_read_enum_type (cs
->c_symnum
,
1999 aux
->x_sym
.x_misc
.x_lnsz
.x_size
,
2000 aux
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.u32
,
2006 /* Shouldn't show up here. */
2010 return builtin_type (objfile
)->builtin_unsigned_char
;
2013 return builtin_type (objfile
)->builtin_unsigned_short
;
2016 return builtin_type (objfile
)->builtin_unsigned_int
;
2019 if (cs
->c_sclass
== C_FIELD
2020 && aux
->x_sym
.x_misc
.x_lnsz
.x_size
2021 > gdbarch_long_bit (gdbarch
))
2022 return builtin_type (objfile
)->builtin_unsigned_long_long
;
2024 return builtin_type (objfile
)->builtin_unsigned_long
;
2026 complaint (_("Unexpected type for symbol %s"), cs
->c_name
);
2027 return builtin_type (objfile
)->builtin_void
;
2030 /* This page contains subroutines of read_type. */
2032 /* Read the description of a structure (or union type) and return an
2033 object describing the type. */
2035 static struct type
*
2036 coff_read_struct_type (int index
, int length
, int lastsym
,
2037 struct objfile
*objfile
)
2041 struct nextfield
*next
;
2046 struct nextfield
*list
= 0;
2047 struct nextfield
*newobj
;
2051 struct coff_symbol member_sym
;
2052 struct coff_symbol
*ms
= &member_sym
;
2053 struct internal_syment sub_sym
;
2054 union internal_auxent sub_aux
;
2057 type
= coff_alloc_type (index
);
2058 type
->set_code (TYPE_CODE_STRUCT
);
2059 INIT_CPLUS_SPECIFIC (type
);
2060 type
->set_length (length
);
2062 while (!done
&& symnum
< lastsym
&& symnum
< nlist_nsyms_global
)
2064 read_one_sym (ms
, &sub_sym
, &sub_aux
);
2066 name
= EXTERNAL_NAME (name
, objfile
->obfd
.get ());
2068 switch (ms
->c_sclass
)
2073 /* Get space to record the next field's data. */
2074 newobj
= XALLOCA (struct nextfield
);
2075 newobj
->next
= list
;
2078 /* Save the data. */
2079 list
->field
.set_name (obstack_strdup (&objfile
->objfile_obstack
,
2081 list
->field
.set_type (decode_type (ms
, ms
->c_type
, &sub_aux
,
2083 list
->field
.set_loc_bitpos (8 * ms
->c_value
);
2084 list
->field
.set_bitsize (0);
2090 /* Get space to record the next field's data. */
2091 newobj
= XALLOCA (struct nextfield
);
2092 newobj
->next
= list
;
2095 /* Save the data. */
2096 list
->field
.set_name (obstack_strdup (&objfile
->objfile_obstack
,
2098 list
->field
.set_type (decode_type (ms
, ms
->c_type
, &sub_aux
,
2100 list
->field
.set_loc_bitpos (ms
->c_value
);
2101 list
->field
.set_bitsize (sub_aux
.x_sym
.x_misc
.x_lnsz
.x_size
);
2110 /* Now create the vector of fields, and record how big it is. */
2112 type
->alloc_fields (nfields
);
2114 /* Copy the saved-up fields into the field vector. */
2116 for (n
= nfields
; list
; list
= list
->next
)
2117 type
->field (--n
) = list
->field
;
2122 /* Read a definition of an enumeration type,
2123 and create and return a suitable type object.
2124 Also defines the symbols that represent the values of the type. */
2126 static struct type
*
2127 coff_read_enum_type (int index
, int length
, int lastsym
,
2128 struct objfile
*objfile
)
2130 struct gdbarch
*gdbarch
= objfile
->arch ();
2135 struct pending
**symlist
;
2136 struct coff_symbol member_sym
;
2137 struct coff_symbol
*ms
= &member_sym
;
2138 struct internal_syment sub_sym
;
2139 union internal_auxent sub_aux
;
2140 struct pending
*osyms
, *syms
;
2144 int unsigned_enum
= 1;
2146 type
= coff_alloc_type (index
);
2147 if (within_function
)
2148 symlist
= get_local_symbols ();
2150 symlist
= get_file_symbols ();
2152 o_nsyms
= osyms
? osyms
->nsyms
: 0;
2154 while (!done
&& symnum
< lastsym
&& symnum
< nlist_nsyms_global
)
2156 read_one_sym (ms
, &sub_sym
, &sub_aux
);
2158 name
= EXTERNAL_NAME (name
, objfile
->obfd
.get ());
2160 switch (ms
->c_sclass
)
2163 sym
= new (&objfile
->objfile_obstack
) symbol
;
2165 name
= obstack_strdup (&objfile
->objfile_obstack
, name
);
2166 sym
->set_linkage_name (name
);
2167 sym
->set_aclass_index (LOC_CONST
);
2168 sym
->set_domain (VAR_DOMAIN
);
2169 sym
->set_value_longest (ms
->c_value
);
2170 add_symbol_to_list (sym
, symlist
);
2175 /* Sometimes the linker (on 386/ix 2.0.2 at least) screws
2176 up the count of how many symbols to read. So stop
2183 /* Now fill in the fields of the type-structure. */
2186 type
->set_length (length
);
2187 else /* Assume ints. */
2188 type
->set_length (gdbarch_int_bit (gdbarch
) / TARGET_CHAR_BIT
);
2189 type
->set_code (TYPE_CODE_ENUM
);
2190 type
->alloc_fields (nsyms
);
2192 /* Find the symbols for the values and put them into the type.
2193 The symbols can be found in the symlist that we put them on
2194 to cause them to be defined. osyms contains the old value
2195 of that symlist; everything up to there was defined by us. */
2196 /* Note that we preserve the order of the enum constants, so
2197 that in something like "enum {FOO, LAST_THING=FOO}" we print
2198 FOO, not LAST_THING. */
2200 for (syms
= *symlist
, n
= 0; syms
; syms
= syms
->next
)
2206 for (; j
< syms
->nsyms
; j
++, n
++)
2208 struct symbol
*xsym
= syms
->symbol
[j
];
2210 xsym
->set_type (type
);
2211 type
->field (n
).set_name (xsym
->linkage_name ());
2212 type
->field (n
).set_loc_enumval (xsym
->value_longest ());
2213 if (xsym
->value_longest () < 0)
2215 type
->field (n
).set_bitsize (0);
2222 type
->set_is_unsigned (true);
2227 /* Register our ability to parse symbols for coff BFD files. */
2229 static const struct sym_fns coff_sym_fns
=
2231 coff_new_init
, /* sym_new_init: init anything gbl to
2233 coff_symfile_init
, /* sym_init: read initial info, setup
2235 coff_symfile_read
, /* sym_read: read a symbol file into
2237 coff_symfile_finish
, /* sym_finish: finished with file,
2239 default_symfile_offsets
, /* sym_offsets: xlate external to
2241 default_symfile_segments
, /* sym_segments: Get segment
2242 information from a file */
2243 NULL
, /* sym_read_linetable */
2245 default_symfile_relocate
, /* sym_relocate: Relocate a debug
2247 NULL
, /* sym_probe_fns */
2250 void _initialize_coffread ();
2252 _initialize_coffread ()
2254 add_symtab_fns (bfd_target_coff_flavour
, &coff_sym_fns
);
2257 = register_symbol_register_impl (LOC_REGISTER
, &coff_register_funcs
);