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