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