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