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, Boston, MA 02111-1307, USA. */
26 #include "breakpoint.h"
31 #include "gdb_string.h"
34 #include "coff/internal.h" /* Internal format of COFF symbols in BFD */
35 #include "libcoff.h" /* FIXME secret internal data from BFD */
40 #include "gdb-stabs.h"
41 #include "stabsread.h"
42 #include "complaints.h"
45 extern void _initialize_coffread
PARAMS ((void));
47 struct coff_symfile_info
{
48 file_ptr min_lineno_offset
; /* Where in file lowest line#s are */
49 file_ptr max_lineno_offset
; /* 1+last byte of line#s in file */
51 CORE_ADDR textaddr
; /* Addr of .text section. */
52 unsigned int textsize
; /* Size of .text section. */
53 struct stab_section_list
*stabsects
; /* .stab sections. */
54 asection
*stabstrsect
; /* Section pointer for .stab section */
58 /* Translate an external name string into a user-visible name. */
59 #define EXTERNAL_NAME(string, abfd) \
60 (string[0] == bfd_get_symbol_leading_char(abfd)? string+1: string)
62 /* To be an sdb debug type, type must have at least a basic or primary
63 derived type. Using this rather than checking against T_NULL is
64 said to prevent core dumps if we try to operate on Michael Bloom
67 #define SDB_TYPE(type) (BTYPE(type) | (type & N_TMASK))
69 /* Convert from an sdb register number to an internal gdb register number.
70 This should be defined in tm.h, if REGISTER_NAMES is not set up
71 to map one to one onto the sdb register numbers. */
73 #ifndef SDB_REG_TO_REGNUM
74 # define SDB_REG_TO_REGNUM(value) (value)
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 /* Complaints about various problems in the file being read */
131 struct complaint ef_complaint
=
132 {"Unmatched .ef symbol(s) ignored starting at symnum %d", 0, 0};
134 struct complaint ef_stack_complaint
=
135 {"`.ef' symbol without matching `.bf' symbol ignored starting at symnum %d", 0, 0};
137 struct complaint eb_stack_complaint
=
138 {"`.eb' symbol without matching `.bb' symbol ignored starting at symnum %d", 0, 0};
140 struct complaint bf_no_aux_complaint
=
141 {"`.bf' symbol %d has no aux entry", 0, 0};
143 struct complaint ef_no_aux_complaint
=
144 {"`.ef' symbol %d has no aux entry", 0, 0};
146 struct complaint lineno_complaint
=
147 {"Line number pointer %d lower than start of line numbers", 0, 0};
149 struct complaint unexpected_type_complaint
=
150 {"Unexpected type for symbol %s", 0, 0};
152 struct complaint bad_sclass_complaint
=
153 {"Bad n_sclass for symbol %s", 0, 0};
155 struct complaint misordered_blocks_complaint
=
156 {"Blocks out of order at address %x", 0, 0};
158 struct complaint tagndx_bad_complaint
=
159 {"Symbol table entry for %s has bad tagndx value", 0, 0};
161 struct complaint eb_complaint
=
162 {"Mismatched .eb symbol ignored starting at symnum %d", 0, 0};
164 /* Simplified internal version of coff symbol table information */
168 int c_symnum
; /* symbol number of this entry */
169 int c_naux
; /* 0 if syment only, 1 if syment + auxent, etc */
176 static struct type
*coff_read_struct_type
PARAMS ((int, int, int));
178 static struct type
*decode_base_type
PARAMS ((struct coff_symbol
*,
180 union internal_auxent
*));
182 static struct type
*decode_type
PARAMS ((struct coff_symbol
*, unsigned int,
183 union internal_auxent
*));
185 static struct type
*decode_function_type
PARAMS ((struct coff_symbol
*,
187 union internal_auxent
*));
189 static struct type
*coff_read_enum_type
PARAMS ((int, int, int));
191 static struct symbol
*process_coff_symbol
PARAMS ((struct coff_symbol
*,
192 union internal_auxent
*,
193 struct section_offsets
*,
196 static void patch_opaque_types
PARAMS ((struct symtab
*));
198 static void patch_type
PARAMS ((struct type
*, struct type
*));
200 static void enter_linenos
PARAMS ((long, int, int, struct section_offsets
*));
202 static void free_linetab
PARAMS ((void));
204 static int init_lineno
PARAMS ((bfd
*, long, int));
206 static char *getsymname
PARAMS ((struct internal_syment
*));
208 static char *coff_getfilename
PARAMS ((union internal_auxent
*));
210 static void free_stringtab
PARAMS ((void));
212 static int init_stringtab
PARAMS ((bfd
*, long));
214 static void read_one_sym
PARAMS ((struct coff_symbol
*,
215 struct internal_syment
*,
216 union internal_auxent
*));
218 static void coff_symtab_read
PARAMS ((long, int, struct section_offsets
*,
221 static void find_linenos
PARAMS ((bfd
*, sec_ptr
, PTR
));
223 static void coff_symfile_init
PARAMS ((struct objfile
*));
225 static void coff_new_init
PARAMS ((struct objfile
*));
227 static void coff_symfile_read
PARAMS ((struct objfile
*,
228 struct section_offsets
*, int));
230 static void coff_symfile_finish
PARAMS ((struct objfile
*));
232 static void record_minimal_symbol
PARAMS ((char *, CORE_ADDR
,
233 enum minimal_symbol_type
,
236 static void coff_end_symtab
PARAMS ((struct objfile
*));
238 static void complete_symtab
PARAMS ((char *, CORE_ADDR
, unsigned int));
240 static void coff_start_symtab
PARAMS ((char *));
242 static struct type
*coff_alloc_type
PARAMS ((int));
244 static struct type
**coff_lookup_type
PARAMS ((int));
246 static void coff_locate_sections
PARAMS ((bfd
*, asection
*, PTR
));
248 /* We are called once per section from coff_symfile_read. We
249 need to examine each section we are passed, check to see
250 if it is something we are interested in processing, and
251 if so, stash away some access information for the section.
253 FIXME: The section names should not be hardwired strings (what
254 should they be? I don't think most object file formats have enough
255 section flags to specify what kind of debug section it is
259 coff_locate_sections (abfd
, sectp
, csip
)
264 register struct coff_symfile_info
*csi
;
267 csi
= (struct coff_symfile_info
*) csip
;
268 name
= bfd_get_section_name (abfd
, sectp
);
269 if (STREQ (name
, ".text"))
271 csi
->textaddr
= bfd_section_vma (abfd
, sectp
);
272 csi
->textsize
+= bfd_section_size (abfd
, sectp
);
274 else if (strncmp (name
, ".text", sizeof ".text" - 1) == 0)
276 csi
->textsize
+= bfd_section_size (abfd
, sectp
);
278 else if (STREQ (name
, ".stabstr"))
280 csi
->stabstrsect
= sectp
;
282 else if (strncmp (name
, ".stab", sizeof ".stab" - 1) == 0)
286 /* We can have multiple .stab sections if linked with
288 for (s
= name
+ sizeof ".stab" - 1; *s
!= '\0'; s
++)
293 struct stab_section_list
*n
, **pn
;
295 n
= ((struct stab_section_list
*)
296 xmalloc (sizeof (struct stab_section_list
)));
299 for (pn
= &csi
->stabsects
; *pn
!= NULL
; pn
= &(*pn
)->next
)
303 /* This will be run after coffstab_build_psymtabs is called
304 in coff_symfile_read, at which point we no longer need
306 make_cleanup (free
, n
);
311 /* Return the section_offsets* that CS points to. */
312 static int cs_to_section
PARAMS ((struct coff_symbol
*, struct objfile
*));
314 struct find_targ_sec_arg
{
319 static void find_targ_sec
PARAMS ((bfd
*, asection
*, void *));
321 static void find_targ_sec (abfd
, sect
, obj
)
326 struct find_targ_sec_arg
*args
= (struct find_targ_sec_arg
*)obj
;
327 if (sect
->target_index
== args
->targ_index
)
328 *args
->resultp
= sect
;
331 /* Return the section number (SECT_OFF_*) that CS points to. */
333 cs_to_section (cs
, objfile
)
334 struct coff_symbol
*cs
;
335 struct objfile
*objfile
;
337 asection
*sect
= NULL
;
338 struct find_targ_sec_arg args
;
339 int off
= SECT_OFF_TEXT
;
341 args
.targ_index
= cs
->c_secnum
;
342 args
.resultp
= §
;
343 bfd_map_over_sections (objfile
->obfd
, find_targ_sec
, &args
);
346 /* This is the section. Figure out what SECT_OFF_* code it is. */
347 if (bfd_get_section_flags (abfd
, sect
) & SEC_CODE
)
349 else if (bfd_get_section_flags (abfd
, sect
) & SEC_LOAD
)
357 /* Return the address of the section of a COFF symbol. */
359 static CORE_ADDR cs_section_address
PARAMS ((struct coff_symbol
*, bfd
*));
362 cs_section_address (cs
, abfd
)
363 struct coff_symbol
*cs
;
366 asection
*sect
= NULL
;
367 struct find_targ_sec_arg args
;
370 args
.targ_index
= cs
->c_secnum
;
371 args
.resultp
= §
;
372 bfd_map_over_sections (abfd
, find_targ_sec
, &args
);
374 addr
= bfd_get_section_vma (objfile
->obfd
, sect
);
378 /* Look up a coff type-number index. Return the address of the slot
379 where the type for that index is stored.
380 The type-number is in INDEX.
382 This can be used for finding the type associated with that index
383 or for associating a new type with the index. */
385 static struct type
**
386 coff_lookup_type (index
)
389 if (index
>= type_vector_length
)
391 int old_vector_length
= type_vector_length
;
393 type_vector_length
*= 2;
394 if (index
/* is still */ >= type_vector_length
)
395 type_vector_length
= index
* 2;
397 type_vector
= (struct type
**)
398 xrealloc ((char *) type_vector
,
399 type_vector_length
* sizeof (struct type
*));
400 memset (&type_vector
[old_vector_length
], 0,
401 (type_vector_length
- old_vector_length
) * sizeof(struct type
*));
403 return &type_vector
[index
];
406 /* Make sure there is a type allocated for type number index
407 and return the type object.
408 This can create an empty (zeroed) type object. */
411 coff_alloc_type (index
)
414 register struct type
**type_addr
= coff_lookup_type (index
);
415 register struct type
*type
= *type_addr
;
417 /* If we are referring to a type not known at all yet,
418 allocate an empty type for it.
419 We will fill it in later if we find out how. */
422 type
= alloc_type (current_objfile
);
428 /* Start a new symtab for a new source file.
429 This is called when a COFF ".file" symbol is seen;
430 it indicates the start of data for one original source file. */
433 coff_start_symtab (name
)
437 /* We fill in the filename later. start_symtab puts
438 this pointer into last_source_file and we put it in
439 subfiles->name, which end_symtab frees; that's why
440 it must be malloc'd. */
441 savestring (name
, strlen(name
)),
442 /* We never know the directory name for COFF. */
444 /* The start address is irrelevant, since we set
445 last_source_start_addr in coff_end_symtab. */
447 record_debugformat ("COFF");
450 /* Save the vital information from when starting to read a file,
451 for use when closing off the current file.
452 NAME is the file name the symbols came from, START_ADDR is the first
453 text address for the file, and SIZE is the number of bytes of text. */
456 complete_symtab (name
, start_addr
, size
)
458 CORE_ADDR start_addr
;
461 if (last_source_file
!= NULL
)
462 free (last_source_file
);
463 last_source_file
= savestring (name
, strlen (name
));
464 current_source_start_addr
= start_addr
;
465 current_source_end_addr
= start_addr
+ size
;
467 if (current_objfile
-> ei
.entry_point
>= current_source_start_addr
&&
468 current_objfile
-> ei
.entry_point
< current_source_end_addr
)
470 current_objfile
-> ei
.entry_file_lowpc
= current_source_start_addr
;
471 current_objfile
-> ei
.entry_file_highpc
= current_source_end_addr
;
475 /* Finish the symbol definitions for one main source file,
476 close off all the lexical contexts for that file
477 (creating struct block's for them), then make the
478 struct symtab for that file and put it in the list of all such. */
481 coff_end_symtab (objfile
)
482 struct objfile
*objfile
;
484 struct symtab
*symtab
;
486 last_source_start_addr
= current_source_start_addr
;
488 symtab
= end_symtab (current_source_end_addr
, objfile
, 0);
491 free_named_symtabs (symtab
->filename
);
493 /* Reinitialize for beginning of new file. */
494 last_source_file
= NULL
;
498 record_minimal_symbol (name
, address
, type
, objfile
)
501 enum minimal_symbol_type type
;
502 struct objfile
*objfile
;
504 /* We don't want TDESC entry points in the minimal symbol table */
505 if (name
[0] == '@') return;
507 prim_record_minimal_symbol (name
, address
, type
, objfile
);
510 /* coff_symfile_init ()
511 is the coff-specific initialization routine for reading symbols.
512 It is passed a struct objfile which contains, among other things,
513 the BFD for the file whose symbols are being read, and a slot for
514 a pointer to "private data" which we fill with cookies and other
515 treats for coff_symfile_read ().
517 We will only be called if this is a COFF or COFF-like file.
518 BFD handles figuring out the format of the file, and code in symtab.c
519 uses BFD's determination to vector to us.
521 The ultimate result is a new symtab (or, FIXME, eventually a psymtab). */
524 coff_symfile_init (objfile
)
525 struct objfile
*objfile
;
527 /* Allocate struct to keep track of stab reading. */
528 objfile
->sym_stab_info
= (struct dbx_symfile_info
*)
529 xmmalloc (objfile
->md
, sizeof (struct dbx_symfile_info
));
531 memset ((PTR
) objfile
->sym_stab_info
, 0, sizeof (struct dbx_symfile_info
));
533 /* Allocate struct to keep track of the symfile */
534 objfile
->sym_private
= xmmalloc (objfile
->md
,
535 sizeof (struct coff_symfile_info
));
537 memset (objfile
->sym_private
, 0, sizeof (struct coff_symfile_info
));
539 /* COFF objects may be reordered, so set OBJF_REORDERED. If we
540 find this causes a significant slowdown in gdb then we could
541 set it in the debug symbol readers only when necessary. */
542 objfile
->flags
|= OBJF_REORDERED
;
544 init_entry_point_info (objfile
);
547 /* This function is called for every section; it finds the outer limits
548 of the line table (minimum and maximum file offset) so that the
549 mainline code can read the whole thing for efficiency. */
553 find_linenos (abfd
, asect
, vpinfo
)
558 struct coff_symfile_info
*info
;
560 file_ptr offset
, maxoff
;
562 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
563 count
= asect
->lineno_count
;
568 size
= count
* local_linesz
;
570 info
= (struct coff_symfile_info
*)vpinfo
;
571 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
572 offset
= asect
->line_filepos
;
575 if (offset
< info
->min_lineno_offset
|| info
->min_lineno_offset
== 0)
576 info
->min_lineno_offset
= offset
;
578 maxoff
= offset
+ size
;
579 if (maxoff
> info
->max_lineno_offset
)
580 info
->max_lineno_offset
= maxoff
;
584 /* The BFD for this file -- only good while we're actively reading
585 symbols into a psymtab or a symtab. */
587 static bfd
*symfile_bfd
;
589 /* Read a symbol file, after initialization by coff_symfile_init. */
593 coff_symfile_read (objfile
, section_offsets
, mainline
)
594 struct objfile
*objfile
;
595 struct section_offsets
*section_offsets
;
598 struct coff_symfile_info
*info
;
599 struct dbx_symfile_info
*dbxinfo
;
600 bfd
*abfd
= objfile
->obfd
;
601 coff_data_type
*cdata
= coff_data (abfd
);
602 char *name
= bfd_get_filename (abfd
);
606 int stringtab_offset
;
607 struct cleanup
*back_to
;
610 info
= (struct coff_symfile_info
*) objfile
-> sym_private
;
611 dbxinfo
= objfile
->sym_stab_info
;
612 symfile_bfd
= abfd
; /* Kludge for swap routines */
614 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
615 num_symbols
= bfd_get_symcount (abfd
); /* How many syms */
616 symtab_offset
= cdata
->sym_filepos
; /* Symbol table file offset */
617 stringtab_offset
= symtab_offset
+ /* String table file offset */
618 num_symbols
* cdata
->local_symesz
;
620 /* Set a few file-statics that give us specific information about
621 the particular COFF file format we're reading. */
622 local_n_btmask
= cdata
->local_n_btmask
;
623 local_n_btshft
= cdata
->local_n_btshft
;
624 local_n_tmask
= cdata
->local_n_tmask
;
625 local_n_tshift
= cdata
->local_n_tshift
;
626 local_linesz
= cdata
->local_linesz
;
627 local_symesz
= cdata
->local_symesz
;
628 local_auxesz
= cdata
->local_auxesz
;
630 /* Allocate space for raw symbol and aux entries, based on their
631 space requirements as reported by BFD. */
632 temp_sym
= (char *) xmalloc
633 (cdata
->local_symesz
+ cdata
->local_auxesz
);
634 temp_aux
= temp_sym
+ cdata
->local_symesz
;
635 back_to
= make_cleanup ((make_cleanup_func
) free_current_contents
, &temp_sym
);
637 /* We need to know whether this is a PE file, because in PE files,
638 unlike standard COFF files, symbol values are stored as offsets
639 from the section address, rather than as absolute addresses.
640 FIXME: We should use BFD to read the symbol table, and thus avoid
642 pe_file
= strncmp (bfd_get_target (objfile
->obfd
), "pe", 2) == 0;
646 /* Read the line number table, all at once. */
647 info
->min_lineno_offset
= 0;
648 info
->max_lineno_offset
= 0;
649 bfd_map_over_sections (abfd
, find_linenos
, (PTR
) info
);
651 make_cleanup ((make_cleanup_func
) free_linetab
, 0);
652 val
= init_lineno (abfd
, info
->min_lineno_offset
,
653 info
->max_lineno_offset
- info
->min_lineno_offset
);
655 error ("\"%s\": error reading line numbers\n", name
);
657 /* Now read the string table, all at once. */
659 make_cleanup ((make_cleanup_func
) free_stringtab
, 0);
660 val
= init_stringtab (abfd
, stringtab_offset
);
662 error ("\"%s\": can't get string table", name
);
664 init_minimal_symbol_collection ();
665 make_cleanup ((make_cleanup_func
) discard_minimal_symbols
, 0);
667 /* Now that the executable file is positioned at symbol table,
668 process it and define symbols accordingly. */
670 coff_symtab_read ((long) symtab_offset
, num_symbols
, section_offsets
,
673 /* Sort symbols alphabetically within each block. */
678 for (s
= objfile
-> symtabs
; s
!= NULL
; s
= s
-> next
)
679 sort_symtab_syms (s
);
682 /* Install any minimal symbols that have been collected as the current
683 minimal symbols for this objfile. */
685 install_minimal_symbols (objfile
);
687 bfd_map_over_sections (abfd
, coff_locate_sections
, (PTR
) info
);
691 if (! info
->stabstrsect
)
696 ("The debugging information in `%s' is corrupted.\n"
697 "The file has a `.stabs' section, but no `.stabstr' section.\n"),
699 return_to_top_level (RETURN_ERROR
);
702 /* FIXME: dubious. Why can't we use something normal like
703 bfd_get_section_contents? */
704 bfd_seek (abfd
, abfd
->where
, 0);
706 stabstrsize
= bfd_section_size (abfd
, info
->stabstrsect
);
708 coffstab_build_psymtabs (objfile
,
711 info
->textaddr
, info
->textsize
,
713 info
->stabstrsect
->filepos
, stabstrsize
);
716 do_cleanups (back_to
);
720 coff_new_init (ignore
)
721 struct objfile
*ignore
;
725 /* Perform any local cleanups required when we are done with a particular
726 objfile. I.E, we are in the process of discarding all symbol information
727 for an objfile, freeing up all memory held for it, and unlinking the
728 objfile struct from the global list of known objfiles. */
731 coff_symfile_finish (objfile
)
732 struct objfile
*objfile
;
734 if (objfile
-> sym_private
!= NULL
)
736 mfree (objfile
-> md
, objfile
-> sym_private
);
741 /* Given pointers to a symbol table in coff style exec file,
742 analyze them and create struct symtab's describing the symbols.
743 NSYMS is the number of symbols in the symbol table.
744 We read them one at a time using read_one_sym (). */
747 coff_symtab_read (symtab_offset
, nsyms
, section_offsets
, objfile
)
750 struct section_offsets
*section_offsets
;
751 struct objfile
*objfile
;
753 register struct context_stack
*new;
754 struct coff_symbol coff_symbol
;
755 register struct coff_symbol
*cs
= &coff_symbol
;
756 static struct internal_syment main_sym
;
757 static union internal_auxent main_aux
;
758 struct coff_symbol fcn_cs_saved
;
759 static struct internal_syment fcn_sym_saved
;
760 static union internal_auxent fcn_aux_saved
;
762 /* A .file is open. */
763 int in_source_file
= 0;
764 int next_file_symnum
= -1;
765 /* Name of the current file. */
766 char *filestring
= "";
768 int fcn_first_line
= 0;
769 CORE_ADDR fcn_first_line_addr
;
770 int fcn_last_line
= 0;
771 int fcn_start_addr
= 0;
772 long fcn_line_ptr
= 0;
776 /* Work around a stdio bug in SunOS4.1.1 (this makes me nervous....
777 it's hard to know I've really worked around it. The fix should be
778 harmless, anyway). The symptom of the bug is that the first
779 fread (in read_one_sym), will (in my example) actually get data
780 from file offset 268, when the fseek was to 264 (and ftell shows
781 264). This causes all hell to break loose. I was unable to
782 reproduce this on a short test program which operated on the same
783 file, performing (I think) the same sequence of operations.
785 It stopped happening when I put in this (former) rewind().
787 FIXME: Find out if this has been reported to Sun, whether it has
788 been fixed in a later release, etc. */
790 bfd_seek (objfile
->obfd
, 0, 0);
792 /* Position to read the symbol table. */
793 val
= bfd_seek (objfile
->obfd
, (long) symtab_offset
, 0);
795 perror_with_name (objfile
->name
);
797 current_objfile
= objfile
;
798 nlist_bfd_global
= objfile
->obfd
;
799 nlist_nsyms_global
= nsyms
;
800 last_source_file
= NULL
;
801 memset (opaque_type_chain
, 0, sizeof opaque_type_chain
);
803 if (type_vector
) /* Get rid of previous one */
804 free ((PTR
) type_vector
);
805 type_vector_length
= 160;
806 type_vector
= (struct type
**)
807 xmalloc (type_vector_length
* sizeof (struct type
*));
808 memset (type_vector
, 0, type_vector_length
* sizeof (struct type
*));
810 coff_start_symtab ("");
813 while (symnum
< nsyms
)
815 QUIT
; /* Make this command interruptable. */
817 read_one_sym (cs
, &main_sym
, &main_aux
);
819 if (cs
->c_symnum
== next_file_symnum
&& cs
->c_sclass
!= C_FILE
)
821 if (last_source_file
)
822 coff_end_symtab (objfile
);
824 coff_start_symtab ("_globals_");
825 complete_symtab ("_globals_", 0, 0);
826 /* done with all files, everything from here on out is globals */
829 /* Special case for file with type declarations only, no text. */
830 if (!last_source_file
&& SDB_TYPE (cs
->c_type
)
831 && cs
->c_secnum
== N_DEBUG
)
832 complete_symtab (filestring
, 0, 0);
834 /* Typedefs should not be treated as symbol definitions. */
835 if (ISFCN (cs
->c_type
) && cs
->c_sclass
!= C_TPDEF
)
837 /* Record all functions -- external and static -- in minsyms. */
838 tmpaddr
= cs
->c_value
+ ANOFFSET (section_offsets
, SECT_OFF_TEXT
);
839 record_minimal_symbol (cs
->c_name
, tmpaddr
, mst_text
, objfile
);
841 fcn_line_ptr
= main_aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
;
842 fcn_start_addr
= tmpaddr
;
844 fcn_sym_saved
= main_sym
;
845 fcn_aux_saved
= main_aux
;
849 switch (cs
->c_sclass
)
858 complain (&bad_sclass_complaint
, cs
->c_name
);
862 /* c_value field contains symnum of next .file entry in table
863 or symnum of first global after last .file. */
864 next_file_symnum
= cs
->c_value
;
866 filestring
= coff_getfilename (&main_aux
);
870 /* Complete symbol table for last object file
871 containing debugging information. */
872 if (last_source_file
)
874 coff_end_symtab (objfile
);
875 coff_start_symtab (filestring
);
880 /* C_LABEL is used for labels and static functions. Including
881 it here allows gdb to see static functions when no debug
882 info is available. */
884 /* However, labels within a function can make weird backtraces,
885 so filter them out (from phdm@macqel.be). */
891 case C_THUMBSTATFUNC
:
892 if (cs
->c_name
[0] == '.')
894 if (STREQ (cs
->c_name
, ".text")) {
895 /* FIXME: don't wire in ".text" as section name
897 /* Check for in_source_file deals with case of
898 a file with debugging symbols
899 followed by a later file with no symbols. */
901 complete_symtab (filestring
,
902 cs
->c_value
+ ANOFFSET (section_offsets
, SECT_OFF_TEXT
),
903 main_aux
.x_scn
.x_scnlen
);
906 /* flush rest of '.' symbols */
909 else if (!SDB_TYPE (cs
->c_type
)
910 && cs
->c_name
[0] == 'L'
911 && (strncmp (cs
->c_name
, "LI%", 3) == 0
912 || strncmp (cs
->c_name
, "LF%", 3) == 0
913 || strncmp (cs
->c_name
,"LC%",3) == 0
914 || strncmp (cs
->c_name
,"LP%",3) == 0
915 || strncmp (cs
->c_name
,"LPB%",4) == 0
916 || strncmp (cs
->c_name
,"LBB%",4) == 0
917 || strncmp (cs
->c_name
,"LBE%",4) == 0
918 || strncmp (cs
->c_name
,"LPBX%",5) == 0))
919 /* At least on a 3b1, gcc generates swbeg and string labels
920 that look like this. Ignore them. */
922 /* fall in for static symbols that don't start with '.' */
927 /* Record it in the minimal symbols regardless of
928 SDB_TYPE. This parallels what we do for other debug
929 formats, and probably is needed to make
930 print_address_symbolic work right without the (now
931 gone) "set fast-symbolic-addr off" kludge. */
933 /* FIXME: should use mst_abs, and not relocate, if absolute. */
934 enum minimal_symbol_type ms_type
;
937 if (cs
->c_secnum
== N_UNDEF
)
939 /* This is a common symbol. See if the target
940 environment knows where it has been relocated to. */
942 if (target_lookup_symbol (cs
->c_name
, &reladdr
))
944 /* Error in lookup; ignore symbol. */
948 /* The address has already been relocated; make sure that
949 objfile_relocate doesn't relocate it again. */
951 ms_type
= cs
->c_sclass
== C_EXT
952 || cs
->c_sclass
== C_THUMBEXT
?
953 mst_bss
: mst_file_bss
;
957 sec
= cs_to_section (cs
, objfile
);
958 tmpaddr
= cs
->c_value
;
959 if (cs
->c_sclass
== C_EXT
|| cs
->c_sclass
== C_THUMBEXTFUNC
960 || cs
->c_sclass
== C_THUMBEXT
)
961 tmpaddr
+= ANOFFSET (section_offsets
, sec
);
966 case SECT_OFF_RODATA
:
968 cs
->c_sclass
== C_EXT
|| cs
->c_sclass
== C_THUMBEXTFUNC
969 || cs
->c_sclass
== C_THUMBEXT
?
970 mst_text
: mst_file_text
;
971 #ifdef SMASH_TEXT_ADDRESS
972 if (tmpaddr
& 1) /* FIXME: delete this line */
973 SMASH_TEXT_ADDRESS (tmpaddr
);
978 cs
->c_sclass
== C_EXT
|| cs
->c_sclass
== C_THUMBEXT
?
979 mst_data
: mst_file_data
;
983 cs
->c_sclass
== C_EXT
|| cs
->c_sclass
== C_THUMBEXT
?
984 mst_data
: mst_file_data
;
987 ms_type
= mst_unknown
;
992 if (cs
->c_name
[0] != '@' /* Skip tdesc symbols */)
994 struct minimal_symbol
*msym
;
996 msym
= prim_record_minimal_symbol_and_info
997 (cs
->c_name
, tmpaddr
, ms_type
, (char *)cs
->c_sclass
, sec
,
999 #ifdef COFF_MAKE_MSYMBOL_SPECIAL
1001 COFF_MAKE_MSYMBOL_SPECIAL(cs
->c_sclass
, msym
);
1004 if (SDB_TYPE (cs
->c_type
))
1007 sym
= process_coff_symbol
1008 (cs
, &main_aux
, section_offsets
, objfile
);
1009 SYMBOL_VALUE (sym
) = tmpaddr
;
1010 SYMBOL_SECTION (sym
) = sec
;
1016 if (STREQ (cs
->c_name
, ".bf"))
1018 within_function
= 1;
1020 /* value contains address of first non-init type code */
1021 /* main_aux.x_sym.x_misc.x_lnsz.x_lnno
1022 contains line number of '{' } */
1023 if (cs
->c_naux
!= 1)
1024 complain (&bf_no_aux_complaint
, cs
->c_symnum
);
1025 fcn_first_line
= main_aux
.x_sym
.x_misc
.x_lnsz
.x_lnno
;
1026 fcn_first_line_addr
= cs
->c_value
;
1028 /* Might want to check that locals are 0 and
1029 context_stack_depth is zero, and complain if not. */
1032 new = push_context (depth
, fcn_start_addr
);
1033 fcn_cs_saved
.c_name
= getsymname (&fcn_sym_saved
);
1035 process_coff_symbol (&fcn_cs_saved
, &fcn_aux_saved
,
1036 section_offsets
, objfile
);
1038 else if (STREQ (cs
->c_name
, ".ef"))
1040 /* the value of .ef is the address of epilogue code;
1041 not useful for gdb. */
1042 /* { main_aux.x_sym.x_misc.x_lnsz.x_lnno
1043 contains number of lines to '}' */
1045 if (context_stack_depth
<= 0)
1046 { /* We attempted to pop an empty context stack */
1047 complain (&ef_stack_complaint
, cs
->c_symnum
);
1048 within_function
= 0;
1052 new = pop_context ();
1053 /* Stack must be empty now. */
1054 if (context_stack_depth
> 0 || new == NULL
)
1056 complain (&ef_complaint
, cs
->c_symnum
);
1057 within_function
= 0;
1060 if (cs
->c_naux
!= 1)
1062 complain (&ef_no_aux_complaint
, cs
->c_symnum
);
1063 fcn_last_line
= 0x7FFFFFFF;
1067 fcn_last_line
= main_aux
.x_sym
.x_misc
.x_lnsz
.x_lnno
;
1069 /* fcn_first_line is the line number of the opening '{'.
1070 Do not record it - because it would affect gdb's idea
1071 of the line number of the first statement of the function -
1072 except for one-line functions, for which it is also the line
1073 number of all the statements and of the closing '}', and
1074 for which we do not have any other statement-line-number. */
1075 if (fcn_last_line
== 1)
1076 record_line (current_subfile
, fcn_first_line
,
1077 fcn_first_line_addr
);
1079 enter_linenos (fcn_line_ptr
, fcn_first_line
, fcn_last_line
,
1082 finish_block (new->name
, &local_symbols
, new->old_blocks
,
1084 #if defined (FUNCTION_EPILOGUE_SIZE)
1085 /* This macro should be defined only on
1087 fcn_aux_saved.x_sym.x_misc.x_fsize
1088 field is always zero.
1089 So use the .bf record information that
1090 points to the epilogue and add the size
1093 + FUNCTION_EPILOGUE_SIZE
1094 + ANOFFSET (section_offsets
, SECT_OFF_TEXT
),
1096 fcn_cs_saved
.c_value
1097 + fcn_aux_saved
.x_sym
.x_misc
.x_fsize
1098 + ANOFFSET (section_offsets
, SECT_OFF_TEXT
),
1102 within_function
= 0;
1107 if (STREQ (cs
->c_name
, ".bb"))
1109 tmpaddr
= cs
->c_value
;
1110 tmpaddr
+= ANOFFSET (section_offsets
, SECT_OFF_TEXT
);
1111 push_context (++depth
, tmpaddr
);
1113 else if (STREQ (cs
->c_name
, ".eb"))
1115 if (context_stack_depth
<= 0)
1116 { /* We attempted to pop an empty context stack */
1117 complain (&eb_stack_complaint
, cs
->c_symnum
);
1121 new = pop_context ();
1122 if (depth
-- != new->depth
)
1124 complain (&eb_complaint
, symnum
);
1127 if (local_symbols
&& context_stack_depth
> 0)
1130 cs
->c_value
+ ANOFFSET (section_offsets
, SECT_OFF_TEXT
);
1131 /* Make a block for the local symbols within. */
1132 finish_block (0, &local_symbols
, new->old_blocks
,
1133 new->start_addr
, tmpaddr
, objfile
);
1135 /* Now pop locals of block just finished. */
1136 local_symbols
= new->locals
;
1141 process_coff_symbol (cs
, &main_aux
, section_offsets
, objfile
);
1146 if (last_source_file
)
1147 coff_end_symtab (objfile
);
1149 /* Patch up any opaque types (references to types that are not defined
1150 in the file where they are referenced, e.g. "struct foo *bar"). */
1151 ALL_OBJFILE_SYMTABS (objfile
, s
)
1152 patch_opaque_types (s
);
1154 current_objfile
= NULL
;
1157 /* Routines for reading headers and symbols from executable. */
1159 /* Read the next symbol, swap it, and return it in both internal_syment
1160 form, and coff_symbol form. Also return its first auxent, if any,
1161 in internal_auxent form, and skip any other auxents. */
1164 read_one_sym (cs
, sym
, aux
)
1165 register struct coff_symbol
*cs
;
1166 register struct internal_syment
*sym
;
1167 register union internal_auxent
*aux
;
1171 cs
->c_symnum
= symnum
;
1172 bfd_read (temp_sym
, local_symesz
, 1, nlist_bfd_global
);
1173 bfd_coff_swap_sym_in (symfile_bfd
, temp_sym
, (char *)sym
);
1174 cs
->c_naux
= sym
->n_numaux
& 0xff;
1175 if (cs
->c_naux
>= 1)
1177 bfd_read (temp_aux
, local_auxesz
, 1, nlist_bfd_global
);
1178 bfd_coff_swap_aux_in (symfile_bfd
, temp_aux
, sym
->n_type
, sym
->n_sclass
,
1179 0, cs
->c_naux
, (char *)aux
);
1180 /* If more than one aux entry, read past it (only the first aux
1182 for (i
= 1; i
< cs
->c_naux
; i
++)
1183 bfd_read (temp_aux
, local_auxesz
, 1, nlist_bfd_global
);
1185 cs
->c_name
= getsymname (sym
);
1186 cs
->c_value
= sym
->n_value
;
1187 cs
->c_sclass
= (sym
->n_sclass
& 0xff);
1188 cs
->c_secnum
= sym
->n_scnum
;
1189 cs
->c_type
= (unsigned) sym
->n_type
;
1190 if (!SDB_TYPE (cs
->c_type
))
1194 if (cs
->c_sclass
& 128)
1195 printf("thumb symbol %s, class 0x%x\n", cs
->c_name
, cs
->c_sclass
);
1198 symnum
+= 1 + cs
->c_naux
;
1200 /* The PE file format stores symbol values as offsets within the
1201 section, rather than as absolute addresses. We correct that
1202 here, if the symbol has an appropriate storage class. FIXME: We
1203 should use BFD to read the symbols, rather than duplicating the
1207 switch (cs
->c_sclass
)
1211 case C_THUMBEXTFUNC
:
1216 case C_THUMBSTATFUNC
:
1222 if (cs
->c_secnum
!= 0)
1223 cs
->c_value
+= cs_section_address (cs
, symfile_bfd
);
1229 /* Support for string table handling */
1231 static char *stringtab
= NULL
;
1234 init_stringtab (abfd
, offset
)
1240 unsigned char lengthbuf
[4];
1244 /* If the file is stripped, the offset might be zero, indicating no
1245 string table. Just return with `stringtab' set to null. */
1249 if (bfd_seek (abfd
, offset
, 0) < 0)
1252 val
= bfd_read ((char *)lengthbuf
, sizeof lengthbuf
, 1, abfd
);
1253 length
= bfd_h_get_32 (symfile_bfd
, lengthbuf
);
1255 /* If no string table is needed, then the file may end immediately
1256 after the symbols. Just return with `stringtab' set to null. */
1257 if (val
!= sizeof lengthbuf
|| length
< sizeof lengthbuf
)
1260 stringtab
= (char *) xmalloc (length
);
1261 /* This is in target format (probably not very useful, and not currently
1262 used), not host format. */
1263 memcpy (stringtab
, lengthbuf
, sizeof lengthbuf
);
1264 if (length
== sizeof length
) /* Empty table -- just the count */
1267 val
= bfd_read (stringtab
+ sizeof lengthbuf
, length
- sizeof lengthbuf
, 1, abfd
);
1268 if (val
!= length
- sizeof lengthbuf
|| stringtab
[length
- 1] != '\0')
1283 getsymname (symbol_entry
)
1284 struct internal_syment
*symbol_entry
;
1286 static char buffer
[SYMNMLEN
+1];
1289 if (symbol_entry
->_n
._n_n
._n_zeroes
== 0)
1291 /* FIXME: Probably should be detecting corrupt symbol files by
1292 seeing whether offset points to within the stringtab. */
1293 result
= stringtab
+ symbol_entry
->_n
._n_n
._n_offset
;
1297 strncpy (buffer
, symbol_entry
->_n
._n_name
, SYMNMLEN
);
1298 buffer
[SYMNMLEN
] = '\0';
1304 /* Extract the file name from the aux entry of a C_FILE symbol. Return
1305 only the last component of the name. Result is in static storage and
1306 is only good for temporary use. */
1309 coff_getfilename (aux_entry
)
1310 union internal_auxent
*aux_entry
;
1312 static char buffer
[BUFSIZ
];
1313 register char *temp
;
1316 if (aux_entry
->x_file
.x_n
.x_zeroes
== 0)
1317 strcpy (buffer
, stringtab
+ aux_entry
->x_file
.x_n
.x_offset
);
1320 strncpy (buffer
, aux_entry
->x_file
.x_fname
, FILNMLEN
);
1321 buffer
[FILNMLEN
] = '\0';
1325 /* FIXME: We should not be throwing away the information about what
1326 directory. It should go into dirname of the symtab, or some such
1328 if ((temp
= strrchr (result
, '/')) != NULL
)
1333 /* Support for line number handling. */
1335 static char *linetab
= NULL
;
1336 static long linetab_offset
;
1337 static unsigned long linetab_size
;
1339 /* Read in all the line numbers for fast lookups later. Leave them in
1340 external (unswapped) format in memory; we'll swap them as we enter
1341 them into GDB's data structures. */
1344 init_lineno (abfd
, offset
, size
)
1351 linetab_offset
= offset
;
1352 linetab_size
= size
;
1359 if (bfd_seek (abfd
, offset
, 0) < 0)
1362 /* Allocate the desired table, plus a sentinel */
1363 linetab
= (char *) xmalloc (size
+ local_linesz
);
1365 val
= bfd_read (linetab
, size
, 1, abfd
);
1369 /* Terminate it with an all-zero sentinel record */
1370 memset (linetab
+ size
, 0, local_linesz
);
1383 #if !defined (L_LNNO32)
1384 #define L_LNNO32(lp) ((lp)->l_lnno)
1388 enter_linenos (file_offset
, first_line
, last_line
, section_offsets
)
1390 register int first_line
;
1391 register int last_line
;
1392 struct section_offsets
*section_offsets
;
1394 register char *rawptr
;
1395 struct internal_lineno lptr
;
1399 if (file_offset
< linetab_offset
)
1401 complain (&lineno_complaint
, file_offset
);
1402 if (file_offset
> linetab_size
) /* Too big to be an offset? */
1404 file_offset
+= linetab_offset
; /* Try reading at that linetab offset */
1407 rawptr
= &linetab
[file_offset
- linetab_offset
];
1409 /* skip first line entry for each function */
1410 rawptr
+= local_linesz
;
1411 /* line numbers start at one for the first line of the function */
1415 bfd_coff_swap_lineno_in (symfile_bfd
, rawptr
, &lptr
);
1416 rawptr
+= local_linesz
;
1417 /* The next function, or the sentinel, will have L_LNNO32 zero; we exit. */
1418 if (L_LNNO32 (&lptr
) && L_LNNO32 (&lptr
) <= last_line
)
1419 record_line (current_subfile
, first_line
+ L_LNNO32 (&lptr
),
1421 + ANOFFSET (section_offsets
, SECT_OFF_TEXT
));
1428 patch_type (type
, real_type
)
1430 struct type
*real_type
;
1432 register struct type
*target
= TYPE_TARGET_TYPE (type
);
1433 register struct type
*real_target
= TYPE_TARGET_TYPE (real_type
);
1434 int field_size
= TYPE_NFIELDS (real_target
) * sizeof (struct field
);
1436 TYPE_LENGTH (target
) = TYPE_LENGTH (real_target
);
1437 TYPE_NFIELDS (target
) = TYPE_NFIELDS (real_target
);
1438 TYPE_FIELDS (target
) = (struct field
*) TYPE_ALLOC (target
, field_size
);
1440 memcpy (TYPE_FIELDS (target
), TYPE_FIELDS (real_target
), field_size
);
1442 if (TYPE_NAME (real_target
))
1444 if (TYPE_NAME (target
))
1445 free (TYPE_NAME (target
));
1446 TYPE_NAME (target
) = concat (TYPE_NAME (real_target
), NULL
);
1450 /* Patch up all appropriate typedef symbols in the opaque_type_chains
1451 so that they can be used to print out opaque data structures properly. */
1454 patch_opaque_types (s
)
1457 register struct block
*b
;
1459 register struct symbol
*real_sym
;
1461 /* Go through the per-file symbols only */
1462 b
= BLOCKVECTOR_BLOCK (BLOCKVECTOR (s
), STATIC_BLOCK
);
1463 for (i
= BLOCK_NSYMS (b
) - 1; i
>= 0; i
--)
1465 /* Find completed typedefs to use to fix opaque ones.
1466 Remove syms from the chain when their types are stored,
1467 but search the whole chain, as there may be several syms
1468 from different files with the same name. */
1469 real_sym
= BLOCK_SYM (b
, i
);
1470 if (SYMBOL_CLASS (real_sym
) == LOC_TYPEDEF
&&
1471 SYMBOL_NAMESPACE (real_sym
) == VAR_NAMESPACE
&&
1472 TYPE_CODE (SYMBOL_TYPE (real_sym
)) == TYPE_CODE_PTR
&&
1473 TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (real_sym
))) != 0)
1475 register char *name
= SYMBOL_NAME (real_sym
);
1476 register int hash
= hashname (name
);
1477 register struct symbol
*sym
, *prev
;
1480 for (sym
= opaque_type_chain
[hash
]; sym
;)
1482 if (name
[0] == SYMBOL_NAME (sym
)[0] &&
1483 STREQ (name
+ 1, SYMBOL_NAME (sym
) + 1))
1487 SYMBOL_VALUE_CHAIN (prev
) = SYMBOL_VALUE_CHAIN (sym
);
1491 opaque_type_chain
[hash
] = SYMBOL_VALUE_CHAIN (sym
);
1494 patch_type (SYMBOL_TYPE (sym
), SYMBOL_TYPE (real_sym
));
1498 sym
= SYMBOL_VALUE_CHAIN (prev
);
1502 sym
= opaque_type_chain
[hash
];
1508 sym
= SYMBOL_VALUE_CHAIN (sym
);
1515 static struct symbol
*
1516 process_coff_symbol (cs
, aux
, section_offsets
, objfile
)
1517 register struct coff_symbol
*cs
;
1518 register union internal_auxent
*aux
;
1519 struct section_offsets
*section_offsets
;
1520 struct objfile
*objfile
;
1522 register struct symbol
*sym
1523 = (struct symbol
*) obstack_alloc (&objfile
->symbol_obstack
,
1524 sizeof (struct symbol
));
1527 memset (sym
, 0, sizeof (struct symbol
));
1529 name
= EXTERNAL_NAME (name
, objfile
->obfd
);
1530 SYMBOL_NAME (sym
) = obsavestring (name
, strlen (name
),
1531 &objfile
->symbol_obstack
);
1532 SYMBOL_LANGUAGE (sym
) = language_auto
;
1533 SYMBOL_INIT_DEMANGLED_NAME (sym
, &objfile
->symbol_obstack
);
1535 /* default assumptions */
1536 SYMBOL_VALUE (sym
) = cs
->c_value
;
1537 SYMBOL_NAMESPACE (sym
) = VAR_NAMESPACE
;
1538 SYMBOL_SECTION (sym
) = cs_to_section (cs
, objfile
);
1540 if (ISFCN (cs
->c_type
))
1542 SYMBOL_VALUE (sym
) += ANOFFSET (section_offsets
, SECT_OFF_TEXT
);
1544 lookup_function_type (decode_function_type (cs
, cs
->c_type
, aux
));
1546 SYMBOL_CLASS (sym
) = LOC_BLOCK
;
1547 if (cs
->c_sclass
== C_STAT
|| cs
->c_sclass
== C_THUMBSTAT
1548 || cs
->c_sclass
== C_THUMBSTATFUNC
)
1549 add_symbol_to_list (sym
, &file_symbols
);
1550 else if (cs
->c_sclass
== C_EXT
|| cs
->c_sclass
== C_THUMBEXT
1551 || cs
->c_sclass
== C_THUMBEXTFUNC
)
1552 add_symbol_to_list (sym
, &global_symbols
);
1556 SYMBOL_TYPE (sym
) = decode_type (cs
, cs
->c_type
, aux
);
1557 switch (cs
->c_sclass
)
1563 SYMBOL_CLASS (sym
) = LOC_LOCAL
;
1564 add_symbol_to_list (sym
, &local_symbols
);
1568 case C_THUMBEXTFUNC
:
1570 SYMBOL_CLASS (sym
) = LOC_STATIC
;
1571 SYMBOL_VALUE_ADDRESS (sym
) = (CORE_ADDR
) cs
->c_value
;
1572 SYMBOL_VALUE_ADDRESS (sym
) += ANOFFSET (section_offsets
, SECT_OFF_TEXT
);
1573 add_symbol_to_list (sym
, &global_symbols
);
1577 case C_THUMBSTATFUNC
:
1579 SYMBOL_CLASS (sym
) = LOC_STATIC
;
1580 SYMBOL_VALUE_ADDRESS (sym
) = (CORE_ADDR
) cs
->c_value
;
1581 SYMBOL_VALUE_ADDRESS (sym
) += ANOFFSET (section_offsets
, SECT_OFF_TEXT
);
1582 if (within_function
) {
1583 /* Static symbol of local scope */
1584 add_symbol_to_list (sym
, &local_symbols
);
1587 /* Static symbol at top level of file */
1588 add_symbol_to_list (sym
, &file_symbols
);
1592 #ifdef C_GLBLREG /* AMD coff */
1596 SYMBOL_CLASS (sym
) = LOC_REGISTER
;
1597 SYMBOL_VALUE (sym
) = SDB_REG_TO_REGNUM(cs
->c_value
);
1598 add_symbol_to_list (sym
, &local_symbols
);
1606 SYMBOL_CLASS (sym
) = LOC_ARG
;
1607 add_symbol_to_list (sym
, &local_symbols
);
1608 #if !defined (BELIEVE_PCC_PROMOTION)
1609 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
1611 /* If PCC says a parameter is a short or a char,
1612 aligned on an int boundary, realign it to the
1613 "little end" of the int. */
1614 struct type
*temptype
;
1615 temptype
= lookup_fundamental_type (current_objfile
,
1617 if (TYPE_LENGTH (SYMBOL_TYPE (sym
)) < TYPE_LENGTH (temptype
)
1618 && TYPE_CODE (SYMBOL_TYPE (sym
)) == TYPE_CODE_INT
1619 && 0 == SYMBOL_VALUE (sym
) % TYPE_LENGTH (temptype
))
1621 SYMBOL_VALUE (sym
) +=
1622 TYPE_LENGTH (temptype
)
1623 - TYPE_LENGTH (SYMBOL_TYPE (sym
));
1630 SYMBOL_CLASS (sym
) = LOC_REGPARM
;
1631 SYMBOL_VALUE (sym
) = SDB_REG_TO_REGNUM(cs
->c_value
);
1632 add_symbol_to_list (sym
, &local_symbols
);
1633 #if !defined (BELIEVE_PCC_PROMOTION)
1634 /* FIXME: This should retain the current type, since it's just
1635 a register value. gnu@adobe, 26Feb93 */
1637 /* If PCC says a parameter is a short or a char,
1638 it is really an int. */
1639 struct type
*temptype
;
1641 lookup_fundamental_type (current_objfile
, FT_INTEGER
);
1642 if (TYPE_LENGTH (SYMBOL_TYPE (sym
)) < TYPE_LENGTH (temptype
)
1643 && TYPE_CODE (SYMBOL_TYPE (sym
)) == TYPE_CODE_INT
)
1646 (TYPE_UNSIGNED (SYMBOL_TYPE (sym
))
1647 ? lookup_fundamental_type (current_objfile
,
1648 FT_UNSIGNED_INTEGER
)
1656 SYMBOL_CLASS (sym
) = LOC_TYPEDEF
;
1657 SYMBOL_NAMESPACE (sym
) = VAR_NAMESPACE
;
1659 /* If type has no name, give it one */
1660 if (TYPE_NAME (SYMBOL_TYPE (sym
)) == 0)
1662 if (TYPE_CODE (SYMBOL_TYPE (sym
)) == TYPE_CODE_PTR
1663 || TYPE_CODE (SYMBOL_TYPE (sym
)) == TYPE_CODE_FUNC
)
1665 /* If we are giving a name to a type such as "pointer to
1666 foo" or "function returning foo", we better not set
1667 the TYPE_NAME. If the program contains "typedef char
1668 *caddr_t;", we don't want all variables of type char
1669 * to print as caddr_t. This is not just a
1670 consequence of GDB's type management; CC and GCC (at
1671 least through version 2.4) both output variables of
1672 either type char * or caddr_t with the type
1673 refering to the C_TPDEF symbol for caddr_t. If a future
1674 compiler cleans this up it GDB is not ready for it
1675 yet, but if it becomes ready we somehow need to
1676 disable this check (without breaking the PCC/GCC2.4
1681 Fortunately, this check seems not to be necessary
1682 for anything except pointers or functions. */
1686 TYPE_NAME (SYMBOL_TYPE (sym
)) =
1687 concat (SYMBOL_NAME (sym
), NULL
);
1690 /* Ignore vendor section for Harris CX/UX targets. */
1691 else if (cs
->c_name
[0] == '$')
1693 #endif /* CXUX_TARGET */
1695 /* Keep track of any type which points to empty structured type,
1696 so it can be filled from a definition from another file. A
1697 simple forward reference (TYPE_CODE_UNDEF) is not an
1698 empty structured type, though; the forward references
1699 work themselves out via the magic of coff_lookup_type. */
1700 if (TYPE_CODE (SYMBOL_TYPE (sym
)) == TYPE_CODE_PTR
&&
1701 TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym
))) == 0 &&
1702 TYPE_CODE (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym
))) !=
1705 register int i
= hashname (SYMBOL_NAME (sym
));
1707 SYMBOL_VALUE_CHAIN (sym
) = opaque_type_chain
[i
];
1708 opaque_type_chain
[i
] = sym
;
1710 add_symbol_to_list (sym
, &file_symbols
);
1716 SYMBOL_CLASS (sym
) = LOC_TYPEDEF
;
1717 SYMBOL_NAMESPACE (sym
) = STRUCT_NAMESPACE
;
1719 /* Some compilers try to be helpful by inventing "fake"
1720 names for anonymous enums, structures, and unions, like
1721 "~0fake" or ".0fake". Thanks, but no thanks... */
1722 if (TYPE_TAG_NAME (SYMBOL_TYPE (sym
)) == 0)
1723 if (SYMBOL_NAME(sym
) != NULL
1724 && *SYMBOL_NAME(sym
) != '~'
1725 && *SYMBOL_NAME(sym
) != '.')
1726 TYPE_TAG_NAME (SYMBOL_TYPE (sym
)) =
1727 concat (SYMBOL_NAME (sym
), NULL
);
1729 add_symbol_to_list (sym
, &file_symbols
);
1739 /* Decode a coff type specifier; return the type that is meant. */
1741 static struct type
*
1742 decode_type (cs
, c_type
, aux
)
1743 register struct coff_symbol
*cs
;
1744 unsigned int c_type
;
1745 register union internal_auxent
*aux
;
1747 register struct type
*type
= 0;
1748 unsigned int new_c_type
;
1750 if (c_type
& ~N_BTMASK
)
1752 new_c_type
= DECREF (c_type
);
1755 type
= decode_type (cs
, new_c_type
, aux
);
1756 type
= lookup_pointer_type (type
);
1758 else if (ISFCN (c_type
))
1760 type
= decode_type (cs
, new_c_type
, aux
);
1761 type
= lookup_function_type (type
);
1763 else if (ISARY (c_type
))
1766 register unsigned short *dim
;
1767 struct type
*base_type
, *index_type
, *range_type
;
1769 /* Define an array type. */
1770 /* auxent refers to array, not base type */
1771 if (aux
->x_sym
.x_tagndx
.l
== 0)
1774 /* shift the indices down */
1775 dim
= &aux
->x_sym
.x_fcnary
.x_ary
.x_dimen
[0];
1778 for (i
= 0; *dim
&& i
< DIMNUM
- 1; i
++, dim
++)
1782 base_type
= decode_type (cs
, new_c_type
, aux
);
1783 index_type
= lookup_fundamental_type (current_objfile
, FT_INTEGER
);
1785 create_range_type ((struct type
*) NULL
, index_type
, 0, n
- 1);
1787 create_array_type ((struct type
*) NULL
, base_type
, range_type
);
1792 /* Reference to existing type. This only occurs with the
1793 struct, union, and enum types. EPI a29k coff
1794 fakes us out by producing aux entries with a nonzero
1795 x_tagndx for definitions of structs, unions, and enums, so we
1796 have to check the c_sclass field. SCO 3.2v4 cc gets confused
1797 with pointers to pointers to defined structs, and generates
1798 negative x_tagndx fields. */
1799 if (cs
->c_naux
> 0 && aux
->x_sym
.x_tagndx
.l
!= 0)
1801 if (cs
->c_sclass
!= C_STRTAG
1802 && cs
->c_sclass
!= C_UNTAG
1803 && cs
->c_sclass
!= C_ENTAG
1804 && aux
->x_sym
.x_tagndx
.l
>= 0)
1806 type
= coff_alloc_type (aux
->x_sym
.x_tagndx
.l
);
1811 complain (&tagndx_bad_complaint
, cs
->c_name
);
1812 /* And fall through to decode_base_type... */
1816 return decode_base_type (cs
, BTYPE (c_type
), aux
);
1819 /* Decode a coff type specifier for function definition;
1820 return the type that the function returns. */
1822 static struct type
*
1823 decode_function_type (cs
, c_type
, aux
)
1824 register struct coff_symbol
*cs
;
1825 unsigned int c_type
;
1826 register union internal_auxent
*aux
;
1828 if (aux
->x_sym
.x_tagndx
.l
== 0)
1829 cs
->c_naux
= 0; /* auxent refers to function, not base type */
1831 return decode_type (cs
, DECREF (c_type
), aux
);
1836 static struct type
*
1837 decode_base_type (cs
, c_type
, aux
)
1838 register struct coff_symbol
*cs
;
1839 unsigned int c_type
;
1840 register union internal_auxent
*aux
;
1847 /* shows up with "void (*foo)();" structure members */
1848 return lookup_fundamental_type (current_objfile
, FT_VOID
);
1851 /* DGUX actually defines both T_ARG and T_VOID to the same value. */
1854 /* Shows up in DGUX, I think. Not sure where. */
1855 return lookup_fundamental_type (current_objfile
, FT_VOID
); /* shouldn't show up here */
1861 /* Intel 960 COFF has this symbol and meaning. */
1862 return lookup_fundamental_type (current_objfile
, FT_VOID
);
1866 return lookup_fundamental_type (current_objfile
, FT_CHAR
);
1869 return lookup_fundamental_type (current_objfile
, FT_SHORT
);
1872 return lookup_fundamental_type (current_objfile
, FT_INTEGER
);
1875 if (cs
->c_sclass
== C_FIELD
1876 && aux
->x_sym
.x_misc
.x_lnsz
.x_size
> TARGET_LONG_BIT
)
1877 return lookup_fundamental_type (current_objfile
, FT_LONG_LONG
);
1879 return lookup_fundamental_type (current_objfile
, FT_LONG
);
1882 return lookup_fundamental_type (current_objfile
, FT_FLOAT
);
1885 return lookup_fundamental_type (current_objfile
, FT_DBL_PREC_FLOAT
);
1888 return lookup_fundamental_type (current_objfile
, FT_EXT_PREC_FLOAT
);
1891 if (cs
->c_naux
!= 1)
1893 /* anonymous structure type */
1894 type
= coff_alloc_type (cs
->c_symnum
);
1895 TYPE_CODE (type
) = TYPE_CODE_STRUCT
;
1896 TYPE_NAME (type
) = NULL
;
1897 /* This used to set the tag to "<opaque>". But I think setting it
1898 to NULL is right, and the printing code can print it as
1900 TYPE_TAG_NAME (type
) = NULL
;
1901 INIT_CPLUS_SPECIFIC(type
);
1902 TYPE_LENGTH (type
) = 0;
1903 TYPE_FIELDS (type
) = 0;
1904 TYPE_NFIELDS (type
) = 0;
1908 type
= coff_read_struct_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 if (cs
->c_naux
!= 1)
1917 /* anonymous union type */
1918 type
= coff_alloc_type (cs
->c_symnum
);
1919 TYPE_NAME (type
) = NULL
;
1920 /* This used to set the tag to "<opaque>". But I think setting it
1921 to NULL is right, and the printing code can print it as
1923 TYPE_TAG_NAME (type
) = NULL
;
1924 INIT_CPLUS_SPECIFIC(type
);
1925 TYPE_LENGTH (type
) = 0;
1926 TYPE_FIELDS (type
) = 0;
1927 TYPE_NFIELDS (type
) = 0;
1931 type
= coff_read_struct_type (cs
->c_symnum
,
1932 aux
->x_sym
.x_misc
.x_lnsz
.x_size
,
1933 aux
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
);
1935 TYPE_CODE (type
) = TYPE_CODE_UNION
;
1939 if (cs
->c_naux
!= 1)
1941 /* anonymous enum type */
1942 type
= coff_alloc_type (cs
->c_symnum
);
1943 TYPE_CODE (type
) = TYPE_CODE_ENUM
;
1944 TYPE_NAME (type
) = NULL
;
1945 /* This used to set the tag to "<opaque>". But I think setting it
1946 to NULL is right, and the printing code can print it as
1948 TYPE_TAG_NAME (type
) = NULL
;
1949 TYPE_LENGTH (type
) = 0;
1950 TYPE_FIELDS (type
) = 0;
1951 TYPE_NFIELDS(type
) = 0;
1955 type
= coff_read_enum_type (cs
->c_symnum
,
1956 aux
->x_sym
.x_misc
.x_lnsz
.x_size
,
1957 aux
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
);
1962 /* shouldn't show up here */
1966 return lookup_fundamental_type (current_objfile
, FT_UNSIGNED_CHAR
);
1969 return lookup_fundamental_type (current_objfile
, FT_UNSIGNED_SHORT
);
1972 return lookup_fundamental_type (current_objfile
, FT_UNSIGNED_INTEGER
);
1975 if (cs
->c_sclass
== C_FIELD
1976 && aux
->x_sym
.x_misc
.x_lnsz
.x_size
> TARGET_LONG_BIT
)
1977 return lookup_fundamental_type (current_objfile
, FT_UNSIGNED_LONG_LONG
);
1979 return lookup_fundamental_type (current_objfile
, FT_UNSIGNED_LONG
);
1981 complain (&unexpected_type_complaint
, cs
->c_name
);
1982 return lookup_fundamental_type (current_objfile
, FT_VOID
);
1985 /* This page contains subroutines of read_type. */
1987 /* Read the description of a structure (or union type) and return an
1988 object describing the type. */
1990 static struct type
*
1991 coff_read_struct_type (index
, length
, lastsym
)
1998 struct nextfield
*next
;
2002 register struct type
*type
;
2003 register struct nextfield
*list
= 0;
2004 struct nextfield
*new;
2008 struct coff_symbol member_sym
;
2009 register struct coff_symbol
*ms
= &member_sym
;
2010 struct internal_syment sub_sym
;
2011 union internal_auxent sub_aux
;
2014 type
= coff_alloc_type (index
);
2015 TYPE_CODE (type
) = TYPE_CODE_STRUCT
;
2016 INIT_CPLUS_SPECIFIC(type
);
2017 TYPE_LENGTH (type
) = length
;
2019 while (!done
&& symnum
< lastsym
&& symnum
< nlist_nsyms_global
)
2021 read_one_sym (ms
, &sub_sym
, &sub_aux
);
2023 name
= EXTERNAL_NAME (name
, current_objfile
->obfd
);
2025 switch (ms
->c_sclass
)
2030 /* Get space to record the next field's data. */
2031 new = (struct nextfield
*) alloca (sizeof (struct nextfield
));
2035 /* Save the data. */
2039 ¤t_objfile
->symbol_obstack
);
2040 FIELD_TYPE (list
->field
) = decode_type (ms
, ms
->c_type
, &sub_aux
);
2041 FIELD_BITPOS (list
->field
) = 8 * ms
->c_value
;
2042 FIELD_BITSIZE (list
->field
) = 0;
2048 /* Get space to record the next field's data. */
2049 new = (struct nextfield
*) alloca (sizeof (struct nextfield
));
2053 /* Save the data. */
2057 ¤t_objfile
->symbol_obstack
);
2058 FIELD_TYPE (list
->field
) = decode_type (ms
, ms
->c_type
, &sub_aux
);
2059 FIELD_BITPOS (list
->field
) = ms
->c_value
;
2060 FIELD_BITSIZE (list
->field
) = sub_aux
.x_sym
.x_misc
.x_lnsz
.x_size
;
2069 /* Now create the vector of fields, and record how big it is. */
2071 TYPE_NFIELDS (type
) = nfields
;
2072 TYPE_FIELDS (type
) = (struct field
*)
2073 TYPE_ALLOC (type
, sizeof (struct field
) * nfields
);
2075 /* Copy the saved-up fields into the field vector. */
2077 for (n
= nfields
; list
; list
= list
->next
)
2078 TYPE_FIELD (type
, --n
) = list
->field
;
2083 /* Read a definition of an enumeration type,
2084 and create and return a suitable type object.
2085 Also defines the symbols that represent the values of the type. */
2088 static struct type
*
2089 coff_read_enum_type (index
, length
, lastsym
)
2094 register struct symbol
*sym
;
2095 register struct type
*type
;
2098 struct pending
**symlist
;
2099 struct coff_symbol member_sym
;
2100 register struct coff_symbol
*ms
= &member_sym
;
2101 struct internal_syment sub_sym
;
2102 union internal_auxent sub_aux
;
2103 struct pending
*osyms
, *syms
;
2107 int unsigned_enum
= 1;
2109 type
= coff_alloc_type (index
);
2110 if (within_function
)
2111 symlist
= &local_symbols
;
2113 symlist
= &file_symbols
;
2115 o_nsyms
= osyms
? osyms
->nsyms
: 0;
2117 while (!done
&& symnum
< lastsym
&& symnum
< nlist_nsyms_global
)
2119 read_one_sym (ms
, &sub_sym
, &sub_aux
);
2121 name
= EXTERNAL_NAME (name
, current_objfile
->obfd
);
2123 switch (ms
->c_sclass
)
2126 sym
= (struct symbol
*) obstack_alloc
2127 (¤t_objfile
->symbol_obstack
,
2128 sizeof (struct symbol
));
2129 memset (sym
, 0, sizeof (struct symbol
));
2132 obsavestring (name
, strlen (name
),
2133 ¤t_objfile
->symbol_obstack
);
2134 SYMBOL_CLASS (sym
) = LOC_CONST
;
2135 SYMBOL_NAMESPACE (sym
) = VAR_NAMESPACE
;
2136 SYMBOL_VALUE (sym
) = ms
->c_value
;
2137 add_symbol_to_list (sym
, symlist
);
2142 /* Sometimes the linker (on 386/ix 2.0.2 at least) screws
2143 up the count of how many symbols to read. So stop
2150 /* Now fill in the fields of the type-structure. */
2153 TYPE_LENGTH (type
) = length
;
2155 TYPE_LENGTH (type
) = TARGET_INT_BIT
/ TARGET_CHAR_BIT
; /* Assume ints */
2156 TYPE_CODE (type
) = TYPE_CODE_ENUM
;
2157 TYPE_NFIELDS (type
) = nsyms
;
2158 TYPE_FIELDS (type
) = (struct field
*)
2159 TYPE_ALLOC (type
, sizeof (struct field
) * nsyms
);
2161 /* Find the symbols for the values and put them into the type.
2162 The symbols can be found in the symlist that we put them on
2163 to cause them to be defined. osyms contains the old value
2164 of that symlist; everything up to there was defined by us. */
2165 /* Note that we preserve the order of the enum constants, so
2166 that in something like "enum {FOO, LAST_THING=FOO}" we print
2167 FOO, not LAST_THING. */
2169 for (syms
= *symlist
, n
= 0; syms
; syms
= syms
->next
)
2175 for (; j
< syms
->nsyms
; j
++,n
++)
2177 struct symbol
*xsym
= syms
->symbol
[j
];
2178 SYMBOL_TYPE (xsym
) = type
;
2179 TYPE_FIELD_NAME (type
, n
) = SYMBOL_NAME (xsym
);
2180 TYPE_FIELD_BITPOS (type
, n
) = SYMBOL_VALUE (xsym
);
2181 if (SYMBOL_VALUE (xsym
) < 0)
2183 TYPE_FIELD_BITSIZE (type
, n
) = 0;
2190 TYPE_FLAGS (type
) |= TYPE_FLAG_UNSIGNED
;
2195 /* Register our ability to parse symbols for coff BFD files. */
2197 static struct sym_fns coff_sym_fns
=
2199 bfd_target_coff_flavour
,
2200 coff_new_init
, /* sym_new_init: init anything gbl to entire symtab */
2201 coff_symfile_init
, /* sym_init: read initial info, setup for sym_read() */
2202 coff_symfile_read
, /* sym_read: read a symbol file into symtab */
2203 coff_symfile_finish
, /* sym_finish: finished with file, cleanup */
2204 default_symfile_offsets
,
2205 /* sym_offsets: xlate external to internal form */
2206 NULL
/* next: pointer to next struct sym_fns */
2210 _initialize_coffread ()
2212 add_symtab_fns (&coff_sym_fns
);