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 static struct type
*coff_read_struct_type
PARAMS ((int, int, int));
181 static struct type
*decode_base_type
PARAMS ((struct coff_symbol
*,
183 union internal_auxent
*));
185 static struct type
*decode_type
PARAMS ((struct coff_symbol
*, unsigned int,
186 union internal_auxent
*));
188 static struct type
*decode_function_type
PARAMS ((struct coff_symbol
*,
190 union internal_auxent
*));
192 static struct type
*coff_read_enum_type
PARAMS ((int, int, int));
194 static struct symbol
*process_coff_symbol
PARAMS ((struct coff_symbol
*,
195 union internal_auxent
*,
196 struct section_offsets
*,
199 static void patch_opaque_types
PARAMS ((struct symtab
*));
201 static void patch_type
PARAMS ((struct type
*, struct type
*));
203 static void enter_linenos
PARAMS ((long, int, int, struct section_offsets
*));
205 static void free_linetab
PARAMS ((void));
207 static int init_lineno
PARAMS ((bfd
*, long, int));
209 static char *getsymname
PARAMS ((struct internal_syment
*));
211 static char *coff_getfilename
PARAMS ((union internal_auxent
*));
213 static void free_stringtab
PARAMS ((void));
215 static int init_stringtab
PARAMS ((bfd
*, long));
217 static void read_one_sym
PARAMS ((struct coff_symbol
*,
218 struct internal_syment
*,
219 union internal_auxent
*));
221 static void coff_symtab_read
PARAMS ((long, int, struct section_offsets
*,
224 static void find_linenos
PARAMS ((bfd
*, sec_ptr
, PTR
));
226 static void coff_symfile_init
PARAMS ((struct objfile
*));
228 static void coff_new_init
PARAMS ((struct objfile
*));
230 static void coff_symfile_read
PARAMS ((struct objfile
*,
231 struct section_offsets
*, int));
233 static void coff_symfile_finish
PARAMS ((struct objfile
*));
235 static void record_minimal_symbol
PARAMS ((char *, CORE_ADDR
,
236 enum minimal_symbol_type
,
239 static void coff_end_symtab
PARAMS ((struct objfile
*));
241 static void complete_symtab
PARAMS ((char *, CORE_ADDR
, unsigned int));
243 static void coff_start_symtab
PARAMS ((char *));
245 static struct type
*coff_alloc_type
PARAMS ((int));
247 static struct type
**coff_lookup_type
PARAMS ((int));
249 static void coff_locate_sections
PARAMS ((bfd
*, asection
*, PTR
));
251 /* We are called once per section from coff_symfile_read. We
252 need to examine each section we are passed, check to see
253 if it is something we are interested in processing, and
254 if so, stash away some access information for the section.
256 FIXME: The section names should not be hardwired strings (what
257 should they be? I don't think most object file formats have enough
258 section flags to specify what kind of debug section it is
262 coff_locate_sections (abfd
, sectp
, csip
)
267 register struct coff_symfile_info
*csi
;
270 csi
= (struct coff_symfile_info
*) csip
;
271 name
= bfd_get_section_name (abfd
, sectp
);
272 if (STREQ (name
, ".text"))
274 csi
->textaddr
= bfd_section_vma (abfd
, sectp
);
275 csi
->textsize
+= bfd_section_size (abfd
, sectp
);
277 else if (strncmp (name
, ".text", sizeof ".text" - 1) == 0)
279 csi
->textsize
+= bfd_section_size (abfd
, sectp
);
281 else if (STREQ (name
, ".stabstr"))
283 csi
->stabstrsect
= sectp
;
285 else if (strncmp (name
, ".stab", sizeof ".stab" - 1) == 0)
289 /* We can have multiple .stab sections if linked with
291 for (s
= name
+ sizeof ".stab" - 1; *s
!= '\0'; s
++)
296 struct stab_section_list
*n
, **pn
;
298 n
= ((struct stab_section_list
*)
299 xmalloc (sizeof (struct stab_section_list
)));
302 for (pn
= &csi
->stabsects
; *pn
!= NULL
; pn
= &(*pn
)->next
)
306 /* This will be run after coffstab_build_psymtabs is called
307 in coff_symfile_read, at which point we no longer need
309 make_cleanup (free
, n
);
314 /* Return the section_offsets* that CS points to. */
315 static int cs_to_section
PARAMS ((struct coff_symbol
*, struct objfile
*));
317 struct find_targ_sec_arg
323 static void find_targ_sec
PARAMS ((bfd
*, asection
*, void *));
326 find_targ_sec (abfd
, sect
, obj
)
331 struct find_targ_sec_arg
*args
= (struct find_targ_sec_arg
*) obj
;
332 if (sect
->target_index
== args
->targ_index
)
333 *args
->resultp
= sect
;
336 /* Return the section number (SECT_OFF_*) that CS points to. */
338 cs_to_section (cs
, objfile
)
339 struct coff_symbol
*cs
;
340 struct objfile
*objfile
;
342 asection
*sect
= NULL
;
343 struct find_targ_sec_arg args
;
344 int off
= SECT_OFF_TEXT
;
346 args
.targ_index
= cs
->c_secnum
;
347 args
.resultp
= §
;
348 bfd_map_over_sections (objfile
->obfd
, find_targ_sec
, &args
);
351 /* This is the section. Figure out what SECT_OFF_* code it is. */
352 if (bfd_get_section_flags (abfd
, sect
) & SEC_CODE
)
354 else if (bfd_get_section_flags (abfd
, sect
) & SEC_LOAD
)
362 /* Return the address of the section of a COFF symbol. */
364 static CORE_ADDR cs_section_address
PARAMS ((struct coff_symbol
*, bfd
*));
367 cs_section_address (cs
, abfd
)
368 struct coff_symbol
*cs
;
371 asection
*sect
= NULL
;
372 struct find_targ_sec_arg args
;
375 args
.targ_index
= cs
->c_secnum
;
376 args
.resultp
= §
;
377 bfd_map_over_sections (abfd
, find_targ_sec
, &args
);
379 addr
= bfd_get_section_vma (objfile
->obfd
, sect
);
383 /* Look up a coff type-number index. Return the address of the slot
384 where the type for that index is stored.
385 The type-number is in INDEX.
387 This can be used for finding the type associated with that index
388 or for associating a new type with the index. */
390 static struct type
**
391 coff_lookup_type (index
)
394 if (index
>= type_vector_length
)
396 int old_vector_length
= type_vector_length
;
398 type_vector_length
*= 2;
399 if (index
/* is still */ >= type_vector_length
)
400 type_vector_length
= index
* 2;
402 type_vector
= (struct type
**)
403 xrealloc ((char *) type_vector
,
404 type_vector_length
* sizeof (struct type
*));
405 memset (&type_vector
[old_vector_length
], 0,
406 (type_vector_length
- old_vector_length
) * sizeof (struct type
*));
408 return &type_vector
[index
];
411 /* Make sure there is a type allocated for type number index
412 and return the type object.
413 This can create an empty (zeroed) type object. */
416 coff_alloc_type (index
)
419 register struct type
**type_addr
= coff_lookup_type (index
);
420 register struct type
*type
= *type_addr
;
422 /* If we are referring to a type not known at all yet,
423 allocate an empty type for it.
424 We will fill it in later if we find out how. */
427 type
= alloc_type (current_objfile
);
433 /* Start a new symtab for a new source file.
434 This is called when a COFF ".file" symbol is seen;
435 it indicates the start of data for one original source file. */
438 coff_start_symtab (name
)
442 /* We fill in the filename later. start_symtab puts
443 this pointer into last_source_file and we put it in
444 subfiles->name, which end_symtab frees; that's why
445 it must be malloc'd. */
446 savestring (name
, strlen (name
)),
447 /* We never know the directory name for COFF. */
449 /* The start address is irrelevant, since we set
450 last_source_start_addr in coff_end_symtab. */
452 record_debugformat ("COFF");
455 /* Save the vital information from when starting to read a file,
456 for use when closing off the current file.
457 NAME is the file name the symbols came from, START_ADDR is the first
458 text address for the file, and SIZE is the number of bytes of text. */
461 complete_symtab (name
, start_addr
, size
)
463 CORE_ADDR start_addr
;
466 if (last_source_file
!= NULL
)
467 free (last_source_file
);
468 last_source_file
= savestring (name
, strlen (name
));
469 current_source_start_addr
= start_addr
;
470 current_source_end_addr
= start_addr
+ size
;
472 if (current_objfile
->ei
.entry_point
>= current_source_start_addr
&&
473 current_objfile
->ei
.entry_point
< current_source_end_addr
)
475 current_objfile
->ei
.entry_file_lowpc
= current_source_start_addr
;
476 current_objfile
->ei
.entry_file_highpc
= current_source_end_addr
;
480 /* Finish the symbol definitions for one main source file,
481 close off all the lexical contexts for that file
482 (creating struct block's for them), then make the
483 struct symtab for that file and put it in the list of all such. */
486 coff_end_symtab (objfile
)
487 struct objfile
*objfile
;
489 struct symtab
*symtab
;
491 last_source_start_addr
= current_source_start_addr
;
493 symtab
= end_symtab (current_source_end_addr
, objfile
, 0);
496 free_named_symtabs (symtab
->filename
);
498 /* Reinitialize for beginning of new file. */
499 last_source_file
= NULL
;
503 record_minimal_symbol (name
, address
, type
, objfile
)
506 enum minimal_symbol_type type
;
507 struct objfile
*objfile
;
509 /* We don't want TDESC entry points in the minimal symbol table */
513 prim_record_minimal_symbol (name
, address
, type
, objfile
);
516 /* coff_symfile_init ()
517 is the coff-specific initialization routine for reading symbols.
518 It is passed a struct objfile which contains, among other things,
519 the BFD for the file whose symbols are being read, and a slot for
520 a pointer to "private data" which we fill with cookies and other
521 treats for coff_symfile_read ().
523 We will only be called if this is a COFF or COFF-like file.
524 BFD handles figuring out the format of the file, and code in symtab.c
525 uses BFD's determination to vector to us.
527 The ultimate result is a new symtab (or, FIXME, eventually a psymtab). */
530 coff_symfile_init (objfile
)
531 struct objfile
*objfile
;
533 /* Allocate struct to keep track of stab reading. */
534 objfile
->sym_stab_info
= (struct dbx_symfile_info
*)
535 xmmalloc (objfile
->md
, sizeof (struct dbx_symfile_info
));
537 memset ((PTR
) objfile
->sym_stab_info
, 0, sizeof (struct dbx_symfile_info
));
539 /* Allocate struct to keep track of the symfile */
540 objfile
->sym_private
= xmmalloc (objfile
->md
,
541 sizeof (struct coff_symfile_info
));
543 memset (objfile
->sym_private
, 0, sizeof (struct coff_symfile_info
));
545 /* COFF objects may be reordered, so set OBJF_REORDERED. If we
546 find this causes a significant slowdown in gdb then we could
547 set it in the debug symbol readers only when necessary. */
548 objfile
->flags
|= OBJF_REORDERED
;
550 init_entry_point_info (objfile
);
553 /* This function is called for every section; it finds the outer limits
554 of the line table (minimum and maximum file offset) so that the
555 mainline code can read the whole thing for efficiency. */
559 find_linenos (abfd
, asect
, vpinfo
)
564 struct coff_symfile_info
*info
;
566 file_ptr offset
, maxoff
;
568 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
569 count
= asect
->lineno_count
;
574 size
= count
* local_linesz
;
576 info
= (struct coff_symfile_info
*) vpinfo
;
577 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
578 offset
= asect
->line_filepos
;
581 if (offset
< info
->min_lineno_offset
|| info
->min_lineno_offset
== 0)
582 info
->min_lineno_offset
= offset
;
584 maxoff
= offset
+ size
;
585 if (maxoff
> info
->max_lineno_offset
)
586 info
->max_lineno_offset
= maxoff
;
590 /* The BFD for this file -- only good while we're actively reading
591 symbols into a psymtab or a symtab. */
593 static bfd
*symfile_bfd
;
595 /* Read a symbol file, after initialization by coff_symfile_init. */
599 coff_symfile_read (objfile
, section_offsets
, mainline
)
600 struct objfile
*objfile
;
601 struct section_offsets
*section_offsets
;
604 struct coff_symfile_info
*info
;
605 struct dbx_symfile_info
*dbxinfo
;
606 bfd
*abfd
= objfile
->obfd
;
607 coff_data_type
*cdata
= coff_data (abfd
);
608 char *name
= bfd_get_filename (abfd
);
612 int stringtab_offset
;
613 struct cleanup
*back_to
;
616 info
= (struct coff_symfile_info
*) objfile
->sym_private
;
617 dbxinfo
= objfile
->sym_stab_info
;
618 symfile_bfd
= abfd
; /* Kludge for swap routines */
620 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
621 num_symbols
= bfd_get_symcount (abfd
); /* How many syms */
622 symtab_offset
= cdata
->sym_filepos
; /* Symbol table file offset */
623 stringtab_offset
= symtab_offset
+ /* String table file offset */
624 num_symbols
* cdata
->local_symesz
;
626 /* Set a few file-statics that give us specific information about
627 the particular COFF file format we're reading. */
628 local_n_btmask
= cdata
->local_n_btmask
;
629 local_n_btshft
= cdata
->local_n_btshft
;
630 local_n_tmask
= cdata
->local_n_tmask
;
631 local_n_tshift
= cdata
->local_n_tshift
;
632 local_linesz
= cdata
->local_linesz
;
633 local_symesz
= cdata
->local_symesz
;
634 local_auxesz
= cdata
->local_auxesz
;
636 /* Allocate space for raw symbol and aux entries, based on their
637 space requirements as reported by BFD. */
638 temp_sym
= (char *) xmalloc
639 (cdata
->local_symesz
+ cdata
->local_auxesz
);
640 temp_aux
= temp_sym
+ cdata
->local_symesz
;
641 back_to
= make_cleanup ((make_cleanup_func
) free_current_contents
, &temp_sym
);
643 /* We need to know whether this is a PE file, because in PE files,
644 unlike standard COFF files, symbol values are stored as offsets
645 from the section address, rather than as absolute addresses.
646 FIXME: We should use BFD to read the symbol table, and thus avoid
648 pe_file
= strncmp (bfd_get_target (objfile
->obfd
), "pe", 2) == 0;
652 /* Read the line number table, all at once. */
653 info
->min_lineno_offset
= 0;
654 info
->max_lineno_offset
= 0;
655 bfd_map_over_sections (abfd
, find_linenos
, (PTR
) info
);
657 make_cleanup ((make_cleanup_func
) free_linetab
, 0);
658 val
= init_lineno (abfd
, info
->min_lineno_offset
,
659 info
->max_lineno_offset
- info
->min_lineno_offset
);
661 error ("\"%s\": error reading line numbers\n", name
);
663 /* Now read the string table, all at once. */
665 make_cleanup ((make_cleanup_func
) free_stringtab
, 0);
666 val
= init_stringtab (abfd
, stringtab_offset
);
668 error ("\"%s\": can't get string table", name
);
670 init_minimal_symbol_collection ();
671 make_cleanup ((make_cleanup_func
) discard_minimal_symbols
, 0);
673 /* Now that the executable file is positioned at symbol table,
674 process it and define symbols accordingly. */
676 coff_symtab_read ((long) symtab_offset
, num_symbols
, section_offsets
,
679 /* Sort symbols alphabetically within each block. */
684 for (s
= objfile
->symtabs
; s
!= NULL
; s
= s
->next
)
685 sort_symtab_syms (s
);
688 /* Install any minimal symbols that have been collected as the current
689 minimal symbols for this objfile. */
691 install_minimal_symbols (objfile
);
693 bfd_map_over_sections (abfd
, coff_locate_sections
, (PTR
) info
);
697 if (!info
->stabstrsect
)
702 ("The debugging information in `%s' is corrupted.\n"
703 "The file has a `.stabs' section, but no `.stabstr' section.\n"),
705 return_to_top_level (RETURN_ERROR
);
708 /* FIXME: dubious. Why can't we use something normal like
709 bfd_get_section_contents? */
710 bfd_seek (abfd
, abfd
->where
, 0);
712 stabstrsize
= bfd_section_size (abfd
, info
->stabstrsect
);
714 coffstab_build_psymtabs (objfile
,
717 info
->textaddr
, info
->textsize
,
719 info
->stabstrsect
->filepos
, stabstrsize
);
722 do_cleanups (back_to
);
726 coff_new_init (ignore
)
727 struct objfile
*ignore
;
731 /* Perform any local cleanups required when we are done with a particular
732 objfile. I.E, we are in the process of discarding all symbol information
733 for an objfile, freeing up all memory held for it, and unlinking the
734 objfile struct from the global list of known objfiles. */
737 coff_symfile_finish (objfile
)
738 struct objfile
*objfile
;
740 if (objfile
->sym_private
!= NULL
)
742 mfree (objfile
->md
, objfile
->sym_private
);
747 /* Given pointers to a symbol table in coff style exec file,
748 analyze them and create struct symtab's describing the symbols.
749 NSYMS is the number of symbols in the symbol table.
750 We read them one at a time using read_one_sym (). */
753 coff_symtab_read (symtab_offset
, nsyms
, section_offsets
, objfile
)
756 struct section_offsets
*section_offsets
;
757 struct objfile
*objfile
;
759 register struct context_stack
*new;
760 struct coff_symbol coff_symbol
;
761 register struct coff_symbol
*cs
= &coff_symbol
;
762 static struct internal_syment main_sym
;
763 static union internal_auxent main_aux
;
764 struct coff_symbol fcn_cs_saved
;
765 static struct internal_syment fcn_sym_saved
;
766 static union internal_auxent fcn_aux_saved
;
768 /* A .file is open. */
769 int in_source_file
= 0;
770 int next_file_symnum
= -1;
771 /* Name of the current file. */
772 char *filestring
= "";
774 int fcn_first_line
= 0;
775 CORE_ADDR fcn_first_line_addr
;
776 int fcn_last_line
= 0;
777 int fcn_start_addr
= 0;
778 long fcn_line_ptr
= 0;
782 /* Work around a stdio bug in SunOS4.1.1 (this makes me nervous....
783 it's hard to know I've really worked around it. The fix should be
784 harmless, anyway). The symptom of the bug is that the first
785 fread (in read_one_sym), will (in my example) actually get data
786 from file offset 268, when the fseek was to 264 (and ftell shows
787 264). This causes all hell to break loose. I was unable to
788 reproduce this on a short test program which operated on the same
789 file, performing (I think) the same sequence of operations.
791 It stopped happening when I put in this (former) rewind().
793 FIXME: Find out if this has been reported to Sun, whether it has
794 been fixed in a later release, etc. */
796 bfd_seek (objfile
->obfd
, 0, 0);
798 /* Position to read the symbol table. */
799 val
= bfd_seek (objfile
->obfd
, (long) symtab_offset
, 0);
801 perror_with_name (objfile
->name
);
803 current_objfile
= objfile
;
804 nlist_bfd_global
= objfile
->obfd
;
805 nlist_nsyms_global
= nsyms
;
806 last_source_file
= NULL
;
807 memset (opaque_type_chain
, 0, sizeof opaque_type_chain
);
809 if (type_vector
) /* Get rid of previous one */
810 free ((PTR
) type_vector
);
811 type_vector_length
= 160;
812 type_vector
= (struct type
**)
813 xmalloc (type_vector_length
* sizeof (struct type
*));
814 memset (type_vector
, 0, type_vector_length
* sizeof (struct type
*));
816 coff_start_symtab ("");
819 while (symnum
< nsyms
)
821 QUIT
; /* Make this command interruptable. */
823 read_one_sym (cs
, &main_sym
, &main_aux
);
825 if (cs
->c_symnum
== next_file_symnum
&& cs
->c_sclass
!= C_FILE
)
827 if (last_source_file
)
828 coff_end_symtab (objfile
);
830 coff_start_symtab ("_globals_");
831 complete_symtab ("_globals_", 0, 0);
832 /* done with all files, everything from here on out is globals */
835 /* Special case for file with type declarations only, no text. */
836 if (!last_source_file
&& SDB_TYPE (cs
->c_type
)
837 && cs
->c_secnum
== N_DEBUG
)
838 complete_symtab (filestring
, 0, 0);
840 /* Typedefs should not be treated as symbol definitions. */
841 if (ISFCN (cs
->c_type
) && cs
->c_sclass
!= C_TPDEF
)
843 /* Record all functions -- external and static -- in minsyms. */
844 tmpaddr
= cs
->c_value
+ ANOFFSET (section_offsets
, SECT_OFF_TEXT
);
845 record_minimal_symbol (cs
->c_name
, tmpaddr
, mst_text
, objfile
);
847 fcn_line_ptr
= main_aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
;
848 fcn_start_addr
= tmpaddr
;
850 fcn_sym_saved
= main_sym
;
851 fcn_aux_saved
= main_aux
;
855 switch (cs
->c_sclass
)
864 complain (&bad_sclass_complaint
, cs
->c_name
);
868 /* c_value field contains symnum of next .file entry in table
869 or symnum of first global after last .file. */
870 next_file_symnum
= cs
->c_value
;
872 filestring
= coff_getfilename (&main_aux
);
876 /* Complete symbol table for last object file
877 containing debugging information. */
878 if (last_source_file
)
880 coff_end_symtab (objfile
);
881 coff_start_symtab (filestring
);
886 /* C_LABEL is used for labels and static functions. Including
887 it here allows gdb to see static functions when no debug
888 info is available. */
890 /* However, labels within a function can make weird backtraces,
891 so filter them out (from phdm@macqel.be). */
897 case C_THUMBSTATFUNC
:
898 if (cs
->c_name
[0] == '.')
900 if (STREQ (cs
->c_name
, ".text"))
902 /* FIXME: don't wire in ".text" as section name
904 /* Check for in_source_file deals with case of
905 a file with debugging symbols
906 followed by a later file with no symbols. */
908 complete_symtab (filestring
,
909 cs
->c_value
+ ANOFFSET (section_offsets
, SECT_OFF_TEXT
),
910 main_aux
.x_scn
.x_scnlen
);
913 /* flush rest of '.' symbols */
916 else if (!SDB_TYPE (cs
->c_type
)
917 && cs
->c_name
[0] == 'L'
918 && (strncmp (cs
->c_name
, "LI%", 3) == 0
919 || strncmp (cs
->c_name
, "LF%", 3) == 0
920 || strncmp (cs
->c_name
, "LC%", 3) == 0
921 || strncmp (cs
->c_name
, "LP%", 3) == 0
922 || strncmp (cs
->c_name
, "LPB%", 4) == 0
923 || strncmp (cs
->c_name
, "LBB%", 4) == 0
924 || strncmp (cs
->c_name
, "LBE%", 4) == 0
925 || strncmp (cs
->c_name
, "LPBX%", 5) == 0))
926 /* At least on a 3b1, gcc generates swbeg and string labels
927 that look like this. Ignore them. */
929 /* fall in for static symbols that don't start with '.' */
934 /* Record it in the minimal symbols regardless of
935 SDB_TYPE. This parallels what we do for other debug
936 formats, and probably is needed to make
937 print_address_symbolic work right without the (now
938 gone) "set fast-symbolic-addr off" kludge. */
940 /* FIXME: should use mst_abs, and not relocate, if absolute. */
941 enum minimal_symbol_type ms_type
;
944 if (cs
->c_secnum
== N_UNDEF
)
946 /* This is a common symbol. See if the target
947 environment knows where it has been relocated to. */
949 if (target_lookup_symbol (cs
->c_name
, &reladdr
))
951 /* Error in lookup; ignore symbol. */
955 /* The address has already been relocated; make sure that
956 objfile_relocate doesn't relocate it again. */
958 ms_type
= cs
->c_sclass
== C_EXT
959 || cs
->c_sclass
== C_THUMBEXT
?
960 mst_bss
: mst_file_bss
;
964 sec
= cs_to_section (cs
, objfile
);
965 tmpaddr
= cs
->c_value
;
966 if (cs
->c_sclass
== C_EXT
|| cs
->c_sclass
== C_THUMBEXTFUNC
967 || cs
->c_sclass
== C_THUMBEXT
)
968 tmpaddr
+= ANOFFSET (section_offsets
, sec
);
973 case SECT_OFF_RODATA
:
975 cs
->c_sclass
== C_EXT
|| cs
->c_sclass
== C_THUMBEXTFUNC
976 || cs
->c_sclass
== C_THUMBEXT
?
977 mst_text
: mst_file_text
;
978 #ifdef SMASH_TEXT_ADDRESS
979 if (tmpaddr
& 1) /* FIXME: delete this line */
980 SMASH_TEXT_ADDRESS (tmpaddr
);
985 cs
->c_sclass
== C_EXT
|| cs
->c_sclass
== C_THUMBEXT
?
986 mst_data
: mst_file_data
;
990 cs
->c_sclass
== C_EXT
|| cs
->c_sclass
== C_THUMBEXT
?
991 mst_data
: mst_file_data
;
994 ms_type
= mst_unknown
;
999 if (cs
->c_name
[0] != '@' /* Skip tdesc symbols */ )
1001 struct minimal_symbol
*msym
;
1003 msym
= prim_record_minimal_symbol_and_info
1004 (cs
->c_name
, tmpaddr
, ms_type
, (char *) cs
->c_sclass
, sec
,
1006 #ifdef COFF_MAKE_MSYMBOL_SPECIAL
1008 COFF_MAKE_MSYMBOL_SPECIAL (cs
->c_sclass
, msym
);
1011 if (SDB_TYPE (cs
->c_type
))
1014 sym
= process_coff_symbol
1015 (cs
, &main_aux
, section_offsets
, objfile
);
1016 SYMBOL_VALUE (sym
) = tmpaddr
;
1017 SYMBOL_SECTION (sym
) = sec
;
1023 if (STREQ (cs
->c_name
, ".bf"))
1025 within_function
= 1;
1027 /* value contains address of first non-init type code */
1028 /* main_aux.x_sym.x_misc.x_lnsz.x_lnno
1029 contains line number of '{' } */
1030 if (cs
->c_naux
!= 1)
1031 complain (&bf_no_aux_complaint
, cs
->c_symnum
);
1032 fcn_first_line
= main_aux
.x_sym
.x_misc
.x_lnsz
.x_lnno
;
1033 fcn_first_line_addr
= cs
->c_value
;
1035 /* Might want to check that locals are 0 and
1036 context_stack_depth is zero, and complain if not. */
1039 new = push_context (depth
, fcn_start_addr
);
1040 fcn_cs_saved
.c_name
= getsymname (&fcn_sym_saved
);
1042 process_coff_symbol (&fcn_cs_saved
, &fcn_aux_saved
,
1043 section_offsets
, objfile
);
1045 else if (STREQ (cs
->c_name
, ".ef"))
1047 /* the value of .ef is the address of epilogue code;
1048 not useful for gdb. */
1049 /* { main_aux.x_sym.x_misc.x_lnsz.x_lnno
1050 contains number of lines to '}' */
1052 if (context_stack_depth
<= 0)
1053 { /* We attempted to pop an empty context stack */
1054 complain (&ef_stack_complaint
, cs
->c_symnum
);
1055 within_function
= 0;
1059 new = pop_context ();
1060 /* Stack must be empty now. */
1061 if (context_stack_depth
> 0 || new == NULL
)
1063 complain (&ef_complaint
, cs
->c_symnum
);
1064 within_function
= 0;
1067 if (cs
->c_naux
!= 1)
1069 complain (&ef_no_aux_complaint
, cs
->c_symnum
);
1070 fcn_last_line
= 0x7FFFFFFF;
1074 fcn_last_line
= main_aux
.x_sym
.x_misc
.x_lnsz
.x_lnno
;
1076 /* fcn_first_line is the line number of the opening '{'.
1077 Do not record it - because it would affect gdb's idea
1078 of the line number of the first statement of the function -
1079 except for one-line functions, for which it is also the line
1080 number of all the statements and of the closing '}', and
1081 for which we do not have any other statement-line-number. */
1082 if (fcn_last_line
== 1)
1083 record_line (current_subfile
, fcn_first_line
,
1084 fcn_first_line_addr
);
1086 enter_linenos (fcn_line_ptr
, fcn_first_line
, fcn_last_line
,
1089 finish_block (new->name
, &local_symbols
, new->old_blocks
,
1091 #if defined (FUNCTION_EPILOGUE_SIZE)
1092 /* This macro should be defined only on
1094 fcn_aux_saved.x_sym.x_misc.x_fsize
1095 field is always zero.
1096 So use the .bf record information that
1097 points to the epilogue and add the size
1100 + FUNCTION_EPILOGUE_SIZE
1101 + ANOFFSET (section_offsets
, SECT_OFF_TEXT
),
1103 fcn_cs_saved
.c_value
1104 + fcn_aux_saved
.x_sym
.x_misc
.x_fsize
1105 + ANOFFSET (section_offsets
, SECT_OFF_TEXT
),
1109 within_function
= 0;
1114 if (STREQ (cs
->c_name
, ".bb"))
1116 tmpaddr
= cs
->c_value
;
1117 tmpaddr
+= ANOFFSET (section_offsets
, SECT_OFF_TEXT
);
1118 push_context (++depth
, tmpaddr
);
1120 else if (STREQ (cs
->c_name
, ".eb"))
1122 if (context_stack_depth
<= 0)
1123 { /* We attempted to pop an empty context stack */
1124 complain (&eb_stack_complaint
, cs
->c_symnum
);
1128 new = pop_context ();
1129 if (depth
-- != new->depth
)
1131 complain (&eb_complaint
, symnum
);
1134 if (local_symbols
&& context_stack_depth
> 0)
1137 cs
->c_value
+ ANOFFSET (section_offsets
, SECT_OFF_TEXT
);
1138 /* Make a block for the local symbols within. */
1139 finish_block (0, &local_symbols
, new->old_blocks
,
1140 new->start_addr
, tmpaddr
, objfile
);
1142 /* Now pop locals of block just finished. */
1143 local_symbols
= new->locals
;
1148 process_coff_symbol (cs
, &main_aux
, section_offsets
, objfile
);
1153 if (last_source_file
)
1154 coff_end_symtab (objfile
);
1156 /* Patch up any opaque types (references to types that are not defined
1157 in the file where they are referenced, e.g. "struct foo *bar"). */
1158 ALL_OBJFILE_SYMTABS (objfile
, s
)
1159 patch_opaque_types (s
);
1161 current_objfile
= NULL
;
1164 /* Routines for reading headers and symbols from executable. */
1166 /* Read the next symbol, swap it, and return it in both internal_syment
1167 form, and coff_symbol form. Also return its first auxent, if any,
1168 in internal_auxent form, and skip any other auxents. */
1171 read_one_sym (cs
, sym
, aux
)
1172 register struct coff_symbol
*cs
;
1173 register struct internal_syment
*sym
;
1174 register union internal_auxent
*aux
;
1178 cs
->c_symnum
= symnum
;
1179 bfd_read (temp_sym
, local_symesz
, 1, nlist_bfd_global
);
1180 bfd_coff_swap_sym_in (symfile_bfd
, temp_sym
, (char *) sym
);
1181 cs
->c_naux
= sym
->n_numaux
& 0xff;
1182 if (cs
->c_naux
>= 1)
1184 bfd_read (temp_aux
, local_auxesz
, 1, nlist_bfd_global
);
1185 bfd_coff_swap_aux_in (symfile_bfd
, temp_aux
, sym
->n_type
, sym
->n_sclass
,
1186 0, cs
->c_naux
, (char *) aux
);
1187 /* If more than one aux entry, read past it (only the first aux
1189 for (i
= 1; i
< cs
->c_naux
; i
++)
1190 bfd_read (temp_aux
, local_auxesz
, 1, nlist_bfd_global
);
1192 cs
->c_name
= getsymname (sym
);
1193 cs
->c_value
= sym
->n_value
;
1194 cs
->c_sclass
= (sym
->n_sclass
& 0xff);
1195 cs
->c_secnum
= sym
->n_scnum
;
1196 cs
->c_type
= (unsigned) sym
->n_type
;
1197 if (!SDB_TYPE (cs
->c_type
))
1201 if (cs
->c_sclass
& 128)
1202 printf ("thumb symbol %s, class 0x%x\n", cs
->c_name
, cs
->c_sclass
);
1205 symnum
+= 1 + cs
->c_naux
;
1207 /* The PE file format stores symbol values as offsets within the
1208 section, rather than as absolute addresses. We correct that
1209 here, if the symbol has an appropriate storage class. FIXME: We
1210 should use BFD to read the symbols, rather than duplicating the
1214 switch (cs
->c_sclass
)
1218 case C_THUMBEXTFUNC
:
1223 case C_THUMBSTATFUNC
:
1229 if (cs
->c_secnum
!= 0)
1230 cs
->c_value
+= cs_section_address (cs
, symfile_bfd
);
1236 /* Support for string table handling */
1238 static char *stringtab
= NULL
;
1241 init_stringtab (abfd
, offset
)
1247 unsigned char lengthbuf
[4];
1251 /* If the file is stripped, the offset might be zero, indicating no
1252 string table. Just return with `stringtab' set to null. */
1256 if (bfd_seek (abfd
, offset
, 0) < 0)
1259 val
= bfd_read ((char *) lengthbuf
, sizeof lengthbuf
, 1, abfd
);
1260 length
= bfd_h_get_32 (symfile_bfd
, lengthbuf
);
1262 /* If no string table is needed, then the file may end immediately
1263 after the symbols. Just return with `stringtab' set to null. */
1264 if (val
!= sizeof lengthbuf
|| length
< sizeof lengthbuf
)
1267 stringtab
= (char *) xmalloc (length
);
1268 /* This is in target format (probably not very useful, and not currently
1269 used), not host format. */
1270 memcpy (stringtab
, lengthbuf
, sizeof lengthbuf
);
1271 if (length
== sizeof length
) /* Empty table -- just the count */
1274 val
= bfd_read (stringtab
+ sizeof lengthbuf
, length
- sizeof lengthbuf
, 1, abfd
);
1275 if (val
!= length
- sizeof lengthbuf
|| stringtab
[length
- 1] != '\0')
1290 getsymname (symbol_entry
)
1291 struct internal_syment
*symbol_entry
;
1293 static char buffer
[SYMNMLEN
+ 1];
1296 if (symbol_entry
->_n
._n_n
._n_zeroes
== 0)
1298 /* FIXME: Probably should be detecting corrupt symbol files by
1299 seeing whether offset points to within the stringtab. */
1300 result
= stringtab
+ symbol_entry
->_n
._n_n
._n_offset
;
1304 strncpy (buffer
, symbol_entry
->_n
._n_name
, SYMNMLEN
);
1305 buffer
[SYMNMLEN
] = '\0';
1311 /* Extract the file name from the aux entry of a C_FILE symbol. Return
1312 only the last component of the name. Result is in static storage and
1313 is only good for temporary use. */
1316 coff_getfilename (aux_entry
)
1317 union internal_auxent
*aux_entry
;
1319 static char buffer
[BUFSIZ
];
1320 register char *temp
;
1323 if (aux_entry
->x_file
.x_n
.x_zeroes
== 0)
1324 strcpy (buffer
, stringtab
+ aux_entry
->x_file
.x_n
.x_offset
);
1327 strncpy (buffer
, aux_entry
->x_file
.x_fname
, FILNMLEN
);
1328 buffer
[FILNMLEN
] = '\0';
1332 /* FIXME: We should not be throwing away the information about what
1333 directory. It should go into dirname of the symtab, or some such
1335 if ((temp
= strrchr (result
, '/')) != NULL
)
1340 /* Support for line number handling. */
1342 static char *linetab
= NULL
;
1343 static long linetab_offset
;
1344 static unsigned long linetab_size
;
1346 /* Read in all the line numbers for fast lookups later. Leave them in
1347 external (unswapped) format in memory; we'll swap them as we enter
1348 them into GDB's data structures. */
1351 init_lineno (abfd
, offset
, size
)
1358 linetab_offset
= offset
;
1359 linetab_size
= size
;
1366 if (bfd_seek (abfd
, offset
, 0) < 0)
1369 /* Allocate the desired table, plus a sentinel */
1370 linetab
= (char *) xmalloc (size
+ local_linesz
);
1372 val
= bfd_read (linetab
, size
, 1, abfd
);
1376 /* Terminate it with an all-zero sentinel record */
1377 memset (linetab
+ size
, 0, local_linesz
);
1390 #if !defined (L_LNNO32)
1391 #define L_LNNO32(lp) ((lp)->l_lnno)
1395 enter_linenos (file_offset
, first_line
, last_line
, section_offsets
)
1397 register int first_line
;
1398 register int last_line
;
1399 struct section_offsets
*section_offsets
;
1401 register char *rawptr
;
1402 struct internal_lineno lptr
;
1406 if (file_offset
< linetab_offset
)
1408 complain (&lineno_complaint
, file_offset
);
1409 if (file_offset
> linetab_size
) /* Too big to be an offset? */
1411 file_offset
+= linetab_offset
; /* Try reading at that linetab offset */
1414 rawptr
= &linetab
[file_offset
- linetab_offset
];
1416 /* skip first line entry for each function */
1417 rawptr
+= local_linesz
;
1418 /* line numbers start at one for the first line of the function */
1423 bfd_coff_swap_lineno_in (symfile_bfd
, rawptr
, &lptr
);
1424 rawptr
+= local_linesz
;
1425 /* The next function, or the sentinel, will have L_LNNO32 zero; we exit. */
1426 if (L_LNNO32 (&lptr
) && L_LNNO32 (&lptr
) <= last_line
)
1427 record_line (current_subfile
, first_line
+ L_LNNO32 (&lptr
),
1429 + ANOFFSET (section_offsets
, SECT_OFF_TEXT
));
1436 patch_type (type
, real_type
)
1438 struct type
*real_type
;
1440 register struct type
*target
= TYPE_TARGET_TYPE (type
);
1441 register struct type
*real_target
= TYPE_TARGET_TYPE (real_type
);
1442 int field_size
= TYPE_NFIELDS (real_target
) * sizeof (struct field
);
1444 TYPE_LENGTH (target
) = TYPE_LENGTH (real_target
);
1445 TYPE_NFIELDS (target
) = TYPE_NFIELDS (real_target
);
1446 TYPE_FIELDS (target
) = (struct field
*) TYPE_ALLOC (target
, field_size
);
1448 memcpy (TYPE_FIELDS (target
), TYPE_FIELDS (real_target
), field_size
);
1450 if (TYPE_NAME (real_target
))
1452 if (TYPE_NAME (target
))
1453 free (TYPE_NAME (target
));
1454 TYPE_NAME (target
) = concat (TYPE_NAME (real_target
), NULL
);
1458 /* Patch up all appropriate typedef symbols in the opaque_type_chains
1459 so that they can be used to print out opaque data structures properly. */
1462 patch_opaque_types (s
)
1465 register struct block
*b
;
1467 register struct symbol
*real_sym
;
1469 /* Go through the per-file symbols only */
1470 b
= BLOCKVECTOR_BLOCK (BLOCKVECTOR (s
), STATIC_BLOCK
);
1471 for (i
= BLOCK_NSYMS (b
) - 1; i
>= 0; i
--)
1473 /* Find completed typedefs to use to fix opaque ones.
1474 Remove syms from the chain when their types are stored,
1475 but search the whole chain, as there may be several syms
1476 from different files with the same name. */
1477 real_sym
= BLOCK_SYM (b
, i
);
1478 if (SYMBOL_CLASS (real_sym
) == LOC_TYPEDEF
&&
1479 SYMBOL_NAMESPACE (real_sym
) == VAR_NAMESPACE
&&
1480 TYPE_CODE (SYMBOL_TYPE (real_sym
)) == TYPE_CODE_PTR
&&
1481 TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (real_sym
))) != 0)
1483 register char *name
= SYMBOL_NAME (real_sym
);
1484 register int hash
= hashname (name
);
1485 register struct symbol
*sym
, *prev
;
1488 for (sym
= opaque_type_chain
[hash
]; sym
;)
1490 if (name
[0] == SYMBOL_NAME (sym
)[0] &&
1491 STREQ (name
+ 1, SYMBOL_NAME (sym
) + 1))
1495 SYMBOL_VALUE_CHAIN (prev
) = SYMBOL_VALUE_CHAIN (sym
);
1499 opaque_type_chain
[hash
] = SYMBOL_VALUE_CHAIN (sym
);
1502 patch_type (SYMBOL_TYPE (sym
), SYMBOL_TYPE (real_sym
));
1506 sym
= SYMBOL_VALUE_CHAIN (prev
);
1510 sym
= opaque_type_chain
[hash
];
1516 sym
= SYMBOL_VALUE_CHAIN (sym
);
1523 static struct symbol
*
1524 process_coff_symbol (cs
, aux
, section_offsets
, objfile
)
1525 register struct coff_symbol
*cs
;
1526 register union internal_auxent
*aux
;
1527 struct section_offsets
*section_offsets
;
1528 struct objfile
*objfile
;
1530 register struct symbol
*sym
1531 = (struct symbol
*) obstack_alloc (&objfile
->symbol_obstack
,
1532 sizeof (struct symbol
));
1535 memset (sym
, 0, sizeof (struct symbol
));
1537 name
= EXTERNAL_NAME (name
, objfile
->obfd
);
1538 SYMBOL_NAME (sym
) = obsavestring (name
, strlen (name
),
1539 &objfile
->symbol_obstack
);
1540 SYMBOL_LANGUAGE (sym
) = language_auto
;
1541 SYMBOL_INIT_DEMANGLED_NAME (sym
, &objfile
->symbol_obstack
);
1543 /* default assumptions */
1544 SYMBOL_VALUE (sym
) = cs
->c_value
;
1545 SYMBOL_NAMESPACE (sym
) = VAR_NAMESPACE
;
1546 SYMBOL_SECTION (sym
) = cs_to_section (cs
, objfile
);
1548 if (ISFCN (cs
->c_type
))
1550 SYMBOL_VALUE (sym
) += ANOFFSET (section_offsets
, SECT_OFF_TEXT
);
1552 lookup_function_type (decode_function_type (cs
, cs
->c_type
, aux
));
1554 SYMBOL_CLASS (sym
) = LOC_BLOCK
;
1555 if (cs
->c_sclass
== C_STAT
|| cs
->c_sclass
== C_THUMBSTAT
1556 || cs
->c_sclass
== C_THUMBSTATFUNC
)
1557 add_symbol_to_list (sym
, &file_symbols
);
1558 else if (cs
->c_sclass
== C_EXT
|| cs
->c_sclass
== C_THUMBEXT
1559 || cs
->c_sclass
== C_THUMBEXTFUNC
)
1560 add_symbol_to_list (sym
, &global_symbols
);
1564 SYMBOL_TYPE (sym
) = decode_type (cs
, cs
->c_type
, aux
);
1565 switch (cs
->c_sclass
)
1571 SYMBOL_CLASS (sym
) = LOC_LOCAL
;
1572 add_symbol_to_list (sym
, &local_symbols
);
1576 case C_THUMBEXTFUNC
:
1578 SYMBOL_CLASS (sym
) = LOC_STATIC
;
1579 SYMBOL_VALUE_ADDRESS (sym
) = (CORE_ADDR
) cs
->c_value
;
1580 SYMBOL_VALUE_ADDRESS (sym
) += ANOFFSET (section_offsets
, SECT_OFF_TEXT
);
1581 add_symbol_to_list (sym
, &global_symbols
);
1585 case C_THUMBSTATFUNC
:
1587 SYMBOL_CLASS (sym
) = LOC_STATIC
;
1588 SYMBOL_VALUE_ADDRESS (sym
) = (CORE_ADDR
) cs
->c_value
;
1589 SYMBOL_VALUE_ADDRESS (sym
) += ANOFFSET (section_offsets
, SECT_OFF_TEXT
);
1590 if (within_function
)
1592 /* Static symbol of local scope */
1593 add_symbol_to_list (sym
, &local_symbols
);
1597 /* Static symbol at top level of file */
1598 add_symbol_to_list (sym
, &file_symbols
);
1602 #ifdef C_GLBLREG /* AMD coff */
1606 SYMBOL_CLASS (sym
) = LOC_REGISTER
;
1607 SYMBOL_VALUE (sym
) = SDB_REG_TO_REGNUM (cs
->c_value
);
1608 add_symbol_to_list (sym
, &local_symbols
);
1616 SYMBOL_CLASS (sym
) = LOC_ARG
;
1617 add_symbol_to_list (sym
, &local_symbols
);
1618 #if !defined (BELIEVE_PCC_PROMOTION)
1619 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
1621 /* If PCC says a parameter is a short or a char,
1622 aligned on an int boundary, realign it to the
1623 "little end" of the int. */
1624 struct type
*temptype
;
1625 temptype
= lookup_fundamental_type (current_objfile
,
1627 if (TYPE_LENGTH (SYMBOL_TYPE (sym
)) < TYPE_LENGTH (temptype
)
1628 && TYPE_CODE (SYMBOL_TYPE (sym
)) == TYPE_CODE_INT
1629 && 0 == SYMBOL_VALUE (sym
) % TYPE_LENGTH (temptype
))
1631 SYMBOL_VALUE (sym
) +=
1632 TYPE_LENGTH (temptype
)
1633 - TYPE_LENGTH (SYMBOL_TYPE (sym
));
1640 SYMBOL_CLASS (sym
) = LOC_REGPARM
;
1641 SYMBOL_VALUE (sym
) = SDB_REG_TO_REGNUM (cs
->c_value
);
1642 add_symbol_to_list (sym
, &local_symbols
);
1643 #if !defined (BELIEVE_PCC_PROMOTION)
1644 /* FIXME: This should retain the current type, since it's just
1645 a register value. gnu@adobe, 26Feb93 */
1647 /* If PCC says a parameter is a short or a char,
1648 it is really an int. */
1649 struct type
*temptype
;
1651 lookup_fundamental_type (current_objfile
, FT_INTEGER
);
1652 if (TYPE_LENGTH (SYMBOL_TYPE (sym
)) < TYPE_LENGTH (temptype
)
1653 && TYPE_CODE (SYMBOL_TYPE (sym
)) == TYPE_CODE_INT
)
1656 (TYPE_UNSIGNED (SYMBOL_TYPE (sym
))
1657 ? lookup_fundamental_type (current_objfile
,
1658 FT_UNSIGNED_INTEGER
)
1666 SYMBOL_CLASS (sym
) = LOC_TYPEDEF
;
1667 SYMBOL_NAMESPACE (sym
) = VAR_NAMESPACE
;
1669 /* If type has no name, give it one */
1670 if (TYPE_NAME (SYMBOL_TYPE (sym
)) == 0)
1672 if (TYPE_CODE (SYMBOL_TYPE (sym
)) == TYPE_CODE_PTR
1673 || TYPE_CODE (SYMBOL_TYPE (sym
)) == TYPE_CODE_FUNC
)
1675 /* If we are giving a name to a type such as "pointer to
1676 foo" or "function returning foo", we better not set
1677 the TYPE_NAME. If the program contains "typedef char
1678 *caddr_t;", we don't want all variables of type char
1679 * to print as caddr_t. This is not just a
1680 consequence of GDB's type management; CC and GCC (at
1681 least through version 2.4) both output variables of
1682 either type char * or caddr_t with the type
1683 refering to the C_TPDEF symbol for caddr_t. If a future
1684 compiler cleans this up it GDB is not ready for it
1685 yet, but if it becomes ready we somehow need to
1686 disable this check (without breaking the PCC/GCC2.4
1691 Fortunately, this check seems not to be necessary
1692 for anything except pointers or functions. */
1696 TYPE_NAME (SYMBOL_TYPE (sym
)) =
1697 concat (SYMBOL_NAME (sym
), NULL
);
1700 /* Ignore vendor section for Harris CX/UX targets. */
1701 else if (cs
->c_name
[0] == '$')
1703 #endif /* CXUX_TARGET */
1705 /* Keep track of any type which points to empty structured type,
1706 so it can be filled from a definition from another file. A
1707 simple forward reference (TYPE_CODE_UNDEF) is not an
1708 empty structured type, though; the forward references
1709 work themselves out via the magic of coff_lookup_type. */
1710 if (TYPE_CODE (SYMBOL_TYPE (sym
)) == TYPE_CODE_PTR
&&
1711 TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym
))) == 0 &&
1712 TYPE_CODE (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym
))) !=
1715 register int i
= hashname (SYMBOL_NAME (sym
));
1717 SYMBOL_VALUE_CHAIN (sym
) = opaque_type_chain
[i
];
1718 opaque_type_chain
[i
] = sym
;
1720 add_symbol_to_list (sym
, &file_symbols
);
1726 SYMBOL_CLASS (sym
) = LOC_TYPEDEF
;
1727 SYMBOL_NAMESPACE (sym
) = STRUCT_NAMESPACE
;
1729 /* Some compilers try to be helpful by inventing "fake"
1730 names for anonymous enums, structures, and unions, like
1731 "~0fake" or ".0fake". Thanks, but no thanks... */
1732 if (TYPE_TAG_NAME (SYMBOL_TYPE (sym
)) == 0)
1733 if (SYMBOL_NAME (sym
) != NULL
1734 && *SYMBOL_NAME (sym
) != '~'
1735 && *SYMBOL_NAME (sym
) != '.')
1736 TYPE_TAG_NAME (SYMBOL_TYPE (sym
)) =
1737 concat (SYMBOL_NAME (sym
), NULL
);
1739 add_symbol_to_list (sym
, &file_symbols
);
1749 /* Decode a coff type specifier; return the type that is meant. */
1751 static struct type
*
1752 decode_type (cs
, c_type
, aux
)
1753 register struct coff_symbol
*cs
;
1754 unsigned int c_type
;
1755 register union internal_auxent
*aux
;
1757 register struct type
*type
= 0;
1758 unsigned int new_c_type
;
1760 if (c_type
& ~N_BTMASK
)
1762 new_c_type
= DECREF (c_type
);
1765 type
= decode_type (cs
, new_c_type
, aux
);
1766 type
= lookup_pointer_type (type
);
1768 else if (ISFCN (c_type
))
1770 type
= decode_type (cs
, new_c_type
, aux
);
1771 type
= lookup_function_type (type
);
1773 else if (ISARY (c_type
))
1776 register unsigned short *dim
;
1777 struct type
*base_type
, *index_type
, *range_type
;
1779 /* Define an array type. */
1780 /* auxent refers to array, not base type */
1781 if (aux
->x_sym
.x_tagndx
.l
== 0)
1784 /* shift the indices down */
1785 dim
= &aux
->x_sym
.x_fcnary
.x_ary
.x_dimen
[0];
1788 for (i
= 0; *dim
&& i
< DIMNUM
- 1; i
++, dim
++)
1792 base_type
= decode_type (cs
, new_c_type
, aux
);
1793 index_type
= lookup_fundamental_type (current_objfile
, FT_INTEGER
);
1795 create_range_type ((struct type
*) NULL
, index_type
, 0, n
- 1);
1797 create_array_type ((struct type
*) NULL
, base_type
, range_type
);
1802 /* Reference to existing type. This only occurs with the
1803 struct, union, and enum types. EPI a29k coff
1804 fakes us out by producing aux entries with a nonzero
1805 x_tagndx for definitions of structs, unions, and enums, so we
1806 have to check the c_sclass field. SCO 3.2v4 cc gets confused
1807 with pointers to pointers to defined structs, and generates
1808 negative x_tagndx fields. */
1809 if (cs
->c_naux
> 0 && aux
->x_sym
.x_tagndx
.l
!= 0)
1811 if (cs
->c_sclass
!= C_STRTAG
1812 && cs
->c_sclass
!= C_UNTAG
1813 && cs
->c_sclass
!= C_ENTAG
1814 && aux
->x_sym
.x_tagndx
.l
>= 0)
1816 type
= coff_alloc_type (aux
->x_sym
.x_tagndx
.l
);
1821 complain (&tagndx_bad_complaint
, cs
->c_name
);
1822 /* And fall through to decode_base_type... */
1826 return decode_base_type (cs
, BTYPE (c_type
), aux
);
1829 /* Decode a coff type specifier for function definition;
1830 return the type that the function returns. */
1832 static struct type
*
1833 decode_function_type (cs
, c_type
, aux
)
1834 register struct coff_symbol
*cs
;
1835 unsigned int c_type
;
1836 register union internal_auxent
*aux
;
1838 if (aux
->x_sym
.x_tagndx
.l
== 0)
1839 cs
->c_naux
= 0; /* auxent refers to function, not base type */
1841 return decode_type (cs
, DECREF (c_type
), aux
);
1846 static struct type
*
1847 decode_base_type (cs
, c_type
, aux
)
1848 register struct coff_symbol
*cs
;
1849 unsigned int c_type
;
1850 register union internal_auxent
*aux
;
1857 /* shows up with "void (*foo)();" structure members */
1858 return lookup_fundamental_type (current_objfile
, FT_VOID
);
1861 /* DGUX actually defines both T_ARG and T_VOID to the same value. */
1864 /* Shows up in DGUX, I think. Not sure where. */
1865 return lookup_fundamental_type (current_objfile
, FT_VOID
); /* shouldn't show up here */
1871 /* Intel 960 COFF has this symbol and meaning. */
1872 return lookup_fundamental_type (current_objfile
, FT_VOID
);
1876 return lookup_fundamental_type (current_objfile
, FT_CHAR
);
1879 return lookup_fundamental_type (current_objfile
, FT_SHORT
);
1882 return lookup_fundamental_type (current_objfile
, FT_INTEGER
);
1885 if (cs
->c_sclass
== C_FIELD
1886 && aux
->x_sym
.x_misc
.x_lnsz
.x_size
> TARGET_LONG_BIT
)
1887 return lookup_fundamental_type (current_objfile
, FT_LONG_LONG
);
1889 return lookup_fundamental_type (current_objfile
, FT_LONG
);
1892 return lookup_fundamental_type (current_objfile
, FT_FLOAT
);
1895 return lookup_fundamental_type (current_objfile
, FT_DBL_PREC_FLOAT
);
1898 return lookup_fundamental_type (current_objfile
, FT_EXT_PREC_FLOAT
);
1901 if (cs
->c_naux
!= 1)
1903 /* anonymous structure type */
1904 type
= coff_alloc_type (cs
->c_symnum
);
1905 TYPE_CODE (type
) = TYPE_CODE_STRUCT
;
1906 TYPE_NAME (type
) = NULL
;
1907 /* This used to set the tag to "<opaque>". But I think setting it
1908 to NULL is right, and the printing code can print it as
1910 TYPE_TAG_NAME (type
) = NULL
;
1911 INIT_CPLUS_SPECIFIC (type
);
1912 TYPE_LENGTH (type
) = 0;
1913 TYPE_FIELDS (type
) = 0;
1914 TYPE_NFIELDS (type
) = 0;
1918 type
= coff_read_struct_type (cs
->c_symnum
,
1919 aux
->x_sym
.x_misc
.x_lnsz
.x_size
,
1920 aux
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
);
1925 if (cs
->c_naux
!= 1)
1927 /* anonymous union type */
1928 type
= coff_alloc_type (cs
->c_symnum
);
1929 TYPE_NAME (type
) = NULL
;
1930 /* This used to set the tag to "<opaque>". But I think setting it
1931 to NULL is right, and the printing code can print it as
1933 TYPE_TAG_NAME (type
) = NULL
;
1934 INIT_CPLUS_SPECIFIC (type
);
1935 TYPE_LENGTH (type
) = 0;
1936 TYPE_FIELDS (type
) = 0;
1937 TYPE_NFIELDS (type
) = 0;
1941 type
= coff_read_struct_type (cs
->c_symnum
,
1942 aux
->x_sym
.x_misc
.x_lnsz
.x_size
,
1943 aux
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
);
1945 TYPE_CODE (type
) = TYPE_CODE_UNION
;
1949 if (cs
->c_naux
!= 1)
1951 /* anonymous enum type */
1952 type
= coff_alloc_type (cs
->c_symnum
);
1953 TYPE_CODE (type
) = TYPE_CODE_ENUM
;
1954 TYPE_NAME (type
) = NULL
;
1955 /* This used to set the tag to "<opaque>". But I think setting it
1956 to NULL is right, and the printing code can print it as
1958 TYPE_TAG_NAME (type
) = NULL
;
1959 TYPE_LENGTH (type
) = 0;
1960 TYPE_FIELDS (type
) = 0;
1961 TYPE_NFIELDS (type
) = 0;
1965 type
= coff_read_enum_type (cs
->c_symnum
,
1966 aux
->x_sym
.x_misc
.x_lnsz
.x_size
,
1967 aux
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
);
1972 /* shouldn't show up here */
1976 return lookup_fundamental_type (current_objfile
, FT_UNSIGNED_CHAR
);
1979 return lookup_fundamental_type (current_objfile
, FT_UNSIGNED_SHORT
);
1982 return lookup_fundamental_type (current_objfile
, FT_UNSIGNED_INTEGER
);
1985 if (cs
->c_sclass
== C_FIELD
1986 && aux
->x_sym
.x_misc
.x_lnsz
.x_size
> TARGET_LONG_BIT
)
1987 return lookup_fundamental_type (current_objfile
, FT_UNSIGNED_LONG_LONG
);
1989 return lookup_fundamental_type (current_objfile
, FT_UNSIGNED_LONG
);
1991 complain (&unexpected_type_complaint
, cs
->c_name
);
1992 return lookup_fundamental_type (current_objfile
, FT_VOID
);
1995 /* This page contains subroutines of read_type. */
1997 /* Read the description of a structure (or union type) and return an
1998 object describing the type. */
2000 static struct type
*
2001 coff_read_struct_type (index
, length
, lastsym
)
2008 struct nextfield
*next
;
2012 register struct type
*type
;
2013 register struct nextfield
*list
= 0;
2014 struct nextfield
*new;
2018 struct coff_symbol member_sym
;
2019 register struct coff_symbol
*ms
= &member_sym
;
2020 struct internal_syment sub_sym
;
2021 union internal_auxent sub_aux
;
2024 type
= coff_alloc_type (index
);
2025 TYPE_CODE (type
) = TYPE_CODE_STRUCT
;
2026 INIT_CPLUS_SPECIFIC (type
);
2027 TYPE_LENGTH (type
) = length
;
2029 while (!done
&& symnum
< lastsym
&& symnum
< nlist_nsyms_global
)
2031 read_one_sym (ms
, &sub_sym
, &sub_aux
);
2033 name
= EXTERNAL_NAME (name
, current_objfile
->obfd
);
2035 switch (ms
->c_sclass
)
2040 /* Get space to record the next field's data. */
2041 new = (struct nextfield
*) alloca (sizeof (struct nextfield
));
2045 /* Save the data. */
2049 ¤t_objfile
->symbol_obstack
);
2050 FIELD_TYPE (list
->field
) = decode_type (ms
, ms
->c_type
, &sub_aux
);
2051 FIELD_BITPOS (list
->field
) = 8 * ms
->c_value
;
2052 FIELD_BITSIZE (list
->field
) = 0;
2058 /* Get space to record the next field's data. */
2059 new = (struct nextfield
*) alloca (sizeof (struct nextfield
));
2063 /* Save the data. */
2067 ¤t_objfile
->symbol_obstack
);
2068 FIELD_TYPE (list
->field
) = decode_type (ms
, ms
->c_type
, &sub_aux
);
2069 FIELD_BITPOS (list
->field
) = ms
->c_value
;
2070 FIELD_BITSIZE (list
->field
) = sub_aux
.x_sym
.x_misc
.x_lnsz
.x_size
;
2079 /* Now create the vector of fields, and record how big it is. */
2081 TYPE_NFIELDS (type
) = nfields
;
2082 TYPE_FIELDS (type
) = (struct field
*)
2083 TYPE_ALLOC (type
, sizeof (struct field
) * nfields
);
2085 /* Copy the saved-up fields into the field vector. */
2087 for (n
= nfields
; list
; list
= list
->next
)
2088 TYPE_FIELD (type
, --n
) = list
->field
;
2093 /* Read a definition of an enumeration type,
2094 and create and return a suitable type object.
2095 Also defines the symbols that represent the values of the type. */
2098 static struct type
*
2099 coff_read_enum_type (index
, length
, lastsym
)
2104 register struct symbol
*sym
;
2105 register struct type
*type
;
2108 struct pending
**symlist
;
2109 struct coff_symbol member_sym
;
2110 register struct coff_symbol
*ms
= &member_sym
;
2111 struct internal_syment sub_sym
;
2112 union internal_auxent sub_aux
;
2113 struct pending
*osyms
, *syms
;
2117 int unsigned_enum
= 1;
2119 type
= coff_alloc_type (index
);
2120 if (within_function
)
2121 symlist
= &local_symbols
;
2123 symlist
= &file_symbols
;
2125 o_nsyms
= osyms
? osyms
->nsyms
: 0;
2127 while (!done
&& symnum
< lastsym
&& symnum
< nlist_nsyms_global
)
2129 read_one_sym (ms
, &sub_sym
, &sub_aux
);
2131 name
= EXTERNAL_NAME (name
, current_objfile
->obfd
);
2133 switch (ms
->c_sclass
)
2136 sym
= (struct symbol
*) obstack_alloc
2137 (¤t_objfile
->symbol_obstack
,
2138 sizeof (struct symbol
));
2139 memset (sym
, 0, sizeof (struct symbol
));
2142 obsavestring (name
, strlen (name
),
2143 ¤t_objfile
->symbol_obstack
);
2144 SYMBOL_CLASS (sym
) = LOC_CONST
;
2145 SYMBOL_NAMESPACE (sym
) = VAR_NAMESPACE
;
2146 SYMBOL_VALUE (sym
) = ms
->c_value
;
2147 add_symbol_to_list (sym
, symlist
);
2152 /* Sometimes the linker (on 386/ix 2.0.2 at least) screws
2153 up the count of how many symbols to read. So stop
2160 /* Now fill in the fields of the type-structure. */
2163 TYPE_LENGTH (type
) = length
;
2165 TYPE_LENGTH (type
) = TARGET_INT_BIT
/ TARGET_CHAR_BIT
; /* Assume ints */
2166 TYPE_CODE (type
) = TYPE_CODE_ENUM
;
2167 TYPE_NFIELDS (type
) = nsyms
;
2168 TYPE_FIELDS (type
) = (struct field
*)
2169 TYPE_ALLOC (type
, sizeof (struct field
) * nsyms
);
2171 /* Find the symbols for the values and put them into the type.
2172 The symbols can be found in the symlist that we put them on
2173 to cause them to be defined. osyms contains the old value
2174 of that symlist; everything up to there was defined by us. */
2175 /* Note that we preserve the order of the enum constants, so
2176 that in something like "enum {FOO, LAST_THING=FOO}" we print
2177 FOO, not LAST_THING. */
2179 for (syms
= *symlist
, n
= 0; syms
; syms
= syms
->next
)
2185 for (; j
< syms
->nsyms
; j
++, n
++)
2187 struct symbol
*xsym
= syms
->symbol
[j
];
2188 SYMBOL_TYPE (xsym
) = type
;
2189 TYPE_FIELD_NAME (type
, n
) = SYMBOL_NAME (xsym
);
2190 TYPE_FIELD_BITPOS (type
, n
) = SYMBOL_VALUE (xsym
);
2191 if (SYMBOL_VALUE (xsym
) < 0)
2193 TYPE_FIELD_BITSIZE (type
, n
) = 0;
2200 TYPE_FLAGS (type
) |= TYPE_FLAG_UNSIGNED
;
2205 /* Register our ability to parse symbols for coff BFD files. */
2207 static struct sym_fns coff_sym_fns
=
2209 bfd_target_coff_flavour
,
2210 coff_new_init
, /* sym_new_init: init anything gbl to entire symtab */
2211 coff_symfile_init
, /* sym_init: read initial info, setup for sym_read() */
2212 coff_symfile_read
, /* sym_read: read a symbol file into symtab */
2213 coff_symfile_finish
, /* sym_finish: finished with file, cleanup */
2214 default_symfile_offsets
,
2215 /* sym_offsets: xlate external to internal form */
2216 NULL
/* next: pointer to next struct sym_fns */
2220 _initialize_coffread ()
2222 add_symtab_fns (&coff_sym_fns
);