1 /* Read coff symbol tables and convert to internal format, for GDB.
2 Design and support routines derived from dbxread.c, and UMAX COFF
3 specific routines written 9/1/87 by David D. Johnson, Brown University.
4 Revised 11/27/87 ddj@cs.brown.edu
5 Copyright (C) 1987 Free Software Foundation, Inc.
7 GDB is distributed in the hope that it will be useful, but WITHOUT ANY
8 WARRANTY. No author or distributor accepts responsibility to anyone
9 for the consequences of using it or for whether it serves any
10 particular purpose or works at all, unless he says so in writing.
11 Refer to the GDB General Public License for full details.
13 Everyone is granted permission to copy, modify and redistribute GDB,
14 but only under the conditions described in the GDB General Public
15 License. A copy of this license is supposed to have been given to you
16 along with GDB so you can know your rights and responsibilities. It
17 should be in a file named COPYING. Among other things, the copyright
18 notice and this notice must be preserved on all copies.
20 In other words, go ahead and share GDB, but don't try to stop
21 anyone else from sharing it farther. Help stamp out software hoarding!
27 #include "initialize.h"
33 #include <sys/types.h>
34 #include <sys/param.h>
37 static void add_symbol_to_list ();
38 static void read_coff_symtab ();
39 static void patch_opaque_types ();
40 static struct type
*decode_function_type ();
41 static struct type
*decode_type ();
42 static struct type
*decode_base_type ();
43 static struct type
*read_enum_type ();
44 static struct type
*read_struct_type ();
45 static void finish_block ();
46 static struct blockvector
*make_blockvector ();
47 static struct symbol
*process_coff_symbol ();
48 static int init_stringtab ();
49 static void free_stringtab ();
50 static char *getfilename ();
51 static char *getsymname ();
52 static int init_lineno ();
53 static void enter_linenos ();
57 /* Name of source file whose symbol data we are now processing.
58 This comes from a symbol named ".file". */
60 static char *last_source_file
;
62 /* Core address of start and end of text of current source file.
63 This comes from a ".text" symbol where x_nlinno > 0. */
65 static CORE_ADDR cur_src_start_addr
;
66 static CORE_ADDR cur_src_end_addr
;
68 /* End of the text segment of the executable file,
69 as found in the symbol _etext. */
71 static CORE_ADDR end_of_text_addr
;
73 /* The addresses of the symbol table stream and number of symbols
74 of the object file we are reading (as copied into core). */
76 static FILE *nlist_stream_global
;
77 static int nlist_nsyms_global
;
79 /* The file and text section headers of the symbol file */
81 static FILHDR file_hdr
;
82 static SCNHDR text_hdr
;
84 /* The index in the symbol table of the last coff symbol that was processed. */
88 /* Vector of types defined so far, indexed by their coff symnum. */
90 static struct typevector
*type_vector
;
92 /* Number of elements allocated for type_vector currently. */
94 static int type_vector_length
;
96 /* Vector of line number information. */
98 static struct linetable
*line_vector
;
100 /* Index of next entry to go in line_vector_index. */
102 static int line_vector_index
;
104 /* Last line number recorded in the line vector. */
106 static int prev_line_number
;
108 /* Number of elements allocated for line_vector currently. */
110 static int line_vector_length
;
112 /* Chain of typedefs of pointers to empty struct/union types.
113 They are chained thru the SYMBOL_VALUE. */
116 static struct symbol
*opaque_type_chain
[HASHSIZE
];
118 /* Record the symbols defined for each context in a list.
119 We don't create a struct block for the context until we
120 know how long to make it. */
124 struct pending
*next
;
125 struct symbol
*symbol
;
128 /* Here are the three lists that symbols are put on. */
130 struct pending
*file_symbols
; /* static at top level, and types */
132 struct pending
*global_symbols
; /* global functions and variables */
134 struct pending
*local_symbols
; /* everything local to lexical context */
136 /* List of unclosed lexical contexts
137 (that will become blocks, eventually). */
141 struct context_stack
*next
;
142 struct pending
*locals
;
143 struct pending_block
*old_blocks
;
145 CORE_ADDR start_addr
;
149 struct context_stack
*context_stack
;
151 /* Nonzero if within a function (so symbols should be local,
152 if nothing says specifically). */
156 /* List of blocks already made (lexical contexts already closed).
157 This is used at the end to make the blockvector. */
161 struct pending_block
*next
;
165 struct pending_block
*pending_blocks
;
167 extern CORE_ADDR first_object_file_end
; /* From blockframe.c */
169 /* File name symbols were loaded from. */
171 static char *symfile
;
176 /* Look up a coff type-number index. Return the address of the slot
177 where the type for that index is stored.
178 The type-number is in INDEX.
180 This can be used for finding the type associated with that index
181 or for associating a new type with the index. */
183 static struct type
**
184 coff_lookup_type (index
)
187 if (index
>= type_vector_length
)
189 type_vector_length
*= 2;
190 type_vector
= (struct typevector
*)
191 xrealloc (type_vector
, sizeof (struct typevector
)
192 + type_vector_length
* sizeof (struct type
*));
193 bzero (&type_vector
->type
[type_vector_length
/ 2],
194 type_vector_length
* sizeof (struct type
*) / 2);
196 return &type_vector
->type
[index
];
199 /* Make sure there is a type allocated for type number index
200 and return the type object.
201 This can create an empty (zeroed) type object. */
204 coff_alloc_type (index
)
207 register struct type
**type_addr
= coff_lookup_type (index
);
208 register struct type
*type
= *type_addr
;
210 /* If we are referring to a type not known at all yet,
211 allocate an empty type for it.
212 We will fill it in later if we find out how. */
215 type
= (struct type
*) obstack_alloc (symbol_obstack
,
216 sizeof (struct type
));
217 bzero (type
, sizeof (struct type
));
223 /* maintain the lists of symbols and blocks */
225 /* Add a symbol to one of the lists of symbols. */
227 add_symbol_to_list (symbol
, listhead
)
228 struct symbol
*symbol
;
229 struct pending
**listhead
;
231 register struct pending
*link
232 = (struct pending
*) xmalloc (sizeof (struct pending
));
234 link
->next
= *listhead
;
235 link
->symbol
= symbol
;
239 /* Take one of the lists of symbols and make a block from it.
240 Put the block on the list of pending blocks. */
243 finish_block (symbol
, listhead
, old_blocks
, start
, end
)
244 struct symbol
*symbol
;
245 struct pending
**listhead
;
246 struct pending_block
*old_blocks
;
247 CORE_ADDR start
, end
;
249 register struct pending
*next
, *next1
;
250 register struct block
*block
;
251 register struct pending_block
*pblock
;
252 struct pending_block
*opblock
;
255 /* Count the length of the list of symbols. */
257 for (next
= *listhead
, i
= 0; next
; next
= next
->next
, i
++);
259 block
= (struct block
*) xmalloc (sizeof (struct block
) + (i
- 1) * sizeof (struct symbol
*));
261 /* Copy the symbols into the block. */
263 BLOCK_NSYMS (block
) = i
;
264 for (next
= *listhead
; next
; next
= next
->next
)
265 BLOCK_SYM (block
, --i
) = next
->symbol
;
267 BLOCK_START (block
) = start
;
268 BLOCK_END (block
) = end
;
269 BLOCK_SUPERBLOCK (block
) = 0; /* Filled in when containing block is made */
271 /* Put the block in as the value of the symbol that names it. */
275 SYMBOL_BLOCK_VALUE (symbol
) = block
;
276 BLOCK_FUNCTION (block
) = symbol
;
279 BLOCK_FUNCTION (block
) = 0;
281 /* Now free the links of the list, and empty the list. */
283 for (next
= *listhead
; next
; next
= next1
)
290 /* Install this block as the superblock
291 of all blocks made since the start of this scope
292 that don't have superblocks yet. */
295 for (pblock
= pending_blocks
; pblock
!= old_blocks
; pblock
= pblock
->next
)
297 if (BLOCK_SUPERBLOCK (pblock
->block
) == 0)
298 BLOCK_SUPERBLOCK (pblock
->block
) = block
;
302 /* Record this block on the list of all blocks in the file.
303 Put it after opblock, or at the beginning if opblock is 0.
304 This puts the block in the list after all its subblocks. */
306 pblock
= (struct pending_block
*) xmalloc (sizeof (struct pending_block
));
307 pblock
->block
= block
;
310 pblock
->next
= opblock
->next
;
311 opblock
->next
= pblock
;
315 pblock
->next
= pending_blocks
;
316 pending_blocks
= pblock
;
320 static struct blockvector
*
323 register struct pending_block
*next
, *next1
;
324 register struct blockvector
*blockvector
;
327 /* Count the length of the list of blocks. */
329 for (next
= pending_blocks
, i
= 0; next
; next
= next
->next
, i
++);
331 blockvector
= (struct blockvector
*) xmalloc (sizeof (struct blockvector
) + (i
- 1) * sizeof (struct block
*));
333 /* Copy the blocks into the blockvector.
334 This is done in reverse order, which happens to put
335 the blocks into the proper order (ascending starting address).
336 finish_block has hair to insert each block into the list
337 after its subblocks in order to make sure this is true. */
339 BLOCKVECTOR_NBLOCKS (blockvector
) = i
;
340 for (next
= pending_blocks
; next
; next
= next
->next
)
341 BLOCKVECTOR_BLOCK (blockvector
, --i
) = next
->block
;
343 /* Now free the links of the list, and empty the list. */
345 for (next
= pending_blocks
; next
; next
= next1
)
355 /* Manage the vector of line numbers. */
358 record_line (line
, pc
)
362 /* Make sure line vector is big enough. */
364 if (line_vector_index
+ 2 >= line_vector_length
)
366 line_vector_length
*= 2;
367 line_vector
= (struct linetable
*)
368 xrealloc (line_vector
, sizeof (struct linetable
)
369 + line_vector_length
* sizeof (int));
372 /* If this line is not continguous with previous one recorded,
373 all lines between subsequent line and current one are same pc.
374 Add one item to line vector, and if more than one line skipped,
375 record a line-number entry for it. */
376 if (prev_line_number
> 0 && line
!= prev_line_number
+ 1)
377 line_vector
->item
[line_vector_index
++] = pc
;
378 if (prev_line_number
< 0 || line
> prev_line_number
+ 2)
379 line_vector
->item
[line_vector_index
++] = - line
;
380 prev_line_number
= line
;
382 /* Record the core address of the line. */
383 line_vector
->item
[line_vector_index
++] = pc
;
386 /* Start a new symtab for a new source file.
387 This is called when a COFF ".file" symbol is seen;
388 it indicates the start of data for one original source file. */
397 last_source_file
= 0;
399 /* Initialize the source file information for this file. */
401 line_vector_index
= 0;
402 line_vector_length
= 1000;
403 prev_line_number
= -2; /* Force first line number to be explicit */
404 line_vector
= (struct linetable
*)
405 xmalloc (sizeof (struct linetable
) + line_vector_length
* sizeof (int));
408 /* Save the vital information for use when closing off the current file.
409 NAME is the file name the symbols came from, START_ADDR is the first
410 text address for the file, and SIZE is the number of bytes of text. */
413 complete_symtab (name
, start_addr
, size
)
415 CORE_ADDR start_addr
;
418 last_source_file
= savestring (name
, strlen (name
));
419 cur_src_start_addr
= start_addr
;
420 cur_src_end_addr
= start_addr
+ size
;
423 /* Finish the symbol definitions for one main source file,
424 close off all the lexical contexts for that file
425 (creating struct block's for them), then make the
426 struct symtab for that file and put it in the list of all such. */
431 register struct symtab
*symtab
;
432 register struct context_stack
*cstk
;
433 register struct blockvector
*blockvector
;
434 register struct linetable
*lv
;
436 /* Finish the lexical context of the last function in the file. */
440 cstk
= context_stack
;
441 /* Make a block for the local symbols within. */
442 finish_block (cstk
->name
, &local_symbols
, cstk
->old_blocks
,
443 cstk
->start_addr
, cur_src_end_addr
);
447 finish_block (0, &file_symbols
, 0, cur_src_start_addr
, cur_src_end_addr
);
448 finish_block (0, &global_symbols
, 0, cur_src_start_addr
, cur_src_end_addr
);
449 blockvector
= make_blockvector ();
451 /* Now create the symtab objects proper this source file. */
453 symtab
= (struct symtab
*) xmalloc (sizeof (struct symtab
));
454 /* Fill in its components. */
455 symtab
->blockvector
= blockvector
;
456 symtab
->free_code
= free_contents
;
457 symtab
->free_ptr
= 0;
458 symtab
->filename
= last_source_file
;
460 lv
->nitems
= line_vector_index
;
461 symtab
->linetable
= (struct linetable
*)
462 xrealloc (lv
, sizeof (struct linetable
) + lv
->nitems
* sizeof (int));
464 symtab
->line_charpos
= 0;
466 /* Link the new symtab into the list of such. */
467 symtab
->next
= symtab_list
;
468 symtab_list
= symtab
;
471 line_vector_length
= -1;
472 last_source_file
= 0;
475 /* Accumulate the misc functions in bunches of 127.
476 At the end, copy them all into one newly allocated structure. */
478 #define MISC_BUNCH_SIZE 127
482 struct misc_bunch
*next
;
483 struct misc_function contents
[MISC_BUNCH_SIZE
];
486 /* Bunch currently being filled up.
487 The next field points to chain of filled bunches. */
489 static struct misc_bunch
*misc_bunch
;
491 /* Number of slots filled in current bunch. */
493 static int misc_bunch_index
;
495 /* Total number of misc functions recorded so far. */
497 static int misc_count
;
500 init_misc_functions ()
504 misc_bunch_index
= MISC_BUNCH_SIZE
;
508 record_misc_function (name
, address
)
512 register struct misc_bunch
*new;
514 if (misc_bunch_index
== MISC_BUNCH_SIZE
)
516 new = (struct misc_bunch
*) xmalloc (sizeof (struct misc_bunch
));
517 misc_bunch_index
= 0;
518 new->next
= misc_bunch
;
521 misc_bunch
->contents
[misc_bunch_index
].name
= savestring (name
, strlen (name
));
522 misc_bunch
->contents
[misc_bunch_index
].address
= address
;
528 compare_misc_functions (fn1
, fn2
)
529 struct misc_function
*fn1
, *fn2
;
531 /* Return a signed result based on unsigned comparisons
532 so that we sort into unsigned numeric order. */
533 if (fn1
->address
< fn2
->address
)
535 if (fn1
->address
> fn2
->address
)
541 discard_misc_bunches ()
543 register struct misc_bunch
*next
;
547 next
= misc_bunch
->next
;
554 condense_misc_bunches ()
557 register struct misc_bunch
*bunch
;
558 #ifdef NAMES_HAVE_UNDERSCORE
565 = (struct misc_function
*)
566 xmalloc (misc_count
* sizeof (struct misc_function
));
572 for (i
= 0; i
< misc_bunch_index
; i
++)
576 misc_function_vector
[j
] = bunch
->contents
[i
];
577 tmp
= misc_function_vector
[j
].name
;
578 misc_function_vector
[j
].name
= (tmp
[0] == '_' ? tmp
+ offset
: tmp
);
582 misc_bunch_index
= MISC_BUNCH_SIZE
;
585 misc_function_count
= j
;
587 /* Sort the misc functions by address. */
589 qsort (misc_function_vector
, j
, sizeof (struct misc_function
),
590 compare_misc_functions
);
593 /* Call sort_syms to sort alphabetically
594 the symbols of each block of each symtab. */
597 compare_symbols (s1
, s2
)
598 struct symbol
**s1
, **s2
;
600 /* Names that are less should come first. */
601 register int namediff
= strcmp (SYMBOL_NAME (*s1
), SYMBOL_NAME (*s2
));
602 if (namediff
!= 0) return namediff
;
603 /* For symbols of the same name, registers should come first. */
604 return ((SYMBOL_CLASS (*s2
) == LOC_REGISTER
)
605 - (SYMBOL_CLASS (*s1
) == LOC_REGISTER
));
611 register struct symtab
*s
;
613 register struct blockvector
*bv
;
614 register struct block
*b
;
616 for (s
= symtab_list
; s
; s
= s
->next
)
618 bv
= BLOCKVECTOR (s
);
619 nbl
= BLOCKVECTOR_NBLOCKS (bv
);
620 for (i
= 0; i
< nbl
; i
++)
622 b
= BLOCKVECTOR_BLOCK (bv
, i
);
623 qsort (&BLOCK_SYM (b
, 0), BLOCK_NSYMS (b
),
624 sizeof (struct symbol
*), compare_symbols
);
629 /* This is the symbol-file command. Read the file, analyze its symbols,
630 and add a struct symtab to symtab_list. */
633 symbol_file_command (name
)
640 extern void close ();
642 struct cleanup
*old_chain
;
648 if (symtab_list
&& !query ("Discard symbol table? ", 0))
649 error ("Not confirmed.");
654 if (symtab_list
&& !query ("Load new symbol table from \"%s\"? ", name
))
655 error ("Not confirmed.");
664 desc
= openp (getenv ("PATH"), 1, name
, O_RDONLY
, 0, &absolute_name
);
666 perror_with_name (name
);
668 name
= absolute_name
;
671 old_chain
= make_cleanup (close
, desc
);
672 make_cleanup (free_current_contents
, &name
);
674 if ((num_symbols
= read_file_hdr (desc
, &file_hdr
)) < 0)
675 error ("File \"%s\" not in executable format.", name
);
677 if (num_symbols
== 0)
680 printf ("%s does not have a symbol-table.\n", name
);
685 num_sections
= file_hdr
.f_nscns
;
686 symtab_offset
= file_hdr
.f_symptr
;
688 if (read_section_hdr (desc
, _TEXT
, &text_hdr
, num_sections
) < 0)
689 error ("\"%s\": can't read text section header", name
);
691 /* Read the line number table, all at once. */
693 val
= init_lineno (desc
, text_hdr
.s_lnnoptr
, text_hdr
.s_nlnno
);
695 error ("\"%s\": error reading line numbers\n", name
);
697 /* Now read the string table, all at once. */
699 val
= init_stringtab (desc
, symtab_offset
+ num_symbols
* SYMESZ
);
701 error ("\"%s\": can't get string table", name
);
702 make_cleanup (free_stringtab
, 0);
704 /* Position to read the symbol table. Do not read it all at once. */
705 val
= lseek (desc
, (long)symtab_offset
, 0);
707 perror_with_name (name
);
709 printf ("Reading symbol data from %s...", name
);
712 /* Throw away the old symbol table. */
716 init_misc_functions ();
717 make_cleanup (discard_misc_bunches
, 0);
719 /* Now that the executable file is positioned at symbol table,
720 process it and define symbols accordingly. */
722 read_coff_symtab (desc
, num_symbols
);
724 patch_opaque_types ();
726 /* Sort symbols alphabetically within each block. */
730 /* Go over the misc functions and install them in vector. */
732 condense_misc_bunches ();
734 /* Don't allow char * to have a typename (else would get caddr_t.) */
736 TYPE_NAME (lookup_pointer_type (builtin_type_char
)) = 0;
738 /* Make a default for file to list. */
740 select_source_symtab (symtab_list
);
742 symfile
= savestring (name
, strlen (name
));
744 do_cleanups (old_chain
);
750 /* Return name of file symbols were loaded from, or 0 if none.. */
758 /* Simplified internal version of coff symbol table information */
762 int c_symnum
; /* symbol number of this entry */
763 int c_nsyms
; /* 1 if syment only, 2 if syment + auxent */
770 /* Given pointers to a symbol table in coff style exec file,
771 analyze them and create struct symtab's describing the symbols.
772 NSYMS is the number of symbols in the symbol table.
773 We read them one at a time using read_one_sym (). */
776 read_coff_symtab (desc
, nsyms
)
780 FILE *stream
= fdopen (desc
, "r");
781 register struct context_stack
*new;
782 struct coff_symbol coff_symbol
;
783 register struct coff_symbol
*cs
= &coff_symbol
;
784 static SYMENT main_sym
;
785 static AUXENT main_aux
;
787 int num_object_files
= 0;
788 int next_file_symnum
= 0;
795 nlist_stream_global
= stream
;
796 nlist_nsyms_global
= nsyms
;
797 last_source_file
= 0;
798 bzero (opaque_type_chain
, sizeof opaque_type_chain
);
800 type_vector_length
= 160;
801 type_vector
= (struct typevector
*)
802 xmalloc (sizeof (struct typevector
)
803 + type_vector_length
* sizeof (struct type
*));
804 bzero (type_vector
->type
, type_vector_length
* sizeof (struct type
*));
809 while (symnum
< nsyms
)
811 read_one_sym (cs
, &main_sym
, &main_aux
);
814 * If we are finished with the previous file's symbols, and the
815 * next thing is not a C_FILE, then we have hit the global symbols.
817 if (cs
->c_symnum
== next_file_symnum
&& cs
->c_sclass
!= C_FILE
)
819 CORE_ADDR last_file_end
= cur_src_end_addr
;
821 if (last_source_file
)
825 complete_symtab ("_globals_", 0, first_object_file_end
);
826 /* done with all files, everything from here on out is globals */
829 if (ISFCN (cs
->c_type
))
832 * gdb expects all functions to also be in misc_function
835 record_misc_function (cs
->c_name
, cs
->c_value
);
837 fcn_line_ptr
= main_aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
;
840 new = (struct context_stack
*)
841 xmalloc (sizeof (struct context_stack
));
842 new->depth
= depth
= 0;
846 new->old_blocks
= pending_blocks
;
847 new->start_addr
= cs
->c_value
;
848 new->name
= process_coff_symbol (cs
, &main_aux
);
852 switch (cs
->c_sclass
)
861 printf ("Bad n_sclass = %d\n", cs
->c_sclass
);
866 * c_value field contains symnum of next .file entry in table
867 * or symnum of first global after last .file.
869 next_file_symnum
= cs
->c_value
;
870 filestring
= getfilename (&main_aux
);
872 * Complete symbol table for last object file
873 * containing debugging information.
875 if (last_source_file
)
880 complete_symtab (filestring
, 0, 0); /* FIXME, 0 0 is wrong */
885 if (cs
->c_secnum
== N_ABS
&& strcmp (cs
->c_name
, _ETEXT
) == 0)
887 end_of_text_addr
= cs
->c_value
;
889 if (cs
->c_type
== T_NULL
)
891 if (cs
->c_secnum
<= 1) /* text or abs */
893 record_misc_function (cs
->c_name
, cs
->c_value
);
899 (void) process_coff_symbol (cs
, &main_aux
);
903 if (cs
->c_type
== T_NULL
&& cs
->c_secnum
> N_UNDEF
)
905 /* These ".text", ".data", ".bss" entries don't seem to
906 * appear in A/UX COFF output. -- gnu@toad.com 4Apr88
908 if (strcmp (cs
->c_name
, _TEXT
) == 0)
910 /* We have a ".text" symbol */
911 if (num_object_files
== 1)
913 /* Record end address of first file, crt0.s */
914 first_object_file_end
=
915 cs
->c_value
+ main_aux
.x_scn
.x_scnlen
;
918 * Fill in missing information for debugged
919 * object file only if we have line number info.
921 if (main_aux
.x_scn
.x_nlinno
> 0)
923 complete_symtab (filestring
, cs
->c_value
,
924 main_aux
.x_scn
.x_scnlen
);
928 else if (strcmp (cs
->c_name
, _DATA
) == 0)
930 else if (strcmp (cs
->c_name
, _BSS
) == 0)
933 /* get rid of assembly labels here */
934 /* record_misc_function (cs->c_name, cs->c_value); */
937 (void) process_coff_symbol (cs
, &main_aux
);
941 if (strcmp (cs
->c_name
, ".bf") == 0)
943 /* value contains address of first non-init type code */
944 /* main_aux.x_sym.x_misc.x_lnsz.x_lnno
945 contains line number of '{' } */
946 fcn_first_line
= main_aux
.x_sym
.x_misc
.x_lnsz
.x_lnno
;
948 else if (strcmp (cs
->c_name
, ".ef") == 0)
950 /* value contains address of exit/return from function */
951 /* round it up to next multiple of 16 */
952 cs
->c_value
= (cs
->c_value
+ 15) & -16;
953 /* { main_aux.x_sym.x_misc.x_lnsz.x_lnno
954 contains number of lines to '}' */
955 fcn_last_line
= main_aux
.x_sym
.x_misc
.x_lnsz
.x_lnno
;
956 enter_linenos (fcn_line_ptr
, fcn_first_line
, fcn_last_line
);
959 finish_block (new->name
, &local_symbols
, new->old_blocks
,
960 new->start_addr
, cs
->c_value
);
968 if (strcmp (cs
->c_name
, ".bb") == 0)
970 new = (struct context_stack
*)
971 xmalloc (sizeof (struct context_stack
));
974 new->next
= context_stack
;
976 new->locals
= local_symbols
;
977 new->old_blocks
= pending_blocks
;
978 new->start_addr
= cs
->c_value
;
982 else if (strcmp (cs
->c_name
, ".eb") == 0)
985 if (new == 0 || depth
!= new->depth
)
986 error ("Invalid symbol data: .bb/.eb symbol mismatch.");
987 if (local_symbols
&& context_stack
->next
)
989 /* Make a block for the local symbols within. */
990 finish_block (0, &local_symbols
, new->old_blocks
,
991 new->start_addr
, cs
->c_value
);
994 local_symbols
= new->locals
;
995 context_stack
= new->next
;
1001 (void) process_coff_symbol (cs
, &main_aux
);
1006 if (last_source_file
)
1011 /* Routines for reading headers and symbols from executable. */
1013 /* Read COFF file header, check magic number,
1014 and return number of symbols. */
1015 read_file_hdr (chan
, file_hdr
)
1019 lseek (chan
, 0L, 0);
1020 if (myread (chan
, (char *)file_hdr
, FILHSZ
) < 0)
1023 if (BADMAG(file_hdr
))
1024 return -1; /* Non understood file */
1025 return file_hdr
->f_nsyms
; /* OK magic number, return # syms */
1028 read_aout_hdr (chan
, aout_hdr
, size
)
1033 lseek (chan
, (long)FILHSZ
, 0);
1034 if (size
!= sizeof (AOUTHDR
))
1036 if (myread (chan
, (char *)aout_hdr
, size
) != size
)
1041 read_section_hdr (chan
, section_name
, section_hdr
, nsects
)
1043 register char *section_name
;
1044 SCNHDR
*section_hdr
;
1045 register int nsects
;
1049 if (lseek (chan
, FILHSZ
+ sizeof (AOUTHDR
), 0) < 0)
1052 for (i
= 0; i
< nsects
; i
++)
1054 if (myread (chan
, (char *)section_hdr
, SCNHSZ
) < 0)
1056 if (strncmp (section_hdr
->s_name
, section_name
, 8) == 0)
1062 read_one_sym (cs
, sym
, aux
)
1063 register struct coff_symbol
*cs
;
1064 register SYMENT
*sym
;
1065 register AUXENT
*aux
;
1067 cs
->c_symnum
= symnum
;
1068 fread ((char *)sym
, SYMESZ
, 1, nlist_stream_global
);
1069 cs
->c_nsyms
= (sym
->n_numaux
& 0xff) + 1;
1070 if (cs
->c_nsyms
== 2)
1072 /* doc for coff says there is either no aux entry or just one */
1073 fread ((char *)aux
, AUXESZ
, 1, nlist_stream_global
);
1075 else if (cs
->c_nsyms
> 2)
1076 error ("more than one aux symbol table entry at symnum=%d\n", symnum
);
1078 cs
->c_name
= getsymname (sym
);
1079 cs
->c_value
= sym
->n_value
;
1080 cs
->c_sclass
= (sym
->n_sclass
& 0xff);
1081 cs
->c_secnum
= sym
->n_scnum
;
1082 cs
->c_type
= (unsigned) sym
->n_type
;
1086 fprintf(stderr
, "sym %3x: %2x %s %x %x %x", cs
->c_symnum
,
1087 cs
->c_sclass
, cs
->c_name
, cs
->c_value
, cs
->c_secnum
, cs
->c_type
);
1088 if (cs
->c_nsyms
> 1)
1089 fprintf(stderr
, " +aux %s\n", (char *)aux
);
1091 fprintf(stderr
, "\n");
1095 symnum
+= cs
->c_nsyms
;
1098 /* Support for string table handling */
1100 static char *stringtab
= NULL
;
1103 init_stringtab (chan
, offset
)
1110 if (lseek (chan
, offset
, 0) < 0)
1113 val
= myread (chan
, (char *)&buffer
, sizeof buffer
);
1115 /* If no string table, we get 0 bytes back from the read. That's OK. */
1121 if (val
!= sizeof buffer
)
1126 stringtab
= (char *) xmalloc (buffer
);
1127 bcopy (&buffer
, stringtab
, sizeof buffer
);
1129 val
= myread (chan
, stringtab
+ sizeof buffer
, buffer
- sizeof buffer
);
1130 if (val
!= buffer
- sizeof buffer
|| stringtab
[buffer
- 1] != '\0')
1145 getsymname (symbol_entry
)
1146 SYMENT
*symbol_entry
;
1148 static char buffer
[SYMNMLEN
+1];
1151 if (symbol_entry
->n_zeroes
== 0)
1154 error("Symbol entry references nonexistent string table");
1155 result
= stringtab
+ symbol_entry
->n_offset
;
1159 strncpy (buffer
, symbol_entry
->n_name
, SYMNMLEN
);
1160 buffer
[SYMNMLEN
] = '\0';
1167 getfilename (aux_entry
)
1170 static char buffer
[BUFSIZ
];
1171 register char *temp
;
1173 extern char *rindex ();
1176 if (aux_entry
->x_file
.x_foff
!= 0)
1177 strcpy (buffer
, stringtab
+ aux_entry
->x_file
.x_foff
);
1181 strncpy (buffer
, aux_entry
->x_file
.x_fname
, FILNMLEN
);
1182 buffer
[FILNMLEN
] = '\0';
1185 if ((temp
= rindex (result
, '/')) != NULL
)
1190 /* Support for line number handling */
1191 static char *linetab
= NULL
;
1192 static long linetab_offset
;
1193 static int linetab_count
;
1196 init_lineno (chan
, offset
, count
)
1203 if (lseek (chan
, offset
, 0) < 0)
1208 linetab
= (char *) xmalloc (count
* LINESZ
);
1210 val
= myread (chan
, linetab
, count
* LINESZ
);
1211 if (val
!= count
* LINESZ
)
1214 linetab_offset
= offset
;
1215 linetab_count
= count
;
1220 enter_linenos (file_offset
, first_line
, last_line
)
1222 register int first_line
;
1223 register int last_line
;
1225 register char *rawptr
= &linetab
[file_offset
- linetab_offset
];
1226 register struct lineno
*lptr
;
1228 /* skip first line entry for each function */
1230 /* line numbers start at one for the first line of the function */
1233 for (lptr
= (struct lineno
*)rawptr
;
1234 lptr
->l_lnno
&& lptr
->l_lnno
<= last_line
;
1235 rawptr
+= LINESZ
, lptr
= (struct lineno
*)rawptr
)
1237 record_line (first_line
+ lptr
->l_lnno
, lptr
->l_addr
.l_paddr
);
1245 register char *p
= name
;
1246 register int total
= p
[0];
1259 return total
% HASHSIZE
;
1263 patch_type (type
, real_type
)
1265 struct type
*real_type
;
1267 register struct type
*target
= TYPE_TARGET_TYPE (type
);
1268 register struct type
*real_target
= TYPE_TARGET_TYPE (real_type
);
1269 int field_size
= TYPE_NFIELDS (real_target
) * sizeof (struct field
);
1271 TYPE_LENGTH (target
) = TYPE_LENGTH (real_target
);
1272 TYPE_NFIELDS (target
) = TYPE_NFIELDS (real_target
);
1273 TYPE_FIELDS (target
) = (struct field
*)
1274 obstack_alloc (symbol_obstack
, field_size
);
1276 bcopy (TYPE_FIELDS (real_target
), TYPE_FIELDS (target
), field_size
);
1278 if (TYPE_NAME (real_target
))
1280 if (TYPE_NAME (target
))
1281 free (TYPE_NAME (target
));
1282 TYPE_NAME (target
) = concat (TYPE_NAME (real_target
), "", "");
1286 /* Patch up all appropriate typdef symbols in the opaque_type_chains
1287 so that they can be used to print out opaque data structures properly */
1290 patch_opaque_types ()
1294 /* Look at each symbol in the per-file block of each symtab. */
1295 for (s
= symtab_list
; s
; s
= s
->next
)
1297 register struct block
*b
;
1300 /* Go through the per-file symbols only */
1301 b
= BLOCKVECTOR_BLOCK (BLOCKVECTOR (s
), 1);
1302 for (i
= BLOCK_NSYMS (b
) - 1; i
>= 0; i
--)
1304 register struct symbol
*real_sym
;
1306 /* Find completed typedefs to use to fix opaque ones.
1307 Remove syms from the chain when their types are stored,
1308 but search the whole chain, as there may be several syms
1309 from different files with the same name. */
1310 real_sym
= BLOCK_SYM (b
, i
);
1311 if (SYMBOL_CLASS (real_sym
) == LOC_TYPEDEF
&&
1312 SYMBOL_NAMESPACE (real_sym
) == VAR_NAMESPACE
&&
1313 TYPE_CODE (SYMBOL_TYPE (real_sym
)) == TYPE_CODE_PTR
&&
1314 TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (real_sym
))) != 0)
1316 register char *name
= SYMBOL_NAME (real_sym
);
1317 register int hash
= hashname (name
);
1318 register struct symbol
*sym
, *prev
;
1321 for (sym
= opaque_type_chain
[hash
]; sym
;)
1323 if (name
[0] == SYMBOL_NAME (sym
)[0] &&
1324 !strcmp (name
+ 1, SYMBOL_NAME (sym
) + 1))
1327 SYMBOL_VALUE (prev
) = SYMBOL_VALUE (sym
);
1329 opaque_type_chain
[hash
]
1330 = (struct symbol
*) SYMBOL_VALUE (sym
);
1332 patch_type (SYMBOL_TYPE (sym
), SYMBOL_TYPE (real_sym
));
1335 sym
= (struct symbol
*) SYMBOL_VALUE (prev
);
1337 sym
= opaque_type_chain
[hash
];
1342 sym
= (struct symbol
*) SYMBOL_VALUE (sym
);
1350 static struct symbol
*
1351 process_coff_symbol (cs
, aux
)
1352 register struct coff_symbol
*cs
;
1353 register AUXENT
*aux
;
1355 register struct symbol
*sym
= (struct symbol
*)
1356 xmalloc (sizeof (struct symbol
));
1359 #ifdef NAMES_HAVE_UNDERSCORE
1365 bzero (sym
, sizeof (struct symbol
));
1367 name
= (name
[0] == '_' ? name
+ offset
: name
);
1368 SYMBOL_NAME (sym
) = savestring (name
, strlen (name
));
1370 /* default assumptions */
1371 SYMBOL_VALUE (sym
) = cs
->c_value
;
1372 SYMBOL_NAMESPACE (sym
) = VAR_NAMESPACE
;
1374 if (ISFCN (cs
->c_type
))
1376 SYMBOL_TYPE (sym
) = decode_function_type (cs
, cs
->c_type
, aux
);
1377 SYMBOL_CLASS (sym
) = LOC_BLOCK
;
1378 if (cs
->c_sclass
== C_STAT
)
1379 add_symbol_to_list (sym
, &file_symbols
);
1380 else if (cs
->c_sclass
== C_EXT
)
1381 add_symbol_to_list (sym
, &global_symbols
);
1385 SYMBOL_TYPE (sym
) = decode_type (cs
, cs
->c_type
, aux
);
1386 switch (cs
->c_sclass
)
1392 SYMBOL_CLASS (sym
) = LOC_LOCAL
;
1393 add_symbol_to_list (sym
, &local_symbols
);
1397 SYMBOL_CLASS (sym
) = LOC_STATIC
;
1398 add_symbol_to_list (sym
, &global_symbols
);
1402 SYMBOL_CLASS (sym
) = LOC_STATIC
;
1403 if (within_function
) {
1404 /* Static symbol of local scope */
1405 add_symbol_to_list (sym
, &local_symbols
);
1408 /* Static symbol at top level of file */
1409 add_symbol_to_list (sym
, &file_symbols
);
1415 SYMBOL_CLASS (sym
) = LOC_REGISTER
;
1416 add_symbol_to_list (sym
, &local_symbols
);
1423 SYMBOL_CLASS (sym
) = LOC_ARG
;
1424 add_symbol_to_list (sym
, &local_symbols
);
1425 /* If PCC says a parameter is a short or a char,
1426 it is really an int. */
1427 if (SYMBOL_TYPE (sym
) == builtin_type_char
1428 || SYMBOL_TYPE (sym
) == builtin_type_short
)
1429 SYMBOL_TYPE (sym
) = builtin_type_int
;
1430 else if (SYMBOL_TYPE (sym
) == builtin_type_unsigned_char
1431 || SYMBOL_TYPE (sym
) == builtin_type_unsigned_short
)
1432 SYMBOL_TYPE (sym
) = builtin_type_unsigned_int
;
1436 SYMBOL_CLASS (sym
) = LOC_TYPEDEF
;
1437 SYMBOL_NAMESPACE (sym
) = VAR_NAMESPACE
;
1439 /* If type has no name, give it one */
1440 if (TYPE_NAME (SYMBOL_TYPE (sym
)) == 0
1441 && (TYPE_FLAGS (SYMBOL_TYPE (sym
)) & TYPE_FLAG_PERM
) == 0)
1442 TYPE_NAME (SYMBOL_TYPE (sym
))
1443 = concat (SYMBOL_NAME (sym
), "", "");
1445 /* Keep track of any type which points to empty structured type,
1446 so it can be filled from a definition from another file */
1447 if (TYPE_CODE (SYMBOL_TYPE (sym
)) == TYPE_CODE_PTR
&&
1448 TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym
))) == 0)
1450 register int i
= hashname (SYMBOL_NAME (sym
));
1452 SYMBOL_VALUE (sym
) = (int) opaque_type_chain
[i
];
1453 opaque_type_chain
[i
] = sym
;
1455 add_symbol_to_list (sym
, &file_symbols
);
1461 SYMBOL_CLASS (sym
) = LOC_TYPEDEF
;
1462 SYMBOL_NAMESPACE (sym
) = STRUCT_NAMESPACE
;
1463 if (TYPE_NAME (SYMBOL_TYPE (sym
)) == 0
1464 && (TYPE_FLAGS (SYMBOL_TYPE (sym
)) & TYPE_FLAG_PERM
) == 0)
1465 TYPE_NAME (SYMBOL_TYPE (sym
))
1467 (cs
->c_sclass
== C_ENTAG
1469 : (cs
->c_sclass
== C_STRTAG
1470 ? "struct " : "union ")),
1472 add_symbol_to_list (sym
, &file_symbols
);
1482 /* Decode a coff type specifier;
1483 return the type that is meant. */
1487 decode_type (cs
, c_type
, aux
)
1488 register struct coff_symbol
*cs
;
1489 unsigned int c_type
;
1490 register AUXENT
*aux
;
1492 register struct type
*type
= 0;
1494 unsigned int new_c_type
;
1496 if (c_type
& ~N_BTMASK
)
1498 new_c_type
= DECREF (c_type
);
1501 type
= decode_type (cs
, new_c_type
, aux
);
1502 type
= lookup_pointer_type (type
);
1504 else if (ISFCN (c_type
))
1506 type
= decode_type (cs
, new_c_type
, aux
);
1507 type
= lookup_function_type (type
);
1509 else if (ISARY (c_type
))
1512 register unsigned short *dim
;
1513 struct type
*base_type
;
1515 /* Define an array type. */
1516 /* auxent refers to array, not base type */
1517 if (aux
->x_sym
.x_tagndx
== 0)
1520 /* shift the indices down */
1521 dim
= &aux
->x_sym
.x_fcnary
.x_ary
.x_dimen
[0];
1524 for (i
= 0; *dim
&& i
< DIMNUM
- 1; i
++, dim
++)
1528 type
= (struct type
*)
1529 obstack_alloc (symbol_obstack
, sizeof (struct type
));
1530 bzero (type
, sizeof (struct type
));
1532 base_type
= decode_type (cs
, new_c_type
, aux
);
1534 TYPE_CODE (type
) = TYPE_CODE_ARRAY
;
1535 TYPE_TARGET_TYPE (type
) = base_type
;
1536 TYPE_LENGTH (type
) = n
* TYPE_LENGTH (base_type
);
1541 /* Reference to existing type */
1542 if (cs
->c_nsyms
> 1 && aux
->x_sym
.x_tagndx
!= 0)
1544 type
= coff_alloc_type (aux
->x_sym
.x_tagndx
);
1548 return decode_base_type (cs
, BTYPE (c_type
), aux
);
1551 /* Decode a coff type specifier for function definition;
1552 return the type that the function returns. */
1556 decode_function_type (cs
, c_type
, aux
)
1557 register struct coff_symbol
*cs
;
1558 unsigned int c_type
;
1559 register AUXENT
*aux
;
1561 if (aux
->x_sym
.x_tagndx
== 0)
1562 cs
->c_nsyms
= 1; /* auxent refers to function, not base type */
1564 return decode_type (cs
, DECREF (cs
->c_type
), aux
);
1571 decode_base_type (cs
, c_type
, aux
)
1572 register struct coff_symbol
*cs
;
1573 unsigned int c_type
;
1574 register AUXENT
*aux
;
1581 /* NULL seems to be used as the basic type of void functions */
1582 return builtin_type_void
;
1586 /* shouldn't show up here */
1590 return builtin_type_char
;
1593 return builtin_type_short
;
1596 return builtin_type_int
;
1599 return builtin_type_long
;
1602 return builtin_type_float
;
1605 return builtin_type_double
;
1608 if (cs
->c_nsyms
!= 2)
1610 /* anonymous structure type */
1611 type
= coff_alloc_type (cs
->c_symnum
);
1612 TYPE_CODE (type
) = TYPE_CODE_STRUCT
;
1613 TYPE_NAME (type
) = concat ("struct ", "<opaque>", "");
1614 TYPE_LENGTH (type
) = 0;
1615 TYPE_FIELDS (type
) = 0;
1616 TYPE_NFIELDS (type
) = 0;
1620 type
= read_struct_type (cs
->c_symnum
,
1621 aux
->x_sym
.x_misc
.x_lnsz
.x_size
,
1622 aux
->x_sym
.x_fcnary
.x_fcn
.x_endndx
);
1627 if (cs
->c_nsyms
!= 2)
1629 /* anonymous union type */
1630 type
= coff_alloc_type (cs
->c_symnum
);
1631 TYPE_NAME (type
) = concat ("union ", "<opaque>", "");
1632 TYPE_LENGTH (type
) = 0;
1633 TYPE_FIELDS (type
) = 0;
1634 TYPE_NFIELDS (type
) = 0;
1638 type
= read_struct_type (cs
->c_symnum
,
1639 aux
->x_sym
.x_misc
.x_lnsz
.x_size
,
1640 aux
->x_sym
.x_fcnary
.x_fcn
.x_endndx
);
1642 TYPE_CODE (type
) = TYPE_CODE_UNION
;
1646 return read_enum_type (cs
->c_symnum
,
1647 aux
->x_sym
.x_misc
.x_lnsz
.x_size
,
1648 aux
->x_sym
.x_fcnary
.x_fcn
.x_endndx
);
1651 /* shouldn't show up here */
1655 return builtin_type_unsigned_char
;
1658 return builtin_type_unsigned_short
;
1661 return builtin_type_unsigned_int
;
1664 return builtin_type_unsigned_long
;
1666 printf ("unexpected type %d at symnum %d, name %s\n", c_type
, cs
->c_symnum
,
1668 return builtin_type_void
;
1671 /* This page contains subroutines of read_type. */
1673 /* Read the description of a structure (or union type)
1674 and return an object describing the type. */
1676 static struct type
*
1677 read_struct_type (index
, length
, lastsym
)
1684 struct nextfield
*next
;
1688 register struct type
*type
;
1689 register struct nextfield
*list
= 0;
1690 struct nextfield
*new;
1694 #ifdef NAMES_HAVE_UNDERSCORE
1699 struct coff_symbol member_sym
;
1700 register struct coff_symbol
*ms
= &member_sym
;
1704 type
= coff_alloc_type (index
);
1705 TYPE_CODE (type
) = TYPE_CODE_STRUCT
;
1706 TYPE_LENGTH (type
) = length
;
1708 while (symnum
< lastsym
&& symnum
< nlist_nsyms_global
)
1710 read_one_sym (ms
, &sub_sym
, &sub_aux
);
1712 name
= (name
[0] == '_' ? name
+ offset
: name
);
1714 switch (ms
->c_sclass
)
1719 /* Get space to record the next field's data. */
1720 new = (struct nextfield
*) alloca (sizeof (struct nextfield
));
1724 /* Save the data. */
1725 list
->field
.name
= savestring (name
, strlen (name
));
1726 list
->field
.type
= decode_type (ms
, ms
->c_type
, &sub_aux
);
1727 list
->field
.bitpos
= 8 * ms
->c_value
;
1728 list
->field
.bitsize
= 0;
1734 /* Get space to record the next field's data. */
1735 new = (struct nextfield
*) alloca (sizeof (struct nextfield
));
1739 /* Save the data. */
1740 list
->field
.name
= savestring (name
, strlen (name
));
1741 list
->field
.type
= decode_type (ms
, ms
->c_type
, &sub_aux
);
1742 list
->field
.bitpos
= ms
->c_value
;
1743 list
->field
.bitsize
= sub_aux
.x_sym
.x_misc
.x_lnsz
.x_size
;
1751 /* Now create the vector of fields, and record how big it is. */
1753 TYPE_NFIELDS (type
) = nfields
;
1754 TYPE_FIELDS (type
) = (struct field
*)
1755 obstack_alloc (symbol_obstack
, sizeof (struct field
) * nfields
);
1757 /* Copy the saved-up fields into the field vector. */
1759 for (n
= nfields
; list
; list
= list
->next
)
1760 TYPE_FIELD (type
, --n
) = list
->field
;
1765 /* Read a definition of an enumeration type,
1766 and create and return a suitable type object.
1767 Also defines the symbols that represent the values of the type. */
1769 static struct type
*
1770 read_enum_type (index
, length
, lastsym
)
1775 register struct symbol
*sym
;
1776 register struct type
*type
;
1778 struct pending
**symlist
;
1779 struct coff_symbol member_sym
;
1780 register struct coff_symbol
*ms
= &member_sym
;
1783 struct pending
*osyms
, *syms
;
1786 #ifdef NAMES_HAVE_UNDERSCORE
1792 type
= coff_alloc_type (index
);
1793 if (within_function
)
1794 symlist
= &local_symbols
;
1796 symlist
= &file_symbols
;
1799 while (symnum
< lastsym
&& symnum
< nlist_nsyms_global
)
1801 read_one_sym (ms
, &sub_sym
, &sub_aux
);
1803 name
= (name
[0] == '_' ? name
+ offset
: name
);
1805 switch (ms
->c_sclass
)
1808 sym
= (struct symbol
*) xmalloc (sizeof (struct symbol
));
1809 bzero (sym
, sizeof (struct symbol
));
1811 SYMBOL_NAME (sym
) = savestring (name
, strlen (name
));
1812 SYMBOL_CLASS (sym
) = LOC_CONST
;
1813 SYMBOL_NAMESPACE (sym
) = VAR_NAMESPACE
;
1814 SYMBOL_VALUE (sym
) = ms
->c_value
;
1815 add_symbol_to_list (sym
, symlist
);
1824 /* Now fill in the fields of the type-structure. */
1826 TYPE_LENGTH (type
) = sizeof (int);
1827 TYPE_CODE (type
) = TYPE_CODE_ENUM
;
1828 TYPE_NFIELDS (type
) = nsyms
;
1829 TYPE_FIELDS (type
) = (struct field
*)
1830 obstack_alloc (symbol_obstack
, sizeof (struct field
) * nsyms
);
1832 /* Find the symbols for the values and put them into the type.
1833 The symbols can be found in the symlist that we put them on
1834 to cause them to be defined. osyms contains the old value
1835 of that symlist; everything up to there was defined by us. */
1837 for (syms
= *symlist
, n
= nsyms
; syms
!= osyms
; syms
= syms
->next
)
1839 SYMBOL_TYPE (syms
->symbol
) = type
;
1840 TYPE_FIELD_NAME (type
, --n
) = SYMBOL_NAME (syms
->symbol
);
1841 TYPE_FIELD_VALUE (type
, n
) = SYMBOL_VALUE (syms
->symbol
);
1842 TYPE_FIELD_BITPOS (type
, n
) = 0;
1843 TYPE_FIELD_BITSIZE (type
, n
) = 0;
1853 add_com ("symbol-file", class_files
, symbol_file_command
,
1854 "Load symbol table (in coff format) from executable file FILE.");
1859 #endif /* COFF_FORMAT */