1 /* Read coff symbol tables and convert to internal format, for GDB.
2 Copyright 1987, 88, 89, 90, 91, 92, 93, 94, 96, 97, 1998
3 Free Software Foundation, Inc.
4 Contributed by David D. Johnson, Brown University (ddj@cs.brown.edu).
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
27 #include "breakpoint.h"
32 #include "gdb_string.h"
35 #include "coff/internal.h" /* Internal format of COFF symbols in BFD */
36 #include "libcoff.h" /* FIXME secret internal data from BFD */
41 #include "gdb-stabs.h"
42 #include "stabsread.h"
43 #include "complaints.h"
46 extern void _initialize_coffread
PARAMS ((void));
48 struct coff_symfile_info
50 file_ptr min_lineno_offset
; /* Where in file lowest line#s are */
51 file_ptr max_lineno_offset
; /* 1+last byte of line#s in file */
53 CORE_ADDR textaddr
; /* Addr of .text section. */
54 unsigned int textsize
; /* Size of .text section. */
55 struct stab_section_list
*stabsects
; /* .stab sections. */
56 asection
*stabstrsect
; /* Section pointer for .stab section */
60 /* Translate an external name string into a user-visible name. */
61 #define EXTERNAL_NAME(string, abfd) \
62 (string[0] == bfd_get_symbol_leading_char(abfd)? string+1: string)
64 /* To be an sdb debug type, type must have at least a basic or primary
65 derived type. Using this rather than checking against T_NULL is
66 said to prevent core dumps if we try to operate on Michael Bloom
69 #define SDB_TYPE(type) (BTYPE(type) | (type & N_TMASK))
71 /* Convert from an sdb register number to an internal gdb register number.
72 This should be defined in tm.h, if REGISTER_NAMES is not set up
73 to map one to one onto the sdb register numbers. */
75 #ifndef SDB_REG_TO_REGNUM
76 #define SDB_REG_TO_REGNUM(value) (value)
79 /* Core address of start and end of text of current source file.
80 This comes from a ".text" symbol where x_nlinno > 0. */
82 static CORE_ADDR current_source_start_addr
;
83 static CORE_ADDR current_source_end_addr
;
85 /* The addresses of the symbol table stream and number of symbols
86 of the object file we are reading (as copied into core). */
88 static bfd
*nlist_bfd_global
;
89 static int nlist_nsyms_global
;
92 /* Pointers to scratch storage, used for reading raw symbols and auxents. */
94 static char *temp_sym
;
95 static char *temp_aux
;
97 /* Local variables that hold the shift and mask values for the
98 COFF file that we are currently reading. These come back to us
99 from BFD, and are referenced by their macro names, as well as
100 internally to the BTYPE, ISPTR, ISFCN, ISARY, ISTAG, and DECREF
101 macros from include/coff/internal.h . */
103 static unsigned local_n_btmask
;
104 static unsigned local_n_btshft
;
105 static unsigned local_n_tmask
;
106 static unsigned local_n_tshift
;
108 #define N_BTMASK local_n_btmask
109 #define N_BTSHFT local_n_btshft
110 #define N_TMASK local_n_tmask
111 #define N_TSHIFT local_n_tshift
113 /* Local variables that hold the sizes in the file of various COFF structures.
114 (We only need to know this to read them from the file -- BFD will then
115 translate the data in them, into `internal_xxx' structs in the right
116 byte order, alignment, etc.) */
118 static unsigned local_linesz
;
119 static unsigned local_symesz
;
120 static unsigned local_auxesz
;
122 /* This is set if this is a PE format file. */
126 /* Chain of typedefs of pointers to empty struct/union types.
127 They are chained thru the SYMBOL_VALUE_CHAIN. */
129 static struct symbol
*opaque_type_chain
[HASHSIZE
];
131 /* Complaints about various problems in the file being read */
133 struct complaint ef_complaint
=
134 {"Unmatched .ef symbol(s) ignored starting at symnum %d", 0, 0};
136 struct complaint ef_stack_complaint
=
137 {"`.ef' symbol without matching `.bf' symbol ignored starting at symnum %d", 0, 0};
139 struct complaint eb_stack_complaint
=
140 {"`.eb' symbol without matching `.bb' symbol ignored starting at symnum %d", 0, 0};
142 struct complaint bf_no_aux_complaint
=
143 {"`.bf' symbol %d has no aux entry", 0, 0};
145 struct complaint ef_no_aux_complaint
=
146 {"`.ef' symbol %d has no aux entry", 0, 0};
148 struct complaint lineno_complaint
=
149 {"Line number pointer %d lower than start of line numbers", 0, 0};
151 struct complaint unexpected_type_complaint
=
152 {"Unexpected type for symbol %s", 0, 0};
154 struct complaint bad_sclass_complaint
=
155 {"Bad n_sclass for symbol %s", 0, 0};
157 struct complaint misordered_blocks_complaint
=
158 {"Blocks out of order at address %x", 0, 0};
160 struct complaint tagndx_bad_complaint
=
161 {"Symbol table entry for %s has bad tagndx value", 0, 0};
163 struct complaint eb_complaint
=
164 {"Mismatched .eb symbol ignored starting at symnum %d", 0, 0};
166 /* Simplified internal version of coff symbol table information */
171 int c_symnum
; /* symbol number of this entry */
172 int c_naux
; /* 0 if syment only, 1 if syment + auxent, etc */
179 extern void stabsread_clear_cache
PARAMS ((void));
181 static struct type
*coff_read_struct_type
PARAMS ((int, int, int));
183 static struct type
*decode_base_type
PARAMS ((struct coff_symbol
*,
185 union internal_auxent
*));
187 static struct type
*decode_type
PARAMS ((struct coff_symbol
*, unsigned int,
188 union internal_auxent
*));
190 static struct type
*decode_function_type
PARAMS ((struct coff_symbol
*,
192 union internal_auxent
*));
194 static struct type
*coff_read_enum_type
PARAMS ((int, int, int));
196 static struct symbol
*process_coff_symbol
PARAMS ((struct coff_symbol
*,
197 union internal_auxent
*,
200 static void patch_opaque_types
PARAMS ((struct symtab
*));
202 static void patch_type
PARAMS ((struct type
*, struct type
*));
204 static void enter_linenos
PARAMS ((long, int, int, struct objfile
*));
206 static void free_linetab
PARAMS ((void));
208 static void free_linetab_cleanup (void *ignore
);
210 static int init_lineno
PARAMS ((bfd
*, long, int));
212 static char *getsymname
PARAMS ((struct internal_syment
*));
214 static char *coff_getfilename
PARAMS ((union internal_auxent
*));
216 static void free_stringtab
PARAMS ((void));
218 static void free_stringtab_cleanup (void *ignore
);
220 static int init_stringtab
PARAMS ((bfd
*, long));
222 static void read_one_sym
PARAMS ((struct coff_symbol
*,
223 struct internal_syment
*,
224 union internal_auxent
*));
226 static void coff_symtab_read
PARAMS ((long, unsigned int, struct objfile
*));
228 static void find_linenos
PARAMS ((bfd
*, sec_ptr
, PTR
));
230 static void coff_symfile_init
PARAMS ((struct objfile
*));
232 static void coff_new_init
PARAMS ((struct objfile
*));
234 static void coff_symfile_read
PARAMS ((struct objfile
*, int));
236 static void coff_symfile_finish
PARAMS ((struct objfile
*));
238 static void record_minimal_symbol
PARAMS ((char *, CORE_ADDR
,
239 enum minimal_symbol_type
,
242 static void coff_end_symtab
PARAMS ((struct objfile
*));
244 static void complete_symtab
PARAMS ((char *, CORE_ADDR
, unsigned int));
246 static void coff_start_symtab
PARAMS ((char *));
248 static struct type
*coff_alloc_type
PARAMS ((int));
250 static struct type
**coff_lookup_type
PARAMS ((int));
252 static void coff_locate_sections
PARAMS ((bfd
*, asection
*, PTR
));
254 /* We are called once per section from coff_symfile_read. We
255 need to examine each section we are passed, check to see
256 if it is something we are interested in processing, and
257 if so, stash away some access information for the section.
259 FIXME: The section names should not be hardwired strings (what
260 should they be? I don't think most object file formats have enough
261 section flags to specify what kind of debug section it is
265 coff_locate_sections (abfd
, sectp
, csip
)
270 register struct coff_symfile_info
*csi
;
273 csi
= (struct coff_symfile_info
*) csip
;
274 name
= bfd_get_section_name (abfd
, sectp
);
275 if (STREQ (name
, ".text"))
277 csi
->textaddr
= bfd_section_vma (abfd
, sectp
);
278 csi
->textsize
+= bfd_section_size (abfd
, sectp
);
280 else if (strncmp (name
, ".text", sizeof ".text" - 1) == 0)
282 csi
->textsize
+= bfd_section_size (abfd
, sectp
);
284 else if (STREQ (name
, ".stabstr"))
286 csi
->stabstrsect
= sectp
;
288 else if (strncmp (name
, ".stab", sizeof ".stab" - 1) == 0)
292 /* We can have multiple .stab sections if linked with
294 for (s
= name
+ sizeof ".stab" - 1; *s
!= '\0'; s
++)
299 struct stab_section_list
*n
, **pn
;
301 n
= ((struct stab_section_list
*)
302 xmalloc (sizeof (struct stab_section_list
)));
305 for (pn
= &csi
->stabsects
; *pn
!= NULL
; pn
= &(*pn
)->next
)
309 /* This will be run after coffstab_build_psymtabs is called
310 in coff_symfile_read, at which point we no longer need
312 make_cleanup (free
, n
);
317 /* Return the section_offsets* that CS points to. */
318 static int cs_to_section
PARAMS ((struct coff_symbol
*, struct objfile
*));
320 struct find_targ_sec_arg
326 static void find_targ_sec
PARAMS ((bfd
*, asection
*, void *));
329 find_targ_sec (abfd
, sect
, obj
)
334 struct find_targ_sec_arg
*args
= (struct find_targ_sec_arg
*) obj
;
335 if (sect
->target_index
== args
->targ_index
)
336 *args
->resultp
= sect
;
339 /* Return the section number (SECT_OFF_*) that CS points to. */
341 cs_to_section (cs
, objfile
)
342 struct coff_symbol
*cs
;
343 struct objfile
*objfile
;
345 asection
*sect
= NULL
;
346 struct find_targ_sec_arg args
;
347 int off
= SECT_OFF_TEXT (objfile
);
349 args
.targ_index
= cs
->c_secnum
;
350 args
.resultp
= §
;
351 bfd_map_over_sections (objfile
->obfd
, find_targ_sec
, &args
);
354 /* This is the section. Figure out what SECT_OFF_* code it is. */
355 if (bfd_get_section_flags (abfd
, sect
) & SEC_CODE
)
356 off
= SECT_OFF_TEXT (objfile
);
357 else if (bfd_get_section_flags (abfd
, sect
) & SEC_LOAD
)
358 off
= SECT_OFF_DATA (objfile
);
360 off
= SECT_OFF_BSS (objfile
);
365 /* Return the address of the section of a COFF symbol. */
367 static CORE_ADDR cs_section_address
PARAMS ((struct coff_symbol
*, bfd
*));
370 cs_section_address (cs
, abfd
)
371 struct coff_symbol
*cs
;
374 asection
*sect
= NULL
;
375 struct find_targ_sec_arg args
;
378 args
.targ_index
= cs
->c_secnum
;
379 args
.resultp
= §
;
380 bfd_map_over_sections (abfd
, find_targ_sec
, &args
);
382 addr
= bfd_get_section_vma (objfile
->obfd
, sect
);
386 /* Look up a coff type-number index. Return the address of the slot
387 where the type for that index is stored.
388 The type-number is in INDEX.
390 This can be used for finding the type associated with that index
391 or for associating a new type with the index. */
393 static struct type
**
394 coff_lookup_type (index
)
397 if (index
>= type_vector_length
)
399 int old_vector_length
= type_vector_length
;
401 type_vector_length
*= 2;
402 if (index
/* is still */ >= type_vector_length
)
403 type_vector_length
= index
* 2;
405 type_vector
= (struct type
**)
406 xrealloc ((char *) type_vector
,
407 type_vector_length
* sizeof (struct type
*));
408 memset (&type_vector
[old_vector_length
], 0,
409 (type_vector_length
- old_vector_length
) * sizeof (struct type
*));
411 return &type_vector
[index
];
414 /* Make sure there is a type allocated for type number index
415 and return the type object.
416 This can create an empty (zeroed) type object. */
419 coff_alloc_type (index
)
422 register struct type
**type_addr
= coff_lookup_type (index
);
423 register struct type
*type
= *type_addr
;
425 /* If we are referring to a type not known at all yet,
426 allocate an empty type for it.
427 We will fill it in later if we find out how. */
430 type
= alloc_type (current_objfile
);
436 /* Start a new symtab for a new source file.
437 This is called when a COFF ".file" symbol is seen;
438 it indicates the start of data for one original source file. */
441 coff_start_symtab (name
)
445 /* We fill in the filename later. start_symtab puts
446 this pointer into last_source_file and we put it in
447 subfiles->name, which end_symtab frees; that's why
448 it must be malloc'd. */
449 savestring (name
, strlen (name
)),
450 /* We never know the directory name for COFF. */
452 /* The start address is irrelevant, since we set
453 last_source_start_addr in coff_end_symtab. */
455 record_debugformat ("COFF");
458 /* Save the vital information from when starting to read a file,
459 for use when closing off the current file.
460 NAME is the file name the symbols came from, START_ADDR is the first
461 text address for the file, and SIZE is the number of bytes of text. */
464 complete_symtab (name
, start_addr
, size
)
466 CORE_ADDR start_addr
;
469 if (last_source_file
!= NULL
)
470 free (last_source_file
);
471 last_source_file
= savestring (name
, strlen (name
));
472 current_source_start_addr
= start_addr
;
473 current_source_end_addr
= start_addr
+ size
;
475 if (current_objfile
->ei
.entry_point
>= current_source_start_addr
&&
476 current_objfile
->ei
.entry_point
< current_source_end_addr
)
478 current_objfile
->ei
.entry_file_lowpc
= current_source_start_addr
;
479 current_objfile
->ei
.entry_file_highpc
= current_source_end_addr
;
483 /* Finish the symbol definitions for one main source file,
484 close off all the lexical contexts for that file
485 (creating struct block's for them), then make the
486 struct symtab for that file and put it in the list of all such. */
489 coff_end_symtab (objfile
)
490 struct objfile
*objfile
;
492 struct symtab
*symtab
;
494 last_source_start_addr
= current_source_start_addr
;
496 symtab
= end_symtab (current_source_end_addr
, objfile
, 0);
499 free_named_symtabs (symtab
->filename
);
501 /* Reinitialize for beginning of new file. */
502 last_source_file
= NULL
;
506 record_minimal_symbol (name
, address
, type
, objfile
)
509 enum minimal_symbol_type type
;
510 struct objfile
*objfile
;
512 /* We don't want TDESC entry points in the minimal symbol table */
516 prim_record_minimal_symbol (name
, address
, type
, objfile
);
519 /* coff_symfile_init ()
520 is the coff-specific initialization routine for reading symbols.
521 It is passed a struct objfile which contains, among other things,
522 the BFD for the file whose symbols are being read, and a slot for
523 a pointer to "private data" which we fill with cookies and other
524 treats for coff_symfile_read ().
526 We will only be called if this is a COFF or COFF-like file.
527 BFD handles figuring out the format of the file, and code in symtab.c
528 uses BFD's determination to vector to us.
530 The ultimate result is a new symtab (or, FIXME, eventually a psymtab). */
533 coff_symfile_init (objfile
)
534 struct objfile
*objfile
;
536 /* Allocate struct to keep track of stab reading. */
537 objfile
->sym_stab_info
= (struct dbx_symfile_info
*)
538 xmmalloc (objfile
->md
, sizeof (struct dbx_symfile_info
));
540 memset ((PTR
) objfile
->sym_stab_info
, 0, sizeof (struct dbx_symfile_info
));
542 /* Allocate struct to keep track of the symfile */
543 objfile
->sym_private
= xmmalloc (objfile
->md
,
544 sizeof (struct coff_symfile_info
));
546 memset (objfile
->sym_private
, 0, sizeof (struct coff_symfile_info
));
548 /* COFF objects may be reordered, so set OBJF_REORDERED. If we
549 find this causes a significant slowdown in gdb then we could
550 set it in the debug symbol readers only when necessary. */
551 objfile
->flags
|= OBJF_REORDERED
;
553 init_entry_point_info (objfile
);
556 /* This function is called for every section; it finds the outer limits
557 of the line table (minimum and maximum file offset) so that the
558 mainline code can read the whole thing for efficiency. */
562 find_linenos (abfd
, asect
, vpinfo
)
567 struct coff_symfile_info
*info
;
569 file_ptr offset
, maxoff
;
571 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
572 count
= asect
->lineno_count
;
577 size
= count
* local_linesz
;
579 info
= (struct coff_symfile_info
*) vpinfo
;
580 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
581 offset
= asect
->line_filepos
;
584 if (offset
< info
->min_lineno_offset
|| info
->min_lineno_offset
== 0)
585 info
->min_lineno_offset
= offset
;
587 maxoff
= offset
+ size
;
588 if (maxoff
> info
->max_lineno_offset
)
589 info
->max_lineno_offset
= maxoff
;
593 /* The BFD for this file -- only good while we're actively reading
594 symbols into a psymtab or a symtab. */
596 static bfd
*symfile_bfd
;
598 /* Read a symbol file, after initialization by coff_symfile_init. */
602 coff_symfile_read (objfile
, mainline
)
603 struct objfile
*objfile
;
606 struct coff_symfile_info
*info
;
607 struct dbx_symfile_info
*dbxinfo
;
608 bfd
*abfd
= objfile
->obfd
;
609 coff_data_type
*cdata
= coff_data (abfd
);
610 char *name
= bfd_get_filename (abfd
);
612 unsigned int num_symbols
;
614 int stringtab_offset
;
615 struct cleanup
*back_to
;
620 info
= (struct coff_symfile_info
*) objfile
->sym_private
;
621 dbxinfo
= objfile
->sym_stab_info
;
622 symfile_bfd
= abfd
; /* Kludge for swap routines */
624 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
625 num_symbols
= bfd_get_symcount (abfd
); /* How many syms */
626 symtab_offset
= cdata
->sym_filepos
; /* Symbol table file offset */
627 stringtab_offset
= symtab_offset
+ /* String table file offset */
628 num_symbols
* cdata
->local_symesz
;
630 /* Set a few file-statics that give us specific information about
631 the particular COFF file format we're reading. */
632 local_n_btmask
= cdata
->local_n_btmask
;
633 local_n_btshft
= cdata
->local_n_btshft
;
634 local_n_tmask
= cdata
->local_n_tmask
;
635 local_n_tshift
= cdata
->local_n_tshift
;
636 local_linesz
= cdata
->local_linesz
;
637 local_symesz
= cdata
->local_symesz
;
638 local_auxesz
= cdata
->local_auxesz
;
640 /* Allocate space for raw symbol and aux entries, based on their
641 space requirements as reported by BFD. */
642 temp_sym
= (char *) xmalloc
643 (cdata
->local_symesz
+ cdata
->local_auxesz
);
644 temp_aux
= temp_sym
+ cdata
->local_symesz
;
645 back_to
= make_cleanup (free_current_contents
, &temp_sym
);
647 /* We need to know whether this is a PE file, because in PE files,
648 unlike standard COFF files, symbol values are stored as offsets
649 from the section address, rather than as absolute addresses.
650 FIXME: We should use BFD to read the symbol table, and thus avoid
653 strncmp (bfd_get_target (objfile
->obfd
), "pe", 2) == 0
654 || strncmp (bfd_get_target (objfile
->obfd
), "epoc-pe", 7) == 0;
658 /* Read the line number table, all at once. */
659 info
->min_lineno_offset
= 0;
660 info
->max_lineno_offset
= 0;
661 bfd_map_over_sections (abfd
, find_linenos
, (PTR
) info
);
663 make_cleanup (free_linetab_cleanup
, 0 /*ignore*/);
664 val
= init_lineno (abfd
, info
->min_lineno_offset
,
665 info
->max_lineno_offset
- info
->min_lineno_offset
);
667 error ("\"%s\": error reading line numbers\n", name
);
669 /* Now read the string table, all at once. */
671 make_cleanup (free_stringtab_cleanup
, 0 /*ignore*/);
672 val
= init_stringtab (abfd
, stringtab_offset
);
674 error ("\"%s\": can't get string table", name
);
676 init_minimal_symbol_collection ();
677 make_cleanup_discard_minimal_symbols ();
679 /* Now that the executable file is positioned at symbol table,
680 process it and define symbols accordingly. */
682 coff_symtab_read ((long) symtab_offset
, num_symbols
, objfile
);
684 /* Sort symbols alphabetically within each block. */
689 for (s
= objfile
->symtabs
; s
!= NULL
; s
= s
->next
)
690 sort_symtab_syms (s
);
693 /* Install any minimal symbols that have been collected as the current
694 minimal symbols for this objfile. */
696 install_minimal_symbols (objfile
);
698 bfd_map_over_sections (abfd
, coff_locate_sections
, (PTR
) info
);
702 if (!info
->stabstrsect
)
707 ("The debugging information in `%s' is corrupted.\n"
708 "The file has a `.stabs' section, but no `.stabstr' section.\n"),
710 return_to_top_level (RETURN_ERROR
);
713 /* FIXME: dubious. Why can't we use something normal like
714 bfd_get_section_contents? */
715 bfd_seek (abfd
, abfd
->where
, 0);
717 stabstrsize
= bfd_section_size (abfd
, info
->stabstrsect
);
719 coffstab_build_psymtabs (objfile
,
721 info
->textaddr
, info
->textsize
,
723 info
->stabstrsect
->filepos
, stabstrsize
);
726 do_cleanups (back_to
);
730 coff_new_init (ignore
)
731 struct objfile
*ignore
;
735 /* Perform any local cleanups required when we are done with a particular
736 objfile. I.E, we are in the process of discarding all symbol information
737 for an objfile, freeing up all memory held for it, and unlinking the
738 objfile struct from the global list of known objfiles. */
741 coff_symfile_finish (objfile
)
742 struct objfile
*objfile
;
744 if (objfile
->sym_private
!= NULL
)
746 mfree (objfile
->md
, objfile
->sym_private
);
749 /* Let stabs reader clean up */
750 stabsread_clear_cache ();
754 /* Given pointers to a symbol table in coff style exec file,
755 analyze them and create struct symtab's describing the symbols.
756 NSYMS is the number of symbols in the symbol table.
757 We read them one at a time using read_one_sym (). */
760 coff_symtab_read (symtab_offset
, nsyms
, objfile
)
763 struct objfile
*objfile
;
765 register struct context_stack
*new;
766 struct coff_symbol coff_symbol
;
767 register struct coff_symbol
*cs
= &coff_symbol
;
768 static struct internal_syment main_sym
;
769 static union internal_auxent main_aux
;
770 struct coff_symbol fcn_cs_saved
;
771 static struct internal_syment fcn_sym_saved
;
772 static union internal_auxent fcn_aux_saved
;
774 /* A .file is open. */
775 int in_source_file
= 0;
776 int next_file_symnum
= -1;
777 /* Name of the current file. */
778 char *filestring
= "";
780 int fcn_first_line
= 0;
781 CORE_ADDR fcn_first_line_addr
;
782 int fcn_last_line
= 0;
783 int fcn_start_addr
= 0;
784 long fcn_line_ptr
= 0;
788 /* Work around a stdio bug in SunOS4.1.1 (this makes me nervous....
789 it's hard to know I've really worked around it. The fix should be
790 harmless, anyway). The symptom of the bug is that the first
791 fread (in read_one_sym), will (in my example) actually get data
792 from file offset 268, when the fseek was to 264 (and ftell shows
793 264). This causes all hell to break loose. I was unable to
794 reproduce this on a short test program which operated on the same
795 file, performing (I think) the same sequence of operations.
797 It stopped happening when I put in this (former) rewind().
799 FIXME: Find out if this has been reported to Sun, whether it has
800 been fixed in a later release, etc. */
802 bfd_seek (objfile
->obfd
, 0, 0);
804 /* Position to read the symbol table. */
805 val
= bfd_seek (objfile
->obfd
, (long) symtab_offset
, 0);
807 perror_with_name (objfile
->name
);
809 current_objfile
= objfile
;
810 nlist_bfd_global
= objfile
->obfd
;
811 nlist_nsyms_global
= nsyms
;
812 last_source_file
= NULL
;
813 memset (opaque_type_chain
, 0, sizeof opaque_type_chain
);
815 if (type_vector
) /* Get rid of previous one */
816 free ((PTR
) type_vector
);
817 type_vector_length
= 160;
818 type_vector
= (struct type
**)
819 xmalloc (type_vector_length
* sizeof (struct type
*));
820 memset (type_vector
, 0, type_vector_length
* sizeof (struct type
*));
822 coff_start_symtab ("");
825 while (symnum
< nsyms
)
827 QUIT
; /* Make this command interruptable. */
829 read_one_sym (cs
, &main_sym
, &main_aux
);
831 if (cs
->c_symnum
== next_file_symnum
&& cs
->c_sclass
!= C_FILE
)
833 if (last_source_file
)
834 coff_end_symtab (objfile
);
836 coff_start_symtab ("_globals_");
837 complete_symtab ("_globals_", 0, 0);
838 /* done with all files, everything from here on out is globals */
841 /* Special case for file with type declarations only, no text. */
842 if (!last_source_file
&& SDB_TYPE (cs
->c_type
)
843 && cs
->c_secnum
== N_DEBUG
)
844 complete_symtab (filestring
, 0, 0);
846 /* Typedefs should not be treated as symbol definitions. */
847 if (ISFCN (cs
->c_type
) && cs
->c_sclass
!= C_TPDEF
)
849 /* Record all functions -- external and static -- in minsyms. */
850 tmpaddr
= cs
->c_value
+ ANOFFSET (objfile
->section_offsets
, SECT_OFF_TEXT (objfile
));
851 record_minimal_symbol (cs
->c_name
, tmpaddr
, mst_text
, objfile
);
853 fcn_line_ptr
= main_aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
;
854 fcn_start_addr
= tmpaddr
;
856 fcn_sym_saved
= main_sym
;
857 fcn_aux_saved
= main_aux
;
861 switch (cs
->c_sclass
)
870 complain (&bad_sclass_complaint
, cs
->c_name
);
874 /* c_value field contains symnum of next .file entry in table
875 or symnum of first global after last .file. */
876 next_file_symnum
= cs
->c_value
;
878 filestring
= coff_getfilename (&main_aux
);
882 /* Complete symbol table for last object file
883 containing debugging information. */
884 if (last_source_file
)
886 coff_end_symtab (objfile
);
887 coff_start_symtab (filestring
);
892 /* C_LABEL is used for labels and static functions. Including
893 it here allows gdb to see static functions when no debug
894 info is available. */
896 /* However, labels within a function can make weird backtraces,
897 so filter them out (from phdm@macqel.be). */
903 case C_THUMBSTATFUNC
:
904 if (cs
->c_name
[0] == '.')
906 if (STREQ (cs
->c_name
, ".text"))
908 /* FIXME: don't wire in ".text" as section name
910 /* Check for in_source_file deals with case of
911 a file with debugging symbols
912 followed by a later file with no symbols. */
914 complete_symtab (filestring
,
915 cs
->c_value
+ ANOFFSET (objfile
->section_offsets
, SECT_OFF_TEXT (objfile
)),
916 main_aux
.x_scn
.x_scnlen
);
919 /* flush rest of '.' symbols */
922 else if (!SDB_TYPE (cs
->c_type
)
923 && cs
->c_name
[0] == 'L'
924 && (strncmp (cs
->c_name
, "LI%", 3) == 0
925 || strncmp (cs
->c_name
, "LF%", 3) == 0
926 || strncmp (cs
->c_name
, "LC%", 3) == 0
927 || strncmp (cs
->c_name
, "LP%", 3) == 0
928 || strncmp (cs
->c_name
, "LPB%", 4) == 0
929 || strncmp (cs
->c_name
, "LBB%", 4) == 0
930 || strncmp (cs
->c_name
, "LBE%", 4) == 0
931 || strncmp (cs
->c_name
, "LPBX%", 5) == 0))
932 /* At least on a 3b1, gcc generates swbeg and string labels
933 that look like this. Ignore them. */
935 /* fall in for static symbols that don't start with '.' */
940 /* Record it in the minimal symbols regardless of
941 SDB_TYPE. This parallels what we do for other debug
942 formats, and probably is needed to make
943 print_address_symbolic work right without the (now
944 gone) "set fast-symbolic-addr off" kludge. */
946 /* FIXME: should use mst_abs, and not relocate, if absolute. */
947 enum minimal_symbol_type ms_type
;
950 if (cs
->c_secnum
== N_UNDEF
)
952 /* This is a common symbol. See if the target
953 environment knows where it has been relocated to. */
955 if (target_lookup_symbol (cs
->c_name
, &reladdr
))
957 /* Error in lookup; ignore symbol. */
961 /* The address has already been relocated; make sure that
962 objfile_relocate doesn't relocate it again. */
964 ms_type
= cs
->c_sclass
== C_EXT
965 || cs
->c_sclass
== C_THUMBEXT
?
966 mst_bss
: mst_file_bss
;
970 sec
= cs_to_section (cs
, objfile
);
971 tmpaddr
= cs
->c_value
;
972 if (cs
->c_sclass
== C_EXT
|| cs
->c_sclass
== C_THUMBEXTFUNC
973 || cs
->c_sclass
== C_THUMBEXT
)
974 tmpaddr
+= ANOFFSET (objfile
->section_offsets
, sec
);
976 if (sec
== SECT_OFF_TEXT (objfile
))
979 cs
->c_sclass
== C_EXT
|| cs
->c_sclass
== C_THUMBEXTFUNC
980 || cs
->c_sclass
== C_THUMBEXT
?
981 mst_text
: mst_file_text
;
982 #ifdef SMASH_TEXT_ADDRESS
983 if (tmpaddr
& 1) /* FIXME: delete this line */
984 SMASH_TEXT_ADDRESS (tmpaddr
);
988 if (sec
== SECT_OFF_DATA (objfile
))
991 cs
->c_sclass
== C_EXT
|| cs
->c_sclass
== C_THUMBEXT
?
992 mst_data
: mst_file_data
;
995 if (sec
== SECT_OFF_BSS (objfile
))
998 cs
->c_sclass
== C_EXT
|| cs
->c_sclass
== C_THUMBEXT
?
999 mst_data
: mst_file_data
;
1002 ms_type
= mst_unknown
;
1005 if (cs
->c_name
[0] != '@' /* Skip tdesc symbols */ )
1007 struct minimal_symbol
*msym
;
1009 msym
= prim_record_minimal_symbol_and_info
1010 (cs
->c_name
, tmpaddr
, ms_type
, (char *) cs
->c_sclass
, sec
,
1012 #ifdef COFF_MAKE_MSYMBOL_SPECIAL
1014 COFF_MAKE_MSYMBOL_SPECIAL (cs
->c_sclass
, msym
);
1017 if (SDB_TYPE (cs
->c_type
))
1020 sym
= process_coff_symbol
1021 (cs
, &main_aux
, objfile
);
1022 SYMBOL_VALUE (sym
) = tmpaddr
;
1023 SYMBOL_SECTION (sym
) = sec
;
1029 if (STREQ (cs
->c_name
, ".bf"))
1031 within_function
= 1;
1033 /* value contains address of first non-init type code */
1034 /* main_aux.x_sym.x_misc.x_lnsz.x_lnno
1035 contains line number of '{' } */
1036 if (cs
->c_naux
!= 1)
1037 complain (&bf_no_aux_complaint
, cs
->c_symnum
);
1038 fcn_first_line
= main_aux
.x_sym
.x_misc
.x_lnsz
.x_lnno
;
1039 fcn_first_line_addr
= cs
->c_value
;
1041 /* Might want to check that locals are 0 and
1042 context_stack_depth is zero, and complain if not. */
1045 new = push_context (depth
, fcn_start_addr
);
1046 fcn_cs_saved
.c_name
= getsymname (&fcn_sym_saved
);
1048 process_coff_symbol (&fcn_cs_saved
, &fcn_aux_saved
, objfile
);
1050 else if (STREQ (cs
->c_name
, ".ef"))
1052 /* the value of .ef is the address of epilogue code;
1053 not useful for gdb. */
1054 /* { main_aux.x_sym.x_misc.x_lnsz.x_lnno
1055 contains number of lines to '}' */
1057 if (context_stack_depth
<= 0)
1058 { /* We attempted to pop an empty context stack */
1059 complain (&ef_stack_complaint
, cs
->c_symnum
);
1060 within_function
= 0;
1064 new = pop_context ();
1065 /* Stack must be empty now. */
1066 if (context_stack_depth
> 0 || new == NULL
)
1068 complain (&ef_complaint
, cs
->c_symnum
);
1069 within_function
= 0;
1072 if (cs
->c_naux
!= 1)
1074 complain (&ef_no_aux_complaint
, cs
->c_symnum
);
1075 fcn_last_line
= 0x7FFFFFFF;
1079 fcn_last_line
= main_aux
.x_sym
.x_misc
.x_lnsz
.x_lnno
;
1081 /* fcn_first_line is the line number of the opening '{'.
1082 Do not record it - because it would affect gdb's idea
1083 of the line number of the first statement of the function -
1084 except for one-line functions, for which it is also the line
1085 number of all the statements and of the closing '}', and
1086 for which we do not have any other statement-line-number. */
1087 if (fcn_last_line
== 1)
1088 record_line (current_subfile
, fcn_first_line
,
1089 fcn_first_line_addr
);
1091 enter_linenos (fcn_line_ptr
, fcn_first_line
, fcn_last_line
,
1094 finish_block (new->name
, &local_symbols
, new->old_blocks
,
1096 #if defined (FUNCTION_EPILOGUE_SIZE)
1097 /* This macro should be defined only on
1099 fcn_aux_saved.x_sym.x_misc.x_fsize
1100 field is always zero.
1101 So use the .bf record information that
1102 points to the epilogue and add the size
1105 + FUNCTION_EPILOGUE_SIZE
1106 + ANOFFSET (objfile
->section_offsets
, SECT_OFF_TEXT (objfile
)),
1108 fcn_cs_saved
.c_value
1109 + fcn_aux_saved
.x_sym
.x_misc
.x_fsize
1110 + ANOFFSET (objfile
->section_offsets
, SECT_OFF_TEXT (objfile
)),
1114 within_function
= 0;
1119 if (STREQ (cs
->c_name
, ".bb"))
1121 tmpaddr
= cs
->c_value
;
1122 tmpaddr
+= ANOFFSET (objfile
->section_offsets
, SECT_OFF_TEXT (objfile
));
1123 push_context (++depth
, tmpaddr
);
1125 else if (STREQ (cs
->c_name
, ".eb"))
1127 if (context_stack_depth
<= 0)
1128 { /* We attempted to pop an empty context stack */
1129 complain (&eb_stack_complaint
, cs
->c_symnum
);
1133 new = pop_context ();
1134 if (depth
-- != new->depth
)
1136 complain (&eb_complaint
, symnum
);
1139 if (local_symbols
&& context_stack_depth
> 0)
1142 cs
->c_value
+ ANOFFSET (objfile
->section_offsets
, SECT_OFF_TEXT (objfile
));
1143 /* Make a block for the local symbols within. */
1144 finish_block (0, &local_symbols
, new->old_blocks
,
1145 new->start_addr
, tmpaddr
, objfile
);
1147 /* Now pop locals of block just finished. */
1148 local_symbols
= new->locals
;
1153 process_coff_symbol (cs
, &main_aux
, objfile
);
1158 if (last_source_file
)
1159 coff_end_symtab (objfile
);
1161 /* Patch up any opaque types (references to types that are not defined
1162 in the file where they are referenced, e.g. "struct foo *bar"). */
1163 ALL_OBJFILE_SYMTABS (objfile
, s
)
1164 patch_opaque_types (s
);
1166 current_objfile
= NULL
;
1169 /* Routines for reading headers and symbols from executable. */
1171 /* Read the next symbol, swap it, and return it in both internal_syment
1172 form, and coff_symbol form. Also return its first auxent, if any,
1173 in internal_auxent form, and skip any other auxents. */
1176 read_one_sym (cs
, sym
, aux
)
1177 register struct coff_symbol
*cs
;
1178 register struct internal_syment
*sym
;
1179 register union internal_auxent
*aux
;
1183 cs
->c_symnum
= symnum
;
1184 bfd_read (temp_sym
, local_symesz
, 1, nlist_bfd_global
);
1185 bfd_coff_swap_sym_in (symfile_bfd
, temp_sym
, (char *) sym
);
1186 cs
->c_naux
= sym
->n_numaux
& 0xff;
1187 if (cs
->c_naux
>= 1)
1189 bfd_read (temp_aux
, local_auxesz
, 1, nlist_bfd_global
);
1190 bfd_coff_swap_aux_in (symfile_bfd
, temp_aux
, sym
->n_type
, sym
->n_sclass
,
1191 0, cs
->c_naux
, (char *) aux
);
1192 /* If more than one aux entry, read past it (only the first aux
1194 for (i
= 1; i
< cs
->c_naux
; i
++)
1195 bfd_read (temp_aux
, local_auxesz
, 1, nlist_bfd_global
);
1197 cs
->c_name
= getsymname (sym
);
1198 cs
->c_value
= sym
->n_value
;
1199 cs
->c_sclass
= (sym
->n_sclass
& 0xff);
1200 cs
->c_secnum
= sym
->n_scnum
;
1201 cs
->c_type
= (unsigned) sym
->n_type
;
1202 if (!SDB_TYPE (cs
->c_type
))
1206 if (cs
->c_sclass
& 128)
1207 printf ("thumb symbol %s, class 0x%x\n", cs
->c_name
, cs
->c_sclass
);
1210 symnum
+= 1 + cs
->c_naux
;
1212 /* The PE file format stores symbol values as offsets within the
1213 section, rather than as absolute addresses. We correct that
1214 here, if the symbol has an appropriate storage class. FIXME: We
1215 should use BFD to read the symbols, rather than duplicating the
1219 switch (cs
->c_sclass
)
1223 case C_THUMBEXTFUNC
:
1228 case C_THUMBSTATFUNC
:
1234 if (cs
->c_secnum
!= 0)
1235 cs
->c_value
+= cs_section_address (cs
, symfile_bfd
);
1241 /* Support for string table handling */
1243 static char *stringtab
= NULL
;
1246 init_stringtab (abfd
, offset
)
1252 unsigned char lengthbuf
[4];
1256 /* If the file is stripped, the offset might be zero, indicating no
1257 string table. Just return with `stringtab' set to null. */
1261 if (bfd_seek (abfd
, offset
, 0) < 0)
1264 val
= bfd_read ((char *) lengthbuf
, sizeof lengthbuf
, 1, abfd
);
1265 length
= bfd_h_get_32 (symfile_bfd
, lengthbuf
);
1267 /* If no string table is needed, then the file may end immediately
1268 after the symbols. Just return with `stringtab' set to null. */
1269 if (val
!= sizeof lengthbuf
|| length
< sizeof lengthbuf
)
1272 stringtab
= (char *) xmalloc (length
);
1273 /* This is in target format (probably not very useful, and not currently
1274 used), not host format. */
1275 memcpy (stringtab
, lengthbuf
, sizeof lengthbuf
);
1276 if (length
== sizeof length
) /* Empty table -- just the count */
1279 val
= bfd_read (stringtab
+ sizeof lengthbuf
, length
- sizeof lengthbuf
, 1, abfd
);
1280 if (val
!= length
- sizeof lengthbuf
|| stringtab
[length
- 1] != '\0')
1295 free_stringtab_cleanup (void *ignore
)
1301 getsymname (symbol_entry
)
1302 struct internal_syment
*symbol_entry
;
1304 static char buffer
[SYMNMLEN
+ 1];
1307 if (symbol_entry
->_n
._n_n
._n_zeroes
== 0)
1309 /* FIXME: Probably should be detecting corrupt symbol files by
1310 seeing whether offset points to within the stringtab. */
1311 result
= stringtab
+ symbol_entry
->_n
._n_n
._n_offset
;
1315 strncpy (buffer
, symbol_entry
->_n
._n_name
, SYMNMLEN
);
1316 buffer
[SYMNMLEN
] = '\0';
1322 /* Extract the file name from the aux entry of a C_FILE symbol. Return
1323 only the last component of the name. Result is in static storage and
1324 is only good for temporary use. */
1327 coff_getfilename (aux_entry
)
1328 union internal_auxent
*aux_entry
;
1330 static char buffer
[BUFSIZ
];
1331 register char *temp
;
1334 if (aux_entry
->x_file
.x_n
.x_zeroes
== 0)
1335 strcpy (buffer
, stringtab
+ aux_entry
->x_file
.x_n
.x_offset
);
1338 strncpy (buffer
, aux_entry
->x_file
.x_fname
, FILNMLEN
);
1339 buffer
[FILNMLEN
] = '\0';
1343 /* FIXME: We should not be throwing away the information about what
1344 directory. It should go into dirname of the symtab, or some such
1346 if ((temp
= strrchr (result
, '/')) != NULL
)
1351 /* Support for line number handling. */
1353 static char *linetab
= NULL
;
1354 static long linetab_offset
;
1355 static unsigned long linetab_size
;
1357 /* Read in all the line numbers for fast lookups later. Leave them in
1358 external (unswapped) format in memory; we'll swap them as we enter
1359 them into GDB's data structures. */
1362 init_lineno (abfd
, offset
, size
)
1369 linetab_offset
= offset
;
1370 linetab_size
= size
;
1377 if (bfd_seek (abfd
, offset
, 0) < 0)
1380 /* Allocate the desired table, plus a sentinel */
1381 linetab
= (char *) xmalloc (size
+ local_linesz
);
1383 val
= bfd_read (linetab
, size
, 1, abfd
);
1387 /* Terminate it with an all-zero sentinel record */
1388 memset (linetab
+ size
, 0, local_linesz
);
1402 free_linetab_cleanup (void *ignore
)
1407 #if !defined (L_LNNO32)
1408 #define L_LNNO32(lp) ((lp)->l_lnno)
1412 enter_linenos (file_offset
, first_line
, last_line
, objfile
)
1414 register int first_line
;
1415 register int last_line
;
1416 struct objfile
*objfile
;
1418 register char *rawptr
;
1419 struct internal_lineno lptr
;
1423 if (file_offset
< linetab_offset
)
1425 complain (&lineno_complaint
, file_offset
);
1426 if (file_offset
> linetab_size
) /* Too big to be an offset? */
1428 file_offset
+= linetab_offset
; /* Try reading at that linetab offset */
1431 rawptr
= &linetab
[file_offset
- linetab_offset
];
1433 /* skip first line entry for each function */
1434 rawptr
+= local_linesz
;
1435 /* line numbers start at one for the first line of the function */
1440 bfd_coff_swap_lineno_in (symfile_bfd
, rawptr
, &lptr
);
1441 rawptr
+= local_linesz
;
1442 /* The next function, or the sentinel, will have L_LNNO32 zero; we exit. */
1443 if (L_LNNO32 (&lptr
) && L_LNNO32 (&lptr
) <= last_line
)
1444 record_line (current_subfile
, first_line
+ L_LNNO32 (&lptr
),
1446 + ANOFFSET (objfile
->section_offsets
, SECT_OFF_TEXT (objfile
)));
1453 patch_type (type
, real_type
)
1455 struct type
*real_type
;
1457 register struct type
*target
= TYPE_TARGET_TYPE (type
);
1458 register struct type
*real_target
= TYPE_TARGET_TYPE (real_type
);
1459 int field_size
= TYPE_NFIELDS (real_target
) * sizeof (struct field
);
1461 TYPE_LENGTH (target
) = TYPE_LENGTH (real_target
);
1462 TYPE_NFIELDS (target
) = TYPE_NFIELDS (real_target
);
1463 TYPE_FIELDS (target
) = (struct field
*) TYPE_ALLOC (target
, field_size
);
1465 memcpy (TYPE_FIELDS (target
), TYPE_FIELDS (real_target
), field_size
);
1467 if (TYPE_NAME (real_target
))
1469 if (TYPE_NAME (target
))
1470 free (TYPE_NAME (target
));
1471 TYPE_NAME (target
) = concat (TYPE_NAME (real_target
), NULL
);
1475 /* Patch up all appropriate typedef symbols in the opaque_type_chains
1476 so that they can be used to print out opaque data structures properly. */
1479 patch_opaque_types (s
)
1482 register struct block
*b
;
1484 register struct symbol
*real_sym
;
1486 /* Go through the per-file symbols only */
1487 b
= BLOCKVECTOR_BLOCK (BLOCKVECTOR (s
), STATIC_BLOCK
);
1488 for (i
= BLOCK_NSYMS (b
) - 1; i
>= 0; i
--)
1490 /* Find completed typedefs to use to fix opaque ones.
1491 Remove syms from the chain when their types are stored,
1492 but search the whole chain, as there may be several syms
1493 from different files with the same name. */
1494 real_sym
= BLOCK_SYM (b
, i
);
1495 if (SYMBOL_CLASS (real_sym
) == LOC_TYPEDEF
&&
1496 SYMBOL_NAMESPACE (real_sym
) == VAR_NAMESPACE
&&
1497 TYPE_CODE (SYMBOL_TYPE (real_sym
)) == TYPE_CODE_PTR
&&
1498 TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (real_sym
))) != 0)
1500 register char *name
= SYMBOL_NAME (real_sym
);
1501 register int hash
= hashname (name
);
1502 register struct symbol
*sym
, *prev
;
1505 for (sym
= opaque_type_chain
[hash
]; sym
;)
1507 if (name
[0] == SYMBOL_NAME (sym
)[0] &&
1508 STREQ (name
+ 1, SYMBOL_NAME (sym
) + 1))
1512 SYMBOL_VALUE_CHAIN (prev
) = SYMBOL_VALUE_CHAIN (sym
);
1516 opaque_type_chain
[hash
] = SYMBOL_VALUE_CHAIN (sym
);
1519 patch_type (SYMBOL_TYPE (sym
), SYMBOL_TYPE (real_sym
));
1523 sym
= SYMBOL_VALUE_CHAIN (prev
);
1527 sym
= opaque_type_chain
[hash
];
1533 sym
= SYMBOL_VALUE_CHAIN (sym
);
1540 static struct symbol
*
1541 process_coff_symbol (cs
, aux
, objfile
)
1542 register struct coff_symbol
*cs
;
1543 register union internal_auxent
*aux
;
1544 struct objfile
*objfile
;
1546 register struct symbol
*sym
1547 = (struct symbol
*) obstack_alloc (&objfile
->symbol_obstack
,
1548 sizeof (struct symbol
));
1551 memset (sym
, 0, sizeof (struct symbol
));
1553 name
= EXTERNAL_NAME (name
, objfile
->obfd
);
1554 SYMBOL_NAME (sym
) = obsavestring (name
, strlen (name
),
1555 &objfile
->symbol_obstack
);
1556 SYMBOL_LANGUAGE (sym
) = language_auto
;
1557 SYMBOL_INIT_DEMANGLED_NAME (sym
, &objfile
->symbol_obstack
);
1559 /* default assumptions */
1560 SYMBOL_VALUE (sym
) = cs
->c_value
;
1561 SYMBOL_NAMESPACE (sym
) = VAR_NAMESPACE
;
1562 SYMBOL_SECTION (sym
) = cs_to_section (cs
, objfile
);
1564 if (ISFCN (cs
->c_type
))
1566 SYMBOL_VALUE (sym
) += ANOFFSET (objfile
->section_offsets
, SECT_OFF_TEXT (objfile
));
1568 lookup_function_type (decode_function_type (cs
, cs
->c_type
, aux
));
1570 SYMBOL_CLASS (sym
) = LOC_BLOCK
;
1571 if (cs
->c_sclass
== C_STAT
|| cs
->c_sclass
== C_THUMBSTAT
1572 || cs
->c_sclass
== C_THUMBSTATFUNC
)
1573 add_symbol_to_list (sym
, &file_symbols
);
1574 else if (cs
->c_sclass
== C_EXT
|| cs
->c_sclass
== C_THUMBEXT
1575 || cs
->c_sclass
== C_THUMBEXTFUNC
)
1576 add_symbol_to_list (sym
, &global_symbols
);
1580 SYMBOL_TYPE (sym
) = decode_type (cs
, cs
->c_type
, aux
);
1581 switch (cs
->c_sclass
)
1587 SYMBOL_CLASS (sym
) = LOC_LOCAL
;
1588 add_symbol_to_list (sym
, &local_symbols
);
1592 case C_THUMBEXTFUNC
:
1594 SYMBOL_CLASS (sym
) = LOC_STATIC
;
1595 SYMBOL_VALUE_ADDRESS (sym
) = (CORE_ADDR
) cs
->c_value
;
1596 SYMBOL_VALUE_ADDRESS (sym
) += ANOFFSET (objfile
->section_offsets
, SECT_OFF_TEXT (objfile
));
1597 add_symbol_to_list (sym
, &global_symbols
);
1601 case C_THUMBSTATFUNC
:
1603 SYMBOL_CLASS (sym
) = LOC_STATIC
;
1604 SYMBOL_VALUE_ADDRESS (sym
) = (CORE_ADDR
) cs
->c_value
;
1605 SYMBOL_VALUE_ADDRESS (sym
) += ANOFFSET (objfile
->section_offsets
, SECT_OFF_TEXT (objfile
));
1606 if (within_function
)
1608 /* Static symbol of local scope */
1609 add_symbol_to_list (sym
, &local_symbols
);
1613 /* Static symbol at top level of file */
1614 add_symbol_to_list (sym
, &file_symbols
);
1618 #ifdef C_GLBLREG /* AMD coff */
1622 SYMBOL_CLASS (sym
) = LOC_REGISTER
;
1623 SYMBOL_VALUE (sym
) = SDB_REG_TO_REGNUM (cs
->c_value
);
1624 add_symbol_to_list (sym
, &local_symbols
);
1632 SYMBOL_CLASS (sym
) = LOC_ARG
;
1633 add_symbol_to_list (sym
, &local_symbols
);
1634 #if !defined (BELIEVE_PCC_PROMOTION)
1635 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
1637 /* If PCC says a parameter is a short or a char,
1638 aligned on an int boundary, realign it to the
1639 "little end" of the int. */
1640 struct type
*temptype
;
1641 temptype
= lookup_fundamental_type (current_objfile
,
1643 if (TYPE_LENGTH (SYMBOL_TYPE (sym
)) < TYPE_LENGTH (temptype
)
1644 && TYPE_CODE (SYMBOL_TYPE (sym
)) == TYPE_CODE_INT
1645 && 0 == SYMBOL_VALUE (sym
) % TYPE_LENGTH (temptype
))
1647 SYMBOL_VALUE (sym
) +=
1648 TYPE_LENGTH (temptype
)
1649 - TYPE_LENGTH (SYMBOL_TYPE (sym
));
1656 SYMBOL_CLASS (sym
) = LOC_REGPARM
;
1657 SYMBOL_VALUE (sym
) = SDB_REG_TO_REGNUM (cs
->c_value
);
1658 add_symbol_to_list (sym
, &local_symbols
);
1659 #if !defined (BELIEVE_PCC_PROMOTION)
1660 /* FIXME: This should retain the current type, since it's just
1661 a register value. gnu@adobe, 26Feb93 */
1663 /* If PCC says a parameter is a short or a char,
1664 it is really an int. */
1665 struct type
*temptype
;
1667 lookup_fundamental_type (current_objfile
, FT_INTEGER
);
1668 if (TYPE_LENGTH (SYMBOL_TYPE (sym
)) < TYPE_LENGTH (temptype
)
1669 && TYPE_CODE (SYMBOL_TYPE (sym
)) == TYPE_CODE_INT
)
1672 (TYPE_UNSIGNED (SYMBOL_TYPE (sym
))
1673 ? lookup_fundamental_type (current_objfile
,
1674 FT_UNSIGNED_INTEGER
)
1682 SYMBOL_CLASS (sym
) = LOC_TYPEDEF
;
1683 SYMBOL_NAMESPACE (sym
) = VAR_NAMESPACE
;
1685 /* If type has no name, give it one */
1686 if (TYPE_NAME (SYMBOL_TYPE (sym
)) == 0)
1688 if (TYPE_CODE (SYMBOL_TYPE (sym
)) == TYPE_CODE_PTR
1689 || TYPE_CODE (SYMBOL_TYPE (sym
)) == TYPE_CODE_FUNC
)
1691 /* If we are giving a name to a type such as "pointer to
1692 foo" or "function returning foo", we better not set
1693 the TYPE_NAME. If the program contains "typedef char
1694 *caddr_t;", we don't want all variables of type char
1695 * to print as caddr_t. This is not just a
1696 consequence of GDB's type management; CC and GCC (at
1697 least through version 2.4) both output variables of
1698 either type char * or caddr_t with the type
1699 refering to the C_TPDEF symbol for caddr_t. If a future
1700 compiler cleans this up it GDB is not ready for it
1701 yet, but if it becomes ready we somehow need to
1702 disable this check (without breaking the PCC/GCC2.4
1707 Fortunately, this check seems not to be necessary
1708 for anything except pointers or functions. */
1712 TYPE_NAME (SYMBOL_TYPE (sym
)) =
1713 concat (SYMBOL_NAME (sym
), NULL
);
1716 /* Ignore vendor section for Harris CX/UX targets. */
1717 else if (cs
->c_name
[0] == '$')
1719 #endif /* CXUX_TARGET */
1721 /* Keep track of any type which points to empty structured type,
1722 so it can be filled from a definition from another file. A
1723 simple forward reference (TYPE_CODE_UNDEF) is not an
1724 empty structured type, though; the forward references
1725 work themselves out via the magic of coff_lookup_type. */
1726 if (TYPE_CODE (SYMBOL_TYPE (sym
)) == TYPE_CODE_PTR
&&
1727 TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym
))) == 0 &&
1728 TYPE_CODE (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym
))) !=
1731 register int i
= hashname (SYMBOL_NAME (sym
));
1733 SYMBOL_VALUE_CHAIN (sym
) = opaque_type_chain
[i
];
1734 opaque_type_chain
[i
] = sym
;
1736 add_symbol_to_list (sym
, &file_symbols
);
1742 SYMBOL_CLASS (sym
) = LOC_TYPEDEF
;
1743 SYMBOL_NAMESPACE (sym
) = STRUCT_NAMESPACE
;
1745 /* Some compilers try to be helpful by inventing "fake"
1746 names for anonymous enums, structures, and unions, like
1747 "~0fake" or ".0fake". Thanks, but no thanks... */
1748 if (TYPE_TAG_NAME (SYMBOL_TYPE (sym
)) == 0)
1749 if (SYMBOL_NAME (sym
) != NULL
1750 && *SYMBOL_NAME (sym
) != '~'
1751 && *SYMBOL_NAME (sym
) != '.')
1752 TYPE_TAG_NAME (SYMBOL_TYPE (sym
)) =
1753 concat (SYMBOL_NAME (sym
), NULL
);
1755 add_symbol_to_list (sym
, &file_symbols
);
1765 /* Decode a coff type specifier; return the type that is meant. */
1767 static struct type
*
1768 decode_type (cs
, c_type
, aux
)
1769 register struct coff_symbol
*cs
;
1770 unsigned int c_type
;
1771 register union internal_auxent
*aux
;
1773 register struct type
*type
= 0;
1774 unsigned int new_c_type
;
1776 if (c_type
& ~N_BTMASK
)
1778 new_c_type
= DECREF (c_type
);
1781 type
= decode_type (cs
, new_c_type
, aux
);
1782 type
= lookup_pointer_type (type
);
1784 else if (ISFCN (c_type
))
1786 type
= decode_type (cs
, new_c_type
, aux
);
1787 type
= lookup_function_type (type
);
1789 else if (ISARY (c_type
))
1792 register unsigned short *dim
;
1793 struct type
*base_type
, *index_type
, *range_type
;
1795 /* Define an array type. */
1796 /* auxent refers to array, not base type */
1797 if (aux
->x_sym
.x_tagndx
.l
== 0)
1800 /* shift the indices down */
1801 dim
= &aux
->x_sym
.x_fcnary
.x_ary
.x_dimen
[0];
1804 for (i
= 0; *dim
&& i
< DIMNUM
- 1; i
++, dim
++)
1808 base_type
= decode_type (cs
, new_c_type
, aux
);
1809 index_type
= lookup_fundamental_type (current_objfile
, FT_INTEGER
);
1811 create_range_type ((struct type
*) NULL
, index_type
, 0, n
- 1);
1813 create_array_type ((struct type
*) NULL
, base_type
, range_type
);
1818 /* Reference to existing type. This only occurs with the
1819 struct, union, and enum types. EPI a29k coff
1820 fakes us out by producing aux entries with a nonzero
1821 x_tagndx for definitions of structs, unions, and enums, so we
1822 have to check the c_sclass field. SCO 3.2v4 cc gets confused
1823 with pointers to pointers to defined structs, and generates
1824 negative x_tagndx fields. */
1825 if (cs
->c_naux
> 0 && aux
->x_sym
.x_tagndx
.l
!= 0)
1827 if (cs
->c_sclass
!= C_STRTAG
1828 && cs
->c_sclass
!= C_UNTAG
1829 && cs
->c_sclass
!= C_ENTAG
1830 && aux
->x_sym
.x_tagndx
.l
>= 0)
1832 type
= coff_alloc_type (aux
->x_sym
.x_tagndx
.l
);
1837 complain (&tagndx_bad_complaint
, cs
->c_name
);
1838 /* And fall through to decode_base_type... */
1842 return decode_base_type (cs
, BTYPE (c_type
), aux
);
1845 /* Decode a coff type specifier for function definition;
1846 return the type that the function returns. */
1848 static struct type
*
1849 decode_function_type (cs
, c_type
, aux
)
1850 register struct coff_symbol
*cs
;
1851 unsigned int c_type
;
1852 register union internal_auxent
*aux
;
1854 if (aux
->x_sym
.x_tagndx
.l
== 0)
1855 cs
->c_naux
= 0; /* auxent refers to function, not base type */
1857 return decode_type (cs
, DECREF (c_type
), aux
);
1862 static struct type
*
1863 decode_base_type (cs
, c_type
, aux
)
1864 register struct coff_symbol
*cs
;
1865 unsigned int c_type
;
1866 register union internal_auxent
*aux
;
1873 /* shows up with "void (*foo)();" structure members */
1874 return lookup_fundamental_type (current_objfile
, FT_VOID
);
1877 /* DGUX actually defines both T_ARG and T_VOID to the same value. */
1880 /* Shows up in DGUX, I think. Not sure where. */
1881 return lookup_fundamental_type (current_objfile
, FT_VOID
); /* shouldn't show up here */
1887 /* Intel 960 COFF has this symbol and meaning. */
1888 return lookup_fundamental_type (current_objfile
, FT_VOID
);
1892 return lookup_fundamental_type (current_objfile
, FT_CHAR
);
1895 return lookup_fundamental_type (current_objfile
, FT_SHORT
);
1898 return lookup_fundamental_type (current_objfile
, FT_INTEGER
);
1901 if (cs
->c_sclass
== C_FIELD
1902 && aux
->x_sym
.x_misc
.x_lnsz
.x_size
> TARGET_LONG_BIT
)
1903 return lookup_fundamental_type (current_objfile
, FT_LONG_LONG
);
1905 return lookup_fundamental_type (current_objfile
, FT_LONG
);
1908 return lookup_fundamental_type (current_objfile
, FT_FLOAT
);
1911 return lookup_fundamental_type (current_objfile
, FT_DBL_PREC_FLOAT
);
1914 return lookup_fundamental_type (current_objfile
, FT_EXT_PREC_FLOAT
);
1917 if (cs
->c_naux
!= 1)
1919 /* anonymous structure type */
1920 type
= coff_alloc_type (cs
->c_symnum
);
1921 TYPE_CODE (type
) = TYPE_CODE_STRUCT
;
1922 TYPE_NAME (type
) = NULL
;
1923 /* This used to set the tag to "<opaque>". But I think setting it
1924 to NULL is right, and the printing code can print it as
1926 TYPE_TAG_NAME (type
) = NULL
;
1927 INIT_CPLUS_SPECIFIC (type
);
1928 TYPE_LENGTH (type
) = 0;
1929 TYPE_FIELDS (type
) = 0;
1930 TYPE_NFIELDS (type
) = 0;
1934 type
= coff_read_struct_type (cs
->c_symnum
,
1935 aux
->x_sym
.x_misc
.x_lnsz
.x_size
,
1936 aux
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
);
1941 if (cs
->c_naux
!= 1)
1943 /* anonymous union type */
1944 type
= coff_alloc_type (cs
->c_symnum
);
1945 TYPE_NAME (type
) = NULL
;
1946 /* This used to set the tag to "<opaque>". But I think setting it
1947 to NULL is right, and the printing code can print it as
1949 TYPE_TAG_NAME (type
) = NULL
;
1950 INIT_CPLUS_SPECIFIC (type
);
1951 TYPE_LENGTH (type
) = 0;
1952 TYPE_FIELDS (type
) = 0;
1953 TYPE_NFIELDS (type
) = 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
.l
);
1961 TYPE_CODE (type
) = TYPE_CODE_UNION
;
1965 if (cs
->c_naux
!= 1)
1967 /* anonymous enum type */
1968 type
= coff_alloc_type (cs
->c_symnum
);
1969 TYPE_CODE (type
) = TYPE_CODE_ENUM
;
1970 TYPE_NAME (type
) = NULL
;
1971 /* This used to set the tag to "<opaque>". But I think setting it
1972 to NULL is right, and the printing code can print it as
1974 TYPE_TAG_NAME (type
) = NULL
;
1975 TYPE_LENGTH (type
) = 0;
1976 TYPE_FIELDS (type
) = 0;
1977 TYPE_NFIELDS (type
) = 0;
1981 type
= coff_read_enum_type (cs
->c_symnum
,
1982 aux
->x_sym
.x_misc
.x_lnsz
.x_size
,
1983 aux
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
);
1988 /* shouldn't show up here */
1992 return lookup_fundamental_type (current_objfile
, FT_UNSIGNED_CHAR
);
1995 return lookup_fundamental_type (current_objfile
, FT_UNSIGNED_SHORT
);
1998 return lookup_fundamental_type (current_objfile
, FT_UNSIGNED_INTEGER
);
2001 if (cs
->c_sclass
== C_FIELD
2002 && aux
->x_sym
.x_misc
.x_lnsz
.x_size
> TARGET_LONG_BIT
)
2003 return lookup_fundamental_type (current_objfile
, FT_UNSIGNED_LONG_LONG
);
2005 return lookup_fundamental_type (current_objfile
, FT_UNSIGNED_LONG
);
2007 complain (&unexpected_type_complaint
, cs
->c_name
);
2008 return lookup_fundamental_type (current_objfile
, FT_VOID
);
2011 /* This page contains subroutines of read_type. */
2013 /* Read the description of a structure (or union type) and return an
2014 object describing the type. */
2016 static struct type
*
2017 coff_read_struct_type (index
, length
, lastsym
)
2024 struct nextfield
*next
;
2028 register struct type
*type
;
2029 register struct nextfield
*list
= 0;
2030 struct nextfield
*new;
2034 struct coff_symbol member_sym
;
2035 register struct coff_symbol
*ms
= &member_sym
;
2036 struct internal_syment sub_sym
;
2037 union internal_auxent sub_aux
;
2040 type
= coff_alloc_type (index
);
2041 TYPE_CODE (type
) = TYPE_CODE_STRUCT
;
2042 INIT_CPLUS_SPECIFIC (type
);
2043 TYPE_LENGTH (type
) = length
;
2045 while (!done
&& symnum
< lastsym
&& symnum
< nlist_nsyms_global
)
2047 read_one_sym (ms
, &sub_sym
, &sub_aux
);
2049 name
= EXTERNAL_NAME (name
, current_objfile
->obfd
);
2051 switch (ms
->c_sclass
)
2056 /* Get space to record the next field's data. */
2057 new = (struct nextfield
*) alloca (sizeof (struct nextfield
));
2061 /* Save the data. */
2065 ¤t_objfile
->symbol_obstack
);
2066 FIELD_TYPE (list
->field
) = decode_type (ms
, ms
->c_type
, &sub_aux
);
2067 FIELD_BITPOS (list
->field
) = 8 * ms
->c_value
;
2068 FIELD_BITSIZE (list
->field
) = 0;
2074 /* Get space to record the next field's data. */
2075 new = (struct nextfield
*) alloca (sizeof (struct nextfield
));
2079 /* Save the data. */
2083 ¤t_objfile
->symbol_obstack
);
2084 FIELD_TYPE (list
->field
) = decode_type (ms
, ms
->c_type
, &sub_aux
);
2085 FIELD_BITPOS (list
->field
) = ms
->c_value
;
2086 FIELD_BITSIZE (list
->field
) = sub_aux
.x_sym
.x_misc
.x_lnsz
.x_size
;
2095 /* Now create the vector of fields, and record how big it is. */
2097 TYPE_NFIELDS (type
) = nfields
;
2098 TYPE_FIELDS (type
) = (struct field
*)
2099 TYPE_ALLOC (type
, sizeof (struct field
) * nfields
);
2101 /* Copy the saved-up fields into the field vector. */
2103 for (n
= nfields
; list
; list
= list
->next
)
2104 TYPE_FIELD (type
, --n
) = list
->field
;
2109 /* Read a definition of an enumeration type,
2110 and create and return a suitable type object.
2111 Also defines the symbols that represent the values of the type. */
2114 static struct type
*
2115 coff_read_enum_type (index
, length
, lastsym
)
2120 register struct symbol
*sym
;
2121 register struct type
*type
;
2124 struct pending
**symlist
;
2125 struct coff_symbol member_sym
;
2126 register struct coff_symbol
*ms
= &member_sym
;
2127 struct internal_syment sub_sym
;
2128 union internal_auxent sub_aux
;
2129 struct pending
*osyms
, *syms
;
2133 int unsigned_enum
= 1;
2135 type
= coff_alloc_type (index
);
2136 if (within_function
)
2137 symlist
= &local_symbols
;
2139 symlist
= &file_symbols
;
2141 o_nsyms
= osyms
? osyms
->nsyms
: 0;
2143 while (!done
&& symnum
< lastsym
&& symnum
< nlist_nsyms_global
)
2145 read_one_sym (ms
, &sub_sym
, &sub_aux
);
2147 name
= EXTERNAL_NAME (name
, current_objfile
->obfd
);
2149 switch (ms
->c_sclass
)
2152 sym
= (struct symbol
*) obstack_alloc
2153 (¤t_objfile
->symbol_obstack
,
2154 sizeof (struct symbol
));
2155 memset (sym
, 0, sizeof (struct symbol
));
2158 obsavestring (name
, strlen (name
),
2159 ¤t_objfile
->symbol_obstack
);
2160 SYMBOL_CLASS (sym
) = LOC_CONST
;
2161 SYMBOL_NAMESPACE (sym
) = VAR_NAMESPACE
;
2162 SYMBOL_VALUE (sym
) = ms
->c_value
;
2163 add_symbol_to_list (sym
, symlist
);
2168 /* Sometimes the linker (on 386/ix 2.0.2 at least) screws
2169 up the count of how many symbols to read. So stop
2176 /* Now fill in the fields of the type-structure. */
2179 TYPE_LENGTH (type
) = length
;
2181 TYPE_LENGTH (type
) = TARGET_INT_BIT
/ TARGET_CHAR_BIT
; /* Assume ints */
2182 TYPE_CODE (type
) = TYPE_CODE_ENUM
;
2183 TYPE_NFIELDS (type
) = nsyms
;
2184 TYPE_FIELDS (type
) = (struct field
*)
2185 TYPE_ALLOC (type
, sizeof (struct field
) * nsyms
);
2187 /* Find the symbols for the values and put them into the type.
2188 The symbols can be found in the symlist that we put them on
2189 to cause them to be defined. osyms contains the old value
2190 of that symlist; everything up to there was defined by us. */
2191 /* Note that we preserve the order of the enum constants, so
2192 that in something like "enum {FOO, LAST_THING=FOO}" we print
2193 FOO, not LAST_THING. */
2195 for (syms
= *symlist
, n
= 0; syms
; syms
= syms
->next
)
2201 for (; j
< syms
->nsyms
; j
++, n
++)
2203 struct symbol
*xsym
= syms
->symbol
[j
];
2204 SYMBOL_TYPE (xsym
) = type
;
2205 TYPE_FIELD_NAME (type
, n
) = SYMBOL_NAME (xsym
);
2206 TYPE_FIELD_BITPOS (type
, n
) = SYMBOL_VALUE (xsym
);
2207 if (SYMBOL_VALUE (xsym
) < 0)
2209 TYPE_FIELD_BITSIZE (type
, n
) = 0;
2216 TYPE_FLAGS (type
) |= TYPE_FLAG_UNSIGNED
;
2221 /* Register our ability to parse symbols for coff BFD files. */
2223 static struct sym_fns coff_sym_fns
=
2225 bfd_target_coff_flavour
,
2226 coff_new_init
, /* sym_new_init: init anything gbl to entire symtab */
2227 coff_symfile_init
, /* sym_init: read initial info, setup for sym_read() */
2228 coff_symfile_read
, /* sym_read: read a symbol file into symtab */
2229 coff_symfile_finish
, /* sym_finish: finished with file, cleanup */
2230 default_symfile_offsets
, /* sym_offsets: xlate external to internal form */
2231 NULL
/* next: pointer to next struct sym_fns */
2235 _initialize_coffread ()
2237 add_symtab_fns (&coff_sym_fns
);