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