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