]> Git Repo - binutils.git/blob - gdb/coffread.c
gdb-3.3
[binutils.git] / gdb / coffread.c
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 [email protected]
5    Copyright (C) 1987, 1988, 1989 Free Software Foundation, Inc.
6
7 This file is part of GDB.
8
9 GDB is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 1, or (at your option)
12 any later version.
13
14 GDB is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GDB; see the file COPYING.  If not, write to
21 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
22 \f
23 #include "defs.h"
24 #include "param.h"
25 #ifdef COFF_FORMAT
26 #include "symtab.h"
27
28 #ifdef USG
29 #include <sys/types.h>
30 #include <fcntl.h>
31 #endif
32
33 #include <a.out.h>
34 #include <stdio.h>
35 #include <obstack.h>
36 #include <sys/param.h>
37 #include <sys/file.h>
38
39 static void add_symbol_to_list ();
40 static void read_coff_symtab ();
41 static void patch_opaque_types ();
42 static struct type *decode_function_type ();
43 static struct type *decode_type ();
44 static struct type *decode_base_type ();
45 static struct type *read_enum_type ();
46 static struct type *read_struct_type ();
47 static void finish_block ();
48 static struct blockvector *make_blockvector ();
49 static struct symbol *process_coff_symbol ();
50 static int init_stringtab ();
51 static void free_stringtab ();
52 static char *getfilename ();
53 static char *getsymname ();
54 static int init_lineno ();
55 static void enter_linenos ();
56
57 extern int fclose ();
58 extern void free_all_symtabs ();
59 extern void free_all_psymtabs ();
60
61
62 /* Name of source file whose symbol data we are now processing.
63    This comes from a symbol named ".file".  */
64
65 static char *last_source_file;
66
67 /* Core address of start and end of text of current source file.
68    This comes from a ".text" symbol where x_nlinno > 0.  */
69
70 static CORE_ADDR cur_src_start_addr;
71 static CORE_ADDR cur_src_end_addr;
72
73 /* Core address of the end of the first object file.  */
74 static CORE_ADDR first_object_file_end;
75
76 /* End of the text segment of the executable file,
77    as found in the symbol _etext.  */
78
79 static CORE_ADDR end_of_text_addr;
80
81 /* The addresses of the symbol table stream and number of symbols
82    of the object file we are reading (as copied into core).  */
83
84 static FILE *nlist_stream_global;
85 static int nlist_nsyms_global;
86
87 /* The file, a.out  and text section headers of the symbol file */
88
89 static FILHDR file_hdr;
90 static SCNHDR text_hdr;
91 static AOUTHDR aout_hdr;
92
93 /* The index in the symbol table of the last coff symbol that was processed.  */
94
95 static int symnum;
96
97 /* Vector of types defined so far, indexed by their coff symnum.  */
98
99 static struct typevector *type_vector;
100
101 /* Number of elements allocated for type_vector currently.  */
102
103 static int type_vector_length;
104
105 /* Vector of line number information.  */
106
107 static struct linetable *line_vector;
108
109 /* Index of next entry to go in line_vector_index.  */
110
111 static int line_vector_index;
112
113 /* Last line number recorded in the line vector.  */
114
115 static int prev_line_number;
116
117 /* Number of elements allocated for line_vector currently.  */
118
119 static int line_vector_length;
120
121 /* Chain of typedefs of pointers to empty struct/union types.
122    They are chained thru the SYMBOL_VALUE.  */
123
124 #define HASHSIZE 127
125 static struct symbol *opaque_type_chain[HASHSIZE];
126
127 /* Record the symbols defined for each context in a list.
128    We don't create a struct block for the context until we
129    know how long to make it.  */
130
131 struct pending
132 {
133   struct pending *next;
134   struct symbol *symbol;
135 };
136
137 /* Here are the three lists that symbols are put on.  */
138
139 struct pending *file_symbols;   /* static at top level, and types */
140
141 struct pending *global_symbols; /* global functions and variables */
142
143 struct pending *local_symbols;  /* everything local to lexical context */
144
145 /* List of unclosed lexical contexts
146    (that will become blocks, eventually).  */
147
148 struct context_stack
149 {
150   struct context_stack *next;
151   struct pending *locals;
152   struct pending_block *old_blocks;
153   struct symbol *name;
154   CORE_ADDR start_addr;
155   int depth;
156 };
157
158 struct context_stack *context_stack;
159
160 /* Nonzero if within a function (so symbols should be local,
161    if nothing says specifically).  */
162
163 int within_function;
164
165 /* List of blocks already made (lexical contexts already closed).
166    This is used at the end to make the blockvector.  */
167
168 struct pending_block
169 {
170   struct pending_block *next;
171   struct block *block;
172 };
173
174 struct pending_block *pending_blocks;
175
176 extern CORE_ADDR startup_file_start;    /* From blockframe.c */
177 extern CORE_ADDR startup_file_end;      /* From blockframe.c */
178
179 /* File name symbols were loaded from.  */
180
181 static char *symfile;
182 \f
183 /* Look up a coff type-number index.  Return the address of the slot
184    where the type for that index is stored.
185    The type-number is in INDEX. 
186
187    This can be used for finding the type associated with that index
188    or for associating a new type with the index.  */
189
190 static struct type **
191 coff_lookup_type (index)
192      register int index;
193 {
194   if (index >= type_vector_length)
195     {
196       int old_vector_length = type_vector_length;
197
198       type_vector_length *= 2;
199       if (type_vector_length < index) {
200         type_vector_length = index * 2;
201       }
202       type_vector = (struct typevector *)
203         xrealloc (type_vector, sizeof (struct typevector)
204                                 + type_vector_length * sizeof (struct type *));
205       bzero (&type_vector->type[ old_vector_length ],
206              (type_vector_length - old_vector_length) * sizeof(struct type *));
207     }
208   return &type_vector->type[index];
209 }
210
211 /* Make sure there is a type allocated for type number index
212    and return the type object.
213    This can create an empty (zeroed) type object.  */
214
215 static struct type *
216 coff_alloc_type (index)
217      int index;
218 {
219   register struct type **type_addr = coff_lookup_type (index);
220   register struct type *type = *type_addr;
221
222   /* If we are referring to a type not known at all yet,
223      allocate an empty type for it.
224      We will fill it in later if we find out how.  */
225   if (type == 0)
226     {
227       type = (struct type *) obstack_alloc (symbol_obstack,
228                                             sizeof (struct type));
229       bzero (type, sizeof (struct type));
230       *type_addr = type;
231     }
232   return type;
233 }
234 \f
235 /* maintain the lists of symbols and blocks */
236
237 /* Add a symbol to one of the lists of symbols.  */
238 static void
239 add_symbol_to_list (symbol, listhead)
240      struct symbol *symbol;
241      struct pending **listhead;
242 {
243   register struct pending *link
244     = (struct pending *) xmalloc (sizeof (struct pending));
245
246   link->next = *listhead;
247   link->symbol = symbol;
248   *listhead = link;
249 }
250
251 /* Take one of the lists of symbols and make a block from it.
252    Put the block on the list of pending blocks.  */
253
254 static void
255 finish_block (symbol, listhead, old_blocks, start, end)
256      struct symbol *symbol;
257      struct pending **listhead;
258      struct pending_block *old_blocks;
259      CORE_ADDR start, end;
260 {
261   register struct pending *next, *next1;
262   register struct block *block;
263   register struct pending_block *pblock;
264   struct pending_block *opblock;
265   register int i;
266
267   /* Count the length of the list of symbols.  */
268
269   for (next = *listhead, i = 0; next; next = next->next, i++);
270
271   block = (struct block *)
272             obstack_alloc (symbol_obstack, sizeof (struct block) + (i - 1) * sizeof (struct symbol *));
273
274   /* Copy the symbols into the block.  */
275
276   BLOCK_NSYMS (block) = i;
277   for (next = *listhead; next; next = next->next)
278     BLOCK_SYM (block, --i) = next->symbol;
279
280   BLOCK_START (block) = start;
281   BLOCK_END (block) = end;
282   BLOCK_SUPERBLOCK (block) = 0; /* Filled in when containing block is made */
283
284   /* Put the block in as the value of the symbol that names it.  */
285
286   if (symbol)
287     {
288       SYMBOL_BLOCK_VALUE (symbol) = block;
289       BLOCK_FUNCTION (block) = symbol;
290     }
291   else
292     BLOCK_FUNCTION (block) = 0;
293
294   /* Now free the links of the list, and empty the list.  */
295
296   for (next = *listhead; next; next = next1)
297     {
298       next1 = next->next;
299       free (next);
300     }
301   *listhead = 0;
302
303   /* Install this block as the superblock
304      of all blocks made since the start of this scope
305      that don't have superblocks yet.  */
306
307   opblock = 0;
308   for (pblock = pending_blocks; pblock != old_blocks; pblock = pblock->next)
309     {
310       if (BLOCK_SUPERBLOCK (pblock->block) == 0)
311         BLOCK_SUPERBLOCK (pblock->block) = block;
312       opblock = pblock;
313     }
314
315   /* Record this block on the list of all blocks in the file.
316      Put it after opblock, or at the beginning if opblock is 0.
317      This puts the block in the list after all its subblocks.  */
318
319   pblock = (struct pending_block *) xmalloc (sizeof (struct pending_block));
320   pblock->block = block;
321   if (opblock)
322     {
323       pblock->next = opblock->next;
324       opblock->next = pblock;
325     }
326   else
327     {
328       pblock->next = pending_blocks;
329       pending_blocks = pblock;
330     }
331 }
332
333 static struct blockvector *
334 make_blockvector ()
335 {
336   register struct pending_block *next, *next1;
337   register struct blockvector *blockvector;
338   register int i;
339
340   /* Count the length of the list of blocks.  */
341
342   for (next = pending_blocks, i = 0; next; next = next->next, i++);
343
344   blockvector = (struct blockvector *)
345                   obstack_alloc (symbol_obstack, sizeof (struct blockvector) + (i - 1) * sizeof (struct block *));
346
347   /* Copy the blocks into the blockvector.
348      This is done in reverse order, which happens to put
349      the blocks into the proper order (ascending starting address).
350      finish_block has hair to insert each block into the list
351      after its subblocks in order to make sure this is true.  */
352
353   BLOCKVECTOR_NBLOCKS (blockvector) = i;
354   for (next = pending_blocks; next; next = next->next)
355     BLOCKVECTOR_BLOCK (blockvector, --i) = next->block;
356
357   /* Now free the links of the list, and empty the list.  */
358
359   for (next = pending_blocks; next; next = next1)
360     {
361       next1 = next->next;
362       free (next);
363     }
364   pending_blocks = 0;
365
366   return blockvector;
367 }
368
369 /* Manage the vector of line numbers.  */
370
371 static
372 record_line (line, pc)
373      int line;
374      CORE_ADDR pc;
375 {
376   struct linetable_entry *e;
377   /* Make sure line vector is big enough.  */
378
379   if (line_vector_index + 2 >= line_vector_length)
380     {
381       line_vector_length *= 2;
382       line_vector = (struct linetable *)
383         xrealloc (line_vector, sizeof (struct linetable)
384                   + (line_vector_length
385                      * sizeof (struct linetable_entry)));
386     }
387
388   e = line_vector->item + line_vector_index++;
389   e->line = line; e->pc = pc;
390 }
391 \f
392 /* Start a new symtab for a new source file.
393    This is called when a COFF ".file" symbol is seen;
394    it indicates the start of data for one original source file.  */
395
396 static void
397 start_symtab ()
398 {
399   file_symbols = 0;
400   global_symbols = 0;
401   context_stack = 0;
402   within_function = 0;
403   last_source_file = 0;
404
405   /* Initialize the source file information for this file.  */
406
407   line_vector_index = 0;
408   line_vector_length = 1000;
409   prev_line_number = -2;        /* Force first line number to be explicit */
410   line_vector = (struct linetable *)
411     xmalloc (sizeof (struct linetable)
412              + line_vector_length * sizeof (struct linetable_entry));
413 }
414
415 /* Save the vital information for use when closing off the current file.
416    NAME is the file name the symbols came from, START_ADDR is the first
417    text address for the file, and SIZE is the number of bytes of text.  */
418
419 static void
420 complete_symtab (name, start_addr, size)
421     char *name;
422     CORE_ADDR start_addr;
423     unsigned int size;
424 {
425   last_source_file = savestring (name, strlen (name));
426   cur_src_start_addr = start_addr;
427   cur_src_end_addr = start_addr + size;
428
429   if (aout_hdr.entry < cur_src_end_addr
430       && aout_hdr.entry >= cur_src_start_addr)
431     {
432       startup_file_start = cur_src_start_addr;
433       startup_file_end = cur_src_end_addr;
434     }
435 }
436
437 /* Finish the symbol definitions for one main source file,
438    close off all the lexical contexts for that file
439    (creating struct block's for them), then make the
440    struct symtab for that file and put it in the list of all such. */
441
442 static void
443 end_symtab ()
444 {
445   register struct symtab *symtab;
446   register struct context_stack *cstk;
447   register struct blockvector *blockvector;
448   register struct linetable *lv;
449
450   /* Finish the lexical context of the last function in the file.  */
451
452   if (context_stack)
453     {
454       cstk = context_stack;
455       context_stack = 0;
456       /* Make a block for the local symbols within.  */
457       finish_block (cstk->name, &local_symbols, cstk->old_blocks,
458                     cstk->start_addr, cur_src_end_addr);
459       free (cstk);
460     }
461
462   /* Ignore a file that has no functions with real debugging info.  */
463   if (pending_blocks == 0 && file_symbols == 0 && global_symbols == 0)
464     {
465       free (line_vector);
466       line_vector = 0;
467       line_vector_length = -1;
468       last_source_file = 0;
469       return;
470     }
471
472   /* Create the two top-level blocks for this file.  */
473   finish_block (0, &file_symbols, 0, cur_src_start_addr, cur_src_end_addr);
474   finish_block (0, &global_symbols, 0, cur_src_start_addr, cur_src_end_addr);
475
476   /* Create the blockvector that points to all the file's blocks.  */
477   blockvector = make_blockvector ();
478
479   /* Now create the symtab object for this source file.  */
480   symtab = (struct symtab *) xmalloc (sizeof (struct symtab));
481   symtab->free_ptr = 0;
482
483   /* Fill in its components.  */
484   symtab->blockvector = blockvector;
485   symtab->free_code = free_linetable;
486   symtab->filename = last_source_file;
487   lv = line_vector;
488   lv->nitems = line_vector_index;
489   symtab->linetable = (struct linetable *)
490     xrealloc (lv, (sizeof (struct linetable)
491                    + lv->nitems * sizeof (struct linetable_entry)));
492   symtab->nlines = 0;
493   symtab->line_charpos = 0;
494
495   /* Link the new symtab into the list of such.  */
496   symtab->next = symtab_list;
497   symtab_list = symtab;
498
499   /* Reinitialize for beginning of new file. */
500   line_vector = 0;
501   line_vector_length = -1;
502   last_source_file = 0;
503 }
504 \f
505 /* Accumulate the misc functions in bunches of 127.
506    At the end, copy them all into one newly allocated structure.  */
507
508 #define MISC_BUNCH_SIZE 127
509
510 struct misc_bunch
511 {
512   struct misc_bunch *next;
513   struct misc_function contents[MISC_BUNCH_SIZE];
514 };
515
516 /* Bunch currently being filled up.
517    The next field points to chain of filled bunches.  */
518
519 static struct misc_bunch *misc_bunch;
520
521 /* Number of slots filled in current bunch.  */
522
523 static int misc_bunch_index;
524
525 /* Total number of misc functions recorded so far.  */
526
527 static int misc_count;
528
529 static void
530 init_misc_functions ()
531 {
532   misc_count = 0;
533   misc_bunch = 0;
534   misc_bunch_index = MISC_BUNCH_SIZE;
535 }
536
537 static void
538 record_misc_function (name, address)
539      char *name;
540      CORE_ADDR address;
541 {
542   register struct misc_bunch *new;
543
544   if (misc_bunch_index == MISC_BUNCH_SIZE)
545     {
546       new = (struct misc_bunch *) xmalloc (sizeof (struct misc_bunch));
547       misc_bunch_index = 0;
548       new->next = misc_bunch;
549       misc_bunch = new;
550     }
551   misc_bunch->contents[misc_bunch_index].name = savestring (name, strlen (name));
552   misc_bunch->contents[misc_bunch_index].address = address;
553   misc_bunch->contents[misc_bunch_index].type = mf_unknown;
554   misc_bunch_index++;
555   misc_count++;
556 }
557
558 /* if we see a function symbol, we do record_misc_function.
559  * however, if it turns out the next symbol is '.bf', then
560  * we call here to undo the misc definition
561  */
562 static void
563 unrecord_misc_function ()
564 {
565   if (misc_bunch_index == 0)
566     error ("Internal error processing symbol table, at symbol %d.",
567            symnum);
568   misc_bunch_index--;
569   misc_count--;
570 }
571
572
573 static int
574 compare_misc_functions (fn1, fn2)
575      struct misc_function *fn1, *fn2;
576 {
577   /* Return a signed result based on unsigned comparisons
578      so that we sort into unsigned numeric order.  */
579   if (fn1->address < fn2->address)
580     return -1;
581   if (fn1->address > fn2->address)
582     return 1;
583   return 0;
584 }
585
586 static void
587 discard_misc_bunches ()
588 {
589   register struct misc_bunch *next;
590
591   while (misc_bunch)
592     {
593       next = misc_bunch->next;
594       free (misc_bunch);
595       misc_bunch = next;
596     }
597 }
598
599 static void
600 condense_misc_bunches ()
601 {
602   register int i, j;
603   register struct misc_bunch *bunch;
604 #ifdef NAMES_HAVE_UNDERSCORE
605   int offset = 1;
606 #else
607   int offset = 0;
608 #endif
609
610   misc_function_vector
611     = (struct misc_function *)
612       xmalloc (misc_count * sizeof (struct misc_function));
613
614   j = 0;
615   bunch = misc_bunch;
616   while (bunch)
617     {
618       for (i = 0; i < misc_bunch_index; i++)
619         {
620           register char *tmp;
621
622           misc_function_vector[j] = bunch->contents[i];
623           tmp = misc_function_vector[j].name;
624           misc_function_vector[j].name = (tmp[0] == '_' ? tmp + offset : tmp);
625           j++;
626         }
627       bunch = bunch->next;
628       misc_bunch_index = MISC_BUNCH_SIZE;
629     }
630
631   misc_function_count = j;
632
633   /* Sort the misc functions by address.  */
634
635   qsort (misc_function_vector, j, sizeof (struct misc_function),
636          compare_misc_functions);
637 }
638
639 /* Call sort_syms to sort alphabetically
640    the symbols of each block of each symtab.  */
641
642 static int
643 compare_symbols (s1, s2)
644      struct symbol **s1, **s2;
645 {
646   /* Names that are less should come first.  */
647   register int namediff = strcmp (SYMBOL_NAME (*s1), SYMBOL_NAME (*s2));
648   if (namediff != 0) return namediff;
649   /* For symbols of the same name, registers should come first.  */
650   return ((SYMBOL_CLASS (*s2) == LOC_REGISTER)
651           - (SYMBOL_CLASS (*s1) == LOC_REGISTER));
652 }
653
654 static void
655 sort_syms ()
656 {
657   register struct symtab *s;
658   register int i, nbl;
659   register struct blockvector *bv;
660   register struct block *b;
661
662   for (s = symtab_list; s; s = s->next)
663     {
664       bv = BLOCKVECTOR (s);
665       nbl = BLOCKVECTOR_NBLOCKS (bv);
666       for (i = 0; i < nbl; i++)
667         {
668           b = BLOCKVECTOR_BLOCK (bv, i);
669           if (BLOCK_SHOULD_SORT (b))
670                   qsort (&BLOCK_SYM (b, 0), BLOCK_NSYMS (b),
671                          sizeof (struct symbol *), compare_symbols);
672         }
673     }
674 }
675 \f
676 /* This is the symbol-file command.  Read the file, analyze its symbols,
677    and add a struct symtab to symtab_list.  */
678
679 void
680 symbol_file_command (name)
681      char *name;
682 {
683   int desc;
684   int num_symbols;
685   int num_sections;
686   int symtab_offset;
687   extern void close ();
688   register int val;
689   struct cleanup *old_chain;
690
691   dont_repeat ();
692
693   if (name == 0)
694     {
695       if (symtab_list && !query ("Discard symbol table? ", 0))
696         error ("Not confirmed.");
697       if (symfile)
698         free (symfile);
699       symfile = 0;
700       free_all_symtabs ();
701       return;
702     }
703
704   name = tilde_expand (name);
705   make_cleanup (free, name);
706
707   if (symtab_list && !query ("Load new symbol table from \"%s\"? ", name))
708     error ("Not confirmed.");
709
710   if (symfile)
711     free (symfile);
712   symfile = 0;
713
714   {
715     char *absolute_name;
716
717     desc = openp (getenv ("PATH"), 1, name, O_RDONLY, 0, &absolute_name);
718     if (desc < 0)
719       perror_with_name (name);
720     else
721       name = absolute_name;
722   }
723
724   old_chain = make_cleanup (close, desc);
725   make_cleanup (free_current_contents, &name);
726
727   if ((num_symbols = read_file_hdr (desc, &file_hdr)) < 0)
728     error ("File \"%s\" not in executable format.", name);
729
730   /* If an a.out header is present, read it in.  If not (e.g. a .o file)
731      deal with its absence.  */
732   if (file_hdr.f_opthdr == 0
733       || read_aout_hdr (desc, &aout_hdr, file_hdr.f_opthdr) < 0)
734     {
735       /* We will not actually be able to run code, since backtraces would
736          fly off the bottom of the stack (there is no way to reliably
737          detect bottom of stack), but that's fine since the kernel won't
738          run something without an a.out header anyway.  Passive examination
739          of .o files is one place this might make sense.  */
740       /* ~0 will not be in any file.  */
741       aout_hdr.entry = ~0;
742       /* set the startup file to be an empty range.  */
743       startup_file_start = 0;
744       startup_file_end = 0;
745     }
746
747   if (num_symbols == 0)
748     {
749       free_all_symtabs ();
750       printf ("%s does not have a symbol-table.\n", name);
751       fflush (stdout);
752       return;
753     }
754
755   printf ("Reading symbol data from %s...", name);
756   fflush (stdout);
757
758   /* Throw away the old symbol table.  */
759
760   free_all_symtabs ();
761   free_all_psymtabs ();         /* Make sure that partial_symtab_list */
762                                 /* is 0 also. */
763
764   num_sections = file_hdr.f_nscns;
765   symtab_offset = file_hdr.f_symptr;
766
767   if (read_section_hdr (desc, _TEXT, &text_hdr, num_sections) < 0)
768     error ("\"%s\": can't read text section header", name);
769
770   /* Read the line number table, all at once.  */
771
772   val = init_lineno (desc, text_hdr.s_lnnoptr, text_hdr.s_nlnno);
773   if (val < 0)
774     error ("\"%s\": error reading line numbers\n", name);
775
776   /* Now read the string table, all at once.  */
777
778   val = init_stringtab (desc, symtab_offset + num_symbols * SYMESZ);
779   if (val < 0)
780     {
781       free_all_symtabs ();
782       printf ("\"%s\": can't get string table", name);
783       fflush (stdout);
784       return;
785     }
786   make_cleanup (free_stringtab, 0);
787
788   /* Position to read the symbol table.  Do not read it all at once. */
789   val = lseek (desc, (long)symtab_offset, 0);
790   if (val < 0)
791     perror_with_name (name);
792
793   init_misc_functions ();
794   make_cleanup (discard_misc_bunches, 0);
795
796   /* Now that the executable file is positioned at symbol table,
797      process it and define symbols accordingly.  */
798
799   read_coff_symtab (desc, num_symbols);
800
801   patch_opaque_types ();
802
803   /* Sort symbols alphabetically within each block.  */
804
805   sort_syms ();
806
807   /* Go over the misc functions and install them in vector.  */
808
809   condense_misc_bunches ();
810
811   /* Don't allow char * to have a typename (else would get caddr_t.)  */
812
813   TYPE_NAME (lookup_pointer_type (builtin_type_char)) = 0;
814
815   /* Make a default for file to list.  */
816
817   select_source_symtab (symtab_list);
818
819   symfile = savestring (name, strlen (name));
820
821   do_cleanups (old_chain);
822
823   printf ("done.\n");
824   fflush (stdout);
825 }
826
827 /* Return name of file symbols were loaded from, or 0 if none..  */
828
829 char *
830 get_sym_file ()
831 {
832   return symfile;
833 }
834 \f
835 /* Simplified internal version of coff symbol table information */
836
837 struct coff_symbol {
838   char *c_name;
839   int c_symnum;         /* symbol number of this entry */
840   int c_nsyms;          /* 1 if syment only, 2 if syment + auxent */
841   long c_value;
842   int c_sclass;
843   int c_secnum;
844   unsigned int c_type;
845 };
846
847 /* Given pointers to a symbol table in coff style exec file,
848    analyze them and create struct symtab's describing the symbols.
849    NSYMS is the number of symbols in the symbol table.
850    We read them one at a time using read_one_sym ().  */
851
852 static void
853 read_coff_symtab (desc, nsyms)
854      int desc;
855      int nsyms;
856 {
857   int newfd;                    /* Avoid multiple closes on same desc */
858   FILE *stream; 
859   register struct context_stack *new;
860   struct coff_symbol coff_symbol;
861   register struct coff_symbol *cs = &coff_symbol;
862   static SYMENT main_sym;
863   static AUXENT main_aux;
864   struct coff_symbol fcn_cs_saved;
865   static SYMENT fcn_sym_saved;
866   static AUXENT fcn_aux_saved;
867
868   int num_object_files = 0;
869   int next_file_symnum = -1;
870   char *filestring;
871   int depth;
872   int fcn_first_line;
873   int fcn_last_line;
874   int fcn_start_addr;
875   long fcn_line_ptr;
876   struct cleanup *old_chain;
877
878
879   newfd = dup (desc);
880   if (newfd == -1)
881     fatal ("Too many open files");
882   stream = fdopen (newfd, "r");
883
884   old_chain = make_cleanup (free_all_symtabs, 0);
885   make_cleanup (fclose, stream);
886   nlist_stream_global = stream;
887   nlist_nsyms_global = nsyms;
888   last_source_file = 0;
889   bzero (opaque_type_chain, sizeof opaque_type_chain);
890
891   type_vector_length = 160;
892   type_vector = (struct typevector *)
893                 xmalloc (sizeof (struct typevector)
894                                 + type_vector_length * sizeof (struct type *));
895   bzero (type_vector->type, type_vector_length * sizeof (struct type *));
896
897   start_symtab ();
898
899   symnum = 0;
900   while (symnum < nsyms)
901     {
902       QUIT;                     /* Make this command interruptable.  */
903       read_one_sym (cs, &main_sym, &main_aux);
904
905       if (cs->c_symnum == next_file_symnum && cs->c_sclass != C_FILE)
906         {
907           CORE_ADDR last_file_end = cur_src_end_addr;
908
909           if (last_source_file)
910             end_symtab ();
911
912           start_symtab ();
913           complete_symtab ("_globals_", 0, first_object_file_end);
914           /* done with all files, everything from here on out is globals */
915         }
916
917       /* Special case for file with type declarations only, no text.  */
918       if (!last_source_file && cs->c_type != T_NULL && cs->c_secnum == N_DEBUG)
919         complete_symtab (filestring, 0, 0);
920
921       /* Typedefs should not be treated as symbol definitions.  */
922       if (ISFCN (cs->c_type) && cs->c_sclass != C_TPDEF)
923         {
924           /* record as misc function.  if we get '.bf' next,
925            * then we undo this step
926            */
927           record_misc_function (cs->c_name, cs->c_value);
928
929           fcn_line_ptr = main_aux.x_sym.x_fcnary.x_fcn.x_lnnoptr;
930           fcn_start_addr = cs->c_value;
931           fcn_cs_saved = *cs;
932           fcn_sym_saved = main_sym;
933           fcn_aux_saved = main_aux;
934           continue;
935         }
936
937       switch (cs->c_sclass)
938         {
939           case C_EFCN:
940           case C_EXTDEF:
941           case C_ULABEL:
942           case C_USTATIC:
943           case C_LINE:
944           case C_ALIAS:
945           case C_HIDDEN:
946             printf ("Bad n_sclass = %d\n", cs->c_sclass);
947             break;
948
949           case C_FILE:
950             /*
951              * c_value field contains symnum of next .file entry in table
952              * or symnum of first global after last .file.
953              */
954             next_file_symnum = cs->c_value;
955             filestring = getfilename (&main_aux);
956             /*
957              * Complete symbol table for last object file
958              * containing debugging information.
959              */
960             if (last_source_file)
961               {
962                 end_symtab ();
963                 start_symtab ();
964               }
965             num_object_files++;
966             break;
967
968           case C_STAT:
969             if (cs->c_name[0] == '.') {
970                     if (strcmp (cs->c_name, _TEXT) == 0) {
971                             if (num_object_files == 1) {
972                                     /* last address of startup file */
973                                     first_object_file_end = cs->c_value +
974                                             main_aux.x_scn.x_scnlen;
975                             }
976                             /* for some reason the old code didn't do
977                              * this if this section entry had
978                              * main_aux.x_scn.x_nlinno equal to 0
979                              */
980                             complete_symtab (filestring, cs->c_value,
981                                              main_aux.x_scn.x_scnlen);
982                     }
983                     /* flush rest of '.' symbols */
984                     break;
985             }
986             /* fall in for static symbols that don't start with '.' */
987           case C_EXT:
988             if (cs->c_sclass == C_EXT &&
989                 cs->c_secnum == N_ABS &&
990                 strcmp (cs->c_name, _ETEXT) == 0)
991                     end_of_text_addr = cs->c_value;
992             if (cs->c_type == T_NULL) {
993                     if (cs->c_secnum <= 1) {    /* text or abs */
994                             record_misc_function (cs->c_name, cs->c_value);
995                             break;
996                     } else {
997                             cs->c_type = T_INT;
998                     }
999             }
1000             (void) process_coff_symbol (cs, &main_aux);
1001             break;
1002
1003           case C_FCN:
1004             if (strcmp (cs->c_name, ".bf") == 0)
1005               {
1006 #if 0
1007                 /* Don't do this; we want all functions to be on the
1008                    mfl now.  */
1009                 unrecord_misc_function ();
1010 #endif
1011
1012                 within_function = 1;
1013
1014                 /* value contains address of first non-init type code */
1015                 /* main_aux.x_sym.x_misc.x_lnsz.x_lnno
1016                             contains line number of '{' } */
1017                 fcn_first_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
1018
1019                 new = (struct context_stack *)
1020                   xmalloc (sizeof (struct context_stack));
1021                 new->depth = depth = 0;
1022                 new->next = 0;
1023                 context_stack = new;
1024                 new->locals = 0;
1025                 new->old_blocks = pending_blocks;
1026                 new->start_addr = fcn_start_addr;
1027                 fcn_cs_saved.c_name = getsymname (&fcn_sym_saved);
1028                 new->name = process_coff_symbol (&fcn_cs_saved,
1029                                                  &fcn_aux_saved);
1030               }
1031             else if (strcmp (cs->c_name, ".ef") == 0)
1032               {
1033                       /* the value of .ef is the address of epilogue code;
1034                        * not useful for gdb
1035                        */
1036                 /* { main_aux.x_sym.x_misc.x_lnsz.x_lnno
1037                             contains number of lines to '}' */
1038                 fcn_last_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
1039                 enter_linenos (fcn_line_ptr, fcn_first_line, fcn_last_line);
1040                 new = context_stack;
1041
1042                 if (new == 0)
1043                   error ("Invalid symbol data; .bf/.ef/.bb/.eb symbol mismatch, at symbol %d.",
1044                          symnum);
1045                 
1046                 finish_block (new->name, &local_symbols, new->old_blocks,
1047                               new->start_addr,
1048                               fcn_cs_saved.c_value +
1049                                   fcn_aux_saved.x_sym.x_misc.x_fsize);
1050                 context_stack = 0;
1051                 within_function = 0;
1052                 free (new);
1053               }
1054             break;
1055
1056           case C_BLOCK:
1057             if (strcmp (cs->c_name, ".bb") == 0)
1058               {
1059                 new = (struct context_stack *)
1060                             xmalloc (sizeof (struct context_stack));
1061                 depth++;
1062                 new->depth = depth;
1063                 new->next = context_stack;
1064                 context_stack = new;
1065                 new->locals = local_symbols;
1066                 new->old_blocks = pending_blocks;
1067                 new->start_addr = cs->c_value;
1068                 new->name = 0;
1069                 local_symbols = 0;
1070               }
1071             else if (strcmp (cs->c_name, ".eb") == 0)
1072               {
1073                 new = context_stack;
1074                 if (new == 0 || depth != new->depth)
1075                   error ("Invalid symbol data: .bb/.eb symbol mismatch at symbol %d.",
1076                          symnum);
1077                 if (local_symbols && context_stack->next)
1078                   {
1079                     /* Make a block for the local symbols within.  */
1080                     finish_block (0, &local_symbols, new->old_blocks,
1081                                   new->start_addr, cs->c_value);
1082                   }
1083                 depth--;
1084                 local_symbols = new->locals;
1085                 context_stack = new->next;
1086                 free (new);
1087               }
1088             break;
1089
1090           default:
1091             (void) process_coff_symbol (cs, &main_aux);
1092             break;
1093         }
1094     }
1095
1096   if (last_source_file)
1097     end_symtab ();
1098   fclose (stream);
1099   discard_cleanups (old_chain);
1100 }
1101 \f
1102 /* Routines for reading headers and symbols from executable.  */
1103
1104 /* Read COFF file header, check magic number,
1105    and return number of symbols. */
1106 read_file_hdr (chan, file_hdr)
1107     int chan;
1108     FILHDR *file_hdr;
1109 {
1110   lseek (chan, 0L, 0);
1111   if (myread (chan, (char *)file_hdr, FILHSZ) < 0)
1112     return -1;
1113
1114   switch (file_hdr->f_magic)
1115     {
1116 #ifdef MC68MAGIC
1117     case MC68MAGIC:
1118 #endif
1119 #ifdef NS32GMAGIC
1120       case NS32GMAGIC:
1121       case NS32SMAGIC:
1122 #endif
1123 #ifdef I386MAGIC
1124     case I386MAGIC:
1125 #endif
1126 #ifdef CLIPPERMAGIC
1127     case CLIPPERMAGIC:
1128 #endif      
1129         return file_hdr->f_nsyms;
1130
1131       default:
1132 #ifdef BADMAG
1133         if (BADMAG(file_hdr))
1134           return -1;
1135         else
1136           return file_hdr->f_nsyms;
1137 #else
1138         return -1;
1139 #endif
1140     }
1141 }
1142
1143 read_aout_hdr (chan, aout_hdr, size)
1144     int chan;
1145     AOUTHDR *aout_hdr;
1146     int size;
1147 {
1148   lseek (chan, (long)FILHSZ, 0);
1149   if (size != sizeof (AOUTHDR))
1150     return -1;
1151   if (myread (chan, (char *)aout_hdr, size) != size)
1152     return -1;
1153   return 0;
1154 }
1155
1156 read_section_hdr (chan, section_name, section_hdr, nsects)
1157     register int chan;
1158     register char *section_name;
1159     SCNHDR *section_hdr;
1160     register int nsects;
1161 {
1162   register int i;
1163
1164   if (lseek (chan, FILHSZ + sizeof (AOUTHDR), 0) < 0)
1165     return -1;
1166
1167   for (i = 0; i < nsects; i++)
1168     {
1169       if (myread (chan, (char *)section_hdr, SCNHSZ) < 0)
1170         return -1;
1171       if (strncmp (section_hdr->s_name, section_name, 8) == 0)
1172         return 0;
1173     }
1174     return -1;
1175 }
1176
1177 read_one_sym (cs, sym, aux)
1178     register struct coff_symbol *cs;
1179     register SYMENT *sym;
1180     register AUXENT *aux;
1181 {
1182   cs->c_symnum = symnum;
1183   fread ((char *)sym, SYMESZ, 1, nlist_stream_global);
1184   cs->c_nsyms = (sym->n_numaux & 0xff) + 1;
1185   if (cs->c_nsyms == 2)
1186     {
1187       /* doc for coff says there is either no aux entry or just one */
1188       fread ((char *)aux, AUXESZ, 1, nlist_stream_global);
1189     }
1190   else if (cs->c_nsyms > 2)
1191     error ("more than one aux symbol table entry at symnum=%d\n", symnum);
1192
1193   cs->c_name = getsymname (sym);
1194   cs->c_value = sym->n_value;
1195   cs->c_sclass = (sym->n_sclass & 0xff);
1196   cs->c_secnum = sym->n_scnum;
1197   cs->c_type = (unsigned) sym->n_type;
1198
1199   symnum += cs->c_nsyms;
1200 }
1201 \f
1202 /* Support for string table handling */
1203
1204 static char *stringtab = NULL;
1205
1206 static int
1207 init_stringtab (chan, offset)
1208     int chan;
1209     long offset;
1210 {
1211   long buffer;
1212   int val;
1213
1214   if (stringtab)
1215     {
1216       free (stringtab);
1217       stringtab = NULL;
1218     }
1219
1220   if (lseek (chan, offset, 0) < 0)
1221     return -1;
1222
1223   val = myread (chan, (char *)&buffer, sizeof buffer);
1224
1225   /* If no string table is needed, then the file may end immediately
1226      after the symbols.  Just return with `stringtab' set to null. */
1227   if (val != sizeof buffer || buffer == 0)
1228     return 0;
1229
1230   stringtab = (char *) xmalloc (buffer);
1231   if (stringtab == NULL)
1232     return -1;
1233
1234   bcopy (&buffer, stringtab, sizeof buffer);
1235
1236   val = myread (chan, stringtab + sizeof buffer, buffer - sizeof buffer);
1237   if (val != buffer - sizeof buffer || stringtab[buffer - 1] != '\0')
1238     return -1;
1239
1240   return 0;
1241 }
1242
1243 static void
1244 free_stringtab ()
1245 {
1246   if (stringtab)
1247     free (stringtab);
1248   stringtab = NULL;
1249 }
1250
1251 static char *
1252 getsymname (symbol_entry)
1253     SYMENT *symbol_entry;
1254 {
1255   static char buffer[SYMNMLEN+1];
1256   char *result;
1257
1258   if (symbol_entry->n_zeroes == 0)
1259     {
1260       result = stringtab + symbol_entry->n_offset;
1261     }
1262   else
1263     {
1264       strncpy (buffer, symbol_entry->n_name, SYMNMLEN);
1265       buffer[SYMNMLEN] = '\0';
1266       result = buffer;
1267     }
1268   return result;
1269 }
1270
1271 static char *
1272 getfilename (aux_entry)
1273     AUXENT *aux_entry;
1274 {
1275   static char buffer[BUFSIZ];
1276   register char *temp;
1277   char *result;
1278   extern char *rindex ();
1279
1280 #ifndef COFF_NO_LONG_FILE_NAMES
1281   if (aux_entry->x_file.x_foff != 0)
1282     strcpy (buffer, stringtab + aux_entry->x_file.x_foff);
1283   else
1284 #endif
1285     {
1286       strncpy (buffer, aux_entry->x_file.x_fname, FILNMLEN);
1287       buffer[FILNMLEN] = '\0';
1288     }
1289   result = buffer;
1290   if ((temp = rindex (result, '/')) != NULL)
1291     result = temp + 1;
1292   return (result);
1293 }
1294
1295 /* Support for line number handling */
1296 static char *linetab = NULL;
1297 static long linetab_offset;
1298 static int linetab_count;
1299
1300 static int
1301 init_lineno (chan, offset, count)
1302     int chan;
1303     long offset;
1304     int count;
1305 {
1306   int val;
1307
1308   if (lseek (chan, offset, 0) < 0)
1309     return -1;
1310   
1311   if (linetab)
1312     free (linetab);
1313   linetab = (char *) xmalloc (count * LINESZ);
1314
1315   val = myread (chan, linetab, count * LINESZ);
1316   if (val != count * LINESZ)
1317     return -1;
1318
1319   linetab_offset = offset;
1320   linetab_count = count;
1321   return 0;
1322 }
1323
1324 static void
1325 enter_linenos (file_offset, first_line, last_line)
1326     long file_offset;
1327     register int first_line;
1328     register int last_line;
1329 {
1330   register char *rawptr = &linetab[file_offset - linetab_offset];
1331   struct lineno lptr;
1332
1333   /* skip first line entry for each function */
1334   rawptr += LINESZ;
1335   /* line numbers start at one for the first line of the function */
1336   first_line--;
1337
1338   /* Bcopy since occaisionally rawptr isn't pointing at long
1339      boundaries.  */
1340   for (bcopy (rawptr, &lptr, LINESZ);
1341        lptr.l_lnno && lptr.l_lnno <= last_line;
1342        rawptr += LINESZ, bcopy (rawptr, &lptr, LINESZ))
1343     {
1344       record_line (first_line + lptr.l_lnno, lptr.l_addr.l_paddr);
1345     }
1346 }
1347 \f
1348 static int
1349 hashname (name)
1350      char *name;
1351 {
1352   register char *p = name;
1353   register int total = p[0];
1354   register int c;
1355
1356   c = p[1];
1357   total += c << 2;
1358   if (c)
1359     {
1360       c = p[2];
1361       total += c << 4;
1362       if (c)
1363         total += p[3] << 6;
1364     }
1365   
1366   return total % HASHSIZE;
1367 }
1368
1369 static void
1370 patch_type (type, real_type)
1371     struct type *type;
1372     struct type *real_type;
1373 {
1374   register struct type *target = TYPE_TARGET_TYPE (type);
1375   register struct type *real_target = TYPE_TARGET_TYPE (real_type);
1376   int field_size = TYPE_NFIELDS (real_target) * sizeof (struct field);
1377
1378   TYPE_LENGTH (target) = TYPE_LENGTH (real_target);
1379   TYPE_NFIELDS (target) = TYPE_NFIELDS (real_target);
1380   TYPE_FIELDS (target) = (struct field *)
1381                                 obstack_alloc (symbol_obstack, field_size);
1382
1383   bcopy (TYPE_FIELDS (real_target), TYPE_FIELDS (target), field_size);
1384
1385   if (TYPE_NAME (real_target))
1386     {
1387       if (TYPE_NAME (target))
1388         free (TYPE_NAME (target));
1389       TYPE_NAME (target) = concat (TYPE_NAME (real_target), "", "");
1390     }
1391 }
1392
1393 /* Patch up all appropriate typdef symbols in the opaque_type_chains
1394    so that they can be used to print out opaque data structures properly */
1395
1396 static void
1397 patch_opaque_types ()
1398 {
1399   struct symtab *s;
1400
1401   /* Look at each symbol in the per-file block of each symtab.  */
1402   for (s = symtab_list; s; s = s->next)
1403     {
1404       register struct block *b;
1405       register int i;
1406
1407       /* Go through the per-file symbols only */
1408       b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), 1);
1409       for (i = BLOCK_NSYMS (b) - 1; i >= 0; i--)
1410         {
1411           register struct symbol *real_sym;
1412
1413           /* Find completed typedefs to use to fix opaque ones.
1414              Remove syms from the chain when their types are stored,
1415              but search the whole chain, as there may be several syms
1416              from different files with the same name.  */
1417           real_sym = BLOCK_SYM (b, i);
1418           if (SYMBOL_CLASS (real_sym) == LOC_TYPEDEF &&
1419               SYMBOL_NAMESPACE (real_sym) == VAR_NAMESPACE &&
1420               TYPE_CODE (SYMBOL_TYPE (real_sym)) == TYPE_CODE_PTR &&
1421               TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (real_sym))) != 0)
1422             {
1423               register char *name = SYMBOL_NAME (real_sym);
1424               register int hash = hashname (name);
1425               register struct symbol *sym, *prev;
1426
1427               prev = 0;
1428               for (sym = opaque_type_chain[hash]; sym;)
1429                 {
1430                   if (name[0] == SYMBOL_NAME (sym)[0] &&
1431                       !strcmp (name + 1, SYMBOL_NAME (sym) + 1))
1432                     {
1433                       if (prev)
1434                         SYMBOL_VALUE (prev) = SYMBOL_VALUE (sym);
1435                       else
1436                         opaque_type_chain[hash]
1437                           = (struct symbol *) SYMBOL_VALUE (sym);
1438
1439                       patch_type (SYMBOL_TYPE (sym), SYMBOL_TYPE (real_sym));
1440
1441                       if (prev)
1442                         sym = (struct symbol *) SYMBOL_VALUE (prev);
1443                       else
1444                         sym = opaque_type_chain[hash];
1445                     }
1446                   else
1447                     {
1448                       prev = sym;
1449                       sym = (struct symbol *) SYMBOL_VALUE (sym);
1450                     }
1451                 }
1452             }
1453         }
1454     }
1455 }
1456 \f
1457 static struct symbol *
1458 process_coff_symbol (cs, aux)
1459      register struct coff_symbol *cs;
1460      register AUXENT *aux;
1461 {
1462   register struct symbol *sym
1463     = (struct symbol *) obstack_alloc (symbol_obstack, sizeof (struct symbol));
1464   char *name;
1465   char *dot;
1466 #ifdef NAMES_HAVE_UNDERSCORE
1467   int offset = 1;
1468 #else
1469   int offset = 0;
1470 #endif
1471
1472   bzero (sym, sizeof (struct symbol));
1473   name = cs->c_name;
1474   name = (name[0] == '_' ? name + offset : name);
1475   SYMBOL_NAME (sym) = obstack_copy0 (symbol_obstack, name, strlen (name));
1476
1477   /* default assumptions */
1478   SYMBOL_VALUE (sym) = cs->c_value;
1479   SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1480
1481   if (ISFCN (cs->c_type))
1482     {
1483       SYMBOL_TYPE (sym) = 
1484         lookup_function_type (decode_function_type (cs, cs->c_type, aux));
1485       SYMBOL_CLASS (sym) = LOC_BLOCK;
1486       if (cs->c_sclass == C_STAT)
1487         add_symbol_to_list (sym, &file_symbols);
1488       else if (cs->c_sclass == C_EXT)
1489         add_symbol_to_list (sym, &global_symbols);
1490     }
1491   else
1492     {
1493       SYMBOL_TYPE (sym) = decode_type (cs, cs->c_type, aux);
1494       switch (cs->c_sclass)
1495         {
1496           case C_NULL:
1497             break;
1498
1499           case C_AUTO:
1500             SYMBOL_CLASS (sym) = LOC_LOCAL;
1501             add_symbol_to_list (sym, &local_symbols);
1502             break;
1503
1504           case C_EXT:
1505             SYMBOL_CLASS (sym) = LOC_STATIC;
1506             add_symbol_to_list (sym, &global_symbols);
1507             break;
1508
1509           case C_STAT:
1510             SYMBOL_CLASS (sym) = LOC_STATIC;
1511             if (within_function) {
1512               /* Static symbol of local scope */
1513               add_symbol_to_list (sym, &local_symbols);
1514             }
1515             else {
1516               /* Static symbol at top level of file */
1517               add_symbol_to_list (sym, &file_symbols);
1518             }
1519             break;
1520
1521           case C_REG:
1522             SYMBOL_CLASS (sym) = LOC_REGISTER;
1523             add_symbol_to_list (sym, &local_symbols);
1524             break;
1525
1526           case C_LABEL:
1527             break;
1528
1529           case C_ARG:
1530             SYMBOL_CLASS (sym) = LOC_ARG;
1531             add_symbol_to_list (sym, &local_symbols);
1532 #ifndef clipper    
1533             /* If PCC says a parameter is a short or a char,
1534                it is really an int.  */
1535             if (SYMBOL_TYPE (sym) == builtin_type_char
1536                 || SYMBOL_TYPE (sym) == builtin_type_short)
1537               SYMBOL_TYPE (sym) = builtin_type_int;
1538             else if (SYMBOL_TYPE (sym) == builtin_type_unsigned_char
1539                      || SYMBOL_TYPE (sym) == builtin_type_unsigned_short)
1540               SYMBOL_TYPE (sym) = builtin_type_unsigned_int;
1541 #endif
1542             break;
1543
1544           case C_REGPARM:
1545             SYMBOL_CLASS (sym) = LOC_REGPARM;
1546             add_symbol_to_list (sym, &local_symbols);
1547 #ifndef clipper
1548             /* If PCC says a parameter is a short or a char,
1549                it is really an int.  */
1550             if (SYMBOL_TYPE (sym) == builtin_type_char
1551                 || SYMBOL_TYPE (sym) == builtin_type_short)
1552               SYMBOL_TYPE (sym) = builtin_type_int;
1553             else if (SYMBOL_TYPE (sym) == builtin_type_unsigned_char
1554                      || SYMBOL_TYPE (sym) == builtin_type_unsigned_short)
1555               SYMBOL_TYPE (sym) = builtin_type_unsigned_int;
1556 #endif
1557             break;
1558             
1559           case C_TPDEF:
1560             SYMBOL_CLASS (sym) = LOC_TYPEDEF;
1561             SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1562
1563             /* If type has no name, give it one */
1564             if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0 
1565                 && (TYPE_FLAGS (SYMBOL_TYPE (sym)) & TYPE_FLAG_PERM) == 0)
1566               TYPE_NAME (SYMBOL_TYPE (sym))
1567                                           = concat (SYMBOL_NAME (sym), "", "");
1568
1569             /* Keep track of any type which points to empty structured type,
1570                 so it can be filled from a definition from another file */
1571             if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_PTR &&
1572                 TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym))) == 0)
1573               {
1574                 register int i = hashname (SYMBOL_NAME (sym));
1575
1576                 SYMBOL_VALUE (sym) = (int) opaque_type_chain[i];
1577                 opaque_type_chain[i] = sym;
1578               }
1579             add_symbol_to_list (sym, &file_symbols);
1580             break;
1581
1582           case C_STRTAG:
1583           case C_UNTAG:
1584           case C_ENTAG:
1585             SYMBOL_CLASS (sym) = LOC_TYPEDEF;
1586             SYMBOL_NAMESPACE (sym) = STRUCT_NAMESPACE;
1587             if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0
1588                 && (TYPE_FLAGS (SYMBOL_TYPE (sym)) & TYPE_FLAG_PERM) == 0)
1589               TYPE_NAME (SYMBOL_TYPE (sym))
1590                 = concat ("",
1591                           (cs->c_sclass == C_ENTAG
1592                            ? "enum "
1593                            : (cs->c_sclass == C_STRTAG
1594                               ? "struct " : "union ")),
1595                           SYMBOL_NAME (sym));
1596             add_symbol_to_list (sym, &file_symbols);
1597             break;
1598
1599           default:
1600             break;
1601         }
1602     }
1603   return sym;
1604 }
1605 \f
1606 /* Decode a coff type specifier;
1607    return the type that is meant.  */
1608
1609 static
1610 struct type *
1611 decode_type (cs, c_type, aux)
1612      register struct coff_symbol *cs;
1613      unsigned int c_type;
1614      register AUXENT *aux;
1615 {
1616   register struct type *type = 0;
1617   register int n;
1618   unsigned int new_c_type;
1619
1620   if (c_type & ~N_BTMASK)
1621     {
1622       new_c_type = DECREF (c_type);
1623       if (ISPTR (c_type))
1624         {
1625           type = decode_type (cs, new_c_type, aux);
1626           type = lookup_pointer_type (type);
1627         }
1628       else if (ISFCN (c_type))
1629         {
1630           type = decode_type (cs, new_c_type, aux);
1631           type = lookup_function_type (type);
1632         }
1633       else if (ISARY (c_type))
1634         {
1635           int i, n;
1636           register unsigned short *dim;
1637           struct type *base_type;
1638
1639           /* Define an array type.  */
1640           /* auxent refers to array, not base type */
1641           if (aux->x_sym.x_tagndx == 0)
1642             cs->c_nsyms = 1;
1643
1644           /* shift the indices down */
1645           dim = &aux->x_sym.x_fcnary.x_ary.x_dimen[0];
1646           i = 1;
1647           n = dim[0];
1648           for (i = 0; *dim && i < DIMNUM - 1; i++, dim++)
1649             *dim = *(dim + 1);
1650           *dim = 0;
1651
1652           type = (struct type *)
1653                     obstack_alloc (symbol_obstack, sizeof (struct type));
1654           bzero (type, sizeof (struct type));
1655
1656           base_type = decode_type (cs, new_c_type, aux);
1657
1658           TYPE_CODE (type) = TYPE_CODE_ARRAY;
1659           TYPE_TARGET_TYPE (type) = base_type;
1660           TYPE_LENGTH (type) = n * TYPE_LENGTH (base_type);
1661         }
1662       return type;
1663     }
1664
1665   /* Reference to existing type */
1666   if (cs->c_nsyms > 1 && aux->x_sym.x_tagndx != 0)
1667     {
1668       type = coff_alloc_type (aux->x_sym.x_tagndx);
1669       return type;
1670     }
1671
1672   return decode_base_type (cs, BTYPE (c_type), aux);
1673 }
1674
1675 /* Decode a coff type specifier for function definition;
1676    return the type that the function returns.  */
1677
1678 static
1679 struct type *
1680 decode_function_type (cs, c_type, aux)
1681      register struct coff_symbol *cs;
1682      unsigned int c_type;
1683      register AUXENT *aux;
1684 {
1685   if (aux->x_sym.x_tagndx == 0)
1686     cs->c_nsyms = 1;    /* auxent refers to function, not base type */
1687
1688   return decode_type (cs, DECREF (cs->c_type), aux);
1689 }
1690 \f
1691 /* basic C types */
1692
1693 static
1694 struct type *
1695 decode_base_type (cs, c_type, aux)
1696      register struct coff_symbol *cs;
1697      unsigned int c_type;
1698      register AUXENT *aux;
1699 {
1700   struct type *type;
1701
1702   switch (c_type)
1703     {
1704       case T_NULL:
1705         /* shows up with "void (*foo)();" structure members */
1706         return builtin_type_void;
1707
1708       case T_ARG:
1709         /* shouldn't show up here */
1710         break;
1711
1712       case T_CHAR:
1713         return builtin_type_char;
1714
1715       case T_SHORT:
1716         return builtin_type_short;
1717
1718       case T_INT:
1719         return builtin_type_int;
1720
1721       case T_LONG:
1722         return builtin_type_long;
1723
1724       case T_FLOAT:
1725         return builtin_type_float;
1726
1727       case T_DOUBLE:
1728         return builtin_type_double;
1729
1730       case T_STRUCT:
1731         if (cs->c_nsyms != 2)
1732           {
1733             /* anonymous structure type */
1734             type = coff_alloc_type (cs->c_symnum);
1735             TYPE_CODE (type) = TYPE_CODE_STRUCT;
1736             TYPE_NAME (type) = concat ("struct ", "<opaque>", "");
1737             TYPE_LENGTH (type) = 0;
1738             TYPE_FIELDS (type) = 0;
1739             TYPE_NFIELDS (type) = 0;
1740           }
1741         else
1742           {
1743             type = read_struct_type (cs->c_symnum,
1744                                     aux->x_sym.x_misc.x_lnsz.x_size,
1745                                     aux->x_sym.x_fcnary.x_fcn.x_endndx);
1746           }
1747         return type;
1748
1749       case T_UNION:
1750         if (cs->c_nsyms != 2)
1751           {
1752             /* anonymous union type */
1753             type = coff_alloc_type (cs->c_symnum);
1754             TYPE_NAME (type) = concat ("union ", "<opaque>", "");
1755             TYPE_LENGTH (type) = 0;
1756             TYPE_FIELDS (type) = 0;
1757             TYPE_NFIELDS (type) = 0;
1758           }
1759         else
1760           {
1761             type = read_struct_type (cs->c_symnum,
1762                                     aux->x_sym.x_misc.x_lnsz.x_size,
1763                                     aux->x_sym.x_fcnary.x_fcn.x_endndx);
1764           }
1765         TYPE_CODE (type) = TYPE_CODE_UNION;
1766         return type;
1767
1768       case T_ENUM:
1769         return read_enum_type (cs->c_symnum,
1770                                     aux->x_sym.x_misc.x_lnsz.x_size,
1771                                     aux->x_sym.x_fcnary.x_fcn.x_endndx);
1772
1773       case T_MOE:
1774         /* shouldn't show up here */
1775         break;
1776
1777       case T_UCHAR:
1778         return builtin_type_unsigned_char;
1779
1780       case T_USHORT:
1781         return builtin_type_unsigned_short;
1782
1783       case T_UINT:
1784         return builtin_type_unsigned_int;
1785
1786       case T_ULONG:
1787         return builtin_type_unsigned_long;
1788     }
1789   printf ("unexpected type %d at symnum %d\n", c_type, cs->c_symnum);
1790   return builtin_type_void;
1791 }
1792 \f
1793 /* This page contains subroutines of read_type.  */
1794
1795 /* Read the description of a structure (or union type)
1796    and return an object describing the type.  */
1797
1798 static struct type *
1799 read_struct_type (index, length, lastsym)
1800      int index;
1801      int length;
1802      int lastsym;
1803 {
1804   struct nextfield
1805     {
1806       struct nextfield *next;
1807       struct field field;
1808     };
1809
1810   register struct type *type;
1811   register struct nextfield *list = 0;
1812   struct nextfield *new;
1813   int nfields = 0;
1814   register int n;
1815   char *name;
1816 #ifdef NAMES_HAVE_UNDERSCORE
1817   int offset = 1;
1818 #else
1819   int offset = 0;
1820 #endif
1821   struct coff_symbol member_sym;
1822   register struct coff_symbol *ms = &member_sym;
1823   SYMENT sub_sym;
1824   AUXENT sub_aux;
1825   int done = 0;
1826
1827   type = coff_alloc_type (index);
1828   TYPE_CODE (type) = TYPE_CODE_STRUCT;
1829   TYPE_LENGTH (type) = length;
1830
1831   while (!done && symnum < lastsym && symnum < nlist_nsyms_global)
1832     {
1833       read_one_sym (ms, &sub_sym, &sub_aux);
1834       name = ms->c_name;
1835       name = (name[0] == '_' ? name + offset : name);
1836
1837       switch (ms->c_sclass)
1838         {
1839           case C_MOS:
1840           case C_MOU:
1841
1842             /* Get space to record the next field's data.  */
1843             new = (struct nextfield *) alloca (sizeof (struct nextfield));
1844             new->next = list;
1845             list = new;
1846
1847             /* Save the data.  */
1848             list->field.name = savestring (name, strlen (name));
1849             list->field.type = decode_type (ms, ms->c_type, &sub_aux);
1850             list->field.bitpos = 8 * ms->c_value;
1851             list->field.bitsize = 0;
1852             nfields++;
1853             break;
1854
1855           case C_FIELD:
1856
1857             /* Get space to record the next field's data.  */
1858             new = (struct nextfield *) alloca (sizeof (struct nextfield));
1859             new->next = list;
1860             list = new;
1861
1862             /* Save the data.  */
1863             list->field.name = savestring (name, strlen (name));
1864             list->field.type = decode_type (ms, ms->c_type, &sub_aux);
1865             list->field.bitpos = ms->c_value;
1866             list->field.bitsize = sub_aux.x_sym.x_misc.x_lnsz.x_size;
1867             nfields++;
1868             break;
1869
1870           case C_EOS:
1871             done = 1;
1872             break;
1873         }
1874     }
1875   /* Now create the vector of fields, and record how big it is.  */
1876
1877   TYPE_NFIELDS (type) = nfields;
1878   TYPE_FIELDS (type) = (struct field *)
1879                 obstack_alloc (symbol_obstack, sizeof (struct field) * nfields);
1880
1881   /* Copy the saved-up fields into the field vector.  */
1882
1883   for (n = nfields; list; list = list->next)
1884     TYPE_FIELD (type, --n) = list->field;
1885
1886   return type;
1887 }
1888 \f
1889 /* Read a definition of an enumeration type,
1890    and create and return a suitable type object.
1891    Also defines the symbols that represent the values of the type.  */
1892
1893 static struct type *
1894 read_enum_type (index, length, lastsym)
1895      int index;
1896      int length;
1897      int lastsym;
1898 {
1899   register struct symbol *sym;
1900   register struct type *type;
1901   int nsyms = 0;
1902   struct pending **symlist;
1903   struct coff_symbol member_sym;
1904   register struct coff_symbol *ms = &member_sym;
1905   SYMENT sub_sym;
1906   AUXENT sub_aux;
1907   struct pending *osyms, *syms;
1908   register int n;
1909   char *name;
1910 #ifdef NAMES_HAVE_UNDERSCORE
1911   int offset = 1;
1912 #else
1913   int offset = 0;
1914 #endif
1915
1916   type = coff_alloc_type (index);
1917   if (within_function)
1918     symlist = &local_symbols;
1919   else
1920     symlist = &file_symbols;
1921   osyms = *symlist;
1922
1923   while (symnum < lastsym && symnum < nlist_nsyms_global)
1924     {
1925       read_one_sym (ms, &sub_sym, &sub_aux);
1926       name = ms->c_name;
1927       name = (name[0] == '_' ? name + offset : name);
1928
1929       switch (ms->c_sclass)
1930         {
1931           case C_MOE:
1932             sym = (struct symbol *) xmalloc (sizeof (struct symbol));
1933             bzero (sym, sizeof (struct symbol));
1934
1935             SYMBOL_NAME (sym) = savestring (name, strlen (name));
1936             SYMBOL_CLASS (sym) = LOC_CONST;
1937             SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1938             SYMBOL_VALUE (sym) = ms->c_value;
1939             add_symbol_to_list (sym, symlist);
1940             nsyms++;
1941             break;
1942
1943           case C_EOS:
1944             break;
1945         }
1946     }
1947
1948   /* Now fill in the fields of the type-structure.  */
1949
1950   TYPE_LENGTH (type) = sizeof (int);
1951   TYPE_CODE (type) = TYPE_CODE_ENUM;
1952   TYPE_NFIELDS (type) = nsyms;
1953   TYPE_FIELDS (type) = (struct field *)
1954                 obstack_alloc (symbol_obstack, sizeof (struct field) * nsyms);
1955
1956   /* Find the symbols for the values and put them into the type.
1957      The symbols can be found in the symlist that we put them on
1958      to cause them to be defined.  osyms contains the old value
1959      of that symlist; everything up to there was defined by us.  */
1960
1961   for (syms = *symlist, n = nsyms; syms != osyms; syms = syms->next)
1962     {
1963       SYMBOL_TYPE (syms->symbol) = type;
1964       TYPE_FIELD_NAME (type, --n) = SYMBOL_NAME (syms->symbol);
1965       TYPE_FIELD_VALUE (type, n) = 0;
1966       TYPE_FIELD_BITPOS (type, n) = SYMBOL_VALUE (syms->symbol);
1967       TYPE_FIELD_BITSIZE (type, n) = 0;
1968     }
1969   return type;
1970 }
1971
1972 /* This function is really horrible, but to avoid it, there would need
1973    to be more filling in of forward references.  THIS SHOULD BE MOVED
1974    OUT OF COFFREAD.C AND DBXREAD.C TO SOME PLACE WHERE IT CAN BE SHARED. */
1975 int
1976 fill_in_vptr_fieldno (type)
1977      struct type *type;
1978 {
1979   if (TYPE_VPTR_FIELDNO (type) < 0)
1980     TYPE_VPTR_FIELDNO (type) =
1981       fill_in_vptr_fieldno (TYPE_BASECLASS (type, 1));
1982   return TYPE_VPTR_FIELDNO (type);
1983 }
1984
1985 /* partial symbol tables are not implemented in coff, therefore
1986    block_for_pc() (and others) will never decide to call this. */
1987
1988 extern struct symtab *
1989 psymtab_to_symtab ()
1990 {
1991   fatal ("error: Someone called psymtab_to_symtab\n");
1992 }
1993
1994 /* These will stay zero all the time */
1995 struct psymbol_allocation_list global_psymbols, static_psymbols;
1996
1997 _initialize_coff ()
1998 {
1999   symfile = 0;
2000
2001   bzero (&global_psymbols, sizeof (global_psymbols));
2002   bzero (&static_psymbols, sizeof (static_psymbols));
2003
2004   add_com ("symbol-file", class_files, symbol_file_command,
2005            "Load symbol table (in coff format) from executable file FILE.");
2006 }
2007
2008
2009 #endif /* COFF_FORMAT */
2010
This page took 0.134721 seconds and 4 git commands to generate.