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