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