Add C++ as a separate language.
[platform/upstream/binutils.git] / gdb / dwarfread.c
1 /* DWARF debugging format support for GDB.
2    Copyright (C) 1991 Free Software Foundation, Inc.
3    Written by Fred Fish at Cygnus Support, portions based on dbxread.c,
4    mipsread.c, coffread.c, and dwarfread.c from a Data General SVR4 gdb port.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
21
22 /*
23
24 FIXME: Figure out how to get the frame pointer register number in the
25 execution environment of the target.  Remove R_FP kludge
26
27 FIXME: Add generation of dependencies list to partial symtab code.
28
29 FIXME: Currently we ignore host/target byte ordering and integer size
30 differences.  Should remap data from external form to an internal form
31 before trying to use it.
32
33 FIXME: Resolve minor differences between what information we put in the
34 partial symbol table and what dbxread puts in.  For example, we don't yet
35 put enum constants there.  And dbxread seems to invent a lot of typedefs
36 we never see.  Use the new printpsym command to see the partial symbol table
37 contents.
38
39 FIXME: Change forward declarations of static functions to allow for compilers
40 without prototypes.
41
42 FIXME: Figure out a better way to tell gdb (all the debug reading routines)
43 the names of the gccX_compiled flags.
44
45 FIXME: Figure out a better way to tell gdb about the name of the function
46 contain the user's entry point (I.E. main())
47
48 FIXME: The current DWARF specification has a very strong bias towards
49 machines with 32-bit integers, as it assumes that many attributes of the
50 program (such as an address) will fit in such an integer.  There are many
51 references in the spec to things that are 2, 4, or 8 bytes long.  Given that
52 we will probably run into problems on machines where some of these assumptions
53 are invalid (64-bit ints for example), we don't bother at this time to try to
54 make this code more flexible and just use shorts, ints, and longs (and their
55 sizes) where it seems appropriate.  I.E. we use a short int to hold DWARF
56 tags, and assume that the tag size in the file is the same as sizeof(short).
57
58 FIXME: Figure out how to get the name of the symbol indicating that a module
59 has been compiled with gcc (gcc_compiledXX) in a more portable way than
60 hardcoding it into the object file readers.
61
62 FIXME: See other FIXME's and "ifdef 0" scattered throughout the code for
63 other things to work on, if you get bored. :-)
64
65 */
66
67 #include <stdio.h>
68 #include <varargs.h>
69 #include <fcntl.h>
70
71 #include "defs.h"
72 #include "param.h"
73 #include "bfd.h"
74 #include "symtab.h"
75 #include "symfile.h"
76 #include "dwarf.h"
77 #include "ansidecl.h"
78
79 #ifdef MAINTENANCE      /* Define to 1 to compile in some maintenance stuff */
80 #define SQUAWK(stuff) dwarfwarn stuff
81 #else
82 #define SQUAWK(stuff)
83 #endif
84
85 #ifndef R_FP            /* FIXME */
86 #define R_FP 14         /* Kludge to get frame pointer register number */
87 #endif
88
89 typedef unsigned int DIEREF;    /* Reference to a DIE */
90
91 #define GCC_COMPILED_FLAG_SYMBOL "gcc_compiled%"        /* FIXME */
92 #define GCC2_COMPILED_FLAG_SYMBOL "gcc2_compiled%"      /* FIXME */
93
94 #define STREQ(a,b)              (strcmp(a,b)==0)
95
96 extern CORE_ADDR startup_file_start;    /* From blockframe.c */
97 extern CORE_ADDR startup_file_end;      /* From blockframe.c */
98 extern CORE_ADDR entry_scope_lowpc;     /* From blockframe.c */
99 extern CORE_ADDR entry_scope_highpc;    /* From blockframc.c */
100 extern CORE_ADDR main_scope_lowpc;      /* From blockframe.c */
101 extern CORE_ADDR main_scope_highpc;     /* From blockframc.c */
102 extern int info_verbose;                /* From main.c; nonzero => verbose */
103
104
105 /* The DWARF debugging information consists of two major pieces,
106    one is a block of DWARF Information Entries (DIE's) and the other
107    is a line number table.  The "struct dieinfo" structure contains
108    the information for a single DIE, the one currently being processed.
109
110    In order to make it easier to randomly access the attribute fields
111    of the current DIE, which are specifically unordered within the DIE
112    each DIE is scanned and an instance of the "struct dieinfo"
113    structure is initialized.
114
115    Initialization is done in two levels.  The first, done by basicdieinfo(),
116    just initializes those fields that are vital to deciding whether or not
117    to use this DIE, how to skip past it, etc.  The second, done by the
118    function completedieinfo(), fills in the rest of the information.
119
120    Attributes which have block forms are not interpreted at the time
121    the DIE is scanned, instead we just save pointers to the start
122    of their value fields.
123
124    Some fields have a flag <name>_p that is set when the value of the
125    field is valid (I.E. we found a matching attribute in the DIE).  Since
126    we may want to test for the presence of some attributes in the DIE,
127    such as AT_is_external, without restricting the values of the field,
128    we need someway to note that we found such an attribute.
129    
130  */
131    
132 typedef char BLOCK;
133
134 struct dieinfo {
135   char *        die;                    /* Pointer to the raw DIE data */
136   long          dielength;              /* Length of the raw DIE data */
137   DIEREF        dieref;                 /* Offset of this DIE */
138   short         dietag;                 /* Tag for this DIE */
139   long          at_padding;
140   long          at_sibling;
141   BLOCK *       at_location;
142   char *        at_name;
143   unsigned short at_fund_type;
144   BLOCK *       at_mod_fund_type;
145   long          at_user_def_type;
146   BLOCK *       at_mod_u_d_type;
147   short         at_ordering;
148   BLOCK *       at_subscr_data;
149   long          at_byte_size;
150   short         at_bit_offset;
151   long          at_bit_size;
152   BLOCK *       at_deriv_list;
153   BLOCK *       at_element_list;
154   long          at_stmt_list;
155   long          at_low_pc;
156   long          at_high_pc;
157   long          at_language;
158   long          at_member;
159   long          at_discr;
160   BLOCK *       at_discr_value;
161   short         at_visibility;
162   long          at_import;
163   BLOCK *       at_string_length;
164   char *        at_comp_dir;
165   char *        at_producer;
166   long          at_loclist;
167   long          at_frame_base;
168   short         at_incomplete;
169   long          at_start_scope;
170   long          at_stride_size;
171   long          at_src_info;
172   short         at_prototyped;
173   BLOCK *       at_const_data;
174   short         at_is_external;
175   unsigned int  at_is_external_p:1;
176   unsigned int  at_stmt_list_p:1;
177 };
178
179 static int diecount;    /* Approximate count of dies for compilation unit */
180 static struct dieinfo *curdie;  /* For warnings and such */
181
182 static char *dbbase;    /* Base pointer to dwarf info */
183 static int dbroff;      /* Relative offset from start of .debug section */
184 static char *lnbase;    /* Base pointer to line section */
185 static int isreg;       /* Kludge to identify register variables */
186
187 static CORE_ADDR baseaddr;      /* Add to each symbol value */
188
189 /* Each partial symbol table entry contains a pointer to private data for the
190    read_symtab() function to use when expanding a partial symbol table entry
191    to a full symbol table entry.  For DWARF debugging info, this data is
192    contained in the following structure and macros are provided for easy
193    access to the members given a pointer to a partial symbol table entry.
194
195    dbfoff       Always the absolute file offset to the start of the ".debug"
196                 section for the file containing the DIE's being accessed.
197
198    dbroff       Relative offset from the start of the ".debug" access to the
199                 first DIE to be accessed.  When building the partial symbol
200                 table, this value will be zero since we are accessing the
201                 entire ".debug" section.  When expanding a partial symbol
202                 table entry, this value will be the offset to the first
203                 DIE for the compilation unit containing the symbol that
204                 triggers the expansion.
205
206    dblength     The size of the chunk of DIE's being examined, in bytes.
207
208    lnfoff       The absolute file offset to the line table fragment.  Ignored
209                 when building partial symbol tables, but used when expanding
210                 them, and contains the absolute file offset to the fragment
211                 of the ".line" section containing the line numbers for the
212                 current compilation unit.
213  */
214
215 struct dwfinfo {
216   int dbfoff;           /* Absolute file offset to start of .debug section */
217   int dbroff;           /* Relative offset from start of .debug section */
218   int dblength;         /* Size of the chunk of DIE's being examined */
219   int lnfoff;           /* Absolute file offset to line table fragment */
220 };
221
222 #define DBFOFF(p) (((struct dwfinfo *)((p)->read_symtab_private))->dbfoff)
223 #define DBROFF(p) (((struct dwfinfo *)((p)->read_symtab_private))->dbroff)
224 #define DBLENGTH(p) (((struct dwfinfo *)((p)->read_symtab_private))->dblength)
225 #define LNFOFF(p) (((struct dwfinfo *)((p)->read_symtab_private))->lnfoff)
226
227 /*  Record the symbols defined for each context in a linked list.  We don't
228     create a struct block for the context until we know how long to make it.
229     Global symbols for each file are maintained in the global_symbols list. */
230
231 struct pending_symbol {
232   struct pending_symbol *next;          /* Next pending symbol */
233   struct symbol *symbol;                /* The actual symbol */
234 };
235
236 static struct pending_symbol *global_symbols;   /* global funcs and vars */
237 static struct block *global_symbol_block;
238
239 /*  Line number entries are read into a dynamically expandable vector before
240     being added to the symbol table section.  Once we know how many there are
241     we can add them. */
242
243 static struct linetable *line_vector;   /* Vector of line numbers. */
244 static int line_vector_index;           /* Index of next entry.  */
245 static int line_vector_length;          /* Current allocation limit */
246
247 /* Scope information is kept in a scope tree, one node per scope.  Each time
248    a new scope is started, a child node is created under the current node
249    and set to the current scope.  Each time a scope is closed, the current
250    scope moves back up the tree to the parent of the current scope.
251
252    Each scope contains a pointer to the list of symbols defined in the scope,
253    a pointer to the block vector for the scope, a pointer to the symbol
254    that names the scope (if any), and the range of PC values that mark
255    the start and end of the scope.  */
256
257 struct scopenode {
258     struct scopenode *parent;
259     struct scopenode *child;
260     struct scopenode *sibling;
261     struct pending_symbol *symbols;
262     struct block *block;
263     struct symbol *namesym;
264     CORE_ADDR lowpc;
265     CORE_ADDR highpc;
266 };
267
268 static struct scopenode *scopetree;
269 static struct scopenode *scope;
270
271 /* DIES which have user defined types or modified user defined types refer to
272    other DIES for the type information.  Thus we need to associate the offset
273    of a DIE for a user defined type with a pointer to the type information.
274
275    Originally this was done using a simple but expensive algorithm, with an
276    array of unsorted structures, each containing an offset/type-pointer pair.
277    This array was scanned linearly each time a lookup was done.  The result
278    was that gdb was spending over half it's startup time munging through this
279    array of pointers looking for a structure that had the right offset member.
280
281    The second attempt used the same array of structures, but the array was
282    sorted using qsort each time a new offset/type was recorded, and a binary
283    search was used to find the type pointer for a given DIE offset.  This was
284    even slower, due to the overhead of sorting the array each time a new
285    offset/type pair was entered.
286
287    The third attempt uses a fixed size array of type pointers, indexed by a
288    value derived from the DIE offset.  Since the minimum DIE size is 4 bytes,
289    we can divide any DIE offset by 4 to obtain a unique index into this fixed
290    size array.  Since each element is a 4 byte pointer, it takes exactly as
291    much memory to hold this array as to hold the DWARF info for a given
292    compilation unit.  But it gets freed as soon as we are done with it. */
293
294 static struct type **utypes;    /* Pointer to array of user type pointers */
295 static int numutypes;           /* Max number of user type pointers */
296
297 /* Forward declarations of static functions so we don't have to worry
298    about ordering within this file.  The EXFUN macro may be slightly
299    misleading.  Should probably be called DCLFUN instead, or something
300    more intuitive, since it can be used for both static and external
301    definitions. */
302
303 static void dwarfwarn ();       /* EXFUN breaks with <varargs.h> (FIXME)*/
304
305 static void
306 EXFUN (scan_partial_symbols, (char *thisdie AND char *enddie));
307
308 static void
309 EXFUN (scan_compilation_units,
310        (char *filename AND CORE_ADDR addr AND char *thisdie AND char *enddie
311         AND unsigned int dbfoff AND unsigned int lnoffset));
312
313 static struct partial_symtab *
314 EXFUN(start_psymtab, (char *symfile_name AND CORE_ADDR addr
315                       AND char *filename AND CORE_ADDR textlow
316                       AND CORE_ADDR texthigh AND int dbfoff
317                       AND int curoff AND int culength AND int lnfoff
318                       AND struct partial_symbol *global_syms
319                       AND struct partial_symbol *static_syms));
320 static void
321 EXFUN(add_partial_symbol, (struct dieinfo *dip));
322
323 static void
324 EXFUN(add_psymbol_to_list,
325       (struct psymbol_allocation_list *listp AND char *name
326       AND enum namespace space AND enum address_class class
327       AND CORE_ADDR value));
328
329 static void
330 EXFUN(init_psymbol_list, (int total_symbols));
331
332 static void
333 EXFUN(basicdieinfo, (struct dieinfo *dip AND char *diep));
334
335 static void
336 EXFUN(completedieinfo, (struct dieinfo *dip));
337
338 static void
339 EXFUN(dwarf_psymtab_to_symtab, (struct partial_symtab *pst));
340
341 static void
342 EXFUN(psymtab_to_symtab_1, (struct partial_symtab *pst AND int desc ));
343
344 static struct symtab *
345 EXFUN(read_ofile_symtab, (struct partial_symtab *pst AND int desc));
346
347 static void
348 EXFUN(process_dies, (char *thisdie AND char *enddie));
349
350 static void
351 EXFUN(read_lexical_block_scope,
352       (struct dieinfo *dip AND char *thisdie AND char *enddie));
353
354 static void
355 EXFUN(read_structure_scope,
356      (struct dieinfo *dip AND char *thisdie AND char *enddie));
357
358 static struct type *
359 EXFUN(decode_array_element_type, (char *scan AND char *end));
360
361 static struct type *
362 EXFUN(decode_subscr_data, (char *scan AND char *end));
363
364 static void
365 EXFUN(read_array_type, (struct dieinfo *dip));
366
367 static void
368 EXFUN(read_subroutine_type,
369      (struct dieinfo *dip AND char *thisdie AND char *enddie));
370
371 static void
372 EXFUN(read_enumeration,
373      (struct dieinfo *dip AND char *thisdie AND char *enddie));
374
375 static struct type *
376 EXFUN(struct_type,
377       (struct dieinfo *dip AND char *thisdie AND char *enddie));
378
379 static struct type *
380 EXFUN(enum_type, (struct dieinfo *dip));
381
382 static void
383 EXFUN(read_func_scope,
384       (struct dieinfo *dip AND char *thisdie AND char *enddie));
385
386 static void
387 EXFUN(read_file_scope,
388       (struct dieinfo *dip AND char *thisdie AND char *enddie));
389
390 static void
391 EXFUN(start_symtab, (void));
392
393 static void
394 EXFUN(end_symtab, (char *filename AND long language));
395
396 static int
397 EXFUN(scopecount, (struct scopenode *node));
398
399 static void
400 EXFUN(openscope,
401       (struct symbol *namesym AND CORE_ADDR lowpc AND CORE_ADDR highpc));
402
403 static void
404 EXFUN(freescope, (struct scopenode *node));
405
406 static struct block *
407 EXFUN(buildblock, (struct pending_symbol *syms));
408
409 static void
410 EXFUN(closescope, (void));
411
412 static void
413 EXFUN(record_line, (int line AND CORE_ADDR pc));
414
415 static void
416 EXFUN(decode_line_numbers, (char *linetable));
417
418 static struct type *
419 EXFUN(decode_die_type, (struct dieinfo *dip));
420
421 static struct type *
422 EXFUN(decode_mod_fund_type, (char *typedata));
423
424 static struct type *
425 EXFUN(decode_mod_u_d_type, (char *typedata));
426
427 static struct type *
428 EXFUN(decode_modified_type,
429       (unsigned char *modifiers AND unsigned short modcount AND int mtype));
430
431 static struct type *
432 EXFUN(decode_fund_type, (unsigned short fundtype));
433
434 static char *
435 EXFUN(create_name, (char *name AND struct obstack *obstackp));
436
437 static void
438 EXFUN(add_symbol_to_list,
439       (struct symbol *symbol AND struct pending_symbol **listhead));
440
441 static struct block **
442 EXFUN(gatherblocks, (struct block **dest AND struct scopenode *node));
443
444 static struct blockvector *
445 EXFUN(make_blockvector, (void));
446
447 static struct type *
448 EXFUN(lookup_utype, (DIEREF dieref));
449
450 static struct type *
451 EXFUN(alloc_utype, (DIEREF dieref AND struct type *usetype));
452
453 static struct symbol *
454 EXFUN(new_symbol, (struct dieinfo *dip));
455
456 static int
457 EXFUN(locval, (char *loc));
458
459 static void
460 EXFUN(record_misc_function, (char *name AND CORE_ADDR address));
461
462 static int
463 EXFUN(compare_psymbols,
464       (struct partial_symbol *s1 AND struct partial_symbol *s2));
465
466
467 /*
468
469 GLOBAL FUNCTION
470
471         dwarf_build_psymtabs -- build partial symtabs from DWARF debug info
472
473 SYNOPSIS
474
475         void dwarf_build_psymtabs (int desc, char *filename, CORE_ADDR addr,
476              int mainline, unsigned int dbfoff, unsigned int dbsize,
477              unsigned int lnoffset, unsigned int lnsize)
478
479 DESCRIPTION
480
481         This function is called upon to build partial symtabs from files
482         containing DIE's (Dwarf Information Entries) and DWARF line numbers.
483
484         It is passed a file descriptor for an open file containing the DIES
485         and line number information, the corresponding filename for that
486         file, a base address for relocating the symbols, a flag indicating
487         whether or not this debugging information is from a "main symbol
488         table" rather than a shared library or dynamically linked file,
489         and file offset/size pairs for the DIE information and line number
490         information.
491
492 RETURNS
493
494         No return value.
495
496  */
497
498 void
499 DEFUN(dwarf_build_psymtabs,
500       (desc, filename, addr, mainline, dbfoff, dbsize, lnoffset, lnsize),
501       int desc AND
502       char *filename AND
503       CORE_ADDR addr AND
504       int mainline AND
505       unsigned int dbfoff AND
506       unsigned int dbsize AND
507       unsigned int lnoffset AND
508       unsigned int lnsize)
509 {
510   struct cleanup *back_to;
511   
512   dbbase = xmalloc (dbsize);
513   dbroff = 0;
514   if ((lseek (desc, dbfoff, 0) != dbfoff) ||
515       (read (desc, dbbase, dbsize) != dbsize))
516     {
517       free (dbbase);
518       error ("can't read DWARF data from '%s'", filename);
519     }
520   back_to = make_cleanup (free, dbbase);
521   
522   /* If we are reinitializing, or if we have never loaded syms yet, init.
523      Since we have no idea how many DIES we are looking at, we just guess
524      some arbitrary value. */
525   
526   if (mainline || global_psymbols.size == 0 || static_psymbols.size == 0)
527     {
528       init_psymbol_list (1024);
529     }
530   
531   init_misc_bunches ();
532   make_cleanup (discard_misc_bunches, 0);
533   
534   /* Follow the compilation unit sibling chain, building a partial symbol
535      table entry for each one.  Save enough information about each compilation
536      unit to locate the full DWARF information later. */
537   
538   scan_compilation_units (filename, addr, dbbase, dbbase + dbsize,
539                           dbfoff, lnoffset);
540   
541   /* Go over the miscellaneous functions and install them in the miscellaneous
542      function vector. */
543   
544   condense_misc_bunches (!mainline);
545   do_cleanups (back_to);
546 }
547
548
549 /*
550
551 LOCAL FUNCTION
552
553         record_misc_function -- add entry to miscellaneous function vector
554
555 SYNOPSIS
556
557         static void record_misc_function (char *name, CORE_ADDR address)
558
559 DESCRIPTION
560
561         Given a pointer to the name of a symbol that should be added to the
562         miscellaneous function vector, and the address associated with that
563         symbol, records this information for later use in building the
564         miscellaneous function vector.
565
566 NOTES
567
568         FIXME:  For now we just use mf_text as the type.  This should be
569         fixed.
570  */
571
572 static void
573 DEFUN(record_misc_function, (name, address), char *name AND CORE_ADDR address)
574 {
575   prim_record_misc_function (obsavestring (name, strlen (name)), address,
576                              mf_text);
577 }
578
579 /*
580
581 LOCAL FUNCTION
582
583         dwarfwarn -- issue a DWARF related warning
584
585 DESCRIPTION
586
587         Issue warnings about DWARF related things that aren't serious enough
588         to warrant aborting with an error, but should not be ignored either.
589         This includes things like detectable corruption in DIE's, missing
590         DIE's, unimplemented features, etc.
591
592         In general, running across tags or attributes that we don't recognize
593         is not considered to be a problem and we should not issue warnings
594         about such.
595
596 NOTES
597
598         We mostly follow the example of the error() routine, but without
599         returning to command level.  It is arguable about whether warnings
600         should be issued at all, and if so, where they should go (stdout or
601         stderr).
602
603         We assume that curdie is valid and contains at least the basic
604         information for the DIE where the problem was noticed.
605 */
606
607 static void
608 dwarfwarn (va_alist)
609      va_dcl
610 {
611   va_list ap;
612   char *fmt;
613   
614   va_start (ap);
615   fmt = va_arg (ap, char *);
616   warning_setup ();
617   fprintf (stderr, "DWARF warning (ref 0x%x): ", curdie -> dieref);
618   if (curdie -> at_name)
619     {
620       fprintf (stderr, "'%s': ", curdie -> at_name);
621     }
622   vfprintf (stderr, fmt, ap);
623   fprintf (stderr, "\n");
624   fflush (stderr);
625   va_end (ap);
626 }
627
628 /*
629
630 LOCAL FUNCTION
631
632         compare_psymbols -- compare two partial symbols by name
633
634 DESCRIPTION
635
636         Given pointer to two partial symbol table entries, compare
637         them by name and return -N, 0, or +N (ala strcmp).  Typically
638         used by sorting routines like qsort().
639
640 NOTES
641
642         This is a copy from dbxread.c.  It should be moved to a generic
643         gdb file and made available for all psymtab builders (FIXME).
644
645         Does direct compare of first two characters before punting
646         and passing to strcmp for longer compares.  Note that the
647         original version had a bug whereby two null strings or two
648         identically named one character strings would return the
649         comparison of memory following the null byte.
650
651  */
652
653 static int
654 DEFUN(compare_psymbols, (s1, s2), 
655       struct partial_symbol *s1 AND
656       struct partial_symbol *s2)
657 {
658   register char *st1 = SYMBOL_NAME (s1);
659   register char *st2 = SYMBOL_NAME (s2);
660
661   if ((st1[0] - st2[0]) || !st1[0])
662     {
663       return (st1[0] - st2[0]);
664     }
665   else if ((st1[1] - st2[1]) || !st1[1])
666     {
667       return (st1[1] - st2[1]);
668     }
669   else
670     {
671       return (strcmp (st1 + 2, st2 + 2));
672     }
673 }
674
675 /*
676
677 LOCAL FUNCTION
678
679         read_lexical_block_scope -- process all dies in a lexical block
680
681 SYNOPSIS
682
683         static void read_lexical_block_scope (struct dieinfo *dip,
684                 char *thisdie, char *enddie)
685
686 DESCRIPTION
687
688         Process all the DIES contained within a lexical block scope.
689         Start a new scope, process the dies, and then close the scope.
690
691  */
692
693 static void
694 DEFUN(read_lexical_block_scope, (dip, thisdie, enddie),
695      struct dieinfo *dip AND
696      char *thisdie AND
697      char *enddie)
698 {
699   openscope (NULL, dip -> at_low_pc, dip -> at_high_pc);
700   process_dies (thisdie + dip -> dielength, enddie);
701   closescope ();
702 }
703
704 /*
705
706 LOCAL FUNCTION
707
708         lookup_utype -- look up a user defined type from die reference
709
710 SYNOPSIS
711
712         static type *lookup_utype (DIEREF dieref)
713
714 DESCRIPTION
715
716         Given a DIE reference, lookup the user defined type associated with
717         that DIE, if it has been registered already.  If not registered, then
718         return NULL.  Alloc_utype() can be called to register an empty
719         type for this reference, which will be filled in later when the
720         actual referenced DIE is processed.
721  */
722
723 static struct type *
724 DEFUN(lookup_utype, (dieref), DIEREF dieref)
725 {
726   struct type *type = NULL;
727   int utypeidx;
728   
729   utypeidx = (dieref - dbroff) / 4;
730   if ((utypeidx < 0) || (utypeidx >= numutypes))
731     {
732       dwarfwarn ("reference to DIE (0x%x) outside compilation unit", dieref);
733     }
734   else
735     {
736       type = *(utypes + utypeidx);
737     }
738   return (type);
739 }
740
741
742 /*
743
744 LOCAL FUNCTION
745
746         alloc_utype  -- add a user defined type for die reference
747
748 SYNOPSIS
749
750         static type *alloc_utype (DIEREF dieref, struct type *utypep)
751
752 DESCRIPTION
753
754         Given a die reference DIEREF, and a possible pointer to a user
755         defined type UTYPEP, register that this reference has a user
756         defined type and either use the specified type in UTYPEP or
757         make a new empty type that will be filled in later.
758
759         We should only be called after calling lookup_utype() to verify that
760         there is not currently a type registered for DIEREF.
761  */
762
763 static struct type *
764 DEFUN(alloc_utype, (dieref, utypep),
765      DIEREF dieref AND
766      struct type *utypep)
767 {
768   struct type **typep;
769   int utypeidx;
770   
771   utypeidx = (dieref - dbroff) / 4;
772   typep = utypes + utypeidx;
773   if ((utypeidx < 0) || (utypeidx >= numutypes))
774     {
775       utypep = builtin_type_int;
776       dwarfwarn ("reference to DIE (0x%x) outside compilation unit", dieref);
777     }
778   else if (*typep != NULL)
779     {
780       utypep = *typep;
781       SQUAWK (("internal error: dup user type allocation"));
782     }
783   else
784     {
785       if (utypep == NULL)
786         {
787           utypep = (struct type *)
788             obstack_alloc (symbol_obstack, sizeof (struct type));
789           (void) memset (utypep, 0, sizeof (struct type));
790         }
791       *typep = utypep;
792     }
793   return (utypep);
794 }
795
796 /*
797
798 LOCAL FUNCTION
799
800         decode_die_type -- return a type for a specified die
801
802 SYNOPSIS
803
804         static struct type *decode_die_type (struct dieinfo *dip)
805
806 DESCRIPTION
807
808         Given a pointer to a die information structure DIP, decode the
809         type of the die and return a pointer to the decoded type.  All
810         dies without specific types default to type int.
811  */
812
813 static struct type *
814 DEFUN(decode_die_type, (dip), struct dieinfo *dip)
815 {
816   struct type *type = NULL;
817   
818   if (dip -> at_fund_type != 0)
819     {
820       type = decode_fund_type (dip -> at_fund_type);
821     }
822   else if (dip -> at_mod_fund_type != NULL)
823     {
824       type = decode_mod_fund_type (dip -> at_mod_fund_type);
825     }
826   else if (dip -> at_user_def_type)
827     {
828       if ((type = lookup_utype (dip -> at_user_def_type)) == NULL)
829         {
830           type = alloc_utype (dip -> at_user_def_type, NULL);
831         }
832     }
833   else if (dip -> at_mod_u_d_type)
834     {
835       type = decode_mod_u_d_type (dip -> at_mod_u_d_type);
836     }
837   else
838     {
839       type = builtin_type_int;
840     }
841   return (type);
842 }
843
844 /*
845
846 LOCAL FUNCTION
847
848         struct_type -- compute and return the type for a struct or union
849
850 SYNOPSIS
851
852         static struct type *struct_type (struct dieinfo *dip, char *thisdie,
853             char *enddie)
854
855 DESCRIPTION
856
857         Given pointer to a die information structure for a die which
858         defines a union or structure, and pointers to the raw die data
859         that define the range of dies which define the members, compute
860         and return the user defined type for the structure or union.
861  */
862
863 static struct type *
864 DEFUN(struct_type, (dip, thisdie, enddie),
865      struct dieinfo *dip AND
866      char *thisdie AND
867      char *enddie)
868 {
869   struct type *type;
870   struct nextfield {
871     struct nextfield *next;
872     struct field field;
873   };
874   struct nextfield *list = NULL;
875   struct nextfield *new;
876   int nfields = 0;
877   int n;
878   char *tpart1;
879   char *tpart2;
880   char *tpart3;
881   struct dieinfo mbr;
882   
883   if ((type = lookup_utype (dip -> dieref)) == NULL)
884     {
885       type = alloc_utype (dip -> dieref, NULL);
886     }
887   switch (dip -> dietag)
888     {
889     case TAG_structure_type:
890       TYPE_CODE (type) = TYPE_CODE_STRUCT;
891       tpart1 = "struct ";
892       break;
893     case TAG_union_type:
894       TYPE_CODE (type) = TYPE_CODE_UNION;
895       tpart1 = "union ";
896       break;
897     default:
898       tpart1 = "";
899       SQUAWK (("missing structure or union tag"));
900       TYPE_CODE (type) = TYPE_CODE_UNDEF;
901       break;
902     }
903   if (dip -> at_name == NULL)
904     {
905       tpart2 = "{...}";
906     }
907   else
908     {
909       tpart2 = dip -> at_name;
910     }
911   if (dip -> at_byte_size == 0)
912     {
913       tpart3 = " <opaque>";
914     } else {
915       TYPE_LENGTH (type) = dip -> at_byte_size;
916       tpart3 = "";
917     }
918   TYPE_NAME (type) = concat (tpart1, tpart2, tpart3);
919   thisdie += dip -> dielength;
920   while (thisdie < enddie)
921     {
922       basicdieinfo (&mbr, thisdie);
923       completedieinfo (&mbr);
924       if (mbr.dielength <= sizeof (long))
925         {
926           break;
927         }
928       switch (mbr.dietag)
929         {
930         case TAG_member:
931           /* Get space to record the next field's data.  */
932           new = (struct nextfield *) alloca (sizeof (struct nextfield));
933           new -> next = list;
934           list = new;
935           /* Save the data.  */
936           list -> field.name = savestring (mbr.at_name, strlen (mbr.at_name));
937           list -> field.type = decode_die_type (&mbr);
938           list -> field.bitpos = 8 * locval (mbr.at_location);
939           list -> field.bitsize = 0;
940           nfields++;
941           break;
942         default:
943           SQUAWK (("bad member of '%s'", TYPE_NAME (type)));
944           break;
945         }
946       thisdie += mbr.dielength;
947     }
948   /* Now create the vector of fields, and record how big it is.  */
949   TYPE_NFIELDS (type) = nfields;
950   TYPE_FIELDS (type) = (struct field *)
951     obstack_alloc (symbol_obstack, sizeof (struct field) * nfields);
952   /* Copy the saved-up fields into the field vector.  */
953   for (n = nfields; list; list = list -> next)
954     {
955       TYPE_FIELD (type, --n) = list -> field;
956     }   
957   return (type);
958 }
959
960 /*
961
962 LOCAL FUNCTION
963
964         read_structure_scope -- process all dies within struct or union
965
966 SYNOPSIS
967
968         static void read_structure_scope (struct dieinfo *dip,
969                 char *thisdie, char *enddie)
970
971 DESCRIPTION
972
973         Called when we find the DIE that starts a structure or union
974         scope (definition) to process all dies that define the members
975         of the structure or union.  DIP is a pointer to the die info
976         struct for the DIE that names the structure or union.
977
978 NOTES
979
980         Note that we need to call struct_type regardless of whether or not
981         we have a symbol, since we might have a structure or union without
982         a tag name (thus no symbol for the tagname).
983  */
984
985 static void
986 DEFUN(read_structure_scope, (dip, thisdie, enddie),
987      struct dieinfo *dip AND
988      char *thisdie AND
989      char *enddie)
990 {
991   struct type *type;
992   struct symbol *sym;
993   
994   type = struct_type (dip, thisdie, enddie);
995   if ((sym = new_symbol (dip)) != NULL)
996     {
997       SYMBOL_TYPE (sym) = type;
998     }
999 }
1000
1001 /*
1002
1003 LOCAL FUNCTION
1004
1005         decode_array_element_type -- decode type of the array elements
1006
1007 SYNOPSIS
1008
1009         static struct type *decode_array_element_type (char *scan, char *end)
1010
1011 DESCRIPTION
1012
1013         As the last step in decoding the array subscript information for an
1014         array DIE, we need to decode the type of the array elements.  We are
1015         passed a pointer to this last part of the subscript information and
1016         must return the appropriate type.  If the type attribute is not
1017         recognized, just warn about the problem and return type int.
1018  */
1019
1020 static struct type *
1021 DEFUN(decode_array_element_type, (scan, end), char *scan AND char *end)
1022 {
1023   struct type *typep;
1024   short attribute;
1025   DIEREF dieref;
1026   unsigned short fundtype;
1027   
1028   (void) memcpy (&attribute, scan, sizeof (short));
1029   scan += sizeof (short);
1030   switch (attribute)
1031     {
1032     case AT_fund_type:
1033       (void) memcpy (&fundtype, scan, sizeof (short));
1034       typep = decode_fund_type (fundtype);
1035       break;
1036     case AT_mod_fund_type:
1037       typep = decode_mod_fund_type (scan);
1038       break;
1039     case AT_user_def_type:
1040       (void) memcpy (&dieref, scan, sizeof (DIEREF));
1041       if ((typep = lookup_utype (dieref)) == NULL)
1042         {
1043           typep = alloc_utype (dieref, NULL);
1044         }
1045       break;
1046     case AT_mod_u_d_type:
1047       typep = decode_mod_u_d_type (scan);
1048       break;
1049     default:
1050       SQUAWK (("bad array element type attribute 0x%x", attribute));
1051       typep = builtin_type_int;
1052       break;
1053     }
1054   return (typep);
1055 }
1056
1057 /*
1058
1059 LOCAL FUNCTION
1060
1061         decode_subscr_data -- decode array subscript and element type data
1062
1063 SYNOPSIS
1064
1065         static struct type *decode_subscr_data (char *scan, char *end)
1066
1067 DESCRIPTION
1068
1069         The array subscripts and the data type of the elements of an
1070         array are described by a list of data items, stored as a block
1071         of contiguous bytes.  There is a data item describing each array
1072         dimension, and a final data item describing the element type.
1073         The data items are ordered the same as their appearance in the
1074         source (I.E. leftmost dimension first, next to leftmost second,
1075         etc).
1076
1077         We are passed a pointer to the start of the block of bytes
1078         containing the data items, and a pointer to the first byte past
1079         the data.  This function decodes the data and returns a type.
1080
1081 BUGS
1082         FIXME:  This code only implements the forms currently used
1083         by the AT&T and GNU C compilers.
1084
1085         The end pointer is supplied for error checking, maybe we should
1086         use it for that...
1087  */
1088
1089 static struct type *
1090 DEFUN(decode_subscr_data, (scan, end), char *scan AND char *end)
1091 {
1092   struct type *typep = NULL;
1093   struct type *nexttype;
1094   int format;
1095   short fundtype;
1096   long lowbound;
1097   long highbound;
1098   
1099   format = *scan++;
1100   switch (format)
1101     {
1102     case FMT_ET:
1103       typep = decode_array_element_type (scan, end);
1104       break;
1105     case FMT_FT_C_C:
1106       (void) memcpy (&fundtype, scan, sizeof (short));
1107       scan += sizeof (short);
1108       if (fundtype != FT_integer && fundtype != FT_signed_integer
1109           && fundtype != FT_unsigned_integer)
1110         {
1111           SQUAWK (("array subscripts must be integral types, not type 0x%x",
1112                      fundtype));
1113         }
1114       else
1115         {
1116           (void) memcpy (&lowbound, scan, sizeof (long));
1117           scan += sizeof (long);
1118           (void) memcpy (&highbound, scan, sizeof (long));
1119           scan += sizeof (long);
1120           nexttype = decode_subscr_data (scan, end);
1121           if (nexttype != NULL)
1122             {
1123               typep = (struct type *)
1124                 obstack_alloc (symbol_obstack, sizeof (struct type));
1125               (void) memset (typep, 0, sizeof (struct type));
1126               TYPE_CODE (typep) = TYPE_CODE_ARRAY;
1127               TYPE_LENGTH (typep) = TYPE_LENGTH (nexttype);
1128               TYPE_LENGTH (typep) *= lowbound + highbound + 1;
1129               TYPE_TARGET_TYPE (typep) = nexttype;
1130             }               
1131         }
1132       break;
1133     case FMT_FT_C_X:
1134     case FMT_FT_X_C:
1135     case FMT_FT_X_X:
1136     case FMT_UT_C_C:
1137     case FMT_UT_C_X:
1138     case FMT_UT_X_C:
1139     case FMT_UT_X_X:
1140       SQUAWK (("array subscript format 0x%x not handled yet", format));
1141       break;
1142     default:
1143       SQUAWK (("unknown array subscript format %x", format));
1144       break;
1145     }
1146   return (typep);
1147 }
1148
1149 /*
1150
1151 LOCAL FUNCTION
1152
1153         read_array_type -- read TAG_array_type DIE
1154
1155 SYNOPSIS
1156
1157         static void read_array_type (struct dieinfo *dip)
1158
1159 DESCRIPTION
1160
1161         Extract all information from a TAG_array_type DIE and add to
1162         the user defined type vector.
1163  */
1164
1165 static void
1166 DEFUN(read_array_type, (dip), struct dieinfo *dip)
1167 {
1168   struct type *type;
1169   char *sub;
1170   char *subend;
1171   short temp;
1172   
1173   if (dip -> at_ordering != ORD_row_major)
1174     {
1175       /* FIXME:  Can gdb even handle column major arrays? */
1176       SQUAWK (("array not row major; not handled correctly"));
1177     }
1178   if ((sub = dip -> at_subscr_data) != NULL)
1179     {
1180       (void) memcpy (&temp, sub, sizeof (short));
1181       subend = sub + sizeof (short) + temp;
1182       sub += sizeof (short);
1183       type = decode_subscr_data (sub, subend);
1184       if (type == NULL)
1185         {
1186           type = alloc_utype (dip -> dieref, NULL);
1187           TYPE_CODE (type) = TYPE_CODE_ARRAY;
1188           TYPE_TARGET_TYPE (type) = builtin_type_int;
1189           TYPE_LENGTH (type) = 1 * TYPE_LENGTH (TYPE_TARGET_TYPE (type));
1190         }
1191       else
1192         {
1193           type = alloc_utype (dip -> dieref, type);
1194         }
1195     }
1196 }
1197
1198 /*
1199
1200 LOCAL FUNCTION
1201
1202         read_subroutine_type -- process TAG_subroutine_type dies
1203
1204 SYNOPSIS
1205
1206         static void read_subroutine_type (struct dieinfo *dip, char thisdie,
1207                 char *enddie)
1208
1209 DESCRIPTION
1210
1211         Handle DIES due to C code like:
1212
1213         struct foo {
1214             int (*funcp)(int a, long l);  (Generates TAG_subroutine_type DIE)
1215             int b;
1216         };
1217
1218 NOTES
1219
1220         The parameter DIES are currently ignored.  See if gdb has a way to
1221         include this info in it's type system, and decode them if so.  Is
1222         this what the type structure's "arg_types" field is for?  (FIXME)
1223  */
1224
1225 static void
1226 DEFUN(read_subroutine_type, (dip, thisdie, enddie),
1227      struct dieinfo *dip AND
1228      char *thisdie AND
1229      char *enddie)
1230 {
1231   struct type *type;
1232   
1233   type = decode_die_type (dip);
1234   type = lookup_function_type (type);
1235   type = alloc_utype (dip -> dieref, type);
1236 }
1237
1238 /*
1239
1240 LOCAL FUNCTION
1241
1242         read_enumeration -- process dies which define an enumeration
1243
1244 SYNOPSIS
1245
1246         static void read_enumeration (struct dieinfo *dip, char *thisdie,
1247                 char *enddie)
1248
1249 DESCRIPTION
1250
1251         Given a pointer to a die which begins an enumeration, process all
1252         the dies that define the members of the enumeration.
1253
1254 NOTES
1255
1256         Note that we need to call enum_type regardless of whether or not we
1257         have a symbol, since we might have an enum without a tag name (thus
1258         no symbol for the tagname).
1259  */
1260
1261 static void
1262 DEFUN(read_enumeration, (dip, thisdie, enddie),
1263      struct dieinfo *dip AND
1264      char *thisdie AND
1265      char *enddie)
1266 {
1267   struct type *type;
1268   struct symbol *sym;
1269   
1270   type = enum_type (dip);
1271   if ((sym = new_symbol (dip)) != NULL)
1272     {
1273       SYMBOL_TYPE (sym) = type;
1274     }
1275 }
1276
1277 /*
1278
1279 LOCAL FUNCTION
1280
1281         enum_type -- decode and return a type for an enumeration
1282
1283 SYNOPSIS
1284
1285         static type *enum_type (struct dieinfo *dip)
1286
1287 DESCRIPTION
1288
1289         Given a pointer to a die information structure for the die which
1290         starts an enumeration, process all the dies that define the members
1291         of the enumeration and return a type pointer for the enumeration.
1292  */
1293
1294 static struct type *
1295 DEFUN(enum_type, (dip), struct dieinfo *dip)
1296 {
1297   struct type *type;
1298   struct nextfield {
1299     struct nextfield *next;
1300     struct field field;
1301   };
1302   struct nextfield *list = NULL;
1303   struct nextfield *new;
1304   int nfields = 0;
1305   int n;
1306   char *tpart1;
1307   char *tpart2;
1308   char *tpart3;
1309   char *scan;
1310   char *listend;
1311   long temp;
1312   
1313   if ((type = lookup_utype (dip -> dieref)) == NULL)
1314     {
1315       type = alloc_utype (dip -> dieref, NULL);
1316     }
1317   TYPE_CODE (type) = TYPE_CODE_ENUM;
1318   tpart1 = "enum ";
1319   if (dip -> at_name == NULL)
1320     {
1321       tpart2 = "{...}";
1322     } else {
1323       tpart2 = dip -> at_name;
1324     }
1325   if (dip -> at_byte_size == 0)
1326     {
1327       tpart3 = " <opaque>";
1328     }
1329   else
1330     {
1331       TYPE_LENGTH (type) = dip -> at_byte_size;
1332       tpart3 = "";
1333     }
1334   TYPE_NAME (type) = concat (tpart1, tpart2, tpart3);
1335   if ((scan = dip -> at_element_list) != NULL)
1336     {
1337       (void) memcpy (&temp, scan, sizeof (temp));
1338       listend = scan + temp + sizeof (temp);
1339       scan += sizeof (temp);
1340       while (scan < listend)
1341         {
1342           new = (struct nextfield *) alloca (sizeof (struct nextfield));
1343           new -> next = list;
1344           list = new;
1345           list -> field.type = NULL;
1346           list -> field.bitsize = 0;
1347           (void) memcpy (&list -> field.bitpos, scan, sizeof (long));
1348           scan += sizeof (long);
1349           list -> field.name = savestring (scan, strlen (scan));
1350           scan += strlen (scan) + 1;
1351           nfields++;
1352         }
1353     }
1354   /* Now create the vector of fields, and record how big it is.  */
1355   TYPE_NFIELDS (type) = nfields;
1356   TYPE_FIELDS (type) = (struct field *)
1357     obstack_alloc (symbol_obstack, sizeof (struct field) * nfields);
1358   /* Copy the saved-up fields into the field vector.  */
1359   for (n = nfields; list; list = list -> next)
1360     {
1361       TYPE_FIELD (type, --n) = list -> field;
1362     }   
1363   return (type);
1364 }
1365
1366 /*
1367
1368 LOCAL FUNCTION
1369
1370         read_func_scope -- process all dies within a function scope
1371
1372 SYNOPSIS
1373
1374         static void read_func_scope (struct dieinfo dip, char *thisdie,
1375             char *enddie)
1376
1377 DESCRIPTION
1378
1379         Process all dies within a given function scope.  We are passed
1380         a die information structure pointer DIP for the die which
1381         starts the function scope, and pointers into the raw die data
1382         that define the dies within the function scope.
1383
1384         For now, we ignore lexical block scopes within the function.
1385         The problem is that AT&T cc does not define a DWARF lexical
1386         block scope for the function itself, while gcc defines a
1387         lexical block scope for the function.  We need to think about
1388         how to handle this difference, or if it is even a problem.
1389         (FIXME)
1390  */
1391
1392 static void
1393 DEFUN(read_func_scope, (dip, thisdie, enddie),
1394      struct dieinfo *dip AND
1395      char *thisdie AND
1396      char *enddie)
1397 {
1398   struct symbol *sym;
1399   
1400   if (entry_point >= dip -> at_low_pc && entry_point < dip -> at_high_pc)
1401     {
1402       entry_scope_lowpc = dip -> at_low_pc;
1403       entry_scope_highpc = dip -> at_high_pc;
1404     }
1405   if (strcmp (dip -> at_name, "main") == 0)     /* FIXME: hardwired name */
1406     {
1407       main_scope_lowpc = dip -> at_low_pc;
1408       main_scope_highpc = dip -> at_high_pc;
1409     }
1410   sym = new_symbol (dip);
1411   openscope (sym, dip -> at_low_pc, dip -> at_high_pc);
1412   process_dies (thisdie + dip -> dielength, enddie);
1413   closescope ();
1414 }
1415
1416 /*
1417
1418 LOCAL FUNCTION
1419
1420         read_file_scope -- process all dies within a file scope
1421
1422 SYNOPSIS
1423
1424         static void read_file_scope (struct dieinfo *dip, char *thisdie
1425                 char *enddie)
1426
1427 DESCRIPTION
1428
1429         Process all dies within a given file scope.  We are passed a
1430         pointer to the die information structure for the die which
1431         starts the file scope, and pointers into the raw die data which
1432         mark the range of dies within the file scope.
1433
1434         When the partial symbol table is built, the file offset for the line
1435         number table for each compilation unit is saved in the partial symbol
1436         table entry for that compilation unit.  As the symbols for each
1437         compilation unit are read, the line number table is read into memory
1438         and the variable lnbase is set to point to it.  Thus all we have to
1439         do is use lnbase to access the line number table for the current
1440         compilation unit.
1441  */
1442
1443 static void
1444 DEFUN(read_file_scope, (dip, thisdie, enddie),
1445      struct dieinfo *dip AND
1446      char *thisdie AND
1447      char *enddie)
1448 {
1449   struct cleanup *back_to;
1450   
1451   if (entry_point >= dip -> at_low_pc && entry_point < dip -> at_high_pc)
1452     {
1453       startup_file_start = dip -> at_low_pc;
1454       startup_file_end = dip -> at_high_pc;
1455     }
1456   numutypes = (enddie - thisdie) / 4;
1457   utypes = (struct type **) xmalloc (numutypes * sizeof (struct type *));
1458   back_to = make_cleanup (free, utypes);
1459   (void) memset (utypes, 0, numutypes * sizeof (struct type *));
1460   start_symtab ();
1461   openscope (NULL, dip -> at_low_pc, dip -> at_high_pc);
1462   decode_line_numbers (lnbase);
1463   process_dies (thisdie + dip -> dielength, enddie);
1464   closescope ();
1465   end_symtab (dip -> at_name, dip -> at_language);
1466   do_cleanups (back_to);
1467   utypes = NULL;
1468   numutypes = 0;
1469 }
1470
1471 /*
1472
1473 LOCAL FUNCTION
1474
1475         start_symtab -- do initialization for starting new symbol table
1476
1477 SYNOPSIS
1478
1479         static void start_symtab (void)
1480
1481 DESCRIPTION
1482
1483         Called whenever we are starting to process dies for a new
1484         compilation unit, to perform initializations.  Right now
1485         the only thing we really have to do is initialize storage
1486         space for the line number vector.
1487
1488  */
1489
1490 static void
1491 DEFUN_VOID (start_symtab)
1492 {
1493   int nbytes;
1494
1495   line_vector_index = 0;
1496   line_vector_length = 1000;
1497   nbytes = sizeof (struct linetable);
1498   nbytes += line_vector_length * sizeof (struct linetable_entry);
1499   line_vector = (struct linetable *) xmalloc (nbytes);
1500 }
1501
1502 /*
1503
1504 LOCAL FUNCTION
1505
1506         process_dies -- process a range of DWARF Information Entries
1507
1508 SYNOPSIS
1509
1510         static void process_dies (char *thisdie, char *enddie)
1511
1512 DESCRIPTION
1513
1514         Process all DIE's in a specified range.  May be (and almost
1515         certainly will be) called recursively.
1516  */
1517
1518 static void
1519 DEFUN(process_dies, (thisdie, enddie), char *thisdie AND char *enddie)
1520 {
1521   char *nextdie;
1522   struct dieinfo di;
1523   
1524   while (thisdie < enddie)
1525     {
1526       basicdieinfo (&di, thisdie);
1527       if (di.dielength < sizeof (long))
1528         {
1529           break;
1530         }
1531       else if (di.dietag == TAG_padding)
1532         {
1533           nextdie = thisdie + di.dielength;
1534         }
1535       else
1536         {
1537           completedieinfo (&di);
1538           if (di.at_sibling != 0)
1539             {
1540               nextdie = dbbase + di.at_sibling - dbroff;
1541             }
1542           else
1543             {
1544               nextdie = thisdie + di.dielength;
1545             }
1546           switch (di.dietag)
1547             {
1548             case TAG_compile_unit:
1549               read_file_scope (&di, thisdie, nextdie);
1550               break;
1551             case TAG_global_subroutine:
1552             case TAG_subroutine:
1553               if (!di.at_is_external_p)
1554                 {
1555                   read_func_scope (&di, thisdie, nextdie);
1556                 }
1557               break;
1558             case TAG_lexical_block:
1559               read_lexical_block_scope (&di, thisdie, nextdie);
1560               break;
1561             case TAG_structure_type:
1562             case TAG_union_type:
1563               read_structure_scope (&di, thisdie, nextdie);
1564               break;
1565             case TAG_enumeration_type:
1566               read_enumeration (&di, thisdie, nextdie);
1567               break;
1568             case TAG_subroutine_type:
1569               read_subroutine_type (&di, thisdie, nextdie);
1570               break;
1571             case TAG_array_type:
1572               read_array_type (&di);
1573               break;
1574             default:
1575               (void) new_symbol (&di);
1576               break;
1577             }
1578         }
1579       thisdie = nextdie;
1580     }
1581 }
1582
1583 /*
1584
1585 LOCAL FUNCTION
1586
1587         end_symtab -- finish processing for a compilation unit
1588
1589 SYNOPSIS
1590
1591         static void end_symtab (char *filename, long language)
1592
1593 DESCRIPTION
1594
1595         Complete the symbol table entry for the current compilation
1596         unit.  Make the struct symtab and put it on the list of all
1597         such symtabs.
1598
1599  */
1600
1601 static void
1602 DEFUN(end_symtab, (filename, language), char *filename AND long language)
1603 {
1604   struct symtab *symtab;
1605   struct blockvector *blockvector;
1606   int nbytes;
1607   
1608   /* Ignore a file that has no functions with real debugging info.  */
1609   if (global_symbols == NULL && scopetree -> block == NULL)
1610     {
1611       free (line_vector);
1612       line_vector = NULL;
1613       line_vector_length = -1;
1614       freescope (scopetree);
1615       scope = scopetree = NULL;
1616     }
1617   
1618   /* Create the blockvector that points to all the file's blocks.  */
1619   
1620   blockvector = make_blockvector ();
1621   
1622   /* Now create the symtab object for this source file.  */
1623   
1624   symtab = (struct symtab *) xmalloc (sizeof (struct symtab));
1625   (void) memset (symtab, 0, sizeof (struct symtab));
1626   
1627   symtab -> free_ptr = 0;
1628   
1629   /* Fill in its components.  */
1630   symtab -> blockvector = blockvector;
1631   symtab -> free_code = free_linetable;
1632   symtab -> filename = savestring (filename, strlen (filename));
1633   
1634   /* Save the line number information. */
1635   
1636   line_vector -> nitems = line_vector_index;
1637   nbytes = sizeof (struct linetable);
1638   if (line_vector_index > 1)
1639     {
1640       nbytes += (line_vector_index - 1) * sizeof (struct linetable_entry);
1641     }
1642   symtab -> linetable = (struct linetable *) xrealloc (line_vector, nbytes);
1643   symtab -> nlines = 0;
1644   symtab -> line_charpos = 0;
1645   
1646   /* FIXME:  The following may need to be expanded for other languages */
1647   switch (language)
1648     {
1649       case LANG_C89:
1650       case LANG_C:
1651         symtab -> language = language_c;
1652         break;
1653       case LANG_C_PLUS_PLUS:
1654         symtab -> language = language_cplus;
1655         break;
1656       default:
1657         ;
1658     }
1659
1660   /* Link the new symtab into the list of such.  */
1661   symtab -> next = symtab_list;
1662   symtab_list = symtab;
1663   
1664   /* Recursively free the scope tree */
1665   freescope (scopetree);
1666   scope = scopetree = NULL;
1667   
1668   /* Reinitialize for beginning of new file. */
1669   line_vector = 0;
1670   line_vector_length = -1;
1671 }
1672
1673 /*
1674
1675 LOCAL FUNCTION
1676
1677         scopecount -- count the number of enclosed scopes
1678
1679 SYNOPSIS
1680
1681         static int scopecount (struct scopenode *node)
1682
1683 DESCRIPTION
1684
1685         Given pointer to a node, compute the size of the subtree which is
1686         rooted in this node, which also happens to be the number of scopes
1687         to the subtree.
1688  */
1689
1690 static int
1691 DEFUN(scopecount, (node), struct scopenode *node)
1692 {
1693   int count = 0;
1694   
1695   if (node != NULL)
1696     {
1697       count += scopecount (node -> child);
1698       count += scopecount (node -> sibling);
1699       count++;
1700     }
1701   return (count);
1702 }
1703
1704 /*
1705
1706 LOCAL FUNCTION
1707
1708         openscope -- start a new lexical block scope
1709
1710 SYNOPSIS
1711
1712         static void openscope (struct symbol *namesym, CORE_ADDR lowpc,
1713                 CORE_ADDR highpc)
1714
1715 DESCRIPTION
1716
1717         Start a new scope by allocating a new scopenode, adding it as the
1718         next child of the current scope (if any) or as the root of the
1719         scope tree, and then making the new node the current scope node.
1720  */
1721
1722 static void
1723 DEFUN(openscope, (namesym, lowpc, highpc),
1724      struct symbol *namesym AND
1725      CORE_ADDR lowpc AND
1726      CORE_ADDR highpc)
1727 {
1728   struct scopenode *new;
1729   struct scopenode *child;
1730   
1731   new = (struct scopenode *) xmalloc (sizeof (*new));
1732   (void) memset (new, 0, sizeof (*new));
1733   new -> namesym = namesym;
1734   new -> lowpc = lowpc;
1735   new -> highpc = highpc;
1736   if (scope == NULL)
1737     {
1738       scopetree = new;
1739     }
1740   else if ((child = scope -> child) == NULL)
1741     {
1742       scope -> child = new;
1743       new -> parent = scope;
1744     }
1745   else
1746     {
1747     while (child -> sibling != NULL)
1748       {
1749         child = child -> sibling;
1750       }
1751     child -> sibling = new;
1752     new -> parent = scope;
1753   }
1754   scope = new;
1755 }
1756
1757 /*
1758
1759 LOCAL FUNCTION
1760
1761         freescope -- free a scope tree rooted at the given node
1762
1763 SYNOPSIS
1764
1765         static void freescope (struct scopenode *node)
1766
1767 DESCRIPTION
1768
1769         Given a pointer to a node in the scope tree, free the subtree
1770         rooted at that node.  First free all the children and sibling
1771         nodes, and then the node itself.  Used primarily for cleaning
1772         up after ourselves and returning memory to the system.
1773  */
1774
1775 static void
1776 DEFUN(freescope, (node), struct scopenode *node)
1777 {
1778   if (node != NULL)
1779     {
1780       freescope (node -> child);
1781       freescope (node -> sibling);
1782       free (node);
1783     }
1784 }
1785
1786 /*
1787
1788 LOCAL FUNCTION
1789
1790         buildblock -- build a new block from pending symbols list
1791
1792 SYNOPSIS
1793
1794         static struct block *buildblock (struct pending_symbol *syms)
1795
1796 DESCRIPTION
1797
1798         Given a pointer to a list of symbols, build a new block and free
1799         the symbol list structure.  Also check each symbol to see if it
1800         is the special symbol that flags that this block was compiled by
1801         gcc, and if so, mark the block appropriately.
1802  */
1803
1804 static struct block *
1805 DEFUN(buildblock, (syms), struct pending_symbol *syms)
1806 {
1807   struct pending_symbol *next, *next1;
1808   int i;
1809   struct block *newblock;
1810   int nbytes;
1811   
1812   for (next = syms, i = 0 ; next ; next = next -> next, i++) {;}
1813   
1814   /* Allocate a new block */
1815   
1816   nbytes = sizeof (struct block);
1817   if (i > 1)
1818     {
1819       nbytes += (i - 1) * sizeof (struct symbol *);
1820     }
1821   newblock = (struct block *) obstack_alloc (symbol_obstack, nbytes);
1822   (void) memset (newblock, 0, nbytes);
1823   
1824   /* Copy the symbols into the block.  */
1825   
1826   BLOCK_NSYMS (newblock) = i;
1827   for (next = syms ; next ; next = next -> next)
1828     {
1829       BLOCK_SYM (newblock, --i) = next -> symbol;
1830       if (STREQ (GCC_COMPILED_FLAG_SYMBOL, SYMBOL_NAME (next -> symbol)) ||
1831           STREQ (GCC2_COMPILED_FLAG_SYMBOL, SYMBOL_NAME (next -> symbol)))
1832         {
1833           BLOCK_GCC_COMPILED (newblock) = 1;
1834         }
1835     }    
1836   
1837   /* Now free the links of the list, and empty the list.  */
1838   
1839   for (next = syms ; next ; next = next1)
1840     {
1841       next1 = next -> next;
1842       free (next);
1843     }
1844   
1845   return (newblock);
1846 }
1847
1848 /*
1849
1850 LOCAL FUNCTION
1851
1852         closescope -- close a lexical block scope
1853
1854 SYNOPSIS
1855
1856         static void closescope (void)
1857
1858 DESCRIPTION
1859
1860         Close the current lexical block scope.  Closing the current scope
1861         is as simple as moving the current scope pointer up to the parent
1862         of the current scope pointer.  But we also take this opportunity
1863         to build the block for the current scope first, since we now have
1864         all of it's symbols.
1865  */
1866
1867 static void
1868 DEFUN_VOID(closescope)
1869 {
1870   struct scopenode *child;
1871   
1872   if (scope == NULL)
1873     {
1874       error ("DWARF parse error, too many close scopes");
1875     }
1876   else
1877     {
1878       if (scope -> parent == NULL)
1879         {
1880           global_symbol_block = buildblock (global_symbols);
1881           global_symbols = NULL;
1882           BLOCK_START (global_symbol_block) = scope -> lowpc + baseaddr;
1883           BLOCK_END (global_symbol_block) = scope -> highpc + baseaddr;
1884         }
1885       scope -> block = buildblock (scope -> symbols);
1886       scope -> symbols = NULL;
1887       BLOCK_START (scope -> block) = scope -> lowpc + baseaddr;
1888       BLOCK_END (scope -> block) = scope -> highpc + baseaddr;
1889     
1890       /* Put the local block in as the value of the symbol that names it.  */
1891     
1892       if (scope -> namesym)
1893         {
1894           SYMBOL_BLOCK_VALUE (scope -> namesym) = scope -> block;
1895           BLOCK_FUNCTION (scope -> block) = scope -> namesym;
1896         }
1897     
1898     /*  Install this scope's local block as the superblock of all child
1899         scope blocks. */
1900     
1901     for (child = scope -> child ; child ; child = child -> sibling)
1902       {
1903         BLOCK_SUPERBLOCK (child -> block) = scope -> block;
1904       }
1905     
1906       scope = scope -> parent;
1907     }
1908 }
1909
1910 /*
1911
1912 LOCAL FUNCTION
1913
1914         record_line -- record a line number entry in the line vector
1915
1916 SYNOPSIS
1917
1918         static void record_line (int line, CORE_ADDR pc)
1919
1920 DESCRIPTION
1921
1922         Given a line number and the corresponding pc value, record
1923         this pair in the line number vector, expanding the vector as
1924         necessary.
1925  */
1926
1927 static void
1928 DEFUN(record_line, (line, pc), int line AND CORE_ADDR pc)
1929 {
1930   struct linetable_entry *e;
1931   int nbytes;
1932   
1933   /* Make sure line vector is big enough.  */
1934   
1935   if (line_vector_index + 2 >= line_vector_length)
1936     {
1937       line_vector_length *= 2;
1938       nbytes = sizeof (struct linetable);
1939       nbytes += (line_vector_length * sizeof (struct linetable_entry));
1940       line_vector = (struct linetable *) xrealloc (line_vector, nbytes);
1941     }
1942   e = line_vector -> item + line_vector_index++;
1943   e -> line = line;
1944   e -> pc = pc;
1945 }
1946
1947 /*
1948
1949 LOCAL FUNCTION
1950
1951         decode_line_numbers -- decode a line number table fragment
1952
1953 SYNOPSIS
1954
1955         static void decode_line_numbers (char *tblscan, char *tblend,
1956                 long length, long base, long line, long pc)
1957
1958 DESCRIPTION
1959
1960         Translate the DWARF line number information to gdb form.
1961
1962         The ".line" section contains one or more line number tables, one for
1963         each ".line" section from the objects that were linked.
1964
1965         The AT_stmt_list attribute for each TAG_source_file entry in the
1966         ".debug" section contains the offset into the ".line" section for the
1967         start of the table for that file.
1968
1969         The table itself has the following structure:
1970
1971         <table length><base address><source statement entry>
1972         4 bytes       4 bytes       10 bytes
1973
1974         The table length is the total size of the table, including the 4 bytes
1975         for the length information.
1976
1977         The base address is the address of the first instruction generated
1978         for the source file.
1979
1980         Each source statement entry has the following structure:
1981
1982         <line number><statement position><address delta>
1983         4 bytes      2 bytes             4 bytes
1984
1985         The line number is relative to the start of the file, starting with
1986         line 1.
1987
1988         The statement position either -1 (0xFFFF) or the number of characters
1989         from the beginning of the line to the beginning of the statement.
1990
1991         The address delta is the difference between the base address and
1992         the address of the first instruction for the statement.
1993
1994         Note that we must copy the bytes from the packed table to our local
1995         variables before attempting to use them, to avoid alignment problems
1996         on some machines, particularly RISC processors.
1997
1998 BUGS
1999
2000         Does gdb expect the line numbers to be sorted?  They are now by
2001         chance/luck, but are not required to be.  (FIXME)
2002
2003         The line with number 0 is unused, gdb apparently can discover the
2004         span of the last line some other way. How?  (FIXME)
2005  */
2006
2007 static void
2008 DEFUN(decode_line_numbers, (linetable), char *linetable)
2009 {
2010   char *tblscan;
2011   char *tblend;
2012   long length;
2013   long base;
2014   long line;
2015   long pc;
2016   
2017   if (linetable != NULL)
2018     {
2019       tblscan = tblend = linetable;
2020       (void) memcpy (&length, tblscan, sizeof (long));
2021       tblscan += sizeof (long);
2022       tblend += length;
2023       (void) memcpy (&base, tblscan, sizeof (long));
2024       base += baseaddr;
2025       tblscan += sizeof (long);
2026       while (tblscan < tblend)
2027         {
2028           (void) memcpy (&line, tblscan, sizeof (long));
2029           tblscan += sizeof (long) + sizeof (short);
2030           (void) memcpy (&pc, tblscan, sizeof (long));
2031           tblscan += sizeof (long);
2032           pc += base;
2033           if (line > 0)
2034             {
2035               record_line (line, pc);
2036             }
2037         }
2038     }
2039 }
2040
2041 /*
2042
2043 LOCAL FUNCTION
2044
2045         add_symbol_to_list -- add a symbol to head of current symbol list
2046
2047 SYNOPSIS
2048
2049         static void add_symbol_to_list (struct symbol *symbol, struct
2050                 pending_symbol **listhead)
2051
2052 DESCRIPTION
2053
2054         Given a pointer to a symbol and a pointer to a pointer to a
2055         list of symbols, add this symbol as the current head of the
2056         list.  Typically used for example to add a symbol to the
2057         symbol list for the current scope.
2058
2059  */
2060
2061 static void
2062 DEFUN(add_symbol_to_list, (symbol, listhead),
2063      struct symbol *symbol AND struct pending_symbol **listhead)
2064 {
2065   struct pending_symbol *link;
2066   
2067   if (symbol != NULL)
2068     {
2069       link = (struct pending_symbol *) xmalloc (sizeof (*link));
2070       link -> next = *listhead;
2071       link -> symbol = symbol;
2072       *listhead = link;
2073     }
2074 }
2075
2076 /*
2077
2078 LOCAL FUNCTION
2079
2080         gatherblocks -- walk a scope tree and build block vectors
2081
2082 SYNOPSIS
2083
2084         static struct block **gatherblocks (struct block **dest,
2085                 struct scopenode *node)
2086
2087 DESCRIPTION
2088
2089         Recursively walk a scope tree rooted in the given node, adding blocks
2090         to the array pointed to by DEST, in preorder.  I.E., first we add the
2091         block for the current scope, then all the blocks for child scopes,
2092         and finally all the blocks for sibling scopes.
2093  */
2094
2095 static struct block **
2096 DEFUN(gatherblocks, (dest, node),
2097       struct block **dest AND struct scopenode *node)
2098 {
2099   if (node != NULL)
2100     {
2101       *dest++ = node -> block;
2102       dest = gatherblocks (dest, node -> child);
2103       dest = gatherblocks (dest, node -> sibling);
2104     }
2105   return (dest);
2106 }
2107
2108 /*
2109
2110 LOCAL FUNCTION
2111
2112         make_blockvector -- make a block vector from current scope tree
2113
2114 SYNOPSIS
2115
2116         static struct blockvector *make_blockvector (void)
2117
2118 DESCRIPTION
2119
2120         Make a blockvector from all the blocks in the current scope tree.
2121         The first block is always the global symbol block, followed by the
2122         block for the root of the scope tree which is the local symbol block,
2123         followed by all the remaining blocks in the scope tree, which are all
2124         local scope blocks.
2125
2126 NOTES
2127
2128         Note that since the root node of the scope tree is created at the time
2129         each file scope is entered, there are always at least two blocks,
2130         neither of which may have any symbols, but always contribute a block
2131         to the block vector.  So the test for number of blocks greater than 1
2132         below is unnecessary given bug free code.
2133
2134         The resulting block structure varies slightly from that produced
2135         by dbxread.c, in that block 0 and block 1 are sibling blocks while
2136         with dbxread.c, block 1 is a child of block 0.  This does not
2137         seem to cause any problems, but probably should be fixed. (FIXME)
2138  */
2139
2140 static struct blockvector *
2141 DEFUN_VOID(make_blockvector)
2142 {
2143   struct blockvector *blockvector = NULL;
2144   int i;
2145   int nbytes;
2146   
2147   /* Recursively walk down the tree, counting the number of blocks.
2148      Then add one to account for the global's symbol block */
2149   
2150   i = scopecount (scopetree) + 1;
2151   nbytes = sizeof (struct blockvector);
2152   if (i > 1)
2153     {
2154       nbytes += (i - 1) * sizeof (struct block *);
2155     }
2156   blockvector = (struct blockvector *)
2157     obstack_alloc (symbol_obstack, nbytes);
2158   
2159   /* Copy the blocks into the blockvector. */
2160   
2161   BLOCKVECTOR_NBLOCKS (blockvector) = i;
2162   BLOCKVECTOR_BLOCK (blockvector, 0) = global_symbol_block;
2163   gatherblocks (&BLOCKVECTOR_BLOCK (blockvector, 1), scopetree);
2164   
2165   return (blockvector);
2166 }
2167
2168 /*
2169
2170 LOCAL FUNCTION
2171
2172         locval -- compute the value of a location attribute
2173
2174 SYNOPSIS
2175
2176         static int locval (char *loc)
2177
2178 DESCRIPTION
2179
2180         Given pointer to a string of bytes that define a location, compute
2181         the location and return the value.
2182
2183         When computing values involving the current value of the frame pointer,
2184         the value zero is used, which results in a value relative to the frame
2185         pointer, rather than the absolute value.  This is what GDB wants
2186         anyway.
2187     
2188         When the result is a register number, the global isreg flag is set,
2189         otherwise it is cleared.  This is a kludge until we figure out a better
2190         way to handle the problem.  Gdb's design does not mesh well with the
2191         DWARF notion of a location computing interpreter, which is a shame
2192         because the flexibility goes unused.
2193
2194 NOTES
2195
2196         Note that stack[0] is unused except as a default error return.
2197         Note that stack overflow is not yet handled.
2198  */
2199
2200 static int
2201 DEFUN(locval, (loc), char *loc)
2202 {
2203   unsigned short nbytes;
2204   auto int stack[64];
2205   int stacki;
2206   char *end;
2207   long regno;
2208   
2209   (void) memcpy (&nbytes, loc, sizeof (short));
2210   end = loc + sizeof (short) + nbytes;
2211   stacki = 0;
2212   stack[stacki] = 0;
2213   isreg = 0;
2214   for (loc += sizeof (short); loc < end; loc += sizeof (long))
2215     {
2216       switch (*loc++) {
2217       case 0:
2218         /* error */
2219         loc = end;
2220         break;
2221       case OP_REG:
2222         /* push register (number) */
2223         (void) memcpy (&stack[++stacki], loc, sizeof (long));
2224         isreg = 1;
2225         break;
2226       case OP_BASEREG:
2227         /* push value of register (number) */
2228         /* Actually, we compute the value as if register has 0 */
2229         (void) memcpy (&regno, loc, sizeof (long));
2230         if (regno == R_FP)
2231           {
2232             stack[++stacki] = 0;
2233           }
2234         else
2235           {
2236             stack[++stacki] = 0;
2237             SQUAWK (("BASEREG %d not handled!", regno));
2238           }
2239         break;
2240       case OP_ADDR:
2241         /* push address (relocated address) */
2242         (void) memcpy (&stack[++stacki], loc, sizeof (long));
2243         break;
2244       case OP_CONST:
2245         /* push constant (number) */
2246         (void) memcpy (&stack[++stacki], loc, sizeof (long));
2247         break;
2248       case OP_DEREF2:
2249         /* pop, deref and push 2 bytes (as a long) */
2250         SQUAWK (("OP_DEREF2 address %#x not handled", stack[stacki]));
2251         break;
2252       case OP_DEREF4:   /* pop, deref and push 4 bytes (as a long) */
2253         SQUAWK (("OP_DEREF4 address %#x not handled", stack[stacki]));
2254         break;
2255       case OP_ADD:      /* pop top 2 items, add, push result */
2256         stack[stacki - 1] += stack[stacki];
2257         stacki--;
2258         break;
2259       }
2260     }
2261   return (stack[stacki]);
2262 }
2263
2264 /*
2265
2266 LOCAL FUNCTION
2267
2268         read_ofile_symtab -- build a full symtab entry from chunk of DIE's
2269
2270 SYNOPSIS
2271
2272         static struct symtab *read_ofile_symtab (struct partial_symtab *pst,
2273                 int desc)
2274
2275 DESCRIPTION
2276
2277         DESC is the file descriptor for the file, positioned at the
2278         beginning of the symtab
2279         SYM_SIZE is the size of the symbol section to read
2280         TEXT_OFFSET is the beginning of the text segment we are reading
2281         symbols for
2282         TEXT_SIZE is the size of the text segment read in.
2283         OFFSET is a relocation offset which gets added to each symbol
2284
2285  */
2286
2287 static struct symtab *
2288 DEFUN(read_ofile_symtab, (pst, desc),
2289       struct partial_symtab *pst AND
2290       int desc)
2291 {
2292   struct cleanup *back_to;
2293   long lnsize;
2294   int foffset;
2295   
2296   /* Allocate a buffer for the entire chunk of DIE's for this compilation
2297      unit, seek to the location in the file, and read in all the DIE's. */
2298
2299   diecount = 0;
2300   dbbase = xmalloc (DBLENGTH(pst));
2301   dbroff = DBROFF(pst);
2302   foffset = DBFOFF(pst) + dbroff;
2303   if ((lseek (desc, foffset, 0) != foffset) ||
2304       (read (desc, dbbase, DBLENGTH(pst)) != DBLENGTH(pst)))
2305     {
2306       free (dbbase);
2307       error ("can't read DWARF data");
2308     }
2309   back_to = make_cleanup (free, dbbase);
2310
2311   /* If there is a line number table associated with this compilation unit
2312      then read the first long word from the line number table fragment, which
2313      contains the size of the fragment in bytes (including the long word
2314      itself).  Allocate a buffer for the fragment and read it in for future
2315      processing. */
2316
2317   lnbase = NULL;
2318   if (LNFOFF (pst))
2319     {
2320       if ((lseek (desc, LNFOFF (pst), 0) != LNFOFF (pst)) ||
2321           (read (desc, &lnsize, sizeof(long)) != sizeof(long)))
2322         {
2323           error ("can't read DWARF line number table size");
2324         }
2325       lnbase = xmalloc (lnsize);
2326       if ((lseek (desc, LNFOFF (pst), 0) != LNFOFF (pst)) ||
2327           (read (desc, lnbase, lnsize) != lnsize))
2328         {
2329           free (lnbase);
2330           error ("can't read DWARF line numbers");
2331         }
2332       make_cleanup (free, lnbase);
2333     }
2334
2335   process_dies (dbbase, dbbase + DBLENGTH(pst));
2336   do_cleanups (back_to);
2337   return (symtab_list);
2338 }
2339
2340 /*
2341
2342 LOCAL FUNCTION
2343
2344         psymtab_to_symtab_1 -- do grunt work for building a full symtab entry
2345
2346 SYNOPSIS
2347
2348         static void psymtab_to_symtab_1 (struct partial_symtab *pst, int desc)
2349
2350 DESCRIPTION
2351
2352         Called once for each partial symbol table entry that needs to be
2353         expanded into a full symbol table entry.
2354
2355 */
2356
2357 static void
2358 DEFUN(psymtab_to_symtab_1,
2359       (pst, desc),
2360       struct partial_symtab *pst AND
2361       int desc)
2362 {
2363   int i;
2364   
2365   if (!pst)
2366     {
2367       return;
2368     }
2369   if (pst->readin)
2370     {
2371       fprintf (stderr, "Psymtab for %s already read in.  Shouldn't happen.\n",
2372                pst -> filename);
2373       return;
2374     }
2375   
2376   /* Read in all partial symtabs on which this one is dependent */
2377   for (i = 0; i < pst -> number_of_dependencies; i++)
2378     if (!pst -> dependencies[i] -> readin)
2379       {
2380         /* Inform about additional files that need to be read in.  */
2381         if (info_verbose)
2382           {
2383             fputs_filtered (" ", stdout);
2384             wrap_here ("");
2385             fputs_filtered ("and ", stdout);
2386             wrap_here ("");
2387             printf_filtered ("%s...", pst -> dependencies[i] -> filename);
2388             wrap_here ("");             /* Flush output */
2389             fflush (stdout);
2390           }
2391         psymtab_to_symtab_1 (pst -> dependencies[i], desc);
2392       }
2393   
2394   if (DBLENGTH(pst))            /* Otherwise it's a dummy */
2395     {
2396       /* Init stuff necessary for reading in symbols */
2397       pst -> symtab = read_ofile_symtab (pst, desc);
2398       if (info_verbose)
2399         {
2400           printf_filtered ("%d DIE's, sorting...", diecount);
2401           fflush (stdout);
2402         }
2403       sort_symtab_syms (pst -> symtab);
2404     }
2405   pst -> readin = 1;
2406 }
2407
2408 /*
2409
2410 LOCAL FUNCTION
2411
2412         dwarf_psymtab_to_symtab -- build a full symtab entry from partial one
2413
2414 SYNOPSIS
2415
2416         static void dwarf_psymtab_to_symtab (struct partial_symtab *pst)
2417
2418 DESCRIPTION
2419
2420         This is the DWARF support entry point for building a full symbol
2421         table entry from a partial symbol table entry.  We are passed a
2422         pointer to the partial symbol table entry that needs to be expanded.
2423
2424 */
2425
2426 static void
2427 DEFUN(dwarf_psymtab_to_symtab, (pst), struct partial_symtab *pst)
2428 {
2429   int desc;
2430   struct cleanup *old_chain;
2431   bfd *sym_bfd;
2432   
2433   if (!pst)
2434     {
2435       return;
2436     }
2437   if (pst -> readin)
2438     {
2439       fprintf (stderr, "Psymtab for %s already read in.  Shouldn't happen.\n",
2440                pst -> filename);
2441       return;
2442     }
2443   
2444   if (DBLENGTH(pst) || pst -> number_of_dependencies)
2445     {
2446       /* Print the message now, before starting serious work, to avoid
2447          disconcerting pauses.  */
2448       if (info_verbose)
2449         {
2450           printf_filtered ("Reading in symbols for %s...", pst -> filename);
2451           fflush (stdout);
2452         }
2453       
2454       /* Open symbol file.  Symbol_file_command guarantees that the symbol
2455          file name will be absolute, so there is no need for openp.  */
2456       desc = open (pst -> symfile_name, O_RDONLY, 0);
2457       
2458       if (desc < 0)
2459         {
2460           perror_with_name (pst -> symfile_name);
2461         }
2462       
2463       sym_bfd = bfd_fdopenr (pst -> symfile_name, NULL, desc);
2464       if (!sym_bfd)
2465         {
2466           (void) close (desc);
2467           error ("Could not open `%s' to read symbols: %s",
2468                  pst -> symfile_name, bfd_errmsg (bfd_error));
2469         }
2470       old_chain = make_cleanup (bfd_close, sym_bfd);
2471       if (!bfd_check_format (sym_bfd, bfd_object))
2472         {
2473           error ("\"%s\": can't read symbols: %s.",
2474                  pst -> symfile_name, bfd_errmsg (bfd_error));
2475         }
2476       
2477       psymtab_to_symtab_1 (pst, desc);
2478       
2479 #if 0 /* FIXME:  Check to see what dbxread is doing here and see if
2480          we need to do an equivalent or is this something peculiar to
2481          stabs/a.out format. */
2482       /* Match with global symbols.  This only needs to be done once,
2483          after all of the symtabs and dependencies have been read in.   */
2484       scan_file_globals ();
2485 #endif
2486       
2487       do_cleanups (old_chain);
2488       
2489       /* Finish up the debug error message.  */
2490       if (info_verbose)
2491         {
2492           printf_filtered ("done.\n");
2493         }
2494     }
2495 }
2496
2497 /*
2498
2499 LOCAL FUNCTION
2500
2501         init_psymbol_list -- initialize storage for partial symbols
2502
2503 SYNOPSIS
2504
2505         static void init_psymbol_list (int total_symbols)
2506
2507 DESCRIPTION
2508
2509         Initializes storage for all of the partial symbols that will be
2510         created by dwarf_build_psymtabs and subsidiaries.
2511  */
2512
2513 static void
2514 DEFUN(init_psymbol_list, (total_symbols), int total_symbols)
2515 {
2516   /* Free any previously allocated psymbol lists.  */
2517   
2518   if (global_psymbols.list)
2519     {
2520       free (global_psymbols.list);
2521     }
2522   if (static_psymbols.list)
2523     {
2524       free (static_psymbols.list);
2525     }
2526   
2527   /* Current best guess is that there are approximately a twentieth
2528      of the total symbols (in a debugging file) are global or static
2529      oriented symbols */
2530   
2531   global_psymbols.size = total_symbols / 10;
2532   static_psymbols.size = total_symbols / 10;
2533   global_psymbols.next = global_psymbols.list = (struct partial_symbol *)
2534     xmalloc (global_psymbols.size * sizeof (struct partial_symbol));
2535   static_psymbols.next = static_psymbols.list = (struct partial_symbol *)
2536     xmalloc (static_psymbols.size * sizeof (struct partial_symbol));
2537 }
2538
2539 /*
2540
2541 LOCAL FUNCTION
2542
2543         start_psymtab -- allocate and partially fill a partial symtab entry
2544
2545 DESCRIPTION
2546
2547         Allocate and partially fill a partial symtab.  It will be completely
2548         filled at the end of the symbol list.
2549    
2550         SYMFILE_NAME is the name of the symbol-file we are reading from, and
2551         ADDR is the address relative to which its symbols are (incremental)
2552         or 0 (normal).  FILENAME is the name of the compilation unit that
2553         these symbols were defined in, and they appear starting a address
2554         TEXTLOW.  DBROFF is the absolute file offset in SYMFILE_NAME where
2555         the full symbols can be read for compilation unit FILENAME.
2556         GLOBAL_SYMS and STATIC_SYMS are pointers to the current end of the
2557         psymtab vector.
2558
2559  */
2560
2561 static struct partial_symtab *
2562 DEFUN(start_psymtab,
2563       (symfile_name, addr, filename, textlow, texthigh, dbfoff, curoff,
2564        culength, lnfoff, global_syms, static_syms),
2565       char *symfile_name AND
2566       CORE_ADDR addr AND
2567       char *filename AND
2568       CORE_ADDR textlow AND
2569       CORE_ADDR texthigh AND
2570       int dbfoff AND
2571       int curoff AND
2572       int culength AND
2573       int lnfoff AND
2574       struct partial_symbol *global_syms AND
2575       struct partial_symbol *static_syms)
2576 {
2577   struct partial_symtab *result;
2578
2579   result = (struct partial_symtab *)
2580       obstack_alloc (psymbol_obstack, sizeof (struct partial_symtab));
2581   (void) memset (result, 0, sizeof (struct partial_symtab));
2582   result -> addr = addr;
2583   result -> symfile_name =  create_name (symfile_name, psymbol_obstack);
2584   result -> filename = create_name (filename, psymbol_obstack);
2585   result -> textlow = textlow;
2586   result -> texthigh = texthigh;
2587   result -> read_symtab_private = (char *) obstack_alloc (psymbol_obstack,
2588                                                   sizeof (struct dwfinfo));
2589   DBFOFF (result) = dbfoff;
2590   DBROFF (result) = curoff;
2591   DBLENGTH (result) = culength;
2592   LNFOFF (result)  = lnfoff;
2593   result -> readin = 0;
2594   result -> symtab = NULL;
2595   result -> read_symtab = dwarf_psymtab_to_symtab;
2596   result -> globals_offset = global_syms - global_psymbols.list;
2597   result -> statics_offset = static_syms - static_psymbols.list;
2598   
2599   result->n_global_syms = 0;
2600   result->n_static_syms = 0;
2601   
2602   return result;
2603 }
2604
2605 /*
2606
2607 LOCAL FUNCTION
2608
2609         add_psymbol_to_list -- add a partial symbol to given list
2610
2611 DESCRIPTION
2612
2613         Add a partial symbol to one of the partial symbol vectors (pointed to
2614         by listp).  The vector is grown as necessary.
2615
2616  */
2617
2618 static void
2619 DEFUN(add_psymbol_to_list,
2620       (listp, name, space, class, value),
2621       struct psymbol_allocation_list *listp AND
2622       char *name AND
2623       enum namespace space AND
2624       enum address_class class AND
2625       CORE_ADDR value)
2626 {
2627   struct partial_symbol *psym;
2628   int newsize;
2629   
2630   if (listp -> next >= listp -> list + listp -> size)
2631     {
2632       newsize = listp -> size * 2;
2633       listp -> list = (struct partial_symbol *)
2634         xrealloc (listp -> list, (newsize * sizeof (struct partial_symbol)));
2635       /* Next assumes we only went one over.  Should be good if program works
2636          correctly */
2637       listp -> next = listp -> list + listp -> size;
2638       listp -> size = newsize;
2639     }
2640   psym = listp -> next++;
2641   SYMBOL_NAME (psym) = create_name (name, psymbol_obstack);
2642   SYMBOL_NAMESPACE (psym) = space;
2643   SYMBOL_CLASS (psym) = class;
2644   SYMBOL_VALUE (psym) = value;
2645 }
2646
2647 /*
2648
2649 LOCAL FUNCTION
2650
2651         add_partial_symbol -- add symbol to partial symbol table
2652
2653 DESCRIPTION
2654
2655         Given a DIE, if it is one of the types that we want to
2656         add to a partial symbol table, finish filling in the die info
2657         and then add a partial symbol table entry for it.
2658
2659 */
2660
2661 static void
2662 DEFUN(add_partial_symbol, (dip), struct dieinfo *dip)
2663 {
2664   switch (dip -> dietag)
2665     {
2666     case TAG_global_subroutine:
2667       record_misc_function (dip -> at_name, dip -> at_low_pc);
2668       add_psymbol_to_list (&global_psymbols, dip -> at_name, VAR_NAMESPACE,
2669                            LOC_BLOCK, dip -> at_low_pc);
2670       break;
2671     case TAG_global_variable:
2672       add_psymbol_to_list (&global_psymbols, dip -> at_name, VAR_NAMESPACE,
2673                            LOC_STATIC, 0);
2674       break;
2675     case TAG_subroutine:
2676       add_psymbol_to_list (&static_psymbols, dip -> at_name, VAR_NAMESPACE,
2677                            LOC_BLOCK, dip -> at_low_pc);
2678       break;
2679     case TAG_local_variable:
2680       add_psymbol_to_list (&static_psymbols, dip -> at_name, VAR_NAMESPACE,
2681                            LOC_STATIC, 0);
2682       break;
2683     case TAG_typedef:
2684       add_psymbol_to_list (&static_psymbols, dip -> at_name, VAR_NAMESPACE,
2685                            LOC_TYPEDEF, 0);
2686       break;
2687     case TAG_structure_type:
2688     case TAG_union_type:
2689     case TAG_enumeration_type:
2690       add_psymbol_to_list (&static_psymbols, dip -> at_name, STRUCT_NAMESPACE,
2691                            LOC_TYPEDEF, 0);
2692       break;
2693     }
2694 }
2695
2696 /*
2697
2698 LOCAL FUNCTION
2699
2700         scan_partial_symbols -- scan DIE's within a single compilation unit
2701
2702 DESCRIPTION
2703
2704         Process the DIE's within a single compilation unit, looking for
2705         interesting DIE's that contribute to the partial symbol table entry
2706         for this compilation unit.  Since we cannot follow any sibling
2707         chains without reading the complete DIE info for every DIE,
2708         it is probably faster to just sequentially check each one to
2709         see if it is one of the types we are interested in, and if
2710         so, then extracting all the attributes info and generating a
2711         partial symbol table entry.
2712
2713  */
2714
2715 static void
2716 DEFUN(scan_partial_symbols, (thisdie, enddie), char *thisdie AND char *enddie)
2717 {
2718   char *nextdie;
2719   struct dieinfo di;
2720   
2721   while (thisdie < enddie)
2722     {
2723       basicdieinfo (&di, thisdie);
2724       if (di.dielength < sizeof (long))
2725         {
2726           break;
2727         }
2728       else
2729         {
2730           nextdie = thisdie + di.dielength;
2731           switch (di.dietag)
2732             {
2733             case TAG_global_subroutine:
2734             case TAG_global_variable:
2735             case TAG_subroutine:
2736             case TAG_local_variable:
2737             case TAG_typedef:
2738             case TAG_structure_type:
2739             case TAG_union_type:
2740             case TAG_enumeration_type:
2741               completedieinfo (&di);
2742               /* Don't attempt to add anonymous structures, unions, or
2743                  enumerations since they have no name.  Also check that
2744                  this is the place where the actual definition occurs,
2745                  rather than just a reference to an external. */
2746               if (di.at_name != NULL && !di.at_is_external_p)
2747                 {
2748                   add_partial_symbol (&di);
2749                 }
2750               break;
2751             }
2752         }
2753       thisdie = nextdie;
2754     }
2755 }
2756
2757 /*
2758
2759 LOCAL FUNCTION
2760
2761         scan_compilation_units -- build a psymtab entry for each compilation
2762
2763 DESCRIPTION
2764
2765         This is the top level dwarf parsing routine for building partial
2766         symbol tables.
2767
2768         It scans from the beginning of the DWARF table looking for the first
2769         TAG_compile_unit DIE, and then follows the sibling chain to locate
2770         each additional TAG_compile_unit DIE.
2771    
2772         For each TAG_compile_unit DIE it creates a partial symtab structure,
2773         calls a subordinate routine to collect all the compilation unit's
2774         global DIE's, file scope DIEs, typedef DIEs, etc, and then links the
2775         new partial symtab structure into the partial symbol table.  It also
2776         records the appropriate information in the partial symbol table entry
2777         to allow the chunk of DIE's and line number table for this compilation
2778         unit to be located and re-read later, to generate a complete symbol
2779         table entry for the compilation unit.
2780
2781         Thus it effectively partitions up a chunk of DIE's for multiple
2782         compilation units into smaller DIE chunks and line number tables,
2783         and associates them with a partial symbol table entry.
2784
2785 NOTES
2786
2787         If any compilation unit has no line number table associated with
2788         it for some reason (a missing at_stmt_list attribute, rather than
2789         just one with a value of zero, which is valid) then we ensure that
2790         the recorded file offset is zero so that the routine which later
2791         reads line number table fragments knows that there is no fragment
2792         to read.
2793
2794 RETURNS
2795
2796         Returns no value.
2797
2798  */
2799
2800 static void
2801 DEFUN(scan_compilation_units,
2802       (filename, addr, thisdie, enddie, dbfoff, lnoffset),
2803       char *filename AND
2804       CORE_ADDR addr AND
2805       char *thisdie AND
2806       char *enddie AND
2807       unsigned int dbfoff AND
2808       unsigned int lnoffset)
2809 {
2810   char *nextdie;
2811   struct dieinfo di;
2812   struct partial_symtab *pst;
2813   int culength;
2814   int curoff;
2815   int curlnoffset;
2816
2817   while (thisdie < enddie)
2818     {
2819       basicdieinfo (&di, thisdie);
2820       if (di.dielength < sizeof (long))
2821         {
2822           break;
2823         }
2824       else if (di.dietag != TAG_compile_unit)
2825         {
2826           nextdie = thisdie + di.dielength;
2827         }
2828       else
2829         {
2830           completedieinfo (&di);
2831           if (di.at_sibling != 0)
2832             {
2833               nextdie = dbbase + di.at_sibling - dbroff;
2834             }
2835           else
2836             {
2837               nextdie = thisdie + di.dielength;
2838             }
2839           curoff = thisdie - dbbase;
2840           culength = nextdie - thisdie;
2841           curlnoffset = di.at_stmt_list_p ? lnoffset + di.at_stmt_list : 0;
2842           pst = start_psymtab (filename, addr, di.at_name,
2843                                      di.at_low_pc, di.at_high_pc,
2844                                      dbfoff, curoff, culength, curlnoffset,
2845                                      global_psymbols.next,
2846                                      static_psymbols.next);
2847           scan_partial_symbols (thisdie + di.dielength, nextdie);
2848           pst -> n_global_syms = global_psymbols.next -
2849             (global_psymbols.list + pst -> globals_offset);
2850           pst -> n_static_syms = static_psymbols.next - 
2851             (static_psymbols.list + pst -> statics_offset);
2852           /* Sort the global list; don't sort the static list */
2853           qsort (global_psymbols.list + pst -> globals_offset,
2854                  pst -> n_global_syms, sizeof (struct partial_symbol),
2855                  compare_psymbols);
2856           /* If there is already a psymtab or symtab for a file of this name,
2857              remove it. (If there is a symtab, more drastic things also
2858              happen.)  This happens in VxWorks.  */
2859           free_named_symtabs (pst -> filename);
2860           /* Place the partial symtab on the partial symtab list */
2861           pst -> next = partial_symtab_list;
2862           partial_symtab_list = pst;
2863         }
2864       thisdie = nextdie;      
2865     }
2866 }
2867
2868 /*
2869
2870 LOCAL FUNCTION
2871
2872         new_symbol -- make a symbol table entry for a new symbol
2873
2874 SYNOPSIS
2875
2876         static struct symbol *new_symbol (struct dieinfo *dip)
2877
2878 DESCRIPTION
2879
2880         Given a pointer to a DWARF information entry, figure out if we need
2881         to make a symbol table entry for it, and if so, create a new entry
2882         and return a pointer to it.
2883  */
2884
2885 static struct symbol *
2886 DEFUN(new_symbol, (dip), struct dieinfo *dip)
2887 {
2888   struct symbol *sym = NULL;
2889   
2890   if (dip -> at_name != NULL)
2891     {
2892       sym = (struct symbol *) obstack_alloc (symbol_obstack,
2893                                              sizeof (struct symbol));
2894       (void) memset (sym, 0, sizeof (struct symbol));
2895       SYMBOL_NAME (sym) = create_name (dip -> at_name, symbol_obstack);
2896       /* default assumptions */
2897       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
2898       SYMBOL_CLASS (sym) = LOC_STATIC;
2899       SYMBOL_TYPE (sym) = decode_die_type (dip);
2900       switch (dip -> dietag)
2901         {
2902         case TAG_label:
2903           SYMBOL_VALUE (sym) = dip -> at_low_pc + baseaddr;
2904           SYMBOL_CLASS (sym) = LOC_LABEL;
2905           break;
2906         case TAG_global_subroutine:
2907         case TAG_subroutine:
2908           SYMBOL_VALUE (sym) = dip -> at_low_pc + baseaddr;
2909           SYMBOL_TYPE (sym) = lookup_function_type (SYMBOL_TYPE (sym));
2910           SYMBOL_CLASS (sym) = LOC_BLOCK;
2911           if (dip -> dietag == TAG_global_subroutine)
2912             {
2913               add_symbol_to_list (sym, &global_symbols);
2914             }
2915           else
2916             {
2917               add_symbol_to_list (sym, &scope -> symbols);
2918             }
2919           break;
2920         case TAG_global_variable:
2921         case TAG_local_variable:
2922           if (dip -> at_location != NULL)
2923             {
2924               SYMBOL_VALUE (sym) = locval (dip -> at_location);
2925             }
2926           if (dip -> dietag == TAG_global_variable)
2927             {
2928               add_symbol_to_list (sym, &global_symbols);
2929               SYMBOL_CLASS (sym) = LOC_STATIC;
2930               SYMBOL_VALUE (sym) += baseaddr;
2931             }
2932           else
2933             {
2934               add_symbol_to_list (sym, &scope -> symbols);
2935               if (scope -> parent != NULL)
2936                 {
2937                   if (isreg)
2938                     {
2939                       SYMBOL_CLASS (sym) = LOC_REGISTER;
2940                     }
2941                   else
2942                     {
2943                       SYMBOL_CLASS (sym) = LOC_LOCAL;
2944                     }
2945                 }
2946               else
2947                 {
2948                   SYMBOL_CLASS (sym) = LOC_STATIC;
2949                   SYMBOL_VALUE (sym) += baseaddr;
2950                 }
2951             }
2952           break;
2953         case TAG_formal_parameter:
2954           if (dip -> at_location != NULL)
2955             {
2956               SYMBOL_VALUE (sym) = locval (dip -> at_location);
2957             }
2958           add_symbol_to_list (sym, &scope -> symbols);
2959           if (isreg)
2960             {
2961               SYMBOL_CLASS (sym) = LOC_REGPARM;
2962             }
2963           else
2964             {
2965               SYMBOL_CLASS (sym) = LOC_ARG;
2966             }
2967           break;
2968         case TAG_unspecified_parameters:
2969           /* From varargs functions; gdb doesn't seem to have any interest in
2970              this information, so just ignore it for now. (FIXME?) */
2971           break;
2972         case TAG_structure_type:
2973         case TAG_union_type:
2974         case TAG_enumeration_type:
2975           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
2976           SYMBOL_NAMESPACE (sym) = STRUCT_NAMESPACE;
2977           add_symbol_to_list (sym, &scope -> symbols);
2978           break;
2979         case TAG_typedef:
2980           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
2981           SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
2982           add_symbol_to_list (sym, &scope -> symbols);
2983           break;
2984         default:
2985           /* Not a tag we recognize.  Hopefully we aren't processing trash
2986              data, but since we must specifically ignore things we don't
2987              recognize, there is nothing else we should do at this point. */
2988           break;
2989         }
2990     }
2991   return (sym);
2992 }
2993
2994 /*
2995
2996 LOCAL FUNCTION
2997
2998         decode_mod_fund_type -- decode a modified fundamental type
2999
3000 SYNOPSIS
3001
3002         static struct type *decode_mod_fund_type (char *typedata)
3003
3004 DESCRIPTION
3005
3006         Decode a block of data containing a modified fundamental
3007         type specification.  TYPEDATA is a pointer to the block,
3008         which consists of a two byte length, containing the size
3009         of the rest of the block.  At the end of the block is a
3010         two byte value that gives the fundamental type.  Everything
3011         in between are type modifiers.
3012
3013         We simply compute the number of modifiers and call the general
3014         function decode_modified_type to do the actual work.
3015 */
3016
3017 static struct type *
3018 DEFUN(decode_mod_fund_type, (typedata), char *typedata)
3019 {
3020   struct type *typep = NULL;
3021   unsigned short modcount;
3022   unsigned char *modifiers;
3023   
3024   /* Get the total size of the block, exclusive of the size itself */
3025   (void) memcpy (&modcount, typedata, sizeof (short));
3026   /* Deduct the size of the fundamental type bytes at the end of the block. */
3027   modcount -= sizeof (short);
3028   /* Skip over the two size bytes at the beginning of the block. */
3029   modifiers = (unsigned char *) typedata + sizeof (short);
3030   /* Now do the actual decoding */
3031   typep = decode_modified_type (modifiers, modcount, AT_mod_fund_type);
3032   return (typep);
3033 }
3034
3035 /*
3036
3037 LOCAL FUNCTION
3038
3039         decode_mod_u_d_type -- decode a modified user defined type
3040
3041 SYNOPSIS
3042
3043         static struct type *decode_mod_u_d_type (char *typedata)
3044
3045 DESCRIPTION
3046
3047         Decode a block of data containing a modified user defined
3048         type specification.  TYPEDATA is a pointer to the block,
3049         which consists of a two byte length, containing the size
3050         of the rest of the block.  At the end of the block is a
3051         four byte value that gives a reference to a user defined type.
3052         Everything in between are type modifiers.
3053
3054         We simply compute the number of modifiers and call the general
3055         function decode_modified_type to do the actual work.
3056 */
3057
3058 static struct type *
3059 DEFUN(decode_mod_u_d_type, (typedata), char *typedata)
3060 {
3061   struct type *typep = NULL;
3062   unsigned short modcount;
3063   unsigned char *modifiers;
3064   
3065   /* Get the total size of the block, exclusive of the size itself */
3066   (void) memcpy (&modcount, typedata, sizeof (short));
3067   /* Deduct the size of the reference type bytes at the end of the block. */
3068   modcount -= sizeof (long);
3069   /* Skip over the two size bytes at the beginning of the block. */
3070   modifiers = (unsigned char *) typedata + sizeof (short);
3071   /* Now do the actual decoding */
3072   typep = decode_modified_type (modifiers, modcount, AT_mod_u_d_type);
3073   return (typep);
3074 }
3075
3076 /*
3077
3078 LOCAL FUNCTION
3079
3080         decode_modified_type -- decode modified user or fundamental type
3081
3082 SYNOPSIS
3083
3084         static struct type *decode_modified_type (unsigned char *modifiers,
3085             unsigned short modcount, int mtype)
3086
3087 DESCRIPTION
3088
3089         Decode a modified type, either a modified fundamental type or
3090         a modified user defined type.  MODIFIERS is a pointer to the
3091         block of bytes that define MODCOUNT modifiers.  Immediately
3092         following the last modifier is a short containing the fundamental
3093         type or a long containing the reference to the user defined
3094         type.  Which one is determined by MTYPE, which is either
3095         AT_mod_fund_type or AT_mod_u_d_type to indicate what modified
3096         type we are generating.
3097
3098         We call ourself recursively to generate each modified type,`
3099         until MODCOUNT reaches zero, at which point we have consumed
3100         all the modifiers and generate either the fundamental type or
3101         user defined type.  When the recursion unwinds, each modifier
3102         is applied in turn to generate the full modified type.
3103
3104 NOTES
3105
3106         If we find a modifier that we don't recognize, and it is not one
3107         of those reserved for application specific use, then we issue a
3108         warning and simply ignore the modifier.
3109
3110 BUGS
3111
3112         We currently ignore MOD_const and MOD_volatile.  (FIXME)
3113
3114  */
3115
3116 static struct type *
3117 DEFUN(decode_modified_type,
3118       (modifiers, modcount, mtype),
3119       unsigned char *modifiers AND unsigned short modcount AND int mtype)
3120 {
3121   struct type *typep = NULL;
3122   unsigned short fundtype;
3123   DIEREF dieref;
3124   unsigned char modifier;
3125   
3126   if (modcount == 0)
3127     {
3128       switch (mtype)
3129         {
3130         case AT_mod_fund_type:
3131           (void) memcpy (&fundtype, modifiers, sizeof (short));
3132           typep = decode_fund_type (fundtype);
3133           break;
3134         case AT_mod_u_d_type:
3135           (void) memcpy (&dieref, modifiers, sizeof (DIEREF));
3136           if ((typep = lookup_utype (dieref)) == NULL)
3137             {
3138               typep = alloc_utype (dieref, NULL);
3139             }
3140           break;
3141         default:
3142           SQUAWK (("botched modified type decoding (mtype 0x%x)", mtype));
3143           typep = builtin_type_int;
3144           break;
3145         }
3146     }
3147   else
3148     {
3149       modifier = *modifiers++;
3150       typep = decode_modified_type (modifiers, --modcount, mtype);
3151       switch (modifier)
3152         {
3153         case MOD_pointer_to:
3154           typep = lookup_pointer_type (typep);
3155           break;
3156         case MOD_reference_to:
3157           typep = lookup_reference_type (typep);
3158           break;
3159         case MOD_const:
3160           SQUAWK (("type modifier 'const' ignored"));   /* FIXME */
3161           break;
3162         case MOD_volatile:
3163           SQUAWK (("type modifier 'volatile' ignored"));        /* FIXME */
3164           break;
3165         default:
3166           if (!(MOD_lo_user <= modifier && modifier <= MOD_hi_user))
3167             {
3168               SQUAWK (("unknown type modifier %u", modifier));
3169             }
3170           break;
3171         }
3172     }
3173   return (typep);
3174 }
3175
3176 /*
3177
3178 LOCAL FUNCTION
3179
3180         decode_fund_type -- translate basic DWARF type to gdb base type
3181
3182 DESCRIPTION
3183
3184         Given an integer that is one of the fundamental DWARF types,
3185         translate it to one of the basic internal gdb types and return
3186         a pointer to the appropriate gdb type (a "struct type *").
3187
3188 NOTES
3189
3190         If we encounter a fundamental type that we are unprepared to
3191         deal with, and it is not in the range of those types defined
3192         as application specific types, then we issue a warning and
3193         treat the type as builtin_type_int.
3194 */
3195
3196 static struct type *
3197 DEFUN(decode_fund_type, (fundtype), unsigned short fundtype)
3198 {
3199   struct type *typep = NULL;
3200   
3201   switch (fundtype)
3202     {
3203
3204     case FT_void:
3205       typep = builtin_type_void;
3206       break;
3207     
3208     case FT_pointer:            /* (void *) */
3209       typep = lookup_pointer_type (builtin_type_void);
3210       break;
3211     
3212     case FT_char:
3213     case FT_signed_char:
3214       typep = builtin_type_char;
3215       break;
3216     
3217     case FT_short:
3218     case FT_signed_short:
3219       typep = builtin_type_short;
3220       break;
3221     
3222     case FT_integer:
3223     case FT_signed_integer:
3224     case FT_boolean:            /* Was FT_set in AT&T version */
3225       typep = builtin_type_int;
3226       break;
3227     
3228     case FT_long:
3229     case FT_signed_long:
3230       typep = builtin_type_long;
3231       break;
3232     
3233     case FT_float:
3234       typep = builtin_type_float;
3235       break;
3236     
3237     case FT_dbl_prec_float:
3238       typep = builtin_type_double;
3239       break;
3240     
3241     case FT_unsigned_char:
3242       typep = builtin_type_unsigned_char;
3243       break;
3244     
3245     case FT_unsigned_short:
3246       typep = builtin_type_unsigned_short;
3247       break;
3248     
3249     case FT_unsigned_integer:
3250       typep = builtin_type_unsigned_int;
3251       break;
3252     
3253     case FT_unsigned_long:
3254       typep = builtin_type_unsigned_long;
3255       break;
3256     
3257     case FT_ext_prec_float:
3258       typep = builtin_type_long_double;
3259       break;
3260     
3261     case FT_complex:
3262       typep = builtin_type_complex;
3263       break;
3264     
3265     case FT_dbl_prec_complex:
3266       typep = builtin_type_double_complex;
3267       break;
3268     
3269     case FT_long_long:
3270     case FT_signed_long_long:
3271       typep = builtin_type_long_long;
3272       break;
3273
3274     case FT_unsigned_long_long:
3275       typep = builtin_type_unsigned_long_long;
3276       break;
3277
3278     }
3279
3280   if ((typep == NULL) && !(FT_lo_user <= fundtype && fundtype <= FT_hi_user))
3281     {
3282       SQUAWK (("unexpected fundamental type 0x%x", fundtype));
3283       typep = builtin_type_void;
3284     }
3285     
3286   return (typep);
3287 }
3288
3289 /*
3290
3291 LOCAL FUNCTION
3292
3293         create_name -- allocate a fresh copy of a string on an obstack
3294
3295 DESCRIPTION
3296
3297         Given a pointer to a string and a pointer to an obstack, allocates
3298         a fresh copy of the string on the specified obstack.
3299
3300 */
3301
3302 static char *
3303 DEFUN(create_name, (name, obstackp), char *name AND struct obstack *obstackp)
3304 {
3305   int length;
3306   char *newname;
3307
3308   length = strlen (name) + 1;
3309   newname = (char *) obstack_alloc (obstackp, length);
3310   (void) strcpy (newname, name);
3311   return (newname);
3312 }
3313
3314 /*
3315
3316 LOCAL FUNCTION
3317
3318         basicdieinfo -- extract the minimal die info from raw die data
3319
3320 SYNOPSIS
3321
3322         void basicdieinfo (char *diep, struct dieinfo *dip)
3323
3324 DESCRIPTION
3325
3326         Given a pointer to raw DIE data, and a pointer to an instance of a
3327         die info structure, this function extracts the basic information
3328         from the DIE data required to continue processing this DIE, along
3329         with some bookkeeping information about the DIE.
3330
3331         The information we absolutely must have includes the DIE tag,
3332         and the DIE length.  If we need the sibling reference, then we
3333         will have to call completedieinfo() to process all the remaining
3334         DIE information.
3335
3336         Note that since there is no guarantee that the data is properly
3337         aligned in memory for the type of access required (indirection
3338         through anything other than a char pointer), we use memcpy to
3339         shuffle data items larger than a char.  Possibly inefficient, but
3340         quite portable.
3341
3342         We also take care of some other basic things at this point, such
3343         as ensuring that the instance of the die info structure starts
3344         out completely zero'd and that curdie is initialized for use
3345         in error reporting if we have a problem with the current die.
3346
3347 NOTES
3348
3349         All DIE's must have at least a valid length, thus the minimum
3350         DIE size is sizeof (long).  In order to have a valid tag, the
3351         DIE size must be at least sizeof (short) larger, otherwise they
3352         are forced to be TAG_padding DIES.
3353
3354         Padding DIES must be at least sizeof(long) in length, implying that
3355         if a padding DIE is used for alignment and the amount needed is less
3356         than sizeof(long) then the padding DIE has to be big enough to align
3357         to the next alignment boundry.
3358  */
3359
3360 static void
3361 DEFUN(basicdieinfo, (dip, diep), struct dieinfo *dip AND char *diep)
3362 {
3363   curdie = dip;
3364   (void) memset (dip, 0, sizeof (struct dieinfo));
3365   dip -> die = diep;
3366   dip -> dieref = dbroff + (diep - dbbase);
3367   (void) memcpy (&dip -> dielength, diep, sizeof (long));
3368   if (dip -> dielength < sizeof (long))
3369     {
3370       dwarfwarn ("malformed DIE, bad length (%d bytes)", dip -> dielength);
3371     }
3372   else if (dip -> dielength < (sizeof (long) + sizeof (short)))
3373     {
3374       dip -> dietag = TAG_padding;
3375     }
3376   else
3377     {
3378       (void) memcpy (&dip -> dietag, diep + sizeof (long), sizeof (short));
3379     }
3380 }
3381
3382 /*
3383
3384 LOCAL FUNCTION
3385
3386         completedieinfo -- finish reading the information for a given DIE
3387
3388 SYNOPSIS
3389
3390         void completedieinfo (struct dieinfo *dip)
3391
3392 DESCRIPTION
3393
3394         Given a pointer to an already partially initialized die info structure,
3395         scan the raw DIE data and finish filling in the die info structure
3396         from the various attributes found.
3397    
3398         Note that since there is no guarantee that the data is properly
3399         aligned in memory for the type of access required (indirection
3400         through anything other than a char pointer), we use memcpy to
3401         shuffle data items larger than a char.  Possibly inefficient, but
3402         quite portable.
3403
3404 NOTES
3405
3406         Each time we are called, we increment the diecount variable, which
3407         keeps an approximate count of the number of dies processed for
3408         each compilation unit.  This information is presented to the user
3409         if the info_verbose flag is set.
3410
3411  */
3412
3413 static void
3414 DEFUN(completedieinfo, (dip), struct dieinfo *dip)
3415 {
3416   char *diep;                   /* Current pointer into raw DIE data */
3417   char *end;                    /* Terminate DIE scan here */
3418   unsigned short attr;          /* Current attribute being scanned */
3419   unsigned short form;          /* Form of the attribute */
3420   short block2sz;               /* Size of a block2 attribute field */
3421   long block4sz;                /* Size of a block4 attribute field */
3422   
3423   diecount++;
3424   diep = dip -> die;
3425   end = diep + dip -> dielength;
3426   diep += sizeof (long) + sizeof (short);
3427   while (diep < end)
3428     {
3429       (void) memcpy (&attr, diep, sizeof (short));
3430       diep += sizeof (short);
3431       switch (attr)
3432         {
3433         case AT_fund_type:
3434           (void) memcpy (&dip -> at_fund_type, diep, sizeof (short));
3435           break;
3436         case AT_ordering:
3437           (void) memcpy (&dip -> at_ordering, diep, sizeof (short));
3438           break;
3439         case AT_bit_offset:
3440           (void) memcpy (&dip -> at_bit_offset, diep, sizeof (short));
3441           break;
3442         case AT_visibility:
3443           (void) memcpy (&dip -> at_visibility, diep, sizeof (short));
3444           break;
3445         case AT_sibling:
3446           (void) memcpy (&dip -> at_sibling, diep, sizeof (long));
3447           break;
3448         case AT_stmt_list:
3449           (void) memcpy (&dip -> at_stmt_list, diep, sizeof (long));
3450           dip -> at_stmt_list_p = 1;
3451           break;
3452         case AT_low_pc:
3453           (void) memcpy (&dip -> at_low_pc, diep, sizeof (long));
3454           break;
3455         case AT_high_pc:
3456           (void) memcpy (&dip -> at_high_pc, diep, sizeof (long));
3457           break;
3458         case AT_language:
3459           (void) memcpy (&dip -> at_language, diep, sizeof (long));
3460           break;
3461         case AT_user_def_type:
3462           (void) memcpy (&dip -> at_user_def_type, diep, sizeof (long));
3463           break;
3464         case AT_byte_size:
3465           (void) memcpy (&dip -> at_byte_size, diep, sizeof (long));
3466           break;
3467         case AT_bit_size:
3468           (void) memcpy (&dip -> at_bit_size, diep, sizeof (long));
3469           break;
3470         case AT_member:
3471           (void) memcpy (&dip -> at_member, diep, sizeof (long));
3472           break;
3473         case AT_discr:
3474           (void) memcpy (&dip -> at_discr, diep, sizeof (long));
3475           break;
3476         case AT_import:
3477           (void) memcpy (&dip -> at_import, diep, sizeof (long));
3478           break;
3479         case AT_location:
3480           dip -> at_location = diep;
3481           break;
3482         case AT_mod_fund_type:
3483           dip -> at_mod_fund_type = diep;
3484           break;
3485         case AT_subscr_data:
3486           dip -> at_subscr_data = diep;
3487           break;
3488         case AT_mod_u_d_type:
3489           dip -> at_mod_u_d_type = diep;
3490           break;
3491         case AT_deriv_list:
3492           dip -> at_deriv_list = diep;
3493           break;
3494         case AT_element_list:
3495           dip -> at_element_list = diep;
3496           break;
3497         case AT_discr_value:
3498           dip -> at_discr_value = diep;
3499           break;
3500         case AT_string_length:
3501           dip -> at_string_length = diep;
3502           break;
3503         case AT_name:
3504           dip -> at_name = diep;
3505           break;
3506         case AT_comp_dir:
3507           dip -> at_comp_dir = diep;
3508           break;
3509         case AT_producer:
3510           dip -> at_producer = diep;
3511           break;
3512         case AT_loclist:
3513           (void) memcpy (&dip -> at_loclist, diep, sizeof (long));
3514           break;
3515         case AT_frame_base:
3516           (void) memcpy (&dip -> at_frame_base, diep, sizeof (long));
3517           break;
3518         case AT_incomplete:
3519           (void) memcpy (&dip -> at_incomplete, diep, sizeof (short));
3520           break;
3521         case AT_start_scope:
3522           (void) memcpy (&dip -> at_start_scope, diep, sizeof (long));
3523           break;
3524         case AT_stride_size:
3525           (void) memcpy (&dip -> at_stride_size, diep, sizeof (long));
3526           break;
3527         case AT_src_info:
3528           (void) memcpy (&dip -> at_src_info, diep, sizeof (long));
3529           break;
3530         case AT_prototyped:
3531           (void) memcpy (&dip -> at_prototyped, diep, sizeof (short));
3532           break;
3533         case AT_const_data:
3534           dip -> at_const_data = diep;
3535           break;
3536         case AT_is_external:
3537           (void) memcpy (&dip -> at_is_external, diep, sizeof (short));
3538           dip -> at_is_external_p = 1;
3539           break;
3540         default:
3541           /* Found an attribute that we are unprepared to handle.  However
3542              it is specifically one of the design goals of DWARF that
3543              consumers should ignore unknown attributes.  As long as the
3544              form is one that we recognize (so we know how to skip it),
3545              we can just ignore the unknown attribute. */
3546           break;
3547         }
3548       form = attr & 0xF;
3549       switch (form)
3550         {
3551         case FORM_DATA2:
3552           diep += sizeof (short);
3553           break;
3554         case FORM_DATA4:
3555           diep += sizeof (long);
3556           break;
3557         case FORM_DATA8:
3558           diep += 8 * sizeof (char);    /* sizeof (long long) ? */
3559           break;
3560         case FORM_ADDR:
3561         case FORM_REF:
3562           diep += sizeof (long);
3563           break;
3564         case FORM_BLOCK2:
3565           (void) memcpy (&block2sz, diep, sizeof (short));
3566           block2sz += sizeof (short);
3567           diep += block2sz;
3568           break;
3569         case FORM_BLOCK4:
3570           (void) memcpy (&block4sz, diep, sizeof (long));
3571           block4sz += sizeof (long);
3572           diep += block4sz;
3573           break;
3574         case FORM_STRING:
3575           diep += strlen (diep) + 1;
3576           break;
3577         default:
3578           SQUAWK (("unknown attribute form (0x%x), skipped rest", form));
3579           diep = end;
3580           break;
3581         }
3582     }
3583 }