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