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