1 /* Read coff symbol tables and convert to internal format, for GDB.
2 Copyright 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
3 1997, 1998, 1999, 2000, 2001, 2002, 2003
4 Free Software Foundation, Inc.
5 Contributed by David D. Johnson, Brown University (ddj@cs.brown.edu).
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
28 #include "breakpoint.h"
31 #include "gdb_obstack.h"
33 #include "gdb_string.h"
36 #include "coff/internal.h" /* Internal format of COFF symbols in BFD */
37 #include "libcoff.h" /* FIXME secret internal data from BFD */
42 #include "gdb-stabs.h"
43 #include "stabsread.h"
44 #include "complaints.h"
46 #include "gdb_assert.h"
48 #include "dictionary.h"
50 #include "coff-pe-read.h"
52 extern void _initialize_coffread (void);
54 struct coff_symfile_info
56 file_ptr min_lineno_offset
; /* Where in file lowest line#s are */
57 file_ptr max_lineno_offset
; /* 1+last byte of line#s in file */
59 CORE_ADDR textaddr
; /* Addr of .text section. */
60 unsigned int textsize
; /* Size of .text section. */
61 struct stab_section_list
*stabsects
; /* .stab sections. */
62 asection
*stabstrsect
; /* Section pointer for .stab section */
66 /* Translate an external name string into a user-visible name. */
67 #define EXTERNAL_NAME(string, abfd) \
68 (string[0] == bfd_get_symbol_leading_char(abfd)? string+1: string)
70 /* To be an sdb debug type, type must have at least a basic or primary
71 derived type. Using this rather than checking against T_NULL is
72 said to prevent core dumps if we try to operate on Michael Bloom
75 #define SDB_TYPE(type) (BTYPE(type) | (type & N_TMASK))
77 /* Core address of start and end of text of current source file.
78 This comes from a ".text" symbol where x_nlinno > 0. */
80 static CORE_ADDR current_source_start_addr
;
81 static CORE_ADDR current_source_end_addr
;
83 /* The addresses of the symbol table stream and number of symbols
84 of the object file we are reading (as copied into core). */
86 static bfd
*nlist_bfd_global
;
87 static int nlist_nsyms_global
;
90 /* Pointers to scratch storage, used for reading raw symbols and auxents. */
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 structures.
112 (We only need to know this to read them from the file -- BFD will then
113 translate the data in them, into `internal_xxx' structs in the right
114 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 thru 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 + auxent, etc */
142 extern void stabsread_clear_cache (void);
144 static struct type
*coff_read_struct_type (int, int, int);
146 static struct type
*decode_base_type (struct coff_symbol
*,
147 unsigned int, union internal_auxent
*);
149 static struct type
*decode_type (struct coff_symbol
*, unsigned int,
150 union internal_auxent
*);
152 static struct type
*decode_function_type (struct coff_symbol
*,
154 union internal_auxent
*);
156 static struct type
*coff_read_enum_type (int, int, int);
158 static struct symbol
*process_coff_symbol (struct coff_symbol
*,
159 union internal_auxent
*,
162 static void patch_opaque_types (struct symtab
*);
164 static void enter_linenos (long, int, int, struct objfile
*);
166 static void free_linetab (void);
168 static void free_linetab_cleanup (void *ignore
);
170 static int init_lineno (bfd
*, long, int);
172 static char *getsymname (struct internal_syment
*);
174 static char *coff_getfilename (union internal_auxent
*);
176 static void free_stringtab (void);
178 static void free_stringtab_cleanup (void *ignore
);
180 static int init_stringtab (bfd
*, long);
182 static void read_one_sym (struct coff_symbol
*,
183 struct internal_syment
*, union internal_auxent
*);
185 static void coff_symtab_read (long, unsigned int, struct objfile
*);
187 /* We are called once per section from coff_symfile_read. We
188 need to examine each section we are passed, check to see
189 if it is something we are interested in processing, and
190 if so, stash away some access information for the section.
192 FIXME: The section names should not be hardwired strings (what
193 should they be? I don't think most object file formats have enough
194 section flags to specify what kind of debug section it is
198 coff_locate_sections (bfd
*abfd
, asection
*sectp
, void *csip
)
200 struct coff_symfile_info
*csi
;
203 csi
= (struct coff_symfile_info
*) csip
;
204 name
= bfd_get_section_name (abfd
, sectp
);
205 if (STREQ (name
, ".text"))
207 csi
->textaddr
= bfd_section_vma (abfd
, sectp
);
208 csi
->textsize
+= bfd_section_size (abfd
, sectp
);
210 else if (strncmp (name
, ".text", sizeof ".text" - 1) == 0)
212 csi
->textsize
+= bfd_section_size (abfd
, sectp
);
214 else if (STREQ (name
, ".stabstr"))
216 csi
->stabstrsect
= sectp
;
218 else if (strncmp (name
, ".stab", sizeof ".stab" - 1) == 0)
222 /* We can have multiple .stab sections if linked with
224 for (s
= name
+ sizeof ".stab" - 1; *s
!= '\0'; s
++)
229 struct stab_section_list
*n
, **pn
;
231 n
= ((struct stab_section_list
*)
232 xmalloc (sizeof (struct stab_section_list
)));
235 for (pn
= &csi
->stabsects
; *pn
!= NULL
; pn
= &(*pn
)->next
)
239 /* This will be run after coffstab_build_psymtabs is called
240 in coff_symfile_read, at which point we no longer need
242 make_cleanup (xfree
, n
);
247 /* Return the section_offsets* that CS points to. */
248 static int cs_to_section (struct coff_symbol
*, struct objfile
*);
250 struct find_targ_sec_arg
257 find_targ_sec (bfd
*abfd
, asection
*sect
, void *obj
)
259 struct find_targ_sec_arg
*args
= (struct find_targ_sec_arg
*) obj
;
260 if (sect
->target_index
== args
->targ_index
)
261 *args
->resultp
= sect
;
264 /* Return the section number (SECT_OFF_*) that CS points to. */
266 cs_to_section (struct coff_symbol
*cs
, struct objfile
*objfile
)
268 asection
*sect
= NULL
;
269 struct find_targ_sec_arg args
;
270 int off
= SECT_OFF_TEXT (objfile
);
272 args
.targ_index
= cs
->c_secnum
;
273 args
.resultp
= §
;
274 bfd_map_over_sections (objfile
->obfd
, find_targ_sec
, &args
);
277 /* This is the section. Figure out what SECT_OFF_* code it is. */
278 if (bfd_get_section_flags (abfd
, sect
) & SEC_CODE
)
279 off
= SECT_OFF_TEXT (objfile
);
280 else if (bfd_get_section_flags (abfd
, sect
) & SEC_LOAD
)
281 off
= SECT_OFF_DATA (objfile
);
283 /* Just return the bfd section index. */
289 /* Return the address of the section of a COFF symbol. */
291 static CORE_ADDR
cs_section_address (struct coff_symbol
*, bfd
*);
294 cs_section_address (struct coff_symbol
*cs
, bfd
*abfd
)
296 asection
*sect
= NULL
;
297 struct find_targ_sec_arg args
;
300 args
.targ_index
= cs
->c_secnum
;
301 args
.resultp
= §
;
302 bfd_map_over_sections (abfd
, find_targ_sec
, &args
);
304 addr
= bfd_get_section_vma (objfile
->obfd
, sect
);
308 /* Look up a coff type-number index. Return the address of the slot
309 where the type for that index is stored.
310 The type-number is in INDEX.
312 This can be used for finding the type associated with that index
313 or for associating a new type with the index. */
315 static struct type
**
316 coff_lookup_type (int index
)
318 if (index
>= type_vector_length
)
320 int old_vector_length
= type_vector_length
;
322 type_vector_length
*= 2;
323 if (index
/* is still */ >= type_vector_length
)
324 type_vector_length
= index
* 2;
326 type_vector
= (struct type
**)
327 xrealloc ((char *) type_vector
,
328 type_vector_length
* sizeof (struct type
*));
329 memset (&type_vector
[old_vector_length
], 0,
330 (type_vector_length
- old_vector_length
) * sizeof (struct type
*));
332 return &type_vector
[index
];
335 /* Make sure there is a type allocated for type number index
336 and return the type object.
337 This can create an empty (zeroed) type object. */
340 coff_alloc_type (int index
)
342 struct type
**type_addr
= coff_lookup_type (index
);
343 struct type
*type
= *type_addr
;
345 /* If we are referring to a type not known at all yet,
346 allocate an empty type for it.
347 We will fill it in later if we find out how. */
350 type
= alloc_type (current_objfile
);
356 /* Start a new symtab for a new source file.
357 This is called when a COFF ".file" symbol is seen;
358 it indicates the start of data for one original source file. */
361 coff_start_symtab (char *name
)
364 /* We fill in the filename later. start_symtab puts
365 this pointer into last_source_file and we put it in
366 subfiles->name, which end_symtab frees; that's why
367 it must be malloc'd. */
368 savestring (name
, strlen (name
)),
369 /* We never know the directory name for COFF. */
371 /* The start address is irrelevant, since we set
372 last_source_start_addr in coff_end_symtab. */
374 record_debugformat ("COFF");
377 /* Save the vital information from when starting to read a file,
378 for use when closing off the current file.
379 NAME is the file name the symbols came from, START_ADDR is the first
380 text address for the file, and SIZE is the number of bytes of text. */
383 complete_symtab (char *name
, CORE_ADDR start_addr
, unsigned int size
)
385 if (last_source_file
!= NULL
)
386 xfree (last_source_file
);
387 last_source_file
= savestring (name
, strlen (name
));
388 current_source_start_addr
= start_addr
;
389 current_source_end_addr
= start_addr
+ size
;
391 if (current_objfile
->ei
.entry_point
>= current_source_start_addr
&&
392 current_objfile
->ei
.entry_point
< current_source_end_addr
)
394 current_objfile
->ei
.deprecated_entry_file_lowpc
= current_source_start_addr
;
395 current_objfile
->ei
.deprecated_entry_file_highpc
= current_source_end_addr
;
399 /* Finish the symbol definitions for one main source file,
400 close off all the lexical contexts for that file
401 (creating struct block's for them), then make the
402 struct symtab for that file and put it in the list of all such. */
405 coff_end_symtab (struct objfile
*objfile
)
407 struct symtab
*symtab
;
409 last_source_start_addr
= current_source_start_addr
;
411 symtab
= end_symtab (current_source_end_addr
, objfile
, SECT_OFF_TEXT (objfile
));
414 free_named_symtabs (symtab
->filename
);
416 /* Reinitialize for beginning of new file. */
417 last_source_file
= NULL
;
421 record_minimal_symbol (char *name
, CORE_ADDR address
,
422 enum minimal_symbol_type type
, struct objfile
*objfile
)
424 /* We don't want TDESC entry points in the minimal symbol table */
428 prim_record_minimal_symbol (name
, address
, type
, objfile
);
431 /* coff_symfile_init ()
432 is the coff-specific initialization routine for reading symbols.
433 It is passed a struct objfile which contains, among other things,
434 the BFD for the file whose symbols are being read, and a slot for
435 a pointer to "private data" which we fill with cookies and other
436 treats for coff_symfile_read ().
438 We will only be called if this is a COFF or COFF-like file.
439 BFD handles figuring out the format of the file, and code in symtab.c
440 uses BFD's determination to vector to us.
442 The ultimate result is a new symtab (or, FIXME, eventually a psymtab). */
445 coff_symfile_init (struct objfile
*objfile
)
447 /* Allocate struct to keep track of stab reading. */
448 objfile
->sym_stab_info
= (struct dbx_symfile_info
*)
449 xmmalloc (objfile
->md
, sizeof (struct dbx_symfile_info
));
451 memset (objfile
->sym_stab_info
, 0,
452 sizeof (struct dbx_symfile_info
));
454 /* Allocate struct to keep track of the symfile */
455 objfile
->sym_private
= xmmalloc (objfile
->md
,
456 sizeof (struct coff_symfile_info
));
458 memset (objfile
->sym_private
, 0, sizeof (struct coff_symfile_info
));
460 /* COFF objects may be reordered, so set OBJF_REORDERED. If we
461 find this causes a significant slowdown in gdb then we could
462 set it in the debug symbol readers only when necessary. */
463 objfile
->flags
|= OBJF_REORDERED
;
465 init_entry_point_info (objfile
);
468 /* This function is called for every section; it finds the outer limits
469 of the line table (minimum and maximum file offset) so that the
470 mainline code can read the whole thing for efficiency. */
474 find_linenos (bfd
*abfd
, sec_ptr asect
, void *vpinfo
)
476 struct coff_symfile_info
*info
;
478 file_ptr offset
, maxoff
;
480 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
481 count
= asect
->lineno_count
;
486 size
= count
* local_linesz
;
488 info
= (struct coff_symfile_info
*) vpinfo
;
489 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
490 offset
= asect
->line_filepos
;
493 if (offset
< info
->min_lineno_offset
|| info
->min_lineno_offset
== 0)
494 info
->min_lineno_offset
= offset
;
496 maxoff
= offset
+ size
;
497 if (maxoff
> info
->max_lineno_offset
)
498 info
->max_lineno_offset
= maxoff
;
502 /* The BFD for this file -- only good while we're actively reading
503 symbols into a psymtab or a symtab. */
505 static bfd
*symfile_bfd
;
507 /* Read a symbol file, after initialization by coff_symfile_init. */
511 coff_symfile_read (struct objfile
*objfile
, int mainline
)
513 struct coff_symfile_info
*info
;
514 struct dbx_symfile_info
*dbxinfo
;
515 bfd
*abfd
= objfile
->obfd
;
516 coff_data_type
*cdata
= coff_data (abfd
);
517 char *name
= bfd_get_filename (abfd
);
519 unsigned int num_symbols
;
521 int stringtab_offset
;
522 struct cleanup
*back_to
, *cleanup_minimal_symbols
;
527 info
= (struct coff_symfile_info
*) objfile
->sym_private
;
528 dbxinfo
= objfile
->sym_stab_info
;
529 symfile_bfd
= abfd
; /* Kludge for swap routines */
531 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
532 num_symbols
= bfd_get_symcount (abfd
); /* How many syms */
533 symtab_offset
= cdata
->sym_filepos
; /* Symbol table file offset */
534 stringtab_offset
= symtab_offset
+ /* String table file offset */
535 num_symbols
* cdata
->local_symesz
;
537 /* Set a few file-statics that give us specific information about
538 the particular COFF file format we're reading. */
539 local_n_btmask
= cdata
->local_n_btmask
;
540 local_n_btshft
= cdata
->local_n_btshft
;
541 local_n_tmask
= cdata
->local_n_tmask
;
542 local_n_tshift
= cdata
->local_n_tshift
;
543 local_linesz
= cdata
->local_linesz
;
544 local_symesz
= cdata
->local_symesz
;
545 local_auxesz
= cdata
->local_auxesz
;
547 /* Allocate space for raw symbol and aux entries, based on their
548 space requirements as reported by BFD. */
549 temp_sym
= (char *) xmalloc
550 (cdata
->local_symesz
+ cdata
->local_auxesz
);
551 temp_aux
= temp_sym
+ cdata
->local_symesz
;
552 back_to
= make_cleanup (free_current_contents
, &temp_sym
);
554 /* We need to know whether this is a PE file, because in PE files,
555 unlike standard COFF files, symbol values are stored as offsets
556 from the section address, rather than as absolute addresses.
557 FIXME: We should use BFD to read the symbol table, and thus avoid
560 strncmp (bfd_get_target (objfile
->obfd
), "pe", 2) == 0
561 || strncmp (bfd_get_target (objfile
->obfd
), "epoc-pe", 7) == 0;
565 info
->min_lineno_offset
= 0;
566 info
->max_lineno_offset
= 0;
568 /* Only read line number information if we have symbols.
570 On Windows NT, some of the system's DLL's have sections with
571 PointerToLinenumbers fields that are non-zero, but point at
572 random places within the image file. (In the case I found,
573 KERNEL32.DLL's .text section has a line number info pointer that
574 points into the middle of the string `lib\\i386\kernel32.dll'.)
576 However, these DLL's also have no symbols. The line number
577 tables are meaningless without symbols. And in fact, GDB never
578 uses the line number information unless there are symbols. So we
579 can avoid spurious error messages (and maybe run a little
580 faster!) by not even reading the line number table unless we have
584 /* Read the line number table, all at once. */
585 bfd_map_over_sections (abfd
, find_linenos
, (void *) info
);
587 make_cleanup (free_linetab_cleanup
, 0 /*ignore*/);
588 val
= init_lineno (abfd
, info
->min_lineno_offset
,
589 info
->max_lineno_offset
- info
->min_lineno_offset
);
591 error ("\"%s\": error reading line numbers\n", name
);
594 /* Now read the string table, all at once. */
596 make_cleanup (free_stringtab_cleanup
, 0 /*ignore*/);
597 val
= init_stringtab (abfd
, stringtab_offset
);
599 error ("\"%s\": can't get string table", name
);
601 init_minimal_symbol_collection ();
602 cleanup_minimal_symbols
= make_cleanup_discard_minimal_symbols ();
604 /* Now that the executable file is positioned at symbol table,
605 process it and define symbols accordingly. */
607 coff_symtab_read ((long) symtab_offset
, num_symbols
, objfile
);
609 /* Install any minimal symbols that have been collected as the current
610 minimal symbols for this objfile. */
612 install_minimal_symbols (objfile
);
614 /* Free the installed minimal symbol data. */
615 do_cleanups (cleanup_minimal_symbols
);
617 bfd_map_over_sections (abfd
, coff_locate_sections
, (void *) info
);
621 if (!info
->stabstrsect
)
623 error (("The debugging information in `%s' is corrupted.\n"
624 "The file has a `.stabs' section, but no `.stabstr' "
629 /* FIXME: dubious. Why can't we use something normal like
630 bfd_get_section_contents? */
631 bfd_seek (abfd
, abfd
->where
, 0);
633 stabstrsize
= bfd_section_size (abfd
, info
->stabstrsect
);
635 coffstab_build_psymtabs (objfile
,
637 info
->textaddr
, info
->textsize
,
639 info
->stabstrsect
->filepos
, stabstrsize
);
641 if (dwarf2_has_info (abfd
))
643 /* DWARF2 sections. */
644 dwarf2_build_psymtabs (objfile
, mainline
);
647 do_cleanups (back_to
);
651 coff_new_init (struct objfile
*ignore
)
655 /* Perform any local cleanups required when we are done with a particular
656 objfile. I.E, we are in the process of discarding all symbol information
657 for an objfile, freeing up all memory held for it, and unlinking the
658 objfile struct from the global list of known objfiles. */
661 coff_symfile_finish (struct objfile
*objfile
)
663 if (objfile
->sym_private
!= NULL
)
665 xmfree (objfile
->md
, objfile
->sym_private
);
668 /* Let stabs reader clean up */
669 stabsread_clear_cache ();
673 /* Given pointers to a symbol table in coff style exec file,
674 analyze them and create struct symtab's describing the symbols.
675 NSYMS is the number of symbols in the symbol table.
676 We read them one at a time using read_one_sym (). */
679 coff_symtab_read (long symtab_offset
, unsigned int nsyms
,
680 struct objfile
*objfile
)
682 struct context_stack
*new;
683 struct coff_symbol coff_symbol
;
684 struct coff_symbol
*cs
= &coff_symbol
;
685 static struct internal_syment main_sym
;
686 static union internal_auxent main_aux
;
687 struct coff_symbol fcn_cs_saved
;
688 static struct internal_syment fcn_sym_saved
;
689 static union internal_auxent fcn_aux_saved
;
691 /* A .file is open. */
692 int in_source_file
= 0;
693 int next_file_symnum
= -1;
694 /* Name of the current file. */
695 char *filestring
= "";
697 int fcn_first_line
= 0;
698 CORE_ADDR fcn_first_line_addr
= 0;
699 int fcn_last_line
= 0;
700 int fcn_start_addr
= 0;
701 long fcn_line_ptr
= 0;
705 /* Work around a stdio bug in SunOS4.1.1 (this makes me nervous....
706 it's hard to know I've really worked around it. The fix should be
707 harmless, anyway). The symptom of the bug is that the first
708 fread (in read_one_sym), will (in my example) actually get data
709 from file offset 268, when the fseek was to 264 (and ftell shows
710 264). This causes all hell to break loose. I was unable to
711 reproduce this on a short test program which operated on the same
712 file, performing (I think) the same sequence of operations.
714 It stopped happening when I put in this (former) rewind().
716 FIXME: Find out if this has been reported to Sun, whether it has
717 been fixed in a later release, etc. */
719 bfd_seek (objfile
->obfd
, 0, 0);
721 /* Position to read the symbol table. */
722 val
= bfd_seek (objfile
->obfd
, (long) symtab_offset
, 0);
724 perror_with_name (objfile
->name
);
726 current_objfile
= objfile
;
727 nlist_bfd_global
= objfile
->obfd
;
728 nlist_nsyms_global
= nsyms
;
729 last_source_file
= NULL
;
730 memset (opaque_type_chain
, 0, sizeof opaque_type_chain
);
732 if (type_vector
) /* Get rid of previous one */
734 type_vector_length
= 160;
735 type_vector
= (struct type
**)
736 xmalloc (type_vector_length
* sizeof (struct type
*));
737 memset (type_vector
, 0, type_vector_length
* sizeof (struct type
*));
739 coff_start_symtab ("");
742 while (symnum
< nsyms
)
744 QUIT
; /* Make this command interruptable. */
746 read_one_sym (cs
, &main_sym
, &main_aux
);
748 if (cs
->c_symnum
== next_file_symnum
&& cs
->c_sclass
!= C_FILE
)
750 if (last_source_file
)
751 coff_end_symtab (objfile
);
753 coff_start_symtab ("_globals_");
754 complete_symtab ("_globals_", 0, 0);
755 /* done with all files, everything from here on out is globals */
758 /* Special case for file with type declarations only, no text. */
759 if (!last_source_file
&& SDB_TYPE (cs
->c_type
)
760 && cs
->c_secnum
== N_DEBUG
)
761 complete_symtab (filestring
, 0, 0);
763 /* Typedefs should not be treated as symbol definitions. */
764 if (ISFCN (cs
->c_type
) && cs
->c_sclass
!= C_TPDEF
)
766 /* Record all functions -- external and static -- in minsyms. */
767 tmpaddr
= cs
->c_value
+ ANOFFSET (objfile
->section_offsets
, SECT_OFF_TEXT (objfile
));
768 record_minimal_symbol (cs
->c_name
, tmpaddr
, mst_text
, objfile
);
770 fcn_line_ptr
= main_aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
;
771 fcn_start_addr
= tmpaddr
;
773 fcn_sym_saved
= main_sym
;
774 fcn_aux_saved
= main_aux
;
778 switch (cs
->c_sclass
)
787 complaint (&symfile_complaints
, "Bad n_sclass for symbol %s",
792 /* c_value field contains symnum of next .file entry in table
793 or symnum of first global after last .file. */
794 next_file_symnum
= cs
->c_value
;
796 filestring
= coff_getfilename (&main_aux
);
800 /* Complete symbol table for last object file
801 containing debugging information. */
802 if (last_source_file
)
804 coff_end_symtab (objfile
);
805 coff_start_symtab (filestring
);
810 /* C_LABEL is used for labels and static functions. Including
811 it here allows gdb to see static functions when no debug
812 info is available. */
814 /* However, labels within a function can make weird backtraces,
815 so filter them out (from phdm@macqel.be). */
821 case C_THUMBSTATFUNC
:
822 if (cs
->c_name
[0] == '.')
824 if (STREQ (cs
->c_name
, ".text"))
826 /* FIXME: don't wire in ".text" as section name
828 /* Check for in_source_file deals with case of
829 a file with debugging symbols
830 followed by a later file with no symbols. */
832 complete_symtab (filestring
,
833 cs
->c_value
+ ANOFFSET (objfile
->section_offsets
, SECT_OFF_TEXT (objfile
)),
834 main_aux
.x_scn
.x_scnlen
);
837 /* flush rest of '.' symbols */
840 else if (!SDB_TYPE (cs
->c_type
)
841 && cs
->c_name
[0] == 'L'
842 && (strncmp (cs
->c_name
, "LI%", 3) == 0
843 || strncmp (cs
->c_name
, "LF%", 3) == 0
844 || strncmp (cs
->c_name
, "LC%", 3) == 0
845 || strncmp (cs
->c_name
, "LP%", 3) == 0
846 || strncmp (cs
->c_name
, "LPB%", 4) == 0
847 || strncmp (cs
->c_name
, "LBB%", 4) == 0
848 || strncmp (cs
->c_name
, "LBE%", 4) == 0
849 || strncmp (cs
->c_name
, "LPBX%", 5) == 0))
850 /* At least on a 3b1, gcc generates swbeg and string labels
851 that look like this. Ignore them. */
853 /* fall in for static symbols that don't start with '.' */
858 /* Record it in the minimal symbols regardless of
859 SDB_TYPE. This parallels what we do for other debug
860 formats, and probably is needed to make
861 print_address_symbolic work right without the (now
862 gone) "set fast-symbolic-addr off" kludge. */
864 enum minimal_symbol_type ms_type
;
867 if (cs
->c_secnum
== N_UNDEF
)
869 /* This is a common symbol. See if the target
870 environment knows where it has been relocated to. */
872 if (target_lookup_symbol (cs
->c_name
, &reladdr
))
874 /* Error in lookup; ignore symbol. */
878 /* The address has already been relocated; make sure that
879 objfile_relocate doesn't relocate it again. */
881 ms_type
= cs
->c_sclass
== C_EXT
882 || cs
->c_sclass
== C_THUMBEXT
?
883 mst_bss
: mst_file_bss
;
885 else if (cs
->c_secnum
== N_ABS
)
887 /* Use the correct minimal symbol type (and don't
888 relocate) for absolute values. */
890 sec
= cs_to_section (cs
, objfile
);
891 tmpaddr
= cs
->c_value
;
895 sec
= cs_to_section (cs
, objfile
);
896 tmpaddr
= cs
->c_value
;
897 /* Statics in a PE file also get relocated */
898 if (cs
->c_sclass
== C_EXT
899 || cs
->c_sclass
== C_THUMBEXTFUNC
900 || cs
->c_sclass
== C_THUMBEXT
901 || (pe_file
&& (cs
->c_sclass
== C_STAT
)))
902 tmpaddr
+= ANOFFSET (objfile
->section_offsets
, sec
);
904 if (sec
== SECT_OFF_TEXT (objfile
))
907 cs
->c_sclass
== C_EXT
|| cs
->c_sclass
== C_THUMBEXTFUNC
908 || cs
->c_sclass
== C_THUMBEXT
?
909 mst_text
: mst_file_text
;
910 tmpaddr
= SMASH_TEXT_ADDRESS (tmpaddr
);
912 else if (sec
== SECT_OFF_DATA (objfile
))
915 cs
->c_sclass
== C_EXT
|| cs
->c_sclass
== C_THUMBEXT
?
916 mst_data
: mst_file_data
;
918 else if (sec
== SECT_OFF_BSS (objfile
))
921 cs
->c_sclass
== C_EXT
|| cs
->c_sclass
== C_THUMBEXT
?
922 mst_data
: mst_file_data
;
925 ms_type
= mst_unknown
;
928 if (cs
->c_name
[0] != '@' /* Skip tdesc symbols */ )
930 struct minimal_symbol
*msym
;
932 /* FIXME: cagney/2001-02-01: The nasty (int) -> (long)
933 -> (void*) cast is to ensure that that the value of
934 cs->c_sclass can be correctly stored in a void
935 pointer in MSYMBOL_INFO. Better solutions
937 gdb_assert (sizeof (void *) >= sizeof (cs
->c_sclass
));
938 msym
= prim_record_minimal_symbol_and_info
939 (cs
->c_name
, tmpaddr
, ms_type
, (void *) (long) cs
->c_sclass
,
942 COFF_MAKE_MSYMBOL_SPECIAL (cs
->c_sclass
, msym
);
944 if (SDB_TYPE (cs
->c_type
))
947 sym
= process_coff_symbol
948 (cs
, &main_aux
, objfile
);
949 SYMBOL_VALUE (sym
) = tmpaddr
;
950 SYMBOL_SECTION (sym
) = sec
;
956 if (STREQ (cs
->c_name
, ".bf"))
960 /* value contains address of first non-init type code */
961 /* main_aux.x_sym.x_misc.x_lnsz.x_lnno
962 contains line number of '{' } */
964 complaint (&symfile_complaints
,
965 "`.bf' symbol %d has no aux entry", cs
->c_symnum
);
966 fcn_first_line
= main_aux
.x_sym
.x_misc
.x_lnsz
.x_lnno
;
967 fcn_first_line_addr
= cs
->c_value
;
969 /* Might want to check that locals are 0 and
970 context_stack_depth is zero, and complain if not. */
973 new = push_context (depth
, fcn_start_addr
);
974 fcn_cs_saved
.c_name
= getsymname (&fcn_sym_saved
);
976 process_coff_symbol (&fcn_cs_saved
, &fcn_aux_saved
, objfile
);
978 else if (STREQ (cs
->c_name
, ".ef"))
980 if (!within_function
)
981 error ("Bad coff function information\n");
982 /* the value of .ef is the address of epilogue code;
983 not useful for gdb. */
984 /* { main_aux.x_sym.x_misc.x_lnsz.x_lnno
985 contains number of lines to '}' */
987 if (context_stack_depth
<= 0)
988 { /* We attempted to pop an empty context stack */
989 complaint (&symfile_complaints
,
990 "`.ef' symbol without matching `.bf' symbol ignored starting at symnum %d",
996 new = pop_context ();
997 /* Stack must be empty now. */
998 if (context_stack_depth
> 0 || new == NULL
)
1000 complaint (&symfile_complaints
,
1001 "Unmatched .ef symbol(s) ignored starting at symnum %d",
1003 within_function
= 0;
1006 if (cs
->c_naux
!= 1)
1008 complaint (&symfile_complaints
,
1009 "`.ef' symbol %d has no aux entry", cs
->c_symnum
);
1010 fcn_last_line
= 0x7FFFFFFF;
1014 fcn_last_line
= main_aux
.x_sym
.x_misc
.x_lnsz
.x_lnno
;
1016 /* fcn_first_line is the line number of the opening '{'.
1017 Do not record it - because it would affect gdb's idea
1018 of the line number of the first statement of the function -
1019 except for one-line functions, for which it is also the line
1020 number of all the statements and of the closing '}', and
1021 for which we do not have any other statement-line-number. */
1022 if (fcn_last_line
== 1)
1023 record_line (current_subfile
, fcn_first_line
,
1024 fcn_first_line_addr
);
1026 enter_linenos (fcn_line_ptr
, fcn_first_line
, fcn_last_line
,
1029 finish_block (new->name
, &local_symbols
, new->old_blocks
,
1031 #if defined (FUNCTION_EPILOGUE_SIZE)
1032 /* This macro should be defined only on
1034 fcn_aux_saved.x_sym.x_misc.x_fsize
1035 field is always zero.
1036 So use the .bf record information that
1037 points to the epilogue and add the size
1040 + FUNCTION_EPILOGUE_SIZE
1041 + ANOFFSET (objfile
->section_offsets
, SECT_OFF_TEXT (objfile
)),
1043 fcn_cs_saved
.c_value
1044 + fcn_aux_saved
.x_sym
.x_misc
.x_fsize
1045 + ANOFFSET (objfile
->section_offsets
, SECT_OFF_TEXT (objfile
)),
1049 within_function
= 0;
1054 if (STREQ (cs
->c_name
, ".bb"))
1056 tmpaddr
= cs
->c_value
;
1057 tmpaddr
+= ANOFFSET (objfile
->section_offsets
, SECT_OFF_TEXT (objfile
));
1058 push_context (++depth
, tmpaddr
);
1060 else if (STREQ (cs
->c_name
, ".eb"))
1062 if (context_stack_depth
<= 0)
1063 { /* We attempted to pop an empty context stack */
1064 complaint (&symfile_complaints
,
1065 "`.eb' symbol without matching `.bb' symbol ignored starting at symnum %d",
1070 new = pop_context ();
1071 if (depth
-- != new->depth
)
1073 complaint (&symfile_complaints
,
1074 "Mismatched .eb symbol ignored starting at symnum %d",
1078 if (local_symbols
&& context_stack_depth
> 0)
1081 cs
->c_value
+ ANOFFSET (objfile
->section_offsets
, SECT_OFF_TEXT (objfile
));
1082 /* Make a block for the local symbols within. */
1083 finish_block (0, &local_symbols
, new->old_blocks
,
1084 new->start_addr
, tmpaddr
, objfile
);
1086 /* Now pop locals of block just finished. */
1087 local_symbols
= new->locals
;
1092 process_coff_symbol (cs
, &main_aux
, objfile
);
1097 if ((nsyms
== 0) && (pe_file
))
1099 /* We've got no debugging symbols, but it's is a portable
1100 executable, so try to read the export table */
1101 read_pe_exported_syms (objfile
);
1104 if (last_source_file
)
1105 coff_end_symtab (objfile
);
1107 /* Patch up any opaque types (references to types that are not defined
1108 in the file where they are referenced, e.g. "struct foo *bar"). */
1109 ALL_OBJFILE_SYMTABS (objfile
, s
)
1110 patch_opaque_types (s
);
1112 current_objfile
= NULL
;
1115 /* Routines for reading headers and symbols from executable. */
1117 /* Read the next symbol, swap it, and return it in both internal_syment
1118 form, and coff_symbol form. Also return its first auxent, if any,
1119 in internal_auxent form, and skip any other auxents. */
1122 read_one_sym (struct coff_symbol
*cs
,
1123 struct internal_syment
*sym
,
1124 union internal_auxent
*aux
)
1128 cs
->c_symnum
= symnum
;
1129 bfd_bread (temp_sym
, local_symesz
, nlist_bfd_global
);
1130 bfd_coff_swap_sym_in (symfile_bfd
, temp_sym
, (char *) sym
);
1131 cs
->c_naux
= sym
->n_numaux
& 0xff;
1132 if (cs
->c_naux
>= 1)
1134 bfd_bread (temp_aux
, local_auxesz
, nlist_bfd_global
);
1135 bfd_coff_swap_aux_in (symfile_bfd
, temp_aux
, sym
->n_type
, sym
->n_sclass
,
1136 0, cs
->c_naux
, (char *) aux
);
1137 /* If more than one aux entry, read past it (only the first aux
1139 for (i
= 1; i
< cs
->c_naux
; i
++)
1140 bfd_bread (temp_aux
, local_auxesz
, nlist_bfd_global
);
1142 cs
->c_name
= getsymname (sym
);
1143 cs
->c_value
= sym
->n_value
;
1144 cs
->c_sclass
= (sym
->n_sclass
& 0xff);
1145 cs
->c_secnum
= sym
->n_scnum
;
1146 cs
->c_type
= (unsigned) sym
->n_type
;
1147 if (!SDB_TYPE (cs
->c_type
))
1151 if (cs
->c_sclass
& 128)
1152 printf ("thumb symbol %s, class 0x%x\n", cs
->c_name
, cs
->c_sclass
);
1155 symnum
+= 1 + cs
->c_naux
;
1157 /* The PE file format stores symbol values as offsets within the
1158 section, rather than as absolute addresses. We correct that
1159 here, if the symbol has an appropriate storage class. FIXME: We
1160 should use BFD to read the symbols, rather than duplicating the
1164 switch (cs
->c_sclass
)
1168 case C_THUMBEXTFUNC
:
1173 case C_THUMBSTATFUNC
:
1179 if (cs
->c_secnum
!= 0)
1180 cs
->c_value
+= cs_section_address (cs
, symfile_bfd
);
1186 /* Support for string table handling */
1188 static char *stringtab
= NULL
;
1191 init_stringtab (bfd
*abfd
, long offset
)
1195 unsigned char lengthbuf
[4];
1199 /* If the file is stripped, the offset might be zero, indicating no
1200 string table. Just return with `stringtab' set to null. */
1204 if (bfd_seek (abfd
, offset
, 0) < 0)
1207 val
= bfd_bread ((char *) lengthbuf
, sizeof lengthbuf
, abfd
);
1208 length
= bfd_h_get_32 (symfile_bfd
, lengthbuf
);
1210 /* If no string table is needed, then the file may end immediately
1211 after the symbols. Just return with `stringtab' set to null. */
1212 if (val
!= sizeof lengthbuf
|| length
< sizeof lengthbuf
)
1215 stringtab
= (char *) xmalloc (length
);
1216 /* This is in target format (probably not very useful, and not currently
1217 used), not host format. */
1218 memcpy (stringtab
, lengthbuf
, sizeof lengthbuf
);
1219 if (length
== sizeof length
) /* Empty table -- just the count */
1222 val
= bfd_bread (stringtab
+ sizeof lengthbuf
, length
- sizeof lengthbuf
,
1224 if (val
!= length
- sizeof lengthbuf
|| stringtab
[length
- 1] != '\0')
1231 free_stringtab (void)
1239 free_stringtab_cleanup (void *ignore
)
1245 getsymname (struct internal_syment
*symbol_entry
)
1247 static char buffer
[SYMNMLEN
+ 1];
1250 if (symbol_entry
->_n
._n_n
._n_zeroes
== 0)
1252 /* FIXME: Probably should be detecting corrupt symbol files by
1253 seeing whether offset points to within the stringtab. */
1254 result
= stringtab
+ symbol_entry
->_n
._n_n
._n_offset
;
1258 strncpy (buffer
, symbol_entry
->_n
._n_name
, SYMNMLEN
);
1259 buffer
[SYMNMLEN
] = '\0';
1265 /* Extract the file name from the aux entry of a C_FILE symbol. Return
1266 only the last component of the name. Result is in static storage and
1267 is only good for temporary use. */
1270 coff_getfilename (union internal_auxent
*aux_entry
)
1272 static char buffer
[BUFSIZ
];
1276 if (aux_entry
->x_file
.x_n
.x_zeroes
== 0)
1277 strcpy (buffer
, stringtab
+ aux_entry
->x_file
.x_n
.x_offset
);
1280 strncpy (buffer
, aux_entry
->x_file
.x_fname
, FILNMLEN
);
1281 buffer
[FILNMLEN
] = '\0';
1285 /* FIXME: We should not be throwing away the information about what
1286 directory. It should go into dirname of the symtab, or some such
1288 if ((temp
= strrchr (result
, '/')) != NULL
)
1293 /* Support for line number handling. */
1295 static char *linetab
= NULL
;
1296 static long linetab_offset
;
1297 static unsigned long linetab_size
;
1299 /* Read in all the line numbers for fast lookups later. Leave them in
1300 external (unswapped) format in memory; we'll swap them as we enter
1301 them into GDB's data structures. */
1304 init_lineno (bfd
*abfd
, long offset
, int size
)
1308 linetab_offset
= offset
;
1309 linetab_size
= size
;
1316 if (bfd_seek (abfd
, offset
, 0) < 0)
1319 /* Allocate the desired table, plus a sentinel */
1320 linetab
= (char *) xmalloc (size
+ local_linesz
);
1322 val
= bfd_bread (linetab
, size
, abfd
);
1326 /* Terminate it with an all-zero sentinel record */
1327 memset (linetab
+ size
, 0, local_linesz
);
1341 free_linetab_cleanup (void *ignore
)
1346 #if !defined (L_LNNO32)
1347 #define L_LNNO32(lp) ((lp)->l_lnno)
1351 enter_linenos (long file_offset
, int first_line
,
1352 int last_line
, struct objfile
*objfile
)
1355 struct internal_lineno lptr
;
1359 if (file_offset
< linetab_offset
)
1361 complaint (&symfile_complaints
,
1362 "Line number pointer %ld lower than start of line numbers",
1364 if (file_offset
> linetab_size
) /* Too big to be an offset? */
1366 file_offset
+= linetab_offset
; /* Try reading at that linetab offset */
1369 rawptr
= &linetab
[file_offset
- linetab_offset
];
1371 /* skip first line entry for each function */
1372 rawptr
+= local_linesz
;
1373 /* line numbers start at one for the first line of the function */
1378 bfd_coff_swap_lineno_in (symfile_bfd
, rawptr
, &lptr
);
1379 rawptr
+= local_linesz
;
1380 /* The next function, or the sentinel, will have L_LNNO32 zero; we exit. */
1381 if (L_LNNO32 (&lptr
) && L_LNNO32 (&lptr
) <= last_line
)
1382 record_line (current_subfile
, first_line
+ L_LNNO32 (&lptr
),
1384 + ANOFFSET (objfile
->section_offsets
, SECT_OFF_TEXT (objfile
)));
1391 patch_type (struct type
*type
, struct type
*real_type
)
1393 struct type
*target
= TYPE_TARGET_TYPE (type
);
1394 struct type
*real_target
= TYPE_TARGET_TYPE (real_type
);
1395 int field_size
= TYPE_NFIELDS (real_target
) * sizeof (struct field
);
1397 TYPE_LENGTH (target
) = TYPE_LENGTH (real_target
);
1398 TYPE_NFIELDS (target
) = TYPE_NFIELDS (real_target
);
1399 TYPE_FIELDS (target
) = (struct field
*) TYPE_ALLOC (target
, field_size
);
1401 memcpy (TYPE_FIELDS (target
), TYPE_FIELDS (real_target
), field_size
);
1403 if (TYPE_NAME (real_target
))
1405 if (TYPE_NAME (target
))
1406 xfree (TYPE_NAME (target
));
1407 TYPE_NAME (target
) = concat (TYPE_NAME (real_target
), NULL
);
1411 /* Patch up all appropriate typedef symbols in the opaque_type_chains
1412 so that they can be used to print out opaque data structures properly. */
1415 patch_opaque_types (struct symtab
*s
)
1418 struct dict_iterator iter
;
1419 struct symbol
*real_sym
;
1421 /* Go through the per-file symbols only */
1422 b
= BLOCKVECTOR_BLOCK (BLOCKVECTOR (s
), STATIC_BLOCK
);
1423 ALL_BLOCK_SYMBOLS (b
, iter
, real_sym
)
1425 /* Find completed typedefs to use to fix opaque ones.
1426 Remove syms from the chain when their types are stored,
1427 but search the whole chain, as there may be several syms
1428 from different files with the same name. */
1429 if (SYMBOL_CLASS (real_sym
) == LOC_TYPEDEF
&&
1430 SYMBOL_DOMAIN (real_sym
) == VAR_DOMAIN
&&
1431 TYPE_CODE (SYMBOL_TYPE (real_sym
)) == TYPE_CODE_PTR
&&
1432 TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (real_sym
))) != 0)
1434 char *name
= DEPRECATED_SYMBOL_NAME (real_sym
);
1435 int hash
= hashname (name
);
1436 struct symbol
*sym
, *prev
;
1439 for (sym
= opaque_type_chain
[hash
]; sym
;)
1441 if (name
[0] == DEPRECATED_SYMBOL_NAME (sym
)[0] &&
1442 STREQ (name
+ 1, DEPRECATED_SYMBOL_NAME (sym
) + 1))
1446 SYMBOL_VALUE_CHAIN (prev
) = SYMBOL_VALUE_CHAIN (sym
);
1450 opaque_type_chain
[hash
] = SYMBOL_VALUE_CHAIN (sym
);
1453 patch_type (SYMBOL_TYPE (sym
), SYMBOL_TYPE (real_sym
));
1457 sym
= SYMBOL_VALUE_CHAIN (prev
);
1461 sym
= opaque_type_chain
[hash
];
1467 sym
= SYMBOL_VALUE_CHAIN (sym
);
1474 static struct symbol
*
1475 process_coff_symbol (struct coff_symbol
*cs
,
1476 union internal_auxent
*aux
,
1477 struct objfile
*objfile
)
1480 = (struct symbol
*) obstack_alloc (&objfile
->symbol_obstack
,
1481 sizeof (struct symbol
));
1484 memset (sym
, 0, sizeof (struct symbol
));
1486 name
= EXTERNAL_NAME (name
, objfile
->obfd
);
1487 SYMBOL_LANGUAGE (sym
) = language_auto
;
1488 SYMBOL_SET_NAMES (sym
, name
, strlen (name
), objfile
);
1490 /* default assumptions */
1491 SYMBOL_VALUE (sym
) = cs
->c_value
;
1492 SYMBOL_DOMAIN (sym
) = VAR_DOMAIN
;
1493 SYMBOL_SECTION (sym
) = cs_to_section (cs
, objfile
);
1495 if (ISFCN (cs
->c_type
))
1497 SYMBOL_VALUE (sym
) += ANOFFSET (objfile
->section_offsets
, SECT_OFF_TEXT (objfile
));
1499 lookup_function_type (decode_function_type (cs
, cs
->c_type
, aux
));
1501 SYMBOL_CLASS (sym
) = LOC_BLOCK
;
1502 if (cs
->c_sclass
== C_STAT
|| cs
->c_sclass
== C_THUMBSTAT
1503 || cs
->c_sclass
== C_THUMBSTATFUNC
)
1504 add_symbol_to_list (sym
, &file_symbols
);
1505 else if (cs
->c_sclass
== C_EXT
|| cs
->c_sclass
== C_THUMBEXT
1506 || cs
->c_sclass
== C_THUMBEXTFUNC
)
1507 add_symbol_to_list (sym
, &global_symbols
);
1511 SYMBOL_TYPE (sym
) = decode_type (cs
, cs
->c_type
, aux
);
1512 switch (cs
->c_sclass
)
1518 SYMBOL_CLASS (sym
) = LOC_LOCAL
;
1519 add_symbol_to_list (sym
, &local_symbols
);
1523 case C_THUMBEXTFUNC
:
1525 SYMBOL_CLASS (sym
) = LOC_STATIC
;
1526 SYMBOL_VALUE_ADDRESS (sym
) = (CORE_ADDR
) cs
->c_value
;
1527 SYMBOL_VALUE_ADDRESS (sym
) += ANOFFSET (objfile
->section_offsets
, SECT_OFF_TEXT (objfile
));
1528 add_symbol_to_list (sym
, &global_symbols
);
1532 case C_THUMBSTATFUNC
:
1534 SYMBOL_CLASS (sym
) = LOC_STATIC
;
1535 SYMBOL_VALUE_ADDRESS (sym
) = (CORE_ADDR
) cs
->c_value
;
1536 SYMBOL_VALUE_ADDRESS (sym
) += ANOFFSET (objfile
->section_offsets
, SECT_OFF_TEXT (objfile
));
1537 if (within_function
)
1539 /* Static symbol of local scope */
1540 add_symbol_to_list (sym
, &local_symbols
);
1544 /* Static symbol at top level of file */
1545 add_symbol_to_list (sym
, &file_symbols
);
1549 #ifdef C_GLBLREG /* AMD coff */
1553 SYMBOL_CLASS (sym
) = LOC_REGISTER
;
1554 SYMBOL_VALUE (sym
) = SDB_REG_TO_REGNUM (cs
->c_value
);
1555 add_symbol_to_list (sym
, &local_symbols
);
1563 SYMBOL_CLASS (sym
) = LOC_ARG
;
1564 add_symbol_to_list (sym
, &local_symbols
);
1565 #if !defined (BELIEVE_PCC_PROMOTION)
1566 if (TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
)
1568 /* If PCC says a parameter is a short or a char,
1569 aligned on an int boundary, realign it to the
1570 "little end" of the int. */
1571 struct type
*temptype
;
1572 temptype
= lookup_fundamental_type (current_objfile
,
1574 if (TYPE_LENGTH (SYMBOL_TYPE (sym
)) < TYPE_LENGTH (temptype
)
1575 && TYPE_CODE (SYMBOL_TYPE (sym
)) == TYPE_CODE_INT
1576 && 0 == SYMBOL_VALUE (sym
) % TYPE_LENGTH (temptype
))
1578 SYMBOL_VALUE (sym
) +=
1579 TYPE_LENGTH (temptype
)
1580 - TYPE_LENGTH (SYMBOL_TYPE (sym
));
1587 SYMBOL_CLASS (sym
) = LOC_REGPARM
;
1588 SYMBOL_VALUE (sym
) = SDB_REG_TO_REGNUM (cs
->c_value
);
1589 add_symbol_to_list (sym
, &local_symbols
);
1590 #if !defined (BELIEVE_PCC_PROMOTION)
1591 /* FIXME: This should retain the current type, since it's just
1592 a register value. gnu@adobe, 26Feb93 */
1594 /* If PCC says a parameter is a short or a char,
1595 it is really an int. */
1596 struct type
*temptype
;
1598 lookup_fundamental_type (current_objfile
, FT_INTEGER
);
1599 if (TYPE_LENGTH (SYMBOL_TYPE (sym
)) < TYPE_LENGTH (temptype
)
1600 && TYPE_CODE (SYMBOL_TYPE (sym
)) == TYPE_CODE_INT
)
1603 (TYPE_UNSIGNED (SYMBOL_TYPE (sym
))
1604 ? lookup_fundamental_type (current_objfile
,
1605 FT_UNSIGNED_INTEGER
)
1613 SYMBOL_CLASS (sym
) = LOC_TYPEDEF
;
1614 SYMBOL_DOMAIN (sym
) = VAR_DOMAIN
;
1616 /* If type has no name, give it one */
1617 if (TYPE_NAME (SYMBOL_TYPE (sym
)) == 0)
1619 if (TYPE_CODE (SYMBOL_TYPE (sym
)) == TYPE_CODE_PTR
1620 || TYPE_CODE (SYMBOL_TYPE (sym
)) == TYPE_CODE_FUNC
)
1622 /* If we are giving a name to a type such as "pointer to
1623 foo" or "function returning foo", we better not set
1624 the TYPE_NAME. If the program contains "typedef char
1625 *caddr_t;", we don't want all variables of type char
1626 * to print as caddr_t. This is not just a
1627 consequence of GDB's type management; CC and GCC (at
1628 least through version 2.4) both output variables of
1629 either type char * or caddr_t with the type
1630 refering to the C_TPDEF symbol for caddr_t. If a future
1631 compiler cleans this up it GDB is not ready for it
1632 yet, but if it becomes ready we somehow need to
1633 disable this check (without breaking the PCC/GCC2.4
1638 Fortunately, this check seems not to be necessary
1639 for anything except pointers or functions. */
1643 TYPE_NAME (SYMBOL_TYPE (sym
)) =
1644 concat (DEPRECATED_SYMBOL_NAME (sym
), NULL
);
1647 /* Ignore vendor section for Harris CX/UX targets. */
1648 else if (cs
->c_name
[0] == '$')
1650 #endif /* CXUX_TARGET */
1652 /* Keep track of any type which points to empty structured type,
1653 so it can be filled from a definition from another file. A
1654 simple forward reference (TYPE_CODE_UNDEF) is not an
1655 empty structured type, though; the forward references
1656 work themselves out via the magic of coff_lookup_type. */
1657 if (TYPE_CODE (SYMBOL_TYPE (sym
)) == TYPE_CODE_PTR
&&
1658 TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym
))) == 0 &&
1659 TYPE_CODE (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym
))) !=
1662 int i
= hashname (DEPRECATED_SYMBOL_NAME (sym
));
1664 SYMBOL_VALUE_CHAIN (sym
) = opaque_type_chain
[i
];
1665 opaque_type_chain
[i
] = sym
;
1667 add_symbol_to_list (sym
, &file_symbols
);
1673 SYMBOL_CLASS (sym
) = LOC_TYPEDEF
;
1674 SYMBOL_DOMAIN (sym
) = STRUCT_DOMAIN
;
1676 /* Some compilers try to be helpful by inventing "fake"
1677 names for anonymous enums, structures, and unions, like
1678 "~0fake" or ".0fake". Thanks, but no thanks... */
1679 if (TYPE_TAG_NAME (SYMBOL_TYPE (sym
)) == 0)
1680 if (DEPRECATED_SYMBOL_NAME (sym
) != NULL
1681 && *DEPRECATED_SYMBOL_NAME (sym
) != '~'
1682 && *DEPRECATED_SYMBOL_NAME (sym
) != '.')
1683 TYPE_TAG_NAME (SYMBOL_TYPE (sym
)) =
1684 concat (DEPRECATED_SYMBOL_NAME (sym
), NULL
);
1686 add_symbol_to_list (sym
, &file_symbols
);
1696 /* Decode a coff type specifier; return the type that is meant. */
1698 static struct type
*
1699 decode_type (struct coff_symbol
*cs
, unsigned int c_type
,
1700 union internal_auxent
*aux
)
1702 struct type
*type
= 0;
1703 unsigned int new_c_type
;
1705 if (c_type
& ~N_BTMASK
)
1707 new_c_type
= DECREF (c_type
);
1710 type
= decode_type (cs
, new_c_type
, aux
);
1711 type
= lookup_pointer_type (type
);
1713 else if (ISFCN (c_type
))
1715 type
= decode_type (cs
, new_c_type
, aux
);
1716 type
= lookup_function_type (type
);
1718 else if (ISARY (c_type
))
1721 unsigned short *dim
;
1722 struct type
*base_type
, *index_type
, *range_type
;
1724 /* Define an array type. */
1725 /* auxent refers to array, not base type */
1726 if (aux
->x_sym
.x_tagndx
.l
== 0)
1729 /* shift the indices down */
1730 dim
= &aux
->x_sym
.x_fcnary
.x_ary
.x_dimen
[0];
1733 for (i
= 0; *dim
&& i
< DIMNUM
- 1; i
++, dim
++)
1737 base_type
= decode_type (cs
, new_c_type
, aux
);
1738 index_type
= lookup_fundamental_type (current_objfile
, FT_INTEGER
);
1740 create_range_type ((struct type
*) NULL
, index_type
, 0, n
- 1);
1742 create_array_type ((struct type
*) NULL
, base_type
, range_type
);
1747 /* Reference to existing type. This only occurs with the
1748 struct, union, and enum types. EPI a29k coff
1749 fakes us out by producing aux entries with a nonzero
1750 x_tagndx for definitions of structs, unions, and enums, so we
1751 have to check the c_sclass field. SCO 3.2v4 cc gets confused
1752 with pointers to pointers to defined structs, and generates
1753 negative x_tagndx fields. */
1754 if (cs
->c_naux
> 0 && aux
->x_sym
.x_tagndx
.l
!= 0)
1756 if (cs
->c_sclass
!= C_STRTAG
1757 && cs
->c_sclass
!= C_UNTAG
1758 && cs
->c_sclass
!= C_ENTAG
1759 && aux
->x_sym
.x_tagndx
.l
>= 0)
1761 type
= coff_alloc_type (aux
->x_sym
.x_tagndx
.l
);
1766 complaint (&symfile_complaints
,
1767 "Symbol table entry for %s has bad tagndx value",
1769 /* And fall through to decode_base_type... */
1773 return decode_base_type (cs
, BTYPE (c_type
), aux
);
1776 /* Decode a coff type specifier for function definition;
1777 return the type that the function returns. */
1779 static struct type
*
1780 decode_function_type (struct coff_symbol
*cs
, unsigned int c_type
,
1781 union internal_auxent
*aux
)
1783 if (aux
->x_sym
.x_tagndx
.l
== 0)
1784 cs
->c_naux
= 0; /* auxent refers to function, not base type */
1786 return decode_type (cs
, DECREF (c_type
), aux
);
1791 static struct type
*
1792 decode_base_type (struct coff_symbol
*cs
, unsigned int c_type
,
1793 union internal_auxent
*aux
)
1800 /* shows up with "void (*foo)();" structure members */
1801 return lookup_fundamental_type (current_objfile
, FT_VOID
);
1804 /* DGUX actually defines both T_ARG and T_VOID to the same value. */
1807 /* Shows up in DGUX, I think. Not sure where. */
1808 return lookup_fundamental_type (current_objfile
, FT_VOID
); /* shouldn't show up here */
1814 /* Intel 960 COFF has this symbol and meaning. */
1815 return lookup_fundamental_type (current_objfile
, FT_VOID
);
1819 return lookup_fundamental_type (current_objfile
, FT_CHAR
);
1822 return lookup_fundamental_type (current_objfile
, FT_SHORT
);
1825 return lookup_fundamental_type (current_objfile
, FT_INTEGER
);
1828 if (cs
->c_sclass
== C_FIELD
1829 && aux
->x_sym
.x_misc
.x_lnsz
.x_size
> TARGET_LONG_BIT
)
1830 return lookup_fundamental_type (current_objfile
, FT_LONG_LONG
);
1832 return lookup_fundamental_type (current_objfile
, FT_LONG
);
1835 return lookup_fundamental_type (current_objfile
, FT_FLOAT
);
1838 return lookup_fundamental_type (current_objfile
, FT_DBL_PREC_FLOAT
);
1841 return lookup_fundamental_type (current_objfile
, FT_EXT_PREC_FLOAT
);
1844 if (cs
->c_naux
!= 1)
1846 /* anonymous structure type */
1847 type
= coff_alloc_type (cs
->c_symnum
);
1848 TYPE_CODE (type
) = TYPE_CODE_STRUCT
;
1849 TYPE_NAME (type
) = NULL
;
1850 /* This used to set the tag to "<opaque>". But I think setting it
1851 to NULL is right, and the printing code can print it as
1853 TYPE_TAG_NAME (type
) = NULL
;
1854 INIT_CPLUS_SPECIFIC (type
);
1855 TYPE_LENGTH (type
) = 0;
1856 TYPE_FIELDS (type
) = 0;
1857 TYPE_NFIELDS (type
) = 0;
1861 type
= coff_read_struct_type (cs
->c_symnum
,
1862 aux
->x_sym
.x_misc
.x_lnsz
.x_size
,
1863 aux
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
);
1868 if (cs
->c_naux
!= 1)
1870 /* anonymous union type */
1871 type
= coff_alloc_type (cs
->c_symnum
);
1872 TYPE_NAME (type
) = NULL
;
1873 /* This used to set the tag to "<opaque>". But I think setting it
1874 to NULL is right, and the printing code can print it as
1876 TYPE_TAG_NAME (type
) = NULL
;
1877 INIT_CPLUS_SPECIFIC (type
);
1878 TYPE_LENGTH (type
) = 0;
1879 TYPE_FIELDS (type
) = 0;
1880 TYPE_NFIELDS (type
) = 0;
1884 type
= coff_read_struct_type (cs
->c_symnum
,
1885 aux
->x_sym
.x_misc
.x_lnsz
.x_size
,
1886 aux
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
);
1888 TYPE_CODE (type
) = TYPE_CODE_UNION
;
1892 if (cs
->c_naux
!= 1)
1894 /* anonymous enum type */
1895 type
= coff_alloc_type (cs
->c_symnum
);
1896 TYPE_CODE (type
) = TYPE_CODE_ENUM
;
1897 TYPE_NAME (type
) = NULL
;
1898 /* This used to set the tag to "<opaque>". But I think setting it
1899 to NULL is right, and the printing code can print it as
1901 TYPE_TAG_NAME (type
) = NULL
;
1902 TYPE_LENGTH (type
) = 0;
1903 TYPE_FIELDS (type
) = 0;
1904 TYPE_NFIELDS (type
) = 0;
1908 type
= coff_read_enum_type (cs
->c_symnum
,
1909 aux
->x_sym
.x_misc
.x_lnsz
.x_size
,
1910 aux
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
);
1915 /* shouldn't show up here */
1919 return lookup_fundamental_type (current_objfile
, FT_UNSIGNED_CHAR
);
1922 return lookup_fundamental_type (current_objfile
, FT_UNSIGNED_SHORT
);
1925 return lookup_fundamental_type (current_objfile
, FT_UNSIGNED_INTEGER
);
1928 if (cs
->c_sclass
== C_FIELD
1929 && aux
->x_sym
.x_misc
.x_lnsz
.x_size
> TARGET_LONG_BIT
)
1930 return lookup_fundamental_type (current_objfile
, FT_UNSIGNED_LONG_LONG
);
1932 return lookup_fundamental_type (current_objfile
, FT_UNSIGNED_LONG
);
1934 complaint (&symfile_complaints
, "Unexpected type for symbol %s", cs
->c_name
);
1935 return lookup_fundamental_type (current_objfile
, FT_VOID
);
1938 /* This page contains subroutines of read_type. */
1940 /* Read the description of a structure (or union type) and return an
1941 object describing the type. */
1943 static struct type
*
1944 coff_read_struct_type (int index
, int length
, int lastsym
)
1948 struct nextfield
*next
;
1953 struct nextfield
*list
= 0;
1954 struct nextfield
*new;
1958 struct coff_symbol member_sym
;
1959 struct coff_symbol
*ms
= &member_sym
;
1960 struct internal_syment sub_sym
;
1961 union internal_auxent sub_aux
;
1964 type
= coff_alloc_type (index
);
1965 TYPE_CODE (type
) = TYPE_CODE_STRUCT
;
1966 INIT_CPLUS_SPECIFIC (type
);
1967 TYPE_LENGTH (type
) = length
;
1969 while (!done
&& symnum
< lastsym
&& symnum
< nlist_nsyms_global
)
1971 read_one_sym (ms
, &sub_sym
, &sub_aux
);
1973 name
= EXTERNAL_NAME (name
, current_objfile
->obfd
);
1975 switch (ms
->c_sclass
)
1980 /* Get space to record the next field's data. */
1981 new = (struct nextfield
*) alloca (sizeof (struct nextfield
));
1985 /* Save the data. */
1989 ¤t_objfile
->symbol_obstack
);
1990 FIELD_TYPE (list
->field
) = decode_type (ms
, ms
->c_type
, &sub_aux
);
1991 FIELD_BITPOS (list
->field
) = 8 * ms
->c_value
;
1992 FIELD_BITSIZE (list
->field
) = 0;
1993 FIELD_STATIC_KIND (list
->field
) = 0;
1999 /* Get space to record the next field's data. */
2000 new = (struct nextfield
*) alloca (sizeof (struct nextfield
));
2004 /* Save the data. */
2008 ¤t_objfile
->symbol_obstack
);
2009 FIELD_TYPE (list
->field
) = decode_type (ms
, ms
->c_type
, &sub_aux
);
2010 FIELD_BITPOS (list
->field
) = ms
->c_value
;
2011 FIELD_BITSIZE (list
->field
) = sub_aux
.x_sym
.x_misc
.x_lnsz
.x_size
;
2012 FIELD_STATIC_KIND (list
->field
) = 0;
2021 /* Now create the vector of fields, and record how big it is. */
2023 TYPE_NFIELDS (type
) = nfields
;
2024 TYPE_FIELDS (type
) = (struct field
*)
2025 TYPE_ALLOC (type
, sizeof (struct field
) * nfields
);
2027 /* Copy the saved-up fields into the field vector. */
2029 for (n
= nfields
; list
; list
= list
->next
)
2030 TYPE_FIELD (type
, --n
) = list
->field
;
2035 /* Read a definition of an enumeration type,
2036 and create and return a suitable type object.
2037 Also defines the symbols that represent the values of the type. */
2040 static struct type
*
2041 coff_read_enum_type (int index
, int length
, int lastsym
)
2047 struct pending
**symlist
;
2048 struct coff_symbol member_sym
;
2049 struct coff_symbol
*ms
= &member_sym
;
2050 struct internal_syment sub_sym
;
2051 union internal_auxent sub_aux
;
2052 struct pending
*osyms
, *syms
;
2056 int unsigned_enum
= 1;
2058 type
= coff_alloc_type (index
);
2059 if (within_function
)
2060 symlist
= &local_symbols
;
2062 symlist
= &file_symbols
;
2064 o_nsyms
= osyms
? osyms
->nsyms
: 0;
2066 while (!done
&& symnum
< lastsym
&& symnum
< nlist_nsyms_global
)
2068 read_one_sym (ms
, &sub_sym
, &sub_aux
);
2070 name
= EXTERNAL_NAME (name
, current_objfile
->obfd
);
2072 switch (ms
->c_sclass
)
2075 sym
= (struct symbol
*) obstack_alloc
2076 (¤t_objfile
->symbol_obstack
,
2077 sizeof (struct symbol
));
2078 memset (sym
, 0, sizeof (struct symbol
));
2080 DEPRECATED_SYMBOL_NAME (sym
) =
2081 obsavestring (name
, strlen (name
),
2082 ¤t_objfile
->symbol_obstack
);
2083 SYMBOL_CLASS (sym
) = LOC_CONST
;
2084 SYMBOL_DOMAIN (sym
) = VAR_DOMAIN
;
2085 SYMBOL_VALUE (sym
) = ms
->c_value
;
2086 add_symbol_to_list (sym
, symlist
);
2091 /* Sometimes the linker (on 386/ix 2.0.2 at least) screws
2092 up the count of how many symbols to read. So stop
2099 /* Now fill in the fields of the type-structure. */
2102 TYPE_LENGTH (type
) = length
;
2104 TYPE_LENGTH (type
) = TARGET_INT_BIT
/ TARGET_CHAR_BIT
; /* Assume ints */
2105 TYPE_CODE (type
) = TYPE_CODE_ENUM
;
2106 TYPE_NFIELDS (type
) = nsyms
;
2107 TYPE_FIELDS (type
) = (struct field
*)
2108 TYPE_ALLOC (type
, sizeof (struct field
) * nsyms
);
2110 /* Find the symbols for the values and put them into the type.
2111 The symbols can be found in the symlist that we put them on
2112 to cause them to be defined. osyms contains the old value
2113 of that symlist; everything up to there was defined by us. */
2114 /* Note that we preserve the order of the enum constants, so
2115 that in something like "enum {FOO, LAST_THING=FOO}" we print
2116 FOO, not LAST_THING. */
2118 for (syms
= *symlist
, n
= 0; syms
; syms
= syms
->next
)
2124 for (; j
< syms
->nsyms
; j
++, n
++)
2126 struct symbol
*xsym
= syms
->symbol
[j
];
2127 SYMBOL_TYPE (xsym
) = type
;
2128 TYPE_FIELD_NAME (type
, n
) = DEPRECATED_SYMBOL_NAME (xsym
);
2129 TYPE_FIELD_BITPOS (type
, n
) = SYMBOL_VALUE (xsym
);
2130 if (SYMBOL_VALUE (xsym
) < 0)
2132 TYPE_FIELD_BITSIZE (type
, n
) = 0;
2133 TYPE_FIELD_STATIC_KIND (type
, n
) = 0;
2140 TYPE_FLAGS (type
) |= TYPE_FLAG_UNSIGNED
;
2145 /* Register our ability to parse symbols for coff BFD files. */
2147 static struct sym_fns coff_sym_fns
=
2149 bfd_target_coff_flavour
,
2150 coff_new_init
, /* sym_new_init: init anything gbl to entire symtab */
2151 coff_symfile_init
, /* sym_init: read initial info, setup for sym_read() */
2152 coff_symfile_read
, /* sym_read: read a symbol file into symtab */
2153 coff_symfile_finish
, /* sym_finish: finished with file, cleanup */
2154 default_symfile_offsets
, /* sym_offsets: xlate external to internal form */
2155 NULL
/* next: pointer to next struct sym_fns */
2159 _initialize_coffread (void)
2161 add_symtab_fns (&coff_sym_fns
);