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