Move putchar_filtered() to utils.c.
[platform/upstream/binutils.git] / gdb / dstread.c
1 /* Read apollo DST symbol tables and convert to internal format, for GDB.
2    Contributed by Troy Rollo, University of NSW (troy@cbme.unsw.edu.au).
3    Copyright 1993 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., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21 \f
22 #include "defs.h"
23 #include "symtab.h"
24 #include "gdbtypes.h"
25 #include "breakpoint.h"
26 #include "bfd.h"
27 #include "symfile.h"
28 #include "objfiles.h"
29 #include "buildsym.h"
30 #include "obstack.h"
31
32 #include "gdb_string.h"
33
34 #include "dst.h"
35
36 CORE_ADDR cur_src_start_addr, cur_src_end_addr;
37 dst_sec blocks_info, lines_info, symbols_info;
38
39 /* Vector of line number information.  */
40
41 static struct linetable *line_vector;
42
43 /* Index of next entry to go in line_vector_index.  */
44
45 static int line_vector_index;
46
47 /* Last line number recorded in the line vector.  */
48
49 static int prev_line_number;
50
51 /* Number of elements allocated for line_vector currently.  */
52
53 static int line_vector_length;
54
55 static int init_dst_sections (int);
56
57 static void read_dst_symtab (struct objfile *);
58
59 static void find_dst_sections (bfd *, sec_ptr, PTR);
60
61 static void dst_symfile_init (struct objfile *);
62
63 static void dst_new_init (struct objfile *);
64
65 static void dst_symfile_read (struct objfile *, int);
66
67 static void dst_symfile_finish (struct objfile *);
68
69 static void dst_end_symtab (struct objfile *);
70
71 static void complete_symtab (char *, CORE_ADDR, unsigned int);
72
73 static void dst_start_symtab (void);
74
75 static void dst_record_line (int, CORE_ADDR);
76
77 /* Manage the vector of line numbers.  */
78 /* FIXME: Use record_line instead.  */
79
80 static void
81 dst_record_line (int line, CORE_ADDR pc)
82 {
83   struct linetable_entry *e;
84   /* Make sure line vector is big enough.  */
85
86   if (line_vector_index + 2 >= line_vector_length)
87     {
88       line_vector_length *= 2;
89       line_vector = (struct linetable *)
90         xrealloc ((char *) line_vector, sizeof (struct linetable)
91                   + (line_vector_length
92                      * sizeof (struct linetable_entry)));
93     }
94
95   e = line_vector->item + line_vector_index++;
96   e->line = line;
97   e->pc = pc;
98 }
99 \f
100 /* Start a new symtab for a new source file.
101    It indicates the start of data for one original source file.  */
102 /* FIXME: use start_symtab, like coffread.c now does.  */
103
104 static void
105 dst_start_symtab (void)
106 {
107   /* Initialize the source file line number information for this file.  */
108
109   if (line_vector)              /* Unlikely, but maybe possible? */
110     xfree (line_vector);
111   line_vector_index = 0;
112   line_vector_length = 1000;
113   prev_line_number = -2;        /* Force first line number to be explicit */
114   line_vector = (struct linetable *)
115     xmalloc (sizeof (struct linetable)
116              + line_vector_length * sizeof (struct linetable_entry));
117 }
118
119 /* Save the vital information from when starting to read a file,
120    for use when closing off the current file.
121    NAME is the file name the symbols came from, START_ADDR is the first
122    text address for the file, and SIZE is the number of bytes of text.  */
123
124 static void
125 complete_symtab (char *name, CORE_ADDR start_addr, unsigned int size)
126 {
127   last_source_file = savestring (name, strlen (name));
128   cur_src_start_addr = start_addr;
129   cur_src_end_addr = start_addr + size;
130
131   if (current_objfile->ei.entry_point >= cur_src_start_addr &&
132       current_objfile->ei.entry_point < cur_src_end_addr)
133     {
134       current_objfile->ei.entry_file_lowpc = cur_src_start_addr;
135       current_objfile->ei.entry_file_highpc = cur_src_end_addr;
136     }
137 }
138
139 /* Finish the symbol definitions for one main source file,
140    close off all the lexical contexts for that file
141    (creating struct block's for them), then make the
142    struct symtab for that file and put it in the list of all such. */
143 /* FIXME: Use end_symtab, like coffread.c now does.  */
144
145 static void
146 dst_end_symtab (struct objfile *objfile)
147 {
148   register struct symtab *symtab;
149   register struct blockvector *blockvector;
150   register struct linetable *lv;
151
152   /* Create the blockvector that points to all the file's blocks.  */
153
154   blockvector = make_blockvector (objfile);
155
156   /* Now create the symtab object for this source file.  */
157   symtab = allocate_symtab (last_source_file, objfile);
158
159   /* Fill in its components.  */
160   symtab->blockvector = blockvector;
161   symtab->free_code = free_linetable;
162   symtab->free_ptr = 0;
163   symtab->filename = last_source_file;
164   symtab->dirname = NULL;
165   symtab->debugformat = obsavestring ("Apollo DST", 10,
166                                       &objfile->symbol_obstack);
167   lv = line_vector;
168   lv->nitems = line_vector_index;
169   symtab->linetable = (struct linetable *)
170     xrealloc ((char *) lv, (sizeof (struct linetable)
171                             + lv->nitems * sizeof (struct linetable_entry)));
172
173   free_named_symtabs (symtab->filename);
174
175   /* Reinitialize for beginning of new file. */
176   line_vector = 0;
177   line_vector_length = -1;
178   last_source_file = NULL;
179 }
180 \f
181 /* dst_symfile_init ()
182    is the dst-specific initialization routine for reading symbols.
183
184    We will only be called if this is a DST or DST-like file.
185    BFD handles figuring out the format of the file, and code in symtab.c
186    uses BFD's determination to vector to us.
187
188    The ultimate result is a new symtab (or, FIXME, eventually a psymtab).  */
189
190 static void
191 dst_symfile_init (struct objfile *objfile)
192 {
193   asection *section;
194   bfd *abfd = objfile->obfd;
195
196   init_entry_point_info (objfile);
197
198 }
199
200 /* This function is called for every section; it finds the outer limits
201    of the line table (minimum and maximum file offset) so that the
202    mainline code can read the whole thing for efficiency.  */
203
204 /* ARGSUSED */
205 static void
206 find_dst_sections (bfd *abfd, sec_ptr asect, PTR vpinfo)
207 {
208   int size, count;
209   long base;
210   file_ptr offset, maxoff;
211   dst_sec *section;
212
213 /* WARNING WILL ROBINSON!  ACCESSING BFD-PRIVATE DATA HERE!  FIXME!  */
214   size = asect->_raw_size;
215   offset = asect->filepos;
216   base = asect->vma;
217 /* End of warning */
218
219   section = NULL;
220   if (!strcmp (asect->name, ".blocks"))
221     section = &blocks_info;
222   else if (!strcmp (asect->name, ".lines"))
223     section = &lines_info;
224   else if (!strcmp (asect->name, ".symbols"))
225     section = &symbols_info;
226   if (!section)
227     return;
228   section->size = size;
229   section->position = offset;
230   section->base = base;
231 }
232
233
234 /* The BFD for this file -- only good while we're actively reading
235    symbols into a psymtab or a symtab.  */
236
237 static bfd *symfile_bfd;
238
239 /* Read a symbol file, after initialization by dst_symfile_init.  */
240 /* FIXME!  Addr and Mainline are not used yet -- this will not work for
241    shared libraries or add_file!  */
242
243 /* ARGSUSED */
244 static void
245 dst_symfile_read (struct objfile *objfile, int mainline)
246 {
247   bfd *abfd = objfile->obfd;
248   char *name = bfd_get_filename (abfd);
249   int desc;
250   register int val;
251   int num_symbols;
252   int symtab_offset;
253   int stringtab_offset;
254
255   symfile_bfd = abfd;           /* Kludge for swap routines */
256
257 /* WARNING WILL ROBINSON!  ACCESSING BFD-PRIVATE DATA HERE!  FIXME!  */
258   desc = fileno ((FILE *) (abfd->iostream));    /* File descriptor */
259
260   /* Read the line number table, all at once.  */
261   bfd_map_over_sections (abfd, find_dst_sections, (PTR) NULL);
262
263   val = init_dst_sections (desc);
264   if (val < 0)
265     error ("\"%s\": error reading debugging symbol tables\n", name);
266
267   init_minimal_symbol_collection ();
268   make_cleanup_discard_minimal_symbols ();
269
270   /* Now that the executable file is positioned at symbol table,
271      process it and define symbols accordingly.  */
272
273   read_dst_symtab (objfile);
274
275   /* Sort symbols alphabetically within each block.  */
276
277   {
278     struct symtab *s;
279     for (s = objfile->symtabs; s != NULL; s = s->next)
280       {
281         sort_symtab_syms (s);
282       }
283   }
284
285   /* Install any minimal symbols that have been collected as the current
286      minimal symbols for this objfile. */
287
288   install_minimal_symbols (objfile);
289 }
290
291 static void
292 dst_new_init (struct objfile *ignore)
293 {
294   /* Nothin' to do */
295 }
296
297 /* Perform any local cleanups required when we are done with a particular
298    objfile.  I.E, we are in the process of discarding all symbol information
299    for an objfile, freeing up all memory held for it, and unlinking the
300    objfile struct from the global list of known objfiles. */
301
302 static void
303 dst_symfile_finish (struct objfile *objfile)
304 {
305   /* Nothing to do */
306 }
307 \f
308
309 /* Get the next line number from the DST. Returns 0 when we hit an
310  * end directive or cannot continue for any other reason.
311  *
312  * Note that ordinary pc deltas are multiplied by two. Apparently
313  * this is what was really intended.
314  */
315 static int
316 get_dst_line (signed char **buffer, long *pc)
317 {
318   static last_pc = 0;
319   static long last_line = 0;
320   static int last_file = 0;
321   dst_ln_entry_ptr_t entry;
322   int size;
323   dst_src_loc_t *src_loc;
324
325   if (*pc != -1)
326     {
327       last_pc = *pc;
328       *pc = -1;
329     }
330   entry = (dst_ln_entry_ptr_t) * buffer;
331
332   while (dst_ln_ln_delta (*entry) == dst_ln_escape_flag)
333     {
334       switch (entry->esc.esc_code)
335         {
336         case dst_ln_pad:
337           size = 1;             /* pad byte */
338           break;
339         case dst_ln_file:
340           /* file escape.  Next 4 bytes are a dst_src_loc_t */
341           size = 5;
342           src_loc = (dst_src_loc_t *) (*buffer + 1);
343           last_line = src_loc->line_number;
344           last_file = src_loc->file_index;
345           break;
346         case dst_ln_dln1_dpc1:
347           /* 1 byte line delta, 1 byte pc delta */
348           last_line += (*buffer)[1];
349           last_pc += 2 * (unsigned char) (*buffer)[2];
350           dst_record_line (last_line, last_pc);
351           size = 3;
352           break;
353         case dst_ln_dln2_dpc2:
354           /* 2 bytes line delta, 2 bytes pc delta */
355           last_line += *(short *) (*buffer + 1);
356           last_pc += 2 * (*(short *) (*buffer + 3));
357           size = 5;
358           dst_record_line (last_line, last_pc);
359           break;
360         case dst_ln_ln4_pc4:
361           /* 4 bytes ABSOLUTE line number, 4 bytes ABSOLUTE pc */
362           last_line = *(unsigned long *) (*buffer + 1);
363           last_pc = *(unsigned long *) (*buffer + 5);
364           size = 9;
365           dst_record_line (last_line, last_pc);
366           break;
367         case dst_ln_dln1_dpc0:
368           /* 1 byte line delta, pc delta = 0 */
369           size = 2;
370           last_line += (*buffer)[1];
371           break;
372         case dst_ln_ln_off_1:
373           /* statement escape, stmt # = 1 (2nd stmt on line) */
374           size = 1;
375           break;
376         case dst_ln_ln_off:
377           /* statement escape, stmt # = next byte */
378           size = 2;
379           break;
380         case dst_ln_entry:
381           /* entry escape, next byte is entry number */
382           size = 2;
383           break;
384         case dst_ln_exit:
385           /* exit escape */
386           size = 1;
387           break;
388         case dst_ln_stmt_end:
389           /* gap escape, 4 bytes pc delta */
390           size = 5;
391           /* last_pc += 2 * (*(long *) (*buffer + 1)); */
392           /* Apparently this isn't supposed to actually modify
393            * the pc value. Totally weird.
394            */
395           break;
396         case dst_ln_escape_11:
397         case dst_ln_escape_12:
398         case dst_ln_escape_13:
399           size = 1;
400           break;
401         case dst_ln_nxt_byte:
402           /* This shouldn't happen. If it does, we're SOL */
403           return 0;
404           break;
405         case dst_ln_end:
406           /* end escape, final entry follows */
407           return 0;
408         }
409       *buffer += (size < 0) ? -size : size;
410       entry = (dst_ln_entry_ptr_t) * buffer;
411     }
412   last_line += dst_ln_ln_delta (*entry);
413   last_pc += entry->delta.pc_delta * 2;
414   (*buffer)++;
415   dst_record_line (last_line, last_pc);
416   return 1;
417 }
418
419 static void
420 enter_all_lines (char *buffer, long address)
421 {
422   if (buffer)
423     while (get_dst_line (&buffer, &address));
424 }
425
426 static int
427 get_dst_entry (char *buffer, dst_rec_ptr_t *ret_entry)
428 {
429   int size;
430   dst_rec_ptr_t entry;
431   static int last_type;
432   int ar_size;
433   static unsigned lu3;
434
435   entry = (dst_rec_ptr_t) buffer;
436   switch (entry->rec_type)
437     {
438     case dst_typ_pad:
439       size = 0;
440       break;
441     case dst_typ_comp_unit:
442       size = sizeof (DST_comp_unit (entry));
443       break;
444     case dst_typ_section_tab:
445       size = sizeof (DST_section_tab (entry))
446         + ((int) DST_section_tab (entry).number_of_sections
447            - dst_dummy_array_size) * sizeof (long);
448       break;
449     case dst_typ_file_tab:
450       size = sizeof (DST_file_tab (entry))
451         + ((int) DST_file_tab (entry).number_of_files
452            - dst_dummy_array_size) * sizeof (dst_file_desc_t);
453       break;
454     case dst_typ_block:
455       size = sizeof (DST_block (entry))
456         + ((int) DST_block (entry).n_of_code_ranges
457            - dst_dummy_array_size) * sizeof (dst_code_range_t);
458       break;
459     case dst_typ_5:
460       size = -1;
461       break;
462     case dst_typ_var:
463       size = sizeof (DST_var (entry)) -
464         sizeof (dst_var_loc_long_t) * dst_dummy_array_size +
465         DST_var (entry).no_of_locs *
466         (DST_var (entry).short_locs ?
467          sizeof (dst_var_loc_short_t) :
468          sizeof (dst_var_loc_long_t));
469       break;
470     case dst_typ_pointer:
471       size = sizeof (DST_pointer (entry));
472       break;
473     case dst_typ_array:
474       size = sizeof (DST_array (entry));
475       break;
476     case dst_typ_subrange:
477       size = sizeof (DST_subrange (entry));
478       break;
479     case dst_typ_set:
480       size = sizeof (DST_set (entry));
481       break;
482     case dst_typ_implicit_enum:
483       size = sizeof (DST_implicit_enum (entry))
484         + ((int) DST_implicit_enum (entry).nelems
485            - dst_dummy_array_size) * sizeof (dst_rel_offset_t);
486       break;
487     case dst_typ_explicit_enum:
488       size = sizeof (DST_explicit_enum (entry))
489         + ((int) DST_explicit_enum (entry).nelems
490            - dst_dummy_array_size) * sizeof (dst_enum_elem_t);
491       break;
492     case dst_typ_short_rec:
493       size = sizeof (DST_short_rec (entry))
494         + DST_short_rec (entry).nfields * sizeof (dst_short_field_t)
495         - dst_dummy_array_size * sizeof (dst_field_t);
496       break;
497     case dst_typ_short_union:
498       size = sizeof (DST_short_union (entry))
499         + DST_short_union (entry).nfields * sizeof (dst_short_field_t)
500         - dst_dummy_array_size * sizeof (dst_field_t);
501       break;
502     case dst_typ_file:
503       size = sizeof (DST_file (entry));
504       break;
505     case dst_typ_offset:
506       size = sizeof (DST_offset (entry));
507       break;
508     case dst_typ_alias:
509       size = sizeof (DST_alias (entry));
510       break;
511     case dst_typ_signature:
512       size = sizeof (DST_signature (entry)) +
513         ((int) DST_signature (entry).nargs -
514          dst_dummy_array_size) * sizeof (dst_arg_t);
515       break;
516     case dst_typ_21:
517       size = -1;
518       break;
519     case dst_typ_old_label:
520       size = sizeof (DST_old_label (entry));
521       break;
522     case dst_typ_scope:
523       size = sizeof (DST_scope (entry));
524       break;
525     case dst_typ_end_scope:
526       size = 0;
527       break;
528     case dst_typ_25:
529     case dst_typ_26:
530       size = -1;
531       break;
532     case dst_typ_string_tab:
533     case dst_typ_global_name_tab:
534       size = sizeof (DST_string_tab (entry))
535         + DST_string_tab (entry).length
536         - dst_dummy_array_size;
537       break;
538     case dst_typ_forward:
539       size = sizeof (DST_forward (entry));
540       get_dst_entry ((char *) entry + DST_forward (entry).rec_off, &entry);
541       break;
542     case dst_typ_aux_size:
543       size = sizeof (DST_aux_size (entry));
544       break;
545     case dst_typ_aux_align:
546       size = sizeof (DST_aux_align (entry));
547       break;
548     case dst_typ_aux_field_size:
549       size = sizeof (DST_aux_field_size (entry));
550       break;
551     case dst_typ_aux_field_off:
552       size = sizeof (DST_aux_field_off (entry));
553       break;
554     case dst_typ_aux_field_align:
555       size = sizeof (DST_aux_field_align (entry));
556       break;
557     case dst_typ_aux_qual:
558       size = sizeof (DST_aux_qual (entry));
559       break;
560     case dst_typ_aux_var_bound:
561       size = sizeof (DST_aux_var_bound (entry));
562       break;
563     case dst_typ_extension:
564       size = DST_extension (entry).rec_size;
565       break;
566     case dst_typ_string:
567       size = sizeof (DST_string (entry));
568       break;
569     case dst_typ_old_entry:
570       size = 48;                /* Obsolete entry type */
571       break;
572     case dst_typ_const:
573       size = sizeof (DST_const (entry))
574         + DST_const (entry).value.length
575         - sizeof (DST_const (entry).value.val);
576       break;
577     case dst_typ_reference:
578       size = sizeof (DST_reference (entry));
579       break;
580     case dst_typ_old_record:
581     case dst_typ_old_union:
582     case dst_typ_record:
583     case dst_typ_union:
584       size = sizeof (DST_record (entry))
585         + ((int) DST_record (entry).nfields
586            - dst_dummy_array_size) * sizeof (dst_field_t);
587       break;
588     case dst_typ_aux_type_deriv:
589       size = sizeof (DST_aux_type_deriv (entry));
590       break;
591     case dst_typ_locpool:
592       size = sizeof (DST_locpool (entry))
593         + ((int) DST_locpool (entry).length -
594            dst_dummy_array_size);
595       break;
596     case dst_typ_variable:
597       size = sizeof (DST_variable (entry));
598       break;
599     case dst_typ_label:
600       size = sizeof (DST_label (entry));
601       break;
602     case dst_typ_entry:
603       size = sizeof (DST_entry (entry));
604       break;
605     case dst_typ_aux_lifetime:
606       size = sizeof (DST_aux_lifetime (entry));
607       break;
608     case dst_typ_aux_ptr_base:
609       size = sizeof (DST_aux_ptr_base (entry));
610       break;
611     case dst_typ_aux_src_range:
612       size = sizeof (DST_aux_src_range (entry));
613       break;
614     case dst_typ_aux_reg_val:
615       size = sizeof (DST_aux_reg_val (entry));
616       break;
617     case dst_typ_aux_unit_names:
618       size = sizeof (DST_aux_unit_names (entry))
619         + ((int) DST_aux_unit_names (entry).number_of_names
620            - dst_dummy_array_size) * sizeof (dst_rel_offset_t);
621       break;
622     case dst_typ_aux_sect_info:
623       size = sizeof (DST_aux_sect_info (entry))
624         + ((int) DST_aux_sect_info (entry).number_of_refs
625            - dst_dummy_array_size) * sizeof (dst_sect_ref_t);
626       break;
627     default:
628       size = -1;
629       break;
630     }
631   if (size == -1)
632     {
633       fprintf_unfiltered (gdb_stderr, "Warning: unexpected DST entry type (%d) found\nLast valid entry was of type: %d\n",
634                           (int) entry->rec_type,
635                           last_type);
636       fprintf_unfiltered (gdb_stderr, "Last unknown_3 value: %d\n", lu3);
637       size = 0;
638     }
639   else
640     last_type = entry->rec_type;
641   if (size & 1)                 /* Align on a word boundary */
642     size++;
643   size += 2;
644   *ret_entry = entry;
645   return size;
646 }
647
648 static int
649 next_dst_entry (char **buffer, dst_rec_ptr_t *entry, dst_sec *table)
650 {
651   if (*buffer - table->buffer >= table->size)
652     {
653       *entry = NULL;
654       return 0;
655     }
656   *buffer += get_dst_entry (*buffer, entry);
657   return 1;
658 }
659
660 #define NEXT_BLK(a, b) next_dst_entry(a, b, &blocks_info)
661 #define NEXT_SYM(a, b) next_dst_entry(a, b, &symbols_info)
662 #define DST_OFFSET(a, b) ((char *) (a) + (b))
663
664 static dst_rec_ptr_t section_table = NULL;
665
666 char *
667 get_sec_ref (dst_sect_ref_t *ref)
668 {
669   dst_sec *section = NULL;
670   long offset;
671
672   if (!section_table || !ref->sect_index)
673     return NULL;
674   offset = DST_section_tab (section_table).section_base[ref->sect_index - 1]
675     + ref->sect_offset;
676   if (offset >= blocks_info.base &&
677       offset < blocks_info.base + blocks_info.size)
678     section = &blocks_info;
679   else if (offset >= symbols_info.base &&
680            offset < symbols_info.base + symbols_info.size)
681     section = &symbols_info;
682   else if (offset >= lines_info.base &&
683            offset < lines_info.base + lines_info.size)
684     section = &lines_info;
685   if (!section)
686     return NULL;
687   return section->buffer + (offset - section->base);
688 }
689
690 CORE_ADDR
691 dst_get_addr (int section, long offset)
692 {
693   if (!section_table || !section)
694     return 0;
695   return DST_section_tab (section_table).section_base[section - 1] + offset;
696 }
697
698 CORE_ADDR
699 dst_sym_addr (dst_sect_ref_t *ref)
700 {
701   if (!section_table || !ref->sect_index)
702     return 0;
703   return DST_section_tab (section_table).section_base[ref->sect_index - 1]
704     + ref->sect_offset;
705 }
706
707 static struct type *
708 create_new_type (struct objfile *objfile)
709 {
710   struct type *type;
711
712   type = (struct type *)
713     obstack_alloc (&objfile->symbol_obstack, sizeof (struct type));
714   memset (type, 0, sizeof (struct type));
715   return type;
716 }
717
718 static struct symbol *
719 create_new_symbol (struct objfile *objfile, char *name)
720 {
721   struct symbol *sym = (struct symbol *)
722   obstack_alloc (&objfile->symbol_obstack, sizeof (struct symbol));
723   memset (sym, 0, sizeof (struct symbol));
724   SYMBOL_NAME (sym) = obsavestring (name, strlen (name),
725                                     &objfile->symbol_obstack);
726   SYMBOL_VALUE (sym) = 0;
727   SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
728
729   SYMBOL_CLASS (sym) = LOC_BLOCK;
730   return sym;
731 };
732
733 static struct type *decode_dst_type (struct objfile *, dst_rec_ptr_t);
734
735 static struct type *
736 decode_type_desc (struct objfile *objfile, dst_type_t *type_desc,
737                   dst_rec_ptr_t base)
738 {
739   struct type *type;
740   dst_rec_ptr_t entry;
741   if (type_desc->std_type.user_defined_type)
742     {
743       entry = (dst_rec_ptr_t) DST_OFFSET (base,
744                                           dst_user_type_offset (*type_desc));
745       type = decode_dst_type (objfile, entry);
746     }
747   else
748     {
749       switch (type_desc->std_type.dtc)
750         {
751         case dst_int8_type:
752           type = builtin_type_signed_char;
753           break;
754         case dst_int16_type:
755           type = builtin_type_short;
756           break;
757         case dst_int32_type:
758           type = builtin_type_long;
759           break;
760         case dst_uint8_type:
761           type = builtin_type_unsigned_char;
762           break;
763         case dst_uint16_type:
764           type = builtin_type_unsigned_short;
765           break;
766         case dst_uint32_type:
767           type = builtin_type_unsigned_long;
768           break;
769         case dst_real32_type:
770           type = builtin_type_float;
771           break;
772         case dst_real64_type:
773           type = builtin_type_double;
774           break;
775         case dst_complex_type:
776           type = builtin_type_complex;
777           break;
778         case dst_dcomplex_type:
779           type = builtin_type_double_complex;
780           break;
781         case dst_bool8_type:
782           type = builtin_type_char;
783           break;
784         case dst_bool16_type:
785           type = builtin_type_short;
786           break;
787         case dst_bool32_type:
788           type = builtin_type_long;
789           break;
790         case dst_char_type:
791           type = builtin_type_char;
792           break;
793           /* The next few are more complex. I will take care
794            * of them properly at a later point.
795            */
796         case dst_string_type:
797           type = builtin_type_void;
798           break;
799         case dst_ptr_type:
800           type = builtin_type_void;
801           break;
802         case dst_set_type:
803           type = builtin_type_void;
804           break;
805         case dst_proc_type:
806           type = builtin_type_void;
807           break;
808         case dst_func_type:
809           type = builtin_type_void;
810           break;
811           /* Back tto some ordinary ones */
812         case dst_void_type:
813           type = builtin_type_void;
814           break;
815         case dst_uchar_type:
816           type = builtin_type_unsigned_char;
817           break;
818         default:
819           type = builtin_type_void;
820           break;
821         }
822     }
823   return type;
824 }
825
826 struct structure_list
827 {
828   struct structure_list *next;
829   struct type *type;
830 };
831
832 static struct structure_list *struct_list = NULL;
833
834 static struct type *
835 find_dst_structure (char *name)
836 {
837   struct structure_list *element;
838
839   for (element = struct_list; element; element = element->next)
840     if (!strcmp (name, TYPE_NAME (element->type)))
841       return element->type;
842   return NULL;
843 }
844
845
846 static struct type *
847 decode_dst_structure (struct objfile *objfile, dst_rec_ptr_t entry, int code,
848                       int version)
849 {
850   struct type *type, *child_type;
851   char *struct_name;
852   char *name, *field_name;
853   int i;
854   int fieldoffset, fieldsize;
855   dst_type_t type_desc;
856   struct structure_list *element;
857
858   struct_name = DST_OFFSET (entry, DST_record (entry).noffset);
859   name = concat ((code == TYPE_CODE_UNION) ? "union " : "struct ",
860                  struct_name, NULL);
861   type = find_dst_structure (name);
862   if (type)
863     {
864       xfree (name);
865       return type;
866     }
867   type = create_new_type (objfile);
868   TYPE_NAME (type) = obstack_copy0 (&objfile->symbol_obstack,
869                                     name, strlen (name));
870   xfree (name);
871   TYPE_CODE (type) = code;
872   TYPE_LENGTH (type) = DST_record (entry).size;
873   TYPE_NFIELDS (type) = DST_record (entry).nfields;
874   TYPE_FIELDS (type) = (struct field *)
875     obstack_alloc (&objfile->symbol_obstack, sizeof (struct field) *
876                    DST_record (entry).nfields);
877   fieldoffset = fieldsize = 0;
878   INIT_CPLUS_SPECIFIC (type);
879   element = (struct structure_list *)
880     xmalloc (sizeof (struct structure_list));
881   element->type = type;
882   element->next = struct_list;
883   struct_list = element;
884   for (i = 0; i < DST_record (entry).nfields; i++)
885     {
886       switch (version)
887         {
888         case 2:
889           field_name = DST_OFFSET (entry,
890                                    DST_record (entry).f.ofields[i].noffset);
891           fieldoffset = DST_record (entry).f.ofields[i].foffset * 8 +
892             DST_record (entry).f.ofields[i].bit_offset;
893           fieldsize = DST_record (entry).f.ofields[i].size;
894           type_desc = DST_record (entry).f.ofields[i].type_desc;
895           break;
896         case 1:
897           field_name = DST_OFFSET (entry,
898                                    DST_record (entry).f.fields[i].noffset);
899           type_desc = DST_record (entry).f.fields[i].type_desc;
900           switch (DST_record (entry).f.fields[i].f.field_loc.format_tag)
901             {
902             case dst_field_byte:
903               fieldoffset = DST_record (entry).f.
904                 fields[i].f.field_byte.offset * 8;
905               fieldsize = -1;
906               break;
907             case dst_field_bit:
908               fieldoffset = DST_record (entry).f.
909                 fields[i].f.field_bit.byte_offset * 8 +
910                 DST_record (entry).f.
911                 fields[i].f.field_bit.bit_offset;
912               fieldsize = DST_record (entry).f.
913                 fields[i].f.field_bit.nbits;
914               break;
915             case dst_field_loc:
916               fieldoffset += fieldsize;
917               fieldsize = -1;
918               break;
919             }
920           break;
921         case 0:
922           field_name = DST_OFFSET (entry,
923                                    DST_record (entry).f.sfields[i].noffset);
924           fieldoffset = DST_record (entry).f.sfields[i].foffset;
925           type_desc = DST_record (entry).f.sfields[i].type_desc;
926           if (i < DST_record (entry).nfields - 1)
927             fieldsize = DST_record (entry).f.sfields[i + 1].foffset;
928           else
929             fieldsize = DST_record (entry).size;
930           fieldsize -= fieldoffset;
931           fieldoffset *= 8;
932           fieldsize *= 8;
933         }
934       TYPE_FIELDS (type)[i].name =
935         obstack_copy0 (&objfile->symbol_obstack,
936                        field_name, strlen (field_name));
937       TYPE_FIELDS (type)[i].type = decode_type_desc (objfile,
938                                                      &type_desc,
939                                                      entry);
940       if (fieldsize == -1)
941         fieldsize = TYPE_LENGTH (TYPE_FIELDS (type)[i].type) *
942           8;
943       TYPE_FIELDS (type)[i].bitsize = fieldsize;
944       TYPE_FIELDS (type)[i].bitpos = fieldoffset;
945     }
946   return type;
947 }
948
949 static struct type *
950 decode_dst_type (struct objfile *objfile, dst_rec_ptr_t entry)
951 {
952   struct type *child_type, *type, *range_type, *index_type;
953
954   switch (entry->rec_type)
955     {
956     case dst_typ_var:
957       return decode_type_desc (objfile,
958                                &DST_var (entry).type_desc,
959                                entry);
960       break;
961     case dst_typ_variable:
962       return decode_type_desc (objfile,
963                                &DST_variable (entry).type_desc,
964                                entry);
965       break;
966     case dst_typ_short_rec:
967       return decode_dst_structure (objfile, entry, TYPE_CODE_STRUCT, 0);
968     case dst_typ_short_union:
969       return decode_dst_structure (objfile, entry, TYPE_CODE_UNION, 0);
970     case dst_typ_union:
971       return decode_dst_structure (objfile, entry, TYPE_CODE_UNION, 1);
972     case dst_typ_record:
973       return decode_dst_structure (objfile, entry, TYPE_CODE_STRUCT, 1);
974     case dst_typ_old_union:
975       return decode_dst_structure (objfile, entry, TYPE_CODE_UNION, 2);
976     case dst_typ_old_record:
977       return decode_dst_structure (objfile, entry, TYPE_CODE_STRUCT, 2);
978     case dst_typ_pointer:
979       return make_pointer_type (
980                                  decode_type_desc (objfile,
981                                              &DST_pointer (entry).type_desc,
982                                                    entry),
983                                  NULL);
984     case dst_typ_array:
985       child_type = decode_type_desc (objfile,
986                                      &DST_pointer (entry).type_desc,
987                                      entry);
988       index_type = lookup_fundamental_type (objfile,
989                                             FT_INTEGER);
990       range_type = create_range_type ((struct type *) NULL,
991                                       index_type, DST_array (entry).lo_bound,
992                                       DST_array (entry).hi_bound);
993       return create_array_type ((struct type *) NULL, child_type,
994                                 range_type);
995     case dst_typ_alias:
996       return decode_type_desc (objfile,
997                                &DST_alias (entry).type_desc,
998                                entry);
999     default:
1000       return builtin_type_int;
1001     }
1002 }
1003
1004 struct symbol_list
1005 {
1006   struct symbol_list *next;
1007   struct symbol *symbol;
1008 };
1009
1010 static struct symbol_list *dst_global_symbols = NULL;
1011 static int total_globals = 0;
1012
1013 static void
1014 decode_dst_locstring (char *locstr, struct symbol *sym)
1015 {
1016   dst_loc_entry_t *entry, *next_entry;
1017   CORE_ADDR temp;
1018   int count = 0;
1019
1020   while (1)
1021     {
1022       if (count++ == 100)
1023         {
1024           fprintf_unfiltered (gdb_stderr, "Error reading locstring\n");
1025           break;
1026         }
1027       entry = (dst_loc_entry_t *) locstr;
1028       next_entry = (dst_loc_entry_t *) (locstr + 1);
1029       switch (entry->header.code)
1030         {
1031         case dst_lsc_end:       /* End of string */
1032           return;
1033         case dst_lsc_indirect:  /* Indirect through previous. Arg == 6 */
1034           /* Or register ax x == arg */
1035           if (entry->header.arg < 6)
1036             {
1037               SYMBOL_CLASS (sym) = LOC_REGISTER;
1038               SYMBOL_VALUE (sym) = entry->header.arg + 8;
1039             }
1040           /* We predict indirects */
1041           locstr++;
1042           break;
1043         case dst_lsc_dreg:
1044           SYMBOL_CLASS (sym) = LOC_REGISTER;
1045           SYMBOL_VALUE (sym) = entry->header.arg;
1046           locstr++;
1047           break;
1048         case dst_lsc_section:   /* Section (arg+1) */
1049           SYMBOL_VALUE (sym) = dst_get_addr (entry->header.arg + 1, 0);
1050           locstr++;
1051           break;
1052         case dst_lsc_sec_byte:  /* Section (next_byte+1) */
1053           SYMBOL_VALUE (sym) = dst_get_addr (locstr[1] + 1, 0);
1054           locstr += 2;
1055           break;
1056         case dst_lsc_add:       /* Add (arg+1)*2 */
1057         case dst_lsc_sub:       /* Subtract (arg+1)*2 */
1058           temp = (entry->header.arg + 1) * 2;
1059           locstr++;
1060           if (*locstr == dst_multiply_256)
1061             {
1062               temp <<= 8;
1063               locstr++;
1064             }
1065           switch (entry->header.code)
1066             {
1067             case dst_lsc_add:
1068               if (SYMBOL_CLASS (sym) == LOC_LOCAL)
1069                 SYMBOL_CLASS (sym) = LOC_ARG;
1070               SYMBOL_VALUE (sym) += temp;
1071               break;
1072             case dst_lsc_sub:
1073               SYMBOL_VALUE (sym) -= temp;
1074               break;
1075             }
1076           break;
1077         case dst_lsc_add_byte:
1078         case dst_lsc_sub_byte:
1079           switch (entry->header.arg & 0x03)
1080             {
1081             case 1:
1082               temp = (unsigned char) locstr[1];
1083               locstr += 2;
1084               break;
1085             case 2:
1086               temp = *(unsigned short *) (locstr + 1);
1087               locstr += 3;
1088               break;
1089             case 3:
1090               temp = *(unsigned long *) (locstr + 1);
1091               locstr += 5;
1092               break;
1093             }
1094           if (*locstr == dst_multiply_256)
1095             {
1096               temp <<= 8;
1097               locstr++;
1098             }
1099           switch (entry->header.code)
1100             {
1101             case dst_lsc_add_byte:
1102               if (SYMBOL_CLASS (sym) == LOC_LOCAL)
1103                 SYMBOL_CLASS (sym) = LOC_ARG;
1104               SYMBOL_VALUE (sym) += temp;
1105               break;
1106             case dst_lsc_sub_byte:
1107               SYMBOL_VALUE (sym) -= temp;
1108               break;
1109             }
1110           break;
1111         case dst_lsc_sbreg:     /* Stack base register (frame pointer). Arg==0 */
1112           if (next_entry->header.code != dst_lsc_indirect)
1113             {
1114               SYMBOL_VALUE (sym) = 0;
1115               SYMBOL_CLASS (sym) = LOC_STATIC;
1116               return;
1117             }
1118           SYMBOL_VALUE (sym) = 0;
1119           SYMBOL_CLASS (sym) = LOC_LOCAL;
1120           locstr++;
1121           break;
1122         default:
1123           SYMBOL_VALUE (sym) = 0;
1124           SYMBOL_CLASS (sym) = LOC_STATIC;
1125           return;
1126         }
1127     }
1128 }
1129
1130 static struct symbol_list *
1131 process_dst_symbols (struct objfile *objfile, dst_rec_ptr_t entry, char *name,
1132                      int *nsyms_ret)
1133 {
1134   struct symbol_list *list = NULL, *element;
1135   struct symbol *sym;
1136   char *symname;
1137   int nsyms = 0;
1138   char *location;
1139   long line;
1140   dst_type_t symtype;
1141   struct type *type;
1142   dst_var_attr_t attr;
1143   dst_var_loc_t loc_type;
1144   unsigned loc_index;
1145   long loc_value;
1146
1147   if (!entry)
1148     {
1149       *nsyms_ret = 0;
1150       return NULL;
1151     }
1152   location = (char *) entry;
1153   while (NEXT_SYM (&location, &entry) &&
1154          entry->rec_type != dst_typ_end_scope)
1155     {
1156       if (entry->rec_type == dst_typ_var)
1157         {
1158           if (DST_var (entry).short_locs)
1159             {
1160               loc_type = DST_var (entry).locs.shorts[0].loc_type;
1161               loc_index = DST_var (entry).locs.shorts[0].loc_index;
1162               loc_value = DST_var (entry).locs.shorts[0].location;
1163             }
1164           else
1165             {
1166               loc_type = DST_var (entry).locs.longs[0].loc_type;
1167               loc_index = DST_var (entry).locs.longs[0].loc_index;
1168               loc_value = DST_var (entry).locs.longs[0].location;
1169             }
1170           if (loc_type == dst_var_loc_external)
1171             continue;
1172           symname = DST_OFFSET (entry, DST_var (entry).noffset);
1173           line = DST_var (entry).src_loc.line_number;
1174           symtype = DST_var (entry).type_desc;
1175           attr = DST_var (entry).attributes;
1176         }
1177       else if (entry->rec_type == dst_typ_variable)
1178         {
1179           symname = DST_OFFSET (entry,
1180                                 DST_variable (entry).noffset);
1181           line = DST_variable (entry).src_loc.line_number;
1182           symtype = DST_variable (entry).type_desc;
1183           attr = DST_variable (entry).attributes;
1184         }
1185       else
1186         {
1187           continue;
1188         }
1189       if (symname && name && !strcmp (symname, name))
1190         /* It's the function return value */
1191         continue;
1192       sym = create_new_symbol (objfile, symname);
1193
1194       if ((attr & (1 << dst_var_attr_global)) ||
1195           (attr & (1 << dst_var_attr_static)))
1196         SYMBOL_CLASS (sym) = LOC_STATIC;
1197       else
1198         SYMBOL_CLASS (sym) = LOC_LOCAL;
1199       SYMBOL_LINE (sym) = line;
1200       SYMBOL_TYPE (sym) = decode_type_desc (objfile, &symtype,
1201                                             entry);
1202       SYMBOL_VALUE (sym) = 0;
1203       switch (entry->rec_type)
1204         {
1205         case dst_typ_var:
1206           switch (loc_type)
1207             {
1208             case dst_var_loc_abs:
1209               SYMBOL_VALUE_ADDRESS (sym) = loc_value;
1210               break;
1211             case dst_var_loc_sect_off:
1212             case dst_var_loc_ind_sect_off:      /* What is this? */
1213               SYMBOL_VALUE_ADDRESS (sym) = dst_get_addr (
1214                                                           loc_index,
1215                                                           loc_value);
1216               break;
1217             case dst_var_loc_ind_reg_rel:       /* What is this? */
1218             case dst_var_loc_reg_rel:
1219               /* If it isn't fp relative, specify the
1220                * register it's relative to.
1221                */
1222               if (loc_index)
1223                 {
1224                   sym->aux_value.basereg = loc_index;
1225                 }
1226               SYMBOL_VALUE (sym) = loc_value;
1227               if (loc_value > 0 &&
1228                   SYMBOL_CLASS (sym) == LOC_BASEREG)
1229                 SYMBOL_CLASS (sym) = LOC_BASEREG_ARG;
1230               break;
1231             case dst_var_loc_reg:
1232               SYMBOL_VALUE (sym) = loc_index;
1233               SYMBOL_CLASS (sym) = LOC_REGISTER;
1234               break;
1235             }
1236           break;
1237         case dst_typ_variable:
1238           /* External variable..... don't try to interpret
1239            * its nonexistant locstring.
1240            */
1241           if (DST_variable (entry).loffset == -1)
1242             continue;
1243           decode_dst_locstring (DST_OFFSET (entry,
1244                                             DST_variable (entry).loffset),
1245                                 sym);
1246         }
1247       element = (struct symbol_list *)
1248         xmalloc (sizeof (struct symbol_list));
1249
1250       if (attr & (1 << dst_var_attr_global))
1251         {
1252           element->next = dst_global_symbols;
1253           dst_global_symbols = element;
1254           total_globals++;
1255         }
1256       else
1257         {
1258           element->next = list;
1259           list = element;
1260           nsyms++;
1261         }
1262       element->symbol = sym;
1263     }
1264   *nsyms_ret = nsyms;
1265   return list;
1266 }
1267
1268
1269 static struct symbol *
1270 process_dst_function (struct objfile *objfile, dst_rec_ptr_t entry, char *name,
1271                       CORE_ADDR address)
1272 {
1273   struct symbol *sym;
1274   struct type *type, *ftype;
1275   dst_rec_ptr_t sym_entry, typ_entry;
1276   char *location;
1277   struct symbol_list *element;
1278
1279   type = builtin_type_int;
1280   sym = create_new_symbol (objfile, name);
1281   SYMBOL_CLASS (sym) = LOC_BLOCK;
1282
1283   if (entry)
1284     {
1285       location = (char *) entry;
1286       do
1287         {
1288           NEXT_SYM (&location, &sym_entry);
1289         }
1290       while (sym_entry && sym_entry->rec_type != dst_typ_signature);
1291
1292       if (sym_entry)
1293         {
1294           SYMBOL_LINE (sym) =
1295             DST_signature (sym_entry).src_loc.line_number;
1296           if (DST_signature (sym_entry).result)
1297             {
1298               typ_entry = (dst_rec_ptr_t)
1299                 DST_OFFSET (sym_entry,
1300                             DST_signature (sym_entry).result);
1301               type = decode_dst_type (objfile, typ_entry);
1302             }
1303         }
1304     }
1305
1306   if (!type->function_type)
1307     {
1308       ftype = create_new_type (objfile);
1309       type->function_type = ftype;
1310       ftype->target_type = type;
1311       ftype->code = TYPE_CODE_FUNC;
1312     }
1313   SYMBOL_TYPE (sym) = type->function_type;
1314
1315   /* Now add ourselves to the global symbols list */
1316   element = (struct symbol_list *)
1317     xmalloc (sizeof (struct symbol_list));
1318
1319   element->next = dst_global_symbols;
1320   dst_global_symbols = element;
1321   total_globals++;
1322   element->symbol = sym;
1323
1324   return sym;
1325 }
1326
1327 static struct block *
1328 process_dst_block (struct objfile *objfile, dst_rec_ptr_t entry)
1329 {
1330   struct block *block;
1331   struct symbol *function = NULL;
1332   CORE_ADDR address;
1333   long size;
1334   char *name;
1335   dst_rec_ptr_t child_entry, symbol_entry;
1336   struct block *child_block;
1337   int total_symbols = 0;
1338   char fake_name[20];
1339   static long fake_seq = 0;
1340   struct symbol_list *symlist, *nextsym;
1341   int symnum;
1342
1343   if (DST_block (entry).noffset)
1344     name = DST_OFFSET (entry, DST_block (entry).noffset);
1345   else
1346     name = NULL;
1347   if (DST_block (entry).n_of_code_ranges)
1348     {
1349       address = dst_sym_addr (
1350                                &DST_block (entry).code_ranges[0].code_start);
1351       size = DST_block (entry).code_ranges[0].code_size;
1352     }
1353   else
1354     {
1355       address = -1;
1356       size = 0;
1357     }
1358   symbol_entry = (dst_rec_ptr_t) get_sec_ref (&DST_block (entry).symbols_start);
1359   switch (DST_block (entry).block_type)
1360     {
1361       /* These are all really functions. Even the "program" type.
1362        * This is because the Apollo OS was written in Pascal, and
1363        * in Pascal, the main procedure is described as the Program.
1364        * Cute, huh?
1365        */
1366     case dst_block_procedure:
1367     case dst_block_function:
1368     case dst_block_subroutine:
1369     case dst_block_program:
1370       prim_record_minimal_symbol (name, address, mst_text, objfile);
1371       function = process_dst_function (
1372                                         objfile,
1373                                         symbol_entry,
1374                                         name,
1375                                         address);
1376       enter_all_lines (get_sec_ref (&DST_block (entry).code_ranges[0].lines_start), address);
1377       break;
1378     case dst_block_block_data:
1379       break;
1380
1381     default:
1382       /* GDB has to call it something, and the module name
1383        * won't cut it
1384        */
1385       sprintf (fake_name, "block_%08lx", fake_seq++);
1386       function = process_dst_function (
1387                                         objfile, NULL, fake_name, address);
1388       break;
1389     }
1390   symlist = process_dst_symbols (objfile, symbol_entry,
1391                                  name, &total_symbols);
1392   block = (struct block *)
1393     obstack_alloc (&objfile->symbol_obstack,
1394                    sizeof (struct block) +
1395                      (total_symbols - 1) * sizeof (struct symbol *));
1396
1397   symnum = 0;
1398   while (symlist)
1399     {
1400       nextsym = symlist->next;
1401
1402       block->sym[symnum] = symlist->symbol;
1403
1404       xfree (symlist);
1405       symlist = nextsym;
1406       symnum++;
1407     }
1408   BLOCK_NSYMS (block) = total_symbols;
1409   BLOCK_START (block) = address;
1410   BLOCK_END (block) = address + size;
1411   BLOCK_SUPERBLOCK (block) = 0;
1412   if (function)
1413     {
1414       SYMBOL_BLOCK_VALUE (function) = block;
1415       BLOCK_FUNCTION (block) = function;
1416     }
1417   else
1418     BLOCK_FUNCTION (block) = 0;
1419
1420   if (DST_block (entry).child_block_off)
1421     {
1422       child_entry = (dst_rec_ptr_t) DST_OFFSET (entry,
1423                                          DST_block (entry).child_block_off);
1424       while (child_entry)
1425         {
1426           child_block = process_dst_block (objfile, child_entry);
1427           if (child_block)
1428             {
1429               if (BLOCK_START (child_block) <
1430                   BLOCK_START (block) ||
1431                   BLOCK_START (block) == -1)
1432                 BLOCK_START (block) =
1433                   BLOCK_START (child_block);
1434               if (BLOCK_END (child_block) >
1435                   BLOCK_END (block) ||
1436                   BLOCK_END (block) == -1)
1437                 BLOCK_END (block) =
1438                   BLOCK_END (child_block);
1439               BLOCK_SUPERBLOCK (child_block) = block;
1440             }
1441           if (DST_block (child_entry).sibling_block_off)
1442             child_entry = (dst_rec_ptr_t) DST_OFFSET (
1443                                                        child_entry,
1444                                  DST_block (child_entry).sibling_block_off);
1445           else
1446             child_entry = NULL;
1447         }
1448     }
1449   record_pending_block (objfile, block, NULL);
1450   return block;
1451 }
1452
1453
1454 static void
1455 read_dst_symtab (struct objfile *objfile)
1456 {
1457   char *buffer;
1458   dst_rec_ptr_t entry, file_table, root_block;
1459   char *source_file;
1460   struct block *block, *global_block;
1461   int symnum;
1462   struct symbol_list *nextsym;
1463   int module_num = 0;
1464   struct structure_list *element;
1465
1466   current_objfile = objfile;
1467   buffer = blocks_info.buffer;
1468   while (NEXT_BLK (&buffer, &entry))
1469     {
1470       if (entry->rec_type == dst_typ_comp_unit)
1471         {
1472           file_table = (dst_rec_ptr_t) DST_OFFSET (entry,
1473                                           DST_comp_unit (entry).file_table);
1474           section_table = (dst_rec_ptr_t) DST_OFFSET (entry,
1475                                        DST_comp_unit (entry).section_table);
1476           root_block = (dst_rec_ptr_t) DST_OFFSET (entry,
1477                                    DST_comp_unit (entry).root_block_offset);
1478           source_file = DST_OFFSET (file_table,
1479                                 DST_file_tab (file_table).files[0].noffset);
1480           /* Point buffer to the start of the next comp_unit */
1481           buffer = DST_OFFSET (entry,
1482                                DST_comp_unit (entry).data_size);
1483           dst_start_symtab ();
1484
1485           block = process_dst_block (objfile, root_block);
1486
1487           global_block = (struct block *)
1488             obstack_alloc (&objfile->symbol_obstack,
1489                            sizeof (struct block) +
1490                              (total_globals - 1) *
1491                            sizeof (struct symbol *));
1492           BLOCK_NSYMS (global_block) = total_globals;
1493           for (symnum = 0; symnum < total_globals; symnum++)
1494             {
1495               nextsym = dst_global_symbols->next;
1496
1497               global_block->sym[symnum] =
1498                 dst_global_symbols->symbol;
1499
1500               xfree (dst_global_symbols);
1501               dst_global_symbols = nextsym;
1502             }
1503           dst_global_symbols = NULL;
1504           total_globals = 0;
1505           BLOCK_FUNCTION (global_block) = 0;
1506           BLOCK_START (global_block) = BLOCK_START (block);
1507           BLOCK_END (global_block) = BLOCK_END (block);
1508           BLOCK_SUPERBLOCK (global_block) = 0;
1509           BLOCK_SUPERBLOCK (block) = global_block;
1510           record_pending_block (objfile, global_block, NULL);
1511
1512           complete_symtab (source_file,
1513                            BLOCK_START (block),
1514                            BLOCK_END (block) - BLOCK_START (block));
1515           module_num++;
1516           dst_end_symtab (objfile);
1517         }
1518     }
1519   if (module_num)
1520     prim_record_minimal_symbol ("<end_of_program>",
1521                                 BLOCK_END (block), mst_text, objfile);
1522   /* One more faked symbol to make sure nothing can ever run off the
1523    * end of the symbol table. This one represents the end of the
1524    * text space. It used to be (CORE_ADDR) -1 (effectively the highest
1525    * int possible), but some parts of gdb treated it as a signed
1526    * number and failed comparisons. We could equally use 7fffffff,
1527    * but no functions are ever mapped to an address higher than
1528    * 40000000
1529    */
1530   prim_record_minimal_symbol ("<end_of_text>",
1531                               (CORE_ADDR) 0x40000000,
1532                               mst_text, objfile);
1533   while (struct_list)
1534     {
1535       element = struct_list;
1536       struct_list = element->next;
1537       xfree (element);
1538     }
1539 }
1540 \f
1541
1542 /* Support for line number handling */
1543 static char *linetab = NULL;
1544 static long linetab_offset;
1545 static unsigned long linetab_size;
1546
1547 /* Read in all the line numbers for fast lookups later.  Leave them in
1548    external (unswapped) format in memory; we'll swap them as we enter
1549    them into GDB's data structures.  */
1550 static int
1551 init_one_section (int chan, dst_sec *secinfo)
1552 {
1553   if (secinfo->size == 0
1554       || lseek (chan, secinfo->position, 0) == -1
1555       || (secinfo->buffer = xmalloc (secinfo->size)) == NULL
1556       || myread (chan, secinfo->buffer, secinfo->size) == -1)
1557     return 0;
1558   else
1559     return 1;
1560 }
1561
1562 static int
1563 init_dst_sections (int chan)
1564 {
1565
1566   if (!init_one_section (chan, &blocks_info) ||
1567       !init_one_section (chan, &lines_info) ||
1568       !init_one_section (chan, &symbols_info))
1569     return -1;
1570   else
1571     return 0;
1572 }
1573
1574 /* Fake up support for relocating symbol addresses.  FIXME.  */
1575
1576 struct section_offsets dst_symfile_faker =
1577 {0};
1578
1579 void
1580 dst_symfile_offsets (struct objfile *objfile, struct section_addr_info *addrs)
1581 {
1582   objfile->num_sections = 1;
1583   objfile->section_offsets = &dst_symfile_faker;
1584 }
1585
1586 /* Register our ability to parse symbols for DST BFD files */
1587
1588 static struct sym_fns dst_sym_fns =
1589 {
1590   /* FIXME: Can this be integrated with coffread.c?  If not, should it be
1591      a separate flavour like ecoff?  */
1592   (enum bfd_flavour) -2,
1593
1594   dst_new_init,                 /* sym_new_init: init anything gbl to entire symtab */
1595   dst_symfile_init,             /* sym_init: read initial info, setup for sym_read() */
1596   dst_symfile_read,             /* sym_read: read a symbol file into symtab */
1597   dst_symfile_finish,           /* sym_finish: finished with file, cleanup */
1598   dst_symfile_offsets,          /* sym_offsets:  xlate external to internal form */
1599   NULL                          /* next: pointer to next struct sym_fns */
1600 };
1601
1602 void
1603 _initialize_dstread (void)
1604 {
1605   add_symtab_fns (&dst_sym_fns);
1606 }